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 MacSuperEnabled( inIsActivating
) ;
587 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
591 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
593 wxWindow::SetTitle( title
) ;
597 if( wxApp::s_macDefaultEncodingIsPC
)
598 label
= wxMacMakeMacStringFromPC( m_label
) ;
602 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
605 bool wxTopLevelWindowMac::Show(bool show
)
607 if ( !wxWindow::Show(show
) )
612 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
613 ::SelectWindow( (WindowRef
)m_macWindow
) ;
614 // no need to generate events here, they will get them triggered by macos
615 // actually they should be , but apparently they are not
616 wxSize
size(m_width
, m_height
);
617 wxSizeEvent
event(size
, m_windowId
);
618 event
.SetEventObject(this);
619 GetEventHandler()->ProcessEvent(event
);
623 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
637 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
641 int former_w
= m_width
;
642 int former_h
= m_height
;
644 int actualWidth
= width
;
645 int actualHeight
= height
;
649 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
650 actualWidth
= m_minWidth
;
651 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
652 actualHeight
= m_minHeight
;
653 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
654 actualWidth
= m_maxWidth
;
655 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
656 actualHeight
= m_maxHeight
;
658 bool doMove
= false ;
659 bool doResize
= false ;
661 if ( actualX
!= former_x
|| actualY
!= former_y
)
665 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
670 if ( doMove
|| doResize
)
674 m_width
= actualWidth
;
675 m_height
= actualHeight
;
678 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
681 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
683 // the OS takes care of invalidating and erasing the new area so we only have to
684 // take care of refreshing for full repaints
686 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
690 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
692 wxFrame
* frame
= (wxFrame
*) this ;
693 frame
->PositionStatusBar();
694 frame
->PositionToolBar();
697 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
699 MacRepositionScrollBars() ;
702 wxPoint
point(m_x
, m_y
);
703 wxMoveEvent
event(point
, m_windowId
);
704 event
.SetEventObject(this);
705 GetEventHandler()->ProcessEvent(event
) ;
709 MacRepositionScrollBars() ;
710 wxSize
size(m_width
, m_height
);
711 wxSizeEvent
event(size
, m_windowId
);
712 event
.SetEventObject(this);
713 GetEventHandler()->ProcessEvent(event
);
720 * Invalidation Mechanism
722 * The update mechanism reflects exactely the windows mechanism
723 * the rect gets added to the window invalidate region, if the eraseBackground flag
724 * has been true for any part of the update rgn the background is erased in the entire region
725 * not just in the specified rect.
727 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
728 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
729 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
730 * will get the eraseBackground event first
733 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
736 GetPort( &formerPort
) ;
737 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
739 m_macNeedsErasing
|= eraseBackground
;
741 // if we already know that we will have to erase, there's no need to track the rest
742 if ( !m_macNeedsErasing
)
744 // we end only here if eraseBackground is false
745 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
746 // we will have to erase anyway
748 RgnHandle updateRgn
= NewRgn();
749 RgnHandle diffRgn
= NewRgn() ;
750 if ( updateRgn
&& diffRgn
)
752 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
754 LocalToGlobal( &pt
) ;
755 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
756 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
757 if ( !EmptyRgn( diffRgn
) )
759 m_macNeedsErasing
= true ;
763 DisposeRgn( updateRgn
);
765 DisposeRgn( diffRgn
);
767 if ( !m_macNeedsErasing
)
769 RgnHandle rectRgn
= NewRgn() ;
770 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
771 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
772 DisposeRgn( rectRgn
) ;
775 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
776 // turn this on to debug the refreshing cycle
777 #if wxMAC_DEBUG_REDRAW
780 SetPort( formerPort
) ;