network_gui.cpp

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