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