Lazarus

Using the Lazarus IDE => Editor => Topic started by: fedkad on April 30, 2019, 11:29:57 am

Title: Unable to paste into Lazarus editor from another application
Post by: fedkad on April 30, 2019, 11:29:57 am

I haven't isolated the rare case that it works. It looks pretty random. But, I can say that 90 percent it does not work.

My environment:
Lazarus 2.0.2 x86_64-linux-gtk2
Ubuntu Linux 19.04
Desktop: Gnome
Title: Re: Unable to paste into Lazarus editor from another application
Post by: lucamar on April 30, 2019, 11:48:24 am
Strange. I use various various versions of (K|L)Ubuntu and it has always worked Indeed, I use an external editor frequently to store or edit pieces of code in some specific ways and copy/paste from/to the Lazarus editor without problems.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: Martin_fr on April 30, 2019, 12:16:30 pm
Can you test with a new project?
- Add a memo and a button to a form.
- In the button OnClick do
Code: Pascal  [Select][+][-]
  1. Memo1.Text := ClipBoard.AsText

You need
Code: Pascal  [Select][+][-]
  1. uses ClipBrd;

Copy something, from either of those Apps, and press the button.
Try to copy using the menu (or contextmenu) labeled "Copy".


Note GTK has more than one ClipBoard. Selected text can often be pasted with the middle mouse. This uses a different ClipBoard
For this try
Code: Pascal  [Select][+][-]
  1. PrimarySelection.AsText

And for good measure see what SecondarySelection does (if anything)

Title: Re: Unable to paste into Lazarus editor from another application
Post by: fedkad on April 30, 2019, 09:16:07 pm
Hello Martin_fr.

I did what you suggested. As you say: ClipBoard.AsText pastes data which was copied to the clipboard before. PrimarySelection.AsText "pastes" data that is selected in another application. SecondarySelection.AsText does nothing (empty string). However, these have nothing to do with my problem. And actually when the problem occurs with the Lazarus editor, the same happens with the program: ClipBoard.AsText and PrimarySelection.AsText paste empty string; I verified this by using:  Label1.Caption := IntToStr(Length(ClipBoard.AsText)).

I think Lazarus and the simple test program use the same code (libraries etc.), so they behave the same. Please note that, when this problem occurs (that is, when I cannot paste anything to Lazarus editor or in the test program), I can paste the data to another application, like LO Writer, Atom editor, Firefox, etc.

BTW; when this problem occurs, the context menu's Paste command or Ctrl+V on the test program's Memos works. But, the context menu's Paste command or Ctrl+V on Lazarus editor does not work.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: Martin_fr on April 30, 2019, 09:42:51 pm
Memo context menu paste, is handled by gtk/qt. Because Memo is a native control.

Clipboard.AsText is the LCL access to the clipboard.

SynEdit (the source editor) is not a native control. It uses Clipboard.AsText (with some additions).
After your test, the problem is in Clipboard.AsText and not in the "some additions" part.

Clipboard can contain text and other formats. AsText obviously will not return an image from the clipboard.

It is possible that some of the apps you use, store their text to the clipboard with a format the lcl does not understand.

You can report a bug, but without further info, I don't know if anyone will be able to do much about it.




I am not particularly familiar with the clipboard code. (Not tested any of the below)
But you could have a look at

    property FormatCount: Integer read GetFormatCount;
    property Formats[Index: Integer]: TClipboardFormat read GetFormats;

When you copy the unknown text, iterate through the formats, and list them.

There is a function ClipboardFormatToMimeType that may map the format to a string...

Maybe that turns out something useful.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: wp on April 30, 2019, 10:04:31 pm
Maybe not relevant, but you mention Ctrl+V: if you selected classic keyboard scheme in the IDE options / Keyboard mapping (like I do) then this will never work, you must use Shift+Ins instead.

