Recent

Author Topic: Clipboard and RichMemo  (Read 2560 times)

CésarV

  • New Member
  • *
  • Posts: 16
Clipboard and RichMemo
« on: September 16, 2022, 05:22:16 pm »
Hello,
I use a button to automatically select all text from a RichMemo1 and transfer it, via the clipboard, to another RichMemo2.
As long as the application is open: no problem. Copy/Paste OK with all formatting attributes.

 But after closing the app (after the button procedure) the clipboard looks empty: no way to paste the text elsewhere.

By consulting the history of the clipboard, the text stored in memory appears there, but it has lost all its attributes of format, color ... and even that I cannot "paste" it anywhere (rtf compatible of course) . And it's annoying because if the user made a mistake, he could reopen the application and paste the lost text back into it...

Cordially.


(re) Bonjour,
J'utilise un bouton pour sélectionner automatiquement tout le texte d'un RichMemo et le transférer, via le presse papier, vers un autre RichMemo.
Tant que l'application est ouverte : pas de pb. Copier/Coller OK avec tous les attributs de formatage.

 Mais après la fermeture de l'appli (après la procedure du bouton) le presse papier à l'air vide : plus moyen de coller le texte ailleurs.

En consultant l'historique du presse papier, le texte mis en mémoire y figure bien, mais il perdu tous ses attributs de format, couleur ... et même ça je ne peux pas le "coller" nulle part (compatible rtf bien sûr). Et c'est gènant parce que si l'utilisateur a fait une erreur, il pourrait réouvrir l'application et y recoller le texte perdu ...

Cordialement.
« Last Edit: September 16, 2022, 09:41:00 pm by CésarV »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6381
  • Compiler Developer
Re: Perte Presse papier
« Reply #1 on: September 16, 2022, 05:37:52 pm »
In the international part of the forum please use English as language or at least include an automatic translation (e.g. by Google Translate) in addition to your native language.

jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: Clipboard and RichMemo
« Reply #2 on: September 17, 2022, 12:40:34 am »
I don't know about recent Laz but the one I just looked at in the Destructor of the Clipboard class.

Code: Pascal  [Select][+][-]
  1.  
  2.   Clear;
  3.   inherited Destroy;            
  4.  

just partial of the code of course.

 The CLEAR does what it states, it clears the clipboard.

EDIT:

  I forgot, if you are using the system's context menu then I'z not sure about that one. It should work,

« Last Edit: September 17, 2022, 12:43:52 am by jamie »
The only true wisdom is knowing you know nothing

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Clipboard and RichMemo
« Reply #3 on: September 17, 2022, 08:21:32 am »
hello,
depending what is your O.S, the clipboard is cleared when an application is terminated. On windows with Lazarus it is this case.
May be, before closing your application you can save the rtf content of the clipboard in a file and if you want, reload the clipboard with this content when reopening your application.

Ami calmant, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: Clipboard and RichMemo
« Reply #4 on: September 17, 2022, 04:28:59 pm »
Actually windows does maintain the clipboard when app exits if there is text on it.

I really don't thing the LCL should be clearing the text of the clipboard when it free's itself.

Or we could be using a BOOLEAN to indicate this before it gets cleared.

Property KeepTextOnClipboardAfterFree:Boolean;
The only true wisdom is knowing you know nothing

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Clipboard and RichMemo
« Reply #5 on: September 17, 2022, 05:20:01 pm »
Actually windows does maintain the clipboard when app exits if there is text on it.
Ok i have tried to select text in a tmemo an copy it to the clipboard. When i close the laz app the text is always in the clipboard.
Now i select text in a trichmemo (see  attachment1) and copy it in the clipboard (see attachment2). When i close the laz app nothing is in the clipboard.
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

CésarV

  • New Member
  • *
  • Posts: 16
Re: Clipboard and RichMemo
« Reply #6 on: September 17, 2022, 08:44:00 pm »
Ha yes ! Nothing in the clipboard ...
I use windows 10x64 and 11x64.+

BUT : When I try to copy test in a RichEDIT (app made with Delphi) , then close the app and try to paste in other app in  RichMEMO (made with LAZARUS) : that works !
the opposite dont match ...
« Last Edit: September 17, 2022, 08:45:34 pm by CésarV »

CésarV

  • New Member
  • *
  • Posts: 16
Re: Jurassik Pork
« Reply #7 on: September 17, 2022, 09:50:54 pm »
This application is for my wife who often does anything ...
For the text to be saved in a file, she must first give it a name that she can forget. I did do a Messagedialog on closing to remind her, but you never know... So I want to save the text before closing...   :D 8-)
« Last Edit: September 18, 2022, 01:48:25 pm by CésarV »

paweld

  • Hero Member
  • *****
  • Posts: 1591
Re: Clipboard and RichMemo
« Reply #8 on: September 17, 2022, 11:05:24 pm »
Example attached.
Best regards / Pozdrawiam
paweld

