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