Changeset - r8738:9e9d09d2fe02
[Not reviewed]
master
0 2 0
rubidium - 17 years ago 2008-03-26 10:38:31
rubidium@openttd.org
(svn r12426) -Cleanup: sprinkle some coding style over a few files.
2 files changed with 1236 insertions and 1218 deletions:
0 comments (0 inline, 0 general)
src/misc_gui.cpp
Show inline comments
 
@@ -92,26 +92,20 @@ static const WindowDesc _land_info_desc 
 

	
 
static void Place_LandInfo(TileIndex tile)
 
{
 
	Player *p;
 
	Window *w;
 
	Town *t;
 
	Money old_money;
 
	CommandCost costclear;
 
	AcceptedCargo ac;
 
	TileDesc td;
 
	StringID str;
 

	
 
	DeleteWindowById(WC_LAND_INFO, 0);
 

	
 
	w = AllocateWindowDesc(&_land_info_desc);
 
	Window *w = AllocateWindowDesc(&_land_info_desc);
 
	WP(w, void_d).data = &_landinfo_data;
 

	
 
	p = GetPlayer(IsValidPlayer(_local_player) ? _local_player : PLAYER_FIRST);
 
	t = ClosestTownFromTile(tile, _patches.dist_local_authority);
 
	Player *p = GetPlayer(IsValidPlayer(_local_player) ? _local_player : PLAYER_FIRST);
 
	Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority);
 

	
 
	old_money = p->player_money;
 
	Money old_money = p->player_money;
 
	p->player_money = INT64_MAX;
 
	costclear = DoCommand(tile, 0, 0, 0, CMD_LANDSCAPE_CLEAR);
 
	CommandCost costclear = DoCommand(tile, 0, 0, 0, CMD_LANDSCAPE_CLEAR);
 
	p->player_money = old_money;
 

	
 
	/* Because build_date is not set yet in every TileDesc, we make sure it is empty */
 
