1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/osx/cocoa/overlay.mm
 
   3 // Purpose:     common wxOverlay code
 
   4 // Author:      Stefan Csomor
 
   7 // Copyright:   (c) wxWidgets team
 
   8 // Licence:     wxWindows licence
 
   9 /////////////////////////////////////////////////////////////////////////////
 
  11 // ============================================================================
 
  13 // ============================================================================
 
  15 // ----------------------------------------------------------------------------
 
  17 // ----------------------------------------------------------------------------
 
  19 // For compilers that support precompilation, includes "wx.h".
 
  20 #include "wx/wxprec.h"
 
  26 #include "wx/overlay.h"
 
  29     #include "wx/dcclient.h"
 
  32 #include "wx/dcgraph.h"
 
  34 #include "wx/private/overlay.h"
 
  36 #ifdef wxHAS_NATIVE_OVERLAY
 
  38 // ============================================================================
 
  40 // ============================================================================
 
  42 wxOverlayImpl::wxOverlayImpl()
 
  45     m_overlayContext = NULL ;
 
  46     m_overlayWindow = NULL ;
 
  49 wxOverlayImpl::~wxOverlayImpl()
 
  54 bool wxOverlayImpl::IsOk()
 
  56     return m_overlayWindow != NULL ;
 
  59 void wxOverlayImpl::CreateOverlayWindow()
 
  63         m_overlayParentWindow = m_window->MacGetTopLevelWindowRef();
 
  64         [m_overlayParentWindow makeKeyAndOrderFront:nil];
 
  66         NSView* view = m_window->GetHandle();
 
  68         NSPoint viewOriginBase, viewOriginScreen;
 
  69         viewOriginBase = [view convertPoint:NSMakePoint(0, 0) toView:nil];
 
  70         viewOriginScreen = [m_overlayParentWindow convertBaseToScreen:viewOriginBase];
 
  72         NSSize viewSize = [view frame].size;
 
  73         if ( [view isFlipped] )
 
  74             viewOriginScreen.y -= viewSize.height;
 
  76         m_overlayWindow=[[NSWindow alloc] initWithContentRect:NSMakeRect(viewOriginScreen.x,viewOriginScreen.y,
 
  79                                                     styleMask:NSBorderlessWindowMask 
 
  80                                                       backing:NSBackingStoreBuffered 
 
  83         [m_overlayParentWindow addChildWindow:m_overlayWindow ordered:NSWindowAbove];
 
  87         m_overlayParentWindow = NULL ;
 
  89         cgbounds = CGDisplayBounds(CGMainDisplayID());
 
  91         m_overlayWindow=[[NSWindow alloc] initWithContentRect:NSMakeRect(cgbounds.origin.x,cgbounds.origin.y,
 
  94                                                   styleMask:NSBorderlessWindowMask 
 
  95                                                     backing:NSBackingStoreBuffered 
 
  98     [m_overlayWindow setOpaque:NO];
 
  99     [m_overlayWindow setIgnoresMouseEvents:YES];
 
 100     [m_overlayWindow setAlphaValue:1.0];
 
 102     [m_overlayWindow orderFront:nil];
 
 105 void wxOverlayImpl::Init( wxDC* dc, int x , int y , int width , int height )
 
 107     wxASSERT_MSG( !IsOk() , _("You cannot Init an overlay twice") );
 
 109     m_window = dc->GetWindow();
 
 112     if ( dc->IsKindOf( CLASSINFO( wxClientDC ) ))
 
 114         wxPoint origin = m_window->GetClientAreaOrigin();
 
 121     CreateOverlayWindow();
 
 122     wxASSERT_MSG(  m_overlayWindow != NULL , _("Couldn't create the overlay window") );
 
 123     m_overlayContext = (CGContextRef) [[m_overlayWindow graphicsContext] graphicsPort];
 
 124     wxASSERT_MSG(  m_overlayContext != NULL , _("Couldn't init the context on the overlay window") );
 
 129         NSView* view = m_window->GetHandle();    
 
 130         NSSize viewSize = [view frame].size;
 
 131         ySize = viewSize.height;
 
 136         cgbounds = CGDisplayBounds(CGMainDisplayID());
 
 137         ySize = cgbounds.size.height;
 
 142     CGContextTranslateCTM( m_overlayContext, 0, ySize );
 
 143     CGContextScaleCTM( m_overlayContext, 1, -1 );
 
 144     CGContextTranslateCTM( m_overlayContext, -m_x , -m_y );
 
 147 void wxOverlayImpl::BeginDrawing( wxDC* dc)
 
 149     wxDCImpl *impl = dc->GetImpl();
 
 150     wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
 
 153         win_impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( m_overlayContext ) );
 
 154         dc->SetClippingRegion( m_x , m_y , m_width , m_height ) ;
 
 158 void wxOverlayImpl::EndDrawing( wxDC* dc)
 
 160     wxDCImpl *impl = dc->GetImpl();
 
 161     wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
 
 163         win_impl->SetGraphicsContext(NULL);
 
 165     CGContextFlush( m_overlayContext );
 
 168 void wxOverlayImpl::Clear(wxDC* WXUNUSED(dc))
 
 170     wxASSERT_MSG( IsOk() , _("You cannot Clear an overlay that is not inited") );
 
 171     CGRect box  = CGRectMake( m_x - 1, m_y - 1 , m_width + 2 , m_height + 2 );
 
 172     CGContextClearRect( m_overlayContext, box );
 
 175 void wxOverlayImpl::Reset()
 
 177     if ( m_overlayContext )
 
 179         m_overlayContext = NULL ;
 
 182     // todo : don't dispose, only hide and reposition on next run
 
 185         [m_overlayParentWindow removeChildWindow:m_overlayWindow];
 
 186         [m_overlayWindow release];
 
 187         m_overlayWindow = NULL ;
 
 191 #endif // wxHAS_NATIVE_OVERLAY