2008-03-08

Scrollbars, Again

A few days ago I changed the way that the ScrollableBase models its scrollable area. Instead of assigning two arbitrary co-ordinates that the class can scroll through, it now models this region as a virtual canvas. The developer specifies the width, height and co-ordinates of the canvas, and the class can then scroll around it. This meant some changes in the ScrollingPanel and MultiLineTextBox, but it’s made the whole thing much easier to work with and much easier to visualise.

Today I’ve fixed most of the bugs in the SliderVertical class. Most of the maths was wrong - it’s much easier to see the problems now that the scrolling makes sense - and now it all seems to work. There are still a few problems and outstanding changes to make:

  • The MultiLineTextBox doesn’t seem to show the whole of the last line of text when new text is appended (see the debug console for an example);
  • The last pixel in the scrollbar’s height is affected by rounding errors (not sure if there’s a practical way around this);
  • Wiring up the scrollbar/textbox events causes problems.

The last one is interesting. If we change the text in the textbox or drag it around, we want the scrollbar to move. If we move the scrollbar, we want the textbox to move. Here’s the problem - if the scrollbar moves and we move the textbox, moving the textbox will move the scrollbar. Infinite loop.

I’ve added a scrollbar to the debug console. Moving the scrollbar doesn’t affect the text yet due to the problem outlined above, but it does mimic the text’s movement and it does resize to match the amount of text displayed.

Woopsi Debug Console with Scrollbar

EDIT: Fixed the infinite loop problem. It’s just a matter of checking if the scrollbar is active or not. If it’s not active, ignore any events that come from it as they’re not user-triggered. If it is active, process only the scrollbar events as any textbox events will be triggered by the scrollbar. That works, but it’s not perfect, because the textbox could be updated by something elsewhere and not scroll because the user has activated the scrollbar.