network_gui.cpp

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