diff --git a/src/main_window.c b/src/main_window.c index 7dd02ba..12dfd83 100644 --- a/src/main_window.c +++ b/src/main_window.c @@ -33,6 +33,8 @@ #include +#include + #include "settings.h" #include "mime_db.h" #include "util.h" @@ -229,6 +231,8 @@ cb_rstto_main_window_edit (GtkWidget *widget, RsttoMainWindow *window); static void cb_rstto_main_window_save_copy (GtkWidget *widget, RsttoMainWindow *window); static void +cb_rstto_main_window_print (GtkWidget *widget, RsttoMainWindow *window); +static void cb_rstto_main_window_delete (GtkWidget *widget, RsttoMainWindow *window); static void cb_rstto_main_window_refresh ( @@ -376,6 +380,12 @@ static GtkActionEntry action_entries[] = "s", /* Keyboard shortcut */ N_ ("Save a copy of the image"), /* Tooltip text */ G_CALLBACK (cb_rstto_main_window_save_copy), }, + { "print", + GTK_STOCK_PRINT, /* Icon-name */ + NULL, /* Label-text */ + "p", /* Keyboard shortcut */ + N_ ("Print the image"), /* Tooltip text */ + G_CALLBACK (cb_rstto_main_window_print), }, { "properties", GTK_STOCK_PROPERTIES, /* Icon-name */ N_ ("_Properties..."), /* Label-text */ @@ -1540,9 +1550,9 @@ rstto_main_window_update_buttons (RsttoMainWindow *window) } gtk_widget_hide (window->priv->t_bar_s_window); gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/save-copy"), FALSE); - /* + gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/print"), FALSE); - */ + gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/properties"), FALSE); gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/close"), FALSE); gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/edit-menu/delete"), FALSE); @@ -1635,9 +1645,9 @@ rstto_main_window_update_buttons (RsttoMainWindow *window) gtk_widget_hide (window->priv->toolbar); } gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/save-copy"), TRUE); - /* + gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/print"), TRUE); - */ + gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/properties"), TRUE); gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/close"), TRUE); gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/edit-menu/delete"), TRUE); @@ -1743,6 +1753,8 @@ rstto_main_window_update_buttons (RsttoMainWindow *window) gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/save-copy"), TRUE); + gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/print"), TRUE); + gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/properties"), TRUE); gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/file-menu/close"), TRUE); gtk_widget_set_sensitive ( gtk_ui_manager_get_widget ( window->priv->ui_manager, "/main-menu/edit-menu/delete"), TRUE); @@ -3542,6 +3554,102 @@ cb_rstto_main_window_dnd_files (GtkWidget *widget, /* PRINTING CALLBACKS */ /**********************/ +static void +cb_gtk_print__draw_page (GtkPrintOperation *op, GtkPrintContext *ctx, gint nr, gpointer user_data) +{ + RsttoMainWindow *window = (RsttoMainWindow *) user_data; /* so you feel at home */ + GdkPixbuf *src_pixbuf = NULL; + cairo_t *draw_ctx = NULL; + GError *err = NULL; + char *img_path = NULL; + gdouble print_h = .0, print_w = .0; + int img_h = 0, img_w = 0; + gdouble hard_margin_top = .0, hard_margin_bottom = .0, + hard_margin_left = .0, hard_margin_right = .0, + height_margin = .0, width_margin = .0; + g_assert (nr == 0); /* One image, one page to print */ + draw_ctx = gtk_print_context_get_cairo_context (ctx); + g_assert (draw_ctx); + gtk_print_context_get_hard_margins (draw_ctx, &hard_margin_top, &hard_margin_bottom, &hard_margin_left, &hard_margin_right); + height_margin = hard_margin_top + hard_margin_bottom; + width_margin = hard_margin_left + hard_margin_right; + img_path = rstto_file_get_path(rstto_image_list_iter_get_file(window->priv->iter)); + g_assert (img_path); + src_pixbuf = gdk_pixbuf_new_from_file (img_path, &err); + g_assert (!err); + img_h = gdk_pixbuf_get_height (src_pixbuf); + img_w = gdk_pixbuf_get_width (src_pixbuf); + g_object_unref (src_pixbuf); + print_w = gtk_print_context_get_width (ctx) + width_margin; + print_h = gtk_print_context_get_height (ctx) + height_margin; + if ((img_h >= img_w) == (print_h >= print_w)) /* Do image and print zone have the same orientation? */ + { + src_pixbuf = gdk_pixbuf_new_from_file_at_scale (img_path, + floor (print_w), + floor (print_h), + TRUE, + &err); + } + else + { + GdkPixbuf *buf_pixbuf = gdk_pixbuf_new_from_file_at_scale (img_path, + floor (print_h), + floor (print_w), + TRUE, + &err); + g_assert (buf_pixbuf && !err); + src_pixbuf = gdk_pixbuf_rotate_simple (buf_pixbuf, 90); + g_assert (src_pixbuf); + g_object_unref (buf_pixbuf); + } + g_assert (!err); + + gdk_cairo_set_source_pixbuf (draw_ctx, src_pixbuf, .0, .0); + cairo_paint (draw_ctx); + +finalise: + if (src_pixbuf) g_object_unref (src_pixbuf); + if (img_path) g_free (img_path); +} + +/** + * cb_rstto_main_window_print: + * @widget: + * @window: + * + * + */ +static void +cb_rstto_main_window_print (GtkWidget *widget, RsttoMainWindow *window) +{ + GtkPrintOperation *op = NULL; + GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR; + GtkPrintSettings *settings = NULL; + GError *err = NULL; + + op = gtk_print_operation_new (); + if (!op) + { + goto finalise; + } + gtk_print_operation_set_n_pages (op, 1); + gtk_print_operation_set_unit (op, GTK_UNIT_MM); + + g_signal_connect (op, "draw-page", G_CALLBACK (cb_gtk_print__draw_page), window); + res = gtk_print_operation_run (op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW (window), &err); + if (GTK_PRINT_OPERATION_RESULT_ERROR == res) + { + /* TODO: Treat error cases (printing did not work) */ + if (err) + { + } + goto finalise; + } + +finalise: + if (op) g_object_unref (op); +} + /*************************/ /* GUI-RELATED CALLBACKS */ /*************************/ diff --git a/src/main_window_ui.xml b/src/main_window_ui.xml index 5e8003b..9e9fe68 100644 --- a/src/main_window_ui.xml +++ b/src/main_window_ui.xml @@ -5,10 +5,10 @@ - +