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