Tk Source Code

View Ticket
Login
Ticket UUID: b094cb4aa98e57fac8d9b6545188ecdb0ce35d8b
Title: ttk::treeview steadily enlarges width when repeatedly configuring -show
Type: Bug Version: core-8-6-branch
Submitter: fvogel Created on: 2019-12-08 09:49:00
Subsystem: 88. Themed Tk Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2019-12-14 20:10:22
Resolution: Fixed Closed By: fvogel
    Closed on: 2019-12-14 20:10:22
Description:

ttk::treeview steadily enlarges width when repeatedly configuring -show

This happens on Windows with vista and xpnative themes, but not with winnative, clam, alt, default or classic themes.

It does not happen on macOS with any available theme (aqua, clam, alt, default, classic). I didn't try on Linux.

A minimal demo script:

package require Tk
pack [ttk::treeview .t]
update; winfo width .t
.t configure -show {tree}
update; winfo width .t
.t configure -show {tree}
update; winfo width .t

The output of this for me on Windows Vista with "vista" theme is: 203 204 205

So each time the widget gets configured to what it already is configured (-show {tree}) its width increases by one pixel.

A less minimal demo script exercising the available themes:

package require Tk

pack [ttk::treeview .t]
update; winfo width .t

foreach th [ttk::style theme names] {
    puts $th
    ttk::style theme use $th ; update

    set ref [winfo width .t]
    for {set i 1} {$i < 200} {incr i} {
        .t configure -show {tree}
        update
    }
    if {$ref ne [winfo width .t]} {
        puts "    Width changed!  --> $ref ne [winfo width .t]"
    }

    after 2000
}

With vista or xpnative themes only, the widget enlarges its width every time it is configured with what it already has. The treeview width grows ~200 pixels when configuring it 200 times with -show {tree}.

User Comments: fvogel added on 2019-12-14 20:10:22:
Merged to core-8-6-branch and trunk.

fvogel added on 2019-12-08 21:44:40:

With the minimal script provided, analysis shows that:

  • The tree width enlarges when calling .t configure -show {tree} because Stretch() is called with n=1, which enlarges column #0 by one pixel.
  • Climbing up the call stack, DistributeWidth() also has n=1, and ResizeColumns() calls it with PickupSlack(tv, delta), delta being 1 pixel. So ResizeColumns() is called with newWidth being one pixel more than the current width of the treeview.
  • Further climbing up the call stack, ResizeColumns() is called by TreeviewDoLayout(), which is called as the layoutProc in DrawWidget() called at idle time.

So the treeview width increase can be noticed when the widget is redrawn, at idle time, however this redraw only makes the problem apparent, it does not create it since it was already here, just not visible.

This can be confirmed by the following code showing that the default width of column #0, which should be 200 pixels (this is set in InitColumn()) finds itself increased of 1 pixel after the display update has been performed:

package require Tk
pack [ttk::treeview .t -show {tree}]
.t column #0 -width    ; # returns the expected 200 pixels
update
.t column #0 -width    ; # returns 201 but it should be 200 !!

What is happening is as follows:

  • The user configures the widget to the (already configured) value of -show.
  • TtkWidgetConfigureCommand() is called, and calls the configureProc TreeviewConfigure() - which code is fully OK, no problem here - and then calls SizeChanged().
  • SizeChanged() runs the sizeProc TreeviewSize() to get the new reqwidth/reqheight to provide to Tk_GeometryRequest(). TreeviewSize() returns a wrong width for the treeview, the value is the current width + 1. This must be wrong, because the widget geometry must not change when configuring it to an unchanged -show content. And then Tk_GeometryRequest() will resize the treeview window accordingly, making it one pixel wider in the horizontal direction.

So the question is why TreeviewSize() does return a width that is larger than the current width of the treeview. This is not entirely trivial, but here is what I have found:

  • TreeviewSize() first calls Ttk_LayoutSize() to get padWidth and padHeight for the treeview. Both returned values are 3, and here is where it goes wrong. Indeed, the widthPtr computed just after is padWidth + TreeWidth(tv), that is 3 + the sum of the display columns widths. This is one pixel too much, because in reality the padding is 1 pixel in each direction, which makes 2 in total for each direction.
  • Now the call chain to compute the layout size is Ttk_LayoutSize() -> Ttk_NodeListSize() -> Ttk_NodeSize() for the background element (size is 0x0), then recursively Ttk_NodeListSize() -> Ttk_NodeSize() for the Treeview.field, which is composed of the Treeview.padding that encloses the Treeview.treearea. For the Treeview.field, Ttk_NodeSize() returns a (wrong) size of 3x3 pixels.
  • For the Treeview.field, Ttk_NodeSize() gets the size by calling Ttk_ElementSize(), which in turn runs eclass->specPtr->size(), in this case this is GenericElementSize() in ttkWinXPTheme.c (this is the first function in the above list that is in ttkWinXPTheme.c, so far the code was common to all themes).
  • GenericElementSize() gets the size by calling elementData->procs->GetThemePartSize() which provide the returned 3x3 size. Now notice the IGNORE_THEMESIZE in that function GenericElementSize(). This flag is not set for the Treeview.field element, but it is documented a little bit at the beginning of ttkWinXPTheme.c. So it turns out the WIN32 API function GetThemePartSize() can give wrong results for some elements, and for the parts for which it happens, this can be avoided by setting this IGNORE_THEMESIZE flag which will skip the call to GetThemePartSize() in GenericElementSize().

The fix therefore consists in setting this IGNORE_THEMESIZE flag when defining the Treeview.field element in the ElementInfoTable of ttkWinXPTheme. This is [8ee62608cc].