Version: 3.0.2
wxScrolled< T > Class Template Reference

#include <wx/scrolwin.h>

+ Inheritance diagram for wxScrolled< T >:

Detailed Description

template<class T>
class wxScrolled< T >

The wxScrolled class manages scrolling for its client area, transforming the coordinates according to the scrollbar positions, and setting the scroll positions, thumb sizes and ranges according to the area in view.

There are two commonly used (but not the only possible!) specializations of this class:

  • wxScrolledWindow, aka wxScrolled<wxPanel>, is equivalent to wxScrolledWindow from earlier versions. Derived from wxPanel, it shares wxPanel's behaviour with regard to TAB traversal and focus handling. Use this if the scrolled window will have child controls.
  • wxScrolledCanvas, aka wxScrolled<wxWindow>, derives from wxWindow and so doesn't handle children specially. This is suitable e.g. for implementing scrollable controls such as tree or list controls.

Starting from version 2.4 of wxWidgets, there are several ways to use a wxScrolledWindow (and now wxScrolled). In particular, there are three ways to set the size of the scrolling area:

One way is to set the scrollbars directly using a call to SetScrollbars(). This is the way it used to be in any previous version of wxWidgets and it will be kept for backwards compatibility.

An additional method of manual control, which requires a little less computation of your own, is to set the total size of the scrolling area by calling either wxWindow::SetVirtualSize(), or wxWindow::FitInside(), and setting the scrolling increments for it by calling SetScrollRate(). Scrolling in some orientation is enabled by setting a non-zero increment for it.

The most automatic and newest way is to simply let sizers determine the scrolling area. This is now the default when you set an interior sizer into a wxScrolled with wxWindow::SetSizer(). The scrolling area will be set to the size requested by the sizer and the scrollbars will be assigned for each orientation according to the need for them and the scrolling increment set by SetScrollRate(). As above, scrolling is only enabled in orientations with a non-zero increment. You can influence the minimum size of the scrolled area controlled by a sizer by calling wxWindow::SetVirtualSizeHints(). (Calling SetScrollbars() has analogous effects in wxWidgets 2.4 – in later versions it may not continue to override the sizer.)

Note that if maximum size hints are still supported by wxWindow::SetVirtualSizeHints(), use them at your own dire risk. They may or may not have been removed for 2.4, but it really only makes sense to set minimum size hints here. We should probably replace wxWindow::SetVirtualSizeHints() with wxWindow::SetMinVirtualSize() or similar and remove it entirely in future.

Todo:
review docs for this class replacing SetVirtualSizeHints() with SetMinClientSize().

As with all windows, an application can draw onto a wxScrolled using a device context.

You have the option of handling the OnPaint handler or overriding the wxScrolled::OnDraw() function, which is passed a pre-scrolled device context (prepared by wxScrolled::DoPrepareDC()).

If you don't wish to calculate your own scrolling, you must call DoPrepareDC() when not drawing from within OnDraw(), to set the device origin for the device context according to the current scroll position.

A wxScrolled will normally scroll itself and therefore its child windows as well. It might however be desired to scroll a different window than itself: e.g. when designing a spreadsheet, you will normally only have to scroll the (usually white) cell area, whereas the (usually grey) label area will scroll very differently. For this special purpose, you can call SetTargetWindow() which means that pressing the scrollbars will scroll a different window.

Note that the underlying system knows nothing about scrolling coordinates, so that all system functions (mouse events, expose events, refresh calls etc) as well as the position of subwindows are relative to the "physical" origin of the scrolled window. If the user insert a child window at position (10,10) and scrolls the window down 100 pixels (moving the child window out of the visible area), the child window will report a position of (10,-90).

Styles

This class supports the following styles:

  • wxHSCROLL:
    If this style is specified and wxVSCROLL isn't, the window will be scrollable only in horizontal direction (by default, i.e. if neither this style nor wxVSCROLL is specified, it scrolls in both directions).
  • wxVSCROLL:
    If this style is specified and wxHSCROLL isn't, the window will be scrollable only in vertical direction (by default, i.e. if neither this style nor wxHSCROLL is specified, it scrolls in both directions).
  • wxALWAYS_SHOW_SB:
    Since wxWidgets 2.9.5, specifying this style makes the window always show its scrollbars, even if they are not used. See ShowScrollbars().
  • wxRETAINED:
    Uses a backing pixmap to speed refreshes. Motif only.

