GUI: Dead kittens.
[gnumeric.git] / src / libgnumeric.c
blob1437d80cf73a394b89a009a7d84f0e0e91a03bbc
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 "dialogs/embedded-ui.h"
64 #include <goffice/goffice.h>
66 #ifdef HAVE_SYS_RESOURCE_H
67 #include <sys/resource.h>
68 #endif
69 #include <locale.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
75 static void
76 walk_resource_path (const char *path, int level, int size)
78 char **children = g_resources_enumerate_children (path, 0, NULL);
79 int i;
81 if (!children)
82 return;
84 for (i = 0; children[i]; i++) {
85 const char *child = children[i];
86 char *subpath;
87 GBytes *data;
89 if (level == 0) {
90 size = atol (child);
91 if (size <= 0)
92 continue;
95 subpath = g_build_path ("/", path, child, NULL);
97 data = g_resources_lookup_data (subpath, 0, NULL);
98 if (data) {
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,
106 size,
107 pixbuf);
109 g_object_unref (pixbuf);
110 g_free (iconname);
113 g_bytes_unref (data);
114 } else
115 walk_resource_path (subpath, level + 1, size);
116 g_free (subpath);
119 g_strfreev (children);
122 static void
123 fake_gtk_icon_theme_add_resource_path (GtkIconTheme G_GNUC_UNUSED *theme,
124 const char *path)
126 walk_resource_path (path, 0, -1);
129 #endif
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 /*********************************
140 * Public Actions */
142 "version", 'v',
143 0, G_OPTION_ARG_NONE, &param_show_version,
144 N_("Display Gnumeric's version"),
145 NULL
148 /*********************************
149 * Public Variables */
151 "lib-dir", 'L',
152 0, G_OPTION_ARG_FILENAME, &param_lib_dir,
153 N_("Set the root library directory"),
154 N_("DIR")
157 "data-dir", 'D',
158 0, G_OPTION_ARG_FILENAME, &param_data_dir,
159 N_("Adjust the root data directory"),
160 N_("DIR")
163 /**************************************
164 * Hidden debugging flags */
166 { NULL }
169 static gboolean
170 cb_gnm_option_group_post_parse (GOptionContext *context,
171 GOptionGroup *group,
172 gpointer data,
173 GError **error)
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 ());
178 exit (0);
180 return TRUE;
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
192 * by libspreadsheet
194 * Since: 1.8
196 GOptionGroup *
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);
205 return group;
209 * gnm_pre_parse_init:
210 * @argc:
211 * @argv:
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.
218 gchar const **
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
231 struct rlimit rlim;
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);
243 #endif
245 /* Default value is bogus, see 732184. */
246 if (!getenv ("G_ENABLE_DIAGNOSTIC")) {
247 #ifdef HAVE_SETENV
248 setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
249 #elif defined(HAVE_PUTENV)
250 putenv ("G_ENABLE_DIAGNOSTIC=0");
251 #else
252 /* No big deal. */
253 #endif
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);
265 gutils_init ();
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, "");
276 return argv;
279 void
280 gnm_pre_parse_shutdown (void)
282 go_shell_argv_to_glib_encoding_free ();
285 void
286 gnm_init (void)
288 static gboolean inited = FALSE;
289 if (inited)
290 return;
291 inited = TRUE;
293 // Might be duplicate call, but that's ok
294 gutils_init ();
296 libgoffice_init ();
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);
313 mathfunc_init ();
315 gnm_style_init ();
316 gnm_conf_init ();
317 gnm_color_init ();
318 gnm_font_init (); /* requires config */
320 value_init ();
321 parse_util_init ();
322 _gnm_expr_init ();
323 gnm_sheet_cell_init ();
324 clipboard_init ();
325 gui_clipboard_init ();
326 dependent_types_init ();
327 gnm_rendered_value_init ();
328 functions_init ();
329 print_init ();
330 gnm_autofill_init ();
331 sheet_objects_init ();
332 _gnm_hlink_init ();
334 /* The statically linked in file formats */
335 gnm_xml_sax_read_init ();
336 gnm_xml_sax_write_init ();
337 stf_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));
345 void
346 gnm_shutdown (void)
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);
358 stf_shutdown ();
359 gnm_xml_sax_write_shutdown ();
360 gnm_xml_sax_read_shutdown ();
362 gnm_autofill_shutdown ();
363 print_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 ();
374 value_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 ());
389 gutils_shutdown ();