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 if ( wxModelessWindows
.Find(this) )
175 wxModelessWindows
.DeleteObject(this);
177 DisposeRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
181 // ----------------------------------------------------------------------------
182 // wxTopLevelWindowMac maximize/minimize
183 // ----------------------------------------------------------------------------
185 void wxTopLevelWindowMac::Maximize(bool maximize
)
187 // not available on mac
190 bool wxTopLevelWindowMac::IsMaximized() const
195 void wxTopLevelWindowMac::Iconize(bool iconize
)
197 // not available on mac
200 bool wxTopLevelWindowMac::IsIconized() const
202 // mac dialogs cannot be iconized
206 void wxTopLevelWindowMac::Restore()
208 // not available on mac
211 // ----------------------------------------------------------------------------
212 // wxTopLevelWindowMac misc
213 // ----------------------------------------------------------------------------
215 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
218 wxTopLevelWindowBase::SetIcon(icon
);
223 EventHandlerUPP wxMacWindowEventHandlerUPP
= NULL
;
225 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
227 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
229 OSStatus result
= eventNotHandledErr
;
231 switch ( GetEventClass( event
) )
233 case kEventClassTextInput
:
234 if ( wxMacConvertEventToRecord( event
, &rec
) )
236 wxWindow
* focus
= wxWindow::FindFocus() ;
237 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent( focus
, rec
.message
, rec
.modifiers
, rec
.when
, rec
.where
.h
, rec
.where
.v
) )
239 // was handled internally
252 void wxTopLevelWindowMac::MacInstallEventHandler()
255 if ( wxMacWindowEventHandlerUPP
== NULL
)
257 wxMacWindowEventHandlerUPP
= NewEventHandlerUPP( wxMacWindowEventHandler
) ;
260 static const EventTypeSpec eventList
[] =
262 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
}
264 if ( m_macEventHandler
)
266 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
267 m_macEventHandler
= NULL
;
269 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), wxMacWindowEventHandlerUPP
, WXSIZEOF(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
273 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
277 const wxString
& name
)
280 m_windowStyle
= style
;
301 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
303 // translate the window attributes in the appropriate window class and attributes
305 WindowClass wclass
= 0;
306 WindowAttributes attr
= kWindowNoAttributes
;
308 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
311 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
312 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
313 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
316 wclass
= kFloatingWindowClass
;
317 if ( HasFlag(wxTINY_CAPTION_VERT
) )
319 attr
|= kWindowSideTitlebarAttribute
;
324 wclass
= kPlainWindowClass
;
327 else if ( HasFlag( wxCAPTION
) )
329 if ( HasFlag( wxDIALOG_MODAL
) )
331 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
335 wclass
= kDocumentWindowClass
;
340 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
341 HasFlag( wxSYSTEM_MENU
) )
343 wclass
= kDocumentWindowClass
;
347 wclass
= kPlainWindowClass
;
351 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
353 attr
|= kWindowFullZoomAttribute
;
354 attr
|= kWindowCollapseBoxAttribute
;
356 if ( HasFlag( wxRESIZE_BORDER
) )
358 attr
|= kWindowResizableAttribute
;
360 if ( HasFlag( wxSYSTEM_MENU
) )
362 attr
|= kWindowCloseBoxAttribute
;
365 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
366 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
368 if( wxApp::s_macDefaultEncodingIsPC
)
369 label
= wxMacMakeMacStringFromPC( title
) ;
372 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
373 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
374 MacInstallEventHandler() ;
379 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
381 ((Point
*)localOrigin
)->h
= 0;
382 ((Point
*)localOrigin
)->v
= 0;
383 ((Rect
*)clipRect
)->left
= 0;
384 ((Rect
*)clipRect
)->top
= 0;
385 ((Rect
*)clipRect
)->right
= m_width
;
386 ((Rect
*)clipRect
)->bottom
= m_height
;
387 *window
= m_macWindow
;
391 void wxTopLevelWindowMac::Clear()
396 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
398 return m_macRootControl
;
402 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
405 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
407 BeginUpdate( (WindowRef
)m_macWindow
) ;
409 RgnHandle updateRgn
= NewRgn();
410 RgnHandle diffRgn
= NewRgn() ;
411 if ( updateRgn
&& diffRgn
)
413 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
414 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
415 if ( !EmptyRgn( updateRgn
) )
417 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
421 DisposeRgn( updateRgn
);
423 DisposeRgn( diffRgn
);
424 EndUpdate( (WindowRef
)m_macWindow
) ;
425 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
426 m_macNeedsErasing
= false ;
430 // Raise the window to the top of the Z order
431 void wxTopLevelWindowMac::Raise()
433 ::BringToFront( (WindowRef
)m_macWindow
) ;
436 // Lower the window to the bottom of the Z order
437 void wxTopLevelWindowMac::Lower()
439 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
442 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
444 EventRecord
*ev
= (EventRecord
*) evr
;
445 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
446 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
447 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
449 event
.m_leftDown
= isDown
&& !controlDown
;
451 event
.m_middleDown
= FALSE
;
452 event
.m_rightDown
= isDown
&& controlDown
;
454 if ( ev
->what
== mouseDown
)
457 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
459 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
461 else if ( ev
->what
== mouseUp
)
464 event
.SetEventType(wxEVT_RIGHT_UP
) ;
466 event
.SetEventType(wxEVT_LEFT_UP
) ;
470 event
.SetEventType(wxEVT_MOTION
) ;
473 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
474 event
.m_controlDown
= ev
->modifiers
& controlKey
;
475 event
.m_altDown
= ev
->modifiers
& optionKey
;
476 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
478 Point localwhere
= ev
->where
;
482 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
483 ::GlobalToLocal( &localwhere
) ;
486 if ( ev
->what
== mouseDown
)
488 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
490 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
492 // This is not right if the second mouse down
493 // event occured in a differen window. We
494 // correct this in MacDispatchMouseEvent.
496 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
498 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
504 gs_lastWhen
= ev
->when
;
506 gs_lastWhere
= localwhere
;
509 event
.m_x
= localwhere
.h
;
510 event
.m_y
= localwhere
.v
;
514 event
.m_timeStamp
= ev
->when
;
515 event
.SetEventObject(this);
516 if ( wxTheApp
->s_captureWindow
)
520 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
523 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
524 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
526 if ( ev
->what
== mouseUp
)
528 wxTheApp
->s_captureWindow
= NULL
;
529 if ( wxBusyCursorCount
== 0 )
531 m_cursor
.MacInstall() ;
537 MacDispatchMouseEvent( event
) ;
541 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
543 MacFireMouseEvent( ev
) ;
546 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
552 MacFireMouseEvent( ev
) ;
558 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
564 MacFireMouseEvent( ev
) ;
569 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
571 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
572 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
573 event
.SetEventObject(this);
575 GetEventHandler()->ProcessEvent(event
);
577 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
579 MacSuperEnabled( inIsActivating
) ;
582 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
586 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
588 wxWindow::SetTitle( title
) ;
592 if( wxApp::s_macDefaultEncodingIsPC
)
593 label
= wxMacMakeMacStringFromPC( m_label
) ;
597 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
600 bool wxTopLevelWindowMac::Show(bool show
)
602 if ( !wxWindow::Show(show
) )
607 ::ShowWindow( (WindowRef
)m_macWindow
) ;
608 ::SelectWindow( (WindowRef
)m_macWindow
) ;
609 // no need to generate events here, they will get them triggered by macos
610 // actually they should be , but apparently they are not
611 wxSize
size(m_width
, m_height
);
612 wxSizeEvent
event(size
, m_windowId
);
613 event
.SetEventObject(this);
614 GetEventHandler()->ProcessEvent(event
);
618 ::HideWindow( (WindowRef
)m_macWindow
) ;
632 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
636 int former_w
= m_width
;
637 int former_h
= m_height
;
639 int actualWidth
= width
;
640 int actualHeight
= height
;
644 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
645 actualWidth
= m_minWidth
;
646 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
647 actualHeight
= m_minHeight
;
648 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
649 actualWidth
= m_maxWidth
;
650 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
651 actualHeight
= m_maxHeight
;
653 bool doMove
= false ;
654 bool doResize
= false ;
656 if ( actualX
!= former_x
|| actualY
!= former_y
)
660 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
665 if ( doMove
|| doResize
)
669 m_width
= actualWidth
;
670 m_height
= actualHeight
;
673 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
676 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
678 // the OS takes care of invalidating and erasing the new area
679 // we have erased the old one
681 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
683 wxFrame
* frame
= (wxFrame
*) this ;
684 frame
->PositionStatusBar();
685 frame
->PositionToolBar();
688 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
690 MacRepositionScrollBars() ;
693 wxPoint
point(m_x
, m_y
);
694 wxMoveEvent
event(point
, m_windowId
);
695 event
.SetEventObject(this);
696 GetEventHandler()->ProcessEvent(event
) ;
700 MacRepositionScrollBars() ;
701 wxSize
size(m_width
, m_height
);
702 wxSizeEvent
event(size
, m_windowId
);
703 event
.SetEventObject(this);
704 GetEventHandler()->ProcessEvent(event
);
711 * Invalidation Mechanism
713 * The update mechanism reflects exactely the windows mechanism
714 * the rect gets added to the window invalidate region, if the eraseBackground flag
715 * has been true for any part of the update rgn the background is erased in the entire region
716 * not just in the specified rect.
718 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
719 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
720 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
721 * will get the eraseBackground event first
724 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
727 GetPort( &formerPort
) ;
728 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
730 m_macNeedsErasing
|= eraseBackground
;
732 // if we already know that we will have to erase, there's no need to track the rest
733 if ( !m_macNeedsErasing
)
735 // we end only here if eraseBackground is false
736 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
737 // we will have to erase anyway
739 RgnHandle updateRgn
= NewRgn();
740 RgnHandle diffRgn
= NewRgn() ;
741 if ( updateRgn
&& diffRgn
)
743 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
745 LocalToGlobal( &pt
) ;
746 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
747 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
748 if ( !EmptyRgn( diffRgn
) )
750 m_macNeedsErasing
= true ;
754 DisposeRgn( updateRgn
);
756 DisposeRgn( diffRgn
);
758 if ( !m_macNeedsErasing
)
760 RgnHandle rectRgn
= NewRgn() ;
761 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
762 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
763 DisposeRgn( rectRgn
) ;
766 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
767 // turn this on to debug the refreshing cycle
768 #if wxMAC_DEBUG_REDRAW
771 SetPort( formerPort
) ;