comparison dwt/widgets/Widget.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0 f565d3a95c0a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.widgets.Widget;
12
13 import dwt.dwthelper.utils;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.events.DisposeListener;
19 import dwt.internal.DWTEventListener;
20 import dwt.internal.cocoa.NSEvent;
21 import dwt.internal.cocoa.NSRect;
22 import dwt.internal.cocoa.OS;
23 import dwt.internal.cocoa.objc_super;
24
25
26 /**
27 * This class is the abstract superclass of all user interface objects.
28 * Widgets are created, disposed and issue notification to listeners
29 * when events occur which affect them.
30 * <dl>
31 * <dt><b>Styles:</b></dt>
32 * <dd>(none)</dd>
33 * <dt><b>Events:</b></dt>
34 * <dd>Dispose</dd>
35 * </dl>
36 * <p>
37 * IMPORTANT: This class is intended to be subclassed <em>only</em>
38 * within the DWT implementation. However, it has not been marked
39 * final to allow those outside of the DWT development team to implement
40 * patched versions of the class in order to get around specific
41 * limitations in advance of when those limitations can be addressed
42 * by the team. Any class built using subclassing to access the internals
43 * of this class will likely fail to compile or run between releases and
44 * may be strongly platform specific. Subclassing should not be attempted
45 * without an intimate and detailed understanding of the workings of the
46 * hierarchy. No support is provided for user-written classes which are
47 * implemented as subclasses of this class.
48 * </p>
49 *
50 * @see #checkSubclass
51 */
52 public abstract class Widget {
53 int style, state;
54 Display display;
55 EventTable eventTable;
56 Object data;
57
58 int jniRef;
59
60 /* Global state flags */
61 static final int DISPOSED = 1 << 0;
62 static final int CANVAS = 1 << 1;
63 static final int KEYED_DATA = 1 << 2;
64 static final int DISABLED = 1 << 3;
65 static final int HIDDEN = 1 << 4;
66 static final int GRAB = 1 << 5;
67 static final int MOVED = 1 << 6;
68 static final int RESIZED = 1 << 7;
69 static final int EXPANDING = 1 << 8;
70 static final int IGNORE_WHEEL = 1 << 9;
71 static final int PARENT_BACKGROUND = 1 << 10;
72 static final int THEME_BACKGROUND = 1 << 11;
73
74 /* A layout was requested on this widget */
75 static final int LAYOUT_NEEDED = 1<<12;
76
77 /* The preferred size of a child has changed */
78 static final int LAYOUT_CHANGED = 1<<13;
79
80 /* A layout was requested in this widget hierachy */
81 static final int LAYOUT_CHILD = 1<<14;
82
83 /* More global state flags */
84 static final int RELEASED = 1<<15;
85 static final int DISPOSE_SENT = 1<<16;
86 static final int FOREIGN_HANDLE = 1<<17;
87 static final int DRAG_DETECT = 1<<18;
88
89 /* Safari fixes */
90 static final int SAFARI_EVENTS_FIX = 1<<19;
91 static final String SAFARI_EVENTS_FIX_KEY = "dwt.internal.safariEventsFix"; //$NON-NLS-1$
92
93 /* Default size for widgets */
94 static final int DEFAULT_WIDTH = 64;
95 static final int DEFAULT_HEIGHT = 64;
96
97 Widget () {
98 /* Do nothing */
99 }
100
101 /**
102 * Constructs a new instance of this class given its parent
103 * and a style value describing its behavior and appearance.
104 * <p>
105 * The style value is either one of the style constants defined in
106 * class <code>DWT</code> which is applicable to instances of this
107 * class, or must be built by <em>bitwise OR</em>'ing together
108 * (that is, using the <code>int</code> "|" operator) two or more
109 * of those <code>DWT</code> style constants. The class description
110 * lists the style constants that are applicable to the class.
111 * Style bits are also inherited from superclasses.
112 * </p>
113 *
114 * @param parent a widget which will be the parent of the new instance (cannot be null)
115 * @param style the style of widget to construct
116 *
117 * @exception IllegalArgumentException <ul>
118 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
119 * <li>ERROR_INVALID_ARGUMENT - if the parent is disposed</li>
120 * </ul>
121 * @exception DWTException <ul>
122 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
123 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
124 * </ul>
125 *
126 * @see DWT
127 * @see #checkSubclass
128 * @see #getStyle
129 */
130 public Widget (Widget parent, int style) {
131 checkSubclass ();
132 checkParent (parent);
133 this.style = style;
134 display = parent.display;
135 }
136
137 void callSuper(int id, int selector, int arg0) {
138 objc_super super_struct = new objc_super();
139 super_struct.receiver = id;
140 super_struct.cls = OS.objc_msgSend(id, OS.sel_superclass);
141 OS.objc_msgSendSuper(super_struct, selector, arg0);
142 }
143
144 bool acceptsFirstResponder () {
145 return false;
146 }
147
148 bool becomeFirstResponder () {
149 return true;
150 }
151
152 bool resignFirstResponder () {
153 return true;
154 }
155
156 /**
157 * Adds the listener to the collection of listeners who will
158 * be notified when an event of the given type occurs. When the
159 * event does occur in the widget, the listener is notified by
160 * sending it the <code>handleEvent()</code> message. The event
161 * type is one of the event constants defined in class <code>DWT</code>.
162 *
163 * @param eventType the type of event to listen for
164 * @param listener the listener which should be notified when the event occurs
165 *
166 * @exception IllegalArgumentException <ul>
167 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
168 * </ul>
169 * @exception DWTException <ul>
170 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
171 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
172 * </ul>
173 *
174 * @see Listener
175 * @see DWT
176 * @see #removeListener(int, Listener)
177 * @see #notifyListeners
178 */
179 public void addListener (int eventType, Listener listener) {
180 checkWidget();
181 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
182 _addListener (eventType, listener);
183 }
184
185 void _addListener (int eventType, Listener listener) {
186 if (eventTable is null) eventTable = new EventTable ();
187 eventTable.hook (eventType, listener);
188 }
189
190 /**
191 * Adds the listener to the collection of listeners who will
192 * be notified when the widget is disposed. When the widget is
193 * disposed, the listener is notified by sending it the
194 * <code>widgetDisposed()</code> message.
195 *
196 * @param listener the listener which should be notified when the receiver is disposed
197 *
198 * @exception IllegalArgumentException <ul>
199 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
200 * </ul>
201 * @exception DWTException <ul>
202 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
203 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
204 * </ul>
205 *
206 * @see DisposeListener
207 * @see #removeDisposeListener
208 */
209 public void addDisposeListener (DisposeListener listener) {
210 checkWidget();
211 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
212 TypedListener typedListener = new TypedListener (listener);
213 addListener (DWT.Dispose, typedListener);
214 }
215
216 static int checkBits (int style, int int0, int int1, int int2, int int3, int int4, int int5) {
217 int mask = int0 | int1 | int2 | int3 | int4 | int5;
218 if ((style & mask) is 0) style |= int0;
219 if ((style & int0) !is 0) style = (style & ~mask) | int0;
220 if ((style & int1) !is 0) style = (style & ~mask) | int1;
221 if ((style & int2) !is 0) style = (style & ~mask) | int2;
222 if ((style & int3) !is 0) style = (style & ~mask) | int3;
223 if ((style & int4) !is 0) style = (style & ~mask) | int4;
224 if ((style & int5) !is 0) style = (style & ~mask) | int5;
225 return style;
226 }
227
228 void checkOpen () {
229 /* Do nothing */
230 }
231
232 void checkOrientation (Widget parent) {
233 style &= ~DWT.MIRRORED;
234 if ((style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT)) is 0) {
235 if (parent !is null) {
236 if ((parent.style & DWT.LEFT_TO_RIGHT) !is 0) style |= DWT.LEFT_TO_RIGHT;
237 if ((parent.style & DWT.RIGHT_TO_LEFT) !is 0) style |= DWT.RIGHT_TO_LEFT;
238 }
239 }
240 style = checkBits (style, DWT.LEFT_TO_RIGHT, DWT.RIGHT_TO_LEFT, 0, 0, 0, 0);
241 }
242
243 void checkParent (Widget parent) {
244 if (parent is null) error (DWT.ERROR_NULL_ARGUMENT);
245 if (parent.isDisposed ()) error (DWT.ERROR_INVALID_ARGUMENT);
246 parent.checkWidget ();
247 parent.checkOpen ();
248 }
249
250 /**
251 * Checks that this class can be subclassed.
252 * <p>
253 * The DWT class library is intended to be subclassed
254 * only at specific, controlled points (most notably,
255 * <code>Composite</code> and <code>Canvas</code> when
256 * implementing new widgets). This method enforces this
257 * rule unless it is overridden.
258 * </p><p>
259 * <em>IMPORTANT:</em> By providing an implementation of this
260 * method that allows a subclass of a class which does not
261 * normally allow subclassing to be created, the implementer
262 * agrees to be fully responsible for the fact that any such
263 * subclass will likely fail between DWT releases and will be
264 * strongly platform specific. No support is provided for
265 * user-written classes which are implemented in this fashion.
266 * </p><p>
267 * The ability to subclass outside of the allowed DWT classes
268 * is intended purely to enable those not on the DWT development
269 * team to implement patches in order to get around specific
270 * limitations in advance of when those limitations can be
271 * addressed by the team. Subclassing should not be attempted
272 * without an intimate and detailed understanding of the hierarchy.
273 * </p>
274 *
275 * @exception DWTException <ul>
276 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
277 * </ul>
278 */
279 protected void checkSubclass () {
280 if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS);
281 }
282
283 /**
284 * Throws an <code>DWTException</code> if the receiver can not
285 * be accessed by the caller. This may include both checks on
286 * the state of the receiver and more generally on the entire
287 * execution context. This method <em>should</em> be called by
288 * widget implementors to enforce the standard DWT invariants.
289 * <p>
290 * Currently, it is an error to invoke any method (other than
291 * <code>isDisposed()</code>) on a widget that has had its
292 * <code>dispose()</code> method called. It is also an error
293 * to call widget methods from any thread that is different
294 * from the thread that created the widget.
295 * </p><p>
296 * In future releases of DWT, there may be more or fewer error
297 * checks and exceptions may be thrown for different reasons.
298 * </p>
299 *
300 * @exception DWTException <ul>
301 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
302 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
303 * </ul>
304 */
305 protected void checkWidget () {
306 Display display = this.display;
307 if (display is null) error (DWT.ERROR_WIDGET_DISPOSED);
308 if (display.thread !is Thread.currentThread ()) error (DWT.ERROR_THREAD_INVALID_ACCESS);
309 if ((state & DISPOSED) !is 0) error (DWT.ERROR_WIDGET_DISPOSED);
310 }
311
312 bool clickOnLink(int textView, int link, int charIndex) {
313 return true;
314 }
315
316 void comboBoxSelectionDidChange(int notification) {
317 }
318
319 void createHandle () {
320 }
321
322 void createJNIRef () {
323 jniRef = OS.NewGlobalRef(this);
324 if (jniRef is 0) error (DWT.ERROR_NO_HANDLES);
325 }
326
327 void createWidget () {
328 createJNIRef ();
329 createHandle ();
330 }
331
332 void destroyWidget () {
333 releaseHandle ();
334 }
335
336 /**
337 * Disposes of the operating system resources associated with
338 * the receiver and all its descendants. After this method has
339 * been invoked, the receiver and all descendants will answer
340 * <code>true</code> when sent the message <code>isDisposed()</code>.
341 * Any internal connections between the widgets in the tree will
342 * have been removed to facilitate garbage collection.
343 * <p>
344 * NOTE: This method is not called recursively on the descendants
345 * of the receiver. This means that, widget implementers can not
346 * detect when a widget is being disposed of by re-implementing
347 * this method, but should instead listen for the <code>Dispose</code>
348 * event.
349 * </p>
350 *
351 * @exception DWTException <ul>
352 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
353 * </ul>
354 *
355 * @see #addDisposeListener
356 * @see #removeDisposeListener
357 * @see #checkWidget
358 */
359 public void dispose () {
360 /*
361 * Note: It is valid to attempt to dispose a widget
362 * more than once. If this happens, fail silently.
363 */
364 if (isDisposed ()) return;
365 if (!isValidThread ()) error (DWT.ERROR_THREAD_INVALID_ACCESS);
366 release (true);
367 }
368
369 void drawBackground (int control, int context) {
370 /* Do nothing */
371 }
372
373 void drawRect(int id, NSRect rect) {
374 objc_super super_struct = new objc_super();
375 super_struct.receiver = id;
376 super_struct.cls = OS.objc_msgSend(id, OS.sel_superclass);
377 OS.objc_msgSendSuper(super_struct, OS.sel_drawRect_1, rect);
378 }
379
380 void drawWidget (int control, int context, int damageRgn, int visibleRgn, int theEvent) {
381 }
382
383 void error (int code) {
384 DWT.error(code);
385 }
386
387 bool filters (int eventType) {
388 return display.filters (eventType);
389 }
390
391 int fixMnemonic (char [] buffer) {
392 int i=0, j=0;
393 while (i < buffer.length) {
394 if ((buffer [j++] = buffer [i++]) is '&') {
395 if (i is buffer.length) {continue;}
396 if (buffer [i] is '&') {i++; continue;}
397 j--;
398 }
399 }
400 return j;
401 }
402
403 /**
404 * Returns the application defined widget data associated
405 * with the receiver, or null if it has not been set. The
406 * <em>widget data</em> is a single, unnamed field that is
407 * stored with every widget.
408 * <p>
409 * Applications may put arbitrary objects in this field. If
410 * the object stored in the widget data needs to be notified
411 * when the widget is disposed of, it is the application's
412 * responsibility to hook the Dispose event on the widget and
413 * do so.
414 * </p>
415 *
416 * @return the widget data
417 *
418 * @exception DWTException <ul>
419 * <li>ERROR_WIDGET_DISPOSED - when the receiver has been disposed</li>
420 * <li>ERROR_THREAD_INVALID_ACCESS - when called from the wrong thread</li>
421 * </ul>
422 *
423 * @see #setData(Object)
424 */
425 public Object getData () {
426 checkWidget();
427 return (state & KEYED_DATA) !is 0 ? ((Object []) data) [0] : data;
428 }
429
430 /**
431 * Returns the application defined property of the receiver
432 * with the specified name, or null if it has not been set.
433 * <p>
434 * Applications may have associated arbitrary objects with the
435 * receiver in this fashion. If the objects stored in the
436 * properties need to be notified when the widget is disposed
437 * of, it is the application's responsibility to hook the
438 * Dispose event on the widget and do so.
439 * </p>
440 *
441 * @param key the name of the property
442 * @return the value of the property or null if it has not been set
443 *
444 * @exception IllegalArgumentException <ul>
445 * <li>ERROR_NULL_ARGUMENT - if the key is null</li>
446 * </ul>
447 * @exception DWTException <ul>
448 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
449 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
450 * </ul>
451 *
452 * @see #setData(String, Object)
453 */
454 public Object getData (String key) {
455 checkWidget();
456 if (key is null) error (DWT.ERROR_NULL_ARGUMENT);
457 if ((state & KEYED_DATA) !is 0) {
458 Object [] table = (Object []) data;
459 for (int i=1; i<table.length; i+=2) {
460 if (key.equals (table [i])) return table [i+1];
461 }
462 }
463 return null;
464 }
465
466 /**
467 * Returns the <code>Display</code> that is associated with
468 * the receiver.
469 * <p>
470 * A widget's display is either provided when it is created
471 * (for example, top level <code>Shell</code>s) or is the
472 * same as its parent's display.
473 * </p>
474 *
475 * @return the receiver's display
476 *
477 * @exception DWTException <ul>
478 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
479 * </ul>
480 */
481 public Display getDisplay () {
482 Display display = this.display;
483 if (display is null) error (DWT.ERROR_WIDGET_DISPOSED);
484 return display;
485 }
486
487 /**
488 * Returns an array of listeners who will be notified when an event
489 * of the given type occurs. The event type is one of the event constants
490 * defined in class <code>DWT</code>.
491 *
492 * @param eventType the type of event to listen for
493 * @return an array of listeners that will be notified when the event occurs
494 *
495 * @exception DWTException <ul>
496 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
497 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
498 * </ul>
499 *
500 * @see Listener
501 * @see DWT
502 * @see #addListener(int, Listener)
503 * @see #removeListener(int, Listener)
504 * @see #notifyListeners
505 *
506 * @since 3.4
507 */
508 public Listener[] getListeners (int eventType) {
509 checkWidget();
510 if (eventTable is null) return new Listener[0];
511 return eventTable.getListeners(eventType);
512 }
513
514 String getName () {
515 String string = getClass ().getName ();
516 int index = string.lastIndexOf ('.');
517 if (index is -1) return string;
518 return string.substring (index + 1, string.length ());
519 }
520
521 String getNameText () {
522 return "";
523 }
524
525 /**
526 * Returns the receiver's style information.
527 * <p>
528 * Note that the value which is returned by this method <em>may
529 * not match</em> the value which was provided to the constructor
530 * when the receiver was created. This can occur when the underlying
531 * operating system does not support a particular combination of
532 * requested styles. For example, if the platform widget used to
533 * implement a particular DWT widget always has scroll bars, the
534 * result of calling this method would always have the
535 * <code>DWT.H_SCROLL</code> and <code>DWT.V_SCROLL</code> bits set.
536 * </p>
537 *
538 * @return the style bits
539 *
540 * @exception DWTException <ul>
541 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
542 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
543 * </ul>
544 */
545 public int getStyle () {
546 checkWidget();
547 return style;
548 }
549
550 void helpRequested(int theEvent) {
551 }
552
553 bool hooks (int eventType) {
554 if (eventTable is null) return false;
555 return eventTable.hooks (eventType);
556 }
557
558 /**
559 * Returns <code>true</code> if the widget has been disposed,
560 * and <code>false</code> otherwise.
561 * <p>
562 * This method gets the dispose state for the widget.
563 * When a widget has been disposed, it is an error to
564 * invoke any other method using the widget.
565 * </p>
566 *
567 * @return <code>true</code> when the widget is disposed and <code>false</code> otherwise
568 */
569 public bool isDisposed () {
570 return (state & DISPOSED) !is 0;
571 }
572
573 bool isFlipped () {
574 return true;
575 }
576
577 /**
578 * Returns <code>true</code> if there are any listeners
579 * for the specified event type associated with the receiver,
580 * and <code>false</code> otherwise. The event type is one of
581 * the event constants defined in class <code>DWT</code>.
582 *
583 * @param eventType the type of event
584 * @return true if the event is hooked
585 *
586 * @exception DWTException <ul>
587 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
588 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
589 * </ul>
590 *
591 * @see DWT
592 */
593 public bool isListening (int eventType) {
594 checkWidget();
595 return hooks (eventType);
596 }
597
598 bool isTrimHandle (int trimHandle) {
599 return false;
600 }
601
602 bool isValidSubclass () {
603 return Display.isValidClass (getClass ());
604 }
605
606 bool isValidThread () {
607 return getDisplay ().isValidThread ();
608 }
609
610 void flagsChanged(int event) {
611 }
612
613 void mouseDragged(int event) {
614 }
615
616 void mouseDown(int event) {
617 }
618
619 void rightMouseDown(int event) {
620 }
621
622 void mouseEntered(int event) {
623 }
624
625 void mouseUp(int event) {
626 }
627
628 int menuForEvent (int event) {
629 return 0;
630 }
631
632 void menuNeedsUpdate(int menu) {
633 }
634
635 void menu_willHighlightItem(int menu, int item) {
636 }
637
638 void menuWillClose(int menu) {
639 }
640
641 void menuWillOpen(int menu) {
642 }
643
644 int numberOfRowsInTableView(int aTableView) {
645 return 0;
646 }
647
648 int outlineView_child_ofItem(int outlineView, int index, int item) {
649 return 0;
650 }
651
652 int outlineView_objectValueForTableColumn_byItem(int outlineView, int tableColumn, int item) {
653 return 0;
654 }
655
656 bool outlineView_isItemExpandable(int outlineView, int item) {
657 return false;
658 }
659
660 int outlineView_numberOfChildrenOfItem(int outlineView, int item) {
661 return 0;
662 }
663
664 void outlineView_willDisplayCell_forTableColumn_item(int outlineView, int cell, int tableColumn, int item) {
665 }
666
667 bool outlineView_shouldCollapseItem(int outlineView, int item) {
668 return false;
669 }
670
671 bool outlineView_shouldExpandItem(int outlineView, int item) {
672 return false;
673 }
674
675 void outlineViewSelectionDidChange(int notification) {
676 }
677
678 void outlineView_setObjectValue_forTableColumn_byItem(int outlineView, int object, int tableColumn, int item) {
679 }
680
681 /**
682 * Notifies all of the receiver's listeners for events
683 * of the given type that one such event has occurred by
684 * invoking their <code>handleEvent()</code> method. The
685 * event type is one of the event constants defined in class
686 * <code>DWT</code>.
687 *
688 * @param eventType the type of event which has occurred
689 * @param event the event data
690 *
691 * @exception DWTException <ul>
692 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
693 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
694 * </ul>
695 *
696 * @see DWT
697 * @see #addListener
698 * @see #removeListener(int, Listener)
699 */
700 public void notifyListeners (int eventType, Event event) {
701 checkWidget();
702 if (event is null) event = new Event ();
703 sendEvent (eventType, event);
704 }
705
706 void postEvent (int eventType) {
707 sendEvent (eventType, null, false);
708 }
709
710 void postEvent (int eventType, Event event) {
711 sendEvent (eventType, event, false);
712 }
713
714 void release (bool destroy) {
715 if ((state & DISPOSE_SENT) is 0) {
716 state |= DISPOSE_SENT;
717 sendEvent (DWT.Dispose);
718 }
719 if ((state & DISPOSED) is 0) {
720 releaseChildren (destroy);
721 }
722 if ((state & RELEASED) is 0) {
723 state |= RELEASED;
724 if (destroy) {
725 releaseParent ();
726 releaseWidget ();
727 destroyWidget ();
728 } else {
729 releaseWidget ();
730 releaseHandle ();
731 }
732 }
733 }
734
735 void releaseChildren (bool destroy) {
736 }
737
738 void releaseHandle () {
739 state |= DISPOSED;
740 display = null;
741 if (jniRef !is 0) OS.DeleteGlobalRef(jniRef);
742 jniRef = 0;
743 }
744
745 void releaseParent () {
746 /* Do nothing */
747 }
748
749 void releaseWidget () {
750 eventTable = null;
751 data = null;
752 }
753
754 /**
755 * Removes the listener from the collection of listeners who will
756 * be notified when an event of the given type occurs. The event
757 * type is one of the event constants defined in class <code>DWT</code>.
758 *
759 * @param eventType the type of event to listen for
760 * @param listener the listener which should no longer be notified when the event occurs
761 *
762 * @exception IllegalArgumentException <ul>
763 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
764 * </ul>
765 * @exception DWTException <ul>
766 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
767 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
768 * </ul>
769 *
770 * @see Listener
771 * @see DWT
772 * @see #addListener
773 * @see #notifyListeners
774 */
775 public void removeListener (int eventType, Listener handler) {
776 checkWidget();
777 if (handler is null) error (DWT.ERROR_NULL_ARGUMENT);
778 if (eventTable is null) return;
779 eventTable.unhook (eventType, handler);
780 }
781
782 /**
783 * Removes the listener from the collection of listeners who will
784 * be notified when an event of the given type occurs.
785 * <p>
786 * <b>IMPORTANT:</b> This method is <em>not</em> part of the DWT
787 * public API. It is marked public only so that it can be shared
788 * within the packages provided by DWT. It should never be
789 * referenced from application code.
790 * </p>
791 *
792 * @param eventType the type of event to listen for
793 * @param listener the listener which should no longer be notified when the event occurs
794 *
795 * @exception IllegalArgumentException <ul>
796 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
797 * </ul>
798 * @exception DWTException <ul>
799 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
800 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
801 * </ul>
802 *
803 * @see Listener
804 * @see #addListener
805 */
806 protected void removeListener (int eventType, DWTEventListener handler) {
807 checkWidget();
808 if (handler is null) error (DWT.ERROR_NULL_ARGUMENT);
809 if (eventTable is null) return;
810 eventTable.unhook (eventType, handler);
811 }
812
813 /**
814 * Removes the listener from the collection of listeners who will
815 * be notified when the widget is disposed.
816 *
817 * @param listener the listener which should no longer be notified when the receiver is disposed
818 *
819 * @exception IllegalArgumentException <ul>
820 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
821 * </ul>
822 * @exception DWTException <ul>
823 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
824 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
825 * </ul>
826 *
827 * @see DisposeListener
828 * @see #addDisposeListener
829 */
830 public void removeDisposeListener (DisposeListener listener) {
831 checkWidget();
832 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
833 if (eventTable is null) return;
834 eventTable.unhook (DWT.Dispose, listener);
835 }
836
837 void sendArrowSelection () {
838 }
839
840 void sendEvent (Event event) {
841 Display display = event.display;
842 if (!display.filterEvent (event)) {
843 if (eventTable !is null) eventTable.sendEvent (event);
844 }
845 }
846
847 void sendEvent (int eventType) {
848 sendEvent (eventType, null, true);
849 }
850
851 void sendEvent (int eventType, Event event) {
852 sendEvent (eventType, event, true);
853 }
854
855 void sendEvent (int eventType, Event event, bool send) {
856 if (eventTable is null && !display.filters (eventType)) {
857 return;
858 }
859 if (event is null) event = new Event ();
860 event.type = eventType;
861 event.display = display;
862 event.widget = this;
863 if (event.time is 0) {
864 event.time = display.getLastEventTime ();
865 }
866 if (send) {
867 sendEvent (event);
868 } else {
869 display.postEvent (event);
870 }
871 }
872
873 bool sendKeyEvent (int type, int theEvent) {
874 // int [] length = new int [1];
875 // int status = OS.GetEventParameter (theEvent, OS.kEventParamKeyUnicodes, OS.typeUnicodeText, null, 4, length, (char[])null);
876 // if (status is OS.noErr && length [0] > 2) {
877 // int count = 0;
878 // int [] chord = new int [1];
879 // OS.GetEventParameter (theEvent, OS.kEventParamMouseChord, OS.typeUInt32, null, 4, null, chord);
880 // int [] modifiers = new int [1];
881 // OS.GetEventParameter (theEvent, OS.kEventParamKeyModifiers, OS.typeUInt32, null, 4, null, modifiers);
882 // char [] chars = new char [length [0] / 2];
883 // OS.GetEventParameter (theEvent, OS.kEventParamKeyUnicodes, OS.typeUnicodeText, null, chars.length * 2, null, chars);
884 // for (int i=0; i<chars.length; i++) {
885 // Event event = new Event ();
886 // event.character = chars [i];
887 // setInputState (event, type, chord [0], modifiers [0]);
888 // if (sendKeyEvent (type, event)) chars [count++] = chars [i];
889 // }
890 // if (count is 0) return false;
891 // if (count !is chars.length - 1) {
892 // OS.SetEventParameter (theEvent, OS.kEventParamKeyUnicodes, OS.typeUnicodeText, count * 2, chars);
893 // }
894 // return true;
895 // } else {
896 // Event event = new Event ();
897 // if (!setKeyState (event, type, theEvent)) return true;
898 // return sendKeyEvent (type, event);
899 // }
900 return false;
901 }
902
903 bool sendKeyEvent (int type, Event event) {
904 sendEvent (type, event);
905 // widget could be disposed at this point
906
907 /*
908 * It is possible (but unlikely), that application
909 * code could have disposed the widget in the key
910 * events. If this happens, end the processing of
911 * the key by returning false.
912 */
913 if (isDisposed ()) return false;
914 return event.doit;
915 }
916
917 void sendDoubleSelection() {
918 }
919
920 void sendHorizontalSelection () {
921 }
922
923 void sendSelection () {
924 }
925
926 void sendVerticalSelection () {
927 }
928
929 /**
930 * Sets the application defined widget data associated
931 * with the receiver to be the argument. The <em>widget
932 * data</em> is a single, unnamed field that is stored
933 * with every widget.
934 * <p>
935 * Applications may put arbitrary objects in this field. If
936 * the object stored in the widget data needs to be notified
937 * when the widget is disposed of, it is the application's
938 * responsibility to hook the Dispose event on the widget and
939 * do so.
940 * </p>
941 *
942 * @param data the widget data
943 *
944 * @exception DWTException <ul>
945 * <li>ERROR_WIDGET_DISPOSED - when the receiver has been disposed</li>
946 * <li>ERROR_THREAD_INVALID_ACCESS - when called from the wrong thread</li>
947 * </ul>
948 *
949 * @see #getData()
950 */
951 public void setData (Object data) {
952 checkWidget();
953 if (SAFARI_EVENTS_FIX_KEY.equals (data)) {
954 state |= SAFARI_EVENTS_FIX;
955 return;
956 }
957 if ((state & KEYED_DATA) !is 0) {
958 ((Object []) this.data) [0] = data;
959 } else {
960 this.data = data;
961 }
962 }
963
964 /**
965 * Sets the application defined property of the receiver
966 * with the specified name to the given value.
967 * <p>
968 * Applications may associate arbitrary objects with the
969 * receiver in this fashion. If the objects stored in the
970 * properties need to be notified when the widget is disposed
971 * of, it is the application's responsibility to hook the
972 * Dispose event on the widget and do so.
973 * </p>
974 *
975 * @param key the name of the property
976 * @param value the new value for the property
977 *
978 * @exception IllegalArgumentException <ul>
979 * <li>ERROR_NULL_ARGUMENT - if the key is null</li>
980 * </ul>
981 * @exception DWTException <ul>
982 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
983 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
984 * </ul>
985 *
986 * @see #getData(String)
987 */
988 public void setData (String key, Object value) {
989 checkWidget();
990 if (key is null) error (DWT.ERROR_NULL_ARGUMENT);
991 int index = 1;
992 Object [] table = null;
993 if ((state & KEYED_DATA) !is 0) {
994 table = (Object []) data;
995 while (index < table.length) {
996 if (key.equals (table [index])) break;
997 index += 2;
998 }
999 }
1000 if (value !is null) {
1001 if ((state & KEYED_DATA) !is 0) {
1002 if (index is table.length) {
1003 Object [] newTable = new Object [table.length + 2];
1004 System.arraycopy (table, 0, newTable, 0, table.length);
1005 data = table = newTable;
1006 }
1007 } else {
1008 table = new Object [3];
1009 table [0] = data;
1010 data = table;
1011 state |= KEYED_DATA;
1012 }
1013 table [index] = key;
1014 table [index + 1] = value;
1015 } else {
1016 if ((state & KEYED_DATA) !is 0) {
1017 if (index !is table.length) {
1018 int length = table.length - 2;
1019 if (length is 1) {
1020 data = table [0];
1021 state &= ~KEYED_DATA;
1022 } else {
1023 Object [] newTable = new Object [length];
1024 System.arraycopy (table, 0, newTable, 0, index);
1025 System.arraycopy (table, index + 2, newTable, index, length - index);
1026 data = newTable;
1027 }
1028 }
1029 }
1030 }
1031 }
1032
1033 bool setInputState (Event event, int type, int chord, int modifiers) {
1034 // if ((chord & 0x01) !is 0) event.stateMask |= DWT.BUTTON1;
1035 // if ((chord & 0x02) !is 0) event.stateMask |= DWT.BUTTON3;
1036 // if ((chord & 0x04) !is 0) event.stateMask |= DWT.BUTTON2;
1037 // if ((chord & 0x08) !is 0) event.stateMask |= DWT.BUTTON4;
1038 // if ((chord & 0x10) !is 0) event.stateMask |= DWT.BUTTON5;
1039 //
1040 // if ((modifiers & OS.optionKey) !is 0) event.stateMask |= DWT.ALT;
1041 // if ((modifiers & OS.shiftKey) !is 0) event.stateMask |= DWT.SHIFT;
1042 // if ((modifiers & OS.controlKey) !is 0) event.stateMask |= DWT.CONTROL;
1043 // if ((modifiers & OS.cmdKey) !is 0) event.stateMask |= DWT.COMMAND;
1044 // switch (type) {
1045 // case DWT.MouseDown:
1046 // case DWT.MouseDoubleClick:
1047 // if (event.button is 1) event.stateMask &= ~DWT.BUTTON1;
1048 // if (event.button is 2) event.stateMask &= ~DWT.BUTTON2;
1049 // if (event.button is 3) event.stateMask &= ~DWT.BUTTON3;
1050 // if (event.button is 4) event.stateMask &= ~DWT.BUTTON4;
1051 // if (event.button is 5) event.stateMask &= ~DWT.BUTTON5;
1052 // break;
1053 // case DWT.MouseUp:
1054 // if (event.button is 1) event.stateMask |= DWT.BUTTON1;
1055 // if (event.button is 2) event.stateMask |= DWT.BUTTON2;
1056 // if (event.button is 3) event.stateMask |= DWT.BUTTON3;
1057 // if (event.button is 4) event.stateMask |= DWT.BUTTON4;
1058 // if (event.button is 5) event.stateMask |= DWT.BUTTON5;
1059 // break;
1060 // case DWT.KeyDown:
1061 // case DWT.Traverse: {
1062 // if (event.keyCode !is 0 || event.character !is 0) return true;
1063 // int lastModifiers = display.lastModifiers;
1064 // if ((modifiers & OS.alphaLock) !is 0 && (lastModifiers & OS.alphaLock) is 0) {
1065 // event.keyCode = DWT.CAPS_LOCK;
1066 // return true;
1067 // }
1068 // if ((modifiers & OS.shiftKey) !is 0 && (lastModifiers & OS.shiftKey) is 0) {
1069 // event.stateMask &= ~DWT.SHIFT;
1070 // event.keyCode = DWT.SHIFT;
1071 // return true;
1072 // }
1073 // if ((modifiers & OS.controlKey) !is 0 && (lastModifiers & OS.controlKey) is 0) {
1074 // event.stateMask &= ~DWT.CONTROL;
1075 // event.keyCode = DWT.CONTROL;
1076 // return true;
1077 // }
1078 // if ((modifiers & OS.cmdKey) !is 0 && (lastModifiers & OS.cmdKey) is 0) {
1079 // event.stateMask &= ~DWT.COMMAND;
1080 // event.keyCode = DWT.COMMAND;
1081 // return true;
1082 // }
1083 // if ((modifiers & OS.optionKey) !is 0 && (lastModifiers & OS.optionKey) is 0) {
1084 // event.stateMask &= ~DWT.ALT;
1085 // event.keyCode = DWT.ALT;
1086 // return true;
1087 // }
1088 // break;
1089 // }
1090 // case DWT.KeyUp: {
1091 // if (event.keyCode !is 0 || event.character !is 0) return true;
1092 // int lastModifiers = display.lastModifiers;
1093 // if ((modifiers & OS.alphaLock) is 0 && (lastModifiers & OS.alphaLock) !is 0) {
1094 // event.keyCode = DWT.CAPS_LOCK;
1095 // return true;
1096 // }
1097 // if ((modifiers & OS.shiftKey) is 0 && (lastModifiers & OS.shiftKey) !is 0) {
1098 // event.stateMask |= DWT.SHIFT;
1099 // event.keyCode = DWT.SHIFT;
1100 // return true;
1101 // }
1102 // if ((modifiers & OS.controlKey) is 0 && (lastModifiers & OS.controlKey) !is 0) {
1103 // event.stateMask |= DWT.CONTROL;
1104 // event.keyCode = DWT.CONTROL;
1105 // return true;
1106 // }
1107 // if ((modifiers & OS.cmdKey) is 0 && (lastModifiers & OS.cmdKey) !is 0) {
1108 // event.stateMask |= DWT.COMMAND;
1109 // event.keyCode = DWT.COMMAND;
1110 // return true;
1111 // }
1112 // if ((modifiers & OS.optionKey) is 0 && (lastModifiers & OS.optionKey) !is 0) {
1113 // event.stateMask |= DWT.ALT;
1114 // event.keyCode = DWT.ALT;
1115 // return true;
1116 // }
1117 // break;
1118 // }
1119 // }
1120 return true;
1121 }
1122
1123 bool setKeyState (Event event, int type, NSEvent nsEvent) {
1124 // bool isNull = false;
1125 // int [] keyCode = new int [1];
1126 // OS.GetEventParameter (theEvent, OS.kEventParamKeyCode, OS.typeUInt32, null, keyCode.length * 4, null, keyCode);
1127 // event.keyCode = Display.translateKey (keyCode [0]);
1128 switch (event.keyCode) {
1129 // case DWT.LF: {
1130 // /*
1131 // * Feature in the Macintosh. When the numeric key pad
1132 // * Enter key is pressed, it generates '\n'. This is the
1133 // * correct platform behavior but is not portable. The
1134 // * fix is to convert the '\n' into '\r'.
1135 // */
1136 // event.keyCode = DWT.KEYPAD_CR;
1137 // event.character = '\r';
1138 // break;
1139 // }
1140 case DWT.BS: event.character = '\b'; break;
1141 // case DWT.CR: event.character = '\r'; break;
1142 case DWT.DEL: event.character = 0x7F; break;
1143 // case DWT.ESC: event.character = 0x1B; break;
1144 // case DWT.TAB: event.character = '\t'; break;
1145 // default: {
1146 // if (event.keyCode is 0 || (DWT.KEYPAD_MULTIPLY <= event.keyCode && event.keyCode <= DWT.KEYPAD_CR)) {
1147 // int [] length = new int [1];
1148 // int status = OS.GetEventParameter (theEvent, OS.kEventParamKeyUnicodes, OS.typeUnicodeText, null, 4, length, (char[])null);
1149 // if (status is OS.noErr && length [0] !is 0) {
1150 // char [] chars = new char [1];
1151 // OS.GetEventParameter (theEvent, OS.kEventParamKeyUnicodes, OS.typeUnicodeText, null, 2, null, chars);
1152 // event.character = chars [0];
1153 // }
1154 // /*
1155 // * Bug in the Mactonish. For some reason, Ctrl+Shift+'2' and Ctrl+Shift+'6'
1156 // * fail to give 0x0 (^@ or ASCII NUL) and 0x1e (^^). Other control character
1157 // * key sequences such as ^A or even Ctrl+Shift+'-' (^_ or 0x1f) are correctly
1158 // * translated to control characters. Since it is not possible to know which
1159 // * key combination gives '@' on an international keyboard, there is no way to
1160 // * test for either character and convert it to a control character (Shift+'2'
1161 // * gives '@' only on an English keyboard) to work around the problem.
1162 // *
1163 // * There is no fix at this time.
1164 // */
1165 // }
1166 // if (event.keyCode is 0) {
1167 // int kchrPtr = OS.GetScriptManagerVariable ((short) OS.smKCHRCache);
1168 // if (display.kchrPtr !is kchrPtr) {
1169 // display.kchrPtr = kchrPtr;
1170 // display.kchrState [0] = 0;
1171 // }
1172 // int result = OS.KeyTranslate (display.kchrPtr, (short)keyCode [0], display.kchrState);
1173 // if (result <= 0x7f) {
1174 // event.keyCode = result & 0x7f;
1175 // } else {
1176 // int [] encoding = new int [1];
1177 // short keyScript = (short) OS.GetScriptManagerVariable ((short) OS.smKeyScript);
1178 // short regionCode = (short) OS.GetScriptManagerVariable ((short) OS.smRegionCode);
1179 // if (OS.UpgradeScriptInfoToTextEncoding (keyScript, (short) OS.kTextLanguageDontCare, regionCode, null, encoding) is OS.paramErr) {
1180 // if (OS.UpgradeScriptInfoToTextEncoding (keyScript, (short) OS.kTextLanguageDontCare, (short) OS.kTextRegionDontCare, null, encoding) is OS.paramErr) {
1181 // encoding [0] = OS.kTextEncodingMacRoman;
1182 // }
1183 // }
1184 // int [] encodingInfo = new int [1];
1185 // OS.CreateTextToUnicodeInfoByEncoding (encoding [0], encodingInfo);
1186 // if (encodingInfo [0] !is 0) {
1187 // char [] chars = new char [1];
1188 // int [] nchars = new int [1];
1189 // byte [] buffer = new byte [2];
1190 // buffer [0] = 1;
1191 // buffer [1] = (byte) (result & 0xFF);
1192 // OS.ConvertFromPStringToUnicode (encodingInfo [0], buffer, chars.length * 2, nchars, chars);
1193 // OS.DisposeTextToUnicodeInfo (encodingInfo);
1194 // event.keyCode = chars [0];
1195 // }
1196 // }
1197 // }
1198 // break;
1199 // }
1200 }
1201 // if (event.keyCode is 0 && event.character is 0) {
1202 // if (!isNull) return false;
1203 // }
1204 // int [] chord = new int [1];
1205 // OS.GetEventParameter (theEvent, OS.kEventParamMouseChord, OS.typeUInt32, null, 4, null, chord);
1206 // int [] modifiers = new int [1];
1207 // OS.GetEventParameter (theEvent, OS.kEventParamKeyModifiers, OS.typeUInt32, null, 4, null, modifiers);
1208 // return setInputState (event, type, chord [0], modifiers [0]);
1209 return true;
1210 }
1211
1212 void tableViewSelectionDidChange (int aNotification) {
1213 }
1214
1215 int tableView_objectValueForTableColumn_row(int aTableView, int aTableColumn, int rowIndex) {
1216 return 0;
1217 }
1218
1219 void tableView_setObjectValue_forTableColumn_row(int aTableView, int anObject, int aTableColumn, int rowIndex) {
1220 }
1221
1222 bool tableView_shouldEditTableColumn_row(int aTableView, int aTableColumn, int rowIndex) {
1223 return true;
1224 }
1225
1226 void tableView_willDisplayCell_forTableColumn_row(int aTableView, int aCell, int aTableColumn, int rowIndex) {
1227 }
1228
1229 /**
1230 * Returns a string containing a concise, human-readable
1231 * description of the receiver.
1232 *
1233 * @return a string representation of the receiver
1234 */
1235 public String toString () {
1236 String string = "*Disposed*";
1237 if (!isDisposed ()) {
1238 string = "*Wrong Thread*";
1239 if (isValidThread ()) string = getNameText ();
1240 }
1241 return getName () + " {" + string + "}";
1242 }
1243
1244 void willSelectTabViewItem(int tabView, int tabViewItem) {
1245 }
1246
1247 void windowDidMove(int notification) {
1248 }
1249
1250 void windowDidResize(int notification) {
1251 }
1252
1253 void windowDidResignKey(int notification) {
1254 }
1255
1256 void windowDidBecomeKey(int notification) {
1257 }
1258
1259 void windowSendEvent(int id, int event) {
1260 callSuper(id, OS.sel_sendEvent_1, event);
1261 }
1262
1263 bool windowShouldClose(int window) {
1264 return false;
1265 }
1266
1267 void windowWillClose(int notification) {
1268 }
1269
1270 }