2 * Routines for saving pixmaps using the Gdk-Pixmap library
3 * Copyright 2007, Stephen Fisher (see AUTHORS file)
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <epan/filesystem.h>
34 #include "ui/last_open_dir.h"
35 #include "ui/simple_dialog.h"
37 #include "ui/gtk/pixmap_save.h"
38 #include "ui/gtk/gui_utils.h"
39 #include "ui/gtk/file_dlg.h"
41 #include "ui/gtk/old-gtk-compat.h"
42 #if GTK_CHECK_VERSION(2,22,0)
43 #if !GTK_CHECK_VERSION(3,0,0)
44 #include "ui/gtk/gui_utils.h"
49 pixmap_save_cb(GtkWidget *w, gpointer pixmap_ptr _U_)
52 #if GTK_CHECK_VERSION(2,22,0)
53 surface_info_t *surface_info = (surface_info_t *)g_object_get_data(G_OBJECT(w), "surface-info");
55 GdkPixmap *pixmap = g_object_get_data(G_OBJECT(w), "pixmap");
58 GdkPixbufFormat *pixbuf_format;
59 GtkWidget *main_vb, *save_as_type_hb, *type_lb, *type_cm;
60 GSList *file_formats,*ffp;
64 guint format_index = 0;
65 guint default_index = 0;
67 gchar *filename, *file_type;
70 GtkWidget *msg_dialog;
72 #if GTK_CHECK_VERSION(2,22,0)
73 pixbuf = gdk_pixbuf_get_from_surface (surface_info->surface,
74 0, 0, surface_info->width, surface_info->height);
76 pixbuf = gdk_pixbuf_get_from_drawable(NULL, pixmap, NULL,
80 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
81 "%sCould not get image from graph%s",
82 simple_dialog_primary_start(),
83 simple_dialog_primary_end());
87 save_as_w = file_selection_new("Wireshark: Save Graph As ...",
89 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(save_as_w), TRUE);
91 /* Container for each row of widgets */
92 main_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 0, FALSE);
93 file_selection_set_extra_widget(save_as_w, main_vb);
94 gtk_widget_show(main_vb);
96 save_as_type_hb = ws_gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0, FALSE);
97 gtk_box_pack_start(GTK_BOX(main_vb), save_as_type_hb, FALSE, FALSE, 0);
98 gtk_widget_show(save_as_type_hb);
100 type_lb = gtk_label_new("File type: ");
101 gtk_box_pack_start(GTK_BOX(save_as_type_hb), type_lb, FALSE, FALSE, 0);
102 gtk_widget_show(type_lb);
104 type_cm = gtk_combo_box_text_new();
105 gtk_box_pack_start(GTK_BOX(save_as_type_hb), type_cm, FALSE, FALSE, 0);
107 /* List all of the file formats the gdk-pixbuf library supports */
108 file_formats = gdk_pixbuf_get_formats();
111 pixbuf_format = (GdkPixbufFormat *)ffp->data;
112 if (gdk_pixbuf_format_is_writable(pixbuf_format)) {
113 format_name = gdk_pixbuf_format_get_name(pixbuf_format);
114 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(type_cm),
116 if (!(g_ascii_strcasecmp(format_name, "png")))
117 default_index = format_index;
120 ffp = g_slist_next(ffp);
122 g_slist_free(file_formats);
124 gtk_combo_box_set_active(GTK_COMBO_BOX(type_cm), default_index);
125 gtk_widget_show(type_cm);
127 gtk_widget_show(save_as_w);
128 window_present(save_as_w);
129 parent = gtk_widget_get_parent_window(w);
130 gdk_window_set_transient_for(gtk_widget_get_window(save_as_w), parent);
133 * Loop until the user either selects a file or gives up.
136 if (gtk_dialog_run(GTK_DIALOG(save_as_w)) != GTK_RESPONSE_ACCEPT) {
137 /* They clicked "Cancel" or closed the dialog or.... */
138 window_destroy(save_as_w);
142 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(save_as_w));
144 /* Perhaps the user specified a directory instead of a file.
145 Check whether they did. */
146 if (test_for_directory(filename) == EISDIR) {
147 /* It's a directory - set the file selection box to display that
148 directory, and leave the selection box displayed. */
149 set_last_open_dir(filename);
151 file_selection_set_current_folder(save_as_w,
152 get_last_open_dir());
153 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(save_as_w), "");
157 file_type = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(type_cm));
158 ret = gdk_pixbuf_save(pixbuf, filename, file_type, &error, NULL);
163 msg_dialog = gtk_message_dialog_new(GTK_WINDOW(save_as_w),
164 GTK_DIALOG_DESTROY_WITH_PARENT,
167 "%s", error->message);
168 gtk_dialog_run(GTK_DIALOG(msg_dialog));
169 gtk_widget_destroy(msg_dialog);
173 window_destroy(save_as_w);