sourceview: Fix a crash with Scintilla
[anjuta.git] / plugins / sourceview / sourceview-print.c
blobe006cd035befb2404599f3cc83bf1ec1e60d3d05
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #include "sourceview-print.h"
18 #include "sourceview-private.h"
19 #include <gtksourceview/gtksourceprintcompositor.h>
20 #include <libanjuta/interfaces/ianjuta-document.h>
22 #define PRINT_LINEWRAP "sourceview-print-linewrap"
23 #define PRINT_HEADER "sourceview-print-header"
24 #define PRINT_FOOTER "sourceview-print-footer"
25 #define PRINT_HIGHLIGHT "sourceview-print-highlight"
26 #define PRINT_LINENUMBERS "sourceview-print-linenumbers"
28 typedef struct _SourceviewPrinting SourceviewPrinting;
30 struct _SourceviewPrinting
32 Sourceview* sv;
33 GtkSourcePrintCompositor *compositor;
34 AnjutaStatus* status;
38 static gboolean
39 paginate (GtkPrintOperation *operation,
40 GtkPrintContext *context,
41 SourceviewPrinting* printing)
43 if (gtk_source_print_compositor_paginate (printing->compositor, context))
45 gint n_pages;
46 anjuta_status_progress_tick (printing->status, NULL,
47 _("Preparing pages for printing"));
48 n_pages = gtk_source_print_compositor_get_n_pages (printing->compositor);
49 gtk_print_operation_set_n_pages (operation, n_pages);
51 return TRUE;
54 return FALSE;
58 static void
59 draw_page (GtkPrintOperation *operation,
60 GtkPrintContext *context,
61 gint page_nr,
62 SourceviewPrinting* printing)
64 gtk_source_print_compositor_draw_page (printing->compositor, context, page_nr);
67 static void
68 end_print (GtkPrintOperation *operation,
69 GtkPrintContext *context,
70 SourceviewPrinting* printing)
72 g_object_unref (printing->compositor);
73 g_slice_free (SourceviewPrinting, printing);
76 static void
77 custom_widget_apply (GtkPrintOperation* operation,
78 GtkWidget* widget,
79 SourceviewPrinting* printing)
81 if (g_settings_get_boolean (printing->sv->priv->settings, PRINT_LINEWRAP))
83 gtk_source_print_compositor_set_wrap_mode (printing->compositor,
84 GTK_WRAP_WORD_CHAR);
86 else
87 gtk_source_print_compositor_set_wrap_mode (printing->compositor,
88 GTK_WRAP_NONE);
90 gtk_source_print_compositor_set_print_line_numbers (printing->compositor,
91 g_settings_get_boolean (printing->sv->priv->settings,
92 PRINT_LINENUMBERS));
94 gtk_source_print_compositor_set_print_header (printing->compositor,
95 g_settings_get_boolean (printing->sv->priv->settings,
96 PRINT_HEADER));
97 gtk_source_print_compositor_set_print_footer (printing->compositor,
98 g_settings_get_boolean (printing->sv->priv->settings,
99 PRINT_FOOTER));
101 gtk_source_print_compositor_set_highlight_syntax (printing->compositor,
102 g_settings_get_boolean (printing->sv->priv->settings,
103 PRINT_HIGHLIGHT));
106 static GObject*
107 create_custom_widget (GtkPrintOperation* operation,
108 Sourceview* sv)
110 GtkWidget* toggle_linewrap =
111 gtk_check_button_new_with_label (_("Wrap lines"));
112 GtkWidget* toggle_linenumbers =
113 gtk_check_button_new_with_label (_("Line numbers"));
114 GtkWidget* toggle_header =
115 gtk_check_button_new_with_label (_("Header"));
116 GtkWidget* toggle_footer =
117 gtk_check_button_new_with_label (_("Footer"));
118 GtkWidget* toggle_highlight =
119 gtk_check_button_new_with_label (_("Highlight source code"));
120 GtkWidget* vbox = gtk_vbox_new (TRUE, 5);
122 g_settings_bind (sv->priv->settings, PRINT_LINEWRAP,
123 toggle_linewrap, "active", G_SETTINGS_BIND_DEFAULT);
124 g_settings_bind (sv->priv->settings, PRINT_LINENUMBERS,
125 toggle_linenumbers, "active", G_SETTINGS_BIND_DEFAULT);
126 g_settings_bind (sv->priv->settings, PRINT_HEADER,
127 toggle_header, "active", G_SETTINGS_BIND_DEFAULT);
128 g_settings_bind (sv->priv->settings, PRINT_FOOTER,
129 toggle_footer, "active", G_SETTINGS_BIND_DEFAULT);
130 g_settings_bind (sv->priv->settings, PRINT_HIGHLIGHT,
131 toggle_highlight, "active", G_SETTINGS_BIND_DEFAULT);
133 gtk_box_pack_start (GTK_BOX (vbox), toggle_linewrap, FALSE, FALSE, 5);
134 gtk_box_pack_start (GTK_BOX (vbox), toggle_linenumbers, FALSE, FALSE, 5);
135 gtk_box_pack_start (GTK_BOX (vbox), toggle_header, FALSE, FALSE, 5);
136 gtk_box_pack_start (GTK_BOX (vbox), toggle_footer, FALSE, FALSE, 5);
137 gtk_box_pack_start (GTK_BOX (vbox), toggle_highlight, FALSE, FALSE, 5);
139 gtk_widget_show_all (vbox);
141 return G_OBJECT (vbox);
144 static GtkPrintOperation*
145 print_setup (Sourceview* sv)
148 GtkSourceView *view = GTK_SOURCE_VIEW (sv->priv->view);
149 GtkSourcePrintCompositor *compositor;
150 GtkPrintOperation *operation;
151 SourceviewPrinting* printing = g_slice_new0(SourceviewPrinting);
152 const gchar *filename;
153 gchar *basename;
155 filename = ianjuta_document_get_filename (IANJUTA_DOCUMENT (sv), NULL);
156 basename = g_filename_display_basename (filename);
158 compositor = gtk_source_print_compositor_new_from_view (view);
160 gtk_source_print_compositor_set_header_format (compositor,
161 TRUE,
162 "%x",
163 basename,
164 "Page %N/%Q");
166 gtk_source_print_compositor_set_footer_format (compositor,
167 TRUE,
168 "%T",
169 basename,
170 "Page %N/%Q");
172 operation = gtk_print_operation_new ();
174 gtk_print_operation_set_job_name (operation, basename);
176 gtk_print_operation_set_show_progress (operation, TRUE);
178 printing->compositor = compositor;
179 printing->sv = sv;
180 printing->status = anjuta_shell_get_status (sv->priv->plugin->shell, NULL);
182 g_signal_connect (G_OBJECT (operation), "paginate",
183 G_CALLBACK (paginate), printing);
184 g_signal_connect (G_OBJECT (operation), "draw-page",
185 G_CALLBACK (draw_page), printing);
186 g_signal_connect (G_OBJECT (operation), "end-print",
187 G_CALLBACK (end_print), printing);
188 g_signal_connect (G_OBJECT (operation), "create-custom-widget",
189 G_CALLBACK (create_custom_widget), sv);
190 g_signal_connect (G_OBJECT (operation), "custom-widget-apply",
191 G_CALLBACK (custom_widget_apply), printing);
193 anjuta_status_progress_reset (printing->status);
194 anjuta_status_progress_add_ticks (printing->status, 100);
195 g_free (basename);
197 return operation;
200 void
201 sourceview_print(Sourceview* sv)
203 GtkPrintOperation* operation = print_setup (sv);
205 gtk_print_operation_run (operation,
206 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
207 NULL, NULL);
208 g_object_unref (operation);
212 void
213 sourceview_print_preview(Sourceview* sv)
215 GtkPrintOperation* operation = print_setup (sv);
216 gtk_print_operation_run (operation,
217 GTK_PRINT_OPERATION_ACTION_PREVIEW,
218 NULL, NULL);
219 g_object_unref (operation);