Skip to main content
summaryrefslogtreecommitdiffstats
blob: f4412502fa8c41b10cc78c60dcf78928cf873f77 (plain) (blame)
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
package org.eclipse.swt.widgets;

/*
 * Licensed Materials - Property of IBM,
 * (c) Copyright IBM Corp. 1998, 2001  All Rights Reserved
 */

import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.photon.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;

public /*final*/ class Display extends Device {

	/**** TEMPORARY CODE FOR MOTIF TABLE ***/
	static int textHighlightThickness = 0;
	
	/* TEMPORARY HACK FOR PHOTON */
	public boolean embedded;
	
	/* Photon Only Public Fields */
	public int app_context;
//	public int phEventSize = PhEvent_t.sizeof + 1024;
//	public int phEvent = OS.malloc (phEventSize);
	
	/* Deferred Events */
	Event [] eventQueue;
	
	/* Events Dispatching and Callback */
	Callback windowCallback, drawCallback, workCallback, inputCallback;
	int windowProc, drawProc, workProc, inputProc, input, pulse;
	boolean idle;

	/* Sync/Async Widget Communication */
	Synchronizer synchronizer = new Synchronizer (this);
	Thread thread = Thread.currentThread ();
	
	/* Display Shutdown */
	Runnable [] disposeList;
	
	/* Timers */
	int [] timers;
	Runnable [] timerList;
	Callback timerCallback;
	int timerProc, timerHandle;
	
	/* Keyboard */
	int lastKey, lastAscii;
	
	/* Key Mappings. */
	private static final int [] [] KeyTable = {
		
		/* Keyboard and Mouse Masks */
		{OS.Pk_Alt_L,     SWT.ALT},
		{OS.Pk_Alt_R,     SWT.ALT},
		{OS.Pk_Shift_L,   SWT.SHIFT},
		{OS.Pk_Shift_R,   SWT.SHIFT},
		{OS.Pk_Control_L, SWT.CONTROL},
		{OS.Pk_Control_R, SWT.CONTROL},
			
//		{OS.VK_LBUTTON, SWT.BUTTON1},
//		{OS.VK_MBUTTON, SWT.BUTTON3},
//		{OS.VK_RBUTTON, SWT.BUTTON2},
		
		/* Non-Numeric Keypad Constants */
		{OS.Pk_Up,		SWT.ARROW_UP},
		{OS.Pk_Down,	SWT.ARROW_DOWN},
		{OS.Pk_Left,	SWT.ARROW_LEFT},
		{OS.Pk_Right,	SWT.ARROW_RIGHT},
		{OS.Pk_Prior,	SWT.PAGE_UP},
		{OS.Pk_Next,	SWT.PAGE_DOWN},
		{OS.Pk_Home,	SWT.HOME},
		{OS.Pk_End,		SWT.END},
		{OS.Pk_Insert,	SWT.INSERT},
//		{OS.Pk_Delete,	SWT.DELETE},
//		{OS.Pk_Escape, 0x001B},
	
		/* Functions Keys */
		{OS.Pk_F1,	SWT.F1},
		{OS.Pk_F2,	SWT.F2},
		{OS.Pk_F3,	SWT.F3},
		{OS.Pk_F4,	SWT.F4},
		{OS.Pk_F5,	SWT.F5},
		{OS.Pk_F6,	SWT.F6},
		{OS.Pk_F7,	SWT.F7},
		{OS.Pk_F8,	SWT.F8},
		{OS.Pk_F9,	SWT.F9},
		{OS.Pk_F10,	SWT.F10},
		{OS.Pk_F11,	SWT.F11},
		{OS.Pk_F12,	SWT.F12},
		
		
		/* Numeric Keypad Constants */
		/*
		{OS.XK_KP_Add,		SWT.KP_PLUS},
		{OS.XK_KP_Subtract,	SWT.KP_MINUS},
		{OS.XK_KP_Multiply,	SWT.KP_TIMES},
		{OS.XK_KP_Divide,	SWT.KP_DIVIDE},
		{OS.XK_KP_Decimal,	SWT.KP_PERIOD},
		{OS.XK_KP_Enter,	SWT.KP_ENTER},
		{OS.XK_KP_0,		SWT.KP_0},
		{OS.XK_KP_1,		SWT.KP_1},
		{OS.XK_KP_2,		SWT.KP_2},
		{OS.XK_KP_3,		SWT.KP_3},
		{OS.XK_KP_4,		SWT.KP_4},
		{OS.XK_KP_5,		SWT.KP_5},
		{OS.XK_KP_6,		SWT.KP_6},
		{OS.XK_KP_7,		SWT.KP_7},
		{OS.XK_KP_8,		SWT.KP_8},
		{OS.XK_KP_9,		SWT.KP_9},
		*/
	};
	
	/* Multiple Displays. */
	static Display Default;
	static Display [] Displays = new Display [4];

	/* Window Classes */
	static int ClassesPtr;
	static int PtButton;
	static int PtList;
	static int PtLabel;
	static int PtWindow;
	static int PtToggleButton;
	static int PtComboBox;
	static int PtText;
	static int PtMultiText;
	static int PtScrollbar;
	static int PtScrollContainer;
	static int PtScrollArea;
	static int PtContainer;
	static int PtProgress;
	static int PtPanelGroup;
	static int PtPane;
	static int PtTree;
	static int PtSlider;
	static int PtSeparator;
				
	/* Colors */
	int WIDGET_DARK_SHADOW, WIDGET_NORMAL_SHADOW, WIDGET_LIGHT_SHADOW;
	int WIDGET_HIGHLIGHT_SHADOW, WIDGET_BACKGROUND, WIDGET_FOREGROUND, WIDGET_BORDER;
	int LIST_FOREGROUND, LIST_BACKGROUND, LIST_SELECTION, LIST_SELECTION_TEXT;
	
	/* Fonts */
	byte [] TEXT_FONT, LIST_FONT;

	/* Package name */
	static final String PACKAGE_NAME;
	static {
		String name = Display.class.getName ();
		int index = name.lastIndexOf ('.');
		PACKAGE_NAME = name.substring (0, index + 1);
	}

	/* Photon Draw Buffer - shared by all widgets */
	static int DrawBufferSize = 1024 * 48;

	/* Display Data */
	Object data;
	String [] keys;
	Object [] values;
			
public Display () {
	this (null);
}

public Display (DeviceData data) {
	super (data);
}

public void asyncExec (Runnable runnable) {
	if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
	synchronizer.asyncExec (runnable);
}

public void beep () {
	checkDevice ();
	OS.PtBeep ();
}

protected void checkDevice () {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
}

synchronized void checkDisplay () {
	for (int i=0; i<Displays.length; i++) {
		if (Displays [i] != null && Displays [i].thread == thread) {
			error (SWT.ERROR_THREAD_INVALID_ACCESS);
		}
	}
}

protected void checkSubclass () {
	if (!isValidClass (getClass ())) error (SWT.ERROR_INVALID_SUBCLASS);
}

protected void create (DeviceData data) {
	checkSubclass ();
	checkDisplay ();
	createDisplay (data);
	register ();
	if (Default == null) Default = this;
}

void createDisplay (DeviceData data) {
	OS.PtInit (null);
	OS.PgSetDrawBufferSize (DrawBufferSize);
	app_context = OS.PtCreateAppContext ();
}

synchronized void deregister () {
	for (int i=0; i<Displays.length; i++) {
		if (this == Displays [i]) Displays [i] = null;
	}
}

protected void destroy () {
	if (this == Default) Default = null;
	deregister ();
	destroyDisplay ();
}

void destroyDisplay () {
	// NEED to destroy app_context ???
}

public void disposeExec (Runnable runnable) {
	checkDevice ();
	if (disposeList == null) disposeList = new Runnable [4];
	for (int i=0; i<disposeList.length; i++) {
		if (disposeList [i] == null) {
			disposeList [i] = runnable;
			return;
		}
	}
	Runnable [] newDisposeList = new Runnable [disposeList.length + 4];
	System.arraycopy (disposeList, 0, newDisposeList, 0, disposeList.length);
	newDisposeList [disposeList.length] = runnable;
	disposeList = newDisposeList;
}

int drawProc (int handle, int damage) {
	Widget widget = WidgetTable.get (handle);
	if (widget == null) return 0;
	return widget.processPaint (damage);
}

void error (int code) {
	SWT.error(code);
}

public static synchronized Display findDisplay (Thread thread) {
	for (int i=0; i<Displays.length; i++) {
		Display display = Displays [i];
		if (display != null && display.thread == thread) {
			return display;
		}
	}
	return null;
}

public Widget findWidget (int handle) {
	checkDevice ();
	return WidgetTable.get (handle);
}

public Shell getActiveShell () {
	checkDevice ();
	int handle = 0;
	while ((handle = OS.PtNextTopLevelWidget (handle)) != 0) {
		int state = OS.PtWindowGetState (handle);
		if (state != -1 && (state & OS.Ph_WM_STATE_ISFOCUS) != 0) {
			Widget widget = WidgetTable.get (handle);
			if (widget instanceof Shell) return (Shell) widget;
		}
	}
	return null;
}

public Rectangle getBounds () {
	checkDevice ();
	PhRect_t rect = new PhRect_t ();
	OS.PhWindowQueryVisible (OS.Ph_QUERY_GRAPHICS, 0, 1, rect);
	int width = rect.lr_x - rect.ul_x + 1;
	int height = rect.lr_y - rect.ul_y + 1;
	return new Rectangle (rect.ul_x, rect.ul_y, width, height);
}

public Control getCursorControl () {
	checkDevice ();
	int ig = OS.PhInputGroup (0);
	PhCursorInfo_t info = new PhCursorInfo_t ();
	OS.PhQueryCursor ((short) ig, info);
	PhRect_t rect = new PhRect_t ();
	rect.ul_x = rect.lr_x = info.pos_x;
	rect.ul_y = rect.lr_y = info.pos_y;
	int handle = 0;
	//DOESN'T WORK WHEN SHELLS OVERLAP (NEED Z ORDER)
	while ((handle = OS.PtNextTopLevelWidget (handle)) != 0) {
		int child = handle, parent = 0;
		short [] x = new short [1], y = new short [1];
		do {
			OS.PtGetAbsPosition (child, x, y);
			rect.ul_x = rect.lr_x = (short) (info.pos_x - x [0]);
			rect.ul_y = rect.lr_y = (short) (info.pos_y - y [0]);
			if ((child = OS.PtHit (child, 1, rect)) == 0) break;
			parent = child;
			if (OS.PtWidgetIsClassMember (child, OS.PtContainer ()) == 0) break;
		} while (child != 0);
		if (parent != 0) {
			do {
				Widget widget = WidgetTable.get (parent);
				if (widget != null && widget instanceof Control) {
					Control control = (Control) widget;
					if (control.getEnabled ()) return control;
					return control;
				}
			} while ((parent = OS.PtWidgetParent (parent)) != 0);
		}
	}
	return null;
}

public Point getCursorLocation () {
	checkDevice ();
	int ig = OS.PhInputGroup (0);
	PhCursorInfo_t info = new PhCursorInfo_t ();
	OS.PhQueryCursor ((short)ig, info);
	return new Point (info.pos_x, info.pos_y);
}

public static synchronized Display getCurrent () {
	Thread current = Thread.currentThread ();
	for (int i=0; i<Displays.length; i++) {
		Display display = Displays [i];
		if (display != null && display.thread == current) return display;
	}
	return null;
}

public static synchronized Display getDefault () {
	if (Default == null) Default = new Display ();
	return Default;
}

public static void setAppName (String name) {
	/* Do nothing */
}

public int getDoubleClickTime () {
	checkDevice ();
	//NOT DONE
	return 250;
}

public Control getFocusControl () {
	checkDevice ();
	int handle = 0;
	while ((handle = OS.PtNextTopLevelWidget (handle)) != 0) {
		int state = OS.PtWindowGetState (handle);
		if (state != -1 && (state & OS.Ph_WM_STATE_ISFOCUS) != 0) {
			int focusHandle = OS.PtContainerFindFocus (handle);
			if (focusHandle != 0) {
				Widget widget = WidgetTable.get (focusHandle);
				if (widget instanceof Control) return (Control) widget;
			}
			return null;
		}
	}
	return null;
}

public int getIconDepth () {
	return getDepth ();
}

public Shell [] getShells () {
	checkDevice ();
	/*
	* NOTE:  Need to check that the shells that belong
	* to another display have not been disposed by the
	* other display's thread as the shells list is being
	* processed.
	*/
	int count = 0;
	Shell [] shells = WidgetTable.shells ();
	for (int i=0; i<shells.length; i++) {
		Shell shell = shells [i];
		if (!shell.isDisposed () && this == shell.getDisplay ()) {
			count++;
		}
	}
	if (count == shells.length) return shells;
	int index = 0;
	Shell [] result = new Shell [count];
	for (int i=0; i<shells.length; i++) {
		Shell shell = shells [i];
		if (!shell.isDisposed () && this == shell.getDisplay ()) {
			result [index++] = shell;
		}
	}
	return result;
}

public Color getSystemColor (int id) {
	checkDevice ();
	int color = 0x000000;
	switch (id) {
		case SWT.COLOR_TITLE_FOREGROUND: 				color = 0x000000; break;
		case SWT.COLOR_TITLE_BACKGROUND:				color = 0x6493E7; break;
		case SWT.COLOR_TITLE_BACKGROUND_GRADIENT:			color = 0x0000FF; break;
		case SWT.COLOR_TITLE_INACTIVE_FOREGROUND:			color = 0x000000; break;
		case SWT.COLOR_TITLE_INACTIVE_BACKGROUND: 		color = 0xABBBD3; break;
		case SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT:	color = 0x0000FF; break;
		case SWT.COLOR_WIDGET_DARK_SHADOW:				color = WIDGET_DARK_SHADOW; break;
		case SWT.COLOR_WIDGET_NORMAL_SHADOW:			color = WIDGET_NORMAL_SHADOW; break;
		case SWT.COLOR_WIDGET_LIGHT_SHADOW: 			color = WIDGET_LIGHT_SHADOW; break;
		case SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW:			color = WIDGET_HIGHLIGHT_SHADOW; break;
		case SWT.COLOR_WIDGET_BACKGROUND: 				color = WIDGET_BACKGROUND; break;
		case SWT.COLOR_WIDGET_FOREGROUND:				color = WIDGET_FOREGROUND; break;
		case SWT.COLOR_WIDGET_BORDER: 				color = WIDGET_BORDER; break;
		case SWT.COLOR_LIST_FOREGROUND: 				color = LIST_FOREGROUND; break;
		case SWT.COLOR_LIST_BACKGROUND: 				color = LIST_BACKGROUND; break;
		case SWT.COLOR_LIST_SELECTION: 				color = LIST_SELECTION; break;
		case SWT.COLOR_LIST_SELECTION_TEXT: 			color = LIST_SELECTION_TEXT; break;
		default:
			return super.getSystemColor (id);
	}
	return Color.photon_new (this, color);
}

public Font getSystemFont () {
	checkDevice ();
	return Font.photon_new (this, TEXT_FONT);
}

public Thread getSyncThread () {
	if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
	return synchronizer.syncThread;
}

public Thread getThread () {
	if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
	return thread;
}

protected void init () {
	super.init ();
	initializeDisplay ();
	initializeWidgetClasses ();
	initializeWidgetColors ();
	initializeWidgetFonts ();
}

void initializeDisplay () {
	windowCallback = new Callback (this, "windowProc", 3);
	windowProc = windowCallback.getAddress ();
	if (windowProc == 0) error (SWT.ERROR_NO_MORE_CALLBACKS);
	drawCallback = new Callback (this, "drawProc", 2);
	drawProc = drawCallback.getAddress ();
	if (drawProc == 0) error (SWT.ERROR_NO_MORE_CALLBACKS);
	workCallback = new Callback (this, "workProc", 1);
	workProc = workCallback.getAddress ();
	if (workProc == 0) error (SWT.ERROR_NO_MORE_CALLBACKS);
	inputCallback = new Callback (this, "inputProc", 4);
	inputProc = inputCallback.getAddress ();
	if (inputProc == 0) error (SWT.ERROR_NO_MORE_CALLBACKS);
	timerCallback = new Callback (this, "timerProc", 3);
	timerProc = timerCallback.getAddress ();
	if (timerProc == 0) error (SWT.ERROR_NO_MORE_CALLBACKS);

	pulse = OS.PtAppCreatePulse (app_context, -1);
	input = OS.PtAppAddInput (app_context, pulse, inputProc, 0);
	int [] args = {
		OS.Pt_ARG_REGION_OPAQUE, 0, ~0,
		OS.Pt_ARG_FILL_COLOR, OS.Pg_TRANSPARENT, 0,
		OS.Pt_ARG_REGION_SENSE, OS.Ph_EV_TIMER, ~0,
	};
	OS.PtSetParentWidget (0);
	timerHandle = OS.PtCreateWidget (OS.PtRegion (), 0, args.length / 3, args);
	if (timerHandle == 0) error (SWT.ERROR_NO_HANDLES);
	OS.PtRealizeWidget (timerHandle);
}

void initializeWidgetClasses () {
	int [] args = {OS.Pt_SET_DRAW_F, drawProc, 0};
	int [] buffer = {
		OS.PtCreateWidgetClass (OS.PtButton (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtList (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtLabel (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtWindow (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtToggleButton (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtComboBox (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtText (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtMultiText (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtScrollbar (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtScrollContainer (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtScrollArea (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtContainer (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtProgress (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtPanelGroup (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtPane (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtTree (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtSlider (), 0, args.length / 3, args), 0, 0,
		OS.PtCreateWidgetClass (OS.PtSeparator (), 0, args.length / 3, args), 0, 0,
	};
	ClassesPtr = OS.malloc (buffer.length * 4);
	OS.memmove (ClassesPtr, buffer, buffer.length * 4);
	PtButton = ClassesPtr;
	PtList = ClassesPtr + 12;
	PtLabel = ClassesPtr + 24;
	PtWindow = ClassesPtr + 36;
	PtToggleButton = ClassesPtr + 48;
	PtComboBox = ClassesPtr + 60;
	PtText = ClassesPtr + 72;
	PtMultiText = ClassesPtr + 84;	
	PtScrollbar = ClassesPtr + 96;
	PtScrollContainer = ClassesPtr + 108;
	PtScrollArea = ClassesPtr + 120;
	PtContainer = ClassesPtr + 132;
	PtProgress = ClassesPtr + 144;
	PtPanelGroup = ClassesPtr + 156;
	PtPane = ClassesPtr + 168;
	PtTree = ClassesPtr + 180;
	PtSlider = ClassesPtr + 192;
	PtSeparator = ClassesPtr + 204;
}

void initializeWidgetColors () {
	OS.PtSetParentWidget (0);
	int shellHandle = OS.PtCreateWidget (OS.PtWindow (), 0, 0, null);
	int [] args = {
		OS.Pt_ARG_COLOR, 0, 0,
		OS.Pt_ARG_FILL_COLOR, 0, 0,
	};
	OS.PtGetResources (shellHandle, args.length / 3, args);
	WIDGET_FOREGROUND = args [1];
	WIDGET_BACKGROUND = args [4];
	int handle = OS.PtCreateWidget (OS.PtList (), shellHandle, 0, null);
	args = new int [] {	
		OS.Pt_ARG_COLOR, 0, 0,
		OS.Pt_ARG_FILL_COLOR, 0, 0,
		OS.Pt_ARG_SELECTION_FILL_COLOR, 0, 0,
		OS.Pt_ARG_SELECTION_TEXT_COLOR, 0, 0,
		OS.Pt_ARG_OUTLINE_COLOR, 0, 0,
		OS.Pt_ARG_OUTLINE_COLOR, 0, 0,
		OS.Pt_ARG_OUTLINE_COLOR, 0, 0,
		OS.Pt_ARG_INLINE_COLOR, 0, 0,
		OS.Pt_ARG_INLINE_COLOR, 0, 0,
	};
	OS.PtGetResources (handle, args.length / 3, args);
	LIST_FOREGROUND = args [1];
	LIST_BACKGROUND = args [4];
	LIST_SELECTION = args [7];
	LIST_SELECTION_TEXT = args [10];
	WIDGET_BORDER = args [13];
	WIDGET_DARK_SHADOW = args [16];
	WIDGET_NORMAL_SHADOW = args [19];
	WIDGET_LIGHT_SHADOW = args [22];
	WIDGET_HIGHLIGHT_SHADOW = args [25];
	OS.PtDestroyWidget (shellHandle);
}

void initializeWidgetFonts () {
	OS.PtSetParentWidget (0);
	int shellHandle = OS.PtCreateWidget (OS.PtWindow (), 0, 0, null);
	int listHandle = OS.PtCreateWidget (OS.PtList (), shellHandle, 0, null);
	int [] args = {OS.Pt_ARG_LIST_FONT, 0, 0};
	OS.PtGetResources (listHandle, args.length / 3, args);
	int count = OS.strlen (args [1]);
	LIST_FONT = new byte [count + 1];
	OS.memmove (LIST_FONT, args [1], count);
	int textHandle = OS.PtCreateWidget (OS.PtText (), shellHandle, 0, null);
	args = new int [] {OS.Pt_ARG_TEXT_FONT, 0, 0};
	OS.PtGetResources (textHandle, args.length / 3, args);
	count = OS.strlen (args [1]);
	TEXT_FONT = new byte [count + 1];
	OS.memmove (TEXT_FONT, args [1], count);
	OS.PtDestroyWidget (shellHandle);
}

int inputProc (int data, int rcvid, int message, int size) {
	if (embedded) {
		runDeferredEvents ();
		runAsyncMessages ();
	}
	return OS.Pt_CONTINUE;
}

public int internal_new_GC (GCData data) {
	int phGC = OS.PgCreateGC(0); // NOTE: PgCreateGC ignores the parameter
	if (phGC == 0) SWT.error(SWT.ERROR_NO_HANDLES);

	data.device = this;
	data.rid = OS.Ph_DEV_RID;
	return phGC;
}

public void internal_dispose_GC (int phGC, GCData data) {
	OS.PgDestroyGC(phGC);
}

boolean isValidThread () {
	return thread == Thread.currentThread ();
}

public Object getData (String key) {
	checkDevice ();
	if (key == null) error (SWT.ERROR_NULL_ARGUMENT);
	if (keys == null) return null;
	for (int i=0; i<keys.length; i++) {
		if (keys [i].equals (key)) return values [i];
	}
	return null;
}

public Object getData () {
	checkDevice ();
	return data;
}
static boolean isValidClass (Class clazz) {
	String name = clazz.getName ();
	int index = name.lastIndexOf ('.');
	return name.substring (0, index + 1).equals (PACKAGE_NAME);
}

void postEvent (Event event) {
	/*
	* Place the event at the end of the event queue.
	* This code is always called in the Display's
	* thread so it must be re-enterant but does not
	* need to be synchronized.
	*/
	if (eventQueue == null) eventQueue = new Event [4];
	int index = 0;
	int length = eventQueue.length;
	while (index < length) {
		if (eventQueue [index] == null) break;
		index++;
	}
	if (index == length) {
		Event [] newQueue = new Event [length + 4];
		System.arraycopy (eventQueue, 0, newQueue, 0, length);
		eventQueue = newQueue;
	}
	eventQueue [index] = event;
	if (embedded) wake ();
}

public boolean readAndDispatch () {
	checkDevice ();
	idle = false;
	int id = OS.PtAppAddWorkProc (app_context, workProc, 0);
	OS.PtAppProcessEvent (app_context);
	OS.PtAppRemoveWorkProc (app_context, id);
	if (!idle) {
		runDeferredEvents ();
		return true;
	}
	return runAsyncMessages ();
}

synchronized void register () {
	for (int i=0; i<Displays.length; i++) {
		if (Displays [i] == null) {
			Displays [i] = this;
			return;
		}
	}
	Display [] newDisplays = new Display [Displays.length + 4];
	System.arraycopy (Displays, 0, newDisplays, 0, Displays.length);
	newDisplays [Displays.length] = this;
	Displays = newDisplays;
}

protected void release () {
	super.release ();

	/* Release shells */
	Shell [] shells = WidgetTable.shells ();
	for (int i=0; i<shells.length; i++) {
		Shell shell = shells [i];
		if (!shell.isDisposed ()) {
			if (this == shell.getDisplay ()) shell.dispose ();
		}
	}
	while (readAndDispatch ()) {};
	
	/* Run dispose list */
	if (disposeList != null) {
		for (int i=0; i<disposeList.length; i++) {
			if (disposeList [i] != null) disposeList [i].run ();
		}
	}
	disposeList = null;
	
	/* Release synchronizer */
	synchronizer.releaseSynchronizer ();
	synchronizer = null;

	releaseDisplay ();
}

void releaseDisplay () {
	
	OS.PtDestroyWidget (timerHandle);
	
	/* Free the classes array */
	OS.free (ClassesPtr);
	
	/* Free pulses and input proc */
	OS.PtAppRemoveInput (app_context, input);
	OS.PtAppDeletePulse (app_context, pulse);
	
	/* Free the window proc */
	windowCallback.dispose ();
	windowCallback = null;

	/* Free the timers */
	if (timers != null) {
		for (int i=0; i<=timers.length; i++) {
			 OS.PtDestroyWidget (timers [i]);
		}
	}
	timers = null;
	timerList = null;
	timerProc = 0;
	timerCallback.dispose ();
	timerCallback = null;
	
	/* Free callbacks */
	drawCallback.dispose();
	drawCallback = null;
	workCallback.dispose();
	workCallback = null;
	inputCallback.dispose();
	inputCallback = null;
		
	/* Release references */
	thread = null;
	data = null;
	keys = null;
	values = null;
}

boolean runAsyncMessages () {
	return synchronizer.runAsyncMessages ();
}

boolean runDeferredEvents () {
	/*
	* Run deferred events.  This code is always
	* called  in the Display's thread so it must
	* be re-enterant need not be synchronized.
	*/
	while (eventQueue != null) {
		
		/* Take an event off the queue */
		Event event = eventQueue [0];
		if (event == null) break;
		int length = eventQueue.length;
		System.arraycopy (eventQueue, 1, eventQueue, 0, --length);
		eventQueue [length] = null;

		/* Run the event */
		Widget widget = event.widget;
		if (widget != null && !widget.isDisposed ()) {
			Widget item = event.item;
			if (item == null || !item.isDisposed ()) {
				widget.notifyListeners (event.type, event);
			}
		}

		/*
		* At this point, the event queue could
		* be null due to a recursive invokation
		* when running the event.
		*/
	}

	/* Clear the queue */
	eventQueue = null;
	return true;
}

public void setData (String key, Object value) {
	checkDevice ();
	if (key == null) error (SWT.ERROR_NULL_ARGUMENT);
	
	/* Remove the key/value pair */
	if (value == null) {
		if (keys == null) return;
		int index = 0;
		while (index < keys.length && !keys [index].equals (key)) index++;
		if (index == keys.length) return;
		if (keys.length == 1) {
			keys = null;
			values = null;
		} else {
			String [] newKeys = new String [keys.length - 1];
			Object [] newValues = new Object [values.length - 1];
			System.arraycopy (keys, 0, newKeys, 0, index);
			System.arraycopy (keys, index + 1, newKeys, index, newKeys.length - index);
			System.arraycopy (values, 0, newValues, 0, index);
			System.arraycopy (values, index + 1, newValues, index, newValues.length - index);
			keys = newKeys;
			values = newValues;
		}
		return;
	}
	
	/* Add the key/value pair */
	if (keys == null) {
		keys = new String [] {key};
		values = new Object [] {value};
		return;
	}
	for (int i=0; i<keys.length; i++) {
		if (keys [i].equals (key)) {
			values [i] = value;
			return;
		}
	}
	String [] newKeys = new String [keys.length + 1];
	Object [] newValues = new Object [values.length + 1];
	System.arraycopy (keys, 0, newKeys, 0, keys.length);
	System.arraycopy (values, 0, newValues, 0, values.length);
	newKeys [keys.length] = key;
	newValues [values.length] = value;
	keys = newKeys;
	values = newValues;
}

public void setData (Object data) {
	checkDevice ();
	this.data = data;
}

public void setSynchronizer (Synchronizer synchronizer) {
	checkDevice ();
	if (synchronizer == null) error (SWT.ERROR_NULL_ARGUMENT);
	if (this.synchronizer != null) {
		this.synchronizer.runAsyncMessages();
	}
	this.synchronizer = synchronizer;
}

public boolean sleep () {
	checkDevice ();
	OS.PtAppProcessEvent (app_context);
	runDeferredEvents ();
	return true;
}

public void syncExec (Runnable runnable) {
	if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
	synchronizer.syncExec (runnable);
}

public void timerExec (int milliseconds, Runnable runnable) {
	checkDevice ();
	if (timerList == null) timerList = new Runnable [4];
	if (timers == null) timers = new int [4];
	int index = 0;
	while (index < timerList.length) {
		if (timerList [index] == null) break;
		index++;
	}
	if (index == timerList.length) {
		Runnable [] newTimerList = new Runnable [timerList.length + 4];
		System.arraycopy (timerList, 0, newTimerList, 0, timerList.length);
		timerList = newTimerList;
		int [] newTimers = new int [timers.length + 4];
		System.arraycopy (timers, 0, newTimers, 0, timers.length);
		timers = newTimers;
	}
	int [] args = {OS.Pt_ARG_TIMER_INITIAL, milliseconds, 0};
	int handle = OS.PtCreateWidget (OS.PtTimer (), timerHandle, args.length / 3, args);
	OS.PtRealizeWidget (handle);
	if (handle != 0) {
		OS.PtAddCallback (handle, OS.Pt_CB_TIMER_ACTIVATE, timerProc, index);
		timerList [index] = runnable;
		timers [index] = handle;
	}

}
int timerProc (int handle, int index, int info) {
	if (timerList == null) return 0;
	if (0 <= index && index < timerList.length) {
		Runnable runnable = timerList [index];
		timerList [index] = null;
		OS.PtDestroyWidget (timers [index]);
		timers [index] = 0;
		if (runnable != null) runnable.run ();
	}
	return 0;
}

static int translateKey (int key) {
	for (int i=0; i<KeyTable.length; i++) {
		if (KeyTable [i] [0] == key) return KeyTable [i] [1];
	}
	return 0;
}

static int untranslateKey (int key) {
	for (int i=0; i<KeyTable.length; i++) {
		if (KeyTable [i] [1] == key) return KeyTable [i] [0];
	}
	return 0;
}

public void update() {
	checkDevice ();
	Shell[] shells = WidgetTable.shells ();
	for (int i=0; i<shells.length; i++) {
		Shell shell = shells [i];
		if (!shell.isDisposed () && this == shell.getDisplay ()) {
			shell.update ();
		}
	}
}

public void wake () {
	if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED);
//	int flags = OS.PtEnter (0);	
	OS.PtAppPulseTrigger (app_context, pulse);
//	if (flags >= 0) OS.PtLeave (flags);
}

int windowProc (int handle, int data, int info) {
	Widget widget = WidgetTable.get (handle);
	if (widget == null) return OS.Pt_CONTINUE;
	return widget.processEvent (data, info);
}

int workProc (int data) {
	idle = true;
	return OS.Pt_CONTINUE;
}

}

Back to the top