1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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/>.
21 #include <gnumeric-config.h>
23 #include "gnumeric-expr-entry.h"
24 #include <tools/dao.h>
26 #include <workbook-control.h>
28 #include <goffice/goffice.h>
29 #include <gsf/gsf-impl-utils.h>
30 #include <glib/gi18n-lib.h>
37 GtkWidget
*new_workbook
;
38 GtkWidget
*output_range
;
40 GnmExprEntry
*output_entry
;
41 GtkWidget
*clear_outputrange_button
;
42 GtkWidget
*retain_format_button
;
43 GtkWidget
*retain_comments_button
;
50 GtkBoxClass parent_class
;
52 void (*gnm_dao_changed
) (GnmDao
*gdao
);
53 void (*gnm_dao_activate
) (GnmDao
*gdao
);
56 static GtkBoxClass
*gnm_dao_parent_class
;
65 static guint gnm_dao_signals
[LAST_SIGNAL
] = { 0 };
68 gnm_dao_init (GnmDao
*gdao
)
72 gdao
->gui
= gnm_gtk_builder_load ("res:ui/dao.ui", NULL
, NULL
);
73 if (gdao
->gui
== NULL
)
76 toplevel
= go_gtk_builder_get_widget (gdao
->gui
, "output-grid");
78 gdao
->new_sheet
= go_gtk_builder_get_widget (gdao
->gui
,
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
,
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
;
99 gtk_container_add (GTK_CONTAINER (gdao
), toplevel
);
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
);
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
),
123 G_STRUCT_OFFSET (GnmDaoClass
, gnm_dao_changed
),
125 g_cclosure_marshal_VOID__VOID
,
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
),
133 g_cclosure_marshal_VOID__VOID
,
137 GSF_CLASS (GnmDao
, gnm_dao
,
138 gnm_dao_class_init
, gnm_dao_init
, GTK_TYPE_BOX
)
141 tool_set_focus_output_range (G_GNUC_UNUSED GtkWidget
*widget
,
142 G_GNUC_UNUSED GdkEventFocus
*event
,
145 gtk_toggle_button_set_active
146 (GTK_TOGGLE_BUTTON (gdao
->output_range
), TRUE
);
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
[] = {
160 "newworkbook-button",
161 "outputrange-button",
167 cb_emit_readiness_changed (G_GNUC_UNUSED GtkWidget
*dummy
, GnmDao
*gdao
)
169 g_signal_emit_by_name (G_OBJECT (gdao
), "readiness-changed");
173 cb_emit_activate (G_GNUC_UNUSED GtkWidget
*dummy
, GnmDao
*gdao
)
175 g_signal_emit_by_name (G_OBJECT (gdao
), "activate");
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
,
185 gtk_widget_set_sensitive (gdao
->retain_format_button
,
187 gtk_widget_set_sensitive (gdao
->retain_comments_button
,
193 gnm_dao_setup_signals (GnmDao
*gdao
)
195 g_signal_connect (G_OBJECT (gdao
->output_range
),
197 G_CALLBACK (cb_focus_on_entry
),
200 (G_OBJECT (gnm_expr_entry_get_entry
201 (GNM_EXPR_ENTRY (gdao
->output_entry
))),
203 G_CALLBACK (tool_set_focus_output_range
), gdao
);
204 g_signal_connect_after (G_OBJECT (gdao
->output_entry
),
206 G_CALLBACK (cb_set_sensitivity
), gdao
);
207 g_signal_connect_after (G_OBJECT (gdao
->output_entry
),
209 G_CALLBACK (cb_emit_readiness_changed
),
211 g_signal_connect (G_OBJECT (gdao
->output_entry
),
213 G_CALLBACK (cb_emit_activate
), gdao
);
214 g_signal_connect_after (G_OBJECT (gdao
->output_range
),
216 G_CALLBACK (cb_set_sensitivity
), gdao
);
217 g_signal_connect_after (G_OBJECT (gdao
->output_range
),
219 G_CALLBACK (cb_emit_readiness_changed
),
224 gnm_dao_new (WBCGtk
*wbcg
, gchar
*inplace_str
)
226 GnmDao
*gdao
= GNM_DAO (g_object_new (GNM_DAO_TYPE
, NULL
));
229 g_return_val_if_fail (wbcg
!= NULL
, NULL
);
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
),
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
);
253 gnm_dao_set_inplace (GnmDao
*gdao
, gchar
*inplace_str
)
255 g_return_if_fail (gdao
!= NULL
);
258 gtk_button_set_label (GTK_BUTTON (gdao
->in_place
),
260 gtk_widget_show (gdao
->in_place
);
262 gtk_widget_hide (gdao
->in_place
);
266 gnm_dao_get_data (GnmDao
*gdao
, data_analysis_output_t
**dao
)
268 gboolean dao_ready
= FALSE
;
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
)),
281 if (dao_ready
&& NULL
!= dao
) {
283 GnmValue
*output_range
= NULL
;
288 *dao
= dao_init_new_sheet (*dao
);
291 *dao
= dao_init (*dao
, NewWorkbookOutput
);
294 output_range
= gnm_expr_entry_parse_as_value
295 (GNM_EXPR_ENTRY (gdao
->output_entry
),
296 wb_control_cur_sheet (GNM_WBC
298 *dao
= dao_init (*dao
, RangeOutput
);
299 dao_load_from_value (*dao
, output_range
);
300 value_release (output_range
);
303 (*dao
) = dao_init ((*dao
), InPlaceOutput
);
304 /* It is the callers responsibility to fill the */
305 /* dao with the appropriate range. */
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
));
321 = (gtk_combo_box_get_active
322 (GTK_COMBO_BOX (gdao
->put_menu
))
330 gnm_dao_is_ready (GnmDao
*gdao
)
332 return gnm_dao_get_data (gdao
, NULL
);
336 gnm_dao_is_finite (GnmDao
*gdao
)
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));
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
);
358 gnm_dao_load_range (GnmDao
*gdao
, GnmRange
const *range
)
360 g_return_if_fail (gdao
!= NULL
);
362 gnm_expr_entry_load_from_range
364 wb_control_cur_sheet (GNM_WBC (gdao
->wbcg
)),
369 gnm_dao_focus_output_range (GnmDao
*gdao
)
371 gtk_toggle_button_set_active
372 (GTK_TOGGLE_BUTTON (gdao
->output_range
), TRUE
);