Tk Source Code

View Ticket
Login
Ticket UUID: 031dfcf1ddbad11bffa15bc0c0cad0979d01d86e
Title: ttk::treeview -anchor does not work with column #0
Type: Bug Version: core-8-6-branch
Submitter: fvogel Created on: 2020-07-08 22:03:40
Subsystem: 88. Themed Tk Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2020-08-19 20:30:32
Resolution: Fixed Closed By: fvogel
    Closed on: 2020-08-19 20:30:32
Description:

ttk::treeview -anchor does not work with column #0

package require Tk

ttk::treeview .t -columns [list "Col 1" "Col 2" "Col 3"]
for {set i 1} {$i < 6} {incr i} {
    set val [list]
    for {set j 1} {$j <=3} {incr j} {
        lappend val "Value $j of item $i"
    }
    .t insert {} end -text "Item # $i" -values $val
}

pack .t -expand true -fill both

.t column #1 -anchor e       ; # works
.t column #2 -anchor center  ; # works

.t column #0 -anchor e  ; # no effect --> WRONG!

User Comments: fvogel added on 2020-08-19 20:30:32:

Plan applied through [15c3a464].

Branch then merged into core-8-6-branch ([8e52d751]) and trunk ([99d715aa]).

I'm finally convinced by the removal of the focus ring of selected items in the treeview. tablelist behaves the same, and that's a strong argument for me.


bll added on 2020-07-19 19:47:50:
The scrollbar is a little unusual in that it has three layers, trough, thumb and grip.  Most of the other widgets only have two layers.  I suspect -unit is not
necessary, otherwise some of the other themes would demonstrate drawing issues.

Sounds like a plan.

fvogel added on 2020-07-19 17:54:35:

Thsnks for your testing!

I'm wondering if we should remove the documentation for -expand 1 since it has the same effect as ommitting -side in layouts.

Also, I think we could remove all -sticky news and TTK_FILL_BOTH everywhere, since this is the default anyway.

-border is used in fontchooser.tcl, console.tcl, vistaTheme.tcl. Also its C counterpart, TTK_BORDER is used in a number of files. This must be to ensure that the element it applies to is drawn after its children in order to avoid leaking effects of children over parents. I didn't find a way to demonstrate (by a script) the usefulness of this flag however.

-unit is not used in the Tk/ttk codebase however its C counterpart, TTK_UNIT can be found in ttkWinXPTheme.c (2 occurrences when defining scrollbar layouts). Its effect could perhaps be demonstrated by changing the state of the element that has this flag set (not the root element, the one that has the flag).

Unless you manage to show a difference with -border or -unit, I think I will remove the documentation for those. If some day we decide to remove them it will be much easier if they are undocumented (therefore an internal thing only).


bll added on 2020-07-18 17:52:03:
Looks ok to me.

I was misremembering -border, so it seems to not be used.

I had -expand 1 -unit 1 for scrollbar thumbs,
probably copied from somewhere, but it seems
to have no effect on the thumb or the grip
(grip is set as -sticky {} ).

The plastik theme uses -unit 1 for thumbs, and again I see no difference with 
or without.

If -border and -unit are not useful, should they be documented?  Or be
marked as deprecated?

fvogel added on 2020-07-18 13:16:35:

I have now :

  • better documented the pack-like algorithm for placing elements in ttk layouts
  • removed the superfluous position specifications that result from the above better understanding
  • documented -border and -unit. Why not. They are called in the code, yes, but are they useful? Perhaps.
  • removed the focus ring surrounding the selected item in ttk::treeview. On this one I'm still a bit dubious: isn't the whole point of ttk the platform-native look and feel? If some platforms have this focus ring why would we remove it from the corresponding theme? Anyway. I hope you'll answer future users complaints ;-)

All in all this makes quite some changes. Could you please test with your apps? The only visible change should be the working -column option in column #0 of treeviews, and the removal of the focus ring at that same place. Nothing else should have visibly changed, can you please confirm?


bll added on 2020-07-18 01:30:07:
-border, -unit, -expand are used within layouts.  I don't believe they are used
at the ttk level.  Yes, they need documenting, but there needs to be a new
manual page about the ttk layouts, border, focus rings, backgrounds, foregrounds, selections, layouts, how to adjust a layout, how to build a 
layout, how to use graphics within layouts, etc.

-border and -unit are used in conjunction with graphics in layouts,
they won't go away.

Yes, my suggestion is to do away with the focus ring for treeview.
It simply seems unnecessary since the selection has a color.

In the ideal world, a non-focused treeview widget would have an
-inactiveselectbackground like the 'text' widget does.

fvogel added on 2020-07-17 23:03:06:

I have now documented the -expand layout option in [9e18163730].

However, having investigated this, at the end of the day I think -expand is not needed, as Joe perhaps thought. Indeed, saying -expand 1 is the same as omitting -side. In both cases the entire cavity becomes the allocated parcel.

So documenting -expand is probably not needed after all.

Regarding -border and -unit I would't document them either. Joe had doubts, and I'm not sure they are really needed. -border is used to specify that the element shall be drawn after its children, and -unit propagates the element state to its children.


fvogel added on 2020-07-17 20:37:33:

-border, -unit, -expand: may go away: looks like the original author wasn't sure to keep this. But nowadays, after years of ttk in the core and Joe having left anyway, I believe we should document those, since they are used (and work) in ttk.


fvogel added on 2020-07-17 20:23:52:

