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