1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
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"
46 #include "ToolUtils.h"
49 #define wxMAC_DEBUG_REDRAW 0
50 #ifndef wxMAC_DEBUG_REDRAW
51 #define wxMAC_DEBUG_REDRAW 0
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // list of all frames and modeless dialogs
59 wxWindowList wxModelessWindows
;
61 // double click testing
62 static Point gs_lastWhere
;
63 static long gs_lastWhen
= 0;
67 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
70 // ============================================================================
71 // wxTopLevelWindowMac implementation
72 // ============================================================================
74 // ---------------------------------------------------------------------------
76 // ---------------------------------------------------------------------------
80 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
82 static const EventTypeSpec eventList
[] =
84 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
86 { kEventClassKeyboard
, kEventRawKeyDown
} ,
87 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
88 { kEventClassKeyboard
, kEventRawKeyUp
} ,
89 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
91 { kEventClassWindow
, kEventWindowUpdate
} ,
92 { kEventClassWindow
, kEventWindowActivated
} ,
93 { kEventClassWindow
, kEventWindowDeactivated
} ,
94 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
95 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
96 { kEventClassWindow
, kEventWindowClose
} ,
98 { kEventClassMouse
, kEventMouseDown
} ,
99 { kEventClassMouse
, kEventMouseUp
} ,
100 { kEventClassMouse
, kEventMouseMoved
} ,
101 { kEventClassMouse
, kEventMouseDragged
} ,
105 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
107 OSStatus result
= eventNotHandledErr
;
109 wxWindow
* focus
= wxWindow::FindFocus() ;
117 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
119 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
120 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
121 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
122 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
123 sizeof( Point
), NULL
, &point
);
125 switch ( GetEventKind( event
) )
127 case kEventTextInputUnicodeForKeyEvent
:
128 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
129 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
130 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
133 ControlHandle macControl
= (ControlHandle
) control
->GetMacControl() ;
136 ::HandleControlKey( macControl
, keyCode
, charCode
, modifiers
) ;
141 // this may lead to double events sent to a window in case all handlers have skipped the key down event
142 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
143 UInt32 message = (keyCode << 8) + charCode;
145 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
146 focus , message , modifiers , when , point.h , point.v ) )
157 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
159 OSStatus result
= eventNotHandledErr
;
161 wxWindow
* focus
= wxWindow::FindFocus() ;
166 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
168 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
169 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
170 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
171 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
172 sizeof( Point
), NULL
, &point
);
174 UInt32 message
= (keyCode
<< 8) + charCode
;
175 switch( GetEventKind( event
) )
177 case kEventRawKeyRepeat
:
178 case kEventRawKeyDown
:
180 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
181 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
182 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
183 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
184 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
188 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
191 case kEventRawKeyUp
:
192 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
193 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
198 case kEventRawKeyModifiersChanged
:
200 wxKeyEvent
event(wxEVT_KEY_DOWN
);
202 event
.m_shiftDown
= modifiers
& shiftKey
;
203 event
.m_controlDown
= modifiers
& controlKey
;
204 event
.m_altDown
= modifiers
& optionKey
;
205 event
.m_metaDown
= modifiers
& cmdKey
;
209 event
.m_timeStamp
= when
;
210 wxWindow
* focus
= wxWindow::FindFocus() ;
211 event
.SetEventObject(focus
);
213 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
215 event
.m_keyCode
= WXK_CONTROL
;
216 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
217 focus
->GetEventHandler()->ProcessEvent( event
) ;
219 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
221 event
.m_keyCode
= WXK_SHIFT
;
222 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
223 focus
->GetEventHandler()->ProcessEvent( event
) ;
225 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
227 event
.m_keyCode
= WXK_ALT
;
228 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
229 focus
->GetEventHandler()->ProcessEvent( event
) ;
231 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
233 event
.m_keyCode
= WXK_COMMAND
;
234 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
235 focus
->GetEventHandler()->ProcessEvent( event
) ;
237 wxTheApp
->s_lastModifiers
= modifiers
;
245 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
247 OSStatus result
= eventNotHandledErr
;
249 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
251 UInt32 modifiers
= 0;
252 EventMouseButton button
= 0 ;
255 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
256 sizeof( Point
), NULL
, &point
);
257 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
258 sizeof( UInt32
), NULL
, &modifiers
);
259 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
260 sizeof( EventMouseButton
), NULL
, &button
);
261 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
262 sizeof( UInt32
), NULL
, &click
);
264 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
265 modifiers
+= btnState
;
268 short windowPart
= ::FindWindow(point
, &window
);
270 if ( IsWindowActive(window
) && windowPart
== inContent
)
272 switch ( GetEventKind( event
) )
274 case kEventMouseDown
:
275 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
279 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
282 case kEventMouseMoved
:
283 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
286 case kEventMouseDragged
:
287 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
299 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
301 OSStatus result
= eventNotHandledErr
;
302 OSStatus err
= noErr
;
305 WindowRef windowRef
;
306 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
308 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
309 sizeof( WindowRef
), NULL
, &windowRef
);
311 switch( GetEventKind( event
) )
313 case kEventWindowUpdate
:
314 if ( !wxPendingDelete
.Member(toplevelWindow
) )
315 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
318 case kEventWindowActivated
:
319 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
322 case kEventWindowDeactivated
:
323 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
326 case kEventWindowClose
:
327 toplevelWindow
->Close() ;
330 case kEventWindowBoundsChanged
:
331 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
332 NULL
, sizeof( UInt32
), NULL
, &attributes
);
335 Rect newContentRect
;
337 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
338 sizeof( newContentRect
), NULL
, &newContentRect
);
340 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
341 newContentRect
.right
- newContentRect
.left
,
342 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
347 case kEventWindowBoundsChanging
:
348 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
349 NULL
, sizeof( UInt32
), NULL
, &attributes
);
352 Rect newContentRect
;
354 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
355 sizeof( newContentRect
), NULL
, &newContentRect
);
357 wxSize formerSize
= toplevelWindow
->GetSize() ;
359 if ( (attributes
& kWindowBoundsChangeSizeChanged
) ||
360 ( attributes
& kWindowBoundsChangeOriginChanged
) )
361 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
362 newContentRect
.right
- newContentRect
.left
,
363 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
366 toplevelWindow
->GetPosition( &x
, &y
) ;
367 toplevelWindow
->GetSize( &w
, &h
) ;
368 Rect adjustedRect
= { y
, x
, y
+ h
, x
+ w
} ;
370 if ( !EqualRect( &newContentRect
, &adjustedRect
) )
372 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( adjustedRect
) , &adjustedRect
) ;
375 if ( toplevelWindow
->GetSize() != formerSize
)
376 toplevelWindow
->Update() ;
387 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
389 OSStatus result
= eventNotHandledErr
;
391 switch ( GetEventClass( event
) )
393 case kEventClassKeyboard
:
394 result
= KeyboardEventHandler( handler
, event
, data
) ;
396 case kEventClassTextInput
:
397 result
= TextInputEventHandler( handler
, event
, data
) ;
399 case kEventClassWindow
:
400 result
= WindowEventHandler( handler
, event
, data
) ;
402 case kEventClassMouse
:
403 result
= MouseEventHandler( handler
, event
, data
) ;
411 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
415 // ---------------------------------------------------------------------------
416 // wxWindowMac utility functions
417 // ---------------------------------------------------------------------------
419 // Find an item given the Macintosh Window Reference
421 wxList
*wxWinMacWindowList
= NULL
;
422 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
424 if ( wxWinMacWindowList
== NULL
)
426 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
429 return (wxTopLevelWindowMac
*)node
->GetData();
432 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
434 // adding NULL WindowRef is (first) surely a result of an error and
435 // (secondly) breaks menu command processing
436 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
438 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
439 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
442 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
444 wxWinMacWindowList
->DeleteObject(win
);
448 // ----------------------------------------------------------------------------
449 // wxTopLevelWindowMac creation
450 // ----------------------------------------------------------------------------
452 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
453 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
455 void wxTopLevelWindowMac::Init()
458 m_maximizeOnShow
= FALSE
;
459 m_macNoEraseUpdateRgn
= NewRgn() ;
460 m_macNeedsErasing
= false ;
463 m_macEventHandler
= NULL
;
467 class wxMacDeferredWindowDeleter
: public wxObject
470 wxMacDeferredWindowDeleter( WindowRef windowRef
)
472 m_macWindow
= windowRef
;
474 virtual ~wxMacDeferredWindowDeleter()
476 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
479 WindowRef m_macWindow
;
482 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
484 const wxString
& title
,
488 const wxString
& name
)
493 m_windowStyle
= style
;
497 m_windowId
= id
== -1 ? NewControlId() : id
;
499 wxTopLevelWindows
.Append(this);
502 parent
->AddChild(this);
507 wxTopLevelWindowMac::~wxTopLevelWindowMac()
511 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
512 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
516 if ( m_macEventHandler
)
518 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
519 m_macEventHandler
= NULL
;
523 wxRemoveMacWindowAssociation( this ) ;
525 if ( wxModelessWindows
.Find(this) )
526 wxModelessWindows
.DeleteObject(this);
528 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
532 // ----------------------------------------------------------------------------
533 // wxTopLevelWindowMac maximize/minimize
534 // ----------------------------------------------------------------------------
536 void wxTopLevelWindowMac::Maximize(bool maximize
)
538 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
543 Point pt
= { 0, 0 } ;
544 SetPortWindowPort((WindowRef
)m_macWindow
) ;
545 LocalToGlobal( &pt
) ;
548 GetWindowPortBounds((WindowRef
)m_macWindow
, &tempRect
) ;
549 SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
550 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
553 bool wxTopLevelWindowMac::IsMaximized() const
555 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
558 void wxTopLevelWindowMac::Iconize(bool iconize
)
560 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
561 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
564 bool wxTopLevelWindowMac::IsIconized() const
566 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
569 void wxTopLevelWindowMac::Restore()
571 // not available on mac
574 // ----------------------------------------------------------------------------
575 // wxTopLevelWindowMac misc
576 // ----------------------------------------------------------------------------
578 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
581 wxTopLevelWindowBase::SetIcon(icon
);
584 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
588 const wxString
& name
)
590 OSStatus err
= noErr
;
592 m_windowStyle
= style
;
613 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
615 // translate the window attributes in the appropriate window class and attributes
617 WindowClass wclass
= 0;
618 WindowAttributes attr
= kWindowNoAttributes
;
620 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
623 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
624 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
625 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
628 wclass
= kFloatingWindowClass
;
629 if ( HasFlag(wxTINY_CAPTION_VERT
) )
631 attr
|= kWindowSideTitlebarAttribute
;
637 wclass
= kPlainWindowClass
;
639 wclass
= kFloatingWindowClass
;
643 else if ( HasFlag( wxCAPTION
) )
645 wclass
= kDocumentWindowClass
;
649 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
650 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
652 wclass
= kDocumentWindowClass
;
657 wclass
= kPlainWindowClass
;
659 wclass
= kModalWindowClass
;
664 if ( HasFlag( wxMINIMIZE_BOX
) )
666 attr
|= kWindowCollapseBoxAttribute
;
668 if ( HasFlag( wxMAXIMIZE_BOX
) )
670 attr
|= kWindowFullZoomAttribute
;
672 if ( HasFlag( wxRESIZE_BORDER
) )
674 attr
|= kWindowResizableAttribute
;
676 if ( HasFlag( wxCLOSE_BOX
) )
678 attr
|= kWindowCloseBoxAttribute
;
682 #if 0 // having problems right now with that
683 if (HasFlag(wxSTAY_ON_TOP
))
684 wclass
= kUtilityWindowClass
;
689 if ( HasFlag(wxFRAME_SHAPED
) )
691 WindowDefSpec customWindowDefSpec
;
692 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
693 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
695 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
696 attr
, &theBoundsRect
,
697 (WindowRef
*) &m_macWindow
);
702 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
705 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
706 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
707 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
708 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
710 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
711 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
712 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
718 if ( HasFlag(wxFRAME_SHAPED
) )
720 // default shape matches the window size
721 wxRegion
rgn(0, 0, m_width
, m_height
);
727 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
729 ((Point
*)localOrigin
)->h
= 0;
730 ((Point
*)localOrigin
)->v
= 0;
731 ((Rect
*)clipRect
)->left
= 0;
732 ((Rect
*)clipRect
)->top
= 0;
733 ((Rect
*)clipRect
)->right
= m_width
;
734 ((Rect
*)clipRect
)->bottom
= m_height
;
735 *window
= m_macWindow
;
739 void wxTopLevelWindowMac::ClearBackground()
741 wxWindow::ClearBackground() ;
744 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
746 return m_macRootControl
;
750 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
752 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
754 RgnHandle visRgn
= NewRgn() ;
755 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
756 BeginUpdate( (WindowRef
)m_macWindow
) ;
758 RgnHandle updateRgn
= NewRgn();
759 RgnHandle diffRgn
= NewRgn() ;
761 if ( updateRgn
&& diffRgn
)
764 // macos internal control redraws clean up areas we'd like to redraw ourselves
765 // therefore we pick the boundary rect and make sure we can redraw it
766 // this has to be intersected by the visRgn in order to avoid drawing over its own
768 RgnHandle trueUpdateRgn
= NewRgn() ;
769 Rect trueUpdateRgnBoundary
;
770 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
771 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
772 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
773 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
775 DisposeRgn( trueUpdateRgn
) ;
776 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
778 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
780 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
781 if ( !EmptyRgn( updateRgn
) )
783 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
787 DisposeRgn( updateRgn
);
789 DisposeRgn( diffRgn
);
791 DisposeRgn( visRgn
) ;
793 EndUpdate( (WindowRef
)m_macWindow
) ;
794 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
795 m_macNeedsErasing
= false ;
799 // Raise the window to the top of the Z order
800 void wxTopLevelWindowMac::Raise()
802 ::SelectWindow( (WindowRef
)m_macWindow
) ;
805 // Lower the window to the bottom of the Z order
806 void wxTopLevelWindowMac::Lower()
808 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
811 void wxTopLevelWindowMac::MacFireMouseEvent(
812 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
814 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
815 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
816 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
818 event
.m_leftDown
= isDown
&& !controlDown
;
820 event
.m_middleDown
= FALSE
;
821 event
.m_rightDown
= isDown
&& controlDown
;
823 if ( kind
== mouseDown
)
826 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
828 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
830 else if ( kind
== mouseUp
)
833 event
.SetEventType(wxEVT_RIGHT_UP
) ;
835 event
.SetEventType(wxEVT_LEFT_UP
) ;
839 event
.SetEventType(wxEVT_MOTION
) ;
842 event
.m_shiftDown
= modifiers
& shiftKey
;
843 event
.m_controlDown
= modifiers
& controlKey
;
844 event
.m_altDown
= modifiers
& optionKey
;
845 event
.m_metaDown
= modifiers
& cmdKey
;
853 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
854 ::GlobalToLocal( &localwhere
) ;
857 if ( kind
== mouseDown
)
859 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
861 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
863 // This is not right if the second mouse down
864 // event occured in a differen window. We
865 // correct this in MacDispatchMouseEvent.
867 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
869 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
875 gs_lastWhen
= timestamp
;
877 gs_lastWhere
= localwhere
;
880 event
.m_x
= localwhere
.h
;
881 event
.m_y
= localwhere
.v
;
885 event
.m_timeStamp
= timestamp
;
886 event
.SetEventObject(this);
887 if ( wxTheApp
->s_captureWindow
)
891 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
894 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
895 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
897 if ( kind
== mouseUp
)
899 wxTheApp
->s_captureWindow
= NULL
;
902 m_cursor
.MacInstall() ;
908 MacDispatchMouseEvent( event
) ;
914 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
916 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
917 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
920 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
926 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
927 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
933 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
939 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
940 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
948 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
950 if(s_macDeactivateWindow
)
952 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
953 s_macDeactivateWindow
->MacActivate(timestamp
, false);
957 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
959 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
961 if(s_macDeactivateWindow
==this)
962 s_macDeactivateWindow
=NULL
;
963 MacDelayedDeactivation(timestamp
);
964 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
965 event
.m_timeStamp
= timestamp
;
966 event
.SetEventObject(this);
968 GetEventHandler()->ProcessEvent(event
);
970 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
972 // Early versions of MacOS X don't refresh backgrounds properly,
973 // so refresh the whole window on activation and deactivation.
974 long osVersion
= UMAGetSystemVersion();
975 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
981 // for the moment we have to resolve some redrawing issues like this
982 // the OS is stealing some redrawing areas as soon as it draws a control
989 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
995 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
997 wxWindow::SetTitle( title
) ;
998 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
1001 bool wxTopLevelWindowMac::Show(bool show
)
1003 if ( !wxWindow::Show(show
) )
1008 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1009 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1010 // no need to generate events here, they will get them triggered by macos
1011 // actually they should be , but apparently they are not
1012 wxSize
size(m_width
, m_height
);
1013 wxSizeEvent
event(size
, m_windowId
);
1014 event
.SetEventObject(this);
1015 GetEventHandler()->ProcessEvent(event
);
1019 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1033 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1035 int former_x
= m_x
;
1036 int former_y
= m_y
;
1037 int former_w
= m_width
;
1038 int former_h
= m_height
;
1040 int actualWidth
= width
;
1041 int actualHeight
= height
;
1045 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1046 actualWidth
= m_minWidth
;
1047 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1048 actualHeight
= m_minHeight
;
1049 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1050 actualWidth
= m_maxWidth
;
1051 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1052 actualHeight
= m_maxHeight
;
1054 bool doMove
= false ;
1055 bool doResize
= false ;
1057 if ( actualX
!= former_x
|| actualY
!= former_y
)
1061 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1066 if ( doMove
|| doResize
)
1070 m_width
= actualWidth
;
1071 m_height
= actualHeight
;
1074 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1077 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1079 // the OS takes care of invalidating and erasing the new area so we only have to
1080 // take care of refreshing for full repaints
1082 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
1086 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1088 wxFrame
* frame
= (wxFrame
*) this ;
1089 frame
->PositionStatusBar();
1090 frame
->PositionToolBar();
1093 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1095 MacRepositionScrollBars() ;
1098 wxPoint
point(m_x
, m_y
);
1099 wxMoveEvent
event(point
, m_windowId
);
1100 event
.SetEventObject(this);
1101 GetEventHandler()->ProcessEvent(event
) ;
1105 MacRepositionScrollBars() ;
1106 wxSize
size(m_width
, m_height
);
1107 wxSizeEvent
event(size
, m_windowId
);
1108 event
.SetEventObject(this);
1109 GetEventHandler()->ProcessEvent(event
);
1116 * Invalidation Mechanism
1118 * The update mechanism reflects exactely the windows mechanism
1119 * the rect gets added to the window invalidate region, if the eraseBackground flag
1120 * has been true for any part of the update rgn the background is erased in the entire region
1121 * not just in the specified rect.
1123 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1124 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1125 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1126 * will get the eraseBackground event first
1129 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1131 GrafPtr formerPort
;
1132 GetPort( &formerPort
) ;
1133 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1135 m_macNeedsErasing
|= eraseBackground
;
1137 // if we already know that we will have to erase, there's no need to track the rest
1138 if ( !m_macNeedsErasing
)
1140 // we end only here if eraseBackground is false
1141 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1142 // we will have to erase anyway
1144 RgnHandle updateRgn
= NewRgn();
1145 RgnHandle diffRgn
= NewRgn() ;
1146 if ( updateRgn
&& diffRgn
)
1148 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1150 LocalToGlobal( &pt
) ;
1151 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1152 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1153 if ( !EmptyRgn( diffRgn
) )
1155 m_macNeedsErasing
= true ;
1159 DisposeRgn( updateRgn
);
1161 DisposeRgn( diffRgn
);
1163 if ( !m_macNeedsErasing
)
1165 RgnHandle rectRgn
= NewRgn() ;
1166 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1167 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1168 DisposeRgn( rectRgn
) ;
1171 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1172 // turn this on to debug the refreshing cycle
1173 #if wxMAC_DEBUG_REDRAW
1176 SetPort( formerPort
) ;
1180 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1182 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1183 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1186 // The empty region signifies that the shape should be removed from the
1188 if ( region
.IsEmpty() )
1190 wxSize sz
= GetClientSize();
1191 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1192 return SetShape(rgn
);
1195 // Make a copy of the region
1196 RgnHandle shapeRegion
= NewRgn();
1197 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1199 // Dispose of any shape region we may already have
1200 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1204 // Save the region so we can use it later
1205 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1207 // Tell the window manager that the window has changed shape
1208 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1215 // ---------------------------------------------------------------------------
1216 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1217 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1218 // ---------------------------------------------------------------------------
1222 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1224 GetWindowPortBounds(window
, inRect
);
1225 Point pt
= {inRect
->left
, inRect
->top
};
1226 SetPort((GrafPtr
) GetWindowPort(window
));
1229 inRect
->left
= pt
.h
;
1230 inRect
->bottom
+= pt
.v
;
1231 inRect
->right
+= pt
.h
;
1235 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1237 /*------------------------------------------------------
1238 Define which options your custom window supports.
1239 --------------------------------------------------------*/
1240 //just enable everything for our demo
1241 *(OptionBits
*)param
=//kWindowCanGrow|
1243 //kWindowCanCollapse|
1244 //kWindowCanGetWindowRegion|
1245 //kWindowHasTitleBar|
1246 //kWindowSupportsDragHilite|
1247 kWindowCanDrawInCurrentPort
|
1248 //kWindowCanMeasureTitle|
1249 kWindowWantsDisposeAtProcessDeath
|
1250 kWindowSupportsSetGrowImageRegion
|
1251 kWindowDefSupportsColorGrafPort
;
1255 // The content region is left as a rectangle matching the window size, this is
1256 // so the origin in the paint event, and etc. still matches what the
1257 // programmer expects.
1258 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1261 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1264 wxRect r
= win
->GetRect();
1265 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1269 // The structure region is set to the shape given to the SetShape method.
1270 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1272 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1278 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1279 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1280 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1281 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1287 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1289 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1291 switch(rgnRec
->regionCode
)
1293 case kWindowStructureRgn
:
1294 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1296 case kWindowContentRgn
:
1297 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1300 SetEmptyRgn(rgnRec
->winRgn
);
1307 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1309 /*------------------------------------------------------
1310 Determine the region of the window which was hit
1311 --------------------------------------------------------*/
1313 static RgnHandle tempRgn
=nil
;
1318 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1320 //Mac OS 8.5 or later
1321 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1322 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1325 return wNoHit
;//no significant area was hit.
1329 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1333 case kWindowMsgHitTest
:
1334 return wxShapedMacWindowHitTest(window
,param
);
1336 case kWindowMsgGetFeatures
:
1337 return wxShapedMacWindowGetFeatures(window
,param
);
1339 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1340 case kWindowMsgGetRegion
:
1341 return wxShapedMacWindowGetRegion(window
,param
);
1348 // ---------------------------------------------------------------------------