Recent

Author Topic: Undo function (TEdit)  (Read 1840 times)

hedgehog

  • New Member
  • *
  • Posts: 48
Undo function (TEdit)
« on: February 24, 2024, 12:40:16 pm »
Hi, all!

Does anyone know how the Undo function of the TEdit component should work?

I'm developing my own small component, and I can't understand the algorithm for how this function works.

For example, why doesn't this work:
<Ctrl+A>, <Delete>, <Ctrl-Z>
(Laz2.2.6 + Windows 10)

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: Undo function (TEdit)
« Reply #1 on: February 24, 2024, 01:10:38 pm »
I do not think, this undo is part of the component.
This is part of Windows.

If you like an undo in an Edit, you probably have to write it.

hedgehog

  • New Member
  • *
  • Posts: 48
Re: Undo function (TEdit)
« Reply #2 on: February 24, 2024, 02:33:38 pm »
Is there a document that describes the best algorithm for handling the Undo function for components of type TEdit?

Thaddy

  • Hero Member
  • *****
  • Posts: 16185
  • Censorship about opinions does not belong here.
Re: Undo function (TEdit)
« Reply #3 on: February 24, 2024, 02:58:08 pm »
That is called a stack.
« Last Edit: February 24, 2024, 03:01:13 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Undo function (TEdit)
« Reply #4 on: February 24, 2024, 03:08:15 pm »
About a document I can't say anything but I would start on my own by adding an array for a record that keeps track about changes and use that array within a keyup event to act.
Of course to add or remove an entry should be better checked, above is just a quick and dirty variant but it help to understand how it might work I hope.
When the minimalistic variant works, you can work on the logic that it for example replace many single letters to one string if they are written continues etc...
Or that a remove should only be done when new data is added etc ....
Lots of possibilities.
My idea would be like
Code: Pascal  [Select][+][-]
  1. type
  2.   TEditHistory = record
  3.     Line: Integer; // in what line did a change happen
  4.     Position: Integer; // at what position of above line did the change happen
  5.     Action: TEditAction; // simple enum that tell if you added a char or pressed delete etc ...
  6.     Old: string; // original content if enum told delete or replace ...
  7.     New: string; // new char(s)
  8.   end;
And make heavy use of the OnKeyUp() event for all possibilities that can happen, including checks if something was pasted, replaced, deleted etc ....
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 16185
  • Censorship about opinions does not belong here.
Re: Undo function (TEdit)
« Reply #5 on: February 24, 2024, 03:40:55 pm »
TRecall? Which is in classes....
If I smell bad code it usually is bad code and that includes my own code.

hedgehog

  • New Member
  • *
  • Posts: 48
Re: Undo function (TEdit)
« Reply #6 on: February 25, 2024, 07:22:57 am »
Yes guys, thank you very much!
Of course I know what the stack is for :)

The question is a little different.
Here's an example:
___________
|Text            |
|__________|

Then the user quickly presses keys 1,2,3
___________
|Text123       |
|__________|

What should be on the stack?
1) ----------
Text 
Text123 

or

2)----------
Text 
Text1
Text12
Text123 

Thanks



cdbc

  • Hero Member
  • *****
  • Posts: 1664
    • http://www.cdbc.dk
Re: Undo function (TEdit)
« Reply #7 on: February 25, 2024, 08:44:22 am »
Hi
Start the apps you use the most, one after another and see what strategy they're using... Then I'd try to mimic a golden median of that, in your own app...
Just my "Nickles Worth"
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

hedgehog

  • New Member
  • *
  • Posts: 48
Re: Undo function (TEdit)
« Reply #8 on: February 25, 2024, 10:16:45 am »
Hi
Start the apps you use the most, one after another and see what strategy they're using... Then I'd try to mimic a golden median of that, in your own app...
Just my "Nickles Worth"
Regards Benny

Yes, this is probably the best way

Thaddy

  • Hero Member
  • *****
  • Posts: 16185
  • Censorship about opinions does not belong here.
Re: Undo function (TEdit)
« Reply #9 on: February 25, 2024, 11:04:18 am »
<sigh> the algorithm is a stack and the ready made solution is Trecall.
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1664
    • http://www.cdbc.dk
Re: Undo function (TEdit)
« Reply #10 on: February 25, 2024, 12:57:23 pm »
Hi
@Thaddy: I had a looksee, to me it just seems like an elaborate TPersistent...
But... where's the 'Retieve' method, to complement 'Store' & 'Forget'?!?
I guess, I expected a bit more... %)
Maybe I'm just to dumb to see it  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

hedgehog

  • New Member
  • *
  • Posts: 48
Re: Undo function (TEdit)
« Reply #11 on: February 25, 2024, 01:09:42 pm »
solution is Trecall.
I'm probably too stupid, sorry! What is this?

cdbc

  • Hero Member
  • *****
  • Posts: 1664
    • http://www.cdbc.dk
Re: Undo function (TEdit)
« Reply #12 on: February 25, 2024, 01:13:56 pm »
Hi
"TRecall" is a class implemented in unit 'Classes', you can Ctrl+click to see for yourself  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

hedgehog

  • New Member
  • *
  • Posts: 48
Re: Undo function (TEdit)
« Reply #13 on: February 25, 2024, 01:23:52 pm »
Hi
"TRecall" is a class implemented in unit 'Classes', you can Ctrl+click to see for yourself  8)
Regards Benny
Thanks, CDBC.

This seems unnecessary to me.
Is this why the simplest text editor can be tens of megabytes in size?

hedgehog

  • New Member
  • *
  • Posts: 48
Re: Undo function (TEdit)
« Reply #14 on: February 25, 2024, 01:30:22 pm »
I want to show my work.
Beautiful?

 

TinyPortal © 2005-2018