Document wxHSCROLL and wxVSCROLL styles for wxScrolled<>.
[wxWidgets.git] / interface / wx / scrolwin.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: scrolwin.h
3 // Purpose: interface of wxScrolled template
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 Possible values for the second argument of wxScrolled::ShowScrollbars().
11 */
12 enum wxScrollbarVisibility
13 {
14 wxSHOW_SB_NEVER = -1, ///< Never show the scrollbar at all.
15 wxSHOW_SB_DEFAULT, ///< Show scrollbar only if it is needed.
16 wxSHOW_SB_ALWAYS ///< Always show scrollbar, even if not needed.
17 };
18
19 /**
20
21 The wxScrolled class manages scrolling for its client area, transforming
22 the coordinates according to the scrollbar positions, and setting the
23 scroll positions, thumb sizes and ranges according to the area in view.
24
25 There are two commonly used (but not the only possible!) specializations of
26 this class:
27
28 - ::wxScrolledWindow, aka wxScrolled<wxPanel>, is equivalent to
29 ::wxScrolledWindow from earlier versions. Derived from wxPanel, it shares
30 wxPanel's behaviour with regard to TAB traversal and focus handling. Use
31 this if the scrolled window will have child controls.
32
33 - ::wxScrolledCanvas, aka wxScrolled<wxWindow>, derives from wxWindow and
34 so doesn't handle children specially. This is suitable e.g. for
35 implementing scrollable controls such as tree or list controls.
36
37 Starting from version 2.4 of wxWidgets, there are several ways to use a
38 ::wxScrolledWindow (and now wxScrolled). In particular, there are
39 three ways to set the size of the scrolling area:
40
41 One way is to set the scrollbars directly using a call to SetScrollbars().
42 This is the way it used to be in any previous version of wxWidgets and it
43 will be kept for backwards compatibility.
44
45 An additional method of manual control, which requires a little less
46 computation of your own, is to set the total size of the scrolling area by
47 calling either wxWindow::SetVirtualSize(), or wxWindow::FitInside(), and
48 setting the scrolling increments for it by calling SetScrollRate().
49 Scrolling in some orientation is enabled by setting a non-zero increment
50 for it.
51
52 The most automatic and newest way is to simply let sizers determine the
53 scrolling area. This is now the default when you set an interior sizer into
54 a wxScrolled with wxWindow::SetSizer(). The scrolling area will be
55 set to the size requested by the sizer and the scrollbars will be assigned
56 for each orientation according to the need for them and the scrolling
57 increment set by SetScrollRate(). As above, scrolling is only enabled in
58 orientations with a non-zero increment. You can influence the minimum size
59 of the scrolled area controlled by a sizer by calling
60 wxWindow::SetVirtualSizeHints(). (Calling SetScrollbars() has analogous
61 effects in wxWidgets 2.4 -- in later versions it may not continue to
62 override the sizer.)
63
64 Note that if maximum size hints are still supported by
65 wxWindow::SetVirtualSizeHints(), use them at your own dire risk. They may
66 or may not have been removed for 2.4, but it really only makes sense to set
67 minimum size hints here. We should probably replace
68 wxWindow::SetVirtualSizeHints() with wxWindow::SetMinVirtualSize() or
69 similar and remove it entirely in future.
70
71 @todo review docs for this class replacing SetVirtualSizeHints() with
72 SetMinClientSize().
73
74 As with all windows, an application can draw onto a wxScrolled using a
75 @ref overview_dc "device context".
76
77 You have the option of handling the OnPaint handler or overriding the
78 wxScrolled::OnDraw() function, which is passed a pre-scrolled device
79 context (prepared by wxScrolled::DoPrepareDC()).
80
81 If you don't wish to calculate your own scrolling, you must call
82 DoPrepareDC() when not drawing from within OnDraw(), to set the device
83 origin for the device context according to the current scroll position.
84
85 A wxScrolled will normally scroll itself and therefore its child windows
86 as well. It might however be desired to scroll a different window than
87 itself: e.g. when designing a spreadsheet, you will normally only have to
88 scroll the (usually white) cell area, whereas the (usually grey) label area
89 will scroll very differently. For this special purpose, you can call
90 SetTargetWindow() which means that pressing the scrollbars will scroll a
91 different window.
92
93 Note that the underlying system knows nothing about scrolling coordinates,
94 so that all system functions (mouse events, expose events, refresh calls
95 etc) as well as the position of subwindows are relative to the "physical"
96 origin of the scrolled window. If the user insert a child window at
97 position (10,10) and scrolls the window down 100 pixels (moving the child
98 window out of the visible area), the child window will report a position
99 of (10,-90).
100
101 @beginStyleTable
102 @style{wxHSCROLL}
103 If this style is specified and ::wxVSCROLL isn't, the window will be
104 scrollable only in horizontal direction (by default, i.e. if neither
105 this style nor ::wxVSCROLL is specified, it scrolls in both
106 directions).
107 @style{wxVSCROLL}
108 If this style is specified and ::wxHSCROLL isn't, the window will be
109 scrollable only in vertical direction (by default, i.e. if neither
110 this style nor ::wxHSCROLL is specified, it scrolls in both
111 directions).
112 @style{wxRETAINED}
113 Uses a backing pixmap to speed refreshes. Motif only.
114 @endStyleTable
115
116
117 @beginEventEmissionTable{wxScrollWinEvent}
118 @event{EVT_SCROLLWIN(func)}
119 Process all scroll events.
120 @event{EVT_SCROLLWIN_TOP(func)}
121 Process @c wxEVT_SCROLLWIN_TOP scroll-to-top events.
122 @event{EVT_SCROLLWIN_BOTTOM(func)}
123 Process @c wxEVT_SCROLLWIN_BOTTOM scroll-to-bottom events.
124 @event{EVT_SCROLLWIN_LINEUP(func)}
125 Process @c wxEVT_SCROLLWIN_LINEUP line up events.
126 @event{EVT_SCROLLWIN_LINEDOWN(func)}
127 Process @c wxEVT_SCROLLWIN_LINEDOWN line down events.
128 @event{EVT_SCROLLWIN_PAGEUP(func)}
129 Process @c wxEVT_SCROLLWIN_PAGEUP page up events.
130 @event{EVT_SCROLLWIN_PAGEDOWN(func)}
131 Process @c wxEVT_SCROLLWIN_PAGEDOWN page down events.
132 @event{EVT_SCROLLWIN_THUMBTRACK(func)}
133 Process @c wxEVT_SCROLLWIN_THUMBTRACK thumbtrack events
134 (frequent events sent as the user drags the thumbtrack).
135 @event{EVT_SCROLLWIN_THUMBRELEASE(func)}
136 Process @c wxEVT_SCROLLWIN_THUMBRELEASE thumb release events.
137 @endEventTable
138
139 @note
140 Don't confuse wxScrollWinEvents generated by this class with
141 wxScrollEvent objects generated by wxScrollBar and wxSlider.
142
143
144 @remarks
145 Use wxScrolled for applications where the user scrolls by a fixed amount,
146 and where a 'page' can be interpreted to be the current visible portion of
147 the window. For more sophisticated applications, use the wxScrolled
148 implementation as a guide to build your own scroll behaviour or use
149 wxVScrolledWindow or its variants.
150
151 @since The wxScrolled template exists since version 2.9.0. In older versions,
152 only ::wxScrolledWindow (equivalent of wxScrolled<wxPanel>) was
153 available.
154
155 @library{wxcore}
156 @category{miscwnd}
157
158 @see wxScrollBar, wxClientDC, wxPaintDC,
159 wxVScrolledWindow, wxHScrolledWindow, wxHVScrolledWindow,
160 */
161 template<class T>
162 class wxScrolled : public T
163 {
164 public:
165 /// Default constructor.
166 wxScrolled();
167
168 /**
169 Constructor.
170
171 @param parent
172 Parent window.
173 @param id
174 Window identifier. The value @c wxID_ANY indicates a default value.
175 @param pos
176 Window position. If a position of ::wxDefaultPosition is specified
177 then a default position is chosen.
178 @param size
179 Window size. If a size of ::wxDefaultSize is specified then the
180 window is sized appropriately.
181 @param style
182 Window style. See wxScrolled.
183 @param name
184 Window name.
185
186 @remarks The window is initially created without visible scrollbars.
187 Call SetScrollbars() to specify how big the virtual window
188 size should be.
189 */
190 wxScrolled(wxWindow* parent, wxWindowID id = -1,
191 const wxPoint& pos = wxDefaultPosition,
192 const wxSize& size = wxDefaultSize,
193 long style = wxHSCROLL | wxVSCROLL,
194 const wxString& name = "scrolledWindow");
195
196 /**
197 Translates the logical coordinates to the device ones. For example, if
198 a window is scrolled 10 pixels to the bottom, the device coordinates of
199 the origin are (0, 0) (as always), but the logical coordinates are (0,
200 10) and so the call to CalcScrolledPosition(0, 10, xx, yy) will return
201 0 in yy.
202
203 @beginWxPerlOnly
204 In wxPerl this method takes two parameters and returns a
205 2-element list (xx, yy).
206 @endWxPerlOnly
207
208 @see CalcUnscrolledPosition()
209 */
210 void CalcScrolledPosition(int x, int y, int* xx, int* yy) const;
211 wxPoint CalcScrolledPosition(const wxPoint& pt) const;
212
213 /**
214 Translates the device coordinates to the logical ones. For example, if
215 a window is scrolled 10 pixels to the bottom, the device coordinates of
216 the origin are (0, 0) (as always), but the logical coordinates are (0,
217 10) and so the call to CalcUnscrolledPosition(0, 0, xx, yy) will return
218 10 in yy.
219
220 @beginWxPerlOnly
221 In wxPerl this method takes two parameters and returns a
222 2-element list (xx, yy).
223 @endWxPerlOnly
224
225 @see CalcScrolledPosition()
226 */
227 void CalcUnscrolledPosition(int x, int y, int* xx, int* yy) const;
228 wxPoint CalcUnscrolledPosition(const wxPoint& pt) const;
229
230 /**
231 Creates the window for two-step construction. Derived classes
232 should call or replace this function. See wxScrolled::wxScrolled()
233 for details.
234 */
235 bool Create(wxWindow* parent, wxWindowID id = -1,
236 const wxPoint& pos = wxDefaultPosition,
237 const wxSize& size = wxDefaultSize,
238 long style = wxHSCROLL | wxVSCROLL,
239 const wxString& name = "scrolledWindow");
240
241 /**
242 Disable use of keyboard keys for scrolling.
243
244 By default cursor movement keys (including Home, End, Page Up and Down)
245 are used to scroll the window appropriately. If the derived class uses
246 these keys for something else, e.g. changing the currently selected
247 item, this function can be used to disable this behaviour as it's not
248 only not necessary then but can actually be actively harmful if another
249 object forwards a keyboard event corresponding to one of the above keys
250 to us using ProcessWindowEvent() because the event will always be
251 processed which can be undesirable.
252
253 @since 2.9.1
254 */
255 void DisableKeyboardScrolling();
256
257 /**
258 Call this function to prepare the device context for drawing a scrolled
259 image.
260
261 It sets the device origin according to the current scroll position.
262 DoPrepareDC() is called automatically within the default @c wxEVT_PAINT
263 event handler, so your OnDraw() override will be passed an already
264 'pre-scrolled' device context. However, if you wish to draw from
265 outside of OnDraw() (e.g. from your own @c wxEVT_PAINT handler), you
266 must call this function yourself.
267
268 For example:
269 @code
270 void MyWindow::OnEvent(wxMouseEvent& event)
271 {
272 wxClientDC dc(this);
273 DoPrepareDC(dc);
274
275 dc.SetPen(*wxBLACK_PEN);
276 float x, y;
277 event.Position(&x, &y);
278 if (xpos > -1 && ypos > -1 && event.Dragging())
279 {
280 dc.DrawLine(xpos, ypos, x, y);
281 }
282 xpos = x;
283 ypos = y;
284 }
285 @endcode
286
287 Notice that the function sets the origin by moving it relatively to the
288 current origin position, so you shouldn't change the origin before
289 calling DoPrepareDC() or, if you do, reset it to (0, 0) later. If you
290 call DoPrepareDC() immediately after device context creation, as in the
291 example above, this problem doesn't arise, of course, so it is
292 customary to do it like this.
293 */
294 void DoPrepareDC(wxDC& dc);
295
296 /**
297 Enable or disable physical scrolling in the given direction. Physical
298 scrolling is the physical transfer of bits up or down the
299 screen when a scroll event occurs. If the application scrolls by a
300 variable amount (e.g. if there are different font sizes) then physical
301 scrolling will not work, and you should switch it off. Note that you
302 will have to reposition child windows yourself, if physical scrolling
303 is disabled.
304
305 @param xScrolling
306 If @true, enables physical scrolling in the x direction.
307 @param yScrolling
308 If @true, enables physical scrolling in the y direction.
309
310 @remarks Physical scrolling may not be available on all platforms. Where
311 it is available, it is enabled by default.
312 */
313 void EnableScrolling(bool xScrolling, bool yScrolling);
314
315 /**
316 Set the scrollbar visibility.
317
318 By default the scrollbar in the corresponding direction is only shown
319 if it is needed, i.e. if the virtual size of the scrolled window in
320 this direction is greater than the current physical window size. Using
321 this function the scrollbar visibility can be changed to be:
322 - wxSHOW_SB_ALWAYS: To always show the scrollbar, even if it is
323 not needed currently (wxALWAYS_SHOW_SB style can be used during
324 the window creation to achieve the same effect but it applies
325 in both directions).
326 - wxSHOW_SB_NEVER: To never show the scrollbar at all. In this case
327 the program should presumably provide some other way for the
328 user to scroll the window.
329 - wxSHOW_SB_DEFAULT: To restore the default behaviour described
330 above.
331
332 @param horz
333 The desired visibility for the horizontal scrollbar.
334 @param vert
335 The desired visibility for the vertical scrollbar.
336
337 @since 2.9.0
338 */
339 void ShowScrollbars(wxScrollbarVisibility horz, wxScrollbarVisibility vert);
340
341 /**
342 Get the number of pixels per scroll unit (line), in each direction, as
343 set by SetScrollbars(). A value of zero indicates no scrolling in that
344 direction.
345
346 @param xUnit
347 Receives the number of pixels per horizontal unit.
348 @param yUnit
349 Receives the number of pixels per vertical unit.
350
351 @beginWxPerlOnly
352 In wxPerl this method takes no parameters and returns a
353 2-element list (xUnit, yUnit).
354 @endWxPerlOnly
355
356 @see SetScrollbars(), GetVirtualSize()
357 */
358 void GetScrollPixelsPerUnit(int* xUnit, int* yUnit) const;
359
360 /**
361 Get the position at which the visible portion of the window starts.
362
363 @param x
364 Receives the first visible x position in scroll units.
365 @param y
366 Receives the first visible y position in scroll units.
367
368 @remarks
369 If either of the scrollbars is not at the home position, @a x
370 and/or @a y will be greater than zero.
371 Combined with wxWindow::GetClientSize(), the application can use this
372 function to efficiently redraw only the visible portion of the window.
373 The positions are in logical scroll units, not pixels, so to convert
374 to pixels you will have to multiply by the number of pixels per scroll
375 increment.
376
377 @beginWxPerlOnly
378 In wxPerl this method takes no parameters and returns a
379 2-element list (x, y).
380 @endWxPerlOnly
381
382 @see SetScrollbars(), Scroll()
383 */
384 void GetViewStart(int* x, int* y) const;
385
386 /**
387 This is a simple overload of GetViewStart(int*,int*); see that function
388 for more info.
389 */
390 wxPoint GetViewStart() const;
391
392 /**
393 Gets the size in device units of the scrollable window area (as
394 opposed to the client size, which is the area of the window currently
395 visible).
396
397 @param x
398 Receives the length of the scrollable window, in pixels.
399 @param y
400 Receives the height of the scrollable window, in pixels.
401
402 @remarks Use wxDC::DeviceToLogicalX() and wxDC::DeviceToLogicalY() to
403 translate these units to logical units.
404
405 @beginWxPerlOnly
406 In wxPerl this method takes no parameters and returns a
407 2-element list (xUnit, yUnit).
408 @endWxPerlOnly
409
410 @see SetScrollbars(), GetScrollPixelsPerUnit()
411 */
412 void GetVirtualSize(int* x, int* y) const;
413
414 /**
415 Motif only: @true if the window has a backing bitmap.
416 */
417 bool IsRetained() const;
418
419 /**
420 Called by the default paint event handler to allow the application to
421 define painting behaviour without having to worry about calling
422 DoPrepareDC().
423
424 Instead of overriding this function you may also just process the paint
425 event in the derived class as usual, but then you will have to call
426 DoPrepareDC() yourself.
427 */
428 virtual void OnDraw(wxDC& dc);
429
430 /**
431 This function is for backwards compatibility only and simply calls
432 DoPrepareDC() now. Notice that it is not called by the default paint
433 event handle (DoPrepareDC() is), so overriding this method in your
434 derived class is useless.
435 */
436 void PrepareDC(wxDC& dc);
437
438 /**
439 Scrolls a window so the view start is at the given point.
440
441 @param x
442 The x position to scroll to, in scroll units.
443 @param y
444 The y position to scroll to, in scroll units.
445
446 @remarks The positions are in scroll units, not pixels, so to convert to
447 pixels you will have to multiply by the number of
448 pixels per scroll increment. If either parameter is
449 ::wxDefaultCoord (-1), that position will be ignored (no change
450 in that direction).
451
452 @see SetScrollbars(), GetScrollPixelsPerUnit()
453 */
454 void Scroll(int x, int y);
455
456 /**
457 This is an overload of Scroll(int,int); see that function for more info.
458 */
459 void Scroll(const wxPoint& pt);
460
461 /**
462 Set the horizontal and vertical scrolling increment only. See the
463 pixelsPerUnit parameter in SetScrollbars().
464 */
465 void SetScrollRate(int xstep, int ystep);
466
467 /**
468 Sets up vertical and/or horizontal scrollbars.
469
470 The first pair of parameters give the number of pixels per 'scroll
471 step', i.e. amount moved when the up or down scroll arrows are pressed.
472 The second pair gives the length of scrollbar in scroll steps, which
473 sets the size of the virtual window.
474
475 @a xPos and @a yPos optionally specify a position to scroll to
476 immediately.
477
478 For example, the following gives a window horizontal and vertical
479 scrollbars with 20 pixels per scroll step, and a size of 50 steps (1000
480 pixels) in each direction:
481 @code
482 window->SetScrollbars(20, 20, 50, 50);
483 @endcode
484
485 wxScrolled manages the page size itself, using the current client
486 window size as the page size.
487
488 Note that for more sophisticated scrolling applications, for example
489 where scroll steps may be variable according to the position in the
490 document, it will be necessary to derive a new class from wxWindow,
491 overriding OnSize() and adjusting the scrollbars appropriately.
492
493 @param pixelsPerUnitX
494 Pixels per scroll unit in the horizontal direction.
495 @param pixelsPerUnitY
496 Pixels per scroll unit in the vertical direction.
497 @param noUnitsX
498 Number of units in the horizontal direction.
499 @param noUnitsY
500 Number of units in the vertical direction.
501 @param xPos
502 Position to initialize the scrollbars in the horizontal direction,
503 in scroll units.
504 @param yPos
505 Position to initialize the scrollbars in the vertical direction, in
506 scroll units.
507 @param noRefresh
508 Will not refresh window if @true.
509
510 @see wxWindow::SetVirtualSize()
511 */
512 void SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY,
513 int noUnitsX,
514 int noUnitsY,
515 int xPos = 0,
516 int yPos = 0,
517 bool noRefresh = false);
518
519 /**
520 Call this function to tell wxScrolled to perform the actual scrolling
521 on a different window (and not on itself).
522
523 This method is useful when only a part of the window should be
524 scrolled. A typical example is a control consisting of a fixed header
525 and the scrollable contents window: the scrollbars are attached to the
526 main window itself, hence it, and not the contents window must be
527 derived from wxScrolled, but only the contents window scrolls when the
528 scrollbars are used. To implement such setup, you need to call this
529 method with the contents window as argument.
530
531 Notice that if this method is used, GetSizeAvailableForScrollTarget()
532 method must be overridden.
533 */
534 void SetTargetWindow(wxWindow *window);
535 wxWindow *GetTargetWindow() const;
536
537
538 void SetTargetRect(const wxRect& rect);
539 wxRect GetTargetRect() const;
540
541 int GetScrollPageSize(int orient) const;
542 void SetScrollPageSize(int orient, int pageSize);
543 int GetScrollLines( int orient ) const;
544 void SetScale(double xs, double ys);
545 double GetScaleX() const;
546 double GetScaleY() const;
547
548 virtual void AdjustScrollbars();
549
550 /**
551 Are we generating the autoscroll events?
552 */
553 bool IsAutoScrolling() const;
554
555 /**
556 Stop generating the scroll events when mouse is held outside the
557 window.
558 */
559 void StopAutoScrolling();
560
561 /**
562 This method can be overridden in a derived class to forbid sending the
563 auto scroll events - note that unlike StopAutoScrolling() it doesn't
564 stop the timer, so it will be called repeatedly and will typically
565 return different values depending on the current mouse position
566
567 The base class version just returns true.
568 */
569 virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const;
570
571
572 protected:
573 /**
574 Function which must be overridden to implement the size available for
575 the scroll target for the given size of the main window.
576
577 This method must be overridden if SetTargetWindow() is used (it is
578 never called otherwise). The implementation should decrease the @a size
579 to account for the size of the non-scrollable parts of the main window
580 and return only the size available for the scrollable window itself.
581 E.g. in the example given in SetTargetWindow() documentation the
582 function would subtract the height of the header window from the
583 vertical component of @a size.
584 */
585 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
586 };
587
588
589 /**
590 Scrolled window derived from wxPanel.
591
592 See wxScrolled for a detailed description.
593
594 @note Note that because this class derives from wxPanel, it shares its
595 behaviour with regard to TAB traversal and focus handling (in
596 particular, it forwards focus to its children). If you don't want
597 this behaviour, use ::wxScrolledCanvas instead.
598
599 @note ::wxScrolledWindow is an alias for wxScrolled<wxPanel> since version
600 2.9.0. In older versions, it was a standalone class.
601
602 @library{wxcore}
603 @category{miscwnd}
604
605 @see wxScrolled, ::wxScrolledCanvas
606 */
607 typedef wxScrolled<wxPanel> wxScrolledWindow;
608
609 /**
610 Alias for wxScrolled<wxWindow>. Scrolled window that doesn't have children
611 and so doesn't need or want special handling of TAB traversal.
612
613 @since 2.9.0
614
615 @library{wxcore}
616 @category{miscwnd}
617
618 @see wxScrolled, ::wxScrolledWindow
619 */
620 typedef wxScrolled<wxWindow> wxScrolledCanvas;