1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/renderer.cpp
3 // Purpose: implementation of wxRendererNative for Mac
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
20 #include "wx/string.h"
22 #include "wx/bitmap.h"
23 #include "wx/settings.h"
24 #include "wx/dcclient.h"
25 #include "wx/toplevel.h"
28 #include "wx/renderer.h"
29 #include "wx/graphics.h"
30 #include "wx/mac/uma.h"
33 class WXDLLEXPORT wxRendererMac
: public wxDelegateRendererNative
36 // draw the header control button (used by wxListCtrl)
37 virtual void DrawHeaderButton( wxWindow
*win
,
41 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
42 wxHeaderButtonParams
* params
= NULL
);
44 virtual int GetHeaderButtonHeight(wxWindow
*win
);
46 // draw the expanded/collapsed icon for a tree control item
47 virtual void DrawTreeItemButton( wxWindow
*win
,
52 // draw a (vertical) sash
53 virtual void DrawSplitterSash( wxWindow
*win
,
62 wxBitmap m_bmpTreeExpanded
;
63 wxBitmap m_bmpTreeCollapsed
;
66 // ============================================================================
68 // ============================================================================
71 wxRendererNative
& wxRendererNative::GetDefault()
73 static wxRendererMac s_rendererMac
;
78 void wxRendererMac::DrawHeaderButton( wxWindow
*win
,
82 wxHeaderSortIconType sortArrow
,
83 wxHeaderButtonParams
* params
)
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
);
90 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
92 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
93 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
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
)
102 RgnHandle updateRgn
= NewRgn();
103 RectRgn( updateRgn
, &r
);
104 HIViewSetNeedsDisplayInRegion( (HIViewRef
) win
->GetHandle(), updateRgn
, true );
105 DisposeRgn( updateRgn
);
109 CGContextRef cgContext
;
111 #if wxMAC_USE_CORE_GRAPHICS
112 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
116 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
117 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
119 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
120 CGContextScaleCTM( cgContext
, 1, -1 );
122 HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle
) dc
.m_macCurrentClipRgn
), cgContext
);
123 CGContextClip( cgContext
);
124 HIViewConvertRect( &headerRect
, (HIViewRef
) win
->GetHandle(), (HIViewRef
) win
->MacGetTopLevelWindow()->GetHandle() );
128 HIThemeButtonDrawInfo drawInfo
;
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
;
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
;
142 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
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
) )
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
);
157 #if wxMAC_USE_CORE_GRAPHICS
159 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
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
) )
169 sortArrow
= wxHDR_SORT_ICON_NONE
;
171 flags
&= ~wxCONTROL_SELECTED
;
173 DrawHeaderButtonContents(win
, dc
, newRect
, flags
, sortArrow
, params
);
177 int wxRendererMac::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
179 SInt32 standardHeight
;
182 errStatus
= GetThemeMetric( kThemeMetricListHeaderHeight
, &standardHeight
);
183 if (errStatus
== noErr
)
185 return standardHeight
;
190 void wxRendererMac::DrawTreeItemButton( wxWindow
*win
,
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
);
201 const wxCoord x
= rect
.x
;
202 const wxCoord y
= rect
.y
;
203 const wxCoord w
= rect
.width
;
204 const wxCoord h
= rect
.height
;
207 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
209 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
210 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
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
)
219 RgnHandle updateRgn
= NewRgn();
220 RectRgn( updateRgn
, &r
);
221 HIViewSetNeedsDisplayInRegion( (HIViewRef
) win
->GetHandle(), updateRgn
, true );
222 DisposeRgn( updateRgn
);
226 CGContextRef cgContext
;
228 #if wxMAC_USE_CORE_GRAPHICS
229 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
233 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
234 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
236 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
237 CGContextScaleCTM( cgContext
, 1, -1 );
239 HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle
) dc
.m_macCurrentClipRgn
), cgContext
);
240 CGContextClip( cgContext
);
241 HIViewConvertRect( &headerRect
, (HIViewRef
) win
->GetHandle(), (HIViewRef
) win
->MacGetTopLevelWindow()->GetHandle() );
245 HIThemeButtonDrawInfo drawInfo
;
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
;
257 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
261 #if wxMAC_USE_CORE_GRAPHICS
263 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
268 void wxRendererMac::DrawSplitterSash( wxWindow
*win
,
272 wxOrientation orient
,
273 int WXUNUSED(flags
) )
275 bool hasMetal
= win
->MacGetTopLevelWindow()->MacGetMetalAppearance();
277 GetThemeMetric( kThemeMetricSmallPaneSplitterHeight
, &height
);
279 if (orient
== wxVERTICAL
)
280 splitterRect
= CGRectMake( position
, 0, height
, size
.y
);
282 splitterRect
= CGRectMake( 0, position
, size
.x
, height
);
284 #if !wxMAC_USE_CORE_GRAPHICS
287 (HIViewRef
) win
->GetHandle(),
288 (HIViewRef
) win
->MacGetTopLevelWindow()->GetHandle() );
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
294 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
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
)
304 RgnHandle updateRgn
= NewRgn();
305 RectRgn( updateRgn
, &r
);
306 HIViewSetNeedsDisplayInRegion( (HIViewRef
) win
->GetHandle(), updateRgn
, true );
307 DisposeRgn( updateRgn
);
311 CGContextRef cgContext
;
313 #if wxMAC_USE_CORE_GRAPHICS
314 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
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 );
323 HIThemeSplitterDrawInfo drawInfo
;
324 drawInfo
.version
= 0;
325 drawInfo
.state
= kThemeStateActive
;
326 drawInfo
.adornment
= hasMetal
? kHIThemeSplitterAdornmentMetal
: kHIThemeSplitterAdornmentNone
;
327 HIThemeDrawPaneSplitter( &splitterRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
329 #if wxMAC_USE_CORE_GRAPHICS
331 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);