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