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