Tk Source Code

View Ticket
Login
Ticket UUID: 5e4e44a93b16e06edba03ef9dd350758e210be8b
Title: ttk::panedwindow does not normalize to "horizontal" or "vertical", chokes on drag
Type: Bug Version: 8.6
Submitter: xolodho Created on: 2022-10-24 05:03:43
Subsystem: 21. [panedwindow] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2022-12-07 17:48:06
Resolution: Fixed Closed By: oehhar
    Closed on: 2022-12-07 17:48:06
Description:
first of all: this bug affects ttk::panedwindow, not classic
panedwindow.

to reproduce, try this script in a clean wish session:

    pack [tk::panedwindow .pan0 -orient hor]
    pack [ttk::panedwindow .pan1 -orient hor]
    foreach pan {.pan0 .pan1} {
      $pan add [button $pan.b0 -text "left"]
      $pan add [button $pan.b1 -text "right"]
      puts "-orient => [$pan cget -orient]"
    }
    puts "try dragging the sash of each panedwindow ..."


you get a runtime error when dragging the ttk panedwindow, but the tk
panedwindow is ok. the error reads:

              can't read "delta": no such variable

----------------------------------
what happened ?


ttk::panedwindow admits abbreviations for the -orient option, e.g. "hor"
instead of "horizontal", but then it does not
expand/normalize/canonicalize the abbreviation to the proper full name
(e.g. "hor" => "horizontal"). And because of this, it later fails when
attempting to drag the sash, because there is a "switch" sentence (in
proc ttk::panedwindow::Drag) that matches literally the strings
"vertical" or "horizontal", not "h" "ho" "hor" etc, here:

    proc ttk::panedwindow::Drag {w x y} {
        variable State
        if {!$State(pressed)} { return }
        switch -- [$w cget -orient] {
          horizontal  { set delta [expr {$x - $State(pressX)}] }
          vertical  { set delta [expr {$y - $State(pressY)}] }
        }
        $w sashpos $State(sash) [expr {$State(sashPos) + $delta}]
    }

now, because `cget` returned "hor", the switch matches nothing, so the
`delta` variable remains undefined, and you get a runtime error while
trying to drag the sash, because expr tries to read the undefined
`delta`.

as the example showed on stdout, if you `cget -orient` after having
specified `-orient hor`, you get back "hor" literally, instead of
"horizontal" as the classic panedwindow does, so these two widgets
behave differently here. ttk::panedwindow should expand abbreviations to
the corresponding full name like the classic panedwindow does, so that
you always get consistent results via `cget` and avoid gotchas like that
in the Drag procedure.
User Comments: oehhar added on 2022-12-07 17:48:06:

Thank you ! I think, a documented general normalisation would be a great step. This could be a TIP for Tk 9.0.

I also see regression issues. It is a script-level visible change.

Other opinions welcome.

Thank you and take care, Harald


jan.nijtmans added on 2022-12-07 17:01:35:

Second part fixed [95763fe6e4da5ab8|here], as requested by Koen Danckaert and Harald Oehlmann.

I also experimented doing the same with booleans (yes|on -> 1 and no|off -> 0) and integers (0xff -> 255), but I am doubting that would be a good idea. So, I leave it with only those 4.

This part is only done with Tk 8.7, as there still is a tiny possibility of regression. For 8.6 I prefer to keep it as it is now (sorry!) just to lower the possibility of unexpected problems in a patch-release.


oehhar added on 2022-12-02 17:05:00:

For me, it is prefered, that "abreviations" are always expanded to the full value.

Imagine a package writes:

scrollbar .s -orient hor

, and the program using this extension finds:

% .s cget -orient
hor

it has to do the abreviation logic on its own. This is specially dirty, if, later on, the abreviation is not valid any more, as there are now multiple options with the same stem.

For me, all abreviations should go away, as there is alwyas an issue build in.

I like abreviations only when typing on the Android console, where each key is a pain ;-).

My 2 cents, Harald


jan.nijtmans added on 2022-12-02 15:03:38:

[e556f83710af095c|here] is another possible solution, equivalent to Koen's solution, only it works for _all_ usages of TCL_CONFIG_STRING_TABLE, TCL_CONFIG_RELIEF, TCL_CONFIG_JUSTIFY and TCL_CONFIG_ANCHOR. It makes sure that whatever we are 'configure'ing, the option value is always expanded to its fully form.

How about this? Advantage: the individual widgets don't have to do anything special any more.


danckaert added on 2022-11-29 11:46:15:

I also encountered this (just before I saw this ticket) and fixed it in the following way at my side:

