GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / dialogs / dialog-zoom.c
blob8697e5ee4afde84cfe6377b45d9352724599f21c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /**
3 * dialog-zoom.c: Sets the magnification factor
5 * Author:
6 * Jody Goldberg <jody@gnome.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) version 3.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 * USA
24 #include <gnumeric-config.h>
25 #include <glib/gi18n-lib.h>
26 #include <gnumeric.h>
27 #include "dialogs.h"
28 #include "help.h"
30 #include <gui-util.h>
31 #include <commands.h>
32 #include <workbook-control.h>
33 #include <workbook.h>
34 #include <wbc-gtk.h>
35 #include <sheet.h>
37 #include <gtk/gtk.h>
39 #define ZOOM_DIALOG_KEY "zoom-dialog"
40 #define ZOOM_DIALOG_FACTOR_KEY "zoom-dialog-factor"
42 enum {
43 COL_SHEET_NAME,
44 COL_SHEET_PTR
47 typedef struct {
48 WBCGtk *wbcg;
49 GtkWidget *dialog;
50 GtkWidget *entry;
51 GtkWidget *ok_button;
52 GtkWidget *cancel_button;
53 GtkRadioButton *custom;
54 GtkBuilder *gui;
56 GtkSpinButton *zoom;
57 GtkTreeView *sheet_list;
58 GtkListStore *sheet_list_model;
59 GtkTreeSelection *sheet_list_selection;
60 } ZoomState;
62 static const struct {
63 char const * const name;
64 gint const factor;
65 } buttons[] = {
66 { "radio_200", 200 },
67 { "radio_100", 100 },
68 { "radio_75", 75 },
69 { "radio_50", 50 },
70 { "radio_25", 25 },
71 { NULL, 0}
74 static void
75 cb_zoom_destroy (ZoomState *state)
77 if (state->sheet_list_model) {
78 g_object_unref (state->sheet_list_model);
79 state->sheet_list_model = NULL;
82 if (state->gui != NULL) {
83 g_object_unref (state->gui);
84 state->gui = NULL;
87 state->dialog = NULL;
88 g_free (state);
91 static void
92 cb_zoom_cancel_clicked (G_GNUC_UNUSED GtkWidget *button,
93 ZoomState *state)
95 gtk_widget_destroy (state->dialog);
96 return;
99 static void
100 radio_toggled (GtkToggleButton *togglebutton, ZoomState *state)
102 gint factor;
104 /* We are only interested in the new state */
105 if (gtk_toggle_button_get_active (togglebutton)) {
106 factor = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (togglebutton),
107 ZOOM_DIALOG_FACTOR_KEY));
108 gtk_spin_button_set_value (GTK_SPIN_BUTTON (state->zoom),
109 factor);
113 static void
114 focus_to_custom (GtkToggleButton *togglebutton, ZoomState *state)
116 if (gtk_toggle_button_get_active (togglebutton))
117 gtk_widget_grab_focus (GTK_WIDGET (&state->zoom->entry));
120 static gboolean
121 custom_selected (G_GNUC_UNUSED GtkWidget *widget,
122 G_GNUC_UNUSED GdkEventFocus *event, ZoomState *state)
124 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->custom), TRUE);
125 return FALSE;
128 static void
129 cb_zoom_ok_clicked (G_GNUC_UNUSED GtkWidget *button, ZoomState *state)
131 GSList *sheets = NULL;
132 GList *l, *tmp;
134 l = gtk_tree_selection_get_selected_rows (state->sheet_list_selection, NULL);
135 for (tmp = l; tmp; tmp = tmp->next) {
136 GtkTreePath *path = tmp->data;
137 GtkTreeIter iter;
139 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (state->sheet_list_model), &iter, path)) {
140 Sheet *this_sheet;
141 gtk_tree_model_get (GTK_TREE_MODEL (state->sheet_list_model),
142 &iter,
143 COL_SHEET_PTR, &this_sheet,
144 -1);
145 sheets = g_slist_prepend (sheets, this_sheet);
147 gtk_tree_path_free (path);
149 g_list_free (l);
151 if (sheets) {
152 WorkbookControl *wbc = GNM_WBC (state->wbcg);
153 double new_zoom = gtk_spin_button_get_value (state->zoom) / 100;
154 sheets = g_slist_reverse (sheets);
155 cmd_zoom (wbc, sheets, new_zoom);
158 gtk_widget_destroy (state->dialog);
161 void
162 dialog_zoom (WBCGtk *wbcg, Sheet *sheet)
164 ZoomState *state;
165 GSList *l, *sheets;
166 int i, row, cur_row;
167 gboolean is_custom = TRUE;
168 GtkRadioButton *radio;
169 GtkWidget *focus_target;
170 GtkBuilder *gui;
171 GtkTreeViewColumn *column;
173 g_return_if_fail (wbcg != NULL);
174 g_return_if_fail (sheet != NULL);
176 if (gnm_dialog_raise_if_exists (wbcg, ZOOM_DIALOG_KEY))
177 return;
178 gui = gnm_gtk_builder_load ("res:ui/dialog-zoom.ui", NULL, GO_CMD_CONTEXT (wbcg));
179 if (gui == NULL)
180 return;
182 state = g_new (ZoomState, 1);
183 state->wbcg = wbcg;
184 state->gui = gui;
185 state->dialog = go_gtk_builder_get_widget (state->gui, "Zoom");
186 g_return_if_fail (state->dialog != NULL);
188 /* Get the list of sheets */
189 state->sheet_list_model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
190 state->sheet_list = GTK_TREE_VIEW (go_gtk_builder_get_widget (state->gui, "sheet_list"));
191 gtk_tree_view_set_headers_visible (state->sheet_list, FALSE);
192 gtk_tree_view_set_model (state->sheet_list, GTK_TREE_MODEL (state->sheet_list_model));
193 state->sheet_list_selection = gtk_tree_view_get_selection (state->sheet_list);
194 gtk_tree_selection_set_mode (state->sheet_list_selection, GTK_SELECTION_MULTIPLE);
196 column = gtk_tree_view_column_new_with_attributes (_("Name"),
197 gtk_cell_renderer_text_new (),
198 "text", 0,
199 NULL);
200 gtk_tree_view_column_set_sort_column_id (column, COL_SHEET_NAME);
201 gtk_tree_view_append_column (GTK_TREE_VIEW (state->sheet_list), column);
203 sheets = workbook_sheets (wb_control_get_workbook (GNM_WBC (wbcg)));
204 cur_row = row = 0;
205 for (l = sheets; l; l = l->next) {
206 GtkTreeIter iter;
207 Sheet *this_sheet = l->data;
209 gtk_list_store_append (state->sheet_list_model, &iter);
210 gtk_list_store_set (state->sheet_list_model,
211 &iter,
212 COL_SHEET_NAME, this_sheet->name_unquoted,
213 COL_SHEET_PTR, this_sheet,
214 -1);
216 if (this_sheet == sheet)
217 cur_row = row;
218 row++;
220 g_slist_free (sheets);
223 GtkTreePath *path = gtk_tree_path_new_from_indices (cur_row, -1);
224 gtk_tree_view_set_cursor (state->sheet_list, path, NULL, FALSE);
225 gtk_tree_path_free (path);
228 state->zoom = GTK_SPIN_BUTTON (go_gtk_builder_get_widget (state->gui, "zoom"));
229 g_return_if_fail (state->zoom != NULL);
230 state->custom = GTK_RADIO_BUTTON (go_gtk_builder_get_widget (state->gui, "radio_custom"));
231 g_return_if_fail (state->custom != NULL);
232 focus_target = GTK_WIDGET (state->custom);
233 g_signal_connect (G_OBJECT (state->custom),
234 "clicked",
235 G_CALLBACK (focus_to_custom), (gpointer) state);
236 g_signal_connect (G_OBJECT (state->zoom),
237 "focus_in_event",
238 G_CALLBACK (custom_selected), state);
240 for (i = 0; buttons[i].name != NULL; i++) {
241 radio = GTK_RADIO_BUTTON (go_gtk_builder_get_widget (state->gui, buttons[i].name));
242 g_return_if_fail (radio != NULL);
244 g_object_set_data (G_OBJECT (radio), ZOOM_DIALOG_FACTOR_KEY,
245 GINT_TO_POINTER(buttons[i].factor));
247 g_signal_connect (G_OBJECT (radio),
248 "toggled",
249 G_CALLBACK (radio_toggled), state);
251 if (((int)(sheet->last_zoom_factor_used * 100. + .5)) == buttons[i].factor) {
252 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio), TRUE);
253 is_custom = FALSE;
254 focus_target = GTK_WIDGET (radio);
258 if (is_custom) {
259 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->custom), TRUE);
260 gtk_spin_button_set_value (state->zoom,
261 (int)(sheet->last_zoom_factor_used * 100. + .5));
263 state->ok_button = go_gtk_builder_get_widget (state->gui, "ok_button");
264 g_signal_connect (G_OBJECT (state->ok_button),
265 "clicked",
266 G_CALLBACK (cb_zoom_ok_clicked), state);
268 state->cancel_button = go_gtk_builder_get_widget (state->gui, "cancel_button");
269 g_signal_connect (G_OBJECT (state->cancel_button),
270 "clicked",
271 G_CALLBACK (cb_zoom_cancel_clicked), state);
273 gnm_editable_enters (GTK_WINDOW (state->dialog),
274 GTK_WIDGET (&state->zoom->entry));
276 gnm_init_help_button (
277 go_gtk_builder_get_widget (state->gui, "help_button"),
278 GNUMERIC_HELP_LINK_ZOOM);
280 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state->dialog), wbcg,
281 GNM_DIALOG_DESTROY_SHEET_REMOVED);
283 gnm_keyed_dialog (wbcg, GTK_WINDOW (state->dialog),
284 ZOOM_DIALOG_KEY);
285 g_object_set_data_full (G_OBJECT (state->dialog),
286 "state", state, (GDestroyNotify) cb_zoom_destroy);
287 wbc_gtk_attach_guru (state->wbcg, state->dialog);
288 gtk_widget_show (state->dialog);
290 gtk_widget_grab_focus (focus_target);