changes to integrate new/recent/browse in new session dialog.
[ardour.git] / gtk2_ardour / midi_time_axis.cc
index 82faf1890c089fa4fb5afaaf981a04a3ce683866..a4ffc442a3f7414093be61da1ef2d90f5dd65a73 100644 (file)
@@ -19,6 +19,8 @@
 #include <cstdlib>
 #include <cmath>
 
+#include <strings.h> // for ffs(3)
+
 #include <algorithm>
 #include <string>
 #include <vector>
@@ -55,8 +57,6 @@
 #include "ardour/track.h"
 #include "ardour/types.h"
 
-#include "midi++/names.h"
-
 #include "ardour_ui.h"
 #include "ardour_button.h"
 #include "automation_line.h"
@@ -68,6 +68,7 @@
 #include "ghostregion.h"
 #include "gui_thread.h"
 #include "keyboard.h"
+#include "midi_channel_selector.h"
 #include "midi_scroomer.h"
 #include "midi_streamview.h"
 #include "midi_region_view.h"
@@ -96,8 +97,8 @@ using namespace Gtkmm2ext;
 using namespace Editing;
 
 // Minimum height at which a control is displayed
-static const uint32_t MIDI_CONTROLS_BOX_MIN_HEIGHT = 162;
-static const uint32_t KEYBOARD_MIN_HEIGHT = 140;
+static const uint32_t MIDI_CONTROLS_BOX_MIN_HEIGHT = 140;
+static const uint32_t KEYBOARD_MIN_HEIGHT = 130;
 
 MidiTimeAxisView::MidiTimeAxisView (PublicEditor& ed, Session* sess, Canvas& canvas)
        : AxisView(sess) // virtually inherited
@@ -112,6 +113,7 @@ MidiTimeAxisView::MidiTimeAxisView (PublicEditor& ed, Session* sess, Canvas& can
        , _meter_color_mode_item(0)
        , _channel_color_mode_item(0)
        , _track_color_mode_item(0)
+       , _channel_selector (0)
        , _step_edit_item (0)
        , controller_menu (0)
        , _step_editor (0)
@@ -148,6 +150,7 @@ MidiTimeAxisView::set_route (boost::shared_ptr<Route> rt)
                                               atoi (gui_property ("note-range-max").c_str()),
                                               true);
        }
+
        midi_view()->NoteRangeChanged.connect (
                sigc::mem_fun (*this, &MidiTimeAxisView::note_range_changed));
        _view->ContentsHeightChanged.connect (
@@ -198,7 +201,7 @@ MidiTimeAxisView::set_route (boost::shared_ptr<Route> rt)
                v->pack_start (*manage (new Label ("")), true, true);
                v->show ();
                h->show ();
-               controls_hbox.pack_start(*v);
+               controls_hbox.pack_start(*v, false, false);
 
                controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
                controls_base_selected_name = "MidiTrackControlsBaseSelected";
@@ -211,6 +214,22 @@ MidiTimeAxisView::set_route (boost::shared_ptr<Route> rt)
                _view->RegionViewAdded.connect (
                        sigc::mem_fun(*this, &MidiTimeAxisView::region_view_added));
 
+               midi_track()->PlaybackChannelModeChanged.connect (*this, invalidator (*this),
+                                                                 boost::bind (&MidiTimeAxisView::playback_channel_mode_changed, this),
+                                                                 gui_context());
+               midi_track()->PlaybackChannelMaskChanged.connect (*this, invalidator (*this),
+                                                                 boost::bind (&MidiTimeAxisView::playback_channel_mode_changed, this),
+                                                                 gui_context());
+               midi_track()->CaptureChannelModeChanged.connect (*this, invalidator (*this),
+                                                                 boost::bind (&MidiTimeAxisView::capture_channel_mode_changed, this),
+                                                                 gui_context());
+               midi_track()->CaptureChannelMaskChanged.connect (*this, invalidator (*this),
+                                                                 boost::bind (&MidiTimeAxisView::capture_channel_mode_changed, this),
+                                                                 gui_context());
+
+               playback_channel_mode_changed ();
+               capture_channel_mode_changed ();
+
                if (!_editor.have_idled()) {
                        /* first idle will do what we need */
                } else {
@@ -218,7 +237,6 @@ MidiTimeAxisView::set_route (boost::shared_ptr<Route> rt)
                }
        }
 
