network_gui.cpp

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