1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/nonownedwnd.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Stefan Csomor
6 // RCS-ID: $Id: nonownedwnd.cpp 50329 2007-11-29 17:00:58Z VS $
7 // Copyright: (c) Stefan Csomor 2008
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #include "wx/hashmap.h"
19 #include "wx/evtloop.h"
20 #include "wx/tooltip.h"
21 #include "wx/nonownedwnd.h"
23 #include "wx/osx/private.h"
24 #include "wx/settings.h"
27 #if wxUSE_SYSTEM_OPTIONS
28 #include "wx/sysopt.h"
32 // TODO BEGIN move to nonowned_osx.cpp
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // trace mask for activation tracing messages
40 #define TRACE_ACTIVATE "activation"
42 wxWindow
* g_MacLastWindow
= NULL
;
44 // ---------------------------------------------------------------------------
45 // wxWindowMac utility functions
46 // ---------------------------------------------------------------------------
48 // Find an item given the Macintosh Window Reference
50 WX_DECLARE_HASH_MAP(WXWindow
, wxNonOwnedWindow
*, wxPointerHash
, wxPointerEqual
, MacWindowMap
);
52 static MacWindowMap wxWinMacWindowList
;
54 wxNonOwnedWindow
*wxFindWindowFromWXWindow(WXWindow inWindowRef
)
56 MacWindowMap::iterator node
= wxWinMacWindowList
.find(inWindowRef
);
58 return (node
== wxWinMacWindowList
.end()) ? NULL
: node
->second
;
61 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef
, wxNonOwnedWindow
*win
) ;
62 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef
, wxNonOwnedWindow
*win
)
64 // adding NULL WindowRef is (first) surely a result of an error and
66 wxCHECK_RET( inWindowRef
!= (WXWindow
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
68 wxWinMacWindowList
[inWindowRef
] = win
;
71 void wxRemoveWXWindowAssociation(wxNonOwnedWindow
*win
) ;
72 void wxRemoveWXWindowAssociation(wxNonOwnedWindow
*win
)
74 MacWindowMap::iterator it
;
75 for ( it
= wxWinMacWindowList
.begin(); it
!= wxWinMacWindowList
.end(); ++it
)
77 if ( it
->second
== win
)
79 wxWinMacWindowList
.erase(it
);
85 wxNonOwnedWindow
* wxNonOwnedWindow::GetFromWXWindow( WXWindow win
)
87 return wxFindWindowFromWXWindow( win
);
90 // ----------------------------------------------------------------------------
91 // wxNonOwnedWindow creation
92 // ----------------------------------------------------------------------------
94 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl
, wxObject
)
96 wxNonOwnedWindow
*wxNonOwnedWindow::s_macDeactivateWindow
= NULL
;
98 void wxNonOwnedWindow::Init()
103 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
108 const wxString
& name
)
113 m_windowStyle
= style
;
117 m_windowId
= id
== -1 ? NewControlId() : id
;
118 m_windowStyle
= style
;
125 wxRect display
= wxGetClientDisplayRect() ;
127 if ( x
== wxDefaultPosition
.x
)
130 if ( y
== wxDefaultPosition
.y
)
133 int w
= WidthDefault(size
.x
);
134 int h
= HeightDefault(size
.y
);
136 // temporary define, TODO
138 m_nowpeer
= new wxNonOwnedWindowCarbonImpl( this );
139 #elif wxOSX_USE_COCOA
140 m_nowpeer
= new wxNonOwnedWindowCocoaImpl( this );
141 #elif wxOSX_USE_IPHONE
142 m_nowpeer
= new wxNonOwnedWindowIPhoneImpl( this );
145 m_nowpeer
->Create( parent
, wxPoint(x
,y
) , wxSize(w
,h
) , style
, GetExtraStyle(), name
) ;
146 wxAssociateWindowWithWXWindow( m_nowpeer
->GetWXWindow() , this ) ;
148 // temporary cast, TODO
149 m_peer
= (wxMacControl
*) wxWidgetImpl::CreateContentView(this);
151 m_peer
= wxWidgetImpl::CreateContentView(this);
154 DoSetWindowVariant( m_windowVariant
) ;
156 wxWindowCreateEvent
event(this);
157 HandleWindowEvent(event
);
159 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
162 parent
->AddChild(this);
167 wxNonOwnedWindow::~wxNonOwnedWindow()
169 wxRemoveWXWindowAssociation( this ) ;
171 m_nowpeer
->Destroy();
173 // avoid dangling refs
174 if ( s_macDeactivateWindow
== this )
175 s_macDeactivateWindow
= NULL
;
178 // ----------------------------------------------------------------------------
179 // wxNonOwnedWindow misc
180 // ----------------------------------------------------------------------------
182 bool wxNonOwnedWindow::ShowWithEffect(wxShowEffect effect
,
185 if ( !wxWindow::Show(true) )
188 // because apps expect a size event to occur at this moment
189 wxSizeEvent
event(GetSize() , m_windowId
);
190 event
.SetEventObject(this);
191 HandleWindowEvent(event
);
194 return m_nowpeer
->ShowWithEffect(true, effect
, timeout
);
197 bool wxNonOwnedWindow::HideWithEffect(wxShowEffect effect
,
200 if ( !wxWindow::Show(false) )
203 return m_nowpeer
->ShowWithEffect(false, effect
, timeout
);
206 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
208 int left
, top
, width
, height
;
209 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
210 return wxPoint(left
, top
);
213 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
215 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
218 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
220 return m_nowpeer
->SetBackgroundColour(c
);
225 // Raise the window to the top of the Z order
226 void wxNonOwnedWindow::Raise()
231 // Lower the window to the bottom of the Z order
232 void wxNonOwnedWindow::Lower()
237 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
239 if (s_macDeactivateWindow
)
241 wxLogTrace(TRACE_ACTIVATE
,
242 wxT("Doing delayed deactivation of %p"),
243 s_macDeactivateWindow
);
245 s_macDeactivateWindow
->MacActivate(timestamp
, false);
249 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
251 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
253 if (s_macDeactivateWindow
== this)
254 s_macDeactivateWindow
= NULL
;
256 MacDelayedDeactivation(timestamp
);
259 bool wxNonOwnedWindow::Show(bool show
)
261 if ( !wxWindow::Show(show
) )
265 m_nowpeer
->Show(show
);
269 // because apps expect a size event to occur at this moment
270 wxSizeEvent
event(GetSize() , m_windowId
);
271 event
.SetEventObject(this);
272 HandleWindowEvent(event
);
278 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
280 return m_nowpeer
->SetTransparent(alpha
);
284 bool wxNonOwnedWindow::CanSetTransparent()
286 return m_nowpeer
->CanSetTransparent();
290 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
292 if ( GetExtraStyle() == exStyle
)
295 wxWindow::SetExtraStyle( exStyle
) ;
298 m_nowpeer
->SetExtraStyle(exStyle
);
301 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
303 if ( !wxWindow::SetBackgroundStyle(style
) )
306 return m_nowpeer
->SetBackgroundStyle(style
);
309 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
311 m_cachedClippedRectValid
= false ;
313 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
314 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
317 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
320 m_nowpeer
->GetPosition(x1
, y1
);
328 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
332 m_nowpeer
->GetSize(w
, h
);
340 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
343 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
352 void wxNonOwnedWindow::Update()
357 WXWindow
wxNonOwnedWindow::GetWXWindow() const
359 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
362 // ---------------------------------------------------------------------------
363 // Shape implementation
364 // ---------------------------------------------------------------------------
367 bool wxNonOwnedWindow::SetShape(const wxRegion
& region
)
369 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
370 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
372 // The empty region signifies that the shape
373 // should be removed from the window.
374 if ( region
.IsEmpty() )
376 wxSize sz
= GetClientSize();
377 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
381 return SetShape(rgn
);
384 return m_nowpeer
->SetShape(region
);
388 // TODO END move to nonowned_osx.cpp
393 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCarbonImpl
, wxNonOwnedWindowImpl
)
396 WXWindow
wxNonOwnedWindowCarbonImpl::GetWXWindow() const
398 return (WXWindow
) m_macWindow
;
400 void wxNonOwnedWindowCarbonImpl::Raise()
402 ::SelectWindow( m_macWindow
) ;
405 void wxNonOwnedWindowCarbonImpl::Lower()
407 ::SendBehind( m_macWindow
, NULL
) ;
410 bool wxNonOwnedWindowCarbonImpl::Show(bool show
)
412 bool plainTransition
= true;
414 #if wxUSE_SYSTEM_OPTIONS
415 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) )
416 plainTransition
= ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1 ) ;
422 if ( plainTransition
)
423 ::ShowWindow( (WindowRef
)m_macWindow
);
425 ::TransitionWindow( (WindowRef
)m_macWindow
, kWindowZoomTransitionEffect
, kWindowShowTransitionAction
, NULL
);
427 ::SelectWindow( (WindowRef
)m_macWindow
) ;
433 if ( plainTransition
)
434 ::HideWindow( (WindowRef
)m_macWindow
);
436 ::TransitionWindow( (WindowRef
)m_macWindow
, kWindowZoomTransitionEffect
, kWindowHideTransitionAction
, NULL
);
442 void wxNonOwnedWindowCarbonImpl::Update()
444 HIWindowFlush(m_macWindow
) ;
447 bool wxNonOwnedWindowCarbonImpl::SetTransparent(wxByte alpha
)
449 OSStatus result
= SetWindowAlpha((WindowRef
)m_macWindow
, (CGFloat
)((alpha
)/255.0));
450 return result
== noErr
;
453 bool wxNonOwnedWindowCarbonImpl::SetBackgroundColour(const wxColour
& col
)
455 if ( col
== wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDocumentWindowBackground
)) )
457 SetThemeWindowBackground( (WindowRef
) m_macWindow
, kThemeBrushDocumentWindowBackground
, false ) ;
458 SetBackgroundStyle(wxBG_STYLE_SYSTEM
);
460 else if ( col
== wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive
)) )
462 SetThemeWindowBackground( (WindowRef
) m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
463 SetBackgroundStyle(wxBG_STYLE_SYSTEM
);
468 void wxNonOwnedWindowCarbonImpl::SetExtraStyle( long exStyle
)
470 if ( m_macWindow
!= NULL
)
472 bool metal
= exStyle
& wxFRAME_EX_METAL
;
474 if ( MacGetMetalAppearance() != metal
)
476 if ( MacGetUnifiedAppearance() )
477 MacSetUnifiedAppearance( !metal
) ;
479 MacSetMetalAppearance( metal
) ;
484 bool wxNonOwnedWindowCarbonImpl::SetBackgroundStyle(wxBackgroundStyle style
)
486 if ( style
== wxBG_STYLE_TRANSPARENT
)
488 OSStatus err
= HIWindowChangeFeatures( m_macWindow
, 0, kWindowIsOpaque
);
490 err
= ReshapeCustomWindow( m_macWindow
);
497 bool wxNonOwnedWindowCarbonImpl::CanSetTransparent()
502 void wxNonOwnedWindowCarbonImpl::GetContentArea( int &left
, int &top
, int &width
, int &height
) const
504 Rect content
, structure
;
506 GetWindowBounds( m_macWindow
, kWindowStructureRgn
, &structure
) ;
507 GetWindowBounds( m_macWindow
, kWindowContentRgn
, &content
) ;
509 left
= content
.left
- structure
.left
;
510 top
= content
.top
- structure
.top
;
511 width
= content
.right
- content
.left
;
512 height
= content
.bottom
- content
.top
;
515 void wxNonOwnedWindowCarbonImpl::MoveWindow(int x
, int y
, int width
, int height
)
517 Rect bounds
= { y
, x
, y
+ height
, x
+ width
} ;
518 verify_noerr(SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
521 void wxNonOwnedWindowCarbonImpl::GetPosition( int &x
, int &y
) const
525 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
531 void wxNonOwnedWindowCarbonImpl::GetSize( int &width
, int &height
) const
535 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
537 width
= bounds
.right
- bounds
.left
;
538 height
= bounds
.bottom
- bounds
.top
;
541 bool wxNonOwnedWindowCarbonImpl::MacGetUnifiedAppearance() const
543 return MacGetWindowAttributes() & kWindowUnifiedTitleAndToolbarAttribute
;
546 void wxNonOwnedWindowCarbonImpl::MacChangeWindowAttributes( wxUint32 attributesToSet
, wxUint32 attributesToClear
)
548 ChangeWindowAttributes( m_macWindow
, attributesToSet
, attributesToClear
) ;
551 wxUint32
wxNonOwnedWindowCarbonImpl::MacGetWindowAttributes() const
554 GetWindowAttributes( m_macWindow
, &attr
) ;
558 void wxNonOwnedWindowCarbonImpl::MacSetMetalAppearance( bool set
)
560 if ( MacGetUnifiedAppearance() )
561 MacSetUnifiedAppearance( false ) ;
563 MacChangeWindowAttributes( set
? kWindowMetalAttribute
: kWindowNoAttributes
,
564 set
? kWindowNoAttributes
: kWindowMetalAttribute
) ;
567 bool wxNonOwnedWindowCarbonImpl::MacGetMetalAppearance() const
569 return MacGetWindowAttributes() & kWindowMetalAttribute
;
572 void wxNonOwnedWindowCarbonImpl::MacSetUnifiedAppearance( bool set
)
574 if ( MacGetMetalAppearance() )
575 MacSetMetalAppearance( false ) ;
577 MacChangeWindowAttributes( set
? kWindowUnifiedTitleAndToolbarAttribute
: kWindowNoAttributes
,
578 set
? kWindowNoAttributes
: kWindowUnifiedTitleAndToolbarAttribute
) ;
580 // For some reason, Tiger uses white as the background color for this appearance,
581 // while most apps using it use the typical striped background. Restore that behavior
583 // TODO: Determine if we need this on Leopard as well. (should be harmless either way,
585 m_wxPeer
->SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ) ;
589 // ----------------------------------------------------------------------------
591 // ----------------------------------------------------------------------------
593 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
595 // ============================================================================
596 // wxNonOwnedWindow implementation
597 // ============================================================================
599 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
600 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
602 // ---------------------------------------------------------------------------
604 // ---------------------------------------------------------------------------
606 static const EventTypeSpec eventList
[] =
608 // TODO: remove control related event like key and mouse (except for WindowLeave events)
610 { kEventClassKeyboard
, kEventRawKeyDown
} ,
611 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
612 { kEventClassKeyboard
, kEventRawKeyUp
} ,
613 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
615 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
616 { kEventClassTextInput
, kEventTextInputUpdateActiveInputArea
} ,
618 { kEventClassWindow
, kEventWindowShown
} ,
619 { kEventClassWindow
, kEventWindowActivated
} ,
620 { kEventClassWindow
, kEventWindowDeactivated
} ,
621 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
622 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
623 { kEventClassWindow
, kEventWindowClose
} ,
624 { kEventClassWindow
, kEventWindowGetRegion
} ,
626 // we have to catch these events on the toplevel window level,
627 // as controls don't get the raw mouse events anymore
629 { kEventClassMouse
, kEventMouseDown
} ,
630 { kEventClassMouse
, kEventMouseUp
} ,
631 { kEventClassMouse
, kEventMouseWheelMoved
} ,
632 { kEventClassMouse
, kEventMouseMoved
} ,
633 { kEventClassMouse
, kEventMouseDragged
} ,
636 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
638 OSStatus result
= eventNotHandledErr
;
639 // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
640 // FindFocus does not return the actual focus window, but the enclosing window
641 wxWindow
* focus
= wxWindow::DoFindFocus();
643 focus
= data
? ((wxNonOwnedWindowImpl
*) data
)->GetWXPeer() : NULL
;
645 unsigned char charCode
;
653 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
656 ByteCount dataSize
= 0 ;
657 if ( GetEventParameter( event
, kEventParamKeyUnicodes
, typeUnicodeText
, NULL
, 0 , &dataSize
, NULL
) == noErr
)
660 int numChars
= dataSize
/ sizeof( UniChar
) + 1;
662 UniChar
* charBuf
= buf
;
664 if ( numChars
* 2 > 4 )
665 charBuf
= new UniChar
[ numChars
] ;
666 GetEventParameter( event
, kEventParamKeyUnicodes
, typeUnicodeText
, NULL
, dataSize
, NULL
, charBuf
) ;
667 charBuf
[ numChars
- 1 ] = 0;
669 #if SIZEOF_WCHAR_T == 2
670 uniChar
= charBuf
[0] ;
672 wxMBConvUTF16 converter
;
673 converter
.MB2WC( uniChar
, (const char*)charBuf
, 2 ) ;
676 if ( numChars
* 2 > 4 )
681 GetEventParameter( event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, sizeof(char), NULL
, &charCode
);
682 GetEventParameter( event
, kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
683 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
684 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
, sizeof(Point
), NULL
, &point
);
686 UInt32 message
= (keyCode
<< 8) + charCode
;
687 switch ( GetEventKind( event
) )
689 case kEventRawKeyRepeat
:
690 case kEventRawKeyDown
:
692 WXEVENTREF formerEvent
= wxTheApp
->MacGetCurrentEvent() ;
693 WXEVENTHANDLERCALLREF formerHandler
= wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
694 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
695 if ( /* focus && */ wxTheApp
->MacSendKeyDownEvent(
696 focus
, message
, modifiers
, when
, point
.h
, point
.v
, uniChar
[0] ) )
700 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
704 case kEventRawKeyUp
:
705 if ( /* focus && */ wxTheApp
->MacSendKeyUpEvent(
706 focus
, message
, modifiers
, when
, point
.h
, point
.v
, uniChar
[0] ) )
712 case kEventRawKeyModifiersChanged
:
714 wxKeyEvent
event(wxEVT_KEY_DOWN
);
716 event
.m_shiftDown
= modifiers
& shiftKey
;
717 event
.m_controlDown
= modifiers
& controlKey
;
718 event
.m_altDown
= modifiers
& optionKey
;
719 event
.m_metaDown
= modifiers
& cmdKey
;
724 event
.m_uniChar
= uniChar
[0] ;
727 event
.SetTimestamp(when
);
728 event
.SetEventObject(focus
);
730 if ( /* focus && */ (modifiers
^ wxApp::s_lastModifiers
) & controlKey
)
732 event
.m_keyCode
= WXK_CONTROL
;
733 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
734 focus
->HandleWindowEvent( event
) ;
736 if ( /* focus && */ (modifiers
^ wxApp::s_lastModifiers
) & shiftKey
)
738 event
.m_keyCode
= WXK_SHIFT
;
739 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
740 focus
->HandleWindowEvent( event
) ;
742 if ( /* focus && */ (modifiers
^ wxApp::s_lastModifiers
) & optionKey
)
744 event
.m_keyCode
= WXK_ALT
;
745 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
746 focus
->HandleWindowEvent( event
) ;
748 if ( /* focus && */ (modifiers
^ wxApp::s_lastModifiers
) & cmdKey
)
750 event
.m_keyCode
= WXK_COMMAND
;
751 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
752 focus
->HandleWindowEvent( event
) ;
755 wxApp::s_lastModifiers
= modifiers
;
766 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
767 // for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the
770 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
772 EventMouseButton g_lastButton
= 0 ;
773 bool g_lastButtonWasFakeRight
= false ;
775 void SetupMouseEvent( wxMouseEvent
&wxevent
, wxMacCarbonEvent
&cEvent
)
777 UInt32 modifiers
= cEvent
.GetParameter
<UInt32
>(kEventParamKeyModifiers
, typeUInt32
) ;
778 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
780 // these parameters are not given for all events
781 EventMouseButton button
= 0 ;
782 UInt32 clickCount
= 0 ;
783 UInt32 mouseChord
= 0;
785 cEvent
.GetParameter
<EventMouseButton
>( kEventParamMouseButton
, typeMouseButton
, &button
) ;
786 cEvent
.GetParameter
<UInt32
>( kEventParamClickCount
, typeUInt32
, &clickCount
) ;
787 // the chord is the state of the buttons pressed currently
788 cEvent
.GetParameter
<UInt32
>( kEventParamMouseChord
, typeUInt32
, &mouseChord
) ;
790 wxevent
.m_x
= screenMouseLocation
.h
;
791 wxevent
.m_y
= screenMouseLocation
.v
;
792 wxevent
.m_shiftDown
= modifiers
& shiftKey
;
793 wxevent
.m_controlDown
= modifiers
& controlKey
;
794 wxevent
.m_altDown
= modifiers
& optionKey
;
795 wxevent
.m_metaDown
= modifiers
& cmdKey
;
796 wxevent
.m_clickCount
= clickCount
;
797 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
799 // a control click is interpreted as a right click
800 bool thisButtonIsFakeRight
= false ;
801 if ( button
== kEventMouseButtonPrimary
&& (modifiers
& controlKey
) )
803 button
= kEventMouseButtonSecondary
;
804 thisButtonIsFakeRight
= true ;
807 // otherwise we report double clicks by connecting a left click with a ctrl-left click
808 if ( clickCount
> 1 && button
!= g_lastButton
)
811 // we must make sure that our synthetic 'right' button corresponds in
812 // mouse down, moved and mouse up, and does not deliver a right down and left up
814 if ( cEvent
.GetKind() == kEventMouseDown
)
816 g_lastButton
= button
;
817 g_lastButtonWasFakeRight
= thisButtonIsFakeRight
;
823 g_lastButtonWasFakeRight
= false ;
825 else if ( g_lastButton
== kEventMouseButtonSecondary
&& g_lastButtonWasFakeRight
)
826 button
= g_lastButton
;
828 // Adjust the chord mask to remove the primary button and add the
829 // secondary button. It is possible that the secondary button is
830 // already pressed, e.g. on a mouse connected to a laptop, but this
831 // possibility is ignored here:
832 if( thisButtonIsFakeRight
&& ( mouseChord
& 1U ) )
833 mouseChord
= ((mouseChord
& ~1U) | 2U);
836 wxevent
.m_leftDown
= true ;
838 wxevent
.m_rightDown
= true ;
840 wxevent
.m_middleDown
= true ;
842 // translate into wx types
843 switch ( cEvent
.GetKind() )
845 case kEventMouseDown
:
848 case kEventMouseButtonPrimary
:
849 wxevent
.SetEventType( clickCount
> 1 ? wxEVT_LEFT_DCLICK
: wxEVT_LEFT_DOWN
) ;
852 case kEventMouseButtonSecondary
:
853 wxevent
.SetEventType( clickCount
> 1 ? wxEVT_RIGHT_DCLICK
: wxEVT_RIGHT_DOWN
) ;
856 case kEventMouseButtonTertiary
:
857 wxevent
.SetEventType( clickCount
> 1 ? wxEVT_MIDDLE_DCLICK
: wxEVT_MIDDLE_DOWN
) ;
868 case kEventMouseButtonPrimary
:
869 wxevent
.SetEventType( wxEVT_LEFT_UP
) ;
872 case kEventMouseButtonSecondary
:
873 wxevent
.SetEventType( wxEVT_RIGHT_UP
) ;
876 case kEventMouseButtonTertiary
:
877 wxevent
.SetEventType( wxEVT_MIDDLE_UP
) ;
885 case kEventMouseWheelMoved
:
887 wxevent
.SetEventType( wxEVT_MOUSEWHEEL
) ;
889 EventMouseWheelAxis axis
= cEvent
.GetParameter
<EventMouseWheelAxis
>(kEventParamMouseWheelAxis
, typeMouseWheelAxis
) ;
890 SInt32 delta
= cEvent
.GetParameter
<SInt32
>(kEventParamMouseWheelDelta
, typeSInt32
) ;
892 wxevent
.m_wheelRotation
= delta
;
893 wxevent
.m_wheelDelta
= 1;
894 wxevent
.m_linesPerAction
= 1;
895 if ( axis
== kEventMouseWheelAxisX
)
896 wxevent
.m_wheelAxis
= 1;
900 case kEventMouseEntered
:
901 case kEventMouseExited
:
902 case kEventMouseDragged
:
903 case kEventMouseMoved
:
904 wxevent
.SetEventType( wxEVT_MOTION
) ;
911 #define NEW_CAPTURE_HANDLING 1
914 wxMacTopLevelMouseEventHandler(EventHandlerCallRef
WXUNUSED(handler
),
918 wxNonOwnedWindow
* toplevelWindow
= data
? ((wxNonOwnedWindowImpl
*) data
)->GetWXPeer() : NULL
;
920 OSStatus result
= eventNotHandledErr
;
922 wxMacCarbonEvent
cEvent( event
) ;
924 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
925 Point windowMouseLocation
= screenMouseLocation
;
927 WindowRef window
= NULL
;
928 short windowPart
= ::FindWindow(screenMouseLocation
, &window
);
930 wxWindow
* currentMouseWindow
= NULL
;
931 ControlRef control
= NULL
;
933 #if NEW_CAPTURE_HANDLING
934 if ( wxApp::s_captureWindow
)
936 window
= (WindowRef
) wxApp::s_captureWindow
->MacGetTopLevelWindowRef() ;
937 windowPart
= inContent
;
943 QDGlobalToLocalPoint( GetWindowPort( window
), &windowMouseLocation
);
945 if ( wxApp::s_captureWindow
946 #if !NEW_CAPTURE_HANDLING
947 && wxApp::s_captureWindow
->MacGetTopLevelWindowRef() == (WXWindow
) window
&& windowPart
== inContent
951 currentMouseWindow
= wxApp::s_captureWindow
;
953 else if ( (IsWindowActive(window
) && windowPart
== inContent
) )
955 ControlPartCode part
;
956 control
= FindControlUnderMouse( windowMouseLocation
, window
, &part
) ;
957 // if there is no control below the mouse position, send the event to the toplevel window itself
960 currentMouseWindow
= (wxWindow
*) toplevelWindow
;
964 currentMouseWindow
= (wxWindow
*) wxFindWindowFromWXWidget( (WXWidget
) control
) ;
965 #ifndef __WXUNIVERSAL__
966 if ( currentMouseWindow
== NULL
&& cEvent
.GetKind() == kEventMouseMoved
)
969 // for wxToolBar to function we have to send certaint events to it
970 // instead of its children (wxToolBarTools)
972 GetSuperControl(control
, &parent
);
973 wxWindow
*wxParent
= (wxWindow
*) wxFindWindowFromWXWidget((WXWidget
) parent
) ;
974 if ( wxParent
&& wxParent
->IsKindOf( CLASSINFO( wxToolBar
) ) )
975 currentMouseWindow
= wxParent
;
981 // disabled windows must not get any input messages
982 if ( currentMouseWindow
&& !currentMouseWindow
->MacIsReallyEnabled() )
983 currentMouseWindow
= NULL
;
987 wxMouseEvent
wxevent(wxEVT_LEFT_DOWN
);
988 SetupMouseEvent( wxevent
, cEvent
) ;
990 // handle all enter / leave events
992 if ( currentMouseWindow
!= g_MacLastWindow
)
994 if ( g_MacLastWindow
)
996 wxMouseEvent
eventleave(wxevent
);
997 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
998 g_MacLastWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
999 eventleave
.SetEventObject( g_MacLastWindow
) ;
1000 wxevent
.SetId( g_MacLastWindow
->GetId() ) ;
1003 wxToolTip::RelayEvent( g_MacLastWindow
, eventleave
);
1006 g_MacLastWindow
->HandleWindowEvent(eventleave
);
1009 if ( currentMouseWindow
)
1011 wxMouseEvent
evententer(wxevent
);
1012 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
1013 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
1014 evententer
.SetEventObject( currentMouseWindow
) ;
1015 wxevent
.SetId( currentMouseWindow
->GetId() ) ;
1018 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
1021 currentMouseWindow
->HandleWindowEvent(evententer
);
1024 g_MacLastWindow
= currentMouseWindow
;
1027 if ( windowPart
== inMenuBar
)
1029 // special case menu bar, as we are having a low-level runloop we must do it ourselves
1030 if ( cEvent
.GetKind() == kEventMouseDown
)
1032 ::MenuSelect( screenMouseLocation
) ;
1037 else if ( currentMouseWindow
)
1039 wxWindow
*currentMouseWindowParent
= currentMouseWindow
->GetParent();
1041 currentMouseWindow
->ScreenToClient( &wxevent
.m_x
, &wxevent
.m_y
) ;
1043 wxevent
.SetEventObject( currentMouseWindow
) ;
1044 wxevent
.SetId( currentMouseWindow
->GetId() ) ;
1046 // make tooltips current
1049 if ( wxevent
.GetEventType() == wxEVT_MOTION
)
1050 wxToolTip::RelayEvent( currentMouseWindow
, wxevent
);
1053 if ( currentMouseWindow
->HandleWindowEvent(wxevent
) )
1055 if ((currentMouseWindowParent
!= NULL
) &&
1056 (currentMouseWindowParent
->GetChildren().Find(currentMouseWindow
) == NULL
))
1057 currentMouseWindow
= NULL
;
1063 // if the user code did _not_ handle the event, then perform the
1064 // default processing
1065 if ( wxevent
.GetEventType() == wxEVT_LEFT_DOWN
)
1067 // ... that is set focus to this window
1068 if (currentMouseWindow
->CanAcceptFocus() && wxWindow::FindFocus()!=currentMouseWindow
)
1069 currentMouseWindow
->SetFocus();
1073 if ( cEvent
.GetKind() == kEventMouseUp
&& wxApp::s_captureWindow
)
1075 wxApp::s_captureWindow
= NULL
;
1081 wxWindow
* cursorTarget
= currentMouseWindow
;
1082 wxPoint
cursorPoint( wxevent
.m_x
, wxevent
.m_y
) ;
1084 extern wxCursor gGlobalCursor
;
1086 if (!gGlobalCursor
.IsOk())
1088 while ( cursorTarget
&& !cursorTarget
->MacSetupCursor( cursorPoint
) )
1090 cursorTarget
= cursorTarget
->GetParent() ;
1092 cursorPoint
+= cursorTarget
->GetPosition();
1097 else // currentMouseWindow == NULL
1099 // don't mess with controls we don't know about
1100 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
1101 // so we try sending them the correct control directly
1102 if ( cEvent
.GetKind() == kEventMouseDown
&& toplevelWindow
&& control
)
1104 EventModifiers modifiers
= cEvent
.GetParameter
<EventModifiers
>(kEventParamKeyModifiers
, typeUInt32
) ;
1105 Point clickLocation
= windowMouseLocation
;
1108 hiPoint
.x
= clickLocation
.h
;
1109 hiPoint
.y
= clickLocation
.v
;
1110 HIViewConvertPoint( &hiPoint
, (ControlRef
) toplevelWindow
->GetHandle() , control
) ;
1111 clickLocation
.h
= (int)hiPoint
.x
;
1112 clickLocation
.v
= (int)hiPoint
.y
;
1114 HandleControlClick( control
, clickLocation
, modifiers
, (ControlActionUPP
) -1 ) ;
1122 static pascal OSStatus
1123 wxNonOwnedWindowEventHandler(EventHandlerCallRef
WXUNUSED(handler
),
1127 OSStatus result
= eventNotHandledErr
;
1129 wxMacCarbonEvent
cEvent( event
) ;
1131 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
1132 wxNonOwnedWindow
* toplevelWindow
= data
? ((wxNonOwnedWindowImpl
*) data
)->GetWXPeer() : NULL
;
1134 switch ( GetEventKind( event
) )
1136 case kEventWindowActivated
:
1138 toplevelWindow
->MacActivate( cEvent
.GetTicks() , true) ;
1139 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, true , toplevelWindow
->GetId());
1140 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
1141 wxevent
.SetEventObject(toplevelWindow
);
1142 toplevelWindow
->HandleWindowEvent(wxevent
);
1143 // we still sending an eventNotHandledErr in order to allow for default processing
1147 case kEventWindowDeactivated
:
1149 toplevelWindow
->MacActivate(cEvent
.GetTicks() , false) ;
1150 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, false , toplevelWindow
->GetId());
1151 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
1152 wxevent
.SetEventObject(toplevelWindow
);
1153 toplevelWindow
->HandleWindowEvent(wxevent
);
1154 // we still sending an eventNotHandledErr in order to allow for default processing
1158 case kEventWindowShown
:
1159 toplevelWindow
->Refresh() ;
1163 case kEventWindowClose
:
1164 toplevelWindow
->Close() ;
1168 case kEventWindowBoundsChanged
:
1170 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
, typeUInt32
) ;
1171 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
1172 wxRect
r( newRect
.left
, newRect
.top
, newRect
.right
- newRect
.left
, newRect
.bottom
- newRect
.top
) ;
1173 if ( attributes
& kWindowBoundsChangeSizeChanged
)
1175 #ifndef __WXUNIVERSAL__
1176 // according to the other ports we handle this within the OS level
1177 // resize event, not within a wxSizeEvent
1178 wxFrame
*frame
= wxDynamicCast( toplevelWindow
, wxFrame
) ;
1181 frame
->PositionBars();
1184 wxSizeEvent
event( r
.GetSize() , toplevelWindow
->GetId() ) ;
1185 event
.SetEventObject( toplevelWindow
) ;
1187 toplevelWindow
->HandleWindowEvent(event
) ;
1188 toplevelWindow
->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1191 if ( attributes
& kWindowBoundsChangeOriginChanged
)
1193 wxMoveEvent
event( r
.GetLeftTop() , toplevelWindow
->GetId() ) ;
1194 event
.SetEventObject( toplevelWindow
) ;
1195 toplevelWindow
->HandleWindowEvent(event
) ;
1202 case kEventWindowBoundsChanging
:
1204 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
1205 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
1207 if ( (attributes
& kWindowBoundsChangeSizeChanged
) || (attributes
& kWindowBoundsChangeOriginChanged
) )
1209 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
1210 int left
, top
, right
, bottom
;
1212 toplevelWindow
->GetNonOwnedPeer()->GetContentArea(left
, top
, right
, bottom
);
1215 newRect
.left
- left
,
1217 newRect
.right
- newRect
.left
+ left
+ right
,
1218 newRect
.bottom
- newRect
.top
+ top
+ bottom
) ;
1220 // this is a EVT_SIZING not a EVT_SIZE type !
1221 wxSizeEvent
wxevent( r
, toplevelWindow
->GetId() ) ;
1222 wxevent
.SetEventObject( toplevelWindow
) ;
1223 wxRect adjustR
= r
;
1224 if ( toplevelWindow
->HandleWindowEvent(wxevent
) )
1225 adjustR
= wxevent
.GetRect() ;
1227 if ( toplevelWindow
->GetMaxWidth() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxWidth() )
1228 adjustR
.SetWidth( toplevelWindow
->GetMaxWidth() ) ;
1229 if ( toplevelWindow
->GetMaxHeight() != -1 && adjustR
.GetHeight() > toplevelWindow
->GetMaxHeight() )
1230 adjustR
.SetHeight( toplevelWindow
->GetMaxHeight() ) ;
1231 if ( toplevelWindow
->GetMinWidth() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinWidth() )
1232 adjustR
.SetWidth( toplevelWindow
->GetMinWidth() ) ;
1233 if ( toplevelWindow
->GetMinHeight() != -1 && adjustR
.GetHeight() < toplevelWindow
->GetMinHeight() )
1234 adjustR
.SetHeight( toplevelWindow
->GetMinHeight() ) ;
1235 const Rect adjustedRect
= { adjustR
.y
+ top
, adjustR
.x
+ left
, adjustR
.y
+ adjustR
.height
- bottom
, adjustR
.x
+ adjustR
.width
- right
} ;
1236 if ( !EqualRect( &newRect
, &adjustedRect
) )
1237 cEvent
.SetParameter
<Rect
>( kEventParamCurrentBounds
, &adjustedRect
) ;
1238 toplevelWindow
->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1245 case kEventWindowGetRegion
:
1247 if ( toplevelWindow
->GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT
)
1249 WindowRegionCode windowRegionCode
;
1251 // Fetch the region code that is being queried
1252 GetEventParameter( event
,
1253 kEventParamWindowRegionCode
,
1254 typeWindowRegionCode
, NULL
,
1255 sizeof windowRegionCode
, NULL
,
1256 &windowRegionCode
) ;
1258 // If it is the opaque region code then set the
1259 // region to empty and return noErr to stop event
1261 if ( windowRegionCode
== kWindowOpaqueRgn
) {
1263 GetEventParameter( event
,
1264 kEventParamRgnHandle
,
1265 typeQDRgnHandle
, NULL
,
1266 sizeof region
, NULL
,
1268 SetEmptyRgn(region
) ;
1282 // mix this in from window.cpp
1283 pascal OSStatus
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
1285 pascal OSStatus
wxNonOwnedEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1287 OSStatus result
= eventNotHandledErr
;
1289 switch ( GetEventClass( event
) )
1291 case kEventClassTextInput
:
1292 result
= wxMacUnicodeTextEventHandler( handler
, event
, data
) ;
1295 case kEventClassKeyboard
:
1296 result
= KeyboardEventHandler( handler
, event
, data
) ;
1299 case kEventClassWindow
:
1300 result
= wxNonOwnedWindowEventHandler( handler
, event
, data
) ;
1303 case kEventClassMouse
:
1304 result
= wxMacTopLevelMouseEventHandler( handler
, event
, data
) ;
1314 DEFINE_ONE_SHOT_HANDLER_GETTER( wxNonOwnedEventHandler
)
1316 // ---------------------------------------------------------------------------
1317 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1318 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1319 // ---------------------------------------------------------------------------
1321 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1323 GetWindowPortBounds(window
, inRect
);
1324 Point pt
= { inRect
->top
,inRect
->left
};
1325 QDLocalToGlobalPoint( GetWindowPort( window
), &pt
);
1326 inRect
->bottom
+= pt
.v
- inRect
->top
;
1327 inRect
->right
+= pt
.h
- inRect
->left
;
1329 inRect
->left
= pt
.h
;
1332 static SInt32
wxShapedMacWindowGetFeatures(WindowRef
WXUNUSED(window
), SInt32 param
)
1334 /*------------------------------------------------------
1335 Define which options your custom window supports.
1336 --------------------------------------------------------*/
1337 //just enable everything for our demo
1338 *(OptionBits
*)param
=
1341 kWindowCanCollapse
|
1342 //kWindowCanGetWindowRegion |
1343 //kWindowHasTitleBar |
1344 //kWindowSupportsDragHilite |
1345 kWindowCanDrawInCurrentPort
|
1346 //kWindowCanMeasureTitle |
1347 kWindowWantsDisposeAtProcessDeath
|
1348 kWindowSupportsGetGrowImageRegion
|
1349 kWindowDefSupportsColorGrafPort
;
1354 // The content region is left as a rectangle matching the window size, this is
1355 // so the origin in the paint event, and etc. still matches what the
1356 // programmer expects.
1357 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1360 wxNonOwnedWindow
* win
= wxNonOwnedWindow::GetFromWXWindow((WXWindow
)window
);
1364 wxShapedMacWindowGetPos( window
, &r
) ;
1365 RectRgn( rgn
, &r
) ;
1369 // The structure region is set to the shape given to the SetShape method.
1370 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1372 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1378 wxShapedMacWindowGetPos(window
, &windowRect
); // how big is the window
1379 CopyRgn(cachedRegion
, rgn
); // make a copy of our cached region
1380 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1381 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1385 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1387 GetWindowRegionPtr rgnRec
= (GetWindowRegionPtr
)param
;
1392 switch (rgnRec
->regionCode
)
1394 case kWindowStructureRgn
:
1395 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1398 case kWindowContentRgn
:
1399 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1403 SetEmptyRgn(rgnRec
->winRgn
);
1410 // Determine the region of the window which was hit
1412 static SInt32
wxShapedMacWindowHitTest(WindowRef window
, SInt32 param
)
1415 static RgnHandle tempRgn
= NULL
;
1417 if (tempRgn
== NULL
)
1420 // get the point clicked
1421 SetPt( &hitPoint
, LoWord(param
), HiWord(param
) );
1423 // Mac OS 8.5 or later
1424 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1425 if (PtInRgn( hitPoint
, tempRgn
)) //in window content region?
1428 // no significant area was hit
1432 static pascal long wxShapedMacWindowDef(short WXUNUSED(varCode
), WindowRef window
, SInt16 message
, SInt32 param
)
1436 case kWindowMsgHitTest
:
1437 return wxShapedMacWindowHitTest(window
, param
);
1439 case kWindowMsgGetFeatures
:
1440 return wxShapedMacWindowGetFeatures(window
, param
);
1442 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1443 case kWindowMsgGetRegion
:
1444 return wxShapedMacWindowGetRegion(window
, param
);
1457 wxPoint m_position
;
1459 bool m_wasResizable
;
1462 wxNonOwnedWindowCarbonImpl::wxNonOwnedWindowCarbonImpl()
1466 wxNonOwnedWindowCarbonImpl::wxNonOwnedWindowCarbonImpl( wxNonOwnedWindow
* nonownedwnd
) : wxNonOwnedWindowImpl( nonownedwnd
)
1468 m_macEventHandler
= NULL
;
1470 m_macFullScreenData
= NULL
;
1473 wxNonOwnedWindowCarbonImpl::~wxNonOwnedWindowCarbonImpl()
1476 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
1479 if ( m_macEventHandler
)
1481 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
1482 m_macEventHandler
= NULL
;
1486 DisposeWindow( m_macWindow
);
1488 FullScreenData
*data
= (FullScreenData
*) m_macFullScreenData
;
1490 m_macFullScreenData
= NULL
;
1496 void wxNonOwnedWindowCarbonImpl::Destroy()
1498 wxPendingDelete
.Append( new wxDeferredObjectDeleter( this ) ) ;
1501 void wxNonOwnedWindowInstallTopLevelWindowEventHandler(WindowRef window
, EventHandlerRef
* handler
, void *ref
)
1503 InstallWindowEventHandler(window
, GetwxNonOwnedEventHandlerUPP(),
1504 GetEventTypeCount(eventList
), eventList
, ref
, handler
);
1507 bool wxNonOwnedWindowCarbonImpl::SetShape(const wxRegion
& region
)
1509 // Make a copy of the region
1510 RgnHandle shapeRegion
= NewRgn();
1511 HIShapeGetAsQDRgn( region
.GetWXHRGN(), shapeRegion
);
1513 // Dispose of any shape region we may already have
1514 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
) m_wxPeer
->GetWXWindow() );
1518 // Save the region so we can use it later
1519 SetWRefCon((WindowRef
) m_wxPeer
->GetWXWindow(), (URefCon
)shapeRegion
);
1521 // inform the window manager that the window has changed shape
1522 ReshapeCustomWindow((WindowRef
) m_wxPeer
->GetWXWindow());
1528 void wxNonOwnedWindowCarbonImpl::MacInstallTopLevelWindowEventHandler()
1530 if ( m_macEventHandler
!= NULL
)
1532 verify_noerr( ::RemoveEventHandler( (EventHandlerRef
) m_macEventHandler
) ) ;
1534 wxNonOwnedWindowInstallTopLevelWindowEventHandler(MAC_WXHWND(m_macWindow
),(EventHandlerRef
*)&m_macEventHandler
,this);
1537 void wxNonOwnedWindowCarbonImpl::Create(
1541 long style
, long extraStyle
,
1542 const wxString
& name
)
1545 OSStatus err
= noErr
;
1554 ::SetRect(&theBoundsRect
, x
, y
, x
+ w
, y
+ h
);
1556 // translate the window attributes in the appropriate window class and attributes
1557 WindowClass wclass
= 0;
1558 WindowAttributes attr
= kWindowNoAttributes
;
1559 WindowGroupRef group
= NULL
;
1560 bool activationScopeSet
= false;
1561 WindowActivationScope activationScope
= kWindowActivationScopeNone
;
1563 if ( style
& wxFRAME_TOOL_WINDOW
)
1566 ( style
& wxMINIMIZE_BOX
) || ( style
& wxMAXIMIZE_BOX
) ||
1567 ( style
& wxSYSTEM_MENU
) || ( style
& wxCAPTION
) ||
1568 ( style
&wxTINY_CAPTION_HORIZ
) || ( style
&wxTINY_CAPTION_VERT
)
1571 if ( ( style
& wxSTAY_ON_TOP
) )
1572 wclass
= kUtilityWindowClass
;
1574 wclass
= kFloatingWindowClass
;
1576 if ( ( style
&wxTINY_CAPTION_VERT
) )
1577 attr
|= kWindowSideTitlebarAttribute
;
1581 wclass
= kPlainWindowClass
;
1582 activationScopeSet
= true;
1583 activationScope
= kWindowActivationScopeNone
;
1586 else if ( ( style
& wxPOPUP_WINDOW
) )
1588 if ( ( style
& wxBORDER_NONE
) )
1590 wclass
= kHelpWindowClass
; // has no border
1591 attr
|= kWindowNoShadowAttribute
;
1595 wclass
= kPlainWindowClass
; // has a single line border, it will have to do for now
1597 group
= GetWindowGroupOfClass(kFloatingWindowClass
) ;
1598 // make sure we don't deactivate something
1599 activationScopeSet
= true;
1600 activationScope
= kWindowActivationScopeNone
;
1602 else if ( ( style
& wxCAPTION
) )
1604 wclass
= kDocumentWindowClass
;
1605 attr
|= kWindowInWindowMenuAttribute
;
1607 else if ( ( style
& wxFRAME_DRAWER
) )
1609 wclass
= kDrawerWindowClass
;
1613 if ( ( style
& wxMINIMIZE_BOX
) || ( style
& wxMAXIMIZE_BOX
) ||
1614 ( style
& wxCLOSE_BOX
) || ( style
& wxSYSTEM_MENU
) )
1616 wclass
= kDocumentWindowClass
;
1618 else if ( ( style
& wxNO_BORDER
) )
1620 wclass
= kSimpleWindowClass
;
1624 wclass
= kPlainWindowClass
;
1628 if ( wclass
!= kPlainWindowClass
)
1630 if ( ( style
& wxMINIMIZE_BOX
) )
1631 attr
|= kWindowCollapseBoxAttribute
;
1633 if ( ( style
& wxMAXIMIZE_BOX
) )
1634 attr
|= kWindowFullZoomAttribute
;
1636 if ( ( style
& wxRESIZE_BORDER
) )
1637 attr
|= kWindowResizableAttribute
;
1639 if ( ( style
& wxCLOSE_BOX
) )
1640 attr
|= kWindowCloseBoxAttribute
;
1642 attr
|= kWindowLiveResizeAttribute
;
1644 if ( ( style
&wxSTAY_ON_TOP
) )
1645 group
= GetWindowGroupOfClass(kUtilityWindowClass
) ;
1647 if ( ( style
& wxFRAME_FLOAT_ON_PARENT
) )
1648 group
= GetWindowGroupOfClass(kFloatingWindowClass
) ;
1650 if ( group
== NULL
&& parent
!= NULL
)
1652 WindowRef parenttlw
= (WindowRef
) parent
->MacGetTopLevelWindowRef();
1654 group
= GetWindowGroupParent( GetWindowGroup( parenttlw
) );
1657 attr
|= kWindowCompositingAttribute
;
1658 #if 0 // wxOSX_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1659 attr
|= kWindowFrameworkScaledAttribute
;
1662 if ( ( style
&wxFRAME_SHAPED
) )
1664 WindowDefSpec customWindowDefSpec
;
1665 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
1666 customWindowDefSpec
.u
.defProc
=
1668 (WindowDefUPP
) wxShapedMacWindowDef
;
1670 NewWindowDefUPP(wxShapedMacWindowDef
);
1672 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
1673 attr
, &theBoundsRect
,
1674 (WindowRef
*) &m_macWindow
);
1678 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
1681 if ( err
== noErr
&& m_macWindow
!= NULL
&& group
!= NULL
)
1682 SetWindowGroup( (WindowRef
) m_macWindow
, group
) ;
1684 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
1686 // setup a separate group for each window, so that overlays can be handled easily
1688 WindowGroupRef overlaygroup
= NULL
;
1689 verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether
| kWindowGroupAttrLayerTogether
| kWindowGroupAttrHideOnCollapse
, &overlaygroup
));
1690 verify_noerr( SetWindowGroupParent( overlaygroup
, GetWindowGroup( (WindowRef
) m_macWindow
)));
1691 verify_noerr( SetWindowGroup( (WindowRef
) m_macWindow
, overlaygroup
));
1693 if ( activationScopeSet
)
1695 verify_noerr( SetWindowActivationScope( (WindowRef
) m_macWindow
, activationScope
));
1698 // the create commands are only for content rect,
1699 // so we have to set the size again as structure bounds
1700 SetWindowBounds( m_macWindow
, kWindowStructureRgn
, &theBoundsRect
) ;
1702 // Causes the inner part of the window not to be metal
1703 // if the style is used before window creation.
1704 #if 0 // TARGET_API_MAC_OSX
1705 if ( m_macUsesCompositing
&& m_macWindow
!= NULL
)
1707 if ( GetExtraStyle() & wxFRAME_EX_METAL
)
1708 MacSetMetalAppearance( true ) ;
1712 if ( m_macWindow
!= NULL
)
1714 MacSetUnifiedAppearance( true ) ;
1717 HIViewRef growBoxRef
= 0 ;
1718 err
= HIViewFindByID( HIViewGetRoot( m_macWindow
), kHIViewWindowGrowBoxID
, &growBoxRef
);
1719 if ( err
== noErr
&& growBoxRef
!= 0 )
1720 HIGrowBoxViewSetTransparent( growBoxRef
, true ) ;
1722 // the frame window event handler
1723 InstallStandardEventHandler( GetWindowEventTarget(m_macWindow
) ) ;
1724 MacInstallTopLevelWindowEventHandler() ;
1726 if ( extraStyle
& wxFRAME_EX_METAL
)
1727 MacSetMetalAppearance(true);
1729 if ( ( style
&wxFRAME_SHAPED
) )
1731 // default shape matches the window size
1732 wxRegion
rgn( 0, 0, w
, h
);
1737 bool wxNonOwnedWindowCarbonImpl::ShowWithEffect(bool show
,
1738 wxShowEffect effect
,
1741 WindowTransitionEffect transition
= 0 ;
1744 case wxSHOW_EFFECT_ROLL_TO_LEFT
:
1745 case wxSHOW_EFFECT_ROLL_TO_RIGHT
:
1746 case wxSHOW_EFFECT_ROLL_TO_TOP
:
1747 case wxSHOW_EFFECT_ROLL_TO_BOTTOM
:
1748 case wxSHOW_EFFECT_SLIDE_TO_LEFT
:
1749 case wxSHOW_EFFECT_SLIDE_TO_RIGHT
:
1750 case wxSHOW_EFFECT_SLIDE_TO_TOP
:
1751 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM
:
1752 transition
= kWindowGenieTransitionEffect
;
1754 case wxSHOW_EFFECT_BLEND
:
1755 transition
= kWindowFadeTransitionEffect
;
1757 case wxSHOW_EFFECT_EXPAND
:
1758 // having sheets would be fine, but this might lead to a repositioning
1761 transition
= kWindowSheetTransitionEffect
;
1764 transition
= kWindowZoomTransitionEffect
;
1767 case wxSHOW_EFFECT_MAX
:
1768 wxFAIL_MSG( "invalid effect flag" );
1772 TransitionWindowOptions options
;
1773 options
.version
= 0;
1774 options
.duration
= timeout
/ 1000.0;
1775 options
.window
= transition
== kWindowSheetTransitionEffect
? (WindowRef
) m_wxPeer
->GetParent()->MacGetTopLevelWindowRef() :0;
1776 options
.userData
= 0;
1778 wxSize size
= wxGetDisplaySize();
1780 GetWindowBounds( (WindowRef
)m_macWindow
, kWindowStructureRgn
, &bounds
);
1781 CGRect hiBounds
= CGRectMake( bounds
.left
, bounds
.top
, bounds
.right
- bounds
.left
, bounds
.bottom
- bounds
.top
);
1785 case wxSHOW_EFFECT_ROLL_TO_RIGHT
:
1786 case wxSHOW_EFFECT_SLIDE_TO_RIGHT
:
1787 hiBounds
.origin
.x
= 0;
1788 hiBounds
.size
.width
= 0;
1791 case wxSHOW_EFFECT_ROLL_TO_LEFT
:
1792 case wxSHOW_EFFECT_SLIDE_TO_LEFT
:
1793 hiBounds
.origin
.x
= size
.x
;
1794 hiBounds
.size
.width
= 0;
1797 case wxSHOW_EFFECT_ROLL_TO_TOP
:
1798 case wxSHOW_EFFECT_SLIDE_TO_TOP
:
1799 hiBounds
.origin
.y
= size
.y
;
1800 hiBounds
.size
.height
= 0;
1803 case wxSHOW_EFFECT_ROLL_TO_BOTTOM
:
1804 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM
:
1805 hiBounds
.origin
.y
= 0;
1806 hiBounds
.size
.height
= 0;
1810 break; // direction doesn't make sense
1813 ::TransitionWindowWithOptions
1815 (WindowRef
)m_macWindow
,
1817 show
? kWindowShowTransitionAction
: kWindowHideTransitionAction
,
1818 transition
== kWindowGenieTransitionEffect
? &hiBounds
: NULL
,
1825 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1831 void wxNonOwnedWindowCarbonImpl::SetTitle( const wxString
& title
, wxFontEncoding encoding
)
1833 SetWindowTitleWithCFString( m_macWindow
, wxCFStringRef( title
, encoding
) ) ;
1836 bool wxNonOwnedWindowCarbonImpl::IsMaximized() const
1838 return IsWindowInStandardState( m_macWindow
, NULL
, NULL
) ;
1841 bool wxNonOwnedWindowCarbonImpl::IsIconized() const
1843 return IsWindowCollapsed((WindowRef
)GetWXWindow() ) ;
1846 void wxNonOwnedWindowCarbonImpl::Iconize( bool iconize
)
1848 if ( IsWindowCollapsable( m_macWindow
) )
1849 CollapseWindow( m_macWindow
, iconize
) ;
1852 void wxNonOwnedWindowCarbonImpl::Maximize(bool maximize
)
1854 Point idealSize
= { 0 , 0 } ;
1857 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1859 HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay
,kHICoordSpace72DPIGlobal
,
1861 idealSize
.h
= bounds
.size
.width
;
1862 idealSize
.v
= bounds
.size
.height
;
1865 GetAvailableWindowPositioningBounds(GetMainDevice(),&rect
) ;
1866 idealSize
.h
= rect
.right
- rect
.left
;
1867 idealSize
.v
= rect
.bottom
- rect
.top
;
1870 ZoomWindowIdeal( (WindowRef
)GetWXWindow() , maximize
? inZoomOut
: inZoomIn
, &idealSize
) ;
1873 bool wxNonOwnedWindowCarbonImpl::IsFullScreen() const
1875 return m_macFullScreenData
!= NULL
;
1878 bool wxNonOwnedWindowCarbonImpl::ShowFullScreen(bool show
, long style
)
1882 FullScreenData
*data
= (FullScreenData
*)m_macFullScreenData
;
1884 data
= new FullScreenData() ;
1886 m_macFullScreenData
= data
;
1887 data
->m_position
= m_wxPeer
->GetPosition() ;
1888 data
->m_size
= m_wxPeer
->GetSize() ;
1889 #if wxOSX_USE_CARBON
1890 WindowAttributes attr
= 0;
1891 GetWindowAttributes((WindowRef
) GetWXWindow(), &attr
);
1892 data
->m_wasResizable
= attr
& kWindowResizableAttribute
;
1893 if ( style
& wxFULLSCREEN_NOMENUBAR
)
1897 wxRect client
= wxGetClientDisplayRect() ;
1899 int left
, top
, right
, bottom
;
1907 GetContentArea( left
, top
, right
, bottom
) ;
1909 if ( style
& wxFULLSCREEN_NOCAPTION
)
1915 if ( style
& wxFULLSCREEN_NOBORDER
)
1922 if ( style
& wxFULLSCREEN_NOTOOLBAR
)
1927 if ( style
& wxFULLSCREEN_NOSTATUSBAR
)
1932 m_wxPeer
->SetSize( x
, y
, w
, h
) ;
1933 if ( data
->m_wasResizable
)
1935 #if wxOSX_USE_CARBON
1936 ChangeWindowAttributes( (WindowRef
) GetWXWindow() , kWindowNoAttributes
, kWindowResizableAttribute
) ;
1940 else if ( m_macFullScreenData
!= NULL
)
1942 FullScreenData
*data
= (FullScreenData
*) m_macFullScreenData
;
1943 #if wxOSX_USE_CARBON
1945 if ( data
->m_wasResizable
)
1946 ChangeWindowAttributes( (WindowRef
) GetWXWindow() , kWindowResizableAttribute
, kWindowNoAttributes
) ;
1948 m_wxPeer
->SetPosition( data
->m_position
) ;
1949 m_wxPeer
->SetSize( data
->m_size
) ;
1952 m_macFullScreenData
= NULL
;
1958 // Attracts the users attention to this window if the application is
1959 // inactive (should be called when a background event occurs)
1961 static pascal void wxMacNMResponse( NMRecPtr ptr
)
1964 DisposePtr( (Ptr
)ptr
) ;
1967 void wxNonOwnedWindowCarbonImpl::RequestUserAttention(int WXUNUSED(flags
))
1969 NMRecPtr notificationRequest
= (NMRecPtr
) NewPtr( sizeof( NMRec
) ) ;
1971 memset( notificationRequest
, 0 , sizeof(*notificationRequest
) ) ;
1972 notificationRequest
->qType
= nmType
;
1973 notificationRequest
->nmMark
= 1 ;
1974 notificationRequest
->nmIcon
= 0 ;
1975 notificationRequest
->nmSound
= 0 ;
1976 notificationRequest
->nmStr
= NULL
;
1977 notificationRequest
->nmResp
= wxMacNMResponse
;
1979 verify_noerr( NMInstall( notificationRequest
) ) ;
1982 void wxNonOwnedWindowCarbonImpl::ScreenToWindow( int *x
, int *y
)
1984 HIPoint p
= CGPointMake( (x
? *x
: 0), (y
? *y
: 0) );
1985 HIViewRef contentView
;
1986 // TODO check toolbar offset
1987 HIViewFindByID( HIViewGetRoot( m_macWindow
), kHIViewWindowContentID
, &contentView
) ;
1988 HIPointConvert( &p
, kHICoordSpace72DPIGlobal
, NULL
, kHICoordSpaceView
, contentView
);
1995 void wxNonOwnedWindowCarbonImpl::WindowToScreen( int *x
, int *y
)
1997 HIPoint p
= CGPointMake( (x
? *x
: 0), (y
? *y
: 0) );
1998 HIViewRef contentView
;
1999 // TODO check toolbar offset
2000 HIViewFindByID( HIViewGetRoot( m_macWindow
), kHIViewWindowContentID
, &contentView
) ;
2001 HIPointConvert( &p
, kHICoordSpaceView
, contentView
, kHICoordSpace72DPIGlobal
, NULL
);
2007 #endif // wxOSX_USE_CARBON