Compilation: don't compile dialogs separately.
[gnumeric.git] / src / dialogs / dialog-zoom.c
blobf31df3a674ef857cb3e2ed5c597929ce21393b2e
1 /*
2 * dialog-zoom.c: Sets the magnification factor
4 * Author:
5 * Jody Goldberg <jody@gnome.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
25 #include <gnumeric.h>
26 #include "dialogs.h"
27 #include "help.h"
29 #include <gui-util.h>
30 #include <commands.h>
31 #include <workbook-control.h>
32 #include <workbook.h>
33 #include <wbc-gtk.h>
34 #include <sheet.h>
36 #include <gtk/gtk.h>
38 #define ZOOM_DIALOG_KEY "zoom-dialog"
39 #define ZOOM_DIALOG_FACTOR_KEY "zoom-dialog-factor"
41 enum {
42 COL_SHEET_NAME,
43 COL_SHEET_PTR
46 typedef struct {
47 WBCGtk *wbcg;
48 GtkWidget *dialog;
49 GtkWidget *entry;
50 GtkWidget *ok_button;
51 GtkWidget *cancel_button;
52 GtkRadioButton *custom;
53 GtkBuilder *gui;
55 GtkSpinButton *zoom;
56 GtkTreeView *sheet_list;
57 GtkListStore *sheet_list_model;
58 GtkTreeSelection *sheet_list_selection;
59 } ZoomState;
61 static const struct {
62 char const * const name;
63 gint const factor;
64 } buttons[] = {
65 { "radio_200", 200 },
66 { "radio_100", 100 },
67 { "radio_75", 75 },
68 { "radio_50", 50 },
69 { "radio_25", 25 },
70 { NULL, 0}
73 static void
74 cb_zoom_destroy (ZoomState *state)
76 if (state->sheet_list_model) {
77 g_object_unref (state->sheet_list_model);
78 state->sheet_list_model = NULL;
81 if (state->gui != NULL) {
82 g_object_unref (state->gui);
83 state->gui = NULL;
86 state->dialog = NULL;
87 g_free (state);
90 static void
91 cb_zoom_cancel_clicked (G_GNUC_UNUSED GtkWidget *button,
92 ZoomState *state)
94 gtk_widget_destroy (state->dialog);
95 return;
98 static void
99 radio_toggled (GtkToggleButton *togglebutton, ZoomState *state)
101 gint factor;
103 /* We are only interested in the new state */
104 if (gtk_toggle_button_get_active (togglebutton)) {
105 factor = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (togglebutton),
106 ZOOM_DIALOG_FACTOR_KEY));
107 gtk_spin_button_set_value (GTK_SPIN_BUTTON (state->zoom),
108 factor);
112 static void
113 focus_to_custom (GtkToggleButton *togglebutton, ZoomState *state)
115 if (gtk_toggle_button_get_active (togglebutton))
116 gtk_widget_grab_focus (GTK_WIDGET (&state->zoom->entry));
119 static gboolean
120 custom_selected (G_GNUC_UNUSED GtkWidget *widget,
121 G_GNUC_UNUSED GdkEventFocus *event, ZoomState *state)
123 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->custom), TRUE);
124 return FALSE;
127 static void
128 cb_zoom_ok_clicked (G_GNUC_UNUSED GtkWidget *button, ZoomState *state)
130 GSList *sheets = NULL;
131 GList *l, *tmp;
133 l = gtk_tree_selection_get_selected_rows (state->sheet_list_selection, NULL);
134 for (tmp = l; tmp; tmp = tmp->next) {
135 GtkTreePath *path = tmp->data;
136 GtkTreeIter iter;
138 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (state->sheet_list_model), &iter, path)) {
139 Sheet *this_sheet;
140 gtk_tree_model_get (GTK_TREE_MODEL (state->sheet_list_model),
141 &iter,
142 COL_SHEET_PTR, &this_sheet,
143 -1);
144 sheets = g_slist_prepend (sheets, this_sheet);
146 gtk_tree_path_free (path);
148 g_list_free (l);
150 if (sheets) {
151 WorkbookControl *wbc = GNM_WBC (state->wbcg);
152 double new_zoom = gtk_spin_button_get_value (state->zoom) / 100;
153 sheets = g_slist_reverse (sheets);
154 cmd_zoom (wbc, sheets, new_zoom);
157 gtk_widget_destroy (state->dialog);
160 void
161 dialog_zoom (WBCGtk *wbcg, Sheet *sheet)
163 ZoomState *state;
164 GSList *l, *sheets;
165 int i, row, cur_row;
166 gboolean is_custom = TRUE;
167 GtkRadioButton *radio;
168 GtkWidget *focus_target;
169 GtkBuilder *gui;
170 GtkTreeViewColumn *column;
172 g_return_if_fail (wbcg != NULL);
173 g_return_if_fail (sheet != NULL);
175 if (gnm_dialog_raise_if_exists (wbcg, ZOOM_DIALOG_KEY))
176 return;
177 gui = gnm_gtk_builder_load ("res:ui/dialog-zoom.ui", NULL, GO_CMD_CONTEXT (wbcg));
178 if (gui == NULL)
179 return;
181 state = g_new (ZoomState, 1);
182 state->wbcg = wbcg;
183 state->gui = gui;
184 state->dialog = go_gtk_builder_get_widget (state->gui, "Zoom");
185 g_return_if_fail (state->dialog != NULL);
187 /* Get the list of sheets */
188 state->sheet_list_model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
189 state->sheet_list = GTK_TREE_VIEW (go_gtk_builder_get_widget (state->gui, "sheet_list"));
190 gtk_tree_view_set_headers_visible (state->sheet_list, FALSE);
191 gtk_tree_view_set_model (state->sheet_list, GTK_TREE_MODEL (state->sheet_list_model));
192 state->sheet_list_selection = gtk_tree_view_get_selection (state->sheet_list);
193 gtk_tree_selection_set_mode (state->sheet_list_selection, GTK_SELECTION_MULTIPLE);
195 column = gtk_tree_view_column_new_with_attributes (_("Name"),
196 gtk_cell_renderer_text_new (),
197 "text", 0,
198 NULL);
199 gtk_tree_view_column_set_sort_column_id (column, COL_SHEET_NAME);
200 gtk_tree_view_append_column (GTK_TREE_VIEW (state->sheet_list), column);
202 sheets = workbook_sheets (wb_control_get_workbook (GNM_WBC (wbcg)));
203 cur_row = row = 0;
204 for (l = sheets; l; l = l->next) {
205 GtkTreeIter iter;
206 Sheet *this_sheet = l->data;
208 gtk_list_store_append (state->sheet_list_model, &iter);
209 gtk_list_store_set (state->sheet_list_model,
210 &iter,
211 COL_SHEET_NAME, this_sheet->name_unquoted,
212 COL_SHEET_PTR, this_sheet,
213 -1);
215 if (this_sheet == sheet)
216 cur_row = row;
217 row++;
219 g_slist_free (sheets);
222 GtkTreePath *path = gtk_tree_path_new_from_indices (cur_row, -1);
223 gtk_tree_view_set_cursor (state->sheet_list, path, NULL, FALSE);
224 gtk_tree_path_free (path);
227 state->zoom = GTK_SPIN_BUTTON (go_gtk_builder_get_widget (state->gui, "zoom"));
228 g_return_if_fail (state->zoom != NULL);
229 state->custom = GTK_RADIO_BUTTON (go_gtk_builder_get_widget (state->gui, "radio_custom"));
230 g_return_if_fail (state->custom != NULL);
231 focus_target = GTK_WIDGET (state->custom);
232 g_signal_connect (G_OBJECT (state->custom),
233 "clicked",
234 G_CALLBACK (focus_to_custom), (gpointer) state);
235 g_signal_connect (G_OBJECT (state->zoom),
236 "focus_in_event",
237 G_CALLBACK (custom_selected), state);
239 for (i = 0; buttons[i].name != NULL; i++) {
240 radio = GTK_RADIO_BUTTON (go_gtk_builder_get_widget (state->gui, buttons[i].name));
241 g_return_if_fail (radio != NULL);
243 g_object_set_data (G_OBJECT (radio), ZOOM_DIALOG_FACTOR_KEY,
244 GINT_TO_POINTER(buttons[i].factor));
246 g_signal_connect (G_OBJECT (radio),
247 "toggled",
248 G_CALLBACK (radio_toggled), state);
250 if (((int)(sheet->last_zoom_factor_used * 100. + .5)) == buttons[i].factor) {
251 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio), TRUE);
252 is_custom = FALSE;
253 focus_target = GTK_WIDGET (radio);
257 if (is_custom) {
258 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->custom), TRUE);
259 gtk_spin_button_set_value (state->zoom,
260 (int)(sheet->last_zoom_factor_used * 100. + .5));
262 state->ok_button = go_gtk_builder_get_widget (state->gui, "ok_button");
263 g_signal_connect (G_OBJECT (state->ok_button),
264 "clicked",
265 G_CALLBACK (cb_zoom_ok_clicked), state);
267 state->cancel_button = go_gtk_builder_get_widget (state->gui, "cancel_button");
268 g_signal_connect (G_OBJECT (state->cancel_button),
269 "clicked",
270 G_CALLBACK (cb_zoom_cancel_clicked), state);
272 gnm_editable_enters (GTK_WINDOW (state->dialog),
273 GTK_WIDGET (&state->zoom->entry));
275 gnm_init_help_button (
276 go_gtk_builder_get_widget (state->gui, "help_button"),
277 GNUMERIC_HELP_LINK_ZOOM);
279 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state->dialog), wbcg,
280 GNM_DIALOG_DESTROY_SHEET_REMOVED);
282 gnm_keyed_dialog (wbcg, GTK_WINDOW (state->dialog),
283 ZOOM_DIALOG_KEY);
284 g_object_set_data_full (G_OBJECT (state->dialog),
285 "state", state, (GDestroyNotify) cb_zoom_destroy);
286 wbc_gtk_attach_guru (state->wbcg, state->dialog);
287 gtk_widget_show (state->dialog);
289 gtk_widget_grab_focus (focus_target);