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 // double click testing
58 static Point gs_lastWhere
;
59 static long gs_lastWhen
= 0;
62 extern int wxBusyCursorCount
;
65 // ============================================================================
66 // wxTopLevelWindowMac implementation
67 // ============================================================================
69 // ---------------------------------------------------------------------------
70 // wxWindowMac utility functions
71 // ---------------------------------------------------------------------------
73 // Find an item given the Macintosh Window Reference
75 wxList
*wxWinMacWindowList
= NULL
;
76 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
78 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
81 return (wxTopLevelWindowMac
*)node
->Data();
84 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
86 // adding NULL WindowRef is (first) surely a result of an error and
87 // (secondly) breaks menu command processing
88 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, "attempt to add a NULL WindowRef to window list" );
90 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
91 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
94 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
96 wxWinMacWindowList
->DeleteObject(win
);
100 // ----------------------------------------------------------------------------
101 // wxTopLevelWindowMac creation
102 // ----------------------------------------------------------------------------
104 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
106 void wxTopLevelWindowMac::Init()
109 m_maximizeOnShow
= FALSE
;
110 m_macNoEraseUpdateRgn
= NewRgn() ;
111 m_macNeedsErasing
= false ;
115 class wxMacDeferredWindowDeleter
: public wxObject
118 wxMacDeferredWindowDeleter( WindowRef windowRef
)
120 m_macWindow
= windowRef
;
122 virtual ~wxMacDeferredWindowDeleter()
124 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
127 WindowRef m_macWindow
;
130 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
132 const wxString
& title
,
136 const wxString
& name
)
141 m_windowStyle
= style
;
145 m_windowId
= id
== -1 ? NewControlId() : id
;
147 wxTopLevelWindows
.Append(this);
150 parent
->AddChild(this);
155 wxTopLevelWindowMac::~wxTopLevelWindowMac()
159 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
160 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
163 wxRemoveMacWindowAssociation( this ) ;
165 wxTopLevelWindows
.DeleteObject(this);
167 if ( wxModelessWindows
.Find(this) )
168 wxModelessWindows
.DeleteObject(this);
170 // If this is the last top-level window, exit.
171 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
173 wxTheApp
->SetTopWindow(NULL
);
175 if ( wxTheApp
->GetExitOnFrameDelete() )
177 wxTheApp
->ExitMainLoop() ;
180 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
184 // ----------------------------------------------------------------------------
185 // wxTopLevelWindowMac maximize/minimize
186 // ----------------------------------------------------------------------------
188 void wxTopLevelWindowMac::Maximize(bool maximize
)
190 // not available on mac
193 bool wxTopLevelWindowMac::IsMaximized() const
198 void wxTopLevelWindowMac::Iconize(bool iconize
)
200 // not available on mac
203 bool wxTopLevelWindowMac::IsIconized() const
205 // mac dialogs cannot be iconized
209 void wxTopLevelWindowMac::Restore()
211 // not available on mac
214 // ----------------------------------------------------------------------------
215 // wxTopLevelWindowMac misc
216 // ----------------------------------------------------------------------------
218 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
221 wxTopLevelWindowBase::SetIcon(icon
);
224 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
228 const wxString
& name
)
231 m_windowStyle
= style
;
252 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
254 // translate the window attributes in the appropriate window class and attributes
256 WindowClass wclass
= 0;
257 WindowAttributes attr
= kWindowNoAttributes
;
259 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
262 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
263 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
264 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
267 wclass
= kFloatingWindowClass
;
268 if ( HasFlag(wxTINY_CAPTION_VERT
) )
270 attr
|= kWindowSideTitlebarAttribute
;
275 wclass
= kPlainWindowClass
;
278 else if ( HasFlag( wxCAPTION
) )
280 if ( HasFlag( wxDIALOG_MODAL
) )
282 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
286 wclass
= kDocumentWindowClass
;
291 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
292 HasFlag( wxSYSTEM_MENU
) )
294 wclass
= kDocumentWindowClass
;
298 wclass
= kPlainWindowClass
;
302 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
304 attr
|= kWindowFullZoomAttribute
;
305 attr
|= kWindowCollapseBoxAttribute
;
307 if ( HasFlag( wxRESIZE_BORDER
) )
309 attr
|= kWindowResizableAttribute
;
311 if ( HasFlag( wxSYSTEM_MENU
) )
313 attr
|= kWindowCloseBoxAttribute
;
316 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
317 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
319 if( wxApp::s_macDefaultEncodingIsPC
)
320 label
= wxMacMakeMacStringFromPC( title
) ;
323 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
324 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
329 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
331 ((Point
*)localOrigin
)->h
= 0;
332 ((Point
*)localOrigin
)->v
= 0;
333 ((Rect
*)clipRect
)->left
= 0;
334 ((Rect
*)clipRect
)->top
= 0;
335 ((Rect
*)clipRect
)->right
= m_width
;
336 ((Rect
*)clipRect
)->bottom
= m_height
;
337 *window
= m_macWindow
;
341 void wxTopLevelWindowMac::Clear()
346 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
348 return m_macRootControl
;
352 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
355 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
357 BeginUpdate( (WindowRef
)m_macWindow
) ;
359 RgnHandle updateRgn
= NewRgn();
360 RgnHandle diffRgn
= NewRgn() ;
361 if ( updateRgn
&& diffRgn
)
363 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
364 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
365 if ( !EmptyRgn( updateRgn
) )
367 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
371 DisposeRgn( updateRgn
);
373 DisposeRgn( diffRgn
);
374 EndUpdate( (WindowRef
)m_macWindow
) ;
375 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
376 m_macNeedsErasing
= false ;
380 // Raise the window to the top of the Z order
381 void wxTopLevelWindowMac::Raise()
383 ::BringToFront( (WindowRef
)m_macWindow
) ;
386 // Lower the window to the bottom of the Z order
387 void wxTopLevelWindowMac::Lower()
389 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
392 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
394 EventRecord
*ev
= (EventRecord
*) evr
;
395 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
396 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
397 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
399 event
.m_leftDown
= isDown
&& !controlDown
;
401 event
.m_middleDown
= FALSE
;
402 event
.m_rightDown
= isDown
&& controlDown
;
404 if ( ev
->what
== mouseDown
)
407 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
409 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
411 else if ( ev
->what
== mouseUp
)
414 event
.SetEventType(wxEVT_RIGHT_UP
) ;
416 event
.SetEventType(wxEVT_LEFT_UP
) ;
420 event
.SetEventType(wxEVT_MOTION
) ;
423 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
424 event
.m_controlDown
= ev
->modifiers
& controlKey
;
425 event
.m_altDown
= ev
->modifiers
& optionKey
;
426 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
428 Point localwhere
= ev
->where
;
432 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
433 ::GlobalToLocal( &localwhere
) ;
436 if ( ev
->what
== mouseDown
)
438 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
440 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
442 // This is not right if the second mouse down
443 // event occured in a differen window. We
444 // correct this in MacDispatchMouseEvent.
446 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
448 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
454 gs_lastWhen
= ev
->when
;
456 gs_lastWhere
= localwhere
;
459 event
.m_x
= localwhere
.h
;
460 event
.m_y
= localwhere
.v
;
464 event
.m_timeStamp
= ev
->when
;
465 event
.SetEventObject(this);
466 if ( wxTheApp
->s_captureWindow
)
470 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
473 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
474 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
476 if ( ev
->what
== mouseUp
)
478 wxTheApp
->s_captureWindow
= NULL
;
479 if ( wxBusyCursorCount
== 0 )
481 m_cursor
.MacInstall() ;
487 MacDispatchMouseEvent( event
) ;
491 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
493 MacFireMouseEvent( ev
) ;
496 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
502 MacFireMouseEvent( ev
) ;
508 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
514 MacFireMouseEvent( ev
) ;
519 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
521 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
522 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
523 event
.SetEventObject(this);
525 GetEventHandler()->ProcessEvent(event
);
527 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
529 MacSuperEnabled( inIsActivating
) ;
532 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
536 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
538 wxWindow::SetTitle( title
) ;
542 if( wxApp::s_macDefaultEncodingIsPC
)
543 label
= wxMacMakeMacStringFromPC( m_label
) ;
547 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
550 bool wxTopLevelWindowMac::Show(bool show
)
552 if ( !wxWindow::Show(show
) )
557 ::ShowWindow( (WindowRef
)m_macWindow
) ;
558 ::SelectWindow( (WindowRef
)m_macWindow
) ;
559 // no need to generate events here, they will get them triggered by macos
560 // actually they should be , but apparently they are not
561 wxSize
size(m_width
, m_height
);
562 wxSizeEvent
event(size
, m_windowId
);
563 event
.SetEventObject(this);
564 GetEventHandler()->ProcessEvent(event
);
568 ::HideWindow( (WindowRef
)m_macWindow
) ;
582 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
586 int former_w
= m_width
;
587 int former_h
= m_height
;
589 int actualWidth
= width
;
590 int actualHeight
= height
;
594 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
595 actualWidth
= m_minWidth
;
596 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
597 actualHeight
= m_minHeight
;
598 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
599 actualWidth
= m_maxWidth
;
600 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
601 actualHeight
= m_maxHeight
;
603 bool doMove
= false ;
604 bool doResize
= false ;
606 if ( actualX
!= former_x
|| actualY
!= former_y
)
610 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
615 if ( doMove
|| doResize
)
619 m_width
= actualWidth
;
620 m_height
= actualHeight
;
623 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
626 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
628 // the OS takes care of invalidating and erasing the new area
629 // we have erased the old one
631 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
633 wxFrame
* frame
= (wxFrame
*) this ;
634 frame
->PositionStatusBar();
635 frame
->PositionToolBar();
638 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
640 MacRepositionScrollBars() ;
643 wxPoint
point(m_x
, m_y
);
644 wxMoveEvent
event(point
, m_windowId
);
645 event
.SetEventObject(this);
646 GetEventHandler()->ProcessEvent(event
) ;
650 MacRepositionScrollBars() ;
651 wxSize
size(m_width
, m_height
);
652 wxSizeEvent
event(size
, m_windowId
);
653 event
.SetEventObject(this);
654 GetEventHandler()->ProcessEvent(event
);
661 * Invalidation Mechanism
663 * The update mechanism reflects exactely the windows mechanism
664 * the rect gets added to the window invalidate region, if the eraseBackground flag
665 * has been true for any part of the update rgn the background is erased in the entire region
666 * not just in the specified rect.
668 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
669 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
670 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
671 * will get the eraseBackground event first
674 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
677 GetPort( &formerPort
) ;
678 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
680 m_macNeedsErasing
|= eraseBackground
;
682 // if we already know that we will have to erase, there's no need to track the rest
683 if ( !m_macNeedsErasing
)
685 // we end only here if eraseBackground is false
686 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
687 // we will have to erase anyway
689 RgnHandle updateRgn
= NewRgn();
690 RgnHandle diffRgn
= NewRgn() ;
691 if ( updateRgn
&& diffRgn
)
693 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
695 LocalToGlobal( &pt
) ;
696 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
697 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
698 if ( !EmptyRgn( diffRgn
) )
700 m_macNeedsErasing
= true ;
704 DisposeRgn( updateRgn
);
706 DisposeRgn( diffRgn
);
708 if ( !m_macNeedsErasing
)
710 RgnHandle rectRgn
= NewRgn() ;
711 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
712 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
713 DisposeRgn( rectRgn
) ;
716 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
717 // turn this on to debug the refreshing cycle
718 #if wxMAC_DEBUG_REDRAW
721 SetPort( formerPort
) ;