]> git.saurik.com Git - wxWidgets.git/blob - include/wx/univ/scrolbar.h
c82692f9fe1efea1fa312fce3c3148f429ca7833
[wxWidgets.git] / include / wx / univ / scrolbar.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/scrolbar.h
3 // Purpose: wxScrollBar for wxUniversal
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.08.00
7 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNIV_SCROLBAR_H_
12 #define _WX_UNIV_SCROLBAR_H_
13
14 class WXDLLIMPEXP_FWD_CORE wxScrollTimer;
15
16 #include "wx/univ/scrarrow.h"
17 #include "wx/renderer.h"
18
19 // ----------------------------------------------------------------------------
20 // the actions supported by this control
21 // ----------------------------------------------------------------------------
22
23 // scroll the bar
24 #define wxACTION_SCROLL_START wxT("start") // to the beginning
25 #define wxACTION_SCROLL_END wxT("end") // to the end
26 #define wxACTION_SCROLL_LINE_UP wxT("lineup") // one line up/left
27 #define wxACTION_SCROLL_PAGE_UP wxT("pageup") // one page up/left
28 #define wxACTION_SCROLL_LINE_DOWN wxT("linedown") // one line down/right
29 #define wxACTION_SCROLL_PAGE_DOWN wxT("pagedown") // one page down/right
30
31 // the scrollbar thumb may be dragged
32 #define wxACTION_SCROLL_THUMB_DRAG wxT("thumbdrag")
33 #define wxACTION_SCROLL_THUMB_MOVE wxT("thumbmove")
34 #define wxACTION_SCROLL_THUMB_RELEASE wxT("thumbrelease")
35
36 // ----------------------------------------------------------------------------
37 // wxScrollBar
38 // ----------------------------------------------------------------------------
39
40 class WXDLLIMPEXP_CORE wxScrollBar : public wxScrollBarBase,
41 public wxControlWithArrows
42 {
43 public:
44 // scrollbar elements: they correspond to wxHT_SCROLLBAR_XXX constants but
45 // start from 0 which allows to use them as array indices
46 enum Element
47 {
48 Element_Arrow_Line_1,
49 Element_Arrow_Line_2,
50 Element_Arrow_Page_1,
51 Element_Arrow_Page_2,
52 Element_Thumb,
53 Element_Bar_1,
54 Element_Bar_2,
55 Element_Max
56 };
57
58 wxScrollBar();
59 wxScrollBar(wxWindow *parent,
60 wxWindowID id,
61 const wxPoint& pos = wxDefaultPosition,
62 const wxSize& size = wxDefaultSize,
63 long style = wxSB_HORIZONTAL,
64 const wxValidator& validator = wxDefaultValidator,
65 const wxString& name = wxScrollBarNameStr);
66
67 bool Create(wxWindow *parent,
68 wxWindowID id,
69 const wxPoint& pos = wxDefaultPosition,
70 const wxSize& size = wxDefaultSize,
71 long style = wxSB_HORIZONTAL,
72 const wxValidator& validator = wxDefaultValidator,
73 const wxString& name = wxScrollBarNameStr);
74
75 virtual ~wxScrollBar();
76
77 // implement base class pure virtuals
78 virtual int GetThumbPosition() const;
79 virtual int GetThumbSize() const;
80 virtual int GetPageSize() const;
81 virtual int GetRange() const;
82
83 virtual void SetThumbPosition(int thumbPos);
84 virtual void SetScrollbar(int position, int thumbSize,
85 int range, int pageSize,
86 bool refresh = true);
87
88 // wxScrollBar actions
89 void ScrollToStart();
90 void ScrollToEnd();
91 bool ScrollLines(int nLines);
92 bool ScrollPages(int nPages);
93
94 virtual bool PerformAction(const wxControlAction& action,
95 long numArg = 0,
96 const wxString& strArg = wxEmptyString);
97
98 static wxInputHandler *GetStdInputHandler(wxInputHandler *handlerDef);
99 virtual wxInputHandler *DoGetStdInputHandler(wxInputHandler *handlerDef)
100 {
101 return GetStdInputHandler(handlerDef);
102 }
103
104 // scrollbars around a normal window should not receive the focus
105 virtual bool AcceptsFocus() const;
106
107 // wxScrollBar sub elements state (combination of wxCONTROL_XXX)
108 void SetState(Element which, int flags);
109 int GetState(Element which) const;
110
111 // implement wxControlWithArrows methods
112 virtual wxRenderer *GetRenderer() const { return m_renderer; }
113 virtual wxWindow *GetWindow() { return this; }
114 virtual bool IsVertical() const { return wxScrollBarBase::IsVertical(); }
115 virtual int GetArrowState(wxScrollArrows::Arrow arrow) const;
116 virtual void SetArrowFlag(wxScrollArrows::Arrow arrow, int flag, bool set);
117 virtual bool OnArrow(wxScrollArrows::Arrow arrow);
118 virtual wxScrollArrows::Arrow HitTestArrow(const wxPoint& pt) const;
119
120 // for wxControlRenderer::DrawScrollbar() only
121 const wxScrollArrows& GetArrows() const { return m_arrows; }
122
123 // returns one of wxHT_SCROLLBAR_XXX constants
124 wxHitTest HitTestBar(const wxPoint& pt) const;
125
126 // idle processing
127 virtual void OnInternalIdle();
128
129 protected:
130 virtual wxSize DoGetBestClientSize() const;
131 virtual void DoDraw(wxControlRenderer *renderer);
132 virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
133
134 // forces update of thumb's visual appearence (does nothing if m_dirty=false)
135 void UpdateThumb();
136
137 // SetThumbPosition() helper
138 void DoSetThumb(int thumbPos);
139
140 // common part of all ctors
141 void Init();
142
143 // is this scrollbar attached to a window or a standalone control?
144 bool IsStandalone() const;
145
146 // scrollbar geometry methods:
147
148 // gets the bounding box for a scrollbar element for the given (by default
149 // - current) thumb position
150 wxRect GetScrollbarRect(wxScrollBar::Element elem, int thumbPos = -1) const;
151
152 // returns the size of the scrollbar shaft excluding the arrows
153 wxCoord GetScrollbarSize() const;
154
155 // translate the scrollbar position (in logical units) into physical
156 // coordinate (in pixels) and the other way round
157 wxCoord ScrollbarToPixel(int thumbPos = -1);
158 int PixelToScrollbar(wxCoord coord);
159
160 // return the starting and ending positions, in pixels, of the thumb of a
161 // scrollbar with the given logical position, thumb size and range and the
162 // given physical length
163 static void GetScrollBarThumbSize(wxCoord length,
164 int thumbPos,
165 int thumbSize,
166 int range,
167 wxCoord *thumbStart,
168 wxCoord *thumbEnd);
169
170 private:
171 // total range of the scrollbar in logical units
172 int m_range;
173
174 // the current and previous (after last refresh - this is used for
175 // repainting optimisation) size of the thumb in logical units (from 0 to
176 // m_range) and its position (from 0 to m_range - m_thumbSize)
177 int m_thumbSize,
178 m_thumbPos,
179 m_thumbPosOld;
180
181 // the page size, i.e. the number of lines by which to scroll when page
182 // up/down action is performed
183 int m_pageSize;
184
185 // the state of the sub elements
186 int m_elementsState[Element_Max];
187
188 // the dirty flag: if set, scrollbar must be updated
189 bool m_dirty;
190
191 // the object handling the arrows
192 wxScrollArrows m_arrows;
193
194 friend WXDLLIMPEXP_CORE class wxControlRenderer; // for geometry methods
195 friend class wxStdScrollBarInputHandler; // for geometry methods
196
197 DECLARE_EVENT_TABLE()
198 DECLARE_DYNAMIC_CLASS(wxScrollBar)
199 };
200
201 // ----------------------------------------------------------------------------
202 // Standard scrollbar input handler which can be used as a base class
203 // ----------------------------------------------------------------------------
204
205 class WXDLLIMPEXP_CORE wxStdScrollBarInputHandler : public wxStdInputHandler
206 {
207 public:
208 // constructor takes a renderer (used for scrollbar hit testing) and the
209 // base handler to which all unhandled events are forwarded
210 wxStdScrollBarInputHandler(wxRenderer *renderer,
211 wxInputHandler *inphand);
212
213 virtual bool HandleKey(wxInputConsumer *consumer,
214 const wxKeyEvent& event,
215 bool pressed);
216 virtual bool HandleMouse(wxInputConsumer *consumer,
217 const wxMouseEvent& event);
218 virtual bool HandleMouseMove(wxInputConsumer *consumer, const wxMouseEvent& event);
219
220 virtual ~wxStdScrollBarInputHandler();
221
222 // this method is called by wxScrollBarTimer only and may be overridden
223 //
224 // return true to continue scrolling, false to stop the timer
225 virtual bool OnScrollTimer(wxScrollBar *scrollbar,
226 const wxControlAction& action);
227
228 protected:
229 // return true if the mouse button can be used to activate scrollbar, false
230 // if not (any button under GTK+ unlike left button only which is default)
231 virtual bool IsAllowedButton(int button) const
232 { return button == wxMOUSE_BTN_LEFT; }
233
234 // set or clear the specified flag on the scrollbar element corresponding
235 // to m_htLast
236 void SetElementState(wxScrollBar *scrollbar, int flag, bool doIt);
237
238 // [un]highlight the scrollbar element corresponding to m_htLast
239 virtual void Highlight(wxScrollBar *scrollbar, bool doIt)
240 { SetElementState(scrollbar, wxCONTROL_CURRENT, doIt); }
241
242 // [un]press the scrollbar element corresponding to m_htLast
243 virtual void Press(wxScrollBar *scrollbar, bool doIt)
244 { SetElementState(scrollbar, wxCONTROL_PRESSED, doIt); }
245
246 // stop scrolling because we reached the end point
247 void StopScrolling(wxScrollBar *scrollbar);
248
249 // get the mouse coordinates in the scrollbar direction from the event
250 wxCoord GetMouseCoord(const wxScrollBar *scrollbar,
251 const wxMouseEvent& event) const;
252
253 // generate a "thumb move" action for this mouse event
254 void HandleThumbMove(wxScrollBar *scrollbar, const wxMouseEvent& event);
255
256
257 // the window (scrollbar) which has capture or NULL and the flag telling if
258 // the mouse is inside the element which captured it or not
259 wxWindow *m_winCapture;
260 bool m_winHasMouse;
261 int m_btnCapture; // the mouse button which has captured mouse
262
263 // the position where we started scrolling by page
264 wxPoint m_ptStartScrolling;
265
266 // one of wxHT_SCROLLBAR_XXX value: where has the mouse been last time?
267 wxHitTest m_htLast;
268
269 // the renderer (we use it only for hit testing)
270 wxRenderer *m_renderer;
271
272 // the offset of the top/left of the scrollbar relative to the mouse to
273 // keep during the thumb drag
274 int m_ofsMouse;
275
276 // the timer for generating scroll events when the mouse stays pressed on
277 // a scrollbar
278 wxScrollTimer *m_timerScroll;
279 };
280
281 #endif // _WX_UNIV_SCROLBAR_H_
282