-
        MIDI::Name::MidiPatchManager& patch_manager = MIDI::Name::MidiPatchManager::instance();
 
        MIDI::Name::MasterDeviceNames::Models::const_iterator m = patch_manager.all_models().begin();
@@ -226,53 +244,78 @@ MidiTimeAxisView::set_route (boost::shared_ptr<Route> rt)
                _midnam_model_selector.append_text(m->c_str());
        }
 
+       if (gui_property (X_("midnam-model-name")).empty()) {
+               set_gui_property (X_("midnam-model-name"), "Generic");
+       }
+
+       if (gui_property (X_("midnam-custom-device-mode")).empty()) {
+               boost::shared_ptr<MIDI::Name::MasterDeviceNames> device_names = get_device_names();
+               if (device_names) {
+                       set_gui_property (X_("midnam-custom-device-mode"),
+                                         *device_names->custom_device_mode_names().begin());
+               }
+       }
+
        _midnam_model_selector.set_active_text (gui_property (X_("midnam-model-name")));
-       model_changed();
        _midnam_custom_device_mode_selector.set_active_text (gui_property (X_("midnam-custom-device-mode")));
 
-       _midnam_model_selector.signal_changed().connect(
-               sigc::mem_fun(*this, &MidiTimeAxisView::model_changed));
-       _midnam_custom_device_mode_selector.signal_changed().connect(
-               sigc::mem_fun(*this, &MidiTimeAxisView::custom_device_mode_changed));
-
        ARDOUR_UI::instance()->set_tip (_midnam_model_selector, _("External MIDI Device"));
        ARDOUR_UI::instance()->set_tip (_midnam_custom_device_mode_selector, _("External Device Mode"));
 
        _midi_controls_box.set_homogeneous(false);
        _midi_controls_box.set_border_width (10);
-       if (!patch_manager.all_models().empty()) {
-               _channel_selector.set_border_width(2);
-               _midi_controls_box.resize(3, 2);
-               _midi_controls_box.attach(_channel_selector, 0, 2, 0, 1);
 
-               _midi_controls_box.attach(*manage(new HSeparator()), 0, 2, 1, 2);
+       _channel_status_box.set_homogeneous (false);
+       _channel_status_box.set_spacing (6);
+       
+       _channel_selector_button.set_label (_("Chns"));
+       ARDOUR_UI::instance()->set_tip (_channel_selector_button, _("Click to edit channel settings"));
+       
+       /* fixed sized labels to prevent silly nonsense (though obviously,
+        * they cause their own too)
+        */
+
+       _playback_channel_status.set_size_request (65, -1);
+       _capture_channel_status.set_size_request (60, -1);
+
+       _channel_status_box.pack_start (_playback_channel_status, false, false);
+       _channel_status_box.pack_start (_capture_channel_status, false, false);
+       _channel_status_box.pack_start (_channel_selector_button, false, false);
+       _channel_status_box.show_all ();
+
+       _channel_selector_button.signal_clicked().connect (sigc::mem_fun (*this, &MidiTimeAxisView::toggle_channel_selector));
+       
+       _midi_controls_box.pack_start (_channel_status_box, false, false, 10);
+
+       if (!patch_manager.all_models().empty()) {
 
                _midnam_model_selector.set_size_request(22, 30);
                _midnam_model_selector.set_border_width(2);
-               _midi_controls_box.attach(_midnam_model_selector, 0, 1, 2, 3);
+               _midnam_model_selector.show ();
+               _midi_controls_box.pack_start (_midnam_model_selector);
 
                _midnam_custom_device_mode_selector.set_size_request(10, 30);
                _midnam_custom_device_mode_selector.set_border_width(2);
-               _midi_controls_box.attach(_midnam_custom_device_mode_selector, 0, 1, 3, 4);
-       } else {
-               _midi_controls_box.attach(_channel_selector, 0, 1, 0, 1);
-       }
+               _midnam_custom_device_mode_selector.show ();
 
-       controls_vbox.pack_start(_midi_controls_box, false, false);
+               _midi_controls_box.pack_start (_midnam_custom_device_mode_selector);
+       } 
 
