[gnome-builder] editor: basic print support



commit 4ae3ad01a28943b1fe70884bdbe0a59442c78787
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Aug 17 17:11:46 2015 +0200

    editor: basic print support

 data/ui/gb-view-stack.ui               |   15 +++
 src/Makefile.am                        |    2 +
 src/editor/gb-editor-print-operation.c |  207 ++++++++++++++++++++++++++++++++
 src/editor/gb-editor-print-operation.h |   34 +++++
 src/editor/gb-editor-view-actions.c    |   61 ++++++++++
 5 files changed, 319 insertions(+), 0 deletions(-)
---
diff --git a/data/ui/gb-view-stack.ui b/data/ui/gb-view-stack.ui
index b89d6f1..1600dba 100644
--- a/data/ui/gb-view-stack.ui
+++ b/data/ui/gb-view-stack.ui
@@ -340,6 +340,21 @@
           </object>
         </child>
         <child>
+          <object class="GtkBox">
+            <property name="orientation">vertical</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkModelButton">
+                <property name="action-name">view.print</property>
+                <property name="halign">fill</property>
+                <property name="hexpand">true</property>
+                <property name="text" translatable="yes">Print</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
           <object class="GtkModelButton">
             <property name="halign">fill</property>
             <property name="hexpand">true</property>
diff --git a/src/Makefile.am b/src/Makefile.am
index 444d63b..215d3b7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -34,6 +34,8 @@ libgnome_builder_la_SOURCES = \
        editor/gb-editor-frame.h \
        editor/gb-editor-map-bin.c \
        editor/gb-editor-map-bin.h \
+       editor/gb-editor-print-operation.c \
+       editor/gb-editor-print-operation.h \
        editor/gb-editor-settings-widget.c \
        editor/gb-editor-settings-widget.h \
        editor/gb-editor-tweak-widget.c \
