Recent

Author Topic: How to not wipe out the graphical clipboard at program end? (gtk2 + KDE)  (Read 8801 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #15 on: December 20, 2024, 03:48:19 pm »
I just installed Ubuntu 22.04 in virtual machine and Gimp can indeed paste the clipboard contents as a new image. Something is up on your machine perhaps?

Thanks a lot jeremiah for testing this. I'm surprised...

I use Ubuntu 22.04 64-bit with KDE Plasma Desktop. Probability you used another Desktop... Could it be, that this makes the difference? I cannot estimate that (do not have enough knowledge about Ubuntu desktops).

Or can it be, that your distribution perhaps includes such an utility which Bart spoke of?
Quote
There are utilities on *nix that keep the clipboard persistent.

This is just to show how to save your picture to temp file then store the clipboard. I know you said not what you want but stare at it for a while and see if you can use it else just throw it away. Works on Ubuntu here also as does the previous.

I tested your proj-clipboard-4.zip. To use a temporary file would be a possible workaround. Unfortunately it does not work on my system just like your demo from reply #4. Which is no surprise, because you again use gdk_pixbuf_new_from_file() and gtk_clipboard_set_image().

Well, I wanted to use gdk_pixbuf_new_from_stream in the gtk_fix pas but it is not implemented yet in Lazarus/FP.  :(
So I just used the previous in the gtk_fix pas and while it works it needs to be enhanced. ie, instead of only looking for bmp it should check the format on the clipboard then perhaps a case statement for different image types.

proj-clipboard-5.zip does sadly also not work on my system, which is no surprise, because you again use gdk_pixbuf_new_from_file() and gtk_clipboard_set_image().

Started anew. One form not hidden and in close event clipboard is preserved. However, using the same code in fix_gtk_pix_clipboard.pas does not. No mess with files this time Hartmut so that's good. Works in Ubuntu as long as gtk is initialized, comment out and fails.

Thank you jeremiah for this new idea with gtk_init(). I tested new-clip.zip:

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. var
  3.  Clipper: PGtkClipboard;
  4.  pixbuf: PGdkPixbuf;
  5. begin
  6.  gtk_init(@argc, @argv);  // Initialize GTK   Needed on ubuntu
  7.  Clipper := gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
  8.  pixbuf := gtk_clipboard_wait_for_image(Clipper);
  9.  
  10.  if pixbuf <> nil then
  11.   begin
  12.    writeln('assigned_n');
  13.    gtk_clipboard_set_image(Clipper, pixbuf);
  14.    gtk_clipboard_store(Clipper);
  15.   end;
  16. end;

but unfortunately it does not work on my system too (the clipboard stays empty). I did not use any command line parameters for gtk_init(@argc, @argv), should I?

For me it is strange, that your demos do not work on my distribution, while they work apparently on yours. Can it be, that your distribution perhaps has such an utility running, which Bart spoke of, which keeps the clipboard persistent? Can you therefore please test my attached demo on your distribution? It only saves a new picture in the clipboard:
 - if you exit this demo and the picture is still in the clipboard, then we know, that such an utility is running on your distribution
 - if you exit this demo and the picture is not longer in the clipboard, then we know, that your code really works. Thanks a lot.

jeremiah

  • New Member
  • *
  • Posts: 18
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #16 on: December 20, 2024, 06:30:26 pm »
KDE!!?? No wonder. KDE is not GTK, it is QT!!  Lol, no wonder... I use Cinnamon on Debian and the Ubuntu virtual machine is Gnome, both are GTK.

I have no clipboard utilities running here, never have. When I exit your demo there is nothing on the clipboard.

jeremiah

  • New Member
  • *
  • Posts: 18
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #17 on: December 20, 2024, 07:41:40 pm »
Linux desktop managers usually implement both gtk and qt. ie, Cinnamon is gtk but has support for qt though not full qt. KDE is qt and supports gtk though not full gtk. They will try to implement enough so the program will run but it is limited.

Yes, if you give your program to someone running any gtk desktop it should run. (gnome)

I think it is possible to preserve the clipboard in qt though I do not know how much support Lazarus/Free Pascal have.

The clipboard in Linux is managed by X11 and historically when the program quit the clipboard was wiped to free memory, precious in those days. Desktop managers can hack around this and use their own version of a clipboard, KDE does this I think, or as gtk does and utilize the store feature that has been implemented in X11. Having a variety of desktop managers or windows managers is nice but also a giant pain for those programming. I have used gtk desktops from the beginning even though I really do not like the path Gnome is going with gtk and assume someday I will start using qt desktops.

Search in your Lazarus/LCL/interfaces/qt5 source and check the bindings to see what is implemented. Best place to start if you want to preserve the clipboard in KDE and KDE might have their own implementation so scour them also.

LV

  • Full Member
  • ***
  • Posts: 204
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #18 on: December 21, 2024, 08:45:45 am »
Then it would be easier for all, that I implement nothing and tell the user, that he may not exit my program, as long as he does not want to loose the clipboard...

It seems like you are asking a rhetorical question, and you are answering yourself.
But seriously, the answer to the topic of your post may be the following.

You have a program (GTK2) that reads a graphic file. The user can somehow modify it.
The image should be saved in the clipboard when the program is closed.

Solution scheme.
1. Create a simple utility for reading and sending an image to the clipboard. It is just a few lines of code.
2. When your program is closed, insert the code to run this utility, for example using TProcess.

That's it. Your program is closed, the image is in the clipboard. 8-)


Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #19 on: December 21, 2024, 01:04:19 pm »
... the answer to the topic of your post may be the following.
You have a program (GTK2) that reads a graphic file. The user can somehow modify it.
The image should be saved in the clipboard when the program is closed.
Solution scheme.
1. Create a simple utility for reading and sending an image to the clipboard. It is just a few lines of code.
2. When your program is closed, insert the code to run this utility, for example using TProcess.

This would be a very cumbersome solution:
 - my program needs to know the path to the utility, so that it can be started
 - the utility must be started in a way, that it continues to run, after its parent has stopped
 - the utility would run never-ending in the background
 - when my program is started one more time and exits one more time, it must detect, whether the utility is already running, to avoid to start it again and again
 - in this case the already running utility must be triggered, to do it's job once again
 - my program can be started more than once at the same time, which can make things not easier.

Of course everything of this is somehow "possible", but for me it's not worth the effort. I definitely don't wont to go a way like this.

As said multiple times, I search for a solution like unit 'fix_gtk_clipboard' but for graphical contents - or some kind of procedure FormClose() for the same purpose, which runs on KDE Plasma Desktop (because for Gnome I still have a solution from jeremiah). This would be a *common* solution and very easy to use in every project :-)) That was the intention, why I started this Topic.



