Setting a dirty flag for a file

Peter Brigham MD pmbrig at gmail.com
Thu Jul 21 17:08:52 EDT 2011


On Jul 21, 2011, at 4:02 PM, Mark Schonewille wrote:

> Hi Charles,
> 
> I read your e-mail again. I understand that you want to know whether one or more fields have changed, not whether a file is open.
> 
> Usually, I generate an md5Digest and save that:
> 
> on closeField
>  makeDigest 
> end closeField
> 
> on makeDigest
>  put empty into myDigest
>  repeat with x = 1 to number of fields
>    put md5Digest(fld x & myDigest) into myDigest
>  end repeat
>  set the cDigest of this stack to myDigest
> end makeDigest
> 
> function dataChanged
>  put empty into myDigest
>  repeat with x = 1 to number of fields
>    put md5Digest(fld x & myDigest) into myDigest
>  end repeat
>  return (the cDigest of this stack is myDigest)
> end dataChanged
> 
> You just will have to figure out what is a smart event to check the digest: closing a field, closing a window, opening a window, refreshing the file menu, etc.
> 
> When you close the window, you might want to do this:
> 
> on closeStackRequest
>  if dataChanged then
>    answer "Do you want?" with "Don't Save" or "OK" or "No"
>    if it containt "Don't" then
>      exit closeStackRequest
>    else if it is "No" then
>      pass closeStackRequest
>    else
>      // do your saving stuff here
>      // return true if the file was saved
>      if the result is true then
>        pass closeStackRequest
>      end if
>    end if
>  end if
> end closeStackRequest


I use a frontscript -- wholesale is better than retail:

on closefield
   setDirty
   pass closefield
end closefield

on setDirty tf
   if tf = empty then put true into tf
   put the version into v
   replace "." with empty in v
   if v < 453 then exit setDirty
   -- version must be 4.5.3 or higher,
   -- or you just create a new customprop
   set the modifiedMark of stack "myStack" to tf
end setDirty

If you want to exclude certain fields, use a customprop to mark fields that shouldn't trigger setDirty when edited:

   set the the dontFlagDirty of fld "excludedFld" to true

Then in the closeField handler in the frontscript, insert as the first line

   if the dontFlagDirty of the target <> true then pass closefield

Changes other than field content that should be saved (radiobuttons or checkboxes, etc) can be handled, eg, with a mouseup handler in the frontscript, and suitable checks on the target. (Pass the mouseup!)

I also do:

on preopenstack
   set dirty false
   ... <rest of your initialization here> ...
   ...
end preopenstack

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the use-livecode mailing list