1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/ctrlrend.cpp
3 // Purpose: wxControlRenderer implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/control.h"
30 #include "wx/checklst.h"
31 #include "wx/listbox.h"
32 #include "wx/scrolbar.h"
39 #include "wx/univ/theme.h"
40 #include "wx/univ/renderer.h"
41 #include "wx/univ/colschem.h"
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
48 // wxRenderer: drawing helpers
49 // ----------------------------------------------------------------------------
51 void wxRenderer::StandardDrawFrame(wxDC
& dc
,
52 const wxRect
& rectFrame
,
53 const wxRect
& rectLabel
)
55 // draw left, bottom and right lines entirely
56 DrawVerticalLine(dc
, rectFrame
.GetLeft(),
57 rectFrame
.GetTop(), rectFrame
.GetBottom() - 2);
58 DrawHorizontalLine(dc
, rectFrame
.GetBottom() - 1,
59 rectFrame
.GetLeft(), rectFrame
.GetRight());
60 DrawVerticalLine(dc
, rectFrame
.GetRight() - 1,
61 rectFrame
.GetTop(), rectFrame
.GetBottom() - 1);
63 // and 2 parts of the top line
64 DrawHorizontalLine(dc
, rectFrame
.GetTop(),
65 rectFrame
.GetLeft() + 1, rectLabel
.GetLeft());
66 DrawHorizontalLine(dc
, rectFrame
.GetTop(),
67 rectLabel
.GetRight(), rectFrame
.GetRight() - 2);
71 void wxRenderer::StandardDrawTextLine(wxDC
& dc
,
74 int selStart
, int selEnd
,
77 if ( (selStart
== -1) || !(flags
& wxCONTROL_FOCUSED
) )
80 dc
.DrawText(text
, rect
.x
, rect
.y
);
82 else // we have selection
87 // draw the part before selection
88 wxString
s(text
, (size_t)selStart
);
91 dc
.DrawText(s
, x
, rect
.y
);
93 dc
.GetTextExtent(s
, &width
, NULL
);
97 // draw the selection itself
98 s
= wxString(text
.c_str() + selStart
, text
.c_str() + selEnd
);
101 wxColour colFg
= dc
.GetTextForeground(),
102 colBg
= dc
.GetTextBackground();
103 dc
.SetTextForeground(wxTHEME_COLOUR(HIGHLIGHT_TEXT
));
104 dc
.SetTextBackground(wxTHEME_COLOUR(HIGHLIGHT
));
105 dc
.SetBackgroundMode(wxSOLID
);
107 dc
.DrawText(s
, x
, rect
.y
);
108 dc
.GetTextExtent(s
, &width
, NULL
);
111 dc
.SetBackgroundMode(wxTRANSPARENT
);
112 dc
.SetTextBackground(colBg
);
113 dc
.SetTextForeground(colFg
);
116 // draw the final part
117 s
= text
.c_str() + selEnd
;
120 dc
.DrawText(s
, x
, rect
.y
);
125 // ----------------------------------------------------------------------------
126 // wxRenderer: scrollbar geometry
127 // ----------------------------------------------------------------------------
132 void wxRenderer::StandardScrollBarThumbSize(wxCoord length
,
139 // the thumb can't be made less than this number of pixels
140 static const wxCoord thumbMinWidth
= 8; // FIXME: should be configurable
142 *thumbStart
= (length
*thumbPos
) / range
;
143 *thumbEnd
= (length
*(thumbPos
+ thumbSize
)) / range
;
145 if ( *thumbEnd
- *thumbStart
< thumbMinWidth
)
147 // adjust the end if possible
148 if ( *thumbStart
<= length
- thumbMinWidth
)
150 // yes, just make it wider
151 *thumbEnd
= *thumbStart
+ thumbMinWidth
;
153 else // it is at the bottom of the scrollbar
155 // so move it a bit up
156 *thumbStart
= length
- thumbMinWidth
;
163 wxRect
wxRenderer::StandardGetScrollbarRect(const wxScrollBar
*scrollbar
,
164 wxScrollBar::Element elem
,
166 const wxSize
& sizeArrow
)
168 if ( thumbPos
== -1 )
170 thumbPos
= scrollbar
->GetThumbPosition();
173 wxSize sizeTotal
= scrollbar
->GetClientSize();
174 wxCoord
*start
, *width
;
175 wxCoord length
, arrow
;
177 if ( scrollbar
->IsVertical() )
180 rect
.width
= sizeTotal
.x
;
181 length
= sizeTotal
.y
;
183 width
= &rect
.height
;
189 rect
.height
= sizeTotal
.y
;
190 length
= sizeTotal
.x
;
198 case wxScrollBar::Element_Arrow_Line_1
:
203 case wxScrollBar::Element_Arrow_Line_2
:
204 *start
= length
- arrow
;
208 case wxScrollBar::Element_Arrow_Page_1
:
209 case wxScrollBar::Element_Arrow_Page_2
:
210 // we don't have them at all
213 case wxScrollBar::Element_Thumb
:
214 case wxScrollBar::Element_Bar_1
:
215 case wxScrollBar::Element_Bar_2
:
216 // we need to calculate the thumb position - do it
219 wxCoord thumbStart
, thumbEnd
;
220 int range
= scrollbar
->GetRange();
228 StandardScrollBarThumbSize(length
,
230 scrollbar
->GetThumbSize(),
236 if ( elem
== wxScrollBar::Element_Thumb
)
239 *width
= thumbEnd
- thumbStart
;
241 else if ( elem
== wxScrollBar::Element_Bar_1
)
246 else // elem == wxScrollBar::Element_Bar_2
249 *width
= length
- thumbEnd
;
252 // everything is relative to the start of the shaft so far
257 case wxScrollBar::Element_Max
:
259 wxFAIL_MSG( _T("unknown scrollbar element") );
266 wxCoord
wxRenderer::StandardScrollBarSize(const wxScrollBar
*scrollbar
,
267 const wxSize
& sizeArrowSB
)
269 wxCoord sizeArrow
, sizeTotal
;
270 if ( scrollbar
->GetWindowStyle() & wxVERTICAL
)
272 sizeArrow
= sizeArrowSB
.y
;
273 sizeTotal
= scrollbar
->GetSize().y
;
277 sizeArrow
= sizeArrowSB
.x
;
278 sizeTotal
= scrollbar
->GetSize().x
;
281 return sizeTotal
- 2*sizeArrow
;
285 wxCoord
wxRenderer::StandardScrollbarToPixel(const wxScrollBar
*scrollbar
,
287 const wxSize
& sizeArrow
)
289 int range
= scrollbar
->GetRange();
292 // the only valid position anyhow
296 if ( thumbPos
== -1 )
298 // by default use the current thumb position
299 thumbPos
= scrollbar
->GetThumbPosition();
302 return ( thumbPos
*StandardScrollBarSize(scrollbar
, sizeArrow
) ) / range
303 + (scrollbar
->IsVertical() ? sizeArrow
.y
: sizeArrow
.x
);
307 int wxRenderer::StandardPixelToScrollbar(const wxScrollBar
*scrollbar
,
309 const wxSize
& sizeArrow
)
311 return ( (coord
- (scrollbar
->IsVertical() ? sizeArrow
.y
: sizeArrow
.x
)) *
312 scrollbar
->GetRange() ) /
313 StandardScrollBarSize(scrollbar
, sizeArrow
);
317 wxHitTest
wxRenderer::StandardHitTestScrollbar(const wxScrollBar
*scrollbar
,
319 const wxSize
& sizeArrowSB
)
321 // we only need to work with either x or y coord depending on the
322 // orientation, choose one (but still check the other one to verify if the
323 // mouse is in the window at all)
324 wxCoord coord
, sizeArrow
, sizeTotal
;
325 wxSize size
= scrollbar
->GetSize();
326 if ( scrollbar
->GetWindowStyle() & wxVERTICAL
)
328 if ( pt
.x
< 0 || pt
.x
> size
.x
)
332 sizeArrow
= sizeArrowSB
.y
;
337 if ( pt
.y
< 0 || pt
.y
> size
.y
)
341 sizeArrow
= sizeArrowSB
.x
;
345 // test for the arrows first as it's faster
346 if ( coord
< 0 || coord
> sizeTotal
)
350 else if ( coord
< sizeArrow
)
352 return wxHT_SCROLLBAR_ARROW_LINE_1
;
354 else if ( coord
> sizeTotal
- sizeArrow
)
356 return wxHT_SCROLLBAR_ARROW_LINE_2
;
360 // calculate the thumb position in pixels
361 sizeTotal
-= 2*sizeArrow
;
362 wxCoord thumbStart
, thumbEnd
;
363 int range
= scrollbar
->GetRange();
366 // clicking the scrollbar without range has no effect
371 StandardScrollBarThumbSize(sizeTotal
,
372 scrollbar
->GetThumbPosition(),
373 scrollbar
->GetThumbSize(),
379 // now compare with the thumb position
381 if ( coord
< thumbStart
)
382 return wxHT_SCROLLBAR_BAR_1
;
383 else if ( coord
> thumbEnd
)
384 return wxHT_SCROLLBAR_BAR_2
;
386 return wxHT_SCROLLBAR_THUMB
;
390 #endif // wxUSE_SCROLLBAR
392 wxRenderer::~wxRenderer()
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 wxControlRenderer::wxControlRenderer(wxWindow
*window
,
402 wxRenderer
*renderer
)
406 m_renderer
= renderer
;
408 wxSize size
= m_window
->GetClientSize();
411 m_rect
.width
= size
.x
;
412 m_rect
.height
= size
.y
;
415 void wxControlRenderer::DrawLabel(const wxBitmap
& bitmap
,
416 wxCoord marginX
, wxCoord marginY
)
418 m_dc
.SetBackgroundMode(wxTRANSPARENT
);
419 m_dc
.SetFont(m_window
->GetFont());
420 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
422 wxString label
= m_window
->GetLabel();
423 if ( !label
.empty() || bitmap
.Ok() )
425 wxRect rectLabel
= m_rect
;
428 rectLabel
.Inflate(-marginX
, -marginY
);
431 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
433 m_renderer
->DrawButtonLabel(m_dc
,
437 m_window
->GetStateFlags(),
438 ctrl
->GetAlignment(),
439 ctrl
->GetAccelIndex());
443 void wxControlRenderer::DrawFrame()
445 m_dc
.SetFont(m_window
->GetFont());
446 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
447 m_dc
.SetTextBackground(m_window
->GetBackgroundColour());
449 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
451 m_renderer
->DrawFrame(m_dc
,
452 m_window
->GetLabel(),
454 m_window
->GetStateFlags(),
455 ctrl
->GetAlignment(),
456 ctrl
->GetAccelIndex());
459 void wxControlRenderer::DrawButtonBorder()
461 int flags
= m_window
->GetStateFlags();
463 m_renderer
->DrawButtonBorder(m_dc
, m_rect
, flags
, &m_rect
);
466 // m_renderer->DrawButtonSurface(m_dc, wxTHEME_BG_COLOUR(m_window), m_rect, flags );
469 void wxControlRenderer::DrawBitmap(const wxBitmap
& bitmap
)
471 int style
= m_window
->GetWindowStyle();
472 DrawBitmap(m_dc
, bitmap
, m_rect
,
473 style
& wxALIGN_MASK
,
474 style
& wxBI_EXPAND
? wxEXPAND
: wxSTRETCH_NOT
);
478 void wxControlRenderer::DrawBitmap(wxDC
&dc
,
479 const wxBitmap
& bitmap
,
484 // we may change the bitmap if we stretch it
485 wxBitmap bmp
= bitmap
;
489 int width
= bmp
.GetWidth(),
490 height
= bmp
.GetHeight();
494 if ( stretch
& wxTILE
)
497 for ( ; x
< rect
.width
; x
+= width
)
499 for ( y
= 0; y
< rect
.height
; y
+= height
)
501 // no need to use mask here as we cover the entire window area
502 dc
.DrawBitmap(bmp
, x
, y
);
507 else if ( stretch
& wxEXPAND
)
509 // stretch bitmap to fill the entire control
510 bmp
= wxBitmap(wxImage(bmp
.ConvertToImage()).Scale(rect
.width
, rect
.height
));
512 #endif // wxUSE_IMAGE
513 else // not stretched, not tiled
515 if ( alignment
& wxALIGN_RIGHT
)
517 x
= rect
.GetRight() - width
;
519 else if ( alignment
& wxALIGN_CENTRE
)
521 x
= (rect
.GetLeft() + rect
.GetRight() - width
+ 1) / 2;
523 else // alignment & wxALIGN_LEFT
528 if ( alignment
& wxALIGN_BOTTOM
)
530 y
= rect
.GetBottom() - height
;
532 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
534 y
= (rect
.GetTop() + rect
.GetBottom() - height
+ 1) / 2;
536 else // alignment & wxALIGN_TOP
543 dc
.DrawBitmap(bmp
, x
, y
, true /* use mask */);
548 void wxControlRenderer::DrawScrollbar(const wxScrollBar
*scrollbar
,
549 int WXUNUSED(thumbPosOld
))
551 // we will only redraw the parts which must be redrawn and not everything
552 wxRegion rgnUpdate
= scrollbar
->GetUpdateRegion();
555 wxRect rectUpdate
= rgnUpdate
.GetBox();
556 wxLogTrace(_T("scrollbar"),
557 _T("%s redraw: update box is (%d, %d)-(%d, %d)"),
558 scrollbar
->IsVertical() ? _T("vert") : _T("horz"),
559 rectUpdate
.GetLeft(),
561 rectUpdate
.GetRight(),
562 rectUpdate
.GetBottom());
564 #if 0 //def WXDEBUG_SCROLLBAR
565 static bool s_refreshDebug
= false;
566 if ( s_refreshDebug
)
568 wxClientDC
dc(wxConstCast(scrollbar
, wxScrollBar
));
569 dc
.SetBrush(*wxRED_BRUSH
);
570 dc
.SetPen(*wxTRANSPARENT_PEN
);
571 dc
.DrawRectangle(rectUpdate
);
573 // under Unix we use "--sync" X option for this
579 #endif // WXDEBUG_SCROLLBAR
582 wxOrientation orient
= scrollbar
->IsVertical() ? wxVERTICAL
586 for ( int nBar
= 0; nBar
< 2; nBar
++ )
588 wxScrollBar::Element elem
=
589 (wxScrollBar::Element
)(wxScrollBar::Element_Bar_1
+ nBar
);
591 wxRect rectBar
= m_renderer
->GetScrollbarRect(scrollbar
, elem
);
593 if ( rgnUpdate
.Contains(rectBar
) )
595 wxLogTrace(_T("scrollbar"),
596 _T("drawing bar part %d at (%d, %d)-(%d, %d)"),
601 rectBar
.GetBottom());
603 m_renderer
->DrawScrollbarShaft(m_dc
,
606 scrollbar
->GetState(elem
));
611 for ( int nArrow
= 0; nArrow
< 2; nArrow
++ )
613 wxScrollBar::Element elem
=
614 (wxScrollBar::Element
)(wxScrollBar::Element_Arrow_Line_1
+ nArrow
);
616 wxRect rectArrow
= m_renderer
->GetScrollbarRect(scrollbar
, elem
);
617 if ( rgnUpdate
.Contains(rectArrow
) )
619 wxLogTrace(_T("scrollbar"),
620 _T("drawing arrow %d at (%d, %d)-(%d, %d)"),
624 rectArrow
.GetRight(),
625 rectArrow
.GetBottom());
627 scrollbar
->GetArrows().DrawArrow
629 (wxScrollArrows::Arrow
)nArrow
,
632 true // draw a scrollbar arrow, not just an arrow
637 // TODO: support for page arrows
640 wxScrollBar::Element elem
= wxScrollBar::Element_Thumb
;
641 wxRect rectThumb
= m_renderer
->GetScrollbarRect(scrollbar
, elem
);
642 if ( rectThumb
.width
&& rectThumb
.height
&& rgnUpdate
.Contains(rectThumb
) )
644 wxLogTrace(_T("scrollbar"),
645 _T("drawing thumb at (%d, %d)-(%d, %d)"),
648 rectThumb
.GetRight(),
649 rectThumb
.GetBottom());
651 m_renderer
->DrawScrollbarThumb(m_dc
,
654 scrollbar
->GetState(elem
));
658 #endif // wxUSE_SCROLLBAR
660 void wxControlRenderer::DrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
662 wxASSERT_MSG( x1
== x2
|| y1
== y2
,
663 _T("line must be either horizontal or vertical") );
666 m_renderer
->DrawVerticalLine(m_dc
, x1
, y1
, y2
);
668 m_renderer
->DrawHorizontalLine(m_dc
, y1
, x1
, x2
);
673 void wxControlRenderer::DrawItems(const wxListBox
*lbox
,
674 size_t itemFirst
, size_t itemLast
)
676 DoDrawItems(lbox
, itemFirst
, itemLast
);
679 void wxControlRenderer::DoDrawItems(const wxListBox
*lbox
,
680 size_t itemFirst
, size_t itemLast
,
681 #if wxUSE_CHECKLISTBOX
684 bool WXUNUSED(isCheckLbox
))
687 // prepare for the drawing: calc the initial position
688 wxCoord lineHeight
= lbox
->GetLineHeight();
690 // note that SetClippingRegion() needs the physical (unscrolled)
691 // coordinates while we use the logical (scrolled) ones for the drawing
694 wxSize size
= lbox
->GetClientSize();
696 rect
.height
= size
.y
;
698 // keep the text inside the client rect or we will overwrite the vertical
699 // scrollbar for the long strings
700 m_dc
.SetClippingRegion(rect
.x
, rect
.y
, rect
.width
+ 1, rect
.height
+ 1);
702 // adjust the rect position now
703 lbox
->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
704 rect
.y
+= itemFirst
*lineHeight
;
705 rect
.height
= lineHeight
;
707 // the rect should go to the right visible border so adjust the width if x
708 // is shifted (rightmost point should stay the same)
709 rect
.width
-= rect
.x
;
711 // we'll keep the text colour unchanged
712 m_dc
.SetTextForeground(lbox
->GetForegroundColour());
714 // an item should have the focused rect only when the lbox has focus, so
715 // make sure that we never set wxCONTROL_FOCUSED flag if it doesn't
716 int itemCurrent
= wxWindow::FindFocus() == (wxWindow
*)lbox
// cast needed
717 ? lbox
->GetCurrentItem()
719 for ( size_t n
= itemFirst
; n
< itemLast
; n
++ )
722 if ( (int)n
== itemCurrent
)
723 flags
|= wxCONTROL_FOCUSED
;
724 if ( lbox
->IsSelected(n
) )
725 flags
|= wxCONTROL_SELECTED
;
727 #if wxUSE_CHECKLISTBOX
730 wxCheckListBox
*checklstbox
= wxStaticCast(lbox
, wxCheckListBox
);
731 if ( checklstbox
->IsChecked(n
) )
732 flags
|= wxCONTROL_CHECKED
;
734 m_renderer
->DrawCheckItem(m_dc
, lbox
->GetString(n
),
740 #endif // wxUSE_CHECKLISTBOX
742 m_renderer
->DrawItem(m_dc
, lbox
->GetString(n
), rect
, flags
);
745 rect
.y
+= lineHeight
;
749 #endif // wxUSE_LISTBOX
751 #if wxUSE_CHECKLISTBOX
753 void wxControlRenderer::DrawCheckItems(const wxCheckListBox
*lbox
,
754 size_t itemFirst
, size_t itemLast
)
756 DoDrawItems(lbox
, itemFirst
, itemLast
, true);
759 #endif // wxUSE_CHECKLISTBOX
763 void wxControlRenderer::DrawProgressBar(const wxGauge
*gauge
)
766 m_dc
.SetBrush(wxBrush(m_window
->GetBackgroundColour(), wxSOLID
));
767 m_dc
.SetPen(*wxTRANSPARENT_PEN
);
768 m_dc
.DrawRectangle(m_rect
);
770 int max
= gauge
->GetRange();
777 // calc the filled rect
778 int pos
= gauge
->GetValue();
779 int left
= max
- pos
;
781 wxRect rect
= m_rect
;
782 rect
.Deflate(1); // FIXME this depends on the border width
784 wxColour col
= m_window
->UseFgCol() ? m_window
->GetForegroundColour()
785 : wxTHEME_COLOUR(GAUGE
);
786 m_dc
.SetBrush(wxBrush(col
, wxSOLID
));
788 if ( gauge
->IsSmooth() )
790 // just draw the rectangle in one go
791 if ( gauge
->IsVertical() )
793 // vert bars grow from bottom to top
794 wxCoord dy
= ((rect
.height
- 1) * left
) / max
;
800 // grow from left to right
801 rect
.width
-= ((rect
.width
- 1) * left
) / max
;
804 m_dc
.DrawRectangle(rect
);
808 wxSize sizeStep
= m_renderer
->GetProgressBarStep();
809 int step
= gauge
->IsVertical() ? sizeStep
.y
: sizeStep
.x
;
811 // we divide by it below!
812 wxCHECK_RET( step
, _T("invalid wxGauge step") );
814 // round up to make the progress appear to start faster
815 int lenTotal
= gauge
->IsVertical() ? rect
.height
: rect
.width
;
816 int steps
= ((lenTotal
+ step
- 1) * pos
) / (max
* step
);
818 // calc the coords of one small rect
821 if ( gauge
->IsVertical() )
823 // draw from bottom to top: so first set y to the bottom
824 rect
.y
+= rect
.height
- 1;
826 // then adjust the height
829 // and then adjust y again to be what it should for the first rect
830 rect
.y
-= rect
.height
;
835 // remember that this will be the coord which will change
843 // don't leave 2 empty pixels in the beginning
853 for ( int n
= 0; n
< steps
; n
++ )
855 wxRect rectSegment
= rect
;
856 rectSegment
.Deflate(dx
, dy
);
858 m_dc
.DrawRectangle(rectSegment
);
863 // this can only happen for the last step of vertical gauge
864 rect
.height
= *px
- step
- 1;
867 else if ( *px
> lenTotal
- step
)
869 // this can only happen for the last step of horizontal gauge
870 rect
.width
= lenTotal
- *px
- 1;
876 #endif // wxUSE_GAUGE