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 37 38 public class Menu extends Widget { 39 49 public int handle; 50 51 int x, y, hBrush, hwndCB, id0, id1; 52 int foreground = -1, background = -1; 53 Image backgroundImage; 54 boolean hasLocation; 55 MenuItem cascade; 56 Decorations parent; 57 ImageList imageList; 58 59 60 static final int ID_PPC = 100; 61 62 63 static final int ID_SPMM = 102; 64 static final int ID_SPBM = 103; 65 static final int ID_SPMB = 104; 66 static final int ID_SPBB = 105; 67 static final int ID_SPSOFTKEY0 = 106; 68 static final int ID_SPSOFTKEY1 = 107; 69 70 89 public Menu (Control parent) { 90 this (checkNull (parent).menuShell (), SWT.POP_UP); 91 } 92 93 124 public Menu (Decorations parent, int style) { 125 this (parent, checkStyle (style), 0); 126 } 127 128 148 public Menu (Menu parentMenu) { 149 this (checkNull (parentMenu).parent, SWT.DROP_DOWN); 150 } 151 152 172 public Menu (MenuItem parentItem) { 173 this (checkNull (parentItem).parent); 174 } 175 176 Menu (Decorations parent, int style, int handle) { 177 super (parent, checkStyle (style)); 178 this.parent = parent; 179 this.handle = handle; 180 194 checkOrientation (parent); 195 createWidget (); 196 } 197 198 void _setVisible (boolean visible) { 199 if ((style & (SWT.BAR | SWT.DROP_DOWN)) != 0) return; 200 int hwndParent = parent.handle; 201 if (visible) { 202 int flags = OS.TPM_LEFTBUTTON; 203 if (OS.GetKeyState (OS.VK_LBUTTON) >= 0) flags |= OS.TPM_RIGHTBUTTON; 204 if ((style & SWT.RIGHT_TO_LEFT) != 0) flags |= OS.TPM_RIGHTALIGN; 205 if ((parent.style & SWT.MIRRORED) != 0) { 206 flags &= ~OS.TPM_RIGHTALIGN; 207 if ((style & SWT.LEFT_TO_RIGHT) != 0) flags |= OS.TPM_RIGHTALIGN; 208 } 209 int nX = x, nY = y; 210 if (!hasLocation) { 211 int pos = OS.GetMessagePos (); 212 nX = (short) (pos & 0xFFFF); 213 nY = (short) (pos >> 16); 214 } 215 228 boolean success = OS.TrackPopupMenu (handle, flags, nX, nY, 0, hwndParent, null); 229 if (!success && GetMenuItemCount (handle) == 0) { 230 OS.SendMessage (hwndParent, OS.WM_MENUSELECT, 0xFFFF0000, 0); 231 } 232 } else { 233 OS.SendMessage (hwndParent, OS.WM_CANCELMODE, 0, 0); 234 } 235 } 236 237 256 public void addHelpListener (HelpListener listener) { 257 checkWidget (); 258 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 259 TypedListener typedListener = new TypedListener (listener); 260 addListener (SWT.Help, typedListener); 261 } 262 263 282 public void addMenuListener (MenuListener listener) { 283 checkWidget (); 284 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 285 TypedListener typedListener = new TypedListener (listener); 286 addListener (SWT.Hide,typedListener); 287 addListener (SWT.Show,typedListener); 288 } 289 290 static Control checkNull (Control control) { 291 if (control == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 292 return control; 293 } 294 295 static Menu checkNull (Menu menu) { 296 if (menu == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 297 return menu; 298 } 299 300 static MenuItem checkNull (MenuItem item) { 301 if (item == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 302 return item; 303 } 304 305 static int checkStyle (int style) { 306 return checkBits (style, SWT.POP_UP, SWT.BAR, SWT.DROP_DOWN, 0, 0, 0); 307 } 308 309 void createHandle () { 310 if (handle != 0) return; 311 if ((style & SWT.BAR) != 0) { 312 if (OS.IsPPC) { 313 int hwndShell = parent.handle; 314 SHMENUBARINFO mbi = new SHMENUBARINFO (); 315 mbi.cbSize = SHMENUBARINFO.sizeof; 316 mbi.hwndParent = hwndShell; 317 mbi.dwFlags = OS.SHCMBF_HIDDEN; 318 mbi.nToolBarId = ID_PPC; 319 mbi.hInstRes = OS.GetLibraryHandle (); 320 boolean success = OS.SHCreateMenuBar (mbi); 321 hwndCB = mbi.hwndMB; 322 if (!success) error (SWT.ERROR_NO_HANDLES); 323 324 OS.SendMessage (hwndCB, OS.TB_DELETEBUTTON, 0, 0); 325 return; 326 } 327 336 if (OS.IsSP) { 337 338 int nToolBarId; 339 if ((style & SWT.BUTTON1) != 0) { 340 nToolBarId = ((style & SWT.BUTTON2) != 0) ? ID_SPBB : ID_SPBM; 341 } else { 342 nToolBarId = ((style & SWT.BUTTON2) != 0) ? ID_SPMB : ID_SPMM; 343 } 344 345 346 SHMENUBARINFO mbi = new SHMENUBARINFO (); 347 mbi.cbSize = SHMENUBARINFO.sizeof; 348 mbi.hwndParent = parent.handle; 349 mbi.dwFlags = OS.SHCMBF_HIDDEN; 350 mbi.nToolBarId = nToolBarId; 351 mbi.hInstRes = OS.GetLibraryHandle (); 352 if (!OS.SHCreateMenuBar (mbi)) error (SWT.ERROR_NO_HANDLES); 353 hwndCB = mbi.hwndMB; 354 355 361 OS.ShowWindow (hwndCB, OS.SW_HIDE); 362 363 TBBUTTONINFO info = new TBBUTTONINFO (); 364 info.cbSize = TBBUTTONINFO.sizeof; 365 info.dwMask = OS.TBIF_COMMAND; 366 MenuItem item; 367 368 369 if (nToolBarId == ID_SPMM || nToolBarId == ID_SPMB) { 370 int hMenu = OS.SendMessage (hwndCB, OS.SHCMBM_GETSUBMENU, 0, ID_SPSOFTKEY0); 371 372 OS.RemoveMenu (hMenu, 0, OS.MF_BYPOSITION); 373 Menu menu = new Menu (parent, SWT.DROP_DOWN, hMenu); 374 item = new MenuItem (this, menu, SWT.CASCADE, 0); 375 } else { 376 item = new MenuItem (this, null, SWT.PUSH, 0); 377 } 378 info.idCommand = id0 = item.id; 379 OS.SendMessage (hwndCB, OS.TB_SETBUTTONINFO, ID_SPSOFTKEY0, info); 380 381 382 if (nToolBarId == ID_SPMM || nToolBarId == ID_SPBM) { 383 int hMenu = OS.SendMessage (hwndCB, OS.SHCMBM_GETSUBMENU, 0, ID_SPSOFTKEY1); 384 OS.RemoveMenu (hMenu, 0, OS.MF_BYPOSITION); 385 Menu menu = new Menu (parent, SWT.DROP_DOWN, hMenu); 386 item = new MenuItem (this, menu, SWT.CASCADE, 1); 387 } else { 388 item = new MenuItem (this, null, SWT.PUSH, 1); 389 } 390 info.idCommand = id1 = item.id; 391 OS.SendMessage (hwndCB, OS.TB_SETBUTTONINFO, ID_SPSOFTKEY1, info); 392 393 398 int dwMask = OS.SHMBOF_NODEFAULT | OS.SHMBOF_NOTIFY; 399 int lParam = dwMask << 16 | dwMask; 400 OS.SendMessage (hwndCB, OS.SHCMBM_OVERRIDEKEY, OS.VK_ESCAPE, lParam); 401 return; 402 } 403 handle = OS.CreateMenu (); 404 if (handle == 0) error (SWT.ERROR_NO_HANDLES); 405 if (OS.IsHPC) { 406 int hwndShell = parent.handle; 407 hwndCB = OS.CommandBar_Create (OS.GetModuleHandle (null), hwndShell, 1); 408 if (hwndCB == 0) error (SWT.ERROR_NO_HANDLES); 409 OS.CommandBar_Show (hwndCB, false); 410 OS.CommandBar_InsertMenubarEx (hwndCB, 0, handle, 0); 411 415 if ((parent.style & SWT.CLOSE) != 0 && (parent.style & SWT.TITLE) == 0) { 416 OS.CommandBar_AddAdornments (hwndCB, 0, 0); 417 } 418 } 419 } else { 420 handle = OS.CreatePopupMenu (); 421 if (handle == 0) error (SWT.ERROR_NO_HANDLES); 422 } 423 } 424 425 void createItem (MenuItem item, int index) { 426 int count = GetMenuItemCount (handle); 427 if (!(0 <= index && index <= count)) error (SWT.ERROR_INVALID_RANGE); 428 display.addMenuItem (item); 429 boolean success = false; 430 if ((OS.IsPPC || OS.IsSP) && hwndCB != 0) { 431 if (OS.IsSP) return; 432 TBBUTTON lpButton = new TBBUTTON (); 433 lpButton.idCommand = item.id; 434 lpButton.fsStyle = (byte) OS.TBSTYLE_AUTOSIZE; 435 if ((item.style & SWT.CASCADE) != 0) lpButton.fsStyle |= OS.TBSTYLE_DROPDOWN | 0x80; 436 if ((item.style & SWT.SEPARATOR) != 0) lpButton.fsStyle = (byte) OS.BTNS_SEP; 437 lpButton.fsState = (byte) OS.TBSTATE_ENABLED; 438 lpButton.iBitmap = OS.I_IMAGENONE; 439 success = OS.SendMessage (hwndCB, OS.TB_INSERTBUTTON, index, lpButton) != 0; 440 } else { 441 if (OS.IsWinCE) { 442 int uFlags = OS.MF_BYPOSITION; 443 TCHAR lpNewItem = null; 444 if ((item.style & SWT.SEPARATOR) != 0) { 445 uFlags |= OS.MF_SEPARATOR; 446 } else { 447 lpNewItem = new TCHAR (0, " ", true); 448 } 449 success = OS.InsertMenu (handle, index, uFlags, item.id, lpNewItem); 450 if (success) { 451 MENUITEMINFO info = new MENUITEMINFO (); 452 info.cbSize = MENUITEMINFO.sizeof; 453 info.fMask = OS.MIIM_DATA; 454 info.dwItemData = item.id; 455 success = OS.SetMenuItemInfo (handle, index, true, info); 456 } 457 } else { 458 470 int hHeap = OS.GetProcessHeap (); 471 TCHAR buffer = new TCHAR (0, " ", true); 472 int byteCount = buffer.length () * TCHAR.sizeof; 473 int pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 474 OS.MoveMemory (pszText, buffer, byteCount); 475 MENUITEMINFO info = new MENUITEMINFO (); 476 info.cbSize = MENUITEMINFO.sizeof; 477 info.fMask = OS.MIIM_ID | OS.MIIM_TYPE | OS.MIIM_DATA; 478 info.wID = info.dwItemData = item.id; 479 info.fType = item.widgetStyle (); 480 info.dwTypeData = pszText; 481 success = OS.InsertMenuItem (handle, index, true, info); 482 if (pszText != 0) OS.HeapFree (hHeap, 0, pszText); 483 } 484 } 485 if (!success) { 486 display.removeMenuItem (item); 487 error (SWT.ERROR_ITEM_NOT_ADDED); 488 } 489 redraw (); 490 } 491 492 void createWidget () { 493 507 createHandle (); 509 parent.addMenu (this); 510 } 511 512 int defaultBackground () { 513 return OS.GetSysColor (OS.COLOR_MENU); 514 } 515 516 int defaultForeground () { 517 return OS.GetSysColor (OS.COLOR_MENUTEXT); 518 } 519 520 void destroyAccelerators () { 521 parent.destroyAccelerators (); 522 } 523 524 void destroyItem (MenuItem item) { 525 if (OS.IsWinCE) { 526 if ((OS.IsPPC || OS.IsSP) && hwndCB != 0) { 527 if (OS.IsSP) { 528 redraw(); 529 return; 530 } 531 int index = OS.SendMessage (hwndCB, OS.TB_COMMANDTOINDEX, item.id, 0); 532 if (OS.SendMessage (hwndCB, OS.TB_DELETEBUTTON, index, 0) == 0) { 533 error (SWT.ERROR_ITEM_NOT_REMOVED); 534 } 535 int count = OS.SendMessage (hwndCB, OS.TB_BUTTONCOUNT, 0, 0); 536 if (count == 0) { 537 if (imageList != null) { 538 OS.SendMessage (handle, OS.TB_SETIMAGELIST, 0, 0); 539 display.releaseImageList (imageList); 540 imageList = null; 541 } 542 } 543 } else { 544 int index = 0; 545 MENUITEMINFO info = new MENUITEMINFO (); 546 info.cbSize = MENUITEMINFO.sizeof; 547 info.fMask = OS.MIIM_DATA; 548 while (OS.GetMenuItemInfo (handle, index, true, info)) { 549 if (info.dwItemData == item.id) break; 550 index++; 551 } 552 if (info.dwItemData != item.id) { 553 error (SWT.ERROR_ITEM_NOT_REMOVED); 554 } 555 if (!OS.DeleteMenu (handle, index, OS.MF_BYPOSITION)) { 556 error (SWT.ERROR_ITEM_NOT_REMOVED); 557 } 558 } 559 } else { 560 if (!OS.DeleteMenu (handle, item.id, OS.MF_BYCOMMAND)) { 561 error (SWT.ERROR_ITEM_NOT_REMOVED); 562 } 563 } 564 redraw (); 565 } 566 567 void destroyWidget () { 568 int hMenu = handle, hCB = hwndCB; 569 releaseHandle (); 570 if (OS.IsWinCE && hCB != 0) { 571 OS.CommandBar_Destroy (hCB); 572 } else { 573 if (hMenu != 0) OS.DestroyMenu (hMenu); 574 } 575 } 576 577 void fixMenus (Decorations newParent) { 578 MenuItem [] items = getItems (); 579 for (int i=0; i<items.length; i++) { 580 items [i].fixMenus (newParent); 581 } 582 parent.removeMenu (this); 583 newParent.addMenu (this); 584 this.parent = newParent; 585 } 586 587 599 Color getBackground () { 600 checkWidget (); 601 return Color.win32_new (display, background != -1 ? background : defaultBackground ()); 602 } 603 604 616 Image getBackgroundImage () { 617 checkWidget (); 618 return backgroundImage; 619 } 620 621 641 Rectangle getBounds () { 642 checkWidget (); 643 if (OS.IsWinCE) return new Rectangle (0, 0, 0, 0); 644 if ((style & SWT.BAR) != 0) { 645 if (parent.menuBar != this) { 646 return new Rectangle (0, 0, 0, 0); 647 } 648 int hwndShell = parent.handle; 649 MENUBARINFO info = new MENUBARINFO (); 650 info.cbSize = MENUBARINFO.sizeof; 651 if (OS.GetMenuBarInfo (hwndShell, OS.OBJID_MENU, 0, info)) { 652 int width = info.right - info.left; 653 int height = info.bottom - info.top; 654 return new Rectangle (info.left, info.top, width, height); 655 } 656 } else { 657 int count = GetMenuItemCount (handle); 658 if (count != 0) { 659 RECT rect1 = new RECT (); 660 if (OS.GetMenuItemRect (0, handle, 0, rect1)) { 661 RECT rect2 = new RECT (); 662 if (OS.GetMenuItemRect (0, handle, count - 1, rect2)) { 663 int x = rect1.left - 2, y = rect1.top - 2; 664 int width = (rect2.right - rect2.left) + 4; 665 int height = (rect2.bottom - rect1.top) + 4; 666 return new Rectangle (x, y, width, height); 667 } 668 } 669 } 670 } 671 return new Rectangle (0, 0, 0, 0); 672 } 673 674 686 public MenuItem getDefaultItem () { 687 checkWidget (); 688 if (OS.IsWinCE) return null; 689 int id = OS.GetMenuDefaultItem (handle, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED); 690 if (id == -1) return null; 691 MENUITEMINFO info = new MENUITEMINFO (); 692 info.cbSize = MENUITEMINFO.sizeof; 693 info.fMask = OS.MIIM_ID; 694 if (OS.GetMenuItemInfo (handle, id, false, info)) { 695 return display.getMenuItem (info.wID); 696 } 697 return null; 698 } 699 700 715 public boolean getEnabled () { 716 checkWidget (); 717 return (state & DISABLED) == 0; 718 } 719 720 730 Color getForeground () { 731 checkWidget (); 732 return Color.win32_new (display, foreground != -1 ? foreground : defaultForeground ()); 733 } 734 735 750 public MenuItem getItem (int index) { 751 checkWidget (); 752 int id = 0; 753 if ((OS.IsPPC || OS.IsSP) && hwndCB != 0) { 754 if (OS.IsPPC) { 755 TBBUTTON lpButton = new TBBUTTON (); 756 int result = OS.SendMessage (hwndCB, OS.TB_GETBUTTON, index, lpButton); 757 if (result == 0) error (SWT.ERROR_CANNOT_GET_ITEM); 758 id = lpButton.idCommand; 759 } 760 if (OS.IsSP) { 761 if (!(0 <= index && index <= 1)) error (SWT.ERROR_CANNOT_GET_ITEM); 762 id = index == 0 ? id0 : id1; 763 } 764 } else { 765 MENUITEMINFO info = new MENUITEMINFO (); 766 info.cbSize = MENUITEMINFO.sizeof; 767 info.fMask = OS.MIIM_DATA; 768 if (!OS.GetMenuItemInfo (handle, index, true, info)) { 769 error (SWT.ERROR_INVALID_RANGE); 770 } 771 id = info.dwItemData; 772 } 773 return display.getMenuItem (id); 774 } 775 776 786 public int getItemCount () { 787 checkWidget (); 788 return GetMenuItemCount (handle); 789 } 790 791 807 public MenuItem [] getItems () { 808 checkWidget (); 809 if ((OS.IsPPC || OS.IsSP) && hwndCB != 0) { 810 if (OS.IsSP) { 811 MenuItem [] result = new MenuItem [2]; 812 result[0] = display.getMenuItem (id0); 813 result[1] = display.getMenuItem (id1); 814 return result; 815 } 816 int count = OS.SendMessage (hwndCB, OS.TB_BUTTONCOUNT, 0, 0); 817 TBBUTTON lpButton = new TBBUTTON (); 818 MenuItem [] result = new MenuItem [count]; 819 for (int i=0; i<count; i++) { 820 OS.SendMessage (hwndCB, OS.TB_GETBUTTON, i, lpButton); 821 result [i] = display.getMenuItem (lpButton.idCommand); 822 } 823 return result; 824 } 825 int index = 0, count = 0; 826 int length = OS.IsWinCE ? 4 : OS.GetMenuItemCount (handle); 827 MenuItem [] items = new MenuItem [length]; 828 MENUITEMINFO info = new MENUITEMINFO (); 829 info.cbSize = MENUITEMINFO.sizeof; 830 info.fMask = OS.MIIM_DATA; 831 while (OS.GetMenuItemInfo (handle, index, true, info)) { 832 if (count == items.length) { 833 MenuItem [] newItems = new MenuItem [count + 4]; 834 System.arraycopy (items, 0, newItems, 0, count); 835 items = newItems; 836 } 837 MenuItem item = display.getMenuItem (info.dwItemData); 838 if (item != null) items [count++] = item; 839 index++; 840 } 841 if (count == items.length) return items; 842 MenuItem [] result = new MenuItem [count]; 843 System.arraycopy (items, 0, result, 0, count); 844 return result; 845 } 846 847 int GetMenuItemCount (int handle) { 848 if (OS.IsWinCE) { 849 if ((OS.IsPPC || OS.IsSP) && hwndCB != 0) { 850 return OS.IsSP ? 2 : OS.SendMessage (hwndCB, OS.TB_BUTTONCOUNT, 0, 0); 851 } 852 int count = 0; 853 MENUITEMINFO info = new MENUITEMINFO (); 854 info.cbSize = MENUITEMINFO.sizeof; 855 while (OS.GetMenuItemInfo (handle, count, true, info)) count++; 856 return count; 857 } 858 return OS.GetMenuItemCount (handle); 859 } 860 861 String getNameText () { 862 String result = ""; 863 MenuItem [] items = getItems (); 864 int length = items.length; 865 if (length > 0) { 866 for (int i=0; i<length-1; i++) { 867 result = result + items [i].getNameText() + ", "; 868 } 869 result = result + items [length-1].getNameText (); 870 } 871 return result; 872 } 873 874 884 public Decorations getParent () { 885 checkWidget (); 886 return parent; 887 } 888 889 901 public MenuItem getParentItem () { 902 checkWidget (); 903 return cascade; 904 } 905 906 918 public Menu getParentMenu () { 919 checkWidget (); 920 if (cascade != null) return cascade.parent; 921 return null; 922 } 923 924 939 public Shell getShell () { 940 checkWidget (); 941 return parent.getShell (); 942 } 943 944 961 public boolean getVisible () { 962 checkWidget (); 963 if ((style & SWT.BAR) != 0) { 964 return this == parent.menuShell ().menuBar; 965 } 966 if ((style & SWT.POP_UP) != 0) { 967 Menu [] popups = display.popups; 968 if (popups == null) return false; 969 for (int i=0; i<popups.length; i++) { 970 if (popups [i] == this) return true; 971 } 972 } 973 Shell shell = getShell (); 974 Menu menu = shell.activeMenu; 975 while (menu != null && menu != this) { 976 menu = menu.getParentMenu (); 977 } 978 return this == menu; 979 } 980 981 int imageIndex (Image image) { 982 if (hwndCB == 0 || image == null) return OS.I_IMAGENONE; 983 if (imageList == null) { 984 Rectangle bounds = image.getBounds (); 985 imageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height); 986 int index = imageList.add (image); 987 int hImageList = imageList.getHandle (); 988 OS.SendMessage (hwndCB, OS.TB_SETIMAGELIST, 0, hImageList); 989 return index; 990 } 991 int index = imageList.indexOf (image); 992 if (index == -1) { 993 index = imageList.add (image); 994 } else { 995 imageList.put (index, image); 996 } 997 return index; 998 } 999 1000 1017public int indexOf (MenuItem item) { 1018 checkWidget (); 1019 if (item == null) error (SWT.ERROR_NULL_ARGUMENT); 1020 if (item.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); 1021 if (item.parent != this) return -1; 1022 if ((OS.IsPPC || OS.IsSP) && hwndCB != 0) { 1023 if (OS.IsPPC) { 1024 return OS.SendMessage (hwndCB, OS.TB_COMMANDTOINDEX, item.id, 0); 1025 } 1026 if (OS.IsSP) { 1027 if (item.id == id0) return 0; 1028 if (item.id == id1) return 1; 1029 return -1; 1030 } 1031 } 1032 int index = 0; 1033 MENUITEMINFO info = new MENUITEMINFO (); 1034 info.cbSize = MENUITEMINFO.sizeof; 1035 info.fMask = OS.MIIM_DATA; 1036 while (OS.GetMenuItemInfo (handle, index, true, info)) { 1037 if (info.dwItemData == item.id) return index; 1038 index++; 1039 } 1040 return -1; 1041} 1042 1043 1058public boolean isEnabled () { 1059 checkWidget (); 1060 Menu parentMenu = getParentMenu (); 1061 if (parentMenu == null) { 1062 return getEnabled () && parent.isEnabled (); 1063 } 1064 return getEnabled () && parentMenu.isEnabled (); 1065} 1066 1067 1081public boolean isVisible () { 1082 checkWidget (); 1083 return getVisible (); 1084} 1085 1086void redraw () { 1087 if (!isVisible ()) return; 1088 if ((style & SWT.BAR) != 0) { 1089 display.addBar (this); 1090 } else { 1091 update (); 1092 } 1093} 1094 1095void releaseHandle () { 1096 super.releaseHandle (); 1097 handle = hwndCB = 0; 1098} 1099 1100void releaseChildren (boolean destroy) { 1101 MenuItem [] items = getItems (); 1102 for (int i=0; i<items.length; i++) { 1103 MenuItem item = items [i]; 1104 if (item != null && !item.isDisposed ()) { 1105 if (OS.IsPPC && hwndCB != 0) { 1106 item.dispose (); 1107 } else { 1108 item.release (false); 1109 } 1110 } 1111 } 1112 super.releaseChildren (destroy); 1113} 1114 1115void releaseParent () { 1116 super.releaseParent (); 1117 if (cascade != null) cascade.releaseMenu (); 1118 if ((style & SWT.BAR) != 0) { 1119 display.removeBar (this); 1120 if (this == parent.menuBar) { 1121 parent.setMenuBar (null); 1122 } 1123 } else { 1124 if ((style & SWT.POP_UP) != 0) { 1125 display.removePopup (this); 1126 } 1127 } 1128} 1129 1130void releaseWidget () { 1131 super.releaseWidget (); 1132 backgroundImage = null; 1133 if (hBrush == 0) OS.DeleteObject (hBrush); 1134 hBrush = 0; 1135 if (OS.IsPPC && hwndCB != 0) { 1136 if (imageList != null) { 1137 OS.SendMessage (hwndCB, OS.TB_SETIMAGELIST, 0, 0); 1138 display.releaseToolImageList (imageList); 1139 imageList = null; 1140 } 1141 } 1142 if (parent != null) parent.removeMenu (this); 1143 parent = null; 1144 cascade = null; 1145} 1146 1147 1164public void removeHelpListener (HelpListener listener) { 1165 checkWidget (); 1166 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 1167 if (eventTable == null) return; 1168 eventTable.unhook (SWT.Help, listener); 1169} 1170 1171 1188public void removeMenuListener (MenuListener listener) { 1189 checkWidget (); 1190 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 1191 if (eventTable == null) return; 1192 eventTable.unhook (SWT.Hide, listener); 1193 eventTable.unhook (SWT.Show, listener); 1194} 1195 1196 1213 void setBackground (Color color) { 1214 checkWidget (); 1215 int pixel = -1; 1216 if (color != null) { 1217 if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 1218 pixel = color.handle; 1219 } 1220 if (pixel == background) return; 1221 background = pixel; 1222 updateBackground (); 1223} 1224 1225 1244 void setBackgroundImage (Image image) { 1245 checkWidget (); 1246 if (image != null) { 1247 if (image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); 1248 if (image.type != SWT.BITMAP) error (SWT.ERROR_INVALID_ARGUMENT); 1249 } 1250 if (backgroundImage == image) return; 1251 backgroundImage = image; 1252 updateBackground (); 1253} 1254 1255 1272 void setForeground (Color color) { 1273 checkWidget (); 1274 int pixel = -1; 1275 if (color != null) { 1276 if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 1277 pixel = color.handle; 1278 } 1279 if (pixel == foreground) return; 1280 foreground = pixel; 1281 updateForeground (); 1282} 1283 1284 1298public void setDefaultItem (MenuItem item) { 1299 checkWidget (); 1300 int newID = -1; 1301 if (item != null) { 1302 if (item.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); 1303 if (item.parent != this) return; 1304 newID = item.id; 1305 } 1306 if (OS.IsWinCE) return; 1307 int oldID = OS.GetMenuDefaultItem (handle, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED); 1308 if (newID == oldID) return; 1309 OS.SetMenuDefaultItem (handle, newID, OS.MF_BYCOMMAND); 1310 redraw (); 1311} 1312 1313 1326public void setEnabled (boolean enabled) { 1327 checkWidget (); 1328 state &= ~DISABLED; 1329 if (!enabled) state |= DISABLED; 1330} 1331 1332 1352public void setLocation (int x, int y) { 1353 checkWidget (); 1354 if ((style & (SWT.BAR | SWT.DROP_DOWN)) != 0) return; 1355 this.x = x; 1356 this.y = y; 1357 hasLocation = true; 1358} 1359 1360 1384public void setLocation (Point location) { 1385 checkWidget (); 1386 if (location == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 1387 setLocation (location.x, location.y); 1388} 1389 1390 1406public void setVisible (boolean visible) { 1407 checkWidget (); 1408 if ((style & (SWT.BAR | SWT.DROP_DOWN)) != 0) return; 1409 if (visible) { 1410 display.addPopup (this); 1411 } else { 1412 display.removePopup (this); 1413 _setVisible (false); 1414 } 1415} 1416 1417void update () { 1418 if (OS.IsPPC || OS.IsSP) return; 1419 if (OS.IsHPC) { 1420 1425 Menu menuBar = parent.menuBar; 1426 if (menuBar != null) { 1427 Menu menu = this; 1428 while (menu != null && menu != menuBar) { 1429 menu = menu.getParentMenu (); 1430 } 1431 if (menu == menuBar) { 1432 OS.CommandBar_DrawMenuBar (menuBar.hwndCB, 0); 1433 OS.CommandBar_Show (menuBar.hwndCB, true); 1434 } 1435 } 1436 return; 1437 } 1438 if (OS.IsWinCE) return; 1439 if ((style & SWT.BAR) != 0) { 1440 if (this == parent.menuBar) OS.DrawMenuBar (parent.handle); 1441 return; 1442 } 1443 if (OS.WIN32_VERSION < OS.VERSION (4, 10)) { 1444 return; 1445 } 1446 boolean hasCheck = false, hasImage = false; 1447 MenuItem [] items = getItems (); 1448 for (int i=0; i<items.length; i++) { 1449 MenuItem item = items [i]; 1450 if (item.image != null) { 1451 if ((hasImage = true) && hasCheck) break; 1452 } 1453 if ((item.style & (SWT.CHECK | SWT.RADIO)) != 0) { 1454 if ((hasCheck = true) && hasImage) break; 1455 } 1456 } 1457 1458 1477 if (!OS.IsWin95) { 1478 if (OS.WIN32_VERSION < OS.VERSION (6, 0)) { 1479 MENUITEMINFO info = new MENUITEMINFO (); 1480 info.cbSize = MENUITEMINFO.sizeof; 1481 info.fMask = OS.MIIM_BITMAP; 1482 for (int i=0; i<items.length; i++) { 1483 MenuItem item = items [i]; 1484 if ((style & SWT.SEPARATOR) == 0) { 1485 if (item.image == null || foreground != -1) { 1486 info.hbmpItem = hasImage || foreground != -1 ? OS.HBMMENU_CALLBACK : 0; 1487 OS.SetMenuItemInfo (handle, item.id, false, info); 1488 } 1489 } 1490 } 1491 } 1492 } 1493 1494 1495 MENUINFO lpcmi = new MENUINFO (); 1496 lpcmi.cbSize = MENUINFO.sizeof; 1497 lpcmi.fMask = OS.MIM_STYLE; 1498 OS.GetMenuInfo (handle, lpcmi); 1499 if (hasImage && !hasCheck) { 1500 lpcmi.dwStyle |= OS.MNS_CHECKORBMP; 1501 } else { 1502 lpcmi.dwStyle &= ~OS.MNS_CHECKORBMP; 1503 } 1504 OS.SetMenuInfo (handle, lpcmi); 1505} 1506 1507void updateBackground () { 1508 if (hBrush == 0) OS.DeleteObject (hBrush); 1509 hBrush = 0; 1510 if (backgroundImage != null) { 1511 hBrush = OS.CreatePatternBrush (backgroundImage.handle); 1512 } else { 1513 if (background != -1) hBrush = OS.CreateSolidBrush (background); 1514 } 1515 MENUINFO lpcmi = new MENUINFO (); 1516 lpcmi.cbSize = MENUINFO.sizeof; 1517 lpcmi.fMask = OS.MIM_BACKGROUND; 1518 lpcmi.hbrBack = hBrush; 1519 OS.SetMenuInfo (handle, lpcmi); 1520} 1521 1522void updateForeground () { 1523 if (OS.WIN32_VERSION < OS.VERSION (4, 10)) return; 1524 MENUITEMINFO info = new MENUITEMINFO (); 1525 info.cbSize = MENUITEMINFO.sizeof; 1526 int index = 0; 1527 while (OS.GetMenuItemInfo (handle, index, true, info)) { 1528 info.fMask = OS.MIIM_BITMAP; 1529 info.hbmpItem = OS.HBMMENU_CALLBACK; 1530 OS.SetMenuItemInfo (handle, index, true, info); 1531 index++; 1532 } 1533 redraw (); 1534} 1535} 1536 | Popular Tags |