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 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
180 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
181 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
182 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
183 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
187 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
189 case kEventRawKeyUp
:
190 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
191 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
196 case kEventRawKeyModifiersChanged
:
198 wxKeyEvent
event(wxEVT_KEY_DOWN
);
200 event
.m_shiftDown
= modifiers
& shiftKey
;
201 event
.m_controlDown
= modifiers
& controlKey
;
202 event
.m_altDown
= modifiers
& optionKey
;
203 event
.m_metaDown
= modifiers
& cmdKey
;
207 event
.m_timeStamp
= when
;
208 wxWindow
* focus
= wxWindow::FindFocus() ;
209 event
.SetEventObject(focus
);
211 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
213 event
.m_keyCode
= WXK_CONTROL
;
214 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
215 focus
->GetEventHandler()->ProcessEvent( event
) ;
217 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
219 event
.m_keyCode
= WXK_SHIFT
;
220 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
221 focus
->GetEventHandler()->ProcessEvent( event
) ;
223 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
225 event
.m_keyCode
= WXK_ALT
;
226 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
227 focus
->GetEventHandler()->ProcessEvent( event
) ;
229 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
231 event
.m_keyCode
= WXK_COMMAND
;
232 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
233 focus
->GetEventHandler()->ProcessEvent( event
) ;
235 wxTheApp
->s_lastModifiers
= modifiers
;
243 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
245 OSStatus result
= eventNotHandledErr
;
247 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
249 UInt32 modifiers
= 0;
250 EventMouseButton button
= 0 ;
253 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
254 sizeof( Point
), NULL
, &point
);
255 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
256 sizeof( UInt32
), NULL
, &modifiers
);
257 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
258 sizeof( EventMouseButton
), NULL
, &button
);
259 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
260 sizeof( UInt32
), NULL
, &click
);
262 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
263 modifiers
+= btnState
;
266 short windowPart
= ::FindWindow(point
, &window
);
268 if ( IsWindowActive(window
) && windowPart
== inContent
)
270 switch ( GetEventKind( event
) )
272 case kEventMouseDown
:
273 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
277 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
280 case kEventMouseMoved
:
281 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
284 case kEventMouseDragged
:
285 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
297 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
299 OSStatus result
= eventNotHandledErr
;
300 OSStatus err
= noErr
;
303 WindowRef windowRef
;
304 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
306 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
307 sizeof( WindowRef
), NULL
, &windowRef
);
309 switch( GetEventKind( event
) )
311 case kEventWindowUpdate
:
312 if ( !wxPendingDelete
.Member(toplevelWindow
) )
313 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
316 case kEventWindowActivated
:
317 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
320 case kEventWindowDeactivated
:
321 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
324 case kEventWindowClose
:
325 toplevelWindow
->Close() ;
328 case kEventWindowBoundsChanged
:
329 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
330 NULL
, sizeof( UInt32
), NULL
, &attributes
);
333 Rect newContentRect
;
335 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
336 sizeof( newContentRect
), NULL
, &newContentRect
);
338 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
339 newContentRect
.right
- newContentRect
.left
,
340 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
345 case kEventWindowBoundsChanging
:
346 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
347 NULL
, sizeof( UInt32
), NULL
, &attributes
);
350 Rect newContentRect
;
352 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
353 sizeof( newContentRect
), NULL
, &newContentRect
);
355 wxRect
contentRect(newContentRect
.left
, newContentRect
.top
,
356 newContentRect
.right
- newContentRect
.left
,
357 newContentRect
.bottom
- newContentRect
.top
) ;
359 bool handled
= false ;
360 if ((attributes
& kWindowBoundsChangeSizeChanged
) != 0)
362 wxSizeEvent
event(contentRect
, toplevelWindow
->GetId());
363 event
.SetEventObject(toplevelWindow
);
364 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
365 contentRect
= event
.GetRect() ;
367 else if ( attributes
& kWindowBoundsChangeOriginChanged
!= 0)
369 wxMoveEvent
event(contentRect
, toplevelWindow
->GetId());
370 event
.SetEventObject(toplevelWindow
);
371 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
372 contentRect
= event
.GetRect() ;
376 SetRect( &newContentRect
, contentRect
.GetLeft() , contentRect
.GetTop() , contentRect
.GetRight() , contentRect
.GetBottom() ) ;
377 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( newContentRect
), &newContentRect
);
388 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
390 OSStatus result
= eventNotHandledErr
;
392 switch ( GetEventClass( event
) )
394 case kEventClassKeyboard
:
395 result
= KeyboardEventHandler( handler
, event
, data
) ;
397 case kEventClassTextInput
:
398 result
= TextInputEventHandler( handler
, event
, data
) ;
400 case kEventClassWindow
:
401 result
= WindowEventHandler( handler
, event
, data
) ;
403 case kEventClassMouse
:
404 result
= MouseEventHandler( handler
, event
, data
) ;
412 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
416 // ---------------------------------------------------------------------------
417 // wxWindowMac utility functions
418 // ---------------------------------------------------------------------------
420 // Find an item given the Macintosh Window Reference
422 wxList
*wxWinMacWindowList
= NULL
;
423 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
425 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
428 return (wxTopLevelWindowMac
*)node
->GetData();
431 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
433 // adding NULL WindowRef is (first) surely a result of an error and
434 // (secondly) breaks menu command processing
435 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
437 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
438 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
441 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
443 wxWinMacWindowList
->DeleteObject(win
);
447 // ----------------------------------------------------------------------------
448 // wxTopLevelWindowMac creation
449 // ----------------------------------------------------------------------------
451 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
452 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
454 void wxTopLevelWindowMac::Init()
457 m_maximizeOnShow
= FALSE
;
458 m_macNoEraseUpdateRgn
= NewRgn() ;
459 m_macNeedsErasing
= false ;
462 m_macEventHandler
= NULL
;
466 class wxMacDeferredWindowDeleter
: public wxObject
469 wxMacDeferredWindowDeleter( WindowRef windowRef
)
471 m_macWindow
= windowRef
;
473 virtual ~wxMacDeferredWindowDeleter()
475 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
478 WindowRef m_macWindow
;
481 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
483 const wxString
& title
,
487 const wxString
& name
)
492 m_windowStyle
= style
;
496 m_windowId
= id
== -1 ? NewControlId() : id
;
498 wxTopLevelWindows
.Append(this);
501 parent
->AddChild(this);
506 wxTopLevelWindowMac::~wxTopLevelWindowMac()
510 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
511 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
515 if ( m_macEventHandler
)
517 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
518 m_macEventHandler
= NULL
;
522 wxRemoveMacWindowAssociation( this ) ;
524 if ( wxModelessWindows
.Find(this) )
525 wxModelessWindows
.DeleteObject(this);
527 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
531 // ----------------------------------------------------------------------------
532 // wxTopLevelWindowMac maximize/minimize
533 // ----------------------------------------------------------------------------
535 void wxTopLevelWindowMac::Maximize(bool maximize
)
537 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
542 Point pt
= { 0, 0 } ;
543 SetPortWindowPort((WindowRef
)m_macWindow
) ;
544 LocalToGlobal( &pt
) ;
547 GetWindowPortBounds((WindowRef
)m_macWindow
, &tempRect
) ;
548 SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
549 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
552 bool wxTopLevelWindowMac::IsMaximized() const
554 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
557 void wxTopLevelWindowMac::Iconize(bool iconize
)
559 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
560 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
563 bool wxTopLevelWindowMac::IsIconized() const
565 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
568 void wxTopLevelWindowMac::Restore()
570 // not available on mac
573 // ----------------------------------------------------------------------------
574 // wxTopLevelWindowMac misc
575 // ----------------------------------------------------------------------------
577 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
580 wxTopLevelWindowBase::SetIcon(icon
);
583 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
587 const wxString
& name
)
589 OSStatus err
= noErr
;
591 m_windowStyle
= style
;
612 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
614 // translate the window attributes in the appropriate window class and attributes
616 WindowClass wclass
= 0;
617 WindowAttributes attr
= kWindowNoAttributes
;
619 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
622 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
623 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
624 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
627 wclass
= kFloatingWindowClass
;
628 if ( HasFlag(wxTINY_CAPTION_VERT
) )
630 attr
|= kWindowSideTitlebarAttribute
;
636 wclass
= kPlainWindowClass
;
638 wclass
= kFloatingWindowClass
;
642 else if ( HasFlag( wxCAPTION
) )
644 wclass
= kDocumentWindowClass
;
648 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
649 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
651 wclass
= kDocumentWindowClass
;
656 wclass
= kPlainWindowClass
;
658 wclass
= kModalWindowClass
;
663 if ( HasFlag( wxMINIMIZE_BOX
) )
665 attr
|= kWindowCollapseBoxAttribute
;
667 if ( HasFlag( wxMAXIMIZE_BOX
) )
669 attr
|= kWindowFullZoomAttribute
;
671 if ( HasFlag( wxRESIZE_BORDER
) )
673 attr
|= kWindowResizableAttribute
;
675 if ( HasFlag( wxCLOSE_BOX
) )
677 attr
|= kWindowCloseBoxAttribute
;
681 #if 0 // having problems right now with that
682 if (HasFlag(wxSTAY_ON_TOP
))
683 wclass
= kUtilityWindowClass
;
688 if ( HasFlag(wxFRAME_SHAPED
) )
690 WindowDefSpec customWindowDefSpec
;
691 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
692 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
694 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
695 attr
, &theBoundsRect
,
696 (WindowRef
*) &m_macWindow
);
701 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
704 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
705 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
706 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
707 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
709 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
710 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
711 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
717 if ( HasFlag(wxFRAME_SHAPED
) )
719 // default shape matches the window size
720 wxRegion
rgn(0, 0, m_width
, m_height
);
726 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
728 ((Point
*)localOrigin
)->h
= 0;
729 ((Point
*)localOrigin
)->v
= 0;
730 ((Rect
*)clipRect
)->left
= 0;
731 ((Rect
*)clipRect
)->top
= 0;
732 ((Rect
*)clipRect
)->right
= m_width
;
733 ((Rect
*)clipRect
)->bottom
= m_height
;
734 *window
= m_macWindow
;
738 void wxTopLevelWindowMac::ClearBackground()
740 wxWindow::ClearBackground() ;
743 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
745 return m_macRootControl
;
749 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
751 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
753 RgnHandle visRgn
= NewRgn() ;
754 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
755 BeginUpdate( (WindowRef
)m_macWindow
) ;
757 RgnHandle updateRgn
= NewRgn();
758 RgnHandle diffRgn
= NewRgn() ;
760 if ( updateRgn
&& diffRgn
)
763 // macos internal control redraws clean up areas we'd like to redraw ourselves
764 // therefore we pick the boundary rect and make sure we can redraw it
765 // this has to be intersected by the visRgn in order to avoid drawing over its own
767 RgnHandle trueUpdateRgn
= NewRgn() ;
768 Rect trueUpdateRgnBoundary
;
769 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
770 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
771 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
772 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
774 DisposeRgn( trueUpdateRgn
) ;
775 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
777 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
779 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
780 if ( !EmptyRgn( updateRgn
) )
782 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
786 DisposeRgn( updateRgn
);
788 DisposeRgn( diffRgn
);
790 DisposeRgn( visRgn
) ;
792 EndUpdate( (WindowRef
)m_macWindow
) ;
793 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
794 m_macNeedsErasing
= false ;
798 // Raise the window to the top of the Z order
799 void wxTopLevelWindowMac::Raise()
801 ::SelectWindow( (WindowRef
)m_macWindow
) ;
804 // Lower the window to the bottom of the Z order
805 void wxTopLevelWindowMac::Lower()
807 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
810 void wxTopLevelWindowMac::MacFireMouseEvent(
811 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
813 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
814 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
815 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
817 event
.m_leftDown
= isDown
&& !controlDown
;
819 event
.m_middleDown
= FALSE
;
820 event
.m_rightDown
= isDown
&& controlDown
;
822 if ( kind
== mouseDown
)
825 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
827 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
829 else if ( kind
== mouseUp
)
832 event
.SetEventType(wxEVT_RIGHT_UP
) ;
834 event
.SetEventType(wxEVT_LEFT_UP
) ;
838 event
.SetEventType(wxEVT_MOTION
) ;
841 event
.m_shiftDown
= modifiers
& shiftKey
;
842 event
.m_controlDown
= modifiers
& controlKey
;
843 event
.m_altDown
= modifiers
& optionKey
;
844 event
.m_metaDown
= modifiers
& cmdKey
;
852 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
853 ::GlobalToLocal( &localwhere
) ;
856 if ( kind
== mouseDown
)
858 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
860 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
862 // This is not right if the second mouse down
863 // event occured in a differen window. We
864 // correct this in MacDispatchMouseEvent.
866 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
868 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
874 gs_lastWhen
= timestamp
;
876 gs_lastWhere
= localwhere
;
879 event
.m_x
= localwhere
.h
;
880 event
.m_y
= localwhere
.v
;
884 event
.m_timeStamp
= timestamp
;
885 event
.SetEventObject(this);
886 if ( wxTheApp
->s_captureWindow
)
890 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
893 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
894 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
896 if ( kind
== mouseUp
)
898 wxTheApp
->s_captureWindow
= NULL
;
901 m_cursor
.MacInstall() ;
907 MacDispatchMouseEvent( event
) ;
913 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
915 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
916 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
919 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
925 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
926 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
932 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
938 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
939 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
947 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
949 if(s_macDeactivateWindow
)
951 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
952 s_macDeactivateWindow
->MacActivate(timestamp
, false);
956 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
958 wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
959 if(s_macDeactivateWindow
==this)
960 s_macDeactivateWindow
=NULL
;
961 MacDelayedDeactivation(timestamp
);
962 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
963 event
.m_timeStamp
= timestamp
;
964 event
.SetEventObject(this);
966 GetEventHandler()->ProcessEvent(event
);
968 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
970 // Early versions of MacOS X don't refresh backgrounds properly,
971 // so refresh the whole window on activation and deactivation.
972 long osVersion
= UMAGetSystemVersion();
973 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
979 // for the moment we have to resolve some redrawing issues like this
980 // the OS is stealing some redrawing areas as soon as it draws a control
987 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
993 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
995 wxWindow::SetTitle( title
) ;
996 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
999 bool wxTopLevelWindowMac::Show(bool show
)
1001 if ( !wxWindow::Show(show
) )
1006 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1007 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1008 // no need to generate events here, they will get them triggered by macos
1009 // actually they should be , but apparently they are not
1010 wxSize
size(m_width
, m_height
);
1011 wxSizeEvent
event(size
, m_windowId
);
1012 event
.SetEventObject(this);
1013 GetEventHandler()->ProcessEvent(event
);
1017 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1031 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1033 int former_x
= m_x
;
1034 int former_y
= m_y
;
1035 int former_w
= m_width
;
1036 int former_h
= m_height
;
1038 int actualWidth
= width
;
1039 int actualHeight
= height
;
1043 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1044 actualWidth
= m_minWidth
;
1045 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1046 actualHeight
= m_minHeight
;
1047 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1048 actualWidth
= m_maxWidth
;
1049 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1050 actualHeight
= m_maxHeight
;
1052 bool doMove
= false ;
1053 bool doResize
= false ;
1055 if ( actualX
!= former_x
|| actualY
!= former_y
)
1059 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1064 if ( doMove
|| doResize
)
1068 m_width
= actualWidth
;
1069 m_height
= actualHeight
;
1072 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1075 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1077 // the OS takes care of invalidating and erasing the new area so we only have to
1078 // take care of refreshing for full repaints
1080 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
1084 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1086 wxFrame
* frame
= (wxFrame
*) this ;
1087 frame
->PositionStatusBar();
1088 frame
->PositionToolBar();
1091 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1093 MacRepositionScrollBars() ;
1096 wxPoint
point(m_x
, m_y
);
1097 wxMoveEvent
event(point
, m_windowId
);
1098 event
.SetEventObject(this);
1099 GetEventHandler()->ProcessEvent(event
) ;
1103 MacRepositionScrollBars() ;
1104 wxSize
size(m_width
, m_height
);
1105 wxSizeEvent
event(size
, m_windowId
);
1106 event
.SetEventObject(this);
1107 GetEventHandler()->ProcessEvent(event
);
1114 * Invalidation Mechanism
1116 * The update mechanism reflects exactely the windows mechanism
1117 * the rect gets added to the window invalidate region, if the eraseBackground flag
1118 * has been true for any part of the update rgn the background is erased in the entire region
1119 * not just in the specified rect.
1121 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1122 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1123 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1124 * will get the eraseBackground event first
1127 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1129 GrafPtr formerPort
;
1130 GetPort( &formerPort
) ;
1131 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1133 m_macNeedsErasing
|= eraseBackground
;
1135 // if we already know that we will have to erase, there's no need to track the rest
1136 if ( !m_macNeedsErasing
)
1138 // we end only here if eraseBackground is false
1139 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1140 // we will have to erase anyway
1142 RgnHandle updateRgn
= NewRgn();
1143 RgnHandle diffRgn
= NewRgn() ;
1144 if ( updateRgn
&& diffRgn
)
1146 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1148 LocalToGlobal( &pt
) ;
1149 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1150 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1151 if ( !EmptyRgn( diffRgn
) )
1153 m_macNeedsErasing
= true ;
1157 DisposeRgn( updateRgn
);
1159 DisposeRgn( diffRgn
);
1161 if ( !m_macNeedsErasing
)
1163 RgnHandle rectRgn
= NewRgn() ;
1164 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1165 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1166 DisposeRgn( rectRgn
) ;
1169 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1170 // turn this on to debug the refreshing cycle
1171 #if wxMAC_DEBUG_REDRAW
1174 SetPort( formerPort
) ;
1178 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1180 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1181 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1184 // The empty region signifies that the shape should be removed from the
1186 if ( region
.IsEmpty() )
1188 wxSize sz
= GetClientSize();
1189 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1190 return SetShape(rgn
);
1193 // Make a copy of the region
1194 RgnHandle shapeRegion
= NewRgn();
1195 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1197 // Dispose of any shape region we may already have
1198 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1202 // Save the region so we can use it later
1203 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1205 // Tell the window manager that the window has changed shape
1206 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1213 // ---------------------------------------------------------------------------
1214 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1215 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1216 // ---------------------------------------------------------------------------
1220 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1222 GetWindowPortBounds(window
, inRect
);
1223 Point pt
= {inRect
->left
, inRect
->top
};
1224 SetPort((GrafPtr
) GetWindowPort(window
));
1227 inRect
->left
= pt
.h
;
1228 inRect
->bottom
+= pt
.v
;
1229 inRect
->right
+= pt
.h
;
1233 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1235 /*------------------------------------------------------
1236 Define which options your custom window supports.
1237 --------------------------------------------------------*/
1238 //just enable everything for our demo
1239 *(OptionBits
*)param
=//kWindowCanGrow|
1241 //kWindowCanCollapse|
1242 //kWindowCanGetWindowRegion|
1243 //kWindowHasTitleBar|
1244 //kWindowSupportsDragHilite|
1245 kWindowCanDrawInCurrentPort
|
1246 //kWindowCanMeasureTitle|
1247 kWindowWantsDisposeAtProcessDeath
|
1248 kWindowSupportsSetGrowImageRegion
|
1249 kWindowDefSupportsColorGrafPort
;
1253 // The content region is left as a rectangle matching the window size, this is
1254 // so the origin in the paint event, and etc. still matches what the
1255 // programmer expects.
1256 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1259 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1262 wxRect r
= win
->GetRect();
1263 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1267 // The structure region is set to the shape given to the SetShape method.
1268 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1270 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1276 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1277 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1278 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1279 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1285 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1287 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1289 switch(rgnRec
->regionCode
)
1291 case kWindowStructureRgn
:
1292 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1294 case kWindowContentRgn
:
1295 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1298 SetEmptyRgn(rgnRec
->winRgn
);
1305 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1307 /*------------------------------------------------------
1308 Determine the region of the window which was hit
1309 --------------------------------------------------------*/
1311 static RgnHandle tempRgn
=nil
;
1316 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1318 //Mac OS 8.5 or later
1319 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1320 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1323 return wNoHit
;//no significant area was hit.
1327 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1331 case kWindowMsgHitTest
:
1332 return wxShapedMacWindowHitTest(window
,param
);
1334 case kWindowMsgGetFeatures
:
1335 return wxShapedMacWindowGetFeatures(window
,param
);
1337 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1338 case kWindowMsgGetRegion
:
1339 return wxShapedMacWindowGetRegion(window
,param
);
1346 // ---------------------------------------------------------------------------