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"
43 #include "wx/tooltip.h"
46 #define wxMAC_DEBUG_REDRAW 0
47 #ifndef wxMAC_DEBUG_REDRAW
48 #define wxMAC_DEBUG_REDRAW 0
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // list of all frames and modeless dialogs
56 wxWindowList wxModelessWindows
;
58 // double click testing
59 static Point gs_lastWhere
;
60 static long gs_lastWhen
= 0;
63 extern int wxBusyCursorCount
;
66 // ============================================================================
67 // wxTopLevelWindowMac implementation
68 // ============================================================================
70 // ---------------------------------------------------------------------------
71 // wxWindowMac utility functions
72 // ---------------------------------------------------------------------------
74 // Find an item given the Macintosh Window Reference
76 wxList
*wxWinMacWindowList
= NULL
;
77 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WXWindow inWindowRef
)
79 wxNode
*node
= wxWinMacWindowList
->Find((long)inWindowRef
);
82 return (wxTopLevelWindowMac
*)node
->Data();
85 void wxAssociateWinWithMacWindow(WXWindow inWindowRef
, wxTopLevelWindowMac
*win
)
87 // adding NULL WindowRef is (first) surely a result of an error and
88 // (secondly) breaks menu command processing
89 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, "attempt to add a NULL WindowRef to window list" );
91 if ( !wxWinMacWindowList
->Find((long)inWindowRef
) )
92 wxWinMacWindowList
->Append((long)inWindowRef
, win
);
95 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
97 wxWinMacWindowList
->DeleteObject(win
);
101 // ----------------------------------------------------------------------------
102 // wxTopLevelWindowMac creation
103 // ----------------------------------------------------------------------------
105 WXHWND
wxTopLevelWindowMac::s_macWindowInUpdate
= NULL
;
107 void wxTopLevelWindowMac::Init()
110 m_maximizeOnShow
= FALSE
;
111 m_macNoEraseUpdateRgn
= NewRgn() ;
112 m_macNeedsErasing
= false ;
114 m_macEventHandler
= NULL
;
117 class wxMacDeferredWindowDeleter
: public wxObject
120 wxMacDeferredWindowDeleter( WindowRef windowRef
)
122 m_macWindow
= windowRef
;
124 virtual ~wxMacDeferredWindowDeleter()
126 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
129 WindowRef m_macWindow
;
132 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
134 const wxString
& title
,
138 const wxString
& name
)
143 m_windowStyle
= style
;
147 m_windowId
= id
== -1 ? NewControlId() : id
;
149 wxTopLevelWindows
.Append(this);
152 parent
->AddChild(this);
157 wxTopLevelWindowMac::~wxTopLevelWindowMac()
161 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
162 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
166 if ( m_macEventHandler
)
168 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
169 m_macEventHandler
= NULL
;
172 wxRemoveMacWindowAssociation( this ) ;
174 wxTopLevelWindows
.DeleteObject(this);
176 if ( wxModelessWindows
.Find(this) )
177 wxModelessWindows
.DeleteObject(this);
179 // If this is the last top-level window, exit.
180 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
182 wxTheApp
->SetTopWindow(NULL
);
184 if ( wxTheApp
->GetExitOnFrameDelete() )
186 wxTheApp
->ExitMainLoop() ;
189 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
193 // ----------------------------------------------------------------------------
194 // wxTopLevelWindowMac maximize/minimize
195 // ----------------------------------------------------------------------------
197 void wxTopLevelWindowMac::Maximize(bool maximize
)
199 // not available on mac
202 bool wxTopLevelWindowMac::IsMaximized() const
207 void wxTopLevelWindowMac::Iconize(bool iconize
)
209 // not available on mac
212 bool wxTopLevelWindowMac::IsIconized() const
214 // mac dialogs cannot be iconized
218 void wxTopLevelWindowMac::Restore()
220 // not available on mac
223 // ----------------------------------------------------------------------------
224 // wxTopLevelWindowMac misc
225 // ----------------------------------------------------------------------------
227 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
230 wxTopLevelWindowBase::SetIcon(icon
);
235 EventHandlerUPP wxMacWindowEventHandlerUPP
= NULL
;
237 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
239 OSStatus result
= eventNotHandledErr
;
241 switch ( GetEventClass( event
) )
243 case kEventClassTextInput
:
244 if ( wxMacConvertEventToRecord( event
, &rec
) )
246 wxTheApp
->MacHandleOneEvent( &rec
) ;
258 void wxTopLevelWindowMac::MacInstallEventHandler()
261 if ( wxMacWindowEventHandlerUPP
== NULL
)
263 wxMacWindowEventHandlerUPP
= NewEventHandlerUPP( wxMacWindowEventHandler
) ;
266 static const EventTypeSpec eventList
[] =
268 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
}
270 if ( m_macEventHandler
)
272 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
273 m_macEventHandler
= NULL
;
275 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), wxMacWindowEventHandlerUPP
, WXSIZEOF(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
279 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
283 const wxString
& name
)
286 m_windowStyle
= style
;
307 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
309 // translate the window attributes in the appropriate window class and attributes
311 WindowClass wclass
= 0;
312 WindowAttributes attr
= kWindowNoAttributes
;
314 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
317 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
318 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
319 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
322 wclass
= kFloatingWindowClass
;
323 if ( HasFlag(wxTINY_CAPTION_VERT
) )
325 attr
|= kWindowSideTitlebarAttribute
;
330 wclass
= kPlainWindowClass
;
333 else if ( HasFlag( wxCAPTION
) )
335 if ( HasFlag( wxDIALOG_MODAL
) )
337 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
341 wclass
= kDocumentWindowClass
;
346 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
347 HasFlag( wxSYSTEM_MENU
) )
349 wclass
= kDocumentWindowClass
;
353 wclass
= kPlainWindowClass
;
357 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
359 attr
|= kWindowFullZoomAttribute
;
360 attr
|= kWindowCollapseBoxAttribute
;
362 if ( HasFlag( wxRESIZE_BORDER
) )
364 attr
|= kWindowResizableAttribute
;
366 if ( HasFlag( wxSYSTEM_MENU
) )
368 attr
|= kWindowCloseBoxAttribute
;
371 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
372 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
374 if( wxApp::s_macDefaultEncodingIsPC
)
375 label
= wxMacMakeMacStringFromPC( title
) ;
378 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
379 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
380 MacInstallEventHandler() ;
385 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
387 ((Point
*)localOrigin
)->h
= 0;
388 ((Point
*)localOrigin
)->v
= 0;
389 ((Rect
*)clipRect
)->left
= 0;
390 ((Rect
*)clipRect
)->top
= 0;
391 ((Rect
*)clipRect
)->right
= m_width
;
392 ((Rect
*)clipRect
)->bottom
= m_height
;
393 *window
= m_macWindow
;
397 void wxTopLevelWindowMac::Clear()
402 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
404 return m_macRootControl
;
408 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
411 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
413 BeginUpdate( (WindowRef
)m_macWindow
) ;
415 RgnHandle updateRgn
= NewRgn();
416 RgnHandle diffRgn
= NewRgn() ;
417 if ( updateRgn
&& diffRgn
)
419 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
420 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
421 if ( !EmptyRgn( updateRgn
) )
423 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
427 DisposeRgn( updateRgn
);
429 DisposeRgn( diffRgn
);
430 EndUpdate( (WindowRef
)m_macWindow
) ;
431 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
432 m_macNeedsErasing
= false ;
436 // Raise the window to the top of the Z order
437 void wxTopLevelWindowMac::Raise()
439 ::BringToFront( (WindowRef
)m_macWindow
) ;
442 // Lower the window to the bottom of the Z order
443 void wxTopLevelWindowMac::Lower()
445 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
448 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
450 EventRecord
*ev
= (EventRecord
*) evr
;
451 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
452 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
453 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
455 event
.m_leftDown
= isDown
&& !controlDown
;
457 event
.m_middleDown
= FALSE
;
458 event
.m_rightDown
= isDown
&& controlDown
;
460 if ( ev
->what
== mouseDown
)
463 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
465 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
467 else if ( ev
->what
== mouseUp
)
470 event
.SetEventType(wxEVT_RIGHT_UP
) ;
472 event
.SetEventType(wxEVT_LEFT_UP
) ;
476 event
.SetEventType(wxEVT_MOTION
) ;
479 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
480 event
.m_controlDown
= ev
->modifiers
& controlKey
;
481 event
.m_altDown
= ev
->modifiers
& optionKey
;
482 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
484 Point localwhere
= ev
->where
;
488 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
489 ::GlobalToLocal( &localwhere
) ;
492 if ( ev
->what
== mouseDown
)
494 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
496 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
498 // This is not right if the second mouse down
499 // event occured in a differen window. We
500 // correct this in MacDispatchMouseEvent.
502 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
504 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
510 gs_lastWhen
= ev
->when
;
512 gs_lastWhere
= localwhere
;
515 event
.m_x
= localwhere
.h
;
516 event
.m_y
= localwhere
.v
;
520 event
.m_timeStamp
= ev
->when
;
521 event
.SetEventObject(this);
522 if ( wxTheApp
->s_captureWindow
)
526 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
529 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
530 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
532 if ( ev
->what
== mouseUp
)
534 wxTheApp
->s_captureWindow
= NULL
;
535 if ( wxBusyCursorCount
== 0 )
537 m_cursor
.MacInstall() ;
543 MacDispatchMouseEvent( event
) ;
547 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
549 MacFireMouseEvent( ev
) ;
552 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
558 MacFireMouseEvent( ev
) ;
564 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
570 MacFireMouseEvent( ev
) ;
575 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
577 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
578 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
579 event
.SetEventObject(this);
581 GetEventHandler()->ProcessEvent(event
);
583 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
585 MacSuperEnabled( inIsActivating
) ;
588 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
592 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
594 wxWindow::SetTitle( title
) ;
598 if( wxApp::s_macDefaultEncodingIsPC
)
599 label
= wxMacMakeMacStringFromPC( m_label
) ;
603 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
606 bool wxTopLevelWindowMac::Show(bool show
)
608 if ( !wxWindow::Show(show
) )
613 ::ShowWindow( (WindowRef
)m_macWindow
) ;
614 ::SelectWindow( (WindowRef
)m_macWindow
) ;
615 // no need to generate events here, they will get them triggered by macos
616 // actually they should be , but apparently they are not
617 wxSize
size(m_width
, m_height
);
618 wxSizeEvent
event(size
, m_windowId
);
619 event
.SetEventObject(this);
620 GetEventHandler()->ProcessEvent(event
);
624 ::HideWindow( (WindowRef
)m_macWindow
) ;
638 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
642 int former_w
= m_width
;
643 int former_h
= m_height
;
645 int actualWidth
= width
;
646 int actualHeight
= height
;
650 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
651 actualWidth
= m_minWidth
;
652 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
653 actualHeight
= m_minHeight
;
654 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
655 actualWidth
= m_maxWidth
;
656 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
657 actualHeight
= m_maxHeight
;
659 bool doMove
= false ;
660 bool doResize
= false ;
662 if ( actualX
!= former_x
|| actualY
!= former_y
)
666 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
671 if ( doMove
|| doResize
)
675 m_width
= actualWidth
;
676 m_height
= actualHeight
;
679 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
682 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
684 // the OS takes care of invalidating and erasing the new area
685 // we have erased the old one
687 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
689 wxFrame
* frame
= (wxFrame
*) this ;
690 frame
->PositionStatusBar();
691 frame
->PositionToolBar();
694 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
696 MacRepositionScrollBars() ;
699 wxPoint
point(m_x
, m_y
);
700 wxMoveEvent
event(point
, m_windowId
);
701 event
.SetEventObject(this);
702 GetEventHandler()->ProcessEvent(event
) ;
706 MacRepositionScrollBars() ;
707 wxSize
size(m_width
, m_height
);
708 wxSizeEvent
event(size
, m_windowId
);
709 event
.SetEventObject(this);
710 GetEventHandler()->ProcessEvent(event
);
717 * Invalidation Mechanism
719 * The update mechanism reflects exactely the windows mechanism
720 * the rect gets added to the window invalidate region, if the eraseBackground flag
721 * has been true for any part of the update rgn the background is erased in the entire region
722 * not just in the specified rect.
724 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
725 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
726 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
727 * will get the eraseBackground event first
730 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
733 GetPort( &formerPort
) ;
734 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
736 m_macNeedsErasing
|= eraseBackground
;
738 // if we already know that we will have to erase, there's no need to track the rest
739 if ( !m_macNeedsErasing
)
741 // we end only here if eraseBackground is false
742 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
743 // we will have to erase anyway
745 RgnHandle updateRgn
= NewRgn();
746 RgnHandle diffRgn
= NewRgn() ;
747 if ( updateRgn
&& diffRgn
)
749 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
751 LocalToGlobal( &pt
) ;
752 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
753 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
754 if ( !EmptyRgn( diffRgn
) )
756 m_macNeedsErasing
= true ;
760 DisposeRgn( updateRgn
);
762 DisposeRgn( diffRgn
);
764 if ( !m_macNeedsErasing
)
766 RgnHandle rectRgn
= NewRgn() ;
767 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
768 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
769 DisposeRgn( rectRgn
) ;
772 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
773 // turn this on to debug the refreshing cycle
774 #if wxMAC_DEBUG_REDRAW
777 SetPort( formerPort
) ;