]>
git.saurik.com Git - wxWidgets.git/blob - src/univ/ctrlrend.cpp
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 wxRenderer::~wxRenderer()
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 wxControlRenderer::wxControlRenderer(wxWindow
*window
,
61 m_renderer
= renderer
;
63 wxSize size
= m_window
->GetClientSize();
66 m_rect
.width
= size
.x
;
67 m_rect
.height
= size
.y
;
70 void wxControlRenderer::DrawLabel(const wxBitmap
& bitmap
,
71 wxCoord marginX
, wxCoord marginY
)
73 m_dc
.SetBackgroundMode(wxTRANSPARENT
);
74 m_dc
.SetFont(m_window
->GetFont());
75 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
77 wxString label
= m_window
->GetLabel();
78 if ( !label
.empty() || bitmap
.Ok() )
80 wxRect rectLabel
= m_rect
;
83 rectLabel
.Inflate(-marginX
, -marginY
);
86 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
88 m_renderer
->DrawButtonLabel(m_dc
,
92 m_window
->GetStateFlags(),
94 ctrl
->GetAccelIndex());
98 void wxControlRenderer::DrawFrame()
100 m_dc
.SetFont(m_window
->GetFont());
101 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
102 m_dc
.SetTextBackground(m_window
->GetBackgroundColour());
104 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
106 m_renderer
->DrawFrame(m_dc
,
107 m_window
->GetLabel(),
109 m_window
->GetStateFlags(),
110 ctrl
->GetAlignment(),
111 ctrl
->GetAccelIndex());
114 void wxControlRenderer::DrawButtonBorder()
116 int flags
= m_window
->GetStateFlags();
118 m_renderer
->DrawButtonBorder(m_dc
, m_rect
, flags
, &m_rect
);
121 // m_renderer->DrawButtonSurface(m_dc, wxTHEME_BG_COLOUR(m_window), m_rect, flags );
124 void wxControlRenderer::DrawBitmap(const wxBitmap
& bitmap
)
126 int style
= m_window
->GetWindowStyle();
127 DrawBitmap(m_dc
, bitmap
, m_rect
,
128 style
& wxALIGN_MASK
,
129 style
& wxBI_EXPAND
? wxEXPAND
: wxSTRETCH_NOT
);
133 void wxControlRenderer::DrawBitmap(wxDC
&dc
,
134 const wxBitmap
& bitmap
,
139 // we may change the bitmap if we stretch it
140 wxBitmap bmp
= bitmap
;
144 int width
= bmp
.GetWidth(),
145 height
= bmp
.GetHeight();
149 if ( stretch
& wxTILE
)
152 for ( ; x
< rect
.width
; x
+= width
)
154 for ( y
= 0; y
< rect
.height
; y
+= height
)
156 // no need to use mask here as we cover the entire window area
157 dc
.DrawBitmap(bmp
, x
, y
);
162 else if ( stretch
& wxEXPAND
)
164 // stretch bitmap to fill the entire control
165 bmp
= wxBitmap(wxImage(bmp
.ConvertToImage()).Scale(rect
.width
, rect
.height
));
167 #endif // wxUSE_IMAGE
168 else // not stretched, not tiled
170 if ( alignment
& wxALIGN_RIGHT
)
172 x
= rect
.GetRight() - width
;
174 else if ( alignment
& wxALIGN_CENTRE
)
176 x
= (rect
.GetLeft() + rect
.GetRight() - width
+ 1) / 2;
178 else // alignment & wxALIGN_LEFT
183 if ( alignment
& wxALIGN_BOTTOM
)
185 y
= rect
.GetBottom() - height
;
187 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
189 y
= (rect
.GetTop() + rect
.GetBottom() - height
+ 1) / 2;
191 else // alignment & wxALIGN_TOP
198 dc
.DrawBitmap(bmp
, x
, y
, true /* use mask */);
203 void wxControlRenderer::DrawScrollbar(const wxScrollBar
*scrollbar
,
204 int WXUNUSED(thumbPosOld
))
206 // we will only redraw the parts which must be redrawn and not everything
207 wxRegion rgnUpdate
= scrollbar
->GetUpdateRegion();
210 wxRect rectUpdate
= rgnUpdate
.GetBox();
211 wxLogTrace(_T("scrollbar"),
212 _T("%s redraw: update box is (%d, %d)-(%d, %d)"),
213 scrollbar
->IsVertical() ? _T("vert") : _T("horz"),
214 rectUpdate
.GetLeft(),
216 rectUpdate
.GetRight(),
217 rectUpdate
.GetBottom());
219 #if 0 //def WXDEBUG_SCROLLBAR
220 static bool s_refreshDebug
= false;
221 if ( s_refreshDebug
)
223 wxClientDC
dc(wxConstCast(scrollbar
, wxScrollBar
));
224 dc
.SetBrush(*wxRED_BRUSH
);
225 dc
.SetPen(*wxTRANSPARENT_PEN
);
226 dc
.DrawRectangle(rectUpdate
);
228 // under Unix we use "--sync" X option for this
234 #endif // WXDEBUG_SCROLLBAR
237 wxOrientation orient
= scrollbar
->IsVertical() ? wxVERTICAL
241 for ( int nBar
= 0; nBar
< 2; nBar
++ )
243 wxScrollBar::Element elem
=
244 (wxScrollBar::Element
)(wxScrollBar::Element_Bar_1
+ nBar
);
246 wxRect rectBar
= scrollbar
->GetScrollbarRect(elem
);
248 if ( rgnUpdate
.Contains(rectBar
) )
250 wxLogTrace(_T("scrollbar"),
251 _T("drawing bar part %d at (%d, %d)-(%d, %d)"),
256 rectBar
.GetBottom());
258 m_renderer
->DrawScrollbarShaft(m_dc
,
261 scrollbar
->GetState(elem
));
266 for ( int nArrow
= 0; nArrow
< 2; nArrow
++ )
268 wxScrollBar::Element elem
=
269 (wxScrollBar::Element
)(wxScrollBar::Element_Arrow_Line_1
+ nArrow
);
271 wxRect rectArrow
= scrollbar
->GetScrollbarRect(elem
);
272 if ( rgnUpdate
.Contains(rectArrow
) )
274 wxLogTrace(_T("scrollbar"),
275 _T("drawing arrow %d at (%d, %d)-(%d, %d)"),
279 rectArrow
.GetRight(),
280 rectArrow
.GetBottom());
282 scrollbar
->GetArrows().DrawArrow
284 (wxScrollArrows::Arrow
)nArrow
,
287 true // draw a scrollbar arrow, not just an arrow
292 // TODO: support for page arrows
295 wxScrollBar::Element elem
= wxScrollBar::Element_Thumb
;
296 wxRect rectThumb
= scrollbar
->GetScrollbarRect(elem
);
297 if ( rectThumb
.width
&& rectThumb
.height
&& rgnUpdate
.Contains(rectThumb
) )
299 wxLogTrace(_T("scrollbar"),
300 _T("drawing thumb at (%d, %d)-(%d, %d)"),
303 rectThumb
.GetRight(),
304 rectThumb
.GetBottom());
306 m_renderer
->DrawScrollbarThumb(m_dc
,
309 scrollbar
->GetState(elem
));
313 #endif // wxUSE_SCROLLBAR
315 void wxControlRenderer::DrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
317 wxASSERT_MSG( x1
== x2
|| y1
== y2
,
318 _T("line must be either horizontal or vertical") );
321 m_renderer
->DrawVerticalLine(m_dc
, x1
, y1
, y2
);
323 m_renderer
->DrawHorizontalLine(m_dc
, y1
, x1
, x2
);
328 void wxControlRenderer::DrawItems(const wxListBox
*lbox
,
329 size_t itemFirst
, size_t itemLast
)
331 DoDrawItems(lbox
, itemFirst
, itemLast
);
334 void wxControlRenderer::DoDrawItems(const wxListBox
*lbox
,
335 size_t itemFirst
, size_t itemLast
,
336 #if wxUSE_CHECKLISTBOX
339 bool WXUNUSED(isCheckLbox
)
343 // prepare for the drawing: calc the initial position
344 wxCoord lineHeight
= lbox
->GetLineHeight();
346 // note that SetClippingRegion() needs the physical (unscrolled)
347 // coordinates while we use the logical (scrolled) ones for the drawing
350 wxSize size
= lbox
->GetClientSize();
352 rect
.height
= size
.y
;
354 // keep the text inside the client rect or we will overwrite the vertical
355 // scrollbar for the long strings
356 m_dc
.SetClippingRegion(rect
.x
, rect
.y
, rect
.width
+ 1, rect
.height
+ 1);
358 // adjust the rect position now
359 lbox
->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
360 rect
.y
+= itemFirst
*lineHeight
;
361 rect
.height
= lineHeight
;
363 // the rect should go to the right visible border so adjust the width if x
364 // is shifted (rightmost point should stay the same)
365 rect
.width
-= rect
.x
;
367 // we'll keep the text colour unchanged
368 m_dc
.SetTextForeground(lbox
->GetForegroundColour());
370 // an item should have the focused rect only when the lbox has focus, so
371 // make sure that we never set wxCONTROL_FOCUSED flag if it doesn't
372 int itemCurrent
= wxWindow::FindFocus() == (wxWindow
*)lbox
// cast needed
373 ? lbox
->GetCurrentItem()
375 for ( size_t n
= itemFirst
; n
< itemLast
; n
++ )
378 if ( (int)n
== itemCurrent
)
379 flags
|= wxCONTROL_FOCUSED
;
380 if ( lbox
->IsSelected(n
) )
381 flags
|= wxCONTROL_SELECTED
;
383 #if wxUSE_CHECKLISTBOX
386 wxCheckListBox
*checklstbox
= wxStaticCast(lbox
, wxCheckListBox
);
387 if ( checklstbox
->IsChecked(n
) )
388 flags
|= wxCONTROL_CHECKED
;
390 m_renderer
->DrawCheckItem(m_dc
, lbox
->GetString(n
),
396 #endif // wxUSE_CHECKLISTBOX
398 m_renderer
->DrawItem(m_dc
, lbox
->GetString(n
), rect
, flags
);
401 rect
.y
+= lineHeight
;
405 #endif // wxUSE_LISTBOX
407 #if wxUSE_CHECKLISTBOX
409 void wxControlRenderer::DrawCheckItems(const wxCheckListBox
*lbox
,
410 size_t itemFirst
, size_t itemLast
)
412 DoDrawItems(lbox
, itemFirst
, itemLast
, true);
415 #endif // wxUSE_CHECKLISTBOX
419 void wxControlRenderer::DrawProgressBar(const wxGauge
*gauge
)
422 m_dc
.SetBrush(wxBrush(m_window
->GetBackgroundColour(), wxSOLID
));
423 m_dc
.SetPen(*wxTRANSPARENT_PEN
);
424 m_dc
.DrawRectangle(m_rect
);
426 int max
= gauge
->GetRange();
433 // calc the filled rect
434 int pos
= gauge
->GetValue();
435 int left
= max
- pos
;
437 wxRect rect
= m_rect
;
438 rect
.Deflate(1); // FIXME this depends on the border width
440 wxColour col
= m_window
->UseFgCol() ? m_window
->GetForegroundColour()
441 : wxTHEME_COLOUR(GAUGE
);
442 m_dc
.SetBrush(wxBrush(col
, wxSOLID
));
444 if ( gauge
->IsSmooth() )
446 // just draw the rectangle in one go
447 if ( gauge
->IsVertical() )
449 // vert bars grow from bottom to top
450 wxCoord dy
= ((rect
.height
- 1) * left
) / max
;
456 // grow from left to right
457 rect
.width
-= ((rect
.width
- 1) * left
) / max
;
460 m_dc
.DrawRectangle(rect
);
464 wxSize sizeStep
= m_renderer
->GetProgressBarStep();
465 int step
= gauge
->IsVertical() ? sizeStep
.y
: sizeStep
.x
;
467 // we divide by it below!
468 wxCHECK_RET( step
, _T("invalid wxGauge step") );
470 // round up to make the progress appear to start faster
471 int lenTotal
= gauge
->IsVertical() ? rect
.height
: rect
.width
;
472 int steps
= ((lenTotal
+ step
- 1) * pos
) / (max
* step
);
474 // calc the coords of one small rect
477 if ( gauge
->IsVertical() )
479 // draw from bottom to top: so first set y to the bottom
480 rect
.y
+= rect
.height
- 1;
482 // then adjust the height
485 // and then adjust y again to be what it should for the first rect
486 rect
.y
-= rect
.height
;
491 // remember that this will be the coord which will change
499 // don't leave 2 empty pixels in the beginning
509 for ( int n
= 0; n
< steps
; n
++ )
511 wxRect rectSegment
= rect
;
512 rectSegment
.Deflate(dx
, dy
);
514 m_dc
.DrawRectangle(rectSegment
);
519 // this can only happen for the last step of vertical gauge
520 rect
.height
= *px
- step
- 1;
523 else if ( *px
> lenTotal
- step
)
525 // this can only happen for the last step of horizontal gauge
526 rect
.width
= lenTotal
- *px
- 1;
532 #endif // wxUSE_GAUGE