cairo: Use pango_cairo_context_set_resolution() to get font sizes correct.
[pspp] / src / ui / gui / psppire-output-view.c
index 2ac85a52ada2e3418963a86fbab8cb6718fd5865..626d52e07e710c525a31c21e3eb0a959ec970582 100644 (file)
 
 #include "ui/gui/psppire-output-view.h"
 
+#include <cairo/cairo-svg.h>
 #include <errno.h>
 #include <stdbool.h>
 
+#if HAVE_RSVG
+#include "librsvg/rsvg.h"
+#endif
 #include "libpspp/assertion.h"
 #include "libpspp/string-map.h"
+#include "output/cairo-fsm.h"
 #include "output/cairo.h"
 #include "output/driver-provider.h"
 #include "output/driver.h"
@@ -36,7 +41,7 @@
 
 #include "gl/c-xvasprintf.h"
 #include "gl/minmax.h"
-#include "gl/tmpdir.h"
+#include "gl/clean-temp.h"
 #include "gl/xalloc.h"
 
 #include <gettext.h>
@@ -50,22 +55,24 @@ struct output_view_item
 
 struct psppire_output_view
   {
-    struct xr_driver *xr;
-    int font_height;
+    struct xr_fsm_style *style;
+    int object_spacing;
 
     GtkLayout *output;
     int render_width;
     int max_width;
     glong y;
 
-    struct string_map render_opts;
     GtkTreeView *overview;
     GtkTreePath *cur_group;
 
     GtkWidget *toplevel;
 
+    guint buttontime; /* Time of the button event */
+
     struct output_view_item *items;
     size_t n_items, allocated_items;
+    struct output_view_item *selected_item;
 
     /* Variables pertaining to printing */
     GtkPrintSettings *print_settings;
@@ -83,21 +90,21 @@ enum
     N_COLS
   };
 
+static GtkTargetList *build_target_list (const struct output_item *item);
+static void clipboard_get_cb (GtkClipboard     *clipboard,
+                             GtkSelectionData *selection_data,
+                             guint             info,
+                             gpointer          data);
+
 /* Draws a white background on the GtkLayout to match the white background of
    each of the output items. */
 static gboolean
 layout_draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
 {
-  cairo_save (cr);
-
   int width = gtk_widget_get_allocated_width (widget);
   int height = gtk_widget_get_allocated_height (widget);
-  cairo_rectangle (cr, 0, 0, width, height);
-  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
-  cairo_fill (cr);
-
-  cairo_restore (cr);
-
+  GtkStyleContext *context = gtk_widget_get_style_context (widget);
+  gtk_render_background (context, cr, 0, 0, width, height);
   return FALSE;                 /* Continue drawing the GtkDrawingAreas. */
 }
 
@@ -108,80 +115,60 @@ draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
   if (!gdk_cairo_get_clip_rectangle (cr, &clip))
     return TRUE;
 
-  struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering");
-  xr_rendering_draw (r, cr, clip.x, clip.y,
-                     clip.x + clip.width, clip.y + clip.height);
+  struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
+
+  /* Draw the background based on the state of the widget
+     which can be selected or not selected */
+  GtkStyleContext *context = gtk_widget_get_style_context (widget);
+  gtk_render_background (context, cr, clip.x, clip.y,
+                        clip.x + clip.width, clip.y + clip.height);
+  /* Select the default foreground color based on current style
+     and state of the widget */
+  GtkStateFlags state = gtk_widget_get_state_flags (widget);
+  GdkRGBA color;
+  gtk_style_context_get_color (context, state, &color);
+  cairo_set_source_rgba (cr, color.red, color.green, color.blue, color.alpha);
+  xr_fsm_draw_region (fsm, cr, clip.x, clip.y, clip.width, clip.height);
+
   return TRUE;
 }
 
 static void
-free_rendering (gpointer rendering_)
+free_fsm (gpointer fsm_)
 {
-  struct xr_rendering *rendering = rendering_;
-  xr_rendering_destroy (rendering);
+  struct xr_fsm *fsm = fsm_;
+  xr_fsm_destroy (fsm);
 }
 
