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