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( (int) splitterRect
.origin
.x
, (int) splitterRect
.origin
.y
, (int) splitterRect
.size
.width
,
252 (int) splitterRect
.size
.height
);
253 win
->Refresh( &rect
);
257 CGContextRef cgContext
;
259 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
261 HIThemeSplitterDrawInfo drawInfo
;
262 drawInfo
.version
= 0;
263 drawInfo
.state
= kThemeStateActive
;
264 drawInfo
.adornment
= hasMetal
? kHIThemeSplitterAdornmentMetal
: kHIThemeSplitterAdornmentNone
;
265 HIThemeDrawPaneSplitter( &splitterRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
270 wxRendererMac::DrawItemSelectionRect(wxWindow
*win
,
275 if ( !(flags
& wxCONTROL_SELECTED
) )
278 wxColour
col( wxMacCreateCGColorFromHITheme( (flags
& wxCONTROL_FOCUSED
) ?
279 kThemeBrushAlternatePrimaryHighlightColor
280 : kThemeBrushSecondaryHighlightColor
) );
281 wxBrush
selBrush( col
);
283 dc
.SetPen( *wxTRANSPARENT_PEN
);
284 dc
.SetBrush( selBrush
);
285 dc
.DrawRectangle( rect
);
290 wxRendererMac::DrawMacThemeButton(wxWindow
*win
,
297 // now the wxGCDC is using native transformations
298 const wxCoord x
= rect
.x
;
299 const wxCoord y
= rect
.y
;
300 const wxCoord w
= rect
.width
;
301 const wxCoord h
= rect
.height
;
303 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
305 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
306 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
308 win
->Refresh( &rect
);
312 CGContextRef cgContext
;
313 cgContext
= (CGContextRef
) dc
.GetGraphicsContext()->GetNativeContext();
315 HIThemeButtonDrawInfo drawInfo
;
318 memset( &drawInfo
, 0, sizeof(drawInfo
) );
319 drawInfo
.version
= 0;
320 drawInfo
.kind
= kind
;
321 drawInfo
.state
= (flags
& wxCONTROL_DISABLED
) ? kThemeStateInactive
: kThemeStateActive
;
322 drawInfo
.value
= (flags
& wxCONTROL_SELECTED
) ? kThemeButtonOn
: kThemeButtonOff
;
323 if (flags
& wxCONTROL_UNDETERMINED
)
324 drawInfo
.value
= kThemeButtonMixed
;
325 drawInfo
.adornment
= adornment
;
327 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
332 wxRendererMac::DrawCheckBox(wxWindow
*win
,
337 if (flags
& wxCONTROL_CHECKED
)
338 flags
|= wxCONTROL_SELECTED
;
340 DrawMacThemeButton(win
, dc
, rect
, flags
,
341 kThemeCheckBox
, kThemeAdornmentNone
);
345 wxRendererMac::DrawComboBoxDropButton(wxWindow
*win
,
351 if (win
->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
))
352 kind
= kThemeArrowButtonSmall
;
353 else if (win
->GetWindowVariant() == wxWINDOW_VARIANT_MINI
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
))
354 kind
= kThemeArrowButtonMini
;
356 kind
= kThemeArrowButton
;
358 DrawMacThemeButton(win
, dc
, rect
, flags
,
359 kind
, kThemeAdornmentArrowDownArrow
);
363 wxRendererMac::DrawPushButton(wxWindow
*win
,
369 if (win
->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
))
370 kind
= kThemeBevelButtonSmall
;
371 // There is no kThemeBevelButtonMini, but in this case, use Small
372 else if (win
->GetWindowVariant() == wxWINDOW_VARIANT_MINI
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
))
373 kind
= kThemeBevelButtonSmall
;
375 kind
= kThemeBevelButton
;
377 DrawMacThemeButton(win
, dc
, rect
, flags
,
378 kind
, kThemeAdornmentNone
);
382 wxRendererMac::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
386 wxDelegateRendererNative::DrawFocusRect(win
, dc
, rect
, flags
);
390 CGRect cgrect
= CGRectMake( rect
.x
, rect
.y
, rect
.width
, rect
.height
) ;
392 HIThemeFrameDrawInfo info
;
393 memset( &info
, 0 , sizeof(info
) ) ;
397 info
.state
= kThemeStateActive
;
398 info
.isFocused
= true ;
400 CGContextRef cgContext
= (CGContextRef
) win
->MacGetCGContextRef() ;
401 wxASSERT( cgContext
) ;
403 HIThemeDrawFocusRect( &cgrect
, true , cgContext
, kHIThemeOrientationNormal
) ;