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"
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 void DrawTreeItemButton(wxWindow
*win
,
75 virtual void DrawSplitterBorder(wxWindow
*win
,
80 virtual void DrawSplitterSash(wxWindow
*win
,
87 virtual void DrawComboBoxDropButton(wxWindow
*win
,
92 virtual void DrawDropArrow(wxWindow
*win
,
97 virtual void DrawCheckBox(wxWindow
*win
,
102 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
104 virtual void DrawPushButton(wxWindow
*win
,
109 virtual void DrawItemSelectionRect(wxWindow
*win
,
114 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
116 virtual void DrawChoice(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
118 virtual void DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
120 virtual void DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
122 virtual void DrawRadioBitmap(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
=0);
124 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
125 virtual void DrawTitleBarBitmap(wxWindow
*win
,
128 wxTitleBarButton button
,
130 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
132 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
134 virtual wxRendererVersion
GetVersion() const
136 return wxRendererVersion(wxRendererVersion::Current_Version
,
137 wxRendererVersion::Current_Age
);
141 // Cleanup by deleting standard renderer
142 static void Cleanup();
144 // Get the generic object
145 static wxRendererGeneric
* DoGetGeneric();
148 // draw the rectange using the first pen for the left and top sides and
149 // the second one for the bottom and right ones
150 void DrawShadedRect(wxDC
& dc
, wxRect
*rect
,
151 const wxPen
& pen1
, const wxPen
& pen2
);
159 static wxRendererGeneric
* sm_rendererGeneric
;
162 // ============================================================================
163 // wxRendererGeneric implementation
164 // ============================================================================
166 // Get the generic object
167 wxRendererGeneric
* wxRendererGeneric::DoGetGeneric()
169 if (!sm_rendererGeneric
)
170 sm_rendererGeneric
= new wxRendererGeneric
;
171 return sm_rendererGeneric
;
174 // ----------------------------------------------------------------------------
175 // wxRendererGeneric creation
176 // ----------------------------------------------------------------------------
179 wxRendererNative
& wxRendererNative::GetGeneric()
181 return * wxRendererGeneric::DoGetGeneric();
184 void wxRendererGeneric::Cleanup()
186 wxDELETE(sm_rendererGeneric
);
189 wxRendererGeneric
* wxRendererGeneric::sm_rendererGeneric
= NULL
;
191 wxRendererGeneric::wxRendererGeneric()
192 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
)),
193 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)),
194 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)),
195 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
))
199 // ----------------------------------------------------------------------------
200 // wxRendererGeneric helpers
201 // ----------------------------------------------------------------------------
204 wxRendererGeneric::DrawShadedRect(wxDC
& dc
,
209 // draw the rectangle
211 dc
.DrawLine(rect
->GetLeft(), rect
->GetTop(),
212 rect
->GetLeft(), rect
->GetBottom());
213 dc
.DrawLine(rect
->GetLeft() + 1, rect
->GetTop(),
214 rect
->GetRight(), rect
->GetTop());
216 dc
.DrawLine(rect
->GetRight(), rect
->GetTop(),
217 rect
->GetRight(), rect
->GetBottom());
218 dc
.DrawLine(rect
->GetLeft(), rect
->GetBottom(),
219 rect
->GetRight() + 1, rect
->GetBottom());
225 // ----------------------------------------------------------------------------
226 // tree/list ctrl drawing
227 // ----------------------------------------------------------------------------
230 wxRendererGeneric::DrawHeaderButton(wxWindow
* win
,
234 wxHeaderSortIconType sortArrow
,
235 wxHeaderButtonParams
* params
)
237 const wxCoord x
= rect
.x
,
242 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
243 dc
.SetPen(*wxTRANSPARENT_PEN
);
244 dc
.DrawRectangle(rect
);
246 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
248 dc
.SetPen(m_penBlack
);
249 dc
.DrawLine( x
+w
-1, y
, x
+w
-1, y
+h
); // right (outer)
250 dc
.DrawLine( x
, y
+h
-1, x
+w
, y
+h
-1 ); // bottom (outer)
252 dc
.SetPen(m_penDarkGrey
);
253 dc
.DrawLine( x
+w
-2, y
+1, x
+w
-2, y
+h
-1 ); // right (inner)
254 dc
.DrawLine( x
+1, y
+h
-2, x
+w
-1, y
+h
-2 ); // bottom (inner)
256 dc
.SetPen(m_penHighlight
);
257 dc
.DrawLine( x
, y
, x
, y
+h
-1 ); // left (outer)
258 dc
.DrawLine( x
, y
, x
+w
-1, y
); // top (outer)
260 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
265 wxRendererGeneric::DrawHeaderButtonContents(wxWindow
*win
,
269 wxHeaderSortIconType sortArrow
,
270 wxHeaderButtonParams
* params
)
274 // Mark this item as selected. For the generic version we'll just draw an
276 if ( flags
& wxCONTROL_SELECTED
)
278 // draw a line at the bottom of the header button, overlaying the
279 // native hot-tracking line (on XP)
280 const int penwidth
= 3;
281 int y
= rect
.y
+ rect
.height
+ 1 - penwidth
;
282 wxColour c
= (params
&& params
->m_selectionColour
.Ok()) ?
283 params
->m_selectionColour
: wxColour(0x66, 0x66, 0x66);
284 wxPen
pen(c
, penwidth
);
285 pen
.SetCap(wxCAP_BUTT
);
287 dc
.DrawLine(rect
.x
, y
, rect
.x
+ rect
.width
, y
);
290 // Draw an up or down arrow
292 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
296 // make a rect for the arrow
299 ar
.y
+= (rect
.height
- ar
.height
)/2;
300 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
301 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
304 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
306 triPt
[0].x
= ar
.width
/ 2;
308 triPt
[1].x
= ar
.width
;
309 triPt
[1].y
= ar
.height
;
311 triPt
[2].y
= ar
.height
;
317 triPt
[1].x
= ar
.width
;
319 triPt
[2].x
= ar
.width
/ 2;
320 triPt
[2].y
= ar
.height
;
323 wxColour c
= (params
&& params
->m_arrowColour
.Ok()) ?
324 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
326 wxDCPenChanger
setPen(dc
, c
);
327 wxDCBrushChanger
setBrush(dc
, c
);
329 wxDCClipper
clip(dc
, rect
);
330 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
332 labelWidth
+= arrowSpace
;
336 // draw the bitmap if there is one
337 if ( params
&& params
->m_labelBitmap
.Ok() )
339 int w
= params
->m_labelBitmap
.GetWidth();
340 int h
= params
->m_labelBitmap
.GetHeight();
342 const int margin
= 1; // an extra pixel on either side of the bitmap
344 bmpWidth
= w
+ 2*margin
;
345 labelWidth
+= bmpWidth
;
347 int x
= rect
.x
+ margin
;
348 const int y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
350 const int extraSpace
= rect
.width
- labelWidth
;
351 if ( params
->m_labelText
.empty() && extraSpace
> 0 )
353 // use the alignment flags
354 switch (params
->m_labelAlignment
)
370 wxDCClipper
clip(dc
, rect
);
371 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
374 // Draw a label if one is given
375 if ( params
&& !params
->m_labelText
.empty() )
377 const int margin
= 5; // number of pixels to reserve on either side of the label
378 labelWidth
+= 2*margin
;
380 wxFont font
= params
->m_labelFont
.Ok() ?
381 params
->m_labelFont
: win
->GetFont();
382 wxColour clr
= params
->m_labelColour
.Ok() ?
383 params
->m_labelColour
: win
->GetForegroundColour();
385 wxString
label( params
->m_labelText
);
388 dc
.SetTextForeground(clr
);
389 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
392 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
394 int x
= rect
.x
+ bmpWidth
+ margin
;
395 const int y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
397 // truncate and add an ellipsis (...) if the text is too wide.
398 const int availWidth
= rect
.width
- labelWidth
;
399 if ( tw
> availWidth
)
401 label
= wxControl::Ellipsize(label
,
405 wxELLIPSIZE_FLAGS_NONE
);
406 tw
= dc
.GetTextExtent(label
).x
;
408 else // enough space, we can respect alignment
410 switch (params
->m_labelAlignment
)
417 x
+= (availWidth
- tw
)/2;
421 x
+= availWidth
- tw
;
426 dc
.DrawText(label
, x
, y
);
435 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
437 // Copied and adapted from src/generic/listctrl.cpp
438 const int HEADER_OFFSET_Y
= 1;
439 const int EXTRA_HEIGHT
= 4;
443 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
445 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
449 // draw the plus or minus sign
451 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
457 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
458 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
460 dc
.DrawRectangle(rect
);
463 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
464 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
466 // half of the length of the horz lines in "-" and "+"
467 const wxCoord halfWidth
= rect
.width
/2 - 2;
468 dc
.SetPen(*wxBLACK_PEN
);
469 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
470 xMiddle
+ halfWidth
+ 1, yMiddle
);
472 if ( !(flags
& wxCONTROL_EXPANDED
) )
475 const wxCoord halfHeight
= rect
.height
/2 - 2;
476 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
477 xMiddle
, yMiddle
+ halfHeight
+ 1);
481 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
485 wxSplitterRenderParams
486 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
492 if ( win
->HasFlag(wxSP_3DSASH
) )
494 else if ( win
->HasFlag(wxSP_NOSASH
) )
499 if ( win
->HasFlag(wxSP_3DBORDER
) )
504 return wxSplitterRenderParams(sashWidth
, border
, false);
508 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
510 const wxRect
& rectOrig
,
513 if ( win
->HasFlag(wxSP_3DBORDER
) )
515 wxRect rect
= rectOrig
;
516 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
517 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
522 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
524 const wxSize
& sizeReal
,
526 wxOrientation orient
,
529 // to avoid duplicating the same code for horizontal and vertical sashes,
530 // simply mirror the DC instead if needed (i.e. if horz splitter)
531 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
532 wxSize size
= dc
.Reflect(sizeReal
);
535 // we draw a Win32-like grey sash with possible 3D border here:
537 // ---- this is position
542 // GWGGGDB where G is light grey (face)
543 // GWGGGDB W white (light)
544 // GWGGGDB D dark grey (shadow)
545 // GWGGGDB B black (dark shadow)
547 // GWGGGDB and lower letters are our border (already drawn)
551 // only the middle 3 columns are drawn unless wxSP_3D is specified
553 const wxCoord h
= size
.y
;
556 // If we're drawing the border, draw the sash 3d lines shorter
557 if ( win
->HasFlag(wxSP_3DBORDER
) )
562 dc
.SetPen(*wxTRANSPARENT_PEN
);
563 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
565 if ( win
->HasFlag(wxSP_3DSASH
) )
568 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
570 dc
.SetPen(m_penLightGrey
);
571 dc
.DrawLine(position
, offset
, position
, h
- offset
);
573 dc
.SetPen(m_penHighlight
);
574 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
576 dc
.SetPen(m_penDarkGrey
);
577 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
579 dc
.SetPen(m_penBlack
);
580 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
585 dc
.DrawRectangle(position
, 0, 3, h
);
589 // ----------------------------------------------------------------------------
591 // ----------------------------------------------------------------------------
594 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
599 DrawPushButton(win
,dc
,rect
,flags
);
600 DrawDropArrow(win
,dc
,rect
,flags
);
604 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
609 // This generic implementation should be good
610 // enough for Windows platforms (including XP).
612 int arrowHalf
= rect
.width
/5;
613 int rectMid
= rect
.width
/ 2;
614 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
616 // This should always result in arrow with odd width.
619 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
620 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
621 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
623 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
624 dc
.SetPen(wxPen(win
->GetForegroundColour()));
625 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
629 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
634 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
635 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
636 dc
.DrawRectangle(rect
);
638 if ( flags
& wxCONTROL_CHECKED
)
640 dc
.DrawCheckMark(rect
.Deflate(2, 2));
644 wxSize
wxRendererGeneric::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
646 return wxSize(16, 16);
650 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
655 // Don't try anything too fancy. It'll just turn out looking
656 // out-of-place on most platforms.
657 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
658 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
659 win
->GetBackgroundColour();
660 dc
.SetBrush(wxBrush(bgCol
));
661 dc
.SetPen(wxPen(bgCol
));
662 dc
.DrawRectangle(rect
);
666 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* win
,
672 if ( flags
& wxCONTROL_SELECTED
)
674 if ( flags
& wxCONTROL_FOCUSED
)
676 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
680 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
685 brush
= *wxTRANSPARENT_BRUSH
;
689 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
)
690 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
691 && IsControlActive( (ControlRef
)win
->GetHandle() )
694 dc
.SetPen( *wxBLACK_PEN
);
696 dc
.SetPen( *wxTRANSPARENT_PEN
);
698 dc
.DrawRectangle( rect
);
700 // it's unused everywhere except in wxOSX/Carbon
705 wxRendererGeneric::DrawFocusRect(wxWindow
* WXUNUSED(win
), wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(flags
))
707 // draw the pixels manually because the "dots" in wxPen with wxDOT style
708 // may be short traits and not really dots
710 // note that to behave in the same manner as DrawRect(), we must exclude
711 // the bottom and right borders from the rectangle
712 wxCoord x1
= rect
.GetLeft(),
714 x2
= rect
.GetRight(),
715 y2
= rect
.GetBottom();
717 dc
.SetPen(m_penBlack
);
720 dc
.SetLogicalFunction(wxCOPY
);
722 // this seems to be closer than what Windows does than wxINVERT although
723 // I'm still not sure if it's correct
724 dc
.SetLogicalFunction(wxAND_REVERSE
);
728 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
729 dc
.DrawPoint(z
, rect
.GetTop());
731 wxCoord shift
= z
== x2
? 0 : 1;
732 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
735 shift
= z
== y2
? 0 : 1;
736 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
739 shift
= z
== x1
? 0 : 1;
740 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
743 dc
.SetLogicalFunction(wxCOPY
);
746 void wxRendererGeneric::DrawChoice(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
747 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
749 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawChoice");
752 void wxRendererGeneric::DrawComboBox(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
753 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
755 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawComboBox");
758 void wxRendererGeneric::DrawRadioBitmap(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
759 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
761 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawRadioBitmap");
764 void wxRendererGeneric::DrawTextCtrl(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
765 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
767 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawTextCtrl");
770 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
772 void wxRendererGeneric::DrawTitleBarBitmap(wxWindow
* WXUNUSED(win
),
774 const wxRect
& WXUNUSED(rect
),
775 wxTitleBarButton
WXUNUSED(button
),
778 // no need to fail here, if wxHAS_DRAW_TITLE_BAR_BITMAP is defined this
779 // will be implemented in the native renderer and this version is never
780 // going to be used -- but we still need to define it to allow
781 // instantiation of this class (which would have been pure virtual
785 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
788 // ----------------------------------------------------------------------------
789 // A module to allow cleanup of generic renderer.
790 // ----------------------------------------------------------------------------
792 class wxGenericRendererModule
: public wxModule
794 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
796 wxGenericRendererModule() {}
797 bool OnInit() { return true; }
798 void OnExit() { wxRendererGeneric::Cleanup(); }
801 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)