use system/gtk default font on OS X, rather than specifying Lucida Grande; make large...
[ardour.git] / gtk2_ardour / mixer_ui.cc
index 295e956060c63cb11fd3168ba95479c558cdc8da..474cef239517fe67881710697fc72154448e82cf 100644 (file)
@@ -47,6 +47,7 @@
 #include "mixer_strip.h"
 #include "monitor_section.h"
 #include "plugin_selector.h"
+#include "public_editor.h"
 #include "ardour_ui.h"
 #include "prompter.h"
 #include "utils.h"
@@ -65,9 +66,25 @@ using namespace std;
 
 using PBD::atoi;
 
+Mixer_UI* Mixer_UI::_instance = 0;
+
+Mixer_UI*
+Mixer_UI::instance () 
+{
+       if (!_instance) {
+               _instance  = new Mixer_UI;
+       } 
+
+       return _instance;
+}
+
 Mixer_UI::Mixer_UI ()
        : Window (Gtk::WINDOW_TOPLEVEL)
+       , _following_editor_selection (false)
 {
+       /* allow this window to become the key focus window */
+       set_flags (CAN_FOCUS);
+
        _strip_width = Config->get_default_narrow_ms() ? Narrow : Wide;
        track_menu = 0;
         _monitor_section = 0;
@@ -80,6 +97,7 @@ Mixer_UI::Mixer_UI ()
 
        Route::SyncOrderKeys.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::sync_order_keys, this, _1), gui_context());
 
+       scroller_base.set_flags (Gtk::CAN_FOCUS);
        scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
        scroller_base.set_name ("MixerWindow");
        scroller_base.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
@@ -173,8 +191,8 @@ Mixer_UI::Mixer_UI ()
 #else
        global_hpacker.pack_start (out_packer, false, false, 12);
 #endif
-       list_hpane.add1(list_vpacker);
-       list_hpane.add2(global_hpacker);
+       list_hpane.pack1(list_vpacker, true, true);
+       list_hpane.pack2(global_hpacker, true, false);
 
        rhs_pane1.signal_size_allocate().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::pane_allocation_handler),
                                                        static_cast<Gtk::Paned*> (&rhs_pane1)));
@@ -186,21 +204,15 @@ Mixer_UI::Mixer_UI ()
        add (global_vpacker);
        set_name ("MixerWindow");
 
-       WindowTitle title(Glib::get_application_name());
-       title += _("Mixer");
-       set_title (title.get_string());
+       update_title ();
 
        set_wmclass (X_("ardour_mixer"), PROGRAM_NAME);
 
-       add_accel_group (ActionManager::ui_manager->get_accel_group());
-
        signal_delete_event().connect (sigc::mem_fun (*this, &Mixer_UI::hide_window));
        add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
 
        signal_configure_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
 
-       _selection.RoutesChanged.connect (sigc::mem_fun(*this, &Mixer_UI::follow_strip_selection));
-
        route_group_display_button_box->show();
        route_group_add_button->show();
        route_group_remove_button->show();
@@ -232,7 +244,7 @@ Mixer_UI::Mixer_UI ()
         MonitorSection::setup_knob_images ();
 
 #ifndef DEFER_PLUGIN_SELECTOR_LOAD
-       _plugin_selector = new PluginSelector (PluginManager::the_manager ());
+       _plugin_selector = new PluginSelector (PluginManager::instance ());
 #endif
 }
 
@@ -240,6 +252,13 @@ Mixer_UI::~Mixer_UI ()
 {
 }
 
+void
+Mixer_UI::track_editor_selection ()
+{
+       PublicEditor::instance().get_selection().TracksChanged.connect (sigc::mem_fun (*this, &Mixer_UI::follow_editor_selection));
+}
+
+
 void
 Mixer_UI::ensure_float (Window& win)
 {
@@ -265,8 +284,14 @@ Mixer_UI::show_window ()
                for (ri = rows.begin(); ri != rows.end(); ++ri) {
                        ms = (*ri)[track_columns.strip];
                        ms->set_width_enum (ms->get_width_enum (), ms->width_owner());
+                       /* Fix visibility of mixer strip stuff */
+                       ms->parameter_changed (X_("mixer-strip-visibility"));
                }
        }
