2000-05-02 08:04:31 +00:00
|
|
|
/* dlg_utils.c
|
|
|
|
* Utilities to use when constructing dialogs
|
|
|
|
*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
2000-05-02 08:04:31 +00:00
|
|
|
*
|
|
|
|
* Ethereal - Network traffic analyzer
|
2003-09-20 04:59:43 +00:00
|
|
|
* By Gerald Combs <gerald@ethereal.com>
|
2000-05-02 08:04:31 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
2000-05-02 08:04:31 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
2000-05-02 08:04:31 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk/gdkkeysyms.h>
|
|
|
|
|
2004-05-26 03:49:24 +00:00
|
|
|
#include <epan/filesystem.h>
|
|
|
|
|
|
|
|
#include "globals.h"
|
|
|
|
|
2000-07-05 02:45:42 +00:00
|
|
|
#include "gtkglobals.h"
|
2001-12-12 21:39:01 +00:00
|
|
|
#include "ui_util.h"
|
2004-03-29 22:40:58 +00:00
|
|
|
#include "dlg_utils.h"
|
2004-05-26 03:49:24 +00:00
|
|
|
#include "keys.h"
|
2002-11-10 11:00:29 +00:00
|
|
|
#include "compat_macros.h"
|
2004-05-26 03:49:24 +00:00
|
|
|
#include "main.h"
|
2000-07-05 02:45:42 +00:00
|
|
|
|
2004-01-21 21:19:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2004-05-26 03:49:24 +00:00
|
|
|
|
|
|
|
/* Keys ... */
|
|
|
|
#define E_FS_CALLER_PTR_KEY "fs_caller_ptr"
|
|
|
|
|
2004-06-01 17:33:37 +00:00
|
|
|
static gchar *last_open_dir = NULL;
|
|
|
|
static gboolean updated_last_open_dir = FALSE;
|
|
|
|
|
2004-05-26 03:49:24 +00:00
|
|
|
|
2000-05-02 08:04:31 +00:00
|
|
|
static void
|
|
|
|
dlg_activate (GtkWidget *widget, gpointer ok_button);
|
|
|
|
|
2004-05-26 03:49:24 +00:00
|
|
|
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4) || GTK_MAJOR_VERSION < 2
|
|
|
|
static void file_selection_browse_ok_cb(GtkWidget *w, gpointer data);
|
|
|
|
#endif
|
|
|
|
static void file_selection_browse_destroy_cb(GtkWidget *win, GtkWidget* file_te);
|
2004-01-21 21:19:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* create a button for the button row (helper for dlg_button_row_new) */
|
|
|
|
static GtkWidget *
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(GtkWidget *hbox, GtkWidget *button_hbox, gchar *stock_id)
|
2004-01-21 21:19:34 +00:00
|
|
|
{
|
|
|
|
GtkWidget *button;
|
|
|
|
|
|
|
|
button = BUTTON_NEW_FROM_STOCK(stock_id);
|
|
|
|
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
|
2004-01-25 21:27:16 +00:00
|
|
|
OBJECT_SET_DATA(hbox, stock_id, button);
|
|
|
|
gtk_box_pack_end(GTK_BOX(button_hbox), button, FALSE, FALSE, 0);
|
2004-01-21 21:19:34 +00:00
|
|
|
gtk_widget_show(button);
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* create a button row for a dialog */
|
|
|
|
|
|
|
|
/* The purpose of this is, to have one place available, where all button rows
|
|
|
|
* from all dialogs are layouted. This will:
|
|
|
|
*
|
|
|
|
* a.) keep the button layout more consistent over the different dialogs
|
|
|
|
* b.) being able to switch between different button layouts, e.g.:
|
|
|
|
* GTK1 (e.g. win32) "OK" "Apply" "Cancel"
|
|
|
|
* GTK2 (e.g. GNOME) "Apply" "Cancel" "OK"
|
|
|
|
*/
|
|
|
|
GtkWidget *
|
|
|
|
dlg_button_row_new(gchar *stock_id_first, ...)
|
|
|
|
{
|
|
|
|
gint buttons = 0;
|
|
|
|
va_list stock_id_list;
|
|
|
|
gchar *stock_id = stock_id_first;
|
2004-01-25 21:27:16 +00:00
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkWidget *button_hbox;
|
|
|
|
GtkWidget *help_hbox;
|
|
|
|
GtkWidget *button;
|
2004-01-21 21:19:34 +00:00
|
|
|
|
|
|
|
gchar *ok = NULL;
|
|
|
|
gchar *apply = NULL;
|
|
|
|
gchar *save = NULL;
|
2004-07-13 07:15:45 +00:00
|
|
|
gchar *dont_save = NULL;
|
2004-01-21 21:19:34 +00:00
|
|
|
gchar *cancel = NULL;
|
|
|
|
gchar *close = NULL;
|
|
|
|
gchar *clear = NULL;
|
|
|
|
gchar *stop = NULL;
|
|
|
|
gchar *create_stat = NULL;
|
|
|
|
gchar *help = NULL;
|
|
|
|
gchar *print = NULL;
|
|
|
|
gchar *find = NULL;
|
|
|
|
gchar *jump = NULL;
|
2004-01-29 22:37:45 +00:00
|
|
|
gchar *yes = NULL;
|
|
|
|
gchar *no = NULL;
|
2004-01-21 21:19:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
va_start(stock_id_list, stock_id_first);
|
|
|
|
|
|
|
|
/* get all buttons needed */
|
|
|
|
while(stock_id != NULL) {
|
|
|
|
if (strcmp(stock_id, GTK_STOCK_OK) == 0) {
|
|
|
|
ok = stock_id;
|
|
|
|
} else if (strcmp(stock_id, ETHEREAL_STOCK_CREATE_STAT) == 0) {
|
|
|
|
create_stat = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_APPLY) == 0) {
|
|
|
|
apply = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_SAVE) == 0) {
|
|
|
|
save = stock_id;
|
2004-07-13 07:15:45 +00:00
|
|
|
} else if (strcmp(stock_id, ETHEREAL_STOCK_DONT_SAVE) == 0) {
|
|
|
|
dont_save = stock_id;
|
2004-01-21 21:19:34 +00:00
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_CANCEL) == 0) {
|
|
|
|
cancel = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_CLOSE) == 0) {
|
|
|
|
close = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_CLEAR) == 0) {
|
|
|
|
clear = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_STOP) == 0) {
|
|
|
|
stop = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_HELP) == 0) {
|
|
|
|
help = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_PRINT) == 0) {
|
|
|
|
print = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_FIND) == 0) {
|
|
|
|
find = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_JUMP_TO) == 0) {
|
|
|
|
jump = stock_id;
|
2004-01-29 22:37:45 +00:00
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_YES) == 0) {
|
|
|
|
yes = stock_id;
|
|
|
|
} else if (strcmp(stock_id, GTK_STOCK_NO) == 0) {
|
|
|
|
no = stock_id;
|
2004-01-21 21:19:34 +00:00
|
|
|
} else {
|
|
|
|
/* we don't know that button! */
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
buttons++;
|
|
|
|
stock_id = va_arg(stock_id_list, gchar *);
|
|
|
|
}
|
|
|
|
va_end(stock_id_list);
|
|
|
|
|
|
|
|
/* we should have at least one button */
|
|
|
|
g_assert(buttons);
|
|
|
|
|
2004-01-25 21:27:16 +00:00
|
|
|
|
|
|
|
hbox = gtk_hbox_new(FALSE, 0);
|
|
|
|
gtk_widget_show(hbox);
|
|
|
|
|
2004-01-21 21:19:34 +00:00
|
|
|
button_hbox = gtk_hbutton_box_new();
|
2004-01-25 21:27:16 +00:00
|
|
|
gtk_box_pack_end(GTK_BOX(hbox), button_hbox, TRUE, TRUE, 0);
|
|
|
|
gtk_widget_show(button_hbox);
|
|
|
|
|
|
|
|
help_hbox = gtk_hbutton_box_new();
|
|
|
|
gtk_box_pack_end(GTK_BOX(hbox), help_hbox, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show(help_hbox);
|
2004-01-21 21:19:34 +00:00
|
|
|
|
|
|
|
if (buttons == 1) {
|
|
|
|
/* if only one button, simply put it in the middle (default) */
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, stock_id_first);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
|
2004-01-25 21:27:16 +00:00
|
|
|
/* do we have a help button? -> special handling for it */
|
|
|
|
if (help) {
|
|
|
|
button = BUTTON_NEW_FROM_STOCK(help);
|
|
|
|
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
|
|
|
|
OBJECT_SET_DATA(hbox, help, button);
|
|
|
|
gtk_box_pack_start(GTK_BOX(help_hbox), button, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show(button);
|
|
|
|
buttons--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if more than one button, sort buttons from left to right */
|
|
|
|
/* (the whole button cluster will then be right aligned) */
|
|
|
|
gtk_button_box_set_layout (GTK_BUTTON_BOX(button_hbox), GTK_BUTTONBOX_END);
|
|
|
|
gtk_button_box_set_spacing(GTK_BUTTON_BOX(button_hbox), 5);
|
|
|
|
|
2004-09-11 23:03:36 +00:00
|
|
|
/* GTK+ 1.3 and later - on Win32, we use 1.3[.x] or 2.x, not 1.2[.x] */
|
|
|
|
#if !defined(_WIN32) && GTK_MAJOR_VERSION >= 2
|
2004-01-21 21:19:34 +00:00
|
|
|
/* beware: sequence of buttons are important! */
|
|
|
|
|
|
|
|
/* XXX: this can be implemented more elegant of course, but it works as it should */
|
|
|
|
if (buttons == 2) {
|
|
|
|
if (ok && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, ok);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (print && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, print);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (find && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, find);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (jump && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, jump);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (save && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, save);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (ok && clear) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, clear);
|
|
|
|
dlg_button_new(hbox, button_hbox, ok);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (save && close) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, close);
|
|
|
|
dlg_button_new(hbox, button_hbox, save);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (create_stat && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, create_stat);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (buttons == 3) {
|
|
|
|
if (ok && save && close) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, save);
|
|
|
|
dlg_button_new(hbox, button_hbox, close);
|
|
|
|
dlg_button_new(hbox, button_hbox, ok);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (ok && apply && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, apply);
|
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, ok);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (apply && save && close) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, save);
|
|
|
|
dlg_button_new(hbox, button_hbox, close);
|
|
|
|
dlg_button_new(hbox, button_hbox, apply);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
2004-01-29 22:37:45 +00:00
|
|
|
if (yes && no && cancel) {
|
|
|
|
dlg_button_new(hbox, button_hbox, no);
|
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
2004-07-13 07:15:45 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, yes);
|
2004-01-29 22:37:45 +00:00
|
|
|
return hbox;
|
|
|
|
}
|
2004-07-13 07:15:45 +00:00
|
|
|
if (save && dont_save && cancel) {
|
|
|
|
dlg_button_new(hbox, button_hbox, dont_save);
|
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, save);
|
|
|
|
return hbox;
|
|
|
|
}
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (buttons == 4) {
|
|
|
|
if (ok && apply && save && cancel) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, save);
|
|
|
|
dlg_button_new(hbox, button_hbox, apply);
|
|
|
|
dlg_button_new(hbox, button_hbox, cancel);
|
|
|
|
dlg_button_new(hbox, button_hbox, ok);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
if (ok && apply && save && close) {
|
2004-01-25 21:27:16 +00:00
|
|
|
dlg_button_new(hbox, button_hbox, save);
|
|
|
|
dlg_button_new(hbox, button_hbox, apply);
|
|
|
|
dlg_button_new(hbox, button_hbox, close);
|
|
|
|
dlg_button_new(hbox, button_hbox, ok);
|
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* beware: sequence of buttons is important! */
|
2004-01-25 21:27:16 +00:00
|
|
|
if (ok != NULL) dlg_button_new(hbox, button_hbox, ok);
|
|
|
|
if (jump != NULL) dlg_button_new(hbox, button_hbox, jump);
|
|
|
|
if (find != NULL) dlg_button_new(hbox, button_hbox, find);
|
|
|
|
if (print != NULL) dlg_button_new(hbox, button_hbox, print);
|
|
|
|
if (create_stat != NULL) dlg_button_new(hbox, button_hbox, create_stat);
|
|
|
|
if (apply != NULL) dlg_button_new(hbox, button_hbox, apply);
|
2004-01-29 22:37:45 +00:00
|
|
|
if (yes != NULL) dlg_button_new(hbox, button_hbox, yes);
|
|
|
|
if (no != NULL) dlg_button_new(hbox, button_hbox, no);
|
2004-01-25 21:27:16 +00:00
|
|
|
if (save != NULL) dlg_button_new(hbox, button_hbox, save);
|
2004-07-13 07:15:45 +00:00
|
|
|
if (dont_save != NULL) dlg_button_new(hbox, button_hbox, dont_save);
|
2004-01-25 21:27:16 +00:00
|
|
|
if (stop != NULL) dlg_button_new(hbox, button_hbox, stop);
|
|
|
|
if (close != NULL) dlg_button_new(hbox, button_hbox, close);
|
|
|
|
if (clear != NULL) dlg_button_new(hbox, button_hbox, clear);
|
|
|
|
if (cancel != NULL) dlg_button_new(hbox, button_hbox, cancel);
|
2004-01-21 21:19:34 +00:00
|
|
|
|
|
|
|
/* GTK2: we don't know that button combination, add it to the above list! */
|
|
|
|
/* g_assert_not_reached(); */
|
2004-01-25 21:27:16 +00:00
|
|
|
return hbox;
|
2004-01-21 21:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-19 22:17:08 +00:00
|
|
|
/* this is called, when a dialog was closed */
|
2004-05-24 17:41:26 +00:00
|
|
|
static void dlg_destroy_cb(GtkWidget *dialog _U_, gpointer data _U_)
|
2004-05-19 22:17:08 +00:00
|
|
|
{
|
2004-07-12 20:01:41 +00:00
|
|
|
#if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
|
2004-05-22 19:56:19 +00:00
|
|
|
if(top_level) {
|
|
|
|
/* bring main window back to front (workaround for a bug in win32 GTK2.x)
|
|
|
|
XXX - do this only on Windows? */
|
|
|
|
gtk_window_present(GTK_WINDOW(top_level));
|
|
|
|
}
|
2004-05-20 12:47:28 +00:00
|
|
|
#endif
|
2004-05-22 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
2004-05-19 22:17:08 +00:00
|
|
|
|
2000-07-05 02:45:42 +00:00
|
|
|
/* Create a dialog box window that belongs to Ethereal's main window. */
|
|
|
|
GtkWidget *
|
2000-08-23 06:56:31 +00:00
|
|
|
dlg_window_new(const gchar *title)
|
2000-07-05 02:45:42 +00:00
|
|
|
{
|
2003-09-24 08:43:34 +00:00
|
|
|
GtkWidget *win;
|
2000-07-05 02:45:42 +00:00
|
|
|
|
2002-11-03 17:38:45 +00:00
|
|
|
#if GTK_MAJOR_VERSION < 2
|
2004-02-13 00:53:37 +00:00
|
|
|
win = window_new(GTK_WINDOW_DIALOG, title);
|
2002-11-03 17:38:45 +00:00
|
|
|
#else
|
2004-02-13 00:53:37 +00:00
|
|
|
win = window_new(GTK_WINDOW_TOPLEVEL, title);
|
2002-11-03 17:38:45 +00:00
|
|
|
#endif
|
2004-05-23 17:37:36 +00:00
|
|
|
|
2003-12-13 03:36:17 +00:00
|
|
|
/*
|
|
|
|
* XXX - if we're running in the capture child process, we can't easily
|
|
|
|
* make this window transient for the main process's window. We just
|
|
|
|
* punt here.
|
|
|
|
*
|
|
|
|
* Perhaps the child process should only capture packets, write them to
|
|
|
|
* a file, and somehow notify the parent process and let *it* do all
|
|
|
|
* the GUI work. If we can do that efficiently (so that we don't drop
|
|
|
|
* more packets), perhaps we can also do so even when we're *not* doing
|
|
|
|
* an "Update list of packets in real time" capture. That'd let the
|
|
|
|
* child process run set-UID on platforms where you need that in order
|
|
|
|
* to capture, and might also simplify the job of having the GUI main
|
|
|
|
* loop wait both for user input and packet arrival.
|
|
|
|
*/
|
2003-12-12 02:50:04 +00:00
|
|
|
if (top_level) {
|
|
|
|
gtk_window_set_transient_for(GTK_WINDOW(win), GTK_WINDOW(top_level));
|
|
|
|
}
|
2004-05-22 19:56:19 +00:00
|
|
|
|
2004-05-23 17:37:36 +00:00
|
|
|
SIGNAL_CONNECT(win, "destroy", dlg_destroy_cb, NULL);
|
2004-05-22 19:56:19 +00:00
|
|
|
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-24 08:43:34 +00:00
|
|
|
/* Create a file selection dialog box window that belongs to Ethereal's
|
|
|
|
main window. */
|
2004-03-27 11:16:58 +00:00
|
|
|
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
|
|
|
|
GtkWidget *
|
2004-03-29 22:40:58 +00:00
|
|
|
file_selection_new(const gchar *title, file_selection_action_t action)
|
2003-09-24 08:43:34 +00:00
|
|
|
{
|
|
|
|
GtkWidget *win;
|
2004-03-29 22:40:58 +00:00
|
|
|
GtkFileChooserAction gtk_action;
|
2004-03-29 23:03:01 +00:00
|
|
|
const gchar *ok_button_text;
|
2003-09-24 08:43:34 +00:00
|
|
|
|
2004-03-29 22:40:58 +00:00
|
|
|
switch (action) {
|
|
|
|
|
|
|
|
case FILE_SELECTION_OPEN:
|
|
|
|
gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
|
2004-03-29 23:03:01 +00:00
|
|
|
ok_button_text = GTK_STOCK_OPEN;
|
2004-03-29 22:40:58 +00:00
|
|
|
break;
|
|
|
|
|
2004-09-30 02:02:35 +00:00
|
|
|
case FILE_SELECTION_READ_BROWSE:
|
|
|
|
gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
|
|
|
|
ok_button_text = GTK_STOCK_OK;
|
|
|
|
break;
|
|
|
|
|
2004-03-29 22:40:58 +00:00
|
|
|
case FILE_SELECTION_SAVE:
|
|
|
|
gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
|
2004-03-29 23:03:01 +00:00
|
|
|
ok_button_text = GTK_STOCK_SAVE;
|
2004-03-29 22:40:58 +00:00
|
|
|
break;
|
|
|
|
|
2004-09-30 02:02:35 +00:00
|
|
|
case FILE_SELECTION_WRITE_BROWSE:
|
|
|
|
gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
|
|
|
|
ok_button_text = GTK_STOCK_OK;
|
|
|
|
break;
|
|
|
|
|
2004-03-29 22:40:58 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
gtk_action = -1;
|
2004-03-29 23:03:01 +00:00
|
|
|
ok_button_text = NULL;
|
2004-03-29 22:40:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
win = gtk_file_chooser_dialog_new(title, GTK_WINDOW(top_level), gtk_action,
|
2004-09-30 02:02:35 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
ok_button_text, GTK_RESPONSE_ACCEPT,
|
|
|
|
#else
|
2004-03-29 23:03:01 +00:00
|
|
|
ok_button_text, GTK_RESPONSE_ACCEPT,
|
2004-03-27 11:16:58 +00:00
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
2004-09-30 02:02:35 +00:00
|
|
|
#endif
|
2004-03-27 11:16:58 +00:00
|
|
|
NULL);
|
2004-06-01 17:33:37 +00:00
|
|
|
|
|
|
|
/* If we've opened a file before, start out by showing the files in the directory
|
|
|
|
in which that file resided. */
|
|
|
|
if (last_open_dir)
|
|
|
|
file_selection_set_current_folder(win, last_open_dir);
|
|
|
|
|
2004-03-29 22:40:58 +00:00
|
|
|
return win;
|
|
|
|
}
|
2004-03-27 11:16:58 +00:00
|
|
|
#else
|
2004-03-29 22:40:58 +00:00
|
|
|
GtkWidget *
|
|
|
|
file_selection_new(const gchar *title, file_selection_action_t action _U_)
|
|
|
|
{
|
|
|
|
GtkWidget *win;
|
|
|
|
|
2003-09-24 08:43:34 +00:00
|
|
|
win = gtk_file_selection_new(title);
|
|
|
|
#if GTK_MAJOR_VERSION >= 2
|
|
|
|
gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER_ON_PARENT);
|
|
|
|
#endif
|
|
|
|
gtk_window_set_transient_for(GTK_WINDOW(win), GTK_WINDOW(top_level));
|
2004-06-01 17:33:37 +00:00
|
|
|
|
2004-06-03 21:17:06 +00:00
|
|
|
/* XXX - why are we doing this? We don't do it with the GtkFileChooser,
|
|
|
|
as it complains that the file name isn't being set to an absolute
|
|
|
|
path; does this provoke a similar complaint? */
|
2004-06-01 17:33:37 +00:00
|
|
|
gtk_file_selection_set_filename(GTK_FILE_SELECTION(win), "");
|
|
|
|
|
|
|
|
/* If we've opened a file before, start out by showing the files in the directory
|
|
|
|
in which that file resided. */
|
|
|
|
if (last_open_dir)
|
|
|
|
file_selection_set_current_folder(win, last_open_dir);
|
|
|
|
|
2003-09-24 08:43:34 +00:00
|
|
|
return win;
|
2000-07-05 02:45:42 +00:00
|
|
|
}
|
2004-03-29 22:40:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set the current folder for a file selection dialog. */
|
|
|
|
gboolean
|
|
|
|
file_selection_set_current_folder(GtkWidget *fs, const gchar *filename)
|
|
|
|
{
|
|
|
|
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
|
2004-06-20 19:35:04 +00:00
|
|
|
gboolean ret;
|
|
|
|
int filename_len = strlen(filename);
|
|
|
|
gchar *new_filename;
|
|
|
|
|
2004-07-12 18:39:03 +00:00
|
|
|
/* trim filename, so gtk_file_chooser_set_current_folder() likes it, see below */
|
|
|
|
if (filename[filename_len -1] == G_DIR_SEPARATOR
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2004-07-12 18:39:03 +00:00
|
|
|
&& filename_len > 3) /* e.g. "D:\" */
|
|
|
|
#else
|
|
|
|
&& filename_len > 1) /* e.g. "/" */
|
|
|
|
#endif
|
|
|
|
{
|
2004-06-20 19:35:04 +00:00
|
|
|
new_filename = g_strdup(filename);
|
2004-09-11 23:03:36 +00:00
|
|
|
new_filename[filename_len-1] = '\0';
|
2004-06-20 19:35:04 +00:00
|
|
|
} else {
|
|
|
|
new_filename = g_strdup(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this function is very pedantic about it's filename parameter */
|
|
|
|
/* no trailing '\' allowed, unless a win32 root dir "D:\" */
|
|
|
|
ret = gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fs), new_filename);
|
|
|
|
g_free(new_filename);
|
|
|
|
return ret;
|
2004-03-29 22:40:58 +00:00
|
|
|
#else
|
2004-06-20 19:35:04 +00:00
|
|
|
gtk_file_selection_set_filename(GTK_FILE_SELECTION(fs), filename);
|
|
|
|
return TRUE;
|
2004-03-29 22:40:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
2000-07-05 02:45:42 +00:00
|
|
|
|
2004-03-29 22:55:13 +00:00
|
|
|
/* Set the "extra" widget for a file selection dialog, with user-supplied
|
|
|
|
options. */
|
|
|
|
void
|
|
|
|
file_selection_set_extra_widget(GtkWidget *fs, GtkWidget *extra)
|
|
|
|
{
|
|
|
|
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
|
|
|
|
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(fs), extra);
|
|
|
|
#else
|
|
|
|
gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(fs)->action_area), extra,
|
|
|
|
FALSE, FALSE, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-05-26 03:49:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A generic select_file routine that is intended to be connected to
|
|
|
|
* a Browse button on other dialog boxes. This allows the user to browse
|
|
|
|
* for a file and select it. We fill in the text_entry that is given to us.
|
|
|
|
*
|
|
|
|
* We display the window label specified in our args.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
file_selection_browse(GtkWidget *file_bt, GtkWidget *file_te, const char *label, file_selection_action_t action)
|
|
|
|
{
|
|
|
|
GtkWidget *caller = gtk_widget_get_toplevel(file_bt);
|
|
|
|
GtkWidget *fs;
|
|
|
|
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
|
|
|
|
gchar *f_name;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Has a file selection dialog box already been opened for that top-level
|
|
|
|
widget? */
|
|
|
|
fs = OBJECT_GET_DATA(caller, E_FILE_SEL_DIALOG_PTR_KEY);
|
|
|
|
if (fs != NULL) {
|
|
|
|
/* Yes. Just re-activate that dialog box. */
|
|
|
|
reactivate_window(fs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs = file_selection_new(label, action);
|
|
|
|
|
|
|
|
OBJECT_SET_DATA(fs, PRINT_FILE_TE_KEY, file_te);
|
|
|
|
|
|
|
|
/* Set the E_FS_CALLER_PTR_KEY for the new dialog to point to our caller. */
|
|
|
|
OBJECT_SET_DATA(fs, E_FS_CALLER_PTR_KEY, caller);
|
|
|
|
|
|
|
|
/* Set the E_FILE_SEL_DIALOG_PTR_KEY for the caller to point to us */
|
|
|
|
OBJECT_SET_DATA(caller, E_FILE_SEL_DIALOG_PTR_KEY, fs);
|
|
|
|
|
|
|
|
/* Call a handler when the file selection box is destroyed, so we can inform
|
|
|
|
our caller, if any, that it's been destroyed. */
|
|
|
|
SIGNAL_CONNECT(fs, "destroy", GTK_SIGNAL_FUNC(file_selection_browse_destroy_cb),
|
|
|
|
file_te);
|
|
|
|
|
|
|
|
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
|
|
|
|
if (gtk_dialog_run(GTK_DIALOG(fs)) == GTK_RESPONSE_ACCEPT)
|
|
|
|
{
|
|
|
|
f_name = g_strdup(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs)));
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(file_te), f_name);
|
|
|
|
g_free(f_name);
|
|
|
|
}
|
|
|
|
window_destroy(fs);
|
|
|
|
#else
|
|
|
|
SIGNAL_CONNECT(GTK_FILE_SELECTION(fs)->ok_button, "clicked",
|
|
|
|
file_selection_browse_ok_cb, fs);
|
|
|
|
|
2005-02-27 13:00:22 +00:00
|
|
|
window_set_cancel_button(fs, GTK_FILE_SELECTION(fs)->cancel_button,
|
|
|
|
window_cancel_button_cb);
|
2004-05-26 03:49:24 +00:00
|
|
|
|
|
|
|
SIGNAL_CONNECT(fs, "delete_event", window_delete_event_cb, fs);
|
|
|
|
|
|
|
|
gtk_widget_show(fs);
|
|
|
|
window_present(fs);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4) || GTK_MAJOR_VERSION < 2
|
|
|
|
static void
|
|
|
|
file_selection_browse_ok_cb(GtkWidget *w _U_, gpointer data)
|
|
|
|
{
|
|
|
|
gchar *f_name;
|
|
|
|
GtkWidget *win = data;
|
|
|
|
|
|
|
|
f_name = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION (data)));
|
|
|
|
|
|
|
|
/* Perhaps the user specified a directory instead of a file.
|
|
|
|
Check whether they did. */
|
|
|
|
if (test_for_directory(f_name) == EISDIR) {
|
|
|
|
/* It's a directory - set the file selection box to display it. */
|
|
|
|
set_last_open_dir(f_name);
|
|
|
|
g_free(f_name);
|
2004-06-01 17:33:37 +00:00
|
|
|
file_selection_set_current_folder(data, last_open_dir);
|
2004-05-26 03:49:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(OBJECT_GET_DATA(win, PRINT_FILE_TE_KEY)),
|
|
|
|
f_name);
|
|
|
|
window_destroy(GTK_WIDGET(win));
|
|
|
|
|
|
|
|
g_free(f_name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
file_selection_browse_destroy_cb(GtkWidget *win, GtkWidget* parent_te)
|
|
|
|
{
|
|
|
|
GtkWidget *caller;
|
|
|
|
|
|
|
|
/* Get the widget that requested that we be popped up.
|
|
|
|
(It should arrange to destroy us if it's destroyed, so
|
|
|
|
that we don't get a pointer to a non-existent window here.) */
|
|
|
|
caller = OBJECT_GET_DATA(win, E_FS_CALLER_PTR_KEY);
|
|
|
|
|
|
|
|
/* Tell it we no longer exist. */
|
|
|
|
OBJECT_SET_DATA(caller, E_FILE_SEL_DIALOG_PTR_KEY, NULL);
|
|
|
|
|
|
|
|
/* Give the focus to the file text entry widget so the user can just press
|
|
|
|
Return to print to the file. */
|
|
|
|
gtk_widget_grab_focus(parent_te);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-01 17:33:37 +00:00
|
|
|
void
|
|
|
|
set_last_open_dir(char *dirname)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
gchar *new_last_open_dir;
|
|
|
|
|
|
|
|
if (dirname) {
|
|
|
|
len = strlen(dirname);
|
|
|
|
if (dirname[len-1] == G_DIR_SEPARATOR) {
|
|
|
|
new_last_open_dir = g_strconcat(dirname, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_last_open_dir = g_strconcat(dirname,
|
|
|
|
G_DIR_SEPARATOR_S, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_open_dir == NULL ||
|
|
|
|
strcmp(last_open_dir, new_last_open_dir) != 0)
|
|
|
|
updated_last_open_dir = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_last_open_dir = NULL;
|
|
|
|
if (last_open_dir != NULL)
|
|
|
|
updated_last_open_dir = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_open_dir) {
|
|
|
|
g_free(last_open_dir);
|
|
|
|
}
|
|
|
|
last_open_dir = new_last_open_dir;
|
|
|
|
}
|
2004-05-26 03:49:24 +00:00
|
|
|
|
2004-06-01 17:33:37 +00:00
|
|
|
char *
|
|
|
|
get_last_open_dir(void)
|
|
|
|
{
|
|
|
|
return last_open_dir;
|
|
|
|
}
|
2004-05-26 03:49:24 +00:00
|
|
|
|
2000-05-02 08:04:31 +00:00
|
|
|
/* Set the "activate" signal for a widget to call a routine to
|
|
|
|
activate the "OK" button for a dialog box.
|
|
|
|
|
|
|
|
XXX - there should be a way to specify that a GtkEntry widget
|
|
|
|
shouldn't itself handle the Return key, but should let it be
|
|
|
|
passed on to the parent, so that you don't have to do this
|
|
|
|
by hand for every GtkEntry widget in a dialog box, but, alas,
|
|
|
|
there isn't. (Does this problem exist for other widgets?
|
|
|
|
I.e., are there any others that seize the Return key? */
|
|
|
|
void
|
|
|
|
dlg_set_activate(GtkWidget *widget, GtkWidget *ok_button)
|
|
|
|
{
|
2002-11-10 11:00:29 +00:00
|
|
|
SIGNAL_CONNECT(widget, "activate", dlg_activate, ok_button);
|
2000-05-02 08:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-03-05 11:56:00 +00:00
|
|
|
dlg_activate (GtkWidget *widget _U_, gpointer ok_button)
|
2000-05-02 08:04:31 +00:00
|
|
|
{
|
|
|
|
gtk_widget_activate(GTK_WIDGET(ok_button));
|
|
|
|
}
|
|
|
|
|
2002-11-03 17:38:45 +00:00
|
|
|
#if GTK_MAJOR_VERSION < 2
|
2000-05-08 04:23:46 +00:00
|
|
|
/* Sigh. GTK+ appears not to acknowledge that it should be possible
|
2000-05-26 07:32:56 +00:00
|
|
|
to attach mnemonics to anything other than menu items; provide
|
|
|
|
routines to create radio and check buttons with labels that
|
|
|
|
include mnemonics. */
|
|
|
|
typedef struct {
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkAccelGroup *accel_group;
|
|
|
|
} fix_label_args_t;
|
|
|
|
|
|
|
|
static void
|
|
|
|
dlg_fix_label_callback(GtkWidget *label_widget, gpointer data)
|
2000-05-08 04:23:46 +00:00
|
|
|
{
|
2000-05-26 07:32:56 +00:00
|
|
|
fix_label_args_t *args = data;
|
|
|
|
gchar *label;
|
2000-05-08 04:23:46 +00:00
|
|
|
guint accel_key;
|
|
|
|
|
2000-05-26 07:32:56 +00:00
|
|
|
gtk_label_get(GTK_LABEL(label_widget), &label);
|
|
|
|
accel_key = gtk_label_parse_uline(GTK_LABEL(label_widget), label);
|
2000-05-08 04:23:46 +00:00
|
|
|
if (accel_key != GDK_VoidSymbol) {
|
|
|
|
/* Yes, we have a mnemonic. */
|
2000-05-26 07:32:56 +00:00
|
|
|
gtk_widget_add_accelerator(args->button, "clicked", args->accel_group,
|
2000-05-08 04:23:46 +00:00
|
|
|
accel_key, 0, GTK_ACCEL_LOCKED);
|
2000-05-26 07:32:56 +00:00
|
|
|
gtk_widget_add_accelerator(args->button, "clicked", args->accel_group,
|
2000-05-08 04:23:46 +00:00
|
|
|
accel_key, GDK_MOD1_MASK, GTK_ACCEL_LOCKED);
|
|
|
|
}
|
2000-05-26 07:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dlg_fix_button_label(GtkWidget *button, GtkAccelGroup *accel_group)
|
|
|
|
{
|
|
|
|
fix_label_args_t args;
|
|
|
|
|
|
|
|
args.button = button;
|
|
|
|
args.accel_group = accel_group;
|
|
|
|
gtk_container_foreach(GTK_CONTAINER(button), dlg_fix_label_callback, &args);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
dlg_radio_button_new_with_label_with_mnemonic(GSList *group,
|
|
|
|
const gchar *label, GtkAccelGroup *accel_group)
|
|
|
|
{
|
|
|
|
GtkWidget *radio_button;
|
2000-05-08 04:23:46 +00:00
|
|
|
|
2000-05-26 07:32:56 +00:00
|
|
|
radio_button = gtk_radio_button_new_with_label (group, label);
|
|
|
|
dlg_fix_button_label(radio_button, accel_group);
|
2000-05-08 04:23:46 +00:00
|
|
|
return radio_button;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
dlg_check_button_new_with_label_with_mnemonic(const gchar *label,
|
|
|
|
GtkAccelGroup *accel_group)
|
|
|
|
{
|
|
|
|
GtkWidget *check_button;
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2000-05-26 07:32:56 +00:00
|
|
|
check_button = gtk_check_button_new_with_label (label);
|
|
|
|
dlg_fix_button_label(check_button, accel_group);
|
2000-05-08 04:23:46 +00:00
|
|
|
return check_button;
|
|
|
|
}
|
2004-01-07 00:10:52 +00:00
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
dlg_toggle_button_new_with_label_with_mnemonic(const gchar *label,
|
|
|
|
GtkAccelGroup *accel_group)
|
|
|
|
{
|
|
|
|
GtkWidget *toggle_button;
|
|
|
|
|
|
|
|
toggle_button = gtk_toggle_button_new_with_label (label);
|
|
|
|
dlg_fix_button_label(toggle_button, accel_group);
|
|
|
|
return toggle_button;
|
|
|
|
}
|
2002-11-03 17:38:45 +00:00
|
|
|
#endif
|