@@ -126,7 +120,7 @@ static void Place_LandInfo(TileIndex til
 
	if (td.owner != OWNER_NONE && td.owner != OWNER_WATER) GetNameOfOwner(td.owner, tile);
 
	GetString(_landinfo_data[1], STR_01A7_OWNER, lastof(_landinfo_data[1]));
 

	
 
	str = STR_01A4_COST_TO_CLEAR_N_A;
 
	StringID str = STR_01A4_COST_TO_CLEAR_N_A;
 
	if (CmdSucceeded(costclear)) {
 
		SetDParam(0, costclear.GetCost());
 
		str = STR_01A5_COST_TO_CLEAR;
 
@@ -147,36 +141,33 @@ static void Place_LandInfo(TileIndex til
 
	}
 
	GetString(_landinfo_data[4], STR_01A8_LOCAL_AUTHORITY, lastof(_landinfo_data[4]));
 

	
 
	{
 
		char *p = GetString(_landinfo_data[5], STR_01CE_CARGO_ACCEPTED, lastof(_landinfo_data[5]));
 
		bool found = false;
 
	char *strp = GetString(_landinfo_data[5], STR_01CE_CARGO_ACCEPTED, lastof(_landinfo_data[5]));
 
	bool found = false;
 

	
 
		for (CargoID i = 0; i < NUM_CARGO; ++i) {
 
			if (ac[i] > 0) {
 
				/* Add a comma between each item. */
 
				if (found) {
 
					*p++ = ',';
 
					*p++ = ' ';
 
				}
 
				found = true;
 
	for (CargoID i = 0; i < NUM_CARGO; ++i) {
 
		if (ac[i] > 0) {
 
			/* Add a comma between each item. */
 
			if (found) {
 
				*strp++ = ',';
 
				*strp++ = ' ';
 
			}
 
			found = true;
 

	
 
				/* If the accepted value is less than 8, show it in 1/8:ths */
 
				if (ac[i] < 8) {
 
					SetDParam(0, ac[i]);
 
					SetDParam(1, GetCargo(i)->name);
 
					p = GetString(p, STR_01D1_8, lastof(_landinfo_data[5]));
 
				} else {
 
					p = GetString(p, GetCargo(i)->name, lastof(_landinfo_data[5]));
 
				}
 
			/* If the accepted value is less than 8, show it in 1/8:ths */
 
			if (ac[i] < 8) {
 
				SetDParam(0, ac[i]);
 
				SetDParam(1, GetCargo(i)->name);
 
				strp = GetString(strp, STR_01D1_8, lastof(_landinfo_data[5]));
 
			} else {
 
				strp = GetString(strp, GetCargo(i)->name, lastof(_landinfo_data[5]));
 
			}
 
		}
 

	
 
		if (!found) _landinfo_data[5][0] = '\0';
 
	}
 
	if (!found) _landinfo_data[5][0] = '\0';
 

	
 
	if (td.build_date != 0) {
 
		SetDParam(0, td.build_date);
 
	GetString(_landinfo_data[6], STR_BUILD_DATE, lastof(_landinfo_data[6]));
 
		GetString(_landinfo_data[6], STR_BUILD_DATE, lastof(_landinfo_data[6]));
 
	} else {
 
		_landinfo_data[6][0] = '\0';
 
	}
 
@@ -265,40 +256,41 @@ static const char *credits[] = {
 
static void AboutWindowProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_CREATE: // Set up window counter and start position of scroller
 
		WP(w, scroller_d).counter = 5;
 
		WP(w, scroller_d).height = w->height - 40;
 
		break;
 
	case WE_PAINT: {
 
		uint i;
 
		int y = WP(w, scroller_d).height;
 
		DrawWindowWidgets(w);
 
		case WE_CREATE: // Set up window counter and start position of scroller
 
			WP(w, scroller_d).counter = 5;
 
			WP(w, scroller_d).height = w->height - 40;
 
			break;
 

	
 
		/* Show original copyright and revision version */
 
		DrawStringCentered(210, 17, STR_00B6_ORIGINAL_COPYRIGHT, TC_FROMSTRING);
 
		DrawStringCentered(210, 17 + 10, STR_00B7_VERSION, TC_FROMSTRING);
 
		case WE_PAINT: {
 
			int y = WP(w, scroller_d).height;
 
			DrawWindowWidgets(w);
 

	
 
			/* Show original copyright and revision version */
 
			DrawStringCentered(210, 17, STR_00B6_ORIGINAL_COPYRIGHT, TC_FROMSTRING);
 
			DrawStringCentered(210, 17 + 10, STR_00B7_VERSION, TC_FROMSTRING);
 

	
 
		/* Show all scrolling credits */
 
		for (i = 0; i < lengthof(credits); i++) {
 
			if (y >= 50 && y < (w->height - 40)) {
 
				DoDrawString(credits[i], 10, y, TC_BLACK);
 
			/* Show all scrolling credits */
 
			for (uint i = 0; i < lengthof(credits); i++) {
 
				if (y >= 50 && y < (w->height - 40)) {
 
					DoDrawString(credits[i], 10, y, TC_BLACK);
 
				}
 
				y += 10;
 
			}
 
			y += 10;
 
		}
 

	
 
		/* If the last text has scrolled start anew from the start */
 
		if (y < 50) WP(w, scroller_d).height = w->height - 40;
 
			/* If the last text has scrolled start anew from the start */
 
			if (y < 50) WP(w, scroller_d).height = w->height - 40;
 

	
 
		DoDrawStringCentered(210, w->height - 25, "Website: http://www.openttd.org", TC_BLACK);
 
		DrawStringCentered(210, w->height - 15, STR_00BA_COPYRIGHT_OPENTTD, TC_FROMSTRING);
 
	} break;
 
	case WE_TICK: // Timer to scroll the text and adjust the new top
 
		if (--WP(w, scroller_d).counter == 0) {
 
			WP(w, scroller_d).counter = 5;
 
			WP(w, scroller_d).height--;
 
			SetWindowDirty(w);
 
		}
 
		break;
 
			DoDrawStringCentered(210, w->height - 25, "Website: http://www.openttd.org", TC_BLACK);
 
			DrawStringCentered(210, w->height - 15, STR_00BA_COPYRIGHT_OPENTTD, TC_FROMSTRING);
 
		} break;
 

	
 
		case WE_TICK: // Timer to scroll the text and adjust the new top
 
			if (--WP(w, scroller_d).counter == 0) {
 
				WP(w, scroller_d).counter = 5;
 
				WP(w, scroller_d).height--;
 
				SetWindowDirty(w);
 
			}
 
			break;
 
	}
 
}
 

	
 
@@ -343,82 +335,83 @@ static const PalSpriteID _tree_sprites[]
 
static void BuildTreesWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_PAINT: {
 
		int x,y;
 
		int i, count;
 
		case WE_PAINT: {
 
			int i, count;
 

	
 
			DrawWindowWidgets(w);
 

	
 
			WP(w, tree_d).base = i = _tree_base_by_landscape[_opt.landscape];
 
			WP(w, tree_d).count = count = _tree_count_by_landscape[_opt.landscape];
 

	
 
		DrawWindowWidgets(w);
 
			int x = 18;
 
			int y = 54;
 
			do {
 
				DrawSprite(_tree_sprites[i].sprite, _tree_sprites[i].pal, x, y);
 
				x += 35;
 
				if (!(++i & 3)) {
 
					x -= 35 * 4;
 
					y += 47;
 
				}
 
			} while (--count);
 
		} break;
 

	
 
		WP(w, tree_d).base = i = _tree_base_by_landscape[_opt.landscape];
 
		WP(w, tree_d).count = count = _tree_count_by_landscape[_opt.landscape];
 
		case WE_CLICK: {
 
			int wid = e->we.click.widget;
 

	
 
		x = 18;
 
		y = 54;
 
		do {
 
			DrawSprite(_tree_sprites[i].sprite, _tree_sprites[i].pal, x, y);
 
			x += 35;
 
			if (!(++i & 3)) {
 
				x -= 35 * 4;
 
				y += 47;
 
			switch (wid) {
 
			case 0:
 
					ResetObjectToPlace();
 
					break;
 

	
 
				case 3: case 4: case 5: case 6:
 
				case 7: case 8: case 9: case 10:
 
				case 11:case 12: case 13: case 14:
 
					if (wid - 3 >= WP(w, tree_d).count) break;
 

	
 
					if (HandlePlacePushButton(w, wid, SPR_CURSOR_TREE, VHM_RECT, NULL)) {
 
						_tree_to_plant = WP(w, tree_d).base + wid - 3;
 
					}
 
					break;
 

	
 
				case 15: // tree of random type.
 
					if (HandlePlacePushButton(w, 15, SPR_CURSOR_TREE, VHM_RECT, NULL)) {
 
						_tree_to_plant = -1;
 
					}
 
					break;
 

	
 
				case 16: // place trees randomly over the landscape
 
					w->LowerWidget(16);
 
					w->flags4 |= 5 << WF_TIMEOUT_SHL;
 
					SndPlayFx(SND_15_BEEP);
 
					PlaceTreesRandomly();
 
					MarkWholeScreenDirty();
 
					break;
 
			}
 
		} while (--count);
 
	} break;
 

	
 
	case WE_CLICK: {
 
		int wid = e->we.click.widget;
 
		} break;
 

	
 
		switch (wid) {
 
		case 0:
 
			ResetObjectToPlace();
 
			break;
 

	
 
		case 3: case 4: case 5: case 6:
 
		case 7: case 8: case 9: case 10:
 
		case 11:case 12: case 13: case 14:
 
			if (wid - 3 >= WP(w, tree_d).count) break;
 

	
 
			if (HandlePlacePushButton(w, wid, SPR_CURSOR_TREE, VHM_RECT, NULL))
 
				_tree_to_plant = WP(w, tree_d).base + wid - 3;
 
		case WE_PLACE_OBJ:
 
			VpStartPlaceSizing(e->we.place.tile, VPM_X_AND_Y_LIMITED, DDSP_PLANT_TREES);
 
			VpSetPlaceSizingLimit(20);
 
			break;
 

	
 
		case 15: // tree of random type.
 
			if (HandlePlacePushButton(w, 15, SPR_CURSOR_TREE, VHM_RECT, NULL))
 
				_tree_to_plant = -1;
 
		case WE_PLACE_DRAG:
 
			VpSelectTilesWithMethod(e->we.place.pt.x, e->we.place.pt.y, e->we.place.select_method);
 
			return;
 

	
 
		case WE_PLACE_MOUSEUP:
 
			if (e->we.place.pt.x != -1 && e->we.place.select_proc == DDSP_PLANT_TREES) {
 
				DoCommandP(e->we.place.tile, _tree_to_plant, e->we.place.starttile, NULL,
 
					CMD_PLANT_TREE | CMD_MSG(STR_2805_CAN_T_PLANT_TREE_HERE));
 
			}
 
			break;
 

	
 
		case 16: // place trees randomly over the landscape
 
			w->LowerWidget(16);
 
			w->flags4 |= 5 << WF_TIMEOUT_SHL;
 
			SndPlayFx(SND_15_BEEP);
 
			PlaceTreesRandomly();
 
			MarkWholeScreenDirty();
 
		case WE_TIMEOUT:
 
			w->RaiseWidget(16);
 
			break;
 
		}
 
	} break;
 

	
 
	case WE_PLACE_OBJ:
 
		VpStartPlaceSizing(e->we.place.tile, VPM_X_AND_Y_LIMITED, DDSP_PLANT_TREES);
 
		VpSetPlaceSizingLimit(20);
 
		break;
 

	
 
	case WE_PLACE_DRAG:
 
		VpSelectTilesWithMethod(e->we.place.pt.x, e->we.place.pt.y, e->we.place.select_method);
 
		return;
 

	
 
	case WE_PLACE_MOUSEUP:
 
		if (e->we.place.pt.x != -1 && e->we.place.select_proc == DDSP_PLANT_TREES) {
 
			DoCommandP(e->we.place.tile, _tree_to_plant, e->we.place.starttile, NULL,
 
				CMD_PLANT_TREE | CMD_MSG(STR_2805_CAN_T_PLANT_TREE_HERE));
 
		}
 
		break;
 

	
 
	case WE_TIMEOUT:
 
		w->RaiseWidget(16);
 
		break;
 

	
 
	case WE_ABORT_PLACE_OBJ:
 
		w->RaiseButtons();
 
		break;
 
		case WE_ABORT_PLACE_OBJ:
 
			w->RaiseButtons();
 
			break;
 
	}
 
}
 

	
 
@@ -513,70 +506,72 @@ static const Widget _errmsg_face_widgets
 
static void ErrmsgWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_PAINT:
 
		CopyInDParam(0, _errmsg_decode_params, lengthof(_errmsg_decode_params));
 
		DrawWindowWidgets(w);
 
		CopyInDParam(0, _errmsg_decode_params, lengthof(_errmsg_decode_params));
 
		case WE_PAINT:
 
			CopyInDParam(0, _errmsg_decode_params, lengthof(_errmsg_decode_params));
 
			DrawWindowWidgets(w);
 
			CopyInDParam(0, _errmsg_decode_params, lengthof(_errmsg_decode_params));
 

	
 
		/* If the error message comes from a NewGRF, we must use the text ref. stack reserved for error messages.
 
		 * If the message doesn't come from a NewGRF, it won't use the TTDP-style text ref. stack, so we won't hurt anything
 
		 */
 
		SwitchToErrorRefStack();
 
		RewindTextRefStack();
 
			/* If the error message comes from a NewGRF, we must use the text ref. stack reserved for error messages.
 
			* If the message doesn't come from a NewGRF, it won't use the TTDP-style text ref. stack, so we won't hurt anything
 
			*/
 
			SwitchToErrorRefStack();
 
			RewindTextRefStack();
 

	
 
		if (!IsWindowOfPrototype(w, _errmsg_face_widgets)) {
 
			DrawStringMultiCenter(
 
				120,
 
				(_errmsg_message_1 == INVALID_STRING_ID ? 25 : 15),
 
				_errmsg_message_2,
 
				w->width - 2);
 
			if (_errmsg_message_1 != INVALID_STRING_ID)
 
			if (!IsWindowOfPrototype(w, _errmsg_face_widgets)) {
 
				DrawStringMultiCenter(
 
					120,
 
					30,
 
					_errmsg_message_1,
 
					(_errmsg_message_1 == INVALID_STRING_ID ? 25 : 15),
 
					_errmsg_message_2,
 
					w->width - 2);
 
		} else {
 
			const Player *p = GetPlayer((PlayerID)GetDParamX(_errmsg_decode_params,2));
 
			DrawPlayerFace(p->face, p->player_color, 2, 16);
 
				if (_errmsg_message_1 != INVALID_STRING_ID) {
 
					DrawStringMultiCenter(
 
						120,
 
						30,
 
						_errmsg_message_1,
 
						w->width - 2);
 
				}
 
			} else {
 
				const Player *p = GetPlayer((PlayerID)GetDParamX(_errmsg_decode_params,2));
 
				DrawPlayerFace(p->face, p->player_color, 2, 16);
 

	
 
			DrawStringMultiCenter(
 
				214,
 
				(_errmsg_message_1 == INVALID_STRING_ID ? 65 : 45),
 
				_errmsg_message_2,
 
				w->width - 2);
 
			if (_errmsg_message_1 != INVALID_STRING_ID)
 
				DrawStringMultiCenter(
 
					214,
 
					90,
 
					_errmsg_message_1,
 
					(_errmsg_message_1 == INVALID_STRING_ID ? 65 : 45),
 
					_errmsg_message_2,
 
					w->width - 2);
 
		}
 
				if (_errmsg_message_1 != INVALID_STRING_ID) {
 
					DrawStringMultiCenter(
 
						214,
 
						90,
 
						_errmsg_message_1,
 
						w->width - 2);
 
				}
 
			}
 

	
 
		/* Switch back to the normal text ref. stack for NewGRF texts */
 
		SwitchToNormalRefStack();
 
		break;
 

	
 
	case WE_MOUSELOOP:
 
		if (_right_button_down) DeleteWindow(w);
 
		break;
 
			/* Switch back to the normal text ref. stack for NewGRF texts */
 
			SwitchToNormalRefStack();
 
			break;
 

	
 
	case WE_4:
 
		if (--_errmsg_duration == 0) DeleteWindow(w);
 
		break;
 
		case WE_MOUSELOOP:
 
			if (_right_button_down) DeleteWindow(w);
 
			break;
 

	
 
		case WE_4:
 
			if (--_errmsg_duration == 0) DeleteWindow(w);
 
			break;
 

	
 
	case WE_DESTROY:
 
		SetRedErrorSquare(0);
 
		_switch_mode_errorstr = INVALID_STRING_ID;
 
		break;
 
		case WE_DESTROY:
 
			SetRedErrorSquare(0);
 
			_switch_mode_errorstr = INVALID_STRING_ID;
 
			break;
 

	
 
	case WE_KEYPRESS:
 
		if (e->we.keypress.keycode == WKC_SPACE) {
 
			/* Don't continue. */
 
			e->we.keypress.cont = false;
 
			DeleteWindow(w);
 
		}
 
		break;
 
		case WE_KEYPRESS:
 
			if (e->we.keypress.keycode == WKC_SPACE) {
 
				/* Don't continue. */
 
				e->we.keypress.cont = false;
 
				DeleteWindow(w);
 
			}
 
			break;
 
	}
 
}
 

	
 
@@ -588,8 +583,7 @@ void ShowErrorMessage(StringID msg_1, St
 

	
 
	DeleteWindowById(WC_ERRMSG, 0);
 

	
 
	//assert(msg_2);
 
	if (msg_2 == 0) msg_2 = STR_EMPTY;
 
	if (msg_2 == STR_NULL) msg_2 = STR_EMPTY;
 

	
 
	_errmsg_message_1 = msg_1;
 
	_errmsg_message_2 = msg_2;
 
@@ -598,8 +592,7 @@ void ShowErrorMessage(StringID msg_1, St
 
	if (!_errmsg_duration) return;
 

	
 
	if (_errmsg_message_1 != STR_013B_OWNED_BY || GetDParamX(_errmsg_decode_params,2) >= 8) {
 

	
 
		if ( (x|y) != 0) {
 
		if ((x | y) != 0) {
 
			pt = RemapCoords2(x, y);
 
			vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 

	
 
@@ -617,7 +610,7 @@ void ShowErrorMessage(StringID msg_1, St
 
		}
 
		w = AllocateWindow(pt.x, pt.y, 240, 46, ErrmsgWndProc, WC_ERRMSG, _errmsg_widgets);
 
	} else {
 
		if ( (x|y) != 0) {
 
		if ((x | y) != 0) {
 
			pt = RemapCoords2(x, y);
 
			vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
			pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (334/2), 0, _screen.width - 334);
 
@@ -699,17 +692,15 @@ static const Widget _tooltips_widgets[] 
 
static void TooltipsWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
		case WE_PAINT: {
 
			uint arg;
 
		case WE_PAINT:
 
			GfxFillRect(0, 0, w->width - 1, w->height - 1, 0);
 
			GfxFillRect(1, 1, w->width - 2, w->height - 2, 0x44);
 

	
 
			for (arg = 0; arg < WP(w, tooltips_d).paramcount; arg++) {
 
			for (uint arg = 0; arg < WP(w, tooltips_d).paramcount; arg++) {
 
				SetDParam(arg, WP(w, tooltips_d).params[arg]);
 
			}
 
			DrawStringMultiCenter((w->width >> 1), (w->height >> 1) - 5, WP(w, tooltips_d).string_id, w->width - 2);
 
			break;
 
		}
 

	
 
		case WE_MOUSELOOP:
 
			/* We can show tooltips while dragging tools. These are shown as long as
 
@@ -731,21 +722,16 @@ static void TooltipsWndProc(Window *w, W
 
 * added to a tooltip; currently only supports parameters of {NUM} (integer) */
 
void GuiShowTooltipsWithArgs(StringID str, uint paramcount, const uint64 params[])
 
{
 
	char buffer[512];
 
	Dimension br;
 
	Window *w;
 
	uint i;
 
	int x, y;
 

	
 
	DeleteWindowById(WC_TOOLTIPS, 0);
 

	
 
	/* We only show measurement tooltips with patch setting on */
 
	if (str == STR_NULL || (paramcount != 0 && !_patches.measure_tooltip)) return;
 

	
 
	for (i = 0; i != paramcount; i++) SetDParam(i, params[i]);
 
	for (uint i = 0; i != paramcount; i++) SetDParam(i, params[i]);
 
	char buffer[512];
 
	GetString(buffer, str, lastof(buffer));
 

	
 
	br = GetStringBoundingBox(buffer);
 
	Dimension br = GetStringBoundingBox(buffer);
 
	br.width += 6; br.height += 4; // increase slightly to have some space around the box
 

	
 
	/* Cut tooltip length to 200 pixels max, wrap to new line if longer */
 
@@ -757,11 +743,11 @@ void GuiShowTooltipsWithArgs(StringID st
 
	/* Correctly position the tooltip position, watch out for window and cursor size
 
	 * Clamp value to below main toolbar and above statusbar. If tooltip would
 
	 * go below window, flip it so it is shown above the cursor */
 
	y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, 22, _screen.height - 12);
 
	int y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, 22, _screen.height - 12);
 
	if (y + br.height > _screen.height - 12) y = _cursor.pos.y + _cursor.offs.y - br.height - 5;
 
	x = Clamp(_cursor.pos.x - (br.width >> 1), 0, _screen.width - br.width);
 
	int x = Clamp(_cursor.pos.x - (br.width >> 1), 0, _screen.width - br.width);
 

	
 
	w = AllocateWindow(x, y, br.width, br.height, TooltipsWndProc, WC_TOOLTIPS, _tooltips_widgets);
 
	Window *w = AllocateWindow(x, y, br.width, br.height, TooltipsWndProc, WC_TOOLTIPS, _tooltips_widgets);
 

	
 
	WP(w, tooltips_d).string_id = str;
 
	assert(sizeof(WP(w, tooltips_d).params[0]) == sizeof(params[0]));
 
@@ -863,14 +849,12 @@ void SetHScrollCount(Window *w, int num)
 
static void DelChar(Textbuf *tb, bool backspace)
 
{
 
	WChar c;
 
	uint width;
 
	size_t len;
 
	char *s = tb->buf + tb->caretpos;
 

	
 
	if (backspace) s = Utf8PrevChar(s);
 

	
 
	len = Utf8Decode(&c, s);
 
	width = GetCharacterWidth(FS_NORMAL, c);
 
	size_t len = Utf8Decode(&c, s);
 
	uint width = GetCharacterWidth(FS_NORMAL, c);
 

	
 
	tb->width  -= width;
 
	if (backspace) {
 
@@ -949,35 +933,38 @@ bool InsertTextBufferChar(Textbuf *tb, W
 
bool MoveTextBufferPos(Textbuf *tb, int navmode)
 
{
 
	switch (navmode) {
 
	case WKC_LEFT:
 
		if (tb->caretpos != 0) {
 
			WChar c;
 
			const char *s = Utf8PrevChar(tb->buf + tb->caretpos);
 
			Utf8Decode(&c, s);
 
			tb->caretpos    = s - tb->buf; // -= (tb->buf + tb->caretpos - s)
 
			tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
 
		case WKC_LEFT:
 
			if (tb->caretpos != 0) {
 
				WChar c;
 
				const char *s = Utf8PrevChar(tb->buf + tb->caretpos);
 
				Utf8Decode(&c, s);
 
				tb->caretpos    = s - tb->buf; // -= (tb->buf + tb->caretpos - s)
 
				tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
 

	
 
			return true;
 
		}
 
		break;
 
	case WKC_RIGHT:
 
		if (tb->caretpos < tb->length) {
 
			WChar c;
 
				return true;
 
			}
 
			break;
 

	
 
		case WKC_RIGHT:
 
			if (tb->caretpos < tb->length) {
 
				WChar c;
 

	
 
			tb->caretpos   += Utf8Decode(&c, tb->buf + tb->caretpos);
 
			tb->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
 
				tb->caretpos   += Utf8Decode(&c, tb->buf + tb->caretpos);
 
				tb->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
 

	
 
				return true;
 
			}
 
			break;
 

	
 
		case WKC_HOME:
 
			tb->caretpos = 0;
 
			tb->caretxoffs = 0;
 
			return true;
 
		}
 
		break;
 
	case WKC_HOME:
 
		tb->caretpos = 0;
 
		tb->caretxoffs = 0;
 
		return true;
 
	case WKC_END:
 
		tb->caretpos = tb->length;
 
		tb->caretxoffs = tb->width;
 
		return true;
 

	
 
		case WKC_END:
 
			tb->caretpos = tb->length;
 
			tb->caretxoffs = tb->width;
 
			return true;
 
	}
 

	
 
	return false;
 
@@ -1029,32 +1016,33 @@ int HandleEditBoxKey(Window *w, querystr
 
	e->we.keypress.cont = false;
 

	
 
	switch (e->we.keypress.keycode) {
 
	case WKC_ESC: return 2;
 
	case WKC_RETURN: case WKC_NUM_ENTER: return 1;
 
	case (WKC_CTRL | 'V'):
 
		if (InsertTextBufferClipboard(&string->text))
 
			w->InvalidateWidget(wid);
 
		break;
 
	case (WKC_CTRL | 'U'):
 
		DeleteTextBufferAll(&string->text);
 
		w->InvalidateWidget(wid);
 
		break;
 
	case WKC_BACKSPACE: case WKC_DELETE:
 
		if (DeleteTextBufferChar(&string->text, e->we.keypress.keycode))
 
		case WKC_ESC: return 2;
 

	
 
		case WKC_RETURN: case WKC_NUM_ENTER: return 1;
 

	
 
		case (WKC_CTRL | 'V'):
 
			if (InsertTextBufferClipboard(&string->text)) w->InvalidateWidget(wid);
 
			break;
 

	
 
		case (WKC_CTRL | 'U'):
 
			DeleteTextBufferAll(&string->text);
 
			w->InvalidateWidget(wid);
 
		break;
 
	case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
 
		if (MoveTextBufferPos(&string->text, e->we.keypress.keycode))
 
			w->InvalidateWidget(wid);
 
		break;
 
	default:
 
		if (IsValidChar(e->we.keypress.key, string->afilter)) {
 
			if (InsertTextBufferChar(&string->text, e->we.keypress.key)) {
 
				w->InvalidateWidget(wid);
 
			break;
 

	
 
		case WKC_BACKSPACE: case WKC_DELETE:
 
			if (DeleteTextBufferChar(&string->text, e->we.keypress.keycode)) w->InvalidateWidget(wid);
 
			break;
 

	
 
		case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
 
			if (MoveTextBufferPos(&string->text, e->we.keypress.keycode)) w->InvalidateWidget(wid);
 
			break;
 

	
 
		default:
 
			if (IsValidChar(e->we.keypress.key, string->afilter)) {
 
				if (InsertTextBufferChar(&string->text, e->we.keypress.key)) w->InvalidateWidget(wid);
 
			} else { // key wasn't caught. Continue only if standard entry specified
 
				e->we.keypress.cont = (string->afilter == CS_ALPHANUMERAL);
 
			}
 
		} else { // key wasn't caught. Continue only if standard entry specified
 
			e->we.keypress.cont = (string->afilter == CS_ALPHANUMERAL);
 
		}
 
	}
 

	
 
	return 0;
 
@@ -1090,11 +1078,12 @@ void DrawEditBox(Window *w, querystr_d *
 

	
 
	/* Limit the drawing of the string inside the widget boundaries */
 
	if (!FillDrawPixelInfo(&dpi,
 
	      wi->left + 4,
 
	      wi->top + 1,
 
	      wi->right - wi->left - 4,
 
	      wi->bottom - wi->top - 1)
 
	) return;
 
			wi->left + 4,
 
			wi->top + 1,
 
			wi->right - wi->left - 4,
 
			wi->bottom - wi->top - 1)) {
 
		return;
 
	}
 

	
 
	old_dpi = _cur_dpi;
 
	_cur_dpi = &dpi;
 
@@ -1222,7 +1211,6 @@ char _orig_str_buf[lengthof(_edit_str_bu
 
 * @param afilter filters out unwanted character input */
 
void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth, Window *parent, CharSetFilter afilter)
 
{
 
	Window *w;
 
	uint realmaxlen = maxlen & ~0x1000;
 

	
 
	assert(realmaxlen < lengthof(_edit_str_buf));
 
@@ -1230,7 +1218,7 @@ void ShowQueryString(StringID str, Strin
 
	DeleteWindowById(WC_QUERY_STRING, 0);
 
	DeleteWindowById(WC_SAVELOAD, 0);
 

	
 
	w = AllocateWindowDesc(&_query_string_desc);
 
	Window *w = AllocateWindowDesc(&_query_string_desc);
 
	w->parent = parent;
 

	
 
	GetString(_edit_str_buf, str, lastof(_edit_str_buf));
 
@@ -1474,196 +1462,201 @@ static void SaveLoadDlgWndProc(Window *w
 
	static FiosItem o_dir;
 

	
 
	switch (e->event) {
 
	case WE_CREATE: // Set up OPENTTD button
 
		w->vscroll.cap = 10;
 
		w->resize.step_width = 2;
 
		w->resize.step_height = 10;
 

	
 
		o_dir.type = FIOS_TYPE_DIRECT;
 
		switch (_saveload_mode) {
 
			case SLD_SAVE_GAME:
 
			case SLD_LOAD_GAME:
 
				FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
 
				break;
 
		case WE_CREATE: // Set up OPENTTD button
 
			w->vscroll.cap = 10;
 
			w->resize.step_width = 2;
 
			w->resize.step_height = 10;
 

	
 
			case SLD_SAVE_SCENARIO:
 
			case SLD_LOAD_SCENARIO:
 
				FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
 
				break;
 

	
 
			case SLD_LOAD_HEIGHTMAP:
 
				FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
 
				break;
 

	
 
			default:
 
				ttd_strlcpy(o_dir.name, _personal_dir, lengthof(o_dir.name));
 
		}
 
		break;
 

	
 
	case WE_PAINT: {
 
		int pos;
 
		int y;
 
			o_dir.type = FIOS_TYPE_DIRECT;
 
			switch (_saveload_mode) {
 
				case SLD_SAVE_GAME:
 
				case SLD_LOAD_GAME:
 
					FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
 
					break;
 

	
 
		SetVScrollCount(w, _fios_num);
 
		DrawWindowWidgets(w);
 
		DrawFiosTexts(w->width);
 

	
 
		if (_savegame_sort_dirty) {
 
			_savegame_sort_dirty = false;
 
			MakeSortedSaveGameList();
 
		}
 

	
 
		GfxFillRect(w->widget[7].left + 1, w->widget[7].top + 1, w->widget[7].right, w->widget[7].bottom, 0xD7);
 
		DrawSortButtonState(w, _savegame_sort_order & SORT_BY_NAME ? 2 : 3, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
 

	
 
		y = w->widget[7].top + 1;
 
		for (pos = w->vscroll.pos; pos < _fios_num; pos++) {
 
			const FiosItem *item = _fios_list + pos;
 
				case SLD_SAVE_SCENARIO:
 
				case SLD_LOAD_SCENARIO:
 
					FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
 
					break;
 

	
 
			DoDrawStringTruncated(item->title, 4, y, _fios_colors[item->type], w->width - 18);
 
			y += 10;
 
			if (y >= w->vscroll.cap * 10 + w->widget[7].top + 1) break;
 
		}
 
				case SLD_LOAD_HEIGHTMAP:
 
					FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
 
					break;
 

	
 
		if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
 
			DrawEditBox(w, &WP(w, querystr_d), 10);
 
		}
 
		break;
 
	}
 

	
 
	case WE_CLICK:
 
		switch (e->we.click.widget) {
 
		case 2: // Sort save names by name
 
			_savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
 
				SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
 
			_savegame_sort_dirty = true;
 
			SetWindowDirty(w);
 
				default:
 
					ttd_strlcpy(o_dir.name, _personal_dir, lengthof(o_dir.name));
 
			}
 
			break;
 

	
 
		case 3: // Sort save names by date
 
			_savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
 
				SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
 
			_savegame_sort_dirty = true;
 
			SetWindowDirty(w);
 
			break;
 
		case WE_PAINT: {
 
			int pos;
 
			int y;
 

	
 
		case 6: // OpenTTD 'button', jumps to OpenTTD directory
 
			FiosBrowseTo(&o_dir);
 
			SetWindowDirty(w);
 
			BuildFileList();
 
			break;
 
			SetVScrollCount(w, _fios_num);
 
			DrawWindowWidgets(w);
 
			DrawFiosTexts(w->width);
 

	
 
		case 7: { // Click the listbox
 
			int y = (e->we.click.pt.y - w->widget[e->we.click.widget].top - 1) / 10;
 
			char *name;
 
			const FiosItem *file;
 

	
 
			if (y < 0 || (y += w->vscroll.pos) >= w->vscroll.count) return;
 

	
 
			file = _fios_list + y;
 
			if (_savegame_sort_dirty) {
 
				_savegame_sort_dirty = false;
 
				MakeSortedSaveGameList();
 
			}
 

	
 
			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;
 
			GfxFillRect(w->widget[7].left + 1, w->widget[7].top + 1, w->widget[7].right, w->widget[7].bottom, 0xD7);
 
			DrawSortButtonState(w, _savegame_sort_order & SORT_BY_NAME ? 2 : 3, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
 

	
 
					SetFiosType(file->type);
 
					ttd_strlcpy(_file_to_saveload.name, name, sizeof(_file_to_saveload.name));
 
					ttd_strlcpy(_file_to_saveload.title, file->title, sizeof(_file_to_saveload.title));
 
			y = w->widget[7].top + 1;
 
			for (pos = w->vscroll.pos; pos < _fios_num; pos++) {
 
				const FiosItem *item = _fios_list + pos;
 

	
 
					DeleteWindow(w);
 
				} else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
 
					SetFiosType(file->type);
 
					ttd_strlcpy(_file_to_saveload.name, name, sizeof(_file_to_saveload.name));
 
					ttd_strlcpy(_file_to_saveload.title, file->title, sizeof(_file_to_saveload.title));
 
				DoDrawStringTruncated(item->title, 4, y, _fios_colors[item->type], w->width - 18);
 
				y += 10;
 
				if (y >= w->vscroll.cap * 10 + w->widget[7].top + 1) break;
 
			}
 

	
 
					DeleteWindow(w);
 
					ShowHeightmapLoad();
 
				} else {
 
					/* SLD_SAVE_GAME, SLD_SAVE_SCENARIO copy clicked name to editbox */
 
					ttd_strlcpy(WP(w, querystr_d).text.buf, file->title, WP(w, querystr_d).text.maxlength);
 
					UpdateTextBufferSize(&WP(w, querystr_d).text);
 
					w->InvalidateWidget(10);
 
				}
 
			} else {
 
				/* Changed directory, need repaint. */
 
				SetWindowDirty(w);
 
				BuildFileList();
 
			if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
 
				DrawEditBox(w, &WP(w, querystr_d), 10);
 
			}
 
			break;
 
		}
 

	
 
		case 10: // edit box
 
			ShowOnScreenKeyboard(w, &WP(w, querystr_d), e->we.click.widget, 0, 0);
 
		case WE_CLICK:
 
			switch (e->we.click.widget) {
 
				case 2: // Sort save names by name
 
					_savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
 
						SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
 
					_savegame_sort_dirty = true;
 
					SetWindowDirty(w);
 
					break;
 

	
 
				case 3: // Sort save names by date
 
					_savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
 
						SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
 
					_savegame_sort_dirty = true;
 
					SetWindowDirty(w);
 
					break;
 

	
 
				case 6: // OpenTTD 'button', jumps to OpenTTD directory
 
					FiosBrowseTo(&o_dir);
 
					SetWindowDirty(w);
 
					BuildFileList();
 
					break;
 

	
 
				case 7: { // Click the listbox
 
					int y = (e->we.click.pt.y - w->widget[e->we.click.widget].top - 1) / 10;
 
					char *name;
 
					const FiosItem *file;
 

	
 
					if (y < 0 || (y += w->vscroll.pos) >= w->vscroll.count) return;
 

	
 
					file = _fios_list + y;
 

	
 
					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;
 

	
 
							SetFiosType(file->type);
 
							ttd_strlcpy(_file_to_saveload.name, name, sizeof(_file_to_saveload.name));
 
							ttd_strlcpy(_file_to_saveload.title, file->title, sizeof(_file_to_saveload.title));
 

	
 
							DeleteWindow(w);
 
						} else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
 
							SetFiosType(file->type);
 
							ttd_strlcpy(_file_to_saveload.name, name, sizeof(_file_to_saveload.name));
 
							ttd_strlcpy(_file_to_saveload.title, file->title, sizeof(_file_to_saveload.title));
 

	
 
							DeleteWindow(w);
 
							ShowHeightmapLoad();
 
						} else {
 
							/* SLD_SAVE_GAME, SLD_SAVE_SCENARIO copy clicked name to editbox */
 
							ttd_strlcpy(WP(w, querystr_d).text.buf, file->title, WP(w, querystr_d).text.maxlength);
 
							UpdateTextBufferSize(&WP(w, querystr_d).text);
 
							w->InvalidateWidget(10);
 
						}
 
					} else {
 
						/* Changed directory, need repaint. */
 
						SetWindowDirty(w);
 
						BuildFileList();
 
					}
 
					break;
 
				}
 

	
 
				case 10: // edit box
 
					ShowOnScreenKeyboard(w, &WP(w, querystr_d), e->we.click.widget, 0, 0);
 
					break;
 

	
 
				case 11: case 12: // Delete, Save game
 
					break;
 
			}
 
			break;
 

	
 
		case 11: case 12: // Delete, Save game
 
		case WE_MOUSELOOP:
 
			if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
 
				HandleEditBox(w, &WP(w, querystr_d), 10);
 
			}
 
			break;
 
		}
 
		break;
 
	case WE_MOUSELOOP:
 
		if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
 
			HandleEditBox(w, &WP(w, querystr_d), 10);
 
		}
 
		break;
 
	case WE_KEYPRESS:
 
		if (e->we.keypress.keycode == WKC_ESC) {
 
			DeleteWindow(w);
 
			return;
 
		}
 

	
 
		if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
 
			if (HandleEditBoxKey(w, &WP(w, querystr_d), 10, e) == 1) // Press Enter
 
					w->HandleButtonClick(12);
 
		}
 
		break;
 
	case WE_TIMEOUT:
 
		/* This test protects against using widgets 11 and 12 which are only available
 
		 * in those two saveload mode  */
 
		if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO)) break;
 

	
 
		if (w->IsWidgetLowered(11)) { // Delete button clicked
 
			if (!FiosDelete(WP(w, querystr_d).text.buf)) {
 
				ShowErrorMessage(INVALID_STRING_ID, STR_4008_UNABLE_TO_DELETE_FILE, 0, 0);
 
			} else {
 
				BuildFileList();
 
				/* Reset file name to current date on successful delete */
 
				if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
 
		case WE_KEYPRESS:
 
			if (e->we.keypress.keycode == WKC_ESC) {
 
				DeleteWindow(w);
 
				return;
 
			}
 

	
 
			UpdateTextBufferSize(&WP(w, querystr_d).text);
 
			SetWindowDirty(w);
 
		} else if (w->IsWidgetLowered(12)) { // Save button clicked
 
			_switch_mode = SM_SAVE;
 
			FiosMakeSavegameName(_file_to_saveload.name, WP(w, querystr_d).text.buf, sizeof(_file_to_saveload.name));
 
			if ((_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) &&
 
					HandleEditBoxKey(w, &WP(w, querystr_d), 10, e) == 1) { // Press Enter
 
				w->HandleButtonClick(12);
 
			}
 
			break;
 

	
 
		case WE_TIMEOUT:
 
			/* This test protects against using widgets 11 and 12 which are only available
 
			* in those two saveload mode  */
 
			if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO)) break;
 

	
 
			if (w->IsWidgetLowered(11)) { // Delete button clicked
 
				if (!FiosDelete(WP(w, querystr_d).text.buf)) {
 
					ShowErrorMessage(INVALID_STRING_ID, STR_4008_UNABLE_TO_DELETE_FILE, 0, 0);
 
				} else {
 
					BuildFileList();
 
					/* Reset file name to current date on successful delete */
 
					if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
 
				}
 

	
 
				UpdateTextBufferSize(&WP(w, querystr_d).text);
 
				SetWindowDirty(w);
 
			} else if (w->IsWidgetLowered(12)) { // Save button clicked
 
				_switch_mode = SM_SAVE;
 
				FiosMakeSavegameName(_file_to_saveload.name, WP(w, querystr_d).text.buf, sizeof(_file_to_saveload.name));
 

	
 
			/* In the editor set up the vehicle engines correctly (date might have changed) */
 
			if (_game_mode == GM_EDITOR) StartupEngines();
 
		}
 
		break;
 
	case WE_DESTROY:
 
		/* pause is only used in single-player, non-editor mode, non menu mode */
 
		if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
 
			if (_pause_game >= 0) DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
 
		}
 
		FiosFreeSavegameList();
 
		ClrBit(_no_scroll, SCROLL_SAVE);
 
		break;
 
	case WE_RESIZE: {
 
		/* Widget 2 and 3 have to go with halve speed, make it so obiwan */
 
		uint diff = e->we.sizing.diff.x / 2;
 
		w->widget[2].right += diff;
 
		w->widget[3].left  += diff;
 
		w->widget[3].right += e->we.sizing.diff.x;
 
				/* In the editor set up the vehicle engines correctly (date might have changed) */
 
				if (_game_mode == GM_EDITOR) StartupEngines();
 
			}
 
			break;
 

	
 
		case WE_DESTROY:
 
			/* pause is only used in single-player, non-editor mode, non menu mode */
 
			if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
 
				if (_pause_game >= 0) DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
 
			}
 
			FiosFreeSavegameList();
 
			ClrBit(_no_scroll, SCROLL_SAVE);
 
			break;
 

	
 
		/* Same for widget 11 and 12 in save-dialog */
 
		if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
 
			w->widget[11].right += diff;
 
			w->widget[12].left  += diff;
 
			w->widget[12].right += e->we.sizing.diff.x;
 
		}
 
		case WE_RESIZE: {
 
			/* Widget 2 and 3 have to go with halve speed, make it so obiwan */
 
			uint diff = e->we.sizing.diff.x / 2;
 
			w->widget[2].right += diff;
 
			w->widget[3].left  += diff;
 
			w->widget[3].right += e->we.sizing.diff.x;
 

	
 
		w->vscroll.cap += e->we.sizing.diff.y / 10;
 
			/* Same for widget 11 and 12 in save-dialog */
 
			if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
 
				w->widget[11].right += diff;
 
				w->widget[12].left  += diff;
 
				w->widget[12].right += e->we.sizing.diff.x;
 
			}
 

	
 
			w->vscroll.cap += e->we.sizing.diff.y / 10;
 
		} break;
 
	}
 
}
src/network/network_gui.cpp
Show inline comments
 
@@ -255,58 +255,57 @@ static void NetworkGameWindowWndProc(Win
 
	list_d *ld = &WP(w, network_ql_d).l;
 

	
 
	switch (e->event) {
 
	case WE_CREATE: // Focus input box
 
		w->vscroll.cap = 13;
 
		w->resize.step_height = NET_PRC__SIZE_OF_ROW;
 
		case WE_CREATE: // Focus input box
 
			w->vscroll.cap = 13;
 
			w->resize.step_height = NET_PRC__SIZE_OF_ROW;
 

	
 
		nd->field = NGWW_PLAYER;
 
		nd->server = NULL;
 
			nd->field = NGWW_PLAYER;
 
			nd->server = NULL;
 

	
 
		WP(w, network_ql_d).sort_list = NULL;
 
		ld->flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE);
 
		ld->sort_type = _ng_sorting.criteria;
 
		break;
 
			WP(w, network_ql_d).sort_list = NULL;
 
			ld->flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE);
 
			ld->sort_type = _ng_sorting.criteria;
 
			break;
 

	
 
	case WE_PAINT: {
 
		const NetworkGameList *sel = nd->server;
 
		const SortButtonState arrow = (ld->flags & VL_DESC) ? SBS_DOWN : SBS_UP;
 
		case WE_PAINT: {
 
			const NetworkGameList *sel = nd->server;
 
			const SortButtonState arrow = (ld->flags & VL_DESC) ? SBS_DOWN : SBS_UP;
 

	
 
		if (ld->flags & VL_REBUILD) {
 
			BuildNetworkGameList(&WP(w, network_ql_d));
 
			SetVScrollCount(w, ld->list_length);
 
		}
 
		if (ld->flags & VL_RESORT) SortNetworkGameList(&WP(w, network_ql_d));
 
			if (ld->flags & VL_REBUILD) {
 
				BuildNetworkGameList(&WP(w, network_ql_d));
 
				SetVScrollCount(w, ld->list_length);
 
			}
 
			if (ld->flags & VL_RESORT) SortNetworkGameList(&WP(w, network_ql_d));
 

	
 
		/* 'Refresh' button invisible if no server selected */
 
		w->SetWidgetDisabledState(NGWW_REFRESH, sel == NULL);
 
		/* 'Join' button disabling conditions */
 
		w->SetWidgetDisabledState(NGWW_JOIN, sel == NULL || // no Selected Server
 
				!sel->online || // Server offline
 
				sel->info.clients_on >= sel->info.clients_max || // Server full
 
				!sel->info.compatible); // Revision mismatch
 
			/* 'Refresh' button invisible if no server selected */
 
			w->SetWidgetDisabledState(NGWW_REFRESH, sel == NULL);
 
			/* 'Join' button disabling conditions */
 
			w->SetWidgetDisabledState(NGWW_JOIN, sel == NULL || // no Selected Server
 
					!sel->online || // Server offline
 
					sel->info.clients_on >= sel->info.clients_max || // Server full
 
					!sel->info.compatible); // Revision mismatch
 

	
 
		/* 'NewGRF Settings' button invisible if no NewGRF is used */
 
		w->SetWidgetHiddenState(NGWW_NEWGRF, sel == NULL ||
 
				!sel->online ||
 
				sel->info.grfconfig == NULL);
 
			/* 'NewGRF Settings' button invisible if no NewGRF is used */
 
			w->SetWidgetHiddenState(NGWW_NEWGRF, sel == NULL ||
 
					!sel->online ||
 
					sel->info.grfconfig == NULL);
 

	
 
		SetDParam(0, 0x00);
 
		SetDParam(1, _lan_internet_types_dropdown[_network_lan_internet]);
 
		DrawWindowWidgets(w);
 
			SetDParam(0, 0x00);
 
			SetDParam(1, _lan_internet_types_dropdown[_network_lan_internet]);
 
			DrawWindowWidgets(w);
 

	
 
		/* Edit box to set player name */
 
		DrawEditBox(w, &WP(w, network_ql_d).q, NGWW_PLAYER);
 

	
 
		DrawString(w->widget[NGWW_PLAYER].left - 100, 23, STR_NETWORK_PLAYER_NAME, TC_GOLD);
 
			/* Edit box to set player name */
 
			DrawEditBox(w, &WP(w, network_ql_d).q, NGWW_PLAYER);
 

	
 
		/* Sort based on widgets: name, clients, compatibility */
 
		switch (ld->sort_type) {
 
			case NGWW_NAME    - NGWW_NAME: DrawSortButtonState(w, NGWW_NAME,    arrow); break;
 
			case NGWW_CLIENTS - NGWW_NAME: DrawSortButtonState(w, NGWW_CLIENTS, arrow); break;
 
			case NGWW_INFO    - NGWW_NAME: DrawSortButtonState(w, NGWW_INFO,    arrow); break;
 
		}
 
			DrawString(w->widget[NGWW_PLAYER].left - 100, 23, STR_NETWORK_PLAYER_NAME, TC_GOLD);
 

	
 
		{ // draw list of games
 
			/* Sort based on widgets: name, clients, compatibility */
 
			switch (ld->sort_type) {
 
				case NGWW_NAME    - NGWW_NAME: DrawSortButtonState(w, NGWW_NAME,    arrow); break;
 
				case NGWW_CLIENTS - NGWW_NAME: DrawSortButtonState(w, NGWW_CLIENTS, arrow); break;
 
				case NGWW_INFO    - NGWW_NAME: DrawSortButtonState(w, NGWW_INFO,    arrow); break;
 
			}
 

	
 
			uint16 y = NET_PRC__OFFSET_TOP_WIDGET + 3;
 
			int32 n = 0;
 
			int32 pos = w->vscroll.pos;
 
@@ -347,261 +346,270 @@ static void NetworkGameWindowWndProc(Win
 
				y += NET_PRC__SIZE_OF_ROW;
 
				if (++n == w->vscroll.cap) break; // max number of games in the window
 
			}
 
		}
 

	
 
		/* Draw the right menu */
 
		GfxFillRect(w->widget[NGWW_DETAILS].left + 1, 43, w->widget[NGWW_DETAILS].right - 1, 92, 157);
 
		if (sel == NULL) {
 
			DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 58, STR_NETWORK_GAME_INFO, TC_FROMSTRING);
 
		} else if (!sel->online) {
 
			SetDParamStr(0, sel->info.server_name);
 
			DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 68, STR_ORANGE, TC_FROMSTRING); // game name
 
			/* Draw the right menu */
 
			GfxFillRect(w->widget[NGWW_DETAILS].left + 1, 43, w->widget[NGWW_DETAILS].right - 1, 92, 157);
 
			if (sel == NULL) {
 
				DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 58, STR_NETWORK_GAME_INFO, TC_FROMSTRING);
 
			} else if (!sel->online) {
 
				SetDParamStr(0, sel->info.server_name);
 
				DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 68, STR_ORANGE, TC_FROMSTRING); // game name
 

	
 
			DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 132, STR_NETWORK_SERVER_OFFLINE, TC_FROMSTRING); // server offline
 
		} else { // show game info
 
			uint16 y = 100;
 
			const uint16 x = w->widget[NGWW_DETAILS].left + 5;
 
				DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 132, STR_NETWORK_SERVER_OFFLINE, TC_FROMSTRING); // server offline
 
			} else { // show game info
 
				uint16 y = 100;
 
				const uint16 x = w->widget[NGWW_DETAILS].left + 5;
 

	
 
			DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 48, STR_NETWORK_GAME_INFO, TC_FROMSTRING);
 
				DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, 48, STR_NETWORK_GAME_INFO, TC_FROMSTRING);
 

	
 

	
 
			SetDParamStr(0, sel->info.server_name);
 
			DrawStringCenteredTruncated(w->widget[NGWW_DETAILS].left, w->widget[NGWW_DETAILS].right, 62, STR_ORANGE, TC_BLACK); // game name
 
				SetDParamStr(0, sel->info.server_name);
 
				DrawStringCenteredTruncated(w->widget[NGWW_DETAILS].left, w->widget[NGWW_DETAILS].right, 62, STR_ORANGE, TC_BLACK); // game name
 

	
 
				SetDParamStr(0, sel->info.map_name);
 
				DrawStringCenteredTruncated(w->widget[NGWW_DETAILS].left, w->widget[NGWW_DETAILS].right, 74, STR_02BD, TC_BLACK); // map name
 

	
 
			SetDParamStr(0, sel->info.map_name);
 
			DrawStringCenteredTruncated(w->widget[NGWW_DETAILS].left, w->widget[NGWW_DETAILS].right, 74, STR_02BD, TC_BLACK); // map name
 
				SetDParam(0, sel->info.clients_on);
 
				SetDParam(1, sel->info.clients_max);
 
				SetDParam(2, sel->info.companies_on);
 
				SetDParam(3, sel->info.companies_max);
 
				DrawString(x, y, STR_NETWORK_CLIENTS, TC_GOLD);
 
				y += 10;
 

	
 
				SetDParam(0, STR_NETWORK_LANG_ANY + sel->info.server_lang);
 
				DrawString(x, y, STR_NETWORK_LANGUAGE, TC_GOLD); // server language
 
				y += 10;
 

	
 
			SetDParam(0, sel->info.clients_on);
 
			SetDParam(1, sel->info.clients_max);
 
			SetDParam(2, sel->info.companies_on);
 
			SetDParam(3, sel->info.companies_max);
 
			DrawString(x, y, STR_NETWORK_CLIENTS, TC_GOLD);
 
			y += 10;
 
				SetDParam(0, STR_TEMPERATE_LANDSCAPE + sel->info.map_set);
 
				DrawString(x, y, STR_NETWORK_TILESET, TC_GOLD); // tileset
 
				y += 10;
 

	
 
				SetDParam(0, sel->info.map_width);
 
				SetDParam(1, sel->info.map_height);
 
				DrawString(x, y, STR_NETWORK_MAP_SIZE, TC_GOLD); // map size
 
				y += 10;
 

	
 
				SetDParamStr(0, sel->info.server_revision);
 
				DrawString(x, y, STR_NETWORK_SERVER_VERSION, TC_GOLD); // server version
 
				y += 10;
 

	
 
				SetDParamStr(0, sel->info.hostname);
 
				SetDParam(1, sel->port);
 
				DrawString(x, y, STR_NETWORK_SERVER_ADDRESS, TC_GOLD); // server address
 
				y += 10;
 

	
 
			SetDParam(0, STR_NETWORK_LANG_ANY + sel->info.server_lang);
 
			DrawString(x, y, STR_NETWORK_LANGUAGE, TC_GOLD); // server language
 
			y += 10;
 
				SetDParam(0, sel->info.start_date);
 
				DrawString(x, y, STR_NETWORK_START_DATE, TC_GOLD); // start date
 
				y += 10;
 

	
 
				SetDParam(0, sel->info.game_date);
 
				DrawString(x, y, STR_NETWORK_CURRENT_DATE, TC_GOLD); // current date
 
				y += 10;
 

	
 
				y += 2;
 

	
 
				if (!sel->info.compatible) {
 
					DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, y, sel->info.version_compatible ? STR_NETWORK_GRF_MISMATCH : STR_NETWORK_VERSION_MISMATCH, TC_FROMSTRING); // server mismatch
 
				} else if (sel->info.clients_on == sel->info.clients_max) {
 
					/* Show: server full, when clients_on == clients_max */
 
					DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, y, STR_NETWORK_SERVER_FULL, TC_FROMSTRING); // server full
 
				} else if (sel->info.use_password) {
 
					DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, y, STR_NETWORK_PASSWORD, TC_FROMSTRING); // password warning
 
				}
 

	
 
			SetDParam(0, STR_TEMPERATE_LANDSCAPE + sel->info.map_set);
 
			DrawString(x, y, STR_NETWORK_TILESET, TC_GOLD); // tileset
 
			y += 10;
 
				y += 10;
 
			}
 
		} break;
 

	
 
			SetDParam(0, sel->info.map_width);
 
			SetDParam(1, sel->info.map_height);
 
			DrawString(x, y, STR_NETWORK_MAP_SIZE, TC_GOLD); // map size
 
			y += 10;
 
		case WE_CLICK:
 
			nd->field = e->we.click.widget;
 
			switch (e->we.click.widget) {
 
				case NGWW_PLAYER:
 
					ShowOnScreenKeyboard(w, &WP(w, network_ql_d).q,  NGWW_PLAYER, 0, 0);
 
					break;
 

	
 
				case NGWW_CANCEL: // Cancel button
 
					DeleteWindowById(WC_NETWORK_WINDOW, 0);
 
					break;
 

	
 
				case NGWW_CONN_BTN: // 'Connection' droplist
 
					ShowDropDownMenu(w, _lan_internet_types_dropdown, _network_lan_internet, NGWW_CONN_BTN, 0, 0); // do it for widget NSSW_CONN_BTN
 
					break;
 

	
 
			SetDParamStr(0, sel->info.server_revision);
 
			DrawString(x, y, STR_NETWORK_SERVER_VERSION, TC_GOLD); // server version
 
			y += 10;
 
				case NGWW_NAME: // Sort by name
 
				case NGWW_CLIENTS: // Sort by connected clients
 
				case NGWW_INFO: // Connectivity (green dot)
 
					if (ld->sort_type == e->we.click.widget - NGWW_NAME) ld->flags ^= VL_DESC;
 
					ld->flags |= VL_RESORT;
 
					ld->sort_type = e->we.click.widget - NGWW_NAME;
 

	
 
					_ng_sorting.order = !!(ld->flags & VL_DESC);
 
					_ng_sorting.criteria = ld->sort_type;
 
					SetWindowDirty(w);
 
					break;
 

	
 
				case NGWW_MATRIX: { // Matrix to show networkgames
 
					NetworkGameList *cur_item;
 
					uint32 id_v = (e->we.click.pt.y - NET_PRC__OFFSET_TOP_WIDGET) / NET_PRC__SIZE_OF_ROW;
 

	
 
			SetDParamStr(0, sel->info.hostname);
 
			SetDParam(1, sel->port);
 
			DrawString(x, y, STR_NETWORK_SERVER_ADDRESS, TC_GOLD); // server address
 
			y += 10;
 
					if (id_v >= w->vscroll.cap) return; // click out of bounds
 
					id_v += w->vscroll.pos;
 

	
 
					cur_item = _network_game_list;
 
					for (; id_v > 0 && cur_item != NULL; id_v--) cur_item = cur_item->next;
 

	
 
			SetDParam(0, sel->info.start_date);
 
			DrawString(x, y, STR_NETWORK_START_DATE, TC_GOLD); // start date
 
			y += 10;
 
					nd->server = cur_item;
 
					SetWindowDirty(w);
 
				} break;
 

	
 
				case NGWW_FIND: // Find server automatically
 
					switch (_network_lan_internet) {
 
						case 0: NetworkUDPSearchGame(); break;
 
						case 1: NetworkUDPQueryMasterServer(); break;
 
					}
 
					break;
 

	
 
			SetDParam(0, sel->info.game_date);
 
			DrawString(x, y, STR_NETWORK_CURRENT_DATE, TC_GOLD); // current date
 
			y += 10;
 
				case NGWW_ADD: // Add a server
 
					ShowQueryString(
 
						BindCString(_network_default_ip),
 
						STR_NETWORK_ENTER_IP,
 
						31 | 0x1000,  // maximum number of characters OR
 
						250, // characters up to this width pixels, whichever is satisfied first
 
						w, CS_ALPHANUMERAL);
 
					break;
 

	
 
			y += 2;
 
				case NGWW_START: // Start server
 
					ShowNetworkStartServerWindow();
 
					break;
 

	
 
			if (!sel->info.compatible) {
 
				DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, y, sel->info.version_compatible ? STR_NETWORK_GRF_MISMATCH : STR_NETWORK_VERSION_MISMATCH, TC_FROMSTRING); // server mismatch
 
			} else if (sel->info.clients_on == sel->info.clients_max) {
 
				/* Show: server full, when clients_on == clients_max */
 
				DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, y, STR_NETWORK_SERVER_FULL, TC_FROMSTRING); // server full
 
			} else if (sel->info.use_password) {
 
				DrawStringCentered(w->widget[NGWW_DETAILS].left + 115, y, STR_NETWORK_PASSWORD, TC_FROMSTRING); // password warning
 
				case NGWW_JOIN: // Join Game
 
					if (nd->server != NULL) {
 
						snprintf(_network_last_host, sizeof(_network_last_host), "%s", inet_ntoa(*(struct in_addr *)&nd->server->ip));
 
						_network_last_port = nd->server->port;
 
						ShowNetworkLobbyWindow(nd->server);
 
					}
 
					break;
 

	
 
				case NGWW_REFRESH: // Refresh
 
					if (nd->server != NULL) NetworkUDPQueryServer(nd->server->info.hostname, nd->server->port);
 
					break;
 

	
 
				case NGWW_NEWGRF: // NewGRF Settings
 
					if (nd->server != NULL) ShowNewGRFSettings(false, false, false, &nd->server->info.grfconfig);
 
					break;
 
			}
 
			break;
 

	
 
		case WE_DROPDOWN_SELECT: // we have selected a dropdown item in the list
 
			switch (e->we.dropdown.button) {
 
				case NGWW_CONN_BTN:
 
					_network_lan_internet = e->we.dropdown.index;
 
					break;
 

	
 
				default:
 
					NOT_REACHED();
 
			}
 

	
 
			y += 10;
 
		}
 
	} break;
 

	
 
	case WE_CLICK:
 
		nd->field = e->we.click.widget;
 
		switch (e->we.click.widget) {
 
		case NGWW_PLAYER:
 
			ShowOnScreenKeyboard(w, &WP(w, network_ql_d).q,  NGWW_PLAYER, 0, 0);
 
			break;
 
		case NGWW_CANCEL: // Cancel button
 
			DeleteWindowById(WC_NETWORK_WINDOW, 0);
 
			break;
 
		case NGWW_CONN_BTN: // 'Connection' droplist
 
			ShowDropDownMenu(w, _lan_internet_types_dropdown, _network_lan_internet, NGWW_CONN_BTN, 0, 0); // do it for widget NSSW_CONN_BTN
 
			break;
 
		case NGWW_NAME: // Sort by name
 
		case NGWW_CLIENTS: // Sort by connected clients
 
		case NGWW_INFO: // Connectivity (green dot)
 
			if (ld->sort_type == e->we.click.widget - NGWW_NAME) ld->flags ^= VL_DESC;
 
			ld->flags |= VL_RESORT;
 
			ld->sort_type = e->we.click.widget - NGWW_NAME;
 

	
 
			_ng_sorting.order = !!(ld->flags & VL_DESC);
 
			_ng_sorting.criteria = ld->sort_type;
 
			SetWindowDirty(w);
 
			break;
 
		case NGWW_MATRIX: { // Matrix to show networkgames
 
			NetworkGameList *cur_item;
 
			uint32 id_v = (e->we.click.pt.y - NET_PRC__OFFSET_TOP_WIDGET) / NET_PRC__SIZE_OF_ROW;
 

	
 
			if (id_v >= w->vscroll.cap) return; // click out of bounds
 
			id_v += w->vscroll.pos;
 

	
 
			cur_item = _network_game_list;
 
			for (; id_v > 0 && cur_item != NULL; id_v--) cur_item = cur_item->next;
 

	
 
			nd->server = cur_item;
 
			SetWindowDirty(w);
 
		} break;
 
		case NGWW_FIND: // Find server automatically
 
			switch (_network_lan_internet) {
 
				case 0: NetworkUDPSearchGame(); break;
 
				case 1: NetworkUDPQueryMasterServer(); break;
 
			}
 
		case WE_MOUSELOOP:
 
			if (nd->field == NGWW_PLAYER) HandleEditBox(w, &WP(w, network_ql_d).q, NGWW_PLAYER);
 
			break;
 
		case NGWW_ADD: { // Add a server
 
			ShowQueryString(
 
				BindCString(_network_default_ip),
 
				STR_NETWORK_ENTER_IP,
 
				31 | 0x1000,  // maximum number of characters OR
 
				250, // characters up to this width pixels, whichever is satisfied first
 
				w, CS_ALPHANUMERAL);
 
		} break;
 
		case NGWW_START: // Start server
 
			ShowNetworkStartServerWindow();
 
			break;
 
		case NGWW_JOIN: // Join Game
 
			if (nd->server != NULL) {
 
				snprintf(_network_last_host, sizeof(_network_last_host), "%s", inet_ntoa(*(struct in_addr *)&nd->server->ip));
 
				_network_last_port = nd->server->port;
 
				ShowNetworkLobbyWindow(nd->server);
 
			}
 
			break;
 
		case NGWW_REFRESH: // Refresh
 
			if (nd->server != NULL)
 
				NetworkUDPQueryServer(nd->server->info.hostname, nd->server->port);
 
			break;
 
		case NGWW_NEWGRF: // NewGRF Settings
 
			if (nd->server != NULL) ShowNewGRFSettings(false, false, false, &nd->server->info.grfconfig);
 

	
 
		case WE_MESSAGE:
 
			if (e->we.message.msg != 0) nd->server = NULL;
 
			ld->flags |= VL_REBUILD;
 
			SetWindowDirty(w);
 
			break;
 

	
 
	} break;
 

	
 
	case WE_DROPDOWN_SELECT: // we have selected a dropdown item in the list
 
		switch (e->we.dropdown.button) {
 
			case NGWW_CONN_BTN:
 
				_network_lan_internet = e->we.dropdown.index;
 
		case WE_KEYPRESS:
 
			if (nd->field != NGWW_PLAYER) {
 
				if (nd->server != NULL) {
 
					if (e->we.keypress.keycode == WKC_DELETE) { // Press 'delete' to remove servers
 
						NetworkGameListRemoveItem(nd->server);
 
						NetworkRebuildHostList();
 
						nd->server = NULL;
 
					}
 
				}
 
				break;
 
			default:
 
				NOT_REACHED();
 
		}
 

	
 
		SetWindowDirty(w);
 
		break;
 

	
 
	case WE_MOUSELOOP:
 
		if (nd->field == NGWW_PLAYER) HandleEditBox(w, &WP(w, network_ql_d).q, NGWW_PLAYER);
 
		break;
 
			}
 

	
 
	case WE_MESSAGE:
 
		if (e->we.message.msg != 0) nd->server = NULL;
 
		ld->flags |= VL_REBUILD;
 
		SetWindowDirty(w);
 
		break;
 
			if (HandleEditBoxKey(w, &WP(w, network_ql_d).q, NGWW_PLAYER, e) == 1) break; // enter pressed
 

	
 
	case WE_KEYPRESS:
 
		if (nd->field != NGWW_PLAYER) {
 
			if (nd->server != NULL) {
 
				if (e->we.keypress.keycode == WKC_DELETE) { // Press 'delete' to remove servers
 
					NetworkGameListRemoveItem(nd->server);
 
					NetworkRebuildHostList();
 
					nd->server = NULL;
 
				}
 
			/* The name is only allowed when it starts with a letter! */
 
			if (_edit_str_net_buf[0] != '\0' && _edit_str_net_buf[0] != ' ') {
 
				ttd_strlcpy(_network_player_name, _edit_str_net_buf, lengthof(_network_player_name));
 
			} else {
 
				ttd_strlcpy(_network_player_name, "Player", lengthof(_network_player_name));
 
			}
 

	
 
			break;
 
		}
 

	
 
		if (HandleEditBoxKey(w, &WP(w, network_ql_d).q, NGWW_PLAYER, e) == 1) break; // enter pressed
 

	
 
		/* The name is only allowed when it starts with a letter! */
 
		if (_edit_str_net_buf[0] != '\0' && _edit_str_net_buf[0] != ' ') {
 
			ttd_strlcpy(_network_player_name, _edit_str_net_buf, lengthof(_network_player_name));
 
		} else {
 
			ttd_strlcpy(_network_player_name, "Player", lengthof(_network_player_name));
 
		}
 
		case WE_ON_EDIT_TEXT:
 
			NetworkAddServer(e->we.edittext.str);
 
			NetworkRebuildHostList();
 
			break;
 

	
 
		break;
 
		case WE_RESIZE: {
 
			w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
 

	
 
	case WE_ON_EDIT_TEXT:
 
		NetworkAddServer(e->we.edittext.str);
 
		NetworkRebuildHostList();
 
		break;
 
			w->widget[NGWW_MATRIX].data = (w->vscroll.cap << 8) + 1;
 

	
 
	case WE_RESIZE: {
 
		w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
 
			SetVScrollCount(w, ld->list_length);
 

	
 
		w->widget[NGWW_MATRIX].data = (w->vscroll.cap << 8) + 1;
 

	
 
		SetVScrollCount(w, ld->list_length);
 

	
 
		int widget_width = w->widget[NGWW_FIND].right - w->widget[NGWW_FIND].left;
 
		int space = (w->width - 4 * widget_width - 25) / 3;
 
			int widget_width = w->widget[NGWW_FIND].right - w->widget[NGWW_FIND].left;
 
			int space = (w->width - 4 * widget_width - 25) / 3;
 

	
 
		int offset = 10;
 
		for (uint i = 0; i < 4; i++) {
 
			w->widget[NGWW_FIND + i].left  = offset;
 
			offset += widget_width;
 
			w->widget[NGWW_FIND + i].right = offset;
 
			offset += space;
 
		}
 
	} break;
 
			int offset = 10;
 
			for (uint i = 0; i < 4; i++) {
 
				w->widget[NGWW_FIND + i].left  = offset;
 
				offset += widget_width;
 
				w->widget[NGWW_FIND + i].right = offset;
 
				offset += space;
 
			}
 
		} break;
 

	
 
	case WE_DESTROY: // Nicely clean up the sort-list
 
		free(WP(w, network_ql_d).sort_list);
 
		break;
 
		case WE_DESTROY: // Nicely clean up the sort-list
 
			free(WP(w, network_ql_d).sort_list);
 
			break;
 
	}
 
}
 

	
 