CésarV

  • New Member
  • *
  • Posts: 16
Re: Clipboard and RichMemo
« Reply #9 on: September 18, 2022, 02:20:46 pm »
paweld,

Thank you for your answer and the attached example.
I can actually copy a selection in your RichMemo.Text, close the project, and paste that selection into a RichEdit in other app  %)...
But I don't understand why...   %)  :(
« Last Edit: September 18, 2022, 04:05:59 pm by CésarV »

jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: Clipboard and RichMemo
« Reply #10 on: September 18, 2022, 03:01:41 pm »
if you have the Clipbrd in use, more than likely using the systems clipboard will also force it to clear when the app exits.

 So, like I said as I showed the source code section, the "CLEAR" is being called when the clipbrd unit finalizes.
The only true wisdom is knowing you know nothing

CésarV

  • New Member
  • *
  • Posts: 16
Solved
« Reply #11 on: September 18, 2022, 04:06:31 pm »
It took me a while, but I finally understood...
Thanks a lot.
« Last Edit: September 18, 2022, 04:10:39 pm by CésarV »

rvk

  • Hero Member
  • *****
  • Posts: 6988
Re: Clipboard and RichMemo
« Reply #12 on: September 18, 2022, 04:12:28 pm »
@paweld, The code seems a bit unstable.
Pressing Ctrl+C a few times, sometimes the clipboard is empty, and sometimes it gives an error (can't reproduce right now).

But I don't understand why...   %)  :(
I think it is because Lazarus doesn't play well with the Windows clipboard.
It uses an internal clipboard too.

When using CopyToClipboard in the TRichMemo it will use the actual Windows clipboard.
Lazarus clears that clipboard and puts it's own cached clipboard on it.
But because the TRichMemo didn't use the Lazarus clipboard, it's copied data is deleted.

The code of paweld does a Windows copy to clipboard but then reads that data (because it then has multiple formats) into the cached clipboard of Lazarus (via a list). After that the clipboard is owned by Lazarus and contains the copied data. And Lazarus leaves its own cached clipboard on the Windows clipboard on exit.

So, like I said as I showed the source code section, the "CLEAR" is being called when the clipbrd unit finalizes.
Actually, that part only clears the internal cached clipboard of Lazarus. When you trace through the code you will notice the Windows clipboard is already empty before that line. You can comment the Clear out in TClipboard.Destroy and it will still delete the Windows clipboard.

The problem lies somewhere in that TClipboard (when used for the first time) takes ownership of the Windows clipboard (TClipboard.GetOwnerShip).
After that Lazarus owns the clipboard and replaces everything on it with it's own cached version. I think it does this also on exit (when it still owns the clipboard). See the ClipboardGetOwnerShip() in TClipboard.Destroy. Although, commenting out that line isn't sufficient.

Anyway, I think it is a real bug that Lazarus just deletes the Windows clipboard on exit (even though it was owned by the program itself).
It's lucky it doesn't touch the clipboard if another program has copied something to the clipboard because that would be really really wrong.

Quote
The TClipboard in clipbrd.pp has an internal cache system, so that an application can use all types of clipboards even if the underlying
platform does not support it.

BTW. It would also be possible to change TRichMemo to not use the Windows clipboard directly but the Lazarus clipboard functionality. That would fix it too (although I don't like this very much). It would be better to have an option to eliminate the Lazarus clipboard completely. At least that caching and replacing thing.
« Last Edit: September 18, 2022, 04:24:45 pm by rvk »

paweld

  • Hero Member
  • *****
  • Posts: 1591
Re: Clipboard and RichMemo
« Reply #13 on: September 18, 2022, 04:40:36 pm »
@rvk: in fact, sometimes the error happens. below is the modified code, which in my case eliminated the error you described.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CopyFromRichMemo;var
  2.   i: Integer;
  3.   fid: PtrUInt;
  4.   ms: TMemoryStream;
  5.   clplist: TClipbrdList;
  6. begin
  7.   RichMemo1.CopyToClipboard;
  8.   sleep(25);
  9.   clplist := TClipbrdList.Create;
  10.   for i := 0 to Clipboard.FormatCount - 1 do
  11.   begin
  12.     ms := TMemoryStream.Create;
  13.     if Clipboard.GetFormat(Clipboard.Formats[i], ms) then
  14.       clplist.Add(Clipboard.Formats[i], ms)
  15.     else
  16.       ms.Free;
  17.   end;
  18.   Clipboard.Clear;
  19.   for i := clplist.Count - 1 downto 0 do
  20.   begin
  21.     clplist.Data[i].Position := 0;
  22.     if clplist.Keys[i] > 0 then // <--------------- Add this line
  23.       Clipboard.AddFormat(clplist.Keys[i], clplist.Data[i]);
  24.     clplist.Data[i].Free;
  25.   end;
  26.   clplist.Free;
  27. end;              
         
« Last Edit: September 18, 2022, 04:46:22 pm by paweld »
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018