/**
  * Adds the listener to the collection of listeners who will be notified when the user changes the
  * receiver's selection, by sending it one of the messages defined in the <code>SelectionListener
  * </code> interface.
  *
  * <p>When <code>widgetSelected</code> is called, the item field of the event object is valid. If
  * the receiver has <code>SWT.CHECK</code> style set and the check selection changes, the event
  * object detail field contains the value <code>SWT.CHECK</code>. <code>widgetDefaultSelected
  * </code> is typically called when an item is double-clicked.
  *
  * @param listener the listener which should be notified when the user changes the receiver's
  *     selection
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if the listener 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 SelectionListener
  * @see SelectionEvent
  * @see #removeSelectionListener(SelectionListener)
  */
 public void addSelectionListener(SelectionListener listener) {
   checkWidget();
   if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   TypedListener typedListener = new TypedListener(listener);
   addListener(SWT.Selection, typedListener);
   addListener(SWT.DefaultSelection, typedListener);
 }
 /**
  * Positions the TableCursor over the cell at the given row and column in the parent table.
  *
  * @param row the TableItem of the row for the cell to select
  * @param column the index of column for the cell to select
  * @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(TableItem row, int column) {
   checkWidget();
   int columnCount = table.getColumnCount();
   int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1;
   if (row == null || row.isDisposed() || column < 0 || column > maxColumnIndex)
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   setRowColumn(table.indexOf(row), column, false);
 }
 /**
  * Positions the TableCursor over the cell at the given row and column in the parent table.
  *
  * @param row the index of the row for the cell to select
  * @param column the index of column for the cell to select
  * @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(int row, int column) {
   checkWidget();
   int columnCount = table.getColumnCount();
   int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1;
   if (row < 0 || row >= table.getItemCount() || column < 0 || column > maxColumnIndex)
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   setRowColumn(row, column, false);
 }
 /**
  * Removes the listener from the collection of listeners who will be notified when the user
  * changes the receiver's selection.
  *
  * @param listener the listener which should no longer be notified
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if the listener 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 SelectionListener
  * @see #addSelectionListener(SelectionListener)
  * @since 3.0
  */
 public void removeSelectionListener(SelectionListener listener) {
   checkWidget();
   if (listener == null) {
     SWT.error(SWT.ERROR_NULL_ARGUMENT);
   }
   removeListener(SWT.Selection, listener);
   removeListener(SWT.DefaultSelection, listener);
 }