Events emitted by this class

The following event handler macros redirect the events to member function handlers 'func' with prototypes like:

void handlerFuncName(wxScrollWinEvent& event)

Event macros for events emitted by this class:

  • EVT_SCROLLWIN(func):
    Process all scroll events.
  • EVT_SCROLLWIN_TOP(func):
    Process wxEVT_SCROLLWIN_TOP scroll-to-top events.
  • EVT_SCROLLWIN_BOTTOM(func):
    Process wxEVT_SCROLLWIN_BOTTOM scroll-to-bottom events.
  • EVT_SCROLLWIN_LINEUP(func):
    Process wxEVT_SCROLLWIN_LINEUP line up events.
  • EVT_SCROLLWIN_LINEDOWN(func):
    Process wxEVT_SCROLLWIN_LINEDOWN line down events.
  • EVT_SCROLLWIN_PAGEUP(func):
    Process wxEVT_SCROLLWIN_PAGEUP page up events.
  • EVT_SCROLLWIN_PAGEDOWN(func):
    Process wxEVT_SCROLLWIN_PAGEDOWN page down events.
  • EVT_SCROLLWIN_THUMBTRACK(func):
    Process wxEVT_SCROLLWIN_THUMBTRACK thumbtrack events (frequent events sent as the user drags the thumbtrack).
  • EVT_SCROLLWIN_THUMBRELEASE(func):
    Process wxEVT_SCROLLWIN_THUMBRELEASE thumb release events.
Note
Don't confuse wxScrollWinEvents generated by this class with wxScrollEvent objects generated by wxScrollBar and wxSlider.
Remarks
Use wxScrolled for applications where the user scrolls by a fixed amount, and where a 'page' can be interpreted to be the current visible portion of the window. For more sophisticated applications, use the wxScrolled implementation as a guide to build your own scroll behaviour or use wxVScrolledWindow or its variants.
Since
The wxScrolled template exists since version 2.9.0. In older versions, only wxScrolledWindow (equivalent of wxScrolled<wxPanel>) was available.

Library:  wxCore
Category:  Miscellaneous Windows
See Also
wxScrollBar, wxClientDC, wxPaintDC, wxVScrolledWindow, wxHScrolledWindow, wxHVScrolledWindow,

Public Member Functions

 wxScrolled ()
 Default constructor.
 
 wxScrolled (wxWindow *parent, wxWindowID id=-1, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxHSCROLL|wxVSCROLL, const wxString &name="scrolledWindow")
 Constructor.
 
void CalcScrolledPosition (int x, int y, int *xx, int *yy) const
 Translates the logical coordinates to the device ones.
 
wxPoint CalcScrolledPosition (const wxPoint &pt) const
 
void CalcUnscrolledPosition (int x, int y, int *xx, int *yy) const
 Translates the device coordinates to the logical ones.
 
wxPoint CalcUnscrolledPosition (const wxPoint &pt) const
 
bool Create (wxWindow *parent, wxWindowID id=-1, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxHSCROLL|wxVSCROLL, const wxString &name="scrolledWindow")
 Creates the window for two-step construction.
 
void DisableKeyboardScrolling ()
 Disable use of keyboard keys for scrolling.
 
void DoPrepareDC (wxDC &dc)
 Call this function to prepare the device context for drawing a scrolled image.
 
void EnableScrolling (bool xScrolling, bool yScrolling)
 Enable or disable use of wxWindow::ScrollWindow() for scrolling.
 
void ShowScrollbars (wxScrollbarVisibility horz, wxScrollbarVisibility vert)
 Set the scrollbar visibility.
 
void GetScrollPixelsPerUnit (int *xUnit, int *yUnit) const
 Get the number of pixels per scroll unit (line), in each direction, as set by SetScrollbars().
 
