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