]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/renderer.cpp
aac95cbb8331afb17445dd20afa4115de4b63643
[wxWidgets.git] / src / mac / carbon / renderer.cpp
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 const wxCoord x = dc.LogicalToDeviceX(rect.x /*- 1*/);
86 const wxCoord y = dc.LogicalToDeviceY(rect.y /*- 1*/);
87 const wxCoord w = dc.LogicalToDeviceXRel(rect.width);
88 const wxCoord h = dc.LogicalToDeviceYRel(rect.height);
89
90 dc.SetBrush( *wxTRANSPARENT_BRUSH );
91
92 HIRect headerRect = CGRectMake( x, y, w, h );
93 if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) )
94 {
95 Rect r =
96 {
97 (short) headerRect.origin.y, (short) headerRect.origin.x,
98 (short) (headerRect.origin.y + headerRect.size.height),
99 (short) (headerRect.origin.x + headerRect.size.width)
100 };
101
102 RgnHandle updateRgn = NewRgn();
103 RectRgn( updateRgn, &r );
104 HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true );
105 DisposeRgn( updateRgn );
106 }
107 else
108 {
109 CGContextRef cgContext;
110
111 #if wxMAC_USE_CORE_GRAPHICS
112 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
113 #else
114 Rect bounds;
115
116 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
117 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
118
119 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
120 CGContextScaleCTM( cgContext, 1, -1 );
121
122 HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle) dc.m_macCurrentClipRgn ), cgContext );
123 CGContextClip( cgContext );
124 HIViewConvertRect( &headerRect, (HIViewRef) win->GetHandle(), (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() );
125 #endif
126
127 {
128 HIThemeButtonDrawInfo drawInfo;
129 HIRect labelRect;
130
131 memset( &drawInfo, 0, sizeof(drawInfo) );
132 drawInfo.version = 0;
133 drawInfo.kind = kThemeListHeaderButton;
134 drawInfo.state = (flags & wxCONTROL_DISABLED) ? kThemeStateInactive : kThemeStateActive;
135 drawInfo.value = (flags & wxCONTROL_SELECTED) ? kThemeButtonOn : kThemeButtonOff;
136 drawInfo.adornment = kThemeAdornmentNone;
137
138 // The down arrow is drawn automatically, change it to an up arrow if needed.
139 if ( sortArrow == wxHDR_SORT_ICON_UP )
140 drawInfo.adornment = kThemeAdornmentHeaderButtonSortUp;
141
142 HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect );
143
144 // If we don't want any arrows we need to draw over the one already there
145 if ( (flags & wxCONTROL_SELECTED) && (sortArrow == wxHDR_SORT_ICON_NONE) )
146 {
147 // clip to the header rectangle
148 CGContextSaveGState( cgContext );
149 CGContextClipToRect( cgContext, headerRect );
150 // but draw bigger than that so the arrow will get clipped off
151 headerRect.size.width += 25;
152 HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect );
153 CGContextRestoreGState( cgContext );
154 }
155 }
156
157 #if wxMAC_USE_CORE_GRAPHICS
158 #else
159 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
160 #endif
161 }
162
163 // Reserve room for the arrows before writing the label, and turn off the
164 // flags we've already handled
165 wxRect newRect(rect);
166 if ( (flags & wxCONTROL_SELECTED) && (sortArrow != wxHDR_SORT_ICON_NONE) )
167 {
168 newRect.width -= 12;
169 sortArrow = wxHDR_SORT_ICON_NONE;
170 }
171 flags &= ~wxCONTROL_SELECTED;
172
173 DrawHeaderButtonContents(win, dc, newRect, flags, sortArrow, params);
174 }
175
176
177 int wxRendererMac::GetHeaderButtonHeight(wxWindow* WXUNUSED(win))
178 {
179 SInt32 standardHeight;
180 OSStatus errStatus;
181
182 errStatus = GetThemeMetric( kThemeMetricListHeaderHeight, &standardHeight );
183 if (errStatus == noErr)
184 {
185 return standardHeight;
186 }
187 return -1;
188 }
189
190 void wxRendererMac::DrawTreeItemButton( wxWindow *win,
191 wxDC& dc,
192 const wxRect& rect,
193 int flags )
194 {
195 #if 0 // The rect has already been adjusted, if that ever changes then uncomment this code.
196 const wxCoord x = dc.LogicalToDeviceX(rect.x /*- 1*/);
197 const wxCoord y = dc.LogicalToDeviceY(rect.y /*- 1*/);
198 const wxCoord w = dc.LogicalToDeviceXRel(rect.width);
199 const wxCoord h = dc.LogicalToDeviceYRel(rect.height);
200 #else
201 const wxCoord x = rect.x;
202 const wxCoord y = rect.y;
203 const wxCoord w = rect.width;
204 const wxCoord h = rect.height;
205 #endif
206
207 dc.SetBrush( *wxTRANSPARENT_BRUSH );
208
209 HIRect headerRect = CGRectMake( x, y, w, h );
210 if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) )
211 {
212 Rect r =
213 {
214 (short) headerRect.origin.y, (short) headerRect.origin.x,
215 (short) (headerRect.origin.y + headerRect.size.height),
216 (short) (headerRect.origin.x + headerRect.size.width)
217 };
218
219 RgnHandle updateRgn = NewRgn();
220 RectRgn( updateRgn, &r );
221 HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true );
222 DisposeRgn( updateRgn );
223 }
224 else
225 {
226 CGContextRef cgContext;
227
228 #if wxMAC_USE_CORE_GRAPHICS
229 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
230 #else
231 Rect bounds;
232
233 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
234 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
235
236 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
237 CGContextScaleCTM( cgContext, 1, -1 );
238
239 HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle) dc.m_macCurrentClipRgn ), cgContext );
240 CGContextClip( cgContext );
241 HIViewConvertRect( &headerRect, (HIViewRef) win->GetHandle(), (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() );
242 #endif
243
244 {
245 HIThemeButtonDrawInfo drawInfo;
246 HIRect labelRect;
247
248 memset( &drawInfo, 0, sizeof(drawInfo) );
249 drawInfo.version = 0;
250 drawInfo.kind = kThemeDisclosureButton;
251 drawInfo.state = (flags & wxCONTROL_DISABLED) ? kThemeStateInactive : kThemeStateActive;
252 // Apple mailing list posts say to use the arrow adornment constants, but those don't work.
253 // We need to set the value using the 'old' DrawThemeButton constants instead.
254 drawInfo.value = (flags & wxCONTROL_EXPANDED) ? kThemeDisclosureDown : kThemeDisclosureRight;
255 drawInfo.adornment = kThemeAdornmentNone;
256
257 HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect );
258
259 }
260
261 #if wxMAC_USE_CORE_GRAPHICS
262 #else
263 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
264 #endif
265 }
266 }
267
268 void wxRendererMac::DrawSplitterSash( wxWindow *win,
269 wxDC& dc,
270 const wxSize& size,
271 wxCoord position,
272 wxOrientation orient,
273 int WXUNUSED(flags) )
274 {
275 bool hasMetal = win->MacGetTopLevelWindow()->MacGetMetalAppearance();
276 SInt32 height;
277 GetThemeMetric( kThemeMetricSmallPaneSplitterHeight, &height );
278 HIRect splitterRect;
279 if (orient == wxVERTICAL)
280 splitterRect = CGRectMake( position, 0, height, size.y );
281 else
282 splitterRect = CGRectMake( 0, position, size.x, height );
283
284 #if !wxMAC_USE_CORE_GRAPHICS
285 HIViewConvertRect(
286 &splitterRect,
287 (HIViewRef) win->GetHandle(),
288 (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() );
289 #endif
290
291 // under compositing we should only draw when called by the OS, otherwise just issue a redraw command
292 // strange redraw errors occur if we don't do this
293
294 if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) )
295 {
296 Rect r =
297 {
298 (short) splitterRect.origin.y,
299 (short) splitterRect.origin.x,
300 (short) (splitterRect.origin.y + splitterRect.size.height),
301 (short) (splitterRect.origin.x + splitterRect.size.width)
302 };
303
304 RgnHandle updateRgn = NewRgn();
305 RectRgn( updateRgn, &r );
306 HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true );
307 DisposeRgn( updateRgn );
308 }
309 else
310 {
311 CGContextRef cgContext;
312
313 #if wxMAC_USE_CORE_GRAPHICS
314 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
315 #else
316 Rect bounds;
317 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
318 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
319 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
320 CGContextScaleCTM( cgContext, 1, -1 );
321 #endif
322
323 HIThemeSplitterDrawInfo drawInfo;
324 drawInfo.version = 0;
325 drawInfo.state = kThemeStateActive;
326 drawInfo.adornment = hasMetal ? kHIThemeSplitterAdornmentMetal : kHIThemeSplitterAdornmentNone;
327 HIThemeDrawPaneSplitter( &splitterRect, &drawInfo, cgContext, kHIThemeOrientationNormal );
328
329 #if wxMAC_USE_CORE_GRAPHICS
330 #else
331 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
332 #endif
333 }
334 }