示例#1
0
  /**
   * Allocates the operating system resources associated with the receiver.
   *
   * @param device the device on which to allocate the color
   * @param red the amount of red in the color
   * @param green the amount of green in the color
   * @param blue the amount of blue in the color
   * @param alpha the amount of alpha in the color. Currently, SWT only honors extreme values for
   *     alpha i.e. 0 (transparent) or 255 (opaque).
   * @exception IllegalArgumentException
   *     <ul>
   *       <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0
   *           and 255
   *     </ul>
   *
   * @see #dispose
   */
  void init(int red, int green, int blue, int alpha) {
    if (red > 255
        || red < 0
        || green > 255
        || green < 0
        || blue > 255
        || blue < 0
        || alpha > 255
        || alpha < 0) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    handle = (red & 0xFF) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 16);
    this.alpha = alpha;

    /* If this is not a palette-based device, return */
    long /*int*/ hPal = device.hPalette;
    if (hPal == 0) return;

    int[] colorRefCount = device.colorRefCount;
    /* Add this color to the default palette now */
    /* First find out if the color already exists */
    int index = OS.GetNearestPaletteIndex(hPal, handle);
    /* See if the nearest color actually is the color */
    byte[] entry = new byte[4];
    OS.GetPaletteEntries(hPal, index, 1, entry);
    if ((entry[0] == (byte) red) && (entry[1] == (byte) green) && (entry[2] == (byte) blue)) {
      /* Found the color. Increment the ref count and return */
      colorRefCount[index]++;
      return;
    }
    /* Didn't find the color, allocate it now. Find the first free entry */
    int i = 0;
    while (i < colorRefCount.length) {
      if (colorRefCount[i] == 0) {
        index = i;
        break;
      }
      i++;
    }
    if (i == colorRefCount.length) {
      /* No free entries, use the closest one */
      /* Remake the handle from the actual rgbs */
      handle = (entry[0] & 0xFF) | ((entry[1] & 0xFF) << 8) | ((entry[2] & 0xFF) << 16);
    } else {
      /* Found a free entry */
      entry = new byte[] {(byte) (red & 0xFF), (byte) (green & 0xFF), (byte) (blue & 0xFF), 0};
      OS.SetPaletteEntries(hPal, index, 1, entry);
    }
    colorRefCount[index]++;
  }
 /**
  * Sets the selection state of the receiver.
  *
  * <p>When the receiver is of type <code>CHECK</code> or <code>RADIO</code>, it is selected when
  * it is checked.
  *
  * @param selected the new selection 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 setSelection(boolean selected) {
   checkWidget();
   if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return;
   if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) return;
   long /*int*/ hMenu = parent.handle;
   if (OS.IsWinCE) {
     int index = parent.indexOf(this);
     if (index == -1) return;
     int uCheck = OS.MF_BYPOSITION | (selected ? OS.MF_CHECKED : OS.MF_UNCHECKED);
     OS.CheckMenuItem(hMenu, index, uCheck);
   } else {
     MENUITEMINFO info = new MENUITEMINFO();
     info.cbSize = MENUITEMINFO.sizeof;
     info.fMask = OS.MIIM_STATE;
     boolean success = OS.GetMenuItemInfo(hMenu, id, false, info);
     if (!success) error(SWT.ERROR_CANNOT_SET_SELECTION);
     info.fState &= ~OS.MFS_CHECKED;
     if (selected) info.fState |= OS.MFS_CHECKED;
     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_SELECTION,
             null,
             " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$
       }
     }
   }
   parent.redraw();
 }
  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 receiver's text. The string may include the mnemonic character and accelerator text.
   *
   * <p>Mnemonics are indicated by an '&amp;' that causes the next character to be the mnemonic.
   * When the user presses a key sequence that matches the mnemonic, a selection event occurs. On
   * most platforms, the mnemonic appears underlined but may be emphasised in a platform specific
   * manner. The mnemonic indicator character '&amp;' can be escaped by doubling it in the string,
   * causing a single '&amp;' to be displayed.
   *
   * <p>Accelerator text is indicated by the '\t' character. On platforms that support accelerator
   * text, the text that follows the '\t' character is displayed to the user, typically indicating
   * the key stroke that will cause the item to become selected. On most platforms, the accelerator
   * text appears right aligned in the menu. Setting the accelerator text does not install the
   * accelerator key sequence. The accelerator key sequence is installed using #setAccelerator.
   *
   * @param string the new text
   * @exception IllegalArgumentException
   *     <ul>
   *       <li>ERROR_NULL_ARGUMENT - if the text is null
   *     </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>
   *
   * @see #setAccelerator
   */
  public void setText(String string) {
    checkWidget();
    if (string == null) error(SWT.ERROR_NULL_ARGUMENT);
    if ((style & SWT.SEPARATOR) != 0) return;
    if (text.equals(string)) return;
    super.setText(string);
    long /*int*/ hHeap = OS.GetProcessHeap();
    long /*int*/ pszText = 0;
    boolean success = false;
    if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) {
      /*
       * Bug in WinCE PPC.  Tool items on the menubar don't resize
       * correctly when the character '&' is used (even when it
       * is a sequence '&&').  The fix is to remove all '&' from
       * the string.
       */
      if (string.indexOf('&') != -1) {
        int length = string.length();
        char[] text = new char[length];
        string.getChars(0, length, text, 0);
        int i = 0, j = 0;
        for (i = 0; i < length; i++) {
          if (text[i] != '&') text[j++] = text[i];
        }
        if (j < i) string = new String(text, 0, j);
      }
      /* Use the character encoding for the default locale */
      TCHAR buffer = new TCHAR(0, string, true);
      int byteCount = buffer.length() * TCHAR.sizeof;
      pszText = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
      OS.MoveMemory(pszText, buffer, byteCount);
      long /*int*/ hwndCB = parent.hwndCB;
      TBBUTTONINFO info2 = new TBBUTTONINFO();
      info2.cbSize = TBBUTTONINFO.sizeof;
      info2.dwMask = OS.TBIF_TEXT;
      info2.pszText = pszText;
      success = OS.SendMessage(hwndCB, OS.TB_SETBUTTONINFO, id, info2) != 0;
    } else {
      MENUITEMINFO info = new MENUITEMINFO();
      info.cbSize = MENUITEMINFO.sizeof;
      long /*int*/ hMenu = parent.handle;

      /* Use the character encoding for the default locale */
      TCHAR buffer = new TCHAR(0, string, true);
      int byteCount = buffer.length() * TCHAR.sizeof;
      pszText = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
      OS.MoveMemory(pszText, buffer, byteCount);
      /*
       * Bug in Windows 2000.  For some reason, when MIIM_TYPE is set
       * on a menu item that also has MIIM_BITMAP, the MIIM_TYPE clears
       * the MIIM_BITMAP style.  The fix is to use MIIM_STRING.
       */
      if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) {
        info.fMask = OS.MIIM_STRING;
      } else {
        info.fMask = OS.MIIM_TYPE;
        info.fType = widgetStyle();
      }
      info.dwTypeData = pszText;
      success = OS.SetMenuItemInfo(hMenu, id, false, info);
    }
    if (pszText != 0) OS.HeapFree(hHeap, 0, pszText);
    if (!success) {
      int error = OS.GetLastError();
      SWT.error(
          SWT.ERROR_CANNOT_SET_TEXT,
          null,
          " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$
    }
    parent.redraw();
  }
