c7327c2bb264688889363fc47bdbd0347b627308
[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 <config.h>
25
26 #include "collect.h"
27 #include "compat.h"
28 #include "intl.h"
29 #include "layout.h"
30 #include "main-defines.h"
31 #include "utilops.h"
32 #include "ui-bookmark.h"
33 #include "ui-fileops.h"
34 #include "ui-misc.h"
35
36 struct ShortcutsData
37 {
38         GtkWidget *vbox;
39         GtkWidget *bookmarks;
40         LayoutWindow *lw;
41
42         FileDialog *dialog;
43         GtkWidget *dialog_name_entry;
44
45         GtkWidget *add_button;
46 };
47
48 #define SHORTCUTS     "shortcuts"
49
50 static void shortcuts_bookmark_select(const gchar *path, gpointer data)
51 {
52         auto scd = static_cast<ShortcutsData *>(data);
53
54         if (file_extension_match(path, GQ_COLLECTION_EXT))
55                 {
56                 collection_window_new(path);
57                 }
58         else
59                 {
60                 layout_set_path(scd->lw, path);
61                 }
62
63 }
64
65 static void shortcuts_add_close(ShortcutsData *scd)
66 {
67         if (scd->dialog) file_dialog_close(scd->dialog);
68         scd->dialog_name_entry = nullptr;
69         scd->dialog = nullptr;
70 }
71
72 static void shortcuts_add_ok_cb(FileDialog *fd, gpointer data)
73 {
74         auto scd = static_cast<ShortcutsData *>(data);
75         const gchar *name = gtk_entry_get_text(GTK_ENTRY(scd->dialog_name_entry));
76         gboolean empty_name = (name[0] == '\0');
77
78         name = gtk_entry_get_text(GTK_ENTRY(scd->dialog_name_entry));
79
80         if (empty_name)
81                 {
82                 name = filename_from_path(fd->dest_path);
83                 }
84
85         bookmark_list_add(scd->bookmarks, name, fd->dest_path);
86
87         shortcuts_add_close(scd);
88 }
89
90 static void shortcuts_add_cancel_cb(FileDialog *, gpointer data)
91 {
92         auto scd = static_cast<ShortcutsData *>(data);
93
94         shortcuts_add_close(scd);
95 }
96
97 static void shortcuts_add_cb(GtkWidget *button, gpointer data)
98 {
99         auto scd = static_cast<ShortcutsData *>(data);
100         GtkWidget *hbox;
101         const gchar *title;
102
103         if (scd->dialog)
104                 {
105                 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(scd->dialog)->dialog));
106                 return;
107                 }
108
109         title = _("Add Shortcut");
110         scd->dialog = file_util_file_dlg(title,
111                                         "add_shortcuts", button,
112                                         shortcuts_add_cancel_cb, scd);
113         file_dialog_add_button(scd->dialog, GQ_ICON_OK, "OK", shortcuts_add_ok_cb, TRUE);
114
115         generic_dialog_add_message(GENERIC_DIALOG(scd->dialog), nullptr, title, nullptr, FALSE);
116
117         file_dialog_add_path_widgets(scd->dialog, nullptr, nullptr, "add_shortcuts", nullptr, nullptr);
118
119         hbox = pref_box_new(GENERIC_DIALOG(scd->dialog)->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
120
121         pref_label_new(hbox, _("Name:"));
122
123         scd->dialog_name_entry = gtk_entry_new();
124         gq_gtk_box_pack_start(GTK_BOX(hbox), scd->dialog_name_entry, TRUE, TRUE, 0);
125         generic_dialog_attach_default(GENERIC_DIALOG(scd->dialog), scd->dialog_name_entry);
126         gtk_widget_show(scd->dialog_name_entry);
127
128         gtk_widget_show(GENERIC_DIALOG(scd->dialog)->dialog);
129 }
130
131 static void shortcuts_destroy(GtkWidget *, gpointer data)
132 {
133         auto scd = static_cast<ShortcutsData *>(data);
134
135         shortcuts_add_close(scd);
136
137         g_free(scd);
138 }
139
140 static GtkWidget *shortcuts_new(LayoutWindow *lw)
141 {
142         ShortcutsData *scd;
143         GtkWidget *tbar;
144
145         if (!lw) return nullptr;
146
147         scd = g_new0(ShortcutsData, 1);
148
149         scd->lw = lw;
150
151         scd->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
152         g_object_set_data(G_OBJECT(scd->vbox), "shortcuts_data", scd);
153         g_signal_connect(G_OBJECT(scd->vbox), "destroy",
154                         G_CALLBACK(shortcuts_destroy), scd);
155
156         scd->bookmarks = bookmark_list_new(SHORTCUTS, shortcuts_bookmark_select, scd);
157         gq_gtk_box_pack_start(GTK_BOX(scd->vbox), scd->bookmarks, TRUE, TRUE, 0);
158         gtk_widget_show(scd->bookmarks);
159
160         tbar = pref_toolbar_new(scd->vbox);
161
162         scd->add_button = pref_toolbar_button(tbar, GQ_ICON_ADD, _("Add"), FALSE,
163                                         _("Add Shortcut"),
164                                         G_CALLBACK(shortcuts_add_cb), scd);
165
166         return scd->vbox;
167 }
168
169 GtkWidget *shortcuts_new_from_config(LayoutWindow *lw, const gchar **, const gchar **)
170 {
171         GtkWidget *shortcuts_bar;
172
173         shortcuts_bar = shortcuts_new(lw);
174         gtk_widget_show(shortcuts_bar);
175
176         return shortcuts_bar;
177 }
178
179 GtkWidget *shortcuts_new_default(LayoutWindow *lw)
180 {
181         return shortcuts_new_from_config(lw, nullptr, nullptr);
182 }
183
184 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */