1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/overlay.mm
3 // Purpose: common wxOverlay code
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: overlay.cpp 55419 2008-09-02 16:53:23Z SC $
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 setHasShadow:YES];
101 [m_overlayWindow setIgnoresMouseEvents:YES];
102 [m_overlayWindow setAlphaValue:0.5];
104 [m_overlayWindow orderFront:nil];
107 void wxOverlayImpl::Init( wxDC* dc, int x , int y , int width , int height )
109 wxASSERT_MSG( !IsOk() , _("You cannot Init an overlay twice") );
111 m_window = dc->GetWindow();
114 if ( dc->IsKindOf( CLASSINFO( wxClientDC ) ))
116 wxPoint origin = m_window->GetClientAreaOrigin();
123 CreateOverlayWindow();
124 wxASSERT_MSG( m_overlayWindow != NULL , _("Couldn't create the overlay window") );
125 m_overlayContext = (CGContextRef) [[m_overlayWindow graphicsContext] graphicsPort];
126 wxASSERT_MSG( m_overlayContext != NULL , _("Couldn't init the context on the overlay window") );
131 NSView* view = m_window->GetHandle();
132 NSSize viewSize = [view frame].size;
133 ySize = viewSize.height;
138 cgbounds = CGDisplayBounds(CGMainDisplayID());
139 ySize = cgbounds.size.height;
144 CGContextTranslateCTM( m_overlayContext, 0, ySize );
145 CGContextScaleCTM( m_overlayContext, 1, -1 );
146 CGContextTranslateCTM( m_overlayContext, -m_x , -m_y );
149 void wxOverlayImpl::BeginDrawing( wxDC* dc)
151 wxDCImpl *impl = dc->GetImpl();
152 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
155 win_impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( m_overlayContext ) );
156 dc->SetClippingRegion( m_x , m_y , m_width , m_height ) ;
160 void wxOverlayImpl::EndDrawing( wxDC* dc)
162 wxDCImpl *impl = dc->GetImpl();
163 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
165 win_impl->SetGraphicsContext(NULL);
167 CGContextFlush( m_overlayContext );
170 void wxOverlayImpl::Clear(wxDC* WXUNUSED(dc))
172 wxASSERT_MSG( IsOk() , _("You cannot Clear an overlay that is not inited") );
173 CGRect box = CGRectMake( m_x - 1, m_y - 1 , m_width + 2 , m_height + 2 );
174 CGContextClearRect( m_overlayContext, box );
177 void wxOverlayImpl::Reset()
179 if ( m_overlayContext )
181 m_overlayContext = NULL ;
184 // todo : don't dispose, only hide and reposition on next run
187 [m_overlayParentWindow removeChildWindow:m_overlayWindow];
188 [m_overlayWindow release];
189 m_overlayWindow = NULL ;
193 #endif // wxHAS_NATIVE_OVERLAY