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