How to store text for undo? Strings too short.

BlitzMax Forums/BlitzMax Programming/How to store text for undo? Strings too short.

JoshK(Posted 2007) [#1]
My undo routines were working great until I started editing some large text files. It looks like strings can only store so many characters before they get cut off. So how can I quickly transfer the contents of a TextArea gadget into some kind of storage? It has to be fast, because it happens every time the user hits a key.


H&K(Posted 2007) [#2]
Strings (plural)


JoshK(Posted 2007) [#3]
It seems the problem was actually that I was using SetGadgetText instead of SetTextAreaText, which cuts off the text.


klepto2(Posted 2007) [#4]
As far as I know you only store the keyhit and the position and this stuff. In short words: Store only the task and if you
undo\redo something you have to do the opposite of this task.
It is some kind of 'motion capturing' .

If you would save the whole text then you have a lot of memory usage which is not a good thing.
EG: you test is around 10000 chars then
you have to save 10000 bytes + the additional info for each char (color,format, etc.) only with the chars the memusage is increased enormous if you have just 500 (1000 at all) undo and redo steps (if I'm correct thats around 9mb).

Use state constants instead: eg: REMOVECHAR,ADDCHAR and save the position and other needed info of the task in a TList or similar thing.


JoshK(Posted 2007) [#5]
How are you supposed to get what was typed if EventData() does not return anything on a textarea gadgetaction event?


klepto2(Posted 2007) [#6]
thats true this very difficult to explain, but maybe you could take a look to the maxide.bmx espacially the types :
TDiff and TOpenCode (Methods Redo\Undo\updatecode\calcdiff)

It works that only the difference between the previous and currentstate is saved and not the whole text.

PS: my previous post was not directly related to the textarea gadget, sorry. (I'm currently working on my own textarea gadget with predefined undo\redo functions etc. so I was a bit out of this context.


JoshK(Posted 2007) [#7]
I got the Windows default undo behavior to work with textareas. I can't seem to get redo to work, though.

First set the number of undo levels allowed:
SendMessageA(QueryGadget(gadget,QUERY_HWND),1106,100,0)

Then when you want to undo, use this:
hwnd=QueryGadget(gadget,QUERY_HWND)
Repeat
If SendMessageA(hwnd,198,0,0)'em_canundo
undoname=SendMessageA(hwnd,1110,0,0)'em_getundoname
SendMessageA(hwnd,199,0,0)'em_undo
If undoname Exit
Else
Exit
EndIf
Forever


*(Posted 2007) [#8]
Couldnt you just get the text of a text area, store that somewhere and when they undo it restore the old text. Redo is just restoring the text that was there.


FlameDuck(Posted 2007) [#9]
By using the "command" software design pattern.


JoshK(Posted 2007) [#10]
That's actually what I implemented, albeit without so much programmer architecture BS.


Dreamora(Posted 2007) [#11]
programming paradigms are no BS, they might look complex and hard at first, but you will learn their Pro very fast. Because their power and long living effect makes the little larger work to understand theory before implementing strange stuff really unimportant.
Paradigms thought are mainly for people that want to create bugfree stuff upfront instead of running after them afterwards with trial and error fixing methods :)