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