]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/renderer.cpp
Don't use bitmaps to draw tree item buttons, use the HITheme drawing functions instea...
[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 const wxCoord x = dc.LogicalToDeviceX(rect.x /*- 1*/);
196 const wxCoord y = dc.LogicalToDeviceY(rect.y /*- 1*/);
197 const wxCoord w = dc.LogicalToDeviceXRel(rect.width);
198 const wxCoord h = dc.LogicalToDeviceYRel(rect.height);
199
200 dc.SetBrush( *wxTRANSPARENT_BRUSH );
201
202 HIRect headerRect = CGRectMake( x, y, w, h );
203 if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) )
204 {
205 Rect r =
206 {
207 (short) headerRect.origin.y, (short) headerRect.origin.x,
208 (short) (headerRect.origin.y + headerRect.size.height),
209 (short) (headerRect.origin.x + headerRect.size.width)
210 };
211
212 RgnHandle updateRgn = NewRgn();
213 RectRgn( updateRgn, &r );
214 HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true );
215 DisposeRgn( updateRgn );
216 }
217 else
218 {
219 CGContextRef cgContext;
220
221 #if wxMAC_USE_CORE_GRAPHICS
222 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
223 #else
224 Rect bounds;
225
226 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
227 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
228
229 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
230 CGContextScaleCTM( cgContext, 1, -1 );
231
232 HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle) dc.m_macCurrentClipRgn ), cgContext );
233 CGContextClip( cgContext );
234 HIViewConvertRect( &headerRect, (HIViewRef) win->GetHandle(), (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() );
235 #endif
236
237 {
238 HIThemeButtonDrawInfo drawInfo;
239 HIRect labelRect;
240
241 memset( &drawInfo, 0, sizeof(drawInfo) );
242 drawInfo.version = 0;
243 drawInfo.kind = kThemeDisclosureButton;
244 drawInfo.state = (flags & wxCONTROL_DISABLED) ? kThemeStateInactive : kThemeStateActive;
245 // Apple mailing list posts say to use the arrow adornment constants, but those don't work.
246 // We need to set the value using the 'old' DrawThemeButton constants instead.
247 drawInfo.value = (flags & wxCONTROL_EXPANDED) ? kThemeDisclosureDown : kThemeDisclosureRight;
248 drawInfo.adornment = kThemeAdornmentNone;
249
250 HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect );
251
252 }
253
254 #if wxMAC_USE_CORE_GRAPHICS
255 #else
256 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
257 #endif
258 }
259 }
260
261 void wxRendererMac::DrawSplitterSash( wxWindow *win,
262 wxDC& dc,
263 const wxSize& size,
264 wxCoord position,
265 wxOrientation orient,
266 int WXUNUSED(flags) )
267 {
268 bool hasMetal = win->MacGetTopLevelWindow()->MacGetMetalAppearance();
269 SInt32 height;
270 GetThemeMetric( kThemeMetricSmallPaneSplitterHeight, &height );
271 HIRect splitterRect;
272 if (orient == wxVERTICAL)
273 splitterRect = CGRectMake( position, 0, height, size.y );
274 else
275 splitterRect = CGRectMake( 0, position, size.x, height );
276
277 #if !wxMAC_USE_CORE_GRAPHICS
278 HIViewConvertRect(
279 &splitterRect,
280 (HIViewRef) win->GetHandle(),
281 (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() );
282 #endif
283
284 // under compositing we should only draw when called by the OS, otherwise just issue a redraw command
285 // strange redraw errors occur if we don't do this
286
287 if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) )
288 {
289 Rect r =
290 {
291 (short) splitterRect.origin.y,
292 (short) splitterRect.origin.x,
293 (short) (splitterRect.origin.y + splitterRect.size.height),
294 (short) (splitterRect.origin.x + splitterRect.size.width)
295 };
296
297 RgnHandle updateRgn = NewRgn();
298 RectRgn( updateRgn, &r );
299 HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle(), updateRgn, true );
300 DisposeRgn( updateRgn );
301 }
302 else
303 {
304 CGContextRef cgContext;
305
306 #if wxMAC_USE_CORE_GRAPHICS
307 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
308 #else
309 Rect bounds;
310 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
311 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
312 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
313 CGContextScaleCTM( cgContext, 1, -1 );
314 #endif
315
316 HIThemeSplitterDrawInfo drawInfo;
317 drawInfo.version = 0;
318 drawInfo.state = kThemeStateActive;
319 drawInfo.adornment = hasMetal ? kHIThemeSplitterAdornmentMetal : kHIThemeSplitterAdornmentNone;
320 HIThemeDrawPaneSplitter( &splitterRect, &drawInfo, cgContext, kHIThemeOrientationNormal );
321
322 #if wxMAC_USE_CORE_GRAPHICS
323 #else
324 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
325 #endif
326 }
327 }