]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
allow NULL parent menu
[wxWidgets.git] / src / osx / cocoa / nonownedwnd.mm
CommitLineData
33e90275 1/////////////////////////////////////////////////////////////////////////////
0f9b48d1 2// Name: src/osx/cocoa/nonownedwnd.mm
33e90275
SC
3// Purpose: non owned window for cocoa
4// Author: DavidStefan Csomor
5// Modified by:
6// Created: 2008-06-20
a9a4f229 7// RCS-ID: $Id$
33e90275
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
05cf95ed
SC
13#ifndef WX_PRECOMP
14 #include "wx/nonownedwnd.h"
15 #include "wx/frame.h"
697dce56 16 #include "wx/app.h"
40c4350f 17 #include "wx/dialog.h"
d814b237
SC
18 #include "wx/menuitem.h"
19 #include "wx/menu.h"
33e90275
SC
20#endif
21
33e90275 22#include "wx/osx/private.h"
33e90275 23
5d57348e
SC
24NSScreen* wxOSXGetMenuScreen()
25{
26 if ( [NSScreen screens] == nil )
27 return [NSScreen mainScreen];
28 else
29 {
30 return [[NSScreen screens] objectAtIndex:0];
31 }
32}
33
33e90275
SC
34NSRect wxToNSRect( NSView* parent, const wxRect& r )
35{
5d57348e 36 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
33e90275
SC
37 int y = r.y;
38 int x = r.x ;
94929e7b 39 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2 40 y = (int)(frame.size.height - ( r.y + r.height ));
33e90275
SC
41 return NSMakeRect(x, y, r.width , r.height);
42}
43
44wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
45{
5d57348e 46 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
e490b0d2
VZ
47 int y = (int)rect.origin.y;
48 int x = (int)rect.origin.x;
94929e7b 49 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2
VZ
50 y = (int)(frame.size.height - (rect.origin.y + rect.size.height));
51 return wxRect( x, y, (int)rect.size.width, (int)rect.size.height );
33e90275
SC
52}
53
54NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
55{
5d57348e 56 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
33e90275
SC
57 int x = p.x ;
58 int y = p.y;
94929e7b 59 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2 60 y = (int)(frame.size.height - ( p.y ));
33e90275
SC
61 return NSMakePoint(x, y);
62}
63
64wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
65{
5d57348e 66 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
e490b0d2
VZ
67 int x = (int)p.x;
68 int y = (int)p.y;
94929e7b 69 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2 70 y = (int)(frame.size.height - ( p.y ));
33e90275
SC
71 return wxPoint( x, y);
72}
73
46f573dc
KO
74bool shouldHandleSelector(SEL selector)
75{
03647350 76 if (selector == @selector(noop:)
46f573dc
KO
77 || selector == @selector(complete:)
78 || selector == @selector(deleteBackward:)
79 || selector == @selector(deleteForward:)
80 || selector == @selector(insertNewline:)
81 || selector == @selector(insertTab:)
a46cbd24 82 || selector == @selector(insertBacktab:)
46f573dc
KO
83 || selector == @selector(keyDown:)
84 || selector == @selector(keyUp:)
85 || selector == @selector(scrollPageUp:)
86 || selector == @selector(scrollPageDown:)
87 || selector == @selector(scrollToBeginningOfDocument:)
88 || selector == @selector(scrollToEndOfDocument:))
89 return false;
03647350 90
46f573dc
KO
91 return true;
92
93}
94
dbeddfb9 95//
e17ac396 96// wx category for NSWindow (our own and wrapped instances)
dbeddfb9
SC
97//
98
e17ac396
SC
99@interface NSWindow (wxNSWindowSupport)
100
101- (wxNonOwnedWindowCocoaImpl*) WX_implementation;
102
103- (bool) WX_filterSendEvent:(NSEvent *) event;
104
105@end
106
107@implementation NSWindow (wxNSWindowSupport)
108
109- (wxNonOwnedWindowCocoaImpl*) WX_implementation
110{
111 return (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
112}
113
114// TODO in cocoa everything during a drag is sent to the NSWindow the mouse down occured,
115// this does not conform to the wx behaviour if the window is not captured, so try to resend
116// or capture all wx mouse event handling at the tlw as we did for carbon
117
118- (bool) WX_filterSendEvent:(NSEvent *) event
119{
120 bool handled = false;
121 if ( ([event type] >= NSLeftMouseDown) && ([event type] <= NSMouseExited) )
122 {
123 wxWindow* cw = wxWindow::GetCapture();
124 if ( cw != NULL )
125 {
126 ((wxWidgetCocoaImpl*)cw->GetPeer())->DoHandleMouseEvent( event);
127 handled = true;
128 }
129 }
130 return handled;
131}
132@end
133
134//
135// wx native implementation
136//
9130dfd7 137
dbeddfb9 138@interface wxNSWindow : NSWindow
dbeddfb9 139{
dbeddfb9
SC
140}
141
e17ac396 142- (void) sendEvent:(NSEvent *)event;
0bcac6c7 143- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
9130dfd7 144- (void)noResponderFor: (SEL) selector;
dbeddfb9
SC
145@end
146
147@implementation wxNSWindow
148
e17ac396
SC
149- (void)sendEvent:(NSEvent *) event
150{
151 if ( ![self WX_filterSendEvent: event] )
0b6f851f
SC
152 {
153 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
154 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
155
156 if (wxTheApp)
157 wxTheApp->MacSetCurrentEvent(event, NULL);
158
e17ac396 159 [super sendEvent: event];
0b6f851f
SC
160
161 if (wxTheApp)
162 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
163 }
e17ac396
SC
164}
165
0bcac6c7
KO
166// The default implementation always moves the window back onto the screen,
167// even when the programmer explicitly wants to hide it.
168- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
169{
219665bb 170 wxUnusedVar(screen);
0bcac6c7
KO
171 return frameRect;
172}
173
46f573dc
KO
174- (void)doCommandBySelector:(SEL)selector
175{
03647350 176 if (shouldHandleSelector(selector) &&
46f573dc
KO
177 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
178 [super doCommandBySelector:selector];
179}
180
181
9130dfd7
KO
182// NB: if we don't do this, all key downs that get handled lead to a NSBeep
183- (void)noResponderFor: (SEL) selector
184{
46f573dc 185 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
9130dfd7 186 {
20ede392 187 [super noResponderFor:selector];
9130dfd7
KO
188 }
189}
dbeddfb9 190
2c3ce6c4
KO
191// We need this for borderless windows, i.e. shaped windows or windows without
192// a title bar. For more info, see:
193// http://lists.apple.com/archives/cocoa-dev/2008/May/msg02091.html
194- (BOOL)canBecomeKeyWindow
195{
196 return YES;
197}
198
dbeddfb9
SC
199@end
200
bb839504 201@interface wxNSPanel : NSPanel
dbeddfb9
SC
202{
203}
204
247bb510 205- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
9130dfd7 206- (void)noResponderFor: (SEL) selector;
e17ac396 207- (void)sendEvent:(NSEvent *)event;
dbeddfb9
SC
208@end
209
03647350 210@implementation wxNSPanel
dbeddfb9 211
247bb510
VZ
212- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
213{
214 wxNonOwnedWindowCocoaImpl* impl = (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
215 if (impl && impl->IsFullScreen())
216 return frameRect;
217 else
218 return [super constrainFrameRect:frameRect toScreen:screen];
219}
220
09a9eb20
KO
221- (BOOL)canBecomeKeyWindow
222{
223 return YES;
224}
225
46f573dc
KO
226- (void)doCommandBySelector:(SEL)selector
227{
228 if (shouldHandleSelector(selector))
229 [super doCommandBySelector:selector];
230}
231
232// NB: if we don't do this, it seems that all events that end here lead to a NSBeep
9130dfd7
KO
233- (void)noResponderFor: (SEL) selector
234{
46f573dc 235 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
9130dfd7 236 {
20ede392 237 [super noResponderFor:selector];
9130dfd7
KO
238 }
239}
240
e17ac396
SC
241- (void)sendEvent:(NSEvent *) event
242{
243 if ( ![self WX_filterSendEvent: event] )
244 [super sendEvent: event];
245}
246
dbeddfb9
SC
247@end
248
249
250//
251// controller
252//
253
c8fdb345 254@interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
dbeddfb9
SC
255{
256}
257
258- (void)windowDidResize:(NSNotification *)notification;
259- (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
09a9eb20
KO
260- (void)windowDidResignKey:(NSNotification *)notification;
261- (void)windowDidBecomeKey:(NSNotification *)notification;
dbeddfb9
SC
262- (void)windowDidMove:(NSNotification *)notification;
263- (BOOL)windowShouldClose:(id)window;
1232607d 264- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
dbeddfb9
SC
265
266@end
267
fbbe829a
SC
268extern int wxOSXGetIdFromSelector(SEL action );
269
dbeddfb9
SC
270@implementation wxNonOwnedWindowController
271
272- (id) init
273{
f599a415 274 self = [super init];
dbeddfb9
SC
275 return self;
276}
277
fbbe829a
SC
278- (BOOL) triggerMenu:(SEL) action
279{
280 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
281 if ( mbar )
282 {
283 wxMenu* menu = NULL;
284 wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
285 if ( menu != NULL && menuitem != NULL)
286 return menu->HandleCommandProcess(menuitem);
287 }
288 return NO;
289}
290
291- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
292{
293 SEL action = [menuItem action];
294
295 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
296 if ( mbar )
297 {
298 wxMenu* menu = NULL;
299 wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
300 if ( menu != NULL && menuitem != NULL)
301 {
40a35c1f
SC
302 menu->HandleCommandUpdateStatus(menuitem);
303 return menuitem->IsEnabled();
fbbe829a
SC
304 }
305 }
306 return YES;
307}
308
309- (void)undo:(id)sender
310{
e7794cf2 311 wxUnusedVar(sender);
fbbe829a
SC
312 [self triggerMenu:_cmd];
313}
314
315- (void)redo:(id)sender
316{
e7794cf2 317 wxUnusedVar(sender);
fbbe829a
SC
318 [self triggerMenu:_cmd];
319}
320
321- (void)cut:(id)sender
322{
e7794cf2 323 wxUnusedVar(sender);
fbbe829a
SC
324 [self triggerMenu:_cmd];
325}
326
327- (void)copy:(id)sender
328{
e7794cf2 329 wxUnusedVar(sender);
fbbe829a
SC
330 [self triggerMenu:_cmd];
331}
332
333- (void)paste:(id)sender
334{
e7794cf2 335 wxUnusedVar(sender);
fbbe829a
SC
336 [self triggerMenu:_cmd];
337}
338
339- (void)delete:(id)sender
340{
e7794cf2 341 wxUnusedVar(sender);
fbbe829a
SC
342 [self triggerMenu:_cmd];
343}
344
345- (void)selectAll:(id)sender
346{
e7794cf2 347 wxUnusedVar(sender);
fbbe829a
SC
348 [self triggerMenu:_cmd];
349}
350
dbeddfb9
SC
351- (BOOL)windowShouldClose:(id)nwindow
352{
e17ac396 353 wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];
dbeddfb9
SC
354 if ( windowimpl )
355 {
356 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
357 if ( wxpeer )
358 wxpeer->Close();
359 }
360 return NO;
361}
362
e17ac396 363- (NSSize)windowWillResize:(NSWindow *)window
dbeddfb9
SC
364 toSize:(NSSize)proposedFrameSize
365{
e17ac396 366 NSRect frame = [window frame];
5da125c7 367 wxRect wxframe = wxFromNSRect( NULL, frame );
e490b0d2
VZ
368 wxframe.SetWidth( (int)proposedFrameSize.width );
369 wxframe.SetHeight( (int)proposedFrameSize.height );
e17ac396
SC
370
371 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
5da125c7
SC
372 if ( windowimpl )
373 {
374 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
375 if ( wxpeer )
376 {
377 wxpeer->HandleResizing( 0, &wxframe );
378 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
379 return newSize;
380 }
381 }
382
dbeddfb9
SC
383 return proposedFrameSize;
384}
385
386- (void)windowDidResize:(NSNotification *)notification
387{
e17ac396
SC
388 NSWindow* window = (NSWindow*) [notification object];
389 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
390 if ( windowimpl )
391 {
392 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
393 if ( wxpeer )
394 wxpeer->HandleResized(0);
395 }
396}
397
398- (void)windowDidMove:(NSNotification *)notification
399{
400 wxNSWindow* window = (wxNSWindow*) [notification object];
e17ac396 401 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
402 if ( windowimpl )
403 {
404 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
405 if ( wxpeer )
406 wxpeer->HandleMoved(0);
407 }
408}
409
09a9eb20 410- (void)windowDidBecomeKey:(NSNotification *)notification
dbeddfb9 411{
e17ac396
SC
412 NSWindow* window = (NSWindow*) [notification object];
413 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
414 if ( windowimpl )
415 {
416 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
417 if ( wxpeer )
418 wxpeer->HandleActivated(0, true);
419 }
420}
421
09a9eb20 422- (void)windowDidResignKey:(NSNotification *)notification
dbeddfb9 423{
e17ac396
SC
424 NSWindow* window = (NSWindow*) [notification object];
425 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
426 if ( windowimpl )
427 {
428 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
429 if ( wxpeer )
09a9eb20 430 {
dbeddfb9 431 wxpeer->HandleActivated(0, false);
03647350
VZ
432 // Needed for popup window since the firstResponder
433 // (focus in wx) doesn't change when this
09a9eb20
KO
434 // TLW becomes inactive.
435 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
436 event.SetEventObject(wxpeer);
437 wxpeer->HandleWindowEvent(event);
438 }
dbeddfb9
SC
439 }
440}
441
7cb2a241
SC
442- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
443{
444 wxUnusedVar(sender);
445
446 if ([anObject isKindOfClass:[wxNSTextField class]])
447 {
448 wxNSTextField* tf = (wxNSTextField*) anObject;
449 wxNSTextFieldEditor* editor = [tf fieldEditor];
450 if ( editor == nil )
451 {
452 editor = [[wxNSTextFieldEditor alloc] init];
453 [editor setFieldEditor:YES];
454 [tf setFieldEditor:editor];
c2a22141 455 [editor release];
7cb2a241
SC
456 }
457 return editor;
458 }
459
460 return nil;
461}
462
1232607d
KO
463- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
464{
e17ac396
SC
465 wxUnusedVar(newFrame);
466 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
1232607d
KO
467 if ( windowimpl )
468 {
469 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
470 wxMaximizeEvent event(wxpeer->GetId());
471 event.SetEventObject(wxpeer);
472 return !wxpeer->HandleWindowEvent(event);
473 }
474 return true;
475}
476
dbeddfb9
SC
477@end
478
33e90275
SC
479IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
480
03647350 481wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
33e90275
SC
482 wxNonOwnedWindowImpl(nonownedwnd)
483{
484 m_macWindow = NULL;
485 m_macFullScreenData = NULL;
486}
03647350
VZ
487
488wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
33e90275
SC
489{
490 m_macWindow = NULL;
491 m_macFullScreenData = NULL;
492}
03647350 493
33e90275
SC
494wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
495{
17e2694c
SC
496 if ( !m_wxPeer->IsNativeWindowWrapper() )
497 {
17e2694c
SC
498 [m_macWindow setDelegate:nil];
499 [m_macWindow release];
500 }
33e90275
SC
501}
502
0aaa6ace 503void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
33e90275 504{
17e2694c
SC
505 if ( !m_wxPeer->IsNativeWindowWrapper() )
506 {
507 [m_macWindow setDelegate:nil];
508 }
33e90275
SC
509}
510
40c4350f 511void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
20ede392 512long style, long extraStyle, const wxString& WXUNUSED(name) )
33e90275 513{
dbeddfb9 514 static wxNonOwnedWindowController* controller = NULL;
03647350 515
dbeddfb9
SC
516 if ( !controller )
517 controller =[[wxNonOwnedWindowController alloc] init];
518
519
33e90275 520 int windowstyle = NSBorderlessWindowMask;
03647350
VZ
521
522 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
09a9eb20
KO
523 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
524 {
dbeddfb9 525 m_macWindow = [wxNSPanel alloc];
09a9eb20 526 }
33e90275 527 else
dbeddfb9 528 m_macWindow = [wxNSWindow alloc];
03647350 529
e912ebe3 530 CGWindowLevel level = kCGNormalWindowLevel;
03647350 531
33e90275
SC
532 if ( style & wxFRAME_TOOL_WINDOW )
533 {
aa960026
KO
534 windowstyle |= NSUtilityWindowMask;
535 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
536 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
537 {
538 windowstyle |= NSTitledWindowMask ;
539 }
33e90275
SC
540 }
541 else if ( ( style & wxPOPUP_WINDOW ) )
542 {
543 level = kCGPopUpMenuWindowLevel;
544 /*
545 if ( ( style & wxBORDER_NONE ) )
546 {
547 wclass = kHelpWindowClass ; // has no border
548 attr |= kWindowNoShadowAttribute;
549 }
550 else
551 {
552 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
553 }
554 */
555 }
556 else if ( ( style & wxCAPTION ) )
557 {
558 windowstyle |= NSTitledWindowMask ;
559 }
560 else if ( ( style & wxFRAME_DRAWER ) )
561 {
562 /*
563 wclass = kDrawerWindowClass;
564 */
565 }
566 else
567 {
568 // set these even if we have no title, otherwise the controls won't be visible
569 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
570 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
571 {
572 windowstyle |= NSTitledWindowMask ;
573 }
574 /*
575 else if ( ( style & wxNO_BORDER ) )
576 {
577 wclass = kSimpleWindowClass ;
578 }
579 else
580 {
581 wclass = kPlainWindowClass ;
582 }
583 */
584 }
585
586 if ( windowstyle & NSTitledWindowMask )
587 {
588 if ( ( style & wxMINIMIZE_BOX ) )
589 windowstyle |= NSMiniaturizableWindowMask ;
590
591 if ( ( style & wxMAXIMIZE_BOX ) )
592 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
593
594 if ( ( style & wxRESIZE_BORDER ) )
595 windowstyle |= NSResizableWindowMask ;
596
597 if ( ( style & wxCLOSE_BOX) )
598 windowstyle |= NSClosableWindowMask ;
599 }
600 if ( extraStyle & wxFRAME_EX_METAL)
601 windowstyle |= NSTexturedBackgroundWindowMask;
602
bb839504
KO
603 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
604 level = kCGFloatingWindowLevel;
605
33e90275
SC
606 if ( ( style & wxSTAY_ON_TOP ) )
607 level = kCGUtilityWindowLevel;
03647350 608
33e90275 609 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
03647350 610
01639b08 611 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
03647350 612
33e90275
SC
613 [m_macWindow initWithContentRect:r
614 styleMask:windowstyle
615 backing:NSBackingStoreBuffered
03647350 616 defer:NO
33e90275 617 ];
03647350 618
40c4350f
VZ
619 // If the parent is modal, windows with wxFRAME_FLOAT_ON_PARENT style need
620 // to be in kCGUtilityWindowLevel and not kCGFloatingWindowLevel to stay
621 // above the parent.
622 wxDialog * const parentDialog = wxDynamicCast(parent, wxDialog);
623 if (parentDialog && parentDialog->IsModal())
624 {
625 if (level == kCGFloatingWindowLevel)
626 {
627 level = kCGUtilityWindowLevel;
628 }
629
630 // Cocoa's modal loop does not process other windows by default, but
631 // don't call this on normal window levels so nested modal dialogs will
632 // still behave modally.
633 if (level != kCGNormalWindowLevel)
634 {
635 if ([m_macWindow isKindOfClass:[NSPanel class]])
636 {
637 [(NSPanel*)m_macWindow setWorksWhenModal:YES];
638 }
639 }
640 }
641
33e90275 642 [m_macWindow setLevel:level];
9d243a47 643 m_macWindowLevel = level;
dbeddfb9
SC
644
645 [m_macWindow setDelegate:controller];
03647350 646
826da451 647 [m_macWindow setAcceptsMouseMovedEvents: YES];
b61b8371
SC
648
649 if ( ( style & wxFRAME_SHAPED) )
650 {
651 [m_macWindow setOpaque:NO];
652 [m_macWindow setAlphaValue:1.0];
653 }
4539e903
SC
654
655 if ( !(style & wxFRAME_TOOL_WINDOW) )
656 [m_macWindow setHidesOnDeactivate:NO];
33e90275
SC
657}
658
17e2694c
SC
659void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
660{
661 m_macWindow = nativeWindow;
662}
33e90275
SC
663
664WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
665{
666 return m_macWindow;
667}
668
669void wxNonOwnedWindowCocoaImpl::Raise()
670{
add051eb 671 [m_macWindow makeKeyAndOrderFront:nil];
33e90275 672}
03647350 673
33e90275
SC
674void wxNonOwnedWindowCocoaImpl::Lower()
675{
676 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
677}
678
dbc7ceb9
KO
679void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
680{
a029e98a 681 [m_macWindow orderFront:nil];
eabe8426 682 [[m_macWindow contentView] setNeedsDisplay: YES];
dbc7ceb9
KO
683}
684
33e90275
SC
685bool wxNonOwnedWindowCocoaImpl::Show(bool show)
686{
687 if ( show )
688 {
1f83c631
SC
689 wxNonOwnedWindow* wxpeer = GetWXPeer();
690 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
dbc7ceb9 691 [m_macWindow makeKeyAndOrderFront:nil];
1f83c631
SC
692 else
693 [m_macWindow orderFront:nil];
eabe8426 694 [[m_macWindow contentView] setNeedsDisplay: YES];
33e90275 695 }
03647350 696 else
33e90275
SC
697 [m_macWindow orderOut:nil];
698 return true;
699}
03647350 700
ab9a0b84
VZ
701bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
702 wxShowEffect effect,
703 unsigned timeout)
33e90275 704{
ab9a0b84
VZ
705 return wxWidgetCocoaImpl::
706 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
33e90275
SC
707}
708
709void wxNonOwnedWindowCocoaImpl::Update()
710{
711 [m_macWindow displayIfNeeded];
712}
713
714bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
715{
716 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
717 return true;
718}
719
776036d6 720bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
33e90275 721{
776036d6
SC
722 [m_macWindow setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
723 green:(CGFloat) (col.Green() / 255.0)
724 blue:(CGFloat) (col.Blue() / 255.0)
725 alpha:(CGFloat) (col.Alpha() / 255.0)]];
33e90275
SC
726 return true;
727}
728
729void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
730{
731 if ( m_macWindow )
732 {
733 bool metal = exStyle & wxFRAME_EX_METAL ;
734 int windowStyle = [ m_macWindow styleMask];
735 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
736 {
9a83f860 737 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
33e90275
SC
738 }
739 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
740 {
9a83f860 741 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
03647350 742 }
33e90275
SC
743 }
744}
03647350 745
b6dc21e7
KO
746void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
747{
748 if (m_macWindow)
749 {
750 CGWindowLevel level = kCGNormalWindowLevel;
751
752 if (style & wxSTAY_ON_TOP)
753 level = kCGUtilityWindowLevel;
754 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
755 level = kCGFloatingWindowLevel;
756
757 [m_macWindow setLevel: level];
9d243a47 758 m_macWindowLevel = level;
b6dc21e7
KO
759 }
760}
761
eabe8426 762bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
33e90275 763{
eabe8426
KO
764 if ( style == wxBG_STYLE_TRANSPARENT )
765 {
766 [m_macWindow setOpaque:NO];
767 [m_macWindow setBackgroundColor:[NSColor clearColor]];
768 }
769
33e90275
SC
770 return true;
771}
03647350 772
33e90275
SC
773bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
774{
775 return true;
776}
777
778void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
779{
780 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
54f11060
SC
781 // do not trigger refreshes upon invisible and possible partly created objects
782 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
33e90275
SC
783}
784
785void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
786{
787 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
788 x = r.GetLeft();
789 y = r.GetTop();
790}
791
792void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
793{
794 NSRect rect = [m_macWindow frame];
e490b0d2
VZ
795 width = (int)rect.size.width;
796 height = (int)rect.size.height;
33e90275
SC
797}
798
b0ec7fbb 799void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
33e90275 800{
33e90275 801 NSRect rect = [[m_macWindow contentView] frame];
e490b0d2
VZ
802 left = (int)rect.origin.x;
803 top = (int)rect.origin.y;
804 width = (int)rect.size.width;
805 height = (int)rect.size.height;
33e90275 806}
03647350 807
20ede392 808bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
33e90275 809{
eabe8426
KO
810 [m_macWindow setOpaque:NO];
811 [m_macWindow setBackgroundColor:[NSColor clearColor]];
812
813 return true;
33e90275
SC
814}
815
03647350 816void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
33e90275
SC
817{
818 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
819}
03647350 820
33e90275
SC
821bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
822{
cbe06760
VZ
823 if (([m_macWindow styleMask] & NSResizableWindowMask) != 0)
824 {
825 return [m_macWindow isZoomed];
826 }
827 else
828 {
829 NSRect rectScreen = [[NSScreen mainScreen] visibleFrame];
830 NSRect rectWindow = [m_macWindow frame];
831 return (rectScreen.origin.x == rectWindow.origin.x &&
832 rectScreen.origin.y == rectWindow.origin.y &&
833 rectScreen.size.width == rectWindow.size.width &&
834 rectScreen.size.height == rectWindow.size.height);
835 }
33e90275 836}
03647350 837
33e90275
SC
838bool wxNonOwnedWindowCocoaImpl::IsIconized() const
839{
840 return [m_macWindow isMiniaturized];
841}
03647350 842
33e90275
SC
843void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
844{
845 if ( iconize )
846 [m_macWindow miniaturize:nil];
847 else
848 [m_macWindow deminiaturize:nil];
849}
03647350 850
20ede392 851void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
33e90275
SC
852{
853 [m_macWindow zoom:nil];
854}
03647350 855
33e90275
SC
856
857// http://cocoadevcentral.com/articles/000028.php
858
859typedef struct
860{
861 int m_formerLevel;
862 NSRect m_formerFrame;
863} FullScreenData ;
864
865bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
866{
867 return m_macFullScreenData != NULL ;
868}
03647350 869
20ede392 870bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
33e90275
SC
871{
872 if ( show )
873 {
874 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
875 delete data ;
876 data = new FullScreenData();
877
878 m_macFullScreenData = data ;
879 data->m_formerLevel = [m_macWindow level];
880 data->m_formerFrame = [m_macWindow frame];
2077a6ad
SC
881#if 0
882 // CGDisplayCapture( kCGDirectMainDisplay );
883 //[m_macWindow setLevel:NSMainMenuWindowLevel+1/*CGShieldingWindowLevel()*/];
884#endif
247bb510
VZ
885 NSRect screenframe = [[NSScreen mainScreen] frame];
886 NSRect frame = NSMakeRect (0, 0, 100, 100);
887 NSRect contentRect;
888 contentRect = [NSWindow contentRectForFrameRect: frame
d1fc9578 889 styleMask: [m_macWindow styleMask]];
247bb510
VZ
890 screenframe.origin.y += (frame.origin.y - contentRect.origin.y);
891 screenframe.size.height += (frame.size.height - contentRect.size.height);
892 [m_macWindow setFrame:screenframe display:YES];
2077a6ad 893
d1fc9578 894 SetSystemUIMode(kUIModeAllHidden,
2077a6ad 895 kUIOptionDisableAppleMenu
d1fc9578 896 /*
2077a6ad 897 | kUIOptionDisableProcessSwitch
d1fc9578
SC
898 | kUIOptionDisableForceQuit
899 */);
33e90275
SC
900 }
901 else if ( m_macFullScreenData != NULL )
902 {
903 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
2077a6ad
SC
904#if 0
905 // CGDisplayRelease( kCGDirectMainDisplay );
906 // [m_macWindow setLevel:data->m_formerLevel];
907#endif
908
33e90275
SC
909 [m_macWindow setFrame:data->m_formerFrame display:YES];
910 delete data ;
911 m_macFullScreenData = NULL ;
2077a6ad 912
d1fc9578 913 SetSystemUIMode(kUIModeNormal, 0);
33e90275 914 }
03647350 915
33e90275
SC
916 return true;
917}
918
32b8e701 919void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
33e90275 920{
98182acc 921 NSRequestUserAttentionType flagsOSX;
32b8e701
VZ
922 switch ( flagsWX )
923 {
924 case wxUSER_ATTENTION_INFO:
925 flagsOSX = NSInformationalRequest;
926 break;
927
928 case wxUSER_ATTENTION_ERROR:
929 flagsOSX = NSCriticalRequest;
930 break;
931
932 default:
933 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
934 return;
935 }
936
937 [NSApp requestUserAttention:flagsOSX];
33e90275 938}
dbeddfb9
SC
939
940void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
941{
942 wxPoint p((x ? *x : 0), (y ? *y : 0) );
dbeddfb9 943 NSPoint nspt = wxToNSPoint( NULL, p );
54f11060
SC
944 nspt = [m_macWindow convertScreenToBase:nspt];
945 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
946 p = wxFromNSPoint([m_macWindow contentView], nspt);
dbeddfb9 947 if ( x )
03647350 948 *x = p.x;
dbeddfb9
SC
949 if ( y )
950 *y = p.y;
951}
952
953void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
954{
54f11060
SC
955 wxPoint p((x ? *x : 0), (y ? *y : 0) );
956 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
957 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
958 nspt = [m_macWindow convertBaseToScreen:nspt];
959 p = wxFromNSPoint( NULL, nspt);
dbeddfb9
SC
960 if ( x )
961 *x = p.x;
962 if ( y )
963 *y = p.y;
964}
965
dbc7ceb9
KO
966bool wxNonOwnedWindowCocoaImpl::IsActive()
967{
968 return [m_macWindow isKeyWindow];
969}
970
efb2fa41
KO
971void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
972{
973 [m_macWindow setDocumentEdited:modified];
974}
975
ebf7d5c4 976bool wxNonOwnedWindowCocoaImpl::IsModified() const
efb2fa41
KO
977{
978 return [m_macWindow isDocumentEdited];
979}
980
9d243a47
SC
981void wxNonOwnedWindowCocoaImpl::RestoreWindowLevel()
982{
983 if ( [m_macWindow level] != m_macWindowLevel )
984 [m_macWindow setLevel:m_macWindowLevel];
985}
986
987//
988//
989//
990
17e2694c
SC
991wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
992{
993 wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
994 now->Create( parent, nativeWindow );
995 return now;
996}
997
dbeddfb9
SC
998wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
999 long style, long extraStyle, const wxString& name )
1000{
1001 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
1002 now->Create( parent, pos, size, style , extraStyle, name );
1003 return now;
54f11060 1004}
9d243a47 1005