static const Widget _network_game_window_widgets[] = {
 
/* TOP */
 
{   WWT_CLOSEBOX,   RESIZE_NONE,   BGC,     0,    10,     0,    13, STR_00C5,                       STR_018B_CLOSE_WINDOW},            // NGWW_CLOSE
 
{    WWT_CAPTION,   RESIZE_RIGHT,  BGC,    11,   449,     0,    13, STR_NETWORK_MULTIPLAYER,        STR_NULL},
 
{      WWT_PANEL,   RESIZE_RB,     BGC,     0,   449,    14,   263, 0x0,                            STR_NULL},
 
{   WWT_CLOSEBOX,   RESIZE_NONE,   BGC,     0,    10,     0,    13, STR_00C5,                         STR_018B_CLOSE_WINDOW},            // NGWW_CLOSE
 
{    WWT_CAPTION,   RESIZE_RIGHT,  BGC,    11,   449,     0,    13, STR_NETWORK_MULTIPLAYER,          STR_NULL},
 
{      WWT_PANEL,   RESIZE_RB,     BGC,     0,   449,    14,   263, 0x0,                              STR_NULL},
 

	
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,     9,    85,    23,    35, STR_NETWORK_CONNECTION,         STR_NULL},
 
{ WWT_DROPDOWNIN,   RESIZE_NONE,   BGC,    90,   181,    22,    33, STR_NETWORK_LAN_INTERNET_COMBO, STR_NETWORK_CONNECTION_TIP},       // NGWW_CONN_BTN
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,     9,    85,    23,    35, STR_NETWORK_CONNECTION,           STR_NULL},
 
