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"
40 // ----------------------------------------------------------------------------
41 // wxRendererGeneric: our wxRendererNative implementation
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxRendererGeneric
: public wxRendererNative
49 virtual int DrawHeaderButton(wxWindow
*win
,
53 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
54 wxHeaderButtonParams
* params
= NULL
);
56 virtual int DrawHeaderButtonContents(wxWindow
*win
,
60 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
61 wxHeaderButtonParams
* params
= NULL
);
63 virtual int GetHeaderButtonHeight(wxWindow
*win
);
65 virtual void DrawTreeItemButton(wxWindow
*win
,
70 virtual void DrawSplitterBorder(wxWindow
*win
,
75 virtual void DrawSplitterSash(wxWindow
*win
,
82 virtual void DrawComboBoxDropButton(wxWindow
*win
,
87 virtual void DrawDropArrow(wxWindow
*win
,
92 virtual void DrawCheckBox(wxWindow
*win
,
97 virtual void DrawPushButton(wxWindow
*win
,
102 virtual void DrawItemSelectionRect(wxWindow
*win
,
107 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
109 virtual wxRendererVersion
GetVersion() const
111 return wxRendererVersion(wxRendererVersion::Current_Version
,
112 wxRendererVersion::Current_Age
);
116 // Cleanup by deleting standard renderer
117 static void Cleanup();
119 // Get the generic object
120 static wxRendererGeneric
* DoGetGeneric();
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
);
134 static wxRendererGeneric
* sm_rendererGeneric
;
137 // ============================================================================
138 // wxRendererGeneric implementation
139 // ============================================================================
141 // Get the generic object
142 wxRendererGeneric
* wxRendererGeneric::DoGetGeneric()
144 if (!sm_rendererGeneric
)
145 sm_rendererGeneric
= new wxRendererGeneric
;
146 return sm_rendererGeneric
;
149 // ----------------------------------------------------------------------------
150 // wxRendererGeneric creation
151 // ----------------------------------------------------------------------------
154 wxRendererNative
& wxRendererNative::GetGeneric()
156 return * wxRendererGeneric::DoGetGeneric();
159 void wxRendererGeneric::Cleanup()
161 if (sm_rendererGeneric
)
162 delete sm_rendererGeneric
;
164 sm_rendererGeneric
= NULL
;
167 wxRendererGeneric
* wxRendererGeneric::sm_rendererGeneric
= NULL
;
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
))
177 // ----------------------------------------------------------------------------
178 // wxRendererGeneric helpers
179 // ----------------------------------------------------------------------------
182 wxRendererGeneric::DrawShadedRect(wxDC
& dc
,
187 // draw the rectangle
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());
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());
203 // ----------------------------------------------------------------------------
204 // tree/list ctrl drawing
205 // ----------------------------------------------------------------------------
208 wxRendererGeneric::DrawHeaderButton(wxWindow
* win
,
212 wxHeaderSortIconType sortArrow
,
213 wxHeaderButtonParams
* params
)
215 const int CORNER
= 1;
217 const wxCoord x
= rect
.x
,
222 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
223 dc
.SetPen(*wxTRANSPARENT_PEN
);
224 dc
.DrawRectangle(rect
);
226 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
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)
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)
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 );
242 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
247 wxRendererGeneric::DrawHeaderButtonContents(wxWindow
*win
,
251 wxHeaderSortIconType sortArrow
,
252 wxHeaderButtonParams
* params
)
256 // Mark this item as selected. For the generic version we'll just draw an
258 if ( flags
& wxCONTROL_SELECTED
)
260 // draw a line at the bottom of the header button, overlaying the
261 // native hot-tracking line (on XP)
262 const int penwidth
= 3;
263 int y
= rect
.y
+ rect
.height
+ 1 - penwidth
;
264 wxColour c
= (params
&& params
->m_selectionColour
.Ok()) ?
265 params
->m_selectionColour
: wxColour(0x66, 0x66, 0x66);
266 wxPen
pen(c
, penwidth
);
267 pen
.SetCap(wxCAP_BUTT
);
269 dc
.DrawLine(rect
.x
, y
, rect
.x
+ rect
.width
, y
);
272 // Draw an up or down arrow
274 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
278 // make a rect for the arrow
281 ar
.y
+= (rect
.height
- ar
.height
)/2;
282 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
283 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
286 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
288 triPt
[0].x
= ar
.width
/ 2;
290 triPt
[1].x
= ar
.width
;
291 triPt
[1].y
= ar
.height
;
293 triPt
[2].y
= ar
.height
;
299 triPt
[1].x
= ar
.width
;
301 triPt
[2].x
= ar
.width
/ 2;
302 triPt
[2].y
= ar
.height
;
305 wxColour c
= (params
&& params
->m_arrowColour
.Ok()) ?
306 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
308 dc
.SetBrush(wxBrush(c
));
309 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
311 labelWidth
+= arrowSpace
;
313 const int margin
= 5; // number of pixels to reserve on either side of the label
317 if ( params
&& params
->m_labelBitmap
.Ok() )
318 bmpWidth
= params
->m_labelBitmap
.GetWidth() + 2;
320 labelWidth
+= bmpWidth
+ 2*margin
;
322 // Draw a label if one is given
323 if ( params
&& !params
->m_labelText
.empty() )
325 wxFont font
= params
->m_labelFont
.Ok() ?
326 params
->m_labelFont
: win
->GetFont();
327 wxColour clr
= params
->m_labelColour
.Ok() ?
328 params
->m_labelColour
: win
->GetForegroundColour();
330 wxString
label( params
->m_labelText
);
333 dc
.SetTextForeground(clr
);
334 dc
.SetBackgroundMode(wxTRANSPARENT
);
336 int tw
, th
, td
, x
, y
;
337 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
339 y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
341 // truncate and add an ellipsis (...) if the text is too wide.
342 int targetWidth
= rect
.width
- arrowSpace
- bmpWidth
- 2*margin
;
343 if ( tw
> targetWidth
)
346 dc
.GetTextExtent( wxT("..."), &ellipsisWidth
, NULL
);
348 label
.Truncate( label
.length() - 1 );
349 dc
.GetTextExtent( label
, &tw
, &th
);
350 } while (tw
+ ellipsisWidth
> targetWidth
&& label
.length() );
351 label
.append( wxT("...") );
355 switch (params
->m_labelAlignment
)
362 x
= rect
.x
+ wxMax(0, (rect
.width
- arrowSpace
- tw
- bmpWidth
)/2);
365 x
= rect
.x
+ wxMax(0, rect
.width
- arrowSpace
- margin
- tw
- bmpWidth
);
369 dc
.DrawText(label
, x
, y
);
373 // draw the bitmap if there is one
374 if ( params
&& params
->m_labelBitmap
.Ok() )
377 w
= params
->m_labelBitmap
.GetWidth();
378 h
= params
->m_labelBitmap
.GetHeight();
380 y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
382 // if there is a text label, then put the bitmap at the end of the label
387 // otherwise use the alignment flags
390 switch (params
->m_labelAlignment
)
397 x
= rect
.x
+ wxMax(1, (rect
.width
- arrowSpace
- w
)/2);
400 x
= rect
.x
+ wxMax(1, rect
.width
- arrowSpace
- margin
- w
);
404 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
410 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
412 // Copied and adapted from src/generic/listctrl.cpp
413 const int HEADER_OFFSET_Y
= 1;
414 const int EXTRA_HEIGHT
= 4;
418 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
420 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
424 // draw the plus or minus sign
426 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
432 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
433 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
435 dc
.DrawRectangle(rect
);
438 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
439 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
441 // half of the length of the horz lines in "-" and "+"
442 const wxCoord halfWidth
= rect
.width
/2 - 2;
443 dc
.SetPen(*wxBLACK_PEN
);
444 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
445 xMiddle
+ halfWidth
+ 1, yMiddle
);
447 if ( !(flags
& wxCONTROL_EXPANDED
) )
450 const wxCoord halfHeight
= rect
.height
/2 - 2;
451 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
452 xMiddle
, yMiddle
+ halfHeight
+ 1);
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
460 wxSplitterRenderParams
461 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
467 if ( win
->HasFlag(wxSP_3DSASH
) )
469 else if ( win
->HasFlag(wxSP_NOSASH
) )
474 if ( win
->HasFlag(wxSP_3DBORDER
) )
479 return wxSplitterRenderParams(sashWidth
, border
, false);
483 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
485 const wxRect
& rectOrig
,
488 if ( win
->HasFlag(wxSP_3DBORDER
) )
490 wxRect rect
= rectOrig
;
491 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
492 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
497 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
499 const wxSize
& sizeReal
,
501 wxOrientation orient
,
504 // to avoid duplicating the same code for horizontal and vertical sashes,
505 // simply mirror the DC instead if needed (i.e. if horz splitter)
506 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
507 wxSize size
= dc
.Reflect(sizeReal
);
510 // we draw a Win32-like grey sash with possible 3D border here:
512 // ---- this is position
517 // GWGGGDB where G is light grey (face)
518 // GWGGGDB W white (light)
519 // GWGGGDB D dark grey (shadow)
520 // GWGGGDB B black (dark shadow)
522 // GWGGGDB and lower letters are our border (already drawn)
526 // only the middle 3 columns are drawn unless wxSP_3D is specified
528 const wxCoord h
= size
.y
;
531 // If we're drawing the border, draw the sash 3d lines shorter
532 if ( win
->HasFlag(wxSP_3DBORDER
) )
537 dc
.SetPen(*wxTRANSPARENT_PEN
);
538 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
540 if ( win
->HasFlag(wxSP_3DSASH
) )
543 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
545 dc
.SetPen(m_penLightGrey
);
546 dc
.DrawLine(position
, offset
, position
, h
- offset
);
548 dc
.SetPen(m_penHighlight
);
549 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
551 dc
.SetPen(m_penDarkGrey
);
552 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
554 dc
.SetPen(m_penBlack
);
555 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
560 dc
.DrawRectangle(position
, 0, 3, h
);
564 // ----------------------------------------------------------------------------
566 // ----------------------------------------------------------------------------
569 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
574 DrawPushButton(win
,dc
,rect
,flags
);
575 DrawDropArrow(win
,dc
,rect
,flags
);
579 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
584 // This generic implementation should be good
585 // enough for Windows platforms (including XP).
587 int arrowHalf
= rect
.width
/5;
588 int rectMid
= rect
.width
/ 2;
589 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
591 // This should always result in arrow with odd width.
594 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
595 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
596 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
598 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
599 dc
.SetPen(wxPen(win
->GetForegroundColour()));
600 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
604 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
609 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
610 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
611 dc
.DrawRectangle(rect
);
613 if ( flags
& wxCONTROL_CHECKED
)
615 dc
.DrawCheckMark(rect
.Deflate(2, 2));
620 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
625 // Don't try anything too fancy. It'll just turn out looking
626 // out-of-place on most platforms.
627 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
628 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
629 win
->GetBackgroundColour();
630 dc
.SetBrush(wxBrush(bgCol
));
631 dc
.SetPen(wxPen(bgCol
));
632 dc
.DrawRectangle(rect
);
636 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* WXUNUSED(win
),
642 if ( flags
& wxCONTROL_SELECTED
)
644 if ( flags
& wxCONTROL_FOCUSED
)
646 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
650 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
655 brush
= *wxTRANSPARENT_BRUSH
;
659 dc
.SetPen(flags
& wxCONTROL_CURRENT
? *wxBLACK_PEN
: *wxTRANSPARENT_PEN
);
661 dc
.DrawRectangle( rect
);
665 // ----------------------------------------------------------------------------
666 // A module to allow cleanup of generic renderer.
667 // ----------------------------------------------------------------------------
669 class wxGenericRendererModule
: public wxModule
671 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
673 wxGenericRendererModule() {}
674 bool OnInit() { return true; }
675 void OnExit() { wxRendererGeneric::Cleanup(); }
678 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)