deprecate OnScroll() which existed for compatibility only even before
[wxWidgets.git] / include / wx / scrolwin.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/scrolwin.h
3 // Purpose: wxScrolledWindow, wxScrolledControl and wxScrollHelper
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 30.08.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_SCROLWIN_H_BASE_
13 #define _WX_SCROLWIN_H_BASE_
14
15 #include "wx/panel.h"
16
17 class WXDLLIMPEXP_FWD_CORE wxScrollHelperEvtHandler;
18 class WXDLLIMPEXP_FWD_BASE wxTimer;
19
20 // default scrolled window style: scroll in both directions
21 #define wxScrolledWindowStyle (wxHSCROLL | wxVSCROLL)
22
23 // values for the second argument of wxScrollHelper::ShowScrollbars()
24 enum wxScrollbarVisibility
25 {
26 wxSHOW_SB_NEVER = -1, // never show the scrollbar at all
27 wxSHOW_SB_DEFAULT, // show scrollbar only if it is needed
28 wxSHOW_SB_ALWAYS // always show scrollbar, even if not needed
29 };
30
31 // ----------------------------------------------------------------------------
32 // The hierarchy of scrolling classes is a bit complicated because we want to
33 // put as much functionality as possible in a mix-in class not deriving from
34 // wxWindow so that other classes could derive from the same base class on all
35 // platforms irrespectively of whether they are native controls (and hence
36 // don't use our scrolling) or not.
37 //
38 // So we have
39 //
40 // wxScrollHelperBase
41 // |
42 // |
43 // \|/
44 // wxWindow wxScrollHelper
45 // | \ / /
46 // | \ / /
47 // | _| |_ /
48 // | wxScrolledWindow /
49 // | /
50 // \|/ /
51 // wxControl /
52 // \ /
53 // \ /
54 // _| |_
55 // wxScrolledControl
56 //
57 // ----------------------------------------------------------------------------
58
59 class WXDLLIMPEXP_CORE wxScrollHelperBase
60 {
61 public:
62 // ctor must be given the associated window
63 wxScrollHelperBase(wxWindow *winToScroll);
64 virtual ~wxScrollHelperBase();
65
66 // configure the scrolling
67 virtual void SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY,
68 int noUnitsX, int noUnitsY,
69 int xPos = 0, int yPos = 0,
70 bool noRefresh = false );
71
72 // scroll to the given (in logical coords) position
73 void Scroll(int x, int y) { DoScroll(x, y); }
74 void Scroll(const wxPoint& pt) { DoScroll(pt.x, pt.y); }
75
76 // get/set the page size for this orientation (wxVERTICAL/wxHORIZONTAL)
77 int GetScrollPageSize(int orient) const;
78 void SetScrollPageSize(int orient, int pageSize);
79
80 // get the number of lines the window can scroll,
81 // returns 0 if no scrollbars are there.
82 int GetScrollLines( int orient ) const;
83
84 // Set the x, y scrolling increments.
85 void SetScrollRate( int xstep, int ystep );
86
87 // get the size of one logical unit in physical ones
88 void GetScrollPixelsPerUnit(int *pixelsPerUnitX, int *pixelsPerUnitY) const;
89
90 // Set scrollbar visibility: it is possible to show scrollbar only if it is
91 // needed (i.e. if our virtual size is greater than the current size of the
92 // associated window), always (as wxALWAYS_SHOW_SB style does) or never (in
93 // which case you should provide some other way to scroll the window as the
94 // user wouldn't be able to do it at all)
95 void ShowScrollbars(wxScrollbarVisibility horz, wxScrollbarVisibility vert)
96 {
97 DoShowScrollbars(horz, vert);
98 }
99
100 // Enable/disable Windows scrolling in either direction. If true, wxWidgets
101 // scrolls the canvas and only a bit of the canvas is invalidated; no
102 // Clear() is necessary. If false, the whole canvas is invalidated and a
103 // Clear() is necessary. Disable for when the scroll increment is used to
104 // actually scroll a non-constant distance
105 virtual void EnableScrolling(bool x_scrolling, bool y_scrolling);
106
107 // Get the view start
108 void GetViewStart(int *x, int *y) const { DoGetViewStart(x, y); }
109
110 wxPoint GetViewStart() const
111 {
112 wxPoint pt;
113 DoGetViewStart(&pt.x, &pt.y);
114 return pt;
115 }
116
117 // Set the scale factor, used in PrepareDC
118 void SetScale(double xs, double ys) { m_scaleX = xs; m_scaleY = ys; }
119 double GetScaleX() const { return m_scaleX; }
120 double GetScaleY() const { return m_scaleY; }
121
122 // translate between scrolled and unscrolled coordinates
123 void CalcScrolledPosition(int x, int y, int *xx, int *yy) const
124 { DoCalcScrolledPosition(x, y, xx, yy); }
125 wxPoint CalcScrolledPosition(const wxPoint& pt) const
126 {
127 wxPoint p2;
128 DoCalcScrolledPosition(pt.x, pt.y, &p2.x, &p2.y);
129 return p2;
130 }
131
132 void CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
133 { DoCalcUnscrolledPosition(x, y, xx, yy); }
134 wxPoint CalcUnscrolledPosition(const wxPoint& pt) const
135 {
136 wxPoint p2;
137 DoCalcUnscrolledPosition(pt.x, pt.y, &p2.x, &p2.y);
138 return p2;
139 }
140
141 void DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const;
142 void DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const;
143
144 // Adjust the scrollbars
145 virtual void AdjustScrollbars() = 0;
146
147 // Calculate scroll increment
148 int CalcScrollInc(wxScrollWinEvent& event);
149
150 // Normally the wxScrolledWindow will scroll itself, but in some rare
151 // occasions you might want it to scroll [part of] another window (e.g. a
152 // child of it in order to scroll only a portion the area between the
153 // scrollbars (spreadsheet: only cell area will move).
154 void SetTargetWindow(wxWindow *target);
155 wxWindow *GetTargetWindow() const;
156
157 void SetTargetRect(const wxRect& rect) { m_rectToScroll = rect; }
158 wxRect GetTargetRect() const { return m_rectToScroll; }
159
160 // Override this function to draw the graphic (or just process EVT_PAINT)
161 virtual void OnDraw(wxDC& WXUNUSED(dc)) { }
162
163 // change the DC origin according to the scroll position.
164 virtual void DoPrepareDC(wxDC& dc);
165
166 // are we generating the autoscroll events?
167 bool IsAutoScrolling() const { return m_timerAutoScroll != NULL; }
168
169 // stop generating the scroll events when mouse is held outside the window
170 void StopAutoScrolling();
171
172 // this method can be overridden in a derived class to forbid sending the
173 // auto scroll events - note that unlike StopAutoScrolling() it doesn't
174 // stop the timer, so it will be called repeatedly and will typically
175 // return different values depending on the current mouse position
176 //
177 // the base class version just returns true
178 virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const;
179
180 // the methods to be called from the window event handlers
181 void HandleOnScroll(wxScrollWinEvent& event);
182 void HandleOnSize(wxSizeEvent& event);
183 void HandleOnPaint(wxPaintEvent& event);
184 void HandleOnChar(wxKeyEvent& event);
185 void HandleOnMouseEnter(wxMouseEvent& event);
186 void HandleOnMouseLeave(wxMouseEvent& event);
187 #if wxUSE_MOUSEWHEEL
188 void HandleOnMouseWheel(wxMouseEvent& event);
189 #endif // wxUSE_MOUSEWHEEL
190 void HandleOnChildFocus(wxChildFocusEvent& event);
191
192 #if WXWIN_COMPATIBILITY_2_8
193 wxDEPRECATED(
194 void OnScroll(wxScrollWinEvent& event) { HandleOnScroll(event); }
195 )
196 #endif // WXWIN_COMPATIBILITY_2_8
197
198 protected:
199 // get pointer to our scroll rect if we use it or NULL
200 const wxRect *GetScrollRect() const
201 {
202 return m_rectToScroll.width != 0 ? &m_rectToScroll : NULL;
203 }
204
205 // get the size of the target window
206 wxSize GetTargetSize() const
207 {
208 return m_rectToScroll.width != 0 ? m_rectToScroll.GetSize()
209 : m_targetWindow->GetClientSize();
210 }
211
212 void GetTargetSize(int *w, int *h) const
213 {
214 wxSize size = GetTargetSize();
215 if ( w )
216 *w = size.x;
217 if ( h )
218 *h = size.y;
219 }
220
221 // implementation of public methods with the same name
222 virtual void DoGetViewStart(int *x, int *y) const;
223 virtual void DoScroll(int x, int y) = 0;
224 virtual void DoShowScrollbars(wxScrollbarVisibility horz,
225 wxScrollbarVisibility vert) = 0;
226
227 // implementations of various wxWindow virtual methods which should be
228 // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER())
229 bool ScrollLayout();
230 void ScrollDoSetVirtualSize(int x, int y);
231 wxSize ScrollGetBestVirtualSize() const;
232
233 // change just the target window (unlike SetWindow which changes m_win as
234 // well)
235 void DoSetTargetWindow(wxWindow *target);
236
237 // delete the event handler we installed
238 void DeleteEvtHandler();
239
240 // calls wxScrollHelperEvtHandler::ResetDrawnFlag(), see explanation
241 // in wxScrollHelperEvtHandler::ProcessEvent()
242 void ResetDrawnFlag();
243
244 // this function should be overridden to return the size available for
245 // m_targetWindow inside m_win of the given size
246 //
247 // the default implementation is only good for m_targetWindow == m_win
248 // case, if we're scrolling a subwindow you must override this method
249 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size)
250 {
251 // returning just size from here is wrong but it was decided that it is
252 // not wrong enough to break the existing code (which doesn't override
253 // this recently added function at all) by adding this assert
254 //
255 // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" );
256
257 return size;
258 }
259
260
261 double m_scaleX;
262 double m_scaleY;
263
264 wxWindow *m_win,
265 *m_targetWindow;
266
267 wxRect m_rectToScroll;
268
269 wxTimer *m_timerAutoScroll;
270
271 int m_xScrollPixelsPerLine;
272 int m_yScrollPixelsPerLine;
273 int m_xScrollPosition;
274 int m_yScrollPosition;
275 int m_xScrollLines;
276 int m_yScrollLines;
277 int m_xScrollLinesPerPage;
278 int m_yScrollLinesPerPage;
279
280 bool m_xScrollingEnabled;
281 bool m_yScrollingEnabled;
282
283 #if wxUSE_MOUSEWHEEL
284 int m_wheelRotation;
285 #endif // wxUSE_MOUSEWHEEL
286
287 wxScrollHelperEvtHandler *m_handler;
288
289 DECLARE_NO_COPY_CLASS(wxScrollHelperBase)
290 };
291
292 // this macro can be used in a wxScrollHelper-derived class to forward wxWindow
293 // methods to corresponding wxScrollHelper methods
294 #define WX_FORWARD_TO_SCROLL_HELPER() \
295 public: \
296 virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \
297 virtual bool Layout() { return ScrollLayout(); } \
298 virtual void DoSetVirtualSize(int x, int y) \
299 { ScrollDoSetVirtualSize(x, y); } \
300 virtual wxSize GetBestVirtualSize() const \
301 { return ScrollGetBestVirtualSize(); }
302
303 // include the declaration of the real wxScrollHelper
304 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
305 #include "wx/gtk/scrolwin.h"
306 #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
307 #include "wx/gtk1/scrolwin.h"
308 #else
309 #define wxHAS_GENERIC_SCROLLWIN
310 #include "wx/generic/scrolwin.h"
311 #endif
312
313 // ----------------------------------------------------------------------------
314 // wxScrolled<T>: a wxWindow which knows how to scroll
315 // ----------------------------------------------------------------------------
316
317 // helper class for wxScrolled<T> below
318 struct WXDLLIMPEXP_CORE wxScrolledT_Helper
319 {
320 static wxSize FilterBestSize(const wxWindow *win,
321 const wxScrollHelper *helper,
322 const wxSize& origBest);
323 #ifdef __WXMSW__
324 static WXLRESULT FilterMSWWindowProc(WXUINT nMsg, WXLRESULT origResult);
325 #endif
326 };
327
328 // Scrollable window base on window type T. This used to be wxScrolledWindow,
329 // but wxScrolledWindow includes wxControlContainer functionality and that's
330 // not always desirable.
331 template<class T>
332 class WXDLLIMPEXP_CORE wxScrolled : public T,
333 public wxScrollHelper,
334 private wxScrolledT_Helper
335 {
336 public:
337 wxScrolled() : wxScrollHelper(this) { }
338 wxScrolled(wxWindow *parent,
339 wxWindowID winid = wxID_ANY,
340 const wxPoint& pos = wxDefaultPosition,
341 const wxSize& size = wxDefaultSize,
342 long style = wxScrolledWindowStyle,
343 const wxString& name = wxPanelNameStr)
344 : wxScrollHelper(this)
345 {
346 Create(parent, winid, pos, size, style, name);
347 }
348
349 bool Create(wxWindow *parent,
350 wxWindowID winid,
351 const wxPoint& pos = wxDefaultPosition,
352 const wxSize& size = wxDefaultSize,
353 long style = wxScrolledWindowStyle,
354 const wxString& name = wxPanelNameStr)
355 {
356 m_targetWindow = this;
357
358 #ifdef __WXMAC__
359 this->MacSetClipChildren(true);
360 #endif
361
362 this->Connect(wxEVT_PAINT, wxPaintEventHandler(wxScrolled::OnPaint));
363
364 // by default, we're scrollable in both directions (but if one of the
365 // styles is specified explicitly, we shouldn't add the other one
366 // automatically)
367 if ( !(style & (wxHSCROLL | wxVSCROLL)) )
368 style |= wxHSCROLL | wxVSCROLL;
369
370 return T::Create(parent, winid, pos, size, style, name);
371 }
372
373 // we need to return a special WM_GETDLGCODE value to process just the
374 // arrows but let the other navigation characters through
375 #ifdef __WXMSW__
376 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
377 {
378 return FilterMSWWindowProc(nMsg, T::MSWWindowProc(nMsg, wParam, lParam));
379 }
380 #endif // __WXMSW__
381
382 WX_FORWARD_TO_SCROLL_HELPER()
383
384 protected:
385 virtual wxSize DoGetBestSize() const
386 {
387 return FilterBestSize(this, this, T::DoGetBestSize());
388 }
389
390 private:
391 // this is needed for wxEVT_PAINT processing hack described in
392 // wxScrollHelperEvtHandler::ProcessEvent()
393 void OnPaint(wxPaintEvent& event)
394 {
395 // the user code didn't really draw the window if we got here, so set
396 // this flag to try to call OnDraw() later
397 ResetDrawnFlag();
398 event.Skip();
399 }
400
401 // VC++ 6 gives warning for the declaration of template member function
402 // without definition
403 #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
404 DECLARE_NO_COPY_CLASS(wxScrolled)
405 #endif
406 };
407
408 // VC++ <= 6 requires this; it's unlikely any other specializations would
409 // be needed by user code _and_ they were using VC6, so we list only wxWindow
410 // (typical use) and wxPanel (wxScrolledWindow use) specializations here
411 WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled<wxPanel> )
412 WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled<wxWindow> )
413
414 // for compatibility with existing code, we provide wxScrolledWindow
415 // "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we
416 // want wxScrolledWindow to show in wxRTTI information (the class is widely
417 // used and likelihood of its wxRTTI information being used too is high):
418 class WXDLLIMPEXP_CORE wxScrolledWindow : public wxScrolled<wxPanel>
419 {
420 public:
421 wxScrolledWindow() : wxScrolled<wxPanel>() {}
422 wxScrolledWindow(wxWindow *parent,
423 wxWindowID winid = wxID_ANY,
424 const wxPoint& pos = wxDefaultPosition,
425 const wxSize& size = wxDefaultSize,
426 long style = wxScrolledWindowStyle,
427 const wxString& name = wxPanelNameStr)
428 : wxScrolled<wxPanel>(parent, winid, pos, size, style, name) {}
429
430 DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow)
431 };
432
433 typedef wxScrolled<wxWindow> wxScrolledCanvas;
434
435 #endif // _WX_SCROLWIN_H_BASE_