more miscellaneous fixes to make things even nicer
[ardour.git] / gtk2_ardour / playlist_selector.cc
1 /*
2     Copyright (C) 2004 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
22 #include <gtkmm/button.h>
23
24 #include <ardour/session_playlist.h>
25 #include <ardour/diskstream.h>
26 #include <ardour/playlist.h>
27 #include <ardour/audio_track.h>
28 #include <ardour/audioplaylist.h>
29 #include <ardour/configuration.h>
30
31 #include <gtkmm2ext/gtk_ui.h>
32
33 #include "playlist_selector.h"
34 #include "route_ui.h"
35 #include "gui_thread.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace sigc;
41 using namespace Gtk;
42 using namespace ARDOUR;
43
44 PlaylistSelector::PlaylistSelector ()
45         : ArdourDialog ("playlist selector")
46 {
47         rui = 0;
48         
49         set_position (WIN_POS_MOUSE);
50         set_name ("PlaylistSelectorWindow");
51         set_title (_("ardour: playlists"));
52         set_modal(true);
53         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
54         set_size_request (300, 200);
55
56         model = TreeStore::create (columns);
57         tree.set_model (model);
58         tree.append_column (_("Playlists grouped by track"), columns.text);
59
60         scroller.add (tree);
61         scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
62
63         // GTK2FIX do we need this stuff or is GTK applying some policy now?
64         //set_border_width (6);
65         // set_spacing (12);
66
67         get_vbox()->pack_start (scroller);
68
69         Button* b = add_button (_("close"), RESPONSE_CANCEL);
70         b->signal_clicked().connect (mem_fun(*this, &PlaylistSelector::close_button_click));
71
72 }
73
74 PlaylistSelector::~PlaylistSelector ()
75 {
76         clear_map ();
77 }
78
79 void
80 PlaylistSelector::clear_map ()
81 {
82         for (DSPL_Map::iterator x = dspl_map.begin(); x != dspl_map.end(); ++x) {
83                 delete x->second;
84         }
85         dspl_map.clear ();
86 }
87
88 void
89 PlaylistSelector::show_for (RouteUI* ruix)
90 {
91         vector<const char*> item;
92         DiskStream* this_ds;
93         string str;
94
95         rui = ruix;
96
97         str = _("ardour: playlist for ");
98         str += rui->route().name();
99
100         set_title (str);
101
102         clear_map ();
103         select_connection.disconnect ();
104
105         model->clear ();
106         
107         session->foreach_playlist (this, &PlaylistSelector::add_playlist_to_map);
108
109         this_ds = rui->get_diskstream();
110
111         TreeModel::Row others = *(model->append ());
112
113         others[columns.text] = _("Other tracks");
114         others[columns.playlist] = 0;
115         
116         for (DSPL_Map::iterator x = dspl_map.begin(); x != dspl_map.end(); ++x) {
117
118                 DiskStream* ds = session->diskstream_by_id (x->first);
119
120                 if (ds == 0) {
121                         continue;
122                 }
123
124                 /* add a node for the diskstream */
125
126                 string nodename;
127
128                 if (ds->name().empty()) {
129                         nodename = _("unassigned");
130                 } else {
131                         nodename = ds->name().c_str();
132                 }
133                 
134                 TreeModel::Row row;
135                 TreeModel::Row* selected_row = 0;
136                 TreePath this_path;
137
138                 if (ds == this_ds) {
139                         row = *(model->prepend());
140                         row[columns.text] = nodename;
141                         row[columns.playlist] = 0;
142                 } else {
143                         row = *(model->append (others.children()));
144                         row[columns.text] = nodename;
145                         row[columns.playlist] = 0;
146                 }
147
148                 /* Now insert all the playlists for this diskstream/track in a subtree */
149                 
150                 list<Playlist*> *pls = x->second;
151                 
152                 for (list<Playlist*>::iterator p = pls->begin(); p != pls->end(); ++p) {
153
154                         TreeModel::Row child_row;
155
156                         child_row = *(model->append (row.children()));
157                         child_row[columns.text] = (*p)->name();
158                         child_row[columns.playlist] = *p;
159
160                         if (*p == this_ds->playlist()) {
161                                 selected_row = &child_row;
162                         } 
163                 }
164                 
165                 if (selected_row != 0) {
166                         tree.get_selection()->select (*selected_row);
167                 }
168         }
169
170         show_all ();
171         select_connection = tree.get_selection()->signal_changed().connect (mem_fun(*this, &PlaylistSelector::selection_changed));
172 }
173
174 void
175 PlaylistSelector::add_playlist_to_map (Playlist *pl)
176 {
177         AudioPlaylist* apl;
178
179         if (pl->frozen()) {
180                 return;
181         }
182
183         if ((apl = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
184                 return;
185         }
186
187         DSPL_Map::iterator x;
188
189         if ((x = dspl_map.find (apl->get_orig_diskstream_id())) == dspl_map.end()) {
190
191                 pair<ARDOUR::id_t,list<Playlist*>*> newp (apl->get_orig_diskstream_id(), new list<Playlist*>);
192                 
193                 x = dspl_map.insert (dspl_map.end(), newp);
194         }
195
196         x->second->push_back (pl);
197 }
198
199 void
200 PlaylistSelector::set_session (Session* s)
201 {
202         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlaylistSelector::set_session), s));
203
204         session = s;
205
206         if (session) {
207                 session->going_away.connect (bind (mem_fun(*this, &PlaylistSelector::set_session), static_cast<Session*> (0)));
208         }
209 }
210
211 void
212 PlaylistSelector::close_button_click ()
213 {
214         rui = 0;
215         hide ();
216 }
217
218 void
219 PlaylistSelector::selection_changed ()
220 {
221         Playlist *playlist;
222
223         TreeModel::iterator iter = tree.get_selection()->get_selected();
224
225         if (!iter) {
226                 /* nothing selected */
227                 return;
228         }
229
230         if ((playlist = ((*iter)[columns.playlist])) != 0) {
231                 
232                 AudioTrack* at;
233                 AudioPlaylist* apl;
234                 
235                 if ((at = dynamic_cast<AudioTrack*> (&rui->route())) == 0) {
236                         /* eh? */
237                         return;
238                 }
239                 
240                 if ((apl = dynamic_cast<AudioPlaylist*> (playlist)) == 0) {
241                         /* eh? */
242                         return;
243                 }
244                 
245                 at->disk_stream().use_playlist (apl);
246
247                 hide ();
248         }
249
250 }
251