]>
git.saurik.com Git - wxWidgets.git/blob - interface/scrolwin.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxScrolledWindow
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 The wxScrolled<T> class manages scrolling for its client area, transforming
13 the coordinates according to the scrollbar positions, and setting the
14 scroll positions, thumb sizes and ranges according to the area in view.
16 There are two commonly used (but not the only possible!) specializations of
19 - ::wxScrolledWindow, aka wxScrolled<wxPanel>, is equivalent to
20 ::wxScrolledWindow from earlier versions. Derived from wxPanel, it shares
21 wxPanel's behaviour with regard to TAB traversal and focus handling. Use
22 this if the scrolled window will have children controls.
24 - ::wxScrolledCanvas, aka wxScrolled<wxWindow>, derives from wxWindow and
25 so doesn't handle children specially. This is suitable e.g. for
26 implementating scrollable controls such as tree or list controls.
28 Starting from version 2.4 of wxWidgets, there are several ways to use a
29 wxScrolled<T>. In particular, there are now three ways to set the
30 size of the scrolling area:
32 One way is to set the scrollbars directly using a call to SetScrollbars().
33 This is the way it used to be in any previous version of wxWidgets and it
34 will be kept for backwards compatibility.
36 An additional method of manual control, which requires a little less
37 computation of your own, is to set the total size of the scrolling area by
38 calling either wxWindow::SetVirtualSize(), or wxWindow::FitInside(), and
39 setting the scrolling increments for it by calling SetScrollRate().
40 Scrolling in some orientation is enabled by setting a non-zero increment
43 The most automatic and newest way is to simply let sizers determine the
44 scrolling area. This is now the default when you set an interior sizer into
45 a wxScrolled<T> with wxWindow::SetSizer(). The scrolling area will be
46 set to the size requested by the sizer and the scrollbars will be assigned
47 for each orientation according to the need for them and the scrolling
48 increment set by SetScrollRate(). As above, scrolling is only enabled in
49 orientations with a non-zero increment. You can influence the minimum size
50 of the scrolled area controlled by a sizer by calling
51 wxWindow::SetVirtualSizeHints(). (Calling SetScrollbars() has analogous
52 effects in wxWidgets 2.4 -- in later versions it may not continue to
55 Note that if maximum size hints are still supported by
56 wxWindow::SetVirtualSizeHints(), use them at your own dire risk. They may
57 or may not have been removed for 2.4, but it really only makes sense to set
58 minimum size hints here. We should probably replace
59 wxWindow::SetVirtualSizeHints() with wxWindow::SetMinVirtualSize() or
60 similar and remove it entirely in future.
62 As with all windows, an application can draw onto a wxScrolled<T> using
63 a @ref overview_dcoverview "device context".
65 You have the option of handling the OnPaint handler or overriding the
66 wxScrolled<T>::OnDraw() function, which is passed a pre-scrolled device
67 context (prepared by wxScrolled<T>::DoPrepareDC()).
69 If you don't wish to calculate your own scrolling, you must call
70 DoPrepareDC() when not drawing from within OnDraw(), to set the device
71 origin for the device context according to the current scroll position.
73 A wxScrolled<T> will normally scroll itself and therefore its child windows
74 as well. It might however be desired to scroll a different window than
75 itself: e.g. when designing a spreadsheet, you will normally only have to
76 scroll the (usually white) cell area, whereas the (usually grey) label area
77 will scroll very differently. For this special purpose, you can call
78 SetTargetWindow() which means that pressing the scrollbars will scroll a
81 Note that the underlying system knows nothing about scrolling coordinates,
82 so that all system functions (mouse events, expose events, refresh calls
83 etc) as well as the position of subwindows are relative to the "physical"
84 origin of the scrolled window. If the user insert a child window at
85 position (10,10) and scrolls the window down 100 pixels (moving the child
86 window out of the visible area), the child window will report a position
91 Uses a backing pixmap to speed refreshes. Motif only.
95 Use wxScrolled<T> for applications where the user scrolls by a fixed
96 amount, and where a 'page' can be interpreted to be the current visible
97 portion of the window. For more sophisticated applications, use the
98 wxScrolled<T> implementation as a guide to build your own scroll
99 behaviour or use wxVScrolledWindow or its variants.
101 @since wxScrolled<T> template exists since version 2.9.0. In older
102 versions, only ::wxScrolledWindow (equivalent of wxScrolled<wxPanel>)
108 @see wxScrollBar, wxClientDC, wxPaintDC,
109 wxVScrolledWindow, wxHScrolledWindow, wxHVScrolledWindow,
112 class wxScrolled
: public T
115 /// Default constructor.
124 Window identifier. The value @c wxID_ANY indicates a default value.
126 Window position. If a position of @c wxDefaultPosition is specified
127 then a default position is chosen.
129 Window size. If a size of @c wxDefaultSize is specified then the
130 window is sized appropriately.
132 Window style. See wxScrolled<T>.
136 @remarks The window is initially created without visible scrollbars.
137 Call SetScrollbars() to specify how big the virtual window
140 wxScrolled(wxWindow
* parent
, wxWindowID id
= -1,
141 const wxPoint
& pos
= wxDefaultPosition
,
142 const wxSize
& size
= wxDefaultSize
,
143 long style
= wxHSCROLL
| wxVSCROLL
,
144 const wxString
& name
= "scrolledWindow");
148 Translates the logical coordinates to the device ones. For example, if
149 a window is scrolled 10 pixels to the bottom, the device coordinates of
150 the origin are (0, 0) (as always), but the logical coordinates are (0,
151 10) and so the call to CalcScrolledPosition(0, 10, xx, yy) will return
154 @see CalcUnscrolledPosition()
156 void CalcScrolledPosition(int x
, int y
, int* xx
, int* yy
) const;
159 Translates the device coordinates to the logical ones. For example, if
160 a window is scrolled 10 pixels to the bottom, the device coordinates of
161 the origin are (0, 0) (as always), but the logical coordinates are (0,
162 10) and so the call to CalcUnscrolledPosition(0, 0, xx, yy) will return
165 @see CalcScrolledPosition()
167 void CalcUnscrolledPosition(int x
, int y
, int* xx
, int* yy
) const;
170 Creates the window for two-step construction. Derived classes
171 should call or replace this function. See wxScrolled<T> constructor
174 bool Create(wxWindow
* parent
, wxWindowID id
= -1,
175 const wxPoint
& pos
= wxDefaultPosition
,
176 const wxSize
& size
= wxDefaultSize
,
177 long style
= wxHSCROLL
| wxVSCROLL
,
178 const wxString
& name
= "scrolledWindow");
181 Call this function to prepare the device context for drawing a scrolled
184 It sets the device origin according to the current scroll position.
185 DoPrepareDC() is called automatically within the default OnPaint()
186 event handler, so your OnDraw() override will be passed a
187 'pre-scrolled' device context. However, if you wish to draw from
188 outside of OnDraw() (via OnPaint()), or you wish to implement OnPaint()
189 yourself, you must call this function yourself.
193 void MyWindow::OnEvent(wxMouseEvent& event)
198 dc.SetPen(*wxBLACK_PEN);
200 event.Position(&x, &y);
201 if (xpos > -1 && ypos > -1 && event.Dragging())
203 dc.DrawLine(xpos, ypos, x, y);
211 void DoPrepareDC(wxDC
& dc
);
214 Enable or disable physical scrolling in the given direction. Physical
215 scrolling is the physical transfer of bits up or down the
216 screen when a scroll event occurs. If the application scrolls by a
217 variable amount (e.g. if there are different font sizes) then physical
218 scrolling will not work, and you should switch it off. Note that you
219 will have to reposition child windows yourself, if physical scrolling
223 If @true, enables physical scrolling in the x direction.
225 If @true, enables physical scrolling in the y direction.
227 @remarks Physical scrolling may not be available on all platforms. Where
228 it is available, it is enabled by default.
230 void EnableScrolling(bool xScrolling
, bool yScrolling
);
233 Get the number of pixels per scroll unit (line), in each direction, as
234 set by SetScrollbars(). A value of zero indicates no scrolling in that
238 Receives the number of pixels per horizontal unit.
240 Receives the number of pixels per vertical unit.
242 @see SetScrollbars(), GetVirtualSize()
244 void GetScrollPixelsPerUnit(int* xUnit
, int* yUnit
) const;
247 Get the position at which the visible portion of the window starts.
250 Receives the first visible x position in scroll units.
252 Receives the first visible y position in scroll units.
254 @remarks If either of the scrollbars is not at the home position, x
255 and/or y will be greater than zero. Combined with
256 wxWindow::GetClientSize(), the application can use this
257 function to efficiently redraw only the visible portion
258 of the window. The positions are in logical scroll
259 units, not pixels, so to convert to pixels you will
260 have to multiply by the number of pixels per scroll
265 void GetViewStart(int* x
, int* y
) const;
268 Gets the size in device units of the scrollable window area (as
269 opposed to the client size, which is the area of the window currently
273 Receives the length of the scrollable window, in pixels.
275 Receives the height of the scrollable window, in pixels.
277 @remarks Use wxDC::DeviceToLogicalX() and wxDC::DeviceToLogicalY() to
278 translate these units to logical units.
280 @see SetScrollbars(), GetScrollPixelsPerUnit()
282 void GetVirtualSize(int* x
, int* y
) const;
285 Motif only: @true if the window has a backing bitmap.
287 bool IsRetained() const;
290 Called by the default paint event handler to allow the application to
291 define painting behaviour without having to worry about calling
294 Instead of overriding this function you may also just process the paint
295 event in the derived class as usual, but then you will have to call
296 DoPrepareDC() yourself.
298 virtual void OnDraw(wxDC
& dc
);
301 This function is for backwards compatibility only and simply calls
302 DoPrepareDC() now. Notice that it is not called by the default paint
303 event handle (DoPrepareDC() is), so overriding this method in your
304 derived class is useless.
306 void PrepareDC(wxDC
& dc
);
309 Scrolls a window so the view start is at the given point.
312 The x position to scroll to, in scroll units.
314 The y position to scroll to, in scroll units.
316 @remarks The positions are in scroll units, not pixels, so to convert to
317 pixels you will have to multiply by the number of
318 pixels per scroll increment. If either parameter is -1,
319 that position will be ignored (no change in that
322 @see SetScrollbars(), GetScrollPixelsPerUnit()
324 void Scroll(int x
, int y
);
327 Set the horizontal and vertical scrolling increment only. See the
328 pixelsPerUnit parameter in SetScrollbars().
330 void SetScrollRate(int xstep
, int ystep
);
333 Sets up vertical and/or horizontal scrollbars.
335 The first pair of parameters give the number of pixels per 'scroll
336 step', i.e. amount moved when the up or down scroll arrows are pressed.
337 The second pair gives the length of scrollbar in scroll steps, which
338 sets the size of the virtual window.
340 @a xPos and @a yPos optionally specify a position to scroll to
343 For example, the following gives a window horizontal and vertical
344 scrollbars with 20 pixels per scroll step, and a size of 50 steps (1000
345 pixels) in each direction:
347 window->SetScrollbars(20, 20, 50, 50);
350 wxScrolled<T> manages the page size itself, using the current client
351 window size as the page size.
353 Note that for more sophisticated scrolling applications, for example
354 where scroll steps may be variable according to the position in the
355 document, it will be necessary to derive a new class from wxWindow,
356 overriding OnSize() and adjusting the scrollbars appropriately.
358 @param pixelsPerUnitX
359 Pixels per scroll unit in the horizontal direction.
360 @param pixelsPerUnitY
361 Pixels per scroll unit in the vertical direction.
363 Number of units in the horizontal direction.
365 Number of units in the vertical direction.
367 Position to initialize the scrollbars in the horizontal direction,
370 Position to initialize the scrollbars in the vertical direction, in
373 Will not refresh window if @true.
375 @see wxWindow::SetVirtualSize()
377 void SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
382 bool noRefresh
= false);
385 Call this function to tell wxScrolled to perform the actual
386 scrolling on a different window (and not on itself).
388 void SetTargetWindow(wxWindow
* window
);
393 Scrolled window derived from wxPanel.
395 See wxScrolled<T> for detailed description.
397 @note Note that because this class derives from wxPanel, it shares its
398 behavior with regard to TAB traversal and focus handling (in
399 particular, it forwards focus to its children). If you don't want
400 this behaviour, use ::wxScrolledCanvas instead.
402 @note wxScrolledWindow is an alias for wxScrolled<wxPanel> since version
403 2.9.0. In older versions, it was a standalone class.
408 @see wxScrolled, wxScrolledCanvas
410 typedef wxScrolled
<wxPanel
> wxScrolledWindow
;
413 Alias for wxScrolled<wxWindow>. Scrolled window that doesn't have children
414 and so doesn't need or want special handling of TAB traversal.
421 @see wxScrolled, ::wxScrolledWindow
423 typedef wxScrolled
<wxWindow
> wxScrolledCanvas
;