1.12.42
[gnumeric.git] / src / dialogs / dialog-stf-format-page.c
blob5425e32c4f9ab52bcb4c35b402aa3f4481c85c49
1 /*
2 * dialog-stf-format-page.c : Controls the widgets on the format page of the dialog
4 * Copyright 2001 Almer S. Tigelaar <almer@gnome.org>
5 * Copyright 2003 Morten Welinder <terra@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, see <https://www.gnu.org/licenses/>.
21 #include <gnumeric-config.h>
22 #include <glib/gi18n-lib.h>
23 #include <gnumeric.h>
24 #include <libgnumeric.h>
25 #include <dialogs/dialog-stf.h>
26 #include <gnm-format.h>
27 #include <sheet.h>
28 #include <workbook.h>
29 #include <workbook-control.h>
30 #include <gui-util.h>
31 #include <gdk/gdkkeysyms.h>
33 /*************************************************************************************************
34 * MISC UTILITY FUNCTIONS
35 *************************************************************************************************/
37 static void format_page_update_preview (StfDialogData *pagedata);
39 static void
40 format_page_update_column_selection (StfDialogData *pagedata)
42 char *text = NULL;
44 if (pagedata->format.col_import_count == pagedata->format.col_import_array_len) {
45 text = g_strdup_printf (_("Importing %i columns and ignoring none."),
46 pagedata->format.col_import_count);
47 } else {
48 text = g_strdup_printf (_("Importing %i columns and ignoring %i."),
49 pagedata->format.col_import_count,
50 pagedata->format.col_import_array_len - pagedata->format.col_import_count);
53 gtk_label_set_text (GTK_LABEL (pagedata->format.column_selection_label), text);
55 g_free (text);
58 static void
59 format_page_trim_menu_changed (G_GNUC_UNUSED GtkMenu *menu,
60 StfDialogData *data)
62 StfTrimType_t trim;
63 int trimtype = gtk_combo_box_get_active (GTK_COMBO_BOX (data->format.format_trim));
65 switch (trimtype) {
66 case -1:
67 case 0:
68 trim = TRIM_TYPE_LEFT | TRIM_TYPE_RIGHT;
69 break;
70 default:
71 g_warning ("Unknown trim type selected (%d)", trimtype);
72 /* Fall through. */
73 case 1:
74 trim = TRIM_TYPE_NEVER;
75 break;
76 case 2:
77 trim = TRIM_TYPE_LEFT;
78 break;
79 case 3:
80 trim = TRIM_TYPE_RIGHT;
81 break;
84 stf_parse_options_set_trim_spaces (data->parseoptions, trim);
85 format_page_update_preview (data);
89 * More or less a copy of gtk_tree_view_clamp_column_visible.
91 static void
92 tree_view_clamp_column_visible (GtkTreeView *tree_view,
93 GtkTreeViewColumn *column)
95 GtkAdjustment *hadjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (tree_view));
96 double hval = gtk_adjustment_get_value (hadjustment);
97 double hps = gtk_adjustment_get_page_size (hadjustment);
98 GtkWidget *button = gtk_tree_view_column_get_button (column);
99 GtkAllocation ba;
101 gtk_widget_get_allocation (button, &ba);
103 if (hval + hps < ba.x + ba.width)
104 gtk_adjustment_set_value (hadjustment,
105 ba.x + ba.width - hps);
106 else if (hval > ba.x)
107 gtk_adjustment_set_value (hadjustment, ba.x);
110 static void
111 activate_column (StfDialogData *pagedata, int i)
113 GtkCellRenderer *cell;
114 GtkTreeViewColumn *column;
115 RenderData_t *renderdata = pagedata->format.renderdata;
117 cell = stf_preview_get_cell_renderer (renderdata,
118 pagedata->format.index);
119 if (cell) {
120 g_object_set (G_OBJECT (cell),
121 "background", NULL,
122 NULL);
125 pagedata->format.index = i;
127 column = stf_preview_get_column (renderdata, i);
128 if (column) {
129 tree_view_clamp_column_visible (renderdata->tree_view, column);
132 cell = stf_preview_get_cell_renderer (renderdata, i);
133 if (cell) {
134 g_object_set (G_OBJECT (cell),
135 "background", "lightgrey",
136 NULL);
137 gtk_widget_queue_draw (GTK_WIDGET (renderdata->tree_view));
140 /* FIXME: warp focus away from the header. */
143 static void
144 cb_col_check_clicked (GtkToggleButton *togglebutton, gpointer _i)
146 int i = GPOINTER_TO_INT (_i);
147 StfDialogData *pagedata =
148 g_object_get_data (G_OBJECT (togglebutton), "pagedata");
149 gboolean active = gtk_toggle_button_get_active (togglebutton);
150 GtkCellRenderer *renderer;
151 GtkTreeViewColumn* column;
152 GtkWidget *check_autofit;
154 g_return_if_fail (i < pagedata->format.col_import_array_len);
156 if (pagedata->format.col_import_array[i] == active)
157 return;
159 renderer = stf_preview_get_cell_renderer (pagedata->format.renderdata, i);
160 g_object_set (G_OBJECT (renderer), "strikethrough", !active, NULL);
161 gtk_widget_queue_draw (GTK_WIDGET (pagedata->format.renderdata->tree_view));
163 if (!active) {
164 pagedata->format.col_import_array[i] = FALSE;
165 pagedata->format.col_import_count--;
166 format_page_update_column_selection (pagedata);
167 } else {
168 if (pagedata->format.col_import_count < GNM_MAX_COLS) {
169 pagedata->format.col_import_array[i] = TRUE;
170 pagedata->format.col_import_count++;
171 format_page_update_column_selection (pagedata);
172 } else {
173 char *msg = g_strdup_printf(
174 ngettext("A maximum of %d column can be imported.",
175 "A maximum of %d columns can be imported.",
176 GNM_MAX_COLS),
177 GNM_MAX_COLS);
178 gtk_toggle_button_set_active (togglebutton, FALSE);
179 go_gtk_notice_dialog (GTK_WINDOW (pagedata->dialog),
180 GTK_MESSAGE_WARNING,
181 "%s", msg);
182 g_free (msg);
186 column = stf_preview_get_column (pagedata->format.renderdata, i);
187 check_autofit = g_object_get_data (G_OBJECT (column), "checkbox-autofit");
189 gtk_widget_set_sensitive (check_autofit, active);
190 return;
193 static void
194 cb_format_clicked (GtkButton *widget, gpointer _i)
196 int i = GPOINTER_TO_INT (_i);
197 StfDialogData *pagedata =
198 g_object_get_data (G_OBJECT (widget), "pagedata");
199 gint result;
200 GtkWidget *dialog = gtk_dialog_new_with_buttons
201 (_("Format Selector"),
202 GTK_WINDOW (pagedata->dialog),
203 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
204 GNM_STOCK_OK, GTK_RESPONSE_ACCEPT,
205 GNM_STOCK_CANCEL, GTK_RESPONSE_REJECT,
206 NULL);
207 GOFormatSel *format_selector
208 = GO_FORMAT_SEL (go_format_sel_new_full (TRUE));
209 GtkWidget *w = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
211 go_format_sel_set_style_format (format_selector, g_ptr_array_index (pagedata->format.formats, i));
212 go_format_sel_set_locale (format_selector, pagedata->locale);
213 gtk_box_pack_start (GTK_BOX (w), GTK_WIDGET (format_selector),
214 FALSE, TRUE, 5);
215 gtk_widget_show (GTK_WIDGET (format_selector));
217 result = gtk_dialog_run (GTK_DIALOG (dialog));
218 switch (result) {
219 case GTK_RESPONSE_ACCEPT: {
220 GOFormat *sf;
221 GtkTreeViewColumn* column = stf_preview_get_column (pagedata->format.renderdata, i);
222 GtkWidget *format_label = g_object_get_data (G_OBJECT (column),
223 "formatlabel");
225 sf = g_ptr_array_index (pagedata->format.formats, i);
226 go_format_unref (sf);
228 sf = go_format_ref (go_format_sel_get_fmt (format_selector));
229 gtk_button_set_label (GTK_BUTTON (format_label),
230 go_format_sel_format_classification (sf));
231 g_ptr_array_index (pagedata->format.formats, i) = sf;
233 format_page_update_preview (pagedata);
234 break;
236 default:
237 break;
239 gtk_widget_destroy (dialog);
240 return;
243 static void
244 cb_col_check_autofit_clicked (GtkToggleButton *togglebutton, gpointer _i)
246 int i = GPOINTER_TO_INT (_i);
247 StfDialogData *pagedata =
248 g_object_get_data (G_OBJECT (togglebutton), "pagedata");
249 gboolean active = gtk_toggle_button_get_active (togglebutton);
251 g_return_if_fail (i < pagedata->format.col_import_array_len);
253 pagedata->format.col_autofit_array[i] = active;
255 return;
258 static void
259 check_columns_for_import (StfDialogData *pagedata, int from, int to)
261 int i;
263 g_return_if_fail (pagedata != NULL);
264 g_return_if_fail (!(from < 0));
265 g_return_if_fail (to < pagedata->format.renderdata->colcount);
266 g_return_if_fail (to < pagedata->format.col_import_array_len);
268 for (i = from; i <= to; i++) {
269 if (!pagedata->format.col_import_array[i]) {
270 GtkTreeViewColumn* column = stf_preview_get_column (pagedata->format.renderdata, i);
271 GtkWidget *w = g_object_get_data (G_OBJECT (column), "checkbox");
272 if (pagedata->format.col_import_count >= GNM_MAX_COLS)
273 break;
274 gtk_widget_hide (w);
275 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
276 /* Note this caused a signal to be send that sets the */
277 /* pagedata fields */
278 gtk_widget_show (w);
283 static void
284 uncheck_columns_for_import (StfDialogData *pagedata, int from, int to)
286 int i;
288 g_return_if_fail (pagedata != NULL);
289 g_return_if_fail (!(from < 0));
290 g_return_if_fail (to < pagedata->format.renderdata->colcount);
291 g_return_if_fail (to < pagedata->format.col_import_array_len);
293 for (i = from; i <= to; i++) {
294 if (pagedata->format.col_import_array[i]) {
295 GtkTreeViewColumn* column = stf_preview_get_column (pagedata->format.renderdata, i);
296 GtkWidget *w = g_object_get_data (G_OBJECT (column), "checkbox");
298 gtk_widget_hide (w);
299 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), FALSE);
300 /* Note this caused a signal to be send that sets the */
301 /* pagedata fields */
302 gtk_widget_show (w);
307 static void
308 cb_popup_menu_uncheck_right (GtkWidget *widget, gpointer data)
310 StfDialogData *pagedata = data;
312 uncheck_columns_for_import (pagedata, pagedata->format.index + 1,
313 pagedata->format.renderdata->colcount - 1);
316 static void
317 cb_popup_menu_check_right (GtkWidget *widget, gpointer data)
319 StfDialogData *pagedata = data;
321 check_columns_for_import (pagedata, pagedata->format.index + 1,
322 pagedata->format.renderdata->colcount - 1);
325 static void
326 cb_popup_menu_uncheck_left (GtkWidget *widget, gpointer data)
328 StfDialogData *pagedata = data;
330 uncheck_columns_for_import (pagedata, 0, pagedata->format.index - 1);
333 static void
334 cb_popup_menu_check_left (GtkWidget *widget, gpointer data)
336 StfDialogData *pagedata = data;
338 check_columns_for_import (pagedata, 0, pagedata->format.index - 1);
341 static void
342 cb_popup_menu_extend_format (GtkWidget *widget, gpointer data)
344 StfDialogData *pagedata = data;
345 guint index = pagedata->format.index;
346 GPtrArray *formats = pagedata->format.formats;
347 GOFormat *colformat = g_ptr_array_index (formats, pagedata->format.index);
349 for (index++; index < formats->len; index++) {
350 GOFormat *sf = g_ptr_array_index (formats, index);
351 GtkTreeViewColumn* column =
352 stf_preview_get_column (pagedata->format.renderdata,
353 index);
354 GtkWidget *w = g_object_get_data (G_OBJECT (column),
355 "formatlabel");
356 go_format_unref (sf);
357 g_ptr_array_index (formats, index) = go_format_ref (colformat);
358 gtk_button_set_label (GTK_BUTTON (w),
359 go_format_sel_format_classification (colformat));
362 format_page_update_preview (data);
365 static void
366 format_context_menu (StfDialogData *pagedata,
367 GdkEvent *event,
368 int col)
370 enum {
371 COLUMN_POPUP_ITEM_IGNORE,
372 COLUMN_POPUP_ITEM_NOT_FIRST,
373 COLUMN_POPUP_ITEM_NOT_LAST,
374 COLUMN_POPUP_ITEM_ANY
377 static const struct {
378 const char *text;
379 void (*function) (GtkWidget *widget, gpointer data);
380 int flags;
381 } actions[] = {
382 { N_("Ignore all columns on right"), &cb_popup_menu_uncheck_right, COLUMN_POPUP_ITEM_NOT_LAST},
383 { N_("Ignore all columns on left"), &cb_popup_menu_uncheck_left, COLUMN_POPUP_ITEM_NOT_FIRST},
384 { N_("Import all columns on right"), &cb_popup_menu_check_right, COLUMN_POPUP_ITEM_NOT_LAST},
385 { N_("Import all columns on left"), &cb_popup_menu_check_left, COLUMN_POPUP_ITEM_NOT_FIRST},
386 { N_("Copy format to right"), &cb_popup_menu_extend_format, COLUMN_POPUP_ITEM_NOT_LAST}
389 GtkWidget *menu = gtk_menu_new ();
390 unsigned i;
392 for (i = 0; i < G_N_ELEMENTS (actions); i++) {
393 int flags = actions[i].flags;
394 GtkWidget *item = gtk_menu_item_new_with_label
395 (_(actions[i].text));
396 switch (flags) {
397 case COLUMN_POPUP_ITEM_IGNORE:
398 gtk_widget_set_sensitive (item, FALSE);
399 break;
400 case COLUMN_POPUP_ITEM_NOT_FIRST:
401 gtk_widget_set_sensitive (item, col > 0);
402 break;
403 case COLUMN_POPUP_ITEM_NOT_LAST:
404 gtk_widget_set_sensitive
405 (item, col < pagedata->format.renderdata->colcount - 1);
406 break;
407 case COLUMN_POPUP_ITEM_ANY:
408 default:
409 break;
411 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
412 gtk_widget_show (item);
413 g_signal_connect (G_OBJECT (item),
414 "activate",
415 G_CALLBACK (actions[i].function),
416 pagedata);
419 gnumeric_popup_menu (GTK_MENU (menu), event);
423 static gint
424 cb_col_event (GtkWidget *widget, GdkEvent *event, gpointer _col)
426 if (event->type == GDK_BUTTON_PRESS) {
427 GdkEventButton *event_button = &event->button;
428 StfDialogData *pagedata =
429 g_object_get_data (G_OBJECT (widget), "pagedata");
430 int col = GPOINTER_TO_INT (_col);
432 activate_column (pagedata, col);
434 if (event_button->button == 1) {
435 GtkWidget *check = g_object_get_data (G_OBJECT (widget), "checkbox");
436 GtkAllocation a;
438 * We use overlapping buttons and that does not
439 * actually work...
441 * In a square area the height of the hbox, click the
442 * checkbox.
444 int xmax;
446 gtk_widget_get_allocation
447 (gtk_bin_get_child (GTK_BIN (widget)),
448 &a);
449 xmax = a.height;
450 if (event_button->x <= xmax)
451 gtk_button_clicked (GTK_BUTTON (check));
452 } else if (event_button->button == 3) {
453 format_context_menu (pagedata, event, col);
455 return TRUE;
458 return FALSE;
461 static gint
462 cb_treeview_button_press (GtkWidget *treeview,
463 GdkEventButton *event,
464 StfDialogData *pagedata)
466 if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
467 int dx, col;
468 stf_preview_find_column (pagedata->format.renderdata, (int)event->x, &col, &dx);
469 activate_column (pagedata, col);
470 return TRUE;
471 } else if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
472 int dx, col;
473 stf_preview_find_column (pagedata->format.renderdata, (int)event->x, &col, &dx);
474 activate_column (pagedata, col);
475 format_context_menu (pagedata, (GdkEvent*)event, col);
476 return TRUE;
479 return FALSE;
482 static gint
483 cb_treeview_key_press (GtkWidget *treeview,
484 GdkEventKey *event,
485 StfDialogData *pagedata)
487 if (event->type == GDK_KEY_PRESS) {
488 switch (event->keyval) {
489 case GDK_KEY_Left:
490 case GDK_KEY_KP_Left:
491 if (pagedata->format.index > 0)
492 activate_column (pagedata,
493 pagedata->format.index - 1);
494 return TRUE;
496 case GDK_KEY_Right:
497 case GDK_KEY_KP_Right:
498 if (pagedata->format.index + 1 < (int)pagedata->format.formats->len)
499 activate_column (pagedata,
500 pagedata->format.index + 1);
501 return TRUE;
503 case GDK_KEY_space:
504 case GDK_KEY_Return: {
505 GtkTreeViewColumn *column = stf_preview_get_column
506 (pagedata->format.renderdata,
507 pagedata->format.index);
508 GtkToggleButton *button =
509 g_object_get_data (G_OBJECT (column),
510 "checkbox");
511 gtk_toggle_button_set_active
512 (button,
513 !gtk_toggle_button_get_active (button));
514 return TRUE;
517 default:
518 ; /* Nothing. */
522 return FALSE;
526 * format_page_update_preview
527 * @pagedata: mother struct
529 * Will simply utilize the preview rendering functions to update
530 * the preview
532 * returns : nothing
534 static void
535 format_page_update_preview (StfDialogData *pagedata)
537 RenderData_t *renderdata = pagedata->format.renderdata;
538 unsigned int ui;
539 int i;
540 int col_import_array_len_old, old_part;
541 GStringChunk *lines_chunk;
542 char *msg = NULL;
544 stf_preview_colformats_clear (renderdata);
545 for (ui = 0; ui < pagedata->format.formats->len; ui++) {
546 GOFormat *sf = g_ptr_array_index (pagedata->format.formats, ui);
547 stf_preview_colformats_add (renderdata, sf);
550 lines_chunk = g_string_chunk_new (100 * 1024);
551 stf_preview_set_lines (renderdata, lines_chunk,
552 stf_parse_general (pagedata->parseoptions,
553 lines_chunk,
554 pagedata->cur,
555 pagedata->cur_end));
557 col_import_array_len_old = pagedata->format.col_import_array_len;
558 pagedata->format.col_import_array_len = renderdata->colcount;
560 pagedata->format.col_autofit_array =
561 g_renew(gboolean, pagedata->format.col_autofit_array,
562 pagedata->format.col_import_array_len);
563 pagedata->format.col_import_array =
564 g_renew(gboolean, pagedata->format.col_import_array,
565 pagedata->format.col_import_array_len);
566 old_part = (col_import_array_len_old < pagedata->format.col_import_array_len)
567 ? col_import_array_len_old
568 : pagedata->format.col_import_array_len;
569 pagedata->format.col_import_count = 0;
570 for (i = 0; i < old_part; i++)
571 if (pagedata->format.col_import_array[i])
572 pagedata->format.col_import_count++;
573 for (i = old_part;
574 i < pagedata->format.col_import_array_len; i++) {
575 if (pagedata->format.col_import_count < GNM_MAX_COLS) {
576 pagedata->format.col_import_array[i] = TRUE;
577 pagedata->format.col_import_count++;
578 } else {
579 pagedata->format.col_import_array[i] = FALSE;
581 pagedata->format.col_autofit_array[i] = TRUE;
584 format_page_update_column_selection (pagedata);
586 if (old_part < renderdata->colcount)
587 msg = g_strdup_printf
588 (_("A maximum of %d columns can be imported."),
589 GNM_MAX_COLS);
591 for (i = old_part; i < renderdata->colcount; i++) {
592 GtkTreeViewColumn *column =
593 stf_preview_get_column (renderdata, i);
594 GtkWidget *button = gtk_tree_view_column_get_button (column);
596 if (NULL == g_object_get_data (G_OBJECT (column), "checkbox")) {
597 GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
598 GtkWidget *check,
599 *check_autofit = gtk_check_button_new_with_label (_("Auto fit"));
600 char * label_text = g_strdup_printf
601 (pagedata->format.col_header, i+1);
602 GOFormat const *fmt = i < (int)pagedata->parseoptions->formats->len
603 ? g_ptr_array_index (pagedata->parseoptions->formats, i)
604 : go_format_general ();
605 GtkWidget *format_label = gtk_button_new_with_label
606 (go_format_sel_format_classification (fmt));
607 GtkWidget *format_icon
608 = gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON);
610 check = gtk_check_button_new_with_label (label_text);
611 g_free (label_text);
612 gtk_button_set_image (GTK_BUTTON (format_label), format_icon);
614 g_object_set (G_OBJECT (stf_preview_get_cell_renderer
615 (pagedata->format.renderdata, i)),
616 "strikethrough",
617 !pagedata->format.col_import_array[i], NULL);
618 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check),
619 pagedata->
620 format.col_import_array[i]);
621 label_text = g_strdup_printf
622 (_("If this checkbox is selected, "
623 "column %i will be imported into "
624 "Gnumeric."), i+1);
625 gtk_widget_set_tooltip_text
626 (check,
627 label_text);
628 gtk_widget_set_tooltip_text
629 (check_autofit,
630 _("If this checkbox is selected, "
631 "the width of the column will be adjusted "
632 "to the longest entry."));
633 g_free (label_text);
634 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check_autofit),
635 pagedata->
636 format.col_autofit_array[i]);
637 g_object_set_data (G_OBJECT (check), "pagedata", pagedata);
638 g_object_set_data (G_OBJECT (check_autofit), "pagedata", pagedata);
639 g_object_set_data (G_OBJECT (format_label), "pagedata", pagedata);
640 gtk_box_pack_start (GTK_BOX(vbox), check, FALSE, FALSE, 0);
641 gtk_box_pack_start (GTK_BOX(vbox), format_label, TRUE, TRUE, 0);
642 gtk_box_pack_start (GTK_BOX(vbox), check_autofit, TRUE, TRUE, 0);
643 gtk_widget_show_all (vbox);
645 gtk_tree_view_column_set_widget (column, vbox);
646 g_object_set_data (G_OBJECT (column), "pagedata", pagedata);
647 g_object_set_data (G_OBJECT (column), "checkbox", check);
648 g_object_set_data (G_OBJECT (column), "checkbox-autofit", check_autofit);
649 g_object_set_data (G_OBJECT (column), "formatlabel", format_label);
650 g_object_set_data (G_OBJECT (button),
651 "pagedata", pagedata);
652 g_object_set_data (G_OBJECT (button),
653 "checkbox", check);
654 g_object_set_data (G_OBJECT (button),
655 "formatlabel", format_label);
656 g_object_set (G_OBJECT (column), "clickable", TRUE, NULL);
658 g_signal_connect (G_OBJECT (check),
659 "toggled",
660 G_CALLBACK (cb_col_check_clicked),
661 GINT_TO_POINTER (i));
662 g_signal_connect (G_OBJECT (check_autofit),
663 "toggled",
664 G_CALLBACK (cb_col_check_autofit_clicked),
665 GINT_TO_POINTER (i));
666 g_signal_connect (G_OBJECT (format_label),
667 "clicked",
668 G_CALLBACK (cb_format_clicked),
669 GINT_TO_POINTER (i));
670 g_signal_connect (G_OBJECT (button),
671 "event",
672 G_CALLBACK (cb_col_event),
673 GINT_TO_POINTER (i));
676 g_free (msg);
679 /*************************************************************************************************
680 * SIGNAL HANDLERS
681 *************************************************************************************************/
683 static void
684 locale_changed_cb (GOLocaleSel *ls, char const *new_locale,
685 StfDialogData *pagedata)
687 g_free (pagedata->locale);
688 pagedata->locale = g_strdup (new_locale);
692 * stf_dialog_format_page_prepare
693 * @pagedata: mother struct
695 * This will prepare the widgets on the format page before
696 * the page gets displayed
698 void
699 stf_dialog_format_page_prepare (StfDialogData *data)
701 GOFormat *general = go_format_general ();
702 GPtrArray *formats = data->parseoptions->formats;
704 /* Set the trim. */
705 format_page_trim_menu_changed (NULL, data);
707 /* If necessary add new items (non-visual) */
708 while ((int)data->format.formats->len < data->format.renderdata->colcount) {
709 GOFormat const *fmt =
710 data->format.formats->len < formats->len
711 ? g_ptr_array_index (formats, data->format.formats->len)
712 : general;
713 g_ptr_array_add (data->format.formats, go_format_ref (fmt));
716 data->format.manual_change = TRUE;
717 activate_column (data, 0);
720 /*************************************************************************************************
721 * FORMAT EXPORTED FUNCTIONS
722 *************************************************************************************************/
724 void
725 stf_dialog_format_page_cleanup (StfDialogData *pagedata)
727 GPtrArray *formats = pagedata->format.formats;
728 if (formats)
729 g_ptr_array_free (formats, TRUE);
731 stf_preview_free (pagedata->format.renderdata);
732 g_free (pagedata->format.col_import_array);
733 g_free (pagedata->format.col_autofit_array);
734 pagedata->format.col_import_array = NULL;
735 pagedata->format.col_autofit_array = NULL;
736 pagedata->format.col_import_array_len = 0;
737 pagedata->format.col_import_count = 0;
740 void
741 stf_dialog_format_page_init (GtkBuilder *gui, StfDialogData *pagedata)
743 /* GtkWidget * format_hbox; */
745 g_return_if_fail (gui != NULL);
746 g_return_if_fail (pagedata != NULL);
748 /* Create/get object and fill information struct */
749 pagedata->format.col_import_array = NULL;
750 pagedata->format.col_autofit_array = NULL;
751 pagedata->format.col_import_array_len = 0;
752 pagedata->format.col_import_count = 0;
753 pagedata->format.col_header = _("Column %d");
755 pagedata->format.format_data_container = go_gtk_builder_get_widget (gui, "format_data_container");
756 pagedata->format.format_trim = go_gtk_builder_get_widget (gui, "format_trim");
757 pagedata->format.column_selection_label = go_gtk_builder_get_widget (gui, "column_selection_label");
759 pagedata->format.locale_selector =
760 GO_LOCALE_SEL (go_locale_sel_new ());
761 if (pagedata->locale && !go_locale_sel_set_locale (pagedata->format.locale_selector, pagedata->locale)) {
762 g_free (pagedata->locale);
763 pagedata->locale = go_locale_sel_get_locale (pagedata->format.locale_selector);
765 gtk_grid_attach (
766 GTK_GRID (go_gtk_builder_get_widget (gui, "locale-grid")),
767 GTK_WIDGET (pagedata->format.locale_selector),
768 3, 0, 1, 1);
769 gtk_widget_show_all (GTK_WIDGET (pagedata->format.locale_selector));
770 gtk_widget_set_sensitive
771 (GTK_WIDGET (pagedata->format.locale_selector),
772 !pagedata->fixed_locale);
774 /* Set properties */
775 pagedata->format.renderdata =
776 stf_preview_new (pagedata->format.format_data_container,
777 workbook_date_conv (wb_control_get_workbook (GNM_WBC (pagedata->wbcg))));
778 pagedata->format.formats = g_ptr_array_new_with_free_func ((GDestroyNotify)go_format_unref);
779 pagedata->format.index = -1;
780 pagedata->format.manual_change = FALSE;
782 /* Update widgets before connecting signals, see #333407. */
783 gtk_combo_box_set_active (GTK_COMBO_BOX (pagedata->format.format_trim),
785 format_page_update_column_selection (pagedata);
787 /* Connect signals */
788 g_signal_connect (G_OBJECT (pagedata->format.locale_selector),
789 "locale_changed",
790 G_CALLBACK (locale_changed_cb), pagedata);
792 g_signal_connect (G_OBJECT (pagedata->format.format_trim),
793 "changed",
794 G_CALLBACK (format_page_trim_menu_changed), pagedata);
795 g_signal_connect (G_OBJECT (pagedata->format.renderdata->tree_view),
796 "button_press_event",
797 G_CALLBACK (cb_treeview_button_press),
798 pagedata);
799 g_signal_connect (G_OBJECT (pagedata->format.renderdata->tree_view),
800 "key_press_event",
801 G_CALLBACK (cb_treeview_key_press),
802 pagedata);