1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dcclient.mm
3 // Purpose: wxWindowDCImpl, wxPaintDCImpl, and wxClientDCImpl classes
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/window.h"
18 #include "wx/cocoa/dcclient.h"
20 #import <AppKit/NSView.h>
21 #import <AppKit/NSAffineTransform.h>
22 #import <AppKit/NSColor.h>
23 #import <AppKit/NSGraphicsContext.h>
24 #import <AppKit/NSBezierPath.h>
25 #import <AppKit/NSWindow.h>
30 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxCocoaDCImpl)
32 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner)
33 : wxCocoaDCImpl(owner)
35 , m_lockedNSView(NULL)
39 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner, wxWindow *window)
40 : wxCocoaDCImpl(owner)
42 , m_lockedNSView(NULL)
44 wxLogDebug(wxT("non-client window DC's are not supported, oh well"));
47 wxWindowDCImpl::~wxWindowDCImpl(void)
49 CocoaUnwindStackAndLoseFocus();
52 bool wxWindowDCImpl::CocoaLockFocusOnNSView(WX_NSView nsview)
54 if([nsview lockFocusIfCanDraw])
56 sm_cocoaDCStack.Insert(this);
57 CocoaApplyTransformations();
58 m_lockedNSView = nsview;
61 wxLogDebug(wxT("focus lock failed!"));
65 bool wxWindowDCImpl::CocoaUnlockFocusOnNSView()
67 [[m_lockedNSView window] flushWindow];
68 [m_lockedNSView unlockFocus];
69 m_lockedNSView = NULL;
73 bool wxWindowDCImpl::CocoaLockFocus()
75 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView());
76 NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height);
77 [newTransform retain];
78 [m_cocoaWxToBoundsTransform release];
79 m_cocoaWxToBoundsTransform = newTransform;
80 return CocoaLockFocusOnNSView(m_window->GetNonClientNSView());
83 bool wxWindowDCImpl::CocoaUnlockFocus()
85 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView());
86 return CocoaUnlockFocusOnNSView();
89 bool wxWindowDCImpl::CocoaGetBounds(void *rectData)
95 NSRect *pRect = (NSRect*)rectData;
96 *pRect = [m_lockedNSView bounds];
103 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
105 wxClientDCImpl::wxClientDCImpl(wxDC *owner)
106 : wxWindowDCImpl(owner)
110 wxClientDCImpl::wxClientDCImpl(wxDC *owner, wxWindow *window)
111 : wxWindowDCImpl(owner)
116 wxClientDCImpl::~wxClientDCImpl(void)
118 CocoaUnwindStackAndLoseFocus();
121 bool wxClientDCImpl::CocoaLockFocus()
123 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView());
124 NSAffineTransform *newTransform = m_window->CocoaGetWxToBoundsTransform();
125 [newTransform retain];
126 [m_cocoaWxToBoundsTransform release];
127 m_cocoaWxToBoundsTransform = newTransform;
128 return CocoaLockFocusOnNSView(m_window->GetNSView());
131 bool wxClientDCImpl::CocoaUnlockFocus()
133 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView());
134 return CocoaUnlockFocusOnNSView();
140 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
142 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner)
143 : wxWindowDCImpl(owner)
147 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner, wxWindow *window)
148 : wxWindowDCImpl(owner)
151 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"));
152 sm_cocoaDCStack.Insert(this);
153 m_lockedNSView = window->GetNSView();
154 NSAffineTransform *newTransform = window->CocoaGetWxToBoundsTransform();
155 [newTransform retain];
156 [m_cocoaWxToBoundsTransform release];
157 m_cocoaWxToBoundsTransform = newTransform;
158 CocoaApplyTransformations();
161 wxPaintDCImpl::~wxPaintDCImpl(void)
163 CocoaUnwindStackAndLoseFocus();
166 bool wxPaintDCImpl::CocoaLockFocus()
168 wxFAIL_MSG(wxT("wxPaintDCImpl cannot be asked to lock focus!"));
172 bool wxPaintDCImpl::CocoaUnlockFocus()
174 // wxPaintDCImpl focus can never be unlocked.