]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/renderer.cpp
Applied patch 1586499: wxCoordRound function
[wxWidgets.git] / src / mac / carbon / renderer.cpp
CommitLineData
9c7f49f5 1///////////////////////////////////////////////////////////////////////////////
7af14d71 2// Name: src/mac/carbon/renderer.cpp
38c4cb6a 3// Purpose: implementation of wxRendererNative for Mac
9c7f49f5
VZ
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>
65571936 9// License: wxWindows licence
9c7f49f5
VZ
10///////////////////////////////////////////////////////////////////////////////
11
9c7f49f5
VZ
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"
4c948343
VZ
21 #include "wx/dc.h"
22 #include "wx/bitmap.h"
23 #include "wx/settings.h"
ed4b0fdc 24 #include "wx/dcclient.h"
1832043f 25 #include "wx/toplevel.h"
7af14d71 26#endif
9c7f49f5
VZ
27
28#include "wx/renderer.h"
8acd14d1 29#include "wx/graphics.h"
547aafd2 30#include "wx/mac/uma.h"
9c7f49f5 31
9c7f49f5 32
5cb80ad2 33class WXDLLEXPORT wxRendererMac : public wxDelegateRendererNative
9c7f49f5
VZ
34{
35public:
36 // draw the header control button (used by wxListCtrl)
7af14d71
DS
37 virtual void DrawHeaderButton( wxWindow *win,
38 wxDC& dc,
39 const wxRect& rect,
4b94ddc4 40 int flags = 0,
80752b57 41 wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
4b94ddc4 42 wxHeaderButtonParams* params = NULL );
9c7f49f5 43
4b94ddc4
RD
44 virtual int GetHeaderButtonHeight(wxWindow *win);
45
9c7f49f5 46 // draw the expanded/collapsed icon for a tree control item
7af14d71
DS
47 virtual void DrawTreeItemButton( wxWindow *win,
48 wxDC& dc,
49 const wxRect& rect,
50 int flags = 0 );
9c7f49f5 51
b3208e11 52 // draw a (vertical) sash
7af14d71
DS
53 virtual void DrawSplitterSash( wxWindow *win,
54 wxDC& dc,
55 const wxSize& size,
56 wxCoord position,
57 wxOrientation orient,
58 int flags = 0 );
b3208e11 59
9c7f49f5
VZ
60private:
61 // the tree buttons
7af14d71
DS
62 wxBitmap m_bmpTreeExpanded;
63 wxBitmap m_bmpTreeCollapsed;
9c7f49f5
VZ
64};
65
9c7f49f5
VZ
66// ============================================================================
67// implementation
68// ============================================================================
69
7af14d71 70// static
f0244295 71wxRendererNative& wxRendererNative::GetDefault()
9c7f49f5
VZ
72{
73 static wxRendererMac s_rendererMac;
74
75 return s_rendererMac;
76}
77
7af14d71
DS
78void wxRendererMac::DrawHeaderButton( wxWindow *win,
79 wxDC& dc,
80 const wxRect& rect,
4b94ddc4 81 int flags,
80752b57 82 wxHeaderSortIconType sortArrow,
4b94ddc4 83 wxHeaderButtonParams* params )
9c7f49f5 84{
977d15a6
SC
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);
7af14d71 89
11d1adbf 90 dc.SetBrush( *wxTRANSPARENT_BRUSH );
9c7f49f5 91
a62f32af
SC
92 HIRect headerRect = CGRectMake( x, y, w, h );
93 if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) )
2480cf88 94 {
a62f32af 95 Rect r =
2480cf88 96 {
a62f32af
SC
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;
7af14d71 110
2480cf88 111#if wxMAC_USE_CORE_GRAPHICS
be01a403 112 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
2480cf88 113#else
a62f32af 114 Rect bounds;
7af14d71 115
a62f32af
SC
116 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
117 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
7af14d71 118
a62f32af
SC
119 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
120 CGContextScaleCTM( cgContext, 1, -1 );
7af14d71 121
a62f32af
SC
122 HIShapeReplacePathInCGContext( HIShapeCreateWithQDRgn( (RgnHandle) dc.m_macCurrentClipRgn ), cgContext );
123 CGContextClip( cgContext );
124 HIViewConvertRect( &headerRect, (HIViewRef) win->GetHandle(), (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() );
2480cf88 125#endif
7af14d71 126
a62f32af
SC
127 {
128 HIThemeButtonDrawInfo drawInfo;
129 HIRect labelRect;
7af14d71 130
a62f32af
SC
131 memset( &drawInfo, 0, sizeof(drawInfo) );
132 drawInfo.version = 0;
a62f32af 133 drawInfo.kind = kThemeListHeaderButton;
4b94ddc4
RD
134 drawInfo.state = (flags & wxCONTROL_DISABLED) ? kThemeStateInactive : kThemeStateActive;
135 drawInfo.value = (flags & wxCONTROL_SELECTED) ? kThemeButtonOn : kThemeButtonOff;
a62f32af 136 drawInfo.adornment = kThemeAdornmentNone;
4b94ddc4
RD
137
138 // The down arrow is drawn automatically, change it to an up arrow if needed.
80752b57 139 if ( sortArrow == wxHDR_SORT_ICON_UP )
4b94ddc4
RD
140 drawInfo.adornment = kThemeAdornmentHeaderButtonSortUp;
141
a62f32af 142 HIThemeDrawButton( &headerRect, &drawInfo, cgContext, kHIThemeOrientationNormal, &labelRect );
4b94ddc4
RD
143
144 // If we don't want any arrows we need to draw over the one already there
80752b57 145 if ( (flags & wxCONTROL_SELECTED) && (sortArrow == wxHDR_SORT_ICON_NONE) )
4b94ddc4
RD
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 }
a62f32af 155 }
7af14d71 156
2480cf88
SC
157#if wxMAC_USE_CORE_GRAPHICS
158#else
a62f32af 159 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
2480cf88 160#endif
11d1adbf 161 }
4b94ddc4
RD
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);
80752b57 166 if ( (flags & wxCONTROL_SELECTED) && (sortArrow != wxHDR_SORT_ICON_NONE) )
4b94ddc4
RD
167 {
168 newRect.width -= 12;
80752b57 169 sortArrow = wxHDR_SORT_ICON_NONE;
4b94ddc4 170 }
80752b57 171 flags &= ~wxCONTROL_SELECTED;
4b94ddc4 172
80752b57 173 DrawHeaderButtonContents(win, dc, newRect, flags, sortArrow, params);
4b94ddc4
RD
174}
175
176
177int 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;
9c7f49f5
VZ
188}
189
7af14d71
DS
190void wxRendererMac::DrawTreeItemButton( wxWindow *win,
191 wxDC& dc,
192 const wxRect& rect,
193 int flags )
9c7f49f5 194{
46b59ead
KO
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 =
9c7f49f5 206 {
46b59ead
KO
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 );
9c7f49f5 216 }
46b59ead
KO
217 else
218 {
219 CGContextRef cgContext;
9c7f49f5 220
46b59ead
KO
221#if wxMAC_USE_CORE_GRAPHICS
222 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
223#else
224 Rect bounds;
9c7f49f5 225
46b59ead
KO
226 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
227 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
7af14d71 228
46b59ead
KO
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
7af14d71 236
7af14d71 237 {
46b59ead
KO
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 );
7af14d71 257#endif
46b59ead 258 }
9c7f49f5
VZ
259}
260
7af14d71
DS
261void wxRendererMac::DrawSplitterSash( wxWindow *win,
262 wxDC& dc,
263 const wxSize& size,
264 wxCoord position,
265 wxOrientation orient,
266 int WXUNUSED(flags) )
b3208e11 267{
a62f32af
SC
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 );
7af14d71 276
2480cf88 277#if !wxMAC_USE_CORE_GRAPHICS
a62f32af
SC
278 HIViewConvertRect(
279 &splitterRect,
280 (HIViewRef) win->GetHandle(),
281 (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() );
2480cf88 282#endif
547aafd2 283
a62f32af
SC
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
547aafd2 286
a62f32af
SC
287 if ( !dc.IsKindOf( CLASSINFO( wxPaintDC ) ) )
288 {
289 Rect r =
547aafd2 290 {
a62f32af
SC
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;
7af14d71 305
20b69855 306#if wxMAC_USE_CORE_GRAPHICS
be01a403 307 cgContext = (CGContextRef) dc.GetGraphicsContext()->GetNativeContext();
20b69855 308#else
a62f32af
SC
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 );
2480cf88 314#endif
547aafd2 315
a62f32af
SC
316 HIThemeSplitterDrawInfo drawInfo;
317 drawInfo.version = 0;
318 drawInfo.state = kThemeStateActive;
319 drawInfo.adornment = hasMetal ? kHIThemeSplitterAdornmentMetal : kHIThemeSplitterAdornmentNone;
320 HIThemeDrawPaneSplitter( &splitterRect, &drawInfo, cgContext, kHIThemeOrientationNormal );
2480cf88
SC
321
322#if wxMAC_USE_CORE_GRAPHICS
323#else
a62f32af 324 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
20b69855 325#endif
547aafd2 326 }
b3208e11 327}