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"
42 #include "wx/tooltip.h"
45 #define wxMAC_DEBUG_REDRAW 0
46 #ifndef wxMAC_DEBUG_REDRAW
47 #define wxMAC_DEBUG_REDRAW 0
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // list of all frames and modeless dialogs
55 wxWindowList wxModelessWindows
;
57 // ============================================================================
58 // wxTopLevelWindowMac implementation
59 // ============================================================================
61 // ---------------------------------------------------------------------------
62 // wxWindowMac utility functions
63 // ---------------------------------------------------------------------------
65 // Find an item given the Macintosh Window Reference
67 wxList
*wxWinMacWindowList
= NULL
;
68 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
70 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
73 return (wxTopLevelWindowMac
*)node
->Data();
76 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
78 // adding NULL WindowRef is (first) surely a result of an error and
79 // (secondly) breaks menu command processing
80 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, "attempt to add a NULL WindowRef to window list" );
82 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
83 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
86 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
88 wxWinMacWindowList
->DeleteObject(win
);
92 // ----------------------------------------------------------------------------
93 // wxTopLevelWindowMac creation
94 // ----------------------------------------------------------------------------
96 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
98 void wxTopLevelWindowMac::Init()
101 m_maximizeOnShow
= FALSE
;
102 m_macNoEraseUpdateRgn
= NewRgn() ;
103 m_macNeedsErasing
= false ;
107 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
109 const wxString
& title
,
113 const wxString
& name
)
118 m_windowStyle
= style
;
122 m_windowId
= id
== -1 ? NewControlId() : id
;
124 wxTopLevelWindows
.Append(this);
127 parent
->AddChild(this);
132 wxTopLevelWindowMac::~wxTopLevelWindowMac()
136 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
137 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
140 wxRemoveMacWindowAssociation( this ) ;
142 wxTopLevelWindows
.DeleteObject(this);
144 if ( wxModelessWindows
.Find(this) )
145 wxModelessWindows
.DeleteObject(this);
147 // If this is the last top-level window, exit.
148 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
150 wxTheApp
->SetTopWindow(NULL
);
152 if ( wxTheApp
->GetExitOnFrameDelete() )
154 wxTheApp
->ExitMainLoop() ;
157 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
161 // ----------------------------------------------------------------------------
162 // wxTopLevelWindowMac maximize/minimize
163 // ----------------------------------------------------------------------------
165 void wxTopLevelWindowMac::Maximize(bool maximize
)
167 // not available on mac
170 bool wxTopLevelWindowMac::IsMaximized() const
175 void wxTopLevelWindowMac::Iconize(bool iconize
)
177 // not available on mac
180 bool wxTopLevelWindowMac::IsIconized() const
182 // mac dialogs cannot be iconized
186 void wxTopLevelWindowMac::Restore()
188 // not available on mac
191 // ----------------------------------------------------------------------------
192 // wxTopLevelWindowMac misc
193 // ----------------------------------------------------------------------------
195 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
198 wxTopLevelWindowBase::SetIcon(icon
);
201 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
205 const wxString
& name
)
208 m_windowStyle
= style
;
229 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
231 // translate the window attributes in the appropriate window class and attributes
233 WindowClass wclass
= 0;
234 WindowAttributes attr
= kWindowNoAttributes
;
236 if ( HasFlag( wxFRAME_TOOL_WINDOW
) || HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
) )
238 wclass
= kFloatingWindowClass
;
239 if ( HasFlag(wxTINY_CAPTION_VERT
) )
241 attr
|= kWindowSideTitlebarAttribute
;
244 else if ( HasFlag( wxCAPTION
) )
246 if ( HasFlag( wxDIALOG_MODAL
) )
248 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
252 wclass
= kDocumentWindowClass
;
257 wclass
= kModalWindowClass
;
260 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
262 attr
|= kWindowFullZoomAttribute
;
263 attr
|= kWindowCollapseBoxAttribute
;
265 if ( HasFlag( wxRESIZE_BORDER
) )
267 attr
|= kWindowResizableAttribute
;
269 if ( HasFlag( wxSYSTEM_MENU
) )
271 attr
|= kWindowCloseBoxAttribute
;
274 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
275 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
277 if( wxApp::s_macDefaultEncodingIsPC
)
278 label
= wxMacMakeMacStringFromPC( title
) ;
281 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
282 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
287 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
289 ((Point
*)localOrigin
)->h
= 0;
290 ((Point
*)localOrigin
)->v
= 0;
291 ((Rect
*)clipRect
)->left
= 0;
292 ((Rect
*)clipRect
)->top
= 0;
293 ((Rect
*)clipRect
)->right
= m_width
;
294 ((Rect
*)clipRect
)->bottom
= m_height
;
295 *window
= m_macWindow
;
299 void wxTopLevelWindowMac::Clear()
304 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
306 return m_macRootControl
;
310 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
313 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
315 BeginUpdate( (WindowRef
)m_macWindow
) ;
317 RgnHandle updateRgn
= NewRgn();
318 RgnHandle diffRgn
= NewRgn() ;
319 if ( updateRgn
&& diffRgn
)
321 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
322 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
323 if ( !EmptyRgn( updateRgn
) )
325 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
329 DisposeRgn( updateRgn
);
331 DisposeRgn( diffRgn
);
332 EndUpdate( (WindowRef
)m_macWindow
) ;
333 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
334 m_macNeedsErasing
= false ;
338 // Raise the window to the top of the Z order
339 void wxTopLevelWindowMac::Raise()
341 ::BringToFront( (WindowRef
)m_macWindow
) ;
344 // Lower the window to the bottom of the Z order
345 void wxTopLevelWindowMac::Lower()
347 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
352 extern int wxBusyCursorCount
;
354 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
356 EventRecord
*ev
= (EventRecord
*) evr
;
357 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
358 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
359 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
361 event
.m_leftDown
= isDown
&& !controlDown
;
363 event
.m_middleDown
= FALSE
;
364 event
.m_rightDown
= isDown
&& controlDown
;
366 if ( ev
->what
== mouseDown
)
369 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
371 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
373 else if ( ev
->what
== mouseUp
)
376 event
.SetEventType(wxEVT_RIGHT_UP
) ;
378 event
.SetEventType(wxEVT_LEFT_UP
) ;
382 event
.SetEventType(wxEVT_MOTION
) ;
385 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
386 event
.m_controlDown
= ev
->modifiers
& controlKey
;
387 event
.m_altDown
= ev
->modifiers
& optionKey
;
388 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
390 Point localwhere
= ev
->where
;
394 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
395 ::GlobalToLocal( &localwhere
) ;
398 if ( ev
->what
== mouseDown
)
400 if ( ev
->when
- lastWhen
<= GetDblTime() )
402 if ( abs( localwhere
.h
- lastWhere
.h
) < 3 || abs( localwhere
.v
- lastWhere
.v
) < 3 )
405 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
407 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
413 lastWhen
= ev
->when
;
415 lastWhere
= localwhere
;
418 event
.m_x
= localwhere
.h
;
419 event
.m_y
= localwhere
.v
;
423 event
.m_timeStamp
= ev
->when
;
424 event
.SetEventObject(this);
425 if ( wxTheApp
->s_captureWindow
)
429 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
432 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
433 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
435 if ( ev
->what
== mouseUp
)
437 wxTheApp
->s_captureWindow
= NULL
;
438 if ( wxBusyCursorCount
== 0 )
440 m_cursor
.MacInstall() ;
446 MacDispatchMouseEvent( event
) ;
450 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
452 MacFireMouseEvent( ev
) ;
455 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
461 MacFireMouseEvent( ev
) ;
467 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
473 MacFireMouseEvent( ev
) ;
478 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
480 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
481 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
482 event
.SetEventObject(this);
484 GetEventHandler()->ProcessEvent(event
);
486 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
488 MacSuperEnabled( inIsActivating
) ;
491 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
495 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
497 wxWindow::SetTitle( title
) ;
501 if( wxApp::s_macDefaultEncodingIsPC
)
502 label
= wxMacMakeMacStringFromPC( m_label
) ;
506 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
509 bool wxTopLevelWindowMac::Show(bool show
)
511 if ( !wxWindow::Show(show
) )
516 ::ShowWindow( (WindowRef
)m_macWindow
) ;
517 ::SelectWindow( (WindowRef
)m_macWindow
) ;
518 // no need to generate events here, they will get them triggered by macos
519 // actually they should be , but apparently they are not
520 wxSize
size(m_width
, m_height
);
521 wxSizeEvent
event(size
, m_windowId
);
522 event
.SetEventObject(this);
523 GetEventHandler()->ProcessEvent(event
);
527 ::HideWindow( (WindowRef
)m_macWindow
) ;
541 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
545 int former_w
= m_width
;
546 int former_h
= m_height
;
548 int actualWidth
= width
;
549 int actualHeight
= height
;
553 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
554 actualWidth
= m_minWidth
;
555 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
556 actualHeight
= m_minHeight
;
557 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
558 actualWidth
= m_maxWidth
;
559 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
560 actualHeight
= m_maxHeight
;
562 bool doMove
= false ;
563 bool doResize
= false ;
565 if ( actualX
!= former_x
|| actualY
!= former_y
)
569 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
574 if ( doMove
|| doResize
)
578 m_width
= actualWidth
;
579 m_height
= actualHeight
;
582 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
585 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
587 // the OS takes care of invalidating and erasing the new area
588 // we have erased the old one
590 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
592 wxFrame
* frame
= (wxFrame
*) this ;
593 frame
->PositionStatusBar();
594 frame
->PositionToolBar();
597 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
599 MacRepositionScrollBars() ;
602 wxPoint
point(m_x
, m_y
);
603 wxMoveEvent
event(point
, m_windowId
);
604 event
.SetEventObject(this);
605 GetEventHandler()->ProcessEvent(event
) ;
609 MacRepositionScrollBars() ;
610 wxSize
size(m_width
, m_height
);
611 wxSizeEvent
event(size
, m_windowId
);
612 event
.SetEventObject(this);
613 GetEventHandler()->ProcessEvent(event
);
620 * Invalidation Mechanism
622 * The update mechanism reflects exactely the windows mechanism
623 * the rect gets added to the window invalidate region, if the eraseBackground flag
624 * has been true for any part of the update rgn the background is erased in the entire region
625 * not just in the specified rect.
627 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
628 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
629 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
630 * will get the eraseBackground event first
633 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
636 GetPort( &formerPort
) ;
637 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
639 m_macNeedsErasing
|= eraseBackground
;
641 // if we already know that we will have to erase, there's no need to track the rest
642 if ( !m_macNeedsErasing
)
644 // we end only here if eraseBackground is false
645 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
646 // we will have to erase anyway
648 RgnHandle updateRgn
= NewRgn();
649 RgnHandle diffRgn
= NewRgn() ;
650 if ( updateRgn
&& diffRgn
)
652 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
654 LocalToGlobal( &pt
) ;
655 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
656 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
657 if ( !EmptyRgn( diffRgn
) )
659 m_macNeedsErasing
= true ;
663 DisposeRgn( updateRgn
);
665 DisposeRgn( diffRgn
);
667 if ( !m_macNeedsErasing
)
669 RgnHandle rectRgn
= NewRgn() ;
670 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
671 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
672 DisposeRgn( rectRgn
) ;
675 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
676 // turn this on to debug the refreshing cycle
677 #if wxMAC_DEBUG_REDRAW
680 SetPort( formerPort
) ;