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