Fix deprecation warning for poppler >= 0.82
[geeqie.git] / src / shortcuts.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "shortcuts.h"
23
24 #include <glib-object.h>
25 #include <glib.h>
26
27 #include "collect.h"
28 #include "compat.h"
29 #include "intl.h"
30 #include "layout.h"
31 #include "main-defines.h"
32 #include "ui-bookmark.h"
33 #include "ui-fileops.h"
34 #include "ui-misc.h"
35 #include "ui-utildlg.h"
36 #include "utilops.h"
37
38 struct ShortcutsData
39 {
40         GtkWidget *vbox;
41         GtkWidget *bookmarks;
42         LayoutWindow *lw;
43
44         FileDialog *dialog;
45         GtkWidget *dialog_name_entry;
46
47         GtkWidget *add_button;
48 };
49
50 #define SHORTCUTS     "shortcuts"
51
52 static void shortcuts_bookmark_select(const gchar *path, gpointer data)
53 {
54         auto scd = static_cast<ShortcutsData *>(data);
55
56         if (file_extension_match(path, GQ_COLLECTION_EXT))
57                 {
58                 collection_window_new(path);
59                 }
60         else
61                 {
62                 layout_set_path(scd->lw, path);
63                 }
64
65 }
66
67 static void shortcuts_add_close(ShortcutsData *scd)
68 {
69         if (scd->dialog) file_dialog_close(scd->dialog);
70         scd->dialog_name_entry = nullptr;
71         scd->dialog = nullptr;
72 }
73
74 static void shortcuts_add_ok_cb(FileDialog *fd, gpointer data)
75 {
76         auto scd = static_cast<ShortcutsData *>(data);
77         const gchar *name = gtk_entry_get_text(GTK_ENTRY(scd->dialog_name_entry));
78         gboolean empty_name = (name[0] == '\0');
79
80         name = gtk_entry_get_text(GTK_ENTRY(scd->dialog_name_entry));
81
82         if (empty_name)
83                 {
84                 name = filename_from_path(fd->dest_path);
85                 }
86
87         bookmark_list_add(scd->bookmarks, name, fd->dest_path);
88
89         shortcuts_add_close(scd);
90 }
91
92 static void shortcuts_add_cancel_cb(FileDialog *, gpointer data)
93 {
94         auto scd = static_cast<ShortcutsData *>(data);
95
96         shortcuts_add_close(scd);
97 }
98
99 static void shortcuts_add_cb(GtkWidget *button, gpointer data)
100 {
101         auto scd = static_cast<ShortcutsData *>(data);
102         GtkWidget *hbox;
103         const gchar *title;
104
105         if (scd->dialog)
106                 {
107                 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(scd->dialog)->dialog));
108                 return;
109                 }
110
111         title = _("Add Shortcut");
112         scd->dialog = file_util_file_dlg(title,
113                                         "add_shortcuts", button,
114                                         shortcuts_add_cancel_cb, scd);
115         file_dialog_add_button(scd->dialog, GQ_ICON_OK, "OK", shortcuts_add_ok_cb, TRUE);
116
117         generic_dialog_add_message(GENERIC_DIALOG(scd->dialog), nullptr, title, nullptr, FALSE);
118
119         file_dialog_add_path_widgets(scd->dialog, nullptr, nullptr, "add_shortcuts", nullptr, nullptr);
120
121         hbox = pref_box_new(GENERIC_DIALOG(scd->dialog)->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
122
123         pref_label_new(hbox, _("Name:"));
124
125         scd->dialog_name_entry = gtk_entry_new();
126         gq_gtk_box_pack_start(GTK_BOX(hbox), scd->dialog_name_entry, TRUE, TRUE, 0);
127         generic_dialog_attach_default(GENERIC_DIALOG(scd->dialog), scd->dialog_name_entry);
128         gtk_widget_show(scd->dialog_name_entry);
129
130         gtk_widget_show(GENERIC_DIALOG(scd->dialog)->dialog);
131 }
132
133 static void shortcuts_destroy(GtkWidget *, gpointer data)
134 {
135         auto scd = static_cast<ShortcutsData *>(data);
136
137         shortcuts_add_close(scd);
138
139         g_free(scd);
140 }
141
142 static GtkWidget *shortcuts_new(LayoutWindow *lw)
143 {
144         ShortcutsData *scd;
145         GtkWidget *tbar;
146
147         if (!lw) return nullptr;
148
149         scd = g_new0(ShortcutsData, 1);
150
151         scd->lw = lw;
152
153         scd->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
154         g_object_set_data(G_OBJECT(scd->vbox), "shortcuts_data", scd);
155         g_signal_connect(G_OBJECT(scd->vbox), "destroy",
156                         G_CALLBACK(shortcuts_destroy), scd);
157
158         scd->bookmarks = bookmark_list_new(SHORTCUTS, shortcuts_bookmark_select, scd);
159         gq_gtk_box_pack_start(GTK_BOX(scd->vbox), scd->bookmarks, TRUE, TRUE, 0);
160         gtk_widget_show(scd->bookmarks);
161
162         tbar = pref_toolbar_new(scd->vbox);
163
164         scd->add_button = pref_toolbar_button(tbar, GQ_ICON_ADD, _("Add"), FALSE,
165                                         _("Add Shortcut"),
166                                         G_CALLBACK(shortcuts_add_cb), scd);
167
168         return scd->vbox;
169 }
170
171 GtkWidget *shortcuts_new_from_config(LayoutWindow *lw, const gchar **, const gchar **)
172 {
173         GtkWidget *shortcuts_bar;
174
175         shortcuts_bar = shortcuts_new(lw);
176         gtk_widget_show(shortcuts_bar);
177
178         return shortcuts_bar;
179 }
180
181 GtkWidget *shortcuts_new_default(LayoutWindow *lw)
182 {
183         return shortcuts_new_from_config(lw, nullptr, nullptr);
184 }
185
186 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */