cleanup up cleanup at session destruction; clarify the meaning of 3 signals (DropRefe...
[ardour.git] / gtk2_ardour / editor_selection_list.cc
1 /*
2     Copyright (C) 2000 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 <cstdlib>
21 #include <cmath>
22 #include <vector>
23
24 #include <gtkmm.h>
25
26 #include "ardour/named_selection.h"
27 #include "ardour/session_selection.h"
28 #include "ardour/playlist.h"
29
30 #include <gtkmm2ext/stop_signal.h>
31
32 #include "editor.h"
33 #include "keyboard.h"
34 #include "selection.h"
35 #include "time_axis_view.h"
36 #include "ardour_ui.h"
37 #include "prompter.h"
38
39 #include "i18n.h"
40
41 using namespace std;
42 using namespace ARDOUR;
43 using namespace PBD;
44 using namespace Gtk;
45 using namespace Gtkmm2ext;
46
47 void
48 Editor::handle_new_named_selection ()
49 {
50         ARDOUR_UI::instance()->call_slot (boost::bind (&Editor::redisplay_named_selections, this));
51 }
52
53 void
54 Editor::add_named_selection_to_named_selection_display (boost::shared_ptr<NamedSelection> selection)
55 {
56         TreeModel::Row row = *(named_selection_model->append());
57         row[named_selection_columns.text] = selection.name;
58         row[named_selection_columns.selection] = selection;
59 }
60
61 void
62 Editor::redisplay_named_selections ()
63 {
64         named_selection_model->clear ();
65         session->foreach_named_selection (*this, &Editor::add_named_selection_to_named_selection_display);
66 }
67
68 bool
69 Editor::named_selection_display_key_release (GdkEventKey* ev)
70 {
71         if (session == 0) {
72                 return true;
73         }
74
75         switch (ev->keyval) {
76         case GDK_Delete:
77                 remove_selected_named_selections ();
78                 return true;
79                 break;
80         default:
81                 return false;
82                 break;
83         }
84
85 }
86
87 void
88 Editor::remove_selected_named_selections ()
89 {
90         Glib::RefPtr<TreeSelection> selection = named_selection_display.get_selection();
91         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
92
93         if (selection->count_selected_rows() == 0) {
94                 return;
95         }
96
97         for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
98
99                 TreeIter iter;
100
101                 if ((iter = named_selection_model->get_iter (*i))) {
102                         session->remove_named_selection ((*iter)[named_selection_columns.selection]);
103                 }
104         }
105 }
106
107 bool
108 Editor::named_selection_display_button_release (GdkEventButton *ev)
109 {
110         TreeModel::Children rows = named_selection_model->children();
111         TreeModel::Children::iterator i;
112         Glib::RefPtr<TreeSelection> selection = named_selection_display.get_selection();
113
114         for (i = rows.begin(); i != rows.end(); ++i) {
115                 if (selection->is_selected (i)) {
116                         switch (ev->button) {
117                         case 1:
118                                 if (Keyboard::is_delete_event (ev)) {
119                                         session->remove_named_selection ((*i)[named_selection_columns.selection]);
120                                         return true;
121                                 }
122                                 break;
123                         case 2:
124                                 break;
125                         case 3:
126                                 break;
127                         default:
128                                 break;
129                         }
130                 }
131         }
132
133         return false;
134 }
135
136
137 void
138 Editor::named_selection_display_selection_changed ()
139 {
140 }
141
142 void
143 Editor::create_named_selection ()
144 {
145         string name;
146
147         if (session == 0) {
148                 return;
149         }
150
151         /* check for a range-based selection */
152
153         if (selection->time.empty()) {
154                 return;
155         }
156
157         TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
158
159         if (views->empty()) {
160                 delete views;
161                 return;
162         }
163
164         boost::shared_ptr<Playlist>        what_we_found;
165         list<boost::shared_ptr<Playlist> > thelist;
166
167         for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
168
169                 boost::shared_ptr<Playlist> pl = (*i)->playlist();
170
171                 if (pl && (what_we_found = pl->copy (selection->time, false)) != 0) {
172                         thelist.push_back (what_we_found);
173                 }
174         }
175
176         if (!thelist.empty()) {
177
178                 ArdourPrompter p;
179
180                 p.set_prompt (_("Name for Chunk:"));
181                 p.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
182                 p.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
183                 p.change_labels (_("Create Chunk"), _("Forget it"));
184                 p.show_all ();
185
186                 switch (p.run ()) {
187
188                 case Gtk::RESPONSE_ACCEPT:
189                         p.get_result (name);
190                         if (name.empty()) {
191                                 return;
192                         }
193                         break;
194                 default:
195                         return;
196                 }
197
198                 boost::shared_ptr<NamedSelection> ns (new NamedSelection (name, thelist));
199                 
200                 /* make the one we just added be selected */
201
202                 TreeModel::Children::iterator added = named_selection_model->children().end();
203                 --added;
204                 named_selection_display.get_selection()->select (*added);
205
206         } else {
207                 error << _("No selectable material found in the currently selected time range") << endmsg;
208         }
209 }
210