From 6a210e573d947d61172da0363cbd7832e5459323 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 30 Aug 2012 09:30:59 -0700 Subject: Add support for printing only the selected dives Right now we just implicitly decide "print only selected dives" when there is more than one dive selected (and then print all dives if only one dive is selected). We probably should have an checkbutton in the dive details page for the choice. But I wanted to avoid the pain that is gtk as far as possible for the initial implementation. The code is ready to be changed to just use a checkbutton instead. Signed-off-by: Linus Torvalds --- display.h | 1 + print.c | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/display.h b/display.h index 581530efd..ef52e99f5 100644 --- a/display.h +++ b/display.h @@ -28,6 +28,7 @@ extern void attach_tooltip(int x, int y, int w, int h, const char *text); struct options { enum { PRETTY, TABLE } type; + int print_selected; }; extern char zoomed_plot; diff --git a/print.c b/print.c index 46e3c94f6..0e49bc9c3 100644 --- a/print.c +++ b/print.c @@ -13,6 +13,23 @@ static struct options print_options; +/* Return the 'i'th dive for printing, taking our dive selection into account */ +static struct dive *get_dive_for_printing(int idx) +{ + if (print_options.print_selected) { + int i; + struct dive *dive; + for_each_dive(i, dive) { + if (!dive->selected) + continue; + if (!idx) + return dive; + idx--; + } + return NULL; + } + return get_dive(idx); +} static void set_font(PangoLayout *layout, PangoFontDescription *font, double size, int align) @@ -21,7 +38,6 @@ static void set_font(PangoLayout *layout, PangoFontDescription *font, pango_layout_set_font_description(layout, font); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); pango_layout_set_alignment(layout, align); - } /* @@ -283,7 +299,7 @@ static void print(int divenr, cairo_t *cr, double x, double y, double w, { struct dive *dive; - dive = get_dive(divenr); + dive = get_dive_for_printing(divenr); if (!dive) return; cairo_save(cr); @@ -330,7 +346,7 @@ static void print_table(int divenr, cairo_t *cr, double x, double y, { struct dive *dive; - dive = get_dive(divenr); + dive = get_dive_for_printing(divenr); if (!dive) return; cairo_save(cr); @@ -380,7 +396,7 @@ static void draw_table(GtkPrintOperation *operation, gint page_nr, gpointer user_data) { - int nr; + int i, nr; int n_dive_per_page = 25; cairo_t *cr; double w, h; @@ -394,7 +410,6 @@ static void draw_table(GtkPrintOperation *operation, nr = page_nr*n_dive_per_page; print_table_header(cr, 0, 0+h, w, h, font); - int i; for (i = 0; i < n_dive_per_page; i++) { print_table(nr+i, cr, 0, h*1.5+h*i, w, h, font); } @@ -402,17 +417,34 @@ static void draw_table(GtkPrintOperation *operation, pango_font_description_free(font); } +static int nr_selected_dives(void) +{ + int i, dives; + struct dive *dive; + + dives = 0; + for_each_dive(i, dive) + dives += dive->selected; + return dives; +} + static void begin_print(GtkPrintOperation *operation, gpointer user_data) { - int dives_per_page = 1; + int pages, dives; + int dives_per_page; + + dives = nr_selected_dives(); + print_options.print_selected = dives > 1; + if (dives <= 1) + dives = dive_table.nr; + if (print_options.type == PRETTY) { - dives_per_page = 6; + dives_per_page = 6; } else { dives_per_page = 25; } - int pages; - pages = (dive_table.nr + dives_per_page - 1) / dives_per_page; - gtk_print_operation_set_n_pages(operation, pages); + pages = (dives + dives_per_page - 1) / dives_per_page; + gtk_print_operation_set_n_pages(operation, pages); } -- cgit v1.2.3-70-g09d2