Store subtitle language(s) in Film, and allow setup of those
[dcpomatic.git] / src / wx / language_tag_dialog.cc
index 2854479a7469647ec9ad1e4f852cf99752c82d2f..94e266f2c5b88e2a4e601fc5b528632ba54eb84b 100644 (file)
@@ -37,6 +37,7 @@
 #include <vector>
 
 
+using std::min;
 using std::pair;
 using std::string;
 using std::vector;
@@ -69,6 +70,11 @@ public:
                                SetItemState (item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
                                EnsureVisible (item);
                        }
+               } else {
+                       if (GetItemCount() > 0) {
+                               /* The new list sometimes isn't visible without this */
+                               EnsureVisible (0);
+                       }
                }
        }
 
@@ -197,6 +203,8 @@ LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
        button_sizer->Add (_add_variant, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
        _add_external = new wxButton(this, wxID_ANY, "Add external");
        button_sizer->Add (_add_external, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
+       _remove = new wxButton(this, wxID_ANY, "Remove");
+       button_sizer->Add (_remove, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
 
        _choose_subtag_panel = new LanguageSubtagPanel (this);
        _choose_subtag_panel->set (dcp::LanguageTag::LANGUAGE, "");
@@ -216,23 +224,13 @@ LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
 
        SetSizerAndFit (overall_sizer);
 
-       bool have_language = false;
-       vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> > subtags = tag.subtags();
-       for (vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> >::const_iterator i = subtags.begin(); i != subtags.end(); ++i) {
-               add_to_current_tag (i->first, i->second);
-               if (i->first == dcp::LanguageTag::LANGUAGE) {
-                       have_language = true;
-               }
-       }
-
-       if (!have_language) {
-               add_to_current_tag (dcp::LanguageTag::LANGUAGE, dcp::LanguageTag::SubtagData("en", "English"));
-       }
+       set (tag);
 
        _add_script->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::SCRIPT, boost::optional<dcp::LanguageTag::SubtagData>()));
        _add_region->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::REGION, boost::optional<dcp::LanguageTag::SubtagData>()));
        _add_variant->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::VARIANT, boost::optional<dcp::LanguageTag::SubtagData>()));
        _add_external->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::EXTLANG, boost::optional<dcp::LanguageTag::SubtagData>()));
+       _remove->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::remove_from_current_tag, this));
        _choose_subtag_panel->SelectionChanged.connect(bind(&LanguageTagDialog::chosen_subtag_changed, this, _1));
        _choose_subtag_panel->SearchChanged.connect(bind(&LanguageTagDialog::search_changed, this, _1));
        _current_tag_list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageTagDialog::current_tag_selection_changed, this));
@@ -240,6 +238,24 @@ LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
 }
 
 
+void
+LanguageTagDialog::remove_from_current_tag ()
+{
+       long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+       if (selected <= 0) {
+               return;
+       }
+
+       _current_tag_subtags.erase (_current_tag_subtags.begin() + selected);
+       _current_tag_list->DeleteItem (selected);
+
+       _current_tag_list->SetItemState (min(selected, _current_tag_list->GetItemCount() - 1L), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
+
+       setup_sensitivity ();
+       current_tag_selection_changed ();
+}
+
+
 dcp::LanguageTag LanguageTagDialog::get () const
 {
        dcp::LanguageTag tag;
@@ -276,6 +292,27 @@ dcp::LanguageTag LanguageTagDialog::get () const
 }
 
 
+void
+LanguageTagDialog::set (dcp::LanguageTag tag)
+{
+       _current_tag_subtags.clear ();
+       _current_tag_list->DeleteAllItems ();
+
+       bool have_language = false;
+       vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> > subtags = tag.subtags();
+       for (vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> >::const_iterator i = subtags.begin(); i != subtags.end(); ++i) {
+               add_to_current_tag (i->first, i->second);
+               if (i->first == dcp::LanguageTag::LANGUAGE) {
+                       have_language = true;
+               }
+       }
+
+       if (!have_language) {
+               add_to_current_tag (dcp::LanguageTag::LANGUAGE, dcp::LanguageTag::SubtagData("en", "English"));
+       }
+}
+
+
 string LanguageTagDialog::subtag_type_name (dcp::LanguageTag::SubtagType type)
 {
        switch (type) {
@@ -354,6 +391,8 @@ LanguageTagDialog::chosen_subtag_changed (optional<dcp::LanguageTag::SubtagData>
                _current_tag_list->SetItem (selected, 0, subtag_type_name(_current_tag_subtags[selected].type));
                _current_tag_list->SetItem (selected, 1, selection->description);
        }
+
+       setup_sensitivity ();
 }
 
 void
@@ -381,6 +420,8 @@ LanguageTagDialog::setup_sensitivity ()
                                break;
                }
        }
+       long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+       _remove->Enable (selected > 0);
 }