Unable to paste into Lazarus editor from another application lazarus / 9-Editor ---------------------------------------------------------------------- fedkad · 2019-04-30 09:29 Copy and paste within Lazarus editor works.Copy from Lazarus editor and paste into another application (like gedit) works.Copy from another application (like Firefox, gedit, etc.) and paste into Lazarus editor most of the time does not work. 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 ---------------------------------------------------------------------- lucamar · 2019-04-30 09:48 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. ---------------------------------------------------------------------- Martin_fr · 2019-04-30 10:16 Can you test with a new project? - Add a memo and a button to a form. - In the button OnClick do Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Memo1.Text := ClipBoard.AsText You need Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PrimarySelection.AsText And for good measure see what SecondarySelection does (if anything) ---------------------------------------------------------------------- fedkad · 2019-04-30 19:16 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. ---------------------------------------------------------------------- Martin_fr · 2019-04-30 19:42 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. ---------------------------------------------------------------------- wp · 2019-04-30 20:04 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/ ---------------------------------------------------------------------- fedkad · 2019-05-01 12:43 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  fc, i : Integer;  outstr : String;begin  if Clipboard.HasFormat(CF_TEXT) then  begin    Memo1.Text := ClipBoard.AsText;    Label1.Caption := IntToStr(Length(ClipBoard.AsText));  end  else    Label1.Caption := 'There is no text on the Clipboard';  Memo2.Text :=  PrimarySelection.AsText;  fc := ClipBoard.FormatCount;  outstr := 'FormatCount : ' + IntToStr(fc) + lineending;  for i:=1 to fc do  begin    outstr := outstr + inttostr(i) + '/' +      inttostr(ClipBoard.Formats[i].Size) + ' : ' +      ClipBoard.Formats[i].ToString + lineending;  end;  Memo3.text := outstr;end; When this problematic case occurs; after I launch my program: After the first click to button; Label1 displays 0; Memo1 and Memo2 become empty, while Memo3 displays FormatCount : 0After the second click to the button (without doing anything else on the system): Label1 displays There is no text on the Clipboard; Memo1 is untouched, Memo2 becomes empty, while Memo3 displays FormatCount : 0 ---------------------------------------------------------------------- guest58172 · 2019-05-01 17:39 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 ? ---------------------------------------------------------------------- Martin_fr · 2019-05-01 18:29 Quote from: Basile-B on May 01, 2019, 07:39:20 pmMartin, 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 ---------------------------------------------------------------------- guest58172 · 2019-05-01 19:13 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. ---------------------------------------------------------------------- Martin_fr · 2019-05-01 19:50 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? ---------------------------------------------------------------------- fedkad · 2019-05-02 09:07 Quote from: Basile-B on May 01, 2019, 07:39:20 pmMartin, 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   {$ifdef GTK_REMOVE_CLIPBOARD_NULL}  This should give compile time error!  {$endif} But the code within $if / $endif was not activated. ---------------------------------------------------------------------- JuhaManninen · 2019-05-04 07:30 Quote from: fedkad on May 02, 2019, 11:07:55 amI 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. QuoteHowever, 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   {$ifdef GTK_REMOVE_CLIPBOARD_NULL}  This should give compile time error!  {$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. ---------------------------------------------------------------------- fedkad · 2019-05-04 10:27 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. ---------------------------------------------------------------------- JuhaManninen · 2019-05-04 11:03 Quote from: fedkad on May 04, 2019, 12:27:31 pmI 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_Laz arus 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. ---------------------------------------------------------------------- Martin_fr · 2019-05-04 14:37 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 ---------------------------------------------------------------------- fedkad · 2019-05-04 17:19 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: Open Gedit. Enter some text. Select some text in Gedit. Copy to Clipboard.Switch to Lazarus editor or a Lazarus program that uses ClipBoard.AsText to import some text from Clipboard.The first try will return True for Clipboard.HasFormat(CF_TEXT), but a 0 length string for ClipBoard.AsText.The second try will return False for Clipboard.HasFormat(CF_TEXT). ---------------------------------------------------------------------- Bart · 2019-05-04 17:22 Works fine for me with trunk of today on Mint 18.2-64. Bart ---------------------------------------------------------------------- fedkad · 2019-05-04 17:26 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. ---------------------------------------------------------------------- Martin_fr · 2019-05-04 17:40 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. ---------------------------------------------------------------------- simonm · 2019-11-04 08:48 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. ---------------------------------------------------------------------- Thaddy · 2019-11-04 12:07 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 ---------------------------------------------------------------------- fedkad · 2019-11-04 19:10 Quote from: simonm on November 04, 2019, 09:48:47 amWithout 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. ---------------------------------------------------------------------- WayneSherman · 2023-09-09 06:57 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 returns before ClipboardSelectionReceivedHandler processes the clipboard event and subsequently TGtk2WidgetSet.ClipboardGetData thinks there is no data.  When ClipboardSelectionReceivedHandler does finally process the event it gets ignored since it is too late. ---------------------------------------------------------------------- WayneSherman · 2023-09-09 07:52 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.  :-)