void GetViewStart (int *x, int *y) const
 Get the position at which the visible portion of the window starts.
 
wxPoint GetViewStart () const
 This is a simple overload of GetViewStart(int*,int*); see that function for more info.
 
void GetVirtualSize (int *x, int *y) const
 Gets the size in device units of the scrollable window area (as opposed to the client size, which is the area of the window currently visible).
 
bool IsRetained () const
 Motif only: true if the window has a backing bitmap.
 
virtual void OnDraw (wxDC &dc)
 Called by the default paint event handler to allow the application to define painting behaviour without having to worry about calling DoPrepareDC().
 
void PrepareDC (wxDC &dc)
 This function is for backwards compatibility only and simply calls DoPrepareDC() now.
 
void Scroll (int x, int y)
 Scrolls a window so the view start is at the given point.
 
void Scroll (const wxPoint &pt)
 This is an overload of Scroll(int,int); see that function for more info.
 
void SetScrollRate (int xstep, int ystep)
 Set the horizontal and vertical scrolling increment only.
 
void SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY, int noUnitsX, int noUnitsY, int xPos=0, int yPos=0, bool noRefresh=false)
 Sets up vertical and/or horizontal scrollbars.
 
void SetTargetWindow (wxWindow *window)
 Call this function to tell wxScrolled to perform the actual scrolling on a different window (and not on itself).
 
wxWindowGetTargetWindow () const
 
void SetTargetRect (const wxRect &rect)
 
wxRect GetTargetRect () const
 
int GetScrollPageSize (int orient) const
 
void SetScrollPageSize (int orient, int pageSize)
 
int GetScrollLines (int orient) const
 
void SetScale (double xs, double ys)
 
double GetScaleX () const
 
double GetScaleY () const
 
virtual void AdjustScrollbars ()
 
bool IsAutoScrolling () const
 Are we generating the autoscroll events?
 
void StopAutoScrolling ()
 Stop generating the scroll events when mouse is held outside the window.
 
virtual bool SendAutoScrollEvents (wxScrollWinEvent &event) const
 This method can be overridden in a derived class to forbid sending the auto scroll events - note that unlike StopAutoScrolling() it doesn't stop the timer, so it will be called repeatedly and will typically return different values depending on the current mouse position.
 

Protected Member Functions

virtual wxSize GetSizeAvailableForScrollTarget (const wxSize &size)
 Function which must be overridden to implement the size available for the scroll target for the given size of the main window.
 

Constructor & Destructor Documentation

template<class T >
wxScrolled< T >::wxScrolled ( )

Default constructor.

template<class T >
wxScrolled< T >::wxScrolled ( wxWindow parent,
wxWindowID  id = -1,
const wxPoint pos = wxDefaultPosition,
const wxSize size = wxDefaultSize,
long  style = wxHSCROLL|wxVSCROLL,
const wxString name = "scrolledWindow" 
)

Constructor.

Parameters
parentParent window.
idWindow identifier. The value wxID_ANY indicates a default value.
posWindow position. If a position of wxDefaultPosition is specified then a default position is chosen.
sizeWindow size. If a size of wxDefaultSize is specified then the window is sized appropriately.
styleWindow style. See wxScrolled.
nameWindow name.
Remarks
The window is initially created without visible scrollbars. Call SetScrollbars() to specify how big the virtual window size should be.

Member Function Documentation

template<class T >
virtual void wxScrolled< T >::AdjustScrollbars ( )
virtual
template<class T >
void wxScrolled< T >::CalcScrolledPosition ( int  x,
int  y,
int *  xx,
int *  yy 
) const

Translates the logical coordinates to the device ones.

For example, if a window is scrolled 10 pixels to the bottom, the device coordinates of the origin are (0, 0) (as always), but the logical coordinates are (0, 10) and so the call to CalcScrolledPosition(0, 10, xx, yy) will return 0 in yy.

wxPerl Note: In wxPerl this method takes two parameters and returns a 2-element list (xx, yy).

