]> git.saurik.com Git - wxWidgets.git/blob - interface/scrolwin.h
Fixed wxScrolledWindow references to properly link (and appropriately changed others...
[wxWidgets.git] / interface / 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 @wxheader{scrolwin.h}
11
12 The wxScrolled 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.
15
16 There are two commonly used (but not the only possible!) specializations of
17 this class:
18
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.
23
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.
27
28 Starting from version 2.4 of wxWidgets, there are several ways to use a
29 wxScrolled. In particular, there are now three ways to set the size of the
30 scrolling area:
31
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.
35
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
41 for it.
42
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 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
53 override the sizer.)
54
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.
61
62 As with all windows, an application can draw onto a wxScrolled using a
63 @ref overview_dc "device context".
64
65 You have the option of handling the OnPaint handler or overriding the
66 wxScrolled::OnDraw() function, which is passed a pre-scrolled device
67 context (prepared by wxScrolled::DoPrepareDC()).
68
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.
72
73 A wxScrolled 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
79 different window.
80
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
87 of (10,-90).
88
89 @beginStyleTable
90 @style{wxRETAINED}
91 Uses a backing pixmap to speed refreshes. Motif only.
92 @endStyleTable
93
94 @remarks
95 Use wxScrolled for applications where the user scrolls by a fixed amount,
96 and where a 'page' can be interpreted to be the current visible portion of
97 the window. For more sophisticated applications, use the wxScrolled
98 implementation as a guide to build your own scroll behaviour or use
99 wxVScrolledWindow or its variants.
100
101 @since wxScrolled template exists since version 2.9.0. In older versions,
102 only ::wxScrolledWindow (equivalent of wxScrolled<wxPanel>) was
103 available.
104
105 @library{wxcore}
106 @category{miscwnd}
107
108 @see wxScrollBar, wxClientDC, wxPaintDC,
109 wxVScrolledWindow, wxHScrolledWindow, wxHVScrolledWindow,
110 */
111 template<class T>
112 class wxScrolled : public T
113 {
114 public:
115 /// Default constructor.
116 wxScrolled();
117
118 /**
119 Constructor.
120
121 @param parent
122 Parent window.
123 @param id
124 Window identifier. The value @c wxID_ANY indicates a default value.
125 @param pos
126 Window position. If a position of @c wxDefaultPosition is specified
127 then a default position is chosen.
128 @param size
129 Window size. If a size of @c wxDefaultSize is specified then the
130 window is sized appropriately.
131 @param style
132 Window style. See wxScrolled.
133 @param name
134 Window name.
135
136 @remarks The window is initially created without visible scrollbars.
137 Call SetScrollbars() to specify how big the virtual window
138 size should be.
139 */
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");
145
146
147 /**
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
152 0 in yy.
153
154 @see CalcUnscrolledPosition()
155 */
156 void CalcScrolledPosition(int x, int y, int* xx, int* yy) const;
157
158 /**
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
163 10 in yy.
164
165 @see CalcScrolledPosition()
166 */
167 void CalcUnscrolledPosition(int x, int y, int* xx, int* yy) const;
168
169 /**
170 Creates the window for two-step construction. Derived classes
171 should call or replace this function. See wxScrolled::wxScrolled()
172 for details.
173 */
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");
179
180 /**
181 Call this function to prepare the device context for drawing a scrolled
182 image.
183
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.
190
191 For example:
192 @code
193 void MyWindow::OnEvent(wxMouseEvent& event)
194 {
195 wxClientDC dc(this);
196 DoPrepareDC(dc);
197
198 dc.SetPen(*wxBLACK_PEN);
199 float x, y;
200 event.Position(&x, &y);
201 if (xpos > -1 && ypos > -1 && event.Dragging())
202 {
203 dc.DrawLine(xpos, ypos, x, y);
204 }
205 xpos = x;
206 ypos = y;
207 }
208 @endcode
209
210 */
211 void DoPrepareDC(wxDC& dc);
212
213 /**
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
220 is disabled.
221
222 @param xScrolling
223 If @true, enables physical scrolling in the x direction.
224 @param yScrolling
225 If @true, enables physical scrolling in the y direction.
226
227 @remarks Physical scrolling may not be available on all platforms. Where
228 it is available, it is enabled by default.
229 */
230 void EnableScrolling(bool xScrolling, bool yScrolling);
231
232 /**
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
235 direction.
236
237 @param xUnit
238 Receives the number of pixels per horizontal unit.
239 @param yUnit
240 Receives the number of pixels per vertical unit.
241
242 @see SetScrollbars(), GetVirtualSize()
243 */
244 void GetScrollPixelsPerUnit(int* xUnit, int* yUnit) const;
245
246 /**
247 Get the position at which the visible portion of the window starts.
248
249 @param x
250 Receives the first visible x position in scroll units.
251 @param y
252 Receives the first visible y position in scroll units.
253
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
261 increment.
262
263 @see SetScrollbars()
264 */
265 void GetViewStart(int* x, int* y) const;
266
267 /**
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
270 visible).
271
272 @param x
273 Receives the length of the scrollable window, in pixels.
274 @param y
275 Receives the height of the scrollable window, in pixels.
276
277 @remarks Use wxDC::DeviceToLogicalX() and wxDC::DeviceToLogicalY() to
278 translate these units to logical units.
279
280 @see SetScrollbars(), GetScrollPixelsPerUnit()
281 */
282 void GetVirtualSize(int* x, int* y) const;
283
284 /**
285 Motif only: @true if the window has a backing bitmap.
286 */
287 bool IsRetained() const;
288
289 /**
290 Called by the default paint event handler to allow the application to
291 define painting behaviour without having to worry about calling
292 DoPrepareDC().
293
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.
297 */
298 virtual void OnDraw(wxDC& dc);
299
300 /**
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.
305 */
306 void PrepareDC(wxDC& dc);
307
308 /**
309 Scrolls a window so the view start is at the given point.
310
311 @param x
312 The x position to scroll to, in scroll units.
313 @param y
314 The y position to scroll to, in scroll units.
315
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
320 direction).
321
322 @see SetScrollbars(), GetScrollPixelsPerUnit()
323 */
324 void Scroll(int x, int y);
325
326 /**
327 Set the horizontal and vertical scrolling increment only. See the
328 pixelsPerUnit parameter in SetScrollbars().
329 */
330 void SetScrollRate(int xstep, int ystep);
331
332 /**
333 Sets up vertical and/or horizontal scrollbars.
334
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.
339
340 @a xPos and @a yPos optionally specify a position to scroll to
341 immediately.
342
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:
346 @code
347 window->SetScrollbars(20, 20, 50, 50);
348 @endcode
349
350 wxScrolled manages the page size itself, using the current client
351 window size as the page size.
352
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.
357
358 @param pixelsPerUnitX
359 Pixels per scroll unit in the horizontal direction.
360 @param pixelsPerUnitY
361 Pixels per scroll unit in the vertical direction.
362 @param noUnitsX
363 Number of units in the horizontal direction.
364 @param noUnitsY
365 Number of units in the vertical direction.
366 @param xPos
367 Position to initialize the scrollbars in the horizontal direction,
368 in scroll units.
369 @param yPos
370 Position to initialize the scrollbars in the vertical direction, in
371 scroll units.
372 @param noRefresh
373 Will not refresh window if @true.
374
375 @see wxWindow::SetVirtualSize()
376 */
377 void SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY,
378 int noUnitsX,
379 int noUnitsY,
380 int xPos = 0,
381 int yPos = 0,
382 bool noRefresh = false);
383
384 /**
385 Call this function to tell wxScrolled to perform the actual
386 scrolling on a different window (and not on itself).
387 */
388 void SetTargetWindow(wxWindow* window);
389 };
390
391
392 /**
393 Scrolled window derived from wxPanel.
394
395 See wxScrolled for detailed description.
396
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.
401
402 @note ::wxScrolledWindow is an alias for wxScrolled<wxPanel> since version
403 2.9.0. In older versions, it was a standalone class.
404
405 @library{wxcore}
406 @category{miscwnd}
407
408 @see wxScrolled, ::wxScrolledCanvas
409 */
410 typedef wxScrolled<wxPanel> wxScrolledWindow;
411
412 /**
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.
415
416 @since 2.9.0
417
418 @library{wxcore}
419 @category{miscwnd}
420
421 @see wxScrolled, ::wxScrolledWindow
422 */
423 typedef wxScrolled<wxWindow> wxScrolledCanvas;