r80@gandalf: fugalh | 2006-06-22 16:37:01 -0600
[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 Gtk;
44 using namespace Gtkmm2ext;
45
46 void
47 Editor::handle_new_named_selection ()
48 {
49         ARDOUR_UI::instance()->call_slot (mem_fun(*this, &Editor::redisplay_named_selections));
50 }
51
52 void
53 Editor::add_named_selection_to_named_selection_display (NamedSelection& selection)
54 {
55         TreeModel::Row row = *(named_selection_model->append());
56         row[named_selection_columns.text] = selection.name;
57         row[named_selection_columns.selection] = &selection;
58 }
59
60 void
61 Editor::redisplay_named_selections ()
62 {
63         named_selection_model->clear ();
64         session->foreach_named_selection (*this, &Editor::add_named_selection_to_named_selection_display);
65 }
66
67 gint
68 Editor::named_selection_display_button_press (GdkEventButton *ev)
69 {
70
71         TreeModel::Children rows = named_selection_model->children();
72         TreeModel::Children::iterator i;
73         Glib::RefPtr<TreeSelection> selection = named_selection_display.get_selection();
74
75         for (i = rows.begin(); i != rows.end(); ++i) {
76                 if (selection->is_selected (i)) {
77                         switch (ev->button) {
78                         case 1:
79                                 if (Keyboard::is_delete_event (ev)) {
80                                         session->remove_named_selection ((*i)[named_selection_columns.selection]);
81                                         return stop_signal (named_selection_display, "button_press_event");
82                                 }
83                                 break;
84                         case 2:
85                                 break;
86                         case 3:
87                                 break;
88                         default:
89                                 break;
90                         }
91                 }
92         }
93         return FALSE;
94 }
95
96
97 void
98 Editor::named_selection_display_selection_changed ()
99 {
100 }
101
102 void
103 Editor::name_selection ()
104 {
105         ArdourPrompter p;
106
107         p.set_prompt (_("Name for Chunk:"));
108         p.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
109         p.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
110         p.change_labels (_("Create Chunk"), _("Forget it"));
111         p.show_all ();
112
113         switch (p.run ()) {
114         case Gtk::RESPONSE_ACCEPT:
115           string name;
116                 p.get_result (name);
117                 if (name.length()) {
118                   create_named_selection (name);
119                 }       
120                 break;
121         }
122
123 }
124
125 void
126 Editor::named_selection_name_chosen ()
127 {
128         Gtk::Main::quit ();
129 }
130
131 void
132 Editor::create_named_selection (const string & name)
133 {
134         if (session == 0) {
135                 return;
136         }
137
138         /* check for a range-based selection */
139
140         if (selection->time.empty()) {
141                 return;
142         }
143
144         
145         TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
146
147         if (views->empty()) {
148                 delete views;
149                 return;
150         }
151
152         Playlist*       what_we_found;
153         list<Playlist*> thelist;
154
155         for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
156                 
157                 Playlist *pl = (*i)->playlist();
158                 
159                 if (pl) {
160                         
161                         if ((what_we_found = pl->copy (selection->time, false)) != 0) {
162
163                                 thelist.push_back (what_we_found);
164                         }
165                 }
166         }
167
168         NamedSelection* ns;
169         TreeModel::Row row = *(named_selection_model->append());
170
171         ns = new NamedSelection (name, thelist);
172         row[named_selection_columns.selection] = ns;
173         row[named_selection_columns.text] = name;
174
175         /* make the one we just added be selected */
176
177         named_selection_display.get_selection()->select (row);
178
179 }
180