+       
+       /* force focus into main area */
+       scroller_base.grab_focus ();
+
        _visible = true;
 }
 
@@ -330,7 +355,7 @@ Mixer_UI::add_strip (RouteList& routes)
 
                TreeModel::Row row = *(track_model->append());
                row[track_columns.text] = route->name();
-               row[track_columns.visible] = strip->marked_for_display();
+               row[track_columns.visible] = strip->route()->is_master() ? true : strip->marked_for_display();
                row[track_columns.route] = route;
                row[track_columns.strip] = strip;
 
@@ -361,8 +386,6 @@ Mixer_UI::remove_strip (MixerStrip* strip)
 
        ENSURE_GUI_THREAD (*this, &Mixer_UI::remove_strip, strip);
 
-       cerr << "Mixer UI removing strip for " << strip << endl;
-
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator ri;
        list<MixerStrip *>::iterator i;
@@ -430,30 +453,104 @@ Mixer_UI::sync_order_keys (string const & src)
 }
 
 void
-Mixer_UI::follow_strip_selection ()
+Mixer_UI::follow_editor_selection ()
+{
+       if (!Config->get_link_editor_and_mixer_selection() || _following_editor_selection) {
+               return;
+       }
+
+       _following_editor_selection = true;
+       _selection.block_routes_changed (true);
+       
+       TrackSelection& s (PublicEditor::instance().get_selection().tracks);
+
+       _selection.clear_routes ();
+
+       for (TrackViewList::iterator i = s.begin(); i != s.end(); ++i) {
+               RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*i);
+               if (rtav) {
+                       MixerStrip* ms = strip_by_route (rtav->route());
+                       if (ms) {
+                               _selection.add (ms);
+                       }
+               }
+       }
+
+       _following_editor_selection = false;
+       _selection.block_routes_changed (false);
+}
+
+
+MixerStrip*
+Mixer_UI::strip_by_route (boost::shared_ptr<Route> r)
 {
        for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-               (*i)->set_selected (_selection.selected ((*i)->route()));
+               if ((*i)->route() == r) {
+                       return (*i);
+               }
        }
+
+       return 0;
 }
 
 bool
 Mixer_UI::strip_button_release_event (GdkEventButton *ev, MixerStrip *strip)
 {
        if (ev->button == 1) {
-
-               /* this allows the user to click on the strip to terminate comment
-                  editing. XXX it needs improving so that we don't select the strip
-                  at the same time.
-               */
-
-               if (_selection.selected (strip->route())) {
-                       _selection.remove (strip->route());
+               if (_selection.selected (strip)) {
+                       /* primary-click: toggle selection state of strip */
+                       if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
+                               _selection.remove (strip);
+                       } 
                } else {
-                       if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
-                               _selection.add (strip->route());
+                       if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
+                               _selection.add (strip);
+                       } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
+
+                               if (!_selection.selected(strip)) {
+                               
+                                       /* extend selection */
+                                       
+                                       vector<MixerStrip*> tmp;
+                                       bool accumulate = false;
+                                       
+                                       tmp.push_back (strip);
+
+                                       for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
+                                               if ((*i) == strip) {
+                                                       /* hit clicked strip, start accumulating till we hit the first 
+                                                          selected strip
+                                                       */
+                                                       if (accumulate) {
+                                                               /* done */
+                                                               break;
+                                                       } else {
+                                                               accumulate = true;
+                                                       }
+                                               } else if (_selection.selected (*i)) {
+                                                       /* hit selected strip. if currently accumulating others,
+                                                          we're done. if not accumulating others, start doing so.
+                                                       */
+                                                       if (accumulate) {
+                                                               /* done */
+                                                               break;
+                                                       } else {
+                                                               accumulate = true;
+                                                       }
+                                               } else {
+                                                       if (accumulate) {
+                                                               tmp.push_back (*i);
+                                                       }
+                                               }
+                                       }
+
+                                       for (vector<MixerStrip*>::iterator i = tmp.begin(); i != tmp.end(); ++i) {
+                                               _selection.add (*i);
+                                       }
+                               }
+
                        } else {
-                               _selection.set (strip->route());
+                               _selection.set (strip);
                        }
                }
        }
