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 int CORNER
= 1;
223 const wxCoord x
= rect
.x
,
228 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
229 dc
.SetPen(*wxTRANSPARENT_PEN
);
230 dc
.DrawRectangle(rect
);
232 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
234 dc
.SetPen(m_penBlack
);
235 dc
.DrawLine( x
+w
-CORNER
+1, y
, x
+w
, y
+h
); // right (outer)
236 dc
.DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
238 dc
.SetPen(m_penDarkGrey
);
239 dc
.DrawLine( x
+w
-CORNER
, y
, x
+w
-1, y
+h
); // right (inner)
240 dc
.DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
242 dc
.SetPen(m_penHighlight
);
243 dc
.DrawRectangle( x
, y
, w
-CORNER
+1, 1 ); // top (outer)
244 dc
.DrawRectangle( x
, y
, 1, h
); // left (outer)
245 dc
.DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
246 dc
.DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
248 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
253 wxRendererGeneric::DrawHeaderButtonContents(wxWindow
*win
,
257 wxHeaderSortIconType sortArrow
,
258 wxHeaderButtonParams
* params
)
262 // Mark this item as selected. For the generic version we'll just draw an
264 if ( flags
& wxCONTROL_SELECTED
)
266 // draw a line at the bottom of the header button, overlaying the
267 // native hot-tracking line (on XP)
268 const int penwidth
= 3;
269 int y
= rect
.y
+ rect
.height
+ 1 - penwidth
;
270 wxColour c
= (params
&& params
->m_selectionColour
.Ok()) ?
271 params
->m_selectionColour
: wxColour(0x66, 0x66, 0x66);
272 wxPen
pen(c
, penwidth
);
273 pen
.SetCap(wxCAP_BUTT
);
275 dc
.DrawLine(rect
.x
, y
, rect
.x
+ rect
.width
, y
);
278 // Draw an up or down arrow
280 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
284 // make a rect for the arrow
287 ar
.y
+= (rect
.height
- ar
.height
)/2;
288 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
289 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
292 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
294 triPt
[0].x
= ar
.width
/ 2;
296 triPt
[1].x
= ar
.width
;
297 triPt
[1].y
= ar
.height
;
299 triPt
[2].y
= ar
.height
;
305 triPt
[1].x
= ar
.width
;
307 triPt
[2].x
= ar
.width
/ 2;
308 triPt
[2].y
= ar
.height
;
311 wxColour c
= (params
&& params
->m_arrowColour
.Ok()) ?
312 params
->m_arrowColour
: wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
314 dc
.SetBrush(wxBrush(c
));
315 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
317 labelWidth
+= arrowSpace
;
319 const int margin
= 5; // number of pixels to reserve on either side of the label
323 if ( params
&& params
->m_labelBitmap
.Ok() )
324 bmpWidth
= params
->m_labelBitmap
.GetWidth() + 2;
326 labelWidth
+= bmpWidth
+ 2*margin
;
328 // Draw a label if one is given
329 if ( params
&& !params
->m_labelText
.empty() )
331 wxFont font
= params
->m_labelFont
.Ok() ?
332 params
->m_labelFont
: win
->GetFont();
333 wxColour clr
= params
->m_labelColour
.Ok() ?
334 params
->m_labelColour
: win
->GetForegroundColour();
336 wxString
label( params
->m_labelText
);
339 dc
.SetTextForeground(clr
);
340 dc
.SetBackgroundMode(wxTRANSPARENT
);
342 int tw
, th
, td
, x
, y
;
343 dc
.GetTextExtent( label
, &tw
, &th
, &td
);
345 y
= rect
.y
+ wxMax(0, (rect
.height
- (th
+td
)) / 2);
347 // truncate and add an ellipsis (...) if the text is too wide.
348 int targetWidth
= rect
.width
- arrowSpace
- bmpWidth
- 2*margin
;
349 if ( tw
> targetWidth
)
352 dc
.GetTextExtent( wxT("..."), &ellipsisWidth
, NULL
);
354 label
.Truncate( label
.length() - 1 );
355 dc
.GetTextExtent( label
, &tw
, &th
);
356 } while (tw
+ ellipsisWidth
> targetWidth
&& label
.length() );
357 label
.append( wxT("...") );
361 switch (params
->m_labelAlignment
)
368 x
= rect
.x
+ wxMax(0, (rect
.width
- arrowSpace
- tw
- bmpWidth
)/2);
371 x
= rect
.x
+ wxMax(0, rect
.width
- arrowSpace
- margin
- tw
- bmpWidth
);
375 dc
.DrawText(label
, x
, y
);
379 // draw the bitmap if there is one
380 if ( params
&& params
->m_labelBitmap
.Ok() )
383 w
= params
->m_labelBitmap
.GetWidth();
384 h
= params
->m_labelBitmap
.GetHeight();
386 y
= rect
.y
+ wxMax(1, (rect
.height
- h
) / 2);
388 // if there is a text label, then put the bitmap at the end of the label
393 // otherwise use the alignment flags
396 switch (params
->m_labelAlignment
)
403 x
= rect
.x
+ wxMax(1, (rect
.width
- arrowSpace
- w
)/2);
406 x
= rect
.x
+ wxMax(1, rect
.width
- arrowSpace
- margin
- w
);
410 dc
.DrawBitmap(params
->m_labelBitmap
, x
, y
, true);
416 int wxRendererGeneric::GetHeaderButtonHeight(wxWindow
*win
)
418 // Copied and adapted from src/generic/listctrl.cpp
419 const int HEADER_OFFSET_Y
= 1;
420 const int EXTRA_HEIGHT
= 4;
424 win
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
426 return h
+ d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
430 // draw the plus or minus sign
432 wxRendererGeneric::DrawTreeItemButton(wxWindow
* WXUNUSED(win
),
438 wxDCPenChanger
penChanger(dc
, *wxGREY_PEN
);
439 wxDCBrushChanger
brushChanger(dc
, *wxWHITE_BRUSH
);
441 dc
.DrawRectangle(rect
);
444 const wxCoord xMiddle
= rect
.x
+ rect
.width
/2;
445 const wxCoord yMiddle
= rect
.y
+ rect
.height
/2;
447 // half of the length of the horz lines in "-" and "+"
448 const wxCoord halfWidth
= rect
.width
/2 - 2;
449 dc
.SetPen(*wxBLACK_PEN
);
450 dc
.DrawLine(xMiddle
- halfWidth
, yMiddle
,
451 xMiddle
+ halfWidth
+ 1, yMiddle
);
453 if ( !(flags
& wxCONTROL_EXPANDED
) )
456 const wxCoord halfHeight
= rect
.height
/2 - 2;
457 dc
.DrawLine(xMiddle
, yMiddle
- halfHeight
,
458 xMiddle
, yMiddle
+ halfHeight
+ 1);
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 wxSplitterRenderParams
467 wxRendererGeneric::GetSplitterParams(const wxWindow
*win
)
473 if ( win
->HasFlag(wxSP_3DSASH
) )
475 else if ( win
->HasFlag(wxSP_NOSASH
) )
480 if ( win
->HasFlag(wxSP_3DBORDER
) )
485 return wxSplitterRenderParams(sashWidth
, border
, false);
489 wxRendererGeneric::DrawSplitterBorder(wxWindow
*win
,
491 const wxRect
& rectOrig
,
494 if ( win
->HasFlag(wxSP_3DBORDER
) )
496 wxRect rect
= rectOrig
;
497 DrawShadedRect(dc
, &rect
, m_penDarkGrey
, m_penHighlight
);
498 DrawShadedRect(dc
, &rect
, m_penBlack
, m_penLightGrey
);
503 wxRendererGeneric::DrawSplitterSash(wxWindow
*win
,
505 const wxSize
& sizeReal
,
507 wxOrientation orient
,
510 // to avoid duplicating the same code for horizontal and vertical sashes,
511 // simply mirror the DC instead if needed (i.e. if horz splitter)
512 wxMirrorDC
dc(dcReal
, orient
!= wxVERTICAL
);
513 wxSize size
= dc
.Reflect(sizeReal
);
516 // we draw a Win32-like grey sash with possible 3D border here:
518 // ---- this is position
523 // GWGGGDB where G is light grey (face)
524 // GWGGGDB W white (light)
525 // GWGGGDB D dark grey (shadow)
526 // GWGGGDB B black (dark shadow)
528 // GWGGGDB and lower letters are our border (already drawn)
532 // only the middle 3 columns are drawn unless wxSP_3D is specified
534 const wxCoord h
= size
.y
;
537 // If we're drawing the border, draw the sash 3d lines shorter
538 if ( win
->HasFlag(wxSP_3DBORDER
) )
543 dc
.SetPen(*wxTRANSPARENT_PEN
);
544 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)));
546 if ( win
->HasFlag(wxSP_3DSASH
) )
549 dc
.DrawRectangle(position
+ 2, 0, 3, h
);
551 dc
.SetPen(m_penLightGrey
);
552 dc
.DrawLine(position
, offset
, position
, h
- offset
);
554 dc
.SetPen(m_penHighlight
);
555 dc
.DrawLine(position
+ 1, 0, position
+ 1, h
);
557 dc
.SetPen(m_penDarkGrey
);
558 dc
.DrawLine(position
+ 5, 0, position
+ 5, h
);
560 dc
.SetPen(m_penBlack
);
561 dc
.DrawLine(position
+ 6, offset
, position
+ 6, h
- offset
);
566 dc
.DrawRectangle(position
, 0, 3, h
);
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
575 wxRendererGeneric::DrawComboBoxDropButton(wxWindow
*win
,
580 DrawPushButton(win
,dc
,rect
,flags
);
581 DrawDropArrow(win
,dc
,rect
,flags
);
585 wxRendererGeneric::DrawDropArrow(wxWindow
*win
,
590 // This generic implementation should be good
591 // enough for Windows platforms (including XP).
593 int arrowHalf
= rect
.width
/5;
594 int rectMid
= rect
.width
/ 2;
595 int arrowTopY
= (rect
.height
/2) - (arrowHalf
/2);
597 // This should always result in arrow with odd width.
600 wxPoint(rectMid
- arrowHalf
, arrowTopY
),
601 wxPoint(rectMid
+ arrowHalf
, arrowTopY
),
602 wxPoint(rectMid
, arrowTopY
+ arrowHalf
)
604 dc
.SetBrush(wxBrush(win
->GetForegroundColour()));
605 dc
.SetPen(wxPen(win
->GetForegroundColour()));
606 dc
.DrawPolygon(WXSIZEOF(pt
), pt
, rect
.x
, rect
.y
);
610 wxRendererGeneric::DrawCheckBox(wxWindow
*WXUNUSED(win
),
615 dc
.SetPen(*(flags
& wxCONTROL_DISABLED
? wxGREY_PEN
: wxBLACK_PEN
));
616 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
617 dc
.DrawRectangle(rect
);
619 if ( flags
& wxCONTROL_CHECKED
)
621 dc
.DrawCheckMark(rect
.Deflate(2, 2));
626 wxRendererGeneric::DrawPushButton(wxWindow
*win
,
631 // Don't try anything too fancy. It'll just turn out looking
632 // out-of-place on most platforms.
633 wxColour bgCol
= flags
& wxCONTROL_DISABLED
?
634 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
) :
635 win
->GetBackgroundColour();
636 dc
.SetBrush(wxBrush(bgCol
));
637 dc
.SetPen(wxPen(bgCol
));
638 dc
.DrawRectangle(rect
);
643 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* win
,
648 wxRendererGeneric::DrawItemSelectionRect(wxWindow
* WXUNUSED(win
),
655 if ( flags
& wxCONTROL_SELECTED
)
657 if ( flags
& wxCONTROL_FOCUSED
)
659 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
663 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
));
668 brush
= *wxTRANSPARENT_BRUSH
;
672 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
)
674 && IsControlActive( (ControlRef
)win
->GetHandle() )
677 dc
.SetPen( *wxBLACK_PEN
);
679 dc
.SetPen( *wxTRANSPARENT_PEN
);
681 dc
.DrawRectangle( rect
);
685 wxRendererGeneric::DrawFocusRect(wxWindow
* WXUNUSED(win
), wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(flags
))
687 // draw the pixels manually because the "dots" in wxPen with wxDOT style
688 // may be short traits and not really dots
690 // note that to behave in the same manner as DrawRect(), we must exclude
691 // the bottom and right borders from the rectangle
692 wxCoord x1
= rect
.GetLeft(),
694 x2
= rect
.GetRight(),
695 y2
= rect
.GetBottom();
697 dc
.SetPen(m_penBlack
);
700 dc
.SetLogicalFunction(wxCOPY
);
702 // this seems to be closer than what Windows does than wxINVERT although
703 // I'm still not sure if it's correct
704 dc
.SetLogicalFunction(wxAND_REVERSE
);
708 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
709 dc
.DrawPoint(z
, rect
.GetTop());
711 wxCoord shift
= z
== x2
? 0 : 1;
712 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
715 shift
= z
== y2
? 0 : 1;
716 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
719 shift
= z
== x1
? 0 : 1;
720 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
723 dc
.SetLogicalFunction(wxCOPY
);
726 // ----------------------------------------------------------------------------
727 // A module to allow cleanup of generic renderer.
728 // ----------------------------------------------------------------------------
730 class wxGenericRendererModule
: public wxModule
732 DECLARE_DYNAMIC_CLASS(wxGenericRendererModule
)
734 wxGenericRendererModule() {}
735 bool OnInit() { return true; }
736 void OnExit() { wxRendererGeneric::Cleanup(); }
739 IMPLEMENT_DYNAMIC_CLASS(wxGenericRendererModule
, wxModule
)