]> git.saurik.com Git - wxWidgets.git/blame - include/wx/scrolwin.h
add wxVector(size_t size[, const value_type& value]) ctors
[wxWidgets.git] / include / wx / scrolwin.h
CommitLineData
1e6feb95
VZ
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>
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
1e6feb95 23// ----------------------------------------------------------------------------
d32e78bd
VZ
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//
1e6feb95
VZ
49// ----------------------------------------------------------------------------
50
53a2db12 51class WXDLLIMPEXP_CORE wxScrollHelper
fa3541bd
JS
52{
53public:
d32e78bd
VZ
54 // ctor must be given the associated window
55 wxScrollHelper(wxWindow *winToScroll);
1e6feb95
VZ
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,
d775fa82 62 bool noRefresh = false );
1e6feb95
VZ
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
5713b349
RR
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
566d84a7
RL
75 // Set the x, y scrolling increments.
76 void SetScrollRate( int xstep, int ystep );
77
1e6feb95
VZ
78 // get the size of one logical unit in physical ones
79 virtual void GetScrollPixelsPerUnit(int *pixelsPerUnitX,
80 int *pixelsPerUnitY) const;
81
d775fa82 82 // Enable/disable Windows scrolling in either direction. If true, wxWidgets
1e6feb95 83 // scrolls the canvas and only a bit of the canvas is invalidated; no
d775fa82 84 // Clear() is necessary. If false, the whole canvas is invalidated and a
1e6feb95
VZ
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
1e6feb95
VZ
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
8c2f3797
VS
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 }
0d6d6051 115
8c2f3797
VS
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;
1e6feb95
VZ
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).
af4088f1 129 virtual void SetTargetWindow(wxWindow *target);
1e6feb95
VZ
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 //
d775fa82 152 // the base class version just returns true
1e6feb95
VZ
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);
e421922f 162#if wxUSE_MOUSEWHEEL
1e6feb95 163 void HandleOnMouseWheel(wxMouseEvent& event);
e421922f 164#endif // wxUSE_MOUSEWHEEL
06b9c2a2 165 void HandleOnChildFocus(wxChildFocusEvent& event);
1e6feb95 166
2ec0173d
VZ
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
1e6feb95
VZ
171protected:
172 // get pointer to our scroll rect if we use it or NULL
d5d58a93 173 const wxRect *GetScrollRect() const
1e6feb95
VZ
174 {
175 return m_rectToScroll.width != 0 ? &m_rectToScroll : NULL;
176 }
177
178 // get the size of the target window
179 wxSize GetTargetSize() const
fa3541bd 180 {
1e6feb95
VZ
181 return m_rectToScroll.width != 0 ? m_rectToScroll.GetSize()
182 : m_targetWindow->GetClientSize();
fa3541bd 183 }
1e6feb95 184
2d6dd9c0 185 void GetTargetSize(int *w, int *h) const
1e6feb95
VZ
186 {
187 wxSize size = GetTargetSize();
188 if ( w )
189 *w = size.x;
190 if ( h )
191 *h = size.y;
192 }
193
d32e78bd
VZ
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;
d32e78bd 199
349efbaa
VZ
200 // change just the target window (unlike SetWindow which changes m_win as
201 // well)
af4088f1 202 void DoSetTargetWindow(wxWindow *target);
349efbaa
VZ
203
204 // delete the event handler we installed
205 void DeleteEvtHandler();
206
16361ec9
VS
207 // calls wxScrollHelperEvtHandler::ResetDrawnFlag(), see explanation
208 // in wxScrollHelperEvtHandler::ProcessEvent()
209 void ResetDrawnFlag();
210
69367c56
VZ
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
f9ec0ea7
VZ
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 {
5eda3bcd
VZ
229 // returning just size from here is wrong but it was decided that it is
230 // not wrong enough to break the existing code (which doesn't override
231 // this recently added function at all) by adding this assert
232 //
233 // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" );
f9ec0ea7
VZ
234
235 return size;
236 }
237
d32e78bd 238
39ae1f13
WS
239 double m_scaleX;
240 double m_scaleY;
241
1e6feb95
VZ
242 wxWindow *m_win,
243 *m_targetWindow;
244
245 wxRect m_rectToScroll;
246
247 wxTimer *m_timerAutoScroll;
248
249 int m_xScrollPixelsPerLine;
250 int m_yScrollPixelsPerLine;
251 int m_xScrollPosition;
252 int m_yScrollPosition;
253 int m_xScrollLines;
254 int m_yScrollLines;
255 int m_xScrollLinesPerPage;
256 int m_yScrollLinesPerPage;
257
258 bool m_xScrollingEnabled;
259 bool m_yScrollingEnabled;
260
e421922f
VZ
261#if wxUSE_MOUSEWHEEL
262 int m_wheelRotation;
263#endif // wxUSE_MOUSEWHEEL
349efbaa
VZ
264
265 wxScrollHelperEvtHandler *m_handler;
22f3361e 266
69367c56 267
22f3361e 268 DECLARE_NO_COPY_CLASS(wxScrollHelper)
fa3541bd 269};
1e6feb95 270
d32e78bd
VZ
271// this macro can be used in a wxScrollHelper-derived class to forward wxWindow
272// methods to corresponding wxScrollHelper methods
273#define WX_FORWARD_TO_SCROLL_HELPER() \
8b64a7e2 274public: \
d32e78bd
VZ
275 virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \
276 virtual bool Layout() { return ScrollLayout(); } \
277 virtual void DoSetVirtualSize(int x, int y) \
278 { ScrollDoSetVirtualSize(x, y); } \
279 virtual wxSize GetBestVirtualSize() const \
7d616e99 280 { return ScrollGetBestVirtualSize(); }
d32e78bd
VZ
281
282// include the declaration of wxScrollHelperNative if needed
1be7a35c 283#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
d32e78bd 284 #include "wx/gtk/scrolwin.h"
1be7a35c
MR
285#elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
286 #include "wx/gtk1/scrolwin.h"
d32e78bd
VZ
287#else
288 typedef wxScrollHelper wxScrollHelperNative;
adfea104
JS
289#endif
290
1e6feb95 291// ----------------------------------------------------------------------------
16361ec9 292// wxScrolled<T>: a wxWindow which knows how to scroll
1e6feb95
VZ
293// ----------------------------------------------------------------------------
294
16361ec9
VS
295// helper class for wxScrolled<T> below
296struct WXDLLIMPEXP_CORE wxScrolledT_Helper
297{
298 static wxSize FilterBestSize(const wxWindow *win,
299 const wxScrollHelperNative *helper,
300 const wxSize& origBest);
301#ifdef __WXMSW__
f2a6c918 302 static WXLRESULT FilterMSWWindowProc(WXUINT nMsg, WXLRESULT origResult);
16361ec9
VS
303#endif
304};
305
306// Scrollable window base on window type T. This used to be wxScrolledWindow,
307// but wxScrolledWindow includes wxControlContainer functionality and that's
308// not always desirable.
309template<class T>
310class WXDLLIMPEXP_CORE wxScrolled : public T,
311 public wxScrollHelperNative,
312 private wxScrolledT_Helper
d32e78bd
VZ
313{
314public:
16361ec9
VS
315 wxScrolled() : wxScrollHelperNative(this) { }
316 wxScrolled(wxWindow *parent,
317 wxWindowID winid = wxID_ANY,
318 const wxPoint& pos = wxDefaultPosition,
319 const wxSize& size = wxDefaultSize,
320 long style = wxScrolledWindowStyle,
321 const wxString& name = wxPanelNameStr)
d32e78bd 322 : wxScrollHelperNative(this)
1e6feb95 323 {
d32e78bd
VZ
324 Create(parent, winid, pos, size, style, name);
325 }
c801d85f 326
d32e78bd
VZ
327 bool Create(wxWindow *parent,
328 wxWindowID winid,
329 const wxPoint& pos = wxDefaultPosition,
330 const wxSize& size = wxDefaultSize,
331 long style = wxScrolledWindowStyle,
16361ec9
VS
332 const wxString& name = wxPanelNameStr)
333 {
334 m_targetWindow = this;
335
336#ifdef __WXMAC__
9fdb68d9 337 this->MacSetClipChildren(true);
16361ec9
VS
338#endif
339
340 this->Connect(wxEVT_PAINT, wxPaintEventHandler(wxScrolled::OnPaint));
341
342 // by default, we're scrollable in both directions (but if one of the
343 // styles is specified explicitly, we shouldn't add the other one
344 // automatically)
345 if ( !(style & (wxHSCROLL | wxVSCROLL)) )
346 style |= wxHSCROLL | wxVSCROLL;
347
348 return T::Create(parent, winid, pos, size, style, name);
349 }
d32e78bd 350
6f02a879
VZ
351 // we need to return a special WM_GETDLGCODE value to process just the
352 // arrows but let the other navigation characters through
353#ifdef __WXMSW__
16361ec9
VS
354 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
355 {
f2a6c918 356 return FilterMSWWindowProc(nMsg, T::MSWWindowProc(nMsg, wParam, lParam));
16361ec9 357 }
6f02a879
VZ
358#endif // __WXMSW__
359
d32e78bd
VZ
360 WX_FORWARD_TO_SCROLL_HELPER()
361
362protected:
16361ec9
VS
363 virtual wxSize DoGetBestSize() const
364 {
365 return FilterBestSize(this, this, T::DoGetBestSize());
366 }
7d616e99 367
16361ec9 368private:
d32e78bd
VZ
369 // this is needed for wxEVT_PAINT processing hack described in
370 // wxScrollHelperEvtHandler::ProcessEvent()
16361ec9
VS
371 void OnPaint(wxPaintEvent& event)
372 {
373 // the user code didn't really draw the window if we got here, so set
374 // this flag to try to call OnDraw() later
375 ResetDrawnFlag();
376 event.Skip();
377 }
378
707d9f40
VZ
379 // VC++ 6 gives warning for the declaration of template member function
380 // without definition
381#if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
16361ec9 382 DECLARE_NO_COPY_CLASS(wxScrolled)
707d9f40 383#endif
16361ec9
VS
384};
385
386// VC++ <= 6 requires this; it's unlikely any other specializations would
387// be needed by user code _and_ they were using VC6, so we list only wxWindow
388// (typical use) and wxPanel (wxScrolledWindow use) specializations here
389WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled<wxPanel> )
390WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled<wxWindow> )
391
392// for compatibility with existing code, we provide wxScrolledWindow
393// "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we
394// want wxScrolledWindow to show in wxRTTI information (the class is widely
395// used and likelihood of its wxRTTI information being used too is high):
396class WXDLLIMPEXP_CORE wxScrolledWindow : public wxScrolled<wxPanel>
397{
398public:
399 wxScrolledWindow() : wxScrolled<wxPanel>() {}
400 wxScrolledWindow(wxWindow *parent,
401 wxWindowID winid = wxID_ANY,
402 const wxPoint& pos = wxDefaultPosition,
403 const wxSize& size = wxDefaultSize,
404 long style = wxScrolledWindowStyle,
405 const wxString& name = wxPanelNameStr)
406 : wxScrolled<wxPanel>(parent, winid, pos, size, style, name) {}
d32e78bd 407
d32e78bd 408 DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow)
d32e78bd
VZ
409};
410
16361ec9 411typedef wxScrolled<wxWindow> wxScrolledCanvas;
566d84a7 412
16361ec9 413#endif // _WX_SCROLWIN_H_BASE_