@@ -479,11 +576,7 @@ Mixer_UI::set_session (Session* sess)
        XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
        set_state (*node);
 
-       WindowTitle title(_session->name());
-       title += _("Mixer");
-       title += Glib::get_application_name();
-
-       set_title (title.get_string());
+       update_title ();
 
        initial_track_display ();
 
@@ -492,6 +585,8 @@ Mixer_UI::set_session (Session* sess)
        _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
        _session->route_groups_reordered.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
        _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), ui_bind (&Mixer_UI::parameter_changed, this, _1), gui_context());
+       _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
+       _session->StateSaved.connect (_session_connections, invalidator (*this), ui_bind (&Mixer_UI::update_title, this), gui_context());
 
        Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
 
@@ -512,7 +607,7 @@ Mixer_UI::session_going_away ()
        _in_group_rebuild_or_clear = true;
        group_model->clear ();
        _in_group_rebuild_or_clear = false;
-       
+
        _selection.clear ();
        track_model->clear ();
 
@@ -526,13 +621,12 @@ Mixer_UI::session_going_away ()
 
        strips.clear ();
 
-       WindowTitle title(Glib::get_application_name());
-       title += _("Mixer");
-       set_title (title.get_string());
-
        stop_updating ();
 
        SessionHandlePtr::session_going_away ();
+
+       _session = 0;
+       update_title ();
 }
 
 void
@@ -747,15 +841,14 @@ Mixer_UI::redisplay_track_list ()
                        continue;
                }
 
-               bool visible = (*i)[track_columns.visible];
-
-               if (visible) {
-                       strip->set_marked_for_display (true);
+               if (!strip_redisplay_does_not_reset_order_keys) {
                        strip->route()->set_order_key (N_("signal"), order);
+               }
 
-                       if (!strip_redisplay_does_not_reset_order_keys) {
-                               strip->route()->set_order_key (N_("signal"), order);
-                       }
+               bool const visible = (*i)[track_columns.visible];
+
+               if (visible) {
+                       strip->set_gui_property ("visible", true);
 
                        if (strip->packed()) {
 
@@ -773,12 +866,11 @@ Mixer_UI::redisplay_track_list ()
                                        strip_packer.pack_start (*strip, false, false);
                                }
                                strip->set_packed (true);
-                               //strip->show();
                        }
 
                } else {
 
-                       strip->set_marked_for_display (false);
+                       strip->set_gui_property ("visible", false);
 
                        if (strip->route()->is_master() || strip->route()->is_monitor()) {
                                /* do nothing, these cannot be hidden */
@@ -797,8 +889,9 @@ Mixer_UI::redisplay_track_list ()
 
        // Resigc::bind all of the midi controls automatically
 
-       if (auto_rebinding)
+       if (auto_rebinding) {
                auto_rebind_midi_controls ();
+       }
 
        _group_tabs->set_dirty ();
 }
@@ -1133,7 +1226,7 @@ void
 Mixer_UI::new_route_group ()
 {
        RouteList rl;
-       
+
        _group_tabs->run_new_group_dialog (rl);
 }
 
@@ -1462,21 +1555,13 @@ Mixer_UI::pane_allocation_handler (Allocation&, Gtk::Paned* which)
        char buf[32];
        XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
        XMLNode* geometry;
-       int width, height;
+       int height;
        static int32_t done[3] = { 0, 0, 0 };
 
-       width = default_width;
        height = default_height;
 
        if ((geometry = find_named_node (*node, "geometry")) != 0) {
 
-
-               if ((prop = geometry->property ("x_size")) == 0) {
-                       prop = geometry->property ("x-size");
-               }
-               if (prop) {
-                       width = atoi (prop->value());
-               }
                if ((prop = geometry->property ("y_size")) == 0) {
                        prop = geometry->property ("y-size");
                }
@@ -1521,7 +1606,7 @@ Mixer_UI::pane_allocation_handler (Allocation&, Gtk::Paned* which)
        }
 }
 void
