GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / libgnumeric.c
blobac1632086db9f6e52cb4b4c88c037c4d6bb3c126
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * libgnumeric.c: global initialization and management code
5 * Copyright (C) 2000-2007 Jody Goldberg (jody@gnome.org)
6 * Copyright (C) 1997-1999 Miguel de Icaza (miguel@kernel.org)
7 * Copyright (C) 2006-2009 Morten Welinder (terra@gnome.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) version 3.
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, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 * USA
24 #include <gnumeric-config.h>
25 #include <glib/gi18n-lib.h>
26 #include "gnumeric.h"
27 #include "libgnumeric.h"
28 #include "gutils.h"
30 #include "application.h"
31 #include "stf.h"
32 #include "gnm-format.h"
33 #include "command-context.h"
34 #include "command-context-stderr.h"
35 #include "workbook.h"
36 #include "sheet-object.h"
37 #include "number-match.h"
38 #include "expr-name.h"
39 #include "func.h"
40 #include "print-info.h"
41 #include "style-font.h"
42 #include "mstyle.h"
43 #include "style-border.h"
44 #include "style-color.h"
45 #include "print.h"
46 #include "dependent.h"
47 #include "sheet-autofill.h"
48 #include "sheet-private.h"
49 #include "xml-sax.h"
50 #include "clipboard.h"
51 #include "gui-clipboard.h"
52 #include "value.h"
53 #include "expr.h"
54 #include "expr-deriv.h"
55 #include "parse-util.h"
56 #include "rendered-value.h"
57 #include "gnumeric-conf.h"
58 #include "gnm-plugin.h"
59 #include "mathfunc.h"
60 #include "hlink.h"
61 #include "wbc-gtk-impl.h"
62 #include "gnmresources.h"
63 #include <goffice/goffice.h>
65 #ifdef HAVE_SYS_RESOURCE_H
66 #include <sys/resource.h>
67 #endif
68 #include <locale.h>
70 #ifndef HAVE_GTK_ICON_THEME_ADD_RESOURCE_PATH
71 #define gtk_icon_theme_add_resource_path fake_gtk_icon_theme_add_resource_path
74 static void
75 walk_resource_path (const char *path, int level, int size)
77 char **children = g_resources_enumerate_children (path, 0, NULL);
78 int i;
80 if (!children)
81 return;
83 for (i = 0; children[i]; i++) {
84 const char *child = children[i];
85 char *subpath;
86 GBytes *data;
88 if (level == 0) {
89 size = atol (child);
90 if (size <= 0)
91 continue;
94 subpath = g_build_path ("/", path, child, NULL);
96 data = g_resources_lookup_data (subpath, 0, NULL);
97 if (data) {
98 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_resource (subpath, NULL);
99 if (pixbuf && size > 0 && strchr (child, '.')) {
100 char *iconname = g_strdup (child);
101 strchr(iconname, '.')[0] = 0;
102 if (gnm_debug_flag ("icons"))
103 g_printerr ("Defining icon %s at size %d\n", iconname, size);
104 gtk_icon_theme_add_builtin_icon (iconname,
105 size,
106 pixbuf);
108 g_object_unref (pixbuf);
109 g_free (iconname);
112 g_bytes_unref (data);
113 } else
114 walk_resource_path (subpath, level + 1, size);
115 g_free (subpath);
118 g_strfreev (children);
121 static void
122 fake_gtk_icon_theme_add_resource_path (GtkIconTheme G_GNUC_UNUSED *theme,
123 const char *path)
125 walk_resource_path (path, 0, -1);
128 #endif
130 static gboolean param_show_version = FALSE;
131 static char *param_lib_dir = NULL;
132 static char *param_data_dir = NULL;
134 static GOptionEntry const libspreadsheet_options [] = {
135 /*********************************
136 * Public Actions */
138 "version", 'v',
139 0, G_OPTION_ARG_NONE, &param_show_version,
140 N_("Display Gnumeric's version"),
141 NULL
144 /*********************************
145 * Public Variables */
147 "lib-dir", 'L',
148 0, G_OPTION_ARG_FILENAME, &param_lib_dir,
149 N_("Set the root library directory"),
150 N_("DIR")
153 "data-dir", 'D',
154 0, G_OPTION_ARG_FILENAME, &param_data_dir,
155 N_("Adjust the root data directory"),
156 N_("DIR")
159 /**************************************
160 * Hidden debugging flags */
162 { NULL }
165 static gboolean
166 cb_gnm_option_group_post_parse (GOptionContext *context,
167 GOptionGroup *group,
168 gpointer data,
169 GError **error)
171 if (param_show_version) {
172 g_print (_("gnumeric version '%s'\ndatadir := '%s'\nlibdir := '%s'\n"),
173 GNM_VERSION_FULL, gnm_sys_data_dir (), gnm_sys_lib_dir ());
174 exit (0);
176 return TRUE;
180 * gnm_get_option_group: (skip)
182 * Returns a #GOptionGroup for the commandline arguments recognized
183 * by libspreadsheet. You should add this group to your #GOptionContext with
184 * g_option_context_add_group(), if you are using g_option_context_parse() to
185 * parse your commandline arguments.
187 * Returns a #GOptionGroup for the commandline arguments recognized
188 * by libspreadsheet
190 * Since: 1.8
192 GOptionGroup *
193 gnm_get_option_group (void)
195 GOptionGroup *group = g_option_group_new ("libspreadsheet",
196 _("Gnumeric Options"), _("Show Gnumeric Options"), NULL, NULL);
197 g_option_group_add_entries (group, libspreadsheet_options);
198 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
199 g_option_group_set_parse_hooks (group, NULL,
200 &cb_gnm_option_group_post_parse);
201 return group;
205 * gnm_pre_parse_init:
206 * @argc:
207 * @argv:
209 * Initialization to be done before cmd line arguments are handled.
210 * Needs to be called first, before any other initialization.
212 * Returns: (transfer none): the arguments in UTF-8 encoding.
214 gchar const **
215 gnm_pre_parse_init (int argc, gchar const **argv)
218 * NO CODE BEFORE THIS POINT, PLEASE!
220 * Using threads (by way of libraries) makes our stack too small in some
221 * circumstances. It is hard to control directly, but setting the stack
222 * limit to something not unlimited seems to work.
224 * See http://bugzilla.gnome.org/show_bug.cgi?id=92131
226 #ifdef HAVE_SYS_RESOURCE_H
227 struct rlimit rlim;
229 if (getrlimit (RLIMIT_STACK, &rlim) == 0) {
230 rlim_t our_lim = 64 * 1024 * 1024;
231 if (rlim.rlim_max != RLIM_INFINITY)
232 our_lim = MIN (our_lim, rlim.rlim_max);
233 if (rlim.rlim_cur != RLIM_INFINITY &&
234 rlim.rlim_cur < our_lim) {
235 rlim.rlim_cur = our_lim;
236 (void)setrlimit (RLIMIT_STACK, &rlim);
239 #endif
241 /* Default value is bogus, see 732184. */
242 if (!getenv ("G_ENABLE_DIAGNOSTIC")) {
243 #ifdef HAVE_SETENV
244 setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
245 #elif defined(HAVE_PUTENV)
246 putenv ("G_ENABLE_DIAGNOSTIC=0");
247 #else
248 /* No big deal. */
249 #endif
252 /* On win32 argv contains 'ansi' encoded args. We need to manually
253 * pull in the real versions and convert them to utf-8 */
254 argv = go_shell_argv_to_glib_encoding (argc, argv);
256 g_set_prgname (argv[0]);
258 /* Make stdout line buffered - we only use it for debug info */
259 setvbuf (stdout, NULL, _IOLBF, 0);
261 gutils_init ();
263 bindtextdomain (GETTEXT_PACKAGE, gnm_locale_dir ());
264 bindtextdomain (GETTEXT_PACKAGE "-functions", gnm_locale_dir ());
265 textdomain (GETTEXT_PACKAGE);
267 /* Force all of the locale segments to update from the environment.
268 * Unless we do this they will default to C
270 setlocale (LC_ALL, "");
272 return argv;
275 void
276 gnm_pre_parse_shutdown (void)
278 go_shell_argv_to_glib_encoding_free ();
281 void
282 gnm_init (void)
284 static gboolean inited = FALSE;
285 if (inited)
286 return;
287 inited = TRUE;
289 // Might be duplicate call, but that's ok
290 gutils_init ();
292 libgoffice_init ();
293 _gnm_register_resource ();
294 if (gdk_screen_get_default ()) {
295 /* Only when we have a gui. */
296 gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (),
297 "/org/gnumeric/gnumeric/icons");
299 go_plugin_service_define ("function_group",
300 &gnm_plugin_service_function_group_get_type);
301 go_plugin_service_define ("ui",
302 &gnm_plugin_service_ui_get_type);
303 go_plugin_service_define ("solver",
304 &gnm_plugin_service_solver_get_type);
305 go_plugin_loader_module_register_version ("gnumeric", GNM_VERSION_FULL);
307 g_object_new (GNM_APP_TYPE, NULL);
308 mathfunc_init ();
310 gnm_style_init ();
311 gnm_conf_init ();
312 gnm_color_init ();
313 gnm_font_init (); /* requires config */
315 value_init ();
316 parse_util_init ();
317 _gnm_expr_init ();
318 gnm_sheet_cell_init ();
319 clipboard_init ();
320 gui_clipboard_init ();
321 dependent_types_init ();
322 gnm_rendered_value_init ();
323 functions_init ();
324 print_init ();
325 gnm_autofill_init ();
326 sheet_objects_init ();
327 _gnm_hlink_init ();
329 /* The statically linked in file formats */
330 gnm_xml_sax_read_init ();
331 gnm_xml_sax_write_init ();
332 stf_init ();
334 /* Make sure that images will be displayed with the correct
335 resolution, see #628472 */
336 go_image_set_default_dpi (gnm_app_display_dpi_get (TRUE),
337 gnm_app_display_dpi_get (FALSE));
340 void
341 gnm_shutdown (void)
343 GSList *plugin_states;
345 gnm_app_clipboard_clear (TRUE);
347 plugin_states = go_plugins_shutdown ();
348 if (NULL != plugin_states) {
349 gnm_conf_set_plugins_file_states (plugin_states);
350 g_slist_free_full (plugin_states, g_free);
353 stf_shutdown ();
354 gnm_xml_sax_write_shutdown ();
355 gnm_xml_sax_read_shutdown ();
357 gnm_autofill_shutdown ();
358 print_shutdown ();
359 functions_shutdown ();
361 gnm_rendered_value_shutdown ();
362 dependent_types_shutdown ();
363 gui_clipboard_shutdown ();
364 clipboard_shutdown ();
365 gnm_sheet_cell_shutdown ();
366 _gnm_expr_deriv_shutdown ();
367 _gnm_expr_shutdown ();
368 parse_util_shutdown ();
369 value_shutdown ();
371 // The style leak printer may access font/border/color info so
372 // shut styles down first.
373 gnm_style_shutdown ();
374 gnm_font_shutdown ();
375 gnm_border_shutdown ();
376 gnm_color_shutdown ();
378 gnm_conf_shutdown ();
380 _gnm_unregister_resource ();
381 libgoffice_shutdown ();
382 go_plugin_services_shutdown ();
383 g_object_unref (gnm_app_get_app ());
384 gutils_shutdown ();