comparison dwt/widgets/Widget.d @ 14:762fbe6f17d3

Ported dwt.widgets.Widget
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 23 Aug 2008 13:49:00 +0200
parents f565d3a95c0a
children 5b53d338c709
comparison
equal deleted inserted replaced
13:f565d3a95c0a 14:762fbe6f17d3
11 * Port to the D programming language: 11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com> 12 * Jacob Carlborg <jacob.carlborg@gmail.com>
13 *******************************************************************************/ 13 *******************************************************************************/
14 module dwt.widgets.Widget; 14 module dwt.widgets.Widget;
15 15
16 import dwt.dwthelper.utils; 16
17 17 import tango.core.Thread;
18 18
19 import dwt.DWT; 19 import dwt.DWT;
20 import dwt.DWTException; 20 import dwt.DWTException;
21 import dwt.events.DisposeListener; 21 import dwt.events.DisposeListener;
22 import dwt.internal.DWTEventListener; 22 import dwt.internal.DWTEventListener;
23 import dwt.internal.cocoa.NSEvent; 23 import dwt.internal.cocoa.NSEvent;
24 import dwt.internal.cocoa.NSRect; 24 import dwt.internal.cocoa.NSRect;
25 import dwt.internal.cocoa.OS; 25 import dwt.internal.cocoa.OS;
26 import dwt.internal.cocoa.objc_super; 26 import dwt.internal.cocoa.objc_super;
27 27
28 import dwt.dwthelper.utils;
29 import objc = dwt.internal.objc.runtime;
30
31 import dwt.widgets.Display;
32 import dwt.widgets.Event;
33 import dwt.widgets.EventTable;
34 import dwt.widgets.Listener;
35 import dwt.widgets.TypedListener;
28 36
29 /** 37 /**
30 * This class is the abstract superclass of all user interface objects. 38 * This class is the abstract superclass of all user interface objects.
31 * Widgets are created, disposed and issue notification to listeners 39 * Widgets are created, disposed and issue notification to listeners
32 * when events occur which affect them. 40 * when events occur which affect them.
59 Object data; 67 Object data;
60 68
61 int jniRef; 69 int jniRef;
62 70
63 /* Global state flags */ 71 /* Global state flags */
64 static final int DISPOSED = 1 << 0; 72 static const int DISPOSED = 1 << 0;
65 static final int CANVAS = 1 << 1; 73 static const int CANVAS = 1 << 1;
66 static final int KEYED_DATA = 1 << 2; 74 static const int KEYED_DATA = 1 << 2;
67 static final int DISABLED = 1 << 3; 75 static const int DISABLED = 1 << 3;
68 static final int HIDDEN = 1 << 4; 76 static const int HIDDEN = 1 << 4;
69 static final int GRAB = 1 << 5; 77 static const int GRAB = 1 << 5;
70 static final int MOVED = 1 << 6; 78 static const int MOVED = 1 << 6;
71 static final int RESIZED = 1 << 7; 79 static const int RESIZED = 1 << 7;
72 static final int EXPANDING = 1 << 8; 80 static const int EXPANDING = 1 << 8;
73 static final int IGNORE_WHEEL = 1 << 9; 81 static const int IGNORE_WHEEL = 1 << 9;
74 static final int PARENT_BACKGROUND = 1 << 10; 82 static const int PARENT_BACKGROUND = 1 << 10;
75 static final int THEME_BACKGROUND = 1 << 11; 83 static const int THEME_BACKGROUND = 1 << 11;
76 84
77 /* A layout was requested on this widget */ 85 /* A layout was requested on this widget */
78 static final int LAYOUT_NEEDED = 1<<12; 86 static const int LAYOUT_NEEDED = 1<<12;
79 87
80 /* The preferred size of a child has changed */ 88 /* The preferred size of a child has changed */
81 static final int LAYOUT_CHANGED = 1<<13; 89 static const int LAYOUT_CHANGED = 1<<13;
82 90
83 /* A layout was requested in this widget hierachy */ 91 /* A layout was requested in this widget hierachy */
84 static final int LAYOUT_CHILD = 1<<14; 92 static const int LAYOUT_CHILD = 1<<14;
85 93
86 /* More global state flags */ 94 /* More global state flags */
87 static final int RELEASED = 1<<15; 95 static const int RELEASED = 1<<15;
88 static final int DISPOSE_SENT = 1<<16; 96 static const int DISPOSE_SENT = 1<<16;
89 static final int FOREIGN_HANDLE = 1<<17; 97 static const int FOREIGN_HANDLE = 1<<17;
90 static final int DRAG_DETECT = 1<<18; 98 static const int DRAG_DETECT = 1<<18;
91 99
92 /* Safari fixes */ 100 /* Safari fixes */
93 static final int SAFARI_EVENTS_FIX = 1<<19; 101 static const int SAFARI_EVENTS_FIX = 1<<19;
94 static final String SAFARI_EVENTS_FIX_KEY = "dwt.internal.safariEventsFix"; //$NON-NLS-1$ 102 static const String SAFARI_EVENTS_FIX_KEY = "dwt.internal.safariEventsFix"; //$NON-NLS-1$
95 103
96 /* Default size for widgets */ 104 /* Default size for widgets */
97 static final int DEFAULT_WIDTH = 64; 105 static const int DEFAULT_WIDTH = 64;
98 static final int DEFAULT_HEIGHT = 64; 106 static const int DEFAULT_HEIGHT = 64;
99 107
100 Widget () { 108 this () {
101 /* Do nothing */ 109 /* Do nothing */
102 } 110 }
103 111
104 /** 112 /**
105 * Constructs a new instance of this class given its parent 113 * Constructs a new instance of this class given its parent
128 * 136 *
129 * @see DWT 137 * @see DWT
130 * @see #checkSubclass 138 * @see #checkSubclass
131 * @see #getStyle 139 * @see #getStyle
132 */ 140 */
133 public Widget (Widget parent, int style) { 141 public this (Widget parent, int style) {
134 checkSubclass (); 142 checkSubclass ();
135 checkParent (parent); 143 checkParent (parent);
136 this.style = style; 144 this.style = style;
137 display = parent.display; 145 display = parent.display;
138 } 146 }
139 147
140 void callSuper(int id, int selector, int arg0) { 148 void callSuper(objc.id id, int selector, int arg0) {
141 objc_super super_struct = new objc_super(); 149 objc_super super_struct;
142 super_struct.receiver = id; 150 super_struct.receiver = id;
143 super_struct.cls = OS.objc_msgSend(id, OS.sel_superclass); 151 super_struct.cls = OS.objc_msgSend(id, OS.sel_superclass);
144 OS.objc_msgSendSuper(super_struct, selector, arg0); 152 OS.objc_msgSendSuper(super_struct, selector, arg0);
145 } 153 }
146 154
306 * </ul> 314 * </ul>
307 */ 315 */
308 protected void checkWidget () { 316 protected void checkWidget () {
309 Display display = this.display; 317 Display display = this.display;
310 if (display is null) error (DWT.ERROR_WIDGET_DISPOSED); 318 if (display is null) error (DWT.ERROR_WIDGET_DISPOSED);
311 if (display.thread !is Thread.currentThread ()) error (DWT.ERROR_THREAD_INVALID_ACCESS); 319 if (display.thread !is Thread.getThis ()) error (DWT.ERROR_THREAD_INVALID_ACCESS);
312 if ((state & DISPOSED) !is 0) error (DWT.ERROR_WIDGET_DISPOSED); 320 if ((state & DISPOSED) !is 0) error (DWT.ERROR_WIDGET_DISPOSED);
313 } 321 }
314 322
315 bool clickOnLink(int textView, int link, int charIndex) { 323 bool clickOnLink(int textView, int link, int charIndex) {
316 return true; 324 return true;
320 } 328 }
321 329
322 void createHandle () { 330 void createHandle () {
323 } 331 }
324 332
325 void createJNIRef () { 333 /*void createJNIRef () {
326 jniRef = OS.NewGlobalRef(this); 334 jniRef = OS.NewGlobalRef(this);
327 if (jniRef is 0) error (DWT.ERROR_NO_HANDLES); 335 if (jniRef is 0) error (DWT.ERROR_NO_HANDLES);
328 } 336 }*/
329 337
330 void createWidget () { 338 void createWidget () {
331 createJNIRef (); 339 //createJNIRef ();
332 createHandle (); 340 createHandle ();
333 } 341 }
334 342
335 void destroyWidget () { 343 void destroyWidget () {
336 releaseHandle (); 344 releaseHandle ();
371 379
372 void drawBackground (int control, int context) { 380 void drawBackground (int control, int context) {
373 /* Do nothing */ 381 /* Do nothing */
374 } 382 }
375 383
376 void drawRect(int id, NSRect rect) { 384 void drawRect(objc.id id, NSRect rect) {
377 objc_super super_struct = new objc_super(); 385 objc_super super_struct;
378 super_struct.receiver = id; 386 super_struct.receiver = id;
379 super_struct.cls = OS.objc_msgSend(id, OS.sel_superclass); 387 super_struct.cls = OS.objc_msgSend(id, OS.sel_superclass);
380 OS.objc_msgSendSuper(super_struct, OS.sel_drawRect_1, rect); 388 OS.objc_msgSendSuper(super_struct, OS.sel_drawRect_1, rect);
381 } 389 }
382 390
425 * 433 *
426 * @see #setData(Object) 434 * @see #setData(Object)
427 */ 435 */
428 public Object getData () { 436 public Object getData () {
429 checkWidget(); 437 checkWidget();
430 return (state & KEYED_DATA) !is 0 ? ((Object []) data) [0] : data; 438 return (state & KEYED_DATA) !is 0 ? (cast(ArrayWrapperObject) data).array [0] : data;
431 } 439 }
432 440
433 /** 441 /**
434 * Returns the application defined property of the receiver 442 * Returns the application defined property of the receiver
435 * with the specified name, or null if it has not been set. 443 * with the specified name, or null if it has not been set.
454 * 462 *
455 * @see #setData(String, Object) 463 * @see #setData(String, Object)
456 */ 464 */
457 public Object getData (String key) { 465 public Object getData (String key) {
458 checkWidget(); 466 checkWidget();
459 if (key is null) error (DWT.ERROR_NULL_ARGUMENT); 467 //if (key is null) error (DWT.ERROR_NULL_ARGUMENT);
460 if ((state & KEYED_DATA) !is 0) { 468 if ((state & KEYED_DATA) !is 0) {
461 Object [] table = (Object []) data; 469 Object [] table = (cast(ArrayWrapperObject) data).array;
462 for (int i=1; i<table.length; i+=2) { 470 for (int i=1; i<table.length; i+=2) {
463 if (key.equals (table [i])) return table [i+1]; 471 String tablekey = (cast(ArrayWrapperString) table[i]).array;
472 if (key.equals (tablekey)) return table [i+1];
464 } 473 }
465 } 474 }
466 return null; 475 return null;
467 } 476 }
468 477
513 if (eventTable is null) return new Listener[0]; 522 if (eventTable is null) return new Listener[0];
514 return eventTable.getListeners(eventType); 523 return eventTable.getListeners(eventType);
515 } 524 }
516 525
517 String getName () { 526 String getName () {
518 String string = getClass ().getName (); 527 String string = this.classinfo.name;
519 int index = string.lastIndexOf ('.'); 528 int index = string.lastIndexOf ('.');
520 if (index is -1) return string; 529 if (index is -1) return string;
521 return string.substring (index + 1, string.length ()); 530 return string.substring (index + 1, string.length ());
522 } 531 }
523 532
739 } 748 }
740 749
741 void releaseHandle () { 750 void releaseHandle () {
742 state |= DISPOSED; 751 state |= DISPOSED;
743 display = null; 752 display = null;
744 if (jniRef !is 0) OS.DeleteGlobalRef(jniRef); 753 //if (jniRef !is 0) OS.DeleteGlobalRef(jniRef);
745 jniRef = 0; 754 //jniRef = 0;
746 } 755 }
747 756
748 void releaseParent () { 757 void releaseParent () {
749 /* Do nothing */ 758 /* Do nothing */
750 } 759 }
956 if (SAFARI_EVENTS_FIX_KEY.equals (data)) { 965 if (SAFARI_EVENTS_FIX_KEY.equals (data)) {
957 state |= SAFARI_EVENTS_FIX; 966 state |= SAFARI_EVENTS_FIX;
958 return; 967 return;
959 } 968 }
960 if ((state & KEYED_DATA) !is 0) { 969 if ((state & KEYED_DATA) !is 0) {
961 ((Object []) this.data) [0] = data; 970 (cast(ArrayWrapperObject) this.data).array [0] = data;
962 } else { 971 } else {
963 this.data = data; 972 this.data = data;
964 } 973 }
965 } 974 }
966 975
988 * 997 *
989 * @see #getData(String) 998 * @see #getData(String)
990 */ 999 */
991 public void setData (String key, Object value) { 1000 public void setData (String key, Object value) {
992 checkWidget(); 1001 checkWidget();
993 if (key is null) error (DWT.ERROR_NULL_ARGUMENT); 1002 //if (key is null) error (DWT.ERROR_NULL_ARGUMENT);
994 int index = 1; 1003 int index = 1;
995 Object [] table = null; 1004 Object [] table = null;
996 if ((state & KEYED_DATA) !is 0) { 1005 if ((state & KEYED_DATA) !is 0) {
997 table = (Object []) data; 1006 table = (cast(ArrayWrapperObject) data).array;
998 while (index < table.length) { 1007 while (index < table.length) {
999 if (key.equals (table [index])) break; 1008 if (key.equals (table [index])) break;
1000 index += 2; 1009 index += 2;
1001 } 1010 }
1002 } 1011 }
1257 } 1266 }
1258 1267
1259 void windowDidBecomeKey(int notification) { 1268 void windowDidBecomeKey(int notification) {
1260 } 1269 }
1261 1270
1262 void windowSendEvent(int id, int event) { 1271 void windowSendEvent(objc.id id, int event) {
1263 callSuper(id, OS.sel_sendEvent_1, event); 1272 callSuper(objc.id, OS.sel_sendEvent_1, event);
1264 } 1273 }
1265 1274
1266 bool windowShouldClose(int window) { 1275 bool windowShouldClose(int window) {
1267 return false; 1276 return false;
1268 } 1277 }