]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/nonownedwnd.cpp
new file added
[wxWidgets.git] / src / osx / carbon / nonownedwnd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/nonownedwnd.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Stefan Csomor
5 // Created: 2008-03-24
6 // RCS-ID: $Id$
7 // Copyright: (c) Stefan Csomor 2008
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/app.h"
16 #endif // WX_PRECOMP
17
18 #include "wx/hashmap.h"
19 #include "wx/evtloop.h"
20 #include "wx/tooltip.h"
21 #include "wx/nonownedwnd.h"
22
23 #include "wx/osx/private.h"
24 #include "wx/settings.h"
25 #include "wx/frame.h"
26
27 #if wxUSE_SYSTEM_OPTIONS
28 #include "wx/sysopt.h"
29 #endif
30
31 // ============================================================================
32 // wxNonOwnedWindow implementation
33 // ============================================================================
34
35 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
36 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
37
38 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCarbonImpl , wxNonOwnedWindowImpl )
39
40 WXWindow wxNonOwnedWindowCarbonImpl::GetWXWindow() const
41 {
42 return (WXWindow) m_macWindow;
43 }
44 void wxNonOwnedWindowCarbonImpl::Raise()
45 {
46 ::SelectWindow( m_macWindow ) ;
47 }
48
49 void wxNonOwnedWindowCarbonImpl::Lower()
50 {
51 ::SendBehind( m_macWindow , NULL ) ;
52 }
53
54 void wxNonOwnedWindowCarbonImpl::ShowWithoutActivating()
55 {
56 bool plainTransition = true;
57
58 #if wxUSE_SYSTEM_OPTIONS
59 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
60 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
61 #endif
62
63 if ( plainTransition )
64 ::ShowWindow( (WindowRef)m_macWindow );
65 else
66 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
67 }
68
69 bool wxNonOwnedWindowCarbonImpl::Show(bool show)
70 {
71 bool plainTransition = true;
72
73 #if wxUSE_SYSTEM_OPTIONS
74 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
75 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
76 #endif
77
78 if (show)
79 {
80 ShowWithoutActivating();
81 ::SelectWindow( (WindowRef)m_macWindow ) ;
82 }
83 else
84 {
85 #if wxOSX_USE_CARBON
86 if ( plainTransition )
87 ::HideWindow( (WindowRef)m_macWindow );
88 else
89 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
90 #endif
91 }
92 return true;
93 }
94
95 void wxNonOwnedWindowCarbonImpl::Update()
96 {
97 HIWindowFlush(m_macWindow) ;
98 }
99
100 bool wxNonOwnedWindowCarbonImpl::SetTransparent(wxByte alpha)
101 {
102 OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, (CGFloat)((alpha)/255.0));
103 return result == noErr;
104 }
105
106 bool wxNonOwnedWindowCarbonImpl::SetBackgroundColour(const wxColour& col )
107 {
108 if ( col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDocumentWindowBackground)) )
109 {
110 SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDocumentWindowBackground, false ) ;
111 m_wxPeer->SetBackgroundStyle(wxBG_STYLE_SYSTEM);
112 // call directly if object is not yet completely constructed
113 if ( m_wxPeer->GetNonOwnedPeer() == NULL )
114 SetBackgroundStyle(wxBG_STYLE_SYSTEM);
115 }
116 else if ( col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive)) )
117 {
118 SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDialogBackgroundActive, false ) ;
119 m_wxPeer->SetBackgroundStyle(wxBG_STYLE_SYSTEM);
120 // call directly if object is not yet completely constructed
121 if ( m_wxPeer->GetNonOwnedPeer() == NULL )
122 SetBackgroundStyle(wxBG_STYLE_SYSTEM);
123 }
124 return true;
125 }
126
127 void wxNonOwnedWindowCarbonImpl::SetExtraStyle( long exStyle )
128 {
129 if ( m_macWindow != NULL )
130 {
131 bool metal = exStyle & wxFRAME_EX_METAL ;
132
133 if ( MacGetMetalAppearance() != metal )
134 {
135 if ( MacGetUnifiedAppearance() )
136 MacSetUnifiedAppearance( !metal ) ;
137
138 MacSetMetalAppearance( metal ) ;
139 }
140 }
141 }
142
143 bool wxNonOwnedWindowCarbonImpl::SetBackgroundStyle(wxBackgroundStyle style)
144 {
145 if ( style == wxBG_STYLE_TRANSPARENT )
146 {
147 OSStatus err = HIWindowChangeFeatures( m_macWindow, 0, kWindowIsOpaque );
148 verify_noerr( err );
149 err = ReshapeCustomWindow( m_macWindow );
150 verify_noerr( err );
151 }
152
153 return true ;
154 }
155
156 bool wxNonOwnedWindowCarbonImpl::CanSetTransparent()
157 {
158 return true;
159 }
160
161 void wxNonOwnedWindowCarbonImpl::GetContentArea( int &left , int &top , int &width , int &height ) const
162 {
163 Rect content, structure ;
164
165 GetWindowBounds( m_macWindow, kWindowStructureRgn , &structure ) ;
166 GetWindowBounds( m_macWindow, kWindowContentRgn , &content ) ;
167
168 left = content.left - structure.left ;
169 top = content.top - structure.top ;
170 width = content.right - content.left ;
171 height = content.bottom - content.top ;
172 }
173
174 void wxNonOwnedWindowCarbonImpl::MoveWindow(int x, int y, int width, int height)
175 {
176 Rect bounds = { y , x , y + height , x + width } ;
177 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
178 }
179
180 void wxNonOwnedWindowCarbonImpl::GetPosition( int &x, int &y ) const
181 {
182 Rect bounds ;
183
184 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
185
186 x = bounds.left ;
187 y = bounds.top ;
188 }
189
190 void wxNonOwnedWindowCarbonImpl::GetSize( int &width, int &height ) const
191 {
192 Rect bounds ;
193
194 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
195
196 width = bounds.right - bounds.left ;
197 height = bounds.bottom - bounds.top ;
198 }
199
200 bool wxNonOwnedWindowCarbonImpl::MacGetUnifiedAppearance() const
201 {
202 return MacGetWindowAttributes() & kWindowUnifiedTitleAndToolbarAttribute ;
203 }
204
205 void wxNonOwnedWindowCarbonImpl::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
206 {
207 ChangeWindowAttributes( m_macWindow, attributesToSet, attributesToClear ) ;
208 }
209
210 wxUint32 wxNonOwnedWindowCarbonImpl::MacGetWindowAttributes() const
211 {
212 UInt32 attr = 0 ;
213 GetWindowAttributes( m_macWindow, &attr ) ;
214 return attr ;
215 }
216
217 void wxNonOwnedWindowCarbonImpl::MacSetMetalAppearance( bool set )
218 {
219 if ( MacGetUnifiedAppearance() )
220 MacSetUnifiedAppearance( false ) ;
221
222 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
223 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
224 }
225
226 bool wxNonOwnedWindowCarbonImpl::MacGetMetalAppearance() const
227 {
228 return MacGetWindowAttributes() & kWindowMetalAttribute ;
229 }
230
231 void wxNonOwnedWindowCarbonImpl::MacSetUnifiedAppearance( bool set )
232 {
233 if ( MacGetMetalAppearance() )
234 MacSetMetalAppearance( false ) ;
235
236 MacChangeWindowAttributes( set ? kWindowUnifiedTitleAndToolbarAttribute : kWindowNoAttributes ,
237 set ? kWindowNoAttributes : kWindowUnifiedTitleAndToolbarAttribute) ;
238
239 // For some reason, Tiger uses white as the background color for this appearance,
240 // while most apps using it use the typical striped background. Restore that behaviour
241 // for wx.
242 // TODO: Determine if we need this on Leopard as well. (should be harmless either way,
243 // though)
244 // since when creating the peering is not yet completely set-up we call both setters
245 // explicitly
246 m_wxPeer->SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ) ;
247 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ) ;
248 }
249
250
251 // ----------------------------------------------------------------------------
252 // globals
253 // ----------------------------------------------------------------------------
254
255 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
256
257 WXDLLEXPORT void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
258
259 // ---------------------------------------------------------------------------
260 // Carbon Events
261 // ---------------------------------------------------------------------------
262
263 static const EventTypeSpec eventList[] =
264 {
265 // TODO: remove control related event like key and mouse (except for WindowLeave events)
266
267 { kEventClassKeyboard, kEventRawKeyDown } ,
268 { kEventClassKeyboard, kEventRawKeyRepeat } ,
269 { kEventClassKeyboard, kEventRawKeyUp } ,
270 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
271
272 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
273 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
274
275 { kEventClassWindow , kEventWindowShown } ,
276 { kEventClassWindow , kEventWindowActivated } ,
277 { kEventClassWindow , kEventWindowDeactivated } ,
278 { kEventClassWindow , kEventWindowBoundsChanging } ,
279 { kEventClassWindow , kEventWindowBoundsChanged } ,
280 { kEventClassWindow , kEventWindowClose } ,
281 { kEventClassWindow , kEventWindowGetRegion } ,
282
283 // we have to catch these events on the toplevel window level,
284 // as controls don't get the raw mouse events anymore
285
286 { kEventClassMouse , kEventMouseDown } ,
287 { kEventClassMouse , kEventMouseUp } ,
288 { kEventClassMouse , kEventMouseWheelMoved } ,
289 { kEventClassMouse , kEventMouseMoved } ,
290 { kEventClassMouse , kEventMouseDragged } ,
291 } ;
292
293 static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
294 {
295 OSStatus result = eventNotHandledErr ;
296 // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
297 // FindFocus does not return the actual focus window, but the enclosing window
298 wxWindow* focus = wxWindow::DoFindFocus();
299 if ( focus == NULL )
300 focus = data ? ((wxNonOwnedWindowImpl*) data)->GetWXPeer() : NULL ;
301
302 unsigned char charCode ;
303 wxChar uniChar[2] ;
304 uniChar[0] = 0;
305 uniChar[1] = 0;
306
307 UInt32 keyCode ;
308 UInt32 modifiers ;
309 Point point ;
310 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
311
312 #if wxUSE_UNICODE
313 ByteCount dataSize = 0 ;
314 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
315 {
316 UniChar buf[2] ;
317 int numChars = dataSize / sizeof( UniChar) + 1;
318
319 UniChar* charBuf = buf ;
320
321 if ( numChars * 2 > 4 )
322 charBuf = new UniChar[ numChars ] ;
323 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
324 charBuf[ numChars - 1 ] = 0;
325
326 wxMBConvUTF16 converter ;
327 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
328
329 if ( numChars * 2 > 4 )
330 delete[] charBuf ;
331 }
332 #endif // wxUSE_UNICODE
333
334 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
335 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
336 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
337 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
338
339 UInt32 message = (keyCode << 8) + charCode;
340 switch ( GetEventKind( event ) )
341 {
342 case kEventRawKeyRepeat :
343 case kEventRawKeyDown :
344 {
345 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
346 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
347 wxTheApp->MacSetCurrentEvent( event , handler ) ;
348 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
349 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
350 {
351 result = noErr ;
352 }
353 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
354 }
355 break ;
356
357 case kEventRawKeyUp :
358 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
359 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
360 {
361 result = noErr ;
362 }
363 break ;
364
365 case kEventRawKeyModifiersChanged :
366 {
367 wxKeyEvent event(wxEVT_KEY_DOWN);
368
369 event.m_shiftDown = modifiers & shiftKey;
370 event.m_rawControlDown = modifiers & controlKey;
371 event.m_altDown = modifiers & optionKey;
372 event.m_controlDown = event.m_metaDown = modifiers & cmdKey;
373 event.m_x = point.h;
374 event.m_y = point.v;
375
376 #if wxUSE_UNICODE
377 event.m_uniChar = uniChar[0] ;
378 #endif
379
380 event.SetTimestamp(when);
381 event.SetEventObject(focus);
382
383 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
384 {
385 event.m_keyCode = WXK_RAW_CONTROL ;
386 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
387 focus->HandleWindowEvent( event ) ;
388 }
389 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
390 {
391 event.m_keyCode = WXK_SHIFT ;
392 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
393 focus->HandleWindowEvent( event ) ;
394 }
395 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
396 {
397 event.m_keyCode = WXK_ALT ;
398 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
399 focus->HandleWindowEvent( event ) ;
400 }
401 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
402 {
403 event.m_keyCode = WXK_CONTROL;
404 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
405 focus->HandleWindowEvent( event ) ;
406 }
407
408 wxApp::s_lastModifiers = modifiers ;
409 }
410 break ;
411
412 default:
413 break;
414 }
415
416 return result ;
417 }
418
419 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
420 // for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the
421 // mouse down at all
422 //
423 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
424
425 EventMouseButton g_lastButton = 0 ;
426 bool g_lastButtonWasFakeRight = false ;
427
428 WXDLLEXPORT void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
429 {
430 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
431 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
432
433 // these parameters are not given for all events
434 EventMouseButton button = 0 ;
435 UInt32 clickCount = 0 ;
436 UInt32 mouseChord = 0;
437
438 cEvent.GetParameter<EventMouseButton>( kEventParamMouseButton, typeMouseButton , &button ) ;
439 cEvent.GetParameter<UInt32>( kEventParamClickCount, typeUInt32 , &clickCount ) ;
440 // the chord is the state of the buttons pressed currently
441 cEvent.GetParameter<UInt32>( kEventParamMouseChord, typeUInt32 , &mouseChord ) ;
442
443 wxevent.m_x = screenMouseLocation.h;
444 wxevent.m_y = screenMouseLocation.v;
445 wxevent.m_shiftDown = modifiers & shiftKey;
446 wxevent.m_rawControlDown = modifiers & controlKey;
447 wxevent.m_altDown = modifiers & optionKey;
448 wxevent.m_controlDown = wxevent.m_metaDown = modifiers & cmdKey;
449 wxevent.m_clickCount = clickCount;
450 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
451
452 // a control click is interpreted as a right click
453 bool thisButtonIsFakeRight = false ;
454 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
455 {
456 button = kEventMouseButtonSecondary ;
457 thisButtonIsFakeRight = true ;
458 }
459
460 // otherwise we report double clicks by connecting a left click with a ctrl-left click
461 if ( clickCount > 1 && button != g_lastButton )
462 clickCount = 1 ;
463
464 // we must make sure that our synthetic 'right' button corresponds in
465 // mouse down, moved and mouse up, and does not deliver a right down and left up
466
467 if ( cEvent.GetKind() == kEventMouseDown )
468 {
469 g_lastButton = button ;
470 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
471 }
472
473 if ( button == 0 )
474 {
475 g_lastButton = 0 ;
476 g_lastButtonWasFakeRight = false ;
477 }
478 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
479 button = g_lastButton ;
480
481 // Adjust the chord mask to remove the primary button and add the
482 // secondary button. It is possible that the secondary button is
483 // already pressed, e.g. on a mouse connected to a laptop, but this
484 // possibility is ignored here:
485 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
486 mouseChord = ((mouseChord & ~1U) | 2U);
487
488 if(mouseChord & 1U)
489 wxevent.m_leftDown = true ;
490 if(mouseChord & 2U)
491 wxevent.m_rightDown = true ;
492 if(mouseChord & 4U)
493 wxevent.m_middleDown = true ;
494
495 // translate into wx types
496 switch ( cEvent.GetKind() )
497 {
498 case kEventMouseDown :
499 switch ( button )
500 {
501 case kEventMouseButtonPrimary :
502 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
503 break ;
504
505 case kEventMouseButtonSecondary :
506 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
507 break ;
508
509 case kEventMouseButtonTertiary :
510 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
511 break ;
512
513 default:
514 break ;
515 }
516 break ;
517
518 case kEventMouseUp :
519 switch ( button )
520 {
521 case kEventMouseButtonPrimary :
522 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
523 break ;
524
525 case kEventMouseButtonSecondary :
526 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
527 break ;
528
529 case kEventMouseButtonTertiary :
530 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
531 break ;
532
533 default:
534 break ;
535 }
536 break ;
537 // TODO http://developer.apple.com/qa/qa2005/qa1453.html
538 // add declaration for 10.4 and change to kEventMouseScroll
539 case kEventMouseWheelMoved :
540 {
541 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
542
543 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
544 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
545
546 wxevent.m_wheelRotation = delta;
547 wxevent.m_wheelDelta = 1;
548 wxevent.m_linesPerAction = 1;
549 if ( axis == kEventMouseWheelAxisX )
550 wxevent.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL;
551 }
552 break ;
553
554 case kEventMouseEntered :
555 case kEventMouseExited :
556 case kEventMouseDragged :
557 case kEventMouseMoved :
558 wxevent.SetEventType( wxEVT_MOTION ) ;
559 break;
560 default :
561 break ;
562 }
563 }
564
565 #define NEW_CAPTURE_HANDLING 1
566
567 pascal OSStatus
568 wxMacTopLevelMouseEventHandler(EventHandlerCallRef WXUNUSED(handler),
569 EventRef event,
570 void *data)
571 {
572 wxNonOwnedWindow* toplevelWindow = data ? ((wxNonOwnedWindowImpl*) data)->GetWXPeer() : NULL ;
573
574 OSStatus result = eventNotHandledErr ;
575
576 wxMacCarbonEvent cEvent( event ) ;
577
578 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
579 Point windowMouseLocation = screenMouseLocation ;
580
581 WindowRef window = NULL;
582 short windowPart = ::FindWindow(screenMouseLocation, &window);
583
584 wxWindow* currentMouseWindow = NULL ;
585 ControlRef control = NULL ;
586
587 #if NEW_CAPTURE_HANDLING
588 if ( wxApp::s_captureWindow )
589 {
590 window = (WindowRef) wxApp::s_captureWindow->MacGetTopLevelWindowRef() ;
591 windowPart = inContent ;
592 }
593 #endif
594
595 if ( window )
596 {
597 QDGlobalToLocalPoint( GetWindowPort( window ), &windowMouseLocation );
598
599 if ( wxApp::s_captureWindow
600 #if !NEW_CAPTURE_HANDLING
601 && wxApp::s_captureWindow->MacGetTopLevelWindowRef() == (WXWindow) window && windowPart == inContent
602 #endif
603 )
604 {
605 currentMouseWindow = wxApp::s_captureWindow ;
606 }
607 else if ( (IsWindowActive(window) && windowPart == inContent) )
608 {
609 ControlPartCode part ;
610 control = FindControlUnderMouse( windowMouseLocation , window , &part ) ;
611 // if there is no control below the mouse position, send the event to the toplevel window itself
612 if ( control == 0 )
613 {
614 currentMouseWindow = (wxWindow*) toplevelWindow ;
615 }
616 else
617 {
618 currentMouseWindow = (wxWindow*) wxFindWindowFromWXWidget( (WXWidget) control ) ;
619 #ifndef __WXUNIVERSAL__
620 if ( currentMouseWindow == NULL && cEvent.GetKind() == kEventMouseMoved )
621 {
622 #if wxUSE_TOOLBAR
623 // for wxToolBar to function we have to send certaint events to it
624 // instead of its children (wxToolBarTools)
625 ControlRef parent ;
626 GetSuperControl(control, &parent );
627 wxWindow *wxparent = (wxWindow*) wxFindWindowFromWXWidget((WXWidget) parent ) ;
628 if ( wxparent && wxparent->IsKindOf( CLASSINFO( wxToolBar ) ) )
629 currentMouseWindow = wxparent ;
630 #endif
631 }
632 #endif
633 }
634
635 // disabled windows must not get any input messages
636 if ( currentMouseWindow && !currentMouseWindow->MacIsReallyEnabled() )
637 currentMouseWindow = NULL;
638 }
639 }
640
641 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
642 SetupMouseEvent( wxevent , cEvent ) ;
643
644 // handle all enter / leave events
645
646 if ( currentMouseWindow != g_MacLastWindow )
647 {
648 if ( g_MacLastWindow )
649 {
650 wxMouseEvent eventleave(wxevent);
651 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
652 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
653 eventleave.SetEventObject( g_MacLastWindow ) ;
654 wxevent.SetId( g_MacLastWindow->GetId() ) ;
655
656 #if wxUSE_TOOLTIPS
657 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
658 #endif
659
660 g_MacLastWindow->HandleWindowEvent(eventleave);
661 }
662
663 if ( currentMouseWindow )
664 {
665 wxMouseEvent evententer(wxevent);
666 evententer.SetEventType( wxEVT_ENTER_WINDOW );
667 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
668 evententer.SetEventObject( currentMouseWindow ) ;
669 wxevent.SetId( currentMouseWindow->GetId() ) ;
670
671 #if wxUSE_TOOLTIPS
672 wxToolTip::RelayEvent( currentMouseWindow , evententer );
673 #endif
674
675 currentMouseWindow->HandleWindowEvent(evententer);
676 }
677
678 g_MacLastWindow = currentMouseWindow ;
679 }
680
681 if ( windowPart == inMenuBar )
682 {
683 // special case menu bar, as we are having a low-level runloop we must do it ourselves
684 if ( cEvent.GetKind() == kEventMouseDown )
685 {
686 ::MenuSelect( screenMouseLocation ) ;
687 ::HiliteMenu(0);
688 result = noErr ;
689 }
690 }
691 else if ( window && windowPart == inProxyIcon )
692 {
693 // special case proxy icon bar, as we are having a low-level runloop we must do it ourselves
694 if ( cEvent.GetKind() == kEventMouseDown )
695 {
696 if ( ::TrackWindowProxyDrag( window, screenMouseLocation ) != errUserWantsToDragWindow )
697 {
698 // TODO Track change of file path and report back
699 result = noErr ;
700 }
701 }
702 }
703 else if ( currentMouseWindow )
704 {
705 wxWindow *currentMouseWindowParent = currentMouseWindow->GetParent();
706
707 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
708
709 wxevent.SetEventObject( currentMouseWindow ) ;
710 wxevent.SetId( currentMouseWindow->GetId() ) ;
711
712 // make tooltips current
713
714 #if wxUSE_TOOLTIPS
715 if ( wxevent.GetEventType() == wxEVT_MOTION )
716 wxToolTip::RelayEvent( currentMouseWindow , wxevent );
717 #endif
718
719 if ( currentMouseWindow->HandleWindowEvent(wxevent) )
720 {
721 if ( currentMouseWindowParent &&
722 !currentMouseWindowParent->GetChildren().Member(currentMouseWindow) )
723 currentMouseWindow = NULL;
724
725 result = noErr;
726 }
727 else
728 {
729 // if the user code did _not_ handle the event, then perform the
730 // default processing
731 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
732 {
733 // ... that is set focus to this window
734 if (currentMouseWindow->CanAcceptFocus() && wxWindow::FindFocus()!=currentMouseWindow)
735 currentMouseWindow->SetFocus();
736 }
737 }
738
739 if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
740 {
741 wxApp::s_captureWindow = NULL ;
742 // update cursor ?
743 }
744
745 // update cursor
746
747 wxWindow* cursorTarget = currentMouseWindow ;
748 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
749
750 extern wxCursor gGlobalCursor;
751
752 if (!gGlobalCursor.IsOk())
753 {
754 while ( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
755 {
756 cursorTarget = cursorTarget->GetParent() ;
757 if ( cursorTarget )
758 cursorPoint += cursorTarget->GetPosition();
759 }
760 }
761
762 }
763 else // currentMouseWindow == NULL
764 {
765 if (toplevelWindow && !control)
766 {
767 extern wxCursor gGlobalCursor;
768 if (!gGlobalCursor.IsOk())
769 {
770 // update cursor when over toolbar and titlebar etc.
771 wxSTANDARD_CURSOR->MacInstall() ;
772 }
773 }
774
775 // don't mess with controls we don't know about
776 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
777 // so we try sending them the correct control directly
778 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
779 {
780 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
781 Point clickLocation = windowMouseLocation ;
782
783 HIPoint hiPoint ;
784 hiPoint.x = clickLocation.h ;
785 hiPoint.y = clickLocation.v ;
786 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
787 clickLocation.h = (int)hiPoint.x ;
788 clickLocation.v = (int)hiPoint.y ;
789
790 HandleControlClick( control , clickLocation , modifiers , (ControlActionUPP ) -1 ) ;
791 result = noErr ;
792 }
793 }
794
795 return result ;
796 }
797
798 static pascal OSStatus
799 wxNonOwnedWindowEventHandler(EventHandlerCallRef WXUNUSED(handler),
800 EventRef event,
801 void *data)
802 {
803 OSStatus result = eventNotHandledErr ;
804
805 wxMacCarbonEvent cEvent( event ) ;
806
807 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
808 wxNonOwnedWindow* toplevelWindow = data ? ((wxNonOwnedWindowImpl*) data)->GetWXPeer() : NULL;
809
810 switch ( GetEventKind( event ) )
811 {
812 case kEventWindowActivated :
813 {
814 toplevelWindow->HandleActivated( cEvent.GetTicks() , true) ;
815 // we still sending an eventNotHandledErr in order to allow for default processing
816 }
817 break ;
818
819 case kEventWindowDeactivated :
820 {
821 toplevelWindow->HandleActivated( cEvent.GetTicks() , false) ;
822 // we still sending an eventNotHandledErr in order to allow for default processing
823 }
824 break ;
825
826 case kEventWindowShown :
827 toplevelWindow->Refresh() ;
828 result = noErr ;
829 break ;
830
831 case kEventWindowClose :
832 toplevelWindow->Close() ;
833 result = noErr ;
834 break ;
835
836 case kEventWindowBoundsChanged :
837 {
838 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes, typeUInt32) ;
839 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
840 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
841 if ( attributes & kWindowBoundsChangeSizeChanged )
842 {
843 toplevelWindow->HandleResized(cEvent.GetTicks() ) ;
844 }
845
846 if ( attributes & kWindowBoundsChangeOriginChanged )
847 {
848 toplevelWindow->HandleMoved(cEvent.GetTicks() ) ;
849 }
850
851 result = noErr ;
852 }
853 break ;
854
855 case kEventWindowBoundsChanging :
856 {
857 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
858 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
859
860 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
861 {
862 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
863 int left , top , width , height ;
864 // structure width
865 int swidth, sheight;
866
867 toplevelWindow->GetNonOwnedPeer()->GetContentArea(left, top, width, height);
868 toplevelWindow->GetNonOwnedPeer()->GetSize(swidth, sheight);
869 int deltawidth = swidth - width;
870 int deltaheight = sheight - height;
871 wxRect adjustR(
872 newRect.left - left,
873 newRect.top - top,
874 newRect.right - newRect.left + deltawidth,
875 newRect.bottom - newRect.top + deltaheight ) ;
876
877 toplevelWindow->HandleResizing( cEvent.GetTicks(), &adjustR );
878
879 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + top + adjustR.height - deltaheight ,
880 adjustR.x + left + adjustR.width - deltawidth } ;
881 if ( !EqualRect( &newRect , &adjustedRect ) )
882 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
883 }
884
885 result = noErr ;
886 }
887 break ;
888
889 case kEventWindowGetRegion :
890 {
891 if ( toplevelWindow->GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT )
892 {
893 WindowRegionCode windowRegionCode ;
894
895 // Fetch the region code that is being queried
896 GetEventParameter( event,
897 kEventParamWindowRegionCode,
898 typeWindowRegionCode, NULL,
899 sizeof windowRegionCode, NULL,
900 &windowRegionCode ) ;
901
902 // If it is the opaque region code then set the
903 // region to empty and return noErr to stop event
904 // propagation
905 if ( windowRegionCode == kWindowOpaqueRgn ) {
906 RgnHandle region;
907 GetEventParameter( event,
908 kEventParamRgnHandle,
909 typeQDRgnHandle, NULL,
910 sizeof region, NULL,
911 &region) ;
912 SetEmptyRgn(region) ;
913 result = noErr ;
914 }
915 }
916 }
917 break ;
918
919 default :
920 break ;
921 }
922
923 return result ;
924 }
925
926 // mix this in from window.cpp
927 pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
928
929 static pascal OSStatus wxNonOwnedEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
930 {
931 OSStatus result = eventNotHandledErr ;
932
933 switch ( GetEventClass( event ) )
934 {
935 case kEventClassTextInput :
936 {
937 // TODO remove as soon as all events are on implementation classes only
938 wxNonOwnedWindow* toplevelWindow = data ? ((wxNonOwnedWindowImpl*) data)->GetWXPeer() : NULL;
939
940 result = wxMacUnicodeTextEventHandler( handler, event , toplevelWindow ) ;
941 }
942 break ;
943
944 case kEventClassKeyboard :
945 result = KeyboardEventHandler( handler, event , data ) ;
946 break ;
947
948 case kEventClassWindow :
949 result = wxNonOwnedWindowEventHandler( handler, event , data ) ;
950 break ;
951
952 case kEventClassMouse :
953 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
954 break ;
955
956 default :
957 break ;
958 }
959
960 return result ;
961 }
962
963 DEFINE_ONE_SHOT_HANDLER_GETTER( wxNonOwnedEventHandler )
964
965 // ---------------------------------------------------------------------------
966 // Support functions for shaped windows, based on Apple's CustomWindow sample at
967 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
968 // ---------------------------------------------------------------------------
969
970 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
971 {
972 GetWindowPortBounds(window, inRect);
973 Point pt = { inRect->top ,inRect->left };
974 QDLocalToGlobalPoint( GetWindowPort( window ), &pt );
975 inRect->bottom += pt.v - inRect->top;
976 inRect->right += pt.h - inRect->left;
977 inRect->top = pt.v;
978 inRect->left = pt.h;
979 }
980
981 static SInt32 wxShapedMacWindowGetFeatures(WindowRef WXUNUSED(window), SInt32 param)
982 {
983 /*------------------------------------------------------
984 Define which options your custom window supports.
985 --------------------------------------------------------*/
986 //just enable everything for our demo
987 *(OptionBits*)param =
988 //kWindowCanGrow |
989 //kWindowCanZoom |
990 kWindowCanCollapse |
991 //kWindowCanGetWindowRegion |
992 //kWindowHasTitleBar |
993 //kWindowSupportsDragHilite |
994 kWindowCanDrawInCurrentPort |
995 //kWindowCanMeasureTitle |
996 kWindowWantsDisposeAtProcessDeath |
997 kWindowSupportsGetGrowImageRegion |
998 kWindowDefSupportsColorGrafPort;
999
1000 return 1;
1001 }
1002
1003 // The content region is left as a rectangle matching the window size, this is
1004 // so the origin in the paint event, and etc. still matches what the
1005 // programmer expects.
1006 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1007 {
1008 SetEmptyRgn(rgn);
1009 wxNonOwnedWindow* win = wxNonOwnedWindow::GetFromWXWindow((WXWindow)window);
1010 if (win)
1011 {
1012 Rect r ;
1013 wxShapedMacWindowGetPos( window, &r ) ;
1014 RectRgn( rgn , &r ) ;
1015 }
1016 }
1017
1018 // The structure region is set to the shape given to the SetShape method.
1019 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1020 {
1021 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1022
1023 SetEmptyRgn(rgn);
1024 if (cachedRegion)
1025 {
1026 Rect windowRect;
1027 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1028 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
1029 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1030 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1031 }
1032 }
1033
1034 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1035 {
1036 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1037
1038 if (rgnRec == NULL)
1039 return paramErr;
1040
1041 switch (rgnRec->regionCode)
1042 {
1043 case kWindowStructureRgn:
1044 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1045 break;
1046
1047 case kWindowContentRgn:
1048 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1049 break;
1050
1051 default:
1052 SetEmptyRgn(rgnRec->winRgn);
1053 break;
1054 }
1055
1056 return noErr;
1057 }
1058
1059 // Determine the region of the window which was hit
1060 //
1061 static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
1062 {
1063 Point hitPoint;
1064 static RgnHandle tempRgn = NULL;
1065
1066 if (tempRgn == NULL)
1067 tempRgn = NewRgn();
1068
1069 // get the point clicked
1070 SetPt( &hitPoint, LoWord(param), HiWord(param) );
1071
1072 // Mac OS 8.5 or later
1073 wxShapedMacWindowStructureRegion(window, tempRgn);
1074 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
1075 return wInContent;
1076
1077 // no significant area was hit
1078 return wNoHit;
1079 }
1080
1081 static pascal long wxShapedMacWindowDef(short WXUNUSED(varCode), WindowRef window, SInt16 message, SInt32 param)
1082 {
1083 switch (message)
1084 {
1085 case kWindowMsgHitTest:
1086 return wxShapedMacWindowHitTest(window, param);
1087
1088 case kWindowMsgGetFeatures:
1089 return wxShapedMacWindowGetFeatures(window, param);
1090
1091 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1092 case kWindowMsgGetRegion:
1093 return wxShapedMacWindowGetRegion(window, param);
1094
1095 default:
1096 break;
1097 }
1098
1099 return 0;
1100 }
1101
1102 // implementation
1103
1104 typedef struct
1105 {
1106 wxPoint m_position ;
1107 wxSize m_size ;
1108 bool m_wasResizable ;
1109 } FullScreenData ;
1110
1111 wxNonOwnedWindowCarbonImpl::wxNonOwnedWindowCarbonImpl()
1112 {
1113 }
1114
1115 wxNonOwnedWindowCarbonImpl::wxNonOwnedWindowCarbonImpl( wxNonOwnedWindow* nonownedwnd) : wxNonOwnedWindowImpl( nonownedwnd)
1116 {
1117 m_macEventHandler = NULL;
1118 m_macWindow = NULL;
1119 m_macFullScreenData = NULL ;
1120 }
1121
1122 wxNonOwnedWindowCarbonImpl::~wxNonOwnedWindowCarbonImpl()
1123 {
1124 #if wxUSE_TOOLTIPS
1125 wxToolTip::NotifyWindowDelete(m_macWindow) ;
1126 #endif
1127
1128 if ( m_macEventHandler )
1129 {
1130 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
1131 m_macEventHandler = NULL ;
1132 }
1133
1134 if ( m_macWindow && !m_wxPeer->IsNativeWindowWrapper())
1135 DisposeWindow( m_macWindow );
1136
1137 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1138 delete data ;
1139 m_macFullScreenData = NULL ;
1140
1141 m_macWindow = NULL;
1142
1143 }
1144
1145 void wxNonOwnedWindowCarbonImpl::WillBeDestroyed()
1146 {
1147 if ( m_macEventHandler )
1148 {
1149 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
1150 m_macEventHandler = NULL ;
1151 }
1152 }
1153
1154 static void wxNonOwnedWindowInstallTopLevelWindowEventHandler(WindowRef window, EventHandlerRef* handler, void *ref)
1155 {
1156 InstallWindowEventHandler(window, GetwxNonOwnedEventHandlerUPP(),
1157 GetEventTypeCount(eventList), eventList, ref, handler );
1158 }
1159
1160 bool wxNonOwnedWindowCarbonImpl::SetShape(const wxRegion& region)
1161 {
1162 // Make a copy of the region
1163 RgnHandle shapeRegion = NewRgn();
1164 HIShapeGetAsQDRgn( region.GetWXHRGN(), shapeRegion );
1165
1166 // Dispose of any shape region we may already have
1167 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef) m_wxPeer->GetWXWindow() );
1168 if ( oldRgn )
1169 DisposeRgn(oldRgn);
1170
1171 // Save the region so we can use it later
1172 SetWRefCon((WindowRef) m_wxPeer->GetWXWindow(), (URefCon)shapeRegion);
1173
1174 // inform the window manager that the window has changed shape
1175 ReshapeCustomWindow((WindowRef) m_wxPeer->GetWXWindow());
1176
1177 return true;
1178 }
1179
1180
1181 void wxNonOwnedWindowCarbonImpl::MacInstallTopLevelWindowEventHandler()
1182 {
1183 if ( m_macEventHandler != NULL )
1184 {
1185 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1186 }
1187 wxNonOwnedWindowInstallTopLevelWindowEventHandler(MAC_WXHWND(m_macWindow),(EventHandlerRef *)&m_macEventHandler,this);
1188 }
1189
1190 void wxNonOwnedWindowCarbonImpl::Create(
1191 wxWindow* WXUNUSED(parent),
1192 WXWindow nativeWindow )
1193 {
1194 m_macWindow = nativeWindow;
1195 }
1196
1197 void wxNonOwnedWindowCarbonImpl::Create(
1198 wxWindow* parent,
1199 const wxPoint& pos,
1200 const wxSize& size,
1201 long style, long extraStyle,
1202 const wxString& WXUNUSED(name) )
1203 {
1204
1205 OSStatus err = noErr ;
1206 Rect theBoundsRect;
1207
1208 int x = (int)pos.x;
1209 int y = (int)pos.y;
1210
1211 int w = size.x;
1212 int h = size.y;
1213
1214 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1215
1216 // translate the window attributes in the appropriate window class and attributes
1217 WindowClass wclass = 0;
1218 WindowAttributes attr = kWindowNoAttributes ;
1219 WindowGroupRef group = NULL ;
1220 bool activationScopeSet = false;
1221 WindowActivationScope activationScope = kWindowActivationScopeNone;
1222
1223 if ( style & wxFRAME_TOOL_WINDOW )
1224 {
1225 if (
1226 ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
1227 ( style & wxSYSTEM_MENU ) || ( style & wxCAPTION ) ||
1228 ( style & wxTINY_CAPTION)
1229 )
1230 {
1231 if ( ( style & wxSTAY_ON_TOP ) )
1232 wclass = kUtilityWindowClass;
1233 else
1234 wclass = kFloatingWindowClass ;
1235
1236 if ( ( style & wxTINY_CAPTION) )
1237 attr |= kWindowSideTitlebarAttribute ;
1238 }
1239 else
1240 {
1241 if ( style & wxNO_BORDER )
1242 {
1243 wclass = kSimpleWindowClass ;
1244 }
1245 else
1246 {
1247 wclass = kPlainWindowClass ;
1248 }
1249 activationScopeSet = true;
1250 activationScope = kWindowActivationScopeNone;
1251 }
1252 }
1253 else if ( ( style & wxPOPUP_WINDOW ) )
1254 {
1255 if ( ( style & wxBORDER_NONE ) )
1256 {
1257 wclass = kHelpWindowClass ; // has no border
1258 attr |= kWindowNoShadowAttribute;
1259 }
1260 else
1261 {
1262 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
1263 }
1264 group = GetWindowGroupOfClass(kFloatingWindowClass) ;
1265 // make sure we don't deactivate something
1266 activationScopeSet = true;
1267 activationScope = kWindowActivationScopeNone;
1268 }
1269 else if ( ( style & wxCAPTION ) )
1270 {
1271 wclass = kDocumentWindowClass ;
1272 attr |= kWindowInWindowMenuAttribute ;
1273 }
1274 else if ( ( style & wxFRAME_DRAWER ) )
1275 {
1276 wclass = kDrawerWindowClass;
1277 }
1278 else
1279 {
1280 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
1281 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
1282 {
1283 wclass = kDocumentWindowClass ;
1284 }
1285 else if ( ( style & wxNO_BORDER ) )
1286 {
1287 wclass = kSimpleWindowClass ;
1288 }
1289 else
1290 {
1291 wclass = kPlainWindowClass ;
1292 }
1293 }
1294
1295 if ( wclass != kPlainWindowClass )
1296 {
1297 if ( ( style & wxMINIMIZE_BOX ) )
1298 attr |= kWindowCollapseBoxAttribute ;
1299
1300 if ( ( style & wxMAXIMIZE_BOX ) )
1301 attr |= kWindowFullZoomAttribute ;
1302
1303 if ( ( style & wxRESIZE_BORDER ) )
1304 attr |= kWindowResizableAttribute ;
1305
1306 if ( ( style & wxCLOSE_BOX) )
1307 attr |= kWindowCloseBoxAttribute ;
1308 }
1309 attr |= kWindowLiveResizeAttribute;
1310
1311 if ( ( style &wxSTAY_ON_TOP) )
1312 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1313
1314 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) )
1315 group = GetWindowGroupOfClass(kFloatingWindowClass) ;
1316
1317 if ( group == NULL && parent != NULL )
1318 {
1319 WindowRef parenttlw = (WindowRef) parent->MacGetTopLevelWindowRef();
1320 if( parenttlw )
1321 group = GetWindowGroupParent( GetWindowGroup( parenttlw ) );
1322 }
1323
1324 attr |= kWindowCompositingAttribute;
1325 #if 0 // TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1326 attr |= kWindowFrameworkScaledAttribute;
1327 #endif
1328
1329 if ( ( style &wxFRAME_SHAPED) )
1330 {
1331 WindowDefSpec customWindowDefSpec;
1332 customWindowDefSpec.defType = kWindowDefProcPtr;
1333 customWindowDefSpec.u.defProc =
1334 #ifdef __LP64__
1335 (WindowDefUPP) wxShapedMacWindowDef;
1336 #else
1337 NewWindowDefUPP(wxShapedMacWindowDef);
1338 #endif
1339 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1340 attr, &theBoundsRect,
1341 (WindowRef*) &m_macWindow);
1342 }
1343 else
1344 {
1345 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1346 }
1347
1348 if ( err == noErr && m_macWindow != NULL && group != NULL )
1349 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1350
1351 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1352
1353 // setup a separate group for each window, so that overlays can be handled easily
1354
1355 WindowGroupRef overlaygroup = NULL;
1356 verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrHideOnCollapse, &overlaygroup ));
1357 verify_noerr( SetWindowGroupParent( overlaygroup, GetWindowGroup( (WindowRef) m_macWindow )));
1358 verify_noerr( SetWindowGroup( (WindowRef) m_macWindow , overlaygroup ));
1359
1360 if ( activationScopeSet )
1361 {
1362 verify_noerr( SetWindowActivationScope( (WindowRef) m_macWindow , activationScope ));
1363 }
1364
1365 // the create commands are only for content rect,
1366 // so we have to set the size again as structure bounds
1367 SetWindowBounds( m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1368
1369 // Causes the inner part of the window not to be metal
1370 // if the style is used before window creation.
1371 #if 0 // TARGET_API_MAC_OSX
1372 if ( m_macUsesCompositing && m_macWindow != NULL )
1373 {
1374 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1375 MacSetMetalAppearance( true ) ;
1376 }
1377 #endif
1378
1379 if ( m_macWindow != NULL )
1380 {
1381 MacSetUnifiedAppearance( true ) ;
1382 }
1383
1384 HIViewRef growBoxRef = 0 ;
1385 err = HIViewFindByID( HIViewGetRoot( m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
1386 if ( err == noErr && growBoxRef != 0 )
1387 HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
1388
1389 // the frame window event handler
1390 InstallStandardEventHandler( GetWindowEventTarget(m_macWindow) ) ;
1391 MacInstallTopLevelWindowEventHandler() ;
1392
1393 if ( extraStyle & wxFRAME_EX_METAL)
1394 MacSetMetalAppearance(true);
1395
1396 if ( ( style &wxFRAME_SHAPED) )
1397 {
1398 // default shape matches the window size
1399 wxRegion rgn( 0, 0, w, h );
1400 SetShape( rgn );
1401 }
1402 }
1403
1404 bool wxNonOwnedWindowCarbonImpl::ShowWithEffect(bool show,
1405 wxShowEffect effect,
1406 unsigned timeout)
1407 {
1408 WindowTransitionEffect transition = 0 ;
1409 switch( effect )
1410 {
1411 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1412 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1413 case wxSHOW_EFFECT_ROLL_TO_TOP:
1414 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1415 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1416 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1417 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1418 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1419 transition = kWindowGenieTransitionEffect;
1420 break;
1421 case wxSHOW_EFFECT_BLEND:
1422 transition = kWindowFadeTransitionEffect;
1423 break;
1424 case wxSHOW_EFFECT_EXPAND:
1425 // having sheets would be fine, but this might lead to a repositioning
1426 #if 0
1427 if ( GetParent() )
1428 transition = kWindowSheetTransitionEffect;
1429 else
1430 #endif
1431 transition = kWindowZoomTransitionEffect;
1432 break;
1433
1434 case wxSHOW_EFFECT_NONE:
1435 // wxNonOwnedWindow is supposed to call Show() itself in this case
1436 wxFAIL_MSG( "ShowWithEffect() shouldn't be called" );
1437 return false;
1438
1439 case wxSHOW_EFFECT_MAX:
1440 wxFAIL_MSG( "invalid effect flag" );
1441 return false;
1442 }
1443
1444 TransitionWindowOptions options;
1445 options.version = 0;
1446 options.duration = timeout / 1000.0;
1447 options.window = transition == kWindowSheetTransitionEffect ? (WindowRef) m_wxPeer->GetParent()->MacGetTopLevelWindowRef() :0;
1448 options.userData = 0;
1449
1450 wxSize size = wxGetDisplaySize();
1451 Rect bounds;
1452 GetWindowBounds( (WindowRef)m_macWindow, kWindowStructureRgn, &bounds );
1453 CGRect hiBounds = CGRectMake( bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top );
1454
1455 switch ( effect )
1456 {
1457 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1458 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1459 hiBounds.origin.x = 0;
1460 hiBounds.size.width = 0;
1461 break;
1462
1463 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1464 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1465 hiBounds.origin.x = size.x;
1466 hiBounds.size.width = 0;
1467 break;
1468
1469 case wxSHOW_EFFECT_ROLL_TO_TOP:
1470 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1471 hiBounds.origin.y = size.y;
1472 hiBounds.size.height = 0;
1473 break;
1474
1475 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1476 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1477 hiBounds.origin.y = 0;
1478 hiBounds.size.height = 0;
1479 break;
1480
1481 default:
1482 break; // direction doesn't make sense
1483 }
1484
1485 ::TransitionWindowWithOptions
1486 (
1487 (WindowRef)m_macWindow,
1488 transition,
1489 show ? kWindowShowTransitionAction : kWindowHideTransitionAction,
1490 transition == kWindowGenieTransitionEffect ? &hiBounds : NULL,
1491 false,
1492 &options
1493 );
1494
1495 if ( show )
1496 {
1497 ::SelectWindow( (WindowRef)m_macWindow ) ;
1498 }
1499
1500 return true;
1501 }
1502
1503 void wxNonOwnedWindowCarbonImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
1504 {
1505 SetWindowTitleWithCFString( m_macWindow , wxCFStringRef( title , encoding ) ) ;
1506 }
1507
1508 bool wxNonOwnedWindowCarbonImpl::IsMaximized() const
1509 {
1510 return IsWindowInStandardState( m_macWindow , NULL , NULL ) ;
1511 }
1512
1513 bool wxNonOwnedWindowCarbonImpl::IsIconized() const
1514 {
1515 return IsWindowCollapsed((WindowRef)GetWXWindow() ) ;
1516 }
1517
1518 void wxNonOwnedWindowCarbonImpl::Iconize( bool iconize )
1519 {
1520 if ( IsWindowCollapsable( m_macWindow ) )
1521 CollapseWindow( m_macWindow , iconize ) ;
1522 }
1523
1524 void wxNonOwnedWindowCarbonImpl::Maximize(bool maximize)
1525 {
1526 Point idealSize = { 0 , 0 } ;
1527 if ( maximize )
1528 {
1529 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1530 HIRect bounds ;
1531 HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay,kHICoordSpace72DPIGlobal,
1532 &bounds);
1533 idealSize.h = bounds.size.width;
1534 idealSize.v = bounds.size.height;
1535 #else
1536 Rect rect ;
1537 GetAvailableWindowPositioningBounds(GetMainDevice(),&rect) ;
1538 idealSize.h = rect.right - rect.left ;
1539 idealSize.v = rect.bottom - rect.top ;
1540 #endif
1541 }
1542 ZoomWindowIdeal( (WindowRef)GetWXWindow() , maximize ? inZoomOut : inZoomIn , &idealSize ) ;
1543 }
1544
1545 bool wxNonOwnedWindowCarbonImpl::IsFullScreen() const
1546 {
1547 return m_macFullScreenData != NULL ;
1548 }
1549
1550 bool wxNonOwnedWindowCarbonImpl::ShowFullScreen(bool show, long style)
1551 {
1552 if ( show )
1553 {
1554 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1555 delete data ;
1556 data = new FullScreenData() ;
1557
1558 m_macFullScreenData = data ;
1559 data->m_position = m_wxPeer->GetPosition() ;
1560 data->m_size = m_wxPeer->GetSize() ;
1561 #if wxOSX_USE_CARBON
1562 WindowAttributes attr = 0;
1563 GetWindowAttributes((WindowRef) GetWXWindow(), &attr);
1564 data->m_wasResizable = attr & kWindowResizableAttribute;
1565 if ( style & wxFULLSCREEN_NOMENUBAR )
1566 HideMenuBar() ;
1567 #endif
1568
1569 wxRect client = wxGetClientDisplayRect() ;
1570
1571 int left, top, width, height ;
1572 int x, y, w, h ;
1573
1574 x = client.x ;
1575 y = client.y ;
1576 w = client.width ;
1577 h = client.height ;
1578
1579 GetContentArea( left, top, width, height ) ;
1580 int outerwidth, outerheight;
1581 GetSize( outerwidth, outerheight );
1582
1583 if ( style & wxFULLSCREEN_NOCAPTION )
1584 {
1585 y -= top ;
1586 h += top ;
1587 // avoid adding the caption twice to the height
1588 outerheight -= top;
1589 }
1590
1591 if ( style & wxFULLSCREEN_NOBORDER )
1592 {
1593 x -= left ;
1594 w += outerwidth - width;
1595 h += outerheight - height;
1596 }
1597
1598 if ( style & wxFULLSCREEN_NOTOOLBAR )
1599 {
1600 // TODO
1601 }
1602
1603 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1604 {
1605 // TODO
1606 }
1607
1608 m_wxPeer->SetSize( x , y , w, h ) ;
1609 if ( data->m_wasResizable )
1610 {
1611 #if wxOSX_USE_CARBON
1612 ChangeWindowAttributes( (WindowRef) GetWXWindow() , kWindowNoAttributes , kWindowResizableAttribute ) ;
1613 #endif
1614 }
1615 }
1616 else if ( m_macFullScreenData != NULL )
1617 {
1618 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1619 #if wxOSX_USE_CARBON
1620 ShowMenuBar() ;
1621 if ( data->m_wasResizable )
1622 ChangeWindowAttributes( (WindowRef) GetWXWindow() , kWindowResizableAttribute , kWindowNoAttributes ) ;
1623 #endif
1624 m_wxPeer->SetPosition( data->m_position ) ;
1625 m_wxPeer->SetSize( data->m_size ) ;
1626
1627 delete data ;
1628 m_macFullScreenData = NULL ;
1629 }
1630
1631 return true;
1632 }
1633
1634 // Attracts the users attention to this window if the application is
1635 // inactive (should be called when a background event occurs)
1636
1637 static pascal void wxMacNMResponse( NMRecPtr ptr )
1638 {
1639 NMRemove( ptr ) ;
1640 DisposePtr( (Ptr)ptr ) ;
1641 }
1642
1643 void wxNonOwnedWindowCarbonImpl::RequestUserAttention(int WXUNUSED(flags))
1644 {
1645 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
1646
1647 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1648 notificationRequest->qType = nmType ;
1649 notificationRequest->nmMark = 1 ;
1650 notificationRequest->nmIcon = 0 ;
1651 notificationRequest->nmSound = 0 ;
1652 notificationRequest->nmStr = NULL ;
1653 notificationRequest->nmResp = wxMacNMResponse ;
1654
1655 verify_noerr( NMInstall( notificationRequest ) ) ;
1656 }
1657
1658 void wxNonOwnedWindowCarbonImpl::ScreenToWindow( int *x, int *y )
1659 {
1660 HIPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
1661 HIViewRef contentView ;
1662 // TODO check toolbar offset
1663 HIViewFindByID( HIViewGetRoot( m_macWindow ), kHIViewWindowContentID , &contentView) ;
1664 HIPointConvert( &p, kHICoordSpace72DPIGlobal, NULL, kHICoordSpaceView, contentView );
1665 if ( x )
1666 *x = (int)p.x;
1667 if ( y )
1668 *y = (int)p.y;
1669 }
1670
1671 void wxNonOwnedWindowCarbonImpl::WindowToScreen( int *x, int *y )
1672 {
1673 HIPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
1674 HIViewRef contentView ;
1675 // TODO check toolbar offset
1676 HIViewFindByID( HIViewGetRoot( m_macWindow ), kHIViewWindowContentID , &contentView) ;
1677 HIPointConvert( &p, kHICoordSpaceView, contentView, kHICoordSpace72DPIGlobal, NULL );
1678 if ( x )
1679 *x = (int)p.x;
1680 if ( y )
1681 *y = (int)p.y;
1682 }
1683
1684 bool wxNonOwnedWindowCarbonImpl::IsActive()
1685 {
1686 return ActiveNonFloatingWindow() == m_macWindow;
1687 }
1688
1689 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
1690 long style, long extraStyle, const wxString& name )
1691 {
1692 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCarbonImpl( wxpeer );
1693 now->Create( parent, pos, size, style , extraStyle, name );
1694 return now;
1695 }
1696
1697 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow )
1698 {
1699 wxNonOwnedWindowCarbonImpl* now = new wxNonOwnedWindowCarbonImpl( wxpeer );
1700 now->Create( parent, nativeWindow );
1701 return now;
1702 }
1703