Developing a cross-platform application would never be easy job, but I consider GTK# framework as one of the solid options to provide cross-platform UI, though the lack of documentation is embarassing sometimes. In this post I want to show an example of using
Gtk.Clipboard object to get HTML markup from the clipboard.
GTK# documentation and examples do not provide excessive information about using
Gtk.Clipboard object to get various types of content. To get text from clipboard, the
Gtk.Clipboard.WaitForText() method could be used, but how to get, by example, HTML table from LibreOffice Writer document or formatted text from webpage?
In this solution we:
- Create target using
Gdk.Atom.Intern()method, passing MIME type string
- Get selection object by using
Gtk.Clipboard.WaitForContents()method for created target.
- If the selection is not null, then it’s
Dataproperty generally contains UTF-8 encoded string with HTML markup.
The solution is currently tested only on Linux with Mono 3.10.
Gtk.Clipboard.WaitForTargets() method in the GTK# 2.12 seems to have broken interface. It defined as
bool WaitForTargets (Gdk.Atom targets, out int n_targets), but should be something like
bool WaitForTargets (Gdk.Atom  targets, out int n_targets) to return array of available targets. As a result, we can’t see what targets are available in the clipboard, but only a number of them:
More to Explore
We got the basic example to work, but many things are need to be explored:
- Primary: the selection data encoding seems to not always UTF-8, so we need a way to determine the encoding.
- Which other common MIME types are supported by
- How this code will behave on the different platforms?