See Also
CalcUnscrolledPosition()
template<class T >
wxPoint wxScrolled< T >::CalcScrolledPosition ( const wxPoint pt) const
template<class T >
void wxScrolled< T >::CalcUnscrolledPosition ( int  x,
int  y,
int *  xx,
int *  yy 
) const

Translates the device coordinates to the logical ones.

For example, if a window is scrolled 10 pixels to the bottom, the device coordinates of the origin are (0, 0) (as always), but the logical coordinates are (0, 10) and so the call to CalcUnscrolledPosition(0, 0, xx, yy) will return 10 in yy.

wxPerl Note: In wxPerl this method takes two parameters and returns a 2-element list (xx, yy).

See Also
CalcScrolledPosition()
template<class T >
wxPoint wxScrolled< T >::CalcUnscrolledPosition ( const wxPoint pt) const
template<class T >
bool wxScrolled< T >::Create ( wxWindow parent,
wxWindowID  id = -1,
const wxPoint pos = wxDefaultPosition,
const wxSize size = wxDefaultSize,
long  style = wxHSCROLL|wxVSCROLL,
const wxString name = "scrolledWindow" 
)

Creates the window for two-step construction.

Derived classes should call or replace this function. See wxScrolled::wxScrolled() for details.

template<class T >
void wxScrolled< T >::DisableKeyboardScrolling ( )

Disable use of keyboard keys for scrolling.

By default cursor movement keys (including Home, End, Page Up and Down) are used to scroll the window appropriately. If the derived class uses these keys for something else, e.g. changing the currently selected item, this function can be used to disable this behaviour as it's not only not necessary then but can actually be actively harmful if another object forwards a keyboard event corresponding to one of the above keys to us using ProcessWindowEvent() because the event will always be processed which can be undesirable.

Since
2.9.1
template<class T >
void wxScrolled< T >::DoPrepareDC ( wxDC dc)

Call this function to prepare the device context for drawing a scrolled image.

It sets the device origin according to the current scroll position. DoPrepareDC() is called automatically within the default wxEVT_PAINT event handler, so your OnDraw() override will be passed an already 'pre-scrolled' device context. However, if you wish to draw from outside of OnDraw() (e.g. from your own wxEVT_PAINT handler), you must call this function yourself.

For example:

void MyWindow::OnEvent(wxMouseEvent& event)
{
wxClientDC dc(this);
float x, y;
event.Position(&x, &y);
if (xpos > -1 && ypos > -1 && event.Dragging())
{
dc.DrawLine(xpos, ypos, x, y);
}
xpos = x;
ypos = y;
}

Notice that the function sets the origin by moving it relatively to the current origin position, so you shouldn't change the origin before calling DoPrepareDC() or, if you do, reset it to (0, 0) later. If you call DoPrepareDC() immediately after device context creation, as in the example above, this problem doesn't arise, of course, so it is customary to do it like this.

template<class T >
void wxScrolled< T >::EnableScrolling ( bool  xScrolling,
bool  yScrolling 
)

Enable or disable use of wxWindow::ScrollWindow() for scrolling.

By default, when a scrolled window is logically scrolled, wxWindow::ScrollWindow() is called on the underlying window which scrolls the window contents and only invalidates the part of the window newly brought into view. If false is passed as an argument, then this "physical scrolling" is disabled and the window is entirely invalidated whenever it is scrolled by calling wxWindow::Refresh().

It should be rarely necessary to disable physical scrolling, so this method shouldn't be called in normal circumstances.

Parameters
xScrollingIf true, enables physical scrolling in the x direction.
yScrollingIf true, enables physical scrolling in the y direction.
template<class T >
double wxScrolled< T >::GetScaleX ( ) const
template<class T >
double wxScrolled< T >::GetScaleY ( ) const
template<class T >
int wxScrolled< T >::GetScrollLines ( int  orient) const
template<class T >
int wxScrolled< T >::GetScrollPageSize ( int  orient) const
template<class T >
void wxScrolled< T >::GetScrollPixelsPerUnit ( int *  xUnit,
int *  yUnit 
) const

Get the number of pixels per scroll unit (line), in each direction, as set by SetScrollbars().