To further investigate the clipboard you could also try my program "Clipboard Explorer" (https://sourceforge.net/p/wp-laz/code/HEAD/tree/ClipboardExplorer/trunk/). After pressing the "Refresh" button it lists all formats contained in the clipboard and displays the clipboard content for each format as a hex dump or, when possible, in a memo, RichMemo, or image.

The minimum requirement for pasting text into the IDE is a text/plain format (CF_TEXT).

Note that for compilation you need the packages KControls and RichMemo which you can install easily via Online-Package-Manager.

Of course this program was written with Lazarus, and when Lazarus has a bug in clipboard handling this program will have this bug, too.

A similar program is Jesus Reyes' "clipanalizer" found on CCR: https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/applications/clipanalizer/
Title: Re: Unable to paste into Lazarus editor from another application
Post by: fedkad on May 01, 2019, 02:43:45 pm
I couldn't try "Clipboard Explorer" (clipbex) program very much: After I compiled and ran it, it displayed its main window. However, when I do a "Refresh" it starts displaying something on its main window and immediately crashes. Here is its console output:

Code: [Select]
./clipbex
Gtk-Message: 15:58:49.041: Failed to load module "canberra-gtk-module"
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!
Peek !!!

(clipbex:30502): Gdk-CRITICAL **: 15:58:51.253: IA__gdk_x11_atom_to_xatom_for_display: assertion 'ATOM_TO_INDEX (atom) < virtual_atom_array->len' failed
The program 'clipbex' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAtom (invalid Atom parameter)'.
  (Details: serial 9826 error_code 5 request_code 24 minor_code 0)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)

Sometimes, it does not crash, but in this case it displays nothing. I think, it is a similar case like the following (see next paragraph and also the attached screenshot).

I ran the "Clipboard Analizer" (clipanalizer) program. Although it is buggy (crashes from time to time), when this problem occurs (that is, when Lazarus cannot read the clipboard contents, while other applications can) this program displays 0 length data for everything as can be seen in the attachment.

---

As a simpler test case, I added 3 memos, 1 button and 1 label to an empty form with the following code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   fc, i : Integer;
  4.   outstr : String;
  5. begin
  6.   if Clipboard.HasFormat(CF_TEXT) then
  7.   begin
  8.     Memo1.Text := ClipBoard.AsText;
  9.     Label1.Caption := IntToStr(Length(ClipBoard.AsText));
  10.   end
  11.   else
  12.     Label1.Caption := 'There is no text on the Clipboard';
  13.   Memo2.Text :=  PrimarySelection.AsText;
  14.   fc := ClipBoard.FormatCount;
  15.   outstr := 'FormatCount : ' + IntToStr(fc) + lineending;
  16.   for i:=1 to fc do
  17.   begin
  18.     outstr := outstr + inttostr(i) + '/' +
  19.       inttostr(ClipBoard.Formats[i].Size) + ' : ' +
  20.       ClipBoard.Formats[i].ToString + lineending;
  21.   end;
  22.   Memo3.text := outstr;
  23. end;

When this problematic case occurs; after I launch my program:
Title: Re: Unable to paste into Lazarus editor from another application
Post by: guest58172 on May 01, 2019, 07:39:20 pm
Martin, probably unrelated but this bug reminds me the fix for the null trailing character. When it's applied there is issues with copy paste from a synedit to another.

fedkad, can you try to compile Lazarus with this additional defines : GTK_REMOVE_CLIPBOARD_NULL, then test, come back an tell us if the things work better ?
Title: Re: Unable to paste into Lazarus editor from another application
Post by: Martin_fr on May 01, 2019, 08:29:49 pm
Martin, probably unrelated but this bug reminds me the fix for the null trailing character. When it's applied there is issues with copy paste from a synedit to another.
Am I missing something? I thought that had been fixed? https://bugs.freepascal.org/view.php?id=21453

IIRC (but its a bit ago), the initial fix failed, because it removed #0 from binary too. But then the fix was moved so it applies to text only. (Or rather does not add it to start with)

Revision: 58530 Author: martin Date: 15 July 2018 12:23:43
Message: Clipboard: fix trailing null issue for gtk2. Issue 0021453
Title: Re: Unable to paste into Lazarus editor from another application
Post by: guest58172 on May 01, 2019, 09:13:36 pm
Yes indeed, it's fixed. I don't know how I've managed to miss that. Nice to see the BT upgraded BTW.

fedkad: ignore my previous suggestion, it can solve nothing.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: Martin_fr on May 01, 2019, 09:50:48 pm
I dont know the 2 apps. So I can only guess...

There may be an issue somewhere deep in lcl-gtk that leads to the 0 length. I don't know much about the gtk part. So no idea.

Given that the 2nd app only shows one format by name, maybe the text is only stored in that format, and Lazarus does not support that?
Title: Re: Unable to paste into Lazarus editor from another application
Post by: fedkad on May 02, 2019, 11:07:55 am
Martin, probably unrelated but this bug reminds me the fix for the null trailing character. When it's applied there is issues with copy paste from a synedit to another.

