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