]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: scrolwin.h | |
e54c96f1 | 3 | // Purpose: interface of wxScrolledWindow |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
23324ae1 | 10 | @wxheader{scrolwin.h} |
7c913512 | 11 | |
16361ec9 | 12 | The wxScrolled<T> class manages scrolling for its client area, transforming |
23324ae1 FM |
13 | the coordinates according to the scrollbar positions, and setting the |
14 | scroll positions, thumb sizes and ranges according to the area in view. | |
7c913512 | 15 | |
16361ec9 VS |
16 | There are two commonly used (but not the only possible!) specializations of |
17 | this class: | |
18 | ||
19 | - ::wxScrolledWindow, aka wxScrolled<wxPanel>, is equivalent to | |
20 | ::wxScrolledWindow from earlier versions. Derived from wxPanel, it shares | |
21 | wxPanel's behaviour with regard to TAB traversal and focus handling. Use | |
22 | this if the scrolled window will have children controls. | |
23 | ||
24 | - ::wxScrolledCanvas, aka wxScrolled<wxWindow>, derives from wxWindow and | |
25 | so doesn't handle children specially. This is suitable e.g. for | |
26 | implementating scrollable controls such as tree or list controls. | |
27 | ||
23324ae1 | 28 | Starting from version 2.4 of wxWidgets, there are several ways to use a |
16361ec9 | 29 | wxScrolled<T>. In particular, there are now three ways to set the |
23324ae1 | 30 | size of the scrolling area: |
7c913512 | 31 | |
16361ec9 VS |
32 | One way is to set the scrollbars directly using a call to SetScrollbars(). |
33 | This is the way it used to be in any previous version of wxWidgets and it | |
34 | will be kept for backwards compatibility. | |
7c913512 | 35 | |
23324ae1 FM |
36 | An additional method of manual control, which requires a little less |
37 | computation of your own, is to set the total size of the scrolling area by | |
16361ec9 VS |
38 | calling either wxWindow::SetVirtualSize(), or wxWindow::FitInside(), and |
39 | setting the scrolling increments for it by calling SetScrollRate(). | |
23324ae1 FM |
40 | Scrolling in some orientation is enabled by setting a non-zero increment |
41 | for it. | |
7c913512 | 42 | |
23324ae1 | 43 | The most automatic and newest way is to simply let sizers determine the |
16361ec9 VS |
44 | scrolling area. This is now the default when you set an interior sizer into |
45 | a wxScrolled<T> with wxWindow::SetSizer(). The scrolling area will be | |
46 | set to the size requested by the sizer and the scrollbars will be assigned | |
47 | for each orientation according to the need for them and the scrolling | |
48 | increment set by SetScrollRate(). As above, scrolling is only enabled in | |
49 | orientations with a non-zero increment. You can influence the minimum size | |
50 | of the scrolled area controlled by a sizer by calling | |
51 | wxWindow::SetVirtualSizeHints(). (Calling SetScrollbars() has analogous | |
52 | effects in wxWidgets 2.4 -- in later versions it may not continue to | |
53 | override the sizer.) | |
54 | ||
55 | Note that if maximum size hints are still supported by | |
56 | wxWindow::SetVirtualSizeHints(), use them at your own dire risk. They may | |
57 | or may not have been removed for 2.4, but it really only makes sense to set | |
58 | minimum size hints here. We should probably replace | |
59 | wxWindow::SetVirtualSizeHints() with wxWindow::SetMinVirtualSize() or | |
60 | similar and remove it entirely in future. | |
61 | ||
62 | As with all windows, an application can draw onto a wxScrolled<T> using | |
23324ae1 | 63 | a @ref overview_dcoverview "device context". |
7c913512 | 64 | |
16361ec9 VS |
65 | You have the option of handling the OnPaint handler or overriding the |
66 | wxScrolled<T>::OnDraw() function, which is passed a pre-scrolled device | |
67 | context (prepared by wxScrolled<T>::DoPrepareDC()). | |
68 | ||
69 | If you don't wish to calculate your own scrolling, you must call | |
70 | DoPrepareDC() when not drawing from within OnDraw(), to set the device | |
71 | origin for the device context according to the current scroll position. | |
72 | ||
73 | A wxScrolled<T> will normally scroll itself and therefore its child windows | |
74 | as well. It might however be desired to scroll a different window than | |
75 | itself: e.g. when designing a spreadsheet, you will normally only have to | |
76 | scroll the (usually white) cell area, whereas the (usually grey) label area | |
77 | will scroll very differently. For this special purpose, you can call | |
78 | SetTargetWindow() which means that pressing the scrollbars will scroll a | |
79 | different window. | |
80 | ||
81 | Note that the underlying system knows nothing about scrolling coordinates, | |
82 | so that all system functions (mouse events, expose events, refresh calls | |
83 | etc) as well as the position of subwindows are relative to the "physical" | |
84 | origin of the scrolled window. If the user insert a child window at | |
23324ae1 | 85 | position (10,10) and scrolls the window down 100 pixels (moving the child |
16361ec9 VS |
86 | window out of the visible area), the child window will report a position |
87 | of (10,-90). | |
7c913512 | 88 | |
23324ae1 | 89 | @beginStyleTable |
8c6791e4 | 90 | @style{wxRETAINED} |
23324ae1 FM |
91 | Uses a backing pixmap to speed refreshes. Motif only. |
92 | @endStyleTable | |
7c913512 | 93 | |
16361ec9 VS |
94 | @remarks |
95 | Use wxScrolled<T> for applications where the user scrolls by a fixed | |
96 | amount, and where a 'page' can be interpreted to be the current visible | |
97 | portion of the window. For more sophisticated applications, use the | |
98 | wxScrolled<T> implementation as a guide to build your own scroll | |
99 | behaviour or use wxVScrolledWindow or its variants. | |
100 | ||
101 | @since wxScrolled<T> template exists since version 2.9.0. In older | |
102 | versions, only ::wxScrolledWindow (equivalent of wxScrolled<wxPanel>) | |
103 | was available. | |
104 | ||
23324ae1 FM |
105 | @library{wxcore} |
106 | @category{miscwnd} | |
7c913512 | 107 | |
16361ec9 VS |
108 | @see wxScrollBar, wxClientDC, wxPaintDC, |
109 | wxVScrolledWindow, wxHScrolledWindow, wxHVScrolledWindow, | |
23324ae1 | 110 | */ |
16361ec9 VS |
111 | template<class T> |
112 | class wxScrolled : public T | |
23324ae1 FM |
113 | { |
114 | public: | |
16361ec9 VS |
115 | /// Default constructor. |
116 | wxScrolled(); | |
117 | ||
23324ae1 FM |
118 | /** |
119 | Constructor. | |
3c4f71cc | 120 | |
7c913512 | 121 | @param parent |
4cc4bfaf | 122 | Parent window. |
7c913512 | 123 | @param id |
16361ec9 | 124 | Window identifier. The value @c wxID_ANY indicates a default value. |
7c913512 | 125 | @param pos |
16361ec9 VS |
126 | Window position. If a position of @c wxDefaultPosition is specified |
127 | then a default position is chosen. | |
7c913512 | 128 | @param size |
16361ec9 VS |
129 | Window size. If a size of @c wxDefaultSize is specified then the |
130 | window is sized appropriately. | |
7c913512 | 131 | @param style |
16361ec9 | 132 | Window style. See wxScrolled<T>. |
7c913512 | 133 | @param name |
4cc4bfaf | 134 | Window name. |
3c4f71cc | 135 | |
16361ec9 VS |
136 | @remarks The window is initially created without visible scrollbars. |
137 | Call SetScrollbars() to specify how big the virtual window | |
138 | size should be. | |
23324ae1 | 139 | */ |
16361ec9 VS |
140 | wxScrolled(wxWindow* parent, wxWindowID id = -1, |
141 | const wxPoint& pos = wxDefaultPosition, | |
142 | const wxSize& size = wxDefaultSize, | |
143 | long style = wxHSCROLL | wxVSCROLL, | |
144 | const wxString& name = "scrolledWindow"); | |
23324ae1 | 145 | |
23324ae1 FM |
146 | |
147 | /** | |
16361ec9 VS |
148 | Translates the logical coordinates to the device ones. For example, if |
149 | a window is scrolled 10 pixels to the bottom, the device coordinates of | |
150 | the origin are (0, 0) (as always), but the logical coordinates are (0, | |
151 | 10) and so the call to CalcScrolledPosition(0, 10, xx, yy) will return | |
152 | 0 in yy. | |
3c4f71cc | 153 | |
4cc4bfaf | 154 | @see CalcUnscrolledPosition() |
23324ae1 | 155 | */ |
328f5751 | 156 | void CalcScrolledPosition(int x, int y, int* xx, int* yy) const; |
23324ae1 FM |
157 | |
158 | /** | |
16361ec9 VS |
159 | Translates the device coordinates to the logical ones. For example, if |
160 | a window is scrolled 10 pixels to the bottom, the device coordinates of | |
161 | the origin are (0, 0) (as always), but the logical coordinates are (0, | |
162 | 10) and so the call to CalcUnscrolledPosition(0, 0, xx, yy) will return | |
163 | 10 in yy. | |
3c4f71cc | 164 | |
4cc4bfaf | 165 | @see CalcScrolledPosition() |
23324ae1 | 166 | */ |
328f5751 | 167 | void CalcUnscrolledPosition(int x, int y, int* xx, int* yy) const; |
23324ae1 FM |
168 | |
169 | /** | |
170 | Creates the window for two-step construction. Derived classes | |
16361ec9 | 171 | should call or replace this function. See wxScrolled<T> constructor |
23324ae1 FM |
172 | for details. |
173 | */ | |
174 | bool Create(wxWindow* parent, wxWindowID id = -1, | |
175 | const wxPoint& pos = wxDefaultPosition, | |
176 | const wxSize& size = wxDefaultSize, | |
4cc4bfaf | 177 | long style = wxHSCROLL | wxVSCROLL, |
23324ae1 FM |
178 | const wxString& name = "scrolledWindow"); |
179 | ||
180 | /** | |
16361ec9 VS |
181 | Call this function to prepare the device context for drawing a scrolled |
182 | image. | |
183 | ||
184 | It sets the device origin according to the current scroll position. | |
185 | DoPrepareDC() is called automatically within the default OnPaint() | |
186 | event handler, so your OnDraw() override will be passed a | |
187 | 'pre-scrolled' device context. However, if you wish to draw from | |
188 | outside of OnDraw() (via OnPaint()), or you wish to implement OnPaint() | |
189 | yourself, you must call this function yourself. | |
190 | ||
191 | For example: | |
192 | @code | |
193 | void MyWindow::OnEvent(wxMouseEvent& event) | |
194 | { | |
195 | wxClientDC dc(this); | |
196 | DoPrepareDC(dc); | |
197 | ||
198 | dc.SetPen(*wxBLACK_PEN); | |
199 | float x, y; | |
200 | event.Position(&x, &y); | |
201 | if (xpos > -1 && ypos > -1 && event.Dragging()) | |
202 | { | |
203 | dc.DrawLine(xpos, ypos, x, y); | |
204 | } | |
205 | xpos = x; | |
206 | ypos = y; | |
207 | } | |
208 | @endcode | |
209 | ||
23324ae1 FM |
210 | */ |
211 | void DoPrepareDC(wxDC& dc); | |
212 | ||
213 | /** | |
214 | Enable or disable physical scrolling in the given direction. Physical | |
215 | scrolling is the physical transfer of bits up or down the | |
216 | screen when a scroll event occurs. If the application scrolls by a | |
217 | variable amount (e.g. if there are different font sizes) then physical | |
218 | scrolling will not work, and you should switch it off. Note that you | |
219 | will have to reposition child windows yourself, if physical scrolling | |
220 | is disabled. | |
3c4f71cc | 221 | |
7c913512 | 222 | @param xScrolling |
4cc4bfaf | 223 | If @true, enables physical scrolling in the x direction. |
7c913512 | 224 | @param yScrolling |
4cc4bfaf | 225 | If @true, enables physical scrolling in the y direction. |
3c4f71cc | 226 | |
23324ae1 | 227 | @remarks Physical scrolling may not be available on all platforms. Where |
4cc4bfaf | 228 | it is available, it is enabled by default. |
23324ae1 FM |
229 | */ |
230 | void EnableScrolling(bool xScrolling, bool yScrolling); | |
231 | ||
232 | /** | |
16361ec9 VS |
233 | Get the number of pixels per scroll unit (line), in each direction, as |
234 | set by SetScrollbars(). A value of zero indicates no scrolling in that | |
235 | direction. | |
3c4f71cc | 236 | |
7c913512 | 237 | @param xUnit |
4cc4bfaf | 238 | Receives the number of pixels per horizontal unit. |
7c913512 | 239 | @param yUnit |
4cc4bfaf | 240 | Receives the number of pixels per vertical unit. |
3c4f71cc | 241 | |
4cc4bfaf | 242 | @see SetScrollbars(), GetVirtualSize() |
23324ae1 | 243 | */ |
328f5751 | 244 | void GetScrollPixelsPerUnit(int* xUnit, int* yUnit) const; |
23324ae1 FM |
245 | |
246 | /** | |
247 | Get the position at which the visible portion of the window starts. | |
3c4f71cc | 248 | |
7c913512 | 249 | @param x |
4cc4bfaf | 250 | Receives the first visible x position in scroll units. |
7c913512 | 251 | @param y |
4cc4bfaf | 252 | Receives the first visible y position in scroll units. |
3c4f71cc | 253 | |
23324ae1 | 254 | @remarks If either of the scrollbars is not at the home position, x |
4cc4bfaf | 255 | and/or y will be greater than zero. Combined with |
16361ec9 | 256 | wxWindow::GetClientSize(), the application can use this |
4cc4bfaf FM |
257 | function to efficiently redraw only the visible portion |
258 | of the window. The positions are in logical scroll | |
259 | units, not pixels, so to convert to pixels you will | |
260 | have to multiply by the number of pixels per scroll | |
261 | increment. | |
3c4f71cc | 262 | |
4cc4bfaf | 263 | @see SetScrollbars() |
23324ae1 | 264 | */ |
328f5751 | 265 | void GetViewStart(int* x, int* y) const; |
23324ae1 FM |
266 | |
267 | /** | |
268 | Gets the size in device units of the scrollable window area (as | |
269 | opposed to the client size, which is the area of the window currently | |
270 | visible). | |
3c4f71cc | 271 | |
7c913512 | 272 | @param x |
4cc4bfaf | 273 | Receives the length of the scrollable window, in pixels. |
7c913512 | 274 | @param y |
4cc4bfaf | 275 | Receives the height of the scrollable window, in pixels. |
3c4f71cc | 276 | |
16361ec9 | 277 | @remarks Use wxDC::DeviceToLogicalX() and wxDC::DeviceToLogicalY() to |
4cc4bfaf | 278 | translate these units to logical units. |
3c4f71cc | 279 | |
4cc4bfaf | 280 | @see SetScrollbars(), GetScrollPixelsPerUnit() |
23324ae1 | 281 | */ |
328f5751 | 282 | void GetVirtualSize(int* x, int* y) const; |
23324ae1 FM |
283 | |
284 | /** | |
285 | Motif only: @true if the window has a backing bitmap. | |
286 | */ | |
328f5751 | 287 | bool IsRetained() const; |
23324ae1 FM |
288 | |
289 | /** | |
16361ec9 VS |
290 | Called by the default paint event handler to allow the application to |
291 | define painting behaviour without having to worry about calling | |
23324ae1 | 292 | DoPrepareDC(). |
16361ec9 VS |
293 | |
294 | Instead of overriding this function you may also just process the paint | |
295 | event in the derived class as usual, but then you will have to call | |
296 | DoPrepareDC() yourself. | |
23324ae1 FM |
297 | */ |
298 | virtual void OnDraw(wxDC& dc); | |
299 | ||
300 | /** | |
7c913512 | 301 | This function is for backwards compatibility only and simply calls |
16361ec9 VS |
302 | DoPrepareDC() now. Notice that it is not called by the default paint |
303 | event handle (DoPrepareDC() is), so overriding this method in your | |
304 | derived class is useless. | |
23324ae1 FM |
305 | */ |
306 | void PrepareDC(wxDC& dc); | |
307 | ||
308 | /** | |
309 | Scrolls a window so the view start is at the given point. | |
3c4f71cc | 310 | |
7c913512 | 311 | @param x |
4cc4bfaf | 312 | The x position to scroll to, in scroll units. |
7c913512 | 313 | @param y |
4cc4bfaf | 314 | The y position to scroll to, in scroll units. |
3c4f71cc | 315 | |
23324ae1 | 316 | @remarks The positions are in scroll units, not pixels, so to convert to |
4cc4bfaf FM |
317 | pixels you will have to multiply by the number of |
318 | pixels per scroll increment. If either parameter is -1, | |
319 | that position will be ignored (no change in that | |
320 | direction). | |
3c4f71cc | 321 | |
4cc4bfaf | 322 | @see SetScrollbars(), GetScrollPixelsPerUnit() |
23324ae1 FM |
323 | */ |
324 | void Scroll(int x, int y); | |
325 | ||
326 | /** | |
16361ec9 VS |
327 | Set the horizontal and vertical scrolling increment only. See the |
328 | pixelsPerUnit parameter in SetScrollbars(). | |
23324ae1 FM |
329 | */ |
330 | void SetScrollRate(int xstep, int ystep); | |
331 | ||
332 | /** | |
333 | Sets up vertical and/or horizontal scrollbars. | |
3c4f71cc | 334 | |
16361ec9 VS |
335 | The first pair of parameters give the number of pixels per 'scroll |
336 | step', i.e. amount moved when the up or down scroll arrows are pressed. | |
337 | The second pair gives the length of scrollbar in scroll steps, which | |
338 | sets the size of the virtual window. | |
339 | ||
340 | @a xPos and @a yPos optionally specify a position to scroll to | |
341 | immediately. | |
342 | ||
343 | For example, the following gives a window horizontal and vertical | |
344 | scrollbars with 20 pixels per scroll step, and a size of 50 steps (1000 | |
345 | pixels) in each direction: | |
346 | @code | |
347 | window->SetScrollbars(20, 20, 50, 50); | |
348 | @endcode | |
349 | ||
350 | wxScrolled<T> manages the page size itself, using the current client | |
351 | window size as the page size. | |
352 | ||
353 | Note that for more sophisticated scrolling applications, for example | |
354 | where scroll steps may be variable according to the position in the | |
355 | document, it will be necessary to derive a new class from wxWindow, | |
356 | overriding OnSize() and adjusting the scrollbars appropriately. | |
357 | ||
7c913512 | 358 | @param pixelsPerUnitX |
4cc4bfaf | 359 | Pixels per scroll unit in the horizontal direction. |
7c913512 | 360 | @param pixelsPerUnitY |
4cc4bfaf | 361 | Pixels per scroll unit in the vertical direction. |
7c913512 | 362 | @param noUnitsX |
4cc4bfaf | 363 | Number of units in the horizontal direction. |
7c913512 | 364 | @param noUnitsY |
4cc4bfaf | 365 | Number of units in the vertical direction. |
7c913512 | 366 | @param xPos |
16361ec9 VS |
367 | Position to initialize the scrollbars in the horizontal direction, |
368 | in scroll units. | |
7c913512 | 369 | @param yPos |
16361ec9 VS |
370 | Position to initialize the scrollbars in the vertical direction, in |
371 | scroll units. | |
7c913512 | 372 | @param noRefresh |
4cc4bfaf | 373 | Will not refresh window if @true. |
3c4f71cc | 374 | |
16361ec9 | 375 | @see wxWindow::SetVirtualSize() |
23324ae1 FM |
376 | */ |
377 | void SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY, | |
378 | int noUnitsX, | |
379 | int noUnitsY, | |
380 | int xPos = 0, | |
381 | int yPos = 0, | |
4cc4bfaf | 382 | bool noRefresh = false); |
23324ae1 FM |
383 | |
384 | /** | |
16361ec9 VS |
385 | Call this function to tell wxScrolled to perform the actual |
386 | scrolling on a different window (and not on itself). | |
23324ae1 FM |
387 | */ |
388 | void SetTargetWindow(wxWindow* window); | |
389 | }; | |
e54c96f1 | 390 | |
16361ec9 VS |
391 | |
392 | /** | |
393 | Scrolled window derived from wxPanel. | |
394 | ||
395 | See wxScrolled<T> for detailed description. | |
396 | ||
397 | @note Note that because this class derives from wxPanel, it shares its | |
398 | behavior with regard to TAB traversal and focus handling (in | |
399 | particular, it forwards focus to its children). If you don't want | |
400 | this behaviour, use ::wxScrolledCanvas instead. | |
401 | ||
402 | @note wxScrolledWindow is an alias for wxScrolled<wxPanel> since version | |
403 | 2.9.0. In older versions, it was a standalone class. | |
404 | ||
405 | @library{wxcore} | |
406 | @category{miscwnd} | |
407 | ||
408 | @see wxScrolled, wxScrolledCanvas | |
409 | */ | |
410 | typedef wxScrolled<wxPanel> wxScrolledWindow; | |
411 | ||
412 | /** | |
413 | Alias for wxScrolled<wxWindow>. Scrolled window that doesn't have children | |
414 | and so doesn't need or want special handling of TAB traversal. | |
415 | ||
416 | @since 2.9.0 | |
417 | ||
418 | @library{wxcore} | |
419 | @category{miscwnd} | |
420 | ||
421 | @see wxScrolled, ::wxScrolledWindow | |
422 | */ | |
423 | typedef wxScrolled<wxWindow> wxScrolledCanvas; |