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