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
:
180 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
181 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
182 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
183 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
184 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
188 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
191 case kEventRawKeyUp
:
192 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
193 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
198 case kEventRawKeyModifiersChanged
:
200 wxKeyEvent
event(wxEVT_KEY_DOWN
);
202 event
.m_shiftDown
= modifiers
& shiftKey
;
203 event
.m_controlDown
= modifiers
& controlKey
;
204 event
.m_altDown
= modifiers
& optionKey
;
205 event
.m_metaDown
= modifiers
& cmdKey
;
209 event
.m_timeStamp
= when
;
210 wxWindow
* focus
= wxWindow::FindFocus() ;
211 event
.SetEventObject(focus
);
213 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
215 event
.m_keyCode
= WXK_CONTROL
;
216 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
217 focus
->GetEventHandler()->ProcessEvent( event
) ;
219 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
221 event
.m_keyCode
= WXK_SHIFT
;
222 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
223 focus
->GetEventHandler()->ProcessEvent( event
) ;
225 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
227 event
.m_keyCode
= WXK_ALT
;
228 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
229 focus
->GetEventHandler()->ProcessEvent( event
) ;
231 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
233 event
.m_keyCode
= WXK_COMMAND
;
234 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
235 focus
->GetEventHandler()->ProcessEvent( event
) ;
237 wxTheApp
->s_lastModifiers
= modifiers
;
245 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
247 OSStatus result
= eventNotHandledErr
;
249 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
251 UInt32 modifiers
= 0;
252 EventMouseButton button
= 0 ;
255 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
256 sizeof( Point
), NULL
, &point
);
257 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
258 sizeof( UInt32
), NULL
, &modifiers
);
259 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
260 sizeof( EventMouseButton
), NULL
, &button
);
261 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
262 sizeof( UInt32
), NULL
, &click
);
264 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
265 modifiers
+= btnState
;
268 short windowPart
= ::FindWindow(point
, &window
);
270 if ( IsWindowActive(window
) && windowPart
== inContent
)
272 switch ( GetEventKind( event
) )
274 case kEventMouseDown
:
275 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
279 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
282 case kEventMouseMoved
:
283 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
286 case kEventMouseDragged
:
287 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
299 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
301 OSStatus result
= eventNotHandledErr
;
302 OSStatus err
= noErr
;
305 WindowRef windowRef
;
306 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
308 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
309 sizeof( WindowRef
), NULL
, &windowRef
);
311 switch( GetEventKind( event
) )
313 case kEventWindowUpdate
:
314 if ( !wxPendingDelete
.Member(toplevelWindow
) )
315 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
318 case kEventWindowActivated
:
319 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
322 case kEventWindowDeactivated
:
323 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
326 case kEventWindowClose
:
327 toplevelWindow
->Close() ;
330 case kEventWindowBoundsChanged
:
331 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
332 NULL
, sizeof( UInt32
), NULL
, &attributes
);
335 Rect newContentRect
;
337 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
338 sizeof( newContentRect
), NULL
, &newContentRect
);
340 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
341 newContentRect
.right
- newContentRect
.left
,
342 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
347 case kEventWindowBoundsChanging
:
348 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
349 NULL
, sizeof( UInt32
), NULL
, &attributes
);
352 Rect newContentRect
;
354 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
355 sizeof( newContentRect
), NULL
, &newContentRect
);
357 wxRect
contentRect(newContentRect
.left
, newContentRect
.top
,
358 newContentRect
.right
- newContentRect
.left
,
359 newContentRect
.bottom
- newContentRect
.top
) ;
361 bool handled
= false ;
362 if ((attributes
& kWindowBoundsChangeSizeChanged
) != 0)
364 wxSizeEvent
event(contentRect
, toplevelWindow
->GetId());
365 event
.SetEventObject(toplevelWindow
);
366 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
367 contentRect
= event
.GetRect() ;
369 else if ( attributes
& kWindowBoundsChangeOriginChanged
!= 0)
371 wxMoveEvent
event(contentRect
, toplevelWindow
->GetId());
372 event
.SetEventObject(toplevelWindow
);
373 handled
= toplevelWindow
->GetEventHandler()->ProcessEvent(event
);
374 contentRect
= event
.GetRect() ;
378 SetRect( &newContentRect
, contentRect
.GetLeft() , contentRect
.GetTop() , contentRect
.GetRight() , contentRect
.GetBottom() ) ;
379 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( newContentRect
), &newContentRect
);
390 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
392 OSStatus result
= eventNotHandledErr
;
394 switch ( GetEventClass( event
) )
396 case kEventClassKeyboard
:
397 result
= KeyboardEventHandler( handler
, event
, data
) ;
399 case kEventClassTextInput
:
400 result
= TextInputEventHandler( handler
, event
, data
) ;
402 case kEventClassWindow
:
403 result
= WindowEventHandler( handler
, event
, data
) ;
405 case kEventClassMouse
:
406 result
= MouseEventHandler( handler
, event
, data
) ;
414 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
418 // ---------------------------------------------------------------------------
419 // wxWindowMac utility functions
420 // ---------------------------------------------------------------------------
422 // Find an item given the Macintosh Window Reference
424 wxList
*wxWinMacWindowList
= NULL
;
425 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
427 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
430 return (wxTopLevelWindowMac
*)node
->GetData();
433 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
435 // adding NULL WindowRef is (first) surely a result of an error and
436 // (secondly) breaks menu command processing
437 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
439 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
440 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
443 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
445 wxWinMacWindowList
->DeleteObject(win
);
449 // ----------------------------------------------------------------------------
450 // wxTopLevelWindowMac creation
451 // ----------------------------------------------------------------------------
453 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
454 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
456 void wxTopLevelWindowMac::Init()
459 m_maximizeOnShow
= FALSE
;
460 m_macNoEraseUpdateRgn
= NewRgn() ;
461 m_macNeedsErasing
= false ;
464 m_macEventHandler
= NULL
;
468 class wxMacDeferredWindowDeleter
: public wxObject
471 wxMacDeferredWindowDeleter( WindowRef windowRef
)
473 m_macWindow
= windowRef
;
475 virtual ~wxMacDeferredWindowDeleter()
477 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
480 WindowRef m_macWindow
;
483 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
485 const wxString
& title
,
489 const wxString
& name
)
494 m_windowStyle
= style
;
498 m_windowId
= id
== -1 ? NewControlId() : id
;
500 wxTopLevelWindows
.Append(this);
503 parent
->AddChild(this);
508 wxTopLevelWindowMac::~wxTopLevelWindowMac()
512 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
513 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
517 if ( m_macEventHandler
)
519 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
520 m_macEventHandler
= NULL
;
524 wxRemoveMacWindowAssociation( this ) ;
526 if ( wxModelessWindows
.Find(this) )
527 wxModelessWindows
.DeleteObject(this);
529 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
533 // ----------------------------------------------------------------------------
534 // wxTopLevelWindowMac maximize/minimize
535 // ----------------------------------------------------------------------------
537 void wxTopLevelWindowMac::Maximize(bool maximize
)
539 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
544 Point pt
= { 0, 0 } ;
545 SetPortWindowPort((WindowRef
)m_macWindow
) ;
546 LocalToGlobal( &pt
) ;
549 GetWindowPortBounds((WindowRef
)m_macWindow
, &tempRect
) ;
550 SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
551 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
554 bool wxTopLevelWindowMac::IsMaximized() const
556 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
559 void wxTopLevelWindowMac::Iconize(bool iconize
)
561 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
562 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
565 bool wxTopLevelWindowMac::IsIconized() const
567 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
570 void wxTopLevelWindowMac::Restore()
572 // not available on mac
575 // ----------------------------------------------------------------------------
576 // wxTopLevelWindowMac misc
577 // ----------------------------------------------------------------------------
579 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
582 wxTopLevelWindowBase::SetIcon(icon
);
585 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
589 const wxString
& name
)
591 OSStatus err
= noErr
;
593 m_windowStyle
= style
;
614 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
616 // translate the window attributes in the appropriate window class and attributes
618 WindowClass wclass
= 0;
619 WindowAttributes attr
= kWindowNoAttributes
;
621 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
624 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
625 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
626 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
629 wclass
= kFloatingWindowClass
;
630 if ( HasFlag(wxTINY_CAPTION_VERT
) )
632 attr
|= kWindowSideTitlebarAttribute
;
638 wclass
= kPlainWindowClass
;
640 wclass
= kFloatingWindowClass
;
644 else if ( HasFlag( wxCAPTION
) )
646 wclass
= kDocumentWindowClass
;
650 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
651 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
653 wclass
= kDocumentWindowClass
;
658 wclass
= kPlainWindowClass
;
660 wclass
= kModalWindowClass
;
665 if ( HasFlag( wxMINIMIZE_BOX
) )
667 attr
|= kWindowCollapseBoxAttribute
;
669 if ( HasFlag( wxMAXIMIZE_BOX
) )
671 attr
|= kWindowFullZoomAttribute
;
673 if ( HasFlag( wxRESIZE_BORDER
) )
675 attr
|= kWindowResizableAttribute
;
677 if ( HasFlag( wxCLOSE_BOX
) )
679 attr
|= kWindowCloseBoxAttribute
;
683 #if 0 // having problems right now with that
684 if (HasFlag(wxSTAY_ON_TOP
))
685 wclass
= kUtilityWindowClass
;
690 if ( HasFlag(wxFRAME_SHAPED
) )
692 WindowDefSpec customWindowDefSpec
;
693 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
694 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
696 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
697 attr
, &theBoundsRect
,
698 (WindowRef
*) &m_macWindow
);
703 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
706 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
707 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
708 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
709 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
711 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
712 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
713 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
719 if ( HasFlag(wxFRAME_SHAPED
) )
721 // default shape matches the window size
722 wxRegion
rgn(0, 0, m_width
, m_height
);
728 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
730 ((Point
*)localOrigin
)->h
= 0;
731 ((Point
*)localOrigin
)->v
= 0;
732 ((Rect
*)clipRect
)->left
= 0;
733 ((Rect
*)clipRect
)->top
= 0;
734 ((Rect
*)clipRect
)->right
= m_width
;
735 ((Rect
*)clipRect
)->bottom
= m_height
;
736 *window
= m_macWindow
;
740 void wxTopLevelWindowMac::ClearBackground()
742 wxWindow::ClearBackground() ;
745 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
747 return m_macRootControl
;
751 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
753 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
755 RgnHandle visRgn
= NewRgn() ;
756 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
757 BeginUpdate( (WindowRef
)m_macWindow
) ;
759 RgnHandle updateRgn
= NewRgn();
760 RgnHandle diffRgn
= NewRgn() ;
762 if ( updateRgn
&& diffRgn
)
765 // macos internal control redraws clean up areas we'd like to redraw ourselves
766 // therefore we pick the boundary rect and make sure we can redraw it
767 // this has to be intersected by the visRgn in order to avoid drawing over its own
769 RgnHandle trueUpdateRgn
= NewRgn() ;
770 Rect trueUpdateRgnBoundary
;
771 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
772 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
773 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
774 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
776 DisposeRgn( trueUpdateRgn
) ;
777 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
779 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
781 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
782 if ( !EmptyRgn( updateRgn
) )
784 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
788 DisposeRgn( updateRgn
);
790 DisposeRgn( diffRgn
);
792 DisposeRgn( visRgn
) ;
794 EndUpdate( (WindowRef
)m_macWindow
) ;
795 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
796 m_macNeedsErasing
= false ;
800 // Raise the window to the top of the Z order
801 void wxTopLevelWindowMac::Raise()
803 ::SelectWindow( (WindowRef
)m_macWindow
) ;
806 // Lower the window to the bottom of the Z order
807 void wxTopLevelWindowMac::Lower()
809 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
812 void wxTopLevelWindowMac::MacFireMouseEvent(
813 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
815 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
816 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
817 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
819 event
.m_leftDown
= isDown
&& !controlDown
;
821 event
.m_middleDown
= FALSE
;
822 event
.m_rightDown
= isDown
&& controlDown
;
824 if ( kind
== mouseDown
)
827 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
829 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
831 else if ( kind
== mouseUp
)
834 event
.SetEventType(wxEVT_RIGHT_UP
) ;
836 event
.SetEventType(wxEVT_LEFT_UP
) ;
840 event
.SetEventType(wxEVT_MOTION
) ;
843 event
.m_shiftDown
= modifiers
& shiftKey
;
844 event
.m_controlDown
= modifiers
& controlKey
;
845 event
.m_altDown
= modifiers
& optionKey
;
846 event
.m_metaDown
= modifiers
& cmdKey
;
854 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
855 ::GlobalToLocal( &localwhere
) ;
858 if ( kind
== mouseDown
)
860 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
862 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
864 // This is not right if the second mouse down
865 // event occured in a differen window. We
866 // correct this in MacDispatchMouseEvent.
868 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
870 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
876 gs_lastWhen
= timestamp
;
878 gs_lastWhere
= localwhere
;
881 event
.m_x
= localwhere
.h
;
882 event
.m_y
= localwhere
.v
;
886 event
.m_timeStamp
= timestamp
;
887 event
.SetEventObject(this);
888 if ( wxTheApp
->s_captureWindow
)
892 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
895 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
896 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
898 if ( kind
== mouseUp
)
900 wxTheApp
->s_captureWindow
= NULL
;
903 m_cursor
.MacInstall() ;
909 MacDispatchMouseEvent( event
) ;
915 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
917 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
918 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
921 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
927 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
928 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
934 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
940 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
941 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
949 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
951 if(s_macDeactivateWindow
)
953 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
954 s_macDeactivateWindow
->MacActivate(timestamp
, false);
958 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
960 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
962 if(s_macDeactivateWindow
==this)
963 s_macDeactivateWindow
=NULL
;
964 MacDelayedDeactivation(timestamp
);
965 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
966 event
.m_timeStamp
= timestamp
;
967 event
.SetEventObject(this);
969 GetEventHandler()->ProcessEvent(event
);
971 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
973 // Early versions of MacOS X don't refresh backgrounds properly,
974 // so refresh the whole window on activation and deactivation.
975 long osVersion
= UMAGetSystemVersion();
976 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
982 // for the moment we have to resolve some redrawing issues like this
983 // the OS is stealing some redrawing areas as soon as it draws a control
990 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
996 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
998 wxWindow::SetTitle( title
) ;
999 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
1002 bool wxTopLevelWindowMac::Show(bool show
)
1004 if ( !wxWindow::Show(show
) )
1009 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1010 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1011 // no need to generate events here, they will get them triggered by macos
1012 // actually they should be , but apparently they are not
1013 wxSize
size(m_width
, m_height
);
1014 wxSizeEvent
event(size
, m_windowId
);
1015 event
.SetEventObject(this);
1016 GetEventHandler()->ProcessEvent(event
);
1020 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1034 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1036 int former_x
= m_x
;
1037 int former_y
= m_y
;
1038 int former_w
= m_width
;
1039 int former_h
= m_height
;
1041 int actualWidth
= width
;
1042 int actualHeight
= height
;
1046 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1047 actualWidth
= m_minWidth
;
1048 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1049 actualHeight
= m_minHeight
;
1050 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1051 actualWidth
= m_maxWidth
;
1052 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1053 actualHeight
= m_maxHeight
;
1055 bool doMove
= false ;
1056 bool doResize
= false ;
1058 if ( actualX
!= former_x
|| actualY
!= former_y
)
1062 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1067 if ( doMove
|| doResize
)
1071 m_width
= actualWidth
;
1072 m_height
= actualHeight
;
1075 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1078 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1080 // the OS takes care of invalidating and erasing the new area so we only have to
1081 // take care of refreshing for full repaints
1083 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
1087 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1089 wxFrame
* frame
= (wxFrame
*) this ;
1090 frame
->PositionStatusBar();
1091 frame
->PositionToolBar();
1094 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1096 MacRepositionScrollBars() ;
1099 wxPoint
point(m_x
, m_y
);
1100 wxMoveEvent
event(point
, m_windowId
);
1101 event
.SetEventObject(this);
1102 GetEventHandler()->ProcessEvent(event
) ;
1106 MacRepositionScrollBars() ;
1107 wxSize
size(m_width
, m_height
);
1108 wxSizeEvent
event(size
, m_windowId
);
1109 event
.SetEventObject(this);
1110 GetEventHandler()->ProcessEvent(event
);
1117 * Invalidation Mechanism
1119 * The update mechanism reflects exactely the windows mechanism
1120 * the rect gets added to the window invalidate region, if the eraseBackground flag
1121 * has been true for any part of the update rgn the background is erased in the entire region
1122 * not just in the specified rect.
1124 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1125 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1126 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1127 * will get the eraseBackground event first
1130 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1132 GrafPtr formerPort
;
1133 GetPort( &formerPort
) ;
1134 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1136 m_macNeedsErasing
|= eraseBackground
;
1138 // if we already know that we will have to erase, there's no need to track the rest
1139 if ( !m_macNeedsErasing
)
1141 // we end only here if eraseBackground is false
1142 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1143 // we will have to erase anyway
1145 RgnHandle updateRgn
= NewRgn();
1146 RgnHandle diffRgn
= NewRgn() ;
1147 if ( updateRgn
&& diffRgn
)
1149 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1151 LocalToGlobal( &pt
) ;
1152 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1153 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1154 if ( !EmptyRgn( diffRgn
) )
1156 m_macNeedsErasing
= true ;
1160 DisposeRgn( updateRgn
);
1162 DisposeRgn( diffRgn
);
1164 if ( !m_macNeedsErasing
)
1166 RgnHandle rectRgn
= NewRgn() ;
1167 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1168 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1169 DisposeRgn( rectRgn
) ;
1172 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1173 // turn this on to debug the refreshing cycle
1174 #if wxMAC_DEBUG_REDRAW
1177 SetPort( formerPort
) ;
1181 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1183 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1184 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1187 // The empty region signifies that the shape should be removed from the
1189 if ( region
.IsEmpty() )
1191 wxSize sz
= GetClientSize();
1192 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1193 return SetShape(rgn
);
1196 // Make a copy of the region
1197 RgnHandle shapeRegion
= NewRgn();
1198 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1200 // Dispose of any shape region we may already have
1201 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1205 // Save the region so we can use it later
1206 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1208 // Tell the window manager that the window has changed shape
1209 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1216 // ---------------------------------------------------------------------------
1217 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1218 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1219 // ---------------------------------------------------------------------------
1223 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1225 GetWindowPortBounds(window
, inRect
);
1226 Point pt
= {inRect
->left
, inRect
->top
};
1227 SetPort((GrafPtr
) GetWindowPort(window
));
1230 inRect
->left
= pt
.h
;
1231 inRect
->bottom
+= pt
.v
;
1232 inRect
->right
+= pt
.h
;
1236 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1238 /*------------------------------------------------------
1239 Define which options your custom window supports.
1240 --------------------------------------------------------*/
1241 //just enable everything for our demo
1242 *(OptionBits
*)param
=//kWindowCanGrow|
1244 //kWindowCanCollapse|
1245 //kWindowCanGetWindowRegion|
1246 //kWindowHasTitleBar|
1247 //kWindowSupportsDragHilite|
1248 kWindowCanDrawInCurrentPort
|
1249 //kWindowCanMeasureTitle|
1250 kWindowWantsDisposeAtProcessDeath
|
1251 kWindowSupportsSetGrowImageRegion
|
1252 kWindowDefSupportsColorGrafPort
;
1256 // The content region is left as a rectangle matching the window size, this is
1257 // so the origin in the paint event, and etc. still matches what the
1258 // programmer expects.
1259 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1262 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1265 wxRect r
= win
->GetRect();
1266 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1270 // The structure region is set to the shape given to the SetShape method.
1271 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1273 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1279 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1280 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1281 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1282 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1288 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1290 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1292 switch(rgnRec
->regionCode
)
1294 case kWindowStructureRgn
:
1295 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1297 case kWindowContentRgn
:
1298 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1301 SetEmptyRgn(rgnRec
->winRgn
);
1308 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1310 /*------------------------------------------------------
1311 Determine the region of the window which was hit
1312 --------------------------------------------------------*/
1314 static RgnHandle tempRgn
=nil
;
1319 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1321 //Mac OS 8.5 or later
1322 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1323 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1326 return wNoHit
;//no significant area was hit.
1330 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1334 case kWindowMsgHitTest
:
1335 return wxShapedMacWindowHitTest(window
,param
);
1337 case kWindowMsgGetFeatures
:
1338 return wxShapedMacWindowGetFeatures(window
,param
);
1340 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1341 case kWindowMsgGetRegion
:
1342 return wxShapedMacWindowGetRegion(window
,param
);
1349 // ---------------------------------------------------------------------------