]> git.saurik.com Git - wxWidgets.git/blob - src/generic/renderg.cpp
Patch from Ricardo Pedroso that fixes a segfault when the
[wxWidgets.git] / src / generic / renderg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/renderg.cpp
3 // Purpose: generic implementation of wxRendererNative (for any platform)
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 license
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 #endif //WX_PRECOMP
30
31 #include "wx/gdicmn.h"
32 #include "wx/dc.h"
33
34 #include "wx/settings.h"
35 #include "wx/splitter.h"
36 #include "wx/dcmirror.h"
37 #include "wx/module.h"
38 #include "wx/renderer.h"
39
40 // ----------------------------------------------------------------------------
41 // wxRendererGeneric: our wxRendererNative implementation
42 // ----------------------------------------------------------------------------
43
44 class WXDLLEXPORT wxRendererGeneric : public wxRendererNative
45 {
46 public:
47 wxRendererGeneric();
48
49 virtual void DrawHeaderButton(wxWindow *win,
50 wxDC& dc,
51 const wxRect& rect,
52 int flags = 0);
53
54 virtual void DrawTreeItemButton(wxWindow *win,
55 wxDC& dc,
56 const wxRect& rect,
57 int flags = 0);
58
59 virtual void DrawSplitterBorder(wxWindow *win,
60 wxDC& dc,
61 const wxRect& rect,
62 int flags = 0);
63
64 virtual void DrawSplitterSash(wxWindow *win,
65 wxDC& dc,
66 const wxSize& size,
67 wxCoord position,
68 wxOrientation orient,
69 int flags = 0);
70
71 virtual void DrawComboBoxDropButton(wxWindow *win,
72 wxDC& dc,
73 const wxRect& rect,
74 int flags = 0);
75
76 virtual void DrawDropArrow(wxWindow *win,
77 wxDC& dc,
78 const wxRect& rect,
79 int flags = 0);
80
81 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
82
83 virtual wxRendererVersion GetVersion() const
84 {
85 return wxRendererVersion(wxRendererVersion::Current_Version,
86 wxRendererVersion::Current_Age);
87 }
88
89
90 // Cleanup by deleting standard renderer
91 static void Cleanup();
92
93 // Get the generic object
94 static wxRendererGeneric* DoGetGeneric();
95
96 protected:
97 // draw the rectange using the first pen for the left and top sides and
98 // the second one for the bottom and right ones
99 void DrawShadedRect(wxDC& dc, wxRect *rect,
100 const wxPen& pen1, const wxPen& pen2);
101
102 // the standard pens
103 wxPen m_penBlack,
104 m_penDarkGrey,
105 m_penLightGrey,
106 m_penHighlight;
107
108 static wxRendererGeneric* sm_rendererGeneric;
109 };
110
111 // ============================================================================
112 // wxRendererGeneric implementation
113 // ============================================================================
114
115 // Get the generic object
116 wxRendererGeneric* wxRendererGeneric::DoGetGeneric()
117 {
118 if (!sm_rendererGeneric)
119 sm_rendererGeneric = new wxRendererGeneric;
120 return sm_rendererGeneric;
121 }
122
123 // ----------------------------------------------------------------------------
124 // wxRendererGeneric creation
125 // ----------------------------------------------------------------------------
126
127 /* static */
128 wxRendererNative& wxRendererNative::GetGeneric()
129 {
130 return * wxRendererGeneric::DoGetGeneric();
131 }
132
133 void wxRendererGeneric::Cleanup()
134 {
135 if (sm_rendererGeneric)
136 delete sm_rendererGeneric;
137
138 sm_rendererGeneric = NULL;
139 }
140
141 wxRendererGeneric* wxRendererGeneric::sm_rendererGeneric = NULL;
142
143 wxRendererGeneric::wxRendererGeneric()
144 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW)),
145 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)),
146 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)),
147 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT))
148 {
149 }
150
151 // ----------------------------------------------------------------------------
152 // wxRendererGeneric helpers
153 // ----------------------------------------------------------------------------
154
155 void
156 wxRendererGeneric::DrawShadedRect(wxDC& dc,
157 wxRect *rect,
158 const wxPen& pen1,
159 const wxPen& pen2)
160 {
161 // draw the rectangle
162 dc.SetPen(pen1);
163 dc.DrawLine(rect->GetLeft(), rect->GetTop(),
164 rect->GetLeft(), rect->GetBottom());
165 dc.DrawLine(rect->GetLeft() + 1, rect->GetTop(),
166 rect->GetRight(), rect->GetTop());
167 dc.SetPen(pen2);
168 dc.DrawLine(rect->GetRight(), rect->GetTop(),
169 rect->GetRight(), rect->GetBottom());
170 dc.DrawLine(rect->GetLeft(), rect->GetBottom(),
171 rect->GetRight() + 1, rect->GetBottom());
172
173 // adjust the rect
174 rect->Inflate(-1);
175 }
176
177 // ----------------------------------------------------------------------------
178 // tree/list ctrl drawing
179 // ----------------------------------------------------------------------------
180
181 void
182 wxRendererGeneric::DrawHeaderButton(wxWindow * WXUNUSED(win),
183 wxDC& dc,
184 const wxRect& rect,
185 int WXUNUSED(flags))
186 {
187 const int CORNER = 1;
188
189 const wxCoord x = rect.x,
190 y = rect.y,
191 w = rect.width,
192 h = rect.height;
193
194 dc.SetBrush(*wxTRANSPARENT_BRUSH);
195
196 dc.SetPen(m_penBlack);
197 dc.DrawLine( x+w-CORNER+1, y, x+w, y+h ); // right (outer)
198 dc.DrawRectangle( x, y+h, w+1, 1 ); // bottom (outer)
199
200 dc.SetPen(m_penDarkGrey);
201 dc.DrawLine( x+w-CORNER, y, x+w-1, y+h ); // right (inner)
202 dc.DrawRectangle( x+1, y+h-1, w-2, 1 ); // bottom (inner)
203
204 dc.SetPen(m_penHighlight);
205 dc.DrawRectangle( x, y, w-CORNER+1, 1 ); // top (outer)
206 dc.DrawRectangle( x, y, 1, h ); // left (outer)
207 dc.DrawLine( x, y+h-1, x+1, y+h-1 );
208 dc.DrawLine( x+w-1, y, x+w-1, y+1 );
209 }
210
211 // draw the plus or minus sign
212 void
213 wxRendererGeneric::DrawTreeItemButton(wxWindow * WXUNUSED(win),
214 wxDC& dc,
215 const wxRect& rect,
216 int flags)
217 {
218 // white background
219 dc.SetPen(*wxGREY_PEN);
220 dc.SetBrush(*wxWHITE_BRUSH);
221 dc.DrawRectangle(rect);
222
223 // black lines
224 const wxCoord xMiddle = rect.x + rect.width/2;
225 const wxCoord yMiddle = rect.y + rect.height/2;
226
227 // half of the length of the horz lines in "-" and "+"
228 const wxCoord halfWidth = rect.width/2 - 2;
229 dc.SetPen(*wxBLACK_PEN);
230 dc.DrawLine(xMiddle - halfWidth, yMiddle,
231 xMiddle + halfWidth + 1, yMiddle);
232
233 if ( !(flags & wxCONTROL_EXPANDED) )
234 {
235 // turn "-" into "+"
236 const wxCoord halfHeight = rect.height/2 - 2;
237 dc.DrawLine(xMiddle, yMiddle - halfHeight,
238 xMiddle, yMiddle + halfHeight + 1);
239 }
240 }
241
242 // ----------------------------------------------------------------------------
243 // sash drawing
244 // ----------------------------------------------------------------------------
245
246 wxSplitterRenderParams
247 wxRendererGeneric::GetSplitterParams(const wxWindow *win)
248 {
249 // see below
250 wxCoord sashWidth,
251 border;
252
253 if ( win->HasFlag(wxSP_3DSASH) )
254 sashWidth = 7;
255 else if ( win->HasFlag(wxSP_NOSASH) )
256 sashWidth = 0;
257 else // no 3D effect
258 sashWidth = 3;
259
260 if ( win->HasFlag(wxSP_3DBORDER) )
261 border = 2;
262 else // no 3D effect
263 border = 0;
264
265 return wxSplitterRenderParams(sashWidth, border, false);
266 }
267
268 void
269 wxRendererGeneric::DrawSplitterBorder(wxWindow *win,
270 wxDC& dc,
271 const wxRect& rectOrig,
272 int WXUNUSED(falgs))
273 {
274 if ( win->HasFlag(wxSP_3DBORDER) )
275 {
276 wxRect rect = rectOrig;
277 DrawShadedRect(dc, &rect, m_penDarkGrey, m_penHighlight);
278 DrawShadedRect(dc, &rect, m_penBlack, m_penLightGrey);
279 }
280 }
281
282 void
283 wxRendererGeneric::DrawSplitterSash(wxWindow *win,
284 wxDC& dcReal,
285 const wxSize& sizeReal,
286 wxCoord position,
287 wxOrientation orient,
288 int WXUNUSED(flags))
289 {
290 // to avoid duplicating the same code for horizontal and vertical sashes,
291 // simply mirror the DC instead if needed (i.e. if horz splitter)
292 wxMirrorDC dc(dcReal, orient != wxVERTICAL);
293 wxSize size = dc.Reflect(sizeReal);
294
295
296 // we draw a Win32-like grey sash with possible 3D border here:
297 //
298 // ---- this is position
299 // /
300 // v
301 // dWGGGDd
302 // GWGGGDB
303 // GWGGGDB where G is light grey (face)
304 // GWGGGDB W white (light)
305 // GWGGGDB D dark grey (shadow)
306 // GWGGGDB B black (dark shadow)
307 // GWGGGDB
308 // GWGGGDB and lower letters are our border (already drawn)
309 // GWGGGDB
310 // wWGGGDd
311 //
312 // only the middle 3 columns are drawn unless wxSP_3D is specified
313
314 const wxCoord h = size.y;
315 wxCoord offset = 0;
316
317 // If we're drawing the border, draw the sash 3d lines shorter
318 if ( win->HasFlag(wxSP_3DBORDER) )
319 {
320 offset = 1;
321 }
322
323 dc.SetPen(*wxTRANSPARENT_PEN);
324 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));
325
326 if ( win->HasFlag(wxSP_3DSASH) )
327 {
328 // Draw the 3D sash
329 dc.DrawRectangle(position + 2, 0, 3, h);
330
331 dc.SetPen(m_penLightGrey);
332 dc.DrawLine(position, offset, position, h - offset);
333
334 dc.SetPen(m_penHighlight);
335 dc.DrawLine(position + 1, 0, position + 1, h);
336
337 dc.SetPen(m_penDarkGrey);
338 dc.DrawLine(position + 5, 0, position + 5, h);
339
340 dc.SetPen(m_penBlack);
341 dc.DrawLine(position + 6, offset, position + 6, h - offset);
342 }
343 else
344 {
345 // Draw a flat sash
346 dc.DrawRectangle(position, 0, 3, h);
347 }
348 }
349
350 // ----------------------------------------------------------------------------
351 // button drawing
352 // ----------------------------------------------------------------------------
353
354 void
355 wxRendererGeneric::DrawComboBoxDropButton(wxWindow *win,
356 wxDC& dc,
357 const wxRect& rect,
358 int WXUNUSED(flags))
359 {
360 // Creating a generic button background that would actually be
361 // useful is rather difficult to accomplish. Best compromise
362 // is to use window's background colour to achieve transparent'
363 // ish appearance that should look decent in combo box style
364 // controls.
365 wxColour col = win->GetBackgroundColour();
366 dc.SetBrush(wxBrush(col));
367 dc.SetPen(wxPen(col));
368 dc.DrawRectangle(rect);
369 DrawDropArrow(win,dc,rect);
370 }
371
372
373 void
374 wxRendererGeneric::DrawDropArrow(wxWindow *win,
375 wxDC& dc,
376 const wxRect& rect,
377 int WXUNUSED(flags))
378 {
379 // This generic implementation should be good
380 // enough for Windows platforms (including XP).
381
382 int arrowHalf = rect.width/5;
383 int rectMid = rect.width / 2;
384 int arrowTopY = (rect.height/2) - (arrowHalf/2);
385
386 // This should always result in arrow with odd width.
387 wxPoint pt[] =
388 {
389 wxPoint(rectMid - arrowHalf, arrowTopY),
390 wxPoint(rectMid + arrowHalf, arrowTopY),
391 wxPoint(rectMid, arrowTopY + arrowHalf)
392 };
393 dc.SetBrush(wxBrush(win->GetForegroundColour()));
394 dc.SetPen(wxPen(win->GetForegroundColour()));
395 dc.DrawPolygon(WXSIZEOF(pt), pt, rect.x, rect.y);
396 }
397
398 // ----------------------------------------------------------------------------
399 // A module to allow cleanup of generic renderer.
400 // ----------------------------------------------------------------------------
401
402 class wxGenericRendererModule: public wxModule
403 {
404 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule)
405 public:
406 wxGenericRendererModule() {}
407 bool OnInit() { return true; };
408 void OnExit() { wxRendererGeneric::Cleanup(); };
409 };
410
411 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule, wxModule)
412