1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dcclient.mm
3 // Purpose: wxWindowDCImpl, wxPaintDCImpl, and wxClientDCImpl classes
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
14 #include "wx/window.h"
17 #include "wx/cocoa/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_ABSTRACT_CLASS(wxWindowDCImpl, wxCocoaDCImpl)
31 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner)
32 : wxCocoaDCImpl(owner)
34 , m_lockedNSView(NULL)
38 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner, wxWindow *window)
39 : wxCocoaDCImpl(owner)
41 , m_lockedNSView(NULL)
43 wxLogDebug(wxT("non-client window DC's are not supported, oh well"));
46 wxWindowDCImpl::~wxWindowDCImpl(void)
48 CocoaUnwindStackAndLoseFocus();
51 bool wxWindowDCImpl::CocoaLockFocusOnNSView(WX_NSView nsview)
53 if([nsview lockFocusIfCanDraw])
55 sm_cocoaDCStack.Insert(this);
56 CocoaApplyTransformations();
57 m_lockedNSView = nsview;
60 wxLogDebug(wxT("focus lock failed!"));
64 bool wxWindowDCImpl::CocoaUnlockFocusOnNSView()
66 [[m_lockedNSView window] flushWindow];
67 [m_lockedNSView unlockFocus];
68 m_lockedNSView = NULL;
72 bool wxWindowDCImpl::CocoaLockFocus()
74 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView());
75 NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height);
76 [newTransform retain];
77 [m_cocoaWxToBoundsTransform release];
78 m_cocoaWxToBoundsTransform = newTransform;
79 return CocoaLockFocusOnNSView(m_window->GetNonClientNSView());
82 bool wxWindowDCImpl::CocoaUnlockFocus()
84 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView());
85 return CocoaUnlockFocusOnNSView();
88 bool wxWindowDCImpl::CocoaGetBounds(void *rectData)
94 NSRect *pRect = (NSRect*)rectData;
95 *pRect = [m_lockedNSView bounds];
102 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
104 wxClientDCImpl::wxClientDCImpl(wxDC *owner)
105 : wxWindowDCImpl(owner)
109 wxClientDCImpl::wxClientDCImpl(wxDC *owner, wxWindow *window)
110 : wxWindowDCImpl(owner)
115 wxClientDCImpl::~wxClientDCImpl(void)
117 CocoaUnwindStackAndLoseFocus();
120 bool wxClientDCImpl::CocoaLockFocus()
122 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView());
123 NSAffineTransform *newTransform = m_window->CocoaGetWxToBoundsTransform();
124 [newTransform retain];
125 [m_cocoaWxToBoundsTransform release];
126 m_cocoaWxToBoundsTransform = newTransform;
127 return CocoaLockFocusOnNSView(m_window->GetNSView());
130 bool wxClientDCImpl::CocoaUnlockFocus()
132 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView());
133 return CocoaUnlockFocusOnNSView();
139 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
141 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner)
142 : wxWindowDCImpl(owner)
146 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner, wxWindow *window)
147 : wxWindowDCImpl(owner)
150 wxASSERT_MSG([NSView focusView]==window->GetNSView(), wxT("PaintDC's NSView does not have focus. Please use wxPaintDCImpl only as the first DC created in a paint handler"));
151 sm_cocoaDCStack.Insert(this);
152 m_lockedNSView = window->GetNSView();
153 NSAffineTransform *newTransform = window->CocoaGetWxToBoundsTransform();
154 [newTransform retain];
155 [m_cocoaWxToBoundsTransform release];
156 m_cocoaWxToBoundsTransform = newTransform;
157 CocoaApplyTransformations();
160 wxPaintDCImpl::~wxPaintDCImpl(void)
162 CocoaUnwindStackAndLoseFocus();
165 bool wxPaintDCImpl::CocoaLockFocus()
167 wxFAIL_MSG(wxT("wxPaintDCImpl cannot be asked to lock focus!"));
171 bool wxPaintDCImpl::CocoaUnlockFocus()
173 // wxPaintDCImpl focus can never be unlocked.