1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file network_content_gui.cpp Implementation of the Network Content related GUIs. */
9 
10 #include "../stdafx.h"
11 #include "../strings_func.h"
12 #include "../gfx_func.h"
13 #include "../window_func.h"
14 #include "../error.h"
15 #include "../ai/ai.hpp"
16 #include "../game/game.hpp"
17 #include "../base_media_base.h"
18 #include "../openttd.h"
19 #include "../sortlist_type.h"
20 #include "../stringfilter_type.h"
21 #include "../querystring_gui.h"
22 #include "../core/geometry_func.hpp"
23 #include "../textfile_gui.h"
24 #include "network_content_gui.h"
25 
26 
27 #include "table/strings.h"
28 #include "../table/sprites.h"
29 
30 #include <bitset>
31 
32 #include "../safeguards.h"
33 
34 
35 /** Whether the user accepted to enter external websites during this session. */
36 static bool _accepted_external_search = false;
37 
38 
39 /** Window for displaying the textfile of an item in the content list. */
40 struct ContentTextfileWindow : public TextfileWindow {
41 	const ContentInfo *ci; ///< View the textfile of this ContentInfo.
42 
ContentTextfileWindowContentTextfileWindow43 	ContentTextfileWindow(TextfileType file_type, const ContentInfo *ci) : TextfileWindow(file_type), ci(ci)
44 	{
45 		const char *textfile = this->ci->GetTextfile(file_type);
46 		this->LoadTextfile(textfile, GetContentInfoSubDir(this->ci->type));
47 	}
48 
GetTypeStringContentTextfileWindow49 	StringID GetTypeString() const
50 	{
51 		switch (this->ci->type) {
52 			case CONTENT_TYPE_NEWGRF:        return STR_CONTENT_TYPE_NEWGRF;
53 			case CONTENT_TYPE_BASE_GRAPHICS: return STR_CONTENT_TYPE_BASE_GRAPHICS;
54 			case CONTENT_TYPE_BASE_SOUNDS:   return STR_CONTENT_TYPE_BASE_SOUNDS;
55 			case CONTENT_TYPE_BASE_MUSIC:    return STR_CONTENT_TYPE_BASE_MUSIC;
56 			case CONTENT_TYPE_AI:            return STR_CONTENT_TYPE_AI;
57 			case CONTENT_TYPE_AI_LIBRARY:    return STR_CONTENT_TYPE_AI_LIBRARY;
58 			case CONTENT_TYPE_GAME:          return STR_CONTENT_TYPE_GAME_SCRIPT;
59 			case CONTENT_TYPE_GAME_LIBRARY:  return STR_CONTENT_TYPE_GS_LIBRARY;
60 			case CONTENT_TYPE_SCENARIO:      return STR_CONTENT_TYPE_SCENARIO;
61 			case CONTENT_TYPE_HEIGHTMAP:     return STR_CONTENT_TYPE_HEIGHTMAP;
62 			default: NOT_REACHED();
63 		}
64 	}
65 
SetStringParametersContentTextfileWindow66 	void SetStringParameters(int widget) const override
67 	{
68 		if (widget == WID_TF_CAPTION) {
69 			SetDParam(0, this->GetTypeString());
70 			SetDParamStr(1, this->ci->name);
71 		}
72 	}
73 };
74 
ShowContentTextfileWindow(TextfileType file_type,const ContentInfo * ci)75 void ShowContentTextfileWindow(TextfileType file_type, const ContentInfo *ci)
76 {
77 	CloseWindowById(WC_TEXTFILE, file_type);
78 	new ContentTextfileWindow(file_type, ci);
79 }
80 
81 /** Nested widgets for the download window. */
82 static const NWidgetPart _nested_network_content_download_status_window_widgets[] = {
83 	NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CONTENT_DOWNLOAD_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
84 	NWidget(WWT_PANEL, COLOUR_GREY, WID_NCDS_BACKGROUND),
85 		NWidget(NWID_SPACER), SetMinimalSize(350, 0), SetMinimalTextLines(3, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 30),
86 		NWidget(NWID_HORIZONTAL),
87 			NWidget(NWID_SPACER), SetMinimalSize(125, 0),
88 			NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCDS_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
89 			NWidget(NWID_SPACER), SetFill(1, 0),
90 		EndContainer(),
91 		NWidget(NWID_SPACER), SetMinimalSize(0, 4),
92 	EndContainer(),
93 };
94 
95 /** Window description for the download window */
96 static WindowDesc _network_content_download_status_window_desc(
97 	WDP_CENTER, nullptr, 0, 0,
98 	WC_NETWORK_STATUS_WINDOW, WC_NONE,
99 	WDF_MODAL,
100 	_nested_network_content_download_status_window_widgets, lengthof(_nested_network_content_download_status_window_widgets)
101 );
102 
BaseNetworkContentDownloadStatusWindow(WindowDesc * desc)103 BaseNetworkContentDownloadStatusWindow::BaseNetworkContentDownloadStatusWindow(WindowDesc *desc) :
104 		Window(desc), cur_id(UINT32_MAX)
105 {
106 	_network_content_client.AddCallback(this);
107 	_network_content_client.DownloadSelectedContent(this->total_files, this->total_bytes);
108 
109 	this->InitNested(WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
110 }
111 
Close()112 void BaseNetworkContentDownloadStatusWindow::Close()
113 {
114 	_network_content_client.RemoveCallback(this);
115 	this->Window::Close();
116 }
117 
DrawWidget(const Rect & r,int widget) const118 void BaseNetworkContentDownloadStatusWindow::DrawWidget(const Rect &r, int widget) const
119 {
120 	if (widget != WID_NCDS_BACKGROUND) return;
121 
122 	/* Draw nice progress bar :) */
123 	DrawFrameRect(r.left + 20, r.top + 4, r.left + 20 + (int)((this->width - 40LL) * this->downloaded_bytes / this->total_bytes), r.top + 14, COLOUR_MAUVE, FR_NONE);
124 
125 	int y = r.top + 20;
126 	SetDParam(0, this->downloaded_bytes);
127 	SetDParam(1, this->total_bytes);
128 	SetDParam(2, this->downloaded_bytes * 100LL / this->total_bytes);
129 	DrawString(r.left + 2, r.right - 2, y, STR_CONTENT_DOWNLOAD_PROGRESS_SIZE, TC_FROMSTRING, SA_HOR_CENTER);
130 
131 	StringID str;
132 	if (this->downloaded_bytes == this->total_bytes) {
133 		str = STR_CONTENT_DOWNLOAD_COMPLETE;
134 	} else if (!this->name.empty()) {
135 		SetDParamStr(0, this->name);
136 		SetDParam(1, this->downloaded_files);
137 		SetDParam(2, this->total_files);
138 		str = STR_CONTENT_DOWNLOAD_FILE;
139 	} else {
140 		str = STR_CONTENT_DOWNLOAD_INITIALISE;
141 	}
142 
143 	y += FONT_HEIGHT_NORMAL + 5;
144 	DrawStringMultiLine(r.left + 2, r.right - 2, y, y + FONT_HEIGHT_NORMAL * 2, str, TC_FROMSTRING, SA_CENTER);
145 }
146 
OnDownloadProgress(const ContentInfo * ci,int bytes)147 void BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(const ContentInfo *ci, int bytes)
148 {
149 	if (ci->id != this->cur_id) {
150 		this->name = ci->filename;
151 		this->cur_id = ci->id;
152 		this->downloaded_files++;
153 	}
154 
155 	this->downloaded_bytes += bytes;
156 	this->SetDirty();
157 }
158 
159 
160 /** Window for showing the download status of content */
161 struct NetworkContentDownloadStatusWindow : public BaseNetworkContentDownloadStatusWindow {
162 private:
163 	std::vector<ContentType> receivedTypes;     ///< Types we received so we can update their cache
164 
165 public:
166 	/**
167 	 * Create a new download window based on a list of content information
168 	 * with flags whether to download them or not.
169 	 */
NetworkContentDownloadStatusWindowNetworkContentDownloadStatusWindow170 	NetworkContentDownloadStatusWindow() : BaseNetworkContentDownloadStatusWindow(&_network_content_download_status_window_desc)
171 	{
172 		this->parent = FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_CONTENT_LIST);
173 	}
174 
CloseNetworkContentDownloadStatusWindow175 	void Close() override
176 	{
177 		TarScanner::Mode mode = TarScanner::NONE;
178 		for (auto ctype : this->receivedTypes) {
179 			switch (ctype) {
180 				case CONTENT_TYPE_AI:
181 				case CONTENT_TYPE_AI_LIBRARY:
182 					/* AI::Rescan calls the scanner. */
183 					break;
184 				case CONTENT_TYPE_GAME:
185 				case CONTENT_TYPE_GAME_LIBRARY:
186 					/* Game::Rescan calls the scanner. */
187 					break;
188 
189 				case CONTENT_TYPE_BASE_GRAPHICS:
190 				case CONTENT_TYPE_BASE_SOUNDS:
191 				case CONTENT_TYPE_BASE_MUSIC:
192 					mode |= TarScanner::BASESET;
193 					break;
194 
195 				case CONTENT_TYPE_NEWGRF:
196 					/* ScanNewGRFFiles calls the scanner. */
197 					break;
198 
199 				case CONTENT_TYPE_SCENARIO:
200 				case CONTENT_TYPE_HEIGHTMAP:
201 					mode |= TarScanner::SCENARIO;
202 					break;
203 
204 				default:
205 					break;
206 			}
207 		}
208 
209 		TarScanner::DoScan(mode);
210 
211 		/* Tell all the backends about what we've downloaded */
212 		for (auto ctype : this->receivedTypes) {
213 			switch (ctype) {
214 				case CONTENT_TYPE_AI:
215 				case CONTENT_TYPE_AI_LIBRARY:
216 					AI::Rescan();
217 					break;
218 
219 				case CONTENT_TYPE_GAME:
220 				case CONTENT_TYPE_GAME_LIBRARY:
221 					Game::Rescan();
222 					break;
223 
224 				case CONTENT_TYPE_BASE_GRAPHICS:
225 					BaseGraphics::FindSets();
226 					SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
227 					break;
228 
229 				case CONTENT_TYPE_BASE_SOUNDS:
230 					BaseSounds::FindSets();
231 					SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
232 					break;
233 
234 				case CONTENT_TYPE_BASE_MUSIC:
235 					BaseMusic::FindSets();
236 					SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
237 					break;
238 
239 				case CONTENT_TYPE_NEWGRF:
240 					RequestNewGRFScan();
241 					break;
242 
243 				case CONTENT_TYPE_SCENARIO:
244 				case CONTENT_TYPE_HEIGHTMAP:
245 					extern void ScanScenarios();
246 					ScanScenarios();
247 					InvalidateWindowData(WC_SAVELOAD, 0, 0);
248 					break;
249 
250 				default:
251 					break;
252 			}
253 		}
254 
255 		/* Always invalidate the download window; tell it we are going to be gone */
256 		InvalidateWindowData(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_CONTENT_LIST, 2);
257 
258 		this->BaseNetworkContentDownloadStatusWindow::Close();
259 	}
260 
OnClickNetworkContentDownloadStatusWindow261 	void OnClick(Point pt, int widget, int click_count) override
262 	{
263 		if (widget == WID_NCDS_CANCELOK) {
264 			if (this->downloaded_bytes != this->total_bytes) {
265 				_network_content_client.CloseConnection();
266 				this->Close();
267 			} else {
268 				/* If downloading succeeded, close the online content window. This will close
269 				 * the current window as well. */
270 				CloseWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_CONTENT_LIST);
271 			}
272 		}
273 	}
274 
OnDownloadProgressNetworkContentDownloadStatusWindow275 	void OnDownloadProgress(const ContentInfo *ci, int bytes) override
276 	{
277 		BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(ci, bytes);
278 		include(this->receivedTypes, ci->type);
279 
280 		/* When downloading is finished change cancel in ok */
281 		if (this->downloaded_bytes == this->total_bytes) {
282 			this->GetWidget<NWidgetCore>(WID_NCDS_CANCELOK)->widget_data = STR_BUTTON_OK;
283 		}
284 	}
285 };
286 
287 /** Filter data for NetworkContentListWindow. */
288 struct ContentListFilterData {
289 	StringFilter string_filter; ///< Text filter of content list
290 	std::bitset<CONTENT_TYPE_END> types; ///< Content types displayed
291 };
292 
293 /** Filter criteria for NetworkContentListWindow. */
294 enum ContentListFilterCriteria {
295 	CONTENT_FILTER_TEXT = 0,        ///< Filter by query sting
296 	CONTENT_FILTER_TYPE_OR_SELECTED,///< Filter by being of displayed type or selected for download
297 };
298 
299 /** Window that lists the content that's at the content server */
300 class NetworkContentListWindow : public Window, ContentCallback {
301 	/** List with content infos. */
302 	typedef GUIList<const ContentInfo *, ContentListFilterData &> GUIContentList;
303 
304 	static const uint EDITBOX_MAX_SIZE   =  50; ///< Maximum size of the editbox in characters.
305 
306 	static Listing last_sorting;     ///< The last sorting setting.
307 	static Filtering last_filtering; ///< The last filtering setting.
308 	static GUIContentList::SortFunction * const sorter_funcs[];   ///< Sorter functions
309 	static GUIContentList::FilterFunction * const filter_funcs[]; ///< Filter functions.
310 	GUIContentList content;      ///< List with content
311 	bool auto_select;            ///< Automatically select all content when the meta-data becomes available
312 	ContentListFilterData filter_data; ///< Filter for content list
313 	QueryString filter_editbox;  ///< Filter editbox;
314 	Dimension checkbox_size;     ///< Size of checkbox/"blot" sprite
315 
316 	const ContentInfo *selected; ///< The selected content info
317 	int list_pos;                ///< Our position in the list
318 	uint filesize_sum;           ///< The sum of all selected file sizes
319 	Scrollbar *vscroll;          ///< Cache of the vertical scrollbar
320 
321 	static char content_type_strs[CONTENT_TYPE_END][64]; ///< Cached strings for all content types.
322 
323 	/** Search external websites for content */
OpenExternalSearch()324 	void OpenExternalSearch()
325 	{
326 		extern void OpenBrowser(const char *url);
327 
328 		char url[1024];
329 		const char *last = lastof(url);
330 
331 		char *pos = strecpy(url, "https://grfsearch.openttd.org/?", last);
332 
333 		if (this->auto_select) {
334 			pos = strecpy(pos, "do=searchgrfid&q=", last);
335 
336 			bool first = true;
337 			for (const ContentInfo *ci : this->content) {
338 				if (ci->state != ContentInfo::DOES_NOT_EXIST) continue;
339 
340 				if (!first) pos = strecpy(pos, ",", last);
341 				first = false;
342 
343 				pos += seprintf(pos, last, "%08X", ci->unique_id);
344 				pos = strecpy(pos, ":", last);
345 				pos = md5sumToString(pos, last, ci->md5sum);
346 			}
347 		} else {
348 			pos = strecpy(pos, "do=searchtext&q=", last);
349 
350 			/* Escape search term */
351 			for (const char *search = this->filter_editbox.text.buf; *search != '\0'; search++) {
352 				/* Remove quotes */
353 				if (*search == '\'' || *search == '"') continue;
354 
355 				/* Escape special chars, such as &%,= */
356 				if (*search < 0x30) {
357 					pos += seprintf(pos, last, "%%%02X", *search);
358 				} else if (pos < last) {
359 					*pos = *search;
360 					*++pos = '\0';
361 				}
362 			}
363 		}
364 
365 		OpenBrowser(url);
366 	}
367 
368 	/**
369 	 * Callback function for disclaimer about entering external websites.
370 	 */
ExternalSearchDisclaimerCallback(Window * w,bool accepted)371 	static void ExternalSearchDisclaimerCallback(Window *w, bool accepted)
372 	{
373 		if (accepted) {
374 			_accepted_external_search = true;
375 			((NetworkContentListWindow*)w)->OpenExternalSearch();
376 		}
377 	}
378 
379 	/**
380 	 * (Re)build the network game list as its amount has changed because
381 	 * an item has been added or deleted for example
382 	 */
BuildContentList()383 	void BuildContentList()
384 	{
385 		if (!this->content.NeedRebuild()) return;
386 
387 		/* Create temporary array of games to use for listing */
388 		this->content.clear();
389 
390 		bool all_available = true;
391 
392 		for (ConstContentIterator iter = _network_content_client.Begin(); iter != _network_content_client.End(); iter++) {
393 			if ((*iter)->state == ContentInfo::DOES_NOT_EXIST) all_available = false;
394 			this->content.push_back(*iter);
395 		}
396 
397 		this->SetWidgetDisabledState(WID_NCL_SEARCH_EXTERNAL, this->auto_select && all_available);
398 
399 		this->FilterContentList();
400 		this->content.shrink_to_fit();
401 		this->content.RebuildDone();
402 		this->SortContentList();
403 
404 		this->vscroll->SetCount((int)this->content.size()); // Update the scrollbar
405 		this->ScrollToSelected();
406 	}
407 
408 	/** Sort content by name. */
NameSorter(const ContentInfo * const & a,const ContentInfo * const & b)409 	static bool NameSorter(const ContentInfo * const &a, const ContentInfo * const &b)
410 	{
411 		return strnatcmp(a->name.c_str(), b->name.c_str(), true) < 0; // Sort by name (natural sorting).
412 	}
413 
414 	/** Sort content by type. */
TypeSorter(const ContentInfo * const & a,const ContentInfo * const & b)415 	static bool TypeSorter(const ContentInfo * const &a, const ContentInfo * const &b)
416 	{
417 		int r = 0;
418 		if (a->type != b->type) {
419 			r = strnatcmp(content_type_strs[a->type], content_type_strs[b->type]);
420 		}
421 		if (r == 0) return NameSorter(a, b);
422 		return r < 0;
423 	}
424 
425 	/** Sort content by state. */
StateSorter(const ContentInfo * const & a,const ContentInfo * const & b)426 	static bool StateSorter(const ContentInfo * const &a, const ContentInfo * const &b)
427 	{
428 		int r = a->state - b->state;
429 		if (r == 0) return TypeSorter(a, b);
430 		return r < 0;
431 	}
432 
433 	/** Sort the content list */
SortContentList()434 	void SortContentList()
435 	{
436 		if (!this->content.Sort()) return;
437 
438 		int idx = find_index(this->content, this->selected);
439 		if (idx >= 0) this->list_pos = idx;
440 	}
441 
442 	/** Filter content by tags/name */
TagNameFilter(const ContentInfo * const * a,ContentListFilterData & filter)443 	static bool CDECL TagNameFilter(const ContentInfo * const *a, ContentListFilterData &filter)
444 	{
445 		filter.string_filter.ResetState();
446 		for (auto &tag : (*a)->tags) filter.string_filter.AddLine(tag.c_str());
447 
448 		filter.string_filter.AddLine((*a)->name.c_str());
449 		return filter.string_filter.GetState();
450 	}
451 
452 	/** Filter content by type, but still show content selected for download. */
TypeOrSelectedFilter(const ContentInfo * const * a,ContentListFilterData & filter)453 	static bool CDECL TypeOrSelectedFilter(const ContentInfo * const *a, ContentListFilterData &filter)
454 	{
455 		if (filter.types.none()) return true;
456 		if (filter.types[(*a)->type]) return true;
457 		return ((*a)->state == ContentInfo::SELECTED || (*a)->state == ContentInfo::AUTOSELECTED);
458 	}
459 
460 	/** Filter the content list */
FilterContentList()461 	void FilterContentList()
462 	{
463 		/* Apply filters. */
464 		bool changed = false;
465 		if (!this->filter_data.string_filter.IsEmpty()) {
466 			this->content.SetFilterType(CONTENT_FILTER_TEXT);
467 			changed |= this->content.Filter(this->filter_data);
468 		}
469 		if (this->filter_data.types.any()) {
470 			this->content.SetFilterType(CONTENT_FILTER_TYPE_OR_SELECTED);
471 			changed |= this->content.Filter(this->filter_data);
472 		}
473 		if (!changed) return;
474 
475 		/* update list position */
476 		int idx = find_index(this->content, this->selected);
477 		if (idx >= 0) {
478 			this->list_pos = idx;
479 			return;
480 		}
481 
482 		/* previously selected item not in list anymore */
483 		this->selected = nullptr;
484 		this->list_pos = 0;
485 	}
486 
487 	/**
488 	 * Update filter state based on current window state.
489 	 * @return true if filter state was changed, otherwise false.
490 	 */
UpdateFilterState()491 	bool UpdateFilterState()
492 	{
493 		Filtering old_params = this->content.GetFiltering();
494 		bool new_state = !this->filter_data.string_filter.IsEmpty() || this->filter_data.types.any();
495 		if (new_state != old_params.state) {
496 			this->content.SetFilterState(new_state);
497 		}
498 		return new_state != old_params.state;
499 	}
500 
501 	/** Make sure that the currently selected content info is within the visible part of the matrix */
ScrollToSelected()502 	void ScrollToSelected()
503 	{
504 		if (this->selected == nullptr) return;
505 
506 		this->vscroll->ScrollTowards(this->list_pos);
507 	}
508 
509 	friend void BuildContentTypeStringList();
510 public:
511 	/**
512 	 * Create the content list window.
513 	 * @param desc the window description to pass to Window's constructor.
514 	 * @param select_all Whether the select all button is allowed or not.
515 	 * @param types the main type of content to display or #CONTENT_TYPE_END.
516 	 *   When a type other than #CONTENT_TYPE_END is given, dependencies of
517 	 *   other types are only shown when content that depend on them are
518 	 *   selected.
519 	 */
NetworkContentListWindow(WindowDesc * desc,bool select_all,const std::bitset<CONTENT_TYPE_END> & types)520 	NetworkContentListWindow(WindowDesc *desc, bool select_all, const std::bitset<CONTENT_TYPE_END> &types) :
521 			Window(desc),
522 			auto_select(select_all),
523 			filter_editbox(EDITBOX_MAX_SIZE),
524 			selected(nullptr),
525 			list_pos(0)
526 	{
527 		this->checkbox_size = maxdim(maxdim(GetSpriteSize(SPR_BOX_EMPTY), GetSpriteSize(SPR_BOX_CHECKED)), GetSpriteSize(SPR_BLOT));
528 
529 		this->CreateNestedTree();
530 		this->vscroll = this->GetScrollbar(WID_NCL_SCROLLBAR);
531 		this->FinishInitNested(WN_NETWORK_WINDOW_CONTENT_LIST);
532 
533 		this->GetWidget<NWidgetStacked>(WID_NCL_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all);
534 
535 		this->querystrings[WID_NCL_FILTER] = &this->filter_editbox;
536 		this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
537 		this->SetFocusedWidget(WID_NCL_FILTER);
538 		this->SetWidgetDisabledState(WID_NCL_SEARCH_EXTERNAL, this->auto_select);
539 		this->filter_data.types = types;
540 
541 		_network_content_client.AddCallback(this);
542 		this->content.SetListing(this->last_sorting);
543 		this->content.SetFiltering(this->last_filtering);
544 		this->content.SetSortFuncs(this->sorter_funcs);
545 		this->content.SetFilterFuncs(this->filter_funcs);
546 		this->UpdateFilterState();
547 		this->content.ForceRebuild();
548 		this->FilterContentList();
549 		this->SortContentList();
550 		this->InvalidateData();
551 	}
552 
Close()553 	void Close() override
554 	{
555 		_network_content_client.RemoveCallback(this);
556 		this->Window::Close();
557 	}
558 
UpdateWidgetSize(int widget,Dimension * size,const Dimension & padding,Dimension * fill,Dimension * resize)559 	void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
560 	{
561 		switch (widget) {
562 			case WID_NCL_FILTER_CAPT:
563 				*size = maxdim(*size, GetStringBoundingBox(STR_CONTENT_FILTER_TITLE));
564 				break;
565 
566 			case WID_NCL_CHECKBOX:
567 				size->width = this->checkbox_size.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
568 				break;
569 
570 			case WID_NCL_TYPE: {
571 				Dimension d = *size;
572 				for (int i = CONTENT_TYPE_BEGIN; i < CONTENT_TYPE_END; i++) {
573 					d = maxdim(d, GetStringBoundingBox(STR_CONTENT_TYPE_BASE_GRAPHICS + i - CONTENT_TYPE_BASE_GRAPHICS));
574 				}
575 				size->width = d.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
576 				break;
577 			}
578 
579 			case WID_NCL_MATRIX:
580 				resize->height = std::max(this->checkbox_size.height, (uint)FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
581 				size->height = 10 * resize->height;
582 				break;
583 		}
584 	}
585 
586 
DrawWidget(const Rect & r,int widget) const587 	void DrawWidget(const Rect &r, int widget) const override
588 	{
589 		switch (widget) {
590 			case WID_NCL_FILTER_CAPT:
591 				DrawString(r.left, r.right, r.top, STR_CONTENT_FILTER_TITLE, TC_FROMSTRING, SA_RIGHT);
592 				break;
593 
594 			case WID_NCL_DETAILS:
595 				this->DrawDetails(r);
596 				break;
597 
598 			case WID_NCL_MATRIX:
599 				this->DrawMatrix(r);
600 				break;
601 		}
602 	}
603 
OnPaint()604 	void OnPaint() override
605 	{
606 		const SortButtonState arrow = this->content.IsDescSortOrder() ? SBS_DOWN : SBS_UP;
607 
608 		if (this->content.NeedRebuild()) {
609 			this->BuildContentList();
610 		}
611 
612 		this->DrawWidgets();
613 
614 		switch (this->content.SortType()) {
615 			case WID_NCL_CHECKBOX - WID_NCL_CHECKBOX: this->DrawSortButtonState(WID_NCL_CHECKBOX, arrow); break;
616 			case WID_NCL_TYPE     - WID_NCL_CHECKBOX: this->DrawSortButtonState(WID_NCL_TYPE,     arrow); break;
617 			case WID_NCL_NAME     - WID_NCL_CHECKBOX: this->DrawSortButtonState(WID_NCL_NAME,     arrow); break;
618 		}
619 	}
620 
621 	/**
622 	 * Draw/fill the matrix with the list of content to download.
623 	 * @param r The boundaries of the matrix.
624 	 */
DrawMatrix(const Rect & r) const625 	void DrawMatrix(const Rect &r) const
626 	{
627 		const NWidgetBase *nwi_checkbox = this->GetWidget<NWidgetBase>(WID_NCL_CHECKBOX);
628 		const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(WID_NCL_NAME);
629 		const NWidgetBase *nwi_type = this->GetWidget<NWidgetBase>(WID_NCL_TYPE);
630 
631 		int line_height = std::max(this->checkbox_size.height, (uint)FONT_HEIGHT_NORMAL);
632 
633 		/* Fill the matrix with the information */
634 		int sprite_y_offset = WD_MATRIX_TOP + (line_height - this->checkbox_size.height) / 2 - 1;
635 		int text_y_offset = WD_MATRIX_TOP + (line_height - FONT_HEIGHT_NORMAL) / 2;
636 		uint y = r.top;
637 
638 		auto iter = this->content.begin() + this->vscroll->GetPosition();
639 		size_t last = this->vscroll->GetPosition() + this->vscroll->GetCapacity();
640 		auto end = (last < this->content.size()) ? this->content.begin() + last : this->content.end();
641 
642 		for (/**/; iter != end; iter++) {
643 			const ContentInfo *ci = *iter;
644 
645 			if (ci == this->selected) GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 1, PC_GREY);
646 
647 			SpriteID sprite;
648 			SpriteID pal = PAL_NONE;
649 			switch (ci->state) {
650 				case ContentInfo::UNSELECTED:     sprite = SPR_BOX_EMPTY;   break;
651 				case ContentInfo::SELECTED:       sprite = SPR_BOX_CHECKED; break;
652 				case ContentInfo::AUTOSELECTED:   sprite = SPR_BOX_CHECKED; break;
653 				case ContentInfo::ALREADY_HERE:   sprite = SPR_BLOT; pal = PALETTE_TO_GREEN; break;
654 				case ContentInfo::DOES_NOT_EXIST: sprite = SPR_BLOT; pal = PALETTE_TO_RED;   break;
655 				default: NOT_REACHED();
656 			}
657 			DrawSprite(sprite, pal, nwi_checkbox->pos_x + (pal == PAL_NONE ? 2 : 3), y + sprite_y_offset + (pal == PAL_NONE ? 1 : 0));
658 
659 			StringID str = STR_CONTENT_TYPE_BASE_GRAPHICS + ci->type - CONTENT_TYPE_BASE_GRAPHICS;
660 			DrawString(nwi_type->pos_x, nwi_type->pos_x + nwi_type->current_x - 1, y + text_y_offset, str, TC_BLACK, SA_HOR_CENTER);
661 
662 			DrawString(nwi_name->pos_x + WD_FRAMERECT_LEFT, nwi_name->pos_x + nwi_name->current_x - WD_FRAMERECT_RIGHT, y + text_y_offset, ci->name, TC_BLACK);
663 			y += this->resize.step_height;
664 		}
665 	}
666 
667 	/**
668 	 * Helper function to draw the details part of this window.
669 	 * @param r the rectangle to stay within while drawing
670 	 */
DrawDetails(const Rect & r) const671 	void DrawDetails(const Rect &r) const
672 	{
673 		static const int DETAIL_LEFT         =  5; ///< Number of pixels at the left
674 		static const int DETAIL_RIGHT        =  5; ///< Number of pixels at the right
675 		static const int DETAIL_TOP          =  5; ///< Number of pixels at the top
676 
677 		/* Height for the title banner */
678 		int DETAIL_TITLE_HEIGHT = 5 * FONT_HEIGHT_NORMAL;
679 
680 		/* Create the nice grayish rectangle at the details top */
681 		GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + DETAIL_TITLE_HEIGHT, PC_DARK_BLUE);
682 		DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_INSET_TOP, STR_CONTENT_DETAIL_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
683 
684 		/* Draw the total download size */
685 		SetDParam(0, this->filesize_sum);
686 		DrawString(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_NORMAL, STR_CONTENT_TOTAL_DOWNLOAD_SIZE);
687 
688 		if (this->selected == nullptr) return;
689 
690 		/* And fill the rest of the details when there's information to place there */
691 		DrawStringMultiLine(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + DETAIL_TITLE_HEIGHT / 2, r.top + DETAIL_TITLE_HEIGHT, STR_CONTENT_DETAIL_SUBTITLE_UNSELECTED + this->selected->state, TC_FROMSTRING, SA_CENTER);
692 
693 		/* Also show the total download size, so keep some space from the bottom */
694 		const uint max_y = r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_WIDE;
695 		int y = r.top + DETAIL_TITLE_HEIGHT + DETAIL_TOP;
696 
697 		if (this->selected->upgrade) {
698 			SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
699 			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_UPDATE);
700 			y += WD_PAR_VSEP_WIDE;
701 		}
702 
703 		SetDParamStr(0, this->selected->name);
704 		y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_NAME);
705 
706 		if (!this->selected->version.empty()) {
707 			SetDParamStr(0, this->selected->version);
708 			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_VERSION);
709 		}
710 
711 		if (!this->selected->description.empty()) {
712 			SetDParamStr(0, this->selected->description);
713 			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DESCRIPTION);
714 		}
715 
716 		if (!this->selected->url.empty()) {
717 			SetDParamStr(0, this->selected->url);
718 			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_URL);
719 		}
720 
721 		SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
722 		y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TYPE);
723 
724 		y += WD_PAR_VSEP_WIDE;
725 		SetDParam(0, this->selected->filesize);
726 		y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_FILESIZE);
727 
728 		if (!this->selected->dependencies.empty()) {
729 			/* List dependencies */
730 			char buf[DRAW_STRING_BUFFER] = "";
731 			char *p = buf;
732 			for (auto &cid : this->selected->dependencies) {
733 				/* Try to find the dependency */
734 				ConstContentIterator iter = _network_content_client.Begin();
735 				for (; iter != _network_content_client.End(); iter++) {
736 					const ContentInfo *ci = *iter;
737 					if (ci->id != cid) continue;
738 
739 					p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name.c_str());
740 					break;
741 				}
742 			}
743 			SetDParamStr(0, buf);
744 			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DEPENDENCIES);
745 		}
746 
747 		if (!this->selected->tags.empty()) {
748 			/* List all tags */
749 			char buf[DRAW_STRING_BUFFER] = "";
750 			char *p = buf;
751 			for (auto &tag : this->selected->tags) {
752 				p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", tag.c_str());
753 			}
754 			SetDParamStr(0, buf);
755 			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TAGS);
756 		}
757 
758 		if (this->selected->IsSelected()) {
759 			/* When selected show all manually selected content that depends on this */
760 			ConstContentVector tree;
761 			_network_content_client.ReverseLookupTreeDependency(tree, this->selected);
762 
763 			char buf[DRAW_STRING_BUFFER] = "";
764 			char *p = buf;
765 			for (const ContentInfo *ci : tree) {
766 				if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue;
767 
768 				p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name.c_str());
769 			}
770 			if (p != buf) {
771 				SetDParamStr(0, buf);
772 				y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_SELECTED_BECAUSE_OF);
773 			}
774 		}
775 	}
776 
OnClick(Point pt,int widget,int click_count)777 	void OnClick(Point pt, int widget, int click_count) override
778 	{
779 		if (widget >= WID_NCL_TEXTFILE && widget < WID_NCL_TEXTFILE + TFT_END) {
780 			if (this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE) return;
781 
782 			ShowContentTextfileWindow((TextfileType)(widget - WID_NCL_TEXTFILE), this->selected);
783 			return;
784 		}
785 
786 		switch (widget) {
787 			case WID_NCL_MATRIX: {
788 				uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NCL_MATRIX);
789 				if (id_v >= this->content.size()) return; // click out of bounds
790 
791 				this->selected = this->content[id_v];
792 				this->list_pos = id_v;
793 
794 				const NWidgetBase *checkbox = this->GetWidget<NWidgetBase>(WID_NCL_CHECKBOX);
795 				if (click_count > 1 || IsInsideBS(pt.x, checkbox->pos_x, checkbox->current_x)) {
796 					_network_content_client.ToggleSelectedState(this->selected);
797 					this->content.ForceResort();
798 				}
799 
800 				if (this->filter_data.types.any()) {
801 					this->content.ForceRebuild();
802 				}
803 
804 				this->InvalidateData();
805 				break;
806 			}
807 
808 			case WID_NCL_CHECKBOX:
809 			case WID_NCL_TYPE:
810 			case WID_NCL_NAME:
811 				if (this->content.SortType() == widget - WID_NCL_CHECKBOX) {
812 					this->content.ToggleSortOrder();
813 					if (this->content.size() > 0) this->list_pos = (int)this->content.size() - this->list_pos - 1;
814 				} else {
815 					this->content.SetSortType(widget - WID_NCL_CHECKBOX);
816 					this->content.ForceResort();
817 					this->SortContentList();
818 				}
819 				this->ScrollToSelected();
820 				this->InvalidateData();
821 				break;
822 
823 			case WID_NCL_SELECT_ALL:
824 				_network_content_client.SelectAll();
825 				this->InvalidateData();
826 				break;
827 
828 			case WID_NCL_SELECT_UPDATE:
829 				_network_content_client.SelectUpgrade();
830 				this->InvalidateData();
831 				break;
832 
833 			case WID_NCL_UNSELECT:
834 				_network_content_client.UnselectAll();
835 				this->InvalidateData();
836 				break;
837 
838 			case WID_NCL_CANCEL:
839 				this->Close();
840 				break;
841 
842 			case WID_NCL_OPEN_URL:
843 				if (this->selected != nullptr) {
844 					extern void OpenBrowser(const char *url);
845 					OpenBrowser(this->selected->url.c_str());
846 				}
847 				break;
848 
849 			case WID_NCL_DOWNLOAD:
850 				if (BringWindowToFrontById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD) == nullptr) new NetworkContentDownloadStatusWindow();
851 				break;
852 
853 			case WID_NCL_SEARCH_EXTERNAL:
854 				if (_accepted_external_search) {
855 					this->OpenExternalSearch();
856 				} else {
857 					ShowQuery(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER_CAPTION, STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER, this, ExternalSearchDisclaimerCallback);
858 				}
859 				break;
860 		}
861 	}
862 
OnKeyPress(WChar key,uint16 keycode)863 	EventState OnKeyPress(WChar key, uint16 keycode) override
864 	{
865 		if (this->vscroll->UpdateListPositionOnKeyPress(this->list_pos, keycode) == ES_NOT_HANDLED) {
866 			switch (keycode) {
867 				case WKC_SPACE:
868 				case WKC_RETURN:
869 					if (keycode == WKC_RETURN || !IsWidgetFocused(WID_NCL_FILTER)) {
870 						if (this->selected != nullptr) {
871 							_network_content_client.ToggleSelectedState(this->selected);
872 							this->content.ForceResort();
873 							this->InvalidateData();
874 						}
875 						if (this->filter_data.types.any()) {
876 							this->content.ForceRebuild();
877 							this->InvalidateData();
878 						}
879 						return ES_HANDLED;
880 					}
881 					/* space is pressed and filter is focused. */
882 					FALLTHROUGH;
883 
884 				default:
885 					return ES_NOT_HANDLED;
886 			}
887 		}
888 
889 		if (this->content.size() == 0) {
890 			if (this->UpdateFilterState()) {
891 				this->content.ForceRebuild();
892 				this->InvalidateData();
893 			}
894 			return ES_HANDLED;
895 		}
896 
897 		this->selected = this->content[this->list_pos];
898 
899 		if (this->UpdateFilterState()) {
900 			this->content.ForceRebuild();
901 		} else {
902 			/* Scroll to the new content if it is outside the current range. */
903 			this->ScrollToSelected();
904 		}
905 
906 		/* redraw window */
907 		this->InvalidateData();
908 		return ES_HANDLED;
909 	}
910 
OnEditboxChanged(int wid)911 	void OnEditboxChanged(int wid) override
912 	{
913 		if (wid == WID_NCL_FILTER) {
914 			this->filter_data.string_filter.SetFilterTerm(this->filter_editbox.text.buf);
915 			this->UpdateFilterState();
916 			this->content.ForceRebuild();
917 			this->InvalidateData();
918 		}
919 	}
920 
OnResize()921 	void OnResize() override
922 	{
923 		this->vscroll->SetCapacityFromWidget(this, WID_NCL_MATRIX);
924 	}
925 
OnReceiveContentInfo(const ContentInfo * rci)926 	void OnReceiveContentInfo(const ContentInfo *rci) override
927 	{
928 		if (this->auto_select && !rci->IsSelected()) _network_content_client.ToggleSelectedState(rci);
929 		this->content.ForceRebuild();
930 		this->InvalidateData(0, false);
931 	}
932 
OnDownloadComplete(ContentID cid)933 	void OnDownloadComplete(ContentID cid) override
934 	{
935 		this->content.ForceResort();
936 		this->InvalidateData();
937 	}
938 
OnConnect(bool success)939 	void OnConnect(bool success) override
940 	{
941 		if (!success) {
942 			ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_CONNECT, INVALID_STRING_ID, WL_ERROR);
943 			this->Close();
944 			return;
945 		}
946 
947 		this->InvalidateData();
948 	}
949 
950 	/**
951 	 * Some data on this window has become invalid.
952 	 * @param data Information about the changed data.
953 	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
954 	 */
OnInvalidateData(int data=0,bool gui_scope=true)955 	void OnInvalidateData(int data = 0, bool gui_scope = true) override
956 	{
957 		if (!gui_scope) return;
958 		if (this->content.NeedRebuild()) this->BuildContentList();
959 
960 		/* To sum all the bytes we intend to download */
961 		this->filesize_sum = 0;
962 		bool show_select_all = false;
963 		bool show_select_upgrade = false;
964 		for (const ContentInfo *ci : this->content) {
965 			switch (ci->state) {
966 				case ContentInfo::SELECTED:
967 				case ContentInfo::AUTOSELECTED:
968 					this->filesize_sum += ci->filesize;
969 					break;
970 
971 				case ContentInfo::UNSELECTED:
972 					show_select_all = true;
973 					show_select_upgrade |= ci->upgrade;
974 					break;
975 
976 				default:
977 					break;
978 			}
979 		}
980 
981 		/* If data == 2 then the status window caused this OnInvalidate */
982 		this->SetWidgetDisabledState(WID_NCL_DOWNLOAD, this->filesize_sum == 0 || (FindWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD) != nullptr && data != 2));
983 		this->SetWidgetDisabledState(WID_NCL_UNSELECT, this->filesize_sum == 0);
984 		this->SetWidgetDisabledState(WID_NCL_SELECT_ALL, !show_select_all);
985 		this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade);
986 		this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || this->selected->url.empty());
987 		for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
988 			this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE || this->selected->GetTextfile(tft) == nullptr);
989 		}
990 
991 		this->GetWidget<NWidgetCore>(WID_NCL_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;
992 	}
993 };
994 
995 Listing NetworkContentListWindow::last_sorting = {false, 1};
996 Filtering NetworkContentListWindow::last_filtering = {false, 0};
997 
998 NetworkContentListWindow::GUIContentList::SortFunction * const NetworkContentListWindow::sorter_funcs[] = {
999 	&StateSorter,
1000 	&TypeSorter,
1001 	&NameSorter,
1002 };
1003 
1004 NetworkContentListWindow::GUIContentList::FilterFunction * const NetworkContentListWindow::filter_funcs[] = {
1005 	&TagNameFilter,
1006 	&TypeOrSelectedFilter,
1007 };
1008 
1009 char NetworkContentListWindow::content_type_strs[CONTENT_TYPE_END][64];
1010 
1011 /**
1012  * Build array of all strings corresponding to the content types.
1013  */
BuildContentTypeStringList()1014 void BuildContentTypeStringList()
1015 {
1016 	for (int i = CONTENT_TYPE_BEGIN; i < CONTENT_TYPE_END; i++) {
1017 		GetString(NetworkContentListWindow::content_type_strs[i], STR_CONTENT_TYPE_BASE_GRAPHICS + i - CONTENT_TYPE_BASE_GRAPHICS, lastof(NetworkContentListWindow::content_type_strs[i]));
1018 	}
1019 }
1020 
1021 /** The widgets for the content list. */
1022 static const NWidgetPart _nested_network_content_list_widgets[] = {
1023 	NWidget(NWID_HORIZONTAL),
1024 		NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1025 		NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_CONTENT_TITLE, STR_NULL),
1026 		NWidget(WWT_DEFSIZEBOX, COLOUR_LIGHT_BLUE),
1027 	EndContainer(),
1028 	NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NCL_BACKGROUND),
1029 		NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
1030 		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
1031 			/* Top */
1032 			NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_NCL_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0),
1033 			NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NCL_FILTER), SetFill(1, 0), SetResize(1, 0),
1034 						SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1035 		EndContainer(),
1036 		NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
1037 		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
1038 			/* Left side. */
1039 			NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
1040 				NWidget(NWID_HORIZONTAL),
1041 					NWidget(NWID_VERTICAL),
1042 						NWidget(NWID_HORIZONTAL),
1043 							NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_CHECKBOX), SetMinimalSize(13, 1), SetDataTip(STR_EMPTY, STR_NULL),
1044 							NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TYPE),
1045 											SetDataTip(STR_CONTENT_TYPE_CAPTION, STR_CONTENT_TYPE_CAPTION_TOOLTIP),
1046 							NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_NAME), SetResize(1, 0), SetFill(1, 0),
1047 											SetDataTip(STR_CONTENT_NAME_CAPTION, STR_CONTENT_NAME_CAPTION_TOOLTIP),
1048 						EndContainer(),
1049 						NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NCL_MATRIX), SetResize(1, 14), SetFill(1, 1), SetScrollbar(WID_NCL_SCROLLBAR), SetMatrixDataTip(1, 0, STR_CONTENT_MATRIX_TOOLTIP),
1050 					EndContainer(),
1051 					NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NCL_SCROLLBAR),
1052 				EndContainer(),
1053 				NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, 8, 0),
1054 					NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NCL_SEL_ALL_UPDATE), SetResize(1, 0), SetFill(1, 0),
1055 						NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SELECT_UPDATE), SetResize(1, 0), SetFill(1, 0),
1056 												SetDataTip(STR_CONTENT_SELECT_UPDATES_CAPTION, STR_CONTENT_SELECT_UPDATES_CAPTION_TOOLTIP),
1057 						NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SELECT_ALL), SetResize(1, 0), SetFill(1, 0),
1058 												SetDataTip(STR_CONTENT_SELECT_ALL_CAPTION, STR_CONTENT_SELECT_ALL_CAPTION_TOOLTIP),
1059 					EndContainer(),
1060 					NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_UNSELECT), SetResize(1, 0), SetFill(1, 0),
1061 												SetDataTip(STR_CONTENT_UNSELECT_ALL_CAPTION, STR_CONTENT_UNSELECT_ALL_CAPTION_TOOLTIP),
1062 				EndContainer(),
1063 			EndContainer(),
1064 			/* Right side. */
1065 			NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
1066 				NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NCL_DETAILS), SetResize(1, 1), SetFill(1, 1), EndContainer(),
1067 				NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, 8, 0),
1068 					NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1069 					NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1070 				EndContainer(),
1071 				NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, 8, 0),
1072 					NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_OPEN_URL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1073 					NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1074 				EndContainer(),
1075 			EndContainer(),
1076 		EndContainer(),
1077 		NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
1078 		/* Bottom. */
1079 		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
1080 			NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SEARCH_EXTERNAL), SetResize(1, 0), SetFill(1, 0),
1081 										SetDataTip(STR_CONTENT_SEARCH_EXTERNAL, STR_CONTENT_SEARCH_EXTERNAL_TOOLTIP),
1082 			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, 8, 0),
1083 				NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_CANCEL), SetResize(1, 0), SetFill(1, 0),
1084 											SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1085 				NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
1086 											SetDataTip(STR_CONTENT_DOWNLOAD_CAPTION, STR_CONTENT_DOWNLOAD_CAPTION_TOOLTIP),
1087 			EndContainer(),
1088 		EndContainer(),
1089 		NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetResize(1, 0),
1090 		/* Resize button. */
1091 		NWidget(NWID_HORIZONTAL),
1092 			NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1093 			NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
1094 		EndContainer(),
1095 	EndContainer(),
1096 };
1097 
1098 /** Window description of the content list */
1099 static WindowDesc _network_content_list_desc(
1100 	WDP_CENTER, "list_content", 630, 460,
1101 	WC_NETWORK_WINDOW, WC_NONE,
1102 	0,
1103 	_nested_network_content_list_widgets, lengthof(_nested_network_content_list_widgets)
1104 );
1105 
1106 /**
1107  * Show the content list window with a given set of content
1108  * @param cv the content to show, or nullptr when it has to search for itself
1109  * @param type1 the first type to (only) show or #CONTENT_TYPE_END to show all.
1110  * @param type2 the second type to (only) show in addition to type1. If type2 is != #CONTENT_TYPE_END, then also type1 should be != #CONTENT_TYPE_END.
1111  *   If type2 != #CONTENT_TYPE_END, then type1 != type2 must be true.
1112  */
ShowNetworkContentListWindow(ContentVector * cv,ContentType type1,ContentType type2)1113 void ShowNetworkContentListWindow(ContentVector *cv, ContentType type1, ContentType type2)
1114 {
1115 #if defined(WITH_ZLIB)
1116 	std::bitset<CONTENT_TYPE_END> types;
1117 	_network_content_client.Clear();
1118 	if (cv == nullptr) {
1119 		assert(type1 != CONTENT_TYPE_END || type2 == CONTENT_TYPE_END);
1120 		assert(type1 == CONTENT_TYPE_END || type1 != type2);
1121 		_network_content_client.RequestContentList(type1);
1122 		if (type2 != CONTENT_TYPE_END) _network_content_client.RequestContentList(type2);
1123 
1124 		if (type1 != CONTENT_TYPE_END) types[type1] = true;
1125 		if (type2 != CONTENT_TYPE_END) types[type2] = true;
1126 	} else {
1127 		_network_content_client.RequestContentList(cv, true);
1128 	}
1129 
1130 	CloseWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_CONTENT_LIST);
1131 	new NetworkContentListWindow(&_network_content_list_desc, cv != nullptr, types);
1132 #else
1133 	ShowErrorMessage(STR_CONTENT_NO_ZLIB, STR_CONTENT_NO_ZLIB_SUB, WL_ERROR);
1134 	/* Connection failed... clean up the mess */
1135 	if (cv != nullptr) {
1136 		for (ContentInfo *ci : *cv) delete ci;
1137 	}
1138 #endif /* WITH_ZLIB */
1139 }
1140