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