Oh yeah, of course there is no problem when the focus ring is entirely removed. Are you suggesting we do that?

If so, let's do it rather here than with ttk::style layout Item {...}.

Apropos ttk::style layout Item {...}: I'm now seeing that -expand for instance is not documented in the ttk::style man page, nor -border and -unit. I can add those information in that page, unless this was undocumented on purpose (e.g. to allow for future removal) - opinion?.

Back to the focus ring topic: what is the rest of the world doing? Do other treeview widgets, e.g. tablelist or treectrl, have a focus ring displayed on the selected row item?

About -focuscolor and rendering of the focus ring on different platforms: this depends on the theme. See FocusElementDraw here and there.


bll added on 2020-07-17 14:22:53:
Ok.  Apparently it is not a windows thing.
I don't know why the color can't be changed on windows, but
removing the focus ring entirely works:

ttk::style layout Item {
    Treeitem.padding -sticky nswe -children {
      Treeitem.indicator -side left -sticky {} 
      Treeitem.image -side left -sticky {} 
      Treeitem.text -side left -sticky {}
    }
}

bll added on 2020-07-17 13:56:59:
Forgot...

% ttk::style layout Item
Treeitem.padding -sticky nswe -children {Treeitem.indicator -side left -sticky {} Treeitem.image -side left -sticky {} Treeitem.focus -side left -sticky {} -children {Treeitem.text -side left -sticky {}}}

That's on Linux.

  ttk::style configure Item \
      -focuscolor [dict get [ttk::style map Treeview -background] selected]

This resolves the issue on Linux, testing on Windows.
Does not resolve the issue on Windows.  From the windows treeview documentation,
I did not see anything to set the focus ring color.

bll added on 2020-07-17 13:18:06:
Testing on windows...

% ttk::style layout Treeview
Treeview.field -sticky nswe -border 1 -children {Treeview.padding -sticky nswe -children {Treeview.treearea -sticky nswe}}

The focus ring is not part of Tk.  It's a windows thing.
Unless the color can be changed, there's probably not much that can be done
about it.

bll added on 2020-07-17 00:45:06:
Ex 3: https://tkdocs.com/images/w_treeview_all.png

Ex 4: https://static.packt-cdn.com/products/9781788622301/graphics/8d5c945d-a738-4d5a-afa2-3c9f85d751cc.png

bll added on 2020-07-17 00:33:18:
Well, those links didn't work.

bll added on 2020-07-17 00:30:58:

Even in that first example, the reddish colored focus ring is not needed, as the selection color indicates which item is selected. I have not tested on windows, but my expectations are like this:

tk tutorial - ex3

Where the selection color covers the entire line. I actually don't know how that first example only has a background that only covers a partial line.

In this example, you can see how the focus ring is just a distraction:

example 4


fvogel added on 2020-07-16 22:06:42:

Hmmm, Im' not so sure that the focus ring no longer fitting the text size can be ignored as a problem. See this example for instance. Granted, there are counterexamples to this as well, where the focus ring is large.

That said, I have now spent some fair amount if time trying to avoid enlarging the focus ring and my conclusion is that it's just not possible without changing the ttk generic layout and drawing code quite a bit.

Indeed, the treeview first places the layout, and then draws it. The sequence of things is as follows:

1. Placement (computation of element sizes and parcels, that is the layout)

  • Ttk_PlaceLayout() --> Ttk_PlaceNodeList()
  • Ttk_PlaceNodeList() first computes the width and height of the node and then computes the parcel (the area that will be allocated to the node in the cavity). This second step calls Ttk_PositionBox, which takes the TTK_PACK_* and TTK_EXPAND flags into account to position the parcel, and finally, through Ttk_StickBox, which takes the TTK_STICK_* flags into account, this parcel is enlarged or not and its coordinates get adjusted.

2. Drawing

  • Using the parcel position and size computed previously, the text element uses the -anchor value to adjust its parcel accordingly. Anchor is used only at the drawing stage not the layout stage.
  • If the parcel is as small as to just contain the text that will be displayed, then -anchor has obviously no effect.

So in our use case we want the focus ring to be the size of the text it encloses, and not more. We also want this group (focus, text) to be movable through the -anchor column option of the treeview.

Therefore no TTK_STICK_* flag must be given to the 'focus' element (otherwise the focus ring would enlarge to the size of the cavity when drawn).

For a 'focus' Ttk Treeitem element with TTK_EXPAND flag only, the parcel allocated in Ttk_PositionBox is originally the entire cavity. That large cavity would leave room so that -anchor would have an effect. But immediately after, still in Ttk_PositionBox, it gets reduced to the minimum size fittibng the enclosed text because no TTK_STICK_* flag was given. And at this point since the parcel is as small as the text size the -anchor has no effect!

I don't know how to change this. Instead I'm now leaning toward documenting that the -anchor option has no effect on column #0.


bll added on 2020-07-08 22:36:02:
I vote as not a problem.
I'm not even sure why treeview has a focus ring, it's not needed.

fvogel added on 2020-07-08 22:14:08:

I have proposed a fix, see [477e403be7].

The -anchor given to column #0 is now propagated to the displayItem, but this is not enough. It is also needed that the parcel for the text expands (TTK_FILL_BOTH), and since this text is enclosed in a 'focus' ring element, this element must also have the same flag.

The -anchor given to column #0 is now honored and the proposed test script works, meaning the bug is fixed.

However the focus ring of the selected item is now the size of the large parcel instead of fitting the text size closely as before the fix. Is this a problem or is it not?