(merge from 2.0-ongoing -r1911:1912) fix audio clock handling of key press; fix crash...
[ardour.git] / gtk2_ardour / editor_region_list.cc
index ccfa1c378a95fbfbd4a3486dee73d55d2c62601f..8a50332193400448173cfba7c0b5392dae996abe 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <cstdlib>
 #include <cmath>
 #include <algorithm>
 #include <string>
+#include <sstream>
 
 #include <pbd/basename.h>
 
 #include <ardour/audioregion.h>
-#include <ardour/audiosource.h>
+#include <ardour/audiofilesource.h>
+#include <ardour/silentfilesource.h>
 #include <ardour/session_region.h>
 
 #include <gtkmm2ext/stop_signal.h>
 
 #include "editor.h"
 #include "editing.h"
+#include "keyboard.h"
 #include "ardour_ui.h"
 #include "gui_thread.h"
 #include "actions.h"
@@ -48,22 +50,27 @@ using namespace Glib;
 using namespace Editing;
 
 void
-Editor::handle_region_removed (boost::shared_ptr<Region> region)
+Editor::handle_region_removed (boost::weak_ptr<Region> wregion)
 {
-       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::handle_region_removed), region));
+       ENSURE_GUI_THREAD (mem_fun (*this, &Editor::redisplay_regions));
        redisplay_regions ();
 }
 
 void
-Editor::handle_new_region (boost::shared_ptr<Region> region)
+Editor::handle_new_region (boost::weak_ptr<Region> wregion)
 {
-       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::handle_new_region), region));
+       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::handle_new_region), wregion));
 
        /* don't copy region - the one we are being notified
           about belongs to the session, and so it will
           never be edited.
        */
-       add_region_to_region_display (region);
+
+       boost::shared_ptr<Region> region (wregion.lock());
+       
+       if (region) {
+               add_region_to_region_display (region);
+       }
 }
 
 void
@@ -80,6 +87,9 @@ Editor::add_region_to_region_display (boost::shared_ptr<Region> region)
        string str;
        TreeModel::Row row;
        Gdk::Color c;
+       bool missing_source;
+
+       missing_source = boost::dynamic_pointer_cast<SilentFileSource>(region->source());
 
        if (!show_automatic_regions_in_region_list && region->automatic()) {
                return;
@@ -96,7 +106,8 @@ Editor::add_region_to_region_display (boost::shared_ptr<Region> region)
                        parent = *(region_list_model->append());
                        
                        parent[region_list_columns.name] = _("Hidden");
-                       /// XXX FIX ME parent[region_list_columns.region]->reset ();
+                       boost::shared_ptr<Region> proxy = parent[region_list_columns.region];
+                       proxy.reset ();
 
                } else {
 
@@ -104,7 +115,8 @@ Editor::add_region_to_region_display (boost::shared_ptr<Region> region)
 
                                parent = *(region_list_model->insert(iter));
                                parent[region_list_columns.name] = _("Hidden");
-                               /// XXX FIX ME parent[region_list_columns.region]->reset ();
+                               boost::shared_ptr<Region> proxy = parent[region_list_columns.region];
+                               proxy.reset ();
 
                        } else {
                                parent = *iter;
@@ -117,15 +129,27 @@ Editor::add_region_to_region_display (boost::shared_ptr<Region> region)
        } else if (region->whole_file()) {
 
                row = *(region_list_model->append());
-               set_color(c, rgba_from_style ("RegionListWholeFile", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
+               if (missing_source) {
+                       c.set_rgb(65535,0,0);     // FIXME: error color from style
+               } else {
+                       set_color(c, rgba_from_style ("RegionListWholeFile", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
+               }
                row[region_list_columns.color_] = c;
 
                if (region->source()->name()[0] == '/') { // external file
 
                        if (region->whole_file()) {
+
+                               boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(region->source());
+
                                str = ".../";
-                               str += PBD::basename_nosuffix (region->source()->name());
-                               
+
+                               if (afs) {
+                                       str = region_name_from_path (afs->path(), region->n_channels() > 1);
+                               } else {
+                                       str += region->source()->name();
+                               }
+
                        } else {
                                str = region->name();
                        }
@@ -136,6 +160,18 @@ Editor::add_region_to_region_display (boost::shared_ptr<Region> region)
 
                }
 
+               if (region->n_channels() > 1) {
+                       std::stringstream foo;
+                       foo << region->n_channels ();
+                       str += " [";
+                       str += foo.str();
+                       str += ']';
+               }
+
+               if (missing_source) {
+                       str += _(" (MISSING)");
+               }
+
                row[region_list_columns.name] = str;
                row[region_list_columns.region] = region;
 
@@ -195,12 +231,19 @@ Editor::region_list_selection_changed()
                TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
                TreeIter iter;
 
-               /* just set the first selected region (in fact, the selection model might be SINGLE, which
-                  means there can only be one.
-               */
-               
                if ((iter = region_list_model->get_iter (*i))) {
-                       set_selected_regionview_from_region_list (((*iter)[region_list_columns.region]), Selection::Set);
+                       boost::shared_ptr<Region> r = (*iter)[region_list_columns.region];
+                       
+                       /* they could have clicked on a row that is just a placeholder, like "Hidden" */
+                       
+                       if (r) {
+                               
+                               /* just set the first selected region (in fact, the selection model might be SINGLE, which
+                                  means there can only be one.
+                               */
+                               
+                               set_selected_regionview_from_region_list (r, Selection::Set);
+                       }
                }
        }
 }
@@ -235,17 +278,12 @@ Editor::redisplay_regions ()
                for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
                        add_region_to_region_display (*r);
                }
+               tmp_region_list.clear();
                
                region_list_display.set_model (region_list_model);
        }
 }
 
