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