]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dcclient.mm
Make mouse capture checking asserts stronger and more detailed.
[wxWidgets.git] / src / cocoa / dcclient.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dcclient.mm
3 // Purpose: wxWindowDCImpl, wxPaintDCImpl, and wxClientDCImpl classes
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/04/01
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12 #ifndef WX_PRECOMP
13 #include "wx/log.h"
14 #include "wx/window.h"
15 #endif //WX_PRECOMP
16
17 #include "wx/cocoa/dcclient.h"
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 * wxWindowDCImpl
28 */
29 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxCocoaDCImpl)
30
31 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner)
32 : wxCocoaDCImpl(owner)
33 , m_window(NULL)
34 , m_lockedNSView(NULL)
35 {
36 };
37
38 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner, wxWindow *window)
39 : wxCocoaDCImpl(owner)
40 , m_window(window)
41 , m_lockedNSView(NULL)
42 {
43 wxLogDebug(wxT("non-client window DC's are not supported, oh well"));
44 };
45
46 wxWindowDCImpl::~wxWindowDCImpl(void)
47 {
48 CocoaUnwindStackAndLoseFocus();
49 };
50
51 bool wxWindowDCImpl::CocoaLockFocusOnNSView(WX_NSView nsview)
52 {
53 if([nsview lockFocusIfCanDraw])
54 {
55 sm_cocoaDCStack.Insert(this);
56 CocoaApplyTransformations();
57 m_lockedNSView = nsview;
58 return true;
59 }
60 wxLogDebug(wxT("focus lock failed!"));
61 return false;
62 }
63
64 bool wxWindowDCImpl::CocoaUnlockFocusOnNSView()
65 {
66 [[m_lockedNSView window] flushWindow];
67 [m_lockedNSView unlockFocus];
68 m_lockedNSView = NULL;
69 return true;
70 }
71
72 bool wxWindowDCImpl::CocoaLockFocus()
73 {
74 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView());
75 NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height);
76 [newTransform retain];
77 [m_cocoaWxToBoundsTransform release];
78 m_cocoaWxToBoundsTransform = newTransform;
79 return CocoaLockFocusOnNSView(m_window->GetNonClientNSView());
80 }
81
82 bool wxWindowDCImpl::CocoaUnlockFocus()
83 {
84 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView());
85 return CocoaUnlockFocusOnNSView();
86 }
87
88 bool wxWindowDCImpl::CocoaGetBounds(void *rectData)
89 {
90 if(!rectData)
91 return false;
92 if(!m_lockedNSView)
93 return false;
94 NSRect *pRect = (NSRect*)rectData;
95 *pRect = [m_lockedNSView bounds];
96 return true;
97 }
98
99 /*
100 * wxClientDCImpl
101 */
102 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
103
104 wxClientDCImpl::wxClientDCImpl(wxDC *owner)
105 : wxWindowDCImpl(owner)
106 {
107 };
108
109 wxClientDCImpl::wxClientDCImpl(wxDC *owner, wxWindow *window)
110 : wxWindowDCImpl(owner)
111 {
112 m_window = window;
113 };
114
115 wxClientDCImpl::~wxClientDCImpl(void)
116 {
117 CocoaUnwindStackAndLoseFocus();
118 };
119
120 bool wxClientDCImpl::CocoaLockFocus()
121 {
122 wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView());
123 NSAffineTransform *newTransform = m_window->CocoaGetWxToBoundsTransform();
124 [newTransform retain];
125 [m_cocoaWxToBoundsTransform release];
126 m_cocoaWxToBoundsTransform = newTransform;
127 return CocoaLockFocusOnNSView(m_window->GetNSView());
128 }
129
130 bool wxClientDCImpl::CocoaUnlockFocus()
131 {
132 wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView());
133 return CocoaUnlockFocusOnNSView();
134 }
135
136 /*
137 * wxPaintDCImpl
138 */
139 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
140
141 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner)
142 : wxWindowDCImpl(owner)
143 {
144 };
145
146 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner, wxWindow *window)
147 : wxWindowDCImpl(owner)
148 {
149 m_window = window;
150 wxASSERT_MSG([NSView focusView]==window->GetNSView(), wxT("PaintDC's NSView does not have focus. Please use wxPaintDCImpl only as the first DC created in a paint handler"));
151 sm_cocoaDCStack.Insert(this);
152 m_lockedNSView = window->GetNSView();
153 NSAffineTransform *newTransform = window->CocoaGetWxToBoundsTransform();
154 [newTransform retain];
155 [m_cocoaWxToBoundsTransform release];
156 m_cocoaWxToBoundsTransform = newTransform;
157 CocoaApplyTransformations();
158 };
159
160 wxPaintDCImpl::~wxPaintDCImpl(void)
161 {
162 CocoaUnwindStackAndLoseFocus();
163 };
164
165 bool wxPaintDCImpl::CocoaLockFocus()
166 {
167 wxFAIL_MSG(wxT("wxPaintDCImpl cannot be asked to lock focus!"));
168 return false;
169 }
170
171 bool wxPaintDCImpl::CocoaUnlockFocus()
172 {
173 // wxPaintDCImpl focus can never be unlocked.
174 return false;
175 }
176