package org.eclipse.swt.widgets; /* * (c) Copyright IBM Corp. 2000, 2001, 2002. * All Rights Reserved */ import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.photon.*; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; 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. *
*
Styles:
*
(none)
*
Events:
*
Dispose
*
*

* IMPORTANT: This class is intended to be subclassed only * 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. *

* * @see #checkSubclass */ public abstract class Widget { public int handle; int style, state; EventTable eventTable; Object data; String [] keys; Object [] values; static final int DISPOSED = 0x00000001; static final int HANDLE = 0x00000002; static final int CANVAS = 0x00000004; static final int DEFAULT_WIDTH = 64; static final int DEFAULT_HEIGHT = 64; static final char Mnemonic = '&'; Widget () { /* Do nothing */ } /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. *

* The style value is either one of the style constants defined in * class SWT which is applicable to instances of this * class, or must be built by bitwise OR'ing together * (that is, using the int "|" operator) two or more * of those SWT style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. *

* * @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 * @exception SWTException * * @see SWT * @see #checkSubclass * @see #getStyle */ public Widget (Widget parent, int style) { checkSubclass (); checkParent (parent); this.style = style; } 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 checkParent (Widget parent) { if (parent == null) error (SWT.ERROR_NULL_ARGUMENT); if (!parent.isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (parent.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT); } /** * Checks that this class can be subclassed. *

* The SWT class library is intended to be subclassed * only at specific, controlled points (most notably, * Composite and Canvas when * implementing new widgets). This method enforces this * rule unless it is overridden. *

* IMPORTANT: 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. *

* 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. *