Example #5
0
 /**
  * Set the horizontal alignment of the CLabel. Use the values LEFT, CENTER and RIGHT to align
  * image and text within the available space.
  *
  * @param align the alignment style of LEFT, RIGHT or CENTER
  * @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
  *       <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or
  *           SWT.CENTER
  *     </ul>
  */
 public void setAlignment(int align) {
   checkWidget();
   if (align != SWT.LEFT && align != SWT.RIGHT && align != SWT.CENTER) {
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   }
   if (this.align != align) {
     this.align = align;
     redraw();
   }
 }
 /**
  * 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();
 }
Example #7
0
  int XmNexposureCallback(int w, int client_data, int call_data) {
    if ((style & SWT.SEPARATOR) != 0) return 0;
    int xDisplay = OS.XtDisplay(handle);
    if (xDisplay == 0) return 0;
    int xWindow = OS.XtWindow(handle);
    if (xWindow == 0) return 0;
    int[] argList = {
      OS.XmNcolormap, 0,
      OS.XmNwidth, 0,
      OS.XmNheight, 0,
    };
    OS.XtGetValues(handle, argList, argList.length / 2);
    int width = argList[3], height = argList[5];

    Image currentImage = image;
    boolean enabled = getEnabled();

    if ((parent.style & SWT.FLAT) != 0) {
      boolean hasCursor = hasCursor();

      /* Set the shadow thickness */
      int thickness = 0;
      if (set || (hasCursor && enabled)) {
        thickness = Math.min(2, display.buttonShadowThickness);
      }
      argList = new int[] {OS.XmNshadowThickness, thickness};
      OS.XtSetValues(handle, argList, argList.length / 2);

      /* Determine if hot image should be used */
      if (enabled && hasCursor && hotImage != null) {
        currentImage = hotImage;
      }
    }

    GCData data = new GCData();
    data.device = display;
    data.display = xDisplay;
    data.drawable = xWindow;
    data.font = parent.font;
    data.colormap = argList[1];
    int xGC = OS.XCreateGC(xDisplay, xWindow, 0, null);
    if (xGC == 0) SWT.error(SWT.ERROR_NO_HANDLES);
    GC gc = GC.motif_new(xGC, data);

    XmAnyCallbackStruct cb = new XmAnyCallbackStruct();
    OS.memmove(cb, call_data, XmAnyCallbackStruct.sizeof);
    if (cb.event != 0) {
      XExposeEvent xEvent = new XExposeEvent();
      OS.memmove(xEvent, cb.event, XExposeEvent.sizeof);
      Rectangle rect = new Rectangle(xEvent.x, xEvent.y, xEvent.width, xEvent.height);
      gc.setClipping(rect);
    }

    if (!enabled) {
      currentImage = disabledImage;
      if (currentImage == null && image != null) {
        currentImage = new Image(display, image, SWT.IMAGE_DISABLE);
      }
      Color disabledColor = display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
      gc.setForeground(disabledColor);
    } else {
      gc.setForeground(parent.getForeground());
    }
    gc.setBackground(parent.getBackground());

    int textX = 0, textY = 0, textWidth = 0, textHeight = 0;
    if (text.length() != 0) {
      int flags = SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC;
      Point textExtent = gc.textExtent(text, flags);
      textWidth = textExtent.x;
      textHeight = textExtent.y;
    }
    int imageX = 0, imageY = 0, imageWidth = 0, imageHeight = 0;
    if (currentImage != null) {
      Rectangle imageBounds = currentImage.getBounds();
      imageWidth = imageBounds.width;
      imageHeight = imageBounds.height;
    }

    int spacing = 0;
    if (textWidth != 0 && imageWidth != 0) spacing = 2;
    if ((parent.style & SWT.RIGHT) != 0) {
      imageX = (width - imageWidth - textWidth - spacing) / 2;
      imageY = (height - imageHeight) / 2;
      textX = spacing + imageX + imageWidth;
      textY = (height - textHeight) / 2;
    } else {
      imageX = (width - imageWidth) / 2;
      imageY = (height - imageHeight - textHeight - spacing) / 2;
      textX = (width - textWidth) / 2;
      textY = spacing + imageY + imageHeight;
    }

    if ((style & SWT.DROP_DOWN) != 0) {
      textX -= 6;
      imageX -= 6;
    }
    if (textWidth > 0) {
      int flags = SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC | SWT.DRAW_TRANSPARENT;
      gc.drawText(text, textX, textY, flags);
    }
    if (imageWidth > 0) gc.drawImage(currentImage, imageX, imageY);
    if ((style & SWT.DROP_DOWN) != 0) {
      int startX = width - 12, startY = (height - 2) / 2;
      int[] arrow = {startX, startY, startX + 3, startY + 3, startX + 6, startY};
      gc.setBackground(parent.getForeground());
      gc.fillPolygon(arrow);
      gc.drawPolygon(arrow);
    }
    gc.dispose();
    OS.XFreeGC(xDisplay, xGC);

    if (!enabled && disabledImage == null) {
      if (currentImage != null) currentImage.dispose();
    }
    return 0;
  }
