1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
35 #include "wx/string.h"
40 #include "wx/mac/uma.h"
41 #include "wx/mac/aga.h"
43 #include "wx/tooltip.h"
46 #define wxMAC_DEBUG_REDRAW 0
47 #ifndef wxMAC_DEBUG_REDRAW
48 #define wxMAC_DEBUG_REDRAW 0
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // list of all frames and modeless dialogs
56 wxWindowList wxModelessWindows
;
58 // double click testing
59 static Point gs_lastWhere
;
60 static long gs_lastWhen
= 0;
62 // ============================================================================
63 // wxTopLevelWindowMac implementation
64 // ============================================================================
66 // ---------------------------------------------------------------------------
67 // wxWindowMac utility functions
68 // ---------------------------------------------------------------------------
70 // Find an item given the Macintosh Window Reference
72 wxList
*wxWinMacWindowList
= NULL
;
73 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
75 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
78 return (wxTopLevelWindowMac
*)node
->GetData();
81 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
83 // adding NULL WindowRef is (first) surely a result of an error and
84 // (secondly) breaks menu command processing
85 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, "attempt to add a NULL WindowRef to window list" );
87 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
88 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
91 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
93 wxWinMacWindowList
->DeleteObject(win
);
97 // ----------------------------------------------------------------------------
98 // wxTopLevelWindowMac creation
99 // ----------------------------------------------------------------------------
101 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
103 void wxTopLevelWindowMac::Init()
106 m_maximizeOnShow
= FALSE
;
107 m_macNoEraseUpdateRgn
= NewRgn() ;
108 m_macNeedsErasing
= false ;
110 m_macEventHandler
= NULL
;
113 class wxMacDeferredWindowDeleter
: public wxObject
116 wxMacDeferredWindowDeleter( WindowRef windowRef
)
118 m_macWindow
= windowRef
;
120 virtual ~wxMacDeferredWindowDeleter()
122 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
125 WindowRef m_macWindow
;
128 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
130 const wxString
& title
,
134 const wxString
& name
)
139 m_windowStyle
= style
;
143 m_windowId
= id
== -1 ? NewControlId() : id
;
145 wxTopLevelWindows
.Append(this);
148 parent
->AddChild(this);
153 wxTopLevelWindowMac::~wxTopLevelWindowMac()
157 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
158 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
162 if ( m_macEventHandler
)
164 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
165 m_macEventHandler
= NULL
;
168 wxRemoveMacWindowAssociation( this ) ;
170 if ( wxModelessWindows
.Find(this) )
171 wxModelessWindows
.DeleteObject(this);
173 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
177 // ----------------------------------------------------------------------------
178 // wxTopLevelWindowMac maximize/minimize
179 // ----------------------------------------------------------------------------
181 void wxTopLevelWindowMac::Maximize(bool maximize
)
183 // not available on mac
186 bool wxTopLevelWindowMac::IsMaximized() const
191 void wxTopLevelWindowMac::Iconize(bool iconize
)
193 // not available on mac
196 bool wxTopLevelWindowMac::IsIconized() const
198 // mac dialogs cannot be iconized
202 void wxTopLevelWindowMac::Restore()
204 // not available on mac
207 // ----------------------------------------------------------------------------
208 // wxTopLevelWindowMac misc
209 // ----------------------------------------------------------------------------
211 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
214 wxTopLevelWindowBase::SetIcon(icon
);
219 EventHandlerUPP wxMacWindowEventHandlerUPP
= NULL
;
221 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
223 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
225 OSStatus result
= eventNotHandledErr
;
227 switch ( GetEventClass( event
) )
229 case kEventClassTextInput
:
230 if ( wxMacConvertEventToRecord( event
, &rec
) )
232 wxTheApp
->m_macCurrentEvent
= &rec
;
233 wxWindow
* focus
= wxWindow::FindFocus() ;
234 if ( (focus
!= NULL
) && !UMAMenuEvent(&rec
) && wxTheApp
->MacSendKeyDownEvent( focus
, rec
.message
, rec
.modifiers
, rec
.when
, rec
.where
.h
, rec
.where
.v
) )
236 // was handled internally
249 void wxTopLevelWindowMac::MacInstallEventHandler()
252 if ( wxMacWindowEventHandlerUPP
== NULL
)
254 wxMacWindowEventHandlerUPP
= NewEventHandlerUPP( wxMacWindowEventHandler
) ;
257 static const EventTypeSpec eventList
[] =
259 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
}
261 if ( m_macEventHandler
)
263 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
264 m_macEventHandler
= NULL
;
266 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), wxMacWindowEventHandlerUPP
, WXSIZEOF(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
270 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
274 const wxString
& name
)
277 m_windowStyle
= style
;
298 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
300 // translate the window attributes in the appropriate window class and attributes
302 WindowClass wclass
= 0;
303 WindowAttributes attr
= kWindowNoAttributes
;
305 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
308 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
309 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
310 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
313 wclass
= kFloatingWindowClass
;
314 if ( HasFlag(wxTINY_CAPTION_VERT
) )
316 attr
|= kWindowSideTitlebarAttribute
;
322 wclass
= kPlainWindowClass
;
324 wclass
= kFloatingWindowClass
;
328 else if ( HasFlag( wxCAPTION
) )
330 if ( HasFlag( wxDIALOG_MODAL
) )
332 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
336 wclass
= kDocumentWindowClass
;
341 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
342 HasFlag( wxSYSTEM_MENU
) )
344 wclass
= kDocumentWindowClass
;
349 wclass
= kPlainWindowClass
;
351 wclass
= kModalWindowClass
;
356 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
358 attr
|= kWindowFullZoomAttribute
;
359 attr
|= kWindowCollapseBoxAttribute
;
361 if ( HasFlag( wxRESIZE_BORDER
) )
363 attr
|= kWindowResizableAttribute
;
365 if ( HasFlag( wxSYSTEM_MENU
) )
367 attr
|= kWindowCloseBoxAttribute
;
370 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
371 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
373 if( wxApp::s_macDefaultEncodingIsPC
)
374 label
= wxMacMakeMacStringFromPC( title
) ;
377 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
378 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
379 MacInstallEventHandler() ;
384 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
386 ((Point
*)localOrigin
)->h
= 0;
387 ((Point
*)localOrigin
)->v
= 0;
388 ((Rect
*)clipRect
)->left
= 0;
389 ((Rect
*)clipRect
)->top
= 0;
390 ((Rect
*)clipRect
)->right
= m_width
;
391 ((Rect
*)clipRect
)->bottom
= m_height
;
392 *window
= m_macWindow
;
396 void wxTopLevelWindowMac::Clear()
401 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
403 return m_macRootControl
;
407 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
410 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
412 BeginUpdate( (WindowRef
)m_macWindow
) ;
414 RgnHandle updateRgn
= NewRgn();
415 RgnHandle diffRgn
= NewRgn() ;
416 if ( updateRgn
&& diffRgn
)
418 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
419 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
420 if ( !EmptyRgn( updateRgn
) )
422 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
426 DisposeRgn( updateRgn
);
428 DisposeRgn( diffRgn
);
429 EndUpdate( (WindowRef
)m_macWindow
) ;
430 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
431 m_macNeedsErasing
= false ;
435 // Raise the window to the top of the Z order
436 void wxTopLevelWindowMac::Raise()
438 ::BringToFront( (WindowRef
)m_macWindow
) ;
441 // Lower the window to the bottom of the Z order
442 void wxTopLevelWindowMac::Lower()
444 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
447 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
449 EventRecord
*ev
= (EventRecord
*) evr
;
450 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
451 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
452 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
454 event
.m_leftDown
= isDown
&& !controlDown
;
456 event
.m_middleDown
= FALSE
;
457 event
.m_rightDown
= isDown
&& controlDown
;
459 if ( ev
->what
== mouseDown
)
462 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
464 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
466 else if ( ev
->what
== mouseUp
)
469 event
.SetEventType(wxEVT_RIGHT_UP
) ;
471 event
.SetEventType(wxEVT_LEFT_UP
) ;
475 event
.SetEventType(wxEVT_MOTION
) ;
478 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
479 event
.m_controlDown
= ev
->modifiers
& controlKey
;
480 event
.m_altDown
= ev
->modifiers
& optionKey
;
481 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
483 Point localwhere
= ev
->where
;
487 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
488 ::GlobalToLocal( &localwhere
) ;
491 if ( ev
->what
== mouseDown
)
493 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
495 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
497 // This is not right if the second mouse down
498 // event occured in a differen window. We
499 // correct this in MacDispatchMouseEvent.
501 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
503 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
509 gs_lastWhen
= ev
->when
;
511 gs_lastWhere
= localwhere
;
514 event
.m_x
= localwhere
.h
;
515 event
.m_y
= localwhere
.v
;
519 event
.m_timeStamp
= ev
->when
;
520 event
.SetEventObject(this);
521 if ( wxTheApp
->s_captureWindow
)
525 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
528 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
529 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
531 if ( ev
->what
== mouseUp
)
533 wxTheApp
->s_captureWindow
= NULL
;
536 m_cursor
.MacInstall() ;
542 MacDispatchMouseEvent( event
) ;
546 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
548 MacFireMouseEvent( ev
) ;
551 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
557 MacFireMouseEvent( ev
) ;
563 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
569 MacFireMouseEvent( ev
) ;
574 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
576 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
577 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
578 event
.SetEventObject(this);
580 GetEventHandler()->ProcessEvent(event
);
582 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
584 // Early versions of MacOS X don't refresh backgrounds properly,
585 // so refresh the whole window on activation and deactivation.
586 long osVersion
= UMAGetSystemVersion();
587 if (osVersion
>= 0x1000 && osVersion
< 0x1020)
590 MacSuperEnabled( inIsActivating
) ;
593 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
597 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
599 wxWindow::SetTitle( title
) ;
603 if( wxApp::s_macDefaultEncodingIsPC
)
604 label
= wxMacMakeMacStringFromPC( m_label
) ;
608 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
611 bool wxTopLevelWindowMac::Show(bool show
)
613 if ( !wxWindow::Show(show
) )
618 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
619 ::SelectWindow( (WindowRef
)m_macWindow
) ;
620 // no need to generate events here, they will get them triggered by macos
621 // actually they should be , but apparently they are not
622 wxSize
size(m_width
, m_height
);
623 wxSizeEvent
event(size
, m_windowId
);
624 event
.SetEventObject(this);
625 GetEventHandler()->ProcessEvent(event
);
629 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
643 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
647 int former_w
= m_width
;
648 int former_h
= m_height
;
650 int actualWidth
= width
;
651 int actualHeight
= height
;
655 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
656 actualWidth
= m_minWidth
;
657 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
658 actualHeight
= m_minHeight
;
659 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
660 actualWidth
= m_maxWidth
;
661 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
662 actualHeight
= m_maxHeight
;
664 bool doMove
= false ;
665 bool doResize
= false ;
667 if ( actualX
!= former_x
|| actualY
!= former_y
)
671 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
676 if ( doMove
|| doResize
)
680 m_width
= actualWidth
;
681 m_height
= actualHeight
;
684 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
687 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
689 // the OS takes care of invalidating and erasing the new area so we only have to
690 // take care of refreshing for full repaints
692 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
696 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
698 wxFrame
* frame
= (wxFrame
*) this ;
699 frame
->PositionStatusBar();
700 frame
->PositionToolBar();
703 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
705 MacRepositionScrollBars() ;
708 wxPoint
point(m_x
, m_y
);
709 wxMoveEvent
event(point
, m_windowId
);
710 event
.SetEventObject(this);
711 GetEventHandler()->ProcessEvent(event
) ;
715 MacRepositionScrollBars() ;
716 wxSize
size(m_width
, m_height
);
717 wxSizeEvent
event(size
, m_windowId
);
718 event
.SetEventObject(this);
719 GetEventHandler()->ProcessEvent(event
);
726 * Invalidation Mechanism
728 * The update mechanism reflects exactely the windows mechanism
729 * the rect gets added to the window invalidate region, if the eraseBackground flag
730 * has been true for any part of the update rgn the background is erased in the entire region
731 * not just in the specified rect.
733 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
734 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
735 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
736 * will get the eraseBackground event first
739 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
742 GetPort( &formerPort
) ;
743 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
745 m_macNeedsErasing
|= eraseBackground
;
747 // if we already know that we will have to erase, there's no need to track the rest
748 if ( !m_macNeedsErasing
)
750 // we end only here if eraseBackground is false
751 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
752 // we will have to erase anyway
754 RgnHandle updateRgn
= NewRgn();
755 RgnHandle diffRgn
= NewRgn() ;
756 if ( updateRgn
&& diffRgn
)
758 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
760 LocalToGlobal( &pt
) ;
761 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
762 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
763 if ( !EmptyRgn( diffRgn
) )
765 m_macNeedsErasing
= true ;
769 DisposeRgn( updateRgn
);
771 DisposeRgn( diffRgn
);
773 if ( !m_macNeedsErasing
)
775 RgnHandle rectRgn
= NewRgn() ;
776 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
777 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
778 DisposeRgn( rectRgn
) ;
781 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
782 // turn this on to debug the refreshing cycle
783 #if wxMAC_DEBUG_REDRAW
786 SetPort( formerPort
) ;