Compilation: don't compile dialogs separately.
[gnumeric.git] / src / dialogs / dialog-stf-format-page.c
blob43cb2bf06a7fd4129287019999d044e8d2d6ad19
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * dialog-stf-format-page.c : Controls the widgets on the format page of the dialog
5 * Copyright 2001 Almer S. Tigelaar <almer@gnome.org>
6 * Copyright 2003 Morten Welinder <terra@gnome.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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, see <https://www.gnu.org/licenses/>.
22 #include <gnumeric-config.h>
23 #include <glib/gi18n-lib.h>
24 #include <gnumeric.h>
25 #include <libgnumeric.h>
26 #include "dialog-stf.h"
27 #include <gnm-format.h>
28 #include <sheet.h>
29 #include <workbook.h>
30 #include <workbook-control.h>
31 #include <gui-util.h>
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
35 /*************************************************************************************************
36 * MISC UTILITY FUNCTIONS
37 *************************************************************************************************/
39 static void format_page_update_preview (StfDialogData *pagedata);
41 static void
42 format_page_update_column_selection (StfDialogData *pagedata)
44 char *text = NULL;
46 if (pagedata->format.col_import_count == pagedata->format.col_import_array_len) {
47 text = g_strdup_printf (_("Importing %i columns and ignoring none."),
48 pagedata->format.col_import_count);
49 } else {
50 text = g_strdup_printf (_("Importing %i columns and ignoring %i."),
51 pagedata->format.col_import_count,
52 pagedata->format.col_import_array_len - pagedata->format.col_import_count);
55 gtk_label_set_text (GTK_LABEL (pagedata->format.column_selection_label), text);
57 g_free (text);
60 static void
61 format_page_trim_menu_changed (G_GNUC_UNUSED GtkMenu *menu,
62 StfDialogData *data)
64 StfTrimType_t trim;
65 int trimtype = gtk_combo_box_get_active (GTK_COMBO_BOX (data->format.format_trim));
67 switch (trimtype) {
68 case -1:
69 case 0:
70 trim = TRIM_TYPE_LEFT | TRIM_TYPE_RIGHT;
71 break;
72 default:
73 g_warning ("Unknown trim type selected (%d)", trimtype);
74 /* Fall through. */
75 case 1:
76 trim = TRIM_TYPE_NEVER;
77 break;
78 case 2:
79 trim = TRIM_TYPE_LEFT;
80 break;
81 case 3:
82 trim = TRIM_TYPE_RIGHT;
83 break;
86 stf_parse_options_set_trim_spaces (data->parseoptions, trim);
87 format_page_update_preview (data);
91 * More or less a copy of gtk_tree_view_clamp_column_visible.
93 static void
94 tree_view_clamp_column_visible (GtkTreeView *tree_view,
95 GtkTreeViewColumn *column)
97 GtkAdjustment *hadjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (tree_view));
98 double hval = gtk_adjustment_get_value (hadjustment);
99 double hps = gtk_adjustment_get_page_size (hadjustment);
100 GtkWidget *button = gtk_tree_view_column_get_button (column);
101 GtkAllocation ba;
103 gtk_widget_get_allocation (button, &ba);
105 if (hval + hps < ba.x + ba.width)
106 gtk_adjustment_set_value (hadjustment,
107 ba.x + ba.width - hps);
108 else if (hval > ba.x)
109 gtk_adjustment_set_value (hadjustment, ba.x);
112 static void
113 activate_column (StfDialogData *pagedata, int i)
115 GtkCellRenderer *cell;
116 GtkTreeViewColumn *column;
117 RenderData_t *renderdata = pagedata->format.renderdata;
119 cell = stf_preview_get_cell_renderer (renderdata,
120 pagedata->format.index);
121 if (cell) {
122 g_object_set (G_OBJECT (cell),
123 "background", NULL,
124 NULL);
127 pagedata->format.index = i;
129 column = stf_preview_get_column (renderdata, i);
130 if (column) {
131 tree_view_clamp_column_visible (renderdata->tree_view, column);
134 cell = stf_preview_get_cell_renderer (renderdata, i);
135 if (cell) {
136 g_object_set (G_OBJECT (cell),
137 "background", "lightgrey",
138 NULL);
139 gtk_widget_queue_draw (GTK_WIDGET (renderdata->tree_view));
142 /* FIXME: warp focus away from the header. */
145 static void
146 cb_col_check_clicked (GtkToggleButton *togglebutton, gpointer _i)
148 int i = GPOINTER_TO_INT (_i);
149 StfDialogData *pagedata =
150 g_object_get_data (G_OBJECT (togglebutton), "pagedata");
151 gboolean active = gtk_toggle_button_get_active (togglebutton);
152 GtkCellRenderer *renderer;
153 GtkTreeViewColumn* column;
154 GtkWidget *check_autofit;
156 g_return_if_fail (i < pagedata->format.col_import_array_len);
158 if (pagedata->format.col_import_array[i] == active)
159 return;
161 renderer = stf_preview_get_cell_renderer (pagedata->format.renderdata, i);
162 g_object_set (G_OBJECT (renderer), "strikethrough", !active, NULL);
163 gtk_widget_queue_draw (GTK_WIDGET (pagedata->format.renderdata->tree_view));
165 if (!active) {
166 pagedata->format.col_import_array[i] = FALSE;
167 pagedata->format.col_import_count--;
168 format_page_update_column_selection (pagedata);
169 } else {
170 if (pagedata->format.col_import_count < GNM_MAX_COLS) {
171 pagedata->format.col_import_array[i] = TRUE;
172 pagedata->format.col_import_count++;
173 format_page_update_column_selection (pagedata);
174 } else {
175 char *msg = g_strdup_printf(
176 ngettext("A maximum of %d column can be imported.",
177 "A maximum of %d columns can be imported.",
178 GNM_MAX_COLS),
179 GNM_MAX_COLS);
180 gtk_toggle_button_set_active (togglebutton, FALSE);
181 go_gtk_notice_dialog (GTK_WINDOW (pagedata->dialog),
182 GTK_MESSAGE_WARNING,
183 "%s", msg);
184 g_free (msg);
188 column = stf_preview_get_column (pagedata->format.renderdata, i);
189 check_autofit = g_object_get_data (G_OBJECT (column), "checkbox-autofit");
191 gtk_widget_set_sensitive (check_autofit, active);
192 return;
195 static void
196 cb_format_clicked (GtkButton *widget, gpointer _i)
198 int i = GPOINTER_TO_INT (_i);
199 StfDialogData *pagedata =
200 g_object_get_data (G_OBJECT (widget), "pagedata");
201 gint result;
202 GtkWidget *dialog = gtk_dialog_new_with_buttons
203 (_("Format Selector"),
204 GTK_WINDOW (pagedata->dialog),
205 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
206 GNM_STOCK_OK, GTK_RESPONSE_ACCEPT,
207 GNM_STOCK_CANCEL, GTK_RESPONSE_REJECT,
208 NULL);
209 GOFormatSel *format_selector
210 = GO_FORMAT_SEL (go_format_sel_new_full (TRUE));
211 GtkWidget *w = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
213 go_format_sel_set_style_format (format_selector, g_ptr_array_index (pagedata->format.formats, i));
214 go_format_sel_set_locale (format_selector, pagedata->locale);
215 gtk_box_pack_start (GTK_BOX (w), GTK_WIDGET (format_selector),
216 FALSE, TRUE, 5);
217 gtk_widget_show (GTK_WIDGET (format_selector));
219 result = gtk_dialog_run (GTK_DIALOG (dialog));
220 switch (result) {
221 case GTK_RESPONSE_ACCEPT: {
222 GOFormat *sf;
223 GtkTreeViewColumn* column = stf_preview_get_column (pagedata->format.renderdata, i);
224 GtkWidget *format_label = g_object_get_data (G_OBJECT (column),
225 "formatlabel");
227 sf = g_ptr_array_index (pagedata->format.formats, i);
228 go_format_unref (sf);
230 sf = go_format_ref (go_format_sel_get_fmt (format_selector));
231 gtk_button_set_label (GTK_BUTTON (format_label),
232 go_format_sel_format_classification (sf));
233 g_ptr_array_index (pagedata->format.formats, i) = sf;
235 format_page_update_preview (pagedata);
236 break;
238 default:
239 break;
241 gtk_widget_destroy (dialog);
242 return;
245 static void
246 cb_col_check_autofit_clicked (GtkToggleButton *togglebutton, gpointer _i)
248 int i = GPOINTER_TO_INT (_i);
249 StfDialogData *pagedata =
250 g_object_get_data (G_OBJECT (togglebutton), "pagedata");
251 gboolean active = gtk_toggle_button_get_active (togglebutton);
253 g_return_if_fail (i < pagedata->format.col_import_array_len);
255 pagedata->format.col_autofit_array[i] = active;
257 return;
260 static void
261 check_columns_for_import (StfDialogData *pagedata, int from, int to)
263 int i;
265 g_return_if_fail (pagedata != NULL);
266 g_return_if_fail (!(from < 0));
267 g_return_if_fail (to < pagedata->format.renderdata->colcount);
268 g_return_if_fail (to < pagedata->format.col_import_array_len);
270 for (i = from; i <= to; i++) {
271 if (!pagedata->format.col_import_array[i]) {
272 GtkTreeViewColumn* column = stf_preview_get_column (pagedata->format.renderdata, i);
273 GtkWidget *w = g_object_get_data (G_OBJECT (column), "checkbox");
274 if (pagedata->format.col_import_count >= GNM_MAX_COLS)
275 break;
276 gtk_widget_hide (w);
277 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
278 /* Note this caused a signal to be send that sets the */
279 /* pagedata fields */
280 gtk_widget_show (w);
285 static void
286 uncheck_columns_for_import (StfDialogData *pagedata, int from, int to)
288 int i;
290 g_return_if_fail (pagedata != NULL);
291 g_return_if_fail (!(from < 0));
292 g_return_if_fail (to < pagedata->format.renderdata->colcount);
293 g_return_if_fail (to < pagedata->format.col_import_array_len);
295 for (i = from; i <= to; i++) {
296 if (pagedata->format.col_import_array[i]) {
297 GtkTreeViewColumn* column = stf_preview_get_column (pagedata->format.renderdata, i);
298 GtkWidget *w = g_object_get_data (G_OBJECT (column), "checkbox");
300 gtk_widget_hide (w);
301 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), FALSE);
302 /* Note this caused a signal to be send that sets the */
303 /* pagedata fields */
304 gtk_widget_show (w);
309 static void
310 cb_popup_menu_uncheck_right (GtkWidget *widget, gpointer data)
312 StfDialogData *pagedata = data;
314 uncheck_columns_for_import (pagedata, pagedata->format.index + 1,
315 pagedata->format.renderdata->colcount - 1);
318 static void
319 cb_popup_menu_check_right (GtkWidget *widget, gpointer data)
321 StfDialogData *pagedata = data;
323 check_columns_for_import (pagedata, pagedata->format.index + 1,
324 pagedata->format.renderdata->colcount - 1);
327 static void
328 cb_popup_menu_uncheck_left (GtkWidget *widget, gpointer data)
330 StfDialogData *pagedata = data;
332 uncheck_columns_for_import (pagedata, 0, pagedata->format.index - 1);
335 static void
336 cb_popup_menu_check_left (GtkWidget *widget, gpointer data)
338 StfDialogData *pagedata = data;
340 check_columns_for_import (pagedata, 0, pagedata->format.index - 1);
343 static void
344 cb_popup_menu_extend_format (GtkWidget *widget, gpointer data)
346 StfDialogData *pagedata = data;
347 guint index = pagedata->format.index;
348 GPtrArray *formats = pagedata->format.formats;
349 GOFormat *colformat = g_ptr_array_index (formats, pagedata->format.index);
351 for (index++; index < formats->len; index++) {
352 GOFormat *sf = g_ptr_array_index (formats, index);
353 GtkTreeViewColumn* column =
354 stf_preview_get_column (pagedata->format.renderdata,
355 index);
356 GtkWidget *w = g_object_get_data (G_OBJECT (column),
357 "formatlabel");
358 go_format_unref (sf);
359 g_ptr_array_index (formats, index) = go_format_ref (colformat);
360 gtk_button_set_label (GTK_BUTTON (w),
361 go_format_sel_format_classification (colformat));
364 format_page_update_preview (data);
367 static void
368 format_context_menu (StfDialogData *pagedata,
369 GdkEvent *event,
370 int col)
372 enum {
373 COLUMN_POPUP_ITEM_IGNORE,
374 COLUMN_POPUP_ITEM_NOT_FIRST,
375 COLUMN_POPUP_ITEM_NOT_LAST,
376 COLUMN_POPUP_ITEM_ANY
379 static const struct {
380 const char *text;
381 void (*function) (GtkWidget *widget, gpointer data);
382 int flags;
383 } actions[] = {
384 { N_("Ignore all columns on right"), &cb_popup_menu_uncheck_right, COLUMN_POPUP_ITEM_NOT_LAST},
385 { N_("Ignore all columns on left"), &cb_popup_menu_uncheck_left, COLUMN_POPUP_ITEM_NOT_FIRST},
386 { N_("Import all columns on right"), &cb_popup_menu_check_right, COLUMN_POPUP_ITEM_NOT_LAST},
387 { N_("Import all columns on left"), &cb_popup_menu_check_left, COLUMN_POPUP_ITEM_NOT_FIRST},
388 { N_("Copy format to right"), &cb_popup_menu_extend_format, COLUMN_POPUP_ITEM_NOT_LAST}
391 GtkWidget *menu = gtk_menu_new ();
392 unsigned i;
394 for (i = 0; i < G_N_ELEMENTS (actions); i++) {
395 int flags = actions[i].flags;
396 GtkWidget *item = gtk_menu_item_new_with_label
397 (_(actions[i].text));
398 switch (flags) {
399 case COLUMN_POPUP_ITEM_IGNORE:
400 gtk_widget_set_sensitive (item, FALSE);
401 break;
402 case COLUMN_POPUP_ITEM_NOT_FIRST:
403 gtk_widget_set_sensitive (item, col > 0);
404 break;
405 case COLUMN_POPUP_ITEM_NOT_LAST:
406 gtk_widget_set_sensitive
407 (item, col < pagedata->format.renderdata->colcount - 1);
408 break;
409 case COLUMN_POPUP_ITEM_ANY:
410 default:
411 break;
413 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
414 gtk_widget_show (item);
415 g_signal_connect (G_OBJECT (item),
416 "activate",
417 G_CALLBACK (actions[i].function),
418 pagedata);
421 gnumeric_popup_menu (GTK_MENU (menu), event);
425 static gint
426 cb_col_event (GtkWidget *widget, GdkEvent *event, gpointer _col)
428 if (event->type == GDK_BUTTON_PRESS) {
429 GdkEventButton *event_button = &event->button;
430 StfDialogData *pagedata =
431 g_object_get_data (G_OBJECT (widget), "pagedata");
432 int col = GPOINTER_TO_INT (_col);
434 activate_column (pagedata, col);
436 if (event_button->button == 1) {
437 GtkWidget *check = g_object_get_data (G_OBJECT (widget), "checkbox");
438 GtkAllocation a;
440 * We use overlapping buttons and that does not
441 * actually work...
443 * In a square area the height of the hbox, click the
444 * checkbox.
446 int xmax;
448 gtk_widget_get_allocation
449 (gtk_bin_get_child (GTK_BIN (widget)),
450 &a);
451 xmax = a.height;
452 if (event_button->x <= xmax)
453 gtk_button_clicked (GTK_BUTTON (check));
454 } else if (event_button->button == 3) {
455 format_context_menu (pagedata, event, col);
457 return TRUE;
460 return FALSE;
463 static gint
464 cb_treeview_button_press (GtkWidget *treeview,
465 GdkEventButton *event,
466 StfDialogData *pagedata)
468 if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
469 int dx, col;
470 stf_preview_find_column (pagedata->format.renderdata, (int)event->x, &col, &dx);
471 activate_column (pagedata, col);
472 return TRUE;
473 } else if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
474 int dx, col;
475 stf_preview_find_column (pagedata->format.renderdata, (int)event->x, &col, &dx);
476 activate_column (pagedata, col);
477 format_context_menu (pagedata, (GdkEvent*)event, col);
478 return TRUE;
481 return FALSE;
484 static gint
485 cb_treeview_key_press (GtkWidget *treeview,
486 GdkEventKey *event,
487 StfDialogData *pagedata)
489 if (event->type == GDK_KEY_PRESS) {
490 switch (event->keyval) {
491 case GDK_KEY_Left:
492 case GDK_KEY_KP_Left:
493 if (pagedata->format.index > 0)
494 activate_column (pagedata,
495 pagedata->format.index - 1);
496 return TRUE;
498 case GDK_KEY_Right:
499 case GDK_KEY_KP_Right:
500 if (pagedata->format.index + 1 < (int)pagedata->format.formats->len)
501 activate_column (pagedata,
502 pagedata->format.index + 1);
503 return TRUE;
505 case GDK_KEY_space:
506 case GDK_KEY_Return: {
507 GtkTreeViewColumn *column = stf_preview_get_column
508 (pagedata->format.renderdata,
509 pagedata->format.index);
510 GtkToggleButton *button =
511 g_object_get_data (G_OBJECT (column),
512 "checkbox");
513 gtk_toggle_button_set_active
514 (button,
515 !gtk_toggle_button_get_active (button));
516 return TRUE;
519 default:
520 ; /* Nothing. */
524 return FALSE;
528 * format_page_update_preview
529 * @pagedata: mother struct
531 * Will simply utilize the preview rendering functions to update
532 * the preview
534 * returns : nothing
536 static void
537 format_page_update_preview (StfDialogData *pagedata)
539 RenderData_t *renderdata = pagedata->format.renderdata;
540 unsigned int ui;
541 int i;
542 int col_import_array_len_old, old_part;
543 GStringChunk *lines_chunk;
544 char *msg = NULL;
546 stf_preview_colformats_clear (renderdata);
547 for (ui = 0; ui < pagedata->format.formats->len; ui++) {
548 GOFormat *sf = g_ptr_array_index (pagedata->format.formats, ui);
549 stf_preview_colformats_add (renderdata, sf);
552 lines_chunk = g_string_chunk_new (100 * 1024);
553 stf_preview_set_lines (renderdata, lines_chunk,
554 stf_parse_general (pagedata->parseoptions,
555 lines_chunk,
556 pagedata->cur,
557 pagedata->cur_end));
559 col_import_array_len_old = pagedata->format.col_import_array_len;
560 pagedata->format.col_import_array_len = renderdata->colcount;
562 pagedata->format.col_autofit_array =
563 g_renew(gboolean, pagedata->format.col_autofit_array,
564 pagedata->format.col_import_array_len);
565 pagedata->format.col_import_array =
566 g_renew(gboolean, pagedata->format.col_import_array,
567 pagedata->format.col_import_array_len);
568 old_part = (col_import_array_len_old < pagedata->format.col_import_array_len)
569 ? col_import_array_len_old
570 : pagedata->format.col_import_array_len;
571 pagedata->format.col_import_count = 0;
572 for (i = 0; i < old_part; i++)
573 if (pagedata->format.col_import_array[i])
574 pagedata->format.col_import_count++;
575 for (i = old_part;
576 i < pagedata->format.col_import_array_len; i++) {
577 if (pagedata->format.col_import_count < GNM_MAX_COLS) {
578 pagedata->format.col_import_array[i] = TRUE;
579 pagedata->format.col_import_count++;
580 } else {
581 pagedata->format.col_import_array[i] = FALSE;
583 pagedata->format.col_autofit_array[i] = TRUE;
586 format_page_update_column_selection (pagedata);
588 if (old_part < renderdata->colcount)
589 msg = g_strdup_printf
590 (_("A maximum of %d columns can be imported."),
591 GNM_MAX_COLS);
593 for (i = old_part; i < renderdata->colcount; i++) {
594 GtkTreeViewColumn *column =
595 stf_preview_get_column (renderdata, i);
596 GtkWidget *button = gtk_tree_view_column_get_button (column);
598 if (NULL == g_object_get_data (G_OBJECT (column), "checkbox")) {
599 GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
600 GtkWidget *check,
601 *check_autofit = gtk_check_button_new_with_label (_("Auto fit"));
602 char * label_text = g_strdup_printf
603 (pagedata->format.col_header, i+1);
604 GOFormat const *fmt = i < (int)pagedata->parseoptions->formats->len
605 ? g_ptr_array_index (pagedata->parseoptions->formats, i)
606 : go_format_general ();
607 GtkWidget *format_label = gtk_button_new_with_label
608 (go_format_sel_format_classification (fmt));
609 GtkWidget *format_icon
610 = gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON);
612 check = gtk_check_button_new_with_label (label_text);
613 g_free (label_text);
614 gtk_button_set_image (GTK_BUTTON (format_label), format_icon);
616 g_object_set (G_OBJECT (stf_preview_get_cell_renderer
617 (pagedata->format.renderdata, i)),
618 "strikethrough",
619 !pagedata->format.col_import_array[i], NULL);
620 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check),
621 pagedata->
622 format.col_import_array[i]);
623 label_text = g_strdup_printf
624 (_("If this checkbox is selected, "
625 "column %i will be imported into "
626 "Gnumeric."), i+1);
627 gtk_widget_set_tooltip_text
628 (check,
629 label_text);
630 gtk_widget_set_tooltip_text
631 (check_autofit,
632 _("If this checkbox is selected, "
633 "the width of the column will be adjusted "
634 "to the longest entry."));
635 g_free (label_text);
636 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check_autofit),
637 pagedata->
638 format.col_autofit_array[i]);
639 g_object_set_data (G_OBJECT (check), "pagedata", pagedata);
640 g_object_set_data (G_OBJECT (check_autofit), "pagedata", pagedata);
641 g_object_set_data (G_OBJECT (format_label), "pagedata", pagedata);
642 gtk_box_pack_start (GTK_BOX(vbox), check, FALSE, FALSE, 0);
643 gtk_box_pack_start (GTK_BOX(vbox), format_label, TRUE, TRUE, 0);
644 gtk_box_pack_start (GTK_BOX(vbox), check_autofit, TRUE, TRUE, 0);
645 gtk_widget_show_all (vbox);
647 gtk_tree_view_column_set_widget (column, vbox);
648 g_object_set_data (G_OBJECT (column), "pagedata", pagedata);
649 g_object_set_data (G_OBJECT (column), "checkbox", check);
650 g_object_set_data (G_OBJECT (column), "checkbox-autofit", check_autofit);
651 g_object_set_data (G_OBJECT (column), "formatlabel", format_label);
652 g_object_set_data (G_OBJECT (button),
653 "pagedata", pagedata);
654 g_object_set_data (G_OBJECT (button),
655 "checkbox", check);
656 g_object_set_data (G_OBJECT (button),
657 "formatlabel", format_label);
658 g_object_set (G_OBJECT (column), "clickable", TRUE, NULL);
660 g_signal_connect (G_OBJECT (check),
661 "toggled",
662 G_CALLBACK (cb_col_check_clicked),
663 GINT_TO_POINTER (i));
664 g_signal_connect (G_OBJECT (check_autofit),
665 "toggled",
666 G_CALLBACK (cb_col_check_autofit_clicked),
667 GINT_TO_POINTER (i));
668 g_signal_connect (G_OBJECT (format_label),
669 "clicked",
670 G_CALLBACK (cb_format_clicked),
671 GINT_TO_POINTER (i));
672 g_signal_connect (G_OBJECT (button),
673 "event",
674 G_CALLBACK (cb_col_event),
675 GINT_TO_POINTER (i));
678 g_free (msg);
681 /*************************************************************************************************
682 * SIGNAL HANDLERS
683 *************************************************************************************************/
685 static void
686 locale_changed_cb (GOLocaleSel *ls, char const *new_locale,
687 StfDialogData *pagedata)
689 g_free (pagedata->locale);
690 pagedata->locale = g_strdup (new_locale);
694 * stf_dialog_format_page_prepare
695 * @pagedata: mother struct
697 * This will prepare the widgets on the format page before
698 * the page gets displayed
700 void
701 stf_dialog_format_page_prepare (StfDialogData *data)
703 GOFormat *general = go_format_general ();
704 GPtrArray *formats = data->parseoptions->formats;
706 /* Set the trim. */
707 format_page_trim_menu_changed (NULL, data);
709 /* If necessary add new items (non-visual) */
710 while ((int)data->format.formats->len < data->format.renderdata->colcount) {
711 GOFormat const *fmt =
712 data->format.formats->len < formats->len
713 ? g_ptr_array_index (formats, data->format.formats->len)
714 : general;
715 g_ptr_array_add (data->format.formats, go_format_ref (fmt));
718 data->format.manual_change = TRUE;
719 activate_column (data, 0);
722 /*************************************************************************************************
723 * FORMAT EXPORTED FUNCTIONS
724 *************************************************************************************************/
726 void
727 stf_dialog_format_page_cleanup (StfDialogData *pagedata)
729 GPtrArray *formats = pagedata->format.formats;
730 if (formats)
731 g_ptr_array_free (formats, TRUE);
733 stf_preview_free (pagedata->format.renderdata);
734 g_free (pagedata->format.col_import_array);
735 g_free (pagedata->format.col_autofit_array);
736 pagedata->format.col_import_array = NULL;
737 pagedata->format.col_autofit_array = NULL;
738 pagedata->format.col_import_array_len = 0;
739 pagedata->format.col_import_count = 0;
742 void
743 stf_dialog_format_page_init (GtkBuilder *gui, StfDialogData *pagedata)
745 /* GtkWidget * format_hbox; */
747 g_return_if_fail (gui != NULL);
748 g_return_if_fail (pagedata != NULL);
750 /* Create/get object and fill information struct */
751 pagedata->format.col_import_array = NULL;
752 pagedata->format.col_autofit_array = NULL;
753 pagedata->format.col_import_array_len = 0;
754 pagedata->format.col_import_count = 0;
755 pagedata->format.col_header = _("Column %d");
757 pagedata->format.format_data_container = go_gtk_builder_get_widget (gui, "format_data_container");
758 pagedata->format.format_trim = go_gtk_builder_get_widget (gui, "format_trim");
759 pagedata->format.column_selection_label = go_gtk_builder_get_widget (gui, "column_selection_label");
761 pagedata->format.locale_selector =
762 GO_LOCALE_SEL (go_locale_sel_new ());
763 if (pagedata->locale && !go_locale_sel_set_locale (pagedata->format.locale_selector, pagedata->locale)) {
764 g_free (pagedata->locale);
765 pagedata->locale = go_locale_sel_get_locale (pagedata->format.locale_selector);
767 gtk_grid_attach (
768 GTK_GRID (go_gtk_builder_get_widget (gui, "locale-grid")),
769 GTK_WIDGET (pagedata->format.locale_selector),
770 3, 0, 1, 1);
771 gtk_widget_show_all (GTK_WIDGET (pagedata->format.locale_selector));
772 gtk_widget_set_sensitive
773 (GTK_WIDGET (pagedata->format.locale_selector),
774 !pagedata->fixed_locale);
776 /* Set properties */
777 pagedata->format.renderdata =
778 stf_preview_new (pagedata->format.format_data_container,
779 workbook_date_conv (wb_control_get_workbook (GNM_WBC (pagedata->wbcg))));
780 pagedata->format.formats = g_ptr_array_new_with_free_func ((GDestroyNotify)go_format_unref);
781 pagedata->format.index = -1;
782 pagedata->format.manual_change = FALSE;
784 /* Update widgets before connecting signals, see #333407. */
785 gtk_combo_box_set_active (GTK_COMBO_BOX (pagedata->format.format_trim),
787 format_page_update_column_selection (pagedata);
789 /* Connect signals */
790 g_signal_connect (G_OBJECT (pagedata->format.locale_selector),
791 "locale_changed",
792 G_CALLBACK (locale_changed_cb), pagedata);
794 g_signal_connect (G_OBJECT (pagedata->format.format_trim),
795 "changed",
796 G_CALLBACK (format_page_trim_menu_changed), pagedata);
797 g_signal_connect (G_OBJECT (pagedata->format.renderdata->tree_view),
798 "button_press_event",
799 G_CALLBACK (cb_treeview_button_press),
800 pagedata);
801 g_signal_connect (G_OBJECT (pagedata->format.renderdata->tree_view),
802 "key_press_event",
803 G_CALLBACK (cb_treeview_key_press),
804 pagedata);