示例#6
0
 /**
  * Returns an <code>RGBA</code> representing the receiver.
  *
  * @return the RGBA for the color
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  *     </ul>
  *
  * @since 3.104
  */
 public RGBA getRGBA() {
   if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
   return new RGBA(handle & 0xFF, (handle & 0xFF00) >> 8, (handle & 0xFF0000) >> 16, alpha);
 }
示例#7
0
 /**
  * Returns the amount of red in the color, from 0 to 255.
  *
  * @return the red component of the color
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  *     </ul>
  */
 public int getRed() {
   if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
   return handle & 0xFF;
 }
示例#8
0
 /**
  * Returns the amount of green in the color, from 0 to 255.
  *
  * @return the green component of the color
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  *     </ul>
  */
 public int getGreen() {
   if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
   return (handle & 0xFF00) >> 8;
 }
示例#9
0
 /**
  * Constructs a new instance of this class given a device, an <code>RGB</code> describing the
  * desired red, green and blue values, alpha specifying the level of transparency. On limited
  * color devices, the color instance created by this call may not have the same RGB values as the
  * ones specified by the argument. The RGB values on the returned instance will be the color
  * values of the operating system color.
  *
  * <p>You must dispose the color when it is no longer required.
  *
  * @param device the device on which to allocate the color
  * @param rgb the RGB values of the desired color
  * @param alpha the alpha value of the desired color. Currently, SWT only honors extreme values
  *     for alpha i.e. 0 (transparent) or 255 (opaque).
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device
  *       <li>ERROR_NULL_ARGUMENT - if the rgb argument is null
  *       <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument
  *           are not between 0 and 255
  *     </ul>
  *
  * @see #dispose
  * @since 3.104
  */
 public Color(Device device, RGB rgb, int alpha) {
   super(device);
   if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   init(rgb.red, rgb.green, rgb.blue, alpha);
   init();
 }