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