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
) ;
583 if ( (attributes
& kWindowBoundsChangeSizeChanged
) || (attributes
& kWindowBoundsChangeOriginChanged
) )
585 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
586 int left
, top
, right
, bottom
;
587 toplevelWindow
->MacGetContentAreaInset( left
, top
, right
, bottom
) ;
588 wxRect
r( newRect
.left
- left
, newRect
.top
- top
,
589 newRect
.right
- newRect
.left
+ left
+ right
, newRect
.bottom
- newRect
.top
+ top
+ bottom
) ;
590 // this is a EVT_SIZING not a EVT_SIZE type !
591 wxSizeEvent
wxevent( r
, toplevelWindow
->GetId() ) ;
592 wxevent
.SetEventObject( toplevelWindow
) ;
594 if ( toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
595 adjustR
= wxevent
.GetRect() ;
597 if ( toplevelWindow
->GetMaxWidth() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxWidth() )
598 adjustR
.SetWidth( toplevelWindow
->GetMaxWidth() ) ;
599 if ( toplevelWindow
->GetMaxHeight() != -1 && adjustR
.GetHeight() > toplevelWindow
->GetMaxHeight() )
600 adjustR
.SetHeight( toplevelWindow
->GetMaxHeight() ) ;
601 if ( toplevelWindow
->GetMinWidth() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinWidth() )
602 adjustR
.SetWidth( toplevelWindow
->GetMinWidth() ) ;
603 if ( toplevelWindow
->GetMinHeight() != -1 && adjustR
.GetHeight() < toplevelWindow
->GetMinHeight() )
604 adjustR
.SetHeight( toplevelWindow
->GetMinHeight() ) ;
605 Rect adjustedRect
= { adjustR
.y
+ top
, adjustR
.x
+ left
, adjustR
.y
+ adjustR
.height
- bottom
, adjustR
.x
+ adjustR
.width
- right
} ;
606 if ( !EqualRect( &newRect
, &adjustedRect
) )
607 cEvent
.SetParameter( kEventParamCurrentBounds
, &adjustedRect
) ;
619 pascal OSStatus
wxMacTopLevelEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
621 OSStatus result
= eventNotHandledErr
;
623 switch ( GetEventClass( event
) )
625 case kEventClassKeyboard
:
626 result
= KeyboardEventHandler( handler
, event
, data
) ;
628 case kEventClassTextInput
:
629 result
= TextInputEventHandler( handler
, event
, data
) ;
631 case kEventClassWindow
:
632 result
= wxMacTopLevelWindowEventHandler( handler
, event
, data
) ;
634 case kEventClassMouse
:
635 result
= wxMacTopLevelMouseEventHandler( handler
, event
, data
) ;
643 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler
)
645 // ---------------------------------------------------------------------------
646 // wxWindowMac utility functions
647 // ---------------------------------------------------------------------------
649 // Find an item given the Macintosh Window Reference
651 wxList
wxWinMacWindowList(wxKEY_INTEGER
);
652 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
654 wxNode
*node
= wxWinMacWindowList
.Find((long)inWindowRef
);
657 return (wxTopLevelWindowMac
*)node
->GetData();
660 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
) ;
661 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
663 // adding NULL WindowRef is (first) surely a result of an error and
664 // (secondly) breaks menu command processing
665 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
667 if ( !wxWinMacWindowList
.Find((long)inWindowRef
) )
668 wxWinMacWindowList
.Append((long)inWindowRef
, win
);
671 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
) ;
672 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
674 wxWinMacWindowList
.DeleteObject(win
);
678 // ----------------------------------------------------------------------------
679 // wxTopLevelWindowMac creation
680 // ----------------------------------------------------------------------------
682 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
684 void wxTopLevelWindowMac::Init()
687 m_maximizeOnShow
= FALSE
;
689 #if TARGET_API_MAC_OSX
690 m_macUsesCompositing
= TRUE
;
692 m_macUsesCompositing
= FALSE
;
694 m_macEventHandler
= NULL
;
697 class wxMacDeferredWindowDeleter
: public wxObject
700 wxMacDeferredWindowDeleter( WindowRef windowRef
)
702 m_macWindow
= windowRef
;
704 virtual ~wxMacDeferredWindowDeleter()
706 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
709 WindowRef m_macWindow
;
712 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
714 const wxString
& title
,
718 const wxString
& name
)
723 style
= style
& ~wxFRAME_SHAPED
;
725 m_windowStyle
= style
;
729 m_windowId
= id
== -1 ? NewControlId() : id
;
731 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
733 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
735 wxTopLevelWindows
.Append(this);
738 parent
->AddChild(this);
743 wxTopLevelWindowMac::~wxTopLevelWindowMac()
747 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
748 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
751 if ( m_macEventHandler
)
753 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
754 m_macEventHandler
= NULL
;
757 wxRemoveMacWindowAssociation( this ) ;
759 if ( wxModelessWindows
.Find(this) )
760 wxModelessWindows
.DeleteObject(this);
764 // ----------------------------------------------------------------------------
765 // wxTopLevelWindowMac maximize/minimize
766 // ----------------------------------------------------------------------------
768 void wxTopLevelWindowMac::Maximize(bool maximize
)
770 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
771 wxMacWindowClipper
clip (this);
772 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
775 GDHandle device = NULL ;
776 verify_noerr( GetWindowGreatestAreaDevice( (WindowRef) m_macWindow , kWindowContentRgn ,
778 verify_noerr( GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ) ;
783 Point pt = { 0, 0 } ;
784 SetPortWindowPort((WindowRef)m_macWindow) ;
785 LocalToGlobal( &pt ) ;
788 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
789 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
790 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
794 bool wxTopLevelWindowMac::IsMaximized() const
796 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
799 void wxTopLevelWindowMac::Iconize(bool iconize
)
801 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
802 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
805 bool wxTopLevelWindowMac::IsIconized() const
807 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
810 void wxTopLevelWindowMac::Restore()
812 // not available on mac
815 // ----------------------------------------------------------------------------
816 // wxTopLevelWindowMac misc
817 // ----------------------------------------------------------------------------
819 wxPoint
wxTopLevelWindowMac::GetClientAreaOrigin() const
821 return wxPoint(0,0) ;
824 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
827 wxTopLevelWindowBase::SetIcon(icon
);
830 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush
&brush
)
832 wxTopLevelWindowBase::MacSetBackgroundBrush( brush
) ;
834 if ( m_macBackgroundBrush
.Ok() && m_macBackgroundBrush
.GetStyle() != wxTRANSPARENT
&& m_macBackgroundBrush
.MacGetBrushKind() == kwxMacBrushTheme
)
836 SetThemeWindowBackground( (WindowRef
) m_macWindow
, m_macBackgroundBrush
.MacGetTheme() , false ) ;
840 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
842 if ( m_macEventHandler
!= NULL
)
844 verify_noerr( ::RemoveEventHandler( (EventHandlerRef
) m_macEventHandler
) ) ;
846 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacTopLevelEventHandlerUPP(),
847 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
850 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
854 const wxString
& name
)
856 OSStatus err
= noErr
;
858 m_windowStyle
= style
;
872 int w
= WidthDefault(size
.x
);
873 int h
= HeightDefault(size
.y
);
875 ::SetRect(&theBoundsRect
, x
, y
, x
+ w
, y
+ h
);
877 // translate the window attributes in the appropriate window class and attributes
879 WindowClass wclass
= 0;
880 WindowAttributes attr
= kWindowNoAttributes
;
882 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
885 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
886 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
887 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
890 wclass
= kFloatingWindowClass
;
891 if ( HasFlag(wxTINY_CAPTION_VERT
) )
893 attr
|= kWindowSideTitlebarAttribute
;
898 wclass
= kPlainWindowClass
;
901 else if ( HasFlag( wxCAPTION
) )
903 wclass
= kDocumentWindowClass
;
907 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
908 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
910 wclass
= kDocumentWindowClass
;
914 wclass
= kPlainWindowClass
;
918 if ( HasFlag( wxMINIMIZE_BOX
) )
920 attr
|= kWindowCollapseBoxAttribute
;
922 if ( HasFlag( wxMAXIMIZE_BOX
) )
924 attr
|= kWindowFullZoomAttribute
;
926 if ( HasFlag( wxRESIZE_BORDER
) )
928 attr
|= kWindowResizableAttribute
;
930 if ( HasFlag( wxCLOSE_BOX
) )
932 attr
|= kWindowCloseBoxAttribute
;
935 if (UMAGetSystemVersion() >= 0x1000)
937 // turn on live resizing (OS X only)
938 attr
|= kWindowLiveResizeAttribute
;
941 if (HasFlag(wxSTAY_ON_TOP
))
942 wclass
= kUtilityWindowClass
;
944 #if TARGET_API_MAC_OSX
945 attr
|= kWindowCompositingAttribute
;
948 if ( HasFlag(wxFRAME_SHAPED
) )
950 WindowDefSpec customWindowDefSpec
;
951 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
952 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
954 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
955 attr
, &theBoundsRect
,
956 (WindowRef
*) &m_macWindow
);
960 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
963 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
964 wxAssociateWinWithMacWindow( (WindowRef
) m_macWindow
, this ) ;
965 UMASetWTitle( (WindowRef
) m_macWindow
, title
, m_font
.GetEncoding() ) ;
966 if ( m_macUsesCompositing
)
968 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
972 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
974 // the root control level handleer
975 MacInstallEventHandler() ;
977 // the frame window event handler
978 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
979 MacInstallTopLevelWindowEventHandler() ;
983 if ( HasFlag(wxFRAME_SHAPED
) )
985 // default shape matches the window size
986 wxRegion
rgn(0, 0, w
, h
);
990 wxWindowCreateEvent
event(this);
991 GetEventHandler()->ProcessEvent(event
);
994 void wxTopLevelWindowMac::ClearBackground()
996 wxWindow::ClearBackground() ;
999 // Raise the window to the top of the Z order
1000 void wxTopLevelWindowMac::Raise()
1002 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1005 // Lower the window to the bottom of the Z order
1006 void wxTopLevelWindowMac::Lower()
1008 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
1012 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1014 if(s_macDeactivateWindow
)
1016 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1017 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1021 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1023 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1025 if(s_macDeactivateWindow
==this)
1026 s_macDeactivateWindow
=NULL
;
1027 MacDelayedDeactivation(timestamp
);
1028 MacPropagateHiliteChanged() ;
1031 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1033 wxWindow::SetTitle( title
) ;
1034 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
1037 bool wxTopLevelWindowMac::Show(bool show
)
1039 if ( !wxTopLevelWindowBase::Show(show
) )
1044 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1045 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1047 ::ShowWindow( (WindowRef
)m_macWindow
);
1052 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1054 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1055 // as apps expect a size event to occur at this moment
1056 wxSizeEvent
event( GetSize() , m_windowId
);
1057 event
.SetEventObject(this);
1058 GetEventHandler()->ProcessEvent(event
);
1062 #if wxUSE_SYSTEM_OPTIONS
1063 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1065 ::HideWindow((WindowRef
) m_macWindow
);
1070 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1074 MacPropagateVisibilityChanged() ;
1079 // we are still using coordinates of the content view, todo switch to structure bounds
1081 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left
, int &top
, int &right
, int &bottom
)
1085 GetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &structure
) ;
1086 GetWindowBounds( (WindowRef
) m_macWindow
, kWindowContentRgn
, &content
) ;
1088 left
= content
.left
- structure
.left
;
1089 top
= content
.top
- structure
.top
;
1090 right
= structure
.right
- content
.right
;
1091 bottom
= structure
.bottom
- content
.bottom
;
1094 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1096 Rect bounds
= { y
, x
, y
+ height
, x
+ width
} ;
1097 verify_noerr(SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1100 void wxTopLevelWindowMac::DoGetPosition( int *x
, int *y
) const
1103 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1104 if(x
) *x
= bounds
.left
;
1105 if(y
) *y
= bounds
.top
;
1107 void wxTopLevelWindowMac::DoGetSize( int *width
, int *height
) const
1110 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1111 if(width
) *width
= bounds
.right
- bounds
.left
;
1112 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1115 void wxTopLevelWindowMac::DoGetClientSize( int *width
, int *height
) const
1118 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowContentRgn
, &bounds
)) ;
1119 if(width
) *width
= bounds
.right
- bounds
.left
;
1120 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1123 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set
)
1125 #if TARGET_API_MAC_OSX
1127 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1128 wxASSERT_MSG( attr
& kWindowCompositingAttribute
,
1129 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1131 MacChangeWindowAttributes( set
? kWindowMetalAttribute
: kWindowNoAttributes
,
1132 set
? kWindowNoAttributes
: kWindowMetalAttribute
) ;
1136 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1138 #if TARGET_API_MAC_OSX
1139 return MacGetWindowAttributes() & kWindowMetalAttribute
;
1145 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet
, wxUint32 attributesToClear
)
1147 ChangeWindowAttributes ( (WindowRef
) m_macWindow
, attributesToSet
, attributesToClear
) ;
1150 wxUint32
wxTopLevelWindowMac::MacGetWindowAttributes() const
1153 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1157 // ---------------------------------------------------------------------------
1158 // Shape implementation
1159 // ---------------------------------------------------------------------------
1162 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1164 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1165 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1167 // The empty region signifies that the shape should be removed from the
1169 if ( region
.IsEmpty() )
1171 wxSize sz
= GetClientSize();
1172 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1173 return SetShape(rgn
);
1176 // Make a copy of the region
1177 RgnHandle shapeRegion
= NewRgn();
1178 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1180 // Dispose of any shape region we may already have
1181 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1185 // Save the region so we can use it later
1186 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1188 // Tell the window manager that the window has changed shape
1189 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1193 // ---------------------------------------------------------------------------
1194 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1195 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1196 // ---------------------------------------------------------------------------
1198 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1200 GetWindowPortBounds(window
, inRect
);
1201 Point pt
= {inRect
->left
, inRect
->top
};
1202 SetPort((GrafPtr
) GetWindowPort(window
));
1205 inRect
->left
= pt
.h
;
1206 inRect
->bottom
+= pt
.v
;
1207 inRect
->right
+= pt
.h
;
1211 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1213 /*------------------------------------------------------
1214 Define which options your custom window supports.
1215 --------------------------------------------------------*/
1216 //just enable everything for our demo
1217 *(OptionBits
*)param
=//kWindowCanGrow|
1219 //kWindowCanCollapse|
1220 //kWindowCanGetWindowRegion|
1221 //kWindowHasTitleBar|
1222 //kWindowSupportsDragHilite|
1223 kWindowCanDrawInCurrentPort
|
1224 //kWindowCanMeasureTitle|
1225 kWindowWantsDisposeAtProcessDeath
|
1226 kWindowSupportsSetGrowImageRegion
|
1227 kWindowDefSupportsColorGrafPort
;
1231 // The content region is left as a rectangle matching the window size, this is
1232 // so the origin in the paint event, and etc. still matches what the
1233 // programmer expects.
1234 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1237 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1241 wxShapedMacWindowGetPos(window
, &r
) ;
1242 RectRgn( rgn
, &r
) ;
1246 // The structure region is set to the shape given to the SetShape method.
1247 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1249 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1255 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1256 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1257 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1258 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1264 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1266 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1268 switch(rgnRec
->regionCode
)
1270 case kWindowStructureRgn
:
1271 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1273 case kWindowContentRgn
:
1274 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1277 SetEmptyRgn(rgnRec
->winRgn
);
1284 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1286 /*------------------------------------------------------
1287 Determine the region of the window which was hit
1288 --------------------------------------------------------*/
1290 static RgnHandle tempRgn
=nil
;
1295 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1297 //Mac OS 8.5 or later
1298 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1299 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1302 return wNoHit
;//no significant area was hit.
1306 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1310 case kWindowMsgHitTest
:
1311 return wxShapedMacWindowHitTest(window
,param
);
1313 case kWindowMsgGetFeatures
:
1314 return wxShapedMacWindowGetFeatures(window
,param
);
1316 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1317 case kWindowMsgGetRegion
:
1318 return wxShapedMacWindowGetRegion(window
,param
);
1324 // ---------------------------------------------------------------------------