Introspection: update bless api
[gnumeric.git] / src / undo.c
blob719d39f2fe7e7dcd47d52b12b7eca10c461453af
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * undo.c:
5 * Authors:
6 * Andreas J. Guelzow <aguelzow@pyrshep.ca>
8 * (C) Copyright 2010 by Andreas J. Guelzow <aguelzow@pyrshep.ca>
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.
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.
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/>.
24 #include <gnumeric-config.h>
26 #include "undo.h"
27 #include "sheet.h"
28 #include "sheet-view.h"
29 #include "sheet-control-gui.h"
30 #include "wbc-gtk.h"
31 #include "wbc-gtk-impl.h"
32 #include "colrow.h"
34 #include <gsf/gsf-impl-utils.h>
35 #include <glib/gi18n-lib.h>
37 /* ------------------------------------------------------------------------- */
39 static GObjectClass *gnm_undo_colrow_restore_state_group_parent_class;
41 static void
42 gnm_undo_colrow_restore_state_group_finalize (GObject *o)
44 GnmUndoColrowRestoreStateGroup *ua = (GnmUndoColrowRestoreStateGroup *)o;
46 colrow_state_group_destroy (ua->saved_state);
47 ua->saved_state = NULL;
48 colrow_index_list_destroy (ua->selection);
49 ua->selection = NULL;
51 G_OBJECT_CLASS (gnm_undo_colrow_restore_state_group_parent_class)->finalize (o);
54 static void
55 gnm_undo_colrow_restore_state_group_undo (GOUndo *u, G_GNUC_UNUSED gpointer data)
57 GnmUndoColrowRestoreStateGroup *ua = (GnmUndoColrowRestoreStateGroup *)u;
59 colrow_restore_state_group (ua->sheet, ua->is_cols, ua->selection, ua->saved_state);
62 static void
63 gnm_undo_colrow_restore_state_group_class_init (GObjectClass *gobject_class)
65 GOUndoClass *uclass = (GOUndoClass *)gobject_class;
67 gnm_undo_colrow_restore_state_group_parent_class = g_type_class_peek_parent (gobject_class);
69 gobject_class->finalize = gnm_undo_colrow_restore_state_group_finalize;
70 uclass->undo = gnm_undo_colrow_restore_state_group_undo;
74 GSF_CLASS (GnmUndoColrowRestoreStateGroup, gnm_undo_colrow_restore_state_group,
75 gnm_undo_colrow_restore_state_group_class_init, NULL, GO_TYPE_UNDO)
77 /**
78 * gnm_undo_colrow_restore_state_group_new:
80 * Returns: a new undo object.
81 **/
83 GOUndo *
84 gnm_undo_colrow_restore_state_group_new (Sheet *sheet, gboolean is_cols,
85 ColRowIndexList *selection,
86 ColRowStateGroup *saved_state)
88 GnmUndoColrowRestoreStateGroup *ua = g_object_new (GNM_TYPE_UNDO_COLROW_RESTORE_STATE_GROUP, NULL);
90 ua->sheet = sheet;
91 ua->is_cols = is_cols;
92 ua->selection = selection;
93 ua->saved_state = saved_state;
95 return (GOUndo *)ua;
98 /* ------------------------------------------------------------------------- */
100 static GObjectClass *gnm_undo_colrow_set_sizes_parent_class;
102 static void
103 gnm_undo_colrow_set_sizes_finalize (GObject *o)
105 GnmUndoColrowSetSizes *ua = (GnmUndoColrowSetSizes *)o;
107 colrow_index_list_destroy (ua->selection);
108 ua->selection = NULL;
110 G_OBJECT_CLASS (gnm_undo_colrow_set_sizes_parent_class)->finalize (o);
113 static void
114 gnm_undo_colrow_set_sizes_undo (GOUndo *u, G_GNUC_UNUSED gpointer data)
116 GnmUndoColrowSetSizes *ua = (GnmUndoColrowSetSizes *)u;
117 ColRowStateGroup *group;
119 group = colrow_set_sizes (ua->sheet, ua->is_cols, ua->selection, ua->new_size,
120 ua->from, ua->to);
121 colrow_state_group_destroy (group);
124 static void
125 gnm_undo_colrow_set_sizes_class_init (GObjectClass *gobject_class)
127 GOUndoClass *uclass = (GOUndoClass *)gobject_class;
129 gnm_undo_colrow_set_sizes_parent_class = g_type_class_peek_parent (gobject_class);
131 gobject_class->finalize = gnm_undo_colrow_set_sizes_finalize;
132 uclass->undo = gnm_undo_colrow_set_sizes_undo;
136 GSF_CLASS (GnmUndoColrowSetSizes, gnm_undo_colrow_set_sizes,
137 gnm_undo_colrow_set_sizes_class_init, NULL, GO_TYPE_UNDO)
140 * gnm_undo_colrow_set_sizes_new:
142 * If r is non-null and new_size < 0, selection is ignored.
144 * Returns: a new undo object.
147 GOUndo *
148 gnm_undo_colrow_set_sizes_new (Sheet *sheet, gboolean is_cols,
149 ColRowIndexList *selection,
150 int new_size, GnmRange const *r)
152 GnmUndoColrowSetSizes *ua;
154 g_return_val_if_fail (selection != NULL || (r != NULL && new_size == -1), NULL);
156 ua = g_object_new (GNM_TYPE_UNDO_COLROW_SET_SIZES, NULL);
158 ua->sheet = sheet;
159 ua->is_cols = is_cols;
160 ua->new_size = new_size;
162 if (r == NULL || new_size >= 0) {
163 ua->selection = selection;
164 ua->from = 0;
165 ua->to = -1;
166 } else {
167 int first, last;
169 if (is_cols) {
170 first = r->start.col;
171 last = r->end.col;
172 ua->from = r->start.row;
173 ua->to = r->end.row;
174 } else {
175 first = r->start.row;
176 last = r->end.row;
177 ua->from = r->start.col;
178 ua->to = r->end.col;
180 ua->selection = colrow_get_index_list (first, last, NULL);
183 return (GOUndo *)ua;
186 /* ------------------------------------------------------------------------- */
188 static GObjectClass *gnm_undo_filter_set_condition_parent_class;
190 static void
191 gnm_undo_filter_set_condition_finalize (GObject *o)
193 GnmUndoFilterSetCondition *ua = (GnmUndoFilterSetCondition *)o;
195 gnm_filter_condition_free (ua->cond);
196 ua->cond = NULL;
198 G_OBJECT_CLASS (gnm_undo_filter_set_condition_parent_class)->finalize (o);
201 static gboolean
202 cb_filter_set_condition_undo (GnmColRowIter const *iter, gint *count)
204 if (iter->cri->visible)
205 (*count)++;
206 return FALSE;
209 static void
210 cb_filter_set_condition_undo_set_pb (SheetControl *control, char *text)
212 SheetControlGUI *scg = (SheetControlGUI *) control;
213 WBCGtk *wbcg = scg_wbcg (scg);
214 if (wbcg != NULL)
215 gtk_progress_bar_set_text
216 (GTK_PROGRESS_BAR (wbcg->progress_bar), text);
219 static void
220 gnm_undo_filter_set_condition_undo (GOUndo *u, G_GNUC_UNUSED gpointer data)
222 GnmUndoFilterSetCondition *ua = (GnmUndoFilterSetCondition *)u;
223 gint count = 0;
224 char const *format;
225 char *text;
227 gnm_filter_set_condition (ua->filter, ua->i,
228 gnm_filter_condition_dup (ua->cond), TRUE);
229 sheet_update (ua->filter->sheet);
231 colrow_foreach (&ua->filter->sheet->rows,
232 ua->filter->r.start.row + 1,
233 ua->filter->r.end.row,
234 (ColRowHandler) cb_filter_set_condition_undo,
235 &count);
236 if (ua->filter->r.end.row - ua->filter->r.start.row > 10) {
237 /* xgettext: The first %d gives the number of rows that match. */
238 /* The second %d gives the total number of rows. Assume that the */
239 /* total number of rows is always large (>10). */
240 /* Note that the english "matches" or "match" is the verb of this sentence! */
241 /* There is no explicit noun associated with the second %d in english, the */
242 /* meaning is really "%d rows of all %d rows match" */
243 /* This is input to ngettext. */
244 format = ngettext ("%d row of %d matches",
245 "%d rows of %d match",
246 count);
247 text = g_strdup_printf (format, count,
248 ua->filter->r.end.row -
249 ua->filter->r.start.row);
250 } else {
251 /* xgettext: The %d gives the number of rows that match. */
252 /* This is input to ngettext. */
253 format = ngettext ("%d row matches",
254 "%d rows match",
255 count);
256 text = g_strdup_printf (format, count);
259 SHEET_FOREACH_CONTROL (ua->filter->sheet, view, control, cb_filter_set_condition_undo_set_pb (control, text););
261 g_free (text);
264 static void
265 gnm_undo_filter_set_condition_class_init (GObjectClass *gobject_class)
267 GOUndoClass *uclass = (GOUndoClass *)gobject_class;
269 gnm_undo_filter_set_condition_parent_class = g_type_class_peek_parent
270 (gobject_class);
272 gobject_class->finalize = gnm_undo_filter_set_condition_finalize;
273 uclass->undo = gnm_undo_filter_set_condition_undo;
277 GSF_CLASS (GnmUndoFilterSetCondition, gnm_undo_filter_set_condition,
278 gnm_undo_filter_set_condition_class_init, NULL, GO_TYPE_UNDO)
281 * gnm_undo_filter_set_condition_new:
283 * if (retrieve_from_filter), cond is ignored
285 * Returns: a new undo object.
288 GOUndo *
289 gnm_undo_filter_set_condition_new (GnmFilter *filter, unsigned i,
290 GnmFilterCondition *cond,
291 gboolean retrieve_from_filter)
293 GnmUndoFilterSetCondition *ua;
295 g_return_val_if_fail (filter != NULL, NULL);
296 g_return_val_if_fail (i < filter->fields->len , NULL);
298 ua = g_object_new (GNM_TYPE_UNDO_FILTER_SET_CONDITION, NULL);
300 ua->filter = filter;
301 ua->i = i;
303 if (retrieve_from_filter)
304 ua->cond = gnm_filter_condition_dup
305 (gnm_filter_get_condition (filter, i));
306 else
307 ua->cond = cond;
309 return (GOUndo *)ua;
311 /* ------------------------------------------------------------------------- */