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 void RefreshLine(size_t line
);
100 // redraw all lines in the specified range (inclusive)
101 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 GetFirstVisibleLine() const { return m_lineFirst
; }
123 // get the last currently visible line
124 size_t GetLastVisibleLine() const { return m_lineFirst
+ m_nVisible
- 1; }
126 // is this line currently visible?
127 bool IsVisible(size_t line
) const
128 { return line
>= m_lineFirst
&& line
<= GetLastVisibleLine(); }
132 // this function must be overridden in the derived class and it should
133 // return the height of the given line in pixels
134 virtual wxCoord
OnGetLineHeight(size_t n
) const = 0;
136 // this function doesn't have to be overridden but it may be useful to do
137 // it if calculating the lines heights is a relatively expensive operation
138 // as it gives the user code a possibility to calculate several of them at
141 // OnGetLinesHint() is normally called just before OnGetLineHeight() but you
142 // shouldn't rely on the latter being called for all lines in the interval
143 // specified here. It is also possible that OnGetLineHeight() will be
144 // called for the lines outside of this interval, so this is really just a
145 // hint, not a promise.
147 // finally note that lineMin is inclusive, while lineMax is exclusive, as
149 virtual void OnGetLinesHint(size_t WXUNUSED(lineMin
),
150 size_t WXUNUSED(lineMax
)) const { }
152 // when the number of lines changes, we try to estimate the total height
153 // of all lines which is a rather expensive operation in terms of lines
154 // access, so if the user code may estimate the average height
155 // better/faster than we do, it should override this function to implement
158 // this function should return the best guess for the total height it may
160 virtual wxCoord
EstimateTotalHeight() const;
163 // the event handlers
164 void OnSize(wxSizeEvent
& event
);
165 void OnScroll(wxScrollWinEvent
& event
);
167 void OnMouseWheel(wxMouseEvent
& event
);
170 // find the index of the line we need to show at the top of the window such
171 // that the last (fully or partially) visible line is the given one
172 size_t FindFirstFromBottom(size_t lineLast
, bool fullyVisible
= false);
174 // get the total height of the lines between lineMin (inclusive) and
175 // lineMax (exclusive)
176 wxCoord
GetLinesHeight(size_t lineMin
, size_t lineMax
) const;
178 // update the thumb size shown by the scrollbar
179 void UpdateScrollbar();
182 // common part of all ctors
186 // the total number of (logical) lines
189 // the total (estimated) height
190 wxCoord m_heightTotal
;
192 // the first currently visible line
195 // the number of currently visible lines (including the last, possibly only
196 // partly, visible one)
199 // accumulated mouse wheel rotation
201 int m_sumWheelRotation
;
204 DECLARE_EVENT_TABLE()
205 DECLARE_NO_COPY_CLASS(wxVScrolledWindow
)
206 DECLARE_ABSTRACT_CLASS(wxVScrolledWindow
)
209 #endif // _WX_VSCROLL_H_