-Mixer_UI::scroll_left () 
+Mixer_UI::scroll_left ()
 {
        Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
        /* stupid GTK: can't rely on clamping across versions */
@@ -1539,29 +1624,40 @@ Mixer_UI::scroll_right ()
 bool
 Mixer_UI::on_key_press_event (GdkEventKey* ev)
 {
-       switch (ev->keyval) {
-       case GDK_Left:
-               scroll_left ();
-               return true;
+        /* focus widget gets first shot, then bindings, otherwise
+           forward to main window
+        */
 
-       case GDK_Right:
-               scroll_right ();
+       if (gtk_window_propagate_key_event (GTK_WINDOW(gobj()), ev)) {
                return true;
-
-       default:
-               break;
        }
-
-       return key_press_focus_accelerator_handler (*this, ev);
+       
+       KeyboardKey k (ev->state, ev->keyval);
+       
+       if (bindings.activate (k, Bindings::Press)) {
+               return true;
+       }
+               
+        return forward_key_press (ev);
 }
 
 bool
 Mixer_UI::on_key_release_event (GdkEventKey* ev)
 {
-       return Gtk::Window::on_key_release_event (ev);
-       // return key_press_focus_accelerator_handler (*this, ev);
-}
+       if (gtk_window_propagate_key_event (GTK_WINDOW(gobj()), ev)) {
+               return true;
+       }
 
+       KeyboardKey k (ev->state, ev->keyval);
+       
+       if (bindings.activate (k, Bindings::Release)) {
+               return true;
+       }
+
+        /* don't forward releases */
+
+        return true;
+}
 
 bool
 Mixer_UI::on_scroll_event (GdkEventScroll* ev)
@@ -1622,7 +1718,7 @@ Mixer_UI::plugin_selector()
 {
 #ifdef DEFER_PLUGIN_SELECTOR_LOAD
        if (!_plugin_selector)
-               _plugin_selector = new PluginSelector (PluginManager::the_manager ());
+               _plugin_selector = new PluginSelector (PluginManager::instance());
 #endif
 
        return _plugin_selector;
@@ -1668,7 +1764,7 @@ Mixer_UI::setup_track_display ()
        b->add (*w);
 
        b->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_track_or_bus));
-       
+
        v->pack_start (*b, false, false);
 
        track_display_frame.set_name("BaseFrame");
@@ -1686,3 +1782,71 @@ Mixer_UI::new_track_or_bus ()
        ARDOUR_UI::instance()->add_route (this);
 }
 
+
+void
+Mixer_UI::update_title ()
+{
+       if (_session) {
+               string n;
+               
+               if (_session->snap_name() != _session->name()) {
+                       n = _session->snap_name ();
+               } else {
+                       n = _session->name ();
+               }
+
+               if (_session->dirty ()) {
+                       n = "*" + n;
+               }
+               
+               WindowTitle title (n);
+               title += S_("Window|Mixer");
+               title += Glib::get_application_name ();
+               set_title (title.get_string());
+
+       } else {
+               
+               WindowTitle title (S_("Window|Mixer"));
+               title += Glib::get_application_name ();
+               set_title (title.get_string());
+       }
+}
+
+MixerStrip*
+Mixer_UI::strip_by_x (int x)
+{
+       for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
+               int x1, x2, y;
+
+               (*i)->translate_coordinates (*this, 0, 0, x1, y);
+               x2 = x1 + (*i)->get_width();
+
+               if (x >= x1 && x <= x2) {
+                       return (*i);
+               }
+       }
+
+       return 0;
+}
+
+void
+Mixer_UI::set_route_targets_for_operation ()
+{
+       _route_targets.clear ();
+
+       if (!_selection.empty()) {
+               _route_targets = _selection.routes;
+               return;
+       }
+
+       /* try to get mixer strip at mouse */
+
+       int x, y;
+       get_pointer (x, y);
+       
+       MixerStrip* ms = strip_by_x (x);
+       
+       if (ms) {
+               _route_targets.insert (ms);
+       }
+}