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