Morten Skaarup Jensen asked in news:comp.lang.tcl : I am writing an editor and would like to know a simple way to find out if the text widget has been edited since the file was loaded so that so that one knows whether or not to save. Bryan Oakley replied:
Override the text widget command, and look for insert and delete subcommands. Here's a quick hack off the top of my head. Don't take this as an example of particularly good coding style, but it does illustrate the point.
To run it through it's paces, just run the following code. Type and/or delete something in the text widget and notice how the save button becomes enabled. Click the save button to simulate saving the data and note how it becomes disabled. Also notice how this works even if you cut or paste data into the widget.
frame .toolbar pack .toolbar -side top -fill x -expand no button .toolbar.save -text "save" -command doSave -bd 1 pack .toolbar.save -side left text .text pack .text -side top -fill both -expand yes rename .text .text_ proc .text {command args} { global textModified if {[string equal $command "insert"] \ || [string equal $command "delete"]} { set textModified 1 } # let the real text widget to all the real work uplevel .text_ $command $args } proc doSave {} { global textModified # pretend we've saved the text... # reset the state set textModified 0 } proc updateUI {args} { global textModified if {$textModified} { .toolbar.save configure -state normal } else { .toolbar.save configure -state disabled } } trace variable textModified w updateUI set textModified 0
Man, I love writing tcl, but sure do miss writing Tk. Tk is just sooooo nice! I literally haven't written this much tk code in a year :-(
See also ANSI color control for a value-added text widget - Arts and crafts of Tcl-Tk programming