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 // ----------------------------------------------------------------------------
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"
45 #if wxUSE_SYSTEM_OPTIONS
46 #include "wx/sysopt.h"
49 #include "ToolUtils.h"
52 #define wxMAC_DEBUG_REDRAW 0
53 #ifndef wxMAC_DEBUG_REDRAW
54 #define wxMAC_DEBUG_REDRAW 0
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // list of all frames and modeless dialogs
62 wxWindowList wxModelessWindows
;
64 // double click testing
65 static Point gs_lastWhere
;
66 static long gs_lastWhen
= 0;
70 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
73 // ============================================================================
74 // wxTopLevelWindowMac implementation
75 // ============================================================================
77 // ---------------------------------------------------------------------------
79 // ---------------------------------------------------------------------------
83 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
85 static const EventTypeSpec eventList
[] =
87 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
89 { kEventClassKeyboard
, kEventRawKeyDown
} ,
90 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
91 { kEventClassKeyboard
, kEventRawKeyUp
} ,
92 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
94 { kEventClassWindow
, kEventWindowShown
} ,
95 { kEventClassWindow
, kEventWindowUpdate
} ,
96 { kEventClassWindow
, kEventWindowActivated
} ,
97 { kEventClassWindow
, kEventWindowDeactivated
} ,
98 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
99 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
100 { kEventClassWindow
, kEventWindowClose
} ,
102 { kEventClassMouse
, kEventMouseDown
} ,
103 { kEventClassMouse
, kEventMouseUp
} ,
104 { kEventClassMouse
, kEventMouseWheelMoved
} ,
105 { kEventClassMouse
, kEventMouseMoved
} ,
106 { kEventClassMouse
, kEventMouseDragged
} ,
110 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
112 OSStatus result
= eventNotHandledErr
;
114 wxWindow
* focus
= wxWindow::FindFocus() ;
122 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
124 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
125 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
126 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
127 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
128 sizeof( Point
), NULL
, &point
);
130 switch ( GetEventKind( event
) )
132 case kEventTextInputUnicodeForKeyEvent
:
133 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
134 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
135 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
138 ControlHandle macControl
= (ControlHandle
) control
->GetMacControl() ;
141 ::HandleControlKey( macControl
, keyCode
, charCode
, modifiers
) ;
146 // this may lead to double events sent to a window in case all handlers have skipped the key down event
147 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
148 UInt32 message = (keyCode << 8) + charCode;
150 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
151 focus , message , modifiers , when , point.h , point.v ) )
162 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
164 OSStatus result
= eventNotHandledErr
;
166 wxWindow
* focus
= wxWindow::FindFocus() ;
171 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
173 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
174 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
175 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
176 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
177 sizeof( Point
), NULL
, &point
);
179 UInt32 message
= (keyCode
<< 8) + charCode
;
180 switch( GetEventKind( event
) )
182 case kEventRawKeyRepeat
:
183 case kEventRawKeyDown
:
185 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
186 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
187 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
188 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
189 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
193 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
196 case kEventRawKeyUp
:
197 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
198 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
203 case kEventRawKeyModifiersChanged
:
205 wxKeyEvent
event(wxEVT_KEY_DOWN
);
207 event
.m_shiftDown
= modifiers
& shiftKey
;
208 event
.m_controlDown
= modifiers
& controlKey
;
209 event
.m_altDown
= modifiers
& optionKey
;
210 event
.m_metaDown
= modifiers
& cmdKey
;
214 event
.m_timeStamp
= when
;
215 wxWindow
* focus
= wxWindow::FindFocus() ;
216 event
.SetEventObject(focus
);
218 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
220 event
.m_keyCode
= WXK_CONTROL
;
221 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
222 focus
->GetEventHandler()->ProcessEvent( event
) ;
224 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
226 event
.m_keyCode
= WXK_SHIFT
;
227 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
228 focus
->GetEventHandler()->ProcessEvent( event
) ;
230 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
232 event
.m_keyCode
= WXK_ALT
;
233 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
234 focus
->GetEventHandler()->ProcessEvent( event
) ;
236 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
238 event
.m_keyCode
= WXK_COMMAND
;
239 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
240 focus
->GetEventHandler()->ProcessEvent( event
) ;
242 wxTheApp
->s_lastModifiers
= modifiers
;
250 pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
252 OSStatus result
= eventNotHandledErr
;
254 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
256 UInt32 modifiers
= 0;
257 EventMouseButton button
= 0 ;
260 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
261 sizeof( Point
), NULL
, &point
);
262 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
263 sizeof( UInt32
), NULL
, &modifiers
);
264 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
265 sizeof( EventMouseButton
), NULL
, &button
);
266 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
267 sizeof( UInt32
), NULL
, &click
);
269 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
270 modifiers
+= btnState
;
272 // temporary hack to support true two button mouse
273 if ( button
== kEventMouseButtonSecondary
)
275 modifiers
|= controlKey
;
278 short windowPart
= ::FindWindow(point
, &window
);
280 // either we really are active or we are capturing mouse events
282 if ( (IsWindowActive(window
) && windowPart
== inContent
) ||
283 (wxTheApp
->s_captureWindow
&& wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() == toplevelWindow
) )
285 switch ( GetEventKind( event
) )
287 case kEventMouseDown
:
288 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
292 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
295 case kEventMouseMoved
:
296 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
299 case kEventMouseDragged
:
300 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
303 case kEventMouseWheelMoved
:
305 //bClearTooltip = false;
306 EventMouseWheelAxis axis
= kEventMouseWheelAxisY
;
308 Point mouseLoc
= {0, 0};
309 if (::GetEventParameter(event
, kEventParamMouseWheelAxis
, typeMouseWheelAxis
,
310 NULL
, sizeof(EventMouseWheelAxis
), NULL
, &axis
) == noErr
&&
311 ::GetEventParameter(event
, kEventParamMouseWheelDelta
, typeLongInteger
,
312 NULL
, sizeof(SInt32
), NULL
, &delta
) == noErr
&&
313 ::GetEventParameter(event
, kEventParamMouseLocation
, typeQDPoint
,
314 NULL
, sizeof(Point
), NULL
, &mouseLoc
) == noErr
)
316 wxMouseEvent
wheelEvent(wxEVT_MOUSEWHEEL
);
318 wheelEvent
.m_x
= mouseLoc
.h
;
319 wheelEvent
.m_y
= mouseLoc
.v
;
321 wheelEvent
.m_wheelRotation
= delta
;
322 wheelEvent
.m_wheelDelta
= 1;
323 wheelEvent
.m_linesPerAction
= 1;
325 wxWindow
* currentMouseWindow
= NULL
;
326 wxWindow::MacGetWindowFromPoint(wxPoint(mouseLoc
.h
, mouseLoc
.v
), ¤tMouseWindow
);
328 if (currentMouseWindow
)
330 currentMouseWindow
->GetEventHandler()->ProcessEvent(wheelEvent
);
345 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
347 OSStatus result
= eventNotHandledErr
;
348 OSStatus err
= noErr
;
351 WindowRef windowRef
;
352 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
354 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
355 sizeof( WindowRef
), NULL
, &windowRef
);
357 switch( GetEventKind( event
) )
359 case kEventWindowUpdate
:
360 if ( !wxPendingDelete
.Member(toplevelWindow
) )
361 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
364 case kEventWindowActivated
:
365 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
368 case kEventWindowDeactivated
:
369 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
372 case kEventWindowShown
:
373 toplevelWindow
->Refresh() ;
376 case kEventWindowClose
:
377 toplevelWindow
->Close() ;
380 case kEventWindowBoundsChanged
:
381 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
382 NULL
, sizeof( UInt32
), NULL
, &attributes
);
385 Rect newContentRect
;
387 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
388 sizeof( newContentRect
), NULL
, &newContentRect
);
390 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
391 newContentRect
.right
- newContentRect
.left
,
392 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
397 case kEventWindowBoundsChanging
:
398 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
399 NULL
, sizeof( UInt32
), NULL
, &attributes
);
402 Rect newContentRect
;
404 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
405 sizeof( newContentRect
), NULL
, &newContentRect
);
407 wxSize formerSize
= toplevelWindow
->GetSize() ;
409 if ( (attributes
& kWindowBoundsChangeSizeChanged
) ||
410 ( attributes
& kWindowBoundsChangeOriginChanged
) )
411 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
412 newContentRect
.right
- newContentRect
.left
,
413 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
416 toplevelWindow
->GetPosition( &x
, &y
) ;
417 toplevelWindow
->GetSize( &w
, &h
) ;
418 Rect adjustedRect
= { y
, x
, y
+ h
, x
+ w
} ;
420 if ( !EqualRect( &newContentRect
, &adjustedRect
) )
422 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( adjustedRect
) , &adjustedRect
) ;
425 if ( toplevelWindow
->GetSize() != formerSize
)
426 toplevelWindow
->Update() ;
437 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
439 OSStatus result
= eventNotHandledErr
;
441 switch ( GetEventClass( event
) )
443 case kEventClassKeyboard
:
444 result
= KeyboardEventHandler( handler
, event
, data
) ;
446 case kEventClassTextInput
:
447 result
= TextInputEventHandler( handler
, event
, data
) ;
449 case kEventClassWindow
:
450 result
= WindowEventHandler( handler
, event
, data
) ;
452 case kEventClassMouse
:
453 result
= MouseEventHandler( handler
, event
, data
) ;
461 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
465 // ---------------------------------------------------------------------------
466 // wxWindowMac utility functions
467 // ---------------------------------------------------------------------------
469 // Find an item given the Macintosh Window Reference
471 wxList
*wxWinMacWindowList
= NULL
;
472 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
474 if ( wxWinMacWindowList
== NULL
)
476 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
479 return (wxTopLevelWindowMac
*)node
->GetData();
482 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
484 // adding NULL WindowRef is (first) surely a result of an error and
485 // (secondly) breaks menu command processing
486 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
488 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
489 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
492 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
494 wxWinMacWindowList
->DeleteObject(win
);
498 // ----------------------------------------------------------------------------
499 // wxTopLevelWindowMac creation
500 // ----------------------------------------------------------------------------
502 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
503 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
504 bool wxTopLevelWindowMac::s_macWindowCompositing
= FALSE
;
506 void wxTopLevelWindowMac::Init()
509 m_maximizeOnShow
= FALSE
;
510 m_macNoEraseUpdateRgn
= NewRgn() ;
511 m_macNeedsErasing
= false ;
513 m_macUsesCompositing
= FALSE
;
515 m_macEventHandler
= NULL
;
519 class wxMacDeferredWindowDeleter
: public wxObject
522 wxMacDeferredWindowDeleter( WindowRef windowRef
)
524 m_macWindow
= windowRef
;
526 virtual ~wxMacDeferredWindowDeleter()
528 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
531 WindowRef m_macWindow
;
534 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
536 const wxString
& title
,
540 const wxString
& name
)
545 m_windowStyle
= style
;
549 m_windowId
= id
== -1 ? NewControlId() : id
;
551 wxTopLevelWindows
.Append(this);
554 parent
->AddChild(this);
559 wxTopLevelWindowMac::~wxTopLevelWindowMac()
563 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
564 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
568 if ( m_macEventHandler
)
570 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
571 m_macEventHandler
= NULL
;
575 wxRemoveMacWindowAssociation( this ) ;
577 if ( wxModelessWindows
.Find(this) )
578 wxModelessWindows
.DeleteObject(this);
580 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
584 // ----------------------------------------------------------------------------
585 // wxTopLevelWindowMac maximize/minimize
586 // ----------------------------------------------------------------------------
588 void wxTopLevelWindowMac::Maximize(bool maximize
)
590 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
591 wxMacWindowClipper
clip (this);
592 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
597 Point pt
= { 0, 0 } ;
598 SetPortWindowPort((WindowRef
)m_macWindow
) ;
599 LocalToGlobal( &pt
) ;
602 GetWindowPortBounds((WindowRef
)m_macWindow
, &tempRect
) ;
603 SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
604 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
607 bool wxTopLevelWindowMac::IsMaximized() const
609 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
612 void wxTopLevelWindowMac::Iconize(bool iconize
)
614 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
615 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
618 bool wxTopLevelWindowMac::IsIconized() const
620 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
623 void wxTopLevelWindowMac::Restore()
625 // not available on mac
628 // ----------------------------------------------------------------------------
629 // wxTopLevelWindowMac misc
630 // ----------------------------------------------------------------------------
632 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
635 wxTopLevelWindowBase::SetIcon(icon
);
638 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
642 const wxString
& name
)
644 OSStatus err
= noErr
;
646 m_windowStyle
= style
;
667 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
669 // translate the window attributes in the appropriate window class and attributes
671 WindowClass wclass
= 0;
672 WindowAttributes attr
= kWindowNoAttributes
;
674 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
677 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
678 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
679 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
682 wclass
= kFloatingWindowClass
;
683 if ( HasFlag(wxTINY_CAPTION_VERT
) )
685 attr
|= kWindowSideTitlebarAttribute
;
691 wclass
= kPlainWindowClass
;
693 wclass
= kFloatingWindowClass
;
697 else if ( HasFlag( wxCAPTION
) )
699 wclass
= kDocumentWindowClass
;
703 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
704 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
706 wclass
= kDocumentWindowClass
;
711 wclass
= kPlainWindowClass
;
713 wclass
= kModalWindowClass
;
718 if ( HasFlag( wxMINIMIZE_BOX
) )
720 attr
|= kWindowCollapseBoxAttribute
;
722 if ( HasFlag( wxMAXIMIZE_BOX
) )
724 attr
|= kWindowFullZoomAttribute
;
726 if ( HasFlag( wxRESIZE_BORDER
) )
728 attr
|= kWindowResizableAttribute
;
730 if ( HasFlag( wxCLOSE_BOX
) )
732 attr
|= kWindowCloseBoxAttribute
;
735 if (UMAGetSystemVersion() >= 0x1000)
737 //turn on live resizing (OS X only)
738 attr
|= kWindowLiveResizeAttribute
;
742 #if 0 // having problems right now with that
743 if (HasFlag(wxSTAY_ON_TOP
))
744 wclass
= kUtilityWindowClass
;
748 //this setup lets us have compositing and non-compositing
749 //windows in the same application.
751 #if UNIVERSAL_INTERFACES_VERSION >= 0x0400
752 if ( wxTopLevelWindowMac::s_macWindowCompositing
)
754 attr
|= kWindowCompositingAttribute
;
755 m_macUsesCompositing
= TRUE
;
760 m_macUsesCompositing
= FALSE
;
764 if ( HasFlag(wxFRAME_SHAPED
) )
766 WindowDefSpec customWindowDefSpec
;
767 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
768 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
770 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
771 attr
, &theBoundsRect
,
772 (WindowRef
*) &m_macWindow
);
777 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
780 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
781 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
782 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
783 if ( wxTopLevelWindowMac::s_macWindowCompositing
)
785 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
789 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
792 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
793 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
794 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
800 if ( HasFlag(wxFRAME_SHAPED
) )
802 // default shape matches the window size
803 wxRegion
rgn(0, 0, m_width
, m_height
);
808 wxWindowCreateEvent
event(this);
809 GetEventHandler()->ProcessEvent(event
);
812 bool wxTopLevelWindowMac::MacEnableCompositing( bool useCompositing
)
814 bool oldval
= s_macWindowCompositing
;
815 s_macWindowCompositing
= useCompositing
;
819 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
821 ((Point
*)localOrigin
)->h
= 0;
822 ((Point
*)localOrigin
)->v
= 0;
823 ((Rect
*)clipRect
)->left
= 0;
824 ((Rect
*)clipRect
)->top
= 0;
825 ((Rect
*)clipRect
)->right
= m_width
;
826 ((Rect
*)clipRect
)->bottom
= m_height
;
827 *window
= m_macWindow
;
831 void wxTopLevelWindowMac::ClearBackground()
833 wxWindow::ClearBackground() ;
836 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
838 return m_macRootControl
;
842 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
844 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
846 RgnHandle visRgn
= NewRgn() ;
847 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
848 BeginUpdate( (WindowRef
)m_macWindow
) ;
850 RgnHandle updateRgn
= NewRgn();
851 RgnHandle diffRgn
= NewRgn() ;
853 if ( updateRgn
&& diffRgn
)
856 // macos internal control redraws clean up areas we'd like to redraw ourselves
857 // therefore we pick the boundary rect and make sure we can redraw it
858 // this has to be intersected by the visRgn in order to avoid drawing over its own
860 RgnHandle trueUpdateRgn
= NewRgn() ;
861 Rect trueUpdateRgnBoundary
;
862 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
863 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
864 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
865 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
867 DisposeRgn( trueUpdateRgn
) ;
868 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
870 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
872 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
873 if ( !EmptyRgn( updateRgn
) )
875 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
879 DisposeRgn( updateRgn
);
881 DisposeRgn( diffRgn
);
883 DisposeRgn( visRgn
) ;
885 EndUpdate( (WindowRef
)m_macWindow
) ;
886 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
887 m_macNeedsErasing
= false ;
891 // Raise the window to the top of the Z order
892 void wxTopLevelWindowMac::Raise()
894 ::SelectWindow( (WindowRef
)m_macWindow
) ;
897 // Lower the window to the bottom of the Z order
898 void wxTopLevelWindowMac::Lower()
900 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
903 void wxTopLevelWindowMac::MacFireMouseEvent(
904 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
906 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
907 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
908 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
910 event
.m_leftDown
= isDown
&& !controlDown
;
912 event
.m_middleDown
= FALSE
;
913 event
.m_rightDown
= isDown
&& controlDown
;
915 if ( kind
== mouseDown
)
918 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
920 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
922 else if ( kind
== mouseUp
)
925 event
.SetEventType(wxEVT_RIGHT_UP
) ;
927 event
.SetEventType(wxEVT_LEFT_UP
) ;
931 event
.SetEventType(wxEVT_MOTION
) ;
934 event
.m_shiftDown
= modifiers
& shiftKey
;
935 event
.m_controlDown
= modifiers
& controlKey
;
936 event
.m_altDown
= modifiers
& optionKey
;
937 event
.m_metaDown
= modifiers
& cmdKey
;
945 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
946 ::GlobalToLocal( &localwhere
) ;
949 if ( kind
== mouseDown
)
951 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
953 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
955 // This is not right if the second mouse down
956 // event occured in a differen window. We
957 // correct this in MacDispatchMouseEvent.
959 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
961 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
967 gs_lastWhen
= timestamp
;
969 gs_lastWhere
= localwhere
;
972 event
.m_x
= localwhere
.h
;
973 event
.m_y
= localwhere
.v
;
977 event
.m_timeStamp
= timestamp
;
978 event
.SetEventObject(this);
979 if ( wxTheApp
->s_captureWindow
)
983 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
986 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
987 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
989 if ( kind
== mouseUp
)
991 wxTheApp
->s_captureWindow
= NULL
;
994 m_cursor
.MacInstall() ;
1000 MacDispatchMouseEvent( event
) ;
1006 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
1008 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1009 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1012 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
1018 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1019 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1025 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
1031 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1032 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1040 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1042 if(s_macDeactivateWindow
)
1044 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1045 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1049 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1051 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1053 if(s_macDeactivateWindow
==this)
1054 s_macDeactivateWindow
=NULL
;
1055 MacDelayedDeactivation(timestamp
);
1056 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
1057 event
.m_timeStamp
= timestamp
;
1058 event
.SetEventObject(this);
1060 GetEventHandler()->ProcessEvent(event
);
1062 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
1064 // Early versions of MacOS X don't refresh backgrounds properly,
1065 // so refresh the whole window on activation and deactivation.
1066 long osVersion
= UMAGetSystemVersion();
1067 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
1073 // for the moment we have to resolve some redrawing issues like this
1074 // the OS is stealing some redrawing areas as soon as it draws a control
1081 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
1087 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1089 wxWindow::SetTitle( title
) ;
1090 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
1093 bool wxTopLevelWindowMac::Show(bool show
)
1095 if ( !wxWindow::Show(show
) )
1100 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1101 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1103 ::ShowWindow( (WindowRef
)m_macWindow
);
1108 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1110 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1111 // no need to generate events here, they will get them triggered by macos
1112 // actually they should be , but apparently they are not
1113 wxSize
size(m_width
, m_height
);
1114 wxSizeEvent
event(size
, m_windowId
);
1115 event
.SetEventObject(this);
1116 GetEventHandler()->ProcessEvent(event
);
1120 #if wxUSE_SYSTEM_OPTIONS
1121 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1123 ::HideWindow((WindowRef
) m_macWindow
);
1128 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1143 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1145 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
1146 wxMacWindowClipper
clip (this);
1148 int former_x
= m_x
;
1149 int former_y
= m_y
;
1150 int former_w
= m_width
;
1151 int former_h
= m_height
;
1153 int actualWidth
= width
;
1154 int actualHeight
= height
;
1158 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1159 actualWidth
= m_minWidth
;
1160 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1161 actualHeight
= m_minHeight
;
1162 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1163 actualWidth
= m_maxWidth
;
1164 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1165 actualHeight
= m_maxHeight
;
1167 bool doMove
= false ;
1168 bool doResize
= false ;
1170 if ( actualX
!= former_x
|| actualY
!= former_y
)
1174 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1179 if ( doMove
|| doResize
)
1185 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1187 m_width
= actualWidth
;
1188 m_height
= actualHeight
;
1191 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1193 // the OS takes care of invalidating and erasing the new area so we only have to
1194 // take care of refreshing for full repaints
1196 if ( doResize
&& HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
1200 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1202 wxFrame
* frame
= (wxFrame
*) this ;
1204 frame
->PositionStatusBar();
1207 frame
->PositionToolBar();
1211 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1213 MacRepositionScrollBars() ;
1216 wxPoint
point(m_x
, m_y
);
1217 wxMoveEvent
event(point
, m_windowId
);
1218 event
.SetEventObject(this);
1219 GetEventHandler()->ProcessEvent(event
) ;
1223 MacRepositionScrollBars() ;
1224 wxSize
size(m_width
, m_height
);
1225 wxSizeEvent
event(size
, m_windowId
);
1226 event
.SetEventObject(this);
1227 GetEventHandler()->ProcessEvent(event
);
1234 * Invalidation Mechanism
1236 * The update mechanism reflects exactely the windows mechanism
1237 * the rect gets added to the window invalidate region, if the eraseBackground flag
1238 * has been true for any part of the update rgn the background is erased in the entire region
1239 * not just in the specified rect.
1241 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1242 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1243 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1244 * will get the eraseBackground event first
1247 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1249 GrafPtr formerPort
;
1250 GetPort( &formerPort
) ;
1251 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1253 m_macNeedsErasing
|= eraseBackground
;
1255 // if we already know that we will have to erase, there's no need to track the rest
1256 if ( !m_macNeedsErasing
)
1258 // we end only here if eraseBackground is false
1259 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1260 // we will have to erase anyway
1262 RgnHandle updateRgn
= NewRgn();
1263 RgnHandle diffRgn
= NewRgn() ;
1264 if ( updateRgn
&& diffRgn
)
1266 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1268 LocalToGlobal( &pt
) ;
1269 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1270 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1271 if ( !EmptyRgn( diffRgn
) )
1273 m_macNeedsErasing
= true ;
1277 DisposeRgn( updateRgn
);
1279 DisposeRgn( diffRgn
);
1281 if ( !m_macNeedsErasing
)
1283 RgnHandle rectRgn
= NewRgn() ;
1284 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1285 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1286 DisposeRgn( rectRgn
) ;
1289 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1290 // turn this on to debug the refreshing cycle
1291 #if wxMAC_DEBUG_REDRAW
1294 SetPort( formerPort
) ;
1298 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1300 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1301 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1304 // The empty region signifies that the shape should be removed from the
1306 if ( region
.IsEmpty() )
1308 wxSize sz
= GetClientSize();
1309 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1310 return SetShape(rgn
);
1313 // Make a copy of the region
1314 RgnHandle shapeRegion
= NewRgn();
1315 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1317 // Dispose of any shape region we may already have
1318 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1322 // Save the region so we can use it later
1323 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1325 // Tell the window manager that the window has changed shape
1326 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1333 // ---------------------------------------------------------------------------
1334 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1335 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1336 // ---------------------------------------------------------------------------
1340 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1342 GetWindowPortBounds(window
, inRect
);
1343 Point pt
= {inRect
->left
, inRect
->top
};
1344 SetPort((GrafPtr
) GetWindowPort(window
));
1347 inRect
->left
= pt
.h
;
1348 inRect
->bottom
+= pt
.v
;
1349 inRect
->right
+= pt
.h
;
1353 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1355 /*------------------------------------------------------
1356 Define which options your custom window supports.
1357 --------------------------------------------------------*/
1358 //just enable everything for our demo
1359 *(OptionBits
*)param
=//kWindowCanGrow|
1361 //kWindowCanCollapse|
1362 //kWindowCanGetWindowRegion|
1363 //kWindowHasTitleBar|
1364 //kWindowSupportsDragHilite|
1365 kWindowCanDrawInCurrentPort
|
1366 //kWindowCanMeasureTitle|
1367 kWindowWantsDisposeAtProcessDeath
|
1368 kWindowSupportsSetGrowImageRegion
|
1369 kWindowDefSupportsColorGrafPort
;
1373 // The content region is left as a rectangle matching the window size, this is
1374 // so the origin in the paint event, and etc. still matches what the
1375 // programmer expects.
1376 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1379 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1382 wxRect r
= win
->GetRect();
1383 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1387 // The structure region is set to the shape given to the SetShape method.
1388 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1390 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1396 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1397 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1398 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1399 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1405 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1407 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1409 switch(rgnRec
->regionCode
)
1411 case kWindowStructureRgn
:
1412 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1414 case kWindowContentRgn
:
1415 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1418 SetEmptyRgn(rgnRec
->winRgn
);
1425 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1427 /*------------------------------------------------------
1428 Determine the region of the window which was hit
1429 --------------------------------------------------------*/
1431 static RgnHandle tempRgn
=nil
;
1436 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1438 //Mac OS 8.5 or later
1439 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1440 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1443 return wNoHit
;//no significant area was hit.
1447 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1451 case kWindowMsgHitTest
:
1452 return wxShapedMacWindowHitTest(window
,param
);
1454 case kWindowMsgGetFeatures
:
1455 return wxShapedMacWindowGetFeatures(window
,param
);
1457 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1458 case kWindowMsgGetRegion
:
1459 return wxShapedMacWindowGetRegion(window
,param
);
1466 // ---------------------------------------------------------------------------