{ WWT_DROPDOWNIN,   RESIZE_NONE,   BGC,    90,   181,    22,    33, STR_NETWORK_LAN_INTERNET_COMBO,   STR_NETWORK_CONNECTION_TIP},       // NGWW_CONN_BTN
 

	
 
{    WWT_EDITBOX,   RESIZE_LR,     BGC,   290,   440,    22,    33, STR_NETWORK_PLAYER_NAME_OSKTITLE, STR_NETWORK_ENTER_NAME_TIP},       // NGWW_PLAYER
 

	
 
/* LEFT SIDE */
 
{ WWT_PUSHTXTBTN,   RESIZE_RIGHT,  BTC,    10,    70,    42,    53, STR_NETWORK_GAME_NAME,          STR_NETWORK_GAME_NAME_TIP},        // NGWW_NAME
 
{ WWT_PUSHTXTBTN,   RESIZE_LR,     BTC,    71,   150,    42,    53, STR_NETWORK_CLIENTS_CAPTION,    STR_NETWORK_CLIENTS_CAPTION_TIP},  // NGWW_CLIENTS
 
{ WWT_PUSHTXTBTN,   RESIZE_LR,     BTC,   151,   190,    42,    53, STR_EMPTY,                      STR_NETWORK_INFO_ICONS_TIP},       // NGWW_INFO
 
{ WWT_PUSHTXTBTN,   RESIZE_RIGHT,  BTC,    10,    70,    42,    53, STR_NETWORK_GAME_NAME,            STR_NETWORK_GAME_NAME_TIP},        // NGWW_NAME
 
{ WWT_PUSHTXTBTN,   RESIZE_LR,     BTC,    71,   150,    42,    53, STR_NETWORK_CLIENTS_CAPTION,      STR_NETWORK_CLIENTS_CAPTION_TIP},  // NGWW_CLIENTS
 
{ WWT_PUSHTXTBTN,   RESIZE_LR,     BTC,   151,   190,    42,    53, STR_EMPTY,                        STR_NETWORK_INFO_ICONS_TIP},       // NGWW_INFO
 

	
 
{     WWT_MATRIX,   RESIZE_RB,     BGC,    10,   190,    54,   236, (13 << 8) + 1,                  STR_NETWORK_CLICK_GAME_TO_SELECT}, // NGWW_MATRIX
 
