1.12.42
[gnumeric.git] / src / dialogs / dialog-data-table.c
blobd5e43ccc4934a615f3f55bb55f8c471daf54c3de
2 /*
3 * dialog-data-table.c: Create a Data Table
5 * Copyright (C) 2006-2007 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) version 3.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
22 #include <gnumeric-config.h>
23 #include <gnumeric.h>
24 #include <dialogs/dialogs.h>
25 #include <dialogs/help.h>
27 #include <expr.h>
28 #include <selection.h>
29 #include <sheet.h>
30 #include <sheet-view.h>
31 #include <workbook.h>
32 #include <workbook-control.h>
33 #include <wbc-gtk.h>
34 #include <gui-util.h>
35 #include <parse-util.h>
36 #include <commands.h>
37 #include <ranges.h>
38 #include <widgets/gnm-expr-entry.h>
39 #include <glib/gi18n.h>
41 #define DIALOG_DATA_TABLE_KEY "dialog-data-table"
43 typedef struct {
44 GtkBuilder *gui;
45 GtkWidget *dialog;
46 GnmExprEntry *row_entry, *col_entry;
48 WBCGtk *wbcg;
49 Sheet *sheet;
50 GnmRange input_range;
51 } GnmDialogDataTable;
53 static void
54 cb_data_table_destroy (GnmDialogDataTable *state)
56 if (state->gui != NULL)
57 g_object_unref (state->gui);
58 g_free (state);
61 static GnmExprEntry *
62 init_entry (GnmDialogDataTable *state, int row)
64 GnmExprEntry *gee = gnm_expr_entry_new (state->wbcg, TRUE);
65 GtkWidget *grid = go_gtk_builder_get_widget (state->gui, "table-grid");
67 g_return_val_if_fail (grid != NULL, NULL);
69 gnm_expr_entry_set_flags (gee,
70 GNM_EE_SINGLE_RANGE | GNM_EE_SHEET_OPTIONAL | GNM_EE_FORCE_REL_REF,
71 GNM_EE_MASK);
72 g_object_set (G_OBJECT (gee), "with-icon", TRUE, NULL);
73 gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET (gee), 1, row, 1, 1);
74 return gee;
77 static void
78 cb_data_table_response (GtkWidget *dialog, gint response_id, GnmDialogDataTable *state)
80 if (response_id == GTK_RESPONSE_HELP)
81 return;
82 if (response_id == GTK_RESPONSE_OK)
83 cmd_create_data_table (GNM_WBC (state->wbcg),
84 state->sheet, &state->input_range,
85 gnm_expr_entry_get_text (state->col_entry),
86 gnm_expr_entry_get_text (state->row_entry));
87 gtk_widget_destroy (dialog);
90 static gboolean
91 data_table_init (GnmDialogDataTable *state, WBCGtk *wbcg)
93 state->gui = gnm_gtk_builder_load ("res:ui/data-table.ui", NULL, GO_CMD_CONTEXT (wbcg));
94 if (state->gui == NULL)
95 return TRUE;
97 state->dialog = go_gtk_builder_get_widget (state->gui, "DataTable");
98 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state->dialog),
99 state->wbcg,
100 GNM_DIALOG_DESTROY_CURRENT_SHEET_REMOVED);
102 state->row_entry = init_entry (state, 0);
103 state->col_entry = init_entry (state, 1);
105 g_signal_connect (G_OBJECT (state->dialog), "response",
106 G_CALLBACK (cb_data_table_response), state);
107 gnm_init_help_button (
108 go_gtk_builder_get_widget (state->gui, "help"),
109 GNUMERIC_HELP_LINK_DATA_TABLE);
111 /* a candidate for merging into attach guru */
112 gnm_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
113 DIALOG_DATA_TABLE_KEY);
114 go_gtk_nonmodal_dialog (wbcg_toplevel (state->wbcg),
115 GTK_WINDOW (state->dialog));
117 wbc_gtk_attach_guru (state->wbcg, state->dialog);
118 g_object_set_data_full (G_OBJECT (state->dialog),
119 "state", state, (GDestroyNotify)cb_data_table_destroy);
121 gtk_widget_show_all (GTK_WIDGET (state->dialog));
123 return FALSE;
126 void
127 dialog_data_table (WBCGtk *wbcg)
129 GnmDialogDataTable *state;
130 GnmRange const *r;
131 GnmRange input_range;
132 SheetView *sv;
133 Sheet *sheet;
135 g_return_if_fail (wbcg != NULL);
137 if (wbc_gtk_get_guru (wbcg) ||
138 gnm_dialog_raise_if_exists (wbcg, DIALOG_DATA_TABLE_KEY))
139 return;
141 sv = wb_control_cur_sheet_view (GNM_WBC (wbcg));
142 r = selection_first_range (sv, GO_CMD_CONTEXT (wbcg), _("Create Data Table"));
143 if (NULL == r)
144 return;
145 if (range_width (r) <= 1 || range_height (r) <= 1) {
146 GError *msg = g_error_new (go_error_invalid(), 0,
147 _("The selection must have more than 1 column and row to create a Data Table."));
148 go_cmd_context_error (GO_CMD_CONTEXT (wbcg), msg);
149 g_error_free (msg);
150 return;
152 input_range = *r;
153 input_range.start.col++;
154 input_range.start.row++;
155 sheet = sv_sheet (sv);
156 if (sheet_range_splits_region (sheet, &input_range, NULL,
157 GO_CMD_CONTEXT (wbcg), _("Data Table")))
158 return;
159 if (cmd_cell_range_is_locked_effective
160 (sheet, &input_range, GNM_WBC (wbcg),
161 _("Data Table")))
162 return;
165 state = g_new0 (GnmDialogDataTable, 1);
166 state->wbcg = wbcg;
167 state->sheet = sheet;
168 state->input_range = input_range;
169 if (data_table_init (state, wbcg)) {
170 go_gtk_notice_dialog (wbcg_toplevel (wbcg), GTK_MESSAGE_ERROR,
171 _("Could not create the Data Table definition dialog."));
172 g_free (state);