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"
40 #include "wx/mac/uma.h"
41 #include "wx/mac/aga.h"
43 #include "wx/tooltip.h"
45 #if wxUSE_SYSTEM_OPTIONS
46 #include "wx/sysopt.h"
49 #include <ToolUtils.h>
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // list of all frames and modeless dialogs
56 wxWindowList wxModelessWindows
;
58 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
60 // ============================================================================
61 // wxTopLevelWindowMac implementation
62 // ============================================================================
64 BEGIN_EVENT_TABLE(wxTopLevelWindowMac
, wxTopLevelWindowBase
)
68 // ---------------------------------------------------------------------------
70 // ---------------------------------------------------------------------------
72 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
74 static const EventTypeSpec eventList
[] =
76 // TODO remove control related event like key and mouse (except for WindowLeave events)
78 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
80 { kEventClassKeyboard
, kEventRawKeyDown
} ,
81 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
82 { kEventClassKeyboard
, kEventRawKeyUp
} ,
83 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
86 { kEventClassWindow
, kEventWindowShown
} ,
87 { kEventClassWindow
, kEventWindowActivated
} ,
88 { kEventClassWindow
, kEventWindowDeactivated
} ,
89 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
90 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
91 { kEventClassWindow
, kEventWindowClose
} ,
93 // we have to catch these events on the toplevel window level, as controls don't get the
94 // raw mouse events anymore
96 { kEventClassMouse
, kEventMouseDown
} ,
97 { kEventClassMouse
, kEventMouseUp
} ,
98 { kEventClassMouse
, kEventMouseWheelMoved
} ,
99 { kEventClassMouse
, kEventMouseMoved
} ,
100 { kEventClassMouse
, kEventMouseDragged
} ,
103 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
105 OSStatus result
= eventNotHandledErr
;
107 wxWindow
* focus
= wxWindow::FindFocus() ;
115 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
117 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
118 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
119 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
120 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
121 sizeof( Point
), NULL
, &point
);
123 switch ( GetEventKind( event
) )
125 case kEventTextInputUnicodeForKeyEvent
:
126 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
127 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
128 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
131 ControlRef macControl
= (ControlRef
) control
->GetHandle() ;
134 ::HandleControlKey( macControl
, keyCode
, charCode
, modifiers
) ;
139 // this may lead to double events sent to a window in case all handlers have skipped the key down event
140 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
141 UInt32 message = (keyCode << 8) + charCode;
143 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
144 focus , message , modifiers , when , point.h , point.v ) )
155 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
157 OSStatus result
= eventNotHandledErr
;
159 wxWindow
* focus
= wxWindow::FindFocus() ;
164 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
166 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
167 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
168 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
169 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
170 sizeof( Point
), NULL
, &point
);
172 UInt32 message
= (keyCode
<< 8) + charCode
;
173 switch( GetEventKind( event
) )
175 case kEventRawKeyRepeat
:
176 case kEventRawKeyDown
:
178 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
179 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
180 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
181 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
182 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
186 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
189 case kEventRawKeyUp
:
190 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
191 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
196 case kEventRawKeyModifiersChanged
:
198 wxKeyEvent
event(wxEVT_KEY_DOWN
);
200 event
.m_shiftDown
= modifiers
& shiftKey
;
201 event
.m_controlDown
= modifiers
& controlKey
;
202 event
.m_altDown
= modifiers
& optionKey
;
203 event
.m_metaDown
= modifiers
& cmdKey
;
207 event
.m_timeStamp
= when
;
208 wxWindow
* focus
= wxWindow::FindFocus() ;
209 event
.SetEventObject(focus
);
211 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
213 event
.m_keyCode
= WXK_CONTROL
;
214 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
215 focus
->GetEventHandler()->ProcessEvent( event
) ;
217 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
219 event
.m_keyCode
= WXK_SHIFT
;
220 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
221 focus
->GetEventHandler()->ProcessEvent( event
) ;
223 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
225 event
.m_keyCode
= WXK_ALT
;
226 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
227 focus
->GetEventHandler()->ProcessEvent( event
) ;
229 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
231 event
.m_keyCode
= WXK_COMMAND
;
232 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
233 focus
->GetEventHandler()->ProcessEvent( event
) ;
235 wxTheApp
->s_lastModifiers
= modifiers
;
243 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
244 // for windows that we didn't create (like eg Scrollbars in a databrowser) , or for controls where we did not handle the
247 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
249 wxWindow
* g_MacLastWindow
= NULL
;
251 static void SetupMouseEvent( wxMouseEvent
&wxevent
, wxMacCarbonEvent
&cEvent
)
253 UInt32 modifiers
= cEvent
.GetParameter
<UInt32
>(kEventParamKeyModifiers
, typeUInt32
) ;
254 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
256 // this parameter are not given for all events
257 EventMouseButton button
= 0 ;
258 UInt32 clickCount
= 0 ;
259 cEvent
.GetParameter
<EventMouseButton
>(kEventParamMouseButton
, typeMouseButton
, &button
) ;
260 cEvent
.GetParameter
<UInt32
>(kEventParamClickCount
, typeUInt32
, &clickCount
) ;
262 wxevent
.m_x
= screenMouseLocation
.h
;
263 wxevent
.m_y
= screenMouseLocation
.v
;
264 wxevent
.m_shiftDown
= modifiers
& shiftKey
;
265 wxevent
.m_controlDown
= modifiers
& controlKey
;
266 wxevent
.m_altDown
= modifiers
& optionKey
;
267 wxevent
.m_metaDown
= modifiers
& cmdKey
;
268 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
269 // a control click is interpreted as a right click
270 if ( button
== kEventMouseButtonPrimary
&& (modifiers
& controlKey
) )
272 button
= kEventMouseButtonSecondary
;
275 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
277 if ( button
!= 0 && cEvent
.GetKind() != kEventMouseUp
)
281 case kEventMouseButtonPrimary
:
282 wxevent
.m_leftDown
= true ;
284 case kEventMouseButtonSecondary
:
285 wxevent
.m_rightDown
= true ;
287 case kEventMouseButtonTertiary
:
288 wxevent
.m_middleDown
= true ;
292 // determinate the correct click button
293 if ( button
== kEventMouseButtonSecondary
)
295 if (cEvent
.GetKind() == kEventMouseDown
)
296 wxevent
.SetEventType( clickCount
> 1 ? wxEVT_RIGHT_DOWN
: wxEVT_RIGHT_DCLICK
) ;
297 else if ( cEvent
.GetKind() == kEventMouseUp
)
298 wxevent
.SetEventType(wxEVT_RIGHT_UP
) ;
300 else if ( button
== kEventMouseButtonTertiary
)
302 if (cEvent
.GetKind() == kEventMouseDown
)
303 wxevent
.SetEventType(clickCount
> 1 ? wxEVT_MIDDLE_DCLICK
: wxEVT_MIDDLE_DOWN
) ;
304 else if ( cEvent
.GetKind() == kEventMouseUp
)
305 wxevent
.SetEventType(wxEVT_MIDDLE_UP
) ;
309 if (cEvent
.GetKind() == kEventMouseDown
)
310 wxevent
.SetEventType(clickCount
> 1 ? wxEVT_LEFT_DCLICK
: wxEVT_LEFT_DOWN
) ;
311 else if ( cEvent
.GetKind() == kEventMouseUp
)
312 wxevent
.SetEventType(wxEVT_LEFT_UP
) ;
313 else if ( cEvent
.GetKind() == kEventMouseWheelMoved
)
315 wxevent
.SetEventType(wxEVT_MOUSEWHEEL
) ;
317 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
318 SInt32 delta
= cEvent
.GetParameter
<SInt32
>(kEventParamMouseWheelDelta
, typeLongInteger
) ;
320 wxevent
.m_wheelRotation
= delta
;
321 wxevent
.m_wheelDelta
= 1;
322 wxevent
.m_linesPerAction
= 1;
325 wxevent
.SetEventType(wxEVT_MOTION
) ;
329 ControlRef
wxMacFindSubControl( Point location
, ControlRef superControl
, ControlPartCode
*outPart
)
333 UInt16 childrenCount
= 0 ;
334 OSStatus err
= CountSubControls( superControl
, &childrenCount
) ;
335 if ( err
== errControlIsNotEmbedder
)
337 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
339 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
341 ControlHandle sibling
;
342 err
= GetIndexedSubControl( superControl
, i
, & sibling
) ;
343 if ( err
== errControlIsNotEmbedder
)
346 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
347 if ( IsControlVisible( sibling
) )
350 GetControlBounds( sibling
, &r
) ;
351 if ( MacPtInRect( location
, &r
) )
353 ControlHandle child
= wxMacFindSubControl( location
, sibling
, outPart
) ;
358 *outPart
= TestControl( sibling
, location
) ;
368 ControlRef
wxMacFindControlUnderMouse( Point location
, WindowRef window
, ControlPartCode
*outPart
)
370 #if TARGET_API_MAC_OSX
371 return FindControlUnderMouse( location
, window
, outPart
) ;
373 ControlRef rootControl
= NULL
;
374 verify_noerr( GetRootControl( window
, &rootControl
) ) ;
375 return wxMacFindSubControl( location
, rootControl
, outPart
) ;
378 pascal OSStatus
wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
381 OSStatus result
= eventNotHandledErr
;
383 wxMacCarbonEvent
cEvent( event
) ;
385 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
386 Point windowMouseLocation
= screenMouseLocation
;
389 short windowPart
= ::FindWindow(screenMouseLocation
, &window
);
391 wxWindow
* currentMouseWindow
= NULL
;
394 // calculate window relative coordinates
397 ::SetPort( UMAGetWindowPort(window
) ) ;
398 ::GlobalToLocal( &windowMouseLocation
) ;
401 if ( wxTheApp
->s_captureWindow
&& wxTheApp
->s_captureWindow
->MacGetTopLevelWindowRef() == (WXWindow
) window
&& windowPart
== inContent
)
403 currentMouseWindow
= wxTheApp
->s_captureWindow
;
405 else if ( (IsWindowActive(window
) && windowPart
== inContent
) )
407 ControlPartCode part
;
408 ControlRef control
= wxMacFindControlUnderMouse( windowMouseLocation
, window
, &part
) ;
409 currentMouseWindow
= wxFindControlFromMacControl( control
) ;
413 wxMouseEvent
wxevent(wxEVT_LEFT_DOWN
);
414 SetupMouseEvent( wxevent
, cEvent
) ;
416 // handle all enter / leave events
418 if ( currentMouseWindow
!= g_MacLastWindow
)
420 if ( g_MacLastWindow
)
422 wxMouseEvent
eventleave(wxevent
);
423 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
424 g_MacLastWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
425 eventleave
.SetEventObject( g_MacLastWindow
) ;
428 wxToolTip::RelayEvent( g_MacLastWindow
, eventleave
);
429 #endif // wxUSE_TOOLTIPS
430 g_MacLastWindow
->GetEventHandler()->ProcessEvent(eventleave
);
432 if ( currentMouseWindow
)
434 wxMouseEvent
evententer(wxevent
);
435 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
436 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
437 evententer
.SetEventObject( currentMouseWindow
) ;
439 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
440 #endif // wxUSE_TOOLTIPS
441 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
443 g_MacLastWindow
= currentMouseWindow
;
446 if ( windowPart
== inMenuBar
)
448 // special case menu bar, as we are having a low-level runloop we must do it ourselves
449 if ( cEvent
.GetKind() == kEventMouseDown
)
451 ::MenuSelect( screenMouseLocation
) ;
454 } // if ( windowPart == inMenuBar )
455 else if ( currentMouseWindow
)
457 currentMouseWindow
->ScreenToClient( &wxevent
.m_x
, &wxevent
.m_y
) ;
459 wxevent
.SetEventObject( currentMouseWindow
) ;
463 wxWindow
* cursorTarget
= currentMouseWindow
;
464 wxPoint
cursorPoint( wxevent
.m_x
, wxevent
.m_y
) ;
466 while( cursorTarget
&& !cursorTarget
->MacSetupCursor( cursorPoint
) )
468 cursorTarget
= cursorTarget
->GetParent() ;
470 cursorPoint
+= cursorTarget
->GetPosition() ;
475 if ( wxevent
.GetEventType() == wxEVT_LEFT_DOWN
)
477 // set focus to this window
478 if (currentMouseWindow
->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow
)
479 currentMouseWindow
->SetFocus();
482 // make tooltips current
485 if ( wxevent
.GetEventType() == wxEVT_MOTION
486 || wxevent
.GetEventType() == wxEVT_ENTER_WINDOW
487 || wxevent
.GetEventType() == wxEVT_LEAVE_WINDOW
)
488 wxToolTip::RelayEvent( currentMouseWindow
, wxevent
);
489 #endif // wxUSE_TOOLTIPS
490 if ( currentMouseWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
492 if ( cEvent
.GetKind() == kEventMouseUp
&& wxTheApp
->s_captureWindow
)
494 wxTheApp
->s_captureWindow
= NULL
;
497 } // else if ( currentMouseWindow )
501 static pascal OSStatus
wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
503 OSStatus result
= eventNotHandledErr
;
505 wxMacCarbonEvent
cEvent( event
) ;
507 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
508 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
510 switch( GetEventKind( event
) )
512 case kEventWindowActivated
:
514 toplevelWindow
->MacActivate( cEvent
.GetTicks() , true) ;
516 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, true , toplevelWindow
->GetId());
517 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
518 wxevent
.SetEventObject(toplevelWindow
);
519 toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
);
523 case kEventWindowDeactivated
:
525 toplevelWindow
->MacActivate(cEvent
.GetTicks() , false) ;
526 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, false , toplevelWindow
->GetId());
527 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
528 wxevent
.SetEventObject(toplevelWindow
);
529 toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
);
533 case kEventWindowShown
:
534 toplevelWindow
->Refresh() ;
537 case kEventWindowClose
:
538 toplevelWindow
->Close() ;
541 case kEventWindowBoundsChanged
:
543 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
544 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
545 wxRect
r( newRect
.left
, newRect
.top
, newRect
.right
- newRect
.left
, newRect
.bottom
- newRect
.top
) ;
546 if ( attributes
& kWindowBoundsChangeSizeChanged
)
548 // according to the other ports we handle this within the OS level
549 // resize event, not within a wxSizeEvent
550 wxFrame
*frame
= wxDynamicCast( toplevelWindow
, wxFrame
) ;
554 frame
->PositionStatusBar();
557 frame
->PositionToolBar();
561 wxSizeEvent
event( r
.GetSize() , toplevelWindow
->GetId() ) ;
562 event
.SetEventObject( toplevelWindow
) ;
564 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
566 if ( attributes
& kWindowBoundsChangeOriginChanged
)
568 wxMoveEvent
event( r
.GetLeftTop() , toplevelWindow
->GetId() ) ;
569 event
.SetEventObject( toplevelWindow
) ;
570 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
575 case kEventWindowBoundsChanging
:
577 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
578 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
579 wxRect
r( newRect
.left
, newRect
.top
, newRect
.right
- newRect
.left
, newRect
.bottom
- newRect
.top
) ;
581 if ( (attributes
& kWindowBoundsChangeSizeChanged
) || (attributes
& kWindowBoundsChangeOriginChanged
) )
583 // this is a EVT_SIZING not a EVT_SIZE type !
584 wxSizeEvent
wxevent( r
, toplevelWindow
->GetId() ) ;
585 wxevent
.SetEventObject( toplevelWindow
) ;
587 if ( toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
589 adjustR
= wxevent
.GetRect() ;
591 if ( toplevelWindow
->GetMaxWidth() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxWidth() )
592 adjustR
.SetWidth( toplevelWindow
->GetMaxWidth() ) ;
593 if ( toplevelWindow
->GetMaxHeight() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxHeight() )
594 adjustR
.SetHeight( toplevelWindow
->GetMaxHeight() ) ;
595 if ( toplevelWindow
->GetMinWidth() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinWidth() )
596 adjustR
.SetWidth( toplevelWindow
->GetMinWidth() ) ;
597 if ( toplevelWindow
->GetMinHeight() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinHeight() )
598 adjustR
.SetHeight( toplevelWindow
->GetMinHeight() ) ;
599 Rect adjustedRect
= { adjustR
.y
, adjustR
.x
, adjustR
.y
+ adjustR
.height
, adjustR
.x
+ adjustR
.width
} ;
600 if ( !EqualRect( &newRect
, &adjustedRect
) )
601 cEvent
.SetParameter( kEventParamCurrentBounds
, &adjustedRect
) ;
613 pascal OSStatus
wxMacTopLevelEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
615 OSStatus result
= eventNotHandledErr
;
617 switch ( GetEventClass( event
) )
619 case kEventClassKeyboard
:
620 result
= KeyboardEventHandler( handler
, event
, data
) ;
622 case kEventClassTextInput
:
623 result
= TextInputEventHandler( handler
, event
, data
) ;
625 case kEventClassWindow
:
626 result
= wxMacTopLevelWindowEventHandler( handler
, event
, data
) ;
628 case kEventClassMouse
:
629 result
= wxMacTopLevelMouseEventHandler( handler
, event
, data
) ;
637 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler
)
639 // ---------------------------------------------------------------------------
640 // wxWindowMac utility functions
641 // ---------------------------------------------------------------------------
643 // Find an item given the Macintosh Window Reference
645 wxList
wxWinMacWindowList(wxKEY_INTEGER
);
646 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
648 wxNode
*node
= wxWinMacWindowList
.Find((long)inWindowRef
);
651 return (wxTopLevelWindowMac
*)node
->GetData();
654 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
) ;
655 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
657 // adding NULL WindowRef is (first) surely a result of an error and
658 // (secondly) breaks menu command processing
659 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
661 if ( !wxWinMacWindowList
.Find((long)inWindowRef
) )
662 wxWinMacWindowList
.Append((long)inWindowRef
, win
);
665 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
) ;
666 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
668 wxWinMacWindowList
.DeleteObject(win
);
672 // ----------------------------------------------------------------------------
673 // wxTopLevelWindowMac creation
674 // ----------------------------------------------------------------------------
676 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
678 void wxTopLevelWindowMac::Init()
681 m_maximizeOnShow
= FALSE
;
683 #if TARGET_API_MAC_OSX
684 m_macUsesCompositing
= TRUE
;
686 m_macUsesCompositing
= FALSE
;
688 m_macEventHandler
= NULL
;
691 class wxMacDeferredWindowDeleter
: public wxObject
694 wxMacDeferredWindowDeleter( WindowRef windowRef
)
696 m_macWindow
= windowRef
;
698 virtual ~wxMacDeferredWindowDeleter()
700 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
703 WindowRef m_macWindow
;
706 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
708 const wxString
& title
,
712 const wxString
& name
)
717 m_windowStyle
= style
;
721 m_windowId
= id
== -1 ? NewControlId() : id
;
723 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
725 wxTopLevelWindows
.Append(this);
728 parent
->AddChild(this);
733 wxTopLevelWindowMac::~wxTopLevelWindowMac()
737 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
738 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
741 if ( m_macEventHandler
)
743 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
744 m_macEventHandler
= NULL
;
747 wxRemoveMacWindowAssociation( this ) ;
749 if ( wxModelessWindows
.Find(this) )
750 wxModelessWindows
.DeleteObject(this);
754 // ----------------------------------------------------------------------------
755 // wxTopLevelWindowMac maximize/minimize
756 // ----------------------------------------------------------------------------
758 void wxTopLevelWindowMac::Maximize(bool maximize
)
760 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
761 wxMacWindowClipper
clip (this);
762 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
765 GDHandle device = NULL ;
766 verify_noerr( GetWindowGreatestAreaDevice( (WindowRef) m_macWindow , kWindowContentRgn ,
768 verify_noerr( GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ) ;
773 Point pt = { 0, 0 } ;
774 SetPortWindowPort((WindowRef)m_macWindow) ;
775 LocalToGlobal( &pt ) ;
778 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
779 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
780 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
784 bool wxTopLevelWindowMac::IsMaximized() const
786 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
789 void wxTopLevelWindowMac::Iconize(bool iconize
)
791 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
792 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
795 bool wxTopLevelWindowMac::IsIconized() const
797 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
800 void wxTopLevelWindowMac::Restore()
802 // not available on mac
805 // ----------------------------------------------------------------------------
806 // wxTopLevelWindowMac misc
807 // ----------------------------------------------------------------------------
809 wxPoint
wxTopLevelWindowMac::GetClientAreaOrigin() const
811 return wxPoint(0,0) ;
814 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
817 wxTopLevelWindowBase::SetIcon(icon
);
820 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush
&brush
)
822 wxTopLevelWindowBase::MacSetBackgroundBrush( brush
) ;
824 if ( m_macBackgroundBrush
.Ok() && m_macBackgroundBrush
.GetStyle() != wxTRANSPARENT
&& m_macBackgroundBrush
.MacGetBrushKind() == kwxMacBrushTheme
)
826 SetThemeWindowBackground( (WindowRef
) m_macWindow
, m_macBackgroundBrush
.MacGetTheme() , false ) ;
830 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
832 if ( m_macEventHandler
!= NULL
)
834 verify_noerr( ::RemoveEventHandler( (EventHandlerRef
) m_macEventHandler
) ) ;
836 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacTopLevelEventHandlerUPP(),
837 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
840 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
844 const wxString
& name
)
846 OSStatus err
= noErr
;
848 m_windowStyle
= style
;
862 int w
= WidthDefault(size
.x
);
863 int h
= HeightDefault(size
.y
);
865 ::SetRect(&theBoundsRect
, x
, y
, x
+ w
, y
+ h
);
867 // translate the window attributes in the appropriate window class and attributes
869 WindowClass wclass
= 0;
870 WindowAttributes attr
= kWindowNoAttributes
;
872 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
875 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
876 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
877 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
880 wclass
= kFloatingWindowClass
;
881 if ( HasFlag(wxTINY_CAPTION_VERT
) )
883 attr
|= kWindowSideTitlebarAttribute
;
888 wclass
= kPlainWindowClass
;
891 else if ( HasFlag( wxCAPTION
) )
893 wclass
= kDocumentWindowClass
;
897 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
898 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
900 wclass
= kDocumentWindowClass
;
904 wclass
= kPlainWindowClass
;
908 if ( HasFlag( wxMINIMIZE_BOX
) )
910 attr
|= kWindowCollapseBoxAttribute
;
912 if ( HasFlag( wxMAXIMIZE_BOX
) )
914 attr
|= kWindowFullZoomAttribute
;
916 if ( HasFlag( wxRESIZE_BORDER
) )
918 attr
|= kWindowResizableAttribute
;
920 if ( HasFlag( wxCLOSE_BOX
) )
922 attr
|= kWindowCloseBoxAttribute
;
925 if (UMAGetSystemVersion() >= 0x1000)
927 // turn on live resizing (OS X only)
928 attr
|= kWindowLiveResizeAttribute
;
931 if (HasFlag(wxSTAY_ON_TOP
))
932 wclass
= kUtilityWindowClass
;
934 #if TARGET_API_MAC_OSX
935 attr
|= kWindowCompositingAttribute
;
938 if ( HasFlag(wxFRAME_SHAPED
) )
940 WindowDefSpec customWindowDefSpec
;
941 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
942 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
944 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
945 attr
, &theBoundsRect
,
946 (WindowRef
*) &m_macWindow
);
950 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
953 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
954 wxAssociateWinWithMacWindow( (WindowRef
) m_macWindow
, this ) ;
955 UMASetWTitle( (WindowRef
) m_macWindow
, title
, m_font
.GetEncoding() ) ;
956 if ( m_macUsesCompositing
)
958 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
962 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
964 MacInstallEventHandler() ;
966 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
967 MacInstallTopLevelWindowEventHandler() ;
971 if ( HasFlag(wxFRAME_SHAPED
) )
973 // default shape matches the window size
974 wxRegion
rgn(0, 0, w
, h
);
978 wxWindowCreateEvent
event(this);
979 GetEventHandler()->ProcessEvent(event
);
982 void wxTopLevelWindowMac::ClearBackground()
984 wxWindow::ClearBackground() ;
987 // Raise the window to the top of the Z order
988 void wxTopLevelWindowMac::Raise()
990 ::SelectWindow( (WindowRef
)m_macWindow
) ;
993 // Lower the window to the bottom of the Z order
994 void wxTopLevelWindowMac::Lower()
996 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
1000 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1002 if(s_macDeactivateWindow
)
1004 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1005 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1009 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1011 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1013 if(s_macDeactivateWindow
==this)
1014 s_macDeactivateWindow
=NULL
;
1015 MacDelayedDeactivation(timestamp
);
1018 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1020 wxWindow::SetTitle( title
) ;
1021 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
1024 bool wxTopLevelWindowMac::Show(bool show
)
1026 if ( !wxTopLevelWindowBase::Show(show
) )
1031 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1032 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1034 ::ShowWindow( (WindowRef
)m_macWindow
);
1039 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1041 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1042 // as apps expect a size event to occur at this moment
1043 wxSizeEvent
event( GetSize() , m_windowId
);
1044 event
.SetEventObject(this);
1045 GetEventHandler()->ProcessEvent(event
);
1049 #if wxUSE_SYSTEM_OPTIONS
1050 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1052 ::HideWindow((WindowRef
) m_macWindow
);
1057 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1061 MacPropagateVisibilityChanged() ;
1066 // we are still using coordinates of the content view, todo switch to structure bounds
1068 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1070 Rect bounds
= { y
, x
, y
+ height
, x
+ width
} ;
1071 verify_noerr(SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1074 void wxTopLevelWindowMac::DoGetPosition( int *x
, int *y
) const
1077 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1078 if(x
) *x
= bounds
.left
;
1079 if(y
) *y
= bounds
.top
;
1081 void wxTopLevelWindowMac::DoGetSize( int *width
, int *height
) const
1084 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1085 if(width
) *width
= bounds
.right
- bounds
.left
;
1086 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1089 void wxTopLevelWindowMac::DoGetClientSize( int *width
, int *height
) const
1092 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowContentRgn
, &bounds
)) ;
1093 if(width
) *width
= bounds
.right
- bounds
.left
;
1094 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1097 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set
)
1099 #if TARGET_API_MAC_OSX
1101 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1102 wxASSERT_MSG( attr
& kWindowCompositingAttribute
,
1103 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1105 MacChangeWindowAttributes( set
? kWindowMetalAttribute
: kWindowNoAttributes
,
1106 set
? kWindowNoAttributes
: kWindowMetalAttribute
) ;
1110 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet
, wxUint32 attributesToClear
)
1112 ChangeWindowAttributes ( (WindowRef
) m_macWindow
, attributesToSet
, attributesToClear
) ;
1115 wxUint32
wxTopLevelWindowMac::MacGetWindowAttributes() const
1118 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1122 // ---------------------------------------------------------------------------
1123 // Shape implementation
1124 // ---------------------------------------------------------------------------
1127 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1129 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1130 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1132 // The empty region signifies that the shape should be removed from the
1134 if ( region
.IsEmpty() )
1136 wxSize sz
= GetClientSize();
1137 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1138 return SetShape(rgn
);
1141 // Make a copy of the region
1142 RgnHandle shapeRegion
= NewRgn();
1143 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1145 // Dispose of any shape region we may already have
1146 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1150 // Save the region so we can use it later
1151 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1153 // Tell the window manager that the window has changed shape
1154 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1158 // ---------------------------------------------------------------------------
1159 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1160 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1161 // ---------------------------------------------------------------------------
1163 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1165 GetWindowPortBounds(window
, inRect
);
1166 Point pt
= {inRect
->left
, inRect
->top
};
1167 SetPort((GrafPtr
) GetWindowPort(window
));
1170 inRect
->left
= pt
.h
;
1171 inRect
->bottom
+= pt
.v
;
1172 inRect
->right
+= pt
.h
;
1176 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1178 /*------------------------------------------------------
1179 Define which options your custom window supports.
1180 --------------------------------------------------------*/
1181 //just enable everything for our demo
1182 *(OptionBits
*)param
=//kWindowCanGrow|
1184 //kWindowCanCollapse|
1185 //kWindowCanGetWindowRegion|
1186 //kWindowHasTitleBar|
1187 //kWindowSupportsDragHilite|
1188 kWindowCanDrawInCurrentPort
|
1189 //kWindowCanMeasureTitle|
1190 kWindowWantsDisposeAtProcessDeath
|
1191 kWindowSupportsSetGrowImageRegion
|
1192 kWindowDefSupportsColorGrafPort
;
1196 // The content region is left as a rectangle matching the window size, this is
1197 // so the origin in the paint event, and etc. still matches what the
1198 // programmer expects.
1199 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1202 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1205 wxRect r
= win
->GetRect();
1206 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1210 // The structure region is set to the shape given to the SetShape method.
1211 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1213 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1219 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1220 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1221 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1222 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1228 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1230 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1232 switch(rgnRec
->regionCode
)
1234 case kWindowStructureRgn
:
1235 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1237 case kWindowContentRgn
:
1238 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1241 SetEmptyRgn(rgnRec
->winRgn
);
1248 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1250 /*------------------------------------------------------
1251 Determine the region of the window which was hit
1252 --------------------------------------------------------*/
1254 static RgnHandle tempRgn
=nil
;
1259 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1261 //Mac OS 8.5 or later
1262 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1263 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1266 return wNoHit
;//no significant area was hit.
1270 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1274 case kWindowMsgHitTest
:
1275 return wxShapedMacWindowHitTest(window
,param
);
1277 case kWindowMsgGetFeatures
:
1278 return wxShapedMacWindowGetFeatures(window
,param
);
1280 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1281 case kWindowMsgGetRegion
:
1282 return wxShapedMacWindowGetRegion(window
,param
);
1288 // ---------------------------------------------------------------------------