network_gui.cpp

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

Generated on Thu Jan 20 22:57:36 2011 for OpenTTD by  doxygen 1.6.1