fix required library versions; fix use of shared_ptr by redirect_box; fix double...
[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     $Id$
19 */
20
21 #include <cstdlib>
22 #include <cmath>
23 #include <vector>
24
25 #include <gtkmm.h>
26
27 #include <ardour/named_selection.h>
28 #include <ardour/session_selection.h>
29 #include <ardour/playlist.h>
30
31 #include <gtkmm2ext/stop_signal.h>
32
33 #include "editor.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 sigc;
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 (mem_fun(*this, &Editor::redisplay_named_selections));
51 }
52
53 void
54 Editor::add_named_selection_to_named_selection_display (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 gint
69 Editor::named_selection_display_button_press (GdkEventButton *ev)
70 {
71
72         TreeModel::Children rows = named_selection_model->children();
73         TreeModel::Children::iterator i;
74         Glib::RefPtr<TreeSelection> selection = named_selection_display.get_selection();
75
76         for (i = rows.begin(); i != rows.end(); ++i) {
77                 if (selection->is_selected (i)) {
78                         switch (ev->button) {
79                         case 1:
80                                 if (Keyboard::is_delete_event (ev)) {
81                                         session->remove_named_selection ((*i)[named_selection_columns.selection]);
82                                         return true;
83                                 }
84                                 break;
85                         case 2:
86                                 break;
87                         case 3:
88                                 break;
89                         default:
90                                 break;
91                         }
92                 }
93         }
94         return FALSE;
95 }
96
97
98 void
99 Editor::named_selection_display_selection_changed ()
100 {
101 }
102
103 void
104 Editor::name_selection ()
105 {
106         ArdourPrompter p;
107
108         p.set_prompt (_("Name for Chunk:"));
109         p.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
110         p.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
111         p.change_labels (_("Create Chunk"), _("Forget it"));
112         p.show_all ();
113
114         switch (p.run ()) {
115         case Gtk::RESPONSE_ACCEPT:
116           string name;
117                 p.get_result (name);
118                 if (name.length()) {
119                   create_named_selection (name);
120                 }       
121                 break;
122         }
123
124 }
125
126 void
127 Editor::named_selection_name_chosen ()
128 {
129         Gtk::Main::quit ();
130 }
131
132 void
133 Editor::create_named_selection (const string & name)
134 {
135         if (session == 0) {
136                 return;
137         }
138
139         /* check for a range-based selection */
140
141         if (selection->time.empty()) {
142                 return;
143         }
144
145         
146         TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
147
148         if (views->empty()) {
149                 delete views;
150                 return;
151         }
152
153         Playlist*       what_we_found;
154         list<Playlist*> thelist;
155
156         for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
157                 
158                 Playlist *pl = (*i)->playlist();
159                 
160                 if (pl) {
161                         
162                         if ((what_we_found = pl->copy (selection->time, false)) != 0) {
163
164                                 thelist.push_back (what_we_found);
165                         }
166                 }
167         }
168
169         NamedSelection* ns;
170         TreeModel::Row row = *(named_selection_model->append());
171
172         ns = new NamedSelection (name, thelist);
173         row[named_selection_columns.selection] = ns;
174         row[named_selection_columns.text] = name;
175
176         /* make the one we just added be selected */
177
178         named_selection_display.get_selection()->select (row);
179
180 }
181