Fix assertion failure when switching from a 2-channel to 1-channel route with the...
[ardour.git] / gtk2_ardour / midi_list_editor.cc
1 /*
2    Copyright (C) 2009 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 */
19
20 #include "evoral/midi_util.h"
21 #include "ardour/midi_region.h"
22 #include "ardour/session.h"
23 #include "ardour/tempo.h"
24
25 #include "midi_list_editor.h"
26
27 #include "i18n.h"
28
29 using namespace std;
30 using namespace Gtk;
31 using namespace Glib;
32 using namespace ARDOUR;
33
34 MidiListEditor::MidiListEditor (Session& s, boost::shared_ptr<MidiRegion> r)
35         : ArdourDialog (r->name(), false, false)
36         , session (s)
37         , region (r)
38 {
39         model = ListStore::create (columns);
40         view.set_model (model);
41
42         view.append_column (_("Start"), columns.start);
43         view.append_column (_("Channel"), columns.channel);
44         view.append_column (_("Num"), columns.note);
45         view.append_column (_("Name"), columns.note_name);
46         view.append_column (_("Vel"), columns.velocity);
47         view.append_column (_("Length"), columns.length);
48         view.append_column (_("End"), columns.end);
49         view.set_headers_visible (true);
50         view.set_name (X_("MidiListView"));
51         view.set_rules_hint (true);
52
53         for (int i = 0; i < 6; ++i) {
54                 CellRendererText* renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (i));
55                 renderer->property_editable() = true;
56                 renderer->signal_edited().connect (mem_fun (*this, &MidiListEditor::edited));
57         }
58
59         scroller.add (view);
60         scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
61
62         redisplay_model ();
63
64         view.show ();
65         scroller.show ();
66
67         get_vbox()->pack_start (scroller);
68         set_size_request (400, 400);
69 }
70
71 MidiListEditor::~MidiListEditor ()
72 {
73 }
74
75 void
76 MidiListEditor::edited (const Glib::ustring& path, const Glib::ustring& /* text */)
77 {
78         TreeModel::iterator iter = model->get_iter (path);
79
80         cerr << "Edit at " << path << endl;
81
82         if (!iter) {
83                 return;
84         }
85
86         boost::shared_ptr<NoteType> note = (*iter)[columns._note];
87
88         cerr << "Edited " << *note << endl;
89
90         redisplay_model ();
91 }
92
93 void
94 MidiListEditor::redisplay_model ()
95 {
96         view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
97         model->clear ();
98
99         MidiModel::Notes notes = region->midi_source(0)->model()->notes();
100         TreeModel::Row row;
101
102         for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
103                 row = *(model->append());
104                 row[columns.channel] = (*i)->channel();
105                 row[columns.note_name] = _("Note");
106                 row[columns.note] = (*i)->note();
107                 row[columns.velocity] = (*i)->velocity();
108
109                 BBT_Time bbt;
110                 BBT_Time dur;
111                 stringstream ss;
112
113                 session.tempo_map().bbt_time (region->position(), bbt);
114
115                 dur.bars = 0;
116                 dur.beats = floor ((*i)->time());
117                 dur.ticks = 0;
118
119                 session.tempo_map().bbt_duration_at (region->position(), dur, 0);
120
121                 ss << bbt;
122                 row[columns.start] = ss.str();
123                 ss << dur;
124                 row[columns.length] = ss.str();
125
126                 session.tempo_map().bbt_time (region->position(), bbt);
127                 /* XXX get end point */
128
129                 ss << bbt;
130                 row[columns.end] = ss.str();
131
132                 row[columns._note] = (*i);
133         }
134
135         view.set_model (model);
136 }