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