X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-output-view.c;h=6d704ae1bccf1bba19eaa468cbddaf6eacbca03f;hb=29917c4f5908454803e663d2ad78bca4bc35e805;hp=7e8b5b3560c11baa069938cb39532d583a340eb2;hpb=01fc7026c655cbea3e0de1e05ace3865d6247513;p=pspp diff --git a/src/ui/gui/psppire-output-view.c b/src/ui/gui/psppire-output-view.c index 7e8b5b3560..6d704ae1bc 100644 --- a/src/ui/gui/psppire-output-view.c +++ b/src/ui/gui/psppire-output-view.c @@ -18,28 +18,22 @@ #include "ui/gui/psppire-output-view.h" +#include #include #include -#if HAVE_RSVG -#include "librsvg/rsvg.h" -#endif #include "libpspp/assertion.h" #include "libpspp/string-map.h" -#include "output/cairo.h" +#include "output/cairo-fsm.h" +#include "output/cairo-pager.h" #include "output/driver-provider.h" #include "output/driver.h" -#include "output/chart-item.h" -#include "output/group-item.h" -#include "output/message-item.h" #include "output/output-item.h" -#include "output/output-item-provider.h" -#include "output/table-item.h" -#include "output/text-item.h" +#include "output/pivot-table.h" #include "gl/c-xvasprintf.h" #include "gl/minmax.h" -#include "gl/tmpdir.h" +#include "gl/clean-temp.h" #include "gl/xalloc.h" #include @@ -53,15 +47,14 @@ struct output_view_item struct psppire_output_view { - struct xr_driver *xr; - int font_height; + struct xr_fsm_style *style; + int object_spacing; GtkLayout *output; int render_width; int max_width; glong y; - struct string_map render_opts; GtkTreeView *overview; GtkTreePath *cur_group; @@ -75,7 +68,10 @@ struct psppire_output_view /* Variables pertaining to printing */ GtkPrintSettings *print_settings; - struct xr_driver *print_xrd; + + struct xr_fsm_style *fsm_style; + struct xr_page_style *page_style; + struct xr_pager *pager; int print_item; int print_n_pages; gboolean paginated; @@ -83,9 +79,9 @@ struct psppire_output_view enum { - COL_NAME, /* Table name. */ + COL_LABEL, /* Output item label. */ COL_ADDR, /* Pointer to the table */ - COL_Y, /* Y position of top of name. */ + COL_Y, /* Y position of top of object. */ N_COLS }; @@ -114,7 +110,7 @@ draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data) if (!gdk_cairo_get_clip_rectangle (cr, &clip)) return TRUE; - struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering"); + struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm"); /* Draw the background based on the state of the widget which can be selected or not selected */ @@ -127,80 +123,42 @@ draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data) GdkRGBA color; gtk_style_context_get_color (context, state, &color); cairo_set_source_rgba (cr, color.red, color.green, color.blue, color.alpha); - xr_rendering_draw (r, cr, clip.x, clip.y, - clip.x + clip.width, clip.y + clip.height); + xr_fsm_draw_region (fsm, cr, clip.x, clip.y, clip.width, clip.height); return TRUE; } static void -free_rendering (gpointer rendering_) +free_fsm (gpointer fsm_) { - struct xr_rendering *rendering = rendering_; - xr_rendering_destroy (rendering); + struct xr_fsm *fsm = fsm_; + xr_fsm_destroy (fsm); } -static void -get_xr_options (struct psppire_output_view *view, struct string_map *options) +static struct xr_fsm_style * +get_xr_fsm_style (struct psppire_output_view *view) { - string_map_clear (options); - GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (view->output)); GtkStateFlags state = gtk_widget_get_state_flags (GTK_WIDGET (view->output)); - /* Use GTK+ default font as proportional font. */ - PangoFontDescription *font_desc; - gtk_style_context_get (context, state, "font", &font_desc, NULL); - char *font_name = pango_font_description_to_string (font_desc); - string_map_insert (options, "prop-font", font_name); - g_free (font_name); - - /* Derived emphasized font from proportional font. */ - pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC); - font_name = pango_font_description_to_string (font_desc); - string_map_insert (options, "emph-font", font_name); - g_free (font_name); - pango_font_description_free (font_desc); - - /* Pretend that the "page" has a reasonable width and a very big length, - so that most tables can be conveniently viewed on-screen with vertical - scrolling only. (The length should not be increased very much because - it is already close enough to INT_MAX when expressed as thousands of a - point.) */ - string_map_insert_nocopy (options, xstrdup ("paper-size"), - xasprintf ("%dx1000000pt", view->render_width)); - string_map_insert (options, "left-margin", "0"); - string_map_insert (options, "right-margin", "0"); - string_map_insert (options, "top-margin", "0"); - string_map_insert (options, "bottom-margin", "0"); -} - -static void -create_xr (struct psppire_output_view *view) -{ - get_xr_options (view, &view->render_opts); - - struct string_map options; - string_map_clone (&options, &view->render_opts); - - GdkWindow *win = gtk_layout_get_bin_window (view->output); - cairo_region_t *region = gdk_window_get_visible_region (win); - GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region); - cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx); + int xr_width = view->render_width * 1000; - view->xr = xr_driver_create (cr, &options); - string_map_destroy (&options); + PangoFontDescription *pf; + gtk_style_context_get (context, state, "font", &pf, NULL); - struct text_item *text_item = text_item_create (TEXT_ITEM_LOG, "X"); - struct xr_rendering *r - = xr_rendering_create (view->xr, text_item_super (text_item), cr); - xr_rendering_measure (r, NULL, &view->font_height); - xr_rendering_destroy (r); - text_item_unref (text_item); + struct xr_fsm_style *style = xmalloc (sizeof *style); + *style = (struct xr_fsm_style) { + .ref_cnt = 1, + .size = { [TABLE_HORZ] = xr_width, [TABLE_VERT] = INT_MAX }, + .min_break = { [TABLE_HORZ] = xr_width / 2, [TABLE_VERT] = 0 }, + .font = pf, + .use_system_colors = true, + .object_spacing = XR_POINT * 12, + .font_resolution = 96.0, + }; - gdk_window_end_draw_frame (win, ctx); - cairo_region_destroy (region); + return style; } /* Return the horizontal position to place a widget whose @@ -227,7 +185,7 @@ find_selected_item (struct psppire_output_view *view) for (item = view->items; item < &view->items[view->n_items]; item++) { GtkWidget *widget = GTK_WIDGET (item->drawing_area); - if GTK_IS_WIDGET (widget) + if (GTK_IS_WIDGET (widget)) { GtkStateFlags state = gtk_widget_get_state_flags (widget); if (state & GTK_STATE_FLAG_SELECTED) @@ -307,16 +265,11 @@ drag_data_get_cb (GtkWidget *widget, GdkDragContext *context, static void create_drawing_area (struct psppire_output_view *view, - GtkWidget *drawing_area, struct xr_rendering *r, + GtkWidget *drawing_area, struct xr_fsm *r, int tw, int th, const struct output_item *item) { - struct string_map options = STRING_MAP_INITIALIZER (options); - string_map_insert (&options, "transparent", "true"); - string_map_insert (&options, "systemcolors", "true"); - xr_rendering_apply_options (r, &options); - g_object_set_data_full (G_OBJECT (drawing_area), - "rendering", r, free_rendering); + "fsm", r, free_fsm); g_signal_connect (drawing_area, "button-press-event", G_CALLBACK (button_press_event_cb), view); gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK); @@ -353,8 +306,8 @@ rerender (struct psppire_output_view *view) if (!view->n_items || ! gdkw) return; - if (view->xr == NULL) - create_xr (view); + if (!view->style) + view->style = get_xr_fsm_style (view); GdkWindow *win = gtk_layout_get_bin_window (view->output); cairo_region_t *region = gdk_window_get_visible_region (win); @@ -365,24 +318,24 @@ rerender (struct psppire_output_view *view) view->max_width = 0; for (item = view->items; item < &view->items[view->n_items]; item++) { - struct xr_rendering *r; + struct xr_fsm *r; GtkAllocation alloc; int tw, th; if (view->y > 0) - view->y += view->font_height / 2; + view->y += view->object_spacing; - if (is_group_open_item (item->item)) + if (item->item->type == OUTPUT_ITEM_GROUP_OPEN) continue; - r = xr_rendering_create (view->xr, item->item, cr); + r = xr_fsm_create_for_scrolling (item->item, view->style, cr); if (r == NULL) { g_warn_if_reached (); continue; } - xr_rendering_measure (r, &tw, &th); + xr_fsm_measure (r, cr, &tw, &th); gint xpos = get_xpos (view, tw); @@ -394,11 +347,15 @@ rerender (struct psppire_output_view *view) else { g_object_set_data_full (G_OBJECT (item->drawing_area), - "rendering", r, free_rendering); + "fsm", r, free_fsm); gtk_widget_set_size_request (item->drawing_area, tw, th); gtk_layout_move (view->output, item->drawing_area, xpos, view->y); } + if (item->item->type == OUTPUT_ITEM_TABLE) + gtk_widget_set_tooltip_text (item->drawing_area, + item->item->table->notes); + { gint minw; gint minh; @@ -423,8 +380,8 @@ rerender (struct psppire_output_view *view) } gtk_layout_set_size (view->output, - view->max_width + view->font_height, - view->y + view->font_height); + view->max_width + view->object_spacing, + view->y + view->object_spacing); gdk_window_end_draw_frame (win, ctx); cairo_region_destroy (region); @@ -437,10 +394,9 @@ psppire_output_view_put (struct psppire_output_view *view, { struct output_view_item *view_item; GtkWidget *drawing_area; - struct string name; int tw, th; - if (is_group_close_item (item)) + if (item->type == OUTPUT_ITEM_GROUP_CLOSE) { if (view->cur_group) { @@ -452,11 +408,12 @@ psppire_output_view_put (struct psppire_output_view *view, } return; } - else if (is_text_item (item)) + else if (item->type == OUTPUT_ITEM_TEXT) { - const struct text_item *text_item = to_text_item (item); - const char *text = text_item_get_text (text_item); - if (text[0] == '\0') + char *text = text_item_get_plain_text (item); + bool text_is_empty = text[0] == '\0'; + free (text); + if (text_is_empty) return; } @@ -468,23 +425,23 @@ psppire_output_view_put (struct psppire_output_view *view, view_item->drawing_area = NULL; GdkWindow *win = gtk_widget_get_window (GTK_WIDGET (view->output)); - if (is_group_open_item (item)) + if (item->type == OUTPUT_ITEM_GROUP_OPEN) tw = th = 0; else if (win) { view_item->drawing_area = drawing_area = gtk_drawing_area_new (); - if (view->xr == NULL) - create_xr (view); + if (!view->style) + view->style = get_xr_fsm_style (view); cairo_region_t *region = gdk_window_get_visible_region (win); GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region); cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx); if (view->y > 0) - view->y += view->font_height / 2; + view->y += view->object_spacing; - struct xr_rendering *r = xr_rendering_create (view->xr, item, cr); + struct xr_fsm *r = xr_fsm_create_for_scrolling (item, view->style, cr); if (r == NULL) { gdk_window_end_draw_frame (win, ctx); @@ -492,7 +449,7 @@ psppire_output_view_put (struct psppire_output_view *view, return; } - xr_rendering_measure (r, &tw, &th); + xr_fsm_measure (r, cr, &tw, &th); create_drawing_area (view, drawing_area, r, tw, th, item); gdk_window_end_draw_frame (win, ctx); cairo_region_destroy (region); @@ -505,8 +462,6 @@ psppire_output_view_put (struct psppire_output_view *view, GtkTreeStore *store = GTK_TREE_STORE ( gtk_tree_view_get_model (view->overview)); - ds_init_empty (&name); - /* Create a new node in the tree and puts a reference to it in 'iter'. */ GtkTreeIter iter; GtkTreeIter parent; @@ -518,52 +473,18 @@ psppire_output_view_put (struct psppire_output_view *view, else gtk_tree_store_append (store, &iter, NULL); - if (is_group_open_item (item)) + if (item->type == OUTPUT_ITEM_GROUP_OPEN) { gtk_tree_path_free (view->cur_group); view->cur_group = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter); } - ds_clear (&name); - if (is_text_item (item)) - { - const struct text_item *text_item = to_text_item (item); - ds_put_cstr (&name, text_item_type_to_string ( - text_item_get_type (text_item))); - } - else if (is_message_item (item)) - { - const struct message_item *msg_item = to_message_item (item); - const struct msg *msg = message_item_get_msg (msg_item); - ds_put_format (&name, "%s: %s", _("Message"), - msg_severity_to_string (msg->severity)); - } - else if (is_table_item (item)) - { - const struct table_item_text *title - = table_item_get_title (to_table_item (item)); - if (title != NULL) - ds_put_format (&name, "Table: %s", title->content); - else - ds_put_cstr (&name, "Table"); - } - else if (is_chart_item (item)) - { - const char *s = chart_item_get_title (to_chart_item (item)); - if (s != NULL) - ds_put_format (&name, "Chart: %s", s); - else - ds_put_cstr (&name, "Chart"); - } - else if (is_group_open_item (item)) - ds_put_cstr (&name, to_group_open_item (item)->command_name); gtk_tree_store_set (store, &iter, - COL_NAME, ds_cstr (&name), + COL_LABEL, output_item_get_label (item), COL_ADDR, item, - COL_Y, (view->y), + COL_Y, view->y, -1); - ds_destroy (&name); GtkTreePath *path = gtk_tree_model_get_path ( GTK_TREE_MODEL (store), &iter); @@ -615,18 +536,16 @@ on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view) return; /* GTK+ fires this signal for trivial changes like the mouse moving in or out - of the window. Check whether the actual rendering options changed and + of the window. Check whether the actual fsm options changed and re-render only if they did. */ - struct string_map options = STRING_MAP_INITIALIZER (options); - get_xr_options (view, &options); - if (!string_map_equals (&options, &view->render_opts)) + struct xr_fsm_style *style = get_xr_fsm_style (view); + if (!view->style || !xr_fsm_style_equals (style, view->style)) { - xr_driver_destroy (view->xr); - view->xr = NULL; - + xr_fsm_style_unref (view->style); + view->style = xr_fsm_style_ref (style); rerender (view); } - string_map_destroy (&options); + xr_fsm_style_unref (style); } enum { @@ -639,38 +558,16 @@ enum { SELECT_FMT_ODT }; -/* GNU Hurd doesn't have PATH_MAX. Use a fallback. - Temporary directory names are usually not that long. */ -#ifndef PATH_MAX -# define PATH_MAX 1024 -#endif - -/* Returns a pixbuf from a svg file */ -/* You must unref the pixbuf after usage */ -static GdkPixbuf * -derive_pixbuf_from_svg (const char *filename) +static void +clear_rectangle (cairo_surface_t *surface, + double x0, double y0, double x1, double y1) { - GError *err = NULL; - GdkPixbuf *pixbuf = NULL; -#if HAVE_RSVG - RsvgHandle *handle = rsvg_handle_new_from_file (filename, &err); - if (err == NULL) - { - rsvg_handle_set_dpi (handle, 300.0); - pixbuf = rsvg_handle_get_pixbuf (handle); - g_object_unref (handle); - } -#else - pixbuf = gdk_pixbuf_new_from_file (filename, &err); -#endif - if (err != NULL) - { - msg (ME, _("Could not open file %s during copy operation: %s"), - filename, err->message); - g_error_free (err); - return NULL; - } - return pixbuf; + cairo_t *cr = cairo_create (surface); + cairo_set_source_rgb (cr, 1, 1, 1); + cairo_new_path (cr); + cairo_rectangle (cr, x0, y0, x1 - x0, y1 - y0); + cairo_fill (cr); + cairo_destroy (cr); } static void @@ -684,19 +581,20 @@ clipboard_get_cb (GtkClipboard *clipboard, gsize length; gchar *text = NULL; struct output_driver *driver = NULL; - char dirname[PATH_MAX], *filename; + char *filename; struct string_map options; + struct temp_dir *td = NULL; if (view->selected_item == NULL) return; - if (path_search (dirname, sizeof dirname, NULL, NULL, true) - || mkdtemp (dirname) == NULL) + td = create_temp_dir ("pspp", NULL, false); + if (td == NULL) { msg_error (errno, _("failed to create temporary directory during clipboard operation")); return; } - filename = xasprintf ("%s/clip.tmp", dirname); + filename = xasprintf ("%s/clip.tmp", td->dir_name); string_map_init (&options); string_map_insert (&options, "output-file", filename); @@ -709,6 +607,7 @@ clipboard_get_cb (GtkClipboard *clipboard, case SELECT_FMT_TEXT: string_map_insert (&options, "format", "txt"); + string_map_insert (&options, "width", "1000"); break; case SELECT_FMT_HTML: @@ -736,8 +635,38 @@ clipboard_get_cb (GtkClipboard *clipboard, (info == SELECT_FMT_SVG) ) { GtkWidget *widget = view->selected_item->drawing_area; - struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering"); - xr_draw_svg_file (r, filename); + struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm"); + + GdkWindow *win = gtk_layout_get_bin_window (view->output); + cairo_region_t *region = gdk_window_get_visible_region (win); + GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region); + cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx); + + int w, h; + xr_fsm_measure (fsm, cr, &w, &h); + + gdk_window_end_draw_frame (win, ctx); + cairo_region_destroy (region); + + cairo_surface_t *surface + = (info == SELECT_FMT_SVG + ? cairo_svg_surface_create (filename, w, h) + : cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h)); + clear_rectangle (surface, 0, 0, w, h); + cairo_t *cr2 = cairo_create (surface); + xr_fsm_draw_all (fsm, cr2); + cairo_destroy (cr2); + if (info == SELECT_FMT_IMG) + { + GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface (surface, + 0, 0, w, h); + if (pixbuf) + { + gtk_selection_data_set_pixbuf (selection_data, pixbuf); + g_object_unref (pixbuf); + } + } + cairo_surface_destroy (surface); } else { @@ -756,19 +685,11 @@ clipboard_get_cb (GtkClipboard *clipboard, driver = NULL; } - if (info == SELECT_FMT_IMG) - { - GdkPixbuf *pixbuf = derive_pixbuf_from_svg (filename); - if (pixbuf) - { - gtk_selection_data_set_pixbuf (selection_data, pixbuf); - g_object_unref (pixbuf); - } - } - else if (g_file_get_contents (filename, &text, &length, NULL)) - gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data), - 8, - (const guchar *) text, length); + if (info != SELECT_FMT_IMG + && g_file_get_contents (filename, &text, &length, NULL)) + gtk_selection_data_set (selection_data, + gtk_selection_data_get_target (selection_data), + 8, (const guchar *) text, length); finish: @@ -779,7 +700,7 @@ clipboard_get_cb (GtkClipboard *clipboard, unlink (filename); free (filename); - rmdir (dirname); + cleanup_temp_dir (td); } static void @@ -801,7 +722,7 @@ CT ( ctn8, "image/svg+xml", 0, SELECT_FMT_SVG ) #define CT(ID, TARGET, FLAGS, INFO) static gchar ID[] = TARGET; CBTARGETS #undef CT -gchar ctnlast[] = "application/vnd.oasis.opendocument.text"; +static gchar ctnlast[] = "application/vnd.oasis.opendocument.text"; static const GtkTargetEntry targets[] = { #define CT(ID, TARGET, FLAGS, INFO) { ID, FLAGS, INFO }, @@ -815,8 +736,7 @@ build_target_list (const struct output_item *item) { GtkTargetList *tl = gtk_target_list_new (targets, G_N_ELEMENTS (targets)); g_return_val_if_fail (tl, NULL); - if (is_table_item (item) || - is_chart_item (item)) + if (item->type == OUTPUT_ITEM_TABLE || item->type == OUTPUT_ITEM_CHART) gtk_target_list_add_image_targets (tl, SELECT_FMT_IMG, TRUE); return tl; } @@ -883,25 +803,12 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview) GtkTreeModel *model; view = xmalloc (sizeof *view); - view->xr = NULL; - view->font_height = 0; - view->output = output; - view->render_width = 0; - view->max_width = 0; - view->y = 0; - string_map_init (&view->render_opts); - view->overview = overview; - view->cur_group = NULL; - view->toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output)); - view->buttontime = 0; - view->items = NULL; - view->n_items = view->allocated_items = 0; - view->selected_item = NULL; - view->print_settings = NULL; - view->print_xrd = NULL; - view->print_item = 0; - view->print_n_pages = 0; - view->paginated = FALSE; + *view = (struct psppire_output_view) { + .object_spacing = 10, + .output = output, + .overview = overview, + .toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output)), + }; g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL); @@ -922,7 +829,7 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview) model = GTK_TREE_MODEL (gtk_tree_store_new ( N_COLS, - G_TYPE_STRING, /* COL_NAME */ + G_TYPE_STRING, /* COL_LABEL */ G_TYPE_POINTER, /* COL_ADDR */ G_TYPE_LONG)); /* COL_Y */ gtk_tree_view_set_model (overview, model); @@ -932,7 +839,7 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview) gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column); renderer = gtk_cell_renderer_text_new (); gtk_tree_view_column_pack_start (column, renderer, TRUE); - gtk_tree_view_column_add_attribute (column, renderer, "text", COL_NAME); + gtk_tree_view_column_add_attribute (column, renderer, "text", COL_LABEL); g_signal_connect (GTK_TREE_VIEW (overview), "row-activated", G_CALLBACK (on_row_activate), view); @@ -952,7 +859,7 @@ psppire_output_view_destroy (struct psppire_output_view *view) g_signal_handlers_disconnect_by_func (view->output, G_CALLBACK (on_style_updated), view); - string_map_destroy (&view->render_opts); + xr_fsm_style_unref (view->style); for (i = 0; i < view->n_items; i++) output_item_unref (view->items[i].item); @@ -963,8 +870,6 @@ psppire_output_view_destroy (struct psppire_output_view *view) if (view->print_settings != NULL) g_object_unref (view->print_settings); - xr_driver_destroy (view->xr); - if (view->cur_group) gtk_tree_path_free (view->cur_group); @@ -1015,55 +920,58 @@ static cairo_t * get_cairo_context_from_print_context (GtkPrintContext *context) { cairo_t *cr = gtk_print_context_get_cairo_context (context); - - /* - For all platforms except windows, gtk_print_context_get_dpi_[xy] returns 72. - Windows returns 600. - */ - double xres = gtk_print_context_get_dpi_x (context); - double yres = gtk_print_context_get_dpi_y (context); - - /* This means that the cairo context now has its dimensions in Points */ - cairo_scale (cr, xres / 72.0, yres / 72.0); - - return cr; + return cairo_reference (cr); } - static void create_xr_print_driver (GtkPrintContext *context, struct psppire_output_view *view) { - struct string_map options; - GtkPageSetup *page_setup; - double width, height; - double left_margin; - double right_margin; - double top_margin; - double bottom_margin; - - page_setup = gtk_print_context_get_page_setup (context); - width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM); - height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM); - left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM); - right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM); - top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM); - bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM); + GtkPageSetup *ps = gtk_print_context_get_page_setup (context); - string_map_init (&options); - string_map_insert_nocopy (&options, xstrdup ("paper-size"), - c_xasprintf("%.2fx%.2fmm", width, height)); - string_map_insert_nocopy (&options, xstrdup ("left-margin"), - c_xasprintf ("%.2fmm", left_margin)); - string_map_insert_nocopy (&options, xstrdup ("right-margin"), - c_xasprintf ("%.2fmm", right_margin)); - string_map_insert_nocopy (&options, xstrdup ("top-margin"), - c_xasprintf ("%.2fmm", top_margin)); - string_map_insert_nocopy (&options, xstrdup ("bottom-margin"), - c_xasprintf ("%.2fmm", bottom_margin)); - - view->print_xrd = xr_driver_create (get_cairo_context_from_print_context (context), &options); - - string_map_destroy (&options); + enum { H = TABLE_HORZ, V = TABLE_VERT }; + int paper[TABLE_N_AXES] = { + [H] = gtk_page_setup_get_paper_width (ps, GTK_UNIT_POINTS) * XR_POINT, + [V] = gtk_page_setup_get_paper_height (ps, GTK_UNIT_POINTS) * XR_POINT, + }; + + /* These are all 1/2 inch. The "margins" that GTK+ gives us are useless: + they are the printer's imagable area. */ + int margins[TABLE_N_AXES][2] = { + [H][0] = XR_POINT * 36, + [H][1] = XR_POINT * 36, + [V][0] = XR_POINT * 36, + [V][1] = XR_POINT * 36, + }; + + double size[TABLE_N_AXES]; + for (int a = 0; a < TABLE_N_AXES; a++) + size[a] = paper[a] - margins[a][0] - margins[a][1]; + + view->page_style = xmalloc (sizeof *view->page_style); + *view->page_style = (struct xr_page_style) { + .ref_cnt = 1, + + .margins = { + [H] = { margins[H][0], margins[H][1] }, + [V] = { margins[V][0], margins[V][1] }, + }, + .initial_page_number = 1, + }; + + view->fsm_style = xmalloc (sizeof *view->fsm_style); + *view->fsm_style = (struct xr_fsm_style) { + .ref_cnt = 1, + + .size = { [H] = size[H], [V] = size[V] }, + .min_break = { [H] = size[H] / 2, [V] = size[V] / 2 }, + .font = pango_font_description_from_string ("Sans Serif 10"), + .fg = CELL_COLOR_BLACK, + .use_system_colors = false, + .object_spacing = 12 * XR_POINT, + .font_resolution = 72.0 + }; + + view->pager = xr_pager_create (view->page_style, view->fsm_style); } static gboolean @@ -1079,22 +987,22 @@ paginate (GtkPrintOperation *operation, } else if (view->print_item < view->n_items) { - xr_driver_output_item (view->print_xrd, - view->items[view->print_item++].item); - while (xr_driver_need_new_page (view->print_xrd)) + xr_pager_add_item (view->pager, view->items[view->print_item++].item); + while (xr_pager_needs_new_page (view->pager)) { - xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context)); + xr_pager_add_page (view->pager, + get_cairo_context_from_print_context (context)); view->print_n_pages ++; } return FALSE; } else { - gtk_print_operation_set_n_pages (operation, view->print_n_pages); + gtk_print_operation_set_n_pages (operation, MAX (1, view->print_n_pages)); /* Re-create the driver to do the real printing. */ - xr_driver_destroy (view->print_xrd); - create_xr_print_driver (context, view); + xr_pager_destroy (view->pager); + view->pager = xr_pager_create (view->page_style, view->fsm_style); view->print_item = 0; view->paginated = TRUE; @@ -1110,7 +1018,7 @@ begin_print (GtkPrintOperation *operation, create_xr_print_driver (context, view); view->print_item = 0; - view->print_n_pages = 1; + view->print_n_pages = 0; view->paginated = FALSE; } @@ -1119,7 +1027,8 @@ end_print (GtkPrintOperation *operation, GtkPrintContext *context, struct psppire_output_view *view) { - xr_driver_destroy (view->print_xrd); + xr_pager_destroy (view->pager); + view->pager = NULL; } @@ -1129,10 +1038,11 @@ draw_page (GtkPrintOperation *operation, gint page_number, struct psppire_output_view *view) { - xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context)); - while (!xr_driver_need_new_page (view->print_xrd) + xr_pager_add_page (view->pager, + get_cairo_context_from_print_context (context)); + while (!xr_pager_needs_new_page (view->pager) && view->print_item < view->n_items) - xr_driver_output_item (view->print_xrd, view->items [view->print_item++].item); + xr_pager_add_item (view->pager, view->items [view->print_item++].item); } @@ -1147,6 +1057,9 @@ psppire_output_view_print (struct psppire_output_view *view, if (view->print_settings != NULL) gtk_print_operation_set_print_settings (print, view->print_settings); + gtk_print_operation_set_use_full_page (print, TRUE); + gtk_print_operation_set_unit (print, GTK_UNIT_POINTS); + g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view); g_signal_connect (print, "end_print", G_CALLBACK (end_print), view); g_signal_connect (print, "paginate", G_CALLBACK (paginate), view); @@ -1183,7 +1096,7 @@ psppire_output_view_submit (struct output_driver *this, { struct psppire_output_view_driver *povd = psppire_output_view_driver_cast (this); - if (is_table_item (item)) + if (item->type == OUTPUT_ITEM_TABLE) psppire_output_view_put (povd->view, item); }