CRONBACH: Simplify.
[gnumeric.git] / component / gnumeric.c
blob013de38b9a23c1c343539dfdeaafa8a1e2148c5f
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Gnumeric GOffice component
4 * gnumeric.c
6 * Copyright (C) 2006-2010
8 * Developed by Jean Bréfort <jean.brefort@normalesup.org>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (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>
25 #include <application.h>
26 #include <gnumeric.h>
27 #include <gnm-plugin.h>
28 #include <gnumeric-conf.h>
29 #include <gui-file.h>
30 #include <gui-util.h>
31 #include <gutils.h>
32 #include <print-cell.h>
33 #include <print.h>
34 #include <ranges.h>
35 #include <selection.h>
36 #include <sheet.h>
37 #include <wbc-gtk-impl.h>
38 #include <workbook-view.h>
39 #include <workbook.h>
41 #include <goffice/goffice.h>
42 #include <goffice/component/goffice-component.h>
43 #include <goffice/component/go-component-factory.h>
44 #include <goffice/component/go-component.h>
45 #include <goffice/app/module-plugin-defs.h>
46 #include <gsf/gsf-impl-utils.h>
47 #include <gsf/gsf-input-memory.h>
48 #include <gsf/gsf-output-memory.h>
50 #include <gtk/gtk.h>
52 #include <glib/gi18n-lib.h>
53 #include <cairo.h>
54 #include <pango/pangocairo.h>
56 GOPluginModuleDepend const go_plugin_depends [] = {
57 { "goffice", GOFFICE_API_VERSION }
59 GOPluginModuleHeader const go_plugin_header =
60 { GOFFICE_MODULE_PLUGIN_MAGIC_NUMBER, G_N_ELEMENTS (go_plugin_depends) };
62 G_MODULE_EXPORT void go_plugin_init (GOPlugin *plugin, GOCmdContext *cc);
63 G_MODULE_EXPORT void go_plugin_shutdown (GOPlugin *plugin, GOCmdContext *cc);
65 typedef struct {
66 GOComponent parent;
68 WorkbookView *wv;
69 Workbook *wb;
70 WBCGtk *edited;
71 Sheet *sheet;
72 int col_start, col_end, row_start, row_end;
73 int width, height;
74 } GOGnmComponent;
76 typedef GOComponentClass GOGnmComponentClass;
78 #define GO_TYPE_GNM_COMPONENT (go_gnm_component_get_type ())
79 #define GO_GNM_COMPONENT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GO_TYPE_GNM_COMPONENT, GOGnmComponent))
80 #define GO_IS_GNM_COMPONENT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GO_TYPE_GNM_COMPONENT))
82 GType go_gnm_component_get_type (void);
84 static GObjectClass *gognm_parent_klass;
86 static gboolean
87 go_gnm_component_get_data (GOComponent *component, gpointer *data, int *length,
88 void (**clearfunc) (gpointer), gpointer *user_data)
90 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
91 if (gognm->wv) {
92 GOCmdContext *cc = go_component_get_command_context (component);
93 GOIOContext *io_context = go_io_context_new (cc);
94 GsfOutput *output = gsf_output_memory_new ();
95 GOFileSaver *gfs = workbook_get_file_saver (gognm->wb);
96 if (gfs == NULL)
97 gfs = go_file_saver_get_default ();
98 wbv_save_to_output (gognm->wv, gfs, output, io_context);
99 *data = (gpointer) gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (output));
100 *length = gsf_output_size (output);
101 *clearfunc = g_object_unref;
102 *user_data = output;
103 g_object_unref (io_context);
104 return TRUE;
106 return FALSE;
109 static void
110 go_gnm_component_update_data (GOGnmComponent *gognm)
112 SheetView *sv;
113 GnmRange const *range;
114 gognm->sheet = wb_view_cur_sheet (gognm->wv);
115 sv = sheet_get_view (gognm->sheet, gognm->wv);
116 range = selection_first_range (sv, NULL, NULL);
117 gognm->col_start = range->start.col;
118 gognm->row_start = range->start.row;
119 gognm->col_end = range->end.col;
120 gognm->row_end = range->end.row;
121 gognm->width = sheet_col_get_distance_pts (
122 gognm->sheet, gognm->col_start, gognm->col_end + 1);
123 gognm->parent.width = gognm->width / 72.;
124 gognm->parent.descent = 0.;
125 gognm->height = sheet_row_get_distance_pts (
126 gognm->sheet, gognm->row_start, gognm->row_end + 1);
127 gognm->parent.ascent = gognm->parent.height = gognm->height / 72.;
130 static void
131 go_gnm_component_set_data (GOComponent *component)
133 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
134 GOCmdContext *cc = go_component_get_command_context (component);
135 GOIOContext *io_context = go_io_context_new (cc);
136 GsfInput *input = gsf_input_memory_new (component->data, component->length, FALSE);
138 g_object_set (G_OBJECT (io_context), "exec-main-loop", FALSE, NULL);
139 if (gognm->wv != NULL) {
140 g_object_unref (gognm->wv);
141 g_object_unref (gognm->wb);
143 gognm->wv = workbook_view_new_from_input (input, NULL, NULL, io_context, NULL);
144 gognm->wb = wb_view_get_workbook (gognm->wv);
145 gnm_app_workbook_list_remove (gognm->wb);
146 g_object_unref (io_context);
147 go_gnm_component_update_data (gognm);
150 static void
151 go_gnm_component_render (GOComponent *component, cairo_t *cr, double width_pixels, double height_pixels)
153 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
154 GnmRange range;
156 if (!gognm->sheet)
157 go_gnm_component_update_data (gognm);
159 range_init (&range, gognm->col_start, gognm->row_start, gognm->col_end, gognm->row_end);
160 cairo_save (cr);
161 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
162 cairo_scale (cr, ((double) width_pixels) / gognm->width, ((double) height_pixels) / gognm->height);
163 cairo_rectangle (cr, 0., 0., gognm->width, gognm->height);
164 cairo_clip (cr); /* not sure it is necessary */
165 gnm_gtk_print_cell_range (cr, gognm->sheet, &range, 0., 0.,
166 gognm->sheet->print_info);
167 /* Now render objects */
168 gnm_print_sheet_objects (cr, gognm->sheet, &range, 0., 0.);
169 cairo_restore (cr);
172 static void
173 cb_gognm_save (G_GNUC_UNUSED GtkAction *a, WBCGtk *wbcg)
175 gpointer data = g_object_get_data (G_OBJECT (wbcg), "component");
176 if (GO_IS_COMPONENT (data)) {
177 GOComponent *component = GO_COMPONENT (data);
178 /* update the component data since not all clients will call set_data */
179 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
180 WorkbookView *wv = wb_control_view (GNM_WBC (wbcg));
181 if (wv != gognm->wv) {
182 if (gognm->wv != NULL) {
183 g_object_unref (gognm->wv);
184 g_object_unref (gognm->wb);
186 gognm->wv = g_object_ref (wv);
187 gognm->wb = wb_view_get_workbook (wv);
188 gnm_app_workbook_list_remove (gognm->wb); /* no need to have this one in the list */
190 go_doc_set_dirty (GO_DOC (gognm->wb), FALSE);
191 go_gnm_component_update_data (gognm);
192 go_component_emit_changed (component);
193 } else
194 gui_file_save (wbcg, wb_control_view (GNM_WBC (wbcg)));
197 static GtkActionEntry const actions[] = {
198 /* File */
199 { "FileSaveEmbed", GNM_N_STOCK_SAVE, NULL,
200 NULL, N_("Save the embedded workbook"),
201 G_CALLBACK (cb_gognm_save) }
204 static void
205 cb_editor_destroyed (GOGnmComponent *gognm)
207 if (gognm->edited && G_OBJECT (gognm->edited)->ref_count > 0)
208 g_object_unref (gognm->edited);
209 gognm->edited = NULL;
212 static GtkWindow*
213 go_gnm_component_edit (GOComponent *component)
215 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
216 WorkbookView *wv;
217 if (gognm->edited) {
218 gdk_window_raise (gtk_widget_get_parent_window (GTK_WIDGET (wbcg_toplevel (gognm->edited))));
219 return wbcg_toplevel (gognm->edited);
221 if (!gognm->wv) {
222 component->ascent = 0.;
223 component->descent = 0.;
224 component->width = 0.;
225 wv = workbook_view_new (workbook_new_with_sheets (1));
226 } else {
227 GOCmdContext *cc = go_component_get_command_context (component);
228 GOIOContext *io_context = GO_IS_IO_CONTEXT (cc)? GO_IO_CONTEXT (g_object_ref (cc)): go_io_context_new (cc);
229 GsfInput *input = gsf_input_memory_new (component->data, component->length, FALSE);
231 g_object_set (G_OBJECT (io_context), "exec-main-loop", FALSE, NULL);
232 wv = workbook_view_new_from_input (input, NULL, NULL, io_context, NULL);
233 g_object_unref (io_context);
235 set_uifilename ("Gnumeric-embed.xml", actions, G_N_ELEMENTS (actions));
236 gognm->edited = wbc_gtk_new (wv, NULL, NULL, NULL);
238 g_object_set_data (G_OBJECT (gognm->edited), "component", gognm);
239 g_signal_connect_swapped (gognm->edited->toplevel, "destroy",
240 G_CALLBACK (cb_editor_destroyed), gognm);
241 return wbcg_toplevel (gognm->edited);
244 static void
245 go_gnm_component_finalize (GObject *obj)
247 GOGnmComponent *gognm = GO_GNM_COMPONENT (obj);
248 if (gognm->wv != NULL) {
249 g_object_unref (gognm->wv);
250 g_object_unref (gognm->wb);
251 gognm->wv = NULL;
253 if (gognm->edited != NULL) {
254 g_object_unref (wb_control_view (GNM_WBC (gognm->edited)));
255 gognm->edited = NULL;
257 G_OBJECT_CLASS (gognm_parent_klass)->finalize (obj);
260 static void
261 go_gnm_component_init (GOComponent *component)
263 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
264 component->resizable = FALSE;
265 component->editable = TRUE;
266 component->snapshot_type = GO_SNAPSHOT_SVG;
267 gognm->row_start = gognm->col_start = 0;
268 gognm->sheet = NULL;
269 gognm->row_end = 9;
270 gognm->col_end = 4;
273 static void
274 go_gnm_component_class_init (GOComponentClass *klass)
276 GObjectClass *obj_klass = (GObjectClass *) klass;
277 obj_klass->finalize = go_gnm_component_finalize;
279 gognm_parent_klass = (GObjectClass*) g_type_class_peek_parent (klass);
281 klass->get_data = go_gnm_component_get_data;
282 klass->set_data = go_gnm_component_set_data;
283 klass->render = go_gnm_component_render;
284 klass->edit = go_gnm_component_edit;
287 GSF_DYNAMIC_CLASS (GOGnmComponent, go_gnm_component,
288 go_gnm_component_class_init, go_gnm_component_init,
289 GO_TYPE_COMPONENT)
291 /*************************************************************************************/
293 G_MODULE_EXPORT void
294 go_plugin_init (GOPlugin *plugin, G_GNUC_UNUSED GOCmdContext *cc)
296 GTypeModule *module;
297 char const *env_var;
298 GSList *dir_list;
299 const char *usr_dir = gnm_usr_dir (TRUE);
301 bindtextdomain (GETTEXT_PACKAGE, gnm_locale_dir ());
302 bindtextdomain (GETTEXT_PACKAGE "-functions", gnm_locale_dir ());
303 #ifdef ENABLE_NLS
304 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
305 #endif
306 module = go_plugin_get_type_module (plugin);
307 go_gnm_component_register_type (module);
308 gnm_init ();
309 if (!gnm_sys_data_dir ())
310 gutils_init ();
311 dir_list = go_slist_create (
312 g_build_filename (gnm_sys_lib_dir (), PLUGIN_SUBDIR, NULL),
313 (usr_dir == NULL ? NULL :
314 g_build_filename (usr_dir, PLUGIN_SUBDIR, NULL)),
315 NULL);
316 dir_list = g_slist_concat
317 (dir_list,
318 go_string_slist_copy (gnm_conf_get_autoformat_extra_dirs ()));
320 env_var = g_getenv ("GNUMERIC_PLUGIN_PATH");
321 if (env_var != NULL)
322 GO_SLIST_CONCAT (dir_list, go_strsplit_to_slist (env_var, G_SEARCHPATH_SEPARATOR));
324 go_components_set_mime_suffix ("application/x-gnumeric", "*.gnumeric");
326 go_plugins_init (go_component_get_command_context (NULL),
327 gnm_conf_get_plugins_file_states (),
328 gnm_conf_get_plugins_active (),
329 dir_list,
330 gnm_conf_get_plugins_activate_newplugins (),
331 gnm_plugin_loader_module_get_type ());
334 G_MODULE_EXPORT void
335 go_plugin_shutdown (G_GNUC_UNUSED GOPlugin *plugin,
336 G_GNUC_UNUSED GOCmdContext *cc)