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