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