provide full scroll-based editing in MIDI list editor
[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 #include <cmath>
20 #include <map>
21
22 #include <gtkmm/cellrenderercombo.h>
23
24 #include "evoral/midi_util.h"
25 #include "evoral/Note.hpp"
26
27 #include "ardour/beats_frames_converter.h"
28 #include "ardour/midi_model.h"
29 #include "ardour/midi_region.h"
30 #include "ardour/midi_source.h"
31 #include "ardour/session.h"
32 #include "ardour/tempo.h"
33
34 #include "gtkmm2ext/gui_thread.h"
35 #include "gtkmm2ext/keyboard.h"
36 #include "gtkmm2ext/actions.h"
37
38 #include "midi_list_editor.h"
39 #include "note_player.h"
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace Gtk;
45 using namespace Gtkmm2ext;
46 using namespace Glib;
47 using namespace ARDOUR;
48 using Timecode::BBT_Time;
49
50 static map<int,std::string> note_length_map;
51
52 static void 
53 fill_note_length_map ()
54 {
55         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat, _("Whole")));
56         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/2, _("Half")));
57         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/3, _("Triplet")));
58         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/4, _("Quarter")));
59         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/8, _("Eighth")));
60         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/16, _("Sixteenth")));
61         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/32, _("Thirty-second")));
62         note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/64, _("Sixty-fourth")));
63 }
64
65 MidiListEditor::MidiListEditor (Session* s, boost::shared_ptr<MidiRegion> r, boost::shared_ptr<MidiTrack> tr)
66         : ArdourWindow (r->name())
67         , buttons (1, 1)
68         , region (r)
69         , track (tr)
70 {
71         if (note_length_map.empty()) {
72                 fill_note_length_map ();
73         }
74         
75         /* We do not handle nested sources/regions. Caller should have tackled this */
76
77         if (r->max_source_level() > 0) {
78                 throw failed_constructor();
79         }
80
81         set_session (s);
82
83         edit_column = -1;
84         editing_renderer = 0;
85         editing_editable = 0;
86
87         model = ListStore::create (columns);
88         view.set_model (model);
89
90         note_length_model = ListStore::create (note_length_columns);
91         TreeModel::Row row;
92         
93         for (std::map<int,string>::iterator i = note_length_map.begin(); i != note_length_map.end(); ++i) {
94                 row = *(note_length_model->append());
95                 row[note_length_columns.ticks] = i->first;
96                 row[note_length_columns.name] = i->second;
97         }
98
99         view.signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
100         view.signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
101         view.signal_scroll_event().connect (sigc::mem_fun (*this, &MidiListEditor::scroll_event), false);
102
103         view.append_column (_("Start"), columns.start);
104         view.append_column (_("Channel"), columns.channel);
105         view.append_column (_("Num"), columns.note);
106         view.append_column (_("Name"), columns.note_name);
107         view.append_column (_("Vel"), columns.velocity);
108
109         /* use a combo renderer for length, so that we can offer a selection
110            of pre-defined note lengths. we still allow edited values with
111            arbitrary length (in ticks).
112          */
113
114         Gtk::TreeViewColumn* lenCol = Gtk::manage (new Gtk::TreeViewColumn (_("Length")));
115         Gtk::CellRendererCombo* comboCell = Gtk::manage(new Gtk::CellRendererCombo);
116         lenCol->pack_start(*comboCell);
117         lenCol->add_attribute (comboCell->property_text(), columns.length);
118
119         comboCell->property_model() = note_length_model;
120         comboCell->property_text_column() = 1;
121         comboCell->property_has_entry() = false;
122
123         view.append_column (*lenCol);
124
125         view.set_headers_visible (true);
126         view.set_rules_hint (true);
127         view.get_selection()->set_mode (SELECTION_MULTIPLE);
128         view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &MidiListEditor::selection_changed));
129
130         for (int i = 0; i < 6; ++i) {
131                 CellRendererText* renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (i));
132
133                 TreeViewColumn* col = view.get_column (i);
134                 col->set_data (X_("colnum"), GUINT_TO_POINTER(i));
135
136                 renderer->property_editable() = true;
137
138                 renderer->signal_editing_started().connect (sigc::bind (sigc::mem_fun (*this, &MidiListEditor::editing_started), i));
139                 renderer->signal_editing_canceled().connect (sigc::mem_fun (*this, &MidiListEditor::editing_canceled));
140                 renderer->signal_edited().connect (sigc::mem_fun (*this, &MidiListEditor::edited));
141         }
142
143         scroller.add (view);
144         scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
145
146         redisplay_model ();
147
148         region->midi_source(0)->model()->ContentsChanged.connect (content_connection, invalidator (*this), 
149                                                                   boost::bind (&MidiListEditor::redisplay_model, this), gui_context());
150
151         buttons.attach (sound_notes_button, 0, 1, 0, 1);
152         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action ("Editor", "sound-midi-notes");
153         if (act) {
154                 gtk_activatable_set_related_action (GTK_ACTIVATABLE (sound_notes_button.gobj()), act->gobj());  
155         }
156
157         view.show ();
158         scroller.show ();
159         buttons.show ();
160         vbox.show ();
161         sound_notes_button.show ();
162
163         vbox.set_spacing (6);
164         vbox.set_border_width (6);
165         vbox.pack_start (buttons, false, false);
166         vbox.pack_start (scroller, true, true);
167
168         add (vbox);
169         set_size_request (-1, 400);
170 }
171
172 MidiListEditor::~MidiListEditor ()
173 {
174 }
175
176 bool
177 MidiListEditor::scroll_event (GdkEventScroll* ev)
178 {
179         TreeModel::Path path;
180         TreeViewColumn* col;
181         int cellx;
182         int celly;
183         int idelta;
184         double fdelta;
185         MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
186         bool apply = false;
187         bool was_selected = false;
188         char* opname;
189
190         if (!view.get_path_at_pos (ev->x, ev->y, path, col, cellx, celly)) {
191                 return false;
192         }
193         
194         if (view.get_selection()->count_selected_rows() == 0) {
195                 was_selected = false;
196         } else if (view.get_selection()->is_selected (path)) {
197                 was_selected = true;
198         } else {
199                 was_selected = false;
200         }
201         
202         int colnum = GPOINTER_TO_UINT (col->get_data (X_("colnum")));
203
204         switch (colnum) {
205         case 0:
206                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
207                         fdelta = 1/64.0;
208                 } else {
209                         fdelta = 1/4.0;
210                 }
211                 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
212                         fdelta = -fdelta;
213                 }
214                 prop = MidiModel::NoteDiffCommand::StartTime;
215                 opname = _("edit note start");
216                 apply = true;
217                 break;
218         case 1:
219                 idelta = 1;
220                 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
221                         idelta = -idelta;
222                 }
223                 prop = MidiModel::NoteDiffCommand::Channel;
224                 opname = _("edit note channel");
225                 apply = true;
226                 break;
227         case 2:
228         case 3:
229                 idelta = 1;
230                 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
231                         idelta = -idelta;
232                 }
233                 prop = MidiModel::NoteDiffCommand::NoteNumber;
234                 opname = _("edit note number");
235                 apply = true;
236                 break;
237
238         case 4:
239                 idelta = 1;
240                 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
241                         idelta = -idelta;
242                 }
243                 prop = MidiModel::NoteDiffCommand::Velocity;
244                 opname = _("edit note velocity");
245                 apply = true;
246                 break;
247
248         case 5:
249                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
250                         fdelta = 1/64.0;
251                 } else {
252                         fdelta = 1/4.0;
253                 }
254                 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
255                         fdelta = -fdelta;
256                 }
257                 prop = MidiModel::NoteDiffCommand::Length;
258                 opname = _("edit note length");
259                 apply = true;
260                 break;
261
262         default:
263                 break;
264         }
265
266
267         if (apply) {
268
269                 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
270                 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
271                 vector<TreeModel::Path> previous_selection;
272
273                 if (was_selected) {
274
275                         /* use selection */
276                         
277                         TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
278                         TreeModel::iterator iter;
279                         boost::shared_ptr<NoteType> note;
280                         
281                         for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
282
283                                 previous_selection.push_back (*i);
284
285                                 if (iter = model->get_iter (*i)) {
286                                         
287                                         note = (*iter)[columns._note];          
288                                         
289                                         switch (prop) {
290                                         case MidiModel::NoteDiffCommand::StartTime:
291                                                 if (note->time() + fdelta >= 0) {
292                                                         cmd->change (note, prop, note->time() + fdelta);
293                                                 } else {
294                                                         cmd->change (note, prop, 0.0);
295                                                 }
296                                                 break;
297                                         case MidiModel::NoteDiffCommand::Velocity:
298                                                 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
299                                                 break;
300                                         case MidiModel::NoteDiffCommand::Length:
301                                                 if (note->length() + fdelta >= 1.0/BBT_Time::ticks_per_beat) {
302                                                         cmd->change (note, prop, note->length() + fdelta);
303                                                 } else {
304                                                         cmd->change (note, prop, 1.0/BBT_Time::ticks_per_beat);
305                                                 }
306                                                 break;
307                                         case MidiModel::NoteDiffCommand::Channel:
308                                                 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
309                                                 break;
310                                         case MidiModel::NoteDiffCommand::NoteNumber:
311                                                 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
312                                                 break;
313                                         default:
314                                                 continue;
315                                         }
316                                 }
317                         }
318
319                 } else {
320
321                         /* just this row */
322                         
323                         TreeModel::iterator iter;
324                         iter = model->get_iter (path);
325
326                         previous_selection.push_back (path);
327
328                         if (iter) {
329                                 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
330                                 
331                                 switch (prop) {
332                                 case MidiModel::NoteDiffCommand::StartTime:
333                                         if (note->time() + fdelta >= 0) {
334                                                 cmd->change (note, prop, note->time() + fdelta);
335                                         } else {
336                                                 cmd->change (note, prop, 0.0);
337                                         }
338                                         break;
339                                 case MidiModel::NoteDiffCommand::Velocity:
340                                         cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
341                                         break;
342                                 case MidiModel::NoteDiffCommand::Length:
343                                         if (note->length() + fdelta >= 1.0/BBT_Time::ticks_per_beat) {
344                                                 cmd->change (note, prop, note->length() + fdelta);
345                                         } else {
346                                                 cmd->change (note, prop, 1.0/BBT_Time::ticks_per_beat);
347                                         }
348                                         break;
349                                 case MidiModel::NoteDiffCommand::Channel:
350                                         cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
351                                         break;
352                                 case MidiModel::NoteDiffCommand::NoteNumber:
353                                         cmd->change (note, prop, (uint8_t) (note->note() + idelta));
354                                         break;
355                                 default:
356                                         break;
357                                 }
358                         }
359                 }
360
361                 m->apply_command (*_session, cmd);
362
363                 /* reset selection to be as it was before we rebuilt */
364                 
365                 for (vector<TreeModel::Path>::iterator i = previous_selection.begin(); i != previous_selection.end(); ++i) {
366                         view.get_selection()->select (*i);
367                 }
368         }
369
370         return true;
371 }
372
373 bool
374 MidiListEditor::key_press (GdkEventKey* ev)
375 {
376         bool ret = false;
377         TreeModel::Path path;
378         TreeViewColumn* col;
379         int colnum;
380
381         switch (ev->keyval) {
382         case GDK_Tab:
383                 if (edit_column > 0) {
384                         colnum = edit_column;
385                         path = edit_path;
386                         if (editing_editable) {
387                                 editing_editable->editing_done ();
388                         }
389                         if (colnum >= 5) {
390                                 /* wrap to next line */
391                                 colnum = 0;
392                                 path.next();
393                         } else {
394                                 colnum++;
395                         }
396                         col = view.get_column (colnum);
397                         view.set_cursor (path, *col, true);
398                         ret = true;
399                 }
400                 break;
401                 
402         case GDK_Up:
403         case GDK_uparrow:
404                 if (edit_column > 0) {
405                         colnum = edit_column;
406                         path = edit_path;
407                         if (editing_editable) {
408                                 editing_editable->editing_done ();
409                         }
410                         path.prev ();
411                         col = view.get_column (colnum);
412                         view.set_cursor (path, *col, true);
413                         ret = true;
414                 }
415                 break;
416
417         case GDK_Down:
418         case GDK_downarrow:
419                 if (edit_column > 0) {
420                         colnum = edit_column;
421                         path = edit_path;
422                         if (editing_editable) {
423                                 editing_editable->editing_done ();
424                         }
425                         path.next ();
426                         col = view.get_column (colnum);
427                         view.set_cursor (path, *col, true);
428                         ret = true;
429                 }
430                 break;
431
432         case GDK_Escape:
433                 stop_editing (true);
434                 break;
435                 
436         }
437
438         return ret;
439 }
440
441 bool
442 MidiListEditor::key_release (GdkEventKey* ev)
443 {
444         bool ret = false;
445         TreeModel::Path path;
446         TreeViewColumn* col;
447         TreeModel::iterator iter;
448         TreeModel::Row row;
449         MidiModel::NoteDiffCommand* cmd;
450         boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
451         boost::shared_ptr<NoteType> note;
452         boost::shared_ptr<NoteType> copy;
453
454         switch (ev->keyval) {
455         case GDK_Insert:
456                 /* add a new note to the model, based on the note at the cursor
457                  * pos
458                  */
459                 view.get_cursor (path, col);
460                 iter = model->get_iter (path);
461                 cmd = m->new_note_diff_command (_("insert new note"));
462                 note = (*iter)[columns._note];
463                 copy.reset (new NoteType (*note.get()));
464                 cmd->add (copy);
465                 m->apply_command (*_session, cmd);
466                 /* model has been redisplayed by now */
467                 path.next ();
468                 /* select, start editing column 2 (note) */
469                 col = view.get_column (2);
470                 view.set_cursor (path, *col, true);
471                 break;
472
473         case GDK_Delete:
474         case GDK_BackSpace:
475                 if (edit_column < 0) {
476                         delete_selected_note ();
477                 }
478                 ret = true;
479                 break;
480
481         case GDK_z:
482                 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
483                         _session->undo (1);
484                         ret = true;
485                 }
486                 break;
487                 
488         case GDK_r:
489                 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
490                         _session->redo (1);
491                         ret = true;
492                 }
493                 break;
494
495         default:
496                 break;
497         }
498
499         return ret;
500 }
501
502 void
503 MidiListEditor::delete_selected_note ()
504 {
505         Glib::RefPtr<TreeSelection> selection = view.get_selection();
506         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
507
508         if (rows.empty()) {
509                 return;
510         }
511
512         typedef vector<boost::shared_ptr<NoteType> > Notes;
513         Notes to_delete;
514
515         for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
516                 TreeIter iter;
517
518                 if ((iter = model->get_iter (*i))) {
519                         boost::shared_ptr<NoteType> note = (*iter)[columns._note];
520                         to_delete.push_back (note);
521                 }
522         }
523
524         boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
525         MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (_("delete notes (from list)"));
526
527         for (Notes::iterator i = to_delete.begin(); i != to_delete.end(); ++i) {
528                 cmd->remove (*i);
529         }
530
531         m->apply_command (*_session, cmd);
532 }
533
534 void
535 MidiListEditor::stop_editing (bool cancelled)
536 {
537         if (!cancelled) {
538                 if (editing_editable) {
539                         editing_editable->editing_done ();
540                 }
541         } else {
542                 if (editing_renderer) {
543                         editing_renderer->stop_editing (cancelled);
544                 }
545         }
546 }
547
548 void
549 MidiListEditor::editing_started (CellEditable* ed, const string& path, int colno)
550 {
551         edit_path = TreePath (path);
552         edit_column = colno;
553         editing_renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (colno));
554         editing_editable = ed;
555
556         if (ed) {
557                 Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ed);
558                 if (e) {
559                         e->signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
560                         e->signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
561                 }
562         }
563 }
564
565 void
566 MidiListEditor::editing_canceled ()
567 {
568         edit_path.clear ();
569         edit_column = -1;
570         editing_renderer = 0;
571         editing_editable = 0;
572 }
573
574 void
575 MidiListEditor::edited (const std::string& path, const std::string& text)
576 {
577         TreeModel::iterator iter = model->get_iter (path);
578
579         if (!iter || text.empty()) {
580                 return;
581         }
582
583         boost::shared_ptr<NoteType> note = (*iter)[columns._note];
584         MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
585
586         double fval;
587         int    ival;
588         bool   apply = false;
589         int    idelta;
590         double fdelta;
591         char*  opname;
592         switch (edit_column) {
593         case 0: // start
594                 break;
595         case 1: // channel
596                 // correct ival for zero-based counting after scan
597                 if (sscanf (text.c_str(), "%d", &ival) == 1 && --ival != note->channel()) {
598                         idelta = ival - note->channel();
599                         prop = MidiModel::NoteDiffCommand::Channel;
600                         opname = _("change note channel");
601                         apply = true;
602                 }
603                 break;
604         case 2: // note
605                 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->note()) {
606                         idelta = ival - note->note();
607                         prop = MidiModel::NoteDiffCommand::NoteNumber;
608                         opname = _("change note number");
609                         apply = true;
610                 }
611                 break;
612         case 3: // name
613                 break;
614         case 4: // velocity
615                 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->velocity()) {
616                         idelta = ival - note->velocity();
617                         prop = MidiModel::NoteDiffCommand::Velocity;
618                         opname = _("change note velocity");
619                         apply = true;
620                 }
621                 break;
622         case 5: // length
623
624                 if (sscanf (text.c_str(), "%lf", &fval) == 1) {
625
626                         /* numeric value entered */
627                         
628                         if (text.find ('.') == string::npos && text.find (',') == string::npos) {
629                                 /* integral => units are ticks */
630                                 fval = fval / BBT_Time::ticks_per_beat;
631                         } else {
632                                 /* non-integral => beats, so use as-is */
633                         }
634
635                 } else {
636
637                         /* assume its text from the combo. look for the map
638                          * entry for the actual note ticks
639                          */
640
641                         int len_ticks = lrint (note->length() * Timecode::BBT_Time::ticks_per_beat);
642                         std::map<int,string>::iterator x = note_length_map.find (len_ticks);
643
644                         if (x == note_length_map.end()) {
645
646                                 /* tick length not in map - was
647                                  * displaying numeric value ... use new value
648                                  * from note length map, and convert to beats.
649                                  */
650                                 
651                                 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
652                                         if (x->second == text) {
653                                                 break;
654                                         }
655                                 }
656                                 
657                                 if (x != note_length_map.end()) {
658                                         fval = x->first / BBT_Time::ticks_per_beat;
659                                 }
660
661                         } else {
662
663                                 fval = -1.0;
664
665                                 if (text != x->second) {
666                                         
667                                         /* get ticks for the newly selected
668                                          * note length
669                                          */
670
671                                         for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
672                                                 if (x->second == text) {
673                                                         break;
674                                                 }
675                                         }
676                                         
677                                         if (x != note_length_map.end()) {
678                                                 /* convert to beats */
679                                                 fval = (double) x->first / BBT_Time::ticks_per_beat;
680                                         }
681                                 }
682                         }
683                 }
684
685                 if (fval > 0.0) {
686                         fdelta = fval - note->length();
687                         prop = MidiModel::NoteDiffCommand::Length;
688                         opname = _("change note length");
689                         apply = true;
690                 }
691                 break;
692
693         default:
694                 break;
695         }
696
697         if (apply) {
698
699                 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
700                 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
701
702                 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
703                 
704                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
705                         if (iter = model->get_iter (*i)) {
706
707                                 note = (*iter)[columns._note];          
708                                 
709                                 switch (prop) {
710                                 case MidiModel::NoteDiffCommand::Velocity:
711                                         cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
712                                         break;
713                                 case MidiModel::NoteDiffCommand::Length:
714                                         cmd->change (note, prop, note->length() + fdelta);
715                                         break;
716                                 case MidiModel::NoteDiffCommand::Channel:
717                                         cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
718                                         break;
719                                 case MidiModel::NoteDiffCommand::NoteNumber:
720                                         cmd->change (note, prop, (uint8_t) (note->note() + idelta));
721                                         break;
722                                 default:
723                                         continue;
724                                 }
725                         }
726                 }
727
728                 m->apply_command (*_session, cmd);
729
730                 /* model has been redisplayed by now */
731                 /* keep selected row(s), move cursor there, don't continue editing */
732                 
733                 TreeViewColumn* col = view.get_column (edit_column);
734                 view.set_cursor (edit_path, *col, 0);
735
736                 /* reset edit info, since we're done */
737
738                 edit_path.clear ();
739                 edit_column = -1;
740                 editing_renderer = 0;
741                 editing_editable = 0;
742         }
743 }
744
745 void
746 MidiListEditor::redisplay_model ()
747 {
748         view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
749         model->clear ();
750
751         if (_session) {
752
753                 BeatsFramesConverter conv (_session->tempo_map(), region->position());
754                 MidiModel::Notes notes = region->midi_source(0)->model()->notes();
755                 TreeModel::Row row;
756                 stringstream ss;
757
758                 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
759                         row = *(model->append());
760                         row[columns.channel] = (*i)->channel() + 1;
761                         row[columns.note_name] = Evoral::midi_note_name ((*i)->note());
762                         row[columns.note] = (*i)->note();
763                         row[columns.velocity] = (*i)->velocity();
764
765                         Timecode::BBT_Time bbt;
766                         double dur;
767
768                         _session->tempo_map().bbt_time (conv.to ((*i)->time()), bbt);
769
770                         ss.str ("");
771                         ss << bbt;
772                         row[columns.start] = ss.str();
773
774                         bbt.bars = 0;
775                         dur = (*i)->end_time() - (*i)->time();
776                         bbt.beats = floor (dur);
777                         bbt.ticks = (uint32_t) lrint (fmod (dur, 1.0) * Timecode::BBT_Time::ticks_per_beat);
778                         
779                         int len_ticks = lrint ((*i)->length() * Timecode::BBT_Time::ticks_per_beat);
780                         std::map<int,string>::iterator x = note_length_map.find (len_ticks);
781
782                         if (x != note_length_map.end()) {
783                                 row[columns.length] = x->second;
784                         } else {
785                                 ss.str ("");
786                                 ss << len_ticks;
787                                 row[columns.length] = ss.str();
788                         }
789
790                         row[columns._note] = (*i);
791                 }
792         }
793
794         view.set_model (model);
795 }
796
797 void
798 MidiListEditor::selection_changed ()
799 {
800         if (!Config->get_sound_midi_notes()) {
801                 return;
802         }
803
804         TreeModel::Path path;
805         TreeModel::iterator iter;
806         boost::shared_ptr<NoteType> note;
807         TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
808
809         NotePlayer* player = new NotePlayer (track);
810
811         for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
812                 if (iter = model->get_iter (*i)) {
813                         note = (*iter)[columns._note];          
814                         player->add (note);
815                 }
816         }
817
818         player->play ();
819 }