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 wxTopLevelWindow
* tlw
= (wxTopLevelWindow
*) data
;
162 wxWindow
* focus
= wxWindow::FindFocus() ;
170 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
172 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
173 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
174 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
175 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
176 sizeof( Point
), NULL
, &point
);
178 UInt32 message
= (keyCode
<< 8) + charCode
;
179 switch( GetEventKind( event
) )
181 case kEventRawKeyRepeat
:
182 case kEventRawKeyDown
:
184 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
185 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
186 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
187 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
188 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
192 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
195 case kEventRawKeyUp
:
196 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
197 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
202 case kEventRawKeyModifiersChanged
:
204 wxKeyEvent
event(wxEVT_KEY_DOWN
);
206 event
.m_shiftDown
= modifiers
& shiftKey
;
207 event
.m_controlDown
= modifiers
& controlKey
;
208 event
.m_altDown
= modifiers
& optionKey
;
209 event
.m_metaDown
= modifiers
& cmdKey
;
213 event
.m_timeStamp
= when
;
214 wxWindow
* focus
= wxWindow::FindFocus() ;
215 event
.SetEventObject(focus
);
217 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
219 event
.m_keyCode
= WXK_CONTROL
;
220 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
221 focus
->GetEventHandler()->ProcessEvent( event
) ;
223 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
225 event
.m_keyCode
= WXK_SHIFT
;
226 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
227 focus
->GetEventHandler()->ProcessEvent( event
) ;
229 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
231 event
.m_keyCode
= WXK_ALT
;
232 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
233 focus
->GetEventHandler()->ProcessEvent( event
) ;
235 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
237 event
.m_keyCode
= WXK_COMMAND
;
238 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
239 focus
->GetEventHandler()->ProcessEvent( event
) ;
241 wxTheApp
->s_lastModifiers
= modifiers
;
249 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
250 // for windows that we didn't create (like eg Scrollbars in a databrowser) , or for controls where we did not handle the
253 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
255 wxWindow
* g_MacLastWindow
= NULL
;
257 static void SetupMouseEvent( wxMouseEvent
&wxevent
, wxMacCarbonEvent
&cEvent
)
259 UInt32 modifiers
= cEvent
.GetParameter
<UInt32
>(kEventParamKeyModifiers
, typeUInt32
) ;
260 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
262 // this parameter are not given for all events
263 EventMouseButton button
= 0 ;
264 UInt32 clickCount
= 0 ;
265 cEvent
.GetParameter
<EventMouseButton
>(kEventParamMouseButton
, typeMouseButton
, &button
) ;
266 cEvent
.GetParameter
<UInt32
>(kEventParamClickCount
, typeUInt32
, &clickCount
) ;
268 wxevent
.m_x
= screenMouseLocation
.h
;
269 wxevent
.m_y
= screenMouseLocation
.v
;
270 wxevent
.m_shiftDown
= modifiers
& shiftKey
;
271 wxevent
.m_controlDown
= modifiers
& controlKey
;
272 wxevent
.m_altDown
= modifiers
& optionKey
;
273 wxevent
.m_metaDown
= modifiers
& cmdKey
;
274 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
275 // a control click is interpreted as a right click
276 if ( button
== kEventMouseButtonPrimary
&& (modifiers
& controlKey
) )
278 button
= kEventMouseButtonSecondary
;
281 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
283 if ( button
!= 0 && cEvent
.GetKind() != kEventMouseUp
)
287 case kEventMouseButtonPrimary
:
288 wxevent
.m_leftDown
= true ;
290 case kEventMouseButtonSecondary
:
291 wxevent
.m_rightDown
= true ;
293 case kEventMouseButtonTertiary
:
294 wxevent
.m_middleDown
= true ;
298 // determinate the correct click button
299 if ( button
== kEventMouseButtonSecondary
)
301 if (cEvent
.GetKind() == kEventMouseDown
)
302 wxevent
.SetEventType( clickCount
> 1 ? wxEVT_RIGHT_DCLICK
: wxEVT_RIGHT_DOWN
) ;
303 else if ( cEvent
.GetKind() == kEventMouseUp
)
304 wxevent
.SetEventType(wxEVT_RIGHT_UP
) ;
306 else if ( button
== kEventMouseButtonTertiary
)
308 if (cEvent
.GetKind() == kEventMouseDown
)
309 wxevent
.SetEventType(clickCount
> 1 ? wxEVT_MIDDLE_DCLICK
: wxEVT_MIDDLE_DOWN
) ;
310 else if ( cEvent
.GetKind() == kEventMouseUp
)
311 wxevent
.SetEventType(wxEVT_MIDDLE_UP
) ;
315 if (cEvent
.GetKind() == kEventMouseDown
)
316 wxevent
.SetEventType(clickCount
> 1 ? wxEVT_LEFT_DCLICK
: wxEVT_LEFT_DOWN
) ;
317 else if ( cEvent
.GetKind() == kEventMouseUp
)
318 wxevent
.SetEventType(wxEVT_LEFT_UP
) ;
319 else if ( cEvent
.GetKind() == kEventMouseWheelMoved
)
321 wxevent
.SetEventType(wxEVT_MOUSEWHEEL
) ;
323 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
324 SInt32 delta
= cEvent
.GetParameter
<SInt32
>(kEventParamMouseWheelDelta
, typeLongInteger
) ;
326 wxevent
.m_wheelRotation
= delta
;
327 wxevent
.m_wheelDelta
= 1;
328 wxevent
.m_linesPerAction
= 1;
331 wxevent
.SetEventType(wxEVT_MOTION
) ;
335 ControlRef
wxMacFindSubControl( Point location
, ControlRef superControl
, ControlPartCode
*outPart
)
339 UInt16 childrenCount
= 0 ;
340 OSStatus err
= CountSubControls( superControl
, &childrenCount
) ;
341 if ( err
== errControlIsNotEmbedder
)
343 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
345 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
347 ControlHandle sibling
;
348 err
= GetIndexedSubControl( superControl
, i
, & sibling
) ;
349 if ( err
== errControlIsNotEmbedder
)
352 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
353 if ( IsControlVisible( sibling
) )
356 GetControlBounds( sibling
, &r
) ;
357 if ( MacPtInRect( location
, &r
) )
359 ControlHandle child
= wxMacFindSubControl( location
, sibling
, outPart
) ;
364 *outPart
= TestControl( sibling
, location
) ;
374 ControlRef
wxMacFindControlUnderMouse( Point location
, WindowRef window
, ControlPartCode
*outPart
)
376 #if TARGET_API_MAC_OSX
377 return FindControlUnderMouse( location
, window
, outPart
) ;
379 ControlRef rootControl
= NULL
;
380 verify_noerr( GetRootControl( window
, &rootControl
) ) ;
381 return wxMacFindSubControl( location
, rootControl
, outPart
) ;
384 pascal OSStatus
wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
387 OSStatus result
= eventNotHandledErr
;
389 wxMacCarbonEvent
cEvent( event
) ;
391 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
392 Point windowMouseLocation
= screenMouseLocation
;
395 short windowPart
= ::FindWindow(screenMouseLocation
, &window
);
397 wxWindow
* currentMouseWindow
= NULL
;
400 // calculate window relative coordinates
403 ::SetPort( UMAGetWindowPort(window
) ) ;
404 ::GlobalToLocal( &windowMouseLocation
) ;
407 if ( wxTheApp
->s_captureWindow
&& wxTheApp
->s_captureWindow
->MacGetTopLevelWindowRef() == (WXWindow
) window
&& windowPart
== inContent
)
409 currentMouseWindow
= wxTheApp
->s_captureWindow
;
411 else if ( (IsWindowActive(window
) && windowPart
== inContent
) )
413 ControlPartCode part
;
414 ControlRef control
= wxMacFindControlUnderMouse( windowMouseLocation
, window
, &part
) ;
416 currentMouseWindow
= (wxWindow
*) data
;
418 currentMouseWindow
= wxFindControlFromMacControl( control
) ;
422 wxMouseEvent
wxevent(wxEVT_LEFT_DOWN
);
423 SetupMouseEvent( wxevent
, cEvent
) ;
425 // handle all enter / leave events
427 if ( currentMouseWindow
!= g_MacLastWindow
)
429 if ( g_MacLastWindow
)
431 wxMouseEvent
eventleave(wxevent
);
432 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
433 g_MacLastWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
434 eventleave
.SetEventObject( g_MacLastWindow
) ;
437 wxToolTip::RelayEvent( g_MacLastWindow
, eventleave
);
438 #endif // wxUSE_TOOLTIPS
439 g_MacLastWindow
->GetEventHandler()->ProcessEvent(eventleave
);
441 if ( currentMouseWindow
)
443 wxMouseEvent
evententer(wxevent
);
444 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
445 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
446 evententer
.SetEventObject( currentMouseWindow
) ;
448 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
449 #endif // wxUSE_TOOLTIPS
450 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
452 g_MacLastWindow
= currentMouseWindow
;
455 if ( windowPart
== inMenuBar
)
457 // special case menu bar, as we are having a low-level runloop we must do it ourselves
458 if ( cEvent
.GetKind() == kEventMouseDown
)
460 ::MenuSelect( screenMouseLocation
) ;
463 } // if ( windowPart == inMenuBar )
464 else if ( currentMouseWindow
)
466 currentMouseWindow
->ScreenToClient( &wxevent
.m_x
, &wxevent
.m_y
) ;
468 wxevent
.SetEventObject( currentMouseWindow
) ;
472 wxWindow
* cursorTarget
= currentMouseWindow
;
473 wxPoint
cursorPoint( wxevent
.m_x
, wxevent
.m_y
) ;
475 while( cursorTarget
&& !cursorTarget
->MacSetupCursor( cursorPoint
) )
477 cursorTarget
= cursorTarget
->GetParent() ;
479 cursorPoint
+= cursorTarget
->GetPosition() ;
484 if ( wxevent
.GetEventType() == wxEVT_LEFT_DOWN
)
486 // set focus to this window
487 if (currentMouseWindow
->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow
)
488 currentMouseWindow
->SetFocus();
491 // make tooltips current
494 if ( wxevent
.GetEventType() == wxEVT_MOTION
495 || wxevent
.GetEventType() == wxEVT_ENTER_WINDOW
496 || wxevent
.GetEventType() == wxEVT_LEAVE_WINDOW
)
497 wxToolTip::RelayEvent( currentMouseWindow
, wxevent
);
498 #endif // wxUSE_TOOLTIPS
499 if ( currentMouseWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
501 if ( cEvent
.GetKind() == kEventMouseUp
&& wxTheApp
->s_captureWindow
)
503 wxTheApp
->s_captureWindow
= NULL
;
506 } // else if ( currentMouseWindow )
510 static pascal OSStatus
wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
512 OSStatus result
= eventNotHandledErr
;
514 wxMacCarbonEvent
cEvent( event
) ;
516 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
517 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
519 switch( GetEventKind( event
) )
521 case kEventWindowActivated
:
523 toplevelWindow
->MacActivate( cEvent
.GetTicks() , true) ;
524 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, true , toplevelWindow
->GetId());
525 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
526 wxevent
.SetEventObject(toplevelWindow
);
527 toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
);
528 // we still sending an eventNotHandledErr in order to allow for default processing
531 case kEventWindowDeactivated
:
533 toplevelWindow
->MacActivate(cEvent
.GetTicks() , false) ;
534 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, false , toplevelWindow
->GetId());
535 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
536 wxevent
.SetEventObject(toplevelWindow
);
537 toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
);
538 // we still sending an eventNotHandledErr in order to allow for default processing
541 case kEventWindowShown
:
542 toplevelWindow
->Refresh() ;
545 case kEventWindowClose
:
546 toplevelWindow
->Close() ;
549 case kEventWindowBoundsChanged
:
551 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
552 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
553 wxRect
r( newRect
.left
, newRect
.top
, newRect
.right
- newRect
.left
, newRect
.bottom
- newRect
.top
) ;
554 if ( attributes
& kWindowBoundsChangeSizeChanged
)
556 // according to the other ports we handle this within the OS level
557 // resize event, not within a wxSizeEvent
558 wxFrame
*frame
= wxDynamicCast( toplevelWindow
, wxFrame
) ;
562 frame
->PositionStatusBar();
565 frame
->PositionToolBar();
569 wxSizeEvent
event( r
.GetSize() , toplevelWindow
->GetId() ) ;
570 event
.SetEventObject( toplevelWindow
) ;
572 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
574 if ( attributes
& kWindowBoundsChangeOriginChanged
)
576 wxMoveEvent
event( r
.GetLeftTop() , toplevelWindow
->GetId() ) ;
577 event
.SetEventObject( toplevelWindow
) ;
578 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
583 case kEventWindowBoundsChanging
:
585 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
586 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
588 if ( (attributes
& kWindowBoundsChangeSizeChanged
) || (attributes
& kWindowBoundsChangeOriginChanged
) )
590 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
591 int left
, top
, right
, bottom
;
592 toplevelWindow
->MacGetContentAreaInset( left
, top
, right
, bottom
) ;
593 wxRect
r( newRect
.left
- left
, newRect
.top
- top
,
594 newRect
.right
- newRect
.left
+ left
+ right
, newRect
.bottom
- newRect
.top
+ top
+ bottom
) ;
595 // this is a EVT_SIZING not a EVT_SIZE type !
596 wxSizeEvent
wxevent( r
, toplevelWindow
->GetId() ) ;
597 wxevent
.SetEventObject( toplevelWindow
) ;
599 if ( toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
600 adjustR
= wxevent
.GetRect() ;
602 if ( toplevelWindow
->GetMaxWidth() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxWidth() )
603 adjustR
.SetWidth( toplevelWindow
->GetMaxWidth() ) ;
604 if ( toplevelWindow
->GetMaxHeight() != -1 && adjustR
.GetHeight() > toplevelWindow
->GetMaxHeight() )
605 adjustR
.SetHeight( toplevelWindow
->GetMaxHeight() ) ;
606 if ( toplevelWindow
->GetMinWidth() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinWidth() )
607 adjustR
.SetWidth( toplevelWindow
->GetMinWidth() ) ;
608 if ( toplevelWindow
->GetMinHeight() != -1 && adjustR
.GetHeight() < toplevelWindow
->GetMinHeight() )
609 adjustR
.SetHeight( toplevelWindow
->GetMinHeight() ) ;
610 Rect adjustedRect
= { adjustR
.y
+ top
, adjustR
.x
+ left
, adjustR
.y
+ adjustR
.height
- bottom
, adjustR
.x
+ adjustR
.width
- right
} ;
611 if ( !EqualRect( &newRect
, &adjustedRect
) )
612 cEvent
.SetParameter( kEventParamCurrentBounds
, &adjustedRect
) ;
624 pascal OSStatus
wxMacTopLevelEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
626 OSStatus result
= eventNotHandledErr
;
628 switch ( GetEventClass( event
) )
630 case kEventClassKeyboard
:
631 result
= KeyboardEventHandler( handler
, event
, data
) ;
633 case kEventClassTextInput
:
634 result
= TextInputEventHandler( handler
, event
, data
) ;
636 case kEventClassWindow
:
637 result
= wxMacTopLevelWindowEventHandler( handler
, event
, data
) ;
639 case kEventClassMouse
:
640 result
= wxMacTopLevelMouseEventHandler( handler
, event
, data
) ;
648 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler
)
650 // ---------------------------------------------------------------------------
651 // wxWindowMac utility functions
652 // ---------------------------------------------------------------------------
654 // Find an item given the Macintosh Window Reference
656 wxList
wxWinMacWindowList(wxKEY_INTEGER
);
657 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
659 wxNode
*node
= wxWinMacWindowList
.Find((long)inWindowRef
);
662 return (wxTopLevelWindowMac
*)node
->GetData();
665 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
) ;
666 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
668 // adding NULL WindowRef is (first) surely a result of an error and
669 // (secondly) breaks menu command processing
670 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
672 if ( !wxWinMacWindowList
.Find((long)inWindowRef
) )
673 wxWinMacWindowList
.Append((long)inWindowRef
, win
);
676 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
) ;
677 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
679 wxWinMacWindowList
.DeleteObject(win
);
683 // ----------------------------------------------------------------------------
684 // wxTopLevelWindowMac creation
685 // ----------------------------------------------------------------------------
687 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
689 void wxTopLevelWindowMac::Init()
692 m_maximizeOnShow
= FALSE
;
694 #if TARGET_API_MAC_OSX
695 m_macUsesCompositing
= TRUE
;
697 m_macUsesCompositing
= FALSE
;
699 m_macEventHandler
= NULL
;
702 class wxMacDeferredWindowDeleter
: public wxObject
705 wxMacDeferredWindowDeleter( WindowRef windowRef
)
707 m_macWindow
= windowRef
;
709 virtual ~wxMacDeferredWindowDeleter()
711 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
714 WindowRef m_macWindow
;
717 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
719 const wxString
& title
,
723 const wxString
& name
)
728 style
= style
& ~wxFRAME_SHAPED
;
730 m_windowStyle
= style
;
734 m_windowId
= id
== -1 ? NewControlId() : id
;
736 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
738 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
740 wxTopLevelWindows
.Append(this);
743 parent
->AddChild(this);
748 wxTopLevelWindowMac::~wxTopLevelWindowMac()
752 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
753 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
756 if ( m_macEventHandler
)
758 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
759 m_macEventHandler
= NULL
;
762 wxRemoveMacWindowAssociation( this ) ;
764 if ( wxModelessWindows
.Find(this) )
765 wxModelessWindows
.DeleteObject(this);
769 // ----------------------------------------------------------------------------
770 // wxTopLevelWindowMac maximize/minimize
771 // ----------------------------------------------------------------------------
773 void wxTopLevelWindowMac::Maximize(bool maximize
)
775 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
776 wxMacWindowClipper
clip (this);
777 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
780 GDHandle device = NULL ;
781 verify_noerr( GetWindowGreatestAreaDevice( (WindowRef) m_macWindow , kWindowContentRgn ,
783 verify_noerr( GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ) ;
788 Point pt = { 0, 0 } ;
789 SetPortWindowPort((WindowRef)m_macWindow) ;
790 LocalToGlobal( &pt ) ;
793 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
794 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
795 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
799 bool wxTopLevelWindowMac::IsMaximized() const
801 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
804 void wxTopLevelWindowMac::Iconize(bool iconize
)
806 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
807 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
810 bool wxTopLevelWindowMac::IsIconized() const
812 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
815 void wxTopLevelWindowMac::Restore()
817 // not available on mac
820 // ----------------------------------------------------------------------------
821 // wxTopLevelWindowMac misc
822 // ----------------------------------------------------------------------------
824 wxPoint
wxTopLevelWindowMac::GetClientAreaOrigin() const
826 return wxPoint(0,0) ;
829 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
832 wxTopLevelWindowBase::SetIcon(icon
);
835 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush
&brush
)
837 wxTopLevelWindowBase::MacSetBackgroundBrush( brush
) ;
839 if ( m_macBackgroundBrush
.Ok() && m_macBackgroundBrush
.GetStyle() != wxTRANSPARENT
&& m_macBackgroundBrush
.MacGetBrushKind() == kwxMacBrushTheme
)
841 SetThemeWindowBackground( (WindowRef
) m_macWindow
, m_macBackgroundBrush
.MacGetTheme() , false ) ;
845 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
847 if ( m_macEventHandler
!= NULL
)
849 verify_noerr( ::RemoveEventHandler( (EventHandlerRef
) m_macEventHandler
) ) ;
851 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacTopLevelEventHandlerUPP(),
852 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
855 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
859 const wxString
& name
)
861 OSStatus err
= noErr
;
863 m_windowStyle
= style
;
877 int w
= WidthDefault(size
.x
);
878 int h
= HeightDefault(size
.y
);
880 ::SetRect(&theBoundsRect
, x
, y
, x
+ w
, y
+ h
);
882 // translate the window attributes in the appropriate window class and attributes
884 WindowClass wclass
= 0;
885 WindowAttributes attr
= kWindowNoAttributes
;
887 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
890 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
891 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
892 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
895 wclass
= kFloatingWindowClass
;
896 if ( HasFlag(wxTINY_CAPTION_VERT
) )
898 attr
|= kWindowSideTitlebarAttribute
;
903 wclass
= kPlainWindowClass
;
906 else if ( HasFlag( wxCAPTION
) )
908 wclass
= kDocumentWindowClass
;
912 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
913 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
915 wclass
= kDocumentWindowClass
;
919 wclass
= kPlainWindowClass
;
923 if ( HasFlag( wxMINIMIZE_BOX
) )
925 attr
|= kWindowCollapseBoxAttribute
;
927 if ( HasFlag( wxMAXIMIZE_BOX
) )
929 attr
|= kWindowFullZoomAttribute
;
931 if ( HasFlag( wxRESIZE_BORDER
) )
933 attr
|= kWindowResizableAttribute
;
935 if ( HasFlag( wxCLOSE_BOX
) )
937 attr
|= kWindowCloseBoxAttribute
;
940 if (UMAGetSystemVersion() >= 0x1000)
942 // turn on live resizing (OS X only)
943 attr
|= kWindowLiveResizeAttribute
;
946 if (HasFlag(wxSTAY_ON_TOP
))
947 wclass
= kUtilityWindowClass
;
949 #if TARGET_API_MAC_OSX
950 attr
|= kWindowCompositingAttribute
;
953 if ( HasFlag(wxFRAME_SHAPED
) )
955 WindowDefSpec customWindowDefSpec
;
956 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
957 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
959 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
960 attr
, &theBoundsRect
,
961 (WindowRef
*) &m_macWindow
);
965 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
968 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
969 wxAssociateWinWithMacWindow( (WindowRef
) m_macWindow
, this ) ;
970 UMASetWTitle( (WindowRef
) m_macWindow
, title
, m_font
.GetEncoding() ) ;
971 if ( m_macUsesCompositing
)
973 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
977 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
979 // the root control level handleer
980 MacInstallEventHandler() ;
982 // the frame window event handler
983 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
984 MacInstallTopLevelWindowEventHandler() ;
988 if ( HasFlag(wxFRAME_SHAPED
) )
990 // default shape matches the window size
991 wxRegion
rgn(0, 0, w
, h
);
995 wxWindowCreateEvent
event(this);
996 GetEventHandler()->ProcessEvent(event
);
999 void wxTopLevelWindowMac::ClearBackground()
1001 wxWindow::ClearBackground() ;
1004 // Raise the window to the top of the Z order
1005 void wxTopLevelWindowMac::Raise()
1007 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1010 // Lower the window to the bottom of the Z order
1011 void wxTopLevelWindowMac::Lower()
1013 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
1017 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1019 if(s_macDeactivateWindow
)
1021 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1022 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1026 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1028 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1030 if(s_macDeactivateWindow
==this)
1031 s_macDeactivateWindow
=NULL
;
1032 MacDelayedDeactivation(timestamp
);
1033 MacPropagateHiliteChanged() ;
1036 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1038 wxWindow::SetTitle( title
) ;
1039 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
1042 bool wxTopLevelWindowMac::Show(bool show
)
1044 if ( !wxTopLevelWindowBase::Show(show
) )
1049 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1050 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1052 ::ShowWindow( (WindowRef
)m_macWindow
);
1057 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1059 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1060 // as apps expect a size event to occur at this moment
1061 wxSizeEvent
event( GetSize() , m_windowId
);
1062 event
.SetEventObject(this);
1063 GetEventHandler()->ProcessEvent(event
);
1067 #if wxUSE_SYSTEM_OPTIONS
1068 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1070 ::HideWindow((WindowRef
) m_macWindow
);
1075 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1079 MacPropagateVisibilityChanged() ;
1084 // we are still using coordinates of the content view, todo switch to structure bounds
1086 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left
, int &top
, int &right
, int &bottom
)
1090 GetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &structure
) ;
1091 GetWindowBounds( (WindowRef
) m_macWindow
, kWindowContentRgn
, &content
) ;
1093 left
= content
.left
- structure
.left
;
1094 top
= content
.top
- structure
.top
;
1095 right
= structure
.right
- content
.right
;
1096 bottom
= structure
.bottom
- content
.bottom
;
1099 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1101 Rect bounds
= { y
, x
, y
+ height
, x
+ width
} ;
1102 verify_noerr(SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1105 void wxTopLevelWindowMac::DoGetPosition( int *x
, int *y
) const
1108 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1109 if(x
) *x
= bounds
.left
;
1110 if(y
) *y
= bounds
.top
;
1112 void wxTopLevelWindowMac::DoGetSize( int *width
, int *height
) const
1115 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1116 if(width
) *width
= bounds
.right
- bounds
.left
;
1117 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1120 void wxTopLevelWindowMac::DoGetClientSize( int *width
, int *height
) const
1123 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowContentRgn
, &bounds
)) ;
1124 if(width
) *width
= bounds
.right
- bounds
.left
;
1125 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1128 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set
)
1130 #if TARGET_API_MAC_OSX
1132 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1133 wxASSERT_MSG( attr
& kWindowCompositingAttribute
,
1134 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1136 MacChangeWindowAttributes( set
? kWindowMetalAttribute
: kWindowNoAttributes
,
1137 set
? kWindowNoAttributes
: kWindowMetalAttribute
) ;
1141 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1143 #if TARGET_API_MAC_OSX
1144 return MacGetWindowAttributes() & kWindowMetalAttribute
;
1150 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet
, wxUint32 attributesToClear
)
1152 ChangeWindowAttributes ( (WindowRef
) m_macWindow
, attributesToSet
, attributesToClear
) ;
1155 wxUint32
wxTopLevelWindowMac::MacGetWindowAttributes() const
1158 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1162 // ---------------------------------------------------------------------------
1163 // Shape implementation
1164 // ---------------------------------------------------------------------------
1167 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1169 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1170 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1172 // The empty region signifies that the shape should be removed from the
1174 if ( region
.IsEmpty() )
1176 wxSize sz
= GetClientSize();
1177 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1178 return SetShape(rgn
);
1181 // Make a copy of the region
1182 RgnHandle shapeRegion
= NewRgn();
1183 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1185 // Dispose of any shape region we may already have
1186 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1190 // Save the region so we can use it later
1191 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1193 // Tell the window manager that the window has changed shape
1194 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1198 // ---------------------------------------------------------------------------
1199 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1200 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1201 // ---------------------------------------------------------------------------
1203 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1205 GetWindowPortBounds(window
, inRect
);
1206 Point pt
= {inRect
->left
, inRect
->top
};
1207 SetPort((GrafPtr
) GetWindowPort(window
));
1210 inRect
->left
= pt
.h
;
1211 inRect
->bottom
+= pt
.v
;
1212 inRect
->right
+= pt
.h
;
1216 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1218 /*------------------------------------------------------
1219 Define which options your custom window supports.
1220 --------------------------------------------------------*/
1221 //just enable everything for our demo
1222 *(OptionBits
*)param
=//kWindowCanGrow|
1224 //kWindowCanCollapse|
1225 //kWindowCanGetWindowRegion|
1226 //kWindowHasTitleBar|
1227 //kWindowSupportsDragHilite|
1228 kWindowCanDrawInCurrentPort
|
1229 //kWindowCanMeasureTitle|
1230 kWindowWantsDisposeAtProcessDeath
|
1231 kWindowSupportsSetGrowImageRegion
|
1232 kWindowDefSupportsColorGrafPort
;
1236 // The content region is left as a rectangle matching the window size, this is
1237 // so the origin in the paint event, and etc. still matches what the
1238 // programmer expects.
1239 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1242 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1246 wxShapedMacWindowGetPos(window
, &r
) ;
1247 RectRgn( rgn
, &r
) ;
1251 // The structure region is set to the shape given to the SetShape method.
1252 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1254 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1260 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1261 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1262 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1263 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1269 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1271 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1273 switch(rgnRec
->regionCode
)
1275 case kWindowStructureRgn
:
1276 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1278 case kWindowContentRgn
:
1279 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1282 SetEmptyRgn(rgnRec
->winRgn
);
1289 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1291 /*------------------------------------------------------
1292 Determine the region of the window which was hit
1293 --------------------------------------------------------*/
1295 static RgnHandle tempRgn
=nil
;
1300 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1302 //Mac OS 8.5 or later
1303 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1304 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1307 return wNoHit
;//no significant area was hit.
1311 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1315 case kWindowMsgHitTest
:
1316 return wxShapedMacWindowHitTest(window
,param
);
1318 case kWindowMsgGetFeatures
:
1319 return wxShapedMacWindowGetFeatures(window
,param
);
1321 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1322 case kWindowMsgGetRegion
:
1323 return wxShapedMacWindowGetRegion(window
,param
);
1329 // ---------------------------------------------------------------------------