Compilation: don't compile dialogs separately.
[gnumeric.git] / src / dialogs / dialog-col-width.c
blobcf579b3da6b46023e998274003367ad53d22ef25
1 /*
2 * dialog-col-width.c: Sets the magnification factor
4 * Author:
5 * Andreas J. Guelzow <aguelzow@taliesin.ca>
7 * (c) Copyright 2002 Andreas J. Guelzow <aguelzow@taliesin.ca>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 #include <gnumeric-config.h>
23 #include <glib/gi18n-lib.h>
24 #include <gnumeric.h>
25 #include "dialogs.h"
26 #include "help.h"
28 #include <gui-util.h>
29 #include <commands.h>
30 #include <workbook-control.h>
31 #include <workbook.h>
32 #include <wbc-gtk.h>
33 #include <sheet.h>
34 #include <sheet-view.h>
35 #include <application.h>
36 #include <workbook-cmd-format.h>
38 #include <gtk/gtk.h>
40 #define COL_WIDTH_DIALOG_KEY "col-width-dialog"
42 typedef struct {
43 WBCGtk *wbcg;
44 Sheet *sheet;
45 SheetView *sv;
46 GtkWidget *dialog;
47 GtkWidget *ok_button;
48 GtkWidget *apply_button;
49 GtkWidget *cancel_button;
50 GtkWidget *default_check;
51 GtkWidget *description;
52 GtkWidget *points;
53 GtkSpinButton *spin;
55 gboolean set_default_value;
57 gint orig_value;
58 gboolean orig_is_default;
59 gboolean orig_some_default;
60 gboolean orig_all_equal;
61 gboolean adjusting;
62 } ColWidthState;
64 static void
65 dialog_col_width_update_points (ColWidthState *state)
67 gint value = gtk_spin_button_get_value_as_int (state->spin);
68 double size_points = value * 72./gnm_app_display_dpi_get (FALSE);
69 gchar *pts;
71 pts = g_strdup_printf ("%.2f",size_points);
72 gtk_label_set_text (GTK_LABEL (state->points), pts);
73 g_free (pts);
76 static void
77 dialog_col_width_button_sensitivity (ColWidthState *state)
79 gint value = gtk_spin_button_get_value_as_int (state->spin);
80 gboolean use_default = gtk_toggle_button_get_active
81 (GTK_TOGGLE_BUTTON (state->default_check));
82 gboolean changed_info;
84 if (state->set_default_value) {
85 changed_info = (state->orig_value != value);
86 } else {
87 changed_info = (((!state->orig_all_equal || (state->orig_value != value)
88 || state->orig_some_default) && !use_default)
89 || (use_default && !state->orig_is_default));
92 gtk_widget_set_sensitive (state->ok_button, changed_info);
93 gtk_widget_set_sensitive (state->apply_button, changed_info);
95 dialog_col_width_update_points (state);
98 static void
99 cb_dialog_col_width_cancel_clicked (G_GNUC_UNUSED GtkWidget *button,
100 ColWidthState *state)
102 gtk_widget_destroy (state->dialog);
103 return;
107 static gint
108 dialog_col_width_set_value (gint value, ColWidthState *state)
110 gint adj_value = value/state->sheet->last_zoom_factor_used + 0.5;
111 gtk_spin_button_set_value (state->spin, adj_value);
112 return adj_value;
115 static void
116 dialog_col_width_load_value (ColWidthState *state)
118 GSList *l;
119 gint value = 0;
120 state->orig_is_default = TRUE;
121 state->orig_some_default = FALSE;
122 state->orig_all_equal = TRUE;
124 state->adjusting = TRUE;
125 if (state->set_default_value) {
126 value = sheet_col_get_default_size_pixels (state->sheet);
127 } else {
128 for (l = state->sv->selections; l; l = l->next){
129 GnmRange *ss = l->data;
130 int col;
132 for (col = ss->start.col; col <= ss->end.col; col++){
133 ColRowInfo const *ri = sheet_col_get_info (state->sheet, col);
134 if (ri->hard_size)
135 state->orig_is_default = FALSE;
136 else
137 state->orig_some_default = TRUE;
138 if (value == 0)
139 value = ri->size_pixels;
140 else if (value != ri->size_pixels){
141 /* Values differ, so let the user enter the data */
142 state->orig_all_equal = FALSE;
146 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->default_check),
147 state->orig_is_default);
149 state->orig_value = dialog_col_width_set_value (value, state);
150 dialog_col_width_button_sensitivity (state);
151 state->adjusting = FALSE;
155 static void
156 cb_dialog_col_width_value_changed (G_GNUC_UNUSED GtkSpinButton *spinbutton,
157 ColWidthState *state)
159 if (!state->adjusting) {
160 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->default_check), FALSE);
161 dialog_col_width_button_sensitivity (state);
165 static void
166 cb_dialog_col_width_default_check_toggled (GtkToggleButton *togglebutton, ColWidthState *state)
168 if (!state->adjusting) {
169 if (gtk_toggle_button_get_active (togglebutton)) {
170 state->adjusting = TRUE;
171 dialog_col_width_set_value (sheet_col_get_default_size_pixels (state->sheet),
172 state);
173 state->adjusting = FALSE;
175 dialog_col_width_button_sensitivity (state);
179 static void
180 cb_dialog_col_width_apply_clicked (G_GNUC_UNUSED GtkWidget *button,
181 ColWidthState *state)
183 gint value = gtk_spin_button_get_value_as_int (state->spin);
184 int size_pixels = (int)(value * state->sheet->last_zoom_factor_used + 0.5);
185 gboolean use_default = gtk_toggle_button_get_active
186 (GTK_TOGGLE_BUTTON (state->default_check));
188 if (state->set_default_value) {
189 double points = value * 72./gnm_app_display_dpi_get (FALSE);
190 cmd_colrow_std_size (GNM_WBC (state->wbcg),
191 state->sheet, TRUE, points);
192 dialog_col_width_load_value (state);
193 } else {
194 if (use_default)
195 size_pixels = 0;
197 workbook_cmd_resize_selected_colrow (GNM_WBC (state->wbcg),
198 state->sheet, TRUE, size_pixels);
199 dialog_col_width_load_value (state);
202 return;
205 static void
206 cb_dialog_col_width_ok_clicked (GtkWidget *button, ColWidthState *state)
208 cb_dialog_col_width_apply_clicked (button, state);
209 gtk_widget_destroy (state->dialog);
210 return;
214 static void
215 dialog_col_width_set_mode (gboolean set_default, ColWidthState *state)
217 state->set_default_value = set_default;
219 if (set_default) {
220 gtk_widget_hide (state->default_check);
221 gtk_label_set_text (GTK_LABEL (state->description),
222 _("Set standard/default column width"));
223 } else {
224 char *text;
225 char *name = g_markup_escape_text (state->sheet->name_unquoted, -1);
226 gtk_widget_show (state->default_check);
227 text = g_strdup_printf (_("Set column width of selection on "
228 "<span style='italic' weight='bold'>%s</span>"),
229 name);
230 gtk_label_set_markup (GTK_LABEL (state->description), text);
231 g_free (text);
232 g_free (name);
237 void
238 dialog_col_width (WBCGtk *wbcg, gboolean use_default)
240 GtkBuilder *gui;
241 ColWidthState *state;
243 g_return_if_fail (wbcg != NULL);
245 if (gnm_dialog_raise_if_exists (wbcg, COL_WIDTH_DIALOG_KEY))
246 return;
247 gui = gnm_gtk_builder_load ("res:ui/col-width.ui", NULL, GO_CMD_CONTEXT (wbcg));
248 if (gui == NULL)
249 return;
251 state = g_new (ColWidthState, 1);
252 state->wbcg = wbcg;
253 state->sv = wb_control_cur_sheet_view (GNM_WBC (wbcg));
254 state->sheet = sv_sheet (state->sv);
255 state->adjusting = FALSE;
256 state->dialog = go_gtk_builder_get_widget (gui, "dialog");
258 state->description = GTK_WIDGET (go_gtk_builder_get_widget (gui, "description"));
259 state->points = GTK_WIDGET (go_gtk_builder_get_widget (gui, "pts-label"));
261 state->spin = GTK_SPIN_BUTTON (go_gtk_builder_get_widget (gui, "spin"));
262 gtk_adjustment_set_lower (gtk_spin_button_get_adjustment (state->spin),
263 GNM_COL_MARGIN + GNM_COL_MARGIN);
264 g_signal_connect (G_OBJECT (state->spin),
265 "value-changed",
266 G_CALLBACK (cb_dialog_col_width_value_changed), state);
268 state->default_check = GTK_WIDGET (go_gtk_builder_get_widget (gui, "default_check"));
269 g_signal_connect (G_OBJECT (state->default_check),
270 "clicked",
271 G_CALLBACK (cb_dialog_col_width_default_check_toggled), state);
273 state->ok_button = go_gtk_builder_get_widget (gui, "ok_button");
274 g_signal_connect (G_OBJECT (state->ok_button),
275 "clicked",
276 G_CALLBACK (cb_dialog_col_width_ok_clicked), state);
277 state->apply_button = go_gtk_builder_get_widget (gui, "apply_button");
278 g_signal_connect (G_OBJECT (state->apply_button),
279 "clicked",
280 G_CALLBACK (cb_dialog_col_width_apply_clicked), state);
282 state->cancel_button = go_gtk_builder_get_widget (gui, "cancel_button");
283 g_signal_connect (G_OBJECT (state->cancel_button),
284 "clicked",
285 G_CALLBACK (cb_dialog_col_width_cancel_clicked), state);
287 gnm_init_help_button (
288 go_gtk_builder_get_widget (gui, "help_button"),
289 GNUMERIC_HELP_LINK_COL_WIDTH);
291 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state->dialog),
292 state->wbcg,
293 GNM_DIALOG_DESTROY_CURRENT_SHEET_REMOVED);
294 dialog_col_width_set_mode (use_default, state);
295 dialog_col_width_load_value (state);
297 wbc_gtk_attach_guru (state->wbcg, state->dialog);
298 g_object_set_data_full (G_OBJECT (state->dialog),
299 "state", state, (GDestroyNotify)g_free);
301 gnm_keyed_dialog (wbcg, GTK_WINDOW (state->dialog),
302 COL_WIDTH_DIALOG_KEY);
303 gtk_widget_show (state->dialog);
304 g_object_unref (gui);