Structure MIDI device selector by manufacturer.
[ardour.git] / gtk2_ardour / midi_list_editor.cc
index b18b57bab79e1e606820ef1860f64e842c404c68..0a87823f8e577845060f9e2c34a3edd97d1a3f5f 100644 (file)
@@ -1,19 +1,19 @@
 /*
     Copyright (C) 2009 Paul Davis
 
-       This program is free software; you can redistribute it and/or modify
-       it under the terms of the GNU General Public License as published by
-       the Free Software Foundation; either version 2 of the License, or
-       (at your option) any later version.
-
-       This program is distributed in the hope that it will be useful,
-       but WITHOUT ANY WARRANTY; without even the implied warranty of
-       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-       GNU General Public License for more details.
-
-       You should have received a copy of the GNU General Public License
-       along with this program; if not, write to the Free Software
-       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+    
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+    
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
 #include <cmath>
@@ -42,6 +42,7 @@
 
 using namespace std;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 using namespace Glib;
 using namespace ARDOUR;
 using Timecode::BBT_Time;
@@ -97,6 +98,7 @@ MidiListEditor::MidiListEditor (Session* s, boost::shared_ptr<MidiRegion> r, boo
 
        view.signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
        view.signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
+       view.signal_scroll_event().connect (sigc::mem_fun (*this, &MidiListEditor::scroll_event), false);
 
        view.append_column (_("Start"), columns.start);
        view.append_column (_("Channel"), columns.channel);
@@ -119,15 +121,18 @@ MidiListEditor::MidiListEditor (Session* s, boost::shared_ptr<MidiRegion> r, boo
        comboCell->property_has_entry() = false;
 
        view.append_column (*lenCol);
-       view.append_column (_("End"), columns.end);
+
        view.set_headers_visible (true);
        view.set_rules_hint (true);
        view.get_selection()->set_mode (SELECTION_MULTIPLE);
        view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &MidiListEditor::selection_changed));
 
-       for (int i = 0; i < 7; ++i) {
+       for (int i = 0; i < 6; ++i) {
                CellRendererText* renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (i));
 
+               TreeViewColumn* col = view.get_column (i);
+               col->set_data (X_("colnum"), GUINT_TO_POINTER(i));
+
                renderer->property_editable() = true;
 
                renderer->signal_editing_started().connect (sigc::bind (sigc::mem_fun (*this, &MidiListEditor::editing_started), i));
@@ -168,6 +173,205 @@ MidiListEditor::~MidiListEditor ()
 {
 }
 
+bool
+MidiListEditor::scroll_event (GdkEventScroll* ev)
+{
+       TreeModel::Path path;
+       TreeViewColumn* col;
+       int cellx;
+       int celly;
+       int idelta = 0;
+       double fdelta = 0;
+       MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
+       bool apply = false;
+       bool was_selected = false;
+       char const * opname;
+
+       if (!view.get_path_at_pos (ev->x, ev->y, path, col, cellx, celly)) {
+               return false;
+       }
+       
+       if (view.get_selection()->count_selected_rows() == 0) {
+               was_selected = false;
+       } else if (view.get_selection()->is_selected (path)) {
+               was_selected = true;
+       } else {
+               was_selected = false;
+       }
+       
+       int colnum = GPOINTER_TO_UINT (col->get_data (X_("colnum")));
+
+       switch (colnum) {
+       case 0:
+               if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
+                       fdelta = 1/64.0;
+               } else {
+                       fdelta = 1/4.0;
+               }
+               if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
+                       fdelta = -fdelta;
+               }
+               prop = MidiModel::NoteDiffCommand::StartTime;
+               opname = _("edit note start");
+               apply = true;
+               break;
+       case 1:
+               idelta = 1;
+               if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
+                       idelta = -idelta;
+               }
+               prop = MidiModel::NoteDiffCommand::Channel;
+               opname = _("edit note channel");
+               apply = true;
+               break;
+       case 2:
+       case 3:
+               idelta = 1;
+               if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
+                       idelta = -idelta;
+               }
+               prop = MidiModel::NoteDiffCommand::NoteNumber;
+               opname = _("edit note number");
+               apply = true;
+               break;
+
+       case 4:
+               idelta = 1;
+               if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
+                       idelta = -idelta;
+               }
+               prop = MidiModel::NoteDiffCommand::Velocity;
+               opname = _("edit note velocity");
+               apply = true;
+               break;
+
+       case 5:
+               if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
+                       fdelta = 1/64.0;
+               } else {
+                       fdelta = 1/4.0;
+               }
+               if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
+                       fdelta = -fdelta;
+               }
+               prop = MidiModel::NoteDiffCommand::Length;
+               opname = _("edit note length");
+               apply = true;
+               break;
+
+       default:
+               break;
+       }
+
+
+       if (apply) {
+
+               boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
+               MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
+               vector<TreeModel::Path> previous_selection;
+
+               if (was_selected) {
+
+                       /* use selection */
+                       
+                       TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
+                       TreeModel::iterator iter;
+                       boost::shared_ptr<NoteType> note;
+                       
+                       for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
+
+                               previous_selection.push_back (*i);
+
+                               if ((iter = model->get_iter (*i))) {
+                                       
+                                       note = (*iter)[columns._note];          
+                                       
+                                       switch (prop) {
+                                       case MidiModel::NoteDiffCommand::StartTime:
+                                               if (note->time() + fdelta >= 0) {
+                                                       cmd->change (note, prop, note->time() + fdelta);
+                                               } else {
+                                                       cmd->change (note, prop, Evoral::MusicalTime());
+                                               }
+                                               break;
+                                       case MidiModel::NoteDiffCommand::Velocity:
+                                               cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
+                                               break;
+                                       case MidiModel::NoteDiffCommand::Length:
+                                               if (note->length().to_double() + fdelta >=
+                                                   Evoral::MusicalTime::tick().to_double()) {
+                                                       cmd->change (note, prop, note->length() + fdelta);
+                                               } else {
+                                                       cmd->change (note, prop, Evoral::MusicalTime::tick());
+                                               }
+                                               break;
+                                       case MidiModel::NoteDiffCommand::Channel:
+                                               cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
+                                               break;
+                                       case MidiModel::NoteDiffCommand::NoteNumber:
+                                               cmd->change (note, prop, (uint8_t) (note->note() + idelta));
+                                               break;
+                                       default:
+                                               continue;
+                                       }
+                               }
+                       }
+
+               } else {
+
+                       /* just this row */
+                       
+                       TreeModel::iterator iter;
+                       iter = model->get_iter (path);
+
+                       previous_selection.push_back (path);
+
+                       if (iter) {
+                               boost::shared_ptr<NoteType> note = (*iter)[columns._note];
+                               
+                               switch (prop) {
+                               case MidiModel::NoteDiffCommand::StartTime:
+                                       if (note->time() + fdelta >= 0) {
+                                               cmd->change (note, prop, note->time() + fdelta);
+                                       } else {
+                                               cmd->change (note, prop, Evoral::MusicalTime());
+                                       }
+                                       break;
+                               case MidiModel::NoteDiffCommand::Velocity:
+                                       cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
+                                       break;
+                               case MidiModel::NoteDiffCommand::Length:
+                                       if (note->length() + fdelta >=
+                                           Evoral::MusicalTime::tick().to_double()) {
+                                               cmd->change (note, prop, note->length() + fdelta);
+                                       } else {
+                                               cmd->change (note, prop, Evoral::MusicalTime::tick());
+                                       }
+                                       break;
+                               case MidiModel::NoteDiffCommand::Channel:
+                                       cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
+                                       break;
+                               case MidiModel::NoteDiffCommand::NoteNumber:
+                                       cmd->change (note, prop, (uint8_t) (note->note() + idelta));
+                                       break;
+                               default:
+                                       break;
+                               }
+                       }
+               }
+
+               m->apply_command (*_session, cmd);
+
+               /* reset selection to be as it was before we rebuilt */
+               
+               for (vector<TreeModel::Path>::iterator i = previous_selection.begin(); i != previous_selection.end(); ++i) {
+                       view.get_selection()->select (*i);
+               }
+       }
+
+       return true;
+}
+
 bool
 MidiListEditor::key_press (GdkEventKey* ev)
 {
@@ -184,7 +388,7 @@ MidiListEditor::key_press (GdkEventKey* ev)
                        if (editing_editable) {
                                editing_editable->editing_done ();
                        }
-                       if (colnum >= 6) {
+                       if (colnum >= 5) {
                                /* wrap to next line */
                                colnum = 0;
                                path.next();
@@ -243,7 +447,6 @@ MidiListEditor::key_release (GdkEventKey* ev)
        TreeModel::Path path;
        TreeViewColumn* col;
        TreeModel::iterator iter;
-       TreeModel::Row row;
        MidiModel::NoteDiffCommand* cmd;
        boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
        boost::shared_ptr<NoteType> note;
@@ -384,9 +587,9 @@ MidiListEditor::edited (const std::string& path, const std::string& text)
        double fval;
        int    ival;
        bool   apply = false;
-       int    idelta;
-       double fdelta;
-       char opname;
+       int    idelta = 0;
+       double fdelta = 0;
+       char const * opname;
        switch (edit_column) {
        case 0: // start
                break;
@@ -436,7 +639,7 @@ MidiListEditor::edited (const std::string& path, const std::string& text)
                         * entry for the actual note ticks
                         */
 
-                       int len_ticks = lrint (note->length() * Timecode::BBT_Time::ticks_per_beat);
+                       uint64_t len_ticks = note->length().to_ticks();
                        std::map<int,string>::iterator x = note_length_map.find (len_ticks);
 
                        if (x == note_length_map.end()) {
@@ -481,15 +684,13 @@ MidiListEditor::edited (const std::string& path, const std::string& text)
                }
 
                if (fval > 0.0) {
-                       fdelta = fval - note->length();
+                       fdelta = fval - note->length().to_double();
                        prop = MidiModel::NoteDiffCommand::Length;
                        opname = _("change note length");
                        apply = true;
                }
                break;
 
-       case 6: // end
-               break;
        default:
                break;
        }
@@ -502,7 +703,7 @@ MidiListEditor::edited (const std::string& path, const std::string& text)
                TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
                
                for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
-                       if (iter = model->get_iter (*i)) {
+                       if ((iter = model->get_iter (*i))) {
 
                                note = (*iter)[columns._note];          
                                
@@ -563,7 +764,6 @@ MidiListEditor::redisplay_model ()
                        row[columns.velocity] = (*i)->velocity();
 
                        Timecode::BBT_Time bbt;
-                       double dur;
 
                        _session->tempo_map().bbt_time (conv.to ((*i)->time()), bbt);
 
@@ -572,11 +772,11 @@ MidiListEditor::redisplay_model ()
                        row[columns.start] = ss.str();
 
                        bbt.bars = 0;
-                       dur = (*i)->end_time() - (*i)->time();
-                       bbt.beats = floor (dur);
-                       bbt.ticks = (uint32_t) lrint (fmod (dur, 1.0) * Timecode::BBT_Time::ticks_per_beat);
+                       const Evoral::MusicalTime dur = (*i)->end_time() - (*i)->time();
+                       bbt.beats = dur.get_beats ();
+                       bbt.ticks = dur.get_ticks ();
                        
-                       int len_ticks = lrint ((*i)->length() * Timecode::BBT_Time::ticks_per_beat);
+                       int len_ticks = (*i)->length().to_ticks();
                        std::map<int,string>::iterator x = note_length_map.find (len_ticks);
 
                        if (x != note_length_map.end()) {
@@ -587,12 +787,6 @@ MidiListEditor::redisplay_model ()
                                row[columns.length] = ss.str();
                        }
 
-                       _session->tempo_map().bbt_time (conv.to ((*i)->end_time()), bbt);
-                       
-                       ss.str ("");
-                       ss << bbt;
-                       row[columns.end] = ss.str();
-
                        row[columns._note] = (*i);
                }
        }
@@ -615,7 +809,7 @@ MidiListEditor::selection_changed ()
        NotePlayer* player = new NotePlayer (track);
 
        for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
-               if (iter = model->get_iter (*i)) {
+               if ((iter = model->get_iter (*i))) {
                        note = (*iter)[columns._note];          
                        player->add (note);
                }