]> git.saurik.com Git - wxWidgets.git/blob - src/generic/renderg.cpp
docstrings and other minor tweaks
[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 #include "wx/module.h"
35 #endif //WX_PRECOMP
36
37 #include "wx/splitter.h"
38 #include "wx/dcmirror.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 wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
54 wxHeaderButtonParams* params = NULL);
55
56 virtual void DrawHeaderButtonContents(wxWindow *win,
57 wxDC& dc,
58 const wxRect& rect,
59 int flags = 0,
60 wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
61 wxHeaderButtonParams* params = NULL);
62
63 virtual int GetHeaderButtonHeight(wxWindow *win);
64
65 virtual void DrawTreeItemButton(wxWindow *win,
66 wxDC& dc,
67 const wxRect& rect,
68 int flags = 0);
69
70 virtual void DrawSplitterBorder(wxWindow *win,
71 wxDC& dc,
72 const wxRect& rect,
73 int flags = 0);
74
75 virtual void DrawSplitterSash(wxWindow *win,
76 wxDC& dc,
77 const wxSize& size,
78 wxCoord position,
79 wxOrientation orient,
80 int flags = 0);
81
82 virtual void DrawComboBoxDropButton(wxWindow *win,
83 wxDC& dc,
84 const wxRect& rect,
85 int flags = 0);
86
87 virtual void DrawDropArrow(wxWindow *win,
88 wxDC& dc,
89 const wxRect& rect,
90 int flags = 0);
91
92 virtual void DrawCheckBox(wxWindow *win,
93 wxDC& dc,
94 const wxRect& rect,
95 int flags = 0);
96
97 virtual void DrawPushButton(wxWindow *win,
98 wxDC& dc,
99 const wxRect& rect,
100 int flags = 0);
101
102 virtual void DrawItemSelectionRect(wxWindow *win,
103 wxDC& dc,
104 const wxRect& rect,
105 int flags = 0);
106
107 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
108
109 virtual wxRendererVersion GetVersion() const
110 {
111 return wxRendererVersion(wxRendererVersion::Current_Version,
112 wxRendererVersion::Current_Age);
113 }
114
115
116 // Cleanup by deleting standard renderer
117 static void Cleanup();
118
119 // Get the generic object
120 static wxRendererGeneric* DoGetGeneric();
121
122 protected:
123 // draw the rectange using the first pen for the left and top sides and
124 // the second one for the bottom and right ones
125 void DrawShadedRect(wxDC& dc, wxRect *rect,
126 const wxPen& pen1, const wxPen& pen2);
127
128 // the standard pens
129 wxPen m_penBlack,
130 m_penDarkGrey,
131 m_penLightGrey,
132 m_penHighlight;
133
134 static wxRendererGeneric* sm_rendererGeneric;
135 };
136
137 // ============================================================================
138 // wxRendererGeneric implementation
139 // ============================================================================
140
141 // Get the generic object
142 wxRendererGeneric* wxRendererGeneric::DoGetGeneric()
143 {
144 if (!sm_rendererGeneric)
145 sm_rendererGeneric = new wxRendererGeneric;
146 return sm_rendererGeneric;
147 }
148
149 // ----------------------------------------------------------------------------
150 // wxRendererGeneric creation
151 // ----------------------------------------------------------------------------
152
153 /* static */
154 wxRendererNative& wxRendererNative::GetGeneric()
155 {
156 return * wxRendererGeneric::DoGetGeneric();
157 }
158
159 void wxRendererGeneric::Cleanup()
160 {
161 if (sm_rendererGeneric)
162 delete sm_rendererGeneric;
163
164 sm_rendererGeneric = NULL;
165 }
166
167 wxRendererGeneric* wxRendererGeneric::sm_rendererGeneric = NULL;
168
169 wxRendererGeneric::wxRendererGeneric()
170 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW)),
171 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)),
172 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)),
173 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT))
174 {
175 }
176
177 // ----------------------------------------------------------------------------
178 // wxRendererGeneric helpers
179 // ----------------------------------------------------------------------------
180
181 void
182 wxRendererGeneric::DrawShadedRect(wxDC& dc,
183 wxRect *rect,
184 const wxPen& pen1,
185 const wxPen& pen2)
186 {
187 // draw the rectangle
188 dc.SetPen(pen1);
189 dc.DrawLine(rect->GetLeft(), rect->GetTop(),
190 rect->GetLeft(), rect->GetBottom());
191 dc.DrawLine(rect->GetLeft() + 1, rect->GetTop(),
192 rect->GetRight(), rect->GetTop());
193 dc.SetPen(pen2);
194 dc.DrawLine(rect->GetRight(), rect->GetTop(),
195 rect->GetRight(), rect->GetBottom());
196 dc.DrawLine(rect->GetLeft(), rect->GetBottom(),
197 rect->GetRight() + 1, rect->GetBottom());
198
199 // adjust the rect
200 rect->Inflate(-1);
201 }
202
203 // ----------------------------------------------------------------------------
204 // tree/list ctrl drawing
205 // ----------------------------------------------------------------------------
206
207 void
208 wxRendererGeneric::DrawHeaderButton(wxWindow* win,
209 wxDC& dc,
210 const wxRect& rect,
211 int flags,
212 wxHeaderSortIconType sortArrow,
213 wxHeaderButtonParams* params)
214 {
215 const int CORNER = 1;
216
217 const wxCoord x = rect.x,
218 y = rect.y,
219 w = rect.width,
220 h = rect.height;
221
222 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));
223 dc.SetPen(*wxTRANSPARENT_PEN);
224 dc.DrawRectangle(rect);
225
226 dc.SetBrush(*wxTRANSPARENT_BRUSH);
227
228 dc.SetPen(m_penBlack);
229 dc.DrawLine( x+w-CORNER+1, y, x+w, y+h ); // right (outer)
230 dc.DrawRectangle( x, y+h, w+1, 1 ); // bottom (outer)
231
232 dc.SetPen(m_penDarkGrey);
233 dc.DrawLine( x+w-CORNER, y, x+w-1, y+h ); // right (inner)
234 dc.DrawRectangle( x+1, y+h-1, w-2, 1 ); // bottom (inner)
235
236 dc.SetPen(m_penHighlight);
237 dc.DrawRectangle( x, y, w-CORNER+1, 1 ); // top (outer)
238 dc.DrawRectangle( x, y, 1, h ); // left (outer)
239 dc.DrawLine( x, y+h-1, x+1, y+h-1 );
240 dc.DrawLine( x+w-1, y, x+w-1, y+1 );
241
242 DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
243 }
244
245
246 void
247 wxRendererGeneric::DrawHeaderButtonContents(wxWindow *win,
248 wxDC& dc,
249 const wxRect& rect,
250 int flags,
251 wxHeaderSortIconType sortArrow,
252 wxHeaderButtonParams* params)
253 {
254 // Mark this item as selected. For the generic version we'll just draw an
255 // underline
256 if ( flags & wxCONTROL_SELECTED )
257 {
258 // draw a line at the bottom of the header button, overlaying the
259 // native hot-tracking line (on XP)
260 const int penwidth = 3;
261 int y = rect.y + rect.height + 1 - penwidth;
262 wxColour c = (params && params->m_selectionColour.Ok()) ?
263 params->m_selectionColour : wxColour(0x66, 0x66, 0x66);
264 wxPen pen(c, penwidth);
265 pen.SetCap(wxCAP_BUTT);
266 dc.SetPen(pen);
267 dc.DrawLine(rect.x, y, rect.x + rect.width, y);
268 }
269
270 // Draw an up or down arrow
271 int arrowSpace = 0;
272 if (sortArrow != wxHDR_SORT_ICON_NONE )
273 {
274 wxRect ar = rect;
275
276 // make a rect for the arrow
277 ar.height = 4;
278 ar.width = 8;
279 ar.y += (rect.height - ar.height)/2;
280 ar.x = ar.x + rect.width - 3*ar.width/2;
281 arrowSpace = 3*ar.width/2; // space to preserve when drawing the label
282
283 wxPoint triPt[3];
284 if ( sortArrow & wxHDR_SORT_ICON_UP )
285 {
286 triPt[0].x = ar.width / 2;
287 triPt[0].y = 0;
288 triPt[1].x = ar.width;
289 triPt[1].y = ar.height;
290 triPt[2].x = 0;
291 triPt[2].y = ar.height;
292 }
293 else
294 {
295 triPt[0].x = 0;
296 triPt[0].y = 0;
297 triPt[1].x = ar.width;
298 triPt[1].y = 0;
299 triPt[2].x = ar.width / 2;
300 triPt[2].y = ar.height;
301 }
302
303 wxColour c = (params && params->m_arrowColour.Ok()) ?
304 params->m_arrowColour : wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW);
305 dc.SetPen(wxPen(c));
306 dc.SetBrush(wxBrush(c));
307 dc.DrawPolygon( 3, triPt, ar.x, ar.y);
308 }
309
310 const int margin = 5; // number of pixels to reserve on either side of the label
311 int bmpWidth = 0;
312 int txtEnd = 0;
313
314 if ( params && params->m_labelBitmap.Ok() )
315 bmpWidth = params->m_labelBitmap.GetWidth() + 2;
316
317 // Draw a label if one is given
318 if ( params && !params->m_labelText.empty() )
319 {
320 wxFont font = params->m_labelFont.Ok() ?
321 params->m_labelFont : win->GetFont();
322 wxColour clr = params->m_labelColour.Ok() ?
323 params->m_labelColour : win->GetForegroundColour();
324
325 wxString label( params->m_labelText );
326
327 dc.SetFont(font);
328 dc.SetTextForeground(clr);
329 dc.SetBackgroundMode(wxTRANSPARENT);
330
331 int tw, th, td, x, y;
332 dc.GetTextExtent( label, &tw, &th, &td);
333 y = rect.y + wxMax(0, (rect.height - (th+td)) / 2);
334
335 // truncate and add an ellipsis (...) if the text is too wide.
336 int targetWidth = rect.width - arrowSpace - bmpWidth - 2*margin;
337 if ( tw > targetWidth )
338 {
339 int ellipsisWidth;
340 dc.GetTextExtent( wxT("..."), &ellipsisWidth, NULL);
341 do {
342 label.Truncate( label.length() - 1 );
343 dc.GetTextExtent( label, &tw, &th);
344 } while (tw + ellipsisWidth > targetWidth && label.length() );
345 label.append( wxT("...") );
346 tw += ellipsisWidth;
347 }
348
349 switch (params->m_labelAlignment)
350 {
351 default:
352 case wxALIGN_LEFT:
353 x = rect.x + margin;
354 break;
355 case wxALIGN_CENTER:
356 x = rect.x + wxMax(0, (rect.width - arrowSpace - tw - bmpWidth)/2);
357 break;
358 case wxALIGN_RIGHT:
359 x = rect.x + wxMax(0, rect.width - arrowSpace - margin - tw - bmpWidth);
360 break;
361 }
362
363 dc.DrawText(label, x, y);
364 txtEnd = x + tw + 2;
365 }
366
367 // draw the bitmap if there is one
368 if ( params && params->m_labelBitmap.Ok() )
369 {
370 int w, h, x, y;
371 w = params->m_labelBitmap.GetWidth();
372 h = params->m_labelBitmap.GetHeight();
373
374 y = rect.y + wxMax(1, (rect.height - h) / 2);
375
376 // if there is a text label, then put the bitmap at the end of the label
377 if ( txtEnd != 0 )
378 {
379 x = txtEnd;
380 }
381 // otherwise use the alignment flags
382 else
383 {
384 switch (params->m_labelAlignment)
385 {
386 default:
387 case wxALIGN_LEFT:
388 x = rect.x + margin;
389 break;
390 case wxALIGN_CENTER:
391 x = rect.x + wxMax(1, (rect.width - arrowSpace - w)/2);
392 break;
393 case wxALIGN_RIGHT:
394 x = rect.x + wxMax(1, rect.width - arrowSpace - margin - w);
395 break;
396 }
397 }
398 dc.DrawBitmap(params->m_labelBitmap, x, y, true);
399 }
400 }
401
402
403 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow *win)
404 {
405 // Copied and adapted from src/generic/listctrl.cpp
406 const int HEADER_OFFSET_Y = 1;
407 const int EXTRA_HEIGHT = 4;
408
409 int w=0, h=14, d=0;
410 if (win)
411 win->GetTextExtent(wxT("Hg"), &w, &h, &d);
412
413 return h + d + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
414 }
415
416
417 // draw the plus or minus sign
418 void
419 wxRendererGeneric::DrawTreeItemButton(wxWindow * WXUNUSED(win),
420 wxDC& dc,
421 const wxRect& rect,
422 int flags)
423 {
424 // store settings
425 wxDCPenChanger penChanger(dc, *wxGREY_PEN);
426 wxDCBrushChanger brushChanger(dc, *wxWHITE_BRUSH);
427
428 dc.DrawRectangle(rect);
429
430 // black lines
431 const wxCoord xMiddle = rect.x + rect.width/2;
432 const wxCoord yMiddle = rect.y + rect.height/2;
433
434 // half of the length of the horz lines in "-" and "+"
435 const wxCoord halfWidth = rect.width/2 - 2;
436 dc.SetPen(*wxBLACK_PEN);
437 dc.DrawLine(xMiddle - halfWidth, yMiddle,
438 xMiddle + halfWidth + 1, yMiddle);
439
440 if ( !(flags & wxCONTROL_EXPANDED) )
441 {
442 // turn "-" into "+"
443 const wxCoord halfHeight = rect.height/2 - 2;
444 dc.DrawLine(xMiddle, yMiddle - halfHeight,
445 xMiddle, yMiddle + halfHeight + 1);
446 }
447 }
448
449 // ----------------------------------------------------------------------------
450 // sash drawing
451 // ----------------------------------------------------------------------------
452
453 wxSplitterRenderParams
454 wxRendererGeneric::GetSplitterParams(const wxWindow *win)
455 {
456 // see below
457 wxCoord sashWidth,
458 border;
459
460 if ( win->HasFlag(wxSP_3DSASH) )
461 sashWidth = 7;
462 else if ( win->HasFlag(wxSP_NOSASH) )
463 sashWidth = 0;
464 else // no 3D effect
465 sashWidth = 3;
466
467 if ( win->HasFlag(wxSP_3DBORDER) )
468 border = 2;
469 else // no 3D effect
470 border = 0;
471
472 return wxSplitterRenderParams(sashWidth, border, false);
473 }
474
475 void
476 wxRendererGeneric::DrawSplitterBorder(wxWindow *win,
477 wxDC& dc,
478 const wxRect& rectOrig,
479 int WXUNUSED(falgs))
480 {
481 if ( win->HasFlag(wxSP_3DBORDER) )
482 {
483 wxRect rect = rectOrig;
484 DrawShadedRect(dc, &rect, m_penDarkGrey, m_penHighlight);
485 DrawShadedRect(dc, &rect, m_penBlack, m_penLightGrey);
486 }
487 }
488
489 void
490 wxRendererGeneric::DrawSplitterSash(wxWindow *win,
491 wxDC& dcReal,
492 const wxSize& sizeReal,
493 wxCoord position,
494 wxOrientation orient,
495 int WXUNUSED(flags))
496 {
497 // to avoid duplicating the same code for horizontal and vertical sashes,
498 // simply mirror the DC instead if needed (i.e. if horz splitter)
499 wxMirrorDC dc(dcReal, orient != wxVERTICAL);
500 wxSize size = dc.Reflect(sizeReal);
501
502
503 // we draw a Win32-like grey sash with possible 3D border here:
504 //
505 // ---- this is position
506 // /
507 // v
508 // dWGGGDd
509 // GWGGGDB
510 // GWGGGDB where G is light grey (face)
511 // GWGGGDB W white (light)
512 // GWGGGDB D dark grey (shadow)
513 // GWGGGDB B black (dark shadow)
514 // GWGGGDB
515 // GWGGGDB and lower letters are our border (already drawn)
516 // GWGGGDB
517 // wWGGGDd
518 //
519 // only the middle 3 columns are drawn unless wxSP_3D is specified
520
521 const wxCoord h = size.y;
522 wxCoord offset = 0;
523
524 // If we're drawing the border, draw the sash 3d lines shorter
525 if ( win->HasFlag(wxSP_3DBORDER) )
526 {
527 offset = 1;
528 }
529
530 dc.SetPen(*wxTRANSPARENT_PEN);
531 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));
532
533 if ( win->HasFlag(wxSP_3DSASH) )
534 {
535 // Draw the 3D sash
536 dc.DrawRectangle(position + 2, 0, 3, h);
537
538 dc.SetPen(m_penLightGrey);
539 dc.DrawLine(position, offset, position, h - offset);
540
541 dc.SetPen(m_penHighlight);
542 dc.DrawLine(position + 1, 0, position + 1, h);
543
544 dc.SetPen(m_penDarkGrey);
545 dc.DrawLine(position + 5, 0, position + 5, h);
546
547 dc.SetPen(m_penBlack);
548 dc.DrawLine(position + 6, offset, position + 6, h - offset);
549 }
550 else
551 {
552 // Draw a flat sash
553 dc.DrawRectangle(position, 0, 3, h);
554 }
555 }
556
557 // ----------------------------------------------------------------------------
558 // button drawing
559 // ----------------------------------------------------------------------------
560
561 void
562 wxRendererGeneric::DrawComboBoxDropButton(wxWindow *win,
563 wxDC& dc,
564 const wxRect& rect,
565 int flags)
566 {
567 DrawPushButton(win,dc,rect,flags);
568 DrawDropArrow(win,dc,rect,flags);
569 }
570
571 void
572 wxRendererGeneric::DrawDropArrow(wxWindow *win,
573 wxDC& dc,
574 const wxRect& rect,
575 int WXUNUSED(flags))
576 {
577 // This generic implementation should be good
578 // enough for Windows platforms (including XP).
579
580 int arrowHalf = rect.width/5;
581 int rectMid = rect.width / 2;
582 int arrowTopY = (rect.height/2) - (arrowHalf/2);
583
584 // This should always result in arrow with odd width.
585 wxPoint pt[] =
586 {
587 wxPoint(rectMid - arrowHalf, arrowTopY),
588 wxPoint(rectMid + arrowHalf, arrowTopY),
589 wxPoint(rectMid, arrowTopY + arrowHalf)
590 };
591 dc.SetBrush(wxBrush(win->GetForegroundColour()));
592 dc.SetPen(wxPen(win->GetForegroundColour()));
593 dc.DrawPolygon(WXSIZEOF(pt), pt, rect.x, rect.y);
594 }
595
596 void
597 wxRendererGeneric::DrawCheckBox(wxWindow *WXUNUSED(win),
598 wxDC& dc,
599 const wxRect& rect,
600 int flags)
601 {
602 dc.SetPen(*(flags & wxCONTROL_DISABLED ? wxGREY_PEN : wxBLACK_PEN));
603 dc.SetBrush( *wxTRANSPARENT_BRUSH );
604 dc.DrawRectangle(rect);
605
606 if ( flags & wxCONTROL_CHECKED )
607 {
608 dc.DrawCheckMark(rect.Deflate(2, 2));
609 }
610 }
611
612 void
613 wxRendererGeneric::DrawPushButton(wxWindow *win,
614 wxDC& dc,
615 const wxRect& rect,
616 int flags)
617 {
618 // Don't try anything too fancy. It'll just turn out looking
619 // out-of-place on most platforms.
620 wxColour bgCol = flags & wxCONTROL_DISABLED ?
621 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) :
622 win->GetBackgroundColour();
623 dc.SetBrush(wxBrush(bgCol));
624 dc.SetPen(wxPen(bgCol));
625 dc.DrawRectangle(rect);
626 }
627
628 void
629 wxRendererGeneric::DrawItemSelectionRect(wxWindow * WXUNUSED(win),
630 wxDC& dc,
631 const wxRect& rect,
632 int flags)
633 {
634 wxBrush brush;
635 if ( flags & wxCONTROL_SELECTED )
636 {
637 if ( flags & wxCONTROL_FOCUSED )
638 {
639 brush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
640 }
641 else // !focused
642 {
643 brush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW));
644 }
645 }
646 else // !selected
647 {
648 brush = *wxTRANSPARENT_BRUSH;
649 }
650
651 dc.SetBrush(brush);
652 dc.SetPen(flags & wxCONTROL_CURRENT ? *wxBLACK_PEN : *wxTRANSPARENT_PEN);
653
654 dc.DrawRectangle( rect );
655 }
656
657
658 // ----------------------------------------------------------------------------
659 // A module to allow cleanup of generic renderer.
660 // ----------------------------------------------------------------------------
661
662 class wxGenericRendererModule: public wxModule
663 {
664 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule)
665 public:
666 wxGenericRendererModule() {}
667 bool OnInit() { return true; };
668 void OnExit() { wxRendererGeneric::Cleanup(); };
669 };
670
671 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule, wxModule)