diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 821fa7f48c..c007a6d375 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -24,6 +24,7 @@ #include "core/alloc_func.hpp" #include "string_func.h" #include "gfx_func.h" +#include "widgets/dropdown_type.h" #include "widgets/dropdown_func.h" #include "table/sprites.h" @@ -157,6 +158,26 @@ enum GameOptionsWidgets { GAMEOPT_SCREENSHOT_BTN, }; +/** + * Update/redraw the languages dropdown + * @param w the window the dropdown belongs to + */ +static void ShowLangDropdown(Window *w) +{ + typedef std::map LangList; + + /* Sort language names */ + LangList langs; + for (int i = 0; i < _dynlang.num; i++) langs[SPECSTR_LANGUAGE_START + i] = i; + + DropDownList *list = new DropDownList(); + for (LangList::iterator it = langs.begin(); it != langs.end(); it++) { + list->push_back(new DropDownListStringItem((*it).first, (*it).second, false)); + } + + ShowDropDownList(w, list, _dynlang.curr, GAMEOPT_LANG_BTN); +} + static void ShowCustCurrency(); static void GameOptionsWndProc(Window *w, WindowEvent *e) @@ -228,7 +249,7 @@ static void GameOptionsWndProc(Window *w, WindowEvent *e) break; // not implemented case GAMEOPT_LANG_TXT: case GAMEOPT_LANG_BTN: /* Setup interface language dropdown */ - ShowDropDownMenu(w, _dynlang.dropdown, _dynlang.curr, GAMEOPT_LANG_BTN, 0, 0); + ShowLangDropdown(w); break; case GAMEOPT_RESOLUTION_TXT: case GAMEOPT_RESOLUTION_BTN: /* Setup resolution dropdown */ diff --git a/src/strings.cpp b/src/strings.cpp index 7849f4f5bb..645cb96152 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1318,14 +1318,6 @@ const char *GetCurrentLocale(const char *param) } #endif /* !(defined(WIN32) || defined(__APPLE__)) */ -static int CDECL LanguageCompareFunc(const void *a, const void *b) -{ - const Language *cmp1 = (const Language*)a; - const Language *cmp2 = (const Language*)b; - - return strcmp(cmp1->file, cmp2->file); -} - int CDECL StringIDSorter(const void *a, const void *b) { const StringID va = *(const StringID*)a; @@ -1432,9 +1424,6 @@ void InitializeLanguagePacks() } if (language_count == 0) error("No available language packs (invalid versions?)"); - /* Sort the language names alphabetically */ - qsort(files, language_count, sizeof(Language), LanguageCompareFunc); - /* Acquire the locale of the current system */ const char *lang = GetCurrentLocale("LC_MESSAGES"); if (lang == NULL) lang = "en_GB"; @@ -1453,7 +1442,6 @@ void InitializeLanguagePacks() dl->ent[dl->num].file = files[i].file; dl->ent[dl->num].name = strdup(hdr.name); - dl->dropdown[dl->num] = SPECSTR_LANGUAGE_START + dl->num; /* We are trying to find a default language. The priority is by * configuration file, local environment and last, if nothing found, @@ -1469,8 +1457,6 @@ void InitializeLanguagePacks() dl->num++; } - /* Terminate the dropdown list */ - dl->dropdown[dl->num] = INVALID_STRING_ID; if (dl->num == 0) error("Invalid version of language packs"); diff --git a/src/strings_func.h b/src/strings_func.h index e75ce8c678..9067f70490 100644 --- a/src/strings_func.h +++ b/src/strings_func.h @@ -69,6 +69,12 @@ void InitializeLanguagePacks(); int CDECL StringIDSorter(const void *a, const void *b); +/** Key comparison function for std::map */ +struct StringIDCompare +{ + bool operator()(StringID s1, StringID s2) { return StringIDSorter(&s1, &s2) < 0; } +}; + void CheckForMissingGlyphsInLoadedLanguagePack(); #endif /* STRINGS_TYPE_H */ diff --git a/src/strings_type.h b/src/strings_type.h index c5c90810a1..3767288214 100644 --- a/src/strings_type.h +++ b/src/strings_type.h @@ -20,11 +20,10 @@ struct Language { /** Used for dynamic language support */ struct DynamicLanguages { - int num; ///< Number of languages - int curr; ///< Currently selected language index - char curr_file[MAX_PATH]; ///< Currently selected language file name without path (needed for saving the filename of the loaded language). - StringID dropdown[MAX_LANG + 1]; ///< List of languages in the settings gui - Language ent[MAX_LANG]; ///< Information about the languages + int num; ///< Number of languages + int curr; ///< Currently selected language index + char curr_file[MAX_PATH]; ///< Currently selected language file name without path (needed for saving the filename of the loaded language). + Language ent[MAX_LANG]; ///< Information about the languages }; // special string constants