network_gui.cpp

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