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