1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: univ/renderer.cpp
3 // Purpose: wxControlRenderer implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "renderer.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/control.h"
34 #include "wx/checklst.h"
35 #include "wx/listbox.h"
36 #include "wx/scrolbar.h"
42 #include "wx/univ/theme.h"
43 #include "wx/univ/renderer.h"
44 #include "wx/univ/colschem.h"
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
51 // wxRenderer: drawing helpers
52 // ----------------------------------------------------------------------------
54 void wxRenderer::StandardDrawFrame(wxDC
& dc
,
55 const wxRect
& rectFrame
,
56 const wxRect
& rectLabel
)
58 // draw left, bottom and right lines entirely
59 DrawVerticalLine(dc
, rectFrame
.GetLeft(),
60 rectFrame
.GetTop(), rectFrame
.GetBottom() - 2);
61 DrawHorizontalLine(dc
, rectFrame
.GetBottom() - 1,
62 rectFrame
.GetLeft(), rectFrame
.GetRight());
63 DrawVerticalLine(dc
, rectFrame
.GetRight() - 1,
64 rectFrame
.GetTop(), rectFrame
.GetBottom() - 1);
66 // and 2 parts of the top line
67 DrawHorizontalLine(dc
, rectFrame
.GetTop(),
68 rectFrame
.GetLeft() + 1, rectLabel
.GetLeft());
69 DrawHorizontalLine(dc
, rectFrame
.GetTop(),
70 rectLabel
.GetRight(), rectFrame
.GetRight() - 2);
74 void wxRenderer::StandardDrawTextLine(wxDC
& dc
,
77 int selStart
, int selEnd
,
80 if ( (selStart
== -1) || !(flags
& wxCONTROL_FOCUSED
) )
83 dc
.DrawText(text
, rect
.x
, rect
.y
);
85 else // we have selection
90 // draw the part before selection
91 wxString
s(text
, (size_t)selStart
);
94 dc
.DrawText(s
, x
, rect
.y
);
96 dc
.GetTextExtent(s
, &width
, NULL
);
100 // draw the selection itself
101 s
= wxString(text
.c_str() + selStart
, text
.c_str() + selEnd
);
104 wxColour colFg
= dc
.GetTextForeground(),
105 colBg
= dc
.GetTextBackground();
106 dc
.SetTextForeground(wxTHEME_COLOUR(HIGHLIGHT_TEXT
));
107 dc
.SetTextBackground(wxTHEME_COLOUR(HIGHLIGHT
));
108 dc
.SetBackgroundMode(wxSOLID
);
110 dc
.DrawText(s
, x
, rect
.y
);
111 dc
.GetTextExtent(s
, &width
, NULL
);
114 dc
.SetBackgroundMode(wxTRANSPARENT
);
115 dc
.SetTextBackground(colBg
);
116 dc
.SetTextForeground(colFg
);
119 // draw the final part
120 s
= text
.c_str() + selEnd
;
123 dc
.DrawText(s
, x
, rect
.y
);
128 // ----------------------------------------------------------------------------
129 // wxRenderer: scrollbar geometry
130 // ----------------------------------------------------------------------------
133 void wxRenderer::StandardScrollBarThumbSize(wxCoord length
,
140 // the thumb can't be made less than this number of pixels
141 static const wxCoord thumbMinWidth
= 8; // FIXME: should be configurable
143 *thumbStart
= (length
*thumbPos
) / range
;
144 *thumbEnd
= (length
*(thumbPos
+ thumbSize
)) / range
;
146 if ( *thumbEnd
- *thumbStart
< thumbMinWidth
)
148 // adjust the end if possible
149 if ( *thumbStart
<= length
- thumbMinWidth
)
151 // yes, just make it wider
152 *thumbEnd
= *thumbStart
+ thumbMinWidth
;
154 else // it is at the bottom of the scrollbar
156 // so move it a bit up
157 *thumbStart
= length
- thumbMinWidth
;
164 wxRect
wxRenderer::StandardGetScrollbarRect(const wxScrollBar
*scrollbar
,
165 wxScrollBar::Element elem
,
167 const wxSize
& sizeArrow
)
169 if ( thumbPos
== -1 )
171 thumbPos
= scrollbar
->GetThumbPosition();
174 wxSize sizeTotal
= scrollbar
->GetClientSize();
175 wxCoord
*start
, *width
, 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 wxRenderer::~wxRenderer()
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 wxControlRenderer::wxControlRenderer(wxWindow
*window
,
400 wxRenderer
*renderer
)
404 m_renderer
= renderer
;
406 wxSize size
= m_window
->GetClientSize();
409 m_rect
.width
= size
.x
;
410 m_rect
.height
= size
.y
;
413 void wxControlRenderer::DrawLabel(const wxBitmap
& bitmap
,
414 wxCoord marginX
, wxCoord marginY
)
416 m_dc
.SetFont(m_window
->GetFont());
417 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
419 wxString label
= m_window
->GetLabel();
420 if ( !label
.empty() || bitmap
.Ok() )
422 wxRect rectLabel
= m_rect
;
425 rectLabel
.Inflate(-marginX
, -marginY
);
428 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
430 m_renderer
->DrawButtonLabel(m_dc
,
434 m_window
->GetStateFlags(),
435 ctrl
->GetAlignment(),
436 ctrl
->GetAccelIndex());
440 void wxControlRenderer::DrawFrame()
442 m_dc
.SetFont(m_window
->GetFont());
443 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
444 m_dc
.SetTextBackground(m_window
->GetBackgroundColour());
446 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
448 m_renderer
->DrawFrame(m_dc
,
449 m_window
->GetLabel(),
451 m_window
->GetStateFlags(),
452 ctrl
->GetAlignment(),
453 ctrl
->GetAccelIndex());
456 void wxControlRenderer::DrawButtonBorder()
458 int flags
= m_window
->GetStateFlags();
460 m_renderer
->DrawButtonBorder(m_dc
, m_rect
, flags
, &m_rect
);
462 m_renderer
->DrawBackground(m_dc
, wxTHEME_BG_COLOUR(m_window
), m_rect
, flags
);
465 void wxControlRenderer::DrawBitmap(const wxBitmap
& bitmap
)
467 int style
= m_window
->GetWindowStyle();
468 DrawBitmap(m_dc
, bitmap
, m_rect
,
469 style
& wxALIGN_MASK
,
470 style
& wxBI_EXPAND
? wxEXPAND
: wxSTRETCH_NOT
);
474 void wxControlRenderer::DrawBitmap(wxDC
&dc
,
475 const wxBitmap
& bitmap
,
480 // we may change the bitmap if we stretch it
481 wxBitmap bmp
= bitmap
;
485 int width
= bmp
.GetWidth(),
486 height
= bmp
.GetHeight();
490 if ( stretch
& wxTILE
)
493 for ( ; x
< rect
.width
; x
+= width
)
495 for ( y
= 0; y
< rect
.height
; y
+= height
)
497 // no need to use mask here as we cover the entire window area
498 dc
.DrawBitmap(bmp
, x
, y
);
502 else if ( stretch
& wxEXPAND
)
504 // stretch bitmap to fill the entire control
505 bmp
= wxImage(bmp
).Scale(rect
.width
, rect
.height
).ConvertToBitmap();
507 else // not stretched, not tiled
509 if ( alignment
& wxALIGN_RIGHT
)
511 x
= rect
.GetRight() - width
;
513 else if ( alignment
& wxALIGN_CENTRE
)
515 x
= (rect
.GetLeft() + rect
.GetRight() - width
+ 1) / 2;
517 else // alignment & wxALIGN_LEFT
522 if ( alignment
& wxALIGN_BOTTOM
)
524 y
= rect
.GetBottom() - height
;
526 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
528 y
= (rect
.GetTop() + rect
.GetBottom() - height
+ 1) / 2;
530 else // alignment & wxALIGN_TOP
537 dc
.DrawBitmap(bmp
, x
, y
, TRUE
/* use mask */);
540 void wxControlRenderer::DrawScrollbar(const wxScrollBar
*scrollbar
,
543 // we will only redraw the parts which must be redrawn and not everything
544 wxRegion rgnUpdate
= scrollbar
->GetUpdateRegion();
547 wxRect rectUpdate
= rgnUpdate
.GetBox();
548 wxLogTrace(_T("scrollbar"),
549 _T("%s redraw: update box is (%d, %d)-(%d, %d)"),
550 scrollbar
->IsVertical() ? _T("vert") : _T("horz"),
551 rectUpdate
.GetLeft(),
553 rectUpdate
.GetRight(),
554 rectUpdate
.GetBottom());
556 #if 0 //def WXDEBUG_SCROLLBAR
557 static bool s_refreshDebug
= FALSE
;
558 if ( s_refreshDebug
)
560 wxClientDC
dc(wxConstCast(scrollbar
, wxScrollBar
));
561 dc
.SetBrush(*wxRED_BRUSH
);
562 dc
.SetPen(*wxTRANSPARENT_PEN
);
563 dc
.DrawRectangle(rectUpdate
);
565 // under Unix we use "--sync" X option for this
571 #endif // WXDEBUG_SCROLLBAR
574 wxOrientation orient
= scrollbar
->IsVertical() ? wxVERTICAL
578 for ( int nBar
= 0; nBar
< 2; nBar
++ )
580 wxScrollBar::Element elem
=
581 (wxScrollBar::Element
)(wxScrollBar::Element_Bar_1
+ nBar
);
583 wxRect rectBar
= m_renderer
->GetScrollbarRect(scrollbar
, elem
);
585 if ( rgnUpdate
.Contains(rectBar
) )
587 wxLogTrace(_T("scrollbar"),
588 _T("drawing bar part %d at (%d, %d)-(%d, %d)"),
593 rectBar
.GetBottom());
595 m_renderer
->DrawScrollbarShaft(m_dc
,
598 scrollbar
->GetState(elem
));
603 for ( int nArrow
= 0; nArrow
< 2; nArrow
++ )
605 wxScrollBar::Element elem
=
606 (wxScrollBar::Element
)(wxScrollBar::Element_Arrow_Line_1
+ nArrow
);
608 wxRect rectArrow
= m_renderer
->GetScrollbarRect(scrollbar
, elem
);
609 if ( rgnUpdate
.Contains(rectArrow
) )
611 wxLogTrace(_T("scrollbar"),
612 _T("drawing arrow %d at (%d, %d)-(%d, %d)"),
616 rectArrow
.GetRight(),
617 rectArrow
.GetBottom());
619 scrollbar
->GetArrows().DrawArrow
621 (wxScrollArrows::Arrow
)nArrow
,
624 TRUE
// draw a scrollbar arrow, not just an arrow
629 // TODO: support for page arrows
632 wxScrollBar::Element elem
= wxScrollBar::Element_Thumb
;
633 wxRect rectThumb
= m_renderer
->GetScrollbarRect(scrollbar
, elem
);
634 if ( rectThumb
.width
&& rectThumb
.height
&& rgnUpdate
.Contains(rectThumb
) )
636 wxLogTrace(_T("scrollbar"),
637 _T("drawing thumb at (%d, %d)-(%d, %d)"),
640 rectThumb
.GetRight(),
641 rectThumb
.GetBottom());
643 m_renderer
->DrawScrollbarThumb(m_dc
,
646 scrollbar
->GetState(elem
));
650 void wxControlRenderer::DrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
652 wxASSERT_MSG( x1
== x2
|| y1
== y2
,
653 _T("line must be either horizontal or vertical") );
656 m_renderer
->DrawVerticalLine(m_dc
, x1
, y1
, y2
);
658 m_renderer
->DrawHorizontalLine(m_dc
, y1
, x1
, x2
);
663 void wxControlRenderer::DrawItems(const wxListBox
*lbox
,
664 size_t itemFirst
, size_t itemLast
)
666 DoDrawItems(lbox
, itemFirst
, itemLast
);
669 void wxControlRenderer::DoDrawItems(const wxListBox
*lbox
,
670 size_t itemFirst
, size_t itemLast
,
673 // prepare for the drawing: calc the initial position
674 wxCoord lineHeight
= lbox
->GetLineHeight();
676 // note that SetClippingRegion() needs the physical (unscrolled)
677 // coordinates while we use the logical (scrolled) ones for the drawing
680 wxSize size
= lbox
->GetClientSize();
682 rect
.height
= size
.y
;
684 // keep the text inside the client rect or we will overwrite the vertical
685 // scrollbar for the long strings
686 m_dc
.SetClippingRegion(rect
.x
, rect
.y
, rect
.width
+ 1, rect
.height
+ 1);
688 // adjust the rect position now
689 lbox
->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
690 rect
.y
+= itemFirst
*lineHeight
;
691 rect
.height
= lineHeight
;
693 // the rect should go to the right visible border so adjust the width if x
694 // is shifted (rightmost point should stay the same)
695 rect
.width
-= rect
.x
;
697 // we'll keep the text colour unchanged
698 m_dc
.SetTextForeground(lbox
->GetForegroundColour());
700 // an item should have the focused rect only when the lbox has focus, so
701 // make sure that we never set wxCONTROL_FOCUSED flag if it doesn't
702 int itemCurrent
= wxWindow::FindFocus() == (wxWindow
*)lbox
// cast needed
703 ? lbox
->GetCurrentItem()
705 for ( size_t n
= itemFirst
; n
< itemLast
; n
++ )
708 if ( (int)n
== itemCurrent
)
709 flags
|= wxCONTROL_FOCUSED
;
710 if ( lbox
->IsSelected(n
) )
711 flags
|= wxCONTROL_SELECTED
;
713 #if wxUSE_CHECKLISTBOX
716 wxCheckListBox
*checklstbox
= wxStaticCast(lbox
, wxCheckListBox
);
717 if ( checklstbox
->IsChecked(n
) )
718 flags
|= wxCONTROL_CHECKED
;
720 m_renderer
->DrawCheckItem(m_dc
, lbox
->GetString(n
),
726 #endif // wxUSE_CHECKLISTBOX
728 m_renderer
->DrawItem(m_dc
, lbox
->GetString(n
), rect
, flags
);
731 rect
.y
+= lineHeight
;
735 #endif // wxUSE_LISTBOX
737 #if wxUSE_CHECKLISTBOX
739 void wxControlRenderer::DrawCheckItems(const wxCheckListBox
*lbox
,
740 size_t itemFirst
, size_t itemLast
)
742 DoDrawItems(lbox
, itemFirst
, itemLast
, TRUE
);
745 #endif // wxUSE_CHECKLISTBOX
749 void wxControlRenderer::DrawProgressBar(const wxGauge
*gauge
)
752 m_dc
.SetBrush(wxBrush(m_window
->GetBackgroundColour(), wxSOLID
));
753 m_dc
.SetPen(*wxTRANSPARENT_PEN
);
754 m_dc
.DrawRectangle(m_rect
);
756 int max
= gauge
->GetRange();
763 // calc the filled rect
764 int pos
= gauge
->GetValue();
765 int left
= max
- pos
;
767 wxRect rect
= m_rect
;
768 rect
.Deflate(1); // FIXME this depends on the border width
770 m_dc
.SetBrush(wxBrush(m_window
->GetForegroundColour(), wxSOLID
));
772 if ( gauge
->IsSmooth() )
774 // just draw the rectangle in one go
775 if ( gauge
->IsVertical() )
777 // vert bars grow from bottom to top
778 wxCoord dy
= ((rect
.height
- 1) * left
) / max
;
784 // grow from left to right
785 rect
.width
-= ((rect
.width
- 1) * left
) / max
;
788 m_dc
.DrawRectangle(rect
);
792 wxSize sizeStep
= m_renderer
->GetProgressBarStep();
793 int step
= gauge
->IsVertical() ? sizeStep
.y
: sizeStep
.x
;
795 // we divide by it below!
796 wxCHECK_RET( step
, _T("invalid wxGauge step") );
798 // round up to make the progress appear to start faster
799 int lenTotal
= gauge
->IsVertical() ? rect
.height
: rect
.width
;
800 int steps
= ((lenTotal
+ step
- 1) * pos
) / (max
* step
);
802 // calc the coords of one small rect
804 if ( gauge
->IsVertical() )
806 // draw from bottom to top: so first set y to the bottom
807 rect
.y
+= rect
.height
- 1;
809 // then adjust the height
812 // and then adjust y again to be what it should for the first rect
813 rect
.y
-= rect
.height
;
818 // remember that this will be the coord which will change
826 // don't leave 2 empty pixels in the beginning
836 for ( int n
= 0; n
< steps
; n
++ )
838 wxRect rectSegment
= rect
;
839 rectSegment
.Deflate(dx
, dy
);
841 m_dc
.DrawRectangle(rectSegment
);
846 // this can only happen for the last step of vertical gauge
847 rect
.height
= *px
- step
- 1;
850 else if ( *px
> lenTotal
- step
)
852 // this can only happen for the last step of horizontal gauge
853 rect
.width
= lenTotal
- *px
- 1;
859 #endif // wxUSE_GAUGE