dialog-zoom.c 4.06 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1 2
/**
 * dialog-zoom.c:  Sets the magnification factor
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
3 4
 *
 * Author:
Jody Goldberg's avatar
Jody Goldberg committed
5
 *        Jody Goldberg <jgoldberg@home.com>
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
6
 *
Jody Goldberg's avatar
Jody Goldberg committed
7
 **/
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
8 9
#include <config.h>
#include <gnome.h>
Jody Goldberg's avatar
Jody Goldberg committed
10
#include <glade/glade.h>
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
11 12
#include "gnumeric.h"
#include "gnumeric-util.h"
Jody Goldberg's avatar
Jody Goldberg committed
13
#include "gnumeric-sheet.h"
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
14 15
#include "dialogs.h"

Michael Meeks's avatar
Michael Meeks committed
16 17

#define NUM_RADIO_BUTTONS	5
18 19
#define GLADE_FILE "dialog-zoom.glade"

Jody Goldberg's avatar
Jody Goldberg committed
20
typedef struct {
Michael Meeks's avatar
Michael Meeks committed
21 22
	int             factor;
	GtkSpinButton  *zoom;
Jody Goldberg's avatar
Jody Goldberg committed
23 24 25 26
} radio_cb_data;

static void
radio_toggled (GtkToggleButton *togglebutton,
Michael Meeks's avatar
Michael Meeks committed
27
	       radio_cb_data   *dat)
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
28
{
Jody Goldberg's avatar
Jody Goldberg committed
29
	/* We are only interested in the new state */
Michael Meeks's avatar
Michael Meeks committed
30
	if (gtk_toggle_button_get_active (togglebutton))
Jody Goldberg's avatar
Jody Goldberg committed
31 32 33
		gtk_spin_button_set_value (GTK_SPIN_BUTTON (dat->zoom),
					   dat->factor);
}
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
34

Jody Goldberg's avatar
Jody Goldberg committed
35
static gboolean
Michael Meeks's avatar
Michael Meeks committed
36 37
custom_selected (GtkWidget *widget, GdkEventFocus   *event,
		 GtkWidget *custom_button)
Jody Goldberg's avatar
Jody Goldberg committed
38 39 40 41
{
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (custom_button), TRUE);
	return FALSE;
}
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
42

Jody Goldberg's avatar
Jody Goldberg committed
43 44 45 46
static void
dialog_zoom_impl (Workbook *wb, Sheet *cur_sheet, GladeXML  *gui)
{
	static struct {
Michael Meeks's avatar
Michael Meeks committed
47 48
		char const * const name;
		float const factor;
Jody Goldberg's avatar
Jody Goldberg committed
49
	} buttons[NUM_RADIO_BUTTONS] = {
Michael Meeks's avatar
Michael Meeks committed
50 51 52 53 54
		{ "radio_200", 2. },
		{ "radio_100", 1. },
		{ "radio_75", .75 },
		{ "radio_50", .50 },
		{ "radio_25", .25 },
Jody Goldberg's avatar
Jody Goldberg committed
55 56
	};
	radio_cb_data	cb_data[NUM_RADIO_BUTTONS];
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
57

Jody Goldberg's avatar
Jody Goldberg committed
58 59
	GtkCList *list;
	GtkWidget *dialog;
Michael Meeks's avatar
Michael Meeks committed
60
	GtkRadioButton *radio, *custom;
Jody Goldberg's avatar
Jody Goldberg committed
61 62
	GtkSpinButton *zoom;
	GList *l, *sheets;
Michael Meeks's avatar
Michael Meeks committed
63
	int i, res, cur_row;
64
	gboolean is_custom = TRUE;
Jody Goldberg's avatar
Jody Goldberg committed
65 66 67 68 69

	list = GTK_CLIST (glade_xml_get_widget (gui, "sheet_list"));
	g_return_if_fail (list);
	zoom  = GTK_SPIN_BUTTON (glade_xml_get_widget (gui, "zoom"));
	g_return_if_fail (zoom != NULL);
Michael Meeks's avatar
Michael Meeks committed
70 71
	custom = GTK_RADIO_BUTTON (glade_xml_get_widget (gui, "radio_custom"));
	g_return_if_fail (custom != NULL);
Jody Goldberg's avatar
Jody Goldberg committed
72 73
	gtk_signal_connect (GTK_OBJECT (zoom), "focus_in_event",
			    GTK_SIGNAL_FUNC (custom_selected),
Michael Meeks's avatar
Michael Meeks committed
74
			    custom);
Jody Goldberg's avatar
Jody Goldberg committed
75 76 77 78

	for (i = NUM_RADIO_BUTTONS; --i >= 0; ) {
		radio  = GTK_RADIO_BUTTON (glade_xml_get_widget (gui, buttons[i].name));
		g_return_if_fail (radio != NULL);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
79

Jody Goldberg's avatar
Jody Goldberg committed
80
		cb_data[i].factor = (int)(buttons[i].factor * 100.);
Michael Meeks's avatar
Michael Meeks committed
81
		cb_data[i].zoom   = zoom;
Jody Goldberg's avatar
Jody Goldberg committed
82 83 84 85

		gtk_signal_connect (GTK_OBJECT (radio), "toggled",
				    GTK_SIGNAL_FUNC (radio_toggled),
				    &(cb_data[i]));
86 87 88 89 90

		if (cur_sheet->last_zoom_factor_used == buttons[i].factor) {
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio), TRUE);
			is_custom = FALSE;
		}
