| 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 | - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen; |
| 203 | - (void)noResponderFor: (SEL) selector; |
| 204 | - (void)sendEvent:(NSEvent *)event; |
| 205 | @end |
| 206 | |
| 207 | @implementation wxNSPanel |
| 208 | |
| 209 | - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen |
| 210 | { |
| 211 | wxNonOwnedWindowCocoaImpl* impl = (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self ); |
| 212 | if (impl && impl->IsFullScreen()) |
| 213 | return frameRect; |
| 214 | else |
| 215 | return [super constrainFrameRect:frameRect toScreen:screen]; |
| 216 | } |
| 217 | |
| 218 | - (BOOL)canBecomeKeyWindow |
| 219 | { |
| 220 | return YES; |
| 221 | } |
| 222 | |
| 223 | - (void)doCommandBySelector:(SEL)selector |
| 224 | { |
| 225 | if (shouldHandleSelector(selector)) |
| 226 | [super doCommandBySelector:selector]; |
| 227 | } |
| 228 | |
| 229 | // NB: if we don't do this, it seems that all events that end here lead to a NSBeep |
| 230 | - (void)noResponderFor: (SEL) selector |
| 231 | { |
| 232 | if (selector != @selector(keyDown:) && selector != @selector(keyUp:)) |
| 233 | { |
| 234 | [super noResponderFor:selector]; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | - (void)sendEvent:(NSEvent *) event |
| 239 | { |
| 240 | if ( ![self WX_filterSendEvent: event] ) |
| 241 | [super sendEvent: event]; |
| 242 | } |
| 243 | |
| 244 | @end |
| 245 | |
| 246 | |
| 247 | // |
| 248 | // controller |
| 249 | // |
| 250 | |
| 251 | @interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | - (void)windowDidResize:(NSNotification *)notification; |
| 256 | - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize; |
| 257 | - (void)windowDidResignKey:(NSNotification *)notification; |
| 258 | - (void)windowDidBecomeKey:(NSNotification *)notification; |
| 259 | - (void)windowDidMove:(NSNotification *)notification; |
| 260 | - (BOOL)windowShouldClose:(id)window; |
| 261 | - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame; |
| 262 | |
| 263 | @end |
| 264 | |
| 265 | @implementation wxNonOwnedWindowController |
| 266 | |
| 267 | - (id) init |
| 268 | { |
| 269 | [super init]; |
| 270 | return self; |
| 271 | } |
| 272 | |
| 273 | - (BOOL)windowShouldClose:(id)nwindow |
| 274 | { |
| 275 | wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation]; |
| 276 | if ( windowimpl ) |
| 277 | { |
| 278 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); |
| 279 | if ( wxpeer ) |
| 280 | wxpeer->Close(); |
| 281 | } |
| 282 | return NO; |
| 283 | } |
| 284 | |
| 285 | - (NSSize)windowWillResize:(NSWindow *)window |
| 286 | toSize:(NSSize)proposedFrameSize |
| 287 | { |
| 288 | NSRect frame = [window frame]; |
| 289 | wxRect wxframe = wxFromNSRect( NULL, frame ); |
| 290 | wxframe.SetWidth( (int)proposedFrameSize.width ); |
| 291 | wxframe.SetHeight( (int)proposedFrameSize.height ); |
| 292 | |
| 293 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; |
| 294 | if ( windowimpl ) |
| 295 | { |
| 296 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); |
| 297 | if ( wxpeer ) |
| 298 | { |
| 299 | wxpeer->HandleResizing( 0, &wxframe ); |
| 300 | NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight()); |
| 301 | return newSize; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return proposedFrameSize; |
| 306 | } |
| 307 | |
| 308 | - (void)windowDidResize:(NSNotification *)notification |
| 309 | { |
| 310 | NSWindow* window = (NSWindow*) [notification object]; |
| 311 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; |
| 312 | if ( windowimpl ) |
| 313 | { |
| 314 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); |
| 315 | if ( wxpeer ) |
| 316 | wxpeer->HandleResized(0); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | - (void)windowDidMove:(NSNotification *)notification |
| 321 | { |
| 322 | wxNSWindow* window = (wxNSWindow*) [notification object]; |
| 323 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; |
| 324 | if ( windowimpl ) |
| 325 | { |
| 326 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); |
| 327 | if ( wxpeer ) |
| 328 | wxpeer->HandleMoved(0); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | - (void)windowDidBecomeKey:(NSNotification *)notification |
| 333 | { |
| 334 | NSWindow* window = (NSWindow*) [notification object]; |
| 335 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; |
| 336 | if ( windowimpl ) |
| 337 | { |
| 338 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); |
| 339 | if ( wxpeer ) |
| 340 | wxpeer->HandleActivated(0, true); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | - (void)windowDidResignKey:(NSNotification *)notification |
| 345 | { |
| 346 | NSWindow* window = (NSWindow*) [notification object]; |
| 347 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; |
| 348 | if ( windowimpl ) |
| 349 | { |
| 350 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); |
| 351 | if ( wxpeer ) |
| 352 | { |
| 353 | wxpeer->HandleActivated(0, false); |
| 354 | // Needed for popup window since the firstResponder |
| 355 | // (focus in wx) doesn't change when this |
| 356 | // TLW becomes inactive. |
| 357 | wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId()); |
| 358 | event.SetEventObject(wxpeer); |
| 359 | wxpeer->HandleWindowEvent(event); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject |
| 365 | { |
| 366 | wxUnusedVar(sender); |
| 367 | |
| 368 | if ([anObject isKindOfClass:[wxNSTextField class]]) |
| 369 | { |
| 370 | wxNSTextField* tf = (wxNSTextField*) anObject; |
| 371 | wxNSTextFieldEditor* editor = [tf fieldEditor]; |
| 372 | if ( editor == nil ) |
| 373 | { |
| 374 | editor = [[wxNSTextFieldEditor alloc] init]; |
| 375 | [editor setFieldEditor:YES]; |
| 376 | [tf setFieldEditor:editor]; |
| 377 | } |
| 378 | return editor; |
| 379 | } |
| 380 | |
| 381 | return nil; |
| 382 | } |
| 383 | |
| 384 | - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame |
| 385 | { |
| 386 | wxUnusedVar(newFrame); |
| 387 | wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation]; |
| 388 | if ( windowimpl ) |
| 389 | { |
| 390 | wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); |
| 391 | wxMaximizeEvent event(wxpeer->GetId()); |
| 392 | event.SetEventObject(wxpeer); |
| 393 | return !wxpeer->HandleWindowEvent(event); |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | @end |
| 399 | |
| 400 | IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl ) |
| 401 | |
| 402 | wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) : |
| 403 | wxNonOwnedWindowImpl(nonownedwnd) |
| 404 | { |
| 405 | m_macWindow = NULL; |
| 406 | m_macFullScreenData = NULL; |
| 407 | } |
| 408 | |
| 409 | wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl() |
| 410 | { |
| 411 | m_macWindow = NULL; |
| 412 | m_macFullScreenData = NULL; |
| 413 | } |
| 414 | |
| 415 | wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl() |
| 416 | { |
| 417 | if ( !m_wxPeer->IsNativeWindowWrapper() ) |
| 418 | { |
| 419 | [m_macWindow setDelegate:nil]; |
| 420 | [m_macWindow release]; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void wxNonOwnedWindowCocoaImpl::WillBeDestroyed() |
| 425 | { |
| 426 | if ( !m_wxPeer->IsNativeWindowWrapper() ) |
| 427 | { |
| 428 | [m_macWindow setDelegate:nil]; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size, |
| 433 | long style, long extraStyle, const wxString& WXUNUSED(name) ) |
| 434 | { |
| 435 | static wxNonOwnedWindowController* controller = NULL; |
| 436 | |
| 437 | if ( !controller ) |
| 438 | controller =[[wxNonOwnedWindowController alloc] init]; |
| 439 | |
| 440 | |
| 441 | int windowstyle = NSBorderlessWindowMask; |
| 442 | |
| 443 | if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) || |
| 444 | GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG ) |
| 445 | { |
| 446 | m_macWindow = [wxNSPanel alloc]; |
| 447 | } |
| 448 | else |
| 449 | m_macWindow = [wxNSWindow alloc]; |
| 450 | |
| 451 | CGWindowLevel level = kCGNormalWindowLevel; |
| 452 | |
| 453 | if ( style & wxFRAME_TOOL_WINDOW ) |
| 454 | { |
| 455 | windowstyle |= NSUtilityWindowMask; |
| 456 | if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) || |
| 457 | ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) ) |
| 458 | { |
| 459 | windowstyle |= NSTitledWindowMask ; |
| 460 | } |
| 461 | } |
| 462 | else if ( ( style & wxPOPUP_WINDOW ) ) |
| 463 | { |
| 464 | level = kCGPopUpMenuWindowLevel; |
| 465 | /* |
| 466 | if ( ( style & wxBORDER_NONE ) ) |
| 467 | { |
| 468 | wclass = kHelpWindowClass ; // has no border |
| 469 | attr |= kWindowNoShadowAttribute; |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | wclass = kPlainWindowClass ; // has a single line border, it will have to do for now |
| 474 | } |
| 475 | */ |
| 476 | } |
| 477 | else if ( ( style & wxCAPTION ) ) |
| 478 | { |
| 479 | windowstyle |= NSTitledWindowMask ; |
| 480 | } |
| 481 | else if ( ( style & wxFRAME_DRAWER ) ) |
| 482 | { |
| 483 | /* |
| 484 | wclass = kDrawerWindowClass; |
| 485 | */ |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | // set these even if we have no title, otherwise the controls won't be visible |
| 490 | if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) || |
| 491 | ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) ) |
| 492 | { |
| 493 | windowstyle |= NSTitledWindowMask ; |
| 494 | } |
| 495 | /* |
| 496 | else if ( ( style & wxNO_BORDER ) ) |
| 497 | { |
| 498 | wclass = kSimpleWindowClass ; |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | wclass = kPlainWindowClass ; |
| 503 | } |
| 504 | */ |
| 505 | } |
| 506 | |
| 507 | if ( windowstyle & NSTitledWindowMask ) |
| 508 | { |
| 509 | if ( ( style & wxMINIMIZE_BOX ) ) |
| 510 | windowstyle |= NSMiniaturizableWindowMask ; |
| 511 | |
| 512 | if ( ( style & wxMAXIMIZE_BOX ) ) |
| 513 | windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ? |
| 514 | |
| 515 | if ( ( style & wxRESIZE_BORDER ) ) |
| 516 | windowstyle |= NSResizableWindowMask ; |
| 517 | |
| 518 | if ( ( style & wxCLOSE_BOX) ) |
| 519 | windowstyle |= NSClosableWindowMask ; |
| 520 | } |
| 521 | if ( extraStyle & wxFRAME_EX_METAL) |
| 522 | windowstyle |= NSTexturedBackgroundWindowMask; |
| 523 | |
| 524 | if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) ) |
| 525 | level = kCGFloatingWindowLevel; |
| 526 | |
| 527 | if ( ( style & wxSTAY_ON_TOP ) ) |
| 528 | level = kCGUtilityWindowLevel; |
| 529 | |
| 530 | NSRect r = wxToNSRect( NULL, wxRect( pos, size) ); |
| 531 | |
| 532 | r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle]; |
| 533 | |
| 534 | [m_macWindow initWithContentRect:r |
| 535 | styleMask:windowstyle |
| 536 | backing:NSBackingStoreBuffered |
| 537 | defer:NO |
| 538 | ]; |
| 539 | |
| 540 | // If the parent is modal, windows with wxFRAME_FLOAT_ON_PARENT style need |
| 541 | // to be in kCGUtilityWindowLevel and not kCGFloatingWindowLevel to stay |
| 542 | // above the parent. |
| 543 | wxDialog * const parentDialog = wxDynamicCast(parent, wxDialog); |
| 544 | if (parentDialog && parentDialog->IsModal()) |
| 545 | { |
| 546 | if (level == kCGFloatingWindowLevel) |
| 547 | { |
| 548 | level = kCGUtilityWindowLevel; |
| 549 | } |
| 550 | |
| 551 | // Cocoa's modal loop does not process other windows by default, but |
| 552 | // don't call this on normal window levels so nested modal dialogs will |
| 553 | // still behave modally. |
| 554 | if (level != kCGNormalWindowLevel) |
| 555 | { |
| 556 | if ([m_macWindow isKindOfClass:[NSPanel class]]) |
| 557 | { |
| 558 | [(NSPanel*)m_macWindow setWorksWhenModal:YES]; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | [m_macWindow setLevel:level]; |
| 564 | |
| 565 | [m_macWindow setDelegate:controller]; |
| 566 | |
| 567 | [m_macWindow setAcceptsMouseMovedEvents: YES]; |
| 568 | |
| 569 | if ( ( style & wxFRAME_SHAPED) ) |
| 570 | { |
| 571 | [m_macWindow setOpaque:NO]; |
| 572 | [m_macWindow setAlphaValue:1.0]; |
| 573 | } |
| 574 | |
| 575 | if ( !(style & wxFRAME_TOOL_WINDOW) ) |
| 576 | [m_macWindow setHidesOnDeactivate:NO]; |
| 577 | } |
| 578 | |
| 579 | void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow ) |
| 580 | { |
| 581 | m_macWindow = nativeWindow; |
| 582 | } |
| 583 | |
| 584 | WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const |
| 585 | { |
| 586 | return m_macWindow; |
| 587 | } |
| 588 | |
| 589 | void wxNonOwnedWindowCocoaImpl::Raise() |
| 590 | { |
| 591 | [m_macWindow orderWindow:NSWindowAbove relativeTo:0]; |
| 592 | } |
| 593 | |
| 594 | void wxNonOwnedWindowCocoaImpl::Lower() |
| 595 | { |
| 596 | [m_macWindow orderWindow:NSWindowBelow relativeTo:0]; |
| 597 | } |
| 598 | |
| 599 | void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating() |
| 600 | { |
| 601 | [m_macWindow orderFront:nil]; |
| 602 | [[m_macWindow contentView] setNeedsDisplay: YES]; |
| 603 | } |
| 604 | |
| 605 | bool wxNonOwnedWindowCocoaImpl::Show(bool show) |
| 606 | { |
| 607 | if ( show ) |
| 608 | { |
| 609 | wxNonOwnedWindow* wxpeer = GetWXPeer(); |
| 610 | if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW)) |
| 611 | [m_macWindow makeKeyAndOrderFront:nil]; |
| 612 | else |
| 613 | [m_macWindow orderFront:nil]; |
| 614 | [[m_macWindow contentView] setNeedsDisplay: YES]; |
| 615 | } |
| 616 | else |
| 617 | [m_macWindow orderOut:nil]; |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, |
| 622 | wxShowEffect effect, |
| 623 | unsigned timeout) |
| 624 | { |
| 625 | return wxWidgetCocoaImpl:: |
| 626 | ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout); |
| 627 | } |
| 628 | |
| 629 | void wxNonOwnedWindowCocoaImpl::Update() |
| 630 | { |
| 631 | [m_macWindow displayIfNeeded]; |
| 632 | } |
| 633 | |
| 634 | bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha) |
| 635 | { |
| 636 | [m_macWindow setAlphaValue:(CGFloat) alpha/255.0]; |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) ) |
| 641 | { |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle ) |
| 646 | { |
| 647 | if ( m_macWindow ) |
| 648 | { |
| 649 | bool metal = exStyle & wxFRAME_EX_METAL ; |
| 650 | int windowStyle = [ m_macWindow styleMask]; |
| 651 | if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) ) |
| 652 | { |
| 653 | wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") ); |
| 654 | } |
| 655 | else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) ) |
| 656 | { |
| 657 | wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") ); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style ) |
| 663 | { |
| 664 | if (m_macWindow) |
| 665 | { |
| 666 | CGWindowLevel level = kCGNormalWindowLevel; |
| 667 | |
| 668 | if (style & wxSTAY_ON_TOP) |
| 669 | level = kCGUtilityWindowLevel; |
| 670 | else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW )) |
| 671 | level = kCGFloatingWindowLevel; |
| 672 | |
| 673 | [m_macWindow setLevel: level]; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style) |
| 678 | { |
| 679 | if ( style == wxBG_STYLE_TRANSPARENT ) |
| 680 | { |
| 681 | [m_macWindow setOpaque:NO]; |
| 682 | [m_macWindow setBackgroundColor:[NSColor clearColor]]; |
| 683 | } |
| 684 | |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | bool wxNonOwnedWindowCocoaImpl::CanSetTransparent() |
| 689 | { |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height) |
| 694 | { |
| 695 | NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) ); |
| 696 | // do not trigger refreshes upon invisible and possible partly created objects |
| 697 | [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()]; |
| 698 | } |
| 699 | |
| 700 | void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const |
| 701 | { |
| 702 | wxRect r = wxFromNSRect( NULL, [m_macWindow frame] ); |
| 703 | x = r.GetLeft(); |
| 704 | y = r.GetTop(); |
| 705 | } |
| 706 | |
| 707 | void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const |
| 708 | { |
| 709 | NSRect rect = [m_macWindow frame]; |
| 710 | width = (int)rect.size.width; |
| 711 | height = (int)rect.size.height; |
| 712 | } |
| 713 | |
| 714 | void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const |
| 715 | { |
| 716 | NSRect outer = NSMakeRect(100,100,100,100); |
| 717 | NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ]; |
| 718 | NSRect rect = [[m_macWindow contentView] frame]; |
| 719 | left = (int)rect.origin.x; |
| 720 | top = (int)rect.origin.y; |
| 721 | width = (int)rect.size.width; |
| 722 | height = (int)rect.size.height; |
| 723 | } |
| 724 | |
| 725 | bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region)) |
| 726 | { |
| 727 | [m_macWindow setOpaque:NO]; |
| 728 | [m_macWindow setBackgroundColor:[NSColor clearColor]]; |
| 729 | |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding ) |
| 734 | { |
| 735 | [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()]; |
| 736 | } |
| 737 | |
| 738 | bool wxNonOwnedWindowCocoaImpl::IsMaximized() const |
| 739 | { |
| 740 | if (([m_macWindow styleMask] & NSResizableWindowMask) != 0) |
| 741 | { |
| 742 | return [m_macWindow isZoomed]; |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | NSRect rectScreen = [[NSScreen mainScreen] visibleFrame]; |
| 747 | NSRect rectWindow = [m_macWindow frame]; |
| 748 | return (rectScreen.origin.x == rectWindow.origin.x && |
| 749 | rectScreen.origin.y == rectWindow.origin.y && |
| 750 | rectScreen.size.width == rectWindow.size.width && |
| 751 | rectScreen.size.height == rectWindow.size.height); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | bool wxNonOwnedWindowCocoaImpl::IsIconized() const |
| 756 | { |
| 757 | return [m_macWindow isMiniaturized]; |
| 758 | } |
| 759 | |
| 760 | void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize ) |
| 761 | { |
| 762 | if ( iconize ) |
| 763 | [m_macWindow miniaturize:nil]; |
| 764 | else |
| 765 | [m_macWindow deminiaturize:nil]; |
| 766 | } |
| 767 | |
| 768 | void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize)) |
| 769 | { |
| 770 | [m_macWindow zoom:nil]; |
| 771 | } |
| 772 | |
| 773 | |
| 774 | // http://cocoadevcentral.com/articles/000028.php |
| 775 | |
| 776 | typedef struct |
| 777 | { |
| 778 | int m_formerLevel; |
| 779 | NSRect m_formerFrame; |
| 780 | } FullScreenData ; |
| 781 | |
| 782 | bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const |
| 783 | { |
| 784 | return m_macFullScreenData != NULL ; |
| 785 | } |
| 786 | |
| 787 | bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style)) |
| 788 | { |
| 789 | if ( show ) |
| 790 | { |
| 791 | FullScreenData *data = (FullScreenData *)m_macFullScreenData ; |
| 792 | delete data ; |
| 793 | data = new FullScreenData(); |
| 794 | |
| 795 | m_macFullScreenData = data ; |
| 796 | data->m_formerLevel = [m_macWindow level]; |
| 797 | data->m_formerFrame = [m_macWindow frame]; |
| 798 | CGDisplayCapture( kCGDirectMainDisplay ); |
| 799 | [m_macWindow setLevel:CGShieldingWindowLevel()]; |
| 800 | NSRect screenframe = [[NSScreen mainScreen] frame]; |
| 801 | NSRect frame = NSMakeRect (0, 0, 100, 100); |
| 802 | NSRect contentRect; |
| 803 | contentRect = [NSWindow contentRectForFrameRect: frame |
| 804 | styleMask: NSTitledWindowMask]; |
| 805 | screenframe.origin.y += (frame.origin.y - contentRect.origin.y); |
| 806 | screenframe.size.height += (frame.size.height - contentRect.size.height); |
| 807 | [m_macWindow setFrame:screenframe display:YES]; |
| 808 | } |
| 809 | else if ( m_macFullScreenData != NULL ) |
| 810 | { |
| 811 | FullScreenData *data = (FullScreenData *) m_macFullScreenData ; |
| 812 | CGDisplayRelease( kCGDirectMainDisplay ); |
| 813 | [m_macWindow setLevel:data->m_formerLevel]; |
| 814 | [m_macWindow setFrame:data->m_formerFrame display:YES]; |
| 815 | delete data ; |
| 816 | m_macFullScreenData = NULL ; |
| 817 | } |
| 818 | |
| 819 | return true; |
| 820 | } |
| 821 | |
| 822 | void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX) |
| 823 | { |
| 824 | NSRequestUserAttentionType flagsOSX; |
| 825 | switch ( flagsWX ) |
| 826 | { |
| 827 | case wxUSER_ATTENTION_INFO: |
| 828 | flagsOSX = NSInformationalRequest; |
| 829 | break; |
| 830 | |
| 831 | case wxUSER_ATTENTION_ERROR: |
| 832 | flagsOSX = NSCriticalRequest; |
| 833 | break; |
| 834 | |
| 835 | default: |
| 836 | wxFAIL_MSG( "invalid RequestUserAttention() flags" ); |
| 837 | return; |
| 838 | } |
| 839 | |
| 840 | [NSApp requestUserAttention:flagsOSX]; |
| 841 | } |
| 842 | |
| 843 | void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y ) |
| 844 | { |
| 845 | wxPoint p((x ? *x : 0), (y ? *y : 0) ); |
| 846 | NSPoint nspt = wxToNSPoint( NULL, p ); |
| 847 | nspt = [m_macWindow convertScreenToBase:nspt]; |
| 848 | nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil]; |
| 849 | p = wxFromNSPoint([m_macWindow contentView], nspt); |
| 850 | if ( x ) |
| 851 | *x = p.x; |
| 852 | if ( y ) |
| 853 | *y = p.y; |
| 854 | } |
| 855 | |
| 856 | void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y ) |
| 857 | { |
| 858 | wxPoint p((x ? *x : 0), (y ? *y : 0) ); |
| 859 | NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p ); |
| 860 | nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil]; |
| 861 | nspt = [m_macWindow convertBaseToScreen:nspt]; |
| 862 | p = wxFromNSPoint( NULL, nspt); |
| 863 | if ( x ) |
| 864 | *x = p.x; |
| 865 | if ( y ) |
| 866 | *y = p.y; |
| 867 | } |
| 868 | |
| 869 | bool wxNonOwnedWindowCocoaImpl::IsActive() |
| 870 | { |
| 871 | return [m_macWindow isKeyWindow]; |
| 872 | } |
| 873 | |
| 874 | void wxNonOwnedWindowCocoaImpl::SetModified(bool modified) |
| 875 | { |
| 876 | [m_macWindow setDocumentEdited:modified]; |
| 877 | } |
| 878 | |
| 879 | bool wxNonOwnedWindowCocoaImpl::IsModified() const |
| 880 | { |
| 881 | return [m_macWindow isDocumentEdited]; |
| 882 | } |
| 883 | |
| 884 | wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow) |
| 885 | { |
| 886 | wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer ); |
| 887 | now->Create( parent, nativeWindow ); |
| 888 | return now; |
| 889 | } |
| 890 | |
| 891 | wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size, |
| 892 | long style, long extraStyle, const wxString& name ) |
| 893 | { |
| 894 | wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer ); |
| 895 | now->Create( parent, pos, size, style , extraStyle, name ); |
| 896 | return now; |
| 897 | } |