Mark an unused argument.
[obnox/wireshark/wip.git] / gtk / pixmap_save.c
1 /* pixmap_save.c
2  * Routines for saving pixmaps using the Gdk-Pixmap library
3  * Copyright 2007, Stephen Fisher (see AUTHORS file)
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  * 
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.
15  * 
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.
20  * 
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.
24  */
25
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <errno.h>
31
32 #include <gtk/gtk.h>
33
34 #if GTK_CHECK_VERSION(2,6,0)
35
36 #include <epan/filesystem.h>
37
38 #include "../simple_dialog.h"
39
40 #include "gtk/pixmap_save.h"
41 #include "gtk/gui_utils.h"
42 #include "gtk/file_dlg.h"
43
44
45 static GtkWidget *save_as_w;
46
47 static void
48 pixbuf_save_destroy_cb(GtkWidget *window _U_, gpointer data _U_)
49 {
50         /* We no longer have a save as dialog */
51         save_as_w = NULL;
52 }
53
54 static gboolean
55 pixbuf_save_button_cb(GtkWidget *save_as_w_lcl, GdkPixbuf *pixbuf)
56 {
57         gchar *filename, *file_type;
58         GtkWidget *type_cm, *simple_w;
59         GError *error = NULL;
60         gboolean ret;
61
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));
65         
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);
72                 g_free(filename);
73                 g_free(file_type);
74                 file_selection_set_current_folder(save_as_w_lcl,
75                                                   get_last_open_dir());
76                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(save_as_w_lcl), "");
77                 return FALSE;
78         }
79
80         ret = gdk_pixbuf_save(pixbuf, filename, file_type, &error, NULL);
81         g_free(filename);
82         g_free(file_type);
83
84         if(!ret) {
85                 simple_w = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
86                                          "%s%s%s",
87                                          simple_dialog_primary_start(),
88                                          error->message,
89                                          simple_dialog_primary_end());
90                 gtk_window_set_transient_for(GTK_WINDOW(simple_w),
91                                              GTK_WINDOW(save_as_w_lcl));
92         }
93         return TRUE;
94 }
95
96 void
97 pixmap_save_cb(GtkWidget *w, gpointer pixmap_ptr _U_)
98 {
99         GdkPixmap *pixmap = g_object_get_data(G_OBJECT(w), "pixmap");
100         GdkPixbuf *pixbuf;
101         GdkPixbufFormat *pixbuf_format;
102         GtkWidget *main_vb, *save_as_type_hb, *type_lb, *type_cm;
103         GSList *file_formats,*ffp;
104         GdkWindow *parent;
105
106         gchar *format_name;
107         guint format_index = 0;
108         guint default_index = 0;
109         
110         pixbuf = gdk_pixbuf_get_from_drawable(NULL, GDK_DRAWABLE(pixmap), NULL,
111                                               0, 0, 0, 0, -1, -1);
112
113         if(!pixbuf) {
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());
118                 return;
119         }
120
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);
125                 return;
126         }
127 #endif
128
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);
133 #endif
134
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);
139
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);
143
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);
147
148         type_cm = gtk_combo_box_new_text();
149         gtk_box_pack_start(GTK_BOX(save_as_type_hb), type_cm, FALSE, FALSE, 0);
150         
151         /* List all of the file formats the gdk-pixbuf library supports */
152         file_formats = gdk_pixbuf_get_formats();
153         ffp = file_formats;
154         while(ffp) {
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),
159                                                   format_name);
160                         if (!(g_ascii_strcasecmp(format_name, "png")))
161                                 default_index = format_index;
162                         format_index++;
163                 }
164                 ffp = g_slist_next(ffp);
165         }
166         g_slist_free(file_formats);
167
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);
171
172         g_signal_connect(save_as_w, "destroy", G_CALLBACK(pixbuf_save_destroy_cb), NULL);
173
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);
179 #else
180         gdk_window_set_transient_for(save_as_w->window, parent);
181 #endif
182         
183 #if 0
184         if(gtk_dialog_run(GTK_DIALOG(save_as_w)) == GTK_RESPONSE_ACCEPT)
185                 pixbuf_save_button_cb(save_as_w, pixbuf);
186
187         window_destroy(save_as_w);
188 #else
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.     */
199         /*                                                                              */
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 */
205                 }
206         }
207         window_destroy(save_as_w);
208 #endif
209 }
210
211 #endif /* GTK_CHECK_VERSION(2,6,0) */