javauis/eswt_qt/org.eclipse.swt/Eclipse SWT/qt/org/eclipse/swt/widgets/Display.java
changeset 35 85266cc22c7f
parent 26 dc7c549001d5
child 47 f40128debb5d
child 49 35baca0e7a2e
equal deleted inserted replaced
26:dc7c549001d5 35:85266cc22c7f
   111 
   111 
   112 /* QApplication */
   112 /* QApplication */
   113 static int handle;
   113 static int handle;
   114 
   114 
   115 /* QApplication event filter */
   115 /* QApplication event filter */
   116 int eventFilterHandle;
   116 static int eventFilterHandle;
   117 
   117 
   118 static final String SWT_OBJECT_INDEX = "swt_obj_idx";
   118 static final String SWT_OBJECT_INDEX = "swt_obj_idx";
   119 static final String ADD_WIDGET_KEY = "org.eclipse.swt.internal.addWidget";
   119 static final String ADD_WIDGET_KEY = "org.eclipse.swt.internal.addWidget";
   120 static final String REMOVE_WIDGET_KEY = "org.eclipse.swt.internal.removeWidget";
   120 static final String REMOVE_WIDGET_KEY = "org.eclipse.swt.internal.removeWidget";
   121 static final String IMAGE_LOAD_SIZE_KEY = "org.eclipse.swt.internal.image.loadSize";
   121 static final String IMAGE_LOAD_SIZE_KEY = "org.eclipse.swt.internal.image.loadSize";
   122 static final String SWT_EVENT_FILTER = "swt_event_filter";
   122 static final String SWT_EVENT_FILTER = "swt_event_filter";
   123 
   123 
   124 public CommandArranger commandArranger;
   124 static CommandArranger commandArranger;
   125 
   125 
   126 Event[] eventQueue;
   126 static Event[] eventQueue;
   127 EventTable eventTable, filterTable;
   127 static EventTable eventTable, filterTable;
   128 
   128 
   129 // Note that this won't work with multiple displays in the same process.
       
   130 // All the displays will have the same appname.
       
   131 static String APP_NAME;
   129 static String APP_NAME;
   132 
   130 
   133 /* Widget Table */
   131 /* Widget Table */
   134 int[] indexTable;
   132 static int[] indexTable;
   135 int freeSlot;
   133 static int freeSlot;
   136 int lastHandle;
   134 static int lastHandle;
   137 Widget lastWidget;
   135 static Widget lastWidget;
   138 Widget[] widgetTable;
   136 static Widget[] widgetTable;
   139 final static int GROW_SIZE = 64;
   137 final static int GROW_SIZE = 64;
   140 
   138 
   141 MobileDevice mobileDevice;
   139 static MobileDevice mobileDevice;
   142 int mobileDeviceHandle;
   140 static int mobileDeviceHandle;
   143 
   141 
   144 /* Modality */
   142 /* Modality */
   145 Shell [] modalShells;
   143 static Shell [] modalShells;
   146 Dialog modalDialog;
   144 static Dialog modalDialog;
   147 
   145 
   148 /* Sync/Async Widget Communication */
   146 /* Sync/Async Widget Communication */
   149 Synchronizer synchronizer = new Synchronizer (this);
   147 static Synchronizer synchronizer;
   150 Thread thread;
   148 Thread thread;
   151 
   149 
   152 static Thread fixedUiThread;
   150 static Thread fixedUiThread;
   153 
   151 
   154 /* Display Shutdown */
   152 /* Display Shutdown */
   155 Runnable [] disposeList;
   153 Runnable [] disposeList;
   156 
   154 
   157 /* System Tray */
   155 /* System Tray */
   158 Tray tray;
   156 static Tray tray;
   159 
   157 
   160 /* Timers */
   158 /* Timers */
   161 int [] timerHandles;
   159 static int [] timerHandles;
   162 Runnable [] timerRunnables;
   160 static Runnable [] timerRunnables;
   163 
   161 
   164 /* Timestamp of the Last Received Event */
   162 /* Timestamp of the Last Received Event */
   165 int lastEventTime;
   163 static int lastEventTime;
   166 
   164 
   167 /* If not to interrupt event processing after every event */
   165 /* If not to interrupt event processing after every event */
   168 boolean noInterrupt;
   166 static boolean noInterrupt;
   169 
   167 
   170 /* Used to block one type of events temporarily */
   168 /* Used to block one type of events temporarily */
   171 int blockedQtEventType;
   169 static int blockedQtEventType;
   172 
   170 
   173 /* Default orientation for Controls when style is not specified */
   171 /* Default orientation for Controls when style is not specified */
   174 int defaultOrientation;
   172 static int defaultOrientation;
   175 boolean defaultOrientationIsSysLangDirection;
   173 static boolean defaultOrientationIsSysLangDirection;
   176 
   174 
   177 // The next Image loaded by Image class constructors will be scaled to
   175 // The next Image loaded by Image class constructors will be scaled to
   178 // this size. Can be set using Display.setData([key], Point). Useful
   176 // this size. Can be set using Display.setData([key], Point). Useful
   179 // for SVG images. 
   177 // for SVG images. 
   180 private Point imageLoadSize;
   178 static private Point imageLoadSize;
   181 
   179 
   182 /* Key Mappings */
   180 /* Key Mappings */
   183 static final int [] [] KeyTable = {
   181 static final int [] [] KeyTable = {
   184     /* Keyboard and Mouse Masks */
   182     /* Keyboard and Mouse Masks */
   185     {OS.QT_KEY_ALT,     SWT.ALT},
   183     {OS.QT_KEY_ALT,     SWT.ALT},
   285 static final int SYMBIAN_FN_LEFT     = 0x18;    // EStdKeyLeftFunc
   283 static final int SYMBIAN_FN_LEFT     = 0x18;    // EStdKeyLeftFunc
   286 static final int SYMBIAN_FN_RIGHT    = 0x19;    // EStdKeyRightFunc
   284 static final int SYMBIAN_FN_RIGHT    = 0x19;    // EStdKeyRightFunc
   287 
   285 
   288 /* Multiple Displays. */
   286 /* Multiple Displays. */
   289 static Display Default;
   287 static Display Default;
       
   288 static Display Internal;
   290 static Display [] Displays = new Display [4];
   289 static Display [] Displays = new Display [4];
   291 
   290 
   292 /* Package name */
   291 /* Package name */
   293 static final String PACKAGE_PREFIX = "org.eclipse.swt.widgets.";
   292 static final String PACKAGE_PREFIX = "org.eclipse.swt.widgets.";
   294 
   293 
   297 
   296 
   298 /* extension package name */
   297 /* extension package name */
   299 static final String INTERNAL_EXTENSION_PACKAGE_PREFIX = "org.eclipse.swt.internal.extension.";
   298 static final String INTERNAL_EXTENSION_PACKAGE_PREFIX = "org.eclipse.swt.internal.extension.";
   300 
   299 
   301 /* Display Data */
   300 /* Display Data */
   302 Object data;
   301 static Object data;
   303 String [] keys;
   302 static String [] keys;
   304 Object [] values;
   303 static Object [] values;
   305 
   304 
   306 /* Last ShortcutOverride event parameters */
   305 /* Last ShortcutOverride event parameters */
   307 int shortcutOverrideKey;
   306 static int shortcutOverrideKey;
   308 int shortcutOverrideModifier;
   307 static int shortcutOverrideModifier;
   309 int shortcutOverrideCharacter;
   308 static int shortcutOverrideCharacter;
   310 
   309 
   311 /* Last control that had a mnemonic hit i.e. shortcut event*/
   310 /* Last control that had a mnemonic hit i.e. shortcut event*/
   312 Control mnemonicControl;
   311 static Control mnemonicControl;
   313 
   312 
   314 /* It is stored here if we are currently in a focus event */
   313 /* It is stored here if we are currently in a focus event */
   315 int focusEvent;
   314 static int focusEvent;
   316 
   315 
   317 /* Settings event specific variables */
   316 /* Settings event specific variables */
   318 static final int SETTINGS_DELAY = 2000;
   317 static final int SETTINGS_DELAY = 2000;
   319 Runnable settingsRunnable;
   318 static Runnable settingsRunnable;
   320 
       
   321 /*
       
   322 * TEMPORARY CODE.  Install the runnable that
       
   323 * gets the current display. This code will
       
   324 * be removed in the future.
       
   325 */
       
   326 static {
       
   327     DeviceFinder = new Runnable () {
       
   328         public void run () {
       
   329             Device device = getCurrent ();
       
   330             if (device == null) {
       
   331                 device = getDefault ();
       
   332             }
       
   333             setDevice (device);
       
   334         }
       
   335     };
       
   336 }
       
   337 
       
   338 /*
       
   339 * TEMPORARY CODE.
       
   340 */
       
   341 static void setDevice (Device device) {
       
   342     CurrentDevice = device;
       
   343 }
       
   344 
   319 
   345 /**
   320 /**
   346  * Constructs a new instance of this class.
   321  * Constructs a new instance of this class.
   347  * <p>
   322  * <p>
   348  * Note: The resulting display is marked as the <em>current</em>
   323  * Note: The resulting display is marked as the <em>current</em>
   370  *
   345  *
   371  * @param data the device data
   346  * @param data the device data
   372  */
   347  */
   373 public Display (DeviceData data) {
   348 public Display (DeviceData data) {
   374     super(data);
   349     super(data);
       
   350 }
       
   351 
       
   352 private Display(DeviceData data, boolean internal) {
       
   353 	super(data, internal);
       
   354 }
       
   355 
       
   356 static Display internalInstance() {
       
   357 	if(Internal == null) {
       
   358 		Internal = new Display(null, true);
       
   359 	}
       
   360 	return Internal;
   375 }
   361 }
   376 
   362 
   377 /**
   363 /**
   378  * Adds the listener to the collection of listeners who will
   364  * Adds the listener to the collection of listeners who will
   379  * be notified when an event of the given type occurs anywhere
   365  * be notified when an event of the given type occurs anywhere
   446     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
   432     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
   447     if (eventTable == null) eventTable = new EventTable ();
   433     if (eventTable == null) eventTable = new EventTable ();
   448     eventTable.hook (eventType, listener);
   434     eventTable.hook (eventType, listener);
   449 }
   435 }
   450 
   436 
   451 void addWidget (int handle, Widget widget) {
   437 static void addWidget (int handle, Widget widget) {
   452     if (handle == 0) return;
   438     if (handle == 0) return;
   453     if (freeSlot == -1) {
   439     if (freeSlot == -1) {
   454         int length = (freeSlot = indexTable.length) + GROW_SIZE;
   440         int length = (freeSlot = indexTable.length) + GROW_SIZE;
   455         int[] newIndexTable = new int[length];
   441         int[] newIndexTable = new int[length];
   456         Widget[] newWidgetTable = new Widget [length];
   442         Widget[] newWidgetTable = new Widget [length];
   512 public void beep () {
   498 public void beep () {
   513     if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
   499     if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
   514     OS.QApplication_beep();
   500     OS.QApplication_beep();
   515 }
   501 }
   516 
   502 
   517 private void checkDefaultOrientation(int sysLangDirection) {
   503 private static void checkDefaultOrientation(int sysLangDirection) {
   518     defaultOrientation = SWT.LEFT_TO_RIGHT;
   504     defaultOrientation = SWT.LEFT_TO_RIGHT;
   519     String s = System.getProperty("org.eclipse.ercp.swt.defaultOrientation");
   505     String s = System.getProperty("org.eclipse.ercp.swt.defaultOrientation");
   520     if (s != null) {
   506     if (s != null) {
   521         if (s.equals("system")) {
   507         if (s.equals("system")) {
   522             defaultOrientation = sysLangDirection;
   508             defaultOrientation = sysLangDirection;
   531     if (thread == null) error (SWT.ERROR_WIDGET_DISPOSED);
   517     if (thread == null) error (SWT.ERROR_WIDGET_DISPOSED);
   532     if (thread != Thread.currentThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
   518     if (thread != Thread.currentThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
   533     if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
   519     if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
   534 }
   520 }
   535 
   521 
   536 static void checkDisplay (Thread thread, boolean multiple) {
   522 final void checkDisplay (Thread thread, boolean multiple) {
   537     synchronized (Device.class) {
   523     synchronized (Device.class) {
   538         for (int i=0; i<Displays.length; i++) {
   524         for (int i=0; i<Displays.length; i++) {
   539             if (Displays [i] != null) {
   525             if (Displays [i] != null) {
   540                 if (!multiple) SWT.error (SWT.ERROR_NOT_IMPLEMENTED, null, " [multiple displays]");
   526             	if(Internal_GfxPackageSupport.internal(this) == Internal_GfxPackageSupport.internal(Displays[i])) {
   541                 if (Displays [i].thread == thread) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS);
   527             		if (!multiple) SWT.error (SWT.ERROR_NOT_IMPLEMENTED, null, " [multiple displays]");
       
   528                     if (Displays [i].thread == thread) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS);
       
   529             	}
   542             }
   530             }
   543         }
   531         }
   544         // On some platforms it's not supported to dispose the Display and
   532         // On some platforms it's not supported to dispose the Display and
   545         // create another one in a different thread. Once a Display is created
   533         // create another one in a different thread. Once a Display is created
   546         // in a thread that thread becomes the UI thread of the process and no
   534         // in a thread that thread becomes the UI thread of the process and no
   548         if(OS.windowServer == OS.WS_SYMBIAN_S60 && fixedUiThread == null) fixedUiThread = thread;
   536         if(OS.windowServer == OS.WS_SYMBIAN_S60 && fixedUiThread == null) fixedUiThread = thread;
   549         if (fixedUiThread != null && !fixedUiThread.equals(thread)) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS);
   537         if (fixedUiThread != null && !fixedUiThread.equals(thread)) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS);
   550     }
   538     }
   551 }
   539 }
   552 
   540 
   553 void checkSubclass () {
   541 final void checkSubclass () {
   554     if (!isValidClass (getClass ())) error (SWT.ERROR_INVALID_SUBCLASS);
   542     if (!isValidClass (getClass ())) error (SWT.ERROR_INVALID_SUBCLASS);
   555 }
   543 }
   556 
   544 
   557 void clearModal (Shell shell) {
   545 final void clearModal (Shell shell) {
   558     if (modalShells == null) return;
   546     if (modalShells == null) return;
   559     int index = 0, length = modalShells.length;
   547     int index = 0, length = modalShells.length;
   560     while (index < length) {
   548     while (index < length) {
   561         if (modalShells [index] == shell) break;
   549         if (modalShells [index] == shell) break;
   562         if (modalShells [index] == null) return;
   550         if (modalShells [index] == null) return;
   588     Event event = new Event ();
   576     Event event = new Event ();
   589     sendEvent (SWT.Close, event);
   577     sendEvent (SWT.Close, event);
   590     if (event.doit) dispose ();
   578     if (event.doit) dispose ();
   591 }
   579 }
   592 
   580 
   593 void createDisplay() {
   581 static void createDisplay() {
   594     // It's possible that there's already a QApplication from an old Display
   582     // It's possible that there's already a QApplication from an old Display
   595     // that has been disposed in an event listener but we were unable to delete
   583     // that has been disposed in an event listener but we were unable to delete
   596     // the QApplication there. If we are creating this Display from an event
   584     // the QApplication there. If we are creating this Display from an event
   597     // listener too then this won't help.
   585     // listener too then this won't help.
   598     tryDestroy();
   586     tryDestroy();
   601     // Perform the required initialisation before Qt APIs can be used.
   589     // Perform the required initialisation before Qt APIs can be used.
   602     if(OS.initUiThread(ApplicationUidWrapper.uid()) != 0) SWT.error(SWT.ERROR_UNSPECIFIED);
   590     if(OS.initUiThread(ApplicationUidWrapper.uid()) != 0) SWT.error(SWT.ERROR_UNSPECIFIED);
   603 
   591 
   604     // Create the object that manages the JNI activity. It's only possible when
   592     // Create the object that manages the JNI activity. It's only possible when
   605     // this object exists.
   593     // this object exists.
   606     JniUtils.handle = OS.JniUtils_new(this);
   594     JniUtils.handle = OS.JniUtils_new();
   607 
   595 
   608     handle = OS.QApplication_swt_new(JniUtils.handle);
   596     handle = OS.QApplication_swt_new(JniUtils.handle);
   609     if(handle == 0) SWT.error(SWT.ERROR_UNSPECIFIED);
   597     if(handle == 0) SWT.error(SWT.ERROR_UNSPECIFIED);
   610     OS.QApplication_setQuitOnLastWindowClosed(false);
   598     OS.QApplication_setQuitOnLastWindowClosed(false);
   611 
   599 
   643  * </p>
   631  * </p>
   644  * @see Device#dispose
   632  * @see Device#dispose
   645  * @see #release
   633  * @see #release
   646  */
   634  */
   647 protected void destroy () {
   635 protected void destroy () {
   648     if (this == Default) Default = null;
   636     boolean notifyExit;
       
   637 	if (notifyExit = (this == Default)) {
       
   638     	Default = null;    	
       
   639     } else if(this == Internal) {
       
   640     	Internal = null;
       
   641     }
   649     deregister (this);
   642     deregister (this);
   650     GraphicsContext sharedGc = SharedGCWrapper.getSharedGC();
   643     if(Default == null && Internal == null) {
   651     if(sharedGc != null && !sharedGc.isDisposed()) sharedGc.dispose();
   644 	    GraphicsContext sharedGc = SharedGCWrapper.getSharedGC();
   652     destroyDisplay ();
   645 	    if(sharedGc != null && !sharedGc.isDisposed()) sharedGc.dispose();
       
   646 	    destroyDisplay ();
       
   647 	    super.destroy();
       
   648     }
       
   649     if(notifyExit) {
       
   650         // Notify MIDP runtime to watch that the MIDlet exits. 
       
   651         // For other runtime environments this does nothing. 
       
   652         ExitNotificationWrapper.notifyExit();
       
   653     }
   653 }
   654 }
   654 
   655 
   655 void destroyDisplay () {
   656 void destroyDisplay () {
   656     OS.QObject_removeEventFilter(handle, eventFilterHandle);
   657     OS.QObject_removeEventFilter(handle, eventFilterHandle);
   657     OS.EventHandler_destroy(eventFilterHandle);
   658     OS.EventHandler_destroy(eventFilterHandle);
   658     OS.QApplication_syncX();
   659     OS.QApplication_syncX();
   659     tryDestroy();
   660     tryDestroy();
   660 
   661 }
   661     // Notify MIDP runtime to watch that the MIDlet exits. 
   662 
   662     // For other runtime environments this does nothing. 
   663 int displayCount() {
   663     ExitNotificationWrapper.notifyExit();
   664     int res = 0;
       
   665     for (int i = 0; i < Displays.length; i++) {
       
   666         if (Displays[i] != null && !Displays[i].isDisposed()) {
       
   667             res++;
       
   668         }
       
   669     }
       
   670     return res;
   664 }
   671 }
   665 
   672 
   666 /**
   673 /**
   667  * Causes the <code>run()</code> method of the runnable to
   674  * Causes the <code>run()</code> method of the runnable to
   668  * be invoked by the user-interface thread just before the
   675  * be invoked by the user-interface thread just before the
   704 }
   711 }
   705 
   712 
   706 /*
   713 /*
   707  * This is the main event handler that is called by native code.
   714  * This is the main event handler that is called by native code.
   708  */
   715  */
   709 final boolean eventProcess(int widgetHandle, int eventType, int time,
   716 static boolean eventProcess(int widgetHandle, int eventType, int time,
   710         int arg1, int arg2, int arg3, int arg4, int arg5, String arg6) {
   717         int arg1, int arg2, int arg3, int arg4, int arg5, String arg6) {
   711     // Silently let blocked events through
   718     // Silently let blocked events through
   712     if(blockedQtEventType == eventType) return false;
   719     if(blockedQtEventType == eventType) return false;
   713 
   720 
   714     // This goes to time field of the events that possibly get sent
   721     // This goes to time field of the events that possibly get sent
   715     lastEventTime = time;
   722     lastEventTime = time;
   716 
   723 
   717     // Get target widget or null if not found or is the Display
   724     // Get target widget or null if not found or is the Display
   718     Widget widget = ((widgetHandle == handle) ? null : getWidget ( widgetHandle ));
   725     Widget widget = ((widgetHandle == handle) ? null : getWidget ( widgetHandle ));
   719 
   726 
   720     if(!noInterrupt && !isDisposed()) {
   727     if(!noInterrupt) {
   721         interrupt();
   728         interrupt();
   722     }
   729     }
   723     
   730     
   724     if (eventType < OS.QSIGNAL_ID_RANGE_FIRST) {
   731     if (eventType < OS.QSIGNAL_ID_RANGE_FIRST) {
   725             return handleQtEvent(widget, widgetHandle, eventType, arg1, arg2,
   732             return handleQtEvent(widget, widgetHandle, eventType, arg1, arg2,
   731         return handleQtSignal(widget, widgetHandle, eventType, arg1, arg2,
   738         return handleQtSignal(widget, widgetHandle, eventType, arg1, arg2,
   732                 arg3, arg4, arg5, arg6);
   739                 arg3, arg4, arg5, arg6);
   733     }
   740     }
   734 }
   741 }
   735 
   742 
   736 boolean filterEvent (Event event) {
   743 static boolean filterEvent (Event event) {
   737     if (filterTable != null) filterTable.sendEvent (event);
   744     if (filterTable != null) filterTable.sendEvent (event);
   738     return false;
   745     return false;
   739 }
   746 }
   740 
   747 
   741 boolean filters (int eventType) {
   748 static boolean filters (int eventType) {
   742     if (filterTable == null) return false;
   749     if (filterTable == null) return false;
   743     return filterTable.hooks (eventType);
   750     return filterTable.hooks (eventType);
   744 }
   751 }
   745 
   752 
   746 Control findControl (int handle) {
   753 static Control findControl (int handle) {
   747     if (handle == 0) return null;
   754     if (handle == 0) return null;
   748     do {
   755     do {
   749         Widget widget = getWidget (handle);
   756         Widget widget = getWidget (handle);
   750         if(widget != null && widget instanceof Control) return (Control)widget;
   757         if(widget != null && widget instanceof Control) return (Control)widget;
   751         handle = OS.QObject_parent(handle);
   758         handle = OS.QObject_parent(handle);
   765  */
   772  */
   766 public static Display findDisplay (Thread thread) {
   773 public static Display findDisplay (Thread thread) {
   767     synchronized (Device.class) {
   774     synchronized (Device.class) {
   768         for (int i=0; i<Displays.length; i++) {
   775         for (int i=0; i<Displays.length; i++) {
   769             Display display = Displays [i];
   776             Display display = Displays [i];
   770             if (display != null && display.thread == thread) {
   777             if (display != null && display.thread == thread && !Internal_GfxPackageSupport.internal(display)) {
   771                 return display;
   778                 return display;
   772             }
   779             }
   773         }
   780         }
   774         return null;
   781         return null;
   775     }
   782     }
   834     // Ignoring base implementation
   841     // Ignoring base implementation
   835     checkDevice ();
   842     checkDevice ();
   836     return OS.QDesktopWidget_availableGeometry(OS.QApplication_desktop(), -1);
   843     return OS.QDesktopWidget_availableGeometry(OS.QApplication_desktop(), -1);
   837 }
   844 }
   838 
   845 
   839 Control[] getControls()
   846 static Control[] getControls()
   840 {
   847 {
   841     // Create a table of all Controls, no duplicates
   848     // Create a table of all Controls, no duplicates
   842     Control controlTable[] = new Control[widgetTable.length];
   849     Control controlTable[] = new Control[widgetTable.length];
   843     int controlTableIndex = 0;
   850     int controlTableIndex = 0;
   844     for(int i = 0; i < widgetTable.length; i++) {
   851     for(int i = 0; i < widgetTable.length; i++) {
  1005 public Control getFocusControl () {
  1012 public Control getFocusControl () {
  1006     checkDevice ();
  1013     checkDevice ();
  1007     return _getFocusControl ();
  1014     return _getFocusControl ();
  1008 }
  1015 }
  1009 
  1016 
  1010 Control _getFocusControl () {
  1017 static Control _getFocusControl () {
  1011     return findControl( OS.QApplication_focusWidget() );
  1018     return findControl( OS.QApplication_focusWidget() );
  1012 }
  1019 }
  1013 
  1020 
  1014 /**
  1021 /**
  1015  * Returns the maximum allowed depth of icons on this display, in bits per pixel.
  1022  * Returns the maximum allowed depth of icons on this display, in bits per pixel.
  1027 public int getIconDepth () {
  1034 public int getIconDepth () {
  1028     checkDevice ();
  1035     checkDevice ();
  1029     return super.getDepth();
  1036     return super.getDepth();
  1030 }
  1037 }
  1031 
  1038 
  1032 int getLastEventTime () {
  1039 static int getLastEventTime () {
  1033     return lastEventTime;
  1040     return lastEventTime;
  1034 }
  1041 }
  1035 
  1042 
  1036 int getMessageCount () {
  1043 static int getMessageCount () {
  1037     return synchronizer.getMessageCount ();
  1044     return synchronizer.getMessageCount ();
  1038 }
  1045 }
  1039 
  1046 
  1040 Dialog getModalDialog () {
  1047 static Dialog getModalDialog () {
  1041     return modalDialog;
  1048     return modalDialog;
  1042 }
  1049 }
  1043 
  1050 
  1044 Menu[] getMenus()
  1051 static Menu[] getMenus()
  1045 {
  1052 {
  1046     // Create a table of all Menus, no duplicates
  1053     // Create a table of all Menus, no duplicates
  1047     Menu menuTable[] = new Menu[widgetTable.length];
  1054     Menu menuTable[] = new Menu[widgetTable.length];
  1048     int menuTableIndex = 0;
  1055     int menuTableIndex = 0;
  1049     for(int i = 0; i < widgetTable.length; i++) {
  1056     for(int i = 0; i < widgetTable.length; i++) {
  1078  *    <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
  1085  *    <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
  1079  * </ul>
  1086  * </ul>
  1080  */
  1087  */
  1081 public Shell [] getShells () {
  1088 public Shell [] getShells () {
  1082     checkDevice ();
  1089     checkDevice ();
       
  1090     return getShells_();
       
  1091 }
       
  1092 
       
  1093 static Shell[] getShells_() {
  1083     int index = 0;
  1094     int index = 0;
  1084     Shell [] result = new Shell [16];
  1095     Shell [] result = new Shell [16];
  1085     for (int i = 0; i < widgetTable.length; i++) {
  1096     for (int i = 0; i < widgetTable.length; i++) {
  1086         Widget widget = widgetTable [i];
  1097         Widget widget = widgetTable [i];
  1087         if (widget != null && widget instanceof Shell) {
  1098         if (widget != null && widget instanceof Shell) {
  1101         }
  1112         }
  1102     }
  1113     }
  1103     if (index == result.length) return result;
  1114     if (index == result.length) return result;
  1104     Shell [] newResult = new Shell [index];
  1115     Shell [] newResult = new Shell [index];
  1105     System.arraycopy (result, 0, newResult, 0, index);
  1116     System.arraycopy (result, 0, newResult, 0, index);
  1106     return newResult;
  1117     return newResult;	
  1107 }
  1118 }
  1108 
  1119 
  1109 /**
  1120 /**
  1110  * Returns the thread that has invoked <code>syncExec</code>
  1121  * Returns the thread that has invoked <code>syncExec</code>
  1111  * or null if no such runnable is currently being invoked by
  1122  * or null if no such runnable is currently being invoked by
  1161         if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
  1172         if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
  1162         return thread;
  1173         return thread;
  1163     }
  1174     }
  1164 }
  1175 }
  1165 
  1176 
  1166 private final int getTimerHandleIndex(int timerHandle) {
  1177 private static int getTimerHandleIndex(int timerHandle) {
  1167     if(timerHandles == null) return -1;
  1178     if(timerHandles == null) return -1;
  1168     int index;
  1179     int index;
  1169     for(index = 0; index < timerHandles.length; ++index) {
  1180     for(index = 0; index < timerHandles.length; ++index) {
  1170         if(timerHandles[index] == timerHandle) {
  1181         if(timerHandles[index] == timerHandle) {
  1171             return index;
  1182             return index;
  1172         }
  1183         }
  1173     }
  1184     }
  1174     return -1;
  1185     return -1;
  1175 }
  1186 }
  1176 
  1187 
  1177 Widget getWidget (int handle) {
  1188 static Widget getWidget (int handle) {
  1178     if (handle == 0) return null;
  1189     if (handle == 0) return null;
  1179     if (lastWidget != null && lastHandle == handle) return lastWidget;
  1190     if (lastWidget != null && lastHandle == handle) return lastWidget;
  1180     int index = OS.QObject_property( handle, SWT_OBJECT_INDEX) - 1;
  1191     int index = OS.QObject_property( handle, SWT_OBJECT_INDEX) - 1;
  1181     if (0 <= index && index < widgetTable.length) {
  1192     if (0 <= index && index < widgetTable.length) {
  1182         lastHandle = handle;
  1193         lastHandle = handle;
  1183         return lastWidget = widgetTable [(int)index];
  1194         return lastWidget = widgetTable [(int)index];
  1184     }
  1195     }
  1185     return null;
  1196     return null;
  1186 }
  1197 }
  1187 
  1198 
  1188 final private boolean handleNativeSwtEvent(Widget widget, int widgetHandle,
  1199 static private boolean handleNativeSwtEvent(Widget widget, int widgetHandle,
  1189         int eventType, int time, int arg1, int arg2, int arg3, int arg4,
  1200         int eventType, int time, int arg1, int arg2, int arg3, int arg4,
  1190         int arg5) {
  1201         int arg5) {
  1191 
  1202 
  1192     switch (eventType) {
  1203     switch (eventType) {
  1193     case OS.QSWTEVENT_SYSTEMSHUTDOWN:
  1204     case OS.QSWTEVENT_SYSTEMSHUTDOWN:
  1248     default:
  1259     default:
  1249         return false;
  1260         return false;
  1250     }
  1261     }
  1251 }
  1262 }
  1252 
  1263 
  1253 final private boolean handleQtEvent(Widget widget, int widgetHandle, int eventType,
  1264 static private boolean handleQtEvent(Widget widget, int widgetHandle, int eventType,
  1254         int arg1, int arg2, int arg3, int arg4, int arg5) {
  1265         int arg1, int arg2, int arg3, int arg4, int arg5) {
  1255 
  1266 
  1256     // These events are coming from an application event filter of QApplication.
  1267     // These events are coming from an application event filter of QApplication.
  1257     // The return value of this method will be returned by the filter.
  1268     // The return value of this method will be returned by the filter.
       
  1269 	
  1258     if (eventType == OS.QEVENT_KEYPRESS && arg1 == OS.QT_KEY_NO) {
  1270     if (eventType == OS.QEVENT_KEYPRESS && arg1 == OS.QT_KEY_NO) {
  1259         qt_key_event_endKeyPressed();
  1271     	qt_key_event_endKeyPressed();
  1260         return false;
  1272         return false;
  1261     }
  1273     }
       
  1274     
  1262     if (widget == null) {
  1275     if (widget == null) {
  1263         return false;
  1276         return false;
  1264     }
  1277     }
  1265 
  1278 
  1266     switch (eventType) {
  1279     switch (eventType) {
  1329     default:
  1342     default:
  1330         return false;
  1343         return false;
  1331     }
  1344     }
  1332 }
  1345 }
  1333 
  1346 
  1334 final private boolean handleQtSignal(Widget widget, int widgetHandle,
  1347 static private boolean handleQtSignal(Widget widget, int widgetHandle,
  1335         int eventType, int arg1, int arg2, int arg3, int arg4, int arg5,
  1348         int eventType, int arg1, int arg2, int arg3, int arg4, int arg5,
  1336         String arg6) {
  1349         String arg6) {
  1337 
  1350 
  1338     // Note that return values here are meaningless. Signals are already
  1351     // Note that return values here are meaningless. Signals are already
  1339     // coming from the widgets and won't be passed to anywhere after this.
  1352     // coming from the widgets and won't be passed to anywhere after this.
  1523     default:
  1536     default:
  1524         return false;
  1537         return false;
  1525     }
  1538     }
  1526 }
  1539 }
  1527 
  1540 
  1528 void hookEvents() {
  1541 static void hookEvents() {
  1529     int focusSignalProxy = OS.SignalHandler_new(handle, this, OS.QSIGNAL_QAPPLICATION_FOCUSCHANGED);
  1542     int focusSignalProxy = OS.SignalHandler_new(handle, OS.QSIGNAL_QAPPLICATION_FOCUSCHANGED);
  1530     OS.QObject_connectOrThrow(handle, "focusChanged(QWidget*, QWidget*)", focusSignalProxy,
  1543     OS.QObject_connectOrThrow(handle, "focusChanged(QWidget*, QWidget*)", focusSignalProxy,
  1531             "widgetSignal(QWidget*, QWidget*)", OS.QT_AUTOCONNECTION);
  1544             "widgetSignal(QWidget*, QWidget*)", OS.QT_AUTOCONNECTION);
  1532 }
  1545 }
  1533 
  1546 
  1534 protected void init () {
  1547 protected void init () {
  1535     checkSubclass ();
  1548     checkSubclass ();
  1536     checkDisplay(thread = Thread.currentThread (), false);
  1549     checkDisplay(thread = Thread.currentThread (), false);
  1537     createDisplay();
  1550     if(!Internal_GfxPackageSupport.initialized()) {
  1538     commandArranger = new CommandArranger();
  1551     	createDisplay();
       
  1552 	    synchronizer = new Synchronizer (this);
       
  1553 	    commandArranger = new CommandArranger(this);
       
  1554     }
  1539     register (this);
  1555     register (this);
  1540     if (Default == null) Default = this;
  1556     if (Default == null && !Internal_GfxPackageSupport.internal(this)) Default = this;
  1541     
  1557     
  1542     super.init ();
  1558     super.init ();
  1543     initializeWidgetTable ();
  1559     if(!Internal_GfxPackageSupport.initialized()) {
  1544 }
  1560     	initializeWidgetTable ();
  1545 
  1561     }
  1546 void initializeWidgetTable () {
  1562 }
       
  1563 
       
  1564 static void initializeWidgetTable () {
  1547     indexTable = new int [GROW_SIZE];
  1565     indexTable = new int [GROW_SIZE];
  1548     widgetTable = new Widget [GROW_SIZE];
  1566     widgetTable = new Widget [GROW_SIZE];
  1549     for (int i=0; i<GROW_SIZE-1; i++) indexTable [i] = i + 1;
  1567     for (int i=0; i<GROW_SIZE-1; i++) indexTable [i] = i + 1;
  1550     indexTable [GROW_SIZE - 1] = -1;
  1568     indexTable [GROW_SIZE - 1] = -1;
  1551 }
  1569 }
  1605     data.buffered = false;
  1623     data.buffered = false;
  1606     data.internalGc.bindTarget(this);
  1624     data.internalGc.bindTarget(this);
  1607     return data.internalGc.getHandle();
  1625     return data.internalGc.getHandle();
  1608 }
  1626 }
  1609 
  1627 
  1610 void interrupt() {
  1628 static void interrupt() {
  1611     /*
  1629     /*
  1612      * Try to interrupt to make the native event processing methods return. By
  1630      * Try to interrupt to make the native event processing methods return. By
  1613      * default Qt processes all available events in one iteration. This is
  1631      * default Qt processes all available events in one iteration. This is
  1614      * a problem e.g. when an exception has been thrown in a listener. It
  1632      * a problem e.g. when an exception has been thrown in a listener. It
  1615      * depends on the event dispatcher implementation what this actually does.
  1633      * depends on the event dispatcher implementation what this actually does.
  1616      * It might be that interrupting is only possible between native UI main
  1634      * It might be that interrupting is only possible between native UI main
  1617      * event loop iterations and in each iteration several QEvents can get
  1635      * event loop iterations and in each iteration several QEvents can get
  1618      * dispatched.
  1636      * dispatched.
  1619      */
  1637      */
  1620     int threadHandle = OS.QObject_thread( Display.handle );
  1638 	synchronized(Device.class) {
  1621     int dispatcher = OS.QAbstractEventDispatcher_instance( threadHandle );
  1639 		if(handle == 0) return;
  1622     OS.QAbstractEventDispatcher_interrupt( dispatcher );
  1640 	}
       
  1641     int threadHandle = OS.QObject_thread(handle);
       
  1642     int dispatcher = OS.QAbstractEventDispatcher_instance(threadHandle);
       
  1643     OS.QAbstractEventDispatcher_interrupt(dispatcher);
  1623 }
  1644 }
  1624 
  1645 
  1625 static boolean isValidClass (Class clazz) {
  1646 static boolean isValidClass (Class clazz) {
  1626     String name = clazz.getName();
  1647     String name = clazz.getName();
  1627         int index = name.lastIndexOf('.');
  1648         int index = name.lastIndexOf('.');
  1970     }
  1991     }
  1971 
  1992 
  1972     return false;
  1993     return false;
  1973 }
  1994 }
  1974 
  1995 
  1975 void postEvent (Event event) {
  1996 static void postEvent (Event event) {
  1976     /*
  1997     /*
  1977     * Place the event at the end of the event queue.
  1998     * Place the event at the end of the event queue.
  1978     * This code is always called in the Display's
  1999     * This code is always called in the Display's
  1979     * thread so it must be re-enterant but does not
  2000     * thread so it must be re-enterant but does not
  1980     * need to be synchronized.
  2001     * need to be synchronized.
  1992         eventQueue = newQueue;
  2013         eventQueue = newQueue;
  1993     }
  2014     }
  1994     eventQueue [index] = event;
  2015     eventQueue [index] = event;
  1995 }
  2016 }
  1996 
  2017 
  1997 void qt_signal_qapplication_focusChanged(int old, int now) {
  2018 static void qt_signal_qapplication_focusChanged(int old, int now) {
  1998     Widget oldWidget = null, nowWidget = null;
  2019     Widget oldWidget = null, nowWidget = null;
  1999 
  2020 
  2000     if(old != 0) oldWidget = getWidget(old);
  2021     if(old != 0) oldWidget = getWidget(old);
  2001     if(now != 0) nowWidget = getWidget(now);
  2022     if(now != 0) nowWidget = getWidget(now);
  2002 
  2023 
  2013 
  2034 
  2014         nowWidget.qt_swt_event_focusWasGained();
  2035         nowWidget.qt_swt_event_focusWasGained();
  2015     }
  2036     }
  2016 }
  2037 }
  2017 
  2038 
  2018 void qt_signal_timer (int timerHandleIndex) {
  2039 static void qt_signal_timer (int timerHandleIndex) {
  2019     if (timerRunnables == null || timerHandles == null) return;
  2040     if (timerRunnables == null || timerHandles == null) return;
  2020 
  2041 
  2021     Runnable runnable = timerRunnables [timerHandleIndex];
  2042     Runnable runnable = timerRunnables [timerHandleIndex];
  2022     timerRunnables [timerHandleIndex] = null;
  2043     timerRunnables [timerHandleIndex] = null;
  2023 
  2044 
  2029     timerHandles [timerHandleIndex] = 0;
  2050     timerHandles [timerHandleIndex] = 0;
  2030 
  2051 
  2031     if (runnable != null) runnable.run ();
  2052     if (runnable != null) runnable.run ();
  2032 }
  2053 }
  2033 
  2054 
  2034 
  2055 static void qt_key_event_endKeyPressed() {
  2035 void qt_key_event_endKeyPressed(){
  2056     Event event = new Event ();
  2036     close();
  2057     sendEvent (SWT.Close, event);
  2037 }
  2058     if (event.doit) {
  2038 
  2059     	if(Default != null) {
  2039 
  2060     		Default.dispose ();
  2040 void qt_swt_event_systemShutdown(){
  2061     	}
       
  2062     }
       
  2063 }
       
  2064 
       
  2065 static void qt_swt_event_systemShutdown(){
  2041     Event event = new Event ();
  2066     Event event = new Event ();
  2042     sendEvent (SWT.Close, event);
  2067     sendEvent (SWT.Close, event);
  2043     ExitNotificationWrapper.notifyExit();
  2068     ExitNotificationWrapper.notifyExit();
  2044 }
  2069 }
  2045 
       
  2046 
  2070 
  2047 /**
  2071 /**
  2048  * Reads an event from the operating system's event queue,
  2072  * Reads an event from the operating system's event queue,
  2049  * dispatches it appropriately, and returns <code>true</code>
  2073  * dispatches it appropriately, and returns <code>true</code>
  2050  * if there is potentially more work to do, or <code>false</code>
  2074  * if there is potentially more work to do, or <code>false</code>
  2089         return false;
  2113         return false;
  2090     }
  2114     }
  2091     return asyncRetVal;
  2115     return asyncRetVal;
  2092 }
  2116 }
  2093 
  2117 
  2094 Point readAndClearImageLoadSize() {
  2118 static Point readAndClearImageLoadSize() {
  2095 	Point imageLoadSize = this.imageLoadSize;
  2119 	Point ilSize = Display.imageLoadSize;
  2096 	this.imageLoadSize = null;
  2120 	Display.imageLoadSize = null;
  2097 	return imageLoadSize;
  2121 	return ilSize;
  2098 }
  2122 }
  2099 
  2123 
  2100 static void register (Display display) {
  2124 static void register (Display display) {
  2101     synchronized (Device.class) {
  2125     synchronized (Device.class) {
  2102         for (int i=0; i<Displays.length; i++) {
  2126         for (int i=0; i<Displays.length; i++) {
  2141     Shell [] shells = getShells ();
  2165     Shell [] shells = getShells ();
  2142     for (int i=0; i<shells.length; i++) {
  2166     for (int i=0; i<shells.length; i++) {
  2143         Shell shell = shells [i];
  2167         Shell shell = shells [i];
  2144         if (!shell.isDisposed ())  shell.dispose ();
  2168         if (!shell.isDisposed ())  shell.dispose ();
  2145     }
  2169     }
  2146     commandArranger.dispose();
  2170     final boolean lastDisplay = (displayCount() == 1);
  2147     if (tray != null) tray.dispose ();
  2171     if(lastDisplay) {
  2148     tray = null;
  2172 	    commandArranger.dispose();
       
  2173 	    if (tray != null) tray.dispose ();
       
  2174 	    tray = null;
       
  2175     }
  2149     while (readAndDispatch ()) {}
  2176     while (readAndDispatch ()) {}
  2150     if (disposeList != null) {
  2177     if (disposeList != null) {
  2151         for (int i=0; i<disposeList.length; i++) {
  2178         for (int i=0; i<disposeList.length; i++) {
  2152             if (disposeList [i] != null) disposeList [i].run ();
  2179             if (disposeList [i] != null) disposeList [i].run ();
  2153         }
  2180         }
  2154     }
  2181     }
       
  2182     if(lastDisplay) {
       
  2183 	    synchronizer.releaseSynchronizer ();
       
  2184 	    synchronizer = null;
       
  2185 	    lastWidget = null;
       
  2186 	    indexTable = null;
       
  2187 	    widgetTable = null;
       
  2188     }
       
  2189     thread = null;
  2155     disposeList = null;
  2190     disposeList = null;
  2156     synchronizer.releaseSynchronizer ();
       
  2157     synchronizer = null;
       
  2158     thread = null;
       
  2159     lastWidget = null;
       
  2160     indexTable = null;
       
  2161     widgetTable = null;
       
  2162     super.release ();
  2191     super.release ();
  2163 }
  2192 }
  2164 
  2193 
  2165 /**
  2194 /**
  2166  * Removes the listener from the collection of listeners who will
  2195  * Removes the listener from the collection of listeners who will
  2220     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
  2249     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
  2221     if (eventTable == null) return;
  2250     if (eventTable == null) return;
  2222     eventTable.unhook (eventType, listener);
  2251     eventTable.unhook (eventType, listener);
  2223 }
  2252 }
  2224 
  2253 
  2225 Widget removeWidget (int handle) {
  2254 static Widget removeWidget (int handle) {
  2226     if (handle == 0) return null;
  2255     if (handle == 0) return null;
  2227     lastWidget = null;
  2256     lastWidget = null;
  2228     Widget widget = null;
  2257     Widget widget = null;
  2229     int index = (int)/*64*/ OS.QObject_property( handle, SWT_OBJECT_INDEX ) - 1;
  2258     int index = (int)/*64*/ OS.QObject_property( handle, SWT_OBJECT_INDEX ) - 1;
  2230     if (0 <= index && index < widgetTable.length) {
  2259     if (0 <= index && index < widgetTable.length) {
  2246 final private boolean repostFailedQEvents() {
  2275 final private boolean repostFailedQEvents() {
  2247     OS.SwtApplication_postDeferredEvents();
  2276     OS.SwtApplication_postDeferredEvents();
  2248     return disposed;
  2277     return disposed;
  2249 }
  2278 }
  2250 
  2279 
  2251 boolean runAsyncMessages (boolean all) {
  2280 static boolean runAsyncMessages (boolean all) {
  2252     return synchronizer.runAsyncMessages (all);
  2281     return synchronizer.runAsyncMessages (all);
  2253 }
  2282 }
  2254 
  2283 
  2255 boolean runDeferredEvents () {
  2284 static boolean runDeferredEvents () {
  2256     /*
  2285     /*
  2257     * Run deferred events.  This code is always
  2286     * Run deferred events.  This code is always
  2258     * called in the Display's thread so it must
  2287     * called in the Display's thread so it must
  2259     * be re-entrant but need not be synchronized.
  2288     * be re-entrant but need not be synchronized.
  2260     */
  2289     */
  2286     /* Clear the queue */
  2315     /* Clear the queue */
  2287     eventQueue = null;
  2316     eventQueue = null;
  2288     return true;
  2317     return true;
  2289 }
  2318 }
  2290 
  2319 
  2291 void runSettings () {
  2320 static void runSettings () {
  2292     Control[] controls = getControls();
  2321     Control[] controls = getControls();
  2293     updateImages(controls);
  2322     updateImages(controls);
  2294     updateLayoutDirections(controls);
  2323     updateLayoutDirections(controls);
  2295     sendEvent(SWT.Settings, null);
  2324     sendEvent(SWT.Settings, null);
  2296     for(int i = 0; i < controls.length; i++) {
  2325     for(int i = 0; i < controls.length; i++) {
  2297         controls[i].doRedraw();
  2326         controls[i].doRedraw();
  2298     }
  2327     }
  2299     Shell [] shells = getShells ();
  2328     Shell [] shells = getShells_();
  2300     for(int i = 0; i < shells.length; i++) {
  2329     for(int i = 0; i < shells.length; i++) {
  2301         Shell shell = shells [i];
  2330         Shell shell = shells [i];
  2302         if(!shell.isDisposed ()) {
  2331         if(!shell.isDisposed ()) {
  2303             shell.layout(true, true);
  2332             shell.layout(true, true);
  2304         }
  2333         }
  2314 final private boolean sendDeferredDeletionQEvents() {
  2343 final private boolean sendDeferredDeletionQEvents() {
  2315     OS.QCoreApplication_sendPostedEvents(handle, 0, OS.QEVENT_DEFERREDDELETE);
  2344     OS.QCoreApplication_sendPostedEvents(handle, 0, OS.QEVENT_DEFERREDDELETE);
  2316     return disposed;
  2345     return disposed;
  2317 }
  2346 }
  2318 
  2347 
  2319 void sendEvent (int eventType, Event event) {
  2348 static void sendEvent (int eventType, Event event) {
  2320     if (eventTable == null && filterTable == null) {
  2349     if (eventTable == null && filterTable == null) {
  2321         return;
  2350         return;
  2322     }
  2351     }
  2323     if (event == null) event = new Event ();
  2352     if (event == null) event = new Event ();
  2324     event.display = this;
  2353     event.display = Default;
  2325     event.type = eventType;
  2354     event.type = eventType;
  2326     if (event.time == 0) event.time = getLastEventTime ();
  2355     if (event.time == 0) event.time = getLastEventTime ();
  2327     if (!filterEvent (event)) {
  2356     if (!filterEvent (event)) {
  2328         if (eventTable != null) eventTable.sendEvent (event);
  2357         if (eventTable != null) eventTable.sendEvent (event);
  2329     }
  2358     }
  2348 final private boolean sendQEvents() {
  2377 final private boolean sendQEvents() {
  2349     OS.QCoreApplication_processEvents( handle, OS.QEVENTLOOP_ALLEVENTS);
  2378     OS.QCoreApplication_processEvents( handle, OS.QEVENTLOOP_ALLEVENTS);
  2350     return disposed;
  2379     return disposed;
  2351 }
  2380 }
  2352 
  2381 
  2353 final private void sendSettings()
  2382 static private void sendSettings() {
  2354 {
  2383     Display instance = Default != null ? Default : Internal;
  2355     if(settingsRunnable != null) {
  2384 	if(settingsRunnable != null) {
  2356         // Cancel the existing timer
  2385         // Cancel the existing timer
  2357         timerExec(-1, settingsRunnable);
  2386         instance.timerExec(-1, settingsRunnable);
  2358     }
  2387     }
  2359     timerExec(SETTINGS_DELAY, settingsRunnable = new Runnable() {
  2388     instance.timerExec(SETTINGS_DELAY, settingsRunnable = new Runnable() {
  2360         public void run() {
  2389         public void run() {
  2361             runSettings();
  2390             runSettings();
  2362             settingsRunnable = null;
  2391             settingsRunnable = null;
  2363         }
  2392         }
  2364     });
  2393     });
  2372  *
  2401  *
  2373  * @param name the new app name or <code>null</code>
  2402  * @param name the new app name or <code>null</code>
  2374  */
  2403  */
  2375 public static void setAppName (String name) {
  2404 public static void setAppName (String name) {
  2376     APP_NAME = name;
  2405     APP_NAME = name;
  2377     if (Display.getCurrent() != null) {
  2406     if (Display.handle != 0 && (!Default.disposed || !Internal.disposed)) {
  2378         OS.QCoreApplication_setApplicationName(handle, name);
  2407         OS.QCoreApplication_setApplicationName(handle, name);
  2379         OS.setSymbianAppName(name);
  2408         OS.setSymbianAppName(name);
  2380     }
  2409     }
  2381 }
  2410 }
  2382 
  2411 
  2498  * @see #getData()
  2527  * @see #getData()
  2499  * @see #disposeExec(Runnable)
  2528  * @see #disposeExec(Runnable)
  2500  */
  2529  */
  2501 public void setData (Object data) {
  2530 public void setData (Object data) {
  2502     checkDevice ();
  2531     checkDevice ();
  2503     this.data = data;
  2532     Display.data = data;
  2504 }
  2533 }
  2505 
  2534 
  2506 void setScreenPaintingAttributes(boolean on) {
  2535 static void setScreenPaintingAttributes(boolean on) {
  2507     OS.QWidget_setAttribute(OS.QApplication_desktop(), OS.QT_WA_PAINTOUTSIDEPAINTEVENT, on);
  2536     OS.QWidget_setAttribute(OS.QApplication_desktop(), OS.QT_WA_PAINTOUTSIDEPAINTEVENT, on);
  2508     OS.QWidget_setAttribute(OS.QApplication_desktop(), OS.QT_WA_PAINTONSCREEN, on);
  2537     OS.QWidget_setAttribute(OS.QApplication_desktop(), OS.QT_WA_PAINTONSCREEN, on);
  2509     OS.QWidget_setAttribute(OS.QApplication_desktop(), OS.QT_WA_PAINTUNCLIPPED, on);
  2538     OS.QWidget_setAttribute(OS.QApplication_desktop(), OS.QT_WA_PAINTUNCLIPPED, on);
  2510 }
  2539 }
  2511 
  2540 
  2512 void setModalDialog (Dialog modalDialog) {
  2541 static void setModalDialog (Dialog modalDialog) {
  2513     this.modalDialog = modalDialog;
  2542     Display.modalDialog = modalDialog;
  2514     Shell [] shells = getShells ();
  2543     Shell [] shells = getShells_();
  2515     for (int i=0; i<shells.length; i++) shells [i].updateModal ();
  2544     for (int i=0; i<shells.length; i++) shells [i].updateModal ();
  2516 }
  2545 }
  2517 
  2546 
  2518 void setModalShell (Shell shell) {
  2547 static void setModalShell (Shell shell) {
  2519     if (modalShells == null) modalShells = new Shell [4];
  2548     if (modalShells == null) modalShells = new Shell [4];
  2520     int index = 0, length = modalShells.length;
  2549     int index = 0, length = modalShells.length;
  2521     while (index < length) {
  2550     while (index < length) {
  2522         if (modalShells [index] == shell) return;
  2551         if (modalShells [index] == shell) return;
  2523         if (modalShells [index] == null) break;
  2552         if (modalShells [index] == null) break;
  2527         Shell [] newModalShells = new Shell [length + 4];
  2556         Shell [] newModalShells = new Shell [length + 4];
  2528         System.arraycopy (modalShells, 0, newModalShells, 0, length);
  2557         System.arraycopy (modalShells, 0, newModalShells, 0, length);
  2529         modalShells = newModalShells;
  2558         modalShells = newModalShells;
  2530     }
  2559     }
  2531     modalShells [index] = shell;
  2560     modalShells [index] = shell;
  2532     Shell [] shells = getShells ();
  2561     Shell [] shells = getShells_();
  2533     for (int i=0; i<shells.length; i++) shells [i].updateModal ();
  2562     for (int i=0; i<shells.length; i++) shells [i].updateModal ();
  2534 }
  2563 }
  2535 
  2564 
  2536 /**
  2565 /**
  2537  * Sets the synchronizer used by the display to be
  2566  * Sets the synchronizer used by the display to be
  2549  * </ul>
  2578  * </ul>
  2550  */
  2579  */
  2551 public void setSynchronizer (Synchronizer synchronizer) {
  2580 public void setSynchronizer (Synchronizer synchronizer) {
  2552     checkDevice ();
  2581     checkDevice ();
  2553     if (synchronizer == null) error (SWT.ERROR_NULL_ARGUMENT);
  2582     if (synchronizer == null) error (SWT.ERROR_NULL_ARGUMENT);
  2554     if (synchronizer == this.synchronizer) return;
  2583     if (synchronizer == Display.synchronizer) return;
  2555     Synchronizer oldSynchronizer;
  2584     Synchronizer oldSynchronizer;
  2556     synchronized (Device.class) {
  2585     synchronized (Device.class) {
  2557         oldSynchronizer = this.synchronizer;
  2586         oldSynchronizer = Display.synchronizer;
  2558         this.synchronizer = synchronizer;
  2587         Display.synchronizer = synchronizer;
  2559     }
  2588     }
  2560     if (oldSynchronizer != null) {
  2589     if (oldSynchronizer != null) {
  2561         oldSynchronizer.runAsyncMessages(true);
  2590         oldSynchronizer.runAsyncMessages(true);
  2562     }
  2591     }
  2563 }
  2592 }
  2609  */
  2638  */
  2610 public void syncExec (Runnable runnable) {
  2639 public void syncExec (Runnable runnable) {
  2611     Synchronizer synchronizer;
  2640     Synchronizer synchronizer;
  2612     synchronized (Device.class) {
  2641     synchronized (Device.class) {
  2613         if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
  2642         if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
  2614         synchronizer = this.synchronizer;
  2643         synchronizer = Display.synchronizer;
  2615     }
  2644     }
  2616     synchronizer.syncExec (runnable);
  2645     synchronizer.syncExec (runnable);
  2617 }
  2646 }
  2618 
  2647 
  2619 /**
  2648 /**
  2685 
  2714 
  2686     int timerHandle = OS.QTimer_new();
  2715     int timerHandle = OS.QTimer_new();
  2687     if (timerHandle != 0) {
  2716     if (timerHandle != 0) {
  2688         OS.QObject_setParent(timerHandle, handle); // QTimer is a child of QApplication
  2717         OS.QObject_setParent(timerHandle, handle); // QTimer is a child of QApplication
  2689         OS.QTimer_setSingleShot(timerHandle, true);
  2718         OS.QTimer_setSingleShot(timerHandle, true);
  2690         int signalProxy = OS.SignalHandler_new(timerHandle, this, OS.QSIGNAL_TIMEOUT);
  2719         int signalProxy = OS.SignalHandler_new(timerHandle, OS.QSIGNAL_TIMEOUT);
  2691         OS.QObject_connectOrThrow(timerHandle, "timeout()", signalProxy, "widgetSignal()", OS.QT_AUTOCONNECTION);
  2720         OS.QObject_connectOrThrow(timerHandle, "timeout()", signalProxy, "widgetSignal()", OS.QT_AUTOCONNECTION);
  2692         OS.QTimer_start(timerHandle, milliseconds);
  2721         OS.QTimer_start(timerHandle, milliseconds);
  2693         timerHandles [index] = timerHandle;
  2722         timerHandles [index] = timerHandle;
  2694         timerRunnables [index] = runnable;
  2723         timerRunnables [index] = runnable;
  2695     }
  2724     }
  2696 }
  2725 }
  2697 
  2726 
  2698 // Converts single mouse button from Qt to SWT for the MouseEvent.button
  2727 // Converts single mouse button from Qt to SWT for the MouseEvent.button
  2699 // field which takes integers, 1, 2, 3, ...
  2728 // field which takes integers, 1, 2, 3, ...
  2700 static final int translateButton(int nativeButton) {
  2729 static int translateButton(int nativeButton) {
  2701     switch(nativeButton) {
  2730     switch(nativeButton) {
  2702     case OS.QT_LEFTBUTTON:
  2731     case OS.QT_LEFTBUTTON:
  2703         return 1;
  2732         return 1;
  2704     case OS.QT_MIDBUTTON:
  2733     case OS.QT_MIDBUTTON:
  2705         return 2;
  2734         return 2;
  2709         return 0;
  2738         return 0;
  2710     }
  2739     }
  2711 }
  2740 }
  2712 
  2741 
  2713 // Converts a mask of buttons from Qt to SWT
  2742 // Converts a mask of buttons from Qt to SWT
  2714 static final int translateButtons(int buttons) {
  2743 static int translateButtons(int buttons) {
  2715     int state = 0;
  2744     int state = 0;
  2716     if((buttons & OS.QT_LEFTBUTTON) != 0) state |= SWT.BUTTON1;
  2745     if((buttons & OS.QT_LEFTBUTTON) != 0) state |= SWT.BUTTON1;
  2717     if((buttons & OS.QT_MIDBUTTON) != 0) state |= SWT.BUTTON2;
  2746     if((buttons & OS.QT_MIDBUTTON) != 0) state |= SWT.BUTTON2;
  2718     if((buttons & OS.QT_RIGHTBUTTON) != 0) state |= SWT.BUTTON3;
  2747     if((buttons & OS.QT_RIGHTBUTTON) != 0) state |= SWT.BUTTON3;
  2719     return state;
  2748     return state;
  2720 }
  2749 }
  2721 
  2750 
  2722 // Converts keys from Qt to SWT
  2751 // Converts keys from Qt to SWT
  2723 static final int translateKey (int key, boolean keypad ) {
  2752 static int translateKey (int key, boolean keypad ) {
  2724     if( keypad ){
  2753     if( keypad ){
  2725         for (int i=0; i<KeyPadKeyTable.length; i++) {
  2754         for (int i=0; i<KeyPadKeyTable.length; i++) {
  2726             if (KeyPadKeyTable [i] [0] == key) return KeyPadKeyTable [i] [1];
  2755             if (KeyPadKeyTable [i] [0] == key) return KeyPadKeyTable [i] [1];
  2727         }
  2756         }
  2728     }
  2757     }
  2731     }
  2760     }
  2732     return 0;
  2761     return 0;
  2733 }
  2762 }
  2734 
  2763 
  2735 // Converts modifiers from Qt to SWT
  2764 // Converts modifiers from Qt to SWT
  2736 static final int translateModifiers(int nativeModifiers) {
  2765 static int translateModifiers(int nativeModifiers) {
  2737     int modifiers = 0;
  2766     int modifiers = 0;
  2738     if ((nativeModifiers & OS.QT_ALTMODIFIER) != 0) modifiers |= SWT.ALT;
  2767     if ((nativeModifiers & OS.QT_ALTMODIFIER) != 0) modifiers |= SWT.ALT;
  2739     if ((nativeModifiers & OS.QT_SHIFTMODIFIER) != 0) modifiers |= SWT.SHIFT;
  2768     if ((nativeModifiers & OS.QT_SHIFTMODIFIER) != 0) modifiers |= SWT.SHIFT;
  2740     if ((nativeModifiers & OS.QT_CONTROLMODIFIER) != 0) modifiers |= SWT.CONTROL;
  2769     if ((nativeModifiers & OS.QT_CONTROLMODIFIER) != 0) modifiers |= SWT.CONTROL;
  2741     return modifiers;
  2770     return modifiers;
  2742 }
  2771 }
  2743 
  2772 
  2744 static final int translateDirection(int orientation) {
  2773 static int translateDirection(int orientation) {
  2745     if(orientation ==  OS.QT_RIGHTTOLEFT) {
  2774     if(orientation ==  OS.QT_RIGHTTOLEFT) {
  2746         return SWT.RIGHT_TO_LEFT;
  2775         return SWT.RIGHT_TO_LEFT;
  2747     } else {
  2776     } else {
  2748         return SWT.LEFT_TO_RIGHT;
  2777         return SWT.LEFT_TO_RIGHT;
  2749     }
  2778     }
  2750 }
  2779 }
  2751 
  2780 
  2752 // Converts native scan code to key location (left/right) for CTRL/ALT/SHIFT/etc
  2781 // Converts native scan code to key location (left/right) for CTRL/ALT/SHIFT/etc
  2753 static final int translateKeyLocation(int nativeScanCode) {
  2782 static int translateKeyLocation(int nativeScanCode) {
  2754     
  2783     
  2755     if(OS.windowServer == OS.WS_SYMBIAN_S60) {
  2784     if(OS.windowServer == OS.WS_SYMBIAN_S60) {
  2756         switch (nativeScanCode) {
  2785         switch (nativeScanCode) {
  2757         case SYMBIAN_SHIFT_LEFT:    //intentional fall through
  2786         case SYMBIAN_SHIFT_LEFT:    //intentional fall through
  2758         case SYMBIAN_ALT_LEFT:
  2787         case SYMBIAN_ALT_LEFT:
  2785     }
  2814     }
  2786 
  2815 
  2787     return 0;
  2816     return 0;
  2788 }
  2817 }
  2789 
  2818 
  2790 private final void tryDestroy() {
  2819 private static void tryDestroy() {
       
  2820 	// Do nothing if there are Displays that have not been disposed
       
  2821 	if((Default != null && !Default.disposed) || 
       
  2822 			(Internal != null && !Internal.disposed)) {
       
  2823 		return;
       
  2824 	}
       
  2825 	
  2791     // Destroy the QApplication only when all event loops have exited.
  2826     // Destroy the QApplication only when all event loops have exited.
  2792     if(handle != 0 && OS.SwtApplication_javaCallbackCount() == 0) {
  2827     if(handle != 0 && OS.SwtApplication_javaCallbackCount() == 0) {
  2793         
  2828         
  2794     	OS.QApplication_closeAllWindows();
  2829     	OS.QApplication_closeAllWindows();
  2795 
  2830 
  2813         OS.cleanUpUiThread();
  2848         OS.cleanUpUiThread();
  2814     }
  2849     }
  2815 }
  2850 }
  2816 
  2851 
  2817 // Converts mouse buttons from SWT to Qt
  2852 // Converts mouse buttons from SWT to Qt
  2818 static final int unTranslateButton(int button) {
  2853 static int unTranslateButton(int button) {
  2819     switch(button) {
  2854     switch(button) {
  2820     case 1:
  2855     case 1:
  2821         return OS.QT_LEFTBUTTON;
  2856         return OS.QT_LEFTBUTTON;
  2822     case 2:
  2857     case 2:
  2823         return OS.QT_MIDBUTTON;
  2858         return OS.QT_MIDBUTTON;
  2827         return OS.QT_NOBUTTON;
  2862         return OS.QT_NOBUTTON;
  2828     }
  2863     }
  2829 }
  2864 }
  2830 
  2865 
  2831 // Converts event types from SWT to Qt
  2866 // Converts event types from SWT to Qt
  2832 static final int unTranslateEventType(int type) {
  2867 static int unTranslateEventType(int type) {
  2833     switch (type) {
  2868     switch (type) {
  2834     case SWT.KeyDown:
  2869     case SWT.KeyDown:
  2835         return OS.QEVENT_KEYPRESS;
  2870         return OS.QEVENT_KEYPRESS;
  2836     case SWT.KeyUp:
  2871     case SWT.KeyUp:
  2837         return OS.QEVENT_KEYRELEASE;
  2872         return OS.QEVENT_KEYRELEASE;
  2847         return 0;
  2882         return 0;
  2848     }
  2883     }
  2849 }
  2884 }
  2850 
  2885 
  2851 // Converts keys from SWT to Qt
  2886 // Converts keys from SWT to Qt
  2852 public final static int untranslateKey (int key) {
  2887 static int untranslateKey (int key) {
  2853     for (int i=0; i<KeyTable.length; i++) {
  2888     for (int i=0; i<KeyTable.length; i++) {
  2854         if (KeyTable [i] [1] == key) return KeyTable [i] [0];
  2889         if (KeyTable [i] [1] == key) return KeyTable [i] [0];
  2855     }
  2890     }
  2856     for (int i=0; i<KeyPadKeyTable.length; i++) {
  2891     for (int i=0; i<KeyPadKeyTable.length; i++) {
  2857         if (KeyPadKeyTable [i] [1] == key) return KeyPadKeyTable [i] [0];
  2892         if (KeyPadKeyTable [i] [1] == key) return KeyPadKeyTable [i] [0];
  2858     }
  2893     }
  2859     return 0;
  2894     return 0;
  2860 }
  2895 }
  2861 
  2896 
  2862 // Converts modifiers from SWT to Qt
  2897 // Converts modifiers from SWT to Qt
  2863 static final int unTranslateModifiers(int modifiers) {
  2898 static int unTranslateModifiers(int modifiers) {
  2864     int nativeModifiers = 0;
  2899     int nativeModifiers = 0;
  2865     if ((modifiers & SWT.ALT) != 0) nativeModifiers |= OS.QT_ALTMODIFIER;
  2900     if ((modifiers & SWT.ALT) != 0) nativeModifiers |= OS.QT_ALTMODIFIER;
  2866     if ((modifiers & SWT.SHIFT) != 0) nativeModifiers |= OS.QT_SHIFTMODIFIER;
  2901     if ((modifiers & SWT.SHIFT) != 0) nativeModifiers |= OS.QT_SHIFTMODIFIER;
  2867     if ((modifiers & SWT.CONTROL) != 0) nativeModifiers |= OS.QT_CONTROLMODIFIER;
  2902     if ((modifiers & SWT.CONTROL) != 0) nativeModifiers |= OS.QT_CONTROLMODIFIER;
  2868     return nativeModifiers;
  2903     return nativeModifiers;
  2884     if (OS.QCoreApplication_hasPendingEvents(handle)) {
  2919     if (OS.QCoreApplication_hasPendingEvents(handle)) {
  2885         OS.QCoreApplication_processEvents(handle, OS.QEVENTLOOP_EXCLUDEUSERINPUTEVENTS);
  2920         OS.QCoreApplication_processEvents(handle, OS.QEVENTLOOP_EXCLUDEUSERINPUTEVENTS);
  2886     }
  2921     }
  2887 }
  2922 }
  2888 
  2923 
  2889 void updateImages (Control[] controls) {
  2924 static void updateImages (Control[] controls) {
  2890     for(int i = 0; i < controls.length; ++i) {
  2925     for(int i = 0; i < controls.length; ++i) {
  2891         controls[i].updateImages();
  2926         controls[i].updateImages();
  2892     }
  2927     }
  2893 }
  2928 }
  2894 
  2929 
  2895 void updateLayoutDirection(Widget widget) {
  2930 static void updateLayoutDirection(Widget widget) {
  2896     // If the widget doesn't follow system language direction then leave it
  2931     // If the widget doesn't follow system language direction then leave it
  2897     if((widget.state & WidgetState.FOLLOWS_SYSLANG_DIRECTION) != 0) {
  2932     if((widget.state & WidgetState.FOLLOWS_SYSLANG_DIRECTION) != 0) {
  2898         // Update the style flags. We already know LEFT_TO_RIGHT flag is not 
  2933         // Update the style flags. We already know LEFT_TO_RIGHT flag is not 
  2899         // set because if it was the widget wouldn't be following the system 
  2934         // set because if it was the widget wouldn't be following the system 
  2900         // language direction. 
  2935         // language direction. 
  2911         OS.QWidget_setLayoutDirection(widget.topHandle, 
  2946         OS.QWidget_setLayoutDirection(widget.topHandle, 
  2912                 defaultOrientation == SWT.RIGHT_TO_LEFT ? OS.QT_RIGHTTOLEFT : OS.QT_LEFTTORIGHT);
  2947                 defaultOrientation == SWT.RIGHT_TO_LEFT ? OS.QT_RIGHTTOLEFT : OS.QT_LEFTTORIGHT);
  2913     }
  2948     }
  2914 }
  2949 }
  2915 
  2950 
  2916 void updateLayoutDirections(Control[] controls) {
  2951 static void updateLayoutDirections(Control[] controls) {
  2917     if(!defaultOrientationIsSysLangDirection) return;
  2952     if(!defaultOrientationIsSysLangDirection) return;
  2918     
  2953     
  2919     // Update the default orientation
  2954     // Update the default orientation
  2920     defaultOrientation = translateDirection(OS.QApplication_layoutDirection());
  2955     defaultOrientation = translateDirection(OS.QApplication_layoutDirection());
  2921     
  2956     
  2947         if (thread == Thread.currentThread ()) return;
  2982         if (thread == Thread.currentThread ()) return;
  2948         wakeThread ();
  2983         wakeThread ();
  2949     }
  2984     }
  2950 }
  2985 }
  2951 
  2986 
  2952 void wakeThread () {
  2987 static void wakeThread () {
  2953     int threadHandle = OS.QObject_thread( Display.handle );
  2988     int threadHandle = OS.QObject_thread( Display.handle );
  2954     int dispatcher = OS.QAbstractEventDispatcher_instance( threadHandle );
  2989     int dispatcher = OS.QAbstractEventDispatcher_instance( threadHandle );
  2955     OS.QAbstractEventDispatcher_wakeup( dispatcher );
  2990     OS.QAbstractEventDispatcher_wakeup( dispatcher );
  2956 }
  2991 }
  2957 
  2992 
  2958 int initializeMobileDevice(){
  2993 static int initializeMobileDevice(){
  2959 	if(mobileDeviceHandle == 0){
  2994 	if(mobileDeviceHandle == 0){
  2960 		mobileDeviceHandle = OS.MobileDevice_new();
  2995 		mobileDeviceHandle = OS.MobileDevice_new();
  2961 	}
  2996 	}
  2962 	return mobileDeviceHandle;
  2997 	return mobileDeviceHandle;
  2963 }
  2998 }