Lazarus

Free Pascal => Unix => Topic started by: AlexTP on March 07, 2017, 05:08:34 pm

Title: Copy-to-clipboard, text clears after exiting my app
Post by: AlexTP on March 07, 2017, 05:08:34 pm
CudaText app (LCL, Linux gtk2)
I copy text as

Clipboard.AsText:= 'dddddddddddd';

after my app exits, other apps don't see this text. Clipboard empty. Why?
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Bart on March 07, 2017, 07:10:59 pm
AFAIK this is standard *nix behaviour.

Bart
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: AlexTP on March 07, 2017, 08:34:54 pm
GEdit does it ok: after exiting, it's clipboard text can be pasted.
How to do it?
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Abelisto on March 07, 2017, 10:35:24 pm
Code: Pascal  [Select][+][-]
  1. unit fix_gtk_clip;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.     gtk2, gdk2, Clipbrd;
  9.  
  10. implementation
  11.  
  12. var
  13.     c: PGtkClipboard;
  14.     t: string;
  15.  
  16. finalization
  17.     c := gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
  18.     t := Clipboard.AsText;
  19.     gtk_clipboard_set_text(c, PChar(t), Length(t));
  20.     gtk_clipboard_store(c);
  21. end.
Add this unit somewhere. It is dirty solution but it works for me.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: AlexTP on March 08, 2017, 03:50:21 am
Thank you...

Added code to wiki-
http://wiki.freepascal.org/Clipboard#How_to_fix_empty_GTK2_clipboard_on_exit
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Abelisto on March 08, 2017, 09:25:07 am
There is another topic about the problem with some useful links: http://forum.lazarus.freepascal.org/index.php/topic,35244 (http://forum.lazarus.freepascal.org/index.php/topic,35244)
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: KarenT on May 20, 2017, 09:02:25 pm
I am having this same problem. Tried in Ubuntu and Mint same - same. Clipboard is fine until I close the program.

I added the fix_gtk_clip unit but still clipboard is cleared on exit.

Any progress on this being made possible natively in Lazarus?
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: molly on May 20, 2017, 09:06:46 pm
afai, the 'clearing' of the clipboard after program closes is up to the window manager in use.

Some provide an additional (as in you are required to install one yourself) clipboard manager that solves that issue for you, others have a build in clipboard manager.

Do a search on these forums, the question has been asked (and answered) multiple times.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Thaddy on May 20, 2017, 09:33:31 pm
https://wiki.ubuntu.com/ClipboardPersistence

Not only for Ubuntu.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: KarenT on May 20, 2017, 10:03:36 pm
Thanks, but I had read many links and tried them all before posting. Nothing is working, so came up with a kludge that suits me fine.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ed1Click(Sender: TObject);
  2. begin
  3.   (Sender as TEdit).SelectAll;
  4.   (Sender as TEdit).CopyToClipboard;
  5.   Timer1.Interval:=15000;
  6.   Timer1.Enabled:=True;
  7.   Hide;
  8. end;
  9.  
  10. procedure TForm1.Timer1Timer(Sender: TObject);
  11. begin
  12.   Close;
  13. end;
  14.  
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Thaddy on May 20, 2017, 10:12:32 pm
If that worked, try Application.ProcessMessages instead of the timer.
The message queue is implemented differently on linux, so maybe the queue is not fully processed.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: KarenT on May 21, 2017, 04:30:26 pm
If that worked, try Application.ProcessMessages instead of the timer.

Thanks, but that did not make any difference either.

Even using the "fix_gtk_clip" did not work. Stepping through it, the "t" in this "t := Clipboard.AsText;" was empty as it was closing down.

The thing is, that many people have stated that other programs work fine with cut/paste without special Clipboard-Managers, so there must be a solution.

