+static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
+{
+ struct clipboard_data_instance *cdi =
+ (struct clipboard_data_instance *)data;
+ struct gui_data *inst = cdi->inst;
+
+ if (inst->current_cdi == cdi) {
+ term_deselect(inst->term);
+ inst->current_cdi = NULL;
+ }
+ sfree(cdi->pasteout_data_utf8);
+ sfree(cdi);
+}
+
+void write_clip(void *frontend, wchar_t *data, int *attr, int len,
+ int must_deselect)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ struct clipboard_data_instance *cdi;
+
+ if (inst->direct_to_font) {
+ /* In this clipboard mode, we just can't paste if we're in
+ * direct-to-font mode. Fortunately, that shouldn't be
+ * important, because we'll only use this clipboard handling
+ * code on systems where that kind of font doesn't exist
+ * anyway. */
+ return;
+ }
+
+ cdi = snew(struct clipboard_data_instance);
+ cdi->inst = inst;
+ inst->current_cdi = cdi;
+ cdi->pasteout_data_utf8 = snewn(len*6, char);
+ {
+ const wchar_t *tmp = data;
+ int tmplen = len;
+ cdi->pasteout_data_utf8_len =
+ charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
+ len*6, CS_UTF8, NULL, NULL, 0);
+ }
+
+ /*
+ * It would be nice to just call gtk_clipboard_set_text() in place
+ * of all of the faffing below. Unfortunately, that won't give me
+ * access to the clipboard-clear event, which we use to visually
+ * deselect text in the terminal.
+ */
+ {
+ GtkTargetList *targetlist;
+ GtkTargetEntry *targettable;
+ gint n_targets;
+
+ targetlist = gtk_target_list_new(NULL, 0);
+ gtk_target_list_add_text_targets(targetlist, 0);
+ targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
+ gtk_clipboard_set_with_data(inst->clipboard, targettable, n_targets,
+ clipboard_provide_data, clipboard_clear,
+ cdi);
+ gtk_target_table_free(targettable, n_targets);
+ gtk_target_list_unref(targetlist);
+ }
+}
+
+static void clipboard_text_received(GtkClipboard *clipboard,
+ const gchar *text, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ int length;
+
+ if (!text)
+ return;
+
+ length = strlen(text);
+
+ if (inst->pastein_data)
+ sfree(inst->pastein_data);
+
+ inst->pastein_data = snewn(length, wchar_t);
+ inst->pastein_data_len = mb_to_wc(CS_UTF8, 0, text, length,
+ inst->pastein_data, length);
+
+ term_do_paste(inst->term);
+}
+
+void request_paste(void *frontend)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ gtk_clipboard_request_text(inst->clipboard, clipboard_text_received, inst);
+}
+
+#else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
+
+/* ----------------------------------------------------------------------
+ * Clipboard handling for X, using the low-level gtk_selection_*
+ * interface, handling conversions to fiddly things like compound text
+ * ourselves, and storing in X cut buffers too.
+ *
+ * This version of the clipboard code has to be kept around for GTK1,
+ * which doesn't have the higher-level GtkClipboard interface at all.
+ * And since it works on GTK2 and GTK3 too and has had a good few
+ * years of shakedown and bug fixing, we might as well keep using it
+ * where it's applicable.
+ *
+ * It's _possible_ that we might be able to replicate all the
+ * important wrinkles of this code in GtkClipboard. (In particular,
+ * cut buffers or local analogue look as if they might be accessible
+ * via gtk_clipboard_set_can_store(), and delivering text in
+ * non-Unicode formats only in the direct-to-font case ought to be
+ * possible if we can figure out the right set of things to put in the
+ * GtkTargetList.) But that work can wait until there's a need for it!
+ */
+