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 wxTheApp
->s_lastModifiers
= modifiers
;
237 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
239 OSStatus result
= eventNotHandledErr
;
241 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
243 UInt32 modifiers
= 0;
244 EventMouseButton button
= 0 ;
247 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
248 sizeof( Point
), NULL
, &point
);
249 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
250 sizeof( UInt32
), NULL
, &modifiers
);
251 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
252 sizeof( EventMouseButton
), NULL
, &button
);
253 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
254 sizeof( UInt32
), NULL
, &click
);
256 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
257 modifiers
+= btnState
;
260 short windowPart
= ::FindWindow(point
, &window
);
262 if ( IsWindowActive(window
) && windowPart
== inContent
)
264 switch ( GetEventKind( event
) )
266 case kEventMouseDown
:
267 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
271 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
274 case kEventMouseMoved
:
275 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
278 case kEventMouseDragged
:
279 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
291 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
293 OSStatus result
= eventNotHandledErr
;
294 OSStatus err
= noErr
;
297 WindowRef windowRef
;
298 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
300 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
301 sizeof( WindowRef
), NULL
, &windowRef
);
303 switch( GetEventKind( event
) )
305 case kEventWindowUpdate
:
306 if ( !wxPendingDelete
.Member(toplevelWindow
) )
307 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
310 case kEventWindowActivated
:
311 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
314 case kEventWindowDeactivated
:
315 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
318 case kEventWindowClose
:
319 toplevelWindow
->Close() ;
322 case kEventWindowBoundsChanged
:
323 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
324 NULL
, sizeof( UInt32
), NULL
, &attributes
);
327 Rect newContentRect
;
329 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
330 sizeof( newContentRect
), NULL
, &newContentRect
);
332 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
333 newContentRect
.right
- newContentRect
.left
,
334 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
339 case kEventWindowBoundsChanging
:
340 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
341 NULL
, sizeof( UInt32
), NULL
, &attributes
);
344 Rect newContentRect
;
346 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
347 sizeof( newContentRect
), NULL
, &newContentRect
);
349 wxRect
contentRect(newContentRect
.left
, newContentRect
.top
,
350 newContentRect
.right
- newContentRect
.left
,
351 newContentRect
.bottom
- newContentRect
.top
) ;
353 bool handled
= false ;
354 if ((attributes
& kWindowBoundsChangeSizeChanged
) != 0)
356 wxSizeEvent
event(contentRect
, toplevelWindow
->GetId());
357 event
.SetEventObject(toplevelWindow
);
358 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
359 contentRect
= event
.GetRect() ;
361 else if ( attributes
& kWindowBoundsChangeOriginChanged
!= 0)
363 wxMoveEvent
event(contentRect
, toplevelWindow
->GetId());
364 event
.SetEventObject(toplevelWindow
);
365 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
366 contentRect
= event
.GetRect() ;
370 SetRect( &newContentRect
, contentRect
.GetLeft() , contentRect
.GetTop() , contentRect
.GetRight() , contentRect
.GetBottom() ) ;
371 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( newContentRect
), &newContentRect
);
382 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
384 OSStatus result
= eventNotHandledErr
;
386 switch ( GetEventClass( event
) )
388 case kEventClassKeyboard
:
389 result
= KeyboardEventHandler( handler
, event
, data
) ;
391 case kEventClassTextInput
:
392 result
= TextInputEventHandler( handler
, event
, data
) ;
394 case kEventClassWindow
:
395 result
= WindowEventHandler( handler
, event
, data
) ;
397 case kEventClassMouse
:
398 result
= MouseEventHandler( handler
, event
, data
) ;
406 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
410 // ---------------------------------------------------------------------------
411 // wxWindowMac utility functions
412 // ---------------------------------------------------------------------------
414 // Find an item given the Macintosh Window Reference
416 wxList
*wxWinMacWindowList
= NULL
;
417 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
419 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
422 return (wxTopLevelWindowMac
*)node
->GetData();
425 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
427 // adding NULL WindowRef is (first) surely a result of an error and
428 // (secondly) breaks menu command processing
429 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
431 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
432 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
435 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
437 wxWinMacWindowList
->DeleteObject(win
);
441 // ----------------------------------------------------------------------------
442 // wxTopLevelWindowMac creation
443 // ----------------------------------------------------------------------------
445 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
446 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
448 void wxTopLevelWindowMac::Init()
451 m_maximizeOnShow
= FALSE
;
452 m_macNoEraseUpdateRgn
= NewRgn() ;
453 m_macNeedsErasing
= false ;
456 m_macEventHandler
= NULL
;
460 class wxMacDeferredWindowDeleter
: public wxObject
463 wxMacDeferredWindowDeleter( WindowRef windowRef
)
465 m_macWindow
= windowRef
;
467 virtual ~wxMacDeferredWindowDeleter()
469 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
472 WindowRef m_macWindow
;
475 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
477 const wxString
& title
,
481 const wxString
& name
)
486 m_windowStyle
= style
;
490 m_windowId
= id
== -1 ? NewControlId() : id
;
492 wxTopLevelWindows
.Append(this);
495 parent
->AddChild(this);
500 wxTopLevelWindowMac::~wxTopLevelWindowMac()
504 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
505 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
509 if ( m_macEventHandler
)
511 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
512 m_macEventHandler
= NULL
;
516 wxRemoveMacWindowAssociation( this ) ;
518 if ( wxModelessWindows
.Find(this) )
519 wxModelessWindows
.DeleteObject(this);
521 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
525 // ----------------------------------------------------------------------------
526 // wxTopLevelWindowMac maximize/minimize
527 // ----------------------------------------------------------------------------
529 void wxTopLevelWindowMac::Maximize(bool maximize
)
531 // not available on mac
534 bool wxTopLevelWindowMac::IsMaximized() const
539 void wxTopLevelWindowMac::Iconize(bool iconize
)
541 // not available on mac
544 bool wxTopLevelWindowMac::IsIconized() const
546 // mac dialogs cannot be iconized
550 void wxTopLevelWindowMac::Restore()
552 // not available on mac
555 // ----------------------------------------------------------------------------
556 // wxTopLevelWindowMac misc
557 // ----------------------------------------------------------------------------
559 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
562 wxTopLevelWindowBase::SetIcon(icon
);
565 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
569 const wxString
& name
)
571 OSStatus err
= noErr
;
573 m_windowStyle
= style
;
594 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
596 // translate the window attributes in the appropriate window class and attributes
598 WindowClass wclass
= 0;
599 WindowAttributes attr
= kWindowNoAttributes
;
601 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
604 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
605 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
606 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
609 wclass
= kFloatingWindowClass
;
610 if ( HasFlag(wxTINY_CAPTION_VERT
) )
612 attr
|= kWindowSideTitlebarAttribute
;
618 wclass
= kPlainWindowClass
;
620 wclass
= kFloatingWindowClass
;
624 else if ( HasFlag( wxCAPTION
) )
626 wclass
= kDocumentWindowClass
;
630 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
631 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
633 wclass
= kDocumentWindowClass
;
638 wclass
= kPlainWindowClass
;
640 wclass
= kModalWindowClass
;
645 if ( HasFlag( wxMINIMIZE_BOX
) )
647 attr
|= kWindowCollapseBoxAttribute
;
649 if ( HasFlag( wxMAXIMIZE_BOX
) )
651 attr
|= kWindowFullZoomAttribute
;
653 if ( HasFlag( wxRESIZE_BORDER
) )
655 attr
|= kWindowResizableAttribute
;
657 if ( HasFlag( wxCLOSE_BOX
) )
659 attr
|= kWindowCloseBoxAttribute
;
663 #if 0 // having problems right now with that
664 if (HasFlag(wxSTAY_ON_TOP
))
665 wclass
= kUtilityWindowClass
;
670 if ( HasFlag(wxFRAME_SHAPED
) )
672 WindowDefSpec customWindowDefSpec
;
673 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
674 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
676 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
677 attr
, &theBoundsRect
,
678 (WindowRef
*) &m_macWindow
);
683 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
686 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
687 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
688 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
689 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
691 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
692 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
693 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
699 if ( HasFlag(wxFRAME_SHAPED
) )
701 // default shape matches the window size
702 wxRegion
rgn(0, 0, m_width
, m_height
);
708 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
710 ((Point
*)localOrigin
)->h
= 0;
711 ((Point
*)localOrigin
)->v
= 0;
712 ((Rect
*)clipRect
)->left
= 0;
713 ((Rect
*)clipRect
)->top
= 0;
714 ((Rect
*)clipRect
)->right
= m_width
;
715 ((Rect
*)clipRect
)->bottom
= m_height
;
716 *window
= m_macWindow
;
720 void wxTopLevelWindowMac::ClearBackground()
722 wxWindow::ClearBackground() ;
725 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
727 return m_macRootControl
;
731 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
733 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
735 RgnHandle visRgn
= NewRgn() ;
736 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
737 BeginUpdate( (WindowRef
)m_macWindow
) ;
739 RgnHandle updateRgn
= NewRgn();
740 RgnHandle diffRgn
= NewRgn() ;
742 if ( updateRgn
&& diffRgn
)
745 // macos internal control redraws clean up areas we'd like to redraw ourselves
746 // therefore we pick the boundary rect and make sure we can redraw it
747 // this has to be intersected by the visRgn in order to avoid drawing over its own
749 RgnHandle trueUpdateRgn
= NewRgn() ;
750 Rect trueUpdateRgnBoundary
;
751 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
752 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
753 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
754 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
756 DisposeRgn( trueUpdateRgn
) ;
757 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
759 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
761 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
762 if ( !EmptyRgn( updateRgn
) )
764 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
768 DisposeRgn( updateRgn
);
770 DisposeRgn( diffRgn
);
772 DisposeRgn( visRgn
) ;
774 EndUpdate( (WindowRef
)m_macWindow
) ;
775 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
776 m_macNeedsErasing
= false ;
780 // Raise the window to the top of the Z order
781 void wxTopLevelWindowMac::Raise()
783 ::SelectWindow( (WindowRef
)m_macWindow
) ;
786 // Lower the window to the bottom of the Z order
787 void wxTopLevelWindowMac::Lower()
789 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
792 void wxTopLevelWindowMac::MacFireMouseEvent(
793 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
795 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
796 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
797 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
799 event
.m_leftDown
= isDown
&& !controlDown
;
801 event
.m_middleDown
= FALSE
;
802 event
.m_rightDown
= isDown
&& controlDown
;
804 if ( kind
== mouseDown
)
807 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
809 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
811 else if ( kind
== mouseUp
)
814 event
.SetEventType(wxEVT_RIGHT_UP
) ;
816 event
.SetEventType(wxEVT_LEFT_UP
) ;
820 event
.SetEventType(wxEVT_MOTION
) ;
823 event
.m_shiftDown
= modifiers
& shiftKey
;
824 event
.m_controlDown
= modifiers
& controlKey
;
825 event
.m_altDown
= modifiers
& optionKey
;
826 event
.m_metaDown
= modifiers
& cmdKey
;
834 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
835 ::GlobalToLocal( &localwhere
) ;
838 if ( kind
== mouseDown
)
840 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
842 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
844 // This is not right if the second mouse down
845 // event occured in a differen window. We
846 // correct this in MacDispatchMouseEvent.
848 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
850 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
856 gs_lastWhen
= timestamp
;
858 gs_lastWhere
= localwhere
;
861 event
.m_x
= localwhere
.h
;
862 event
.m_y
= localwhere
.v
;
866 event
.m_timeStamp
= timestamp
;
867 event
.SetEventObject(this);
868 if ( wxTheApp
->s_captureWindow
)
872 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
875 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
876 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
878 if ( kind
== mouseUp
)
880 wxTheApp
->s_captureWindow
= NULL
;
883 m_cursor
.MacInstall() ;
889 MacDispatchMouseEvent( event
) ;
895 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
897 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
898 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
901 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
907 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
908 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
914 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
920 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
921 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
929 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
931 if(s_macDeactivateWindow
)
933 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
934 s_macDeactivateWindow
->MacActivate(timestamp
, false);
938 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
940 wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
941 if(s_macDeactivateWindow
==this)
942 s_macDeactivateWindow
=NULL
;
943 MacDelayedDeactivation(timestamp
);
944 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
945 event
.m_timeStamp
= timestamp
;
946 event
.SetEventObject(this);
948 GetEventHandler()->ProcessEvent(event
);
950 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
952 // Early versions of MacOS X don't refresh backgrounds properly,
953 // so refresh the whole window on activation and deactivation.
954 long osVersion
= UMAGetSystemVersion();
955 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
961 // for the moment we have to resolve some redrawing issues like this
962 // the OS is stealing some redrawing areas as soon as it draws a control
969 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
975 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
977 wxWindow::SetTitle( title
) ;
978 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
981 bool wxTopLevelWindowMac::Show(bool show
)
983 if ( !wxWindow::Show(show
) )
988 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
989 ::SelectWindow( (WindowRef
)m_macWindow
) ;
990 // no need to generate events here, they will get them triggered by macos
991 // actually they should be , but apparently they are not
992 wxSize
size(m_width
, m_height
);
993 wxSizeEvent
event(size
, m_windowId
);
994 event
.SetEventObject(this);
995 GetEventHandler()->ProcessEvent(event
);
999 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1013 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1015 int former_x
= m_x
;
1016 int former_y
= m_y
;
1017 int former_w
= m_width
;
1018 int former_h
= m_height
;
1020 int actualWidth
= width
;
1021 int actualHeight
= height
;
1025 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1026 actualWidth
= m_minWidth
;
1027 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1028 actualHeight
= m_minHeight
;
1029 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1030 actualWidth
= m_maxWidth
;
1031 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1032 actualHeight
= m_maxHeight
;
1034 bool doMove
= false ;
1035 bool doResize
= false ;
1037 if ( actualX
!= former_x
|| actualY
!= former_y
)
1041 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1046 if ( doMove
|| doResize
)
1050 m_width
= actualWidth
;
1051 m_height
= actualHeight
;
1054 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1057 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1059 // the OS takes care of invalidating and erasing the new area so we only have to
1060 // take care of refreshing for full repaints
1062 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
1066 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1068 wxFrame
* frame
= (wxFrame
*) this ;
1069 frame
->PositionStatusBar();
1070 frame
->PositionToolBar();
1073 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1075 MacRepositionScrollBars() ;
1078 wxPoint
point(m_x
, m_y
);
1079 wxMoveEvent
event(point
, m_windowId
);
1080 event
.SetEventObject(this);
1081 GetEventHandler()->ProcessEvent(event
) ;
1085 MacRepositionScrollBars() ;
1086 wxSize
size(m_width
, m_height
);
1087 wxSizeEvent
event(size
, m_windowId
);
1088 event
.SetEventObject(this);
1089 GetEventHandler()->ProcessEvent(event
);
1096 * Invalidation Mechanism
1098 * The update mechanism reflects exactely the windows mechanism
1099 * the rect gets added to the window invalidate region, if the eraseBackground flag
1100 * has been true for any part of the update rgn the background is erased in the entire region
1101 * not just in the specified rect.
1103 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1104 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1105 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1106 * will get the eraseBackground event first
1109 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1111 GrafPtr formerPort
;
1112 GetPort( &formerPort
) ;
1113 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1115 m_macNeedsErasing
|= eraseBackground
;
1117 // if we already know that we will have to erase, there's no need to track the rest
1118 if ( !m_macNeedsErasing
)
1120 // we end only here if eraseBackground is false
1121 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1122 // we will have to erase anyway
1124 RgnHandle updateRgn
= NewRgn();
1125 RgnHandle diffRgn
= NewRgn() ;
1126 if ( updateRgn
&& diffRgn
)
1128 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1130 LocalToGlobal( &pt
) ;
1131 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1132 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1133 if ( !EmptyRgn( diffRgn
) )
1135 m_macNeedsErasing
= true ;
1139 DisposeRgn( updateRgn
);
1141 DisposeRgn( diffRgn
);
1143 if ( !m_macNeedsErasing
)
1145 RgnHandle rectRgn
= NewRgn() ;
1146 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1147 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1148 DisposeRgn( rectRgn
) ;
1151 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1152 // turn this on to debug the refreshing cycle
1153 #if wxMAC_DEBUG_REDRAW
1156 SetPort( formerPort
) ;
1160 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1162 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1163 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1166 // The empty region signifies that the shape should be removed from the
1168 if ( region
.IsEmpty() )
1170 wxSize sz
= GetClientSize();
1171 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1172 return SetShape(rgn
);
1175 // Make a copy of the region
1176 RgnHandle shapeRegion
= NewRgn();
1177 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1179 // Dispose of any shape region we may already have
1180 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1184 // Save the region so we can use it later
1185 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1187 // Tell the window manager that the window has changed shape
1188 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1195 // ---------------------------------------------------------------------------
1196 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1197 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1198 // ---------------------------------------------------------------------------
1202 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1204 GetWindowPortBounds(window
, inRect
);
1205 Point pt
= {inRect
->left
, inRect
->top
};
1206 SetPort((GrafPtr
) GetWindowPort(window
));
1209 inRect
->left
= pt
.h
;
1210 inRect
->bottom
+= pt
.v
;
1211 inRect
->right
+= pt
.h
;
1215 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1217 /*------------------------------------------------------
1218 Define which options your custom window supports.
1219 --------------------------------------------------------*/
1220 //just enable everything for our demo
1221 *(OptionBits
*)param
=//kWindowCanGrow|
1223 //kWindowCanCollapse|
1224 //kWindowCanGetWindowRegion|
1225 //kWindowHasTitleBar|
1226 //kWindowSupportsDragHilite|
1227 kWindowCanDrawInCurrentPort
|
1228 //kWindowCanMeasureTitle|
1229 kWindowWantsDisposeAtProcessDeath
|
1230 kWindowSupportsSetGrowImageRegion
|
1231 kWindowDefSupportsColorGrafPort
;
1235 // The content region is left as a rectangle matching the window size, this is
1236 // so the origin in the paint event, and etc. still matches what the
1237 // programmer expects.
1238 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1241 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1244 wxRect r
= win
->GetRect();
1245 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1249 // The structure region is set to the shape given to the SetShape method.
1250 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1252 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1258 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1259 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1260 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1261 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1267 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1269 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1271 switch(rgnRec
->regionCode
)
1273 case kWindowStructureRgn
:
1274 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1276 case kWindowContentRgn
:
1277 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1280 SetEmptyRgn(rgnRec
->winRgn
);
1287 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1289 /*------------------------------------------------------
1290 Determine the region of the window which was hit
1291 --------------------------------------------------------*/
1293 static RgnHandle tempRgn
=nil
;
1298 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1300 //Mac OS 8.5 or later
1301 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1302 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1305 return wNoHit
;//no significant area was hit.
1309 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1313 case kWindowMsgHitTest
:
1314 return wxShapedMacWindowHitTest(window
,param
);
1316 case kWindowMsgGetFeatures
:
1317 return wxShapedMacWindowGetFeatures(window
,param
);
1319 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1320 case kWindowMsgGetRegion
:
1321 return wxShapedMacWindowGetRegion(window
,param
);
1328 // ---------------------------------------------------------------------------