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