Jody Goldberg's avatar
Jody Goldberg committed
91 92
	}

93 94 95 96 97
	if (is_custom) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (custom), TRUE);
		gtk_spin_button_set_value (zoom, 
					   (int)(cur_sheet->last_zoom_factor_used * 100. + .5));
	}
Jody Goldberg's avatar
Jody Goldberg committed
98

99
	/* Get the list of sheets */
Jody Goldberg's avatar
Jody Goldberg committed
100
	gtk_clist_freeze (list);
101

Jody Goldberg's avatar
Jody Goldberg committed
102
	sheets = workbook_sheets (wb);
103
	cur_row = 0;
Michael Meeks's avatar
Michael Meeks committed
104
	for (l = sheets; l; l = l->next) {
Jody Goldberg's avatar
Jody Goldberg committed
105 106 107
		Sheet *sheet = l->data;
		int const row = gtk_clist_append (list, &sheet->name);

Michael Meeks's avatar
Michael Meeks committed
108 109
		if (sheet == cur_sheet)
			cur_row = row;
110
		gtk_clist_set_row_data (list, row, sheet);
Jody Goldberg's avatar
Jody Goldberg committed
111 112 113
	}
	g_list_free (sheets);

Michael Meeks's avatar
Michael Meeks committed
114 115
	gtk_clist_select_row (list, cur_row, 0);
	gtk_clist_moveto     (list, cur_row, 0, .5, 0.0);
116
	gtk_clist_thaw (list);
Michael Meeks's avatar
Michael Meeks committed
117

Jody Goldberg's avatar
Jody Goldberg committed
118 119
	dialog = glade_xml_get_widget (gui, "Zoom");
	if (dialog == NULL) {
120
		printf ("Corrupt file " GLADE_FILE "\n");
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
121 122 123
		return;
	}

Jody Goldberg's avatar
Jody Goldberg committed
124 125 126 127
	/* Make the dialog a child of the application so that it will iconify */
	gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (wb->toplevel));

	/* Bring up the dialog */
128
	res = gnome_dialog_run (GNOME_DIALOG (dialog));
Jody Goldberg's avatar
Jody Goldberg committed
129
	if (res == 0) {
130
		float const new_zoom = gtk_spin_button_get_value_as_int(zoom) / 100.;
Jody Goldberg's avatar
Jody Goldberg committed
131 132
		for (l = list->selection; l != NULL ; l = l->next) {
			Sheet * s = gtk_clist_get_row_data (list, GPOINTER_TO_INT(l->data));
133
			sheet_set_zoom_factor (s, new_zoom);
Jody Goldberg's avatar
Jody Goldberg committed
134
		}
135 136
	}

Jody Goldberg's avatar
Jody Goldberg committed
137
	/* If the user closed the dialog with prejudice, its already destroyed */
138 139
	if (res >= 0)
		gnome_dialog_close (GNOME_DIALOG (dialog));
Jody Goldberg's avatar
Jody Goldberg committed
140 141 142 143 144 145 146 147 148 149 150
}

/* Wrapper to ensure the libglade object gets removed on error */
void
dialog_zoom (Workbook *wb, Sheet *sheet)
{
	GladeXML  *gui;

	g_return_if_fail (wb != NULL);
	g_return_if_fail (sheet != NULL);

151
	gui = glade_xml_new (GNUMERIC_GLADEDIR "/" GLADE_FILE , NULL);
Jody Goldberg's avatar
Jody Goldberg committed
152
	if (!gui) {
153
		printf ("Could not find " GLADE_FILE "\n");
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
154 155 156
		return;
	}

Jody Goldberg's avatar
Jody Goldberg committed
157 158 159
	dialog_zoom_impl (wb, sheet, gui);
	
	gtk_object_unref (GTK_OBJECT (gui));
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
160
}