fedkad, can you try to compile Lazarus with this additional defines : GTK_REMOVE_CLIPBOARD_NULL, then test, come back an tell us if the things work better ?

I am new to this. What I did was to add the GTK_REMOVE_CLIPBOARD_NULL define to Configure "Build Lazarus" dialog box under Defines, checked it and then did a Build (Normal IDE). To be sure I restarted Lazarus and check the build date (2019-05-02). I recompiled my test program, but nothing changed. Nothing changed in Lazarus Editor also: the same problem of not pasting anything from gedit for example.

However, I might have done something wrong with the compilation: I tried to test whether GTK_REMOVE_CLIPBOARD_NULL is defined, by inserting the following code to my test program:

Code: Pascal  [Select][+][-]
  1.   {$ifdef GTK_REMOVE_CLIPBOARD_NULL}
  2.   This should give compile time error!
  3.   {$endif}  

But the code within $if / $endif was not activated.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: JuhaManninen on May 04, 2019, 09:30:52 am
I am new to this. What I did was to add the GTK_REMOVE_CLIPBOARD_NULL define to Configure "Build Lazarus" dialog box under Defines, checked it and then did a Build (Normal IDE). To be sure I restarted Lazarus and check the build date (2019-05-02). I recompiled my test program, but nothing changed. Nothing changed in Lazarus Editor also: the same problem of not pasting anything from gedit for example.
I removed the leftover GTK_REMOVE_CLIPBOARD_NULL block from trunk r61134. It is not needed any more.

Quote
However, I might have done something wrong with the compilation: I tried to test whether GTK_REMOVE_CLIPBOARD_NULL is defined, by inserting the following code to my test program:
Code: Pascal  [Select][+][-]
  1.   {$ifdef GTK_REMOVE_CLIPBOARD_NULL}
  2.   This should give compile time error!
  3.   {$endif}  
But the code within $if / $endif was not activated.
GTK_REMOVE_CLIPBOARD_NULL was not defined in your own test program. It was only defined in Lazarus itself.
Anyway you don't need it.
Try building Lazarus clean. Strange problems often go away by doing so. Select the "Clean all" in Configure Build Lazarus dialog.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: fedkad on May 04, 2019, 12:27:31 pm
I am using the latest released version (2.0.2) of Lazarus, not the latest trunk (=beta). I will need a separate installation to test on the latest trunk.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: JuhaManninen on May 04, 2019, 01:03:59 pm
I am using the latest released version (2.0.2) of Lazarus, not the latest trunk (=beta). I will need a separate installation to test on the latest trunk.
Yes, except that you don't need to install it really. Just get the sources and run "make".
 http://wiki.freepascal.org/Getting_Lazarus#Development_version_of_Lazarus
Then start with "./lazarus", or using a separate configuration "./lazarus --pcp=~/.lazarus_trunk" or similar.
For your bug it should make no difference because my latest commit was only a cleanup, but testing trunk is recommended anyway. It is really so easy when you have FPC installed from your distro repos already.
BTW, "trunk" is not beta. It is the development version that changes by every new commit.

You wrote earlier the problem happens only with Lazarus source editor which is SynEdit. Do you use a docked IDE? It had some issues with local menus.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: Martin_fr on May 04, 2019, 04:37:28 pm
I can confirm the issue happens with trunk too.

It seems to be something in the lcl interface code. At least my guess after some debugging.

https://bugs.freepascal.org/view.php?id=35482
Title: Re: Unable to paste into Lazarus editor from another application
Post by: fedkad on May 04, 2019, 07:19:35 pm
I downloaded and compiled the latest trunk of Lazarus (61136).
(No docked IDE here as far as know. I use Lazarus as it is.)
I can confirm that this problem is present in the latest trunk too.

Please, test with Gedit as follows:

Title: Re: Unable to paste into Lazarus editor from another application
Post by: Bart on May 04, 2019, 07:22:51 pm
Works fine for me with trunk of today on Mint 18.2-64.

Bart
Title: Re: Unable to paste into Lazarus editor from another application
Post by: fedkad on May 04, 2019, 07:26:57 pm
Do you have gnome-text-editor (gedit - Version 3.32.0) installed in your system?
Please, test with it as detailed in my previous post.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: Martin_fr on May 04, 2019, 07:40:37 pm
Copy from gedit to lazarus also does not work.

