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