Committed Jaako's renderer patch
[wxWidgets.git] / src / generic / renderg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 void DrawCheckButton(wxWindow *win,
82 wxDC& dc,
83 const wxRect& rect,
84 int flags = 0);
85
86 virtual void DrawPushButton(wxWindow *win,
87 wxDC& dc,
88 const wxRect& rect,
89 int flags = 0);
90
91 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
92
93 virtual wxRendererVersion GetVersion() const
94 {
95 return wxRendererVersion(wxRendererVersion::Current_Version,
96 wxRendererVersion::Current_Age);
97 }
98
99
100 // Cleanup by deleting standard renderer
101 static void Cleanup();
102
103 // Get the generic object
104 static wxRendererGeneric* DoGetGeneric();
105
106 protected:
107 // draw the rectange using the first pen for the left and top sides and
108 // the second one for the bottom and right ones
109 void DrawShadedRect(wxDC& dc, wxRect *rect,
110 const wxPen& pen1, const wxPen& pen2);
111
112 // the standard pens
113 wxPen m_penBlack,
114 m_penDarkGrey,
115 m_penLightGrey,
116 m_penHighlight;
117
118 static wxRendererGeneric* sm_rendererGeneric;
119 };
120
121 // ============================================================================
122 // wxRendererGeneric implementation
123 // ============================================================================
124
125 // Get the generic object
126 wxRendererGeneric* wxRendererGeneric::DoGetGeneric()
127 {
128 if (!sm_rendererGeneric)
129 sm_rendererGeneric = new wxRendererGeneric;
130 return sm_rendererGeneric;
131 }
132
133 // ----------------------------------------------------------------------------
134 // wxRendererGeneric creation
135 // ----------------------------------------------------------------------------
136
137 /* static */
138 wxRendererNative& wxRendererNative::GetGeneric()
139 {
140 return * wxRendererGeneric::DoGetGeneric();
141 }
142
143 void wxRendererGeneric::Cleanup()
144 {
145 if (sm_rendererGeneric)
146 delete sm_rendererGeneric;
147
148 sm_rendererGeneric = NULL;
149 }
150
151 wxRendererGeneric* wxRendererGeneric::sm_rendererGeneric = NULL;
152
153 wxRendererGeneric::wxRendererGeneric()
154 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW)),
155 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)),
156 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)),
157 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT))
158 {
159 }
160
161 // ----------------------------------------------------------------------------
162 // wxRendererGeneric helpers
163 // ----------------------------------------------------------------------------
164
165 void
166 wxRendererGeneric::DrawShadedRect(wxDC& dc,
167 wxRect *rect,
168 const wxPen& pen1,
169 const wxPen& pen2)
170 {
171 // draw the rectangle
172 dc.SetPen(pen1);
173 dc.DrawLine(rect->GetLeft(), rect->GetTop(),
174 rect->GetLeft(), rect->GetBottom());
175 dc.DrawLine(rect->GetLeft() + 1, rect->GetTop(),
176 rect->GetRight(), rect->GetTop());
177 dc.SetPen(pen2);
178 dc.DrawLine(rect->GetRight(), rect->GetTop(),
179 rect->GetRight(), rect->GetBottom());
180 dc.DrawLine(rect->GetLeft(), rect->GetBottom(),
181 rect->GetRight() + 1, rect->GetBottom());
182
183 // adjust the rect
184 rect->Inflate(-1);
185 }
186
187 // ----------------------------------------------------------------------------
188 // tree/list ctrl drawing
189 // ----------------------------------------------------------------------------
190
191 void
192 wxRendererGeneric::DrawHeaderButton(wxWindow * WXUNUSED(win),
193 wxDC& dc,
194 const wxRect& rect,
195 int WXUNUSED(flags))
196 {
197 const int CORNER = 1;
198
199 const wxCoord x = rect.x,
200 y = rect.y,
201 w = rect.width,
202 h = rect.height;
203
204 dc.SetBrush(*wxTRANSPARENT_BRUSH);
205
206 dc.SetPen(m_penBlack);
207 dc.DrawLine( x+w-CORNER+1, y, x+w, y+h ); // right (outer)
208 dc.DrawRectangle( x, y+h, w+1, 1 ); // bottom (outer)
209
210 dc.SetPen(m_penDarkGrey);
211 dc.DrawLine( x+w-CORNER, y, x+w-1, y+h ); // right (inner)
212 dc.DrawRectangle( x+1, y+h-1, w-2, 1 ); // bottom (inner)
213
214 dc.SetPen(m_penHighlight);
215 dc.DrawRectangle( x, y, w-CORNER+1, 1 ); // top (outer)
216 dc.DrawRectangle( x, y, 1, h ); // left (outer)
217 dc.DrawLine( x, y+h-1, x+1, y+h-1 );
218 dc.DrawLine( x+w-1, y, x+w-1, y+1 );
219 }
220
221 // draw the plus or minus sign
222 void
223 wxRendererGeneric::DrawTreeItemButton(wxWindow * WXUNUSED(win),
224 wxDC& dc,
225 const wxRect& rect,
226 int flags)
227 {
228 // white background
229 dc.SetPen(*wxGREY_PEN);
230 dc.SetBrush(*wxWHITE_BRUSH);
231 dc.DrawRectangle(rect);
232
233 // black lines
234 const wxCoord xMiddle = rect.x + rect.width/2;
235 const wxCoord yMiddle = rect.y + rect.height/2;
236
237 // half of the length of the horz lines in "-" and "+"
238 const wxCoord halfWidth = rect.width/2 - 2;
239 dc.SetPen(*wxBLACK_PEN);
240 dc.DrawLine(xMiddle - halfWidth, yMiddle,
241 xMiddle + halfWidth + 1, yMiddle);
242
243 if ( !(flags & wxCONTROL_EXPANDED) )
244 {
245 // turn "-" into "+"
246 const wxCoord halfHeight = rect.height/2 - 2;
247 dc.DrawLine(xMiddle, yMiddle - halfHeight,
248 xMiddle, yMiddle + halfHeight + 1);
249 }
250 }
251
252 // ----------------------------------------------------------------------------
253 // sash drawing
254 // ----------------------------------------------------------------------------
255
256 wxSplitterRenderParams
257 wxRendererGeneric::GetSplitterParams(const wxWindow *win)
258 {
259 // see below
260 wxCoord sashWidth,
261 border;
262
263 if ( win->HasFlag(wxSP_3DSASH) )
264 sashWidth = 7;
265 else if ( win->HasFlag(wxSP_NOSASH) )
266 sashWidth = 0;
267 else // no 3D effect
268 sashWidth = 3;
269
270 if ( win->HasFlag(wxSP_3DBORDER) )
271 border = 2;
272 else // no 3D effect
273 border = 0;
274
275 return wxSplitterRenderParams(sashWidth, border, false);
276 }
277
278 void
279 wxRendererGeneric::DrawSplitterBorder(wxWindow *win,
280 wxDC& dc,
281 const wxRect& rectOrig,
282 int WXUNUSED(falgs))
283 {
284 if ( win->HasFlag(wxSP_3DBORDER) )
285 {
286 wxRect rect = rectOrig;
287 DrawShadedRect(dc, &rect, m_penDarkGrey, m_penHighlight);
288 DrawShadedRect(dc, &rect, m_penBlack, m_penLightGrey);
289 }
290 }
291
292 void
293 wxRendererGeneric::DrawSplitterSash(wxWindow *win,
294 wxDC& dcReal,
295 const wxSize& sizeReal,
296 wxCoord position,
297 wxOrientation orient,
298 int WXUNUSED(flags))
299 {
300 // to avoid duplicating the same code for horizontal and vertical sashes,
301 // simply mirror the DC instead if needed (i.e. if horz splitter)
302 wxMirrorDC dc(dcReal, orient != wxVERTICAL);
303 wxSize size = dc.Reflect(sizeReal);
304
305
306 // we draw a Win32-like grey sash with possible 3D border here:
307 //
308 // ---- this is position
309 // /
310 // v
311 // dWGGGDd
312 // GWGGGDB
313 // GWGGGDB where G is light grey (face)
314 // GWGGGDB W white (light)
315 // GWGGGDB D dark grey (shadow)
316 // GWGGGDB B black (dark shadow)
317 // GWGGGDB
318 // GWGGGDB and lower letters are our border (already drawn)
319 // GWGGGDB
320 // wWGGGDd
321 //
322 // only the middle 3 columns are drawn unless wxSP_3D is specified
323
324 const wxCoord h = size.y;
325 wxCoord offset = 0;
326
327 // If we're drawing the border, draw the sash 3d lines shorter
328 if ( win->HasFlag(wxSP_3DBORDER) )
329 {
330 offset = 1;
331 }
332
333 dc.SetPen(*wxTRANSPARENT_PEN);
334 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));
335
336 if ( win->HasFlag(wxSP_3DSASH) )
337 {
338 // Draw the 3D sash
339 dc.DrawRectangle(position + 2, 0, 3, h);
340
341 dc.SetPen(m_penLightGrey);
342 dc.DrawLine(position, offset, position, h - offset);
343
344 dc.SetPen(m_penHighlight);
345 dc.DrawLine(position + 1, 0, position + 1, h);
346
347 dc.SetPen(m_penDarkGrey);
348 dc.DrawLine(position + 5, 0, position + 5, h);
349
350 dc.SetPen(m_penBlack);
351 dc.DrawLine(position + 6, offset, position + 6, h - offset);
352 }
353 else
354 {
355 // Draw a flat sash
356 dc.DrawRectangle(position, 0, 3, h);
357 }
358 }
359
360 // ----------------------------------------------------------------------------
361 // button drawing
362 // ----------------------------------------------------------------------------
363
364 void
365 wxRendererGeneric::DrawComboBoxDropButton(wxWindow *win,
366 wxDC& dc,
367 const wxRect& rect,
368 int flags)
369 {
370 DrawPushButton(win,dc,rect,flags);
371 DrawDropArrow(win,dc,rect,flags);
372 }
373
374 void
375 wxRendererGeneric::DrawDropArrow(wxWindow *win,
376 wxDC& dc,
377 const wxRect& rect,
378 int WXUNUSED(flags))
379 {
380 // This generic implementation should be good
381 // enough for Windows platforms (including XP).
382
383 int arrowHalf = rect.width/5;
384 int rectMid = rect.width / 2;
385 int arrowTopY = (rect.height/2) - (arrowHalf/2);
386
387 // This should always result in arrow with odd width.
388 wxPoint pt[] =
389 {
390 wxPoint(rectMid - arrowHalf, arrowTopY),
391 wxPoint(rectMid + arrowHalf, arrowTopY),
392 wxPoint(rectMid, arrowTopY + arrowHalf)
393 };
394 dc.SetBrush(wxBrush(win->GetForegroundColour()));
395 dc.SetPen(wxPen(win->GetForegroundColour()));
396 dc.DrawPolygon(WXSIZEOF(pt), pt, rect.x, rect.y);
397 }
398
399 void
400 wxRendererGeneric::DrawCheckButton(wxWindow *WXUNUSED(win),
401 wxDC& dc,
402 const wxRect& rect,
403 int flags)
404 {
405 dc.SetPen(*(flags & wxCONTROL_DISABLED ? wxGREY_PEN : wxBLACK_PEN));
406 dc.SetBrush( *wxTRANSPARENT_BRUSH );
407 dc.DrawRectangle(rect);
408
409 if ( flags & wxCONTROL_CHECKED )
410 {
411 dc.DrawCheckMark(rect.Deflate(2, 2));
412 }
413 }
414
415 void
416 wxRendererGeneric::DrawPushButton(wxWindow *win,
417 wxDC& dc,
418 const wxRect& rect,
419 int flags)
420 {
421 // Don't try anything too fancy. It'll just turn out looking
422 // out-of-place on most platforms.
423 wxColour bgCol = flags & wxCONTROL_DISABLED ?
424 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) :
425 win->GetBackgroundColour();
426 dc.SetBrush(wxBrush(bgCol));
427 dc.SetPen(wxPen(bgCol));
428 dc.DrawRectangle(rect);
429 }
430
431 // ----------------------------------------------------------------------------
432 // A module to allow cleanup of generic renderer.
433 // ----------------------------------------------------------------------------
434
435 class wxGenericRendererModule: public wxModule
436 {
437 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule)
438 public:
439 wxGenericRendererModule() {}
440 bool OnInit() { return true; };
441 void OnExit() { wxRendererGeneric::Cleanup(); };
442 };
443
444 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule, wxModule)