blob: bd118978915e630177e1df23603e2feec73ff8bb [file] [log] [blame]
package org.eclipse.swt.widgets;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved
*/
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.*;
/**
* Instances of this class are selectable user interface
* objects that represent the dynamically positionable
* areas of a <code>CoolBar</code>.
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>(none)</dd>
* <dt><b>Events:</b></dt>
* <dd>(none)</dd>
* </dl>
* <p>
* IMPORTANT: This class is <em>not</em> intended to be subclassed.
* </p>
*/
public class CoolItem extends Item {
Control control;
CoolBar parent;
int preferredWidth, requestedWidth;
Rectangle itemBounds = new Rectangle(0, 0, 0, 0);
static final int MARGIN_WIDTH = 4;
static final int MARGIN_HEIGHT = 2;
static final int GRABBER_WIDTH = 2;
static final int MINIMUM_WIDTH = (2 * MARGIN_WIDTH) + GRABBER_WIDTH;
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>CoolBar</code>) and a style value
* describing its behavior and appearance. The item is added
* to the end of the items maintained by its parent.
* <p>
* The style value is either one of the style constants defined in
* class <code>SWT</code> which is applicable to instances of this
* class, or must be built by <em>bitwise OR</em>'ing together
* (that is, using the <code>int</code> "|" operator) two or more
* of those <code>SWT</code> style constants. The class description
* for all SWT widget classes should include a comment which
* describes the style constants which are applicable to the class.
* </p>
*
* @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 <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @see SWT
* @see Widget#checkSubclass
* @see Widget#getStyle
*/
public CoolItem (CoolBar parent, int style) {
super(parent, style);
this.parent = parent;
parent.createItem (this, parent.getItemCount());
}
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>CoolBar</code>), a style value
* describing its behavior and appearance, and the index
* at which to place it in the items maintained by its parent.
* <p>
* The style value is either one of the style constants defined in
* class <code>SWT</code> which is applicable to instances of this
* class, or must be built by <em>bitwise OR</em>'ing together
* (that is, using the <code>int</code> "|" operator) two or more
* of those <code>SWT</code> style constants. The class description
* for all SWT widget classes should include a comment which
* describes the style constants which are applicable to the class.
* </p>
*
* @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 to store the receiver in its parent
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @see SWT
* @see Widget#checkSubclass
* @see Widget#getStyle
*/
public CoolItem (CoolBar parent, int style, int index) {
super(parent, 0);
this.parent = parent;
parent.createItem (this, index);
}
protected void checkSubclass () {
if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
}
/**
* Returns the preferred size of the receiver.
* <p>
* The <em>prefered size</em> of a <code>CoolItem</code> 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 <code>SWT.DEFAULT</code> is passed for the hint.
* </p>
*
* @param wHint the width hint (can be <code>SWT.DEFAULT</code>)
* @param hHint the height hint (can be <code>SWT.DEFAULT</code>)
* @return the preferred size
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see Layout
*/
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 + MARGIN_WIDTH;
height += 2 * MARGIN_HEIGHT;
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;
}
/**
* Returns a rectangle describing the receiver's size and location
* relative to its parent.
*
* @return the receiver's bounding rectangle
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public Rectangle getBounds () {
checkWidget();
return new Rectangle(itemBounds.x, itemBounds.y, itemBounds.width, itemBounds.height);
}
/**
* Gets the control which is associated with the receiver.
*
* @return the control
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public Control getControl () {
checkWidget();
return control;
}
public Display getDisplay () {
Composite parent = this.parent;
if (parent == null) error (SWT.ERROR_WIDGET_DISPOSED);
return parent.getDisplay ();
}
/**
* Returns the receiver's parent, which must be a <code>CoolBar</code>.
*
* @return the receiver's parent
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public CoolBar getParent () {
checkWidget();
return parent;
}
public Point getSize () {
checkWidget();
return new Point (itemBounds.width, itemBounds.height);
}
/**
* Sets the control which is associated with the receiver
* to the argument.
*
* @param control the new control
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
* <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </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);
}
Control oldControl = this.control;
if (oldControl != null) oldControl.setVisible(false);
this.control = control;
if (control != null && !control.isDisposed ()) {
Rectangle bounds = getBounds();
control.setBounds (
bounds.x + MINIMUM_WIDTH,
bounds.y + MARGIN_HEIGHT,
bounds.width - MINIMUM_WIDTH - MARGIN_WIDTH,
bounds.height - (2 * MARGIN_HEIGHT));
control.setVisible(true);
}
}
public void setSize (int width, int height) {
checkWidget();
int newWidth = Math.max (width, MINIMUM_WIDTH);
itemBounds.width = preferredWidth = requestedWidth = newWidth;
itemBounds.height = height;
if (control != null) {
int controlWidth = newWidth - MINIMUM_WIDTH - MARGIN_WIDTH;
int controlHeight = height - (2 * MARGIN_HEIGHT);
control.setSize(controlWidth, controlHeight);
}
parent.relayout();
}
public void setSize (Point size) {
checkWidget();
if (size == null) error (SWT.ERROR_NULL_ARGUMENT);
setSize (size.x, size.y);
}
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 controlHeight = Math.min(height, control.getSize().y);
control.setBounds(
x + MINIMUM_WIDTH,
y + MARGIN_HEIGHT,
width - MINIMUM_WIDTH - MARGIN_WIDTH,
controlHeight);
}
}
public Point getPreferredSize () {
checkWidget();
int height = getSize().y;
return new Point(preferredWidth, height + (2 * MARGIN_HEIGHT));
}
public void setPreferredSize (int width, int height) {
checkWidget();
preferredWidth = Math.max (width, MINIMUM_WIDTH);
Rectangle bounds = getBounds();
setBounds(bounds.x, bounds.y, bounds.width, height);
if (height != bounds.height) parent.relayout();
}
public void setPreferredSize (Point size) {
checkWidget();
if (size == null) error(SWT.ERROR_NULL_ARGUMENT);
setPreferredSize(size.x, size.y);
}
}