Merged with trunk R1612.
[ardour.git] / gtk2_ardour / connection_editor.cc
index 1f55ba0930bd0696727b0046a043a8c6511df198..79d7605497c4ce849cdd51c83f2fc108d6819cb2 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
+
+#include <map>
+#include <vector>
 #include <stdint.h>
 
 #include <gtkmm2ext/gtk_ui.h>
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
 using namespace Gtk;
 using namespace sigc;
 
 ConnectionEditor::ConnectionEditor ()
-       : ArdourDialog ("connection editor"),
-         input_connection_display (1),
-         output_connection_display (1),
+       : ArdourDialog (_("ardour: connections")),
          input_frame (_("Input Connections")),
          output_frame (_("Output Connections")),
          new_input_connection_button (_("New Input")),
@@ -136,23 +137,32 @@ ConnectionEditor::ConnectionEditor ()
        right_vbox.set_border_width (5);
        right_vbox.pack_start (port_and_selector_box);
 
-       input_connection_display.set_shadow_type (Gtk::SHADOW_IN);
-       input_connection_display.set_selection_mode (GTK_SELECTION_SINGLE);
+       input_connection_model = ListStore::create (connection_columns);
+       output_connection_model = ListStore::create (connection_columns);
+       
+       input_connection_display.set_model (input_connection_model);
+       output_connection_display.set_model (output_connection_model);
+
+       input_connection_display.append_column (_("Connections"), connection_columns.name);
+       output_connection_display.append_column (_("Connections"), connection_columns.name);
+
+       input_connection_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
        input_connection_display.set_size_request (80, -1);
        input_connection_display.set_name ("ConnectionEditorConnectionList");
-       input_connection_display.select_row.connect (bind (mem_fun(*this, &ConnectionEditor::connection_selected), true));
 
-       output_connection_display.set_shadow_type (Gtk::SHADOW_IN);
-       output_connection_display.set_selection_mode (GTK_SELECTION_SINGLE);
+       output_connection_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
        output_connection_display.set_size_request (80, -1);
        output_connection_display.set_name ("ConnectionEditorConnectionList");
-       output_connection_display.select_row.connect (bind (mem_fun(*this, &ConnectionEditor::connection_selected), false));
+
+       input_connection_display.get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::selection_changed), &input_connection_display));
+       output_connection_display.get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::selection_changed), &output_connection_display));
+
 
        input_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
        output_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
 
-       input_scroller.add_with_viewport (input_connection_display);
-       output_scroller.add_with_viewport (output_connection_display);
+       input_scroller.add (input_connection_display);
+       output_scroller.add (output_connection_display);
 
        input_box.set_border_width (5);
        input_box.set_spacing (5);
@@ -183,11 +193,8 @@ ConnectionEditor::ConnectionEditor ()
        main_vbox.pack_start (main_hbox);
        main_vbox.pack_start (button_frame, false, false);
 
-       set_title (_("ardour: connections"));
-       add (main_vbox);
-
-       delete_event.connect (bind (ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
-
+       get_vbox()->pack_start (main_vbox);
+       
        clear_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::clear));
        add_port_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::add_port));
        new_input_connection_button.signal_clicked().connect (bind (mem_fun(*this, &ConnectionEditor::new_connection), true));
@@ -242,55 +249,56 @@ ConnectionEditor::clear ()
        }
 }
 