{  WWT_SCROLLBAR,   RESIZE_LRB,    BGC,   191,   202,    42,   236, 0x0,                            STR_0190_SCROLL_BAR_SCROLLS_LIST},
 
{     WWT_MATRIX,   RESIZE_RB,     BGC,    10,   190,    54,   236, (13 << 8) + 1,                    STR_NETWORK_CLICK_GAME_TO_SELECT}, // NGWW_MATRIX
 
{  WWT_SCROLLBAR,   RESIZE_LRB,    BGC,   191,   202,    42,   236, 0x0,                              STR_0190_SCROLL_BAR_SCROLLS_LIST},
 

	
 
/* RIGHT SIDE */
 
{      WWT_PANEL,   RESIZE_LRB,    BGC,   210,   440,    42,   236, 0x0,                            STR_NULL},                         // NGWW_DETAILS
 
{      WWT_PANEL,   RESIZE_LRB,    BGC,   210,   440,    42,   236, 0x0,                              STR_NULL},                         // NGWW_DETAILS
 

	
 
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,   BTC,   215,   315,   215,   226, STR_NETWORK_JOIN_GAME,          STR_NULL},                         // NGWW_JOIN
 
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,   BTC,   330,   435,   215,   226, STR_NETWORK_REFRESH,            STR_NETWORK_REFRESH_TIP},          // NGWW_REFRESH
 
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,   BTC,   215,   315,   215,   226, STR_NETWORK_JOIN_GAME,            STR_NULL},                         // NGWW_JOIN
 
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,   BTC,   330,   435,   215,   226, STR_NETWORK_REFRESH,              STR_NETWORK_REFRESH_TIP},          // NGWW_REFRESH
 

	
 
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,   BTC,   330,   435,   197,   208, STR_NEWGRF_SETTINGS_BUTTON,     STR_NULL},                         // NGWW_NEWGRF
 
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,   BTC,   330,   435,   197,   208, STR_NEWGRF_SETTINGS_BUTTON,       STR_NULL},                         // NGWW_NEWGRF
 

	
 
/* BOTTOM */
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,    10,   110,   246,   257, STR_NETWORK_FIND_SERVER,        STR_NETWORK_FIND_SERVER_TIP},      // NGWW_FIND
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,   118,   218,   246,   257, STR_NETWORK_ADD_SERVER,         STR_NETWORK_ADD_SERVER_TIP},       // NGWW_ADD
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,   226,   326,   246,   257, STR_NETWORK_START_SERVER,       STR_NETWORK_START_SERVER_TIP},     // NGWW_START
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,   334,   434,   246,   257, STR_012E_CANCEL,                STR_NULL},                         // NGWW_CANCEL
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,    10,   110,   246,   257, STR_NETWORK_FIND_SERVER,          STR_NETWORK_FIND_SERVER_TIP},      // NGWW_FIND
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,   118,   218,   246,   257, STR_NETWORK_ADD_SERVER,           STR_NETWORK_ADD_SERVER_TIP},       // NGWW_ADD
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,   226,   326,   246,   257, STR_NETWORK_START_SERVER,         STR_NETWORK_START_SERVER_TIP},     // NGWW_START
 
{ WWT_PUSHTXTBTN,   RESIZE_TB,     BTC,   334,   434,   246,   257, STR_012E_CANCEL,                  STR_NULL},                         // NGWW_CANCEL
 

	
 
{  WWT_RESIZEBOX,   RESIZE_LRTB,   BGC,   438,   449,   252,   263, 0x0,                            STR_RESIZE_BUTTON },
 
{  WWT_RESIZEBOX,   RESIZE_LRTB,   BGC,   438,   449,   252,   263, 0x0,                              STR_RESIZE_BUTTON },
 

	
 
{   WIDGETS_END},
 
};
 
@@ -617,12 +625,11 @@ static const WindowDesc _network_game_wi
 
void ShowNetworkGameWindow()
 
{
 
	static bool first = true;
 
	Window *w;
 
	DeleteWindowById(WC_NETWORK_WINDOW, 0);
 

	
 
	/* Only show once */
 
	if (first) {
 
		char* const *srv;
 
		char * const *srv;
 

	
 
		first = false;
 
		// add all servers from the config file to our list
 
@@ -634,7 +641,7 @@ void ShowNetworkGameWindow()
 
		_ng_sorting.order = 0;    // sort ascending by default
 
	}
 

	
 
	w = AllocateWindowDesc(&_network_game_window_desc);
 
	Window *w = AllocateWindowDesc(&_network_game_window_desc);
 
	if (w != NULL) {
 
		querystr_d *querystr = &WP(w, network_ql_d).q;
 

	
 
@@ -687,245 +694,254 @@ static void NetworkStartServerWindowWndP
 
	network_d *nd = &WP(w, network_ql_d).n;
 

	
 
	switch (e->event) {
 
	case WE_CREATE: // focus input box
 
		nd->field = NSSW_GAMENAME;
 
		_network_game_info.use_password = (_network_server_password[0] != '\0');
 
		break;
 

	
 
	case WE_PAINT: {
 
		int y = NSSWND_START, pos;
 
		const FiosItem *item;
 

	
 
		/* draw basic widgets */
 
		SetDParam(1, _connection_types_dropdown[_network_advertise]);
 
		SetDParam(2, _network_game_info.clients_max);
 
		SetDParam(3, _network_game_info.companies_max);
 
		SetDParam(4, _network_game_info.spectators_max);
 
		SetDParam(5, STR_NETWORK_LANG_ANY + _network_game_info.server_lang);
 
		DrawWindowWidgets(w);
 

	
 
		/* editbox to set game name */
 
		DrawEditBox(w, &WP(w, network_ql_d).q, NSSW_GAMENAME);
 

	
 
		/* if password is set, draw red '*' next to 'Set password' button */
 
		if (_network_game_info.use_password) DoDrawString("*", 408, 23, TC_RED);
 

	
 
		/* draw list of maps */
 
		GfxFillRect(11, 63, 258, 215, 0xD7);  // black background of maps list
 

	
 
		pos = w->vscroll.pos;
 
		while (pos < _fios_num + 1) {
 
			item = _fios_list + pos - 1;
 
			if (item == nd->map || (pos == 0 && nd->map == NULL))
 
				GfxFillRect(11, y - 1, 258, y + 10, 155); // show highlighted item with a different colour
 

	
 
			if (pos == 0) {
 
				DrawString(14, y, STR_4010_GENERATE_RANDOM_NEW_GAME, TC_DARK_GREEN);
 
			} else {
 
				DoDrawString(item->title, 14, y, _fios_colors[item->type] );
 
			}
 
			pos++;
 
			y += NSSWND_ROWSIZE;
 

	
 
			if (y >= w->vscroll.cap * NSSWND_ROWSIZE + NSSWND_START) break;
 
		}
 
	} break;
 

	
 
	case WE_CLICK:
 
		if (e->we.click.widget != NSSW_CONNTYPE_BTN && e->we.click.widget != NSSW_LANGUAGE_BTN) HideDropDownMenu(w);
 
		nd->field = e->we.click.widget;
 
		switch (e->we.click.widget) {
 
		case NSSW_CLOSE:  // Close 'X'
 
		case NSSW_CANCEL: // Cancel button
 
			ShowNetworkGameWindow();
 
			break;
 
		case NSSW_GAMENAME:
 
			ShowOnScreenKeyboard(w, &WP(w, network_ql_d).q,  NSSW_GAMENAME, 0, 0);
 
			break;
 
		case NSSW_SETPWD: // Set password button
 
			nd->widget_id = NSSW_SETPWD;
 
			ShowQueryString(BindCString(_network_server_password), STR_NETWORK_SET_PASSWORD, 20, 250, w, CS_ALPHANUMERAL);
 
		case WE_CREATE: // focus input box
 
			nd->field = NSSW_GAMENAME;
 
			_network_game_info.use_password = (_network_server_password[0] != '\0');
 
			break;
 

	
 
		case NSSW_SELMAP: { // Select map
 
			int y = (e->we.click.pt.y - NSSWND_START) / NSSWND_ROWSIZE;
 
		case WE_PAINT: {
 
			int y = NSSWND_START, pos;
 
			const FiosItem *item;
 

	
 
			/* draw basic widgets */
 
			SetDParam(1, _connection_types_dropdown[_network_advertise]);
 
			SetDParam(2, _network_game_info.clients_max);
 
			SetDParam(3, _network_game_info.companies_max);
 
			SetDParam(4, _network_game_info.spectators_max);
 
			SetDParam(5, STR_NETWORK_LANG_ANY + _network_game_info.server_lang);
 
			DrawWindowWidgets(w);
 

	
 
			/* editbox to set game name */
 
			DrawEditBox(w, &WP(w, network_ql_d).q, NSSW_GAMENAME);
 

	
 
			/* if password is set, draw red '*' next to 'Set password' button */
 
			if (_network_game_info.use_password) DoDrawString("*", 408, 23, TC_RED);
 

	
 
			/* draw list of maps */
 
			GfxFillRect(11, 63, 258, 215, 0xD7);  // black background of maps list
 

	
 
			pos = w->vscroll.pos;
 
			while (pos < _fios_num + 1) {
 
				item = _fios_list + pos - 1;
 
				if (item == nd->map || (pos == 0 && nd->map == NULL))
 
					GfxFillRect(11, y - 1, 258, y + 10, 155); // show highlighted item with a different colour
 

	
 
				if (pos == 0) {
 
					DrawString(14, y, STR_4010_GENERATE_RANDOM_NEW_GAME, TC_DARK_GREEN);
 
				} else {
 
					DoDrawString(item->title, 14, y, _fios_colors[item->type] );
 
				}
 
				pos++;
 
				y += NSSWND_ROWSIZE;
 

	
 
			y += w->vscroll.pos;
 
			if (y >= w->vscroll.count) return;
 
				if (y >= w->vscroll.cap * NSSWND_ROWSIZE + NSSWND_START) break;
 
			}
 
		} break;
 

	
 
		case WE_CLICK:
 
			if (e->we.click.widget != NSSW_CONNTYPE_BTN && e->we.click.widget != NSSW_LANGUAGE_BTN) HideDropDownMenu(w);
 
			nd->field = e->we.click.widget;
 
			switch (e->we.click.widget) {
 
				case NSSW_CLOSE:  // Close 'X'
 
				case NSSW_CANCEL: // Cancel button
 
					ShowNetworkGameWindow();
 
					break;
 

	
 
				case NSSW_GAMENAME:
 
					ShowOnScreenKeyboard(w, &WP(w, network_ql_d).q,  NSSW_GAMENAME, 0, 0);
 
					break;
 

	
 
				case NSSW_SETPWD: // Set password button
 
					nd->widget_id = NSSW_SETPWD;
 
					ShowQueryString(BindCString(_network_server_password), STR_NETWORK_SET_PASSWORD, 20, 250, w, CS_ALPHANUMERAL);
 
					break;
 

	
 
				case NSSW_SELMAP: { // Select map
 
					int y = (e->we.click.pt.y - NSSWND_START) / NSSWND_ROWSIZE;
 

	
 
					y += w->vscroll.pos;
 
					if (y >= w->vscroll.count) return;
 

	
 
					nd->map = (y == 0) ? NULL : _fios_list + y - 1;
 
					SetWindowDirty(w);
 
				} break;
 

	
 
				case NSSW_CONNTYPE_BTN: // Connection type
 
					ShowDropDownMenu(w, _connection_types_dropdown, _network_advertise, NSSW_CONNTYPE_BTN, 0, 0); // do it for widget NSSW_CONNTYPE_BTN
 
					break;
 

	
 
			nd->map = (y == 0) ? NULL : _fios_list + y - 1;
 
			SetWindowDirty(w);
 
			} break;
 
		case NSSW_CONNTYPE_BTN: // Connection type
 
			ShowDropDownMenu(w, _connection_types_dropdown, _network_advertise, NSSW_CONNTYPE_BTN, 0, 0); // do it for widget NSSW_CONNTYPE_BTN
 
				case NSSW_CLIENTS_BTND:    case NSSW_CLIENTS_BTNU:    // Click on up/down button for number of clients
 
				case NSSW_COMPANIES_BTND:  case NSSW_COMPANIES_BTNU:  // Click on up/down button for number of companies
 
				case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU: // Click on up/down button for number of spectators
 
					/* Don't allow too fast scrolling */
 
					if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
						w->HandleButtonClick(e->we.click.widget);
 
						SetWindowDirty(w);
 
						switch (e->we.click.widget) {
 
							default: NOT_REACHED();
 
							case NSSW_CLIENTS_BTND: case NSSW_CLIENTS_BTNU:
 
								_network_game_info.clients_max    = Clamp(_network_game_info.clients_max    + e->we.click.widget - NSSW_CLIENTS_TXT,    2, MAX_CLIENTS);
 
								break;
 
							case NSSW_COMPANIES_BTND: case NSSW_COMPANIES_BTNU:
 
								_network_game_info.companies_max  = Clamp(_network_game_info.companies_max  + e->we.click.widget - NSSW_COMPANIES_TXT,  1, MAX_PLAYERS);
 
								break;
 
							case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU:
 
								_network_game_info.spectators_max = Clamp(_network_game_info.spectators_max + e->we.click.widget - NSSW_SPECTATORS_TXT, 0, MAX_CLIENTS);
 
								break;
 
						}
 
					}
 
					_left_button_clicked = false;
 
					break;
 

	
 
				case NSSW_CLIENTS_TXT:    // Click on number of players
 
					nd->widget_id = NSSW_CLIENTS_TXT;
 
					SetDParam(0, _network_game_info.clients_max);
 
					ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_NETWORK_NUMBER_OF_CLIENTS,    3, 50, w, CS_NUMERAL);
 
					break;
 

	
 
				case NSSW_COMPANIES_TXT:  // Click on number of companies
 
					nd->widget_id = NSSW_COMPANIES_TXT;
 
					SetDParam(0, _network_game_info.companies_max);
 
					ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_NETWORK_NUMBER_OF_COMPANIES,  3, 50, w, CS_NUMERAL);
 
					break;
 

	
 
				case NSSW_SPECTATORS_TXT: // Click on number of spectators
 
					nd->widget_id = NSSW_SPECTATORS_TXT;
 
					SetDParam(0, _network_game_info.spectators_max);
 
					ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_NETWORK_NUMBER_OF_SPECTATORS, 3, 50, w, CS_NUMERAL);
 
					break;
 

	
 
				case NSSW_LANGUAGE_BTN: { // Language
 
					uint sel = 0;
 
					for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
 
						if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _network_game_info.server_lang) {
 
							sel = i;
 
							break;
 
						}
 
					}
 
					ShowDropDownMenu(w, _language_dropdown, sel, NSSW_LANGUAGE_BTN, 0, 0);
 
				} break;
 

	
 
				case NSSW_START: // Start game
 
					_is_network_server = true;
 

	
 
					if (nd->map == NULL) { // start random new game
 
						ShowGenerateLandscape();
 
					} else { // load a scenario
 
						char *name = FiosBrowseTo(nd->map);
 
						if (name != NULL) {
 
							SetFiosType(nd->map->type);
 
							_file_to_saveload.filetype = FT_SCENARIO;
 
							ttd_strlcpy(_file_to_saveload.name, name, sizeof(_file_to_saveload.name));
 
							ttd_strlcpy(_file_to_saveload.title, nd->map->title, sizeof(_file_to_saveload.title));
 

	
 
							DeleteWindow(w);
 
							SwitchMode(SM_START_SCENARIO);
 
						}
 
					}
 
					break;
 

	
 
				case NSSW_LOAD: // Load game
 
					_is_network_server = true;
 
					/* XXX - WC_NETWORK_WINDOW (this window) should stay, but if it stays, it gets
 
					* copied all the elements of 'load game' and upon closing that, it segfaults */
 
					DeleteWindow(w);
 
					ShowSaveLoadDialog(SLD_LOAD_GAME);
 
					break;
 
			}
 
			break;
 
		case NSSW_CLIENTS_BTND:    case NSSW_CLIENTS_BTNU:    // Click on up/down button for number of clients
 
		case NSSW_COMPANIES_BTND:  case NSSW_COMPANIES_BTNU:  // Click on up/down button for number of companies
 
		case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU: // Click on up/down button for number of spectators
 
			/* Don't allow too fast scrolling */
 
			if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
 
				w->HandleButtonClick(e->we.click.widget);
 
				SetWindowDirty(w);
 
				switch (e->we.click.widget) {
 

	
 
		case WE_DROPDOWN_SELECT: // we have selected a dropdown item in the list
 
			switch (e->we.dropdown.button) {
 
				case NSSW_CONNTYPE_BTN:
 
					_network_advertise = (e->we.dropdown.index != 0);
 
					break;
 
				case NSSW_LANGUAGE_BTN:
 
					_network_game_info.server_lang = _language_dropdown[e->we.dropdown.index] - STR_NETWORK_LANG_ANY;
 
					break;
 
				default:
 
					NOT_REACHED();
 
			}
 

	
 
			SetWindowDirty(w);
 
			break;
 

	
 
		case WE_MOUSELOOP:
 
			if (nd->field == NSSW_GAMENAME) HandleEditBox(w, &WP(w, network_ql_d).q, NSSW_GAMENAME);
 
			break;
 

	
 
		case WE_KEYPRESS:
 
			if (nd->field == NSSW_GAMENAME) {
 
				if (HandleEditBoxKey(w, &WP(w, network_ql_d).q, NSSW_GAMENAME, e) == 1) break; // enter pressed
 

	
 
				ttd_strlcpy(_network_server_name, WP(w, network_ql_d).q.text.buf, sizeof(_network_server_name));
 
			}
 
			break;
 

	
 
		case WE_ON_EDIT_TEXT:
 
			if (e->we.edittext.str == NULL) break;
 

	
 
			if (nd->widget_id == NSSW_SETPWD) {
 
				ttd_strlcpy(_network_server_password, e->we.edittext.str, lengthof(_network_server_password));
 
				_network_game_info.use_password = (_network_server_password[0] != '\0');
 
			} else {
 
				int32 value = atoi(e->we.edittext.str);
 
				w->InvalidateWidget(nd->widget_id);
 
				switch (nd->widget_id) {
 
					default: NOT_REACHED();
 
					case NSSW_CLIENTS_BTND: case NSSW_CLIENTS_BTNU:
 
						_network_game_info.clients_max    = Clamp(_network_game_info.clients_max    + e->we.click.widget - NSSW_CLIENTS_TXT,    2, MAX_CLIENTS);
 
						break;
 
					case NSSW_COMPANIES_BTND: case NSSW_COMPANIES_BTNU:
 
						_network_game_info.companies_max  = Clamp(_network_game_info.companies_max  + e->we.click.widget - NSSW_COMPANIES_TXT,  1, MAX_PLAYERS);
 
						break;
 
					case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU:
 
						_network_game_info.spectators_max = Clamp(_network_game_info.spectators_max + e->we.click.widget - NSSW_SPECTATORS_TXT, 0, MAX_CLIENTS);
 
						break;
 
					case NSSW_CLIENTS_TXT:    _network_game_info.clients_max    = Clamp(value, 2, MAX_CLIENTS); break;
 
					case NSSW_COMPANIES_TXT:  _network_game_info.companies_max  = Clamp(value, 1, MAX_PLAYERS); break;
 
					case NSSW_SPECTATORS_TXT: _network_game_info.spectators_max = Clamp(value, 0, MAX_CLIENTS); break;
 
				}
 
			}
 
			_left_button_clicked = false;
 
			break;
 
		case NSSW_CLIENTS_TXT:    // Click on number of players
 
			nd->widget_id = NSSW_CLIENTS_TXT;
 
			SetDParam(0, _network_game_info.clients_max);
 
			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_NETWORK_NUMBER_OF_CLIENTS,    3, 50, w, CS_NUMERAL);
 
			break;
 
		case NSSW_COMPANIES_TXT:  // Click on number of companies
 
			nd->widget_id = NSSW_COMPANIES_TXT;
 
			SetDParam(0, _network_game_info.companies_max);
 
			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_NETWORK_NUMBER_OF_COMPANIES,  3, 50, w, CS_NUMERAL);
 
			break;
 
		case NSSW_SPECTATORS_TXT: // Click on number of spectators
 
			nd->widget_id = NSSW_SPECTATORS_TXT;
 
			SetDParam(0, _network_game_info.spectators_max);
 
			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_NETWORK_NUMBER_OF_SPECTATORS, 3, 50, w, CS_NUMERAL);
 
			break;
 
		case NSSW_LANGUAGE_BTN: { // Language
 
			uint sel = 0;
 
			for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
 
				if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _network_game_info.server_lang) {
 
					sel = i;
 
					break;
 
				}
 
			}
 
			ShowDropDownMenu(w, _language_dropdown, sel, NSSW_LANGUAGE_BTN, 0, 0);
 
			break;
 
		}
 
		case NSSW_START: // Start game
 
			_is_network_server = true;
 

	
 
			if (nd->map == NULL) { // start random new game
 
				ShowGenerateLandscape();
 
			} else { // load a scenario
 
				char *name = FiosBrowseTo(nd->map);
 
				if (name != NULL) {
 
					SetFiosType(nd->map->type);
 
					_file_to_saveload.filetype = FT_SCENARIO;
 
					ttd_strlcpy(_file_to_saveload.name, name, sizeof(_file_to_saveload.name));
 
					ttd_strlcpy(_file_to_saveload.title, nd->map->title, sizeof(_file_to_saveload.title));
 

	
 
					DeleteWindow(w);
 
					SwitchMode(SM_START_SCENARIO);
 
				}
 
			}
 
			SetWindowDirty(w);
 
			break;
 
		case NSSW_LOAD: // Load game
 
			_is_network_server = true;
 
			/* XXX - WC_NETWORK_WINDOW (this window) should stay, but if it stays, it gets
 
			 * copied all the elements of 'load game' and upon closing that, it segfaults */
 
			DeleteWindow(w);
 
			ShowSaveLoadDialog(SLD_LOAD_GAME);
 
			break;
 
		}
 
		break;
 

	
 
	case WE_DROPDOWN_SELECT: // we have selected a dropdown item in the list
 
		switch (e->we.dropdown.button) {
 
			case NSSW_CONNTYPE_BTN:
 
				_network_advertise = (e->we.dropdown.index != 0);
 
				break;
 
			case NSSW_LANGUAGE_BTN:
 
				_network_game_info.server_lang = _language_dropdown[e->we.dropdown.index] - STR_NETWORK_LANG_ANY;
 
				break;
 
			default:
 
				NOT_REACHED();
 
		}
 

	
 
		SetWindowDirty(w);
 
		break;
 

	
 
	case WE_MOUSELOOP:
 
		if (nd->field == NSSW_GAMENAME) HandleEditBox(w, &WP(w, network_ql_d).q, NSSW_GAMENAME);
 
		break;
 

	
 
	case WE_KEYPRESS:
 
		if (nd->field == NSSW_GAMENAME) {
 
			if (HandleEditBoxKey(w, &WP(w, network_ql_d).q, NSSW_GAMENAME, e) == 1) break; // enter pressed
 

	
 
			ttd_strlcpy(_network_server_name, WP(w, network_ql_d).q.text.buf, sizeof(_network_server_name));
 
		}
 
		break;
 

	
 
	case WE_ON_EDIT_TEXT:
 
		if (e->we.edittext.str == NULL) break;
 

	
 
		if (nd->widget_id == NSSW_SETPWD) {
 
			ttd_strlcpy(_network_server_password, e->we.edittext.str, lengthof(_network_server_password));
 
			_network_game_info.use_password = (_network_server_password[0] != '\0');
 
		} else {
 
			int32 value = atoi(e->we.edittext.str);
 
			w->InvalidateWidget(nd->widget_id);
 
			switch (nd->widget_id) {
 
				default: NOT_REACHED();
 
				case NSSW_CLIENTS_TXT:    _network_game_info.clients_max    = Clamp(value, 2, MAX_CLIENTS); break;
 
				case NSSW_COMPANIES_TXT:  _network_game_info.companies_max  = Clamp(value, 1, MAX_PLAYERS); break;
 
				case NSSW_SPECTATORS_TXT: _network_game_info.spectators_max = Clamp(value, 0, MAX_CLIENTS); break;
 
			}
 
		}
 

	
 
		SetWindowDirty(w);
 
		break;
 
	}
 
}
 

	
 
