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