* * @exception SWTException */ protected void checkSubclass () { if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); } /** * Throws an SWTException 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 should be called by * widget implementors to enforce the standard SWT invariants. *

* Currently, it is an error to invoke any method (other than * isDisposed()) on a widget that has had its * dispose() method called. It is also an error * to call widget methods from any thread that is different * from the thread that created the widget. *

* In future releases of SWT, there may be more or fewer error * checks and exceptions may be thrown for different reasons. *

* * @exception SWTException */ protected void checkWidget () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (isDisposed ()) error (SWT.ERROR_WIDGET_DISPOSED); } int copyPhImage(int image) { if (image == 0) return 0; int newImage = OS.PiDuplicateImage (image, 0); /* * Bug in Photon. The image returned by PiDuplicateImage might * have the same mask_bm/alpha as the original image. The fix * is to detect this case and copy mask_bm/alpha if necessary. */ PhImage_t phImage = new PhImage_t(); OS.memmove (phImage, image, PhImage_t.sizeof); PhImage_t newPhImage = new PhImage_t(); OS.memmove(newPhImage, newImage, PhImage_t.sizeof); if (newPhImage.mask_bm != 0 && phImage.mask_bm == newPhImage.mask_bm) { int length = newPhImage.mask_bpl * newPhImage.size_h; int ptr = OS.malloc(length); OS.memmove(ptr, newPhImage.mask_bm, length); newPhImage.mask_bm = ptr; } if (newPhImage.alpha != 0 && phImage.alpha == newPhImage.alpha) { PgAlpha_t alpha = new PgAlpha_t(); OS.memmove(alpha, phImage.alpha, PgAlpha_t.sizeof); if (alpha.src_alpha_map_map != 0) { int length = alpha.src_alpha_map_bpl * alpha.src_alpha_map_dim_h; int ptr = OS.malloc(length); OS.memmove(ptr, alpha.src_alpha_map_map, length); alpha.src_alpha_map_map = ptr; } int ptr = OS.malloc(PgAlpha_t.sizeof); OS.memmove(ptr, alpha, PgAlpha_t.sizeof); newPhImage.alpha = ptr; } OS.memmove(newImage, newPhImage, PhImage_t.sizeof); return newImage; } /** * 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 handleEvent() message. * * @param eventType the type of event to listen for * @param listener the listener which should be notified when the event occurs * * @exception IllegalArgumentException * @exception SWTException * * @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 * widgetDisposed() message. * * @param listener the listener which should be notified when the receiver is disposed * * @exception IllegalArgumentException * @exception SWTException * * @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); } void createHandle (int index) { /* Do nothing */ } int createToolTip (String string, int handle, byte [] font) { if (string == null || string.length () == 0 || handle == 0) { return 0; } int shellHandle = OS.PtFindDisjoint (handle); byte [] buffer = Converter.wcsToMbcs (null, string, true); Display display = getDisplay (); int fill = display.INFO_BACKGROUND; int text_color = display.INFO_FOREGROUND; int toolTipHandle = OS.PtInflateBalloon (shellHandle, handle, OS.Pt_BALLOON_RIGHT, buffer, font, fill, text_color); /* * Feature in Photon. The position of the inflated balloon * is relative to the widget position and not to the cursor * position. The fix is to re-position the balloon. */ int ig = OS.PhInputGroup (0); PhCursorInfo_t info = new PhCursorInfo_t (); OS.PhQueryCursor ((short)ig, info); short [] absX = new short [1], absY = new short [1]; OS.PtGetAbsPosition (shellHandle, absX, absY); int x = info.pos_x - absX [0] + 16; int y = info.pos_y - absY [0] + 16; PhArea_t shellArea = new PhArea_t (); OS.PtWidgetArea (shellHandle, shellArea); PhArea_t toolTipArea = new PhArea_t (); OS.PtWidgetArea (toolTipHandle, toolTipArea); x = Math.max (0, Math.min (x, shellArea.size_w - toolTipArea.size_w)); y = Math.max (0, Math.min (y, shellArea.size_h - toolTipArea.size_h)); PhPoint_t pt = new PhPoint_t (); pt.x = (short) x; pt.y = (short) y; int ptr = OS.malloc (PhPoint_t.sizeof); OS.memmove (ptr, pt, PhPoint_t.sizeof); OS.PtSetResource (toolTipHandle, OS.Pt_ARG_POS, ptr, 0); OS.free (ptr); return toolTipHandle; } void createWidget (int index) { createHandle (index); hookEvents (); register (); } void deregister () { if (handle == 0) return; WidgetTable.remove (handle); } void destroyToolTip (int toolTipHandle) { if (toolTipHandle != 0) OS.PtDestroyWidget (toolTipHandle); } void destroyWidget () { int topHandle = topHandle (); releaseHandle (); if (topHandle != 0) { OS.PtDestroyWidget (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 * true when sent the message isDisposed(). * Any internal connections between the widgets in the tree will * have been removed to facilitate garbage collection. *

* 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 Dispose * event. *

* * @exception SWTException * * @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 (); } static 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 * widget data is a single, unnamed field that is * stored with every widget. *

* 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. *

* * @return the widget data * * @exception SWTException * * @see #setData */ public Object getData () { checkWidget(); return data; } /** * Returns the application defined property of the receiver * with the specified name, or null if it has not been set. *

* 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. *

* * @param key the name of the property * @return the value of the property or null if it has not been set * * @exception IllegalArgumentException * @exception SWTException * * @see #setData */ public Object getData (String key) { checkWidget(); if (key == null) error (SWT.ERROR_NULL_ARGUMENT); if (keys == null) return null; for (int i=0; iDisplay that is associated with * the receiver. *

* A widget's display is either provided when it is created * (for example, top level Shells) or is the * same as its parent's display. *

* * @return the receiver's display * * @exception SWTException */ public abstract Display getDisplay (); String getName () { 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. *

* Note that the value which is returned by this method may * not match 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 * SWT.H_SCROLL and SWT.V_SCROLL bits set. *

* * @return the style bits * * @exception SWTException */ public int getStyle () { checkWidget(); return style; } boolean hooks (int eventType) { if (eventTable == null) return false; return eventTable.hooks (eventType); } void hookEvents () { /* Do nothing */ } /** * Returns true if the widget has been disposed, * and false otherwise. *

* 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. *

* * @return true when the widget is disposed and false otherwise */ public boolean isDisposed () { if (handle != 0) return false; if ((state & HANDLE) != 0) return true; return (state & DISPOSED) != 0; } boolean isValidSubclass () { return Display.isValidClass (getClass ()); } protected boolean isListening (int eventType) { checkWidget(); return hooks (eventType); } boolean isValidThread () { return getDisplay ().isValidThread (); } /** * Notifies all of the receiver's listeners for events * of the given type that one such event has occurred by * invoking their handleEvent() method. * * @param eventType the type of event which has occurred * @param event the event data * * @exception IllegalArgumentException * @exception SWTException */ public void notifyListeners (int eventType, Event event) { checkWidget(); if (event == null) error (SWT.ERROR_NULL_ARGUMENT); sendEvent (eventType, event); } void postEvent (int eventType, Event event) { if (eventTable == null) return; Display display = getDisplay (); event.type = eventType; event.widget = this; event.display = display; if (event.time == 0) { event.time = (int) System.currentTimeMillis (); } display.postEvent (event); } void postEvent (int eventType) { if (eventTable == null) return; postEvent (eventType, new Event ()); } int processActivate (int info) { return OS.Pt_CONTINUE; } int processArm (int info) { return OS.Pt_CONTINUE; } int processDefaultSelection (int info) { return OS.Pt_CONTINUE; } int processFocusIn (int info) { return OS.Pt_CONTINUE; } int processFocusOut (int info) { return OS.Pt_CONTINUE; } int processHide (int info) { return OS.Pt_CONTINUE; } int processHotkey (int data, int info) { return OS.Pt_CONTINUE; } int processKey (int info) { return OS.Pt_CONTINUE; } int processEvent (int widget, int data, int info) { switch (data) { case SWT.Activate: return processActivate (info); case SWT.Arm: return processArm (info); // case SWT.Dispose: return processDispose (info); case SWT.DefaultSelection: return processDefaultSelection (info); case SWT.FocusIn: return processFocusIn (info); case SWT.FocusOut: return processFocusOut (info); // case SWT.Help: return processHelp (info); case SWT.Hide: return processHide (info); case SWT.KeyDown: case SWT.KeyUp: return processKey (info); case SWT.Modify: return processModify (info); case SWT.MouseDown: case SWT.MouseMove: case SWT.MouseUp: return processMouse (info); case SWT.MouseEnter: return processMouseEnter (info); // case SWT.MouseExit: return processMouseOut (info); case SWT.Move: return processMove (info); // case SWT.Paint: return processPaint (info); case SWT.Resize: return processResize (info); case SWT.Show: return processShow (info); case SWT.Selection: return processSelection (info); case SWT.Verify: return processVerify (info); } return OS.Pt_CONTINUE; } int processModify (int info) { return OS.Pt_CONTINUE; } int processMouse (int info) { return OS.Pt_CONTINUE; } int processMouseEnter (int info) { return OS.Pt_CONTINUE; } int processMove (int info) { return OS.Pt_CONTINUE; } int processPaint (int damage) { return OS.Pt_CONTINUE; } int processResize (int info) { return OS.Pt_CONTINUE; } int processShow (int info) { return OS.Pt_CONTINUE; } int processSelection (int info) { return OS.Pt_CONTINUE; } int processVerify (int info) { return OS.Pt_CONTINUE; } void releaseChild () { /* Do nothing */ } void register () { if (handle == 0) return; WidgetTable.put (handle, this); } void releaseHandle () { handle = 0; state |= DISPOSED; } void releaseWidget () { sendEvent (SWT.Dispose); deregister (); eventTable = null; data = null; keys = null; values = 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 * @exception SWTException * * @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. *

* IMPORTANT: This method is not 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. *

* * @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 * @exception SWTException * * @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 * @exception SWTException * * @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 replaceMnemonic (int mnemonic, boolean normal, boolean alt) { Display display = getDisplay (); int [] args = {OS.Pt_ARG_ACCEL_KEY, 0, 0}; OS.PtGetResources (handle, args.length / 3, args); if (args [1] != 0) { int length = OS.strlen (args [1]); if (length > 0) { byte [] buffer = new byte [length]; OS.memmove (buffer, args [1], length); char [] accelText = Converter.mbcsToWcs (null, buffer); if (accelText.length > 0) { char key = Character.toLowerCase (accelText [0]); if (normal) { OS.PtRemoveHotkeyHandler (handle, key, 0, (short)0, SWT.Activate, display.windowProc); } if (alt) { OS.PtRemoveHotkeyHandler (handle, key, OS.Pk_KM_Alt, (short)0, SWT.Activate, display.windowProc); } } } } if (mnemonic == 0) return; char key = Character.toLowerCase ((char)mnemonic); if (normal) { OS.PtAddHotkeyHandler (handle, key, 0, (short)0, SWT.Activate, display.windowProc); } if (alt) { OS.PtAddHotkeyHandler (handle, key, OS.Pk_KM_Alt, (short)0, SWT.Activate, display.windowProc); } } void sendEvent (int eventType, Event event) { if (eventTable == null) return; Display display = getDisplay (); event.widget = this; event.type = eventType; event.display = display; if (event.time == 0) { event.time = (int) System.currentTimeMillis (); } eventTable.sendEvent (event); } void sendEvent (int eventType) { if (eventTable == null) return; sendEvent (eventType, new Event ()); } void sendEvent (Event event) { if (eventTable == null) return; eventTable.sendEvent (event); } /** * Sets the application defined widget data associated * with the receiver to be the argument. The widget * data is a single, unnamed field that is stored * with every widget. *

* 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. *

* * @param data the widget data * * @exception SWTException */ public void setData (Object data) { checkWidget(); this.data = data; } /** * Sets the application defined property of the receiver * with the specified name to the given value. *

* 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. *

* * @param key the name of the property * @param value the new value for the property * * @exception IllegalArgumentException * @exception SWTException * * @see #getData */ public void setData (String key, Object value) { checkWidget(); if (key == null) error (SWT.ERROR_NULL_ARGUMENT); /* Remove the key/value pair */ if (value == null) { if (keys == null) return; int index = 0; while (index < keys.length && !keys [index].equals (key)) index++; if (index == keys.length) return; if (keys.length == 1) { keys = null; values = null; } else { String [] newKeys = new String [keys.length - 1]; Object [] newValues = new Object [values.length - 1]; System.arraycopy (keys, 0, newKeys, 0, index); System.arraycopy (keys, index + 1, newKeys, index, newKeys.length - index); System.arraycopy (values, 0, newValues, 0, index); System.arraycopy (values, index + 1, newValues, index, newValues.length - index); keys = newKeys; values = newValues; } return; } /* Add the key/value pair */ if (keys == null) { keys = new String [] {key}; values = new Object [] {value}; return; } for (int i=0; i