static const Widget _network_start_server_window_widgets[] = {
 
/* Window decoration and background panel */
 
{   WWT_CLOSEBOX,   RESIZE_NONE,   BGC,     0,    10,     0,    13, STR_00C5,                         STR_018B_CLOSE_WINDOW },               // NSSW_CLOSE
 
{    WWT_CAPTION,   RESIZE_NONE,   BGC,    11,   419,     0,    13, STR_NETWORK_START_GAME_WINDOW,    STR_NULL},
 
{      WWT_PANEL,   RESIZE_NONE,   BGC,     0,   419,    14,   243, 0x0,                              STR_NULL},
 
{   WWT_CLOSEBOX,   RESIZE_NONE,   BGC,     0,    10,     0,    13, STR_00C5,                           STR_018B_CLOSE_WINDOW },               // NSSW_CLOSE
 
{    WWT_CAPTION,   RESIZE_NONE,   BGC,    11,   419,     0,    13, STR_NETWORK_START_GAME_WINDOW,      STR_NULL},
 
{      WWT_PANEL,   RESIZE_NONE,   BGC,     0,   419,    14,   243, 0x0,                                STR_NULL},
 

	
 
/* Set game name and password widgets */
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,    10,    90,    22,    34, STR_NETWORK_NEW_GAME_NAME,        STR_NULL},
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,    10,    90,    22,    34, STR_NETWORK_NEW_GAME_NAME,          STR_NULL},
 
{    WWT_EDITBOX,   RESIZE_NONE,   BGC,   100,   272,    22,    33, STR_NETWORK_NEW_GAME_NAME_OSKTITLE, STR_NETWORK_NEW_GAME_NAME_TIP},        // NSSW_GAMENAME
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,   285,   405,    22,    33, STR_NETWORK_SET_PASSWORD,         STR_NETWORK_PASSWORD_TIP},             // NSSW_SETPWD
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,   285,   405,    22,    33, STR_NETWORK_SET_PASSWORD,           STR_NETWORK_PASSWORD_TIP},             // NSSW_SETPWD
 

	
 
/* List of playable scenarios */
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,    10,   110,    43,    55, STR_NETWORK_SELECT_MAP,           STR_NULL},
 
{      WWT_INSET,   RESIZE_NONE,   BGC,    10,   271,    62,   216, STR_NULL,                         STR_NETWORK_SELECT_MAP_TIP},           // NSSW_SELMAP
 
{  WWT_SCROLLBAR,   RESIZE_NONE,   BGC,   259,   270,    63,   215, 0x0,                              STR_0190_SCROLL_BAR_SCROLLS_LIST},
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,    10,   110,    43,    55, STR_NETWORK_SELECT_MAP,             STR_NULL},
 
{      WWT_INSET,   RESIZE_NONE,   BGC,    10,   271,    62,   216, STR_NULL,                           STR_NETWORK_SELECT_MAP_TIP},           // NSSW_SELMAP
 
{  WWT_SCROLLBAR,   RESIZE_NONE,   BGC,   259,   270,    63,   215, 0x0,                                STR_0190_SCROLL_BAR_SCROLLS_LIST},
 

	
 
/* Combo/selection boxes to control Connection Type / Max Clients / Max Companies / Max Observers / Language */
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,    63,    75, STR_NETWORK_CONNECTION,           STR_NULL},
 
{ WWT_DROPDOWNIN,   RESIZE_NONE,   BGC,   280,   410,    77,    88, STR_NETWORK_LAN_INTERNET_COMBO,   STR_NETWORK_CONNECTION_TIP},           // NSSW_CONNTYPE_BTN
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,    63,    75, STR_NETWORK_CONNECTION,             STR_NULL},
 
{ WWT_DROPDOWNIN,   RESIZE_NONE,   BGC,   280,   410,    77,    88, STR_NETWORK_LAN_INTERNET_COMBO,     STR_NETWORK_CONNECTION_TIP},           // NSSW_CONNTYPE_BTN
 

	
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,    95,   107, STR_NETWORK_NUMBER_OF_CLIENTS,    STR_NULL},
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   280,   291,   109,   120, SPR_ARROW_DOWN,                   STR_NETWORK_NUMBER_OF_CLIENTS_TIP},    // NSSW_CLIENTS_BTND
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BGC,   292,   397,   109,   120, STR_NETWORK_CLIENTS_SELECT,       STR_NETWORK_NUMBER_OF_CLIENTS_TIP},    // NSSW_CLIENTS_TXT
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   398,   410,   109,   120, SPR_ARROW_UP,                     STR_NETWORK_NUMBER_OF_CLIENTS_TIP},    // NSSW_CLIENTS_BTNU
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,    95,   107, STR_NETWORK_NUMBER_OF_CLIENTS,      STR_NULL},
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   280,   291,   109,   120, SPR_ARROW_DOWN,                     STR_NETWORK_NUMBER_OF_CLIENTS_TIP},    // NSSW_CLIENTS_BTND
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BGC,   292,   397,   109,   120, STR_NETWORK_CLIENTS_SELECT,         STR_NETWORK_NUMBER_OF_CLIENTS_TIP},    // NSSW_CLIENTS_TXT
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   398,   410,   109,   120, SPR_ARROW_UP,                       STR_NETWORK_NUMBER_OF_CLIENTS_TIP},    // NSSW_CLIENTS_BTNU
 

	
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,   127,   139, STR_NETWORK_NUMBER_OF_COMPANIES,  STR_NULL},
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   280,   291,   141,   152, SPR_ARROW_DOWN,                   STR_NETWORK_NUMBER_OF_COMPANIES_TIP},  // NSSW_COMPANIES_BTND
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BGC,   292,   397,   141,   152, STR_NETWORK_COMPANIES_SELECT,     STR_NETWORK_NUMBER_OF_COMPANIES_TIP},  // NSSW_COMPANIES_TXT
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   398,   410,   141,   152, SPR_ARROW_UP,                     STR_NETWORK_NUMBER_OF_COMPANIES_TIP},  // NSSW_COMPANIES_BTNU
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,   127,   139, STR_NETWORK_NUMBER_OF_COMPANIES,    STR_NULL},
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   280,   291,   141,   152, SPR_ARROW_DOWN,                     STR_NETWORK_NUMBER_OF_COMPANIES_TIP},  // NSSW_COMPANIES_BTND
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BGC,   292,   397,   141,   152, STR_NETWORK_COMPANIES_SELECT,       STR_NETWORK_NUMBER_OF_COMPANIES_TIP},  // NSSW_COMPANIES_TXT
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   398,   410,   141,   152, SPR_ARROW_UP,                       STR_NETWORK_NUMBER_OF_COMPANIES_TIP},  // NSSW_COMPANIES_BTNU
 

	
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,   159,   171, STR_NETWORK_NUMBER_OF_SPECTATORS, STR_NULL},
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   280,   291,   173,   184, SPR_ARROW_DOWN,                   STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, // NSSW_SPECTATORS_BTND
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BGC,   292,   397,   173,   184, STR_NETWORK_SPECTATORS_SELECT,    STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, // NSSW_SPECTATORS_TXT
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   398,   410,   173,   184, SPR_ARROW_UP,                     STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, // NSSW_SPECTATORS_BTNU
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,   159,   171, STR_NETWORK_NUMBER_OF_SPECTATORS,   STR_NULL},
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   280,   291,   173,   184, SPR_ARROW_DOWN,                     STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, // NSSW_SPECTATORS_BTND
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BGC,   292,   397,   173,   184, STR_NETWORK_SPECTATORS_SELECT,      STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, // NSSW_SPECTATORS_TXT
 
{     WWT_IMGBTN,   RESIZE_NONE,   BGC,   398,   410,   173,   184, SPR_ARROW_UP,                       STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, // NSSW_SPECTATORS_BTNU
 

	
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,   191,   203, STR_NETWORK_LANGUAGE_SPOKEN,      STR_NULL},
 
{ WWT_DROPDOWNIN,   RESIZE_NONE,   BGC,   280,   410,   205,   216, STR_NETWORK_LANGUAGE_COMBO,       STR_NETWORK_LANGUAGE_TIP},             // NSSW_LANGUAGE_BTN
 
{       WWT_TEXT,   RESIZE_NONE,   BGC,   280,   419,   191,   203, STR_NETWORK_LANGUAGE_SPOKEN,        STR_NULL},
 
{ WWT_DROPDOWNIN,   RESIZE_NONE,   BGC,   280,   410,   205,   216, STR_NETWORK_LANGUAGE_COMBO,         STR_NETWORK_LANGUAGE_TIP},             // NSSW_LANGUAGE_BTN
 

	
 
/* Buttons Start / Load / Cancel */
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,    40,   140,   224,   235, STR_NETWORK_START_GAME,           STR_NETWORK_START_GAME_TIP},           // NSSW_START
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,   150,   250,   224,   235, STR_NETWORK_LOAD_GAME,            STR_NETWORK_LOAD_GAME_TIP},            // NSSW_LOAD
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,   260,   360,   224,   235, STR_012E_CANCEL,                  STR_NULL},                             // NSSW_CANCEL
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,    40,   140,   224,   235, STR_NETWORK_START_GAME,             STR_NETWORK_START_GAME_TIP},           // NSSW_START
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,   150,   250,   224,   235, STR_NETWORK_LOAD_GAME,              STR_NETWORK_LOAD_GAME_TIP},            // NSSW_LOAD
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,   BTC,   260,   360,   224,   235, STR_012E_CANCEL,                    STR_NULL},                             // NSSW_CANCEL
 

	
 
{   WIDGETS_END},
 
};
 
@@ -940,10 +956,9 @@ static const WindowDesc _network_start_s
 

	
 
static void ShowNetworkStartServerWindow()
 
