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