<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://tcl.activestate.com/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu Feb 09 07:41:31 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='19'>
<header><title>Add a Text Changed Flag to Tk&apos;s Text Widget</title><author address="mailto:mckay@eecs.umich.edu">Neil McKay</author><status type='project' state='final' tclversion="8.4a2" vote='after'>$Revision: 1.6 $</status><history></history><created day='3' month='jan' year='2001' /><obsoleted tip='26'/></header>
<abstract>This TIP adds a <emph style="italic">text changed</emph> flag to the Tk text widget. The flag would initially be reset, but would be set whenever the contents of the text widget changes.</abstract>
<body><section title="Rationale">
<para>When creating a text editor, it is often useful to know when the contents of the edit buffer have changed, e.g. in order to ask the user whether or not to save changes on exit. It is possible to create key bindings in Tk&apos;s text widget that will set a flag whenever the user changes the widget&apos;s contents; however, this is awkward, and it still requires that the programmer set the flag whenever text is changed programmatically. A better solution is to include a <emph style="italic">text changed</emph> flag in the code for the text widget itself; this can be accomplished with a relatively small amount of code.</para>
</section>
<section title="Flag Behavior">
<para>The <emph style="italic">text changed</emph> flag should behave as follows:</para>
<itemize><item.i><para>It should be reset when the text widget is created</para></item.i><item.i><para>It should be set whenever characters are inserted into or deleted from the widget</para></item.i><item.i><para>It must be resettable programmatically via a Tcl command</para></item.i></itemize>
</section>
<section title="Reference Implementation">
<para>At the Tcl level, one possible implementation is to add a <emph style="italic">changed</emph> widget command to the text widget. One possible syntax for this command is:</para>
<verbatim><vline encoding='base64'>ICAgIC50eHQgY2hhbmdlZCA/Ym9vbGVhbj8=</vline></verbatim>
<para>where .txt is a text widget. With no <emph style="italic">boolean</emph> argument, the command returns the state of the text-changed flag; with an argument, it sets the state of the text-changed flag to the value of the argument.</para>
</section>
<section title="Example">
<para>A typical sequence of commands in a text editor would be</para>
<enumerate><item.e index='1'><para>Create a text widget</para></item.e><item.e index='2'><para>Read a file and put its contents into the text widget</para></item.e><item.e index='3'><para>Mark the text as unchanged</para></item.e><item.e index='4'><para>Edit the text</para></item.e><item.e index='5'><para>Write the text out, if it has changed.</para></item.e></enumerate>
<para>This could be accomplished by the following Tcl code fragment:</para>
<verbatim><vline encoding='base64'>IGdyaWQgW2J1dHRvbiAuYiAtdGV4dCBRdWl0IC1jb21tYW5kIEVuZEVkaXRd</vline><vline encoding='base64'>IGdyaWQgW3RleHQgLnRd</vline><vline encoding='base64'></vline><vline encoding='base64'>IHByb2MgRW5kRWRpdCB7fSB7</vline><vline encoding='base64'>ICAgICBpZiB7Wy50IGNoYW5nZWRdfSB7</vline><vline encoding='base64'>ICAgICAgICAgc2V0IHJlc3VsdCBbdGtfbWVzc2FnZUJveCAtdHlwZSB5ZXNubyAtbWVzc2FnZSAiU2F2ZSBjaGFuZ2VzPyJd</vline><vline encoding='base64'>ICAgICAgICAgaWYge1tzdHJpbmcgY29tcGFyZSAkcmVzdWx0ICJ5ZXMiXSA9PSAwfSB7</vline><vline encoding='base64'>ICAgICAgICAgICAgIHNldCBmaCBbb3BlbiAkZmlsZU5hbWUgInciXQ==</vline><vline encoding='base64'>ICAgICAgICAgICAgIHB1dHMgLW5vbmV3bGluZSAkZmggWy50IGdldCAxLjAgZW5kLTFjXQ==</vline><vline encoding='base64'>ICAgICAgICAgICAgIGNsb3NlICRmaA==</vline><vline encoding='base64'>ICAgICAgICAgfQ==</vline><vline encoding='base64'>ICAgICB9</vline><vline encoding='base64'>ICAgICBleGl0</vline><vline encoding='base64'>IH0=</vline><vline encoding='base64'></vline><vline encoding='base64'>IHNldCBmaCBbb3BlbiAkZmlsZU5hbWUgInIiXQ==</vline><vline encoding='base64'>IC50IGluc2VydCBlbmQgW3JlYWQgJGZoXQ==</vline><vline encoding='base64'>IGNsb3NlICRmaA==</vline><vline encoding='base64'>IC50IGNoYW5nZWQgZmFsc2U=</vline></verbatim>
</section>
<section title="Copyright">
<para>This document is in the public domain.</para>
</section>
<section title="Patch">
<para>The <emph style="italic">changed</emph> text widget command, as described above, may be added to Tk8.4a2 by applying the patch at <url ref="http://www.cs.man.ac.uk/fellowsd-bin/TIP/19.patch"/></para>
</section>
</body></TIP>

