]>
Commit | Line | Data |
---|---|---|
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: 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" | |
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 | #endif | |
19 | ||
20 | #include "wx/osx/private.h" | |
21 | ||
22 | NSScreen* wxOSXGetMenuScreen() | |
23 | { | |
24 | if ( [NSScreen screens] == nil ) | |
25 | return [NSScreen mainScreen]; | |
26 | else | |
27 | { | |
28 | return [[NSScreen screens] objectAtIndex:0]; | |
29 | } | |
30 | } | |
31 | ||
32 | NSRect wxToNSRect( NSView* parent, const wxRect& r ) | |
33 | { | |
34 | NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame]; | |
35 | int y = r.y; | |
36 | int x = r.x ; | |
37 | if ( parent == NULL || ![ parent isFlipped ] ) | |
38 | y = (int)(frame.size.height - ( r.y + r.height )); | |
39 | return NSMakeRect(x, y, r.width , r.height); | |
40 | } | |
41 | ||
42 | wxRect wxFromNSRect( NSView* parent, const NSRect& rect ) | |
43 | { | |
44 | NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame]; | |
45 | int y = (int)rect.origin.y; | |
46 | int x = (int)rect.origin.x; | |
47 | if ( parent == NULL || ![ parent isFlipped ] ) | |
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 ); | |
50 | } | |
51 | ||
52 | NSPoint wxToNSPoint( NSView* parent, const wxPoint& p ) | |
53 | { | |
54 | NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame]; | |
55 | int x = p.x ; | |
56 | int y = p.y; | |
57 | if ( parent == NULL || ![ parent isFlipped ] ) | |
58 | y = (int)(frame.size.height - ( p.y )); | |
59 | return NSMakePoint(x, y); | |
60 | } | |
61 | ||
62 | wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p ) | |
63 | { | |
64 | NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame]; | |
65 | int x = (int)p.x; | |
66 | int y = (int)p.y; | |
67 | if ( parent == NULL || ![ parent isFlipped ] ) | |
68 | y = (int)(frame.size.height - ( p.y )); | |
69 | return wxPoint( x, y); | |
70 | } | |
71 | ||
72 | bool shouldHandleSelector(SEL selector) | |
73 | { | |
74 | if (selector == @selector(noop:) | |
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; | |
87 | ||
88 | return true; | |
89 | ||
90 | } | |
91 | ||
92 | // | |
93 | // wx category for NSWindow (our own and wrapped instances) | |
94 | // | |
95 | ||
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 | // | |
134 | ||
135 | @interface wxNSWindow : NSWindow | |
136 | { | |
137 | } | |
138 | ||
139 | - (void) sendEvent:(NSEvent *)event; | |
140 | - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen; | |
141 | - (void)noResponderFor: (SEL) selector; | |
142 | @end | |
143 | ||
144 | @implementation wxNSWindow | |
145 | ||
146 | - (void)sendEvent:(NSEvent *) event | |
147 | { | |
148 | if ( ![self WX_filterSendEvent: event] ) | |
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 | ||
156 | [super sendEvent: event]; | |
157 | ||
158 | if (wxTheApp) | |
159 | wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler); | |
160 | } | |
161 | } | |
162 | ||
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 | { | |
167 | wxUnusedVar(screen); | |
168 | return frameRect; | |
169 | } | |
170 | ||
171 | - (void)doCommandBySelector:(SEL)selector | |
172 | { | |
173 | if (shouldHandleSelector(selector) && | |
174 | !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) ) | |
175 | [super doCommandBySelector:selector]; | |
176 | } | |
177 | ||
178 | ||
179 | // NB: if we don't do this, all key downs that get handled lead to a NSBeep | |
180 | - (void)noResponderFor: (SEL) selector | |
181 | { | |
182 | if (selector != @selector(keyDown:) && selector != @selector(keyUp:)) | |
183 | { | |
184 | [super noResponderFor:selector]; | |
185 | } | |
186 | } | |
187 | ||
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 | ||
196 | @end | |
197 | ||
198 | @interface wxNSPanel : NSPanel | |
199 | { | |
200 | } | |
201 | ||
202 | - (void)noResponderFor: (SEL) selector; | |
203 | - (void)sendEvent:(NSEvent *)event; | |
204 | @end | |
205 | ||
206 | @implementation wxNSPanel | |
207 | ||
208 | - (BOOL)canBecomeKeyWindow | |
209 | { | |
210 | return YES; | |
211 | } | |
212 | ||
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 | |
220 | - (void)noResponderFor: (SEL) selector | |
221 | { | |
222 | if (selector != @selector(keyDown:) && selector != @selector(keyUp:)) | |
223 | { | |
224 | [super noResponderFor:selector]; | |
225 | } | |
226 | } | |
227 | ||
228 | - (void)sendEvent:(NSEvent *) event | |
229 | { | |
230 | if ( ![self WX_filterSendEvent: event] ) | |
231 | [super sendEvent: event]; | |
232 | } | |
233 | ||
234 | @end | |
235 | ||
236 | ||
237 | // | |
238 | // controller | |
239 | // | |
240 | ||
241 | @interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>) | |
242 | { | |
243 | } | |
244 | ||
245 | - (void)windowDidResize:(NSNotification *)notification; | |
246 | - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize; | |
247 | - (void)windowDidResignKey:(NSNotification *)notification; | |
248 | - (void)windowDidBecomeKey:(NSNotification *)notification; | |
249 | - (void)windowDidMove:(NSNotification *)notification; | |
250 | - (BOOL)windowShouldClose:(id)window; | |
251 | - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame; | |
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 | { | |
265 | wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation]; | |
266 | if ( windowimpl ) | |
267 | { | |
268 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); | |
269 | if ( wxpeer ) | |
270 | wxpeer->Close(); | |
271 | } | |
272 | return NO; | |
273 | } | |
274 | ||
275 | - (NSSize)windowWillResize:(NSWindow *)window | |
276 | toSize:(NSSize)proposedFrameSize | |
277 | { | |
278 | NSRect frame = [window frame]; | |
279 | wxRect wxframe = wxFromNSRect( NULL, frame ); | |
280 | wxframe.SetWidth( (int)proposedFrameSize.width ); | |
281 | wxframe.SetHeight( (int)proposedFrameSize.height ); | |
282 | ||
283 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; | |
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 | ||
295 | return proposedFrameSize; | |
296 | } | |
297 | ||
298 | - (void)windowDidResize:(NSNotification *)notification | |
299 | { | |
300 | NSWindow* window = (NSWindow*) [notification object]; | |
301 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; | |
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]; | |
313 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; | |
314 | if ( windowimpl ) | |
315 | { | |
316 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); | |
317 | if ( wxpeer ) | |
318 | wxpeer->HandleMoved(0); | |
319 | } | |
320 | } | |
321 | ||
322 | - (void)windowDidBecomeKey:(NSNotification *)notification | |
323 | { | |
324 | NSWindow* window = (NSWindow*) [notification object]; | |
325 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; | |
326 | if ( windowimpl ) | |
327 | { | |
328 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); | |
329 | if ( wxpeer ) | |
330 | wxpeer->HandleActivated(0, true); | |
331 | } | |
332 | } | |
333 | ||
334 | - (void)windowDidResignKey:(NSNotification *)notification | |
335 | { | |
336 | NSWindow* window = (NSWindow*) [notification object]; | |
337 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; | |
338 | if ( windowimpl ) | |
339 | { | |
340 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); | |
341 | if ( wxpeer ) | |
342 | { | |
343 | wxpeer->HandleActivated(0, false); | |
344 | // Needed for popup window since the firstResponder | |
345 | // (focus in wx) doesn't change when this | |
346 | // TLW becomes inactive. | |
347 | wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId()); | |
348 | event.SetEventObject(wxpeer); | |
349 | wxpeer->HandleWindowEvent(event); | |
350 | } | |
351 | } | |
352 | } | |
353 | ||
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 | ||
374 | - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame | |
375 | { | |
376 | wxUnusedVar(newFrame); | |
377 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; | |
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 | ||
388 | @end | |
389 | ||
390 | IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl ) | |
391 | ||
392 | wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) : | |
393 | wxNonOwnedWindowImpl(nonownedwnd) | |
394 | { | |
395 | m_macWindow = NULL; | |
396 | m_macFullScreenData = NULL; | |
397 | } | |
398 | ||
399 | wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl() | |
400 | { | |
401 | m_macWindow = NULL; | |
402 | m_macFullScreenData = NULL; | |
403 | } | |
404 | ||
405 | wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl() | |
406 | { | |
407 | if ( !m_wxPeer->IsNativeWindowWrapper() ) | |
408 | { | |
409 | [m_macWindow setDelegate:nil]; | |
410 | [m_macWindow release]; | |
411 | } | |
412 | } | |
413 | ||
414 | void wxNonOwnedWindowCocoaImpl::WillBeDestroyed() | |
415 | { | |
416 | if ( !m_wxPeer->IsNativeWindowWrapper() ) | |
417 | { | |
418 | [m_macWindow setDelegate:nil]; | |
419 | } | |
420 | } | |
421 | ||
422 | void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size, | |
423 | long style, long extraStyle, const wxString& WXUNUSED(name) ) | |
424 | { | |
425 | static wxNonOwnedWindowController* controller = NULL; | |
426 | ||
427 | if ( !controller ) | |
428 | controller =[[wxNonOwnedWindowController alloc] init]; | |
429 | ||
430 | ||
431 | int windowstyle = NSBorderlessWindowMask; | |
432 | ||
433 | if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) || | |
434 | GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG ) | |
435 | { | |
436 | m_macWindow = [wxNSPanel alloc]; | |
437 | } | |
438 | else | |
439 | m_macWindow = [wxNSWindow alloc]; | |
440 | ||
441 | CGWindowLevel level = kCGNormalWindowLevel; | |
442 | ||
443 | if ( style & wxFRAME_TOOL_WINDOW ) | |
444 | { | |
445 | windowstyle |= NSUtilityWindowMask; | |
446 | if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) || | |
447 | ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) ) | |
448 | { | |
449 | windowstyle |= NSTitledWindowMask ; | |
450 | } | |
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 | ||
514 | if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) ) | |
515 | level = kCGFloatingWindowLevel; | |
516 | ||
517 | if ( ( style & wxSTAY_ON_TOP ) ) | |
518 | level = kCGUtilityWindowLevel; | |
519 | ||
520 | NSRect r = wxToNSRect( NULL, wxRect( pos, size) ); | |
521 | ||
522 | r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle]; | |
523 | ||
524 | [m_macWindow initWithContentRect:r | |
525 | styleMask:windowstyle | |
526 | backing:NSBackingStoreBuffered | |
527 | defer:NO | |
528 | ]; | |
529 | ||
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 | ||
553 | [m_macWindow setLevel:level]; | |
554 | ||
555 | [m_macWindow setDelegate:controller]; | |
556 | ||
557 | [m_macWindow setAcceptsMouseMovedEvents: YES]; | |
558 | ||
559 | if ( ( style & wxFRAME_SHAPED) ) | |
560 | { | |
561 | [m_macWindow setOpaque:NO]; | |
562 | [m_macWindow setAlphaValue:1.0]; | |
563 | } | |
564 | ||
565 | if ( !(style & wxFRAME_TOOL_WINDOW) ) | |
566 | [m_macWindow setHidesOnDeactivate:NO]; | |
567 | } | |
568 | ||
569 | void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow ) | |
570 | { | |
571 | m_macWindow = nativeWindow; | |
572 | } | |
573 | ||
574 | WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const | |
575 | { | |
576 | return m_macWindow; | |
577 | } | |
578 | ||
579 | void wxNonOwnedWindowCocoaImpl::Raise() | |
580 | { | |
581 | [m_macWindow orderWindow:NSWindowAbove relativeTo:0]; | |
582 | } | |
583 | ||
584 | void wxNonOwnedWindowCocoaImpl::Lower() | |
585 | { | |
586 | [m_macWindow orderWindow:NSWindowBelow relativeTo:0]; | |
587 | } | |
588 | ||
589 | void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating() | |
590 | { | |
591 | [m_macWindow orderFront:nil]; | |
592 | [[m_macWindow contentView] setNeedsDisplay: YES]; | |
593 | } | |
594 | ||
595 | bool wxNonOwnedWindowCocoaImpl::Show(bool show) | |
596 | { | |
597 | if ( show ) | |
598 | { | |
599 | wxNonOwnedWindow* wxpeer = GetWXPeer(); | |
600 | if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW)) | |
601 | [m_macWindow makeKeyAndOrderFront:nil]; | |
602 | else | |
603 | [m_macWindow orderFront:nil]; | |
604 | [[m_macWindow contentView] setNeedsDisplay: YES]; | |
605 | } | |
606 | else | |
607 | [m_macWindow orderOut:nil]; | |
608 | return true; | |
609 | } | |
610 | ||
611 | bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, | |
612 | wxShowEffect effect, | |
613 | unsigned timeout) | |
614 | { | |
615 | return wxWidgetCocoaImpl:: | |
616 | ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout); | |
617 | } | |
618 | ||
619 | void wxNonOwnedWindowCocoaImpl::Update() | |
620 | { | |
621 | [m_macWindow displayIfNeeded]; | |
622 | } | |
623 | ||
624 | bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha) | |
625 | { | |
626 | [m_macWindow setAlphaValue:(CGFloat) alpha/255.0]; | |
627 | return true; | |
628 | } | |
629 | ||
630 | bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) ) | |
631 | { | |
632 | return true; | |
633 | } | |
634 | ||
635 | void 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 | { | |
643 | wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") ); | |
644 | } | |
645 | else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) ) | |
646 | { | |
647 | wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") ); | |
648 | } | |
649 | } | |
650 | } | |
651 | ||
652 | void 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 | ||
667 | bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style) | |
668 | { | |
669 | if ( style == wxBG_STYLE_TRANSPARENT ) | |
670 | { | |
671 | [m_macWindow setOpaque:NO]; | |
672 | [m_macWindow setBackgroundColor:[NSColor clearColor]]; | |
673 | } | |
674 | ||
675 | return true; | |
676 | } | |
677 | ||
678 | bool wxNonOwnedWindowCocoaImpl::CanSetTransparent() | |
679 | { | |
680 | return true; | |
681 | } | |
682 | ||
683 | void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height) | |
684 | { | |
685 | NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) ); | |
686 | // do not trigger refreshes upon invisible and possible partly created objects | |
687 | [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()]; | |
688 | } | |
689 | ||
690 | void 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 | ||
697 | void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const | |
698 | { | |
699 | NSRect rect = [m_macWindow frame]; | |
700 | width = (int)rect.size.width; | |
701 | height = (int)rect.size.height; | |
702 | } | |
703 | ||
704 | void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const | |
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]; | |
709 | left = (int)rect.origin.x; | |
710 | top = (int)rect.origin.y; | |
711 | width = (int)rect.size.width; | |
712 | height = (int)rect.size.height; | |
713 | } | |
714 | ||
715 | bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region)) | |
716 | { | |
717 | [m_macWindow setOpaque:NO]; | |
718 | [m_macWindow setBackgroundColor:[NSColor clearColor]]; | |
719 | ||
720 | return true; | |
721 | } | |
722 | ||
723 | void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding ) | |
724 | { | |
725 | [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()]; | |
726 | } | |
727 | ||
728 | bool wxNonOwnedWindowCocoaImpl::IsMaximized() const | |
729 | { | |
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 | } | |
743 | } | |
744 | ||
745 | bool wxNonOwnedWindowCocoaImpl::IsIconized() const | |
746 | { | |
747 | return [m_macWindow isMiniaturized]; | |
748 | } | |
749 | ||
750 | void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize ) | |
751 | { | |
752 | if ( iconize ) | |
753 | [m_macWindow miniaturize:nil]; | |
754 | else | |
755 | [m_macWindow deminiaturize:nil]; | |
756 | } | |
757 | ||
758 | void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize)) | |
759 | { | |
760 | [m_macWindow zoom:nil]; | |
761 | } | |
762 | ||
763 | ||
764 | // http://cocoadevcentral.com/articles/000028.php | |
765 | ||
766 | typedef struct | |
767 | { | |
768 | int m_formerLevel; | |
769 | NSRect m_formerFrame; | |
770 | } FullScreenData ; | |
771 | ||
772 | bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const | |
773 | { | |
774 | return m_macFullScreenData != NULL ; | |
775 | } | |
776 | ||
777 | bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style)) | |
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 | } | |
801 | ||
802 | return true; | |
803 | } | |
804 | ||
805 | void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX) | |
806 | { | |
807 | NSRequestUserAttentionType flagsOSX; | |
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]; | |
824 | } | |
825 | ||
826 | void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y ) | |
827 | { | |
828 | wxPoint p((x ? *x : 0), (y ? *y : 0) ); | |
829 | NSPoint nspt = wxToNSPoint( NULL, p ); | |
830 | nspt = [m_macWindow convertScreenToBase:nspt]; | |
831 | nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil]; | |
832 | p = wxFromNSPoint([m_macWindow contentView], nspt); | |
833 | if ( x ) | |
834 | *x = p.x; | |
835 | if ( y ) | |
836 | *y = p.y; | |
837 | } | |
838 | ||
839 | void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y ) | |
840 | { | |
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); | |
846 | if ( x ) | |
847 | *x = p.x; | |
848 | if ( y ) | |
849 | *y = p.y; | |
850 | } | |
851 | ||
852 | bool wxNonOwnedWindowCocoaImpl::IsActive() | |
853 | { | |
854 | return [m_macWindow isKeyWindow]; | |
855 | } | |
856 | ||
857 | void wxNonOwnedWindowCocoaImpl::SetModified(bool modified) | |
858 | { | |
859 | [m_macWindow setDocumentEdited:modified]; | |
860 | } | |
861 | ||
862 | bool wxNonOwnedWindowCocoaImpl::IsModified() const | |
863 | { | |
864 | return [m_macWindow isDocumentEdited]; | |
865 | } | |
866 | ||
867 | wxNonOwnedWindowImpl* 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 | ||
874 | wxNonOwnedWindowImpl* 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; | |
880 | } |