Example #8
0
 public void createPartControl(org.eclipse.swt.widgets.Composite frame) {
   final org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHandler tooltip =
       new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHandler(frame.getShell());
   org.eclipse.swt.layout.GridLayout layout = new org.eclipse.swt.layout.GridLayout();
   layout.numColumns = 3;
   frame.setLayout(layout);
   java.lang.String platform = SWT.getPlatform();
   java.lang.String helpKey = "F1";
   if (platform.equals("gtk")) {
     helpKey = "Ctrl+F1";
   }
   if (platform.equals("carbon") || platform.equals("cocoa")) {
     helpKey = "Help";
   }
   org.eclipse.swt.widgets.ToolBar bar = new org.eclipse.swt.widgets.ToolBar(frame, SWT.BORDER);
   for (int i = 0; i < 5; i++) {
     org.eclipse.swt.widgets.ToolItem item = new org.eclipse.swt.widgets.ToolItem(bar, SWT.PUSH);
     item.setText(
         getResourceString("ToolItem.text", new java.lang.Object[] {new java.lang.Integer(i)}));
     item.setData(
         "TIP_TEXT",
         getResourceString("ToolItem.tooltip", new java.lang.Object[] {item.getText(), helpKey}));
     item.setData(
         "TIP_HELPTEXTHANDLER",
         new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler() {
           public java.lang.String getHelpText(org.eclipse.swt.widgets.Widget widget) {
             org.eclipse.swt.widgets.Item item = (org.eclipse.swt.widgets.Item) widget;
             return getResourceString("ToolItem.help", new java.lang.Object[] {item.getText()});
           }
         });
   }
   org.eclipse.swt.layout.GridData gridData = new org.eclipse.swt.layout.GridData();
   gridData.horizontalSpan = 3;
   bar.setLayoutData(gridData);
   tooltip.activateHoverHelp(bar);
   org.eclipse.swt.widgets.Table table = new org.eclipse.swt.widgets.Table(frame, SWT.BORDER);
   for (int i = 0; i < 4; i++) {
     org.eclipse.swt.widgets.TableItem item =
         new org.eclipse.swt.widgets.TableItem(table, SWT.PUSH);
     item.setText(getResourceString("Item", new java.lang.Object[] {new java.lang.Integer(i)}));
     item.setData("TIP_IMAGE", images[hhiInformation]);
     item.setText(
         getResourceString("TableItem.text", new java.lang.Object[] {new java.lang.Integer(i)}));
     item.setData(
         "TIP_TEXT",
         getResourceString("TableItem.tooltip", new java.lang.Object[] {item.getText(), helpKey}));
     item.setData(
         "TIP_HELPTEXTHANDLER",
         new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler() {
           public java.lang.String getHelpText(org.eclipse.swt.widgets.Widget widget) {
             org.eclipse.swt.widgets.Item item = (org.eclipse.swt.widgets.Item) widget;
             return getResourceString("TableItem.help", new java.lang.Object[] {item.getText()});
           }
         });
   }
   table.setLayoutData(new org.eclipse.swt.layout.GridData(GridData.VERTICAL_ALIGN_FILL));
   tooltip.activateHoverHelp(table);
   org.eclipse.swt.widgets.Tree tree = new org.eclipse.swt.widgets.Tree(frame, SWT.BORDER);
   for (int i = 0; i < 4; i++) {
     org.eclipse.swt.widgets.TreeItem item = new org.eclipse.swt.widgets.TreeItem(tree, SWT.PUSH);
     item.setText(getResourceString("Item", new java.lang.Object[] {new java.lang.Integer(i)}));
     item.setData("TIP_IMAGE", images[hhiWarning]);
     item.setText(
         getResourceString("TreeItem.text", new java.lang.Object[] {new java.lang.Integer(i)}));
     item.setData(
         "TIP_TEXT",
         getResourceString("TreeItem.tooltip", new java.lang.Object[] {item.getText(), helpKey}));
     item.setData(
         "TIP_HELPTEXTHANDLER",
         new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler() {
           public java.lang.String getHelpText(org.eclipse.swt.widgets.Widget widget) {
             org.eclipse.swt.widgets.Item item = (org.eclipse.swt.widgets.Item) widget;
             return getResourceString("TreeItem.help", new java.lang.Object[] {item.getText()});
           }
         });
   }
   tree.setLayoutData(new org.eclipse.swt.layout.GridData(GridData.VERTICAL_ALIGN_FILL));
   tooltip.activateHoverHelp(tree);
   org.eclipse.swt.widgets.Button button = new org.eclipse.swt.widgets.Button(frame, SWT.PUSH);
   button.setText(getResourceString("Hello.text"));
   button.setData("TIP_TEXT", getResourceString("Hello.tooltip"));
   tooltip.activateHoverHelp(button);
 }
