1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for Mac
4 // Author: Stefan Csomor
8 // Copyright: (c) 2001-2004 Stefan Csomor
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/toplevel.h"
31 #include "wx/string.h"
36 #include "wx/mac/uma.h"
37 #include "wx/mac/aga.h"
39 #include "wx/tooltip.h"
41 #if wxUSE_SYSTEM_OPTIONS
42 #include "wx/sysopt.h"
45 #include <ToolUtils.h>
48 #define wxMAC_DEBUG_REDRAW 0
49 #ifndef wxMAC_DEBUG_REDRAW
50 #define wxMAC_DEBUG_REDRAW 0
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // list of all frames and modeless dialogs
58 wxWindowList wxModelessWindows
;
60 // double click testing
61 static Point gs_lastWhere
;
62 static long gs_lastWhen
= 0;
66 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
69 // ============================================================================
70 // wxTopLevelWindowMac implementation
71 // ============================================================================
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
79 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
81 static const EventTypeSpec eventList
[] =
83 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
85 { kEventClassKeyboard
, kEventRawKeyDown
} ,
86 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
87 { kEventClassKeyboard
, kEventRawKeyUp
} ,
88 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
90 { kEventClassWindow
, kEventWindowShown
} ,
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
.SetTimestamp(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 kEventWindowShown
:
369 toplevelWindow
->Refresh() ;
372 case kEventWindowClose
:
373 toplevelWindow
->Close() ;
376 case kEventWindowBoundsChanged
:
377 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
378 NULL
, sizeof( UInt32
), NULL
, &attributes
);
381 Rect newContentRect
;
383 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
384 sizeof( newContentRect
), NULL
, &newContentRect
);
386 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
387 newContentRect
.right
- newContentRect
.left
,
388 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
393 case kEventWindowBoundsChanging
:
394 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
395 NULL
, sizeof( UInt32
), NULL
, &attributes
);
398 Rect newContentRect
;
400 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
401 sizeof( newContentRect
), NULL
, &newContentRect
);
403 wxSize formerSize
= toplevelWindow
->GetSize() ;
405 if ( (attributes
& kWindowBoundsChangeSizeChanged
) ||
406 ( attributes
& kWindowBoundsChangeOriginChanged
) )
407 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
408 newContentRect
.right
- newContentRect
.left
,
409 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
412 toplevelWindow
->GetPosition( &x
, &y
) ;
413 toplevelWindow
->GetSize( &w
, &h
) ;
414 Rect adjustedRect
= { y
, x
, y
+ h
, x
+ w
} ;
416 if ( !EqualRect( &newContentRect
, &adjustedRect
) )
418 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( adjustedRect
) , &adjustedRect
) ;
421 if ( toplevelWindow
->GetSize() != formerSize
)
422 toplevelWindow
->Update() ;
433 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
435 OSStatus result
= eventNotHandledErr
;
437 switch ( GetEventClass( event
) )
439 case kEventClassKeyboard
:
440 result
= KeyboardEventHandler( handler
, event
, data
) ;
442 case kEventClassTextInput
:
443 result
= TextInputEventHandler( handler
, event
, data
) ;
445 case kEventClassWindow
:
446 result
= WindowEventHandler( handler
, event
, data
) ;
448 case kEventClassMouse
:
449 result
= MouseEventHandler( handler
, event
, data
) ;
457 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
461 // ---------------------------------------------------------------------------
462 // wxWindowMac utility functions
463 // ---------------------------------------------------------------------------
465 // Find an item given the Macintosh Window Reference
467 wxList
*wxWinMacWindowList
= NULL
;
468 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
470 if ( wxWinMacWindowList
== NULL
)
472 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
475 return (wxTopLevelWindowMac
*)node
->GetData();
478 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
);
479 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
481 // adding NULL WindowRef is (first) surely a result of an error and
482 // (secondly) breaks menu command processing
483 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
485 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
486 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
489 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
491 wxWinMacWindowList
->DeleteObject(win
);
495 // ----------------------------------------------------------------------------
496 // wxTopLevelWindowMac creation
497 // ----------------------------------------------------------------------------
499 WXWindow
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
500 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
501 bool wxTopLevelWindowMac::s_macWindowCompositing
= FALSE
;
503 void wxTopLevelWindowMac::Init()
506 m_maximizeOnShow
= FALSE
;
507 m_macNoEraseUpdateRgn
= NewRgn() ;
508 m_macNeedsErasing
= false ;
510 m_macUsesCompositing
= FALSE
;
512 m_macEventHandler
= NULL
;
516 class wxMacDeferredWindowDeleter
: public wxObject
519 wxMacDeferredWindowDeleter( WindowRef windowRef
)
521 m_macWindow
= windowRef
;
523 virtual ~wxMacDeferredWindowDeleter()
525 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
528 WindowRef m_macWindow
;
531 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
533 const wxString
& title
,
537 const wxString
& name
)
542 m_windowStyle
= style
;
546 m_windowId
= id
== -1 ? NewControlId() : id
;
548 wxTopLevelWindows
.Append(this);
551 parent
->AddChild(this);
556 wxTopLevelWindowMac::~wxTopLevelWindowMac()
560 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
561 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
565 if ( m_macEventHandler
)
567 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
568 m_macEventHandler
= NULL
;
572 wxRemoveMacWindowAssociation( this ) ;
574 if ( wxModelessWindows
.Find(this) )
575 wxModelessWindows
.DeleteObject(this);
577 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
581 // ----------------------------------------------------------------------------
582 // wxTopLevelWindowMac maximize/minimize
583 // ----------------------------------------------------------------------------
585 void wxTopLevelWindowMac::Maximize(bool maximize
)
587 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
588 wxMacWindowClipper
clip (this);
589 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
594 Point pt
= { 0, 0 } ;
595 SetPortWindowPort((WindowRef
)m_macWindow
) ;
596 LocalToGlobal( &pt
) ;
599 GetWindowPortBounds((WindowRef
)m_macWindow
, &tempRect
) ;
600 SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
601 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
604 bool wxTopLevelWindowMac::IsMaximized() const
606 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
609 void wxTopLevelWindowMac::Iconize(bool iconize
)
611 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
612 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
615 bool wxTopLevelWindowMac::IsIconized() const
617 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
620 void wxTopLevelWindowMac::Restore()
622 // not available on mac
625 // ----------------------------------------------------------------------------
626 // wxTopLevelWindowMac misc
627 // ----------------------------------------------------------------------------
629 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
632 wxTopLevelWindowBase::SetIcon(icon
);
635 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
639 const wxString
& name
)
641 OSStatus err
= noErr
;
643 m_windowStyle
= style
;
657 m_width
= WidthDefault(size
.x
);
658 m_height
= HeightDefault(size
.y
);
660 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
662 // translate the window attributes in the appropriate window class and attributes
664 WindowClass wclass
= 0;
665 WindowAttributes attr
= kWindowNoAttributes
;
667 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
670 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
671 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
672 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
675 wclass
= kFloatingWindowClass
;
676 if ( HasFlag(wxTINY_CAPTION_VERT
) )
678 attr
|= kWindowSideTitlebarAttribute
;
684 wclass
= kPlainWindowClass
;
686 wclass
= kFloatingWindowClass
;
690 else if ( HasFlag( wxCAPTION
) )
692 wclass
= kDocumentWindowClass
;
696 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
697 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
699 wclass
= kDocumentWindowClass
;
704 wclass
= kPlainWindowClass
;
706 wclass
= kModalWindowClass
;
711 if ( HasFlag( wxMINIMIZE_BOX
) )
713 attr
|= kWindowCollapseBoxAttribute
;
715 if ( HasFlag( wxMAXIMIZE_BOX
) )
717 attr
|= kWindowFullZoomAttribute
;
719 if ( HasFlag( wxRESIZE_BORDER
) )
721 attr
|= kWindowResizableAttribute
;
723 if ( HasFlag( wxCLOSE_BOX
) )
725 attr
|= kWindowCloseBoxAttribute
;
728 if (UMAGetSystemVersion() >= 0x1000)
730 //turn on live resizing (OS X only)
731 attr
|= kWindowLiveResizeAttribute
;
735 #if 0 // having problems right now with that
736 if (HasFlag(wxSTAY_ON_TOP
))
737 wclass
= kUtilityWindowClass
;
741 //this setup lets us have compositing and non-compositing
742 //windows in the same application.
744 #if UNIVERSAL_INTERFACES_VERSION >= 0x0400
745 if ( wxTopLevelWindowMac::s_macWindowCompositing
)
747 attr
|= kWindowCompositingAttribute
;
748 m_macUsesCompositing
= TRUE
;
753 m_macUsesCompositing
= FALSE
;
757 if ( HasFlag(wxFRAME_SHAPED
) )
759 WindowDefSpec customWindowDefSpec
;
760 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
761 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
763 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
764 attr
, &theBoundsRect
,
765 (WindowRef
*) &m_macWindow
);
770 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
773 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
774 wxAssociateWinWithMacWindow( (WindowRef
) m_macWindow
, this ) ;
775 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
776 if ( wxTopLevelWindowMac::s_macWindowCompositing
)
778 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
782 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
785 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
786 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
787 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
793 if ( HasFlag(wxFRAME_SHAPED
) )
795 // default shape matches the window size
796 wxRegion
rgn(0, 0, m_width
, m_height
);
801 wxWindowCreateEvent
event(this);
802 GetEventHandler()->ProcessEvent(event
);
805 bool wxTopLevelWindowMac::MacEnableCompositing( bool useCompositing
)
807 bool oldval
= s_macWindowCompositing
;
808 s_macWindowCompositing
= useCompositing
;
812 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXWindow
*window
, wxWindowMac
** rootwin
)
814 ((Point
*)localOrigin
)->h
= 0;
815 ((Point
*)localOrigin
)->v
= 0;
816 ((Rect
*)clipRect
)->left
= 0;
817 ((Rect
*)clipRect
)->top
= 0;
818 ((Rect
*)clipRect
)->right
= m_width
;
819 ((Rect
*)clipRect
)->bottom
= m_height
;
820 *window
= m_macWindow
;
824 void wxTopLevelWindowMac::ClearBackground()
826 wxWindow::ClearBackground() ;
829 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
831 return m_macRootControl
;
835 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
837 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
839 RgnHandle visRgn
= NewRgn() ;
840 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
841 BeginUpdate( (WindowRef
)m_macWindow
) ;
843 RgnHandle updateRgn
= NewRgn();
844 RgnHandle diffRgn
= NewRgn() ;
846 if ( updateRgn
&& diffRgn
)
849 // macos internal control redraws clean up areas we'd like to redraw ourselves
850 // therefore we pick the boundary rect and make sure we can redraw it
851 // this has to be intersected by the visRgn in order to avoid drawing over its own
853 RgnHandle trueUpdateRgn
= NewRgn() ;
854 Rect trueUpdateRgnBoundary
;
855 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
856 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
857 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
858 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
860 DisposeRgn( trueUpdateRgn
) ;
861 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
863 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
865 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
866 if ( !EmptyRgn( updateRgn
) )
868 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
872 DisposeRgn( updateRgn
);
874 DisposeRgn( diffRgn
);
876 DisposeRgn( visRgn
) ;
878 EndUpdate( (WindowRef
)m_macWindow
) ;
879 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
880 m_macNeedsErasing
= false ;
884 // Raise the window to the top of the Z order
885 void wxTopLevelWindowMac::Raise()
887 ::SelectWindow( (WindowRef
)m_macWindow
) ;
890 // Lower the window to the bottom of the Z order
891 void wxTopLevelWindowMac::Lower()
893 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
896 void wxTopLevelWindowMac::MacFireMouseEvent(
897 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
899 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
900 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
901 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
903 event
.m_leftDown
= isDown
&& !controlDown
;
905 event
.m_middleDown
= FALSE
;
906 event
.m_rightDown
= isDown
&& controlDown
;
908 if ( kind
== mouseDown
)
911 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
913 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
915 else if ( kind
== mouseUp
)
918 event
.SetEventType(wxEVT_RIGHT_UP
) ;
920 event
.SetEventType(wxEVT_LEFT_UP
) ;
924 event
.SetEventType(wxEVT_MOTION
) ;
927 event
.m_shiftDown
= modifiers
& shiftKey
;
928 event
.m_controlDown
= modifiers
& controlKey
;
929 event
.m_altDown
= modifiers
& optionKey
;
930 event
.m_metaDown
= modifiers
& cmdKey
;
938 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
939 ::GlobalToLocal( &localwhere
) ;
942 if ( kind
== mouseDown
)
944 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
946 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
948 // This is not right if the second mouse down
949 // event occurred in a different window. We
950 // correct this in MacDispatchMouseEvent.
952 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
954 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
960 gs_lastWhen
= timestamp
;
962 gs_lastWhere
= localwhere
;
965 event
.m_x
= localwhere
.h
;
966 event
.m_y
= localwhere
.v
;
970 event
.SetTimestamp(timestamp
);
971 event
.SetEventObject(this);
972 if ( wxTheApp
->s_captureWindow
)
976 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
979 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
980 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
982 if ( kind
== mouseUp
)
984 wxTheApp
->s_captureWindow
= NULL
;
987 m_cursor
.MacInstall() ;
993 MacDispatchMouseEvent( event
) ;
999 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
1001 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1002 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1005 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
1011 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1012 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1018 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
1024 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1025 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1033 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1035 if(s_macDeactivateWindow
)
1037 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1038 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1042 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1044 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1046 if(s_macDeactivateWindow
==this)
1047 s_macDeactivateWindow
=NULL
;
1048 MacDelayedDeactivation(timestamp
);
1049 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
1050 event
.SetTimestamp(timestamp
);
1051 event
.SetEventObject(this);
1053 GetEventHandler()->ProcessEvent(event
);
1055 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
1057 // Early versions of MacOS X don't refresh backgrounds properly,
1058 // so refresh the whole window on activation and deactivation.
1059 long osVersion
= UMAGetSystemVersion();
1060 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
1066 // for the moment we have to resolve some redrawing issues like this
1067 // the OS is stealing some redrawing areas as soon as it draws a control
1074 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
1080 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1082 wxWindow::SetTitle( title
) ;
1083 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
1086 bool wxTopLevelWindowMac::Show(bool show
)
1088 if ( !wxWindow::Show(show
) )
1093 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1094 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1096 ::ShowWindow( (WindowRef
)m_macWindow
);
1101 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1103 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1104 // no need to generate events here, they will get them triggered by macos
1105 // actually they should be , but apparently they are not
1106 wxSize
size(m_width
, m_height
);
1107 wxSizeEvent
event(size
, m_windowId
);
1108 event
.SetEventObject(this);
1109 GetEventHandler()->ProcessEvent(event
);
1113 #if wxUSE_SYSTEM_OPTIONS
1114 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1116 ::HideWindow((WindowRef
) m_macWindow
);
1121 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1136 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1138 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
1139 wxMacWindowClipper
clip (this);
1141 int former_x
= m_x
;
1142 int former_y
= m_y
;
1143 int former_w
= m_width
;
1144 int former_h
= m_height
;
1146 int actualWidth
= width
;
1147 int actualHeight
= height
;
1151 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1152 actualWidth
= m_minWidth
;
1153 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1154 actualHeight
= m_minHeight
;
1155 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1156 actualWidth
= m_maxWidth
;
1157 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1158 actualHeight
= m_maxHeight
;
1160 bool doMove
= false ;
1161 bool doResize
= false ;
1163 if ( actualX
!= former_x
|| actualY
!= former_y
)
1167 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1172 if ( doMove
|| doResize
)
1178 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1180 m_width
= actualWidth
;
1181 m_height
= actualHeight
;
1184 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1186 // the OS takes care of invalidating and erasing the new area so we only have to
1187 // take care of refreshing for full repaints
1189 if ( doResize
&& HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
1193 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1195 wxFrame
* frame
= (wxFrame
*) this ;
1197 frame
->PositionStatusBar();
1200 frame
->PositionToolBar();
1204 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1206 MacRepositionScrollBars() ;
1209 wxPoint
point(m_x
, m_y
);
1210 wxMoveEvent
event(point
, m_windowId
);
1211 event
.SetEventObject(this);
1212 GetEventHandler()->ProcessEvent(event
) ;
1216 MacRepositionScrollBars() ;
1217 wxSize
size(m_width
, m_height
);
1218 wxSizeEvent
event(size
, m_windowId
);
1219 event
.SetEventObject(this);
1220 GetEventHandler()->ProcessEvent(event
);
1227 * Invalidation Mechanism
1229 * The update mechanism reflects exactely the windows mechanism
1230 * the rect gets added to the window invalidate region, if the eraseBackground flag
1231 * has been true for any part of the update rgn the background is erased in the entire region
1232 * not just in the specified rect.
1234 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1235 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1236 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1237 * will get the eraseBackground event first
1240 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1242 GrafPtr formerPort
;
1243 GetPort( &formerPort
) ;
1244 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1246 m_macNeedsErasing
|= eraseBackground
;
1248 // if we already know that we will have to erase, there's no need to track the rest
1249 if ( !m_macNeedsErasing
)
1251 // we end only here if eraseBackground is false
1252 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1253 // we will have to erase anyway
1255 RgnHandle updateRgn
= NewRgn();
1256 RgnHandle diffRgn
= NewRgn() ;
1257 if ( updateRgn
&& diffRgn
)
1259 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1261 LocalToGlobal( &pt
) ;
1262 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1263 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1264 if ( !EmptyRgn( diffRgn
) )
1266 m_macNeedsErasing
= true ;
1270 DisposeRgn( updateRgn
);
1272 DisposeRgn( diffRgn
);
1274 if ( !m_macNeedsErasing
)
1276 RgnHandle rectRgn
= NewRgn() ;
1277 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1278 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1279 DisposeRgn( rectRgn
) ;
1282 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1283 // turn this on to debug the refreshing cycle
1284 #if wxMAC_DEBUG_REDRAW
1287 SetPort( formerPort
) ;
1291 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1293 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1294 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1297 // The empty region signifies that the shape should be removed from the
1299 if ( region
.IsEmpty() )
1301 wxSize sz
= GetClientSize();
1302 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1303 return SetShape(rgn
);
1306 // Make a copy of the region
1307 RgnHandle shapeRegion
= NewRgn();
1308 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1310 // Dispose of any shape region we may already have
1311 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1315 // Save the region so we can use it later
1316 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1318 // Tell the window manager that the window has changed shape
1319 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1326 // ---------------------------------------------------------------------------
1327 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1328 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1329 // ---------------------------------------------------------------------------
1333 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1335 GetWindowPortBounds(window
, inRect
);
1336 Point pt
= {inRect
->left
, inRect
->top
};
1337 SetPort((GrafPtr
) GetWindowPort(window
));
1340 inRect
->left
= pt
.h
;
1341 inRect
->bottom
+= pt
.v
;
1342 inRect
->right
+= pt
.h
;
1346 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1348 /*------------------------------------------------------
1349 Define which options your custom window supports.
1350 --------------------------------------------------------*/
1351 //just enable everything for our demo
1352 *(OptionBits
*)param
=//kWindowCanGrow|
1354 //kWindowCanCollapse|
1355 //kWindowCanGetWindowRegion|
1356 //kWindowHasTitleBar|
1357 //kWindowSupportsDragHilite|
1358 kWindowCanDrawInCurrentPort
|
1359 //kWindowCanMeasureTitle|
1360 kWindowWantsDisposeAtProcessDeath
|
1361 kWindowSupportsSetGrowImageRegion
|
1362 kWindowDefSupportsColorGrafPort
;
1366 // The content region is left as a rectangle matching the window size, this is
1367 // so the origin in the paint event, and etc. still matches what the
1368 // programmer expects.
1369 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1372 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1375 wxRect r
= win
->GetRect();
1376 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1380 // The structure region is set to the shape given to the SetShape method.
1381 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1383 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1389 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1390 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1391 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1392 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1398 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1400 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1402 switch(rgnRec
->regionCode
)
1404 case kWindowStructureRgn
:
1405 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1407 case kWindowContentRgn
:
1408 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1411 SetEmptyRgn(rgnRec
->winRgn
);
1418 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1420 /*------------------------------------------------------
1421 Determine the region of the window which was hit
1422 --------------------------------------------------------*/
1424 static RgnHandle tempRgn
=nil
;
1429 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1431 //Mac OS 8.5 or later
1432 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1433 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1436 return wNoHit
;//no significant area was hit.
1440 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1444 case kWindowMsgHitTest
:
1445 return wxShapedMacWindowHitTest(window
,param
);
1447 case kWindowMsgGetFeatures
:
1448 return wxShapedMacWindowGetFeatures(window
,param
);
1450 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1451 case kWindowMsgGetRegion
:
1452 return wxShapedMacWindowGetRegion(window
,param
);
1459 // ---------------------------------------------------------------------------