diff --git a/generic/ttk/ttkPanedwindow.c b/generic/ttk/ttkPanedwindow.c
index 4fe015ae4..4ede9a87d 100644
--- a/generic/ttk/ttkPanedwindow.c
+++ b/generic/ttk/ttkPanedwindow.c
@@ -57,7 +57,6 @@
 typedef struct {
-    Tcl_Obj    *orientObj;
     int        orient;
     int        width;
     int        height;
@@ -77,7 +76,7 @@ typedef struct {
  */
 static Tk_OptionSpec PanedOptionSpecs[] = {
     {TK_OPTION_STRING_TABLE, "-orient", "orient", "Orient", "vertical",
-       Tk_Offset(Paned,paned.orientObj), Tk_Offset(Paned,paned.orient),
+       -1, Tk_Offset(Paned,paned.orient),
        0, (void *)ttkOrientStrings, READONLY_OPTION|STYLE_CHANGED },
     {TK_OPTION_INT, "-width", "width", "Width", "0",
        -1,Tk_Offset(Paned,paned.width),


jan.nijtmans added on 2022-10-29 14:12:26:

No need to be sorry ;-) I see your point.

But ... since ttk was designed this way, it's not a good idea to change this in a patch-release. It could be changed in 8.7 or 9.0, I'll experiment with this a little.


xolodho added on 2022-10-28 22:27:25:
I'm sorry, but I think the current fix doesn't address the main issue I
wanted to report, and instead touches code that was fine. Let me explain
my point of view ...

I think the correct fix would be to canonicalize any abbreviation to a
standard form, then there is no need to modify the switch in the
ttk::panedwindow::Drag procedure. Tk's panedwindow already gets this right
(and Ttk's panedwindow doesn't!), and *that* was my point in the bug
report. The current fix does not address this issue, and instead lets the
abbreviation stay literally unchanged, and then has to resort to check
downstream its abbreviated form. The new code is even less clear than
before.

So what I think should be done is, abbreviations should be processed just
once at widget creation time (when parsing options), and then become
standard form once and for all. Tk's classic panedwindow and scrollbar
already do this, so if you specify "-orient hor", then with cget you get
back "horizontal", not "hor" (or whatever abbreviation you happened to
type).

So, when I specify "-orient hor" I want Tk to expand that for me at widget
creation time. Then if two panedwindow widgets were specified with
"-orient hor" and "-orient horiz" respectively (for whatever reason, it
doesn't matter), and I call `cget -orient` on each, I can compare the
result for equality literally without having to deal with abbreviation
patterns. But currently, the results for `cget -orient` would be
different, and the Tk widget user should not have to deal with
abbreviations, that's something Tk should hide away.

We should get a uniform option value after construction. This is more
elegant and robust. Otherwise you have to be prepared for parsing
abbreviations all the time, which I think is not nice, it complicates the
API.

This behavior discrepancy seems to happen with other Tk vs Ttk widgets.
Try the following code in Wish to see the difference in Tk scrollbar vs
Ttk scrollbar:

    puts "with Tk scrollbar"
    for {set i 0} {$i < 4} {incr i} {
      set spec [string range horizontal 0 $i]
      set w [tk::scrollbar .sTk$i -orient $spec]
      puts "\t-orient $spec => [$w cget -orient]"
    }
    puts "with Ttk scrollbar"
    for {set i 0} {$i < 4} {incr i} {
      set spec [string range horizontal 0 $i]
      set w [ttk::scrollbar .sTtk$i -orient $spec]
      puts "\t-orient $spec => [$w cget -orient]"
    }
    exit

I get the following output, clearly showing the difference in behavior (Tk
does normalize (good), Ttk doesn't (bad)):

    with Tk scrollbar
            -orient h => horizontal
            -orient ho => horizontal
            -orient hor => horizontal
            -orient hori => horizontal
    with Ttk scrollbar
            -orient h => h
            -orient ho => ho
            -orient hor => hor
            -orient hori => hori

And that's the issue I wanted to report. The ttk::panedwindow::Drag
misbehavior is merely a minor symptom of this deeper misdesign, and
actually ttk::panedwindow::Drag should not be modified at all and be left
as it was, the real bug is upstream: fix option parsing.

my two cents ... :)

jan.nijtmans added on 2022-10-28 14:48:00:

Fixed [88f19868d65df32a|here]. Thanks!


fvogel added on 2022-10-25 20:36:11:

I think we should fix this by letting ttk::panedwindow fully support any abbreviation for valid orientations. See [347a8882f5].