Moved Clear() implementation into wxDC using a new virtual CocoaGetBounds()
[wxWidgets.git] / src / cocoa / dcclient.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/dcclient.mm
3 // Purpose:     wxWindowDC, wxPaintDC, and wxClientDC classes
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/04/01
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14     #include "wx/log.h"
15     #include "wx/window.h"
16     #include "wx/dcclient.h"
17 #endif //WX_PRECOMP
18
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>
25
26 /*
27  * wxWindowDC
28  */
29 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
30
31 wxWindowDC::wxWindowDC(void)
32 :   m_window(NULL)
33 ,   m_lockedNSView(NULL)
34 {
35 };
36
37 wxWindowDC::wxWindowDC( wxWindow *window )
38 :   m_window(window)
39 ,   m_lockedNSView(NULL)
40 {
41     wxLogDebug(wxT("non-client window DC's are not supported, oh well"));
42 };
43
44 wxWindowDC::~wxWindowDC(void)
45 {
46     CocoaUnwindStackAndLoseFocus();
47 };
48
49 bool wxWindowDC::CocoaLockFocusOnNSView(WX_NSView nsview)
50 {
51     if([nsview lockFocusIfCanDraw])
52     {
53         sm_cocoaDCStack.Insert(this);
54         CocoaApplyTransformations();
55         m_lockedNSView = nsview;
56         return true;
57     }
58     wxLogDebug(wxT("focus lock failed!"));
59     return false;
60 }
61
62 bool wxWindowDC::CocoaUnlockFocusOnNSView()
63 {
64     [[m_lockedNSView window] flushWindow];
65     [m_lockedNSView unlockFocus];
66     m_lockedNSView = NULL;
67     return true;
68 }
69
70 bool wxWindowDC::CocoaLockFocus()
71 {
72     wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView());
73     m_cocoaWxToBoundsTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height);
74     return CocoaLockFocusOnNSView(m_window->GetNonClientNSView());
75 }
76
77 bool wxWindowDC::CocoaUnlockFocus()
78 {
79     wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView());
80     return CocoaUnlockFocusOnNSView();
81 }
82
83 bool wxWindowDC::CocoaGetBounds(void *rectData)
84 {
85     if(!rectData)
86         return false;
87     if(!m_lockedNSView)
88         return false;
89     NSRect *pRect = (NSRect*)rectData;
90     *pRect = [m_lockedNSView bounds];
91     return true;
92 }
93
94 /*
95  * wxClientDC
96  */
97 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
98
99 wxClientDC::wxClientDC(void)
100 {
101 };
102
103 wxClientDC::wxClientDC( wxWindow *window )
104 {
105     m_window = window;
106 };
107
108 wxClientDC::~wxClientDC(void)
109 {
110     CocoaUnwindStackAndLoseFocus();
111 };
112
113 bool wxClientDC::CocoaLockFocus()
114 {
115     wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
116     m_cocoaWxToBoundsTransform = m_window->CocoaGetWxToBoundsTransform();
117     return CocoaLockFocusOnNSView(m_window->GetNSView());
118 }
119
120 bool wxClientDC::CocoaUnlockFocus()
121 {
122     wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView());
123     return CocoaUnlockFocusOnNSView();
124 }
125
126 /*
127  * wxPaintDC
128  */
129 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
130
131 wxPaintDC::wxPaintDC(void)
132 {
133 };
134
135 wxPaintDC::wxPaintDC( wxWindow *window )
136 {
137     m_window = window;
138     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"));
139     sm_cocoaDCStack.Insert(this);
140     m_lockedNSView = window->GetNSView();
141     m_cocoaWxToBoundsTransform = window->CocoaGetWxToBoundsTransform();
142     CocoaApplyTransformations();
143 };
144
145 wxPaintDC::~wxPaintDC(void)
146 {
147     CocoaUnwindStackAndLoseFocus();
148 };
149
150 bool wxPaintDC::CocoaLockFocus()
151 {
152     wxFAIL_MSG(wxT("wxPaintDC cannot be asked to lock focus!"));
153     return false;
154 }
155
156 bool wxPaintDC::CocoaUnlockFocus()
157 {
158     // wxPaintDC focus can never be unlocked.
159     return false;
160 }
161