Heikki Vatiainen's SAP (Session Announcement Protocol) dissector.
[obnox/wireshark/wip.git] / gtk / print_dlg.c
1 /* print_dlg.c
2  * Dialog boxes for printing
3  *
4  * $Id: print_dlg.c,v 1.9 1999/11/06 06:27:07 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
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 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <errno.h>
31
32 #ifndef __GLOBALS_H__
33 #include "globals.h"
34 #endif
35
36 #ifndef __KEYS_H__
37 #include "keys.h"
38 #endif
39
40 #ifndef __PRINT_H__
41 #include "print.h"
42 #endif
43
44 #ifndef __PREFS_DLG_H__
45 #include "prefs_dlg.h"
46 #endif
47
48 #ifndef __UTIL_H__
49 #include "util.h"
50 #endif
51
52 static void print_cmd_toggle_dest(GtkWidget *widget, gpointer data);
53 static void print_cmd_toggle_detail(GtkWidget *widget, gpointer data);
54 static void print_file_cb(GtkWidget *file_bt, gpointer file_te);
55 static void print_fs_ok_cb(GtkWidget *w, gpointer data);
56 static void print_fs_cancel_cb(GtkWidget *w, gpointer data);
57 static void print_ok_cb(GtkWidget *ok_bt, gpointer parent_w);
58 static void print_close_cb(GtkWidget *close_bt, gpointer parent_w);
59
60 /*
61  * Remember whether we printed to a printer or a file the last time we
62  * printed something.
63  */
64 static int     print_to_file;
65
66 #define PRINT_SUMMARY_RB_KEY      "printer_summary_radio_button"
67 #define PRINT_HEX_CB_KEY          "printer_hex_check_button"
68 #define PRINT_EXPAND_ALL_RB_KEY   "printer_expand_all_radio_button"
69 #define PRINT_AS_DISPLAYED_RB_KEY "printer_as_displayed_radio_button"
70
71 /* Print the capture */
72 void
73 file_print_cmd_cb(GtkWidget *widget, gpointer data)
74 {
75   GtkWidget     *print_w;
76   GtkWidget     *main_vb, *main_tb, *button;
77 #if 0
78   GtkWidget     *format_hb, *format_lb;
79   GSList        *format_grp;
80 #endif
81   GtkWidget     *dest_rb;
82   GtkWidget     *dest_hb, *dest_lb;
83   GtkWidget     *cmd_lb, *cmd_te;
84   GtkWidget     *file_bt_hb, *file_bt, *file_te;
85   GSList        *dest_grp;
86   GtkWidget     *options_hb;
87   GtkWidget     *print_type_vb, *summary_rb, *detail_rb, *hex_cb;
88   GSList        *summary_grp;
89   GtkWidget     *expand_vb, *expand_all_rb, *as_displayed_rb;
90   GSList        *expand_grp;
91   GtkWidget     *bbox, *ok_bt, *cancel_bt;
92
93   /* XXX - don't pop up one if there's already one open; instead,
94        give it the input focus if that's possible. */
95
96   print_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
97   gtk_window_set_title(GTK_WINDOW(print_w), "Ethereal: Print");
98
99   /* Enclosing containers for each row of widgets */
100   main_vb = gtk_vbox_new(FALSE, 5);
101   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
102   gtk_container_add(GTK_CONTAINER(print_w), main_vb);
103   gtk_widget_show(main_vb);
104   
105   main_tb = gtk_table_new(4, 2, FALSE);
106   gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
107   gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
108   gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
109   gtk_widget_show(main_tb);
110
111   /* XXX - printing multiple frames in PostScript looks as if it's
112      tricky - you have to deal with page boundaries, I think -
113      and I'll have to spend some time learning enough about
114      PostScript to figure it out, so, for now, we only print
115      multiple frames as text. */
116 #if 0
117   /* Output format */
118   format_lb = gtk_label_new("Format:");
119   gtk_misc_set_alignment(GTK_MISC(format_lb), 1.0, 0.5);
120   gtk_table_attach_defaults(GTK_TABLE(main_tb), format_lb, 0, 1, 0, 1);
121   gtk_widget_show(format_lb);
122
123   format_hb = gtk_hbox_new(FALSE, 0);
124   gtk_table_attach_defaults(GTK_TABLE(main_tb), format_hb, 1, 2, 0, 1);
125   gtk_widget_show(format_hb);
126
127   button = gtk_radio_button_new_with_label(NULL, "Plain Text");
128   if (prefs.pr_format == PR_FMT_TEXT)
129     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
130   format_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
131   gtk_box_pack_start(GTK_BOX(format_hb), button, FALSE, FALSE, 10);
132   gtk_widget_show(button);
133
134   button = gtk_radio_button_new_with_label(format_grp, "PostScript");
135   if (prefs.pr_format == PR_FMT_PS)
136     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
137   gtk_box_pack_start(GTK_BOX(format_hb), button, FALSE, FALSE, 10);
138   gtk_widget_show(button);
139 #endif
140
141   /* Output destination */
142   dest_lb = gtk_label_new("Print to:");
143   gtk_misc_set_alignment(GTK_MISC(dest_lb), 1.0, 0.5);
144   gtk_table_attach_defaults(GTK_TABLE(main_tb), dest_lb, 0, 1, 1, 2);
145   gtk_widget_show(dest_lb);
146
147   dest_hb = gtk_hbox_new(FALSE, 0);
148   gtk_table_attach_defaults(GTK_TABLE(main_tb), dest_hb, 1, 2, 1, 2);
149   gtk_widget_show(dest_hb);
150
151   button = gtk_radio_button_new_with_label(NULL, "Command");
152   if (!print_to_file)
153     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
154   dest_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
155   gtk_box_pack_start(GTK_BOX(dest_hb), button, FALSE, FALSE, 10);
156   gtk_widget_show(button);
157
158   dest_rb = gtk_radio_button_new_with_label(dest_grp, "File");
159   if (print_to_file)
160     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(dest_rb), TRUE);
161   gtk_signal_connect(GTK_OBJECT(dest_rb), "toggled",
162                         GTK_SIGNAL_FUNC(print_cmd_toggle_dest), NULL);
163   gtk_box_pack_start(GTK_BOX(dest_hb), dest_rb, FALSE, FALSE, 10);
164   gtk_widget_show(dest_rb);
165
166   /* Command text entry */
167   cmd_lb = gtk_label_new("Command:");
168   gtk_object_set_data(GTK_OBJECT(dest_rb), PRINT_CMD_LB_KEY, cmd_lb);
169   gtk_misc_set_alignment(GTK_MISC(cmd_lb), 1.0, 0.5);
170   gtk_table_attach_defaults(GTK_TABLE(main_tb), cmd_lb, 0, 1, 2, 3);
171   gtk_widget_set_sensitive(cmd_lb, !print_to_file);
172   gtk_widget_show(cmd_lb);
173
174   cmd_te = gtk_entry_new();
175   gtk_object_set_data(GTK_OBJECT(dest_rb), PRINT_CMD_TE_KEY, cmd_te);
176   if (prefs.pr_cmd)
177     gtk_entry_set_text(GTK_ENTRY(cmd_te), prefs.pr_cmd);
178   gtk_table_attach_defaults(GTK_TABLE(main_tb), cmd_te, 1, 2, 2, 3);
179   gtk_widget_set_sensitive(cmd_te, !print_to_file);
180   gtk_widget_show(cmd_te);
181
182   /* File button and text entry */
183   file_bt_hb = gtk_hbox_new(FALSE, 0);
184   gtk_table_attach_defaults(GTK_TABLE(main_tb), file_bt_hb, 0, 1, 3, 4);
185   gtk_widget_show(file_bt_hb);
186
187   file_bt = gtk_button_new_with_label("File:");
188   gtk_object_set_data(GTK_OBJECT(dest_rb), PRINT_FILE_BT_KEY, file_bt);
189   gtk_box_pack_end(GTK_BOX(file_bt_hb), file_bt, FALSE, FALSE, 0);
190   gtk_widget_set_sensitive(file_bt, print_to_file);
191   gtk_widget_show(file_bt);
192
193   file_te = gtk_entry_new();
194   gtk_object_set_data(GTK_OBJECT(dest_rb), PRINT_FILE_TE_KEY, file_te);
195   gtk_table_attach_defaults(GTK_TABLE(main_tb), file_te, 1, 2, 3, 4);
196   gtk_widget_set_sensitive(file_te, print_to_file);
197   gtk_widget_show(file_te);
198
199   gtk_signal_connect(GTK_OBJECT(file_bt), "clicked",
200                 GTK_SIGNAL_FUNC(print_file_cb), GTK_OBJECT(file_te));
201
202   /* Horizontal box into which to put two vertical boxes of option
203      buttons. */
204   options_hb = gtk_hbox_new(FALSE, 0);
205   gtk_container_border_width(GTK_CONTAINER(options_hb), 5);
206   gtk_container_add(GTK_CONTAINER(main_vb), options_hb);
207   gtk_widget_show(options_hb);
208
209   /* Vertical box into which to put the "Print summary"/"Print detail"
210      radio buttons and the "Print hex" check button. */
211   print_type_vb = gtk_vbox_new(FALSE, 5);
212   gtk_container_border_width(GTK_CONTAINER(print_type_vb), 5);
213   gtk_container_add(GTK_CONTAINER(options_hb), print_type_vb);
214   gtk_widget_show(print_type_vb);
215
216   /* "Print summary"/"Print detail" radio buttons */
217   summary_rb = gtk_radio_button_new_with_label(NULL, "Print summary");
218   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(summary_rb), FALSE);
219   summary_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(summary_rb));
220   gtk_container_add(GTK_CONTAINER(print_type_vb), summary_rb);
221   gtk_widget_show(summary_rb);
222   detail_rb = gtk_radio_button_new_with_label(summary_grp, "Print detail");
223   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(detail_rb), TRUE);
224   gtk_signal_connect(GTK_OBJECT(detail_rb), "toggled",
225                         GTK_SIGNAL_FUNC(print_cmd_toggle_detail), NULL);
226   gtk_container_add(GTK_CONTAINER(print_type_vb), detail_rb);
227   gtk_widget_show(detail_rb);
228   
229   /* "Print hex" check button. */
230   hex_cb = gtk_check_button_new_with_label("Print hex data");
231   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(hex_cb), FALSE);
232   gtk_container_add(GTK_CONTAINER(print_type_vb), hex_cb);
233   gtk_widget_show(hex_cb);
234
235   /* Vertical box into which to put the "Expand all levels"/"Print as displayed"
236      radio buttons. */
237   expand_vb = gtk_vbox_new(FALSE, 5);
238   gtk_container_border_width(GTK_CONTAINER(expand_vb), 5);
239   gtk_container_add(GTK_CONTAINER(options_hb), expand_vb);
240   gtk_widget_show(expand_vb);
241
242   /* "Expand all levels"/"Print as displayed" radio buttons */
243   expand_all_rb = gtk_radio_button_new_with_label(NULL, "Expand all levels");
244   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(expand_all_rb), TRUE);
245   expand_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(expand_all_rb));
246   gtk_container_add(GTK_CONTAINER(expand_vb), expand_all_rb);
247   gtk_widget_show(expand_all_rb);
248   as_displayed_rb = gtk_radio_button_new_with_label(expand_grp, "Print as displayed");
249   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(as_displayed_rb), FALSE);
250   gtk_container_add(GTK_CONTAINER(expand_vb), as_displayed_rb);
251   gtk_widget_show(as_displayed_rb);
252
253   gtk_object_set_data(GTK_OBJECT(detail_rb), PRINT_EXPAND_ALL_RB_KEY,
254                         expand_all_rb);
255   gtk_object_set_data(GTK_OBJECT(detail_rb), PRINT_AS_DISPLAYED_RB_KEY,
256                         as_displayed_rb);
257   gtk_object_set_data(GTK_OBJECT(detail_rb), PRINT_HEX_CB_KEY,
258                         hex_cb);
259
260   /* Button row: OK and Cancel buttons */
261   bbox = gtk_hbutton_box_new();
262   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
263   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
264   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
265   gtk_widget_show(bbox);
266
267   ok_bt = gtk_button_new_with_label ("OK");
268   gtk_object_set_data(GTK_OBJECT(ok_bt), PRINT_DEST_RB_KEY, dest_rb);
269   gtk_object_set_data(GTK_OBJECT(ok_bt), PRINT_CMD_TE_KEY, cmd_te);
270   gtk_object_set_data(GTK_OBJECT(ok_bt), PRINT_FILE_TE_KEY, file_te);
271   gtk_object_set_data(GTK_OBJECT(ok_bt), PRINT_SUMMARY_RB_KEY, summary_rb);
272   gtk_object_set_data(GTK_OBJECT(ok_bt), PRINT_HEX_CB_KEY, hex_cb);
273   gtk_object_set_data(GTK_OBJECT(ok_bt), PRINT_EXPAND_ALL_RB_KEY, expand_all_rb);
274   gtk_signal_connect(GTK_OBJECT(ok_bt), "clicked",
275     GTK_SIGNAL_FUNC(print_ok_cb), GTK_OBJECT(print_w));
276   GTK_WIDGET_SET_FLAGS(ok_bt, GTK_CAN_DEFAULT);
277   gtk_box_pack_start (GTK_BOX (bbox), ok_bt, TRUE, TRUE, 0);
278   gtk_widget_grab_default(ok_bt);
279   gtk_widget_show(ok_bt);
280
281   cancel_bt = gtk_button_new_with_label ("Cancel");
282   gtk_signal_connect(GTK_OBJECT(cancel_bt), "clicked",
283     GTK_SIGNAL_FUNC(print_close_cb), GTK_OBJECT(print_w));
284   GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
285   gtk_box_pack_start (GTK_BOX (bbox), cancel_bt, TRUE, TRUE, 0);
286   gtk_widget_show(cancel_bt);
287
288 #if 0
289   display_opt_window_active = TRUE;
290 #endif
291   gtk_widget_show(print_w);
292 }
293
294 static void
295 print_cmd_toggle_dest(GtkWidget *widget, gpointer data)
296 {
297   GtkWidget     *cmd_lb, *cmd_te, *file_bt, *file_te;
298   int            to_file;
299
300   cmd_lb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(widget),
301     PRINT_CMD_LB_KEY));
302   cmd_te = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(widget),
303     PRINT_CMD_TE_KEY));
304   file_bt = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(widget),
305     PRINT_FILE_BT_KEY));
306   file_te = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(widget),
307     PRINT_FILE_TE_KEY));
308   if (GTK_TOGGLE_BUTTON (widget)->active) {
309     /* They selected "Print to File" */
310     to_file = TRUE;
311   } else {
312     /* They selected "Print to Command" */
313     to_file = FALSE;
314   }
315   gtk_widget_set_sensitive(cmd_lb, !to_file);
316   gtk_widget_set_sensitive(cmd_te, !to_file);
317   gtk_widget_set_sensitive(file_bt, to_file);
318   gtk_widget_set_sensitive(file_te, to_file);
319 }
320
321 static void
322 print_cmd_toggle_detail(GtkWidget *widget, gpointer data)
323 {
324   GtkWidget     *expand_all_rb, *as_displayed_rb, *hex_cb;
325   gboolean      print_detail;
326
327   expand_all_rb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(widget),
328     PRINT_EXPAND_ALL_RB_KEY));
329   as_displayed_rb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(widget),
330     PRINT_AS_DISPLAYED_RB_KEY));
331   hex_cb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(widget),
332     PRINT_HEX_CB_KEY));
333   if (GTK_TOGGLE_BUTTON (widget)->active) {
334     /* They selected "Print detail" */
335     print_detail = TRUE;
336   } else {
337     /* They selected "Print summary" */
338     print_detail = FALSE;
339   }
340   gtk_widget_set_sensitive(expand_all_rb, print_detail);
341   gtk_widget_set_sensitive(as_displayed_rb, print_detail);
342   gtk_widget_set_sensitive(hex_cb, print_detail);
343 }
344
345 static void
346 print_file_cb(GtkWidget *file_bt, gpointer file_te)
347 {
348   GtkWidget *fs;
349
350   fs = gtk_file_selection_new ("Ethereal: Print to File");
351         gtk_object_set_data(GTK_OBJECT(fs), PRINT_FILE_TE_KEY, file_te);
352
353   gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
354     "clicked", (GtkSignalFunc) print_fs_ok_cb, fs);
355
356   /* Connect the cancel_button to destroy the widget */
357   gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->cancel_button),
358     "clicked", (GtkSignalFunc) print_fs_cancel_cb, fs);
359
360   gtk_widget_show(fs);
361 }
362
363 static void
364 print_fs_ok_cb(GtkWidget *w, gpointer data)
365 {
366   
367   gtk_entry_set_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(data),
368       PRINT_FILE_TE_KEY)),
369       gtk_file_selection_get_filename (GTK_FILE_SELECTION(data)));
370   gtk_widget_destroy(GTK_WIDGET(data));
371 }
372
373 static void
374 print_fs_cancel_cb(GtkWidget *w, gpointer data)
375 {
376   gtk_widget_destroy(GTK_WIDGET(data));
377 }
378
379 static void
380 print_ok_cb(GtkWidget *ok_bt, gpointer parent_w)
381 {
382   GtkWidget *button;
383   print_args_t print_args;
384
385   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(ok_bt),
386                                               PRINT_DEST_RB_KEY);
387   print_to_file = GTK_TOGGLE_BUTTON (button)->active;
388   print_args.to_file = print_to_file;
389
390   if (print_args.to_file)
391     print_args.dest = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(ok_bt),
392       PRINT_FILE_TE_KEY))));
393   else
394     print_args.dest = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(ok_bt),
395       PRINT_CMD_TE_KEY))));
396
397   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(ok_bt),
398                                               PRINT_SUMMARY_RB_KEY);
399   print_args.print_summary = GTK_TOGGLE_BUTTON (button)->active;
400
401   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(ok_bt),
402                                               PRINT_HEX_CB_KEY);
403   print_args.print_hex = GTK_TOGGLE_BUTTON (button)->active;
404
405   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(ok_bt),
406                                               PRINT_EXPAND_ALL_RB_KEY);
407   print_args.expand_all = GTK_TOGGLE_BUTTON (button)->active;
408
409   gtk_widget_destroy(GTK_WIDGET(parent_w));
410 #if 0
411   display_opt_window_active = FALSE;
412 #endif
413
414   /* Now print the packets */
415   if (!print_packets(&cf, &print_args)) {
416     if (print_args.to_file)
417       simple_dialog(ESD_TYPE_WARN, NULL,
418         file_write_error_message(errno), print_args.dest);
419     else
420       simple_dialog(ESD_TYPE_WARN, NULL, "Couldn't run print command %s.",
421         print_args.dest);
422   }
423
424   g_free(print_args.dest);
425 }
426
427 static void
428 print_close_cb(GtkWidget *close_bt, gpointer parent_w)
429 {
430
431   gtk_grab_remove(GTK_WIDGET(parent_w));
432   gtk_widget_destroy(GTK_WIDGET(parent_w));
433 #if 0
434   display_opt_window_active = FALSE;
435 #endif
436 }
437
438 /* Print a packet */
439 void
440 file_print_packet_cmd_cb(GtkWidget *widget, gpointer data) {
441   FILE *fh;
442   print_args_t print_args;
443
444   switch (prefs.pr_dest) {
445
446   case PR_DEST_CMD:
447     fh = popen(prefs.pr_cmd, "w");
448     print_args.to_file = FALSE;
449     print_args.dest = prefs.pr_cmd;
450     break;
451
452   case PR_DEST_FILE:
453     fh = fopen(prefs.pr_file, "w");
454     print_args.to_file = TRUE;
455     print_args.dest = prefs.pr_file;
456     break;
457
458   default:
459     fh = NULL;  /* XXX - "can't happen" */
460     break;
461   }
462   if (fh == NULL) {
463     switch (prefs.pr_dest) {
464
465     case PR_DEST_CMD:
466       simple_dialog(ESD_TYPE_WARN, NULL, "Couldn't run print command %s.",
467         prefs.pr_cmd);
468       break;
469
470     case PR_DEST_FILE:
471       simple_dialog(ESD_TYPE_WARN, NULL, file_write_error_message(errno),
472         prefs.pr_file);
473       break;
474     }
475     return;
476   }
477
478   print_preamble(fh);
479   print_args.print_summary = FALSE;
480   print_args.print_hex = FALSE;
481   print_args.expand_all = TRUE;
482   proto_tree_print(TRUE, &print_args, (GNode*) cf.protocol_tree, cf.pd,
483                 cf.current_frame, fh);
484   print_finale(fh);
485   close_print_dest(print_args.to_file, fh);
486 }
487