ardour
editor_snapshots.cc
Go to the documentation of this file.
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"
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 using namespace ARDOUR_UI_UTILS;
36 
38  : EditorComponent (e)
39 {
40  _model = ListStore::create (_columns);
41  _display.set_model (_model);
42  _display.append_column (X_("snapshot"), _columns.visible_name);
43  _display.set_size_request (75, -1);
44  _display.set_headers_visible (false);
45  _display.set_reorderable (false);
46  _scroller.add (_display);
47  _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
48 
49  _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSnapshots::selection_changed));
50  _display.signal_button_press_event().connect (sigc::mem_fun (*this, &EditorSnapshots::button_press), false);
51 }
52 
53 void
55 {
56  SessionHandlePtr::set_session (s);
57 
58  redisplay ();
59 }
60 
63 void
65 {
66  if (_display.get_selection()->count_selected_rows() > 0) {
67 
68  TreeModel::iterator i = _display.get_selection()->get_selected();
69 
70  std::string snap_name = (*i)[_columns.real_name];
71 
72  if (snap_name.length() == 0) {
73  return;
74  }
75 
76  if (_session->snap_name() == snap_name) {
77  return;
78  }
79 
80  ARDOUR_UI::instance()->load_session (_session->path(), string (snap_name));
81  }
82 }
83 
84 bool
85 EditorSnapshots::button_press (GdkEventButton* ev)
86 {
87  if (ev->button == 3) {
88  /* Right-click on the snapshot list. Work out which snapshot it
89  was over. */
90  Gtk::TreeModel::Path path;
91  Gtk::TreeViewColumn* col;
92  int cx;
93  int cy;
94  _display.get_path_at_pos ((int) ev->x, (int) ev->y, path, col, cx, cy);
95  Gtk::TreeModel::iterator iter = _model->get_iter (path);
96  if (iter) {
97  Gtk::TreeModel::Row row = *iter;
98  popup_context_menu (ev->button, ev->time, row[_columns.real_name]);
99  }
100  return true;
101  }
102 
103  return false;
104 }
105 
106 
112 void
113 EditorSnapshots::popup_context_menu (int button, int32_t time, std::string snapshot_name)
114 {
115  using namespace Menu_Helpers;
116 
117  MenuList& items (_menu.items());
118  items.clear ();
119 
120  const bool modification_allowed = (_session->snap_name() != snapshot_name && _session->name() != snapshot_name);
121 
122  add_item_with_sensitivity (items, MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::remove), snapshot_name)), modification_allowed);
123 
124  add_item_with_sensitivity (items, MenuElem (_("Rename..."), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::rename), snapshot_name)), modification_allowed);
125 
126  _menu.popup (button, time);
127 }
128 
129 void
130 EditorSnapshots::rename (std::string old_name)
131 {
132  ArdourPrompter prompter(true);
133 
134  string new_name;
135 
136  prompter.set_name ("Prompter");
137  prompter.set_title (_("Rename Snapshot"));
138  prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
139  prompter.set_prompt (_("New name of snapshot"));
140  prompter.set_initial_text (old_name);
141 
142  if (prompter.run() == RESPONSE_ACCEPT) {
143  prompter.get_result (new_name);
144  if (new_name.length()) {
145  _session->rename_state (old_name, new_name);
146  redisplay ();
147  }
148  }
149 }
150 
151 
152 void
154 {
155  vector<string> choices;
156 
157  std::string prompt = string_compose (_("Do you really want to remove snapshot \"%1\" ?\n(which cannot be undone)"), name);
158 
159  choices.push_back (_("No, do nothing."));
160  choices.push_back (_("Yes, remove it."));
161 
162  Gtkmm2ext::Choice prompter (_("Remove snapshot"), prompt, choices);
163 
164  if (prompter.run () == 1) {
165  _session->remove_state (name);
166  redisplay ();
167  }
168 }
169 
170 void
172 {
173  if (_session == 0) {
174  return;
175  }
176 
177  vector<std::string> state_file_paths;
178 
180  state_file_paths);
181 
182  if (state_file_paths.empty()) {
183  return;
184  }
185 
186  vector<string> state_file_names (get_file_names_no_extension(state_file_paths));
187 
188  _model->clear ();
189 
190  for (vector<string>::iterator i = state_file_names.begin(); i != state_file_names.end(); ++i)
191  {
192  string statename = (*i);
193  TreeModel::Row row = *(_model->append());
194 
195  /* this lingers on in case we ever want to change the visible
196  name of the snapshot.
197  */
198 
199  string display_name;
200  display_name = statename;
201 
202  if (statename == _session->snap_name()) {
203  _display.get_selection()->select(row);
204  }
205 
206  row[_columns.visible_name] = display_name;
207  row[_columns.real_name] = statename;
208  }
209 }
210 
Gtk::TreeView _display
void rename(std::string)
const std::string root_path() const
void popup_context_menu(int, int32_t, std::string)
Definition: ardour_ui.h:130
static ARDOUR_UI * instance()
Definition: ardour_ui.h:187
void remove(std::string)
std::string name() const
Definition: session.h:166
Definition: Beats.hpp:239
void add_item_with_sensitivity(Gtk::Menu_Helpers::MenuList &, Gtk::Menu_Helpers::MenuElem, bool)
void set_initial_text(std::string txt)
Definition: prompter.h:50
void remove_state(std::string snapshot_name)
#define _(Text)
Definition: i18n.h:11
int load_session(const std::string &path, const std::string &snapshot, std::string mix_template=std::string())
Definition: ardour_ui.cc:3096
#define X_(Text)
Definition: i18n.h:13
void set_prompt(std::string prompt)
Definition: prompter.h:46
Gtk::TreeModelColumn< std::string > real_name
Glib::RefPtr< Gtk::ListStore > _model
Definition: amp.h:29
std::string path() const
Definition: session.h:165
LIBARDOUR_API std::vector< std::string > get_file_names_no_extension(const std::vector< std::string > &file_paths)
void get_result(std::string &str, bool strip=true)
Definition: prompter.cc:104
std::string snap_name() const
Definition: session.h:167
const char * name
Definition: editor.h:134
EditorSnapshots(Editor *)
LIBARDOUR_API void get_state_files_in_directory(const std::string &directory_path, std::vector< std::string > &result)
Definition: debug.h:30
Gtk::ScrolledWindow _scroller
void set_session(ARDOUR::Session *)
bool button_press(GdkEventButton *)
const SessionDirectory & session_directory() const
Definition: session.h:182
ARDOUR::Session * _session
void rename_state(std::string old_name, std::string new_name)
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
Gtk::TreeModelColumn< std::string > visible_name