make all use of bind/mem_fun be explicitly sigc::
[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 "editor.h"
29 #include "utils.h"
30 #include "prompter.h"
31
32 using namespace std;
33 using namespace sigc;
34 using namespace PBD;
35 using namespace Gtk;
36 using namespace ARDOUR;
37
38 EditorSnapshots::EditorSnapshots (Editor* e)
39         : EditorComponent (e)
40 {
41         _model = ListStore::create (_columns);
42         _display.set_model (_model);
43         _display.append_column (X_("snapshot"), _columns.visible_name);
44         _display.set_name ("SnapshotDisplay");
45         _display.set_size_request (75, -1);
46         _display.set_headers_visible (false);
47         _display.set_reorderable (false);
48         _scroller.add (_display);
49         _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
50
51         _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSnapshots::selection_changed));
52         _display.signal_button_press_event().connect (sigc::mem_fun (*this, &EditorSnapshots::button_press), false);
53 }
54
55 void
56 EditorSnapshots::connect_to_session (Session* s)
57 {
58         EditorComponent::connect_to_session (s);
59
60         redisplay ();
61 }
62
63 /** A new snapshot has been selected.
64  */
65 void
66 EditorSnapshots::selection_changed ()
67 {
68         if (_display.get_selection()->count_selected_rows() > 0) {
69
70                 TreeModel::iterator i = _display.get_selection()->get_selected();
71
72                 Glib::ustring snap_name = (*i)[_columns.real_name];
73
74                 if (snap_name.length() == 0) {
75                         return;
76                 }
77
78                 if (_session->snap_name() == snap_name) {
79                         return;
80                 }
81
82                 ARDOUR_UI::instance()->load_session (_session->path(), string (snap_name));
83         }
84 }
85
86 bool
87 EditorSnapshots::button_press (GdkEventButton* ev)
88 {
89         if (ev->button == 3) {
90                 /* Right-click on the snapshot list. Work out which snapshot it
91                    was over. */
92                 Gtk::TreeModel::Path path;
93                 Gtk::TreeViewColumn* col;
94                 int cx;
95                 int cy;
96                 _display.get_path_at_pos ((int) ev->x, (int) ev->y, path, col, cx, cy);
97                 Gtk::TreeModel::iterator iter = _model->get_iter (path);
98                 if (iter) {
99                         Gtk::TreeModel::Row row = *iter;
100                         popup_context_menu (ev->button, ev->time, row[_columns.real_name]);
101                 }
102                 return true;
103         }
104
105         return false;
106 }
107
108
109 /** Pop up the snapshot display context menu.
110  * @param button Button used to open the menu.
111  * @param time Menu open time.
112  * @snapshot_name Name of the snapshot that the menu click was over.
113  */
114 void
115 EditorSnapshots::popup_context_menu (int button, int32_t time, Glib::ustring snapshot_name)
116 {
117         using namespace Menu_Helpers;
118
119         MenuList& items (_menu.items());
120         items.clear ();
121
122         const bool modification_allowed = (_session->snap_name() != snapshot_name && _session->name() != snapshot_name);
123
124         add_item_with_sensitivity (items, MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::remove), snapshot_name)), modification_allowed);
125
126         add_item_with_sensitivity (items, MenuElem (_("Rename"), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::rename), snapshot_name)), modification_allowed);
127
128         _menu.popup (button, time);
129 }
130
131 void
132 EditorSnapshots::rename (Glib::ustring old_name)
133 {
134         ArdourPrompter prompter(true);
135
136         string new_name;
137
138         prompter.set_name ("Prompter");
139         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
140         prompter.set_prompt (_("New name of snapshot"));
141         prompter.set_initial_text (old_name);
142
143         if (prompter.run() == RESPONSE_ACCEPT) {
144                 prompter.get_result (new_name);
145                 if (new_name.length()) {
146                         _session->rename_state (old_name, new_name);
147                         redisplay ();
148                 }
149         }
150 }
151
152
153 void
154 EditorSnapshots::remove (Glib::ustring name)
155 {
156         vector<string> choices;
157
158         std::string prompt = string_compose (_("Do you really want to remove snapshot \"%1\" ?\n(cannot be undone)"), name);
159
160         choices.push_back (_("No, do nothing."));
161         choices.push_back (_("Yes, remove it."));
162
163         Gtkmm2ext::Choice prompter (prompt, choices);
164
165         if (prompter.run () == 1) {
166                 _session->remove_state (name);
167                 redisplay ();
168         }
169 }
170
171 void
172 EditorSnapshots::redisplay ()
173 {
174         if (_session == 0) {
175                 return;
176         }
177
178         vector<sys::path> state_file_paths;
179
180         get_state_files_in_directory (_session->session_directory().root_path(),
181                                       state_file_paths);
182
183         if (state_file_paths.empty()) {
184                 return;
185         }
186
187         vector<string> state_file_names (get_file_names_no_extension(state_file_paths));
188
189         _model->clear ();
190
191         for (vector<string>::iterator i = state_file_names.begin(); i != state_file_names.end(); ++i)
192         {
193                 string statename = (*i);
194                 TreeModel::Row row = *(_model->append());
195
196                 /* this lingers on in case we ever want to change the visible
197                    name of the snapshot.
198                 */
199
200                 string display_name;
201                 display_name = statename;
202
203                 if (statename == _session->snap_name()) {
204                         _display.get_selection()->select(row);
205                 }
206
207                 row[_columns.visible_name] = display_name;
208                 row[_columns.real_name] = statename;
209         }
210 }
211