ssdiff: move comparison engine into its own file.
[gnumeric.git] / src / dialogs / dialog-workbook-attr.c
blobb5ebf5e41eb3494b881a3c65ec1f3beb0b28ac5d
1 /**
2 * dialog-workbook-attr.c: Implements a dialog to set workbook attributes.
4 * Author:
5 * JP Rosevear <jpr@arcavia.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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, see <https://www.gnu.org/licenses/>.
21 #include <gnumeric-config.h>
22 #include <glib/gi18n-lib.h>
23 #include <gnumeric.h>
24 #include "dialogs.h"
25 #include "help.h"
27 #include <gui-util.h>
28 #include <workbook-view.h>
29 #include <workbook.h>
30 #include <wbc-gtk.h>
31 #include <workbook-priv.h>
32 #include <gtk/gtk.h>
34 #define WORKBOOK_ATTRIBUTE_KEY "workbook-attribute-dialog"
36 enum {
37 ITEM_ICON,
38 ITEM_NAME,
39 PAGE_NUMBER,
40 NUM_COLUMNS
43 typedef struct {
44 GtkBuilder *gui;
45 GtkWidget *dialog;
46 GtkWidget *notebook;
47 GtkWidget *ok_button;
48 GtkWidget *apply_button;
49 gboolean destroying;
51 Workbook *wb;
52 WorkbookView *wbv;
53 WBCGtk *wbcg;
55 GtkTreeStore *store;
56 GtkTreeView *tview;
57 } AttrState;
59 /*****************************************************************************/
60 /* Some utility routines shared by all pages */
62 /* Default to the 'View' page but remember which page we were on between
63 * invocations */
64 static int attr_dialog_page = 0;
66 /*****************************************************************************/
68 static void
69 cb_widget_changed (GtkWidget *widget, AttrState *state)
71 char const *key;
73 key = g_object_get_data (G_OBJECT (widget), "GNUMERIC:VIEWPROPERTY");
74 g_object_set (G_OBJECT (state->wbv),
75 key, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)),
76 NULL);
79 static void
80 cb_attr_dialog_dialog_close (G_GNUC_UNUSED GtkWidget *button,
81 AttrState *state)
83 state->destroying = TRUE;
84 gtk_widget_destroy (state->dialog);
87 static void
88 cb_attr_dialog_dialog_destroy (AttrState *state)
90 if (state->gui != NULL) {
91 g_object_unref (state->gui);
92 state->gui = NULL;
95 state->dialog = NULL;
96 g_free (state);
99 /*****************************************************************************/
101 static void
102 attr_dialog_init_toggle (AttrState *state, char const *name, char const *key)
104 GtkWidget *w = go_gtk_builder_get_widget (state->gui, name);
105 gboolean val = FALSE;
107 g_object_get (G_OBJECT (state->wbv), key, &val, NULL);
108 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), val);
110 g_signal_connect (G_OBJECT (w),
111 "toggled",
112 G_CALLBACK (cb_widget_changed), state);
113 g_object_set_data (G_OBJECT (w), "GNUMERIC:VIEWPROPERTY", (gpointer) key);
114 return;
117 static void
118 attr_dialog_init_widget_page (AttrState *state)
120 attr_dialog_init_toggle
121 (state,
122 "WorkbookView::show_horizontal_scrollbar", "show_horizontal_scrollbar");
123 attr_dialog_init_toggle
124 (state,
125 "WorkbookView::show_vertical_scrollbar", "show_vertical_scrollbar");
126 attr_dialog_init_toggle
127 (state,
128 "WorkbookView::show_notebook_tabs", "show_notebook_tabs");
131 static void
132 attr_dialog_init_autocompletion_page (AttrState *state)
134 attr_dialog_init_toggle
135 (state,
136 "WorkbookView::do_auto_completion", "do_auto_completion");
139 static void
140 attr_dialog_init_cell_marker_page (AttrState *state)
142 attr_dialog_init_toggle
143 (state,
144 "WorkbookView::show_function_cell_markers",
145 "show_function_cell_markers");
146 attr_dialog_init_toggle
147 (state,
148 "WorkbookView::show_extension_markers",
149 "show_extension_markers");
152 static void
153 attr_dialog_init_protection_page (AttrState *state)
155 attr_dialog_init_toggle
156 (state,
157 "WorkbookView::workbook_protected", "protected");
159 /*****************************************************************************/
162 static void
163 attr_dialog_add_item (AttrState *state, char const *page_name,
164 char const *icon_name,
165 int page, char const* parent_path)
167 GtkTreeIter iter, parent;
168 GdkPixbuf *icon = icon_name
169 ? go_gtk_widget_render_icon_pixbuf (GTK_WIDGET (wbcg_toplevel (state->wbcg)), icon_name, GTK_ICON_SIZE_MENU)
170 : NULL;
172 if (parent_path && gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (state->store),
173 &parent, parent_path))
174 gtk_tree_store_append (state->store, &iter, &parent);
175 else
176 gtk_tree_store_append (state->store, &iter, NULL);
178 gtk_tree_store_set (state->store, &iter,
179 ITEM_ICON, icon,
180 ITEM_NAME, _(page_name),
181 PAGE_NUMBER, page,
182 -1);
183 if (icon != NULL)
184 g_object_unref (icon);
187 typedef struct {
188 char const *page_name;
189 char const *icon_name;
190 char const *parent_path;
191 int const page;
192 void (*page_initializer) (AttrState *state);
193 } page_info_t;
195 static page_info_t const page_info[] = {
196 {N_("Widgets"), "gnumeric-object-scrollbar", NULL, 0, &attr_dialog_init_widget_page },
197 {N_("Protection"), GTK_STOCK_DIALOG_AUTHENTICATION, NULL, 1 ,&attr_dialog_init_protection_page },
198 {N_("Auto Completion"), NULL, NULL, 2 ,&attr_dialog_init_autocompletion_page },
199 {N_("Cell Markers"), NULL, NULL, 3 ,&attr_dialog_init_cell_marker_page },
200 {NULL, NULL, NULL, -1, NULL},
203 typedef struct {
204 int const page;
205 GtkTreePath *path;
206 } page_search_t;
208 static gboolean
209 attr_dialog_select_page_search (GtkTreeModel *model,
210 GtkTreePath *path,
211 GtkTreeIter *iter,
212 page_search_t *pst)
214 int page;
215 gtk_tree_model_get (model, iter, PAGE_NUMBER, &page, -1);
216 if (page == pst->page) {
217 pst->path = gtk_tree_path_copy (path);
218 return TRUE;
219 } else
220 return FALSE;
223 static void
224 attr_dialog_select_page (AttrState *state, int page)
226 page_search_t pst = {page, NULL};
228 if (page >= 0)
229 gtk_tree_model_foreach (GTK_TREE_MODEL (state->store),
230 (GtkTreeModelForeachFunc) attr_dialog_select_page_search,
231 &pst);
233 if (pst.path == NULL)
234 pst.path = gtk_tree_path_new_from_string ("0");
236 if (pst.path != NULL) {
237 gtk_tree_view_set_cursor (state->tview, pst.path, NULL, FALSE);
238 gtk_tree_view_expand_row (state->tview, pst.path, TRUE);
239 gtk_tree_path_free (pst.path);
243 static void
244 cb_attr_dialog_selection_changed (GtkTreeSelection *selection,
245 AttrState *state)
247 GtkTreeIter iter;
249 if (gtk_tree_selection_get_selected (selection, NULL, &iter)) {
250 gtk_tree_model_get (GTK_TREE_MODEL (state->store), &iter,
251 PAGE_NUMBER, &attr_dialog_page,
252 -1);
253 gtk_notebook_set_current_page (GTK_NOTEBOOK (state->notebook), attr_dialog_page);
254 } else
255 attr_dialog_select_page (state, attr_dialog_page);
258 /*****************************************************************************/
260 static void
261 attr_dialog_impl (AttrState *state)
263 GtkWidget *dialog = go_gtk_builder_get_widget (state->gui, "WorkbookAttr");
264 GtkTreeViewColumn *column;
265 GtkTreeSelection *selection;
266 int i;
268 g_return_if_fail (dialog != NULL);
270 /* Initialize */
271 state->dialog = dialog;
272 state->notebook = go_gtk_builder_get_widget (state->gui, "notebook");
273 state->destroying = FALSE;
275 state->tview = GTK_TREE_VIEW(go_gtk_builder_get_widget (state->gui, "itemlist"));
276 state->store = gtk_tree_store_new (NUM_COLUMNS,
277 GDK_TYPE_PIXBUF,
278 G_TYPE_STRING,
279 G_TYPE_INT);
280 gtk_tree_view_set_model (state->tview, GTK_TREE_MODEL(state->store));
281 g_object_unref (state->store);
282 selection = gtk_tree_view_get_selection (state->tview);
283 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
284 column = gtk_tree_view_column_new_with_attributes ("",
285 gtk_cell_renderer_pixbuf_new (),
286 "pixbuf", ITEM_ICON,
287 NULL);
288 gtk_tree_view_append_column (state->tview, column);
289 column = gtk_tree_view_column_new_with_attributes ("",
290 gtk_cell_renderer_text_new (),
291 "text", ITEM_NAME,
292 NULL);
293 gtk_tree_view_append_column (state->tview, column);
294 gtk_tree_view_set_expander_column (state->tview, column);
296 g_signal_connect (selection,
297 "changed",
298 G_CALLBACK (cb_attr_dialog_selection_changed), state);
300 for (i = 0; page_info[i].page > -1; i++) {
301 const page_info_t *this_page = &page_info[i];
302 this_page->page_initializer (state);
303 attr_dialog_add_item (state, this_page->page_name, this_page->icon_name,
304 this_page->page, this_page->parent_path);
307 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (state->store), ITEM_NAME, GTK_SORT_ASCENDING);
309 g_signal_connect (G_OBJECT (go_gtk_builder_get_widget (state->gui, "close_button")),
310 "clicked",
311 G_CALLBACK (cb_attr_dialog_dialog_close), state);
313 gnm_init_help_button (
314 go_gtk_builder_get_widget (state->gui, "help_button"),
315 GNUMERIC_HELP_LINK_WORKBOOK_ATTRIBUTE);
317 /* a candidate for merging into attach guru */
318 g_object_set_data_full (G_OBJECT (dialog),
319 "state", state, (GDestroyNotify) cb_attr_dialog_dialog_destroy);
320 wbc_gtk_attach_guru (state->wbcg, state->dialog);
321 gnm_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
322 WORKBOOK_ATTRIBUTE_KEY);
323 gtk_widget_show (state->dialog);
326 void
327 dialog_workbook_attr (WBCGtk *wbcg)
329 GtkBuilder *gui;
330 AttrState *state;
332 g_return_if_fail (wbcg != NULL);
334 if (gnm_dialog_raise_if_exists (wbcg, WORKBOOK_ATTRIBUTE_KEY))
335 return;
337 gui = gnm_gtk_builder_load ("workbook-attr.ui", NULL, GO_CMD_CONTEXT (wbcg));
338 if (gui == NULL)
339 return;
341 /* Initialize */
342 state = g_new (AttrState, 1);
343 state->gui = gui;
344 state->wbcg = wbcg;
345 state->wbv = wb_control_view (GNM_WBC (wbcg));
346 state->wb = wb_control_get_workbook (GNM_WBC (wbcg));
348 attr_dialog_impl (state);
350 /* Select the same page the last invocation used */
351 attr_dialog_select_page (state, attr_dialog_page);