]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/renderer.cpp
old style casts for no-rtti builds
[wxWidgets.git] / src / mac / carbon / renderer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/renderer.cpp
3 // Purpose: implementation of wxRendererNative for Mac
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>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/string.h"
29 #include "wx/dc.h"
30 #include "wx/bitmap.h"
31 #include "wx/settings.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/renderer.h"
35 #include "wx/toplevel.h"
36 #include "wx/dcclient.h"
37 #include "wx/mac/uma.h"
38
39 // ----------------------------------------------------------------------------
40 // wxRendererMac: our wxRendererNative implementation
41 // ----------------------------------------------------------------------------
42
43 class WXDLLEXPORT wxRendererMac : public wxDelegateRendererNative
44 {
45 public:
46 // draw the header control button (used by wxListCtrl)
47 virtual void DrawHeaderButton(wxWindow *win,
48 wxDC& dc,
49 const wxRect& rect,
50 int flags = 0);
51
52 // draw the expanded/collapsed icon for a tree control item
53 virtual void DrawTreeItemButton(wxWindow *win,
54 wxDC& dc,
55 const wxRect& rect,
56 int flags = 0);
57
58 // draw a (vertical) sash
59 virtual void DrawSplitterSash(wxWindow *win,
60 wxDC& dc,
61 const wxSize& size,
62 wxCoord position,
63 wxOrientation orient,
64 int flags = 0);
65
66 private:
67 // the tree buttons
68 wxBitmap m_bmpTreeExpanded,
69 m_bmpTreeCollapsed;
70 };
71
72 // ----------------------------------------------------------------------------
73 // Aqua arrows
74 // ----------------------------------------------------------------------------
75
76 /* XPM */
77 static const char *aqua_arrow_right_xpm[] = {
78 /* columns rows colors chars-per-pixel */
79 "13 11 4 1",
80 " c None",
81 "b c #C0C0C0",
82 "c c #707070",
83 "d c #A0A0A0",
84 /* pixels */
85 " b ",
86 " ddb ",
87 " cccdb ",
88 " cccccd ",
89 " ccccccdb ",
90 " ccccccccd",
91 " ccccccdb ",
92 " cccccb ",
93 " cccdb ",
94 " ddb ",
95 " b "
96 };
97
98 /* XPM */
99 static const char *aqua_arrow_down_xpm[] = {
100 /* columns rows colors chars-per-pixel */
101 "13 11 4 1",
102 " c None",
103 "b c #C0C0C0",
104 "c c #707070",
105 "d c #A0A0A0",
106 /* pixels */
107 " ",
108 " ",
109 " bdcccccccdb ",
110 " dcccccccd ",
111 " bcccccccb ",
112 " dcccccd ",
113 " bcccccb ",
114 " bcccd ",
115 " dcd ",
116 " bcb ",
117 " d "
118 };
119
120 // ============================================================================
121 // implementation
122 // ============================================================================
123
124 /* static */
125 wxRendererNative& wxRendererNative::GetDefault()
126 {
127 static wxRendererMac s_rendererMac;
128
129 return s_rendererMac;
130 }
131
132 void
133 wxRendererMac::DrawHeaderButton(wxWindow *win,
134 wxDC& dc,
135 const wxRect& rect,
136 int flags)
137 {
138 const wxCoord x = rect.x-1,
139 y = rect.y-1,
140 w = rect.width,
141 h = rect.height;
142
143 int major,minor;
144 wxGetOsVersion( &major, &minor );
145
146 dc.SetBrush( *wxTRANSPARENT_BRUSH );
147
148 #if defined(__WXMAC_OSX__) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 )
149 if ( HIThemeDrawButton != 0 )
150 {
151 HIRect headerRect = CGRectMake( x , y , w , h );
152 if ( dc.IsKindOf( CLASSINFO( wxPaintDC ) ) == false )
153 {
154 Rect r = { (short) headerRect.origin.y , (short) headerRect.origin.x ,
155 (short) (headerRect.origin.y + headerRect.size.height) , (short) (headerRect.origin.x + headerRect.size.width) } ;
156 RgnHandle updateRgn = NewRgn() ;
157 RectRgn( updateRgn , &r ) ;
158 HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle() , updateRgn , true ) ;
159 DisposeRgn( updateRgn ) ;
160 }
161 else
162 {
163 CGContextRef cgContext ;
164 #if wxMAC_USE_CORE_GRAPHICS
165 cgContext = (wxMacCGContext*)(dc.GetGraphicContext())->GetNativeContext() ;
166 #else
167 Rect bounds ;
168 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
169 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
170 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
171 CGContextScaleCTM( cgContext , 1 , -1 ) ;
172 HIViewConvertRect( &headerRect , (HIViewRef) win->GetHandle() , (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() ) ;
173 #endif
174 {
175 HIThemeButtonDrawInfo drawInfo ;
176 HIRect labelRect ;
177 memset( &drawInfo , 0 , sizeof(drawInfo) ) ;
178 drawInfo.version = 0 ;
179 drawInfo.state = ( flags & wxCONTROL_DISABLED ) ? kThemeStateInactive : kThemeStateActive ;
180 drawInfo.kind = kThemeListHeaderButton ;
181 drawInfo.value = 0 ;
182 drawInfo.adornment = kThemeAdornmentNone ;
183 HIThemeDrawButton( &headerRect , &drawInfo , cgContext , kHIThemeOrientationNormal , &labelRect ) ;
184 }
185 #if wxMAC_USE_CORE_GRAPHICS
186 #else
187 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
188 #endif
189 }
190 }
191 else
192 #endif
193 {
194 wxMacWindowClipper clipper(win) ;
195 Rect rect = { y , x , y + h , x + w } ;
196 wxPoint origin = win->GetClientAreaOrigin() ;
197 int dx , dy ;
198 dx = origin.x ;
199 dy = origin.y ;
200 win->MacWindowToRootWindow( &dx , &dy ) ;
201 OffsetRect( &rect , dx , dy ) ;
202
203 ThemeButtonDrawInfo drawInfo ;
204 memset( &drawInfo , 0 , sizeof(drawInfo) ) ;
205 drawInfo.state = ( flags & wxCONTROL_DISABLED ) ? kThemeStateInactive : kThemeStateActive ;
206 drawInfo.value = 0 ;
207 drawInfo.adornment = kThemeAdornmentNone ;
208 DrawThemeButton( &rect , kThemeListHeaderButton , &drawInfo , NULL , NULL , NULL , 0 ) ;
209 }
210 }
211
212 void
213 wxRendererMac::DrawTreeItemButton(wxWindow *win,
214 wxDC& dc,
215 const wxRect& rect,
216 int flags)
217 {
218 // init the buttons on demand
219 if ( !m_bmpTreeExpanded.Ok() )
220 {
221 m_bmpTreeExpanded = wxBitmap(aqua_arrow_down_xpm);
222 m_bmpTreeCollapsed = wxBitmap(aqua_arrow_right_xpm);
223 }
224
225 // draw them
226
227 // VZ: this is the old code from treectlg.cpp which apparently doesn't work
228 // but I kept it here just in case it is needed -- if not, please
229 // remove it
230 #if 0 // def __WXMAC__
231 wxMacPortSetter helper(&dc) ;
232 wxMacWindowClipper clipper(this) ;
233 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
234
235 int loc_x = x - 5 ;
236 int loc_y = y_mid - 6 ;
237 MacWindowToRootWindow( & loc_x , & loc_y ) ;
238 Rect bounds = { loc_y , loc_x , loc_y + 18 , loc_x + 12 } ;
239 ThemeButtonDrawInfo info = { kThemeStateActive , item->IsExpanded() ? kThemeDisclosureDown : kThemeDisclosureRight ,
240 kThemeAdornmentNone };
241 DrawThemeButton( &bounds, kThemeDisclosureButton ,
242 &info , NULL , NULL , NULL , NULL ) ;
243 #else // 1
244 dc.DrawBitmap(flags & wxCONTROL_EXPANDED ? m_bmpTreeExpanded
245 : m_bmpTreeCollapsed,
246 rect.x, rect.y, true /* use mask */);
247 #endif // 0/1
248 }
249
250 void
251 wxRendererMac::DrawSplitterSash(wxWindow *win,
252 wxDC& dc,
253 const wxSize& size,
254 wxCoord position,
255 wxOrientation orient,
256 int WXUNUSED(flags))
257 {
258 #if defined(__WXMAC_OSX__) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 )
259 if ( HIThemeDrawPaneSplitter != 0 )
260 {
261 bool hasMetal = win->MacGetTopLevelWindow()->MacGetMetalAppearance() ;
262 SInt32 height ;
263 GetThemeMetric( kThemeMetricSmallPaneSplitterHeight , &height ) ;
264 HIRect splitterRect ;
265 if ( orient == wxVERTICAL )
266 splitterRect = CGRectMake( position , 0 , height, size.y);
267 else
268 splitterRect = CGRectMake( 0 , position , size.x , height );
269 #if !wxMAC_USE_CORE_GRAPHICS
270 HIViewConvertRect( &splitterRect , (HIViewRef) win->GetHandle() , (HIViewRef) win->MacGetTopLevelWindow()->GetHandle() ) ;
271 #endif
272
273 // under compositing we should only draw when called by the OS, otherwise just issue a redraw command
274 // strange redraw errors occur if we don't do this
275
276 if ( dc.IsKindOf( CLASSINFO( wxPaintDC ) ) == false )
277 {
278 Rect r = { (short) splitterRect.origin.y , (short) splitterRect.origin.x ,
279 (short) (splitterRect.origin.y + splitterRect.size.height) , (short) (splitterRect.origin.x + splitterRect.size.width) } ;
280 RgnHandle updateRgn = NewRgn() ;
281 RectRgn( updateRgn , &r ) ;
282 HIViewSetNeedsDisplayInRegion( (HIViewRef) win->GetHandle() , updateRgn , true ) ;
283 DisposeRgn( updateRgn ) ;
284 }
285 else
286 {
287 CGContextRef cgContext ;
288 #if wxMAC_USE_CORE_GRAPHICS
289 cgContext = (wxMacCGContext*)(dc.GetGraphicContext())->GetNativeContext() ;
290 #else
291 Rect bounds ;
292 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
293 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
294 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
295 CGContextScaleCTM( cgContext , 1 , -1 ) ;
296 #endif
297
298 HIThemeSplitterDrawInfo drawInfo ;
299 drawInfo.version = 0 ;
300 drawInfo.state = kThemeStateActive ;
301 drawInfo.adornment = hasMetal ? kHIThemeSplitterAdornmentMetal : kHIThemeSplitterAdornmentNone ;
302 HIThemeDrawPaneSplitter( &splitterRect , &drawInfo , cgContext , kHIThemeOrientationNormal ) ;
303
304 #if wxMAC_USE_CORE_GRAPHICS
305 #else
306 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
307 #endif
308 }
309 }
310 else
311 #endif
312 {
313 // Do the gradient fill:
314 static int grayValues[] =
315 {
316 0xA0, 0xF6, 0xED, 0xE4, 0xE2, 0xD0, 0xA0
317 };
318 dc.SetBrush( *wxTRANSPARENT_BRUSH );
319 if ( orient == wxVERTICAL )
320 {
321 int i;
322 for (i=0; i < (int)WXSIZEOF(grayValues); i++)
323 {
324 dc.SetPen( wxPen( wxColour( grayValues[i] , grayValues[i] , grayValues[i] ),
325 1 , wxSOLID ) );
326 dc.DrawRectangle( position+i, 0, 1, size.y );
327 }
328 }
329 else
330 {
331 int i;
332 for (i=0; i < (int)WXSIZEOF(grayValues); i++)
333 {
334 dc.SetPen( wxPen( wxColour( grayValues[i] , grayValues[i] , grayValues[i] ),
335 1 , wxSOLID ) );
336 dc.DrawRectangle( 0, position+i, size.x, 1 );
337 }
338 }
339 }
340 }
341