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 #define wxMAC_DEBUG_REDRAW 0
47 #ifndef wxMAC_DEBUG_REDRAW
48 #define wxMAC_DEBUG_REDRAW 0
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // list of all frames and modeless dialogs
56 wxWindowList wxModelessWindows
;
58 // double click testing
59 static Point gs_lastWhere
;
60 static long gs_lastWhen
= 0;
63 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
65 // ============================================================================
66 // wxTopLevelWindowMac implementation
67 // ============================================================================
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
75 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
77 static const EventTypeSpec eventList
[] =
79 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
81 { kEventClassKeyboard
, kEventRawKeyDown
} ,
82 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
83 { kEventClassKeyboard
, kEventRawKeyUp
} ,
84 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
86 { kEventClassWindow
, kEventWindowUpdate
} ,
87 { kEventClassWindow
, kEventWindowActivated
} ,
88 { kEventClassWindow
, kEventWindowDeactivated
} ,
89 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
90 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
91 { kEventClassWindow
, kEventWindowClose
} ,
93 { kEventClassMouse
, kEventMouseDown
} ,
94 { kEventClassMouse
, kEventMouseUp
} ,
95 { kEventClassMouse
, kEventMouseMoved
} ,
96 { kEventClassMouse
, kEventMouseDragged
} ,
100 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
102 OSStatus result
= eventNotHandledErr
;
104 wxWindow
* focus
= wxWindow::FindFocus() ;
109 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
113 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
115 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
116 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
117 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
118 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
119 sizeof( Point
), NULL
, &point
);
121 UInt32 message
= (keyCode
<< 8) + charCode
;
123 switch ( GetEventKind( event
) )
125 case kEventTextInputUnicodeForKeyEvent
:
126 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
127 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
137 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
139 OSStatus result
= eventNotHandledErr
;
141 wxWindow
* focus
= wxWindow::FindFocus() ;
146 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
148 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
149 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
150 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
151 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
152 sizeof( Point
), NULL
, &point
);
154 UInt32 message
= (keyCode
<< 8) + charCode
;
155 switch( GetEventKind( event
) )
157 case kEventRawKeyRepeat
:
158 case kEventRawKeyDown
:
159 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
160 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
165 case kEventRawKeyUp
:
166 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
167 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
172 case kEventRawKeyModifiersChanged
:
174 wxKeyEvent
event(wxEVT_KEY_DOWN
);
176 event
.m_shiftDown
= modifiers
& shiftKey
;
177 event
.m_controlDown
= modifiers
& controlKey
;
178 event
.m_altDown
= modifiers
& optionKey
;
179 event
.m_metaDown
= modifiers
& cmdKey
;
183 event
.m_timeStamp
= when
;
184 wxWindow
* focus
= wxWindow::FindFocus() ;
185 event
.SetEventObject(focus
);
187 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
189 event
.m_keyCode
= WXK_CONTROL
;
190 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
191 focus
->GetEventHandler()->ProcessEvent( event
) ;
193 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
195 event
.m_keyCode
= WXK_SHIFT
;
196 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
197 focus
->GetEventHandler()->ProcessEvent( event
) ;
199 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
201 event
.m_keyCode
= WXK_ALT
;
202 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
203 focus
->GetEventHandler()->ProcessEvent( event
) ;
205 wxTheApp
->s_lastModifiers
= modifiers
;
213 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
215 OSStatus result
= eventNotHandledErr
;
217 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
219 UInt32 modifiers
= 0;
220 EventMouseButton button
= 0 ;
223 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
224 sizeof( Point
), NULL
, &point
);
225 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
226 sizeof( UInt32
), NULL
, &modifiers
);
227 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
228 sizeof( EventMouseButton
), NULL
, &button
);
229 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
230 sizeof( UInt32
), NULL
, &click
);
232 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
233 modifiers
+= btnState
;
236 short windowPart
= ::FindWindow(point
, &window
);
238 if ( IsWindowActive(window
) && windowPart
== inContent
)
240 switch ( GetEventKind( event
) )
242 case kEventMouseDown
:
243 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
247 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
250 case kEventMouseMoved
:
251 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
254 case kEventMouseDragged
:
255 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
267 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
269 OSStatus result
= eventNotHandledErr
;
270 OSStatus err
= noErr
;
273 WindowRef windowRef
;
274 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
276 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
277 sizeof( WindowRef
), NULL
, &windowRef
);
279 switch( GetEventKind( event
) )
281 case kEventWindowUpdate
:
282 if ( !wxPendingDelete
.Member(toplevelWindow
) )
283 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
286 case kEventWindowActivated
:
287 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
290 case kEventWindowDeactivated
:
291 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
294 case kEventWindowClose
:
295 toplevelWindow
->Close() ;
298 case kEventWindowBoundsChanged
:
299 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
300 NULL
, sizeof( UInt32
), NULL
, &attributes
);
303 Rect newContentRect
;
305 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
306 sizeof( newContentRect
), NULL
, &newContentRect
);
308 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
309 newContentRect
.right
- newContentRect
.left
,
310 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
321 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
323 OSStatus result
= eventNotHandledErr
;
325 switch ( GetEventClass( event
) )
327 case kEventClassKeyboard
:
328 result
= KeyboardEventHandler( handler
, event
, data
) ;
330 case kEventClassTextInput
:
331 result
= TextInputEventHandler( handler
, event
, data
) ;
333 case kEventClassWindow
:
334 result
= WindowEventHandler( handler
, event
, data
) ;
336 case kEventClassMouse
:
337 result
= MouseEventHandler( handler
, event
, data
) ;
345 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
349 // ---------------------------------------------------------------------------
350 // wxWindowMac utility functions
351 // ---------------------------------------------------------------------------
353 // Find an item given the Macintosh Window Reference
355 wxList
*wxWinMacWindowList
= NULL
;
356 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
358 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
361 return (wxTopLevelWindowMac
*)node
->GetData();
364 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
366 // adding NULL WindowRef is (first) surely a result of an error and
367 // (secondly) breaks menu command processing
368 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
370 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
371 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
374 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
376 wxWinMacWindowList
->DeleteObject(win
);
380 // ----------------------------------------------------------------------------
381 // wxTopLevelWindowMac creation
382 // ----------------------------------------------------------------------------
384 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
386 void wxTopLevelWindowMac::Init()
389 m_maximizeOnShow
= FALSE
;
390 m_macNoEraseUpdateRgn
= NewRgn() ;
391 m_macNeedsErasing
= false ;
394 m_macEventHandler
= NULL
;
398 class wxMacDeferredWindowDeleter
: public wxObject
401 wxMacDeferredWindowDeleter( WindowRef windowRef
)
403 m_macWindow
= windowRef
;
405 virtual ~wxMacDeferredWindowDeleter()
407 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
410 WindowRef m_macWindow
;
413 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
415 const wxString
& title
,
419 const wxString
& name
)
424 m_windowStyle
= style
;
428 m_windowId
= id
== -1 ? NewControlId() : id
;
430 wxTopLevelWindows
.Append(this);
433 parent
->AddChild(this);
438 wxTopLevelWindowMac::~wxTopLevelWindowMac()
442 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
443 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
447 if ( m_macEventHandler
)
449 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
450 m_macEventHandler
= NULL
;
454 wxRemoveMacWindowAssociation( this ) ;
456 if ( wxModelessWindows
.Find(this) )
457 wxModelessWindows
.DeleteObject(this);
459 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
463 // ----------------------------------------------------------------------------
464 // wxTopLevelWindowMac maximize/minimize
465 // ----------------------------------------------------------------------------
467 void wxTopLevelWindowMac::Maximize(bool maximize
)
469 // not available on mac
472 bool wxTopLevelWindowMac::IsMaximized() const
477 void wxTopLevelWindowMac::Iconize(bool iconize
)
479 // not available on mac
482 bool wxTopLevelWindowMac::IsIconized() const
484 // mac dialogs cannot be iconized
488 void wxTopLevelWindowMac::Restore()
490 // not available on mac
493 // ----------------------------------------------------------------------------
494 // wxTopLevelWindowMac misc
495 // ----------------------------------------------------------------------------
497 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
500 wxTopLevelWindowBase::SetIcon(icon
);
503 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
507 const wxString
& name
)
510 m_windowStyle
= style
;
531 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
533 // translate the window attributes in the appropriate window class and attributes
535 WindowClass wclass
= 0;
536 WindowAttributes attr
= kWindowNoAttributes
;
538 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
541 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
542 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
543 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
546 wclass
= kFloatingWindowClass
;
547 if ( HasFlag(wxTINY_CAPTION_VERT
) )
549 attr
|= kWindowSideTitlebarAttribute
;
555 wclass
= kPlainWindowClass
;
557 wclass
= kFloatingWindowClass
;
561 else if ( HasFlag( wxCAPTION
) )
563 wclass
= kDocumentWindowClass
;
567 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
568 HasFlag( wxSYSTEM_MENU
) )
570 wclass
= kDocumentWindowClass
;
575 wclass
= kPlainWindowClass
;
577 wclass
= kModalWindowClass
;
582 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
584 attr
|= kWindowFullZoomAttribute
;
585 attr
|= kWindowCollapseBoxAttribute
;
587 if ( HasFlag( wxRESIZE_BORDER
) )
589 attr
|= kWindowResizableAttribute
;
591 if ( HasFlag( wxSYSTEM_MENU
) )
593 attr
|= kWindowCloseBoxAttribute
;
596 if (HasFlag(wxSTAY_ON_TOP
))
597 wclass
= kUtilityWindowClass
;
599 if ( HasFlag(wxFRAME_SHAPED
) )
601 WindowDefSpec customWindowDefSpec
;
602 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
603 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
605 ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
606 attr
, &theBoundsRect
,
607 (WindowRef
*) &m_macWindow
);
611 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
614 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
615 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
616 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
618 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
619 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
620 GetEventTypeCount(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
625 if ( HasFlag(wxFRAME_SHAPED
) )
627 // default shape matches the window size
628 wxRegion
rgn(0, 0, m_width
, m_height
);
633 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
635 ((Point
*)localOrigin
)->h
= 0;
636 ((Point
*)localOrigin
)->v
= 0;
637 ((Rect
*)clipRect
)->left
= 0;
638 ((Rect
*)clipRect
)->top
= 0;
639 ((Rect
*)clipRect
)->right
= m_width
;
640 ((Rect
*)clipRect
)->bottom
= m_height
;
641 *window
= m_macWindow
;
645 void wxTopLevelWindowMac::Clear()
650 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
652 return m_macRootControl
;
656 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
658 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
660 BeginUpdate( (WindowRef
)m_macWindow
) ;
662 RgnHandle updateRgn
= NewRgn();
663 RgnHandle diffRgn
= NewRgn() ;
664 if ( updateRgn
&& diffRgn
)
667 // macos internal control redraws clean up areas we'd like to redraw ourselves
668 // therefore we pick the boundary rect and make sure we can redraw it
669 RgnHandle trueUpdateRgn
= NewRgn() ;
670 Rect trueUpdateRgnBoundary
;
671 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
672 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
673 RectRgn( (RgnHandle
) updateRgn
, &trueUpdateRgnBoundary
) ;
675 DisposeRgn( trueUpdateRgn
) ;
676 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
678 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
680 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
681 if ( !EmptyRgn( updateRgn
) )
683 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
687 DisposeRgn( updateRgn
);
689 DisposeRgn( diffRgn
);
690 EndUpdate( (WindowRef
)m_macWindow
) ;
691 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
692 m_macNeedsErasing
= false ;
696 // Raise the window to the top of the Z order
697 void wxTopLevelWindowMac::Raise()
699 ::SelectWindow( (WindowRef
)m_macWindow
) ;
702 // Lower the window to the bottom of the Z order
703 void wxTopLevelWindowMac::Lower()
705 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
708 void wxTopLevelWindowMac::MacFireMouseEvent(
709 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
711 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
712 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
713 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
715 event
.m_leftDown
= isDown
&& !controlDown
;
717 event
.m_middleDown
= FALSE
;
718 event
.m_rightDown
= isDown
&& controlDown
;
720 if ( kind
== mouseDown
)
723 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
725 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
727 else if ( kind
== mouseUp
)
730 event
.SetEventType(wxEVT_RIGHT_UP
) ;
732 event
.SetEventType(wxEVT_LEFT_UP
) ;
736 event
.SetEventType(wxEVT_MOTION
) ;
739 event
.m_shiftDown
= modifiers
& shiftKey
;
740 event
.m_controlDown
= modifiers
& controlKey
;
741 event
.m_altDown
= modifiers
& optionKey
;
742 event
.m_metaDown
= modifiers
& cmdKey
;
750 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
751 ::GlobalToLocal( &localwhere
) ;
754 if ( kind
== mouseDown
)
756 if ( timestamp
- gs_lastWhen
<= GetDblTime() )
758 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
760 // This is not right if the second mouse down
761 // event occured in a differen window. We
762 // correct this in MacDispatchMouseEvent.
764 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
766 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
772 gs_lastWhen
= timestamp
;
774 gs_lastWhere
= localwhere
;
777 event
.m_x
= localwhere
.h
;
778 event
.m_y
= localwhere
.v
;
782 event
.m_timeStamp
= timestamp
;
783 event
.SetEventObject(this);
784 if ( wxTheApp
->s_captureWindow
)
788 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
791 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
792 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
794 if ( kind
== mouseUp
)
796 wxTheApp
->s_captureWindow
= NULL
;
799 m_cursor
.MacInstall() ;
805 MacDispatchMouseEvent( event
) ;
811 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
813 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
814 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
817 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
823 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
824 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
830 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
836 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
837 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
845 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
847 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
848 event
.m_timeStamp
= timestamp
;
849 event
.SetEventObject(this);
851 GetEventHandler()->ProcessEvent(event
);
853 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
855 // Early versions of MacOS X don't refresh backgrounds properly,
856 // so refresh the whole window on activation and deactivation.
857 long osVersion
= UMAGetSystemVersion();
858 if (osVersion
>= 0x1000 && osVersion
< 0x1020)
864 // for the moment we have to resolve some redrawing issues like this
865 // the OS is stealing some redrawing areas as soon as it draws a control
872 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
878 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
880 wxWindow::SetTitle( title
) ;
881 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
884 bool wxTopLevelWindowMac::Show(bool show
)
886 if ( !wxWindow::Show(show
) )
891 // this is leading to incorrect window layering in some situations
892 // ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
893 ::ShowWindow( (WindowRef
)m_macWindow
) ;
894 ::SelectWindow( (WindowRef
)m_macWindow
) ;
895 // no need to generate events here, they will get them triggered by macos
896 // actually they should be , but apparently they are not
897 wxSize
size(m_width
, m_height
);
898 wxSizeEvent
event(size
, m_windowId
);
899 event
.SetEventObject(this);
900 GetEventHandler()->ProcessEvent(event
);
904 // this is leading to incorrect window layering in some situations
905 // ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
906 ::HideWindow( (WindowRef
)m_macWindow
) ;
920 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
924 int former_w
= m_width
;
925 int former_h
= m_height
;
927 int actualWidth
= width
;
928 int actualHeight
= height
;
932 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
933 actualWidth
= m_minWidth
;
934 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
935 actualHeight
= m_minHeight
;
936 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
937 actualWidth
= m_maxWidth
;
938 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
939 actualHeight
= m_maxHeight
;
941 bool doMove
= false ;
942 bool doResize
= false ;
944 if ( actualX
!= former_x
|| actualY
!= former_y
)
948 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
953 if ( doMove
|| doResize
)
957 m_width
= actualWidth
;
958 m_height
= actualHeight
;
961 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
964 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
966 // the OS takes care of invalidating and erasing the new area so we only have to
967 // take care of refreshing for full repaints
969 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
973 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
975 wxFrame
* frame
= (wxFrame
*) this ;
976 frame
->PositionStatusBar();
977 frame
->PositionToolBar();
980 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
982 MacRepositionScrollBars() ;
985 wxPoint
point(m_x
, m_y
);
986 wxMoveEvent
event(point
, m_windowId
);
987 event
.SetEventObject(this);
988 GetEventHandler()->ProcessEvent(event
) ;
992 MacRepositionScrollBars() ;
993 wxSize
size(m_width
, m_height
);
994 wxSizeEvent
event(size
, m_windowId
);
995 event
.SetEventObject(this);
996 GetEventHandler()->ProcessEvent(event
);
1003 * Invalidation Mechanism
1005 * The update mechanism reflects exactely the windows mechanism
1006 * the rect gets added to the window invalidate region, if the eraseBackground flag
1007 * has been true for any part of the update rgn the background is erased in the entire region
1008 * not just in the specified rect.
1010 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1011 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1012 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1013 * will get the eraseBackground event first
1016 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1018 GrafPtr formerPort
;
1019 GetPort( &formerPort
) ;
1020 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1022 m_macNeedsErasing
|= eraseBackground
;
1024 // if we already know that we will have to erase, there's no need to track the rest
1025 if ( !m_macNeedsErasing
)
1027 // we end only here if eraseBackground is false
1028 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1029 // we will have to erase anyway
1031 RgnHandle updateRgn
= NewRgn();
1032 RgnHandle diffRgn
= NewRgn() ;
1033 if ( updateRgn
&& diffRgn
)
1035 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1037 LocalToGlobal( &pt
) ;
1038 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1039 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1040 if ( !EmptyRgn( diffRgn
) )
1042 m_macNeedsErasing
= true ;
1046 DisposeRgn( updateRgn
);
1048 DisposeRgn( diffRgn
);
1050 if ( !m_macNeedsErasing
)
1052 RgnHandle rectRgn
= NewRgn() ;
1053 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1054 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1055 DisposeRgn( rectRgn
) ;
1058 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1059 // turn this on to debug the refreshing cycle
1060 #if wxMAC_DEBUG_REDRAW
1063 SetPort( formerPort
) ;
1067 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1069 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1070 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1072 // The empty region signifies that the shape should be removed from the
1074 if ( region
.IsEmpty() )
1076 wxSize sz
= GetClientSize();
1077 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1078 return SetShape(rgn
);
1081 // Make a copy of the region
1082 RgnHandle shapeRegion
= NewRgn();
1083 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1085 // Dispose of any shape region we may already have
1086 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1090 // Save the region so we can use it later
1091 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1093 // Tell the window manager that the window has changed shape
1094 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1100 // ---------------------------------------------------------------------------
1101 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1102 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1103 // ---------------------------------------------------------------------------
1106 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1108 GetWindowPortBounds(window
, inRect
);
1109 Point pt
= {inRect
->left
, inRect
->top
};
1110 SetPort((GrafPtr
) GetWindowPort(window
));
1113 inRect
->left
= pt
.h
;
1114 inRect
->bottom
+= pt
.v
;
1115 inRect
->right
+= pt
.h
;
1119 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1121 /*------------------------------------------------------
1122 Define which options your custom window supports.
1123 --------------------------------------------------------*/
1124 //just enable everything for our demo
1125 *(OptionBits
*)param
=//kWindowCanGrow|
1127 //kWindowCanCollapse|
1128 //kWindowCanGetWindowRegion|
1129 //kWindowHasTitleBar|
1130 //kWindowSupportsDragHilite|
1131 kWindowCanDrawInCurrentPort
|
1132 //kWindowCanMeasureTitle|
1133 kWindowWantsDisposeAtProcessDeath
|
1134 kWindowSupportsSetGrowImageRegion
|
1135 kWindowDefSupportsColorGrafPort
;
1139 // The content region is left as a rectangle matching the window size, this is
1140 // so the origin in the paint event, and etc. still matches what the
1141 // programmer expects.
1142 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1145 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1148 wxRect r
= win
->GetRect();
1149 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1153 // The structure region is set to the shape given to the SetShape method.
1154 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1156 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1162 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1163 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1164 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1165 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1171 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1173 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1175 switch(rgnRec
->regionCode
)
1177 case kWindowStructureRgn
:
1178 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1180 case kWindowContentRgn
:
1181 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1184 SetEmptyRgn(rgnRec
->winRgn
);
1191 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1193 /*------------------------------------------------------
1194 Determine the region of the window which was hit
1195 --------------------------------------------------------*/
1197 static RgnHandle tempRgn
=nil
;
1202 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1204 //Mac OS 8.5 or later
1205 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1206 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1209 return wNoHit
;//no significant area was hit.
1213 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1217 case kWindowMsgHitTest
:
1218 return wxShapedMacWindowHitTest(window
,param
);
1220 case kWindowMsgGetFeatures
:
1221 return wxShapedMacWindowGetFeatures(window
,param
);
1223 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1224 case kWindowMsgGetRegion
:
1225 return wxShapedMacWindowGetRegion(window
,param
);
1231 // ---------------------------------------------------------------------------