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 void DrawChoice(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
117 virtual void DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
119 virtual void DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
121 virtual void DrawRadioBitmap(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
123 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
124 virtual void DrawTitleBarBitmap(wxWindow
*win
,
127 wxTitleBarButton button
,
129 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
131 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
133 virtual wxRendererVersion
GetVersion() const
135 return wxRendererVersion(wxRendererVersion::Current_Version
,
136 wxRendererVersion::Current_Age
);
140 // Cleanup by deleting standard renderer
141 static void Cleanup();
143 // Get the generic object
144 static wxRendererGeneric
* DoGetGeneric();
147 // draw the rectange using the first pen for the left and top sides and
148 // the second one for the bottom and right ones
149 void DrawShadedRect(wxDC
& dc
, wxRect
*rect
,
150 const wxPen
& pen1
, const wxPen
& pen2
);
158 static wxRendererGeneric
* sm_rendererGeneric
;
161 // ============================================================================
162 // wxRendererGeneric implementation
163 // ============================================================================
165 // Get the generic object
166 wxRendererGeneric
* wxRendererGeneric::DoGetGeneric()
168 if (!sm_rendererGeneric
)
169 sm_rendererGeneric
= new wxRendererGeneric
;
170 return sm_rendererGeneric
;
173 // ----------------------------------------------------------------------------
174 // wxRendererGeneric creation
175 // ----------------------------------------------------------------------------
178 wxRendererNative
& wxRendererNative::GetGeneric()
180 return * wxRendererGeneric::DoGetGeneric();
183 void wxRendererGeneric::Cleanup()
185 if (sm_rendererGeneric
)
186 delete sm_rendererGeneric
;
188 sm_rendererGeneric
= NULL
;
191 wxRendererGeneric
* wxRendererGeneric::sm_rendererGeneric
= NULL
;
193 wxRendererGeneric::wxRendererGeneric()
194 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
)),
195 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)),
196 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)),
197 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
))
201 // ----------------------------------------------------------------------------
202 // wxRendererGeneric helpers
203 // ----------------------------------------------------------------------------
206 wxRendererGeneric::DrawShadedRect(wxDC
& dc
,
211 // draw the rectangle
213 dc
.DrawLine(rect
->GetLeft(), rect
->GetTop(),
214 rect
->GetLeft(), rect
->GetBottom());
215 dc
.DrawLine(rect
->GetLeft() + 1, rect
->GetTop(),
216 rect
->GetRight(), rect
->GetTop());
218 dc
.DrawLine(rect
->GetRight(), rect
->GetTop(),
219 rect
->GetRight(), rect
->GetBottom());
220 dc
.DrawLine(rect
->GetLeft(), rect
->GetBottom(),
221 rect
->GetRight() + 1, rect
->GetBottom());
227 // ----------------------------------------------------------------------------
228 // tree/list ctrl drawing
229 // ----------------------------------------------------------------------------
232 wxRendererGeneric::DrawHeaderButton(wxWindow
* win
,
236 wxHeaderSortIconType sortArrow
,
237 wxHeaderButtonParams
* params
)
239 const wxCoord x
= rect
.x
,
244 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
245 dc
.SetPen(*wxTRANSPARENT_PEN
);
246 dc
.DrawRectangle(rect
);
248 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
250 dc
.SetPen(m_penBlack
);
251 dc
.DrawLine( x
+w
-1, y
, x
+w
-1, y
+h
); // right (outer)
252 dc
.DrawLine( x
, y
+h
-1, x
+w
, y
+h
-1 ); // bottom (outer)
254 dc
.SetPen(m_penDarkGrey
);
255 dc
.DrawLine( x
+w
-2, y
+1, x
+w
-2, y
+h
-1 ); // right (inner)
256 dc
.DrawLine( x
+1, y
+h
-2, x
+w
-1, y
+h
-2 ); // bottom (inner)
258 dc
.SetPen(m_penHighlight
);
259 dc
.DrawLine( x
, y
, x
, y
+h
-1 ); // left (outer)
260 dc
.DrawLine( x
, y
, x
+w
-1, y
); // top (outer)
262 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
267 wxRendererGeneric::DrawHeaderButtonContents(wxWindow
*win
,
271 wxHeaderSortIconType sortArrow
,
272 wxHeaderButtonParams
* params
)
276 // Mark this item as selected. For the generic version we'll just draw an
278 if ( flags
& wxCONTROL_SELECTED
)
280 // draw a line at the bottom of the header button, overlaying the
281 // native hot-tracking line (on XP)
282 const int penwidth
= 3;
283 int y
= rect
.y
+ rect
.height
+ 1 - penwidth
;
284 wxColour c
= (params
&& params
->m_selectionColour
.Ok()) ?
285 params
->m_selectionColour
: wxColour(0x66, 0x66, 0x66);
286 wxPen
pen(c
, penwidth
);
287 pen
.SetCap(wxCAP_BUTT
);
289 dc
.DrawLine(rect
.x
, y
, rect
.x
+ rect
.width
, y
);
292 // Draw an up or down arrow
294 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
298 // make a rect for the arrow
301 ar
.y
+= (rect
.height
- ar
.height
)/2;
302 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
303 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
306 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
308 triPt
[0].x
= ar
.width
/ 2;
310 triPt
[1].x
= ar
.width
;
311 triPt
[1].y
= ar
.height
;
313 triPt
[2].y
= ar
.height
;
319 triPt
[1].x
= ar
.width
;
321 triPt
[2].x
= ar
.width
/ 2;
322 triPt
[2].y
= ar
.height
;
325 wxColour c
= (params
&& params
->m_arrowColour
.Ok()) ?
326 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
328 dc
.SetBrush(wxBrush(c
));
329 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
331 labelWidth
+= arrowSpace
;
333 const int margin
= 5; // number of pixels to reserve on either side of the label
336 if ( params
&& params
->m_labelBitmap
.Ok() )
337 bmpWidth
= params
->m_labelBitmap
.GetWidth() + 2;
339 labelWidth
+= bmpWidth
+ 2*margin
;
341 // draw the bitmap if there is one
342 if ( params
&& params
->m_labelBitmap
.Ok() )
345 w
= params
->m_labelBitmap
.GetWidth();
346 h
= params
->m_labelBitmap
.GetHeight();
349 y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
351 if (params
->m_labelText
.empty())
353 // use the alignment flags
354 switch (params
->m_labelAlignment
)
361 x
= rect
.x
+ wxMax(1, (rect
.width
- arrowSpace
- w
)/2);
364 x
= rect
.x
+ wxMax(1, rect
.width
- arrowSpace
- margin
- w
);
368 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
371 // Draw a label if one is given
372 if ( params
&& !params
->m_labelText
.empty() )
374 wxFont font
= params
->m_labelFont
.Ok() ?
375 params
->m_labelFont
: win
->GetFont();
376 wxColour clr
= params
->m_labelColour
.Ok() ?
377 params
->m_labelColour
: win
->GetForegroundColour();
379 wxString
label( params
->m_labelText
);
382 dc
.SetTextForeground(clr
);
383 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
385 int tw
, th
, td
, x
, y
;
386 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
388 y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
390 y
+= 2; // No idea why.
393 // truncate and add an ellipsis (...) if the text is too wide.
394 int targetWidth
= rect
.width
- arrowSpace
- bmpWidth
- 2*margin
;
395 if ( tw
> targetWidth
)
398 dc
.GetTextExtent( wxT("..."), &ellipsisWidth
, NULL
);
400 label
.Truncate( label
.length() - 1 );
401 dc
.GetTextExtent( label
, &tw
, &th
);
402 } while (tw
+ ellipsisWidth
> targetWidth
&& label
.length() );
403 label
.append( wxT("...") );
407 switch (params
->m_labelAlignment
)
414 x
= rect
.x
+ wxMax(0, (rect
.width
- arrowSpace
- tw
- bmpWidth
)/2);
417 x
= rect
.x
+ wxMax(0, rect
.width
- arrowSpace
- margin
- tw
- bmpWidth
);
421 dc
.DrawText(label
, x
+ bmpWidth
, y
);
427 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
429 // Copied and adapted from src/generic/listctrl.cpp
430 const int HEADER_OFFSET_Y
= 1;
431 const int EXTRA_HEIGHT
= 4;
435 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
437 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
441 // draw the plus or minus sign
443 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
449 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
450 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
452 dc
.DrawRectangle(rect
);
455 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
456 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
458 // half of the length of the horz lines in "-" and "+"
459 const wxCoord halfWidth
= rect
.width
/2 - 2;
460 dc
.SetPen(*wxBLACK_PEN
);
461 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
462 xMiddle
+ halfWidth
+ 1, yMiddle
);
464 if ( !(flags
& wxCONTROL_EXPANDED
) )
467 const wxCoord halfHeight
= rect
.height
/2 - 2;
468 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
469 xMiddle
, yMiddle
+ halfHeight
+ 1);
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 wxSplitterRenderParams
478 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
484 if ( win
->HasFlag(wxSP_3DSASH
) )
486 else if ( win
->HasFlag(wxSP_NOSASH
) )
491 if ( win
->HasFlag(wxSP_3DBORDER
) )
496 return wxSplitterRenderParams(sashWidth
, border
, false);
500 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
502 const wxRect
& rectOrig
,
505 if ( win
->HasFlag(wxSP_3DBORDER
) )
507 wxRect rect
= rectOrig
;
508 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
509 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
514 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
516 const wxSize
& sizeReal
,
518 wxOrientation orient
,
521 // to avoid duplicating the same code for horizontal and vertical sashes,
522 // simply mirror the DC instead if needed (i.e. if horz splitter)
523 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
524 wxSize size
= dc
.Reflect(sizeReal
);
527 // we draw a Win32-like grey sash with possible 3D border here:
529 // ---- this is position
534 // GWGGGDB where G is light grey (face)
535 // GWGGGDB W white (light)
536 // GWGGGDB D dark grey (shadow)
537 // GWGGGDB B black (dark shadow)
539 // GWGGGDB and lower letters are our border (already drawn)
543 // only the middle 3 columns are drawn unless wxSP_3D is specified
545 const wxCoord h
= size
.y
;
548 // If we're drawing the border, draw the sash 3d lines shorter
549 if ( win
->HasFlag(wxSP_3DBORDER
) )
554 dc
.SetPen(*wxTRANSPARENT_PEN
);
555 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
557 if ( win
->HasFlag(wxSP_3DSASH
) )
560 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
562 dc
.SetPen(m_penLightGrey
);
563 dc
.DrawLine(position
, offset
, position
, h
- offset
);
565 dc
.SetPen(m_penHighlight
);
566 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
568 dc
.SetPen(m_penDarkGrey
);
569 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
571 dc
.SetPen(m_penBlack
);
572 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
577 dc
.DrawRectangle(position
, 0, 3, h
);
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
586 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
591 DrawPushButton(win
,dc
,rect
,flags
);
592 DrawDropArrow(win
,dc
,rect
,flags
);
596 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
601 // This generic implementation should be good
602 // enough for Windows platforms (including XP).
604 int arrowHalf
= rect
.width
/5;
605 int rectMid
= rect
.width
/ 2;
606 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
608 // This should always result in arrow with odd width.
611 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
612 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
613 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
615 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
616 dc
.SetPen(wxPen(win
->GetForegroundColour()));
617 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
621 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
626 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
627 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
628 dc
.DrawRectangle(rect
);
630 if ( flags
& wxCONTROL_CHECKED
)
632 dc
.DrawCheckMark(rect
.Deflate(2, 2));
636 wxSize
wxRendererGeneric::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
638 return wxSize(16, 16);
642 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
647 // Don't try anything too fancy. It'll just turn out looking
648 // out-of-place on most platforms.
649 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
650 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
651 win
->GetBackgroundColour();
652 dc
.SetBrush(wxBrush(bgCol
));
653 dc
.SetPen(wxPen(bgCol
));
654 dc
.DrawRectangle(rect
);
658 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* win
,
664 if ( flags
& wxCONTROL_SELECTED
)
666 if ( flags
& wxCONTROL_FOCUSED
)
668 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
672 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
677 brush
= *wxTRANSPARENT_BRUSH
;
681 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
)
682 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
683 && IsControlActive( (ControlRef
)win
->GetHandle() )
686 dc
.SetPen( *wxBLACK_PEN
);
688 dc
.SetPen( *wxTRANSPARENT_PEN
);
690 dc
.DrawRectangle( rect
);
692 // it's unused everywhere except in wxOSX/Carbon
697 wxRendererGeneric::DrawFocusRect(wxWindow
* WXUNUSED(win
), wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(flags
))
699 // draw the pixels manually because the "dots" in wxPen with wxDOT style
700 // may be short traits and not really dots
702 // note that to behave in the same manner as DrawRect(), we must exclude
703 // the bottom and right borders from the rectangle
704 wxCoord x1
= rect
.GetLeft(),
706 x2
= rect
.GetRight(),
707 y2
= rect
.GetBottom();
709 dc
.SetPen(m_penBlack
);
712 dc
.SetLogicalFunction(wxCOPY
);
714 // this seems to be closer than what Windows does than wxINVERT although
715 // I'm still not sure if it's correct
716 dc
.SetLogicalFunction(wxAND_REVERSE
);
720 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
721 dc
.DrawPoint(z
, rect
.GetTop());
723 wxCoord shift
= z
== x2
? 0 : 1;
724 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
727 shift
= z
== y2
? 0 : 1;
728 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
731 shift
= z
== x1
? 0 : 1;
732 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
735 dc
.SetLogicalFunction(wxCOPY
);
738 void wxRendererGeneric::DrawChoice(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
739 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
741 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawChoice");
744 void wxRendererGeneric::DrawComboBox(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
745 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
747 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawComboBox");
750 void wxRendererGeneric::DrawRadioBitmap(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
751 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
753 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawRadioBitmap");
756 void wxRendererGeneric::DrawTextCtrl(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
757 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
759 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawTextCtrl");
762 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
764 void wxRendererGeneric::DrawTitleBarBitmap(wxWindow
* WXUNUSED(win
),
766 const wxRect
& WXUNUSED(rect
),
767 wxTitleBarButton
WXUNUSED(button
),
770 // no need to fail here, if wxHAS_DRAW_TITLE_BAR_BITMAP is defined this
771 // will be implemented in the native renderer and this version is never
772 // going to be used -- but we still need to define it to allow
773 // instantiation of this class (which would have been pure virtual
777 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
780 // ----------------------------------------------------------------------------
781 // A module to allow cleanup of generic renderer.
782 // ----------------------------------------------------------------------------
784 class wxGenericRendererModule
: public wxModule
786 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
788 wxGenericRendererModule() {}
789 bool OnInit() { return true; }
790 void OnExit() { wxRendererGeneric::Cleanup(); }
793 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)