2 * dialog-zoom.c: Sets the magnification factor
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
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
31 #include <workbook-control.h>
38 #define ZOOM_DIALOG_KEY "zoom-dialog"
39 #define ZOOM_DIALOG_FACTOR_KEY "zoom-dialog-factor"
51 GtkWidget
*cancel_button
;
52 GtkRadioButton
*custom
;
56 GtkTreeView
*sheet_list
;
57 GtkListStore
*sheet_list_model
;
58 GtkTreeSelection
*sheet_list_selection
;
62 char const * const name
;
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
);
91 cb_zoom_cancel_clicked (G_GNUC_UNUSED GtkWidget
*button
,
94 gtk_widget_destroy (state
->dialog
);
99 radio_toggled (GtkToggleButton
*togglebutton
, ZoomState
*state
)
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
),
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
));
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
);
128 cb_zoom_ok_clicked (G_GNUC_UNUSED GtkWidget
*button
, ZoomState
*state
)
130 GSList
*sheets
= NULL
;
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
;
138 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (state
->sheet_list_model
), &iter
, path
)) {
140 gtk_tree_model_get (GTK_TREE_MODEL (state
->sheet_list_model
),
142 COL_SHEET_PTR
, &this_sheet
,
144 sheets
= g_slist_prepend (sheets
, this_sheet
);
146 gtk_tree_path_free (path
);
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
);
161 dialog_zoom (WBCGtk
*wbcg
, Sheet
*sheet
)
166 gboolean is_custom
= TRUE
;
167 GtkRadioButton
*radio
;
168 GtkWidget
*focus_target
;
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
))
177 gui
= gnm_gtk_builder_load ("res:ui/dialog-zoom.ui", NULL
, GO_CMD_CONTEXT (wbcg
));
181 state
= g_new (ZoomState
, 1);
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 (),
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
)));
204 for (l
= sheets
; l
; l
= l
->next
) {
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
,
211 COL_SHEET_NAME
, this_sheet
->name_unquoted
,
212 COL_SHEET_PTR
, this_sheet
,
215 if (this_sheet
== sheet
)
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
),
234 G_CALLBACK (focus_to_custom
), (gpointer
) state
);
235 g_signal_connect (G_OBJECT (state
->zoom
),
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
),
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
);
253 focus_target
= GTK_WIDGET (radio
);
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
),
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
),
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
),
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
);