I tested what happens when I copy from Lazarus to Lazarus vs gedit to lazarus.

xclip reports the same in both cases. It gets Utf8 from the clipboard (see related issue)

In both cases (as in the issue)
  IsFormatSupported =>   RequestSelectionData =>     gtk_selection_convert in
returns false.

So it falls back to the only format, that it does not test with IsFormatSupported: COMPOUND_TEXT

The difference is probably that lazarus does deliver this (on request?).
gedit obviously does not.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: simonm on November 04, 2019, 09:48:47 am
Without meaning to revive an old thread, I did run into the same problem after going to Fedora 31 where I couldn't paste from any other application into the Lazarus editor.

I recalled that Fedora runs Wayland by default and then logged out and switched to Xorg. Problem solved for me!

I can now paste from other applications into the editor. Hope this helps someone.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: Thaddy on November 04, 2019, 01:07:37 pm
Pasting from geany (on Raspbian Buster) works! which  uses wayland....

This has some interesting info https://stackoverflow.com/questions/39616066/wayland-clipboard-api  and https://wiki.gnome.org/Initiatives/Wayland/PrimarySelection
Title: Re: Unable to paste into Lazarus editor from another application
Post by: fedkad on November 04, 2019, 08:10:06 pm
Without meaning to revive an old thread, I did run into the same problem after going to Fedora 31 where I couldn't paste from any other application into the Lazarus editor.
I recalled that Fedora runs Wayland by default and then logged out and switched to Xorg. Problem solved for me!
I can now paste from other applications into the editor. Hope this helps someone.

It seems that the same thing happened here. Lately, I upgraded to Ubuntu 19.10 and it uses GNOME on Xorg by default. The problem does not occur here.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: WayneSherman on September 09, 2023, 08:57:46 am
Tested using fpc 3.2.2 and Lazarus 2.2.6 (stable / stable) on Xubuntu 20.04 and MXLinux (debian 10 / buster).

I am having this same issue with Lazarus GTK2 editors (synedit, cudatext, ATsynedit).  Clipboard.AsText sometimes does not return the UTF8_STRING clipboard contents.  Other apps like gedit, geany, xclip, and even Lazarus TMemo (gtk2) all work correctly.

I turned on clipboard debugging ({$DEFINE DEBUG_CLIPBOARD}) and captured two logging sessions.

WORKING EXAMPLE DEBUG LOG:
Using gedit, I copied the text "bla bla bla" to the clipboard and then in a simple Lazarus app call Clipboard.AsText:
Code: [Select]
[TGtk2WidgetSet.ClipboardGetData] A ClipboardWidget=000000000156E400 FormatID=text/plain Now=45177.9750512616
[RequestSelectionData] FormatID=131
[RequestSelectionData] TimeID=24436340 Type=CLIPBOARD FormatID=TARGETS
[ClipboardSelectionReceivedHandler] A TimeID=24436340 RequestIndex=0 selection=69=CLIPBOARD target=131=TARGETS theType=4=ATOM format=32 len=96
[ClipboardSelectionReceivedHandler] B DataLen=96
[ClipboardSelectionReceivedHandler] C FirstCharacter=134
IsFormatSupported A 69 00000045 SelData.Target=131 AllID=131 SelData.TheType=4 ATOM=4 Name="ATOM" SelData.Length=96 SelData.Format=32
 23 ""
 22 ""
 21 ""
 20 ""
 19 ""
 18 "GDK_SELECTION"
 17 ""
 16 ""
 15 ""
 14 ""
 13 ""
 12 ""
 11 "text/plain"
 10 "text/plain;charset=utf-8"
 9 "STRING"
 8 "TEXT"
 7 "COMPOUND_TEXT"
 6 "UTF8_STRING"
 5 "application/x-gtk-text-buffer-rich-text"
 4 "GTK_TEXT_BUFFER_CONTENTS"
 3 "SAVE_TARGETS"
 2 "MULTIPLE"
 1 "TARGETS"
 0 "TIMESTAMP"
