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 int 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
,
60 virtual void DrawCheckBox(wxWindow
*win
,
65 virtual void DrawComboBoxDropButton(wxWindow
*win
,
70 virtual void DrawPushButton(wxWindow
*win
,
75 virtual void DrawItemSelectionRect(wxWindow
*win
,
80 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
83 void DrawMacThemeButton(wxWindow
*win
,
91 wxBitmap m_bmpTreeExpanded
;
92 wxBitmap m_bmpTreeCollapsed
;
95 // ============================================================================
97 // ============================================================================
100 wxRendererNative
& wxRendererNative::GetDefault()
102 static wxRendererMac s_rendererMac
;
104 return s_rendererMac
;
107 int wxRendererMac::DrawHeaderButton( wxWindow
*win
,
111 wxHeaderSortIconType sortArrow
,
112 wxHeaderButtonParams
* params
)
114 const wxCoord x
= rect
.x
;
115 const wxCoord y
= rect
.y
;
116 const wxCoord w
= rect
.width
;
117 const wxCoord h
= rect
.height
;
119 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
121 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
122 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
124 win
->Refresh( &rect
);
128 CGContextRef cgContext
;
130 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
133 HIThemeButtonDrawInfo drawInfo
;
136 memset( &drawInfo
, 0, sizeof(drawInfo
) );
137 drawInfo
.version
= 0;
138 drawInfo
.kind
= kThemeListHeaderButton
;
139 drawInfo
.state
= (flags
& wxCONTROL_DISABLED
) ? kThemeStateInactive
: kThemeStateActive
;
140 drawInfo
.value
= (flags
& wxCONTROL_SELECTED
) ? kThemeButtonOn
: kThemeButtonOff
;
141 drawInfo
.adornment
= kThemeAdornmentNone
;
143 // The down arrow is drawn automatically, change it to an up arrow if needed.
144 if ( sortArrow
== wxHDR_SORT_ICON_UP
)
145 drawInfo
.adornment
= kThemeAdornmentHeaderButtonSortUp
;
147 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
149 // If we don't want any arrows we need to draw over the one already there
150 if ( (flags
& wxCONTROL_SELECTED
) && (sortArrow
== wxHDR_SORT_ICON_NONE
) )
152 // clip to the header rectangle
153 CGContextSaveGState( cgContext
);
154 CGContextClipToRect( cgContext
, headerRect
);
155 // but draw bigger than that so the arrow will get clipped off
156 headerRect
.size
.width
+= 25;
157 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
158 CGContextRestoreGState( 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 return 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 // now the wxGCDC is using native transformations
196 const wxCoord x
= rect
.x
;
197 const wxCoord y
= rect
.y
;
198 const wxCoord w
= rect
.width
;
199 const wxCoord h
= rect
.height
;
201 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
203 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
204 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
206 win
->Refresh( &rect
);
210 CGContextRef cgContext
;
212 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
214 HIThemeButtonDrawInfo drawInfo
;
217 memset( &drawInfo
, 0, sizeof(drawInfo
) );
218 drawInfo
.version
= 0;
219 drawInfo
.kind
= kThemeDisclosureButton
;
220 drawInfo
.state
= (flags
& wxCONTROL_DISABLED
) ? kThemeStateInactive
: kThemeStateActive
;
221 // Apple mailing list posts say to use the arrow adornment constants, but those don't work.
222 // We need to set the value using the 'old' DrawThemeButton constants instead.
223 drawInfo
.value
= (flags
& wxCONTROL_EXPANDED
) ? kThemeDisclosureDown
: kThemeDisclosureRight
;
224 drawInfo
.adornment
= kThemeAdornmentNone
;
226 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
230 void wxRendererMac::DrawSplitterSash( wxWindow
*win
,
234 wxOrientation orient
,
235 int WXUNUSED(flags
) )
237 bool hasMetal
= win
->MacGetTopLevelWindow()->MacGetMetalAppearance();
239 GetThemeMetric( kThemeMetricSmallPaneSplitterHeight
, &height
);
241 if (orient
== wxVERTICAL
)
242 splitterRect
= CGRectMake( position
, 0, height
, size
.y
);
244 splitterRect
= CGRectMake( 0, position
, size
.x
, height
);
246 // under compositing we should only draw when called by the OS, otherwise just issue a redraw command
247 // strange redraw errors occur if we don't do this
249 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
251 wxRect
rect( splitterRect
.origin
.x
, splitterRect
.origin
.y
, splitterRect
.size
.width
, splitterRect
.size
.height
);
252 win
->Refresh( &rect
);
256 CGContextRef cgContext
;
258 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
260 HIThemeSplitterDrawInfo drawInfo
;
261 drawInfo
.version
= 0;
262 drawInfo
.state
= kThemeStateActive
;
263 drawInfo
.adornment
= hasMetal
? kHIThemeSplitterAdornmentMetal
: kHIThemeSplitterAdornmentNone
;
264 HIThemeDrawPaneSplitter( &splitterRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
269 wxRendererMac::DrawItemSelectionRect(wxWindow
*win
,
274 if ( !(flags
& wxCONTROL_SELECTED
) )
277 wxBrush
selBrush(wxColour( wxMacCreateCGColorFromHITheme( flags
& wxCONTROL_FOCUSED
?
278 kThemeBrushAlternatePrimaryHighlightColor
279 : kThemeBrushSecondaryHighlightColor
) ) , wxSOLID
);
281 dc
.SetPen( *wxTRANSPARENT_PEN
);
282 dc
.SetBrush( selBrush
);
283 dc
.DrawRectangle( rect
);
288 wxRendererMac::DrawMacThemeButton(wxWindow
*win
,
295 // now the wxGCDC is using native transformations
296 const wxCoord x
= rect
.x
;
297 const wxCoord y
= rect
.y
;
298 const wxCoord w
= rect
.width
;
299 const wxCoord h
= rect
.height
;
301 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
303 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
304 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
306 win
->Refresh( &rect
);
310 CGContextRef cgContext
;
311 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
313 HIThemeButtonDrawInfo drawInfo
;
316 memset( &drawInfo
, 0, sizeof(drawInfo
) );
317 drawInfo
.version
= 0;
318 drawInfo
.kind
= kind
;
319 drawInfo
.state
= (flags
& wxCONTROL_DISABLED
) ? kThemeStateInactive
: kThemeStateActive
;
320 drawInfo
.value
= (flags
& wxCONTROL_SELECTED
) ? kThemeButtonOn
: kThemeButtonOff
;
321 if (flags
& wxCONTROL_UNDETERMINED
)
322 drawInfo
.value
= kThemeButtonMixed
;
323 drawInfo
.adornment
= adornment
;
325 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
330 wxRendererMac::DrawCheckBox(wxWindow
*win
,
335 if (flags
& wxCONTROL_CHECKED
)
336 flags
|= wxCONTROL_SELECTED
;
338 DrawMacThemeButton(win
, dc
, rect
, flags
,
339 kThemeCheckBox
, kThemeAdornmentNone
);
343 wxRendererMac::DrawComboBoxDropButton(wxWindow
*win
,
349 if (win
->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
))
350 kind
= kThemeArrowButtonSmall
;
351 else if (win
->GetWindowVariant() == wxWINDOW_VARIANT_MINI
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
))
352 kind
= kThemeArrowButtonMini
;
354 kind
= kThemeArrowButton
;
356 DrawMacThemeButton(win
, dc
, rect
, flags
,
357 kind
, kThemeAdornmentArrowDownArrow
);
361 wxRendererMac::DrawPushButton(wxWindow
*win
,
367 if (win
->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
))
368 kind
= kThemeBevelButtonSmall
;
369 // There is no kThemeBevelButtonMini, but in this case, use Small
370 else if (win
->GetWindowVariant() == wxWINDOW_VARIANT_MINI
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
))
371 kind
= kThemeBevelButtonSmall
;
373 kind
= kThemeBevelButton
;
375 DrawMacThemeButton(win
, dc
, rect
, flags
,
376 kind
, kThemeAdornmentNone
);
380 wxRendererMac::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
384 wxDelegateRendererNative::DrawFocusRect(win
, dc
, rect
, flags
);
388 CGRect cgrect
= CGRectMake( rect
.x
, rect
.y
, rect
.width
, rect
.height
) ;
390 HIThemeFrameDrawInfo info
;
391 memset( &info
, 0 , sizeof(info
) ) ;
395 info
.state
= kThemeStateActive
;
396 info
.isFocused
= true ;
398 CGContextRef cgContext
= (CGContextRef
) win
->MacGetCGContextRef() ;
399 wxASSERT( cgContext
) ;
401 HIThemeDrawFocusRect( &cgrect
, true , cgContext
, kHIThemeOrientationNormal
) ;