1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for Mac
4 // Author: Stefan Csomor
8 // Copyright: (c) 2001-2004 Stefan Csomor
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
35 #include "wx/string.h"
38 #include "wx/settings.h"
41 #include "wx/mac/uma.h"
42 #include "wx/mac/aga.h"
44 #include "wx/tooltip.h"
46 #if wxUSE_SYSTEM_OPTIONS
47 #include "wx/sysopt.h"
50 #include <ToolUtils.h>
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // list of all frames and modeless dialogs
57 wxWindowList wxModelessWindows
;
59 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
61 // ============================================================================
62 // wxTopLevelWindowMac implementation
63 // ============================================================================
65 BEGIN_EVENT_TABLE(wxTopLevelWindowMac
, wxTopLevelWindowBase
)
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
75 static const EventTypeSpec eventList
[] =
77 // TODO remove control related event like key and mouse (except for WindowLeave events)
79 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
81 { kEventClassKeyboard
, kEventRawKeyDown
} ,
82 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
83 { kEventClassKeyboard
, kEventRawKeyUp
} ,
84 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
87 { kEventClassWindow
, kEventWindowShown
} ,
88 { kEventClassWindow
, kEventWindowActivated
} ,
89 { kEventClassWindow
, kEventWindowDeactivated
} ,
90 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
91 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
92 { kEventClassWindow
, kEventWindowClose
} ,
94 // we have to catch these events on the toplevel window level, as controls don't get the
95 // raw mouse events anymore
97 { kEventClassMouse
, kEventMouseDown
} ,
98 { kEventClassMouse
, kEventMouseUp
} ,
99 { kEventClassMouse
, kEventMouseWheelMoved
} ,
100 { kEventClassMouse
, kEventMouseMoved
} ,
101 { kEventClassMouse
, kEventMouseDragged
} ,
104 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
106 OSStatus result
= eventNotHandledErr
;
108 wxWindow
* focus
= wxWindow::FindFocus() ;
116 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
118 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
119 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
120 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
121 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
122 sizeof( Point
), NULL
, &point
);
124 switch ( GetEventKind( event
) )
126 case kEventTextInputUnicodeForKeyEvent
:
127 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
128 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
129 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
132 ControlRef macControl
= (ControlRef
) control
->GetHandle() ;
135 ::HandleControlKey( macControl
, keyCode
, charCode
, modifiers
) ;
140 // this may lead to double events sent to a window in case all handlers have skipped the key down event
141 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
142 UInt32 message = (keyCode << 8) + charCode;
144 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
145 focus , message , modifiers , when , point.h , point.v ) )
156 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
158 OSStatus result
= eventNotHandledErr
;
160 wxWindow
* focus
= wxWindow::FindFocus() ;
165 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
167 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
168 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
169 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
170 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
171 sizeof( Point
), NULL
, &point
);
173 UInt32 message
= (keyCode
<< 8) + charCode
;
174 switch( GetEventKind( event
) )
176 case kEventRawKeyRepeat
:
177 case kEventRawKeyDown
:
179 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
180 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
181 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
182 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
183 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
187 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
190 case kEventRawKeyUp
:
191 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
192 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
197 case kEventRawKeyModifiersChanged
:
199 wxKeyEvent
event(wxEVT_KEY_DOWN
);
201 event
.m_shiftDown
= modifiers
& shiftKey
;
202 event
.m_controlDown
= modifiers
& controlKey
;
203 event
.m_altDown
= modifiers
& optionKey
;
204 event
.m_metaDown
= modifiers
& cmdKey
;
208 event
.m_timeStamp
= when
;
209 wxWindow
* focus
= wxWindow::FindFocus() ;
210 event
.SetEventObject(focus
);
212 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
214 event
.m_keyCode
= WXK_CONTROL
;
215 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
216 focus
->GetEventHandler()->ProcessEvent( event
) ;
218 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
220 event
.m_keyCode
= WXK_SHIFT
;
221 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
222 focus
->GetEventHandler()->ProcessEvent( event
) ;
224 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
226 event
.m_keyCode
= WXK_ALT
;
227 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
228 focus
->GetEventHandler()->ProcessEvent( event
) ;
230 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
232 event
.m_keyCode
= WXK_COMMAND
;
233 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
234 focus
->GetEventHandler()->ProcessEvent( event
) ;
236 wxTheApp
->s_lastModifiers
= modifiers
;
244 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
245 // for windows that we didn't create (like eg Scrollbars in a databrowser) , or for controls where we did not handle the
248 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
250 wxWindow
* g_MacLastWindow
= NULL
;
252 static void SetupMouseEvent( wxMouseEvent
&wxevent
, wxMacCarbonEvent
&cEvent
)
254 UInt32 modifiers
= cEvent
.GetParameter
<UInt32
>(kEventParamKeyModifiers
, typeUInt32
) ;
255 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
257 // this parameter are not given for all events
258 EventMouseButton button
= 0 ;
259 UInt32 clickCount
= 0 ;
260 cEvent
.GetParameter
<EventMouseButton
>(kEventParamMouseButton
, typeMouseButton
, &button
) ;
261 cEvent
.GetParameter
<UInt32
>(kEventParamClickCount
, typeUInt32
, &clickCount
) ;
263 wxevent
.m_x
= screenMouseLocation
.h
;
264 wxevent
.m_y
= screenMouseLocation
.v
;
265 wxevent
.m_shiftDown
= modifiers
& shiftKey
;
266 wxevent
.m_controlDown
= modifiers
& controlKey
;
267 wxevent
.m_altDown
= modifiers
& optionKey
;
268 wxevent
.m_metaDown
= modifiers
& cmdKey
;
269 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
270 // a control click is interpreted as a right click
271 if ( button
== kEventMouseButtonPrimary
&& (modifiers
& controlKey
) )
273 button
= kEventMouseButtonSecondary
;
276 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
278 if ( button
!= 0 && cEvent
.GetKind() != kEventMouseUp
)
282 case kEventMouseButtonPrimary
:
283 wxevent
.m_leftDown
= true ;
285 case kEventMouseButtonSecondary
:
286 wxevent
.m_rightDown
= true ;
288 case kEventMouseButtonTertiary
:
289 wxevent
.m_middleDown
= true ;
293 // determinate the correct click button
294 if ( button
== kEventMouseButtonSecondary
)
296 if (cEvent
.GetKind() == kEventMouseDown
)
297 wxevent
.SetEventType( clickCount
> 1 ? wxEVT_RIGHT_DOWN
: wxEVT_RIGHT_DCLICK
) ;
298 else if ( cEvent
.GetKind() == kEventMouseUp
)
299 wxevent
.SetEventType(wxEVT_RIGHT_UP
) ;
301 else if ( button
== kEventMouseButtonTertiary
)
303 if (cEvent
.GetKind() == kEventMouseDown
)
304 wxevent
.SetEventType(clickCount
> 1 ? wxEVT_MIDDLE_DCLICK
: wxEVT_MIDDLE_DOWN
) ;
305 else if ( cEvent
.GetKind() == kEventMouseUp
)
306 wxevent
.SetEventType(wxEVT_MIDDLE_UP
) ;
310 if (cEvent
.GetKind() == kEventMouseDown
)
311 wxevent
.SetEventType(clickCount
> 1 ? wxEVT_LEFT_DCLICK
: wxEVT_LEFT_DOWN
) ;
312 else if ( cEvent
.GetKind() == kEventMouseUp
)
313 wxevent
.SetEventType(wxEVT_LEFT_UP
) ;
314 else if ( cEvent
.GetKind() == kEventMouseWheelMoved
)
316 wxevent
.SetEventType(wxEVT_MOUSEWHEEL
) ;
318 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
319 SInt32 delta
= cEvent
.GetParameter
<SInt32
>(kEventParamMouseWheelDelta
, typeLongInteger
) ;
321 wxevent
.m_wheelRotation
= delta
;
322 wxevent
.m_wheelDelta
= 1;
323 wxevent
.m_linesPerAction
= 1;
326 wxevent
.SetEventType(wxEVT_MOTION
) ;
330 ControlRef
wxMacFindSubControl( Point location
, ControlRef superControl
, ControlPartCode
*outPart
)
334 UInt16 childrenCount
= 0 ;
335 OSStatus err
= CountSubControls( superControl
, &childrenCount
) ;
336 if ( err
== errControlIsNotEmbedder
)
338 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
340 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
342 ControlHandle sibling
;
343 err
= GetIndexedSubControl( superControl
, i
, & sibling
) ;
344 if ( err
== errControlIsNotEmbedder
)
347 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
348 if ( IsControlVisible( sibling
) )
351 GetControlBounds( sibling
, &r
) ;
352 if ( MacPtInRect( location
, &r
) )
354 ControlHandle child
= wxMacFindSubControl( location
, sibling
, outPart
) ;
359 *outPart
= TestControl( sibling
, location
) ;
369 ControlRef
wxMacFindControlUnderMouse( Point location
, WindowRef window
, ControlPartCode
*outPart
)
371 #if TARGET_API_MAC_OSX
372 return FindControlUnderMouse( location
, window
, outPart
) ;
374 ControlRef rootControl
= NULL
;
375 verify_noerr( GetRootControl( window
, &rootControl
) ) ;
376 return wxMacFindSubControl( location
, rootControl
, outPart
) ;
379 pascal OSStatus
wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
382 OSStatus result
= eventNotHandledErr
;
384 wxMacCarbonEvent
cEvent( event
) ;
386 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
387 Point windowMouseLocation
= screenMouseLocation
;
390 short windowPart
= ::FindWindow(screenMouseLocation
, &window
);
392 wxWindow
* currentMouseWindow
= NULL
;
395 // calculate window relative coordinates
398 ::SetPort( UMAGetWindowPort(window
) ) ;
399 ::GlobalToLocal( &windowMouseLocation
) ;
402 if ( wxTheApp
->s_captureWindow
&& wxTheApp
->s_captureWindow
->MacGetTopLevelWindowRef() == (WXWindow
) window
&& windowPart
== inContent
)
404 currentMouseWindow
= wxTheApp
->s_captureWindow
;
406 else if ( (IsWindowActive(window
) && windowPart
== inContent
) )
408 ControlPartCode part
;
409 ControlRef control
= wxMacFindControlUnderMouse( windowMouseLocation
, window
, &part
) ;
411 currentMouseWindow
= (wxWindow
*) data
;
413 currentMouseWindow
= wxFindControlFromMacControl( control
) ;
417 wxMouseEvent
wxevent(wxEVT_LEFT_DOWN
);
418 SetupMouseEvent( wxevent
, cEvent
) ;
420 // handle all enter / leave events
422 if ( currentMouseWindow
!= g_MacLastWindow
)
424 if ( g_MacLastWindow
)
426 wxMouseEvent
eventleave(wxevent
);
427 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
428 g_MacLastWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
429 eventleave
.SetEventObject( g_MacLastWindow
) ;
432 wxToolTip::RelayEvent( g_MacLastWindow
, eventleave
);
433 #endif // wxUSE_TOOLTIPS
434 g_MacLastWindow
->GetEventHandler()->ProcessEvent(eventleave
);
436 if ( currentMouseWindow
)
438 wxMouseEvent
evententer(wxevent
);
439 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
440 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
441 evententer
.SetEventObject( currentMouseWindow
) ;
443 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
444 #endif // wxUSE_TOOLTIPS
445 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
447 g_MacLastWindow
= currentMouseWindow
;
450 if ( windowPart
== inMenuBar
)
452 // special case menu bar, as we are having a low-level runloop we must do it ourselves
453 if ( cEvent
.GetKind() == kEventMouseDown
)
455 ::MenuSelect( screenMouseLocation
) ;
458 } // if ( windowPart == inMenuBar )
459 else if ( currentMouseWindow
)
461 currentMouseWindow
->ScreenToClient( &wxevent
.m_x
, &wxevent
.m_y
) ;
463 wxevent
.SetEventObject( currentMouseWindow
) ;
467 wxWindow
* cursorTarget
= currentMouseWindow
;
468 wxPoint
cursorPoint( wxevent
.m_x
, wxevent
.m_y
) ;
470 while( cursorTarget
&& !cursorTarget
->MacSetupCursor( cursorPoint
) )
472 cursorTarget
= cursorTarget
->GetParent() ;
474 cursorPoint
+= cursorTarget
->GetPosition() ;
479 if ( wxevent
.GetEventType() == wxEVT_LEFT_DOWN
)
481 // set focus to this window
482 if (currentMouseWindow
->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow
)
483 currentMouseWindow
->SetFocus();
486 // make tooltips current
489 if ( wxevent
.GetEventType() == wxEVT_MOTION
490 || wxevent
.GetEventType() == wxEVT_ENTER_WINDOW
491 || wxevent
.GetEventType() == wxEVT_LEAVE_WINDOW
)
492 wxToolTip::RelayEvent( currentMouseWindow
, wxevent
);
493 #endif // wxUSE_TOOLTIPS
494 if ( currentMouseWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
496 if ( cEvent
.GetKind() == kEventMouseUp
&& wxTheApp
->s_captureWindow
)
498 wxTheApp
->s_captureWindow
= NULL
;
501 } // else if ( currentMouseWindow )
505 static pascal OSStatus
wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
507 OSStatus result
= eventNotHandledErr
;
509 wxMacCarbonEvent
cEvent( event
) ;
511 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
512 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
514 switch( GetEventKind( event
) )
516 case kEventWindowActivated
:
518 toplevelWindow
->MacActivate( cEvent
.GetTicks() , true) ;
519 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, true , toplevelWindow
->GetId());
520 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
521 wxevent
.SetEventObject(toplevelWindow
);
522 toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
);
523 // we still sending an eventNotHandledErr in order to allow for default processing
526 case kEventWindowDeactivated
:
528 toplevelWindow
->MacActivate(cEvent
.GetTicks() , false) ;
529 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, false , toplevelWindow
->GetId());
530 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
531 wxevent
.SetEventObject(toplevelWindow
);
532 toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
);
533 // we still sending an eventNotHandledErr in order to allow for default processing
536 case kEventWindowShown
:
537 toplevelWindow
->Refresh() ;
540 case kEventWindowClose
:
541 toplevelWindow
->Close() ;
544 case kEventWindowBoundsChanged
:
546 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
547 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
548 wxRect
r( newRect
.left
, newRect
.top
, newRect
.right
- newRect
.left
, newRect
.bottom
- newRect
.top
) ;
549 if ( attributes
& kWindowBoundsChangeSizeChanged
)
551 // according to the other ports we handle this within the OS level
552 // resize event, not within a wxSizeEvent
553 wxFrame
*frame
= wxDynamicCast( toplevelWindow
, wxFrame
) ;
557 frame
->PositionStatusBar();
560 frame
->PositionToolBar();
564 wxSizeEvent
event( r
.GetSize() , toplevelWindow
->GetId() ) ;
565 event
.SetEventObject( toplevelWindow
) ;
567 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
569 if ( attributes
& kWindowBoundsChangeOriginChanged
)
571 wxMoveEvent
event( r
.GetLeftTop() , toplevelWindow
->GetId() ) ;
572 event
.SetEventObject( toplevelWindow
) ;
573 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
578 case kEventWindowBoundsChanging
:
580 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
581 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
582 wxRect
r( newRect
.left
, newRect
.top
, newRect
.right
- newRect
.left
, newRect
.bottom
- newRect
.top
) ;
584 if ( (attributes
& kWindowBoundsChangeSizeChanged
) || (attributes
& kWindowBoundsChangeOriginChanged
) )
586 // this is a EVT_SIZING not a EVT_SIZE type !
587 wxSizeEvent
wxevent( r
, toplevelWindow
->GetId() ) ;
588 wxevent
.SetEventObject( toplevelWindow
) ;
590 if ( toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
592 adjustR
= wxevent
.GetRect() ;
594 if ( toplevelWindow
->GetMaxWidth() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxWidth() )
595 adjustR
.SetWidth( toplevelWindow
->GetMaxWidth() ) ;
596 if ( toplevelWindow
->GetMaxHeight() != -1 && adjustR
.GetHeight() > toplevelWindow
->GetMaxHeight() )
597 adjustR
.SetHeight( toplevelWindow
->GetMaxHeight() ) ;
598 if ( toplevelWindow
->GetMinWidth() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinWidth() )
599 adjustR
.SetWidth( toplevelWindow
->GetMinWidth() ) ;
600 if ( toplevelWindow
->GetMinHeight() != -1 && adjustR
.GetHeight() < toplevelWindow
->GetMinHeight() )
601 adjustR
.SetHeight( toplevelWindow
->GetMinHeight() ) ;
602 Rect adjustedRect
= { adjustR
.y
, adjustR
.x
, adjustR
.y
+ adjustR
.height
, adjustR
.x
+ adjustR
.width
} ;
603 if ( !EqualRect( &newRect
, &adjustedRect
) )
604 cEvent
.SetParameter( kEventParamCurrentBounds
, &adjustedRect
) ;
616 pascal OSStatus
wxMacTopLevelEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
618 OSStatus result
= eventNotHandledErr
;
620 switch ( GetEventClass( event
) )
622 case kEventClassKeyboard
:
623 result
= KeyboardEventHandler( handler
, event
, data
) ;
625 case kEventClassTextInput
:
626 result
= TextInputEventHandler( handler
, event
, data
) ;
628 case kEventClassWindow
:
629 result
= wxMacTopLevelWindowEventHandler( handler
, event
, data
) ;
631 case kEventClassMouse
:
632 result
= wxMacTopLevelMouseEventHandler( handler
, event
, data
) ;
640 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler
)
642 // ---------------------------------------------------------------------------
643 // wxWindowMac utility functions
644 // ---------------------------------------------------------------------------
646 // Find an item given the Macintosh Window Reference
648 wxList
wxWinMacWindowList(wxKEY_INTEGER
);
649 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
651 wxNode
*node
= wxWinMacWindowList
.Find((long)inWindowRef
);
654 return (wxTopLevelWindowMac
*)node
->GetData();
657 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
) ;
658 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
660 // adding NULL WindowRef is (first) surely a result of an error and
661 // (secondly) breaks menu command processing
662 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
664 if ( !wxWinMacWindowList
.Find((long)inWindowRef
) )
665 wxWinMacWindowList
.Append((long)inWindowRef
, win
);
668 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
) ;
669 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
671 wxWinMacWindowList
.DeleteObject(win
);
675 // ----------------------------------------------------------------------------
676 // wxTopLevelWindowMac creation
677 // ----------------------------------------------------------------------------
679 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
681 void wxTopLevelWindowMac::Init()
684 m_maximizeOnShow
= FALSE
;
686 #if TARGET_API_MAC_OSX
687 m_macUsesCompositing
= TRUE
;
689 m_macUsesCompositing
= FALSE
;
691 m_macEventHandler
= NULL
;
694 class wxMacDeferredWindowDeleter
: public wxObject
697 wxMacDeferredWindowDeleter( WindowRef windowRef
)
699 m_macWindow
= windowRef
;
701 virtual ~wxMacDeferredWindowDeleter()
703 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
706 WindowRef m_macWindow
;
709 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
711 const wxString
& title
,
715 const wxString
& name
)
720 m_windowStyle
= style
;
724 m_windowId
= id
== -1 ? NewControlId() : id
;
726 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
728 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
730 wxTopLevelWindows
.Append(this);
733 parent
->AddChild(this);
738 wxTopLevelWindowMac::~wxTopLevelWindowMac()
742 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
743 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
746 if ( m_macEventHandler
)
748 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
749 m_macEventHandler
= NULL
;
752 wxRemoveMacWindowAssociation( this ) ;
754 if ( wxModelessWindows
.Find(this) )
755 wxModelessWindows
.DeleteObject(this);
759 // ----------------------------------------------------------------------------
760 // wxTopLevelWindowMac maximize/minimize
761 // ----------------------------------------------------------------------------
763 void wxTopLevelWindowMac::Maximize(bool maximize
)
765 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
766 wxMacWindowClipper
clip (this);
767 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
770 GDHandle device = NULL ;
771 verify_noerr( GetWindowGreatestAreaDevice( (WindowRef) m_macWindow , kWindowContentRgn ,
773 verify_noerr( GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ) ;
778 Point pt = { 0, 0 } ;
779 SetPortWindowPort((WindowRef)m_macWindow) ;
780 LocalToGlobal( &pt ) ;
783 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
784 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
785 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
789 bool wxTopLevelWindowMac::IsMaximized() const
791 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
794 void wxTopLevelWindowMac::Iconize(bool iconize
)
796 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
797 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
800 bool wxTopLevelWindowMac::IsIconized() const
802 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
805 void wxTopLevelWindowMac::Restore()
807 // not available on mac
810 // ----------------------------------------------------------------------------
811 // wxTopLevelWindowMac misc
812 // ----------------------------------------------------------------------------
814 wxPoint
wxTopLevelWindowMac::GetClientAreaOrigin() const
816 return wxPoint(0,0) ;
819 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
822 wxTopLevelWindowBase::SetIcon(icon
);
825 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush
&brush
)
827 wxTopLevelWindowBase::MacSetBackgroundBrush( brush
) ;
829 if ( m_macBackgroundBrush
.Ok() && m_macBackgroundBrush
.GetStyle() != wxTRANSPARENT
&& m_macBackgroundBrush
.MacGetBrushKind() == kwxMacBrushTheme
)
831 SetThemeWindowBackground( (WindowRef
) m_macWindow
, m_macBackgroundBrush
.MacGetTheme() , false ) ;
835 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
837 if ( m_macEventHandler
!= NULL
)
839 verify_noerr( ::RemoveEventHandler( (EventHandlerRef
) m_macEventHandler
) ) ;
841 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacTopLevelEventHandlerUPP(),
842 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
845 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
849 const wxString
& name
)
851 OSStatus err
= noErr
;
853 m_windowStyle
= style
;
867 int w
= WidthDefault(size
.x
);
868 int h
= HeightDefault(size
.y
);
870 ::SetRect(&theBoundsRect
, x
, y
, x
+ w
, y
+ h
);
872 // translate the window attributes in the appropriate window class and attributes
874 WindowClass wclass
= 0;
875 WindowAttributes attr
= kWindowNoAttributes
;
877 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
880 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
881 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
882 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
885 wclass
= kFloatingWindowClass
;
886 if ( HasFlag(wxTINY_CAPTION_VERT
) )
888 attr
|= kWindowSideTitlebarAttribute
;
893 wclass
= kPlainWindowClass
;
896 else if ( HasFlag( wxCAPTION
) )
898 wclass
= kDocumentWindowClass
;
902 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
903 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
905 wclass
= kDocumentWindowClass
;
909 wclass
= kPlainWindowClass
;
913 if ( HasFlag( wxMINIMIZE_BOX
) )
915 attr
|= kWindowCollapseBoxAttribute
;
917 if ( HasFlag( wxMAXIMIZE_BOX
) )
919 attr
|= kWindowFullZoomAttribute
;
921 if ( HasFlag( wxRESIZE_BORDER
) )
923 attr
|= kWindowResizableAttribute
;
925 if ( HasFlag( wxCLOSE_BOX
) )
927 attr
|= kWindowCloseBoxAttribute
;
930 if (UMAGetSystemVersion() >= 0x1000)
932 // turn on live resizing (OS X only)
933 attr
|= kWindowLiveResizeAttribute
;
936 if (HasFlag(wxSTAY_ON_TOP
))
937 wclass
= kUtilityWindowClass
;
939 #if TARGET_API_MAC_OSX
940 attr
|= kWindowCompositingAttribute
;
943 if ( HasFlag(wxFRAME_SHAPED
) )
945 WindowDefSpec customWindowDefSpec
;
946 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
947 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
949 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
950 attr
, &theBoundsRect
,
951 (WindowRef
*) &m_macWindow
);
955 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
958 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
959 wxAssociateWinWithMacWindow( (WindowRef
) m_macWindow
, this ) ;
960 UMASetWTitle( (WindowRef
) m_macWindow
, title
, m_font
.GetEncoding() ) ;
961 if ( m_macUsesCompositing
)
963 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
967 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
969 // the root control level handleer
970 MacInstallEventHandler() ;
972 // the frame window event handler
973 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
974 MacInstallTopLevelWindowEventHandler() ;
978 if ( HasFlag(wxFRAME_SHAPED
) )
980 // default shape matches the window size
981 wxRegion
rgn(0, 0, w
, h
);
985 wxWindowCreateEvent
event(this);
986 GetEventHandler()->ProcessEvent(event
);
989 void wxTopLevelWindowMac::ClearBackground()
991 wxWindow::ClearBackground() ;
994 // Raise the window to the top of the Z order
995 void wxTopLevelWindowMac::Raise()
997 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1000 // Lower the window to the bottom of the Z order
1001 void wxTopLevelWindowMac::Lower()
1003 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
1007 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1009 if(s_macDeactivateWindow
)
1011 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1012 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1016 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1018 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1020 if(s_macDeactivateWindow
==this)
1021 s_macDeactivateWindow
=NULL
;
1022 MacDelayedDeactivation(timestamp
);
1023 MacPropagateHiliteChanged() ;
1026 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1028 wxWindow::SetTitle( title
) ;
1029 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
1032 bool wxTopLevelWindowMac::Show(bool show
)
1034 if ( !wxTopLevelWindowBase::Show(show
) )
1039 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1040 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1042 ::ShowWindow( (WindowRef
)m_macWindow
);
1047 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1049 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1050 // as apps expect a size event to occur at this moment
1051 wxSizeEvent
event( GetSize() , m_windowId
);
1052 event
.SetEventObject(this);
1053 GetEventHandler()->ProcessEvent(event
);
1057 #if wxUSE_SYSTEM_OPTIONS
1058 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1060 ::HideWindow((WindowRef
) m_macWindow
);
1065 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1069 MacPropagateVisibilityChanged() ;
1074 // we are still using coordinates of the content view, todo switch to structure bounds
1076 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1078 Rect bounds
= { y
, x
, y
+ height
, x
+ width
} ;
1079 verify_noerr(SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1082 void wxTopLevelWindowMac::DoGetPosition( int *x
, int *y
) const
1085 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1086 if(x
) *x
= bounds
.left
;
1087 if(y
) *y
= bounds
.top
;
1089 void wxTopLevelWindowMac::DoGetSize( int *width
, int *height
) const
1092 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1093 if(width
) *width
= bounds
.right
- bounds
.left
;
1094 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1097 void wxTopLevelWindowMac::DoGetClientSize( int *width
, int *height
) const
1100 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowContentRgn
, &bounds
)) ;
1101 if(width
) *width
= bounds
.right
- bounds
.left
;
1102 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1105 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set
)
1107 #if TARGET_API_MAC_OSX
1109 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1110 wxASSERT_MSG( attr
& kWindowCompositingAttribute
,
1111 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1113 MacChangeWindowAttributes( set
? kWindowMetalAttribute
: kWindowNoAttributes
,
1114 set
? kWindowNoAttributes
: kWindowMetalAttribute
) ;
1118 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1120 #if TARGET_API_MAC_OSX
1121 return MacGetWindowAttributes() & kWindowMetalAttribute
;
1127 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet
, wxUint32 attributesToClear
)
1129 ChangeWindowAttributes ( (WindowRef
) m_macWindow
, attributesToSet
, attributesToClear
) ;
1132 wxUint32
wxTopLevelWindowMac::MacGetWindowAttributes() const
1135 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1139 // ---------------------------------------------------------------------------
1140 // Shape implementation
1141 // ---------------------------------------------------------------------------
1144 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1146 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1147 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1149 // The empty region signifies that the shape should be removed from the
1151 if ( region
.IsEmpty() )
1153 wxSize sz
= GetClientSize();
1154 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1155 return SetShape(rgn
);
1158 // Make a copy of the region
1159 RgnHandle shapeRegion
= NewRgn();
1160 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1162 // Dispose of any shape region we may already have
1163 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1167 // Save the region so we can use it later
1168 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1170 // Tell the window manager that the window has changed shape
1171 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1175 // ---------------------------------------------------------------------------
1176 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1177 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1178 // ---------------------------------------------------------------------------
1180 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1182 GetWindowPortBounds(window
, inRect
);
1183 Point pt
= {inRect
->left
, inRect
->top
};
1184 SetPort((GrafPtr
) GetWindowPort(window
));
1187 inRect
->left
= pt
.h
;
1188 inRect
->bottom
+= pt
.v
;
1189 inRect
->right
+= pt
.h
;
1193 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1195 /*------------------------------------------------------
1196 Define which options your custom window supports.
1197 --------------------------------------------------------*/
1198 //just enable everything for our demo
1199 *(OptionBits
*)param
=//kWindowCanGrow|
1201 //kWindowCanCollapse|
1202 //kWindowCanGetWindowRegion|
1203 //kWindowHasTitleBar|
1204 //kWindowSupportsDragHilite|
1205 kWindowCanDrawInCurrentPort
|
1206 //kWindowCanMeasureTitle|
1207 kWindowWantsDisposeAtProcessDeath
|
1208 kWindowSupportsSetGrowImageRegion
|
1209 kWindowDefSupportsColorGrafPort
;
1213 // The content region is left as a rectangle matching the window size, this is
1214 // so the origin in the paint event, and etc. still matches what the
1215 // programmer expects.
1216 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1219 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1223 wxShapedMacWindowGetPos(window
, &r
) ;
1224 RectRgn( rgn
, &r
) ;
1228 // The structure region is set to the shape given to the SetShape method.
1229 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1231 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1237 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1238 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1239 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1240 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1246 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1248 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1250 switch(rgnRec
->regionCode
)
1252 case kWindowStructureRgn
:
1253 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1255 case kWindowContentRgn
:
1256 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1259 SetEmptyRgn(rgnRec
->winRgn
);
1266 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1268 /*------------------------------------------------------
1269 Determine the region of the window which was hit
1270 --------------------------------------------------------*/
1272 static RgnHandle tempRgn
=nil
;
1277 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1279 //Mac OS 8.5 or later
1280 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1281 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1284 return wNoHit
;//no significant area was hit.
1288 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1292 case kWindowMsgHitTest
:
1293 return wxShapedMacWindowHitTest(window
,param
);
1295 case kWindowMsgGetFeatures
:
1296 return wxShapedMacWindowGetFeatures(window
,param
);
1298 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1299 case kWindowMsgGetRegion
:
1300 return wxShapedMacWindowGetRegion(window
,param
);
1306 // ---------------------------------------------------------------------------