/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.widgets; import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; /** * Instances of this class are selectable user interface * objects that represent the dynamically positionable * areas of a CoolBar. *
*
Styles:
*
DROP_DOWN
*
Events:
*
Selection
*
*

* IMPORTANT: This class is not intended to be subclassed. *

*/ public class CoolItem extends Item { Control control; CoolBar parent; boolean ideal; int preferredWidth, preferredHeight, minimumWidth, minimumHeight, requestedWidth; Rectangle itemBounds = new Rectangle(0, 0, 0, 0); static final int MARGIN_WIDTH = 4; static final int GRABBER_WIDTH = 2; static final int MINIMUM_WIDTH = (2 * MARGIN_WIDTH) + GRABBER_WIDTH; private int CHEVRON_HORIZONTAL_TRIM = -1; //platform dependent values private int CHEVRON_VERTICAL_TRIM = -1; private static final int CHEVRON_LEFT_MARGIN = 2; private static final int CHEVRON_IMAGE_WIDTH = 8; //Width to draw the double arrow ToolBar chevron; boolean wrap; Image arrowImage = null; /** * Constructs a new instance of this class given its parent * (which must be a CoolBar) and a style value * describing its behavior and appearance. The item is added * to the end of the items maintained by its parent. *

* The style value is either one of the style constants defined in * class SWT which is applicable to instances of this * class, or must be built by bitwise OR'ing together * (that is, using the int "|" operator) two or more * of those SWT style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. *

* * @param parent a composite control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * * @exception IllegalArgumentException * @exception SWTException * * @see SWT#DROP_DOWN * @see Widget#checkSubclass * @see Widget#getStyle */ public CoolItem (CoolBar parent, int style) { super (parent, style); this.parent = parent; parent.createItem (this, parent.getItemCount()); calculateChevronTrim (); } /** * Constructs a new instance of this class given its parent * (which must be a CoolBar), a style value * describing its behavior and appearance, and the index * at which to place it in the items maintained by its parent. *

* The style value is either one of the style constants defined in * class SWT which is applicable to instances of this * class, or must be built by bitwise OR'ing together * (that is, using the int "|" operator) two or more * of those SWT style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. *

* * @param parent a composite control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * @param index the index at which to store the receiver in its parent * * @exception IllegalArgumentException * @exception SWTException * * @see SWT#DROP_DOWN * @see Widget#checkSubclass * @see Widget#getStyle */ public CoolItem (CoolBar parent, int style, int index) { super(parent, style); this.parent = parent; parent.createItem (this, index); calculateChevronTrim (); } /** * Adds the listener to the collection of listeners that will * be notified when the control is selected, by sending it one * of the messages defined in the SelectionListener * interface. *

* If widgetSelected is called when the mouse is over * the drop-down arrow (or 'chevron') portion of the cool item, * the event object detail field contains the value SWT.ARROW, * and the x and y fields in the event object represent the point at * the bottom left of the chevron, where the menu should be popped up. * widgetDefaultSelected is not called. *

* * @param listener the listener which should be notified * * @exception IllegalArgumentException * @exception SWTException * * @see SelectionListener * @see #removeSelectionListener * @see SelectionEvent * * @since 2.0 */ public void addSelectionListener(SelectionListener listener) { checkWidget(); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.Selection,typedListener); addListener (SWT.DefaultSelection,typedListener); } protected void checkSubclass () { if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); } /* * Find the trim size of the Toolbar widget in the current platform. */ void calculateChevronTrim () { ToolBar tb = new ToolBar (parent, SWT.FLAT); ToolItem ti = new ToolItem (tb, SWT.PUSH); Image image = new Image (display, 1, 1); ti.setImage (image); Point size = tb.computeSize (SWT.DEFAULT, SWT.DEFAULT); CHEVRON_HORIZONTAL_TRIM = size.x - 1; CHEVRON_VERTICAL_TRIM = size.y - 1; tb.dispose (); ti.dispose (); image.dispose (); } /** * Returns the preferred size of the receiver. *

* The preferred size of a CoolItem is the size that * it would best be displayed at. The width hint and height hint arguments * allow the caller to ask the instance questions such as "Given a particular * width, how high does it need to be to show all of the contents?" * To indicate that the caller does not wish to constrain a particular * dimension, the constant SWT.DEFAULT is passed for the hint. *

* * @param wHint the width hint (can be SWT.DEFAULT) * @param hHint the height hint (can be SWT.DEFAULT) * @return the preferred size * * @exception SWTException * * @see Layout * @see #getBounds * @see #getSize * @see CoolBar#getBorderWidth * @see CoolBar#computeTrim * @see CoolBar#getClientArea */ public Point computeSize (int wHint, int hHint) { checkWidget(); int width = wHint, height = hHint; if (wHint == SWT.DEFAULT) width = 32; if (hHint == SWT.DEFAULT) height = 32; width += MINIMUM_WIDTH; return new Point (width, height); } public void dispose () { if (isDisposed()) return; /* * Must call parent.destroyItem() before super.dispose(), since it needs to * query the bounds to properly remove the item. */ parent.destroyItem(this); super.dispose (); parent = null; control = null; /* * Although the parent for the chevron is the CoolBar (CoolItem can not be the parent) * it has to be disposed with the item */ if (chevron != null && !chevron.isDisposed()) chevron.dispose(); chevron = null; if (arrowImage != null && !arrowImage.isDisposed()) arrowImage.dispose(); arrowImage = null; } Image createArrowImage (int width, int height) { Color foreground = parent.getForeground (); Color black = display.getSystemColor (SWT.COLOR_BLACK); Color background = parent.getBackground (); PaletteData palette = new PaletteData (new RGB[]{foreground.getRGB(), background.getRGB(), black.getRGB()}); ImageData imageData = new ImageData (width, height, 4, palette); imageData.transparentPixel = 1; Image image = new Image (display, imageData); GC gc = new GC (image); gc.setBackground (background); gc.fillRectangle (0, 0, width, height); gc.setForeground (black); int startX = 0 ; int startY = height / 6; int step = 2; gc.drawLine (startX, startY, startX + step, startY + step); gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); startX++; gc.drawLine (startX, startY, startX + step, startY + step); gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); startX += 3; gc.drawLine (startX, startY, startX + step, startY + step); gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); startX++; gc.drawLine (startX, startY, startX + step, startY + step); gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); gc.dispose (); return image; } /** * Returns a rectangle describing the receiver's size and location * relative to its parent. * * @return the receiver's bounding rectangle * * @exception SWTException */ public Rectangle getBounds () { checkWidget(); return new Rectangle(itemBounds.x, itemBounds.y, itemBounds.width, itemBounds.height); } /** * Returns the control that is associated with the receiver. * * @return the control that is contained by the receiver * * @exception SWTException */ public Control getControl () { checkWidget(); return control; } /** * Returns the minimum size that the cool item can * be resized to using the cool item's gripper. * * @return a point containing the minimum width and height of the cool item, in pixels * * @exception SWTException * * @since 2.0 */ public Point getMinimumSize () { checkWidget(); return new Point (minimumWidth, minimumHeight); } /** * Returns the receiver's parent, which must be a CoolBar. * * @return the receiver's parent * * @exception SWTException */ public CoolBar getParent () { checkWidget(); return parent; } /** * Returns a point describing the receiver's ideal size. * The x coordinate of the result is the ideal width of the receiver. * The y coordinate of the result is the ideal height of the receiver. * * @return the receiver's ideal size * * @exception SWTException */ public Point getPreferredSize () { checkWidget(); return new Point(preferredWidth, preferredHeight); } /** * Returns a point describing the receiver's size. The * x coordinate of the result is the width of the receiver. * The y coordinate of the result is the height of the * receiver. * * @return the receiver's size * * @exception SWTException */ public Point getSize () { checkWidget(); return new Point (itemBounds.width, itemBounds.height); } int internalGetMinimumWidth () { int width = minimumWidth + MINIMUM_WIDTH; if ((style & SWT.DROP_DOWN) != 0 && width < preferredWidth) { width += CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; } return width; } /* * Called when the chevron is selected. */ void onSelection (Event ev) { Rectangle bounds = chevron.getBounds(); Event event = new Event(); event.detail = SWT.ARROW; event.x = bounds.x; event.y = bounds.y + bounds.height; postEvent (SWT.Selection, event); } /** * Removes the listener from the collection of listeners that * will be notified when the control is selected. * * @param listener the listener which should be notified * * @exception IllegalArgumentException * @exception SWTException * * @see SelectionListener * @see #addSelectionListener * * @since 2.0 */ public void removeSelectionListener(SelectionListener listener) { checkWidget(); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Selection, listener); eventTable.unhook (SWT.DefaultSelection,listener); } void setBounds (int x, int y, int width, int height) { itemBounds.x = x; itemBounds.y = y; itemBounds.width = width; itemBounds.height = height; if (control != null) { int controlWidth = width - MINIMUM_WIDTH; if ((style & SWT.DROP_DOWN) != 0 && width < preferredWidth) { controlWidth -= CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; } control.setBounds ( x + MINIMUM_WIDTH, y, controlWidth, height); } updateChevron(); } /** * Sets the control that is associated with the receiver * to the argument. * * @param control the new control that will be contained by the receiver * * @exception IllegalArgumentException * @exception SWTException */ 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); } this.control = control; if (control != null) { int controlWidth = itemBounds.width - MINIMUM_WIDTH; if ((style & SWT.DROP_DOWN) != 0 && itemBounds.width < preferredWidth) { controlWidth -= CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; } control.setBounds (itemBounds.x + MINIMUM_WIDTH, itemBounds.y, controlWidth, itemBounds.height); } } /** * Sets the minimum size that the cool item can be resized to * using the cool item's gripper, to the point specified by the arguments. * * @param width the minimum width of the cool item, in pixels * @param height the minimum height of the cool item, in pixels * * @exception SWTException * * @since 2.0 */ public void setMinimumSize (int width, int height) { checkWidget (); minimumWidth = width; minimumHeight = height; } /** * Sets the minimum size that the cool item can be resized to * using the cool item's gripper, to the point specified by the argument. * * @param size a point representing the minimum width and height of the cool item, in pixels * * @exception IllegalArgumentException * @exception SWTException * * @since 2.0 */ public void setMinimumSize (Point size) { checkWidget (); if (size == null) error(SWT.ERROR_NULL_ARGUMENT); setMinimumSize(size.x, size.y); } /** * Sets the receiver's ideal size to the point specified by the arguments. * * @param width the new ideal width for the receiver * @param height the new ideal height for the receiver * * @exception SWTException */ public void setPreferredSize (int width, int height) { checkWidget(); ideal = true; preferredWidth = Math.max (width, minimumWidth + MINIMUM_WIDTH); preferredHeight = height; } /** * Sets the receiver's ideal size to the point specified by the argument. * * @param size the new ideal size for the receiver * * @exception IllegalArgumentException * @exception SWTException */ public void setPreferredSize (Point size) { checkWidget(); if (size == null) error(SWT.ERROR_NULL_ARGUMENT); setPreferredSize(size.x, size.y); } /** * Sets the receiver's size to the point specified by the arguments. *

* Note: Attempting to set the width or height of the * receiver to a negative number will cause that * value to be set to zero instead. *

* * @param width the new width for the receiver * @param height the new height for the receiver * * @exception SWTException */ public void setSize (int width, int height) { checkWidget(); width = Math.max(width, minimumWidth + MINIMUM_WIDTH); if (!ideal) { preferredWidth = width; preferredHeight = height; } itemBounds.width = requestedWidth = width; itemBounds.height = height; if (control != null) { int controlWidth = width - MINIMUM_WIDTH; if ((style & SWT.DROP_DOWN) != 0 && width < preferredWidth) { controlWidth -= CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; } control.setSize(controlWidth, height); } parent.relayout(); updateChevron(); } /** * Sets the receiver's size to the point specified by the argument. *

* Note: Attempting to set the width or height of the * receiver to a negative number will cause them to be * set to zero instead. *

* * @param size the new size for the receiver * * @exception IllegalArgumentException * @exception SWTException */ public void setSize (Point size) { checkWidget(); if (size == null) error (SWT.ERROR_NULL_ARGUMENT); setSize (size.x, size.y); } void updateChevron() { if (control != null) { int width = itemBounds.width; if ((style & SWT.DROP_DOWN) != 0 && width < preferredWidth) { int height = Math.min (control.getSize ().y, itemBounds.height); if (chevron == null) { chevron = new ToolBar (parent, SWT.FLAT | SWT.NO_FOCUS); ToolItem toolItem = new ToolItem (chevron, SWT.PUSH); toolItem.addListener (SWT.Selection, new Listener() { public void handleEvent (Event event) { CoolItem.this.onSelection (event); } }); } int imageHeight = Math.max(1, height - CHEVRON_VERTICAL_TRIM); if (arrowImage == null || (arrowImage != null && arrowImage.getBounds().height != imageHeight)) { Image image = createArrowImage (CHEVRON_IMAGE_WIDTH, imageHeight); chevron.getItem (0).setImage (image); if (arrowImage != null) arrowImage.dispose (); arrowImage = image; } chevron.setBackground (parent.getBackground()); chevron.setBounds ( itemBounds.x + width - CHEVRON_LEFT_MARGIN - CHEVRON_IMAGE_WIDTH - CHEVRON_HORIZONTAL_TRIM, itemBounds.y, CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM, height); chevron.setVisible(true); } else { if (chevron != null) { chevron.setVisible(false); } } } } }