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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #if GTK_CHECK_VERSION(2,6,0)
36 #include <epan/filesystem.h>
38 #include "../simple_dialog.h"
40 #include "gtk/pixmap_save.h"
41 #include "gtk/gui_utils.h"
42 #include "gtk/file_dlg.h"
45 static GtkWidget *save_as_w;
48 pixbuf_save_destroy_cb(GtkWidget *window _U_, gpointer data _U_)
50 /* We no longer have a save as dialog */
55 pixbuf_save_button_cb(GtkWidget *save_as_w_lcl, GdkPixbuf *pixbuf)
57 gchar *filename, *file_type;
58 GtkWidget *type_cm, *simple_w;
62 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(save_as_w_lcl));
63 type_cm = g_object_get_data(G_OBJECT(save_as_w_lcl), "type_cm");
64 file_type = gtk_combo_box_get_active_text(GTK_COMBO_BOX(type_cm));
66 /* Perhaps the user specified a directory instead of a file.
67 Check whether they did. */
68 if(test_for_directory(filename) == EISDIR) {
69 /* It's a directory - set the file selection box to display that
70 directory, and leave the selection box displayed. */
71 set_last_open_dir(filename);
74 file_selection_set_current_folder(save_as_w_lcl,
76 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(save_as_w_lcl), "");
80 ret = gdk_pixbuf_save(pixbuf, filename, file_type, &error, NULL);
85 simple_w = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
87 simple_dialog_primary_start(),
89 simple_dialog_primary_end());
90 gtk_window_set_transient_for(GTK_WINDOW(simple_w),
91 GTK_WINDOW(save_as_w_lcl));
97 pixmap_save_cb(GtkWidget *w, gpointer pixmap_ptr _U_)
99 GdkPixmap *pixmap = g_object_get_data(G_OBJECT(w), "pixmap");
101 GdkPixbufFormat *pixbuf_format;
102 GtkWidget *main_vb, *save_as_type_hb, *type_lb, *type_cm;
103 GSList *file_formats,*ffp;
107 guint format_index = 0;
108 guint default_index = 0;
110 pixbuf = gdk_pixbuf_get_from_drawable(NULL, GDK_DRAWABLE(pixmap), NULL,
114 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
115 "%sCould not get image from graph%s",
116 simple_dialog_primary_start(),
117 simple_dialog_primary_end());
121 #if 0 /* XXX: GtkFileChooserDialog/gtk_dialog_run currently being used is effectively modal so this is not req'd */
122 if(save_as_w != NULL) {
123 /* If this save as window is already open, re-open it */
124 reactivate_window(save_as_w);
129 save_as_w = file_selection_new("Wireshark: Save Graph As ...",
130 FILE_SELECTION_SAVE);
131 #if GTK_CHECK_VERSION(2,8,0)
132 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(save_as_w), TRUE);
135 /* Container for each row of widgets */
136 main_vb = gtk_vbox_new(FALSE, 0);
137 file_selection_set_extra_widget(save_as_w, main_vb);
138 gtk_widget_show(main_vb);
140 save_as_type_hb = gtk_hbox_new(FALSE, 0);
141 gtk_box_pack_start(GTK_BOX(main_vb), save_as_type_hb, FALSE, FALSE, 0);
142 gtk_widget_show(save_as_type_hb);
144 type_lb = gtk_label_new("File type: ");
145 gtk_box_pack_start(GTK_BOX(save_as_type_hb), type_lb, FALSE, FALSE, 0);
146 gtk_widget_show(type_lb);
148 type_cm = gtk_combo_box_new_text();
149 gtk_box_pack_start(GTK_BOX(save_as_type_hb), type_cm, FALSE, FALSE, 0);
151 /* List all of the file formats the gdk-pixbuf library supports */
152 file_formats = gdk_pixbuf_get_formats();
155 pixbuf_format = ffp->data;
156 if (gdk_pixbuf_format_is_writable(pixbuf_format)) {
157 format_name = gdk_pixbuf_format_get_name(pixbuf_format);
158 gtk_combo_box_append_text(GTK_COMBO_BOX(type_cm),
160 if (!(g_ascii_strcasecmp(format_name, "png")))
161 default_index = format_index;
164 ffp = g_slist_next(ffp);
166 g_slist_free(file_formats);
168 gtk_combo_box_set_active(GTK_COMBO_BOX(type_cm), default_index);
169 g_object_set_data(G_OBJECT(save_as_w), "type_cm", type_cm);
170 gtk_widget_show(type_cm);
172 g_signal_connect(save_as_w, "destroy", G_CALLBACK(pixbuf_save_destroy_cb), NULL);
174 gtk_widget_show(save_as_w);
175 window_present(save_as_w);
176 parent = gtk_widget_get_parent_window(w);
177 #if GTK_CHECK_VERSION(2,14,0)
178 gdk_window_set_transient_for(gtk_widget_get_window(save_as_w), parent);
180 gdk_window_set_transient_for(save_as_w->window, parent);
184 if(gtk_dialog_run(GTK_DIALOG(save_as_w)) == GTK_RESPONSE_ACCEPT)
185 pixbuf_save_button_cb(save_as_w, pixbuf);
187 window_destroy(save_as_w);
189 /* "Run" the GtkFileChooserDialog. */
190 /* Upon exit: If "Accept" run the OK callback. */
191 /* If the OK callback returns with a FALSE status, re-run the dialog.*/
192 /* If not accept (ie: cancel) destroy the window. */
193 /* XXX: If the OK callback pops up an alert box (eg: for an error) it *must* */
194 /* return with a TRUE status so that the dialog window will be destroyed. */
195 /* Trying to re-run the dialog after popping up an alert box will not work */
196 /* since the user will not be able to dismiss the alert box. */
197 /* The (somewhat unfriendly) effect: the user must re-invoke the */
198 /* GtkFileChooserDialog whenever the OK callback pops up an alert box. */
200 /* ToDo: use GtkFileChooserWidget in a dialog window instead of */
201 /* GtkFileChooserDialog. */
202 while (gtk_dialog_run(GTK_DIALOG(save_as_w)) == GTK_RESPONSE_ACCEPT) {
203 if (pixbuf_save_button_cb(save_as_w, pixbuf)) {
204 break; /* we're done */
207 window_destroy(save_as_w);
211 #endif /* GTK_CHECK_VERSION(2,6,0) */