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