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