Remove ToolbarButtonData
[geeqie.git] / src / window.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Authors: Vladimir Nadvornik, Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "window.h"
22
23 #include <array>
24 #include <cstdio>
25 #include <cstring>
26
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <gdk/gdk.h>
29 #include <glib-object.h>
30
31 #include <config.h>
32
33 #include "debug.h"
34 #include "intl.h"
35 #include "main-defines.h"
36 #include "main.h"
37 #include "misc.h"
38 #include "options.h"
39 #include "pixbuf-util.h"
40 #include "ui-fileops.h"
41 #include "ui-help.h"
42 #include "ui-misc.h"
43 #include "ui-utildlg.h"
44
45 namespace
46 {
47
48 struct HtmlBrowser {
49         operator bool() const { return (binary && *binary) || (command && *command); }
50         gchar *command_result() const;
51
52         const gchar *binary;  /**< the binary to look for in the path */
53         const gchar *command; /**< has 3 capabilities:
54                                *     nullptr  exec binary with html file path as command line
55                                *     string   exec string and use results for command line
56                                *     !string  use text following ! as command line, replacing optional %s with html file path */
57 };
58
59 constexpr std::array<HtmlBrowser, 10> html_browsers{{
60         /* Our specific script */
61         {GQ_APPNAME_LC "_html_browser", nullptr},
62         /* Redhat has a nifty htmlview script to start the user's preferred browser */
63         {"htmlview",                    nullptr},
64         /* Debian has even better approach with alternatives */
65         {"sensible-browser",            nullptr},
66         /* GNOME 2 */
67         {"gconftool-2",                 "gconftool-2 -g /desktop/gnome/url-handlers/http/command"},
68         /* KDE */
69         {"kfmclient",                   "!kfmclient exec \"%s\""},
70         /* use fallbacks */
71         {"firefox",                     nullptr},
72         {"mozilla",                     nullptr},
73         {"konqueror",                   nullptr},
74         {"netscape",                    nullptr},
75         {"opera",                       "!opera --remote 'openURL(%s,new-page)'"},
76 }};
77
78 gchar *HtmlBrowser::command_result() const
79 {
80         gchar *result = nullptr;
81         FILE *f;
82         gchar buf[2048];
83         gint l;
84
85         if (!binary || binary[0] == '\0') return nullptr;
86         if (!file_in_path(binary)) return nullptr;
87
88         if (!command || command[0] == '\0') return g_strdup(binary);
89         if (command[0] == '!') return g_strdup(command + 1);
90
91         f = popen(command, "r");
92         if (!f) return nullptr;
93
94         while ((l = fread(buf, sizeof(gchar), sizeof(buf), f)) > 0)
95                 {
96                 if (!result)
97                         {
98                         gint n = 0;
99
100                         while (n < l && buf[n] != '\n' && buf[n] != '\r') n++;
101                         if (n > 0) result = g_strndup(buf, n);
102                         }
103                 }
104
105         pclose(f);
106
107         return result;
108 }
109
110 } // namespace
111
112 GtkWidget *window_new(const gchar *role, const gchar *icon, const gchar *icon_file, const gchar *subtitle)
113 {
114         gchar *title;
115         GtkWidget *window;
116
117 #if HAVE_GTK4
118         window = gtk_window_new();
119 #else
120         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
121 #endif
122         if (!window) return nullptr;
123
124         if (subtitle)
125                 {
126                 title = g_strdup_printf("%s - %s", subtitle, GQ_APPNAME);
127                 }
128         else
129                 {
130                 title = g_strdup_printf("%s", GQ_APPNAME);
131                 }
132
133         gtk_window_set_title(GTK_WINDOW(window), title);
134         g_free(title);
135
136         window_set_icon(window, icon, icon_file);
137         gtk_window_set_role(GTK_WINDOW(window), role);
138
139         if (options->hide_window_decorations)
140                 {
141                 gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
142                 }
143
144         return window;
145 }
146
147 void window_set_icon(GtkWidget *window, const gchar *icon, const gchar *file)
148 {
149         if (!icon && !file) icon = PIXBUF_INLINE_ICON;
150
151         if (icon)
152                 {
153                 GdkPixbuf *pixbuf;
154
155                 pixbuf = pixbuf_inline(icon);
156                 if (pixbuf)
157                         {
158                         gtk_window_set_icon(GTK_WINDOW(window), pixbuf);
159                         g_object_unref(pixbuf);
160                         }
161                 }
162         else
163                 {
164                 gtk_window_set_icon_from_file(GTK_WINDOW(window), file, nullptr);
165                 }
166 }
167
168 gboolean window_maximized(GtkWidget *window)
169 {
170         GdkWindowState state;
171
172         if (!window || !gtk_widget_get_window(window)) return FALSE;
173
174         state = gdk_window_get_state(gtk_widget_get_window(window));
175         return !!(state & GDK_WINDOW_STATE_MAXIMIZED);
176 }
177
178 /*
179  *-----------------------------------------------------------------------------
180  * Open browser with the help Documentation
181  *-----------------------------------------------------------------------------
182  */
183
184 static int help_browser_command(const gchar *command, const gchar *path)
185 {
186         gchar *result;
187         gchar *buf;
188         gchar *begin;
189         gchar *end;
190         int retval = -1;
191
192         if (!command || !path) return retval;
193
194         DEBUG_1("Help command pre \"%s\", \"%s\"", command, path);
195
196         buf = g_strdup(command);
197         begin = strstr(buf, "%s");
198         if (begin)
199                 {
200                 *begin = '\0';
201                 end = begin + 2;
202                 begin = buf;
203
204                 result = g_strdup_printf("%s%s%s &", begin, path, end);
205                 }
206         else
207                 {
208                 result = g_strdup_printf("%s \"%s\" &", command, path);
209                 }
210         g_free(buf);
211
212         DEBUG_1("Help command post [%s]", result);
213
214         retval = runcmd(result);
215         DEBUG_1("Help command exit code: %d", retval);
216
217         g_free(result);
218         return retval;
219 }
220
221 static void help_browser_run(const gchar *path)
222 {
223         const auto try_browser = [path](const HtmlBrowser &html_browser)
224         {
225                 if (!html_browser) return false;
226
227                 DEBUG_1("Trying browser: name=%s command=%s", html_browser.binary, html_browser.command);
228
229                 gchar *result = html_browser.command_result();
230                 DEBUG_1("Result: %s", result);
231                 if (!result) return false;
232
233                 int ret = help_browser_command(result, path);
234                 g_free(result);
235                 return ret == 0;
236         };
237
238         if (try_browser({options->helpers.html_browser.command_name, options->helpers.html_browser.command_line})) return;
239
240         for (const auto &html_browser : html_browsers)
241                 {
242                 if (try_browser(html_browser)) return;
243                 }
244
245         log_printf("Unable to detect an installed browser.\n");
246 }
247
248 /*
249  *-----------------------------------------------------------------------------
250  * help window
251  *-----------------------------------------------------------------------------
252  */
253
254 static GtkWidget *help_window = nullptr;
255
256 static void help_window_destroy_cb(GtkWidget *, gpointer)
257 {
258         help_window = nullptr;
259 }
260
261 void help_window_show(const gchar *key)
262 {
263         gchar *path;
264
265         if (key && strstr(key, ".html") != nullptr)
266                 {
267                 path = g_build_filename(gq_htmldir, key, NULL);
268                 if (!isfile(path))
269                         {
270                         if (g_strcmp0(key, "index.html") == 0)
271                                 {
272                                 path = g_build_filename("https://www.geeqie.org/help/", "GuideIndex.html", NULL);
273                                 }
274                         else
275                                 {
276                                 path = g_build_filename("https://www.geeqie.org/help/", key, NULL);
277                                 }
278                         }
279                 help_browser_run(path);
280                 g_free(path);
281                 return;
282                 }
283
284         if (help_window)
285                 {
286                 gtk_window_present(GTK_WINDOW(help_window));
287                 if (key) help_window_set_key(help_window, key);
288                 return;
289                 }
290
291         if (!strcmp(key, "release_notes"))
292                 {
293                 path = g_build_filename(gq_helpdir, "README.html", NULL);
294                 if (isfile(path))
295                         {
296                         g_free(path);
297                         path = g_build_filename("file://", gq_helpdir, "README.html", NULL);
298                         help_browser_run(path);
299                         g_free(path);
300                         }
301                 else
302                         {
303                         g_free(path);
304                         path = g_build_filename(gq_helpdir, "README.md", NULL);
305                         help_window = help_window_new(_("Help"), "help", path, key);
306                         g_free(path);
307
308                         g_signal_connect(G_OBJECT(help_window), "destroy",
309                                          G_CALLBACK(help_window_destroy_cb), NULL);
310                         }
311                 }
312         else
313                 {
314                 path = g_build_filename(gq_helpdir, "ChangeLog.html", NULL);
315                 if (isfile(path))
316                         {
317                         g_free(path);
318                         path = g_build_filename("file://", gq_helpdir, "ChangeLog.html", NULL);
319                         help_browser_run(path);
320                         g_free(path);
321                         }
322                 else
323                         {
324                         g_free(path);
325                         path = g_build_filename(gq_helpdir, "ChangeLog", NULL);
326                         help_window = help_window_new(_("Help"), "help", path, key);
327                         g_free(path);
328
329                         g_signal_connect(G_OBJECT(help_window), "destroy",
330                                          G_CALLBACK(help_window_destroy_cb), NULL);
331                         }
332
333                 }
334 }
335
336 /*
337  *-----------------------------------------------------------------------------
338  * on-line help search dialog
339  *-----------------------------------------------------------------------------
340  */
341
342 struct HelpSearchData {
343         GenericDialog *gd;
344         GtkWidget *edit_widget;
345         gchar *text_entry;
346 };
347
348 static void help_search_window_show_icon_press(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer userdata)
349 {
350         auto hsd = static_cast<HelpSearchData *>(userdata);
351
352         g_free(hsd->text_entry);
353         hsd->text_entry = g_strdup("");
354         gq_gtk_entry_set_text(GTK_ENTRY(hsd->edit_widget), hsd->text_entry);
355 }
356
357 static void help_search_window_ok_cb(GenericDialog *, gpointer data)
358 {
359         auto hsd = static_cast<HelpSearchData *>(data);
360         gchar *search_command;
361
362         search_command = g_strconcat(options->help_search_engine,
363                                                 gq_gtk_entry_get_text(GTK_ENTRY(hsd->edit_widget)),
364                                                 NULL);
365         help_browser_run(search_command);
366         g_free(search_command);
367
368         g_free(hsd);
369 }
370
371 static void help_search_window_cancel_cb(GenericDialog *, gpointer data)
372 {
373         auto hsd = static_cast<HelpSearchData *>(data);
374
375         g_free(hsd);
376 }
377
378 void help_search_window_show()
379 {
380         GenericDialog *gd;
381         GtkWidget *table;
382         GtkWidget *label1;
383         GtkWidget *label2;
384
385         auto hsd = g_new0(HelpSearchData, 1);
386         hsd->gd = gd = generic_dialog_new(_("On-line help search"), "help_search",
387                                 nullptr, TRUE,
388                                 help_search_window_cancel_cb, hsd);
389         generic_dialog_add_message(gd, nullptr, _("Search the on-line help files.\n"), nullptr, FALSE);
390
391         generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
392                                   help_search_window_ok_cb, TRUE);
393
394         label1 = pref_label_new(GENERIC_DIALOG(gd)->vbox, _("Search engine:"));
395         gtk_label_set_xalign(GTK_LABEL(label1), 0.0);
396         gtk_label_set_yalign(GTK_LABEL(label1), 0.5);
397
398         label2 = pref_label_new(GENERIC_DIALOG(gd)->vbox, options->help_search_engine);
399         gtk_label_set_xalign(GTK_LABEL(label2), 0.0);
400         gtk_label_set_yalign(GTK_LABEL(label2), 0.5);
401
402         pref_spacer(GENERIC_DIALOG(gd)->vbox, 0);
403
404         table = pref_table_new(gd->vbox, 3, 1, FALSE, TRUE);
405         pref_table_label(table, 0, 0, _("Search terms:"), GTK_ALIGN_END);
406         hsd->edit_widget = gtk_entry_new();
407         gtk_widget_set_size_request(hsd->edit_widget, 300, -1);
408         gq_gtk_grid_attach_default(GTK_GRID(table), hsd->edit_widget, 1, 2, 0, 1);
409         generic_dialog_attach_default(gd, hsd->edit_widget);
410         gtk_widget_show(hsd->edit_widget);
411
412         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(hsd->edit_widget),
413                                                 GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
414         gtk_entry_set_icon_tooltip_text (GTK_ENTRY(hsd->edit_widget),
415                                                 GTK_ENTRY_ICON_SECONDARY, _("Clear"));
416         g_signal_connect(GTK_ENTRY(hsd->edit_widget), "icon-press",
417                                                 G_CALLBACK(help_search_window_show_icon_press), hsd);
418
419         gtk_widget_grab_focus(hsd->edit_widget);
420
421         gtk_widget_show(gd->dialog);
422 }
423 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */