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