gui: Mark "Show tips at startup" for translation.
[pspp.git] / src / ui / gui / main.c
blob0e4ed2c68ca6a0478185e6ee7b804ec388d6e376
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
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/>. */
18 #include <config.h>
20 #include "pre-initialisation.h"
22 #include "ui/gui/options-dialog.h"
23 #include "ui/gui/psppire.h"
25 #include <gtk/gtk.h>
26 #include <stdlib.h>
28 #include "language/lexer/include-path.h"
29 #include "libpspp/argv-parser.h"
30 #include "libpspp/array.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/cast.h"
33 #include "libpspp/copyleft.h"
34 #include "libpspp/message.h"
35 #include "libpspp/str.h"
36 #include "libpspp/string-array.h"
37 #include "libpspp/version.h"
38 #include "ui/source-init-opts.h"
39 #include "ui/gui/psppire-syntax-window.h"
40 #include "ui/gui/psppire-data-window.h"
41 #include "ui/gui/psppire-output-window.h"
42 #include "ui/gui/psppire-conf.h"
43 #include "ui/gui/helper.h"
45 #include "gl/configmake.h"
46 #include "gl/progname.h"
47 #include "gl/relocatable.h"
48 #include "gl/version-etc.h"
49 #include "gl/xalloc.h"
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53 #define N_(msgid) msgid
57 static gboolean
58 show_version_and_exit (void)
60 version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
61 "Ben Pfaff", "John Darrington", "Jason Stover", NULL_SENTINEL);
63 exit (0);
65 return TRUE;
70 static gboolean
71 init_prepare (GSource * source, gint * timeout_)
73 return TRUE;
76 static gboolean
77 init_check (GSource * source)
79 return TRUE;
82 static gboolean
83 init_dispatch (GSource * ss, GSourceFunc callback, gpointer user_data)
85 struct init_source *is = (struct init_source *) ss;
87 bool finished = initialize (is);
88 is->state++;
90 if (finished)
92 g_main_loop_quit (is->loop);
93 return FALSE;
96 return TRUE;
99 static GSourceFuncs init_funcs =
100 {init_prepare, init_check, init_dispatch, NULL, NULL, NULL };
102 static GtkWidget *wsplash = 0;
103 static gint64 start_time = 0;
106 static GtkWidget *
107 create_splash_window (void)
109 GtkWidget *sp = gtk_window_new (GTK_WINDOW_TOPLEVEL);
111 char *splash = relocate_clone (PKGDATADIR"/splash.png");
112 GtkWidget *l = gtk_image_new_from_file (splash);
113 free (splash);
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);
124 GdkGeometry hints;
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);
137 return sp;
141 static gint
142 on_local_options (GApplication * application,
143 GVariantDict * options, gpointer user_data)
146 GVariant *b =
147 g_variant_dict_lookup_value (options, "no-unique",
148 G_VARIANT_TYPE_BOOLEAN);
149 if (b)
151 GApplicationFlags flags = g_application_get_flags (application);
152 flags |= G_APPLICATION_NON_UNIQUE;
153 g_application_set_flags (application, flags);
154 g_variant_unref (b);
158 GVariant *b =
159 g_variant_dict_lookup_value (options, "no-splash",
160 G_VARIANT_TYPE_BOOLEAN);
161 if (b)
162 g_variant_unref (b);
163 else
164 start_time = g_get_monotonic_time ();
168 return -1;
171 #ifdef __APPLE__
172 #define NON_FREE_OS "MacOS"
173 #elif _WIN32
174 #define NON_FREE_OS "Windows"
175 #endif
177 /* Use the imperitive mood for all entries in this table.
178 Each entry should end with a period. */
179 static const char *tips[] =
181 #ifdef NON_FREE_OS
182 N_("PSPP runs best on free platforms such as GNU and GNU/Linux. " NON_FREE_OS " is a non-free system. As such, certain features might work sub-optimally. For best results use a free system instead."),
183 #endif
184 N_("Right click on variable lists to change between viewing the variables' names and their labels."),
185 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."),
186 N_("Directly import your spreadsheets using the \"File | Import Data\" menu."),
187 N_("For an easy way to convert string variables into numerically encoded variables, use \"Automatic Recode\" which preserves the variable names as labels."),
188 N_("When browsing large data sets, use \"Windows | Split\" to see both ends of the data in the same view."),
189 N_("Export your reports to ODT format for easy editing with the Libreoffice.org suite."),
190 N_("Use \"Edit | Options\" to have your Output window automatically appear when statistics are generated."),
191 N_("To easily reorder your variables, drag and drop them in the Variable View or the Data View.")
194 #define N_TIPS (sizeof tips / sizeof tips[0])
196 static void
197 user_tip (GApplication *app)
199 gboolean show_tip = TRUE;
200 psppire_conf_get_boolean ("startup", "show-user-tips", &show_tip);
202 if (!show_tip)
203 return;
205 GtkWindow *parent = gtk_application_get_active_window (GTK_APPLICATION (app));
207 GtkWidget *d =
208 gtk_dialog_new_with_buttons (_("Psppire User Hint"), parent,
209 GTK_DIALOG_MODAL,
210 GTK_MESSAGE_INFO,
211 0, 0,
212 NULL);
214 GtkWidget *pictogram = gtk_image_new_from_icon_name ("user-info", GTK_ICON_SIZE_DIALOG);
216 GtkWidget *next = gtk_button_new_with_mnemonic (_("_Next Tip"));
217 gtk_dialog_add_action_widget (GTK_DIALOG (d), next, 1);
219 GtkWidget *close = gtk_button_new_with_mnemonic (_("_Close"));
220 gtk_dialog_add_action_widget (GTK_DIALOG (d), close, GTK_RESPONSE_CLOSE);
222 gtk_window_set_transient_for (GTK_WINDOW (d), parent);
224 g_object_set (d,
225 "decorated", FALSE,
226 "skip-taskbar-hint", TRUE,
227 "skip-pager-hint", TRUE,
228 "application", app,
229 NULL);
231 GtkWidget *ca = gtk_dialog_get_content_area (GTK_DIALOG (d));
233 g_object_set (ca, "margin", 5, NULL);
235 GtkWidget *check = gtk_check_button_new_with_mnemonic (_("_Show tips at startup"));
236 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), show_tip);
238 srand (time(0));
239 gint x = rand () % N_TIPS;
240 GtkWidget *label = gtk_label_new (gettext (tips[x]));
242 /* Make the font of the label a little larger than the other widgets. */
244 GtkStyleContext *sc = gtk_widget_get_style_context (label);
245 GtkCssProvider *p = gtk_css_provider_new ();
246 const gchar *css = "* {font-size: 130%;}";
247 if (gtk_css_provider_load_from_data (p, css, strlen (css), NULL))
249 gtk_style_context_add_provider (sc, GTK_STYLE_PROVIDER (p),
250 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
252 g_object_unref (p);
255 /* It's more readable if the text is not all in one long line. */
256 g_object_set (label, "wrap", TRUE, NULL);
257 gint width = PANGO_PIXELS (50.0 * width_of_m (label) * PANGO_SCALE);
258 gtk_window_set_default_size (GTK_WINDOW (d), width, -1);
261 if (pictogram)
262 gtk_box_pack_start (GTK_BOX (ca), pictogram, FALSE, FALSE, 5);
263 gtk_box_pack_start (GTK_BOX (ca), label, FALSE, FALSE, 5);
264 gtk_box_pack_end (GTK_BOX (ca), check, FALSE, FALSE, 5);
266 gtk_widget_show_all (d);
268 g_object_set (close,
269 "has-focus", TRUE,
270 "is-focus", TRUE,
271 NULL);
273 while (1 == gtk_dialog_run (GTK_DIALOG (d)))
275 if (++x >= N_TIPS) x = 0;
276 g_object_set (label, "label", gettext (tips[x]), NULL);
279 show_tip = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check));
280 psppire_conf_set_boolean ("startup", "show-user-tips", show_tip);
281 psppire_conf_save ();
283 gtk_widget_destroy (d);
287 static void
288 on_startup (GApplication * app, gpointer ud)
290 GMainContext *context = g_main_context_new ();
292 if (start_time != 0)
294 wsplash = create_splash_window ();
295 gtk_application_add_window (GTK_APPLICATION (app),
296 GTK_WINDOW (wsplash));
298 g_signal_connect_swapped (wsplash, "destroy", G_CALLBACK (user_tip), app);
300 else
302 g_signal_connect (app, "activate", G_CALLBACK (user_tip), NULL);
305 GMainLoop *loop = g_main_loop_new (context, FALSE);
307 GSource *ss = g_source_new (&init_funcs, sizeof (struct init_source));
309 ((struct init_source *) ss)->loop = loop;
310 ((struct init_source *) ss)->state = 0;
312 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
314 g_source_attach (ss, context);
315 g_main_loop_run (loop);
319 static void
320 post_initialise (GApplication * app)
322 register_selection_functions ();
323 psppire_output_window_setup ();
325 GSimpleAction *quit = g_simple_action_new ("quit", NULL);
326 g_signal_connect_swapped (quit, "activate", G_CALLBACK (psppire_quit), app);
327 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit));
331 #define SPLASH_DURATION 1000
333 static gboolean
334 destroy_splash (gpointer ud)
336 GtkWidget *sp = GTK_WIDGET (ud);
337 gtk_widget_destroy (sp);
338 wsplash = NULL;
339 return G_SOURCE_REMOVE;
343 static void
344 wait_for_splash (GApplication *app, GtkWindow *x)
346 if (wsplash)
348 gtk_window_set_transient_for (GTK_WINDOW (wsplash), x);
349 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (wsplash));
350 gtk_window_set_keep_above (GTK_WINDOW (wsplash), TRUE);
351 gtk_window_present (GTK_WINDOW (wsplash));
353 /* Remove the splash screen after SPLASH_DURATION milliseconds */
354 gint64 elapsed_time = (g_get_monotonic_time () - start_time) / 1000;
355 if (SPLASH_DURATION - elapsed_time <= 0)
356 destroy_splash (wsplash);
357 else
358 g_timeout_add (SPLASH_DURATION - elapsed_time, destroy_splash, wsplash);
362 static GtkWidget *fatal_error_dialog = NULL;
363 static GtkWidget *fatal_error_label;
364 static const char *diagnostic_info;
366 static void
367 fatal_error_handler (int sig)
369 /* Reset SIG to its default handling so that if it happens again we won't
370 recurse. */
371 signal (sig, SIG_DFL);
373 static char message [1024];
374 strcpy (message, "proximate cause: ");
375 switch (sig)
377 case SIGABRT:
378 strcat (message, "Assertion Failure/Abort");
379 break;
380 case SIGFPE:
381 strcat (message, "Floating Point Exception");
382 break;
383 case SIGSEGV:
384 strcat (message, "Segmentation Violation");
385 break;
386 default:
387 strcat (message, "Unknown");
388 break;
390 strcat (message, "\n");
391 strcat (message, diagnostic_info);
393 g_object_set (fatal_error_label,
394 "label", message,
395 NULL);
397 gtk_dialog_run (GTK_DIALOG (fatal_error_dialog));
399 /* Re-raise the signal so that we terminate with the correct status. */
400 raise (sig);
403 static void
404 on_activate (GApplication * app, gpointer ud)
406 struct sigaction fatal_error_action;
407 sigset_t sigset;
408 g_return_if_fail (0 == sigemptyset (&sigset));
409 fatal_error_dialog =
410 gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
411 _("Psppire: Fatal Error"));
413 diagnostic_info = prepare_diagnostic_information ();
415 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (fatal_error_dialog),
416 _("You have discovered a bug in PSPP. "
417 "Please report this to %s including all of the following information, "
418 "and a description of what you were doing when this happened."),
419 PACKAGE_BUGREPORT);
421 g_return_if_fail (fatal_error_dialog != NULL);
423 GtkWidget *content_area = gtk_dialog_get_content_area (GTK_DIALOG (fatal_error_dialog));
424 fatal_error_label = gtk_label_new ("");
425 g_object_set (fatal_error_label,
426 "selectable", TRUE,
427 "wrap", TRUE,
428 NULL);
429 gtk_container_add (GTK_CONTAINER (content_area), fatal_error_label);
431 gtk_widget_show_all (content_area);
433 fatal_error_action.sa_handler = fatal_error_handler;
434 fatal_error_action.sa_mask = sigset;
435 fatal_error_action.sa_flags = 0;
437 post_initialise (app);
439 GtkWindow *x = create_data_window ();
440 gtk_application_add_window (GTK_APPLICATION (app), x);
442 wait_for_splash (app, x);
443 sigaction (SIGABRT, &fatal_error_action, NULL);
444 sigaction (SIGSEGV, &fatal_error_action, NULL);
445 sigaction (SIGFPE, &fatal_error_action, NULL);
448 static GtkWindow *
449 find_empty_data_window (GApplication *app)
451 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
452 while (wl)
454 if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
455 psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
456 return GTK_WINDOW (wl->data);
457 wl = wl->next;
459 return NULL;
462 static GtkWindow *
463 find_psppire_window (GApplication *app)
465 GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
466 while (wl)
468 if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
469 return GTK_WINDOW (wl->data);
470 wl = wl->next;
472 return NULL;
475 static void
476 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
477 gpointer ud)
479 /* If the application is already open and we open another file
480 via xdg-open on GNU/Linux or via the file manager, then open is
481 called. Check if we already have a psppire window. */
482 if (find_psppire_window (app) == NULL)
483 post_initialise (app);
485 /* When a new data file is opened, then try to find an empty
486 data window which will then be replaced as in the open file
487 dialog */
488 GtkWindow *victim = find_empty_data_window (app);
490 gchar *file = g_file_get_parse_name (files[0]);
491 GtkWindow *x = psppire_preload_file (file, victim);
492 g_free (file);
494 wait_for_splash (app, x);
498 /* These are arguments which must be processed BEFORE the X server has been initialised */
499 static void
500 process_pre_start_arguments (int *argc, char ***argv)
502 GOptionEntry oe[] = {
503 {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
504 show_version_and_exit, N_("Show version information and exit"), 0},
505 {NULL, 0, 0, 0, NULL, "", 0}
508 GOptionContext *oc = g_option_context_new ("");
509 g_option_context_set_help_enabled (oc, FALSE);
510 g_option_context_set_ignore_unknown_options (oc, FALSE);
511 g_option_context_add_main_entries (oc, oe, NULL);
512 g_option_context_parse (oc, argc, argv, NULL);
513 g_option_context_free (oc);
517 main (int argc, char *argv[])
519 /* Some operating systems need to munge the arguments. */
520 pre_initialisation (&argc, argv);
522 set_program_name (argv[0]);
524 GtkApplication *app =
525 gtk_application_new ("org.gnu.pspp", G_APPLICATION_HANDLES_OPEN);
527 process_pre_start_arguments (&argc, &argv);
529 GOptionEntry oe[] = {
530 {"no-splash", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
531 N_("Do not display the splash screen"), 0},
532 {"no-unique", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
533 N_("Do not attempt single instance negotiation"), 0},
534 {NULL}
537 g_application_add_main_option_entries (G_APPLICATION (app), oe);
539 g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
540 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
541 g_signal_connect (app, "handle-local-options",
542 G_CALLBACK (on_local_options), NULL);
543 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
546 GSimpleAction *act_new_syntax = g_simple_action_new ("new-syntax", NULL);
547 g_signal_connect_swapped (act_new_syntax, "activate",
548 G_CALLBACK (create_syntax_window), NULL);
549 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_syntax));
553 GSimpleAction *act_new_data = g_simple_action_new ("new-data", NULL);
554 g_signal_connect_swapped (act_new_data, "activate",
555 G_CALLBACK (create_data_window), NULL);
556 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
559 g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
561 return g_application_run (G_APPLICATION (app), argc, argv);