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 ()
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 };
100 GtkWidget *wsplash = 0;
101 gint64 start_time = 0;
105 create_splash_window (void)
107 GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
109 const gchar *filename = PKGDATADIR "/splash.png";
110 const char *relocated_filename = relocate (filename);
111 GtkWidget *l = gtk_image_new_from_file (relocated_filename);
112 if (filename != relocated_filename)
113 free (CONST_CAST (char *, relocated_filename));
115 gtk_container_add (GTK_CONTAINER (sp), l);
116 gtk_window_set_type_hint (GTK_WINDOW (sp),
117 GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
118 gtk_window_set_position (GTK_WINDOW (sp), GTK_WIN_POS_CENTER);
119 gtk_window_set_skip_pager_hint (GTK_WINDOW (sp), TRUE);
120 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (sp), TRUE);
121 gtk_window_set_focus_on_map (GTK_WINDOW (sp), FALSE);
122 gtk_window_set_accept_focus (GTK_WINDOW (sp), FALSE);
125 hints.max_height = 100;
126 hints.max_width = 200;
127 gtk_window_set_geometry_hints (GTK_WINDOW (sp),
128 NULL, &hints, GDK_HINT_MAX_SIZE);
131 gtk_window_set_gravity (GTK_WINDOW (sp), GDK_GRAVITY_CENTER);
133 gtk_window_set_modal (GTK_WINDOW (sp), TRUE);
134 gtk_window_set_decorated (GTK_WINDOW (sp), FALSE);
135 gtk_window_set_keep_above (GTK_WINDOW (sp), TRUE);
136 gtk_widget_show_all (sp);
142 on_local_options (GApplication * application,
143 GVariantDict * options, gpointer user_data)
147 g_variant_dict_lookup_value (options, "no-unique",
148 G_VARIANT_TYPE_BOOLEAN);
151 GApplicationFlags flags = g_application_get_flags (application);
152 flags |= G_APPLICATION_NON_UNIQUE;
153 g_application_set_flags (application, flags);
159 g_variant_dict_lookup_value (options, "no-splash",
160 G_VARIANT_TYPE_BOOLEAN);
164 start_time = g_get_monotonic_time ();
173 on_startup (GApplication * app, gpointer ud)
175 GMainContext *context = g_main_context_new ();
179 wsplash = create_splash_window ();
180 gtk_application_add_window (GTK_APPLICATION (app),
181 GTK_WINDOW (wsplash));
184 GMainLoop *loop = g_main_loop_new (context, FALSE);
186 GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
188 ((struct init_source *) ss)->loop = loop;
189 ((struct init_source *) ss)->state = 0;
191 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
193 g_source_attach (ss, context);
194 g_main_loop_run (loop);
199 post_initialise (GApplication * app)
201 register_selection_functions ();
202 psppire_output_window_setup ();
204 GSimpleAction *quit = g_simple_action_new ("quit", NULL);
205 g_signal_connect_swapped (quit, "activate", G_CALLBACK (psppire_quit), app);
206 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit));
210 #define SPLASH_DURATION 1000
213 destroy_splash (gpointer ud)
215 GtkWidget *sp = GTK_WIDGET (ud);
216 gtk_widget_destroy (sp);
218 return G_SOURCE_REMOVE;
223 wait_for_splash (GApplication *app, GtkWindow *x)
227 gtk_window_set_transient_for (GTK_WINDOW (wsplash), x);
228 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (wsplash));
229 gtk_window_set_keep_above (GTK_WINDOW (wsplash), TRUE);
230 gtk_window_present (GTK_WINDOW (wsplash));
232 /* Remove the splash screen after SPLASH_DURATION milliseconds */
233 gint64 elapsed_time = (g_get_monotonic_time () - start_time) / 1000;
234 if (SPLASH_DURATION - elapsed_time <= 0)
235 destroy_splash (wsplash);
237 g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
243 on_activate (GApplication * app, gpointer ud)
245 post_initialise (app);
247 GtkWindow *x = create_data_window ();
248 gtk_application_add_window (GTK_APPLICATION (app), x);
250 wait_for_splash (app, x);
255 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
258 post_initialise (app);
260 gchar *file = g_file_get_parse_name (files[0]);
261 GtkWindow *x = psppire_preload_file (file);
264 wait_for_splash (app, x);
268 /* These are arguments which must be processed BEFORE the X server has been initialised */
270 process_pre_start_arguments (int *argc, char ***argv)
272 GOptionEntry oe[] = {
273 {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
274 show_version_and_exit, N_("Show version information and exit"), 0},
278 GOptionContext *oc = g_option_context_new ("");
279 g_option_context_set_help_enabled (oc, FALSE);
280 g_option_context_set_ignore_unknown_options (oc, FALSE);
281 g_option_context_add_main_entries (oc, oe, NULL);
282 g_option_context_parse (oc, argc, argv, NULL);
285 #if ENABLE_RELOCATABLE && defined(__APPLE__)
287 pspp_macos_setenv (const char * progname)
289 /* helper to set environment variables for pspp to be relocatable.
290 * Due to the latest changes it is not recommended to set it in the shell
293 gchar resolved_path[PATH_MAX];
294 /* on some OSX installations open file limit is 256 and GIMP needs more */
296 limit.rlim_cur = 10000;
297 limit.rlim_max = 10000;
298 setrlimit (RLIMIT_NOFILE, &limit);
299 if (realpath (progname, resolved_path))
303 gchar res_dir[PATH_MAX];
306 app_dir = g_path_get_dirname (resolved_path);
307 g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
308 if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
309 g_print ("pspp is started as MacOS application\n");
314 g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
315 g_setenv ("GTK_PATH", tmp, TRUE);
316 g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
317 g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
318 g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
319 g_setenv ("GEGL_PATH", tmp, TRUE);
320 g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
321 g_setenv ("BABL_PATH", tmp, TRUE);
322 g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
323 g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
324 g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
325 g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
326 g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
327 g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
328 g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
329 g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
330 g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
331 g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
333 if (g_getenv ("HOME")!=NULL)
335 g_snprintf (tmp, sizeof(tmp),
336 "%s/Library/Application Support/pspp/1.3/cache",
338 g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
345 main (int argc, char *argv[])
348 #if ENABLE_RELOCATABLE && defined(__APPLE__)
349 /* remove MacOS session identifier from the command line args */
351 for (gint i = 0; i < argc; i++)
353 if (!g_str_has_prefix (argv[i], "-psn_"))
355 argv[newargc] = argv[i];
361 argv[newargc] = NULL; /* glib expects NULL terminated array */
364 pspp_macos_setenv (argv[0]);
367 set_program_name (argv[0]);
369 GtkApplication *app =
370 gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
372 process_pre_start_arguments (&argc, &argv);
374 GOptionEntry oe[] = {
375 {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
376 N_("Do not display the splash screen"), 0},
377 {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
378 N_("Do not attempt single instance negotiation"), 0},
382 g_application_add_main_option_entries (G_APPLICATION (app), oe);
384 g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
385 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
386 g_signal_connect (app, "handle-local-options",
387 G_CALLBACK (on_local_options), NULL);
388 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
391 GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
392 g_signal_connect_swapped (act_new_syntax, "activate",
393 G_CALLBACK (create_syntax_window), NULL);
394 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
398 GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
399 g_signal_connect_swapped (act_new_data, "activate",
400 G_CALLBACK (create_data_window), NULL);
401 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
404 return g_application_run (G_APPLICATION (app), argc, argv);