blob: 053ba6cc2881d728359448d31f9d7540915f968b [file] [log] [blame]
package org.eclipse.swt.widgets;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved
*/
import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gtk.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;
/**
* Instances of this class represent a selectable user interface
* object that displays a list of strings and issues notificiation
* when a string selected. A list may be single or multi select.
* <p>
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>SINGLE, MULTI</dd>
* <dt><b>Events:</b></dt>
* <dd>Selection, DefaultSelection</dd>
* </dl>
* <p>
* IMPORTANT: This class is <em>not</em> intended to be subclassed.
* </p>
*/
public class List extends Scrollable {
boolean selected;
/**
* Constructs a new instance of this class given its parent
* and a style value describing its behavior and appearance.
* <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 List (Composite parent, int style) {
super (parent, checkStyle (style));
}
/**
* Adds the argument to the end of the receiver's list.
*
* @param string the new item
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the string 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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li>
* </ul>
*
* @see #add(String,int)
*/
public void add (String string) {
checkWidget();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
byte [] buffer = Converter.wcsToMbcs (null, string, true);
int ptr = OS.g_malloc (buffer.length);
OS.memmove (ptr, buffer, buffer.length);
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
int result = OS.gtk_clist_append (handle, new int [] {ptr});
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
OS.g_free (ptr);
}
/**
* Adds the argument to the receiver's list at the given
* zero-relative index.
* <p>
* Note: To add an item at the end of the list, use the
* result of calling <code>getItemCount()</code> as the
* index or use <code>add(String)</code>.
* </p>
*
* @param string the new item
* @param index the index for the item
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the string is null</li>
* <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)</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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li>
* </ul>
*
* @see #add(String)
*/
public void add (String string, int index) {
checkWidget();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
if (index == -1) error (SWT.ERROR_ITEM_NOT_ADDED);
byte [] buffer = Converter.wcsToMbcs (null, string, true);
int ptr = OS.g_malloc (buffer.length);
OS.memmove (ptr, buffer, buffer.length);
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
int result = OS.gtk_clist_insert (handle, index, new int [] {ptr});
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
OS.g_free (ptr);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when the receiver's selection changes, by sending
* it one of the messages defined in the <code>SelectionListener</code>
* interface.
* <p>
* <code>widgetSelected</code> is called when the selection changes.
* <code>widgetDefaultSelected</code> is typically called when an item is double-clicked.
* </p>
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener 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>
*
* @see SelectionListener
* @see #removeSelectionListener
* @see SelectionEvent
*/
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);
}
static int checkStyle (int style) {
return checkBits (style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0);
}
void createHandle (int index) {
state |= HANDLE;
scrolledHandle = OS.gtk_scrolled_window_new (0, 0);
if (scrolledHandle == 0) error (SWT.ERROR_NO_HANDLES);
handle = OS.gtk_clist_new (1);
if (handle == 0) error (SWT.ERROR_NO_HANDLES);
}
void setHandleStyle() {
int selectionMode = ((style & SWT.MULTI) != 0)?
OS.GTK_SELECTION_EXTENDED :
OS.GTK_SELECTION_BROWSE;
OS.gtk_clist_set_selection_mode (handle, selectionMode);
int border = OS.GTK_SHADOW_NONE;
if ((style&SWT.BORDER)!=0) {
border = OS.GTK_SHADOW_ETCHED_IN;
if ((style&SWT.SHADOW_IN)!=0) border = OS.GTK_SHADOW_IN;
if ((style&SWT.SHADOW_OUT)!=0) border = OS.GTK_SHADOW_OUT;
if ((style&SWT.SHADOW_ETCHED_IN)!=0) border = OS.GTK_SHADOW_ETCHED_IN;
if ((style&SWT.SHADOW_ETCHED_OUT)!=0) border = OS.GTK_SHADOW_ETCHED_OUT;
}
OS.gtk_clist_set_shadow_type(handle, border);
setScrollingPolicy();
}
void configure() {
_connectParent();
OS.gtk_container_add (scrolledHandle, handle);
}
void hookEvents () {
//TO DO - get rid of enter/exit for mouse crossing border
super.hookEvents();
signal_connect (handle, "select_row", SWT.Selection, 5);
signal_connect (handle, "unselect_row", SWT.Selection, 5);
}
void showHandle() {
OS.gtk_widget_show (scrolledHandle);
OS.gtk_widget_show (handle);
OS.gtk_widget_realize (handle);
}
public Point computeSize (int wHint, int hHint, boolean changed) {
checkWidget ();
if (wHint == SWT.DEFAULT) wHint = 200;
return super.computeSize (wHint, hHint, changed);
}
/**
* Deselects the item at the given zero-relative index in the receiver.
* If the item at the index was already deselected, it remains
* deselected. Indices that are out of range are ignored.
*
* @param index the index of the item to deselect
*
* @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 deselect (int index) {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_clist_unselect_row (handle, index, 0);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Deselects the items at the given zero-relative indices in the receiver.
* If the item at the given zero-relative index in the receiver
* is selected, it is deselected. If the item at the index
* was not selected, it remains deselected. The range of the
* indices is inclusive. Indices that are out of range are ignored.
*
* @param start the start index of the items to deselect
* @param end the end index of the items to deselect
*
* @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 deselect (int start, int end) {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
for (int i=start; i<=end; i++) {
OS.gtk_clist_unselect_row (handle, i, 0);
}
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Deselects the items at the given zero-relative indices in the receiver.
* If the item at the given zero-relative index in the receiver
* is selected, it is deselected. If the item at the index
* was not selected, it remains deselected. Indices that are out
* of range and duplicate indices are ignored.
*
* @param indices the array of indices for the items to deselect
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener 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 deselect (int [] indices) {
checkWidget();
if (indices == null) error (SWT.ERROR_NULL_ARGUMENT);
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
for (int i=0; i<indices.length; i++) {
OS.gtk_clist_unselect_row (handle, indices [i], 0);
}
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Deselects all selected items 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 void deselectAll () {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_clist_unselect_all (handle);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Returns the zero-relative index of the item which is currently
* has the focus in the receiver, or -1 if no item is has focus.
*
* @return the index of the selected 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 int getFocusIndex () {
checkWidget();
GtkCList clist = new GtkCList();
OS.memmove(clist, handle, GtkCList.sizeof);
return clist.focus_row;
}
/**
* Returns the item at the given, zero-relative index in the
* receiver. Throws an exception if the index is out of range.
*
* @param index the index of the item to return
* @return the item at the given index
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_ITEM - if the operation fails because of an operating system failure</li>
* </ul>
*/
public String getItem (int index) {
checkWidget();
int [] buffer = new int [1];
int result = OS.gtk_clist_get_text (handle, index, 0, buffer);
int length = OS.strlen (buffer [0]);
byte [] buffer1 = new byte [length];
OS.memmove (buffer1, buffer [0], length);
char [] buffer2 = Converter.mbcsToWcs (null, buffer1);
return new String (buffer2, 0, buffer2.length);
}
/**
* Returns the number of items contained in 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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_COUNT - if the operation fails because of an operating system failure</li>
* </ul>
*/
public int getItemCount () {
checkWidget();
GtkCList widget = new GtkCList ();
OS.memmove (widget, handle, GtkCList.sizeof);
return widget.rows;
}
/**
* Returns the height of the area which would be used to
* display <em>one</em> of the items in the tree.
*
* @return the height of one 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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_ITEM_HEIGHT - if the operation fails because of an operating system failure</li>
* </ul>
*/
public int getItemHeight () {
checkWidget();
GtkCList clist = new GtkCList ();
OS.memmove (clist, handle, GtkCList.sizeof);
return clist.row_height;
}
/**
* Returns an array of <code>String</code>s which are the items
* in 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 items in the receiver's list
*
* @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_ITEM - if the operation fails because of an operating system failure while getting an item</li>
* <li>ERROR_CANNOT_GET_COUNT - if the operation fails because of an operating system failure while getting the item count</li>
* </ul>
*/
public String [] getItems () {
checkWidget();
int count = getItemCount ();
String [] result = new String [count];
for (int i=0; i<count; i++) result [i] = getItem (i);
return result;
}
/**
* Returns an array of <code>String</code>s that are currently
* selected in the receiver. An empty array indicates that no
* items are selected.
* <p>
* Note: This is not the actual structure used by the receiver
* to maintain its selection, so modifying the array will
* not affect the receiver.
* </p>
* @return an array representing the selection
*
* @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_SELECTION - if the operation fails because of an operating system failure while getting the selection</li>
* <li>ERROR_CANNOT_GET_ITEM - if the operation fails because of an operating system failure while getting an item</li>
* </ul>
*/
public String [] getSelection () {
checkWidget();
GtkCList widget = new GtkCList ();
OS.memmove (widget, handle, GtkCList.sizeof);
int list = widget.selection;
if (list==0) return new String[0];
int length = OS.g_list_length (list);
String [] items = new String [length];
int [] buffer = new int [1];
for (int i=0; i<length; i++) {
int index = OS.g_list_nth_data (list, i);
int result = OS.gtk_clist_get_text (handle, index, 0, buffer);
int strlen = OS.strlen (buffer [0]);
byte [] buffer1 = new byte [strlen];
OS.memmove (buffer1, buffer [0], strlen);
char [] buffer2 = Converter.mbcsToWcs (null, buffer1);
items [i] = new String (buffer2, 0, buffer2.length);
}
return items;
}
/**
* Returns the number of selected items contained in the receiver.
*
* @return the number of selected 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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_COUNT - if the operation fails because of an operating system failure</li>
* </ul>
*/
public int getSelectionCount () {
checkWidget();
GtkCList widget = new GtkCList ();
OS.memmove (widget, handle, GtkCList.sizeof);
return OS.g_list_length (widget.selection);
}
/**
* Returns the zero-relative index of the item which is currently
* selected in the receiver, or -1 if no item is selected.
*
* @return the index of the selected 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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_SELECTION - if the operation fails because of an operating system failure</li>
* </ul>
*/
public int getSelectionIndex () {
checkWidget();
GtkCList clist = new GtkCList ();
OS.memmove(clist, handle, GtkCList.sizeof);
int list = clist.selection;
if (OS.g_list_length (list) == 0) return -1;
return OS.g_list_nth_data (list, 0);
}
/**
* Returns the zero-relative indices of the items which are currently
* selected in the receiver. The array is empty if no items are selected.
* <p>
* Note: This is not the actual structure used by the receiver
* to maintain its selection, so modifying the array will
* not affect the receiver.
* </p>
* @return the array of indices of the selected 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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_SELECTION - if the operation fails because of an operating system failure</li>
* </ul>
*/
public int [] getSelectionIndices () {
checkWidget();
GtkCList widget = new GtkCList ();
OS.memmove (widget, handle, GtkCList.sizeof);
int list = widget.selection;
int length = OS.g_list_length (list);
int [] indices = new int [length];
for (int i=0; i<length; i++) {
indices [i] = OS.g_list_nth_data (list, i);
}
return indices;
}
/**
* Returns the zero-relative index of the item which is currently
* at the top of the receiver. This index can change when items are
* scrolled or new items are added or removed.
*
* @return the index of the top 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 int getTopIndex () {
checkWidget();
GtkCList clist = new GtkCList ();
OS.memmove(clist, handle, GtkCList.sizeof);
return -clist.voffset / (clist.row_height + 1);
}
/**
* Gets the index of an item.
* <p>
* The list is searched starting at 0 until an
* item is found that is equal to the search item.
* If no item is found, -1 is returned. Indexing
* is zero based.
*
* @param string the search item
* @return the index of the item
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the string 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 int indexOf (String string) {
checkWidget();
return indexOf (string, 0);
}
/**
* Searches the receiver's list starting at the given,
* zero-relative index until an item is found that is equal
* to the argument, and returns the index of that item. If
* no item is found or the starting index is out of range,
* returns -1.
*
* @param string the search item
* @return the index of the item
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the string 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>
* @exception SWTError <ul>
* <li>ERROR_CANNOT_GET_COUNT - if the operation fails because of an operating system failure while getting the item count</li>
* <li>ERROR_CANNOT_GET_ITEM - if the operation fails because of an operating system failure while getting an item</li>
* </ul>
*/
public int indexOf (String string, int start) {
checkWidget();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
String [] items = getItems ();
for (int i=start; i<items.length; i++) {
if (items [i].equals (string)) return i;
}
return -1;
}
/**
* Returns <code>true</code> if the item is selected,
* and <code>false</code> otherwise. Indices out of
* range are ignored.
*
* @param index the index of the item
* @return the visibility state of the item at the index
*
* @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 isSelected (int index) {
checkWidget();
GtkCList clist = new GtkCList ();
OS.memmove (clist, handle, GtkCList.sizeof);
int list = clist.selection;
if (list == 0) return false;
int length = OS.g_list_length (list);
for (int i=0; i<length; i++) {
if (index == OS.g_list_nth_data (list, i)) return true;
}
return false;
}
int processMouseDown (int callData, int arg1, int int2) {
if ((style & SWT.MULTI) != 0) selected = true;
return super.processMouseDown (callData, arg1, int2);
}
int processMouseUp (int callData, int arg1, int int2) {
int result = super.processMouseUp (callData, arg1, int2);
if ((style & SWT.MULTI) != 0) {
/*
* Feature in GTK. When an item is reselected, GTK
* does not issue notification. The fix is to detect
* that the mouse was released over a selected item when
* no selection signal was set and issue a fake selection
* event.
*/
GdkEventButton gdkEvent = new GdkEventButton ();
OS.memmove (gdkEvent, callData, GdkEventButton.sizeof);
int x = (int) gdkEvent.x, y = (int) gdkEvent.y;
int [] row = new int [1], column = new int [1];
int code = OS.gtk_clist_get_selection_info (handle, x, y, row, column);
if (code != 0) {
GtkCList clist = new GtkCList ();
OS.memmove (clist, handle, GtkCList.sizeof);
if (selected && clist.selection != 0) {
int list = clist.selection;
int length = OS.g_list_length (list);
for (int i=0; i<length; i++) {
if (row [0] == OS.g_list_nth_data (list, i)) {
postEvent (SWT.Selection);
}
}
}
}
selected = false;
}
return result;
}
int processSelection (int int0, int int1, int int2) {
GtkCList clist = new GtkCList ();
OS.memmove (clist, handle, GtkCList.sizeof);
if (int0 != clist.focus_row) return 0;
if ((style & SWT.MULTI) != 0) selected = false;
boolean single = true;
if (int2 != 0) {
GdkEventButton gdkEvent = new GdkEventButton ();
OS.memmove (gdkEvent, int2, GdkEventButton.sizeof);
single = gdkEvent.type != OS.GDK_2BUTTON_PRESS;
}
if (single) {
postEvent (SWT.Selection);
} else {
postEvent (SWT.DefaultSelection);
}
return 0;
}
/**
* Removes the item from the receiver at the given
* zero-relative index.
*
* @param index the index for the item
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_REMOVED - if the operation fails because of an operating system failure</li>
* </ul>
*/
public void remove (int index) {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_clist_remove (handle, index);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Removes the items from the receiver which are
* between the given zero-relative start and end
* indices (inclusive).
*
* @param start the start of the range
* @param end the end of the range
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_RANGE - if either the start or end are not between 0 and the number of elements in the list minus 1 (inclusive)</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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_REMOVED - if the operation fails because of an operating system failure</li>
* </ul>
*/
public void remove (int start, int end) {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
int index = start;
while (index <= end) {
OS.gtk_clist_remove (handle, start);
index++;
}
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Searches the receiver's list starting at the first item
* until an item is found that is equal to the argument,
* and removes that item from the list.
*
* @param string the item to remove
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the string is null</li>
* <li>ERROR_INVALID_ARGUMENT - if the string is not found in the list</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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_REMOVED - if the operation fails because of an operating system failure</li>
* </ul>
*/
public void remove (String string) {
checkWidget();
int index = indexOf (string, 0);
if (index == -1) error (SWT.ERROR_ITEM_NOT_REMOVED);
remove (index);
}
/**
* Removes the items from the receiver at the given
* zero-relative indices.
*
* @param indices the array of indices of the items
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_REMOVED - if the operation fails because of an operating system failure</li>
* </ul>
*/
public void remove (int [] indices) {
checkWidget();
if (indices == null) error (SWT.ERROR_NULL_ARGUMENT);
int [] newIndices = new int [indices.length];
System.arraycopy (indices, 0, newIndices, 0, indices.length);
sort (newIndices);
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
int last = -1;
for (int i=0; i<newIndices.length; i++) {
int index = newIndices [i];
if (index != last || i == 0) {
OS.gtk_clist_remove (handle, index);
last = index;
}
}
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Removes all of the items from the receiver.
* <p>
* @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 removeAll () {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_clist_clear (handle);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Removes the listener from the collection of listeners who will
* be notified when the receiver's selection changes.
*
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener 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>
*
* @see SelectionListener
* @see #addSelectionListener
*/
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);
}
/**
* Selects the item at the given zero-relative index in the receiver's
* list. If the item at the index was already selected, it remains
* selected. Indices that are out of range are ignored.
*
* @param index the index of the item to select
*
* @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 select (int index) {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_clist_select_row (handle, index, 0);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Selects the items at the given zero-relative indices in the receiver.
* If the item at the index was already selected, it remains
* selected. The range of the indices is inclusive. Indices that are
* out of range are ignored.
*
* @param start the start of the range
* @param end the end of the range
*
* @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 select (int start, int end) {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
for (int i=start; i<=end; i++) {
OS.gtk_clist_select_row (handle, i, 0);
}
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Selects the items at the given zero-relative indices in the receiver.
* If the item at the given zero-relative index in the receiver
* is not selected, it is selected. If the item at the index
* was selected, it remains selected. Indices that are out
* of range and duplicate indices are ignored.
*
* @param indices the array of indices for the items to select
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener 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 select (int [] indices) {
checkWidget();
if (indices == null) error (SWT.ERROR_NULL_ARGUMENT);
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
for (int i=0; i<indices.length; i++) {
OS.gtk_clist_select_row (handle, indices [i], 0);
}
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Selects all the items 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 void selectAll () {
checkWidget();
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_clist_select_all (handle);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Sets the text of the item in the receiver's list at the given
* zero-relative index to the string argument. This is equivalent
* to <code>remove</code>'ing the old item at the index, and then
* <code>add</code>'ing the new item at that index.
*
* @param index the index for the item
* @param string the new text for the item
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_REMOVED - if the remove operation fails because of an operating system failure</li>
* <li>ERROR_ITEM_NOT_ADDED - if the add operation fails because of an operating system failure</li>
* </ul>
*/
public void setItem (int index, String string) {
checkWidget();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
byte [] buffer = Converter.wcsToMbcs (null, string, true);
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_clist_set_text (handle, index, 0, buffer);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
/**
* Sets the receiver's items to be the given array of items.
*
* @param items the array 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>
* @exception SWTError <ul>
* <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li>
* </ul>
*/
public void setItems (String [] items) {
checkWidget();
if (items == null) error (SWT.ERROR_NULL_ARGUMENT);
removeAll ();
for (int i=0; i<items.length; i++) {
add (items [i]);
}
}
/**
* Selects the item at the given zero-relative index in the receiver.
* If the item at the index was already selected, it remains selected.
* The current selected is first cleared, then the new items are selected.
* Indices that are out of range are ignored.
*
* @param index the index of the item to select
*
* @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 List#deselectAll()
* @see List#select(int)
*/
public void setSelection (int index) {
checkWidget();
if ((style & SWT.MULTI) != 0) deselectAll ();
select (index);
showSelection ();
}
/**
* Selects the items at the given zero-relative indices in the receiver.
* The current selected if first cleared, then the new items are selected.
*
* @param start the start index of the items to select
* @param end the end index of the items to select
*
* @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 Table#deselectAll()
* @see Table#select(int,int)
*/
public void setSelection (int start, int end) {
checkWidget();
if ((style & SWT.MULTI) != 0) deselectAll ();
select (start, end);
showSelection ();
}
/**
* Selects the items at the given zero-relative indices in the receiver.
* The current selection is first cleared, then the new items are selected.
*
* @param indices the indices of the items to select
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener 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>
*
* @see List#deselectAll()
* @see List#select(int[])
*/
public void setSelection(int[] indices) {
checkWidget();
if ((style & SWT.MULTI) != 0) deselectAll ();
select (indices);
showSelection ();
}
/**
* Sets the receiver's selection to be the given array of items.
* The current selected is first cleared, then the new items are
* selected.
*
* @param items the array of items
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener 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>
*
* @see List#deselectAll()
* @see List#select(int)
*/
public void setSelection (String [] items) {
checkWidget();
if (items == null) error (SWT.ERROR_NULL_ARGUMENT);
if ((style & SWT.MULTI) != 0) deselectAll ();
for (int i=items.length-1; i>=0; --i) {
int index = 0;
String string = items [i];
if (string != null) {
while ((index = indexOf (string, index)) != -1) {
select (index);
if (((style & SWT.SINGLE) != 0) && isSelected (index)) {
showSelection ();
return;
}
index++;
}
}
}
if ((style & SWT.SINGLE) != 0) deselectAll ();
showSelection ();
}
/**
* Sets the zero-relative index of the item which is currently
* at the top of the receiver. This index can change when items
* are scrolled or new items are added and removed.
*
* @param index the index of the top 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 void setTopIndex (int index) {
checkWidget();
//BUG IN GTK - doesn't scroll correctly before shell open
OS.gtk_clist_moveto (handle, index, 0, 0.0f, 0.0f);
}
/**
* Shows the selection. If the selection is already showing in the receiver,
* this method simply returns. Otherwise, the items are scrolled until
* the selection is visible.
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener 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 showSelection () {
checkWidget();
GtkCList clist = new GtkCList ();
OS.memmove(clist, handle, GtkCList.sizeof);
int list = clist.selection;
if (OS.g_list_length (list) == 0) return;
int index = OS.g_list_nth_data (list, 0);
int visibility = OS.gtk_clist_row_is_visible (handle, index);
if (visibility == OS.GTK_VISIBILITY_FULL) return;
//BUG IN GTK - doesn't scroll correctly before shell open
OS.gtk_clist_moveto (handle, index, 0, 0.5f, 0.0f);
}
}