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 wxWindow
* focus
= wxWindow::FindFocus() ;
253 // it is wxWindows Convention to have Ctrl Key Combinations at ASCII char value
254 if ( (rec
.modifiers
& controlKey
) && keychar
>= 0 && keychar
< 0x20 )
258 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
259 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent( focus
, keyval
, rec
.modifiers
, rec
.when
, rec
.where
.h
, rec
.where
.v
) )
261 // was handled internally
274 void wxTopLevelWindowMac::MacInstallEventHandler()
277 if ( wxMacWindowEventHandlerUPP
== NULL
)
279 wxMacWindowEventHandlerUPP
= NewEventHandlerUPP( wxMacWindowEventHandler
) ;
282 static const EventTypeSpec eventList
[] =
284 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
}
286 if ( m_macEventHandler
)
288 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
289 m_macEventHandler
= NULL
;
291 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), wxMacWindowEventHandlerUPP
, WXSIZEOF(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
295 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
299 const wxString
& name
)
302 m_windowStyle
= style
;
323 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
325 // translate the window attributes in the appropriate window class and attributes
327 WindowClass wclass
= 0;
328 WindowAttributes attr
= kWindowNoAttributes
;
330 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
333 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
334 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
335 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
338 wclass
= kFloatingWindowClass
;
339 if ( HasFlag(wxTINY_CAPTION_VERT
) )
341 attr
|= kWindowSideTitlebarAttribute
;
346 wclass
= kPlainWindowClass
;
349 else if ( HasFlag( wxCAPTION
) )
351 if ( HasFlag( wxDIALOG_MODAL
) )
353 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
357 wclass
= kDocumentWindowClass
;
362 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
363 HasFlag( wxSYSTEM_MENU
) )
365 wclass
= kDocumentWindowClass
;
369 wclass
= kPlainWindowClass
;
373 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
375 attr
|= kWindowFullZoomAttribute
;
376 attr
|= kWindowCollapseBoxAttribute
;
378 if ( HasFlag( wxRESIZE_BORDER
) )
380 attr
|= kWindowResizableAttribute
;
382 if ( HasFlag( wxSYSTEM_MENU
) )
384 attr
|= kWindowCloseBoxAttribute
;
387 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
388 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
390 if( wxApp::s_macDefaultEncodingIsPC
)
391 label
= wxMacMakeMacStringFromPC( title
) ;
394 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
395 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
396 MacInstallEventHandler() ;
401 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
403 ((Point
*)localOrigin
)->h
= 0;
404 ((Point
*)localOrigin
)->v
= 0;
405 ((Rect
*)clipRect
)->left
= 0;
406 ((Rect
*)clipRect
)->top
= 0;
407 ((Rect
*)clipRect
)->right
= m_width
;
408 ((Rect
*)clipRect
)->bottom
= m_height
;
409 *window
= m_macWindow
;
413 void wxTopLevelWindowMac::Clear()
418 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
420 return m_macRootControl
;
424 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
427 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
429 BeginUpdate( (WindowRef
)m_macWindow
) ;
431 RgnHandle updateRgn
= NewRgn();
432 RgnHandle diffRgn
= NewRgn() ;
433 if ( updateRgn
&& diffRgn
)
435 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
436 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
437 if ( !EmptyRgn( updateRgn
) )
439 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
443 DisposeRgn( updateRgn
);
445 DisposeRgn( diffRgn
);
446 EndUpdate( (WindowRef
)m_macWindow
) ;
447 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
448 m_macNeedsErasing
= false ;
452 // Raise the window to the top of the Z order
453 void wxTopLevelWindowMac::Raise()
455 ::BringToFront( (WindowRef
)m_macWindow
) ;
458 // Lower the window to the bottom of the Z order
459 void wxTopLevelWindowMac::Lower()
461 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
464 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
466 EventRecord
*ev
= (EventRecord
*) evr
;
467 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
468 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
469 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
471 event
.m_leftDown
= isDown
&& !controlDown
;
473 event
.m_middleDown
= FALSE
;
474 event
.m_rightDown
= isDown
&& controlDown
;
476 if ( ev
->what
== mouseDown
)
479 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
481 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
483 else if ( ev
->what
== mouseUp
)
486 event
.SetEventType(wxEVT_RIGHT_UP
) ;
488 event
.SetEventType(wxEVT_LEFT_UP
) ;
492 event
.SetEventType(wxEVT_MOTION
) ;
495 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
496 event
.m_controlDown
= ev
->modifiers
& controlKey
;
497 event
.m_altDown
= ev
->modifiers
& optionKey
;
498 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
500 Point localwhere
= ev
->where
;
504 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
505 ::GlobalToLocal( &localwhere
) ;
508 if ( ev
->what
== mouseDown
)
510 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
512 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
514 // This is not right if the second mouse down
515 // event occured in a differen window. We
516 // correct this in MacDispatchMouseEvent.
518 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
520 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
526 gs_lastWhen
= ev
->when
;
528 gs_lastWhere
= localwhere
;
531 event
.m_x
= localwhere
.h
;
532 event
.m_y
= localwhere
.v
;
536 event
.m_timeStamp
= ev
->when
;
537 event
.SetEventObject(this);
538 if ( wxTheApp
->s_captureWindow
)
542 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
545 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
546 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
548 if ( ev
->what
== mouseUp
)
550 wxTheApp
->s_captureWindow
= NULL
;
551 if ( wxBusyCursorCount
== 0 )
553 m_cursor
.MacInstall() ;
559 MacDispatchMouseEvent( event
) ;
563 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
565 MacFireMouseEvent( ev
) ;
568 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
574 MacFireMouseEvent( ev
) ;
580 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
586 MacFireMouseEvent( ev
) ;
591 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
593 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
594 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
595 event
.SetEventObject(this);
597 GetEventHandler()->ProcessEvent(event
);
599 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
601 MacSuperEnabled( inIsActivating
) ;
604 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
608 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
610 wxWindow::SetTitle( title
) ;
614 if( wxApp::s_macDefaultEncodingIsPC
)
615 label
= wxMacMakeMacStringFromPC( m_label
) ;
619 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
622 bool wxTopLevelWindowMac::Show(bool show
)
624 if ( !wxWindow::Show(show
) )
629 ::ShowWindow( (WindowRef
)m_macWindow
) ;
630 ::SelectWindow( (WindowRef
)m_macWindow
) ;
631 // no need to generate events here, they will get them triggered by macos
632 // actually they should be , but apparently they are not
633 wxSize
size(m_width
, m_height
);
634 wxSizeEvent
event(size
, m_windowId
);
635 event
.SetEventObject(this);
636 GetEventHandler()->ProcessEvent(event
);
640 ::HideWindow( (WindowRef
)m_macWindow
) ;
654 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
658 int former_w
= m_width
;
659 int former_h
= m_height
;
661 int actualWidth
= width
;
662 int actualHeight
= height
;
666 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
667 actualWidth
= m_minWidth
;
668 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
669 actualHeight
= m_minHeight
;
670 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
671 actualWidth
= m_maxWidth
;
672 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
673 actualHeight
= m_maxHeight
;
675 bool doMove
= false ;
676 bool doResize
= false ;
678 if ( actualX
!= former_x
|| actualY
!= former_y
)
682 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
687 if ( doMove
|| doResize
)
691 m_width
= actualWidth
;
692 m_height
= actualHeight
;
695 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
698 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
700 // the OS takes care of invalidating and erasing the new area
701 // we have erased the old one
703 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
705 wxFrame
* frame
= (wxFrame
*) this ;
706 frame
->PositionStatusBar();
707 frame
->PositionToolBar();
710 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
712 MacRepositionScrollBars() ;
715 wxPoint
point(m_x
, m_y
);
716 wxMoveEvent
event(point
, m_windowId
);
717 event
.SetEventObject(this);
718 GetEventHandler()->ProcessEvent(event
) ;
722 MacRepositionScrollBars() ;
723 wxSize
size(m_width
, m_height
);
724 wxSizeEvent
event(size
, m_windowId
);
725 event
.SetEventObject(this);
726 GetEventHandler()->ProcessEvent(event
);
733 * Invalidation Mechanism
735 * The update mechanism reflects exactely the windows mechanism
736 * the rect gets added to the window invalidate region, if the eraseBackground flag
737 * has been true for any part of the update rgn the background is erased in the entire region
738 * not just in the specified rect.
740 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
741 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
742 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
743 * will get the eraseBackground event first
746 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
749 GetPort( &formerPort
) ;
750 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
752 m_macNeedsErasing
|= eraseBackground
;
754 // if we already know that we will have to erase, there's no need to track the rest
755 if ( !m_macNeedsErasing
)
757 // we end only here if eraseBackground is false
758 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
759 // we will have to erase anyway
761 RgnHandle updateRgn
= NewRgn();
762 RgnHandle diffRgn
= NewRgn() ;
763 if ( updateRgn
&& diffRgn
)
765 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
767 LocalToGlobal( &pt
) ;
768 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
769 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
770 if ( !EmptyRgn( diffRgn
) )
772 m_macNeedsErasing
= true ;
776 DisposeRgn( updateRgn
);
778 DisposeRgn( diffRgn
);
780 if ( !m_macNeedsErasing
)
782 RgnHandle rectRgn
= NewRgn() ;
783 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
784 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
785 DisposeRgn( rectRgn
) ;
788 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
789 // turn this on to debug the refreshing cycle
790 #if wxMAC_DEBUG_REDRAW
793 SetPort( formerPort
) ;