-       // restore channel selector settings
-       _channel_selector.set_channel_mode(midi_track()->get_channel_mode(),
-                                          midi_track()->get_channel_mask());
-       _channel_selector.mode_changed.connect(
-               sigc::mem_fun(*midi_track(), &MidiTrack::set_channel_mode));
-       _channel_selector.mode_changed.connect(
-               sigc::mem_fun(*this, &MidiTimeAxisView::set_channel_mode));
+       model_changed();
+       custom_device_mode_changed();
+
+       _midnam_model_selector.signal_changed().connect(
+               sigc::mem_fun(*this, &MidiTimeAxisView::model_changed));
+       _midnam_custom_device_mode_selector.signal_changed().connect(
+               sigc::mem_fun(*this, &MidiTimeAxisView::custom_device_mode_changed));
+
+       controls_vbox.pack_start(_midi_controls_box, false, false);
 
        const string color_mode = gui_property ("color-mode");
        if (!color_mode.empty()) {
                _color_mode = ColorMode (string_2_enum(color_mode, _color_mode));
-               if (_color_mode == ChannelColors) {
-                       _channel_selector.set_channel_colors(CanvasNoteEvent::midi_channel_colors);
+               if (_channel_selector && _color_mode == ChannelColors) {
+                       _channel_selector->set_channel_colors(CanvasNoteEvent::midi_channel_colors);
                }
        }
 
@@ -315,6 +358,8 @@ MidiTimeAxisView::first_idle ()
 
 MidiTimeAxisView::~MidiTimeAxisView ()
 {
+       delete _channel_selector;
+
        delete _piano_roll_header;
        _piano_roll_header = 0;
 
@@ -354,19 +399,14 @@ MidiTimeAxisView::model_changed()
        const Glib::ustring model = _midnam_model_selector.get_active_text();
        set_gui_property (X_("midnam-model-name"), model);
 
-       std::list<std::string> device_modes = MIDI::Name::MidiPatchManager::instance()
+       const std::list<std::string> device_modes = MIDI::Name::MidiPatchManager::instance()
                .custom_device_mode_names_by_model(model);
 
        _midnam_custom_device_mode_selector.clear_items();
 
-       if (device_modes.size() < 2) {
-               _midnam_custom_device_mode_selector.hide();
-       } else {
-               _midnam_custom_device_mode_selector.show();
-               for (std::list<std::string>::const_iterator i = device_modes.begin();
-                    i != device_modes.end(); ++i) {
-                       _midnam_custom_device_mode_selector.append_text(*i);
-               }
+       for (std::list<std::string>::const_iterator i = device_modes.begin();
+            i != device_modes.end(); ++i) {
+               _midnam_custom_device_mode_selector.append_text(*i);
        }
 
        _midnam_custom_device_mode_selector.set_active(0);
@@ -454,10 +494,35 @@ MidiTimeAxisView::append_extra_display_menu_items ()
 
        items.push_back (MenuElem (_("Note Range"), *range_menu));
        items.push_back (MenuElem (_("Note Mode"), *build_note_mode_menu()));
+       items.push_back (MenuElem (_("Channel Selector"),
+                                  sigc::mem_fun(*this, &MidiTimeAxisView::toggle_channel_selector)));
 
+       color_mode_menu = build_color_mode_menu();
+       if (color_mode_menu) {
+               items.push_back (MenuElem (_("Color Mode"), *color_mode_menu));
+       }
+       
        items.push_back (SeparatorElem ());
 }
 
+void
+MidiTimeAxisView::toggle_channel_selector ()
+{
+       if (!_channel_selector) {
+               _channel_selector = new MidiChannelSelectorWindow (midi_track());
+
+               if (_color_mode == ChannelColors) {
+                       _channel_selector->set_channel_colors(CanvasNoteEvent::midi_channel_colors);
+               } else {
+                       _channel_selector->set_default_channel_color ();
+               }
+
+               _channel_selector->show_all ();
+       } else {
+               _channel_selector->cycle_visibility ();
+       }
+}
+
 void
 MidiTimeAxisView::build_automation_action_menu (bool for_selection)
 {
@@ -479,7 +544,7 @@ MidiTimeAxisView::build_automation_action_menu (bool for_selection)
 
        MenuList& automation_items = automation_action_menu->items();
 
-       uint16_t selected_channels = _channel_selector.get_selected_channels();
+       uint16_t selected_channels = midi_track()->get_playback_channel_mask();
 
        if (selected_channels !=  0) {
 
@@ -523,7 +588,7 @@ MidiTimeAxisView::build_automation_action_menu (bool for_selection)
 void
 MidiTimeAxisView::change_all_channel_tracks_visibility (bool yn, Evoral::Parameter param)
 {
-       const uint16_t selected_channels = _channel_selector.get_selected_channels();
+       const uint16_t selected_channels = midi_track()->get_playback_channel_mask();
 
        for (uint8_t chn = 0; chn < 16; chn++) {
                if (selected_channels & (0x0001 << chn)) {
@@ -550,7 +615,7 @@ MidiTimeAxisView::add_channel_command_menu_item (Menu_Helpers::MenuList& items,
           structure if there is more than 1 selected.
         */
 
-       const uint16_t selected_channels = _channel_selector.get_selected_channels();
+       const uint16_t selected_channels = midi_track()->get_playback_channel_mask();
        int chn_cnt = 0;
 
        for (uint8_t chn = 0; chn < 16; chn++) {
@@ -643,6 +708,135 @@ MidiTimeAxisView::add_channel_command_menu_item (Menu_Helpers::MenuList& items,
        }
 }
 
+/** Add a single menu item for a controller on one channel. */
+void
+MidiTimeAxisView::add_single_channel_controller_item(Menu_Helpers::MenuList& ctl_items,
+                                                     int                     ctl,
+                                                     const std::string&      name)
+{
+       using namespace Menu_Helpers;
+
+       const uint16_t selected_channels = midi_track()->get_playback_channel_mask();
+       for (uint8_t chn = 0; chn < 16; chn++) {
+               if (selected_channels & (0x0001 << chn)) {
+
+                       Evoral::Parameter fully_qualified_param (MidiCCAutomation, chn, ctl);
+                       ctl_items.push_back (
+                               CheckMenuElem (
+                                       string_compose ("<b>%1</b>: %2 [%3]", ctl, name, int (chn)),
+                                       sigc::bind (
+                                               sigc::mem_fun (*this, &RouteTimeAxisView::toggle_automation_track),
+                                               fully_qualified_param)));
+                       dynamic_cast<Label*> (ctl_items.back().get_child())->set_use_markup (true);
+
+                       boost::shared_ptr<AutomationTimeAxisView> track = automation_child (
+                               fully_qualified_param);
+
+                       bool visible = false;
+                       if (track) {
+                               if (track->marked_for_display()) {
+                                       visible = true;
+                               }
+                       }
+
+                       CheckMenuItem* cmi = static_cast<CheckMenuItem*>(&ctl_items.back());
+                       _controller_menu_map[fully_qualified_param] = cmi;
+                       cmi->set_active (visible);
+
+                       /* one channel only */
+                       break;
+               }
+       }
+}
+
+/** Add a submenu with 1 item per channel for a controller on many channels. */
+void
+MidiTimeAxisView::add_multi_channel_controller_item(Menu_Helpers::MenuList& ctl_items,
+                                                    int                     ctl,
+                                                    const std::string&      name)
+{
+       using namespace Menu_Helpers;
+
+       const uint16_t selected_channels = midi_track()->get_playback_channel_mask();
+
+       Menu* chn_menu = manage (new Menu);
+       MenuList& chn_items (chn_menu->items());
+
+       /* add a couple of items to hide/show this controller on all channels */
+
+       Evoral::Parameter param_without_channel (MidiCCAutomation, 0, ctl);
+       chn_items.push_back (
+               MenuElem (_("Hide all channels"),
+                         sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::change_all_channel_tracks_visibility),
+                                     false, param_without_channel)));
+       chn_items.push_back (
+               MenuElem (_("Show all channels"),
+                         sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::change_all_channel_tracks_visibility),
+                                     true, param_without_channel)));
+
+       for (uint8_t chn = 0; chn < 16; chn++) {
+               if (selected_channels & (0x0001 << chn)) {
+
+                       /* for each selected channel, add a menu item for this controller */
+
+                       Evoral::Parameter fully_qualified_param (MidiCCAutomation, chn, ctl);
+                       chn_items.push_back (
+                               CheckMenuElem (string_compose (_("Channel %1"), chn+1),
+                                              sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::toggle_automation_track),
+                                                          fully_qualified_param)));
+
+                       boost::shared_ptr<AutomationTimeAxisView> track = automation_child (
+                               fully_qualified_param);
+                       bool visible = false;
+
+                       if (track) {
+                               if (track->marked_for_display()) {
+                                       visible = true;
+                               }
+                       }
+
+                       CheckMenuItem* cmi = static_cast<CheckMenuItem*>(&chn_items.back());
+                       _controller_menu_map[fully_qualified_param] = cmi;
+                       cmi->set_active (visible);
+               }
+       }
+
+       /* add the per-channel menu to the list of controllers, with the name of the controller */
+       ctl_items.push_back (MenuElem (string_compose ("<b>%1</b>: %2", ctl, name),
+                                      *chn_menu));
+       dynamic_cast<Label*> (ctl_items.back().get_child())->set_use_markup (true);
+}
+
+boost::shared_ptr<MIDI::Name::CustomDeviceMode>
+MidiTimeAxisView::get_device_mode()
+{
+       using namespace MIDI::Name;
+
+       boost::shared_ptr<MasterDeviceNames> device_names = get_device_names();
+       if (!device_names) {
+               return boost::shared_ptr<MIDI::Name::CustomDeviceMode>();
+       }
+
+       return device_names->custom_device_mode_by_name(
+               gui_property (X_("midnam-custom-device-mode")));
+}
+
+boost::shared_ptr<MIDI::Name::MasterDeviceNames>
+MidiTimeAxisView::get_device_names()
+{
+       using namespace MIDI::Name;
+
+       const std::string model = gui_property (X_("midnam-model-name"));
+
+       boost::shared_ptr<MIDINameDocument> midnam = MidiPatchManager::instance()
+               .document_by_model(model);
+       if (midnam) {
+               return midnam->master_device_names(model);
+       } else {
+               return boost::shared_ptr<MasterDeviceNames>();
+       }
+}
+
 void
 MidiTimeAxisView::build_controller_menu ()
 {
@@ -661,7 +855,7 @@ MidiTimeAxisView::build_controller_menu ()
           combination covering the currently selected channels for this track
        */
 
-       const uint16_t selected_channels = _channel_selector.get_selected_channels();
+       const uint16_t selected_channels = midi_track()->get_playback_channel_mask();
 
        /* count the number of selected channels because we will build a different menu
           structure if there is more than 1 selected.
@@ -677,149 +871,77 @@ MidiTimeAxisView::build_controller_menu ()
        }
 
        using namespace MIDI::Name;
-       const Glib::ustring model = _midnam_model_selector.get_active_text();
-       boost::shared_ptr<MIDINameDocument> midnam = MidiPatchManager::instance()
-               .document_by_model(model);
-       boost::shared_ptr<MasterDeviceNames> device_names;
-       if (midnam && !midnam->master_device_names_by_model().empty()) {
-               device_names = boost::shared_ptr<MasterDeviceNames>(
-                       midnam->master_device_names_by_model().begin()->second);
-       }
+       boost::shared_ptr<MasterDeviceNames> device_names = get_device_names();
 
        if (device_names && !device_names->controls().empty()) {
-               /* Controllers names available from the midnam file,
-                  generate a custom controller menu */
+               /* Controllers names available in midnam file, generate fancy menu */
+               unsigned n_items  = 0;
+               unsigned n_groups = 0;
+
+               /* TODO: This is not correct, should look up the currently applicable ControlNameList
+                  and only build a menu for that one. */
                for (MasterDeviceNames::ControlNameLists::const_iterator l = device_names->controls().begin();
                     l != device_names->controls().end(); ++l) {
-                       boost::shared_ptr<ControlNameList> name_list = *l;
-
-                       int       chn        = 0;  // TODO
-                       Menu*     group_menu = manage(new Menu());
-                       MenuList& group_items(group_menu->items());
-
-                       for (ControlNameList::Controls::const_iterator c = (*l)->controls().begin();
-                            c != (*l)->controls().end(); ++c) {
-                               Evoral::Parameter fully_qualified_param(MidiCCAutomation, chn, atoi((*c)->number().c_str()));
-                               group_items.push_back(
-                                       CheckMenuElem(string_compose("<b>%1</b>: %2 [%3]",
-                                                                    (*c)->number(), (*c)->name(), int(chn)),
-                                                     sigc::bind(
-                                                             sigc::mem_fun(*this, &RouteTimeAxisView::toggle_automation_track),
-                                                             fully_qualified_param)));
-                               dynamic_cast<Label*> (group_items.back().get_child())->set_use_markup (true);
-                       }
-                       items.push_back(MenuElem(name_list->name(), *group_menu));
-               }
-               return;
-       }
-
-       /* loop over all 127 MIDI controllers, in groups of 16; except don't offer
-          bank select controllers, as they are handled by the `patch' code */
-
-       for (int i = 0; i < 127; i += 16) {
-
-               Menu* ctl_menu = manage (new Menu);
-               MenuList& ctl_items (ctl_menu->items());
-
-               /* for each controller, consider whether to create a submenu or a single item */
-
-               for (int ctl = i; ctl < i+16; ++ctl) {
-
-                       if (ctl == MIDI_CTL_MSB_BANK || ctl == MIDI_CTL_LSB_BANK) {
-                               continue;
-                       }
-
-                       if (chn_cnt > 1) {
-
-                               /* multiple channels - create a submenu, with 1 item per channel */
-
-                               Menu* chn_menu = manage (new Menu);
-                               MenuList& chn_items (chn_menu->items());
-
-                               /* add a couple of items to hide/show this controller on all channels */
-
-                               Evoral::Parameter param_without_channel (MidiCCAutomation, 0, ctl);
-                               chn_items.push_back (
-                                       MenuElem (_("Hide all channels"),
-                                                 sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::change_all_channel_tracks_visibility),
-                                                             false, param_without_channel)));
-                               chn_items.push_back (
-                                       MenuElem (_("Show all channels"),
-                                                 sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::change_all_channel_tracks_visibility),
-                                                             true, param_without_channel)));
-
-                               for (uint8_t chn = 0; chn < 16; chn++) {
-                                       if (selected_channels & (0x0001 << chn)) {
-
-                                               /* for each selected channel, add a menu item for this controller */
-
-                                               Evoral::Parameter fully_qualified_param (MidiCCAutomation, chn, ctl);
-                                               chn_items.push_back (
-                                                       CheckMenuElem (string_compose (_("Channel %1"), chn+1),
-                                                                      sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::toggle_automation_track),
-                                                                                  fully_qualified_param)));
-
-                                               boost::shared_ptr<AutomationTimeAxisView> track = automation_child (
-                                                       fully_qualified_param);
-                                               bool visible = false;
-
-                                               if (track) {
-                                                       if (track->marked_for_display()) {
-                                                               visible = true;
-                                                       }
-                                               }
-
-                                               CheckMenuItem* cmi = static_cast<CheckMenuItem*>(&chn_items.back());
-                                               _controller_menu_map[fully_qualified_param] = cmi;
-                                               cmi->set_active (visible);
+                       boost::shared_ptr<ControlNameList> name_list = l->second;
+                       Menu*                              ctl_menu  = NULL;
+                       
+                       for (ControlNameList::Controls::const_iterator c = name_list->controls().begin();
+                            c != name_list->controls().end();) {
+                               const uint16_t ctl = c->second->number();
+                               if (ctl != MIDI_CTL_MSB_BANK && ctl != MIDI_CTL_LSB_BANK) {
+                                       /* Skip bank select controllers since they're handled specially */
+                                       if (n_items == 0) {
+                                               /* Create a new submenu */
+                                               ctl_menu = manage (new Menu);
+                                       }
+                               
+                                       MenuList& ctl_items (ctl_menu->items());
+                                       if (chn_cnt > 1) {
+                                               add_multi_channel_controller_item(ctl_items, ctl, c->second->name());
+                                       } else {
+                                               add_single_channel_controller_item(ctl_items, ctl, c->second->name());
                                        }
                                }
 
-                               /* add the per-channel menu to the list of controllers, with the name of the controller */
-                               ctl_items.push_back (MenuElem (string_compose ("<b>%1</b>: %2", ctl, midi_name (ctl)),
-                                                              *chn_menu));
-                               dynamic_cast<Label*> (ctl_items.back().get_child())->set_use_markup (true);
-
-                       } else {
-
-                               /* just one channel - create a single menu item for this ctl+channel combination */
-
-                               for (uint8_t chn = 0; chn < 16; chn++) {
-                                       if (selected_channels & (0x0001 << chn)) {
-
-                                               Evoral::Parameter fully_qualified_param (MidiCCAutomation, chn, ctl);
-                                               ctl_items.push_back (
-                                                       CheckMenuElem (
-                                                               string_compose ("<b>%1</b>: %2 [%3]", ctl, midi_name (ctl), int (chn)),
-                                                               sigc::bind (
-                                                                       sigc::mem_fun (*this, &RouteTimeAxisView::toggle_automation_track),
-                                                                       fully_qualified_param)));
-                                               dynamic_cast<Label*> (ctl_items.back().get_child())->set_use_markup (true);
-
-                                               boost::shared_ptr<AutomationTimeAxisView> track = automation_child (
-                                                       fully_qualified_param);
-
-                                               bool visible = false;
-                                               if (track) {
-                                                       if (track->marked_for_display()) {
-                                                               visible = true;
-                                                       }
-                                               }
-
-                                               CheckMenuItem* cmi = static_cast<CheckMenuItem*>(&ctl_items.back());
-                                               _controller_menu_map[fully_qualified_param] = cmi;
-                                               cmi->set_active (visible);
-
-                                               /* one channel only */
-                                               break;
-                                       }
+                               ++c;
+                               if (ctl_menu && (++n_items == 16 || c == name_list->controls().end())) {
+                                       /* Submenu has 16 items or we're done, add it to controller menu and reset */
+                                       items.push_back(
+                                               MenuElem(string_compose(_("Controllers %1-%2"),
+                                                                       (16 * n_groups), (16 * n_groups) + n_items - 1),
+                                                        *ctl_menu));
+                                       ctl_menu = NULL;
+                                       n_items  = 0;
+                                       ++n_groups;
                                }
                        }
                }
+       } else {
+               /* No controllers names, generate generic numeric menu */
+               for (int i = 0; i < 127; i += 16) {
+                       Menu*     ctl_menu = manage (new Menu);
+                       MenuList& ctl_items (ctl_menu->items());
+
+                       for (int ctl = i; ctl < i+16; ++ctl) {
+                               if (ctl == MIDI_CTL_MSB_BANK || ctl == MIDI_CTL_LSB_BANK) {
+                                       /* Skip bank select controllers since they're handled specially */
+                                       continue;
+                               }
 
-               /* add the menu for this block of controllers to the overall controller menu */
+                               if (chn_cnt > 1) {
+                                       add_multi_channel_controller_item(
+                                               ctl_items, ctl, string_compose(_("Controller %1"), ctl));
+                               } else {
+                                       add_single_channel_controller_item(
+                                               ctl_items, ctl, string_compose(_("Controller %1"), ctl));
+                               }
+                       }
 
-               items.push_back (MenuElem (string_compose (_("Controllers %1-%2"), i, i+15), *ctl_menu));
+                       /* Add submenu for this block of controllers to controller menu */
+                       items.push_back (
+                               MenuElem (string_compose (_("Controllers %1-%2"), i, i + 15),
+                                         *ctl_menu));
+               }
        }
 }
 
