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