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;
66 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
68 // ============================================================================
69 // wxTopLevelWindowMac implementation
70 // ============================================================================
72 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
78 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
80 static const EventTypeSpec eventList
[] =
82 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
84 { kEventClassKeyboard
, kEventRawKeyDown
} ,
85 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
86 { kEventClassKeyboard
, kEventRawKeyUp
} ,
87 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
89 { kEventClassWindow
, kEventWindowUpdate
} ,
90 { kEventClassWindow
, kEventWindowActivated
} ,
91 { kEventClassWindow
, kEventWindowDeactivated
} ,
92 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
93 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
94 { kEventClassWindow
, kEventWindowClose
} ,
96 { kEventClassMouse
, kEventMouseDown
} ,
97 { kEventClassMouse
, kEventMouseUp
} ,
98 { kEventClassMouse
, kEventMouseMoved
} ,
99 { kEventClassMouse
, kEventMouseDragged
} ,
103 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
105 OSStatus result
= eventNotHandledErr
;
107 wxWindow
* focus
= wxWindow::FindFocus() ;
112 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
116 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
118 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
119 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
120 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
121 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
122 sizeof( Point
), NULL
, &point
);
124 UInt32 message
= (keyCode
<< 8) + charCode
;
126 switch ( GetEventKind( event
) )
128 case kEventTextInputUnicodeForKeyEvent
:
129 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
130 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
140 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
142 OSStatus result
= eventNotHandledErr
;
144 wxWindow
* focus
= wxWindow::FindFocus() ;
149 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
151 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
152 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
153 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
154 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
155 sizeof( Point
), NULL
, &point
);
157 UInt32 message
= (keyCode
<< 8) + charCode
;
158 switch( GetEventKind( event
) )
160 case kEventRawKeyRepeat
:
161 case kEventRawKeyDown
:
162 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
163 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
168 case kEventRawKeyUp
:
169 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
170 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
175 case kEventRawKeyModifiersChanged
:
177 wxKeyEvent
event(wxEVT_KEY_DOWN
);
179 event
.m_shiftDown
= modifiers
& shiftKey
;
180 event
.m_controlDown
= modifiers
& controlKey
;
181 event
.m_altDown
= modifiers
& optionKey
;
182 event
.m_metaDown
= modifiers
& cmdKey
;
186 event
.m_timeStamp
= when
;
187 wxWindow
* focus
= wxWindow::FindFocus() ;
188 event
.SetEventObject(focus
);
190 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
192 event
.m_keyCode
= WXK_CONTROL
;
193 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
194 focus
->GetEventHandler()->ProcessEvent( event
) ;
196 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
198 event
.m_keyCode
= WXK_SHIFT
;
199 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
200 focus
->GetEventHandler()->ProcessEvent( event
) ;
202 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
204 event
.m_keyCode
= WXK_ALT
;
205 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
206 focus
->GetEventHandler()->ProcessEvent( event
) ;
208 wxTheApp
->s_lastModifiers
= modifiers
;
216 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
218 OSStatus result
= eventNotHandledErr
;
220 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
222 UInt32 modifiers
= 0;
223 EventMouseButton button
= 0 ;
226 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
227 sizeof( Point
), NULL
, &point
);
228 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
229 sizeof( UInt32
), NULL
, &modifiers
);
230 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
231 sizeof( EventMouseButton
), NULL
, &button
);
232 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
233 sizeof( UInt32
), NULL
, &click
);
235 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
236 modifiers
+= btnState
;
239 short windowPart
= ::FindWindow(point
, &window
);
241 if ( IsWindowActive(window
) && windowPart
== inContent
)
243 switch ( GetEventKind( event
) )
245 case kEventMouseDown
:
246 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
250 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
253 case kEventMouseMoved
:
254 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
257 case kEventMouseDragged
:
258 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
270 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
272 OSStatus result
= eventNotHandledErr
;
273 OSStatus err
= noErr
;
276 WindowRef windowRef
;
277 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
279 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
280 sizeof( WindowRef
), NULL
, &windowRef
);
282 switch( GetEventKind( event
) )
284 case kEventWindowUpdate
:
285 if ( !wxPendingDelete
.Member(toplevelWindow
) )
286 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
289 case kEventWindowActivated
:
290 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
293 case kEventWindowDeactivated
:
294 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
297 case kEventWindowClose
:
298 toplevelWindow
->Close() ;
301 case kEventWindowBoundsChanged
:
302 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
303 NULL
, sizeof( UInt32
), NULL
, &attributes
);
306 Rect newContentRect
;
308 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
309 sizeof( newContentRect
), NULL
, &newContentRect
);
311 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
312 newContentRect
.right
- newContentRect
.left
,
313 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
324 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
326 OSStatus result
= eventNotHandledErr
;
328 switch ( GetEventClass( event
) )
330 case kEventClassKeyboard
:
331 result
= KeyboardEventHandler( handler
, event
, data
) ;
333 case kEventClassTextInput
:
334 result
= TextInputEventHandler( handler
, event
, data
) ;
336 case kEventClassWindow
:
337 result
= WindowEventHandler( handler
, event
, data
) ;
339 case kEventClassMouse
:
340 result
= MouseEventHandler( handler
, event
, data
) ;
348 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
352 // ---------------------------------------------------------------------------
353 // wxWindowMac utility functions
354 // ---------------------------------------------------------------------------
356 // Find an item given the Macintosh Window Reference
358 wxList
*wxWinMacWindowList
= NULL
;
359 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
361 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
364 return (wxTopLevelWindowMac
*)node
->GetData();
367 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
369 // adding NULL WindowRef is (first) surely a result of an error and
370 // (secondly) breaks menu command processing
371 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
373 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
374 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
377 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
379 wxWinMacWindowList
->DeleteObject(win
);
383 // ----------------------------------------------------------------------------
384 // wxTopLevelWindowMac creation
385 // ----------------------------------------------------------------------------
387 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
389 void wxTopLevelWindowMac::Init()
392 m_maximizeOnShow
= FALSE
;
393 m_macNoEraseUpdateRgn
= NewRgn() ;
394 m_macNeedsErasing
= false ;
397 m_macEventHandler
= NULL
;
401 class wxMacDeferredWindowDeleter
: public wxObject
404 wxMacDeferredWindowDeleter( WindowRef windowRef
)
406 m_macWindow
= windowRef
;
408 virtual ~wxMacDeferredWindowDeleter()
410 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
413 WindowRef m_macWindow
;
416 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
418 const wxString
& title
,
422 const wxString
& name
)
427 m_windowStyle
= style
;
431 m_windowId
= id
== -1 ? NewControlId() : id
;
433 wxTopLevelWindows
.Append(this);
436 parent
->AddChild(this);
441 wxTopLevelWindowMac::~wxTopLevelWindowMac()
445 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
446 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
450 if ( m_macEventHandler
)
452 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
453 m_macEventHandler
= NULL
;
457 wxRemoveMacWindowAssociation( this ) ;
459 if ( wxModelessWindows
.Find(this) )
460 wxModelessWindows
.DeleteObject(this);
462 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
466 // ----------------------------------------------------------------------------
467 // wxTopLevelWindowMac maximize/minimize
468 // ----------------------------------------------------------------------------
470 void wxTopLevelWindowMac::Maximize(bool maximize
)
472 // not available on mac
475 bool wxTopLevelWindowMac::IsMaximized() const
480 void wxTopLevelWindowMac::Iconize(bool iconize
)
482 // not available on mac
485 bool wxTopLevelWindowMac::IsIconized() const
487 // mac dialogs cannot be iconized
491 void wxTopLevelWindowMac::Restore()
493 // not available on mac
496 // ----------------------------------------------------------------------------
497 // wxTopLevelWindowMac misc
498 // ----------------------------------------------------------------------------
500 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
503 wxTopLevelWindowBase::SetIcon(icon
);
506 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
510 const wxString
& name
)
513 m_windowStyle
= style
;
534 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
536 // translate the window attributes in the appropriate window class and attributes
538 WindowClass wclass
= 0;
539 WindowAttributes attr
= kWindowNoAttributes
;
541 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
544 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
545 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
546 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
549 wclass
= kFloatingWindowClass
;
550 if ( HasFlag(wxTINY_CAPTION_VERT
) )
552 attr
|= kWindowSideTitlebarAttribute
;
558 wclass
= kPlainWindowClass
;
560 wclass
= kFloatingWindowClass
;
564 else if ( HasFlag( wxCAPTION
) )
566 wclass
= kDocumentWindowClass
;
570 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
571 HasFlag( wxSYSTEM_MENU
) )
573 wclass
= kDocumentWindowClass
;
578 wclass
= kPlainWindowClass
;
580 wclass
= kModalWindowClass
;
585 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
587 attr
|= kWindowFullZoomAttribute
;
588 attr
|= kWindowCollapseBoxAttribute
;
590 if ( HasFlag( wxRESIZE_BORDER
) )
592 attr
|= kWindowResizableAttribute
;
594 if ( HasFlag( wxSYSTEM_MENU
) )
596 attr
|= kWindowCloseBoxAttribute
;
599 if (HasFlag(wxSTAY_ON_TOP
))
600 wclass
= kUtilityWindowClass
;
602 if ( HasFlag(wxFRAME_SHAPED
) )
604 WindowDefSpec customWindowDefSpec
;
605 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
606 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
608 ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
609 attr
, &theBoundsRect
,
610 (WindowRef
*) &m_macWindow
);
614 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
617 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
618 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
619 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
621 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
622 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
623 GetEventTypeCount(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
628 if ( HasFlag(wxFRAME_SHAPED
) )
630 // default shape matches the window size
631 wxRegion
rgn(0, 0, m_width
, m_height
);
636 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
638 ((Point
*)localOrigin
)->h
= 0;
639 ((Point
*)localOrigin
)->v
= 0;
640 ((Rect
*)clipRect
)->left
= 0;
641 ((Rect
*)clipRect
)->top
= 0;
642 ((Rect
*)clipRect
)->right
= m_width
;
643 ((Rect
*)clipRect
)->bottom
= m_height
;
644 *window
= m_macWindow
;
648 void wxTopLevelWindowMac::Clear()
653 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
655 return m_macRootControl
;
659 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
661 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
663 BeginUpdate( (WindowRef
)m_macWindow
) ;
665 RgnHandle updateRgn
= NewRgn();
666 RgnHandle diffRgn
= NewRgn() ;
667 if ( updateRgn
&& diffRgn
)
670 // macos internal control redraws clean up areas we'd like to redraw ourselves
671 // therefore we pick the boundary rect and make sure we can redraw it
672 RgnHandle trueUpdateRgn
= NewRgn() ;
673 Rect trueUpdateRgnBoundary
;
674 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
675 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
676 RectRgn( (RgnHandle
) updateRgn
, &trueUpdateRgnBoundary
) ;
678 DisposeRgn( trueUpdateRgn
) ;
679 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
681 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
683 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
684 if ( !EmptyRgn( updateRgn
) )
686 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
690 DisposeRgn( updateRgn
);
692 DisposeRgn( diffRgn
);
693 EndUpdate( (WindowRef
)m_macWindow
) ;
694 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
695 m_macNeedsErasing
= false ;
699 // Raise the window to the top of the Z order
700 void wxTopLevelWindowMac::Raise()
702 ::SelectWindow( (WindowRef
)m_macWindow
) ;
705 // Lower the window to the bottom of the Z order
706 void wxTopLevelWindowMac::Lower()
708 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
711 void wxTopLevelWindowMac::MacFireMouseEvent(
712 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
714 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
715 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
716 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
718 event
.m_leftDown
= isDown
&& !controlDown
;
720 event
.m_middleDown
= FALSE
;
721 event
.m_rightDown
= isDown
&& controlDown
;
723 if ( kind
== mouseDown
)
726 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
728 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
730 else if ( kind
== mouseUp
)
733 event
.SetEventType(wxEVT_RIGHT_UP
) ;
735 event
.SetEventType(wxEVT_LEFT_UP
) ;
739 event
.SetEventType(wxEVT_MOTION
) ;
742 event
.m_shiftDown
= modifiers
& shiftKey
;
743 event
.m_controlDown
= modifiers
& controlKey
;
744 event
.m_altDown
= modifiers
& optionKey
;
745 event
.m_metaDown
= modifiers
& cmdKey
;
753 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
754 ::GlobalToLocal( &localwhere
) ;
757 if ( kind
== mouseDown
)
759 if ( timestamp
- gs_lastWhen
<= GetDblTime() )
761 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
763 // This is not right if the second mouse down
764 // event occured in a differen window. We
765 // correct this in MacDispatchMouseEvent.
767 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
769 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
775 gs_lastWhen
= timestamp
;
777 gs_lastWhere
= localwhere
;
780 event
.m_x
= localwhere
.h
;
781 event
.m_y
= localwhere
.v
;
785 event
.m_timeStamp
= timestamp
;
786 event
.SetEventObject(this);
787 if ( wxTheApp
->s_captureWindow
)
791 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
794 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
795 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
797 if ( kind
== mouseUp
)
799 wxTheApp
->s_captureWindow
= NULL
;
802 m_cursor
.MacInstall() ;
808 MacDispatchMouseEvent( event
) ;
814 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
816 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
817 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
820 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
826 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
827 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
833 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
839 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
840 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
848 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
850 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
851 event
.m_timeStamp
= timestamp
;
852 event
.SetEventObject(this);
854 GetEventHandler()->ProcessEvent(event
);
856 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
858 // Early versions of MacOS X don't refresh backgrounds properly,
859 // so refresh the whole window on activation and deactivation.
860 long osVersion
= UMAGetSystemVersion();
861 if (osVersion
>= 0x1000 && osVersion
< 0x1020)
867 // for the moment we have to resolve some redrawing issues like this
868 // the OS is stealing some redrawing areas as soon as it draws a control
875 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
881 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
883 wxWindow::SetTitle( title
) ;
884 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
887 bool wxTopLevelWindowMac::Show(bool show
)
889 if ( !wxWindow::Show(show
) )
894 // this is leading to incorrect window layering in some situations
895 // ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
896 ::ShowWindow( (WindowRef
)m_macWindow
) ;
897 ::SelectWindow( (WindowRef
)m_macWindow
) ;
898 // no need to generate events here, they will get them triggered by macos
899 // actually they should be , but apparently they are not
900 wxSize
size(m_width
, m_height
);
901 wxSizeEvent
event(size
, m_windowId
);
902 event
.SetEventObject(this);
903 GetEventHandler()->ProcessEvent(event
);
907 // this is leading to incorrect window layering in some situations
908 // ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
909 ::HideWindow( (WindowRef
)m_macWindow
) ;
923 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
927 int former_w
= m_width
;
928 int former_h
= m_height
;
930 int actualWidth
= width
;
931 int actualHeight
= height
;
935 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
936 actualWidth
= m_minWidth
;
937 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
938 actualHeight
= m_minHeight
;
939 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
940 actualWidth
= m_maxWidth
;
941 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
942 actualHeight
= m_maxHeight
;
944 bool doMove
= false ;
945 bool doResize
= false ;
947 if ( actualX
!= former_x
|| actualY
!= former_y
)
951 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
956 if ( doMove
|| doResize
)
960 m_width
= actualWidth
;
961 m_height
= actualHeight
;
964 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
967 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
969 // the OS takes care of invalidating and erasing the new area so we only have to
970 // take care of refreshing for full repaints
972 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
976 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
978 wxFrame
* frame
= (wxFrame
*) this ;
979 frame
->PositionStatusBar();
980 frame
->PositionToolBar();
983 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
985 MacRepositionScrollBars() ;
988 wxPoint
point(m_x
, m_y
);
989 wxMoveEvent
event(point
, m_windowId
);
990 event
.SetEventObject(this);
991 GetEventHandler()->ProcessEvent(event
) ;
995 MacRepositionScrollBars() ;
996 wxSize
size(m_width
, m_height
);
997 wxSizeEvent
event(size
, m_windowId
);
998 event
.SetEventObject(this);
999 GetEventHandler()->ProcessEvent(event
);
1006 * Invalidation Mechanism
1008 * The update mechanism reflects exactely the windows mechanism
1009 * the rect gets added to the window invalidate region, if the eraseBackground flag
1010 * has been true for any part of the update rgn the background is erased in the entire region
1011 * not just in the specified rect.
1013 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1014 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1015 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1016 * will get the eraseBackground event first
1019 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1021 GrafPtr formerPort
;
1022 GetPort( &formerPort
) ;
1023 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1025 m_macNeedsErasing
|= eraseBackground
;
1027 // if we already know that we will have to erase, there's no need to track the rest
1028 if ( !m_macNeedsErasing
)
1030 // we end only here if eraseBackground is false
1031 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1032 // we will have to erase anyway
1034 RgnHandle updateRgn
= NewRgn();
1035 RgnHandle diffRgn
= NewRgn() ;
1036 if ( updateRgn
&& diffRgn
)
1038 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1040 LocalToGlobal( &pt
) ;
1041 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1042 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1043 if ( !EmptyRgn( diffRgn
) )
1045 m_macNeedsErasing
= true ;
1049 DisposeRgn( updateRgn
);
1051 DisposeRgn( diffRgn
);
1053 if ( !m_macNeedsErasing
)
1055 RgnHandle rectRgn
= NewRgn() ;
1056 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1057 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1058 DisposeRgn( rectRgn
) ;
1061 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1062 // turn this on to debug the refreshing cycle
1063 #if wxMAC_DEBUG_REDRAW
1066 SetPort( formerPort
) ;
1070 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1072 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1073 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1075 // The empty region signifies that the shape should be removed from the
1077 if ( region
.IsEmpty() )
1079 wxSize sz
= GetClientSize();
1080 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1081 return SetShape(rgn
);
1084 // Make a copy of the region
1085 RgnHandle shapeRegion
= NewRgn();
1086 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1088 // Dispose of any shape region we may already have
1089 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1093 // Save the region so we can use it later
1094 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1096 // Tell the window manager that the window has changed shape
1097 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1103 // ---------------------------------------------------------------------------
1104 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1105 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1106 // ---------------------------------------------------------------------------
1109 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1111 GetWindowPortBounds(window
, inRect
);
1112 Point pt
= {inRect
->left
, inRect
->top
};
1113 SetPort((GrafPtr
) GetWindowPort(window
));
1116 inRect
->left
= pt
.h
;
1117 inRect
->bottom
+= pt
.v
;
1118 inRect
->right
+= pt
.h
;
1122 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1124 /*------------------------------------------------------
1125 Define which options your custom window supports.
1126 --------------------------------------------------------*/
1127 //just enable everything for our demo
1128 *(OptionBits
*)param
=//kWindowCanGrow|
1130 //kWindowCanCollapse|
1131 //kWindowCanGetWindowRegion|
1132 //kWindowHasTitleBar|
1133 //kWindowSupportsDragHilite|
1134 kWindowCanDrawInCurrentPort
|
1135 //kWindowCanMeasureTitle|
1136 kWindowWantsDisposeAtProcessDeath
|
1137 kWindowSupportsSetGrowImageRegion
|
1138 kWindowDefSupportsColorGrafPort
;
1142 // The content region is left as a rectangle matching the window size, this is
1143 // so the origin in the paint event, and etc. still matches what the
1144 // programmer expects.
1145 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1148 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1151 wxRect r
= win
->GetRect();
1152 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1156 // The structure region is set to the shape given to the SetShape method.
1157 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1159 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1165 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1166 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1167 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1168 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1174 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1176 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1178 switch(rgnRec
->regionCode
)
1180 case kWindowStructureRgn
:
1181 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1183 case kWindowContentRgn
:
1184 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1187 SetEmptyRgn(rgnRec
->winRgn
);
1194 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1196 /*------------------------------------------------------
1197 Determine the region of the window which was hit
1198 --------------------------------------------------------*/
1200 static RgnHandle tempRgn
=nil
;
1205 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1207 //Mac OS 8.5 or later
1208 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1209 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1212 return wNoHit
;//no significant area was hit.
1216 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1220 case kWindowMsgHitTest
:
1221 return wxShapedMacWindowHitTest(window
,param
);
1223 case kWindowMsgGetFeatures
:
1224 return wxShapedMacWindowGetFeatures(window
,param
);
1226 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1227 case kWindowMsgGetRegion
:
1228 return wxShapedMacWindowGetRegion(window
,param
);
1234 // ---------------------------------------------------------------------------