2 Copyright (C) 2009 Paul Davis
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.
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.
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.
21 #include "evoral/midi_util.h"
22 #include "evoral/Note.hpp"
24 #include "ardour/beats_frames_converter.h"
25 #include "ardour/midi_model.h"
26 #include "ardour/midi_region.h"
27 #include "ardour/midi_source.h"
28 #include "ardour/session.h"
29 #include "ardour/tempo.h"
31 #include "midi_list_editor.h"
38 using namespace ARDOUR;
40 MidiListEditor::MidiListEditor (Session* s, boost::shared_ptr<MidiRegion> r)
41 : ArdourWindow (r->name())
44 /* We do not handle nested sources/regions. Caller should have tackled this */
46 if (r->max_source_level() > 0) {
47 throw failed_constructor();
52 model = ListStore::create (columns);
53 view.set_model (model);
55 view.signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press));
56 view.signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release));
58 view.append_column (_("Start"), columns.start);
59 view.append_column (_("Channel"), columns.channel);
60 view.append_column (_("Num"), columns.note);
61 view.append_column (_("Name"), columns.note_name);
62 view.append_column (_("Vel"), columns.velocity);
63 view.append_column (_("Length"), columns.length);
64 view.append_column (_("End"), columns.end);
65 view.set_headers_visible (true);
66 view.set_name (X_("MidiListView"));
67 view.set_rules_hint (true);
69 for (int i = 0; i < 6; ++i) {
70 CellRendererText* renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (i));
71 renderer->property_editable() = true;
73 renderer->signal_editing_started().connect (sigc::bind (sigc::mem_fun (*this, &MidiListEditor::editing_started), i));
74 renderer->signal_editing_canceled().connect (sigc::mem_fun (*this, &MidiListEditor::editing_canceled));
75 renderer->signal_edited().connect (sigc::mem_fun (*this, &MidiListEditor::edited));
79 scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
87 set_size_request (400, 400);
90 MidiListEditor::~MidiListEditor ()
95 MidiListEditor::key_press (GdkEventKey* ev)
97 bool editing = !_current_edit.empty();
101 switch (ev->keyval) {
121 MidiListEditor::key_release (GdkEventKey* ev)
125 switch (ev->keyval) {
127 delete_selected_note ();
138 MidiListEditor::delete_selected_note ()
140 Glib::RefPtr<TreeSelection> selection = view.get_selection();
141 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
147 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
150 /* selection mode is single, so rows.begin() is it */
152 if ((iter = model->get_iter (*i))) {
153 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
154 cerr << "Would have deleted " << *note << endl;
160 MidiListEditor::editing_started (CellEditable*, const string& path, int colno)
162 _current_edit = path;
163 cerr << "Now editing " << _current_edit << " Column " << colno << endl;
167 MidiListEditor::editing_canceled ()
173 MidiListEditor::edited (const std::string& path, const std::string& /* text */)
175 TreeModel::iterator iter = model->get_iter (path);
177 cerr << "Edit at " << path << endl;
183 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
185 cerr << "Edited " << *note << endl;
189 /* keep selected row(s), move cursor there, to allow us to continue editing */
193 MidiListEditor::redisplay_model ()
195 view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
200 BeatsFramesConverter conv (_session->tempo_map(), region->position());
201 MidiModel::Notes notes = region->midi_source(0)->model()->notes();
205 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
206 row = *(model->append());
207 row[columns.channel] = (*i)->channel() + 1;
208 row[columns.note_name] = Evoral::midi_note_name ((*i)->note());
209 row[columns.note] = (*i)->note();
210 row[columns.velocity] = (*i)->velocity();
212 Timecode::BBT_Time bbt;
215 _session->tempo_map().bbt_time (conv.to ((*i)->time()), bbt);
219 row[columns.start] = ss.str();
222 dur = (*i)->end_time() - (*i)->time();
223 bbt.beats = floor (dur);
224 bbt.ticks = (uint32_t) lrint (fmod (dur, 1.0) * Timecode::BBT_Time::ticks_per_beat);
226 _session->tempo_map().bbt_duration_at (region->position(), bbt, 0);
230 row[columns.length] = ss.str();
232 _session->tempo_map().bbt_time (conv.to ((*i)->end_time()), bbt);
236 row[columns.end] = ss.str();
238 row[columns._note] = (*i);
242 view.set_model (model);