1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "pre-initialisation.h"
21 #include "ui/gui/psppire.h"
26 #include "language/lexer/include-path.h"
27 #include "libpspp/argv-parser.h"
28 #include "libpspp/array.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/cast.h"
31 #include "libpspp/copyleft.h"
32 #include "libpspp/str.h"
33 #include "libpspp/string-array.h"
34 #include "libpspp/version.h"
35 #include "ui/source-init-opts.h"
36 #include "ui/gui/psppire-syntax-window.h"
37 #include "ui/gui/psppire-data-window.h"
38 #include "ui/gui/psppire-output-window.h"
40 #include "gl/configmake.h"
41 #include "gl/progname.h"
42 #include "gl/relocatable.h"
43 #include "gl/version-etc.h"
44 #include "gl/xalloc.h"
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) msgid
53 show_version_and_exit (void)
55 version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
56 "Ben Pfaff", "John Darrington", "Jason Stover", NULL_SENTINEL);
66 init_prepare (GSource * source, gint * timeout_)
72 init_check (GSource * source)
78 init_dispatch (GSource * ss, GSourceFunc callback, gpointer user_data)
80 struct init_source *is = (struct init_source *) ss;
82 bool finished = initialize (is);
87 g_main_loop_quit (is->loop);
94 static GSourceFuncs init_funcs =
95 { init_prepare, init_check, init_dispatch, NULL, NULL, NULL };
97 static GtkWidget *wsplash = 0;
98 static gint64 start_time = 0;
102 create_splash_window (void)
104 GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
106 const gchar *filename = PKGDATADIR "/splash.png";
107 const char *relocated_filename = relocate (filename);
108 GtkWidget *l = gtk_image_new_from_file (relocated_filename);
109 if (filename != relocated_filename)
110 free (CONST_CAST (char *, relocated_filename));
112 gtk_container_add (GTK_CONTAINER (sp), l);
113 gtk_window_set_type_hint (GTK_WINDOW (sp),
114 GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
115 gtk_window_set_position (GTK_WINDOW (sp), GTK_WIN_POS_CENTER);
116 gtk_window_set_skip_pager_hint (GTK_WINDOW (sp), TRUE);
117 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (sp), TRUE);
118 gtk_window_set_focus_on_map (GTK_WINDOW (sp), FALSE);
119 gtk_window_set_accept_focus (GTK_WINDOW (sp), FALSE);
122 hints.max_height = 100;
123 hints.max_width = 200;
124 gtk_window_set_geometry_hints (GTK_WINDOW (sp),
125 NULL, &hints, GDK_HINT_MAX_SIZE);
128 gtk_window_set_gravity (GTK_WINDOW (sp), GDK_GRAVITY_CENTER);
130 gtk_window_set_modal (GTK_WINDOW (sp), TRUE);
131 gtk_window_set_decorated (GTK_WINDOW (sp), FALSE);
132 gtk_window_set_keep_above (GTK_WINDOW (sp), TRUE);
133 gtk_widget_show_all (sp);
139 on_local_options (GApplication * application,
140 GVariantDict * options, gpointer user_data)
144 g_variant_dict_lookup_value (options, "no-unique",
145 G_VARIANT_TYPE_BOOLEAN);
148 GApplicationFlags flags = g_application_get_flags (application);
149 flags |= G_APPLICATION_NON_UNIQUE;
150 g_application_set_flags (application, flags);
156 g_variant_dict_lookup_value (options, "no-splash",
157 G_VARIANT_TYPE_BOOLEAN);
161 start_time = g_get_monotonic_time ();
170 on_startup (GApplication * app, gpointer ud)
172 GMainContext *context = g_main_context_new ();
176 wsplash = create_splash_window ();
177 gtk_application_add_window (GTK_APPLICATION (app),
178 GTK_WINDOW (wsplash));
181 GMainLoop *loop = g_main_loop_new (context, FALSE);
183 GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
185 ((struct init_source *) ss)->loop = loop;
186 ((struct init_source *) ss)->state = 0;
188 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
190 g_source_attach (ss, context);
191 g_main_loop_run (loop);
196 post_initialise (GApplication * app)
198 register_selection_functions ();
199 psppire_output_window_setup ();
201 GSimpleAction *quit = g_simple_action_new ("quit", NULL);
202 g_signal_connect_swapped (quit, "activate", G_CALLBACK (psppire_quit), app);
203 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit));
207 #define SPLASH_DURATION 1000
210 destroy_splash (gpointer ud)
212 GtkWidget *sp = GTK_WIDGET (ud);
213 gtk_widget_destroy (sp);
215 return G_SOURCE_REMOVE;
220 wait_for_splash (GApplication *app, GtkWindow *x)
224 gtk_window_set_transient_for (GTK_WINDOW (wsplash), x);
225 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (wsplash));
226 gtk_window_set_keep_above (GTK_WINDOW (wsplash), TRUE);
227 gtk_window_present (GTK_WINDOW (wsplash));
229 /* Remove the splash screen after SPLASH_DURATION milliseconds */
230 gint64 elapsed_time = (g_get_monotonic_time () - start_time) / 1000;
231 if (SPLASH_DURATION - elapsed_time <= 0)
232 destroy_splash (wsplash);
234 g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
239 on_activate (GApplication * app, gpointer ud)
241 post_initialise (app);
243 GtkWindow *x = create_data_window ();
244 gtk_application_add_window (GTK_APPLICATION (app), x);
246 wait_for_splash (app, x);
250 find_empty_data_window (GApplication *app)
252 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
255 if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
256 psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
257 return GTK_WINDOW (wl->data);
264 find_psppire_window (GApplication *app)
266 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
269 if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
270 return GTK_WINDOW (wl->data);
277 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
280 /* If the application is already open and we open another file
281 via xdg-open on GNU/Linux or via the file manager, then open is
282 called. Check if we already have a psppire window. */
283 if (find_psppire_window (app) == NULL)
284 post_initialise (app);
286 /* When a new data file is opened, then try to find an empty
287 data window which will then be replaced as in the open file
289 GtkWindow *victim = find_empty_data_window (app);
291 gchar *file = g_file_get_parse_name (files[0]);
292 GtkWindow *x = psppire_preload_file (file, victim);
295 wait_for_splash (app, x);
299 /* These are arguments which must be processed BEFORE the X server has been initialised */
301 process_pre_start_arguments (int *argc, char ***argv)
303 GOptionEntry oe[] = {
304 {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
305 show_version_and_exit, N_("Show version information and exit"), 0},
306 {NULL, 0, 0, 0, NULL, "", 0}
309 GOptionContext *oc = g_option_context_new ("");
310 g_option_context_set_help_enabled (oc, FALSE);
311 g_option_context_set_ignore_unknown_options (oc, FALSE);
312 g_option_context_add_main_entries (oc, oe, NULL);
313 g_option_context_parse (oc, argc, argv, NULL);
314 g_option_context_free (oc);
318 main (int argc, char *argv[])
320 /* Some operating systems need to munge the arguments. */
321 pre_initialisation (&argc, argv);
323 set_program_name (argv[0]);
325 GtkApplication *app =
326 gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
328 process_pre_start_arguments (&argc, &argv);
330 GOptionEntry oe[] = {
331 {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
332 N_("Do not display the splash screen"), 0},
333 {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
334 N_("Do not attempt single instance negotiation"), 0},
338 g_application_add_main_option_entries (G_APPLICATION (app), oe);
340 g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
341 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
342 g_signal_connect (app, "handle-local-options",
343 G_CALLBACK (on_local_options), NULL);
344 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
347 GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
348 g_signal_connect_swapped (act_new_syntax, "activate",
349 G_CALLBACK (create_syntax_window), NULL);
350 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
354 GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
355 g_signal_connect_swapped (act_new_data, "activate",
356 G_CALLBACK (create_data_window), NULL);
357 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
360 g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
361 return g_application_run (G_APPLICATION (app), argc, argv);