1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 import org.eclipse.swt.graphics.*; 17 import org.eclipse.swt.events.*; 18 19 35 36 public class MenuItem extends Item { 37 Menu parent, menu; 38 int hBitmap, id, accelerator; 39 47 final static int MARGIN_WIDTH = OS.IsWin95 ? 2 : 1; 48 final static int MARGIN_HEIGHT = OS.IsWin95 ? 2 : 1; 49 50 84 public MenuItem (Menu parent, int style) { 85 super (parent, checkStyle (style)); 86 this.parent = parent; 87 parent.createItem (this, parent.getItemCount ()); 88 } 89 90 126 public MenuItem (Menu parent, int style, int index) { 127 super (parent, checkStyle (style)); 128 this.parent = parent; 129 parent.createItem (this, index); 130 } 131 132 MenuItem (Menu parent, Menu menu, int style, int index) { 133 super (parent, checkStyle (style)); 134 this.parent = parent; 135 this.menu = menu; 136 if (menu != null) menu.cascade = this; 137 display.addMenuItem (this); 138 } 139 140 159 public void addArmListener (ArmListener listener) { 160 checkWidget (); 161 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 162 TypedListener typedListener = new TypedListener (listener); 163 addListener (SWT.Arm, typedListener); 164 } 165 166 185 public void addHelpListener (HelpListener listener) { 186 checkWidget (); 187 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 188 TypedListener typedListener = new TypedListener (listener); 189 addListener (SWT.Help, typedListener); 190 } 191 192 216 public void addSelectionListener (SelectionListener listener) { 217 checkWidget (); 218 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 219 TypedListener typedListener = new TypedListener(listener); 220 addListener (SWT.Selection,typedListener); 221 addListener (SWT.DefaultSelection,typedListener); 222 } 223 224 protected void checkSubclass () { 225 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); 226 } 227 228 static int checkStyle (int style) { 229 return checkBits (style, SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, SWT.CASCADE, 0); 230 } 231 232 void destroyWidget () { 233 parent.destroyItem (this); 234 releaseHandle (); 235 } 236 237 void fillAccel (ACCEL accel) { 238 accel.fVirt = 0; 239 accel.cmd = accel.key = 0; 240 if (accelerator == 0 || !getEnabled ()) return; 241 int fVirt = OS.FVIRTKEY; 242 int key = accelerator & SWT.KEY_MASK; 243 int vKey = Display.untranslateKey (key); 244 if (vKey != 0) { 245 key = vKey; 246 } else { 247 switch (key) { 248 254 case 27: key = OS.VK_ESCAPE; break; 255 case 127: key = OS.VK_DELETE; break; 256 default: { 257 key = Display.wcsToMbcs ((char) key); 258 if (key == 0) return; 259 if (OS.IsWinCE) { 260 key = OS.CharUpper ((short) key); 261 } else { 262 vKey = OS.VkKeyScan ((short) key) & 0xFF; 263 if (vKey == -1) { 264 fVirt = 0; 265 } else { 266 key = vKey; 267 } 268 } 269 } 270 } 271 } 272 accel.key = (short) key; 273 accel.cmd = (short) id; 274 accel.fVirt = (byte) fVirt; 275 if ((accelerator & SWT.ALT) != 0) accel.fVirt |= OS.FALT; 276 if ((accelerator & SWT.SHIFT) != 0) accel.fVirt |= OS.FSHIFT; 277 if ((accelerator & SWT.CONTROL) != 0) accel.fVirt |= OS.FCONTROL; 278 } 279 280 void fixMenus (Decorations newParent) { 281 if (menu != null) menu.fixMenus (newParent); 282 } 283 284 299 public int getAccelerator () { 300 checkWidget (); 301 return accelerator; 302 } 303 304 317 Rectangle getBounds () { 318 checkWidget (); 319 if (OS.IsWinCE) return new Rectangle (0, 0, 0, 0); 320 int index = parent.indexOf (this); 321 if (index == -1) return new Rectangle (0, 0, 0, 0); 322 if ((parent.style & SWT.BAR) != 0) { 323 Decorations shell = parent.parent; 324 if (shell.menuBar != parent) { 325 return new Rectangle (0, 0, 0, 0); 326 } 327 int hwndShell = shell.handle; 328 MENUBARINFO info1 = new MENUBARINFO (); 329 info1.cbSize = MENUBARINFO.sizeof; 330 if (!OS.GetMenuBarInfo (hwndShell, OS.OBJID_MENU, 1, info1)) { 331 return new Rectangle (0, 0, 0, 0); 332 } 333 MENUBARINFO info2 = new MENUBARINFO (); 334 info2.cbSize = MENUBARINFO.sizeof; 335 if (!OS.GetMenuBarInfo (hwndShell, OS.OBJID_MENU, index + 1, info2)) { 336 return new Rectangle (0, 0, 0, 0); 337 } 338 int x = info2.left - info1.left; 339 int y = info2.top - info1.top; 340 int width = info2.right - info2.left; 341 int height = info2.bottom - info2.top; 342 return new Rectangle (x, y, width, height); 343 } else { 344 int hMenu = parent.handle; 345 RECT rect1 = new RECT (); 346 if (!OS.GetMenuItemRect (0, hMenu, 0, rect1)) { 347 return new Rectangle (0, 0, 0, 0); 348 } 349 RECT rect2 = new RECT (); 350 if (!OS.GetMenuItemRect (0, hMenu, index, rect2)) { 351 return new Rectangle (0, 0, 0, 0); 352 } 353 int x = rect2.left - rect1.left + 2; 354 int y = rect2.top - rect1.top + 2; 355 int width = rect2.right - rect2.left; 356 int height = rect2.bottom - rect2.top; 357 return new Rectangle (x, y, width, height); 358 } 359 } 360 361 376 public boolean getEnabled () { 377 checkWidget (); 378 if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { 379 int hwndCB = parent.hwndCB; 380 TBBUTTONINFO info = new TBBUTTONINFO (); 381 info.cbSize = TBBUTTONINFO.sizeof; 382 info.dwMask = OS.TBIF_STATE; 383 OS.SendMessage (hwndCB, OS.TB_GETBUTTONINFO, id, info); 384 return (info.fsState & OS.TBSTATE_ENABLED) != 0; 385 } 386 int hMenu = parent.handle; 387 MENUITEMINFO info = new MENUITEMINFO (); 388 info.cbSize = MENUITEMINFO.sizeof; 389 info.fMask = OS.MIIM_STATE; 390 boolean success; 391 if (OS.IsWinCE) { 392 int index = parent.indexOf (this); 393 if (index == -1) error (SWT.ERROR_CANNOT_GET_ENABLED); 394 success = OS.GetMenuItemInfo (hMenu, index, true, info); 395 } else { 396 success = OS.GetMenuItemInfo (hMenu, id, false, info); 397 } 398 if (!success) error (SWT.ERROR_CANNOT_GET_ENABLED); 399 return (info.fState & (OS.MFS_DISABLED | OS.MFS_GRAYED)) == 0; 400 } 401 402 416 public Menu getMenu () { 417 checkWidget (); 418 return menu; 419 } 420 421 String getNameText () { 422 if ((style & SWT.SEPARATOR) != 0) return "|"; 423 return super.getNameText (); 424 } 425 426 436 public Menu getParent () { 437 checkWidget (); 438 return parent; 439 } 440 441 455 public boolean getSelection () { 456 checkWidget (); 457 if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return false; 458 if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) return false; 459 int hMenu = parent.handle; 460 MENUITEMINFO info = new MENUITEMINFO (); 461 info.cbSize = MENUITEMINFO.sizeof; 462 info.fMask = OS.MIIM_STATE; 463 boolean success = OS.GetMenuItemInfo (hMenu, id, false, info); 464 if (!success) error (SWT.ERROR_CANNOT_GET_SELECTION); 465 return (info.fState & OS.MFS_CHECKED) !=0; 466 } 467 468 483 public boolean isEnabled () { 484 return getEnabled () && parent.isEnabled (); 485 } 486 487 void releaseChildren (boolean destroy) { 488 if (menu != null) { 489 menu.release (false); 490 menu = null; 491 } 492 super.releaseChildren (destroy); 493 } 494 495 void releaseHandle () { 496 super.releaseHandle (); 497 parent = null; 498 id = -1; 499 } 500 501 void releaseMenu () { 502 if (!OS.IsSP) setMenu (null); 503 menu = null; 504 } 505 506 void releaseParent () { 507 super.releaseParent (); 508 if (menu != null) menu.dispose (); 509 menu = null; 510 } 511 512 void releaseWidget () { 513 super.releaseWidget (); 514 if (hBitmap != 0) OS.DeleteObject (hBitmap); 515 hBitmap = 0; 516 if (accelerator != 0) { 517 parent.destroyAccelerators (); 518 } 519 accelerator = 0; 520 display.removeMenuItem (this); 521 } 522 523 540 public void removeArmListener (ArmListener listener) { 541 checkWidget (); 542 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 543 if (eventTable == null) return; 544 eventTable.unhook (SWT.Arm, listener); 545 } 546 563 public void removeHelpListener (HelpListener listener) { 564 checkWidget (); 565 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 566 if (eventTable == null) return; 567 eventTable.unhook (SWT.Help, listener); 568 } 569 586 public void removeSelectionListener (SelectionListener listener) { 587 checkWidget (); 588 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 589 if (eventTable == null) return; 590 eventTable.unhook (SWT.Selection, listener); 591 eventTable.unhook (SWT.DefaultSelection,listener); 592 } 593 594 void selectRadio () { 595 int index = 0; 596 MenuItem [] items = parent.getItems (); 597 while (index < items.length && items [index] != this) index++; 598 int i = index - 1; 599 while (i >= 0 && items [i].setRadioSelection (false)) --i; 600 int j = index + 1; 601 while (j < items.length && items [j].setRadioSelection (false)) j++; 602 setSelection (true); 603 } 604 605 621 public void setAccelerator (int accelerator) { 622 checkWidget (); 623 if (this.accelerator == accelerator) return; 624 this.accelerator = accelerator; 625 parent.destroyAccelerators (); 626 } 627 628 641 public void setEnabled (boolean enabled) { 642 checkWidget (); 643 if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { 644 int hwndCB = parent.hwndCB; 645 TBBUTTONINFO info = new TBBUTTONINFO (); 646 info.cbSize = TBBUTTONINFO.sizeof; 647 info.dwMask = OS.TBIF_STATE; 648 OS.SendMessage (hwndCB, OS.TB_GETBUTTONINFO, id, info); 649 info.fsState &= ~OS.TBSTATE_ENABLED; 650 if (enabled) info.fsState |= OS.TBSTATE_ENABLED; 651 OS.SendMessage (hwndCB, OS.TB_SETBUTTONINFO, id, info); 652 } else { 653 int hMenu = parent.handle; 654 if (OS.IsWinCE) { 655 int index = parent.indexOf (this); 656 if (index == -1) return; 657 int uEnable = OS.MF_BYPOSITION | (enabled ? OS.MF_ENABLED : OS.MF_GRAYED); 658 OS.EnableMenuItem (hMenu, index, uEnable); 659 } else { 660 MENUITEMINFO info = new MENUITEMINFO (); 661 info.cbSize = MENUITEMINFO.sizeof; 662 info.fMask = OS.MIIM_STATE; 663 boolean success = OS.GetMenuItemInfo (hMenu, id, false, info); 664 if (!success) error (SWT.ERROR_CANNOT_SET_ENABLED); 665 int bits = OS.MFS_DISABLED | OS.MFS_GRAYED; 666 if (enabled) { 667 if ((info.fState & bits) == 0) return; 668 info.fState &= ~bits; 669 } else { 670 if ((info.fState & bits) == bits) return; 671 info.fState |= bits; 672 } 673 success = OS.SetMenuItemInfo (hMenu, id, false, info); 674 if (!success) { 675 683 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) { 684 success = id == OS.GetMenuDefaultItem (hMenu, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED); 685 } 686 if (!success) error (SWT.ERROR_CANNOT_SET_ENABLED); 687 } 688 } 689 } 690 parent.destroyAccelerators (); 691 parent.redraw (); 692 } 693 694 708 public void setImage (Image image) { 709 checkWidget (); 710 if ((style & SWT.SEPARATOR) != 0) return; 711 super.setImage (image); 712 if (OS.IsWinCE) { 713 if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { 714 int hwndCB = parent.hwndCB; 715 TBBUTTONINFO info = new TBBUTTONINFO (); 716 info.cbSize = TBBUTTONINFO.sizeof; 717 info.dwMask = OS.TBIF_IMAGE; 718 info.iImage = parent.imageIndex (image); 719 OS.SendMessage (hwndCB, OS.TB_SETBUTTONINFO, id, info); 720 } 721 return; 722 } 723 if (OS.WIN32_VERSION < OS.VERSION (4, 10)) return; 724 MENUITEMINFO info = new MENUITEMINFO (); 725 info.cbSize = MENUITEMINFO.sizeof; 726 info.fMask = OS.MIIM_BITMAP; 727 if (parent.foreground != -1) { 728 info.hbmpItem = OS.HBMMENU_CALLBACK; 729 } else { 730 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) { 731 if (hBitmap != 0) OS.DeleteObject (hBitmap); 732 info.hbmpItem = hBitmap = image != null ? Display.create32bitDIB (image) : 0; 733 } else { 734 info.hbmpItem = OS.HBMMENU_CALLBACK; 735 } 736 } 737 int hMenu = parent.handle; 738 OS.SetMenuItemInfo (hMenu, id, false, info); 739 parent.redraw (); 740 } 741 742 767 public void setMenu (Menu menu) { 768 checkWidget (); 769 770 771 if ((style & SWT.CASCADE) == 0) { 772 error (SWT.ERROR_MENUITEM_NOT_CASCADE); 773 } 774 if (menu != null) { 775 if (menu.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); 776 if ((menu.style & SWT.DROP_DOWN) == 0) { 777 error (SWT.ERROR_MENU_NOT_DROP_DOWN); 778 } 779 if (menu.parent != parent.parent) { 780 error (SWT.ERROR_INVALID_PARENT); 781 } 782 } 783 784 785 Menu oldMenu = this.menu; 786 if (oldMenu == menu) return; 787 if (oldMenu != null) oldMenu.cascade = null; 788 this.menu = menu; 789 790 791 if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { 792 if (OS.IsPPC) { 793 int hwndCB = parent.hwndCB; 794 int hMenu = menu == null ? 0 : menu.handle; 795 OS.SendMessage (hwndCB, OS.SHCMBM_SETSUBMENU, id, hMenu); 796 } 797 if (OS.IsSP) error (SWT.ERROR_CANNOT_SET_MENU); 798 } else { 799 807 int hMenu = parent.handle; 808 MENUITEMINFO info = new MENUITEMINFO (); 809 info.cbSize = MENUITEMINFO.sizeof; 810 info.fMask = OS.MIIM_DATA; 811 int index = 0; 812 while (OS.GetMenuItemInfo (hMenu, index, true, info)) { 813 if (info.dwItemData == id) break; 814 index++; 815 } 816 if (info.dwItemData != id) return; 817 boolean restoreBitmap = false, success = false; 818 819 827 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (4, 10)) { 828 info.fMask = OS.MIIM_BITMAP; 829 OS.GetMenuItemInfo (hMenu, index, true, info); 830 restoreBitmap = info.hbmpItem != 0 || parent.foreground != -1; 831 if (restoreBitmap) { 832 info.hbmpItem = 0; 833 success = OS.SetMenuItemInfo (hMenu, id, false, info); 834 } 835 } 836 837 int cch = 128; 838 int hHeap = OS.GetProcessHeap (); 839 int byteCount = cch * TCHAR.sizeof; 840 int pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 841 info.fMask = OS.MIIM_STATE | OS.MIIM_ID | OS.MIIM_TYPE | OS.MIIM_DATA; 842 info.dwTypeData = pszText; 843 info.cch = cch; 844 success = OS.GetMenuItemInfo (hMenu, index, true, info); 845 if (menu != null) { 846 menu.cascade = this; 847 info.fMask |= OS.MIIM_SUBMENU; 848 info.hSubMenu = menu.handle; 849 } 850 OS.RemoveMenu (hMenu, index, OS.MF_BYPOSITION); 851 if (OS.IsWinCE) { 852 859 int uIDNewItem = id; 860 int uFlags = OS.MF_BYPOSITION; 861 if (menu != null) { 862 uFlags |= OS.MF_POPUP; 863 uIDNewItem = menu.handle; 864 } 865 TCHAR lpNewItem = new TCHAR (0, " ", true); 866 success = OS.InsertMenu (hMenu, index, uFlags, uIDNewItem, lpNewItem); 867 if (success) { 868 info.fMask = OS.MIIM_DATA | OS.MIIM_TYPE; 869 success = OS.SetMenuItemInfo (hMenu, index, true, info); 870 if ((info.fState & (OS.MFS_DISABLED | OS.MFS_GRAYED)) != 0) { 871 OS.EnableMenuItem (hMenu, index, OS.MF_BYPOSITION | OS.MF_GRAYED); 872 } 873 if ((info.fState & OS.MFS_CHECKED) != 0) { 874 OS.CheckMenuItem (hMenu, index, OS.MF_BYPOSITION | OS.MF_CHECKED); 875 } 876 } 877 } else { 878 success = OS.InsertMenuItem (hMenu, index, true, info); 879 884 if (OS.WIN32_VERSION >= OS.VERSION (4, 10)) { 885 if (restoreBitmap) { 886 info.fMask = OS.MIIM_BITMAP; 887 if (parent.foreground != -1) { 888 info.hbmpItem = OS.HBMMENU_CALLBACK; 889 } else { 890 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) { 891 info.hbmpItem = hBitmap; 892 } else { 893 info.hbmpItem = OS.HBMMENU_CALLBACK; 894 } 895 } 896 success = OS.SetMenuItemInfo (hMenu, id, false, info); 897 } 898 } 899 } 900 if (pszText != 0) OS.HeapFree (hHeap, 0, pszText); 901 if (!success) error (SWT.ERROR_CANNOT_SET_MENU); 902 } 903 parent.destroyAccelerators (); 904 } 905 906 boolean setRadioSelection (boolean value) { 907 if ((style & SWT.RADIO) == 0) return false; 908 if (getSelection () != value) { 909 setSelection (value); 910 postEvent (SWT.Selection); 911 } 912 return true; 913 } 914 915 928 public void setSelection (boolean selected) { 929 checkWidget (); 930 if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return; 931 if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) return; 932 int hMenu = parent.handle; 933 if (OS.IsWinCE) { 934 int index = parent.indexOf (this); 935 if (index == -1) return; 936 int uCheck = OS.MF_BYPOSITION | (selected ? OS.MF_CHECKED : OS.MF_UNCHECKED); 937 OS.CheckMenuItem (hMenu, index, uCheck); 938 } else { 939 MENUITEMINFO info = new MENUITEMINFO (); 940 info.cbSize = MENUITEMINFO.sizeof; 941 info.fMask = OS.MIIM_STATE; 942 boolean success = OS.GetMenuItemInfo (hMenu, id, false, info); 943 if (!success) error (SWT.ERROR_CANNOT_SET_SELECTION); 944 info.fState &= ~OS.MFS_CHECKED; 945 if (selected) info.fState |= OS.MFS_CHECKED; 946 success = OS.SetMenuItemInfo (hMenu, id, false, info); 947 if (!success) { 948 956 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) { 957 success = id == OS.GetMenuDefaultItem (hMenu, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED); 958 } 959 if (!success) error (SWT.ERROR_CANNOT_SET_SELECTION); 960 } 961 } 962 parent.redraw (); 963 } 964 1001public void setText (String string) { 1002 checkWidget (); 1003 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 1004 if ((style & SWT.SEPARATOR) != 0) return; 1005 if (text.equals (string)) return; 1006 super.setText (string); 1007 int hHeap = OS.GetProcessHeap (); 1008 int pszText = 0; 1009 boolean success = false; 1010 if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { 1011 1017 if (string.indexOf ('&') != -1) { 1018 int length = string.length (); 1019 char[] text = new char [length]; 1020 string.getChars( 0, length, text, 0); 1021 int i = 0, j = 0; 1022 for (i=0; i<length; i++) { 1023 if (text[i] != '&') text [j++] = text [i]; 1024 } 1025 if (j < i) string = new String (text, 0, j); 1026 } 1027 1028 TCHAR buffer = new TCHAR (0, string, true); 1029 int byteCount = buffer.length () * TCHAR.sizeof; 1030 pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 1031 OS.MoveMemory (pszText, buffer, byteCount); 1032 int hwndCB = parent.hwndCB; 1033 TBBUTTONINFO info2 = new TBBUTTONINFO (); 1034 info2.cbSize = TBBUTTONINFO.sizeof; 1035 info2.dwMask = OS.TBIF_TEXT; 1036 info2.pszText = pszText; 1037 success = OS.SendMessage (hwndCB, OS.TB_SETBUTTONINFO, id, info2) != 0; 1038 } else { 1039 MENUITEMINFO info = new MENUITEMINFO (); 1040 info.cbSize = MENUITEMINFO.sizeof; 1041 int hMenu = parent.handle; 1042 1043 1049 boolean restoreBitmap = false; 1050 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (4, 10)) { 1051 info.fMask = OS.MIIM_BITMAP; 1052 OS.GetMenuItemInfo (hMenu, id, false, info); 1053 restoreBitmap = info.hbmpItem != 0 || parent.foreground != -1; 1054 } 1055 1056 1057 TCHAR buffer = new TCHAR (0, string, true); 1058 int byteCount = buffer.length () * TCHAR.sizeof; 1059 pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 1060 OS.MoveMemory (pszText, buffer, byteCount); 1061 info.fMask = OS.MIIM_TYPE; 1062 info.fType = widgetStyle (); 1063 info.dwTypeData = pszText; 1064 success = OS.SetMenuItemInfo (hMenu, id, false, info); 1065 1066 1071 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (4, 10)) { 1072 if (restoreBitmap) { 1073 info.fMask = OS.MIIM_BITMAP; 1074 if (parent.foreground != -1) { 1075 info.hbmpItem = OS.HBMMENU_CALLBACK; 1076 } else { 1077 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) { 1078 info.hbmpItem = hBitmap; 1079 } else { 1080 info.hbmpItem = OS.HBMMENU_CALLBACK; 1081 } 1082 } 1083 success = OS.SetMenuItemInfo (hMenu, id, false, info); 1084 } 1085 } 1086 } 1087 if (pszText != 0) OS.HeapFree (hHeap, 0, pszText); 1088 if (!success) error (SWT.ERROR_CANNOT_SET_TEXT); 1089 parent.redraw (); 1090} 1091 1092int widgetStyle () { 1093 int bits = 0; 1094 Decorations shell = parent.parent; 1095 if ((shell.style & SWT.MIRRORED) != 0) { 1096 if ((parent.style & SWT.LEFT_TO_RIGHT) != 0) { 1097 bits |= OS.MFT_RIGHTJUSTIFY | OS.MFT_RIGHTORDER; 1098 } 1099 } else { 1100 if ((parent.style & SWT.RIGHT_TO_LEFT) != 0) { 1101 bits |= OS.MFT_RIGHTJUSTIFY | OS.MFT_RIGHTORDER; 1102 } 1103 } 1104 if ((style & SWT.SEPARATOR) != 0) return bits | OS.MFT_SEPARATOR; 1105 if ((style & SWT.RADIO) != 0) return bits | OS.MFT_RADIOCHECK; 1106 return bits | OS.MFT_STRING; 1107} 1108 1109LRESULT wmCommandChild (int wParam, int lParam) { 1110 if ((style & SWT.CHECK) != 0) { 1111 setSelection (!getSelection ()); 1112 } else { 1113 if ((style & SWT.RADIO) != 0) { 1114 if ((parent.getStyle () & SWT.NO_RADIO_GROUP) != 0) { 1115 setSelection (!getSelection ()); 1116 } else { 1117 selectRadio (); 1118 } 1119 } 1120 } 1121 Event event = new Event (); 1122 setInputState (event, SWT.Selection); 1123 postEvent (SWT.Selection, event); 1124 return null; 1125} 1126 1127LRESULT wmDrawChild (int wParam, int lParam) { 1128 DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT (); 1129 OS.MoveMemory (struct, lParam, DRAWITEMSTRUCT.sizeof); 1130 if (image != null) { 1131 GCData data = new GCData(); 1132 data.device = display; 1133 GC gc = GC.win32_new (struct.hDC, data); 1134 1140 int x = (parent.style & SWT.BAR) != 0 ? MARGIN_WIDTH * 2 : struct.left; 1141 Image image = getEnabled () ? this.image : new Image (display, this.image, SWT.IMAGE_DISABLE); 1142 gc.drawImage (image, x, struct.top + MARGIN_HEIGHT); 1143 if (this.image != image) image.dispose (); 1144 gc.dispose (); 1145 } 1146 if (parent.foreground != -1) OS.SetTextColor (struct.hDC, parent.foreground); 1147 return null; 1148} 1149 1150LRESULT wmMeasureChild (int wParam, int lParam) { 1151 MEASUREITEMSTRUCT struct = new MEASUREITEMSTRUCT (); 1152 OS.MoveMemory (struct, lParam, MEASUREITEMSTRUCT.sizeof); 1153 int width = 0, height = 0; 1154 if (image != null) { 1155 Rectangle rect = image.getBounds (); 1156 width = rect.width; 1157 height = rect.height; 1158 } else { 1159 1169 MENUINFO lpcmi = new MENUINFO (); 1170 lpcmi.cbSize = MENUINFO.sizeof; 1171 lpcmi.fMask = OS.MIM_STYLE; 1172 int hMenu = parent.handle; 1173 OS.GetMenuInfo (hMenu, lpcmi); 1174 if ((lpcmi.dwStyle & OS.MNS_CHECKORBMP) == 0) { 1175 MenuItem [] items = parent.getItems (); 1176 for (int i=0; i<items.length; i++) { 1177 MenuItem item = items [i]; 1178 if (item.image != null) { 1179 Rectangle rect = item.image.getBounds (); 1180 width = Math.max (width, rect.width); 1181 } 1182 } 1183 } 1184 } 1185 if (width != 0 || height != 0) { 1186 struct.itemWidth = width + MARGIN_WIDTH * 2; 1187 struct.itemHeight = height + MARGIN_HEIGHT * 2; 1188 OS.MoveMemory (lParam, struct, MEASUREITEMSTRUCT.sizeof); 1189 } 1190 return null; 1191} 1192 1193} 1194 | Popular Tags |