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