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 if (sm_rendererGeneric
)
187 delete sm_rendererGeneric
;
189 sm_rendererGeneric
= NULL
;
192 wxRendererGeneric
* wxRendererGeneric::sm_rendererGeneric
= NULL
;
194 wxRendererGeneric::wxRendererGeneric()
195 : m_penBlack(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
)),
196 m_penDarkGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)),
197 m_penLightGrey(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)),
198 m_penHighlight(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
))
202 // ----------------------------------------------------------------------------
203 // wxRendererGeneric helpers
204 // ----------------------------------------------------------------------------
207 wxRendererGeneric::DrawShadedRect(wxDC
& dc
,
212 // draw the rectangle
214 dc
.DrawLine(rect
->GetLeft(), rect
->GetTop(),
215 rect
->GetLeft(), rect
->GetBottom());
216 dc
.DrawLine(rect
->GetLeft() + 1, rect
->GetTop(),
217 rect
->GetRight(), rect
->GetTop());
219 dc
.DrawLine(rect
->GetRight(), rect
->GetTop(),
220 rect
->GetRight(), rect
->GetBottom());
221 dc
.DrawLine(rect
->GetLeft(), rect
->GetBottom(),
222 rect
->GetRight() + 1, rect
->GetBottom());
228 // ----------------------------------------------------------------------------
229 // tree/list ctrl drawing
230 // ----------------------------------------------------------------------------
233 wxRendererGeneric::DrawHeaderButton(wxWindow
* win
,
237 wxHeaderSortIconType sortArrow
,
238 wxHeaderButtonParams
* params
)
240 const wxCoord x
= rect
.x
,
245 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
246 dc
.SetPen(*wxTRANSPARENT_PEN
);
247 dc
.DrawRectangle(rect
);
249 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
251 dc
.SetPen(m_penBlack
);
252 dc
.DrawLine( x
+w
-1, y
, x
+w
-1, y
+h
); // right (outer)
253 dc
.DrawLine( x
, y
+h
-1, x
+w
, y
+h
-1 ); // bottom (outer)
255 dc
.SetPen(m_penDarkGrey
);
256 dc
.DrawLine( x
+w
-2, y
+1, x
+w
-2, y
+h
-1 ); // right (inner)
257 dc
.DrawLine( x
+1, y
+h
-2, x
+w
-1, y
+h
-2 ); // bottom (inner)
259 dc
.SetPen(m_penHighlight
);
260 dc
.DrawLine( x
, y
, x
, y
+h
-1 ); // left (outer)
261 dc
.DrawLine( x
, y
, x
+w
-1, y
); // top (outer)
263 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
268 wxRendererGeneric::DrawHeaderButtonContents(wxWindow
*win
,
272 wxHeaderSortIconType sortArrow
,
273 wxHeaderButtonParams
* params
)
277 // Mark this item as selected. For the generic version we'll just draw an
279 if ( flags
& wxCONTROL_SELECTED
)
281 // draw a line at the bottom of the header button, overlaying the
282 // native hot-tracking line (on XP)
283 const int penwidth
= 3;
284 int y
= rect
.y
+ rect
.height
+ 1 - penwidth
;
285 wxColour c
= (params
&& params
->m_selectionColour
.Ok()) ?
286 params
->m_selectionColour
: wxColour(0x66, 0x66, 0x66);
287 wxPen
pen(c
, penwidth
);
288 pen
.SetCap(wxCAP_BUTT
);
290 dc
.DrawLine(rect
.x
, y
, rect
.x
+ rect
.width
, y
);
293 // Draw an up or down arrow
295 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
299 // make a rect for the arrow
302 ar
.y
+= (rect
.height
- ar
.height
)/2;
303 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
304 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
307 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
309 triPt
[0].x
= ar
.width
/ 2;
311 triPt
[1].x
= ar
.width
;
312 triPt
[1].y
= ar
.height
;
314 triPt
[2].y
= ar
.height
;
320 triPt
[1].x
= ar
.width
;
322 triPt
[2].x
= ar
.width
/ 2;
323 triPt
[2].y
= ar
.height
;
326 wxColour c
= (params
&& params
->m_arrowColour
.Ok()) ?
327 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
329 wxDCPenChanger
setPen(dc
, c
);
330 wxDCBrushChanger
setBrush(dc
, c
);
332 wxDCClipper
clip(dc
, rect
);
333 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
335 labelWidth
+= arrowSpace
;
339 // draw the bitmap if there is one
340 if ( params
&& params
->m_labelBitmap
.Ok() )
342 int w
= params
->m_labelBitmap
.GetWidth();
343 int h
= params
->m_labelBitmap
.GetHeight();
345 const int margin
= 1; // an extra pixel on either side of the bitmap
347 bmpWidth
= w
+ 2*margin
;
348 labelWidth
+= bmpWidth
;
350 int x
= rect
.x
+ margin
;
351 const int y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
353 const int extraSpace
= rect
.width
- labelWidth
;
354 if ( params
->m_labelText
.empty() && extraSpace
> 0 )
356 // use the alignment flags
357 switch (params
->m_labelAlignment
)
373 wxDCClipper
clip(dc
, rect
);
374 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
377 // Draw a label if one is given
378 if ( params
&& !params
->m_labelText
.empty() )
380 const int margin
= 5; // number of pixels to reserve on either side of the label
381 labelWidth
+= 2*margin
;
383 wxFont font
= params
->m_labelFont
.Ok() ?
384 params
->m_labelFont
: win
->GetFont();
385 wxColour clr
= params
->m_labelColour
.Ok() ?
386 params
->m_labelColour
: win
->GetForegroundColour();
388 wxString
label( params
->m_labelText
);
391 dc
.SetTextForeground(clr
);
392 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
395 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
397 int x
= rect
.x
+ bmpWidth
+ margin
;
398 const int y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
400 // truncate and add an ellipsis (...) if the text is too wide.
401 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
413 switch (params
->m_labelAlignment
)
420 x
+= (availWidth
- tw
)/2;
424 x
+= availWidth
- tw
;
429 dc
.DrawText(label
, x
, y
);
438 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
440 // Copied and adapted from src/generic/listctrl.cpp
441 const int HEADER_OFFSET_Y
= 1;
442 const int EXTRA_HEIGHT
= 4;
446 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
448 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
452 // draw the plus or minus sign
454 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
460 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
461 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
463 dc
.DrawRectangle(rect
);
466 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
467 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
469 // half of the length of the horz lines in "-" and "+"
470 const wxCoord halfWidth
= rect
.width
/2 - 2;
471 dc
.SetPen(*wxBLACK_PEN
);
472 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
473 xMiddle
+ halfWidth
+ 1, yMiddle
);
475 if ( !(flags
& wxCONTROL_EXPANDED
) )
478 const wxCoord halfHeight
= rect
.height
/2 - 2;
479 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
480 xMiddle
, yMiddle
+ halfHeight
+ 1);
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 wxSplitterRenderParams
489 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
495 if ( win
->HasFlag(wxSP_3DSASH
) )
497 else if ( win
->HasFlag(wxSP_NOSASH
) )
502 if ( win
->HasFlag(wxSP_3DBORDER
) )
507 return wxSplitterRenderParams(sashWidth
, border
, false);
511 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
513 const wxRect
& rectOrig
,
516 if ( win
->HasFlag(wxSP_3DBORDER
) )
518 wxRect rect
= rectOrig
;
519 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
520 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
525 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
527 const wxSize
& sizeReal
,
529 wxOrientation orient
,
532 // to avoid duplicating the same code for horizontal and vertical sashes,
533 // simply mirror the DC instead if needed (i.e. if horz splitter)
534 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
535 wxSize size
= dc
.Reflect(sizeReal
);
538 // we draw a Win32-like grey sash with possible 3D border here:
540 // ---- this is position
545 // GWGGGDB where G is light grey (face)
546 // GWGGGDB W white (light)
547 // GWGGGDB D dark grey (shadow)
548 // GWGGGDB B black (dark shadow)
550 // GWGGGDB and lower letters are our border (already drawn)
554 // only the middle 3 columns are drawn unless wxSP_3D is specified
556 const wxCoord h
= size
.y
;
559 // If we're drawing the border, draw the sash 3d lines shorter
560 if ( win
->HasFlag(wxSP_3DBORDER
) )
565 dc
.SetPen(*wxTRANSPARENT_PEN
);
566 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
568 if ( win
->HasFlag(wxSP_3DSASH
) )
571 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
573 dc
.SetPen(m_penLightGrey
);
574 dc
.DrawLine(position
, offset
, position
, h
- offset
);
576 dc
.SetPen(m_penHighlight
);
577 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
579 dc
.SetPen(m_penDarkGrey
);
580 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
582 dc
.SetPen(m_penBlack
);
583 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
588 dc
.DrawRectangle(position
, 0, 3, h
);
592 // ----------------------------------------------------------------------------
594 // ----------------------------------------------------------------------------
597 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
602 DrawPushButton(win
,dc
,rect
,flags
);
603 DrawDropArrow(win
,dc
,rect
,flags
);
607 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
612 // This generic implementation should be good
613 // enough for Windows platforms (including XP).
615 int arrowHalf
= rect
.width
/5;
616 int rectMid
= rect
.width
/ 2;
617 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
619 // This should always result in arrow with odd width.
622 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
623 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
624 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
626 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
627 dc
.SetPen(wxPen(win
->GetForegroundColour()));
628 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
632 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
637 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
638 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
639 dc
.DrawRectangle(rect
);
641 if ( flags
& wxCONTROL_CHECKED
)
643 dc
.DrawCheckMark(rect
.Deflate(2, 2));
647 wxSize
wxRendererGeneric::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
649 return wxSize(16, 16);
653 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
658 // Don't try anything too fancy. It'll just turn out looking
659 // out-of-place on most platforms.
660 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
661 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
662 win
->GetBackgroundColour();
663 dc
.SetBrush(wxBrush(bgCol
));
664 dc
.SetPen(wxPen(bgCol
));
665 dc
.DrawRectangle(rect
);
669 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* win
,
675 if ( flags
& wxCONTROL_SELECTED
)
677 if ( flags
& wxCONTROL_FOCUSED
)
679 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
683 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
688 brush
= *wxTRANSPARENT_BRUSH
;
692 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
)
693 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
694 && IsControlActive( (ControlRef
)win
->GetHandle() )
697 dc
.SetPen( *wxBLACK_PEN
);
699 dc
.SetPen( *wxTRANSPARENT_PEN
);
701 dc
.DrawRectangle( rect
);
703 // it's unused everywhere except in wxOSX/Carbon
708 wxRendererGeneric::DrawFocusRect(wxWindow
* WXUNUSED(win
), wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(flags
))
710 // draw the pixels manually because the "dots" in wxPen with wxDOT style
711 // may be short traits and not really dots
713 // note that to behave in the same manner as DrawRect(), we must exclude
714 // the bottom and right borders from the rectangle
715 wxCoord x1
= rect
.GetLeft(),
717 x2
= rect
.GetRight(),
718 y2
= rect
.GetBottom();
720 dc
.SetPen(m_penBlack
);
723 dc
.SetLogicalFunction(wxCOPY
);
725 // this seems to be closer than what Windows does than wxINVERT although
726 // I'm still not sure if it's correct
727 dc
.SetLogicalFunction(wxAND_REVERSE
);
731 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
732 dc
.DrawPoint(z
, rect
.GetTop());
734 wxCoord shift
= z
== x2
? 0 : 1;
735 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
738 shift
= z
== y2
? 0 : 1;
739 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
742 shift
= z
== x1
? 0 : 1;
743 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
746 dc
.SetLogicalFunction(wxCOPY
);
749 void wxRendererGeneric::DrawChoice(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
750 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
752 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawChoice");
755 void wxRendererGeneric::DrawComboBox(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
756 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
758 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawComboBox");
761 void wxRendererGeneric::DrawRadioBitmap(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
762 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
764 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawRadioBitmap");
767 void wxRendererGeneric::DrawTextCtrl(wxWindow
* WXUNUSED(win
), wxDC
& WXUNUSED(dc
),
768 const wxRect
& WXUNUSED(rect
), int WXUNUSED(flags
))
770 wxFAIL_MSG("UNIMPLEMENTED: wxRendererGeneric::DrawTextCtrl");
773 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
775 void wxRendererGeneric::DrawTitleBarBitmap(wxWindow
* WXUNUSED(win
),
777 const wxRect
& WXUNUSED(rect
),
778 wxTitleBarButton
WXUNUSED(button
),
781 // no need to fail here, if wxHAS_DRAW_TITLE_BAR_BITMAP is defined this
782 // will be implemented in the native renderer and this version is never
783 // going to be used -- but we still need to define it to allow
784 // instantiation of this class (which would have been pure virtual
788 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
791 // ----------------------------------------------------------------------------
792 // A module to allow cleanup of generic renderer.
793 // ----------------------------------------------------------------------------
795 class wxGenericRendererModule
: public wxModule
797 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
799 wxGenericRendererModule() {}
800 bool OnInit() { return true; }
801 void OnExit() { wxRendererGeneric::Cleanup(); }
804 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)