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