Example #9
0
 static MenuItem checkNull(MenuItem item) {
   if (item == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   return item;
 }
Example #10
0
 static Menu checkNull(Menu menu) {
   if (menu == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   return menu;
 }
Example #11
0
 static Control checkNull(Control control) {
   if (control == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   return control;
 }
 void paint(Event event) {
   if (row == null) return;
   int columnIndex = column == null ? 0 : table.indexOf(column);
   GC gc = event.gc;
   gc.setBackground(getBackground());
   gc.setForeground(getForeground());
   gc.fillRectangle(event.x, event.y, event.width, event.height);
   int x = 0;
   Point size = getSize();
   Image image = row.getImage(columnIndex);
   if (image != null) {
     Rectangle imageSize = image.getBounds();
     int imageY = (size.y - imageSize.height) / 2;
     gc.drawImage(image, x, imageY);
     x += imageSize.width;
   }
   String text = row.getText(columnIndex);
   if (text.length() > 0) {
     Rectangle bounds = row.getBounds(columnIndex);
     Point extent = gc.stringExtent(text);
     // Temporary code - need a better way to determine table trim
     String platform = SWT.getPlatform();
     if ("win32".equals(platform)) { // $NON-NLS-1$
       if (table.getColumnCount() == 0 || columnIndex == 0) {
         x += 2;
       } else {
         int alignmnent = column.getAlignment();
         switch (alignmnent) {
           case SWT.LEFT:
             x += 6;
             break;
           case SWT.RIGHT:
             x = bounds.width - extent.x - 6;
             break;
           case SWT.CENTER:
             x += (bounds.width - x - extent.x) / 2;
             break;
         }
       }
     } else {
       if (table.getColumnCount() == 0) {
         x += 5;
       } else {
         int alignmnent = column.getAlignment();
         switch (alignmnent) {
           case SWT.LEFT:
             x += 5;
             break;
           case SWT.RIGHT:
             x = bounds.width - extent.x - 2;
             break;
           case SWT.CENTER:
             x += (bounds.width - x - extent.x) / 2 + 2;
             break;
         }
       }
     }
     int textY = (size.y - extent.y) / 2;
     gc.drawString(text, x, textY);
   }
   if (isFocusControl()) {
     Display display = getDisplay();
     gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
     gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
     gc.drawFocus(0, 0, size.x, size.y);
   }
 }
  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();
  }
Example #16
0
  /**
   * Specify a gradient of colours to be drawn in the background of the CLabel.
   *
   * <p>For example, to draw a gradient that varies from dark blue to white in the vertical,
   * direction use the following call to setBackground:
   *
   * <pre>
   * clabel.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE),
   * 	                           display.getSystemColor(SWT.COLOR_WHITE)},
   * 	                 new int[] {100}, true);
   * </pre>
   *
   * @param colors an array of Color that specifies the colors to appear in the gradient in order of
   *     appearance from left/top to right/bottom; The value <code>null</code> clears the background
   *     gradient; the value <code>null</code> can be used inside the array of Color to specify the
   *     background color.
   * @param percents an array of integers between 0 and 100 specifying the percent of the
   *     width/height of the widget at which the color should change; the size of the percents array
   *     must be one less than the size of the colors array.
   * @param vertical indicate the direction of the gradient. True is vertical and false is
   *     horizontal.
   * @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
   *       <li>ERROR_INVALID_ARGUMENT - if the values of colors and percents are not consistent
   *     </ul>
   *
   * @since 3.0
   */
  public void setBackground(Color[] colors, int[] percents, boolean vertical) {
    checkWidget();
    if (colors != null) {
      if (percents == null || percents.length != colors.length - 1) {
        SWT.error(SWT.ERROR_INVALID_ARGUMENT);
      }
      if (getDisplay().getDepth() < 15) {
        // Don't use gradients on low color displays
        colors = new Color[] {colors[colors.length - 1]};
        percents = new int[] {};
      }
      for (int i = 0; i < percents.length; i++) {
        if (percents[i] < 0 || percents[i] > 100) {
          SWT.error(SWT.ERROR_INVALID_ARGUMENT);
        }
        if (i > 0 && percents[i] < percents[i - 1]) {
          SWT.error(SWT.ERROR_INVALID_ARGUMENT);
        }
      }
    }

    // Are these settings the same as before?
    final Color background = getBackground();
    if (backgroundImage == null) {
      if ((gradientColors != null)
          && (colors != null)
          && (gradientColors.length == colors.length)) {
        boolean same = false;
        for (int i = 0; i < gradientColors.length; i++) {
          same =
              (gradientColors[i] == colors[i])
                  || ((gradientColors[i] == null) && (colors[i] == background))
                  || ((gradientColors[i] == background) && (colors[i] == null));
          if (!same) break;
        }
        if (same) {
          for (int i = 0; i < gradientPercents.length; i++) {
            same = gradientPercents[i] == percents[i];
            if (!same) break;
          }
        }
        if (same && this.gradientVertical == vertical) return;
      }
    } else {
      backgroundImage = null;
    }
    // Store the new settings
    if (colors == null) {
      gradientColors = null;
      gradientPercents = null;
      gradientVertical = false;
    } else {
      gradientColors = new Color[colors.length];
      for (int i = 0; i < colors.length; ++i)
        gradientColors[i] = (colors[i] != null) ? colors[i] : background;
      gradientPercents = new int[percents.length];
      for (int i = 0; i < percents.length; ++i) gradientPercents[i] = percents[i];
      gradientVertical = vertical;
    }
    // Refresh with the new settings
    redraw();
  }