1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
3 2020 Free Software Foundation
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "ui/gui/psppire.h"
25 #include <sys/resource.h>
26 #if ENABLE_RELOCATABLE && defined(__APPLE__)
27 static const bool apple_relocatable = true;
29 static const bool apple_relocatable = false;
32 #include "language/lexer/include-path.h"
33 #include "libpspp/argv-parser.h"
34 #include "libpspp/array.h"
35 #include "libpspp/assertion.h"
36 #include "libpspp/cast.h"
37 #include "libpspp/copyleft.h"
38 #include "libpspp/str.h"
39 #include "libpspp/string-array.h"
40 #include "libpspp/version.h"
41 #include "ui/source-init-opts.h"
42 #include "ui/gui/psppire-syntax-window.h"
43 #include "ui/gui/psppire-data-window.h"
44 #include "ui/gui/psppire-output-window.h"
46 #include "gl/configmake.h"
47 #include "gl/progname.h"
48 #include "gl/relocatable.h"
49 #include "gl/version-etc.h"
50 #include "gl/xalloc.h"
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) msgid
59 show_version_and_exit ()
61 version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
62 "Ben Pfaff", "John Darrington", "Jason Stover", NULL_SENTINEL);
72 init_prepare (GSource * source, gint * timeout_)
78 init_check (GSource * source)
84 init_dispatch (GSource * ss, GSourceFunc callback, gpointer user_data)
86 struct init_source *is = (struct init_source *) ss;
88 bool finished = initialize (is);
93 g_main_loop_quit (is->loop);
100 static GSourceFuncs init_funcs =
101 { init_prepare, init_check, init_dispatch, NULL };
105 GtkWidget *wsplash = 0;
106 gint64 start_time = 0;
110 create_splash_window (void)
112 GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
114 const gchar *filename = PKGDATADIR "/splash.png";
115 const char *relocated_filename = relocate (filename);
116 GtkWidget *l = gtk_image_new_from_file (relocated_filename);
117 if (filename != relocated_filename)
118 free (CONST_CAST (char *, relocated_filename));
120 gtk_container_add (GTK_CONTAINER (sp), l);
121 gtk_window_set_type_hint (GTK_WINDOW (sp),
122 GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
123 gtk_window_set_position (GTK_WINDOW (sp), GTK_WIN_POS_CENTER);
124 gtk_window_set_skip_pager_hint (GTK_WINDOW (sp), TRUE);
125 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (sp), TRUE);
126 gtk_window_set_focus_on_map (GTK_WINDOW (sp), FALSE);
127 gtk_window_set_accept_focus (GTK_WINDOW (sp), FALSE);
130 hints.max_height = 100;
131 hints.max_width = 200;
132 gtk_window_set_geometry_hints (GTK_WINDOW (sp),
133 NULL, &hints, GDK_HINT_MAX_SIZE);
136 gtk_window_set_gravity (GTK_WINDOW (sp), GDK_GRAVITY_CENTER);
138 gtk_window_set_modal (GTK_WINDOW (sp), TRUE);
139 gtk_window_set_decorated (GTK_WINDOW (sp), FALSE);
140 gtk_window_set_keep_above (GTK_WINDOW (sp), TRUE);
141 gtk_widget_show_all (sp);
147 on_local_options (GApplication * application,
148 GVariantDict * options, gpointer user_data)
152 g_variant_dict_lookup_value (options, "no-unique",
153 G_VARIANT_TYPE_BOOLEAN);
156 GApplicationFlags flags = g_application_get_flags (application);
157 flags |= G_APPLICATION_NON_UNIQUE;
158 g_application_set_flags (application, flags);
164 g_variant_dict_lookup_value (options, "no-splash",
165 G_VARIANT_TYPE_BOOLEAN);
169 start_time = g_get_monotonic_time ();
178 on_startup (GApplication * app, gpointer ud)
180 GMainContext *context = g_main_context_new ();
184 wsplash = create_splash_window ();
185 gtk_application_add_window (GTK_APPLICATION (app),
186 GTK_WINDOW (wsplash));
189 GMainLoop *loop = g_main_loop_new (context, FALSE);
191 GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
193 ((struct init_source *) ss)->loop = loop;
194 ((struct init_source *) ss)->state = 0;
196 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
198 g_source_attach (ss, context);
199 g_main_loop_run (loop);
204 post_initialise (GApplication * app)
206 register_selection_functions ();
207 psppire_output_window_setup ();
209 GSimpleAction *quit = g_simple_action_new ("quit", NULL);
210 g_signal_connect_swapped (quit, "activate", G_CALLBACK (psppire_quit), app);
211 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit));
215 #define SPLASH_DURATION 1000
218 destroy_splash (gpointer ud)
220 GtkWidget *sp = GTK_WIDGET (ud);
221 gtk_widget_destroy (sp);
223 return G_SOURCE_REMOVE;
228 wait_for_splash (GApplication *app, GtkWindow *x)
232 gtk_window_set_transient_for (GTK_WINDOW (wsplash), x);
233 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (wsplash));
234 gtk_window_set_keep_above (GTK_WINDOW (wsplash), TRUE);
235 gtk_window_present (GTK_WINDOW (wsplash));
237 /* Remove the splash screen after SPLASH_DURATION milliseconds */
238 gint64 elapsed_time = (g_get_monotonic_time () - start_time) / 1000;
239 if (SPLASH_DURATION - elapsed_time <= 0)
240 destroy_splash (wsplash);
242 g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
247 on_activate (GApplication * app, gpointer ud)
249 post_initialise (app);
251 GtkWindow *x = create_data_window ();
252 gtk_application_add_window (GTK_APPLICATION (app), x);
254 wait_for_splash (app, x);
258 find_empty_data_window (GApplication *app)
260 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
263 if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
264 psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
265 return GTK_WINDOW (wl->data);
272 find_psppire_window (GApplication *app)
274 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
277 if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
278 return GTK_WINDOW (wl->data);
285 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
288 /* If the application is already open and we open another file
289 via xdg-open on GNU/Linux or via the file manager, then open is
290 called. Check if we already have a psppire window. */
291 if (find_psppire_window (app) == NULL)
292 post_initialise (app);
294 /* When a new data file is opened, then try to find an empty
295 data window which will then be replaced as in the open file
297 GtkWindow *victim = find_empty_data_window (app);
299 gchar *file = g_file_get_parse_name (files[0]);
300 GtkWindow *x = psppire_preload_file (file, victim);
303 wait_for_splash (app, x);
307 /* These are arguments which must be processed BEFORE the X server has been initialised */
309 process_pre_start_arguments (int *argc, char ***argv)
311 GOptionEntry oe[] = {
312 {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
313 show_version_and_exit, N_("Show version information and exit"), 0},
317 GOptionContext *oc = g_option_context_new ("");
318 g_option_context_set_help_enabled (oc, FALSE);
319 g_option_context_set_ignore_unknown_options (oc, FALSE);
320 g_option_context_add_main_entries (oc, oe, NULL);
321 g_option_context_parse (oc, argc, argv, NULL);
322 g_option_context_free (oc);
326 pspp_macos_setenv (const char * progname)
328 /* helper to set environment variables for pspp to be relocatable.
329 * Due to the latest changes it is not recommended to set it in the shell
332 gchar resolved_path[PATH_MAX];
333 /* on some OSX installations open file limit is 256 and GIMP needs more */
335 limit.rlim_cur = 10000;
336 limit.rlim_max = 10000;
337 setrlimit (RLIMIT_NOFILE, &limit);
338 if (realpath (progname, resolved_path))
342 gchar res_dir[PATH_MAX];
345 app_dir = g_path_get_dirname (resolved_path);
346 g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
347 if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
348 g_print ("pspp is started as MacOS application\n");
353 g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
354 g_setenv ("GTK_PATH", tmp, TRUE);
355 g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
356 g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
357 g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
358 g_setenv ("GEGL_PATH", tmp, TRUE);
359 g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
360 g_setenv ("BABL_PATH", tmp, TRUE);
361 g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
362 g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
363 g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
364 g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
365 g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
366 g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
367 g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
368 g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
369 g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
370 g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
372 if (g_getenv ("HOME")!=NULL)
374 g_snprintf (tmp, sizeof(tmp),
375 "%s/Library/Application Support/pspp/1.3/cache",
377 g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
383 main (int argc, char *argv[])
385 if (apple_relocatable)
387 /* remove MacOS session identifier from the command line args */
389 for (gint i = 0; i < argc; i++)
391 if (!g_str_has_prefix (argv[i], "-psn_"))
393 argv[newargc] = argv[i];
399 argv[newargc] = NULL; /* glib expects NULL terminated array */
402 pspp_macos_setenv (argv[0]);
405 set_program_name (argv[0]);
407 GtkApplication *app =
408 gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
410 process_pre_start_arguments (&argc, &argv);
412 GOptionEntry oe[] = {
413 {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
414 N_("Do not display the splash screen"), 0},
415 {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
416 N_("Do not attempt single instance negotiation"), 0},
420 g_application_add_main_option_entries (G_APPLICATION (app), oe);
422 g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
423 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
424 g_signal_connect (app, "handle-local-options",
425 G_CALLBACK (on_local_options), NULL);
426 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
429 GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
430 g_signal_connect_swapped (act_new_syntax, "activate",
431 G_CALLBACK (create_syntax_window), NULL);
432 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
436 GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
437 g_signal_connect_swapped (act_new_data, "activate",
438 G_CALLBACK (create_data_window), NULL);
439 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
442 g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
443 return g_application_run (G_APPLICATION (app), argc, argv);