diff --git a/src/editor/gb-editor-print-operation.c b/src/editor/gb-editor-print-operation.c
new file mode 100644
index 0000000..0a900e0
--- /dev/null
+++ b/src/editor/gb-editor-print-operation.c
@@ -0,0 +1,207 @@
+/* gb-editor-print-operation.c
+ *
+ * Copyright (C) 2015 Paolo Borelli <pborelli gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "gb-editor-print-operation"
+
+#include <glib/gi18n.h>
+#include <gtksourceview/gtksource.h>
+#include <ide.h>
+
+#include "gb-editor-print-operation.h"
+#include "gb-editor-view.h"
+
+struct _GbEditorPrintOperation
+{
+  GtkPrintOperation parent_instance;
+
+  IdeSourceView *view;
+  GtkSourcePrintCompositor *compositor;
+};
+
+G_DEFINE_TYPE (GbEditorPrintOperation, gb_editor_print_operation, GTK_TYPE_PRINT_OPERATION)
+
+enum {
+  PROP_0,
+  PROP_VIEW,
+  LAST_PROP
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+
+static void
+gb_editor_print_operation_dispose (GObject *object)
+{
+  GbEditorPrintOperation *self = GB_EDITOR_PRINT_OPERATION (object);
+
+  g_clear_object (&self->compositor);
+
+  G_OBJECT_CLASS (gb_editor_print_operation_parent_class)->dispose (object);
+}
+
+static void
+gb_editor_view_print_get_property (GObject    *object,
+                                   guint       prop_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+  GbEditorPrintOperation *self = GB_EDITOR_PRINT_OPERATION (object);
+
+  switch (prop_id)
+    {
+    case PROP_VIEW:
+      g_value_set_object (value, self->view);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_editor_view_print_set_property (GObject      *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+  GbEditorPrintOperation *self = GB_EDITOR_PRINT_OPERATION (object);
+
+  switch (prop_id)
+    {
+    case PROP_VIEW:
+      self->view = g_value_get_object (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_editor_print_operation_begin_print (GtkPrintOperation *operation,
+                                       GtkPrintContext   *context)
+{
+  GbEditorPrintOperation *self = GB_EDITOR_PRINT_OPERATION (operation);
+  GtkSourceBuffer *buffer;
+  guint tab_width;
+  gboolean syntax_hl;
+
+  buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->view)));
+
+  tab_width = gtk_source_view_get_tab_width (GTK_SOURCE_VIEW (self->view));
+  syntax_hl = gtk_source_buffer_get_highlight_syntax (buffer);
+
+  self->compositor = GTK_SOURCE_PRINT_COMPOSITOR (
+    g_object_new (GTK_SOURCE_TYPE_PRINT_COMPOSITOR,
+                  "buffer", buffer,
+                  "tab-width", tab_width,
+                  "highlight-syntax", syntax_hl,
+                  NULL));
+}
+
+static gboolean
+gb_editor_print_operation_paginate (GtkPrintOperation *operation,
+                                    GtkPrintContext   *context)
+{
+  GbEditorPrintOperation *self = GB_EDITOR_PRINT_OPERATION (operation);
+  gboolean finished;
+
+  finished = gtk_source_print_compositor_paginate (self->compositor, context);
+
+  if (finished)
+    {
+      gint n_pages;
+
+      n_pages = gtk_source_print_compositor_get_n_pages (self->compositor);
+      gtk_print_operation_set_n_pages (operation, n_pages);
+    }
+
+  return finished;
+}
+
+static void
+gb_editor_print_operation_draw_page (GtkPrintOperation *operation,
+                                     GtkPrintContext   *context,
+                                     gint               page_nr)
+{
+  GbEditorPrintOperation *self = GB_EDITOR_PRINT_OPERATION (operation);
+
+  gtk_source_print_compositor_draw_page (self->compositor, context, page_nr);
+}
+
+static void
+gb_editor_print_operation_end_print (GtkPrintOperation *operation,
+                                     GtkPrintContext   *context)
+{
+  GbEditorPrintOperation *self = GB_EDITOR_PRINT_OPERATION (operation);
+
+  g_clear_object (&self->compositor);
+}
+
+static void
+gb_editor_print_operation_class_init (GbEditorPrintOperationClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkPrintOperationClass *operation_class = GTK_PRINT_OPERATION_CLASS (klass);
+
+  object_class->dispose = gb_editor_print_operation_dispose;
+  object_class->get_property = gb_editor_view_print_get_property;
+  object_class->set_property = gb_editor_view_print_set_property;
+
+  operation_class->begin_print = gb_editor_print_operation_begin_print;
+  operation_class->draw_page = gb_editor_print_operation_draw_page;
+  operation_class->end_print = gb_editor_print_operation_end_print;
+
+  gParamSpecs [PROP_VIEW] =
+    g_param_spec_object ("view",
+                         _("View"),
+                         _("The source view."),
+                         IDE_TYPE_SOURCE_VIEW,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
+}
+
+static gboolean
+paginate_cb (GtkPrintOperation *operation,
+             GtkPrintContext   *context,
+             gpointer           user_data)
+{
+  return gb_editor_print_operation_paginate (operation, context);
+}
+
+static void
+gb_editor_print_operation_init (GbEditorPrintOperation *self)
+{
+  /* FIXME: gtk decides to call paginate only if it sees a pending signal
+   * handler, even if we override the default handler.
+   * So for now we connect to the signal instead of overriding the vfunc
+   * See https://bugzilla.gnome.org/show_bug.cgi?id=345345
+   */
+  g_signal_connect (self, "paginate", G_CALLBACK (paginate_cb), NULL);
+}
+
+GbEditorPrintOperation *
+gb_editor_print_operation_new (IdeSourceView *view)
+{
+  g_assert (IDE_IS_SOURCE_VIEW (view));
+
+  return g_object_new (GB_TYPE_EDITOR_PRINT_OPERATION,
+                       "view", view,
+                       "allow-async", TRUE,
+                       NULL);
+}
diff --git a/src/editor/gb-editor-print-operation.h b/src/editor/gb-editor-print-operation.h
new file mode 100644
index 0000000..c2e7632
--- /dev/null
+++ b/src/editor/gb-editor-print-operation.h
@@ -0,0 +1,34 @@
+/* gb-editor-print-operation.h
+ *
+ * Copyright (C) 2015 Paolo Borelli <pborelli gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GB_EDITOR_PRINT_OPERATION_H
+#define GB_EDITOR_PRINT_OPERATION_H
+
+#include "gb-editor-view.h"
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_EDITOR_PRINT_OPERATION (gb_editor_print_operation_get_type())
+
+G_DECLARE_FINAL_TYPE (GbEditorPrintOperation, gb_editor_print_operation, GB, EDITOR_PRINT_OPERATION, 
GtkPrintOperation)
+
+GbEditorPrintOperation  *gb_editor_print_operation_new    (IdeSourceView *view);
+
+G_END_DECLS
+
+#endif /* GB_EDITOR_PRINT_OPERATION_H */
diff --git a/src/editor/gb-editor-view-actions.c b/src/editor/gb-editor-view-actions.c
index 61a4a12..3575d3f 100644
--- a/src/editor/gb-editor-view-actions.c
+++ b/src/editor/gb-editor-view-actions.c
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include "gb-editor-frame-private.h"
+#include "gb-editor-print-operation.h"
 #include "gb-editor-view-actions.h"
 #include "gb-editor-view-private.h"
 #include "gb-html-document.h"
