Documentation/Maemo 5 Developer Guide/Using Data Sharing/Clipboard Usage

In Maemo, there is a number of clipboard enhancements to the X clipboard and Gtk+, in order to

  • Support retaining the clipboard data when applications that own the clipboard exit.
  • Be able to copy and paste rich text data between GTK+ text views in different applications.
  • Provide a generally more pleasant user experience; make it easy for application developers to gray out ``Paste menu items when the clipboard data format is not supported by the application.

This section uses the following example code:

[edit] GtkClipboard API changes

gboolean gtk_clipboard_set_can_store (GtkClipboard   *clipboard
                                      GtkTargetEntry *targets,
                                      gint            n_targets);

This function sets what data targets the current clipboard owner can transfer to the clipboard manager. NULL can be passed as targets, together with 0 as n_targets to indicate that all targets can be transferred.

When the clipboard owner changes, these values are reset.

void gtk_clipboard_store (GtkClipboard *clipboard);

This function tells the clipboard to try and store the contents of the targets specified using gtk_clipboard_set_can_store. If no such call has been made, or if there is no clipboard manager around, this function is simply a no-op.

Applications can call this function when exiting, but it is called automatically, when the application is quitting, if quitting with gtk_main_quit(). If the application is not the owner of the clipboard, the function will simply be a no-op.

In addition, adding a convenience function for finding out if a target is supported (in order to be able to gray out ``Paste items, if none of the existing clipboard targets are supported)

gboolean gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
                                                 GdkAtom       target);

[edit] GtkTextBuffer API changes

In order to support rich text copy and paste, some new functions were introduced:

gtk_text_buffer_set_enable_paste_rich_text (GtkTextBuffer *buffer,
                                            gboolean       can_paste_rich_text);
gtk_text_buffer_get_enable_paste_rich_text (GtkTextBuffer *buffer);

The setter function toggles, whether it should be possible to paste rich text in a text buffer.

To prevent applications from getting confused, when text with unexpected tags is pasted to a buffer, the notion of rich text format was added:

gtk_text_buffer_set_rich_text_format (GtkTextBuffer *buffer,
                                      const gchar   *format);
gtk_text_buffer_get_rich_text_format (GtkTextBuffer *buffer);

When a buffer has a certain text format, it can only paste rich text from buffers that have the same text format. If the formats differ, only plain text will be pasted. If a buffer has its format set to NULL, it means that it can paste from any format. For example, a format called html could include the tags bold, italic etc. Thus, it would only be possible to paste text from buffers having the same format specified.

N.B. The string is just an identifier. It is up to the application developers to make sure that when specifying an application as supporting a certain format, also the tags in the buffer are specified for that format.

For further details, MaemoPad source code is a good example to study.