/******************************************************************************* * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.widgets; import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.photon.*; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; /** * Instances of this class are responsible for managing the * connection between SWT and the underlying operating * system. Their most important function is to implement * the SWT event loop in terms of the platform event model. * They also provide various methods for accessing information * about the operating system, and have overall control over * the operating system resources which SWT allocates. *
* Applications which are built with SWT will almost always
* require only a single display. In particular, some platforms
* which SWT supports will not allow more than one active
* display. In other words, some platforms do not support
* creating a new display if one already exists that has not been
* sent the dispose()
message.
*
* In SWT, the thread which creates a Display
* instance is distinguished as the user-interface thread
* for that display.
*
Widget
and its subclasses), may only be called
* from the thread. (To support multi-threaded user-interface
* applications, class Display
provides inter-thread
* communication methods which allow threads other than the
* user-interface thread to request that it perform operations
* on their behalf.)
* Display
s until that display has been disposed.
* (Note that, this is in addition to the restriction mentioned
* above concerning platform support for multiple displays. Thus,
* the only way to have multiple simultaneously active displays,
* even on platforms which support it, is to have multiple threads.)
*
* All SWT API methods which may only be called from the user-interface
* thread are distinguished in their documentation by indicating that
* they throw the "ERROR_THREAD_INVALID_ACCESS
"
* SWT exception.
*
* IMPORTANT: This class is not intended to be subclassed. *
* @see #syncExec * @see #asyncExec * @see #wake * @see #readAndDispatch * @see #sleep * @see Device#dispose * @see Display snippets * @see Sample code and further information */ public class Display extends Device { /* TEMPORARY CODE FOR EMULATED TABLE */ int textHighlightThickness = 0; /* TEMPORARY CODE FOR EMBEDDED */ public boolean embedded; /* Photon Only Public Fields */ public int app_context; // public int phEventSize = PhEvent_t.sizeof + 1024; // public int phEvent = OS.malloc (phEventSize); /* Deferred Events */ Event [] eventQueue; EventTable eventTable, filterTable; /* Events Dispatching and Callback */ Callback windowCallback, drawCallback, workCallback, inputCallback, hotkeyCallback; int windowProc, drawProc, workProc, inputProc, hotkeyProc, input, pulse; boolean idle; /* Sync/Async Widget Communication */ Synchronizer synchronizer = new Synchronizer (this); Thread thread; /* Display Shutdown */ Runnable [] disposeList; /* System Tray */ Tray tray; /* Drag origin */ int dragStartX, dragStartY; /* Timers */ int [] timerIds; Runnable [] timerList; Callback timerCallback; int timerProc, timerHandle; /* Keyboard */ int lastKey, lastAscii; /* Key Mappings. */ private static final int [] [] KeyTable = { /* Keyboard and Mouse Masks */ {OS.Pk_Alt_L, SWT.ALT}, {OS.Pk_Alt_R, SWT.ALT}, {OS.Pk_Shift_L, SWT.SHIFT}, {OS.Pk_Shift_R, SWT.SHIFT}, {OS.Pk_Control_L, SWT.CONTROL}, {OS.Pk_Control_R, SWT.CONTROL}, // {OS.Pk_????, SWT.COMMAND}, // {OS.Pk_????, SWT.COMMAND}, // {OS.VK_LBUTTON, SWT.BUTTON1}, // {OS.VK_MBUTTON, SWT.BUTTON3}, // {OS.VK_RBUTTON, SWT.BUTTON2}, /* Non-Numeric Keypad Keys */ {OS.Pk_Up, SWT.ARROW_UP}, {OS.Pk_Down, SWT.ARROW_DOWN}, {OS.Pk_Left, SWT.ARROW_LEFT}, {OS.Pk_Right, SWT.ARROW_RIGHT}, {OS.Pk_Prior, SWT.PAGE_UP}, {OS.Pk_Next, SWT.PAGE_DOWN}, {OS.Pk_Home, SWT.HOME}, {OS.Pk_End, SWT.END}, {OS.Pk_Insert, SWT.INSERT}, /* Virtual and Ascii Keys */ {OS.Pk_BackSpace, SWT.BS}, {OS.Pk_Return, SWT.CR}, {OS.Pk_Delete, SWT.DEL}, {OS.Pk_Escape, SWT.ESC}, {OS.Pk_Linefeed, SWT.LF}, {OS.Pk_Tab, SWT.TAB}, {OS.Pk_KP_Tab, SWT.TAB}, /* Functions Keys */ {OS.Pk_F1, SWT.F1}, {OS.Pk_F2, SWT.F2}, {OS.Pk_F3, SWT.F3}, {OS.Pk_F4, SWT.F4}, {OS.Pk_F5, SWT.F5}, {OS.Pk_F6, SWT.F6}, {OS.Pk_F7, SWT.F7}, {OS.Pk_F8, SWT.F8}, {OS.Pk_F9, SWT.F9}, {OS.Pk_F10, SWT.F10}, {OS.Pk_F11, SWT.F11}, {OS.Pk_F12, SWT.F12}, {OS.Pk_F13, SWT.F13}, {OS.Pk_F14, SWT.F14}, {OS.Pk_F15, SWT.F15}, /* Numeric Keypad Keys */ {OS.Pk_KP_Multiply, SWT.KEYPAD_MULTIPLY}, {OS.Pk_KP_Add, SWT.KEYPAD_ADD}, {OS.Pk_KP_Enter, SWT.KEYPAD_CR}, {OS.Pk_KP_Subtract, SWT.KEYPAD_SUBTRACT}, {OS.Pk_KP_Decimal, SWT.KEYPAD_DECIMAL}, {OS.Pk_KP_Divide, SWT.KEYPAD_DIVIDE}, {OS.Pk_KP_0, SWT.KEYPAD_0}, {OS.Pk_KP_1, SWT.KEYPAD_1}, {OS.Pk_KP_2, SWT.KEYPAD_2}, {OS.Pk_KP_3, SWT.KEYPAD_3}, {OS.Pk_KP_4, SWT.KEYPAD_4}, {OS.Pk_KP_5, SWT.KEYPAD_5}, {OS.Pk_KP_6, SWT.KEYPAD_6}, {OS.Pk_KP_7, SWT.KEYPAD_7}, {OS.Pk_KP_8, SWT.KEYPAD_8}, {OS.Pk_KP_9, SWT.KEYPAD_9}, {OS.Pk_KP_Equal, SWT.KEYPAD_EQUAL}, /* Other keys */ {OS.Pk_Caps_Lock, SWT.CAPS_LOCK}, {OS.Pk_Num_Lock, SWT.NUM_LOCK}, {OS.Pk_Scroll_Lock, SWT.SCROLL_LOCK}, {OS.Pk_Pause, SWT.PAUSE}, {OS.Pk_Break, SWT.BREAK}, {OS.Pk_Print, SWT.PRINT_SCREEN}, {OS.Pk_Help, SWT.HELP}, }; /* Multiple Displays. */ static Display Default; static Display [] Displays = new Display [4]; /* Window Classes */ int ClassesPtr; int PtButton; int PtList; int PtLabel; int PtToggleButton; int PtComboBox; int PtText; int PtMultiText; int PtScrollbar; int PtContainer; int PtProgress; int PtPanelGroup; int PtSlider; int PtSeparator; int PtToolbar; int PtNumericInteger; /* Colors */ int WIDGET_DARK_SHADOW, WIDGET_NORMAL_SHADOW, WIDGET_LIGHT_SHADOW; int WIDGET_HIGHLIGHT_SHADOW, WIDGET_BACKGROUND, WIDGET_FOREGROUND, WIDGET_BORDER; int LIST_FOREGROUND, LIST_BACKGROUND, LIST_SELECTION, LIST_SELECTION_TEXT; int INFO_FOREGROUND, INFO_BACKGROUND, TEXT_FOREGROUND, TEXT_BACKGROUND; /* Fonts */ byte [] defaultFont; byte [] TEXT_FONT, LIST_FONT, TITLE_FONT, GAUGE_FONT, GROUP_FONT; /* System Cursors */ Cursor [] cursors = new Cursor [SWT.CURSOR_HAND + 1]; /* Images */ int nullImage; /* ScrollBars */ int SCROLLBAR_WIDTH; int SCROLLBAR_HEIGHT; int SCROLLBAR_VERTICAL_BASIC_FLAGS; int SCROLLBAR_HORIZONTAL_BASIC_FLAGS; /* Package name */ static final String PACKAGE_NAME; static { String name = Display.class.getName (); int index = name.lastIndexOf ('.'); PACKAGE_NAME = name.substring (0, index + 1); } /* Photon Draw Buffer - shared by all widgets */ static int DrawBufferSize = 1024 * 48; /* Display Data */ Object data; String [] keys; Object [] values; /* * TEMPORARY CODE. Install the runnable that * gets the current display. This code will * be removed in the future. */ static { DeviceFinder = new Runnable () { public void run () { Device device = getCurrent (); if (device == null) { device = getDefault (); } setDevice (device); } }; } /* * TEMPORARY CODE. */ static void setDevice (Device device) { CurrentDevice = device; } /** * Constructs a new instance of this class. ** Note: The resulting display is marked as the current * display. If this is the first display which has been * constructed since the application started, it is also * marked as the default display. *
* * @exception SWTExceptionSWT
. When the event does occur,
* the listener is notified by sending it the handleEvent()
* message.
*
* Setting the type of an event to SWT.None
from
* within the handleEvent()
method can be used to
* change the event type and stop subsequent Java listeners
* from running. Because event filters run before other listeners,
* event filters can both block other listeners and set arbitrary
* fields within an event. For this reason, event filters are both
* powerful and dangerous. They should generally be avoided for
* performance, debugging and code maintenance reasons.
*
SWT
.
* When the event does occur in the display, 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 run()
method of the runnable to
* be invoked by the user-interface thread at the next
* reasonable opportunity. The caller of this method continues
* to run in parallel, and is not notified when the
* runnable has completed. Specifying null
as the
* runnable simply wakes the user-interface thread when run.
* * Note that at the time the runnable is invoked, widgets * that have the receiver as their display may have been * disposed. Therefore, it is necessary to check for this * case inside the runnable before accessing the widget. *
* * @param runnable code to run on the user-interface thread ornull
*
* @exception SWTException Widget.checkSubclass()
.
*
*
* @exception SWTException
* This method is called before init
.
*
release
.
*
* @see Device#dispose
* @see #release
*/
protected void destroy () {
if (this == Default) Default = null;
deregister (this);
destroyDisplay ();
}
void destroyDisplay () {
// NEED to destroy app_context ???
}
/**
* Causes the run()
method of the runnable to
* be invoked by the user-interface thread just before the
* receiver is disposed. Specifying a null
runnable
* is ignored.
*
* @param runnable code to run at dispose time.
*
* @exception SWTException null
* for the display.
*
* @param thread the user-interface thread
* @return the display for the given thread
*/
public static Display findDisplay (Thread thread) {
synchronized (Device.class) {
for (int i=0; i* IMPORTANT: This method should not be called from * application code. The arguments are platform-specific. *
* * @param handle the handle for the widget * @return the SWT widget that the handle represents * * @exception SWTExceptionWidget
subclass which represents
* the handle/id pair in the currently running application,
* if such exists, or null if no matching widget can be found.
* * IMPORTANT: This method should not be called from * application code. The arguments are platform-specific. *
* * @param handle the handle for the widget * @param id the id for the subwidget (usually an item) * @return the SWT widget that the handle/id pair represents * * @exception SWTExceptionWidget
subclass which represents
* the widget/id pair in the currently running application,
* if such exists, or null if no matching widget can be found.
*
* @param widget the widget
* @param id the id for the subwidget (usually an item)
* @return the SWT subwidget (usually an item) that the widget/id pair represents
*
* @exception SWTException Shell
, or null
* if no shell belonging to the currently running application
* is active.
*
* @return the active shell or null
*
* @exception SWTException null
for the name clears it.
*
* @param name the new app name or null
*/
public static void setAppName (String name) {
/* Do nothing */
}
/**
* Returns the button dismissal alignment, one of LEFT
or RIGHT
.
* The button dismissal alignment is the ordering that should be used when positioning the
* default dismissal button for a dialog. For example, in a dialog that contains an OK and
* CANCEL button, on platforms where the button dismissal alignment is LEFT
, the
* button ordering should be OK/CANCEL. When button dismissal alignment is RIGHT
,
* the button ordering should be CANCEL/OK.
*
* @return the button dismissal order
*
* @exception SWTException * Note: This operation is a hint and is not supported on * platforms that do not have this concept. *
* * @return the high contrast mode * * @exception SWTExceptionSWT
. This cursor should
* not be free'd because it was allocated by the system,
* not the application. A value of null
will
* be returned if the supplied constant is not an SWT cursor
* constant.
*
* @param id the SWT cursor constant
* @return the corresponding cursor or null
*
* @exception SWTException * Typically, applications which want the default look * should simply not set the font on the widgets they * create. Widgets are always created with the correct * default font for the class of user-interface component * they represent. *
* * @return a font * * @exception SWTExceptionSWT
. This image should
* not be free'd because it was allocated by the system,
* not the application. A value of null
will
* be returned either if the supplied constant is not an
* SWT icon constant or if the platform does not define an
* image that corresponds to the constant.
*
* @param id the SWT icon constant
* @return the corresponding image or null
*
* @exception SWTException null
*
* @exception SWTException syncExec
* or null if no such runnable is currently being invoked by
* the user-interface thread.
* * Note: If a runnable invoked by asyncExec is currently * running, this method will return null. *
* * @return the receiver's sync-interface thread * * @exception SWTException
* This method is called after create
.
*
* IMPORTANT: This method is not part of the public
* API for Display
. It is marked public only so that it
* can be shared within the packages provided by SWT. It is not
* available on all platforms, and should never be called from
* application code.
*
* IMPORTANT: This method is not part of the public
* API for Display
. It is marked public only so that it
* can be shared within the packages provided by SWT. It is not
* available on all platforms, and should never be called from
* application code.
*
* 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 display is disposed
* of, it is the application's responsibility to provide a
* disposeExec()
handler which does so.
*
* Applications may put arbitrary objects in this field. If
* the object stored in the display specific data needs to
* be notified when the display is disposed of, it is the
* application's responsibility to provide a
* disposeExec()
handler which does so.
*
* NOTE: On right-to-left platforms where the coordinate * systems are mirrored, special care needs to be taken * when mapping coordinates from one control to another * to ensure the result is correctly mirrored. * * Mapping a point that is the origin of a rectangle and * then adding the width and height is not equivalent to * mapping the rectangle. When one control is mirrored * and the other is not, adding the width and height to a * point that was mapped causes the rectangle to extend * in the wrong direction. Mapping the entire rectangle * instead of just one point causes both the origin and * the corner of the rectangle to be mapped. *
* * @param from the sourceControl
or null
* @param to the destination Control
or null
* @param point to be mapped
* @return point with mapped coordinates
*
* @exception IllegalArgumentException * NOTE: On right-to-left platforms where the coordinate * systems are mirrored, special care needs to be taken * when mapping coordinates from one control to another * to ensure the result is correctly mirrored. * * Mapping a point that is the origin of a rectangle and * then adding the width and height is not equivalent to * mapping the rectangle. When one control is mirrored * and the other is not, adding the width and height to a * point that was mapped causes the rectangle to extend * in the wrong direction. Mapping the entire rectangle * instead of just one point causes both the origin and * the corner of the rectangle to be mapped. *
* * @param from the sourceControl
or null
* @param to the destination Control
or null
* @param x coordinates to be mapped
* @param y coordinates to be mapped
* @return point with mapped coordinates
*
* @exception IllegalArgumentException * NOTE: On right-to-left platforms where the coordinate * systems are mirrored, special care needs to be taken * when mapping coordinates from one control to another * to ensure the result is correctly mirrored. * * Mapping a point that is the origin of a rectangle and * then adding the width and height is not equivalent to * mapping the rectangle. When one control is mirrored * and the other is not, adding the width and height to a * point that was mapped causes the rectangle to extend * in the wrong direction. Mapping the entire rectangle * instead of just one point causes both the origin and * the corner of the rectangle to be mapped. *
* * @param from the sourceControl
or null
* @param to the destination Control
or null
* @param rectangle to be mapped
* @return rectangle with mapped coordinates
*
* @exception IllegalArgumentException * NOTE: On right-to-left platforms where the coordinate * systems are mirrored, special care needs to be taken * when mapping coordinates from one control to another * to ensure the result is correctly mirrored. * * Mapping a point that is the origin of a rectangle and * then adding the width and height is not equivalent to * mapping the rectangle. When one control is mirrored * and the other is not, adding the width and height to a * point that was mapped causes the rectangle to extend * in the wrong direction. Mapping the entire rectangle * instead of just one point causes both the origin and * the corner of the rectangle to be mapped. *
* * @param from the sourceControl
or null
* @param to the destination Control
or null
* @param x coordinates to be mapped
* @param y coordinates to be mapped
* @param width coordinates to be mapped
* @param height coordinates to be mapped
* @return rectangle with mapped coordinates
*
* @exception IllegalArgumentException post
is used to generate low level keyboard
* and mouse events. The intent is to enable automated UI
* testing by simulating the input from the user. Most
* SWT applications should never need to call this method.
* * Note that this operation can fail when the operating system * fails to generate the event for any reason. For example, * this can happen when there is no such key or mouse button * or when the system event queue is full. *
** Event Types: *
KeyDown, KeyUp *
The following fields in the Event
apply:
*
Either one of: *
SWT
MouseDown, MouseUp
*The following fields in the Event
apply:
*
MouseMove
*The following fields in the Event
apply:
*
true
* if there is potentially more work to do, or false
* if the caller can sleep until another event is placed on
* the event queue.
*
* In addition to checking the system event queue, this method also
* checks if any inter-thread messages (created by syncExec()
* or asyncExec()
) are waiting to be processed, and if
* so handles them before returning.
*
false
if the caller can sleep upon return from this method
*
* @exception SWTException true
when sent the message
* isDisposed()
.
*
* When a device is destroyed, resources that were acquired
* on behalf of the programmer need to be returned to the
* operating system. For example, if the device allocated a
* font to be used as the system font, this font would be
* freed in release
. Also,to assist the garbage
* collector and minimize the amount of memory that is not
* reclaimed when the programmer keeps a reference to a
* disposed device, all fields except the handle are zero'd.
* The handle is needed by destroy
.
*
destroy
.
*
* @see Device#dispose
* @see #destroy
*/
protected void release () {
sendEvent (SWT.Dispose, new Event ());
Shell [] shells = WidgetTable.shells ();
for (int i=0; iSWT
.
*
* @param eventType the type of event to listen for
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException
* 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 display is disposed
* of, it is the application's responsibility provide a
* disposeExec()
handler which does so.
*
* Applications may put arbitrary objects in this field. If
* the object stored in the display specific data needs to
* be notified when the display is disposed of, it is the
* application's responsibility provide a
* disposeExec()
handler which does so.
*
true
if an event requiring dispatching was placed on the queue.
*
* @exception SWTException run()
method of the runnable to
* be invoked by the user-interface thread at the next
* reasonable opportunity. The thread which calls this method
* is suspended until the runnable completes. Specifying null
* as the runnable simply wakes the user-interface thread.
* * Note that at the time the runnable is invoked, widgets * that have the receiver as their display may have been * disposed. Therefore, it is necessary to check for this * case inside the runnable before accessing the widget. *
* * @param runnable code to run on the user-interface thread ornull
*
* @exception SWTException run()
method of the runnable to
* be invoked by the user-interface thread after the specified
* number of milliseconds have elapsed. If milliseconds is less
* than zero, the runnable is not executed.
* * Note that at the time the runnable is invoked, widgets * that have the receiver as their display may have been * disposed. Therefore, it is necessary to check for this * case inside the runnable before accessing the widget. *
* * @param milliseconds the delay before running the runnable * @param runnable code to run on the user-interface thread * * @exception IllegalArgumentException