• extra key bindings for a text widget that uses ntext bindings

    From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Fri Oct 10 09:41:52 2025
    From Newsgroup: comp.lang.tcl

    I am using Tcl/Tk 9. I have a text widget as follows:

    oo::class create TextEdit {
    variable FrameName
    variable Text
    }

    oo::define TextEdit constructor parent {
    set FrameName tf#[string range [clock clicks] end-8 end] ;# unique
    ttk::frame $parent.$FrameName
    set Text [text $parent.$FrameName.txt -undo true -wrap word]
    bindtags $Text {$Text Ntext . all}
    # bind $Text doesn't work; nor does bind $parent.$FrameName or $parent
    bind . <Control-BackSpace> [callback on_ctrl_bs]
    # ui::scrollize $parent.$FrameName txt vertical
    }

    oo::define TextEdit method unknown {method_name args} {
    $Text $method_name {*}$args
    }

    oo::define TextEdit method on_ctrl_bs {} {
    if {[focus] eq $Text} {
    set x [$Text index "insert wordstart"]
    set y [$Text index "insert wordend"]
    if {[$Text get $x $y] eq " "} {
    set y $x
    set x [$Text index "[$Text index "insert -1 char"] wordstart"]
    }
    set z [$Text index "$x -1 char"]
    if {[$Text get $z $x] eq " "} {
    set x $z
    }
    $Text delete $x $y
    }
    }

    I want my text widget to delete the word to the left of the text
    cursor (the 'insert' mark) when the user presses Control-BackSpace.

    The code shown above does this correctly.

    However, I am having to bind the on_ctrl_bs callback to the . (root)
    window, since when I try binding it to the text widget or the frame
    containing the text widget or the frame containing the frame, the
    callback is never called.

    Is it possible to bind directly to the text widget in this case?
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Fri Oct 10 09:48:48 2025
    From Newsgroup: comp.lang.tcl

    Actually, my on_ctrl_bs method doesn't work correctly,
    but that is besides the point of my question!
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Fri Oct 10 14:04:08 2025
    From Newsgroup: comp.lang.tcl

    * Mark Summerfield <m.n.summerfield@gmail.com>
    | # bind $Text doesn't work; nor does bind $parent.$FrameName or $parent
    | bind . <Control-BackSpace> [callback on_ctrl_bs]

    It does work for me with a plain text widget *if* the text widget has
    the focus (TAB into the text, or call 'focus'):

    pack [text .t]
    bind .t <Control-BackSpace> { puts "Ctrl-backspace in %W"}
    Hit C-backspace: nothing

    focus .t
    Hit C-backspace, message is printed

    HTH
    R'
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Emiliano@emiliano@example.invalid to comp.lang.tcl on Fri Oct 10 12:45:44 2025
    From Newsgroup: comp.lang.tcl

    On Fri, 10 Oct 2025 09:41:52 -0000 (UTC)
    Mark Summerfield <m.n.summerfield@gmail.com> wrote:

    [...]
    bindtags $Text {$Text Ntext . all}
    # bind $Text doesn't work; nor does bind $parent.$FrameName or $parent

    The correct line is

    bindtags $Text [list $Text Ntext [winfo toplevel $Text] all]

    Regards
    --
    Emiliano <emiliano@example.invalid>
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Fri Oct 10 17:31:25 2025
    From Newsgroup: comp.lang.tcl

    On Fri, 10 Oct 2025 12:45:44 -0300, Emiliano wrote:

    On Fri, 10 Oct 2025 09:41:52 -0000 (UTC)
    Mark Summerfield <m.n.summerfield@gmail.com> wrote:

    [...]
    bindtags $Text {$Text Ntext . all}
    # bind $Text doesn't work; nor does bind $parent.$FrameName or $parent

    The correct line is

    bindtags $Text [list $Text Ntext [winfo toplevel $Text] all]

    Regards

    Thank you for that code, it works great!

    Here're the changes I made (including a Control-BackSpace which I now
    believe works):

    oo::define TextEdit constructor parent {

    bindtags $Text [list $Text Ntext [winfo toplevel $Text] all]
    bind $Text <Control-BackSpace> [callback on_ctrl_bs]


    oo::define TextEdit method on_ctrl_bs {} {
    set i [$Text index "insert -1 char"]
    set x [$Text index "$i wordstart"]
    set y [$Text index "$x wordend"]
    $Text delete $x $y
    }
    --- Synchronet 3.21a-Linux NewsLink 1.2