A value of zero indicates no scrolling in that direction.

Parameters
xUnitReceives the number of pixels per horizontal unit.
yUnitReceives the number of pixels per vertical unit.

wxPerl Note: In wxPerl this method takes no parameters and returns a 2-element list (xUnit, yUnit).

See Also
SetScrollbars(), GetVirtualSize()
template<class T >
virtual wxSize wxScrolled< T >::GetSizeAvailableForScrollTarget ( const wxSize size)
protectedvirtual

Function which must be overridden to implement the size available for the scroll target for the given size of the main window.

This method must be overridden if SetTargetWindow() is used (it is never called otherwise). The implementation should decrease the size to account for the size of the non-scrollable parts of the main window and return only the size available for the scrollable window itself. E.g. in the example given in SetTargetWindow() documentation the function would subtract the height of the header window from the vertical component of size.

template<class T >
wxRect wxScrolled< T >::GetTargetRect ( ) const
template<class T >
wxWindow* wxScrolled< T >::GetTargetWindow ( ) const
template<class T >
void wxScrolled< T >::GetViewStart ( int *  x,
int *  y 
) const

Get the position at which the visible portion of the window starts.

Parameters
xReceives the first visible x position in scroll units.
yReceives the first visible y position in scroll units.
Remarks
If either of the scrollbars is not at the home position, x and/or y will be greater than zero. Combined with wxWindow::GetClientSize(), the application can use this function to efficiently redraw only the visible portion of the window. The positions are in logical scroll units, not pixels, so to convert to pixels you will have to multiply by the number of pixels per scroll increment.

wxPerl Note: In wxPerl this method takes no parameters and returns a 2-element list (x, y).

See Also
SetScrollbars(), Scroll()
template<class T >
wxPoint wxScrolled< T >::GetViewStart ( ) const

This is a simple overload of GetViewStart(int*,int*); see that function for more info.

template<class T >
void wxScrolled< T >::GetVirtualSize ( int *  x,
int *  y 
) const

Gets the size in device units of the scrollable window area (as opposed to the client size, which is the area of the window currently visible).

Parameters
xReceives the length of the scrollable window, in pixels.
yReceives the height of the scrollable window, in pixels.
Remarks
Use wxDC::DeviceToLogicalX() and wxDC::DeviceToLogicalY() to translate these units to logical units.

wxPerl Note: In wxPerl this method takes no parameters and returns a 2-element list (xUnit, yUnit).

See Also
SetScrollbars(), GetScrollPixelsPerUnit()
template<class T >
bool wxScrolled< T >::IsAutoScrolling ( ) const

Are we generating the autoscroll events?

template<class T >
bool wxScrolled< T >::IsRetained ( ) const

Motif only: true if the window has a backing bitmap.

template<class T >
virtual void wxScrolled< T >::OnDraw ( wxDC dc)
virtual

Called by the default paint event handler to allow the application to define painting behaviour without having to worry about calling DoPrepareDC().

Instead of overriding this function you may also just process the paint event in the derived class as usual, but then you will have to call DoPrepareDC() yourself.

template<class T >
void wxScrolled< T >::PrepareDC ( wxDC dc)

This function is for backwards compatibility only and simply calls DoPrepareDC() now.

Notice that it is not called by the default paint event handle (DoPrepareDC() is), so overriding this method in your derived class is useless.

template<class T >
void wxScrolled< T >::Scroll ( int  x,
int  y 
)

Scrolls a window so the view start is at the given point.

Parameters
xThe x position to scroll to, in scroll units.
yThe y position to scroll to, in scroll units.
Remarks
The positions are in scroll units, not pixels, so to convert to pixels you will have to multiply by the number of pixels per scroll increment. If either parameter is wxDefaultCoord (-1), that position will be ignored (no change in that direction).
See Also
SetScrollbars(), GetScrollPixelsPerUnit()
template<class T >
void wxScrolled< T >::Scroll ( const wxPoint pt)

This is an overload of Scroll(int,int); see that function for more info.

