1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.*; 15 import org.eclipse.swt.internal.win32.*; 16 import org.eclipse.swt.*; 17 import org.eclipse.swt.graphics.*; 18 import org.eclipse.swt.events.*; 19 20 36 public class ToolItem extends Item { 37 ToolBar parent; 38 Control control; 39 String toolTipText; 40 Image disabledImage, hotImage; 41 Image disabledImage2; 42 int id; 43 44 78 public ToolItem (ToolBar parent, int style) { 79 super (parent, checkStyle (style)); 80 this.parent = parent; 81 parent.createItem (this, parent.getItemCount ()); 82 } 83 84 120 public ToolItem (ToolBar parent, int style, int index) { 121 super (parent, checkStyle (style)); 122 this.parent = parent; 123 parent.createItem (this, index); 124 } 125 126 151 public void addSelectionListener(SelectionListener listener) { 152 checkWidget(); 153 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 154 TypedListener typedListener = new TypedListener (listener); 155 addListener (SWT.Selection,typedListener); 156 addListener (SWT.DefaultSelection,typedListener); 157 } 158 159 static int checkStyle (int style) { 160 return checkBits (style, SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, SWT.DROP_DOWN, 0); 161 } 162 163 protected void checkSubclass () { 164 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); 165 } 166 167 void click (boolean dropDown) { 168 int hwnd = parent.handle; 169 if (OS.GetKeyState (OS.VK_LBUTTON) < 0) return; 170 int index = OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0); 171 RECT rect = new RECT (); 172 OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect); 173 int hotIndex = OS.SendMessage (hwnd, OS.TB_GETHOTITEM, 0, 0); 174 175 182 int y = rect.top + (rect.bottom - rect.top) / 2; 183 int lParam = ((dropDown ? rect.right - 1 : rect.left) & 0xFFFF) | ((y << 16) & 0xFFFF0000); 184 parent.ignoreMouse = true; 185 OS.SendMessage (hwnd, OS.WM_LBUTTONDOWN, 0, lParam); 186 OS.SendMessage (hwnd, OS.WM_LBUTTONUP, 0, lParam); 187 parent.ignoreMouse = false; 188 189 if (hotIndex != -1) { 190 OS.SendMessage (hwnd, OS.TB_SETHOTITEM, hotIndex, 0); 191 } 192 } 193 194 void destroyWidget () { 195 parent.destroyItem (this); 196 releaseHandle (); 197 } 198 199 210 public Rectangle getBounds () { 211 checkWidget(); 212 int hwnd = parent.handle; 213 int index = OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0); 214 RECT rect = new RECT (); 215 OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect); 216 int width = rect.right - rect.left; 217 int height = rect.bottom - rect.top; 218 return new Rectangle (rect.left, rect.top, width, height); 219 } 220 221 232 public Control getControl () { 233 checkWidget(); 234 return control; 235 } 236 237 251 public Image getDisabledImage () { 252 checkWidget(); 253 return disabledImage; 254 } 255 256 271 public boolean getEnabled () { 272 checkWidget(); 273 if ((style & SWT.SEPARATOR) != 0) { 274 return (state & DISABLED) == 0; 275 } 276 int hwnd = parent.handle; 277 int fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0); 278 return (fsState & OS.TBSTATE_ENABLED) != 0; 279 } 280 281 295 public Image getHotImage () { 296 checkWidget(); 297 return hotImage; 298 } 299 300 310 public ToolBar getParent () { 311 checkWidget(); 312 return parent; 313 } 314 315 332 public boolean getSelection () { 333 checkWidget(); 334 if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return false; 335 int hwnd = parent.handle; 336 int fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0); 337 return (fsState & OS.TBSTATE_CHECKED) != 0; 338 } 339 340 350 public String getToolTipText () { 351 checkWidget(); 352 return toolTipText; 353 } 354 355 365 public int getWidth () { 366 checkWidget(); 367 int hwnd = parent.handle; 368 int index = OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0); 369 RECT rect = new RECT (); 370 OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect); 371 return rect.right - rect.left; 372 } 373 374 389 public boolean isEnabled () { 390 checkWidget(); 391 return getEnabled () && parent.isEnabled (); 392 } 393 394 void releaseWidget () { 395 super.releaseWidget (); 396 releaseImages (); 397 control = null; 398 toolTipText = null; 399 disabledImage = hotImage = null; 400 if (disabledImage2 != null) disabledImage2.dispose (); 401 disabledImage2 = null; 402 } 403 404 void releaseHandle () { 405 super.releaseHandle (); 406 parent = null; 407 id = -1; 408 } 409 410 void releaseImages () { 411 TBBUTTONINFO info = new TBBUTTONINFO (); 412 info.cbSize = TBBUTTONINFO.sizeof; 413 info.dwMask = OS.TBIF_IMAGE | OS.TBIF_STYLE; 414 int hwnd = parent.handle; 415 OS.SendMessage (hwnd, OS.TB_GETBUTTONINFO, id, info); 416 426 if ((info.fsStyle & OS.BTNS_SEP) == 0 && info.iImage != OS.I_IMAGENONE) { 427 ImageList imageList = parent.getImageList (); 428 ImageList hotImageList = parent.getHotImageList (); 429 ImageList disabledImageList = parent.getDisabledImageList(); 430 if (imageList != null) imageList.put (info.iImage, null); 431 if (hotImageList != null) hotImageList.put (info.iImage, null); 432 if (disabledImageList != null) disabledImageList.put (info.iImage, null); 433 } 434 } 435 436 453 public void removeSelectionListener(SelectionListener listener) { 454 checkWidget(); 455 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 456 if (eventTable == null) return; 457 eventTable.unhook (SWT.Selection, listener); 458 eventTable.unhook (SWT.DefaultSelection,listener); 459 } 460 461 void resizeControl () { 462 if (control != null && !control.isDisposed ()) { 463 471 Rectangle itemRect = getBounds (); 472 control.setSize (itemRect.width, itemRect.height); 473 Rectangle rect = control.getBounds (); 474 rect.x = itemRect.x + (itemRect.width - rect.width) / 2; 475 rect.y = itemRect.y + (itemRect.height - rect.height) / 2; 476 control.setLocation (rect.x, rect.y); 477 } 478 } 479 480 void selectRadio () { 481 int index = 0; 482 ToolItem [] items = parent.getItems (); 483 while (index < items.length && items [index] != this) index++; 484 int i = index - 1; 485 while (i >= 0 && items [i].setRadioSelection (false)) --i; 486 int j = index + 1; 487 while (j < items.length && items [j].setRadioSelection (false)) j++; 488 setSelection (true); 489 } 490 491 506 public void setControl (Control control) { 507 checkWidget(); 508 if (control != null) { 509 if (control.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT); 510 if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT); 511 } 512 if ((style & SWT.SEPARATOR) == 0) return; 513 this.control = control; 514 528 if ((parent.style & (SWT.WRAP | SWT.VERTICAL)) != 0) { 529 boolean changed = false; 530 int hwnd = parent.handle; 531 TBBUTTONINFO info = new TBBUTTONINFO (); 532 info.cbSize = TBBUTTONINFO.sizeof; 533 info.dwMask = OS.TBIF_STYLE | OS.TBIF_STATE; 534 OS.SendMessage (hwnd, OS.TB_GETBUTTONINFO, id, info); 535 if (control == null) { 536 if ((info.fsStyle & OS.BTNS_SEP) == 0) { 537 changed = true; 538 info.fsStyle &= ~OS.BTNS_BUTTON; 539 info.fsStyle |= OS.BTNS_SEP; 540 if ((state & DISABLED) != 0) { 541 info.fsState &= ~OS.TBSTATE_ENABLED; 542 } else { 543 info.fsState |= OS.TBSTATE_ENABLED; 544 } 545 } 546 } else { 547 if ((info.fsStyle & OS.BTNS_SEP) != 0) { 548 changed = true; 549 info.fsStyle &= ~OS.BTNS_SEP; 550 info.fsStyle |= OS.BTNS_BUTTON; 551 info.fsState &= ~OS.TBSTATE_ENABLED; 552 info.dwMask |= OS.TBIF_IMAGE; 553 info.iImage = OS.I_IMAGENONE; 554 } 555 } 556 if (changed) { 557 OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info); 558 567 if (OS.SendMessage (hwnd, OS.TB_GETROWS, 0, 0) > 1) { 568 OS.InvalidateRect (hwnd, null, true); 569 } 570 } 571 } 572 resizeControl (); 573 } 574 575 591 public void setEnabled (boolean enabled) { 592 checkWidget(); 593 int hwnd = parent.handle; 594 int fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0); 595 601 if (((fsState & OS.TBSTATE_ENABLED) != 0) == enabled) return; 602 if (enabled) { 603 fsState |= OS.TBSTATE_ENABLED; 604 state &= ~DISABLED; 605 } else { 606 fsState &= ~OS.TBSTATE_ENABLED; 607 state |= DISABLED; 608 } 609 OS.SendMessage (hwnd, OS.TB_SETSTATE, id, fsState); 610 if ((style & SWT.SEPARATOR) == 0) { 611 if (image != null) updateImages (enabled && parent.getEnabled ()); 612 } 613 } 614 615 632 public void setDisabledImage (Image image) { 633 checkWidget(); 634 if ((style & SWT.SEPARATOR) != 0) return; 635 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); 636 disabledImage = image; 637 updateImages (getEnabled () && parent.getEnabled ()); 638 } 639 640 657 public void setHotImage (Image image) { 658 checkWidget(); 659 if ((style & SWT.SEPARATOR) != 0) return; 660 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); 661 hotImage = image; 662 updateImages (getEnabled () && parent.getEnabled ()); 663 } 664 665 public void setImage (Image image) { 666 checkWidget(); 667 if ((style & SWT.SEPARATOR) != 0) return; 668 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); 669 super.setImage (image); 670 updateImages (getEnabled () && parent.getEnabled ()); 671 } 672 673 boolean setRadioSelection (boolean value) { 674 if ((style & SWT.RADIO) == 0) return false; 675 if (getSelection () != value) { 676 setSelection (value); 677 postEvent (SWT.Selection); 678 } 679 return true; 680 } 681 682 697 public void setSelection (boolean selected) { 698 checkWidget(); 699 if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return; 700 int hwnd = parent.handle; 701 int fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0); 702 708 if (((fsState & OS.TBSTATE_CHECKED) != 0) == selected) return; 709 if (selected) { 710 fsState |= OS.TBSTATE_CHECKED; 711 } else { 712 fsState &= ~OS.TBSTATE_CHECKED; 713 } 714 OS.SendMessage (hwnd, OS.TB_SETSTATE, id, fsState); 715 716 726 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { 727 if (!getEnabled () || !parent.getEnabled ()) { 728 updateImages (false); 729 } 730 } 731 } 732 733 758 public void setText (String string) { 759 checkWidget(); 760 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 761 if ((style & SWT.SEPARATOR) != 0) return; 762 if (string.equals (text)) return; 763 super.setText (string); 764 int hwnd = parent.handle; 765 TBBUTTONINFO info = new TBBUTTONINFO (); 766 info.cbSize = TBBUTTONINFO.sizeof; 767 info.dwMask = OS.TBIF_TEXT | OS.TBIF_STYLE; 768 info.fsStyle = (byte) (widgetStyle () | OS.BTNS_AUTOSIZE); 769 int hHeap = OS.GetProcessHeap (), pszText = 0; 770 if (string.length () != 0) { 771 info.fsStyle |= OS.BTNS_SHOWTEXT; 772 TCHAR buffer = new TCHAR (parent.getCodePage (), string, true); 773 int byteCount = buffer.length () * TCHAR.sizeof; 774 pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 775 OS.MoveMemory (pszText, buffer, byteCount); 776 info.pszText = pszText; 777 } 778 OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info); 779 if (pszText != 0) OS.HeapFree (hHeap, 0, pszText); 780 781 789 parent.setDropDownItems (false); 790 int hFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0); 791 OS.SendMessage (hwnd, OS.WM_SETFONT, hFont, 0); 792 parent.setDropDownItems (true); 793 parent.layoutItems (); 794 } 795 796 807 public void setToolTipText (String string) { 808 checkWidget(); 809 toolTipText = string; 810 } 811 812 822 public void setWidth (int width) { 823 checkWidget(); 824 if ((style & SWT.SEPARATOR) == 0) return; 825 if (width < 0) return; 826 int hwnd = parent.handle; 827 TBBUTTONINFO info = new TBBUTTONINFO (); 828 info.cbSize = TBBUTTONINFO.sizeof; 829 info.dwMask = OS.TBIF_SIZE; 830 info.cx = (short) width; 831 OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info); 832 parent.layoutItems (); 833 } 834 835 void updateImages (boolean enabled) { 836 if ((style & SWT.SEPARATOR) != 0) return; 837 int hwnd = parent.handle; 838 TBBUTTONINFO info = new TBBUTTONINFO (); 839 info.cbSize = TBBUTTONINFO.sizeof; 840 info.dwMask = OS.TBIF_IMAGE; 841 OS.SendMessage (hwnd, OS.TB_GETBUTTONINFO, id, info); 842 if (info.iImage == OS.I_IMAGENONE && image == null) return; 843 ImageList imageList = parent.getImageList (); 844 ImageList hotImageList = parent.getHotImageList (); 845 ImageList disabledImageList = parent.getDisabledImageList(); 846 if (info.iImage == OS.I_IMAGENONE) { 847 Rectangle bounds = image.getBounds (); 848 int listStyle = parent.style & SWT.RIGHT_TO_LEFT; 849 if (imageList == null) { 850 imageList = display.getImageListToolBar (listStyle, bounds.width, bounds.height); 851 } 852 if (disabledImageList == null) { 853 disabledImageList = display.getImageListToolBarDisabled (listStyle, bounds.width, bounds.height); 854 } 855 if (hotImageList == null) { 856 hotImageList = display.getImageListToolBarHot (listStyle, bounds.width, bounds.height); 857 } 858 Image disabled = disabledImage; 859 if (disabledImage == null) { 860 if (disabledImage2 != null) disabledImage2.dispose (); 861 disabledImage2 = null; 862 disabled = image; 863 if (!enabled) { 864 disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE); 865 } 866 } 867 874 Image image2 = image, hot = hotImage; 875 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { 876 if (!enabled) image2 = hot = disabled; 877 } 878 info.iImage = imageList.add (image2); 879 disabledImageList.add (disabled); 880 hotImageList.add (hot != null ? hot : image2); 881 parent.setImageList (imageList); 882 parent.setDisabledImageList (disabledImageList); 883 parent.setHotImageList (hotImageList); 884 } else { 885 Image disabled = null; 886 if (disabledImageList != null) { 887 if (image != null) { 888 if (disabledImage2 != null) disabledImage2.dispose (); 889 disabledImage2 = null; 890 disabled = disabledImage; 891 if (disabledImage == null) { 892 disabled = image; 893 if (!enabled) { 894 disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE); 895 } 896 } 897 } 898 disabledImageList.put (info.iImage, disabled); 899 } 900 907 Image image2 = image, hot = hotImage; 908 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { 909 if (!enabled) image2 = hot = disabled; 910 } 911 if (imageList != null) imageList.put (info.iImage, image2); 912 if (hotImageList != null) { 913 hotImageList.put (info.iImage, hot != null ? hot : image2); 914 } 915 if (image == null) info.iImage = OS.I_IMAGENONE; 916 } 917 918 924 info.dwMask |= OS.TBIF_SIZE; 925 info.cx = 0; 926 OS.SendMessage (hwnd, OS.TB_SETBUTTONINFO, id, info); 927 928 parent.layoutItems (); 929 } 930 931 int widgetStyle () { 932 if ((style & SWT.DROP_DOWN) != 0) return OS.BTNS_DROPDOWN; 933 if ((style & SWT.PUSH) != 0) return OS.BTNS_BUTTON; 934 if ((style & SWT.CHECK) != 0) return OS.BTNS_CHECK; 935 940 if ((style & SWT.RADIO) != 0) return OS.BTNS_CHECK; 942 if ((style & SWT.SEPARATOR) != 0) return OS.BTNS_SEP; 943 return OS.BTNS_BUTTON; 944 } 945 946 LRESULT wmCommandChild (int wParam, int lParam) { 947 if ((style & SWT.RADIO) != 0) { 948 if ((parent.getStyle () & SWT.NO_RADIO_GROUP) == 0) { 949 selectRadio (); 950 } 951 } 952 postEvent (SWT.Selection); 953 return null; 954 } 955 956 } 957 | Popular Tags |