Scrolling a wxGrid while it is being updated

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
mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Scrolling a wxGrid while it is being updated

Post by mbeardsley » Tue Feb 28, 2017 5:09 pm

I have a strange problem when scrolling a wxGrid.

The text in the Grid is being updated once a second (on a timer loop), because some of the text fields are constantly changing. When the user tries to scroll down (by either dragging the scroll bars or clicking the down arrow button at the bottom of the scroll bar), the window focus is lost from the entire application. Clicking on some other running app (or the windows task bar), and then clicking back to my app seems to recover it. Sometimes some of the column heading text gets blanked out also (the column heading text is defined once and never changed).

Strangely enough, this does NOT happen if the user scrolls the Grid using just the mouse wheel.

Is there some known problem caused by scrolling a Grid while the text is being updated?

I do have a special cell renderer for one column, but it is just a simple wxGridCellBoolRenderer, could this matter? The rest of the columns are just simple read-only text strings.

This is on VC10 - Windows7 using wxWidgets 3.0.2.

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Scrolling a wxGrid while it is being updated

Post by mbeardsley » Tue Feb 28, 2017 5:58 pm

Well, I have some additional info.

It turns out that the problem only seems to occur if the entire grid is being rebuilt while a scrolling operation is in progress.

Periodically, I delete all rows of the grid and re-create them from scratch, usually due to objects in the list being added or deleted (the objects in the grid are sorted, so rebuilding the list/grid is actually the easiest thing to do).

However, it seems that if I do this at the moment that the user is scrolling the grid up/down, the focus and/or scrolling gets confused and strange problems result.

Is there some way to disable the scrolling while I am updating (or even if I knew that scrolling was in-progress, I could probably delay the rebuilding of the grid)?

Thanks.

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Scrolling a wxGrid while it is being updated

Post by mbeardsley » Tue Feb 28, 2017 8:34 pm

It seems after further inspection that the problem is caused by deleting rows of the table while a scroll operation is in progress.
It gets the scroll/mouse/focus system confused (though scrolling with the mouse wheel seems to be ok).

Is there some sort of function/event that I could latch onto where it would be safe to delete and recreate the grid rows?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3602
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Scrolling a wxGrid while it is being updated

Post by ONEEYEMAN » Tue Feb 28, 2017 8:43 pm

Hi,
Can't you just use the flag which will prevent scrolling while grid is rebuilding or vice versa?

Thank you.

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Scrolling a wxGrid while it is being updated

Post by mbeardsley » Tue Feb 28, 2017 8:58 pm

Unfortunately no.

Turning off the EnableScrolling() does not work if a scroll operation is already in progress (it appears to get ignored in that case).

I'm not sure what you mean by vice-versa, but I assume that you mean don't rebuild while a scroll is in progress, but I haven't found a way to reliably detect that. There doesn't seem to be a wxGrid "scroll" event.

I even tried using wxMouseState to explicitly turn off the left mouse button while I rebuild (even though it would give me problems in other cases), but that doesn't seem to work if the scroll operation has already started.

It does seem that deleting the rows is the problem. If I only add rows, it all seems to work. Unfortunately, I have to be able to handle the case where the new grid has fewer rows than the previous grid.

I am showing a list of objects periodically sent by other computers and that list can both grow and shrink.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3602
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Scrolling a wxGrid while it is being updated

Post by ONEEYEMAN » Tue Feb 28, 2017 9:09 pm

Hi,
Try to catch wxEVT_SCROLL event on the grid and if the flag is not set skip the event. Otherwise just return.

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 14305
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Scrolling a wxGrid while it is being updated

Post by doublemax » Tue Feb 28, 2017 9:14 pm

The issue with the lost focus is a little suspicious. Are you running in debug or release mode? If you're testing in release mode, please try with a debug build. Maybe you're missing some asserts.

Can't you just leave a bunch of empty lines at the end so that you don't have to recreate the whole grid each time?

If that's not reasonable for your purpose, rebuilding the grid from scratch doesn't sound like a good idea. If the new number of rows is smaller, i would just delete the extra lines.

As a workaround, whenever a scroll event happens you could disable the grid update for a short period of time, e.g. 0.5 to 1 second. You can do this by just setting a flag that stops the update internally and starting a timer with that period. As every scroll event will (re-)start the timer, the grid will only get updated when no scroll event occurred for that period of time.
Use the source, Luke!

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Scrolling a wxGrid while it is being updated

Post by mbeardsley » Tue Feb 28, 2017 9:25 pm

doublemax wrote:The issue with the lost focus is a little suspicious. Are you running in debug or release mode? If you're testing in release mode, please try with a debug build. Maybe you're missing some asserts.
This happens in both Debug and Release builds.
doublemax wrote:Can't you just leave a bunch of empty lines at the end so that you don't have to recreate the whole grid each time?

If that's not reasonable for your purpose, rebuilding the grid from scratch doesn't sound like a good idea. If the new number of rows is smaller, i would just delete the extra lines.
Well, I can't delete the extra lines, as that causes the problem to occur. It doesn't seem to matter if I delete one line or all of them if a scroll is in progress. Leaving behind extra lines is not a good idea either as it both looks wrong and distorts the size of the scroll bars.
doublemax wrote:As a workaround, whenever a scroll event happens you could disable the grid update for a short period of time, e.g. 0.5 to 1 second. You can do this by just setting a flag that stops the update internally and starting a timer with that period. As every scroll event will (re-)start the timer, the grid will only get updated when no scroll event occurred for that period of time.
This is probably what I will have to do, though it does not seem very foolproof.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3602
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Scrolling a wxGrid while it is being updated

Post by ONEEYEMAN » Tue Feb 28, 2017 9:36 pm

Hi,
If I understand correctly the issue happens when you need to resort the data?
Then why can't you clear the grid leaving the actual rows but just recreate the view?

Thank ,you.

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Scrolling a wxGrid while it is being updated

Post by mbeardsley » Tue Feb 28, 2017 10:05 pm

It's not just a re-sort.

The "new" data may have more or less rows than the "old" data.
If the "new" data has more rows, I can add them without any issues.
But, if the "new" data has fewer rows, deleting the (now) extra rows causes the problem (but only if a scroll operation is in progress).

I can leave the extra "empty" rows behind, but that doesn't look very good.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3602
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Scrolling a wxGrid while it is being updated

Post by ONEEYEMAN » Tue Feb 28, 2017 11:43 pm

Hi,
Then like I said use the flag and catch wxEVT_SCROLL event.

Thank you.'

Manolo
Can't get richer than this
Can't get richer than this
Posts: 704
Joined: Mon Apr 30, 2012 11:07 pm

Re: Scrolling a wxGrid while it is being updated

Post by Manolo » Tue Feb 28, 2017 11:45 pm

Call your update-grid-func from a wxIdleEvent handler.

Post Reply