[gnumeric] scenarios: further cleanups.
- From: Morten Welinder <mortenw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnumeric] scenarios: further cleanups.
- Date: Tue, 24 Nov 2009 18:09:36 +0000 (UTC)
commit 962761c5857d98d0f6c12c57e4695c4e636b0b69
Author: Morten Welinder <terra gnome org>
Date: Tue Nov 24 13:07:08 2009 -0500
scenarios: further cleanups.
src/commands.c | 15 ++--
src/commands.h | 3 +-
src/dialogs/dialog-scenarios.c | 21 +++-
src/gnumeric.h | 2 +
src/sheet.c | 31 ++++++-
src/sheet.h | 2 +
src/tools/scenarios.c | 219 +++++++++++++++++-----------------------
src/tools/scenarios.h | 40 ++++++--
8 files changed, 177 insertions(+), 156 deletions(-)
---
diff --git a/src/commands.c b/src/commands.c
index 28d6e2a..e8904c6 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -5978,7 +5978,7 @@ cmd_scenario_add_redo (GnmCommand *cmd, WorkbookControl *wbc)
CmdScenarioAdd *me = CMD_SCENARIO_ADD (cmd);
scenario_add (me->cmd.sheet,
- scenario_copy (me->scenario, me->cmd.sheet));
+ gnm_scenario_dup (me->scenario, me->cmd.sheet));
return FALSE;
}
@@ -5989,8 +5989,7 @@ cmd_scenario_add_undo (GnmCommand *cmd,
{
CmdScenarioAdd *me = CMD_SCENARIO_ADD (cmd);
- me->cmd.sheet->scenarios = scenario_delete (me->cmd.sheet->scenarios,
- me->scenario->name);
+ scenario_delete (me->cmd.sheet, me->scenario->name);
return FALSE;
}
@@ -6000,7 +5999,7 @@ cmd_scenario_add_finalize (GObject *cmd)
{
CmdScenarioAdd *me = CMD_SCENARIO_ADD (cmd);
- scenario_free (me->scenario);
+ g_object_unref (me->scenario);
gnm_command_finalize (cmd);
}
@@ -6042,7 +6041,7 @@ cmd_scenario_mngr_redo (GnmCommand *cmd, WorkbookControl *wbc)
dao_init_new_sheet (&dao);
dao.sheet = me->cmd.sheet;
- scenario_free (me->sc->undo);
+ g_object_unref (me->sc->undo);
me->sc->undo = scenario_show (wbc, me->sc->redo, NULL, &dao);
return FALSE;
@@ -6058,7 +6057,7 @@ cmd_scenario_mngr_undo (GnmCommand *cmd,
dao_init_new_sheet (&dao);
dao.sheet = me->cmd.sheet;
- tmp = scenario_copy (me->sc->undo, dao.sheet);
+ tmp = gnm_scenario_dup (me->sc->undo, dao.sheet);
scenario_show (wbc, NULL, tmp, &dao);
return FALSE;
@@ -6069,8 +6068,8 @@ cmd_scenario_mngr_finalize (GObject *cmd)
{
CmdScenarioMngr *me = CMD_SCENARIO_MNGR (cmd);
- scenario_free (me->sc->undo);
- scenario_free (me->sc->redo);
+ g_object_unref (me->sc->undo);
+ g_object_unref (me->sc->redo);
g_free (me->sc);
gnm_command_finalize (cmd);
diff --git a/src/commands.h b/src/commands.h
index bcd47e7..b9356d5 100644
--- a/src/commands.h
+++ b/src/commands.h
@@ -112,8 +112,7 @@ gboolean cmd_define_name (WorkbookControl *wbc, char const *name,
char const *descriptor);
gboolean cmd_remove_name (WorkbookControl *wbc, GnmNamedExpr *nexpr);
-/* FIXME: figure out how to resolve these better. */
-struct GnmScenario_;
+/* FIXME: figure out how to resolve this better. */
struct _scenario_cmd_t;
gboolean cmd_scenario_add (WorkbookControl *wbc, struct GnmScenario_ *s, Sheet *sheet);
gboolean cmd_scenario_mngr (WorkbookControl *wbc, struct _scenario_cmd_t *sc, Sheet *sheet);
diff --git a/src/dialogs/dialog-scenarios.c b/src/dialogs/dialog-scenarios.c
index 5b17ab7..7a2159e 100644
--- a/src/dialogs/dialog-scenarios.c
+++ b/src/dialogs/dialog-scenarios.c
@@ -513,7 +513,7 @@ scenarios_show_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
wbc = WORKBOOK_CONTROL (state->base.wbcg);
state->scenario_state->current =
- scenario_by_name (state->base.sheet->scenarios, value, NULL),
+ gnm_sheet_get_scenario (state->base.sheet, value);
state->scenario_state->old_values =
scenario_show (wbc,
state->scenario_state->current,
@@ -531,6 +531,8 @@ scenarios_delete_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
GtkTreeModel *model;
gchar *value;
gboolean all_deleted;
+ GnmScenario *sc;
+ GList *l;
restore_old_values (state);
@@ -546,12 +548,21 @@ scenarios_delete_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 0, &value, -1);
gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
- all_deleted = scenario_mark_deleted (state->base.sheet->scenarios, value);
+
+ sc = gnm_sheet_get_scenario (state->base.sheet, value);
+ if (sc)
+ sc->marked_deleted = TRUE;
+
set_selection_state (state, FALSE);
- if (all_deleted)
- gtk_widget_set_sensitive
- (state->scenario_state->summary_button, FALSE);
+ all_deleted = TRUE;
+ for (l = state->base.sheet->scenarios; l && all_deleted; l = l->next) {
+ GnmScenario *sc = l->data;
+ all_deleted = sc->marked_deleted;
+ }
+
+ gtk_widget_set_sensitive
+ (state->scenario_state->summary_button, !all_deleted);
}
static void
diff --git a/src/gnumeric.h b/src/gnumeric.h
index 8ae267c..bd2de8d 100644
--- a/src/gnumeric.h
+++ b/src/gnumeric.h
@@ -187,6 +187,8 @@ typedef struct GnmSolverParameters_ GnmSolverParameters;
typedef struct GnmSolverConstraint_ GnmSolverConstraint;
typedef struct GnmSolverFactory_ GnmSolverFactory;
+typedef struct GnmScenario_ GnmScenario;
+
G_END_DECLS
#endif /* _GNM_GNUMERIC_H_ */
diff --git a/src/sheet.c b/src/sheet.c
index a430038..b8c1bf3 100644
--- a/src/sheet.c
+++ b/src/sheet.c
@@ -3969,7 +3969,11 @@ gnm_sheet_finalize (GObject *obj)
sheet_destroy (sheet);
g_object_unref (sheet->solver_parameters);
- scenarios_free (sheet->scenarios);
+ sheet->solver_parameters = NULL;
+
+ go_list_free_custom (sheet->scenarios, g_object_unref);
+ sheet->scenarios = NULL;
+
if (sheet->sort_setups != NULL)
g_hash_table_unref (sheet->sort_setups);
@@ -5457,6 +5461,7 @@ sheet_dup (Sheet const *src)
Workbook *wb;
Sheet *dst;
char *name;
+ GList *l;
g_return_val_if_fail (IS_SHEET (src), NULL);
g_return_val_if_fail (src->workbook != NULL, NULL);
@@ -5504,7 +5509,12 @@ sheet_dup (Sheet const *src)
g_object_unref (dst->solver_parameters);
dst->solver_parameters = gnm_solver_param_dup (src->solver_parameters, dst);
- dst->scenarios = scenarios_dup (src->scenarios, dst);
+ for (l = src->scenarios; l; l = l->next) {
+ GnmScenario *src_sc = l->data;
+ GnmScenario *dst_sc = gnm_scenario_dup (src_sc, dst);
+ dst->scenarios = g_list_prepend (dst->scenarios, dst_sc);
+ }
+ dst->scenarios = g_list_reverse (dst->scenarios);
sheet_mark_dirty (dst);
sheet_redraw_all (dst, TRUE);
@@ -5768,6 +5778,23 @@ gnm_sheet_set_solver_params (Sheet *sheet, GnmSolverParameters *param)
sheet->solver_parameters = param;
}
+GnmScenario *
+gnm_sheet_get_scenario (Sheet *sheet, const char *name)
+{
+ GList *l;
+
+ g_return_val_if_fail (IS_SHEET (sheet), NULL);
+ g_return_val_if_fail (name != NULL, NULL);
+
+ for (l = sheet->scenarios; l; l = l->next) {
+ GnmScenario *sc = l->data;
+ if (strcmp (name, sc->name) == 0)
+ return sc;
+ }
+
+ return NULL;
+}
+
GHashTable *
gnm_sheet_get_sort_setups (Sheet *sheet)
{
diff --git a/src/sheet.h b/src/sheet.h
index a0a098f..61ba6db 100644
--- a/src/sheet.h
+++ b/src/sheet.h
@@ -281,6 +281,8 @@ void gnm_sheet_foreach_name (Sheet const *sheet, GHFunc func, gpointer data);
void gnm_sheet_set_solver_params (Sheet *sheet, GnmSolverParameters *param);
+GnmScenario *gnm_sheet_get_scenario (Sheet *sheet, const char *name);
+
GHashTable *gnm_sheet_get_sort_setups (Sheet *sheet);
void gnm_sheet_add_sort_setup (Sheet *sheet, char *key, gpointer setup);
gconstpointer gnm_sheet_find_sort_setup (Sheet *sheet, char const *key);
diff --git a/src/tools/scenarios.c b/src/tools/scenarios.c
index 738be0d..7a0b771 100644
--- a/src/tools/scenarios.c
+++ b/src/tools/scenarios.c
@@ -6,6 +6,7 @@
* Jukka-Pekka Iivonen <jiivonen hutcs cs hut fi>
*
* (C) Copyright 2003 by Jukka-Pekka Iivonen <jiivonen hutcs cs hut fi>
+ * (C) Copyright 2009 by Morten Welinder <terra gnome org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -25,6 +26,7 @@
#include <gnumeric-config.h>
#include <glib/gi18n-lib.h>
#include <gnumeric.h>
+#include <gsf/gsf-impl-utils.h>
#include <sheet.h>
#include <sheet-filter.h>
@@ -43,29 +45,7 @@
#include <string.h>
-/* Generic stuff **********************************************************/
-
-GnmScenario *
-scenario_by_name (GList *scenarios, gchar const *name, gboolean *all_deleted)
-{
- GnmScenario *res = NULL;
-
- if (all_deleted)
- *all_deleted = TRUE;
-
- while (scenarios != NULL) {
- GnmScenario *s = scenarios->data;
-
- if (strcmp (s->name, name) == 0)
- res = s;
- else if (all_deleted && !s->marked_deleted)
- *all_deleted = FALSE;
-
- scenarios = scenarios->next;
- }
-
- return res;
-}
+/* ------------------------------------------------------------------------- */
typedef GnmValue * (*ScenarioValueCB) (int col, int row, GnmValue *v, gpointer data);
@@ -85,19 +65,68 @@ scenario_for_each_value (GnmScenario *s, ScenarioValueCB fn, gpointer data)
}
}
-/* Scenario: Add ***********************************************************/
+static GnmValue *
+cb_value_free (int col, int row, GnmValue *v, gpointer data)
+{
+ value_release (v);
+
+ return NULL;
+}
+
+/* ------------------------------------------------------------------------- */
+
+static GObjectClass *gnm_scenario_parent_class;
+
+static void
+gnm_scenario_finalize (GObject *obj)
+{
+ GnmScenario *sc = GNM_SCENARIO (obj);
+
+ g_free (sc->name);
+ g_free (sc->comment);
+
+ g_free (sc->cell_sel_str);
+
+ scenario_for_each_value (sc, cb_value_free, NULL);
+
+ g_free (sc->changing_cells);
+
+ gnm_scenario_parent_class->finalize (obj);
+}
+
+GnmScenario *
+gnm_scenario_new (char const *name, char const *comment, Sheet *sheet)
+{
+ GnmScenario *sc = g_object_new (GNM_SCENARIO_TYPE, NULL);
+
+ sc->sheet = sheet;
+ sc->name = g_strdup (name);
+ sc->comment = g_strdup (comment);
+
+ return sc;
+}
+
+static void
+gnm_scenario_class_init (GObjectClass *object_class)
+{
+ gnm_scenario_parent_class = g_type_class_peek_parent (object_class);
+
+ object_class->finalize = gnm_scenario_finalize;
+}
+
+GSF_CLASS (GnmScenario, gnm_scenario,
+ &gnm_scenario_class_init, NULL, G_TYPE_OBJECT)
+
+/* ------------------------------------------------------------------------- */
static GnmScenario *
scenario_new (Sheet *sheet, gchar const *name, gchar const *comment)
{
- GnmScenario *s;
- GList *scenarios = sheet->scenarios;
-
- s = g_new (GnmScenario, 1);
- s->sheet = sheet;
+ GnmScenario *sc;
+ char *actual_name = g_strdup (name);
/* Check if a scenario having the same name already exists. */
- if (scenario_by_name (scenarios, name, NULL)) {
+ if (gnm_sheet_get_scenario (sheet, name)) {
GString *str = g_string_new (NULL);
gchar *tmp;
int i, j, len;
@@ -117,8 +146,8 @@ scenario_new (Sheet *sheet, gchar const *name, gchar const *comment)
for (j = 1; j < 10000; j++) {
g_string_printf (str, "%s [%d]", tmp, j);
- if (!scenario_by_name (scenarios, str->str, NULL)) {
- s->name = g_string_free (str, FALSE);
+ if (!gnm_sheet_get_scenario (sheet, name)) {
+ actual_name = g_string_free (str, FALSE);
str = NULL;
break;
}
@@ -126,15 +155,16 @@ scenario_new (Sheet *sheet, gchar const *name, gchar const *comment)
if (str)
g_string_free (str, TRUE);
g_free (tmp);
- } else
- s->name = g_strdup (name);
+ }
+
+ sc = gnm_scenario_new (actual_name, comment, sheet);
+ g_free (actual_name);
- s->comment = g_strdup (comment);
- s->changing_cells = NULL;
- s->cell_sel_str = NULL;
- s->marked_deleted = FALSE;
+ sc->changing_cells = NULL;
+ sc->cell_sel_str = NULL;
+ sc->marked_deleted = FALSE;
- return s;
+ return sc;
}
@@ -222,106 +252,39 @@ copy_cb (int col, int row, GnmValue *v, copy_cb_t *p)
}
GnmScenario *
-scenario_copy (GnmScenario *s, Sheet *new_sheet)
+gnm_scenario_dup (GnmScenario *src, Sheet *new_sheet)
{
- GnmScenario *p;
+ GnmScenario *dst;
copy_cb_t cb;
- p = g_new (GnmScenario, 1);
+ dst = gnm_scenario_new (src->name, src->comment, new_sheet);
+ dst->cell_sel_str = g_strdup (src->cell_sel_str);
+ dst->range = src->range;
- p->name = g_strdup (s->name);
- p->comment = g_strdup (s->comment);
- /* FIXME: Sheet name change */
- p->cell_sel_str = g_strdup (s->cell_sel_str);
- range_init (&p->range, s->range.start.col, s->range.start.row,
- s->range.end.col, s->range.end.row);
+ cb.rows = src->range.end.row - src->range.start.row + 1;
+ cb.cols = src->range.end.col - src->range.start.col + 1;
+ cb.col_offset = src->range.start.col;
+ cb.row_offset = src->range.start.row;
+ cb.dest = dst;
- cb.rows = s->range.end.row - s->range.start.row + 1;
- cb.cols = s->range.end.col - s->range.start.col + 1;
- cb.col_offset = s->range.start.col;
- cb.row_offset = s->range.start.row;
- cb.dest = p;
+ dst->changing_cells = g_new (GnmValue *, cb.rows * cb.cols);
+ scenario_for_each_value (src, (ScenarioValueCB) copy_cb, &cb);
- p->changing_cells = g_new (GnmValue *, cb.rows * cb.cols);
- scenario_for_each_value (s, (ScenarioValueCB) copy_cb, &cb);
-
- return p;
-}
-
-GList *
-scenarios_dup (GList *list, Sheet *ns)
-{
- GList *cpy = NULL;
-
- while (list != NULL) {
- cpy = g_list_prepend (cpy, scenario_copy (list->data, ns));
- list = list->next;
- }
-
- return g_list_reverse (cpy);
+ return dst;
}
/* Scenario: Remove sheet *************************************************/
-static GnmValue *
-cb_value_free (int col, int row, GnmValue *v, gpointer data)
-{
- value_release (v);
-
- return NULL;
-}
-
void
-scenario_free (GnmScenario *s)
+scenario_delete (Sheet *sheet, gchar *name)
{
- if (s == NULL)
- return;
-
- g_free (s->name);
- g_free (s->comment);
- g_free (s->cell_sel_str);
-
- scenario_for_each_value (s, cb_value_free, NULL);
-
- g_free (s->changing_cells);
- g_free (s);
-}
-
-/**
- * scenarios_free :
- * @list : #GList
- *
- * Free all scenarios in the collection.
- **/
-void
-scenarios_free (GList *list)
-{
- go_list_free_custom (list, (GFreeFunc)scenario_free);
-}
+ GnmScenario *sc;
-gboolean
-scenario_mark_deleted (GList *scenarios, gchar *name)
-{
- GnmScenario *s;
- gboolean all_deleted;
-
- s = scenario_by_name (scenarios, name, &all_deleted);
- s->marked_deleted = TRUE;
-
- return all_deleted;
-}
-
-GList *
-scenario_delete (GList *scenarios, gchar *name)
-{
- GnmScenario *s;
- GList *list;
-
- s = scenario_by_name (scenarios, name, NULL);
- list = g_list_remove (scenarios, s);
- scenario_free (s);
-
- return list;
+ sc = gnm_sheet_get_scenario (sheet, name);
+ if (sc) {
+ sheet->scenarios = g_list_remove (sheet->scenarios, sc);
+ g_object_unref (sc);
+ }
}
/* Scenario: Show **********************************************************/
@@ -349,7 +312,7 @@ scenario_show (WorkbookControl *wbc,
if (old_values) {
scenario_for_each_value (old_values, (ScenarioValueCB) show_cb,
dao);
- scenario_free (old_values);
+ g_object_unref (old_values);
}
if (s == NULL)
@@ -388,7 +351,7 @@ scenario_manager_ok (Sheet *sheet)
GnmScenario *s = cur->data;
if (s->marked_deleted)
- scenario_free (s);
+ g_object_unref (s);
else
list = g_list_append (list, s);
}
diff --git a/src/tools/scenarios.h b/src/tools/scenarios.h
index e604019..6a9df5c 100644
--- a/src/tools/scenarios.h
+++ b/src/tools/scenarios.h
@@ -4,18 +4,42 @@
#include <gnumeric.h>
#include <tools/dao.h>
-typedef struct GnmScenario_ {
+/* ------------------------------------------------------------------------- */
+
+#define GNM_SCENARIO_TYPE (gnm_scenario_get_type ())
+#define GNM_SCENARIO(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GNM_SCENARIO_TYPE, GnmScenario))
+#define GNM_SCENARIO_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GNM_SCENARIO_TYPE, GnmScenarioClass))
+#define GNM_IS_SCENARIO(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GNM_SCENARIO_TYPE))
+
+struct GnmScenario_ {
+ GObject parent;
+
Sheet *sheet;
- gchar *name;
+ char *name;
+ char *comment;
GnmValue **changing_cells;
GnmRange range;
- gchar *comment;
gchar *cell_sel_str;
gboolean marked_deleted;
-} GnmScenario;
+};
+
+typedef struct {
+ GObjectClass parent_class;
+} GnmScenarioClass;
+
+GType gnm_scenario_get_type (void);
+
+GnmScenario *gnm_scenario_new (char const *name, char const *comment,
+ Sheet *sheet);
+
+GnmScenario *gnm_scenario_dup (GnmScenario *s, Sheet *new_sheet);
+
+/* ------------------------------------------------------------------------- */
+
+
typedef struct _scenario_cmd_t {
GnmScenario *redo;
@@ -24,10 +48,6 @@ typedef struct _scenario_cmd_t {
GnmScenario *scenario_by_name (GList *scenarios, const gchar *name,
gboolean *all_deleted);
-void scenario_free (GnmScenario *s);
-
-GList *scenarios_dup (GList *list, Sheet *dst);
-void scenarios_free (GList *list);
void scenario_manager_ok (Sheet *sheet);
GnmScenario *scenario_show (WorkbookControl *wbc,
@@ -40,9 +60,7 @@ gboolean scenario_add_new (gchar const *name,
gchar const *comment,
Sheet *sheet, GnmScenario **new_scenario);
void scenario_add (Sheet *sheet, GnmScenario *scenario);
-gboolean scenario_mark_deleted (GList *scenarios, gchar *name);
-GList *scenario_delete (GList *scenarios, gchar *name);
-GnmScenario *scenario_copy (GnmScenario *s, Sheet *new_sheet);
+void scenario_delete (Sheet *sheet, gchar *name);
void scenario_summary (WorkbookControl *wbc,
Sheet *sheet,
GSList *results,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]