-static void
-get_xr_options (struct psppire_output_view *view, struct string_map *options)
+static struct xr_fsm_style *
+get_xr_fsm_style (struct psppire_output_view *view)
 {
-  string_map_clear (options);
-
   GtkStyleContext *context
     = gtk_widget_get_style_context (GTK_WIDGET (view->output));
   GtkStateFlags state = gtk_widget_get_state_flags (GTK_WIDGET (view->output));
 
-  /* Use GTK+ default font as proportional font. */
-  PangoFontDescription *font_desc;
-  gtk_style_context_get (context, state, "font", &font_desc, NULL);
-  char *font_name = pango_font_description_to_string (font_desc);
-  string_map_insert (options, "prop-font", font_name);
-  g_free (font_name);
-
-  /* Derived emphasized font from proportional font. */
-  pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
-  font_name = pango_font_description_to_string (font_desc);
-  string_map_insert (options, "emph-font", font_name);
-  g_free (font_name);
-  pango_font_description_free (font_desc);
-
-  /* Pretend that the "page" has a reasonable width and a very big length,
-     so that most tables can be conveniently viewed on-screen with vertical
-     scrolling only.  (The length should not be increased very much because
-     it is already close enough to INT_MAX when expressed as thousands of a
-     point.) */
-  string_map_insert_nocopy (options, xstrdup ("paper-size"),
-                            xasprintf ("%dx1000000pt", view->render_width));
-  string_map_insert (options, "left-margin", "0");
-  string_map_insert (options, "right-margin", "0");
-  string_map_insert (options, "top-margin", "0");
-  string_map_insert (options, "bottom-margin", "0");
-}
-
-static void
-create_xr (struct psppire_output_view *view)
-{
-  get_xr_options (view, &view->render_opts);
-
-  struct string_map options;
-  string_map_clone (&options, &view->render_opts);
-
-  GdkWindow *win = gtk_layout_get_bin_window (view->output);
-  cairo_region_t *region = gdk_window_get_visible_region (win);
-  GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
-  cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
-
-  view->xr = xr_driver_create (cr, &options);
-  string_map_destroy (&options);
-
-  struct text_item *text_item = text_item_create (TEXT_ITEM_LOG, "X");
-  struct xr_rendering *r
-    = xr_rendering_create (view->xr, text_item_super (text_item), cr);
-  xr_rendering_measure (r, NULL, &view->font_height);
-  xr_rendering_destroy (r);
-  text_item_unref (text_item);
+  int xr_width = view->render_width * 1000;
+
+  PangoFontDescription *pf;
+  gtk_style_context_get (context, state, "font", &pf, NULL);
+  PangoFontDescription *ff = pango_font_description_from_string ("Monospace");
+  pango_font_description_set_size (ff, pango_font_description_get_size (pf));
+
+  struct xr_fsm_style *style = xmalloc (sizeof *style);
+  *style = (struct xr_fsm_style) {
+    .ref_cnt = 1,
+    .size = { [TABLE_HORZ] = xr_width, [TABLE_VERT] = INT_MAX },
+    .min_break = { [TABLE_HORZ] = xr_width / 2, [TABLE_VERT] = 0 },
+    .fonts = {
+      [XR_FONT_PROPORTIONAL] = pf,
+      [XR_FONT_FIXED] = ff,
+    },
+    .use_system_colors = true,
+    .transparent = true,
+    .font_resolution = 96.0,
+  };
 
-  gdk_window_end_draw_frame (win, ctx);
-  cairo_region_destroy (region);
+  return style;
 }
 
 /* Return the horizontal position to place a widget whose
@@ -196,14 +183,119 @@ get_xpos (const struct psppire_output_view *view, gint child_width)
   return (gtk_widget_get_direction (GTK_WIDGET (view->output)) ==  GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter;
 }
 
+static struct output_view_item *
+find_selected_item (struct psppire_output_view *view)
+{
+  struct output_view_item *item = NULL;
+  if (view == NULL)
+    return NULL;
+  if (view->items == NULL)
+    return NULL;
+
+  for (item = view->items; item < &view->items[view->n_items]; item++)
+    {
+      GtkWidget *widget = GTK_WIDGET (item->drawing_area);
+      if (GTK_IS_WIDGET (widget))
+        {
+         GtkStateFlags state = gtk_widget_get_state_flags (widget);
+         if (state & GTK_STATE_FLAG_SELECTED)
+           return item;
+       }
+    }
+  return NULL;
+}
+
+
+static void
+set_copy_action (struct psppire_output_view *view,
+                gboolean state)
+{
+  GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (view->output));
+  GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
+                                                    "copy");
+  g_object_set (copy_action,
+               "enabled", state,
+               NULL);
+}
+
+static void
+clear_selection (struct psppire_output_view *view)
+{
+  if (view == NULL)
+    return;
+  struct output_view_item *item = find_selected_item (view);
+  if (item == NULL)
+    return;
+  set_copy_action (view, FALSE);
+  GtkWidget *widget = GTK_WIDGET (item->drawing_area);
+  if (GTK_IS_WIDGET (widget))
+    {
+      gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_SELECTED);
+      gtk_widget_queue_draw (widget);
+    }
+}
+
+static gboolean
+off_item_button_press_event_cb (GtkWidget      *widget,
+                               GdkEventButton *event,
+                               struct psppire_output_view *view)
+{
+  /* buttontime is set by button_press_event_cb
+     If our event->time is equal to the time from the
+     button_press_event_cb, then we handle the same event.
+     In that case we must not clear the selection because
+     it was just set by button_press_event_cb from the item */
+  if (event->time != view->buttontime)
+    clear_selection (view);
+  return FALSE; /* Forward the event -> DragNDrop */
+}
+
+static gboolean
+button_press_event_cb (GtkWidget      *widget,
+                      GdkEventButton *event,
+                      struct psppire_output_view *view)
+{
+  view->buttontime = event->time;
+  clear_selection (view);
+  set_copy_action (view, TRUE);
+  gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
+  gtk_widget_queue_draw (widget);
+  return FALSE; /* Forward Event -> off_item will trigger */
+}
+
+static void
+drag_data_get_cb (GtkWidget *widget, GdkDragContext *context,
+                 GtkSelectionData *selection_data,
+                 guint target_type, guint time,
+                 struct psppire_output_view *view)
+{
+  view->selected_item = find_selected_item (view);
+  clipboard_get_cb (NULL, selection_data, target_type, view);
+}
+
 static void
 create_drawing_area (struct psppire_output_view *view,
-                     GtkWidget *drawing_area, struct xr_rendering *r,
-                     int tw, int th)
+                     GtkWidget *drawing_area, struct xr_fsm *r,
+                     int tw, int th, const struct output_item *item)
 {
   g_object_set_data_full (G_OBJECT (drawing_area),
-                          "rendering", r, free_rendering);
-
+                          "fsm", r, free_fsm);
+  g_signal_connect (drawing_area, "button-press-event",
+                   G_CALLBACK (button_press_event_cb), view);
+  gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
+
+  { /* Drag and Drop */
+    GtkTargetList *tl = build_target_list (item);
+    g_assert (tl);
+    gtk_drag_source_set (drawing_area, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
+    gtk_drag_source_set_target_list (drawing_area, tl);
+    gtk_target_list_unref (tl);
+    g_signal_connect (drawing_area, "drag-data-get",
+                     G_CALLBACK (drag_data_get_cb), view);
+  }
+  GtkStyleContext *context = gtk_widget_get_style_context (drawing_area);
+  gtk_style_context_add_class (context,
+                              GTK_STYLE_CLASS_VIEW);
   g_signal_connect (drawing_area, "draw",
                     G_CALLBACK (draw_callback), view);
 
@@ -224,8 +316,8 @@ rerender (struct psppire_output_view *view)
   if (!view->n_items || ! gdkw)
     return;
 
-  if (view->xr == NULL)
-    create_xr (view);
+  if (!view->style)
+    view->style = get_xr_fsm_style (view);
 
   GdkWindow *win = gtk_layout_get_bin_window (view->output);
   cairo_region_t *region = gdk_window_get_visible_region (win);
@@ -236,36 +328,36 @@ rerender (struct psppire_output_view *view)
   view->max_width = 0;
   for (item = view->items; item < &view->items[view->n_items]; item++)
     {
-      struct xr_rendering *r;
+      struct xr_fsm *r;
       GtkAllocation alloc;
       int tw, th;
 
       if (view->y > 0)
-        view->y += view->font_height / 2;
+        view->y += view->object_spacing;
 
       if (is_group_open_item (item->item))
         continue;
 
-      r = xr_rendering_create (view->xr, item->item, cr);
+      r = xr_fsm_create (item->item, view->style, cr);
       if (r == NULL)
         {
           g_warn_if_reached ();
           continue;
         }
 
-      xr_rendering_measure (r, &tw, &th);
+      xr_fsm_measure (r, cr, &tw, &th);
 
       gint xpos = get_xpos (view, tw);
 
       if (!item->drawing_area)
         {
           item->drawing_area = gtk_drawing_area_new ();
-          create_drawing_area (view, item->drawing_area, r, tw, th);
+          create_drawing_area (view, item->drawing_area, r, tw, th, item->item);
         }
       else
         {
           g_object_set_data_full (G_OBJECT (item->drawing_area),
-                                  "rendering", r, free_rendering);
+                                  "fsm", r, free_fsm);
           gtk_widget_set_size_request (item->drawing_area, tw, th);
           gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
         }
@@ -294,13 +386,14 @@ rerender (struct psppire_output_view *view)
     }
 
   gtk_layout_set_size (view->output,
-                       view->max_width + view->font_height,
-                       view->y + view->font_height);
+                       view->max_width + view->object_spacing,
+                       view->y + view->object_spacing);
 
   gdk_window_end_draw_frame (win, ctx);
   cairo_region_destroy (region);
 }
 
