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 NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height);
74 [newTransform retain];
75 [m_cocoaWxToBoundsTransform release];
76 m_cocoaWxToBoundsTransform = newTransform;
77 return CocoaLockFocusOnNSView(m_window->GetNonClientNSView());
80 bool wxWindowDC::CocoaUnlockFocus()
82 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView());
83 return CocoaUnlockFocusOnNSView();
86 bool wxWindowDC::CocoaGetBounds(void *rectData)
92 NSRect *pRect = (NSRect*)rectData;
93 *pRect = [m_lockedNSView bounds];
100 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
102 wxClientDC::wxClientDC(void)
106 wxClientDC::wxClientDC( wxWindow *window )
111 wxClientDC::~wxClientDC(void)
113 CocoaUnwindStackAndLoseFocus();
116 bool wxClientDC::CocoaLockFocus()
118 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
119 NSAffineTransform *newTransform = m_window->CocoaGetWxToBoundsTransform();
120 [newTransform retain];
121 [m_cocoaWxToBoundsTransform release];
122 m_cocoaWxToBoundsTransform = newTransform;
123 return CocoaLockFocusOnNSView(m_window->GetNSView());
126 bool wxClientDC::CocoaUnlockFocus()
128 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
129 return CocoaUnlockFocusOnNSView();
135 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
137 wxPaintDC::wxPaintDC(void)
141 wxPaintDC::wxPaintDC( wxWindow *window )
144 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"));
145 sm_cocoaDCStack.Insert(this);
146 m_lockedNSView = window->GetNSView();
147 NSAffineTransform *newTransform = window->CocoaGetWxToBoundsTransform();
148 [newTransform retain];
149 [m_cocoaWxToBoundsTransform release];
150 m_cocoaWxToBoundsTransform = newTransform;
151 CocoaApplyTransformations();
154 wxPaintDC::~wxPaintDC(void)
156 CocoaUnwindStackAndLoseFocus();
159 bool wxPaintDC::CocoaLockFocus()
161 wxFAIL_MSG(wxT("wxPaintDC cannot be asked to lock focus!"));
165 bool wxPaintDC::CocoaUnlockFocus()
167 // wxPaintDC focus can never be unlocked.