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