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 "ui/gui/psppire.h"
23 #if ENABLE_RELOCATABLE && defined(__APPLE__)
27 #include "language/lexer/include-path.h"
28 #include "libpspp/argv-parser.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/cast.h"
32 #include "libpspp/copyleft.h"
33 #include "libpspp/str.h"
34 #include "libpspp/string-array.h"
35 #include "libpspp/version.h"
36 #include "ui/source-init-opts.h"
37 #include "ui/gui/psppire-syntax-window.h"
38 #include "ui/gui/psppire-data-window.h"
39 #include "ui/gui/psppire-output-window.h"
41 #include "gl/configmake.h"
42 #include "gl/progname.h"
43 #include "gl/relocatable.h"
44 #include "gl/version-etc.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
54 show_version_and_exit (void)
56 version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
57 "Ben Pfaff", "John Darrington", "Jason Stover", NULL_SENTINEL);
67 init_prepare (GSource * source, gint * timeout_)
73 init_check (GSource * source)
79 init_dispatch (GSource * ss, GSourceFunc callback, gpointer user_data)
81 struct init_source *is = (struct init_source *) ss;
83 bool finished = initialize (is);
88 g_main_loop_quit (is->loop);
95 static GSourceFuncs init_funcs =
96 { init_prepare, init_check, init_dispatch, NULL, NULL, NULL };
98 static GtkWidget *wsplash = 0;
99 static gint64 start_time = 0;
103 create_splash_window (void)
105 GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
107 const gchar *filename = PKGDATADIR "/splash.png";
108 const char *relocated_filename = relocate (filename);
109 GtkWidget *l = gtk_image_new_from_file (relocated_filename);
110 if (filename != relocated_filename)
111 free (CONST_CAST (char *, relocated_filename));
113 gtk_container_add (GTK_CONTAINER (sp), l);
114 gtk_window_set_type_hint (GTK_WINDOW (sp),
115 GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
116 gtk_window_set_position (GTK_WINDOW (sp), GTK_WIN_POS_CENTER);
117 gtk_window_set_skip_pager_hint (GTK_WINDOW (sp), TRUE);
118 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (sp), TRUE);
119 gtk_window_set_focus_on_map (GTK_WINDOW (sp), FALSE);
120 gtk_window_set_accept_focus (GTK_WINDOW (sp), FALSE);
123 hints.max_height = 100;
124 hints.max_width = 200;
125 gtk_window_set_geometry_hints (GTK_WINDOW (sp),
126 NULL, &hints, GDK_HINT_MAX_SIZE);
129 gtk_window_set_gravity (GTK_WINDOW (sp), GDK_GRAVITY_CENTER);
131 gtk_window_set_modal (GTK_WINDOW (sp), TRUE);
132 gtk_window_set_decorated (GTK_WINDOW (sp), FALSE);
133 gtk_window_set_keep_above (GTK_WINDOW (sp), TRUE);
134 gtk_widget_show_all (sp);
140 on_local_options (GApplication * application,
141 GVariantDict * options, gpointer user_data)
145 g_variant_dict_lookup_value (options, "no-unique",
146 G_VARIANT_TYPE_BOOLEAN);
149 GApplicationFlags flags = g_application_get_flags (application);
150 flags |= G_APPLICATION_NON_UNIQUE;
151 g_application_set_flags (application, flags);
157 g_variant_dict_lookup_value (options, "no-splash",
158 G_VARIANT_TYPE_BOOLEAN);
162 start_time = g_get_monotonic_time ();
171 on_startup (GApplication * app, gpointer ud)
173 GMainContext *context = g_main_context_new ();
177 wsplash = create_splash_window ();
178 gtk_application_add_window (GTK_APPLICATION (app),
179 GTK_WINDOW (wsplash));
182 GMainLoop *loop = g_main_loop_new (context, FALSE);
184 GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
186 ((struct init_source *) ss)->loop = loop;
187 ((struct init_source *) ss)->state = 0;
189 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
191 g_source_attach (ss, context);
192 g_main_loop_run (loop);
197 post_initialise (GApplication * app)
199 register_selection_functions ();
200 psppire_output_window_setup ();
202 GSimpleAction *quit = g_simple_action_new ("quit", NULL);
203 g_signal_connect_swapped (quit, "activate", G_CALLBACK (psppire_quit), app);
204 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit));
208 #define SPLASH_DURATION 1000
211 destroy_splash (gpointer ud)
213 GtkWidget *sp = GTK_WIDGET (ud);
214 gtk_widget_destroy (sp);
216 return G_SOURCE_REMOVE;
221 wait_for_splash (GApplication *app, GtkWindow *x)
225 gtk_window_set_transient_for (GTK_WINDOW (wsplash), x);
226 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (wsplash));
227 gtk_window_set_keep_above (GTK_WINDOW (wsplash), TRUE);
228 gtk_window_present (GTK_WINDOW (wsplash));
230 /* Remove the splash screen after SPLASH_DURATION milliseconds */
231 gint64 elapsed_time = (g_get_monotonic_time () - start_time) / 1000;
232 if (SPLASH_DURATION - elapsed_time <= 0)
233 destroy_splash (wsplash);
235 g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
240 on_activate (GApplication * app, gpointer ud)
242 post_initialise (app);
244 GtkWindow *x = create_data_window ();
245 gtk_application_add_window (GTK_APPLICATION (app), x);
247 wait_for_splash (app, x);
251 find_empty_data_window (GApplication *app)
253 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
256 if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
257 psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
258 return GTK_WINDOW (wl->data);
265 find_psppire_window (GApplication *app)
267 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
270 if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
271 return GTK_WINDOW (wl->data);
278 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
281 /* If the application is already open and we open another file
282 via xdg-open on GNU/Linux or via the file manager, then open is
283 called. Check if we already have a psppire window. */
284 if (find_psppire_window (app) == NULL)
285 post_initialise (app);
287 /* When a new data file is opened, then try to find an empty
288 data window which will then be replaced as in the open file
290 GtkWindow *victim = find_empty_data_window (app);
292 gchar *file = g_file_get_parse_name (files[0]);
293 GtkWindow *x = psppire_preload_file (file, victim);
296 wait_for_splash (app, x);
300 /* These are arguments which must be processed BEFORE the X server has been initialised */
302 process_pre_start_arguments (int *argc, char ***argv)
304 GOptionEntry oe[] = {
305 {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
306 show_version_and_exit, N_("Show version information and exit"), 0},
310 GOptionContext *oc = g_option_context_new ("");
311 g_option_context_set_help_enabled (oc, FALSE);
312 g_option_context_set_ignore_unknown_options (oc, FALSE);
313 g_option_context_add_main_entries (oc, oe, NULL);
314 g_option_context_parse (oc, argc, argv, NULL);
315 g_option_context_free (oc);
318 #if ENABLE_RELOCATABLE && defined(__APPLE__)
320 pspp_macos_setenv (const char * progname)
322 /* helper to set environment variables for pspp to be relocatable.
323 * Due to the latest changes it is not recommended to set it in the shell
326 gchar resolved_path[PATH_MAX];
327 /* on some OSX installations open file limit is 256 and GIMP needs more */
329 limit.rlim_cur = 10000;
330 limit.rlim_max = 10000;
331 setrlimit (RLIMIT_NOFILE, &limit);
332 if (realpath (progname, resolved_path))
336 gchar res_dir[PATH_MAX];
339 app_dir = g_path_get_dirname (resolved_path);
340 g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
341 if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
342 g_print ("pspp is started as MacOS application\n");
347 g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
348 g_setenv ("GTK_PATH", tmp, TRUE);
349 g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
350 g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
351 g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
352 g_setenv ("GEGL_PATH", tmp, TRUE);
353 g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
354 g_setenv ("BABL_PATH", tmp, TRUE);
355 g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
356 g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
357 g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
358 g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
359 g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
360 g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
361 g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
362 g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
363 g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
364 g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
366 if (g_getenv ("HOME")!=NULL)
368 g_snprintf (tmp, sizeof(tmp),
369 "%s/Library/Application Support/pspp/1.3/cache",
371 g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
378 main (int argc, char *argv[])
381 #if ENABLE_RELOCATABLE && defined(__APPLE__)
382 /* remove MacOS session identifier from the command line args */
384 for (gint i = 0; i < argc; i++)
386 if (!g_str_has_prefix (argv[i], "-psn_"))
388 argv[newargc] = argv[i];
394 argv[newargc] = NULL; /* glib expects NULL terminated array */
397 pspp_macos_setenv (argv[0]);
400 set_program_name (argv[0]);
402 GtkApplication *app =
403 gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
405 process_pre_start_arguments (&argc, &argv);
407 GOptionEntry oe[] = {
408 {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
409 N_("Do not display the splash screen"), 0},
410 {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
411 N_("Do not attempt single instance negotiation"), 0},
415 g_application_add_main_option_entries (G_APPLICATION (app), oe);
417 g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
418 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
419 g_signal_connect (app, "handle-local-options",
420 G_CALLBACK (on_local_options), NULL);
421 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
424 GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
425 g_signal_connect_swapped (act_new_syntax, "activate",
426 G_CALLBACK (create_syntax_window), NULL);
427 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
431 GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
432 g_signal_connect_swapped (act_new_data, "activate",
433 G_CALLBACK (create_data_window), NULL);
434 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
437 g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
438 return g_application_run (G_APPLICATION (app), argc, argv);