-void
-Editor::region_list_clear ()
-{
-       region_list_model->clear();
-}
-
 void
 Editor::build_region_list_menu ()
 {
@@ -336,20 +374,15 @@ Editor::region_list_display_button_press (GdkEventButton *ev)
                }
        }
 
-       if (region == 0) {
-               return false;
-       }
-
-       if (Keyboard::is_delete_event (ev)) {
-               session->remove_region_from_region_list (region);
-               return true;
-       }
-
        if (Keyboard::is_context_menu_event (ev)) {
                show_region_list_display_context_menu (ev->button, ev->time);
                return true;
        }
 
+       if (region == 0) {
+               return false;
+       }
+
        switch (ev->button) {
        case 1:
                /* audition on double click */
@@ -538,7 +571,16 @@ Editor::region_list_selection_mapover (slot<void,boost::shared_ptr<Region> > sl)
                TreeIter iter;
 
                if ((iter = region_list_model->get_iter (*i))) {
-                       sl (((*iter)[region_list_columns.region]));
+
+                       /* some rows don't have a region associated with them, but can still be
+                          selected (XXX maybe prevent them from being selected)
+                       */
+
+                       boost::shared_ptr<Region> r = (*iter)[region_list_columns.region];
+
+                       if (r) {
+                               sl (r);
+                       }
                }
        }
 }
@@ -552,6 +594,7 @@ Editor::hide_a_region (boost::shared_ptr<Region> r)
 void
 Editor::remove_a_region (boost::shared_ptr<Region> r)
 {
+       cerr << "remove " << r->name();
        session->remove_region_from_region_list (r);
 }
 
@@ -570,6 +613,7 @@ Editor::hide_region_from_region_list ()
 void
 Editor::remove_region_from_region_list ()
 {
+       cerr << "Mapping remove over region selection\n";
        region_list_selection_mapover (mem_fun (*this, &Editor::remove_a_region));
 }
 
@@ -582,7 +626,7 @@ Editor::region_list_display_drag_data_received (const RefPtr<Gdk::DragContext>&
        vector<ustring> paths;
 
        if (convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
-               jack_nframes_t pos = 0;
+               nframes_t pos = 0;
                do_embed (paths, false, ImportAsRegion, 0, pos, true);
                context->drag_finish (true, false, time);
        }