1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
35 #include "wx/string.h"
40 #include "wx/mac/uma.h"
41 #include "wx/mac/aga.h"
43 #include "wx/tooltip.h"
46 #include "ToolUtils.h"
49 #define wxMAC_DEBUG_REDRAW 0
50 #ifndef wxMAC_DEBUG_REDRAW
51 #define wxMAC_DEBUG_REDRAW 0
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // list of all frames and modeless dialogs
59 wxWindowList wxModelessWindows
;
61 // double click testing
62 static Point gs_lastWhere
;
63 static long gs_lastWhen
= 0;
67 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
70 // ============================================================================
71 // wxTopLevelWindowMac implementation
72 // ============================================================================
74 // ---------------------------------------------------------------------------
76 // ---------------------------------------------------------------------------
80 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
82 static const EventTypeSpec eventList
[] =
84 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
86 { kEventClassKeyboard
, kEventRawKeyDown
} ,
87 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
88 { kEventClassKeyboard
, kEventRawKeyUp
} ,
89 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
91 { kEventClassWindow
, kEventWindowUpdate
} ,
92 { kEventClassWindow
, kEventWindowActivated
} ,
93 { kEventClassWindow
, kEventWindowDeactivated
} ,
94 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
95 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
96 { kEventClassWindow
, kEventWindowClose
} ,
98 { kEventClassMouse
, kEventMouseDown
} ,
99 { kEventClassMouse
, kEventMouseUp
} ,
100 { kEventClassMouse
, kEventMouseMoved
} ,
101 { kEventClassMouse
, kEventMouseDragged
} ,
105 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
107 OSStatus result
= eventNotHandledErr
;
109 wxWindow
* focus
= wxWindow::FindFocus() ;
114 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
118 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
120 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
121 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
122 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
123 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
124 sizeof( Point
), NULL
, &point
);
126 UInt32 message
= (keyCode
<< 8) + charCode
;
128 switch ( GetEventKind( event
) )
130 case kEventTextInputUnicodeForKeyEvent
:
131 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
132 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
142 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
144 OSStatus result
= eventNotHandledErr
;
146 wxWindow
* focus
= wxWindow::FindFocus() ;
151 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
153 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
154 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
155 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
156 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
157 sizeof( Point
), NULL
, &point
);
159 UInt32 message
= (keyCode
<< 8) + charCode
;
160 switch( GetEventKind( event
) )
162 case kEventRawKeyRepeat
:
163 case kEventRawKeyDown
:
164 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
165 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
170 case kEventRawKeyUp
:
171 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
172 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
177 case kEventRawKeyModifiersChanged
:
179 wxKeyEvent
event(wxEVT_KEY_DOWN
);
181 event
.m_shiftDown
= modifiers
& shiftKey
;
182 event
.m_controlDown
= modifiers
& controlKey
;
183 event
.m_altDown
= modifiers
& optionKey
;
184 event
.m_metaDown
= modifiers
& cmdKey
;
188 event
.m_timeStamp
= when
;
189 wxWindow
* focus
= wxWindow::FindFocus() ;
190 event
.SetEventObject(focus
);
192 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
194 event
.m_keyCode
= WXK_CONTROL
;
195 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
196 focus
->GetEventHandler()->ProcessEvent( event
) ;
198 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
200 event
.m_keyCode
= WXK_SHIFT
;
201 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
202 focus
->GetEventHandler()->ProcessEvent( event
) ;
204 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
206 event
.m_keyCode
= WXK_ALT
;
207 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
208 focus
->GetEventHandler()->ProcessEvent( event
) ;
210 wxTheApp
->s_lastModifiers
= modifiers
;
218 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
220 OSStatus result
= eventNotHandledErr
;
222 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
224 UInt32 modifiers
= 0;
225 EventMouseButton button
= 0 ;
228 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
229 sizeof( Point
), NULL
, &point
);
230 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
231 sizeof( UInt32
), NULL
, &modifiers
);
232 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
233 sizeof( EventMouseButton
), NULL
, &button
);
234 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
235 sizeof( UInt32
), NULL
, &click
);
237 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
238 modifiers
+= btnState
;
241 short windowPart
= ::FindWindow(point
, &window
);
243 if ( IsWindowActive(window
) && windowPart
== inContent
)
245 switch ( GetEventKind( event
) )
247 case kEventMouseDown
:
248 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
252 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
255 case kEventMouseMoved
:
256 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
259 case kEventMouseDragged
:
260 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
272 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
274 OSStatus result
= eventNotHandledErr
;
275 OSStatus err
= noErr
;
278 WindowRef windowRef
;
279 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
281 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
282 sizeof( WindowRef
), NULL
, &windowRef
);
284 switch( GetEventKind( event
) )
286 case kEventWindowUpdate
:
287 if ( !wxPendingDelete
.Member(toplevelWindow
) )
288 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
291 case kEventWindowActivated
:
292 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
295 case kEventWindowDeactivated
:
296 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
299 case kEventWindowClose
:
300 toplevelWindow
->Close() ;
303 case kEventWindowBoundsChanged
:
304 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
305 NULL
, sizeof( UInt32
), NULL
, &attributes
);
308 Rect newContentRect
;
310 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
311 sizeof( newContentRect
), NULL
, &newContentRect
);
313 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
314 newContentRect
.right
- newContentRect
.left
,
315 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
326 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
328 OSStatus result
= eventNotHandledErr
;
330 switch ( GetEventClass( event
) )
332 case kEventClassKeyboard
:
333 result
= KeyboardEventHandler( handler
, event
, data
) ;
335 case kEventClassTextInput
:
336 result
= TextInputEventHandler( handler
, event
, data
) ;
338 case kEventClassWindow
:
339 result
= WindowEventHandler( handler
, event
, data
) ;
341 case kEventClassMouse
:
342 result
= MouseEventHandler( handler
, event
, data
) ;
350 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
352 // Patch 531199 defined a window event handler, as follows.
353 // TODO: merge the moving/sizing event handling with the event
356 static pascal OSStatus
357 WindowHandler( EventHandlerCallRef inHandler
, EventRef inEvent
, void* userData
)
360 SInt16 height
, width
;
362 OSStatus result
= eventNotHandledErr
;
364 GetEventParameter( inEvent
, kEventParamAttributes
, typeUInt32
, NULL
, sizeof( UInt32
), NULL
, &attributes
);
366 if ((attributes
& (kWindowBoundsChangeSizeChanged
| kWindowBoundsChangeOriginChanged
)) != 0)
368 // Extract the current bounds. This is the paramter you get to modify to
369 // alter the window position or size during a window resizing.
370 GetEventParameter( inEvent
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
, sizeof( bounds
), NULL
, &bounds
);
373 rect
.SetLeft(bounds
.left
);
374 rect
.SetTop(bounds
.top
);
375 rect
.SetRight(bounds
.right
);
376 rect
.SetBottom(bounds
.bottom
);
379 wxWindowMac
*pWindow
= (wxWindowMac
*)userData
;
380 if ((attributes
& kWindowBoundsChangeSizeChanged
) != 0) {
381 wxSizeEvent
event(rect
, pWindow
->GetId());
382 event
.SetEventObject(pWindow
);
383 rc
= pWindow
->GetEventHandler()->ProcessEvent(event
);
384 rect
= event
.GetRect();
387 wxMoveEvent
event(rect
, pWindow
->GetId());
388 event
.SetEventObject(pWindow
);
389 rc
= pWindow
->GetEventHandler()->ProcessEvent(event
);
390 rect
= event
.GetRect();
394 bounds
.left
= rect
.GetLeft();
395 bounds
.top
= rect
.GetTop();
396 bounds
.right
= rect
.GetRight();
397 bounds
.bottom
= rect
.GetBottom();
400 // Set the current bounds parameter to our adjusted bounds. Return
401 // noErr to indicate we handled this event.
402 SetEventParameter( inEvent
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( bounds
), &bounds
);
412 // ---------------------------------------------------------------------------
413 // wxWindowMac utility functions
414 // ---------------------------------------------------------------------------
416 // Find an item given the Macintosh Window Reference
418 wxList
*wxWinMacWindowList
= NULL
;
419 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
421 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
424 return (wxTopLevelWindowMac
*)node
->GetData();
427 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
429 // adding NULL WindowRef is (first) surely a result of an error and
430 // (secondly) breaks menu command processing
431 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
433 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
434 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
437 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
439 wxWinMacWindowList
->DeleteObject(win
);
443 // ----------------------------------------------------------------------------
444 // wxTopLevelWindowMac creation
445 // ----------------------------------------------------------------------------
447 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
449 void wxTopLevelWindowMac::Init()
452 m_maximizeOnShow
= FALSE
;
453 m_macNoEraseUpdateRgn
= NewRgn() ;
454 m_macNeedsErasing
= false ;
457 m_macEventHandler
= NULL
;
461 class wxMacDeferredWindowDeleter
: public wxObject
464 wxMacDeferredWindowDeleter( WindowRef windowRef
)
466 m_macWindow
= windowRef
;
468 virtual ~wxMacDeferredWindowDeleter()
470 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
473 WindowRef m_macWindow
;
476 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
478 const wxString
& title
,
482 const wxString
& name
)
487 m_windowStyle
= style
;
491 m_windowId
= id
== -1 ? NewControlId() : id
;
493 wxTopLevelWindows
.Append(this);
496 parent
->AddChild(this);
501 wxTopLevelWindowMac::~wxTopLevelWindowMac()
505 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
506 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
510 if ( m_macEventHandler
)
512 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
513 m_macEventHandler
= NULL
;
517 wxRemoveMacWindowAssociation( this ) ;
519 if ( wxModelessWindows
.Find(this) )
520 wxModelessWindows
.DeleteObject(this);
522 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
526 // ----------------------------------------------------------------------------
527 // wxTopLevelWindowMac maximize/minimize
528 // ----------------------------------------------------------------------------
530 void wxTopLevelWindowMac::Maximize(bool maximize
)
532 // not available on mac
535 bool wxTopLevelWindowMac::IsMaximized() const
540 void wxTopLevelWindowMac::Iconize(bool iconize
)
542 // not available on mac
545 bool wxTopLevelWindowMac::IsIconized() const
547 // mac dialogs cannot be iconized
551 void wxTopLevelWindowMac::Restore()
553 // not available on mac
556 // ----------------------------------------------------------------------------
557 // wxTopLevelWindowMac misc
558 // ----------------------------------------------------------------------------
560 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
563 wxTopLevelWindowBase::SetIcon(icon
);
566 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
570 const wxString
& name
)
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( wxSYSTEM_MENU
) )
633 wclass
= kDocumentWindowClass
;
638 wclass
= kPlainWindowClass
;
640 wclass
= kModalWindowClass
;
645 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
647 attr
|= kWindowFullZoomAttribute
;
648 attr
|= kWindowCollapseBoxAttribute
;
650 if ( HasFlag( wxRESIZE_BORDER
) )
652 attr
|= kWindowResizableAttribute
;
654 if ( HasFlag( wxSYSTEM_MENU
) )
656 attr
|= kWindowCloseBoxAttribute
;
660 #if 0 // having problems right now with that
661 if (HasFlag(wxSTAY_ON_TOP
))
662 wclass
= kUtilityWindowClass
;
667 if ( HasFlag(wxFRAME_SHAPED
) )
669 WindowDefSpec customWindowDefSpec
;
670 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
671 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
673 ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
674 attr
, &theBoundsRect
,
675 (WindowRef
*) &m_macWindow
);
680 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
683 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
684 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
685 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
687 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
688 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
689 GetEventTypeCount(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
691 // Patch 531199 also defined a window event handler, as follows:
693 // install a window event handler to send wxEVT_MOVING and wxEVT_SIZING events
694 EventTypeSpec events
[] = { { kEventClassWindow
, kEventWindowBoundsChanging
} };
695 EventHandlerUPP handlerProc
= NewEventHandlerUPP( WindowHandler
);
696 EventHandlerRef eventHandlerRef
;
697 InstallWindowEventHandler( m_macWindowData
->m_macWindow
, handlerProc
, GetEventTypeCount(events
),
698 events
, (void*)this, &eventHandlerRef
);
706 if ( HasFlag(wxFRAME_SHAPED
) )
708 // default shape matches the window size
709 wxRegion
rgn(0, 0, m_width
, m_height
);
715 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
717 ((Point
*)localOrigin
)->h
= 0;
718 ((Point
*)localOrigin
)->v
= 0;
719 ((Rect
*)clipRect
)->left
= 0;
720 ((Rect
*)clipRect
)->top
= 0;
721 ((Rect
*)clipRect
)->right
= m_width
;
722 ((Rect
*)clipRect
)->bottom
= m_height
;
723 *window
= m_macWindow
;
727 void wxTopLevelWindowMac::Clear()
732 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
734 return m_macRootControl
;
738 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
740 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
742 RgnHandle visRgn
= NewRgn() ;
743 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
744 BeginUpdate( (WindowRef
)m_macWindow
) ;
746 RgnHandle updateRgn
= NewRgn();
747 RgnHandle diffRgn
= NewRgn() ;
749 if ( updateRgn
&& diffRgn
)
752 // macos internal control redraws clean up areas we'd like to redraw ourselves
753 // therefore we pick the boundary rect and make sure we can redraw it
754 // this has to be intersected by the visRgn in order to avoid drawing over its own
756 RgnHandle trueUpdateRgn
= NewRgn() ;
757 Rect trueUpdateRgnBoundary
;
758 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
759 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
760 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
761 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
763 DisposeRgn( trueUpdateRgn
) ;
764 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
766 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
768 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
769 if ( !EmptyRgn( updateRgn
) )
771 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
775 DisposeRgn( updateRgn
);
777 DisposeRgn( diffRgn
);
779 DisposeRgn( visRgn
) ;
781 EndUpdate( (WindowRef
)m_macWindow
) ;
782 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
783 m_macNeedsErasing
= false ;
787 // Raise the window to the top of the Z order
788 void wxTopLevelWindowMac::Raise()
790 ::SelectWindow( (WindowRef
)m_macWindow
) ;
793 // Lower the window to the bottom of the Z order
794 void wxTopLevelWindowMac::Lower()
796 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
799 void wxTopLevelWindowMac::MacFireMouseEvent(
800 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
802 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
803 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
804 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
806 event
.m_leftDown
= isDown
&& !controlDown
;
808 event
.m_middleDown
= FALSE
;
809 event
.m_rightDown
= isDown
&& controlDown
;
811 if ( kind
== mouseDown
)
814 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
816 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
818 else if ( kind
== mouseUp
)
821 event
.SetEventType(wxEVT_RIGHT_UP
) ;
823 event
.SetEventType(wxEVT_LEFT_UP
) ;
827 event
.SetEventType(wxEVT_MOTION
) ;
830 event
.m_shiftDown
= modifiers
& shiftKey
;
831 event
.m_controlDown
= modifiers
& controlKey
;
832 event
.m_altDown
= modifiers
& optionKey
;
833 event
.m_metaDown
= modifiers
& cmdKey
;
841 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
842 ::GlobalToLocal( &localwhere
) ;
845 if ( kind
== mouseDown
)
847 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
849 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
851 // This is not right if the second mouse down
852 // event occured in a differen window. We
853 // correct this in MacDispatchMouseEvent.
855 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
857 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
863 gs_lastWhen
= timestamp
;
865 gs_lastWhere
= localwhere
;
868 event
.m_x
= localwhere
.h
;
869 event
.m_y
= localwhere
.v
;
873 event
.m_timeStamp
= timestamp
;
874 event
.SetEventObject(this);
875 if ( wxTheApp
->s_captureWindow
)
879 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
882 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
883 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
885 if ( kind
== mouseUp
)
887 wxTheApp
->s_captureWindow
= NULL
;
890 m_cursor
.MacInstall() ;
896 MacDispatchMouseEvent( event
) ;
902 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
904 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
905 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
908 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
914 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
915 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
921 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
927 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
928 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
936 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
938 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
939 event
.m_timeStamp
= timestamp
;
940 event
.SetEventObject(this);
942 GetEventHandler()->ProcessEvent(event
);
944 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
946 // Early versions of MacOS X don't refresh backgrounds properly,
947 // so refresh the whole window on activation and deactivation.
948 long osVersion
= UMAGetSystemVersion();
949 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
955 // for the moment we have to resolve some redrawing issues like this
956 // the OS is stealing some redrawing areas as soon as it draws a control
963 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
969 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
971 wxWindow::SetTitle( title
) ;
972 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
975 bool wxTopLevelWindowMac::Show(bool show
)
977 if ( !wxWindow::Show(show
) )
982 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
983 ::SelectWindow( (WindowRef
)m_macWindow
) ;
984 // no need to generate events here, they will get them triggered by macos
985 // actually they should be , but apparently they are not
986 wxSize
size(m_width
, m_height
);
987 wxSizeEvent
event(size
, m_windowId
);
988 event
.SetEventObject(this);
989 GetEventHandler()->ProcessEvent(event
);
993 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1007 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1009 int former_x
= m_x
;
1010 int former_y
= m_y
;
1011 int former_w
= m_width
;
1012 int former_h
= m_height
;
1014 int actualWidth
= width
;
1015 int actualHeight
= height
;
1019 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1020 actualWidth
= m_minWidth
;
1021 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1022 actualHeight
= m_minHeight
;
1023 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1024 actualWidth
= m_maxWidth
;
1025 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1026 actualHeight
= m_maxHeight
;
1028 bool doMove
= false ;
1029 bool doResize
= false ;
1031 if ( actualX
!= former_x
|| actualY
!= former_y
)
1035 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1040 if ( doMove
|| doResize
)
1044 m_width
= actualWidth
;
1045 m_height
= actualHeight
;
1048 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1051 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1053 // the OS takes care of invalidating and erasing the new area so we only have to
1054 // take care of refreshing for full repaints
1056 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
1060 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1062 wxFrame
* frame
= (wxFrame
*) this ;
1063 frame
->PositionStatusBar();
1064 frame
->PositionToolBar();
1067 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1069 MacRepositionScrollBars() ;
1072 wxPoint
point(m_x
, m_y
);
1073 wxMoveEvent
event(point
, m_windowId
);
1074 event
.SetEventObject(this);
1075 GetEventHandler()->ProcessEvent(event
) ;
1079 MacRepositionScrollBars() ;
1080 wxSize
size(m_width
, m_height
);
1081 wxSizeEvent
event(size
, m_windowId
);
1082 event
.SetEventObject(this);
1083 GetEventHandler()->ProcessEvent(event
);
1090 * Invalidation Mechanism
1092 * The update mechanism reflects exactely the windows mechanism
1093 * the rect gets added to the window invalidate region, if the eraseBackground flag
1094 * has been true for any part of the update rgn the background is erased in the entire region
1095 * not just in the specified rect.
1097 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1098 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1099 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1100 * will get the eraseBackground event first
1103 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1105 GrafPtr formerPort
;
1106 GetPort( &formerPort
) ;
1107 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1109 m_macNeedsErasing
|= eraseBackground
;
1111 // if we already know that we will have to erase, there's no need to track the rest
1112 if ( !m_macNeedsErasing
)
1114 // we end only here if eraseBackground is false
1115 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1116 // we will have to erase anyway
1118 RgnHandle updateRgn
= NewRgn();
1119 RgnHandle diffRgn
= NewRgn() ;
1120 if ( updateRgn
&& diffRgn
)
1122 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1124 LocalToGlobal( &pt
) ;
1125 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1126 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1127 if ( !EmptyRgn( diffRgn
) )
1129 m_macNeedsErasing
= true ;
1133 DisposeRgn( updateRgn
);
1135 DisposeRgn( diffRgn
);
1137 if ( !m_macNeedsErasing
)
1139 RgnHandle rectRgn
= NewRgn() ;
1140 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1141 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1142 DisposeRgn( rectRgn
) ;
1145 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1146 // turn this on to debug the refreshing cycle
1147 #if wxMAC_DEBUG_REDRAW
1150 SetPort( formerPort
) ;
1154 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1156 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1157 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1160 // The empty region signifies that the shape should be removed from the
1162 if ( region
.IsEmpty() )
1164 wxSize sz
= GetClientSize();
1165 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1166 return SetShape(rgn
);
1169 // Make a copy of the region
1170 RgnHandle shapeRegion
= NewRgn();
1171 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1173 // Dispose of any shape region we may already have
1174 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1178 // Save the region so we can use it later
1179 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1181 // Tell the window manager that the window has changed shape
1182 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1189 // ---------------------------------------------------------------------------
1190 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1191 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1192 // ---------------------------------------------------------------------------
1196 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1198 GetWindowPortBounds(window
, inRect
);
1199 Point pt
= {inRect
->left
, inRect
->top
};
1200 SetPort((GrafPtr
) GetWindowPort(window
));
1203 inRect
->left
= pt
.h
;
1204 inRect
->bottom
+= pt
.v
;
1205 inRect
->right
+= pt
.h
;
1209 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1211 /*------------------------------------------------------
1212 Define which options your custom window supports.
1213 --------------------------------------------------------*/
1214 //just enable everything for our demo
1215 *(OptionBits
*)param
=//kWindowCanGrow|
1217 //kWindowCanCollapse|
1218 //kWindowCanGetWindowRegion|
1219 //kWindowHasTitleBar|
1220 //kWindowSupportsDragHilite|
1221 kWindowCanDrawInCurrentPort
|
1222 //kWindowCanMeasureTitle|
1223 kWindowWantsDisposeAtProcessDeath
|
1224 kWindowSupportsSetGrowImageRegion
|
1225 kWindowDefSupportsColorGrafPort
;
1229 // The content region is left as a rectangle matching the window size, this is
1230 // so the origin in the paint event, and etc. still matches what the
1231 // programmer expects.
1232 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1235 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1238 wxRect r
= win
->GetRect();
1239 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1243 // The structure region is set to the shape given to the SetShape method.
1244 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1246 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1252 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1253 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1254 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1255 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1261 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1263 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1265 switch(rgnRec
->regionCode
)
1267 case kWindowStructureRgn
:
1268 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1270 case kWindowContentRgn
:
1271 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1274 SetEmptyRgn(rgnRec
->winRgn
);
1281 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1283 /*------------------------------------------------------
1284 Determine the region of the window which was hit
1285 --------------------------------------------------------*/
1287 static RgnHandle tempRgn
=nil
;
1292 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1294 //Mac OS 8.5 or later
1295 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1296 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1299 return wNoHit
;//no significant area was hit.
1303 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1307 case kWindowMsgHitTest
:
1308 return wxShapedMacWindowHitTest(window
,param
);
1310 case kWindowMsgGetFeatures
:
1311 return wxShapedMacWindowGetFeatures(window
,param
);
1313 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1314 case kWindowMsgGetRegion
:
1315 return wxShapedMacWindowGetRegion(window
,param
);
1322 // ---------------------------------------------------------------------------