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