]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/carbon/renderer.cpp | |
3 | // Purpose: implementation of wxRendererNative for Mac | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 20.07.2003 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // License: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // for compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/string.h" | |
21 | #include "wx/dc.h" | |
22 | #include "wx/bitmap.h" | |
23 | #include "wx/settings.h" | |
24 | #include "wx/dcclient.h" | |
25 | #include "wx/toplevel.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/renderer.h" | |
29 | #include "wx/graphics.h" | |
30 | #include "wx/mac/uma.h" | |
31 | ||
32 | ||
33 | class WXDLLEXPORT wxRendererMac : public wxDelegateRendererNative | |
34 | { | |
35 | public: | |
36 | // draw the header control button (used by wxListCtrl) | |
37 | virtual void DrawHeaderButton( wxWindow *win, | |
38 | wxDC& dc, | |
39 | const wxRect& rect, | |
40 | int flags = 0, | |
41 | wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE, | |
42 | wxHeaderButtonParams* params = NULL ); | |
43 | ||
44 | virtual int GetHeaderButtonHeight(wxWindow *win); | |
45 | ||
46 | // draw the expanded/collapsed icon for a tree control item | |
47 | virtual void DrawTreeItemButton( wxWindow *win, | |
48 | wxDC& dc, | |
49 | const wxRect& rect, | |
50 | int flags = 0 ); | |
51 | ||
52 | // draw a (vertical) sash | |
53 | virtual void DrawSplitterSash( wxWindow *win, | |
54 | wxDC& dc, | |
55 | const wxSize& size, | |
56 | wxCoord position, | |
57 | wxOrientation orient, | |
58 | int flags = 0 ); | |
59 | ||
60 | private: | |
61 | // the tree buttons | |
62 | wxBitmap m_bmpTreeExpanded; | |
63 | wxBitmap m_bmpTreeCollapsed; | |
64 | }; | |
65 | ||
66 | // ============================================================================ | |
67 | // implementation | |
68 | // ============================================================================ | |
69 | ||
70 | // static | |
71 | wxRendererNative& wxRendererNative::GetDefault() | |
72 | { | |
73 | static wxRendererMac s_rendererMac; | |
74 | ||
75 | return s_rendererMac; | |
76 | } | |
77 | ||
78 | void wxRendererMac::DrawHeaderButton( wxWindow *win, | |
79 | wxDC& dc, | |
80 | const wxRect& rect, | |
81 | int flags, | |
82 | wxHeaderSortIconType sortArrow, | |
83 | wxHeaderButtonParams* params ) | |
84 | { | |
85 | #if !wxMAC_USE_CORE_GRAPHICS | |
86 | const wxCoord x = dc.LogicalToDeviceX(rect.x); | |
87 | const wxCoord y = dc.LogicalToDeviceY(rect.y); | |
88 | const wxCoord w = dc.LogicalToDeviceXRel(rect.width); | |
89 | const wxCoord h = dc.LogicalToDeviceYRel(rect.height); | |
90 | #else | |
91 | // now the wxGCDC is using native transformations | |
92 | const wxCoord x = rect.x; | |
93 | const wxCoord y = rect.y; | |
94 | const wxCoord w = rect.width; | |
95 | const wxCoord h = rect.height; | |
96 | #endif | |
97 | ||
98 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
99 | ||
100 | HIRect headerRect = CGRectMake( x, y, w, h ); | |
101 | if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) ) | |
102 | { | |
103 | Rect r = | |
104 | { | |
105 | (short) headerRect.origin.y, (short) headerRect.origin.x, | |
106 | (short) (headerRect.origin.y + headerRect.size.height), | |
107 | (short) (headerRect.origin.x + headerRect.size.width) | |
108 | }; | |
109 | ||
110 | RgnHandle updateRgn = NewRgn(); | |
111 | RectRgn( updateRgn, &r ); | |
112 | HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true ); | |
113 | DisposeRgn( updateRgn ); | |
114 | } | |
115 | else | |
116 | { | |
117 | CGContextRef cgContext; | |
118 | ||
119 | #if wxMAC_USE_CORE_GRAPHICS | |
120 | cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext(); | |
121 | #else | |
122 | Rect bounds; | |
123 | ||
124 | GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds ); | |
125 | QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
126 | ||
127 | CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top ); | |
128 | CGContextScaleCTM( cgContext, 1, -1 ); | |
129 | ||
130 | HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle) dc.m_macCurrentClipRgn ), cgContext ); | |
131 | CGContextClip( cgContext ); | |
132 | HIViewConvertRect( &headerRect, (HIViewRef) win->GetHandle(), (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() ); | |
133 | #endif | |
134 | ||
135 | { | |
136 | HIThemeButtonDrawInfo drawInfo; | |
137 | HIRect labelRect; | |
138 | ||
139 | memset( &drawInfo, 0, sizeof(drawInfo) ); | |
140 | drawInfo.version = 0; | |
141 | drawInfo.kind = kThemeListHeaderButton; | |
142 | drawInfo.state = (flags & wxCONTROL_DISABLED) ? kThemeStateInactive : kThemeStateActive; | |
143 | drawInfo.value = (flags & wxCONTROL_SELECTED) ? kThemeButtonOn : kThemeButtonOff; | |
144 | drawInfo.adornment = kThemeAdornmentNone; | |
145 | ||
146 | // The down arrow is drawn automatically, change it to an up arrow if needed. | |
147 | if ( sortArrow == wxHDR_SORT_ICON_UP ) | |
148 | drawInfo.adornment = kThemeAdornmentHeaderButtonSortUp; | |
149 | ||
150 | HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect ); | |
151 | ||
152 | // If we don't want any arrows we need to draw over the one already there | |
153 | if ( (flags & wxCONTROL_SELECTED) && (sortArrow == wxHDR_SORT_ICON_NONE) ) | |
154 | { | |
155 | // clip to the header rectangle | |
156 | CGContextSaveGState( cgContext ); | |
157 | CGContextClipToRect( cgContext, headerRect ); | |
158 | // but draw bigger than that so the arrow will get clipped off | |
159 | headerRect.size.width += 25; | |
160 | HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect ); | |
161 | CGContextRestoreGState( cgContext ); | |
162 | } | |
163 | } | |
164 | ||
165 | #if wxMAC_USE_CORE_GRAPHICS | |
166 | #else | |
167 | QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
168 | #endif | |
169 | } | |
170 | ||
171 | // Reserve room for the arrows before writing the label, and turn off the | |
172 | // flags we've already handled | |
173 | wxRect newRect(rect); | |
174 | if ( (flags & wxCONTROL_SELECTED) && (sortArrow != wxHDR_SORT_ICON_NONE) ) | |
175 | { | |
176 | newRect.width -= 12; | |
177 | sortArrow = wxHDR_SORT_ICON_NONE; | |
178 | } | |
179 | flags &= ~wxCONTROL_SELECTED; | |
180 | ||
181 | DrawHeaderButtonContents(win, dc, newRect, flags, sortArrow, params); | |
182 | } | |
183 | ||
184 | ||
185 | int wxRendererMac::GetHeaderButtonHeight(wxWindow* WXUNUSED(win)) | |
186 | { | |
187 | SInt32 standardHeight; | |
188 | OSStatus errStatus; | |
189 | ||
190 | errStatus = GetThemeMetric( kThemeMetricListHeaderHeight, &standardHeight ); | |
191 | if (errStatus == noErr) | |
192 | { | |
193 | return standardHeight; | |
194 | } | |
195 | return -1; | |
196 | } | |
197 | ||
198 | void wxRendererMac::DrawTreeItemButton( wxWindow *win, | |
199 | wxDC& dc, | |
200 | const wxRect& rect, | |
201 | int flags ) | |
202 | { | |
203 | #if !wxMAC_USE_CORE_GRAPHICS | |
204 | const wxCoord x = dc.LogicalToDeviceX(rect.x); | |
205 | const wxCoord y = dc.LogicalToDeviceY(rect.y); | |
206 | const wxCoord w = dc.LogicalToDeviceXRel(rect.width); | |
207 | const wxCoord h = dc.LogicalToDeviceYRel(rect.height); | |
208 | #else | |
209 | // now the wxGCDC is using native transformations | |
210 | const wxCoord x = rect.x; | |
211 | const wxCoord y = rect.y; | |
212 | const wxCoord w = rect.width; | |
213 | const wxCoord h = rect.height; | |
214 | #endif | |
215 | ||
216 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
217 | ||
218 | HIRect headerRect = CGRectMake( x, y, w, h ); | |
219 | if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) ) | |
220 | { | |
221 | Rect r = | |
222 | { | |
223 | (short) headerRect.origin.y, (short) headerRect.origin.x, | |
224 | (short) (headerRect.origin.y + headerRect.size.height), | |
225 | (short) (headerRect.origin.x + headerRect.size.width) | |
226 | }; | |
227 | ||
228 | RgnHandle updateRgn = NewRgn(); | |
229 | RectRgn( updateRgn, &r ); | |
230 | HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true ); | |
231 | DisposeRgn( updateRgn ); | |
232 | } | |
233 | else | |
234 | { | |
235 | CGContextRef cgContext; | |
236 | ||
237 | #if wxMAC_USE_CORE_GRAPHICS | |
238 | cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext(); | |
239 | #else | |
240 | Rect bounds; | |
241 | ||
242 | GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds ); | |
243 | QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
244 | ||
245 | CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top ); | |
246 | CGContextScaleCTM( cgContext, 1, -1 ); | |
247 | ||
248 | HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle) dc.m_macCurrentClipRgn ), cgContext ); | |
249 | CGContextClip( cgContext ); | |
250 | HIViewConvertRect( &headerRect, (HIViewRef) win->GetHandle(), (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() ); | |
251 | #endif | |
252 | ||
253 | { | |
254 | HIThemeButtonDrawInfo drawInfo; | |
255 | HIRect labelRect; | |
256 | ||
257 | memset( &drawInfo, 0, sizeof(drawInfo) ); | |
258 | drawInfo.version = 0; | |
259 | drawInfo.kind = kThemeDisclosureButton; | |
260 | drawInfo.state = (flags & wxCONTROL_DISABLED) ? kThemeStateInactive : kThemeStateActive; | |
261 | // Apple mailing list posts say to use the arrow adornment constants, but those don't work. | |
262 | // We need to set the value using the 'old' DrawThemeButton constants instead. | |
263 | drawInfo.value = (flags & wxCONTROL_EXPANDED) ? kThemeDisclosureDown : kThemeDisclosureRight; | |
264 | drawInfo.adornment = kThemeAdornmentNone; | |
265 | ||
266 | HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect ); | |
267 | ||
268 | } | |
269 | ||
270 | #if wxMAC_USE_CORE_GRAPHICS | |
271 | #else | |
272 | QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
273 | #endif | |
274 | } | |
275 | } | |
276 | ||
277 | void wxRendererMac::DrawSplitterSash( wxWindow *win, | |
278 | wxDC& dc, | |
279 | const wxSize& size, | |
280 | wxCoord position, | |
281 | wxOrientation orient, | |
282 | int WXUNUSED(flags) ) | |
283 | { | |
284 | bool hasMetal = win->MacGetTopLevelWindow()->MacGetMetalAppearance(); | |
285 | SInt32 height; | |
286 | GetThemeMetric( kThemeMetricSmallPaneSplitterHeight, &height ); | |
287 | HIRect splitterRect; | |
288 | if (orient == wxVERTICAL) | |
289 | splitterRect = CGRectMake( position, 0, height, size.y ); | |
290 | else | |
291 | splitterRect = CGRectMake( 0, position, size.x, height ); | |
292 | ||
293 | #if !wxMAC_USE_CORE_GRAPHICS | |
294 | HIViewConvertRect( | |
295 | &splitterRect, | |
296 | (HIViewRef) win->GetHandle(), | |
297 | (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() ); | |
298 | #endif | |
299 | ||
300 | // under compositing we should only draw when called by the OS, otherwise just issue a redraw command | |
301 | // strange redraw errors occur if we don't do this | |
302 | ||
303 | if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) ) | |
304 | { | |
305 | Rect r = | |
306 | { | |
307 | (short) splitterRect.origin.y, | |
308 | (short) splitterRect.origin.x, | |
309 | (short) (splitterRect.origin.y + splitterRect.size.height), | |
310 | (short) (splitterRect.origin.x + splitterRect.size.width) | |
311 | }; | |
312 | ||
313 | RgnHandle updateRgn = NewRgn(); | |
314 | RectRgn( updateRgn, &r ); | |
315 | HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true ); | |
316 | DisposeRgn( updateRgn ); | |
317 | } | |
318 | else | |
319 | { | |
320 | CGContextRef cgContext; | |
321 | ||
322 | #if wxMAC_USE_CORE_GRAPHICS | |
323 | cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext(); | |
324 | #else | |
325 | Rect bounds; | |
326 | GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds ); | |
327 | QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
328 | CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top ); | |
329 | CGContextScaleCTM( cgContext, 1, -1 ); | |
330 | #endif | |
331 | ||
332 | HIThemeSplitterDrawInfo drawInfo; | |
333 | drawInfo.version = 0; | |
334 | drawInfo.state = kThemeStateActive; | |
335 | drawInfo.adornment = hasMetal ? kHIThemeSplitterAdornmentMetal : kHIThemeSplitterAdornmentNone; | |
336 | HIThemeDrawPaneSplitter( &splitterRect, &drawInfo, cgContext, kHIThemeOrientationNormal ); | |
337 | ||
338 | #if wxMAC_USE_CORE_GRAPHICS | |
339 | #else | |
340 | QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
341 | #endif | |
342 | } | |
343 | } |