]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/ToolItem.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / ToolItem.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.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.internal.win32.*;
22
23 /**
24  * Instances of this class represent a selectable user interface object
25  * that represents a button in a tool bar.
26  * <dl>
27  * <dt><b>Styles:</b></dt>
28  * <dd>PUSH, CHECK, RADIO, SEPARATOR, DROP_DOWN</dd>
29  * <dt><b>Events:</b></dt>
30  * <dd>Selection</dd>
31  * </dl>
32  * <p>
33  * Note: Only one of the styles CHECK, PUSH, RADIO, SEPARATOR and DROP_DOWN
34  * may be specified.
35  * </p><p>
36  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
37  * </p>
38  *
39  * @see <a href="http://www.eclipse.org/swt/snippets/#toolbar">ToolBar, ToolItem snippets</a>
40  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
41  * @noextend This class is not intended to be subclassed by clients.
42  */
43 public class ToolItem extends Item {
44         ToolBar parent;
45         Control control;
46         String toolTipText;
47         Image disabledImage, hotImage;
48         Image disabledImage2;
49         int id;
50         short cx;
51
52 /**
53  * Constructs a new instance of this class given its parent
54  * (which must be a <code>ToolBar</code>) and a style value
55  * describing its behavior and appearance. The item is added
56  * to the end of the items maintained by its parent.
57  * <p>
58  * The style value is either one of the style constants defined in
59  * class <code>SWT</code> which is applicable to instances of this
60  * class, or must be built by <em>bitwise OR</em>'ing together
61  * (that is, using the <code>int</code> "|" operator) two or more
62  * of those <code>SWT</code> style constants. The class description
63  * lists the style constants that are applicable to the class.
64  * Style bits are also inherited from superclasses.
65  * </p>
66  *
67  * @param parent a composite control which will be the parent of the new instance (cannot be null)
68  * @param style the style of control to construct
69  *
70  * @exception IllegalArgumentException <ul>
71  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
72  * </ul>
73  * @exception SWTException <ul>
74  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
75  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
76  * </ul>
77  *
78  * @see SWT#PUSH
79  * @see SWT#CHECK
80  * @see SWT#RADIO
81  * @see SWT#SEPARATOR
82  * @see SWT#DROP_DOWN
83  * @see Widget#checkSubclass
84  * @see Widget#getStyle
85  */
86 public ToolItem (ToolBar parent, int style) {
87         super (parent, checkStyle (style));
88         this.parent = parent;
89         parent.createItem (this, parent.getItemCount ());
90 }
91
92 /**
93  * Constructs a new instance of this class given its parent
94  * (which must be a <code>ToolBar</code>), a style value
95  * describing its behavior and appearance, and the index
96  * at which to place it in the items maintained by its parent.
97  * <p>
98  * The style value is either one of the style constants defined in
99  * class <code>SWT</code> which is applicable to instances of this
100  * class, or must be built by <em>bitwise OR</em>'ing together
101  * (that is, using the <code>int</code> "|" operator) two or more
102  * of those <code>SWT</code> style constants. The class description
103  * lists the style constants that are applicable to the class.
104  * Style bits are also inherited from superclasses.
105  * </p>
106  *
107  * @param parent a composite control which will be the parent of the new instance (cannot be null)
108  * @param style the style of control to construct
109  * @param index the zero-relative index to store the receiver in its parent
110  *
111  * @exception IllegalArgumentException <ul>
112  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
113  *    <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
114  * </ul>
115  * @exception SWTException <ul>
116  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
117  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
118  * </ul>
119  *
120  * @see SWT#PUSH
121  * @see SWT#CHECK
122  * @see SWT#RADIO
123  * @see SWT#SEPARATOR
124  * @see SWT#DROP_DOWN
125  * @see Widget#checkSubclass
126  * @see Widget#getStyle
127  */
128 public ToolItem (ToolBar parent, int style, int index) {
129         super (parent, checkStyle (style));
130         this.parent = parent;
131         parent.createItem (this, index);
132 }
133
134 /**
135  * Adds the listener to the collection of listeners who will
136  * be notified when the control is selected by the user, by sending
137  * it one of the messages defined in the <code>SelectionListener</code>
138  * interface.
139  * <p>
140  * When <code>widgetSelected</code> is called when the mouse is over the arrow portion of a drop-down tool,
141  * the event object detail field contains the value <code>SWT.ARROW</code>.
142  * <code>widgetDefaultSelected</code> is not called.
143  * </p>
144  * <p>
145  * When the <code>SWT.RADIO</code> style bit is set, the <code>widgetSelected</code> method is
146  * also called when the receiver loses selection because another item in the same radio group
147  * was selected by the user. During <code>widgetSelected</code> the application can use
148  * <code>getSelection()</code> to determine the current selected state of the receiver.
149  * </p>
150  *
151  * @param listener the listener which should be notified when the control is selected by the user,
152  *
153  * @exception IllegalArgumentException <ul>
154  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
155  * </ul>
156  * @exception SWTException <ul>
157  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
158  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
159  * </ul>
160  *
161  * @see SelectionListener
162  * @see #removeSelectionListener
163  * @see SelectionEvent
164  */
165 public void addSelectionListener(SelectionListener listener) {
166         checkWidget();
167         if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
168         TypedListener typedListener = new TypedListener (listener);
169         addListener (SWT.Selection,typedListener);
170         addListener (SWT.DefaultSelection,typedListener);
171 }
172
173 static int checkStyle (int style) {
174         return checkBits (style, SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, SWT.DROP_DOWN, 0);
175 }
176
177 @Override
178 protected void checkSubclass () {
179         if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
180 }
181
182 void click (boolean dropDown) {
183         long hwnd = parent.handle;
184         if (OS.GetKeyState (OS.VK_LBUTTON) < 0) return;
185         int index = (int)OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0);
186         RECT rect = new RECT ();
187         OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect);
188         int hotIndex = (int)OS.SendMessage (hwnd, OS.TB_GETHOTITEM, 0, 0);
189
190         /*
191         * In order to emulate all the processing that
192         * happens when a mnemonic key is pressed, fake
193         * a mouse press and release.  This will ensure
194         * that radio and pull down items are handled
195         * properly.
196         */
197         int y = rect.top + (rect.bottom - rect.top) / 2;
198         long lParam = OS.MAKELPARAM (dropDown ? rect.right - 1 : rect.left, y);
199         parent.ignoreMouse = true;
200         OS.SendMessage (hwnd, OS.WM_LBUTTONDOWN, 0, lParam);
201         OS.SendMessage (hwnd, OS.WM_LBUTTONUP, 0, lParam);
202         parent.ignoreMouse = false;
203
204         if (hotIndex != -1) {
205                 OS.SendMessage (hwnd, OS.TB_SETHOTITEM, hotIndex, 0);
206         }
207 }
208
209 Widget [] computeTabList () {
210         if (isTabGroup ()) {
211                 if (getEnabled ()) {
212                         if ((style & SWT.SEPARATOR) != 0) {
213                                 if (control != null) return control.computeTabList();
214                         } else {
215                                 return new Widget [] {this};
216                         }
217                 }
218         }
219         return new Widget [0];
220 }
221
222 @Override
223 void destroyWidget () {
224         parent.destroyItem (this);
225         releaseHandle ();
226 }
227
228 /**
229  * Returns a rectangle describing the receiver's size and location
230  * relative to its parent.
231  *
232  * @return the receiver's bounding rectangle
233  *
234  * @exception SWTException <ul>
235  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
236  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
237  * </ul>
238  */
239 public Rectangle getBounds () {
240         checkWidget();
241         return DPIUtil.autoScaleDown(getBoundsInPixels());
242 }
243
244 Rectangle getBoundsInPixels () {
245         long hwnd = parent.handle;
246         int index = (int)OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0);
247         RECT rect = new RECT ();
248         OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect);
249         int width = rect.right - rect.left;
250         int height = rect.bottom - rect.top;
251         return new Rectangle (rect.left, rect.top, width, height);
252 }
253
254 /**
255  * Returns the control that is used to fill the bounds of
256  * the item when the item is a <code>SEPARATOR</code>.
257  *
258  * @return the control
259  *
260  * @exception SWTException <ul>
261  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
262  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
263  * </ul>
264  */
265 public Control getControl () {
266         checkWidget();
267         return control;
268 }
269
270 /**
271  * Returns the receiver's disabled image if it has one, or null
272  * if it does not.
273  * <p>
274  * The disabled image is displayed when the receiver is disabled.
275  * </p>
276  *
277  * @return the receiver's disabled image
278  *
279  * @exception SWTException <ul>
280  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
281  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
282  * </ul>
283  */
284 public Image getDisabledImage () {
285         checkWidget();
286         return disabledImage;
287 }
288
289 /**
290  * Returns <code>true</code> if the receiver is enabled, and
291  * <code>false</code> otherwise. A disabled control is typically
292  * not selectable from the user interface and draws with an
293  * inactive or "grayed" look.
294  *
295  * @return the receiver's enabled state
296  *
297  * @exception SWTException <ul>
298  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
299  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
300  * </ul>
301  *
302  * @see #isEnabled
303  */
304 public boolean getEnabled () {
305         checkWidget();
306         if ((style & SWT.SEPARATOR) != 0) {
307                 return (state & DISABLED) == 0;
308         }
309         long hwnd = parent.handle;
310         long fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0);
311         return (fsState & OS.TBSTATE_ENABLED) != 0;
312 }
313
314 /**
315  * Returns the receiver's hot image if it has one, or null
316  * if it does not.
317  * <p>
318  * The hot image is displayed when the mouse enters the receiver.
319  * </p>
320  *
321  * @return the receiver's hot image
322  *
323  * @exception SWTException <ul>
324  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
325  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
326  * </ul>
327  */
328 public Image getHotImage () {
329         checkWidget();
330         return hotImage;
331 }
332
333 /**
334  * Returns the receiver's enabled image if it has one, or null
335  * if it does not.
336  *
337  * @return the receiver's enabled image
338  *
339  * @exception SWTException <ul>
340  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
341  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
342  * </ul>
343  */
344 @Override
345 public Image getImage () {
346         return super.getImage();
347 }
348
349 /**
350  * Returns the receiver's parent, which must be a <code>ToolBar</code>.
351  *
352  * @return the receiver's parent
353  *
354  * @exception SWTException <ul>
355  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
356  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
357  * </ul>
358  */
359 public ToolBar getParent () {
360         checkWidget();
361         return parent;
362 }
363
364 /**
365  * Returns <code>true</code> if the receiver is selected,
366  * and false otherwise.
367  * <p>
368  * When the receiver is of type <code>CHECK</code> or <code>RADIO</code>,
369  * it is selected when it is checked (which some platforms draw as a
370  * pushed in button). If the receiver is of any other type, this method
371  * returns false.
372  * </p>
373  *
374  * @return the selection state
375  *
376  * @exception SWTException <ul>
377  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
378  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
379  * </ul>
380  */
381 public boolean getSelection () {
382         checkWidget();
383         if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return false;
384         long hwnd = parent.handle;
385         long fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0);
386         return (fsState & OS.TBSTATE_CHECKED) != 0;
387 }
388
389 /**
390  * Returns the receiver's tool tip text, or null if it has not been set.
391  *
392  * @return the receiver's tool tip text
393  *
394  * @exception SWTException <ul>
395  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
396  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
397  * </ul>
398  */
399 public String getToolTipText () {
400         checkWidget();
401         return toolTipText;
402 }
403
404 /**
405  * Gets the width of the receiver.
406  *
407  * @return the width
408  *
409  * @exception SWTException <ul>
410  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
411  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
412  * </ul>
413  */
414 public int getWidth () {
415         checkWidget();
416         return DPIUtil.autoScaleDown(getWidthInPixels());
417 }
418
419 int getWidthInPixels () {
420         long hwnd = parent.handle;
421         int index = (int)OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0);
422         RECT rect = new RECT ();
423         OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect);
424         return rect.right - rect.left;
425 }
426
427 /**
428  * Returns <code>true</code> if the receiver is enabled and all
429  * of the receiver's ancestors are enabled, and <code>false</code>
430  * otherwise. A disabled control is typically not selectable from the
431  * user interface and draws with an inactive or "grayed" look.
432  *
433  * @return the receiver's enabled state
434  *
435  * @exception SWTException <ul>
436  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
437  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
438  * </ul>
439  *
440  * @see #getEnabled
441  */
442 public boolean isEnabled () {
443         checkWidget();
444         return getEnabled () && parent.isEnabled ();
445 }
446
447 boolean isTabGroup () {
448         ToolItem [] tabList = parent._getTabItemList ();
449         if (tabList != null) {
450                 for (int i=0; i<tabList.length; i++) {
451                         if (tabList [i] == this) return true;
452                 }
453         }
454         if ((style & SWT.SEPARATOR) != 0) return true;
455         int index = parent.indexOf (this);
456         if (index == 0) return true;
457         ToolItem previous = parent.getItem (index - 1);
458         return (previous.getStyle () & SWT.SEPARATOR) != 0;
459 }
460
461 @Override
462 void releaseWidget () {
463         super.releaseWidget ();
464         releaseImages ();
465         control = null;
466         toolTipText = null;
467         disabledImage = hotImage = null;
468         if (disabledImage2 != null) disabledImage2.dispose ();
469         disabledImage2 = null;
470 }
471
472 @Override
473 void releaseHandle () {
474         super.releaseHandle ();
475         parent = null;
476         id = -1;
477 }
478
479 void releaseImages () {
480         TBBUTTONINFO info = new TBBUTTONINFO ();
481         info.cbSize = TBBUTTONINFO.sizeof;
482         info.dwMask = OS.TBIF_IMAGE | OS.TBIF_STYLE;
483         long hwnd = parent.handle;
484         OS.SendMessage (hwnd, OS.TB_GETBUTTONINFO, id, info);
485         /*
486         * Feature in Windows.  For some reason, a tool item that has
487         * the style BTNS_SEP does not return I_IMAGENONE when queried
488         * for an image index, despite the fact that no attempt has been
489         * made to assign an image to the item.  As a result, operations
490         * on an image list that use the wrong index cause random results.
491         * The fix is to ensure that the tool item is not a separator
492         * before using the image index.  Since separators cannot have
493         * an image and one is never assigned, this is not a problem.
494         */
495         if ((info.fsStyle & OS.BTNS_SEP) == 0 && info.iImage != OS.I_IMAGENONE) {
496                 ImageList imageList = parent.getImageList ();
497                 ImageList hotImageList = parent.getHotImageList ();
498                 ImageList disabledImageList = parent.getDisabledImageList();
499                 if (imageList != null) imageList.put (info.iImage, null);
500                 if (hotImageList != null) hotImageList.put (info.iImage, null);
501                 if (disabledImageList != null) disabledImageList.put (info.iImage, null);
502         }
503 }
504
505 /**
506  * Removes the listener from the collection of listeners who will
507  * be notified when the control is selected by the user.
508  *
509  * @param listener the listener which should no longer be notified
510  *
511  * @exception IllegalArgumentException <ul>
512  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
513  * </ul>
514  * @exception SWTException <ul>
515  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
516  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
517  * </ul>
518  *
519  * @see SelectionListener
520  * @see #addSelectionListener
521  */
522 public void removeSelectionListener(SelectionListener listener) {
523         checkWidget();
524         if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
525         if (eventTable == null) return;
526         eventTable.unhook (SWT.Selection, listener);
527         eventTable.unhook (SWT.DefaultSelection,listener);
528 }
529
530 void resizeControl () {
531         if (control != null && !control.isDisposed ()) {
532                 /*
533                 * Set the size and location of the control
534                 * separately to minimize flashing in the
535                 * case where the control does not resize
536                 * to the size that was requested.  This
537                 * case can occur when the control is a
538                 * combo box.
539                 */
540                 Rectangle itemRect = getBounds ();
541                 control.setSize (itemRect.width, itemRect.height);
542                 Rectangle rect = control.getBounds ();
543                 rect.x = itemRect.x + (itemRect.width - rect.width) / 2;
544                 rect.y = itemRect.y + (itemRect.height - rect.height) / 2;
545                 control.setLocation (rect.x, rect.y);
546         }
547 }
548
549 void selectRadio () {
550         int index = 0;
551         ToolItem [] items = parent.getItems ();
552         while (index < items.length && items [index] != this) index++;
553         int i = index - 1;
554         while (i >= 0 && items [i].setRadioSelection (false)) --i;
555         int j = index + 1;
556         while (j < items.length && items [j].setRadioSelection (false)) j++;
557         setSelection (true);
558 }
559
560 /**
561  * Sets the control that is used to fill the bounds of
562  * the item when the item is a <code>SEPARATOR</code>.
563  *
564  * @param control the new control
565  *
566  * @exception IllegalArgumentException <ul>
567  *    <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
568  *    <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree</li>
569  * </ul>
570  * @exception SWTException <ul>
571  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
572  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
573  * </ul>
574  */
575 public void setControl (Control control) {
576         checkWidget();
577         if (control != null) {
578                 if (control.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
579                 if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT);
580         }
581         if ((style & SWT.SEPARATOR) == 0) return;
582         this.control = control;
583         /*
584         * Feature in Windows.  When a tool bar wraps, tool items
585         * with the style BTNS_SEP are used as wrap points.  This
586         * means that controls that are placed on top of separator
587         * items are not positioned properly.  Also, vertical tool
588         * bars are implemented using TB_SETROWS to set the number
589         * of rows.  When a control is placed on top of a separator,
590         * the height of the separator does not grow.  The fix in
591         * both cases is to change the tool item style from BTNS_SEP
592         * to BTNS_BUTTON, causing the item to wrap like a tool item
593         * button.  The new tool item button is disabled to avoid key
594         * traversal and the image is set to I_IMAGENONE to avoid
595         * getting the first image from the image list.
596         */
597         if ((parent.style & (SWT.WRAP | SWT.VERTICAL)) != 0) {
598                 boolean changed = false;
599                 long hwnd = parent.handle;
600                 TBBUTTONINFO info = new TBBUTTONINFO ();
601                 info.cbSize = TBBUTTONINFO.sizeof;
602                 info.dwMask = OS.TBIF_STYLE | OS.TBIF_STATE;
603                 OS.SendMessage (hwnd, OS.TB_GETBUTTONINFO, id, info);
604                 if (control == null) {
605                         if ((info.fsStyle & OS.BTNS_SEP) == 0) {
606                                 changed = true;
607                                 info.fsStyle &= ~(OS.BTNS_BUTTON | OS.BTNS_SHOWTEXT);
608                                 info.fsStyle |= OS.BTNS_SEP;
609                                 if ((state & DISABLED) != 0) {
610                                         info.fsState &= ~OS.TBSTATE_ENABLED;
611                                 } else {
612                                         info.fsState |= OS.TBSTATE_ENABLED;
613                                 }
614                         }
615                 } else {
616                         if ((info.fsStyle & OS.BTNS_SEP) != 0) {
617                                 changed = true;
618                                 info.fsStyle &= ~OS.BTNS_SEP;
619                                 info.fsStyle |= OS.BTNS_BUTTON | OS.BTNS_SHOWTEXT;
620                                 info.fsState &= ~OS.TBSTATE_ENABLED;
621                                 info.dwMask |= OS.TBIF_IMAGE;
622                                 info.iImage = OS.I_IMAGENONE;
623                         }
624                 }
625                 if (changed) {
626                         OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info);
627                         /*
628                         * Bug in Windows.  When TB_SETBUTTONINFO changes the
629                         * style of a tool item from BTNS_SEP to BTNS_BUTTON
630                         * and the tool bar is wrapped, the tool bar does not
631                         * redraw properly.  Windows uses separator items as
632                         * wrap points and sometimes draws etching above or
633                         * below and entire row.  The fix is to redraw the
634                         * tool bar.
635                         */
636                         if (OS.SendMessage (hwnd, OS.TB_GETROWS, 0, 0) > 1) {
637                                 OS.InvalidateRect (hwnd, null, true);
638                         }
639                 }
640         }
641         resizeControl ();
642 }
643
644 /**
645  * Enables the receiver if the argument is <code>true</code>,
646  * and disables it otherwise.
647  * <p>
648  * A disabled control is typically
649  * not selectable from the user interface and draws with an
650  * inactive or "grayed" look.
651  * </p>
652  *
653  * @param enabled the new enabled state
654  *
655  * @exception SWTException <ul>
656  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
657  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
658  * </ul>
659  */
660 public void setEnabled (boolean enabled) {
661         checkWidget();
662         long hwnd = parent.handle;
663         int fsState = (int)OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0);
664         /*
665         * Feature in Windows.  When TB_SETSTATE is used to set the
666         * state of a tool item, the item redraws even when the state
667         * has not changed.  The fix is to detect this case and avoid
668         * setting the state.
669         */
670         if (((fsState & OS.TBSTATE_ENABLED) != 0) == enabled) return;
671         if (enabled) {
672                 fsState |= OS.TBSTATE_ENABLED;
673                 state &= ~DISABLED;
674         } else {
675                 fsState &= ~OS.TBSTATE_ENABLED;
676                 state |= DISABLED;
677         }
678         OS.SendMessage (hwnd, OS.TB_SETSTATE, id, fsState);
679         if ((style & SWT.SEPARATOR) == 0) {
680                 if (image != null) updateImages (enabled && parent.getEnabled ());
681         }
682         if (!enabled && parent.lastFocusId == id) {
683                 parent.lastFocusId = -1;
684         }
685 }
686
687 /**
688  * Sets the receiver's disabled image to the argument, which may be
689  * null indicating that no disabled image should be displayed.
690  * <p>
691  * The disabled image is displayed when the receiver is disabled.
692  * </p>
693  *
694  * @param image the disabled image to display on the receiver (may be null)
695  *
696  * @exception IllegalArgumentException <ul>
697  *    <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
698  * </ul>
699  * @exception SWTException <ul>
700  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
701  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
702  * </ul>
703  */
704 public void setDisabledImage (Image image) {
705         checkWidget();
706         if ((style & SWT.SEPARATOR) != 0) return;
707         if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
708         disabledImage = image;
709         updateImages (getEnabled () && parent.getEnabled ());
710 }
711
712 /**
713  * Sets the receiver's hot image to the argument, which may be
714  * null indicating that no hot image should be displayed.
715  * <p>
716  * The hot image is displayed when the mouse enters the receiver.
717  * </p>
718  *
719  * @param image the hot image to display on the receiver (may be null)
720  *
721  * @exception IllegalArgumentException <ul>
722  *    <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
723  * </ul>
724  * @exception SWTException <ul>
725  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
726  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
727  * </ul>
728  */
729 public void setHotImage (Image image) {
730         checkWidget();
731         if ((style & SWT.SEPARATOR) != 0) return;
732         if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
733         hotImage = image;
734         updateImages (getEnabled () && parent.getEnabled ());
735 }
736
737 @Override
738 public void setImage (Image image) {
739         checkWidget();
740         if ((style & SWT.SEPARATOR) != 0) return;
741         if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
742         super.setImage (image);
743         updateImages (getEnabled () && parent.getEnabled ());
744 }
745
746 boolean setRadioSelection (boolean value) {
747         if ((style & SWT.RADIO) == 0) return false;
748         if (getSelection () != value) {
749                 setSelection (value);
750                 sendSelectionEvent (SWT.Selection);
751         }
752         return true;
753 }
754
755 /**
756  * Sets the selection state of the receiver.
757  * <p>
758  * When the receiver is of type <code>CHECK</code> or <code>RADIO</code>,
759  * it is selected when it is checked (which some platforms draw as a
760  * pushed in button).
761  * </p>
762  *
763  * @param selected the new selection state
764  *
765  * @exception SWTException <ul>
766  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
767  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
768  * </ul>
769  */
770 public void setSelection (boolean selected) {
771         checkWidget();
772         if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return;
773         long hwnd = parent.handle;
774         int fsState = (int)OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0);
775         /*
776         * Feature in Windows.  When TB_SETSTATE is used to set the
777         * state of a tool item, the item redraws even when the state
778         * has not changed.  The fix is to detect this case and avoid
779         * setting the state.
780         */
781         if (((fsState & OS.TBSTATE_CHECKED) != 0) == selected) return;
782         if (selected) {
783                 fsState |= OS.TBSTATE_CHECKED;
784         } else {
785                 fsState &= ~OS.TBSTATE_CHECKED;
786         }
787         OS.SendMessage (hwnd, OS.TB_SETSTATE, id, fsState);
788
789         /*
790         * Bug in Windows.  When a tool item with the style
791         * BTNS_CHECK or BTNS_CHECKGROUP is selected and then
792         * disabled, the item does not draw using the disabled
793         * image.  The fix is to use the disabled image in all
794         * image lists for the item.
795         *
796         * NOTE: This means that the image list must be updated
797         * when the selection changes in a disabled tool item.
798         */
799         if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
800                 if (!getEnabled () || !parent.getEnabled ()) {
801                         updateImages (false);
802                 }
803         }
804 }
805
806 @Override
807 boolean setTabItemFocus () {
808         if (parent.setTabItemFocus ()) {
809                 long hwnd = parent.handle;
810                 int index = (int)OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0);
811                 OS.SendMessage (hwnd, OS.TB_SETHOTITEM, index, 0);
812                 return true;
813         }
814         return false;
815 }
816
817 void _setText (String string) {
818         long hwnd = parent.handle;
819         TBBUTTONINFO info = new TBBUTTONINFO ();
820         info.cbSize = TBBUTTONINFO.sizeof;
821         info.dwMask = OS.TBIF_TEXT | OS.TBIF_STYLE;
822         info.fsStyle = (byte) (widgetStyle () | OS.BTNS_AUTOSIZE);
823         long hHeap = OS.GetProcessHeap (), pszText = 0;
824         if (string.length () != 0) {
825                 info.fsStyle |= OS.BTNS_SHOWTEXT;
826                 TCHAR buffer;
827                 if ((style & SWT.FLIP_TEXT_DIRECTION) != 0) {
828                         int bits  = OS.GetWindowLong (hwnd, OS.GWL_EXSTYLE);
829                         if ((bits & OS.WS_EX_LAYOUTRTL) != 0) {
830                                 buffer = new TCHAR (parent.getCodePage (), LRE + string, true);
831                         } else {
832                                 buffer = new TCHAR (parent.getCodePage (), RLE + string, true);
833                         }
834                 } else {
835                         buffer = new TCHAR(parent.getCodePage (), string, true);
836                 }
837                 int byteCount = buffer.length () * TCHAR.sizeof;
838                 pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
839                 OS.MoveMemory (pszText, buffer, byteCount);
840                 info.pszText = pszText;
841         }
842         OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info);
843         if (pszText != 0) OS.HeapFree (hHeap, 0, pszText);
844 }
845
846 /**
847  * Sets the receiver's text. The string may include
848  * the mnemonic character.
849  * <p>
850  * Mnemonics are indicated by an '&amp;' that causes the next
851  * character to be the mnemonic.  When the user presses a
852  * key sequence that matches the mnemonic, a selection
853  * event occurs. On most platforms, the mnemonic appears
854  * underlined but may be emphasised in a platform specific
855  * manner.  The mnemonic indicator character '&amp;' can be
856  * escaped by doubling it in the string, causing a single
857  * '&amp;' to be displayed.
858  * </p><p>
859  * Note: If control characters like '\n', '\t' etc. are used
860  * in the string, then the behavior is platform dependent.
861  * </p>
862  *
863  * @param string the new text
864  *
865  * @exception IllegalArgumentException <ul>
866  *    <li>ERROR_NULL_ARGUMENT - if the text is null</li>
867  * </ul>
868  * @exception SWTException <ul>
869  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
870  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
871  * </ul>
872  */
873 @Override
874 public void setText (String string) {
875         checkWidget();
876         if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
877         if ((style & SWT.SEPARATOR) != 0) return;
878         if (string.equals (text)) return;
879         super.setText (string);
880         if ((state & HAS_AUTO_DIRECTION) == 0 || !updateTextDirection (AUTO_TEXT_DIRECTION)) {
881                 _setText (string);
882         }
883         /*
884         * Bug in Windows.  For some reason, when the font is set
885         * before any tool item has text, the tool items resize to
886         * a very small size.  Also, a tool item will only show text
887         * when text has already been set on one item and then a new
888         * item is created.  The fix is to use WM_SETFONT to force
889         * the tool bar to redraw and layout.
890         */
891         parent.setDropDownItems (false);
892         long hwnd = parent.handle;
893         long hFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0);
894         OS.SendMessage (hwnd, OS.WM_SETFONT, hFont, 0);
895         parent.setDropDownItems (true);
896         parent.layoutItems ();
897 }
898
899 @Override
900 boolean updateTextDirection(int textDirection) {
901         /* AUTO is handled by super */
902         if (super.updateTextDirection(textDirection) && text.length() != 0) {
903                 _setText (text);
904                 return true;
905         }
906         return false;
907 }
908
909 /**
910  * Sets the receiver's tool tip text to the argument, which
911  * may be null indicating that the default tool tip for the
912  * control will be shown. For a control that has a default
913  * tool tip, such as the Tree control on Windows, setting
914  * the tool tip text to an empty string replaces the default,
915  * causing no tool tip text to be shown.
916  * <p>
917  * The mnemonic indicator (character '&amp;') is not displayed in a tool tip.
918  * To display a single '&amp;' in the tool tip, the character '&amp;' can be
919  * escaped by doubling it in the string.
920  * </p>
921  * <p>
922  * NOTE: This operation is a hint and behavior is platform specific, on Windows
923  * for CJK-style mnemonics of the form " (&amp;C)" at the end of the tooltip text
924  * are not shown in tooltip.
925  * </p>
926  *
927  * @param string the new tool tip text (or null)
928  *
929  * @exception SWTException <ul>
930  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
931  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
932  * </ul>
933  */
934 public void setToolTipText (String string) {
935         checkWidget();
936         toolTipText = string;
937 }
938
939 /**
940  * Sets the width of the receiver, for <code>SEPARATOR</code> ToolItems.
941  *
942  * @param width the new width. If the new value is <code>SWT.DEFAULT</code>,
943  * the width is a fixed-width area whose amount is determined by the platform.
944  * If the new value is 0 a vertical or horizontal line will be drawn, depending
945  * on the setting of the corresponding style bit (<code>SWT.VERTICAL</code> or
946  * <code>SWT.HORIZONTAL</code>). If the new value is <code>SWT.SEPARATOR_FILL</code>
947  * a variable-width space is inserted that acts as a spring between the two adjoining
948  * items which will push them out to the extent of the containing ToolBar.
949  *
950  *
951  * @exception SWTException <ul>
952  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
953  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
954  * </ul>
955  */
956 public void setWidth (int width) {
957         checkWidget();
958         setWidthInPixels(DPIUtil.autoScaleUp(width));
959 }
960
961 void setWidthInPixels (int width) {
962         if ((style & SWT.SEPARATOR) == 0) return;
963         if (width < 0) return;
964         long hwnd = parent.handle;
965         TBBUTTONINFO info = new TBBUTTONINFO ();
966         info.cbSize = TBBUTTONINFO.sizeof;
967         info.dwMask = OS.TBIF_SIZE;
968         info.cx = cx = (short) width;
969         OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info);
970         parent.layoutItems ();
971 }
972
973 void updateImages (boolean enabled) {
974         if ((style & SWT.SEPARATOR) != 0) return;
975         long hwnd = parent.handle;
976         TBBUTTONINFO info = new TBBUTTONINFO ();
977         info.cbSize = TBBUTTONINFO.sizeof;
978         info.dwMask = OS.TBIF_IMAGE;
979         OS.SendMessage (hwnd, OS.TB_GETBUTTONINFO, id, info);
980         if (info.iImage == OS.I_IMAGENONE && image == null) return;
981         ImageList imageList = parent.getImageList ();
982         ImageList hotImageList = parent.getHotImageList ();
983         ImageList disabledImageList = parent.getDisabledImageList();
984         if (info.iImage == OS.I_IMAGENONE) {
985                 Rectangle bounds = image.getBoundsInPixels ();
986                 int listStyle = parent.style & SWT.RIGHT_TO_LEFT;
987                 if (imageList == null) {
988                         imageList = display.getImageListToolBar (listStyle, bounds.width, bounds.height);
989                 }
990                 if (disabledImageList == null) {
991                         disabledImageList = display.getImageListToolBarDisabled (listStyle, bounds.width, bounds.height);
992                 }
993                 if (hotImageList == null) {
994                         hotImageList = display.getImageListToolBarHot (listStyle, bounds.width, bounds.height);
995                 }
996                 Image disabled = disabledImage;
997                 if (disabledImage == null) {
998                         if (disabledImage2 != null) disabledImage2.dispose ();
999                         disabledImage2 = null;
1000                         disabled = image;
1001                         if (!enabled) {
1002                                 disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE);
1003                         }
1004                 }
1005                 /*
1006                 * Bug in Windows.  When a tool item with the style
1007                 * BTNS_CHECK or BTNS_CHECKGROUP is selected and then
1008                 * disabled, the item does not draw using the disabled
1009                 * image.  The fix is to assign the disabled image in
1010                 * all image lists.
1011                 */
1012                 Image image2 = image, hot = hotImage;
1013                 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
1014                         if (!enabled) image2 = hot = disabled;
1015                 }
1016                 info.iImage = imageList.add (image2);
1017                 disabledImageList.add (disabled);
1018                 hotImageList.add (hot != null ? hot : image2);
1019                 parent.setImageList (imageList);
1020                 parent.setDisabledImageList (disabledImageList);
1021                 parent.setHotImageList (hotImageList);
1022         } else {
1023                 Image disabled = null;
1024                 if (disabledImageList != null) {
1025                         if (image != null) {
1026                                 if (disabledImage2 != null) disabledImage2.dispose ();
1027                                 disabledImage2 = null;
1028                                 disabled = disabledImage;
1029                                 if (disabledImage == null) {
1030                                         disabled = image;
1031                                         if (!enabled) {
1032                                                 disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE);
1033                                         }
1034                                 }
1035                         }
1036                         disabledImageList.put (info.iImage, disabled);
1037                 }
1038                 /*
1039                 * Bug in Windows.  When a tool item with the style
1040                 * BTNS_CHECK or BTNS_CHECKGROUP is selected and then
1041                 * disabled, the item does not draw using the disabled
1042                 * image.  The fix is to use the disabled image in all
1043                 * image lists.
1044                 */
1045                 Image image2 = image, hot = hotImage;
1046                 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
1047                         if (!enabled) image2 = hot = disabled;
1048                 }
1049                 if (imageList != null) imageList.put (info.iImage, image2);
1050                 if (hotImageList != null) {
1051                         hotImageList.put (info.iImage, hot != null ? hot : image2);
1052                 }
1053                 if (image == null) info.iImage = OS.I_IMAGENONE;
1054         }
1055
1056         /*
1057         * Bug in Windows.  If the width of an item has already been
1058         * calculated, the tool bar control will not recalculate it to
1059         * include the space for the image.  The fix is to set the width
1060         * to zero, forcing the control recalculate the width for the item.
1061         */
1062         info.dwMask |= OS.TBIF_SIZE;
1063         info.cx = 0;
1064         OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info);
1065         long hFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0);
1066         OS.SendMessage (hwnd, OS.WM_SETFONT, hFont, 0);
1067         parent.layoutItems ();
1068 }
1069
1070 int widgetStyle () {
1071         if ((style & SWT.DROP_DOWN) != 0) return OS.BTNS_DROPDOWN;
1072         if ((style & SWT.PUSH) != 0) return OS.BTNS_BUTTON;
1073         if ((style & SWT.CHECK) != 0) return OS.BTNS_CHECK;
1074         /*
1075         * This code is intentionally commented.  In order to
1076         * consistently support radio tool items across platforms,
1077         * the platform radio behavior is not used.
1078         */
1079 //      if ((style & SWT.RADIO) != 0) return OS.BTNS_CHECKGROUP;
1080         if ((style & SWT.RADIO) != 0) return OS.BTNS_CHECK;
1081         if ((style & SWT.SEPARATOR) != 0) return OS.BTNS_SEP;
1082         return OS.BTNS_BUTTON;
1083 }
1084
1085 LRESULT wmCommandChild (long wParam, long lParam) {
1086         if ((style & SWT.RADIO) != 0) {
1087                 if ((parent.getStyle () & SWT.NO_RADIO_GROUP) == 0) {
1088                         selectRadio ();
1089                 }
1090         }
1091         sendSelectionEvent (SWT.Selection);
1092         return null;
1093 }
1094
1095 }