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
:
179 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
180 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
185 case kEventRawKeyUp
:
186 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
187 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
192 case kEventRawKeyModifiersChanged
:
194 wxKeyEvent
event(wxEVT_KEY_DOWN
);
196 event
.m_shiftDown
= modifiers
& shiftKey
;
197 event
.m_controlDown
= modifiers
& controlKey
;
198 event
.m_altDown
= modifiers
& optionKey
;
199 event
.m_metaDown
= modifiers
& cmdKey
;
203 event
.m_timeStamp
= when
;
204 wxWindow
* focus
= wxWindow::FindFocus() ;
205 event
.SetEventObject(focus
);
207 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
209 event
.m_keyCode
= WXK_CONTROL
;
210 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
211 focus
->GetEventHandler()->ProcessEvent( event
) ;
213 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
215 event
.m_keyCode
= WXK_SHIFT
;
216 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
217 focus
->GetEventHandler()->ProcessEvent( event
) ;
219 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
221 event
.m_keyCode
= WXK_ALT
;
222 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
223 focus
->GetEventHandler()->ProcessEvent( event
) ;
225 wxTheApp
->s_lastModifiers
= modifiers
;
233 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
235 OSStatus result
= eventNotHandledErr
;
237 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
239 UInt32 modifiers
= 0;
240 EventMouseButton button
= 0 ;
243 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
244 sizeof( Point
), NULL
, &point
);
245 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
246 sizeof( UInt32
), NULL
, &modifiers
);
247 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
248 sizeof( EventMouseButton
), NULL
, &button
);
249 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
250 sizeof( UInt32
), NULL
, &click
);
252 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
253 modifiers
+= btnState
;
256 short windowPart
= ::FindWindow(point
, &window
);
258 if ( IsWindowActive(window
) && windowPart
== inContent
)
260 switch ( GetEventKind( event
) )
262 case kEventMouseDown
:
263 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
267 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
270 case kEventMouseMoved
:
271 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
274 case kEventMouseDragged
:
275 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
287 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
289 OSStatus result
= eventNotHandledErr
;
290 OSStatus err
= noErr
;
293 WindowRef windowRef
;
294 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
296 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
297 sizeof( WindowRef
), NULL
, &windowRef
);
299 switch( GetEventKind( event
) )
301 case kEventWindowUpdate
:
302 if ( !wxPendingDelete
.Member(toplevelWindow
) )
303 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
306 case kEventWindowActivated
:
307 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
310 case kEventWindowDeactivated
:
311 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
314 case kEventWindowClose
:
315 toplevelWindow
->Close() ;
318 case kEventWindowBoundsChanged
:
319 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
320 NULL
, sizeof( UInt32
), NULL
, &attributes
);
323 Rect newContentRect
;
325 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
326 sizeof( newContentRect
), NULL
, &newContentRect
);
328 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
329 newContentRect
.right
- newContentRect
.left
,
330 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
335 case kEventWindowBoundsChanging
:
336 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
337 NULL
, sizeof( UInt32
), NULL
, &attributes
);
340 Rect newContentRect
;
342 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
343 sizeof( newContentRect
), NULL
, &newContentRect
);
345 wxRect
contentRect(newContentRect
.left
, newContentRect
.top
,
346 newContentRect
.right
- newContentRect
.left
,
347 newContentRect
.bottom
- newContentRect
.top
) ;
349 bool handled
= false ;
350 if ((attributes
& kWindowBoundsChangeSizeChanged
) != 0)
352 wxSizeEvent
event(contentRect
, toplevelWindow
->GetId());
353 event
.SetEventObject(toplevelWindow
);
354 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
355 contentRect
= event
.GetRect() ;
357 else if ( attributes
& kWindowBoundsChangeOriginChanged
!= 0)
359 wxMoveEvent
event(contentRect
, toplevelWindow
->GetId());
360 event
.SetEventObject(toplevelWindow
);
361 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
362 contentRect
= event
.GetRect() ;
366 SetRect( &newContentRect
, contentRect
.GetLeft() , contentRect
.GetTop() , contentRect
.GetRight() , contentRect
.GetBottom() ) ;
367 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( newContentRect
), &newContentRect
);
378 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
380 OSStatus result
= eventNotHandledErr
;
382 switch ( GetEventClass( event
) )
384 case kEventClassKeyboard
:
385 result
= KeyboardEventHandler( handler
, event
, data
) ;
387 case kEventClassTextInput
:
388 result
= TextInputEventHandler( handler
, event
, data
) ;
390 case kEventClassWindow
:
391 result
= WindowEventHandler( handler
, event
, data
) ;
393 case kEventClassMouse
:
394 result
= MouseEventHandler( handler
, event
, data
) ;
402 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
406 // ---------------------------------------------------------------------------
407 // wxWindowMac utility functions
408 // ---------------------------------------------------------------------------
410 // Find an item given the Macintosh Window Reference
412 wxList
*wxWinMacWindowList
= NULL
;
413 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
415 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
418 return (wxTopLevelWindowMac
*)node
->GetData();
421 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
423 // adding NULL WindowRef is (first) surely a result of an error and
424 // (secondly) breaks menu command processing
425 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
427 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
428 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
431 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
433 wxWinMacWindowList
->DeleteObject(win
);
437 // ----------------------------------------------------------------------------
438 // wxTopLevelWindowMac creation
439 // ----------------------------------------------------------------------------
441 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
442 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
444 void wxTopLevelWindowMac::Init()
447 m_maximizeOnShow
= FALSE
;
448 m_macNoEraseUpdateRgn
= NewRgn() ;
449 m_macNeedsErasing
= false ;
452 m_macEventHandler
= NULL
;
456 class wxMacDeferredWindowDeleter
: public wxObject
459 wxMacDeferredWindowDeleter( WindowRef windowRef
)
461 m_macWindow
= windowRef
;
463 virtual ~wxMacDeferredWindowDeleter()
465 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
468 WindowRef m_macWindow
;
471 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
473 const wxString
& title
,
477 const wxString
& name
)
482 m_windowStyle
= style
;
486 m_windowId
= id
== -1 ? NewControlId() : id
;
488 wxTopLevelWindows
.Append(this);
491 parent
->AddChild(this);
496 wxTopLevelWindowMac::~wxTopLevelWindowMac()
500 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
501 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
505 if ( m_macEventHandler
)
507 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
508 m_macEventHandler
= NULL
;
512 wxRemoveMacWindowAssociation( this ) ;
514 if ( wxModelessWindows
.Find(this) )
515 wxModelessWindows
.DeleteObject(this);
517 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
521 // ----------------------------------------------------------------------------
522 // wxTopLevelWindowMac maximize/minimize
523 // ----------------------------------------------------------------------------
525 void wxTopLevelWindowMac::Maximize(bool maximize
)
527 // not available on mac
530 bool wxTopLevelWindowMac::IsMaximized() const
535 void wxTopLevelWindowMac::Iconize(bool iconize
)
537 // not available on mac
540 bool wxTopLevelWindowMac::IsIconized() const
542 // mac dialogs cannot be iconized
546 void wxTopLevelWindowMac::Restore()
548 // not available on mac
551 // ----------------------------------------------------------------------------
552 // wxTopLevelWindowMac misc
553 // ----------------------------------------------------------------------------
555 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
558 wxTopLevelWindowBase::SetIcon(icon
);
561 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
565 const wxString
& name
)
567 OSStatus err
= noErr
;
569 m_windowStyle
= style
;
590 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
592 // translate the window attributes in the appropriate window class and attributes
594 WindowClass wclass
= 0;
595 WindowAttributes attr
= kWindowNoAttributes
;
597 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
600 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
601 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
602 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
605 wclass
= kFloatingWindowClass
;
606 if ( HasFlag(wxTINY_CAPTION_VERT
) )
608 attr
|= kWindowSideTitlebarAttribute
;
614 wclass
= kPlainWindowClass
;
616 wclass
= kFloatingWindowClass
;
620 else if ( HasFlag( wxCAPTION
) )
622 wclass
= kDocumentWindowClass
;
626 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
627 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
629 wclass
= kDocumentWindowClass
;
634 wclass
= kPlainWindowClass
;
636 wclass
= kModalWindowClass
;
641 if ( HasFlag( wxMINIMIZE_BOX
) )
643 attr
|= kWindowCollapseBoxAttribute
;
645 if ( HasFlag( wxMAXIMIZE_BOX
) )
647 attr
|= kWindowFullZoomAttribute
;
649 if ( HasFlag( wxRESIZE_BORDER
) )
651 attr
|= kWindowResizableAttribute
;
653 if ( HasFlag( wxCLOSE_BOX
) )
655 attr
|= kWindowCloseBoxAttribute
;
659 #if 0 // having problems right now with that
660 if (HasFlag(wxSTAY_ON_TOP
))
661 wclass
= kUtilityWindowClass
;
666 if ( HasFlag(wxFRAME_SHAPED
) )
668 WindowDefSpec customWindowDefSpec
;
669 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
670 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
672 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
673 attr
, &theBoundsRect
,
674 (WindowRef
*) &m_macWindow
);
679 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
682 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
683 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
684 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
685 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
687 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
688 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
689 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
695 if ( HasFlag(wxFRAME_SHAPED
) )
697 // default shape matches the window size
698 wxRegion
rgn(0, 0, m_width
, m_height
);
704 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
706 ((Point
*)localOrigin
)->h
= 0;
707 ((Point
*)localOrigin
)->v
= 0;
708 ((Rect
*)clipRect
)->left
= 0;
709 ((Rect
*)clipRect
)->top
= 0;
710 ((Rect
*)clipRect
)->right
= m_width
;
711 ((Rect
*)clipRect
)->bottom
= m_height
;
712 *window
= m_macWindow
;
716 void wxTopLevelWindowMac::ClearBackground()
718 wxWindow::ClearBackground() ;
721 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
723 return m_macRootControl
;
727 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
729 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
731 RgnHandle visRgn
= NewRgn() ;
732 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
733 BeginUpdate( (WindowRef
)m_macWindow
) ;
735 RgnHandle updateRgn
= NewRgn();
736 RgnHandle diffRgn
= NewRgn() ;
738 if ( updateRgn
&& diffRgn
)
741 // macos internal control redraws clean up areas we'd like to redraw ourselves
742 // therefore we pick the boundary rect and make sure we can redraw it
743 // this has to be intersected by the visRgn in order to avoid drawing over its own
745 RgnHandle trueUpdateRgn
= NewRgn() ;
746 Rect trueUpdateRgnBoundary
;
747 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
748 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
749 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
750 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
752 DisposeRgn( trueUpdateRgn
) ;
753 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
755 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
757 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
758 if ( !EmptyRgn( updateRgn
) )
760 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
764 DisposeRgn( updateRgn
);
766 DisposeRgn( diffRgn
);
768 DisposeRgn( visRgn
) ;
770 EndUpdate( (WindowRef
)m_macWindow
) ;
771 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
772 m_macNeedsErasing
= false ;
776 // Raise the window to the top of the Z order
777 void wxTopLevelWindowMac::Raise()
779 ::SelectWindow( (WindowRef
)m_macWindow
) ;
782 // Lower the window to the bottom of the Z order
783 void wxTopLevelWindowMac::Lower()
785 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
788 void wxTopLevelWindowMac::MacFireMouseEvent(
789 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
791 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
792 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
793 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
795 event
.m_leftDown
= isDown
&& !controlDown
;
797 event
.m_middleDown
= FALSE
;
798 event
.m_rightDown
= isDown
&& controlDown
;
800 if ( kind
== mouseDown
)
803 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
805 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
807 else if ( kind
== mouseUp
)
810 event
.SetEventType(wxEVT_RIGHT_UP
) ;
812 event
.SetEventType(wxEVT_LEFT_UP
) ;
816 event
.SetEventType(wxEVT_MOTION
) ;
819 event
.m_shiftDown
= modifiers
& shiftKey
;
820 event
.m_controlDown
= modifiers
& controlKey
;
821 event
.m_altDown
= modifiers
& optionKey
;
822 event
.m_metaDown
= modifiers
& cmdKey
;
830 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
831 ::GlobalToLocal( &localwhere
) ;
834 if ( kind
== mouseDown
)
836 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
838 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
840 // This is not right if the second mouse down
841 // event occured in a differen window. We
842 // correct this in MacDispatchMouseEvent.
844 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
846 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
852 gs_lastWhen
= timestamp
;
854 gs_lastWhere
= localwhere
;
857 event
.m_x
= localwhere
.h
;
858 event
.m_y
= localwhere
.v
;
862 event
.m_timeStamp
= timestamp
;
863 event
.SetEventObject(this);
864 if ( wxTheApp
->s_captureWindow
)
868 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
871 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
872 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
874 if ( kind
== mouseUp
)
876 wxTheApp
->s_captureWindow
= NULL
;
879 m_cursor
.MacInstall() ;
885 MacDispatchMouseEvent( event
) ;
891 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
893 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
894 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
897 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
903 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
904 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
910 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
916 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
917 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
925 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
927 if(s_macDeactivateWindow
)
929 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
930 s_macDeactivateWindow
->MacActivate(timestamp
, false);
934 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
936 wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
937 if(s_macDeactivateWindow
==this)
938 s_macDeactivateWindow
=NULL
;
939 MacDelayedDeactivation(timestamp
);
940 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
941 event
.m_timeStamp
= timestamp
;
942 event
.SetEventObject(this);
944 GetEventHandler()->ProcessEvent(event
);
946 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
948 // Early versions of MacOS X don't refresh backgrounds properly,
949 // so refresh the whole window on activation and deactivation.
950 long osVersion
= UMAGetSystemVersion();
951 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
957 // for the moment we have to resolve some redrawing issues like this
958 // the OS is stealing some redrawing areas as soon as it draws a control
965 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
971 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
973 wxWindow::SetTitle( title
) ;
974 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
977 bool wxTopLevelWindowMac::Show(bool show
)
979 if ( !wxWindow::Show(show
) )
984 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
985 ::SelectWindow( (WindowRef
)m_macWindow
) ;
986 // no need to generate events here, they will get them triggered by macos
987 // actually they should be , but apparently they are not
988 wxSize
size(m_width
, m_height
);
989 wxSizeEvent
event(size
, m_windowId
);
990 event
.SetEventObject(this);
991 GetEventHandler()->ProcessEvent(event
);
995 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1009 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1011 int former_x
= m_x
;
1012 int former_y
= m_y
;
1013 int former_w
= m_width
;
1014 int former_h
= m_height
;
1016 int actualWidth
= width
;
1017 int actualHeight
= height
;
1021 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1022 actualWidth
= m_minWidth
;
1023 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1024 actualHeight
= m_minHeight
;
1025 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1026 actualWidth
= m_maxWidth
;
1027 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1028 actualHeight
= m_maxHeight
;
1030 bool doMove
= false ;
1031 bool doResize
= false ;
1033 if ( actualX
!= former_x
|| actualY
!= former_y
)
1037 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1042 if ( doMove
|| doResize
)
1046 m_width
= actualWidth
;
1047 m_height
= actualHeight
;
1050 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1053 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1055 // the OS takes care of invalidating and erasing the new area so we only have to
1056 // take care of refreshing for full repaints
1058 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
1062 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1064 wxFrame
* frame
= (wxFrame
*) this ;
1065 frame
->PositionStatusBar();
1066 frame
->PositionToolBar();
1069 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1071 MacRepositionScrollBars() ;
1074 wxPoint
point(m_x
, m_y
);
1075 wxMoveEvent
event(point
, m_windowId
);
1076 event
.SetEventObject(this);
1077 GetEventHandler()->ProcessEvent(event
) ;
1081 MacRepositionScrollBars() ;
1082 wxSize
size(m_width
, m_height
);
1083 wxSizeEvent
event(size
, m_windowId
);
1084 event
.SetEventObject(this);
1085 GetEventHandler()->ProcessEvent(event
);
1092 * Invalidation Mechanism
1094 * The update mechanism reflects exactely the windows mechanism
1095 * the rect gets added to the window invalidate region, if the eraseBackground flag
1096 * has been true for any part of the update rgn the background is erased in the entire region
1097 * not just in the specified rect.
1099 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1100 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1101 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1102 * will get the eraseBackground event first
1105 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1107 GrafPtr formerPort
;
1108 GetPort( &formerPort
) ;
1109 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1111 m_macNeedsErasing
|= eraseBackground
;
1113 // if we already know that we will have to erase, there's no need to track the rest
1114 if ( !m_macNeedsErasing
)
1116 // we end only here if eraseBackground is false
1117 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1118 // we will have to erase anyway
1120 RgnHandle updateRgn
= NewRgn();
1121 RgnHandle diffRgn
= NewRgn() ;
1122 if ( updateRgn
&& diffRgn
)
1124 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1126 LocalToGlobal( &pt
) ;
1127 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1128 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1129 if ( !EmptyRgn( diffRgn
) )
1131 m_macNeedsErasing
= true ;
1135 DisposeRgn( updateRgn
);
1137 DisposeRgn( diffRgn
);
1139 if ( !m_macNeedsErasing
)
1141 RgnHandle rectRgn
= NewRgn() ;
1142 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1143 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1144 DisposeRgn( rectRgn
) ;
1147 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1148 // turn this on to debug the refreshing cycle
1149 #if wxMAC_DEBUG_REDRAW
1152 SetPort( formerPort
) ;
1156 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1158 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1159 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1162 // The empty region signifies that the shape should be removed from the
1164 if ( region
.IsEmpty() )
1166 wxSize sz
= GetClientSize();
1167 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1168 return SetShape(rgn
);
1171 // Make a copy of the region
1172 RgnHandle shapeRegion
= NewRgn();
1173 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1175 // Dispose of any shape region we may already have
1176 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1180 // Save the region so we can use it later
1181 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1183 // Tell the window manager that the window has changed shape
1184 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1191 // ---------------------------------------------------------------------------
1192 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1193 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1194 // ---------------------------------------------------------------------------
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
);
1240 wxRect r
= win
->GetRect();
1241 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1245 // The structure region is set to the shape given to the SetShape method.
1246 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1248 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1254 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1255 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1256 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1257 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1263 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1265 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1267 switch(rgnRec
->regionCode
)
1269 case kWindowStructureRgn
:
1270 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1272 case kWindowContentRgn
:
1273 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1276 SetEmptyRgn(rgnRec
->winRgn
);
1283 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1285 /*------------------------------------------------------
1286 Determine the region of the window which was hit
1287 --------------------------------------------------------*/
1289 static RgnHandle tempRgn
=nil
;
1294 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1296 //Mac OS 8.5 or later
1297 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1298 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1301 return wNoHit
;//no significant area was hit.
1305 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1309 case kWindowMsgHitTest
:
1310 return wxShapedMacWindowHitTest(window
,param
);
1312 case kWindowMsgGetFeatures
:
1313 return wxShapedMacWindowGetFeatures(window
,param
);
1315 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1316 case kWindowMsgGetRegion
:
1317 return wxShapedMacWindowGetRegion(window
,param
);
1324 // ---------------------------------------------------------------------------