-
Notifications
You must be signed in to change notification settings - Fork 119
/
Display.java
5210 lines (4892 loc) · 159 KB
/
Display.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Christoph Läubrich - Issue #64 - Integration with java.util.concurrent framework
*******************************************************************************/
package org.eclipse.swt.widgets;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.ole.win32.*;
import org.eclipse.swt.internal.win32.*;
/**
* 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.
* <p>
* Applications which are built with SWT will <em>almost always</em>
* require only a single display. In particular, some platforms
* which SWT supports will not allow more than one <em>active</em>
* display. In other words, some platforms do not support
* creating a new display if one already exists that has not been
* sent the <code>dispose()</code> message.
* <p>
* In SWT, the thread which creates a <code>Display</code>
* instance is distinguished as the <em>user-interface thread</em>
* for that display.
* </p>
* The user-interface thread for a particular display has the
* following special attributes:
* <ul>
* <li>
* The event loop for that display must be run from the thread.
* </li>
* <li>
* Some SWT API methods (notably, most of the public methods in
* <code>Widget</code> and its subclasses), may only be called
* from the thread. (To support multi-threaded user-interface
* applications, class <code>Display</code> provides inter-thread
* communication methods which allow threads other than the
* user-interface thread to request that it perform operations
* on their behalf.)
* </li>
* <li>
* The thread is not allowed to construct other
* <code>Display</code>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.)
* </li>
* </ul>
* <p>
* Enforcing these attributes allows SWT to be implemented directly
* on the underlying operating system's event model. This has
* numerous benefits including smaller footprint, better use of
* resources, safer memory management, clearer program logic,
* better performance, and fewer overall operating system threads
* required. The down side however, is that care must be taken
* (only) when constructing multi-threaded applications to use the
* inter-thread communication mechanisms which this class provides
* when required.
* </p><p>
* 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 "<code>ERROR_THREAD_INVALID_ACCESS</code>"
* SWT exception.
* </p>
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>(none)</dd>
* <dt><b>Events:</b></dt>
* <dd>Close, Dispose, OpenDocument, Settings, Skin</dd>
* </dl>
* <p>
* IMPORTANT: This class is <em>not</em> intended to be subclassed.
* </p>
* @see #syncExec
* @see #asyncExec
* @see #wake
* @see #readAndDispatch
* @see #sleep
* @see Device#dispose
* @see <a href="http://www.eclipse.org/swt/snippets/#display">Display snippets</a>
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
* @noextend This class is not intended to be subclassed by clients.
*/
public class Display extends Device implements Executor {
/**
* the handle to the OS message queue
* (Warning: This field is platform dependent)
* <p>
* <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
* public API. It is marked public only so that it can be shared
* within the packages provided by SWT. It is not available on all
* platforms and should never be accessed from application code.
* </p>
*
* @noreference This field is not intended to be referenced by clients.
*/
public MSG msg = new MSG ();
static String APP_NAME = "SWT"; //$NON-NLS-1$
static String APP_VERSION = ""; //$NON-NLS-1$
String appLocalDir;
/* Windows and Events */
Event [] eventQueue;
Callback windowCallback;
long windowProc;
int threadId;
TCHAR windowClass, windowShadowClass, windowOwnDCClass;
static int WindowClassCount;
static final String WindowName = "SWT_Window"; //$NON-NLS-1$
static final String WindowShadowName = "SWT_WindowShadow"; //$NON-NLS-1$
static final String WindowOwnDCName = "SWT_WindowOwnDC"; //$NON-NLS-1$
EventTable eventTable, filterTable;
boolean useOwnDC;
boolean externalEventLoop; // events are dispatched outside SWT, e.g. TrackPopupMenu or DoDragDrop
/* Widget Table */
private Map<Long, Control> controlByHandle;
static final int GROW_SIZE = 1024;
/* Startup info */
static STARTUPINFO lpStartupInfo;
static {
lpStartupInfo = new STARTUPINFO ();
lpStartupInfo.cb = STARTUPINFO.sizeof;
OS.GetStartupInfo (lpStartupInfo);
}
/* XP Themes */
long hButtonTheme, hButtonThemeDark, hEditTheme, hExplorerBarTheme, hScrollBarTheme, hScrollBarThemeDark, hTabTheme;
static final char [] EXPLORER = new char [] {'E', 'X', 'P', 'L', 'O', 'R', 'E', 'R', 0};
static final char [] TREEVIEW = new char [] {'T', 'R', 'E', 'E', 'V', 'I', 'E', 'W', 0};
/* Emergency switch to be used in case of regressions. Not supposed to be changed when app is running. */
static final boolean disableCustomThemeTweaks = Boolean.valueOf(System.getProperty("org.eclipse.swt.internal.win32.disableCustomThemeTweaks")); //$NON-NLS-1$
/**
* Changes Windows theme to 'DarkMode_Explorer' for Controls that can benefit from it.
* Effects as of Windows 10 version 1909:<br>
* <ul>
* <li>Button - default background/foreground colors</li>
* <li>Scrollbars in controls tha have them - this is the most important change for many applications.</li>
* <li>Tree - dark theme compatible expander icon.</li>
* <li>Tree - dark theme compatible colors for selected and hovered items.</li>
* <li>The list is not exhaustive. Also, effects can change, because Windows dark theme is not yet official.</li>
* </ul>
* Limitations:<br>
* <ul>
* <li>Only available since Win10 version 1903.</li>
* <li>Does not affect already created controls.</li>
* </ul>
* All Scrollable-based Controls are affected.
*/
static final String USE_DARKMODE_EXPLORER_THEME_KEY = "org.eclipse.swt.internal.win32.useDarkModeExplorerTheme";
boolean useDarkModeExplorerTheme;
/**
* Sets Shell titles to match theme selected in Windows. That is, dark is system is dark.
* Limitations:<br>
* <ul>
* <li>Only available since Win10.</li>
* <li>Does not affect already created Shells.</li>
* </ul>
*/
static final String USE_SHELL_TITLE_COLORING = "org.eclipse.swt.internal.win32.useShellTitleColoring";
boolean useShellTitleColoring;
/**
* Configures background/foreground colors of Menu(SWT.BAR).<br>
* Side effects:
* <ul>
* <li>Menu items are no longer highlighted when mouse hovers over</li>
* </ul>
* Expects a <code>Color</code> value.
*/
static final String MENUBAR_FOREGROUND_COLOR_KEY = "org.eclipse.swt.internal.win32.menuBarForegroundColor"; //$NON-NLS-1$
int menuBarForegroundPixel = -1;
static final String MENUBAR_BACKGROUND_COLOR_KEY = "org.eclipse.swt.internal.win32.menuBarBackgroundColor"; //$NON-NLS-1$
int menuBarBackgroundPixel = -1;
/**
* Color of the 1px separator between Menu(SWT.BAR) and Shell's client area.<br>
* Expects a <code>Color</code> value.
*/
static final String MENUBAR_BORDER_COLOR_KEY = "org.eclipse.swt.internal.win32.menuBarBorderColor"; //$NON-NLS-1$
long menuBarBorderPen;
/**
* Use a different border for Controls.
* This often gives better results for dark themes.
* The effect is slightly different for different controls.
*/
static final String USE_WS_BORDER_ALL_KEY = "org.eclipse.swt.internal.win32.all.use_WS_BORDER"; //$NON-NLS-1$
boolean useWsBorderAll = false;
static final String USE_WS_BORDER_CANVAS_KEY = "org.eclipse.swt.internal.win32.Canvas.use_WS_BORDER"; //$NON-NLS-1$
boolean useWsBorderCanvas = false;
static final String USE_WS_BORDER_LABEL_KEY = "org.eclipse.swt.internal.win32.Label.use_WS_BORDER"; //$NON-NLS-1$
boolean useWsBorderLabel = false;
static final String USE_WS_BORDER_LIST_KEY = "org.eclipse.swt.internal.win32.List.use_WS_BORDER"; //$NON-NLS-1$
boolean useWsBorderList = false;
static final String USE_WS_BORDER_TABLE_KEY = "org.eclipse.swt.internal.win32.Table.use_WS_BORDER"; //$NON-NLS-1$
boolean useWsBorderTable = false;
static final String USE_WS_BORDER_TEXT_KEY = "org.eclipse.swt.internal.win32.Text.use_WS_BORDER"; //$NON-NLS-1$
boolean useWsBorderText = false;
/**
* Changes the color of Table header's column delimiters.
* Only affects custom-drawn header, that is when background/foreground header color is set.
* Expects a <code>Color</code> value.
*/
static final String TABLE_HEADER_LINE_COLOR_KEY = "org.eclipse.swt.internal.win32.Table.headerLineColor"; //$NON-NLS-1$
int tableHeaderLinePixel = -1;
/**
* Disabled Label is drawn with specified foreground color.
* Expects a <code>Color</code> value.
*/
static final String LABEL_DISABLED_FOREGROUND_COLOR_KEY = "org.eclipse.swt.internal.win32.Label.disabledForegroundColor"; //$NON-NLS-1$
int disabledLabelForegroundPixel = -1;
/**
* Use dark theme for Combo.
* Limitations:<br>
* <ul>
* <li>Only available since Win10 version 1903.</li>
* <li>Does not affect already created controls.</li>
* </ul>
* Expects a <code>boolean</code> value.
*/
static final String COMBO_USE_DARK_THEME = "org.eclipse.swt.internal.win32.Combo.useDarkTheme"; //$NON-NLS-1$
boolean comboUseDarkTheme = false;
/**
* Use .setForeground() .setBackground() theme for ProgressBar.
* Limitations:<br>
* <ul>
* <li>Does not affect already created controls.</li>
* </ul>
* Side effects:
* <ul>
* <li>ProgressBar's shine animation is lost.</li>
* </ul>
* Expects a <code>boolean</code> value.
*/
static final String PROGRESSBAR_USE_COLORS = "org.eclipse.swt.internal.win32.ProgressBar.useColors"; //$NON-NLS-1$
boolean progressbarUseColors = false;
/**
* Use dark theme compatible icons for Text with SWT.ICON_SEARCH, SWT.ICON_CANCEL
* Limitations:<br>
* <ul>
* <li>Must be configured before first Text is created.</li>
* </ul>
* Expects a <code>boolean</code> value.
*/
static final String USE_DARKTHEME_TEXT_ICONS = "org.eclipse.swt.internal.win32.Text.useDarkThemeIcons"; //$NON-NLS-1$
boolean textUseDarkthemeIcons = false;
/* Custom icons */
long hIconSearch;
long hIconCancel;
/* Focus */
int focusEvent;
Control focusControl;
/* Menus */
Menu [] bars, popups;
MenuItem [] items;
/*
* The start value for WM_COMMAND id's.
* Windows reserves the values 0..100.
*
* The SmartPhone SWT resource file reserves
* the values 101..107.
*/
static final int ID_START = 108;
/* Filter Hook */
Callback msgFilterCallback;
long msgFilterProc, filterHook;
MSG hookMsg = new MSG ();
boolean runDragDrop = true, dragCancelled = false;
/* Idle Hook */
Callback foregroundIdleCallback;
long foregroundIdleProc, idleHook;
/* Message Hook and Embedding */
boolean ignoreNextKey;
Callback getMsgCallback, embeddedCallback;
long getMsgProc, msgHook, embeddedHwnd, embeddedProc;
static final String AWT_WINDOW_CLASS = "SunAwtWindow"; //$NON-NLS-1$
static final short [] ACCENTS = new short [] {'~', '`', '\'', '^', '"'};
/* Sync/Async Widget Communication */
Synchronizer synchronizer;
Consumer<RuntimeException> runtimeExceptionHandler = DefaultExceptionHandler.RUNTIME_EXCEPTION_HANDLER;
Consumer<Error> errorHandler = DefaultExceptionHandler.RUNTIME_ERROR_HANDLER;
boolean runMessagesInIdle = false, runMessagesInMessageProc = true;
static final String RUN_MESSAGES_IN_IDLE_KEY = "org.eclipse.swt.internal.win32.runMessagesInIdle"; //$NON-NLS-1$
static final String RUN_MESSAGES_IN_MESSAGE_PROC_KEY = "org.eclipse.swt.internal.win32.runMessagesInMessageProc"; //$NON-NLS-1$
static final String USE_OWNDC_KEY = "org.eclipse.swt.internal.win32.useOwnDC"; //$NON-NLS-1$
static final String ACCEL_KEY_HIT = "org.eclipse.swt.internal.win32.accelKeyHit"; //$NON-NLS-1$
static final String EXTERNAL_EVENT_LOOP_KEY = "org.eclipse.swt.internal.win32.externalEventLoop"; //$NON-NLS-1$
static final String APPLOCAL_DIR_KEY = "org.eclipse.swt.internal.win32.appLocalDir"; //$NON-NLS-1$
Thread thread;
/* Display Shutdown */
Runnable [] disposeList;
/* Deferred Layout list */
Composite[] layoutDeferred;
int layoutDeferredCount;
/* System Tray */
Tray tray;
int nextTrayId;
/* TaskBar */
TaskBar taskBar;
static final String TASKBAR_EVENT = "/SWTINTERNAL_ID"; //$NON-NLS-1$
static final String LAUNCHER_PREFIX = "--launcher.openFile "; //$NON-NLS-1$
/* Timers */
long [] timerIds;
Runnable [] timerList;
long nextTimerId = SETTINGS_ID + 1;
/* Settings */
static final long SETTINGS_ID = 100;
static final int SETTINGS_DELAY = 2000;
/* Keyboard and Mouse */
RECT clickRect;
int clickCount, lastTime, lastButton;
long lastClickHwnd;
Point scrollRemainderEvt = new Point(0, 0);
Point scrollRemainderBar = new Point(0, 0);
int lastKey, lastMouse, lastAscii;
boolean lastVirtual, lastDead;
byte [] keyboard = new byte [256];
boolean accelKeyHit, mnemonicKeyHit;
boolean lockActiveWindow, captureChanged, xMouse;
/* Gesture state */
double magStartDistance, lastDistance;
double rotationAngle;
int lastX, lastY;
/* Touch state */
TouchSource [] touchSources;
/* Tool Tips */
int nextToolTipId;
/* MDI */
boolean ignoreRestoreFocus;
Control lastHittestControl;
int lastHittest;
/* Message Only Window */
Callback messageCallback;
long hwndMessage, messageProc;
/* System Resources */
@Deprecated(since = "3.126.0")
LOGFONT lfSystemFont;
@Deprecated(since = "3.126.0")
Font systemFont;
Image errorImage, infoImage, questionImage, warningIcon;
Cursor [] cursors = new Cursor [SWT.CURSOR_HAND + 1];
Resource [] resources;
static final int RESOURCE_SIZE = 1 + 4 + SWT.CURSOR_HAND + 1;
/* ImageList Cache */
ImageList[] imageList, toolImageList, toolHotImageList, toolDisabledImageList;
/* Custom Colors for ChooseColor */
long lpCustColors;
/* Table */
char [] tableBuffer;
/* Resize and move recursion */
int resizeCount;
static final int RESIZE_LIMIT = 4;
/* Display Data */
Object data;
String [] keys;
Object [] values;
/* Key Mappings */
static final int [] [] KeyTable = {
/* Keyboard and Mouse Masks */
{OS.VK_MENU, SWT.ALT},
{OS.VK_SHIFT, SWT.SHIFT},
{OS.VK_CONTROL, SWT.CONTROL},
// {OS.VK_????, SWT.COMMAND},
/* NOT CURRENTLY USED */
// {OS.VK_LBUTTON, SWT.BUTTON1},
// {OS.VK_MBUTTON, SWT.BUTTON3},
// {OS.VK_RBUTTON, SWT.BUTTON2},
/* Non-Numeric Keypad Keys */
{OS.VK_UP, SWT.ARROW_UP},
{OS.VK_DOWN, SWT.ARROW_DOWN},
{OS.VK_LEFT, SWT.ARROW_LEFT},
{OS.VK_RIGHT, SWT.ARROW_RIGHT},
{OS.VK_PRIOR, SWT.PAGE_UP},
{OS.VK_NEXT, SWT.PAGE_DOWN},
{OS.VK_HOME, SWT.HOME},
{OS.VK_END, SWT.END},
{OS.VK_INSERT, SWT.INSERT},
/* Virtual and Ascii Keys */
{OS.VK_BACK, SWT.BS},
{OS.VK_RETURN, SWT.CR},
{OS.VK_DELETE, SWT.DEL},
{OS.VK_ESCAPE, SWT.ESC},
{OS.VK_RETURN, SWT.LF},
{OS.VK_TAB, SWT.TAB},
/* Functions Keys */
{OS.VK_F1, SWT.F1},
{OS.VK_F2, SWT.F2},
{OS.VK_F3, SWT.F3},
{OS.VK_F4, SWT.F4},
{OS.VK_F5, SWT.F5},
{OS.VK_F6, SWT.F6},
{OS.VK_F7, SWT.F7},
{OS.VK_F8, SWT.F8},
{OS.VK_F9, SWT.F9},
{OS.VK_F10, SWT.F10},
{OS.VK_F11, SWT.F11},
{OS.VK_F12, SWT.F12},
{OS.VK_F13, SWT.F13},
{OS.VK_F14, SWT.F14},
{OS.VK_F15, SWT.F15},
{OS.VK_F16, SWT.F16},
{OS.VK_F17, SWT.F17},
{OS.VK_F18, SWT.F18},
{OS.VK_F19, SWT.F19},
{OS.VK_F20, SWT.F20},
/* Numeric Keypad Keys */
{OS.VK_MULTIPLY, SWT.KEYPAD_MULTIPLY},
{OS.VK_ADD, SWT.KEYPAD_ADD},
{OS.VK_RETURN, SWT.KEYPAD_CR},
{OS.VK_SUBTRACT, SWT.KEYPAD_SUBTRACT},
{OS.VK_DECIMAL, SWT.KEYPAD_DECIMAL},
{OS.VK_DIVIDE, SWT.KEYPAD_DIVIDE},
{OS.VK_NUMPAD0, SWT.KEYPAD_0},
{OS.VK_NUMPAD1, SWT.KEYPAD_1},
{OS.VK_NUMPAD2, SWT.KEYPAD_2},
{OS.VK_NUMPAD3, SWT.KEYPAD_3},
{OS.VK_NUMPAD4, SWT.KEYPAD_4},
{OS.VK_NUMPAD5, SWT.KEYPAD_5},
{OS.VK_NUMPAD6, SWT.KEYPAD_6},
{OS.VK_NUMPAD7, SWT.KEYPAD_7},
{OS.VK_NUMPAD8, SWT.KEYPAD_8},
{OS.VK_NUMPAD9, SWT.KEYPAD_9},
// {OS.VK_????, SWT.KEYPAD_EQUAL},
/* Other keys */
{OS.VK_CAPITAL, SWT.CAPS_LOCK},
{OS.VK_NUMLOCK, SWT.NUM_LOCK},
{OS.VK_SCROLL, SWT.SCROLL_LOCK},
{OS.VK_PAUSE, SWT.PAUSE},
{OS.VK_CANCEL, SWT.BREAK},
{OS.VK_SNAPSHOT, SWT.PRINT_SCREEN},
// {OS.VK_????, SWT.HELP},
};
/* Multiple Displays */
static Display Default;
static Display [] Displays = new Display [1];
/* Multiple Monitors */
Monitor[] monitors = null;
int monitorCount = 0;
/* Modality */
Shell [] modalShells;
Dialog modalDialog;
static boolean TrimEnabled = false;
/* Private SWT Window Messages */
static final int SWT_GETACCELCOUNT = OS.WM_APP;
static final int SWT_GETACCEL = OS.WM_APP + 1;
static final int SWT_KEYMSG = OS.WM_APP + 2;
static final int SWT_DESTROY = OS.WM_APP + 3;
static final int SWT_TRAYICONMSG = OS.WM_APP + 4;
static final int SWT_NULL = OS.WM_APP + 5;
static final int SWT_RUNASYNC = OS.WM_APP + 6;
static int TASKBARCREATED;
static int TASKBARBUTTONCREATED;
static int SWT_RESTORECARET;
static int DI_GETDRAGIMAGE;
static int SWT_OPENDOC;
/* Skinning support */
Widget [] skinList = new Widget [GROW_SIZE];
int skinCount;
/* Package Name */
static final String PACKAGE_PREFIX = "org.eclipse.swt.widgets."; //$NON-NLS-1$
/*
* TEMPORARY CODE. Install the runnable that
* gets the current display. This code will
* be removed in the future.
*/
static {
DeviceFinder = () -> {
Device device = getCurrent ();
if (device == null) {
device = getDefault ();
}
setDevice (device);
};
}
static {
CommonWidgetsDPIChangeHandlers.registerCommonHandlers();
}
/*
* TEMPORARY CODE.
*/
static void setDevice (Device device) {
CurrentDevice = device;
}
/**
* Constructs a new instance of this class.
* <p>
* Note: The resulting display is marked as the <em>current</em>
* display. If this is the first display which has been
* constructed since the application started, it is also
* marked as the <em>default</em> display.
* </p>
*
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if called from a thread that already created an existing display</li>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @see #getCurrent
* @see #getDefault
* @see Widget#checkSubclass
* @see Shell
*/
public Display () {
this (null);
}
/**
* Constructs a new instance of this class using the parameter.
*
* @param data the device data
*/
public Display (DeviceData data) {
super (data);
}
Control _getFocusControl () {
return findControl (OS.GetFocus ());
}
void addBar (Menu menu) {
if (bars == null) bars = new Menu [4];
int length = bars.length;
for (int i=0; i<length; i++) {
if (bars [i] == menu) return;
}
int index = 0;
while (index < length) {
if (bars [index] == null) break;
index++;
}
if (index == length) {
Menu [] newBars = new Menu [length + 4];
System.arraycopy (bars, 0, newBars, 0, length);
bars = newBars;
}
bars [index] = menu;
}
void addControl (long handle, Control control) {
if (handle == 0) return;
controlByHandle.put(handle, control);
}
void addSkinnableWidget (Widget widget) {
if (skinCount >= skinList.length) {
Widget[] newSkinWidgets = new Widget [(skinList.length + 1) * 3 / 2];
System.arraycopy (skinList, 0, newSkinWidgets, 0, skinList.length);
skinList = newSkinWidgets;
}
skinList [skinCount++] = widget;
}
/**
* Adds the listener to the collection of listeners who will
* be notified when an event of the given type occurs anywhere
* in a widget. The event type is one of the event constants
* defined in class <code>SWT</code>. When the event does occur,
* the listener is notified by sending it the <code>handleEvent()</code>
* message.
* <p>
* Setting the type of an event to <code>SWT.None</code> from
* within the <code>handleEvent()</code> 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.
* </p>
*
* @param eventType the type of event to listen for
* @param listener the listener which should be notified when the event occurs
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*
* @see Listener
* @see SWT
* @see #removeFilter
* @see #removeListener
*
* @since 3.0
*/
public void addFilter (int eventType, Listener listener) {
checkDevice ();
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
if (filterTable == null) filterTable = new EventTable ();
filterTable.hook (eventType, listener);
}
void addLayoutDeferred (Composite comp) {
if (layoutDeferred == null) layoutDeferred = new Composite [64];
if (layoutDeferredCount == layoutDeferred.length) {
Composite [] temp = new Composite [layoutDeferred.length + 64];
System.arraycopy (layoutDeferred, 0, temp, 0, layoutDeferred.length);
layoutDeferred = temp;
}
layoutDeferred[layoutDeferredCount++] = comp;
}
/**
* Adds the listener to the collection of listeners who will
* be notified when an event of the given type occurs. The event
* type is one of the event constants defined in class <code>SWT</code>.
* When the event does occur in the display, the listener is notified by
* sending it the <code>handleEvent()</code> message.
*
* @param eventType the type of event to listen for
* @param listener the listener which should be notified when the event occurs
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*
* @see Listener
* @see SWT
* @see #removeListener
*
* @since 2.0
*/
public void addListener (int eventType, Listener listener) {
checkDevice ();
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
if (eventTable == null) eventTable = new EventTable ();
eventTable.hook (eventType, listener);
}
void addMenuItem (MenuItem item) {
if (items == null) items = new MenuItem [64];
for (int i=0; i<items.length; i++) {
if (items [i] == null) {
item.id = i + ID_START;
items [i] = item;
return;
}
}
item.id = items.length + ID_START;
MenuItem [] newItems = new MenuItem [items.length + 64];
newItems [items.length] = item;
System.arraycopy (items, 0, newItems, 0, items.length);
items = newItems;
}
void addPopup (Menu menu) {
if (popups == null) popups = new Menu [4];
int length = popups.length;
for (int i=0; i<length; i++) {
if (popups [i] == menu) return;
}
int index = 0;
while (index < length) {
if (popups [index] == null) break;
index++;
}
if (index == length) {
Menu [] newPopups = new Menu [length + 4];
System.arraycopy (popups, 0, newPopups, 0, length);
popups = newPopups;
}
popups [index] = menu;
}
int asciiKey (int key) {
/* Get the current keyboard. */
for (int i=0; i<keyboard.length; i++) keyboard [i] = 0;
if (!OS.GetKeyboardState (keyboard)) return 0;
/* Translate the key to ASCII or UNICODE using the virtual keyboard */
char [] buffer = new char [1];
int len = OS.ToUnicode (key, key, keyboard, buffer, 1, 0);
/* If the key is a dead key, flush dead key state. */
while (len == -1) {
len = OS.ToUnicode (key, key, keyboard, buffer, 1, 0);
}
return (len != 0) ? buffer [0] : 0;
}
/**
* Causes the <code>run()</code> 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 <code>null</code> as the
* runnable simply wakes the user-interface thread when run.
* <p>
* 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.
* </p>
*
* @param runnable code to run on the user-interface thread or <code>null</code>
*
* @exception SWTException <ul>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*
* @see #syncExec
*/
public void asyncExec (Runnable runnable) {
synchronized (Device.class) {
if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
synchronizer.asyncExec (runnable);
}
}
/**
* Executes the given runnable in the user-interface thread of this Display.
* <ul>
* <li>If the calling thread is the user-interface thread of this display it is
* executed immediately and the method returns after the command has run, as with
* the method {@link Display#syncExec(Runnable)}.</li>
* <li>In all other cases the <code>run()</code> method of the runnable is
* asynchronously executed as with the method
* {@link Display#asyncExec(Runnable)} at the next reasonable opportunity. The
* caller of this method continues to run in parallel, and is not notified when
* the runnable has completed.</li>
* </ul>
* <p>
* This can be used in cases where one want to execute some piece of code that
* should be guaranteed to run in the user-interface thread regardless of the
* current thread.
* </p>
*
* <p>
* Note that at the time the runnable is invoked, widgets that have the receiver
* as their display may have been disposed. Therefore, it is advised to check
* for this case inside the runnable before accessing the widget.
* </p>
*
* @param runnable the runnable to execute in the user-interface thread, never
* <code>null</code>
* @throws RejectedExecutionException if this task cannot be accepted for
* execution
* @throws NullPointerException if runnable is null
*/
@Override
public void execute(Runnable runnable) {
Objects.requireNonNull(runnable);
if (isDisposed()) {
throw new RejectedExecutionException(new SWTException (SWT.ERROR_WIDGET_DISPOSED, null));
}
if (thread == Thread.currentThread()) {
syncExec(runnable);
} else {
asyncExec(runnable);
}
}
/**
* Causes the system hardware to emit a short sound
* (if it supports this capability).
*
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*/
public void beep () {
checkDevice ();
OS.MessageBeep (OS.MB_OK);
}
/**
* Checks that this class can be subclassed.
* <p>
* IMPORTANT: See the comment in <code>Widget.checkSubclass()</code>.
* </p>
*
* @exception SWTException <ul>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @see Widget#checkSubclass
*/
protected void checkSubclass () {
if (!isValidClass (getClass ())) error (SWT.ERROR_INVALID_SUBCLASS);
}
@Override
protected void checkDevice () {
if (thread == null) error (SWT.ERROR_WIDGET_DISPOSED);
if (thread != Thread.currentThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
}
static void checkDisplay (Thread thread, boolean multiple) {
synchronized (Device.class) {
for (Display display : Displays) {
if (display != null) {
if (!multiple) SWT.error (SWT.ERROR_NOT_IMPLEMENTED, null, " [multiple displays]"); //$NON-NLS-1$
if (display.thread == thread) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS);
}
}
}
}
void clearModal (Shell shell) {
if (modalShells == null) return;
int index = 0, length = modalShells.length;
while (index < length) {
if (modalShells [index] == shell) break;
if (modalShells [index] == null) return;
index++;
}
if (index == length) return;
System.arraycopy (modalShells, index + 1, modalShells, index, --length - index);
modalShells [length] = null;
if (index == 0 && modalShells [0] == null) modalShells = null;
for (Shell activeShell : getShells ())
activeShell.updateModal ();
}
int controlKey (int key) {
int upper = (int)OS.CharUpper (OS.LOWORD (key));
if (64 <= upper && upper <= 95) return upper & 0xBF;
return key;
}
/**
* Requests that the connection between SWT and the underlying
* operating system be closed.
*
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*
* @see Device#dispose
*
* @since 2.0
*/
public void close () {
checkDevice ();
Event event = new Event ();
sendEvent (SWT.Close, event);
if (event.doit) dispose ();
}
/**
* Creates the device in the operating system. If the device
* does not have a handle, this method may do nothing depending
* on the device.
* <p>
* This method is called before <code>init</code>.
* </p>
*
* @param data the DeviceData which describes the receiver
*
* @see #init
*/
@Override
protected void create (DeviceData data) {
checkSubclass ();
checkDisplay (thread = Thread.currentThread (), true);
createDisplay (data);
register (this);
if (Default == null) Default = this;
}
void createDisplay (DeviceData data) {
}
static long create32bitDIB (Image image) {
int transparentPixel = -1, alpha = -1;
long hMask = 0, hBitmap = 0;
byte[] alphaData = null;
switch (image.type) {
case SWT.ICON:
ICONINFO info = new ICONINFO ();
OS.GetIconInfo (image.handle, info);
hBitmap = info.hbmColor;
hMask = info.hbmMask;
break;
case SWT.BITMAP:
ImageData data = image.getImageData (DPIUtil.getDeviceZoom ());
hBitmap = image.handle;
alpha = data.alpha;
alphaData = data.alphaData;
transparentPixel = data.transparentPixel;
break;
}
BITMAP bm = new BITMAP ();
OS.GetObject (hBitmap, BITMAP.sizeof, bm);
int imgWidth = bm.bmWidth;
int imgHeight = bm.bmHeight;
long hDC = OS.GetDC (0);
long srcHdc = OS.CreateCompatibleDC (hDC);
long oldSrcBitmap = OS.SelectObject (srcHdc, hBitmap);
long memHdc = OS.CreateCompatibleDC (hDC);
BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER ();
bmiHeader.biSize = BITMAPINFOHEADER.sizeof;
bmiHeader.biWidth = imgWidth;
bmiHeader.biHeight = -imgHeight;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = (short)32;
bmiHeader.biCompression = OS.BI_RGB;
byte [] bmi = new byte [BITMAPINFOHEADER.sizeof];
OS.MoveMemory (bmi, bmiHeader, BITMAPINFOHEADER.sizeof);
long [] pBits = new long [1];
long memDib = OS.CreateDIBSection (0, bmi, OS.DIB_RGB_COLORS, pBits, 0, 0);
if (memDib == 0) SWT.error (SWT.ERROR_NO_HANDLES);
long oldMemBitmap = OS.SelectObject (memHdc, memDib);
BITMAP dibBM = new BITMAP ();
OS.GetObject (memDib, BITMAP.sizeof, dibBM);
int sizeInBytes = dibBM.bmWidthBytes * dibBM.bmHeight;
OS.BitBlt (memHdc, 0, 0, imgWidth, imgHeight, srcHdc, 0, 0, OS.SRCCOPY);
byte red = 0, green = 0, blue = 0;
if (transparentPixel != -1) {
if (bm.bmBitsPixel <= 8) {
byte [] color = new byte [4];
OS.GetDIBColorTable (srcHdc, transparentPixel, 1, color);
blue = color [0];
green = color [1];
red = color [2];
} else {
switch (bm.bmBitsPixel) {
case 16:
blue = (byte)((transparentPixel & 0x1F) << 3);
green = (byte)((transparentPixel & 0x3E0) >> 2);