]>
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
7 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/control.h"
29 #include "wx/checklst.h"
30 #include "wx/listbox.h"
31 #include "wx/scrolbar.h"
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
40 #include "wx/univ/colschem.h"
42 // ============================================================================
44 // ============================================================================
46 wxRenderer::~wxRenderer()
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 wxControlRenderer::wxControlRenderer(wxWindow
*window
,
60 m_renderer
= renderer
;
62 wxSize size
= m_window
->GetClientSize();
65 m_rect
.width
= size
.x
;
66 m_rect
.height
= size
.y
;
69 void wxControlRenderer::DrawLabel()
71 m_dc
.SetBackgroundMode(wxTRANSPARENT
);
72 m_dc
.SetFont(m_window
->GetFont());
73 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
75 wxString label
= m_window
->GetLabel();
78 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
80 m_renderer
->DrawLabel(m_dc
,
83 m_window
->GetStateFlags(),
85 ctrl
->GetAccelIndex());
89 void wxControlRenderer::DrawButtonLabel(const wxBitmap
& bitmap
,
90 wxCoord marginX
, wxCoord marginY
)
92 m_dc
.SetBackgroundMode(wxTRANSPARENT
);
93 m_dc
.SetFont(m_window
->GetFont());
94 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
96 wxString label
= m_window
->GetLabel();
97 if ( !label
.empty() || bitmap
.IsOk() )
99 wxRect rectLabel
= m_rect
;
102 rectLabel
.Inflate(-marginX
, -marginY
);
105 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
107 m_renderer
->DrawButtonLabel(m_dc
,
111 m_window
->GetStateFlags(),
112 ctrl
->GetAlignment(),
113 ctrl
->GetAccelIndex());
117 void wxControlRenderer::DrawFrame()
119 m_dc
.SetFont(m_window
->GetFont());
120 m_dc
.SetTextForeground(m_window
->GetForegroundColour());
121 m_dc
.SetTextBackground(m_window
->GetBackgroundColour());
123 wxControl
*ctrl
= wxStaticCast(m_window
, wxControl
);
125 m_renderer
->DrawFrame(m_dc
,
126 m_window
->GetLabel(),
128 m_window
->GetStateFlags(),
129 ctrl
->GetAlignment(),
130 ctrl
->GetAccelIndex());
133 void wxControlRenderer::DrawButtonBorder()
135 int flags
= m_window
->GetStateFlags();
137 m_renderer
->DrawButtonBorder(m_dc
, m_rect
, flags
, &m_rect
);
140 // m_renderer->DrawButtonSurface(m_dc, wxTHEME_BG_COLOUR(m_window), m_rect, flags );
143 void wxControlRenderer::DrawBitmap(const wxBitmap
& bitmap
)
145 int style
= m_window
->GetWindowStyle();
146 DrawBitmap(m_dc
, bitmap
, m_rect
,
147 style
& wxALIGN_MASK
,
148 style
& wxBI_EXPAND
? wxEXPAND
: wxSTRETCH_NOT
);
152 void wxControlRenderer::DrawBitmap(wxDC
&dc
,
153 const wxBitmap
& bitmap
,
158 // we may change the bitmap if we stretch it
159 wxBitmap bmp
= bitmap
;
163 int width
= bmp
.GetWidth(),
164 height
= bmp
.GetHeight();
168 if ( stretch
& wxTILE
)
171 for ( ; x
< rect
.width
; x
+= width
)
173 for ( y
= 0; y
< rect
.height
; y
+= height
)
175 // no need to use mask here as we cover the entire window area
176 dc
.DrawBitmap(bmp
, x
, y
);
181 else if ( stretch
& wxEXPAND
)
183 // stretch bitmap to fill the entire control
184 bmp
= wxBitmap(wxImage(bmp
.ConvertToImage()).Scale(rect
.width
, rect
.height
));
186 #endif // wxUSE_IMAGE
187 else // not stretched, not tiled
189 if ( alignment
& wxALIGN_RIGHT
)
191 x
= rect
.GetRight() - width
;
193 else if ( alignment
& wxALIGN_CENTRE
)
195 x
= (rect
.GetLeft() + rect
.GetRight() - width
+ 1) / 2;
197 else // alignment & wxALIGN_LEFT
202 if ( alignment
& wxALIGN_BOTTOM
)
204 y
= rect
.GetBottom() - height
;
206 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
208 y
= (rect
.GetTop() + rect
.GetBottom() - height
+ 1) / 2;
210 else // alignment & wxALIGN_TOP
217 dc
.DrawBitmap(bmp
, x
, y
, true /* use mask */);
222 void wxControlRenderer::DrawScrollbar(const wxScrollBar
*scrollbar
,
223 int WXUNUSED(thumbPosOld
))
225 // we will only redraw the parts which must be redrawn and not everything
226 wxRegion rgnUpdate
= scrollbar
->GetUpdateRegion();
229 wxRect rectUpdate
= rgnUpdate
.GetBox();
230 wxLogTrace(wxT("scrollbar"),
231 wxT("%s redraw: update box is (%d, %d)-(%d, %d)"),
232 scrollbar
->IsVertical() ? wxT("vert") : wxT("horz"),
233 rectUpdate
.GetLeft(),
235 rectUpdate
.GetRight(),
236 rectUpdate
.GetBottom());
238 #if 0 //def WXDEBUG_SCROLLBAR
239 static bool s_refreshDebug
= false;
240 if ( s_refreshDebug
)
242 wxClientDC
dc(wxConstCast(scrollbar
, wxScrollBar
));
243 dc
.SetBrush(*wxRED_BRUSH
);
244 dc
.SetPen(*wxTRANSPARENT_PEN
);
245 dc
.DrawRectangle(rectUpdate
);
247 // under Unix we use "--sync" X option for this
253 #endif // WXDEBUG_SCROLLBAR
256 wxOrientation orient
= scrollbar
->IsVertical() ? wxVERTICAL
260 for ( int nBar
= 0; nBar
< 2; nBar
++ )
262 wxScrollBar::Element elem
=
263 (wxScrollBar::Element
)(wxScrollBar::Element_Bar_1
+ nBar
);
265 wxRect rectBar
= scrollbar
->GetScrollbarRect(elem
);
267 if ( rgnUpdate
.Contains(rectBar
) )
269 wxLogTrace(wxT("scrollbar"),
270 wxT("drawing bar part %d at (%d, %d)-(%d, %d)"),
275 rectBar
.GetBottom());
277 m_renderer
->DrawScrollbarShaft(m_dc
,
280 scrollbar
->GetState(elem
));
285 for ( int nArrow
= 0; nArrow
< 2; nArrow
++ )
287 wxScrollBar::Element elem
=
288 (wxScrollBar::Element
)(wxScrollBar::Element_Arrow_Line_1
+ nArrow
);
290 wxRect rectArrow
= scrollbar
->GetScrollbarRect(elem
);
291 if ( rgnUpdate
.Contains(rectArrow
) )
293 wxLogTrace(wxT("scrollbar"),
294 wxT("drawing arrow %d at (%d, %d)-(%d, %d)"),
298 rectArrow
.GetRight(),
299 rectArrow
.GetBottom());
301 scrollbar
->GetArrows().DrawArrow
303 (wxScrollArrows::Arrow
)nArrow
,
306 true // draw a scrollbar arrow, not just an arrow
311 // TODO: support for page arrows
314 wxScrollBar::Element elem
= wxScrollBar::Element_Thumb
;
315 wxRect rectThumb
= scrollbar
->GetScrollbarRect(elem
);
316 if ( rectThumb
.width
&& rectThumb
.height
&& rgnUpdate
.Contains(rectThumb
) )
318 wxLogTrace(wxT("scrollbar"),
319 wxT("drawing thumb at (%d, %d)-(%d, %d)"),
322 rectThumb
.GetRight(),
323 rectThumb
.GetBottom());
325 m_renderer
->DrawScrollbarThumb(m_dc
,
328 scrollbar
->GetState(elem
));
332 #endif // wxUSE_SCROLLBAR
334 void wxControlRenderer::DrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
336 wxASSERT_MSG( x1
== x2
|| y1
== y2
,
337 wxT("line must be either horizontal or vertical") );
340 m_renderer
->DrawVerticalLine(m_dc
, x1
, y1
, y2
);
342 m_renderer
->DrawHorizontalLine(m_dc
, y1
, x1
, x2
);
347 void wxControlRenderer::DrawItems(const wxListBox
*lbox
,
348 size_t itemFirst
, size_t itemLast
)
350 DoDrawItems(lbox
, itemFirst
, itemLast
);
353 void wxControlRenderer::DoDrawItems(const wxListBox
*lbox
,
354 size_t itemFirst
, size_t itemLast
,
355 #if wxUSE_CHECKLISTBOX
358 bool WXUNUSED(isCheckLbox
)
362 // prepare for the drawing: calc the initial position
363 wxCoord lineHeight
= lbox
->GetLineHeight();
365 // note that SetClippingRegion() needs the physical (unscrolled)
366 // coordinates while we use the logical (scrolled) ones for the drawing
369 wxSize size
= lbox
->GetClientSize();
371 rect
.height
= size
.y
;
373 // keep the text inside the client rect or we will overwrite the vertical
374 // scrollbar for the long strings
375 m_dc
.SetClippingRegion(rect
.x
, rect
.y
, rect
.width
+ 1, rect
.height
+ 1);
377 // adjust the rect position now
378 lbox
->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
379 rect
.y
+= itemFirst
*lineHeight
;
380 rect
.height
= lineHeight
;
382 // the rect should go to the right visible border so adjust the width if x
383 // is shifted (rightmost point should stay the same)
384 rect
.width
-= rect
.x
;
386 // we'll keep the text colour unchanged
387 m_dc
.SetTextForeground(lbox
->GetForegroundColour());
389 // an item should have the focused rect only when the lbox has focus, so
390 // make sure that we never set wxCONTROL_FOCUSED flag if it doesn't
391 int itemCurrent
= wxWindow::FindFocus() == (wxWindow
*)lbox
// cast needed
392 ? lbox
->GetCurrentItem()
394 for ( size_t n
= itemFirst
; n
< itemLast
; n
++ )
397 if ( (int)n
== itemCurrent
)
398 flags
|= wxCONTROL_FOCUSED
;
399 if ( lbox
->IsSelected(n
) )
400 flags
|= wxCONTROL_SELECTED
;
402 #if wxUSE_CHECKLISTBOX
405 wxCheckListBox
*checklstbox
= wxStaticCast(lbox
, wxCheckListBox
);
406 if ( checklstbox
->IsChecked(n
) )
407 flags
|= wxCONTROL_CHECKED
;
409 m_renderer
->DrawCheckItem(m_dc
, lbox
->GetString(n
),
415 #endif // wxUSE_CHECKLISTBOX
417 m_renderer
->DrawItem(m_dc
, lbox
->GetString(n
), rect
, flags
);
420 rect
.y
+= lineHeight
;
424 #endif // wxUSE_LISTBOX
426 #if wxUSE_CHECKLISTBOX
428 void wxControlRenderer::DrawCheckItems(const wxCheckListBox
*lbox
,
429 size_t itemFirst
, size_t itemLast
)
431 DoDrawItems(lbox
, itemFirst
, itemLast
, true);
434 #endif // wxUSE_CHECKLISTBOX
438 void wxControlRenderer::DrawProgressBar(const wxGauge
*gauge
)
441 m_dc
.SetBrush(wxBrush(m_window
->GetBackgroundColour(), wxSOLID
));
442 m_dc
.SetPen(*wxTRANSPARENT_PEN
);
443 m_dc
.DrawRectangle(m_rect
);
445 int max
= gauge
->GetRange();
452 // calc the filled rect
453 int pos
= gauge
->GetValue();
454 int left
= max
- pos
;
456 wxRect rect
= m_rect
;
457 rect
.Deflate(1); // FIXME this depends on the border width
459 wxColour col
= m_window
->UseFgCol() ? m_window
->GetForegroundColour()
460 : wxTHEME_COLOUR(GAUGE
);
461 m_dc
.SetBrush(wxBrush(col
, wxSOLID
));
463 if ( gauge
->IsSmooth() )
465 // just draw the rectangle in one go
466 if ( gauge
->IsVertical() )
468 // vert bars grow from bottom to top
469 wxCoord dy
= ((rect
.height
- 1) * left
) / max
;
475 // grow from left to right
476 rect
.width
-= ((rect
.width
- 1) * left
) / max
;
479 m_dc
.DrawRectangle(rect
);
483 wxSize sizeStep
= m_renderer
->GetProgressBarStep();
484 int step
= gauge
->IsVertical() ? sizeStep
.y
: sizeStep
.x
;
486 // we divide by it below!
487 wxCHECK_RET( step
, wxT("invalid wxGauge step") );
489 // round up to make the progress appear to start faster
490 int lenTotal
= gauge
->IsVertical() ? rect
.height
: rect
.width
;
491 int steps
= ((lenTotal
+ step
- 1) * pos
) / (max
* step
);
493 // calc the coords of one small rect
496 if ( gauge
->IsVertical() )
498 // draw from bottom to top: so first set y to the bottom
499 rect
.y
+= rect
.height
- 1;
501 // then adjust the height
504 // and then adjust y again to be what it should for the first rect
505 rect
.y
-= rect
.height
;
510 // remember that this will be the coord which will change
518 // don't leave 2 empty pixels in the beginning
528 for ( int n
= 0; n
< steps
; n
++ )
530 wxRect rectSegment
= rect
;
531 rectSegment
.Deflate(dx
, dy
);
533 m_dc
.DrawRectangle(rectSegment
);
538 // this can only happen for the last step of vertical gauge
539 rect
.height
= *px
- step
- 1;
542 else if ( *px
> lenTotal
- step
)
544 // this can only happen for the last step of horizontal gauge
545 rect
.width
= lenTotal
- *px
- 1;
551 #endif // wxUSE_GAUGE