| /******************************************************************************* |
| * Copyright (c) 2000, 2003 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are 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 |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.swt.widgets; |
| |
| |
| import org.eclipse.swt.*; |
| import org.eclipse.swt.internal.*; |
| import org.eclipse.swt.internal.gtk.*; |
| import org.eclipse.swt.events.*; |
| |
| /** |
| * This class is the abstract superclass of all user interface objects. |
| * Widgets are created, disposed and issue notification to listeners |
| * when events occur which affect them. |
| * <dl> |
| * <dt><b>Styles:</b></dt> |
| * <dd>(none)</dd> |
| * <dt><b>Events:</b></dt> |
| * <dd>Dispose</dd> |
| * </dl> |
| * <p> |
| * IMPORTANT: This class is intended to be subclassed <em>only</em> |
| * within the SWT implementation. However, it has not been marked |
| * final to allow those outside of the SWT development team to implement |
| * patched versions of the class in order to get around specific |
| * limitations in advance of when those limitations can be addressed |
| * by the team. Any class built using subclassing to access the internals |
| * of this class will likely fail to compile or run between releases and |
| * may be strongly platform specific. Subclassing should not be attempted |
| * without an intimate and detailed understanding of the workings of the |
| * hierarchy. No support is provided for user-written classes which are |
| * implemented as subclasses of this class. |
| * </p> |
| * |
| * @see #checkSubclass |
| */ |
| public abstract class Widget { |
| /** |
| * the handle to the OS resource |
| * (Warning: This field is platform dependent) |
| */ |
| public int handle; |
| int style, state; |
| Display display; |
| EventTable eventTable; |
| Object data; |
| |
| /* Global state flags */ |
| // static final int AUTOMATIC = 1<<0; |
| // static final int ACTIVE = 1<<1; |
| // static final int AUTOGRAB = 1<<2; |
| // static final int MULTIEXPOSE = 1<<3; |
| // static final int RESIZEREDRAW = 1<<4; |
| // static final int WRAP = 1<<5; |
| static final int DISABLED = 1<<6; |
| // static final int HIDDEN = 1<<7; |
| // static final int FOREGROUND = 1<<8; |
| // static final int BACKGROUND = 1<<9; |
| static final int DISPOSED = 1<<10; |
| static final int HANDLE = 1<<11; |
| static final int CANVAS = 1<<12; |
| // static final int MOVED = 1<<13; |
| // static final int RESIZED = 1<<14; |
| static final int KEYED_DATA = 1<<15; |
| |
| /* Default widths for widgets */ |
| static final int DEFAULT_WIDTH = 64; |
| static final int DEFAULT_HEIGHT = 64; |
| |
| /* GTK signals data */ |
| static final int ACTIVATE = 1; |
| static final int BUTTON_PRESS_EVENT = 2; |
| static final int BUTTON_RELEASE_EVENT = 3; |
| static final int CHANGED = 4; |
| static final int CLICKED = 5; |
| static final int COMMIT = 6; |
| static final int CONFIGURE_EVENT = 7; |
| static final int DELETE_EVENT = 8; |
| static final int DELETE_RANGE = 9; |
| static final int DELETE_TEXT = 10; |
| static final int ENTER_NOTIFY_EVENT = 11; |
| static final int EVENT = 12; |
| static final int EVENT_AFTER = 13; |
| static final int EXPOSE_EVENT = 14; |
| static final int FOCUS_IN_EVENT = 15; |
| static final int FOCUS_OUT_EVENT = 16; |
| static final int HIDE = 17; |
| static final int INSERT_TEXT = 18; |
| static final int KEY_PRESS_EVENT = 19; |
| static final int KEY_RELEASE_EVENT = 20; |
| static final int LEAVE_NOTIFY_EVENT = 21; |
| static final int MAP_EVENT = 22; |
| static final int MNEMONIC_ACTIVATE = 23; |
| static final int MOTION_NOTIFY_EVENT = 24; |
| static final int POPUP_MENU = 25; |
| static final int PREEDIT_CHANGED = 26; |
| static final int REALIZE = 40; |
| static final int ROW_ACTIVATED = 27; |
| static final int ROW_COLLAPSED = 28; |
| static final int ROW_EXPANDED = 29; |
| static final int SELECT = 30; |
| static final int SELECT_CHILD = 31; |
| static final int SHOW = 32; |
| static final int SHOW_HELP = 33; |
| static final int SIZE_ALLOCATE = 34; |
| static final int SWITCH_PAGE = 35; |
| static final int TOGGLED = 36; |
| static final int UNMAP_EVENT = 37; |
| static final int UNREALIZE = 38; |
| static final int VALUE_CHANGED = 39; |
| static final int FOCUS = 40; |
| |
| /** |
| * Prevents uninitialized instances from being created outside the package. |
| */ |
| Widget () {} |
| |
| /** |
| * 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 widget which will be the parent of the new instance (cannot be null) |
| * @param style the style of widget 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 #checkSubclass |
| * @see #getStyle |
| */ |
| public Widget (Widget parent, int style) { |
| checkSubclass (); |
| checkParent (parent); |
| this.style = style; |
| display = parent.display; |
| } |
| |
| /** |
| * Adds the listener to the collection of listeners who will |
| * be notifed when an event of the given type occurs. When the |
| * event does occur in the widget, the listener is notified by |
| * sending it the <code>handleEvent()</code> message. |
| * |
| * @param eventType the type of event to listen for |
| * @param listener the listener which should be notified when the event occurs |
| * |
| * @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 Listener |
| * @see #removeListener |
| */ |
| public void addListener (int eventType, Listener handler) { |
| checkWidget (); |
| if (handler == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (eventTable == null) eventTable = new EventTable (); |
| eventTable.hook (eventType, handler); |
| } |
| |
| /** |
| * Adds the listener to the collection of listeners who will |
| * be notifed when the widget is disposed. When the widget is |
| * disposed, the listener is notified by sending it the |
| * <code>widgetDisposed()</code> message. |
| * |
| * @param listener the listener which should be notified when the receiver is disposed |
| * |
| * @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 DisposeListener |
| * @see #removeDisposeListener |
| */ |
| public void addDisposeListener (DisposeListener listener) { |
| checkWidget (); |
| if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); |
| TypedListener typedListener = new TypedListener (listener); |
| addListener (SWT.Dispose, typedListener); |
| } |
| |
| static int checkBits (int style, int int0, int int1, int int2, int int3, int int4, int int5) { |
| int mask = int0 | int1 | int2 | int3 | int4 | int5; |
| if ((style & mask) == 0) style |= int0; |
| if ((style & int0) != 0) style = (style & ~mask) | int0; |
| if ((style & int1) != 0) style = (style & ~mask) | int1; |
| if ((style & int2) != 0) style = (style & ~mask) | int2; |
| if ((style & int3) != 0) style = (style & ~mask) | int3; |
| if ((style & int4) != 0) style = (style & ~mask) | int4; |
| if ((style & int5) != 0) style = (style & ~mask) | int5; |
| return style; |
| } |
| |
| void checkOrientation (Widget parent) { |
| style &= ~SWT.MIRRORED; |
| if ((style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT)) == 0) { |
| if (parent != null) { |
| if ((parent.style & SWT.LEFT_TO_RIGHT) != 0) style |= SWT.LEFT_TO_RIGHT; |
| if ((parent.style & SWT.RIGHT_TO_LEFT) != 0) style |= SWT.RIGHT_TO_LEFT; |
| } |
| } |
| style = checkBits (style, SWT.LEFT_TO_RIGHT, SWT.RIGHT_TO_LEFT, 0, 0, 0, 0); |
| } |
| |
| /** |
| * Throws an exception if the specified widget can not be |
| * used as a parent for the receiver. |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
| * <li>ERROR_INVALID_ARGUMENT - if the parent is disposed</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
| * </ul> |
| */ |
| void checkParent (Widget parent) { |
| if (parent == null) error (SWT.ERROR_NULL_ARGUMENT); |
| parent.checkWidget (); |
| } |
| |
| /** |
| * Checks that this class can be subclassed. |
| * <p> |
| * The SWT class library is intended to be subclassed |
| * only at specific, controlled points (most notably, |
| * <code>Composite</code> and <code>Canvas</code> when |
| * implementing new widgets). This method enforces this |
| * rule unless it is overridden. |
| * </p><p> |
| * <em>IMPORTANT:</em> By providing an implementation of this |
| * method that allows a subclass of a class which does not |
| * normally allow subclassing to be created, the implementer |
| * agrees to be fully responsible for the fact that any such |
| * subclass will likely fail between SWT releases and will be |
| * strongly platform specific. No support is provided for |
| * user-written classes which are implemented in this fashion. |
| * </p><p> |
| * The ability to subclass outside of the allowed SWT classes |
| * is intended purely to enable those not on the SWT development |
| * team to implement patches in order to get around specific |
| * limitations in advance of when those limitations can be |
| * addressed by the team. Subclassing should not be attempted |
| * without an intimate and detailed understanding of the hierarchy. |
| * </p> |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
| * </ul> |
| */ |
| protected void checkSubclass () { |
| if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); |
| } |
| |
| /** |
| * Throws an <code>SWTException</code> if the receiver can not |
| * be accessed by the caller. This may include both checks on |
| * the state of the receiver and more generally on the entire |
| * execution context. This method <em>should</em> be called by |
| * widget implementors to enforce the standard SWT invariants. |
| * <p> |
| * Currently, it is an error to invoke any method (other than |
| * <code>isDisposed()</code>) on a widget that has had its |
| * <code>dispose()</code> method called. It is also an error |
| * to call widget methods from any thread that is different |
| * from the thread that created the widget. |
| * </p><p> |
| * In future releases of SWT, there may be more or fewer error |
| * checks and exceptions may be thrown for different reasons. |
| * </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> |
| */ |
| protected void checkWidget () { |
| Display display = this.display; |
| if (display == null) error (SWT.ERROR_WIDGET_DISPOSED); |
| if (display.thread != Thread.currentThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); |
| if ((state & DISPOSED) != 0) error (SWT.ERROR_WIDGET_DISPOSED); |
| } |
| |
| void createHandle (int index) { |
| } |
| |
| void createWidget (int index) { |
| createHandle (index); |
| hookEvents (); |
| register (); |
| } |
| |
| void deregister () { |
| if (handle == 0) return; |
| if ((state & HANDLE) != 0) WidgetTable.remove (handle); |
| } |
| |
| void destroyWidget () { |
| int topHandle = topHandle (); |
| releaseHandle (); |
| if (topHandle != 0 && (state & HANDLE) != 0) { |
| OS.gtk_widget_destroy (topHandle); |
| } |
| } |
| |
| /** |
| * Disposes of the operating system resources associated with |
| * the receiver and all its descendents. After this method has |
| * been invoked, the receiver and all descendents will answer |
| * <code>true</code> when sent the message <code>isDisposed()</code>. |
| * Any internal connections between the widgets in the tree will |
| * have been removed to facilitate garbage collection. |
| * <p> |
| * NOTE: This method is not called recursively on the descendents |
| * of the receiver. This means that, widget implementers can not |
| * detect when a widget is being disposed of by re-implementing |
| * this method, but should instead listen for the <code>Dispose</code> |
| * event. |
| * </p> |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| * |
| * @see #addDisposeListener |
| * @see #removeDisposeListener |
| * @see #checkWidget |
| */ |
| public void dispose () { |
| /* |
| * Note: It is valid to attempt to dispose a widget |
| * more than once. If this happens, fail silently. |
| */ |
| if (isDisposed()) return; |
| if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); |
| releaseChild (); |
| releaseWidget (); |
| destroyWidget (); |
| } |
| |
| void error (int code) { |
| SWT.error (code); |
| } |
| |
| /** |
| * Returns the application defined widget data associated |
| * with the receiver, or null if it has not been set. The |
| * <em>widget data</em> is a single, unnamed field that is |
| * stored with every widget. |
| * <p> |
| * Applications may put arbitrary objects in this field. If |
| * the object stored in the widget data needs to be notified |
| * when the widget is disposed of, it is the application's |
| * responsibility to hook the Dispose event on the widget and |
| * do so. |
| * </p> |
| * |
| * @return the widget data |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - when the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - when called from the wrong thread</li> |
| * </ul> |
| * |
| * @see #setData |
| */ |
| public Object getData () { |
| checkWidget(); |
| return (state & KEYED_DATA) != 0 ? ((Object []) data) [0] : data; |
| } |
| /** |
| * Returns the application defined property of the receiver |
| * with the specified name, or null if it has not been set. |
| * <p> |
| * Applications may have associated arbitrary objects with the |
| * receiver in this fashion. If the objects stored in the |
| * properties need to be notified when the widget is disposed |
| * of, it is the application's responsibility to hook the |
| * Dispose event on the widget and do so. |
| * </p> |
| * |
| * @param key the name of the property |
| * @return the value of the property or null if it has not been set |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the key 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 #setData |
| */ |
| public Object getData (String key) { |
| checkWidget(); |
| if (key == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if ((state & KEYED_DATA) != 0) { |
| Object [] table = (Object []) data; |
| for (int i=1; i<table.length; i+=2) { |
| if (key.equals (table [i])) return table [i+1]; |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * Returns the <code>Display</code> that is associated with |
| * the receiver. |
| * <p> |
| * A widget's display is either provided when it is created |
| * (for example, top level <code>Shell</code>s) or is the |
| * same as its parent's display. |
| * </p> |
| * |
| * @return the receiver's display |
| * |
| * @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 Display getDisplay () { |
| Display display = this.display; |
| if (display == null) error (SWT.ERROR_WIDGET_DISPOSED); |
| return display; |
| } |
| |
| String getName () { |
| // String string = getClass ().getName (); |
| // int index = string.lastIndexOf ('.'); |
| // if (index == -1) return string; |
| String string = getClass ().getName (); |
| int index = string.length (); |
| while ((--index > 0) && (string.charAt (index) != '.')); |
| return string.substring (index + 1, string.length ()); |
| } |
| |
| String getNameText () { |
| return ""; |
| } |
| |
| /** |
| * Returns the receiver's style information. |
| * <p> |
| * Note that the value which is returned by this method <em>may |
| * not match</em> the value which was provided to the constructor |
| * when the receiver was created. This can occur when the underlying |
| * operating system does not support a particular combination of |
| * requested styles. For example, if the platform widget used to |
| * implement a particular SWT widget always has scroll bars, the |
| * result of calling this method would always have the |
| * <code>SWT.H_SCROLL</code> and <code>SWT.V_SCROLL</code> bits set. |
| * </p> |
| * |
| * @return the style bits |
| * |
| * @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 getStyle () { |
| checkWidget (); |
| return style; |
| } |
| |
| |
| int gtk_activate (int widget) { |
| return 0; |
| } |
| |
| int gtk_button_press_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_button_release_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_changed (int widget) { |
| return 0; |
| } |
| |
| int gtk_clicked (int widget) { |
| return 0; |
| } |
| |
| int gtk_commit (int imcontext, int text) { |
| return 0; |
| } |
| |
| int gtk_configure_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_delete_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_delete_range (int widget, int iter1, int iter2) { |
| return 0; |
| } |
| |
| int gtk_delete_text (int widget, int start_pos, int end_pos) { |
| return 0; |
| } |
| |
| int gtk_enter_notify_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_event_after (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_expose_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_focus (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_focus_in_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_focus_out_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_hide (int widget) { |
| return 0; |
| } |
| |
| int gtk_insert_text (int widget, int new_text, int new_text_length, int position) { |
| return 0; |
| } |
| |
| int gtk_key_press_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_key_release_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_leave_notify_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_map_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_mnemonic_activate (int widget, int arg1) { |
| return 0; |
| } |
| |
| int gtk_motion_notify_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_popup_menu (int widget) { |
| return 0; |
| } |
| |
| int gtk_preedit_changed (int imcontext) { |
| return 0; |
| } |
| |
| int gtk_realize (int widget) { |
| return 0; |
| } |
| |
| int gtk_row_activated (int tree, int path, int column) { |
| return 0; |
| } |
| |
| int gtk_row_collapsed (int tree, int iter, int path) { |
| return 0; |
| } |
| |
| int gtk_row_expanded (int tree, int iter, int path) { |
| return 0; |
| } |
| |
| int gtk_select (int item) { |
| return 0; |
| } |
| |
| int gtk_select_child (int list, int widget) { |
| return 0; |
| } |
| |
| int gtk_show (int widget) { |
| return 0; |
| } |
| |
| int gtk_show_help (int widget, int helpType) { |
| return 0; |
| } |
| |
| int gtk_size_allocate (int widget, int allocation) { |
| return 0; |
| } |
| |
| int gtk_switch_page (int widget, int page, int page_num) { |
| return 0; |
| } |
| |
| int gtk_timer () { |
| return 0; |
| } |
| |
| int gtk_toggled (int renderer, int pathStr) { |
| return 0; |
| } |
| |
| int gtk_unmap_event (int widget, int event) { |
| return 0; |
| } |
| |
| int gtk_unrealize (int widget) { |
| return 0; |
| } |
| |
| int gtk_value_changed (int adjustment) { |
| return 0; |
| } |
| |
| int fontHeight (int font, int widgetHandle) { |
| int context = OS.gtk_widget_get_pango_context (widgetHandle); |
| int lang = OS.pango_context_get_language (context); |
| int metrics = OS.pango_context_get_metrics (context, font, lang); |
| int ascent = OS.pango_font_metrics_get_ascent (metrics); |
| int descent = OS.pango_font_metrics_get_descent (metrics); |
| OS.pango_font_metrics_unref (metrics); |
| return OS.PANGO_PIXELS (ascent + descent); |
| } |
| |
| boolean filters (int eventType) { |
| return display.filters (eventType); |
| } |
| |
| char [] fixMnemonic (String string) { |
| int length = string.length (); |
| char [] text = new char [length]; |
| string.getChars (0, length, text, 0); |
| int i = 0, j = 0; |
| char [] result = new char [length * 2 + 1]; |
| while (i < length) { |
| switch (text [i]) { |
| case '&': |
| if (i + 1 < length && text [i + 1] == '&') { |
| i++; |
| } else { |
| text [i] = '_'; |
| } |
| break; |
| case '_': |
| result [j++] = '_'; |
| break; |
| } |
| result [j++] = text [i++]; |
| } |
| return result; |
| } |
| |
| /** |
| * Returns <code>true</code> if the widget has been disposed, |
| * and <code>false</code> otherwise. |
| * <p> |
| * This method gets the dispose state for the widget. |
| * When a widget has been disposed, it is an error to |
| * invoke any other method using the widget. |
| * </p> |
| * |
| * @return <code>true</code> when the widget is disposed and <code>false</code> otherwise |
| */ |
| public boolean isDisposed () { |
| if (handle != 0) return false; |
| if ((state & HANDLE) != 0) return true; |
| return (state & DISPOSED) != 0; |
| } |
| |
| /** |
| * Returns <code>true</code> if there are any listeners |
| * for the specified event type associated with the receiver, |
| * and <code>false</code> otherwise. |
| * |
| * @param eventType the type of event |
| * @return true if the event is hooked |
| * |
| * @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> |
| */ |
| protected boolean isListening (int eventType) { |
| checkWidget (); |
| return hooks (eventType); |
| } |
| |
| boolean isValidThread () { |
| return getDisplay ().isValidThread (); |
| } |
| |
| boolean isValidWidget () { |
| if (handle != 0) return true; |
| if ((state & HANDLE) != 0) return false; |
| return (state & DISPOSED) == 0; |
| } |
| |
| boolean isValidSubclass() { |
| return Display.isValidClass(getClass()); |
| } |
| |
| void hookEvents () { |
| } |
| |
| /* |
| * Returns <code>true</code> if the specified eventType is |
| * hooked, and <code>false</code> otherwise. Implementations |
| * of SWT can avoid creating objects and sending events |
| * when an event happens in the operating system but |
| * there are no listeners hooked for the event. |
| * |
| * @param eventType the event to be checked |
| * |
| * @return <code>true</code> when the eventType is hooked and <code>false</code> otherwise |
| * |
| * @see #isListening |
| */ |
| boolean hooks (int eventType) { |
| if (eventTable == null) return false; |
| return eventTable.hooks (eventType); |
| } |
| |
| int hoverProc (int widget) { |
| return 0; |
| } |
| |
| char mbcsToWcs (char ch) { |
| int key = ch & 0xFFFF; |
| if (key <= 0x7F) return ch; |
| byte [] buffer; |
| if (key <= 0xFF) { |
| buffer = new byte [1]; |
| buffer [0] = (byte) key; |
| } else { |
| buffer = new byte [2]; |
| buffer [0] = (byte) ((key >> 8) & 0xFF); |
| buffer [1] = (byte) (key & 0xFF); |
| } |
| char [] result = Converter.mbcsToWcs (null, buffer); |
| if (result.length == 0) return 0; |
| return result [0]; |
| } |
| |
| /** |
| * Notifies all of the receiver's listeners for events |
| * of the given type that one such event has occurred by |
| * invoking their <code>handleEvent()</code> method. |
| * |
| * @param eventType the type of event which has occurred |
| * @param event the event data |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the event 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 notifyListeners (int eventType, Event event) { |
| checkWidget(); |
| if (event == null) error (SWT.ERROR_NULL_ARGUMENT); |
| sendEvent (eventType, event); |
| } |
| |
| void postEvent (int eventType) { |
| sendEvent (eventType, null, false); |
| } |
| |
| void postEvent (int eventType, Event event) { |
| sendEvent (eventType, event, false); |
| } |
| |
| void register () { |
| if (handle == 0) return; |
| if ((state & HANDLE) != 0) WidgetTable.put (handle, this); |
| } |
| |
| void releaseChild () { |
| /* Do nothing */ |
| } |
| |
| void releaseHandle () { |
| handle = 0; |
| state |= DISPOSED; |
| display = null; |
| } |
| |
| void releaseResources () { |
| releaseWidget (); |
| releaseHandle (); |
| } |
| |
| void releaseWidget () { |
| sendEvent (SWT.Dispose); |
| deregister (); |
| eventTable = null; |
| data = null; |
| } |
| |
| /** |
| * Removes the listener from the collection of listeners who will |
| * be notifed when an event of the given type occurs. |
| * |
| * @param eventType the type of event to listen for |
| * @param listener the listener which should no longer be notified when the event occurs |
| * |
| * @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 Listener |
| * @see #addListener |
| */ |
| public void removeListener (int eventType, Listener handler) { |
| checkWidget (); |
| if (handler == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (eventTable == null) return; |
| eventTable.unhook (eventType, handler); |
| } |
| |
| /** |
| * Removes the listener from the collection of listeners who will |
| * be notifed when an event of the given type occurs. |
| * <p> |
| * <b>IMPORTANT:</b> This method is <em>not</em> part of the SWT |
| * public API. It is marked public only so that it can be shared |
| * within the packages provided by SWT. It should never be |
| * referenced from application code. |
| * </p> |
| * |
| * @param eventType the type of event to listen for |
| * @param listener the listener which should no longer be notified when the event occurs |
| * |
| * @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 Listener |
| * @see #addListener |
| */ |
| protected void removeListener (int eventType, SWTEventListener handler) { |
| checkWidget (); |
| if (handler == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (eventTable == null) return; |
| eventTable.unhook (eventType, handler); |
| } |
| |
| /** |
| * Removes the listener from the collection of listeners who will |
| * be notifed when the widget is disposed. |
| * |
| * @param listener the listener which should no longer be notified when the receiver is disposed |
| * |
| * @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 DisposeListener |
| * @see #addDisposeListener |
| */ |
| public void removeDisposeListener (DisposeListener listener) { |
| checkWidget (); |
| if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (eventTable == null) return; |
| eventTable.unhook (SWT.Dispose, listener); |
| } |
| |
| void sendEvent (Event event) { |
| Display display = event.display; |
| if (!display.filterEvent (event)) { |
| if (eventTable != null) eventTable.sendEvent (event); |
| } |
| } |
| |
| void sendEvent (int eventType) { |
| sendEvent (eventType, null, true); |
| } |
| |
| void sendEvent (int eventType, Event event) { |
| sendEvent (eventType, event, true); |
| } |
| |
| void sendEvent (int eventType, Event event, boolean send) { |
| if (eventTable == null && !display.filters (eventType)) { |
| return; |
| } |
| if (event == null) event = new Event (); |
| event.type = eventType; |
| event.display = display; |
| event.widget = this; |
| if (event.time == 0) { |
| event.time = display.getLastEventTime (); |
| } |
| if (send) { |
| sendEvent (event); |
| } else { |
| display.postEvent (event); |
| } |
| } |
| |
| /** |
| * Sets the application defined widget data associated |
| * with the receiver to be the argument. The <em>widget |
| * data</em> is a single, unnamed field that is stored |
| * with every widget. |
| * <p> |
| * Applications may put arbitrary objects in this field. If |
| * the object stored in the widget data needs to be notified |
| * when the widget is disposed of, it is the application's |
| * responsibility to hook the Dispose event on the widget and |
| * do so. |
| * </p> |
| * |
| * @param data the widget data |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - when the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - when called from the wrong thread</li> |
| * </ul> |
| */ |
| public void setData (Object data) { |
| checkWidget(); |
| if ((state & KEYED_DATA) != 0) { |
| ((Object []) this.data) [0] = data; |
| } else { |
| this.data = data; |
| } |
| } |
| |
| /** |
| * Sets the application defined property of the receiver |
| * with the specified name to the given value. |
| * <p> |
| * Applications may associate arbitrary objects with the |
| * receiver in this fashion. If the objects stored in the |
| * properties need to be notified when the widget is disposed |
| * of, it is the application's responsibility to hook the |
| * Dispose event on the widget and do so. |
| * </p> |
| * |
| * @param key the name of the property |
| * @param value the new value for the property |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the key 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 #getData |
| */ |
| public void setData (String key, Object value) { |
| checkWidget(); |
| if (key == null) error (SWT.ERROR_NULL_ARGUMENT); |
| int index = 1; |
| Object [] table = null; |
| if ((state & KEYED_DATA) != 0) { |
| table = (Object []) data; |
| while (index < table.length) { |
| if (key.equals (table [index])) break; |
| index += 2; |
| } |
| } |
| if (value != null) { |
| if ((state & KEYED_DATA) != 0) { |
| if (index == table.length) { |
| Object [] newTable = new Object [table.length + 2]; |
| System.arraycopy (table, 0, newTable, 0, table.length); |
| data = table = newTable; |
| } |
| } else { |
| table = new Object [3]; |
| table [0] = data; |
| data = table; |
| state |= KEYED_DATA; |
| } |
| table [index] = key; |
| table [index + 1] = value; |
| } else { |
| if ((state & KEYED_DATA) != 0) { |
| if (index != table.length) { |
| int length = table.length - 2; |
| if (length == 1) { |
| data = table [0]; |
| state &= ~KEYED_DATA; |
| } else { |
| Object [] newTable = new Object [length]; |
| System.arraycopy (table, 0, newTable, 0, index); |
| System.arraycopy (table, index + 2, newTable, index, length - index); |
| data = newTable; |
| } |
| } |
| } |
| } |
| } |
| |
| void setInputState (Event event, int state) { |
| if ((state & OS.GDK_MOD1_MASK) != 0) event.stateMask |= SWT.ALT; |
| if ((state & OS.GDK_SHIFT_MASK) != 0) event.stateMask |= SWT.SHIFT; |
| if ((state & OS.GDK_CONTROL_MASK) != 0) event.stateMask |= SWT.CONTROL; |
| if ((state & OS.GDK_BUTTON1_MASK) != 0) event.stateMask |= SWT.BUTTON1; |
| if ((state & OS.GDK_BUTTON2_MASK) != 0) event.stateMask |= SWT.BUTTON2; |
| if ((state & OS.GDK_BUTTON3_MASK) != 0) event.stateMask |= SWT.BUTTON3; |
| } |
| |
| void setKeyState (Event event, GdkEventKey keyEvent) { |
| if (keyEvent.length <= 1) { |
| event.keyCode = Display.translateKey (keyEvent.keyval); |
| switch (keyEvent.keyval) { |
| case OS.GDK_BackSpace: event.character = '\b'; break; |
| case OS.GDK_Linefeed: event.character = '\n'; break; |
| case OS.GDK_Return: event.character = '\r'; break; |
| case OS.GDK_Delete: event.character = 0x7F; break; |
| case OS.GDK_Cancel: |
| case OS.GDK_Escape: event.character = 0x1B; break; |
| case OS.GDK_Tab: |
| case OS.GDK_ISO_Left_Tab: event.character = '\t'; break; |
| // case OS.GDK_Clear: event.character = 0xB; break; |
| // case OS.GDK_Pause: event.character = 0x13; break; |
| // case OS.GDK_Scroll_Lock: event.character = 0x14; break; |
| default: { |
| if (event.keyCode == 0) { |
| int key = keyEvent.keyval; |
| if ((keyEvent.state & OS.GDK_CONTROL_MASK) != 0 && (0 <= key && key <= 0x7F)) { |
| if ('a' <= key && key <= 'z') key -= 'a' - 'A'; |
| if (64 <= key && key <= 95) key -= 64; |
| event.character = (char) key; |
| } else { |
| event.character = (char) OS.gdk_keyval_to_unicode (key); |
| } |
| } |
| } |
| } |
| } |
| setInputState (event, keyEvent.state); |
| } |
| |
| /** |
| * Returns a string containing a concise, human-readable |
| * description of the receiver. |
| * |
| * @return a string representation of the receiver |
| */ |
| public String toString () { |
| String string = "*Disposed*"; |
| if (!isDisposed ()) { |
| string = "*Wrong Thread*"; |
| if (isValidThread ()) string = getNameText (); |
| } |
| return getName () + " {" + string + "}"; |
| } |
| |
| int topHandle () { |
| return handle; |
| } |
| |
| int timerProc (int widget) { |
| return 0; |
| } |
| |
| int treeSelectionProc (int model, int path, int iter, int[] selection, int length) { |
| return 0; |
| } |
| |
| boolean translateTraversal (int event) { |
| return false; |
| } |
| |
| char wcsToMbcs (char ch) { |
| int key = ch & 0xFFFF; |
| if (key <= 0x7F) return ch; |
| byte [] buffer = Converter.wcsToMbcs (null, new char [] {ch}, false); |
| if (buffer.length == 1) return (char) buffer [0]; |
| if (buffer.length == 2) { |
| return (char) (((buffer [0] & 0xFF) << 8) | (buffer [1] & 0xFF)); |
| } |
| return 0; |
| } |
| |
| int windowProc (int handle, int user_data) { |
| switch (user_data) { |
| case ACTIVATE: return gtk_activate (handle); |
| case CHANGED: return gtk_changed (handle); |
| case CLICKED: return gtk_clicked (handle); |
| case HIDE: return gtk_hide (handle); |
| case POPUP_MENU: return gtk_popup_menu (handle); |
| case PREEDIT_CHANGED: return gtk_preedit_changed (handle); |
| case REALIZE: return gtk_realize (handle); |
| case SELECT: return gtk_select (handle); |
| case SHOW: return gtk_show (handle); |
| case VALUE_CHANGED: return gtk_value_changed (handle); |
| case UNREALIZE: return gtk_unrealize (handle); |
| default: return 0; |
| } |
| } |
| |
| int windowProc (int handle, int arg0, int user_data) { |
| switch (user_data) { |
| case -BUTTON_PRESS_EVENT: |
| case -BUTTON_RELEASE_EVENT: |
| case -MOTION_NOTIFY_EVENT: { |
| return 1; |
| } |
| case BUTTON_PRESS_EVENT: return gtk_button_press_event (handle, arg0); |
| case BUTTON_RELEASE_EVENT: return gtk_button_release_event (handle, arg0); |
| case COMMIT: return gtk_commit (handle, arg0); |
| case CONFIGURE_EVENT: return gtk_configure_event (handle, arg0); |
| case DELETE_EVENT: return gtk_delete_event (handle, arg0); |
| case ENTER_NOTIFY_EVENT: return gtk_enter_notify_event (handle, arg0); |
| case EVENT: return gtk_event (handle, arg0); |
| case EVENT_AFTER: return gtk_event_after (handle, arg0); |
| case EXPOSE_EVENT: return gtk_expose_event (handle, arg0); |
| case FOCUS: return gtk_focus (handle, arg0); |
| case FOCUS_IN_EVENT: return gtk_focus_in_event (handle, arg0); |
| case FOCUS_OUT_EVENT: return gtk_focus_out_event (handle, arg0); |
| case KEY_PRESS_EVENT: return gtk_key_press_event (handle, arg0); |
| case KEY_RELEASE_EVENT: return gtk_key_release_event (handle, arg0); |
| case LEAVE_NOTIFY_EVENT: return gtk_leave_notify_event (handle, arg0); |
| case MAP_EVENT: return gtk_map_event (handle, arg0); |
| case MNEMONIC_ACTIVATE: return gtk_mnemonic_activate (handle, arg0); |
| case MOTION_NOTIFY_EVENT: return gtk_motion_notify_event (handle, arg0); |
| case SELECT_CHILD: return gtk_select_child (handle, arg0); |
| case SHOW_HELP: return gtk_show_help (handle, arg0); |
| case SIZE_ALLOCATE: return gtk_size_allocate (handle, arg0); |
| case TOGGLED: return gtk_toggled (handle, arg0); |
| case UNMAP_EVENT: return gtk_unmap_event (handle, arg0); |
| default: return 0; |
| } |
| } |
| |
| int windowProc (int handle, int arg0, int arg1, int user_data) { |
| switch (user_data) { |
| case DELETE_RANGE: return gtk_delete_range (handle, arg0, arg1); |
| case DELETE_TEXT: return gtk_delete_text (handle, arg0, arg1); |
| case ROW_ACTIVATED: return gtk_row_activated (handle, arg0, arg1); |
| case ROW_COLLAPSED: return gtk_row_collapsed (handle, arg0, arg1); |
| case ROW_EXPANDED: return gtk_row_expanded(handle, arg0, arg1); |
| case SWITCH_PAGE: return gtk_switch_page (handle, arg0, arg1); |
| default: return 0; |
| } |
| } |
| |
| int windowProc (int handle, int arg0, int arg1, int arg2, int user_data) { |
| switch (user_data) { |
| case INSERT_TEXT: return gtk_insert_text (handle, arg0, arg1, arg2); |
| default: return 0; |
| } |
| } |
| |
| |
| } |