network_gui.cpp

Go to the documentation of this file.
00001 /* $Id: network_gui.cpp 22461 2011-05-15 09:38:54Z 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 
00895 Listing NetworkGameWindow::last_sorting = {false, 5};
00896 GUIGameServerList::SortFunction * const NetworkGameWindow::sorter_funcs[] = {
00897   &NGameNameSorter,
00898   &NGameClientSorter,
00899   &NGameMapSizeSorter,
00900   &NGameDateSorter,
00901   &NGameYearsSorter,
00902   &NGameAllowedSorter
00903 };
00904 
00905 static NWidgetBase *MakeResizableHeader(int *biggest_index)
00906 {
00907   *biggest_index = max<int>(*biggest_index, NGWW_INFO);
00908   return new NWidgetServerListHeader();
00909 }
00910 
00911 static const NWidgetPart _nested_network_game_widgets[] = {
00912   /* TOP */
00913   NWidget(NWID_HORIZONTAL),
00914     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
00915     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_SERVER_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00916   EndContainer(),
00917   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NGWW_MAIN),
00918     NWidget(NWID_VERTICAL), SetPIP(10, 7, 0),
00919       NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
00920         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NGWW_CONNECTION), SetDataTip(STR_NETWORK_SERVER_LIST_CONNECTION, STR_NULL),
00921         NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, NGWW_CONN_BTN),
00922                     SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_CONNECTION_TOOLTIP),
00923         NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
00924         NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NGWW_CLIENT_LABEL), SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME, STR_NULL),
00925         NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, NGWW_CLIENT), SetMinimalSize(151, 12),
00926                     SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE, STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP),
00927       EndContainer(),
00928       NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
00929         /* LEFT SIDE */
00930         NWidget(NWID_VERTICAL),
00931           NWidget(NWID_HORIZONTAL),
00932             NWidget(NWID_VERTICAL),
00933               NWidgetFunction(MakeResizableHeader),
00934               NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, NGWW_MATRIX), SetResize(1, 1), SetFill(1, 0),
00935                         SetDataTip(0, STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT), SetScrollbar(NGWW_SCROLLBAR),
00936             EndContainer(),
00937             NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, NGWW_SCROLLBAR),
00938           EndContainer(),
00939           NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0), SetFill(1, 1),
00940           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NGWW_LASTJOINED_LABEL), SetFill(1, 0),
00941                     SetDataTip(STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER, STR_NULL), SetResize(1, 0),
00942           NWidget(NWID_HORIZONTAL), SetPIP(0, 0, WD_VSCROLLBAR_WIDTH),
00943             NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NGWW_LASTJOINED), SetFill(1, 0), SetResize(1, 0),
00944                       SetDataTip(0x0, STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST),
00945             EndContainer(),
00946           EndContainer(),
00947         EndContainer(),
00948         /* RIGHT SIDE */
00949         NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NGWW_DETAILS),
00950           NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
00951             NWidget(WWT_EMPTY, INVALID_COLOUR, NGWW_DETAILS_SPACER), SetMinimalSize(140, 155), SetResize(0, 1), SetFill(1, 1), // Make sure it's at least this wide
00952             NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
00953               NWidget(NWID_SPACER), SetFill(1, 0),
00954               NWidget(NWID_SELECTION, INVALID_COLOUR, NGWW_NEWGRF_SEL),
00955                 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_NEWGRF), SetFill(1, 0), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL),
00956                 NWidget(NWID_SPACER), SetFill(1, 0),
00957               EndContainer(),
00958             EndContainer(),
00959             NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
00960               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_JOIN), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_JOIN_GAME, STR_NULL),
00961               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_REFRESH), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
00962             EndContainer(),
00963           EndContainer(),
00964         EndContainer(),
00965       EndContainer(),
00966       /* BOTTOM */
00967       NWidget(NWID_HORIZONTAL),
00968         NWidget(NWID_VERTICAL),
00969           NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 7, 4),
00970             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_FIND), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_FIND_SERVER, STR_NETWORK_SERVER_LIST_FIND_SERVER_TOOLTIP),
00971             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_ADD), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_ADD_SERVER, STR_NETWORK_SERVER_LIST_ADD_SERVER_TOOLTIP),
00972             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_START), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_START_SERVER, STR_NETWORK_SERVER_LIST_START_SERVER_TOOLTIP),
00973             NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NGWW_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00974           EndContainer(),
00975           NWidget(NWID_SPACER), SetMinimalSize(0, 6), SetResize(1, 0), SetFill(1, 0),
00976         EndContainer(),
00977         NWidget(NWID_VERTICAL),
00978           NWidget(NWID_SPACER), SetFill(0, 1),
00979           NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
00980         EndContainer(),
00981       EndContainer(),
00982     EndContainer(),
00983   EndContainer(),
00984 };
00985 
00986 static const WindowDesc _network_game_window_desc(
00987   WDP_CENTER, 1000, 730,
00988   WC_NETWORK_WINDOW, WC_NONE,
00989   WDF_UNCLICK_BUTTONS,
00990   _nested_network_game_widgets, lengthof(_nested_network_game_widgets)
00991 );
00992 
00993 void ShowNetworkGameWindow()
00994 {
00995   static bool first = true;
00996   DeleteWindowById(WC_NETWORK_WINDOW, 0);
00997 
00998   /* Only show once */
00999   if (first) {
01000     first = false;
01001     /* add all servers from the config file to our list */
01002     for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
01003       NetworkAddServer(*iter);
01004     }
01005   }
01006 
01007   new NetworkGameWindow(&_network_game_window_desc);
01008 }
01009 
01011 enum NetworkStartServerWidgets {
01012   NSSW_BACKGROUND,
01013   NSSW_GAMENAME_LABEL,
01014   NSSW_GAMENAME,          
01015   NSSW_SETPWD,            
01016   NSSW_CONNTYPE_LABEL,
01017   NSSW_CONNTYPE_BTN,      
01018   NSSW_CLIENTS_LABEL,
01019   NSSW_CLIENTS_BTND,      
01020   NSSW_CLIENTS_TXT,       
01021   NSSW_CLIENTS_BTNU,      
01022   NSSW_COMPANIES_LABEL,
01023   NSSW_COMPANIES_BTND,    
01024   NSSW_COMPANIES_TXT,     
01025   NSSW_COMPANIES_BTNU,    
01026   NSSW_SPECTATORS_LABEL,
01027   NSSW_SPECTATORS_BTND,   
01028   NSSW_SPECTATORS_TXT,    
01029   NSSW_SPECTATORS_BTNU,   
01030 
01031   NSSW_LANGUAGE_LABEL,
01032   NSSW_LANGUAGE_BTN,      
01033 
01034   NSSW_GENERATE_GAME,     
01035   NSSW_LOAD_GAME,         
01036   NSSW_PLAY_SCENARIO,     
01037   NSSW_PLAY_HEIGHTMAP,    
01038 
01039   NSSW_CANCEL,            
01040 };
01041 
01042 struct NetworkStartServerWindow : public QueryStringBaseWindow {
01043   byte field;                  
01044   byte widget_id;              
01045 
01046   NetworkStartServerWindow(const WindowDesc *desc) : QueryStringBaseWindow(NETWORK_NAME_LENGTH)
01047   {
01048     this->InitNested(desc, 0);
01049 
01050     ttd_strlcpy(this->edit_str_buf, _settings_client.network.server_name, this->edit_str_size);
01051 
01052     this->afilter = CS_ALPHANUMERAL;
01053     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 160);
01054     this->SetFocusedWidget(NSSW_GAMENAME);
01055 
01056     this->field = NSSW_GAMENAME;
01057   }
01058 
01059   virtual void SetStringParameters(int widget) const
01060   {
01061     switch (widget) {
01062       case NSSW_CONNTYPE_BTN:
01063         SetDParam(0, _connection_types_dropdown[_settings_client.network.server_advertise]);
01064         break;
01065 
01066       case NSSW_CLIENTS_TXT:
01067         SetDParam(0, _settings_client.network.max_clients);
01068         break;
01069 
01070       case NSSW_COMPANIES_TXT:
01071         SetDParam(0, _settings_client.network.max_companies);
01072         break;
01073 
01074       case NSSW_SPECTATORS_TXT:
01075         SetDParam(0, _settings_client.network.max_spectators);
01076         break;
01077 
01078       case NSSW_LANGUAGE_BTN:
01079         SetDParam(0, STR_NETWORK_LANG_ANY + _settings_client.network.server_lang);
01080         break;
01081     }
01082   }
01083 
01084   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01085   {
01086     switch (widget) {
01087       case NSSW_CONNTYPE_BTN:
01088         *size = maxdim(GetStringBoundingBox(_connection_types_dropdown[0]), GetStringBoundingBox(_connection_types_dropdown[1]));
01089         size->width += padding.width;
01090         size->height += padding.height;
01091         break;
01092     }
01093   }
01094 
01095   virtual void DrawWidget(const Rect &r, int widget) const
01096   {
01097     switch (widget) {
01098       case NSSW_SETPWD:
01099         /* if password is set, draw red '*' next to 'Set password' button */
01100         if (!StrEmpty(_settings_client.network.server_password)) DrawString(r.right + WD_FRAMERECT_LEFT, this->width - WD_FRAMERECT_RIGHT, r.top, "*", TC_RED);
01101     }
01102   }
01103 
01104   virtual void OnPaint()
01105   {
01106     /* draw basic widgets */
01107     this->DrawWidgets();
01108 
01109     /* editbox to set game name */
01110     this->DrawEditBox(NSSW_GAMENAME);
01111   }
01112 
01113   virtual void OnClick(Point pt, int widget, int click_count)
01114   {
01115     this->field = widget;
01116     switch (widget) {
01117       case NSSW_CANCEL: // Cancel button
01118         ShowNetworkGameWindow();
01119         break;
01120 
01121       case NSSW_SETPWD: // Set password button
01122         this->widget_id = NSSW_SETPWD;
01123         SetDParamStr(0, _settings_client.network.server_password);
01124         ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_START_SERVER_SET_PASSWORD, 20, 250, this, CS_ALPHANUMERAL, QSF_NONE);
01125         break;
01126 
01127       case NSSW_CONNTYPE_BTN: // Connection type
01128         ShowDropDownMenu(this, _connection_types_dropdown, _settings_client.network.server_advertise, NSSW_CONNTYPE_BTN, 0, 0); // do it for widget NSSW_CONNTYPE_BTN
01129         break;
01130 
01131       case NSSW_CLIENTS_BTND:    case NSSW_CLIENTS_BTNU:    // Click on up/down button for number of clients
01132       case NSSW_COMPANIES_BTND:  case NSSW_COMPANIES_BTNU:  // Click on up/down button for number of companies
01133       case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU: // Click on up/down button for number of spectators
01134         /* Don't allow too fast scrolling */
01135         if ((this->flags4 & WF_TIMEOUT_MASK) <= WF_TIMEOUT_TRIGGER) {
01136           this->HandleButtonClick(widget);
01137           this->SetDirty();
01138           switch (widget) {
01139             default: NOT_REACHED();
01140             case NSSW_CLIENTS_BTND: case NSSW_CLIENTS_BTNU:
01141               _settings_client.network.max_clients    = Clamp(_settings_client.network.max_clients    + widget - NSSW_CLIENTS_TXT,    2, MAX_CLIENTS);
01142               break;
01143             case NSSW_COMPANIES_BTND: case NSSW_COMPANIES_BTNU:
01144               _settings_client.network.max_companies  = Clamp(_settings_client.network.max_companies  + widget - NSSW_COMPANIES_TXT,  1, MAX_COMPANIES);
01145               break;
01146             case NSSW_SPECTATORS_BTND: case NSSW_SPECTATORS_BTNU:
01147               _settings_client.network.max_spectators = Clamp(_settings_client.network.max_spectators + widget - NSSW_SPECTATORS_TXT, 0, MAX_CLIENTS);
01148               break;
01149           }
01150         }
01151         _left_button_clicked = false;
01152         break;
01153 
01154       case NSSW_CLIENTS_TXT:    // Click on number of clients
01155         this->widget_id = NSSW_CLIENTS_TXT;
01156         SetDParam(0, _settings_client.network.max_clients);
01157         ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS,    4, 50, this, CS_NUMERAL, QSF_NONE);
01158         break;
01159 
01160       case NSSW_COMPANIES_TXT:  // Click on number of companies
01161         this->widget_id = NSSW_COMPANIES_TXT;
01162         SetDParam(0, _settings_client.network.max_companies);
01163         ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES,  3, 50, this, CS_NUMERAL, QSF_NONE);
01164         break;
01165 
01166       case NSSW_SPECTATORS_TXT: // Click on number of spectators
01167         this->widget_id = NSSW_SPECTATORS_TXT;
01168         SetDParam(0, _settings_client.network.max_spectators);
01169         ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, 4, 50, this, CS_NUMERAL, QSF_NONE);
01170         break;
01171 
01172       case NSSW_LANGUAGE_BTN: { // Language
01173         uint sel = 0;
01174         for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
01175           if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _settings_client.network.server_lang) {
01176             sel = i;
01177             break;
01178           }
01179         }
01180         ShowDropDownMenu(this, _language_dropdown, sel, NSSW_LANGUAGE_BTN, 0, 0);
01181         break;
01182       }
01183 
01184       case NSSW_GENERATE_GAME: // Start game
01185         _is_network_server = true;
01186         if (_ctrl_pressed) {
01187           StartNewGameWithoutGUI(GENERATE_NEW_SEED);
01188         } else {
01189           ShowGenerateLandscape();
01190         }
01191         break;
01192 
01193       case NSSW_LOAD_GAME:
01194         _is_network_server = true;
01195         ShowSaveLoadDialog(SLD_LOAD_GAME);
01196         break;
01197 
01198       case NSSW_PLAY_SCENARIO:
01199         _is_network_server = true;
01200         ShowSaveLoadDialog(SLD_LOAD_SCENARIO);
01201         break;
01202 
01203       case NSSW_PLAY_HEIGHTMAP:
01204         _is_network_server = true;
01205         ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP);
01206         break;
01207     }
01208   }
01209 
01210   virtual void OnDropdownSelect(int widget, int index)
01211   {
01212     switch (widget) {
01213       case NSSW_CONNTYPE_BTN:
01214         _settings_client.network.server_advertise = (index != 0);
01215         break;
01216       case NSSW_LANGUAGE_BTN:
01217         _settings_client.network.server_lang = _language_dropdown[index] - STR_NETWORK_LANG_ANY;
01218         break;
01219       default:
01220         NOT_REACHED();
01221     }
01222 
01223     this->SetDirty();
01224   }
01225 
01226   virtual void OnMouseLoop()
01227   {
01228     if (this->field == NSSW_GAMENAME) this->HandleEditBox(NSSW_GAMENAME);
01229   }
01230 
01231   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01232   {
01233     EventState state = ES_NOT_HANDLED;
01234     if (this->field == NSSW_GAMENAME) {
01235       if (this->HandleEditBoxKey(NSSW_GAMENAME, key, keycode, state) == HEBR_CONFIRM) return state;
01236 
01237       strecpy(_settings_client.network.server_name, this->text.buf, lastof(_settings_client.network.server_name));
01238     }
01239 
01240     return state;
01241   }
01242 
01243   virtual void OnTimeout()
01244   {
01245     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};
01246     for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
01247       if (this->IsWidgetLowered(*widget)) {
01248         this->RaiseWidget(*widget);
01249         this->SetWidgetDirty(*widget);
01250       }
01251     }
01252   }
01253 
01254   virtual void OnQueryTextFinished(char *str)
01255   {
01256     if (str == NULL) return;
01257 
01258     if (this->widget_id == NSSW_SETPWD) {
01259       strecpy(_settings_client.network.server_password, str, lastof(_settings_client.network.server_password));
01260     } else {
01261       int32 value = atoi(str);
01262       this->SetWidgetDirty(this->widget_id);
01263       switch (this->widget_id) {
01264         default: NOT_REACHED();
01265         case NSSW_CLIENTS_TXT:    _settings_client.network.max_clients    = Clamp(value, 2, MAX_CLIENTS); break;
01266         case NSSW_COMPANIES_TXT:  _settings_client.network.max_companies  = Clamp(value, 1, MAX_COMPANIES); break;
01267         case NSSW_SPECTATORS_TXT: _settings_client.network.max_spectators = Clamp(value, 0, MAX_CLIENTS); break;
01268       }
01269     }
01270 
01271     this->SetDirty();
01272   }
01273 };
01274 
01275 static const NWidgetPart _nested_network_start_server_window_widgets[] = {
01276   NWidget(NWID_HORIZONTAL),
01277     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
01278     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_START_SERVER_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01279   EndContainer(),
01280   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NSSW_BACKGROUND),
01281     NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
01282       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
01283         NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
01284           /* Game name widgets */
01285           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_GAMENAME_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME, STR_NULL),
01286           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),
01287         EndContainer(),
01288       EndContainer(),
01289 
01290       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
01291         NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
01292           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_CONNTYPE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_CONNECTION, STR_NULL),
01293           NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, NSSW_CONNTYPE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_CONNECTION_TOOLTIP),
01294         EndContainer(),
01295         NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
01296           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_LANGUAGE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_LANGUAGE_SPOKEN, STR_NULL),
01297           NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, NSSW_LANGUAGE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_START_SERVER_LANGUAGE_TOOLTIP),
01298         EndContainer(),
01299         NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
01300           NWidget(NWID_SPACER), SetFill(1, 1),
01301           NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_SETPWD), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_SET_PASSWORD, STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP),
01302         EndContainer(),
01303       EndContainer(),
01304 
01305       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
01306         NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
01307           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_CLIENTS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, STR_NULL),
01308           NWidget(NWID_HORIZONTAL),
01309             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),
01310             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),
01311             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),
01312           EndContainer(),
01313         EndContainer(),
01314 
01315         NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
01316           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_COMPANIES_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, STR_NULL),
01317           NWidget(NWID_HORIZONTAL),
01318             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),
01319             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),
01320             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),
01321           EndContainer(),
01322         EndContainer(),
01323 
01324         NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
01325           NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, NSSW_SPECTATORS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, STR_NULL),
01326           NWidget(NWID_HORIZONTAL),
01327             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),
01328             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),
01329             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),
01330           EndContainer(),
01331         EndContainer(),
01332       EndContainer(),
01333 
01334       /* 'generate game' and 'load game' buttons */
01335       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
01336         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_GENERATE_GAME), SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetFill(1, 0),
01337         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_LOAD_GAME), SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetFill(1, 0),
01338       EndContainer(),
01339 
01340       /* 'play scenario' and 'play heightmap' buttons */
01341       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
01342         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_PLAY_SCENARIO), SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetFill(1, 0),
01343         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_PLAY_HEIGHTMAP), SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetFill(1, 0),
01344       EndContainer(),
01345 
01346       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
01347         NWidget(NWID_SPACER), SetFill(1, 0),
01348         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NSSW_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetMinimalSize(128, 12),
01349         NWidget(NWID_SPACER), SetFill(1, 0),
01350       EndContainer(),
01351     EndContainer(),
01352   EndContainer(),
01353 };
01354 
01355 static const WindowDesc _network_start_server_window_desc(
01356   WDP_CENTER, 0, 0,
01357   WC_NETWORK_WINDOW, WC_NONE,
01358   WDF_UNCLICK_BUTTONS,
01359   _nested_network_start_server_window_widgets, lengthof(_nested_network_start_server_window_widgets)
01360 );
01361 
01362 static void ShowNetworkStartServerWindow()
01363 {
01364   DeleteWindowById(WC_NETWORK_WINDOW, 0);
01365 
01366   new NetworkStartServerWindow(&_network_start_server_window_desc);
01367 }
01368 
01370 enum NetworkLobbyWindowWidgets {
01371   NLWW_BACKGROUND, 
01372   NLWW_TEXT,       
01373   NLWW_HEADER,     
01374   NLWW_MATRIX,     
01375   NLWW_SCROLLBAR,  
01376   NLWW_DETAILS,    
01377   NLWW_JOIN,       
01378   NLWW_NEW,        
01379   NLWW_SPECTATE,   
01380   NLWW_REFRESH,    
01381   NLWW_CANCEL,     
01382 };
01383 
01384 struct NetworkLobbyWindow : public Window {
01385   CompanyID company;       
01386   NetworkGameList *server; 
01387   NetworkCompanyInfo company_info[MAX_COMPANIES];
01388   Scrollbar *vscroll;
01389 
01390   NetworkLobbyWindow(const WindowDesc *desc, NetworkGameList *ngl) :
01391       Window(), company(INVALID_COMPANY), server(ngl)
01392   {
01393     this->CreateNestedTree(desc);
01394     this->vscroll = this->GetScrollbar(NLWW_SCROLLBAR);
01395     this->FinishInitNested(desc, 0);
01396     this->OnResize();
01397   }
01398 
01399   CompanyID NetworkLobbyFindCompanyIndex(byte pos) const
01400   {
01401     /* Scroll through all this->company_info and get the 'pos' item that is not empty */
01402     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01403       if (!StrEmpty(this->company_info[i].company_name)) {
01404         if (pos-- == 0) return i;
01405       }
01406     }
01407 
01408     return COMPANY_FIRST;
01409   }
01410 
01411   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01412   {
01413     switch (widget) {
01414       case NLWW_HEADER:
01415         size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
01416         break;
01417 
01418       case NLWW_MATRIX:
01419         resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
01420         size->height = 10 * resize->height;
01421         break;
01422 
01423       case NLWW_DETAILS:
01424         size->height = 30 + 11 * FONT_HEIGHT_NORMAL;
01425         break;
01426     }
01427   }
01428 
01429   virtual void SetStringParameters(int widget) const
01430   {
01431     switch (widget) {
01432       case NLWW_TEXT:
01433         SetDParamStr(0, this->server->info.server_name);
01434         break;
01435     }
01436   }
01437 
01438   virtual void DrawWidget(const Rect &r, int widget) const
01439   {
01440     switch (widget) {
01441       case NLWW_DETAILS:
01442         this->DrawDetails(r);
01443         break;
01444 
01445       case NLWW_MATRIX:
01446         this->DrawMatrix(r);
01447         break;
01448     }
01449   }
01450 
01451   virtual void OnPaint()
01452   {
01453     const NetworkGameInfo *gi = &this->server->info;
01454 
01455     /* Join button is disabled when no company is selected and for AI companies*/
01456     this->SetWidgetDisabledState(NLWW_JOIN, this->company == INVALID_COMPANY || GetLobbyCompanyInfo(this->company)->ai);
01457     /* Cannot start new company if there are too many */
01458     this->SetWidgetDisabledState(NLWW_NEW, gi->companies_on >= gi->companies_max);
01459     /* Cannot spectate if there are too many spectators */
01460     this->SetWidgetDisabledState(NLWW_SPECTATE, gi->spectators_on >= gi->spectators_max);
01461 
01462     this->vscroll->SetCount(gi->companies_on);
01463 
01464     /* Draw window widgets */
01465     this->DrawWidgets();
01466   }
01467 
01468   void DrawMatrix(const Rect &r) const
01469   {
01470     bool rtl = _current_text_dir == TD_RTL;
01471     uint left = r.left + WD_FRAMERECT_LEFT;
01472     uint right = r.right - WD_FRAMERECT_RIGHT;
01473 
01474     Dimension lock_size = GetSpriteSize(SPR_LOCK);
01475     int lock_width      = lock_size.width;
01476     int lock_y_offset   = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - lock_size.height) / 2;
01477 
01478     Dimension profit_size = GetSpriteSize(SPR_PROFIT_LOT);
01479     int profit_width      = lock_size.width;
01480     int profit_y_offset   = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - profit_size.height) / 2;
01481 
01482     uint text_left   = left  + (rtl ? lock_width + profit_width + 4 : 0);
01483     uint text_right  = right - (rtl ? 0 : lock_width + profit_width + 4);
01484     uint profit_left = rtl ? left : right - profit_width;
01485     uint lock_left   = rtl ? left + profit_width + 2 : right - profit_width - lock_width - 2;
01486 
01487     int y = r.top + WD_MATRIX_TOP;
01488     /* Draw company list */
01489     int pos = this->vscroll->GetPosition();
01490     while (pos < this->server->info.companies_on) {
01491       byte company = NetworkLobbyFindCompanyIndex(pos);
01492       bool income = false;
01493       if (this->company == company) {
01494         GfxFillRect(r.left + 1, y - 2, r.right - 1, y + FONT_HEIGHT_NORMAL, 10); // show highlighted item with a different colour
01495       }
01496 
01497       DrawString(text_left, text_right, y, this->company_info[company].company_name, TC_BLACK);
01498       if (this->company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, lock_left, y + lock_y_offset);
01499 
01500       /* If the company's income was positive puts a green dot else a red dot */
01501       if (this->company_info[company].income >= 0) income = true;
01502       DrawSprite(income ? SPR_PROFIT_LOT : SPR_PROFIT_NEGATIVE, PAL_NONE, profit_left, y + profit_y_offset);
01503 
01504       pos++;
01505       y += this->resize.step_height;
01506       if (pos >= this->vscroll->GetPosition() + this->vscroll->GetCapacity()) break;
01507     }
01508   }
01509 
01510   void DrawDetails(const Rect &r) const
01511   {
01512     const int detail_height = 12 + FONT_HEIGHT_NORMAL + 12;
01513     /* Draw info about selected company when it is selected in the left window */
01514     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, 157);
01515     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);
01516 
01517     if (this->company == INVALID_COMPANY || StrEmpty(this->company_info[this->company].company_name)) return;
01518 
01519     int y = r.top + detail_height + 4;
01520     const NetworkGameInfo *gi = &this->server->info;
01521 
01522     SetDParam(0, gi->clients_on);
01523     SetDParam(1, gi->clients_max);
01524     SetDParam(2, gi->companies_on);
01525     SetDParam(3, gi->companies_max);
01526     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
01527     y += FONT_HEIGHT_NORMAL;
01528 
01529     SetDParamStr(0, this->company_info[this->company].company_name);
01530     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_COMPANY_NAME);
01531     y += FONT_HEIGHT_NORMAL;
01532 
01533     SetDParam(0, this->company_info[this->company].inaugurated_year);
01534     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_INAUGURATION_YEAR); // inauguration year
01535     y += FONT_HEIGHT_NORMAL;
01536 
01537     SetDParam(0, this->company_info[this->company].company_value);
01538     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VALUE); // company value
01539     y += FONT_HEIGHT_NORMAL;
01540 
01541     SetDParam(0, this->company_info[this->company].money);
01542     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_CURRENT_BALANCE); // current balance
01543     y += FONT_HEIGHT_NORMAL;
01544 
01545     SetDParam(0, this->company_info[this->company].income);
01546     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_LAST_YEARS_INCOME); // last year's income
01547     y += FONT_HEIGHT_NORMAL;
01548 
01549     SetDParam(0, this->company_info[this->company].performance);
01550     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PERFORMANCE); // performance
01551     y += FONT_HEIGHT_NORMAL;
01552 
01553     SetDParam(0, this->company_info[this->company].num_vehicle[NETWORK_VEH_TRAIN]);
01554     SetDParam(1, this->company_info[this->company].num_vehicle[NETWORK_VEH_LORRY]);
01555     SetDParam(2, this->company_info[this->company].num_vehicle[NETWORK_VEH_BUS]);
01556     SetDParam(3, this->company_info[this->company].num_vehicle[NETWORK_VEH_SHIP]);
01557     SetDParam(4, this->company_info[this->company].num_vehicle[NETWORK_VEH_PLANE]);
01558     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VEHICLES); // vehicles
01559     y += FONT_HEIGHT_NORMAL;
01560 
01561     SetDParam(0, this->company_info[this->company].num_station[NETWORK_VEH_TRAIN]);
01562     SetDParam(1, this->company_info[this->company].num_station[NETWORK_VEH_LORRY]);
01563     SetDParam(2, this->company_info[this->company].num_station[NETWORK_VEH_BUS]);
01564     SetDParam(3, this->company_info[this->company].num_station[NETWORK_VEH_SHIP]);
01565     SetDParam(4, this->company_info[this->company].num_station[NETWORK_VEH_PLANE]);
01566     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_STATIONS); // stations
01567     y += FONT_HEIGHT_NORMAL;
01568 
01569     SetDParamStr(0, this->company_info[this->company].clients);
01570     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PLAYERS); // players
01571   }
01572 
01573   virtual void OnClick(Point pt, int widget, int click_count)
01574   {
01575     switch (widget) {
01576       case NLWW_CANCEL:   // Cancel button
01577         ShowNetworkGameWindow();
01578         break;
01579 
01580       case NLWW_MATRIX: { // Company list
01581         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, NLWW_MATRIX);
01582         this->company = (id_v >= this->server->info.companies_on) ? INVALID_COMPANY : NetworkLobbyFindCompanyIndex(id_v);
01583         this->SetDirty();
01584 
01585         /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
01586         if (click_count > 1 && !this->IsWidgetDisabled(NLWW_JOIN)) this->OnClick(pt, NLWW_JOIN, 1);
01587         break;
01588       }
01589 
01590       case NLWW_JOIN:     // Join company
01591         /* Button can be clicked only when it is enabled */
01592         NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), this->company);
01593         break;
01594 
01595       case NLWW_NEW:      // New company
01596         NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_NEW_COMPANY);
01597         break;
01598 
01599       case NLWW_SPECTATE: // Spectate game
01600         NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
01601         break;
01602 
01603       case NLWW_REFRESH:  // Refresh
01604         NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
01605         NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
01606         /* Clear the information so removed companies don't remain */
01607         memset(this->company_info, 0, sizeof(this->company_info));
01608         break;
01609     }
01610   }
01611 
01612   virtual void OnResize()
01613   {
01614     this->vscroll->SetCapacityFromWidget(this, NLWW_MATRIX);
01615     this->GetWidget<NWidgetCore>(NLWW_MATRIX)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
01616   }
01617 };
01618 
01619 static const NWidgetPart _nested_network_lobby_window_widgets[] = {
01620   NWidget(NWID_HORIZONTAL),
01621     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
01622     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_GAME_LOBBY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01623   EndContainer(),
01624   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NLWW_BACKGROUND),
01625     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),
01626     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
01627     NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
01628       /* Company list. */
01629       NWidget(NWID_VERTICAL),
01630         NWidget(WWT_PANEL, COLOUR_WHITE, NLWW_HEADER), SetMinimalSize(146, 0), SetResize(1, 0), SetFill(1, 0), EndContainer(),
01631         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),
01632       EndContainer(),
01633       NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, NLWW_SCROLLBAR),
01634       NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetResize(0, 1),
01635       /* Company info. */
01636       NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NLWW_DETAILS), SetMinimalSize(232, 0), SetResize(1, 1), SetFill(1, 1), EndContainer(),
01637     EndContainer(),
01638     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
01639     /* Buttons. */
01640     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 3, 10),
01641       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
01642         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),
01643         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),
01644       EndContainer(),
01645       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
01646         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),
01647         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),
01648       EndContainer(),
01649       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
01650         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NLWW_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
01651         NWidget(NWID_SPACER), SetFill(1, 1),
01652       EndContainer(),
01653     EndContainer(),
01654     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
01655   EndContainer(),
01656 };
01657 
01658 static const WindowDesc _network_lobby_window_desc(
01659   WDP_CENTER, 0, 0,
01660   WC_NETWORK_WINDOW, WC_NONE,
01661   WDF_UNCLICK_BUTTONS,
01662   _nested_network_lobby_window_widgets, lengthof(_nested_network_lobby_window_widgets)
01663 );
01664 
01669 static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
01670 {
01671   DeleteWindowById(WC_NETWORK_WINDOW, 0);
01672 
01673   NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
01674   NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
01675 
01676   new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
01677 }
01678 
01684 NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company)
01685 {
01686   NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, 0));
01687   return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL;
01688 }
01689 
01690 /* The window below gives information about the connected clients
01691  *  and also makes able to give money to them, kick them (if server)
01692  *  and stuff like that. */
01693 
01694 extern void DrawCompanyIcon(CompanyID cid, int x, int y);
01695 
01700 typedef void ClientList_Action_Proc(const NetworkClientInfo *ci);
01701 
01702 static const NWidgetPart _nested_client_list_popup_widgets[] = {
01703   NWidget(WWT_PANEL, COLOUR_GREY, 0), EndContainer(),
01704 };
01705 
01706 static const WindowDesc _client_list_popup_desc(
01707   WDP_AUTO, 0, 0,
01708   WC_CLIENT_LIST_POPUP, WC_CLIENT_LIST,
01709   0,
01710   _nested_client_list_popup_widgets, lengthof(_nested_client_list_popup_widgets)
01711 );
01712 
01713 /* Here we start to define the options out of the menu */
01714 static void ClientList_Kick(const NetworkClientInfo *ci)
01715 {
01716   NetworkServerKickClient(ci->client_id);
01717 }
01718 
01719 static void ClientList_Ban(const NetworkClientInfo *ci)
01720 {
01721   NetworkServerKickOrBanIP(ci->client_id, true);
01722 }
01723 
01724 static void ClientList_GiveMoney(const NetworkClientInfo *ci)
01725 {
01726   ShowNetworkGiveMoneyWindow(ci->client_playas);
01727 }
01728 
01729 static void ClientList_SpeakToClient(const NetworkClientInfo *ci)
01730 {
01731   ShowNetworkChatQueryWindow(DESTTYPE_CLIENT,ci->client_id);
01732 }
01733 
01734 static void ClientList_SpeakToCompany(const NetworkClientInfo *ci)
01735 {
01736   ShowNetworkChatQueryWindow(DESTTYPE_TEAM, ci->client_playas);
01737 }
01738 
01739 static void ClientList_SpeakToAll(const NetworkClientInfo *ci)
01740 {
01741   ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
01742 }
01743 
01745 struct NetworkClientListPopupWindow : Window {
01747   struct ClientListAction {
01748     StringID name;                
01749     ClientList_Action_Proc *proc; 
01750   };
01751 
01752   uint sel_index;
01753   ClientID client_id;
01754   Point desired_location;
01755   SmallVector<ClientListAction, 2> actions; 
01756 
01762   inline void AddAction(StringID name, ClientList_Action_Proc *proc)
01763   {
01764     ClientListAction *action = this->actions.Append();
01765     action->name = name;
01766     action->proc = proc;
01767   }
01768 
01769   NetworkClientListPopupWindow(const WindowDesc *desc, int x, int y, ClientID client_id) :
01770       Window(),
01771       sel_index(0), client_id(client_id)
01772   {
01773     this->desired_location.x = x;
01774     this->desired_location.y = y;
01775 
01776     const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
01777 
01778     if (_network_own_client_id != ci->client_id) {
01779       this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, &ClientList_SpeakToClient);
01780     }
01781 
01782     if (Company::IsValidID(ci->client_playas) || ci->client_playas == COMPANY_SPECTATOR) {
01783       this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_COMPANY, &ClientList_SpeakToCompany);
01784     }
01785     this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, &ClientList_SpeakToAll);
01786 
01787     if (_network_own_client_id != ci->client_id) {
01788       /* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed */
01789       if (Company::IsValidID(_local_company) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
01790         this->AddAction(STR_NETWORK_CLIENTLIST_GIVE_MONEY, &ClientList_GiveMoney);
01791       }
01792     }
01793 
01794     /* A server can kick clients (but not himself) */
01795     if (_network_server && _network_own_client_id != ci->client_id) {
01796       this->AddAction(STR_NETWORK_CLIENTLIST_KICK, &ClientList_Kick);
01797       this->AddAction(STR_NETWORK_CLIENTLIST_BAN, &ClientList_Ban);
01798     }
01799 
01800     this->flags4 &= ~WF_WHITE_BORDER_MASK;
01801     this->InitNested(desc, client_id);
01802   }
01803 
01804   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
01805   {
01806     return this->desired_location;
01807   }
01808 
01809   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01810   {
01811     Dimension d = *size;
01812     for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++) {
01813       d = maxdim(GetStringBoundingBox(action->name), d);
01814     }
01815 
01816     d.height *= this->actions.Length();
01817     d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
01818     d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
01819     *size = d;
01820   }
01821 
01822   virtual void DrawWidget(const Rect &r, int widget) const
01823   {
01824     /* Draw the actions */
01825     int sel = this->sel_index;
01826     int y = r.top + WD_FRAMERECT_TOP;
01827     for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++, y += FONT_HEIGHT_NORMAL) {
01828       TextColour colour;
01829       if (sel-- == 0) { // Selected item, highlight it
01830         GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, 0);
01831         colour = TC_WHITE;
01832       } else {
01833         colour = TC_BLACK;
01834       }
01835 
01836       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, action->name, colour);
01837     }
01838   }
01839 
01840   virtual void OnMouseLoop()
01841   {
01842     /* We selected an action */
01843     uint index = (_cursor.pos.y - this->top - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
01844 
01845     if (_left_button_down) {
01846       if (index == this->sel_index || index >= this->actions.Length()) return;
01847 
01848       this->sel_index = index;
01849       this->SetDirty();
01850     } else {
01851       if (index < this->actions.Length() && _cursor.pos.y >= this->top) {
01852         const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(this->client_id);
01853         if (ci != NULL) this->actions[index].proc(ci);
01854       }
01855 
01856       DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
01857     }
01858   }
01859 };
01860 
01864 static void PopupClientList(ClientID client_id, int x, int y)
01865 {
01866   DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
01867 
01868   if (NetworkClientInfo::GetByClientID(client_id) == NULL) return;
01869 
01870   new NetworkClientListPopupWindow(&_client_list_popup_desc, x, y, client_id);
01871 }
01872 
01873 
01875 enum ClientListWidgets {
01876   CLW_PANEL,
01877 };
01878 
01879 static const NWidgetPart _nested_client_list_widgets[] = {
01880   NWidget(NWID_HORIZONTAL),
01881     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01882     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01883     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01884   EndContainer(),
01885   NWidget(WWT_PANEL, COLOUR_GREY, CLW_PANEL), SetMinimalSize(250, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 1), EndContainer(),
01886 };
01887 
01888 static const WindowDesc _client_list_desc(
01889   WDP_AUTO, 0, 0,
01890   WC_CLIENT_LIST, WC_NONE,
01891   0,
01892   _nested_client_list_widgets, lengthof(_nested_client_list_widgets)
01893 );
01894 
01898 struct NetworkClientListWindow : Window {
01899   int selected_item;
01900 
01901   uint server_client_width;
01902   uint company_icon_width;
01903 
01904   NetworkClientListWindow(const WindowDesc *desc, WindowNumber window_number) :
01905       Window(),
01906       selected_item(-1)
01907   {
01908     this->InitNested(desc, window_number);
01909   }
01910 
01914   bool CheckClientListHeight()
01915   {
01916     int num = 0;
01917     const NetworkClientInfo *ci;
01918 
01919     /* Should be replaced with a loop through all clients */
01920     FOR_ALL_CLIENT_INFOS(ci) {
01921       if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++;
01922     }
01923 
01924     num *= FONT_HEIGHT_NORMAL;
01925 
01926     int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->GetWidget<NWidgetBase>(CLW_PANEL)->current_y);
01927     /* If height is changed */
01928     if (diff != 0) {
01929       ResizeWindow(this, 0, diff);
01930       return false;
01931     }
01932     return true;
01933   }
01934 
01935   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01936   {
01937     if (widget != CLW_PANEL) return;
01938 
01939     this->server_client_width = max(GetStringBoundingBox(STR_NETWORK_SERVER).width, GetStringBoundingBox(STR_NETWORK_CLIENT).width) + WD_FRAMERECT_RIGHT;
01940     this->company_icon_width = GetSpriteSize(SPR_COMPANY_ICON).width + WD_FRAMERECT_LEFT;
01941 
01942     uint width = 100; // Default width
01943     const NetworkClientInfo *ci;
01944     FOR_ALL_CLIENT_INFOS(ci) {
01945       width = max(width, GetStringBoundingBox(ci->client_name).width);
01946     }
01947 
01948     size->width = WD_FRAMERECT_LEFT + this->server_client_width + this->company_icon_width + width + WD_FRAMERECT_RIGHT;
01949   }
01950 
01951   virtual void OnPaint()
01952   {
01953     /* Check if we need to reset the height */
01954     if (!this->CheckClientListHeight()) return;
01955 
01956     this->DrawWidgets();
01957   }
01958 
01959   virtual void DrawWidget(const Rect &r, int widget) const
01960   {
01961     if (widget != CLW_PANEL) return;
01962 
01963     bool rtl = _current_text_dir == TD_RTL;
01964     int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - 10) / 2;
01965     uint y = r.top + WD_FRAMERECT_TOP;
01966     uint left = r.left + WD_FRAMERECT_LEFT;
01967     uint right = r.right - WD_FRAMERECT_RIGHT;
01968     uint type_icon_width = this->server_client_width + this->company_icon_width;
01969 
01970 
01971     uint type_left  = rtl ? right - this->server_client_width : left;
01972     uint type_right = rtl ? right : left + this->server_client_width - 1;
01973     uint icon_left  = rtl ? right - type_icon_width + WD_FRAMERECT_LEFT : left + this->server_client_width;
01974     uint name_left  = rtl ? left : left + type_icon_width;
01975     uint name_right = rtl ? right - type_icon_width : right;
01976 
01977     int i = 0;
01978     const NetworkClientInfo *ci;
01979     FOR_ALL_CLIENT_INFOS(ci) {
01980       TextColour colour;
01981       if (this->selected_item == i++) { // Selected item, highlight it
01982         GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, 0);
01983         colour = TC_WHITE;
01984       } else {
01985         colour = TC_BLACK;
01986       }
01987 
01988       if (ci->client_id == CLIENT_ID_SERVER) {
01989         DrawString(type_left, type_right, y, STR_NETWORK_SERVER, colour);
01990       } else {
01991         DrawString(type_left, type_right, y, STR_NETWORK_CLIENT, colour);
01992       }
01993 
01994       /* Filter out spectators */
01995       if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon(ci->client_playas, icon_left, y + icon_y_offset);
01996 
01997       DrawString(name_left, name_right, y, ci->client_name, colour);
01998 
01999       y += FONT_HEIGHT_NORMAL;
02000     }
02001   }
02002 
02003   virtual void OnClick(Point pt, int widget, int click_count)
02004   {
02005     /* Show the popup with option */
02006     if (this->selected_item != -1) {
02007       NetworkClientInfo *ci;
02008 
02009       int client_no = this->selected_item;
02010       FOR_ALL_CLIENT_INFOS(ci) {
02011         if (client_no == 0) break;
02012         client_no--;
02013       }
02014 
02015       if (ci != NULL) PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top);
02016     }
02017   }
02018 
02019   virtual void OnMouseOver(Point pt, int widget)
02020   {
02021     /* -1 means we left the current window */
02022     if (pt.y == -1) {
02023       this->selected_item = -1;
02024       this->SetDirty();
02025       return;
02026     }
02027 
02028     /* Find the new selected item (if any) */
02029     pt.y -= this->GetWidget<NWidgetBase>(CLW_PANEL)->pos_y;
02030     int item = -1;
02031     if (IsInsideMM(pt.y, WD_FRAMERECT_TOP, this->GetWidget<NWidgetBase>(CLW_PANEL)->current_y - WD_FRAMERECT_BOTTOM)) {
02032       item = (pt.y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
02033     }
02034 
02035     /* It did not change.. no update! */
02036     if (item == this->selected_item) return;
02037     this->selected_item = item;
02038 
02039     /* Repaint */
02040     this->SetDirty();
02041   }
02042 };
02043 
02044 void ShowClientList()
02045 {
02046   AllocateWindowDescFront<NetworkClientListWindow>(&_client_list_desc, 0);
02047 }
02048 
02049 /* Vars needed for the join-GUI */
02050 NetworkJoinStatus _network_join_status;
02051 uint8 _network_join_waiting;
02052 uint32 _network_join_bytes;
02053 uint32 _network_join_bytes_total;
02054 
02056 enum NetworkJoinStatusWidgets {
02057   NJSW_BACKGROUND, 
02058   NJSW_CANCELOK,   
02059 };
02060 
02061 struct NetworkJoinStatusWindow : Window {
02062   NetworkPasswordType password_type;
02063 
02064   NetworkJoinStatusWindow(const WindowDesc *desc) : Window()
02065   {
02066     this->parent = FindWindowById(WC_NETWORK_WINDOW, 0);
02067     this->InitNested(desc, 0);
02068   }
02069 
02070   virtual void DrawWidget(const Rect &r, int widget) const
02071   {
02072     if (widget != NJSW_BACKGROUND) return;
02073 
02074     uint8 progress; // used for progress bar
02075     DrawString(r.left + 2, r.right - 2, r.top + 20, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_FROMSTRING, SA_HOR_CENTER);
02076     switch (_network_join_status) {
02077       case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
02078       case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
02079         progress = 10; // first two stages 10%
02080         break;
02081       case NETWORK_JOIN_STATUS_WAITING:
02082         SetDParam(0, _network_join_waiting);
02083         DrawString(r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, STR_NETWORK_CONNECTING_WAITING, TC_FROMSTRING, SA_HOR_CENTER);
02084         progress = 15; // third stage is 15%
02085         break;
02086       case NETWORK_JOIN_STATUS_DOWNLOADING:
02087         SetDParam(0, _network_join_bytes);
02088         SetDParam(1, _network_join_bytes_total);
02089         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);
02090         if (_network_join_bytes_total == 0) {
02091           progress = 15; // We don't have the final size yet; the server is still compressing!
02092           break;
02093         }
02094         /* FALL THROUGH */
02095       default: // Waiting is 15%, so the resting receivement of map is maximum 70%
02096         progress = 15 + _network_join_bytes * (100 - 15) / _network_join_bytes_total;
02097     }
02098 
02099     /* Draw nice progress bar :) */
02100     DrawFrameRect(r.left + 20, r.top + 5, (int)((this->width - 20) * progress / 100), r.top + 15, COLOUR_MAUVE, FR_NONE);
02101   }
02102 
02103   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
02104   {
02105     if (widget != NJSW_BACKGROUND) return;
02106 
02107     size->height = 25 + 2 * FONT_HEIGHT_NORMAL;
02108 
02109     /* Account for the statuses */
02110     uint width = 0;
02111     for (uint i = 0; i < NETWORK_JOIN_STATUS_END; i++) {
02112       width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_1 + i).width);
02113     }
02114 
02115     /* For the number of waiting (other) players */
02116     SetDParam(0, 255);
02117     width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_WAITING).width);
02118 
02119     /* Account for downloading ~ 10 MiB */
02120     SetDParam(0, 10000000);
02121     SetDParam(1, 10000000);
02122     width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_1).width);
02123     width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_2).width);
02124 
02125     /* Give a bit more clearing for the widest strings than strictly needed */
02126     size->width = width + WD_FRAMERECT_LEFT + WD_FRAMERECT_BOTTOM + 10;
02127   }
02128 
02129   virtual void OnClick(Point pt, int widget, int click_count)
02130   {
02131     if (widget == NJSW_CANCELOK) { // Disconnect button
02132       NetworkDisconnect();
02133       SwitchToMode(SM_MENU);
02134       ShowNetworkGameWindow();
02135     }
02136   }
02137 
02138   virtual void OnQueryTextFinished(char *str)
02139   {
02140     if (StrEmpty(str)) {
02141       NetworkDisconnect();
02142       ShowNetworkGameWindow();
02143       return;
02144     }
02145 
02146     switch (this->password_type) {
02147       case NETWORK_GAME_PASSWORD:    MyClient::SendGamePassword   (str); break;
02148       case NETWORK_COMPANY_PASSWORD: MyClient::SendCompanyPassword(str); break;
02149       default: NOT_REACHED();
02150     }
02151   }
02152 };
02153 
02154 static const NWidgetPart _nested_network_join_status_window_widgets[] = {
02155   NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_CONNECTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02156   NWidget(WWT_PANEL, COLOUR_GREY),
02157     NWidget(WWT_EMPTY, COLOUR_GREY, NJSW_BACKGROUND),
02158     NWidget(NWID_HORIZONTAL),
02159       NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
02160       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NJSW_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_NETWORK_CONNECTION_DISCONNECT, STR_NULL),
02161       NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
02162     EndContainer(),
02163     NWidget(NWID_SPACER), SetMinimalSize(0, 4),
02164   EndContainer(),
02165 };
02166 
02167 static const WindowDesc _network_join_status_window_desc(
02168   WDP_CENTER, 0, 0,
02169   WC_NETWORK_STATUS_WINDOW, WC_NONE,
02170   WDF_MODAL,
02171   _nested_network_join_status_window_widgets, lengthof(_nested_network_join_status_window_widgets)
02172 );
02173 
02174 void ShowJoinStatusWindow()
02175 {
02176   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
02177   new NetworkJoinStatusWindow(&_network_join_status_window_desc);
02178 }
02179 
02180 void ShowNetworkNeedPassword(NetworkPasswordType npt)
02181 {
02182   NetworkJoinStatusWindow *w = (NetworkJoinStatusWindow *)FindWindowById(WC_NETWORK_STATUS_WINDOW, 0);
02183   if (w == NULL) return;
02184   w->password_type = npt;
02185 
02186   StringID caption;
02187   switch (npt) {
02188     default: NOT_REACHED();
02189     case NETWORK_GAME_PASSWORD:    caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION; break;
02190     case NETWORK_COMPANY_PASSWORD: caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION; break;
02191   }
02192   ShowQueryString(STR_EMPTY, caption, NETWORK_PASSWORD_LENGTH, 180, w, CS_ALPHANUMERAL, QSF_NONE);
02193 }
02194 
02195 
02197 enum NetworkCompanyPasswordWindowWidgets {
02198   NCPWW_BACKGROUND,               
02199   NCPWW_LABEL,                    
02200   NCPWW_PASSWORD,                 
02201   NCPWW_SAVE_AS_DEFAULT_PASSWORD, 
02202   NCPWW_CANCEL,                   
02203   NCPWW_OK,                       
02204 };
02205 
02206 struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
02207   NetworkCompanyPasswordWindow(const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(lengthof(_settings_client.network.default_company_pass))
02208   {
02209     this->InitNested(desc, 0);
02210 
02211     this->parent = parent;
02212     this->afilter = CS_ALPHANUMERAL;
02213     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0);
02214     this->SetFocusedWidget(NCPWW_PASSWORD);
02215   }
02216 
02217   void OnOk()
02218   {
02219     if (this->IsWidgetLowered(NCPWW_SAVE_AS_DEFAULT_PASSWORD)) {
02220       snprintf(_settings_client.network.default_company_pass, lengthof(_settings_client.network.default_company_pass), "%s", this->edit_str_buf);
02221     }
02222 
02223     NetworkChangeCompanyPassword(_local_company, this->edit_str_buf);
02224   }
02225 
02226   virtual void OnPaint()
02227   {
02228     this->DrawWidgets();
02229     this->DrawEditBox(NCPWW_PASSWORD);
02230   }
02231 
02232   virtual void OnClick(Point pt, int widget, int click_count)
02233   {
02234     switch (widget) {
02235       case NCPWW_OK:
02236         this->OnOk();
02237         /* FALL THROUGH */
02238 
02239       case NCPWW_CANCEL:
02240         delete this;
02241         break;
02242 
02243       case NCPWW_SAVE_AS_DEFAULT_PASSWORD:
02244         this->ToggleWidgetLoweredState(NCPWW_SAVE_AS_DEFAULT_PASSWORD);
02245         this->SetDirty();
02246         break;
02247     }
02248   }
02249 
02250   virtual void OnMouseLoop()
02251   {
02252     this->HandleEditBox(4);
02253   }
02254 
02255   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
02256   {
02257     EventState state = ES_NOT_HANDLED;
02258     switch (this->HandleEditBoxKey(NCPWW_PASSWORD, key, keycode, state)) {
02259       default: break;
02260 
02261       case HEBR_CONFIRM:
02262         this->OnOk();
02263         /* FALL THROUGH */
02264 
02265       case HEBR_CANCEL:
02266         delete this;
02267         break;
02268     }
02269     return state;
02270   }
02271 
02272   virtual void OnOpenOSKWindow(int wid)
02273   {
02274     ShowOnScreenKeyboard(this, wid, NCPWW_CANCEL, NCPWW_OK);
02275   }
02276 };
02277 
02278 static const NWidgetPart _nested_network_company_password_window_widgets[] = {
02279   NWidget(NWID_HORIZONTAL),
02280     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
02281     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_PASSWORD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02282   EndContainer(),
02283   NWidget(WWT_PANEL, COLOUR_GREY, NCPWW_BACKGROUND),
02284     NWidget(NWID_VERTICAL), SetPIP(5, 5, 5),
02285       NWidget(NWID_HORIZONTAL), SetPIP(5, 5, 5),
02286         NWidget(WWT_TEXT, COLOUR_GREY, NCPWW_LABEL), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_NULL),
02287         NWidget(WWT_EDITBOX, COLOUR_GREY, NCPWW_PASSWORD), SetMinimalSize(194, 12), SetDataTip(STR_COMPANY_VIEW_SET_PASSWORD, STR_NULL),
02288       EndContainer(),
02289       NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
02290         NWidget(NWID_SPACER), SetFill(1, 0),
02291         NWidget(WWT_TEXTBTN, COLOUR_GREY, NCPWW_SAVE_AS_DEFAULT_PASSWORD), SetMinimalSize(194, 12),
02292                       SetDataTip(STR_COMPANY_PASSWORD_MAKE_DEFAULT, STR_COMPANY_PASSWORD_MAKE_DEFAULT_TOOLTIP),
02293       EndContainer(),
02294     EndContainer(),
02295   EndContainer(),
02296   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
02297     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, NCPWW_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_COMPANY_PASSWORD_CANCEL),
02298     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, NCPWW_OK), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_COMPANY_PASSWORD_OK),
02299   EndContainer(),
02300 };
02301 
02302 static const WindowDesc _network_company_password_window_desc(
02303   WDP_AUTO, 0, 0,
02304   WC_COMPANY_PASSWORD_WINDOW, WC_NONE,
02305   WDF_UNCLICK_BUTTONS,
02306   _nested_network_company_password_window_widgets, lengthof(_nested_network_company_password_window_widgets)
02307 );
02308 
02309 void ShowNetworkCompanyPasswordWindow(Window *parent)
02310 {
02311   DeleteWindowById(WC_COMPANY_PASSWORD_WINDOW, 0);
02312 
02313   new NetworkCompanyPasswordWindow(&_network_company_password_window_desc, parent);
02314 }
02315 
02316 #endif /* ENABLE_NETWORK */

Generated on Sun May 15 19:20:10 2011 for OpenTTD by  doxygen 1.6.1