+ struct gui_data *inst = (struct gui_data *)frontend;
+ /* This maps colour indices in cfg to those used in inst->cols. */
+ static const int ww[] = {
+ 6, 7, 8, 9, 10, 11, 12, 13,
+ 14, 15, 16, 17, 18, 19, 20, 21,
+ 0, 1, 2, 3, 4, 5
+ };
+ gboolean success[NCOLOURS];
+ int i;
+
+ assert(lenof(ww) == NCOLOURS);
+
+ if (!inst->colmap) {
+ inst->colmap = gdk_colormap_get_system();
+ } else {
+ gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
+ }
+
+ for (i = 0; i < NCOLOURS; i++) {
+ inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
+ inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
+ inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
+ }
+
+ gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
+ FALSE, FALSE, success);
+ for (i = 0; i < NCOLOURS; i++) {
+ if (!success[i])
+ g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
+ i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
+ }
+
+ set_window_background(inst);
+}
+
+void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ if (inst->pasteout_data)
+ sfree(inst->pasteout_data);
+ inst->pasteout_data = smalloc(len);
+ inst->pasteout_data_len = len;
+ wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
+ NULL, NULL);
+
+ if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
+ GDK_CURRENT_TIME)) {
+ gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
+ GDK_SELECTION_TYPE_STRING, 1);
+ gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
+ inst->compound_text_atom, 1);
+ }
+}
+
+void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
+ guint info, guint time_stamp, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
+ inst->pasteout_data, inst->pasteout_data_len);
+}
+
+gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
+ gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ term_deselect(inst->term);
+ if (inst->pasteout_data)
+ sfree(inst->pasteout_data);
+ inst->pasteout_data = NULL;
+ inst->pasteout_data_len = 0;
+ return TRUE;
+}
+
+void request_paste(void *frontend)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ /*
+ * In Unix, pasting is asynchronous: all we can do at the
+ * moment is to call gtk_selection_convert(), and when the data
+ * comes back _then_ we can call term_do_paste().
+ */
+ gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
+ GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
+}
+
+gint idle_paste_func(gpointer data); /* forward ref */
+
+void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
+ guint time, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+
+ if (seldata->length <= 0 ||
+ seldata->type != GDK_SELECTION_TYPE_STRING)
+ return; /* Nothing happens. */
+
+ if (inst->pastein_data)
+ sfree(inst->pastein_data);
+
+ inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
+ inst->pastein_data_len = seldata->length;
+ mb_to_wc(0, 0, seldata->data, seldata->length,
+ inst->pastein_data, inst->pastein_data_len);
+
+ term_do_paste(inst->term);
+
+ if (term_paste_pending(inst->term))
+ inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
+}
+
+gint idle_paste_func(gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+
+ if (term_paste_pending(inst->term))
+ term_paste(inst->term);
+ else
+ gtk_idle_remove(inst->term_paste_idle_id);
+
+ return TRUE;
+}
+
+
+void get_clip(void *frontend, wchar_t ** p, int *len)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+