Recent

Author Topic: Unable to paste into Lazarus editor from another application  (Read 8901 times)

fedkad

  • Full Member
  • ***
  • Posts: 176
Unable to paste into Lazarus editor from another application
« on: April 30, 2019, 11:29:57 am »
  • 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
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Unable to paste into Lazarus editor from another application
« Reply #1 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9874
  • Debugger - SynEdit - and more
    • wiki
Re: Unable to paste into Lazarus editor from another application
« Reply #2 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)


fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Unable to paste into Lazarus editor from another application
« Reply #3 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.
« Last Edit: April 30, 2019, 09:23:08 pm by fedkad »
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9874
  • Debugger - SynEdit - and more
    • wiki
Re: Unable to paste into Lazarus editor from another application
« Reply #4 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.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Unable to paste into Lazarus editor from another application
« Reply #5 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/
« Last Edit: April 30, 2019, 10:11:03 pm by wp »

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Unable to paste into Lazarus editor from another application
« Reply #6 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:
  • After the first click to button; Label1 displays 0; Memo1 and Memo2 become empty, while Memo3 displays FormatCount : 0
  • After 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
« Last Edit: May 01, 2019, 04:26:07 pm by fedkad »
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

guest58172

  • Guest
Re: Unable to paste into Lazarus editor from another application
« Reply #7 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 ?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9874
  • Debugger - SynEdit - and more
    • wiki
Re: Unable to paste into Lazarus editor from another application
« Reply #8 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

guest58172

  • Guest
Re: Unable to paste into Lazarus editor from another application
« Reply #9 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.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9874
  • Debugger - SynEdit - and more
    • wiki
Re: Unable to paste into Lazarus editor from another application
« Reply #10 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?

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Unable to paste into Lazarus editor from another application
« Reply #11 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.
« Last Edit: May 02, 2019, 08:58:56 pm by fedkad »
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: Unable to paste into Lazarus editor from another application
« Reply #12 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.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: Unable to paste into Lazarus editor from another application
« Reply #13 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.
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: Unable to paste into Lazarus editor from another application
« Reply #14 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.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018