Update Spanish translation
[gnumeric.git] / src / undo.c
blob48a7ac1e9a8adfccbeffb5410f438e29a2815350
1 /*
2 * undo.c:
4 * Authors:
5 * Andreas J. Guelzow <aguelzow@pyrshep.ca>
7 * (C) Copyright 2010 by Andreas J. Guelzow <aguelzow@pyrshep.ca>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
23 #include <gnumeric-config.h>
25 #include <undo.h>
26 #include <sheet.h>
27 #include <sheet-view.h>
28 #include <sheet-control-gui.h>
29 #include <wbc-gtk.h>
30 #include <wbc-gtk-impl.h>
31 #include <colrow.h>
33 #include <gsf/gsf-impl-utils.h>
34 #include <glib/gi18n-lib.h>
36 /* ------------------------------------------------------------------------- */
38 static GObjectClass *gnm_undo_colrow_restore_state_group_parent_class;
40 static void
41 gnm_undo_colrow_restore_state_group_finalize (GObject *o)
43 GnmUndoColrowRestoreStateGroup *ua = (GnmUndoColrowRestoreStateGroup *)o;
45 colrow_state_group_destroy (ua->saved_state);
46 ua->saved_state = NULL;
47 colrow_index_list_destroy (ua->selection);
48 ua->selection = NULL;
50 G_OBJECT_CLASS (gnm_undo_colrow_restore_state_group_parent_class)->finalize (o);
53 static void
54 gnm_undo_colrow_restore_state_group_undo (GOUndo *u, G_GNUC_UNUSED gpointer data)
56 GnmUndoColrowRestoreStateGroup *ua = (GnmUndoColrowRestoreStateGroup *)u;
58 colrow_restore_state_group (ua->sheet, ua->is_cols, ua->selection, ua->saved_state);
61 static void
62 gnm_undo_colrow_restore_state_group_class_init (GObjectClass *gobject_class)
64 GOUndoClass *uclass = (GOUndoClass *)gobject_class;
66 gnm_undo_colrow_restore_state_group_parent_class = g_type_class_peek_parent (gobject_class);
68 gobject_class->finalize = gnm_undo_colrow_restore_state_group_finalize;
69 uclass->undo = gnm_undo_colrow_restore_state_group_undo;
73 GSF_CLASS (GnmUndoColrowRestoreStateGroup, gnm_undo_colrow_restore_state_group,
74 gnm_undo_colrow_restore_state_group_class_init, NULL, GO_TYPE_UNDO)
76 /**
77 * gnm_undo_colrow_restore_state_group_new:
79 * Returns: a new undo object.
80 **/
82 GOUndo *
83 gnm_undo_colrow_restore_state_group_new (Sheet *sheet, gboolean is_cols,
84 ColRowIndexList *selection,
85 ColRowStateGroup *saved_state)
87 GnmUndoColrowRestoreStateGroup *ua = g_object_new (GNM_TYPE_UNDO_COLROW_RESTORE_STATE_GROUP, NULL);
89 ua->sheet = sheet;
90 ua->is_cols = is_cols;
91 ua->selection = selection;
92 ua->saved_state = saved_state;
94 return (GOUndo *)ua;
97 /* ------------------------------------------------------------------------- */
99 static GObjectClass *gnm_undo_colrow_set_sizes_parent_class;
101 static void
102 gnm_undo_colrow_set_sizes_finalize (GObject *o)
104 GnmUndoColrowSetSizes *ua = (GnmUndoColrowSetSizes *)o;
106 colrow_index_list_destroy (ua->selection);
107 ua->selection = NULL;
109 G_OBJECT_CLASS (gnm_undo_colrow_set_sizes_parent_class)->finalize (o);
112 static void
113 gnm_undo_colrow_set_sizes_undo (GOUndo *u, G_GNUC_UNUSED gpointer data)
115 GnmUndoColrowSetSizes *ua = (GnmUndoColrowSetSizes *)u;
116 ColRowStateGroup *group;
118 group = colrow_set_sizes (ua->sheet, ua->is_cols, ua->selection, ua->new_size,
119 ua->from, ua->to);
120 colrow_state_group_destroy (group);
123 static void
124 gnm_undo_colrow_set_sizes_class_init (GObjectClass *gobject_class)
126 GOUndoClass *uclass = (GOUndoClass *)gobject_class;
128 gnm_undo_colrow_set_sizes_parent_class = g_type_class_peek_parent (gobject_class);
130 gobject_class->finalize = gnm_undo_colrow_set_sizes_finalize;
131 uclass->undo = gnm_undo_colrow_set_sizes_undo;
135 GSF_CLASS (GnmUndoColrowSetSizes, gnm_undo_colrow_set_sizes,
136 gnm_undo_colrow_set_sizes_class_init, NULL, GO_TYPE_UNDO)
139 * gnm_undo_colrow_set_sizes_new:
141 * If r is non-null and new_size < 0, selection is ignored.
143 * Returns: a new undo object.
146 GOUndo *
147 gnm_undo_colrow_set_sizes_new (Sheet *sheet, gboolean is_cols,
148 ColRowIndexList *selection,
149 int new_size, GnmRange const *r)
151 GnmUndoColrowSetSizes *ua;
153 g_return_val_if_fail (selection != NULL || (r != NULL && new_size == -1), NULL);
155 ua = g_object_new (GNM_TYPE_UNDO_COLROW_SET_SIZES, NULL);
157 ua->sheet = sheet;
158 ua->is_cols = is_cols;
159 ua->new_size = new_size;
161 if (r == NULL || new_size >= 0) {
162 ua->selection = selection;
163 ua->from = 0;
164 ua->to = -1;
165 } else {
166 int first, last;
168 if (is_cols) {
169 first = r->start.col;
170 last = r->end.col;
171 ua->from = r->start.row;
172 ua->to = r->end.row;
173 } else {
174 first = r->start.row;
175 last = r->end.row;
176 ua->from = r->start.col;
177 ua->to = r->end.col;
179 ua->selection = colrow_get_index_list (first, last, NULL);
182 return (GOUndo *)ua;
185 /* ------------------------------------------------------------------------- */
187 static GObjectClass *gnm_undo_filter_set_condition_parent_class;
189 static void
190 gnm_undo_filter_set_condition_finalize (GObject *o)
192 GnmUndoFilterSetCondition *ua = (GnmUndoFilterSetCondition *)o;
194 gnm_filter_condition_free (ua->cond);
195 ua->cond = NULL;
197 G_OBJECT_CLASS (gnm_undo_filter_set_condition_parent_class)->finalize (o);
200 static gboolean
201 cb_filter_set_condition_undo (GnmColRowIter const *iter, gint *count)
203 if (iter->cri->visible)
204 (*count)++;
205 return FALSE;
208 static void
209 cb_filter_set_condition_undo_set_pb (SheetControl *control, char *text)
211 SheetControlGUI *scg = (SheetControlGUI *) control;
212 WBCGtk *wbcg = scg_wbcg (scg);
213 if (wbcg != NULL)
214 gtk_progress_bar_set_text
215 (GTK_PROGRESS_BAR (wbcg->progress_bar), text);
218 static void
219 gnm_undo_filter_set_condition_undo (GOUndo *u, G_GNUC_UNUSED gpointer data)
221 GnmUndoFilterSetCondition *ua = (GnmUndoFilterSetCondition *)u;
222 gint count = 0;
223 char const *format;
224 char *text;
226 gnm_filter_set_condition (ua->filter, ua->i,
227 gnm_filter_condition_dup (ua->cond), TRUE);
228 sheet_update (ua->filter->sheet);
230 sheet_colrow_foreach (ua->filter->sheet, FALSE,
231 ua->filter->r.start.row + 1,
232 ua->filter->r.end.row,
233 (ColRowHandler) cb_filter_set_condition_undo,
234 &count);
235 if (ua->filter->r.end.row - ua->filter->r.start.row > 10) {
236 /* xgettext: The first %d gives the number of rows that match. */
237 /* The second %d gives the total number of rows. Assume that the */
238 /* total number of rows is always large (>10). */
239 /* Note that the english "matches" or "match" is the verb of this sentence! */
240 /* There is no explicit noun associated with the second %d in english, the */
241 /* meaning is really "%d rows of all %d rows match" */
242 /* This is input to ngettext. */
243 format = ngettext ("%d row of %d matches",
244 "%d rows of %d match",
245 count);
246 text = g_strdup_printf (format, count,
247 ua->filter->r.end.row -
248 ua->filter->r.start.row);
249 } else {
250 /* xgettext: The %d gives the number of rows that match. */
251 /* This is input to ngettext. */
252 format = ngettext ("%d row matches",
253 "%d rows match",
254 count);
255 text = g_strdup_printf (format, count);
258 SHEET_FOREACH_CONTROL (ua->filter->sheet, view, control, cb_filter_set_condition_undo_set_pb (control, text););
260 g_free (text);
263 static void
264 gnm_undo_filter_set_condition_class_init (GObjectClass *gobject_class)
266 GOUndoClass *uclass = (GOUndoClass *)gobject_class;
268 gnm_undo_filter_set_condition_parent_class = g_type_class_peek_parent
269 (gobject_class);
271 gobject_class->finalize = gnm_undo_filter_set_condition_finalize;
272 uclass->undo = gnm_undo_filter_set_condition_undo;
276 GSF_CLASS (GnmUndoFilterSetCondition, gnm_undo_filter_set_condition,
277 gnm_undo_filter_set_condition_class_init, NULL, GO_TYPE_UNDO)
280 * gnm_undo_filter_set_condition_new:
282 * if (retrieve_from_filter), cond is ignored
284 * Returns: a new undo object.
287 GOUndo *
288 gnm_undo_filter_set_condition_new (GnmFilter *filter, unsigned i,
289 GnmFilterCondition *cond,
290 gboolean retrieve_from_filter)
292 GnmUndoFilterSetCondition *ua;
294 g_return_val_if_fail (filter != NULL, NULL);
295 g_return_val_if_fail (i < filter->fields->len , NULL);
297 ua = g_object_new (GNM_TYPE_UNDO_FILTER_SET_CONDITION, NULL);
299 ua->filter = filter;
300 ua->i = i;
302 if (retrieve_from_filter)
303 ua->cond = gnm_filter_condition_dup
304 (gnm_filter_get_condition (filter, i));
305 else
306 ua->cond = cond;
308 return (GOUndo *)ua;
310 /* ------------------------------------------------------------------------- */