Linux desktop managers usually implement both gtk and qt. ie, Cinnamon is gtk but has support for qt though not full qt. KDE is qt and supports gtk though not full gtk. They will try to implement enough so the program will run but it is limited.
...
The clipboard in Linux is managed by X11 and historically when the program quit the clipboard was wiped to free memory, precious in those days. Desktop managers can hack around this and use their own version of a clipboard, KDE does this I think, or as gtk does and utilize the store feature that has been implemented in X11.

Thank you jeremiah for that infos (I did not know that) and for your continuous help.

Quote
I think it is possible to preserve the clipboard in qt though I do not know how much support Lazarus/Free Pascal have.
...
Search in your Lazarus/LCL/interfaces/qt5 source and check the bindings to see what is implemented. Best place to start if you want to preserve the clipboard in KDE and KDE might have their own implementation so scour them also.

I checked folder Lazarus/LCL/interfaces/qt5 which has more than 4 MB of source code in about 40 files. I have no idea what to do there. I have never heard of bindings and by google I found https://en.wikipedia.org/wiki/Binding where I understood nothing. My knowledge about QT is near zero.

To solve this by myself is far beyond my horizon. That's why I'm asking for help in this Topic from someone who is familiar with the clipboard on KDE Plasma Desktop. If I should switch to another subforum for that please let me know and to which.

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #20 on: December 22, 2024, 02:39:14 pm »
There are utilities on *nix that keep the clipboard persistent.
Bart

As long as no *internal* solution was found, which runs on Ubuntu 22.04 with KDE Plasma Desktop, I would like to play with some external utilities for that purpose, like Bart wrote.

Does somebody know the names of those utilities which run on Ubuntu 22.04 with KDE Plasma Desktop?
Thanks in advance.

