void releaseWidget() { super.releaseWidget(); if (hBitmap != 0) OS.DeleteObject(hBitmap); hBitmap = 0; if (accelerator != 0) { parent.destroyAccelerators(); } accelerator = 0; display.removeMenuItem(this); }
void setMenu(Menu menu, boolean dispose) { /* Assign the new menu */ Menu oldMenu = this.menu; if (oldMenu == menu) return; if (oldMenu != null) oldMenu.cascade = null; this.menu = menu; /* Assign the new menu in the OS */ if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { if (OS.IsPPC) { long /*int*/ hwndCB = parent.hwndCB; long /*int*/ hMenu = menu == null ? 0 : menu.handle; OS.SendMessage(hwndCB, OS.SHCMBM_SETSUBMENU, id, hMenu); } if (OS.IsSP) error(SWT.ERROR_CANNOT_SET_MENU); } else { long /*int*/ hMenu = parent.handle; MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_DATA; int index = 0; while (OS.GetMenuItemInfo(hMenu, index, true, info)) { if (info.dwItemData == id) break; index++; } if (info.dwItemData != id) return; int cch = 128; long /*int*/ hHeap = OS.GetProcessHeap(); int byteCount = cch * TCHAR.sizeof; long /*int*/ pszText = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount); info.fMask = OS.MIIM_STATE | OS.MIIM_ID | OS.MIIM_DATA; /* * Bug in Windows. When GetMenuItemInfo() is used to get the text, * for an item that has a bitmap set using MIIM_BITMAP, the text is * not returned. This means that when SetMenuItemInfo() is used to * set the submenu and the current menu state, the text is lost. * The fix is use MIIM_BITMAP and MIIM_STRING. */ if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) { info.fMask |= OS.MIIM_BITMAP | OS.MIIM_STRING; } else { info.fMask |= OS.MIIM_TYPE; } info.dwTypeData = pszText; info.cch = cch; boolean success = OS.GetMenuItemInfo(hMenu, index, true, info); if (menu != null) { menu.cascade = this; info.fMask |= OS.MIIM_SUBMENU; info.hSubMenu = menu.handle; } if (OS.IsWinCE) { OS.RemoveMenu(hMenu, index, OS.MF_BYPOSITION); /* * On WinCE, InsertMenuItem() is not available. The fix is to * use SetMenuItemInfo() but this call does not set the menu item * state and submenu. The fix is to use InsertMenu() to insert * the item, SetMenuItemInfo() to set the string and EnableMenuItem() * and CheckMenuItem() to set the state. */ long /*int*/ uIDNewItem = id; int uFlags = OS.MF_BYPOSITION; if (menu != null) { uFlags |= OS.MF_POPUP; uIDNewItem = menu.handle; } TCHAR lpNewItem = new TCHAR(0, " ", true); success = OS.InsertMenu(hMenu, index, uFlags, uIDNewItem, lpNewItem); if (success) { info.fMask = OS.MIIM_DATA | OS.MIIM_TYPE; success = OS.SetMenuItemInfo(hMenu, index, true, info); if ((info.fState & (OS.MFS_DISABLED | OS.MFS_GRAYED)) != 0) { OS.EnableMenuItem(hMenu, index, OS.MF_BYPOSITION | OS.MF_GRAYED); } if ((info.fState & OS.MFS_CHECKED) != 0) { OS.CheckMenuItem(hMenu, index, OS.MF_BYPOSITION | OS.MF_CHECKED); } } } else { if (dispose || oldMenu == null) { success = OS.SetMenuItemInfo(hMenu, index, true, info); } else { /* * Feature in Windows. When SetMenuItemInfo () is used to * set a submenu and the menu item already has a submenu, * Windows destroys the previous menu. This is undocumented * and unexpected but not necessarily wrong. The fix is to * remove the item with RemoveMenu () which does not destroy * the submenu and then insert the item with InsertMenuItem (). */ OS.RemoveMenu(hMenu, index, OS.MF_BYPOSITION); success = OS.InsertMenuItem(hMenu, index, true, info); } } if (pszText != 0) OS.HeapFree(hHeap, 0, pszText); if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_MENU, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } } parent.destroyAccelerators(); }
/** * Enables the receiver if the argument is <code>true</code>, and disables it otherwise. A * disabled menu item is typically not selectable from the user interface and draws with an * inactive or "grayed" look. * * @param enabled the new enabled state * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setEnabled(boolean enabled) { checkWidget(); if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { long /*int*/ hwndCB = parent.hwndCB; TBBUTTONINFO info = new TBBUTTONINFO(); info.cbSize = TBBUTTONINFO.sizeof; info.dwMask = OS.TBIF_STATE; OS.SendMessage(hwndCB, OS.TB_GETBUTTONINFO, id, info); info.fsState &= ~OS.TBSTATE_ENABLED; if (enabled) info.fsState |= OS.TBSTATE_ENABLED; OS.SendMessage(hwndCB, OS.TB_SETBUTTONINFO, id, info); } else { /* * Feature in Windows. For some reason, when the menu item * is a separator, GetMenuItemInfo() always indicates that * the item is not enabled. The fix is to track the enabled * state for separators. */ if ((style & SWT.SEPARATOR) != 0) { if (enabled) { state &= ~DISABLED; } else { state |= DISABLED; } } long /*int*/ hMenu = parent.handle; if (OS.IsWinCE) { int index = parent.indexOf(this); if (index == -1) return; int uEnable = OS.MF_BYPOSITION | (enabled ? OS.MF_ENABLED : OS.MF_GRAYED); OS.EnableMenuItem(hMenu, index, uEnable); } else { MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_STATE; boolean success = OS.GetMenuItemInfo(hMenu, id, false, info); if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_ENABLED, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } int bits = OS.MFS_DISABLED | OS.MFS_GRAYED; if (enabled) { if ((info.fState & bits) == 0) return; info.fState &= ~bits; } else { if ((info.fState & bits) == bits) return; info.fState |= bits; } success = OS.SetMenuItemInfo(hMenu, id, false, info); if (!success) { /* * Bug in Windows. For some reason SetMenuItemInfo(), * returns a fail code when setting the enabled or * selected state of a default item, but sets the * state anyway. The fix is to ignore the error. * * NOTE: This only happens on Vista. */ if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(6, 0)) { success = id == OS.GetMenuDefaultItem(hMenu, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED); } if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_ENABLED, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } } } } parent.destroyAccelerators(); parent.redraw(); }
/** * Sets the widget accelerator. An accelerator is the bit-wise OR of zero or more modifier masks * and a key. Examples: <code>SWT.MOD1 | SWT.MOD2 | 'T', SWT.MOD3 | SWT.F2</code>. <code> * SWT.CONTROL | SWT.SHIFT | 'T', SWT.ALT | SWT.F2</code>. The default value is zero, indicating * that the menu item does not have an accelerator. * * @param accelerator an integer that is the bit-wise OR of masks and a key * </ul> * * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setAccelerator(int accelerator) { checkWidget(); if (this.accelerator == accelerator) return; this.accelerator = accelerator; parent.destroyAccelerators(); }