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 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
239 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
241 OSStatus result
= eventNotHandledErr
;
243 switch ( GetEventClass( event
) )
245 case kEventClassTextInput
:
246 if ( wxMacConvertEventToRecord( event
, &rec
) )
250 keychar
= short(rec
.message
& charCodeMask
);
251 keycode
= short(rec
.message
& keyCodeMask
) >> 8 ;
252 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
253 wxWindow
* focus
= wxWindow::FindFocus() ;
255 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent( focus
, keyval
, rec
.modifiers
, rec
.when
, rec
.where
.h
, rec
.where
.v
) )
257 // was handled internally
270 void wxTopLevelWindowMac::MacInstallEventHandler()
273 if ( wxMacWindowEventHandlerUPP
== NULL
)
275 wxMacWindowEventHandlerUPP
= NewEventHandlerUPP( wxMacWindowEventHandler
) ;
278 static const EventTypeSpec eventList
[] =
280 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
}
282 if ( m_macEventHandler
)
284 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
285 m_macEventHandler
= NULL
;
287 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), wxMacWindowEventHandlerUPP
, WXSIZEOF(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
291 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
295 const wxString
& name
)
298 m_windowStyle
= style
;
319 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
321 // translate the window attributes in the appropriate window class and attributes
323 WindowClass wclass
= 0;
324 WindowAttributes attr
= kWindowNoAttributes
;
326 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
329 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
330 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
331 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
334 wclass
= kFloatingWindowClass
;
335 if ( HasFlag(wxTINY_CAPTION_VERT
) )
337 attr
|= kWindowSideTitlebarAttribute
;
342 wclass
= kPlainWindowClass
;
345 else if ( HasFlag( wxCAPTION
) )
347 if ( HasFlag( wxDIALOG_MODAL
) )
349 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
353 wclass
= kDocumentWindowClass
;
358 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
359 HasFlag( wxSYSTEM_MENU
) )
361 wclass
= kDocumentWindowClass
;
365 wclass
= kPlainWindowClass
;
369 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
371 attr
|= kWindowFullZoomAttribute
;
372 attr
|= kWindowCollapseBoxAttribute
;
374 if ( HasFlag( wxRESIZE_BORDER
) )
376 attr
|= kWindowResizableAttribute
;
378 if ( HasFlag( wxSYSTEM_MENU
) )
380 attr
|= kWindowCloseBoxAttribute
;
383 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
384 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
386 if( wxApp::s_macDefaultEncodingIsPC
)
387 label
= wxMacMakeMacStringFromPC( title
) ;
390 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
391 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
392 MacInstallEventHandler() ;
397 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
399 ((Point
*)localOrigin
)->h
= 0;
400 ((Point
*)localOrigin
)->v
= 0;
401 ((Rect
*)clipRect
)->left
= 0;
402 ((Rect
*)clipRect
)->top
= 0;
403 ((Rect
*)clipRect
)->right
= m_width
;
404 ((Rect
*)clipRect
)->bottom
= m_height
;
405 *window
= m_macWindow
;
409 void wxTopLevelWindowMac::Clear()
414 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
416 return m_macRootControl
;
420 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
423 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
425 BeginUpdate( (WindowRef
)m_macWindow
) ;
427 RgnHandle updateRgn
= NewRgn();
428 RgnHandle diffRgn
= NewRgn() ;
429 if ( updateRgn
&& diffRgn
)
431 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
432 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
433 if ( !EmptyRgn( updateRgn
) )
435 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
439 DisposeRgn( updateRgn
);
441 DisposeRgn( diffRgn
);
442 EndUpdate( (WindowRef
)m_macWindow
) ;
443 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
444 m_macNeedsErasing
= false ;
448 // Raise the window to the top of the Z order
449 void wxTopLevelWindowMac::Raise()
451 ::BringToFront( (WindowRef
)m_macWindow
) ;
454 // Lower the window to the bottom of the Z order
455 void wxTopLevelWindowMac::Lower()
457 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
460 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
462 EventRecord
*ev
= (EventRecord
*) evr
;
463 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
464 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
465 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
467 event
.m_leftDown
= isDown
&& !controlDown
;
469 event
.m_middleDown
= FALSE
;
470 event
.m_rightDown
= isDown
&& controlDown
;
472 if ( ev
->what
== mouseDown
)
475 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
477 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
479 else if ( ev
->what
== mouseUp
)
482 event
.SetEventType(wxEVT_RIGHT_UP
) ;
484 event
.SetEventType(wxEVT_LEFT_UP
) ;
488 event
.SetEventType(wxEVT_MOTION
) ;
491 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
492 event
.m_controlDown
= ev
->modifiers
& controlKey
;
493 event
.m_altDown
= ev
->modifiers
& optionKey
;
494 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
496 Point localwhere
= ev
->where
;
500 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
501 ::GlobalToLocal( &localwhere
) ;
504 if ( ev
->what
== mouseDown
)
506 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
508 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
510 // This is not right if the second mouse down
511 // event occured in a differen window. We
512 // correct this in MacDispatchMouseEvent.
514 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
516 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
522 gs_lastWhen
= ev
->when
;
524 gs_lastWhere
= localwhere
;
527 event
.m_x
= localwhere
.h
;
528 event
.m_y
= localwhere
.v
;
532 event
.m_timeStamp
= ev
->when
;
533 event
.SetEventObject(this);
534 if ( wxTheApp
->s_captureWindow
)
538 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
541 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
542 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
544 if ( ev
->what
== mouseUp
)
546 wxTheApp
->s_captureWindow
= NULL
;
547 if ( wxBusyCursorCount
== 0 )
549 m_cursor
.MacInstall() ;
555 MacDispatchMouseEvent( event
) ;
559 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
561 MacFireMouseEvent( ev
) ;
564 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
570 MacFireMouseEvent( ev
) ;
576 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
582 MacFireMouseEvent( ev
) ;
587 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
589 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
590 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
591 event
.SetEventObject(this);
593 GetEventHandler()->ProcessEvent(event
);
595 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
597 MacSuperEnabled( inIsActivating
) ;
600 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
604 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
606 wxWindow::SetTitle( title
) ;
610 if( wxApp::s_macDefaultEncodingIsPC
)
611 label
= wxMacMakeMacStringFromPC( m_label
) ;
615 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
618 bool wxTopLevelWindowMac::Show(bool show
)
620 if ( !wxWindow::Show(show
) )
625 ::ShowWindow( (WindowRef
)m_macWindow
) ;
626 ::SelectWindow( (WindowRef
)m_macWindow
) ;
627 // no need to generate events here, they will get them triggered by macos
628 // actually they should be , but apparently they are not
629 wxSize
size(m_width
, m_height
);
630 wxSizeEvent
event(size
, m_windowId
);
631 event
.SetEventObject(this);
632 GetEventHandler()->ProcessEvent(event
);
636 ::HideWindow( (WindowRef
)m_macWindow
) ;
650 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
654 int former_w
= m_width
;
655 int former_h
= m_height
;
657 int actualWidth
= width
;
658 int actualHeight
= height
;
662 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
663 actualWidth
= m_minWidth
;
664 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
665 actualHeight
= m_minHeight
;
666 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
667 actualWidth
= m_maxWidth
;
668 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
669 actualHeight
= m_maxHeight
;
671 bool doMove
= false ;
672 bool doResize
= false ;
674 if ( actualX
!= former_x
|| actualY
!= former_y
)
678 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
683 if ( doMove
|| doResize
)
687 m_width
= actualWidth
;
688 m_height
= actualHeight
;
691 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
694 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
696 // the OS takes care of invalidating and erasing the new area
697 // we have erased the old one
699 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
701 wxFrame
* frame
= (wxFrame
*) this ;
702 frame
->PositionStatusBar();
703 frame
->PositionToolBar();
706 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
708 MacRepositionScrollBars() ;
711 wxPoint
point(m_x
, m_y
);
712 wxMoveEvent
event(point
, m_windowId
);
713 event
.SetEventObject(this);
714 GetEventHandler()->ProcessEvent(event
) ;
718 MacRepositionScrollBars() ;
719 wxSize
size(m_width
, m_height
);
720 wxSizeEvent
event(size
, m_windowId
);
721 event
.SetEventObject(this);
722 GetEventHandler()->ProcessEvent(event
);
729 * Invalidation Mechanism
731 * The update mechanism reflects exactely the windows mechanism
732 * the rect gets added to the window invalidate region, if the eraseBackground flag
733 * has been true for any part of the update rgn the background is erased in the entire region
734 * not just in the specified rect.
736 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
737 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
738 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
739 * will get the eraseBackground event first
742 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
745 GetPort( &formerPort
) ;
746 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
748 m_macNeedsErasing
|= eraseBackground
;
750 // if we already know that we will have to erase, there's no need to track the rest
751 if ( !m_macNeedsErasing
)
753 // we end only here if eraseBackground is false
754 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
755 // we will have to erase anyway
757 RgnHandle updateRgn
= NewRgn();
758 RgnHandle diffRgn
= NewRgn() ;
759 if ( updateRgn
&& diffRgn
)
761 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
763 LocalToGlobal( &pt
) ;
764 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
765 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
766 if ( !EmptyRgn( diffRgn
) )
768 m_macNeedsErasing
= true ;
772 DisposeRgn( updateRgn
);
774 DisposeRgn( diffRgn
);
776 if ( !m_macNeedsErasing
)
778 RgnHandle rectRgn
= NewRgn() ;
779 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
780 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
781 DisposeRgn( rectRgn
) ;
784 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
785 // turn this on to debug the refreshing cycle
786 #if wxMAC_DEBUG_REDRAW
789 SetPort( formerPort
) ;