The timer workaround is adequate for my needs right now, but it would be nice to not keep finding the frailties of using Linux and Lazarus. I wonder if Kylix is still available some where.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: HeavyUser on May 21, 2017, 05:04:25 pm
I wonder if Kylix is still available some where.
https://sourceforge.net/projects/freeclx/
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Thaddy on May 21, 2017, 05:14:59 pm
I wonder if Kylix is still available some where.
https://sourceforge.net/projects/freeclx/
That's just the libraries.
Also note D7-D2007 code still contain CLX and can be massaged to work with FPC if you have an edition with sourcecode.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: HeavyUser on May 21, 2017, 06:02:03 pm
I wonder if Kylix is still available some where.
https://sourceforge.net/projects/freeclx/
That's just the libraries.
Also note D7-D2007 code still contain CLX and can be massaged to work with FPC if you have an edition with sourcecode.
it should contain the clipboard code requested in this thread. Although, it is possible that the code is nothing more that a thin layer on top of the qt clipboard classes.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: KarenT on May 21, 2017, 08:40:01 pm
All too hard for the time I have available and the Timer is solving it fine. The program is for my own use so I can live with it until I see a native-working option for Linux/Lazarus.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Leledumbo on May 21, 2017, 09:11:44 pm
Just install a clipboard manager. It's WM/DE problem, not application problem.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Graeme on May 22, 2017, 10:12:53 am
I wonder if Kylix is still available some where.
Yes, but I really wouldn't recommend using Kylix any more. It is just too out of date these days (compiler and IDE), and CLX uses a terribly old Qt (that just looks crap - doesn't even support anti-aliased text).

   http://geldenhuys.co.uk/~graemeg/kylix3_openedition.tar.gz (http://geldenhuys.co.uk/~graemeg/kylix3_openedition.tar.gz)

Rather use fpGUI or MSEgui for true cross-platform applications, or look at LCL with its inconsistencies across platforms.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Graeme on May 22, 2017, 10:24:33 am
after my app exits, other apps don't see this text. Clipboard empty. Why?
It is standard X11 behaviour, and is actually more efficient than Windows clipboard. Why, because on Windows, if you copy something to the clipboard you instantly get a copy, and thus it uses memory (RAM) to store that copy. On X11 when you copy something to the clipboard, it simply notifies the windowing system of the window that has content available, but no copy actually exists in RAM yet. Once once a target applications requests that content (via a Paste action), does it query the source window/application for the contents. This explains why you clipboard is empty, because that source application has already terminated.

Now some newer Window Managers in X11 support clipboard managers that can go against the X11 design. What these clipboard managers do, is become the "source" of the clipboard content, instead of the original source application. This clipboard managers keep running while the window manager runs, so you never get the "out of scope" situation.

If you want to see the technical details in how this is implemented, so applications (actually the GUI framework) can take advantage of such clipboard managers, take a look at how it was implemented in fpGUI Toolkit.

Quote
commit c2af2a386efef5d52db20482e81748fc36aedb91 (https://github.com/graemeg/fpGUI/commit/c2af2a386efef5d52db20482e81748fc36aedb91)
Author: Andrew Haines <andrewd207@****>
Date:   Wed Apr 24 17:24:50 2013 +0200

    Persistent clipboard support under X11.
   
    Some desktop environments or window managers include support for a clipboard
    manager. This clipboard manager can make the content of the clipboard persistent
    even after the source application closed down.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: Thaddy on May 22, 2017, 11:12:18 am
There is the small complication that keeping data on the clipboard after exiting a process is technically a memory leak.
That's also the reason that X11 developers won't fix it. It is unsafe behavior.
A clipboard manager merely takes ownership of the data but clipboards are inherently not secure unless the manager encrypts and decrypts the data.

This is a known Windows design flaw, although even on Windows the clipboard data is only accessible locally after process termination.
Title: Re: Copy-to-clipboard, text clears after exiting my app
Post by: KarenT on May 22, 2017, 06:58:08 pm
Just install a clipboard manager. It's WM/DE problem, not application problem.

But, I shouldn't have to, gedit handles it and leaves text on the Clipboard after closing. But not an issue as I have mentioned, I'll just go with the Timer. :)
TinyPortal © 2005-2018