blob: ffffdc1c851daa1de11e6f4c25e6c05e55054d24 [file] [log] [blame]
package org.eclipse.swt.widgets;
/*
* Copyright (c) 2000, 2002 IBM Corp. All rights reserved.
* This file is 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
*/
import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gtk.*;
import org.eclipse.swt.graphics.*;
/**
* Instances of this class represent a selectable user interface object
* that represents a hierarchy of tree items in a tree widget.
*
* <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 TreeItem extends Item {
Tree parent;
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Tree</code> or a <code>TreeItem</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
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
* </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 TreeItem (Tree parent, int style) {
super (parent, style);
this.parent = parent;
parent.createItem (this, 0, -1);
}
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Tree</code> or a <code>TreeItem</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
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
* </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 TreeItem (Tree parent, int style, int index) {
super (parent, style);
if (index < 0) error (SWT.ERROR_INVALID_RANGE);
this.parent = parent;
parent.createItem (this, 0, index);
}
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Tree</code> or a <code>TreeItem</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
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
* </p>
*
* @param parentItem 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 TreeItem (TreeItem parentItem, int style) {
super (checkNull (parentItem).getParent(), style);
this.parent = parentItem.getParent ();
parent.createItem (this, parentItem.handle, -1);
}
/**
* Returns the receiver's background color.
*
* @return the background color
*
* @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>
*
* @since 2.0
*
*/
public Color getBackground () {
checkWidget ();
int[] ptr = new int[1];
OS.gtk_tree_model_get(parent.modelHandle, handle, 3, ptr, -1);
if (ptr[0]==0) return parent.getBackground();
GdkColor gdkColor = new GdkColor();
OS.memmove(gdkColor, ptr[0], GdkColor.sizeof);
return Color.gtk_new(getDisplay(), gdkColor);
}
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Tree</code> or a <code>TreeItem</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
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
* </p>
*
* @param parentItem 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 TreeItem (TreeItem parentItem, int style, int index) {
super (checkNull (parentItem).getParent (), style);
if (index < 0) error (SWT.ERROR_ITEM_NOT_ADDED);
this.parent = parentItem.getParent ();
parent.createItem (this, parentItem.handle, index);
}
static TreeItem checkNull (TreeItem item) {
if (item == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
return item;
}
/**
* 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 () {
GdkRectangle rect = new GdkRectangle();
OS.gtk_tree_view_get_cell_area(parent.handle, handle, parent.columnHandle, rect);
return new Rectangle (rect.x, rect.y, rect.width, rect.height);
}
/**
* Returns <code>true</code> if the receiver is checked,
* and false otherwise. When the parent does not have
* the <code>CHECK style, return false.
* <p>
*
* @return the checked state
*
* @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 boolean getChecked () {
checkWidget();
if ((parent.style & SWT.CHECK) == 0) return false;
int[] ptr = new int[1];
OS.gtk_tree_model_get(parent.modelHandle, handle, 5, ptr, -1);
return ptr[0] != 0;
}
public Display getDisplay () {
if (parent == null) error (SWT.ERROR_WIDGET_DISPOSED);
return parent.getDisplay ();
}
/**
* Returns <code>true</code> if the receiver is expanded,
* and false otherwise.
* <p>
*
* @return the expanded state
*
* @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 boolean getExpanded () {
checkWidget();
int path = OS.gtk_tree_model_get_path(parent.modelHandle, handle);
boolean answer = OS.gtk_tree_view_row_expanded(parent.handle, path);
OS.gtk_tree_path_free(path);
return answer;
}
/**
* Returns the foreground color that the receiver will use to draw.
*
* @return the receiver's foreground color
*
* @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>
*
* @since 2.0
*
*/
public Color getForeground () {
checkWidget ();
int[] ptr = new int[1];
OS.gtk_tree_model_get(parent.modelHandle, handle, 2, ptr, -1);
if (ptr[0]==0) return parent.getBackground();
GdkColor gdkColor = new GdkColor();
OS.memmove(gdkColor, ptr[0], GdkColor.sizeof);
return Color.gtk_new(getDisplay(), gdkColor);
}
/**
* Returns <code>true</code> if the receiver is grayed,
* and false otherwise. When the parent does not have
* the <code>CHECK style, return false.
* <p>
*
* @return the grayed state
*
* @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 boolean getGrayed() {
checkWidget();
return false;
}
/**
* Returns the number of items contained in the receiver
* that are direct item children of the receiver.
*
* @return the number of items
*
* @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 int getItemCount () {
checkWidget();
return parent.getItemCount (handle);
}
/**
* Returns an array of <code>TreeItem</code>s which are the
* direct item children of the receiver.
* <p>
* Note: This is not the actual structure used by the receiver
* to maintain its list of items, so modifying the array will
* not affect the receiver.
* </p>
*
* @return the receiver's items
*
* @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 TreeItem [] getItems () {
checkWidget();
return parent.getItems (handle);
}
/**
* Returns the receiver's parent, which must be a <code>Tree</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 Tree getParent () {
checkWidget();
return parent;
}
/**
* Returns the receiver's parent item, which must be a
* <code>TreeItem</code> or null when the receiver is a
* root.
*
* @return the receiver's parent item
*
* @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 TreeItem getParentItem () {
checkWidget();
int path = OS.gtk_tree_model_get_path(parent.modelHandle, handle);
if (OS.gtk_tree_path_get_depth(path)<2) {
OS.gtk_tree_path_free(path);
return null;
}
OS.gtk_tree_path_up(path);
int iter = OS.g_malloc(OS.GtkTreeIter_sizeof());
OS.gtk_tree_model_get_iter(parent.modelHandle, iter, path);
int[] index = new int[1];
OS.gtk_tree_model_get(parent.modelHandle, iter, 4, index, -1);
OS.g_free(iter);
OS.gtk_tree_path_free(path);
return parent.items[index[0]];
}
void releaseChild () {
super.releaseChild ();
parent.destroyItem (this);
}
void releaseWidget () {
super.releaseWidget ();
parent = null;
}
/**
* Sets the receiver's background color to the color specified
* by the argument, or to the default system color for the item
* if the argument is null.
*
* @param color the new color (or null)
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</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>
*
* @since 2.0
*
*/
public void setBackground (Color color) {
checkWidget ();
if (color != null && color.isDisposed ()) {
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
}
GdkColor ptr = (color != null)? color.handle : null;
OS.gtk_tree_store_set(parent.modelHandle, handle, 3, ptr, -1);
}
/**
* Sets the checked state of the receiver.
* <p>
*
* @param checked the new checked state
*
* @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 setChecked (boolean checked) {
checkWidget();
if ((parent.style & SWT.CHECK) == 0) return;
OS.gtk_tree_store_set(parent.modelHandle, handle, 5, checked, -1);
}
/**
* Sets the grayed state of the receiver.
* <p>
*
* @param checked the new grayed state
*
* @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 setGrayed (boolean grayed) {
checkWidget();
}
/**
* Sets the expanded state of the receiver.
* <p>
*
* @param expanded the new expanded state
*
* @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 setExpanded (boolean expanded) {
checkWidget();
int path = OS.gtk_tree_model_get_path(parent.modelHandle, handle);
if (expanded) {
blockSignal(parent.handle, SWT.Expand);
OS.gtk_tree_view_expand_row(parent.handle, path, false);
unblockSignal(parent.handle, SWT.Expand);
} else {
blockSignal(parent.handle, SWT.Collapse);
OS.gtk_tree_view_collapse_row(parent.handle, path);
unblockSignal(parent.handle, SWT.Collapse);
}
OS.gtk_tree_path_free(path);
}
/**
* Sets the receiver's foreground color to the color specified
* by the argument, or to the default system color for the item
* if the argument is null.
*
* @param color the new color (or null)
*
* @since 2.0
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</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>
*
* @since 2.0
*
*/
public void setForeground (Color color){
checkWidget ();
if (color != null && color.isDisposed ()) {
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
}
GdkColor ptr = (color != null)? color.handle : null;
OS.gtk_tree_store_set(parent.modelHandle, handle, 2, ptr, -1);
}
public void setImage (Image image) {
checkWidget();
if (image != null && image.isDisposed()) {
error(SWT.ERROR_INVALID_ARGUMENT);
}
this.image = image;
int pixbuf = 0;
if (image != null) {
int[] w = new int[1], h = new int[1];
boolean hasMask = (image.mask != 0);
OS.gdk_drawable_get_size(image.pixmap, w, h);
int width = w[0], height = h[0];
pixbuf = OS.gdk_pixbuf_new(OS.GDK_COLORSPACE_RGB, hasMask, 8, width, height);
if (pixbuf == 0) SWT.error(SWT.ERROR_NO_HANDLES);
int colormap = OS.gdk_colormap_get_system();
OS.gdk_pixbuf_get_from_drawable(pixbuf, image.pixmap, colormap, 0, 0, 0, 0, width, height);
if (hasMask) {
int gdkMaskImagePtr = OS.gdk_drawable_get_image(image.mask, 0, 0, width, height);
if (gdkMaskImagePtr == 0) SWT.error(SWT.ERROR_NO_HANDLES);
int stride = OS.gdk_pixbuf_get_rowstride(pixbuf);
int pixels = OS.gdk_pixbuf_get_pixels(pixbuf);
byte[] line = new byte[stride];
for (int y=0; y<height; y++) {
OS.memmove(line, pixels + (y * stride), stride);
for (int x=0; x<width; x++) {
if (OS.gdk_image_get_pixel(gdkMaskImagePtr, x, y) != 0) {
line[x*4+3] = (byte)0xFF;
} else {
line[x*4+3] = 0;
}
}
OS.memmove(pixels + (y * stride), line, stride);
}
OS.g_object_unref(gdkMaskImagePtr);
}
}
OS.gtk_tree_store_set(parent.modelHandle, handle, 1, pixbuf, -1);
}
/**
* This label will be displayed to the right of the bitmap,
* or, if the receiver doesn't have a bitmap to the right of
* the horizontal hierarchy connector line.
*/
public void setText (String string) {
checkWidget();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
super.setText (string);
byte[] bytes = Converter.wcsToMbcs(null, string, true);
OS.gtk_tree_store_set(parent.modelHandle, handle, 0, bytes, -1);
}
}