medit/moo/mooedit/mootextprint.c

1566 lines
48 KiB
C
Raw Normal View History

2006-03-25 01:40:54 -08:00
/*
* mootextprint.c
*
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* 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 2 of the License, or
* (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
2006-05-07 11:15:58 -07:00
#define MOOEDIT_COMPILATION
2006-03-25 01:40:54 -08:00
#include "mooedit/mootextprint.h"
2006-05-07 16:44:10 -07:00
#include "mooedit/mooedit.h"
2006-05-07 21:48:37 -07:00
#include "mooedit/mooprint-glade.h"
#include "mooedit/mooeditprefs.h"
#include "mooutils/mooglade.h"
#include "mooutils/moodialogs.h"
2006-05-07 16:44:10 -07:00
#include <time.h>
#include <errno.h>
#include <string.h>
2006-05-07 21:48:37 -07:00
#define PREFS_FONT MOO_EDIT_PREFS_PREFIX "/print/font"
#define PREFS_USE_STYLES MOO_EDIT_PREFS_PREFIX "/print/use_styles"
#define PREFS_USE_CUSTOM_FONT MOO_EDIT_PREFS_PREFIX "/print/use_font"
#define PREFS_WRAP MOO_EDIT_PREFS_PREFIX "/print/wrap"
#define PREFS_ELLIPSIZE MOO_EDIT_PREFS_PREFIX "/print/ellipsize"
#define PREFS_PRINT_HEADER MOO_EDIT_PREFS_PREFIX "/print/header/print"
#define PREFS_PRINT_HEADER_SEPARATOR MOO_EDIT_PREFS_PREFIX "/print/header/separator"
#define PREFS_HEADER_LEFT MOO_EDIT_PREFS_PREFIX "/print/header/left"
#define PREFS_HEADER_CENTER MOO_EDIT_PREFS_PREFIX "/print/header/center"
#define PREFS_HEADER_RIGHT MOO_EDIT_PREFS_PREFIX "/print/header/right"
#define PREFS_PRINT_FOOTER MOO_EDIT_PREFS_PREFIX "/print/footer/print"
#define PREFS_PRINT_FOOTER_SEPARATOR MOO_EDIT_PREFS_PREFIX "/print/footer/separator"
#define PREFS_FOOTER_LEFT MOO_EDIT_PREFS_PREFIX "/print/footer/left"
#define PREFS_FOOTER_CENTER MOO_EDIT_PREFS_PREFIX "/print/footer/center"
#define PREFS_FOOTER_RIGHT MOO_EDIT_PREFS_PREFIX "/print/footer/right"
2006-05-07 16:44:10 -07:00
#define SET_OPTION(print, opt, val) \
G_STMT_START { \
if (val) \
(print)->options |= (opt); \
else \
(print)->options &= (~(opt)); \
} G_STMT_END
#define GET_OPTION(print, opt) ((print->options &= opt) != 0)
typedef struct _HFFormat HFFormat;
static HFFormat *hf_format_parse (const char *strformat);
static void hf_format_free (HFFormat *format);
static char *hf_format_eval (HFFormat *format,
struct tm *tm,
2006-05-13 14:09:09 -07:00
int page,
2006-05-07 16:44:10 -07:00
int total_pages,
const char *filename,
const char *basename);
2006-03-25 01:40:54 -08:00
static GtkPageSetup *page_setup;
static GtkPrintSettings *print_settings;
static void load_default_settings (void);
2006-05-07 21:48:37 -07:00
static void moo_print_init_prefs (void);
2006-03-25 01:40:54 -08:00
static void moo_print_operation_finalize (GObject *object);
static void moo_print_operation_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void moo_print_operation_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void moo_print_operation_begin_print (GtkPrintOperation *operation,
GtkPrintContext *context);
static void moo_print_operation_draw_page (GtkPrintOperation *operation,
GtkPrintContext *context,
2006-05-13 14:09:09 -07:00
int page);
2006-03-25 01:40:54 -08:00
static void moo_print_operation_end_print (GtkPrintOperation *operation,
GtkPrintContext *context);
2006-05-24 00:06:19 -07:00
static GtkWidget *moo_print_operation_create_custom_widget (GtkPrintOperation *operation);
static void moo_print_operation_custom_widget_apply (GtkPrintOperation *operation,
GtkWidget *widget);
2006-03-25 01:40:54 -08:00
2006-05-07 11:15:58 -07:00
G_DEFINE_TYPE(MooPrintOperation, _moo_print_operation, GTK_TYPE_PRINT_OPERATION)
2006-03-25 01:40:54 -08:00
enum {
PROP_0,
2006-03-27 17:27:57 -08:00
PROP_DOC,
PROP_BUFFER,
PROP_WRAP,
PROP_WRAP_MODE,
PROP_ELLIPSIZE,
PROP_FONT,
PROP_USE_STYLES
2006-03-25 01:40:54 -08:00
};
2006-05-07 16:44:10 -07:00
static void
header_footer_destroy (MooPrintHeaderFooter *hf)
{
int i;
if (hf->font)
g_object_unref (hf->font);
if (hf->layout)
g_object_unref (hf->layout);
for (i = 0; i < 3; ++i)
{
g_free (hf->format[i]);
hf_format_free (hf->parsed_format[i]);
}
}
2006-03-25 01:40:54 -08:00
static void
moo_print_operation_finalize (GObject *object)
{
MooPrintOperation *print = MOO_PRINT_OPERATION (object);
if (print->doc)
g_object_unref (print->doc);
if (print->buffer)
g_object_unref (print->buffer);
2006-05-07 16:44:10 -07:00
header_footer_destroy (&print->header);
header_footer_destroy (&print->footer);
g_free (print->tm);
g_free (print->filename);
g_free (print->basename);
2006-03-25 01:40:54 -08:00
2006-05-07 11:15:58 -07:00
G_OBJECT_CLASS(_moo_print_operation_parent_class)->finalize (object);
2006-03-25 01:40:54 -08:00
}
static void
moo_print_operation_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MooPrintOperation *print = MOO_PRINT_OPERATION (object);
switch (prop_id)
{
case PROP_DOC:
2006-05-07 11:15:58 -07:00
_moo_print_operation_set_doc (print, g_value_get_object (value));
2006-03-25 01:40:54 -08:00
break;
2006-03-27 17:27:57 -08:00
case PROP_BUFFER:
2006-05-07 11:15:58 -07:00
_moo_print_operation_set_buffer (print, g_value_get_object (value));
2006-03-27 17:27:57 -08:00
break;
case PROP_WRAP:
2006-05-07 16:44:10 -07:00
SET_OPTION (print, MOO_PRINT_WRAP, g_value_get_boolean (value));
2006-03-27 17:27:57 -08:00
g_object_notify (object, "wrap");
break;
case PROP_WRAP_MODE:
print->wrap_mode = g_value_get_enum (value);
g_object_notify (object, "wrap-mode");
break;
case PROP_ELLIPSIZE:
2006-05-07 16:44:10 -07:00
SET_OPTION (print, MOO_PRINT_ELLIPSIZE, g_value_get_boolean (value));
2006-03-27 17:27:57 -08:00
g_object_notify (object, "ellipsize");
break;
case PROP_FONT:
g_free (print->font);
print->font = g_value_dup_string (value);
g_object_notify (object, "font");
break;
case PROP_USE_STYLES:
2006-05-07 16:44:10 -07:00
SET_OPTION (print, MOO_PRINT_USE_STYLES, g_value_get_boolean (value));
2006-03-27 17:27:57 -08:00
g_object_notify (object, "use-styles");
break;
2006-03-25 01:40:54 -08:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
moo_print_operation_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MooPrintOperation *print = MOO_PRINT_OPERATION (object);
switch (prop_id)
{
case PROP_DOC:
g_value_set_object (value, print->doc);
break;
2006-03-27 17:27:57 -08:00
case PROP_BUFFER:
g_value_set_object (value, print->buffer);
break;
case PROP_WRAP:
2006-05-07 16:44:10 -07:00
g_value_set_boolean (value, GET_OPTION (print, MOO_PRINT_WRAP));
2006-03-27 17:27:57 -08:00
break;
case PROP_WRAP_MODE:
g_value_set_enum (value, print->wrap_mode);
break;
case PROP_ELLIPSIZE:
2006-05-07 16:44:10 -07:00
g_value_set_boolean (value, GET_OPTION (print, MOO_PRINT_ELLIPSIZE));
2006-03-27 17:27:57 -08:00
break;
case PROP_FONT:
g_value_set_string (value, print->font);
break;
case PROP_USE_STYLES:
2006-05-07 16:44:10 -07:00
g_value_set_boolean (value, GET_OPTION (print, MOO_PRINT_USE_STYLES));
2006-03-27 17:27:57 -08:00
break;
2006-03-25 01:40:54 -08:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
2006-05-07 11:15:58 -07:00
_moo_print_operation_class_init (MooPrintOperationClass *klass)
2006-03-25 01:40:54 -08:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkPrintOperationClass *print_class = GTK_PRINT_OPERATION_CLASS (klass);
object_class->finalize = moo_print_operation_finalize;
object_class->set_property = moo_print_operation_set_property;
object_class->get_property = moo_print_operation_get_property;
print_class->begin_print = moo_print_operation_begin_print;
print_class->draw_page = moo_print_operation_draw_page;
print_class->end_print = moo_print_operation_end_print;
2006-05-24 00:06:19 -07:00
print_class->create_custom_widget = moo_print_operation_create_custom_widget;
print_class->custom_widget_apply = moo_print_operation_custom_widget_apply;
2006-03-25 01:40:54 -08:00
g_object_class_install_property (object_class,
PROP_DOC,
g_param_spec_object ("doc",
"doc",
"doc",
GTK_TYPE_TEXT_VIEW,
2006-03-27 17:27:57 -08:00
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_BUFFER,
g_param_spec_object ("buffer",
"buffer",
"buffer",
GTK_TYPE_TEXT_BUFFER,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_FONT,
g_param_spec_string ("font",
"font",
"font",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_WRAP,
g_param_spec_boolean ("wrap",
"wrap",
"wrap",
TRUE,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_WRAP_MODE,
g_param_spec_enum ("wrap-mode",
"wrap-mode",
"wrap-mode",
PANGO_TYPE_WRAP_MODE,
PANGO_WRAP_WORD_CHAR,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_ELLIPSIZE,
g_param_spec_boolean ("ellipsize",
"ellipsize",
"ellipsize",
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_USE_STYLES,
g_param_spec_boolean ("use-styles",
"use-styles",
"use-styles",
TRUE,
G_PARAM_READWRITE));
2006-03-25 01:40:54 -08:00
}
static void
2006-05-07 11:15:58 -07:00
_moo_print_operation_init (MooPrintOperation *print)
2006-03-25 01:40:54 -08:00
{
load_default_settings ();
2006-03-27 17:27:57 -08:00
2006-03-25 01:40:54 -08:00
gtk_print_operation_set_print_settings (GTK_PRINT_OPERATION (print),
print_settings);
if (page_setup)
gtk_print_operation_set_default_page_setup (GTK_PRINT_OPERATION (print),
page_setup);
2006-03-26 10:17:51 -08:00
print->last_line = -1;
2006-05-07 16:44:10 -07:00
print->options = MOO_PRINT_USE_STYLES |
MOO_PRINT_HEADER | MOO_PRINT_FOOTER;
2006-03-27 17:27:57 -08:00
print->wrap_mode = PANGO_WRAP_WORD_CHAR;
2006-05-07 16:44:10 -07:00
print->header.separator = TRUE;
print->footer.separator = TRUE;
2006-03-25 01:40:54 -08:00
}
void
2006-05-07 11:15:58 -07:00
_moo_print_operation_set_doc (MooPrintOperation *print,
GtkTextView *doc)
2006-03-25 01:40:54 -08:00
{
g_return_if_fail (MOO_IS_PRINT_OPERATION (print));
g_return_if_fail (!doc || GTK_IS_TEXT_VIEW (doc));
2006-03-27 17:27:57 -08:00
if (print->doc == doc)
return;
2006-03-25 01:40:54 -08:00
if (print->doc)
g_object_unref (print->doc);
2006-03-25 11:24:35 -08:00
if (print->buffer)
g_object_unref (print->buffer);
2006-03-25 01:40:54 -08:00
print->doc = doc;
if (print->doc)
{
g_object_ref (print->doc);
print->buffer = gtk_text_view_get_buffer (print->doc);
2006-03-25 11:24:35 -08:00
g_object_ref (print->buffer);
2006-03-25 01:40:54 -08:00
}
else
{
print->buffer = NULL;
}
2006-03-27 17:27:57 -08:00
g_object_freeze_notify (G_OBJECT (print));
g_object_notify (G_OBJECT (print), "doc");
g_object_notify (G_OBJECT (print), "buffer");
g_object_thaw_notify (G_OBJECT (print));
}
void
2006-05-07 11:15:58 -07:00
_moo_print_operation_set_buffer (MooPrintOperation *print,
GtkTextBuffer *buffer)
2006-03-27 17:27:57 -08:00
{
g_return_if_fail (MOO_IS_PRINT_OPERATION (print));
g_return_if_fail (!buffer || GTK_IS_TEXT_BUFFER (buffer));
if (print->buffer == buffer)
return;
if (print->doc)
g_object_unref (print->doc);
if (print->buffer)
g_object_unref (print->buffer);
print->doc = NULL;
print->buffer = buffer;
if (print->buffer)
g_object_ref (print->buffer);
g_object_notify (G_OBJECT (print), "buffer");
2006-03-25 01:40:54 -08:00
}
static void
load_default_settings (void)
{
if (!print_settings)
print_settings = gtk_print_settings_new ();
}
void
2006-05-07 11:15:58 -07:00
_moo_edit_page_setup (GtkTextView *view,
GtkWidget *parent)
2006-03-25 01:40:54 -08:00
{
GtkPageSetup *new_page_setup;
GtkWindow *parent_window = NULL;
g_return_if_fail (!view || GTK_IS_TEXT_VIEW (view));
load_default_settings ();
if (!parent && view)
parent = GTK_WIDGET (view);
if (parent)
parent = gtk_widget_get_toplevel (parent);
if (parent && GTK_WIDGET_TOPLEVEL (parent))
parent_window = GTK_WINDOW (parent);
new_page_setup = gtk_print_run_page_setup_dialog (parent_window,
page_setup,
print_settings);
if (page_setup)
g_object_unref (page_setup);
page_setup = new_page_setup;
}
2006-05-07 16:44:10 -07:00
static void
header_footer_get_size (MooPrintHeaderFooter *hf,
MooPrintOperation *print,
GtkPrintContext *context,
PangoFontDescription *default_font)
{
PangoFontDescription *font = default_font;
PangoRectangle rect;
hf->do_print = GET_OPTION (print, MOO_PRINT_HEADER) &&
2006-05-07 21:48:37 -07:00
(hf->parsed_format[0] || hf->parsed_format[1] || hf->parsed_format[2]);
2006-05-07 16:44:10 -07:00
if (hf->layout)
g_object_unref (hf->layout);
hf->layout = NULL;
if (!hf->do_print)
return;
if (hf->font)
font = hf->font;
hf->layout = gtk_print_context_create_layout (context);
pango_layout_set_text (hf->layout, "AAAyyy", -1);
if (font)
pango_layout_set_font_description (hf->layout, font);
pango_layout_get_pixel_extents (hf->layout, NULL, &rect);
hf->text_height = rect.height;
if (hf->separator)
{
hf->separator_height = 1.;
hf->separator_before = hf->text_height / 2;
hf->separator_after = hf->text_height / 2;
}
else
{
hf->separator_height = .0;
hf->separator_before = 0.;
hf->separator_after = 0.;
}
}
static void
moo_print_operation_calc_page_size (MooPrintOperation *print,
GtkPrintContext *context,
PangoFontDescription *default_font)
{
print->page.x = 0.;
print->page.y = 0.;
print->page.width = gtk_print_context_get_width (context);
print->page.height = gtk_print_context_get_height (context);
header_footer_get_size (&print->header, print, context, default_font);
header_footer_get_size (&print->footer, print, context, default_font);
if (print->header.do_print)
{
double delta = print->header.text_height + print->header.separator_before +
print->header.separator_after + print->header.separator_height;
print->page.y += delta;
print->page.height -= delta;
}
if (print->footer.do_print)
{
double delta = print->footer.text_height + print->footer.separator_before +
print->footer.separator_after + print->footer.separator_height;
print->page.height -= delta;
}
if (print->page.height < 0)
{
g_critical ("%s: page messed up", G_STRLOC);
print->page.y = 0.;
print->page.height = gtk_print_context_get_height (context);
}
}
2006-03-25 01:40:54 -08:00
static void
moo_print_operation_begin_print (GtkPrintOperation *operation,
GtkPrintContext *context)
{
MooPrintOperation *print = MOO_PRINT_OPERATION (operation);
2006-03-26 10:17:51 -08:00
PangoFontDescription *font = NULL;
2006-03-25 01:40:54 -08:00
double page_height;
2006-03-27 02:55:27 -08:00
GtkTextIter iter, print_end;
2006-03-26 10:17:51 -08:00
GTimer *timer;
2006-05-07 16:44:10 -07:00
time_t t;
2006-03-25 01:40:54 -08:00
2006-03-27 17:27:57 -08:00
g_return_if_fail (print->buffer != NULL);
2006-03-26 10:17:51 -08:00
g_return_if_fail (print->first_line >= 0);
g_return_if_fail (print->last_line < 0 || print->last_line >= print->first_line);
g_return_if_fail (print->first_line < gtk_text_buffer_get_line_count (print->buffer));
g_return_if_fail (print->last_line < gtk_text_buffer_get_line_count (print->buffer));
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
timer = g_timer_new ();
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
if (print->last_line < 0)
print->last_line = gtk_text_buffer_get_line_count (print->buffer) - 1;
if (print->font)
font = pango_font_description_from_string (print->font);
2006-03-27 17:27:57 -08:00
if (!font && print->doc)
2006-03-26 10:17:51 -08:00
{
PangoContext *widget_ctx;
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
widget_ctx = gtk_widget_get_pango_context (GTK_WIDGET (print->doc));
if (widget_ctx)
{
font = pango_context_get_font_description (widget_ctx);
if (font)
font = pango_font_description_copy_static (font);
}
}
2006-03-25 01:40:54 -08:00
2006-05-07 16:44:10 -07:00
moo_print_operation_calc_page_size (print, context, font);
2006-03-26 10:17:51 -08:00
print->layout = gtk_print_context_create_layout (context);
2006-03-25 01:40:54 -08:00
2006-05-07 16:44:10 -07:00
if (GET_OPTION (print, MOO_PRINT_WRAP))
2006-03-27 02:55:27 -08:00
{
pango_layout_set_width (print->layout, print->page.width * PANGO_SCALE);
2006-03-27 17:27:57 -08:00
pango_layout_set_wrap (print->layout, print->wrap_mode);
2006-03-27 02:55:27 -08:00
}
2006-05-07 16:44:10 -07:00
else if (GET_OPTION (print, MOO_PRINT_ELLIPSIZE))
2006-03-27 02:55:27 -08:00
{
pango_layout_set_width (print->layout, print->page.width * PANGO_SCALE);
pango_layout_set_ellipsize (print->layout, PANGO_ELLIPSIZE_END);
}
2006-03-26 10:17:51 -08:00
if (font)
{
pango_layout_set_font_description (print->layout, font);
pango_font_description_free (font);
}
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
print->pages = g_array_new (FALSE, FALSE, sizeof (GtkTextIter));
gtk_text_buffer_get_iter_at_line (print->buffer, &iter,
print->first_line);
2006-03-27 02:55:27 -08:00
gtk_text_buffer_get_iter_at_line (print->buffer, &print_end,
print->last_line);
gtk_text_iter_forward_line (&print_end);
2006-03-26 10:17:51 -08:00
g_array_append_val (print->pages, iter);
2006-03-25 01:40:54 -08:00
page_height = 0;
2006-03-27 02:55:27 -08:00
while (gtk_text_iter_compare (&iter, &print_end) < 0)
2006-03-25 01:40:54 -08:00
{
2006-03-26 10:17:51 -08:00
GtkTextIter end;
PangoRectangle line_rect;
2006-03-25 01:40:54 -08:00
double line_height;
2006-03-26 10:17:51 -08:00
char *text;
end = iter;
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
if (!gtk_text_iter_ends_line (&end))
gtk_text_iter_forward_to_line_end (&end);
2006-03-25 01:40:54 -08:00
text = gtk_text_buffer_get_slice (print->buffer, &iter, &end, TRUE);
2006-03-26 10:17:51 -08:00
pango_layout_set_text (print->layout, text, -1);
g_free (text);
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
pango_layout_get_pixel_extents (print->layout, NULL, &line_rect);
2006-03-27 02:55:27 -08:00
line_height = line_rect.height;
2006-03-26 10:17:51 -08:00
2006-03-27 02:55:27 -08:00
gtk_text_iter_forward_line (&iter);
2006-05-07 16:44:10 -07:00
if (page_height + line_height > print->page.height + 5.)
2006-03-25 01:40:54 -08:00
{
2006-03-27 02:55:27 -08:00
gboolean part = FALSE;
PangoLayoutIter *layout_iter;
layout_iter = pango_layout_get_iter (print->layout);
2006-05-07 16:44:10 -07:00
if (GET_OPTION (print, MOO_PRINT_WRAP) && pango_layout_get_line_count (print->layout) > 1)
2006-03-27 02:55:27 -08:00
{
double part_height = 0;
do
{
PangoLayoutLine *layout_line;
layout_line = pango_layout_iter_get_line (layout_iter);
pango_layout_line_get_pixel_extents (layout_line, NULL, &line_rect);
2006-05-07 16:44:10 -07:00
if (page_height + part_height + line_rect.height > print->page.height + 5.)
2006-03-27 02:55:27 -08:00
break;
part_height += line_rect.height;
part = TRUE;
}
while (pango_layout_iter_next_line (layout_iter));
}
if (part)
{
int index = pango_layout_iter_get_index (layout_iter);
iter = end;
gtk_text_iter_set_line_index (&iter, index);
line_height = 0;
}
pango_layout_iter_free (layout_iter);
2006-03-26 10:17:51 -08:00
g_array_append_val (print->pages, iter);
2006-03-25 01:40:54 -08:00
page_height = 0;
}
2006-05-07 16:44:10 -07:00
else
{
page_height += line_height;
}
2006-03-25 01:40:54 -08:00
}
2006-05-13 14:09:09 -07:00
gtk_print_operation_set_n_pages (operation, print->pages->len);
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
g_message ("begin_print: %d pages in %f s", print->pages->len,
g_timer_elapsed (timer, NULL));
g_timer_destroy (timer);
2006-05-07 16:44:10 -07:00
if (!print->tm)
print->tm = g_new (struct tm, 1);
errno = 0;
time (&t);
if (errno)
{
int err = errno;
g_critical ("time: %s", g_strerror (err));
g_free (print->tm);
print->tm = NULL;
}
else if (!localtime_r (&t, print->tm))
{
int err = errno;
g_critical ("time: %s", g_strerror (err));
g_free (print->tm);
print->tm = NULL;
}
2006-03-25 01:40:54 -08:00
}
2006-03-27 17:27:57 -08:00
static GSList *
iter_get_attrs (GtkTextIter *iter,
const GtkTextIter *limit)
{
GSList *attrs = NULL, *tags;
PangoAttribute *bg = NULL, *fg = NULL, *style = NULL, *ul = NULL;
PangoAttribute *weight = NULL, *st = NULL;
tags = gtk_text_iter_get_tags (iter);
gtk_text_iter_forward_to_tag_toggle (iter, NULL);
if (gtk_text_iter_compare (iter, limit) > 0)
*iter = *limit;
while (tags)
{
GtkTextTag *tag = tags->data;
gboolean bg_set, fg_set, style_set, ul_set, weight_set, st_set;
g_object_get (tag,
"background-set", &bg_set,
"foreground-set", &fg_set,
"style-set", &style_set,
"underline-set", &ul_set,
"weight-set", &weight_set,
"strikethrough-set", &st_set,
NULL);
if (bg_set)
{
GdkColor *color = NULL;
if (bg) pango_attribute_destroy (bg);
g_object_get (tag, "background-gdk", &color, NULL);
bg = pango_attr_background_new (color->red, color->green, color->blue);
gdk_color_free (color);
}
if (fg_set)
{
GdkColor *color = NULL;
if (fg) pango_attribute_destroy (fg);
g_object_get (tag, "foreground-gdk", &color, NULL);
fg = pango_attr_foreground_new (color->red, color->green, color->blue);
gdk_color_free (color);
}
if (style_set)
{
PangoStyle style_value;
if (style) pango_attribute_destroy (style);
g_object_get (tag, "style", &style_value, NULL);
style = pango_attr_style_new (style_value);
}
if (ul_set)
{
PangoUnderline underline;
if (ul) pango_attribute_destroy (ul);
g_object_get (tag, "underline", &underline, NULL);
ul = pango_attr_underline_new (underline);
}
if (weight_set)
{
PangoWeight weight_value;
if (weight) pango_attribute_destroy (weight);
g_object_get (tag, "weight", &weight_value, NULL);
weight = pango_attr_weight_new (weight_value);
}
if (st_set)
{
gboolean strikethrough;
if (st) pango_attribute_destroy (st);
g_object_get (tag, "strikethrough", &strikethrough, NULL);
st = pango_attr_strikethrough_new (strikethrough);
}
tags = g_slist_delete_link (tags, tags);
}
if (bg)
attrs = g_slist_prepend (attrs, bg);
if (fg)
attrs = g_slist_prepend (attrs, fg);
if (style)
attrs = g_slist_prepend (attrs, style);
if (ul)
attrs = g_slist_prepend (attrs, ul);
if (weight)
attrs = g_slist_prepend (attrs, weight);
if (st)
attrs = g_slist_prepend (attrs, st);
return attrs;
}
static void
fill_layout (PangoLayout *layout,
const GtkTextIter *start,
const GtkTextIter *end)
{
char *text;
PangoAttrList *attr_list;
GtkTextIter segm_start, segm_end;
int start_index;
text = gtk_text_iter_get_text (start, end);
pango_layout_set_text (layout, text, -1);
attr_list = NULL;
segm_start = *start;
start_index = gtk_text_iter_get_line_index (start);
while (gtk_text_iter_compare (&segm_start, end) < 0)
{
GSList *attrs;
segm_end = segm_start;
attrs = iter_get_attrs (&segm_end, end);
if (attrs)
{
int si, ei;
si = gtk_text_iter_get_line_index (&segm_start) - start_index;
ei = gtk_text_iter_get_line_index (&segm_end) - start_index;
while (attrs)
{
PangoAttribute *a = attrs->data;
a->start_index = si;
a->end_index = ei;
if (!attr_list)
attr_list = pango_attr_list_new ();
pango_attr_list_insert (attr_list, a);
attrs = g_slist_delete_link (attrs, attrs);
}
}
segm_start = segm_end;
}
pango_layout_set_attributes (layout, attr_list);
if (attr_list)
pango_attr_list_unref (attr_list);
}
2006-05-07 16:44:10 -07:00
static void
print_header_footer (MooPrintOperation *print,
cairo_t *cr,
MooPrintHeaderFooter *hf,
2006-05-13 14:09:09 -07:00
int page,
2006-05-07 16:44:10 -07:00
gboolean header)
{
double y;
char *text;
PangoRectangle rect;
int total_pages;
if (header)
y = 0;
else
y = print->page.y + print->page.height + hf->separator_before +
hf->separator_after + hf->separator_height;
2006-05-13 14:09:09 -07:00
g_object_get (print, "n-pages", &total_pages, NULL);
2006-05-07 16:44:10 -07:00
if (hf->parsed_format[0] &&
2006-05-13 14:09:09 -07:00
(text = hf_format_eval (hf->parsed_format[0], print->tm, page,
2006-05-07 16:44:10 -07:00
total_pages, print->filename, print->basename)))
{
pango_layout_set_text (hf->layout, text, -1);
cairo_move_to (cr, print->page.x, y);
pango_cairo_show_layout (cr, hf->layout);
g_free (text);
}
if (hf->parsed_format[1] &&
2006-05-13 14:09:09 -07:00
(text = hf_format_eval (hf->parsed_format[1], print->tm, page,
2006-05-07 16:44:10 -07:00
total_pages, print->filename, print->basename)))
{
pango_layout_set_text (hf->layout, text, -1);
pango_layout_get_pixel_extents (hf->layout, NULL, &rect);
cairo_move_to (cr, print->page.x + print->page.width/2 - rect.width/2, y);
pango_cairo_show_layout (cr, hf->layout);
g_free (text);
}
if (hf->parsed_format[2] &&
2006-05-13 14:09:09 -07:00
(text = hf_format_eval (hf->parsed_format[2], print->tm, page,
2006-05-07 16:44:10 -07:00
total_pages, print->filename, print->basename)))
{
pango_layout_set_text (hf->layout, text, -1);
pango_layout_get_pixel_extents (hf->layout, NULL, &rect);
cairo_move_to (cr, print->page.x + print->page.width - rect.width, y);
pango_cairo_show_layout (cr, hf->layout);
g_free (text);
}
if (hf->separator)
{
if (header)
y = hf->text_height + hf->separator_before + hf->separator_height/2;
else
y = print->page.y + print->page.height +
hf->separator_after + hf->separator_height/2;
cairo_move_to (cr, print->page.x, y);
cairo_set_line_width (cr, hf->separator_height);
cairo_line_to (cr, print->page.x + print->page.width, y);
cairo_stroke (cr);
}
}
2006-03-27 17:27:57 -08:00
static void
print_page (MooPrintOperation *print,
const GtkTextIter *start,
const GtkTextIter *end,
2006-05-13 14:09:09 -07:00
int page,
2006-03-27 17:27:57 -08:00
cairo_t *cr)
{
char *text;
GtkTextIter line_start, line_end;
double offset;
cairo_set_source_rgb (cr, 0, 0, 0);
2006-05-07 16:44:10 -07:00
if (print->header.do_print)
2006-05-13 14:09:09 -07:00
print_header_footer (print, cr, &print->header, page, TRUE);
2006-05-07 16:44:10 -07:00
if (print->footer.do_print)
2006-05-13 14:09:09 -07:00
print_header_footer (print, cr, &print->footer, page, FALSE);
2006-05-07 16:44:10 -07:00
if (!GET_OPTION (print, MOO_PRINT_USE_STYLES))
2006-03-27 17:27:57 -08:00
{
text = gtk_text_buffer_get_slice (print->buffer, start, end, TRUE);
2006-03-27 17:27:57 -08:00
pango_layout_set_text (print->layout, text, -1);
g_free (text);
cairo_move_to (cr, print->page.x, print->page.y);
pango_cairo_show_layout (cr, print->layout);
return;
}
line_start = *start;
2006-05-07 16:44:10 -07:00
offset = print->page.y;
2006-03-27 17:27:57 -08:00
while (gtk_text_iter_compare (&line_start, end) < 0)
{
PangoRectangle line_rect;
if (gtk_text_iter_ends_line (&line_start))
{
pango_layout_set_text (print->layout, "", 0);
pango_layout_set_attributes (print->layout, NULL);
}
else
{
line_end = line_start;
gtk_text_iter_forward_to_line_end (&line_end);
if (gtk_text_iter_compare (&line_end, end) > 0)
line_end = *end;
fill_layout (print->layout,&line_start, &line_end);
}
2006-05-07 16:44:10 -07:00
cairo_move_to (cr, print->page.x, offset);
2006-03-27 17:27:57 -08:00
pango_cairo_show_layout (cr, print->layout);
pango_layout_get_pixel_extents (print->layout, NULL, &line_rect);
offset += line_rect.height;
gtk_text_iter_forward_line (&line_start);
}
}
2006-03-25 01:40:54 -08:00
static void
moo_print_operation_draw_page (GtkPrintOperation *operation,
GtkPrintContext *context,
2006-05-13 14:09:09 -07:00
int page)
2006-03-25 01:40:54 -08:00
{
cairo_t *cr;
2006-03-26 10:17:51 -08:00
GtkTextIter start, end;
2006-03-25 01:40:54 -08:00
MooPrintOperation *print = MOO_PRINT_OPERATION (operation);
2006-03-26 10:17:51 -08:00
GTimer *timer;
2006-03-25 01:40:54 -08:00
2006-03-27 17:27:57 -08:00
g_return_if_fail (print->buffer != NULL);
2006-03-26 10:17:51 -08:00
g_return_if_fail (print->pages != NULL);
g_return_if_fail (print->layout != NULL);
2006-05-13 14:09:09 -07:00
g_return_if_fail (page < (int) print->pages->len);
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
timer = g_timer_new ();
2006-03-25 01:40:54 -08:00
cr = gtk_print_context_get_cairo (context);
2006-05-13 14:09:09 -07:00
start = g_array_index (print->pages, GtkTextIter, page);
2006-03-25 01:40:54 -08:00
2006-05-13 14:09:09 -07:00
if (page + 1 < (int) print->pages->len)
end = g_array_index (print->pages, GtkTextIter, page + 1);
2006-03-26 10:17:51 -08:00
else
gtk_text_buffer_get_end_iter (print->buffer, &end);
2006-03-25 01:40:54 -08:00
2006-05-13 14:09:09 -07:00
print_page (print, &start, &end, page, cr);
2006-03-25 01:40:54 -08:00
2006-05-13 14:09:09 -07:00
g_message ("page %d: %f s", page, g_timer_elapsed (timer, NULL));
2006-03-26 10:17:51 -08:00
g_timer_destroy (timer);
2006-03-25 01:40:54 -08:00
}
static void
moo_print_operation_end_print (GtkPrintOperation *operation,
2006-05-07 11:15:58 -07:00
G_GNUC_UNUSED GtkPrintContext *context)
2006-03-25 01:40:54 -08:00
{
MooPrintOperation *print = MOO_PRINT_OPERATION (operation);
2006-03-27 17:27:57 -08:00
g_return_if_fail (print->buffer != NULL);
2006-03-25 01:40:54 -08:00
2006-03-26 10:17:51 -08:00
g_array_free (print->pages, TRUE);
2006-03-25 01:40:54 -08:00
print->layout = NULL;
2006-03-26 10:17:51 -08:00
print->pages = NULL;
2006-03-25 01:40:54 -08:00
}
2006-05-07 21:48:37 -07:00
static void
moo_print_operation_load_prefs (MooPrintOperation *print)
{
char *formats[3];
moo_print_init_prefs ();
formats[0] = g_strdup (moo_prefs_get_string (PREFS_HEADER_LEFT));
formats[1] = g_strdup (moo_prefs_get_string (PREFS_HEADER_CENTER));
formats[2] = g_strdup (moo_prefs_get_string (PREFS_HEADER_RIGHT));
_moo_print_operation_set_header_format (MOO_PRINT_OPERATION (print),
formats[0], formats[1], formats[2]);
g_free (formats[0]);
g_free (formats[1]);
g_free (formats[2]);
formats[0] = g_strdup (moo_prefs_get_string (PREFS_FOOTER_LEFT));
formats[1] = g_strdup (moo_prefs_get_string (PREFS_FOOTER_CENTER));
formats[2] = g_strdup (moo_prefs_get_string (PREFS_FOOTER_RIGHT));
_moo_print_operation_set_footer_format (MOO_PRINT_OPERATION (print),
formats[0], formats[1], formats[2]);
g_free (formats[0]);
g_free (formats[1]);
g_free (formats[2]);
if (moo_prefs_get_bool (PREFS_USE_CUSTOM_FONT))
{
const char *font = moo_prefs_get_string (PREFS_FONT);
g_object_set (print, "font", font, NULL);
}
g_object_set (print, "use-styles", moo_prefs_get_bool (PREFS_USE_STYLES),
"wrap", moo_prefs_get_bool (PREFS_WRAP),
"ellipsize", moo_prefs_get_bool (PREFS_ELLIPSIZE),
NULL);
}
2006-03-25 01:40:54 -08:00
void
2006-05-07 11:15:58 -07:00
_moo_edit_print (GtkTextView *view,
GtkWidget *parent)
2006-03-25 01:40:54 -08:00
{
GtkWindow *parent_window = NULL;
GtkWidget *error_dialog;
GtkPrintOperation *print;
GtkPrintOperationResult res;
GError *error = NULL;
g_return_if_fail (GTK_IS_TEXT_VIEW (view));
2006-05-24 00:06:19 -07:00
print = g_object_new (MOO_TYPE_PRINT_OPERATION,
"doc", view, "show-progress", TRUE, NULL);
2006-03-25 01:40:54 -08:00
2006-05-07 16:44:10 -07:00
if (MOO_IS_EDIT (view))
{
2006-05-07 21:48:37 -07:00
moo_print_operation_load_prefs (MOO_PRINT_OPERATION (print));
2006-05-07 16:44:10 -07:00
_moo_print_operation_set_filename (MOO_PRINT_OPERATION (print),
moo_edit_get_display_filename (MOO_EDIT (view)),
moo_edit_get_display_basename (MOO_EDIT (view)));
}
2006-03-25 01:40:54 -08:00
if (!parent)
parent = GTK_WIDGET (view);
2006-03-27 17:27:57 -08:00
2006-03-25 01:40:54 -08:00
parent = gtk_widget_get_toplevel (parent);
2006-03-27 17:27:57 -08:00
2006-03-25 01:40:54 -08:00
if (GTK_WIDGET_TOPLEVEL (parent))
parent_window = GTK_WINDOW (parent);
res = gtk_print_operation_run (print, parent_window, &error);
switch (res)
{
case GTK_PRINT_OPERATION_RESULT_ERROR:
error_dialog = gtk_message_dialog_new (parent_window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
"Error printing file:\n%s",
error ? error->message : "");
gtk_dialog_run (GTK_DIALOG (error_dialog));
gtk_widget_destroy (error_dialog);
g_error_free (error);
break;
case GTK_PRINT_OPERATION_RESULT_APPLY:
if (print_settings)
g_object_unref (print_settings);
print_settings = gtk_print_operation_get_print_settings (print);
g_object_ref (print_settings);
break;
case GTK_PRINT_OPERATION_RESULT_CANCEL:
break;
}
g_object_unref (print);
}
2006-05-07 16:44:10 -07:00
void
_moo_print_operation_set_filename (MooPrintOperation *print,
const char *filename,
const char *basename)
{
g_return_if_fail (MOO_IS_PRINT_OPERATION (print));
g_free (print->filename);
g_free (print->basename);
print->filename = g_strdup (filename);
print->basename = g_strdup (basename);
}
static void
set_hf_format (MooPrintHeaderFooter *hf,
2006-05-07 21:48:37 -07:00
const char **formats)
2006-05-07 16:44:10 -07:00
{
2006-05-07 21:48:37 -07:00
int i;
HFFormat *parsed[3] = {NULL, NULL, NULL};
2006-05-07 16:44:10 -07:00
2006-05-07 21:48:37 -07:00
for (i = 0; i < 3; ++i)
2006-05-07 16:44:10 -07:00
{
2006-05-07 21:48:37 -07:00
if (formats[i])
parsed[i] = hf_format_parse (formats[i]);
2006-05-07 16:44:10 -07:00
2006-05-07 21:48:37 -07:00
if (formats[i] && !parsed[i])
{
for (i = 0; i < 3; ++i)
hf_format_free (parsed[i]);
return;
}
}
2006-05-07 16:44:10 -07:00
2006-05-07 21:48:37 -07:00
for (i = 0; i < 3; ++i)
{
g_free (hf->format[i]);
hf_format_free (hf->parsed_format[i]);
hf->format[i] = g_strdup (formats[i]);
hf->parsed_format[i] = parsed[i];
}
2006-05-07 16:44:10 -07:00
}
void
_moo_print_operation_set_header_format (MooPrintOperation *print,
const char *left,
const char *center,
const char *right)
{
2006-05-07 21:48:37 -07:00
const char *formats[] = {left, center, right};
2006-05-07 16:44:10 -07:00
g_return_if_fail (MOO_IS_PRINT_OPERATION (print));
2006-05-07 21:48:37 -07:00
set_hf_format (&print->header, formats);
2006-05-07 16:44:10 -07:00
}
void
_moo_print_operation_set_footer_format (MooPrintOperation *print,
const char *left,
const char *center,
const char *right)
{
2006-05-07 21:48:37 -07:00
const char *formats[] = {left, center, right};
2006-05-07 16:44:10 -07:00
g_return_if_fail (MOO_IS_PRINT_OPERATION (print));
2006-05-07 21:48:37 -07:00
set_hf_format (&print->footer, formats);
}
/*****************************************************************************/
/* Header/footer gui
*/
#define SET_TEXT(wid,setting) \
G_STMT_START { \
GtkEntry *entry__ = moo_glade_xml_get_widget (xml, wid); \
const char *s__ = moo_prefs_get_string (setting); \
gtk_entry_set_text (entry__, s__ ? s__ : ""); \
} G_STMT_END
#define GET_TEXT(wid,setting) \
G_STMT_START { \
GtkEntry *entry__ = moo_glade_xml_get_widget (xml, wid); \
const char *s__ = gtk_entry_get_text (entry__); \
moo_prefs_set_string (setting, s__ && s__[0] ? s__ : NULL); \
} G_STMT_END
#define SET_BOOL(wid,setting) \
G_STMT_START { \
gtk_toggle_button_set_active (moo_glade_xml_get_widget (xml, wid), \
moo_prefs_get_bool (setting)); \
} G_STMT_END
#define GET_BOOL(wid,setting) \
G_STMT_START { \
gpointer btn__ = moo_glade_xml_get_widget (xml, wid); \
moo_prefs_set_bool (setting, gtk_toggle_button_get_active (btn__)); \
} G_STMT_END
static void
moo_print_init_prefs (void)
{
moo_prefs_new_key_bool (PREFS_PRINT_HEADER, TRUE);
moo_prefs_new_key_bool (PREFS_PRINT_FOOTER, TRUE);
moo_prefs_new_key_bool (PREFS_PRINT_HEADER_SEPARATOR, TRUE);
moo_prefs_new_key_bool (PREFS_PRINT_FOOTER_SEPARATOR, TRUE);
moo_prefs_new_key_string (PREFS_HEADER_LEFT, NULL);
moo_prefs_new_key_string (PREFS_HEADER_CENTER, NULL);
moo_prefs_new_key_string (PREFS_HEADER_RIGHT, NULL);
moo_prefs_new_key_string (PREFS_FOOTER_LEFT, NULL);
moo_prefs_new_key_string (PREFS_FOOTER_CENTER, NULL);
moo_prefs_new_key_string (PREFS_FOOTER_RIGHT, NULL);
moo_prefs_new_key_string (PREFS_FONT, NULL);
moo_prefs_new_key_bool (PREFS_USE_STYLES, FALSE);
moo_prefs_new_key_bool (PREFS_USE_CUSTOM_FONT, FALSE);
moo_prefs_new_key_bool (PREFS_WRAP, TRUE);
moo_prefs_new_key_bool (PREFS_ELLIPSIZE, FALSE);
}
static void
set_options (MooGladeXML *xml)
{
const char *s;
moo_print_init_prefs ();
SET_BOOL ("print_header", PREFS_PRINT_HEADER);
SET_BOOL ("header_separator", PREFS_PRINT_HEADER_SEPARATOR);
SET_TEXT ("header_left", PREFS_HEADER_LEFT);
SET_TEXT ("header_center", PREFS_HEADER_CENTER);
SET_TEXT ("header_right", PREFS_HEADER_RIGHT);
SET_BOOL ("print_footer", PREFS_PRINT_FOOTER);
SET_BOOL ("footer_separator", PREFS_PRINT_FOOTER_SEPARATOR);
SET_TEXT ("footer_left", PREFS_FOOTER_LEFT);
SET_TEXT ("footer_center", PREFS_FOOTER_CENTER);
SET_TEXT ("footer_right", PREFS_FOOTER_RIGHT);
SET_BOOL ("use_styles", PREFS_USE_STYLES);
SET_BOOL ("use_custom_font", PREFS_USE_CUSTOM_FONT);
SET_BOOL ("wrap", PREFS_WRAP);
SET_BOOL ("ellipsize", PREFS_ELLIPSIZE);
if ((s = moo_prefs_get_string (PREFS_FONT)))
gtk_font_button_set_font_name (moo_glade_xml_get_widget (xml, "font"), s);
}
static void
get_options (MooGladeXML *xml)
{
GET_BOOL ("print_header", PREFS_PRINT_HEADER);
GET_BOOL ("header_separator", PREFS_PRINT_HEADER_SEPARATOR);
GET_TEXT ("header_left", PREFS_HEADER_LEFT);
GET_TEXT ("header_center", PREFS_HEADER_CENTER);
GET_TEXT ("header_right", PREFS_HEADER_RIGHT);
GET_BOOL ("print_footer", PREFS_PRINT_FOOTER);
GET_BOOL ("footer_separator", PREFS_PRINT_FOOTER_SEPARATOR);
GET_TEXT ("footer_left", PREFS_FOOTER_LEFT);
GET_TEXT ("footer_center", PREFS_FOOTER_CENTER);
GET_TEXT ("footer_right", PREFS_FOOTER_RIGHT);
GET_BOOL ("use_styles", PREFS_USE_STYLES);
GET_BOOL ("use_custom_font", PREFS_USE_CUSTOM_FONT);
GET_BOOL ("wrap", PREFS_WRAP);
GET_BOOL ("ellipsize", PREFS_ELLIPSIZE);
if (gtk_toggle_button_get_active (moo_glade_xml_get_widget (xml, "use_custom_font")))
moo_prefs_set_string (PREFS_FONT,
gtk_font_button_get_font_name (moo_glade_xml_get_widget (xml, "font")));
}
void
_moo_edit_print_options_dialog (GtkWidget *parent)
{
GtkWidget *dialog;
MooGladeXML *xml;
xml = moo_glade_xml_new_from_buf (MOO_PRINT_GLADE_XML, -1, NULL);
g_return_if_fail (xml != NULL);
dialog = moo_glade_xml_get_widget (xml, "dialog");
g_return_if_fail (dialog != NULL);
moo_position_window (dialog, parent, FALSE, FALSE, 0, 0);
set_options (xml);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
get_options (xml);
g_object_unref (xml);
gtk_widget_destroy (dialog);
2006-05-07 16:44:10 -07:00
}
2006-05-24 00:06:19 -07:00
static GtkWidget *
moo_print_operation_create_custom_widget (G_GNUC_UNUSED GtkPrintOperation *operation)
{
GtkWidget *page;
MooGladeXML *xml;
xml = moo_glade_xml_new_from_buf (MOO_PRINT_GLADE_XML, -1, "page");
g_return_val_if_fail (xml != NULL, NULL);
page = moo_glade_xml_get_widget (xml, "page");
g_return_val_if_fail (page != NULL, NULL);
g_object_set_data_full (G_OBJECT (page), "moo-glade-xml",
xml, g_object_unref);
set_options (xml);
return page;
}
static void
moo_print_operation_custom_widget_apply (G_GNUC_UNUSED GtkPrintOperation *operation,
GtkWidget *widget)
{
MooGladeXML *xml;
xml = g_object_get_data (G_OBJECT (widget), "moo-glade-xml");
g_return_if_fail (xml != NULL);
get_options (xml);
}
2006-05-07 16:44:10 -07:00
/*****************************************************************************/
/* Format string
*/
typedef enum {
HF_FORMAT_TIME,
HF_FORMAT_PAGE,
HF_FORMAT_TOTAL_PAGES,
HF_FORMAT_FILENAME,
HF_FORMAT_BASENAME
} HFFormatType;
typedef struct {
HFFormatType type;
char *string;
} HFFormatChunk;
struct _HFFormat {
GSList *chunks;
};
static HFFormatChunk *
hf_format_chunk_new (HFFormatType type,
const char *string,
int len)
{
HFFormatChunk *chunk = g_slice_new0 (HFFormatChunk);
chunk->type = type;
if (string)
chunk->string = g_strndup (string, len >= 0 ? len : (int) strlen (string));
return chunk;
}
static void
hf_format_chunk_free (HFFormatChunk *chunk)
{
if (chunk)
{
g_free (chunk->string);
g_slice_free (HFFormatChunk, chunk);
}
}
static void
hf_format_free (HFFormat *format)
{
if (format)
{
g_slist_foreach (format->chunks, (GFunc) hf_format_chunk_free, NULL);
g_slist_free (format->chunks);
g_slice_free (HFFormat, format);
}
}
#define ADD_CHUNK(format,type,string,len) \
format->chunks = \
g_slist_prepend (format->chunks, \
hf_format_chunk_new (type, string, len))
static HFFormat *
hf_format_parse (const char *format_string)
{
HFFormat *format;
const char *p, *str;
g_return_val_if_fail (format_string != NULL, NULL);
format = g_slice_new0 (HFFormat);
p = str = format_string;
while (*p && (p = strchr (p, '%')))
{
switch (p[1])
{
2006-05-07 21:48:37 -07:00
case 'E':
2006-05-07 16:44:10 -07:00
if (p != str)
ADD_CHUNK (format, HF_FORMAT_TIME, str, p - str);
switch (p[2])
{
case 0:
2006-05-07 21:48:37 -07:00
g_warning ("%s: trailing '%%E' in %s",
2006-05-07 16:44:10 -07:00
G_STRLOC, format_string);
goto error;
case 'f':
ADD_CHUNK (format, HF_FORMAT_BASENAME, NULL, 0);
str = p = p + 3;
break;
case 'F':
ADD_CHUNK (format, HF_FORMAT_FILENAME, NULL, 0);
str = p = p + 3;
break;
case 'p':
ADD_CHUNK (format, HF_FORMAT_PAGE, NULL, 0);
str = p = p + 3;
break;
case 'P':
ADD_CHUNK (format, HF_FORMAT_TOTAL_PAGES, NULL, 0);
str = p = p + 3;
break;
default:
2006-05-07 21:48:37 -07:00
g_warning ("%s: unknown format '%%E%c' in %s",
2006-05-07 16:44:10 -07:00
G_STRLOC, p[2], format_string);
goto error;
}
break;
default:
p = p + 2;
}
}
if (*str)
ADD_CHUNK (format, HF_FORMAT_TIME, str, -1);
format->chunks = g_slist_reverse (format->chunks);
return format;
error:
hf_format_free (format);
return NULL;
}
static void
eval_strftime (GString *dest,
const char *format,
struct tm *tm)
{
gsize result;
char buf[1024];
char *retval;
g_return_if_fail (format != NULL);
g_return_if_fail (tm != NULL);
if (!format[0])
return;
result = strftime (buf, 1024, format, tm);
if (!result)
{
g_warning ("%s: strftime failed", G_STRLOC);
return;
}
retval = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
if (!retval)
{
g_warning ("%s: could not convert result of strftime to UTF8", G_STRLOC);
return;
}
g_string_append (dest, retval);
g_free (retval);
}
static char *
hf_format_eval (HFFormat *format,
struct tm *tm,
2006-05-13 14:09:09 -07:00
int page,
2006-05-07 16:44:10 -07:00
int total_pages,
const char *filename,
const char *basename)
{
GString *string;
GSList *l;
g_return_val_if_fail (format != NULL, NULL);
string = g_string_new (NULL);
for (l = format->chunks; l != NULL; l = l->next)
{
HFFormatChunk *chunk = l->data;
switch (chunk->type)
{
case HF_FORMAT_TIME:
eval_strftime (string, chunk->string, tm);
break;
case HF_FORMAT_PAGE:
2006-05-13 14:09:09 -07:00
g_string_append_printf (string, "%d", page + 1);
2006-05-07 16:44:10 -07:00
break;
case HF_FORMAT_TOTAL_PAGES:
g_string_append_printf (string, "%d", total_pages);
break;
case HF_FORMAT_FILENAME:
if (!filename)
g_critical ("%s: filename is NULL", G_STRLOC);
else
g_string_append (string, filename);
break;
case HF_FORMAT_BASENAME:
if (!basename)
g_critical ("%s: basename is NULL", G_STRLOC);
else
g_string_append (string, basename);
break;
}
}
return g_string_free (string, FALSE);
}