src/misc_gui.cpp
branchNewGRF_ports
changeset 10991 d8811e327d12
parent 10731 67db0d431d5e
--- a/src/misc_gui.cpp	Tue May 27 00:50:55 2008 +0000
+++ b/src/misc_gui.cpp	Tue Jun 17 10:32:49 2008 +0000
@@ -50,7 +50,6 @@
 #include "table/strings.h"
 
 /* Variables to display file lists */
-FiosItem *_fios_list;
 SaveLoadDialogMode _saveload_mode;
 
 
@@ -102,7 +101,7 @@
 
 	LandInfoWindow(TileIndex tile) : Window(&_land_info_desc) {
 		Player *p = GetPlayer(IsValidPlayer(_local_player) ? _local_player : PLAYER_FIRST);
-		Town *t = ClosestTownFromTile(tile, _settings.economy.dist_local_authority);
+		Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
 
 		Money old_money = p->player_money;
 		p->player_money = INT64_MAX;
@@ -396,7 +395,7 @@
 			Window(pt.x, pt.y, width, height, WC_ERRMSG, widget),
 			show_player_face(show_player_face)
 	{
-		this->duration = _settings.gui.errmsg_duration;
+		this->duration = _settings_client.gui.errmsg_duration;
 		CopyOutDParam(this->decode_params, 0, lengthof(this->decode_params));
 		this->message_1 = msg1;
 		this->message_2 = msg2;
@@ -465,7 +464,7 @@
 {
 	DeleteWindowById(WC_ERRMSG, 0);
 
-	if (!_settings.gui.errmsg_duration) return;
+	if (!_settings_client.gui.errmsg_duration) return;
 
 	if (msg_2 == STR_NULL) msg_2 = STR_EMPTY;
 
@@ -620,7 +619,7 @@
 	DeleteWindowById(WC_TOOLTIPS, 0);
 
 	/* We only show measurement tooltips with patch setting on */
-	if (str == STR_NULL || (paramcount != 0 && !_settings.gui.measure_tooltip)) return;
+	if (str == STR_NULL || (paramcount != 0 && !_settings_client.gui.measure_tooltip)) return;
 
 	for (uint i = 0; i != paramcount; i++) SetDParam(i, params[i]);
 	char buffer[512];
@@ -717,30 +716,6 @@
 	}
 }
 
-void SetVScrollCount(Window *w, int num)
-{
-	w->vscroll.count = num;
-	num -= w->vscroll.cap;
-	if (num < 0) num = 0;
-	if (num < w->vscroll.pos) w->vscroll.pos = num;
-}
-
-void SetVScroll2Count(Window *w, int num)
-{
-	w->vscroll2.count = num;
-	num -= w->vscroll2.cap;
-	if (num < 0) num = 0;
-	if (num < w->vscroll2.pos) w->vscroll2.pos = num;
-}
-
-void SetHScrollCount(Window *w, int num)
-{
-	w->hscroll.count = num;
-	num -= w->hscroll.cap;
-	if (num < 0) num = 0;
-	if (num < w->hscroll.pos) w->hscroll.pos = num;
-}
-
 /* Delete a character at the caret position in a text buf.
  * If backspace is set, delete the character before the caret,
  * else delete the character after it. */
@@ -751,7 +726,7 @@
 
 	if (backspace) s = Utf8PrevChar(s);
 
-	size_t len = Utf8Decode(&c, s);
+	uint16 len = (uint16)Utf8Decode(&c, s);
 	uint width = GetCharacterWidth(FS_NORMAL, c);
 
 	tb->width  -= width;
@@ -807,7 +782,7 @@
 bool InsertTextBufferChar(Textbuf *tb, WChar key)
 {
 	const byte charwidth = GetCharacterWidth(FS_NORMAL, key);
-	size_t len = Utf8CharLen(key);
+	uint16 len = (uint16)Utf8CharLen(key);
 	if (tb->length < (tb->maxlength - len) && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) {
 		memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->length - tb->caretpos + 1);
 		Utf8Encode(tb->buf + tb->caretpos, key);
@@ -847,7 +822,7 @@
 			if (tb->caretpos < tb->length) {
 				WChar c;
 
-				tb->caretpos   += Utf8Decode(&c, tb->buf + tb->caretpos);
+				tb->caretpos   += (uint16)Utf8Decode(&c, tb->buf + tb->caretpos);
 				tb->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
 
 				return true;
@@ -1083,6 +1058,11 @@
 	{
 		EventState state;
 		switch (this->HandleEditBoxKey(QUERY_STR_WIDGET_TEXT, key, keycode, state)) {
+			default: NOT_REACHED();
+			case 0: {
+				Window *osk = FindWindowById(WC_OSK, 0);
+				if (osk != NULL && osk->parent == this) osk->OnInvalidateData();
+			} break;
 			case 1: this->OnOk(); // Enter pressed, confirms change
 			/* FALL THROUGH */
 			case 2: delete this; break; // ESC pressed, closes window, abandons changes
@@ -1093,8 +1073,9 @@
 	~QueryStringWindow()
 	{
 		if (!this->handled && this->parent != NULL) {
-			this->handled = true;
-			this->parent->OnQueryTextFinished(NULL);
+			Window *parent = this->parent;
+			this->parent = NULL; // so parent doesn't try to delete us again
+			parent->OnQueryTextFinished(NULL);
 		}
 		ClrBit(_no_scroll, SCROLL_EDIT);
 	}
@@ -1207,6 +1188,8 @@
 				 * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
 				QueryCallbackProc *proc = this->proc;
 				Window *parent = this->parent;
+				/* Prevent the destructor calling the callback function */
+				this->proc = NULL;
 				delete this;
 				if (proc != NULL) {
 					proc(parent, true);
@@ -1316,11 +1299,11 @@
 		case SLD_NEW_GAME:
 		case SLD_LOAD_SCENARIO:
 		case SLD_SAVE_SCENARIO:
-			_fios_list = FiosGetScenarioList(_saveload_mode); break;
+			FiosGetScenarioList(_saveload_mode); break;
 		case SLD_LOAD_HEIGHTMAP:
-			_fios_list = FiosGetHeightmapList(_saveload_mode); break;
+			FiosGetHeightmapList(_saveload_mode); break;
 
-		default: _fios_list = FiosGetSavegameList(_saveload_mode); break;
+		default: FiosGetSavegameList(_saveload_mode); break;
 	}
 }
 
@@ -1349,8 +1332,8 @@
 	 * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
 	 * Only sort savegames/scenarios, not directories
 	 */
-	for (int i = 0; i < _fios_num; i++) {
-		switch (_fios_list[i].type) {
+	for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
+		switch (item->type) {
 			case FIOS_TYPE_DIR:    sort_start++; break;
 			case FIOS_TYPE_PARENT: sort_start++; break;
 			case FIOS_TYPE_DRIVE:  sort_end++;   break;
@@ -1358,9 +1341,9 @@
 		}
 	}
 
-	uint s_amount = _fios_num - sort_start - sort_end;
+	uint s_amount = _fios_items.Length() - sort_start - sort_end;
 	if (s_amount > 0) {
-		qsort(_fios_list + sort_start, s_amount, sizeof(FiosItem), compare_FiosItems);
+		qsort(_fios_items.Get(sort_start), s_amount, sizeof(FiosItem), compare_FiosItems);
 	}
 }
 
@@ -1459,10 +1442,9 @@
 
 	virtual void OnPaint()
 	{
-		int pos;
 		int y;
 
-		SetVScrollCount(this, _fios_num);
+		SetVScrollCount(this, _fios_items.Length());
 		this->DrawWidgets();
 		DrawFiosTexts(this->width);
 
@@ -1475,8 +1457,8 @@
 		this->DrawSortButtonState(_savegame_sort_order & SORT_BY_NAME ? 2 : 3, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
 
 		y = this->widget[7].top + 1;
-		for (pos = this->vscroll.pos; pos < _fios_num; pos++) {
-			const FiosItem *item = _fios_list + pos;
+		for (uint pos = this->vscroll.pos; pos < _fios_items.Length(); pos++) {
+			const FiosItem *item = _fios_items.Get(pos);
 
 			DoDrawStringTruncated(item->title, 4, y, _fios_colors[item->type], this->width - 18);
 			y += 10;
@@ -1513,14 +1495,12 @@
 
 			case 7: { // Click the listbox
 				int y = (pt.y - this->widget[widget].top - 1) / 10;
-				char *name;
-				const FiosItem *file;
 
 				if (y < 0 || (y += this->vscroll.pos) >= this->vscroll.count) return;
 
-				file = _fios_list + y;
+				const FiosItem *file = _fios_items.Get(y);
 
-				name = FiosBrowseTo(file);
+				char *name = FiosBrowseTo(file);
 				if (name != NULL) {
 					if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
 						_switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD;