comparison dwt/widgets/Shell.d @ 20:453387bb0706

Ported dwt.widgets.Shell
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sun, 07 Sep 2008 18:22:28 +0200
parents fba856099f87
children 642f460a0908
comparison
equal deleted inserted replaced
19:fba856099f87 20:453387bb0706
10 * 10 *
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.Shell; 14 module dwt.widgets.Shell;
15
16 import dwt.dwthelper.utils;
17 15
18 import dwt.DWT; 16 import dwt.DWT;
19 import dwt.DWTException; 17 import dwt.DWTException;
20 import dwt.events.ShellListener; 18 import dwt.events.ShellListener;
21 import dwt.graphics.Cursor; 19 import dwt.graphics.Cursor;
32 import dwt.internal.cocoa.NSView; 30 import dwt.internal.cocoa.NSView;
33 import dwt.internal.cocoa.NSWindow; 31 import dwt.internal.cocoa.NSWindow;
34 import dwt.internal.cocoa.OS; 32 import dwt.internal.cocoa.OS;
35 import dwt.internal.cocoa.SWTWindow; 33 import dwt.internal.cocoa.SWTWindow;
36 import dwt.internal.cocoa.SWTWindowDelegate; 34 import dwt.internal.cocoa.SWTWindowDelegate;
35
36 import dwt.dwthelper.utils;
37 import dwt.internal.cocoa.NSInteger;
38 import objc = dwt.internal.objc.runtime;
39 import dwt.widgets.Composite;
40 import dwt.widgets.Control;
41 import dwt.widgets.Decorations;
42 import dwt.widgets.Display;
43 import dwt.widgets.Event;
44 import dwt.widgets.EventTable;
45 import dwt.widgets.Layout;
46 import dwt.widgets.Listener;
47 import dwt.widgets.Menu;
48 import dwt.widgets.Monitor;
49 import dwt.widgets.TypedListener;
50 import dwt.widgets.Widget;
37 51
38 /** 52 /**
39 * Instances of this class represent the "windows" 53 * Instances of this class represent the "windows"
40 * which the desktop or "window manager" is managing. 54 * which the desktop or "window manager" is managing.
41 * Instances that do not have a parent (that is, they 55 * Instances that do not have a parent (that is, they
263 */ 277 */
264 public this (Display display, int style) { 278 public this (Display display, int style) {
265 this (display, null, style, 0, false); 279 this (display, null, style, 0, false);
266 } 280 }
267 281
268 this (Display display, Shell parent, int style, int handle, bool embedded) { 282 this (Display display, Shell parent, int style, objc.id handle, bool embedded) {
269 super (); 283 super ();
270 checkSubclass (); 284 checkSubclass ();
271 if (display is null) display = Display.getCurrent (); 285 if (display is null) display = Display.getCurrent ();
272 if (display is null) display = Display.getDefault (); 286 if (display is null) display = Display.getDefault ();
273 if (!display.isValidThread ()) { 287 if (!display.isValidThread ()) {
277 error (DWT.ERROR_INVALID_ARGUMENT); 291 error (DWT.ERROR_INVALID_ARGUMENT);
278 } 292 }
279 this.style = checkStyle (style); 293 this.style = checkStyle (style);
280 this.parent = parent; 294 this.parent = parent;
281 this.display = display; 295 this.display = display;
282 if (handle !is 0) { 296 if (handle !is null) {
283 if (embedded) { 297 if (embedded) {
284 view = new NSView(handle); 298 view = new NSView(handle);
285 } else { 299 } else {
286 window = new NSWindow(handle); 300 window = new NSWindow(handle);
287 state |= FOREIGN_HANDLE; 301 state |= FOREIGN_HANDLE;
365 */ 379 */
366 public this (Shell parent, int style) { 380 public this (Shell parent, int style) {
367 this (parent !is null ? parent.display : null, parent, style, 0, false); 381 this (parent !is null ? parent.display : null, parent, style, 0, false);
368 } 382 }
369 383
370 public static Shell internal_new (Display display, int handle) { 384 public static Shell internal_new (Display display, objc.id handle) {
371 return new Shell (display, null, DWT.NO_TRIM, handle, false); 385 return new Shell (display, null, DWT.NO_TRIM, handle, false);
372 } 386 }
373 387
374 static int checkStyle (int style) { 388 static int checkStyle (int style) {
375 style = Decorations.checkStyle (style); 389 style = Decorations.checkStyle (style);
452 } 466 }
453 467
454 public Rectangle computeTrim (int x, int y, int width, int height) { 468 public Rectangle computeTrim (int x, int y, int width, int height) {
455 checkWidget(); 469 checkWidget();
456 Rectangle trim = super.computeTrim(x, y, width, height); 470 Rectangle trim = super.computeTrim(x, y, width, height);
457 NSRect rect = new NSRect (); 471 NSRect rect = NSRect ();
458 rect.x = trim.x; 472 rect.x = trim.x;
459 rect.y = trim.y; 473 rect.y = trim.y;
460 rect.width = trim.width; 474 rect.width = trim.width;
461 rect.height = trim.height; 475 rect.height = trim.height;
462 rect = NSWindow.static_frameRectForContentRect_styleMask_(rect, window.styleMask()); 476 rect = NSWindow.static_frameRectForContentRect_styleMask_(rect, window.styleMask());
467 state |= CANVAS;// | GRAB | HIDDEN; 481 state |= CANVAS;// | GRAB | HIDDEN;
468 if (window !is null) { 482 if (window !is null) {
469 view = window.contentView(); 483 view = window.contentView();
470 return; 484 return;
471 } else { 485 } else {
472 SWTWindow swtWindow = cast(SWTWindow) new SWTWindow ().alloc (); 486 SWTWindow swtWindow = cast(SWTWindow) (new SWTWindow ()).alloc ();
473 swtWindow.setTag(jniRef); 487 swtWindow.setTag(jniRef);
474 window = cast(NSWindow)swtWindow; 488 window = cast(NSWindow)swtWindow;
475 NSRect rect = new NSRect(); 489 NSRect rect = NSRect();
476 Monitor monitor = getMonitor (); 490 dwt.widgets.Monitor.Monitor monitor = getMonitor ();
477 Rectangle clientArea = monitor.getClientArea (); 491 Rectangle clientArea = monitor.getClientArea ();
478 rect.width = clientArea.width * 5 / 8; 492 rect.width = clientArea.width * 5 / 8;
479 rect.height = clientArea.height * 5 / 8; 493 rect.height = clientArea.height * 5 / 8;
480 int styleMask = OS.NSBorderlessWindowMask; 494 int styleMask = OS.NSBorderlessWindowMask;
481 if ((style & DWT.NO_TRIM) is 0) { 495 if ((style & DWT.NO_TRIM) is 0) {
493 } 507 }
494 508
495 createHandle (null); 509 createHandle (null);
496 510
497 window.setContentView (topView()); 511 window.setContentView (topView());
498 windowDelegate = cast(SWTWindowDelegate)new SWTWindowDelegate().alloc().init(); 512 windowDelegate = cast(SWTWindowDelegate)(new SWTWindowDelegate()).alloc().init();
499 windowDelegate.setTag(jniRef); 513 windowDelegate.setTag(jniRef);
500 window.setDelegate(windowDelegate); 514 window.setDelegate(windowDelegate);
501 } 515 }
502 516
503 void destroyWidget () { 517 void destroyWidget () {
521 void fixShell (Shell newShell, Control control) { 535 void fixShell (Shell newShell, Control control) {
522 if (this is newShell) return; 536 if (this is newShell) return;
523 // if (control is lastActive) setActiveControl (null); 537 // if (control is lastActive) setActiveControl (null);
524 } 538 }
525 539
526 void flagsChanged(int theEvent) { 540 void flagsChanged(objc.id theEvent) {
527 Display display = this.display; 541 Display display = this.display;
528 NSEvent nsEvent = new NSEvent(theEvent); 542 NSEvent nsEvent = new NSEvent(theEvent);
529 int modifiers = nsEvent.modifierFlags(); 543 NSUInteger modifiers = nsEvent.modifierFlags();
530 int lastModifiers = display.lastModifiers; 544 int lastModifiers = display.lastModifiers;
531 // int chord = OS.GetCurrentEventButtonState (); 545 // int chord = OS.GetCurrentEventButtonState ();
532 int type = DWT.KeyUp; 546 int type = DWT.KeyUp;
533 if ((modifiers & OS.NSAlphaShiftKeyMask) !is 0 && (lastModifiers & OS.NSAlphaShiftKeyMask) is 0) type = DWT.KeyDown; 547 if ((modifiers & OS.NSAlphaShiftKeyMask) !is 0 && (lastModifiers & OS.NSAlphaShiftKeyMask) is 0) type = DWT.KeyDown;
534 if ((modifiers & OS.NSAlternateKeyMask) !is 0 && (lastModifiers & OS.NSAlternateKeyMask) is 0) type = DWT.KeyDown; 548 if ((modifiers & OS.NSAlternateKeyMask) !is 0 && (lastModifiers & OS.NSAlternateKeyMask) is 0) type = DWT.KeyDown;
602 checkWidget(); 616 checkWidget();
603 //TODO why super implementation fails 617 //TODO why super implementation fails
604 NSRect rect = window.contentRectForFrameRect_(window.frame()); 618 NSRect rect = window.contentRectForFrameRect_(window.frame());
605 int width = cast(int)rect.width, height = cast(int)rect.height; 619 int width = cast(int)rect.width, height = cast(int)rect.height;
606 if (scrollView !is null) { 620 if (scrollView !is null) {
607 NSSize size = new NSSize(); 621 NSSize size = NSSize();
608 size.width = width; 622 size.width = width;
609 size.height = height; 623 size.height = height;
610 size = NSScrollView.contentSizeForFrameSize(size, (style & DWT.H_SCROLL) !is 0, (style & DWT.V_SCROLL) !is 0, OS.NSNoBorder); 624 size = NSScrollView.contentSizeForFrameSize(size, (style & DWT.H_SCROLL) !is 0, (style & DWT.V_SCROLL) !is 0, OS.NSNoBorder);
611 width = cast(int)size.width; 625 width = cast(int)size.width;
612 height = cast(int)size.height; 626 height = cast(int)size.height;
970 } 984 }
971 985
972 int setBounds (int x, int y, int width, int height, bool move, bool resize) { 986 int setBounds (int x, int y, int width, int height, bool move, bool resize) {
973 // if (fullScreen) setFullScreen (false); 987 // if (fullScreen) setFullScreen (false);
974 if (move && resize) { 988 if (move && resize) {
975 NSRect rect = new NSRect (); 989 NSRect rect = NSRect ();
976 rect.x = x; 990 rect.x = x;
977 //TODO - get the screen for the point 991 //TODO - get the screen for the point
978 int screenHeight = cast(int) window.screen().frame().height; 992 int screenHeight = cast(int) window.screen().frame().height;
979 rect.y = screenHeight - y; 993 rect.y = screenHeight - y;
980 rect.width = width; 994 rect.width = width;
981 rect.height = height; 995 rect.height = height;
982 window.setFrame_display_(rect, false); 996 window.setFrame_display_(rect, false);
983 } else { 997 } else {
984 if (move) { 998 if (move) {
985 NSPoint point = new NSPoint(); 999 NSPoint point = NSPoint();
986 point.x = x; 1000 point.x = x;
987 //TODO - get the screen for the point 1001 //TODO - get the screen for the point
988 int screenHeight = cast(int) window.screen().frame().height; 1002 int screenHeight = cast(int) window.screen().frame().height;
989 point.y = screenHeight - y; 1003 point.y = screenHeight - y;
990 window.setFrameTopLeftPoint (point); 1004 window.setFrameTopLeftPoint (point);
1213 // OS.ReshapeCustomWindow (shellHandle); 1227 // OS.ReshapeCustomWindow (shellHandle);
1214 // redrawWidget (handle, true); 1228 // redrawWidget (handle, true);
1215 // } 1229 // }
1216 } 1230 }
1217 1231
1218 public void setText (String string) { 1232 public void setText (String str) {
1219 checkWidget(); 1233 checkWidget();
1220 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); 1234 if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
1221 super.setText (string); 1235 super.setText (str);
1222 NSString str = NSString.stringWith(string); 1236 NSString nsStr = NSString.stringWith(str);
1223 window.setTitle(str); 1237 window.setTitle(nsStr);
1224 // str.release(); 1238 // str.release();
1225 } 1239 }
1226 1240
1227 public void setVisible (bool visible) { 1241 public void setVisible (bool visible) {
1228 checkWidget(); 1242 checkWidget();
1357 } 1371 }
1358 1372
1359 void windowWillClose(int notification) { 1373 void windowWillClose(int notification) {
1360 } 1374 }
1361 1375
1362 void windowSendEvent(int id, int event) { 1376 void windowSendEvent(objc.id ID, objc.id event) {
1363 NSEvent nsEvent = new NSEvent(event); 1377 NSEvent nsEvent = new NSEvent(event);
1364 int type = nsEvent.type(); 1378 NSEventType type = nsEvent.type();
1365 if (type is OS.NSKeyDown || type is OS.NSKeyUp) { 1379 if (type is OS.NSKeyDown || type is OS.NSKeyUp) {
1366 Control eventTarget = display.getFocusControl(); 1380 Control eventTarget = display.getFocusControl();
1367 if (eventTarget !is null) { 1381 if (eventTarget !is null) {
1368 if (type is OS.NSKeyDown) { 1382 if (type is OS.NSKeyDown) {
1369 bool[] consume = new bool[1]; 1383 bool[] consume = new bool[1];
1370 int key = nsEvent.keyCode(); 1384 short key = nsEvent.keyCode();
1371 if (eventTarget.translateTraversal(key, nsEvent, consume)) return; 1385 if (eventTarget.translateTraversal(key, nsEvent, consume)) return;
1372 if (consume[0]) return; 1386 if (consume[0]) return;
1373 if (eventTarget.isDisposed()) return; 1387 if (eventTarget.isDisposed()) return;
1374 } 1388 }
1375 if (!eventTarget.sendKeyEvent(nsEvent, type is OS.NSKeyDown ? DWT.KeyDown : DWT.KeyUp)) return; 1389 if (!eventTarget.sendKeyEvent(nsEvent, type is OS.NSKeyDown ? DWT.KeyDown : DWT.KeyUp)) return;