]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
No real changes, just backwards propagate the changes to stc.cpp.
[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 }
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);
03647350
VZ
453 // Needed for popup window since the firstResponder
454 // (focus in wx) doesn't change when this
09a9eb20
KO
455 // TLW becomes inactive.
456 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
457 event.SetEventObject(wxpeer);
458 wxpeer->HandleWindowEvent(event);
459 }
dbeddfb9
SC
460 }
461}
462
7cb2a241
SC
463- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
464{
465 wxUnusedVar(sender);
466
467 if ([anObject isKindOfClass:[wxNSTextField class]])
468 {
469 wxNSTextField* tf = (wxNSTextField*) anObject;
470 wxNSTextFieldEditor* editor = [tf fieldEditor];
471 if ( editor == nil )
472 {
473 editor = [[wxNSTextFieldEditor alloc] init];
474 [editor setFieldEditor:YES];
475 [tf setFieldEditor:editor];
c2a22141 476 [editor release];
7cb2a241
SC
477 }
478 return editor;
2712275c
SC
479 }
480
7cb2a241
SC
481 return nil;
482}
483
1232607d
KO
484- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
485{
e17ac396
SC
486 wxUnusedVar(newFrame);
487 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
1232607d
KO
488 if ( windowimpl )
489 {
490 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
491 wxMaximizeEvent event(wxpeer->GetId());
492 event.SetEventObject(wxpeer);
493 return !wxpeer->HandleWindowEvent(event);
494 }
495 return true;
496}
497
dbeddfb9
SC
498@end
499
33e90275
SC
500IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
501
03647350 502wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
33e90275
SC
503 wxNonOwnedWindowImpl(nonownedwnd)
504{
505 m_macWindow = NULL;
506 m_macFullScreenData = NULL;
507}
03647350
VZ
508
509wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
33e90275
SC
510{
511 m_macWindow = NULL;
512 m_macFullScreenData = NULL;
513}
03647350 514
33e90275
SC
515wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
516{
17e2694c
SC
517 if ( !m_wxPeer->IsNativeWindowWrapper() )
518 {
17e2694c
SC
519 [m_macWindow setDelegate:nil];
520 [m_macWindow release];
521 }
33e90275
SC
522}
523
0aaa6ace 524void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
33e90275 525{
17e2694c
SC
526 if ( !m_wxPeer->IsNativeWindowWrapper() )
527 {
528 [m_macWindow setDelegate:nil];
529 }
33e90275
SC
530}
531
40c4350f 532void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
20ede392 533long style, long extraStyle, const wxString& WXUNUSED(name) )
33e90275 534{
dbeddfb9 535 static wxNonOwnedWindowController* controller = NULL;
03647350 536
dbeddfb9
SC
537 if ( !controller )
538 controller =[[wxNonOwnedWindowController alloc] init];
539
540
33e90275 541 int windowstyle = NSBorderlessWindowMask;
03647350
VZ
542
543 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
09a9eb20
KO
544 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
545 {
dbeddfb9 546 m_macWindow = [wxNSPanel alloc];
09a9eb20 547 }
33e90275 548 else
dbeddfb9 549 m_macWindow = [wxNSWindow alloc];
03647350 550
e912ebe3 551 CGWindowLevel level = kCGNormalWindowLevel;
03647350 552
33e90275
SC
553 if ( style & wxFRAME_TOOL_WINDOW )
554 {
aa960026 555 windowstyle |= NSUtilityWindowMask;
33e90275
SC
556 }
557 else if ( ( style & wxPOPUP_WINDOW ) )
558 {
559 level = kCGPopUpMenuWindowLevel;
33e90275
SC
560 }
561 else if ( ( style & wxFRAME_DRAWER ) )
562 {
563 /*
564 wclass = kDrawerWindowClass;
565 */
566 }
eccdb551
SC
567
568 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
569 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) || ( style & wxCAPTION ) )
33e90275 570 {
eccdb551 571 windowstyle |= NSTitledWindowMask ;
33e90275
SC
572 if ( ( style & wxMINIMIZE_BOX ) )
573 windowstyle |= NSMiniaturizableWindowMask ;
eccdb551 574
33e90275 575 if ( ( style & wxMAXIMIZE_BOX ) )
33e90275 576 windowstyle |= NSResizableWindowMask ;
eccdb551 577
33e90275
SC
578 if ( ( style & wxCLOSE_BOX) )
579 windowstyle |= NSClosableWindowMask ;
580 }
eccdb551
SC
581
582 if ( ( style & wxRESIZE_BORDER ) )
583 windowstyle |= NSResizableWindowMask ;
584
33e90275
SC
585 if ( extraStyle & wxFRAME_EX_METAL)
586 windowstyle |= NSTexturedBackgroundWindowMask;
587
bb839504
KO
588 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
589 level = kCGFloatingWindowLevel;
590
33e90275
SC
591 if ( ( style & wxSTAY_ON_TOP ) )
592 level = kCGUtilityWindowLevel;
03647350 593
33e90275 594 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
03647350 595
01639b08 596 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
03647350 597
33e90275
SC
598 [m_macWindow initWithContentRect:r
599 styleMask:windowstyle
600 backing:NSBackingStoreBuffered
03647350 601 defer:NO
33e90275 602 ];
eccdb551
SC
603
604 // if we just have a title bar with no buttons needed, hide them
605 if ( (windowstyle & NSTitledWindowMask) &&
606 !(style & wxCLOSE_BOX) && !(style & wxMAXIMIZE_BOX) && !(style & wxMINIMIZE_BOX) )
607 {
608 [[m_macWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
609 [[m_macWindow standardWindowButton:NSWindowCloseButton] setHidden:YES];
610 [[m_macWindow standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
611 }
612
40c4350f
VZ
613 // If the parent is modal, windows with wxFRAME_FLOAT_ON_PARENT style need
614 // to be in kCGUtilityWindowLevel and not kCGFloatingWindowLevel to stay
615 // above the parent.
616 wxDialog * const parentDialog = wxDynamicCast(parent, wxDialog);
617 if (parentDialog && parentDialog->IsModal())
618 {
619 if (level == kCGFloatingWindowLevel)
620 {
621 level = kCGUtilityWindowLevel;
622 }
623
624 // Cocoa's modal loop does not process other windows by default, but
625 // don't call this on normal window levels so nested modal dialogs will
626 // still behave modally.
627 if (level != kCGNormalWindowLevel)
628 {
629 if ([m_macWindow isKindOfClass:[NSPanel class]])
630 {
631 [(NSPanel*)m_macWindow setWorksWhenModal:YES];
632 }
633 }
634 }
635
33e90275 636 [m_macWindow setLevel:level];
9d243a47 637 m_macWindowLevel = level;
dbeddfb9
SC
638
639 [m_macWindow setDelegate:controller];
03647350 640
b61b8371
SC
641 if ( ( style & wxFRAME_SHAPED) )
642 {
643 [m_macWindow setOpaque:NO];
644 [m_macWindow setAlphaValue:1.0];
645 }
4539e903
SC
646
647 if ( !(style & wxFRAME_TOOL_WINDOW) )
648 [m_macWindow setHidesOnDeactivate:NO];
33e90275
SC
649}
650
17e2694c
SC
651void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
652{
653 m_macWindow = nativeWindow;
654}
33e90275
SC
655
656WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
657{
658 return m_macWindow;
659}
660
661void wxNonOwnedWindowCocoaImpl::Raise()
662{
add051eb 663 [m_macWindow makeKeyAndOrderFront:nil];
33e90275 664}
03647350 665
33e90275
SC
666void wxNonOwnedWindowCocoaImpl::Lower()
667{
668 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
669}
670
dbc7ceb9
KO
671void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
672{
a029e98a 673 [m_macWindow orderFront:nil];
eabe8426 674 [[m_macWindow contentView] setNeedsDisplay: YES];
dbc7ceb9
KO
675}
676
33e90275
SC
677bool wxNonOwnedWindowCocoaImpl::Show(bool show)
678{
679 if ( show )
680 {
1f83c631
SC
681 wxNonOwnedWindow* wxpeer = GetWXPeer();
682 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
dbc7ceb9 683 [m_macWindow makeKeyAndOrderFront:nil];
1f83c631
SC
684 else
685 [m_macWindow orderFront:nil];
eabe8426 686 [[m_macWindow contentView] setNeedsDisplay: YES];
33e90275 687 }
03647350 688 else
33e90275
SC
689 [m_macWindow orderOut:nil];
690 return true;
691}
03647350 692
ab9a0b84
VZ
693bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
694 wxShowEffect effect,
695 unsigned timeout)
33e90275 696{
ab9a0b84
VZ
697 return wxWidgetCocoaImpl::
698 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
33e90275
SC
699}
700
701void wxNonOwnedWindowCocoaImpl::Update()
702{
703 [m_macWindow displayIfNeeded];
704}
705
706bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
707{
708 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
709 return true;
710}
711
776036d6 712bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
33e90275 713{
776036d6
SC
714 [m_macWindow setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
715 green:(CGFloat) (col.Green() / 255.0)
716 blue:(CGFloat) (col.Blue() / 255.0)
717 alpha:(CGFloat) (col.Alpha() / 255.0)]];
33e90275
SC
718 return true;
719}
720
721void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
722{
723 if ( m_macWindow )
724 {
725 bool metal = exStyle & wxFRAME_EX_METAL ;
726 int windowStyle = [ m_macWindow styleMask];
727 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
728 {
9a83f860 729 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
33e90275
SC
730 }
731 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
732 {
9a83f860 733 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
03647350 734 }
33e90275
SC
735 }
736}
03647350 737
b6dc21e7
KO
738void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
739{
740 if (m_macWindow)
741 {
742 CGWindowLevel level = kCGNormalWindowLevel;
743
744 if (style & wxSTAY_ON_TOP)
745 level = kCGUtilityWindowLevel;
746 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
747 level = kCGFloatingWindowLevel;
748
749 [m_macWindow setLevel: level];
9d243a47 750 m_macWindowLevel = level;
b6dc21e7
KO
751 }
752}
753
eabe8426 754bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
33e90275 755{
eabe8426
KO
756 if ( style == wxBG_STYLE_TRANSPARENT )
757 {
758 [m_macWindow setOpaque:NO];
759 [m_macWindow setBackgroundColor:[NSColor clearColor]];
760 }
761
33e90275
SC
762 return true;
763}
03647350 764
33e90275
SC
765bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
766{
767 return true;
768}
769
770void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
771{
772 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
54f11060
SC
773 // do not trigger refreshes upon invisible and possible partly created objects
774 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
33e90275
SC
775}
776
777void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
778{
779 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
780 x = r.GetLeft();
781 y = r.GetTop();
782}
783
784void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
785{
786 NSRect rect = [m_macWindow frame];
e490b0d2
VZ
787 width = (int)rect.size.width;
788 height = (int)rect.size.height;
33e90275
SC
789}
790
b0ec7fbb 791void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
33e90275 792{
33e90275 793 NSRect rect = [[m_macWindow contentView] frame];
e490b0d2
VZ
794 left = (int)rect.origin.x;
795 top = (int)rect.origin.y;
796 width = (int)rect.size.width;
797 height = (int)rect.size.height;
33e90275 798}
03647350 799
20ede392 800bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
33e90275 801{
eabe8426
KO
802 [m_macWindow setOpaque:NO];
803 [m_macWindow setBackgroundColor:[NSColor clearColor]];
804
805 return true;
33e90275
SC
806}
807
03647350 808void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
33e90275
SC
809{
810 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
811}
03647350 812
33e90275
SC
813bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
814{
cbe06760
VZ
815 if (([m_macWindow styleMask] & NSResizableWindowMask) != 0)
816 {
817 return [m_macWindow isZoomed];
818 }
819 else
820 {
821 NSRect rectScreen = [[NSScreen mainScreen] visibleFrame];
822 NSRect rectWindow = [m_macWindow frame];
823 return (rectScreen.origin.x == rectWindow.origin.x &&
824 rectScreen.origin.y == rectWindow.origin.y &&
825 rectScreen.size.width == rectWindow.size.width &&
826 rectScreen.size.height == rectWindow.size.height);
827 }
33e90275 828}
03647350 829
33e90275
SC
830bool wxNonOwnedWindowCocoaImpl::IsIconized() const
831{
832 return [m_macWindow isMiniaturized];
833}
03647350 834
33e90275
SC
835void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
836{
837 if ( iconize )
838 [m_macWindow miniaturize:nil];
839 else
840 [m_macWindow deminiaturize:nil];
841}
03647350 842
20ede392 843void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
33e90275
SC
844{
845 [m_macWindow zoom:nil];
846}
03647350 847
33e90275
SC
848
849// http://cocoadevcentral.com/articles/000028.php
850
851typedef struct
852{
773db5df 853 NSUInteger m_formerStyleMask;
33e90275
SC
854 int m_formerLevel;
855 NSRect m_formerFrame;
856} FullScreenData ;
857
858bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
859{
860 return m_macFullScreenData != NULL ;
861}
03647350 862
20ede392 863bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
33e90275
SC
864{
865 if ( show )
866 {
867 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
868 delete data ;
869 data = new FullScreenData();
870
871 m_macFullScreenData = data ;
872 data->m_formerLevel = [m_macWindow level];
873 data->m_formerFrame = [m_macWindow frame];
773db5df 874 data->m_formerStyleMask = [m_macWindow styleMask];
2077a6ad
SC
875#if 0
876 // CGDisplayCapture( kCGDirectMainDisplay );
877 //[m_macWindow setLevel:NSMainMenuWindowLevel+1/*CGShieldingWindowLevel()*/];
878#endif
247bb510
VZ
879 NSRect screenframe = [[NSScreen mainScreen] frame];
880 NSRect frame = NSMakeRect (0, 0, 100, 100);
881 NSRect contentRect;
773db5df
SC
882
883#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
884 if ( [ m_macWindow respondsToSelector:@selector(setStyleMask:) ] )
885 [m_macWindow setStyleMask:data->m_formerStyleMask & ~ NSResizableWindowMask];
886#endif
887
247bb510 888 contentRect = [NSWindow contentRectForFrameRect: frame
d1fc9578 889 styleMask: [m_macWindow styleMask]];
247bb510
VZ
890 screenframe.origin.y += (frame.origin.y - contentRect.origin.y);
891 screenframe.size.height += (frame.size.height - contentRect.size.height);
892 [m_macWindow setFrame:screenframe display:YES];
2077a6ad 893
d1fc9578 894 SetSystemUIMode(kUIModeAllHidden,
2077a6ad 895 kUIOptionDisableAppleMenu
d1fc9578 896 /*
2077a6ad 897 | kUIOptionDisableProcessSwitch
d1fc9578
SC
898 | kUIOptionDisableForceQuit
899 */);
33e90275
SC
900 }
901 else if ( m_macFullScreenData != NULL )
902 {
903 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
2077a6ad
SC
904#if 0
905 // CGDisplayRelease( kCGDirectMainDisplay );
906 // [m_macWindow setLevel:data->m_formerLevel];
907#endif
908
33e90275 909 [m_macWindow setFrame:data->m_formerFrame display:YES];
773db5df
SC
910#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
911 if ( [ m_macWindow respondsToSelector:@selector(setStyleMask:) ] )
912 [m_macWindow setStyleMask:data->m_formerStyleMask];
913#endif
33e90275
SC
914 delete data ;
915 m_macFullScreenData = NULL ;
2077a6ad 916
d1fc9578 917 SetSystemUIMode(kUIModeNormal, 0);
33e90275 918 }
03647350 919
33e90275
SC
920 return true;
921}
922
32b8e701 923void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
33e90275 924{
98182acc 925 NSRequestUserAttentionType flagsOSX;
32b8e701
VZ
926 switch ( flagsWX )
927 {
928 case wxUSER_ATTENTION_INFO:
929 flagsOSX = NSInformationalRequest;
930 break;
931
932 case wxUSER_ATTENTION_ERROR:
933 flagsOSX = NSCriticalRequest;
934 break;
935
936 default:
937 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
938 return;
939 }
940
941 [NSApp requestUserAttention:flagsOSX];
33e90275 942}
dbeddfb9
SC
943
944void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
945{
946 wxPoint p((x ? *x : 0), (y ? *y : 0) );
dbeddfb9 947 NSPoint nspt = wxToNSPoint( NULL, p );
54f11060
SC
948 nspt = [m_macWindow convertScreenToBase:nspt];
949 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
950 p = wxFromNSPoint([m_macWindow contentView], nspt);
dbeddfb9 951 if ( x )
03647350 952 *x = p.x;
dbeddfb9
SC
953 if ( y )
954 *y = p.y;
955}
956
957void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
958{
54f11060
SC
959 wxPoint p((x ? *x : 0), (y ? *y : 0) );
960 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
961 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
962 nspt = [m_macWindow convertBaseToScreen:nspt];
963 p = wxFromNSPoint( NULL, nspt);
dbeddfb9
SC
964 if ( x )
965 *x = p.x;
966 if ( y )
967 *y = p.y;
968}
969
dbc7ceb9
KO
970bool wxNonOwnedWindowCocoaImpl::IsActive()
971{
972 return [m_macWindow isKeyWindow];
973}
974
efb2fa41
KO
975void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
976{
977 [m_macWindow setDocumentEdited:modified];
978}
979
ebf7d5c4 980bool wxNonOwnedWindowCocoaImpl::IsModified() const
efb2fa41
KO
981{
982 return [m_macWindow isDocumentEdited];
983}
984
6a0e4ead
VZ
985void wxNonOwnedWindowCocoaImpl::SetRepresentedFilename(const wxString& filename)
986{
987 [m_macWindow setRepresentedFilename:wxCFStringRef(filename).AsNSString()];
988}
989
9d243a47
SC
990void wxNonOwnedWindowCocoaImpl::RestoreWindowLevel()
991{
992 if ( [m_macWindow level] != m_macWindowLevel )
993 [m_macWindow setLevel:m_macWindowLevel];
994}
995
996//
997//
998//
999
17e2694c
SC
1000wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
1001{
1002 wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
1003 now->Create( parent, nativeWindow );
1004 return now;
1005}
1006
dbeddfb9
SC
1007wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
1008 long style, long extraStyle, const wxString& name )
1009{
1010 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
1011 now->Create( parent, pos, size, style , extraStyle, name );
1012 return now;
54f11060 1013}
9d243a47 1014