2 * libgnumeric.c: global initialization and management code
4 * Copyright (C) 2000-2007 Jody Goldberg (jody@gnome.org)
5 * Copyright (C) 1997-1999 Miguel de Icaza (miguel@kernel.org)
6 * Copyright (C) 2006-2009 Morten Welinder (terra@gnome.org)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) version 3.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
26 #include <libgnumeric.h>
29 #include <application.h>
31 #include <gnm-format.h>
32 #include <command-context.h>
33 #include <command-context-stderr.h>
35 #include <sheet-object.h>
36 #include <number-match.h>
37 #include <expr-name.h>
39 #include <print-info.h>
40 #include <style-font.h>
42 #include <style-border.h>
43 #include <style-color.h>
45 #include <dependent.h>
46 #include <sheet-autofill.h>
47 #include <sheet-private.h>
49 #include <clipboard.h>
50 #include <gui-clipboard.h>
53 #include <expr-deriv.h>
54 #include <parse-util.h>
55 #include <rendered-value.h>
56 #include <gnumeric-conf.h>
57 #include <gnm-plugin.h>
60 #include <wbc-gtk-impl.h>
61 #include <gnmresources.h>
62 #include <goffice/goffice.h>
64 #ifdef HAVE_SYS_RESOURCE_H
65 #include <sys/resource.h>
69 #ifndef HAVE_GTK_ICON_THEME_ADD_RESOURCE_PATH
70 #define gtk_icon_theme_add_resource_path fake_gtk_icon_theme_add_resource_path
74 walk_resource_path (const char *path
, int level
, int size
)
76 char **children
= g_resources_enumerate_children (path
, 0, NULL
);
82 for (i
= 0; children
[i
]; i
++) {
83 const char *child
= children
[i
];
93 subpath
= g_build_path ("/", path
, child
, NULL
);
95 data
= g_resources_lookup_data (subpath
, 0, NULL
);
97 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_resource (subpath
, NULL
);
98 if (pixbuf
&& size
> 0 && strchr (child
, '.')) {
99 char *iconname
= g_strdup (child
);
100 strchr(iconname
, '.')[0] = 0;
101 if (gnm_debug_flag ("icons"))
102 g_printerr ("Defining icon %s at size %d\n", iconname
, size
);
103 gtk_icon_theme_add_builtin_icon (iconname
,
107 g_object_unref (pixbuf
);
111 g_bytes_unref (data
);
113 walk_resource_path (subpath
, level
+ 1, size
);
117 g_strfreev (children
);
121 fake_gtk_icon_theme_add_resource_path (GtkIconTheme G_GNUC_UNUSED
*theme
,
124 walk_resource_path (path
, 0, -1);
129 static gboolean param_show_version
= FALSE
;
130 static char *param_lib_dir
= NULL
;
131 static char *param_data_dir
= NULL
;
133 static GOptionEntry
const libspreadsheet_options
[] = {
134 /*********************************
138 0, G_OPTION_ARG_NONE
, ¶m_show_version
,
139 N_("Display Gnumeric's version"),
143 /*********************************
144 * Public Variables */
147 0, G_OPTION_ARG_FILENAME
, ¶m_lib_dir
,
148 N_("Set the root library directory"),
153 0, G_OPTION_ARG_FILENAME
, ¶m_data_dir
,
154 N_("Adjust the root data directory"),
158 /**************************************
159 * Hidden debugging flags */
165 cb_gnm_option_group_post_parse (GOptionContext
*context
,
170 if (param_show_version
) {
171 g_print (_("gnumeric version '%s'\ndatadir := '%s'\nlibdir := '%s'\n"),
172 GNM_VERSION_FULL
, gnm_sys_data_dir (), gnm_sys_lib_dir ());
179 * gnm_get_option_group: (skip)
181 * Returns a #GOptionGroup for the commandline arguments recognized
182 * by libspreadsheet. You should add this group to your #GOptionContext with
183 * g_option_context_add_group(), if you are using g_option_context_parse() to
184 * parse your commandline arguments.
186 * Returns a #GOptionGroup for the commandline arguments recognized
192 gnm_get_option_group (void)
194 GOptionGroup
*group
= g_option_group_new ("libspreadsheet",
195 _("Gnumeric Options"), _("Show Gnumeric Options"), NULL
, NULL
);
196 g_option_group_add_entries (group
, libspreadsheet_options
);
197 g_option_group_set_translation_domain (group
, GETTEXT_PACKAGE
);
198 g_option_group_set_parse_hooks (group
, NULL
,
199 &cb_gnm_option_group_post_parse
);
204 * gnm_pre_parse_init:
208 * Initialization to be done before cmd line arguments are handled.
209 * Needs to be called first, before any other initialization.
211 * Returns: (transfer none): the arguments in UTF-8 encoding.
214 gnm_pre_parse_init (int argc
, gchar
const **argv
)
217 * NO CODE BEFORE THIS POINT, PLEASE!
219 * Using threads (by way of libraries) makes our stack too small in some
220 * circumstances. It is hard to control directly, but setting the stack
221 * limit to something not unlimited seems to work.
223 * See http://bugzilla.gnome.org/show_bug.cgi?id=92131
225 #ifdef HAVE_SYS_RESOURCE_H
228 if (getrlimit (RLIMIT_STACK
, &rlim
) == 0) {
229 rlim_t our_lim
= 64 * 1024 * 1024;
230 if (rlim
.rlim_max
!= RLIM_INFINITY
)
231 our_lim
= MIN (our_lim
, rlim
.rlim_max
);
232 if (rlim
.rlim_cur
!= RLIM_INFINITY
&&
233 rlim
.rlim_cur
< our_lim
) {
234 rlim
.rlim_cur
= our_lim
;
235 (void)setrlimit (RLIMIT_STACK
, &rlim
);
240 /* Default value is bogus, see 732184. */
241 if (!getenv ("G_ENABLE_DIAGNOSTIC")) {
243 setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE
);
244 #elif defined(HAVE_PUTENV)
245 putenv ("G_ENABLE_DIAGNOSTIC=0");
251 /* On win32 argv contains 'ansi' encoded args. We need to manually
252 * pull in the real versions and convert them to utf-8 */
253 argv
= go_shell_argv_to_glib_encoding (argc
, argv
);
255 g_set_prgname (argv
[0]);
257 /* Make stdout line buffered - we only use it for debug info */
258 setvbuf (stdout
, NULL
, _IOLBF
, 0);
262 bindtextdomain (GETTEXT_PACKAGE
, gnm_locale_dir ());
263 bindtextdomain (GETTEXT_PACKAGE
"-functions", gnm_locale_dir ());
264 textdomain (GETTEXT_PACKAGE
);
266 /* Force all of the locale segments to update from the environment.
267 * Unless we do this they will default to C
269 setlocale (LC_ALL
, "");
275 gnm_pre_parse_shutdown (void)
277 go_shell_argv_to_glib_encoding_free ();
283 static gboolean inited
= FALSE
;
288 // Might be duplicate call, but that's ok
292 _gnm_register_resource ();
293 if (gdk_screen_get_default ()) {
294 /* Only when we have a gui. */
295 gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (),
296 "/org/gnumeric/gnumeric/icons");
299 gnm_plugins_service_init ();
301 g_object_new (GNM_APP_TYPE
, NULL
);
307 gnm_font_init (); /* requires config */
312 gnm_sheet_cell_init ();
314 gui_clipboard_init ();
315 dependent_types_init ();
316 gnm_rendered_value_init ();
319 gnm_autofill_init ();
320 sheet_objects_init ();
323 /* The statically linked in file formats */
324 gnm_xml_sax_read_init ();
325 gnm_xml_sax_write_init ();
328 /* Make sure that images will be displayed with the correct
329 resolution, see #628472 */
330 go_image_set_default_dpi (gnm_app_display_dpi_get (TRUE
),
331 gnm_app_display_dpi_get (FALSE
));
337 GSList
*plugin_states
;
339 gnm_app_clipboard_clear (TRUE
);
341 plugin_states
= go_plugins_shutdown ();
342 if (NULL
!= plugin_states
) {
343 gnm_conf_set_plugins_file_states (plugin_states
);
344 g_slist_free_full (plugin_states
, g_free
);
348 gnm_xml_sax_write_shutdown ();
349 gnm_xml_sax_read_shutdown ();
351 gnm_autofill_shutdown ();
353 functions_shutdown ();
355 gnm_rendered_value_shutdown ();
356 dependent_types_shutdown ();
357 gui_clipboard_shutdown ();
358 clipboard_shutdown ();
359 gnm_sheet_cell_shutdown ();
360 _gnm_expr_deriv_shutdown ();
361 _gnm_expr_shutdown ();
362 parse_util_shutdown ();
365 // The style leak printer may access font/border/color info so
366 // shut styles down first.
367 gnm_style_shutdown ();
368 gnm_font_shutdown ();
369 gnm_border_shutdown ();
370 gnm_color_shutdown ();
372 gnm_conf_shutdown ();
374 _gnm_unregister_resource ();
375 libgoffice_shutdown ();
376 go_plugin_services_shutdown ();
377 g_object_unref (gnm_app_get_app ());