changeset 8321:c361d51b1bb8 draft

(svn r11886) -Add: sort the strings in languages dropdown
author glx <glx@openttd.org>
date Thu, 17 Jan 2008 00:21:43 +0000
parents 06495654aee6
children 04ad512648d9
files src/settings_gui.cpp src/strings.cpp src/strings_func.h src/strings_type.h
diffstat 4 files changed, 32 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 	GAMEOPT_SCREENSHOT_BTN,
 };
 
+/**
+ * Update/redraw the languages dropdown
+ * @param w   the window the dropdown belongs to
+ */
+static void ShowLangDropdown(Window *w)
+{
+	typedef std::map<StringID, int, StringIDCompare> 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 @@
 					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 */
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -1318,14 +1318,6 @@
 }
 #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 @@
 	}
 	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 @@
 
 		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 @@
 
 		dl->num++;
 	}
-	/* Terminate the dropdown list */
-	dl->dropdown[dl->num] = INVALID_STRING_ID;
 
 	if (dl->num == 0) error("Invalid version of language packs");
 
--- a/src/strings_func.h
+++ b/src/strings_func.h
@@ -69,6 +69,12 @@
 
 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 */
--- a/src/strings_type.h
+++ b/src/strings_type.h
@@ -20,11 +20,10 @@
 
 /** 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