@@ -673,6 +674,65 @@ gb_editor_view_actions_reveal (GSimpleAction *action,
   gb_workbench_reveal_file (workbench, gfile);
 }
 
+static void
+handle_print_result (GbEditorView            *self,
+                     GtkPrintOperation       *operation,
+                     GtkPrintOperationResult  result)
+{
+  if (result == GTK_PRINT_OPERATION_RESULT_ERROR)
+    {
+      GError *error = NULL;
+
+      gtk_print_operation_get_error (operation, &error);
+
+      /* info bar */
+      g_warning ("%s", error->message);
+      g_clear_error (&error);
+    }
+}
+
+static void
+print_done (GtkPrintOperation       *operation,
+            GtkPrintOperationResult  result,
+            gpointer                 user_data)
+{
+  GbEditorView *self = user_data;
+
+  handle_print_result (self, operation, result);
+
+  g_object_unref (operation);
+  g_object_unref (self);
+}
+
+static void
+gb_editor_view_actions_print (GSimpleAction *action,
+                              GVariant      *param,
+                              gpointer       user_data)
+{
+  GbEditorView *self = user_data;
+  GtkWidget *toplevel;
+  g_autoptr(GbEditorPrintOperation) operation;
+  GtkPrintOperationResult result;
+
+  g_assert (GB_IS_EDITOR_VIEW (self));
+
+  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
+
+  operation = gb_editor_print_operation_new (self->frame1->source_view);
+
+  /* keep a ref until "done" is emitted */
+  g_object_ref (operation);
+
+  g_signal_connect_after (operation, "done", G_CALLBACK (print_done), g_object_ref (self));
+
+  result = gtk_print_operation_run (GTK_PRINT_OPERATION (operation),
+                                    GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
+                                    GTK_WINDOW (toplevel),
+                                    NULL);
+
+  handle_print_result (self, GTK_PRINT_OPERATION (operation), result);
+}
+
 static GActionEntry GbEditorViewActions[] = {
   { "auto-indent", NULL, NULL, "false", gb_editor_view_actions_auto_indent },
   { "close", gb_editor_view_actions_close },
@@ -684,6 +744,7 @@ static GActionEntry GbEditorViewActions[] = {
   { "reveal", gb_editor_view_actions_reveal },
   { "save", gb_editor_view_actions_save },
   { "save-as", gb_editor_view_actions_save_as },
+  { "print", gb_editor_view_actions_print },
   { "show-line-numbers", NULL, NULL, "false", gb_editor_view_actions_show_line_numbers },
   { "show-right-margin", NULL, NULL, "false", gb_editor_view_actions_show_right_margin },
   { "smart-backspace", NULL, NULL, "false", gb_editor_view_actions_smart_backspace },


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]