1 /*
2  * analysis-auto-expression.c:
3  *
4  * Author:
5  *   Andreas J. Guelzow  <aguelzow@pyrshep.ca>
6  *
7  * (C) Copyright 2009 by Andreas J. Guelzow  <aguelzow@pyrshep.ca>
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #include <gnumeric-config.h>
25 #include <glib/gi18n-lib.h>
26 #include <gnumeric.h>
27 #include <tools/analysis-auto-expression.h>
28 #include <tools/analysis-tools.h>
29 #include <value.h>
30 #include <ranges.h>
31 #include <expr.h>
32 #include <func.h>
33 #include <numbers.h>
34 
35 static gboolean
analysis_tool_auto_expression_engine_run(data_analysis_output_t * dao,analysis_tools_data_auto_expression_t * info)36 analysis_tool_auto_expression_engine_run (data_analysis_output_t *dao,
37 				      analysis_tools_data_auto_expression_t *info)
38 {
39 	guint     col;
40 	GSList *data = info->base.input;
41 
42 	if (info->below) {
43 		for (col = 0; data != NULL; data = data->next, col++)
44 			dao_set_cell_expr
45 				(dao, col, 0,
46 				 gnm_expr_new_funcall1
47 				 (info->func,
48 				  gnm_expr_new_constant (value_dup (data->data))));
49 
50 		if (info->multiple)
51 			dao_set_cell_expr
52 				(dao, col, 0,
53 				 gnm_expr_new_funcall1
54 				 (info->func,
55 				  make_rangeref (- col, 0, -1, 0)));
56 	} else {
57 		for (col = 0; data != NULL; data = data->next, col++)
58 			dao_set_cell_expr
59 				(dao, 0, col,
60 				 gnm_expr_new_funcall1
61 				 (info->func,
62 				  gnm_expr_new_constant (value_dup (data->data))));
63 
64 		if (info->multiple)
65 			dao_set_cell_expr
66 				(dao, 0, col,
67 				 gnm_expr_new_funcall1
68 				 (info->func,
69 				  make_rangeref (0, - col, 0, -1)));
70 	}
71 	dao_redraw_respan (dao);
72 
73 	return FALSE;
74 }
75 
76 static gboolean
analysis_tool_auto_expression_engine_clean(gpointer specs)77 analysis_tool_auto_expression_engine_clean (gpointer specs)
78 {
79 	analysis_tools_data_auto_expression_t *info = specs;
80 
81 	gnm_func_dec_usage (info->func);
82 	info->func = NULL;
83 
84 	return analysis_tool_generic_clean (specs);
85 }
86 
87 gboolean
analysis_tool_auto_expression_engine(G_GNUC_UNUSED GOCmdContext * gcc,data_analysis_output_t * dao,gpointer specs,analysis_tool_engine_t selector,gpointer result)88 analysis_tool_auto_expression_engine (G_GNUC_UNUSED GOCmdContext *gcc, data_analysis_output_t *dao, gpointer specs,
89 			      analysis_tool_engine_t selector, gpointer result)
90 {
91 	analysis_tools_data_auto_expression_t *info = specs;
92 
93 	switch (selector) {
94 	case TOOL_ENGINE_UPDATE_DESCRIPTOR:
95 		return (dao_command_descriptor
96 			(dao, _("Auto Expression (%s)"), result)
97 			== NULL);
98 	case TOOL_ENGINE_UPDATE_DAO:
99 		prepare_input_range (&info->base.input, info->base.group_by);
100 		if (info->below)
101 			dao_adjust (dao,
102 				    (info->multiple ? 1 : 0)  + g_slist_length (info->base.input),
103 				    1);
104 		else
105 			dao_adjust (dao, 1,
106 				    (info->multiple ? 1 : 0)  + g_slist_length (info->base.input));
107 		return FALSE;
108 	case TOOL_ENGINE_CLEAN_UP:
109 		return analysis_tool_auto_expression_engine_clean (specs);
110 	case TOOL_ENGINE_LAST_VALIDITY_CHECK:
111 		return FALSE;
112 	case TOOL_ENGINE_PREPARE_OUTPUT_RANGE:
113 		dao_prepare_output (NULL, dao, _("Auto Expression"));
114 		return FALSE;
115 	case TOOL_ENGINE_FORMAT_OUTPUT_RANGE:
116 		return dao_format_output (dao, _("Auto Expression"));
117 	case TOOL_ENGINE_PERFORM_CALC:
118 	default:
119 		return analysis_tool_auto_expression_engine_run (dao, specs);
120 	}
121 	return TRUE;
122 }
123 
124