# network_gui.cpp

Go to the documentation of this file.

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