• tablelist and columnwidth

    From greg@21:1/5 to All on Thu May 16 23:13:27 2024
    Hello ,

    I'm currently working with the tablelist widget in Tcl/Tk and have a
    question about column widths. I would like to save the original column
    widths and later determine if a column whose original width was 0 was
    resized interactively by the user. With

    pathName columnwidth columnIndex ?-requested|-stretched|-total?

    I can't manage it.
    The aim is to save the status of the table and, after inserting
    individual rows, the view, sorting, selection, etc. to restore.

    a example table:

    package require tablelist
    proc cmd {tbl} {
    puts "[$tbl columnwidth 0]"
    puts "[$tbl columnwidth 0 -stretched]"
    puts "[$tbl columnwidth 0 -total]"
    puts [join [.tbl configure] \n]
    puts [join [.tbl columnconfigure 0] \n]
    }

    tablelist::tablelist .tbl -columns {0 "ID" right 10 "Name" left 0
    "Class" center}

    pack .tbl -fill both -expand true

    .tbl insert end {1 Herbert 3a}
    .tbl insert end {2 Anna 7d}
    .tbl insert end {3 Anna 7c}

    button .btn -text "cmd" -command [list cmd .tbl]
    pack .btn -side top


    mfg
    Gregot

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From greg@21:1/5 to All on Fri May 17 06:28:31 2024
    Am 16.05.24 um 23:13 schrieb greg:
    Hello ,

    I'm currently working with the tablelist widget in Tcl/Tk and have a
    question about column widths. I would like to save the original column
    widths and later determine if a column whose original width was 0 was
    resized interactively by the user. With

    pathName columnwidth columnIndex ?-requested|-stretched|-total?

    I can't manage it.
    The aim is to save the status of the table and, after inserting
    individual rows, the view, sorting, selection, etc. to restore.

    a example table:

    package require tablelist
    proc cmd {tbl} {
        puts "[$tbl columnwidth 0]"
        puts "[$tbl columnwidth 0 -stretched]"
        puts "[$tbl columnwidth 0 -total]"
        puts [join [.tbl configure] \n]
        puts [join [.tbl columnconfigure 0] \n]
    }

    tablelist::tablelist .tbl -columns {0 "ID" right 10 "Name" left 0
    "Class" center}

    pack .tbl -fill both -expand true

    .tbl insert end {1 Herbert 3a}
    .tbl insert end {2 Anna 7d}
    .tbl insert end {3 Anna 7c}

    button .btn -text "cmd" -command [list cmd .tbl]
    pack .btn -side top


    mfg
    Gregot

    And how do I sort the table by knumber or getkeys?

    mfg
    Gregor


    # the procs
    proc save_tablelist_status {tbl} {
    set statusDict [dict create]

    # Save sorting information
    if {[string length [$tbl sortorder]] > 0} {
    dict set statusDict -sortOrder [$tbl sortorder]
    dict set statusDict -sortColumn [$tbl sortcolumn]
    }

    # Save selections
    set selectedIDs [list]
    foreach row [$tbl curselection] {
    lappend selectedIDs k[$tbl getkeys $row]
    }
    dict set statusDict -selectedRows $selectedIDs

    # Save scroll position
    lassign [$tbl xview] x1 x2
    lassign [$tbl yview] y1 y2
    dict set statusDict -xview $x1
    dict set statusDict -yview $y1

    # Save visible rows and columns
    set firstVisibleRow [$tbl index @0,0]
    set lastVisibleRow [$tbl index @0,[winfo height $tbl]]
    dict set statusDict -visibleRows "$firstVisibleRow $lastVisibleRow"

    set firstVisibleColumn [$tbl columnindex @0,0]
    set lastVisibleColumn [$tbl columnindex @0,[winfo width $tbl]]
    dict set statusDict -visibleColumns "$firstVisibleColumn
    $lastVisibleColumn"

    # Save column widths
    set columnWidths [list]
    set columnCount [$tbl columncount]
    for {set i 0} {$i < $columnCount} {incr i} {
    lappend columnWidths [$tbl columnwidth $i -requested]
    }
    dict set statusDict -columnWidths $columnWidths
    puts "save: $statusDict"
    return $statusDict
    }

    proc restore_tablelist_status {tbl statusDict} {
    # Restore sorting information
    if {[dict exists $statusDict -sortColumn] && [dict get $statusDict -sortColumn] != -1} {
    $tbl sortbycolumn [dict get $statusDict -sortColumn] -[dict get $statusDict -sortOrder]
    }

    # Restore selections
    if {[dict exists $statusDict -selectedRows]} {
    foreach row [dict get $statusDict -selectedRows] {
    $tbl selection set $row
    }
    }

    # Restore scroll position
    if {[dict exists $statusDict -xview] && [dict exists $statusDict
    -yview]} {
    $tbl xview moveto [dict get $statusDict -xview]
    $tbl yview moveto [dict get $statusDict -yview]
    }

    # Restore visible rows and columns
    if {[dict exists $statusDict -visibleRows]} {
    set firstVisibleRow [lindex [dict get $statusDict -visibleRows] 0]
    set lastVisibleRow [lindex [dict get $statusDict -visibleRows] 1]
    $tbl see $firstVisibleRow
    $tbl see $lastVisibleRow
    }

    if {[dict exists $statusDict -visibleColumns]} {
    set firstVisibleColumn [lindex [dict get $statusDict
    -visibleColumns] 0]
    set lastVisibleColumn [lindex [dict get $statusDict -visibleColumns] 1]
    $tbl seecolumn $firstVisibleColumn
    $tbl seecolumn $lastVisibleColumn
    }

    # Restore column widths
    if {[dict exists $statusDict -columnWidths]} {
    set columnWidths [dict get $statusDict -columnWidths]
    set columnCount [$tbl columncount]
    for {set i 0} {$i < $columnCount} {incr i} {
    $tbl columnconfigure $i -width -[lindex $columnWidths $i]
    }
    }
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From nemethi@21:1/5 to All on Fri May 17 15:00:06 2024
    Am 16.05.24 um 23:13 schrieb greg:
    Hello ,

    I'm currently working with the tablelist widget in Tcl/Tk and have a
    question about column widths. I would like to save the original column
    widths and later determine if a column whose original width was 0 was
    resized interactively by the user. With

    pathName columnwidth columnIndex ?-requested|-stretched|-total?

    I can't manage it.
    The aim is to save the status of the table and, after inserting
    individual rows, the view, sorting, selection, etc. to restore.

    a example table:

    package require tablelist
    proc cmd {tbl} {
        puts "[$tbl columnwidth 0]"
        puts "[$tbl columnwidth 0 -stretched]"
        puts "[$tbl columnwidth 0 -total]"
        puts [join [.tbl configure] \n]
        puts [join [.tbl columnconfigure 0] \n]
    }

    tablelist::tablelist .tbl -columns {0 "ID" right 10 "Name" left 0
    "Class" center}

    pack .tbl -fill both -expand true

    .tbl insert end {1 Herbert 3a}
    .tbl insert end {2 Anna 7d}
    .tbl insert end {3 Anna 7c}

    button .btn -text "cmd" -command [list cmd .tbl]
    pack .btn -side top


    mfg
    Gregot

    Hi Greg,

    The columnwidth subcommand returns the current width in pixels of the
    column. Example:

    # Save the width of column 2
    set colWidth [.tbl columnwidth 2]

    ...

    # Restore the width of column 2
    .tbl columnconfigure 2 -width -$colWidth

    --
    Csaba Nemethi https://www.nemethi.de mailto:csaba.nemethi@t-online.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From greg@21:1/5 to All on Fri May 17 15:48:18 2024
    Am 17.05.24 um 06:28 schrieb greg:
    Am 16.05.24 um 23:13 schrieb greg:
    Hello ,

    I'm currently working with the tablelist widget in Tcl/Tk and have a
    question about column widths. I would like to save the original column
    widths and later determine if a column whose original width was 0 was
    resized interactively by the user. With

    pathName columnwidth columnIndex ?-requested|-stretched|-total?

    I can't manage it.
    The aim is to save the status of the table and, after inserting
    individual rows, the view, sorting, selection, etc. to restore.

    a example table:

    package require tablelist
    proc cmd {tbl} {
         puts "[$tbl columnwidth 0]"
         puts "[$tbl columnwidth 0 -stretched]"
         puts "[$tbl columnwidth 0 -total]"
         puts [join [.tbl configure] \n]
         puts [join [.tbl columnconfigure 0] \n]
    }

    tablelist::tablelist .tbl -columns {0 "ID" right 10 "Name" left 0
    "Class" center}

    pack .tbl -fill both -expand true

    .tbl insert end {1 Herbert 3a}
    .tbl insert end {2 Anna 7d}
    .tbl insert end {3 Anna 7c}

    button .btn -text "cmd" -command [list cmd .tbl]
    pack .btn -side top


    mfg
    Gregot

    And how do I sort the table by knumber or getkeys?

    mfg
    Gregor



    # sort column with knumber
    # In column $col all values are unique, except that you have a 1:1 to
    knumber
    # $tbl columnconfigure $col -sortmode command -sortcommand [list createSortCommand $tbl $col]
    #
    # Assumption: knumber is the last value in the list
    # possibly critical access to list with knumber with info frame
    # Speed problem with larger lists and may not work in later versions

    proc createSortCommand {tbl col a b} {
    #puts "tbl: $tbl col: $col a: $a b: $b [lindex [info frame -1] 5 6 ]"
    set ak [searchKpos $a [lindex [info frame -1] 5 6 ] $col]
    set bk [searchKpos $b [lindex [info frame -1] 5 6 ] $col]
    return [sortCmd $ak $bk]
    }

    proc searchKpos {val liste col} {
    foreach item $liste {
    set currentValue [lindex $item $col]
    if {$currentValue eq $val} {
    set kvalue [lindex $item end]
    return $kvalue
    }
    }
    return "error"
    }

    proc sortCmd {a b} {
    if {$a < $b} {
    return -1
    } elseif {$a > $b} {
    return 1
    } else {
    return 0
    }
    return 0
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From nemethi@21:1/5 to All on Fri May 17 15:27:20 2024
    Am 17.05.24 um 06:28 schrieb greg:
    Am 16.05.24 um 23:13 schrieb greg:
    Hello ,

    I'm currently working with the tablelist widget in Tcl/Tk and have a
    question about column widths. I would like to save the original column
    widths and later determine if a column whose original width was 0 was
    resized interactively by the user. With

    pathName columnwidth columnIndex ?-requested|-stretched|-total?

    I can't manage it.
    The aim is to save the status of the table and, after inserting
    individual rows, the view, sorting, selection, etc. to restore.

    a example table:

    package require tablelist
    proc cmd {tbl} {
         puts "[$tbl columnwidth 0]"
         puts "[$tbl columnwidth 0 -stretched]"
         puts "[$tbl columnwidth 0 -total]"
         puts [join [.tbl configure] \n]
         puts [join [.tbl columnconfigure 0] \n]
    }

    tablelist::tablelist .tbl -columns {0 "ID" right 10 "Name" left 0
    "Class" center}

    pack .tbl -fill both -expand true

    .tbl insert end {1 Herbert 3a}
    .tbl insert end {2 Anna 7d}
    .tbl insert end {3 Anna 7c}

    button .btn -text "cmd" -command [list cmd .tbl]
    pack .btn -side top


    mfg
    Gregot

    And how do I sort the table by knumber or getkeys?

    mfg
    Gregor


    # the procs
    proc save_tablelist_status {tbl} {
      set statusDict [dict create]

      # Save sorting information
      if {[string length [$tbl sortorder]] > 0} {
        dict set statusDict -sortOrder [$tbl sortorder]
        dict set statusDict -sortColumn [$tbl sortcolumn]
      }

      # Save selections
      set selectedIDs [list]
      foreach row [$tbl curselection] {
        lappend selectedIDs k[$tbl getkeys $row]
      }
      dict set statusDict -selectedRows $selectedIDs

      # Save scroll position
      lassign [$tbl xview] x1 x2
      lassign [$tbl yview] y1 y2
      dict set statusDict -xview $x1
      dict set statusDict -yview $y1

      # Save visible rows and columns
      set firstVisibleRow [$tbl index @0,0]
      set lastVisibleRow [$tbl index @0,[winfo height $tbl]]
      dict set statusDict -visibleRows "$firstVisibleRow $lastVisibleRow"

      set firstVisibleColumn [$tbl columnindex @0,0]
      set lastVisibleColumn [$tbl columnindex @0,[winfo width $tbl]]
      dict set statusDict -visibleColumns "$firstVisibleColumn $lastVisibleColumn"

      # Save column widths
      set columnWidths [list]
      set columnCount [$tbl columncount]
      for {set i 0} {$i < $columnCount} {incr i} {
        lappend columnWidths [$tbl columnwidth $i -requested]
      }
      dict set statusDict -columnWidths $columnWidths
      puts "save: $statusDict"
      return $statusDict
    }

    proc restore_tablelist_status {tbl statusDict} {
      # Restore sorting information
      if {[dict exists $statusDict -sortColumn] && [dict get $statusDict -sortColumn] != -1} {
        $tbl sortbycolumn [dict get $statusDict -sortColumn] -[dict get $statusDict -sortOrder]
      }

      # Restore selections
      if {[dict exists $statusDict -selectedRows]} {
        foreach row [dict get $statusDict -selectedRows] {
          $tbl selection set $row
        }
      }

      # Restore scroll position
      if {[dict exists $statusDict -xview] && [dict exists $statusDict
    -yview]} {
        $tbl xview moveto [dict get $statusDict -xview]
        $tbl yview moveto [dict get $statusDict -yview]
      }

      # Restore visible rows and columns
      if {[dict exists $statusDict -visibleRows]} {
        set firstVisibleRow [lindex [dict get $statusDict -visibleRows] 0]
        set lastVisibleRow [lindex [dict get $statusDict -visibleRows] 1]
        $tbl see $firstVisibleRow
        $tbl see $lastVisibleRow
      }

      if {[dict exists $statusDict -visibleColumns]} {
        set firstVisibleColumn [lindex [dict get $statusDict
    -visibleColumns] 0]
        set lastVisibleColumn [lindex [dict get $statusDict
    -visibleColumns] 1]
        $tbl seecolumn $firstVisibleColumn
        $tbl seecolumn $lastVisibleColumn
      }

      # Restore column widths
      if {[dict exists $statusDict -columnWidths]} {
        set columnWidths [dict get $statusDict -columnWidths]
        set columnCount [$tbl columncount]
        for {set i 0} {$i < $columnCount} {incr i} {
          $tbl columnconfigure $i -width -[lindex $columnWidths $i]
        }
      }
    }


    You will have to set the widget's -sortcommand opt to a proc that
    compares the two items passed to it as arguments. In order to be able
    to use the corresponding keys as comparison criterion, you should save
    each item's key in a dedicated hidden column.

    --
    Csaba Nemethi https://www.nemethi.de mailto:csaba.nemethi@t-online.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From greg@21:1/5 to All on Sat May 18 02:06:33 2024
    Am 17.05.24 um 15:00 schrieb nemethi:
    Am 16.05.24 um 23:13 schrieb greg:
    Hello ,

    I'm currently working with the tablelist widget in Tcl/Tk and have a
    question about column widths. I would like to save the original column
    widths and later determine if a column whose original width was 0 was
    resized interactively by the user. With

    pathName columnwidth columnIndex ?-requested|-stretched|-total?

    ...
    mfg
    Gregot

    Hi Greg,

    The columnwidth subcommand returns the current width in pixels of the column.  Example:

    # Save the width of column 2
    set colWidth [.tbl columnwidth 2]

    ...

    # Restore the width of column 2
    .tbl columnconfigure 2 -width -$colWidth

    Thanks.
    My question was inaccurate.
    With
    $tbl configure -columns
    I get the original settings when creating the table.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)