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 wxTheApp
->m_macCurrentEvent
= &rec
;
237 wxWindow
* focus
= wxWindow::FindFocus() ;
238 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent( focus
, rec
.message
, rec
.modifiers
, rec
.when
, rec
.where
.h
, rec
.where
.v
) )
240 // was handled internally
253 void wxTopLevelWindowMac::MacInstallEventHandler()
256 if ( wxMacWindowEventHandlerUPP
== NULL
)
258 wxMacWindowEventHandlerUPP
= NewEventHandlerUPP( wxMacWindowEventHandler
) ;
261 static const EventTypeSpec eventList
[] =
263 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
}
265 if ( m_macEventHandler
)
267 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
268 m_macEventHandler
= NULL
;
270 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), wxMacWindowEventHandlerUPP
, WXSIZEOF(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
274 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
278 const wxString
& name
)
281 m_windowStyle
= style
;
302 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
304 // translate the window attributes in the appropriate window class and attributes
306 WindowClass wclass
= 0;
307 WindowAttributes attr
= kWindowNoAttributes
;
309 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
312 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
313 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
314 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
317 wclass
= kFloatingWindowClass
;
318 if ( HasFlag(wxTINY_CAPTION_VERT
) )
320 attr
|= kWindowSideTitlebarAttribute
;
325 wclass
= kPlainWindowClass
;
328 else if ( HasFlag( wxCAPTION
) )
330 if ( HasFlag( wxDIALOG_MODAL
) )
332 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
336 wclass
= kDocumentWindowClass
;
341 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
342 HasFlag( wxSYSTEM_MENU
) )
344 wclass
= kDocumentWindowClass
;
348 wclass
= kPlainWindowClass
;
352 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
354 attr
|= kWindowFullZoomAttribute
;
355 attr
|= kWindowCollapseBoxAttribute
;
357 if ( HasFlag( wxRESIZE_BORDER
) )
359 attr
|= kWindowResizableAttribute
;
361 if ( HasFlag( wxSYSTEM_MENU
) )
363 attr
|= kWindowCloseBoxAttribute
;
366 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
367 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
369 if( wxApp::s_macDefaultEncodingIsPC
)
370 label
= wxMacMakeMacStringFromPC( title
) ;
373 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
374 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
375 MacInstallEventHandler() ;
380 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
382 ((Point
*)localOrigin
)->h
= 0;
383 ((Point
*)localOrigin
)->v
= 0;
384 ((Rect
*)clipRect
)->left
= 0;
385 ((Rect
*)clipRect
)->top
= 0;
386 ((Rect
*)clipRect
)->right
= m_width
;
387 ((Rect
*)clipRect
)->bottom
= m_height
;
388 *window
= m_macWindow
;
392 void wxTopLevelWindowMac::Clear()
397 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
399 return m_macRootControl
;
403 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
406 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
408 BeginUpdate( (WindowRef
)m_macWindow
) ;
410 RgnHandle updateRgn
= NewRgn();
411 RgnHandle diffRgn
= NewRgn() ;
412 if ( updateRgn
&& diffRgn
)
414 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
415 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
416 if ( !EmptyRgn( updateRgn
) )
418 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
422 DisposeRgn( updateRgn
);
424 DisposeRgn( diffRgn
);
425 EndUpdate( (WindowRef
)m_macWindow
) ;
426 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
427 m_macNeedsErasing
= false ;
431 // Raise the window to the top of the Z order
432 void wxTopLevelWindowMac::Raise()
434 ::BringToFront( (WindowRef
)m_macWindow
) ;
437 // Lower the window to the bottom of the Z order
438 void wxTopLevelWindowMac::Lower()
440 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
443 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
445 EventRecord
*ev
= (EventRecord
*) evr
;
446 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
447 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
448 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
450 event
.m_leftDown
= isDown
&& !controlDown
;
452 event
.m_middleDown
= FALSE
;
453 event
.m_rightDown
= isDown
&& controlDown
;
455 if ( ev
->what
== mouseDown
)
458 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
460 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
462 else if ( ev
->what
== mouseUp
)
465 event
.SetEventType(wxEVT_RIGHT_UP
) ;
467 event
.SetEventType(wxEVT_LEFT_UP
) ;
471 event
.SetEventType(wxEVT_MOTION
) ;
474 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
475 event
.m_controlDown
= ev
->modifiers
& controlKey
;
476 event
.m_altDown
= ev
->modifiers
& optionKey
;
477 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
479 Point localwhere
= ev
->where
;
483 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
484 ::GlobalToLocal( &localwhere
) ;
487 if ( ev
->what
== mouseDown
)
489 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
491 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
493 // This is not right if the second mouse down
494 // event occured in a differen window. We
495 // correct this in MacDispatchMouseEvent.
497 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
499 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
505 gs_lastWhen
= ev
->when
;
507 gs_lastWhere
= localwhere
;
510 event
.m_x
= localwhere
.h
;
511 event
.m_y
= localwhere
.v
;
515 event
.m_timeStamp
= ev
->when
;
516 event
.SetEventObject(this);
517 if ( wxTheApp
->s_captureWindow
)
521 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
524 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
525 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
527 if ( ev
->what
== mouseUp
)
529 wxTheApp
->s_captureWindow
= NULL
;
530 if ( wxBusyCursorCount
== 0 )
532 m_cursor
.MacInstall() ;
538 MacDispatchMouseEvent( event
) ;
542 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
544 MacFireMouseEvent( ev
) ;
547 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
553 MacFireMouseEvent( ev
) ;
559 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
565 MacFireMouseEvent( ev
) ;
570 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
572 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
573 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
574 event
.SetEventObject(this);
576 GetEventHandler()->ProcessEvent(event
);
578 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
580 MacSuperEnabled( inIsActivating
) ;
583 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
587 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
589 wxWindow::SetTitle( title
) ;
593 if( wxApp::s_macDefaultEncodingIsPC
)
594 label
= wxMacMakeMacStringFromPC( m_label
) ;
598 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
601 bool wxTopLevelWindowMac::Show(bool show
)
603 if ( !wxWindow::Show(show
) )
608 ::ShowWindow( (WindowRef
)m_macWindow
) ;
609 ::SelectWindow( (WindowRef
)m_macWindow
) ;
610 // no need to generate events here, they will get them triggered by macos
611 // actually they should be , but apparently they are not
612 wxSize
size(m_width
, m_height
);
613 wxSizeEvent
event(size
, m_windowId
);
614 event
.SetEventObject(this);
615 GetEventHandler()->ProcessEvent(event
);
619 ::HideWindow( (WindowRef
)m_macWindow
) ;
633 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
637 int former_w
= m_width
;
638 int former_h
= m_height
;
640 int actualWidth
= width
;
641 int actualHeight
= height
;
645 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
646 actualWidth
= m_minWidth
;
647 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
648 actualHeight
= m_minHeight
;
649 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
650 actualWidth
= m_maxWidth
;
651 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
652 actualHeight
= m_maxHeight
;
654 bool doMove
= false ;
655 bool doResize
= false ;
657 if ( actualX
!= former_x
|| actualY
!= former_y
)
661 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
666 if ( doMove
|| doResize
)
670 m_width
= actualWidth
;
671 m_height
= actualHeight
;
674 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
677 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
679 // the OS takes care of invalidating and erasing the new area
680 // we have erased the old one
682 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
684 wxFrame
* frame
= (wxFrame
*) this ;
685 frame
->PositionStatusBar();
686 frame
->PositionToolBar();
689 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
691 MacRepositionScrollBars() ;
694 wxPoint
point(m_x
, m_y
);
695 wxMoveEvent
event(point
, m_windowId
);
696 event
.SetEventObject(this);
697 GetEventHandler()->ProcessEvent(event
) ;
701 MacRepositionScrollBars() ;
702 wxSize
size(m_width
, m_height
);
703 wxSizeEvent
event(size
, m_windowId
);
704 event
.SetEventObject(this);
705 GetEventHandler()->ProcessEvent(event
);
712 * Invalidation Mechanism
714 * The update mechanism reflects exactely the windows mechanism
715 * the rect gets added to the window invalidate region, if the eraseBackground flag
716 * has been true for any part of the update rgn the background is erased in the entire region
717 * not just in the specified rect.
719 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
720 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
721 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
722 * will get the eraseBackground event first
725 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
728 GetPort( &formerPort
) ;
729 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
731 m_macNeedsErasing
|= eraseBackground
;
733 // if we already know that we will have to erase, there's no need to track the rest
734 if ( !m_macNeedsErasing
)
736 // we end only here if eraseBackground is false
737 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
738 // we will have to erase anyway
740 RgnHandle updateRgn
= NewRgn();
741 RgnHandle diffRgn
= NewRgn() ;
742 if ( updateRgn
&& diffRgn
)
744 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
746 LocalToGlobal( &pt
) ;
747 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
748 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
749 if ( !EmptyRgn( diffRgn
) )
751 m_macNeedsErasing
= true ;
755 DisposeRgn( updateRgn
);
757 DisposeRgn( diffRgn
);
759 if ( !m_macNeedsErasing
)
761 RgnHandle rectRgn
= NewRgn() ;
762 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
763 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
764 DisposeRgn( rectRgn
) ;
767 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
768 // turn this on to debug the refreshing cycle
769 #if wxMAC_DEBUG_REDRAW
772 SetPort( formerPort
) ;