Commit 7aba8116 authored by Chyla Zbigniew's avatar Chyla Zbigniew
Browse files

New function, some code moved from gui-file.c.


* src/gnumeric-util.[ch] (gnumeric_dialog_file_selection): New
function, some code moved from gui-file.c.

* src/workbook.[ch] (workbook_get_filename): New accessor function.

* src/gui-file.h
(gui_file_open): New function.
(gui_file_import): Removed fname argument.

* src/gui-file.c
(gui_file_import): Create and open file selector, using
gnumeric_dialog_file_selection to display it. Use one dialog to select
file and file format, we don't need glade file anymore. Optionally
show all registered openers as importers.
(gui_file_save_as): Uses gnumeric_dialog_file_selection. Take
workbook's current saver if current_saver is NULL (previously we've
been loosing this information), use
gnumeric_option_menu_get_selected_index instead of playing with
signals.
(gui_file_open): New function, some code moved from
dialog_query_load_file.
(gui_file_save): Always call wb_view_preferred_size.
(do_save_as): Take GnumFileSaver as argument.
(cb_select, handle_ok, saver_activate, fill_save_menu,
make_format_chooser, file_dialog_delete_event, fs_key_event,
fs_set_filename): Removed.

* src/workbook-control-gui.c
(cb_file_new): Just call gui_file_open.
(cb_file_import): Call gui_file_import.
(workbook_control_gui_init): Initialize current_saver with NULL.
parent 2fc499eb
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/gnumeric-util.[ch] (gnumeric_dialog_file_selection): New
function, some code moved from gui-file.c.
* src/workbook.[ch] (workbook_get_filename): New accessor function.
* src/gui-file.h
(gui_file_open): New function.
(gui_file_import): Removed fname argument.
* src/gui-file.c
(gui_file_import): Create and open file selector, using
gnumeric_dialog_file_selection to display it. Use one dialog to select
file and file format, we don't need glade file anymore. Optionally
show all registered openers as importers.
(gui_file_save_as): Uses gnumeric_dialog_file_selection. Take
workbook's current saver if current_saver is NULL (previously we've
been loosing this information), use
gnumeric_option_menu_get_selected_index instead of playing with
signals.
(gui_file_open): New function, some code moved from
dialog_query_load_file.
(gui_file_save): Always call wb_view_preferred_size.
(do_save_as): Take GnumFileSaver as argument.
(cb_select, handle_ok, saver_activate, fill_save_menu,
make_format_chooser, file_dialog_delete_event, fs_key_event,
fs_set_filename): Removed.
* src/workbook-control-gui.c
(cb_file_new): Just call gui_file_open.
(cb_file_import): Call gui_file_import.
(workbook_control_gui_init): Initialize current_saver with NULL.
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/workbook.c (workbook_new): Slight modification of the previous
......
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/gnumeric-util.[ch] (gnumeric_dialog_file_selection): New
function, some code moved from gui-file.c.
* src/workbook.[ch] (workbook_get_filename): New accessor function.
* src/gui-file.h
(gui_file_open): New function.
(gui_file_import): Removed fname argument.
* src/gui-file.c
(gui_file_import): Create and open file selector, using
gnumeric_dialog_file_selection to display it. Use one dialog to select
file and file format, we don't need glade file anymore. Optionally
show all registered openers as importers.
(gui_file_save_as): Uses gnumeric_dialog_file_selection. Take
workbook's current saver if current_saver is NULL (previously we've
been loosing this information), use
gnumeric_option_menu_get_selected_index instead of playing with
signals.
(gui_file_open): New function, some code moved from
dialog_query_load_file.
(gui_file_save): Always call wb_view_preferred_size.
(do_save_as): Take GnumFileSaver as argument.
(cb_select, handle_ok, saver_activate, fill_save_menu,
make_format_chooser, file_dialog_delete_event, fs_key_event,
fs_set_filename): Removed.
* src/workbook-control-gui.c
(cb_file_new): Just call gui_file_open.
(cb_file_import): Call gui_file_import.
(workbook_control_gui_init): Initialize current_saver with NULL.
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/workbook.c (workbook_new): Slight modification of the previous
......
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/gnumeric-util.[ch] (gnumeric_dialog_file_selection): New
function, some code moved from gui-file.c.
* src/workbook.[ch] (workbook_get_filename): New accessor function.
* src/gui-file.h
(gui_file_open): New function.
(gui_file_import): Removed fname argument.
* src/gui-file.c
(gui_file_import): Create and open file selector, using
gnumeric_dialog_file_selection to display it. Use one dialog to select
file and file format, we don't need glade file anymore. Optionally
show all registered openers as importers.
(gui_file_save_as): Uses gnumeric_dialog_file_selection. Take
workbook's current saver if current_saver is NULL (previously we've
been loosing this information), use
gnumeric_option_menu_get_selected_index instead of playing with
signals.
(gui_file_open): New function, some code moved from
dialog_query_load_file.
(gui_file_save): Always call wb_view_preferred_size.
(do_save_as): Take GnumFileSaver as argument.
(cb_select, handle_ok, saver_activate, fill_save_menu,
make_format_chooser, file_dialog_delete_event, fs_key_event,
fs_set_filename): Removed.
* src/workbook-control-gui.c
(cb_file_new): Just call gui_file_open.
(cb_file_import): Call gui_file_import.
(workbook_control_gui_init): Initialize current_saver with NULL.
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/workbook.c (workbook_new): Slight modification of the previous
......
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/gnumeric-util.[ch] (gnumeric_dialog_file_selection): New
function, some code moved from gui-file.c.
* src/workbook.[ch] (workbook_get_filename): New accessor function.
* src/gui-file.h
(gui_file_open): New function.
(gui_file_import): Removed fname argument.
* src/gui-file.c
(gui_file_import): Create and open file selector, using
gnumeric_dialog_file_selection to display it. Use one dialog to select
file and file format, we don't need glade file anymore. Optionally
show all registered openers as importers.
(gui_file_save_as): Uses gnumeric_dialog_file_selection. Take
workbook's current saver if current_saver is NULL (previously we've
been loosing this information), use
gnumeric_option_menu_get_selected_index instead of playing with
signals.
(gui_file_open): New function, some code moved from
dialog_query_load_file.
(gui_file_save): Always call wb_view_preferred_size.
(do_save_as): Take GnumFileSaver as argument.
(cb_select, handle_ok, saver_activate, fill_save_menu,
make_format_chooser, file_dialog_delete_event, fs_key_event,
fs_set_filename): Removed.
* src/workbook-control-gui.c
(cb_file_new): Just call gui_file_open.
(cb_file_import): Call gui_file_import.
(workbook_control_gui_init): Initialize current_saver with NULL.
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/workbook.c (workbook_new): Slight modification of the previous
......
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/gnumeric-util.[ch] (gnumeric_dialog_file_selection): New
function, some code moved from gui-file.c.
* src/workbook.[ch] (workbook_get_filename): New accessor function.
* src/gui-file.h
(gui_file_open): New function.
(gui_file_import): Removed fname argument.
* src/gui-file.c
(gui_file_import): Create and open file selector, using
gnumeric_dialog_file_selection to display it. Use one dialog to select
file and file format, we don't need glade file anymore. Optionally
show all registered openers as importers.
(gui_file_save_as): Uses gnumeric_dialog_file_selection. Take
workbook's current saver if current_saver is NULL (previously we've
been loosing this information), use
gnumeric_option_menu_get_selected_index instead of playing with
signals.
(gui_file_open): New function, some code moved from
dialog_query_load_file.
(gui_file_save): Always call wb_view_preferred_size.
(do_save_as): Take GnumFileSaver as argument.
(cb_select, handle_ok, saver_activate, fill_save_menu,
make_format_chooser, file_dialog_delete_event, fs_key_event,
fs_set_filename): Removed.
* src/workbook-control-gui.c
(cb_file_new): Just call gui_file_open.
(cb_file_import): Call gui_file_import.
(workbook_control_gui_init): Initialize current_saver with NULL.
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* src/workbook.c (workbook_new): Slight modification of the previous
......
2001-06-05 Zbigniew Chyla <cyba@gnome.pl>
* import.glade: Removed.
* dialogs.h (dialog_query_load_file): Removed.
2001-06-04 Almer S. Tigelaar <almer@gnome.org>
* dialog-stf-csv-page.c (csv_page_global_change): Cope for
......
......@@ -29,7 +29,6 @@ int dialog_paste_special (WorkbookControlGUI *wbcg);
void dialog_insert_cells (WorkbookControlGUI *wbcg, Sheet *sheet);
void dialog_delete_cells (WorkbookControlGUI *wbcg, Sheet *sheet);
void dialog_zoom (WorkbookControlGUI *wbcg, Sheet *sheet);
char *dialog_query_load_file (WorkbookControlGUI *wbcg);
void dialog_about (WorkbookControlGUI *wbcg);
void dialog_define_names (WorkbookControlGUI *wbcg);
void dialog_cell_comment (WorkbookControlGUI *wbcg,
......
......@@ -45,6 +45,92 @@ gnumeric_dialog_question_yes_no (WorkbookControlGUI *wbcg,
return gnumeric_dialog_run (wbcg, GNOME_DIALOG (dialog)) == 0;
}
static void
fsel_dialog_finish (GtkWidget *widget)
{
gtk_widget_hide (widget);
gtk_main_quit ();
}
static void
fsel_handle_ok (GtkWidget *widget, gboolean *result)
{
GtkFileSelection *fsel;
gchar *file_name;
fsel = GTK_FILE_SELECTION (gtk_widget_get_ancestor (widget, GTK_TYPE_FILE_SELECTION));
file_name = gtk_file_selection_get_filename (fsel);
/* Change into directory if that's what user selected */
if (g_file_test (file_name, G_FILE_TEST_ISDIR)) {
gint name_len;
gchar *dir_name;
name_len = strlen (file_name);
if (name_len < 1 || file_name [name_len - 1] != '/') {
/* The file selector needs a '/' at the end of a directory name */
dir_name = g_strconcat (file_name, "/", NULL);
} else {
dir_name = g_strdup (file_name);
}
gtk_file_selection_set_filename (fsel, dir_name);
g_free (dir_name);
} else {
fsel_dialog_finish (GTK_WIDGET (fsel));
*result = TRUE;
}
}
static void
fsel_handle_cancel (GtkWidget *widget, gpointer user_data)
{
GtkWidget *fsel;
fsel = gtk_widget_get_ancestor (widget, GTK_TYPE_FILE_SELECTION);
fsel_dialog_finish (fsel);
}
static gint
fsel_delete_event (GtkWidget *widget, GdkEventAny *event)
{
fsel_dialog_finish (widget);
return TRUE;
}
static gint
fsel_key_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
if (event->keyval == GDK_Escape) {
gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
fsel_dialog_finish (widget);
return TRUE;
}
return FALSE;
}
gboolean
gnumeric_dialog_file_selection (WorkbookControlGUI *wbcg, GtkFileSelection *fsel)
{
gboolean result = FALSE;
gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
gnumeric_set_transient (wbcg, GTK_WINDOW (fsel));
gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
GTK_SIGNAL_FUNC (fsel_handle_ok), &result);
gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
GTK_SIGNAL_FUNC (fsel_handle_cancel), NULL);
gtk_signal_connect (GTK_OBJECT (fsel), "key_press_event",
GTK_SIGNAL_FUNC (fsel_key_event), NULL);
gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
GTK_SIGNAL_FUNC (fsel_delete_event), NULL);
gtk_widget_show_all (GTK_WIDGET (fsel));
gtk_grab_add (GTK_WIDGET (fsel));
gtk_main ();
return result;
}
/*
* TODO:
* Get rid of trailing newlines /whitespace.
......
......@@ -10,6 +10,7 @@
gboolean gnumeric_dialog_question_yes_no (WorkbookControlGUI *wbcg,
const gchar *message,
gboolean default_answer);
gboolean gnumeric_dialog_file_selection (WorkbookControlGUI *wbcg, GtkFileSelection *fsel);
void gnumeric_notice (WorkbookControlGUI *wbcg, const char *type, const char *str);
void gnumeric_non_modal_dialog (WorkbookControlGUI *wbcg, GtkWindow *dialog);
......
/*
* gui-file.c:
*
* Authors:
* Jon K Hellan (hellan@acm.org)
* Zbigniew Chyla (cyba@gnome.pl)
*/
#include <config.h>
#include <errno.h>
#include <sys/stat.h>
#include <gnome.h>
#include <glade/glade.h>
#include "gnumeric.h"
......@@ -13,7 +21,6 @@
#include "workbook-control-gui-priv.h"
#include "workbook-view.h"
#include "workbook.h"
#include <sys/stat.h>
static gint
file_opener_description_cmp (gconstpointer a, gconstpointer b)
......@@ -33,206 +40,67 @@ file_saver_description_cmp (gconstpointer a, gconstpointer b)
gnum_file_saver_get_description (fs_b));
}
static void
cb_select (GtkWidget *clist, gint row, gint column,
GdkEventButton *event, GtkWidget *dialog)
{
/* If the filter is double-clicked we proceed with importing and
dismiss chooser. */
if (event && event->type == GDK_2BUTTON_PRESS) {
gtk_signal_emit_by_name (GTK_OBJECT (dialog), "clicked", 0);
}
}
/*
* Lets the user choose an import filter for @filename, and
* uses that to load the file
* Lets the user choose an import filter for selected file, and
* uses that to load the file.
*/
gboolean
gui_file_import (WorkbookControlGUI *wbcg, const char *filename)
void
gui_file_import (WorkbookControlGUI *wbcg)
{
GladeXML *gui;
GtkWidget *dialog;
GtkCList *clist;
GnumFileOpener *fo = NULL;
int row;
GList *importers, *l;
gint ret;
GtkFileSelection *fsel;
GtkBox *box;
GtkOptionMenu *omenu;
GtkMenu *menu;
GnumFileOpener *fo = NULL;
gchar *file_name;
gui = gnumeric_glade_xml_new (wbcg, "import.glade");
if (gui == NULL) {
return FALSE;
if (gnome_config_get_bool_with_default (
"Gnumeric/File/ImportUsesAllOpeners=false", NULL)) {
importers = get_file_openers ();
} else {
importers = get_file_importers ();
}
dialog = glade_xml_get_widget (gui, "import-dialog");
gnome_dialog_set_default (GNOME_DIALOG (dialog), 0);
clist = GTK_CLIST (glade_xml_get_widget (gui, "import-clist"));
importers = g_list_sort (g_list_copy (get_file_importers ()),
file_opener_description_cmp);
for (l = importers, row = 0; l != NULL; l = l->next, row++) {
importers = g_list_copy (importers);
importers = g_list_sort (importers, file_opener_description_cmp);
/* Make format chooser */
box = GTK_BOX (gtk_hbox_new (0, GNOME_PAD));
omenu = GTK_OPTION_MENU (gtk_option_menu_new ());
menu = GTK_MENU (gtk_menu_new ());
for (l = importers; l != NULL; l = l->next) {
GnumFileOpener *fo = l->data;
char *text[1];
text[0] = (gchar *) gnum_file_opener_get_description (fo);
gtk_clist_append (clist, text);
gtk_clist_set_row_data (clist, row, l->data);
}
if (row > 0) {
gtk_clist_select_row (clist, 0, 0);
}
g_list_free (importers);
gtk_signal_connect (GTK_OBJECT(clist), "select_row",
GTK_SIGNAL_FUNC (cb_select), (gpointer) dialog);
gtk_widget_grab_focus (GTK_WIDGET (clist));
GtkWidget *item;
ret = gnumeric_dialog_run (wbcg, GNOME_DIALOG (dialog));
if (ret == 0 && clist->selection != NULL) {
fo = gtk_clist_get_row_data (clist, GPOINTER_TO_INT (clist->selection->data));
item = gtk_menu_item_new_with_label (gnum_file_opener_get_description (fo));
gtk_widget_show (item);
gtk_menu_append (menu, item);
}
if (ret != -1) {
gnome_dialog_close (GNOME_DIALOG (dialog));
gtk_option_menu_set_menu (omenu, GTK_WIDGET (menu));
gtk_box_pack_start (box, gtk_label_new (_("File format:")),
FALSE, FALSE, GNOME_PAD);
gtk_box_pack_start (box, GTK_WIDGET (omenu), FALSE, TRUE, GNOME_PAD);
/* Pack it into file selector */
fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Import file")));
gtk_box_pack_start (GTK_BOX (fsel->action_area), GTK_WIDGET (box),
FALSE, TRUE, 0);
/* Show file selector */
if (!gnumeric_dialog_file_selection (wbcg, fsel)) {
g_list_free (importers);
gtk_object_destroy (GTK_OBJECT (fsel));
return;
}
gtk_object_unref (GTK_OBJECT (gui));
fo = g_list_nth_data (importers, gnumeric_option_menu_get_selected_index (omenu));
file_name = gtk_file_selection_get_filename (fsel);
if (fo != NULL) {
return wb_view_open_custom (wb_control_view (WORKBOOK_CONTROL (wbcg)),
WORKBOOK_CONTROL (wbcg), fo, filename);
} else {
return FALSE;
}
}
static void
handle_ok (GtkWidget *widget, gboolean *dialog_result)
{
struct stat sb;
GtkFileSelection *fsel;
char *name;
fsel = GTK_FILE_SELECTION (
gtk_widget_get_ancestor (widget, GTK_TYPE_FILE_SELECTION));
name = gtk_file_selection_get_filename (fsel);
/* Change into directory if that's what user selected */
if ((stat (name, &sb) == 0) && S_ISDIR (sb.st_mode)) {
char *last_slash = strrchr (name, '/');
gchar *dirname;
/* The file selector needs a '/' at the end of a
directory name */
if (!last_slash || *(last_slash + 1) != '\0')
dirname = g_strconcat (name, "/", NULL);
else
dirname = g_strdup (name);
gtk_file_selection_set_filename (fsel, dirname);
g_free (dirname);
} else {
*dialog_result = TRUE;
gtk_main_quit ();
}
}
/**
* saver_activate:
*
* Callback routine to choose the current file saver
*/
static void
saver_activate (GtkMenuItem *item, GnumFileSaver *saver)
{
WorkbookControlGUI *wbcg;
wbcg = gtk_object_get_data (GTK_OBJECT (item), "wbcg");
wbcg->current_saver = saver;
}
static void
fill_save_menu (WorkbookControlGUI *wbcg, GtkOptionMenu *omenu, GtkMenu *menu)
{
GList *savers, *l;
savers = g_list_sort (g_list_copy (get_file_savers ()),
file_saver_description_cmp);
for (l = savers; l != NULL; l = l->next) {
GtkWidget *menu_item;
GnumFileSaver *fs = l->data;
menu_item = gtk_menu_item_new_with_label (gnum_file_saver_get_description (fs));
gtk_object_set_data (GTK_OBJECT (menu_item), "wbcg", wbcg);
gtk_widget_show (menu_item);
gtk_menu_append (menu, menu_item);
gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
GTK_SIGNAL_FUNC (saver_activate), fs);
(void) wb_view_open_custom (wb_control_view (WORKBOOK_CONTROL (wbcg)),
WORKBOOK_CONTROL (wbcg), fo, file_name);
}
gtk_option_menu_set_menu (GTK_OPTION_MENU (omenu), GTK_WIDGET (menu));
if (wbcg->current_saver == NULL ||
g_list_find (savers, wbcg->current_saver) == NULL) {
wbcg->current_saver = get_default_file_saver ();
}
gtk_option_menu_set_history (omenu,
g_list_index (savers, wbcg->current_saver));
g_list_free (savers);
}
static GtkWidget *
make_format_chooser (WorkbookControlGUI *wbcg)
{
GtkWidget *box, *label;
GtkWidget *omenu, *menu;
box = gtk_hbox_new (0, GNOME_PAD);
label = gtk_label_new (_("File format:"));
omenu = gtk_option_menu_new ();
menu = gtk_menu_new ();
fill_save_menu (wbcg, GTK_OPTION_MENU (omenu), GTK_MENU (menu));
gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, GNOME_PAD);
gtk_box_pack_start (GTK_BOX (box), omenu, FALSE, TRUE, GNOME_PAD);
gtk_widget_show_all (box);
return box;
}
static guint
file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
{
gtk_main_quit ();
return TRUE;
}
static gint
fs_key_event (GtkFileSelection *fsel, GdkEventKey *event)
{
if (event->keyval == GDK_Escape) {
gtk_button_clicked (GTK_BUTTON (fsel->cancel_button));
return 1;
} else
return 0;
}
/*
* fs_set_filename
* @fsel file selection dialog
* @wb workbook
* Set default filename in the file selection dialog.
* Set it to the workbook file name sans extension.
*/
static void
fs_set_filename (GtkFileSelection *fsel, Workbook *wb)
{
char *name = g_strdup (wb->filename);
char *p = strrchr (name, '.');
if (p)
*p = '\0';
gtk_file_selection_set_filename (fsel, name);
g_free (name);
gtk_object_destroy (GTK_OBJECT (fsel));
g_list_free (importers);
}
/*
......@@ -279,7 +147,8 @@ can_try_save_to (WorkbookControlGUI *wbcg, const char *name)
}
static gboolean
do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view, const char *name)
do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view,
GnumFileSaver *fs, const char *name)
{
char *filename;
gboolean success = FALSE;
......@@ -290,7 +159,7 @@ do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view, const char *name)
return FALSE;
}
filename = gnum_file_saver_fix_file_name (wbcg->current_saver, name);
filename = gnum_file_saver_fix_file_name (fs, name);
if (!can_try_save_to (wbcg, filename)) {
g_free (filename);
return FALSE;
......@@ -299,7 +168,7 @@ do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view, const char *name)
wb_view_preferred_size (wb_view, GTK_WIDGET (wbcg->notebook)->allocation.width,
GTK_WIDGET (wbcg->notebook)->allocation.height);
if (gnum_file_saver_get_save_scope (wbcg->current_saver) == FILE_SAVE_SHEET &&
if (gnum_file_saver_get_save_scope (fs) == FILE_SAVE_SHEET &&
gnome_config_get_bool_with_default ("Gnumeric/File/AskBeforeSavingOneSheet=true", NULL)) {
gboolean accepted = TRUE;
GList *sheets;
......@@ -320,8 +189,7 @@ do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view, const char *name)
}
}
success = wb_view_save_as (wb_view, WORKBOOK_CONTROL (wbcg),
wbcg->current_saver, filename);
success = wb_view_save_as (wb_view, WORKBOOK_CONTROL (wbcg), fs, filename);
g_free (filename);
return success;
}
......@@ -329,125 +197,129 @@ do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view, const char *name)
gboolean
gui_file_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view)
{
GList *savers, *l;
GtkFileSelection *fsel;