network_gui.cpp

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

Generated on Fri May 27 04:19:44 2011 for OpenTTD by  doxygen 1.6.1