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 "dialogs/embedded-ui.h"
64 #include <goffice/goffice.h>
66 #ifdef HAVE_SYS_RESOURCE_H
67 #include <sys/resource.h>
71 #ifndef HAVE_GTK_ICON_THEME_ADD_RESOURCE_PATH
72 #define gtk_icon_theme_add_resource_path fake_gtk_icon_theme_add_resource_path
76 walk_resource_path (const char *path
, int level
, int size
)
78 char **children
= g_resources_enumerate_children (path
, 0, NULL
);
84 for (i
= 0; children
[i
]; i
++) {
85 const char *child
= children
[i
];
95 subpath
= g_build_path ("/", path
, child
, NULL
);
97 data
= g_resources_lookup_data (subpath
, 0, NULL
);
99 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_resource (subpath
, NULL
);
100 if (pixbuf
&& size
> 0 && strchr (child
, '.')) {
101 char *iconname
= g_strdup (child
);
102 strchr(iconname
, '.')[0] = 0;
103 if (gnm_debug_flag ("icons"))
104 g_printerr ("Defining icon %s at size %d\n", iconname
, size
);
105 gtk_icon_theme_add_builtin_icon (iconname
,
109 g_object_unref (pixbuf
);
113 g_bytes_unref (data
);
115 walk_resource_path (subpath
, level
+ 1, size
);
119 g_strfreev (children
);
123 fake_gtk_icon_theme_add_resource_path (GtkIconTheme G_GNUC_UNUSED
*theme
,
126 walk_resource_path (path
, 0, -1);
131 /* TODO : get rid of this monstrosity */
132 gboolean initial_workbook_open_complete
= FALSE
;
134 static gboolean param_show_version
= FALSE
;
135 static char *param_lib_dir
= NULL
;
136 static char *param_data_dir
= NULL
;
138 static GOptionEntry
const libspreadsheet_options
[] = {
139 /*********************************
143 0, G_OPTION_ARG_NONE
, ¶m_show_version
,
144 N_("Display Gnumeric's version"),
148 /*********************************
149 * Public Variables */
152 0, G_OPTION_ARG_FILENAME
, ¶m_lib_dir
,
153 N_("Set the root library directory"),
158 0, G_OPTION_ARG_FILENAME
, ¶m_data_dir
,
159 N_("Adjust the root data directory"),
163 /**************************************
164 * Hidden debugging flags */
170 cb_gnm_option_group_post_parse (GOptionContext
*context
,
175 if (param_show_version
) {
176 g_print (_("gnumeric version '%s'\ndatadir := '%s'\nlibdir := '%s'\n"),
177 GNM_VERSION_FULL
, gnm_sys_data_dir (), gnm_sys_lib_dir ());
184 * gnm_get_option_group: (skip)
186 * Returns a #GOptionGroup for the commandline arguments recognized
187 * by libspreadsheet. You should add this group to your #GOptionContext with
188 * g_option_context_add_group(), if you are using g_option_context_parse() to
189 * parse your commandline arguments.
191 * Returns a #GOptionGroup for the commandline arguments recognized
197 gnm_get_option_group (void)
199 GOptionGroup
*group
= g_option_group_new ("libspreadsheet",
200 _("Gnumeric Options"), _("Show Gnumeric Options"), NULL
, NULL
);
201 g_option_group_add_entries (group
, libspreadsheet_options
);
202 g_option_group_set_translation_domain (group
, GETTEXT_PACKAGE
);
203 g_option_group_set_parse_hooks (group
, NULL
,
204 &cb_gnm_option_group_post_parse
);
209 * gnm_pre_parse_init:
213 * Initialization to be done before cmd line arguments are handled.
214 * Needs to be called first, before any other initialization.
216 * Returns: (transfer none): the arguments in UTF-8 encoding.
219 gnm_pre_parse_init (int argc
, gchar
const **argv
)
222 * NO CODE BEFORE THIS POINT, PLEASE!
224 * Using threads (by way of libraries) makes our stack too small in some
225 * circumstances. It is hard to control directly, but setting the stack
226 * limit to something not unlimited seems to work.
228 * See http://bugzilla.gnome.org/show_bug.cgi?id=92131
230 #ifdef HAVE_SYS_RESOURCE_H
233 if (getrlimit (RLIMIT_STACK
, &rlim
) == 0) {
234 rlim_t our_lim
= 64 * 1024 * 1024;
235 if (rlim
.rlim_max
!= RLIM_INFINITY
)
236 our_lim
= MIN (our_lim
, rlim
.rlim_max
);
237 if (rlim
.rlim_cur
!= RLIM_INFINITY
&&
238 rlim
.rlim_cur
< our_lim
) {
239 rlim
.rlim_cur
= our_lim
;
240 (void)setrlimit (RLIMIT_STACK
, &rlim
);
245 /* Default value is bogus, see 732184. */
246 if (!getenv ("G_ENABLE_DIAGNOSTIC")) {
248 setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE
);
249 #elif defined(HAVE_PUTENV)
250 putenv ("G_ENABLE_DIAGNOSTIC=0");
256 /* On win32 argv contains 'ansi' encoded args. We need to manually
257 * pull in the real versions and convert them to utf-8 */
258 argv
= go_shell_argv_to_glib_encoding (argc
, argv
);
260 g_set_prgname (argv
[0]);
262 /* Make stdout line buffered - we only use it for debug info */
263 setvbuf (stdout
, NULL
, _IOLBF
, 0);
267 bindtextdomain (GETTEXT_PACKAGE
, gnm_locale_dir ());
268 bindtextdomain (GETTEXT_PACKAGE
"-functions", gnm_locale_dir ());
269 textdomain (GETTEXT_PACKAGE
);
271 /* Force all of the locale segments to update from the environment.
272 * Unless we do this they will default to C
274 setlocale (LC_ALL
, "");
280 gnm_pre_parse_shutdown (void)
282 go_shell_argv_to_glib_encoding_free ();
288 static gboolean inited
= FALSE
;
293 // Might be duplicate call, but that's ok
297 _gnm_register_resource ();
298 if (gdk_screen_get_default ()) {
299 /* Only when we have a gui. */
300 gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (),
301 "/org/gnumeric/gnumeric/icons");
303 gnm_register_ui_files ();
304 go_plugin_service_define ("function_group",
305 &gnm_plugin_service_function_group_get_type
);
306 go_plugin_service_define ("ui",
307 &gnm_plugin_service_ui_get_type
);
308 go_plugin_service_define ("solver",
309 &gnm_plugin_service_solver_get_type
);
310 go_plugin_loader_module_register_version ("gnumeric", GNM_VERSION_FULL
);
312 g_object_new (GNM_APP_TYPE
, NULL
);
318 gnm_font_init (); /* requires config */
323 gnm_sheet_cell_init ();
325 gui_clipboard_init ();
326 dependent_types_init ();
327 gnm_rendered_value_init ();
330 gnm_autofill_init ();
331 sheet_objects_init ();
334 /* The statically linked in file formats */
335 gnm_xml_sax_read_init ();
336 gnm_xml_sax_write_init ();
339 /* Make sure that images will be displayed with the correct
340 resolution, see #628472 */
341 go_image_set_default_dpi (gnm_app_display_dpi_get (TRUE
),
342 gnm_app_display_dpi_get (FALSE
));
348 GSList
*plugin_states
;
350 gnm_app_clipboard_clear (TRUE
);
352 plugin_states
= go_plugins_shutdown ();
353 if (NULL
!= plugin_states
) {
354 gnm_conf_set_plugins_file_states (plugin_states
);
355 g_slist_free_full (plugin_states
, g_free
);
359 gnm_xml_sax_write_shutdown ();
360 gnm_xml_sax_read_shutdown ();
362 gnm_autofill_shutdown ();
364 functions_shutdown ();
366 gnm_rendered_value_shutdown ();
367 dependent_types_shutdown ();
368 gui_clipboard_shutdown ();
369 clipboard_shutdown ();
370 gnm_sheet_cell_shutdown ();
371 _gnm_expr_deriv_shutdown ();
372 _gnm_expr_shutdown ();
373 parse_util_shutdown ();
376 // The style leak printer may access font/border/color info so
377 // shut styles down first.
378 gnm_style_shutdown ();
379 gnm_font_shutdown ();
380 gnm_border_shutdown ();
381 gnm_color_shutdown ();
383 gnm_conf_shutdown ();
385 _gnm_unregister_resource ();
386 libgoffice_shutdown ();
387 go_plugin_services_shutdown ();
388 g_object_unref (gnm_app_get_app ());