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
) && !UMAMenuEvent(&rec
) && 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
;
326 wclass
= kPlainWindowClass
;
328 wclass
= kFloatingWindowClass
;
332 else if ( HasFlag( wxCAPTION
) )
334 if ( HasFlag( wxDIALOG_MODAL
) )
336 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
340 wclass
= kDocumentWindowClass
;
345 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
346 HasFlag( wxSYSTEM_MENU
) )
348 wclass
= kDocumentWindowClass
;
353 wclass
= kPlainWindowClass
;
355 wclass
= kModalWindowClass
;
360 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
362 attr
|= kWindowFullZoomAttribute
;
363 attr
|= kWindowCollapseBoxAttribute
;
365 if ( HasFlag( wxRESIZE_BORDER
) )
367 attr
|= kWindowResizableAttribute
;
369 if ( HasFlag( wxSYSTEM_MENU
) )
371 attr
|= kWindowCloseBoxAttribute
;
374 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
375 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
377 if( wxApp::s_macDefaultEncodingIsPC
)
378 label
= wxMacMakeMacStringFromPC( title
) ;
381 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
382 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
383 MacInstallEventHandler() ;
388 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
390 ((Point
*)localOrigin
)->h
= 0;
391 ((Point
*)localOrigin
)->v
= 0;
392 ((Rect
*)clipRect
)->left
= 0;
393 ((Rect
*)clipRect
)->top
= 0;
394 ((Rect
*)clipRect
)->right
= m_width
;
395 ((Rect
*)clipRect
)->bottom
= m_height
;
396 *window
= m_macWindow
;
400 void wxTopLevelWindowMac::Clear()
405 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
407 return m_macRootControl
;
411 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
414 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
416 BeginUpdate( (WindowRef
)m_macWindow
) ;
418 RgnHandle updateRgn
= NewRgn();
419 RgnHandle diffRgn
= NewRgn() ;
420 if ( updateRgn
&& diffRgn
)
422 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
423 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
424 if ( !EmptyRgn( updateRgn
) )
426 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
430 DisposeRgn( updateRgn
);
432 DisposeRgn( diffRgn
);
433 EndUpdate( (WindowRef
)m_macWindow
) ;
434 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
435 m_macNeedsErasing
= false ;
439 // Raise the window to the top of the Z order
440 void wxTopLevelWindowMac::Raise()
442 ::BringToFront( (WindowRef
)m_macWindow
) ;
445 // Lower the window to the bottom of the Z order
446 void wxTopLevelWindowMac::Lower()
448 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
451 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
453 EventRecord
*ev
= (EventRecord
*) evr
;
454 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
455 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
456 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
458 event
.m_leftDown
= isDown
&& !controlDown
;
460 event
.m_middleDown
= FALSE
;
461 event
.m_rightDown
= isDown
&& controlDown
;
463 if ( ev
->what
== mouseDown
)
466 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
468 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
470 else if ( ev
->what
== mouseUp
)
473 event
.SetEventType(wxEVT_RIGHT_UP
) ;
475 event
.SetEventType(wxEVT_LEFT_UP
) ;
479 event
.SetEventType(wxEVT_MOTION
) ;
482 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
483 event
.m_controlDown
= ev
->modifiers
& controlKey
;
484 event
.m_altDown
= ev
->modifiers
& optionKey
;
485 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
487 Point localwhere
= ev
->where
;
491 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
492 ::GlobalToLocal( &localwhere
) ;
495 if ( ev
->what
== mouseDown
)
497 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
499 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
501 // This is not right if the second mouse down
502 // event occured in a differen window. We
503 // correct this in MacDispatchMouseEvent.
505 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
507 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
513 gs_lastWhen
= ev
->when
;
515 gs_lastWhere
= localwhere
;
518 event
.m_x
= localwhere
.h
;
519 event
.m_y
= localwhere
.v
;
523 event
.m_timeStamp
= ev
->when
;
524 event
.SetEventObject(this);
525 if ( wxTheApp
->s_captureWindow
)
529 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
532 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
533 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
535 if ( ev
->what
== mouseUp
)
537 wxTheApp
->s_captureWindow
= NULL
;
538 if ( wxBusyCursorCount
== 0 )
540 m_cursor
.MacInstall() ;
546 MacDispatchMouseEvent( event
) ;
550 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
552 MacFireMouseEvent( ev
) ;
555 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
561 MacFireMouseEvent( ev
) ;
567 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
573 MacFireMouseEvent( ev
) ;
578 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
580 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
581 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
582 event
.SetEventObject(this);
584 GetEventHandler()->ProcessEvent(event
);
586 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
588 MacSuperEnabled( inIsActivating
) ;
591 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
595 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
597 wxWindow::SetTitle( title
) ;
601 if( wxApp::s_macDefaultEncodingIsPC
)
602 label
= wxMacMakeMacStringFromPC( m_label
) ;
606 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
609 bool wxTopLevelWindowMac::Show(bool show
)
611 if ( !wxWindow::Show(show
) )
616 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
617 ::SelectWindow( (WindowRef
)m_macWindow
) ;
618 // no need to generate events here, they will get them triggered by macos
619 // actually they should be , but apparently they are not
620 wxSize
size(m_width
, m_height
);
621 wxSizeEvent
event(size
, m_windowId
);
622 event
.SetEventObject(this);
623 GetEventHandler()->ProcessEvent(event
);
627 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
641 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
645 int former_w
= m_width
;
646 int former_h
= m_height
;
648 int actualWidth
= width
;
649 int actualHeight
= height
;
653 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
654 actualWidth
= m_minWidth
;
655 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
656 actualHeight
= m_minHeight
;
657 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
658 actualWidth
= m_maxWidth
;
659 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
660 actualHeight
= m_maxHeight
;
662 bool doMove
= false ;
663 bool doResize
= false ;
665 if ( actualX
!= former_x
|| actualY
!= former_y
)
669 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
674 if ( doMove
|| doResize
)
678 m_width
= actualWidth
;
679 m_height
= actualHeight
;
682 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
685 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
687 // the OS takes care of invalidating and erasing the new area so we only have to
688 // take care of refreshing for full repaints
690 if ( doResize
&& !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
694 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
696 wxFrame
* frame
= (wxFrame
*) this ;
697 frame
->PositionStatusBar();
698 frame
->PositionToolBar();
701 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
703 MacRepositionScrollBars() ;
706 wxPoint
point(m_x
, m_y
);
707 wxMoveEvent
event(point
, m_windowId
);
708 event
.SetEventObject(this);
709 GetEventHandler()->ProcessEvent(event
) ;
713 MacRepositionScrollBars() ;
714 wxSize
size(m_width
, m_height
);
715 wxSizeEvent
event(size
, m_windowId
);
716 event
.SetEventObject(this);
717 GetEventHandler()->ProcessEvent(event
);
724 * Invalidation Mechanism
726 * The update mechanism reflects exactely the windows mechanism
727 * the rect gets added to the window invalidate region, if the eraseBackground flag
728 * has been true for any part of the update rgn the background is erased in the entire region
729 * not just in the specified rect.
731 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
732 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
733 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
734 * will get the eraseBackground event first
737 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
740 GetPort( &formerPort
) ;
741 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
743 m_macNeedsErasing
|= eraseBackground
;
745 // if we already know that we will have to erase, there's no need to track the rest
746 if ( !m_macNeedsErasing
)
748 // we end only here if eraseBackground is false
749 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
750 // we will have to erase anyway
752 RgnHandle updateRgn
= NewRgn();
753 RgnHandle diffRgn
= NewRgn() ;
754 if ( updateRgn
&& diffRgn
)
756 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
758 LocalToGlobal( &pt
) ;
759 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
760 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
761 if ( !EmptyRgn( diffRgn
) )
763 m_macNeedsErasing
= true ;
767 DisposeRgn( updateRgn
);
769 DisposeRgn( diffRgn
);
771 if ( !m_macNeedsErasing
)
773 RgnHandle rectRgn
= NewRgn() ;
774 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
775 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
776 DisposeRgn( rectRgn
) ;
779 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
780 // turn this on to debug the refreshing cycle
781 #if wxMAC_DEBUG_REDRAW
784 SetPort( formerPort
) ;