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() ;
114 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
118 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
120 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
121 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
122 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
123 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
124 sizeof( Point
), NULL
, &point
);
126 UInt32 message
= (keyCode
<< 8) + charCode
;
128 switch ( GetEventKind( event
) )
130 case kEventTextInputUnicodeForKeyEvent
:
131 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
132 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
142 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
144 OSStatus result
= eventNotHandledErr
;
146 wxWindow
* focus
= wxWindow::FindFocus() ;
151 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
153 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
154 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
155 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
156 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
157 sizeof( Point
), NULL
, &point
);
159 UInt32 message
= (keyCode
<< 8) + charCode
;
160 switch( GetEventKind( event
) )
162 case kEventRawKeyRepeat
:
163 case kEventRawKeyDown
:
164 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
165 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
170 case kEventRawKeyUp
:
171 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
172 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
177 case kEventRawKeyModifiersChanged
:
179 wxKeyEvent
event(wxEVT_KEY_DOWN
);
181 event
.m_shiftDown
= modifiers
& shiftKey
;
182 event
.m_controlDown
= modifiers
& controlKey
;
183 event
.m_altDown
= modifiers
& optionKey
;
184 event
.m_metaDown
= modifiers
& cmdKey
;
188 event
.m_timeStamp
= when
;
189 wxWindow
* focus
= wxWindow::FindFocus() ;
190 event
.SetEventObject(focus
);
192 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
194 event
.m_keyCode
= WXK_CONTROL
;
195 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
196 focus
->GetEventHandler()->ProcessEvent( event
) ;
198 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
200 event
.m_keyCode
= WXK_SHIFT
;
201 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
202 focus
->GetEventHandler()->ProcessEvent( event
) ;
204 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
206 event
.m_keyCode
= WXK_ALT
;
207 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
208 focus
->GetEventHandler()->ProcessEvent( event
) ;
210 wxTheApp
->s_lastModifiers
= modifiers
;
218 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
220 OSStatus result
= eventNotHandledErr
;
222 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
224 UInt32 modifiers
= 0;
225 EventMouseButton button
= 0 ;
228 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
229 sizeof( Point
), NULL
, &point
);
230 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
231 sizeof( UInt32
), NULL
, &modifiers
);
232 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
233 sizeof( EventMouseButton
), NULL
, &button
);
234 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
235 sizeof( UInt32
), NULL
, &click
);
237 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
238 modifiers
+= btnState
;
241 short windowPart
= ::FindWindow(point
, &window
);
243 if ( IsWindowActive(window
) && windowPart
== inContent
)
245 switch ( GetEventKind( event
) )
247 case kEventMouseDown
:
248 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
252 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
255 case kEventMouseMoved
:
256 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
259 case kEventMouseDragged
:
260 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
272 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
274 OSStatus result
= eventNotHandledErr
;
275 OSStatus err
= noErr
;
278 WindowRef windowRef
;
279 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
281 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
282 sizeof( WindowRef
), NULL
, &windowRef
);
284 switch( GetEventKind( event
) )
286 case kEventWindowUpdate
:
287 if ( !wxPendingDelete
.Member(toplevelWindow
) )
288 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
291 case kEventWindowActivated
:
292 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
295 case kEventWindowDeactivated
:
296 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
299 case kEventWindowClose
:
300 toplevelWindow
->Close() ;
303 case kEventWindowBoundsChanged
:
304 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
305 NULL
, sizeof( UInt32
), NULL
, &attributes
);
308 Rect newContentRect
;
310 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
311 sizeof( newContentRect
), NULL
, &newContentRect
);
313 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
314 newContentRect
.right
- newContentRect
.left
,
315 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
326 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
328 OSStatus result
= eventNotHandledErr
;
330 switch ( GetEventClass( event
) )
332 case kEventClassKeyboard
:
333 result
= KeyboardEventHandler( handler
, event
, data
) ;
335 case kEventClassTextInput
:
336 result
= TextInputEventHandler( handler
, event
, data
) ;
338 case kEventClassWindow
:
339 result
= WindowEventHandler( handler
, event
, data
) ;
341 case kEventClassMouse
:
342 result
= MouseEventHandler( handler
, event
, data
) ;
350 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
354 // ---------------------------------------------------------------------------
355 // wxWindowMac utility functions
356 // ---------------------------------------------------------------------------
358 // Find an item given the Macintosh Window Reference
360 wxList
*wxWinMacWindowList
= NULL
;
361 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
363 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
366 return (wxTopLevelWindowMac
*)node
->GetData();
369 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
371 // adding NULL WindowRef is (first) surely a result of an error and
372 // (secondly) breaks menu command processing
373 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
375 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
376 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
379 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
381 wxWinMacWindowList
->DeleteObject(win
);
385 // ----------------------------------------------------------------------------
386 // wxTopLevelWindowMac creation
387 // ----------------------------------------------------------------------------
389 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
391 void wxTopLevelWindowMac::Init()
394 m_maximizeOnShow
= FALSE
;
395 m_macNoEraseUpdateRgn
= NewRgn() ;
396 m_macNeedsErasing
= false ;
399 m_macEventHandler
= NULL
;
403 class wxMacDeferredWindowDeleter
: public wxObject
406 wxMacDeferredWindowDeleter( WindowRef windowRef
)
408 m_macWindow
= windowRef
;
410 virtual ~wxMacDeferredWindowDeleter()
412 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
415 WindowRef m_macWindow
;
418 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
420 const wxString
& title
,
424 const wxString
& name
)
429 m_windowStyle
= style
;
433 m_windowId
= id
== -1 ? NewControlId() : id
;
435 wxTopLevelWindows
.Append(this);
438 parent
->AddChild(this);
443 wxTopLevelWindowMac::~wxTopLevelWindowMac()
447 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
448 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
452 if ( m_macEventHandler
)
454 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
455 m_macEventHandler
= NULL
;
459 wxRemoveMacWindowAssociation( this ) ;
461 if ( wxModelessWindows
.Find(this) )
462 wxModelessWindows
.DeleteObject(this);
464 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
468 // ----------------------------------------------------------------------------
469 // wxTopLevelWindowMac maximize/minimize
470 // ----------------------------------------------------------------------------
472 void wxTopLevelWindowMac::Maximize(bool maximize
)
474 // not available on mac
477 bool wxTopLevelWindowMac::IsMaximized() const
482 void wxTopLevelWindowMac::Iconize(bool iconize
)
484 // not available on mac
487 bool wxTopLevelWindowMac::IsIconized() const
489 // mac dialogs cannot be iconized
493 void wxTopLevelWindowMac::Restore()
495 // not available on mac
498 // ----------------------------------------------------------------------------
499 // wxTopLevelWindowMac misc
500 // ----------------------------------------------------------------------------
502 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
505 wxTopLevelWindowBase::SetIcon(icon
);
508 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
512 const wxString
& name
)
515 m_windowStyle
= style
;
536 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
538 // translate the window attributes in the appropriate window class and attributes
540 WindowClass wclass
= 0;
541 WindowAttributes attr
= kWindowNoAttributes
;
543 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
546 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
547 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
548 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
551 wclass
= kFloatingWindowClass
;
552 if ( HasFlag(wxTINY_CAPTION_VERT
) )
554 attr
|= kWindowSideTitlebarAttribute
;
560 wclass
= kPlainWindowClass
;
562 wclass
= kFloatingWindowClass
;
566 else if ( HasFlag( wxCAPTION
) )
568 wclass
= kDocumentWindowClass
;
572 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
573 HasFlag( wxSYSTEM_MENU
) )
575 wclass
= kDocumentWindowClass
;
580 wclass
= kPlainWindowClass
;
582 wclass
= kModalWindowClass
;
587 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
589 attr
|= kWindowFullZoomAttribute
;
590 attr
|= kWindowCollapseBoxAttribute
;
592 if ( HasFlag( wxRESIZE_BORDER
) )
594 attr
|= kWindowResizableAttribute
;
596 if ( HasFlag( wxSYSTEM_MENU
) )
598 attr
|= kWindowCloseBoxAttribute
;
602 #if 0 // having problems right now with that
603 if (HasFlag(wxSTAY_ON_TOP
))
604 wclass
= kUtilityWindowClass
;
609 if ( HasFlag(wxFRAME_SHAPED
) )
611 WindowDefSpec customWindowDefSpec
;
612 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
613 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
615 ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
616 attr
, &theBoundsRect
,
617 (WindowRef
*) &m_macWindow
);
622 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
625 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
626 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
627 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
629 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
630 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
631 GetEventTypeCount(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
637 if ( HasFlag(wxFRAME_SHAPED
) )
639 // default shape matches the window size
640 wxRegion
rgn(0, 0, m_width
, m_height
);
646 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
648 ((Point
*)localOrigin
)->h
= 0;
649 ((Point
*)localOrigin
)->v
= 0;
650 ((Rect
*)clipRect
)->left
= 0;
651 ((Rect
*)clipRect
)->top
= 0;
652 ((Rect
*)clipRect
)->right
= m_width
;
653 ((Rect
*)clipRect
)->bottom
= m_height
;
654 *window
= m_macWindow
;
658 void wxTopLevelWindowMac::Clear()
663 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
665 return m_macRootControl
;
669 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
671 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
673 RgnHandle visRgn
= NewRgn() ;
674 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
675 BeginUpdate( (WindowRef
)m_macWindow
) ;
677 RgnHandle updateRgn
= NewRgn();
678 RgnHandle diffRgn
= NewRgn() ;
680 if ( updateRgn
&& diffRgn
)
683 // macos internal control redraws clean up areas we'd like to redraw ourselves
684 // therefore we pick the boundary rect and make sure we can redraw it
685 // this has to be intersected by the visRgn in order to avoid drawing over its own
687 RgnHandle trueUpdateRgn
= NewRgn() ;
688 Rect trueUpdateRgnBoundary
;
689 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
690 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
691 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
692 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
694 DisposeRgn( trueUpdateRgn
) ;
695 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
697 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
699 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
700 if ( !EmptyRgn( updateRgn
) )
702 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
706 DisposeRgn( updateRgn
);
708 DisposeRgn( diffRgn
);
710 DisposeRgn( visRgn
) ;
712 EndUpdate( (WindowRef
)m_macWindow
) ;
713 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
714 m_macNeedsErasing
= false ;
718 // Raise the window to the top of the Z order
719 void wxTopLevelWindowMac::Raise()
721 ::SelectWindow( (WindowRef
)m_macWindow
) ;
724 // Lower the window to the bottom of the Z order
725 void wxTopLevelWindowMac::Lower()
727 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
730 void wxTopLevelWindowMac::MacFireMouseEvent(
731 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
733 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
734 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
735 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
737 event
.m_leftDown
= isDown
&& !controlDown
;
739 event
.m_middleDown
= FALSE
;
740 event
.m_rightDown
= isDown
&& controlDown
;
742 if ( kind
== mouseDown
)
745 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
747 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
749 else if ( kind
== mouseUp
)
752 event
.SetEventType(wxEVT_RIGHT_UP
) ;
754 event
.SetEventType(wxEVT_LEFT_UP
) ;
758 event
.SetEventType(wxEVT_MOTION
) ;
761 event
.m_shiftDown
= modifiers
& shiftKey
;
762 event
.m_controlDown
= modifiers
& controlKey
;
763 event
.m_altDown
= modifiers
& optionKey
;
764 event
.m_metaDown
= modifiers
& cmdKey
;
772 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
773 ::GlobalToLocal( &localwhere
) ;
776 if ( kind
== mouseDown
)
778 if ( timestamp
- gs_lastWhen
<= GetDblTime() )
780 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
782 // This is not right if the second mouse down
783 // event occured in a differen window. We
784 // correct this in MacDispatchMouseEvent.
786 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
788 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
794 gs_lastWhen
= timestamp
;
796 gs_lastWhere
= localwhere
;
799 event
.m_x
= localwhere
.h
;
800 event
.m_y
= localwhere
.v
;
804 event
.m_timeStamp
= timestamp
;
805 event
.SetEventObject(this);
806 if ( wxTheApp
->s_captureWindow
)
810 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
813 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
814 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
816 if ( kind
== mouseUp
)
818 wxTheApp
->s_captureWindow
= NULL
;
821 m_cursor
.MacInstall() ;
827 MacDispatchMouseEvent( event
) ;
833 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
835 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
836 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
839 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
845 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
846 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
852 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
858 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
859 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
867 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
869 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
870 event
.m_timeStamp
= timestamp
;
871 event
.SetEventObject(this);
873 GetEventHandler()->ProcessEvent(event
);
875 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
877 // Early versions of MacOS X don't refresh backgrounds properly,
878 // so refresh the whole window on activation and deactivation.
879 long osVersion
= UMAGetSystemVersion();
880 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
886 // for the moment we have to resolve some redrawing issues like this
887 // the OS is stealing some redrawing areas as soon as it draws a control
894 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
900 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
902 wxWindow::SetTitle( title
) ;
903 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
906 bool wxTopLevelWindowMac::Show(bool show
)
908 if ( !wxWindow::Show(show
) )
913 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
914 ::SelectWindow( (WindowRef
)m_macWindow
) ;
915 // no need to generate events here, they will get them triggered by macos
916 // actually they should be , but apparently they are not
917 wxSize
size(m_width
, m_height
);
918 wxSizeEvent
event(size
, m_windowId
);
919 event
.SetEventObject(this);
920 GetEventHandler()->ProcessEvent(event
);
924 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
938 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
942 int former_w
= m_width
;
943 int former_h
= m_height
;
945 int actualWidth
= width
;
946 int actualHeight
= height
;
950 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
951 actualWidth
= m_minWidth
;
952 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
953 actualHeight
= m_minHeight
;
954 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
955 actualWidth
= m_maxWidth
;
956 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
957 actualHeight
= m_maxHeight
;
959 bool doMove
= false ;
960 bool doResize
= false ;
962 if ( actualX
!= former_x
|| actualY
!= former_y
)
966 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
971 if ( doMove
|| doResize
)
975 m_width
= actualWidth
;
976 m_height
= actualHeight
;
979 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
982 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
984 // the OS takes care of invalidating and erasing the new area so we only have to
985 // take care of refreshing for full repaints
987 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
991 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
993 wxFrame
* frame
= (wxFrame
*) this ;
994 frame
->PositionStatusBar();
995 frame
->PositionToolBar();
998 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1000 MacRepositionScrollBars() ;
1003 wxPoint
point(m_x
, m_y
);
1004 wxMoveEvent
event(point
, m_windowId
);
1005 event
.SetEventObject(this);
1006 GetEventHandler()->ProcessEvent(event
) ;
1010 MacRepositionScrollBars() ;
1011 wxSize
size(m_width
, m_height
);
1012 wxSizeEvent
event(size
, m_windowId
);
1013 event
.SetEventObject(this);
1014 GetEventHandler()->ProcessEvent(event
);
1021 * Invalidation Mechanism
1023 * The update mechanism reflects exactely the windows mechanism
1024 * the rect gets added to the window invalidate region, if the eraseBackground flag
1025 * has been true for any part of the update rgn the background is erased in the entire region
1026 * not just in the specified rect.
1028 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1029 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1030 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1031 * will get the eraseBackground event first
1034 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1036 GrafPtr formerPort
;
1037 GetPort( &formerPort
) ;
1038 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1040 m_macNeedsErasing
|= eraseBackground
;
1042 // if we already know that we will have to erase, there's no need to track the rest
1043 if ( !m_macNeedsErasing
)
1045 // we end only here if eraseBackground is false
1046 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1047 // we will have to erase anyway
1049 RgnHandle updateRgn
= NewRgn();
1050 RgnHandle diffRgn
= NewRgn() ;
1051 if ( updateRgn
&& diffRgn
)
1053 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1055 LocalToGlobal( &pt
) ;
1056 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1057 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1058 if ( !EmptyRgn( diffRgn
) )
1060 m_macNeedsErasing
= true ;
1064 DisposeRgn( updateRgn
);
1066 DisposeRgn( diffRgn
);
1068 if ( !m_macNeedsErasing
)
1070 RgnHandle rectRgn
= NewRgn() ;
1071 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1072 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1073 DisposeRgn( rectRgn
) ;
1076 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1077 // turn this on to debug the refreshing cycle
1078 #if wxMAC_DEBUG_REDRAW
1081 SetPort( formerPort
) ;
1085 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1087 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1088 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1091 // The empty region signifies that the shape should be removed from the
1093 if ( region
.IsEmpty() )
1095 wxSize sz
= GetClientSize();
1096 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1097 return SetShape(rgn
);
1100 // Make a copy of the region
1101 RgnHandle shapeRegion
= NewRgn();
1102 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1104 // Dispose of any shape region we may already have
1105 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1109 // Save the region so we can use it later
1110 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1112 // Tell the window manager that the window has changed shape
1113 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1120 // ---------------------------------------------------------------------------
1121 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1122 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1123 // ---------------------------------------------------------------------------
1127 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1129 GetWindowPortBounds(window
, inRect
);
1130 Point pt
= {inRect
->left
, inRect
->top
};
1131 SetPort((GrafPtr
) GetWindowPort(window
));
1134 inRect
->left
= pt
.h
;
1135 inRect
->bottom
+= pt
.v
;
1136 inRect
->right
+= pt
.h
;
1140 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1142 /*------------------------------------------------------
1143 Define which options your custom window supports.
1144 --------------------------------------------------------*/
1145 //just enable everything for our demo
1146 *(OptionBits
*)param
=//kWindowCanGrow|
1148 //kWindowCanCollapse|
1149 //kWindowCanGetWindowRegion|
1150 //kWindowHasTitleBar|
1151 //kWindowSupportsDragHilite|
1152 kWindowCanDrawInCurrentPort
|
1153 //kWindowCanMeasureTitle|
1154 kWindowWantsDisposeAtProcessDeath
|
1155 kWindowSupportsSetGrowImageRegion
|
1156 kWindowDefSupportsColorGrafPort
;
1160 // The content region is left as a rectangle matching the window size, this is
1161 // so the origin in the paint event, and etc. still matches what the
1162 // programmer expects.
1163 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1166 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1169 wxRect r
= win
->GetRect();
1170 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1174 // The structure region is set to the shape given to the SetShape method.
1175 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1177 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1183 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1184 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1185 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1186 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1192 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1194 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1196 switch(rgnRec
->regionCode
)
1198 case kWindowStructureRgn
:
1199 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1201 case kWindowContentRgn
:
1202 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1205 SetEmptyRgn(rgnRec
->winRgn
);
1212 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1214 /*------------------------------------------------------
1215 Determine the region of the window which was hit
1216 --------------------------------------------------------*/
1218 static RgnHandle tempRgn
=nil
;
1223 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1225 //Mac OS 8.5 or later
1226 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1227 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1230 return wNoHit
;//no significant area was hit.
1234 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1238 case kWindowMsgHitTest
:
1239 return wxShapedMacWindowHitTest(window
,param
);
1241 case kWindowMsgGetFeatures
:
1242 return wxShapedMacWindowGetFeatures(window
,param
);
1244 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1245 case kWindowMsgGetRegion
:
1246 return wxShapedMacWindowGetRegion(window
,param
);
1253 // ---------------------------------------------------------------------------