Tk Source Code

View Ticket
Login
Ticket UUID: 14188104c320c218373dd192cb85574fa098d695
Title: ttk::treeview vertical scrollbar not updating
Type: Bug Version: 8.6.10
Submitter: cjmcdonald Created on: 2020-04-16 11:30:25
Subsystem: 88. Themed Tk Assigned To: fvogel
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2020-04-25 09:52:22
Resolution: Fixed Closed By: fvogel
    Closed on: 2020-04-25 09:52:22
Description:

If the ttk::treeview "see" sub-command is called immediately after inserting some rows, then the vertical scrollbar doesn't update properly in tk 8.6.10.

This can be seen with the attached test program treescroll.tcl. It creates a treeview widget with 10 visible rows. Click on the buttons to populate the widget with varying numbers of rows. The scrollbar often doesn't update properly. Commenting out the .tree see sub-command in setrows bypasses the problem.

The problem is in the TreeviewSeeCommand routine in ttkTreeview.c.

Checkin [04cf797f1e6c54a0] added a fix for a bug, by updating the scrollbar total size. But it did it in a way which bypasses the ttkScroll.c routines, resulting in an inconsistent state.

The following patch fixes the problem:

*** ttkTreeview.c.8.6.10-orig	2019-10-28 15:52:29.000000000 +0000
--- ttkTreeview.c.8.6.10-patched	2020-04-16 11:12:54.852253770 +0100
***************
*** 2842,2848 ****
  	    TtkRedisplayWidget(&tv->core);
  	}
      }
!     tv->tree.yscroll.total = CountRows(tv->tree.root) - 1;
  
      /* Make sure item is visible:
       */
--- 2842,2852 ----
  	    TtkRedisplayWidget(&tv->core);
  	}
      }
! 
!     TtkScrolled(tv->tree.yscrollHandle,
! 	    tv->tree.yscroll.first,
! 	    tv->tree.yscroll.last,
! 	    CountRows(tv->tree.root) - 1);
  
      /* Make sure item is visible:
       */

User Comments: fvogel added on 2020-04-25 09:52:22:
Merged in core-8-6-branch and trunk.

fvogel added on 2020-04-19 10:44:56:

This must be the original bug, yes, thank you Sherlock!

I observe that the test script provided in that SourceForge ticke behaves as expected at the current tip of our bugfix branch, that is without the call to TtkScrolled. I don't think this changes our conclusions on the present ticket, however identification of the original issues and the fact it is still solved brings additional confidence in our bugfix branch. Thanks!


cjmcdonald added on 2020-04-19 09:28:09:

I've just remembered that themed Tk development was originally done as an extension "tile" on sourceforge. The original bug report behind check-in [04cf797f1e6c54a0] is there, at https://sourceforge.net/p/tktable/bugs/312. Unfortunately sourceforge has since changed all the bug numbers (one of the frustrations which prompted people to leave the platform), but you can see from the conclusion date that it's the associated bug.


fvogel added on 2020-04-19 09:06:51:

Thank you for your feedback. [a4025dc5] removes the superfluous call. All tests still pass, in particular the two I have added in the bugfix branch (treeview-9.2 and treeview-9.3).


cjmcdonald added on 2020-04-18 22:59:49:
Yes, I think you are right and it is OK without that line.

TreeviewSeeCommand calls TtkScrollTo to do the necessary scrolling.  And looking more closely at that, the first thing it does is call TtkUpdateScrollInfo which does an immediate layout callback to update the scroll area limits.  So TtkScrollTo is working with up to date scroll info, without needing a prior call to TtkScrolled.

So all good, thanks.

fvogel added on 2020-04-18 16:54:50:

At first sight I would agree with you but fact is that the following works *without* the call to TtkScrolled():

package require Tk
toplevel .top
ttk::treeview .top.tree -show tree -height 10 -columns {label} \
        -yscrollcommand [list .top.vs set]
ttk::scrollbar .top.vs -command {.top.tree yview}
grid .top.tree -row 0 -column 0 -sticky ns
grid .top.vs -row 0 -column 1 -sticky ns

.top.tree insert {} end -id a -text a
.top.tree insert a  end -id b -text b
.top.tree insert b  end -id c -text c
.top.tree insert c  end -id d -text d
.top.tree insert d  end -id e -text e
for {set i 6} {$i <= 15} {incr i} {
    .top.tree insert {} end -id row$i \
            -values [list [format "Row %2.2d" $i]]
}
update
set before [lindex [.top.vs get] 1]
.top.tree see e
update
set after [lindex [.top.vs get] 1]
expr $after < $before    ; # must return 1 if OK

So why does it work without the call to TtkScrolled() in TreeviewSeeCommand()?

When calling the 'see' command, TreeviewSeeCommand() calls TtkRedisplayWidget() if there are items which state had to be changed to 'open'. This schedules an idle DrawWidget() to redraw the treeview. DrawWidget() basically calls the layoutproc and the displayproc of the widget in turn. The treeview layoutproc (TreeviewDoLayout) before returning makes a call to TtkScrolled(). This must be the reason why it works without the call to TtkScrolled() in TreeviewSeeCommand().

Definitely I believe this call is superfluous. Can you prove I'm wrong by crafting a test script that fails when this call is removed?


cjmcdonald added on 2020-04-18 15:50:01:
Hi, I couldn't find the original bug referenced in the change either. But I think that the update to scroll total (via the call to TtkScrolled now) is required. The issue is if child nodes have to be opened in order to see the requested item, that changes the total rows and without doing the update then limit checks in TtkScrollTo can prevent it moving to the correct row. I remember a time when it was almost impossible to get the "see" sub-command to work reliably after updating the tree - I used to have to resort to "update" and "after idle" calls. It's ok now, and I think that's probably due at least in part to the earlier fix.

fvogel added on 2020-04-18 10:34:33:

Thinking further, actually I think that call to TtkScrolled() in TreeviewSeeCommand() is just not needed.

Test treeview-9.2 still passes without this call, and your test script as well does not show any issue withtout this call. In other words, commit [04cf797f1e6c54a0] apparently created an inconsistency in the scroll state when intending to fix a bug on which I can't put my hands on.

Couldn't we just remove that line, what do you think?


fvogel added on 2020-04-18 10:12:05:

I couldn't find the ticket 3530288 mentioned in the commit log [04cf797f1e6c54a0], therefore I couldn't make sure there is no regression on this one.


fvogel added on 2020-04-18 09:48:20:

Thanks!

I have committed your patch and have crafted a new non-regression testcase (treeview-9.2). This is [ca58681e].


Attachments: