Use SafeStringStream instead of std::stringstream to try to fix random crashes on...
[dcpomatic.git] / src / wx / film_editor.cc
index de215a0d294298a484f6407715ef7d9bb9b55933..87310d21ae598ac93d1b67a8e40d3b2e8c61082c 100644 (file)
@@ -45,6 +45,7 @@
 #include "lib/playlist.h"
 #include "lib/content.h"
 #include "lib/content_factory.h"
+#include "lib/safe_stringstream.h"
 #include "timecode.h"
 #include "wx_util.h"
 #include "film_editor.h"
@@ -57,7 +58,6 @@
 
 using std::string;
 using std::cout;
-using std::stringstream;
 using std::pair;
 using std::fixed;
 using std::setprecision;
@@ -420,7 +420,7 @@ FilmEditor::film_changed (Film::Property p)
                return;
        }
 
-       stringstream s;
+       SafeStringStream s;
 
        for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
                (*i)->film_changed (p);
@@ -836,8 +836,8 @@ void
 FilmEditor::content_remove_clicked ()
 {
        ContentList c = selected_content ();
-       if (c.size() == 1) {
-               _film->remove_content (c.front ());
+       for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
+               _film->remove_content (*i);
        }
 
        content_selection_changed ();
@@ -864,30 +864,41 @@ FilmEditor::setup_content_sensitivity ()
        VideoContentList video_selection = selected_video_content ();
        AudioContentList audio_selection = selected_audio_content ();
 
-       _content_remove->Enable   (selection.size() == 1 && _generally_sensitive);
+       _content_remove->Enable   (!selection.empty() && _generally_sensitive);
        _content_earlier->Enable  (selection.size() == 1 && _generally_sensitive);
        _content_later->Enable    (selection.size() == 1 && _generally_sensitive);
        _content_timeline->Enable (!_film->content().empty() && _generally_sensitive);
 
-       _video_panel->Enable    (video_selection.size() > 0 && _generally_sensitive);
-       _audio_panel->Enable    (audio_selection.size() > 0 && _generally_sensitive);
+       _video_panel->Enable    (!video_selection.empty() && _generally_sensitive);
+       _audio_panel->Enable    (!audio_selection.empty() && _generally_sensitive);
        _subtitle_panel->Enable (selection.size() == 1 && dynamic_pointer_cast<FFmpegContent> (selection.front()) && _generally_sensitive);
-       _timing_panel->Enable   (selection.size() == 1 && _generally_sensitive);
+       _timing_panel->Enable   (!selection.empty() && _generally_sensitive);
 }
 
 ContentList
 FilmEditor::selected_content ()
 {
        ContentList sel;
+
+       if (!_film) {
+               return sel;
+       }
+
+       /* The list was populated using a sorted content list, so we must sort it here too
+          so that we can look up by index and get the right thing.
+       */
+       ContentList content = _film->content ();
+       sort (content.begin(), content.end(), ContentSorter ());
+       
        long int s = -1;
-       while (1) {
+       while (true) {
                s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
                if (s == -1) {
                        break;
                }
 
                if (s < int (_film->content().size ())) {
-                       sel.push_back (_film->content()[s]);
+                       sel.push_back (content[s]);
                }
        }
 
@@ -978,7 +989,7 @@ FilmEditor::set_selection (weak_ptr<Content> wc)
                if (content[i] == wc.lock ()) {
                        _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
                } else {
-                       _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
+                       _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
                }
        }
 }