Initial import of gtk2_ardour.
[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 <gtk--.h>
26
27 #include <ardour/named_selection.h>
28 #include <ardour/session_selection.h>
29 #include <ardour/playlist.h>
30
31 #include <gtkmmext/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 Gtkmmext;
45
46 void
47 Editor::handle_new_named_selection ()
48 {
49         ARDOUR_UI::instance()->call_slot (slot (*this, &Editor::redisplay_named_selections));
50 }
51
52 void
53 Editor::add_named_selection_to_named_selection_display (NamedSelection& selection)
54 {
55         const gchar *row[1];
56
57         row[0] = selection.name.c_str();
58         named_selection_display.rows().push_back (row);
59         named_selection_display.rows().back().set_data (&selection);
60 }
61
62 void
63 Editor::redisplay_named_selections ()
64 {
65         named_selection_display.freeze ();
66         named_selection_display.clear ();
67         session->foreach_named_selection (*this, &Editor::add_named_selection_to_named_selection_display);
68         named_selection_display.thaw ();
69 }
70
71 gint
72 Editor::named_selection_display_button_press (GdkEventButton *ev)
73 {
74         NamedSelection* named_selection;
75         gint row;
76         gint col;
77
78         switch (ev->button) {
79         case 1:
80                 if (Keyboard::is_delete_event (ev)) {
81                         if (named_selection_display.get_selection_info ((int)ev->x, (int)ev->y, &row, &col) != 0) {
82                                 if ((named_selection = reinterpret_cast<NamedSelection *> (named_selection_display.get_row_data (row))) != 0) {
83                                         session->remove_named_selection (named_selection);
84                                         return stop_signal (named_selection_display, "button_press_event");
85                                 }
86                         }
87                 } 
88                 break;
89
90         case 2:
91                 break;
92
93         case 3:
94                 break;
95         default:
96                 break;
97         }
98         return FALSE;
99 }
100
101
102 void
103 Editor::named_selection_display_selected (gint row, gint col, GdkEvent *ev)
104 {
105 }
106
107 void
108 Editor::named_selection_display_unselected (gint row, gint col, GdkEvent *ev)
109 {
110 }
111
112 void
113 Editor::name_selection ()
114 {
115         ArdourPrompter p;
116
117         p.set_prompt (_("name for chunk:"));
118         p.done.connect (slot (*this, &Editor::named_selection_name_chosen));
119         p.change_labels (_("Create chunk"), _("Forget it"));
120         p.show_all ();
121
122         Gtk::Main::run ();
123
124         if (p.status == Prompter::entered) {
125                 string name;
126                 p.get_result (name);
127                 
128                 if (name.length()){
129                         create_named_selection (name);
130                 }
131         }
132 }
133
134 void
135 Editor::named_selection_name_chosen ()
136 {
137         Gtk::Main::quit ();
138 }
139
140 void
141 Editor::create_named_selection (string name)
142 {
143         if (session == 0) {
144                 return;
145         }
146
147         /* check for a range-based selection */
148
149         if (selection->time.empty()) {
150                 return;
151         }
152
153         
154         TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
155
156         if (views->empty()) {
157                 delete views;
158                 return;
159         }
160
161         Playlist*       what_we_found;
162         list<Playlist*> thelist;
163
164         for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
165                 
166                 Playlist *pl = (*i)->playlist();
167                 
168                 if (pl) {
169                         
170                         if ((what_we_found = pl->copy (selection->time, false)) != 0) {
171
172                                 thelist.push_back (what_we_found);
173                         }
174                 }
175         }
176
177         NamedSelection* ns;
178
179         ns = new NamedSelection (name, thelist);
180
181         /* make the one we just added be selected */
182
183         named_selection_display.rows().back().select ();
184 }
185