1 /////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/vscroll.h
3 // Purpose: wxVScrolledWindow: generalization of wxScrolledWindow
4 // Author: Vadim Zeitlin
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "vscroll.h"
19 #include "wx/panel.h" // base class
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
26 In the name of this class, "V" may stand for "variable" because it can be
27 used for scrolling lines of variable heights; "virtual" because it is not
28 necessary to know the heights of all lines in advance -- only those which
29 are shown on the screen need to be measured; or, even, "vertical" because
30 this class only supports scrolling in one direction currently (this could
31 and probably will change in the future however).
33 In any case, this is a generalization of the wxScrolledWindow class which
34 can be only used when all lines have the same height. It lacks some other
35 wxScrolledWindow features however, notably it currently lacks support for
36 horizontal scrolling; it can't scroll another window nor only a rectangle
37 of the window and not its entire client area.
39 class WXDLLEXPORT wxVScrolledWindow
: public wxPanel
42 // constructors and such
43 // ---------------------
45 // default ctor, you must call Create() later
46 wxVScrolledWindow() { Init(); }
48 // normal ctor, no need to call Create() after this one
50 // note that wxVSCROLL is always automatically added to our style, there is
51 // no need to specify it explicitly
52 wxVScrolledWindow(wxWindow
*parent
,
53 wxWindowID id
= wxID_ANY
,
54 const wxPoint
& pos
= wxDefaultPosition
,
55 const wxSize
& size
= wxDefaultSize
,
57 const wxString
& name
= wxPanelNameStr
)
61 (void)Create(parent
, id
, pos
, size
, style
, name
);
64 // same as the previous ctor but returns status code: true if ok
66 // just as with the ctor above, wxVSCROLL style is always used, there is no
68 bool Create(wxWindow
*parent
,
69 wxWindowID id
= wxID_ANY
,
70 const wxPoint
& pos
= wxDefaultPosition
,
71 const wxSize
& size
= wxDefaultSize
,
73 const wxString
& name
= wxPanelNameStr
)
75 return wxPanel::Create(parent
, id
, pos
, size
, style
| wxVSCROLL
, name
);
82 // set the number of lines the window contains: the derived class must
83 // provide the heights for all lines with indices up to the one given here
84 // in its OnGetLineHeight()
85 void SetLineCount(size_t count
);
87 // scroll to the specified line: it will become the first visible line in
90 // return true if we scrolled the window, false if nothing was done
91 bool ScrollToLine(size_t line
);
93 // scroll by the specified number of lines/pages
94 virtual bool ScrollLines(int lines
);
95 virtual bool ScrollPages(int pages
);
97 // redraw the specified line
98 virtual void RefreshLine(size_t line
);
100 // redraw all lines in the specified range (inclusive)
101 virtual void RefreshLines(size_t from
, size_t to
);
103 // return the item at the specified (in physical coordinates) position or.
105 // wxNOT_FOUND if none, i.e. if it is below the last item
106 int HitTest(wxCoord x
, wxCoord y
) const;
107 int HitTest(const wxPoint
& pt
) const { return HitTest(pt
.x
, pt
.y
); }
109 // recalculate all our parameters and redisplay all lines
110 virtual void RefreshAll();
116 // get the number of lines this window contains (previously set by
118 size_t GetLineCount() const { return m_lineMax
; }
120 // get the first currently visible line
121 size_t GetVisibleBegin() const { return m_lineFirst
; }
123 // get the first currently visible line
124 size_t GetVisibleEnd() const { return m_lineFirst
+ m_nVisible
; }
126 // is this line currently visible?
127 bool IsVisible(size_t line
) const
128 { return line
>= GetVisibleBegin() && line
< GetVisibleEnd(); }
131 // this is the same as GetVisibleBegin(), exists to match
132 // GetLastVisibleLine() and for backwards compatibility only
133 size_t GetFirstVisibleLine() const { return m_lineFirst
; }
135 // get the last currently visible line
137 // this function is unsafe as it returns (size_t)-1 (i.e. a huge positive
138 // number) if the control is empty, use GetVisibleEnd() instead, this one
139 // is kept for backwards compatibility
140 size_t GetLastVisibleLine() const { return GetVisibleEnd() - 1; }
144 // this function must be overridden in the derived class and it should
145 // return the height of the given line in pixels
146 virtual wxCoord
OnGetLineHeight(size_t n
) const = 0;
148 // this function doesn't have to be overridden but it may be useful to do
149 // it if calculating the lines heights is a relatively expensive operation
150 // as it gives the user code a possibility to calculate several of them at
153 // OnGetLinesHint() is normally called just before OnGetLineHeight() but you
154 // shouldn't rely on the latter being called for all lines in the interval
155 // specified here. It is also possible that OnGetLineHeight() will be
156 // called for the lines outside of this interval, so this is really just a
157 // hint, not a promise.
159 // finally note that lineMin is inclusive, while lineMax is exclusive, as
161 virtual void OnGetLinesHint(size_t WXUNUSED(lineMin
),
162 size_t WXUNUSED(lineMax
)) const { }
164 // when the number of lines changes, we try to estimate the total height
165 // of all lines which is a rather expensive operation in terms of lines
166 // access, so if the user code may estimate the average height
167 // better/faster than we do, it should override this function to implement
170 // this function should return the best guess for the total height it may
172 virtual wxCoord
EstimateTotalHeight() const;
175 // the event handlers
176 void OnSize(wxSizeEvent
& event
);
177 void OnScroll(wxScrollWinEvent
& event
);
179 void OnMouseWheel(wxMouseEvent
& event
);
182 // find the index of the line we need to show at the top of the window such
183 // that the last (fully or partially) visible line is the given one
184 size_t FindFirstFromBottom(size_t lineLast
, bool fullyVisible
= false);
186 // get the total height of the lines between lineMin (inclusive) and
187 // lineMax (exclusive)
188 wxCoord
GetLinesHeight(size_t lineMin
, size_t lineMax
) const;
190 // update the thumb size shown by the scrollbar
191 void UpdateScrollbar();
194 // common part of all ctors
198 // the total number of (logical) lines
201 // the total (estimated) height
202 wxCoord m_heightTotal
;
204 // the first currently visible line
207 // the number of currently visible lines (including the last, possibly only
208 // partly, visible one)
211 // accumulated mouse wheel rotation
213 int m_sumWheelRotation
;
216 DECLARE_EVENT_TABLE()
217 DECLARE_NO_COPY_CLASS(wxVScrolledWindow
)
218 DECLARE_ABSTRACT_CLASS(wxVScrolledWindow
)
221 #endif // _WX_VSCROLL_H_