1 /*******************************************************************************
2 * Copyright (c) 2000, 2017 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
23 * Instances of this class represent a selectable user interface object
24 * corresponding to a tab for a page in a tab folder.
26 * <dt><b>Styles:</b></dt>
28 * <dt><b>Events:</b></dt>
32 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
35 * @see <a href="http://www.eclipse.org/swt/snippets/#tabfolder">TabFolder, TabItem snippets</a>
36 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
37 * @noextend This class is not intended to be subclassed by clients.
39 public class TabItem extends Item {
45 * Constructs a new instance of this class given its parent
46 * (which must be a <code>TabFolder</code>) and a style value
47 * describing its behavior and appearance. The item is added
48 * to the end of the items maintained by its parent.
50 * The style value is either one of the style constants defined in
51 * class <code>SWT</code> which is applicable to instances of this
52 * class, or must be built by <em>bitwise OR</em>'ing together
53 * (that is, using the <code>int</code> "|" operator) two or more
54 * of those <code>SWT</code> style constants. The class description
55 * lists the style constants that are applicable to the class.
56 * Style bits are also inherited from superclasses.
59 * @param parent a composite control which will be the parent of the new instance (cannot be null)
60 * @param style the style of control to construct
62 * @exception IllegalArgumentException <ul>
63 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
65 * @exception SWTException <ul>
66 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
67 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
71 * @see Widget#checkSubclass
72 * @see Widget#getStyle
74 public TabItem (TabFolder parent, int style) {
75 super (parent, style);
77 parent.createItem (this, parent.getItemCount ());
81 * Constructs a new instance of this class given its parent
82 * (which must be a <code>TabFolder</code>), a style value
83 * describing its behavior and appearance, and the index
84 * at which to place it in the items maintained by its parent.
86 * The style value is either one of the style constants defined in
87 * class <code>SWT</code> which is applicable to instances of this
88 * class, or must be built by <em>bitwise OR</em>'ing together
89 * (that is, using the <code>int</code> "|" operator) two or more
90 * of those <code>SWT</code> style constants. The class description
91 * lists the style constants that are applicable to the class.
92 * Style bits are also inherited from superclasses.
95 * @param parent a composite control which will be the parent of the new instance (cannot be null)
96 * @param style the style of control to construct
97 * @param index the zero-relative index to store the receiver in its parent
99 * @exception IllegalArgumentException <ul>
100 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
101 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
103 * @exception SWTException <ul>
104 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
105 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
109 * @see Widget#checkSubclass
110 * @see Widget#getStyle
112 public TabItem (TabFolder parent, int style, int index) {
113 super (parent, style);
114 this.parent = parent;
115 parent.createItem (this, index);
118 void _setText (int index, String string) {
120 * Bug in Windows. In version 6.00 of COMCTL32.DLL, tab
121 * items with an image and a label that includes '&' cause
122 * the tab to draw incorrectly (even when doubled '&&').
123 * The image overlaps the label. The fix is to remove
124 * all '&' characters from the string.
127 if (string.indexOf ('&') != -1) {
128 int length = string.length ();
129 char[] text = new char [length];
130 string.getChars ( 0, length, text, 0);
132 for (i=0; i<length; i++) {
133 if (text[i] != '&') text [j++] = text [i];
135 if (j < i) string = new String (text, 0, j);
138 long hwnd = parent.handle;
139 long hHeap = OS.GetProcessHeap ();
140 TCHAR buffer = new TCHAR (parent.getCodePage (), string, true);
141 int byteCount = buffer.length () * TCHAR.sizeof;
142 long pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
143 OS.MoveMemory (pszText, buffer, byteCount);
144 TCITEM tcItem = new TCITEM ();
145 tcItem.mask = OS.TCIF_TEXT;
146 tcItem.pszText = pszText;
147 OS.SendMessage (hwnd, OS.TCM_SETITEM, index, tcItem);
148 OS.HeapFree (hHeap, 0, pszText);
152 protected void checkSubclass () {
153 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
157 void destroyWidget () {
158 parent.destroyItem (this);
163 * Returns the control that is used to fill the client area of
164 * the tab folder when the user selects the tab item. If no
165 * control has been set, return <code>null</code>.
167 * @return the control
169 * @exception SWTException <ul>
170 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
171 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
174 public Control getControl () {
180 * Returns a rectangle describing the receiver's size and location
181 * relative to its parent.
183 * @return the receiver's bounding rectangle
185 * @exception SWTException <ul>
186 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
187 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
192 public Rectangle getBounds () {
194 return DPIUtil.autoScaleDown(getBoundsInPixels());
197 Rectangle getBoundsInPixels() {
198 int index = parent.indexOf(this);
199 if (index == -1) return new Rectangle (0, 0, 0, 0);
200 RECT itemRect = new RECT ();
201 OS.SendMessage (parent.handle, OS.TCM_GETITEMRECT, index, itemRect);
202 return new Rectangle(itemRect.left, itemRect.top, itemRect.right - itemRect.left, itemRect.bottom - itemRect.top);
206 * Returns the receiver's parent, which must be a <code>TabFolder</code>.
208 * @return the receiver's parent
210 * @exception SWTException <ul>
211 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
212 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
215 public TabFolder getParent () {
221 * Returns the receiver's tool tip text, or null if it has
224 * @return the receiver's tool tip text
226 * @exception SWTException <ul>
227 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
228 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
231 public String getToolTipText () {
237 void releaseHandle () {
238 super.releaseHandle ();
243 void releaseParent () {
244 super.releaseParent ();
245 int index = parent.indexOf (this);
246 if (index == parent.getSelectionIndex ()) {
247 if (control != null) control.setVisible (false);
252 void releaseWidget () {
253 super.releaseWidget ();
258 * Sets the control that is used to fill the client area of
259 * the tab folder when the user selects the tab item.
261 * @param control the new control (or null)
263 * @exception IllegalArgumentException <ul>
264 * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
265 * <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree</li>
267 * @exception SWTException <ul>
268 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
269 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
272 public void setControl (Control control) {
274 if (control != null) {
275 if (control.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
276 if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT);
278 if (this.control != null && this.control.isDisposed ()) {
281 Control oldControl = this.control, newControl = control;
282 this.control = control;
283 int index = parent.indexOf (this), selectionIndex = parent.getSelectionIndex();
284 if (index != selectionIndex) {
285 if (newControl != null) {
286 if (selectionIndex != -1) {
287 Control selectedControl = parent.getItem(selectionIndex).getControl();
288 if (selectedControl == newControl) return;
290 newControl.setVisible(false);
294 if (newControl != null) {
295 newControl.setBounds (parent.getClientAreaInPixels ());
296 newControl.setVisible (true);
298 if (oldControl != null && newControl != null && oldControl != newControl)
299 oldControl.setVisible (false);
303 public void setImage (Image image) {
305 int index = parent.indexOf (this);
306 if (index == -1) return;
307 super.setImage (image);
309 * Bug in Windows. In version 6.00 of COMCTL32.DLL, tab
310 * items with an image and a label that includes '&' cause
311 * the tab to draw incorrectly (even when doubled '&&').
312 * The image overlaps the label. The fix is to remove
313 * all '&' characters from the string and set the text
314 * whenever the image or text is changed.
316 if (text.indexOf ('&') != -1) _setText (index, text);
317 long hwnd = parent.handle;
318 TCITEM tcItem = new TCITEM ();
319 tcItem.mask = OS.TCIF_IMAGE;
320 tcItem.iImage = parent.imageIndex (image);
321 OS.SendMessage (hwnd, OS.TCM_SETITEM, index, tcItem);
324 * Sets the receiver's text. The string may include
325 * the mnemonic character.
327 * Mnemonics are indicated by an '&' that causes the next
328 * character to be the mnemonic. When the user presses a
329 * key sequence that matches the mnemonic, a selection
330 * event occurs. On most platforms, the mnemonic appears
331 * underlined but may be emphasised in a platform specific
332 * manner. The mnemonic indicator character '&' can be
333 * escaped by doubling it in the string, causing a single
334 * '&' to be displayed.
337 * @param string the new text
339 * @exception IllegalArgumentException <ul>
340 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
342 * @exception SWTException <ul>
343 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
344 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
349 public void setText (String string) {
351 if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
352 if (string.equals (text)) return;
353 int index = parent.indexOf (this);
354 if (index == -1) return;
355 super.setText (string);
357 * Need to update direction since it is set via UCC which the new text
360 int textDirection = (state & HAS_AUTO_DIRECTION) != 0 ? AUTO_TEXT_DIRECTION : style & SWT.FLIP_TEXT_DIRECTION;
361 if (!updateTextDirection (textDirection)) {
362 _setText (index, string);
367 boolean updateTextDirection(int textDirection) {
368 /* AUTO is handled by super */
369 if (super.updateTextDirection(textDirection)) {
370 int index = parent.indexOf (this);
372 if ((textDirection & SWT.RIGHT_TO_LEFT) != 0) {
373 _setText(index, RLE + text);
375 } else if ((textDirection & SWT.LEFT_TO_RIGHT) != 0) {
376 _setText(index, LRE + text);
385 * Sets the receiver's tool tip text to the argument, which
386 * may be null indicating that the default tool tip for the
387 * control will be shown. For a control that has a default
388 * tool tip, such as the Tree control on Windows, setting
389 * the tool tip text to an empty string replaces the default,
390 * causing no tool tip text to be shown.
392 * The mnemonic indicator (character '&') is not displayed in a tool tip.
393 * To display a single '&' in the tool tip, the character '&' can be
394 * escaped by doubling it in the string.
397 * NOTE: This operation is a hint and behavior is platform specific, on Windows
398 * for CJK-style mnemonics of the form " (&C)" at the end of the tooltip text
399 * are not shown in tooltip.
402 * @param string the new tool tip text (or null)
404 * @exception SWTException <ul>
405 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
406 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
409 public void setToolTipText (String string) {
411 toolTipText = string;