[Xfce4-commits] <mousepad:master> Switch to GtkSourcePrintCompositor for printing.
Matthew Brush
noreply at xfce.org
Sat May 5 21:31:54 CEST 2012
Updating branch refs/heads/master
to c45ed8da1ae2ed17fb291084cebae1275d33f7c8 (commit)
from 029bef60746206eaf08dc11c89afd59db186db8d (commit)
commit c45ed8da1ae2ed17fb291084cebae1275d33f7c8
Author: Matthew Brush <matt at xfce.org>
Date: Thu Oct 6 19:23:13 2011 -0700
Switch to GtkSourcePrintCompositor for printing.
* Add printing option for syntax highlighting.
* Some of the page footer code implemented in #if 0 blocks.
* Remove mousepad_print_end_print() function.
mousepad/mousepad-print.c | 444 +++++++++++++------------------------------
mousepad/mousepad-private.h | 1 +
2 files changed, 137 insertions(+), 308 deletions(-)
diff --git a/mousepad/mousepad-print.c b/mousepad/mousepad-print.c
index c2ae215..35f31f8 100644
--- a/mousepad/mousepad-print.c
+++ b/mousepad/mousepad-print.c
@@ -42,8 +42,6 @@ static void mousepad_print_begin_print (GtkPrintOperation
static void mousepad_print_draw_page (GtkPrintOperation *operation,
GtkPrintContext *context,
gint page_nr);
-static void mousepad_print_end_print (GtkPrintOperation *operation,
- GtkPrintContext *context);
static void mousepad_print_page_setup_dialog (GtkWidget *button,
GtkPrintOperation *operation);
static void mousepad_print_button_toggled (GtkWidget *button,
@@ -68,31 +66,17 @@ struct _MousepadPrint
GtkPrintOperation __parent__;
/* the document we're going to print */
- MousepadDocument *document;
-
- /* pango layout containing all text */
- PangoLayout *layout;
-
- /* array with the lines drawn on each page */
- GArray *lines;
-
- /* drawing offsets */
- gint x_offset;
- gint y_offset;
-
- /* page line number counter */
- gint line_number;
+ MousepadDocument *document;
/* print dialog widgets */
- GtkWidget *widget_page_headers;
- GtkWidget *widget_line_numbers;
- GtkWidget *widget_text_wrapping;
-
- /* settings */
- guint print_page_headers : 1;
- guint print_line_numbers : 1;
- guint text_wrapping : 1;
- gchar *font_name;
+ GtkWidget *widget_page_headers;
+ GtkWidget *widget_page_footers;
+ GtkWidget *widget_line_numbers;
+ GtkWidget *widget_text_wrapping;
+ GtkWidget *widget_syntax_highlighting;
+
+ /* source view print compositor */
+ GtkSourcePrintCompositor *compositor;
};
@@ -113,7 +97,6 @@ mousepad_print_class_init (MousepadPrintClass *klass)
gtkprintoperation_class = GTK_PRINT_OPERATION_CLASS (klass);
gtkprintoperation_class->begin_print = mousepad_print_begin_print;
gtkprintoperation_class->draw_page = mousepad_print_draw_page;
- gtkprintoperation_class->end_print = mousepad_print_end_print;
gtkprintoperation_class->create_custom_widget = mousepad_print_create_custom_widget;
gtkprintoperation_class->status_changed = mousepad_print_status_changed;
gtkprintoperation_class->done = mousepad_print_done;
@@ -125,13 +108,7 @@ static void
mousepad_print_init (MousepadPrint *print)
{
/* init */
- print->print_page_headers = FALSE;
- print->print_line_numbers = FALSE;
- print->text_wrapping = FALSE;
- print->x_offset = 0;
- print->y_offset = 0;
- print->line_number = 0;
- print->font_name = NULL;
+ print->compositor = NULL;
/* set a custom tab label */
gtk_print_operation_set_custom_tab_label (GTK_PRINT_OPERATION (print), _("Document Settings"));
@@ -145,7 +122,7 @@ mousepad_print_finalize (GObject *object)
MousepadPrint *print = MOUSEPAD_PRINT (object);
/* cleanup */
- g_free (print->font_name);
+ g_object_unref (print->compositor);
(*G_OBJECT_CLASS (mousepad_print_parent_class)->finalize) (object);
}
@@ -163,6 +140,7 @@ mousepad_print_settings_load (GtkPrintOperation *operation)
gint i;
gchar *key;
gchar *value;
+ gchar *font_name = NULL;
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
PangoContext *context;
@@ -248,23 +226,49 @@ mousepad_print_settings_load (GtkPrintOperation *operation)
}
/* restore print settings */
- print->print_page_headers = gtk_print_settings_get_bool (settings, "print-page-headers");
- print->print_line_numbers = gtk_print_settings_get_bool (settings, "print-line-numbers");
- print->text_wrapping = gtk_print_settings_get_bool (settings, "text-wrapping");
- print->font_name = g_strdup (gtk_print_settings_get (settings, "font-name"));
+ g_object_set (print->compositor,
+ "print-header",
+ gtk_print_settings_get_bool (settings, "print-page-headers"),
+#if 0
+ "print-footer",
+ gtk_print_settings_get_bool (settings, "print-page-footers"),
+#endif
+ "print-line-numbers",
+ gtk_print_settings_get_bool (settings, "print-line-numbers"),
+ "wrap-mode",
+ gtk_print_settings_get_bool (settings, "text-wrapping") ? GTK_WRAP_WORD : GTK_WRAP_NONE,
+ "highlight-syntax",
+ gtk_print_settings_get_bool (settings, "syntax-highlighting"),
+ NULL);
+
+ /* font-name setting sets the header, footer, line numbers and body fonts */
+ font_name = g_strdup (gtk_print_settings_get (settings, "font-name"));
/* release reference */
g_object_unref (G_OBJECT (settings));
}
- /* if no font name is set, get the one used in the widget */
- if (G_UNLIKELY (print->font_name == NULL))
- {
- /* get the font description from the context and convert it into a string */
- context = gtk_widget_get_pango_context (GTK_WIDGET (print->document->textview));
- font_desc = pango_context_get_font_description (context);
- print->font_name = pango_font_description_to_string (font_desc);
- }
+ /* if no font name is set, get the one used in the widget */
+ if (G_UNLIKELY (font_name == NULL))
+ {
+ /* get the font description from the context and convert it into a string */
+ context = gtk_widget_get_pango_context (GTK_WIDGET (print->document->textview));
+ font_desc = pango_context_get_font_description (context);
+ font_name = pango_font_description_to_string (font_desc);
+ }
+
+ /* set the same font for all the various parts of the pages the same */
+ g_object_set (print->compositor,
+ "body-font-name", font_name,
+ "line-numbers-font-name", font_name,
+ "header-font-name", font_name,
+#if 0
+ "footer-font-name", font_name,
+#endif
+ NULL);
+
+ /* cleanup */
+ g_free (font_name);
}
@@ -339,10 +343,29 @@ mousepad_print_settings_save (GtkPrintOperation *operation)
gtk_print_settings_set_bool (settings, "page-setup-saved", page_setup != NULL);
/* set print settings */
- gtk_print_settings_set_bool (settings, "print-page-headers", print->print_page_headers);
- gtk_print_settings_set_bool (settings, "print-line-numbers", print->print_line_numbers);
- gtk_print_settings_set_bool (settings, "text-wrapping", print->text_wrapping);
- gtk_print_settings_set (settings, "font-name", print->font_name);
+ gtk_print_settings_set_bool (settings,
+ "print-page-headers",
+ gtk_source_print_compositor_get_print_header (print->compositor));
+#if 0
+ gtk_print_settings_set_bool (settings,
+ "print-page-footers",
+ gtk_source_print_compositor_get_print_footer (print->compositor));
+#endif
+ gtk_print_settings_set_bool (settings,
+ "print-line-numbers",
+ gtk_source_print_compositor_get_print_line_numbers (print->compositor));
+
+ gtk_print_settings_set_bool (settings,
+ "text-wrapping",
+ gtk_source_print_compositor_get_wrap_mode (print->compositor) == GTK_WRAP_NONE ? FALSE : TRUE);
+
+ gtk_print_settings_set_bool (settings,
+ "syntax-highlighting",
+ gtk_source_print_compositor_get_highlight_syntax (print->compositor));
+
+ gtk_print_settings_set (settings,
+ "font-name",
+ gtk_source_print_compositor_get_body_font_name (print->compositor));
/* store all the print settings */
gtk_print_settings_foreach (settings, mousepad_print_settings_save_foreach, keyfile);
@@ -363,273 +386,46 @@ static void
mousepad_print_begin_print (GtkPrintOperation *operation,
GtkPrintContext *context)
{
- MousepadPrint *print = MOUSEPAD_PRINT (operation);
- MousepadDocument *document = print->document;
- GtkTextIter start_iter, end_iter;
- gint page_height;
- gint page_width;
- gint layout_height;
- PangoRectangle rect;
- PangoLayoutLine *line;
- PangoFontDescription *font_desc;
- gchar *text;
- gint i;
- gint size;
- gint n_pages = 1;
-
- /* create the pango layout */
- print->layout = gtk_print_context_create_pango_layout (context);
-
- /* set layout font */
- font_desc = pango_font_description_from_string (print->font_name);
- pango_layout_set_font_description (print->layout, font_desc);
- pango_font_description_free (font_desc);
-
- /* calculate page header height */
- if (print->print_page_headers)
- {
- /* set some pango layout text */
- pango_layout_set_text (print->layout, "Page 1234", -1);
-
- /* get the height */
- pango_layout_get_pixel_size (print->layout, NULL, &size);
-
- /* set the header offset */
- print->y_offset = size + DOCUMENT_SPACING;
- }
-
- /* calculate the line number offset */
- if (print->print_line_numbers)
- {
- /* insert the highest line number in the layout */
- text = g_strdup_printf ("%d", MAX (99, gtk_text_buffer_get_line_count (document->buffer)));
- pango_layout_set_text (print->layout, text, -1);
- g_free (text);
-
- /* get the width of the layout */
- pango_layout_get_pixel_size (print->layout, &size, NULL);
-
- /* set the text offset */
- print->x_offset = size + DOCUMENT_SPACING;
- }
-
- /* set the layout width */
- page_width = gtk_print_context_get_width (context);
- pango_layout_set_width (print->layout, PANGO_SCALE * (page_width - print->x_offset));
-
- /* wrapping mode for the layout */
- pango_layout_set_wrap (print->layout, print->text_wrapping ? PANGO_WRAP_WORD_CHAR : PANGO_WRAP_CHAR);
-
- /* get the text in the entire buffer */
- gtk_text_buffer_get_bounds (document->buffer, &start_iter, &end_iter);
- text = gtk_text_buffer_get_slice (document->buffer, &start_iter, &end_iter, TRUE);
-
- /* set the layout text */
- pango_layout_set_text (print->layout, text, -1);
-
- /* cleanup */
- g_free (text);
-
- /* get the page height in pango units */
- page_height = gtk_print_context_get_height (context) - print->y_offset;
-
- /* create and empty array to store the line numbers */
- print->lines = g_array_new (FALSE, FALSE, sizeof (gint));
-
- /* reset layout height */
- layout_height = 0;
-
- /* start with a zerro */
- g_array_append_val (print->lines, layout_height);
-
- for (i = 0; i < pango_layout_get_line_count (print->layout); i++)
- {
- /* get the line */
-#if PANGO_VERSION_CHECK (1, 16, 0)
- line = pango_layout_get_line_readonly (print->layout, i);
-#else
- line = pango_layout_get_line (print->layout, i);
-#endif
-
- /* if we don't wrap lines, skip the lines that don't start a paragraph */
- if (print->text_wrapping == FALSE && line->is_paragraph_start == FALSE)
- continue;
-
- /* get the line height */
- pango_layout_line_get_pixel_extents (line, NULL, &rect);
-
- /* append the height to the total page height */
- layout_height += rect.height;
-
- /* check if the layout still fits in the page */
- if (layout_height > page_height)
- {
- /* reset the layout height */
- layout_height = 0;
-
- /* increase page counter */
- n_pages++;
-
- /* append the line number to the array */
- g_array_append_val (print->lines, i);
- }
- }
-
- /* append the last line to the array */
- g_array_append_val (print->lines, i);
-
- /* set the number of pages we're going to draw */
- gtk_print_operation_set_n_pages (operation, n_pages);
-}
-
-
-
-static void
-mousepad_print_draw_page (GtkPrintOperation *operation,
- GtkPrintContext *context,
- gint page_nr)
-{
MousepadPrint *print = MOUSEPAD_PRINT (operation);
MousepadDocument *document = print->document;
- PangoLayoutLine *line;
- cairo_t *cr;
- PangoRectangle rect;
- gint i;
- gint x, y;
- gint width, height;
- gint start, end;
- gchar *text;
- PangoLayout *layout;
-
- /* get the cairo context */
- cr = gtk_print_context_get_cairo_context (context);
-
- /* get the start and end line from the array */
- start = g_array_index (print->lines, gint, page_nr);
- end = g_array_index (print->lines, gint, page_nr + 1);
-
- /* set line width */
- cairo_set_line_width (cr, 1);
-
- /* create an empty pango layout */
- layout = gtk_print_context_create_pango_layout (context);
- pango_layout_set_font_description (layout, pango_layout_get_font_description (print->layout));
+ gint n_pages = 1;
+ const gchar *file_name;
/* print header */
- if (print->print_page_headers)
+ if (gtk_source_print_compositor_get_print_header (print->compositor))
{
- /* create page number */
- text = g_strdup_printf (_("Page %d of %d"), page_nr + 1, print->lines->len - 1);
- pango_layout_set_text (layout, text, -1);
- g_free (text);
-
- /* get layout size */
- pango_layout_get_pixel_size (layout, &width, &height);
-
- /* position right of the document */
- x = gtk_print_context_get_width (context) - width;
-
- /* show the layout */
- cairo_move_to (cr, x, 0);
- pango_cairo_show_layout (cr, layout);
-
- /* set the layout width of the filename */
- pango_layout_set_width (layout, PANGO_SCALE * (x - DOCUMENT_SPACING * 2));
-
- /* ellipsize the start of the filename */
- pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_START);
-
- /* set the document filename as text */
if (mousepad_document_get_filename (document))
- pango_layout_set_text (layout, mousepad_document_get_filename (document), -1);
+ file_name = mousepad_document_get_filename (document);
else
- pango_layout_set_text (layout, mousepad_document_get_basename (document), -1);
-
- /* show the layout */
- cairo_move_to (cr, 0, 0);
- pango_cairo_show_layout (cr, layout);
+ file_name = mousepad_document_get_basename (document);
- /* stroke a line under the header */
- cairo_move_to (cr, 0, height + 1);
- cairo_rel_line_to (cr, gtk_print_context_get_width (context), 0);
- cairo_stroke (cr);
-
- /* restore the layout for the line numbers */
- pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
- pango_layout_set_width (layout, -1);
+ gtk_source_print_compositor_set_header_format (print->compositor,
+ TRUE,
+ file_name,
+ NULL,
+ "Page %N of %Q");
}
- /* position and render all lines */
- for (i = start, y = print->y_offset; i < end; i++)
- {
- /* get the line */
-#if PANGO_VERSION_CHECK (1, 16, 0)
- line = pango_layout_get_line_readonly (print->layout, i);
-#else
- line = pango_layout_get_line (print->layout, i);
-#endif
-
- /* if we don't wrap lines, skip the lines that don't start a paragraph */
- if (print->text_wrapping == FALSE && line->is_paragraph_start == FALSE)
- continue;
-
- /* get the line rectangle */
- pango_layout_line_get_pixel_extents (line, NULL, &rect);
-
- /* fix the start position */
- if (G_UNLIKELY (i == start))
- y -= rect.height / 3;
+ /* paginate all of the pages at once */
+ while (!gtk_source_print_compositor_paginate (print->compositor, context))
+ ;
- /* add the line hight to the top offset */
- y += rect.height;
+ n_pages = gtk_source_print_compositor_get_n_pages (print->compositor);
- /* move the cursor to the start of the text */
- cairo_move_to (cr, print->x_offset, y);
-
- /* show the line at the new position */
- pango_cairo_show_layout_line (cr, line);
-
- /* print line number */
- if (print->print_line_numbers && line->is_paragraph_start)
- {
- /* increase page number */
- print->line_number++;
-
- /* render a page number in the layout */
- text = g_strdup_printf ("%d", print->line_number);
- pango_layout_set_text (layout, text, -1);
- g_free (text);
-
- /* move the cursor to the start of the line */
- cairo_move_to (cr, 0, y);
-
- /* pick the first line and draw it on the cairo context */
-#if PANGO_VERSION_CHECK (1, 16, 0)
- line = pango_layout_get_line_readonly (print->layout, 0);
-#else
- line = pango_layout_get_line (print->layout, 0);
-#endif
- pango_cairo_show_layout_line (cr, line);
- }
- }
-
- /* release the layout */
- g_object_unref (G_OBJECT (layout));
+ /* set the number of pages we're going to draw */
+ gtk_print_operation_set_n_pages (operation, n_pages);
}
static void
-mousepad_print_end_print (GtkPrintOperation *operation,
- GtkPrintContext *context)
+mousepad_print_draw_page (GtkPrintOperation *operation,
+ GtkPrintContext *context,
+ gint page_nr)
{
MousepadPrint *print = MOUSEPAD_PRINT (operation);
- /* release the layout */
- g_object_unref (G_OBJECT (print->layout));
-
- /* free array */
- g_array_free (print->lines, TRUE);
+ gtk_source_print_compositor_draw_page (print->compositor, context, page_nr);
}
@@ -672,11 +468,17 @@ mousepad_print_button_toggled (GtkWidget *button,
/* save the correct setting */
if (button == print->widget_page_headers)
- print->print_page_headers = active;
+ gtk_source_print_compositor_set_print_header (print->compositor, active);
+#if 0
+ else if (button == print->widget_page_footers)
+ gtk_source_print_compositor_set_print_footer (print->compositor, active);
+#endif
else if (button == print->widget_line_numbers)
- print->print_line_numbers = active;
+ gtk_source_print_compositor_set_print_line_numbers (print->compositor, active);
else if (button == print->widget_text_wrapping)
- print->text_wrapping = active;
+ gtk_source_print_compositor_set_wrap_mode (print->compositor, active ? GTK_WRAP_WORD : GTK_WRAP_NONE);
+ else if (button == print->widget_syntax_highlighting)
+ gtk_source_print_compositor_set_highlight_syntax (print->compositor, active);
}
@@ -685,11 +487,16 @@ static void
mousepad_print_button_font_set (GtkFontButton *button,
MousepadPrint *print)
{
- /* remove old font name */
- g_free (print->font_name);
+ const gchar *font_name;
+
+ font_name = gtk_font_button_get_font_name (button);
- /* set new font */
- print->font_name = g_strdup (gtk_font_button_get_font_name (button));
+ g_object_set (print->compositor,
+ "body-font-name", font_name,
+ "line-numbers-font-name", font_name,
+ "header-font-name", font_name,
+ "footer-font-name", font_name,
+ NULL);
}
@@ -772,19 +579,38 @@ mousepad_print_create_custom_widget (GtkPrintOperation *operation)
gtk_widget_show (vbox2);
button = print->widget_page_headers = gtk_check_button_new_with_mnemonic (_("Print page _headers"));
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), print->print_page_headers);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
+ gtk_source_print_compositor_get_print_header (print->compositor));
+ g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
+ gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
+ gtk_widget_show (button);
+
+#if 0
+ button = print->widget_page_footers = gtk_check_button_new_with_mnemonic (_("Print page _footers"));
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
+ gtk_source_print_compositor_get_print_footer (print->compositor));
g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
gtk_widget_show (button);
+#endif
button = print->widget_line_numbers = gtk_check_button_new_with_mnemonic (_("Print _line numbers"));
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), print->print_line_numbers);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
+ gtk_source_print_compositor_get_print_line_numbers (print->compositor));
g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
gtk_widget_show (button);
button = print->widget_text_wrapping = gtk_check_button_new_with_mnemonic (_("Enable text _wrapping"));
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), print->text_wrapping);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
+ gtk_source_print_compositor_get_wrap_mode (print->compositor) == GTK_WRAP_NONE ? FALSE : TRUE);
+ g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
+ gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
+ gtk_widget_show (button);
+
+ button = print->widget_syntax_highlighting = gtk_check_button_new_with_mnemonic (_("Enable _syntax highlighting"));
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
+ gtk_source_print_compositor_get_highlight_syntax (print->compositor));
g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
gtk_widget_show (button);
@@ -804,7 +630,7 @@ mousepad_print_create_custom_widget (GtkPrintOperation *operation)
gtk_container_add (GTK_CONTAINER (frame), alignment);
gtk_widget_show (alignment);
- button = gtk_font_button_new_with_font (print->font_name);
+ button = gtk_font_button_new_with_font (gtk_source_print_compositor_get_body_font_name (print->compositor));
gtk_container_add (GTK_CONTAINER (alignment), button);
g_signal_connect (G_OBJECT (button), "font-set", G_CALLBACK (mousepad_print_button_font_set), print);
gtk_widget_show (button);
@@ -855,11 +681,13 @@ mousepad_print_document_interactive (MousepadPrint *print,
mousepad_return_val_if_fail (MOUSEPAD_IS_PRINT (print), FALSE);
mousepad_return_val_if_fail (GTK_IS_PRINT_OPERATION (print), FALSE);
mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
+ mousepad_return_val_if_fail (GTK_IS_SOURCE_BUFFER (document->buffer), FALSE);
mousepad_return_val_if_fail (GTK_IS_WINDOW (parent), FALSE);
mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
/* set the document */
print->document = document;
+ print->compositor = gtk_source_print_compositor_new (GTK_SOURCE_BUFFER (document->buffer));
/* load settings */
mousepad_print_settings_load (GTK_PRINT_OPERATION (print));
diff --git a/mousepad/mousepad-private.h b/mousepad/mousepad-private.h
index 921767b..49ec851 100644
--- a/mousepad/mousepad-private.h
+++ b/mousepad/mousepad-private.h
@@ -26,6 +26,7 @@
#include <gtksourceview/gtksourcestyleschememanager.h>
#include <gtksourceview/gtksourcelanguage.h>
#include <gtksourceview/gtksourcelanguagemanager.h>
+#include <gtksourceview/gtksourceprintcompositor.h>
G_BEGIN_DECLS
More information about the Xfce4-commits
mailing list