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 // ============================================================================
58 // wxTopLevelWindowMac implementation
59 // ============================================================================
61 // ---------------------------------------------------------------------------
62 // wxWindowMac utility functions
63 // ---------------------------------------------------------------------------
65 // Find an item given the Macintosh Window Reference
67 wxList
*wxWinMacWindowList
= NULL
;
68 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
70 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
73 return (wxTopLevelWindowMac
*)node
->Data();
76 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
78 // adding NULL WindowRef is (first) surely a result of an error and
79 // (secondly) breaks menu command processing
80 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, "attempt to add a NULL WindowRef to window list" );
82 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
83 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
86 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
88 wxWinMacWindowList
->DeleteObject(win
);
92 // ----------------------------------------------------------------------------
93 // wxTopLevelWindowMac creation
94 // ----------------------------------------------------------------------------
96 WindowRef
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
98 void wxTopLevelWindowMac::Init()
101 m_maximizeOnShow
= FALSE
;
102 m_macNoEraseUpdateRgn
= NewRgn() ;
103 m_macNeedsErasing
= false ;
106 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
108 const wxString
& title
,
112 const wxString
& name
)
117 m_windowStyle
= style
;
121 m_windowId
= id
== -1 ? NewControlId() : id
;
123 wxTopLevelWindows
.Append(this);
126 parent
->AddChild(this);
131 wxTopLevelWindowMac::~wxTopLevelWindowMac()
133 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
134 UMADisposeWindow( m_macWindow
) ;
136 wxRemoveMacWindowAssociation( this ) ;
138 wxTopLevelWindows
.DeleteObject(this);
140 if ( wxModelessWindows
.Find(this) )
141 wxModelessWindows
.DeleteObject(this);
143 // If this is the last top-level window, exit.
144 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
146 wxTheApp
->SetTopWindow(NULL
);
148 if ( wxTheApp
->GetExitOnFrameDelete() )
150 wxTheApp
->ExitMainLoop() ;
153 DisposeRgn( m_macNoEraseUpdateRgn
) ;
157 // ----------------------------------------------------------------------------
158 // wxTopLevelWindowMac maximize/minimize
159 // ----------------------------------------------------------------------------
161 void wxTopLevelWindowMac::Maximize(bool maximize
)
163 // not available on mac
166 bool wxTopLevelWindowMac::IsMaximized() const
171 void wxTopLevelWindowMac::Iconize(bool iconize
)
173 // not available on mac
176 bool wxTopLevelWindowMac::IsIconized() const
178 // mac dialogs cannot be iconized
182 void wxTopLevelWindowMac::Restore()
184 // not available on mac
187 // ----------------------------------------------------------------------------
188 // wxTopLevelWindowMac misc
189 // ----------------------------------------------------------------------------
191 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
194 wxTopLevelWindowBase::SetIcon(icon
);
197 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
201 const wxString
& name
)
204 m_windowStyle
= style
;
225 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
227 // translate the window attributes in the appropriate window class and attributes
229 WindowClass wclass
= 0;
230 WindowAttributes attr
= kWindowNoAttributes
;
232 if ( HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
) )
234 wclass
= kFloatingWindowClass
;
235 if ( HasFlag(wxTINY_CAPTION_VERT
) )
237 attr
|= kWindowSideTitlebarAttribute
;
240 else if ( HasFlag( wxCAPTION
) )
242 if ( HasFlag( wxDIALOG_MODAL
) )
244 wclass
= kMovableModalWindowClass
;
248 wclass
= kDocumentWindowClass
;
253 wclass
= kModalWindowClass
;
256 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
258 attr
|= kWindowFullZoomAttribute
;
259 attr
|= kWindowCollapseBoxAttribute
;
261 if ( HasFlag( wxRESIZE_BORDER
) )
263 attr
|= kWindowResizableAttribute
;
265 if ( HasFlag( wxSYSTEM_MENU
) )
267 attr
|= kWindowCloseBoxAttribute
;
270 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, &m_macWindow
) ;
271 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
273 if( wxApp::s_macDefaultEncodingIsPC
)
274 label
= wxMacMakeMacStringFromPC( title
) ;
277 UMASetWTitleC( m_macWindow
, label
) ;
278 ::CreateRootControl( m_macWindow
, &m_macRootControl
) ;
283 void wxTopLevelWindowMac::MacGetPortParams(Point
* localOrigin
, Rect
* clipRect
, WindowRef
*window
, wxWindowMac
** rootwin
)
289 clipRect
->right
= m_width
;
290 clipRect
->bottom
= m_height
;
291 *window
= m_macWindow
;
295 void wxTopLevelWindowMac::Clear()
300 ControlHandle
wxTopLevelWindowMac::MacGetContainerForEmbedding()
302 return m_macRootControl
;
306 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
309 AGAPortHelper
help( GetWindowPort(m_macWindow
) ) ;
311 AGAPortHelper
help( (m_macWindow
) ) ;
313 BeginUpdate( m_macWindow
) ;
315 RgnHandle updateRgn
= NewRgn();
316 RgnHandle diffRgn
= NewRgn() ;
317 if ( updateRgn
&& diffRgn
)
319 GetPortVisibleRegion( GetWindowPort( m_macWindow
), updateRgn
);
320 DiffRgn( updateRgn
, m_macNoEraseUpdateRgn
, diffRgn
) ;
321 if ( !EmptyRgn( updateRgn
) )
323 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
327 DisposeRgn( updateRgn
);
329 DisposeRgn( diffRgn
);
330 EndUpdate( m_macWindow
) ;
331 SetEmptyRgn( m_macNoEraseUpdateRgn
) ;
332 m_macNeedsErasing
= false ;
336 // Raise the window to the top of the Z order
337 void wxTopLevelWindowMac::Raise()
339 ::BringToFront( m_macWindow
) ;
342 // Lower the window to the bottom of the Z order
343 void wxTopLevelWindowMac::Lower()
345 ::SendBehind( m_macWindow
, NULL
) ;
350 extern int wxBusyCursorCount
;
352 void wxTopLevelWindowMac::MacFireMouseEvent( EventRecord
*ev
)
354 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
355 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
356 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
358 event
.m_leftDown
= isDown
&& !controlDown
;
360 event
.m_middleDown
= FALSE
;
361 event
.m_rightDown
= isDown
&& controlDown
;
363 if ( ev
->what
== mouseDown
)
366 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
368 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
370 else if ( ev
->what
== mouseUp
)
373 event
.SetEventType(wxEVT_RIGHT_UP
) ;
375 event
.SetEventType(wxEVT_LEFT_UP
) ;
379 event
.SetEventType(wxEVT_MOTION
) ;
382 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
383 event
.m_controlDown
= ev
->modifiers
& controlKey
;
384 event
.m_altDown
= ev
->modifiers
& optionKey
;
385 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
387 Point localwhere
= ev
->where
;
391 ::SetPort( UMAGetWindowPort( m_macWindow
) ) ;
392 ::GlobalToLocal( &localwhere
) ;
395 if ( ev
->what
== mouseDown
)
397 if ( ev
->when
- lastWhen
<= GetDblTime() )
399 if ( abs( localwhere
.h
- lastWhere
.h
) < 3 || abs( localwhere
.v
- lastWhere
.v
) < 3 )
402 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
404 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
410 lastWhen
= ev
->when
;
412 lastWhere
= localwhere
;
415 event
.m_x
= localwhere
.h
;
416 event
.m_y
= localwhere
.v
;
421 wxPoint origin = GetClientAreaOrigin() ;
423 event.m_x += origin.x ;
424 event.m_y += origin.y ;
427 event
.m_timeStamp
= ev
->when
;
428 event
.SetEventObject(this);
429 if ( wxTheApp
->s_captureWindow
)
433 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
436 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
437 if ( ev
->what
== mouseUp
)
439 wxTheApp
->s_captureWindow
= NULL
;
440 if ( wxBusyCursorCount
== 0 )
442 m_cursor
.MacInstall() ;
448 MacDispatchMouseEvent( event
) ;
452 void wxTopLevelWindowMac::MacMouseDown( EventRecord
*ev
, short part
)
454 MacFireMouseEvent( ev
) ;
457 void wxTopLevelWindowMac::MacMouseUp( EventRecord
*ev
, short part
)
463 MacFireMouseEvent( ev
) ;
469 void wxTopLevelWindowMac::MacMouseMoved( EventRecord
*ev
, short part
)
475 MacFireMouseEvent( ev
) ;
480 void wxTopLevelWindowMac::MacActivate( EventRecord
*ev
, bool inIsActivating
)
482 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
483 event
.m_timeStamp
= ev
->when
;
484 event
.SetEventObject(this);
486 GetEventHandler()->ProcessEvent(event
);
488 UMAHighlightAndActivateWindow( m_macWindow
, inIsActivating
) ;
490 MacSuperEnabled( inIsActivating
) ;
493 void wxTopLevelWindowMac::MacKeyDown( EventRecord
*ev
)
497 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
499 wxWindow::SetTitle( title
) ;
503 if( wxApp::s_macDefaultEncodingIsPC
)
504 label
= wxMacMakeMacStringFromPC( m_label
) ;
508 UMASetWTitleC( m_macWindow
, label
) ;
511 bool wxTopLevelWindowMac::Show(bool show
)
513 if ( !wxWindow::Show(show
) )
518 ::ShowWindow( m_macWindow
) ;
519 ::SelectWindow( m_macWindow
) ;
520 // no need to generate events here, they will get them triggered by macos
521 // actually they should be , but apparently they are not
522 wxSize
size(m_width
, m_height
);
523 wxSizeEvent
event(size
, m_windowId
);
524 event
.SetEventObject(this);
525 GetEventHandler()->ProcessEvent(event
);
529 ::HideWindow( m_macWindow
) ;
543 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
547 int former_w
= m_width
;
548 int former_h
= m_height
;
550 int actualWidth
= width
;
551 int actualHeight
= height
;
555 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
556 actualWidth
= m_minWidth
;
557 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
558 actualHeight
= m_minHeight
;
559 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
560 actualWidth
= m_maxWidth
;
561 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
562 actualHeight
= m_maxHeight
;
564 bool doMove
= false ;
565 bool doResize
= false ;
567 if ( actualX
!= former_x
|| actualY
!= former_y
)
571 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
576 if ( doMove
|| doResize
)
580 m_width
= actualWidth
;
581 m_height
= actualHeight
;
584 ::MoveWindow(m_macWindow
, m_x
, m_y
, false); // don't make frontmost
587 ::SizeWindow(m_macWindow
, m_width
, m_height
, true);
589 // the OS takes care of invalidating and erasing the new area
590 // we have erased the old one
592 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
594 wxFrame
* frame
= (wxFrame
*) this ;
595 frame
->PositionStatusBar();
596 frame
->PositionToolBar();
599 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
601 MacRepositionScrollBars() ;
604 wxPoint
point(m_x
, m_y
);
605 wxMoveEvent
event(point
, m_windowId
);
606 event
.SetEventObject(this);
607 GetEventHandler()->ProcessEvent(event
) ;
611 MacRepositionScrollBars() ;
612 wxSize
size(m_width
, m_height
);
613 wxSizeEvent
event(size
, m_windowId
);
614 event
.SetEventObject(this);
615 GetEventHandler()->ProcessEvent(event
);
622 * Invalidation Mechanism
624 * The update mechanism reflects exactely the windows mechanism
625 * the rect gets added to the window invalidate region, if the eraseBackground flag
626 * has been true for any part of the update rgn the background is erased in the entire region
627 * not just in the specified rect.
629 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
630 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
631 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
632 * will get the eraseBackground event first
635 void wxTopLevelWindowMac::MacInvalidate( const Rect
* rect
, bool eraseBackground
)
638 GetPort( &formerPort
) ;
639 SetPortWindowPort( m_macWindow
) ;
641 m_macNeedsErasing
|= eraseBackground
;
643 // if we already know that we will have to erase, there's no need to track the rest
644 if ( !m_macNeedsErasing
)
646 // we end only here if eraseBackground is false
647 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
648 // we will have to erase anyway
650 RgnHandle updateRgn
= NewRgn();
651 RgnHandle diffRgn
= NewRgn() ;
652 if ( updateRgn
&& diffRgn
)
654 GetWindowUpdateRgn( m_macWindow
, updateRgn
);
656 LocalToGlobal( &pt
) ;
657 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
658 DiffRgn( updateRgn
, m_macNoEraseUpdateRgn
, diffRgn
) ;
659 if ( !EmptyRgn( diffRgn
) )
661 m_macNeedsErasing
= true ;
665 DisposeRgn( updateRgn
);
667 DisposeRgn( diffRgn
);
669 if ( !m_macNeedsErasing
)
671 RgnHandle rectRgn
= NewRgn() ;
672 SetRectRgn( rectRgn
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
) ;
673 UnionRgn( m_macNoEraseUpdateRgn
, rectRgn
, m_macNoEraseUpdateRgn
) ;
674 DisposeRgn( rectRgn
) ;
677 InvalWindowRect( m_macWindow
, rect
) ;
678 // turn this on to debug the refreshing cycle
679 #if wxMAC_DEBUG_REDRAW
682 SetPort( formerPort
) ;