jeremiah

  • New Member
  • *
  • Posts: 18
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #21 on: December 22, 2024, 11:51:12 pm »
Sorry, I have been away for the last days. Hopefully next week I am back and can do some digging on KDE.

Edit:  For the short solution your users could simply install Klipper and keep it running, it is a KDE program for saving clipboard contents.
« Last Edit: December 22, 2024, 11:53:53 pm by jeremiah »

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #22 on: December 23, 2024, 04:21:05 pm »
Sorry, I have been away for the last days. Hopefully next week I am back and can do some digging on KDE.

No problem, I can wait. Please keep in mind that i wrote:
Quote
Do you (or somebody else) know, how to preserve the graphical clipboard on KDE, without switching my whole program to QT? Is this possible?
My current project is a larger graphics intensive GTK2-project which does not work on QT. So something which would require to switch the project to QT would not help me. Please don't go in this direction.

For the short solution your users could simply install Klipper and keep it running, it is a KDE program for saving clipboard contents.
Thanks jeremiah, I will check Klipper.

Does somebody know other utilities to preserve the graphical content of the clipboard which run on Ubuntu 22.04 with KDE Plasma Desktop so I can play with them too?

jeremiah

  • New Member
  • *
  • Posts: 18
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #23 on: December 23, 2024, 09:28:50 pm »
Klipper is the clipboard manager for KDE. For me over the years it has not always worked as I thought it should without configuring it to keep everything. I am not a KDE user though I do keep some virtual machines with it for testing and compiling to QT. I have always had to set it to keep contents and then it was hit and miss whether it would save. Again, I am not a KDE user so I very well could be over looking something. I did not find anything in the bindings that is useful, at least what I thought was useful, so the best thing is to just let the KDE user manage Klipper their way and you just put whatever on the clipboard. That is the way it should be anyway.

Open Klipper settings and check for it to save everything then see if your program works. The code we have been passing back n forth here works after configuring Klipper to save everything.

LV

  • Full Member
  • ***
  • Posts: 204
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #24 on: December 24, 2024, 10:20:11 am »
This would be a very cumbersome solution:
 - my program needs to know the path to the utility, so that it can be started
 - the utility must be started in a way, that it continues to run, after its parent has stopped
 - the utility would run never-ending in the background
 - when my program is started one more time and exits one more time, it must detect, whether the utility is already running, to avoid to start it again and again
 - in this case the already running utility must be triggered, to do it's job once again
 - my program can be started more than once at the same time, which can make things not easier.

Of course everything of this is somehow "possible", but for me it's not worth the effort. I definitely don't wont to go a way like this.

OK, go your own way.
This morning I made this for the soul, maybe someone is interested:
1. worker -> worker.lpi -> run -> stop -> quit
2. copy the executable file to the main folder
3. main -> main.lpi -> run -> stop -> quit
4. open the main folder, run main, click the Worker button, click the Clipboard button and close the 'X' program.

Cross-platform solution. Works for me on Windows and Linux (GTK2):
- the program knows the path to the worker utility
- the utility works after closing the main program in the background (you can force it to close).
- when restarting the program, it knows whether the utility is running or not.

Good luck
« Last Edit: December 24, 2024, 10:33:07 am by LV »

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: How to not wipe out the graphical clipboard at program end? (gtk2)
« Reply #25 on: December 25, 2024, 06:59:08 pm »
I did not find anything in the bindings that is useful, at least what I thought was useful

Thank you very much jeremiah for checking and trying this.

Question1: Are you sure that an internal solution for KDE Plasma Desktop with a Lazarus program, which is compiled for GTK2 (not QT) is not possible? Or could someone, who is more familiar with Lazarus and the clipboard on KDE Plasma Desktop find such a solution?
If the answer is a clear "not possible" I can close this Topic. Otherwise I would hope the right one would see this Topic.

I tried Klipper (xclip). After configuring it as you said, it preserves the graphical clipboard after my program has exited, but larger images sometimes are distorted (have a wrong x/y ratio), which is not the case when I paste them at the time, while my program still runs. Therefore and because you wrote "then it was hit and miss whether it would save" I would like also to test another utility.

Question2: Does somebody know other utilities to preserve the graphical content of the clipboard, which run on Ubuntu 22.04 with KDE Plasma Desktop?

 

TinyPortal © 2005-2018