e63829cb06c7a143b80bca16d5bfe0d76e8031a3
[pspp] / src / ui / gui / help-menu.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013, 2015, 2016,
3    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
19 #include <config.h>
20
21 #include <gtk/gtk.h>
22
23 #include "libpspp/cast.h"
24 #include "libpspp/copyleft.h"
25 #include "libpspp/message.h"
26 #include "libpspp/version.h"
27 #include "ui/gui/help-menu.h"
28
29 #include "gl/configmake.h"
30 #include "gl/relocatable.h"
31
32 #include <gettext.h>
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) msgid
35
36 /* Try to open html documentation uri via the default
37    browser on the operating system */
38 #ifdef __APPLE__
39 #define HTMLOPENAPP "open"
40 #elif  _WIN32
41 #define HTMLOPENAPP "wscript"
42 #else
43 #define HTMLOPENAPP "xdg-open"
44 #endif
45
46 static const gchar *artists[] = { "Bastián Díaz", "Hugo Alejandro", NULL};
47
48 /* Opening the htmluri in windows via cmd /start uri opens
49    the windows command shell for a moment. The alternative is
50    to start a script via wscript. This will not be visible*/
51 #ifdef _WIN32
52 static gboolean
53 open_windows_help (const gchar *helpuri, GError **err)
54 {
55   SHELLEXECUTEINFOA info;
56   memset (&info, 0, sizeof (info));
57
58   info.cbSize = sizeof (info);
59   info.fMask = SEE_MASK_FLAG_NO_UI;
60   info.lpVerb = "open";
61   info.lpFile = helpuri;
62   info.nShow = SW_SHOWNORMAL;
63
64   BOOL ret = ShellExecuteExA (&info);
65
66   if (ret)
67     return TRUE;
68
69   /* Contrary to what the microsoft documentation indicates, ShellExecuteExA does
70      not seem to setLastError.  So we have to deal with errors ourselves here.  */
71   const char *msg = 0;
72   switch (GPOINTER_TO_INT (info.hInstApp))
73     {
74     case SE_ERR_FNF:
75       msg = "File not found";
76       break;
77     case SE_ERR_PNF:
78       msg = "Path not found";
79       break;
80     case SE_ERR_ACCESSDENIED:
81       msg = "Access denied";
82       break;
83     case SE_ERR_OOM:
84       msg = "Out of memory";
85       break;
86     case SE_ERR_DLLNOTFOUND:
87       msg = "Dynamic-link library not found";
88       break;
89     case SE_ERR_SHARE:
90       msg = "Cannot share an open file";
91       break;
92     case SE_ERR_ASSOCINCOMPLETE:
93       msg = "File association information not complete";
94       break;
95     case SE_ERR_DDETIMEOUT:
96       msg = "DDE operation timed out";
97       break;
98     case SE_ERR_DDEFAIL:
99       msg = "DDE operation failed";
100       break;
101     case SE_ERR_DDEBUSY:
102       msg = "DDE operation is busy";
103       break;
104     case SE_ERR_NOASSOC:
105       msg = "File association not available";
106       break;
107     default:
108       msg = "Unknown error";
109       break;
110     }
111
112   *err = g_error_new_literal (g_quark_from_static_string ("pspp-help-error"),
113                        0,
114                        msg);
115
116   return FALSE;
117 }
118
119 static gboolean
120 on_activate_link (GtkAboutDialog *label,
121                gchar          *uri,
122                gpointer        user_data)
123 {
124   return  open_windows_help (uri, NULL);
125 }
126 #endif
127
128 static void
129 about_new (GtkMenuItem *mmm, GtkWindow *parent)
130 {
131   GtkWidget *about =  gtk_about_dialog_new ();
132
133 #ifdef _WIN32
134   /* The default handler for Windows doesn't appear to work.  */
135   g_signal_connect (about, "activate-link", G_CALLBACK (on_activate_link), parent);
136 #endif
137
138   gtk_about_dialog_set_logo_icon_name (GTK_ABOUT_DIALOG (about), "pspp");
139
140   gtk_window_set_icon_name (GTK_WINDOW (about), "pspp");
141
142   gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (about), PACKAGE_URL);
143
144   gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (about),
145                                 announced_version);
146
147   gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (about),
148                                 (const gchar **) authors);
149
150   gtk_about_dialog_set_artists (GTK_ABOUT_DIALOG (about),
151                                 artists);
152
153   gtk_about_dialog_set_license (GTK_ABOUT_DIALOG (about),
154                                 copyleft);
155
156   gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG (about),
157                                  _("A program for the analysis of sampled data"));
158
159   gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (about),
160                                   "Free Software Foundation");
161
162   gtk_about_dialog_set_translator_credits
163     (
164      GTK_ABOUT_DIALOG (about),
165      /* TRANSLATORS: Do not translate this string.  Instead, put the names of the people
166         who have helped in the translation. */
167      _("translator-credits")
168 );
169
170   gtk_window_set_transient_for (GTK_WINDOW (about), parent);
171
172   gtk_window_set_modal (GTK_WINDOW (about), TRUE);
173
174   gtk_dialog_run (GTK_DIALOG (about));
175
176   gtk_widget_hide (about);
177 }
178
179
180
181 /* Open the manual at PAGE with the following priorities
182    First: local yelp help system
183    Second: browser with local html doc dir in path pspp.html/<helppage>.html
184    Third:  browers with Internet html help at gnu.org */
185 void
186 online_help (const char *page)
187 {
188   GError *htmlerr = NULL;
189   gchar *htmlfilename = NULL;
190   gchar *htmlfullname = NULL;
191   gchar *htmluri = NULL;
192
193   if (page == NULL)
194     {
195       htmlfilename = g_strdup ("index.html");
196     }
197   else
198     {
199       gchar **tokens = NULL;
200       const int maxtokens = 5;
201       int idx ;
202       /* The page will be translated to the htmlfilename
203          page                   htmlfilename
204          GRAPH#SCATTERPLOT      SCATTERPLOT.html
205          QUICK-CLUSTER          QUICK-CLUSTER.html
206          which is valid for the multiple page html doc*/
207       tokens = g_strsplit (page, "#", maxtokens);
208       for (idx = 0; idx < maxtokens && tokens[idx]; idx++)
209         ;
210       htmlfilename = g_strdup_printf ("%s.html", tokens[idx-1]);
211       g_strfreev (tokens);
212     }
213   /* Hint: pspp.html is a directory...*/
214   htmlfullname = g_strdup_printf ("%s/%s", relocate (DOCDIR "/pspp.html"),
215                                   htmlfilename);
216   if (g_file_test (relocate (DOCDIR "/pspp.html"), G_FILE_TEST_IS_DIR))
217     {
218       GError *urierr = NULL;
219       htmluri =  g_filename_to_uri (htmlfullname,NULL, &urierr);
220       if (!htmluri)
221         {
222           msg (ME, _("Help path conversion error: %s"), urierr->message);
223           htmluri = htmlfullname;
224         }
225       g_clear_error (&urierr);
226     }
227   else
228     htmluri = g_strdup_printf (PACKAGE_URL "manual/html_node/%s",
229                                htmlfilename);
230   g_free (htmlfullname);
231   g_free (htmlfilename);
232
233 #ifdef _WIN32
234   bool ok = open_windows_help (htmluri, &htmlerr);
235 #else
236   gchar *htmlargv[3] = {CONST_CAST (char *, HTMLOPENAPP), htmluri, 0};
237   bool ok = g_spawn_async (NULL, htmlargv,
238                            NULL, G_SPAWN_SEARCH_PATH,
239                            NULL, NULL, NULL, &htmlerr);
240 #endif
241   if (!ok)
242     {
243       msg (ME, _("Cannot open via html: %s "
244                  "with uri: %s "
245                  "The PSSP manual is also available at %s"),
246                   htmlerr->message,
247                   htmluri,
248                   PACKAGE_URL "documentation.html");
249     }
250
251   g_free (htmluri);
252   g_clear_error (&htmlerr);
253 }
254
255 static void
256 reference_manual (GtkMenuItem *menu, gpointer data)
257 {
258   online_help (NULL);
259 }
260
261 GtkWidget *
262 create_help_menu (GtkWindow *toplevel)
263 {
264   GtkWidget *menuitem = gtk_menu_item_new_with_mnemonic (_("_Help"));
265   GtkWidget *menu = gtk_menu_new ();
266
267   GtkWidget *help_about = gtk_menu_item_new_with_mnemonic (_("_About"));
268   GtkWidget *help_ref = gtk_menu_item_new_with_mnemonic (_("_Reference Manual"));
269
270   GtkAccelGroup *accel_group = gtk_accel_group_new ();
271
272   gtk_window_add_accel_group (toplevel, accel_group);
273
274   gtk_widget_add_accelerator (help_ref,
275                               "activate", accel_group,
276                               GDK_KEY_F1, 0,
277                               GTK_ACCEL_VISIBLE);
278
279   gtk_menu_attach (GTK_MENU (menu), help_ref, 0, 1, 0, 1);
280   gtk_menu_attach (GTK_MENU (menu), help_about, 0, 1, 1, 2);
281
282   g_signal_connect (help_about, "activate", G_CALLBACK (about_new), toplevel);
283   g_signal_connect (help_ref, "activate", G_CALLBACK (reference_manual), NULL);
284
285   g_object_set (menuitem, "submenu", menu, NULL);
286
287   gtk_widget_show_all (menuitem);
288
289   g_object_unref (accel_group);
290
291   return menuitem;
292 }