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
, kEventWindowUpdate
} ,
95 { kEventClassWindow
, kEventWindowActivated
} ,
96 { kEventClassWindow
, kEventWindowDeactivated
} ,
97 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
98 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
99 { kEventClassWindow
, kEventWindowClose
} ,
101 { kEventClassMouse
, kEventMouseDown
} ,
102 { kEventClassMouse
, kEventMouseUp
} ,
103 { kEventClassMouse
, kEventMouseWheelMoved
} ,
104 { kEventClassMouse
, kEventMouseMoved
} ,
105 { kEventClassMouse
, kEventMouseDragged
} ,
109 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
111 OSStatus result
= eventNotHandledErr
;
113 wxWindow
* focus
= wxWindow::FindFocus() ;
121 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
123 GetEventParameter( rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
124 GetEventParameter( rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
125 GetEventParameter( rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
126 GetEventParameter( rawEvent
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
127 sizeof( Point
), NULL
, &point
);
129 switch ( GetEventKind( event
) )
131 case kEventTextInputUnicodeForKeyEvent
:
132 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
133 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
134 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
137 ControlHandle macControl
= (ControlHandle
) control
->GetMacControl() ;
140 ::HandleControlKey( macControl
, keyCode
, charCode
, modifiers
) ;
145 // this may lead to double events sent to a window in case all handlers have skipped the key down event
146 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
147 UInt32 message = (keyCode << 8) + charCode;
149 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
150 focus , message , modifiers , when , point.h , point.v ) )
161 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
163 OSStatus result
= eventNotHandledErr
;
165 wxWindow
* focus
= wxWindow::FindFocus() ;
170 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
172 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
173 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
174 GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
175 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
176 sizeof( Point
), NULL
, &point
);
178 UInt32 message
= (keyCode
<< 8) + charCode
;
179 switch( GetEventKind( event
) )
181 case kEventRawKeyRepeat
:
182 case kEventRawKeyDown
:
184 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
185 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
186 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
187 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent(
188 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
192 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
195 case kEventRawKeyUp
:
196 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
197 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
202 case kEventRawKeyModifiersChanged
:
204 wxKeyEvent
event(wxEVT_KEY_DOWN
);
206 event
.m_shiftDown
= modifiers
& shiftKey
;
207 event
.m_controlDown
= modifiers
& controlKey
;
208 event
.m_altDown
= modifiers
& optionKey
;
209 event
.m_metaDown
= modifiers
& cmdKey
;
213 event
.m_timeStamp
= when
;
214 wxWindow
* focus
= wxWindow::FindFocus() ;
215 event
.SetEventObject(focus
);
217 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
219 event
.m_keyCode
= WXK_CONTROL
;
220 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
221 focus
->GetEventHandler()->ProcessEvent( event
) ;
223 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
225 event
.m_keyCode
= WXK_SHIFT
;
226 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
227 focus
->GetEventHandler()->ProcessEvent( event
) ;
229 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
231 event
.m_keyCode
= WXK_ALT
;
232 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
233 focus
->GetEventHandler()->ProcessEvent( event
) ;
235 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
237 event
.m_keyCode
= WXK_COMMAND
;
238 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
239 focus
->GetEventHandler()->ProcessEvent( event
) ;
241 wxTheApp
->s_lastModifiers
= modifiers
;
249 pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
251 OSStatus result
= eventNotHandledErr
;
253 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
255 UInt32 modifiers
= 0;
256 EventMouseButton button
= 0 ;
259 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
260 sizeof( Point
), NULL
, &point
);
261 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
262 sizeof( UInt32
), NULL
, &modifiers
);
263 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
264 sizeof( EventMouseButton
), NULL
, &button
);
265 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
266 sizeof( UInt32
), NULL
, &click
);
268 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
269 modifiers
+= btnState
;
271 // temporary hack to support true two button mouse
272 if ( button
== kEventMouseButtonSecondary
)
274 modifiers
|= controlKey
;
277 short windowPart
= ::FindWindow(point
, &window
);
279 // either we really are active or we are capturing mouse events
281 if ( (IsWindowActive(window
) && windowPart
== inContent
) ||
282 (wxTheApp
->s_captureWindow
&& wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() == toplevelWindow
) )
284 switch ( GetEventKind( event
) )
286 case kEventMouseDown
:
287 toplevelWindow
->MacFireMouseEvent( mouseDown
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
291 toplevelWindow
->MacFireMouseEvent( mouseUp
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
294 case kEventMouseMoved
:
295 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
298 case kEventMouseDragged
:
299 toplevelWindow
->MacFireMouseEvent( nullEvent
, point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
302 case kEventMouseWheelMoved
:
304 //bClearTooltip = false;
305 EventMouseWheelAxis axis
= kEventMouseWheelAxisY
;
307 Point mouseLoc
= {0, 0};
308 if (::GetEventParameter(event
, kEventParamMouseWheelAxis
, typeMouseWheelAxis
,
309 NULL
, sizeof(EventMouseWheelAxis
), NULL
, &axis
) == noErr
&&
310 ::GetEventParameter(event
, kEventParamMouseWheelDelta
, typeLongInteger
,
311 NULL
, sizeof(SInt32
), NULL
, &delta
) == noErr
&&
312 ::GetEventParameter(event
, kEventParamMouseLocation
, typeQDPoint
,
313 NULL
, sizeof(Point
), NULL
, &mouseLoc
) == noErr
)
315 wxMouseEvent
wheelEvent(wxEVT_MOUSEWHEEL
);
317 wheelEvent
.m_x
= mouseLoc
.h
;
318 wheelEvent
.m_y
= mouseLoc
.v
;
320 wheelEvent
.m_wheelRotation
= delta
;
321 wheelEvent
.m_wheelDelta
= 1;
322 wheelEvent
.m_linesPerAction
= 1;
324 wxWindow
* currentMouseWindow
= NULL
;
325 wxWindow::MacGetWindowFromPoint(wxPoint(mouseLoc
.h
, mouseLoc
.v
), ¤tMouseWindow
);
327 if (currentMouseWindow
)
329 currentMouseWindow
->GetEventHandler()->ProcessEvent(wheelEvent
);
344 static pascal OSStatus
WindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
346 OSStatus result
= eventNotHandledErr
;
347 OSStatus err
= noErr
;
350 WindowRef windowRef
;
351 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
353 GetEventParameter( event
, kEventParamDirectObject
, typeWindowRef
, NULL
,
354 sizeof( WindowRef
), NULL
, &windowRef
);
356 switch( GetEventKind( event
) )
358 case kEventWindowUpdate
:
359 if ( !wxPendingDelete
.Member(toplevelWindow
) )
360 toplevelWindow
->MacUpdate( EventTimeToTicks( GetEventTime( event
) ) ) ;
363 case kEventWindowActivated
:
364 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , true) ;
367 case kEventWindowDeactivated
:
368 toplevelWindow
->MacActivate( EventTimeToTicks( GetEventTime( event
) ) , false) ;
371 case kEventWindowClose
:
372 toplevelWindow
->Close() ;
375 case kEventWindowBoundsChanged
:
376 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
377 NULL
, sizeof( UInt32
), NULL
, &attributes
);
380 Rect newContentRect
;
382 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
383 sizeof( newContentRect
), NULL
, &newContentRect
);
385 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
386 newContentRect
.right
- newContentRect
.left
,
387 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
392 case kEventWindowBoundsChanging
:
393 err
= GetEventParameter( event
, kEventParamAttributes
, typeUInt32
,
394 NULL
, sizeof( UInt32
), NULL
, &attributes
);
397 Rect newContentRect
;
399 GetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, NULL
,
400 sizeof( newContentRect
), NULL
, &newContentRect
);
402 wxSize formerSize
= toplevelWindow
->GetSize() ;
404 if ( (attributes
& kWindowBoundsChangeSizeChanged
) ||
405 ( attributes
& kWindowBoundsChangeOriginChanged
) )
406 toplevelWindow
->SetSize( newContentRect
.left
, newContentRect
.top
,
407 newContentRect
.right
- newContentRect
.left
,
408 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
411 toplevelWindow
->GetPosition( &x
, &y
) ;
412 toplevelWindow
->GetSize( &w
, &h
) ;
413 Rect adjustedRect
= { y
, x
, y
+ h
, x
+ w
} ;
415 if ( !EqualRect( &newContentRect
, &adjustedRect
) )
417 SetEventParameter( event
, kEventParamCurrentBounds
, typeQDRectangle
, sizeof( adjustedRect
) , &adjustedRect
) ;
420 if ( toplevelWindow
->GetSize() != formerSize
)
421 toplevelWindow
->Update() ;
432 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
434 OSStatus result
= eventNotHandledErr
;
436 switch ( GetEventClass( event
) )
438 case kEventClassKeyboard
:
439 result
= KeyboardEventHandler( handler
, event
, data
) ;
441 case kEventClassTextInput
:
442 result
= TextInputEventHandler( handler
, event
, data
) ;
444 case kEventClassWindow
:
445 result
= WindowEventHandler( handler
, event
, data
) ;
447 case kEventClassMouse
:
448 result
= MouseEventHandler( handler
, event
, data
) ;
456 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler
)
460 // ---------------------------------------------------------------------------
461 // wxWindowMac utility functions
462 // ---------------------------------------------------------------------------
464 // Find an item given the Macintosh Window Reference
466 wxList
*wxWinMacWindowList
= NULL
;
467 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
469 if ( wxWinMacWindowList
== NULL
)
471 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
474 return (wxTopLevelWindowMac
*)node
->GetData();
477 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
479 // adding NULL WindowRef is (first) surely a result of an error and
480 // (secondly) breaks menu command processing
481 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
483 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
484 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
487 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
489 wxWinMacWindowList
->DeleteObject(win
);
493 // ----------------------------------------------------------------------------
494 // wxTopLevelWindowMac creation
495 // ----------------------------------------------------------------------------
497 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
498 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
499 bool wxTopLevelWindowMac::s_macWindowCompositing
= FALSE
;
501 void wxTopLevelWindowMac::Init()
504 m_maximizeOnShow
= FALSE
;
505 m_macNoEraseUpdateRgn
= NewRgn() ;
506 m_macNeedsErasing
= false ;
508 m_macUsesCompositing
= FALSE
;
510 m_macEventHandler
= NULL
;
514 class wxMacDeferredWindowDeleter
: public wxObject
517 wxMacDeferredWindowDeleter( WindowRef windowRef
)
519 m_macWindow
= windowRef
;
521 virtual ~wxMacDeferredWindowDeleter()
523 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
526 WindowRef m_macWindow
;
529 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
531 const wxString
& title
,
535 const wxString
& name
)
540 m_windowStyle
= style
;
544 m_windowId
= id
== -1 ? NewControlId() : id
;
546 wxTopLevelWindows
.Append(this);
549 parent
->AddChild(this);
554 wxTopLevelWindowMac::~wxTopLevelWindowMac()
558 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
559 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
563 if ( m_macEventHandler
)
565 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
566 m_macEventHandler
= NULL
;
570 wxRemoveMacWindowAssociation( this ) ;
572 if ( wxModelessWindows
.Find(this) )
573 wxModelessWindows
.DeleteObject(this);
575 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
579 // ----------------------------------------------------------------------------
580 // wxTopLevelWindowMac maximize/minimize
581 // ----------------------------------------------------------------------------
583 void wxTopLevelWindowMac::Maximize(bool maximize
)
585 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
586 wxMacWindowClipper
clip (this);
587 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
592 Point pt
= { 0, 0 } ;
593 SetPortWindowPort((WindowRef
)m_macWindow
) ;
594 LocalToGlobal( &pt
) ;
597 GetWindowPortBounds((WindowRef
)m_macWindow
, &tempRect
) ;
598 SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
599 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
602 bool wxTopLevelWindowMac::IsMaximized() const
604 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
607 void wxTopLevelWindowMac::Iconize(bool iconize
)
609 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
610 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
613 bool wxTopLevelWindowMac::IsIconized() const
615 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
618 void wxTopLevelWindowMac::Restore()
620 // not available on mac
623 // ----------------------------------------------------------------------------
624 // wxTopLevelWindowMac misc
625 // ----------------------------------------------------------------------------
627 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
630 wxTopLevelWindowBase::SetIcon(icon
);
633 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
637 const wxString
& name
)
639 OSStatus err
= noErr
;
641 m_windowStyle
= style
;
662 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
664 // translate the window attributes in the appropriate window class and attributes
666 WindowClass wclass
= 0;
667 WindowAttributes attr
= kWindowNoAttributes
;
669 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
672 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
673 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
674 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
677 wclass
= kFloatingWindowClass
;
678 if ( HasFlag(wxTINY_CAPTION_VERT
) )
680 attr
|= kWindowSideTitlebarAttribute
;
686 wclass
= kPlainWindowClass
;
688 wclass
= kFloatingWindowClass
;
692 else if ( HasFlag( wxCAPTION
) )
694 wclass
= kDocumentWindowClass
;
698 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
699 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
701 wclass
= kDocumentWindowClass
;
706 wclass
= kPlainWindowClass
;
708 wclass
= kModalWindowClass
;
713 if ( HasFlag( wxMINIMIZE_BOX
) )
715 attr
|= kWindowCollapseBoxAttribute
;
717 if ( HasFlag( wxMAXIMIZE_BOX
) )
719 attr
|= kWindowFullZoomAttribute
;
721 if ( HasFlag( wxRESIZE_BORDER
) )
723 attr
|= kWindowResizableAttribute
;
725 if ( HasFlag( wxCLOSE_BOX
) )
727 attr
|= kWindowCloseBoxAttribute
;
730 attr
|= kWindowLiveResizeAttribute
; //turn on live resizing
733 #if 0 // having problems right now with that
734 if (HasFlag(wxSTAY_ON_TOP
))
735 wclass
= kUtilityWindowClass
;
739 //this setup lets us have compositing and non-compositing
740 //windows in the same application.
742 #if UNIVERSAL_INTERFACES_VERSION >= 0x0400
743 if ( wxTopLevelWindowMac::s_macWindowCompositing
)
745 attr
|= kWindowCompositingAttribute
;
746 m_macUsesCompositing
= TRUE
;
751 m_macUsesCompositing
= FALSE
;
755 if ( HasFlag(wxFRAME_SHAPED
) )
757 WindowDefSpec customWindowDefSpec
;
758 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
759 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
761 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
762 attr
, &theBoundsRect
,
763 (WindowRef
*) &m_macWindow
);
768 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
771 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
772 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
773 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
774 if ( wxTopLevelWindowMac::s_macWindowCompositing
)
776 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
780 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
783 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
784 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacWindowEventHandlerUPP(),
785 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
791 if ( HasFlag(wxFRAME_SHAPED
) )
793 // default shape matches the window size
794 wxRegion
rgn(0, 0, m_width
, m_height
);
799 wxWindowCreateEvent
event(this);
800 GetEventHandler()->ProcessEvent(event
);
803 bool wxTopLevelWindowMac::MacEnableCompositing( bool useCompositing
)
805 bool oldval
= s_macWindowCompositing
;
806 s_macWindowCompositing
= useCompositing
;
810 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
812 ((Point
*)localOrigin
)->h
= 0;
813 ((Point
*)localOrigin
)->v
= 0;
814 ((Rect
*)clipRect
)->left
= 0;
815 ((Rect
*)clipRect
)->top
= 0;
816 ((Rect
*)clipRect
)->right
= m_width
;
817 ((Rect
*)clipRect
)->bottom
= m_height
;
818 *window
= m_macWindow
;
822 void wxTopLevelWindowMac::ClearBackground()
824 wxWindow::ClearBackground() ;
827 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
829 return m_macRootControl
;
833 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
835 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
837 RgnHandle visRgn
= NewRgn() ;
838 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), visRgn
);
839 BeginUpdate( (WindowRef
)m_macWindow
) ;
841 RgnHandle updateRgn
= NewRgn();
842 RgnHandle diffRgn
= NewRgn() ;
844 if ( updateRgn
&& diffRgn
)
847 // macos internal control redraws clean up areas we'd like to redraw ourselves
848 // therefore we pick the boundary rect and make sure we can redraw it
849 // this has to be intersected by the visRgn in order to avoid drawing over its own
851 RgnHandle trueUpdateRgn
= NewRgn() ;
852 Rect trueUpdateRgnBoundary
;
853 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), trueUpdateRgn
);
854 GetRegionBounds( trueUpdateRgn
, &trueUpdateRgnBoundary
) ;
855 RectRgn( updateRgn
, &trueUpdateRgnBoundary
) ;
856 SectRgn( updateRgn
, visRgn
, updateRgn
) ;
858 DisposeRgn( trueUpdateRgn
) ;
859 SetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
) ;
861 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
863 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
864 if ( !EmptyRgn( updateRgn
) )
866 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
870 DisposeRgn( updateRgn
);
872 DisposeRgn( diffRgn
);
874 DisposeRgn( visRgn
) ;
876 EndUpdate( (WindowRef
)m_macWindow
) ;
877 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
878 m_macNeedsErasing
= false ;
882 // Raise the window to the top of the Z order
883 void wxTopLevelWindowMac::Raise()
885 ::SelectWindow( (WindowRef
)m_macWindow
) ;
888 // Lower the window to the bottom of the Z order
889 void wxTopLevelWindowMac::Lower()
891 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
894 void wxTopLevelWindowMac::MacFireMouseEvent(
895 wxUint16 kind
, wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
897 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
898 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
899 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
901 event
.m_leftDown
= isDown
&& !controlDown
;
903 event
.m_middleDown
= FALSE
;
904 event
.m_rightDown
= isDown
&& controlDown
;
906 if ( kind
== mouseDown
)
909 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
911 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
913 else if ( kind
== mouseUp
)
916 event
.SetEventType(wxEVT_RIGHT_UP
) ;
918 event
.SetEventType(wxEVT_LEFT_UP
) ;
922 event
.SetEventType(wxEVT_MOTION
) ;
925 event
.m_shiftDown
= modifiers
& shiftKey
;
926 event
.m_controlDown
= modifiers
& controlKey
;
927 event
.m_altDown
= modifiers
& optionKey
;
928 event
.m_metaDown
= modifiers
& cmdKey
;
936 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
937 ::GlobalToLocal( &localwhere
) ;
940 if ( kind
== mouseDown
)
942 if ( timestamp
- gs_lastWhen
<= (long) GetDblTime() )
944 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
946 // This is not right if the second mouse down
947 // event occured in a differen window. We
948 // correct this in MacDispatchMouseEvent.
950 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
952 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
958 gs_lastWhen
= timestamp
;
960 gs_lastWhere
= localwhere
;
963 event
.m_x
= localwhere
.h
;
964 event
.m_y
= localwhere
.v
;
968 event
.m_timeStamp
= timestamp
;
969 event
.SetEventObject(this);
970 if ( wxTheApp
->s_captureWindow
)
974 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
977 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
978 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
980 if ( kind
== mouseUp
)
982 wxTheApp
->s_captureWindow
= NULL
;
985 m_cursor
.MacInstall() ;
991 MacDispatchMouseEvent( event
) ;
997 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
999 MacFireMouseEvent( mouseDown
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1000 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1003 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
1009 MacFireMouseEvent( mouseUp
, ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1010 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1016 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
1022 MacFireMouseEvent( nullEvent
/*moved*/ , ((EventRecord
*)ev
)->where
.h
, ((EventRecord
*)ev
)->where
.v
,
1023 ((EventRecord
*)ev
)->modifiers
, ((EventRecord
*)ev
)->when
) ;
1031 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1033 if(s_macDeactivateWindow
)
1035 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1036 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1040 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1042 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1044 if(s_macDeactivateWindow
==this)
1045 s_macDeactivateWindow
=NULL
;
1046 MacDelayedDeactivation(timestamp
);
1047 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
1048 event
.m_timeStamp
= timestamp
;
1049 event
.SetEventObject(this);
1051 GetEventHandler()->ProcessEvent(event
);
1053 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
1055 // Early versions of MacOS X don't refresh backgrounds properly,
1056 // so refresh the whole window on activation and deactivation.
1057 long osVersion
= UMAGetSystemVersion();
1058 if (osVersion
>= 0x1000 && osVersion
< 0x1020 )
1064 // for the moment we have to resolve some redrawing issues like this
1065 // the OS is stealing some redrawing areas as soon as it draws a control
1072 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
1078 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1080 wxWindow::SetTitle( title
) ;
1081 UMASetWTitle( (WindowRef
)m_macWindow
, title
) ;
1084 bool wxTopLevelWindowMac::Show(bool show
)
1086 if ( !wxWindow::Show(show
) )
1091 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1092 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1094 ::ShowWindow( (WindowRef
)m_macWindow
);
1099 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1101 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1102 // no need to generate events here, they will get them triggered by macos
1103 // actually they should be , but apparently they are not
1104 wxSize
size(m_width
, m_height
);
1105 wxSizeEvent
event(size
, m_windowId
);
1106 event
.SetEventObject(this);
1107 GetEventHandler()->ProcessEvent(event
);
1111 #if wxUSE_SYSTEM_OPTIONS
1112 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1114 ::HideWindow((WindowRef
) m_macWindow
);
1119 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1134 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1136 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
1137 wxMacWindowClipper
clip (this);
1139 int former_x
= m_x
;
1140 int former_y
= m_y
;
1141 int former_w
= m_width
;
1142 int former_h
= m_height
;
1144 int actualWidth
= width
;
1145 int actualHeight
= height
;
1149 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
1150 actualWidth
= m_minWidth
;
1151 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
1152 actualHeight
= m_minHeight
;
1153 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
1154 actualWidth
= m_maxWidth
;
1155 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
1156 actualHeight
= m_maxHeight
;
1158 bool doMove
= false ;
1159 bool doResize
= false ;
1161 if ( actualX
!= former_x
|| actualY
!= former_y
)
1165 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
1170 if ( doMove
|| doResize
)
1176 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
1178 m_width
= actualWidth
;
1179 m_height
= actualHeight
;
1182 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
1184 // the OS takes care of invalidating and erasing the new area so we only have to
1185 // take care of refreshing for full repaints
1187 if ( doResize
&& HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
1191 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
1193 wxFrame
* frame
= (wxFrame
*) this ;
1195 frame
->PositionStatusBar();
1198 frame
->PositionToolBar();
1202 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1204 MacRepositionScrollBars() ;
1207 wxPoint
point(m_x
, m_y
);
1208 wxMoveEvent
event(point
, m_windowId
);
1209 event
.SetEventObject(this);
1210 GetEventHandler()->ProcessEvent(event
) ;
1214 MacRepositionScrollBars() ;
1215 wxSize
size(m_width
, m_height
);
1216 wxSizeEvent
event(size
, m_windowId
);
1217 event
.SetEventObject(this);
1218 GetEventHandler()->ProcessEvent(event
);
1225 * Invalidation Mechanism
1227 * The update mechanism reflects exactely the windows mechanism
1228 * the rect gets added to the window invalidate region, if the eraseBackground flag
1229 * has been true for any part of the update rgn the background is erased in the entire region
1230 * not just in the specified rect.
1232 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1233 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1234 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1235 * will get the eraseBackground event first
1238 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
1240 GrafPtr formerPort
;
1241 GetPort( &formerPort
) ;
1242 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
1244 m_macNeedsErasing
|= eraseBackground
;
1246 // if we already know that we will have to erase, there's no need to track the rest
1247 if ( !m_macNeedsErasing
)
1249 // we end only here if eraseBackground is false
1250 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1251 // we will have to erase anyway
1253 RgnHandle updateRgn
= NewRgn();
1254 RgnHandle diffRgn
= NewRgn() ;
1255 if ( updateRgn
&& diffRgn
)
1257 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
1259 LocalToGlobal( &pt
) ;
1260 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
1261 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
1262 if ( !EmptyRgn( diffRgn
) )
1264 m_macNeedsErasing
= true ;
1268 DisposeRgn( updateRgn
);
1270 DisposeRgn( diffRgn
);
1272 if ( !m_macNeedsErasing
)
1274 RgnHandle rectRgn
= NewRgn() ;
1275 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
1276 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
1277 DisposeRgn( rectRgn
) ;
1280 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
1281 // turn this on to debug the refreshing cycle
1282 #if wxMAC_DEBUG_REDRAW
1285 SetPort( formerPort
) ;
1289 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1291 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1292 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1295 // The empty region signifies that the shape should be removed from the
1297 if ( region
.IsEmpty() )
1299 wxSize sz
= GetClientSize();
1300 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1301 return SetShape(rgn
);
1304 // Make a copy of the region
1305 RgnHandle shapeRegion
= NewRgn();
1306 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1308 // Dispose of any shape region we may already have
1309 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1313 // Save the region so we can use it later
1314 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1316 // Tell the window manager that the window has changed shape
1317 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1324 // ---------------------------------------------------------------------------
1325 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1326 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1327 // ---------------------------------------------------------------------------
1331 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1333 GetWindowPortBounds(window
, inRect
);
1334 Point pt
= {inRect
->left
, inRect
->top
};
1335 SetPort((GrafPtr
) GetWindowPort(window
));
1338 inRect
->left
= pt
.h
;
1339 inRect
->bottom
+= pt
.v
;
1340 inRect
->right
+= pt
.h
;
1344 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1346 /*------------------------------------------------------
1347 Define which options your custom window supports.
1348 --------------------------------------------------------*/
1349 //just enable everything for our demo
1350 *(OptionBits
*)param
=//kWindowCanGrow|
1352 //kWindowCanCollapse|
1353 //kWindowCanGetWindowRegion|
1354 //kWindowHasTitleBar|
1355 //kWindowSupportsDragHilite|
1356 kWindowCanDrawInCurrentPort
|
1357 //kWindowCanMeasureTitle|
1358 kWindowWantsDisposeAtProcessDeath
|
1359 kWindowSupportsSetGrowImageRegion
|
1360 kWindowDefSupportsColorGrafPort
;
1364 // The content region is left as a rectangle matching the window size, this is
1365 // so the origin in the paint event, and etc. still matches what the
1366 // programmer expects.
1367 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1370 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1373 wxRect r
= win
->GetRect();
1374 SetRectRgn(rgn
, r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
1378 // The structure region is set to the shape given to the SetShape method.
1379 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1381 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1387 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1388 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1389 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1390 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1396 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1398 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1400 switch(rgnRec
->regionCode
)
1402 case kWindowStructureRgn
:
1403 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1405 case kWindowContentRgn
:
1406 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1409 SetEmptyRgn(rgnRec
->winRgn
);
1416 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1418 /*------------------------------------------------------
1419 Determine the region of the window which was hit
1420 --------------------------------------------------------*/
1422 static RgnHandle tempRgn
=nil
;
1427 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1429 //Mac OS 8.5 or later
1430 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1431 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1434 return wNoHit
;//no significant area was hit.
1438 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1442 case kWindowMsgHitTest
:
1443 return wxShapedMacWindowHitTest(window
,param
);
1445 case kWindowMsgGetFeatures
:
1446 return wxShapedMacWindowGetFeatures(window
,param
);
1448 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1449 case kWindowMsgGetRegion
:
1450 return wxShapedMacWindowGetRegion(window
,param
);
1457 // ---------------------------------------------------------------------------