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