town_gui.cpp

Go to the documentation of this file.
00001 /* $Id: town_gui.cpp 25978 2013-11-13 21:21:47Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "town.h"
00014 #include "viewport_func.h"
00015 #include "error.h"
00016 #include "gui.h"
00017 #include "command_func.h"
00018 #include "company_func.h"
00019 #include "company_base.h"
00020 #include "company_gui.h"
00021 #include "network/network.h"
00022 #include "string_func.h"
00023 #include "strings_func.h"
00024 #include "sound_func.h"
00025 #include "tilehighlight_func.h"
00026 #include "sortlist_type.h"
00027 #include "road_cmd.h"
00028 #include "landscape.h"
00029 #include "querystring_gui.h"
00030 #include "window_func.h"
00031 #include "townname_func.h"
00032 #include "core/geometry_func.hpp"
00033 #include "genworld.h"
00034 
00035 #include "widgets/town_widget.h"
00036 
00037 #include "table/strings.h"
00038 
00039 typedef GUIList<const Town*> GUITownList;
00040 
00041 static const NWidgetPart _nested_town_authority_widgets[] = {
00042   NWidget(NWID_HORIZONTAL),
00043     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00044     NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00045     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00046     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00047   EndContainer(),
00048   NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
00049   NWidget(NWID_HORIZONTAL),
00050     NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_COMMAND_LIST), SetMinimalSize(305, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP), SetScrollbar(WID_TA_SCROLLBAR), EndContainer(),
00051     NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TA_SCROLLBAR),
00052   EndContainer(),
00053   NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
00054   NWidget(NWID_HORIZONTAL),
00055     NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TA_EXECUTE),  SetMinimalSize(317, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_LOCAL_AUTHORITY_DO_IT_BUTTON, STR_LOCAL_AUTHORITY_DO_IT_TOOLTIP),
00056     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00057   EndContainer()
00058 };
00059 
00061 struct TownAuthorityWindow : Window {
00062 private:
00063   Town *town;    
00064   int sel_index; 
00065   Scrollbar *vscroll;
00066   uint displayed_actions_on_previous_painting; 
00067 
00077   static int GetNthSetBit(uint32 bits, int n)
00078   {
00079     if (n >= 0) {
00080       uint i;
00081       FOR_EACH_SET_BIT(i, bits) {
00082         n--;
00083         if (n < 0) return i;
00084       }
00085     }
00086     return -1;
00087   }
00088 
00089 public:
00090   TownAuthorityWindow(const WindowDesc *desc, WindowNumber window_number) : Window(), sel_index(-1), displayed_actions_on_previous_painting(0)
00091   {
00092     this->town = Town::Get(window_number);
00093     this->InitNested(desc, window_number);
00094     this->vscroll = this->GetScrollbar(WID_TA_SCROLLBAR);
00095     this->vscroll->SetCapacity((this->GetWidget<NWidgetBase>(WID_TA_COMMAND_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
00096   }
00097 
00098   virtual void OnPaint()
00099   {
00100     int numact;
00101     uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
00102     if (buttons != displayed_actions_on_previous_painting) this->SetDirty();
00103     displayed_actions_on_previous_painting = buttons;
00104 
00105     this->vscroll->SetCount(numact + 1);
00106 
00107     if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
00108       this->sel_index = -1;
00109     }
00110 
00111     this->SetWidgetDisabledState(WID_TA_EXECUTE, this->sel_index == -1);
00112 
00113     this->DrawWidgets();
00114     if (!this->IsShaded()) this->DrawRatings();
00115   }
00116 
00118   void DrawRatings()
00119   {
00120     NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_RATING_INFO);
00121     uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
00122     uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
00123 
00124     uint y = nwid->pos_y + WD_FRAMERECT_TOP;
00125 
00126     DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
00127     y += FONT_HEIGHT_NORMAL;
00128 
00129     Dimension icon_size = GetSpriteSize(SPR_COMPANY_ICON);
00130     int icon_width      = icon_size.width;
00131     int icon_y_offset   = (FONT_HEIGHT_NORMAL - icon_size.height) / 2;
00132 
00133     Dimension exclusive_size = GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT);
00134     int exclusive_width      = exclusive_size.width;
00135     int exclusive_y_offset   = (FONT_HEIGHT_NORMAL - exclusive_size.height) / 2;
00136 
00137     bool rtl = _current_text_dir == TD_RTL;
00138     uint text_left      = left  + (rtl ? 0 : icon_width + exclusive_width + 4);
00139     uint text_right     = right - (rtl ? icon_width + exclusive_width + 4 : 0);
00140     uint icon_left      = rtl ? right - icon_width : left;
00141     uint exclusive_left = rtl ? right - icon_width - exclusive_width - 2 : left + icon_width + 2;
00142 
00143     /* Draw list of companies */
00144     const Company *c;
00145     FOR_ALL_COMPANIES(c) {
00146       if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
00147         DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
00148 
00149         SetDParam(0, c->index);
00150         SetDParam(1, c->index);
00151 
00152         int r = this->town->ratings[c->index];
00153         StringID str;
00154         (str = STR_CARGO_RATING_APPALLING, r <= RATING_APPALLING) || // Apalling
00155         (str++,                    r <= RATING_VERYPOOR)  || // Very Poor
00156         (str++,                    r <= RATING_POOR)      || // Poor
00157         (str++,                    r <= RATING_MEDIOCRE)  || // Mediocore
00158         (str++,                    r <= RATING_GOOD)      || // Good
00159         (str++,                    r <= RATING_VERYGOOD)  || // Very Good
00160         (str++,                    r <= RATING_EXCELLENT) || // Excellent
00161         (str++,                    true);                    // Outstanding
00162 
00163         SetDParam(2, str);
00164         if (this->town->exclusivity == c->index) {
00165           DrawSprite(SPR_EXCLUSIVE_TRANSPORT, COMPANY_SPRITE_COLOUR(c->index), exclusive_left, y + exclusive_y_offset);
00166         }
00167 
00168         DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
00169         y += FONT_HEIGHT_NORMAL;
00170       }
00171     }
00172 
00173     y = y + WD_FRAMERECT_BOTTOM - nwid->pos_y; // Compute needed size of the widget.
00174     if (y > nwid->current_y) {
00175       /* If the company list is too big to fit, mark ourself dirty and draw again. */
00176       ResizeWindow(this, 0, y - nwid->current_y);
00177     }
00178   }
00179 
00180   virtual void SetStringParameters(int widget) const
00181   {
00182     if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
00183   }
00184 
00185   virtual void DrawWidget(const Rect &r, int widget) const
00186   {
00187     switch (widget) {
00188       case WID_TA_ACTION_INFO:
00189         if (this->sel_index != -1) {
00190           SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8);
00191           DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00192                 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
00193         }
00194         break;
00195       case WID_TA_COMMAND_LIST: {
00196         int numact;
00197         uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
00198         int y = r.top + WD_FRAMERECT_TOP;
00199         int pos = this->vscroll->GetPosition();
00200 
00201         if (--pos < 0) {
00202           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
00203           y += FONT_HEIGHT_NORMAL;
00204         }
00205 
00206         for (int i = 0; buttons; i++, buttons >>= 1) {
00207           if (pos <= -5) break; 
00208 
00209           if ((buttons & 1) && --pos < 0) {
00210             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y,
00211                 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, this->sel_index == i ? TC_WHITE : TC_ORANGE);
00212             y += FONT_HEIGHT_NORMAL;
00213           }
00214         }
00215         break;
00216       }
00217     }
00218   }
00219 
00220   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00221   {
00222     switch (widget) {
00223       case WID_TA_ACTION_INFO: {
00224         assert(size->width > padding.width && size->height > padding.height);
00225         size->width -= WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00226         size->height -= WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00227         Dimension d = {0, 0};
00228         for (int i = 0; i < TACT_COUNT; i++) {
00229           SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
00230           d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
00231         }
00232         *size = maxdim(*size, d);
00233         size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00234         size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00235         break;
00236       }
00237 
00238       case WID_TA_COMMAND_LIST:
00239         size->height = WD_FRAMERECT_TOP + 5 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
00240         size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
00241         for (uint i = 0; i < TACT_COUNT; i++ ) {
00242           size->width = max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
00243         }
00244         size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00245         break;
00246 
00247       case WID_TA_RATING_INFO:
00248         resize->height = FONT_HEIGHT_NORMAL;
00249         size->height = WD_FRAMERECT_TOP + 9 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
00250         break;
00251     }
00252   }
00253 
00254   virtual void OnClick(Point pt, int widget, int click_count)
00255   {
00256     switch (widget) {
00257       case WID_TA_COMMAND_LIST: {
00258         int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL);
00259         if (!IsInsideMM(y, 0, 5)) return;
00260 
00261         y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_company, this->town), y + this->vscroll->GetPosition() - 1);
00262         if (y >= 0) {
00263           this->sel_index = y;
00264           this->SetDirty();
00265         }
00266         /* FALL THROUGH, when double-clicking. */
00267         if (click_count == 1 || y < 0) break;
00268       }
00269 
00270       case WID_TA_EXECUTE:
00271         DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_ERROR_CAN_T_DO_THIS));
00272         break;
00273     }
00274   }
00275 
00276   virtual void OnHundredthTick()
00277   {
00278     this->SetDirty();
00279   }
00280 };
00281 
00282 static const WindowDesc _town_authority_desc(
00283   WDP_AUTO, 317, 222,
00284   WC_TOWN_AUTHORITY, WC_NONE,
00285   0,
00286   _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
00287 );
00288 
00289 static void ShowTownAuthorityWindow(uint town)
00290 {
00291   AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
00292 }
00293 
00294 
00295 /* Town view window. */
00296 struct TownViewWindow : Window {
00297 private:
00298   Town *town; 
00299 
00300 public:
00301   static const int WID_TV_HEIGHT_NORMAL = 150;
00302 
00303   TownViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00304   {
00305     this->CreateNestedTree(desc);
00306 
00307     this->town = Town::Get(window_number);
00308     if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
00309 
00310     this->FinishInitNested(desc, window_number);
00311 
00312     this->flags |= WF_DISABLE_VP_SCROLL;
00313     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
00314     nvp->InitializeViewport(this, this->town->xy, ZOOM_LVL_NEWS);
00315 
00316     /* disable renaming town in network games if you are not the server */
00317     this->SetWidgetDisabledState(WID_TV_CHANGE_NAME, _networking && !_network_server);
00318   }
00319 
00320   virtual void SetStringParameters(int widget) const
00321   {
00322     if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
00323   }
00324 
00325   virtual void DrawWidget(const Rect &r, int widget) const
00326   {
00327     if (widget != WID_TV_INFO) return;
00328 
00329     uint y = r.top + WD_FRAMERECT_TOP;
00330 
00331     SetDParam(0, this->town->cache.population);
00332     SetDParam(1, this->town->cache.num_houses);
00333     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
00334 
00335     SetDParam(0, this->town->supplied[CT_PASSENGERS].old_act);
00336     SetDParam(1, this->town->supplied[CT_PASSENGERS].old_max);
00337     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_PASSENGERS_LAST_MONTH_MAX);
00338 
00339     SetDParam(0, this->town->supplied[CT_MAIL].old_act);
00340     SetDParam(1, this->town->supplied[CT_MAIL].old_max);
00341     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_MAIL_LAST_MONTH_MAX);
00342 
00343     bool first = true;
00344     for (int i = TE_BEGIN; i < TE_END; i++) {
00345       if (this->town->goal[i] == 0) continue;
00346       if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
00347       if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
00348 
00349       if (first) {
00350         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
00351         first = false;
00352       }
00353 
00354       bool rtl = _current_text_dir == TD_RTL;
00355       uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
00356       uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
00357 
00358       const CargoSpec *cargo = FindFirstCargoWithTownEffect((TownEffect)i);
00359       assert(cargo != NULL);
00360 
00361       StringID string;
00362 
00363       if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
00364         /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
00365         string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
00366         if (this->town->received[i].old_act == 0) {
00367           string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
00368 
00369           if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
00370             string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
00371           }
00372         }
00373 
00374         SetDParam(0, cargo->name);
00375       } else {
00376         string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
00377         if (this->town->received[i].old_act < this->town->goal[i]) {
00378           string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
00379         }
00380 
00381         SetDParam(0, cargo->Index());
00382         SetDParam(1, this->town->received[i].old_act);
00383         SetDParam(2, cargo->Index());
00384         SetDParam(3, this->town->goal[i]);
00385       }
00386       DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
00387     }
00388 
00389     if (HasBit(this->town->flags, TOWN_IS_FUNDED)) {
00390       SetDParam(0, ((this->town->growth_rate & (~TOWN_GROW_RATE_CUSTOM)) * TOWN_GROWTH_TICKS + DAY_TICKS) / DAY_TICKS);
00391       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, this->town->fund_buildings_months == 0 ? STR_TOWN_VIEW_TOWN_GROWS_EVERY : STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED);
00392     } else {
00393       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
00394     }
00395 
00396     /* only show the town noise, if the noise option is activated. */
00397     if (_settings_game.economy.station_noise_level) {
00398       SetDParam(0, this->town->noise_reached);
00399       SetDParam(1, this->town->MaxTownNoise());
00400       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
00401     }
00402 
00403     if (this->town->text != NULL) {
00404       SetDParamStr(0, this->town->text);
00405       DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
00406     }
00407   }
00408 
00409   virtual void OnClick(Point pt, int widget, int click_count)
00410   {
00411     switch (widget) {
00412       case WID_TV_CENTER_VIEW: // scroll to location
00413         if (_ctrl_pressed) {
00414           ShowExtraViewPortWindow(this->town->xy);
00415         } else {
00416           ScrollMainWindowToTile(this->town->xy);
00417         }
00418         break;
00419 
00420       case WID_TV_SHOW_AUTHORITY: // town authority
00421         ShowTownAuthorityWindow(this->window_number);
00422         break;
00423 
00424       case WID_TV_CHANGE_NAME: // rename
00425         SetDParam(0, this->window_number);
00426         ShowQueryString(STR_TOWN_NAME, STR_TOWN_VIEW_RENAME_TOWN_BUTTON, MAX_LENGTH_TOWN_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00427         break;
00428 
00429       case WID_TV_EXPAND: { // expand town - only available on Scenario editor
00430         /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
00431         static bool _warn_town_no_roads = false;
00432 
00433         if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
00434           ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
00435           _warn_town_no_roads = true;
00436         }
00437 
00438         DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
00439         break;
00440       }
00441 
00442       case WID_TV_DELETE: // delete town - only available on Scenario editor
00443         DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
00444         break;
00445     }
00446   }
00447 
00448   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00449   {
00450     switch (widget) {
00451       case WID_TV_INFO:
00452         size->height = GetDesiredInfoHeight(size->width);
00453         break;
00454     }
00455   }
00456 
00461   uint GetDesiredInfoHeight(int width) const
00462   {
00463     uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00464 
00465     bool first = true;
00466     for (int i = TE_BEGIN; i < TE_END; i++) {
00467       if (this->town->goal[i] == 0) continue;
00468       if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
00469       if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
00470 
00471       if (first) {
00472         aimed_height += FONT_HEIGHT_NORMAL;
00473         first = false;
00474       }
00475       aimed_height += FONT_HEIGHT_NORMAL;
00476     }
00477     aimed_height += FONT_HEIGHT_NORMAL;
00478 
00479     if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
00480 
00481     if (this->town->text != NULL) {
00482       SetDParamStr(0, this->town->text);
00483       aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width);
00484     }
00485 
00486     return aimed_height;
00487   }
00488 
00489   void ResizeWindowAsNeeded()
00490   {
00491     const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
00492     uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
00493     if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
00494       this->ReInit();
00495     }
00496   }
00497 
00498   virtual void OnResize()
00499   {
00500     if (this->viewport != NULL) {
00501       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
00502       nvp->UpdateViewportCoordinates(this);
00503 
00504       ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
00505     }
00506   }
00507 
00513   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00514   {
00515     if (!gui_scope) return;
00516     /* Called when setting station noise or required cargoes have changed, in order to resize the window */
00517     this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
00518     this->ResizeWindowAsNeeded();
00519   }
00520 
00521   virtual void OnQueryTextFinished(char *str)
00522   {
00523     if (str == NULL) return;
00524 
00525     DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), NULL, str);
00526   }
00527 };
00528 
00529 static const NWidgetPart _nested_town_game_view_widgets[] = {
00530   NWidget(NWID_HORIZONTAL),
00531     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00532     NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00533     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00534     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00535   EndContainer(),
00536   NWidget(WWT_PANEL, COLOUR_BROWN),
00537     NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00538       NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00539     EndContainer(),
00540   EndContainer(),
00541   NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00542   NWidget(NWID_HORIZONTAL),
00543     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00544       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
00545       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_SHOW_AUTHORITY), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP),
00546       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
00547     EndContainer(),
00548     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00549   EndContainer(),
00550 };
00551 
00552 static const WindowDesc _town_game_view_desc(
00553   WDP_AUTO, 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
00554   WC_TOWN_VIEW, WC_NONE,
00555   0,
00556   _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
00557 );
00558 
00559 static const NWidgetPart _nested_town_editor_view_widgets[] = {
00560   NWidget(NWID_HORIZONTAL),
00561     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00562     NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00563     NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
00564     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00565     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00566   EndContainer(),
00567   NWidget(WWT_PANEL, COLOUR_BROWN),
00568     NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00569       NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00570     EndContainer(),
00571   EndContainer(),
00572   NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00573   NWidget(NWID_HORIZONTAL),
00574     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00575       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
00576       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_EXPAND), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_EXPAND_BUTTON, STR_TOWN_VIEW_EXPAND_TOOLTIP),
00577       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_DELETE), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_TOWN_VIEW_DELETE_TOOLTIP),
00578     EndContainer(),
00579     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00580   EndContainer(),
00581 };
00582 
00583 static const WindowDesc _town_editor_view_desc(
00584   WDP_AUTO, 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
00585   WC_TOWN_VIEW, WC_NONE,
00586   0,
00587   _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
00588 );
00589 
00590 void ShowTownViewWindow(TownID town)
00591 {
00592   if (_game_mode == GM_EDITOR) {
00593     AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
00594   } else {
00595     AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
00596   }
00597 }
00598 
00599 static const NWidgetPart _nested_town_directory_widgets[] = {
00600   NWidget(NWID_HORIZONTAL),
00601     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00602     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00603     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00604     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00605   EndContainer(),
00606   NWidget(NWID_HORIZONTAL),
00607     NWidget(NWID_VERTICAL),
00608       NWidget(NWID_HORIZONTAL),
00609         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TD_SORT_NAME), SetMinimalSize(99, 12), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00610         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TD_SORT_POPULATION), SetMinimalSize(97, 12), SetDataTip(STR_SORT_BY_CAPTION_POPULATION, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00611       EndContainer(),
00612       NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
00613               SetFill(1, 0), SetResize(0, 10), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
00614       NWidget(WWT_PANEL, COLOUR_BROWN),
00615         NWidget(WWT_TEXT, COLOUR_BROWN, WID_TD_WORLD_POPULATION), SetPadding(2, 0, 0, 2), SetMinimalSize(196, 12), SetFill(1, 0), SetDataTip(STR_TOWN_POPULATION, STR_NULL),
00616       EndContainer(),
00617     EndContainer(),
00618     NWidget(NWID_VERTICAL),
00619       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
00620       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00621     EndContainer(),
00622   EndContainer(),
00623 };
00624 
00626 struct TownDirectoryWindow : public Window {
00627 private:
00628   /* Runtime saved values */
00629   static Listing last_sorting;
00630   static const Town *last_town;
00631 
00632   /* Constants for sorting towns */
00633   static GUITownList::SortFunction * const sorter_funcs[];
00634 
00635   GUITownList towns;
00636 
00637   Scrollbar *vscroll;
00638 
00639   void BuildSortTownList()
00640   {
00641     if (this->towns.NeedRebuild()) {
00642       this->towns.Clear();
00643 
00644       const Town *t;
00645       FOR_ALL_TOWNS(t) {
00646         *this->towns.Append() = t;
00647       }
00648 
00649       this->towns.Compact();
00650       this->towns.RebuildDone();
00651       this->vscroll->SetCount(this->towns.Length()); // Update scrollbar as well.
00652     }
00653     /* Always sort the towns. */
00654     this->last_town = NULL;
00655     this->towns.Sort();
00656   }
00657 
00659   static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
00660   {
00661     static char buf_cache[64];
00662     const Town *ta = *a;
00663     const Town *tb = *b;
00664     char buf[64];
00665 
00666     SetDParam(0, ta->index);
00667     GetString(buf, STR_TOWN_NAME, lastof(buf));
00668 
00669     /* If 'b' is the same town as in the last round, use the cached value
00670      * We do this to speed stuff up ('b' is called with the same value a lot of
00671      * times after each other) */
00672     if (tb != last_town) {
00673       last_town = tb;
00674       SetDParam(0, tb->index);
00675       GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
00676     }
00677 
00678     return strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
00679   }
00680 
00682   static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
00683   {
00684     uint32 a_population = (*a)->cache.population;
00685     uint32 b_population = (*b)->cache.population;
00686     if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
00687     return (a_population < b_population) ? -1 : 1;
00688   }
00689 
00690 public:
00691   TownDirectoryWindow(const WindowDesc *desc) : Window()
00692   {
00693     this->CreateNestedTree(desc);
00694 
00695     this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
00696 
00697     this->towns.SetListing(this->last_sorting);
00698     this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
00699     this->towns.ForceRebuild();
00700     this->BuildSortTownList();
00701 
00702     this->FinishInitNested(desc, 0);
00703   }
00704 
00705   ~TownDirectoryWindow()
00706   {
00707     this->last_sorting = this->towns.GetListing();
00708   }
00709 
00710   virtual void SetStringParameters(int widget) const
00711   {
00712     if (widget == WID_TD_WORLD_POPULATION) SetDParam(0, GetWorldPopulation());
00713   }
00714 
00715   virtual void DrawWidget(const Rect &r, int widget) const
00716   {
00717     switch (widget) {
00718       case WID_TD_SORT_NAME:
00719         if (this->towns.SortType() == 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00720         break;
00721 
00722       case WID_TD_SORT_POPULATION:
00723         if (this->towns.SortType() != 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00724         break;
00725 
00726       case WID_TD_LIST: {
00727         int n = 0;
00728         int y = r.top + WD_FRAMERECT_TOP;
00729         if (this->towns.Length() == 0) { // No towns available.
00730           DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
00731           break;
00732         }
00733 
00734         /* At least one town available. */
00735         bool rtl = _current_text_dir == TD_RTL;
00736         Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
00737         int text_left  = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
00738         int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
00739         int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
00740 
00741         for (uint i = this->vscroll->GetPosition(); i < this->towns.Length(); i++) {
00742           const Town *t = this->towns[i];
00743           assert(t->xy != INVALID_TILE);
00744 
00745           /* Draw rating icon. */
00746           if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
00747             DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
00748           } else {
00749             SpriteID icon = SPR_TOWN_RATING_APALLING;
00750             if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
00751             if (t->ratings[_local_company] > RATING_GOOD)     icon = SPR_TOWN_RATING_GOOD;
00752             DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
00753           }
00754 
00755           SetDParam(0, t->index);
00756           SetDParam(1, t->cache.population);
00757           DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, STR_TOWN_DIRECTORY_TOWN);
00758 
00759           y += this->resize.step_height;
00760           if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
00761         }
00762         break;
00763       }
00764     }
00765   }
00766 
00767   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00768   {
00769     switch (widget) {
00770       case WID_TD_SORT_NAME:
00771       case WID_TD_SORT_POPULATION: {
00772         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00773         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
00774         d.height += padding.height;
00775         *size = maxdim(*size, d);
00776         break;
00777       }
00778       case WID_TD_LIST: {
00779         Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
00780         for (uint i = 0; i < this->towns.Length(); i++) {
00781           const Town *t = this->towns[i];
00782 
00783           assert(t != NULL);
00784 
00785           SetDParam(0, t->index);
00786           SetDParamMaxDigits(1, 8);
00787           d = maxdim(d, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN));
00788         }
00789         Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
00790         d.width += icon_size.width + 2;
00791         d.height = max(d.height, icon_size.height);
00792         resize->height = d.height;
00793         d.height *= 5;
00794         d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00795         d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00796         *size = maxdim(*size, d);
00797         break;
00798       }
00799       case WID_TD_WORLD_POPULATION: {
00800         SetDParamMaxDigits(0, 10);
00801         Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
00802         d.width += padding.width;
00803         d.height += padding.height;
00804         *size = maxdim(*size, d);
00805         break;
00806       }
00807     }
00808   }
00809 
00810   virtual void OnClick(Point pt, int widget, int click_count)
00811   {
00812     switch (widget) {
00813       case WID_TD_SORT_NAME: // Sort by Name ascending/descending
00814         if (this->towns.SortType() == 0) {
00815           this->towns.ToggleSortOrder();
00816         } else {
00817           this->towns.SetSortType(0);
00818         }
00819         this->BuildSortTownList();
00820         this->SetDirty();
00821         break;
00822 
00823       case WID_TD_SORT_POPULATION: // Sort by Population ascending/descending
00824         if (this->towns.SortType() == 1) {
00825           this->towns.ToggleSortOrder();
00826         } else {
00827           this->towns.SetSortType(1);
00828         }
00829         this->BuildSortTownList();
00830         this->SetDirty();
00831         break;
00832 
00833       case WID_TD_LIST: { // Click on Town Matrix
00834         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
00835         if (id_v >= this->towns.Length()) return; // click out of town bounds
00836 
00837         const Town *t = this->towns[id_v];
00838         assert(t != NULL);
00839         if (_ctrl_pressed) {
00840           ShowExtraViewPortWindow(t->xy);
00841         } else {
00842           ScrollMainWindowToTile(t->xy);
00843         }
00844         break;
00845       }
00846     }
00847   }
00848 
00849   virtual void OnPaint()
00850   {
00851     if (this->towns.NeedRebuild()) this->BuildSortTownList();
00852     this->DrawWidgets();
00853   }
00854 
00855   virtual void OnHundredthTick()
00856   {
00857     this->BuildSortTownList();
00858     this->SetDirty();
00859   }
00860 
00861   virtual void OnResize()
00862   {
00863     this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
00864   }
00865 
00871   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00872   {
00873     if (data == 0) {
00874       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
00875       this->towns.ForceRebuild();
00876     } else {
00877       this->towns.ForceResort();
00878     }
00879   }
00880 };
00881 
00882 Listing TownDirectoryWindow::last_sorting = {false, 0};
00883 const Town *TownDirectoryWindow::last_town = NULL;
00884 
00885 /* Available town directory sorting functions */
00886 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
00887   &TownNameSorter,
00888   &TownPopulationSorter,
00889 };
00890 
00891 static const WindowDesc _town_directory_desc(
00892   WDP_AUTO, 208, 202,
00893   WC_TOWN_DIRECTORY, WC_NONE,
00894   0,
00895   _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
00896 );
00897 
00898 void ShowTownDirectory()
00899 {
00900   if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
00901   new TownDirectoryWindow(&_town_directory_desc);
00902 }
00903 
00904 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00905 {
00906   if (result.Failed()) return;
00907 
00908   if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT, tile);
00909   if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
00910 }
00911 
00912 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00913 {
00914   if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
00915 }
00916 
00917 static const NWidgetPart _nested_found_town_widgets[] = {
00918   NWidget(NWID_HORIZONTAL),
00919     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00920     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00921     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00922     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00923   EndContainer(),
00924   /* Construct new town(s) buttons. */
00925   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00926     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00927     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00928                     SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00929     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00930                     SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00931     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
00932                     SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
00933     /* Town name selection. */
00934     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
00935     NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
00936                     SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
00937     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
00938                     SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
00939     /* Town size selection. */
00940     NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
00941       NWidget(NWID_SPACER), SetFill(1, 0),
00942       NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
00943       NWidget(NWID_SPACER), SetFill(1, 0),
00944     EndContainer(),
00945     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00946       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
00947                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00948       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
00949                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00950     EndContainer(),
00951     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00952     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00953       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
00954                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00955       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
00956                     SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00957     EndContainer(),
00958     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
00959     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
00960                     SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
00961     /* Town roads selection. */
00962     NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
00963       NWidget(NWID_SPACER), SetFill(1, 0),
00964       NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
00965       NWidget(NWID_SPACER), SetFill(1, 0),
00966     EndContainer(),
00967     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00968       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_ORIGINAL), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_ORIGINAL, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00969       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_BETTER), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_BETTER_ROADS, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00970     EndContainer(),
00971     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00972     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00973       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_GRID2), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_2X2_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00974       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_GRID3), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_3X3_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00975     EndContainer(),
00976     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00977     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
00978                     SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
00979     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00980   EndContainer(),
00981 };
00982 
00984 struct FoundTownWindow : Window {
00985 private:
00986   TownSize town_size;     
00987   TownLayout town_layout; 
00988   bool city;              
00989   QueryString townname_editbox; 
00990   bool townnamevalid;     
00991   uint32 townnameparts;   
00992   TownNameParams params;  
00993 
00994 public:
00995   FoundTownWindow(const WindowDesc *desc, WindowNumber window_number) :
00996       town_size(TSZ_MEDIUM),
00997       town_layout(_settings_game.economy.town_layout),
00998       townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
00999       params(_settings_game.game_creation.town_name)
01000   {
01001     this->InitNested(desc, window_number);
01002     this->querystrings[WID_TF_TOWN_NAME_EDITBOX] = &this->townname_editbox;
01003     this->RandomTownName();
01004     this->UpdateButtons(true);
01005   }
01006 
01007   void RandomTownName()
01008   {
01009     this->townnamevalid = GenerateTownName(&this->townnameparts);
01010 
01011     if (!this->townnamevalid) {
01012       this->townname_editbox.text.DeleteAll();
01013     } else {
01014       GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
01015       this->townname_editbox.text.UpdateSize();
01016     }
01017     UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
01018 
01019     this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
01020   }
01021 
01022   void UpdateButtons(bool check_availability)
01023   {
01024     if (check_availability && _game_mode != GM_EDITOR) {
01025       this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
01026       this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
01027           WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
01028       if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
01029     }
01030 
01031     for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
01032       this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
01033     }
01034 
01035     this->SetWidgetLoweredState(WID_TF_CITY, this->city);
01036 
01037     for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
01038       this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
01039     }
01040 
01041     this->SetDirty();
01042   }
01043 
01044   void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
01045   {
01046     const char *name = NULL;
01047 
01048     if (!this->townnamevalid) {
01049       name = this->townname_editbox.text.buf;
01050     } else {
01051       /* If user changed the name, send it */
01052       char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
01053       GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
01054       if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
01055     }
01056 
01057     bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
01058         townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
01059 
01060     if (success) this->RandomTownName();
01061   }
01062 
01063   virtual void OnClick(Point pt, int widget, int click_count)
01064   {
01065     switch (widget) {
01066       case WID_TF_NEW_TOWN:
01067         HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
01068         break;
01069 
01070       case WID_TF_RANDOM_TOWN:
01071         this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
01072         break;
01073 
01074       case WID_TF_TOWN_NAME_RANDOM:
01075         this->RandomTownName();
01076         this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
01077         break;
01078 
01079       case WID_TF_MANY_RANDOM_TOWNS:
01080         _generating_world = true;
01081         UpdateNearestTownForRoadTiles(true);
01082         if (!GenerateTowns(this->town_layout)) {
01083           ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
01084         }
01085         UpdateNearestTownForRoadTiles(false);
01086         _generating_world = false;
01087         break;
01088 
01089       case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
01090         this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
01091         this->UpdateButtons(false);
01092         break;
01093 
01094       case WID_TF_CITY:
01095         this->city ^= true;
01096         this->SetWidgetLoweredState(WID_TF_CITY, this->city);
01097         this->SetDirty();
01098         break;
01099 
01100       case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
01101       case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
01102         this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
01103         this->UpdateButtons(false);
01104         break;
01105     }
01106   }
01107 
01108   virtual void OnPlaceObject(Point pt, TileIndex tile)
01109   {
01110     this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
01111   }
01112 
01113   virtual void OnPlaceObjectAbort()
01114   {
01115     this->RaiseButtons();
01116     this->UpdateButtons(false);
01117   }
01118 
01124   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01125   {
01126     if (!gui_scope) return;
01127     this->UpdateButtons(true);
01128   }
01129 };
01130 
01131 static const WindowDesc _found_town_desc(
01132   WDP_AUTO, 160, 162,
01133   WC_FOUND_TOWN, WC_NONE,
01134   WDF_CONSTRUCTION,
01135   _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
01136 );
01137 
01138 void ShowFoundTownWindow()
01139 {
01140   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
01141   AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
01142 }