+
 void
 psppire_output_view_put (struct psppire_output_view *view,
                          const struct output_item *item)
@@ -344,17 +437,17 @@ psppire_output_view_put (struct psppire_output_view *view,
     {
       view_item->drawing_area = drawing_area = gtk_drawing_area_new ();
 
-      if (view->xr == NULL)
-        create_xr (view);
+      if (!view->style)
+        view->style = get_xr_fsm_style (view);
 
       cairo_region_t *region = gdk_window_get_visible_region (win);
       GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
       cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
 
       if (view->y > 0)
-        view->y += view->font_height / 2;
+        view->y += view->object_spacing;
 
-      struct xr_rendering *r = xr_rendering_create (view->xr, item, cr);
+      struct xr_fsm *r = xr_fsm_create (item, view->style, cr);
       if (r == NULL)
        {
          gdk_window_end_draw_frame (win, ctx);
@@ -362,9 +455,8 @@ psppire_output_view_put (struct psppire_output_view *view,
          return;
        }
 
-      xr_rendering_measure (r, &tw, &th);
-
-      create_drawing_area (view, drawing_area, r, tw, th);
+      xr_fsm_measure (r, cr, &tw, &th);
+      create_drawing_area (view, drawing_area, r, tw, th, item);
       gdk_window_end_draw_frame (win, ctx);
       cairo_region_destroy (region);
     }
@@ -486,18 +578,16 @@ on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view)
     return;
 
   /* GTK+ fires this signal for trivial changes like the mouse moving in or out
-     of the window.  Check whether the actual rendering options changed and
+     of the window.  Check whether the actual fsm options changed and
      re-render only if they did. */
-  struct string_map options = STRING_MAP_INITIALIZER (options);
-  get_xr_options (view, &options);
-  if (!string_map_equals (&options, &view->render_opts))
+  struct xr_fsm_style *style = get_xr_fsm_style (view);
+  if (!view->style || !xr_fsm_style_equals (style, view->style))
     {
-      xr_driver_destroy (view->xr);
-      view->xr = NULL;
-
+      xr_fsm_style_unref (view->style);
+      view->style = xr_fsm_style_ref (style);
       rerender (view);
     }
-  string_map_destroy (&options);
+  xr_fsm_style_unref (style);
 }
 
 enum {
@@ -505,14 +595,38 @@ enum {
   SELECT_FMT_TEXT,
   SELECT_FMT_UTF8,
   SELECT_FMT_HTML,
+  SELECT_FMT_SVG,
+  SELECT_FMT_IMG,
   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
+/* Returns a pixbuf from a svg file      */
+/* You must unref the pixbuf after usage */
+static GdkPixbuf *
+derive_pixbuf_from_svg (const char *filename)
+{
+  GError *err = NULL;
+  GdkPixbuf *pixbuf = NULL;
+#if HAVE_RSVG
+  RsvgHandle *handle = rsvg_handle_new_from_file (filename, &err);
+  if (err == NULL)
+    {
+      rsvg_handle_set_dpi (handle, 300.0);
+      pixbuf = rsvg_handle_get_pixbuf (handle);
+      g_object_unref (handle);
+    }
+#else
+  pixbuf = gdk_pixbuf_new_from_file (filename, &err);
 #endif
+  if (err != NULL)
+    {
+      msg (ME, _("Could not open file %s during copy operation: %s"),
+          filename, err->message);
+      g_error_free (err);
+      return NULL;
+    }
+  return pixbuf;
+}
 
 static void
 clipboard_get_cb (GtkClipboard     *clipboard,
@@ -525,25 +639,20 @@ clipboard_get_cb (GtkClipboard     *clipboard,
   gsize length;
   gchar *text = NULL;
   struct output_driver *driver = NULL;
-  char dirname[PATH_MAX], *filename;
+  char *filename;
   struct string_map options;
+  struct temp_dir *td = NULL;
 
-  GtkTreeSelection *sel = gtk_tree_view_get_selection (view->overview);
-  GtkTreeModel *model = gtk_tree_view_get_model (view->overview);
-
-  GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
-  GList *n = rows;
-
-  if (n == NULL)
+  if (view->selected_item == NULL)
     return;
 
-  if (path_search (dirname, sizeof dirname, NULL, NULL, true)
-      || mkdtemp (dirname) == NULL)
+  td = create_temp_dir ("pspp", NULL, false);
+  if (td == NULL)
     {
       msg_error (errno, _("failed to create temporary directory during clipboard operation"));
       return;
     }
-  filename = xasprintf ("%s/clip.tmp", dirname);
+  filename = xasprintf ("%s/clip.tmp", td->dir_name);
 
   string_map_init (&options);
   string_map_insert (&options, "output-file", filename);
@@ -564,6 +673,11 @@ clipboard_get_cb (GtkClipboard     *clipboard,
       string_map_insert (&options, "css", "false");
       break;
 
+    case SELECT_FMT_SVG:
+    case SELECT_FMT_IMG:
+      /* see below */
+      break;
+
     case SELECT_FMT_ODT:
       string_map_insert (&options, "format", "odt");
       break;
@@ -574,39 +688,64 @@ clipboard_get_cb (GtkClipboard     *clipboard,
       break;
     }
 
-  driver = output_driver_create (&options);
-  if (driver == NULL)
-    goto finish;
-
-  while (n)
+  if ((info == SELECT_FMT_IMG) ||
+      (info == SELECT_FMT_SVG) )
     {
-      GtkTreePath *path = n->data ;
-      GtkTreeIter iter;
-      struct output_item *item ;
+      GtkWidget *widget = view->selected_item->drawing_area;
+      struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
 
-      gtk_tree_model_get_iter (model, &iter, path);
-      gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
+      GdkWindow *win = gtk_layout_get_bin_window (view->output);
+      cairo_region_t *region = gdk_window_get_visible_region (win);
+      GdkDrawingContext *ctx =  gdk_window_begin_draw_frame (win, region);
+      cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
+
+      int w, h;
+      xr_fsm_measure (fsm, cr, &w, &h);
 
-      driver->class->submit (driver, item);
+      gdk_window_end_draw_frame (win, ctx);
+      cairo_region_destroy (region);
 
-      n = n->next;
+      cairo_surface_t *surface = cairo_svg_surface_create (filename, w, h);
+      if (surface)
+        {
+          cairo_t *cr = cairo_create (surface);
+          xr_fsm_draw_all (fsm, cr);
+          cairo_destroy (cr);
+          cairo_surface_destroy (surface);
+        }
+      else
+        g_error ("Could not create cairo svg surface with file %s", filename);
     }
+  else
+    {
+      driver = output_driver_create (&options);
+      if (driver == NULL)
+       goto finish;
 
-  if (driver->class->flush)
-    driver->class->flush (driver);
+      driver->class->submit (driver, view->selected_item->item);
 
+      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;
+      /* 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))
+  if (info == SELECT_FMT_IMG)
     {
-      gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
-                             8,
-                             (const guchar *) text, length);
+      GdkPixbuf *pixbuf = derive_pixbuf_from_svg (filename);
+      if (pixbuf)
+       {
+         gtk_selection_data_set_pixbuf (selection_data, pixbuf);
+         g_object_unref (pixbuf);
+       }
     }
+  else if (g_file_get_contents (filename, &text, &length, NULL))
+    gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
+                           8,
+                           (const guchar *) text, length);
 
  finish:
 
@@ -617,9 +756,7 @@ clipboard_get_cb (GtkClipboard     *clipboard,
 
   unlink (filename);
   free (filename);
-  rmdir (dirname);
-
-  g_list_free (rows);
+  cleanup_temp_dir (td);
 }
 
 static void
@@ -628,48 +765,61 @@ clipboard_clear_cb (GtkClipboard *clipboard,
 {
 }
 
-static const GtkTargetEntry targets[] = {
+#define CBTARGETS                                           \
+CT ( ctn1, "STRING",        0, SELECT_FMT_TEXT )            \
+CT ( ctn2, "TEXT",          0, SELECT_FMT_TEXT )            \
+CT ( ctn3, "COMPOUND_TEXT", 0, SELECT_FMT_TEXT )            \
+CT ( ctn4, "text/plain",    0, SELECT_FMT_TEXT )            \
+CT ( ctn5, "UTF8_STRING",   0, SELECT_FMT_UTF8 )            \
+CT ( ctn6, "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 ) \
+CT ( ctn7, "text/html",     0, SELECT_FMT_HTML )            \
+CT ( ctn8, "image/svg+xml", 0, SELECT_FMT_SVG )
+
+#define CT(ID, TARGET, FLAGS, INFO) static gchar ID[] = TARGET;
+CBTARGETS
+#undef CT
+static gchar ctnlast[] = "application/vnd.oasis.opendocument.text";
 
-  { "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 const GtkTargetEntry targets[] = {
+#define CT(ID, TARGET, FLAGS, INFO) { ID, FLAGS, INFO },
+  CBTARGETS
+#undef CT
+  { ctnlast, 0, SELECT_FMT_ODT }
 };
 
+static GtkTargetList *
+build_target_list (const struct output_item *item)
+{
+  GtkTargetList *tl = gtk_target_list_new (targets, G_N_ELEMENTS (targets));
+  g_return_val_if_fail (tl, NULL);
+  if (is_table_item (item) ||
+      is_chart_item (item))
+    gtk_target_list_add_image_targets (tl, SELECT_FMT_IMG, TRUE);
+  return tl;
+}
+
 static void
 on_copy (struct psppire_output_view *view)
 {
   GtkWidget *widget = GTK_WIDGET (view->overview);
   GtkClipboard *cb = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
 
-  if (!gtk_clipboard_set_with_data (cb, targets, G_N_ELEMENTS (targets),
+  struct output_view_item *ov_item = find_selected_item (view);
+  if (ov_item == NULL)
+    return;
+  view->selected_item = ov_item;
+  GtkTargetList *tl = build_target_list (ov_item->item);
+  g_return_if_fail (tl);
+  gint no_of_targets = 0;
+  GtkTargetEntry *ta = gtk_target_table_new_from_list (tl, &no_of_targets);
+  g_return_if_fail (ta);
+  if (!gtk_clipboard_set_with_data (cb, ta, no_of_targets,
                                     clipboard_get_cb, clipboard_clear_cb,
                                     view))
     clipboard_clear_cb (cb, view);
-}
-
-static void
-on_selection_change (GtkTreeSelection *sel, GAction *copy_action)
-{
-  /* The Copy action is available only if there is something selected */
-  g_object_set (copy_action,
-               "enabled", gtk_tree_selection_count_selected_rows (sel) > 0,
-               NULL);
-}
 
-static void
-on_select_all (struct psppire_output_view *view)
-{
-  GtkTreeSelection *sel = gtk_tree_view_get_selection (view->overview);
-  gtk_tree_view_expand_all (view->overview);
-  gtk_tree_selection_select_all (sel);
+  gtk_target_list_unref (tl);
+  gtk_target_table_free (ta,no_of_targets);
 }
 
 static void
@@ -684,9 +834,6 @@ on_size_allocate (GtkWidget    *widget,
 static void
 on_realize (GtkWidget *overview, GObject *view)
 {
-  GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (overview));
-  gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);
-
   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview));
 
   GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
@@ -696,15 +843,11 @@ on_realize (GtkWidget *overview, GObject *view)
                                                           "select-all");
 
   g_object_set (copy_action, "enabled", FALSE, NULL);
-
-  g_signal_connect_swapped (select_all_action, "activate",
-                           G_CALLBACK (on_select_all), view);
+  g_object_set (select_all_action, "enabled", FALSE, NULL);
 
   g_signal_connect_swapped (copy_action, "activate",
                             G_CALLBACK (on_copy), view);
 
-  g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change),
-                    copy_action);
 }
 
 struct psppire_output_view *
