fd64f4fd68597f0efbebd8e8f3d9260fde6c7aad
[pspp] / src / ui / gui / main.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015,
3    2016, 2020, 2021  Free Software Foundation
4
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.
9
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.
14
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/>. */
17
18 #include <config.h>
19
20 #include "pre-initialisation.h"
21
22 #include "ui/gui/psppire.h"
23
24 #include <gtk/gtk.h>
25 #include <stdlib.h>
26
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/message.h"
34 #include "libpspp/str.h"
35 #include "libpspp/string-array.h"
36 #include "libpspp/version.h"
37 #include "ui/source-init-opts.h"
38 #include "ui/gui/psppire-syntax-window.h"
39 #include "ui/gui/psppire-data-window.h"
40 #include "ui/gui/psppire-output-window.h"
41 #include "ui/gui/psppire-conf.h"
42 #include "ui/gui/helper.h"
43
44 #include "gl/configmake.h"
45 #include "gl/progname.h"
46 #include "gl/relocatable.h"
47 #include "gl/version-etc.h"
48 #include "gl/xalloc.h"
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
53
54
55
56 static gboolean
57 show_version_and_exit (void)
58 {
59   version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
60                "Ben Pfaff", "John Darrington", "Jason Stover", NULL_SENTINEL);
61
62   exit (0);
63
64   return TRUE;
65 }
66
67 \f
68
69 static gboolean
70 init_prepare (GSource * source, gint * timeout_)
71 {
72   return TRUE;
73 }
74
75 static gboolean
76 init_check (GSource * source)
77 {
78   return TRUE;
79 }
80
81 static gboolean
82 init_dispatch (GSource * ss, GSourceFunc callback, gpointer user_data)
83 {
84   struct init_source *is = (struct init_source *) ss;
85
86   bool finished = initialize (is);
87   is->state++;
88
89   if (finished)
90     {
91       g_main_loop_quit (is->loop);
92       return FALSE;
93     }
94
95   return TRUE;
96 }
97
98 static GSourceFuncs init_funcs =
99   {init_prepare, init_check, init_dispatch, NULL, NULL, NULL };
100
101 static GtkWidget *wsplash = 0;
102 static gint64 start_time = 0;
103
104
105 static GtkWidget *
106 create_splash_window (void)
107 {
108   GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
109
110   const gchar *filename = PKGDATADIR "/splash.png";
111   const char *relocated_filename = relocate (filename);
112   GtkWidget *l = gtk_image_new_from_file (relocated_filename);
113   if (filename != relocated_filename)
114     free (CONST_CAST (char *, relocated_filename));
115
116   gtk_container_add (GTK_CONTAINER (sp), l);
117   gtk_window_set_type_hint (GTK_WINDOW (sp),
118                             GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
119   gtk_window_set_position (GTK_WINDOW (sp), GTK_WIN_POS_CENTER);
120   gtk_window_set_skip_pager_hint (GTK_WINDOW (sp), TRUE);
121   gtk_window_set_skip_taskbar_hint (GTK_WINDOW (sp), TRUE);
122   gtk_window_set_focus_on_map (GTK_WINDOW (sp), FALSE);
123   gtk_window_set_accept_focus (GTK_WINDOW (sp), FALSE);
124
125   GdkGeometry hints;
126   hints.max_height = 100;
127   hints.max_width = 200;
128   gtk_window_set_geometry_hints (GTK_WINDOW (sp),
129                                  NULL, &hints, GDK_HINT_MAX_SIZE);
130
131
132   gtk_window_set_gravity (GTK_WINDOW (sp), GDK_GRAVITY_CENTER);
133
134   gtk_window_set_modal (GTK_WINDOW (sp), TRUE);
135   gtk_window_set_decorated (GTK_WINDOW (sp), FALSE);
136   gtk_window_set_keep_above (GTK_WINDOW (sp), TRUE);
137   gtk_widget_show_all (sp);
138   return sp;
139 }
140
141
142 static gint
143 on_local_options (GApplication * application,
144                   GVariantDict * options, gpointer user_data)
145 {
146   {
147     GVariant *b =
148       g_variant_dict_lookup_value (options, "no-unique",
149                                    G_VARIANT_TYPE_BOOLEAN);
150     if (b)
151       {
152         GApplicationFlags flags =  g_application_get_flags (application);
153         flags |= G_APPLICATION_NON_UNIQUE;
154         g_application_set_flags (application, flags);
155         g_variant_unref (b);
156       }
157   }
158   {
159     GVariant *b =
160       g_variant_dict_lookup_value (options, "no-splash",
161                                    G_VARIANT_TYPE_BOOLEAN);
162     if (b)
163       g_variant_unref (b);
164     else
165       start_time = g_get_monotonic_time ();
166   }
167
168
169   return -1;
170 }
171
172 /* Use the imperitive mood for all entries in this table.
173    Each entry should end with a period.   */
174 static const char *tips[] =
175   {
176    N_("Right click on variable lists to change between viewing the variables' names and their labels."),
177    N_("Click \"Paste\" instead of \"OK\" when running procedures.  This allows you to edit your commands before running them and you have better control over your work."),
178    N_("Directly import your spreadsheets using the \"File | Import Data\" menu."),
179    N_("For an easy way to convert string variables into numerically encoded variables, use \"Automatic Recode\"  which preserves the variable names as labels."),
180    N_("When browsing large data sets, use \"Windows | Split\" to see both ends of the data in the same view."),
181    N_("Export your reports to ODT format for easy editing with the Libreoffice.org suite."),
182    N_("Use \"Edit | Options\" to have your Output window automatically appear when statistics are generated."),
183    N_("To easily reorder your variables, drag and drop them in the Variable View or the Data View.")
184   };
185
186 #define N_TIPS  (sizeof tips / sizeof tips[0])
187
188 static void
189 user_tip (GApplication *app)
190 {
191   PsppireConf *conf = psppire_conf_new ();
192
193   gboolean show_tip = TRUE;
194   psppire_conf_get_boolean (conf, "startup", "show-user-tips", &show_tip);
195
196   if (!show_tip)
197     return;
198
199   GtkWindow *parent = gtk_application_get_active_window (GTK_APPLICATION (app));
200
201   GtkWidget *d =
202     gtk_dialog_new_with_buttons (_("Psppire User Hint"), parent,
203                                  GTK_DIALOG_MODAL,
204                                  GTK_MESSAGE_INFO,
205                                  0, 0,
206                                  NULL);
207
208   GtkWidget *pictogram = gtk_image_new_from_icon_name ("user-info", GTK_ICON_SIZE_DIALOG);
209
210   GtkWidget *next = gtk_button_new_with_mnemonic (_("_Next Tip"));
211   gtk_dialog_add_action_widget (GTK_DIALOG (d), next, 1);
212
213   GtkWidget *close = gtk_button_new_with_mnemonic (_("_Close"));
214   gtk_dialog_add_action_widget (GTK_DIALOG (d), close, GTK_RESPONSE_CLOSE);
215
216   gtk_window_set_transient_for (GTK_WINDOW (d), parent);
217
218   g_object_set (d,
219                 "decorated", FALSE,
220                 "skip-taskbar-hint", TRUE,
221                 "skip-pager-hint", TRUE,
222                 "application", app,
223                 NULL);
224
225   GtkWidget *ca = gtk_dialog_get_content_area (GTK_DIALOG (d));
226
227   g_object_set (ca, "margin", 5, NULL);
228
229   GtkWidget *check = gtk_check_button_new_with_mnemonic ("_Show tips at startup");
230   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), show_tip);
231
232   srand (time(0));
233   gint x = rand () % N_TIPS;
234   GtkWidget *label = gtk_label_new (gettext (tips[x]));
235
236   /* Make the font of the label a little larger than the other widgets.  */
237   {
238     GtkStyleContext *sc = gtk_widget_get_style_context (label);
239     GtkCssProvider *p = gtk_css_provider_new ();
240     const gchar *css = "* {font-size: 130%;}";
241     if (gtk_css_provider_load_from_data (p, css, strlen (css), NULL))
242       {
243         gtk_style_context_add_provider (sc, GTK_STYLE_PROVIDER (p),
244                                         GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
245       }
246     g_object_unref (p);
247   }
248
249   /* It's more readable if the text is not all in one long line.  */
250   g_object_set (label, "wrap", TRUE, NULL);
251   gint width = PANGO_PIXELS (50.0 * width_of_m (label) * PANGO_SCALE);
252   gtk_window_set_default_size (GTK_WINDOW (d), width, -1);
253
254
255   if (pictogram)
256     gtk_box_pack_start (GTK_BOX (ca), pictogram, FALSE, FALSE, 5);
257   gtk_box_pack_start (GTK_BOX (ca), label, FALSE, FALSE, 5);
258   gtk_box_pack_end (GTK_BOX (ca), check, FALSE, FALSE, 5);
259
260   gtk_widget_show_all (d);
261
262   g_object_set (close,
263                 "has-focus", TRUE,
264                 "is-focus", TRUE,
265                 NULL);
266
267   while (1 == gtk_dialog_run (GTK_DIALOG (d)))
268     {
269       if (++x >= N_TIPS) x = 0;
270       g_object_set (label, "label", gettext (tips[x]), NULL);
271     }
272
273   show_tip = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check));
274   psppire_conf_set_boolean (conf,
275                             "startup", "show-user-tips",
276                             show_tip);
277
278   g_object_unref (conf);
279
280   gtk_widget_destroy (d);
281 }
282
283
284 static void
285 on_startup (GApplication * app, gpointer ud)
286 {
287   GMainContext *context = g_main_context_new ();
288
289   if (start_time != 0)
290     {
291       wsplash = create_splash_window ();
292       gtk_application_add_window (GTK_APPLICATION (app),
293                                   GTK_WINDOW (wsplash));
294
295       g_signal_connect_swapped (wsplash, "destroy", G_CALLBACK (user_tip), app);
296     }
297   else
298     {
299       g_signal_connect (app, "activate", G_CALLBACK (user_tip), NULL);
300     }
301
302   GMainLoop *loop = g_main_loop_new (context, FALSE);
303
304   GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
305
306   ((struct init_source *) ss)->loop = loop;
307   ((struct init_source *) ss)->state = 0;
308
309   g_source_set_priority (ss, G_PRIORITY_DEFAULT);
310
311   g_source_attach (ss, context);
312   g_main_loop_run (loop);
313 }
314
315
316 static void
317 post_initialise (GApplication * app)
318 {
319   register_selection_functions ();
320   psppire_output_window_setup ();
321
322   GSimpleAction *quit = g_simple_action_new ("quit", NULL);
323   g_signal_connect_swapped (quit, "activate", G_CALLBACK (psppire_quit), app);
324   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit));
325 }
326
327
328 #define SPLASH_DURATION 1000
329
330 static gboolean
331 destroy_splash (gpointer ud)
332 {
333   GtkWidget *sp = GTK_WIDGET (ud);
334   gtk_widget_destroy (sp);
335   wsplash = NULL;
336   return G_SOURCE_REMOVE;
337 }
338
339
340 static void
341 wait_for_splash (GApplication *app, GtkWindow *x)
342 {
343   if (wsplash)
344     {
345       gtk_window_set_transient_for (GTK_WINDOW (wsplash), x);
346       gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (wsplash));
347       gtk_window_set_keep_above (GTK_WINDOW (wsplash), TRUE);
348       gtk_window_present (GTK_WINDOW (wsplash));
349
350       /* Remove the splash screen after SPLASH_DURATION milliseconds */
351       gint64 elapsed_time = (g_get_monotonic_time () - start_time) / 1000;
352       if (SPLASH_DURATION - elapsed_time <= 0)
353         destroy_splash (wsplash);
354       else
355         g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
356     }
357 }
358
359 static GtkWidget *fatal_error_dialog = NULL;
360 static GtkWidget *fatal_error_label;
361 static const char *diagnostic_info;
362
363 static void
364 fatal_error_handler (int sig)
365 {
366   /* Reset SIG to its default handling so that if it happens again we won't
367      recurse. */
368   signal (sig, SIG_DFL);
369
370   static char message [1024];
371   strcpy (message, "proximate cause:    ");
372   switch (sig)
373     {
374     case SIGABRT:
375       strcat (message, "Assertion Failure/Abort");
376       break;
377     case SIGFPE:
378       strcat (message, "Floating Point Exception");
379       break;
380     case SIGSEGV:
381       strcat (message, "Segmentation Violation");
382       break;
383     default:
384       strcat (message, "Unknown");
385       break;
386     }
387   strcat (message, "\n");
388   strcat (message, diagnostic_info);
389
390   g_object_set (fatal_error_label,
391                 "label", message,
392                 NULL);
393
394   gtk_dialog_run (GTK_DIALOG (fatal_error_dialog));
395
396   /* Re-raise the signal so that we terminate with the correct status. */
397   raise (sig);
398 }
399
400 static void
401 on_activate (GApplication * app, gpointer ud)
402 {
403   struct sigaction fatal_error_action;
404   sigset_t sigset;
405   g_return_if_fail (0 == sigemptyset (&sigset));
406   fatal_error_dialog =
407     gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
408                             _("Psppire: Fatal Error"));
409
410   diagnostic_info = prepare_diagnostic_information ();
411
412   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (fatal_error_dialog),
413                                             _("You have discovered a bug in PSPP.  "
414                                               "Please report this to %s including all of the following information, "
415                                               "and a description of what you were doing when this happened."),
416                                             PACKAGE_BUGREPORT);
417
418   g_return_if_fail (fatal_error_dialog != NULL);
419
420   GtkWidget *content_area = gtk_dialog_get_content_area (GTK_DIALOG (fatal_error_dialog));
421   fatal_error_label = gtk_label_new ("");
422   g_object_set (fatal_error_label,
423                 "selectable", TRUE,
424                 "wrap", TRUE,
425                 NULL);
426   gtk_container_add (GTK_CONTAINER (content_area), fatal_error_label);
427
428   gtk_widget_show_all (content_area);
429
430   fatal_error_action.sa_handler = fatal_error_handler;
431   fatal_error_action.sa_mask = sigset;
432   fatal_error_action.sa_flags = 0;
433
434   post_initialise (app);
435
436   GtkWindow *x = create_data_window ();
437   gtk_application_add_window (GTK_APPLICATION (app), x);
438
439   wait_for_splash (app, x);
440   sigaction (SIGABRT, &fatal_error_action, NULL);
441   sigaction (SIGSEGV, &fatal_error_action, NULL);
442   sigaction (SIGFPE,  &fatal_error_action, NULL);
443 }
444
445 static GtkWindow *
446 find_empty_data_window (GApplication *app)
447 {
448   GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
449   while (wl)
450     {
451       if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
452           psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
453         return GTK_WINDOW (wl->data);
454       wl = wl->next;
455     }
456   return NULL;
457 }
458
459 static GtkWindow *
460 find_psppire_window (GApplication *app)
461 {
462   GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
463   while (wl)
464     {
465       if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
466         return GTK_WINDOW (wl->data);
467       wl = wl->next;
468     }
469   return NULL;
470 }
471
472 static void
473 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
474          gpointer ud)
475 {
476   /* If the application is already open and we open another file
477      via xdg-open on GNU/Linux or via the file manager, then open is
478      called. Check if we already have a psppire window. */
479   if (find_psppire_window (app) == NULL)
480     post_initialise (app);
481
482   /* When a new data file is opened, then try to find an empty
483      data window which will then be replaced as in the open file
484      dialog */
485   GtkWindow *victim = find_empty_data_window (app);
486
487   gchar *file = g_file_get_parse_name (files[0]);
488   GtkWindow *x = psppire_preload_file (file, victim);
489   g_free (file);
490
491   wait_for_splash (app, x);
492 }
493
494
495 /* These are arguments which must be processed BEFORE the X server has been initialised */
496 static void
497 process_pre_start_arguments (int *argc, char ***argv)
498 {
499   GOptionEntry oe[] = {
500     {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
501      show_version_and_exit, N_("Show version information and exit"), 0},
502     {NULL, 0, 0, 0, NULL, "", 0}
503   };
504
505   GOptionContext *oc = g_option_context_new ("");
506   g_option_context_set_help_enabled (oc, FALSE);
507   g_option_context_set_ignore_unknown_options (oc, FALSE);
508   g_option_context_add_main_entries (oc, oe, NULL);
509   g_option_context_parse (oc, argc, argv, NULL);
510   g_option_context_free (oc);
511 }
512
513 int
514 main (int argc, char *argv[])
515 {
516   /* Some operating systems need to munge the arguments.  */
517   pre_initialisation (&argc, argv);
518
519   set_program_name (argv[0]);
520
521   GtkApplication *app =
522     gtk_application_new ("gnu.pspp", G_APPLICATION_HANDLES_OPEN);
523
524   process_pre_start_arguments (&argc, &argv);
525
526   GOptionEntry oe[] = {
527     {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
528       N_("Do not display the splash screen"), 0},
529     {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
530       N_("Do not attempt single instance negotiation"), 0},
531     {NULL}
532   };
533
534   g_application_add_main_option_entries (G_APPLICATION (app), oe);
535
536   g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
537   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
538   g_signal_connect (app, "handle-local-options",
539                     G_CALLBACK (on_local_options), NULL);
540   g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
541
542   {
543     GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
544     g_signal_connect_swapped (act_new_syntax, "activate",
545                               G_CALLBACK (create_syntax_window), NULL);
546     g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
547   }
548
549   {
550     GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
551     g_signal_connect_swapped (act_new_data, "activate",
552                               G_CALLBACK (create_data_window), NULL);
553     g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
554   }
555
556   g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
557   return g_application_run (G_APPLICATION (app), argc, argv);
558 }