1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * main-application.c: Main entry point for the Gnumeric application
6 * Jon Kåre Hellan <hellan@acm.org>
7 * Morten Welinder <terra@gnome.org>
8 * Jody Goldberg <jody@gnome.org>
10 * Copyright (C) 2002-2004, Jon Kåre Hellan
13 #include <gnumeric-config.h>
14 #include <glib/gi18n.h>
16 #include "libgnumeric.h"
18 #define _WIN32_WINNT 0x0501
23 #include "command-context.h"
24 #include <goffice/goffice.h>
25 #include "io-context-gtk.h"
26 /* TODO: Get rid of this one */
27 #include "command-context-stderr.h"
28 #include "wbc-gtk-impl.h"
29 #include "workbook-view.h"
32 #include "gnumeric-conf.h"
33 #include "gnumeric-paths.h"
37 #include "gnm-plugin.h"
38 #include "application.h"
42 #include <glib/gstdio.h>
44 #include <sys/types.h>
49 static gboolean immediate_exit_flag
= FALSE
;
50 static gboolean gnumeric_no_splash
= FALSE
;
51 static gboolean gnumeric_no_warnings
= FALSE
;
52 static gchar
*geometry
= NULL
;
53 static gchar
**startup_files
;
55 static const GOptionEntry gnumeric_options
[] = {
56 /*********************************
58 { "geometry", 'g', 0, G_OPTION_ARG_STRING
, &geometry
,
59 N_("Specify the size and location of the initial window"),
60 N_("WIDTHxHEIGHT+XOFF+YOFF")
62 { "no-splash", 0, 0, G_OPTION_ARG_NONE
, &gnumeric_no_splash
,
63 N_("Don't show splash screen"), NULL
},
64 { "no-warnings", 0, 0, G_OPTION_ARG_NONE
, &gnumeric_no_warnings
,
65 N_("Don't display warning dialogs when importing"),
69 /*********************************
73 G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_NONE
, &immediate_exit_flag
,
74 N_("Exit immediately after loading the selected books"),
77 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_FILENAME_ARRAY
, &startup_files
, NULL
, NULL
},
83 handle_paint_events (void)
85 /* FIXME: we need to mask input events correctly here */
86 /* Show something coherent */
87 while (gtk_events_pending () && !initial_workbook_open_complete
)
88 gtk_main_iteration_do (FALSE
);
91 static GObject
*program
= NULL
;
94 gnumeric_arg_shutdown (void)
97 g_object_unref (program
);
103 gnumeric_arg_parse (int argc
, char **argv
)
105 GOptionContext
*ocontext
;
106 GError
*error
= NULL
;
108 ocontext
= g_option_context_new (_("[FILE ...]"));
109 g_option_context_add_main_entries (ocontext
, gnumeric_options
, GETTEXT_PACKAGE
);
110 g_option_context_add_group (ocontext
, gnm_get_option_group ());
112 #if defined(G_OS_WIN32)
113 /* we have already translated to utf8, do not do it again.
114 * http://bugzilla.gnome.org/show_bug.cgi?id=361321 */
115 g_option_context_set_delocalize (ocontext
, FALSE
);
118 g_option_context_add_group (ocontext
, gtk_get_option_group (TRUE
));
119 g_option_context_parse (ocontext
, &argc
, &argv
, &error
);
122 g_option_context_free (ocontext
);
125 g_printerr (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
126 error
->message
, argv
[0]);
127 g_error_free (error
);
131 gtk_init (&argc
, &argv
);
135 * WARNING WARNING WARNING
136 * This does not belong here
137 * but it is expedient for now to get things to compile
139 #warning "REMOVE REMOVE REMOVE"
141 store_plugin_state (void)
143 GSList
*active_plugins
= go_plugins_get_active_plugins ();
144 gnm_conf_set_plugins_active (active_plugins
);
145 g_slist_free (active_plugins
);
149 cb_kill_wbcg (WBCGtk
*wbcg
)
151 gboolean still_open
= wbc_gtk_close (wbcg
);
152 g_assert (!still_open
);
157 cb_workbook_removed (void)
159 if (gnm_app_workbook_list () == NULL
) {
165 main (int argc
, char const **argv
)
167 gboolean opened_workbook
= FALSE
;
170 GSList
*wbcgs_to_kill
= NULL
;
174 gboolean has_console
;
177 /* No code before here, we need to init threads */
178 argv
= gnm_pre_parse_init (argc
, argv
);
181 * Attempt to disable Ubuntu's funky, non-working scroll
182 * bars. This needs to be done before gtk starts loading
183 * modules. Note: the following call will not replace
184 * an existing setting, so you can run with =1 if you like.
186 g_setenv ("LIBOVERLAY_SCROLLBAR", "0", FALSE
);
191 typedef BOOL (CALLBACK
* LPFNATTACHCONSOLE
)(DWORD
);
192 LPFNATTACHCONSOLE MyAttachConsole
;
195 if ((hmod
= GetModuleHandle("kernel32.dll"))) {
196 MyAttachConsole
= (LPFNATTACHCONSOLE
) GetProcAddress(hmod
, "AttachConsole");
197 if (MyAttachConsole
&& MyAttachConsole(ATTACH_PARENT_PROCESS
)) {
198 freopen("CONOUT$", "w", stdout
);
199 freopen("CONOUT$", "w", stderr
);
200 dup2(fileno(stdout
), 1);
201 dup2(fileno(stderr
), 2);
208 gnumeric_arg_parse (argc
, (char **)argv
);
209 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
210 bind_textdomain_codeset (GETTEXT_PACKAGE
"-functions", "UTF-8");
212 gnm_session_init (argv
[0]);
216 cc
= gnm_cmd_context_stderr_new ();
217 go_component_set_default_command_context (cc
);
220 cc
= g_object_new (GNM_TYPE_IO_CONTEXT_GTK
,
221 "show-splash", !gnumeric_no_splash
,
222 "show-warnings", !gnumeric_no_warnings
,
224 ioc
= GO_IO_CONTEXT (g_object_ref (cc
));
225 handle_paint_events ();
227 /* Keep in sync with .desktop file */
228 g_set_application_name (_("Gnumeric Spreadsheet"));
229 gnm_plugins_init (GO_CMD_CONTEXT (ioc
));
234 N
= g_strv_length (startup_files
);
235 go_io_context_set_num_files (ioc
, N
);
236 for (i
= 0; i
< N
&& !initial_workbook_open_complete
; i
++) {
237 char *uri
= go_shell_arg_to_uri (startup_files
[i
]);
240 g_warning ("Ignoring invalid URI.");
244 go_io_context_processing_file (ioc
, uri
);
245 wbv
= workbook_view_new_from_uri (uri
, NULL
, ioc
, NULL
);
248 if (go_io_error_occurred (ioc
) ||
249 go_io_warning_occurred (ioc
)) {
250 go_io_error_display (ioc
);
251 go_io_error_clear (ioc
);
256 workbook_update_history (wb_view_get_workbook (wbv
), GNM_FILE_SAVE_AS_STYLE_SAVE
);
258 wbcg
= wbc_gtk_new (wbv
, NULL
, NULL
, geometry
);
260 sheet_update (wb_view_cur_sheet (wbv
));
261 opened_workbook
= TRUE
;
262 gnm_io_context_gtk_set_transient_for (GNM_IO_CONTEXT_GTK (ioc
),
263 wbcg_toplevel (wbcg
));
264 if (immediate_exit_flag
)
265 wbcgs_to_kill
= g_slist_prepend (wbcgs_to_kill
,
268 /* cheesy attempt to keep the ui from freezing during
270 handle_paint_events ();
271 if (gnm_io_context_gtk_get_interrupted (GNM_IO_CONTEXT_GTK (ioc
)))
272 break; /* Don't load any more workbooks */
279 /* FIXME: Maybe we should quit here if we were asked to open
280 files and failed to do so. */
282 /* If we were intentionally short circuited exit now */
283 if (!initial_workbook_open_complete
) {
284 initial_workbook_open_complete
= TRUE
;
285 if (!opened_workbook
) {
286 gint n_of_sheets
= gnm_conf_get_core_workbook_n_sheet ();
288 workbook_new_with_sheets (n_of_sheets
),
292 if (immediate_exit_flag
) {
294 for (l
= wbcgs_to_kill
; l
; l
= l
->next
)
295 g_idle_add ((GSourceFunc
)cb_kill_wbcg
, l
->data
);
298 g_signal_connect (gnm_app_get_app (),
300 G_CALLBACK (cb_workbook_removed
),
303 gnm_io_context_gtk_discharge_splash (GNM_IO_CONTEXT_GTK (ioc
));
304 g_object_unref (ioc
);
308 g_object_unref (ioc
);
309 g_slist_foreach (wbcgs_to_kill
, (GFunc
)cb_kill_wbcg
, NULL
);
312 g_slist_free (wbcgs_to_kill
);
313 gnumeric_arg_shutdown ();
314 store_plugin_state ();
317 #if defined(G_OS_WIN32)
325 gnm_pre_parse_shutdown ();
326 go_component_set_default_command_context (NULL
);
329 * This helps finding leaks. We might want it in developent
332 if (gnm_debug_flag ("close-displays")) {
336 while (g_main_context_iteration (NULL
, FALSE
))
339 displays
= gdk_display_manager_list_displays
340 (gdk_display_manager_get ());
341 g_slist_foreach (displays
, (GFunc
)gdk_display_close
, NULL
);
342 g_slist_free (displays
);