@@ -911,10 +1033,12 @@ MidiTimeAxisView::set_color_mode (ColorMode mode, bool force, bool redisplay, bo
                        return;
                }
                
-               if (mode == ChannelColors) {
-                       _channel_selector.set_channel_colors(CanvasNoteEvent::midi_channel_colors);
-               } else {
-                       _channel_selector.set_default_channel_color();
+               if (_channel_selector) {
+                       if (mode == ChannelColors) {
+                               _channel_selector->set_channel_colors(CanvasNoteEvent::midi_channel_colors);
+                       } else {
+                               _channel_selector->set_default_channel_color();
+                       }
                }
                
                _color_mode = mode;
@@ -1097,7 +1221,7 @@ MidiTimeAxisView::set_note_selection (uint8_t note)
                return;
        }
 
-       uint16_t chn_mask = _channel_selector.get_selected_channels();
+       uint16_t chn_mask = midi_track()->get_playback_channel_mask();
 
        if (_view->num_selected_regionviews() == 0) {
                _view->foreach_regionview (
@@ -1117,7 +1241,7 @@ MidiTimeAxisView::add_note_selection (uint8_t note)
                return;
        }
 
-       const uint16_t chn_mask = _channel_selector.get_selected_channels();
+       const uint16_t chn_mask = midi_track()->get_playback_channel_mask();
 
        if (_view->num_selected_regionviews() == 0) {
                _view->foreach_regionview (
@@ -1137,7 +1261,7 @@ MidiTimeAxisView::extend_note_selection (uint8_t note)
                return;
        }
 
-       const uint16_t chn_mask = _channel_selector.get_selected_channels();
+       const uint16_t chn_mask = midi_track()->get_playback_channel_mask();
 
        if (_view->num_selected_regionviews() == 0) {
                _view->foreach_regionview (
@@ -1157,7 +1281,7 @@ MidiTimeAxisView::toggle_note_selection (uint8_t note)
                return;
        }
 
-       const uint16_t chn_mask = _channel_selector.get_selected_channels();
+       const uint16_t chn_mask = midi_track()->get_playback_channel_mask();
 
        if (_view->num_selected_regionviews() == 0) {
                _view->foreach_regionview (
@@ -1201,7 +1325,7 @@ MidiTimeAxisView::set_channel_mode (ChannelMode, uint16_t)
           the right ones.
        */
 
-       const uint16_t selected_channels = _channel_selector.get_selected_channels();
+       const uint16_t selected_channels = midi_track()->get_playback_channel_mask();
        bool changed = false;
 
        no_redraw = true;
@@ -1315,14 +1439,13 @@ MidiTimeAxisView::stop_step_editing ()
        }
 }
 
-
 /** @return channel (counted from 0) to add an event to, based on the current setting
  *  of the channel selector.
  */
 uint8_t
 MidiTimeAxisView::get_channel_for_add () const
 {
-       uint16_t const chn_mask = _channel_selector.get_selected_channels ();
+       uint16_t const chn_mask = midi_track()->get_playback_channel_mask();
        int chn_cnt = 0;
        uint8_t channel = 0;
 
@@ -1356,3 +1479,35 @@ MidiTimeAxisView::contents_height_changed ()
 {
        _range_scroomer->set_size_request (-1, _view->child_height ());
 }
+
+void
+MidiTimeAxisView::playback_channel_mode_changed ()
+{
+       switch (midi_track()->get_playback_channel_mode()) {
+       case AllChannels:
+               _playback_channel_status.set_markup (string_compose ("<b>%1</b>: <i>%2</i>", _("Play"), _("all")));
+               break;
+       case FilterChannels:
+               _playback_channel_status.set_markup (string_compose ("<b>%1</b>: <i>%2</i>", _("Play"), _("some")));
+               break;
+       case ForceChannel:
+               _playback_channel_status.set_markup (string_compose ("<b>%1</b>: <i>%2>%3</i>", _("Play"), _("all"), ffs (midi_track()->get_playback_channel_mask())));
+               break;
+       }
+}
+
+void
+MidiTimeAxisView::capture_channel_mode_changed ()
+{
+       switch (midi_track()->get_capture_channel_mode()) {
+       case AllChannels:
+               _capture_channel_status.set_markup (string_compose ("<b>%1</b>: <i>%2</i>", _("Rec"), _("all")));
+               break;
+       case FilterChannels:
+               _capture_channel_status.set_markup (string_compose ("<b>%1</b>: <i>%2</i>", _("Rec"), _("some")));
+               break;
+       case ForceChannel:
+               _capture_channel_status.set_markup (string_compose ("<b>%1</b>: <i>%2>%3</i>", _("Rec"), _("all"), ffs (midi_track()->get_capture_channel_mask())));
+               break;
+       }
+}