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
) ;
503 MacSuperEnabled( inIsActivating
) ;
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
) ;