Move plugin init code
[gnumeric.git] / src / gnm-commands-slicer.c
blob569d5f9d5852a2da9fc4dd1b0665cc47f5d6764a
1 /*
2 * gnm-commands-slicer.c: undo & redo for data slicer manipulation
4 * Copyright (C) 2008 Jody Goldberg (jody@gnome.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
22 #include <gnumeric-config.h>
23 #include <glib/gi18n-lib.h>
24 #include <gnm-commands-slicer.h>
25 #include <gnm-sheet-slicer.h>
26 #include <gnm-command-impl.h>
27 #include <command-context.h>
28 #include <workbook-control.h>
29 #include <sheet-view.h>
30 #include <sheet.h>
31 #include <ranges.h>
32 #include <clipboard.h>
34 #define CMD_SLICER_REFRESH_TYPE (cmd_slicer_refresh_get_type ())
35 #define CMD_SLICER_REFRESH(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), CMD_SLICER_REFRESH_TYPE, CmdSlicerRefresh))
37 typedef struct {
38 GnmCommand cmd;
40 GnmSheetSlicer *slicer;
41 GnmCellRegion *orig_content;
42 GnmRange orig_size;
43 } CmdSlicerRefresh;
45 MAKE_GNM_COMMAND (CmdSlicerRefresh, cmd_slicer_refresh, NULL)
47 static gboolean
48 cmd_slicer_refresh_undo (GnmCommand *cmd, WorkbookControl *wbc)
50 CmdSlicerRefresh *me = CMD_SLICER_REFRESH (cmd);
51 GnmRange const *new_size = gnm_sheet_slicer_get_range (me->slicer);
52 GnmPasteTarget pt;
53 sheet_clear_region (me->cmd.sheet,
54 new_size->start.col, new_size->start.row,
55 new_size->end.col, new_size->end.row,
56 CLEAR_VALUES | CLEAR_FORMATS | CLEAR_MERGES | CLEAR_NOCHECKARRAY | CLEAR_RECALC_DEPS,
57 GO_CMD_CONTEXT (wbc));
58 clipboard_paste_region (me->orig_content,
59 paste_target_init (&pt, me->cmd.sheet, &me->orig_size, PASTE_DEFAULT),
60 GO_CMD_CONTEXT (wbc));
61 cellregion_unref (me->orig_content);
62 me->orig_content = NULL;
63 return FALSE;
66 static gboolean
67 cmd_slicer_refresh_redo (GnmCommand *cmd, WorkbookControl *wbc)
69 CmdSlicerRefresh *me = CMD_SLICER_REFRESH (cmd);
70 GnmRange const *new_size = gnm_sheet_slicer_get_range (me->slicer);
72 me->orig_size = *gnm_sheet_slicer_get_range (me->slicer);
73 me->orig_content = clipboard_copy_range (me->cmd.sheet, &me->orig_size);
75 sheet_clear_region (me->cmd.sheet,
76 new_size->start.col, new_size->start.row,
77 new_size->end.col, new_size->end.row,
78 CLEAR_VALUES | CLEAR_FORMATS | CLEAR_MERGES | CLEAR_NOCHECKARRAY | CLEAR_RECALC_DEPS,
79 GO_CMD_CONTEXT (wbc));
81 gnm_sheet_slicer_regenerate (me->slicer);
83 return FALSE;
86 static void
87 cmd_slicer_refresh_finalize (GObject *cmd)
89 CmdSlicerRefresh *me = CMD_SLICER_REFRESH (cmd);
90 if (NULL != me->orig_content)
91 cellregion_unref (me->orig_content);
92 gnm_command_finalize (cmd);
95 /**
96 * cmd_slicer_refresh:
97 * @wbc: the workbook control.
98 **/
99 gboolean
100 cmd_slicer_refresh (WorkbookControl *wbc)
102 CmdSlicerRefresh *me;
103 char *r_name;
104 SheetView *sv = wb_control_cur_sheet_view (wbc);
105 GnmSheetSlicer *slicer;
107 slicer = gnm_sheet_slicers_at_pos (sv->sheet, &sv->edit_pos);
108 if (NULL == slicer)
109 return FALSE;
111 me = g_object_new (CMD_SLICER_REFRESH_TYPE, NULL);
112 me->cmd.sheet = sv_sheet (sv);
113 me->cmd.size = 1; /* Updated below. */
114 me->orig_content = NULL;
115 me->slicer = slicer;
117 r_name = undo_range_name (me->cmd.sheet,
118 gnm_sheet_slicer_get_range (slicer));
119 me->cmd.cmd_descriptor = g_strdup_printf (_("Refreshing DataSlicer in %s"), r_name);
120 g_free (r_name);
122 return gnm_command_push_undo (wbc, G_OBJECT (me));