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"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // list of all frames and modeless dialogs
49 wxWindowList wxModelessWindows
;
51 // ============================================================================
52 // wxTopLevelWindowMac implementation
53 // ============================================================================
55 // ---------------------------------------------------------------------------
56 // wxWindowMac utility functions
57 // ---------------------------------------------------------------------------
59 // Find an item given the Macintosh Window Reference
61 wxList
*wxWinMacWindowList
= NULL
;
62 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
64 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
67 return (wxTopLevelWindowMac
*)node
->Data();
70 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
72 // adding NULL WindowRef is (first) surely a result of an error and
73 // (secondly) breaks menu command processing
74 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, "attempt to add a NULL WindowRef to window list" );
76 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
77 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
80 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
82 wxWinMacWindowList
->DeleteObject(win
);
86 // ----------------------------------------------------------------------------
87 // wxTopLevelWindowMac creation
88 // ----------------------------------------------------------------------------
90 WindowRef
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
92 void wxTopLevelWindowMac::Init()
95 m_maximizeOnShow
= FALSE
;
96 m_macNoEraseUpdateRgn
= NewRgn() ;
97 m_macNeedsErasing
= false ;
100 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
102 const wxString
& title
,
106 const wxString
& name
)
111 m_windowStyle
= style
;
115 m_windowId
= id
== -1 ? NewControlId() : id
;
117 wxTopLevelWindows
.Append(this);
120 parent
->AddChild(this);
125 wxTopLevelWindowMac::~wxTopLevelWindowMac()
127 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
128 UMADisposeWindow( m_macWindow
) ;
130 wxRemoveMacWindowAssociation( this ) ;
132 wxTopLevelWindows
.DeleteObject(this);
134 if ( wxModelessWindows
.Find(this) )
135 wxModelessWindows
.DeleteObject(this);
137 // If this is the last top-level window, exit.
138 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
140 wxTheApp
->SetTopWindow(NULL
);
142 if ( wxTheApp
->GetExitOnFrameDelete() )
144 wxTheApp
->ExitMainLoop() ;
147 DisposeRgn( m_macNoEraseUpdateRgn
) ;
151 // ----------------------------------------------------------------------------
152 // wxTopLevelWindowMac maximize/minimize
153 // ----------------------------------------------------------------------------
155 void wxTopLevelWindowMac::Maximize(bool maximize
)
157 // not available on mac
160 bool wxTopLevelWindowMac::IsMaximized() const
165 void wxTopLevelWindowMac::Iconize(bool iconize
)
167 // not available on mac
170 bool wxTopLevelWindowMac::IsIconized() const
172 // mac dialogs cannot be iconized
176 void wxTopLevelWindowMac::Restore()
178 // not available on mac
181 // ----------------------------------------------------------------------------
182 // wxTopLevelWindowMac misc
183 // ----------------------------------------------------------------------------
185 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
188 wxTopLevelWindowBase::SetIcon(icon
);
191 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
195 const wxString
& name
)
198 m_windowStyle
= style
;
219 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
221 // translate the window attributes in the appropriate window class and attributes
223 WindowClass wclass
= 0;
224 WindowAttributes attr
= kWindowNoAttributes
;
226 if ( HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
) )
228 wclass
= kFloatingWindowClass
;
229 if ( HasFlag(wxTINY_CAPTION_VERT
) )
231 attr
|= kWindowSideTitlebarAttribute
;
234 else if ( HasFlag( wxCAPTION
) )
236 if ( HasFlag( wxDIALOG_MODAL
) )
238 wclass
= kMovableModalWindowClass
;
242 wclass
= kDocumentWindowClass
;
247 wclass
= kModalWindowClass
;
250 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
252 attr
|= kWindowFullZoomAttribute
;
253 attr
|= kWindowCollapseBoxAttribute
;
255 if ( HasFlag( wxRESIZE_BORDER
) )
257 attr
|= kWindowResizableAttribute
;
259 if ( HasFlag( wxSYSTEM_MENU
) )
261 attr
|= kWindowCloseBoxAttribute
;
264 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, &m_macWindow
) ;
265 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
267 if( wxApp::s_macDefaultEncodingIsPC
)
268 label
= wxMacMakeMacStringFromPC( title
) ;
271 UMASetWTitleC( m_macWindow
, label
) ;
272 ::CreateRootControl( m_macWindow
, &m_macRootControl
) ;
277 void wxTopLevelWindowMac::MacDoGetPortClientParams(Point
* localOrigin
, Rect
* clipRect
, WindowRef
*window
, wxWindowMac
** rootwin
)
283 clipRect
->right
= m_width
;//width;
284 clipRect
->bottom
= m_height
;// height;
285 *window
= m_macWindow
;
289 void wxTopLevelWindowMac::MacGetPortParams(Point
* localOrigin
, Rect
* clipRect
, WindowRef
*window
, wxWindowMac
** rootwin
)
295 clipRect
->right
= m_width
;
296 clipRect
->bottom
= m_height
;
297 *window
= m_macWindow
;
301 void wxTopLevelWindowMac::Clear()
303 wxMacDrawingClientHelper
helper ( this ) ;
305 wxPoint origin
= GetClientAreaOrigin() ;
306 GetClientSize( &w
, &h
) ;
307 ::SetThemeWindowBackground( m_macWindow
, m_macWindowBackgroundTheme
, false ) ;
308 Rect r
= { origin
.y
, origin
.x
, origin
.y
+h
, origin
.x
+w
} ;
312 ControlHandle
wxTopLevelWindowMac::MacGetContainerForEmbedding()
314 return m_macRootControl
;
318 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
321 AGAPortHelper
help( GetWindowPort(m_macWindow
) ) ;
323 AGAPortHelper
help( (m_macWindow
) ) ;
326 BeginUpdate( m_macWindow
) ;
328 RgnHandle updateRgn
= NewRgn();
329 RgnHandle diffRgn
= NewRgn() ;
330 if ( updateRgn
&& diffRgn
)
332 GetPortVisibleRegion( GetWindowPort( m_macWindow
), updateRgn
);
333 DiffRgn( updateRgn
, m_macNoEraseUpdateRgn
, diffRgn
) ;
334 if ( !EmptyRgn( updateRgn
) )
336 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
340 DisposeRgn( updateRgn
);
342 DisposeRgn( diffRgn
);
343 EndUpdate( m_macWindow
) ;
344 SetEmptyRgn( m_macNoEraseUpdateRgn
) ;
345 m_macNeedsErasing
= false ;
349 // Raise the window to the top of the Z order
350 void wxTopLevelWindowMac::Raise()
352 ::BringToFront( m_macWindow
) ;
355 // Lower the window to the bottom of the Z order
356 void wxTopLevelWindowMac::Lower()
358 ::SendBehind( m_macWindow
, NULL
) ;
363 extern int wxBusyCursorCount
;
365 void wxTopLevelWindowMac::MacFireMouseEvent( EventRecord
*ev
)
367 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
368 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
369 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
371 event
.m_leftDown
= isDown
&& !controlDown
;
373 event
.m_middleDown
= FALSE
;
374 event
.m_rightDown
= isDown
&& controlDown
;
376 if ( ev
->what
== mouseDown
)
379 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
381 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
383 else if ( ev
->what
== mouseUp
)
386 event
.SetEventType(wxEVT_RIGHT_UP
) ;
388 event
.SetEventType(wxEVT_LEFT_UP
) ;
392 event
.SetEventType(wxEVT_MOTION
) ;
395 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
396 event
.m_controlDown
= ev
->modifiers
& controlKey
;
397 event
.m_altDown
= ev
->modifiers
& optionKey
;
398 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
400 Point localwhere
= ev
->where
;
404 ::SetPort( UMAGetWindowPort( m_macWindow
) ) ;
405 ::GlobalToLocal( &localwhere
) ;
408 if ( ev
->what
== mouseDown
)
410 if ( ev
->when
- lastWhen
<= GetDblTime() )
412 if ( abs( localwhere
.h
- lastWhere
.h
) < 3 || abs( localwhere
.v
- lastWhere
.v
) < 3 )
415 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
417 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
423 lastWhen
= ev
->when
;
425 lastWhere
= localwhere
;
428 event
.m_x
= localwhere
.h
;
429 event
.m_y
= localwhere
.v
;
434 wxPoint origin = GetClientAreaOrigin() ;
436 event.m_x += origin.x ;
437 event.m_y += origin.y ;
440 event
.m_timeStamp
= ev
->when
;
441 event
.SetEventObject(this);
442 if ( wxTheApp
->s_captureWindow
)
446 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
449 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
450 if ( ev
->what
== mouseUp
)
452 wxTheApp
->s_captureWindow
= NULL
;
453 if ( wxBusyCursorCount
== 0 )
455 m_cursor
.MacInstall() ;
461 MacDispatchMouseEvent( event
) ;
465 void wxTopLevelWindowMac::MacMouseDown( EventRecord
*ev
, short part
)
467 MacFireMouseEvent( ev
) ;
470 void wxTopLevelWindowMac::MacMouseUp( EventRecord
*ev
, short part
)
476 MacFireMouseEvent( ev
) ;
482 void wxTopLevelWindowMac::MacMouseMoved( EventRecord
*ev
, short part
)
488 MacFireMouseEvent( ev
) ;
493 void wxTopLevelWindowMac::MacActivate( EventRecord
*ev
, bool inIsActivating
)
495 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
496 event
.m_timeStamp
= ev
->when
;
497 event
.SetEventObject(this);
499 GetEventHandler()->ProcessEvent(event
);
501 UMAHighlightAndActivateWindow( m_macWindow
, inIsActivating
) ;
504 // MacSuperEnabled( inIsActivating ) ;
505 // MacUpdateImmediately() ;
508 void wxTopLevelWindowMac::MacKeyDown( EventRecord
*ev
)
512 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
514 wxWindow::SetTitle( title
) ;
518 if( wxApp::s_macDefaultEncodingIsPC
)
519 label
= wxMacMakeMacStringFromPC( m_label
) ;
523 UMASetWTitleC( m_macWindow
, label
) ;
526 bool wxTopLevelWindowMac::Show(bool show
)
528 if ( !wxWindow::Show(show
) )
533 ::ShowWindow( m_macWindow
) ;
534 ::SelectWindow( m_macWindow
) ;
535 // no need to generate events here, they will get them triggered by macos
536 // actually they should be , but apparently they are not
537 wxSize
size(m_width
, m_height
);
538 wxSizeEvent
event(size
, m_windowId
);
539 event
.SetEventObject(this);
540 GetEventHandler()->ProcessEvent(event
);
544 ::HideWindow( m_macWindow
) ;
558 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
562 int former_w
= m_width
;
563 int former_h
= m_height
;
565 int actualWidth
= width
;
566 int actualHeight
= height
;
570 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
571 actualWidth
= m_minWidth
;
572 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
573 actualHeight
= m_minHeight
;
574 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
575 actualWidth
= m_maxWidth
;
576 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
577 actualHeight
= m_maxHeight
;
579 bool doMove
= false ;
580 bool doResize
= false ;
582 if ( actualX
!= former_x
|| actualY
!= former_y
)
586 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
591 if ( doMove
|| doResize
)
595 m_width
= actualWidth
;
596 m_height
= actualHeight
;
599 ::MoveWindow(m_macWindow
, m_x
, m_y
, false); // don't make frontmost
602 ::SizeWindow(m_macWindow
, m_width
, m_height
, true);
604 // the OS takes care of invalidating and erasing the new area
605 // we have erased the old one
607 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
609 wxFrame
* frame
= (wxFrame
*) this ;
610 frame
->PositionStatusBar();
611 frame
->PositionToolBar();
614 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
616 MacRepositionScrollBars() ;
619 wxPoint
point(m_x
, m_y
);
620 wxMoveEvent
event(point
, m_windowId
);
621 event
.SetEventObject(this);
622 GetEventHandler()->ProcessEvent(event
) ;
626 MacRepositionScrollBars() ;
627 wxSize
size(m_width
, m_height
);
628 wxSizeEvent
event(size
, m_windowId
);
629 event
.SetEventObject(this);
630 GetEventHandler()->ProcessEvent(event
);
637 * Invalidation Mechanism
639 * The update mechanism reflects exactely the windows mechanism
640 * the rect gets added to the window invalidate region, if the eraseBackground flag
641 * has been true for any part of the update rgn the background is erased in the entire region
642 * not just in the specified rect.
644 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
645 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
646 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
647 * will get the eraseBackground event first
650 void wxTopLevelWindowMac::MacInvalidate( const Rect
* rect
, bool eraseBackground
)
653 GetPort( &formerPort
) ;
654 SetPortWindowPort( m_macWindow
) ;
656 m_macNeedsErasing
|= eraseBackground
;
658 // if we already know that we will have to erase, there's no need to track the rest
659 if ( !m_macNeedsErasing
)
661 // we end only here if eraseBackground is false
662 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
663 // we will have to erase anyway
665 RgnHandle updateRgn
= NewRgn();
666 RgnHandle diffRgn
= NewRgn() ;
667 if ( updateRgn
&& diffRgn
)
669 GetWindowUpdateRgn( m_macWindow
, updateRgn
);
671 LocalToGlobal( &pt
) ;
672 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
673 DiffRgn( updateRgn
, m_macNoEraseUpdateRgn
, diffRgn
) ;
674 if ( !EmptyRgn( diffRgn
) )
676 m_macNeedsErasing
= true ;
680 DisposeRgn( updateRgn
);
682 DisposeRgn( diffRgn
);
684 if ( !m_macNeedsErasing
)
686 RgnHandle rectRgn
= NewRgn() ;
687 SetRectRgn( rectRgn
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
) ;
688 UnionRgn( m_macNoEraseUpdateRgn
, rectRgn
, m_macNoEraseUpdateRgn
) ;
689 DisposeRgn( rectRgn
) ;
692 InvalWindowRect( m_macWindow
, rect
) ;
693 // turn this on to debug the refreshing cycle
697 SetPort( formerPort
) ;