Updated Czech translation
[gnumeric.git] / component / gnumeric.c
blob215aa60b8dd5e01da129dd797bf1a257e5d66ab7
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, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
26 #include <gnumeric-config.h>
27 #include <gnumeric.h>
28 #include <gnm-plugin.h>
29 #include <gnumeric-conf.h>
30 #include <gui-file.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 ();
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 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 ();
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 = wb_view_new_from_input (input, NULL, NULL, io_context, NULL);
143 gognm->wb = wb_view_get_workbook (gognm->wv);
144 g_object_unref (io_context);
145 go_gnm_component_update_data (gognm);
148 static void
149 go_gnm_component_render (GOComponent *component, cairo_t *cr, double width_pixels, double height_pixels)
151 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
152 GnmRange range;
154 if (!gognm->sheet)
155 go_gnm_component_update_data (gognm);
157 range_init (&range, gognm->col_start, gognm->row_start, gognm->col_end, gognm->row_end);
158 cairo_save (cr);
159 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
160 cairo_scale (cr, ((double) width_pixels) / gognm->width, ((double) height_pixels) / gognm->height);
161 cairo_rectangle (cr, 0., 0., gognm->width, gognm->height);
162 cairo_clip (cr); /* not sure it is necessary */
163 gnm_gtk_print_cell_range (cr, gognm->sheet, &range, 0., 0.,
164 gognm->sheet->print_info);
165 /* Now render objects */
166 gnm_print_sheet_objects (cr, gognm->sheet, &range, 0., 0.);
167 cairo_restore (cr);
170 static void
171 cb_gognm_save (G_GNUC_UNUSED GtkAction *a, WBCGtk *wbcg)
173 gpointer data = g_object_get_data (G_OBJECT (wbcg), "component");
174 if (GO_IS_COMPONENT (data)) {
175 GOComponent *component = GO_COMPONENT (data);
176 /* update the component data since not all clients will call set_data */
177 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
178 WorkbookView *wv = wb_control_view (WORKBOOK_CONTROL (wbcg));
179 if (wv != gognm->wv) {
180 if (gognm->wv != NULL) {
181 g_object_unref (gognm->wv);
182 g_object_unref (gognm->wb);
184 gognm->wv = g_object_ref (wv);
185 gognm->wb = g_object_ref (wb_view_get_workbook (wv));
187 go_doc_set_dirty (GO_DOC (gognm->wb), FALSE);
188 go_gnm_component_update_data (gognm);
189 go_component_emit_changed (component);
190 } else
191 gui_file_save (wbcg, wb_control_view (WORKBOOK_CONTROL (wbcg)));
194 static GtkActionEntry const actions[] = {
195 /* File */
196 { "FileSaveEmbed", GTK_STOCK_SAVE, NULL,
197 NULL, N_("Save the embedded workbook"),
198 G_CALLBACK (cb_gognm_save) }
201 static void
202 cb_editor_destroyed (GOGnmComponent *gognm)
204 gognm->edited = NULL;
207 static GtkWindow*
208 go_gnm_component_edit (GOComponent *component)
210 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
211 WorkbookView *wv;
212 if (gognm->edited) {
213 gdk_window_raise (gtk_widget_get_parent_window (GTK_WIDGET (wbcg_toplevel (gognm->edited))));
214 return wbcg_toplevel (gognm->edited);
216 if (!gognm->wv) {
217 component->ascent = 0.;
218 component->descent = 0.;
219 component->width = 0.;
220 wv = workbook_view_new (workbook_new_with_sheets (1));
221 } else {
222 GOCmdContext *cc = go_component_get_command_context ();
223 GOIOContext *io_context = GO_IS_IO_CONTEXT (cc)? GO_IO_CONTEXT (g_object_ref (cc)): go_io_context_new (cc);
224 GsfInput *input = gsf_input_memory_new (component->data, component->length, FALSE);
226 g_object_set (G_OBJECT (io_context), "exec-main-loop", FALSE, NULL);
227 wv = wb_view_new_from_input (input, NULL, NULL, io_context, NULL);
228 g_object_unref (io_context);
230 set_uifilename ("Gnumeric-embed.xml", actions, G_N_ELEMENTS (actions));
231 gognm->edited = wbc_gtk_new (wv, NULL, NULL, NULL);
233 g_object_set_data (G_OBJECT (gognm->edited), "component", gognm);
234 g_signal_connect_swapped (gognm->edited->toplevel, "destroy",
235 G_CALLBACK (cb_editor_destroyed), gognm);
236 return wbcg_toplevel (gognm->edited);
239 static void
240 go_gnm_component_finalize (GObject *obj)
242 GOGnmComponent *gognm = GO_GNM_COMPONENT (obj);
243 if (gognm->wv != NULL) {
244 g_object_unref (gognm->wv);
245 g_object_unref (gognm->wb);
246 gognm->wv = NULL;
248 if (gognm->edited != NULL) {
249 g_object_unref (wb_control_view (WORKBOOK_CONTROL (gognm->edited)));
250 gognm->edited = NULL;
252 G_OBJECT_CLASS (gognm_parent_klass)->finalize (obj);
255 static void
256 go_gnm_component_init (GOComponent *component)
258 GOGnmComponent *gognm = GO_GNM_COMPONENT (component);
259 component->resizable = FALSE;
260 component->editable = TRUE;
261 component->snapshot_type = GO_SNAPSHOT_SVG;
262 gognm->row_start = gognm->col_start = 0;
263 gognm->sheet = NULL;
264 gognm->row_end = 9;
265 gognm->col_end = 4;
268 static void
269 go_gnm_component_class_init (GOComponentClass *klass)
271 GObjectClass *obj_klass = (GObjectClass *) klass;
272 obj_klass->finalize = go_gnm_component_finalize;
274 gognm_parent_klass = (GObjectClass*) g_type_class_peek_parent (klass);
276 klass->get_data = go_gnm_component_get_data;
277 klass->set_data = go_gnm_component_set_data;
278 klass->render = go_gnm_component_render;
279 klass->edit = go_gnm_component_edit;
282 GSF_DYNAMIC_CLASS (GOGnmComponent, go_gnm_component,
283 go_gnm_component_class_init, go_gnm_component_init,
284 GO_TYPE_COMPONENT)
286 /*************************************************************************************/
288 G_MODULE_EXPORT void
289 go_plugin_init (GOPlugin *plugin, G_GNUC_UNUSED GOCmdContext *cc)
291 GTypeModule *module;
292 char const *env_var;
293 GSList *dir_list;
294 const char *usr_dir = gnm_usr_dir (TRUE);
296 bindtextdomain (GETTEXT_PACKAGE, gnm_locale_dir ());
297 bindtextdomain (GETTEXT_PACKAGE "-functions", gnm_locale_dir ());
298 #ifdef ENABLE_NLS
299 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
300 #endif
301 module = go_plugin_get_type_module (plugin);
302 go_gnm_component_register_type (module);
303 gnm_init ();
304 if (!gnm_sys_data_dir ())
305 gutils_init ();
306 dir_list = go_slist_create (
307 g_build_filename (gnm_sys_lib_dir (), PLUGIN_SUBDIR, NULL),
308 (usr_dir == NULL ? NULL :
309 g_build_filename (usr_dir, PLUGIN_SUBDIR, NULL)),
310 NULL);
311 dir_list = g_slist_concat
312 (dir_list,
313 go_string_slist_copy (gnm_conf_get_autoformat_extra_dirs ()));
315 env_var = g_getenv ("GNUMERIC_PLUGIN_PATH");
316 if (env_var != NULL)
317 GO_SLIST_CONCAT (dir_list, go_strsplit_to_slist (env_var, G_SEARCHPATH_SEPARATOR));
319 go_components_set_mime_suffix ("application/x-gnumeric", "*.gnumeric");
321 go_plugins_init (go_component_get_command_context (),
322 gnm_conf_get_plugins_file_states (),
323 gnm_conf_get_plugins_active (),
324 dir_list,
325 gnm_conf_get_plugins_activate_new (),
326 gnm_plugin_loader_module_get_type ());
329 G_MODULE_EXPORT void
330 go_plugin_shutdown (G_GNUC_UNUSED GOPlugin *plugin,
331 G_GNUC_UNUSED GOCmdContext *cc)