{
 
	Window *w;
 
	DeleteWindowById(WC_NETWORK_WINDOW, 0);
 

	
 
	w = AllocateWindowDesc(&_network_start_server_window_desc);
 
	Window *w = AllocateWindowDesc(&_network_start_server_window_desc);
 
	ttd_strlcpy(_edit_str_net_buf, _network_server_name, lengthof(_edit_str_net_buf));
 

	
 
	_saveload_mode = SLD_NEW_GAME;
 
@@ -957,11 +972,8 @@ static void ShowNetworkStartServerWindow
 

	
 
static PlayerID NetworkLobbyFindCompanyIndex(byte pos)
 
{
 
	PlayerID i;
 

	
 
	/* Scroll through all _network_player_info and get the 'pos' item
 
	    that is not empty */
 
	for (i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
 
	/* Scroll through all _network_player_info and get the 'pos' item that is not empty */
 
	for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
 
		if (_network_player_info[i].company_name[0] != '\0') {
 
			if (pos-- == 0) return i;
 
		}
 
@@ -996,142 +1008,148 @@ static void NetworkLobbyWindowWndProc(Wi
 
	network_d *nd = &WP(w, network_d);
 

	
 
	switch (e->event) {
 
	case WE_CREATE:
 
		nd->company = INVALID_PLAYER;
 
		break;
 
		case WE_CREATE:
 
			nd->company = INVALID_PLAYER;
 
			break;
 

	
 
	case WE_PAINT: {
 
		const NetworkGameInfo *gi = &nd->server->info;
 
		int y = NET_PRC__OFFSET_TOP_WIDGET_COMPANY, pos;
 

	
 
		/* Join button is disabled when no company is selected */
 
		w->SetWidgetDisabledState(NLWW_JOIN, nd->company == INVALID_PLAYER);
 
		/* Cannot start new company if there are too many */
 
		w->SetWidgetDisabledState(NLWW_NEW, gi->companies_on >= gi->companies_max);
 
		/* Cannot spectate if there are too many spectators */
 
		w->SetWidgetDisabledState(NLWW_SPECTATE, gi->spectators_on >= gi->spectators_max);
 
		case WE_PAINT: {
 
			const NetworkGameInfo *gi = &nd->server->info;
 
			int y = NET_PRC__OFFSET_TOP_WIDGET_COMPANY, pos;
 

	
 
		/* Draw window widgets */
 
		SetDParamStr(0, gi->server_name);
 
		DrawWindowWidgets(w);
 
			/* Join button is disabled when no company is selected */
 
			w->SetWidgetDisabledState(NLWW_JOIN, nd->company == INVALID_PLAYER);
 
			/* Cannot start new company if there are too many */
 
			w->SetWidgetDisabledState(NLWW_NEW, gi->companies_on >= gi->companies_max);
 
			/* Cannot spectate if there are too many spectators */
 
			w->SetWidgetDisabledState(NLWW_SPECTATE, gi->spectators_on >= gi->spectators_max);
 

	
 
		/* Draw company list */
 
		pos = w->vscroll.pos;
 
		while (pos < gi->companies_on) {
 
			byte company = NetworkLobbyFindCompanyIndex(pos);
 
			bool income = false;
 
			if (nd->company == company)
 
				GfxFillRect(11, y - 1, 154, y + 10, 10); // show highlighted item with a different colour
 

	
 
			DoDrawStringTruncated(_network_player_info[company].company_name, 13, y, TC_BLACK, 135 - 13);
 
			if (_network_player_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, 135, y);
 
			/* Draw window widgets */
 
			SetDParamStr(0, gi->server_name);
 
			DrawWindowWidgets(w);
 

	
 
			/* If the company's income was positive puts a green dot else a red dot */
 
			if (_network_player_info[company].income >= 0) income = true;
 
			DrawSprite(SPR_BLOT, income ? PALETTE_TO_GREEN : PALETTE_TO_RED, 145, y);
 
			/* Draw company list */
 
			pos = w->vscroll.pos;
 
			while (pos < gi->companies_on) {
 
				byte company = NetworkLobbyFindCompanyIndex(pos);
 
				bool income = false;
 
				if (nd->company == company)
 
					GfxFillRect(11, y - 1, 154, y + 10, 10); // show highlighted item with a different colour
 

	
 
			pos++;
 
			y += NET_PRC__SIZE_OF_ROW;
 
			if (pos >= w->vscroll.cap) break;
 
		}
 

	
 
		/* Draw info about selected company when it is selected in the left window */
 
		GfxFillRect(174, 39, 403, 75, 157);
 
		DrawStringCentered(290, 50, STR_NETWORK_COMPANY_INFO, TC_FROMSTRING);
 
		if (nd->company != INVALID_PLAYER) {
 
			const uint x = 183;
 
			const uint trunc_width = w->widget[NLWW_DETAILS].right - x;
 
			y = 80;
 
				DoDrawStringTruncated(_network_player_info[company].company_name, 13, y, TC_BLACK, 135 - 13);
 
				if (_network_player_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, 135, y);
 

	
 
			SetDParam(0, nd->server->info.clients_on);
 
			SetDParam(1, nd->server->info.clients_max);
 
			SetDParam(2, nd->server->info.companies_on);
 
			SetDParam(3, nd->server->info.companies_max);
 
			DrawString(x, y, STR_NETWORK_CLIENTS, TC_GOLD);
 
			y += 10;
 
				/* If the company's income was positive puts a green dot else a red dot */
 
				if (_network_player_info[company].income >= 0) income = true;
 
				DrawSprite(SPR_BLOT, income ? PALETTE_TO_GREEN : PALETTE_TO_RED, 145, y);
 

	
 
			SetDParamStr(0, _network_player_info[nd->company].company_name);
 
			DrawStringTruncated(x, y, STR_NETWORK_COMPANY_NAME, TC_GOLD, trunc_width);
 
			y += 10;
 
				pos++;
 
				y += NET_PRC__SIZE_OF_ROW;
 
				if (pos >= w->vscroll.cap) break;
 
			}
 

	
 
			SetDParam(0, _network_player_info[nd->company].inaugurated_year);
 
			DrawString(x, y, STR_NETWORK_INAUGURATION_YEAR, TC_GOLD); // inauguration year
 
			y += 10;
 

	
 
			SetDParam(0, _network_player_info[nd->company].company_value);
 
			DrawString(x, y, STR_NETWORK_VALUE, TC_GOLD); // company value
 
			y += 10;
 
			/* Draw info about selected company when it is selected in the left window */
 
			GfxFillRect(174, 39, 403, 75, 157);
 
			DrawStringCentered(290, 50, STR_NETWORK_COMPANY_INFO, TC_FROMSTRING);
 
			if (nd->company != INVALID_PLAYER) {
 
				const uint x = 183;
 
				const uint trunc_width = w->widget[NLWW_DETAILS].right - x;
 
				y = 80;
 

	
 
			SetDParam(0, _network_player_info[nd->company].money);
 
			DrawString(x, y, STR_NETWORK_CURRENT_BALANCE, TC_GOLD); // current balance
 
			y += 10;
 
				SetDParam(0, nd->server->info.clients_on);
 
				SetDParam(1, nd->server->info.clients_max);
 
				SetDParam(2, nd->server->info.companies_on);
 
				SetDParam(3, nd->server->info.companies_max);
 
				DrawString(x, y, STR_NETWORK_CLIENTS, TC_GOLD);
 
				y += 10;
 

	
 
			SetDParam(0, _network_player_info[nd->company].income);
 
			DrawString(x, y, STR_NETWORK_LAST_YEARS_INCOME, TC_GOLD); // last year's income
 
			y += 10;
 

	
 
			SetDParam(0, _network_player_info[nd->company].performance);
 
			DrawString(x, y, STR_NETWORK_PERFORMANCE, TC_GOLD); // performance
 
			y += 10;
 
				SetDParamStr(0, _network_player_info[nd->company].company_name);
 
				DrawStringTruncated(x, y, STR_NETWORK_COMPANY_NAME, TC_GOLD, trunc_width);
 
				y += 10;
 

	
 
			SetDParam(0, _network_player_info[nd->company].num_vehicle[0]);
 
			SetDParam(1, _network_player_info[nd->company].num_vehicle[1]);
 
			SetDParam(2, _network_player_info[nd->company].num_vehicle[2]);
 
			SetDParam(3, _network_player_info[nd->company].num_vehicle[3]);
 
			SetDParam(4, _network_player_info[nd->company].num_vehicle[4]);
 
			DrawString(x, y, STR_NETWORK_VEHICLES, TC_GOLD); // vehicles
 
			y += 10;
 
				SetDParam(0, _network_player_info[nd->company].inaugurated_year);
 
				DrawString(x, y, STR_NETWORK_INAUGURATION_YEAR, TC_GOLD); // inauguration year
 
				y += 10;
 

	
 
			SetDParam(0, _network_player_info[nd->company].num_station[0]);
 
			SetDParam(1, _network_player_info[nd->company].num_station[1]);
 
			SetDParam(2, _network_player_info[nd->company].num_station[2]);
 
			SetDParam(3, _network_player_info[nd->company].num_station[3]);
 
			SetDParam(4, _network_player_info[nd->company].num_station[4]);
 
			DrawString(x, y, STR_NETWORK_STATIONS, TC_GOLD); // stations
 
			y += 10;
 
				SetDParam(0, _network_player_info[nd->company].company_value);
 
				DrawString(x, y, STR_NETWORK_VALUE, TC_GOLD); // company value
 
				y += 10;
 

	
 
			SetDParamStr(0, _network_player_info[nd->company].players);
 
			DrawStringTruncated(x, y, STR_NETWORK_PLAYERS, TC_GOLD, trunc_width); // players
 
		}
 
	} break;
 
				SetDParam(0, _network_player_info[nd->company].money);
 
				DrawString(x, y, STR_NETWORK_CURRENT_BALANCE, TC_GOLD); // current balance
 
				y += 10;
 

	
 
	case WE_CLICK:
 
		switch (e->we.click.widget) {
 
		case NLWW_CLOSE:    // Close 'X'
 
		case NLWW_CANCEL:   // Cancel button
 
			ShowNetworkGameWindow();
 
			break;
 
		case NLWW_MATRIX: { // Company list
 
			uint32 id_v = (e->we.click.pt.y - NET_PRC__OFFSET_TOP_WIDGET_COMPANY) / NET_PRC__SIZE_OF_ROW;
 
				SetDParam(0, _network_player_info[nd->company].income);
 
				DrawString(x, y, STR_NETWORK_LAST_YEARS_INCOME, TC_GOLD); // last year's income
 
				y += 10;
 

	
 
			if (id_v >= w->vscroll.cap) break;
 
				SetDParam(0, _network_player_info[nd->company].performance);
 
				DrawString(x, y, STR_NETWORK_PERFORMANCE, TC_GOLD); // performance
 
				y += 10;
 

	
 
			id_v += w->vscroll.pos;
 
			nd->company = (id_v >= nd->server->info.companies_on) ? INVALID_PLAYER : NetworkLobbyFindCompanyIndex(id_v);
 
			SetWindowDirty(w);
 
		} break;
 
		case NLWW_JOIN:     // Join company
 
			/* Button can be clicked only when it is enabled */
 
			_network_playas = nd->company;
 
			NetworkClientConnectGame(_network_last_host, _network_last_port);
 
			break;
 
		case NLWW_NEW:      // New company
 
			_network_playas = PLAYER_NEW_COMPANY;
 
			NetworkClientConnectGame(_network_last_host, _network_last_port);
 
			break;
 
		case NLWW_SPECTATE: // Spectate game
 
			_network_playas = PLAYER_SPECTATOR;
 
			NetworkClientConnectGame(_network_last_host, _network_last_port);
 
			break;
 
		case NLWW_REFRESH:  // Refresh
 
			NetworkTCPQueryServer(_network_last_host, _network_last_port); // company info
 
			NetworkUDPQueryServer(_network_last_host, _network_last_port); // general data
 
			break;
 
				SetDParam(0, _network_player_info[nd->company].num_vehicle[0]);
 
				SetDParam(1, _network_player_info[nd->company].num_vehicle[1]);
 
				SetDParam(2, _network_player_info[nd->company].num_vehicle[2]);
 
				SetDParam(3, _network_player_info[nd->company].num_vehicle[3]);
 
				SetDParam(4, _network_player_info[nd->company].num_vehicle[4]);
 
				DrawString(x, y, STR_NETWORK_VEHICLES, TC_GOLD); // vehicles
 
				y += 10;
 

	
 
				SetDParam(0, _network_player_info[nd->company].num_station[0]);
 
				SetDParam(1, _network_player_info[nd->company].num_station[1]);
 
				SetDParam(2, _network_player_info[nd->company].num_station[2]);
 
				SetDParam(3, _network_player_info[nd->company].num_station[3]);
 
				SetDParam(4, _network_player_info[nd->company].num_station[4]);
 
				DrawString(x, y, STR_NETWORK_STATIONS, TC_GOLD); // stations
 
				y += 10;
 

	
 
				SetDParamStr(0, _network_player_info[nd->company].players);
 
				DrawStringTruncated(x, y, STR_NETWORK_PLAYERS, TC_GOLD, trunc_width); // players
 
			}
 
		} break;
 

	
 
	case WE_MESSAGE:
 
		SetWindowDirty(w);
 
		break;
 
		case WE_CLICK:
 
			switch (e->we.click.widget) {
 
				case NLWW_CLOSE:    // Close 'X'
 
				case NLWW_CANCEL:   // Cancel button
 
					ShowNetworkGameWindow();
 
					break;
 

	
 
				case NLWW_MATRIX: { // Company list
 
					uint32 id_v = (e->we.click.pt.y - NET_PRC__OFFSET_TOP_WIDGET_COMPANY) / NET_PRC__SIZE_OF_ROW;
 

	
 
					if (id_v >= w->vscroll.cap) break;
 

	
 
					id_v += w->vscroll.pos;
 
					nd->company = (id_v >= nd->server->info.companies_on) ? INVALID_PLAYER : NetworkLobbyFindCompanyIndex(id_v);
 
					SetWindowDirty(w);
 
				} break;
 

	
 
				case NLWW_JOIN:     // Join company
 
					/* Button can be clicked only when it is enabled */
 
					_network_playas = nd->company;
 
					NetworkClientConnectGame(_network_last_host, _network_last_port);
 
					break;
 

	
 
				case NLWW_NEW:      // New company
 
					_network_playas = PLAYER_NEW_COMPANY;
 
					NetworkClientConnectGame(_network_last_host, _network_last_port);
 
					break;
 

	
 
				case NLWW_SPECTATE: // Spectate game
 
					_network_playas = PLAYER_SPECTATOR;
 
					NetworkClientConnectGame(_network_last_host, _network_last_port);
 
					break;
 

	
 
				case NLWW_REFRESH:  // Refresh
 
					NetworkTCPQueryServer(_network_last_host, _network_last_port); // company info
 
					NetworkUDPQueryServer(_network_last_host, _network_last_port); // general data
 
					break;
 
			}
 
			break;
 

	
 
		case WE_MESSAGE:
 
			SetWindowDirty(w);
 
			break;
 
	}
 
}
 

	
 
@@ -1171,13 +1189,12 @@ static const WindowDesc _network_lobby_w
 
 * @param ngl Selected game pointer which is passed to the new window */
 
static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
 
{
 
	Window *w;
 
	DeleteWindowById(WC_NETWORK_WINDOW, 0);
 

	
 
	NetworkTCPQueryServer(_network_last_host, _network_last_port); // company info
 
	NetworkUDPQueryServer(_network_last_host, _network_last_port); // general data
 

	
 
	w = AllocateWindowDesc(&_network_lobby_window_desc);
 
	Window *w = AllocateWindowDesc(&_network_lobby_window_desc);
 
	if (w != NULL) {
 
		WP(w, network_ql_d).n.server = ngl;
 
		strcpy(_edit_str_net_buf, "");
 
@@ -1254,36 +1271,39 @@ static void ClientList_Kick(byte client_
 

	
 
static void ClientList_Ban(byte client_no)
 
{
 
	uint i;
 
	uint32 ip = NetworkFindClientInfo(client_no)->client_ip;
 

	
 
	for (i = 0; i < lengthof(_network_ban_list); i++) {
 
	for (uint i = 0; i < lengthof(_network_ban_list); i++) {
 
		if (_network_ban_list[i] == NULL) {
 
			_network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ip));
 
			break;
 
		}
 
	}
 

	
 
	if (client_no < MAX_PLAYERS)
 
	if (client_no < MAX_PLAYERS) {
 
		SEND_COMMAND(PACKET_SERVER_ERROR)(DEREF_CLIENT(client_no), NETWORK_ERROR_KICKED);
 
	}
 
}
 

	
 
static void ClientList_GiveMoney(byte client_no)
 
{
 
	if (NetworkFindClientInfo(client_no) != NULL)
 
	if (NetworkFindClientInfo(client_no) != NULL) {
 
		ShowNetworkGiveMoneyWindow(NetworkFindClientInfo(client_no)->client_playas);
 
	}
 
}
 

	
 
static void ClientList_SpeakToClient(byte client_no)
 
{
 
	if (NetworkFindClientInfo(client_no) != NULL)
 
	if (NetworkFindClientInfo(client_no) != NULL) {
 
		ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, NetworkFindClientInfo(client_no)->client_index);
 
	}
 
}
 

	
 
static void ClientList_SpeakToCompany(byte client_no)
 
{
 
	if (NetworkFindClientInfo(client_no) != NULL)
 
	if (NetworkFindClientInfo(client_no) != NULL) {
 
		ShowNetworkChatQueryWindow(DESTTYPE_TEAM, NetworkFindClientInfo(client_no)->client_playas);
 
	}
 
}
 

	
 
static void ClientList_SpeakToAll(byte client_no)
 
@@ -1293,34 +1313,38 @@ static void ClientList_SpeakToAll(byte c
 

	
 
static void ClientList_None(byte client_no)
 
{
 
	// No action ;)
 
	/* No action ;) */
 
}
 

	
 

	
 

	
 
// Help, a action is clicked! What do we do?
 
/**
 
 * An action is clicked! What do we do?
 
 */
 
static void HandleClientListPopupClick(byte index, byte clientno)
 
{
 
	// A click on the Popup of the ClientList.. handle the command
 
	/* A click on the Popup of the ClientList.. handle the command */
 
	if (index < MAX_CLIENTLIST_ACTION && _clientlist_proc[index] != NULL) {
 
		_clientlist_proc[index](clientno);
 
	}
 
}
 

	
 
// Finds the amount of clients and set the height correct
 
/**
 
 * Finds the amount of clients and set the height correct
 
 */
 
static bool CheckClientListHeight(Window *w)
 
{
 
	int num = 0;
 
	const NetworkClientInfo *ci;
 

	
 
	// Should be replaced with a loop through all clients
 
	/* Should be replaced with a loop through all clients */
 
	FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
 
		num++;
 
	}
 

	
 
	num *= CLNWND_ROWSIZE;
 

	
 
	// If height is changed
 
	/* If height is changed */
 
	if (w->height != CLNWND_OFFSET + num + 1) {
 
		// XXX - magic unfortunately; (num + 2) has to be one bigger than heigh (num + 1)
 
		SetWindowDirty(w);
 
@@ -1332,13 +1356,15 @@ static bool CheckClientListHeight(Window
 
	return true;
 
}
 

	
 
// Finds the amount of actions in the popup and set the height correct
 
/**
 
 * Finds the amount of actions in the popup and set the height correct
 
 */
 
static uint ClientListPopupHeight()
 
{
 
	int i, num = 0;
 
	int num = 0;
 

	
 
	// Find the amount of actions
 
	for (i = 0; i < MAX_CLIENTLIST_ACTION; i++) {
 
	for (int i = 0; i < MAX_CLIENTLIST_ACTION; i++) {
 
		if (_clientlist_action[i][0] == '\0') continue;
 
		if (_clientlist_proc[i] == NULL) continue;
 
		num++;
 
@@ -1349,21 +1375,25 @@ static uint ClientListPopupHeight()
 
	return num + 1;
 
}
 

	
 
// Show the popup (action list)
 
/**
 
 * Show the popup (action list)
 
 */
 
static Window *PopupClientList(Window *w, int client_no, int x, int y)
 
{
 
	int i, h;
 
	int i;
 
	const NetworkClientInfo *ci;
 
	DeleteWindowById(WC_TOOLBAR_MENU, 0);
 

	
 
	// Clean the current actions
 
	/* Clean the current actions */
 
	for (i = 0; i < MAX_CLIENTLIST_ACTION; i++) {
 
		_clientlist_action[i][0] = '\0';
 
		_clientlist_proc[i] = NULL;
 
	}
 

	
 
	// Fill the actions this client has
 
	// Watch is, max 50 chars long!
 
	/*
 
	 * Fill the actions this client has.
 
	 * Watch is, max 50 chars long!
 
	 */
 

	
 
	ci = NetworkFindClientInfo(client_no);
 
	if (ci == NULL) return NULL;
 
@@ -1389,7 +1419,7 @@ static Window *PopupClientList(Window *w
 
		}
 
	}
 

	
 
	// A server can kick clients (but not himself)
 
	/* A server can kick clients (but not himself) */
 
	if (_network_server && _network_own_client_index != ci->client_index) {
 
		GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_KICK, lastof(_clientlist_action[i]));
 
		_clientlist_proc[i++] = &ClientList_Kick;
 
@@ -1404,9 +1434,9 @@ static Window *PopupClientList(Window *w
 
	}
 

	
 
	/* Calculate the height */
 
	h = ClientListPopupHeight();
 
	int h = ClientListPopupHeight();
 

	
 
	// Allocate the popup
 
	/* Allocate the popup */
 
	w = AllocateWindow(x, y, 150, h + 1, ClientListPopupWndProc, WC_TOOLBAR_MENU, _client_list_popup_widgets);
 
	w->widget[0].bottom = w->widget[0].top + h;
 
	w->widget[0].right = w->widget[0].left + 150;
 
@@ -1427,127 +1457,127 @@ static Window *PopupClientList(Window *w
 
static void ClientListPopupWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_PAINT: {
 
		int i, y, sel;
 
		TextColour colour;
 
		DrawWindowWidgets(w);
 
		case WE_PAINT: {
 
			DrawWindowWidgets(w);
 

	
 
			/* Draw the actions */
 
			int sel = WP(w, menu_d).sel_index;
 
			int y = 1;
 
			for (int i = 0; i < MAX_CLIENTLIST_ACTION; i++, y += CLNWND_ROWSIZE) {
 
				if (_clientlist_action[i][0] == '\0') continue;
 
				if (_clientlist_proc[i] == NULL) continue;
 

	
 
		// Draw the actions
 
		sel = WP(w, menu_d).sel_index;
 
		y = 1;
 
		for (i = 0; i < MAX_CLIENTLIST_ACTION; i++, y += CLNWND_ROWSIZE) {
 
			if (_clientlist_action[i][0] == '\0') continue;
 
			if (_clientlist_proc[i] == NULL) continue;
 
				TextColour colour;
 
				if (sel-- == 0) { // Selected item, highlight it
 
					GfxFillRect(1, y, 150 - 2, y + CLNWND_ROWSIZE - 1, 0);
 
					colour = TC_WHITE;
 
				} else {
 
					colour = TC_BLACK;
 
				}
 

	
 
			if (sel-- == 0) { // Selected item, highlight it
 
				GfxFillRect(1, y, 150 - 2, y + CLNWND_ROWSIZE - 1, 0);
 
				colour = TC_WHITE;
 
			} else {
 
				colour = TC_BLACK;
 
				DoDrawString(_clientlist_action[i], 4, y, colour);
 
			}
 
		} break;
 

	
 
		case WE_POPUPMENU_SELECT: {
 
			/* We selected an action */
 
			int index = (e->we.popupmenu.pt.y - w->top) / CLNWND_ROWSIZE;
 

	
 
			if (index >= 0 && e->we.popupmenu.pt.y >= w->top) {
 
				HandleClientListPopupClick(index, WP(w, menu_d).main_button);
 
			}
 

	
 
			DoDrawString(_clientlist_action[i], 4, y, colour);
 
		}
 
	} break;
 

	
 
	case WE_POPUPMENU_SELECT: {
 
		// We selected an action
 
		int index = (e->we.popupmenu.pt.y - w->top) / CLNWND_ROWSIZE;
 

	
 
		if (index >= 0 && e->we.popupmenu.pt.y >= w->top)
 
			HandleClientListPopupClick(index, WP(w, menu_d).main_button);
 
			DeleteWindowById(WC_TOOLBAR_MENU, 0);
 
		} break;
 

	
 
		DeleteWindowById(WC_TOOLBAR_MENU, 0);
 
	} break;
 

	
 
	case WE_POPUPMENU_OVER: {
 
		// Our mouse hoovers over an action? Select it!
 
		int index = (e->we.popupmenu.pt.y - w->top) / CLNWND_ROWSIZE;
 
		case WE_POPUPMENU_OVER: {
 
			/* Our mouse hoovers over an action? Select it! */
 
			int index = (e->we.popupmenu.pt.y - w->top) / CLNWND_ROWSIZE;
 

	
 
		if (index == -1 || index == WP(w, menu_d).sel_index) return;
 
			if (index == -1 || index == WP(w, menu_d).sel_index) return;
 

	
 
		WP(w, menu_d).sel_index = index;
 
		SetWindowDirty(w);
 
	} break;
 

	
 
			WP(w, menu_d).sel_index = index;
 
			SetWindowDirty(w);
 
		} break;
 
	}
 
}
 

	
 
// Main handle for clientlist
 
/**
 
 * Main handle for clientlist
 
 */
 
static void ClientListWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_PAINT: {
 
		NetworkClientInfo *ci;
 
		int y, i = 0;
 
		TextColour colour;
 
		case WE_PAINT: {
 
			NetworkClientInfo *ci;
 
			int i = 0;
 

	
 
			/* Check if we need to reset the height */
 
			if (!CheckClientListHeight(w)) break;
 

	
 
			DrawWindowWidgets(w);
 

	
 
			int y = CLNWND_OFFSET;
 

	
 
		// Check if we need to reset the height
 
		if (!CheckClientListHeight(w)) break;
 
			FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
 
				TextColour colour;
 
				if (_selected_clientlist_item == i++) { // Selected item, highlight it
 
					GfxFillRect(1, y, 248, y + CLNWND_ROWSIZE - 1, 0);
 
					colour = TC_WHITE;
 
				} else {
 
					colour = TC_BLACK;
 
				}
 

	
 
		DrawWindowWidgets(w);
 
				if (ci->client_index == NETWORK_SERVER_INDEX) {
 
					DrawString(4, y, STR_NETWORK_SERVER, colour);
 
				} else {
 
					DrawString(4, y, STR_NETWORK_CLIENT, colour);
 
				}
 

	
 
		y = CLNWND_OFFSET;
 
				/* Filter out spectators */
 
				if (IsValidPlayer(ci->client_playas)) DrawPlayerIcon(ci->client_playas, 64, y + 1);
 

	
 
		FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
 
			if (_selected_clientlist_item == i++) { // Selected item, highlight it
 
				GfxFillRect(1, y, 248, y + CLNWND_ROWSIZE - 1, 0);
 
				colour = TC_WHITE;
 
				DoDrawString(ci->client_name, 81, y, colour);
 

	
 
				y += CLNWND_ROWSIZE;
 
			}
 
		} break;
 

	
 
		case WE_CLICK:
 
			/* Show the popup with option */
 
			if (_selected_clientlist_item != 255) {
 
				PopupClientList(w, _selected_clientlist_item, e->we.click.pt.x + w->left, e->we.click.pt.y + w->top);
 
			}
 
			break;
 

	
 
		case WE_MOUSEOVER:
 
			/* -1 means we left the current window */
 
			if (e->we.mouseover.pt.y == -1) {
 
				_selected_clientlist_y = 0;
 
				_selected_clientlist_item = 255;
 
				SetWindowDirty(w);
 
				break;
 
			}
 
			/* It did not change.. no update! */
 
			if (e->we.mouseover.pt.y == _selected_clientlist_y) break;
 

	
 
			/* Find the new selected item (if any) */
 
			_selected_clientlist_y = e->we.mouseover.pt.y;
 
			if (e->we.mouseover.pt.y > CLNWND_OFFSET) {
 
				_selected_clientlist_item = (e->we.mouseover.pt.y - CLNWND_OFFSET) / CLNWND_ROWSIZE;
 
			} else {
 
				colour = TC_BLACK;
 
			}
 

	
 
			if (ci->client_index == NETWORK_SERVER_INDEX) {
 
				DrawString(4, y, STR_NETWORK_SERVER, colour);
 
			} else {
 
				DrawString(4, y, STR_NETWORK_CLIENT, colour);
 
				_selected_clientlist_item = 255;
 
			}
 

	
 
			// Filter out spectators
 
			if (IsValidPlayer(ci->client_playas)) DrawPlayerIcon(ci->client_playas, 64, y + 1);
 

	
 
			DoDrawString(ci->client_name, 81, y, colour);
 

	
 
			y += CLNWND_ROWSIZE;
 
		}
 
	} break;
 

	
 
	case WE_CLICK:
 
		// Show the popup with option
 
		if (_selected_clientlist_item != 255) {
 
			PopupClientList(w, _selected_clientlist_item, e->we.click.pt.x + w->left, e->we.click.pt.y + w->top);
 
		}
 

	
 
		break;
 

	
 
	case WE_MOUSEOVER:
 
		// -1 means we left the current window
 
		if (e->we.mouseover.pt.y == -1) {
 
			_selected_clientlist_y = 0;
 
			_selected_clientlist_item = 255;
 
			/* Repaint */
 
			SetWindowDirty(w);
 
			break;
 
		}
 
		// It did not change.. no update!
 
		if (e->we.mouseover.pt.y == _selected_clientlist_y) break;
 

	
 
		// Find the new selected item (if any)
 
		_selected_clientlist_y = e->we.mouseover.pt.y;
 
		if (e->we.mouseover.pt.y > CLNWND_OFFSET) {
 
			_selected_clientlist_item = (e->we.mouseover.pt.y - CLNWND_OFFSET) / CLNWND_ROWSIZE;
 
		} else {
 
		case WE_DESTROY: case WE_CREATE:
 
			/* When created or destroyed, data is reset */
 
			_selected_clientlist_item = 255;
 
		}
 

	
 
		// Repaint
 
		SetWindowDirty(w);
 
		break;
 

	
 
	case WE_DESTROY: case WE_CREATE:
 
		// When created or destroyed, data is reset
 
		_selected_clientlist_item = 255;
 
		_selected_clientlist_y = 0;
 
		break;
 
			_selected_clientlist_y = 0;
 
			break;
 
	}
 
}
 

	
 
@@ -1577,54 +1607,52 @@ void ShowNetworkNeedPassword(NetworkPass
 
static void NetworkJoinStatusWindowWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_PAINT: {
 
		uint8 progress; // used for progress bar
 
		DrawWindowWidgets(w);
 
		case WE_PAINT: {
 
			uint8 progress; // used for progress bar
 
			DrawWindowWidgets(w);
 

	
 
		DrawStringCentered(125, 35, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_GREY);
 
		switch (_network_join_status) {
 
			case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
 
			case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
 
				progress = 10; // first two stages 10%
 
				break;
 
			case NETWORK_JOIN_STATUS_WAITING:
 
				SetDParam(0, _network_join_waiting);
 
				DrawStringCentered(125, 46, STR_NETWORK_CONNECTING_WAITING, TC_GREY);
 
				progress = 15; // third stage is 15%
 
				break;
 
			case NETWORK_JOIN_STATUS_DOWNLOADING:
 
				SetDParam(0, _network_join_kbytes);
 
				SetDParam(1, _network_join_kbytes_total);
 
				DrawStringCentered(125, 46, STR_NETWORK_CONNECTING_DOWNLOADING, TC_GREY);
 
				/* Fallthrough */
 
			default: /* Waiting is 15%, so the resting receivement of map is maximum 70% */
 
				progress = 15 + _network_join_kbytes * (100 - 15) / _network_join_kbytes_total;
 
		}
 
			DrawStringCentered(125, 35, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_GREY);
 
			switch (_network_join_status) {
 
				case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
 
				case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
 
					progress = 10; // first two stages 10%
 
					break;
 
				case NETWORK_JOIN_STATUS_WAITING:
 
					SetDParam(0, _network_join_waiting);
 
					DrawStringCentered(125, 46, STR_NETWORK_CONNECTING_WAITING, TC_GREY);
 
					progress = 15; // third stage is 15%
 
					break;
 
				case NETWORK_JOIN_STATUS_DOWNLOADING:
 
					SetDParam(0, _network_join_kbytes);
 
					SetDParam(1, _network_join_kbytes_total);
 
					DrawStringCentered(125, 46, STR_NETWORK_CONNECTING_DOWNLOADING, TC_GREY);
 
					/* Fallthrough */
 
				default: /* Waiting is 15%, so the resting receivement of map is maximum 70% */
 
					progress = 15 + _network_join_kbytes * (100 - 15) / _network_join_kbytes_total;
 
			}
 

	
 
		/* Draw nice progress bar :) */
 
		DrawFrameRect(20, 18, (int)((w->width - 20) * progress / 100), 28, 10, FR_NONE);
 
	} break;
 
			/* Draw nice progress bar :) */
 
			DrawFrameRect(20, 18, (int)((w->width - 20) * progress / 100), 28, 10, FR_NONE);
 
		} break;
 

	
 
	case WE_CLICK:
 
		switch (e->we.click.widget) {
 
			case 2: /* Disconnect button */
 
		case WE_CLICK:
 
			if (e->we.click.widget == 2) { //Disconnect button
 
				NetworkDisconnect();
 
				DeleteWindow(w);
 
				SwitchMode(SM_MENU);
 
				ShowNetworkGameWindow();
 
				break;
 
		}
 
		break;
 

	
 
		/* If the server asks for a password, we need to fill it in */
 
		case WE_ON_EDIT_TEXT_CANCEL:
 
			NetworkDisconnect();
 
			ShowNetworkGameWindow();
 
			}
 
			break;
 

	
 
		case WE_ON_EDIT_TEXT:
 
			SEND_COMMAND(PACKET_CLIENT_PASSWORD)(pw_type, e->we.edittext.str);
 
			break;
 
			/* If the server asks for a password, we need to fill it in */
 
			case WE_ON_EDIT_TEXT_CANCEL:
 
				NetworkDisconnect();
 
				ShowNetworkGameWindow();
 
				break;
 

	
 
			case WE_ON_EDIT_TEXT:
 
				SEND_COMMAND(PACKET_CLIENT_PASSWORD)(pw_type, e->we.edittext.str);
 
				break;
 
	}
 
}
 

	
 
@@ -1645,9 +1673,8 @@ static const WindowDesc _network_join_st
 

	
 
void ShowJoinStatusWindow()
 
{
 
	Window *w;
 
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
	w = AllocateWindowDesc(&_network_join_status_window_desc);
 
	Window *w = AllocateWindowDesc(&_network_join_status_window_desc);
 
	/* Parent the status window to the lobby */
 
	if (w != NULL) w->parent = FindWindowById(WC_NETWORK_WINDOW, 0);
 
}
 
@@ -1802,60 +1829,60 @@ static void ChatTabCompletion(Window *w)
 
static void ChatWindowWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_CREATE:
 
		SendWindowMessage(WC_NEWS_WINDOW, 0, WE_CREATE, w->height, 0);
 
		SetBit(_no_scroll, SCROLL_CHAT); // do not scroll the game with the arrow-keys
 
		break;
 
		case WE_CREATE:
 
			SendWindowMessage(WC_NEWS_WINDOW, 0, WE_CREATE, w->height, 0);
 
			SetBit(_no_scroll, SCROLL_CHAT); // do not scroll the game with the arrow-keys
 
			break;
 

	
 
	case WE_PAINT: {
 
		static const StringID chat_captions[] = {
 
			STR_NETWORK_CHAT_ALL_CAPTION,
 
			STR_NETWORK_CHAT_COMPANY_CAPTION,
 
			STR_NETWORK_CHAT_CLIENT_CAPTION
 
		};
 

	
 
		DrawWindowWidgets(w);
 

	
 
		assert((uint)WP(w, chatquerystr_d).dtype < lengthof(chat_captions));
 
		DrawStringRightAligned(w->widget[2].left - 2, w->widget[2].top + 1, chat_captions[WP(w, chatquerystr_d).dtype], TC_BLACK);
 
		DrawEditBox(w, &WP(w, chatquerystr_d), 2);
 
	} break;
 
		case WE_PAINT: {
 
			static const StringID chat_captions[] = {
 
				STR_NETWORK_CHAT_ALL_CAPTION,
 
				STR_NETWORK_CHAT_COMPANY_CAPTION,
 
				STR_NETWORK_CHAT_CLIENT_CAPTION
 
			};
 

	
 
	case WE_CLICK:
 
		switch (e->we.click.widget) {
 
			case 2:
 
				ShowOnScreenKeyboard(w, &WP(w, chatquerystr_d), 2, 0, 3);
 
				break;
 
			DrawWindowWidgets(w);
 

	
 
			assert((uint)WP(w, chatquerystr_d).dtype < lengthof(chat_captions));
 
			DrawStringRightAligned(w->widget[2].left - 2, w->widget[2].top + 1, chat_captions[WP(w, chatquerystr_d).dtype], TC_BLACK);
 
			DrawEditBox(w, &WP(w, chatquerystr_d), 2);
 
		} break;
 

	
 
			case 3: /* Send */
 
				SendChat(WP(w, chatquerystr_d).text.buf, WP(w, chatquerystr_d).dtype, WP(w, chatquerystr_d).dest);
 
			/* FALLTHROUGH */
 
			case 0: /* Cancel */ DeleteWindow(w); break;
 
		}
 
		break;
 
		case WE_CLICK:
 
			switch (e->we.click.widget) {
 
				case 2:
 
					ShowOnScreenKeyboard(w, &WP(w, chatquerystr_d), 2, 0, 3);
 
					break;
 

	
 
	case WE_MOUSELOOP:
 
		HandleEditBox(w, &WP(w, chatquerystr_d), 2);
 
		break;
 

	
 
	case WE_KEYPRESS:
 
		if (e->we.keypress.keycode == WKC_TAB) {
 
			ChatTabCompletion(w);
 
		} else {
 
			_chat_tab_completion_active = false;
 
			switch (HandleEditBoxKey(w, &WP(w, chatquerystr_d), 2, e)) {
 
				case 1: /* Return */
 
				case 3: /* Send */
 
					SendChat(WP(w, chatquerystr_d).text.buf, WP(w, chatquerystr_d).dtype, WP(w, chatquerystr_d).dest);
 
				/* FALLTHROUGH */
 
				case 2: /* Escape */ DeleteWindow(w); break;
 
				case 0: /* Cancel */ DeleteWindow(w); break;
 
			}
 
		}
 
		break;
 
			break;
 

	
 
		case WE_MOUSELOOP:
 
			HandleEditBox(w, &WP(w, chatquerystr_d), 2);
 
			break;
 

	
 
	case WE_DESTROY:
 
		SendWindowMessage(WC_NEWS_WINDOW, 0, WE_DESTROY, 0, 0);
 
		ClrBit(_no_scroll, SCROLL_CHAT);
 
		break;
 
		case WE_KEYPRESS:
 
			if (e->we.keypress.keycode == WKC_TAB) {
 
				ChatTabCompletion(w);
 
			} else {
 
				_chat_tab_completion_active = false;
 
				switch (HandleEditBoxKey(w, &WP(w, chatquerystr_d), 2, e)) {
 
					case 1: /* Return */
 
						SendChat(WP(w, chatquerystr_d).text.buf, WP(w, chatquerystr_d).dtype, WP(w, chatquerystr_d).dest);
 
					/* FALLTHROUGH */
 
					case 2: /* Escape */ DeleteWindow(w); break;
 
				}
 
			}
 
			break;
 

	
 
		case WE_DESTROY:
 
			SendWindowMessage(WC_NEWS_WINDOW, 0, WE_DESTROY, 0, 0);
 
			ClrBit(_no_scroll, SCROLL_CHAT);
 
			break;
 
	}
 
}
 

	
 
@@ -1877,14 +1904,12 @@ static const WindowDesc _chat_window_des
 

	
 
void ShowNetworkChatQueryWindow(DestType type, int dest)
 
{
 
	Window *w;
 

	
 
	DeleteWindowById(WC_SEND_NETWORK_MSG, 0);
 

	
 
	_edit_str_net_buf[0] = '\0';
 
	_chat_tab_completion_active = false;
 

	
 
	w = AllocateWindowDesc(&_chat_window_desc);
 
	Window *w = AllocateWindowDesc(&_chat_window_desc);
 

	
 
	w->LowerWidget(2);
 
	WP(w, chatquerystr_d).dtype   = type;
0 comments (0 inline, 0 general)