]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
added our own implementation of strto[u]ll() if the system doesn't have one (patch...
[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->CanAcceptFocus() && 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::MacSetBackgroundBrush( const wxBrush &brush )
1035 {
1036 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1037
1038 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1039 {
1040 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1041 }
1042 }
1043
1044 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
1045 {
1046 if ( m_macEventHandler != NULL )
1047 {
1048 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1049 }
1050
1051 InstallWindowEventHandler(
1052 MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
1053 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler );
1054 }
1055
1056 void wxTopLevelWindowMac::MacCreateRealWindow(
1057 const wxString& title,
1058 const wxPoint& pos,
1059 const wxSize& size,
1060 long style,
1061 const wxString& name )
1062 {
1063 OSStatus err = noErr ;
1064 SetName(name);
1065 m_windowStyle = style;
1066 m_isShown = false;
1067
1068 // create frame.
1069 int x = (int)pos.x;
1070 int y = (int)pos.y;
1071
1072 Rect theBoundsRect;
1073 wxRect display = wxGetClientDisplayRect() ;
1074
1075 if ( x == wxDefaultPosition.x )
1076 x = display.x ;
1077
1078 if ( y == wxDefaultPosition.y )
1079 y = display.y ;
1080
1081 int w = WidthDefault(size.x);
1082 int h = HeightDefault(size.y);
1083
1084 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1085
1086 // translate the window attributes in the appropriate window class and attributes
1087 WindowClass wclass = 0;
1088 WindowAttributes attr = kWindowNoAttributes ;
1089 WindowGroupRef group = NULL ;
1090
1091 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
1092 {
1093 if (
1094 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1095 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1096 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
1097 )
1098 {
1099 wclass = kFloatingWindowClass ;
1100
1101 if ( HasFlag(wxTINY_CAPTION_VERT) )
1102 attr |= kWindowSideTitlebarAttribute ;
1103 }
1104 else
1105 {
1106 wclass = kPlainWindowClass ;
1107 }
1108 }
1109 else if ( HasFlag( wxPOPUP_WINDOW ) )
1110 {
1111 // TEMPORARY HACK!
1112 // Until we've got a real wxPopupWindow class on wxMac make it a
1113 // little easier for wxFrame to be used to emulate it and workaround
1114 // the lack of wxPopupWindow.
1115 if ( HasFlag( wxBORDER_NONE ) )
1116 wclass = kHelpWindowClass ; // has no border
1117 else
1118 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
1119 //attr |= kWindowNoShadowAttribute; // turn off the shadow Should we??
1120 group = GetWindowGroupOfClass( // float above other windows
1121 kFloatingWindowClass) ;
1122 }
1123 else if ( HasFlag( wxCAPTION ) )
1124 {
1125 wclass = kDocumentWindowClass ;
1126 attr |= kWindowInWindowMenuAttribute ;
1127 }
1128 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
1129 else if ( HasFlag( wxFRAME_DRAWER ) )
1130 {
1131 wclass = kDrawerWindowClass;
1132 }
1133 #endif //10.2 and up
1134 else
1135 {
1136 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1137 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
1138 {
1139 wclass = kDocumentWindowClass ;
1140 }
1141 else
1142 {
1143 wclass = kPlainWindowClass ;
1144 }
1145 }
1146
1147 if ( wclass != kPlainWindowClass )
1148 {
1149 if ( HasFlag( wxMINIMIZE_BOX ) )
1150 attr |= kWindowCollapseBoxAttribute ;
1151
1152 if ( HasFlag( wxMAXIMIZE_BOX ) )
1153 attr |= kWindowFullZoomAttribute ;
1154
1155 if ( HasFlag( wxRESIZE_BORDER ) )
1156 attr |= kWindowResizableAttribute ;
1157
1158 if ( HasFlag( wxCLOSE_BOX) )
1159 attr |= kWindowCloseBoxAttribute ;
1160 }
1161
1162 // turn on live resizing (OS X only)
1163 if (UMAGetSystemVersion() >= 0x1000)
1164 attr |= kWindowLiveResizeAttribute;
1165
1166 if ( HasFlag(wxSTAY_ON_TOP) )
1167 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1168
1169 if ( HasFlag( wxFRAME_FLOAT_ON_PARENT ) )
1170 group = GetWindowGroupOfClass(kFloatingWindowClass) ;
1171
1172 attr |= kWindowCompositingAttribute;
1173 #if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1174 attr |= kWindowFrameworkScaledAttribute;
1175 #endif
1176
1177 if ( HasFlag(wxFRAME_SHAPED) )
1178 {
1179 WindowDefSpec customWindowDefSpec;
1180 customWindowDefSpec.defType = kWindowDefProcPtr;
1181 customWindowDefSpec.u.defProc =
1182 #ifdef __LP64__
1183 (WindowDefUPP) wxShapedMacWindowDef;
1184 #else
1185 NewWindowDefUPP(wxShapedMacWindowDef);
1186 #endif
1187 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1188 attr, &theBoundsRect,
1189 (WindowRef*) &m_macWindow);
1190 }
1191 else
1192 {
1193 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1194 }
1195
1196 if ( err == noErr && m_macWindow != NULL && group != NULL )
1197 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1198
1199 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1200
1201 // setup a separate group for each window, so that overlays can be handled easily
1202 verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrHideOnCollapse, &group ));
1203 verify_noerr( SetWindowGroupParent( group, GetWindowGroup( (WindowRef) m_macWindow )));
1204 verify_noerr( SetWindowGroup( (WindowRef) m_macWindow , group ));
1205
1206 // the create commands are only for content rect,
1207 // so we have to set the size again as structure bounds
1208 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1209
1210 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1211 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1212 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
1213
1214 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1215 // the content view, so we have to retrieve it explicitly
1216 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1217 m_peer->GetControlRefAddr() ) ;
1218 if ( !m_peer->Ok() )
1219 {
1220 // compatibility mode fallback
1221 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1222 }
1223
1224 // the root control level handler
1225 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1226
1227 // Causes the inner part of the window not to be metal
1228 // if the style is used before window creation.
1229 #if 0 // TARGET_API_MAC_OSX
1230 if ( m_macUsesCompositing && m_macWindow != NULL )
1231 {
1232 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1233 MacSetMetalAppearance( true ) ;
1234 }
1235 #endif
1236
1237 // the frame window event handler
1238 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1239 MacInstallTopLevelWindowEventHandler() ;
1240
1241 DoSetWindowVariant( m_windowVariant ) ;
1242
1243 m_macFocus = NULL ;
1244
1245 if ( HasFlag(wxFRAME_SHAPED) )
1246 {
1247 // default shape matches the window size
1248 wxRegion rgn( 0, 0, w, h );
1249 SetShape( rgn );
1250 }
1251
1252 wxWindowCreateEvent event(this);
1253 GetEventHandler()->ProcessEvent(event);
1254 }
1255
1256 void wxTopLevelWindowMac::ClearBackground()
1257 {
1258 wxWindow::ClearBackground() ;
1259 }
1260
1261 // Raise the window to the top of the Z order
1262 void wxTopLevelWindowMac::Raise()
1263 {
1264 ::SelectWindow( (WindowRef)m_macWindow ) ;
1265 }
1266
1267 // Lower the window to the bottom of the Z order
1268 void wxTopLevelWindowMac::Lower()
1269 {
1270 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1271 }
1272
1273 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1274 {
1275 if (s_macDeactivateWindow)
1276 {
1277 wxLogTrace(TRACE_ACTIVATE,
1278 wxT("Doing delayed deactivation of %p"),
1279 s_macDeactivateWindow);
1280
1281 s_macDeactivateWindow->MacActivate(timestamp, false);
1282 }
1283 }
1284
1285 void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
1286 {
1287 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
1288
1289 if (s_macDeactivateWindow == this)
1290 s_macDeactivateWindow = NULL;
1291
1292 MacDelayedDeactivation(timestamp);
1293 MacPropagateHiliteChanged() ;
1294 }
1295
1296 void wxTopLevelWindowMac::SetTitle(const wxString& title)
1297 {
1298 wxWindow::SetLabel( title ) ;
1299 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
1300 }
1301
1302 wxString wxTopLevelWindowMac::GetTitle() const
1303 {
1304 return wxWindow::GetLabel();
1305 }
1306
1307 bool wxTopLevelWindowMac::Show(bool show)
1308 {
1309 if ( !wxTopLevelWindowBase::Show(show) )
1310 return false;
1311
1312 bool plainTransition = false;
1313
1314 #if wxUSE_SYSTEM_OPTIONS
1315 // code contributed by Ryan Wilcox December 18, 2003
1316 plainTransition = UMAGetSystemVersion() >= 0x1000 ;
1317 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1318 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1319 #endif
1320
1321 if (show)
1322 {
1323 if ( plainTransition )
1324 ::ShowWindow( (WindowRef)m_macWindow );
1325 else
1326 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
1327
1328 ::SelectWindow( (WindowRef)m_macWindow ) ;
1329
1330 // because apps expect a size event to occur at this moment
1331 wxSizeEvent event(GetSize() , m_windowId);
1332 event.SetEventObject(this);
1333 GetEventHandler()->ProcessEvent(event);
1334 }
1335 else
1336 {
1337 if ( plainTransition )
1338 ::HideWindow( (WindowRef)m_macWindow );
1339 else
1340 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
1341 }
1342
1343 MacPropagateVisibilityChanged() ;
1344
1345 return true ;
1346 }
1347
1348 bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
1349 {
1350 if ( show )
1351 {
1352 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1353 delete data ;
1354 data = new FullScreenData() ;
1355
1356 m_macFullScreenData = data ;
1357 data->m_position = GetPosition() ;
1358 data->m_size = GetSize() ;
1359 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
1360
1361 if ( style & wxFULLSCREEN_NOMENUBAR )
1362 HideMenuBar() ;
1363
1364 wxRect client = wxGetClientDisplayRect() ;
1365
1366 int left , top , right , bottom ;
1367 int x, y, w, h ;
1368
1369 x = client.x ;
1370 y = client.y ;
1371 w = client.width ;
1372 h = client.height ;
1373
1374 MacGetContentAreaInset( left , top , right , bottom ) ;
1375
1376 if ( style & wxFULLSCREEN_NOCAPTION )
1377 {
1378 y -= top ;
1379 h += top ;
1380 }
1381
1382 if ( style & wxFULLSCREEN_NOBORDER )
1383 {
1384 x -= left ;
1385 w += left + right ;
1386 h += bottom ;
1387 }
1388
1389 if ( style & wxFULLSCREEN_NOTOOLBAR )
1390 {
1391 // TODO
1392 }
1393
1394 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1395 {
1396 // TODO
1397 }
1398
1399 SetSize( x , y , w, h ) ;
1400 if ( data->m_wasResizable )
1401 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
1402 }
1403 else
1404 {
1405 ShowMenuBar() ;
1406 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1407 if ( data->m_wasResizable )
1408 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
1409 SetPosition( data->m_position ) ;
1410 SetSize( data->m_size ) ;
1411
1412 delete data ;
1413 m_macFullScreenData = NULL ;
1414 }
1415
1416 return false;
1417 }
1418
1419 bool wxTopLevelWindowMac::IsFullScreen() const
1420 {
1421 return m_macFullScreenData != NULL ;
1422 }
1423
1424
1425 bool wxTopLevelWindowMac::SetTransparent(wxByte alpha)
1426 {
1427 OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, float(alpha)/255.0);
1428 return result == noErr;
1429 }
1430
1431
1432 bool wxTopLevelWindowMac::CanSetTransparent()
1433 {
1434 return true;
1435 }
1436
1437
1438 void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
1439 {
1440 if ( GetExtraStyle() == exStyle )
1441 return ;
1442
1443 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
1444
1445 #if TARGET_API_MAC_OSX
1446 if ( m_macWindow != NULL )
1447 {
1448 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1449 if ( MacGetMetalAppearance() != metal )
1450 MacSetMetalAppearance( metal ) ;
1451 }
1452 #endif
1453 }
1454
1455 // TODO: switch to structure bounds -
1456 // we are still using coordinates of the content view
1457 //
1458 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1459 {
1460 Rect content, structure ;
1461
1462 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1463 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1464
1465 left = content.left - structure.left ;
1466 top = content.top - structure.top ;
1467 right = structure.right - content.right ;
1468 bottom = structure.bottom - content.bottom ;
1469 }
1470
1471 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1472 {
1473 m_cachedClippedRectValid = false ;
1474 Rect bounds = { y , x , y + height , x + width } ;
1475 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1476 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1477 }
1478
1479 void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
1480 {
1481 Rect bounds ;
1482
1483 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1484
1485 if (x)
1486 *x = bounds.left ;
1487 if (y)
1488 *y = bounds.top ;
1489 }
1490
1491 void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1492 {
1493 Rect bounds ;
1494
1495 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1496
1497 if (width)
1498 *width = bounds.right - bounds.left ;
1499 if (height)
1500 *height = bounds.bottom - bounds.top ;
1501 }
1502
1503 void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1504 {
1505 Rect bounds ;
1506
1507 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1508
1509 if (width)
1510 *width = bounds.right - bounds.left ;
1511 if (height)
1512 *height = bounds.bottom - bounds.top ;
1513 }
1514
1515 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1516 {
1517 #if TARGET_API_MAC_OSX
1518 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1519 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1520 #endif
1521 }
1522
1523 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1524 {
1525 #if TARGET_API_MAC_OSX
1526 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1527 #else
1528 return false ;
1529 #endif
1530 }
1531
1532 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1533 {
1534 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
1535 }
1536
1537 wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1538 {
1539 UInt32 attr = 0 ;
1540 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1541
1542 return attr ;
1543 }
1544
1545 void wxTopLevelWindowMac::MacPerformUpdates()
1546 {
1547 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1548 // for composited windows this also triggers a redraw of all
1549 // invalid views in the window
1550 if ( UMAGetSystemVersion() >= 0x1030 )
1551 HIWindowFlush((WindowRef) m_macWindow) ;
1552 else
1553 #endif
1554 {
1555 // the only way to trigger the redrawing on earlier systems is to call
1556 // ReceiveNextEvent
1557
1558 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
1559 UInt32 currentEventClass = 0 ;
1560 if ( currentEvent != NULL )
1561 {
1562 currentEventClass = ::GetEventClass( currentEvent ) ;
1563 ::GetEventKind( currentEvent ) ;
1564 }
1565
1566 if ( currentEventClass != kEventClassMenu )
1567 {
1568 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
1569 EventRef theEvent;
1570 ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
1571 }
1572 }
1573 }
1574
1575 // Attracts the users attention to this window if the application is
1576 // inactive (should be called when a background event occurs)
1577
1578 static pascal void wxMacNMResponse( NMRecPtr ptr )
1579 {
1580 NMRemove( ptr ) ;
1581 DisposePtr( (Ptr)ptr ) ;
1582 }
1583
1584 void wxTopLevelWindowMac::RequestUserAttention(int flags )
1585 {
1586 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
1587 static wxMacNMUPP nmupp( wxMacNMResponse );
1588
1589 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1590 notificationRequest->qType = nmType ;
1591 notificationRequest->nmMark = 1 ;
1592 notificationRequest->nmIcon = 0 ;
1593 notificationRequest->nmSound = 0 ;
1594 notificationRequest->nmStr = NULL ;
1595 notificationRequest->nmResp = nmupp ;
1596
1597 verify_noerr( NMInstall( notificationRequest ) ) ;
1598 }
1599
1600 // ---------------------------------------------------------------------------
1601 // Shape implementation
1602 // ---------------------------------------------------------------------------
1603
1604
1605 bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1606 {
1607 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
1608 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1609
1610 // The empty region signifies that the shape
1611 // should be removed from the window.
1612 if ( region.IsEmpty() )
1613 {
1614 wxSize sz = GetClientSize();
1615 wxRegion rgn(0, 0, sz.x, sz.y);
1616 if ( rgn.IsEmpty() )
1617 return false ;
1618 else
1619 return SetShape(rgn);
1620 }
1621
1622 // Make a copy of the region
1623 RgnHandle shapeRegion = NewRgn();
1624 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1625
1626 // Dispose of any shape region we may already have
1627 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1628 if ( oldRgn )
1629 DisposeRgn(oldRgn);
1630
1631 // Save the region so we can use it later
1632 SetWRefCon((WindowRef)MacGetWindowRef(), (URefCon)shapeRegion);
1633
1634 // inform the window manager that the window has changed shape
1635 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1636
1637 return true;
1638 }
1639
1640 // ---------------------------------------------------------------------------
1641 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1642 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1643 // ---------------------------------------------------------------------------
1644
1645 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1646 {
1647 GetWindowPortBounds(window, inRect);
1648 Point pt = { inRect->top ,inRect->left };
1649 wxMacLocalToGlobal( window, &pt ) ;
1650 inRect->bottom += pt.v - inRect->top;
1651 inRect->right += pt.h - inRect->left;
1652 inRect->top = pt.v;
1653 inRect->left = pt.h;
1654 }
1655
1656 static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1657 {
1658 /*------------------------------------------------------
1659 Define which options your custom window supports.
1660 --------------------------------------------------------*/
1661 //just enable everything for our demo
1662 *(OptionBits*)param =
1663 //kWindowCanGrow |
1664 //kWindowCanZoom |
1665 //kWindowCanCollapse |
1666 //kWindowCanGetWindowRegion |
1667 //kWindowHasTitleBar |
1668 //kWindowSupportsDragHilite |
1669 kWindowCanDrawInCurrentPort |
1670 //kWindowCanMeasureTitle |
1671 kWindowWantsDisposeAtProcessDeath |
1672 kWindowSupportsGetGrowImageRegion |
1673 kWindowDefSupportsColorGrafPort;
1674
1675 return 1;
1676 }
1677
1678 // The content region is left as a rectangle matching the window size, this is
1679 // so the origin in the paint event, and etc. still matches what the
1680 // programmer expects.
1681 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1682 {
1683 SetEmptyRgn(rgn);
1684 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1685 if (win)
1686 {
1687 Rect r ;
1688 wxShapedMacWindowGetPos( window, &r ) ;
1689 RectRgn( rgn , &r ) ;
1690 }
1691 }
1692
1693 // The structure region is set to the shape given to the SetShape method.
1694 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1695 {
1696 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1697
1698 SetEmptyRgn(rgn);
1699 if (cachedRegion)
1700 {
1701 Rect windowRect;
1702 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1703 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
1704 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1705 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1706 }
1707 }
1708
1709 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1710 {
1711 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1712
1713 if (rgnRec == NULL)
1714 return paramErr;
1715
1716 switch (rgnRec->regionCode)
1717 {
1718 case kWindowStructureRgn:
1719 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1720 break;
1721
1722 case kWindowContentRgn:
1723 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1724 break;
1725
1726 default:
1727 SetEmptyRgn(rgnRec->winRgn);
1728 break;
1729 }
1730
1731 return noErr;
1732 }
1733
1734 // Determine the region of the window which was hit
1735 //
1736 static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
1737 {
1738 Point hitPoint;
1739 static RgnHandle tempRgn = NULL;
1740
1741 if (tempRgn == NULL)
1742 tempRgn = NewRgn();
1743
1744 // get the point clicked
1745 SetPt( &hitPoint, LoWord(param), HiWord(param) );
1746
1747 // Mac OS 8.5 or later
1748 wxShapedMacWindowStructureRegion(window, tempRgn);
1749 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
1750 return wInContent;
1751
1752 // no significant area was hit
1753 return wNoHit;
1754 }
1755
1756 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1757 {
1758 switch (message)
1759 {
1760 case kWindowMsgHitTest:
1761 return wxShapedMacWindowHitTest(window, param);
1762
1763 case kWindowMsgGetFeatures:
1764 return wxShapedMacWindowGetFeatures(window, param);
1765
1766 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1767 case kWindowMsgGetRegion:
1768 return wxShapedMacWindowGetRegion(window, param);
1769
1770 default:
1771 break;
1772 }
1773
1774 return 0;
1775 }