I’m working on a Haiku app with a BTextView inside a BScrollView. I’m trying to preserve the scroll position when the user resizes the main window, but I can’t seem to stop the scrollbars from jumping back to the top/left during resize.
fTextView = new UndoableTextView("TextView");
fScrollView = new BScrollView("TextViewScroll", fTextView,
B_WILL_DRAW | B_FRAME_EVENTS, true, true, B_PLAIN_BORDER);
I have tried to save the scroll position in MainWindow::FrameResized:
void MainWindow::FrameResized(float newWidth, float newHeight)
{
// Save current scroll position
BPoint scrollOffset = fTextView->Bounds().LeftTop();
if (scrollOffset.x > 0 || scrollOffset.y > 0)
printf("Scroll offset: X%f, X%f\n", scrollOffset.x, scrollOffset.y);
// Call parent implementation
BWindow::FrameResized(newWidth, newHeight);
// Restore scroll position
fTextView->ScrollTo(scrollOffset);
}
The scroll values (scrollOffset) seem to get reset to 0.0 during resize. I confirmed with debug prints that they’re > 0 when resizing begins, but are being set to 0 during or after the resize event.
Any tips on how to properly restore the scroll position when resizing the window, without jumping back to (0,0)? Is there a better hook than FrameResized() for restoring scroll?
One possibility: Are you setting any kind of custom flags on that UndoableTextView? Does it work with a regular BTextView but not the custom one? If you inadvertently clear the B_SCROLL_VIEW_AWARE flag on the BTextView, then the scrollview may indeed behave oddly.
I’m not setting any custom flags, but the UndoableTextView can be changed both manually and programmatically, which might have caused the flag to be cleared.