psppire-output-view: Render only the visible parts of tables.
[pspp] / src / ui / gui / psppire-output-view.c
index 08daef95bf39cf6821c27f68b760a361fb2c4231..fc660896d155a98437143ca5d74f961e6097fac7 100644 (file)
@@ -27,6 +27,7 @@
 #include "output/driver-provider.h"
 #include "output/driver.h"
 #include "output/chart-item.h"
+#include "output/group-item.h"
 #include "output/message-item.h"
 #include "output/output-item.h"
 #include "output/table-item.h"
@@ -82,11 +83,34 @@ enum
     N_COLS
   };
 
+/* 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);
+
+  return FALSE;                 /* Continue drawing the GtkDrawingAreas. */
+}
+
 static gboolean
 draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
 {
+  GdkRectangle clip;
+  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_all (r, cr);
+  xr_rendering_draw (r, cr, clip.x, clip.y,
+                     clip.x + clip.width, clip.y + clip.height);
   return TRUE;
 }
 
@@ -160,7 +184,7 @@ create_xr (struct psppire_output_view *view)
   cairo_destroy (cr);
 }
 
-/* Return the horizontal position to place a widget whose 
+/* Return the horizontal position to place a widget whose
    width is CHILD_WIDTH */
 static gint
 get_xpos (const struct psppire_output_view *view, gint child_width)
@@ -183,9 +207,10 @@ create_drawing_area (struct psppire_output_view *view,
     {
       GdkRGBA green = {0, 1, 0, 1};
       gtk_widget_override_background_color (GTK_WIDGET (view->output),
-                                           GTK_STATE_NORMAL, &green);
+                                           GTK_STATE_FLAG_NORMAL, &green);
       GdkRGBA red = {1, 0, 0, 1};
-      gtk_widget_override_background_color (drawing_area, GTK_STATE_NORMAL, &red);
+      gtk_widget_override_background_color (drawing_area,
+                                            GTK_STATE_FLAG_NORMAL, &red);
     }
 
   g_object_set_data_full (G_OBJECT (drawing_area),
@@ -250,11 +275,23 @@ rerender (struct psppire_output_view *view)
           gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
         }
 
+      {
+       gint minw;
+       gint minh;
+       /* This code probably doesn't bring us anthing, but Gtk
+          shows warnings if get_preferred_width/height is not
+          called before the size_allocate below is called. */
+       gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL);
+       gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL);
+       if (th > minh) th = minh;
+       if (tw > minw) tw = minw;
+      }
       alloc.x = xpos;
       alloc.y = view->y;
       alloc.width = tw;
       alloc.height = th;
-      gtk_widget_size_allocate(item->drawing_area,&alloc);
+
+      gtk_widget_size_allocate (item->drawing_area, &alloc);
 
       if (view->max_width < tw)
         view->max_width = tw;
@@ -279,18 +316,19 @@ psppire_output_view_put (struct psppire_output_view *view,
   GtkTreeIter iter;
   int tw, th;
 
-  if (is_text_item (item))
+  if (is_group_close_item (item))
     {
-      const struct text_item *text_item = to_text_item (item);
-      enum text_item_type type = text_item_get_type (text_item);
-      const char *text = text_item_get_text (text_item);
-
-      if (type == TEXT_ITEM_COMMAND_CLOSE)
+      if (output_get_group_level () == 0)
         {
           view->in_command = false;
           return;
         }
-      else if (text[0] == '\0')
+    }
+  else if (is_text_item (item))
+    {
+      const struct text_item *text_item = to_text_item (item);
+      const char *text = text_item_get_text (text_item);
+      if (text[0] == '\0')
         return;
     }
 
@@ -331,8 +369,7 @@ psppire_output_view_put (struct psppire_output_view *view,
       store = GTK_TREE_STORE (gtk_tree_view_get_model (view->overview));
 
       ds_init_empty (&name);
-      if (is_text_item (item)
-          && text_item_get_type (to_text_item (item)) == TEXT_ITEM_COMMAND_OPEN)
+      if (is_group_open_item (item) && output_get_group_level () == 1)
         {
           gtk_tree_store_append (store, &iter, NULL);
           view->cur_command = iter; /* XXX shouldn't save a GtkTreeIter */
@@ -356,9 +393,10 @@ psppire_output_view_put (struct psppire_output_view *view,
         }
       else if (is_table_item (item))
         {
-          const char *title = table_item_get_title (to_table_item (item));
+          const struct table_item_text *title
+            = table_item_get_title (to_table_item (item));
           if (title != NULL)
-            ds_put_format (&name, "Table: %s", title);
+            ds_put_format (&name, "Table: %s", title->content);
           else
             ds_put_cstr (&name, "Table");
         }
@@ -370,6 +408,8 @@ psppire_output_view_put (struct psppire_output_view *view,
           else
             ds_put_cstr (&name, "Chart");
         }
+      else if (is_group_open_item (item))
+        ds_put_cstr (&name, to_group_open_item (item)->command_name);
       gtk_tree_store_set (store, &iter,
                           COL_NAME, ds_cstr (&name),
                          COL_ADDR, item,
@@ -610,13 +650,8 @@ on_size_allocate (GtkWidget    *widget,
                   GdkRectangle *allocation,
                   struct psppire_output_view *view)
 {
-  int new_render_width = MAX (300, allocation->width);
-
-  if (view->render_width != new_render_width)
-    {
-      view->render_width = new_render_width;
-      rerender (view);
-    }
+  view->render_width = MAX (300, allocation->width);
+  rerender (view);
 }
 
 static void
@@ -629,18 +664,18 @@ on_realize (GtkWidget *overview, GObject *view)
 
   GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
                                                     "copy");
-  
+
   GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
                                                           "select-all");
 
-  g_object_set (copy_action, "enabled", FALSE, NULL); 
+  g_object_set (copy_action, "enabled", FALSE, NULL);
 
   g_signal_connect_swapped (select_all_action, "activate",
                            G_CALLBACK (on_select_all), view);
 
   g_signal_connect_swapped (copy_action, "activate",
                             G_CALLBACK (on_copy), view);
-  
+
   g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change),
                     copy_action);
 }
@@ -653,7 +688,7 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
   GtkCellRenderer *renderer;
 
   GtkTreeModel *model;
-  
+
   view = xmalloc (sizeof *view);
   view->xr = NULL;
   view->font_height = 0;
@@ -674,6 +709,8 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
   view->print_n_pages = 0;
   view->paginated = FALSE;
 
+  g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL);
+
   g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view);
 
   g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
@@ -777,17 +814,17 @@ static cairo_t *
 get_cairo_context_from_print_context (GtkPrintContext *context)
 {
   cairo_t *cr = gtk_print_context_get_cairo_context (context);
-  
+
   /*
     For all platforms except windows, gtk_print_context_get_dpi_[xy] returns 72.
     Windows returns 600.
   */
   double xres = gtk_print_context_get_dpi_x (context);
   double yres = gtk_print_context_get_dpi_y (context);
-  
+
   /* This means that the cairo context now has its dimensions in Points */
   cairo_scale (cr, xres / 72.0, yres / 72.0);
-  
+
   return cr;
 }
 
@@ -906,7 +943,7 @@ psppire_output_view_print (struct psppire_output_view *view,
 
   GtkPrintOperation *print = gtk_print_operation_new ();
 
-  if (view->print_settings != NULL) 
+  if (view->print_settings != NULL)
     gtk_print_operation_set_print_settings (print, view->print_settings);
 
   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);