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 bool wxWindowDC::CocoaGetBounds(void *rectData)
89 NSRect *pRect = (NSRect*)rectData;
90 *pRect = [m_lockedNSView bounds];
97 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
99 wxClientDC::wxClientDC(void)
103 wxClientDC::wxClientDC( wxWindow *window )
108 wxClientDC::~wxClientDC(void)
110 CocoaUnwindStackAndLoseFocus();
113 bool wxClientDC::CocoaLockFocus()
115 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
116 m_cocoaWxToBoundsTransform = m_window->CocoaGetWxToBoundsTransform();
117 return CocoaLockFocusOnNSView(m_window->GetNSView());
120 bool wxClientDC::CocoaUnlockFocus()
122 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
123 return CocoaUnlockFocusOnNSView();
129 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
131 wxPaintDC::wxPaintDC(void)
135 wxPaintDC::wxPaintDC( wxWindow *window )
138 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"));
139 sm_cocoaDCStack.Insert(this);
140 m_lockedNSView = window->GetNSView();
141 m_cocoaWxToBoundsTransform = window->CocoaGetWxToBoundsTransform();
142 CocoaApplyTransformations();
145 wxPaintDC::~wxPaintDC(void)
147 CocoaUnwindStackAndLoseFocus();
150 bool wxPaintDC::CocoaLockFocus()
152 wxFAIL_MSG(wxT("wxPaintDC cannot be asked to lock focus!"));
156 bool wxPaintDC::CocoaUnlockFocus()
158 // wxPaintDC focus can never be unlocked.