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/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)
36 wxWindowDC::wxWindowDC( wxWindow *window )
39 wxFAIL_MSG("non-client window DC's are not supported");
42 wxWindowDC::~wxWindowDC(void)
46 void wxWindowDC::Clear()
50 NSGraphicsContext *context = [NSGraphicsContext currentContext];
51 [context saveGraphicsState];
53 [m_backgroundBrush.GetNSColor() set];
54 [NSBezierPath fillRect:[m_window->GetNSView() bounds]];
56 [context restoreGraphicsState];
62 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
64 wxClientDC::wxClientDC(void)
68 wxClientDC::wxClientDC( wxWindow *window )
73 wxClientDC::~wxClientDC(void)
75 CocoaUnwindStackAndLoseFocus();
78 bool wxClientDC::CocoaLockFocus()
80 wxLogDebug("Locking focus on wxClientDC=%p, NSView=%p",this, m_window->GetNSView());
81 if([m_window->GetNSView() lockFocusIfCanDraw])
83 sm_cocoaDCStack.Insert(this);
84 m_cocoaFlipped = [m_window->GetNSView() isFlipped];
85 m_cocoaHeight = [m_window->GetNSView() bounds].size.height;
86 CocoaApplyTransformations();
89 wxLogDebug("focus lock failed!");
93 bool wxClientDC::CocoaUnlockFocus()
95 wxLogDebug("Unlocking focus on wxClientDC=%p, NSView=%p",this, m_window->GetNSView());
96 [[m_window->GetNSView() window] flushWindow];
97 [m_window->GetNSView() unlockFocus];
104 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
106 wxPaintDC::wxPaintDC(void)
110 wxPaintDC::wxPaintDC( wxWindow *window )
113 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");
114 sm_cocoaDCStack.Insert(this);
115 m_cocoaFlipped = [window->GetNSView() isFlipped];
116 m_cocoaHeight = [window->GetNSView() bounds].size.height;
117 CocoaApplyTransformations();
120 wxPaintDC::~wxPaintDC(void)
122 CocoaUnwindStackAndLoseFocus();
125 bool wxPaintDC::CocoaLockFocus()
127 wxFAIL_MSG("wxPaintDC cannot be asked to lock focus!");
131 bool wxPaintDC::CocoaUnlockFocus()
133 // wxPaintDC focus can never be unlocked.