Zim's clipboard function uses the gtk method Gtk.Clipboard.set_with_data(). This method was supported by the "pygtk" bindings, but is not supported in "gir" bindings which are used for the Gtk3/python3 port. The reason is that the method signature seems incompatible with the "gir" annotation.
As a result, the copying of rich content from zim to the clipboard is broken. This affects also internal copy-paste of e.g. page links. (Copying into zim should work, but tests are broken due to a lack of a way to set the clipboard ...)
A potential work around would be to use Gtk.TextBuffer serialization API. However, this is broken as well, see Gnome bug 791443 Also this serialization API is due to be removed in Gtk4, so would only be a temporary workaround.
So both methods used setting any other content on the clipboard other than plain text (or images) are broken.
For internal copy paste we can probable think of workarounds, but pasting e.g. HTML formatted text outside of zim can not be done with the available API.
What is needed to fix this is a way to directly access the Gtk.Clipboard.set_with_data() method from python. Potentially the ctypes library could be of use here, further research is needed.
Having Gtk.Clipboard.set_with_data() is blocked due to this Gtk+ bug: https://bugzilla.gnome.org/show_bug.cgi?id=656312 Update 2018-07-03: New link: https://gitlab.gnome.org/GNOME/gtk/issues/364
See also: https://gitlab.gnome.org/GNOME/pygobject/issues/133
See also: #390 , a similar issue with drag&drop.
Attempt to get this to work using ctypes here: https://gist.github.com/jaap-karssenberg/4f7d3dba7d5bc725dacf28a382893d69
Got stuck debugging this - all help is welcome to get this to work!
Pointers on ctypes and gnome introspection / to test later
https://stackoverflow.com/a/43212363
Get clipboard html
Some positive results. The original code was failing this assert
assert text == 'Test 123'
Disclaimer - I'm just dabbling into this - so: sorry if the results I got are obvious - really hoping the below actually helps.
I made some changes and I got past a few additional asserts - actually all the way except for the last one:
---- clipboard ----
Set text
TEXT> b'Test 123'
----- target entry list ----
FOUND TARGET: True
---- functions and set_with_data ----
>>> True
>>>> None
TEXT> None
Traceback (most recent call last):
File "ctypes-clipboard-zim.py", line 117, in <module>
assert text == b'Test 456'
AssertionError
I got this basically by changing some of python3 strings (and the assert comparisons) to binary, and it seems to have worked (relevant bits only), as follows
```print("---- clipboard ----")
.....
gtk_clipboard_set_text(clipboard, b'Test 123', len('Test 123'))
print("Set text")
text = gtk_clipboard_wait_for_text(clipboard)
print("TEXT>", text)
assert text == b'Test 123'
print("----- target entry list ----")
.....
libgtk.gtk_target_list_add(
.....
atom_p(b'text/plain'),
0,
.....
ok = gtk_target_list_find(targetentrylist, atom_p(b'text/plain'), None)
print("FOUND TARGET:", ok)
assert ok
print("---- functions and set_with_data ----")
GetFuncType = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
def get_func(clipboard, selection, info, data):
print("GET %r" % ((clipboard, selection, info, data),))
libgtk.gtk_selection_data_set_text(selection, b"Test 456", -1)
......
user_data = b"userdata 123"
......
print(">>>", ok)
assert ok
gtk_clipboard_wait_for_contents = c_func(libgtk, 'gtk_clipboard_wait_for_contents',
(ctypes.c_void_p, ctypes.c_void_p), res=ctypes.c_void_p)
data = gtk_clipboard_wait_for_contents(clipboard, atom_p(b'text/plain'))
.....
assert text == b'Test 456'
```
As shown by the final result there seems to be a problem with either gtk_clipboard_wait_for_contents (which is giving no result) or with gtk_clipboard_set_with_data that is not properly setting the clipboard.
Updated the gist with the bytestring. (What is weird that I had it running up to line 114 before, but now can't reproduce - did a system update in between, so something in string handling in ctypes changed!?)
My guess is that the set_with_data is still not taking effect because wait_for_text works in the first part of the script. Also the "GET" message is not printed.
It is a shame they made this API gone. Since it is a useful feature, I have prepared a plugin to allow rich copy text. Since it probably works on Linux only, I will not push this plugin to the official repository here. (However it would have looked better to have menu items "Edit / Copy, Paste" enriched with "Edit / Copy, Copy Rich HTML, Paste" ordered; now the item "Copy Rich HTML" will be placed at the end of the "Edit" menu.)
In case anyone is missing the feature as I did, they may install the plugin unless GTK bug is solved.
https://github.com/e3rd/zim-plugin-copy-rich
Most helpful comment
It is a shame they made this API gone. Since it is a useful feature, I have prepared a plugin to allow rich copy text. Since it probably works on Linux only, I will not push this plugin to the official repository here. (However it would have looked better to have menu items "Edit / Copy, Paste" enriched with "Edit / Copy, Copy Rich HTML, Paste" ordered; now the item "Copy Rich HTML" will be placed at the end of the "Edit" menu.)
In case anyone is missing the feature as I did, they may install the plugin unless GTK bug is solved.
https://github.com/e3rd/zim-plugin-copy-rich