]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/scrolwin.h
b3664bee2ef805e7cbd96d48507486aaaeb8f90d
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxScrolled template
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 The wxScrolled class manages scrolling for its client area, transforming
12 the coordinates according to the scrollbar positions, and setting the
13 scroll positions, thumb sizes and ranges according to the area in view.
15 There are two commonly used (but not the only possible!) specializations of
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
21 this if the scrolled window will have child controls.
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.
27 Starting from version 2.4 of wxWidgets, there are several ways to use a
28 ::wxScrolledWindow (and now wxScrolled). In particular, there are
29 three ways to set the size of the scrolling area:
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.
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
37 calling either wxWindow::SetVirtualSize(), or wxWindow::FitInside(), and
38 setting the scrolling increments for it by calling SetScrollRate().
39 Scrolling in some orientation is enabled by setting a non-zero increment
42 The most automatic and newest way is to simply let sizers determine the
43 scrolling area. This is now the default when you set an interior sizer into
44 a wxScrolled with wxWindow::SetSizer(). The scrolling area will be
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
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.
61 @todo review docs for this class replacing SetVirtualSizeHints() with
64 As with all windows, an application can draw onto a wxScrolled using a
65 @ref overview_dc "device context".
67 You have the option of handling the OnPaint handler or overriding the
68 wxScrolled::OnDraw() function, which is passed a pre-scrolled device
69 context (prepared by wxScrolled::DoPrepareDC()).
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.
75 A wxScrolled will normally scroll itself and therefore its child windows
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
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
87 position (10,10) and scrolls the window down 100 pixels (moving the child
88 window out of the visible area), the child window will report a position
93 Uses a backing pixmap to speed refreshes. Motif only.
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.
103 @since The wxScrolled template exists since version 2.9.0. In older versions,
104 only ::wxScrolledWindow (equivalent of wxScrolled<wxPanel>) was
110 @see wxScrollBar, wxClientDC, wxPaintDC,
111 wxVScrolledWindow, wxHScrolledWindow, wxHVScrolledWindow,
114 class wxScrolled
: public T
117 /// Default constructor.
126 Window identifier. The value @c wxID_ANY indicates a default value.
128 Window position. If a position of @c wxDefaultPosition is specified
129 then a default position is chosen.
131 Window size. If a size of @c wxDefaultSize is specified then the
132 window is sized appropriately.
134 Window style. See wxScrolled.
138 @remarks The window is initially created without visible scrollbars.
139 Call SetScrollbars() to specify how big the virtual window
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");
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
156 @see CalcUnscrolledPosition()
158 void CalcScrolledPosition(int x
, int y
, int* xx
, int* yy
) const;
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
167 @see CalcScrolledPosition()
169 void CalcUnscrolledPosition(int x
, int y
, int* xx
, int* yy
) const;
172 Creates the window for two-step construction. Derived classes
173 should call or replace this function. See wxScrolled::wxScrolled()
176 bool Create(wxWindow
* parent
, wxWindowID id
= -1,
177 const wxPoint
& pos
= wxDefaultPosition
,
178 const wxSize
& size
= wxDefaultSize
,
179 long style
= wxHSCROLL
| wxVSCROLL
,
180 const wxString
& name
= "scrolledWindow");
183 Call this function to prepare the device context for drawing a scrolled
186 It sets the device origin according to the current scroll position.
187 DoPrepareDC() is called automatically within the default @c wxEVT_PAINT
188 event handler, so your OnDraw() override will be passed an already
189 'pre-scrolled' device context. However, if you wish to draw from
190 outside of OnDraw() (e.g. from your own @c wxEVT_PAINT handler), you
191 must call this function yourself.
195 void MyWindow::OnEvent(wxMouseEvent& event)
200 dc.SetPen(*wxBLACK_PEN);
202 event.Position(&x, &y);
203 if (xpos > -1 && ypos > -1 && event.Dragging())
205 dc.DrawLine(xpos, ypos, x, y);
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.
219 void DoPrepareDC(wxDC
& dc
);
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
231 If @true, enables physical scrolling in the x direction.
233 If @true, enables physical scrolling in the y direction.
235 @remarks Physical scrolling may not be available on all platforms. Where
236 it is available, it is enabled by default.
238 void EnableScrolling(bool xScrolling
, bool yScrolling
);
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
246 Receives the number of pixels per horizontal unit.
248 Receives the number of pixels per vertical unit.
250 @see SetScrollbars(), GetVirtualSize()
252 void GetScrollPixelsPerUnit(int* xUnit
, int* yUnit
) const;
256 Get the position at which the visible portion of the window starts.
259 Receives the first visible x position in scroll units.
261 Receives the first visible y position in scroll units.
263 @remarks If either of the scrollbars is not at the home position, x
264 and/or y will be greater than zero. Combined with
265 wxWindow::GetClientSize(), the application can use this
266 function to efficiently redraw only the visible portion
267 of the window. The positions are in logical scroll
268 units, not pixels, so to convert to pixels you will
269 have to multiply by the number of pixels per scroll
272 @see SetScrollbars(), Scroll()
274 void GetViewStart(int* x
, int* y
) const;
275 wxPoint
GetViewStart() const;
279 Gets the size in device units of the scrollable window area (as
280 opposed to the client size, which is the area of the window currently
284 Receives the length of the scrollable window, in pixels.
286 Receives the height of the scrollable window, in pixels.
288 @remarks Use wxDC::DeviceToLogicalX() and wxDC::DeviceToLogicalY() to
289 translate these units to logical units.
291 @see SetScrollbars(), GetScrollPixelsPerUnit()
293 void GetVirtualSize(int* x
, int* y
) const;
296 Motif only: @true if the window has a backing bitmap.
298 bool IsRetained() const;
301 Called by the default paint event handler to allow the application to
302 define painting behaviour without having to worry about calling
305 Instead of overriding this function you may also just process the paint
306 event in the derived class as usual, but then you will have to call
307 DoPrepareDC() yourself.
309 virtual void OnDraw(wxDC
& dc
);
312 This function is for backwards compatibility only and simply calls
313 DoPrepareDC() now. Notice that it is not called by the default paint
314 event handle (DoPrepareDC() is), so overriding this method in your
315 derived class is useless.
317 void PrepareDC(wxDC
& dc
);
321 Scrolls a window so the view start is at the given point.
324 The x position to scroll to, in scroll units.
326 The y position to scroll to, in scroll units.
328 @remarks The positions are in scroll units, not pixels, so to convert to
329 pixels you will have to multiply by the number of
330 pixels per scroll increment. If either parameter is
331 wxDefaultCoord (-1), that position will be ignored (no change
334 @see SetScrollbars(), GetScrollPixelsPerUnit()
336 void Scroll(int x
, int y
);
337 void Scroll(const wxPoint
& pt
);
341 Set the horizontal and vertical scrolling increment only. See the
342 pixelsPerUnit parameter in SetScrollbars().
344 void SetScrollRate(int xstep
, int ystep
);
347 Sets up vertical and/or horizontal scrollbars.
349 The first pair of parameters give the number of pixels per 'scroll
350 step', i.e. amount moved when the up or down scroll arrows are pressed.
351 The second pair gives the length of scrollbar in scroll steps, which
352 sets the size of the virtual window.
354 @a xPos and @a yPos optionally specify a position to scroll to
357 For example, the following gives a window horizontal and vertical
358 scrollbars with 20 pixels per scroll step, and a size of 50 steps (1000
359 pixels) in each direction:
361 window->SetScrollbars(20, 20, 50, 50);
364 wxScrolled manages the page size itself, using the current client
365 window size as the page size.
367 Note that for more sophisticated scrolling applications, for example
368 where scroll steps may be variable according to the position in the
369 document, it will be necessary to derive a new class from wxWindow,
370 overriding OnSize() and adjusting the scrollbars appropriately.
372 @param pixelsPerUnitX
373 Pixels per scroll unit in the horizontal direction.
374 @param pixelsPerUnitY
375 Pixels per scroll unit in the vertical direction.
377 Number of units in the horizontal direction.
379 Number of units in the vertical direction.
381 Position to initialize the scrollbars in the horizontal direction,
384 Position to initialize the scrollbars in the vertical direction, in
387 Will not refresh window if @true.
389 @see wxWindow::SetVirtualSize()
391 void SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
396 bool noRefresh
= false);
399 Call this function to tell wxScrolled to perform the actual scrolling
400 on a different window (and not on itself).
402 This method is useful when only a part of the window should be
403 scrolled. A typical example is a control consisting of a fixed header
404 and the scrollable contents window: the scrollbars are attached to the
405 main window itself, hence it, and not the contents window must be
406 derived from wxScrolled, but only the contents window scrolls when the
407 scrollbars are used. To implement such setup, you need to call this
408 method with the contents window as argument.
410 Notice that if this method is used, GetSizeAvailableForScrollTarget()
411 method must be overridden.
413 void SetTargetWindow(wxWindow
*window
);
417 Function which must be overridden to implement the size available for
418 the scroll target for the given size of the main window.
420 This method must be overridden if SetTargetWindow() is used (it is
421 never called otherwise). The implementation should decrease the @a size
422 to account for the size of the non-scrollable parts of the main window
423 and return only the size available for the scrollable window itself.
424 E.g. in the example given in SetTargetWindow() documentation the
425 function would subtract the height of the header window from the
426 vertical component of @a size.
428 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
);
433 Scrolled window derived from wxPanel.
435 See wxScrolled for detailed description.
437 @note Note that because this class derives from wxPanel, it shares its
438 behavior with regard to TAB traversal and focus handling (in
439 particular, it forwards focus to its children). If you don't want
440 this behaviour, use ::wxScrolledCanvas instead.
442 @note ::wxScrolledWindow is an alias for wxScrolled<wxPanel> since version
443 2.9.0. In older versions, it was a standalone class.
448 @see wxScrolled, ::wxScrolledCanvas
450 typedef wxScrolled
<wxPanel
> wxScrolledWindow
;
453 Alias for wxScrolled<wxWindow>. Scrolled window that doesn't have children
454 and so doesn't need or want special handling of TAB traversal.
461 @see wxScrolled, ::wxScrolledWindow
463 typedef wxScrolled
<wxWindow
> wxScrolledCanvas
;