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