]>
Commit | Line | Data |
---|---|---|
891d0563 DE |
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 | |
065e208e | 9 | // Licence: wxWidgets licence |
891d0563 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 DE |
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 | |
891d0563 DE |
18 | |
19 | #import <AppKit/NSView.h> | |
20 | #import <AppKit/NSAffineTransform.h> | |
9d180f3a DE |
21 | #import <AppKit/NSColor.h> |
22 | #import <AppKit/NSGraphicsContext.h> | |
23 | #import <AppKit/NSBezierPath.h> | |
fe8f7943 | 24 | #import <AppKit/NSWindow.h> |
891d0563 DE |
25 | |
26 | /* | |
27 | * wxWindowDC | |
28 | */ | |
29 | IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC) | |
30 | ||
31 | wxWindowDC::wxWindowDC(void) | |
fcc9de54 | 32 | : m_window(NULL) |
b66e9853 | 33 | , m_lockedNSView(NULL) |
891d0563 DE |
34 | { |
35 | }; | |
36 | ||
37 | wxWindowDC::wxWindowDC( wxWindow *window ) | |
fcc9de54 | 38 | : m_window(window) |
b66e9853 | 39 | , m_lockedNSView(NULL) |
891d0563 | 40 | { |
2b030203 | 41 | wxLogDebug(wxT("non-client window DC's are not supported, oh well")); |
891d0563 DE |
42 | }; |
43 | ||
44 | wxWindowDC::~wxWindowDC(void) | |
45 | { | |
b66e9853 | 46 | CocoaUnwindStackAndLoseFocus(); |
891d0563 DE |
47 | }; |
48 | ||
b66e9853 DE |
49 | bool wxWindowDC::CocoaLockFocusOnNSView(WX_NSView nsview) |
50 | { | |
51 | if([nsview lockFocusIfCanDraw]) | |
52 | { | |
53 | sm_cocoaDCStack.Insert(this); | |
b66e9853 DE |
54 | CocoaApplyTransformations(); |
55 | m_lockedNSView = nsview; | |
56 | return true; | |
57 | } | |
2b030203 | 58 | wxLogDebug(wxT("focus lock failed!")); |
b66e9853 DE |
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 | { | |
48580976 | 72 | wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView()); |
4db3c8ac | 73 | m_cocoaWxToBoundsTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height); |
b66e9853 DE |
74 | return CocoaLockFocusOnNSView(m_window->GetNonClientNSView()); |
75 | } | |
76 | ||
77 | bool wxWindowDC::CocoaUnlockFocus() | |
78 | { | |
48580976 | 79 | wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDC=%p, NSView=%p"),this, m_window->GetNonClientNSView()); |
b66e9853 DE |
80 | return CocoaUnlockFocusOnNSView(); |
81 | } | |
82 | ||
9d180f3a DE |
83 | void wxWindowDC::Clear() |
84 | { | |
b66e9853 | 85 | if(!CocoaTakeFocus()) return; |
9d180f3a DE |
86 | |
87 | NSGraphicsContext *context = [NSGraphicsContext currentContext]; | |
88 | [context saveGraphicsState]; | |
89 | ||
90 | [m_backgroundBrush.GetNSColor() set]; | |
b66e9853 | 91 | [NSBezierPath fillRect:[m_lockedNSView bounds]]; |
9d180f3a DE |
92 | |
93 | [context restoreGraphicsState]; | |
94 | } | |
95 | ||
891d0563 DE |
96 | /* |
97 | * wxClientDC | |
98 | */ | |
99 | IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC) | |
100 | ||
101 | wxClientDC::wxClientDC(void) | |
102 | { | |
103 | }; | |
104 | ||
105 | wxClientDC::wxClientDC( wxWindow *window ) | |
106 | { | |
d630f41d | 107 | m_window = window; |
891d0563 DE |
108 | }; |
109 | ||
110 | wxClientDC::~wxClientDC(void) | |
111 | { | |
fe8f7943 | 112 | CocoaUnwindStackAndLoseFocus(); |
891d0563 DE |
113 | }; |
114 | ||
fe8f7943 DE |
115 | bool wxClientDC::CocoaLockFocus() |
116 | { | |
48580976 | 117 | wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView()); |
4db3c8ac | 118 | m_cocoaWxToBoundsTransform = m_window->CocoaGetWxToBoundsTransform(); |
b66e9853 | 119 | return CocoaLockFocusOnNSView(m_window->GetNSView()); |
fe8f7943 DE |
120 | } |
121 | ||
122 | bool wxClientDC::CocoaUnlockFocus() | |
123 | { | |
48580976 | 124 | wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDC=%p, NSView=%p"),this, m_window->GetNSView()); |
b66e9853 | 125 | return CocoaUnlockFocusOnNSView(); |
fe8f7943 DE |
126 | } |
127 | ||
891d0563 DE |
128 | /* |
129 | * wxPaintDC | |
130 | */ | |
131 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC) | |
132 | ||
133 | wxPaintDC::wxPaintDC(void) | |
134 | { | |
135 | }; | |
136 | ||
137 | wxPaintDC::wxPaintDC( wxWindow *window ) | |
138 | { | |
d630f41d | 139 | m_window = window; |
2b030203 | 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")); |
fe8f7943 | 141 | sm_cocoaDCStack.Insert(this); |
b66e9853 | 142 | m_lockedNSView = window->GetNSView(); |
4db3c8ac | 143 | m_cocoaWxToBoundsTransform = window->CocoaGetWxToBoundsTransform(); |
fe8f7943 | 144 | CocoaApplyTransformations(); |
891d0563 DE |
145 | }; |
146 | ||
147 | wxPaintDC::~wxPaintDC(void) | |
148 | { | |
fe8f7943 | 149 | CocoaUnwindStackAndLoseFocus(); |
891d0563 DE |
150 | }; |
151 | ||
fe8f7943 DE |
152 | bool wxPaintDC::CocoaLockFocus() |
153 | { | |
2b030203 | 154 | wxFAIL_MSG(wxT("wxPaintDC cannot be asked to lock focus!")); |
fe8f7943 DE |
155 | return false; |
156 | } | |
157 | ||
158 | bool wxPaintDC::CocoaUnlockFocus() | |
159 | { | |
160 | // wxPaintDC focus can never be unlocked. | |
161 | return false; | |
162 | } | |
163 |