Merge "paginate" branch into "master".
authorBen Pfaff <blp@cs.stanford.edu>
Wed, 19 May 2010 05:05:52 +0000 (22:05 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Wed, 19 May 2010 05:05:52 +0000 (22:05 -0700)
This adds printing support to the PSPPIRE output viewer on the master
branch.

1  2 
src/ui/gui/output-viewer.ui
src/ui/gui/psppire-output-window.c

index 0000000000000000000000000000000000000000,e664a6d9930df0cc890b758268a3236516e81fd8..3325e2abc118c1402d9a9d8ab6694b574c069013
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,130 +1,137 @@@
+ <?xml version="1.0"?>
+ <interface>
+   <object class="GtkUIManager" id="uimanager1">
+     <child>
+       <object class="GtkActionGroup" id="actiongroup1">
+         <child>
+           <object class="GtkAction" id="menuitem1">
+             <property name="name">menuitem1</property>
+             <property name="label" translatable="yes">_File</property>
+           </object>
+         </child>
++        <child>
++          <object class="GtkAction" id="file_print">
++            <property name="name">file_print</property>
++            <property name="label" translatable="yes">_Print</property>
++          </object>
++        </child>
+         <child>
+           <object class="GtkAction" id="file_export">
+             <property name="name">file_export</property>
+             <property name="label" translatable="yes">_Export</property>
+           </object>
+         </child>
+         <child>
+           <object class="GtkAction" id="menuitem2">
+             <property name="name">menuitem2</property>
+             <property name="label" translatable="yes">_Edit</property>
+           </object>
+         </child>
+         <child>
+           <object class="GtkAction" id="imagemenuitem7">
+             <property name="stock-id">gtk-copy</property>
+             <property name="name">imagemenuitem7</property>
+           </object>
+         </child>
+         <child>
+           <object class="GtkAction" id="windows_menuitem">
+             <property name="name">windows_menuitem</property>
+             <property name="label" translatable="yes">_Windows</property>
+           </object>
+         </child>
+         <child>
+           <object class="GtkAction" id="windows_minimise-all">
+             <property name="name">windows_minimise-all</property>
+             <property name="label" translatable="yes">_Minimize All Windows</property>
+           </object>
+         </child>
+       </object>
+     </child>
+     <ui>
+       <menubar name="menubar">
+         <menu action="menuitem1">
+           <menuitem action="file_export"/>
++          <menuitem action="file_print"/>
+         </menu>
+         <menu action="menuitem2">
+           <menuitem action="imagemenuitem7"/>
+         </menu>
+         <menu action="windows_menuitem">
+           <menuitem action="windows_minimise-all"/>
+         </menu>
+       </menubar>
+     </ui>
+   </object>
+   <!-- interface-requires gtk+ 2.16 -->
+   <!-- interface-naming-policy toplevel-contextual -->
+   <object class="GtkWindow" id="output-viewer-window">
+     <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+     <property name="default_width">600</property>
+     <property name="default_height">400</property>
+     <child>
+       <object class="GtkVBox" id="vbox1">
+         <property name="visible">True</property>
+         <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+         <child>
+           <object class="GtkMenuBar" constructor="uimanager1" id="menubar">
+             <property name="visible">True</property>
+             <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+           </object>
+           <packing>
+             <property name="expand">False</property>
+             <property name="position">0</property>
+           </packing>
+         </child>
+         <child>
+           <object class="GtkHPaned" id="hpaned1">
+             <property name="visible">True</property>
+             <property name="can_focus">True</property>
+             <property name="position">112</property>
+             <property name="position_set">True</property>
+             <child>
+               <object class="GtkScrolledWindow" id="scrolledwindow2">
+                 <property name="visible">True</property>
+                 <property name="can_focus">True</property>
+                 <property name="hscrollbar_policy">automatic</property>
+                 <property name="vscrollbar_policy">automatic</property>
+                 <child>
+                   <object class="GtkTreeView" id="overview">
+                     <property name="visible">True</property>
+                     <property name="can_focus">True</property>
+                     <property name="headers_visible">False</property>
+                   </object>
+                 </child>
+               </object>
+               <packing>
+                 <property name="resize">False</property>
+                 <property name="shrink">True</property>
+               </packing>
+             </child>
+             <child>
+               <object class="GtkScrolledWindow" id="scrolledwindow1">
+                 <property name="visible">True</property>
+                 <property name="can_focus">True</property>
+                 <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+                 <property name="hscrollbar_policy">automatic</property>
+                 <property name="vscrollbar_policy">automatic</property>
+                 <child>
+                   <object class="GtkLayout" id="output">
+                     <property name="visible">True</property>
+                     <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+                   </object>
+                 </child>
+               </object>
+               <packing>
+                 <property name="resize">True</property>
+                 <property name="shrink">True</property>
+               </packing>
+             </child>
+           </object>
+           <packing>
+             <property name="position">1</property>
+           </packing>
+         </child>
+       </object>
+     </child>
+   </object>
+ </interface>
index 2e02b4e5ec33503f8ffc08d63676bc64e6d8d30b,f78b8855bb9d72d129258d96b45121915b93db64..5c5610a0e8cd398551a6d8b292b9df64f9898758
@@@ -32,7 -32,7 +32,7 @@@
  #include <output/tab.h>
  #include <stdlib.h>
  
- #include "about.h"
+ #include "help-menu.h"
  
  #include "psppire-output-window.h"
  
@@@ -110,8 -110,6 +110,8 @@@ psppire_output_window_dispose (GObject 
    viewer->items = NULL;
    viewer->n_items = viewer->allocated_items = 0;
  
 +  g_object_unref (viewer->print_settings);
 +
    /* Chain up to the parent class */
    G_OBJECT_CLASS (parent_class)->dispose (obj);
  }
@@@ -237,19 -235,7 +237,19 @@@ psppire_output_submit (struct output_dr
        g_free (font_name);
        pango_font_description_free (font_desc);
  
 -      pod->xr = xr_create_driver (cr, &options);
 +      /* 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 (&options, "paper-size", "300x200000mm");
 +      string_map_insert (&options, "headers", "off");
 +      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");
 +
 +      pod->xr = xr_driver_create (cr, &options);
  
        string_map_destroy (&options);
      }
@@@ -410,9 -396,6 +410,9 @@@ on_row_activate (GtkTreeView *overview
    gtk_adjustment_set_value (vadj, y);
  }
  
 +static void psppire_output_window_print (PsppireOutputWindow *window);
 +
 +
  static GtkFileFilter *
  add_filter (GtkFileChooser *chooser, const char *name, const char *pattern)
  {
@@@ -570,16 -553,6 +570,6 @@@ psppire_output_window_init (PsppireOutp
                    G_CALLBACK (cancel_urgency),
                    NULL);
  
-   g_signal_connect (get_action_assert (xml,"help_about"),
-                   "activate",
-                   G_CALLBACK (about_new),
-                   window);
-   g_signal_connect (get_action_assert (xml,"help_reference"),
-                   "activate",
-                   G_CALLBACK (reference_manual),
-                   NULL);
    g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
                    "activate",
                    G_CALLBACK (psppire_window_minimise_all),
  
    {
      GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
+     merge_help_menu (uim);
  
      PSPPIRE_WINDOW (window)->menu =
-       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar1/windows_menuitem/windows_minimise-all")->parent);
+       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows_menuitem/windows_minimise-all")->parent);
    }
  
    g_signal_connect_swapped (get_action_assert (xml, "file_export"), "activate",
                              G_CALLBACK (psppire_output_window_export), window);
  
 +
 +  g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
 +                            G_CALLBACK (psppire_output_window_print), window);
 +
    g_object_unref (xml);
  
    g_signal_connect (window, "delete-event",
@@@ -614,153 -584,3 +605,153 @@@ psppire_output_window_new (void
                                   "description", _("Output Viewer"),
                                   NULL));
  }
 +
 +
 +\f
 +static void
 +create_xr_print_driver (GtkPrintContext *context, PsppireOutputWindow *window)
 +{
 +  struct string_map options;
 +  GtkPageSetup *page_setup;
 +  double width, height;
 +  double left_margin;
 +  double right_margin;
 +  double top_margin;
 +  double bottom_margin;
 +
 +  page_setup = gtk_print_context_get_page_setup (context);
 +  width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
 +  height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
 +  left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
 +  right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
 +  top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
 +  bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
 +
 +  string_map_init (&options);
 +  string_map_insert_nocopy (&options, xstrdup ("paper-size"),
 +                            xasprintf("%.2fx%.2fmm", width, height));
 +  string_map_insert_nocopy (&options, xstrdup ("left-margin"),
 +                            xasprintf ("%.2fmm", left_margin));
 +  string_map_insert_nocopy (&options, xstrdup ("right-margin"),
 +                            xasprintf ("%.2fmm", right_margin));
 +  string_map_insert_nocopy (&options, xstrdup ("top-margin"),
 +                            xasprintf ("%.2fmm", top_margin));
 +  string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
 +                            xasprintf ("%.2fmm", bottom_margin));
 +
 +  window->print_xrd =
 +    xr_driver_create (gtk_print_context_get_cairo_context (context), &options);
 +
 +  string_map_destroy (&options);
 +}
 +
 +static gboolean
 +paginate (GtkPrintOperation *operation,
 +        GtkPrintContext   *context,
 +        PsppireOutputWindow *window)
 +{
 +  g_print ("%s\n", __FUNCTION__);
 +
 +  if ( window->print_item < window->n_items )
 +    {
 +      g_print ("Passing item %d\n", window->print_item);
 +      xr_driver_output_item (window->print_xrd, window->items[window->print_item++]);
 +      bool x = xr_driver_need_new_page (window->print_xrd);
 +      if ( x )
 +      {
 +        g_print ("Need new page: %d\n", x);
 +        xr_driver_next_page (window->print_xrd, NULL);
 +        window->print_n_pages ++;
 +      }
 +      return FALSE;
 +    }
 +  else
 +    {
 +      g_print ("Number of pages is %d\n", window->print_n_pages);
 +      gtk_print_operation_set_n_pages (operation, window->print_n_pages);
 +      window->print_item = 0;
 +
 +      create_xr_print_driver (context, window);
 +
 +      return TRUE;
 +    }
 +}
 +
 +static void
 +begin_print (GtkPrintOperation *operation,
 +           GtkPrintContext   *context,
 +           PsppireOutputWindow *window)
 +{
 +  g_print ("%s\n", __FUNCTION__);
 +
 +  create_xr_print_driver (context, window);
 +
 +  window->print_item = 0;
 +  window->print_n_pages = 1;
 +}
 +
 +static void
 +end_print (GtkPrintOperation *operation,
 +         GtkPrintContext   *context,
 +         PsppireOutputWindow *window)
 +{
 +  g_print ("%s\n", __FUNCTION__);
 +  //  xr_driver_destroy (window->print_xrd);
 +}
 +
 +static void
 +done (GtkPrintOperation *operation,
 +      GtkPrintOperationResult   result,
 +      gpointer           user_data)    
 +{
 +  g_print ("%s %d\n", __FUNCTION__, result);
 +}
 +
 +
 +static void
 +draw_page (GtkPrintOperation *operation,
 +         GtkPrintContext   *context,
 +         gint               page_number,
 +         PsppireOutputWindow *window)
 +{
 +  g_print ("%s: %d\n", __FUNCTION__, page_number);
 +
 +  xr_driver_next_page (window->print_xrd, gtk_print_context_get_cairo_context (context));
 +  while ( window->print_item < window->n_items)
 +    {
 +      xr_driver_output_item (window->print_xrd, window->items [window->print_item++]);
 +      if ( xr_driver_need_new_page (window->print_xrd) )
 +        break;          
 +    }
 +}
 +
 +
 +static void
 +psppire_output_window_print (PsppireOutputWindow *window)
 +{
 +  GtkPrintOperationResult res;
 +
 +  GtkPrintOperation *print = gtk_print_operation_new ();
 +
 +  if (window->print_settings != NULL) 
 +    gtk_print_operation_set_print_settings (print, window->print_settings);
 +
 +  g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
 +  g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
 +  g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
 +  g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
 +  g_signal_connect (print, "done", G_CALLBACK (done),               window);
 +
 +  res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
 +                                 GTK_WINDOW (window), NULL);
 +
 +  if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
 +    {
 +      if (window->print_settings != NULL)
 +        g_object_unref (window->print_settings);
 +      window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
 +      
 +    }
 +
 +  g_object_unref (print);
 +}