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