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