-gint
-ConnectionEditor::on_map (GdkEventAny *ev)
+void
+ConnectionEditor::on_map ()
 {
        refill_connection_display ();
-       return Window::on_map (ev);
+       Window::on_map ();
 }
 
 void
 ConnectionEditor::add_connection (ARDOUR::Connection *connection)
 {
-       using namespace CList_Helpers;
-       const char *rowtext[1];
-
-       rowtext[0] = connection->name().c_str();
+       TreeModel::Row row;
 
        if (dynamic_cast<InputConnection *> (connection)) {
+
                if (push_at_front) {
-                       input_connection_display.rows().push_front (rowtext);
-                       input_connection_display.rows().front().set_data (connection);
+                       row = *(input_connection_model->prepend());
                } else {
-                       input_connection_display.rows().push_back (rowtext);
-                       input_connection_display.rows().back().set_data (connection);
+                       row = *(input_connection_model->append());
                }
+
        } else {
+
                if (push_at_front) {
-                       output_connection_display.rows().push_front (rowtext);
-                       output_connection_display.rows().front().set_data (connection);
+                       row = *(output_connection_model->prepend());
                } else {
-                       output_connection_display.rows().push_back (rowtext);
-                       output_connection_display.rows().back().set_data (connection);
+                       row = *(output_connection_model->append());
                }
        }
+
+       row[connection_columns.connection] = connection;
+       row[connection_columns.name] = connection->name();
 }
 
 void
 ConnectionEditor::remove_connection (ARDOUR::Connection *connection)
 {
-       using namespace Gtk::CList_Helpers;
-       RowList::iterator i;
-       RowList* rlist;
+       TreeModel::iterator i;
+       Glib::RefPtr<TreeModel> model = input_connection_model;
 
-       if (dynamic_cast<InputConnection *> (connection)) {
-               rlist = &input_connection_display.rows();
-       } else {
-               rlist = &output_connection_display.rows();
+       if (dynamic_cast<InputConnection *> (connection) == 0) {
+               model = output_connection_model;
        }
 
-       if ((i = rlist->find_data (connection)) != rlist->end()) {
-               rlist->erase (i);
+       TreeModel::Children rows = model->children();
+
+       for (i = rows.begin(); i != rows.end(); ++i) {
+               if ((*i)[connection_columns.connection] == connection) {
+                       // model->erase (i);
+                       break;
+               }
        }
 }
 
@@ -311,39 +319,48 @@ ConnectionEditor::add_connection_and_select (ARDOUR::Connection *connection)
 {
        add_connection (connection);
 
-       if (dynamic_cast<InputConnection *> (connection)) {
-               input_connection_display.rows().front().select ();
-       } else {
-               output_connection_display.rows().front().select ();
-       }
+       // GTK2FIX
+       // if (dynamic_cast<InputConnection *> (connection)) {
+       // input_connection_display.rows().front().select ();
+        // } else {
+       //      output_connection_display.rows().front().select ();
+       //}
 }
 
 void
 ConnectionEditor::refill_connection_display ()
 {
-       input_connection_display.clear();
-       output_connection_display.clear();
+       input_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
+       output_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
+
+       input_connection_model->clear();
+       output_connection_model->clear();
 
        current_connection = 0;
        
        if (session) {
                session->foreach_connection (this, &ConnectionEditor::add_connection);
        }
+
+       input_connection_display.set_model (input_connection_model);
+       output_connection_display.set_model (output_connection_model);
+
 }
        
 void
-ConnectionEditor::connection_selected (gint row, gint col, GdkEvent *ev, bool input)
+ConnectionEditor::selection_changed (TreeView* view)
 {
        ARDOUR::Connection *old_current = current_connection;
 
+       TreeIter iter;
+       TreeModel::Path path;
+       Glib::RefPtr<TreeView::Selection> selection = view->get_selection();
+       Glib::RefPtr<TreeModel> model = view->get_model();
+       bool input = (view == &input_connection_display);
 
-       if (input) {
-               output_connection_display.unselect_all ();
-               current_connection = reinterpret_cast<ARDOUR::Connection*> (input_connection_display.row(row).get_data());
-       } else {
-               input_connection_display.unselect_all ();
-               current_connection = reinterpret_cast<ARDOUR::Connection*> (output_connection_display.row(row).get_data());
-       }
+       iter = model->get_iter (path);
+       
+       current_connection = (*iter)[connection_columns.connection];
 
        if (old_current != current_connection) {
                config_connection.disconnect ();
@@ -356,7 +373,7 @@ ConnectionEditor::connection_selected (gint row, gint col, GdkEvent *ev, bool in
                connect_connection = current_connection->ConnectionsChanged.connect 
                        (bind (mem_fun(*this, &ConnectionEditor::connections_changed), input));
        }
-
+       
        display_connection_state (input);
        display_ports ();
 }
@@ -381,9 +398,8 @@ ConnectionEditor::display_ports ()
        }
        
        using namespace Notebook_Helpers;
-       using namespace CList_Helpers;
 
-       typedef map<string,vector<pair<string,string> > > PortMap;
+       typedef std::map<std::string,std::vector<std::pair<std::string,std::string> > > PortMap;
        PortMap portmap;
        const char **ports;
        PageList& pages = notebook.pages();
@@ -391,7 +407,7 @@ ConnectionEditor::display_ports ()
        vector<string> rowdata;
        bool for_input;
 
-       current_page = notebook.get_current_page_num ();
+       current_page = notebook.get_current_page ();
        pages.clear ();
 
        /* get relevant current JACK ports */
@@ -434,33 +450,35 @@ ConnectionEditor::display_ports ()
        for (i = portmap.begin(); i != portmap.end(); ++i) {
                
                Box *client_box = manage (new VBox);
-               Gtk::CList *client_port_display = manage (new Gtk::CList (1));
+               Gtk::CTreeView *display = manage (new Gtk::TreeView);
+               RefPtr<TreeModel> model = TreeModel::create (columns);
                ScrolledWindow *scroller = manage (new ScrolledWindow);
 
-               scroller->add_with_viewport (*client_port_display);
-               scroller->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
-
-               client_box->pack_start (*scroller);
-
-               client_port_display->set_selection_mode (GTK_SELECTION_BROWSE);
-               client_port_display->set_name ("ConnectionEditorList");
+               display->set_selection_mode (GTK_SELECTION_SINGLE);
+               display->set_name ("ConnectionEditorList");
 
                for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
                        
-                       rowdata.clear ();
-                       rowdata.push_back (s->first);
-                       client_port_display->rows().push_back (rowdata);
-                       client_port_display->rows().back().set_data (g_strdup (s->second.c_str()), free);
+                       Row row = model->append ();
+
+                       row[displayed_name] = s->first;
+                       row[full_name] = s->second;
                }
 
-               client_port_display->columns_autosize ();
-               client_port_display->select_row.connect (bind (mem_fun(*this, &ConnectionEditor::port_selection_handler), client_port_display));
+               display->get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::port_selection_handler), display));
                
                Label *tab_label = manage (new Label);
 
                tab_label->set_name ("ConnectionEditorNotebookTab");
                tab_label->set_text ((*i).first);
 
