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