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