@@ -717,18 +860,19 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
   GtkTreeModel *model;
 
   view = xmalloc (sizeof *view);
-  view->xr = NULL;
-  view->font_height = 0;
+  view->style = NULL;
+  view->object_spacing = 10;
   view->output = output;
   view->render_width = 0;
   view->max_width = 0;
   view->y = 0;
-  string_map_init (&view->render_opts);
   view->overview = overview;
   view->cur_group = NULL;
   view->toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output));
+  view->buttontime = 0;
   view->items = NULL;
   view->n_items = view->allocated_items = 0;
+  view->selected_item = NULL;
   view->print_settings = NULL;
   view->print_xrd = NULL;
   view->print_item = 0;
@@ -741,6 +885,10 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
 
   g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
 
+  gtk_widget_add_events (GTK_WIDGET (output), GDK_BUTTON_PRESS_MASK);
+  g_signal_connect (output, "button-press-event",
+                   G_CALLBACK (off_item_button_press_event_cb), view);
+
   gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)),
                               GTK_STYLE_CLASS_VIEW);
 
@@ -780,7 +928,7 @@ psppire_output_view_destroy (struct psppire_output_view *view)
   g_signal_handlers_disconnect_by_func (view->output,
                                         G_CALLBACK (on_style_updated), view);
 
-  string_map_destroy (&view->render_opts);
+  xr_fsm_style_unref (view->style);
 
   for (i = 0; i < view->n_items; i++)
     output_item_unref (view->items[i].item);
@@ -791,8 +939,6 @@ psppire_output_view_destroy (struct psppire_output_view *view)
   if (view->print_settings != NULL)
     g_object_unref (view->print_settings);
 
-  xr_driver_destroy (view->xr);
-
   if (view->cur_group)
     gtk_tree_path_free (view->cur_group);