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