template<class T >
virtual bool wxScrolled< T >::SendAutoScrollEvents ( wxScrollWinEvent event) const
virtual

This method can be overridden in a derived class to forbid sending the auto scroll events - note that unlike StopAutoScrolling() it doesn't stop the timer, so it will be called repeatedly and will typically return different values depending on the current mouse position.

The base class version just returns true.

template<class T >
void wxScrolled< T >::SetScale ( double  xs,
double  ys 
)
template<class T >
void wxScrolled< T >::SetScrollbars ( int  pixelsPerUnitX,
int  pixelsPerUnitY,
int  noUnitsX,
int  noUnitsY,
int  xPos = 0,
int  yPos = 0,
bool  noRefresh = false 
)

Sets up vertical and/or horizontal scrollbars.

The first pair of parameters give the number of pixels per 'scroll step', i.e. amount moved when the up or down scroll arrows are pressed. The second pair gives the length of scrollbar in scroll steps, which sets the size of the virtual window.

xPos and yPos optionally specify a position to scroll to immediately.

For example, the following gives a window horizontal and vertical scrollbars with 20 pixels per scroll step, and a size of 50 steps (1000 pixels) in each direction:

window->SetScrollbars(20, 20, 50, 50);

wxScrolled manages the page size itself, using the current client window size as the page size.

Note that for more sophisticated scrolling applications, for example where scroll steps may be variable according to the position in the document, it will be necessary to derive a new class from wxWindow, overriding OnSize() and adjusting the scrollbars appropriately.

Parameters
pixelsPerUnitXPixels per scroll unit in the horizontal direction.
pixelsPerUnitYPixels per scroll unit in the vertical direction.
noUnitsXNumber of units in the horizontal direction.
noUnitsYNumber of units in the vertical direction.
xPosPosition to initialize the scrollbars in the horizontal direction, in scroll units.
yPosPosition to initialize the scrollbars in the vertical direction, in scroll units.
noRefreshWill not refresh window if true.
See Also
wxWindow::SetVirtualSize()
template<class T >
void wxScrolled< T >::SetScrollPageSize ( int  orient,
int  pageSize 
)
template<class T >
void wxScrolled< T >::SetScrollRate ( int  xstep,
int  ystep 
)

Set the horizontal and vertical scrolling increment only.

See the pixelsPerUnit parameter in SetScrollbars().

template<class T >
void wxScrolled< T >::SetTargetRect ( const wxRect rect)
template<class T >
void wxScrolled< T >::SetTargetWindow ( wxWindow window)

Call this function to tell wxScrolled to perform the actual scrolling on a different window (and not on itself).

This method is useful when only a part of the window should be scrolled. A typical example is a control consisting of a fixed header and the scrollable contents window: the scrollbars are attached to the main window itself, hence it, and not the contents window must be derived from wxScrolled, but only the contents window scrolls when the scrollbars are used. To implement such setup, you need to call this method with the contents window as argument.

Notice that if this method is used, GetSizeAvailableForScrollTarget() method must be overridden.

template<class T >
void wxScrolled< T >::ShowScrollbars ( wxScrollbarVisibility  horz,
wxScrollbarVisibility  vert 
)

Set the scrollbar visibility.

By default the scrollbar in the corresponding direction is only shown if it is needed, i.e. if the virtual size of the scrolled window in this direction is greater than the current physical window size. Using this function the scrollbar visibility can be changed to be:

  • wxSHOW_SB_ALWAYS: To always show the scrollbar, even if it is not needed currently (wxALWAYS_SHOW_SB style can be used during the window creation to achieve the same effect but it applies in both directions).
  • wxSHOW_SB_NEVER: To never show the scrollbar at all. In this case the program should presumably provide some other way for the user to scroll the window.
  • wxSHOW_SB_DEFAULT: To restore the default behaviour described above.
Parameters
horzThe desired visibility for the horizontal scrollbar.
vertThe desired visibility for the vertical scrollbar.
Since
2.9.0
template<class T >
void wxScrolled< T >::StopAutoScrolling ( )

Stop generating the scroll events when mouse is held outside the window.