dialog-autosave.c 5.44 KB
Newer Older
1 2 3
/*
 * dialog-autosave.c:
 *
4
 * Authors:
5
 *        Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
6
 *        Miguel de Icaza (miguel@kernel.org)
7
 *
8
 * (C) Copyright 2000, 2001 by Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
9
 **/
10 11
#include <gnumeric-config.h>
#include <gnumeric.h>
12 13
#include "dialogs.h"

14 15 16 17
#include <workbook.h>
#include <workbook-control-gui-priv.h>
#include <gui-util.h>

Jody Goldberg's avatar
fix.  
Jody Goldberg committed
18 19
#include <libgnome/gnome-i18n.h>
#include <glade/glade.h>
20 21

typedef struct {
22 23
	GladeXML  *gui;
	GtkWidget *dialog;
24 25
        GtkWidget *minutes_entry;
        GtkWidget *prompt_cb;
26 27 28 29 30 31
	GtkWidget *autosave_on_off;
	GtkWidget *ok_button;
	GtkWidget *cancel_button;
	GtkWidget *help_button;
	Workbook  *wb;
	WorkbookControlGUI  *wbcg;
32 33
} autosave_t;

34 35
#define AUTOSAVE_KEY            "autosave-setup-dialog"

36
static void
37
autosave_set_sensitivity (GtkWidget *widget, autosave_t *state)
38
{
39 40 41 42 43 44 45
        gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->autosave_on_off));
	gint minutes;
	gint minutes_valid = entry_to_int (GTK_ENTRY (state->minutes_entry), &minutes, FALSE);

	gtk_widget_set_sensitive (state->minutes_entry, active);
	gtk_widget_set_sensitive (state->prompt_cb, active);

Jody Goldberg's avatar
Jody Goldberg committed
46
	gtk_widget_set_sensitive (state->ok_button, !active ||
47
				  ((minutes_valid == 0) && (minutes > 0)));
Jody Goldberg's avatar
Jody Goldberg committed
48

49 50
}

51
gboolean
52
dialog_autosave_prompt (WorkbookControlGUI *wbcg)
53
{
54 55 56 57 58 59 60
	gint      result;
	GtkWidget *dialog;

	dialog = gtk_message_dialog_new (wbcg_toplevel (wbcg),
					 GTK_DIALOG_DESTROY_WITH_PARENT,
					 GTK_MESSAGE_QUESTION,
					 GTK_BUTTONS_YES_NO,
Jody Goldberg's avatar
Jody Goldberg committed
61 62
					 _("Do you want to save the workbook %s ?"),
					 workbook_get_filename (wb_control_workbook
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
								(WORKBOOK_CONTROL (wbcg))));
	result = gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);

	return  result == GTK_RESPONSE_YES;
}



/**
 * dialog_autosave:
 * @window:
 * @state:
 *
 * Destroy the dialog and associated data structures.
 *
 **/
static gboolean
dialog_autosave_destroy (GtkObject *w, autosave_t  *state)
{
	g_return_val_if_fail (w != NULL, FALSE);
	g_return_val_if_fail (state != NULL, FALSE);

	if (state->gui != NULL) {
		g_object_unref (G_OBJECT (state->gui));
		state->gui = NULL;
89
	}
90

91 92 93
	state->dialog = NULL;

	g_free (state);
94

95
	return FALSE;
96 97
}

98 99 100 101 102 103 104 105 106
/**
 * cb_autosave_cancel:
 * @button:
 * @state:
 *
 * Close (destroy) the dialog
 **/
static void
cb_autosave_cancel (GtkWidget *button, autosave_t *state)
107
{
108 109 110
	gtk_widget_destroy (state->dialog);
	return;
}
111

112 113 114 115 116 117 118 119 120 121 122
/**
 * cb_autosave_help:
 * @button:
 * @state:
 **/
static void
cb_autosave_help (GtkWidget *button, autosave_t *state)
{
	gnumeric_help_display ("autosave.html");
	return;
}
123

124 125 126 127 128 129 130 131 132 133
/**
 * cb_autosave_ok:
 * @button:
 * @state:
 **/
