Use the ICU library to sort cinemas rather than strcoll() (part of #2208).
authorCarl Hetherington <cth@carlh.net>
Wed, 16 Mar 2022 21:10:55 +0000 (22:10 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 17 Mar 2022 23:12:44 +0000 (00:12 +0100)
strcoll() just doesn't really seem to work at all on Windows.

src/wx/screens_panel.cc
src/wx/screens_panel.h

index 2075515f353fe46edae7eb3b02ec13ae0064508f..f255d441325ced7d636c29c51468e221e5dcd840 100644 (file)
 #include "lib/cinema.h"
 #include "lib/config.h"
 #include "lib/screen.h"
+#include <unicode/putil.h>
+#include <unicode/ucol.h>
+#include <unicode/uiter.h>
+#include <unicode/utypes.h>
+#include <unicode/ustring.h>
 
 
 using std::cout;
@@ -523,3 +528,38 @@ ScreensPanel::screen_by_tree_list_item (wxTreeListItem item) const
                );
 }
 
+
+ScreensPanel::Comparator::Comparator ()
+{
+       UErrorCode status = U_ZERO_ERROR;
+       _collator = ucol_open(nullptr, &status);
+       if (_collator) {
+               ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
+               ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
+               ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
+       }
+}
+
+ScreensPanel::Comparator::~Comparator ()
+{
+       if (_collator) {
+               ucol_close (_collator);
+       }
+}
+
+int
+ScreensPanel::Comparator::Compare (wxTreeListCtrl* tree_list, unsigned, wxTreeListItem a, wxTreeListItem b)
+{
+       auto utf8_a = wx_to_std(tree_list->GetItemText(a));
+       auto utf8_b = wx_to_std(tree_list->GetItemText(b));
+       if (_collator) {
+               UErrorCode error = U_ZERO_ERROR;
+               boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);
+               u_strFromUTF8(reinterpret_cast<UChar*>(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error);
+               boost::scoped_array<uint16_t> utf16_b(new uint16_t[utf8_b.size() + 1]);
+               u_strFromUTF8(reinterpret_cast<UChar*>(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error);
+               return ucol_strcoll(_collator, reinterpret_cast<UChar*>(utf16_a.get()), -1, reinterpret_cast<UChar*>(utf16_b.get()), -1);
+       } else {
+               return strcoll(utf8_a.c_str(), utf8_b.c_str());
+       }
+}
index c0b90dfcf08237d9fa80a96cfb873d962fdcc336..4b7a801413a5c3e3d5b80c5a17ce1b241dcc3c29 100644 (file)
@@ -36,6 +36,7 @@ namespace dcpomatic {
 
 
 class Cinema;
+class UCollator;
 
 
 class ScreensPanel : public wxPanel
@@ -90,10 +91,13 @@ private:
        class Comparator : public wxTreeListItemComparator
        {
        public:
-               int Compare (wxTreeListCtrl* tree_list, unsigned, wxTreeListItem a, wxTreeListItem b) override
-               {
-                       return strcoll(wx_to_std(tree_list->GetItemText(a)).c_str(), wx_to_std(tree_list->GetItemText(b)).c_str());
-               }
+               Comparator ();
+               ~Comparator ();
+
+               int Compare (wxTreeListCtrl* tree_list, unsigned, wxTreeListItem a, wxTreeListItem b) override;
+
+       private:
+               UCollator* _collator;
        };
 
        Comparator _comparator;