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 // Licence: wxWindows licence
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"
35 #include "wx/control.h"
38 #include "wx/splitter.h"
39 #include "wx/dcmirror.h"
42 #include "wx/osx/private.h"
45 // ----------------------------------------------------------------------------
46 // wxRendererGeneric: our wxRendererNative implementation
47 // ----------------------------------------------------------------------------
49 class WXDLLEXPORT wxRendererGeneric
: public wxRendererNative
54 virtual int DrawHeaderButton(wxWindow
*win
,
58 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
59 wxHeaderButtonParams
* params
= NULL
);
61 virtual int DrawHeaderButtonContents(wxWindow
*win
,
65 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
66 wxHeaderButtonParams
* params
= NULL
);
68 virtual int GetHeaderButtonHeight(wxWindow
*win
);
70 virtual int GetHeaderButtonMargin(wxWindow
*win
);
72 virtual void DrawTreeItemButton(wxWindow
*win
,
77 virtual void DrawSplitterBorder(wxWindow
*win
,
82 virtual void DrawSplitterSash(wxWindow
*win
,
89 virtual void DrawComboBoxDropButton(wxWindow
*win
,
94 virtual void DrawDropArrow(wxWindow
*win
,
99 virtual void DrawCheckBox(wxWindow
*win
,
104 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
106 virtual void DrawPushButton(wxWindow
*win
,
111 virtual void DrawItemSelectionRect(wxWindow
*win
,
116 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
118 virtual void DrawChoice(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
120 virtual void DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
122 virtual void DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
124 virtual void DrawRadioBitmap(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
126 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
127 virtual void DrawTitleBarBitmap(wxWindow
*win
,
130 wxTitleBarButton button
,
132 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
134 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
136 virtual wxRendererVersion
GetVersion() const
138 return wxRendererVersion(wxRendererVersion::Current_Version
,
139 wxRendererVersion::Current_Age
);
143 // Cleanup by deleting standard renderer
144 static void Cleanup();
146 // Get the generic object
147 static wxRendererGeneric
* DoGetGeneric();
150 // draw the rectange using the first pen for the left and top sides and
151 // the second one for the bottom and right ones
152 void DrawShadedRect(wxDC
& dc
, wxRect
*rect
,
153 const wxPen
& pen1
, const wxPen
& pen2
);
161 static wxRendererGeneric
* sm_rendererGeneric
;
164 // ============================================================================
165 // wxRendererGeneric implementation
166 // ============================================================================
168 // Get the generic object
169 wxRendererGeneric
* wxRendererGeneric::DoGetGeneric()
171 if (!sm_rendererGeneric
)
172 sm_rendererGeneric
= new wxRendererGeneric
;
173 return sm_rendererGeneric
;
176 // ----------------------------------------------------------------------------
177 // wxRendererGeneric creation
178 // ----------------------------------------------------------------------------
181 wxRendererNative
& wxRendererNative::GetGeneric()
183 return * wxRendererGeneric::DoGetGeneric();
186 void wxRendererGeneric::Cleanup()
188 wxDELETE(sm_rendererGeneric
);
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
.IsOk()) ?
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
.IsOk()) ?
326 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
328 wxDCPenChanger
setPen(dc
, c
);
329 wxDCBrushChanger
setBrush(dc
, c
);
331 wxDCClipper
clip(dc
, rect
);
332 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
334 labelWidth
+= arrowSpace
;
338 // draw the bitmap if there is one
339 if ( params
&& params
->m_labelBitmap
.IsOk() )
341 int w
= params
->m_labelBitmap
.GetWidth();
342 int h
= params
->m_labelBitmap
.GetHeight();
344 const int margin
= 1; // an extra pixel on either side of the bitmap
346 bmpWidth
= w
+ 2*margin
;
347 labelWidth
+= bmpWidth
;
349 int x
= rect
.x
+ margin
;
350 const int y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
352 const int extraSpace
= rect
.width
- labelWidth
;
353 if ( params
->m_labelText
.empty() && extraSpace
> 0 )
355 // use the alignment flags
356 switch (params
->m_labelAlignment
)
372 wxDCClipper
clip(dc
, rect
);
373 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
376 // Draw a label if one is given
377 if ( params
&& !params
->m_labelText
.empty() )
379 const int margin
= 5; // number of pixels to reserve on either side of the label
380 labelWidth
+= 2*margin
;
382 wxFont font
= params
->m_labelFont
.IsOk() ?
383 params
->m_labelFont
: win
->GetFont();
384 wxColour clr
= params
->m_labelColour
.IsOk() ?
385 params
->m_labelColour
: win
->GetForegroundColour();
387 wxString
label( params
->m_labelText
);
390 dc
.SetTextForeground(clr
);
391 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
394 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
396 int x
= rect
.x
+ bmpWidth
+ margin
;
397 const int y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
399 // truncate and add an ellipsis (...) if the text is too wide.
400 const int availWidth
= rect
.width
- labelWidth
;
402 if ( tw
> availWidth
)
404 label
= wxControl::Ellipsize(label
,
408 wxELLIPSIZE_FLAGS_NONE
);
409 tw
= dc
.GetTextExtent(label
).x
;
411 else // enough space, we can respect alignment
412 #endif // wxUSE_CONTROLS
414 switch (params
->m_labelAlignment
)
421 x
+= (availWidth
- tw
)/2;
425 x
+= availWidth
- tw
;
430 dc
.DrawText(label
, x
, y
);
439 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
441 // Copied and adapted from src/generic/listctrl.cpp
442 const int HEADER_OFFSET_Y
= 1;
443 const int EXTRA_HEIGHT
= 4;
447 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
449 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
452 int wxRendererGeneric::GetHeaderButtonMargin(wxWindow
*WXUNUSED(win
))
458 // draw the plus or minus sign
460 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
466 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
467 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
469 dc
.DrawRectangle(rect
);
472 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
473 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
475 // half of the length of the horz lines in "-" and "+"
476 const wxCoord halfWidth
= rect
.width
/2 - 2;
477 dc
.SetPen(*wxBLACK_PEN
);
478 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
479 xMiddle
+ halfWidth
+ 1, yMiddle
);
481 if ( !(flags
& wxCONTROL_EXPANDED
) )
484 const wxCoord halfHeight
= rect
.height
/2 - 2;
485 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
486 xMiddle
, yMiddle
+ halfHeight
+ 1);
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
494 wxSplitterRenderParams
495 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
501 if ( win
->HasFlag(wxSP_3DSASH
) )
503 else if ( win
->HasFlag(wxSP_NOSASH
) )
508 if ( win
->HasFlag(wxSP_3DBORDER
) )
513 return wxSplitterRenderParams(sashWidth
, border
, false);
517 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
519 const wxRect
& rectOrig
,
522 if ( win
->HasFlag(wxSP_3DBORDER
) )
524 wxRect rect
= rectOrig
;
525 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
526 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
531 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
533 const wxSize
& sizeReal
,
535 wxOrientation orient
,
538 // to avoid duplicating the same code for horizontal and vertical sashes,
539 // simply mirror the DC instead if needed (i.e. if horz splitter)
540 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
541 wxSize size
= dc
.Reflect(sizeReal
);
544 // we draw a Win32-like grey sash with possible 3D border here:
546 // ---- this is position
551 // GWGGGDB where G is light grey (face)
552 // GWGGGDB W white (light)
553 // GWGGGDB D dark grey (shadow)
554 // GWGGGDB B black (dark shadow)
556 // GWGGGDB and lower letters are our border (already drawn)
560 // only the middle 3 columns are drawn unless wxSP_3D is specified
562 const wxCoord h
= size
.y
;
565 // If we're drawing the border, draw the sash 3d lines shorter
566 if ( win
->HasFlag(wxSP_3DBORDER
) )
571 dc
.SetPen(*wxTRANSPARENT_PEN
);
572 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
574 if ( win
->HasFlag(wxSP_3DSASH
) )
577 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
579 dc
.SetPen(m_penLightGrey
);
580 dc
.DrawLine(position
, offset
, position
, h
- offset
);
582 dc
.SetPen(m_penHighlight
);
583 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
585 dc
.SetPen(m_penDarkGrey
);
586 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
588 dc
.SetPen(m_penBlack
);
589 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
594 dc
.DrawRectangle(position
, 0, 3, h
);
598 // ----------------------------------------------------------------------------
600 // ----------------------------------------------------------------------------
603 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
608 DrawPushButton(win
,dc
,rect
,flags
);
609 DrawDropArrow(win
,dc
,rect
,flags
);
613 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
618 // This generic implementation should be good
619 // enough for Windows platforms (including XP).
621 int arrowHalf
= rect
.width
/5;
622 int rectMid
= rect
.width
/ 2;
623 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
625 // This should always result in arrow with odd width.
628 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
629 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
630 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
632 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
633 dc
.SetPen(wxPen(win
->GetForegroundColour()));
634 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
638 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
643 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
644 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
645 dc
.DrawRectangle(rect
);
647 if ( flags
& wxCONTROL_CHECKED
)
649 dc
.DrawCheckMark(rect
.Deflate(2, 2));
653 wxSize
wxRendererGeneric::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
655 return wxSize(16, 16);
659 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
664 // Don't try anything too fancy. It'll just turn out looking
665 // out-of-place on most platforms.
666 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
667 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
668 win
->GetBackgroundColour();
669 dc
.SetBrush(wxBrush(bgCol
));
670 dc
.SetPen(wxPen(bgCol
));
671 dc
.DrawRectangle(rect
);
675 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* win
,
681 if ( flags
& wxCONTROL_SELECTED
)
683 if ( flags
& wxCONTROL_FOCUSED
)
685 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
689 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
694 brush
= *wxTRANSPARENT_BRUSH
;
698 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
)
699 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
700 && IsControlActive( (ControlRef
)win
->GetHandle() )
703 dc
.SetPen( *wxBLACK_PEN
);
705 dc
.SetPen( *wxTRANSPARENT_PEN
);
707 dc
.DrawRectangle( rect
);
709 // it's unused everywhere except in wxOSX/Carbon
714 wxRendererGeneric::DrawFocusRect(wxWindow
* WXUNUSED(win
), wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(flags
))
716 // draw the pixels manually because the "dots" in wxPen with wxDOT style
717 // may be short traits and not really dots
719 // note that to behave in the same manner as DrawRect(), we must exclude
720 // the bottom and right borders from the rectangle
721 wxCoord x1
= rect
.GetLeft(),
723 x2
= rect
.GetRight(),
724 y2
= rect
.GetBottom();
726 dc
.SetPen(m_penBlack
);
729 dc
.SetLogicalFunction(wxCOPY
);
731 // this seems to be closer than what Windows does than wxINVERT although
732 // I'm still not sure if it's correct
733 dc
.SetLogicalFunction(wxAND_REVERSE
);
737 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
738 dc
.DrawPoint(z
, rect
.GetTop());
740 wxCoord shift
= z
== x2
? 0 : 1;
741 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
744 shift
= z
== y2
? 0 : 1;
745 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
748 shift
= z
== x1
? 0 : 1;
749 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
752 dc
.SetLogicalFunction(wxCOPY
);
755 void wxRendererGeneric::DrawChoice(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
756 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
758 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawChoice");
761 void wxRendererGeneric::DrawComboBox(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
762 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
764 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawComboBox");
767 void wxRendererGeneric::DrawRadioBitmap(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
768 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
770 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawRadioBitmap");
773 void wxRendererGeneric::DrawTextCtrl(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
774 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
776 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawTextCtrl");
779 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
781 void wxRendererGeneric::DrawTitleBarBitmap(wxWindow
* WXUNUSED(win
),
783 const wxRect
& WXUNUSED(rect
),
784 wxTitleBarButton
WXUNUSED(button
),
787 // no need to fail here, if wxHAS_DRAW_TITLE_BAR_BITMAP is defined this
788 // will be implemented in the native renderer and this version is never
789 // going to be used -- but we still need to define it to allow
790 // instantiation of this class (which would have been pure virtual
794 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
797 // ----------------------------------------------------------------------------
798 // A module to allow cleanup of generic renderer.
799 // ----------------------------------------------------------------------------
801 class wxGenericRendererModule
: public wxModule
803 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
805 wxGenericRendererModule() {}
806 bool OnInit() { return true; }
807 void OnExit() { wxRendererGeneric::Cleanup(); }
810 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)