static void
cb_autosave_ok (GtkWidget *button, autosave_t *state)
{
		if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->autosave_on_off))) {
			int minutes;
Jody Goldberg's avatar
Jody Goldberg committed
134
			int minutes_valid = entry_to_int (GTK_ENTRY (state->minutes_entry),
135
							  &minutes, TRUE);
136

137
			g_return_if_fail (minutes_valid == 0); /* Why is ok active? */
Jody Goldberg's avatar
Jody Goldberg committed
138 139

		        wbcg_autosave_set (state->wbcg, minutes,
140 141 142 143 144 145
					   gtk_toggle_button_get_active (
						   GTK_TOGGLE_BUTTON (state->prompt_cb)));
		} else
			wbcg_autosave_set (state->wbcg, 0, FALSE);
		gtk_widget_destroy (state->dialog);
}
146

147 148 149 150
void
dialog_autosave (WorkbookControlGUI *wbcg)
{
	autosave_t *state;
151

152
	g_return_if_fail (wbcg != NULL);
153

154 155
	if (gnumeric_dialog_raise_if_exists (wbcg, AUTOSAVE_KEY))
		return;
156

157 158 159
	state = g_new (autosave_t, 1);
	state->wbcg  = wbcg;
	state->wb   = wb_control_workbook (WORKBOOK_CONTROL (wbcg));
160

161 162 163 164
	state->gui = gnumeric_glade_xml_new (wbcg, "autosave.glade");
        if (state->gui == NULL) {
		g_free (state);
                return;
165
	}
166

167 168 169 170 171 172 173 174
	state->dialog = glade_xml_get_widget (state->gui, "AutoSave");
	state->minutes_entry = glade_xml_get_widget (state->gui, "minutes");
	state->prompt_cb = glade_xml_get_widget (state->gui, "prompt_on_off");
	state->autosave_on_off = glade_xml_get_widget (state->gui, "autosave_on_off");
	state->ok_button = glade_xml_get_widget (state->gui, "button1");
	state->cancel_button = glade_xml_get_widget (state->gui, "button2");
	state->help_button = glade_xml_get_widget (state->gui, "button3");

Jody Goldberg's avatar
Jody Goldberg committed
175
	if (!state->dialog || !state->minutes_entry || !state->prompt_cb ||
176 177 178 179 180
	    !state->autosave_on_off) {
		gnumeric_notice (wbcg, GTK_MESSAGE_ERROR,
				 _("Could not create the autosave dialog."));
		g_free (state);
		return;
181
	}
Jody Goldberg's avatar
Jody Goldberg committed
182

183 184 185
	float_to_entry (GTK_ENTRY (state->minutes_entry), wbcg->autosave_minutes);

	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
Jody Goldberg's avatar
Jody Goldberg committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
				  state->minutes_entry);

	g_signal_connect (G_OBJECT (state->autosave_on_off),
		"toggled",
		G_CALLBACK (autosave_set_sensitivity), state);
	g_signal_connect (G_OBJECT (state->minutes_entry),
		"changed",
		G_CALLBACK (autosave_set_sensitivity), state);
	g_signal_connect (G_OBJECT (state->ok_button),
		"clicked",
		G_CALLBACK (cb_autosave_ok), state);
	g_signal_connect (G_OBJECT (state->cancel_button),
		"clicked",
		G_CALLBACK (cb_autosave_cancel), state);
	g_signal_connect (G_OBJECT (state->help_button),
		"clicked",
		G_CALLBACK (cb_autosave_help), state);
	g_signal_connect (G_OBJECT (state->dialog),
		"destroy",
		G_CALLBACK (dialog_autosave_destroy), state);
206 207 208 209 210

	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->autosave_on_off),
				      wbcg->autosave);
	gtk_toggle_button_set_active ((GtkToggleButton *) state->prompt_cb,
				      wbcg->autosave_prompt);
211

212 213 214 215
	autosave_set_sensitivity (NULL, state);
	gnumeric_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
			       AUTOSAVE_KEY);
	gtk_widget_show (state->dialog);
216 217

}
218