]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
appeasing clang
[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
7// RCS-ID: $Id: nonownedwnd.mm 48805 2007-09-19 14:52:25Z 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{
274 [super init];
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 {
302 if ( menu->HandleCommandUpdateStatus(menuitem) )
303 return menuitem->IsEnabled();
304 }
305 }
306 return YES;
307}
308
309- (void)undo:(id)sender
310{
311 [self triggerMenu:_cmd];
312}
313
314- (void)redo:(id)sender
315{
316 [self triggerMenu:_cmd];
317}
318
319- (void)cut:(id)sender
320{
321 [self triggerMenu:_cmd];
322}
323
324- (void)copy:(id)sender
325{
326 [self triggerMenu:_cmd];
327}
328
329- (void)paste:(id)sender
330{
331 [self triggerMenu:_cmd];
332}
333
334- (void)delete:(id)sender
335{
336 [self triggerMenu:_cmd];
337}
338
339- (void)selectAll:(id)sender
340{
341 [self triggerMenu:_cmd];
342}
343
dbeddfb9
SC
344- (BOOL)windowShouldClose:(id)nwindow
345{
e17ac396 346 wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];
dbeddfb9
SC
347 if ( windowimpl )
348 {
349 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
350 if ( wxpeer )
351 wxpeer->Close();
352 }
353 return NO;
354}
355
e17ac396 356- (NSSize)windowWillResize:(NSWindow *)window
dbeddfb9
SC
357 toSize:(NSSize)proposedFrameSize
358{
e17ac396 359 NSRect frame = [window frame];
5da125c7 360 wxRect wxframe = wxFromNSRect( NULL, frame );
e490b0d2
VZ
361 wxframe.SetWidth( (int)proposedFrameSize.width );
362 wxframe.SetHeight( (int)proposedFrameSize.height );
e17ac396
SC
363
364 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
5da125c7
SC
365 if ( windowimpl )
366 {
367 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
368 if ( wxpeer )
369 {
370 wxpeer->HandleResizing( 0, &wxframe );
371 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
372 return newSize;
373 }
374 }
375
dbeddfb9
SC
376 return proposedFrameSize;
377}
378
379- (void)windowDidResize:(NSNotification *)notification
380{
e17ac396
SC
381 NSWindow* window = (NSWindow*) [notification object];
382 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
383 if ( windowimpl )
384 {
385 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
386 if ( wxpeer )
387 wxpeer->HandleResized(0);
388 }
389}
390
391- (void)windowDidMove:(NSNotification *)notification
392{
393 wxNSWindow* window = (wxNSWindow*) [notification object];
e17ac396 394 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
395 if ( windowimpl )
396 {
397 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
398 if ( wxpeer )
399 wxpeer->HandleMoved(0);
400 }
401}
402
09a9eb20 403- (void)windowDidBecomeKey:(NSNotification *)notification
dbeddfb9 404{
e17ac396
SC
405 NSWindow* window = (NSWindow*) [notification object];
406 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
407 if ( windowimpl )
408 {
409 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
410 if ( wxpeer )
411 wxpeer->HandleActivated(0, true);
412 }
413}
414
09a9eb20 415- (void)windowDidResignKey:(NSNotification *)notification
dbeddfb9 416{
e17ac396
SC
417 NSWindow* window = (NSWindow*) [notification object];
418 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
419 if ( windowimpl )
420 {
421 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
422 if ( wxpeer )
09a9eb20 423 {
dbeddfb9 424 wxpeer->HandleActivated(0, false);
03647350
VZ
425 // Needed for popup window since the firstResponder
426 // (focus in wx) doesn't change when this
09a9eb20
KO
427 // TLW becomes inactive.
428 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
429 event.SetEventObject(wxpeer);
430 wxpeer->HandleWindowEvent(event);
431 }
dbeddfb9
SC
432 }
433}
434
7cb2a241
SC
435- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
436{
437 wxUnusedVar(sender);
438
439 if ([anObject isKindOfClass:[wxNSTextField class]])
440 {
441 wxNSTextField* tf = (wxNSTextField*) anObject;
442 wxNSTextFieldEditor* editor = [tf fieldEditor];
443 if ( editor == nil )
444 {
445 editor = [[wxNSTextFieldEditor alloc] init];
446 [editor setFieldEditor:YES];
447 [tf setFieldEditor:editor];
448 }
449 return editor;
450 }
451
452 return nil;
453}
454
1232607d
KO
455- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
456{
e17ac396
SC
457 wxUnusedVar(newFrame);
458 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
1232607d
KO
459 if ( windowimpl )
460 {
461 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
462 wxMaximizeEvent event(wxpeer->GetId());
463 event.SetEventObject(wxpeer);
464 return !wxpeer->HandleWindowEvent(event);
465 }
466 return true;
467}
468
dbeddfb9
SC
469@end
470
33e90275
SC
471IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
472
03647350 473wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
33e90275
SC
474 wxNonOwnedWindowImpl(nonownedwnd)
475{
476 m_macWindow = NULL;
477 m_macFullScreenData = NULL;
478}
03647350
VZ
479
480wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
33e90275
SC
481{
482 m_macWindow = NULL;
483 m_macFullScreenData = NULL;
484}
03647350 485
33e90275
SC
486wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
487{
17e2694c
SC
488 if ( !m_wxPeer->IsNativeWindowWrapper() )
489 {
17e2694c
SC
490 [m_macWindow setDelegate:nil];
491 [m_macWindow release];
492 }
33e90275
SC
493}
494
0aaa6ace 495void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
33e90275 496{
17e2694c
SC
497 if ( !m_wxPeer->IsNativeWindowWrapper() )
498 {
499 [m_macWindow setDelegate:nil];
500 }
33e90275
SC
501}
502
40c4350f 503void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
20ede392 504long style, long extraStyle, const wxString& WXUNUSED(name) )
33e90275 505{
dbeddfb9 506 static wxNonOwnedWindowController* controller = NULL;
03647350 507
dbeddfb9
SC
508 if ( !controller )
509 controller =[[wxNonOwnedWindowController alloc] init];
510
511
33e90275 512 int windowstyle = NSBorderlessWindowMask;
03647350
VZ
513
514 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
09a9eb20
KO
515 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
516 {
dbeddfb9 517 m_macWindow = [wxNSPanel alloc];
09a9eb20 518 }
33e90275 519 else
dbeddfb9 520 m_macWindow = [wxNSWindow alloc];
03647350 521
e912ebe3 522 CGWindowLevel level = kCGNormalWindowLevel;
03647350 523
33e90275
SC
524 if ( style & wxFRAME_TOOL_WINDOW )
525 {
aa960026
KO
526 windowstyle |= NSUtilityWindowMask;
527 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
528 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
529 {
530 windowstyle |= NSTitledWindowMask ;
531 }
33e90275
SC
532 }
533 else if ( ( style & wxPOPUP_WINDOW ) )
534 {
535 level = kCGPopUpMenuWindowLevel;
536 /*
537 if ( ( style & wxBORDER_NONE ) )
538 {
539 wclass = kHelpWindowClass ; // has no border
540 attr |= kWindowNoShadowAttribute;
541 }
542 else
543 {
544 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
545 }
546 */
547 }
548 else if ( ( style & wxCAPTION ) )
549 {
550 windowstyle |= NSTitledWindowMask ;
551 }
552 else if ( ( style & wxFRAME_DRAWER ) )
553 {
554 /*
555 wclass = kDrawerWindowClass;
556 */
557 }
558 else
559 {
560 // set these even if we have no title, otherwise the controls won't be visible
561 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
562 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
563 {
564 windowstyle |= NSTitledWindowMask ;
565 }
566 /*
567 else if ( ( style & wxNO_BORDER ) )
568 {
569 wclass = kSimpleWindowClass ;
570 }
571 else
572 {
573 wclass = kPlainWindowClass ;
574 }
575 */
576 }
577
578 if ( windowstyle & NSTitledWindowMask )
579 {
580 if ( ( style & wxMINIMIZE_BOX ) )
581 windowstyle |= NSMiniaturizableWindowMask ;
582
583 if ( ( style & wxMAXIMIZE_BOX ) )
584 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
585
586 if ( ( style & wxRESIZE_BORDER ) )
587 windowstyle |= NSResizableWindowMask ;
588
589 if ( ( style & wxCLOSE_BOX) )
590 windowstyle |= NSClosableWindowMask ;
591 }
592 if ( extraStyle & wxFRAME_EX_METAL)
593 windowstyle |= NSTexturedBackgroundWindowMask;
594
bb839504
KO
595 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
596 level = kCGFloatingWindowLevel;
597
33e90275
SC
598 if ( ( style & wxSTAY_ON_TOP ) )
599 level = kCGUtilityWindowLevel;
03647350 600
33e90275 601 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
03647350 602
01639b08 603 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
03647350 604
33e90275
SC
605 [m_macWindow initWithContentRect:r
606 styleMask:windowstyle
607 backing:NSBackingStoreBuffered
03647350 608 defer:NO
33e90275 609 ];
03647350 610
40c4350f
VZ
611 // If the parent is modal, windows with wxFRAME_FLOAT_ON_PARENT style need
612 // to be in kCGUtilityWindowLevel and not kCGFloatingWindowLevel to stay
613 // above the parent.
614 wxDialog * const parentDialog = wxDynamicCast(parent, wxDialog);
615 if (parentDialog && parentDialog->IsModal())
616 {
617 if (level == kCGFloatingWindowLevel)
618 {
619 level = kCGUtilityWindowLevel;
620 }
621
622 // Cocoa's modal loop does not process other windows by default, but
623 // don't call this on normal window levels so nested modal dialogs will
624 // still behave modally.
625 if (level != kCGNormalWindowLevel)
626 {
627 if ([m_macWindow isKindOfClass:[NSPanel class]])
628 {
629 [(NSPanel*)m_macWindow setWorksWhenModal:YES];
630 }
631 }
632 }
633
33e90275 634 [m_macWindow setLevel:level];
dbeddfb9
SC
635
636 [m_macWindow setDelegate:controller];
03647350 637
826da451 638 [m_macWindow setAcceptsMouseMovedEvents: YES];
b61b8371
SC
639
640 if ( ( style & wxFRAME_SHAPED) )
641 {
642 [m_macWindow setOpaque:NO];
643 [m_macWindow setAlphaValue:1.0];
644 }
4539e903
SC
645
646 if ( !(style & wxFRAME_TOOL_WINDOW) )
647 [m_macWindow setHidesOnDeactivate:NO];
33e90275
SC
648}
649
17e2694c
SC
650void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
651{
652 m_macWindow = nativeWindow;
653}
33e90275
SC
654
655WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
656{
657 return m_macWindow;
658}
659
660void wxNonOwnedWindowCocoaImpl::Raise()
661{
add051eb 662 [m_macWindow makeKeyAndOrderFront:nil];
33e90275 663}
03647350 664
33e90275
SC
665void wxNonOwnedWindowCocoaImpl::Lower()
666{
667 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
668}
669
dbc7ceb9
KO
670void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
671{
a029e98a 672 [m_macWindow orderFront:nil];
eabe8426 673 [[m_macWindow contentView] setNeedsDisplay: YES];
dbc7ceb9
KO
674}
675
33e90275
SC
676bool wxNonOwnedWindowCocoaImpl::Show(bool show)
677{
678 if ( show )
679 {
1f83c631
SC
680 wxNonOwnedWindow* wxpeer = GetWXPeer();
681 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
dbc7ceb9 682 [m_macWindow makeKeyAndOrderFront:nil];
1f83c631
SC
683 else
684 [m_macWindow orderFront:nil];
eabe8426 685 [[m_macWindow contentView] setNeedsDisplay: YES];
33e90275 686 }
03647350 687 else
33e90275
SC
688 [m_macWindow orderOut:nil];
689 return true;
690}
03647350 691
ab9a0b84
VZ
692bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
693 wxShowEffect effect,
694 unsigned timeout)
33e90275 695{
ab9a0b84
VZ
696 return wxWidgetCocoaImpl::
697 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
33e90275
SC
698}
699
700void wxNonOwnedWindowCocoaImpl::Update()
701{
702 [m_macWindow displayIfNeeded];
703}
704
705bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
706{
707 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
708 return true;
709}
710
20ede392 711bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
33e90275
SC
712{
713 return true;
714}
715
716void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
717{
718 if ( m_macWindow )
719 {
720 bool metal = exStyle & wxFRAME_EX_METAL ;
721 int windowStyle = [ m_macWindow styleMask];
722 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
723 {
9a83f860 724 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
33e90275
SC
725 }
726 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
727 {
9a83f860 728 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
03647350 729 }
33e90275
SC
730 }
731}
03647350 732
b6dc21e7
KO
733void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
734{
735 if (m_macWindow)
736 {
737 CGWindowLevel level = kCGNormalWindowLevel;
738
739 if (style & wxSTAY_ON_TOP)
740 level = kCGUtilityWindowLevel;
741 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
742 level = kCGFloatingWindowLevel;
743
744 [m_macWindow setLevel: level];
745 }
746}
747
eabe8426 748bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
33e90275 749{
eabe8426
KO
750 if ( style == wxBG_STYLE_TRANSPARENT )
751 {
752 [m_macWindow setOpaque:NO];
753 [m_macWindow setBackgroundColor:[NSColor clearColor]];
754 }
755
33e90275
SC
756 return true;
757}
03647350 758
33e90275
SC
759bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
760{
761 return true;
762}
763
764void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
765{
766 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
54f11060
SC
767 // do not trigger refreshes upon invisible and possible partly created objects
768 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
33e90275
SC
769}
770
771void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
772{
773 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
774 x = r.GetLeft();
775 y = r.GetTop();
776}
777
778void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
779{
780 NSRect rect = [m_macWindow frame];
e490b0d2
VZ
781 width = (int)rect.size.width;
782 height = (int)rect.size.height;
33e90275
SC
783}
784
b0ec7fbb 785void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
33e90275 786{
33e90275 787 NSRect rect = [[m_macWindow contentView] frame];
e490b0d2
VZ
788 left = (int)rect.origin.x;
789 top = (int)rect.origin.y;
790 width = (int)rect.size.width;
791 height = (int)rect.size.height;
33e90275 792}
03647350 793
20ede392 794bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
33e90275 795{
eabe8426
KO
796 [m_macWindow setOpaque:NO];
797 [m_macWindow setBackgroundColor:[NSColor clearColor]];
798
799 return true;
33e90275
SC
800}
801
03647350 802void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
33e90275
SC
803{
804 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
805}
03647350 806
33e90275
SC
807bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
808{
cbe06760
VZ
809 if (([m_macWindow styleMask] & NSResizableWindowMask) != 0)
810 {
811 return [m_macWindow isZoomed];
812 }
813 else
814 {
815 NSRect rectScreen = [[NSScreen mainScreen] visibleFrame];
816 NSRect rectWindow = [m_macWindow frame];
817 return (rectScreen.origin.x == rectWindow.origin.x &&
818 rectScreen.origin.y == rectWindow.origin.y &&
819 rectScreen.size.width == rectWindow.size.width &&
820 rectScreen.size.height == rectWindow.size.height);
821 }
33e90275 822}
03647350 823
33e90275
SC
824bool wxNonOwnedWindowCocoaImpl::IsIconized() const
825{
826 return [m_macWindow isMiniaturized];
827}
03647350 828
33e90275
SC
829void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
830{
831 if ( iconize )
832 [m_macWindow miniaturize:nil];
833 else
834 [m_macWindow deminiaturize:nil];
835}
03647350 836
20ede392 837void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
33e90275
SC
838{
839 [m_macWindow zoom:nil];
840}
03647350 841
33e90275
SC
842
843// http://cocoadevcentral.com/articles/000028.php
844
845typedef struct
846{
847 int m_formerLevel;
848 NSRect m_formerFrame;
849} FullScreenData ;
850
851bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
852{
853 return m_macFullScreenData != NULL ;
854}
03647350 855
20ede392 856bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
33e90275
SC
857{
858 if ( show )
859 {
860 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
861 delete data ;
862 data = new FullScreenData();
863
864 m_macFullScreenData = data ;
865 data->m_formerLevel = [m_macWindow level];
866 data->m_formerFrame = [m_macWindow frame];
867 CGDisplayCapture( kCGDirectMainDisplay );
868 [m_macWindow setLevel:CGShieldingWindowLevel()];
247bb510
VZ
869 NSRect screenframe = [[NSScreen mainScreen] frame];
870 NSRect frame = NSMakeRect (0, 0, 100, 100);
871 NSRect contentRect;
872 contentRect = [NSWindow contentRectForFrameRect: frame
873 styleMask: NSTitledWindowMask];
874 screenframe.origin.y += (frame.origin.y - contentRect.origin.y);
875 screenframe.size.height += (frame.size.height - contentRect.size.height);
876 [m_macWindow setFrame:screenframe display:YES];
33e90275
SC
877 }
878 else if ( m_macFullScreenData != NULL )
879 {
880 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
881 CGDisplayRelease( kCGDirectMainDisplay );
882 [m_macWindow setLevel:data->m_formerLevel];
883 [m_macWindow setFrame:data->m_formerFrame display:YES];
884 delete data ;
885 m_macFullScreenData = NULL ;
886 }
03647350 887
33e90275
SC
888 return true;
889}
890
32b8e701 891void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
33e90275 892{
98182acc 893 NSRequestUserAttentionType flagsOSX;
32b8e701
VZ
894 switch ( flagsWX )
895 {
896 case wxUSER_ATTENTION_INFO:
897 flagsOSX = NSInformationalRequest;
898 break;
899
900 case wxUSER_ATTENTION_ERROR:
901 flagsOSX = NSCriticalRequest;
902 break;
903
904 default:
905 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
906 return;
907 }
908
909 [NSApp requestUserAttention:flagsOSX];
33e90275 910}
dbeddfb9
SC
911
912void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
913{
914 wxPoint p((x ? *x : 0), (y ? *y : 0) );
dbeddfb9 915 NSPoint nspt = wxToNSPoint( NULL, p );
54f11060
SC
916 nspt = [m_macWindow convertScreenToBase:nspt];
917 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
918 p = wxFromNSPoint([m_macWindow contentView], nspt);
dbeddfb9 919 if ( x )
03647350 920 *x = p.x;
dbeddfb9
SC
921 if ( y )
922 *y = p.y;
923}
924
925void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
926{
54f11060
SC
927 wxPoint p((x ? *x : 0), (y ? *y : 0) );
928 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
929 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
930 nspt = [m_macWindow convertBaseToScreen:nspt];
931 p = wxFromNSPoint( NULL, nspt);
dbeddfb9
SC
932 if ( x )
933 *x = p.x;
934 if ( y )
935 *y = p.y;
936}
937
dbc7ceb9
KO
938bool wxNonOwnedWindowCocoaImpl::IsActive()
939{
940 return [m_macWindow isKeyWindow];
941}
942
efb2fa41
KO
943void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
944{
945 [m_macWindow setDocumentEdited:modified];
946}
947
ebf7d5c4 948bool wxNonOwnedWindowCocoaImpl::IsModified() const
efb2fa41
KO
949{
950 return [m_macWindow isDocumentEdited];
951}
952
17e2694c
SC
953wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
954{
955 wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
956 now->Create( parent, nativeWindow );
957 return now;
958}
959
dbeddfb9
SC
960wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
961 long style, long extraStyle, const wxString& name )
962{
963 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
964 now->Create( parent, pos, size, style , extraStyle, name );
965 return now;
54f11060 966}