Replaced GtkTable with GtkGrid in dialog-autoformat.
[gnumeric.git] / src / dialogs / dialog-autoformat.c
blobb4a61917577271d71c6087331efb55bca009e33c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * dialog-autoformat.c : implementation of the autoformat dialog
5 * Author : Almer S. Tigelaar <almer@gnome.org>
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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * WORKING NOTE : Once the edit dialog is ready, search for FIXME and
24 * remove the disabling of new/edit/remove buttons!
27 #include <gnumeric-config.h>
28 #include <glib/gi18n-lib.h>
29 #include <gnumeric.h>
30 #include "dialogs.h"
31 #include "help.h"
33 #include <gui-util.h>
34 #include <mstyle.h>
35 #include <style-border.h>
36 #include <value.h>
37 #include <preview-grid-impl.h>
38 #include <format-template.h>
39 #include <file-autoft.h>
40 #include <command-context.h>
41 #include <workbook-control.h>
42 #include <workbook.h>
43 #include <wbc-gtk.h>
44 #include <commands.h>
45 #include <selection.h>
46 #include <ranges.h>
48 #include <goffice/goffice.h>
49 #include <gtk/gtk.h>
50 #include <gsf/gsf-impl-utils.h>
51 #include <string.h>
53 /* Table to show for
54 * previews, please don't make this larger than 5x5
56 #define PREVIEW_COLS 5
57 #define PREVIEW_ROWS 5
58 #define NUM_PREVIEWS 6
59 #define DEFAULT_COL_WIDTH 52
60 #define DEFAULT_ROW_HEIGHT 17
61 #define BORDER 7
62 #define INNER_BORDER 5
63 #define TOTAL_WIDTH (DEFAULT_COL_WIDTH * PREVIEW_COLS)
64 #define TOTAL_HEIGHT (DEFAULT_ROW_HEIGHT * PREVIEW_ROWS)
66 /* Keep these strings very short.
67 They are used as a sample data for a sheet, so you can put anything here
68 ("One", "Two", "Three" for example) */
69 static char const *const
70 demotable[PREVIEW_ROWS][PREVIEW_COLS] = {
71 { N_(" ") , N_("Jan"), N_("Feb"), N_("Mar"), N_("Total") },
72 { N_("North"), N_("6"), N_("13"), N_("20"), N_("39") },
73 { N_("South"), N_("12"), N_("4"), N_("17"), N_("33") },
74 { N_("West") , N_("8"), N_("2"), N_("0"), N_("10") },
75 { N_("Total"), N_("26"), N_("19"), N_("37"), N_("81") }
78 typedef struct {
79 Workbook *wb; /* Workbook we are working on */
80 WBCGtk *wbcg;
81 GocItem *grid[NUM_PREVIEWS]; /* Previewgrid's */
82 GocItem *selrect; /* Selection rectangle */
83 GSList *templates; /* List of GnmFormatTemplate's */
84 GnmFormatTemplate *selected_template; /* The currently selected template */
85 GList *category_groups; /* List of groups of categories */
87 FormatTemplateCategoryGroup *current_category_group; /* Currently selected category group */
89 int preview_top; /* Top index of the previewlist */
90 int preview_index; /* Selected canvas in previewlist */
91 gboolean previews_locked; /* If true, the preview_free and preview_load will not function */
92 gboolean more_down; /* If true, more was clicked and the button caption is now 'Less' */
95 * Gui elements
97 GtkDialog *dialog;
99 GtkComboBox *category;
101 GocCanvas *canvas[NUM_PREVIEWS];
102 GtkFrame *frame[NUM_PREVIEWS];
103 GtkScrollbar *scroll;
104 GtkCheckMenuItem *gridlines;
106 GtkEntry *info_name, *info_author, *info_cat;
107 GtkTextView *info_descr;
109 GtkCheckMenuItem *number, *border, *font, *patterns, *alignment;
111 struct {
112 GtkCheckMenuItem *left;
113 GtkCheckMenuItem *right;
114 GtkCheckMenuItem *top;
115 GtkCheckMenuItem *bottom;
116 } edges;
118 GtkButton *ok, *cancel;
119 } AutoFormatState;
121 /********************************************************************************/
123 typedef struct {
124 PreviewGrid base;
125 GnmFormatTemplate *ft;
126 } AutoFormatGrid;
127 typedef PreviewGridClass AutoFormatGridClass;
129 static GnmStyle *
130 afg_get_cell_style (PreviewGrid *pg, int col, int row)
132 /* If this happens to be NULL the default style
133 * will automatically be used. */
134 AutoFormatGrid *ag = (AutoFormatGrid *) pg;
135 return format_template_get_style (ag->ft, row, col);
138 static GnmValue *
139 afg_get_cell_value (G_GNUC_UNUSED PreviewGrid *pg, int col, int row)
141 char const *text;
142 char *endptr = NULL;
143 double tmp;
145 if (row >= PREVIEW_ROWS || col >= PREVIEW_COLS)
146 return NULL;
148 text = _(demotable[row][col]);
149 tmp = go_strtod (text, &endptr);
151 if (*endptr == '\0')
152 return value_new_float (tmp);
153 return value_new_string (text);
156 static void
157 auto_format_grid_class_init (PreviewGridClass *klass)
159 klass->get_cell_style = afg_get_cell_style;
160 klass->get_cell_value = afg_get_cell_value;
163 static GSF_CLASS (AutoFormatGrid, auto_format_grid,
164 auto_format_grid_class_init, NULL,
165 preview_grid_get_type())
167 static GocItem *
168 auto_format_grid_new (AutoFormatState *state, int i, GnmFormatTemplate *ft)
170 GocItem *item = goc_item_new (
171 goc_canvas_get_root (state->canvas[i]),
172 auto_format_grid_get_type (),
173 "render-gridlines", gtk_check_menu_item_get_active (state->gridlines),
174 "default-col-width", DEFAULT_COL_WIDTH,
175 "default-row-height", DEFAULT_ROW_HEIGHT,
176 "x", 0.,
177 "y", 0.,
178 NULL);
179 ((AutoFormatGrid *) item)->ft = ft;
180 return item;
182 /********************************************************************************
183 * UTILITY FUNCTIONS
184 ********************************************************************************/
186 static void
187 templates_free (AutoFormatState *state)
189 GSList *ptr;
191 g_return_if_fail (state != NULL);
193 for (ptr = state->templates; ptr != NULL ; ptr = ptr->next)
194 format_template_free (ptr->data);
195 g_slist_free (state->templates);
196 state->templates = NULL;
200 * templates_load:
201 * @state: AutoFormatState
203 * This function will load the templates in the currently selected
204 * category group (it looks at state->category_groups to determine the selection)
206 * Return value: TRUE if all went well, FALSE otherwise.
208 static gboolean
209 templates_load (AutoFormatState *state)
211 GSList *l;
212 gint n_templates;
214 g_return_val_if_fail (state != NULL, FALSE);
216 if (state->category_groups == NULL)
217 return FALSE;
219 state->templates = category_group_get_templates_list (
220 state->current_category_group, GO_CMD_CONTEXT (state->wbcg));
221 for (l = state->templates; l != NULL; l = l->next) {
222 GnmFormatTemplate *ft = l->data;
223 range_init (&ft->dimension,
224 0, 0, PREVIEW_COLS - 1, PREVIEW_ROWS - 1);
225 ft->invalidate_hash = TRUE;
227 n_templates = g_slist_length (state->templates);
230 * We need to temporary lock the preview loading/freeing or
231 * else our scrollbar will trigger an event (value_changed) and create the
232 * previews. (which we don't want to happen at this moment)
234 state->previews_locked = TRUE;
236 GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (state->scroll));
237 gtk_adjustment_configure (adjustment,
238 0, 0, n_templates / 2,
239 1, 3, 3);
241 state->previews_locked = FALSE;
244 * Hide the scrollbar when it's not needed
246 gtk_widget_set_visible (GTK_WIDGET (state->scroll),
247 n_templates > NUM_PREVIEWS);
249 return TRUE;
253 * previews_free:
254 * @state: AutoFormatState
256 * This function will free all previews.
258 static void
259 previews_free (AutoFormatState *state)
261 int i;
263 if (state->previews_locked)
264 return;
266 if (state->selrect) {
267 goc_item_destroy (state->selrect);
268 state->selrect = NULL;
271 for (i = 0; i < NUM_PREVIEWS; i++) {
272 GocItem *item = state->grid[i];
273 if (item) {
274 goc_item_destroy (state->grid[i]);
275 state->grid[i] = NULL;
281 * previews_load:
282 * @state: AutoFormatState
283 * @topindex: The index of the template to be displayed in the upper left corner
285 * This function will create grids and rects for each canvas and associate
286 * them with the right format templates.
287 * NOTE : if state->preview_locked is TRUE this function will do nothing,
288 * this is handy in situation where signals can cause previews_load to be
289 * called before previews_free.
291 static void
292 previews_load (AutoFormatState *state, int topindex)
294 GSList *iterator, *start;
295 int i, count = topindex;
297 g_return_if_fail (state != NULL);
299 if (state->previews_locked)
300 return;
302 iterator = state->templates;
303 start = iterator;
304 while (iterator && count > 0) {
305 iterator = g_slist_next (iterator);
306 start = iterator;
307 count--;
310 for (i = 0; i < NUM_PREVIEWS; i++) {
311 if (start == NULL) {
312 gtk_widget_hide (GTK_WIDGET (state->canvas[i]));
313 gtk_frame_set_shadow_type (state->frame[i], GTK_SHADOW_NONE);
314 } else {
315 GnmFormatTemplate *ft = start->data;
317 state->grid[i] = auto_format_grid_new (state, i, ft);
319 /* Are we selected? Then draw a selection rectangle */
320 if (topindex + i == state->preview_index) {
321 GOStyle *style;
322 g_return_if_fail (state->selrect == NULL);
324 state->selrect = goc_item_new (goc_canvas_get_root (state->canvas[i]),
325 GOC_TYPE_RECTANGLE,
326 "x", (double)(-INNER_BORDER),
327 "y", (double)(-INNER_BORDER),
328 "width", (double)(TOTAL_WIDTH + 2 * INNER_BORDER),
329 "height", (double)(TOTAL_HEIGHT + 2 * INNER_BORDER),
330 NULL);
331 style = go_styled_object_get_style (GO_STYLED_OBJECT (state->selrect));
332 style->line.width = 3.;
333 style->line.color = GO_COLOR_RED;
334 style->fill.pattern.back = 0;
336 gtk_frame_set_shadow_type (state->frame[i], GTK_SHADOW_IN);
337 } else
338 gtk_frame_set_shadow_type (state->frame[i], GTK_SHADOW_ETCHED_IN);
340 goc_canvas_scroll_to (state->canvas[i],
341 -BORDER, -BORDER);
343 gtk_widget_set_tooltip_text
344 (GTK_WIDGET (state->canvas[i]),
345 _(ft->name));
347 gtk_widget_show (GTK_WIDGET (state->canvas[i]));
348 start = g_slist_next (start);
352 state->preview_top = topindex;
355 /********************************************************************************
356 * SIGNAL HANDLERS
357 ********************************************************************************/
359 static void
360 cb_ok_clicked (G_GNUC_UNUSED GtkButton *button,
361 AutoFormatState *state)
363 if (state->selected_template)
364 cmd_selection_autoformat (WORKBOOK_CONTROL (state->wbcg),
365 format_template_clone (state->selected_template));
367 gtk_widget_destroy (GTK_WIDGET (state->dialog));
370 static void
371 cb_autoformat_destroy (AutoFormatState *state)
373 templates_free (state);
374 category_group_list_free (state->category_groups);
375 g_free (state);
378 static void
379 cb_scroll_value_changed (GtkAdjustment *adjustment, AutoFormatState *state)
381 previews_free (state);
382 previews_load (state, rint (gtk_adjustment_get_value (adjustment)) * 2);
385 static gboolean
386 cb_canvas_button_press (GocCanvas *canvas,
387 G_GNUC_UNUSED GdkEventButton *event,
388 AutoFormatState *state)
390 GnmFormatTemplate *ft;
391 GSList *ptr;
392 int index = 0;
394 while (canvas != state->canvas[index] && index < NUM_PREVIEWS)
395 index++;
397 g_return_val_if_fail (index < NUM_PREVIEWS, FALSE);
399 state->preview_index = state->preview_top + index;
401 previews_free (state);
402 previews_load (state, state->preview_top);
404 for (ptr = state->templates, index = 0; ptr != NULL ; ptr = ptr->next, index++)
405 if (index == state->preview_index)
406 break;
408 g_return_val_if_fail (ptr != NULL && ptr->data != NULL, FALSE);
410 ft = ptr->data;
411 state->selected_template = ft;
412 gtk_entry_set_text (state->info_name, _(ft->name));
413 gtk_entry_set_text (state->info_author, ft->author);
414 gnumeric_textview_set_text (GTK_TEXT_VIEW (state->info_descr),
415 _(ft->description));
417 gtk_entry_set_text (state->info_cat, _(ft->category->name));
419 return TRUE;
422 static void
423 cb_check_item_toggled (G_GNUC_UNUSED GtkCheckMenuItem *item,
424 AutoFormatState *state)
426 GSList *ptr;
427 int i;
429 for (ptr = state->templates; ptr != NULL ; ptr = ptr->next) {
430 GnmFormatTemplate *ft = ptr->data;
432 ft->number = gtk_check_menu_item_get_active (state->number);
433 ft->border = gtk_check_menu_item_get_active (state->border);
434 ft->font = gtk_check_menu_item_get_active (state->font);
435 ft->patterns = gtk_check_menu_item_get_active (state->patterns);
436 ft->alignment = gtk_check_menu_item_get_active (state->alignment);
438 ft->edges.left = gtk_check_menu_item_get_active (state->edges.left);
439 ft->edges.right = gtk_check_menu_item_get_active (state->edges.right);
440 ft->edges.top = gtk_check_menu_item_get_active (state->edges.top);
441 ft->edges.bottom = gtk_check_menu_item_get_active (state->edges.bottom);
443 ft->invalidate_hash = TRUE;
446 for (i = 0; i < NUM_PREVIEWS; i++)
447 goc_canvas_invalidate (state->canvas [i],
448 -2, -2, INT_MAX/2, INT_MAX/2);
451 static void
452 cb_category_changed (AutoFormatState *state)
454 GList *selection = g_list_nth (state->category_groups,
455 gtk_combo_box_get_active (state->category));
456 char const *tip = NULL;
458 state->current_category_group = (selection != NULL) ? selection->data : NULL;
459 previews_free (state);
460 templates_free (state);
461 if (templates_load (state) == FALSE)
462 g_warning ("Error while loading templates!");
464 if (NULL != state->current_category_group) {
465 tip = state->current_category_group->description;
466 if (NULL == tip)
467 tip = state->current_category_group->name;
469 gtk_widget_set_tooltip_text (GTK_WIDGET (state->category),
470 (NULL != tip) ? _(tip) : "");
471 previews_load (state, 0);
472 cb_check_item_toggled (NULL, state);
473 cb_canvas_button_press (state->canvas[0], NULL, state);
476 static void
477 cb_gridlines_item_toggled (G_GNUC_UNUSED GtkCheckMenuItem *item,
478 AutoFormatState *state)
480 previews_free (state);
481 previews_load (state, state->preview_top);
484 /********************************************************************************
485 * MAIN
486 ********************************************************************************/
488 /* Menus */
489 static GtkActionEntry entries[] = {
490 { "settings", NULL, N_("_Settings"), NULL, NULL, NULL },
491 { "edges", NULL, N_("_Edges"), NULL, NULL, NULL }
494 /* Toggle items */
495 static GtkToggleActionEntry toggle_entries[] = {
496 { "number", NULL, N_("Apply _Number Formats"), NULL,
497 NULL, G_CALLBACK (cb_check_item_toggled), TRUE},
498 { "border", NULL, N_("Apply _Borders"), NULL,
499 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
500 { "font", NULL, N_("Apply _Fonts"), NULL,
501 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
502 { "patterns", NULL, N_("Apply _Patterns"), NULL,
503 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
504 { "alignment", NULL, N_("Apply _Alignment"), NULL,
505 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
506 { "left", NULL, N_("_Left"), NULL,
507 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
508 { "right", NULL, N_("_Right"), NULL,
509 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
510 { "top", NULL, N_("_Top"), NULL,
511 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
512 { "bottom", NULL, N_("_Bottom"), NULL,
513 NULL, G_CALLBACK (cb_check_item_toggled), TRUE },
514 { "gridlines", NULL, N_("_Show Gridlines"), NULL,
515 NULL, G_CALLBACK (cb_gridlines_item_toggled), FALSE }
518 static const char *ui_description =
519 "<ui>"
520 " <menubar name='bar'>"
521 " <menu action='settings'>"
522 " <menuitem action='number'/>"
523 " <menuitem action='border'/>"
524 " <menuitem action='font'/>"
525 " <menuitem action='patterns'/>"
526 " <menuitem action='alignment'/>"
527 " <separator name='settings-sep1'/>"
528 " <menu action='edges'>"
529 " <menuitem action='left'/>"
530 " <menuitem action='right'/>"
531 " <menuitem action='top'/>"
532 " <menuitem action='bottom'/>"
533 " </menu>"
534 " <separator name='settings-sep2'/>"
535 " <menuitem action='gridlines'/>"
536 " </menu>"
537 " </menubar>"
538 "</ui>";
540 static gboolean
541 cb_canvas_focus (GtkWidget *canvas, GtkDirectionType direction,
542 AutoFormatState *state)
544 if (!gtk_widget_has_focus (canvas)) {
545 gtk_widget_grab_focus (canvas);
546 cb_canvas_button_press (GOC_CANVAS (canvas), NULL, state);
547 return TRUE;
549 return FALSE;
553 * dialog_autoformat:
554 * @wb: The Workbook
556 * This function will show the AutoFormatTemplate dialog and apply
557 * the template the user chooses to the current selection in the active
558 * sheet of the workbook if the user desires.
560 void
561 dialog_autoformat (WBCGtk *wbcg)
563 GtkBuilder *gui;
564 AutoFormatState *state;
565 int i;
566 GtkUIManager *ui_manager;
567 GtkActionGroup *action_group;
569 gui = gnm_gtk_builder_new ("autoformat.ui", NULL, GO_CMD_CONTEXT (wbcg));
570 if (gui == NULL)
571 return;
573 state = g_new0 (AutoFormatState, 1);
574 state->wb = wb_control_get_workbook (WORKBOOK_CONTROL (wbcg));
575 state->wbcg = wbcg;
576 state->templates = NULL;
577 state->category_groups = NULL;
578 state->selrect = NULL;
579 for (i = 0; i < NUM_PREVIEWS; i++)
580 state->grid[i] = NULL;
582 state->current_category_group = NULL;
583 state->preview_top = 0;
584 state->preview_index = -1;
585 state->previews_locked = FALSE;
586 state->more_down = FALSE;
587 state->selected_template = NULL;
589 state->dialog = GTK_DIALOG (go_gtk_builder_get_widget (gui, "dialog"));
590 state->category = GTK_COMBO_BOX (go_gtk_builder_get_widget (gui, "format_category"));
591 state->scroll = GTK_SCROLLBAR (go_gtk_builder_get_widget (gui, "format_scroll"));
592 state->gridlines = GTK_CHECK_MENU_ITEM (go_gtk_builder_get_widget (gui, "format_gridlines"));
594 state->info_name = GTK_ENTRY (go_gtk_builder_get_widget (gui, "format_info_name"));
595 state->info_author = GTK_ENTRY (go_gtk_builder_get_widget (gui, "format_info_author"));
596 state->info_cat = GTK_ENTRY (go_gtk_builder_get_widget (gui, "format_info_cat"));
597 state->info_descr = GTK_TEXT_VIEW (go_gtk_builder_get_widget (gui, "format_info_descr"));
599 state->ok = GTK_BUTTON (go_gtk_builder_get_widget (gui, "format_ok"));
600 state->cancel = GTK_BUTTON (go_gtk_builder_get_widget (gui, "format_cancel"));
602 action_group = gtk_action_group_new ("settings-actions");
603 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
604 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), state);
605 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), state);
607 ui_manager = gtk_ui_manager_new ();
608 gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
609 g_object_unref (action_group);
610 gtk_ui_manager_add_ui_from_string (ui_manager, ui_description, -1, NULL);
611 state->number = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/number"));
612 state->border = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/border"));
613 state->font = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/font"));
614 state->patterns = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/patterns"));
615 state->alignment = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/alignment"));
617 state->edges.left = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/edges/left"));
618 state->edges.right = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/edges/right"));
619 state->edges.top = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/edges/top"));
620 state->edges.bottom = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/edges/bottom"));
622 state->gridlines = GTK_CHECK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager, "/bar/settings/gridlines"));
624 gtk_grid_attach (GTK_GRID (go_gtk_builder_get_widget (gui, "preview-grid")),
625 gtk_ui_manager_get_widget (ui_manager, "/bar"),
626 2, 0, 1, 1);
627 g_object_set (gtk_ui_manager_get_widget (ui_manager, "/bar"), "hexpand", TRUE, NULL);
628 for (i = 0; i < NUM_PREVIEWS; i++) {
629 char *name;
631 name = g_strdup_printf ("format_frame%d", i+1);
632 state->frame[i] = GTK_FRAME (go_gtk_builder_get_widget (gui, name));
633 g_free (name);
635 state->canvas[i] = GOC_CANVAS (g_object_new (GOC_TYPE_CANVAS, NULL));
636 gtk_widget_set_size_request (GTK_WIDGET (state->canvas[i]),
637 TOTAL_WIDTH + (2 * BORDER),
638 TOTAL_HEIGHT + (2 * BORDER));
639 gtk_container_add (GTK_CONTAINER (state->frame[i]),
640 GTK_WIDGET (state->canvas[i]));
642 g_signal_connect (G_OBJECT (state->canvas[i]),
643 "button-press-event",
644 G_CALLBACK (cb_canvas_button_press), state);
645 g_signal_connect (G_OBJECT (state->canvas[i]),
646 "focus",
647 G_CALLBACK (cb_canvas_focus), state);
650 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (state->scroll))),
651 "value_changed",
652 G_CALLBACK (cb_scroll_value_changed), state);
653 g_signal_connect (G_OBJECT (state->gridlines),
654 "toggled",
655 G_CALLBACK (cb_gridlines_item_toggled), state);
656 g_signal_connect (G_OBJECT (state->ok),
657 "clicked",
658 G_CALLBACK (cb_ok_clicked), state);
659 g_signal_connect_swapped (G_OBJECT (state->cancel), "clicked",
660 G_CALLBACK (gtk_widget_destroy), state->dialog);
662 /* Fill category list */
663 state->category_groups =
664 g_list_sort (category_group_list_get (), category_group_cmp);
666 if (state->category_groups == NULL) {
667 GtkWidget *dialog;
669 dialog = gtk_message_dialog_new (GTK_WINDOW (state->dialog),
670 GTK_DIALOG_DESTROY_WITH_PARENT,
671 GTK_MESSAGE_WARNING,
672 GTK_BUTTONS_CLOSE,
673 _("An error occurred while reading the category list"));
674 gtk_dialog_run (GTK_DIALOG (dialog));
675 } else {
676 unsigned i, select = 0;
677 GList *ptr = state->category_groups;
678 GtkListStore* store = gtk_list_store_new (1, G_TYPE_STRING);
679 GtkTreeIter iter;
680 GtkCellRenderer *renderer = (GtkCellRenderer*) gtk_cell_renderer_text_new();
681 gtk_combo_box_set_model (state->category, GTK_TREE_MODEL (store));
682 g_object_unref (store);
683 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (state->category), renderer, TRUE);
684 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (state->category), renderer,
685 "text", 0,
686 NULL);
688 for (i = 0 ; ptr != NULL ; ptr = ptr->next, i++) {
689 FormatTemplateCategoryGroup *group = ptr->data;
690 if (!strcmp (group->name, "General" ))
691 select = i;
692 gtk_list_store_append (store, &iter);
693 gtk_list_store_set (store, &iter,
694 0, _(group->name),
695 -1);
698 g_signal_connect_swapped (G_OBJECT (state->category),
699 "changed",
700 G_CALLBACK (cb_category_changed), state);
701 gtk_combo_box_set_active (GTK_COMBO_BOX (state->category), select);
702 gtk_widget_show_all (GTK_WIDGET (state->category));
705 gnumeric_init_help_button (
706 go_gtk_builder_get_widget (gui, "help_button"),
707 GNUMERIC_HELP_LINK_AUTOFORMAT);
709 gtk_dialog_set_default_response (state->dialog, GTK_RESPONSE_OK);
711 /* a candidate for merging into attach guru */
712 go_gtk_nonmodal_dialog (wbcg_toplevel (state->wbcg),
713 GTK_WINDOW (state->dialog));
714 wbc_gtk_attach_guru (state->wbcg, GTK_WIDGET (state->dialog));
715 g_object_set_data_full (G_OBJECT (state->dialog),
716 "state", state, (GDestroyNotify)cb_autoformat_destroy);
718 /* not show all or the scrollbars will appear */
719 gtk_widget_show (GTK_WIDGET (state->dialog));
720 g_object_unref (gui);
721 g_object_unref (ui_manager);