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