GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / widgets / gnm-dao.c
blob7acd52f0fbc91ad7e49d151e3d62ebc455ba73b9
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /**
3 * gnm-dao.c: Implements a widget to specify tool output location.
5 * Copyright (c) 2003 Andreas J. Guelzow <aguelzow@taliesin.ca>
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/>.
19 **/
21 #include <gnumeric-config.h>
22 #include "gnm-dao.h"
23 #include "gnumeric-expr-entry.h"
24 #include <tools/dao.h>
25 #include <value.h>
26 #include <workbook-control.h>
28 #include <goffice/goffice.h>
29 #include <gsf/gsf-impl-utils.h>
30 #include <glib/gi18n-lib.h>
32 struct _GnmDao {
33 GtkBox box;
34 GtkBuilder *gui;
36 GtkWidget *new_sheet;
37 GtkWidget *new_workbook;
38 GtkWidget *output_range;
39 GtkWidget *in_place;
40 GnmExprEntry *output_entry;
41 GtkWidget *clear_outputrange_button;
42 GtkWidget *retain_format_button;
43 GtkWidget *retain_comments_button;
44 GtkWidget *put_menu;
46 WBCGtk *wbcg;
49 typedef struct {
50 GtkBoxClass parent_class;
52 void (*gnm_dao_changed) (GnmDao *gdao);
53 void (*gnm_dao_activate) (GnmDao *gdao);
54 } GnmDaoClass;
56 static GtkBoxClass *gnm_dao_parent_class;
58 /* Signals we emit */
59 enum {
60 GNM_DAO_CHANGED,
61 GNM_DAO_ACTIVATE,
62 LAST_SIGNAL
65 static guint gnm_dao_signals[LAST_SIGNAL] = { 0 };
67 static void
68 gnm_dao_init (GnmDao *gdao)
70 GtkWidget *toplevel;
72 gdao->gui = gnm_gtk_builder_load ("res:ui/dao.ui", NULL, NULL);
73 if (gdao->gui == NULL)
74 return;
76 toplevel = go_gtk_builder_get_widget (gdao->gui, "output-grid");
78 gdao->new_sheet = go_gtk_builder_get_widget (gdao->gui,
79 "newsheet-button");
80 gdao->new_workbook = go_gtk_builder_get_widget (gdao->gui,
81 "newworkbook-button");
82 gdao->output_range = go_gtk_builder_get_widget (gdao->gui,
83 "outputrange-button");
84 gdao->in_place = go_gtk_builder_get_widget (gdao->gui,
85 "inplace-button");
86 gdao->clear_outputrange_button = go_gtk_builder_get_widget
87 (gdao->gui, "clear_outputrange_button");
88 gdao->retain_format_button = go_gtk_builder_get_widget
89 (gdao->gui, "retain_format_button");
90 gdao->retain_comments_button = go_gtk_builder_get_widget
91 (gdao->gui, "retain_comments_button");
92 gdao->put_menu = go_gtk_builder_get_widget
93 (gdao->gui, "put_menu");
94 gtk_combo_box_set_active
95 (GTK_COMBO_BOX (gdao->put_menu), 1);
96 gdao->output_entry = NULL;
97 gdao->wbcg = NULL;
99 gtk_container_add (GTK_CONTAINER (gdao), toplevel);
102 static void
103 gnm_dao_destroy (GtkWidget *widget)
105 GnmDao *gdao = GNM_DAO (widget);
107 g_clear_object (&gdao->gui);
109 ((GtkWidgetClass *)(gnm_dao_parent_class))->destroy (widget);
112 static void
113 gnm_dao_class_init (GObjectClass *klass)
115 ((GtkWidgetClass *)(klass))->destroy = gnm_dao_destroy;
117 gnm_dao_parent_class = g_type_class_peek (gtk_box_get_type ());
119 gnm_dao_signals[GNM_DAO_CHANGED] =
120 g_signal_new ("readiness-changed",
121 G_OBJECT_CLASS_TYPE (klass),
122 G_SIGNAL_RUN_LAST,
123 G_STRUCT_OFFSET (GnmDaoClass, gnm_dao_changed),
124 NULL, NULL,
125 g_cclosure_marshal_VOID__VOID,
126 G_TYPE_NONE, 0);
127 gnm_dao_signals[GNM_DAO_ACTIVATE] =
128 g_signal_new ("activate",
129 G_OBJECT_CLASS_TYPE (klass),
130 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
131 G_STRUCT_OFFSET (GnmDaoClass, gnm_dao_activate),
132 NULL, NULL,
133 g_cclosure_marshal_VOID__VOID,
134 G_TYPE_NONE, 0);
137 GSF_CLASS (GnmDao, gnm_dao,
138 gnm_dao_class_init, gnm_dao_init, GTK_TYPE_BOX)
140 static void
141 tool_set_focus_output_range (G_GNUC_UNUSED GtkWidget *widget,
142 G_GNUC_UNUSED GdkEventFocus *event,
143 GnmDao *gdao)
145 gtk_toggle_button_set_active
146 (GTK_TOGGLE_BUTTON (gdao->output_range), TRUE);
149 static void
150 cb_focus_on_entry (GtkWidget *widget, GtkWidget *entry)
152 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
153 gtk_widget_grab_focus
154 (GTK_WIDGET (gnm_expr_entry_get_entry
155 (GNM_EXPR_ENTRY (entry))));
158 static const char *dao_group[] = {
159 "newsheet-button",
160 "newworkbook-button",
161 "outputrange-button",
162 "inplace-button",
163 NULL
166 static void
167 cb_emit_readiness_changed (G_GNUC_UNUSED GtkWidget *dummy, GnmDao *gdao)
169 g_signal_emit_by_name (G_OBJECT (gdao), "readiness-changed");
172 static void
173 cb_emit_activate (G_GNUC_UNUSED GtkWidget *dummy, GnmDao *gdao)
175 g_signal_emit_by_name (G_OBJECT (gdao), "activate");
178 static void
179 cb_set_sensitivity (G_GNUC_UNUSED GtkWidget *dummy, GnmDao *gdao)
181 int grp_val = gnm_gui_group_value (gdao->gui, dao_group);
183 gtk_widget_set_sensitive (gdao->clear_outputrange_button,
184 (grp_val == 2));
185 gtk_widget_set_sensitive (gdao->retain_format_button,
186 (grp_val == 2));
187 gtk_widget_set_sensitive (gdao->retain_comments_button,
188 (grp_val == 2));
192 static void
193 gnm_dao_setup_signals (GnmDao *gdao)
195 g_signal_connect (G_OBJECT (gdao->output_range),
196 "toggled",
197 G_CALLBACK (cb_focus_on_entry),
198 gdao->output_entry);
199 g_signal_connect
200 (G_OBJECT (gnm_expr_entry_get_entry
201 (GNM_EXPR_ENTRY (gdao->output_entry))),
202 "focus-in-event",
203 G_CALLBACK (tool_set_focus_output_range), gdao);
204 g_signal_connect_after (G_OBJECT (gdao->output_entry),
205 "changed",
206 G_CALLBACK (cb_set_sensitivity), gdao);
207 g_signal_connect_after (G_OBJECT (gdao->output_entry),
208 "changed",
209 G_CALLBACK (cb_emit_readiness_changed),
210 gdao);
211 g_signal_connect (G_OBJECT (gdao->output_entry),
212 "activate",
213 G_CALLBACK (cb_emit_activate), gdao);
214 g_signal_connect_after (G_OBJECT (gdao->output_range),
215 "toggled",
216 G_CALLBACK (cb_set_sensitivity), gdao);
217 g_signal_connect_after (G_OBJECT (gdao->output_range),
218 "toggled",
219 G_CALLBACK (cb_emit_readiness_changed),
220 gdao);
223 GtkWidget *
224 gnm_dao_new (WBCGtk *wbcg, gchar *inplace_str)
226 GnmDao *gdao = GNM_DAO (g_object_new (GNM_DAO_TYPE, NULL));
227 GtkGrid *grid;
229 g_return_val_if_fail (wbcg != NULL, NULL);
230 gdao->wbcg = wbcg;
232 /* Create the output range expression entry */
233 grid = GTK_GRID (go_gtk_builder_get_widget (gdao->gui, "output-grid"));
234 gdao->output_entry = gnm_expr_entry_new (wbcg, TRUE);
235 gnm_expr_entry_set_flags (gdao->output_entry,
236 GNM_EE_SINGLE_RANGE, GNM_EE_MASK);
237 gtk_widget_set_hexpand (GTK_WIDGET (gdao->output_entry), TRUE);
238 gtk_grid_attach (grid, GTK_WIDGET (gdao->output_entry),
239 1, 3, 1, 1);
240 go_atk_setup_label (gdao->output_range,
241 GTK_WIDGET (gdao->output_entry));
242 gtk_widget_show (GTK_WIDGET (gdao->output_entry));
243 /* Finished creating the output range expression entry */
245 gnm_dao_set_inplace (gdao, inplace_str);
246 gnm_dao_setup_signals (gdao);
247 cb_set_sensitivity (NULL, gdao);
249 return GTK_WIDGET (gdao);
252 void
253 gnm_dao_set_inplace (GnmDao *gdao, gchar *inplace_str)
255 g_return_if_fail (gdao != NULL);
257 if (inplace_str) {
258 gtk_button_set_label (GTK_BUTTON (gdao->in_place),
259 inplace_str);
260 gtk_widget_show (gdao->in_place);
261 } else
262 gtk_widget_hide (gdao->in_place);
265 gboolean
266 gnm_dao_get_data (GnmDao *gdao, data_analysis_output_t **dao)
268 gboolean dao_ready = FALSE;
269 int grp_val;
271 g_return_val_if_fail (gdao != NULL, FALSE);
273 grp_val = gnm_gui_group_value (gdao->gui, dao_group);
275 dao_ready = ((grp_val != 2) ||
276 gnm_expr_entry_is_cell_ref
277 (GNM_EXPR_ENTRY (gdao->output_entry),
278 wb_control_cur_sheet (GNM_WBC (gdao->wbcg)),
279 TRUE));
281 if (dao_ready && NULL != dao) {
282 GtkWidget *button;
283 GnmValue *output_range = NULL;
285 switch (grp_val) {
286 case 0:
287 default:
288 *dao = dao_init_new_sheet (*dao);
289 break;
290 case 1:
291 *dao = dao_init (*dao, NewWorkbookOutput);
292 break;
293 case 2:
294 output_range = gnm_expr_entry_parse_as_value
295 (GNM_EXPR_ENTRY (gdao->output_entry),
296 wb_control_cur_sheet (GNM_WBC
297 (gdao->wbcg)));
298 *dao = dao_init (*dao, RangeOutput);
299 dao_load_from_value (*dao, output_range);
300 value_release (output_range);
301 break;
302 case 3:
303 (*dao) = dao_init ((*dao), InPlaceOutput);
304 /* It is the callers responsibility to fill the */
305 /* dao with the appropriate range. */
306 break;
309 button = go_gtk_builder_get_widget (gdao->gui, "autofit_button");
310 (*dao)->autofit_flag = gtk_toggle_button_get_active (
311 GTK_TOGGLE_BUTTON (button));
313 (*dao)->clear_outputrange = gtk_toggle_button_get_active (
314 GTK_TOGGLE_BUTTON (gdao->clear_outputrange_button));
315 (*dao)->retain_format = gtk_toggle_button_get_active (
316 GTK_TOGGLE_BUTTON (gdao->retain_format_button));
317 (*dao)->retain_comments = gtk_toggle_button_get_active (
318 GTK_TOGGLE_BUTTON (gdao->retain_comments_button));
320 (*dao)->put_formulas
321 = (gtk_combo_box_get_active
322 (GTK_COMBO_BOX (gdao->put_menu))
323 != 0);
326 return dao_ready;
329 gboolean
330 gnm_dao_is_ready (GnmDao *gdao)
332 return gnm_dao_get_data (gdao, NULL);
335 gboolean
336 gnm_dao_is_finite (GnmDao *gdao)
338 int grp_val;
340 g_return_val_if_fail (gdao != NULL, FALSE);
342 grp_val = gnm_gui_group_value (gdao->gui, dao_group);
343 return ((grp_val == 2) || (grp_val == 3));
347 void
348 gnm_dao_set_put (GnmDao *gdao, gboolean show_put, gboolean put_formulas)
350 g_return_if_fail (gdao != NULL);
352 gtk_combo_box_set_active
353 (GTK_COMBO_BOX (gdao->put_menu), put_formulas ? 1 : 0);
354 gtk_widget_set_sensitive (GTK_WIDGET (gdao->put_menu), show_put);
357 void
358 gnm_dao_load_range (GnmDao *gdao, GnmRange const *range)
360 g_return_if_fail (gdao != NULL);
362 gnm_expr_entry_load_from_range
363 (gdao->output_entry,
364 wb_control_cur_sheet (GNM_WBC (gdao->wbcg)),
365 range);
368 void
369 gnm_dao_focus_output_range (GnmDao *gdao)
371 gtk_toggle_button_set_active
372 (GTK_TOGGLE_BUTTON (gdao->output_range), TRUE);