A custom scroller control...
Scroller is a custom control (Windows only - coded in Purebasic) offering full scrolling facilities. Use it as the basis for your own custom controls which require scrolling facilities etc! Indeed, one of the scroller demos uses 'Scroller' to create a scrolling-image control which can be added to your own applications!
Scroller is intended for experienced Purebasic users as, for regardless of how much assistance a control like Scroller can render, creating custom controls is still a very technical affair.* The various Scroller demonstration programs begin to show how to create new controls.
A little background on Windows scrolling may help shed some light on just why I have created this control.
Time for some questions and answers then ......
Does Windows itself scroll the contents of a control's client area?
We are of course referring to the case when our end-users attempt to scroll the contents of the control through the scrollbars etc.
It is a common misconception amongst the inexperienced Windows programmers to think that it is enough to slap a couple of scrollbars on a window/control to then have Windows itself take care of all scrolling automatically! Ah, if only this were true!
In fact, Windows is of very little help in this regard. It draws the scrollbars for us... but that is about all! Indeed, there really is little in the way that Windows can do in this regard. For example, Windows cannot know in advance by how much our application wishes to scroll the client area in response to the user clicking the downward arrow on a vertical scrollbar etc. Many applications scroll by different amounts each time to reflect such things as variable line heights etc. This is why Windows will not even move the scroll-box in response to such user actions!
The point is that adding scrollbars to our custom control adds absolutely nothing in the way of scrolling functionality to our control. We have to add all of the code ourselves - and such code can be very tricky depending on the nature of the underlying control.
The answer to this question, therefore, is a resounding no!
Doh!
How then do we set about adding scrolling facilities to our controls?
There are two commonly used methods for achieving this.
The first commonly used method is that adopted, for example, by Purebasic's ScrollArea gadget - which basically cheats! :) When the user clicks a scrollbar button, Purebasic's ScrollArea gadget doesn't actually scroll anything as such. Instead this gadget contains an 'inner container', an additional control which sits inside the ScrollArea which Purebasic moves around in response to the user's actions, giving the impression of scrolling. This inner-container's dimensions will exactly match the respective scrolling ranges etc.
When you add, say, a button gadget to Purebasic's ScrollArea gadget, this button is actually added to the inner-container and when this container is moved around, the button is of course also moved which effectively allows the button to be scrolled up and down etc.
A very neat trick! Very nice.
However, whilst the ScrollArea gadget is suitable as the basis for many custom controls, it does suffer with one huge drawback, namely that Windows imposes a 16-bit limit upon the dimensions of child controls. This results in a maximum scroll range, in Purebasic, of 32767 pixels for the ScrollArea!
Doh!
The second, all powerful method (and thus the more complex!) is to dispense with this inner-container and instead work directly with the control in hand, it's scrollbars, the scrolling, and to employ custom painting techniques as appropriate. This means that our scrolling range is not restricted to 32767 pixels but can instead use the full range of 32-bit values etc. This also means that it is not so easy to add child controls to our custom control (as we have to reposition them manually in response to every scrolling action) but it is nevertheless ideal for controls whose contents are painted directly through gdi etc.
Scroller of course uses this second method.
With this second method (in which we are responsible for most of the work involved in scrolling) we essentially have to repaint our control's client area to reflect the new scroll positions whenever the user hits one of the scrollbars etc. Windows offers a few functions to assist us with this (which Scroller makes good use of!) but we are nevertheless called upon to work our magic with our own painting routines.
How does Scroller assist with the scrolling?
Any custom control based upon Scroller can call upon the latter for all (or some) of it's scrolling needs. Scroller can handle all of the scroll-box positioning, the page sizes and physically scrolls the client area where possible.
The custom control will have to take charge of the painting in order to give some content to the control etc. but Scroller can take charge of the scrolling aspects.
See the various demo programs for some examples of putting Scroller to use.
Of course, more complex custom controls will need to override some (or even all!) of Scroller's default behaviour and take personal charge of the scrolling itself. For example, I myself shall be using Scroller as the basis of a new grid control. This control will need to override Scroller's scrolling routines in order to scroll just parts of the client area (as opposed to the entire client area!)
Swings and roundabouts!
Who is Scroller for?
Quite simply, Scroller can be used as the basis for many custom controls required to offer scrolling facilities.
In cases where Scroller is perhaps not appropriate because of the need to customise the scrolling to such an extent that a new control is perhaps more suitable, Scroller could nevertheless prove useful as a learning aid for those new to the business of creating custom controls and new to the work involved in scrolling a window or control's client area. :)
*Those new to this business should read my tutorial on 'Subclassing' in order to become acquainted with some of the skills required for creating custom controls.