How to implement scrolling?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Caofi
Experienced Solver
Experienced Solver
Posts: 60
Joined: Wed Dec 01, 2021 12:47 am

How to implement scrolling?

Post by Caofi »

So, I have a "ResultCanvas" derived from wxScrolledWindow, and I'm trying to get it to scroll when the output is too large for the given frame size.
But I really have no idea how to do it! And my code is too large to paste into the forum.
Also, I'm using wxAutoBufferedPaintDC to do the drawing (which solved my flickering issue).

Any tips? Thanks!
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to implement scrolling?

Post by doublemax »

It's unclear where you're stuck. The most important part is to call SetScrollbars() to define the virtual size and the scrollsteps.
https://docs.wxwidgets.org/trunk/classw ... 4a8e1210bb

That should already give you the scrollbars and the option to scroll the window.
Use the source, Luke!
Caofi
Experienced Solver
Experienced Solver
Posts: 60
Joined: Wed Dec 01, 2021 12:47 am

Re: How to implement scrolling?

Post by Caofi »

Wow! Thanks!! This has been on my to-do list for ages now, and you solved it with one line of code:

Code: Select all

SetScrollbars(20, 20, 50, 50);
which I put in my constructor for ResultCanvas.

In quick testing it seems to work perfectly.

Edit: OK. I tested it on a larger example, and it is not 100% correct. It scrolls enough to display like a page and 3/4, but after that it just chomps the file. In fact, in total, even when scrolled all the way down, it is only showing half what it should be.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to implement scrolling?

Post by doublemax »

What exactly are you displaying and how do you calculate the virtual size of the content?
Use the source, Luke!
Caofi
Experienced Solver
Experienced Solver
Posts: 60
Joined: Wed Dec 01, 2021 12:47 am

Re: How to implement scrolling?

Post by Caofi »

I'm basically displaying clickable text at this point, but later plan to add in images and such.
Anyway, your post was enough for me to solve it. With a little googling, I found my way to:

Code: Select all

void wxWindow::SetVirtualSize(int width, int height)
I added this to the end of my Draw() method, and now it works correctly, even for the larger example.
Though I suspect my choice for width might not be quite correct, and I might need to use std::max() of all the widths used in the Draw() method.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to implement scrolling?

Post by doublemax »

Caofi wrote: Mon Jan 17, 2022 8:20 am I added this to the end of my Draw() method, and now it works correctly, even for the larger example.
Doing that in the Draw() method doesn't seem right, as it could lead to permanent redraws. You should only call it when the virtual size changes.
Use the source, Luke!
Caofi
Experienced Solver
Experienced Solver
Posts: 60
Joined: Wed Dec 01, 2021 12:47 am

Re: How to implement scrolling?

Post by Caofi »

Thanks for the tip!

So I shifted it from the end of Draw() to InsertNewLine() and InsertLine() (the first is a line feed, the second is a horizontal line).

Also, I did have to use

Code: Select all

m_max_x = std::max(m_max_x, m_x)
to handle long lines correctly.

And then in InsertNewLine() and InsertLine()

Code: Select all

SetVirtualSize(m_max_x, m_y)
Anyway, it is now all working. Thanks for the help!
Post Reply