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 m_cocoaFlipped = [nsview isFlipped];
55 m_cocoaHeight = [nsview bounds].size.height;
56 CocoaApplyTransformations();
57 m_lockedNSView = nsview;
60 wxLogDebug(wxT("focus lock failed!"));
64 bool wxWindowDC::CocoaUnlockFocusOnNSView()
66 [[m_lockedNSView window] flushWindow];
67 [m_lockedNSView unlockFocus];
68 m_lockedNSView = NULL;
72 bool wxWindowDC::CocoaLockFocus()
74 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView());
75 return CocoaLockFocusOnNSView(m_window->GetNonClientNSView());
78 bool wxWindowDC::CocoaUnlockFocus()
80 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView());
81 return CocoaUnlockFocusOnNSView();
84 void wxWindowDC::Clear()
86 if(!CocoaTakeFocus()) return;
88 NSGraphicsContext *context = [NSGraphicsContext currentContext];
89 [context saveGraphicsState];
91 [m_backgroundBrush.GetNSColor() set];
92 [NSBezierPath fillRect:[m_lockedNSView bounds]];
94 [context restoreGraphicsState];
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 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_cocoaFlipped = [window->GetNSView() isFlipped];
144 m_cocoaHeight = [window->GetNSView() bounds].size.height;
145 CocoaApplyTransformations();
148 wxPaintDC::~wxPaintDC(void)
150 CocoaUnwindStackAndLoseFocus();
153 bool wxPaintDC::CocoaLockFocus()
155 wxFAIL_MSG(wxT("wxPaintDC cannot be asked to lock focus!"));
159 bool wxPaintDC::CocoaUnlockFocus()
161 // wxPaintDC focus can never be unlocked.