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
) /*|| HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)*/ )
261 wclass
= kFloatingWindowClass
;
262 if ( HasFlag(wxTINY_CAPTION_VERT
) )
264 attr
|= kWindowSideTitlebarAttribute
;
267 else if ( HasFlag( wxCAPTION
) )
269 if ( HasFlag( wxDIALOG_MODAL
) )
271 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
275 wclass
= kDocumentWindowClass
;
280 wclass
= kDocumentWindowClass
;
283 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
285 attr
|= kWindowFullZoomAttribute
;
286 attr
|= kWindowCollapseBoxAttribute
;
288 if ( HasFlag( wxRESIZE_BORDER
) )
290 attr
|= kWindowResizableAttribute
;
292 if ( HasFlag( wxSYSTEM_MENU
) )
294 attr
|= kWindowCloseBoxAttribute
;
297 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
298 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
300 if( wxApp::s_macDefaultEncodingIsPC
)
301 label
= wxMacMakeMacStringFromPC( title
) ;
304 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
305 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
310 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
312 ((Point
*)localOrigin
)->h
= 0;
313 ((Point
*)localOrigin
)->v
= 0;
314 ((Rect
*)clipRect
)->left
= 0;
315 ((Rect
*)clipRect
)->top
= 0;
316 ((Rect
*)clipRect
)->right
= m_width
;
317 ((Rect
*)clipRect
)->bottom
= m_height
;
318 *window
= m_macWindow
;
322 void wxTopLevelWindowMac::Clear()
327 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
329 return m_macRootControl
;
333 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
336 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
338 BeginUpdate( (WindowRef
)m_macWindow
) ;
340 RgnHandle updateRgn
= NewRgn();
341 RgnHandle diffRgn
= NewRgn() ;
342 if ( updateRgn
&& diffRgn
)
344 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
345 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
346 if ( !EmptyRgn( updateRgn
) )
348 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
352 DisposeRgn( updateRgn
);
354 DisposeRgn( diffRgn
);
355 EndUpdate( (WindowRef
)m_macWindow
) ;
356 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
357 m_macNeedsErasing
= false ;
361 // Raise the window to the top of the Z order
362 void wxTopLevelWindowMac::Raise()
364 ::BringToFront( (WindowRef
)m_macWindow
) ;
367 // Lower the window to the bottom of the Z order
368 void wxTopLevelWindowMac::Lower()
370 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
373 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
375 EventRecord
*ev
= (EventRecord
*) evr
;
376 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
377 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
378 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
380 event
.m_leftDown
= isDown
&& !controlDown
;
382 event
.m_middleDown
= FALSE
;
383 event
.m_rightDown
= isDown
&& controlDown
;
385 if ( ev
->what
== mouseDown
)
388 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
390 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
392 else if ( ev
->what
== mouseUp
)
395 event
.SetEventType(wxEVT_RIGHT_UP
) ;
397 event
.SetEventType(wxEVT_LEFT_UP
) ;
401 event
.SetEventType(wxEVT_MOTION
) ;
404 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
405 event
.m_controlDown
= ev
->modifiers
& controlKey
;
406 event
.m_altDown
= ev
->modifiers
& optionKey
;
407 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
409 Point localwhere
= ev
->where
;
413 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
414 ::GlobalToLocal( &localwhere
) ;
417 if ( ev
->what
== mouseDown
)
419 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
421 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
423 // This is not right if the second mouse down
424 // event occured in a differen window. We
425 // correct this in MacDispatchMouseEvent.
427 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
429 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
435 gs_lastWhen
= ev
->when
;
437 gs_lastWhere
= localwhere
;
440 event
.m_x
= localwhere
.h
;
441 event
.m_y
= localwhere
.v
;
445 event
.m_timeStamp
= ev
->when
;
446 event
.SetEventObject(this);
447 if ( wxTheApp
->s_captureWindow
)
451 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
454 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
455 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
457 if ( ev
->what
== mouseUp
)
459 wxTheApp
->s_captureWindow
= NULL
;
460 if ( wxBusyCursorCount
== 0 )
462 m_cursor
.MacInstall() ;
468 MacDispatchMouseEvent( event
) ;
472 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
474 MacFireMouseEvent( ev
) ;
477 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
483 MacFireMouseEvent( ev
) ;
489 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
495 MacFireMouseEvent( ev
) ;
500 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
502 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
503 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
504 event
.SetEventObject(this);
506 GetEventHandler()->ProcessEvent(event
);
508 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
510 MacSuperEnabled( inIsActivating
) ;
513 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
517 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
519 wxWindow::SetTitle( title
) ;
523 if( wxApp::s_macDefaultEncodingIsPC
)
524 label
= wxMacMakeMacStringFromPC( m_label
) ;
528 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
531 bool wxTopLevelWindowMac::Show(bool show
)
533 if ( !wxWindow::Show(show
) )
538 ::ShowWindow( (WindowRef
)m_macWindow
) ;
539 ::SelectWindow( (WindowRef
)m_macWindow
) ;
540 // no need to generate events here, they will get them triggered by macos
541 // actually they should be , but apparently they are not
542 wxSize
size(m_width
, m_height
);
543 wxSizeEvent
event(size
, m_windowId
);
544 event
.SetEventObject(this);
545 GetEventHandler()->ProcessEvent(event
);
549 ::HideWindow( (WindowRef
)m_macWindow
) ;
563 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
567 int former_w
= m_width
;
568 int former_h
= m_height
;
570 int actualWidth
= width
;
571 int actualHeight
= height
;
575 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
576 actualWidth
= m_minWidth
;
577 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
578 actualHeight
= m_minHeight
;
579 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
580 actualWidth
= m_maxWidth
;
581 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
582 actualHeight
= m_maxHeight
;
584 bool doMove
= false ;
585 bool doResize
= false ;
587 if ( actualX
!= former_x
|| actualY
!= former_y
)
591 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
596 if ( doMove
|| doResize
)
600 m_width
= actualWidth
;
601 m_height
= actualHeight
;
604 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
607 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
609 // the OS takes care of invalidating and erasing the new area
610 // we have erased the old one
612 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
614 wxFrame
* frame
= (wxFrame
*) this ;
615 frame
->PositionStatusBar();
616 frame
->PositionToolBar();
619 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
621 MacRepositionScrollBars() ;
624 wxPoint
point(m_x
, m_y
);
625 wxMoveEvent
event(point
, m_windowId
);
626 event
.SetEventObject(this);
627 GetEventHandler()->ProcessEvent(event
) ;
631 MacRepositionScrollBars() ;
632 wxSize
size(m_width
, m_height
);
633 wxSizeEvent
event(size
, m_windowId
);
634 event
.SetEventObject(this);
635 GetEventHandler()->ProcessEvent(event
);
642 * Invalidation Mechanism
644 * The update mechanism reflects exactely the windows mechanism
645 * the rect gets added to the window invalidate region, if the eraseBackground flag
646 * has been true for any part of the update rgn the background is erased in the entire region
647 * not just in the specified rect.
649 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
650 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
651 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
652 * will get the eraseBackground event first
655 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
658 GetPort( &formerPort
) ;
659 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
661 m_macNeedsErasing
|= eraseBackground
;
663 // if we already know that we will have to erase, there's no need to track the rest
664 if ( !m_macNeedsErasing
)
666 // we end only here if eraseBackground is false
667 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
668 // we will have to erase anyway
670 RgnHandle updateRgn
= NewRgn();
671 RgnHandle diffRgn
= NewRgn() ;
672 if ( updateRgn
&& diffRgn
)
674 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
676 LocalToGlobal( &pt
) ;
677 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
678 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
679 if ( !EmptyRgn( diffRgn
) )
681 m_macNeedsErasing
= true ;
685 DisposeRgn( updateRgn
);
687 DisposeRgn( diffRgn
);
689 if ( !m_macNeedsErasing
)
691 RgnHandle rectRgn
= NewRgn() ;
692 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
693 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
694 DisposeRgn( rectRgn
) ;
697 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
698 // turn this on to debug the refreshing cycle
699 #if wxMAC_DEBUG_REDRAW
702 SetPort( formerPort
) ;