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