+
+enum {
+ SELECT_FMT_NULL,
+ SELECT_FMT_TEXT,
+ SELECT_FMT_UTF8,
+ SELECT_FMT_HTML,
+ SELECT_FMT_ODT
+};
+
+/* GNU Hurd doesn't have PATH_MAX. Use a fallback.
+ Temporary directory names are usually not that long. */
+#ifndef PATH_MAX
+# define PATH_MAX 1024
+#endif
+
+static void
+clipboard_get_cb (GtkClipboard *clipboard,
+ GtkSelectionData *selection_data,
+ guint info,
+ gpointer data)
+{
+ PsppireOutputWindow *window = data;
+
+ gsize length;
+ gchar *text = NULL;
+ struct output_driver *driver = NULL;
+ char dirname[PATH_MAX], *filename;
+ struct string_map options;
+
+ GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
+ GtkTreeModel *model = gtk_tree_view_get_model (window->overview);
+
+ GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
+ GList *n = rows;
+
+ if ( n == NULL)
+ return;
+
+ if (path_search (dirname, sizeof dirname, NULL, NULL, true)
+ || mkdtemp (dirname) == NULL)
+ {
+ msg_error (errno, _("failed to create temporary directory during clipboard operation"));
+ return;
+ }
+ filename = xasprintf ("%s/clip.tmp", dirname);
+
+ string_map_init (&options);
+ string_map_insert (&options, "output-file", filename);
+
+ switch (info)
+ {
+ case SELECT_FMT_UTF8:
+ string_map_insert (&options, "box", "unicode");
+ /* fall-through */
+
+ case SELECT_FMT_TEXT:
+ string_map_insert (&options, "format", "txt");
+ break;
+
+ case SELECT_FMT_HTML:
+ string_map_insert (&options, "format", "html");
+ string_map_insert (&options, "borders", "false");
+ string_map_insert (&options, "css", "false");
+ break;
+
+ case SELECT_FMT_ODT:
+ string_map_insert (&options, "format", "odt");
+ break;
+
+ default:
+ g_warning ("unsupported clip target\n");
+ goto finish;
+ break;
+ }
+
+ driver = output_driver_create (&options);
+ if (driver == NULL)
+ goto finish;
+
+ while (n)
+ {
+ GtkTreePath *path = n->data ;
+ GtkTreeIter iter;
+ struct output_item *item ;
+
+ gtk_tree_model_get_iter (model, &iter, path);
+ gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
+
+ driver->class->submit (driver, item);
+
+ n = n->next;
+ }
+
+ if ( driver->class->flush)
+ driver->class->flush (driver);
+
+
+ /* Some drivers (eg: the odt one) don't write anything until they
+ are closed */
+ output_driver_destroy (driver);
+ driver = NULL;
+
+ if ( g_file_get_contents (filename, &text, &length, NULL) )
+ {
+ gtk_selection_data_set (selection_data, selection_data->target,
+ 8,
+ (const guchar *) text, length);
+ }
+
+ finish:
+
+ if (driver != NULL)
+ output_driver_destroy (driver);
+
+ g_free (text);
+
+ unlink (filename);
+ free (filename);
+ rmdir (dirname);
+
+ g_list_free (rows);
+}
+
+static void
+clipboard_clear_cb (GtkClipboard *clipboard,
+ gpointer data)
+{
+}
+
+static const GtkTargetEntry targets[] = {
+
+ { "STRING", 0, SELECT_FMT_TEXT },
+ { "TEXT", 0, SELECT_FMT_TEXT },
+ { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
+ { "text/plain", 0, SELECT_FMT_TEXT },
+
+ { "UTF8_STRING", 0, SELECT_FMT_UTF8 },
+ { "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 },
+
+ { "text/html", 0, SELECT_FMT_HTML },
+
+ { "application/vnd.oasis.opendocument.text", 0, SELECT_FMT_ODT }
+};
+
+static void
+on_copy (PsppireOutputWindow *window)
+{
+ {
+ GtkClipboard *clipboard =
+ gtk_widget_get_clipboard (GTK_WIDGET (window),
+ GDK_SELECTION_CLIPBOARD);
+
+ if (!gtk_clipboard_set_with_data (clipboard, targets,
+ G_N_ELEMENTS (targets),
+ clipboard_get_cb, clipboard_clear_cb,
+ window))
+
+ clipboard_clear_cb (clipboard, window);
+ }
+}
+
+static void
+on_selection_change (GtkTreeSelection *sel, GtkAction *copy_action)
+{
+ /* The Copy action is available only if there is something selected */
+ gtk_action_set_sensitive (copy_action, gtk_tree_selection_count_selected_rows (sel) > 0);
+}
+
+static void
+on_select_all (PsppireOutputWindow *window)
+{
+ GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
+ gtk_tree_view_expand_all (window->overview);
+ gtk_tree_selection_select_all (sel);
+}
+
+
+static void
+copy_base_to_bg (GtkWidget *dest, GtkWidget *src)
+{
+ int i;
+ for (i = 0; i < 5; ++i)
+ {
+ GdkColor *col = >k_widget_get_style (src)->base[i];
+ gtk_widget_modify_bg (dest, i, col);
+
+ col = >k_widget_get_style (src)->text[i];
+ gtk_widget_modify_fg (dest, i, col);
+ }
+}
+
+static void
+on_dwgarea_realize (GtkWidget *dwg_area, gpointer data)
+{
+ GtkWidget *viewer = GTK_WIDGET (data);
+
+ copy_base_to_bg (dwg_area, viewer);
+}
+
+
+static void
+psppire_output_window_style_set (GtkWidget *w, GtkStyle *prev)
+{
+ GtkWidget *op = GTK_WIDGET (PSPPIRE_OUTPUT_WINDOW (w)->output);
+
+ /* Copy the base style from the parent widget to the container and
+ all its children.
+ We do this, because the container's primary purpose is to
+ display text. This way psppire appears to follow the chosen
+ gnome theme.
+ */
+ copy_base_to_bg (op, w);
+ gtk_container_foreach (GTK_CONTAINER (op), (GtkCallback) copy_base_to_bg,
+ PSPPIRE_OUTPUT_WINDOW (w)->output);
+
+ /* Chain up to the parent class */
+ GTK_WIDGET_CLASS (parent_class)->style_set (w, prev);
+}
+