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"
34 #include "wx/string.h"
39 #include "wx/mac/uma.h"
40 #include "wx/tooltip.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // list of all frames and modeless dialogs
47 wxWindowList wxModelessWindows
;
49 // ============================================================================
50 // wxTopLevelWindowMac implementation
51 // ============================================================================
53 // ---------------------------------------------------------------------------
54 // wxWindowMac utility functions
55 // ---------------------------------------------------------------------------
57 // Find an item given the Macintosh Window Reference
59 wxList
*wxWinMacWindowList
= NULL
;
60 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
62 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
65 return (wxTopLevelWindowMac
*)node
->Data();
68 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
70 // adding NULL WindowRef is (first) surely a result of an error and
71 // (secondly) breaks menu command processing
72 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, "attempt to add a NULL WindowRef to window list" );
74 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
75 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
78 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
80 wxWinMacWindowList
->DeleteObject(win
);
84 // ----------------------------------------------------------------------------
85 // wxTopLevelWindowMac creation
86 // ----------------------------------------------------------------------------
88 WindowRef
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
90 void wxTopLevelWindowMac::Init()
93 m_maximizeOnShow
= FALSE
;
94 m_macNoEraseUpdateRgn
= NewRgn() ;
95 m_macNeedsErasing
= false ;
98 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
100 const wxString
& title
,
104 const wxString
& name
)
109 m_windowStyle
= style
;
113 m_windowId
= id
== -1 ? NewControlId() : id
;
115 wxTopLevelWindows
.Append(this);
118 parent
->AddChild(this);
123 wxTopLevelWindowMac::~wxTopLevelWindowMac()
125 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
126 UMADisposeWindow( m_macWindow
) ;
128 wxRemoveMacWindowAssociation( this ) ;
130 wxTopLevelWindows
.DeleteObject(this);
132 if ( wxModelessWindows
.Find(this) )
133 wxModelessWindows
.DeleteObject(this);
135 // If this is the last top-level window, exit.
136 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
138 wxTheApp
->SetTopWindow(NULL
);
140 if ( wxTheApp
->GetExitOnFrameDelete() )
142 wxTheApp
->ExitMainLoop() ;
145 DisposeRgn( m_macNoEraseUpdateRgn
) ;
149 // ----------------------------------------------------------------------------
150 // wxTopLevelWindowMac maximize/minimize
151 // ----------------------------------------------------------------------------
153 void wxTopLevelWindowMac::Maximize(bool maximize
)
155 // not available on mac
158 bool wxTopLevelWindowMac::IsMaximized() const
163 void wxTopLevelWindowMac::Iconize(bool iconize
)
165 // not available on mac
168 bool wxTopLevelWindowMac::IsIconized() const
170 // mac dialogs cannot be iconized
174 void wxTopLevelWindowMac::Restore()
176 // not available on mac
179 // ----------------------------------------------------------------------------
180 // wxTopLevelWindowMac misc
181 // ----------------------------------------------------------------------------
183 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
186 wxTopLevelWindowBase::SetIcon(icon
);
189 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
193 const wxString
& name
)
196 m_windowStyle
= style
;
217 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
219 // translate the window attributes in the appropriate window class and attributes
221 WindowClass wclass
= 0;
222 WindowAttributes attr
= kWindowNoAttributes
;
224 if ( HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
) )
226 wclass
= kFloatingWindowClass
;
227 if ( HasFlag(wxTINY_CAPTION_VERT
) )
229 attr
|= kWindowSideTitlebarAttribute
;
232 else if ( HasFlag( wxCAPTION
) )
234 if ( HasFlag( wxDIALOG_MODAL
) )
236 wclass
= kMovableModalWindowClass
;
240 wclass
= kDocumentWindowClass
;
245 wclass
= kModalWindowClass
;
248 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
250 attr
|= kWindowFullZoomAttribute
;
251 attr
|= kWindowCollapseBoxAttribute
;
253 if ( HasFlag( wxRESIZE_BORDER
) )
255 attr
|= kWindowResizableAttribute
;
257 if ( HasFlag( wxSYSTEM_MENU
) )
259 attr
|= kWindowCloseBoxAttribute
;
262 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, &m_macWindow
) ;
263 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
265 if( wxApp::s_macDefaultEncodingIsPC
)
266 label
= wxMacMakeMacStringFromPC( title
) ;
269 UMASetWTitleC( m_macWindow
, label
) ;
270 ::CreateRootControl( m_macWindow
, &m_macRootControl
) ;
275 void wxTopLevelWindowMac::MacDoGetPortClientParams(Point
* localOrigin
, Rect
* clipRect
, WindowRef
*window
, wxWindowMac
** rootwin
)
281 clipRect
->right
= m_width
;//width;
282 clipRect
->bottom
= m_height
;// height;
283 *window
= m_macWindow
;
287 void wxTopLevelWindowMac::MacGetPortParams(Point
* localOrigin
, Rect
* clipRect
, WindowRef
*window
, wxWindowMac
** rootwin
)
293 clipRect
->right
= m_width
;
294 clipRect
->bottom
= m_height
;
295 *window
= m_macWindow
;
299 void wxTopLevelWindowMac::Clear()
301 wxMacDrawingClientHelper
helper ( this ) ;
303 wxPoint origin
= GetClientAreaOrigin() ;
304 GetClientSize( &w
, &h
) ;
305 ::SetThemeWindowBackground( m_macWindow
, m_macWindowBackgroundTheme
, false ) ;
306 Rect r
= { origin
.y
, origin
.x
, origin
.y
+h
, origin
.x
+w
} ;
310 ControlHandle
wxTopLevelWindowMac::MacGetContainerForEmbedding()
312 return m_macRootControl
;
316 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
319 AGAPortHelper
help( GetWindowPort(m_macWindow
) ) ;
321 AGAPortHelper
help( (m_macWindow
) ) ;
324 BeginUpdate( m_macWindow
) ;
326 RgnHandle updateRgn
= NewRgn();
327 RgnHandle diffRgn
= NewRgn() ;
328 if ( updateRgn
&& diffRgn
)
330 GetPortVisibleRegion( GetWindowPort( m_macWindow
), updateRgn
);
331 DiffRgn( updateRgn
, m_macNoEraseUpdateRgn
, diffRgn
) ;
332 if ( !EmptyRgn( updateRgn
) )
334 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
338 DisposeRgn( updateRgn
);
340 DisposeRgn( diffRgn
);
341 EndUpdate( m_macWindow
) ;
342 SetEmptyRgn( m_macNoEraseUpdateRgn
) ;
343 m_macNeedsErasing
= false ;
347 // Raise the window to the top of the Z order
348 void wxTopLevelWindowMac::Raise()
350 ::BringToFront( m_macWindow
) ;
353 // Lower the window to the bottom of the Z order
354 void wxTopLevelWindowMac::Lower()
356 ::SendBehind( m_macWindow
, NULL
) ;
361 extern int wxBusyCursorCount
;
363 void wxTopLevelWindowMac::MacFireMouseEvent( EventRecord
*ev
)
365 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
366 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
367 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
369 event
.m_leftDown
= isDown
&& !controlDown
;
371 event
.m_middleDown
= FALSE
;
372 event
.m_rightDown
= isDown
&& controlDown
;
374 if ( ev
->what
== mouseDown
)
377 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
379 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
381 else if ( ev
->what
== mouseUp
)
384 event
.SetEventType(wxEVT_RIGHT_UP
) ;
386 event
.SetEventType(wxEVT_LEFT_UP
) ;
390 event
.SetEventType(wxEVT_MOTION
) ;
393 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
394 event
.m_controlDown
= ev
->modifiers
& controlKey
;
395 event
.m_altDown
= ev
->modifiers
& optionKey
;
396 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
398 Point localwhere
= ev
->where
;
402 ::SetPort( UMAGetWindowPort( m_macWindow
) ) ;
403 ::GlobalToLocal( &localwhere
) ;
406 if ( ev
->what
== mouseDown
)
408 if ( ev
->when
- lastWhen
<= GetDblTime() )
410 if ( abs( localwhere
.h
- lastWhere
.h
) < 3 || abs( localwhere
.v
- lastWhere
.v
) < 3 )
413 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
415 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
421 lastWhen
= ev
->when
;
423 lastWhere
= localwhere
;
426 event
.m_x
= localwhere
.h
;
427 event
.m_y
= localwhere
.v
;
432 wxPoint origin = GetClientAreaOrigin() ;
434 event.m_x += origin.x ;
435 event.m_y += origin.y ;
438 event
.m_timeStamp
= ev
->when
;
439 event
.SetEventObject(this);
440 if ( wxTheApp
->s_captureWindow
)
444 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
447 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
448 if ( ev
->what
== mouseUp
)
450 wxTheApp
->s_captureWindow
= NULL
;
451 if ( wxBusyCursorCount
== 0 )
453 m_cursor
.MacInstall() ;
459 MacDispatchMouseEvent( event
) ;
463 void wxTopLevelWindowMac::MacMouseDown( EventRecord
*ev
, short part
)
465 MacFireMouseEvent( ev
) ;
468 void wxTopLevelWindowMac::MacMouseUp( EventRecord
*ev
, short part
)
474 MacFireMouseEvent( ev
) ;
480 void wxTopLevelWindowMac::MacMouseMoved( EventRecord
*ev
, short part
)
486 MacFireMouseEvent( ev
) ;
491 void wxTopLevelWindowMac::MacActivate( EventRecord
*ev
, bool inIsActivating
)
493 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
494 event
.m_timeStamp
= ev
->when
;
495 event
.SetEventObject(this);
497 GetEventHandler()->ProcessEvent(event
);
499 UMAHighlightAndActivateWindow( m_macWindow
, inIsActivating
) ;
502 // MacSuperEnabled( inIsActivating ) ;
503 // MacUpdateImmediately() ;
506 void wxTopLevelWindowMac::MacKeyDown( EventRecord
*ev
)
510 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
512 wxWindow::SetTitle( title
) ;
516 if( wxApp::s_macDefaultEncodingIsPC
)
517 label
= wxMacMakeMacStringFromPC( m_label
) ;
521 UMASetWTitleC( m_macWindow
, label
) ;
524 bool wxTopLevelWindowMac::Show(bool show
)
526 if ( !wxWindow::Show(show
) )
531 ::ShowWindow( m_macWindow
) ;
532 ::SelectWindow( m_macWindow
) ;
533 // no need to generate events here, they will get them triggered by macos
534 // actually they should be , but apparently they are not
535 wxSize
size(m_width
, m_height
);
536 wxSizeEvent
event(size
, m_windowId
);
537 event
.SetEventObject(this);
538 GetEventHandler()->ProcessEvent(event
);
542 ::HideWindow( m_macWindow
) ;
556 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
560 int former_w
= m_width
;
561 int former_h
= m_height
;
563 int actualWidth
= width
;
564 int actualHeight
= height
;
568 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
569 actualWidth
= m_minWidth
;
570 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
571 actualHeight
= m_minHeight
;
572 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
573 actualWidth
= m_maxWidth
;
574 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
575 actualHeight
= m_maxHeight
;
577 bool doMove
= false ;
578 bool doResize
= false ;
580 if ( actualX
!= former_x
|| actualY
!= former_y
)
584 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
589 if ( doMove
|| doResize
)
593 m_width
= actualWidth
;
594 m_height
= actualHeight
;
597 ::MoveWindow(m_macWindow
, m_x
, m_y
, false); // don't make frontmost
600 ::SizeWindow(m_macWindow
, m_width
, m_height
, true);
602 // the OS takes care of invalidating and erasing the new area
603 // we have erased the old one
605 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
607 wxFrame
* frame
= (wxFrame
*) this ;
608 frame
->PositionStatusBar();
609 frame
->PositionToolBar();
612 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
614 MacRepositionScrollBars() ;
617 wxPoint
point(m_x
, m_y
);
618 wxMoveEvent
event(point
, m_windowId
);
619 event
.SetEventObject(this);
620 GetEventHandler()->ProcessEvent(event
) ;
624 MacRepositionScrollBars() ;
625 wxSize
size(m_width
, m_height
);
626 wxSizeEvent
event(size
, m_windowId
);
627 event
.SetEventObject(this);
628 GetEventHandler()->ProcessEvent(event
);
635 * Invalidation Mechanism
637 * The update mechanism reflects exactely the windows mechanism
638 * the rect gets added to the window invalidate region, if the eraseBackground flag
639 * has been true for any part of the update rgn the background is erased in the entire region
640 * not just in the specified rect.
642 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
643 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
644 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
645 * will get the eraseBackground event first
648 void wxTopLevelWindowMac::MacInvalidate( const Rect
* rect
, bool eraseBackground
)
651 GetPort( &formerPort
) ;
652 SetPortWindowPort( m_macWindow
) ;
654 m_macNeedsErasing
|= eraseBackground
;
656 // if we already know that we will have to erase, there's no need to track the rest
657 if ( !m_macNeedsErasing
)
659 // we end only here if eraseBackground is false
660 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
661 // we will have to erase anyway
663 RgnHandle updateRgn
= NewRgn();
664 RgnHandle diffRgn
= NewRgn() ;
665 if ( updateRgn
&& diffRgn
)
667 GetWindowUpdateRgn( m_macWindow
, updateRgn
);
669 LocalToGlobal( &pt
) ;
670 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
671 DiffRgn( updateRgn
, m_macNoEraseUpdateRgn
, diffRgn
) ;
672 if ( !EmptyRgn( diffRgn
) )
674 m_macNeedsErasing
= true ;
678 DisposeRgn( updateRgn
);
680 DisposeRgn( diffRgn
);
682 if ( !m_macNeedsErasing
)
684 RgnHandle rectRgn
= NewRgn() ;
685 SetRectRgn( rectRgn
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
) ;
686 UnionRgn( m_macNoEraseUpdateRgn
, rectRgn
, m_macNoEraseUpdateRgn
) ;
687 DisposeRgn( rectRgn
) ;
690 InvalWindowRect( m_macWindow
, rect
) ;
691 // turn this on to debug the refreshing cycle
695 SetPort( formerPort
) ;