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: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/dcclient.h"
13 #include "wx/window.h"
16 #import <AppKit/NSView.h>
17 #import <AppKit/NSAffineTransform.h>
18 #import <AppKit/NSColor.h>
19 #import <AppKit/NSGraphicsContext.h>
20 #import <AppKit/NSBezierPath.h>
21 #import <AppKit/NSWindow.h>
26 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
28 wxWindowDC::wxWindowDC(void)
33 wxWindowDC::wxWindowDC( wxWindow *window )
36 wxFAIL_MSG("non-client window DC's are not supported");
39 wxWindowDC::~wxWindowDC(void)
43 void wxWindowDC::Clear()
47 NSGraphicsContext *context = [NSGraphicsContext currentContext];
48 [context saveGraphicsState];
50 [m_backgroundBrush.GetNSColor() set];
51 [NSBezierPath fillRect:[m_window->GetNSView() bounds]];
53 [context restoreGraphicsState];
59 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
61 wxClientDC::wxClientDC(void)
65 wxClientDC::wxClientDC( wxWindow *window )
70 wxClientDC::~wxClientDC(void)
72 CocoaUnwindStackAndLoseFocus();
75 bool wxClientDC::CocoaLockFocus()
77 wxLogDebug("Locking focus on wxClientDC=%p, NSView=%p",this, m_window->GetNSView());
78 if([m_window->GetNSView() lockFocusIfCanDraw])
80 sm_cocoaDCStack.Insert(this);
81 m_cocoaFlipped = [m_window->GetNSView() isFlipped];
82 m_cocoaHeight = [m_window->GetNSView() bounds].size.height;
83 CocoaApplyTransformations();
86 wxLogDebug("focus lock failed!");
90 bool wxClientDC::CocoaUnlockFocus()
92 wxLogDebug("Unlocking focus on wxClientDC=%p, NSView=%p",this, m_window->GetNSView());
93 [[m_window->GetNSView() window] flushWindow];
94 [m_window->GetNSView() unlockFocus];
101 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
103 wxPaintDC::wxPaintDC(void)
107 wxPaintDC::wxPaintDC( wxWindow *window )
110 wxASSERT_MSG([NSView focusView]==window->GetNSView(), "PaintDC's NSView does not have focus. Please use wxPaintDC only as the first DC created in a paint handler");
111 sm_cocoaDCStack.Insert(this);
112 m_cocoaFlipped = [window->GetNSView() isFlipped];
113 m_cocoaHeight = [window->GetNSView() bounds].size.height;
114 CocoaApplyTransformations();
117 wxPaintDC::~wxPaintDC(void)
119 CocoaUnwindStackAndLoseFocus();
122 bool wxPaintDC::CocoaLockFocus()
124 wxFAIL_MSG("wxPaintDC cannot be asked to lock focus!");
128 bool wxPaintDC::CocoaUnlockFocus()
130 // wxPaintDC focus can never be unlocked.