]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/TabItem.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / TabItem.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2017 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
21
22 /**
23  * Instances of this class represent a selectable user interface object
24  * corresponding to a tab for a page in a tab folder.
25  * <dl>
26  * <dt><b>Styles:</b></dt>
27  * <dd>(none)</dd>
28  * <dt><b>Events:</b></dt>
29  * <dd>(none)</dd>
30  * </dl>
31  * <p>
32  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
33  * </p>
34  *
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.
38  */
39 public class TabItem extends Item {
40         TabFolder parent;
41         Control control;
42         String toolTipText;
43
44 /**
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.
49  * <p>
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.
57  * </p>
58  *
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
61  *
62  * @exception IllegalArgumentException <ul>
63  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
64  * </ul>
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>
68  * </ul>
69  *
70  * @see SWT
71  * @see Widget#checkSubclass
72  * @see Widget#getStyle
73  */
74 public TabItem (TabFolder parent, int style) {
75         super (parent, style);
76         this.parent = parent;
77         parent.createItem (this, parent.getItemCount ());
78 }
79
80 /**
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.
85  * <p>
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.
93  * </p>
94  *
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
98  *
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>
102  * </ul>
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>
106  * </ul>
107  *
108  * @see SWT
109  * @see Widget#checkSubclass
110  * @see Widget#getStyle
111  */
112 public TabItem (TabFolder parent, int style, int index) {
113         super (parent, style);
114         this.parent = parent;
115         parent.createItem (this, index);
116 }
117
118 void _setText (int index, String string) {
119         /*
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.
125         */
126         if (image != null) {
127                 if (string.indexOf ('&') != -1) {
128                         int length = string.length ();
129                         char[] text = new char [length];
130                         string.getChars ( 0, length, text, 0);
131                         int i = 0, j = 0;
132                         for (i=0; i<length; i++) {
133                                 if (text[i] != '&') text [j++] = text [i];
134                         }
135                         if (j < i) string = new String (text, 0, j);
136                 }
137         }
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);
149 }
150
151 @Override
152 protected void checkSubclass () {
153         if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
154 }
155
156 @Override
157 void destroyWidget () {
158         parent.destroyItem (this);
159         releaseHandle ();
160 }
161
162 /**
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>.
166  * <p>
167  * @return the control
168  *
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>
172  * </ul>
173  */
174 public Control getControl () {
175         checkWidget();
176         return control;
177 }
178
179 /**
180  * Returns a rectangle describing the receiver's size and location
181  * relative to its parent.
182  *
183  * @return the receiver's bounding rectangle
184  *
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>
188  * </ul>
189  *
190  * @since 3.4
191  */
192 public Rectangle getBounds () {
193         checkWidget();
194         return DPIUtil.autoScaleDown(getBoundsInPixels());
195 }
196
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);
203 }
204
205 /**
206  * Returns the receiver's parent, which must be a <code>TabFolder</code>.
207  *
208  * @return the receiver's parent
209  *
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>
213  * </ul>
214  */
215 public TabFolder getParent () {
216         checkWidget();
217         return parent;
218 }
219
220 /**
221  * Returns the receiver's tool tip text, or null if it has
222  * not been set.
223  *
224  * @return the receiver's tool tip text
225  *
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>
229  * </ul>
230  */
231 public String getToolTipText () {
232         checkWidget();
233         return toolTipText;
234 }
235
236 @Override
237 void releaseHandle () {
238         super.releaseHandle ();
239         parent = null;
240 }
241
242 @Override
243 void releaseParent () {
244         super.releaseParent ();
245         int index = parent.indexOf (this);
246         if (index == parent.getSelectionIndex ()) {
247                 if (control != null) control.setVisible (false);
248         }
249 }
250
251 @Override
252 void releaseWidget () {
253         super.releaseWidget ();
254         control = null;
255 }
256
257 /**
258  * Sets the control that is used to fill the client area of
259  * the tab folder when the user selects the tab item.
260  * <p>
261  * @param control the new control (or null)
262  *
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>
266  * </ul>
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>
270  * </ul>
271  */
272 public void setControl (Control control) {
273         checkWidget();
274         if (control != null) {
275                 if (control.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
276                 if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT);
277         }
278         if (this.control != null && this.control.isDisposed ()) {
279                 this.control = null;
280         }
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;
289                         }
290                         newControl.setVisible(false);
291                         return;
292                 }
293         }
294         if (newControl != null) {
295                 newControl.setBounds (parent.getClientAreaInPixels ());
296                 newControl.setVisible (true);
297         }
298         if (oldControl != null && newControl != null && oldControl != newControl)
299                 oldControl.setVisible (false);
300 }
301
302 @Override
303 public void setImage (Image image) {
304         checkWidget();
305         int index = parent.indexOf (this);
306         if (index == -1) return;
307         super.setImage (image);
308         /*
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.
315         */
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);
322 }
323 /**
324  * Sets the receiver's text.  The string may include
325  * the mnemonic character.
326  * <p>
327  * Mnemonics are indicated by an '&amp;' 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 '&amp;' can be
333  * escaped by doubling it in the string, causing a single
334  * '&amp;' to be displayed.
335  * </p>
336  *
337  * @param string the new text
338  *
339  * @exception IllegalArgumentException <ul>
340  *    <li>ERROR_NULL_ARGUMENT - if the text is null</li>
341  * </ul>
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>
345  * </ul>
346  *
347  */
348 @Override
349 public void setText (String string) {
350         checkWidget();
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);
356         /*
357          * Need to update direction since it is set via UCC which the new text
358          * overrides
359          */
360         int textDirection = (state & HAS_AUTO_DIRECTION) != 0 ? AUTO_TEXT_DIRECTION : style & SWT.FLIP_TEXT_DIRECTION;
361         if (!updateTextDirection (textDirection)) {
362                 _setText (index, string);
363         }
364 }
365
366 @Override
367 boolean updateTextDirection(int textDirection) {
368         /* AUTO is handled by super */
369         if (super.updateTextDirection(textDirection)) {
370                 int index = parent.indexOf (this);
371                 if (index != -1) {
372                         if ((textDirection & SWT.RIGHT_TO_LEFT) != 0) {
373                                 _setText(index, RLE + text);
374                                 return true;
375                         } else if ((textDirection & SWT.LEFT_TO_RIGHT) != 0) {
376                                 _setText(index, LRE + text);
377                                 return true;
378                         }
379                 }
380         }
381         return false;
382 }
383
384 /**
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.
391  * <p>
392  * The mnemonic indicator (character '&amp;') is not displayed in a tool tip.
393  * To display a single '&amp;' in the tool tip, the character '&amp;' can be
394  * escaped by doubling it in the string.
395  * </p>
396  * <p>
397  * NOTE: This operation is a hint and behavior is platform specific, on Windows
398  * for CJK-style mnemonics of the form " (&amp;C)" at the end of the tooltip text
399  * are not shown in tooltip.
400  * </p>
401  *
402  * @param string the new tool tip text (or null)
403  *
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>
407  * </ul>
408  */
409 public void setToolTipText (String string) {
410         checkWidget();
411         toolTipText = string;
412 }
413
414 }