]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
7a58cf75068075c8fe4d06bbce9449fdc4faa6b8
[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 }
502
503 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
504 SetupMouseEvent( wxevent , cEvent ) ;
505
506 // handle all enter / leave events
507
508 if ( currentMouseWindow != g_MacLastWindow )
509 {
510 if ( g_MacLastWindow )
511 {
512 wxMouseEvent eventleave(wxevent);
513 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
514 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
515 eventleave.SetEventObject( g_MacLastWindow ) ;
516 wxevent.SetId( g_MacLastWindow->GetId() ) ;
517
518 #if wxUSE_TOOLTIPS
519 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
520 #endif
521
522 g_MacLastWindow->GetEventHandler()->ProcessEvent(eventleave);
523 }
524
525 if ( currentMouseWindow )
526 {
527 wxMouseEvent evententer(wxevent);
528 evententer.SetEventType( wxEVT_ENTER_WINDOW );
529 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
530 evententer.SetEventObject( currentMouseWindow ) ;
531 wxevent.SetId( currentMouseWindow->GetId() ) ;
532
533 #if wxUSE_TOOLTIPS
534 wxToolTip::RelayEvent( currentMouseWindow , evententer );
535 #endif
536
537 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
538 }
539
540 g_MacLastWindow = currentMouseWindow ;
541 }
542
543 if ( windowPart == inMenuBar )
544 {
545 // special case menu bar, as we are having a low-level runloop we must do it ourselves
546 if ( cEvent.GetKind() == kEventMouseDown )
547 {
548 ::MenuSelect( screenMouseLocation ) ;
549 result = noErr ;
550 }
551 }
552 else if ( currentMouseWindow )
553 {
554 wxWindow *currentMouseWindowParent = currentMouseWindow->GetParent();
555
556 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
557
558 wxevent.SetEventObject( currentMouseWindow ) ;
559 wxevent.SetId( currentMouseWindow->GetId() ) ;
560
561 // make tooltips current
562
563 #if wxUSE_TOOLTIPS
564 if ( wxevent.GetEventType() == wxEVT_MOTION )
565 wxToolTip::RelayEvent( currentMouseWindow , wxevent );
566 #endif
567
568 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
569 {
570 if ((currentMouseWindowParent != NULL) &&
571 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
572 currentMouseWindow = NULL;
573
574 result = noErr;
575 }
576 else
577 {
578 // if the user code did _not_ handle the event, then perform the
579 // default processing
580 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
581 {
582 // ... that is set focus to this window
583 if (currentMouseWindow->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow)
584 currentMouseWindow->SetFocus();
585 }
586
587 ControlPartCode dummyPart ;
588 // if built-in find control is finding the wrong control (ie static box instead of overlaid
589 // button, we cannot let the standard handler do its job, but must handle manually
590
591 if ( ( cEvent.GetKind() == kEventMouseDown )
592 #ifdef __WXMAC_OSX__
593 &&
594 (FindControlUnderMouse(windowMouseLocation , window , &dummyPart) !=
595 wxMacFindControlUnderMouse( toplevelWindow , windowMouseLocation , window , &dummyPart ) )
596 #endif
597 )
598 {
599 if ( currentMouseWindow->MacIsReallyEnabled() )
600 {
601 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
602 Point clickLocation = windowMouseLocation ;
603
604 if ( toplevelWindow->MacUsesCompositing() )
605 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
606
607 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
608 modifiers , (ControlActionUPP ) -1 ) ;
609
610 if ((currentMouseWindowParent != NULL) &&
611 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
612 {
613 currentMouseWindow = NULL;
614 }
615 }
616
617 result = noErr ;
618 }
619 }
620
621 if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
622 {
623 wxApp::s_captureWindow = NULL ;
624 // update cursor ?
625 }
626
627 // update cursor
628
629 wxWindow* cursorTarget = currentMouseWindow ;
630 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
631
632 while ( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
633 {
634 cursorTarget = cursorTarget->GetParent() ;
635 if ( cursorTarget )
636 cursorPoint += cursorTarget->GetPosition();
637 }
638
639 }
640 else // currentMouseWindow == NULL
641 {
642 // don't mess with controls we don't know about
643 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
644 // so we try sending them the correct control directly
645 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
646 {
647 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
648 Point clickLocation = windowMouseLocation ;
649 #if TARGET_API_MAC_OSX
650 if ( toplevelWindow->MacUsesCompositing() )
651 {
652 HIPoint hiPoint ;
653 hiPoint.x = clickLocation.h ;
654 hiPoint.y = clickLocation.v ;
655 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
656 clickLocation.h = (int)hiPoint.x ;
657 clickLocation.v = (int)hiPoint.y ;
658 }
659 #endif // TARGET_API_MAC_OSX
660
661 HandleControlClick( control , clickLocation , modifiers , (ControlActionUPP ) -1 ) ;
662 result = noErr ;
663 }
664 }
665
666 return result ;
667 }
668
669 static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
670 {
671 OSStatus result = eventNotHandledErr ;
672
673 wxMacCarbonEvent cEvent( event ) ;
674
675 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
676 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
677
678 switch ( GetEventKind( event ) )
679 {
680 case kEventWindowActivated :
681 {
682 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
683 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
684 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
685 wxevent.SetEventObject(toplevelWindow);
686 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
687 // we still sending an eventNotHandledErr in order to allow for default processing
688 }
689 break ;
690
691 case kEventWindowDeactivated :
692 {
693 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
694 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
695 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
696 wxevent.SetEventObject(toplevelWindow);
697 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
698 // we still sending an eventNotHandledErr in order to allow for default processing
699 }
700 break ;
701
702 case kEventWindowShown :
703 toplevelWindow->Refresh() ;
704 result = noErr ;
705 break ;
706
707 case kEventWindowClose :
708 toplevelWindow->Close() ;
709 result = noErr ;
710 break ;
711
712 case kEventWindowBoundsChanged :
713 {
714 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes, typeUInt32) ;
715 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
716 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
717 if ( attributes & kWindowBoundsChangeSizeChanged )
718 {
719 // according to the other ports we handle this within the OS level
720 // resize event, not within a wxSizeEvent
721 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
722 if ( frame )
723 {
724 frame->PositionBars();
725 }
726
727 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
728 event.SetEventObject( toplevelWindow ) ;
729
730 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
731 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
732 }
733
734 if ( attributes & kWindowBoundsChangeOriginChanged )
735 {
736 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
737 event.SetEventObject( toplevelWindow ) ;
738 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
739 }
740
741 result = noErr ;
742 }
743 break ;
744
745 case kEventWindowBoundsChanging :
746 {
747 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
748 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
749
750 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
751 {
752 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
753 int left , top , right , bottom ;
754 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
755
756 wxRect r(
757 newRect.left - left,
758 newRect.top - top,
759 newRect.right - newRect.left + left + right,
760 newRect.bottom - newRect.top + top + bottom ) ;
761
762 // this is a EVT_SIZING not a EVT_SIZE type !
763 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
764 wxevent.SetEventObject( toplevelWindow ) ;
765 wxRect adjustR = r ;
766 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
767 adjustR = wxevent.GetRect() ;
768
769 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
770 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
771 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
772 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
773 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
774 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
775 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
776 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
777 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
778 if ( !EqualRect( &newRect , &adjustedRect ) )
779 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
780 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
781 }
782
783 result = noErr ;
784 }
785 break ;
786
787 default :
788 break ;
789 }
790
791 return result ;
792 }
793
794 pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
795 {
796 OSStatus result = eventNotHandledErr ;
797
798 switch ( GetEventClass( event ) )
799 {
800 case kEventClassKeyboard :
801 result = KeyboardEventHandler( handler, event , data ) ;
802 break ;
803
804 case kEventClassWindow :
805 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
806 break ;
807
808 case kEventClassMouse :
809 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
810 break ;
811
812 default :
813 break ;
814 }
815
816 return result ;
817 }
818
819 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
820
821 // ---------------------------------------------------------------------------
822 // wxWindowMac utility functions
823 // ---------------------------------------------------------------------------
824
825 // Find an item given the Macintosh Window Reference
826
827 WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
828
829 static MacWindowMap wxWinMacWindowList;
830
831 wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
832 {
833 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
834
835 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
836 }
837
838 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
839 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
840 {
841 // adding NULL WindowRef is (first) surely a result of an error and
842 // nothing else :-)
843 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
844
845 wxWinMacWindowList[inWindowRef] = win;
846 }
847
848 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
849 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
850 {
851 MacWindowMap::iterator it;
852 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
853 {
854 if ( it->second == win )
855 {
856 wxWinMacWindowList.erase(it);
857 break;
858 }
859 }
860 }
861
862 // ----------------------------------------------------------------------------
863 // wxTopLevelWindowMac creation
864 // ----------------------------------------------------------------------------
865
866 wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
867
868 typedef struct
869 {
870 wxPoint m_position ;
871 wxSize m_size ;
872 bool m_wasResizable ;
873 }
874 FullScreenData ;
875
876 void wxTopLevelWindowMac::Init()
877 {
878 m_iconized =
879 m_maximizeOnShow = false;
880 m_macWindow = NULL ;
881
882 #if TARGET_API_MAC_OSX
883 m_macUsesCompositing = ( UMAGetSystemVersion() >= 0x1030 );
884 #else
885 m_macUsesCompositing = false;
886 #endif
887
888 m_macEventHandler = NULL ;
889 m_macFullScreenData = NULL ;
890 }
891
892 class wxMacDeferredWindowDeleter : public wxObject
893 {
894 public :
895 wxMacDeferredWindowDeleter( WindowRef windowRef )
896 {
897 m_macWindow = windowRef ;
898 }
899
900 virtual ~wxMacDeferredWindowDeleter()
901 {
902 UMADisposeWindow( (WindowRef) m_macWindow ) ;
903 }
904
905 protected :
906 WindowRef m_macWindow ;
907 } ;
908
909 bool wxTopLevelWindowMac::Create(wxWindow *parent,
910 wxWindowID id,
911 const wxString& title,
912 const wxPoint& pos,
913 const wxSize& size,
914 long style,
915 const wxString& name)
916 {
917 // init our fields
918 Init();
919
920 m_windowStyle = style;
921
922 SetName( name );
923
924 m_windowId = id == -1 ? NewControlId() : id;
925 wxWindow::SetLabel( title ) ;
926
927 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
928
929 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
930
931 if (GetExtraStyle() & wxFRAME_EX_METAL)
932 MacSetMetalAppearance(true);
933
934 wxTopLevelWindows.Append(this);
935
936 if ( parent )
937 parent->AddChild(this);
938
939 return true;
940 }
941
942 wxTopLevelWindowMac::~wxTopLevelWindowMac()
943 {
944 if ( m_macWindow )
945 {
946 #if wxUSE_TOOLTIPS
947 wxToolTip::NotifyWindowDelete(m_macWindow) ;
948 #endif
949 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
950 }
951
952 if ( m_macEventHandler )
953 {
954 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
955 m_macEventHandler = NULL ;
956 }
957
958 wxRemoveMacWindowAssociation( this ) ;
959
960 if ( wxModelessWindows.Find(this) )
961 wxModelessWindows.DeleteObject(this);
962
963 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
964 delete data ;
965 m_macFullScreenData = NULL ;
966 }
967
968
969 // ----------------------------------------------------------------------------
970 // wxTopLevelWindowMac maximize/minimize
971 // ----------------------------------------------------------------------------
972
973 void wxTopLevelWindowMac::Maximize(bool maximize)
974 {
975 // TODO: check if this is still necessary
976 #if 0
977 wxMacPortStateHelper help( (GrafPtr)GetWindowPort( (WindowRef)m_macWindow) ) ;
978 wxMacWindowClipper clip( this );
979 #endif
980
981 if ( !IsWindowInStandardState( (WindowRef)m_macWindow, NULL, NULL ) )
982 {
983 Rect rect;
984
985 GetWindowBounds((WindowRef)m_macWindow, kWindowGlobalPortRgn, &rect);
986 SetWindowIdealUserState((WindowRef)m_macWindow, &rect);
987 SetWindowUserState((WindowRef)m_macWindow, &rect);
988 }
989
990 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
991 }
992
993 bool wxTopLevelWindowMac::IsMaximized() const
994 {
995 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
996 }
997
998 void wxTopLevelWindowMac::Iconize(bool iconize)
999 {
1000 if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
1001 CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
1002 }
1003
1004 bool wxTopLevelWindowMac::IsIconized() const
1005 {
1006 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
1007 }
1008
1009 void wxTopLevelWindowMac::Restore()
1010 {
1011 if ( IsMaximized() )
1012 Maximize(false);
1013 else if ( IsIconized() )
1014 Iconize(false);
1015 }
1016
1017 // ----------------------------------------------------------------------------
1018 // wxTopLevelWindowMac misc
1019 // ----------------------------------------------------------------------------
1020
1021 wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1022 {
1023 return wxPoint(0, 0) ;
1024 }
1025
1026 void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
1027 {
1028 // this sets m_icon
1029 wxTopLevelWindowBase::SetIcon(icon);
1030 }
1031
1032 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
1033 {
1034 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1035
1036 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1037 {
1038 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1039 }
1040 }
1041
1042 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
1043 {
1044 if ( m_macEventHandler != NULL )
1045 {
1046 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1047 }
1048
1049 InstallWindowEventHandler(
1050 MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
1051 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler );
1052 }
1053
1054 void wxTopLevelWindowMac::MacCreateRealWindow(
1055 const wxString& title,
1056 const wxPoint& pos,
1057 const wxSize& size,
1058 long style,
1059 const wxString& name )
1060 {
1061 OSStatus err = noErr ;
1062 SetName(name);
1063 m_windowStyle = style;
1064 m_isShown = false;
1065
1066 // create frame.
1067 int x = (int)pos.x;
1068 int y = (int)pos.y;
1069
1070 Rect theBoundsRect;
1071 wxRect display = wxGetClientDisplayRect() ;
1072
1073 if ( x == wxDefaultPosition.x )
1074 x = display.x ;
1075
1076 if ( y == wxDefaultPosition.y )
1077 y = display.y ;
1078
1079 int w = WidthDefault(size.x);
1080 int h = HeightDefault(size.y);
1081
1082 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1083
1084 // translate the window attributes in the appropriate window class and attributes
1085 WindowClass wclass = 0;
1086 WindowAttributes attr = kWindowNoAttributes ;
1087 WindowGroupRef group = NULL ;
1088
1089 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
1090 {
1091 if (
1092 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1093 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1094 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
1095 )
1096 {
1097 wclass = kFloatingWindowClass ;
1098
1099 if ( HasFlag(wxTINY_CAPTION_VERT) )
1100 attr |= kWindowSideTitlebarAttribute ;
1101 }
1102 else
1103 {
1104 wclass = kPlainWindowClass ;
1105 }
1106 }
1107 else if ( HasFlag( wxCAPTION ) )
1108 {
1109 wclass = kDocumentWindowClass ;
1110 attr |= kWindowInWindowMenuAttribute ;
1111 }
1112 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
1113 else if ( HasFlag( wxFRAME_DRAWER ) )
1114 {
1115 wclass = kDrawerWindowClass;
1116 // we must force compositing on a drawer
1117 m_macUsesCompositing = true ;
1118 }
1119 #endif //10.2 and up
1120 else
1121 {
1122 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1123 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
1124 {
1125 wclass = kDocumentWindowClass ;
1126 }
1127 else
1128 {
1129 wclass = kPlainWindowClass ;
1130 }
1131 }
1132
1133 if ( wclass != kPlainWindowClass )
1134 {
1135 if ( HasFlag( wxMINIMIZE_BOX ) )
1136 attr |= kWindowCollapseBoxAttribute ;
1137
1138 if ( HasFlag( wxMAXIMIZE_BOX ) )
1139 attr |= kWindowFullZoomAttribute ;
1140
1141 if ( HasFlag( wxRESIZE_BORDER ) )
1142 attr |= kWindowResizableAttribute ;
1143
1144 if ( HasFlag( wxCLOSE_BOX) )
1145 attr |= kWindowCloseBoxAttribute ;
1146 }
1147
1148 // turn on live resizing (OS X only)
1149 if (UMAGetSystemVersion() >= 0x1000)
1150 attr |= kWindowLiveResizeAttribute;
1151
1152 if ( HasFlag(wxSTAY_ON_TOP) )
1153 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1154
1155 #if TARGET_API_MAC_OSX
1156 if ( m_macUsesCompositing )
1157 attr |= kWindowCompositingAttribute;
1158 #endif
1159
1160 if ( HasFlag(wxFRAME_SHAPED) )
1161 {
1162 WindowDefSpec customWindowDefSpec;
1163 customWindowDefSpec.defType = kWindowDefProcPtr;
1164 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1165
1166 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1167 attr, &theBoundsRect,
1168 (WindowRef*) &m_macWindow);
1169 }
1170 else
1171 {
1172 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1173 }
1174
1175 if ( err == noErr && m_macWindow != NULL && group != NULL )
1176 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1177
1178 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1179
1180 // the create commands are only for content rect,
1181 // so we have to set the size again as structure bounds
1182 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1183
1184 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1185 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1186 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
1187
1188 #if TARGET_API_MAC_OSX
1189 if ( m_macUsesCompositing )
1190 {
1191 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1192 // the content view, so we have to retrieve it explicitly
1193 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1194 m_peer->GetControlRefAddr() ) ;
1195 if ( !m_peer->Ok() )
1196 {
1197 // compatibility mode fallback
1198 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1199 }
1200 }
1201 #endif
1202 {
1203 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
1204 }
1205
1206 // the root control level handler
1207 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1208
1209 #if TARGET_API_MAC_OSX
1210 if ( m_macUsesCompositing && m_macWindow != NULL )
1211 {
1212 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1213 MacSetMetalAppearance( true ) ;
1214 }
1215 #endif
1216
1217 // the frame window event handler
1218 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1219 MacInstallTopLevelWindowEventHandler() ;
1220
1221 DoSetWindowVariant( m_windowVariant ) ;
1222
1223 m_macFocus = NULL ;
1224
1225 if ( HasFlag(wxFRAME_SHAPED) )
1226 {
1227 // default shape matches the window size
1228 wxRegion rgn( 0, 0, w, h );
1229 SetShape( rgn );
1230 }
1231
1232 wxWindowCreateEvent event(this);
1233 GetEventHandler()->ProcessEvent(event);
1234 }
1235
1236 void wxTopLevelWindowMac::ClearBackground()
1237 {
1238 wxWindow::ClearBackground() ;
1239 }
1240
1241 // Raise the window to the top of the Z order
1242 void wxTopLevelWindowMac::Raise()
1243 {
1244 ::SelectWindow( (WindowRef)m_macWindow ) ;
1245 }
1246
1247 // Lower the window to the bottom of the Z order
1248 void wxTopLevelWindowMac::Lower()
1249 {
1250 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1251 }
1252
1253 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1254 {
1255 if (s_macDeactivateWindow)
1256 {
1257 wxLogTrace(TRACE_ACTIVATE,
1258 wxT("Doing delayed deactivation of %p"),
1259 s_macDeactivateWindow);
1260
1261 s_macDeactivateWindow->MacActivate(timestamp, false);
1262 }
1263 }
1264
1265 void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
1266 {
1267 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
1268
1269 if (s_macDeactivateWindow == this)
1270 s_macDeactivateWindow = NULL;
1271
1272 MacDelayedDeactivation(timestamp);
1273 MacPropagateHiliteChanged() ;
1274 }
1275
1276 void wxTopLevelWindowMac::SetTitle(const wxString& title)
1277 {
1278 wxWindow::SetLabel( title ) ;
1279 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
1280 }
1281
1282 wxString wxTopLevelWindowMac::GetTitle() const
1283 {
1284 return wxWindow::GetLabel();
1285 }
1286
1287 bool wxTopLevelWindowMac::Show(bool show)
1288 {
1289 if ( !wxTopLevelWindowBase::Show(show) )
1290 return false;
1291
1292 bool plainTransition = false;
1293
1294 #if wxUSE_SYSTEM_OPTIONS
1295 // code contributed by Ryan Wilcox December 18, 2003
1296 plainTransition = UMAGetSystemVersion() >= 0x1000 ;
1297 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1298 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1299 #endif
1300
1301 if (show)
1302 {
1303 if ( plainTransition )
1304 ::ShowWindow( (WindowRef)m_macWindow );
1305 else
1306 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
1307
1308 ::SelectWindow( (WindowRef)m_macWindow ) ;
1309
1310 // because apps expect a size event to occur at this moment
1311 wxSizeEvent event(GetSize() , m_windowId);
1312 event.SetEventObject(this);
1313 GetEventHandler()->ProcessEvent(event);
1314 }
1315 else
1316 {
1317 if ( plainTransition )
1318 ::HideWindow( (WindowRef)m_macWindow );
1319 else
1320 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
1321 }
1322
1323 MacPropagateVisibilityChanged() ;
1324
1325 return true ;
1326 }
1327
1328 bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
1329 {
1330 if ( show )
1331 {
1332 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1333 delete data ;
1334 data = new FullScreenData() ;
1335
1336 m_macFullScreenData = data ;
1337 data->m_position = GetPosition() ;
1338 data->m_size = GetSize() ;
1339 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
1340
1341 if ( style & wxFULLSCREEN_NOMENUBAR )
1342 HideMenuBar() ;
1343
1344 wxRect client = wxGetClientDisplayRect() ;
1345
1346 int left , top , right , bottom ;
1347 int x, y, w, h ;
1348
1349 x = client.x ;
1350 y = client.y ;
1351 w = client.width ;
1352 h = client.height ;
1353
1354 MacGetContentAreaInset( left , top , right , bottom ) ;
1355
1356 if ( style & wxFULLSCREEN_NOCAPTION )
1357 {
1358 y -= top ;
1359 h += top ;
1360 }
1361
1362 if ( style & wxFULLSCREEN_NOBORDER )
1363 {
1364 x -= left ;
1365 w += left + right ;
1366 h += bottom ;
1367 }
1368
1369 if ( style & wxFULLSCREEN_NOTOOLBAR )
1370 {
1371 // TODO
1372 }
1373
1374 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1375 {
1376 // TODO
1377 }
1378
1379 SetSize( x , y , w, h ) ;
1380 if ( data->m_wasResizable )
1381 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
1382 }
1383 else
1384 {
1385 ShowMenuBar() ;
1386 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1387 if ( data->m_wasResizable )
1388 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
1389 SetPosition( data->m_position ) ;
1390 SetSize( data->m_size ) ;
1391
1392 delete data ;
1393 m_macFullScreenData = NULL ;
1394 }
1395
1396 return false;
1397 }
1398
1399 bool wxTopLevelWindowMac::IsFullScreen() const
1400 {
1401 return m_macFullScreenData != NULL ;
1402 }
1403
1404 void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
1405 {
1406 if ( GetExtraStyle() == exStyle )
1407 return ;
1408
1409 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
1410
1411 #if TARGET_API_MAC_OSX
1412 if ( m_macUsesCompositing && m_macWindow != NULL )
1413 {
1414 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1415 if ( MacGetMetalAppearance() != metal )
1416 MacSetMetalAppearance( metal ) ;
1417 }
1418 #endif
1419 }
1420
1421 // we are still using coordinates of the content view
1422 // TODO: switch to structure bounds
1423 //
1424 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1425 {
1426 Rect content, structure ;
1427
1428 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1429 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1430
1431 left = content.left - structure.left ;
1432 top = content.top - structure.top ;
1433 right = structure.right - content.right ;
1434 bottom = structure.bottom - content.bottom ;
1435 }
1436
1437 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1438 {
1439 m_cachedClippedRectValid = false ;
1440 Rect bounds = { y , x , y + height , x + width } ;
1441 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1442 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1443 }
1444
1445 void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
1446 {
1447 Rect bounds ;
1448
1449 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1450
1451 if (x)
1452 *x = bounds.left ;
1453 if (y)
1454 *y = bounds.top ;
1455 }
1456
1457 void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1458 {
1459 Rect bounds ;
1460
1461 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1462
1463 if (width)
1464 *width = bounds.right - bounds.left ;
1465 if (height)
1466 *height = bounds.bottom - bounds.top ;
1467 }
1468
1469 void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1470 {
1471 Rect bounds ;
1472
1473 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1474
1475 if (width)
1476 *width = bounds.right - bounds.left ;
1477 if (height)
1478 *height = bounds.bottom - bounds.top ;
1479 }
1480
1481 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1482 {
1483 #if TARGET_API_MAC_OSX
1484 wxASSERT_MSG( m_macUsesCompositing ,
1485 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1486
1487 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1488 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1489 #endif
1490 }
1491
1492 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1493 {
1494 #if TARGET_API_MAC_OSX
1495 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1496 #else
1497 return false ;
1498 #endif
1499 }
1500
1501 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1502 {
1503 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
1504 }
1505
1506 wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1507 {
1508 UInt32 attr = 0 ;
1509 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1510
1511 return attr ;
1512 }
1513
1514 void wxTopLevelWindowMac::MacPerformUpdates()
1515 {
1516 #if TARGET_API_MAC_OSX
1517 if ( m_macUsesCompositing )
1518 {
1519 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1520 // for composited windows this also triggers a redraw of all
1521 // invalid views in the window
1522 if ( UMAGetSystemVersion() >= 0x1030 )
1523 HIWindowFlush((WindowRef) m_macWindow) ;
1524 else
1525 #endif
1526 {
1527 // the only way to trigger the redrawing on earlier systems is to call
1528 // ReceiveNextEvent
1529
1530 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
1531 UInt32 currentEventClass = 0 ;
1532 UInt32 currentEventKind = 0 ;
1533 if ( currentEvent != NULL )
1534 {
1535 currentEventClass = ::GetEventClass( currentEvent ) ;
1536 currentEventKind = ::GetEventKind( currentEvent ) ;
1537 }
1538
1539 if ( currentEventClass != kEventClassMenu )
1540 {
1541 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
1542 EventRef theEvent;
1543 OSStatus status = noErr ;
1544 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
1545 }
1546 }
1547 }
1548 else
1549 #endif
1550 {
1551 BeginUpdate( (WindowRef) m_macWindow ) ;
1552
1553 RgnHandle updateRgn = NewRgn();
1554 if ( updateRgn )
1555 {
1556 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
1557 UpdateControls( (WindowRef)m_macWindow , updateRgn ) ;
1558
1559 // if ( !EmptyRgn( updateRgn ) )
1560 // MacDoRedraw( updateRgn , 0 , true) ;
1561
1562 DisposeRgn( updateRgn );
1563 }
1564
1565 EndUpdate( (WindowRef)m_macWindow ) ;
1566 QDFlushPortBuffer( GetWindowPort( (WindowRef)m_macWindow ) , NULL ) ;
1567 }
1568 }
1569
1570 // Attracts the users attention to this window if the application is
1571 // inactive (should be called when a background event occurs)
1572
1573 static pascal void wxMacNMResponse( NMRecPtr ptr )
1574 {
1575 NMRemove( ptr ) ;
1576 DisposePtr( (Ptr)ptr ) ;
1577 }
1578
1579 void wxTopLevelWindowMac::RequestUserAttention(int flags )
1580 {
1581 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
1582 static wxMacNMUPP nmupp( wxMacNMResponse );
1583
1584 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1585 notificationRequest->qType = nmType ;
1586 notificationRequest->nmMark = 1 ;
1587 notificationRequest->nmIcon = 0 ;
1588 notificationRequest->nmSound = 0 ;
1589 notificationRequest->nmStr = NULL ;
1590 notificationRequest->nmResp = nmupp ;
1591
1592 verify_noerr( NMInstall( notificationRequest ) ) ;
1593 }
1594
1595 // ---------------------------------------------------------------------------
1596 // Shape implementation
1597 // ---------------------------------------------------------------------------
1598
1599
1600 bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1601 {
1602 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
1603 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1604
1605 // The empty region signifies that the shape
1606 // should be removed from the window.
1607 if ( region.IsEmpty() )
1608 {
1609 wxSize sz = GetClientSize();
1610 wxRegion rgn(0, 0, sz.x, sz.y);
1611 if ( rgn.IsEmpty() )
1612 return false ;
1613 else
1614 return SetShape(rgn);
1615 }
1616
1617 // Make a copy of the region
1618 RgnHandle shapeRegion = NewRgn();
1619 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1620
1621 // Dispose of any shape region we may already have
1622 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1623 if ( oldRgn )
1624 DisposeRgn(oldRgn);
1625
1626 // Save the region so we can use it later
1627 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1628
1629 // inform the window manager that the window has changed shape
1630 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1631
1632 return true;
1633 }
1634
1635 // ---------------------------------------------------------------------------
1636 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1637 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1638 // ---------------------------------------------------------------------------
1639
1640 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1641 {
1642 GetWindowPortBounds(window, inRect);
1643 Point pt = { inRect->left, inRect->top };
1644
1645 QDLocalToGlobalPoint( GetWindowPort(window), &pt ) ;
1646 inRect->top = pt.v;
1647 inRect->left = pt.h;
1648 inRect->bottom += pt.v;
1649 inRect->right += pt.h;
1650 }
1651
1652 static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1653 {
1654 /*------------------------------------------------------
1655 Define which options your custom window supports.
1656 --------------------------------------------------------*/
1657 //just enable everything for our demo
1658 *(OptionBits*)param =
1659 //kWindowCanGrow |
1660 //kWindowCanZoom |
1661 //kWindowCanCollapse |
1662 //kWindowCanGetWindowRegion |
1663 //kWindowHasTitleBar |
1664 //kWindowSupportsDragHilite |
1665 kWindowCanDrawInCurrentPort |
1666 //kWindowCanMeasureTitle |
1667 kWindowWantsDisposeAtProcessDeath |
1668 kWindowSupportsGetGrowImageRegion |
1669 kWindowDefSupportsColorGrafPort;
1670
1671 return 1;
1672 }
1673
1674 // The content region is left as a rectangle matching the window size, this is
1675 // so the origin in the paint event, and etc. still matches what the
1676 // programmer expects.
1677 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1678 {
1679 SetEmptyRgn(rgn);
1680 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1681 if (win)
1682 {
1683 Rect r ;
1684 wxShapedMacWindowGetPos( window, &r ) ;
1685 RectRgn( rgn , &r ) ;
1686 }
1687 }
1688
1689 // The structure region is set to the shape given to the SetShape method.
1690 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1691 {
1692 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1693
1694 SetEmptyRgn(rgn);
1695 if (cachedRegion)
1696 {
1697 Rect windowRect;
1698 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1699 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
1700 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1701 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1702 }
1703 }
1704
1705 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1706 {
1707 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1708
1709 if (rgnRec == NULL)
1710 return paramErr;
1711
1712 switch (rgnRec->regionCode)
1713 {
1714 case kWindowStructureRgn:
1715 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1716 break;
1717
1718 case kWindowContentRgn:
1719 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1720 break;
1721
1722 default:
1723 SetEmptyRgn(rgnRec->winRgn);
1724 break;
1725 }
1726
1727 return noErr;
1728 }
1729
1730 // Determine the region of the window which was hit
1731 //
1732 static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
1733 {
1734 Point hitPoint;
1735 static RgnHandle tempRgn = NULL;
1736
1737 if (tempRgn == NULL)
1738 tempRgn = NewRgn();
1739
1740 // get the point clicked
1741 SetPt( &hitPoint, LoWord(param), HiWord(param) );
1742
1743 // Mac OS 8.5 or later
1744 wxShapedMacWindowStructureRegion(window, tempRgn);
1745 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
1746 return wInContent;
1747
1748 // no significant area was hit
1749 return wNoHit;
1750 }
1751
1752 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1753 {
1754 switch (message)
1755 {
1756 case kWindowMsgHitTest:
1757 return wxShapedMacWindowHitTest(window, param);
1758
1759 case kWindowMsgGetFeatures:
1760 return wxShapedMacWindowGetFeatures(window, param);
1761
1762 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1763 case kWindowMsgGetRegion:
1764 return wxShapedMacWindowGetRegion(window, param);
1765
1766 default:
1767 break;
1768 }
1769
1770 return 0;
1771 }
1772