Esempio n. 1
0
 /**
  * Sets the control that is used to fill the bounds of the item when the item is a <code>SEPARATOR
  * </code>.
  *
  * @param control the new control
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_INVALID_ARGUMENT - if the control has been disposed
  *       <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree
  *     </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 setControl(Control control) {
   checkWidget();
   if (control != null) {
     if (control.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
     if (control.parent != parent) error(SWT.ERROR_INVALID_PARENT);
   }
   if ((style & SWT.SEPARATOR) == 0) return;
   this.control = control;
   /*
    * Feature in Windows.  When a tool bar wraps, tool items
    * with the style BTNS_SEP are used as wrap points.  This
    * means that controls that are placed on top of separator
    * items are not positioned properly.  Also, vertical tool
    * bars are implemented using TB_SETROWS to set the number
    * of rows.  When a control is placed on top of a separator,
    * the height of the separator does not grow.  The fix in
    * both cases is to change the tool item style from BTNS_SEP
    * to BTNS_BUTTON, causing the item to wrap like a tool item
    * button.  The new tool item button is disabled to avoid key
    * traversal and the image is set to I_IMAGENONE to avoid
    * getting the first image from the image list.
    */
   if ((parent.style & (SWT.WRAP | SWT.VERTICAL)) != 0) {
     boolean changed = false;
     long /*int*/ hwnd = parent.handle;
     TBBUTTONINFO info = new TBBUTTONINFO();
     info.cbSize = TBBUTTONINFO.sizeof;
     info.dwMask = OS.TBIF_STYLE | OS.TBIF_STATE;
     OS.SendMessage(hwnd, OS.TB_GETBUTTONINFO, id, info);
     if (control == null) {
       if ((info.fsStyle & OS.BTNS_SEP) == 0) {
         changed = true;
         info.fsStyle &= ~(OS.BTNS_BUTTON | OS.BTNS_SHOWTEXT);
         info.fsStyle |= OS.BTNS_SEP;
         if ((state & DISABLED) != 0) {
           info.fsState &= ~OS.TBSTATE_ENABLED;
         } else {
           info.fsState |= OS.TBSTATE_ENABLED;
         }
       }
     } else {
       if ((info.fsStyle & OS.BTNS_SEP) != 0) {
         changed = true;
         info.fsStyle &= ~OS.BTNS_SEP;
         info.fsStyle |= OS.BTNS_BUTTON | OS.BTNS_SHOWTEXT;
         info.fsState &= ~OS.TBSTATE_ENABLED;
         info.dwMask |= OS.TBIF_IMAGE;
         info.iImage = OS.I_IMAGENONE;
       }
     }
     if (changed) {
       OS.SendMessage(hwnd, OS.TB_SETBUTTONINFO, id, info);
       /*
        * Bug in Windows.  When TB_SETBUTTONINFO changes the
        * style of a tool item from BTNS_SEP to BTNS_BUTTON
        * and the tool bar is wrapped, the tool bar does not
        * redraw properly.  Windows uses separator items as
        * wrap points and sometimes draws etching above or
        * below and entire row.  The fix is to redraw the
        * tool bar.
        */
       if (OS.SendMessage(hwnd, OS.TB_GETROWS, 0, 0) > 1) {
         OS.InvalidateRect(hwnd, null, true);
       }
     }
   }
   resizeControl();
 }
 /**
  * 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();
 }