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