1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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
24 #include <gnumeric-config.h>
25 #include <glib/gi18n-lib.h>
27 #include "libgnumeric.h"
30 #include "application.h"
32 #include "gnm-format.h"
33 #include "command-context.h"
34 #include "command-context-stderr.h"
36 #include "sheet-object.h"
37 #include "number-match.h"
38 #include "expr-name.h"
40 #include "print-info.h"
41 #include "style-font.h"
43 #include "style-border.h"
44 #include "style-color.h"
46 #include "dependent.h"
47 #include "sheet-autofill.h"
48 #include "sheet-private.h"
50 #include "clipboard.h"
51 #include "gui-clipboard.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"
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>
70 #ifndef HAVE_GTK_ICON_THEME_ADD_RESOURCE_PATH
71 #define gtk_icon_theme_add_resource_path fake_gtk_icon_theme_add_resource_path
75 walk_resource_path (const char *path
, int level
, int size
)
77 char **children
= g_resources_enumerate_children (path
, 0, NULL
);
83 for (i
= 0; children
[i
]; i
++) {
84 const char *child
= children
[i
];
94 subpath
= g_build_path ("/", path
, child
, NULL
);
96 data
= g_resources_lookup_data (subpath
, 0, NULL
);
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
,
108 g_object_unref (pixbuf
);
112 g_bytes_unref (data
);
114 walk_resource_path (subpath
, level
+ 1, size
);
118 g_strfreev (children
);
122 fake_gtk_icon_theme_add_resource_path (GtkIconTheme G_GNUC_UNUSED
*theme
,
125 walk_resource_path (path
, 0, -1);
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 /*********************************
139 0, G_OPTION_ARG_NONE
, ¶m_show_version
,
140 N_("Display Gnumeric's version"),
144 /*********************************
145 * Public Variables */
148 0, G_OPTION_ARG_FILENAME
, ¶m_lib_dir
,
149 N_("Set the root library directory"),
154 0, G_OPTION_ARG_FILENAME
, ¶m_data_dir
,
155 N_("Adjust the root data directory"),
159 /**************************************
160 * Hidden debugging flags */
166 cb_gnm_option_group_post_parse (GOptionContext
*context
,
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 ());
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
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
);
205 * gnm_pre_parse_init:
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.
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
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
);
241 /* Default value is bogus, see 732184. */
242 if (!getenv ("G_ENABLE_DIAGNOSTIC")) {
244 setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE
);
245 #elif defined(HAVE_PUTENV)
246 putenv ("G_ENABLE_DIAGNOSTIC=0");
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);
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
, "");
276 gnm_pre_parse_shutdown (void)
278 go_shell_argv_to_glib_encoding_free ();
284 static gboolean inited
= FALSE
;
289 // Might be duplicate call, but that's ok
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
);
313 gnm_font_init (); /* requires config */
318 gnm_sheet_cell_init ();
320 gui_clipboard_init ();
321 dependent_types_init ();
322 gnm_rendered_value_init ();
325 gnm_autofill_init ();
326 sheet_objects_init ();
329 /* The statically linked in file formats */
330 gnm_xml_sax_read_init ();
331 gnm_xml_sax_write_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
));
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
);
354 gnm_xml_sax_write_shutdown ();
355 gnm_xml_sax_read_shutdown ();
357 gnm_autofill_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 ();
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 ());