~ubuntu-branches/ubuntu/trusty/anjuta/trusty

« back to all changes in this revision

Viewing changes to .pc/00git_sourceview_includes.patch/plugins/sourceview/sourceview-print.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-09-04 10:01:50 UTC
  • mfrom: (1.1.51)
  • Revision ID: package-import@ubuntu.com-20120904100150-d8i99pb0n87fb2bj
Tags: 2:3.5.91-0ubuntu1
* New upstream bugfix release
* debian/patches/00git_sourceview_includes.patch 
  - Applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
6
 
*
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.
11
 
*
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.
15
 
*/
16
 
 
17
 
#include "sourceview-print.h"
18
 
#include "sourceview-private.h"
19
 
#include <gtksourceview/gtksourceprintcompositor.h>
20
 
#include <libanjuta/interfaces/ianjuta-document.h>
21
 
 
22
 
#define PRINT_LINEWRAP "print-linewrap"
23
 
#define PRINT_HEADER "print-header"
24
 
#define PRINT_FOOTER "print-footer"
25
 
#define PRINT_HIGHLIGHT "print-highlight"
26
 
#define PRINT_LINENUMBERS "print-linenumbers"
27
 
 
28
 
typedef struct _SourceviewPrinting SourceviewPrinting;
29
 
 
30
 
struct _SourceviewPrinting
31
 
{
32
 
        Sourceview* sv;
33
 
        GtkSourcePrintCompositor *compositor;
34
 
        AnjutaStatus* status;
35
 
};
36
 
 
37
 
 
38
 
static gboolean
39
 
paginate (GtkPrintOperation        *operation,
40
 
                                        GtkPrintContext          *context,
41
 
                                        SourceviewPrinting* printing)
42
 
{
43
 
        if (gtk_source_print_compositor_paginate (printing->compositor, context))
44
 
        {
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);
50
 
 
51
 
                return TRUE;
52
 
        }
53
 
 
54
 
        return FALSE;
55
 
}
56
 
 
57
 
 
58
 
static void
59
 
draw_page (GtkPrintOperation        *operation,
60
 
                                         GtkPrintContext          *context,
61
 
                                         gint                      page_nr,
62
 
                                         SourceviewPrinting* printing)
63
 
{
64
 
        gtk_source_print_compositor_draw_page (printing->compositor, context, page_nr);
65
 
}
66
 
 
67
 
static void
68
 
end_print (GtkPrintOperation        *operation,
69
 
           GtkPrintContext          *context,
70
 
           SourceviewPrinting* printing)
71
 
{
72
 
        g_object_unref (printing->compositor);
73
 
        g_slice_free (SourceviewPrinting, printing);
74
 
}
75
 
 
76
 
static void
77
 
custom_widget_apply (GtkPrintOperation* operation,
78
 
                     GtkWidget* widget,
79
 
                     SourceviewPrinting* printing)
80
 
{
81
 
        if (g_settings_get_boolean (printing->sv->priv->settings, PRINT_LINEWRAP))
82
 
        {
83
 
                gtk_source_print_compositor_set_wrap_mode (printing->compositor,
84
 
                                                           GTK_WRAP_WORD_CHAR);
85
 
        }
86
 
        else
87
 
                gtk_source_print_compositor_set_wrap_mode (printing->compositor,
88
 
                                                           GTK_WRAP_NONE);
89
 
 
90
 
        gtk_source_print_compositor_set_print_line_numbers (printing->compositor,
91
 
                                                            g_settings_get_boolean (printing->sv->priv->settings,
92
 
                                                                                    PRINT_LINENUMBERS));
93
 
 
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));
100
 
 
101
 
        gtk_source_print_compositor_set_highlight_syntax (printing->compositor,
102
 
                                                          g_settings_get_boolean (printing->sv->priv->settings,
103
 
                                                                                  PRINT_HIGHLIGHT));
104
 
}
105
 
 
106
 
static GObject*
107
 
create_custom_widget (GtkPrintOperation* operation,
108
 
                      Sourceview* sv)
109
 
{
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);
121
 
 
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);
132
 
 
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);
138
 
 
139
 
        gtk_widget_show_all (vbox);
140
 
 
141
 
        return G_OBJECT (vbox);
142
 
}
143
 
 
144
 
static GtkPrintOperation*
145
 
print_setup (Sourceview* sv)
146
 
{
147
 
 
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;
154
 
 
155
 
        filename = ianjuta_document_get_filename (IANJUTA_DOCUMENT (sv), NULL);
156
 
        basename = g_filename_display_basename (filename);
157
 
 
158
 
        compositor = gtk_source_print_compositor_new_from_view (view);
159
 
 
160
 
        gtk_source_print_compositor_set_header_format (compositor,
161
 
                                                       TRUE,
162
 
                                                       "%x",
163
 
                                                       basename,
164
 
                                                       "Page %N/%Q");
165
 
 
166
 
        gtk_source_print_compositor_set_footer_format (compositor,
167
 
                                                       TRUE,
168
 
                                                       "%T",
169
 
                                                       basename,
170
 
                                                       "Page %N/%Q");
171
 
 
172
 
        operation = gtk_print_operation_new ();
173
 
 
174
 
        gtk_print_operation_set_job_name (operation, basename);
175
 
 
176
 
        gtk_print_operation_set_show_progress (operation, TRUE);
177
 
 
178
 
        printing->compositor = compositor;
179
 
        printing->sv = sv;
180
 
        printing->status = anjuta_shell_get_status (sv->priv->plugin->shell, NULL);
181
 
 
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);
192
 
 
193
 
        anjuta_status_progress_reset (printing->status);
194
 
        anjuta_status_progress_add_ticks (printing->status, 100);
195
 
        g_free (basename);
196
 
 
197
 
        return operation;
198
 
}
199
 
 
200
 
void
201
 
sourceview_print(Sourceview* sv)
202
 
{
203
 
        GtkPrintOperation* operation = print_setup (sv);
204
 
 
205
 
        gtk_print_operation_run (operation,
206
 
                                 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
207
 
                                 NULL, NULL);
208
 
        g_object_unref (operation);
209
 
}
210
 
 
211
 
 
212
 
void
213
 
sourceview_print_preview(Sourceview* sv)
214
 
{
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);
220
 
}
221