1 /////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/vscroll.h
3 // Purpose: wxVScrolledWindow: generalization of wxScrolledWindow
4 // Author: Vadim Zeitlin
5 // Modified by: Brad Anderson
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_VSCROLL_H_
13 #define _WX_VSCROLL_H_
15 #include "wx/panel.h" // base class
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
22 In the name of this class, "V" may stand for "variable" because it can be
23 used for scrolling lines of variable heights; "virtual" because it is not
24 necessary to know the heights of all lines in advance -- only those which
25 are shown on the screen need to be measured; or, even, "vertical" because
26 this class only supports scrolling in one direction currently (this could
27 and probably will change in the future however).
29 In any case, this is a generalization of the wxScrolledWindow class which
30 can be only used when all lines have the same height. It lacks some other
31 wxScrolledWindow features however, notably it currently lacks support for
32 horizontal scrolling; it can't scroll another window nor only a rectangle
33 of the window and not its entire client area.
35 class WXDLLEXPORT wxVScrolledWindow
: public wxPanel
38 // constructors and such
39 // ---------------------
41 // default ctor, you must call Create() later
42 wxVScrolledWindow() { Init(); }
44 // normal ctor, no need to call Create() after this one
46 // note that wxVSCROLL is always automatically added to our style, there is
47 // no need to specify it explicitly
48 wxVScrolledWindow(wxWindow
*parent
,
49 wxWindowID id
= wxID_ANY
,
50 const wxPoint
& pos
= wxDefaultPosition
,
51 const wxSize
& size
= wxDefaultSize
,
53 const wxString
& name
= wxPanelNameStr
)
57 (void)Create(parent
, id
, pos
, size
, style
, name
);
60 // same as the previous ctor but returns status code: true if ok
62 // just as with the ctor above, wxVSCROLL style is always used, there is no
64 bool Create(wxWindow
*parent
,
65 wxWindowID id
= wxID_ANY
,
66 const wxPoint
& pos
= wxDefaultPosition
,
67 const wxSize
& size
= wxDefaultSize
,
69 const wxString
& name
= wxPanelNameStr
)
71 return wxPanel::Create(parent
, id
, pos
, size
, style
| wxVSCROLL
, name
);
78 // set the number of lines the window contains: the derived class must
79 // provide the heights for all lines with indices up to the one given here
80 // in its OnGetLineHeight()
81 void SetLineCount(size_t count
);
83 // scroll to the specified line: it will become the first visible line in
86 // return true if we scrolled the window, false if nothing was done
87 bool ScrollToLine(size_t line
);
89 // scroll by the specified number of lines/pages
90 virtual bool ScrollLines(int lines
);
91 virtual bool ScrollPages(int pages
);
93 // redraw the specified line
94 virtual void RefreshLine(size_t line
);
96 // redraw all lines in the specified range (inclusive)
97 virtual void RefreshLines(size_t from
, size_t to
);
99 // return the item at the specified (in physical coordinates) position or.
101 // wxNOT_FOUND if none, i.e. if it is below the last item
102 int HitTest(wxCoord x
, wxCoord y
) const;
103 int HitTest(const wxPoint
& pt
) const { return HitTest(pt
.x
, pt
.y
); }
105 // recalculate all our parameters and redisplay all lines
106 virtual void RefreshAll();
112 // get the number of lines this window contains (previously set by
114 size_t GetLineCount() const { return m_lineMax
; }
116 // get the first currently visible line
117 size_t GetVisibleBegin() const { return m_lineFirst
; }
119 // get the first currently visible line
120 size_t GetVisibleEnd() const { return m_lineFirst
+ m_nVisible
; }
122 // is this line currently visible?
123 bool IsVisible(size_t line
) const
124 { return line
>= GetVisibleBegin() && line
< GetVisibleEnd(); }
127 // this is the same as GetVisibleBegin(), exists to match
128 // GetLastVisibleLine() and for backwards compatibility only
129 size_t GetFirstVisibleLine() const { return m_lineFirst
; }
131 // get the last currently visible line
133 // this function is unsafe as it returns (size_t)-1 (i.e. a huge positive
134 // number) if the control is empty, use GetVisibleEnd() instead, this one
135 // is kept for backwards compatibility
136 size_t GetLastVisibleLine() const { return GetVisibleEnd() - 1; }
138 // layout the children (including the sizer if needed)
139 virtual bool Layout();
142 // this function must be overridden in the derived class and it should
143 // return the height of the given line in pixels
144 virtual wxCoord
OnGetLineHeight(size_t n
) const = 0;
146 // this function doesn't have to be overridden but it may be useful to do
147 // it if calculating the lines heights is a relatively expensive operation
148 // as it gives the user code a possibility to calculate several of them at
151 // OnGetLinesHint() is normally called just before OnGetLineHeight() but you
152 // shouldn't rely on the latter being called for all lines in the interval
153 // specified here. It is also possible that OnGetLineHeight() will be
154 // called for the lines outside of this interval, so this is really just a
155 // hint, not a promise.
157 // finally note that lineMin is inclusive, while lineMax is exclusive, as
159 virtual void OnGetLinesHint(size_t WXUNUSED(lineMin
),
160 size_t WXUNUSED(lineMax
)) const { }
162 // when the number of lines changes, we try to estimate the total height
163 // of all lines which is a rather expensive operation in terms of lines
164 // access, so if the user code may estimate the average height
165 // better/faster than we do, it should override this function to implement
168 // this function should return the best guess for the total height it may
170 virtual wxCoord
EstimateTotalHeight() const;
173 // the event handlers
174 void OnSize(wxSizeEvent
& event
);
175 void OnScroll(wxScrollWinEvent
& event
);
177 void OnMouseWheel(wxMouseEvent
& event
);
180 // find the index of the line we need to show at the top of the window such
181 // that the last (fully or partially) visible line is the given one
182 size_t FindFirstFromBottom(size_t lineLast
, bool fullyVisible
= false);
184 // get the total height of the lines between lineMin (inclusive) and
185 // lineMax (exclusive)
186 wxCoord
GetLinesHeight(size_t lineMin
, size_t lineMax
) const;
188 // update the thumb size shown by the scrollbar
189 void UpdateScrollbar();
192 // common part of all ctors
196 // the total number of (logical) lines
199 // the total (estimated) height
200 wxCoord m_heightTotal
;
202 // the first currently visible line
205 // the number of currently visible lines (including the last, possibly only
206 // partly, visible one)
209 // accumulated mouse wheel rotation
211 int m_sumWheelRotation
;
214 DECLARE_EVENT_TABLE()
215 DECLARE_NO_COPY_CLASS(wxVScrolledWindow
)
216 DECLARE_ABSTRACT_CLASS(wxVScrolledWindow
)
220 // ----------------------------------------------------------------------------
221 // wxHVScrolledWindow
222 // ----------------------------------------------------------------------------
225 This class is strongly influenced by wxVScrolledWindow. In fact, much of
226 code is line for line the same except it explicitly states which axis is
227 being worked on. Like wxVScrolledWindow, this class is here to provide
228 an easy way to implement variable line sizes. The difference is that
229 wxVScrolledWindow only works with vertical scrolling. This class extends
230 the behavior of wxVScrolledWindow to the horizontal axis in addition to the
233 The scrolling is also "virtual" in the sense that line widths and heights
234 only need to be known for lines that are currently visible.
236 Like wxVScrolledWindow, this is a generalization of the wxScrolledWindow
237 class which can be only used when all horizontal lines have the same width
238 and all of the vertical lines have the same height. Like wxVScrolledWinow
239 it lacks some of wxScrolledWindow features such as scrolling another window
240 or only scrolling a rectangle of the window and not its entire client area.
242 If only vertical scrolling is needed, wxVScrolledWindow is recommended
243 because it is simpler to use (and you get to type less).
245 There is no wxHScrolledWindow but horizontal only scrolling is implemented
246 easily enough with this class. If someone feels the need for such a class,
247 implementing it is trivial.
249 class WXDLLEXPORT wxHVScrolledWindow
: public wxPanel
252 // constructors and such
253 // ---------------------
255 // default ctor, you must call Create() later
256 wxHVScrolledWindow() { Init(); }
258 // normal ctor, no need to call Create() after this one
260 // note that wxVSCROLL and wxHSCROLL are always automatically added to our
261 // style, there is no need to specify them explicitly
262 wxHVScrolledWindow(wxWindow
*parent
,
263 wxWindowID id
= wxID_ANY
,
264 const wxPoint
& pos
= wxDefaultPosition
,
265 const wxSize
& size
= wxDefaultSize
,
267 const wxString
& name
= wxPanelNameStr
)
271 (void)Create(parent
, id
, pos
, size
, style
, name
);
274 // same as the previous ctor but returns status code: true if ok
276 // just as with the ctor above, wxVSCROLL and wxHSCROLL styles are always
277 // used, there is no need to specify them
278 bool Create(wxWindow
*parent
,
279 wxWindowID id
= wxID_ANY
,
280 const wxPoint
& pos
= wxDefaultPosition
,
281 const wxSize
& size
= wxDefaultSize
,
283 const wxString
& name
= wxPanelNameStr
)
285 return wxPanel::Create(parent
, id
, pos
, size
,
286 style
| wxVSCROLL
| wxHSCROLL
, name
);
293 // set the number of lines the window contains for each axis: the derived
294 // class must provide the widths and heights for all lines with indices up
295 // to each of the one given here in its OnGetColumnWidth() and
297 void SetRowColumnCounts(size_t rowCount
, size_t columnCount
);
299 // with physical scrolling on, the device origin is changed properly when
300 // a wxPaintDC is prepared, children are actually moved and layed out
301 // properly, and the contents of the window (pixels) are actually moved
302 void EnablePhysicalScrolling(bool scrolling
= true)
303 { m_physicalScrolling
= scrolling
; }
305 // scroll to the specified line: it will become the first visible line in
308 // return true if we scrolled the window, false if nothing was done
309 bool ScrollToRow(size_t row
);
310 bool ScrollToColumn(size_t column
);
311 bool ScrollToRowColumn(size_t row
, size_t column
);
313 // scroll by the specified number of lines/pages
314 virtual bool ScrollRows(int rows
);
315 virtual bool ScrollColumns(int columns
);
316 virtual bool ScrollRowsColumns(int rows
, int columns
);
317 virtual bool ScrollRowPages(int pages
);
318 virtual bool ScrollColumnPages(int pages
);
319 virtual bool ScrollPages(int rowPages
, int columnPages
);
321 // redraw the specified line
322 virtual void RefreshRow(size_t line
);
323 virtual void RefreshColumn(size_t line
);
324 virtual void RefreshRowColumn(size_t row
, size_t column
);
326 // redraw all lines in the specified range (inclusive)
327 virtual void RefreshRows(size_t from
, size_t to
);
328 virtual void RefreshColumns(size_t from
, size_t to
);
329 virtual void RefreshRowsColumns(size_t fromRow
, size_t toRow
,
330 size_t fromColumn
, size_t toColumn
);
332 // return the horizontal and vertical line within a wxPoint at the
333 // specified (in physical coordinates) position or.
335 // wxNOT_FOUND in either or both axes if no line is present at the
336 // requested coordinates, i.e. if it is past the last lines
337 wxPoint
HitTest(wxCoord x
, wxCoord y
) const;
338 wxPoint
HitTest(const wxPoint
& pt
) const { return HitTest(pt
.x
, pt
.y
); }
340 // recalculate all our parameters and redisplay all lines
341 virtual void RefreshAll();
347 // get the number of lines this window contains (previously set by
349 size_t GetRowCount() const { return m_rowsMax
; }
350 size_t GetColumnCount() const { return m_columnsMax
; }
351 wxSize
GetRowColumnCounts() const
352 { return wxSize((int)m_columnsMax
, (int)m_rowsMax
); }
354 // get the first currently visible line/lines
355 size_t GetVisibleRowsBegin() const { return m_rowsFirst
; }
356 size_t GetVisibleColumnsBegin() const { return m_columnsFirst
; }
357 wxPoint
GetVisibleBegin() const
358 { return wxPoint((int)m_columnsFirst
, (int)m_rowsFirst
); }
360 // get the last currently visible line/lines
361 size_t GetVisibleRowsEnd() const
362 { return m_rowsFirst
+ m_nRowsVisible
; }
363 size_t GetVisibleColumnsEnd() const
364 { return m_columnsFirst
+ m_nColumnsVisible
; }
365 wxPoint
GetVisibleEnd() const
366 { return wxPoint((int)(m_columnsFirst
+ m_nColumnsVisible
),
367 (int)(m_rowsFirst
+ m_nRowsVisible
)); }
369 // is this line currently visible?
370 bool IsRowVisible(size_t row
) const
371 { return row
>= m_rowsFirst
&&
372 row
< GetVisibleRowsEnd(); }
373 bool IsColumnVisible(size_t column
) const
374 { return column
>= m_columnsFirst
&&
375 column
< GetVisibleColumnsEnd(); }
376 bool IsVisible(size_t row
, size_t column
) const
377 { return IsRowVisible(row
) && IsColumnVisible(column
); }
379 // layout the children (including the sizer if needed)
380 virtual bool Layout();
383 // these functions must be overridden in the derived class and they should
384 // return the width or height of the given line in pixels
385 virtual wxCoord
OnGetRowHeight(size_t n
) const = 0;
386 virtual wxCoord
OnGetColumnWidth(size_t n
) const = 0;
388 // the following functions don't need to be overridden but it may be useful
389 // to do if calculating the lines widths or heights is a relatively
390 // expensive operation as it gives the user code a possibility to calculate
391 // several of them at once
393 // OnGetRowsHeightHint() and OnGetColumnsWidthHint() are normally called
394 // just before OnGetRowHeight() and OnGetColumnWidth(), respectively, but
395 // you shouldn't rely on the latter methods being called for all lines in
396 // the interval specified here. It is also possible that OnGetRowHeight()
397 // or OnGetColumnWidth() will be called for the lines outside of this
398 // interval, so this is really just a hint, not a promise.
400 // finally note that min is inclusive, while max is exclusive, as usual
401 virtual void OnGetRowsHeightHint(size_t WXUNUSED(rowMin
),
402 size_t WXUNUSED(rowMax
)) const { }
403 virtual void OnGetColumnsWidthHint(size_t WXUNUSED(columnMin
),
404 size_t WXUNUSED(columnMax
)) const { }
406 // when the number of lines changes, we try to estimate the total width or
407 // height of all lines which is a rather expensive operation in terms of
408 // lines access, so if the user code may estimate the average height
409 // better/faster than we do, it should override this function to implement
412 // this function should return the best guess for the total height it may
414 virtual wxCoord
EstimateTotalHeight() const;
415 virtual wxCoord
EstimateTotalWidth() const;
417 // the event handlers
418 void OnSize(wxSizeEvent
& event
);
419 void OnScroll(wxScrollWinEvent
& event
);
421 void OnMouseWheel(wxMouseEvent
& event
);
425 // find the index of the horizontal line we need to show at the top of the
426 // window such that the last (fully or partially) visible line is the given
428 size_t FindFirstFromRight(size_t columnLast
, bool fullyVisible
= false);
430 // find the index of the vertical line we need to show at the top of the
431 // window such that the last (fully or partially) visible line is the given
433 size_t FindFirstFromBottom(size_t rowLast
, bool fullyVisible
= false);
436 // get the total width or height of the lines between lineMin (inclusive)
437 // and lineMax (exclusive)
438 wxCoord
GetRowsHeight(size_t rowMin
, size_t rowMax
) const;
439 wxCoord
GetColumnsWidth(size_t columnMin
, size_t columnMax
) const;
441 // update the thumb size shown by the scrollbar
442 void UpdateScrollbars();
444 // shifts the specified dc by the scroll position
445 void PrepareDC(wxDC
& dc
);
448 // common part of all ctors
452 // the total number of (logical) lines for each axis
456 // the total (estimated) height
457 wxCoord m_heightTotal
;
459 // the total (estimated) width
460 wxCoord m_widthTotal
;
462 // the first currently visible line on each axis
464 size_t m_columnsFirst
;
466 // the number of currently visible lines for each axis (including the last,
467 // possibly only partly, visible one)
468 size_t m_nRowsVisible
;
469 size_t m_nColumnsVisible
;
471 // accumulated mouse wheel rotation
473 int m_sumWheelRotation
;
476 // do child scrolling (used in DoPrepareDC())
477 bool m_physicalScrolling
;
479 DECLARE_EVENT_TABLE()
480 DECLARE_NO_COPY_CLASS(wxHVScrolledWindow
)
481 DECLARE_ABSTRACT_CLASS(wxHVScrolledWindow
)
484 #endif // _WX_VSCROLL_H_