blob: 1bab59de0aec6a928979dab7c5705e1ba02ac0f1 [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 an item in a table.
* <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 TableItem extends Item {
Table parent;
boolean grayed;
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Table</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 TableItem (Table parent, int style, int index) {
super (parent, style);
this.parent = parent;
parent.createItem (this, index);
}
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Table</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 TableItem (Table parent, int style) {
super (parent, style);
this.parent = parent;
parent.createItem (this, parent.getItemCount ());
}
/**
* 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, 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 a rectangle describing the receiver's size and location
* relative to its parent at a column in the table.
*
* @param index the index that specifies the column
* @return the receiver's bounding column 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 (int index) {
checkWidget();
int parentHandle = parent.handle;
int column = OS.gtk_tree_view_get_column (parentHandle, index);
if (column == 0) return new Rectangle (0, 0, 0, 0);
GdkRectangle rect = new GdkRectangle ();
int path = OS.gtk_tree_model_get_path (parent.modelHandle, handle);
OS.gtk_tree_view_get_cell_area (parentHandle, path, column, rect);
OS.gtk_tree_path_free (path);
int headerHeight = parent.getHeaderHeight ();
return new Rectangle (rect.x, rect.y + headerHeight, 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.
*
* @return the checked state of the checkbox
*
* @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, 0, ptr, -1);
return ptr[0] != 0;
}
public Display getDisplay () {
Table parent = this.parent;
if (parent == null) error (SWT.ERROR_WIDGET_DISPOSED);
return parent.getDisplay ();
}
/**
* 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, 1, ptr, -1);
if (ptr [0] == 0) return parent.getForeground ();
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</code> style, return false.
*
* @return the grayed state of the checkbox
*
* @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 ();
if ((parent.style & SWT.CHECK) == 0) return false;
//NOT IMPLEMENTED
return grayed;
}
public Image getImage () {
checkWidget ();
return getImage (0);
}
/**
* Returns the image stored at the given column index in the receiver,
* or null if the image has not been set or if the column does not exist.
*
* @param index the column index
* @return the image stored at the given column index in the receiver
*
* @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 Image getImage (int index) {
checkWidget ();
int parentHandle = parent.handle;
int column = OS.gtk_tree_view_get_column (parentHandle, index);
if (column == 0) return null;
int [] ptr = new int [1];
int modelIndex = parent.columnCount == 0 ? 3 : parent.columns [index].modelIndex;
OS.gtk_tree_model_get (parent.modelHandle, handle, modelIndex, ptr, -1);
if (ptr [0] == 0) return null;
ImageList imageList = parent.imageList;
int imageIndex = imageList.indexOf (ptr [0]);
return imageList.get (imageIndex);
}
/**
* Returns a rectangle describing the size and location
* relative to its parent of an image at a column in the
* table.
*
* @param index the index that specifies the column
* @return the receiver's bounding image 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 getImageBounds (int index) {
checkWidget ();
int parentHandle = parent.handle;
int column = OS.gtk_tree_view_get_column (parentHandle, index);
if (column == 0) return new Rectangle (0, 0, 0, 0);
int list = OS.gtk_tree_view_column_get_cell_renderers (column);
if (list == 0) return new Rectangle (0, 0, 0, 0);
int count = OS.g_list_length (list);
int pixbufRenderer = 0, i = 0;
while (i < count) {
int renderer = OS.g_list_nth_data (list, i);
if (OS.GTK_IS_CELL_RENDERER_PIXBUF (renderer)) {
pixbufRenderer = renderer;
break;
}
i++;
}
OS.g_list_free (list);
if (pixbufRenderer == 0) return new Rectangle (0, 0, 0, 0);
GdkRectangle rect = new GdkRectangle ();
int path = OS.gtk_tree_model_get_path (parent.modelHandle, handle);
OS.gtk_tree_view_get_cell_area (parentHandle, path, column, rect);
OS.gtk_tree_path_free (path);
int [] w = new int[1], h = new int[1];
OS.gtk_cell_renderer_get_size (pixbufRenderer, parentHandle, null, null, null, w, h);
int headerHeight = parent.getHeaderHeight ();
return new Rectangle (rect.x, rect.y + headerHeight, w [0], h [0]);
}
/**
* Gets the image indent.
*
* @return the indent
*
* @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 getImageIndent () {
checkWidget ();
/* Image indent is not supported on GTK */
return 0;
}
/**
* Returns the receiver's parent, which must be a <code>Table</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 Table getParent () {
checkWidget ();
return parent;
}
public String getText () {
checkWidget ();
return getText (0);
}
/**
* Returns the text stored at the given column index in the receiver,
* or empty string if the text has not been set.
*
* @param index the column index
* @return the text stored at the given column index in the receiver
*
* @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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_TEXT - if the column at index does not exist</li>
* </ul>
*/
public String getText (int index) {
checkWidget ();
int parentHandle = parent.handle;
int column = OS.gtk_tree_view_get_column (parentHandle, index);
if (column == 0) error(SWT.ERROR_CANNOT_GET_TEXT);
int [] ptr = new int [1];
int modelIndex = parent.columnCount == 0 ? 3 : parent.columns [index].modelIndex;
OS.gtk_tree_model_get (parent.modelHandle, handle, modelIndex + 1, ptr, -1);
if (ptr [0] == 0) return null;
int length = OS.strlen (ptr [0]);
byte[] buffer = new byte [length];
OS.memmove (buffer, ptr [0], length);
OS.g_free (ptr [0]);
return new String (Converter.mbcsToWcs (null, buffer));
}
void releaseChild () {
super.releaseChild ();
parent.destroyItem (this);
}
void releaseWidget () {
super.releaseWidget ();
if (handle != 0) OS.g_free (handle);
handle = 0;
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 gdkColor = color != null ? color.handle : null;
OS.gtk_list_store_set (parent.modelHandle, handle, 2, gdkColor, -1);
}
/**
* Sets the checked state of the checkbox for this item. This state change
* only applies if the Table was created with the SWT.CHECK style.
*
* @param checked the new checked state of the checkbox
*
* @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();
OS.gtk_list_store_set (parent.modelHandle, handle, 0, checked, -1);
}
/**
* 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)
*
* @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 gdkColor = color != null ? color.handle : null;
OS.gtk_list_store_set (parent.modelHandle, handle, 1, gdkColor, -1);
}
/**
* Sets the grayed state of the checkbox for this item. This state change
* only applies if the Table was created with the SWT.CHECK style.
*
* @param grayed the new grayed state of the checkbox
*
* @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();
//NOT IMPLEMENTED
this.grayed = grayed;
}
/**
* Sets the receiver's image at a column.
*
* @param index the column index
* @param image the new image
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_ARGUMENT - if the image 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>
*/
public void setImage (int index, Image image) {
checkWidget ();
if (image != null && image.isDisposed()) {
error(SWT.ERROR_INVALID_ARGUMENT);
}
int parentHandle = parent.handle;
int column = OS.gtk_tree_view_get_column (parentHandle, index);
if (column == 0) return;
int pixbuf = 0;
if (image != null) {
ImageList imageList = parent.imageList;
if (imageList == null) imageList = parent.imageList = new ImageList ();
int imageIndex = imageList.indexOf (image);
if (imageIndex == -1) imageIndex = imageList.add (image);
pixbuf = imageList.getPixbuf (imageIndex);
}
int modelIndex = parent.columnCount == 0 ? 3 : parent.columns [index].modelIndex;
OS.gtk_list_store_set (parent.modelHandle, handle, modelIndex, pixbuf, -1);
}
public void setImage (Image image) {
checkWidget ();
setImage (0, image);
}
/**
* Sets the image for multiple columns in the Table.
*
* @param images the array of new images
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the array of images is null</li>
* <li>ERROR_INVALID_ARGUMENT - if one of the images 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>
*/
public void setImage (Image [] images) {
checkWidget ();
if (images == null) error (SWT.ERROR_NULL_ARGUMENT);
for (int i=0; i<images.length; i++) {
setImage (i, images [i]);
}
}
/**
* Sets the image indent.
*
* @param indent the new indent
*
* </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 setImageIndent (int indent) {
checkWidget ();
/* Image indent is not supported on GTK */
}
/**
* Sets the receiver's text at a column
*
* @param index the column index
* @param string the new text
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the text is null</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 setText (int index, String string) {
checkWidget ();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
int parentHandle = parent.handle;
int column = OS.gtk_tree_view_get_column (parentHandle, index);
if (column == 0) return;
byte[] buffer = Converter.wcsToMbcs (null, string, true);
int modelIndex = parent.columnCount == 0 ? 3 : parent.columns [index].modelIndex;
OS.gtk_list_store_set (parent.modelHandle, handle, modelIndex + 1, buffer, -1);
}
public void setText (String string) {
checkWidget ();
setText (0, string);
}
/**
* Sets the text for multiple columns in the table.
*
* @param strings the array of new strings
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the text is null</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 setText (String [] strings) {
checkWidget ();
if (strings == null) error (SWT.ERROR_NULL_ARGUMENT);
for (int i=0; i<strings.length; i++) {
String string = strings [i];
if (string != null) setText (i, string);
}
}
}