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
, kEventMouseWheelMoved
} ,
101 { kEventClassMouse
, kEventMouseMoved
} ,
102 { kEventClassMouse
, kEventMouseDragged
} ,
106 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
108 OSStatus result
= eventNotHandledErr
;
110 wxWindow
* focus
= wxWindow::FindFocus() ;
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 switch ( GetEventKind( event
) )
128 case kEventTextInputUnicodeForKeyEvent
:
129 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
130 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
131 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
134 ControlHandle macControl
= (ControlHandle
) control
->GetMacControl() ;
137 ::HandleControlKey( macControl
, keyCode
, charCode
, modifiers
) ;
142 // this may lead to double events sent to a window in case all handlers have skipped the key down event
143 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
144 UInt32 message = (keyCode << 8) + charCode;
146 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
147 focus , message , modifiers , when , point.h , point.v ) )
158 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
160 OSStatus result
= eventNotHandledErr
;
162 wxWindow
* focus
= wxWindow::FindFocus() ;
167 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
169 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
170 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
171 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
172 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
173 sizeof( Point
), NULL
, &point
);
175 UInt32 message
= (keyCode
<< 8) + charCode
;
176 switch( GetEventKind( event
) )
178 case kEventRawKeyRepeat
:
179 case kEventRawKeyDown
:
181 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
182 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
183 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
184 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
185 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
189 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
192 case kEventRawKeyUp
:
193 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
194 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
199 case kEventRawKeyModifiersChanged
:
201 wxKeyEvent
event(wxEVT_KEY_DOWN
);
203 event
.m_shiftDown
= modifiers
& shiftKey
;
204 event
.m_controlDown
= modifiers
& controlKey
;
205 event
.m_altDown
= modifiers
& optionKey
;
206 event
.m_metaDown
= modifiers
& cmdKey
;
210 event
.m_timeStamp
= when
;
211 wxWindow
* focus
= wxWindow::FindFocus() ;
212 event
.SetEventObject(focus
);
214 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
216 event
.m_keyCode
= WXK_CONTROL
;
217 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
218 focus
->GetEventHandler()->ProcessEvent( event
) ;
220 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
222 event
.m_keyCode
= WXK_SHIFT
;
223 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
224 focus
->GetEventHandler()->ProcessEvent( event
) ;
226 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
228 event
.m_keyCode
= WXK_ALT
;
229 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
230 focus
->GetEventHandler()->ProcessEvent( event
) ;
232 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
234 event
.m_keyCode
= WXK_COMMAND
;
235 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
236 focus
->GetEventHandler()->ProcessEvent( event
) ;
238 wxTheApp
->s_lastModifiers
= modifiers
;
246 pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
248 OSStatus result
= eventNotHandledErr
;
250 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
252 UInt32 modifiers
= 0;
253 EventMouseButton button
= 0 ;
256 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
257 sizeof( Point
), NULL
, &point
);
258 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
259 sizeof( UInt32
), NULL
, &modifiers
);
260 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
261 sizeof( EventMouseButton
), NULL
, &button
);
262 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
263 sizeof( UInt32
), NULL
, &click
);
265 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
266 modifiers
+= btnState
;
268 // temporary hack to support true two button mouse
269 if ( button
== kEventMouseButtonSecondary
)
271 modifiers
|= controlKey
;
274 short windowPart
= ::FindWindow(point
, &window
);
276 // either we really are active or we are capturing mouse events
278 if ( (IsWindowActive(window
) && windowPart
== inContent
) ||
279 (wxTheApp
->s_captureWindow
&& wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() == toplevelWindow
) )
281 switch ( GetEventKind( event
) )
283 case kEventMouseDown
:
284 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
288 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
291 case kEventMouseMoved
:
292 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
295 case kEventMouseDragged
:
296 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
299 case kEventMouseWheelMoved
:
301 //bClearTooltip = false;
302 EventMouseWheelAxis axis
= kEventMouseWheelAxisY
;
304 Point mouseLoc
= {0, 0};
305 if (::GetEventParameter(event
, kEventParamMouseWheelAxis
, typeMouseWheelAxis
,
306 NULL
, sizeof(EventMouseWheelAxis
), NULL
, &axis
) == noErr
&&
307 ::GetEventParameter(event
, kEventParamMouseWheelDelta
, typeLongInteger
,
308 NULL
, sizeof(SInt32
), NULL
, &delta
) == noErr
&&
309 ::GetEventParameter(event
, kEventParamMouseLocation
, typeQDPoint
,
310 NULL
, sizeof(Point
), NULL
, &mouseLoc
) == noErr
)
312 wxMouseEvent
wheelEvent(wxEVT_MOUSEWHEEL
);
314 wheelEvent
.m_x
= mouseLoc
.h
;
315 wheelEvent
.m_y
= mouseLoc
.v
;
317 wheelEvent
.m_wheelRotation
= delta
;
318 wheelEvent
.m_wheelDelta
= 1;
319 wheelEvent
.m_linesPerAction
= 1;
321 wxWindow
* currentMouseWindow
= NULL
;
322 wxWindow::MacGetWindowFromPoint(wxPoint(mouseLoc
.h
, mouseLoc
.v
), ¤tMouseWindow
);
324 if (currentMouseWindow
)
326 currentMouseWindow
->GetEventHandler()->ProcessEvent(wheelEvent
);
341 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
343 OSStatus result
= eventNotHandledErr
;
344 OSStatus err
= noErr
;
347 WindowRef windowRef
;
348 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
350 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
351 sizeof( WindowRef
), NULL
, &windowRef
);
353 switch( GetEventKind( event
) )
355 case kEventWindowUpdate
:
356 if ( !wxPendingDelete
.Member(toplevelWindow
) )
357 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
360 case kEventWindowActivated
:
361 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
364 case kEventWindowDeactivated
:
365 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
368 case kEventWindowClose
:
369 toplevelWindow
->Close() ;
372 case kEventWindowBoundsChanged
:
373 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
374 NULL
, sizeof( UInt32
), NULL
, &attributes
);
377 Rect newContentRect
;
379 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
380 sizeof( newContentRect
), NULL
, &newContentRect
);
382 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
383 newContentRect
.right
- newContentRect
.left
,
384 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
389 case kEventWindowBoundsChanging
:
390 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
391 NULL
, sizeof( UInt32
), NULL
, &attributes
);
394 Rect newContentRect
;
396 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
397 sizeof( newContentRect
), NULL
, &newContentRect
);
399 wxSize formerSize
= toplevelWindow
->GetSize() ;
401 if ( (attributes
& kWindowBoundsChangeSizeChanged
) ||
402 ( attributes
& kWindowBoundsChangeOriginChanged
) )
403 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
404 newContentRect
.right
- newContentRect
.left
,
405 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
408 toplevelWindow
->GetPosition( &x
, &y
) ;
409 toplevelWindow
->GetSize( &w
, &h
) ;
410 Rect adjustedRect
= { y
, x
, y
+ h
, x
+ w
} ;
412 if ( !EqualRect( &newContentRect
, &adjustedRect
) )
414 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( adjustedRect
) , &adjustedRect
) ;
417 if ( toplevelWindow
->GetSize() != formerSize
)
418 toplevelWindow
->Update() ;
429 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
431 OSStatus result
= eventNotHandledErr
;
433 switch ( GetEventClass( event
) )
435 case kEventClassKeyboard
:
436 result
= KeyboardEventHandler( handler
, event
, data
) ;
438 case kEventClassTextInput
:
439 result
= TextInputEventHandler( handler
, event
, data
) ;
441 case kEventClassWindow
:
442 result
= WindowEventHandler( handler
, event
, data
) ;
444 case kEventClassMouse
:
445 result
= MouseEventHandler( handler
, event
, data
) ;
453 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
457 // ---------------------------------------------------------------------------
458 // wxWindowMac utility functions
459 // ---------------------------------------------------------------------------
461 // Find an item given the Macintosh Window Reference
463 wxList
*wxWinMacWindowList
= NULL
;
464 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
466 if ( wxWinMacWindowList
== NULL
)
468 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
471 return (wxTopLevelWindowMac
*)node
->GetData();
474 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
476 // adding NULL WindowRef is (first) surely a result of an error and
477 // (secondly) breaks menu command processing
478 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
480 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
481 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
484 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
486 wxWinMacWindowList
->DeleteObject(win
);
490 // ----------------------------------------------------------------------------
491 // wxTopLevelWindowMac creation
492 // ----------------------------------------------------------------------------
494 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
495 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
497 void wxTopLevelWindowMac::Init()
500 m_maximizeOnShow
= FALSE
;
501 m_macNoEraseUpdateRgn
= NewRgn() ;
502 m_macNeedsErasing
= false ;
505 m_macEventHandler
= NULL
;
509 class wxMacDeferredWindowDeleter
: public wxObject
512 wxMacDeferredWindowDeleter( WindowRef windowRef
)
514 m_macWindow
= windowRef
;
516 virtual ~wxMacDeferredWindowDeleter()
518 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
521 WindowRef m_macWindow
;
524 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
526 const wxString
& title
,
530 const wxString
& name
)
535 m_windowStyle
= style
;
539 m_windowId
= id
== -1 ? NewControlId() : id
;
541 wxTopLevelWindows
.Append(this);
544 parent
->AddChild(this);
549 wxTopLevelWindowMac::~wxTopLevelWindowMac()
553 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
554 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
558 if ( m_macEventHandler
)
560 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
561 m_macEventHandler
= NULL
;
565 wxRemoveMacWindowAssociation( this ) ;
567 if ( wxModelessWindows
.Find(this) )
568 wxModelessWindows
.DeleteObject(this);
570 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
574 // ----------------------------------------------------------------------------
575 // wxTopLevelWindowMac maximize/minimize
576 // ----------------------------------------------------------------------------
578 void wxTopLevelWindowMac::Maximize(bool maximize
)
580 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
585 Point pt
= { 0, 0 } ;
586 SetPortWindowPort((WindowRef
)m_macWindow
) ;
587 LocalToGlobal( &pt
) ;
590 GetWindowPortBounds((WindowRef
)m_macWindow
, &tempRect
) ;
591 SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
592 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
595 bool wxTopLevelWindowMac::IsMaximized() const
597 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
600 void wxTopLevelWindowMac::Iconize(bool iconize
)
602 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
603 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
606 bool wxTopLevelWindowMac::IsIconized() const
608 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
611 void wxTopLevelWindowMac::Restore()
613 // not available on mac
616 // ----------------------------------------------------------------------------
617 // wxTopLevelWindowMac misc
618 // ----------------------------------------------------------------------------
620 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
623 wxTopLevelWindowBase::SetIcon(icon
);
626 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
630 const wxString
& name
)
632 OSStatus err
= noErr
;
634 m_windowStyle
= style
;
655 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
657 // translate the window attributes in the appropriate window class and attributes
659 WindowClass wclass
= 0;
660 WindowAttributes attr
= kWindowNoAttributes
;
662 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
665 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
666 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
667 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
670 wclass
= kFloatingWindowClass
;
671 if ( HasFlag(wxTINY_CAPTION_VERT
) )
673 attr
|= kWindowSideTitlebarAttribute
;
679 wclass
= kPlainWindowClass
;
681 wclass
= kFloatingWindowClass
;
685 else if ( HasFlag( wxCAPTION
) )
687 wclass
= kDocumentWindowClass
;
691 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
692 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
694 wclass
= kDocumentWindowClass
;
699 wclass
= kPlainWindowClass
;
701 wclass
= kModalWindowClass
;
706 if ( HasFlag( wxMINIMIZE_BOX
) )
708 attr
|= kWindowCollapseBoxAttribute
;
710 if ( HasFlag( wxMAXIMIZE_BOX
) )
712 attr
|= kWindowFullZoomAttribute
;
714 if ( HasFlag( wxRESIZE_BORDER
) )
716 attr
|= kWindowResizableAttribute
;
718 if ( HasFlag( wxCLOSE_BOX
) )
720 attr
|= kWindowCloseBoxAttribute
;
724 #if 0 // having problems right now with that
725 if (HasFlag(wxSTAY_ON_TOP
))
726 wclass
= kUtilityWindowClass
;
731 if ( HasFlag(wxFRAME_SHAPED
) )
733 WindowDefSpec customWindowDefSpec
;
734 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
735 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
737 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
738 attr
, &theBoundsRect
,
739 (WindowRef
*) &m_macWindow
);
744 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
747 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
748 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
749 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
750 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
752 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
753 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
754 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
760 if ( HasFlag(wxFRAME_SHAPED
) )
762 // default shape matches the window size
763 wxRegion
rgn(0, 0, m_width
, m_height
);
769 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
771 ((Point
*)localOrigin
)->h
= 0;
772 ((Point
*)localOrigin
)->v
= 0;
773 ((Rect
*)clipRect
)->left
= 0;
774 ((Rect
*)clipRect
)->top
= 0;
775 ((Rect
*)clipRect
)->right
= m_width
;
776 ((Rect
*)clipRect
)->bottom
= m_height
;
777 *window
= m_macWindow
;
781 void wxTopLevelWindowMac::ClearBackground()
783 wxWindow::ClearBackground() ;
786 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
788 return m_macRootControl
;
792 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
794 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
796 RgnHandle visRgn
= NewRgn() ;
797 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
798 BeginUpdate( (WindowRef
)m_macWindow
) ;
800 RgnHandle updateRgn
= NewRgn();
801 RgnHandle diffRgn
= NewRgn() ;
803 if ( updateRgn
&& diffRgn
)
806 // macos internal control redraws clean up areas we'd like to redraw ourselves
807 // therefore we pick the boundary rect and make sure we can redraw it
808 // this has to be intersected by the visRgn in order to avoid drawing over its own
810 RgnHandle trueUpdateRgn
= NewRgn() ;
811 Rect trueUpdateRgnBoundary
;
812 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
813 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
814 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
815 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
817 DisposeRgn( trueUpdateRgn
) ;
818 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
820 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
822 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
823 if ( !EmptyRgn( updateRgn
) )
825 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
829 DisposeRgn( updateRgn
);
831 DisposeRgn( diffRgn
);
833 DisposeRgn( visRgn
) ;
835 EndUpdate( (WindowRef
)m_macWindow
) ;
836 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
837 m_macNeedsErasing
= false ;
841 // Raise the window to the top of the Z order
842 void wxTopLevelWindowMac::Raise()
844 ::SelectWindow( (WindowRef
)m_macWindow
) ;
847 // Lower the window to the bottom of the Z order
848 void wxTopLevelWindowMac::Lower()
850 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
853 void wxTopLevelWindowMac::MacFireMouseEvent(
854 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
856 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
857 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
858 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
860 event
.m_leftDown
= isDown
&& !controlDown
;
862 event
.m_middleDown
= FALSE
;
863 event
.m_rightDown
= isDown
&& controlDown
;
865 if ( kind
== mouseDown
)
868 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
870 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
872 else if ( kind
== mouseUp
)
875 event
.SetEventType(wxEVT_RIGHT_UP
) ;
877 event
.SetEventType(wxEVT_LEFT_UP
) ;
881 event
.SetEventType(wxEVT_MOTION
) ;
884 event
.m_shiftDown
= modifiers
& shiftKey
;
885 event
.m_controlDown
= modifiers
& controlKey
;
886 event
.m_altDown
= modifiers
& optionKey
;
887 event
.m_metaDown
= modifiers
& cmdKey
;
895 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
896 ::GlobalToLocal( &localwhere
) ;
899 if ( kind
== mouseDown
)
901 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
903 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
905 // This is not right if the second mouse down
906 // event occured in a differen window. We
907 // correct this in MacDispatchMouseEvent.
909 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
911 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
917 gs_lastWhen
= timestamp
;
919 gs_lastWhere
= localwhere
;
922 event
.m_x
= localwhere
.h
;
923 event
.m_y
= localwhere
.v
;
927 event
.m_timeStamp
= timestamp
;
928 event
.SetEventObject(this);
929 if ( wxTheApp
->s_captureWindow
)
933 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
936 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
937 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
939 if ( kind
== mouseUp
)
941 wxTheApp
->s_captureWindow
= NULL
;
944 m_cursor
.MacInstall() ;
950 MacDispatchMouseEvent( event
) ;
956 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
958 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
959 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
962 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
968 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
969 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
975 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
981 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
982 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
990 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
992 if(s_macDeactivateWindow
)
994 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
995 s_macDeactivateWindow
->MacActivate(timestamp
, false);
999 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1001 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1003 if(s_macDeactivateWindow
==this)
1004 s_macDeactivateWindow
=NULL
;
1005 MacDelayedDeactivation(timestamp
);
1006 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
1007 event
.m_timeStamp
= timestamp
;
1008 event
.SetEventObject(this);
1010 GetEventHandler()->ProcessEvent(event
);
1012 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
1014 // Early versions of MacOS X don't refresh backgrounds properly,
1015 // so refresh the whole window on activation and deactivation.
1016 long osVersion
= UMAGetSystemVersion();
1017 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
1023 // for the moment we have to resolve some redrawing issues like this
1024 // the OS is stealing some redrawing areas as soon as it draws a control
1031 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
1037 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1039 wxWindow::SetTitle( title
) ;
1040 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
1043 bool wxTopLevelWindowMac::Show(bool show
)
1045 if ( !wxWindow::Show(show
) )
1050 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1051 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1052 // no need to generate events here, they will get them triggered by macos
1053 // actually they should be , but apparently they are not
1054 wxSize
size(m_width
, m_height
);
1055 wxSizeEvent
event(size
, m_windowId
);
1056 event
.SetEventObject(this);
1057 GetEventHandler()->ProcessEvent(event
);
1061 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1075 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1077 int former_x
= m_x
;
1078 int former_y
= m_y
;
1079 int former_w
= m_width
;
1080 int former_h
= m_height
;
1082 int actualWidth
= width
;
1083 int actualHeight
= height
;
1087 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1088 actualWidth
= m_minWidth
;
1089 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1090 actualHeight
= m_minHeight
;
1091 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1092 actualWidth
= m_maxWidth
;
1093 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1094 actualHeight
= m_maxHeight
;
1096 bool doMove
= false ;
1097 bool doResize
= false ;
1099 if ( actualX
!= former_x
|| actualY
!= former_y
)
1103 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1108 if ( doMove
|| doResize
)
1114 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1116 m_width
= actualWidth
;
1117 m_height
= actualHeight
;
1120 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1122 // the OS takes care of invalidating and erasing the new area so we only have to
1123 // take care of refreshing for full repaints
1125 if ( doResize
&& HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
1129 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1131 wxFrame
* frame
= (wxFrame
*) this ;
1133 frame
->PositionStatusBar();
1136 frame
->PositionToolBar();
1140 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1142 MacRepositionScrollBars() ;
1145 wxPoint
point(m_x
, m_y
);
1146 wxMoveEvent
event(point
, m_windowId
);
1147 event
.SetEventObject(this);
1148 GetEventHandler()->ProcessEvent(event
) ;
1152 MacRepositionScrollBars() ;
1153 wxSize
size(m_width
, m_height
);
1154 wxSizeEvent
event(size
, m_windowId
);
1155 event
.SetEventObject(this);
1156 GetEventHandler()->ProcessEvent(event
);
1163 * Invalidation Mechanism
1165 * The update mechanism reflects exactely the windows mechanism
1166 * the rect gets added to the window invalidate region, if the eraseBackground flag
1167 * has been true for any part of the update rgn the background is erased in the entire region
1168 * not just in the specified rect.
1170 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1171 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1172 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1173 * will get the eraseBackground event first
1176 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1178 GrafPtr formerPort
;
1179 GetPort( &formerPort
) ;
1180 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1182 m_macNeedsErasing
|= eraseBackground
;
1184 // if we already know that we will have to erase, there's no need to track the rest
1185 if ( !m_macNeedsErasing
)
1187 // we end only here if eraseBackground is false
1188 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1189 // we will have to erase anyway
1191 RgnHandle updateRgn
= NewRgn();
1192 RgnHandle diffRgn
= NewRgn() ;
1193 if ( updateRgn
&& diffRgn
)
1195 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1197 LocalToGlobal( &pt
) ;
1198 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1199 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1200 if ( !EmptyRgn( diffRgn
) )
1202 m_macNeedsErasing
= true ;
1206 DisposeRgn( updateRgn
);
1208 DisposeRgn( diffRgn
);
1210 if ( !m_macNeedsErasing
)
1212 RgnHandle rectRgn
= NewRgn() ;
1213 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1214 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1215 DisposeRgn( rectRgn
) ;
1218 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1219 // turn this on to debug the refreshing cycle
1220 #if wxMAC_DEBUG_REDRAW
1223 SetPort( formerPort
) ;
1227 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1229 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1230 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1233 // The empty region signifies that the shape should be removed from the
1235 if ( region
.IsEmpty() )
1237 wxSize sz
= GetClientSize();
1238 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1239 return SetShape(rgn
);
1242 // Make a copy of the region
1243 RgnHandle shapeRegion
= NewRgn();
1244 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1246 // Dispose of any shape region we may already have
1247 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1251 // Save the region so we can use it later
1252 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1254 // Tell the window manager that the window has changed shape
1255 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1262 // ---------------------------------------------------------------------------
1263 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1264 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1265 // ---------------------------------------------------------------------------
1269 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1271 GetWindowPortBounds(window
, inRect
);
1272 Point pt
= {inRect
->left
, inRect
->top
};
1273 SetPort((GrafPtr
) GetWindowPort(window
));
1276 inRect
->left
= pt
.h
;
1277 inRect
->bottom
+= pt
.v
;
1278 inRect
->right
+= pt
.h
;
1282 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1284 /*------------------------------------------------------
1285 Define which options your custom window supports.
1286 --------------------------------------------------------*/
1287 //just enable everything for our demo
1288 *(OptionBits
*)param
=//kWindowCanGrow|
1290 //kWindowCanCollapse|
1291 //kWindowCanGetWindowRegion|
1292 //kWindowHasTitleBar|
1293 //kWindowSupportsDragHilite|
1294 kWindowCanDrawInCurrentPort
|
1295 //kWindowCanMeasureTitle|
1296 kWindowWantsDisposeAtProcessDeath
|
1297 kWindowSupportsSetGrowImageRegion
|
1298 kWindowDefSupportsColorGrafPort
;
1302 // The content region is left as a rectangle matching the window size, this is
1303 // so the origin in the paint event, and etc. still matches what the
1304 // programmer expects.
1305 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1308 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1311 wxRect r
= win
->GetRect();
1312 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1316 // The structure region is set to the shape given to the SetShape method.
1317 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1319 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1325 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1326 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1327 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1328 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1334 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1336 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1338 switch(rgnRec
->regionCode
)
1340 case kWindowStructureRgn
:
1341 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1343 case kWindowContentRgn
:
1344 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1347 SetEmptyRgn(rgnRec
->winRgn
);
1354 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1356 /*------------------------------------------------------
1357 Determine the region of the window which was hit
1358 --------------------------------------------------------*/
1360 static RgnHandle tempRgn
=nil
;
1365 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1367 //Mac OS 8.5 or later
1368 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1369 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1372 return wNoHit
;//no significant area was hit.
1376 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1380 case kWindowMsgHitTest
:
1381 return wxShapedMacWindowHitTest(window
,param
);
1383 case kWindowMsgGetFeatures
:
1384 return wxShapedMacWindowGetFeatures(window
,param
);
1386 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1387 case kWindowMsgGetRegion
:
1388 return wxShapedMacWindowGetRegion(window
,param
);
1395 // ---------------------------------------------------------------------------