1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/overlay.mm
3 // Purpose: common wxOverlay code
4 // Author: Stefan Csomor
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/overlay.h"
30 #include "wx/dcclient.h"
33 #include "wx/dcgraph.h"
35 #include "wx/private/overlay.h"
37 #ifdef wxHAS_NATIVE_OVERLAY
39 // ============================================================================
41 // ============================================================================
43 wxOverlayImpl::wxOverlayImpl()
46 m_overlayContext = NULL ;
47 m_overlayWindow = NULL ;
50 wxOverlayImpl::~wxOverlayImpl()
55 bool wxOverlayImpl::IsOk()
57 return m_overlayWindow != NULL ;
60 void wxOverlayImpl::CreateOverlayWindow()
64 m_overlayParentWindow = m_window->MacGetTopLevelWindowRef();
65 [m_overlayParentWindow makeKeyAndOrderFront:nil];
67 NSView* view = m_window->GetHandle();
69 NSPoint viewOriginBase, viewOriginScreen;
70 viewOriginBase = [view convertPoint:NSMakePoint(0, 0) toView:nil];
71 viewOriginScreen = [m_overlayParentWindow convertBaseToScreen:viewOriginBase];
73 NSSize viewSize = [view frame].size;
74 if ( [view isFlipped] )
75 viewOriginScreen.y -= viewSize.height;
77 m_overlayWindow=[[NSWindow alloc] initWithContentRect:NSMakeRect(viewOriginScreen.x,viewOriginScreen.y,
80 styleMask:NSBorderlessWindowMask
81 backing:NSBackingStoreBuffered
84 [m_overlayParentWindow addChildWindow:m_overlayWindow ordered:NSWindowAbove];
88 m_overlayParentWindow = NULL ;
90 cgbounds = CGDisplayBounds(CGMainDisplayID());
92 m_overlayWindow=[[NSWindow alloc] initWithContentRect:NSMakeRect(cgbounds.origin.x,cgbounds.origin.y,
95 styleMask:NSBorderlessWindowMask
96 backing:NSBackingStoreBuffered
99 [m_overlayWindow setOpaque:NO];
100 [m_overlayWindow setIgnoresMouseEvents:YES];
101 [m_overlayWindow setAlphaValue:1.0];
103 [m_overlayWindow orderFront:nil];
106 void wxOverlayImpl::Init( wxDC* dc, int x , int y , int width , int height )
108 wxASSERT_MSG( !IsOk() , _("You cannot Init an overlay twice") );
110 m_window = dc->GetWindow();
113 if ( dc->IsKindOf( CLASSINFO( wxClientDC ) ))
115 wxPoint origin = m_window->GetClientAreaOrigin();
122 CreateOverlayWindow();
123 wxASSERT_MSG( m_overlayWindow != NULL , _("Couldn't create the overlay window") );
124 m_overlayContext = (CGContextRef) [[m_overlayWindow graphicsContext] graphicsPort];
125 wxASSERT_MSG( m_overlayContext != NULL , _("Couldn't init the context on the overlay window") );
130 NSView* view = m_window->GetHandle();
131 NSSize viewSize = [view frame].size;
132 ySize = viewSize.height;
137 cgbounds = CGDisplayBounds(CGMainDisplayID());
138 ySize = cgbounds.size.height;
143 CGContextTranslateCTM( m_overlayContext, 0, ySize );
144 CGContextScaleCTM( m_overlayContext, 1, -1 );
145 CGContextTranslateCTM( m_overlayContext, -m_x , -m_y );
148 void wxOverlayImpl::BeginDrawing( wxDC* dc)
150 wxDCImpl *impl = dc->GetImpl();
151 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
154 win_impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( m_overlayContext ) );
155 dc->SetClippingRegion( m_x , m_y , m_width , m_height ) ;
159 void wxOverlayImpl::EndDrawing( wxDC* dc)
161 wxDCImpl *impl = dc->GetImpl();
162 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
164 win_impl->SetGraphicsContext(NULL);
166 CGContextFlush( m_overlayContext );
169 void wxOverlayImpl::Clear(wxDC* WXUNUSED(dc))
171 wxASSERT_MSG( IsOk() , _("You cannot Clear an overlay that is not inited") );
172 CGRect box = CGRectMake( m_x - 1, m_y - 1 , m_width + 2 , m_height + 2 );
173 CGContextClearRect( m_overlayContext, box );
176 void wxOverlayImpl::Reset()
178 if ( m_overlayContext )
180 m_overlayContext = NULL ;
183 // todo : don't dispose, only hide and reposition on next run
186 [m_overlayParentWindow removeChildWindow:m_overlayWindow];
187 [m_overlayWindow release];
188 m_overlayWindow = NULL ;
192 #endif // wxHAS_NATIVE_OVERLAY