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/mac/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 void DrawPushButton(wxWindow
*win
,
106 virtual void DrawItemSelectionRect(wxWindow
*win
,
111 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
113 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
115 virtual wxRendererVersion
GetVersion() const
117 return wxRendererVersion(wxRendererVersion::Current_Version
,
118 wxRendererVersion::Current_Age
);
122 // Cleanup by deleting standard renderer
123 static void Cleanup();
125 // Get the generic object
126 static wxRendererGeneric
* DoGetGeneric();
129 // draw the rectange using the first pen for the left and top sides and
130 // the second one for the bottom and right ones
131 void DrawShadedRect(wxDC
& dc
, wxRect
*rect
,
132 const wxPen
& pen1
, const wxPen
& pen2
);
140 static wxRendererGeneric
* sm_rendererGeneric
;
143 // ============================================================================
144 // wxRendererGeneric implementation
145 // ============================================================================
147 // Get the generic object
148 wxRendererGeneric
* wxRendererGeneric::DoGetGeneric()
150 if (!sm_rendererGeneric
)
151 sm_rendererGeneric
= new wxRendererGeneric
;
152 return sm_rendererGeneric
;
155 // ----------------------------------------------------------------------------
156 // wxRendererGeneric creation
157 // ----------------------------------------------------------------------------
160 wxRendererNative
& wxRendererNative::GetGeneric()
162 return * wxRendererGeneric::DoGetGeneric();
165 void wxRendererGeneric::Cleanup()
167 if (sm_rendererGeneric
)
168 delete sm_rendererGeneric
;
170 sm_rendererGeneric
= NULL
;
173 wxRendererGeneric
* wxRendererGeneric::sm_rendererGeneric
= NULL
;
175 wxRendererGeneric::wxRendererGeneric()
176 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
)),
177 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)),
178 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)),
179 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
))
183 // ----------------------------------------------------------------------------
184 // wxRendererGeneric helpers
185 // ----------------------------------------------------------------------------
188 wxRendererGeneric::DrawShadedRect(wxDC
& dc
,
193 // draw the rectangle
195 dc
.DrawLine(rect
->GetLeft(), rect
->GetTop(),
196 rect
->GetLeft(), rect
->GetBottom());
197 dc
.DrawLine(rect
->GetLeft() + 1, rect
->GetTop(),
198 rect
->GetRight(), rect
->GetTop());
200 dc
.DrawLine(rect
->GetRight(), rect
->GetTop(),
201 rect
->GetRight(), rect
->GetBottom());
202 dc
.DrawLine(rect
->GetLeft(), rect
->GetBottom(),
203 rect
->GetRight() + 1, rect
->GetBottom());
209 // ----------------------------------------------------------------------------
210 // tree/list ctrl drawing
211 // ----------------------------------------------------------------------------
214 wxRendererGeneric::DrawHeaderButton(wxWindow
* win
,
218 wxHeaderSortIconType sortArrow
,
219 wxHeaderButtonParams
* params
)
221 const wxCoord x
= rect
.x
,
226 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
227 dc
.SetPen(*wxTRANSPARENT_PEN
);
228 dc
.DrawRectangle(rect
);
230 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
232 dc
.SetPen(m_penBlack
);
233 dc
.DrawLine( x
+w
-1, y
, x
+w
-1, y
+h
); // right (outer)
234 dc
.DrawLine( x
, y
+h
-1, x
+w
, y
+h
-1 ); // bottom (outer)
236 dc
.SetPen(m_penDarkGrey
);
237 dc
.DrawLine( x
+w
-2, y
+1, x
+w
-2, y
+h
-1 ); // right (inner)
238 dc
.DrawLine( x
+1, y
+h
-2, x
+w
-1, y
+h
-2 ); // bottom (inner)
240 dc
.SetPen(m_penHighlight
);
241 dc
.DrawLine( x
, y
, x
, y
+h
-1 ); // left (outer)
242 dc
.DrawLine( x
, y
, x
+w
-1, y
); // top (outer)
244 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
249 wxRendererGeneric::DrawHeaderButtonContents(wxWindow
*win
,
253 wxHeaderSortIconType sortArrow
,
254 wxHeaderButtonParams
* params
)
258 // Mark this item as selected. For the generic version we'll just draw an
260 if ( flags
& wxCONTROL_SELECTED
)
262 // draw a line at the bottom of the header button, overlaying the
263 // native hot-tracking line (on XP)
264 const int penwidth
= 3;
265 int y
= rect
.y
+ rect
.height
+ 1 - penwidth
;
266 wxColour c
= (params
&& params
->m_selectionColour
.Ok()) ?
267 params
->m_selectionColour
: wxColour(0x66, 0x66, 0x66);
268 wxPen
pen(c
, penwidth
);
269 pen
.SetCap(wxCAP_BUTT
);
271 dc
.DrawLine(rect
.x
, y
, rect
.x
+ rect
.width
, y
);
274 // Draw an up or down arrow
276 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
280 // make a rect for the arrow
283 ar
.y
+= (rect
.height
- ar
.height
)/2;
284 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
285 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
288 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
290 triPt
[0].x
= ar
.width
/ 2;
292 triPt
[1].x
= ar
.width
;
293 triPt
[1].y
= ar
.height
;
295 triPt
[2].y
= ar
.height
;
301 triPt
[1].x
= ar
.width
;
303 triPt
[2].x
= ar
.width
/ 2;
304 triPt
[2].y
= ar
.height
;
307 wxColour c
= (params
&& params
->m_arrowColour
.Ok()) ?
308 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
310 dc
.SetBrush(wxBrush(c
));
311 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
313 labelWidth
+= arrowSpace
;
315 const int margin
= 5; // number of pixels to reserve on either side of the label
319 if ( params
&& params
->m_labelBitmap
.Ok() )
320 bmpWidth
= params
->m_labelBitmap
.GetWidth() + 2;
322 labelWidth
+= bmpWidth
+ 2*margin
;
324 // Draw a label if one is given
325 if ( params
&& !params
->m_labelText
.empty() )
327 wxFont font
= params
->m_labelFont
.Ok() ?
328 params
->m_labelFont
: win
->GetFont();
329 wxColour clr
= params
->m_labelColour
.Ok() ?
330 params
->m_labelColour
: win
->GetForegroundColour();
332 wxString
label( params
->m_labelText
);
335 dc
.SetTextForeground(clr
);
336 dc
.SetBackgroundMode(wxTRANSPARENT
);
338 int tw
, th
, td
, x
, y
;
339 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
341 y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
343 // truncate and add an ellipsis (...) if the text is too wide.
344 int targetWidth
= rect
.width
- arrowSpace
- bmpWidth
- 2*margin
;
345 if ( tw
> targetWidth
)
348 dc
.GetTextExtent( wxT("..."), &ellipsisWidth
, NULL
);
350 label
.Truncate( label
.length() - 1 );
351 dc
.GetTextExtent( label
, &tw
, &th
);
352 } while (tw
+ ellipsisWidth
> targetWidth
&& label
.length() );
353 label
.append( wxT("...") );
357 switch (params
->m_labelAlignment
)
364 x
= rect
.x
+ wxMax(0, (rect
.width
- arrowSpace
- tw
- bmpWidth
)/2);
367 x
= rect
.x
+ wxMax(0, rect
.width
- arrowSpace
- margin
- tw
- bmpWidth
);
371 dc
.DrawText(label
, x
, y
);
375 // draw the bitmap if there is one
376 if ( params
&& params
->m_labelBitmap
.Ok() )
379 w
= params
->m_labelBitmap
.GetWidth();
380 h
= params
->m_labelBitmap
.GetHeight();
382 y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
384 // if there is a text label, then put the bitmap at the end of the label
389 // otherwise use the alignment flags
392 switch (params
->m_labelAlignment
)
399 x
= rect
.x
+ wxMax(1, (rect
.width
- arrowSpace
- w
)/2);
402 x
= rect
.x
+ wxMax(1, rect
.width
- arrowSpace
- margin
- w
);
406 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
412 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
414 // Copied and adapted from src/generic/listctrl.cpp
415 const int HEADER_OFFSET_Y
= 1;
416 const int EXTRA_HEIGHT
= 4;
420 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
422 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
426 // draw the plus or minus sign
428 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
434 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
435 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
437 dc
.DrawRectangle(rect
);
440 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
441 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
443 // half of the length of the horz lines in "-" and "+"
444 const wxCoord halfWidth
= rect
.width
/2 - 2;
445 dc
.SetPen(*wxBLACK_PEN
);
446 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
447 xMiddle
+ halfWidth
+ 1, yMiddle
);
449 if ( !(flags
& wxCONTROL_EXPANDED
) )
452 const wxCoord halfHeight
= rect
.height
/2 - 2;
453 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
454 xMiddle
, yMiddle
+ halfHeight
+ 1);
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 wxSplitterRenderParams
463 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
469 if ( win
->HasFlag(wxSP_3DSASH
) )
471 else if ( win
->HasFlag(wxSP_NOSASH
) )
476 if ( win
->HasFlag(wxSP_3DBORDER
) )
481 return wxSplitterRenderParams(sashWidth
, border
, false);
485 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
487 const wxRect
& rectOrig
,
490 if ( win
->HasFlag(wxSP_3DBORDER
) )
492 wxRect rect
= rectOrig
;
493 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
494 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
499 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
501 const wxSize
& sizeReal
,
503 wxOrientation orient
,
506 // to avoid duplicating the same code for horizontal and vertical sashes,
507 // simply mirror the DC instead if needed (i.e. if horz splitter)
508 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
509 wxSize size
= dc
.Reflect(sizeReal
);
512 // we draw a Win32-like grey sash with possible 3D border here:
514 // ---- this is position
519 // GWGGGDB where G is light grey (face)
520 // GWGGGDB W white (light)
521 // GWGGGDB D dark grey (shadow)
522 // GWGGGDB B black (dark shadow)
524 // GWGGGDB and lower letters are our border (already drawn)
528 // only the middle 3 columns are drawn unless wxSP_3D is specified
530 const wxCoord h
= size
.y
;
533 // If we're drawing the border, draw the sash 3d lines shorter
534 if ( win
->HasFlag(wxSP_3DBORDER
) )
539 dc
.SetPen(*wxTRANSPARENT_PEN
);
540 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
542 if ( win
->HasFlag(wxSP_3DSASH
) )
545 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
547 dc
.SetPen(m_penLightGrey
);
548 dc
.DrawLine(position
, offset
, position
, h
- offset
);
550 dc
.SetPen(m_penHighlight
);
551 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
553 dc
.SetPen(m_penDarkGrey
);
554 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
556 dc
.SetPen(m_penBlack
);
557 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
562 dc
.DrawRectangle(position
, 0, 3, h
);
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
571 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
576 DrawPushButton(win
,dc
,rect
,flags
);
577 DrawDropArrow(win
,dc
,rect
,flags
);
581 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
586 // This generic implementation should be good
587 // enough for Windows platforms (including XP).
589 int arrowHalf
= rect
.width
/5;
590 int rectMid
= rect
.width
/ 2;
591 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
593 // This should always result in arrow with odd width.
596 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
597 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
598 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
600 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
601 dc
.SetPen(wxPen(win
->GetForegroundColour()));
602 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
606 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
611 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
612 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
613 dc
.DrawRectangle(rect
);
615 if ( flags
& wxCONTROL_CHECKED
)
617 dc
.DrawCheckMark(rect
.Deflate(2, 2));
622 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
627 // Don't try anything too fancy. It'll just turn out looking
628 // out-of-place on most platforms.
629 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
630 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
631 win
->GetBackgroundColour();
632 dc
.SetBrush(wxBrush(bgCol
));
633 dc
.SetPen(wxPen(bgCol
));
634 dc
.DrawRectangle(rect
);
639 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* win
,
644 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* WXUNUSED(win
),
651 if ( flags
& wxCONTROL_SELECTED
)
653 if ( flags
& wxCONTROL_FOCUSED
)
655 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
659 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
664 brush
= *wxTRANSPARENT_BRUSH
;
668 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
)
669 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
670 && IsControlActive( (ControlRef
)win
->GetHandle() )
673 dc
.SetPen( *wxBLACK_PEN
);
675 dc
.SetPen( *wxTRANSPARENT_PEN
);
677 dc
.DrawRectangle( rect
);
681 wxRendererGeneric::DrawFocusRect(wxWindow
* WXUNUSED(win
), wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(flags
))
683 // draw the pixels manually because the "dots" in wxPen with wxDOT style
684 // may be short traits and not really dots
686 // note that to behave in the same manner as DrawRect(), we must exclude
687 // the bottom and right borders from the rectangle
688 wxCoord x1
= rect
.GetLeft(),
690 x2
= rect
.GetRight(),
691 y2
= rect
.GetBottom();
693 dc
.SetPen(m_penBlack
);
696 dc
.SetLogicalFunction(wxCOPY
);
698 // this seems to be closer than what Windows does than wxINVERT although
699 // I'm still not sure if it's correct
700 dc
.SetLogicalFunction(wxAND_REVERSE
);
704 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
705 dc
.DrawPoint(z
, rect
.GetTop());
707 wxCoord shift
= z
== x2
? 0 : 1;
708 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
711 shift
= z
== y2
? 0 : 1;
712 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
715 shift
= z
== x1
? 0 : 1;
716 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
719 dc
.SetLogicalFunction(wxCOPY
);
722 // ----------------------------------------------------------------------------
723 // A module to allow cleanup of generic renderer.
724 // ----------------------------------------------------------------------------
726 class wxGenericRendererModule
: public wxModule
728 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
730 wxGenericRendererModule() {}
731 bool OnInit() { return true; }
732 void OnExit() { wxRendererGeneric::Cleanup(); }
735 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)