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