network_gui.cpp

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