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.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.internal.win32.*;
24 * Instances of this class represent a selectable user interface object
25 * that represents a button in a tool bar.
27 * <dt><b>Styles:</b></dt>
28 * <dd>PUSH, CHECK, RADIO, SEPARATOR, DROP_DOWN</dd>
29 * <dt><b>Events:</b></dt>
33 * Note: Only one of the styles CHECK, PUSH, RADIO, SEPARATOR and DROP_DOWN
36 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
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.
43 public class ToolItem extends Item {
47 Image disabledImage, hotImage;
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.
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.
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
70 * @exception IllegalArgumentException <ul>
71 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
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>
83 * @see Widget#checkSubclass
84 * @see Widget#getStyle
86 public ToolItem (ToolBar parent, int style) {
87 super (parent, checkStyle (style));
89 parent.createItem (this, parent.getItemCount ());
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.
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.
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
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>
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>
125 * @see Widget#checkSubclass
126 * @see Widget#getStyle
128 public ToolItem (ToolBar parent, int style, int index) {
129 super (parent, checkStyle (style));
130 this.parent = parent;
131 parent.createItem (this, index);
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>
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.
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.
151 * @param listener the listener which should be notified when the control is selected by the user,
153 * @exception IllegalArgumentException <ul>
154 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
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>
161 * @see SelectionListener
162 * @see #removeSelectionListener
163 * @see SelectionEvent
165 public void addSelectionListener(SelectionListener listener) {
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);
173 static int checkStyle (int style) {
174 return checkBits (style, SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, SWT.DROP_DOWN, 0);
178 protected void checkSubclass () {
179 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
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);
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
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;
204 if (hotIndex != -1) {
205 OS.SendMessage (hwnd, OS.TB_SETHOTITEM, hotIndex, 0);
209 Widget [] computeTabList () {
212 if ((style & SWT.SEPARATOR) != 0) {
213 if (control != null) return control.computeTabList();
215 return new Widget [] {this};
219 return new Widget [0];
223 void destroyWidget () {
224 parent.destroyItem (this);
229 * Returns a rectangle describing the receiver's size and location
230 * relative to its parent.
232 * @return the receiver's bounding rectangle
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>
239 public Rectangle getBounds () {
241 return DPIUtil.autoScaleDown(getBoundsInPixels());
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);
255 * Returns the control that is used to fill the bounds of
256 * the item when the item is a <code>SEPARATOR</code>.
258 * @return the control
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>
265 public Control getControl () {
271 * Returns the receiver's disabled image if it has one, or null
274 * The disabled image is displayed when the receiver is disabled.
277 * @return the receiver's disabled image
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>
284 public Image getDisabledImage () {
286 return disabledImage;
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.
295 * @return the receiver's enabled state
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>
304 public boolean getEnabled () {
306 if ((style & SWT.SEPARATOR) != 0) {
307 return (state & DISABLED) == 0;
309 long hwnd = parent.handle;
310 long fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0);
311 return (fsState & OS.TBSTATE_ENABLED) != 0;
315 * Returns the receiver's hot image if it has one, or null
318 * The hot image is displayed when the mouse enters the receiver.
321 * @return the receiver's hot image
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>
328 public Image getHotImage () {
334 * Returns the receiver's enabled image if it has one, or null
337 * @return the receiver's enabled image
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>
345 public Image getImage () {
346 return super.getImage();
350 * Returns the receiver's parent, which must be a <code>ToolBar</code>.
352 * @return the receiver's parent
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>
359 public ToolBar getParent () {
365 * Returns <code>true</code> if the receiver is selected,
366 * and false otherwise.
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
374 * @return the selection state
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>
381 public boolean getSelection () {
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;
390 * Returns the receiver's tool tip text, or null if it has not been set.
392 * @return the receiver's tool tip text
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>
399 public String getToolTipText () {
405 * Gets the width of the receiver.
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>
414 public int getWidth () {
416 return DPIUtil.autoScaleDown(getWidthInPixels());
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;
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.
433 * @return the receiver's enabled state
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>
442 public boolean isEnabled () {
444 return getEnabled () && parent.isEnabled ();
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;
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;
462 void releaseWidget () {
463 super.releaseWidget ();
467 disabledImage = hotImage = null;
468 if (disabledImage2 != null) disabledImage2.dispose ();
469 disabledImage2 = null;
473 void releaseHandle () {
474 super.releaseHandle ();
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);
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.
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);
506 * Removes the listener from the collection of listeners who will
507 * be notified when the control is selected by the user.
509 * @param listener the listener which should no longer be notified
511 * @exception IllegalArgumentException <ul>
512 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
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>
519 * @see SelectionListener
520 * @see #addSelectionListener
522 public void removeSelectionListener(SelectionListener listener) {
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);
530 void resizeControl () {
531 if (control != null && !control.isDisposed ()) {
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
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);
549 void selectRadio () {
551 ToolItem [] items = parent.getItems ();
552 while (index < items.length && items [index] != this) index++;
554 while (i >= 0 && items [i].setRadioSelection (false)) --i;
556 while (j < items.length && items [j].setRadioSelection (false)) j++;
561 * Sets the control that is used to fill the bounds of
562 * the item when the item is a <code>SEPARATOR</code>.
564 * @param control the new control
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>
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>
575 public void setControl (Control control) {
577 if (control != null) {
578 if (control.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
579 if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT);
581 if ((style & SWT.SEPARATOR) == 0) return;
582 this.control = control;
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.
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) {
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;
612 info.fsState |= OS.TBSTATE_ENABLED;
616 if ((info.fsStyle & OS.BTNS_SEP) != 0) {
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;
626 OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info);
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
636 if (OS.SendMessage (hwnd, OS.TB_GETROWS, 0, 0) > 1) {
637 OS.InvalidateRect (hwnd, null, true);
645 * Enables the receiver if the argument is <code>true</code>,
646 * and disables it otherwise.
648 * A disabled control is typically
649 * not selectable from the user interface and draws with an
650 * inactive or "grayed" look.
653 * @param enabled the new enabled state
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>
660 public void setEnabled (boolean enabled) {
662 long hwnd = parent.handle;
663 int fsState = (int)OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0);
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
670 if (((fsState & OS.TBSTATE_ENABLED) != 0) == enabled) return;
672 fsState |= OS.TBSTATE_ENABLED;
675 fsState &= ~OS.TBSTATE_ENABLED;
678 OS.SendMessage (hwnd, OS.TB_SETSTATE, id, fsState);
679 if ((style & SWT.SEPARATOR) == 0) {
680 if (image != null) updateImages (enabled && parent.getEnabled ());
682 if (!enabled && parent.lastFocusId == id) {
683 parent.lastFocusId = -1;
688 * Sets the receiver's disabled image to the argument, which may be
689 * null indicating that no disabled image should be displayed.
691 * The disabled image is displayed when the receiver is disabled.
694 * @param image the disabled image to display on the receiver (may be null)
696 * @exception IllegalArgumentException <ul>
697 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
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>
704 public void setDisabledImage (Image image) {
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 ());
713 * Sets the receiver's hot image to the argument, which may be
714 * null indicating that no hot image should be displayed.
716 * The hot image is displayed when the mouse enters the receiver.
719 * @param image the hot image to display on the receiver (may be null)
721 * @exception IllegalArgumentException <ul>
722 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
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>
729 public void setHotImage (Image image) {
731 if ((style & SWT.SEPARATOR) != 0) return;
732 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
734 updateImages (getEnabled () && parent.getEnabled ());
738 public void setImage (Image image) {
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 ());
746 boolean setRadioSelection (boolean value) {
747 if ((style & SWT.RADIO) == 0) return false;
748 if (getSelection () != value) {
749 setSelection (value);
750 sendSelectionEvent (SWT.Selection);
756 * Sets the selection state of the receiver.
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
763 * @param selected the new selection state
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>
770 public void setSelection (boolean selected) {
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);
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
781 if (((fsState & OS.TBSTATE_CHECKED) != 0) == selected) return;
783 fsState |= OS.TBSTATE_CHECKED;
785 fsState &= ~OS.TBSTATE_CHECKED;
787 OS.SendMessage (hwnd, OS.TB_SETSTATE, id, fsState);
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.
796 * NOTE: This means that the image list must be updated
797 * when the selection changes in a disabled tool item.
799 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
800 if (!getEnabled () || !parent.getEnabled ()) {
801 updateImages (false);
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);
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;
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);
832 buffer = new TCHAR (parent.getCodePage (), RLE + string, true);
835 buffer = new TCHAR(parent.getCodePage (), string, true);
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;
842 OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info);
843 if (pszText != 0) OS.HeapFree (hHeap, 0, pszText);
847 * Sets the receiver's text. The string may include
848 * the mnemonic character.
850 * Mnemonics are indicated by an '&' 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 '&' can be
856 * escaped by doubling it in the string, causing a single
857 * '&' to be displayed.
859 * Note: If control characters like '\n', '\t' etc. are used
860 * in the string, then the behavior is platform dependent.
863 * @param string the new text
865 * @exception IllegalArgumentException <ul>
866 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
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>
874 public void setText (String string) {
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)) {
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.
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 ();
900 boolean updateTextDirection(int textDirection) {
901 /* AUTO is handled by super */
902 if (super.updateTextDirection(textDirection) && text.length() != 0) {
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.
917 * The mnemonic indicator (character '&') is not displayed in a tool tip.
918 * To display a single '&' in the tool tip, the character '&' can be
919 * escaped by doubling it in the string.
922 * NOTE: This operation is a hint and behavior is platform specific, on Windows
923 * for CJK-style mnemonics of the form " (&C)" at the end of the tooltip text
924 * are not shown in tooltip.
927 * @param string the new tool tip text (or null)
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>
934 public void setToolTipText (String string) {
936 toolTipText = string;
940 * Sets the width of the receiver, for <code>SEPARATOR</code> ToolItems.
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.
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>
956 public void setWidth (int width) {
958 setWidthInPixels(DPIUtil.autoScaleUp(width));
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 ();
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);
990 if (disabledImageList == null) {
991 disabledImageList = display.getImageListToolBarDisabled (listStyle, bounds.width, bounds.height);
993 if (hotImageList == null) {
994 hotImageList = display.getImageListToolBarHot (listStyle, bounds.width, bounds.height);
996 Image disabled = disabledImage;
997 if (disabledImage == null) {
998 if (disabledImage2 != null) disabledImage2.dispose ();
999 disabledImage2 = null;
1002 disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE);
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
1012 Image image2 = image, hot = hotImage;
1013 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
1014 if (!enabled) image2 = hot = disabled;
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);
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) {
1032 disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE);
1036 disabledImageList.put (info.iImage, disabled);
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
1045 Image image2 = image, hot = hotImage;
1046 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
1047 if (!enabled) image2 = hot = disabled;
1049 if (imageList != null) imageList.put (info.iImage, image2);
1050 if (hotImageList != null) {
1051 hotImageList.put (info.iImage, hot != null ? hot : image2);
1053 if (image == null) info.iImage = OS.I_IMAGENONE;
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.
1062 info.dwMask |= OS.TBIF_SIZE;
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 ();
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;
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.
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;
1085 LRESULT wmCommandChild (long wParam, long lParam) {
1086 if ((style & SWT.RADIO) != 0) {
1087 if ((parent.getStyle () & SWT.NO_RADIO_GROUP) == 0) {
1091 sendSelectionEvent (SWT.Selection);