]>
Commit | Line | Data |
---|---|---|
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 WXDLLEXPORT wxScrollHelperEvtHandler; | |
18 | class WXDLLEXPORT 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 WXDLLEXPORT 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 | // Set the x, y scrolling increments. | |
72 | void SetScrollRate( int xstep, int ystep ); | |
73 | ||
74 | // get the size of one logical unit in physical ones | |
75 | virtual void GetScrollPixelsPerUnit(int *pixelsPerUnitX, | |
76 | int *pixelsPerUnitY) const; | |
77 | ||
78 | // Enable/disable Windows scrolling in either direction. If true, wxWidgets | |
79 | // scrolls the canvas and only a bit of the canvas is invalidated; no | |
80 | // Clear() is necessary. If false, the whole canvas is invalidated and a | |
81 | // Clear() is necessary. Disable for when the scroll increment is used to | |
82 | // actually scroll a non-constant distance | |
83 | virtual void EnableScrolling(bool x_scrolling, bool y_scrolling); | |
84 | ||
85 | // Get the view start | |
86 | virtual void GetViewStart(int *x, int *y) const; | |
87 | ||
88 | // Set the scale factor, used in PrepareDC | |
89 | void SetScale(double xs, double ys) { m_scaleX = xs; m_scaleY = ys; } | |
90 | double GetScaleX() const { return m_scaleX; } | |
91 | double GetScaleY() const { return m_scaleY; } | |
92 | ||
93 | // translate between scrolled and unscrolled coordinates | |
94 | void CalcScrolledPosition(int x, int y, int *xx, int *yy) const | |
95 | { DoCalcScrolledPosition(x, y, xx, yy); } | |
96 | wxPoint CalcScrolledPosition(const wxPoint& pt) const | |
97 | { | |
98 | wxPoint p2; | |
99 | DoCalcScrolledPosition(pt.x, pt.y, &p2.x, &p2.y); | |
100 | return p2; | |
101 | } | |
102 | ||
103 | void CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const | |
104 | { DoCalcUnscrolledPosition(x, y, xx, yy); } | |
105 | wxPoint CalcUnscrolledPosition(const wxPoint& pt) const | |
106 | { | |
107 | wxPoint p2; | |
108 | DoCalcUnscrolledPosition(pt.x, pt.y, &p2.x, &p2.y); | |
109 | return p2; | |
110 | } | |
111 | ||
112 | virtual void DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const; | |
113 | virtual void DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const; | |
114 | ||
115 | // Adjust the scrollbars | |
116 | virtual void AdjustScrollbars(void); | |
117 | ||
118 | // Calculate scroll increment | |
119 | virtual int CalcScrollInc(wxScrollWinEvent& event); | |
120 | ||
121 | // Normally the wxScrolledWindow will scroll itself, but in some rare | |
122 | // occasions you might want it to scroll [part of] another window (e.g. a | |
123 | // child of it in order to scroll only a portion the area between the | |
124 | // scrollbars (spreadsheet: only cell area will move). | |
125 | virtual void SetTargetWindow(wxWindow *target); | |
126 | virtual wxWindow *GetTargetWindow() const; | |
127 | ||
128 | void SetTargetRect(const wxRect& rect) { m_rectToScroll = rect; } | |
129 | wxRect GetTargetRect() const { return m_rectToScroll; } | |
130 | ||
131 | // Override this function to draw the graphic (or just process EVT_PAINT) | |
132 | virtual void OnDraw(wxDC& WXUNUSED(dc)) { } | |
133 | ||
134 | // change the DC origin according to the scroll position. | |
135 | virtual void DoPrepareDC(wxDC& dc); | |
136 | ||
137 | // are we generating the autoscroll events? | |
138 | bool IsAutoScrolling() const { return m_timerAutoScroll != NULL; } | |
139 | ||
140 | // stop generating the scroll events when mouse is held outside the window | |
141 | void StopAutoScrolling(); | |
142 | ||
143 | // this method can be overridden in a derived class to forbid sending the | |
144 | // auto scroll events - note that unlike StopAutoScrolling() it doesn't | |
145 | // stop the timer, so it will be called repeatedly and will typically | |
146 | // return different values depending on the current mouse position | |
147 | // | |
148 | // the base class version just returns true | |
149 | virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const; | |
150 | ||
151 | // the methods to be called from the window event handlers | |
152 | void HandleOnScroll(wxScrollWinEvent& event); | |
153 | void HandleOnSize(wxSizeEvent& event); | |
154 | void HandleOnPaint(wxPaintEvent& event); | |
155 | void HandleOnChar(wxKeyEvent& event); | |
156 | void HandleOnMouseEnter(wxMouseEvent& event); | |
157 | void HandleOnMouseLeave(wxMouseEvent& event); | |
158 | #if wxUSE_MOUSEWHEEL | |
159 | void HandleOnMouseWheel(wxMouseEvent& event); | |
160 | #endif // wxUSE_MOUSEWHEEL | |
161 | ||
162 | // FIXME: this is needed for now for wxPlot compilation, should be removed | |
163 | // once it is fixed! | |
164 | void OnScroll(wxScrollWinEvent& event) { HandleOnScroll(event); } | |
165 | ||
166 | protected: | |
167 | // get pointer to our scroll rect if we use it or NULL | |
168 | const wxRect *GetScrollRect() const | |
169 | { | |
170 | return m_rectToScroll.width != 0 ? &m_rectToScroll : NULL; | |
171 | } | |
172 | ||
173 | // get the size of the target window | |
174 | wxSize GetTargetSize() const | |
175 | { | |
176 | return m_rectToScroll.width != 0 ? m_rectToScroll.GetSize() | |
177 | : m_targetWindow->GetClientSize(); | |
178 | } | |
179 | ||
180 | void GetTargetSize(int *w, int *h) | |
181 | { | |
182 | wxSize size = GetTargetSize(); | |
183 | if ( w ) | |
184 | *w = size.x; | |
185 | if ( h ) | |
186 | *h = size.y; | |
187 | } | |
188 | ||
189 | // implementations of various wxWindow virtual methods which should be | |
190 | // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER()) | |
191 | bool ScrollLayout(); | |
192 | void ScrollDoSetVirtualSize(int x, int y); | |
193 | wxSize ScrollGetBestVirtualSize() const; | |
194 | wxSize ScrollGetWindowSizeForVirtualSize(const wxSize& size) const; | |
195 | ||
196 | // change just the target window (unlike SetWindow which changes m_win as | |
197 | // well) | |
198 | void DoSetTargetWindow(wxWindow *target); | |
199 | ||
200 | // delete the event handler we installed | |
201 | void DeleteEvtHandler(); | |
202 | ||
203 | ||
204 | double m_scaleX; | |
205 | double m_scaleY; | |
206 | ||
207 | wxWindow *m_win, | |
208 | *m_targetWindow; | |
209 | ||
210 | wxRect m_rectToScroll; | |
211 | ||
212 | wxTimer *m_timerAutoScroll; | |
213 | ||
214 | int m_xScrollPixelsPerLine; | |
215 | int m_yScrollPixelsPerLine; | |
216 | int m_xScrollPosition; | |
217 | int m_yScrollPosition; | |
218 | int m_xScrollLines; | |
219 | int m_yScrollLines; | |
220 | int m_xScrollLinesPerPage; | |
221 | int m_yScrollLinesPerPage; | |
222 | ||
223 | bool m_xScrollingEnabled; | |
224 | bool m_yScrollingEnabled; | |
225 | ||
226 | #if wxUSE_MOUSEWHEEL | |
227 | int m_wheelRotation; | |
228 | #endif // wxUSE_MOUSEWHEEL | |
229 | ||
230 | wxScrollHelperEvtHandler *m_handler; | |
231 | ||
232 | DECLARE_NO_COPY_CLASS(wxScrollHelper) | |
233 | }; | |
234 | ||
235 | // this macro can be used in a wxScrollHelper-derived class to forward wxWindow | |
236 | // methods to corresponding wxScrollHelper methods | |
237 | #define WX_FORWARD_TO_SCROLL_HELPER() \ | |
238 | public: \ | |
239 | virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \ | |
240 | virtual bool Layout() { return ScrollLayout(); } \ | |
241 | virtual void DoSetVirtualSize(int x, int y) \ | |
242 | { ScrollDoSetVirtualSize(x, y); } \ | |
243 | virtual wxSize GetBestVirtualSize() const \ | |
244 | { return ScrollGetBestVirtualSize(); } \ | |
245 | protected: \ | |
246 | virtual wxSize GetWindowSizeForVirtualSize(const wxSize& size) const \ | |
247 | { return ScrollGetWindowSizeForVirtualSize(size); } | |
248 | ||
249 | // include the declaration of wxScrollHelperNative if needed | |
250 | #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) | |
251 | #include "wx/gtk/scrolwin.h" | |
252 | #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__) | |
253 | #include "wx/gtk1/scrolwin.h" | |
254 | #else | |
255 | typedef wxScrollHelper wxScrollHelperNative; | |
256 | #endif | |
257 | ||
258 | // ---------------------------------------------------------------------------- | |
259 | // wxScrolledWindow: a wxWindow which knows how to scroll | |
260 | // ---------------------------------------------------------------------------- | |
261 | ||
262 | class WXDLLEXPORT wxScrolledWindow : public wxPanel, | |
263 | public wxScrollHelperNative | |
264 | { | |
265 | public: | |
266 | wxScrolledWindow() : wxScrollHelperNative(this) { } | |
267 | wxScrolledWindow(wxWindow *parent, | |
268 | wxWindowID winid = wxID_ANY, | |
269 | const wxPoint& pos = wxDefaultPosition, | |
270 | const wxSize& size = wxDefaultSize, | |
271 | long style = wxScrolledWindowStyle, | |
272 | const wxString& name = wxPanelNameStr) | |
273 | : wxScrollHelperNative(this) | |
274 | { | |
275 | Create(parent, winid, pos, size, style, name); | |
276 | } | |
277 | ||
278 | virtual ~wxScrolledWindow(); | |
279 | ||
280 | bool Create(wxWindow *parent, | |
281 | wxWindowID winid, | |
282 | const wxPoint& pos = wxDefaultPosition, | |
283 | const wxSize& size = wxDefaultSize, | |
284 | long style = wxScrolledWindowStyle, | |
285 | const wxString& name = wxPanelNameStr); | |
286 | ||
287 | // we need to return a special WM_GETDLGCODE value to process just the | |
288 | // arrows but let the other navigation characters through | |
289 | #ifdef __WXMSW__ | |
290 | virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam); | |
291 | #endif // __WXMSW__ | |
292 | ||
293 | WX_FORWARD_TO_SCROLL_HELPER() | |
294 | ||
295 | protected: | |
296 | // this is needed for wxEVT_PAINT processing hack described in | |
297 | // wxScrollHelperEvtHandler::ProcessEvent() | |
298 | void OnPaint(wxPaintEvent& event); | |
299 | ||
300 | private: | |
301 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow) | |
302 | DECLARE_EVENT_TABLE() | |
303 | }; | |
304 | ||
305 | #endif // _WX_SCROLWIN_H_BASE_ | |
306 |