]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
Remove my binary compatibility stuff from HEAD. Patch #1367014 used for reference.
[wxWidgets.git] / src / mac / carbon / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for Mac
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001-2004 Stefan Csomor
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/toplevel.h"
30 #include "wx/frame.h"
31 #include "wx/string.h"
32 #include "wx/log.h"
33 #include "wx/intl.h"
34 #include "wx/settings.h"
35 #include "wx/strconv.h"
36 #include "wx/control.h"
37 #endif //WX_PRECOMP
38
39 #include "wx/mac/uma.h"
40 #include "wx/mac/aga.h"
41 #include "wx/app.h"
42 #include "wx/tooltip.h"
43 #include "wx/dnd.h"
44 #if wxUSE_SYSTEM_OPTIONS
45 #include "wx/sysopt.h"
46 #endif
47
48 #ifndef __DARWIN__
49 #include <ToolUtils.h>
50 #endif
51
52 //For targeting OSX
53 #include "wx/mac/private.h"
54
55 // ----------------------------------------------------------------------------
56 // constants
57 // ----------------------------------------------------------------------------
58
59 // trace mask for activation tracing messages
60 static const wxChar *TRACE_ACTIVATE = _T("activation");
61
62 // ----------------------------------------------------------------------------
63 // globals
64 // ----------------------------------------------------------------------------
65
66 // list of all frames and modeless dialogs
67 wxWindowList wxModelessWindows;
68
69 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
70
71 // ============================================================================
72 // wxTopLevelWindowMac implementation
73 // ============================================================================
74
75 BEGIN_EVENT_TABLE(wxTopLevelWindowMac, wxTopLevelWindowBase)
76 END_EVENT_TABLE()
77
78
79 // ---------------------------------------------------------------------------
80 // Carbon Events
81 // ---------------------------------------------------------------------------
82
83 extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
84
85 static const EventTypeSpec eventList[] =
86 {
87 // TODO remove control related event like key and mouse (except for WindowLeave events)
88 #if 1
89 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
90
91 { kEventClassKeyboard, kEventRawKeyDown } ,
92 { kEventClassKeyboard, kEventRawKeyRepeat } ,
93 { kEventClassKeyboard, kEventRawKeyUp } ,
94 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
95 #endif
96
97 { kEventClassWindow , kEventWindowShown } ,
98 { kEventClassWindow , kEventWindowActivated } ,
99 { kEventClassWindow , kEventWindowDeactivated } ,
100 { kEventClassWindow , kEventWindowBoundsChanging } ,
101 { kEventClassWindow , kEventWindowBoundsChanged } ,
102 { kEventClassWindow , kEventWindowClose } ,
103
104 // we have to catch these events on the toplevel window level, as controls don't get the
105 // raw mouse events anymore
106
107 { kEventClassMouse , kEventMouseDown } ,
108 { kEventClassMouse , kEventMouseUp } ,
109 { kEventClassMouse , kEventMouseWheelMoved } ,
110 { kEventClassMouse , kEventMouseMoved } ,
111 { kEventClassMouse , kEventMouseDragged } ,
112 } ;
113
114 static pascal OSStatus TextInputEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
115 {
116 OSStatus result = eventNotHandledErr ;
117
118 wxWindow* focus = wxWindow::FindFocus() ;
119 unsigned char charCode ;
120 UInt32 keyCode ;
121 UInt32 modifiers ;
122 Point point ;
123
124 EventRef rawEvent ;
125
126 GetEventParameter( event, kEventParamTextInputSendKeyboardEvent, typeEventRef, NULL, sizeof(rawEvent), NULL, &rawEvent ) ;
127
128 GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
129 GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
130 GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
131 GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
132
133 switch ( GetEventKind( event ) )
134 {
135 case kEventTextInputUnicodeForKeyEvent :
136 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
137 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
138 wxControl* control = wxDynamicCast( focus , wxControl ) ;
139 if ( control )
140 {
141 ControlRef macControl = (ControlRef) control->GetHandle() ;
142 if ( macControl )
143 {
144 ::HandleControlKey( macControl , keyCode , charCode , modifiers ) ;
145 result = noErr ;
146 }
147 }
148
149 #if 0
150 // this may lead to double events sent to a window in case all handlers have skipped the key down event
151 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
152 UInt32 message = (keyCode << 8) + charCode;
153
154 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
155 focus , message , modifiers , when , point.h , point.v ) )
156 {
157 result = noErr ;
158 }
159 #endif
160 break ;
161
162 default:
163 break ;
164 }
165
166 return result ;
167 }
168
169 static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
170 {
171 OSStatus result = eventNotHandledErr ;
172 // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
173 // FindFocus does not return the actual focus window,but the enclosing window
174 wxWindow* focus = wxWindow::DoFindFocus();
175 if ( focus == NULL )
176 focus = (wxTopLevelWindowMac*) data ;
177
178 unsigned char charCode ;
179 wxChar uniChar = 0 ;
180 UInt32 keyCode ;
181 UInt32 modifiers ;
182 Point point ;
183 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
184
185 #if wxUSE_UNICODE
186 UInt32 dataSize = 0 ;
187 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize , NULL ) == noErr )
188 {
189 UniChar buf[2] ;
190
191 UniChar* charBuf = buf ;
192
193 if ( dataSize > 4 )
194 charBuf = new UniChar[ dataSize / sizeof( UniChar) ] ;
195 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
196 #if SIZEOF_WCHAR_T == 2
197 uniChar = charBuf[0] ;
198 #else
199 wxMBConvUTF16 converter ;
200 converter.MB2WC( &uniChar , (const char*)charBuf , 1 ) ;
201 #endif
202 if ( dataSize > 4 )
203 delete[] charBuf ;
204 }
205 #endif
206
207 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
208 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
209 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
210 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
211
212 UInt32 message = (keyCode << 8) + charCode;
213 switch( GetEventKind( event ) )
214 {
215 case kEventRawKeyRepeat :
216 case kEventRawKeyDown :
217 {
218 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
219 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
220 wxTheApp->MacSetCurrentEvent( event , handler ) ;
221 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
222 focus , message , modifiers , when , point.h , point.v , uniChar ) )
223 {
224 result = noErr ;
225 }
226 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
227 }
228 break ;
229
230 case kEventRawKeyUp :
231 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
232 focus , message , modifiers , when , point.h , point.v , uniChar ) )
233 {
234 result = noErr ;
235 }
236 break ;
237
238 case kEventRawKeyModifiersChanged :
239 {
240 wxKeyEvent event(wxEVT_KEY_DOWN);
241
242 event.m_shiftDown = modifiers & shiftKey;
243 event.m_controlDown = modifiers & controlKey;
244 event.m_altDown = modifiers & optionKey;
245 event.m_metaDown = modifiers & cmdKey;
246 #if wxUSE_UNICODE
247 event.m_uniChar = uniChar ;
248 #endif
249 event.m_x = point.h;
250 event.m_y = point.v;
251 event.SetTimestamp(when);
252 event.SetEventObject(focus);
253
254 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
255 {
256 event.m_keyCode = WXK_CONTROL ;
257 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
258 focus->GetEventHandler()->ProcessEvent( event ) ;
259 }
260 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
261 {
262 event.m_keyCode = WXK_SHIFT ;
263 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
264 focus->GetEventHandler()->ProcessEvent( event ) ;
265 }
266 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
267 {
268 event.m_keyCode = WXK_ALT ;
269 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
270 focus->GetEventHandler()->ProcessEvent( event ) ;
271 }
272 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
273 {
274 event.m_keyCode = WXK_COMMAND ;
275 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
276 focus->GetEventHandler()->ProcessEvent( event ) ;
277 }
278 wxApp::s_lastModifiers = modifiers ;
279 }
280 break ;
281
282 default:
283 break;
284 }
285
286 return result ;
287 }
288
289 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
290 // for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the
291 // mouse down at all
292 //
293 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
294
295 wxWindow* g_MacLastWindow = NULL ;
296
297 static EventMouseButton lastButton = 0 ;
298
299 static void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
300 {
301 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
302 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
303
304 // this parameter are not given for all events
305 EventMouseButton button = 0 ;
306 UInt32 clickCount = 0 ;
307 cEvent.GetParameter<EventMouseButton>(kEventParamMouseButton, typeMouseButton , &button) ;
308 cEvent.GetParameter<UInt32>(kEventParamClickCount, typeUInt32 , &clickCount ) ;
309
310 wxevent.m_x = screenMouseLocation.h;
311 wxevent.m_y = screenMouseLocation.v;
312 wxevent.m_shiftDown = modifiers & shiftKey;
313 wxevent.m_controlDown = modifiers & controlKey;
314 wxevent.m_altDown = modifiers & optionKey;
315 wxevent.m_metaDown = modifiers & cmdKey;
316 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
317
318 // a control click is interpreted as a right click
319 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
320 button = kEventMouseButtonSecondary ;
321
322 // otherwise we report double clicks by connecting a left click with a ctrl-left click
323 if ( clickCount > 1 && button != lastButton )
324 clickCount = 1 ;
325
326 // we must make sure that our synthetic 'right' button corresponds in
327 // mouse down, moved and mouse up, and does not deliver a right down and left up
328
329 if ( cEvent.GetKind() == kEventMouseDown )
330 lastButton = button ;
331
332 if ( button == 0 )
333 lastButton = 0 ;
334 else if ( lastButton )
335 button = lastButton ;
336
337 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
338 // this button
339 if ( button != 0 && cEvent.GetKind() != kEventMouseUp )
340 {
341 switch( button )
342 {
343 case kEventMouseButtonPrimary :
344 wxevent.m_leftDown = true ;
345 break ;
346
347 case kEventMouseButtonSecondary :
348 wxevent.m_rightDown = true ;
349 break ;
350
351 case kEventMouseButtonTertiary :
352 wxevent.m_middleDown = true ;
353 break ;
354
355 default:
356 break ;
357 }
358 }
359
360 // translate into wx types
361 switch ( cEvent.GetKind() )
362 {
363 case kEventMouseDown :
364 switch ( button )
365 {
366 case kEventMouseButtonPrimary :
367 wxevent.SetEventType(clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
368 break ;
369
370 case kEventMouseButtonSecondary :
371 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
372 break ;
373
374 case kEventMouseButtonTertiary :
375 wxevent.SetEventType(clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
376 break ;
377
378 default:
379 break ;
380 }
381 break ;
382
383 case kEventMouseUp :
384 switch ( button )
385 {
386 case kEventMouseButtonPrimary :
387 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
388 break ;
389
390 case kEventMouseButtonSecondary :
391 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
392 break ;
393
394 case kEventMouseButtonTertiary :
395 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
396 break ;
397
398 default:
399 break ;
400 }
401 break ;
402
403 case kEventMouseWheelMoved :
404 {
405 wxevent.SetEventType(wxEVT_MOUSEWHEEL ) ;
406
407 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
408 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeLongInteger) ;
409
410 wxevent.m_wheelRotation = delta;
411 wxevent.m_wheelDelta = 1;
412 wxevent.m_linesPerAction = 1;
413 }
414 break ;
415
416 default :
417 wxevent.SetEventType(wxEVT_MOTION ) ;
418 break ;
419 }
420 }
421
422 ControlRef wxMacFindSubControl( wxTopLevelWindowMac* toplevelWindow, const Point& location , ControlRef superControl , ControlPartCode *outPart )
423 {
424 if ( superControl )
425 {
426 UInt16 childrenCount = 0 ;
427 OSStatus err = CountSubControls( superControl , &childrenCount ) ;
428 if ( err == errControlIsNotEmbedder )
429 return NULL ;
430 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
431
432 for ( UInt16 i = childrenCount ; i >=1 ; --i )
433 {
434 ControlHandle sibling ;
435 err = GetIndexedSubControl( superControl , i , & sibling ) ;
436 if ( err == errControlIsNotEmbedder )
437 return NULL ;
438
439 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
440 if ( IsControlVisible( sibling ) )
441 {
442 Rect r ;
443 UMAGetControlBoundsInWindowCoords( sibling , &r ) ;
444 if ( MacPtInRect( location , &r ) )
445 {
446 ControlHandle child = wxMacFindSubControl( toplevelWindow , location , sibling , outPart ) ;
447 if ( child )
448 return child ;
449 else
450 {
451 Point testLocation = location ;
452
453 if ( toplevelWindow && toplevelWindow->MacUsesCompositing() )
454 {
455 testLocation.h -= r.left ;
456 testLocation.v -= r.top ;
457 }
458
459 *outPart = TestControl( sibling , testLocation ) ;
460 return sibling ;
461 }
462 }
463 }
464 }
465 }
466
467 return NULL ;
468 }
469
470 ControlRef wxMacFindControlUnderMouse( wxTopLevelWindowMac* toplevelWindow , const Point& location , WindowRef window , ControlPartCode *outPart )
471 {
472 #if TARGET_API_MAC_OSX
473 if ( UMAGetSystemVersion() >= 0x1030 && ( toplevelWindow == 0 || toplevelWindow->MacUsesCompositing() ) )
474 return FindControlUnderMouse( location , window , outPart ) ;
475 #endif
476 ControlRef rootControl = NULL ;
477 verify_noerr( GetRootControl( window , &rootControl ) ) ;
478 return wxMacFindSubControl( toplevelWindow , location , rootControl , outPart ) ;
479
480 }
481
482 #define NEW_CAPTURE_HANDLING 1
483
484 pascal OSStatus wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
485 {
486 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
487
488 OSStatus result = eventNotHandledErr ;
489
490 wxMacCarbonEvent cEvent( event ) ;
491
492 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
493 Point windowMouseLocation = screenMouseLocation ;
494
495 WindowRef window ;
496 short windowPart = ::FindWindow(screenMouseLocation, &window);
497
498 wxWindow* currentMouseWindow = NULL ;
499 ControlRef control = NULL ;
500
501 #if NEW_CAPTURE_HANDLING
502 if ( wxApp::s_captureWindow )
503 {
504 window = (WindowRef) wxApp::s_captureWindow->MacGetTopLevelWindowRef() ;
505 windowPart = inContent ;
506 }
507 #endif
508
509 if ( window )
510 {
511 QDGlobalToLocalPoint( UMAGetWindowPort(window ) , &windowMouseLocation ) ;
512
513 if ( wxApp::s_captureWindow
514 #if !NEW_CAPTURE_HANDLING
515 && wxApp::s_captureWindow->MacGetTopLevelWindowRef() == (WXWindow) window && windowPart == inContent
516 #endif
517 )
518 {
519 currentMouseWindow = wxApp::s_captureWindow ;
520 }
521 else if ( (IsWindowActive(window) && windowPart == inContent) )
522 {
523 ControlPartCode part ;
524 control = wxMacFindControlUnderMouse( toplevelWindow , windowMouseLocation , window , &part ) ;
525 // if there is no control below the mouse position, send the event to the toplevel window itself
526 if ( control == 0 )
527 currentMouseWindow = (wxWindow*) data ;
528 else
529 {
530 currentMouseWindow = wxFindControlFromMacControl( control ) ;
531 if ( currentMouseWindow == NULL && cEvent.GetKind() == kEventMouseMoved )
532 {
533 #if wxUSE_TOOLBAR
534 // for wxToolBar to function we have to send certaint events to it
535 // instead of its children (wxToolBarTools)
536 ControlRef parent ;
537 GetSuperControl(control, &parent );
538 wxWindow *wxParent = wxFindControlFromMacControl( parent ) ;
539 if ( wxParent && wxParent->IsKindOf( CLASSINFO( wxToolBar ) ) )
540 currentMouseWindow = wxParent ;
541 #endif
542 }
543 }
544 }
545 }
546
547 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
548 SetupMouseEvent( wxevent , cEvent ) ;
549
550 // handle all enter / leave events
551
552 if ( currentMouseWindow != g_MacLastWindow )
553 {
554 if ( g_MacLastWindow )
555 {
556 wxMouseEvent eventleave(wxevent);
557 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
558 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
559 eventleave.SetEventObject( g_MacLastWindow ) ;
560 wxevent.SetId( g_MacLastWindow->GetId() ) ;
561 #if wxUSE_TOOLTIPS
562 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
563 #endif // wxUSE_TOOLTIPS
564 g_MacLastWindow->GetEventHandler()->ProcessEvent(eventleave);
565 }
566
567 if ( currentMouseWindow )
568 {
569 wxMouseEvent evententer(wxevent);
570 evententer.SetEventType( wxEVT_ENTER_WINDOW );
571 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
572 evententer.SetEventObject( currentMouseWindow ) ;
573 wxevent.SetId( currentMouseWindow->GetId() ) ;
574 #if wxUSE_TOOLTIPS
575 wxToolTip::RelayEvent( currentMouseWindow , evententer);
576 #endif // wxUSE_TOOLTIPS
577 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
578 }
579
580 g_MacLastWindow = currentMouseWindow ;
581 }
582
583 if ( windowPart == inMenuBar )
584 {
585 // special case menu bar, as we are having a low-level runloop we must do it ourselves
586 if ( cEvent.GetKind() == kEventMouseDown )
587 {
588 ::MenuSelect( screenMouseLocation ) ;
589 result = noErr ;
590 }
591 }
592 else if ( currentMouseWindow )
593 {
594 wxWindow *currentMouseWindowParent = currentMouseWindow->GetParent();
595
596 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
597
598 wxevent.SetEventObject( currentMouseWindow ) ;
599 wxevent.SetId( currentMouseWindow->GetId() ) ;
600
601 // make tooltips current
602
603 #if wxUSE_TOOLTIPS
604 if ( wxevent.GetEventType() == wxEVT_MOTION
605 || wxevent.GetEventType() == wxEVT_ENTER_WINDOW
606 || wxevent.GetEventType() == wxEVT_LEAVE_WINDOW )
607 wxToolTip::RelayEvent( currentMouseWindow , wxevent);
608 #endif // wxUSE_TOOLTIPS
609 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
610 {
611 if ((currentMouseWindowParent != NULL) &&
612 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
613 currentMouseWindow = NULL;
614
615 result = noErr;
616 }
617 else
618 {
619 // if the user code did _not_ handle the event, then perform the
620 // default processing
621 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
622 {
623 // ... that is set focus to this window
624 if (currentMouseWindow->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow)
625 currentMouseWindow->SetFocus();
626 }
627
628 ControlPartCode dummyPart ;
629 // if built-in find control is finding the wrong control (ie static box instead of overlaid
630 // button, we cannot let the standard handler do its job, but must handle manually
631
632 if ( ( cEvent.GetKind() == kEventMouseDown )
633 #ifdef __WXMAC_OSX__
634 &&
635 (FindControlUnderMouse(windowMouseLocation , window , &dummyPart) !=
636 wxMacFindControlUnderMouse( toplevelWindow , windowMouseLocation , window , &dummyPart ) )
637 #endif
638 )
639 {
640 if ( currentMouseWindow->MacIsReallyEnabled() )
641 {
642 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
643 Point clickLocation = windowMouseLocation ;
644
645 if ( toplevelWindow->MacUsesCompositing() )
646 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
647
648 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
649 modifiers , (ControlActionUPP ) -1 ) ;
650
651 if ((currentMouseWindowParent != NULL) &&
652 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
653 currentMouseWindow = NULL;
654 }
655 result = noErr ;
656 }
657 }
658
659 if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
660 {
661 wxApp::s_captureWindow = NULL ;
662 // update cursor ?
663 }
664
665 // update cursor
666
667 wxWindow* cursorTarget = currentMouseWindow ;
668 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
669
670 while ( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
671 {
672 cursorTarget = cursorTarget->GetParent() ;
673 if ( cursorTarget )
674 cursorPoint += cursorTarget->GetPosition();
675 }
676
677 } // else if ( currentMouseWindow )
678 else
679 {
680 // don't mess with controls we don't know about
681 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
682 // so we try sending them the correct control directly
683 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
684 {
685 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
686 Point clickLocation = windowMouseLocation ;
687 if ( toplevelWindow->MacUsesCompositing() )
688 {
689 #ifdef __WXMAC_OSX__
690 HIPoint hiPoint ;
691 hiPoint.x = clickLocation.h ;
692 hiPoint.y = clickLocation.v ;
693 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
694 clickLocation.h = (int)hiPoint.x ;
695 clickLocation.v = (int)hiPoint.y ;
696 #endif
697 }
698
699 HandleControlClick( control , clickLocation , modifiers , (ControlActionUPP ) -1 ) ;
700 result = noErr ;
701 }
702 }
703 return result ;
704 }
705
706 static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
707 {
708 OSStatus result = eventNotHandledErr ;
709
710 wxMacCarbonEvent cEvent( event ) ;
711
712 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
713 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
714
715 switch ( GetEventKind( event ) )
716 {
717 case kEventWindowActivated :
718 {
719 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
720 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
721 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
722 wxevent.SetEventObject(toplevelWindow);
723 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
724 // we still sending an eventNotHandledErr in order to allow for default processing
725 }
726 break ;
727
728 case kEventWindowDeactivated :
729 {
730 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
731 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
732 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
733 wxevent.SetEventObject(toplevelWindow);
734 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
735 // we still sending an eventNotHandledErr in order to allow for default processing
736 }
737 break ;
738
739 case kEventWindowShown :
740 toplevelWindow->Refresh() ;
741 result = noErr ;
742 break ;
743
744 case kEventWindowClose :
745 toplevelWindow->Close() ;
746 result = noErr ;
747 break ;
748
749 case kEventWindowBoundsChanged :
750 {
751 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes, typeUInt32) ;
752 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
753 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
754 if ( attributes & kWindowBoundsChangeSizeChanged )
755 {
756 // according to the other ports we handle this within the OS level
757 // resize event, not within a wxSizeEvent
758 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
759 if ( frame )
760 {
761 #if wxUSE_STATUSBAR
762 frame->PositionStatusBar();
763 #endif
764 #if wxUSE_TOOLBAR
765 frame->PositionToolBar();
766 #endif
767 }
768
769 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
770 event.SetEventObject( toplevelWindow ) ;
771
772 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
773 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
774 }
775
776 if ( attributes & kWindowBoundsChangeOriginChanged )
777 {
778 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
779 event.SetEventObject( toplevelWindow ) ;
780 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
781 }
782
783 result = noErr ;
784 }
785 break ;
786
787 case kEventWindowBoundsChanging :
788 {
789 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
790 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
791
792 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
793 {
794 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
795 int left , top , right , bottom ;
796 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
797
798 wxRect r(
799 newRect.left - left,
800 newRect.top - top,
801 newRect.right - newRect.left + left + right,
802 newRect.bottom - newRect.top + top + bottom ) ;
803
804 // this is a EVT_SIZING not a EVT_SIZE type !
805 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
806 wxevent.SetEventObject( toplevelWindow ) ;
807 wxRect adjustR = r ;
808 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
809 adjustR = wxevent.GetRect() ;
810
811 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
812 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
813 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
814 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
815 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
816 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
817 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
818 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
819 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
820 if ( !EqualRect( &newRect , &adjustedRect ) )
821 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
822 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
823 }
824
825 result = noErr ;
826 }
827 break ;
828
829 default :
830 break ;
831 }
832
833 return result ;
834 }
835
836 pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
837 {
838 OSStatus result = eventNotHandledErr ;
839
840 switch ( GetEventClass( event ) )
841 {
842 case kEventClassKeyboard :
843 result = KeyboardEventHandler( handler, event , data ) ;
844 break ;
845
846 case kEventClassTextInput :
847 result = TextInputEventHandler( handler, event , data ) ;
848 break ;
849
850 case kEventClassWindow :
851 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
852 break ;
853
854 case kEventClassMouse :
855 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
856 break ;
857
858 default :
859 break ;
860 }
861
862 return result ;
863 }
864
865 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
866
867 // ---------------------------------------------------------------------------
868 // wxWindowMac utility functions
869 // ---------------------------------------------------------------------------
870
871 // Find an item given the Macintosh Window Reference
872
873 WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
874
875 static MacWindowMap wxWinMacWindowList;
876
877 wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
878 {
879 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
880
881 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
882 }
883
884 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
885 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
886 {
887 // adding NULL WindowRef is (first) surely a result of an error and
888 // nothing else :-)
889 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
890
891 wxWinMacWindowList[inWindowRef] = win;
892 }
893
894 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
895 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
896 {
897 MacWindowMap::iterator it;
898 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
899 {
900 if ( it->second == win )
901 {
902 wxWinMacWindowList.erase(it);
903 break;
904 }
905 }
906 }
907
908 // ----------------------------------------------------------------------------
909 // wxTopLevelWindowMac creation
910 // ----------------------------------------------------------------------------
911
912 wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
913
914 typedef struct
915 {
916 wxPoint m_position ;
917 wxSize m_size ;
918 }
919 FullScreenData ;
920
921 void wxTopLevelWindowMac::Init()
922 {
923 m_iconized =
924 m_maximizeOnShow = false;
925 m_macWindow = NULL ;
926
927 #if TARGET_API_MAC_OSX
928 m_macUsesCompositing = ( UMAGetSystemVersion() >= 0x1030 );
929 #else
930 m_macUsesCompositing = false;
931 #endif
932
933 m_macEventHandler = NULL ;
934 m_macFullScreenData = NULL ;
935 }
936
937 class wxMacDeferredWindowDeleter : public wxObject
938 {
939 public :
940 wxMacDeferredWindowDeleter( WindowRef windowRef )
941 {
942 m_macWindow = windowRef ;
943 }
944
945 virtual ~wxMacDeferredWindowDeleter()
946 {
947 UMADisposeWindow( (WindowRef) m_macWindow ) ;
948 }
949
950 protected :
951 WindowRef m_macWindow ;
952 } ;
953
954 bool wxTopLevelWindowMac::Create(wxWindow *parent,
955 wxWindowID id,
956 const wxString& title,
957 const wxPoint& pos,
958 const wxSize& size,
959 long style,
960 const wxString& name)
961 {
962 // init our fields
963 Init();
964
965 m_windowStyle = style;
966
967 SetName( name );
968
969 m_windowId = id == -1 ? NewControlId() : id;
970 wxWindow::SetLabel( title ) ;
971
972 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
973
974 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
975
976 if (GetExtraStyle() & wxFRAME_EX_METAL)
977 MacSetMetalAppearance(true);
978
979 wxTopLevelWindows.Append(this);
980
981 if ( parent )
982 parent->AddChild(this);
983
984 return true;
985 }
986
987 wxTopLevelWindowMac::~wxTopLevelWindowMac()
988 {
989 if ( m_macWindow )
990 {
991 #if wxUSE_TOOLTIPS
992 wxToolTip::NotifyWindowDelete(m_macWindow) ;
993 #endif
994 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
995 }
996
997 if ( m_macEventHandler )
998 {
999 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
1000 m_macEventHandler = NULL ;
1001 }
1002
1003 wxRemoveMacWindowAssociation( this ) ;
1004
1005 if ( wxModelessWindows.Find(this) )
1006 wxModelessWindows.DeleteObject(this);
1007
1008 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1009 delete data ;
1010 m_macFullScreenData = NULL ;
1011 }
1012
1013
1014 // ----------------------------------------------------------------------------
1015 // wxTopLevelWindowMac maximize/minimize
1016 // ----------------------------------------------------------------------------
1017
1018 void wxTopLevelWindowMac::Maximize(bool maximize)
1019 {
1020 // TODO Check, is this still necessary
1021 #if 0
1022 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
1023 wxMacWindowClipper clip (this);
1024 #endif
1025
1026 if ( !IsWindowInStandardState( (WindowRef)m_macWindow, NULL, NULL ) )
1027 {
1028 Rect rect;
1029 GetWindowBounds((WindowRef)m_macWindow, kWindowGlobalPortRgn, &rect);
1030 SetWindowIdealUserState((WindowRef)m_macWindow, &rect);
1031 SetWindowUserState((WindowRef)m_macWindow, &rect);
1032 }
1033
1034 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
1035 }
1036
1037 bool wxTopLevelWindowMac::IsMaximized() const
1038 {
1039 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
1040 }
1041
1042 void wxTopLevelWindowMac::Iconize(bool iconize)
1043 {
1044 if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
1045 CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
1046 }
1047
1048 bool wxTopLevelWindowMac::IsIconized() const
1049 {
1050 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
1051 }
1052
1053 void wxTopLevelWindowMac::Restore()
1054 {
1055 if ( IsMaximized() )
1056 Maximize(false);
1057 else if ( IsIconized() )
1058 Iconize(false);
1059 }
1060
1061 // ----------------------------------------------------------------------------
1062 // wxTopLevelWindowMac misc
1063 // ----------------------------------------------------------------------------
1064
1065 wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1066 {
1067 return wxPoint(0, 0) ;
1068 }
1069
1070 void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
1071 {
1072 // this sets m_icon
1073 wxTopLevelWindowBase::SetIcon(icon);
1074 }
1075
1076 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
1077 {
1078 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1079
1080 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1081 {
1082 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1083 }
1084 }
1085
1086 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
1087 {
1088 if ( m_macEventHandler != NULL )
1089 {
1090 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1091 }
1092
1093 InstallWindowEventHandler(
1094 MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
1095 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler );
1096 }
1097
1098 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
1099 const wxPoint& pos,
1100 const wxSize& size,
1101 long style,
1102 const wxString& name )
1103 {
1104 OSStatus err = noErr ;
1105 SetName(name);
1106 m_windowStyle = style;
1107 m_isShown = false;
1108
1109 // create frame.
1110 int x = (int)pos.x;
1111 int y = (int)pos.y;
1112
1113 Rect theBoundsRect;
1114 wxRect display = wxGetClientDisplayRect() ;
1115
1116 if ( x == wxDefaultPosition.x )
1117 x = display.x ;
1118
1119 if ( y == wxDefaultPosition.y )
1120 y = display.y ;
1121
1122 int w = WidthDefault(size.x);
1123 int h = HeightDefault(size.y);
1124
1125 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1126
1127 // translate the window attributes in the appropriate window class and attributes
1128 WindowClass wclass = 0;
1129 WindowAttributes attr = kWindowNoAttributes ;
1130 WindowGroupRef group = NULL ;
1131
1132 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
1133 {
1134 if (
1135 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1136 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1137 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
1138 )
1139 {
1140 wclass = kFloatingWindowClass ;
1141
1142 if ( HasFlag(wxTINY_CAPTION_VERT) )
1143 attr |= kWindowSideTitlebarAttribute ;
1144 }
1145 else
1146 {
1147 wclass = kPlainWindowClass ;
1148 }
1149 }
1150 else if ( HasFlag( wxCAPTION ) )
1151 {
1152 wclass = kDocumentWindowClass ;
1153 attr |= kWindowInWindowMenuAttribute ;
1154 }
1155 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
1156 else if ( HasFlag( wxFRAME_DRAWER ) )
1157 {
1158 wclass = kDrawerWindowClass;
1159 // we must force compositing on a drawer
1160 m_macUsesCompositing = true ;
1161 }
1162 #endif //10.2 and up
1163 else
1164 {
1165 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1166 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
1167 {
1168 wclass = kDocumentWindowClass ;
1169 }
1170 else
1171 {
1172 wclass = kPlainWindowClass ;
1173 }
1174 }
1175
1176 if ( wclass != kPlainWindowClass )
1177 {
1178 if ( HasFlag( wxMINIMIZE_BOX ) )
1179 attr |= kWindowCollapseBoxAttribute ;
1180
1181 if ( HasFlag( wxMAXIMIZE_BOX ) )
1182 attr |= kWindowFullZoomAttribute ;
1183
1184 if ( HasFlag( wxRESIZE_BORDER ) )
1185 attr |= kWindowResizableAttribute ;
1186
1187 if ( HasFlag( wxCLOSE_BOX) )
1188 attr |= kWindowCloseBoxAttribute ;
1189 }
1190
1191 // turn on live resizing (OS X only)
1192 if (UMAGetSystemVersion() >= 0x1000)
1193 attr |= kWindowLiveResizeAttribute;
1194
1195 if ( HasFlag(wxSTAY_ON_TOP) )
1196 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1197
1198 #if TARGET_API_MAC_OSX
1199 if ( m_macUsesCompositing )
1200 attr |= kWindowCompositingAttribute;
1201 #endif
1202
1203 if ( HasFlag(wxFRAME_SHAPED) )
1204 {
1205 WindowDefSpec customWindowDefSpec;
1206 customWindowDefSpec.defType = kWindowDefProcPtr;
1207 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1208
1209 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1210 attr, &theBoundsRect,
1211 (WindowRef*) &m_macWindow);
1212 }
1213 else
1214 {
1215 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1216 }
1217
1218 if ( err == noErr && m_macWindow != NULL && group != NULL )
1219 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1220
1221 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1222
1223 // the create commands are only for content rect, so we have to set the size again as
1224 // structure bounds
1225 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1226
1227 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1228 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1229 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
1230 #if TARGET_API_MAC_OSX
1231
1232 if ( m_macUsesCompositing )
1233 {
1234 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1235 // the content view, so we have to retrieve it explicitly
1236 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1237 m_peer->GetControlRefAddr() ) ;
1238 if ( !m_peer->Ok() )
1239 {
1240 // compatibility mode fallback
1241 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1242 }
1243 }
1244 #endif
1245 {
1246 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
1247 }
1248
1249 // the root control level handler
1250 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1251
1252 #if TARGET_API_MAC_OSX
1253 if ( m_macUsesCompositing && m_macWindow != NULL )
1254 {
1255 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1256 MacSetMetalAppearance( true ) ;
1257 }
1258 #endif
1259
1260 // the frame window event handler
1261 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1262 MacInstallTopLevelWindowEventHandler() ;
1263
1264 DoSetWindowVariant( m_windowVariant ) ;
1265
1266 m_macFocus = NULL ;
1267
1268 if ( HasFlag(wxFRAME_SHAPED) )
1269 {
1270 // default shape matches the window size
1271 wxRegion rgn( 0, 0, w, h );
1272 SetShape( rgn );
1273 }
1274
1275 wxWindowCreateEvent event(this);
1276 GetEventHandler()->ProcessEvent(event);
1277 }
1278
1279 void wxTopLevelWindowMac::ClearBackground()
1280 {
1281 wxWindow::ClearBackground() ;
1282 }
1283
1284 // Raise the window to the top of the Z order
1285 void wxTopLevelWindowMac::Raise()
1286 {
1287 ::SelectWindow( (WindowRef)m_macWindow ) ;
1288 }
1289
1290 // Lower the window to the bottom of the Z order
1291 void wxTopLevelWindowMac::Lower()
1292 {
1293 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1294 }
1295
1296 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1297 {
1298 if (s_macDeactivateWindow)
1299 {
1300 wxLogTrace(TRACE_ACTIVATE,
1301 wxT("Doing delayed deactivation of %p"),
1302 s_macDeactivateWindow);
1303
1304 s_macDeactivateWindow->MacActivate(timestamp, false);
1305 }
1306 }
1307
1308 void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
1309 {
1310 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
1311
1312 if (s_macDeactivateWindow == this)
1313 s_macDeactivateWindow = NULL;
1314
1315 MacDelayedDeactivation(timestamp);
1316 MacPropagateHiliteChanged() ;
1317 }
1318
1319 void wxTopLevelWindowMac::SetTitle(const wxString& title)
1320 {
1321 wxWindow::SetLabel( title ) ;
1322 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
1323 }
1324
1325 wxString wxTopLevelWindowMac::GetTitle() const
1326 {
1327 return wxWindow::GetLabel();
1328 }
1329
1330 bool wxTopLevelWindowMac::Show(bool show)
1331 {
1332 if ( !wxTopLevelWindowBase::Show(show) )
1333 return false;
1334
1335 if (show)
1336 {
1337 #if wxUSE_SYSTEM_OPTIONS // code contributed by Ryan Wilcox December 18, 2003
1338 bool plainTransition = UMAGetSystemVersion() >= 0x1000 ;
1339 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1340 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1341
1342 if ( plainTransition )
1343 {
1344 ::ShowWindow( (WindowRef)m_macWindow );
1345 }
1346 else
1347 #endif
1348 {
1349 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
1350 }
1351
1352 ::SelectWindow( (WindowRef)m_macWindow ) ;
1353
1354 // because apps expect a size event to occur at this moment
1355 wxSizeEvent event( GetSize() , m_windowId);
1356 event.SetEventObject(this);
1357 GetEventHandler()->ProcessEvent(event);
1358 }
1359 else
1360 {
1361 #if wxUSE_SYSTEM_OPTIONS
1362 bool plainTransition = UMAGetSystemVersion() >= 0x1000 ;
1363 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1364 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1365 if ( plainTransition )
1366 {
1367 ::HideWindow((WindowRef) m_macWindow );
1368 }
1369 else
1370 #endif
1371 {
1372 ::TransitionWindow((WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
1373 }
1374 }
1375
1376 MacPropagateVisibilityChanged() ;
1377
1378 return true ;
1379 }
1380
1381 bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
1382 {
1383 if ( show )
1384 {
1385 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1386 delete data ;
1387 data = new FullScreenData() ;
1388
1389 m_macFullScreenData = data ;
1390 data->m_position = GetPosition() ;
1391 data->m_size = GetSize() ;
1392
1393 if ( style & wxFULLSCREEN_NOMENUBAR )
1394 HideMenuBar() ;
1395
1396 wxRect client = wxGetClientDisplayRect() ;
1397
1398 int left , top , right , bottom ;
1399 int x, y, w, h ;
1400
1401 x = client.x ;
1402 y = client.y ;
1403 w = client.width ;
1404 h = client.height ;
1405
1406 MacGetContentAreaInset( left , top , right , bottom ) ;
1407
1408 if ( style & wxFULLSCREEN_NOCAPTION )
1409 {
1410 y -= top ;
1411 h += top ;
1412 }
1413
1414 if ( style & wxFULLSCREEN_NOBORDER )
1415 {
1416 x -= left ;
1417 w += left + right ;
1418 h += bottom ;
1419 }
1420
1421 if ( style & wxFULLSCREEN_NOTOOLBAR )
1422 {
1423 // TODO
1424 }
1425
1426 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1427 {
1428 // TODO
1429 }
1430
1431 SetSize( x , y , w, h ) ;
1432 }
1433 else
1434 {
1435 ShowMenuBar() ;
1436 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1437 SetPosition( data->m_position ) ;
1438 SetSize( data->m_size ) ;
1439
1440 delete data ;
1441 m_macFullScreenData = NULL ;
1442 }
1443
1444 return false;
1445 }
1446
1447 bool wxTopLevelWindowMac::IsFullScreen() const
1448 {
1449 return m_macFullScreenData != NULL ;
1450 }
1451
1452 void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
1453 {
1454 if ( GetExtraStyle() == exStyle )
1455 return ;
1456
1457 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
1458
1459 #if TARGET_API_MAC_OSX
1460 if ( m_macUsesCompositing && m_macWindow != NULL )
1461 {
1462 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1463 if ( MacGetMetalAppearance() != metal )
1464 MacSetMetalAppearance( metal ) ;
1465 }
1466 #endif
1467 }
1468
1469 // we are still using coordinates of the content view; TODO: switch to structure bounds
1470
1471 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1472 {
1473 Rect content ;
1474 Rect structure ;
1475 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1476 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1477
1478 left = content.left - structure.left ;
1479 top = content.top - structure.top ;
1480 right = structure.right - content.right ;
1481 bottom = structure.bottom - content.bottom ;
1482 }
1483
1484 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1485 {
1486 m_cachedClippedRectValid = false ;
1487 Rect bounds = { y , x , y + height , x + width } ;
1488 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1489 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1490 }
1491
1492 void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
1493 {
1494 Rect bounds ;
1495
1496 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1497
1498 if (x)
1499 *x = bounds.left ;
1500 if (y)
1501 *y = bounds.top ;
1502 }
1503
1504 void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1505 {
1506 Rect bounds ;
1507
1508 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1509
1510 if (width)
1511 *width = bounds.right - bounds.left ;
1512 if (height)
1513 *height = bounds.bottom - bounds.top ;
1514 }
1515
1516 void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1517 {
1518 Rect bounds ;
1519
1520 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1521
1522 if (width)
1523 *width = bounds.right - bounds.left ;
1524 if (height)
1525 *height = bounds.bottom - bounds.top ;
1526 }
1527
1528 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1529 {
1530 #if TARGET_API_MAC_OSX
1531 wxASSERT_MSG( m_macUsesCompositing ,
1532 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1533
1534 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1535 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1536 #endif
1537 }
1538
1539 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1540 {
1541 #if TARGET_API_MAC_OSX
1542 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1543 #else
1544 return false ;
1545 #endif
1546 }
1547
1548 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1549 {
1550 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
1551 }
1552
1553 wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1554 {
1555 UInt32 attr = 0 ;
1556 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1557
1558 return attr ;
1559 }
1560
1561 void wxTopLevelWindowMac::MacPerformUpdates()
1562 {
1563 #if TARGET_API_MAC_OSX
1564 if ( m_macUsesCompositing )
1565 {
1566 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1567 // for composited windows this also triggers a redraw of all
1568 // invalid views in the window
1569 if ( UMAGetSystemVersion() >= 0x1030 )
1570 HIWindowFlush((WindowRef) m_macWindow) ;
1571 else
1572 #endif
1573 {
1574 // the only way to trigger the redrawing on earlier systems is to call
1575 // ReceiveNextEvent
1576
1577 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
1578 UInt32 currentEventClass = 0 ;
1579 UInt32 currentEventKind = 0 ;
1580 if ( currentEvent != NULL )
1581 {
1582 currentEventClass = ::GetEventClass( currentEvent ) ;
1583 currentEventKind = ::GetEventKind( currentEvent ) ;
1584 }
1585
1586 if ( currentEventClass != kEventClassMenu )
1587 {
1588 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
1589 EventRef theEvent;
1590 OSStatus status = noErr ;
1591 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
1592 }
1593 }
1594 }
1595 else
1596 #endif
1597 {
1598 BeginUpdate( (WindowRef) m_macWindow ) ;
1599
1600 RgnHandle updateRgn = NewRgn();
1601 if ( updateRgn )
1602 {
1603 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
1604 UpdateControls( (WindowRef)m_macWindow , updateRgn ) ;
1605
1606 // if ( !EmptyRgn( updateRgn ) )
1607 // MacDoRedraw( updateRgn , 0 , true) ;
1608
1609 DisposeRgn( updateRgn );
1610 }
1611
1612 EndUpdate( (WindowRef)m_macWindow ) ;
1613 QDFlushPortBuffer( GetWindowPort( (WindowRef)m_macWindow ) , NULL ) ;
1614 }
1615 }
1616
1617 // Attracts the users attention to this window if the application is
1618 // inactive (should be called when a background event occurs)
1619
1620 static pascal void wxMacNMResponse( NMRecPtr ptr )
1621 {
1622 NMRemove( ptr ) ;
1623 DisposePtr( (Ptr) ptr ) ;
1624 }
1625
1626
1627 void wxTopLevelWindowMac::RequestUserAttention(int flags )
1628 {
1629 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
1630 static wxMacNMUPP nmupp( wxMacNMResponse );
1631
1632 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1633 notificationRequest->qType = nmType ;
1634 notificationRequest->nmMark = 1 ;
1635 notificationRequest->nmIcon = 0 ;
1636 notificationRequest->nmSound = 0 ;
1637 notificationRequest->nmStr = NULL ;
1638 notificationRequest->nmResp = nmupp ;
1639
1640 verify_noerr( NMInstall( notificationRequest ) ) ;
1641 }
1642
1643 // ---------------------------------------------------------------------------
1644 // Shape implementation
1645 // ---------------------------------------------------------------------------
1646
1647
1648 bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1649 {
1650 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
1651 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1652
1653 // The empty region signifies that the shape should be removed from the
1654 // window.
1655 if ( region.IsEmpty() )
1656 {
1657 wxSize sz = GetClientSize();
1658 wxRegion rgn(0, 0, sz.x, sz.y);
1659 if ( rgn.IsEmpty() )
1660 return false ;
1661 else
1662 return SetShape(rgn);
1663 }
1664
1665 // Make a copy of the region
1666 RgnHandle shapeRegion = NewRgn();
1667 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1668
1669 // Dispose of any shape region we may already have
1670 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1671 if ( oldRgn )
1672 DisposeRgn(oldRgn);
1673
1674 // Save the region so we can use it later
1675 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1676
1677 // inform the window manager that the window has changed shape
1678 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1679
1680 return true;
1681 }
1682
1683 // ---------------------------------------------------------------------------
1684 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1685 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1686 // ---------------------------------------------------------------------------
1687
1688 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1689 {
1690 GetWindowPortBounds(window, inRect);
1691 Point pt = {inRect->left, inRect->top};
1692 QDLocalToGlobalPoint( GetWindowPort(window) , &pt ) ;
1693 inRect->top = pt.v;
1694 inRect->left = pt.h;
1695 inRect->bottom += pt.v;
1696 inRect->right += pt.h;
1697 }
1698
1699 static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1700 {
1701 /*------------------------------------------------------
1702 Define which options your custom window supports.
1703 --------------------------------------------------------*/
1704 //just enable everything for our demo
1705 *(OptionBits*)param =
1706 //kWindowCanGrow |
1707 //kWindowCanZoom |
1708 //kWindowCanCollapse |
1709 //kWindowCanGetWindowRegion |
1710 //kWindowHasTitleBar |
1711 //kWindowSupportsDragHilite |
1712 kWindowCanDrawInCurrentPort |
1713 //kWindowCanMeasureTitle |
1714 kWindowWantsDisposeAtProcessDeath |
1715 kWindowSupportsGetGrowImageRegion |
1716 kWindowDefSupportsColorGrafPort;
1717
1718 return 1;
1719 }
1720
1721 // The content region is left as a rectangle matching the window size, this is
1722 // so the origin in the paint event, and etc. still matches what the
1723 // programmer expects.
1724 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1725 {
1726 SetEmptyRgn(rgn);
1727 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1728 if (win)
1729 {
1730 Rect r ;
1731 wxShapedMacWindowGetPos( window, &r ) ;
1732 RectRgn( rgn , &r ) ;
1733 }
1734 }
1735
1736 // The structure region is set to the shape given to the SetShape method.
1737 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1738 {
1739 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1740
1741 SetEmptyRgn(rgn);
1742 if (cachedRegion)
1743 {
1744 Rect windowRect;
1745 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1746 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
1747 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1748 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1749 }
1750 }
1751
1752 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1753 {
1754 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1755
1756 if (rgnRec == NULL)
1757 return paramErr;
1758
1759 switch (rgnRec->regionCode)
1760 {
1761 case kWindowStructureRgn:
1762 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1763 break;
1764
1765 case kWindowContentRgn:
1766 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1767 break;
1768
1769 default:
1770 SetEmptyRgn(rgnRec->winRgn);
1771 break;
1772 }
1773
1774 return noErr;
1775 }
1776
1777 // Determine the region of the window which was hit
1778 //
1779 static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
1780 {
1781 Point hitPoint;
1782 static RgnHandle tempRgn = NULL;
1783
1784 if (tempRgn == NULL)
1785 tempRgn = NewRgn();
1786
1787 // get the point clicked
1788 SetPt( &hitPoint, LoWord(param), HiWord(param) );
1789
1790 // Mac OS 8.5 or later
1791 wxShapedMacWindowStructureRegion(window, tempRgn);
1792 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
1793 return wInContent;
1794
1795 // no significant area was hit
1796 return wNoHit;
1797 }
1798
1799 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1800 {
1801 switch (message)
1802 {
1803 case kWindowMsgHitTest:
1804 return wxShapedMacWindowHitTest(window, param);
1805
1806 case kWindowMsgGetFeatures:
1807 return wxShapedMacWindowGetFeatures(window, param);
1808
1809 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1810 case kWindowMsgGetRegion:
1811 return wxShapedMacWindowGetRegion(window, param);
1812
1813 default:
1814 break;
1815 }
1816
1817 return 0;
1818 }
1819