Fix incorrect ordering when adding cinemas.
[dcpomatic.git] / src / wx / screens_panel.cc
index d5445b035a27c21aedcb7bf358617d48a337cf7d..562ea8f3895d0d67423ee135ae8b76bf4ef35900 100644 (file)
@@ -243,24 +243,28 @@ ScreensPanel::add_cinema_clicked ()
                        );
 
                try {
+                       _ignore_cinemas_changed = true;
+                       ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
                        Config::instance()->add_cinema(cinema);
                } catch (FileError& e) {
                        error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file.  Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what()));
                        return;
                }
 
-               optional<wxTreeListItem> item;
+               wxTreeListItem previous = wxTLI_FIRST;
+               bool found = false;
                for (auto existing_cinema: cinemas) {
-                       if (!item && compare(dialog->name(), existing_cinema->name) < 0) {
-                               if (auto existing_item = cinema_to_item(existing_cinema)) {
-                                       item = add_cinema (cinema, *existing_item);
-                               }
+                       if (compare(dialog->name(), existing_cinema->name) < 0) {
+                               /* existing_cinema should be after the one we're inserting */
+                               found = true;
+                               break;
                        }
+                       auto item = cinema_to_item(existing_cinema);
+                       DCPOMATIC_ASSERT(item);
+                       previous = *item;
                }
 
-               if (!item) {
-                       item = add_cinema (cinema, wxTLI_LAST);
-               }
+               auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
 
                if (item) {
                        _targets->UnselectAll ();
@@ -326,6 +330,8 @@ ScreensPanel::remove_cinema_clicked ()
        }
 
        for (auto const& cinema: _selected_cinemas) {
+               _ignore_cinemas_changed = true;
+               ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
                Config::instance()->remove_cinema(cinema);
                auto item = cinema_to_item(cinema);
                DCPOMATIC_ASSERT(item);