blob: 9f7ae4d90bbfc46d339b5b86659b8036c8a62eba [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.*;
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>
* Note: Only one of SINGLE and MULTI may be specified.
* </p><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
* 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#SINGLE
* @see SWT#MULTI
* @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);
blockSignal (handle, SWT.Selection);
int result = OS.gtk_clist_append (handle, new int [] {ptr});
unblockSignal (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 (!(0 <= index && index <= OS.GTK_CLIST_ROWS (handle))) {
error (SWT.ERROR_INVALID_RANGE);
}
byte [] buffer = Converter.wcsToMbcs (null, string, true);
int ptr = OS.g_malloc (buffer.length);
OS.memmove (ptr, buffer, buffer.length);
blockSignal (handle, SWT.Selection);
int result = OS.gtk_clist_insert (handle, index, new int [] {ptr});
unblockSignal (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;
fixedHandle = OS.gtk_fixed_new ();
if (fixedHandle == 0) error (SWT.ERROR_NO_HANDLES);
OS.gtk_fixed_set_has_window (fixedHandle, true);
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);
int parentHandle = parent.parentingHandle ();
OS.gtk_container_add (parentHandle, fixedHandle);
OS.gtk_container_add (fixedHandle, scrolledHandle);
OS.gtk_container_add (scrolledHandle, handle);
OS.gtk_widget_show (fixedHandle);
OS.gtk_widget_show (scrolledHandle);
OS.gtk_widget_show (handle);
/* Force row_height to be computed */
OS.gtk_clist_set_row_height (handle, 0);
/* Single or Multiple Selection */
int mode = (style & SWT.MULTI) != 0 ? OS.GTK_SELECTION_EXTENDED :OS.GTK_SELECTION_BROWSE;
OS.gtk_clist_set_selection_mode (handle, mode);
//CHECK POLICY
if ((style & SWT.BORDER) != 0) {
OS.gtk_clist_set_shadow_type(handle, OS.GTK_SHADOW_ETCHED_IN);
}
int hsp = (style & SWT.H_SCROLL) != 0 ? OS.GTK_POLICY_AUTOMATIC : OS.GTK_POLICY_NEVER;
int vsp = (style & SWT.V_SCROLL) != 0 ? OS.GTK_POLICY_AUTOMATIC : OS.GTK_POLICY_NEVER;
OS.gtk_scrolled_window_set_policy (scrolledHandle, hsp, vsp);
}
GdkColor defaultBackground () {
Display display = getDisplay ();
return display.COLOR_LIST_BACKGROUND;
}
GdkColor defaultForeground () {
Display display = getDisplay ();
return display.COLOR_LIST_FOREGROUND;
}
void hookEvents () {
//TO DO - get rid of enter/exit for mouse crossing border
super.hookEvents();
Display display = getDisplay ();
int windowProc3 = display.windowProc3;
int windowProc5 = display.windowProc5;
OS.g_signal_connect (handle, OS.select_row, windowProc5, SWT.Selection);
OS.g_signal_connect (handle, OS.unselect_row, windowProc5, SWT.Selection);
OS.g_signal_connect (handle, OS.event_after, windowProc3, 0);
}
public Point computeSize (int wHint, int hHint, boolean changed) {
checkWidget ();
Point size = super.computeSize (wHint, hHint, changed);
if (wHint != SWT.DEFAULT) {
size.x = wHint;
} else {
GtkStyle st = new GtkStyle ();
OS.memmove (st, OS.gtk_widget_get_style (handle));
size.x = OS.gtk_clist_optimal_column_width (handle, 0) + vScrollBarWidth() + st.xthickness * 2;
}
return size;
}
/**
* 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();
blockSignal (handle, SWT.Selection);
OS.gtk_clist_unselect_row (handle, index, 0);
unblockSignal (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();
blockSignal (handle, SWT.Selection);
for (int i=start; i<=end; i++) {
OS.gtk_clist_unselect_row (handle, i, 0);
}
unblockSignal (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);
blockSignal (handle, SWT.Selection);
for (int i=0; i<indices.length; i++) {
OS.gtk_clist_unselect_row (handle, indices [i], 0);
}
unblockSignal (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();
blockSignal (handle, SWT.Selection);
OS.gtk_clist_unselect_all (handle);
unblockSignal (handle, SWT.Selection);
}
GdkColor getBackgroundColor () {
return getBaseColor ();
}
/**
* 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();
return OS.GTK_CLIST_FOCUS_ROW (handle);
}
/**
* 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();
if (!(0 <= index && index < OS.GTK_CLIST_ROWS (handle))) {
error (SWT.ERROR_INVALID_RANGE);
}
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();
return OS.GTK_CLIST_ROWS (handle);
}
/**
* 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();
return OS.GTK_CLIST_ROW_HEIGHT (handle);
}
/**
* 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();
int list = OS.GTK_CLIST_SELECTION (handle);
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();
int selection = OS.GTK_CLIST_SELECTION (handle);
if (selection == 0) return 0;
return OS.g_list_length (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();
int selection = OS.GTK_CLIST_SELECTION (handle);
if (selection == 0 || OS.g_list_length (selection) == 0) return -1;
return OS.g_list_nth_data (selection, 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();
int list = OS.GTK_CLIST_SELECTION (handle);
if (list == 0) return new int [0];
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();
int voffset = OS.GTK_CLIST_VOFFSET (handle);
int row_height = OS.GTK_CLIST_ROW_HEIGHT (handle);
return -voffset / (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();
int list = OS.GTK_CLIST_SELECTION (handle);
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 paintWindow () {
OS.gtk_widget_realize (handle);
return OS.GTK_CLIST_CLIST_WINDOW (handle);
}
int processEvent (int eventNumber, int int0, int int1, int int2) {
if (eventNumber == 0) {
GdkEvent gdkEvent = new GdkEvent ();
OS.memmove (gdkEvent, int0, GdkEvent.sizeof);
switch (gdkEvent.type) {
case OS.GDK_BUTTON_PRESS:
case OS.GDK_2BUTTON_PRESS: {
OS.GTK_CLIST_RESYNC_SELECTION (handle);
break;
}
case OS.GDK_BUTTON_RELEASE: {
/*
* 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.
*/
if ((style & SWT.MULTI) != 0) {
if (selected) {
double[] px = new double [1], py = new double [1];
OS.gdk_event_get_coords (int0, px, py);
int x = (int) (px[0]), y = (int) (py[0]);
int [] row = new int [1], column = new int [1];
if (OS.gtk_clist_get_selection_info (handle, x, y, row, column) != 0) {
int list = OS.GTK_CLIST_SELECTION (handle);
if (list != 0) {
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;
}
break;
}
case OS.GDK_FOCUS_CHANGE: {
/*
* Bug in GTK. When an application opens a new modal top level
* shell from inside the "select_row" signal, the GtkCList does not get the
* mouse up and does not release grabs. The fix is to release the grabs
* when focus is lost.
*/
GdkEventFocus focusEvent = new GdkEventFocus ();
OS.memmove (focusEvent, int0, GdkEventFocus.sizeof);
if (focusEvent.in == 0) {
if (OS.gtk_grab_get_current () == handle) {
OS.gtk_grab_remove (handle);
OS.gdk_pointer_ungrab (OS.GDK_CURRENT_TIME);
}
}
break;
}
}
return 1;
}
return super.processEvent (eventNumber, int0, int1, int2);
}
int processKeyDown (int callData, int arg1, int int2) {
int result = super.processKeyDown (callData, arg1, int2);
if (result != 0) return result;
/*
* Feature in GTK. When an item is reselected using
* the space bar or default selected using the return key,
* GTK does not issue notification. The fix is to ignore the
* notification that is sent by GTK and look for the space key.
*/
GdkEventKey keyEvent = new GdkEventKey ();
OS.memmove (keyEvent, callData, GdkEventKey.sizeof);
int key = keyEvent.keyval;
switch (key) {
case OS.GDK_Return:
case OS.GDK_KP_Enter:
case OS.GDK_space: {
if (OS.GTK_CLIST_FOCUS_ROW (handle) != -1) {
int type = key == OS.GDK_space ? SWT.Selection : SWT.DefaultSelection;
postEvent (type);
}
break;
}
}
return result;
}
int processMouseDown (int int0, int int1, int int2) {
int result = super.processMouseDown (int0, int1, int2);
if ((style & SWT.MULTI) != 0) selected = true;
return result;
}
int processSelection (int int0, int int1, int int2) {
if (int0 != OS.GTK_CLIST_FOCUS_ROW (handle)) return 0;
if ((style & SWT.MULTI) != 0) selected = false;
int eventType = SWT.Selection;
if (int2 != 0) {
GdkEvent gdkEvent = new GdkEvent ();
OS.memmove (gdkEvent, int2, GdkEvent.sizeof);
if (gdkEvent.type == OS.GDK_2BUTTON_PRESS) {
eventType = SWT.DefaultSelection;
}
}
postEvent (eventType);
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();
if (!(0 <= index && index < OS.GTK_CLIST_ROWS (handle))) {
error (SWT.ERROR_INVALID_RANGE);
}
blockSignal (handle, SWT.Selection);
OS.gtk_clist_remove (handle, index);
unblockSignal (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();
if (!(0 < start && start <= end && end < OS.GTK_CLIST_ROWS (handle))) {
error (SWT.ERROR_INVALID_RANGE);
}
blockSignal (handle, SWT.Selection);
int index = start;
while (index <= end) {
OS.gtk_clist_remove (handle, start);
index++;
}
unblockSignal (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);
blockSignal (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;
}
}
unblockSignal (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();
blockSignal (handle, SWT.Selection);
OS.gtk_clist_clear (handle);
unblockSignal (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();
blockSignal (handle, SWT.Selection);
OS.gtk_clist_select_row (handle, index, 0);
unblockSignal (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();
blockSignal (handle, SWT.Selection);
for (int i=start; i<=end; i++) {
OS.gtk_clist_select_row (handle, i, 0);
}
unblockSignal (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);
blockSignal (handle, SWT.Selection);
for (int i=0; i<indices.length; i++) {
OS.gtk_clist_select_row (handle, indices [i], 0);
}
unblockSignal (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();
blockSignal (handle, SWT.Selection);
OS.gtk_clist_select_all (handle);
unblockSignal (handle, SWT.Selection);
}
void setBackgroundColor (GdkColor color) {
super.setBackgroundColor (color);
OS.gtk_widget_modify_base (handle, 0, color);
}
/**
* 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);
if (!(0 <= index && index < OS.GTK_CLIST_ROWS (handle))) {
error (SWT.ERROR_INVALID_RANGE);
}
byte [] buffer = Converter.wcsToMbcs (null, string, true);
blockSignal (handle, SWT.Selection);
OS.gtk_clist_set_text (handle, index, 0, buffer);
unblockSignal (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 ();
}
public void setRedraw (boolean redraw) {
checkWidget ();
if (redraw) {
OS.gtk_clist_thaw (handle);
} else {
OS.gtk_clist_freeze (handle);
}
}
/**
* 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 (indices == null) error (SWT.ERROR_NULL_ARGUMENT);
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();
int selection = OS.GTK_CLIST_SELECTION (handle);
if (selection == 0) return;
if (OS.g_list_length (selection) == 0) return;
int index = OS.g_list_nth_data (selection, 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);
}
}