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