1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dcclient.mm
3 // Purpose: wxWindowDC, wxPaintDC, and wxClientDC classes
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/window.h"
16 #include "wx/dcclient.h"
19 #import <AppKit/NSView.h>
20 #import <AppKit/NSAffineTransform.h>
21 #import <AppKit/NSColor.h>
22 #import <AppKit/NSGraphicsContext.h>
23 #import <AppKit/NSBezierPath.h>
24 #import <AppKit/NSWindow.h>
29 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
31 wxWindowDC::wxWindowDC(void)
33 , m_lockedNSView(NULL)
37 wxWindowDC::wxWindowDC( wxWindow *window )
39 , m_lockedNSView(NULL)
41 wxLogDebug(wxT("non-client window DC's are not supported, oh well"));
44 wxWindowDC::~wxWindowDC(void)
46 CocoaUnwindStackAndLoseFocus();
49 bool wxWindowDC::CocoaLockFocusOnNSView(WX_NSView nsview)
51 if([nsview lockFocusIfCanDraw])
53 sm_cocoaDCStack.Insert(this);
54 CocoaApplyTransformations();
55 m_lockedNSView = nsview;
58 wxLogDebug(wxT("focus lock failed!"));
62 bool wxWindowDC::CocoaUnlockFocusOnNSView()
64 [[m_lockedNSView window] flushWindow];
65 [m_lockedNSView unlockFocus];
66 m_lockedNSView = NULL;
70 bool wxWindowDC::CocoaLockFocus()
72 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView());
73 m_cocoaWxToBoundsTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height);
74 return CocoaLockFocusOnNSView(m_window->GetNonClientNSView());
77 bool wxWindowDC::CocoaUnlockFocus()
79 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView());
80 return CocoaUnlockFocusOnNSView();
83 void wxWindowDC::Clear()
85 if(!CocoaTakeFocus()) return;
87 NSGraphicsContext *context = [NSGraphicsContext currentContext];
88 [context saveGraphicsState];
90 [m_backgroundBrush.GetNSColor() set];
91 [NSBezierPath fillRect:[m_lockedNSView bounds]];
93 [context restoreGraphicsState];
99 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
101 wxClientDC::wxClientDC(void)
105 wxClientDC::wxClientDC( wxWindow *window )
110 wxClientDC::~wxClientDC(void)
112 CocoaUnwindStackAndLoseFocus();
115 bool wxClientDC::CocoaLockFocus()
117 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
118 m_cocoaWxToBoundsTransform = m_window->CocoaGetWxToBoundsTransform();
119 return CocoaLockFocusOnNSView(m_window->GetNSView());
122 bool wxClientDC::CocoaUnlockFocus()
124 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
125 return CocoaUnlockFocusOnNSView();
131 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
133 wxPaintDC::wxPaintDC(void)
137 wxPaintDC::wxPaintDC( wxWindow *window )
140 wxASSERT_MSG([NSView focusView]==window->GetNSView(), wxT("PaintDC's NSView does not have focus. Please use wxPaintDC only as the first DC created in a paint handler"));
141 sm_cocoaDCStack.Insert(this);
142 m_lockedNSView = window->GetNSView();
143 m_cocoaWxToBoundsTransform = window->CocoaGetWxToBoundsTransform();
144 CocoaApplyTransformations();
147 wxPaintDC::~wxPaintDC(void)
149 CocoaUnwindStackAndLoseFocus();
152 bool wxPaintDC::CocoaLockFocus()
154 wxFAIL_MSG(wxT("wxPaintDC cannot be asked to lock focus!"));
158 bool wxPaintDC::CocoaUnlockFocus()
160 // wxPaintDC focus can never be unlocked.