[TGtk2WidgetSet.ClipboardGetData] B  Format=UTF8_STRING FormatAtom=71 Now=45177.9751306134
[RequestSelectionData] FormatID=71
[RequestSelectionData] TimeID=24436340 Type=CLIPBOARD FormatID=UTF8_STRING
[ClipboardSelectionReceivedHandler] A TimeID=24436340 RequestIndex=0 selection=69=CLIPBOARD target=71=UTF8_STRING theType=71=UTF8_STRING format=8 len=11
[ClipboardSelectionReceivedHandler] B DataLen=11
[ClipboardSelectionReceivedHandler] C FirstCharacter=98
[TGtk2WidgetSet.ClipboardGetData] C  Length=11 Now=45177.9751616435  SelData.Selection=69 SelData.Length=11
[TGtk2WidgetSet.ClipboardGetData] END  Now=45177.9751690278

NOT WORKING EXAMPLE DEBUG LOG:
Using Remote Viewer, I copied the text "This is a test" from a remote Windows notepad to the clipboard and then in a simple Lazarus app call Clipboard.AsText:
Code: [Select]
[TGtk2WidgetSet.ClipboardGetData] A ClipboardWidget=0000000002283400 FormatID=text/plain Now=45177.9765332523
[RequestSelectionData] FormatID=131
[RequestSelectionData] TimeID=24564910 Type=CLIPBOARD FormatID=TARGETS
[ClipboardSelectionReceivedHandler] A TimeID=24564910 RequestIndex=0 selection=69=CLIPBOARD target=131=TARGETS theType=4=ATOM format=32 len=64
[ClipboardSelectionReceivedHandler] B DataLen=64
[ClipboardSelectionReceivedHandler] C FirstCharacter=134
IsFormatSupported A 69 00000045 SelData.Target=131 AllID=131 SelData.TheType=4 ATOM=4 Name="ATOM" SelData.Length=64 SelData.Format=32
 15 ""
 14 ""
 13 ""
 12 ""
 11 ""
 10 ""
 9 ""
 8 ""
 7 "text/plain"
 6 "TEXT"
 5 "STRING"
 4 "text/plain;charset=utf-8"
 3 "UTF8_STRING"
 2 "MULTIPLE"
 1 "TARGETS"
 0 "TIMESTAMP"
[TGtk2WidgetSet.ClipboardGetData] B  Format=UTF8_STRING FormatAtom=71 Now=45177.9765464815
[RequestSelectionData] FormatID=71
[RequestSelectionData] TimeID=24564910 Type=CLIPBOARD FormatID=UTF8_STRING
[TGtk2WidgetSet.ClipboardGetData] C  Length=0 Now=45177.9765604167  SelData.Selection=0 SelData.Length=0
[TGtk2WidgetSet.ClipboardGetData] REQUESTED FORMAT NOT SUPPORTED  Length=0
[ClipboardSelectionReceivedHandler] A TimeID=24564910 RequestIndex=-1 selection=69=CLIPBOARD target=71=UTF8_STRING theType=71=UTF8_STRING format=8 len=14

In both examples, the listing of formats works (although the list is different), and both call:
Code: [Select]
[TGtk2WidgetSet.ClipboardGetData] B  Format=UTF8_STRING FormatAtom=71 Now=45177...
[RequestSelectionData] FormatID=71
[RequestSelectionData] TimeID=24436340 Type=CLIPBOARD FormatID=UTF8_STRING

Following that, in the NON working example, there seems to be a timing issue with the reception or matching of clipboard events. gtk_clipboard_wait_for_contents (https://gitlab.com/freepascal.org/lazarus/lazarus/-/blob/lazarus_2_2_6/lcl/interfaces/gtk2/gtk2proc.inc#L7541) returns before ClipboardSelectionReceivedHandler (https://gitlab.com/freepascal.org/lazarus/lazarus/-/blob/lazarus_2_2_6/lcl/interfaces/gtk2/gtk2callback.inc#L3454) processes the clipboard event and subsequently TGtk2WidgetSet.ClipboardGetData (https://gitlab.com/freepascal.org/lazarus/lazarus/-/blob/lazarus_2_2_6/lcl/interfaces/gtk2/gtk2winapi.inc#L278) thinks there is no data.  When ClipboardSelectionReceivedHandler does finally process the event it gets ignored since it is too late.
Title: Re: Unable to paste into Lazarus editor from another application
Post by: WayneSherman on September 09, 2023, 09:52:02 am
I just tested again with fpc trunk and Lazarus trunk.  This issue is fixed in trunk for my tests.

It is also fixed in the latest cudatext.  :-)
TinyPortal © 2005-2018