Vkeybd: use ArdourWidgets for all GUI elements
[ardour.git] / gtk2_ardour / editor_snapshots.cc
1 /*
2  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23
24 #include <glib.h>
25 #include "pbd/gstdio_compat.h"
26
27 #include <glibmm.h>
28 #include <glibmm/datetime.h>
29
30 #include <gtkmm/liststore.h>
31
32 #include "ardour/filename_extensions.h"
33 #include "ardour/session.h"
34 #include "ardour/session_state_utils.h"
35 #include "ardour/session_directory.h"
36
37 #include "widgets/choice.h"
38 #include "widgets/prompter.h"
39
40 #include "editor_snapshots.h"
41 #include "ardour_ui.h"
42 #include "utils.h"
43
44 #include "pbd/i18n.h"
45
46 using namespace std;
47 using namespace PBD;
48 using namespace Gtk;
49 using namespace ARDOUR;
50 using namespace ARDOUR_UI_UTILS;
51
52 EditorSnapshots::EditorSnapshots (Editor* e)
53         : EditorComponent (e)
54 {
55         _snapshot_model = ListStore::create (_columns);
56         _snapshot_display.set_model (_snapshot_model);
57         _snapshot_display.append_column (_("Snapshot (click to load)"), _columns.visible_name);
58         _snapshot_display.append_column (_("Modified Date"), _columns.time_formatted);
59         _snapshot_display.set_size_request (75, -1);
60         _snapshot_display.set_headers_visible (true);
61         _snapshot_display.set_reorderable (false);
62         _scroller.add (_snapshot_display);
63         _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
64
65         _snapshot_display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSnapshots::selection_changed));
66         _snapshot_display.signal_button_press_event().connect (sigc::mem_fun (*this, &EditorSnapshots::button_press), false);
67 }
68
69 void
70 EditorSnapshots::set_session (Session* s)
71 {
72         SessionHandlePtr::set_session (s);
73
74         redisplay ();
75 }
76
77 /* A new snapshot has been selected. */
78 void
79 EditorSnapshots::selection_changed ()
80 {
81         if (_snapshot_display.get_selection()->count_selected_rows() == 0) {
82                 return;
83         }
84
85         TreeModel::iterator i = _snapshot_display.get_selection()->get_selected();
86
87         std::string snap_name = (*i)[_columns.real_name];
88
89         if (snap_name.length() == 0) {
90                 return;
91         }
92
93         if (_session->snap_name() == snap_name) {
94                 return;
95         }
96
97         _snapshot_display.set_sensitive (false);
98         ARDOUR_UI::instance()->load_session (_session->path(), string (snap_name));
99         _snapshot_display.set_sensitive (true);
100 }
101
102 bool
103 EditorSnapshots::button_press (GdkEventButton* ev)
104 {
105         if (ev->button == 3) {
106                 /* Right-click on the snapshot list.
107                  * Work out which snapshot it was over.
108                  */
109                 Gtk::TreeModel::Path path;
110                 Gtk::TreeViewColumn* col;
111                 int cx;
112                 int cy;
113                 _snapshot_display.get_path_at_pos ((int) ev->x, (int) ev->y, path, col, cx, cy);
114                 Gtk::TreeModel::iterator iter = _snapshot_model->get_iter (path);
115                 if (iter) {
116                         Gtk::TreeModel::Row row = *iter;
117                         popup_context_menu (ev->button, ev->time, row[_columns.real_name]);
118                 }
119                 return true;
120         }
121         return false;
122 }
123
124
125 /** Pop up the snapshot display context menu.
126  * @param button Button used to open the menu.
127  * @param time Menu open time.
128  * @param snapshot_name Name of the snapshot that the menu click was over.
129  */
130 void
131 EditorSnapshots::popup_context_menu (int button, int32_t time, std::string snapshot_name)
132 {
133         using namespace Menu_Helpers;
134
135         MenuList& items (_menu.items());
136         items.clear ();
137
138         const bool modification_allowed = (_session->snap_name() != snapshot_name && _session->name() != snapshot_name);
139
140         add_item_with_sensitivity (items, MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::remove), snapshot_name)), modification_allowed);
141
142         add_item_with_sensitivity (items, MenuElem (_("Rename..."), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::rename), snapshot_name)), modification_allowed);
143
144         _menu.popup (button, time);
145 }
146
147 void
148 EditorSnapshots::rename (std::string old_name)
149 {
150         ArdourWidgets::Prompter prompter(true);
151
152         string new_name;
153
154         prompter.set_name ("Prompter");
155         prompter.set_title (_("Rename Snapshot"));
156         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
157         prompter.set_prompt (_("New name of snapshot"));
158         prompter.set_initial_text (old_name);
159
160         if (prompter.run() == RESPONSE_ACCEPT) {
161                 prompter.get_result (new_name);
162                 if (new_name.length()) {
163                         _session->rename_state (old_name, new_name);
164                         redisplay ();
165                 }
166         }
167 }
168
169
170 void
171 EditorSnapshots::remove (std::string name)
172 {
173         vector<string> choices;
174
175         std::string prompt = string_compose (_("Do you really want to remove snapshot \"%1\" ?\n(which cannot be undone)"), name);
176
177         choices.push_back (_("No, do nothing."));
178         choices.push_back (_("Yes, remove it."));
179
180         ArdourWidgets::Choice prompter (_("Remove snapshot"), prompt, choices);
181
182         if (prompter.run () == 1) {
183                 _session->remove_state (name);
184                 redisplay ();
185         }
186 }
187
188 void
189 EditorSnapshots::redisplay ()
190 {
191         if (_session == 0) {
192                 return;
193         }
194
195         vector<std::string> state_file_paths;
196
197         get_state_files_in_directory (_session->session_directory().root_path(),
198                                       state_file_paths);
199
200         if (state_file_paths.empty()) {
201                 return;
202         }
203
204         vector<string> state_file_names (get_file_names_no_extension(state_file_paths));
205
206         _snapshot_model->clear ();
207
208         for (vector<string>::iterator i = state_file_names.begin(); i != state_file_names.end(); ++i)
209         {
210                 string statename = (*i);
211                 TreeModel::Row row = *(_snapshot_model->append());
212
213                 /* this lingers on in case we ever want to change the visible
214                    name of the snapshot.
215                 */
216
217                 string display_name;
218                 display_name = statename;
219
220                 if (statename == _session->snap_name()) {
221                         _snapshot_display.get_selection()->select(row);
222                 }
223
224                 std::string s = Glib::build_filename (_session->path(), statename + ARDOUR::statefile_suffix);
225
226                 GStatBuf gsb;
227                 g_stat (s.c_str(), &gsb);
228                 Glib::DateTime gdt(Glib::DateTime::create_now_local (gsb.st_mtime));
229
230                 row[_columns.visible_name] = display_name;
231                 row[_columns.real_name] = statename;
232                 row[_columns.time_formatted] = gdt.format ("%F %H:%M");
233         }
234 }
235