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