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:
[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:
[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:
[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.