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
) )
238 keychar
= short(rec
.message
& charCodeMask
);
239 keycode
= short(rec
.message
& keyCodeMask
) >> 8 ;
240 wxWindow
* focus
= wxWindow::FindFocus() ;
241 // it is wxWindows Convention to have Ctrl Key Combinations at ASCII char value
242 if ( (rec
.modifiers
& controlKey
) && keychar
>= 0 && keychar
< 0x20 )
246 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
247 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyDownEvent( focus
, keyval
, rec
.modifiers
, rec
.when
, rec
.where
.h
, rec
.where
.v
) )
249 // was handled internally
262 void wxTopLevelWindowMac::MacInstallEventHandler()
265 if ( wxMacWindowEventHandlerUPP
== NULL
)
267 wxMacWindowEventHandlerUPP
= NewEventHandlerUPP( wxMacWindowEventHandler
) ;
270 static const EventTypeSpec eventList
[] =
272 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
}
274 if ( m_macEventHandler
)
276 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
277 m_macEventHandler
= NULL
;
279 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), wxMacWindowEventHandlerUPP
, WXSIZEOF(eventList
), eventList
, this, &((EventHandlerRef
)m_macEventHandler
));
283 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
287 const wxString
& name
)
290 m_windowStyle
= style
;
311 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
313 // translate the window attributes in the appropriate window class and attributes
315 WindowClass wclass
= 0;
316 WindowAttributes attr
= kWindowNoAttributes
;
318 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
321 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
322 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
323 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
326 wclass
= kFloatingWindowClass
;
327 if ( HasFlag(wxTINY_CAPTION_VERT
) )
329 attr
|= kWindowSideTitlebarAttribute
;
334 wclass
= kPlainWindowClass
;
337 else if ( HasFlag( wxCAPTION
) )
339 if ( HasFlag( wxDIALOG_MODAL
) )
341 wclass
= kDocumentWindowClass
; // kMovableModalWindowClass ;
345 wclass
= kDocumentWindowClass
;
350 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
351 HasFlag( wxSYSTEM_MENU
) )
353 wclass
= kDocumentWindowClass
;
357 wclass
= kPlainWindowClass
;
361 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) )
363 attr
|= kWindowFullZoomAttribute
;
364 attr
|= kWindowCollapseBoxAttribute
;
366 if ( HasFlag( wxRESIZE_BORDER
) )
368 attr
|= kWindowResizableAttribute
;
370 if ( HasFlag( wxSYSTEM_MENU
) )
372 attr
|= kWindowCloseBoxAttribute
;
375 ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
376 wxAssociateWinWithMacWindow( m_macWindow
, this ) ;
378 if( wxApp::s_macDefaultEncodingIsPC
)
379 label
= wxMacMakeMacStringFromPC( title
) ;
382 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
383 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlHandle
*)&m_macRootControl
) ;
384 MacInstallEventHandler() ;
389 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin
, WXRECTPTR clipRect
, WXHWND
*window
, wxWindowMac
** rootwin
)
391 ((Point
*)localOrigin
)->h
= 0;
392 ((Point
*)localOrigin
)->v
= 0;
393 ((Rect
*)clipRect
)->left
= 0;
394 ((Rect
*)clipRect
)->top
= 0;
395 ((Rect
*)clipRect
)->right
= m_width
;
396 ((Rect
*)clipRect
)->bottom
= m_height
;
397 *window
= m_macWindow
;
401 void wxTopLevelWindowMac::Clear()
406 WXWidget
wxTopLevelWindowMac::MacGetContainerForEmbedding()
408 return m_macRootControl
;
412 void wxTopLevelWindowMac::MacUpdate( long timestamp
)
415 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
417 BeginUpdate( (WindowRef
)m_macWindow
) ;
419 RgnHandle updateRgn
= NewRgn();
420 RgnHandle diffRgn
= NewRgn() ;
421 if ( updateRgn
&& diffRgn
)
423 GetPortVisibleRegion( GetWindowPort( (WindowRef
)m_macWindow
), updateRgn
);
424 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
425 if ( !EmptyRgn( updateRgn
) )
427 MacRedraw( updateRgn
, timestamp
, m_macNeedsErasing
|| !EmptyRgn( diffRgn
) ) ;
431 DisposeRgn( updateRgn
);
433 DisposeRgn( diffRgn
);
434 EndUpdate( (WindowRef
)m_macWindow
) ;
435 SetEmptyRgn( (RgnHandle
) m_macNoEraseUpdateRgn
) ;
436 m_macNeedsErasing
= false ;
440 // Raise the window to the top of the Z order
441 void wxTopLevelWindowMac::Raise()
443 ::BringToFront( (WindowRef
)m_macWindow
) ;
446 // Lower the window to the bottom of the Z order
447 void wxTopLevelWindowMac::Lower()
449 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
452 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr
)
454 EventRecord
*ev
= (EventRecord
*) evr
;
455 wxMouseEvent
event(wxEVT_LEFT_DOWN
);
456 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
457 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
459 event
.m_leftDown
= isDown
&& !controlDown
;
461 event
.m_middleDown
= FALSE
;
462 event
.m_rightDown
= isDown
&& controlDown
;
464 if ( ev
->what
== mouseDown
)
467 event
.SetEventType(wxEVT_RIGHT_DOWN
) ;
469 event
.SetEventType(wxEVT_LEFT_DOWN
) ;
471 else if ( ev
->what
== mouseUp
)
474 event
.SetEventType(wxEVT_RIGHT_UP
) ;
476 event
.SetEventType(wxEVT_LEFT_UP
) ;
480 event
.SetEventType(wxEVT_MOTION
) ;
483 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
484 event
.m_controlDown
= ev
->modifiers
& controlKey
;
485 event
.m_altDown
= ev
->modifiers
& optionKey
;
486 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
488 Point localwhere
= ev
->where
;
492 ::SetPort( UMAGetWindowPort( (WindowRef
)m_macWindow
) ) ;
493 ::GlobalToLocal( &localwhere
) ;
496 if ( ev
->what
== mouseDown
)
498 if ( ev
->when
- gs_lastWhen
<= GetDblTime() )
500 if ( abs( localwhere
.h
- gs_lastWhere
.h
) < 3 && abs( localwhere
.v
- gs_lastWhere
.v
) < 3 )
502 // This is not right if the second mouse down
503 // event occured in a differen window. We
504 // correct this in MacDispatchMouseEvent.
506 event
.SetEventType(wxEVT_RIGHT_DCLICK
) ;
508 event
.SetEventType(wxEVT_LEFT_DCLICK
) ;
514 gs_lastWhen
= ev
->when
;
516 gs_lastWhere
= localwhere
;
519 event
.m_x
= localwhere
.h
;
520 event
.m_y
= localwhere
.v
;
524 event
.m_timeStamp
= ev
->when
;
525 event
.SetEventObject(this);
526 if ( wxTheApp
->s_captureWindow
)
530 wxTheApp
->s_captureWindow
->ScreenToClient( &x
, &y
) ;
533 event
.SetEventObject( wxTheApp
->s_captureWindow
) ;
534 wxTheApp
->s_captureWindow
->GetEventHandler()->ProcessEvent( event
) ;
536 if ( ev
->what
== mouseUp
)
538 wxTheApp
->s_captureWindow
= NULL
;
539 if ( wxBusyCursorCount
== 0 )
541 m_cursor
.MacInstall() ;
547 MacDispatchMouseEvent( event
) ;
551 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev
, short part
)
553 MacFireMouseEvent( ev
) ;
556 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev
, short part
)
562 MacFireMouseEvent( ev
) ;
568 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev
, short part
)
574 MacFireMouseEvent( ev
) ;
579 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev
, bool inIsActivating
)
581 wxActivateEvent
event(wxEVT_ACTIVATE
, inIsActivating
, m_windowId
);
582 event
.m_timeStamp
= ((EventRecord
*)ev
)->when
;
583 event
.SetEventObject(this);
585 GetEventHandler()->ProcessEvent(event
);
587 UMAHighlightAndActivateWindow( (WindowRef
)m_macWindow
, inIsActivating
) ;
589 MacSuperEnabled( inIsActivating
) ;
592 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev
)
596 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
598 wxWindow::SetTitle( title
) ;
602 if( wxApp::s_macDefaultEncodingIsPC
)
603 label
= wxMacMakeMacStringFromPC( m_label
) ;
607 UMASetWTitleC( (WindowRef
)m_macWindow
, label
) ;
610 bool wxTopLevelWindowMac::Show(bool show
)
612 if ( !wxWindow::Show(show
) )
617 ::ShowWindow( (WindowRef
)m_macWindow
) ;
618 ::SelectWindow( (WindowRef
)m_macWindow
) ;
619 // no need to generate events here, they will get them triggered by macos
620 // actually they should be , but apparently they are not
621 wxSize
size(m_width
, m_height
);
622 wxSizeEvent
event(size
, m_windowId
);
623 event
.SetEventObject(this);
624 GetEventHandler()->ProcessEvent(event
);
628 ::HideWindow( (WindowRef
)m_macWindow
) ;
642 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
646 int former_w
= m_width
;
647 int former_h
= m_height
;
649 int actualWidth
= width
;
650 int actualHeight
= height
;
654 if ((m_minWidth
!= -1) && (actualWidth
< m_minWidth
))
655 actualWidth
= m_minWidth
;
656 if ((m_minHeight
!= -1) && (actualHeight
< m_minHeight
))
657 actualHeight
= m_minHeight
;
658 if ((m_maxWidth
!= -1) && (actualWidth
> m_maxWidth
))
659 actualWidth
= m_maxWidth
;
660 if ((m_maxHeight
!= -1) && (actualHeight
> m_maxHeight
))
661 actualHeight
= m_maxHeight
;
663 bool doMove
= false ;
664 bool doResize
= false ;
666 if ( actualX
!= former_x
|| actualY
!= former_y
)
670 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
675 if ( doMove
|| doResize
)
679 m_width
= actualWidth
;
680 m_height
= actualHeight
;
683 ::MoveWindow((WindowRef
)m_macWindow
, m_x
, m_y
, false); // don't make frontmost
686 ::SizeWindow((WindowRef
)m_macWindow
, m_width
, m_height
, true);
688 // the OS takes care of invalidating and erasing the new area
689 // we have erased the old one
691 if ( IsKindOf( CLASSINFO( wxFrame
) ) )
693 wxFrame
* frame
= (wxFrame
*) this ;
694 frame
->PositionStatusBar();
695 frame
->PositionToolBar();
698 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
700 MacRepositionScrollBars() ;
703 wxPoint
point(m_x
, m_y
);
704 wxMoveEvent
event(point
, m_windowId
);
705 event
.SetEventObject(this);
706 GetEventHandler()->ProcessEvent(event
) ;
710 MacRepositionScrollBars() ;
711 wxSize
size(m_width
, m_height
);
712 wxSizeEvent
event(size
, m_windowId
);
713 event
.SetEventObject(this);
714 GetEventHandler()->ProcessEvent(event
);
721 * Invalidation Mechanism
723 * The update mechanism reflects exactely the windows mechanism
724 * the rect gets added to the window invalidate region, if the eraseBackground flag
725 * has been true for any part of the update rgn the background is erased in the entire region
726 * not just in the specified rect.
728 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
729 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
730 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
731 * will get the eraseBackground event first
734 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect
, bool eraseBackground
)
737 GetPort( &formerPort
) ;
738 SetPortWindowPort( (WindowRef
)m_macWindow
) ;
740 m_macNeedsErasing
|= eraseBackground
;
742 // if we already know that we will have to erase, there's no need to track the rest
743 if ( !m_macNeedsErasing
)
745 // we end only here if eraseBackground is false
746 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
747 // we will have to erase anyway
749 RgnHandle updateRgn
= NewRgn();
750 RgnHandle diffRgn
= NewRgn() ;
751 if ( updateRgn
&& diffRgn
)
753 GetWindowUpdateRgn( (WindowRef
)m_macWindow
, updateRgn
);
755 LocalToGlobal( &pt
) ;
756 OffsetRgn( updateRgn
, -pt
.h
, -pt
.v
) ;
757 DiffRgn( updateRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
, diffRgn
) ;
758 if ( !EmptyRgn( diffRgn
) )
760 m_macNeedsErasing
= true ;
764 DisposeRgn( updateRgn
);
766 DisposeRgn( diffRgn
);
768 if ( !m_macNeedsErasing
)
770 RgnHandle rectRgn
= NewRgn() ;
771 SetRectRgn( rectRgn
, ((Rect
*)rect
)->left
, ((Rect
*)rect
)->top
, ((Rect
*)rect
)->right
, ((Rect
*)rect
)->bottom
) ;
772 UnionRgn( (RgnHandle
) m_macNoEraseUpdateRgn
, rectRgn
, (RgnHandle
) m_macNoEraseUpdateRgn
) ;
773 DisposeRgn( rectRgn
) ;
776 InvalWindowRect( (WindowRef
)m_macWindow
, (Rect
*)rect
) ;
777 // turn this on to debug the refreshing cycle
778 #if wxMAC_DEBUG_REDRAW
781 SetPort( formerPort
) ;