# network_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: network_gui.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifdef ENABLE_NETWORK
    00013 #include "../stdafx.h"
    00014 #include "../strings_func.h"
    00015 #include "../date_func.h"
    00016 #include "../fios.h"
    00017 #include "network_client.h"
    00018 #include "network_gui.h"
    00019 #include "network_gamelist.h"
    00020 #include "network.h"
    00021 #include "network_base.h"
    00022 #include "../gui.h"
    00023 #include "network_udp.h"
    00024 #include "../window_func.h"
    00025 #include "../gfx_func.h"
    00026 #include "../widgets/dropdown_func.h"
    00027 #include "../querystring_gui.h"
    00028 #include "../sortlist_type.h"
    00029 #include "../company_base.h"
    00030 #include "../company_func.h"
    00031 #include "../core/geometry_func.hpp"
    00032
    00033 #include "table/strings.h"
    00034 #include "../table/sprites.h"
    00035
    00036
    00037 static void ShowNetworkStartServerWindow();
    00038 static void ShowNetworkLobbyWindow(NetworkGameList *ngl);
    00039 extern void SwitchToMode(SwitchMode new_mode);
    00040
    00041 static const StringID _connection_types_dropdown[] = {
    00042   STR_NETWORK_START_SERVER_LAN_INTERNET,
    00043   STR_NETWORK_START_SERVER_INTERNET_ADVERTISE,
    00044   INVALID_STRING_ID
    00045 };
    00046
    00047 static const StringID _lan_internet_types_dropdown[] = {
    00048   STR_NETWORK_SERVER_LIST_LAN,
    00049   STR_NETWORK_SERVER_LIST_INTERNET,
    00050   INVALID_STRING_ID
    00051 };
    00052
    00053 static StringID _language_dropdown[NETLANG_COUNT + 1] = {STR_NULL};
    00054
    00055 void SortNetworkLanguages()
    00056 {
    00057   /* Init the strings */
    00058   if (_language_dropdown[0] == STR_NULL) {
    00059     for (int i = 0; i < NETLANG_COUNT; i++) _language_dropdown[i] = STR_NETWORK_LANG_ANY + i;
    00060     _language_dropdown[NETLANG_COUNT] = INVALID_STRING_ID;
    00061   }
    00062
    00063   /* Sort the strings (we don't move 'any' and the 'invalid' one) */
    00064   QSortT(_language_dropdown, NETLANG_COUNT - 1, &StringIDSorter);
    00065 }
    00066
    00070 void UpdateNetworkGameWindow(bool unselect)
    00071 {
    00072   InvalidateWindowData(WC_NETWORK_WINDOW, 0, unselect ? 1 : 0);
    00073 }
    00074
    00076 enum NetworkGameWindowWidgets {
    00077   NGWW_MAIN,
    00078
    00079   NGWW_CONNECTION,
    00080   NGWW_CONN_BTN,
    00081   NGWW_CLIENT_LABEL,
    00082   NGWW_CLIENT,
    00083
    00084   NGWW_HEADER,
    00085   NGWW_NAME,
    00086   NGWW_CLIENTS,
    00087   NGWW_MAPSIZE,
    00088   NGWW_DATE,
    00089   NGWW_YEARS,
    00090   NGWW_INFO,
    00091
    00092   NGWW_MATRIX,
    00093   NGWW_SCROLLBAR,
    00094
    00095   NGWW_LASTJOINED_LABEL,
    00096   NGWW_LASTJOINED,
    00097
    00098   NGWW_DETAILS,
    00099   NGWW_DETAILS_SPACER,
    00100   NGWW_JOIN,
    00101   NGWW_REFRESH,
    00102   NGWW_NEWGRF,
    00103   NGWW_NEWGRF_SEL,
    00104
    00105   NGWW_FIND,
    00106   NGWW_ADD,
    00107   NGWW_START,
    00108   NGWW_CANCEL,
    00109 };
    00110
    00111 typedef GUIList<NetworkGameList*> GUIGameServerList;
    00112 typedef uint16 ServerListPosition;
    00113 static const ServerListPosition SLP_INVALID = 0xFFFF;
    00114
    00116 class NWidgetServerListHeader : public NWidgetContainer {
    00117   static const uint MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER = 150;
    00118   bool visible[6];
    00119 public:
    00120   NWidgetServerListHeader() : NWidgetContainer(NWID_HORIZONTAL)
    00121   {
    00122     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME_TOOLTIP);
    00123     leaf->SetResize(1, 0);
    00124     leaf->SetFill(1, 0);
    00125     this->Add(leaf);
    00126
    00127     this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_CLIENTS, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION_TOOLTIP));
    00128     this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_MAPSIZE, STR_NETWORK_SERVER_LIST_MAP_SIZE_CAPTION, STR_NETWORK_SERVER_LIST_MAP_SIZE_CAPTION_TOOLTIP));
    00129     this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_DATE, STR_NETWORK_SERVER_LIST_DATE_CAPTION, STR_NETWORK_SERVER_LIST_DATE_CAPTION_TOOLTIP));
    00130     this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_YEARS, STR_NETWORK_SERVER_LIST_YEARS_CAPTION, STR_NETWORK_SERVER_LIST_YEARS_CAPTION_TOOLTIP));
    00131
    00132     leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_INFO, STR_EMPTY, STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP);
    00133     leaf->SetMinimalSize(40, 12);
    00134     leaf->SetFill(0, 1);
    00135     this->Add(leaf);
    00136
    00137     /* First and last are always visible, the rest is implicitly zeroed */
    00138     this->visible[0] = true;
    00139     *lastof(this->visible) = true;
    00140   }
    00141
    00142   void SetupSmallestSize(Window *w, bool init_array)
    00143   {
    00144     /* Oh yeah, we ought to be findable! */
    00145     w->nested_array[NGWW_HEADER] = this;
    00146
    00147     this->smallest_x = this->head->smallest_x + this->tail->smallest_x; // First and last are always shown, rest not
    00148     this->smallest_y = 0; // Biggest child.
    00149     this->fill_x = 1;
    00150     this->fill_y = 0;
    00151     this->resize_x = 1; // We only resize in this direction
    00152     this->resize_y = 0; // We never resize in this direction
    00153
    00154     /* First initialise some variables... */
    00155     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00156       child_wid->SetupSmallestSize(w, init_array);
    00157       this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
    00158     }
    00159
    00160     /* ... then in a second pass make sure the 'current' sizes are set. Won't change for most widgets. */
    00161     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00162       child_wid->current_x = child_wid->smallest_x;
    00163       child_wid->current_y = this->smallest_y;
    00164     }
    00165   }
    00166
    00167   void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    00168   {
    00169     assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
    00170
    00171     this->pos_x = x;
    00172     this->pos_y = y;
    00173     this->current_x = given_width;
    00174     this->current_y = given_height;
    00175
    00176     given_width -= this->tail->smallest_x;
    00177     NWidgetBase *child_wid = this->head->next;
    00178     /* The first and last widget are always visible, determine which other should be visible */
    00179     for (uint i = 1; i < lengthof(this->visible) - 1; i++) {
    00180       if (given_width - child_wid->smallest_x > MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER && this->visible[i - 1]) {
    00181         this->visible[i] = true;
    00182         given_width -= child_wid->smallest_x;
    00183       } else {
    00184         this->visible[i] = false;
    00185       }
    00186       child_wid = child_wid->next;
    00187     }
    00188
    00189     /* All remaining space goes to the first (name) widget */
    00190     this->head->current_x = given_width;
    00191
    00192     /* Now assign the widgets to their rightful place */
    00193     uint position = 0; // Place to put next child relative to origin of the container.
    00194     uint i = rtl ? lengthof(this->visible) - 1 : 0;
    00195     child_wid = rtl ? this->tail : this->head;
    00196     while (child_wid != NULL) {
    00197       if (this->visible[i]) {
    00198         child_wid->AssignSizePosition(sizing, x + position, y, child_wid->current_x, this->current_y, rtl);
    00199         position += child_wid->current_x;
    00200       }
    00201
    00202       child_wid = rtl ? child_wid->prev : child_wid->next;
    00203       i += rtl ? -1 : 1;
    00204     }
    00205   }
    00206
    00207   /* virtual */ void Draw(const Window *w)
    00208   {
    00209     int i = 0;
    00210     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00211       if (!this->visible[i++]) continue;
    00212
    00213       child_wid->Draw(w);
    00214     }
    00215   }
    00216
    00217   /* virtual */ NWidgetCore *GetWidgetFromPos(int x, int y)
    00218   {
    00219     if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    00220
    00221     int i = 0;
    00222     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
    00223       if (!this->visible[i++]) continue;
    00224       NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
    00225       if (nwid != NULL) return nwid;
    00226     }
    00227     return NULL;
    00228   }
    00229
    00235   bool IsWidgetVisible(NetworkGameWindowWidgets widget) const
    00236   {
    00237     assert((uint)(widget - NGWW_NAME) < lengthof(this->visible));
    00238     return this->visible[widget - NGWW_NAME];
    00239   }
    00240 };
    00241
    00242 class NetworkGameWindow : public QueryStringBaseWindow {
    00243 protected:
    00244   /* Runtime saved values */
    00245   static Listing last_sorting;
    00246
    00247   /* Constants for sorting servers */
    00248   static GUIGameServerList::SortFunction * const sorter_funcs[];
    00249
    00250   byte field;
    00251   NetworkGameList *server;
    00252   NetworkGameList *last_joined;
    00253   GUIGameServerList servers;
    00254   ServerListPosition list_pos;
    00255
    00260   void BuildNetworkGameList()
    00261   {
    00262     if (!this->servers.NeedRebuild()) return;
    00263
    00264     /* Create temporary array of games to use for listing */
    00265     this->servers.Clear();
    00266
    00267     for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) {
    00268       *this->servers.Append() = ngl;
    00269     }
    00270
    00271     this->servers.Compact();
    00272     this->servers.RebuildDone();
    00273     this->vscroll.SetCount(this->servers.Length());
    00274   }
    00275
    00277   static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b)
    00278   {
    00279     return strcasecmp((*a)->info.server_name, (*b)->info.server_name);
    00280   }
    00281
    00285   static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b)
    00286   {
    00287     /* Reverse as per default we are interested in most-clients first */
    00288     int r = (*a)->info.clients_on - (*b)->info.clients_on;
    00289
    00290     if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max;
    00291     if (r == 0) r = NGameNameSorter(a, b);
    00292
    00293     return r;
    00294   }
    00295
    00297   static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b)
    00298   {
    00299     /* Sort by the area of the map. */
    00300     int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width);
    00301
    00302     if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width;
    00303     return (r != 0) ? r : NGameClientSorter(a, b);
    00304   }
    00305
    00307   static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b)
    00308   {
    00309     int r = (*a)->info.game_date - (*b)->info.game_date;
    00310     return (r != 0) ? r : NGameClientSorter(a, b);
    00311   }
    00312
    00314   static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b)
    00315   {
    00316     int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date;
    00317     return (r != 0) ? r : NGameDateSorter(a, b);
    00318   }
    00319
    00322   static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b)
    00323   {
    00324     /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
    00325     int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision);
    00326
    00327     /* Reverse default as we are interested in version-compatible clients first */
    00328     if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible;
    00329     /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
    00330     if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible;
    00331     /* Passworded servers should be below unpassworded servers */
    00332     if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password;
    00333     /* Finally sort on the name of the server */
    00334     if (r == 0) r = NGameNameSorter(a, b);
    00335
    00336     return r;
    00337   }
    00338
    00340   void SortNetworkGameList()
    00341   {
    00342     if (!this->servers.Sort()) return;
    00343
    00344     /* After sorting ngl->sort_list contains the sorted items. Put these back
    00345      * into the original list. Basically nothing has changed, we are only
    00346      * shuffling the ->next pointers. While iterating, look for the
    00347      * currently selected server and set list_pos to its position */
    00348     this->list_pos = SLP_INVALID;
    00349     _network_game_list = this->servers[0];
    00350     NetworkGameList *item = _network_game_list;
    00351     if (item == this->server) this->list_pos = 0;
    00352     for (uint i = 1; i != this->servers.Length(); i++) {
    00353       item->next = this->servers[i];
    00354       item = item->next;
    00355       if (item == this->server) this->list_pos = i;
    00356     }
    00357     item->next = NULL;
    00358   }
    00359
    00366   void DrawServerLine(const NetworkGameList *cur_item, uint y, bool highlight) const
    00367   {
    00368     const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(NGWW_NAME);
    00369     const NWidgetBase *nwi_info = this->GetWidget<NWidgetBase>(NGWW_INFO);
    00370
    00371     /* show highlighted item with a different colour */
    00372     if (highlight) GfxFillRect(nwi_name->pos_x + 1, y - 2, nwi_info->pos_x + nwi_info->current_x - 2, y + FONT_HEIGHT_NORMAL - 1, 10);
    00373
    00374     DrawString(nwi_name->pos_x + WD_FRAMERECT_LEFT, nwi_name->pos_x + nwi_name->current_x - WD_FRAMERECT_RIGHT, y, cur_item->info.server_name, TC_BLACK);
    00375
    00376     /* only draw details if the server is online */
    00377     if (cur_item->online) {
    00378       const NWidgetServerListHeader *nwi_header = this->GetWidget<NWidgetServerListHeader>(NGWW_HEADER);
    00379
    00380       if (nwi_header->IsWidgetVisible(NGWW_CLIENTS)) {
    00381         const NWidgetBase *nwi_clients = this->GetWidget<NWidgetBase>(NGWW_CLIENTS);
    00382         SetDParam(0, cur_item->info.clients_on);
    00383         SetDParam(1, cur_item->info.clients_max);
    00384         SetDParam(2, cur_item->info.companies_on);
    00385         SetDParam(3, cur_item->info.companies_max);
    00386         DrawString(nwi_clients->pos_x, nwi_clients->pos_x + nwi_clients->current_x - 1, y, STR_NETWORK_SERVER_LIST_GENERAL_ONLINE, TC_FROMSTRING, SA_CENTER);
    00387       }
    00388
    00389       if (nwi_header->IsWidgetVisible(NGWW_MAPSIZE)) {
    00390         /* map size */
    00391         const NWidgetBase *nwi_mapsize = this->GetWidget<NWidgetBase>(NGWW_MAPSIZE);
    00392         SetDParam(0, cur_item->info.map_width);
    00393         SetDParam(1, cur_item->info.map_height);
    00394         DrawString(nwi_mapsize->pos_x, nwi_mapsize->pos_x + nwi_mapsize->current_x - 1, y, STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT, TC_FROMSTRING, SA_CENTER);
    00395       }
    00396
    00397       if (nwi_header->IsWidgetVisible(NGWW_DATE)) {
    00398         /* current date */
    00399         const NWidgetBase *nwi_date = this->GetWidget<NWidgetBase>(NGWW_DATE);
    00400         YearMonthDay ymd;
    00401         ConvertDateToYMD(cur_item->info.game_date, &ymd);
    00402         SetDParam(0, ymd.year);
    00403         DrawString(nwi_date->pos_x, nwi_date->pos_x + nwi_date->current_x - 1, y, STR_JUST_INT, TC_BLACK, SA_CENTER);
    00404       }
    00405
    00406       if (nwi_header->IsWidgetVisible(NGWW_YEARS)) {
    00407         /* number of years the game is running */
    00408         const NWidgetBase *nwi_years = this->GetWidget<NWidgetBase>(NGWW_YEARS);
    00409         YearMonthDay ymd_cur, ymd_start;
    00410         ConvertDateToYMD(cur_item->info.game_date, &ymd_cur);
    00411         ConvertDateToYMD(cur_item->info.start_date, &ymd_start);
    00412         SetDParam(0, ymd_cur.year - ymd_start.year);
    00413         DrawString(nwi_years->pos_x, nwi_years->pos_x + nwi_years->current_x - 1, y, STR_JUST_INT, TC_BLACK, SA_CENTER);
    00414       }
    00415
    00416       /* Align the sprites */
    00417       y += (FONT_HEIGHT_NORMAL - 10) / 2;
    00418
    00419       /* draw a lock if the server is password protected */
    00420       if (cur_item->info.use_password) DrawSprite(SPR_LOCK, PAL_NONE, nwi_info->pos_x + 5, y - 1);
    00421
    00422       /* draw red or green icon, depending on compatibility with server */
    00423       DrawSprite(SPR_BLOT, (cur_item->info.compatible ? PALETTE_TO_GREEN : (cur_item->info.version_compatible ? PALETTE_TO_YELLOW : PALETTE_TO_RED)), nwi_info->pos_x + 15, y);
    00424
    00425       /* draw flag according to server language */
    00426       DrawSprite(SPR_FLAGS_BASE + cur_item->info.server_lang, PAL_NONE, nwi_info->pos_x + 25, y);
    00427     }
    00428   }
    00429
    00437   void ScrollToSelectedServer()
    00438   {
    00439     if (this->list_pos == SLP_INVALID) return; // no server selected
    00440     this->vscroll.ScrollTowards(this->list_pos);
    00441   }
    00442
    00443 public:
    00444   NetworkGameWindow(const WindowDesc *desc) : QueryStringBaseWindow(NETWORK_CLIENT_NAME_LENGTH)
    00445   {
    00446     this->InitNested(desc, 0);
    00447
    00448     ttd_strlcpy(this->edit_str_buf, _settings_client.network.client_name, this->edit_str_size);
    00449     this->afilter = CS_ALPHANUMERAL;
    00450     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 120);
    00451     this->SetFocusedWidget(NGWW_CLIENT);
    00452
    00453     UpdateNetworkGameWindow(true);
    00454
    00455     this->field = NGWW_CLIENT;
    00456     this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
    00457     this->server = this->last_joined;
    00458
    00459     this->servers.SetListing(this->last_sorting);
    00460     this->servers.SetSortFuncs(this->sorter_funcs);
    00461     this->servers.ForceRebuild();
    00462     this->SortNetworkGameList();
    00463   }
    00464
    00465   ~NetworkGameWindow()
    00466   {
    00467     this->last_sorting = this->servers.GetListing();
    00468   }
    00469
    00470   virtual void SetStringParameters(int widget) const
    00471   {
    00472     switch (widget) {
    00473       case NGWW_CONN_BTN:
    00474         SetDParam(0, _lan_internet_types_dropdown[_settings_client.network.lan_internet]);
    00475         break;
    00476     }
    00477   }
    00478
    00479   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00480   {
    00481     switch (widget) {
    00482       case NGWW_CONN_BTN:
    00483         *size = maxdim(GetStringBoundingBox(_lan_internet_types_dropdown[0]), GetStringBoundingBox(_lan_internet_types_dropdown[1]));
    00484         size->width += padding.width;
    00485         size->height += padding.height;
    00486         break;
    00487
    00488       case NGWW_MATRIX:
    00489         resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    00490         size->height = 10 * resize->height;
    00491         break;
    00492
    00493       case NGWW_LASTJOINED:
    00494         size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    00495         break;
    00496
    00497       case NGWW_NAME:
    00498         size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
    00499         break;
    00500
    00501       case NGWW_CLIENTS:
    00502         size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
    00503         SetDParam(0, 255);
    00504         SetDParam(1, 255);
    00505         SetDParam(2, 15);
    00506         SetDParam(3, 15);
    00507         *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_GENERAL_ONLINE));
    00508         break;
    00509
    00510       case NGWW_MAPSIZE:
    00511         size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
    00512         SetDParam(0, 2048);
    00513         SetDParam(1, 2048);
    00514         *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT));
    00515         break;
    00516
    00517       case NGWW_DATE:
    00518       case NGWW_YEARS:
    00519         size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
    00520         SetDParam(0, 99999);
    00521         *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
    00522         break;
    00523
    00524       case NGWW_DETAILS_SPACER:
    00525         size->height = 20 + 12 * FONT_HEIGHT_NORMAL;
    00526         break;
    00527     }
    00528   }
    00529
    00530   virtual void DrawWidget(const Rect &r, int widget) const
    00531   {
    00532     switch (widget) {
    00533       case NGWW_MATRIX: {
    00534         uint16 y = r.top + WD_MATRIX_TOP;
    00535
    00536         const int max = min(this->vscroll.GetPosition() + this->vscroll.GetCapacity(), (int)this->servers.Length());
    00537
    00538         for (int i = this->vscroll.GetPosition(); i < max; ++i) {
    00539           const NetworkGameList *ngl = this->servers[i];
    00540           this->DrawServerLine(ngl, y, ngl == this->server);
    00541           y += this->resize.step_height;
    00542         }
    00543       } break;
    00544
    00545       case NGWW_LASTJOINED:
    00546         /* Draw the last joined server, if any */
    00547         if (this->last_joined != NULL) this->DrawServerLine(this->last_joined, r.top + WD_MATRIX_TOP, this->last_joined == this->server);
    00548         break;
    00549
    00550       case NGWW_DETAILS:
    00551         this->DrawDetails(r);
    00552         break;
    00553
    00554       case NGWW_NAME:
    00555       case NGWW_CLIENTS:
    00556       case NGWW_MAPSIZE:
    00557       case NGWW_DATE:
    00558       case NGWW_YEARS:
    00559       case NGWW_INFO:
    00560         if (widget - NGWW_NAME == this->servers.SortType()) this->DrawSortButtonState(widget, this->servers.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    00561         break;
    00562     }
    00563   }
    00564
    00565
    00566   virtual void OnPaint()
    00567   {
    00568     if (this->servers.NeedRebuild()) {
    00569       this->BuildNetworkGameList();
    00570     }
    00571     this->SortNetworkGameList();
    00572
    00573     NetworkGameList *sel = this->server;
    00574     /* 'Refresh' button invisible if no server selected */
    00575     this->SetWidgetDisabledState(NGWW_REFRESH, sel == NULL);
    00576     /* 'Join' button disabling conditions */
    00577     this->SetWidgetDisabledState(NGWW_JOIN, sel == NULL || // no Selected Server
    00578         !sel->online || // Server offline
    00579         sel->info.clients_on >= sel->info.clients_max || // Server full
    00580         !sel->info.compatible); // Revision mismatch
    00581
    00582     /* 'NewGRF Settings' button invisible if no NewGRF is used */
    00583     this->GetWidget<NWidgetStacked>(NGWW_NEWGRF_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL);
    00584
    00585     this->DrawWidgets();
    00586     /* Edit box to set client name */
    00587     this->DrawEditBox(NGWW_CLIENT);
    00588   }
    00589
    00590   void DrawDetails(const Rect &r) const
    00591   {
    00592     NetworkGameList *sel = this->server;
    00593
    00594     const int detail_height = 6 + 8 + 6 + 3 * FONT_HEIGHT_NORMAL;
    00595
    00596     /* Draw the right menu */
    00597     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, 157);
    00598     if (sel == NULL) {
    00599       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 4 + FONT_HEIGHT_NORMAL, STR_NETWORK_SERVER_LIST_GAME_INFO, TC_FROMSTRING, SA_CENTER);
    00600     } else if (!sel->online) {
    00601       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 4 + FONT_HEIGHT_NORMAL, sel->info.server_name, TC_ORANGE, SA_CENTER); // game name
    00602
    00603       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + detail_height + 4, STR_NETWORK_SERVER_LIST_SERVER_OFFLINE, TC_FROMSTRING, SA_CENTER); // server offline
    00604     } else { // show game info
    00605
    00606       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6, STR_NETWORK_SERVER_LIST_GAME_INFO, TC_FROMSTRING, SA_CENTER);
    00607       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 4 + FONT_HEIGHT_NORMAL, sel->info.server_name, TC_ORANGE, SA_CENTER); // game name
    00608       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 8 + 2 * FONT_HEIGHT_NORMAL, sel->info.map_name, TC_BLACK, SA_CENTER); // map name
    00609
    00610       uint16 y = r.top + detail_height + 4;
    00611
    00612       SetDParam(0, sel->info.clients_on);
    00613       SetDParam(1, sel->info.clients_max);
    00614       SetDParam(2, sel->info.companies_on);
    00615       SetDParam(3, sel->info.companies_max);
    00616       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
    00617       y += FONT_HEIGHT_NORMAL;
    00618
    00619       SetDParam(0, STR_NETWORK_LANG_ANY + sel->info.server_lang);
    00620       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANGUAGE); // server language
    00621       y += FONT_HEIGHT_NORMAL;
    00622
    00623       SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + sel->info.map_set);
    00624       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_TILESET); // tileset
    00625       y += FONT_HEIGHT_NORMAL;
    00626
    00627       SetDParam(0, sel->info.map_width);
    00628       SetDParam(1, sel->info.map_height);
    00629       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE); // map size
    00630       y += FONT_HEIGHT_NORMAL;
    00631
    00632       SetDParamStr(0, sel->info.server_revision);
    00633       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
    00634       y += FONT_HEIGHT_NORMAL;
    00635
    00636       SetDParamStr(0, sel->address.GetAddressAsString());
    00637       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
    00638       y += FONT_HEIGHT_NORMAL;
    00639
    00640       SetDParam(0, sel->info.start_date);
    00641       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE); // start date
    00642       y += FONT_HEIGHT_NORMAL;
    00643
    00644       SetDParam(0, sel->info.game_date);
    00645       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE); // current date
    00646       y += FONT_HEIGHT_NORMAL;
    00647
    00648       y += WD_PAR_VSEP_NORMAL;
    00649
    00650       if (!sel->info.compatible) {
    00651         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, sel->info.version_compatible ? STR_NETWORK_SERVER_LIST_GRF_MISMATCH : STR_NETWORK_SERVER_LIST_VERSION_MISMATCH, TC_FROMSTRING, SA_CENTER); // server mismatch
    00652       } else if (sel->info.clients_on == sel->info.clients_max) {
    00653         /* Show: server full, when clients_on == max_clients */
    00654         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_FULL, TC_FROMSTRING, SA_CENTER); // server full
    00655       } else if (sel->info.use_password) {
    00656         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_PASSWORD, TC_FROMSTRING, SA_CENTER); // password warning
    00657       }
    00658     }
    00659   }
    00660
    00661   virtual void OnClick(Point pt, int widget)
    00662   {
    00663     this->field = widget;
    00664     switch (widget) {
    00665       case NGWW_CANCEL: // Cancel button
    00666         DeleteWindowById(WC_NETWORK_WINDOW, 0);
    00667         break;
    00668
    00669       case NGWW_CONN_BTN: // 'Connection' droplist
    00670         ShowDropDownMenu(this, _lan_internet_types_dropdown, _settings_client.network.lan_internet, NGWW_CONN_BTN, 0, 0); // do it for widget NSSW_CONN_BTN
    00671         break;
    00672
    00673       case NGWW_NAME:    // Sort by name
    00674       case NGWW_CLIENTS: // Sort by connected clients
    00675       case NGWW_MAPSIZE: // Sort by map size
    00676       case NGWW_DATE:    // Sort by date
    00677       case NGWW_YEARS:   // Sort by years
    00678       case NGWW_INFO:    // Connectivity (green dot)
    00679         if (this->servers.SortType() == widget - NGWW_NAME) {
    00680           this->servers.ToggleSortOrder();
    00681           if (this->list_pos != SLP_INVALID) this->list_pos = this->servers.Length() - this->list_pos - 1;
    00682         } else {
    00683           this->servers.SetSortType(widget - NGWW_NAME);
    00684           this->servers.ForceResort();
    00685           this->SortNetworkGameList();
    00686         }
    00687         this->ScrollToSelectedServer();
    00688         this->SetDirty();
    00689         break;
    00690
    00691       case NGWW_MATRIX: { // Matrix to show networkgames
    00692         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(NGWW_MATRIX)->pos_y) / this->resize.step_height;
    00693
    00694         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
    00695         id_v += this->vscroll.GetPosition();
    00696
    00697         this->server = (id_v < this->servers.Length()) ? this->servers[id_v] : NULL;
    00698         this->list_pos = (server == NULL) ? SLP_INVALID : id_v;
    00699         this->SetDirty();
    00700       } break;
    00701
    00702       case NGWW_LASTJOINED: {
    00703         NetworkGameList *last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
    00704         if (last_joined != NULL) {
    00705           this->server = last_joined;
    00706
    00707           /* search the position of the newly selected server */
    00708           for (uint i = 0; i < this->servers.Length(); i++) {
    00709             if (this->servers[i] == this->server) {
    00710               this->list_pos = i;
    00711               break;
    00712             }
    00713           }
    00714           this->ScrollToSelectedServer();
    00715           this->SetDirty();
    00716         }
    00717       } break;
    00718
    00719       case NGWW_FIND: // Find server automatically
    00720         switch (_settings_client.network.lan_internet) {
    00721           case 0: NetworkUDPSearchGame(); break;
    00722           case 1: NetworkUDPQueryMasterServer(); break;
    00723         }
    00724         break;
    00725
    00726       case NGWW_ADD: // Add a server
    00727         SetDParamStr(0, _settings_client.network.connect_to_ip);
    00728         ShowQueryString(
    00729           STR_JUST_RAW_STRING,
    00730           STR_NETWORK_SERVER_LIST_ENTER_IP,
    00731           NETWORK_HOSTNAME_LENGTH,  // maximum number of characters including '\0'
    00732           0,                        // no limit in pixels
    00733           this, CS_ALPHANUMERAL, QSF_ACCEPT_UNCHANGED);
    00734         break;
    00735
    00736       case NGWW_START: // Start server
    00737         ShowNetworkStartServerWindow();
    00738         break;
    00739
    00740       case NGWW_JOIN: // Join Game
    00741         if (this->server != NULL) {
    00742           snprintf(_settings_client.network.last_host, sizeof(_settings_client.network.last_host), "%s", this->server->address.GetHostname());
    00743           _settings_client.network.last_port = this->server->address.GetPort();
    00744           ShowNetworkLobbyWindow(this->server);
    00745         }
    00746         break;
    00747
    00748       case NGWW_REFRESH: // Refresh
    00749         if (this->server != NULL) NetworkUDPQueryServer(this->server->address);
    00750         break;
    00751
    00752       case NGWW_NEWGRF: // NewGRF Settings
    00753         if (this->server != NULL) ShowNewGRFSettings(false, false, false, &this->server->info.grfconfig);
    00754         break;
    00755     }
    00756   }
    00757
    00758   virtual void OnDoubleClick(Point pt, int widget)
    00759   {
    00760     if (widget == NGWW_MATRIX || widget == NGWW_LASTJOINED) {
    00761       /* is the Join button enabled? */
    00762       if (!this->IsWidgetDisabled(NGWW_JOIN)) this->OnClick(pt, NGWW_JOIN);
    00763     }
    00764   }
    00765
    00766   virtual void OnDropdownSelect(int widget, int index)
    00767   {
    00768     switch (widget) {
    00769       case NGWW_CONN_BTN:
    00770         _settings_client.network.lan_internet = index;
    00771         break;
    00772
    00773       default:
    00774         NOT_REACHED();
    00775     }
    00776
    00777     this->SetDirty();
    00778   }
    00779
    00780   virtual void OnMouseLoop()
    00781   {
    00782     if (this->field == NGWW_CLIENT) this->HandleEditBox(NGWW_CLIENT);
    00783   }
    00784
    00785   virtual void OnInvalidateData(int data)
    00786   {
    00787     switch (data) {
    00788       /* Remove the selection */
    00789       case 1:
    00790         this->server = NULL;
    00791         this->list_pos = SLP_INVALID;
    00792         break;
    00793
    00794       /* Reiterate the whole server list as we downloaded some files */
    00795       case 2:
    00796         for (NetworkGameList **iter = this->servers.Begin(); iter != this->servers.End(); iter++) {
    00797           NetworkGameList *item = *iter;
    00798           bool missing_grfs = false;
    00799           for (GRFConfig *c = item->info.grfconfig; c != NULL; c = c->next) {
    00800             if (c->status != GCS_NOT_FOUND) continue;
    00801
    00802             const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
    00803             if (f == NULL) {
    00804               missing_grfs = true;
    00805               continue;
    00806             }
    00807
    00808             c->filename  = f->filename;
    00809             c->name      = f->name;
    00810             c->info      = f->info;
    00811             c->status    = GCS_UNKNOWN;
    00812           }
    00813
    00814           if (!missing_grfs) item->info.compatible = item->info.version_compatible;
    00815         }
    00816         break;
    00817     }
    00818     this->servers.ForceRebuild();
    00819     this->SetDirty();
    00820   }
    00821
    00822   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00823   {
    00824     EventState state = ES_NOT_HANDLED;
    00825
    00826     /* handle up, down, pageup, pagedown, home and end */
    00827     if (keycode == WKC_UP || keycode == WKC_DOWN || keycode == WKC_PAGEUP || keycode == WKC_PAGEDOWN || keycode == WKC_HOME || keycode == WKC_END) {
    00828       if (this->servers.Length() == 0) return ES_HANDLED;
    00829       switch (keycode) {
    00830         case WKC_UP:
    00831           /* scroll up by one */
    00832           if (this->server == NULL) return ES_HANDLED;
    00833           if (this->list_pos > 0) this->list_pos--;
    00834           break;
    00835         case WKC_DOWN:
    00836           /* scroll down by one */
    00837           if (this->server == NULL) return ES_HANDLED;
    00838           if (this->list_pos < this->servers.Length() - 1) this->list_pos++;
    00839           break;
    00840         case WKC_PAGEUP:
    00841           /* scroll up a page */
    00842           if (this->server == NULL) return ES_HANDLED;
    00843           this->list_pos = (this->list_pos < this->vscroll.GetCapacity()) ? 0 : this->list_pos - this->vscroll.GetCapacity();
    00844           break;
    00845         case WKC_PAGEDOWN:
    00846           /* scroll down a page */
    00847           if (this->server == NULL) return ES_HANDLED;
    00848           this->list_pos = min(this->list_pos + this->vscroll.GetCapacity(), (int)this->servers.Length() - 1);
    00849           break;
    00850         case WKC_HOME:
    00851           /* jump to beginning */
    00852           this->list_pos = 0;
    00853           break;
    00854         case WKC_END:
    00855           /* jump to end */
    00856           this->list_pos = this->servers.Length() - 1;
    00857           break;
    00858         default: break;
    00859       }
    00860
    00861       this->server = this->servers[this->list_pos];
    00862
    00863       /* scroll to the new server if it is outside the current range */
    00864       this->ScrollToSelectedServer();
    00865
    00866       /* redraw window */
    00867       this->SetDirty();
    00868       return ES_HANDLED;
    00869     }
    00870
    00871     if (this->field != NGWW_CLIENT) {
    00872       if (this->server != NULL) {
    00873         if (keycode == WKC_DELETE) { // Press 'delete' to remove servers
    00874           NetworkGameListRemoveItem(this->server);
    00875           this->server = NULL;
    00876           this->list_pos = SLP_INVALID;
    00877         }
    00878       }
    00879       return state;
    00880     }
    00881
    00882     if (this->HandleEditBoxKey(NGWW_CLIENT, key, keycode, state) == HEBR_CONFIRM) return state;
    00883
    00884     /* The name is only allowed when it starts with a letter! */
    00885     if (!StrEmpty(this->edit_str_buf) && this->edit_str_buf[0] != ' ') {
    00886       strecpy(_settings_client.network.client_name, this->edit_str_buf, lastof(_settings_client.network.client_name));
    00887     } else {
    00888       strecpy(_settings_client.network.client_name, "Player", lastof(_settings_client.network.client_name));
    00889     }
    00890     return state;
    00891   }
    00892
    00893   virtual void OnQueryTextFinished(char *str)
    00894   {
    00895     if (!StrEmpty(str)) NetworkAddServer(str);
    00896   }
    00897
    00898   virtual void OnResize()
    00899   {
    00900     this->vscroll.SetCapacityFromWidget(this, NGWW_MATRIX);
    00901     this->GetWidget<NWidgetCore>(NGWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00902   }
    00903 };
    00904
    00905 Listing NetworkGameWindow::last_sorting = {false, 5};
    00906 GUIGameServerList::SortFunction * const NetworkGameWindow::sorter_funcs[] = {
    00907   &NGameNameSorter,
    00908   &NGameClientSorter,
    00909   &NGameMapSizeSorter,
    00910   &NGameDateSorter,
    00911   &NGameYearsSorter,
    00912   &NGameAllowedSorter
    00913 };
    00914
    00915 static NWidgetBase *MakeResizableHeader(int *biggest_index)
    00916 {
    00917   *biggest_index = max<int>(*biggest_index, NGWW_INFO);
    00918   return new NWidgetServerListHeader();
    00919 }
    00920
    00921 static const NWidgetPart _nested_network_game_widgets[] = {
    00922   /* TOP */
    00923   NWidget(NWID_HORIZONTAL),
    00924     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
    00925     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_SERVER_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00926   EndContainer(),
    00927   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NGWW_MAIN),
    00928     NWidget(NWID_VERTICAL), SetPIP(10, 7, 0),
    00929       NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
    00930         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NGWW_CONNECTION), SetDataTip(STR_NETWORK_SERVER_LIST_CONNECTION, STR_NULL),
    00931         NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, NGWW_CONN_BTN),
    00932                     SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_CONNECTION_TOOLTIP),
    00933         NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
    00934         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NGWW_CLIENT_LABEL), SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME, STR_NULL),
    00935         NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, NGWW_CLIENT), SetMinimalSize(151, 12),
    00936                     SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE, STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP),
    00937       EndContainer(),
    00938       NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
    00939         /* LEFT SIDE */
    00940         NWidget(NWID_VERTICAL),
    00941           NWidget(NWID_HORIZONTAL),
    00942             NWidget(NWID_VERTICAL),
    00943               NWidgetFunction(MakeResizableHeader),
    00944               NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, NGWW_MATRIX), SetResize(1, 1), SetFill(1, 0),
    00945                         SetDataTip(0, STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT),
    00946             EndContainer(),
    00947             NWidget(WWT_SCROLLBAR, COLOUR_LIGHT_BLUE, NGWW_SCROLLBAR),
    00948           EndContainer(),
    00949           NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0), SetFill(1, 1),
    00950           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NGWW_LASTJOINED_LABEL), SetFill(1, 0),
    00951                     SetDataTip(STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER, STR_NULL), SetResize(1, 0),
    00952           NWidget(NWID_HORIZONTAL), SetPIP(0, 0, WD_VSCROLLBAR_WIDTH),
    00953             NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NGWW_LASTJOINED), SetFill(1, 0), SetResize(1, 0),
    00954                       SetDataTip(0x0, STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST),
    00955             EndContainer(),
    00956           EndContainer(),
    00957         EndContainer(),
    00958         /* RIGHT SIDE */
    00959         NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NGWW_DETAILS),
    00960           NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
    00961             NWidget(WWT_EMPTY, INVALID_COLOUR, NGWW_DETAILS_SPACER), SetMinimalSize(140, 155), SetResize(0, 1), SetFill(1, 1), // Make sure it's at least this wide
    00962             NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
    00963               NWidget(NWID_SPACER), SetFill(1, 0),
    00964               NWidget(NWID_SELECTION, INVALID_COLOUR, NGWW_NEWGRF_SEL),
    00965                 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_NEWGRF), SetFill(1, 0), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL),
    00966                 NWidget(NWID_SPACER), SetFill(1, 0),
    00967               EndContainer(),
    00968             EndContainer(),
    00969             NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
    00970               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_JOIN), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_JOIN_GAME, STR_NULL),
    00971               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_REFRESH), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
    00972             EndContainer(),
    00973           EndContainer(),
    00974         EndContainer(),
    00975       EndContainer(),
    00976       /* BOTTOM */
    00977       NWidget(NWID_HORIZONTAL),
    00978         NWidget(NWID_VERTICAL),
    00979           NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 7, 4),
    00980             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_FIND), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_FIND_SERVER, STR_NETWORK_SERVER_LIST_FIND_SERVER_TOOLTIP),
    00981             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_ADD), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_ADD_SERVER, STR_NETWORK_SERVER_LIST_ADD_SERVER_TOOLTIP),
    00982             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_START), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_START_SERVER, STR_NETWORK_SERVER_LIST_START_SERVER_TOOLTIP),
    00983             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    00984           EndContainer(),
    00985           NWidget(NWID_SPACER), SetMinimalSize(0, 6), SetResize(1, 0), SetFill(1, 0),
    00986         EndContainer(),
    00987         NWidget(NWID_VERTICAL),
    00988           NWidget(NWID_SPACER), SetFill(0, 1),
    00989           NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
    00990         EndContainer(),
    00991       EndContainer(),
    00992     EndContainer(),
    00993   EndContainer(),
    00994 };
    00995
    00996 static const WindowDesc _network_game_window_desc(
    00997   WDP_CENTER, 1000, 730,
    00998   WC_NETWORK_WINDOW, WC_NONE,
    00999   WDF_UNCLICK_BUTTONS,
    01000   _nested_network_game_widgets, lengthof(_nested_network_game_widgets)
    01001 );
    01002
    01003 void ShowNetworkGameWindow()
    01004 {
    01005   static bool first = true;
    01006   DeleteWindowById(WC_NETWORK_WINDOW, 0);
    01007
    01008   /* Only show once */
    01009   if (first) {
    01010     first = false;
    01011     /* add all servers from the config file to our list */
    01012     for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
    01013       NetworkAddServer(*iter);
    01014     }
    01015   }
    01016
    01017   new NetworkGameWindow(&_network_game_window_desc);
    01018 }
    01019
    01021 enum NetworkStartServerWidgets {
    01022   NSSW_BACKGROUND,
    01023   NSSW_GAMENAME_LABEL,
    01024   NSSW_GAMENAME,
    01025   NSSW_SETPWD,
    01026   NSSW_SELECT_MAP_LABEL,
    01027   NSSW_SELMAP,
    01028   NSSW_SCROLLBAR,
    01029   NSSW_CONNTYPE_LABEL,
    01030   NSSW_CONNTYPE_BTN,
    01031   NSSW_CLIENTS_LABEL,
    01032   NSSW_CLIENTS_BTND,
    01033   NSSW_CLIENTS_TXT,
    01034   NSSW_CLIENTS_BTNU,
    01035   NSSW_COMPANIES_LABEL,
    01036   NSSW_COMPANIES_BTND,
    01037   NSSW_COMPANIES_TXT,
    01038   NSSW_COMPANIES_BTNU,
    01039   NSSW_SPECTATORS_LABEL,
    01040   NSSW_SPECTATORS_BTND,
    01041   NSSW_SPECTATORS_TXT,
    01042   NSSW_SPECTATORS_BTNU,
    01043
    01044   NSSW_LANGUAGE_LABEL,
    01045   NSSW_LANGUAGE_BTN,
    01046   NSSW_START,
    01047   NSSW_LOAD,
    01048   NSSW_CANCEL,
    01049 };
    01050
    01051 struct NetworkStartServerWindow : public QueryStringBaseWindow {
    01052   byte field;
    01053   FiosItem *map;
    01054   byte widget_id;
    01055
    01056   NetworkStartServerWindow(const WindowDesc *desc) : QueryStringBaseWindow(NETWORK_NAME_LENGTH)
    01057   {
    01058     this->InitNested(desc, 0);
    01059
    01060     ttd_strlcpy(this->edit_str_buf, _settings_client.network.server_name, this->edit_str_size);
    01061
    01062     _saveload_mode = SLD_NEW_GAME;
    01063     BuildFileList();
    01064     this->vscroll.SetCapacity(14);
    01065     this->vscroll.SetCount(_fios_items.Length() + 1);
    01066
    01067     this->afilter = CS_ALPHANUMERAL;
    01068     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 160);
    01069     this->SetFocusedWidget(NSSW_GAMENAME);
    01070
    01071     this->field = NSSW_GAMENAME;
    01072   }
    01073
    01074   virtual void SetStringParameters(int widget) const
    01075   {
    01076     switch (widget) {
    01077       case NSSW_CONNTYPE_BTN:
    01078         SetDParam(0, _connection_types_dropdown[_settings_client.network.server_advertise]);
    01079         break;
    01080
    01081       case NSSW_CLIENTS_TXT:
    01082         SetDParam(0, _settings_client.network.max_clients);
    01083         break;
    01084
    01085       case NSSW_COMPANIES_TXT:
    01086         SetDParam(0, _settings_client.network.max_companies);
    01087         break;
    01088
    01089       case NSSW_SPECTATORS_TXT:
    01090         SetDParam(0, _settings_client.network.max_spectators);
    01091         break;
    01092
    01093       case NSSW_LANGUAGE_BTN:
    01094         SetDParam(0, STR_NETWORK_LANG_ANY + _settings_client.network.server_lang);
    01095         break;
    01096     }
    01097   }
    01098
    01099   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01100   {
    01101     switch (widget) {
    01102       case NSSW_CONNTYPE_BTN:
    01103         *size = maxdim(GetStringBoundingBox(_connection_types_dropdown[0]), GetStringBoundingBox(_connection_types_dropdown[1]));
    01104         size->width += padding.width;
    01105         size->height += padding.height;
    01106         break;
    01107
    01108       case NSSW_SELMAP:
    01109         resize->height = FONT_HEIGHT_NORMAL;
    01110         size->height = 14 * resize->height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01111         break;
    01112     }
    01113   }
    01114
    01115   virtual void DrawWidget(const Rect &r, int widget) const
    01116   {
    01117     switch (widget) {
    01118       case NSSW_SELMAP:
    01119         this->DrawMapSelection(r);
    01120         break;
    01121
    01122       case NSSW_SETPWD:
    01123         /* if password is set, draw red '*' next to 'Set password' button */
    01124         if (!StrEmpty(_settings_client.network.server_password)) DrawString(r.right + WD_FRAMERECT_LEFT, this->width - WD_FRAMERECT_RIGHT, r.top, "*", TC_RED);
    01125     }
    01126   }
    01127
    01128   virtual void OnPaint()
    01129   {
    01130     /* draw basic widgets */
    01131     this->DrawWidgets();
    01132
    01133     /* editbox to set game name */
    01134     this->DrawEditBox(NSSW_GAMENAME);
    01135   }
    01136
    01137   void DrawMapSelection(const Rect &r) const
    01138   {
    01139     int y = r.top + WD_FRAMERECT_TOP;
    01140     /* draw list of maps */
    01141     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0xD7);  // black background of maps list
    01142
    01143     for (uint pos = this->vscroll.GetPosition(); pos < _fios_items.Length() + 1; pos++) {
    01144       const FiosItem *item = (pos == 0) ? NULL : _fios_items.Get(pos - 1);
    01145       if (item == this->map) { // this->map == NULL for first item
    01146         GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, 155); // show highlighted item with a different colour
    01147       }
    01148
    01149       if (pos == 0) {
    01150         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_START_SERVER_SERVER_RANDOM_GAME, TC_DARK_GREEN);
    01151       } else {
    01152         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[item->type] );
    01153       }
    01154       y += FONT_HEIGHT_NORMAL;
    01155
    01156       if (y >= this->vscroll.GetCapacity() * FONT_HEIGHT_NORMAL + r.top) break;
    01157     }
    01158   }
    01159
    01160   virtual void OnClick(Point pt, int widget)
    01161   {
    01162     this->field = widget;
    01163     switch (widget) {
    01164       case NSSW_CANCEL: // Cancel button
    01165         ShowNetworkGameWindow();
    01166         break;
    01167
    01168       case NSSW_SETPWD: // Set password button
    01169         this->widget_id = NSSW_SETPWD;
    01170         SetDParamStr(0, _settings_client.network.server_password);
    01171         ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_START_SERVER_SET_PASSWORD, 20, 250, this, CS_ALPHANUMERAL, QSF_NONE);
    01172         break;
    01173
    01174       case NSSW_SELMAP: { // Select map
    01175         int y = (pt.y - this->GetWidget<NWidgetBase>(NSSW_SELMAP)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
    01176
    01177         y += this->vscroll.GetPosition();
    01178         if (y >= this->vscroll.GetCount()) return;
    01179
    01180         this->map = (y == 0) ? NULL : _fios_items.Get(y - 1);
    01181         this->SetDirty();
    01182       } break;
    01183
    01184       case NSSW_CONNTYPE_BTN: // Connection type
    01185         ShowDropDownMenu(this, _connection_types_dropdown, _settings_client.network.server_advertise, NSSW_CONNTYPE_BTN, 0, 0); // do it for widget NSSW_CONNTYPE_BTN
    01186         break;
    01187
    01188       case NSSW_CLIENTS_BTND:    case NSSW_CLIENTS_BTNU:    // Click on up/down button for number of clients
    01189       case NSSW_COMPANIES_BTND:  case NSSW_COMPANIES_BTNU:  // Click on up/down button for number of companies
    01190       case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU: // Click on up/down button for number of spectators
    01191         /* Don't allow too fast scrolling */
    01192         if ((this->flags4 & WF_TIMEOUT_MASK) <= WF_TIMEOUT_TRIGGER) {
    01193           this->HandleButtonClick(widget);
    01194           this->SetDirty();
    01195           switch (widget) {
    01196             default: NOT_REACHED();
    01197             case NSSW_CLIENTS_BTND: case NSSW_CLIENTS_BTNU:
    01198               _settings_client.network.max_clients    = Clamp(_settings_client.network.max_clients    + widget - NSSW_CLIENTS_TXT,    2, MAX_CLIENTS);
    01199               break;
    01200             case NSSW_COMPANIES_BTND: case NSSW_COMPANIES_BTNU:
    01201               _settings_client.network.max_companies  = Clamp(_settings_client.network.max_companies  + widget - NSSW_COMPANIES_TXT,  1, MAX_COMPANIES);
    01202               break;
    01203             case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU:
    01204               _settings_client.network.max_spectators = Clamp(_settings_client.network.max_spectators + widget - NSSW_SPECTATORS_TXT, 0, MAX_CLIENTS);
    01205               break;
    01206           }
    01207         }
    01208         _left_button_clicked = false;
    01209         break;
    01210
    01211       case NSSW_CLIENTS_TXT:    // Click on number of clients
    01212         this->widget_id = NSSW_CLIENTS_TXT;
    01213         SetDParam(0, _settings_client.network.max_clients);
    01214         ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS,    4, 50, this, CS_NUMERAL, QSF_NONE);
    01215         break;
    01216
    01217       case NSSW_COMPANIES_TXT:  // Click on number of companies
    01218         this->widget_id = NSSW_COMPANIES_TXT;
    01219         SetDParam(0, _settings_client.network.max_companies);
    01220         ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES,  3, 50, this, CS_NUMERAL, QSF_NONE);
    01221         break;
    01222
    01223       case NSSW_SPECTATORS_TXT: // Click on number of spectators
    01224         this->widget_id = NSSW_SPECTATORS_TXT;
    01225         SetDParam(0, _settings_client.network.max_spectators);
    01226         ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, 4, 50, this, CS_NUMERAL, QSF_NONE);
    01227         break;
    01228
    01229       case NSSW_LANGUAGE_BTN: { // Language
    01230         uint sel = 0;
    01231         for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
    01232           if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _settings_client.network.server_lang) {
    01233             sel = i;
    01234             break;
    01235           }
    01236         }
    01237         ShowDropDownMenu(this, _language_dropdown, sel, NSSW_LANGUAGE_BTN, 0, 0);
    01238       } break;
    01239
    01240       case NSSW_START: // Start game
    01241         _is_network_server = true;
    01242
    01243         if (this->map == NULL) { // start random new game
    01244           ShowGenerateLandscape();
    01245         } else { // load a scenario
    01246           const char *name = FiosBrowseTo(this->map);
    01247           if (name != NULL) {
    01248             SetFiosType(this->map->type);
    01249             _file_to_saveload.filetype = FT_SCENARIO;
    01250             strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    01251             strecpy(_file_to_saveload.title, this->map->title, lastof(_file_to_saveload.title));
    01252
    01253             delete this;
    01254             SwitchToMode(SM_START_SCENARIO);
    01255           }
    01256         }
    01257         break;
    01258
    01259       case NSSW_LOAD: // Load game
    01260         _is_network_server = true;
    01261         /* XXX - WC_NETWORK_WINDOW (this window) should stay, but if it stays, it gets
    01262          * copied all the elements of 'load game' and upon closing that, it segfaults */
    01263         delete this;
    01264         ShowSaveLoadDialog(SLD_LOAD_GAME);
    01265         break;
    01266     }
    01267   }
    01268
    01269   virtual void OnDropdownSelect(int widget, int index)
    01270   {
    01271     switch (widget) {
    01272       case NSSW_CONNTYPE_BTN:
    01273         _settings_client.network.server_advertise = (index != 0);
    01274         break;
    01275       case NSSW_LANGUAGE_BTN:
    01276         _settings_client.network.server_lang = _language_dropdown[index] - STR_NETWORK_LANG_ANY;
    01277         break;
    01278       default:
    01279         NOT_REACHED();
    01280     }
    01281
    01282     this->SetDirty();
    01283   }
    01284
    01285   virtual void OnMouseLoop()
    01286   {
    01287     if (this->field == NSSW_GAMENAME) this->HandleEditBox(NSSW_GAMENAME);
    01288   }
    01289
    01290   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01291   {
    01292     EventState state = ES_NOT_HANDLED;
    01293     if (this->field == NSSW_GAMENAME) {
    01294       if (this->HandleEditBoxKey(NSSW_GAMENAME, key, keycode, state) == HEBR_CONFIRM) return state;
    01295
    01296       strecpy(_settings_client.network.server_name, this->text.buf, lastof(_settings_client.network.server_name));
    01297     }
    01298
    01299     return state;
    01300   }
    01301
    01302   virtual void OnTimeout()
    01303   {
    01304     static const int raise_widgets[] = {NSSW_CLIENTS_BTND, NSSW_CLIENTS_BTNU, NSSW_COMPANIES_BTND, NSSW_COMPANIES_BTNU, NSSW_SPECTATORS_BTND, NSSW_SPECTATORS_BTNU, WIDGET_LIST_END};
    01305     for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
    01306       if (this->IsWidgetLowered(*widget)) {
    01307         this->RaiseWidget(*widget);
    01308         this->SetWidgetDirty(*widget);
    01309       }
    01310     }
    01311   }
    01312
    01313   virtual void OnQueryTextFinished(char *str)
    01314   {
    01315     if (str == NULL) return;
    01316
    01317     if (this->widget_id == NSSW_SETPWD) {
    01318       strecpy(_settings_client.network.server_password, str, lastof(_settings_client.network.server_password));
    01319     } else {
    01320       int32 value = atoi(str);
    01321       this->SetWidgetDirty(this->widget_id);
    01322       switch (this->widget_id) {
    01323         default: NOT_REACHED();
    01324         case NSSW_CLIENTS_TXT:    _settings_client.network.max_clients    = Clamp(value, 2, MAX_CLIENTS); break;
    01325         case NSSW_COMPANIES_TXT:  _settings_client.network.max_companies  = Clamp(value, 1, MAX_COMPANIES); break;
    01326         case NSSW_SPECTATORS_TXT: _settings_client.network.max_spectators = Clamp(value, 0, MAX_CLIENTS); break;
    01327       }
    01328     }
    01329
    01330     this->SetDirty();
    01331   }
    01332 };
    01333
    01334 static const NWidgetPart _nested_network_start_server_window_widgets[] = {
    01335   NWidget(NWID_HORIZONTAL),
    01336     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
    01337     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_START_SERVER_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01338   EndContainer(),
    01339   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NSSW_BACKGROUND),
    01340     NWidget(NWID_HORIZONTAL), SetPIP(10, 8, 10),
    01341       NWidget(NWID_VERTICAL), SetPIP(10, 0, 10),
    01342         /* Game name widgets */
    01343         NWidget(NWID_HORIZONTAL), SetPIP(0, 2, 0),
    01344           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_GAMENAME_LABEL), SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME, STR_NULL),
    01345           NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, NSSW_GAMENAME), SetMinimalSize(10, 12), SetFill(1, 0),
    01346                             SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME_OSKTITLE, STR_NETWORK_START_SERVER_NEW_GAME_NAME_TOOLTIP),
    01347         EndContainer(),
    01348         /* List of playable scenarios. */
    01349         NWidget(NWID_SPACER), SetMinimalSize(0, 8), SetFill(1, 0),
    01350         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_SELECT_MAP_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_SELECT_MAP, STR_NULL),
    01351         NWidget(NWID_SPACER), SetMinimalSize(0, 6), SetFill(1, 0),
    01352         NWidget(NWID_HORIZONTAL),
    01353           NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NSSW_SELMAP), SetMinimalSize(250, 0), SetFill(1, 1), SetDataTip(STR_NULL, STR_NETWORK_START_SERVER_SELECT_MAP_TOOLTIP), EndContainer(),
    01354           NWidget(WWT_SCROLLBAR, COLOUR_LIGHT_BLUE, NSSW_SCROLLBAR),
    01355         EndContainer(),
    01356       EndContainer(),
    01357       NWidget(NWID_VERTICAL), SetPIP(10, 0, 10),
    01358         /* Password widgets. */
    01359         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_SETPWD), SetFill(1, 0),
    01360                             SetDataTip(STR_NETWORK_START_SERVER_SET_PASSWORD, STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP),
    01361         /* Combo/selection boxes to control Connection Type / Max Clients / Max Companies / Max Observers / Language */
    01362         NWidget(NWID_SPACER), SetFill(1, 1),
    01363         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_CONNTYPE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_CONNECTION, STR_NULL),
    01364         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    01365         NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, NSSW_CONNTYPE_BTN), SetFill(1, 0),
    01366                         SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_CONNECTION_TOOLTIP),
    01367
    01368         NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    01369         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_CLIENTS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, STR_NULL),
    01370         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    01371         NWidget(NWID_HORIZONTAL),
    01372           NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, NSSW_CLIENTS_BTND), SetMinimalSize(12, 12), SetFill(0, 1),
    01373                         SetDataTip(SPR_ARROW_DOWN, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP),
    01374           NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, NSSW_CLIENTS_TXT), SetFill(1, 0),
    01375                         SetDataTip(STR_NETWORK_START_SERVER_CLIENTS_SELECT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP),
    01376           NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, NSSW_CLIENTS_BTNU), SetMinimalSize(12, 12), SetFill(0, 1),
    01377                         SetDataTip(SPR_ARROW_UP, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP),
    01378         EndContainer(),
    01379
    01380         NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    01381         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_COMPANIES_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, STR_NULL),
    01382         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    01383         NWidget(NWID_HORIZONTAL),
    01384           NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, NSSW_COMPANIES_BTND), SetMinimalSize(12, 12), SetFill(0, 1),
    01385                         SetDataTip(SPR_ARROW_DOWN, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP),
    01386           NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, NSSW_COMPANIES_TXT), SetFill(1, 0),
    01387                         SetDataTip(STR_NETWORK_START_SERVER_COMPANIES_SELECT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP),
    01388           NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, NSSW_COMPANIES_BTNU), SetMinimalSize(12, 12), SetFill(0, 1),
    01389                         SetDataTip(SPR_ARROW_UP, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP),
    01390         EndContainer(),
    01391
    01392         NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    01393         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_SPECTATORS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, STR_NULL),
    01394         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    01395         NWidget(NWID_HORIZONTAL),
    01396           NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, NSSW_SPECTATORS_BTND), SetMinimalSize(12, 12), SetFill(0, 1),
    01397                         SetDataTip(SPR_ARROW_DOWN, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS_TOOLTIP),
    01398           NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, NSSW_SPECTATORS_TXT), SetFill(1, 0),
    01399                         SetDataTip(STR_NETWORK_START_SERVER_SPECTATORS_SELECT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS_TOOLTIP),
    01400           NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, NSSW_SPECTATORS_BTNU), SetMinimalSize(12, 12), SetFill(0, 1),
    01401                         SetDataTip(SPR_ARROW_UP, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS_TOOLTIP),
    01402         EndContainer(),
    01403
    01404         NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    01405         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_LANGUAGE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_LANGUAGE_SPOKEN, STR_NULL),
    01406         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    01407         NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, NSSW_LANGUAGE_BTN), SetFill(1, 0),
    01408                         SetDataTip(STR_BLACK_STRING, STR_NETWORK_START_SERVER_LANGUAGE_TOOLTIP),
    01409       EndContainer(),
    01410     EndContainer(),
    01411     /* Buttons Start / Load / Cancel. */
    01412     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 5, 10),
    01413       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_START), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_START_GAME, STR_NETWORK_START_SERVER_START_GAME_TOOLTIP),
    01414       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_LOAD), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_LOAD_GAME, STR_NETWORK_START_SERVER_LOAD_GAME_TOOLTIP),
    01415       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    01416     EndContainer(),
    01417     NWidget(NWID_SPACER), SetMinimalSize(0, 10),
    01418   EndContainer(),
    01419 };
    01420
    01421 static const WindowDesc _network_start_server_window_desc(
    01422   WDP_CENTER, 0, 0,
    01423   WC_NETWORK_WINDOW, WC_NONE,
    01424   WDF_UNCLICK_BUTTONS,
    01425   _nested_network_start_server_window_widgets, lengthof(_nested_network_start_server_window_widgets)
    01426 );
    01427
    01428 static void ShowNetworkStartServerWindow()
    01429 {
    01430   DeleteWindowById(WC_NETWORK_WINDOW, 0);
    01431
    01432   new NetworkStartServerWindow(&_network_start_server_window_desc);
    01433 }
    01434
    01436 enum NetworkLobbyWindowWidgets {
    01437   NLWW_BACKGROUND,
    01438   NLWW_TEXT,
    01439   NLWW_HEADER,
    01440   NLWW_MATRIX,
    01441   NLWW_SCROLLBAR,
    01442   NLWW_DETAILS,
    01443   NLWW_JOIN,
    01444   NLWW_NEW,
    01445   NLWW_SPECTATE,
    01446   NLWW_REFRESH,
    01447   NLWW_CANCEL,
    01448 };
    01449
    01450 struct NetworkLobbyWindow : public Window {
    01451   CompanyID company;
    01452   NetworkGameList *server;
    01453   NetworkCompanyInfo company_info[MAX_COMPANIES];
    01454
    01455   NetworkLobbyWindow(const WindowDesc *desc, NetworkGameList *ngl) :
    01456       Window(), company(INVALID_COMPANY), server(ngl)
    01457   {
    01458     this->InitNested(desc, 0);
    01459     this->OnResize();
    01460   }
    01461
    01462   CompanyID NetworkLobbyFindCompanyIndex(byte pos) const
    01463   {
    01464     /* Scroll through all this->company_info and get the 'pos' item that is not empty */
    01465     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    01466       if (!StrEmpty(this->company_info[i].company_name)) {
    01467         if (pos-- == 0) return i;
    01468       }
    01469     }
    01470
    01471     return COMPANY_FIRST;
    01472   }
    01473
    01474   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01475   {
    01476     switch (widget) {
    01477       case NLWW_HEADER:
    01478         size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;;
    01479         break;
    01480
    01481       case NLWW_MATRIX:
    01482         resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    01483         size->height = 10 * resize->height;
    01484         break;
    01485
    01486       case NLWW_DETAILS:
    01487         size->height = 30 + 11 * FONT_HEIGHT_NORMAL;
    01488         break;
    01489     }
    01490   }
    01491
    01492   virtual void SetStringParameters(int widget) const
    01493   {
    01494     switch (widget) {
    01495       case NLWW_TEXT:
    01496         SetDParamStr(0, this->server->info.server_name);
    01497         break;
    01498     }
    01499   }
    01500
    01501   virtual void DrawWidget(const Rect &r, int widget) const
    01502   {
    01503     switch (widget) {
    01504       case NLWW_DETAILS:
    01505         this->DrawDetails(r);
    01506         break;
    01507
    01508       case NLWW_MATRIX:
    01509         this->DrawMatrix(r);
    01510         break;
    01511     }
    01512   }
    01513
    01514   virtual void OnPaint()
    01515   {
    01516     const NetworkGameInfo *gi = &this->server->info;
    01517
    01518     /* Join button is disabled when no company is selected and for AI companies*/
    01519     this->SetWidgetDisabledState(NLWW_JOIN, this->company == INVALID_COMPANY || GetLobbyCompanyInfo(this->company)->ai);
    01520     /* Cannot start new company if there are too many */
    01521     this->SetWidgetDisabledState(NLWW_NEW, gi->companies_on >= gi->companies_max);
    01522     /* Cannot spectate if there are too many spectators */
    01523     this->SetWidgetDisabledState(NLWW_SPECTATE, gi->spectators_on >= gi->spectators_max);
    01524
    01525     this->vscroll.SetCount(gi->companies_on);
    01526
    01527     /* Draw window widgets */
    01528     this->DrawWidgets();
    01529   }
    01530
    01531   void DrawMatrix(const Rect &r) const
    01532   {
    01533     bool rtl = _dynlang.text_dir == TD_RTL;
    01534     uint left = r.left + WD_FRAMERECT_LEFT;
    01535     uint right = r.right - WD_FRAMERECT_RIGHT;
    01536
    01537     uint text_left  = left + (rtl ? 20 : 0);
    01538     uint text_right = right - (rtl ? 0 : 20);
    01539     uint blob_left  = rtl ? left : right - 10;
    01540     uint lock_left  = rtl ? left + 10 : right - 20;
    01541
    01542     int y = r.top + WD_MATRIX_TOP;
    01543     /* Draw company list */
    01544     int pos = this->vscroll.GetPosition();
    01545     while (pos < this->server->info.companies_on) {
    01546       byte company = NetworkLobbyFindCompanyIndex(pos);
    01547       bool income = false;
    01548       if (this->company == company) {
    01549         GfxFillRect(r.left + 1, y - 2, r.right - 1, y + FONT_HEIGHT_NORMAL, 10); // show highlighted item with a different colour
    01550       }
    01551
    01552       DrawString(text_left, text_right, y, this->company_info[company].company_name, TC_BLACK);
    01553       if (this->company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, lock_left, y);
    01554
    01555       /* If the company's income was positive puts a green dot else a red dot */
    01556       if (this->company_info[company].income >= 0) income = true;
    01557       DrawSprite(SPR_BLOT, income ? PALETTE_TO_GREEN : PALETTE_TO_RED, blob_left, y + (FONT_HEIGHT_NORMAL - 10) / 2);
    01558
    01559       pos++;
    01560       y += this->resize.step_height;
    01561       if (pos >= this->vscroll.GetPosition() + this->vscroll.GetCapacity()) break;
    01562     }
    01563   }
    01564
    01565   void DrawDetails(const Rect &r) const
    01566   {
    01567     const int detail_height = 12 + FONT_HEIGHT_NORMAL + 12;
    01568     /* Draw info about selected company when it is selected in the left window */
    01569     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, 157);
    01570     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 12, STR_NETWORK_GAME_LOBBY_COMPANY_INFO, TC_FROMSTRING, SA_CENTER);
    01571
    01572     if (this->company == INVALID_COMPANY || StrEmpty(this->company_info[this->company].company_name)) return;
    01573
    01574     int y = r.top + detail_height + 4;
    01575     const NetworkGameInfo *gi = &this->server->info;
    01576
    01577     SetDParam(0, gi->clients_on);
    01578     SetDParam(1, gi->clients_max);
    01579     SetDParam(2, gi->companies_on);
    01580     SetDParam(3, gi->companies_max);
    01581     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
    01582     y += FONT_HEIGHT_NORMAL;
    01583
    01584     SetDParamStr(0, this->company_info[this->company].company_name);
    01585     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_COMPANY_NAME);
    01586     y += FONT_HEIGHT_NORMAL;
    01587
    01588     SetDParam(0, this->company_info[this->company].inaugurated_year);
    01589     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_INAUGURATION_YEAR); // inauguration year
    01590     y += FONT_HEIGHT_NORMAL;
    01591
    01592     SetDParam(0, this->company_info[this->company].company_value);
    01593     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VALUE); // company value
    01594     y += FONT_HEIGHT_NORMAL;
    01595
    01596     SetDParam(0, this->company_info[this->company].money);
    01597     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_CURRENT_BALANCE); // current balance
    01598     y += FONT_HEIGHT_NORMAL;
    01599
    01600     SetDParam(0, this->company_info[this->company].income);
    01601     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_LAST_YEARS_INCOME); // last year's income
    01602     y += FONT_HEIGHT_NORMAL;
    01603
    01604     SetDParam(0, this->company_info[this->company].performance);
    01605     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PERFORMANCE); // performance
    01606     y += FONT_HEIGHT_NORMAL;
    01607
    01608     for (uint i = 0; i < lengthof(this->company_info[this->company].num_vehicle); i++) {
    01609       SetDParam(i, this->company_info[this->company].num_vehicle[i]);
    01610     }
    01611     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VEHICLES); // vehicles
    01612     y += FONT_HEIGHT_NORMAL;
    01613
    01614     for (uint i = 0; i < lengthof(this->company_info[this->company].num_station); i++) {
    01615       SetDParam(i, this->company_info[this->company].num_station[i]);
    01616     }
    01617     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_STATIONS); // stations
    01618     y += FONT_HEIGHT_NORMAL;
    01619
    01620     SetDParamStr(0, this->company_info[this->company].clients);
    01621     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PLAYERS); // players
    01622   }
    01623
    01624   virtual void OnClick(Point pt, int widget)
    01625   {
    01626     switch (widget) {
    01627       case NLWW_CANCEL:   // Cancel button
    01628         ShowNetworkGameWindow();
    01629         break;
    01630
    01631       case NLWW_MATRIX: { // Company list
    01632         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(NLWW_MATRIX)->pos_y) / this->resize.step_height;
    01633
    01634         if (id_v >= this->vscroll.GetCapacity()) break;
    01635
    01636         id_v += this->vscroll.GetPosition();
    01637         this->company = (id_v >= this->server->info.companies_on) ? INVALID_COMPANY : NetworkLobbyFindCompanyIndex(id_v);
    01638         this->SetDirty();
    01639       } break;
    01640
    01641       case NLWW_JOIN:     // Join company
    01642         /* Button can be clicked only when it is enabled */
    01643         NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), this->company);
    01644         break;
    01645
    01646       case NLWW_NEW:      // New company
    01647         NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_NEW_COMPANY);
    01648         break;
    01649
    01650       case NLWW_SPECTATE: // Spectate game
    01651         NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
    01652         break;
    01653
    01654       case NLWW_REFRESH:  // Refresh
    01655         NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
    01656         NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
    01657         /* Clear the information so removed companies don't remain */
    01658         memset(this->company_info, 0, sizeof(this->company_info));
    01659         break;
    01660     }
    01661   }
    01662
    01663   virtual void OnDoubleClick(Point pt, int widget)
    01664   {
    01665     if (widget == NLWW_MATRIX) {
    01666       /* is the Join button enabled? */
    01667       if (!this->IsWidgetDisabled(NLWW_JOIN)) this->OnClick(pt, NLWW_JOIN);
    01668     }
    01669   }
    01670
    01671   virtual void OnResize()
    01672   {
    01673     this->vscroll.SetCapacityFromWidget(this, NLWW_MATRIX);
    01674     this->GetWidget<NWidgetCore>(NLWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    01675   }
    01676 };
    01677
    01678 static const NWidgetPart _nested_network_lobby_window_widgets[] = {
    01679   NWidget(NWID_HORIZONTAL),
    01680     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
    01681     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_GAME_LOBBY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01682   EndContainer(),
    01683   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NLWW_BACKGROUND),
    01684     NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NLWW_TEXT), SetDataTip(STR_NETWORK_GAME_LOBBY_PREPARE_TO_JOIN, STR_NULL), SetResize(1, 0), SetPadding(10, 10, 0, 10),
    01685     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
    01686     NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
    01687       /* Company list. */
    01688       NWidget(NWID_VERTICAL),
    01689         NWidget(WWT_PANEL, COLOUR_WHITE, NLWW_HEADER), SetMinimalSize(146, 0), SetResize(1, 0), SetFill(1, 0), EndContainer(),
    01690         NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, NLWW_MATRIX), SetMinimalSize(146, 0), SetResize(1, 1), SetFill(1, 1), SetDataTip(0, STR_NETWORK_GAME_LOBBY_COMPANY_LIST_TOOLTIP),
    01691       EndContainer(),
    01692       NWidget(WWT_SCROLLBAR, COLOUR_LIGHT_BLUE, NLWW_SCROLLBAR),
    01693       NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetResize(0, 1),
    01694       /* Company info. */
    01695       NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NLWW_DETAILS), SetMinimalSize(232, 0), SetResize(1, 1), SetFill(1, 1), EndContainer(),
    01696     EndContainer(),
    01697     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
    01698     /* Buttons. */
    01699     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 3, 10),
    01700       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
    01701         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NLWW_JOIN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_GAME_LOBBY_JOIN_COMPANY, STR_NETWORK_GAME_LOBBY_JOIN_COMPANY_TOOLTIP),
    01702         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NLWW_NEW), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_GAME_LOBBY_NEW_COMPANY, STR_NETWORK_GAME_LOBBY_NEW_COMPANY_TOOLTIP),
    01703       EndContainer(),
    01704       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
    01705         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NLWW_SPECTATE), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_GAME_LOBBY_SPECTATE_GAME, STR_NETWORK_GAME_LOBBY_SPECTATE_GAME_TOOLTIP),
    01706         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NLWW_REFRESH), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
    01707       EndContainer(),
    01708       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
    01709         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NLWW_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    01710         NWidget(NWID_SPACER), SetFill(1, 1),
    01711       EndContainer(),
    01712     EndContainer(),
    01713     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
    01714   EndContainer(),
    01715 };
    01716
    01717 static const WindowDesc _network_lobby_window_desc(
    01718   WDP_CENTER, 0, 0,
    01719   WC_NETWORK_WINDOW, WC_NONE,
    01720   WDF_UNCLICK_BUTTONS,
    01721   _nested_network_lobby_window_widgets, lengthof(_nested_network_lobby_window_widgets)
    01722 );
    01723
    01724 /* Show the networklobbywindow with the selected server
    01725  * @param ngl Selected game pointer which is passed to the new window */
    01726 static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
    01727 {
    01728   DeleteWindowById(WC_NETWORK_WINDOW, 0);
    01729
    01730   NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
    01731   NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
    01732
    01733   new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
    01734 }
    01735
    01741 NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company)
    01742 {
    01743   NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, 0));
    01744   return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL;
    01745 }
    01746
    01747 /* The window below gives information about the connected clients
    01748  *  and also makes able to give money to them, kick them (if server)
    01749  *  and stuff like that. */
    01750
    01751 extern void DrawCompanyIcon(CompanyID cid, int x, int y);
    01752
    01753 /* Every action must be of this form */
    01754 typedef void ClientList_Action_Proc(byte client_no);
    01755
    01756 static const NWidgetPart _nested_client_list_popup_widgets[] = {
    01757   NWidget(WWT_PANEL, COLOUR_GREY, 0), EndContainer(),
    01758 };
    01759
    01760 static const WindowDesc _client_list_popup_desc(
    01761   WDP_AUTO, 0, 0,
    01762   WC_TOOLBAR_MENU, WC_CLIENT_LIST,
    01763   0,
    01764   _nested_client_list_popup_widgets, lengthof(_nested_client_list_popup_widgets)
    01765 );
    01766
    01767 /* Finds the Xth client-info that is active */
    01768 static NetworkClientInfo *NetworkFindClientInfo(byte client_no)
    01769 {
    01770   NetworkClientInfo *ci;
    01771
    01772   FOR_ALL_CLIENT_INFOS(ci) {
    01773     if (client_no == 0) return ci;
    01774     client_no--;
    01775   }
    01776
    01777   return NULL;
    01778 }
    01779
    01780 /* Here we start to define the options out of the menu */
    01781 static void ClientList_Kick(byte client_no)
    01782 {
    01783   const NetworkClientInfo *ci = NetworkFindClientInfo(client_no);
    01784
    01785   if (ci == NULL) return;
    01786
    01787   NetworkServerKickClient(ci->client_id);
    01788 }
    01789
    01790 static void ClientList_Ban(byte client_no)
    01791 {
    01792   NetworkClientInfo *ci = NetworkFindClientInfo(client_no);
    01793
    01794   if (ci == NULL) return;
    01795
    01796   NetworkServerBanIP(GetClientIP(ci));
    01797 }
    01798
    01799 static void ClientList_GiveMoney(byte client_no)
    01800 {
    01801   if (NetworkFindClientInfo(client_no) != NULL) {
    01802     ShowNetworkGiveMoneyWindow(NetworkFindClientInfo(client_no)->client_playas);
    01803   }
    01804 }
    01805
    01806 static void ClientList_SpeakToClient(byte client_no)
    01807 {
    01808   if (NetworkFindClientInfo(client_no) != NULL) {
    01809     ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, NetworkFindClientInfo(client_no)->client_id);
    01810   }
    01811 }
    01812
    01813 static void ClientList_SpeakToCompany(byte client_no)
    01814 {
    01815   if (NetworkFindClientInfo(client_no) != NULL) {
    01816     ShowNetworkChatQueryWindow(DESTTYPE_TEAM, NetworkFindClientInfo(client_no)->client_playas);
    01817   }
    01818 }
    01819
    01820 static void ClientList_SpeakToAll(byte client_no)
    01821 {
    01822   ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
    01823 }
    01824
    01826 struct NetworkClientListPopupWindow : Window {
    01828   struct ClientListAction {
    01829     StringID name;
    01830     ClientList_Action_Proc *proc;
    01831   };
    01832
    01833   uint sel_index;
    01834   int client_no;
    01835   Point desired_location;
    01836   SmallVector<ClientListAction, 2> actions;
    01837
    01843   inline void AddAction(StringID name, ClientList_Action_Proc *proc)
    01844   {
    01845     ClientListAction *action = this->actions.Append();
    01846     action->name = name;
    01847     action->proc = proc;
    01848   }
    01849
    01850   NetworkClientListPopupWindow(const WindowDesc *desc, int x, int y, int client_no) :
    01851       Window(),
    01852       sel_index(0), client_no(client_no)
    01853   {
    01854     this->desired_location.x = x;
    01855     this->desired_location.y = y;
    01856
    01857     const NetworkClientInfo *ci = NetworkFindClientInfo(client_no);
    01858
    01859     if (_network_own_client_id != ci->client_id) {
    01860       this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, &ClientList_SpeakToClient);
    01861     }
    01862
    01863     if (Company::IsValidID(ci->client_playas) || ci->client_playas == COMPANY_SPECTATOR) {
    01864       this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_COMPANY, &ClientList_SpeakToCompany);
    01865     }
    01866     this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, &ClientList_SpeakToAll);
    01867
    01868     if (_network_own_client_id != ci->client_id) {
    01869       /* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed */
    01870       if (Company::IsValidID(_local_company) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
    01871         this->AddAction(STR_NETWORK_CLIENTLIST_GIVE_MONEY, &ClientList_GiveMoney);
    01872       }
    01873     }
    01874
    01875     /* A server can kick clients (but not himself) */
    01876     if (_network_server && _network_own_client_id != ci->client_id) {
    01877       this->AddAction(STR_NETWORK_CLIENTLIST_KICK, &ClientList_Kick);
    01878       this->AddAction(STR_NETWORK_CLIENTLIST_BAN, &ClientList_Ban);
    01879     }
    01880
    01881     this->flags4 &= ~WF_WHITE_BORDER_MASK;
    01882     this->InitNested(desc, 0);
    01883   }
    01884
    01885   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    01886   {
    01887     return this->desired_location;
    01888   }
    01889
    01890   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01891   {
    01892     Dimension d = *size;
    01893     for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++) {
    01894       d = maxdim(GetStringBoundingBox(action->name), d);
    01895     }
    01896
    01897     d.height *= this->actions.Length();
    01898     d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    01899     d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01900     *size = d;
    01901   }
    01902
    01903   virtual void DrawWidget(const Rect &r, int widget) const
    01904   {
    01905     /* Draw the actions */
    01906     int sel = this->sel_index;
    01907     int y = r.top + WD_FRAMERECT_TOP;
    01908     for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++, y += FONT_HEIGHT_NORMAL) {
    01909       TextColour colour;
    01910       if (sel-- == 0) { // Selected item, highlight it
    01911         GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, 0);
    01912         colour = TC_WHITE;
    01913       } else {
    01914         colour = TC_BLACK;
    01915       }
    01916
    01917       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, action->name, colour);
    01918     }
    01919   }
    01920
    01921   virtual void OnPaint()
    01922   {
    01923     this->DrawWidgets();
    01924   }
    01925
    01926   virtual void OnMouseLoop()
    01927   {
    01928     /* We selected an action */
    01929     uint index = (_cursor.pos.y - this->top - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
    01930
    01931     if (_left_button_down) {
    01932       if (index == this->sel_index || index >= this->actions.Length()) return;
    01933
    01934       this->sel_index = index;
    01935       this->SetDirty();
    01936     } else {
    01937       if (index < this->actions.Length() && _cursor.pos.y >= this->top) {
    01938         this->actions[index].proc(this->client_no);
    01939       }
    01940
    01941       DeleteWindowById(WC_TOOLBAR_MENU, 0);
    01942     }
    01943   }
    01944 };
    01945
    01949 static void PopupClientList(int client_no, int x, int y)
    01950 {
    01951   DeleteWindowById(WC_TOOLBAR_MENU, 0);
    01952
    01953   if (NetworkFindClientInfo(client_no) == NULL) return;
    01954
    01955   new NetworkClientListPopupWindow(&_client_list_popup_desc, x, y, client_no);
    01956 }
    01957
    01958
    01960 enum ClientListWidgets {
    01961   CLW_PANEL,
    01962 };
    01963
    01964 static const NWidgetPart _nested_client_list_widgets[] = {
    01965   NWidget(NWID_HORIZONTAL),
    01966     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01967     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01968     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01969   EndContainer(),
    01970   NWidget(WWT_PANEL, COLOUR_GREY, CLW_PANEL), SetMinimalSize(250, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 1), EndContainer(),
    01971 };
    01972
    01973 static const WindowDesc _client_list_desc(
    01974   WDP_AUTO, 0, 0,
    01975   WC_CLIENT_LIST, WC_NONE,
    01976   0,
    01977   _nested_client_list_widgets, lengthof(_nested_client_list_widgets)
    01978 );
    01979
    01983 struct NetworkClientListWindow : Window {
    01984   int selected_item;
    01985
    01986   uint server_client_width;
    01987   uint company_icon_width;
    01988
    01989   NetworkClientListWindow(const WindowDesc *desc, WindowNumber window_number) :
    01990       Window(),
    01991       selected_item(-1)
    01992   {
    01993     this->InitNested(desc, window_number);
    01994   }
    01995
    01999   bool CheckClientListHeight()
    02000   {
    02001     int num = 0;
    02002     const NetworkClientInfo *ci;
    02003
    02004     /* Should be replaced with a loop through all clients */
    02005     FOR_ALL_CLIENT_INFOS(ci) {
    02006       if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++;
    02007     }
    02008
    02009     num *= FONT_HEIGHT_NORMAL;
    02010
    02011     int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->GetWidget<NWidgetBase>(CLW_PANEL)->current_y);
    02012     /* If height is changed */
    02013     if (diff != 0) {
    02014       ResizeWindow(this, 0, diff);
    02015       return false;
    02016     }
    02017     return true;
    02018   }
    02019
    02020   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    02021   {
    02022     if (widget != CLW_PANEL) return;
    02023
    02024     this->server_client_width = max(GetStringBoundingBox(STR_NETWORK_SERVER).width, GetStringBoundingBox(STR_NETWORK_CLIENT).width) + WD_FRAMERECT_RIGHT;
    02025     this->company_icon_width = GetSpriteSize(SPR_COMPANY_ICON).width + WD_FRAMERECT_LEFT;
    02026
    02027     uint width = 100; // Default width
    02028     const NetworkClientInfo *ci;
    02029     FOR_ALL_CLIENT_INFOS(ci) {
    02030       width = max(width, GetStringBoundingBox(ci->client_name).width);
    02031     }
    02032
    02033     size->width = WD_FRAMERECT_LEFT + this->server_client_width + this->company_icon_width + width + WD_FRAMERECT_RIGHT;
    02034   }
    02035
    02036   virtual void OnPaint()
    02037   {
    02038     /* Check if we need to reset the height */
    02039     if (!this->CheckClientListHeight()) return;
    02040
    02041     this->DrawWidgets();
    02042   }
    02043
    02044   virtual void DrawWidget(const Rect &r, int widget) const
    02045   {
    02046     if (widget != CLW_PANEL) return;
    02047
    02048     bool rtl = _dynlang.text_dir == TD_RTL;
    02049     int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - 10) / 2;
    02050     uint y = r.top + WD_FRAMERECT_TOP;
    02051     uint left = r.left + WD_FRAMERECT_LEFT;
    02052     uint right = r.right - WD_FRAMERECT_RIGHT;
    02053     uint type_icon_width = this->server_client_width + this->company_icon_width;
    02054
    02055
    02056     uint type_left  = rtl ? right - this->server_client_width : left;
    02057     uint type_right = rtl ? right : left + this->server_client_width - 1;
    02058     uint icon_left  = rtl ? right - type_icon_width + WD_FRAMERECT_LEFT : left + this->server_client_width;
    02059     uint name_left  = rtl ? left : left + type_icon_width;
    02060     uint name_right = rtl ? right - type_icon_width : right;
    02061
    02062     int i = 0;
    02063     const NetworkClientInfo *ci;
    02064     FOR_ALL_CLIENT_INFOS(ci) {
    02065       TextColour colour;
    02066       if (this->selected_item == i++) { // Selected item, highlight it
    02067         GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, 0);
    02068         colour = TC_WHITE;
    02069       } else {
    02070         colour = TC_BLACK;
    02071       }
    02072
    02073       if (ci->client_id == CLIENT_ID_SERVER) {
    02074         DrawString(type_left, type_right, y, STR_NETWORK_SERVER, colour);
    02075       } else {
    02076         DrawString(type_left, type_right, y, STR_NETWORK_CLIENT, colour);
    02077       }
    02078
    02079       /* Filter out spectators */
    02080       if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon(ci->client_playas, icon_left, y + icon_y_offset);
    02081
    02082       DrawString(name_left, name_right, y, ci->client_name, colour);
    02083
    02084       y += FONT_HEIGHT_NORMAL;
    02085     }
    02086   }
    02087
    02088   virtual void OnClick(Point pt, int widget)
    02089   {
    02090     /* Show the popup with option */
    02091     if (this->selected_item != -1) {
    02092       PopupClientList(this->selected_item, pt.x + this->left, pt.y + this->top);
    02093     }
    02094   }
    02095
    02096   virtual void OnMouseOver(Point pt, int widget)
    02097   {
    02098     /* -1 means we left the current window */
    02099     if (pt.y == -1) {
    02100       this->selected_item = -1;
    02101       this->SetDirty();
    02102       return;
    02103     }
    02104
    02105     /* Find the new selected item (if any) */
    02106     pt.y -= this->GetWidget<NWidgetBase>(CLW_PANEL)->pos_y;
    02107     int item = -1;
    02108     if (IsInsideMM(pt.y, WD_FRAMERECT_TOP, this->GetWidget<NWidgetBase>(CLW_PANEL)->current_y - WD_FRAMERECT_BOTTOM)) {
    02109       item = (pt.y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
    02110     }
    02111
    02112     /* It did not change.. no update! */
    02113     if (item == this->selected_item) return;
    02114     this->selected_item = item;
    02115
    02116     /* Repaint */
    02117     this->SetDirty();
    02118   }
    02119 };
    02120
    02121 void ShowClientList()
    02122 {
    02123   AllocateWindowDescFront<NetworkClientListWindow>(&_client_list_desc, 0);
    02124 }
    02125
    02126
    02127 static NetworkPasswordType pw_type;
    02128
    02129
    02130 void ShowNetworkNeedPassword(NetworkPasswordType npt)
    02131 {
    02132   StringID caption;
    02133
    02134   pw_type = npt;
    02135   switch (npt) {
    02136     default: NOT_REACHED();
    02137     case NETWORK_GAME_PASSWORD:    caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION; break;
    02138     case NETWORK_COMPANY_PASSWORD: caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION; break;
    02139   }
    02140   ShowQueryString(STR_EMPTY, caption, 20, 180, FindWindowById(WC_NETWORK_STATUS_WINDOW, 0), CS_ALPHANUMERAL, QSF_NONE);
    02141 }
    02142
    02143 /* Vars needed for the join-GUI */
    02144 NetworkJoinStatus _network_join_status;
    02145 uint8 _network_join_waiting;
    02146 uint32 _network_join_bytes;
    02147 uint32 _network_join_bytes_total;
    02148
    02150 enum NetworkJoinStatusWidgets {
    02151   NJSW_BACKGROUND,
    02152   NJSW_CANCELOK,
    02153 };
    02154
    02155 struct NetworkJoinStatusWindow : Window {
    02156   NetworkJoinStatusWindow(const WindowDesc *desc) : Window()
    02157   {
    02158     this->parent = FindWindowById(WC_NETWORK_WINDOW, 0);
    02159     this->InitNested(desc, 0);
    02160   }
    02161
    02162   virtual void OnPaint()
    02163   {
    02164     this->DrawWidgets();
    02165   }
    02166
    02167   virtual void DrawWidget(const Rect &r, int widget) const
    02168   {
    02169     if (widget != NJSW_BACKGROUND) return;
    02170
    02171     uint8 progress; // used for progress bar
    02172     DrawString(r.left + 2, r.right - 2, r.top + 20, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_FROMSTRING, SA_CENTER);
    02173     switch (_network_join_status) {
    02174       case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
    02175       case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
    02176         progress = 10; // first two stages 10%
    02177         break;
    02178       case NETWORK_JOIN_STATUS_WAITING:
    02179         SetDParam(0, _network_join_waiting);
    02180         DrawString(r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, STR_NETWORK_CONNECTING_WAITING, TC_FROMSTRING, SA_CENTER);
    02181         progress = 15; // third stage is 15%
    02182         break;
    02183       case NETWORK_JOIN_STATUS_DOWNLOADING:
    02184         SetDParam(0, _network_join_bytes);
    02185         SetDParam(1, _network_join_bytes_total);
    02186         DrawString(r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, STR_NETWORK_CONNECTING_DOWNLOADING, TC_FROMSTRING, SA_CENTER);
    02187         /* Fallthrough */
    02188       default: // Waiting is 15%, so the resting receivement of map is maximum 70%
    02189         progress = 15 + _network_join_bytes * (100 - 15) / _network_join_bytes_total;
    02190     }
    02191
    02192     /* Draw nice progress bar :) */
    02193     DrawFrameRect(r.left + 20, r.top + 5, (int)((this->width - 20) * progress / 100), r.top + 15, COLOUR_MAUVE, FR_NONE);
    02194   }
    02195
    02196   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    02197   {
    02198     if (widget != NJSW_BACKGROUND) return;
    02199
    02200     size->height = 25 + 2 * FONT_HEIGHT_NORMAL;
    02201
    02202     /* Account for the statuses */
    02203     uint width = 0;
    02204     for (uint i = 0; i < NETWORK_JOIN_STATUS_END; i++) {
    02205       width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_1 + i).width);
    02206     }
    02207
    02208     /* For the number of waiting (other) players */
    02209     SetDParam(0, 255);
    02210     width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_WAITING).width);
    02211
    02212     /* Account for downloading ~ 10 MiB */
    02213     SetDParam(0, 10000000);
    02214     SetDParam(1, 10000000);
    02215     width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING).width);
    02216
    02217     /* Give a bit more clearing for the widest strings than strictly needed */
    02218     size->width = width + WD_FRAMERECT_LEFT + WD_FRAMERECT_BOTTOM + 10;
    02219   }
    02220
    02221   virtual void OnClick(Point pt, int widget)
    02222   {
    02223     if (widget == NJSW_CANCELOK) { // Disconnect button
    02224       NetworkDisconnect();
    02225       SwitchToMode(SM_MENU);
    02226       ShowNetworkGameWindow();
    02227     }
    02228   }
    02229
    02230   virtual void OnQueryTextFinished(char *str)
    02231   {
    02232     if (StrEmpty(str)) {
    02233       NetworkDisconnect();
    02234       ShowNetworkGameWindow();
    02235     } else {
    02236       SEND_COMMAND(PACKET_CLIENT_PASSWORD)(pw_type, str);
    02237     }
    02238   }
    02239 };
    02240
    02241 static const NWidgetPart _nested_network_join_status_window_widgets[] = {
    02242   NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_CONNECTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    02243   NWidget(WWT_PANEL, COLOUR_GREY),
    02244     NWidget(WWT_EMPTY, COLOUR_GREY, NJSW_BACKGROUND),
    02245     NWidget(NWID_HORIZONTAL),
    02246       NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
    02247       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NJSW_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_NETWORK_CONNECTION_DISCONNECT, STR_NULL),
    02248       NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
    02249     EndContainer(),
    02250     NWidget(NWID_SPACER), SetMinimalSize(0, 4),
    02251   EndContainer(),
    02252 };
    02253
    02254 static const WindowDesc _network_join_status_window_desc(
    02255   WDP_CENTER, 0, 0,
    02256   WC_NETWORK_STATUS_WINDOW, WC_NONE,
    02257   WDF_MODAL,
    02258   _nested_network_join_status_window_widgets, lengthof(_nested_network_join_status_window_widgets)
    02259 );
    02260
    02261 void ShowJoinStatusWindow()
    02262 {
    02263   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    02264   new NetworkJoinStatusWindow(&_network_join_status_window_desc);
    02265 }
    02266
    02267
    02269 enum NetworkCompanyPasswordWindowWidgets {
    02270   NCPWW_BACKGROUND,
    02271   NCPWW_LABEL,
    02272   NCPWW_PASSWORD,
    02273   NCPWW_SAVE_AS_DEFAULT_PASSWORD,
    02274   NCPWW_CANCEL,
    02275   NCPWW_OK,
    02276 };
    02277
    02278 struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
    02279   NetworkCompanyPasswordWindow(const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(lengthof(_settings_client.network.default_company_pass))
    02280   {
    02281     this->InitNested(desc, 0);
    02282
    02283     this->parent = parent;
    02284     this->afilter = CS_ALPHANUMERAL;
    02285     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0);
    02286     this->SetFocusedWidget(NCPWW_PASSWORD);
    02287   }
    02288
    02289   void OnOk()
    02290   {
    02291     if (this->IsWidgetLowered(NCPWW_SAVE_AS_DEFAULT_PASSWORD)) {
    02292       snprintf(_settings_client.network.default_company_pass, lengthof(_settings_client.network.default_company_pass), "%s", this->edit_str_buf);
    02293     }
    02294
    02295     /* empty password is a '*' because of console argument */
    02296     if (StrEmpty(this->edit_str_buf)) snprintf(this->edit_str_buf, this->edit_str_size, "*");
    02297     char *password = this->edit_str_buf;
    02298     NetworkChangeCompanyPassword(1, &password);
    02299   }
    02300
    02301   virtual void OnPaint()
    02302   {
    02303     this->DrawWidgets();
    02304     this->DrawEditBox(NCPWW_PASSWORD);
    02305   }
    02306
    02307   virtual void OnClick(Point pt, int widget)
    02308   {
    02309     switch (widget) {
    02310       case NCPWW_OK:
    02311         this->OnOk();
    02312
    02313       /* FALL THROUGH */
    02314       case NCPWW_CANCEL:
    02315         delete this;
    02316         break;
    02317
    02318       case NCPWW_SAVE_AS_DEFAULT_PASSWORD:
    02319         this->ToggleWidgetLoweredState(NCPWW_SAVE_AS_DEFAULT_PASSWORD);
    02320         this->SetDirty();
    02321         break;
    02322     }
    02323   }
    02324
    02325   virtual void OnMouseLoop()
    02326   {
    02327     this->HandleEditBox(4);
    02328   }
    02329
    02330   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    02331   {
    02332     EventState state = ES_NOT_HANDLED;
    02333     switch (this->HandleEditBoxKey(NCPWW_PASSWORD, key, keycode, state)) {
    02334       default: break;
    02335
    02336       case HEBR_CONFIRM:
    02337         this->OnOk();
    02338         /* FALL THROUGH */
    02339
    02340       case HEBR_CANCEL:
    02341         delete this;
    02342         break;
    02343     }
    02344     return state;
    02345   }
    02346
    02347   virtual void OnOpenOSKWindow(int wid)
    02348   {
    02349     ShowOnScreenKeyboard(this, wid, NCPWW_CANCEL, NCPWW_OK);
    02350   }
    02351 };
    02352
    02353 static const NWidgetPart _nested_network_company_password_window_widgets[] = {
    02354   NWidget(NWID_HORIZONTAL),
    02355     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    02356     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_PASSWORD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    02357   EndContainer(),
    02358   NWidget(WWT_PANEL, COLOUR_GREY, NCPWW_BACKGROUND),
    02359     NWidget(NWID_VERTICAL), SetPIP(5, 5, 5),
    02360       NWidget(NWID_HORIZONTAL), SetPIP(5, 5, 5),
    02361         NWidget(WWT_TEXT, COLOUR_GREY, NCPWW_LABEL), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_NULL),
    02362         NWidget(WWT_EDITBOX, COLOUR_GREY, NCPWW_PASSWORD), SetMinimalSize(194, 12), SetDataTip(STR_COMPANY_VIEW_SET_PASSWORD, STR_NULL),
    02363       EndContainer(),
    02364       NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
    02365         NWidget(NWID_SPACER), SetFill(1, 0),
    02366         NWidget(WWT_TEXTBTN, COLOUR_GREY, NCPWW_SAVE_AS_DEFAULT_PASSWORD), SetMinimalSize(194, 12),
    02367                       SetDataTip(STR_COMPANY_PASSWORD_MAKE_DEFAULT, STR_COMPANY_PASSWORD_MAKE_DEFAULT_TOOLTIP),
    02368       EndContainer(),
    02369     EndContainer(),
    02370   EndContainer(),
    02371   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    02372     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, NCPWW_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_COMPANY_PASSWORD_CANCEL),
    02373     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, NCPWW_OK), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_COMPANY_PASSWORD_OK),
    02374   EndContainer(),
    02375 };
    02376
    02377 static const WindowDesc _network_company_password_window_desc(
    02378   WDP_AUTO, 0, 0,
    02379   WC_COMPANY_PASSWORD_WINDOW, WC_NONE,
    02380   WDF_UNCLICK_BUTTONS,
    02381   _nested_network_company_password_window_widgets, lengthof(_nested_network_company_password_window_widgets)
    02382 );
    02383
    02384 void ShowNetworkCompanyPasswordWindow(Window *parent)
    02385 {
    02386   DeleteWindowById(WC_COMPANY_PASSWORD_WINDOW, 0);
    02387
    02388   new NetworkCompanyPasswordWindow(&_network_company_password_window_desc, parent);
    02389 }
    02390
    02391 #endif /* ENABLE_NETWORK */