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/osx/private.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 wxSize
GetCheckBoxSize(wxWindow
* win
);
67 virtual void DrawComboBoxDropButton(wxWindow
*win
,
72 virtual void DrawPushButton(wxWindow
*win
,
77 virtual void DrawItemSelectionRect(wxWindow
*win
,
82 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
85 void DrawMacThemeButton(wxWindow
*win
,
93 wxBitmap m_bmpTreeExpanded
;
94 wxBitmap m_bmpTreeCollapsed
;
97 // ============================================================================
99 // ============================================================================
102 wxRendererNative
& wxRendererNative::GetDefault()
104 static wxRendererMac s_rendererMac
;
106 return s_rendererMac
;
109 int wxRendererMac::DrawHeaderButton( wxWindow
*win
,
113 wxHeaderSortIconType sortArrow
,
114 wxHeaderButtonParams
* params
)
116 const wxCoord x
= rect
.x
;
117 const wxCoord y
= rect
.y
;
118 const wxCoord w
= rect
.width
;
119 const wxCoord h
= rect
.height
;
121 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
123 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
124 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
126 win
->Refresh( &rect
);
130 CGContextRef cgContext
;
131 wxGCDCImpl
*impl
= (wxGCDCImpl
*) dc
.GetImpl();
133 cgContext
= (CGContextRef
) impl
->GetGraphicsContext()->GetNativeContext();
136 HIThemeButtonDrawInfo drawInfo
;
139 memset( &drawInfo
, 0, sizeof(drawInfo
) );
140 drawInfo
.version
= 0;
141 drawInfo
.kind
= kThemeListHeaderButton
;
142 drawInfo
.state
= (flags
& wxCONTROL_DISABLED
) ? kThemeStateInactive
: kThemeStateActive
;
143 drawInfo
.value
= (flags
& wxCONTROL_SELECTED
) ? kThemeButtonOn
: kThemeButtonOff
;
144 drawInfo
.adornment
= kThemeAdornmentNone
;
146 // The down arrow is drawn automatically, change it to an up arrow if needed.
147 if ( sortArrow
== wxHDR_SORT_ICON_UP
)
148 drawInfo
.adornment
= kThemeAdornmentHeaderButtonSortUp
;
150 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
152 // If we don't want any arrows we need to draw over the one already there
153 if ( (flags
& wxCONTROL_SELECTED
) && (sortArrow
== wxHDR_SORT_ICON_NONE
) )
155 // clip to the header rectangle
156 CGContextSaveGState( cgContext
);
157 CGContextClipToRect( cgContext
, headerRect
);
158 // but draw bigger than that so the arrow will get clipped off
159 headerRect
.size
.width
+= 25;
160 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
161 CGContextRestoreGState( cgContext
);
166 // Reserve room for the arrows before writing the label, and turn off the
167 // flags we've already handled
168 wxRect
newRect(rect
);
169 if ( (flags
& wxCONTROL_SELECTED
) && (sortArrow
!= wxHDR_SORT_ICON_NONE
) )
172 sortArrow
= wxHDR_SORT_ICON_NONE
;
174 flags
&= ~wxCONTROL_SELECTED
;
176 return DrawHeaderButtonContents(win
, dc
, newRect
, flags
, sortArrow
, params
);
180 int wxRendererMac::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
182 SInt32 standardHeight
;
185 errStatus
= GetThemeMetric( kThemeMetricListHeaderHeight
, &standardHeight
);
186 if (errStatus
== noErr
)
188 return standardHeight
;
193 void wxRendererMac::DrawTreeItemButton( wxWindow
*win
,
198 // now the wxGCDC is using native transformations
199 const wxCoord x
= rect
.x
;
200 const wxCoord y
= rect
.y
;
201 const wxCoord w
= rect
.width
;
202 const wxCoord h
= rect
.height
;
204 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
206 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
207 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
209 win
->Refresh( &rect
);
213 CGContextRef cgContext
;
215 wxGCDCImpl
*impl
= (wxGCDCImpl
*) dc
.GetImpl();
216 cgContext
= (CGContextRef
) impl
->GetGraphicsContext()->GetNativeContext();
218 HIThemeButtonDrawInfo drawInfo
;
221 memset( &drawInfo
, 0, sizeof(drawInfo
) );
222 drawInfo
.version
= 0;
223 drawInfo
.kind
= kThemeDisclosureButton
;
224 drawInfo
.state
= (flags
& wxCONTROL_DISABLED
) ? kThemeStateInactive
: kThemeStateActive
;
225 // Apple mailing list posts say to use the arrow adornment constants, but those don't work.
226 // We need to set the value using the 'old' DrawThemeButton constants instead.
227 drawInfo
.value
= (flags
& wxCONTROL_EXPANDED
) ? kThemeDisclosureDown
: kThemeDisclosureRight
;
228 drawInfo
.adornment
= kThemeAdornmentNone
;
230 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
234 void wxRendererMac::DrawSplitterSash( wxWindow
*win
,
238 wxOrientation orient
,
239 int WXUNUSED(flags
) )
241 bool hasMetal
= win
->MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL
;
243 GetThemeMetric( kThemeMetricSmallPaneSplitterHeight
, &height
);
245 if (orient
== wxVERTICAL
)
246 splitterRect
= CGRectMake( position
, 0, height
, size
.y
);
248 splitterRect
= CGRectMake( 0, position
, size
.x
, height
);
250 // under compositing we should only draw when called by the OS, otherwise just issue a redraw command
251 // strange redraw errors occur if we don't do this
253 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
255 wxRect
rect( (int) splitterRect
.origin
.x
, (int) splitterRect
.origin
.y
, (int) splitterRect
.size
.width
,
256 (int) splitterRect
.size
.height
);
257 win
->Refresh( &rect
);
261 CGContextRef cgContext
;
262 wxGCDCImpl
*impl
= (wxGCDCImpl
*) dc
.GetImpl();
263 cgContext
= (CGContextRef
) impl
->GetGraphicsContext()->GetNativeContext();
265 HIThemeSplitterDrawInfo drawInfo
;
266 drawInfo
.version
= 0;
267 drawInfo
.state
= kThemeStateActive
;
268 drawInfo
.adornment
= hasMetal
? kHIThemeSplitterAdornmentMetal
: kHIThemeSplitterAdornmentNone
;
269 HIThemeDrawPaneSplitter( &splitterRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
274 wxRendererMac::DrawItemSelectionRect(wxWindow
* WXUNUSED(win
),
279 if ( !(flags
& wxCONTROL_SELECTED
) )
282 wxColour
col( wxMacCreateCGColorFromHITheme( (flags
& wxCONTROL_FOCUSED
) ?
283 kThemeBrushAlternatePrimaryHighlightColor
284 : kThemeBrushSecondaryHighlightColor
) );
285 wxBrush
selBrush( col
);
287 dc
.SetPen( *wxTRANSPARENT_PEN
);
288 dc
.SetBrush( selBrush
);
289 dc
.DrawRectangle( rect
);
294 wxRendererMac::DrawMacThemeButton(wxWindow
*win
,
301 // now the wxGCDC is using native transformations
302 const wxCoord x
= rect
.x
;
303 const wxCoord y
= rect
.y
;
304 const wxCoord w
= rect
.width
;
305 const wxCoord h
= rect
.height
;
307 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
309 HIRect headerRect
= CGRectMake( x
, y
, w
, h
);
310 if ( !dc
.IsKindOf( CLASSINFO( wxPaintDC
) ) )
312 win
->Refresh( &rect
);
316 wxGCDCImpl
*impl
= (wxGCDCImpl
*) dc
.GetImpl();
317 CGContextRef cgContext
;
318 cgContext
= (CGContextRef
) impl
->GetGraphicsContext()->GetNativeContext();
320 HIThemeButtonDrawInfo drawInfo
;
323 memset( &drawInfo
, 0, sizeof(drawInfo
) );
324 drawInfo
.version
= 0;
325 drawInfo
.kind
= kind
;
326 drawInfo
.state
= (flags
& wxCONTROL_DISABLED
) ? kThemeStateInactive
: kThemeStateActive
;
327 drawInfo
.value
= (flags
& wxCONTROL_SELECTED
) ? kThemeButtonOn
: kThemeButtonOff
;
328 if (flags
& wxCONTROL_UNDETERMINED
)
329 drawInfo
.value
= kThemeButtonMixed
;
330 drawInfo
.adornment
= adornment
;
332 HIThemeDrawButton( &headerRect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
, &labelRect
);
337 wxRendererMac::DrawCheckBox(wxWindow
*win
,
342 if (flags
& wxCONTROL_CHECKED
)
343 flags
|= wxCONTROL_SELECTED
;
345 DrawMacThemeButton(win
, dc
, rect
, flags
,
346 kThemeCheckBox
, kThemeAdornmentNone
);
349 wxSize
wxRendererMac::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
352 SInt32 width
, height
;
355 errStatus
= GetThemeMetric(kThemeMetricCheckBoxWidth
, &width
);
356 if (errStatus
== noErr
)
358 size
.SetWidth(width
);
361 errStatus
= GetThemeMetric(kThemeMetricCheckBoxHeight
, &height
);
362 if (errStatus
== noErr
)
364 size
.SetHeight(height
);
371 wxRendererMac::DrawComboBoxDropButton(wxWindow
*win
,
377 if (win
->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
))
378 kind
= kThemeArrowButtonSmall
;
379 else if (win
->GetWindowVariant() == wxWINDOW_VARIANT_MINI
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
))
380 kind
= kThemeArrowButtonMini
;
382 kind
= kThemeArrowButton
;
384 DrawMacThemeButton(win
, dc
, rect
, flags
,
385 kind
, kThemeAdornmentArrowDownArrow
);
389 wxRendererMac::DrawPushButton(wxWindow
*win
,
395 if (win
->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_SMALL
))
396 kind
= kThemeBevelButtonSmall
;
397 // There is no kThemeBevelButtonMini, but in this case, use Small
398 else if (win
->GetWindowVariant() == wxWINDOW_VARIANT_MINI
|| (win
->GetParent() && win
->GetParent()->GetWindowVariant() == wxWINDOW_VARIANT_MINI
))
399 kind
= kThemeBevelButtonSmall
;
401 kind
= kThemeBevelButton
;
403 DrawMacThemeButton(win
, dc
, rect
, flags
,
404 kind
, kThemeAdornmentNone
);
408 wxRendererMac::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
412 wxDelegateRendererNative::DrawFocusRect(win
, dc
, rect
, flags
);
416 CGRect cgrect
= CGRectMake( rect
.x
, rect
.y
, rect
.width
, rect
.height
) ;
418 HIThemeFrameDrawInfo info
;
419 memset( &info
, 0 , sizeof(info
) ) ;
423 info
.state
= kThemeStateActive
;
424 info
.isFocused
= true ;
426 CGContextRef cgContext
= (CGContextRef
) win
->MacGetCGContextRef() ;
427 wxASSERT( cgContext
) ;
429 HIThemeDrawFocusRect( &cgrect
, true , cgContext
, kHIThemeOrientationNormal
) ;