1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/renderg.cpp
3 // Purpose: generic implementation of wxRendererNative (for any platform)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/renderer.h"
30 #include "wx/string.h"
32 #include "wx/settings.h"
33 #include "wx/gdicmn.h"
34 #include "wx/module.h"
37 #include "wx/splitter.h"
38 #include "wx/dcmirror.h"
41 #include "wx/osx/private.h"
44 // ----------------------------------------------------------------------------
45 // wxRendererGeneric: our wxRendererNative implementation
46 // ----------------------------------------------------------------------------
48 class WXDLLEXPORT wxRendererGeneric
: public wxRendererNative
53 virtual int DrawHeaderButton(wxWindow
*win
,
57 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
58 wxHeaderButtonParams
* params
= NULL
);
60 virtual int DrawHeaderButtonContents(wxWindow
*win
,
64 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
65 wxHeaderButtonParams
* params
= NULL
);
67 virtual int GetHeaderButtonHeight(wxWindow
*win
);
69 virtual void DrawTreeItemButton(wxWindow
*win
,
74 virtual void DrawSplitterBorder(wxWindow
*win
,
79 virtual void DrawSplitterSash(wxWindow
*win
,
86 virtual void DrawComboBoxDropButton(wxWindow
*win
,
91 virtual void DrawDropArrow(wxWindow
*win
,
96 virtual void DrawCheckBox(wxWindow
*win
,
101 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
103 virtual void DrawPushButton(wxWindow
*win
,
108 virtual void DrawItemSelectionRect(wxWindow
*win
,
113 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
115 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
117 virtual wxRendererVersion
GetVersion() const
119 return wxRendererVersion(wxRendererVersion::Current_Version
,
120 wxRendererVersion::Current_Age
);
124 // Cleanup by deleting standard renderer
125 static void Cleanup();
127 // Get the generic object
128 static wxRendererGeneric
* DoGetGeneric();
131 // draw the rectange using the first pen for the left and top sides and
132 // the second one for the bottom and right ones
133 void DrawShadedRect(wxDC
& dc
, wxRect
*rect
,
134 const wxPen
& pen1
, const wxPen
& pen2
);
142 static wxRendererGeneric
* sm_rendererGeneric
;
145 // ============================================================================
146 // wxRendererGeneric implementation
147 // ============================================================================
149 // Get the generic object
150 wxRendererGeneric
* wxRendererGeneric::DoGetGeneric()
152 if (!sm_rendererGeneric
)
153 sm_rendererGeneric
= new wxRendererGeneric
;
154 return sm_rendererGeneric
;
157 // ----------------------------------------------------------------------------
158 // wxRendererGeneric creation
159 // ----------------------------------------------------------------------------
162 wxRendererNative
& wxRendererNative::GetGeneric()
164 return * wxRendererGeneric::DoGetGeneric();
167 void wxRendererGeneric::Cleanup()
169 if (sm_rendererGeneric
)
170 delete sm_rendererGeneric
;
172 sm_rendererGeneric
= NULL
;
175 wxRendererGeneric
* wxRendererGeneric::sm_rendererGeneric
= NULL
;
177 wxRendererGeneric::wxRendererGeneric()
178 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
)),
179 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)),
180 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)),
181 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
))
185 // ----------------------------------------------------------------------------
186 // wxRendererGeneric helpers
187 // ----------------------------------------------------------------------------
190 wxRendererGeneric::DrawShadedRect(wxDC
& dc
,
195 // draw the rectangle
197 dc
.DrawLine(rect
->GetLeft(), rect
->GetTop(),
198 rect
->GetLeft(), rect
->GetBottom());
199 dc
.DrawLine(rect
->GetLeft() + 1, rect
->GetTop(),
200 rect
->GetRight(), rect
->GetTop());
202 dc
.DrawLine(rect
->GetRight(), rect
->GetTop(),
203 rect
->GetRight(), rect
->GetBottom());
204 dc
.DrawLine(rect
->GetLeft(), rect
->GetBottom(),
205 rect
->GetRight() + 1, rect
->GetBottom());
211 // ----------------------------------------------------------------------------
212 // tree/list ctrl drawing
213 // ----------------------------------------------------------------------------
216 wxRendererGeneric::DrawHeaderButton(wxWindow
* win
,
220 wxHeaderSortIconType sortArrow
,
221 wxHeaderButtonParams
* params
)
223 const wxCoord x
= rect
.x
,
228 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
229 dc
.SetPen(*wxTRANSPARENT_PEN
);
230 dc
.DrawRectangle(rect
);
232 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
234 dc
.SetPen(m_penBlack
);
235 dc
.DrawLine( x
+w
-1, y
, x
+w
-1, y
+h
); // right (outer)
236 dc
.DrawLine( x
, y
+h
-1, x
+w
, y
+h
-1 ); // bottom (outer)
238 dc
.SetPen(m_penDarkGrey
);
239 dc
.DrawLine( x
+w
-2, y
+1, x
+w
-2, y
+h
-1 ); // right (inner)
240 dc
.DrawLine( x
+1, y
+h
-2, x
+w
-1, y
+h
-2 ); // bottom (inner)
242 dc
.SetPen(m_penHighlight
);
243 dc
.DrawLine( x
, y
, x
, y
+h
-1 ); // left (outer)
244 dc
.DrawLine( x
, y
, x
+w
-1, y
); // top (outer)
246 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
251 wxRendererGeneric::DrawHeaderButtonContents(wxWindow
*win
,
255 wxHeaderSortIconType sortArrow
,
256 wxHeaderButtonParams
* params
)
260 // Mark this item as selected. For the generic version we'll just draw an
262 if ( flags
& wxCONTROL_SELECTED
)
264 // draw a line at the bottom of the header button, overlaying the
265 // native hot-tracking line (on XP)
266 const int penwidth
= 3;
267 int y
= rect
.y
+ rect
.height
+ 1 - penwidth
;
268 wxColour c
= (params
&& params
->m_selectionColour
.Ok()) ?
269 params
->m_selectionColour
: wxColour(0x66, 0x66, 0x66);
270 wxPen
pen(c
, penwidth
);
271 pen
.SetCap(wxCAP_BUTT
);
273 dc
.DrawLine(rect
.x
, y
, rect
.x
+ rect
.width
, y
);
276 // Draw an up or down arrow
278 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
282 // make a rect for the arrow
285 ar
.y
+= (rect
.height
- ar
.height
)/2;
286 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
287 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
290 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
292 triPt
[0].x
= ar
.width
/ 2;
294 triPt
[1].x
= ar
.width
;
295 triPt
[1].y
= ar
.height
;
297 triPt
[2].y
= ar
.height
;
303 triPt
[1].x
= ar
.width
;
305 triPt
[2].x
= ar
.width
/ 2;
306 triPt
[2].y
= ar
.height
;
309 wxColour c
= (params
&& params
->m_arrowColour
.Ok()) ?
310 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
312 dc
.SetBrush(wxBrush(c
));
313 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
315 labelWidth
+= arrowSpace
;
317 const int margin
= 5; // number of pixels to reserve on either side of the label
321 if ( params
&& params
->m_labelBitmap
.Ok() )
322 bmpWidth
= params
->m_labelBitmap
.GetWidth() + 2;
324 labelWidth
+= bmpWidth
+ 2*margin
;
326 // Draw a label if one is given
327 if ( params
&& !params
->m_labelText
.empty() )
329 wxFont font
= params
->m_labelFont
.Ok() ?
330 params
->m_labelFont
: win
->GetFont();
331 wxColour clr
= params
->m_labelColour
.Ok() ?
332 params
->m_labelColour
: win
->GetForegroundColour();
334 wxString
label( params
->m_labelText
);
337 dc
.SetTextForeground(clr
);
338 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
340 int tw
, th
, td
, x
, y
;
341 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
343 y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
345 // truncate and add an ellipsis (...) if the text is too wide.
346 int targetWidth
= rect
.width
- arrowSpace
- bmpWidth
- 2*margin
;
347 if ( tw
> targetWidth
)
350 dc
.GetTextExtent( wxT("..."), &ellipsisWidth
, NULL
);
352 label
.Truncate( label
.length() - 1 );
353 dc
.GetTextExtent( label
, &tw
, &th
);
354 } while (tw
+ ellipsisWidth
> targetWidth
&& label
.length() );
355 label
.append( wxT("...") );
359 switch (params
->m_labelAlignment
)
366 x
= rect
.x
+ wxMax(0, (rect
.width
- arrowSpace
- tw
- bmpWidth
)/2);
369 x
= rect
.x
+ wxMax(0, rect
.width
- arrowSpace
- margin
- tw
- bmpWidth
);
373 dc
.DrawText(label
, x
, y
);
377 // draw the bitmap if there is one
378 if ( params
&& params
->m_labelBitmap
.Ok() )
381 w
= params
->m_labelBitmap
.GetWidth();
382 h
= params
->m_labelBitmap
.GetHeight();
384 y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
386 // if there is a text label, then put the bitmap at the end of the label
391 // otherwise use the alignment flags
394 switch (params
->m_labelAlignment
)
401 x
= rect
.x
+ wxMax(1, (rect
.width
- arrowSpace
- w
)/2);
404 x
= rect
.x
+ wxMax(1, rect
.width
- arrowSpace
- margin
- w
);
408 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
414 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
416 // Copied and adapted from src/generic/listctrl.cpp
417 const int HEADER_OFFSET_Y
= 1;
418 const int EXTRA_HEIGHT
= 4;
422 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
424 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
428 // draw the plus or minus sign
430 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
436 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
437 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
439 dc
.DrawRectangle(rect
);
442 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
443 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
445 // half of the length of the horz lines in "-" and "+"
446 const wxCoord halfWidth
= rect
.width
/2 - 2;
447 dc
.SetPen(*wxBLACK_PEN
);
448 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
449 xMiddle
+ halfWidth
+ 1, yMiddle
);
451 if ( !(flags
& wxCONTROL_EXPANDED
) )
454 const wxCoord halfHeight
= rect
.height
/2 - 2;
455 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
456 xMiddle
, yMiddle
+ halfHeight
+ 1);
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
464 wxSplitterRenderParams
465 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
471 if ( win
->HasFlag(wxSP_3DSASH
) )
473 else if ( win
->HasFlag(wxSP_NOSASH
) )
478 if ( win
->HasFlag(wxSP_3DBORDER
) )
483 return wxSplitterRenderParams(sashWidth
, border
, false);
487 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
489 const wxRect
& rectOrig
,
492 if ( win
->HasFlag(wxSP_3DBORDER
) )
494 wxRect rect
= rectOrig
;
495 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
496 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
501 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
503 const wxSize
& sizeReal
,
505 wxOrientation orient
,
508 // to avoid duplicating the same code for horizontal and vertical sashes,
509 // simply mirror the DC instead if needed (i.e. if horz splitter)
510 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
511 wxSize size
= dc
.Reflect(sizeReal
);
514 // we draw a Win32-like grey sash with possible 3D border here:
516 // ---- this is position
521 // GWGGGDB where G is light grey (face)
522 // GWGGGDB W white (light)
523 // GWGGGDB D dark grey (shadow)
524 // GWGGGDB B black (dark shadow)
526 // GWGGGDB and lower letters are our border (already drawn)
530 // only the middle 3 columns are drawn unless wxSP_3D is specified
532 const wxCoord h
= size
.y
;
535 // If we're drawing the border, draw the sash 3d lines shorter
536 if ( win
->HasFlag(wxSP_3DBORDER
) )
541 dc
.SetPen(*wxTRANSPARENT_PEN
);
542 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
544 if ( win
->HasFlag(wxSP_3DSASH
) )
547 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
549 dc
.SetPen(m_penLightGrey
);
550 dc
.DrawLine(position
, offset
, position
, h
- offset
);
552 dc
.SetPen(m_penHighlight
);
553 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
555 dc
.SetPen(m_penDarkGrey
);
556 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
558 dc
.SetPen(m_penBlack
);
559 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
564 dc
.DrawRectangle(position
, 0, 3, h
);
568 // ----------------------------------------------------------------------------
570 // ----------------------------------------------------------------------------
573 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
578 DrawPushButton(win
,dc
,rect
,flags
);
579 DrawDropArrow(win
,dc
,rect
,flags
);
583 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
588 // This generic implementation should be good
589 // enough for Windows platforms (including XP).
591 int arrowHalf
= rect
.width
/5;
592 int rectMid
= rect
.width
/ 2;
593 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
595 // This should always result in arrow with odd width.
598 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
599 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
600 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
602 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
603 dc
.SetPen(wxPen(win
->GetForegroundColour()));
604 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
608 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
613 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
614 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
615 dc
.DrawRectangle(rect
);
617 if ( flags
& wxCONTROL_CHECKED
)
619 dc
.DrawCheckMark(rect
.Deflate(2, 2));
623 wxSize
wxRendererGeneric::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
625 return wxSize(16, 16);
629 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
634 // Don't try anything too fancy. It'll just turn out looking
635 // out-of-place on most platforms.
636 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
637 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
638 win
->GetBackgroundColour();
639 dc
.SetBrush(wxBrush(bgCol
));
640 dc
.SetPen(wxPen(bgCol
));
641 dc
.DrawRectangle(rect
);
646 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* win
,
651 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* WXUNUSED(win
),
658 if ( flags
& wxCONTROL_SELECTED
)
660 if ( flags
& wxCONTROL_FOCUSED
)
662 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
666 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
671 brush
= *wxTRANSPARENT_BRUSH
;
675 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
)
676 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
677 && IsControlActive( (ControlRef
)win
->GetHandle() )
680 dc
.SetPen( *wxBLACK_PEN
);
682 dc
.SetPen( *wxTRANSPARENT_PEN
);
684 dc
.DrawRectangle( rect
);
688 wxRendererGeneric::DrawFocusRect(wxWindow
* WXUNUSED(win
), wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(flags
))
690 // draw the pixels manually because the "dots" in wxPen with wxDOT style
691 // may be short traits and not really dots
693 // note that to behave in the same manner as DrawRect(), we must exclude
694 // the bottom and right borders from the rectangle
695 wxCoord x1
= rect
.GetLeft(),
697 x2
= rect
.GetRight(),
698 y2
= rect
.GetBottom();
700 dc
.SetPen(m_penBlack
);
703 dc
.SetLogicalFunction(wxCOPY
);
705 // this seems to be closer than what Windows does than wxINVERT although
706 // I'm still not sure if it's correct
707 dc
.SetLogicalFunction(wxAND_REVERSE
);
711 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
712 dc
.DrawPoint(z
, rect
.GetTop());
714 wxCoord shift
= z
== x2
? 0 : 1;
715 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
718 shift
= z
== y2
? 0 : 1;
719 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
722 shift
= z
== x1
? 0 : 1;
723 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
726 dc
.SetLogicalFunction(wxCOPY
);
729 // ----------------------------------------------------------------------------
730 // A module to allow cleanup of generic renderer.
731 // ----------------------------------------------------------------------------
733 class wxGenericRendererModule
: public wxModule
735 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
737 wxGenericRendererModule() {}
738 bool OnInit() { return true; }
739 void OnExit() { wxRendererGeneric::Cleanup(); }
742 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)