+               display->set_model (model);
+
+               scroller->add (*client_port_display);
+               scroller->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
+
+               client_box->pack_start (*scroller);
+
                pages.push_back (TabElem (*client_box, *tab_label));
        }
 
@@ -472,7 +490,7 @@ ConnectionEditor::display_ports ()
 void
 ConnectionEditor::display_connection_state (bool for_input)
 {
-       LockMonitor lm (port_display_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm  (port_display_lock);
        uint32_t limit;
 
        if (session == 0 || current_connection == 0) {
@@ -515,23 +533,25 @@ ConnectionEditor::display_connection_state (bool for_input)
                        snprintf(buf, sizeof(buf)-1, _("out %d"), n+1);
                }
                        
-               title[0] = buf;
-               clist = manage (new CList (1, title));
-               scroller = new ScrolledWindow;
+               tview = manage (new TreeView());
+               Glib::RefPtr<ListStore> port_model = ListStore::create (*port_display_columns);
+               
+               tview->set_model (port_model);
+               tview->append_column (_(buf), port_display_columns->name);
+               tview->set_selection()->set_mode (Gtk::SELECTION_SINGLE);
+               tview->set_data ("port", (gpointer) ((intptr_t) n));
+               tview->set_headers_visible (true);
+               tview->set_name ("ConnectionEditorPortList");
+               tview->signal_button_press_event().connect (bind (mem_fun(*this, &ConnectionEditor::port_column_click), clist));
+
+               scroller = manage (new ScrolledWindow);
                
-               scroller->add_with_viewport (*clist);
+               scroller->add (*tview);
                scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
 
                port_displays.insert (port_displays.end(), scroller);
                port_box.pack_start (*scroller);
 
-               clist->set_data ("port", (gpointer) ((intptr_t) n));
-
-               clist->set_name ("ConnectionEditorPortList");
-               clist->click_column.connect (bind (mem_fun(*this, &ConnectionEditor::port_column_click), clist));
-               clist->set_selection_mode (GTK_SELECTION_SINGLE);
-               clist->set_shadow_type (Gtk::SHADOW_IN);
-
                scroller->set_size_request (-1, 75);
 
                /* now fill the clist with the current connections */
@@ -539,25 +559,30 @@ ConnectionEditor::display_connection_state (bool for_input)
                const ARDOUR::Connection::PortList& connections = current_connection->port_connections (n);
        
                for (ARDOUR::Connection::PortList::const_iterator i = connections.begin(); i != connections.end(); ++i) {
-                       const gchar *data[1];
-                       
-                       data[0] = (*i).c_str();
-                       clist->rows().push_back (data);
-               }
 
-               clist->columns_autosize ();
-               clist->button_release_event.connect (bind (mem_fun(*this, &ConnectionEditor::port_button_event), clist));
+                       TreeModel::Row row = *(model->append());
+
+                       row[port_connection_columns.name] = (*i)->name();
+               }
        }
 
        port_box.show_all ();
 }
 
 void
