Compilation: fix warning.
[gnumeric.git] / src / main-application.c
blob60eb357d32ba11779533aefa78ebda8c583d9bec
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * main-application.c: Main entry point for the Gnumeric application
5 * Author:
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>
15 #include "gnumeric.h"
16 #include "libgnumeric.h"
17 #ifdef G_OS_WIN32
18 #define _WIN32_WINNT 0x0501
19 #include <windows.h>
20 #include <io.h>
21 #endif
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"
30 #include "workbook.h"
31 #include "gui-file.h"
32 #include "gnumeric-conf.h"
33 #include "gnumeric-paths.h"
34 #include "session.h"
35 #include "sheet.h"
36 #include "gutils.h"
37 #include "gnm-plugin.h"
38 #include "application.h"
39 #include "func.h"
41 #include <gtk/gtk.h>
42 #include <glib/gstdio.h>
44 #include <sys/types.h>
45 #include <time.h>
46 #include <string.h>
47 #include <locale.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 /*********************************
57 * Public Variables */
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"),
66 NULL
69 /*********************************
70 * Hidden Actions */
72 "quit", 0,
73 G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &immediate_exit_flag,
74 N_("Exit immediately after loading the selected books"),
75 NULL
77 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &startup_files, NULL, NULL },
78 { NULL }
82 static void
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;
93 static void
94 gnumeric_arg_shutdown (void)
96 if (program) {
97 g_object_unref (program);
98 program = NULL;
102 static void
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);
116 #endif
118 g_option_context_add_group (ocontext, gtk_get_option_group (TRUE));
119 g_option_context_parse (ocontext, &argc, &argv, &error);
121 if (ocontext)
122 g_option_context_free (ocontext);
124 if (error) {
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);
128 exit (1);
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"
140 static void
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);
148 static gboolean
149 cb_kill_wbcg (WBCGtk *wbcg)
151 gboolean still_open = wbc_gtk_close (wbcg);
152 g_assert (!still_open);
153 return FALSE;
156 static void
157 cb_workbook_removed (void)
159 if (gnm_app_workbook_list () == NULL) {
160 gtk_main_quit ();
165 main (int argc, char const **argv)
167 gboolean opened_workbook = FALSE;
168 GOIOContext *ioc;
169 WorkbookView *wbv;
170 GSList *wbcgs_to_kill = NULL;
171 GOCmdContext *cc;
173 #ifdef G_OS_WIN32
174 gboolean has_console;
175 #endif
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);
188 #ifdef G_OS_WIN32
189 has_console = FALSE;
191 typedef BOOL (CALLBACK* LPFNATTACHCONSOLE)(DWORD);
192 LPFNATTACHCONSOLE MyAttachConsole;
193 HMODULE hmod;
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);
202 has_console = TRUE;
206 #endif
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]);
214 gnm_init ();
216 cc = gnm_cmd_context_stderr_new ();
217 go_component_set_default_command_context (cc);
218 g_object_unref (cc);
220 cc = g_object_new (GNM_TYPE_IO_CONTEXT_GTK,
221 "show-splash", !gnumeric_no_splash,
222 "show-warnings", !gnumeric_no_warnings,
223 NULL);
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));
231 if (startup_files) {
232 int i, N;
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]);
239 if (uri == NULL) {
240 g_warning ("Ignoring invalid URI.");
241 continue;
244 go_io_context_processing_file (ioc, uri);
245 wbv = workbook_view_new_from_uri (uri, NULL, ioc, NULL);
246 g_free (uri);
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);
253 if (wbv != NULL) {
254 WBCGtk *wbcg;
256 workbook_update_history (wb_view_get_workbook (wbv), GNM_FILE_SAVE_AS_STYLE_SAVE);
258 wbcg = wbc_gtk_new (wbv, NULL, NULL, geometry);
259 geometry = NULL;
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,
266 wbcg);
268 /* cheesy attempt to keep the ui from freezing during
269 load */
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 */
276 g_object_unref (cc);
277 cc = NULL;
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 ();
287 wbc_gtk_new (NULL,
288 workbook_new_with_sheets (n_of_sheets),
289 NULL, geometry);
292 if (immediate_exit_flag) {
293 GSList *l;
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 (),
299 "workbook_removed",
300 G_CALLBACK (cb_workbook_removed),
301 NULL);
303 gnm_io_context_gtk_discharge_splash (GNM_IO_CONTEXT_GTK (ioc));
304 g_object_unref (ioc);
306 gtk_main ();
307 } else {
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 ();
315 gnm_shutdown ();
317 #if defined(G_OS_WIN32)
318 if (has_console) {
319 close(1);
320 close(2);
321 FreeConsole();
323 #endif
325 gnm_pre_parse_shutdown ();
326 go_component_set_default_command_context (NULL);
329 * This helps finding leaks. We might want it in developent
330 * only.
332 if (gnm_debug_flag ("close-displays")) {
333 GSList *displays;
335 gdk_flush();
336 while (g_main_context_iteration (NULL, FALSE))
337 ;/* nothing */
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);
345 return 0;