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>
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.
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.
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.
25 #include "pbd/gstdio_compat.h"
28 #include <glibmm/datetime.h>
30 #include <gtkmm/liststore.h>
32 #include "ardour/filename_extensions.h"
33 #include "ardour/session.h"
34 #include "ardour/session_state_utils.h"
35 #include "ardour/session_directory.h"
37 #include "widgets/choice.h"
38 #include "widgets/prompter.h"
40 #include "editor_snapshots.h"
41 #include "ardour_ui.h"
49 using namespace ARDOUR;
50 using namespace ARDOUR_UI_UTILS;
52 EditorSnapshots::EditorSnapshots (Editor* e)
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);
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);
70 EditorSnapshots::set_session (Session* s)
72 SessionHandlePtr::set_session (s);
77 /* A new snapshot has been selected. */
79 EditorSnapshots::selection_changed ()
81 if (_snapshot_display.get_selection()->count_selected_rows() == 0) {
85 TreeModel::iterator i = _snapshot_display.get_selection()->get_selected();
87 std::string snap_name = (*i)[_columns.real_name];
89 if (snap_name.length() == 0) {
93 if (_session->snap_name() == snap_name) {
97 _snapshot_display.set_sensitive (false);
98 ARDOUR_UI::instance()->load_session (_session->path(), string (snap_name));
99 _snapshot_display.set_sensitive (true);
103 EditorSnapshots::button_press (GdkEventButton* ev)
105 if (ev->button == 3) {
106 /* Right-click on the snapshot list.
107 * Work out which snapshot it was over.
109 Gtk::TreeModel::Path path;
110 Gtk::TreeViewColumn* col;
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);
116 Gtk::TreeModel::Row row = *iter;
117 popup_context_menu (ev->button, ev->time, row[_columns.real_name]);
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.
131 EditorSnapshots::popup_context_menu (int button, int32_t time, std::string snapshot_name)
133 using namespace Menu_Helpers;
135 MenuList& items (_menu.items());
138 const bool modification_allowed = (_session->snap_name() != snapshot_name && _session->name() != snapshot_name);
140 add_item_with_sensitivity (items, MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::remove), snapshot_name)), modification_allowed);
142 add_item_with_sensitivity (items, MenuElem (_("Rename..."), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::rename), snapshot_name)), modification_allowed);
144 _menu.popup (button, time);
148 EditorSnapshots::rename (std::string old_name)
150 ArdourWidgets::Prompter prompter(true);
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);
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);
171 EditorSnapshots::remove (std::string name)
173 vector<string> choices;
175 std::string prompt = string_compose (_("Do you really want to remove snapshot \"%1\" ?\n(which cannot be undone)"), name);
177 choices.push_back (_("No, do nothing."));
178 choices.push_back (_("Yes, remove it."));
180 ArdourWidgets::Choice prompter (_("Remove snapshot"), prompt, choices);
182 if (prompter.run () == 1) {
183 _session->remove_state (name);
189 EditorSnapshots::redisplay ()
195 vector<std::string> state_file_paths;
197 get_state_files_in_directory (_session->session_directory().root_path(),
200 if (state_file_paths.empty()) {
204 vector<string> state_file_names (get_file_names_no_extension(state_file_paths));
206 _snapshot_model->clear ();
208 for (vector<string>::iterator i = state_file_names.begin(); i != state_file_names.end(); ++i)
210 string statename = (*i);
211 TreeModel::Row row = *(_snapshot_model->append());
213 /* this lingers on in case we ever want to change the visible
214 name of the snapshot.
218 display_name = statename;
220 if (statename == _session->snap_name()) {
221 _snapshot_display.get_selection()->select(row);
224 std::string s = Glib::build_filename (_session->path(), statename + ARDOUR::statefile_suffix);
227 g_stat (s.c_str(), &gsb);
228 Glib::DateTime gdt(Glib::DateTime::create_now_local (gsb.st_mtime));
230 row[_columns.visible_name] = display_name;
231 row[_columns.real_name] = statename;
232 row[_columns.time_formatted] = gdt.format ("%F %H:%M");