-ConnectionEditor::port_selection_handler (gint row, gint col, GdkEvent *ev, Gtk::CList *clist)
+ConnectionEditor::port_selection_changed (TreeView* tview)
 {
-       using namespace CList_Helpers;
+       Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
+       TreeModel::iterator iter = sel->get_selected();
+
+       if (!current_connection) {
+               return;
+       }
+
+       if (iter) {
+               TreeModel::Row row = *iter;
+               string other_port_name = row[port_display_columns.full_name];
 
-       string other_port_name = (char *) clist->rows()[row].get_data();
        
        if (current_connection && selected_port >= 0) {
                current_connection->add_connection (selected_port, other_port_name);
@@ -574,29 +599,24 @@ ConnectionEditor::add_port ()
 }
 
 void
-ConnectionEditor::port_column_click (gint col, CList *clist)
+ConnectionEditor::connection_port_button_press_event (GdkEventButton* ev, TreeView* tview)
 {
-       /* Gack. CList's don't respond visually to a change
-          in their state, so rename them to force a style
-          switch.
-       */
+       Glib::Mutex::Lock lm  (port_display_lock);
 
-       LockMonitor lm (port_display_lock, __LINE__, __FILE__);
-
-       int which_port = reinterpret_cast<intptr_t> (clist->get_data ("port"));
+       int which_port = reinterpret_cast<intptr_t> (treeview->get_data ("port"));
 
        if (which_port != selected_port) {
                
                selected_port = which_port;
                display_ports ();
 
-               clist->set_name ("ConnectionEditorPortListSelected");
+               tview->set_name ("ConnectionEditorPortListSelected");
 
                for (slist<ScrolledWindow *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
 
                        Widget *child = (*i)->get_child();
 
-                       if (static_cast<CList *> (child) != clist) {
+                       if (static_cast<TreeView *> (child) != tview) {
                                child->set_name ("ConnectionEditorPortList");
                                child->queue_draw ();
                        }
@@ -610,23 +630,25 @@ ConnectionEditor::port_column_click (gint col, CList *clist)
        }
 }
 
-gint
-ConnectionEditor::connection_click (GdkEventButton *ev, CList *clist)
+void
+ConnectionEditor::connection_selection_changed (TreeView* tview);
 {
-       gint row, col;
+       Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
+       TreeModel::iterator iter = sel->get_selected();
 
-       if (clist->get_selection_info ((int)ev->x, (int)ev->y, &row, &col) == 0) {
-               return FALSE;
+       if (iter) {
+               TreeModel::Row row = *iter;
+               current_connection = row[XXXX_display_columns.connection];
+       } else {
+               current_connection = 0;
        }
-
-       current_connection = reinterpret_cast<ARDOUR::Connection *> (clist->row(row).get_data ());
-
-       return TRUE;
 }
 
 void
 ConnectionEditor::new_connection (bool for_input)
 {
+       string name;
+
        if (session == 0) {
                return;
        }
@@ -634,16 +656,11 @@ ConnectionEditor::new_connection (bool for_input)
        ArdourPrompter prompter (true);
        prompter.set_prompt (_("Name for new connection:"));
        prompter.done.connect (Gtk::Main::quit.slot());
-       prompter.show_all();
-
-       Gtk::Main::run();
 
-       if (prompter.status == Gtkmm2ext::Prompter::entered) {
-               string name;
+       switch (prompter.run()) {
+       case Gtk::RESPONSE_ACCEPT:
                prompter.get_result (name);
-
                push_at_front = true;
-
                if (name.length()) {
                        if (for_input) {
                                session->add_connection (new ARDOUR::InputConnection (name));
@@ -652,6 +669,10 @@ ConnectionEditor::new_connection (bool for_input)
                        }
                }
                push_at_front = false;
+               break;
+
+       default:
+               break;
        }
 }
 
@@ -665,28 +686,35 @@ ConnectionEditor::delete_connection ()
 }
 
 gint
-ConnectionEditor::port_button_event (GdkEventButton *ev, CList *clist)
+ConnectionEditor::port_button_event (GdkEventButton *ev, Treeview* treeview)
 {
        int row, col;
+       TreeIter iter;
+       TreeModel::Path path;
+       TreeViewColumn* column;
+       int cellx;
+       int celly;
 
        if (current_connection == 0) {
-               return FALSE;
+               return false;
        }
 
-       if (clist->get_selection_info ((int) ev->x, (int) ev->y, &row, &col) == 0) {
-               return FALSE;
+       if (!(Keyboard::is_delete_event (ev))) {
+               return false;
        }
 
-       if (!(Keyboard::is_delete_event (ev))) {
-               return FALSE;
+       if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
+               return false;
        }
 
-       string port_name = clist->cell (row, col).get_text ();
-       int which_port = (intptr_t) clist->get_data ("port");
-       
-       current_connection->remove_connection (which_port, port_name);
+       if ((iter = treeview->get_model()->get_iter (path))) {
+               /* path is valid */
+               
+               string port_name = (*iter)[columns.full_name];
+               int which_port = (intptr_t) treeview->get_data ("port");        
 
-       return TRUE;
+               current_connection->remove_connection (which_port, port_name);
+       }
+
+       return true;
 }
-       
-