town_gui.cpp

Go to the documentation of this file.
00001 /* $Id: town_gui.cpp 23740 2012-01-03 21:32:51Z 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   WDF_UNCLICK_BUTTONS,
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->population);
00332     SetDParam(1, this->town->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->population <= 90)) continue;
00347       if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->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->population <= 90)) continue;
00469       if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->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   WDF_UNCLICK_BUTTONS,
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   WDF_UNCLICK_BUTTONS,
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 eachother) */
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     return (*a)->population - (*b)->population;
00685   }
00686 
00687 public:
00688   TownDirectoryWindow(const WindowDesc *desc) : Window()
00689   {
00690     this->CreateNestedTree(desc);
00691 
00692     this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
00693 
00694     this->towns.SetListing(this->last_sorting);
00695     this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
00696     this->towns.ForceRebuild();
00697     this->BuildSortTownList();
00698 
00699     this->FinishInitNested(desc, 0);
00700   }
00701 
00702   ~TownDirectoryWindow()
00703   {
00704     this->last_sorting = this->towns.GetListing();
00705   }
00706 
00707   virtual void SetStringParameters(int widget) const
00708   {
00709     if (widget == WID_TD_WORLD_POPULATION) SetDParam(0, GetWorldPopulation());
00710   }
00711 
00712   virtual void DrawWidget(const Rect &r, int widget) const
00713   {
00714     switch (widget) {
00715       case WID_TD_SORT_NAME:
00716         if (this->towns.SortType() == 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00717         break;
00718 
00719       case WID_TD_SORT_POPULATION:
00720         if (this->towns.SortType() != 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00721         break;
00722 
00723       case WID_TD_LIST: {
00724         int n = 0;
00725         int y = r.top + WD_FRAMERECT_TOP;
00726         if (this->towns.Length() == 0) { // No towns available.
00727           DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
00728           break;
00729         }
00730         /* At least one town available. */
00731         for (uint i = this->vscroll->GetPosition(); i < this->towns.Length(); i++) {
00732           const Town *t = this->towns[i];
00733 
00734           assert(t->xy != INVALID_TILE);
00735 
00736           SetDParam(0, t->index);
00737           SetDParam(1, t->population);
00738           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TOWN_DIRECTORY_TOWN);
00739 
00740           y += this->resize.step_height;
00741           if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
00742         }
00743         break;
00744       }
00745     }
00746   }
00747 
00748   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00749   {
00750     switch (widget) {
00751       case WID_TD_SORT_NAME:
00752       case WID_TD_SORT_POPULATION: {
00753         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00754         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
00755         d.height += padding.height;
00756         *size = maxdim(*size, d);
00757         break;
00758       }
00759       case WID_TD_LIST: {
00760         Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
00761         for (uint i = 0; i < this->towns.Length(); i++) {
00762           const Town *t = this->towns[i];
00763 
00764           assert(t != NULL);
00765 
00766           SetDParam(0, t->index);
00767           SetDParam(1, 10000000); // 10^7
00768           d = maxdim(d, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN));
00769         }
00770         resize->height = d.height;
00771         d.height *= 5;
00772         d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00773         d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00774         *size = maxdim(*size, d);
00775         break;
00776       }
00777       case WID_TD_WORLD_POPULATION: {
00778         SetDParam(0, 1000000000); // 10^9
00779         Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
00780         d.width += padding.width;
00781         d.height += padding.height;
00782         *size = maxdim(*size, d);
00783         break;
00784       }
00785     }
00786   }
00787 
00788   virtual void OnClick(Point pt, int widget, int click_count)
00789   {
00790     switch (widget) {
00791       case WID_TD_SORT_NAME: // Sort by Name ascending/descending
00792         if (this->towns.SortType() == 0) {
00793           this->towns.ToggleSortOrder();
00794         } else {
00795           this->towns.SetSortType(0);
00796         }
00797         this->BuildSortTownList();
00798         this->SetDirty();
00799         break;
00800 
00801       case WID_TD_SORT_POPULATION: // Sort by Population ascending/descending
00802         if (this->towns.SortType() == 1) {
00803           this->towns.ToggleSortOrder();
00804         } else {
00805           this->towns.SetSortType(1);
00806         }
00807         this->BuildSortTownList();
00808         this->SetDirty();
00809         break;
00810 
00811       case WID_TD_LIST: { // Click on Town Matrix
00812         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
00813         if (id_v >= this->towns.Length()) return; // click out of town bounds
00814 
00815         const Town *t = this->towns[id_v];
00816         assert(t != NULL);
00817         if (_ctrl_pressed) {
00818           ShowExtraViewPortWindow(t->xy);
00819         } else {
00820           ScrollMainWindowToTile(t->xy);
00821         }
00822         break;
00823       }
00824     }
00825   }
00826 
00827   virtual void OnPaint()
00828   {
00829     if (this->towns.NeedRebuild()) this->BuildSortTownList();
00830     this->DrawWidgets();
00831   }
00832 
00833   virtual void OnHundredthTick()
00834   {
00835     this->BuildSortTownList();
00836     this->SetDirty();
00837   }
00838 
00839   virtual void OnResize()
00840   {
00841     this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
00842   }
00843 
00849   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00850   {
00851     if (data == 0) {
00852       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
00853       this->towns.ForceRebuild();
00854     } else {
00855       this->towns.ForceResort();
00856     }
00857   }
00858 };
00859 
00860 Listing TownDirectoryWindow::last_sorting = {false, 0};
00861 const Town *TownDirectoryWindow::last_town = NULL;
00862 
00863 /* Available town directory sorting functions */
00864 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
00865   &TownNameSorter,
00866   &TownPopulationSorter,
00867 };
00868 
00869 static const WindowDesc _town_directory_desc(
00870   WDP_AUTO, 208, 202,
00871   WC_TOWN_DIRECTORY, WC_NONE,
00872   WDF_UNCLICK_BUTTONS,
00873   _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
00874 );
00875 
00876 void ShowTownDirectory()
00877 {
00878   if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
00879   new TownDirectoryWindow(&_town_directory_desc);
00880 }
00881 
00882 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00883 {
00884   if (result.Failed()) return;
00885 
00886   SndPlayTileFx(SND_1F_SPLAT, tile);
00887   if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
00888 }
00889 
00890 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00891 {
00892   if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
00893 }
00894 
00895 static const NWidgetPart _nested_found_town_widgets[] = {
00896   NWidget(NWID_HORIZONTAL),
00897     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00898     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00899     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00900     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00901   EndContainer(),
00902   /* Construct new town(s) buttons. */
00903   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00904     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00905     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00906                     SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00907     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00908                     SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00909     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
00910                     SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
00911     /* Town name selection. */
00912     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
00913     NWidget(WWT_EDITBOX, COLOUR_WHITE, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
00914                     SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
00915     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
00916                     SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
00917     /* Town size selection. */
00918     NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
00919       NWidget(NWID_SPACER), SetFill(1, 0),
00920       NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
00921       NWidget(NWID_SPACER), SetFill(1, 0),
00922     EndContainer(),
00923     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00924       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
00925                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00926       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
00927                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00928     EndContainer(),
00929     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00930     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00931       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
00932                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00933       NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
00934                     SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00935     EndContainer(),
00936     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
00937     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
00938                     SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
00939     /* Town roads 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_ROAD_LAYOUT, 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_LAYOUT_ORIGINAL), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_ORIGINAL, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00947       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),
00948     EndContainer(),
00949     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00950     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00951       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),
00952       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),
00953     EndContainer(),
00954     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00955     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
00956                     SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
00957     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00958   EndContainer(),
00959 };
00960 
00962 struct FoundTownWindow : QueryStringBaseWindow {
00963 private:
00964   TownSize town_size;     
00965   TownLayout town_layout; 
00966   bool city;              
00967   bool townnamevalid;     
00968   uint32 townnameparts;   
00969   TownNameParams params;  
00970 
00971 public:
00972   FoundTownWindow(const WindowDesc *desc, WindowNumber window_number) :
00973       QueryStringBaseWindow(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
00974       town_size(TSZ_MEDIUM),
00975       town_layout(_settings_game.economy.town_layout),
00976       params(_settings_game.game_creation.town_name)
00977   {
00978     this->InitNested(desc, window_number);
00979     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, this->max_chars);
00980     this->RandomTownName();
00981     this->UpdateButtons(true);
00982   }
00983 
00984   void RandomTownName()
00985   {
00986     this->townnamevalid = GenerateTownName(&this->townnameparts);
00987 
00988     if (!this->townnamevalid) {
00989       this->edit_str_buf[0] = '\0';
00990     } else {
00991       GetTownName(this->edit_str_buf, &this->params, this->townnameparts, &this->edit_str_buf[this->edit_str_size - 1]);
00992     }
00993     UpdateTextBufferSize(&this->text);
00994     UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
00995 
00996     this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
00997   }
00998 
00999   void UpdateButtons(bool check_availability)
01000   {
01001     if (check_availability && _game_mode != GM_EDITOR) {
01002       this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
01003       this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
01004           WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
01005       if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
01006     }
01007 
01008     for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
01009       this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
01010     }
01011 
01012     this->SetWidgetLoweredState(WID_TF_CITY, this->city);
01013 
01014     for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
01015       this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
01016     }
01017 
01018     this->SetDirty();
01019   }
01020 
01021   void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
01022   {
01023     const char *name = NULL;
01024 
01025     if (!this->townnamevalid) {
01026       name = this->edit_str_buf;
01027     } else {
01028       /* If user changed the name, send it */
01029       char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
01030       GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
01031       if (strcmp(buf, this->edit_str_buf) != 0) name = this->edit_str_buf;
01032     }
01033 
01034     bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
01035         townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
01036 
01037     if (success) this->RandomTownName();
01038   }
01039 
01040   virtual void OnPaint()
01041   {
01042     this->DrawWidgets();
01043     if (!this->IsShaded()) this->DrawEditBox(WID_TF_TOWN_NAME_EDITBOX);
01044   }
01045 
01046   virtual void OnClick(Point pt, int widget, int click_count)
01047   {
01048     switch (widget) {
01049       case WID_TF_NEW_TOWN:
01050         HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
01051         break;
01052 
01053       case WID_TF_RANDOM_TOWN:
01054         this->HandleButtonClick(WID_TF_RANDOM_TOWN);
01055         this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
01056         break;
01057 
01058       case WID_TF_TOWN_NAME_RANDOM:
01059         this->RandomTownName();
01060         this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
01061         break;
01062 
01063       case WID_TF_MANY_RANDOM_TOWNS:
01064         this->HandleButtonClick(WID_TF_MANY_RANDOM_TOWNS);
01065 
01066         _generating_world = true;
01067         UpdateNearestTownForRoadTiles(true);
01068         if (!GenerateTowns(this->town_layout)) {
01069           ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
01070         }
01071         UpdateNearestTownForRoadTiles(false);
01072         _generating_world = false;
01073         break;
01074 
01075       case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
01076         this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
01077         this->UpdateButtons(false);
01078         break;
01079 
01080       case WID_TF_CITY:
01081         this->city ^= true;
01082         this->SetWidgetLoweredState(WID_TF_CITY, this->city);
01083         this->SetDirty();
01084         break;
01085 
01086       case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
01087       case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
01088         this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
01089         this->UpdateButtons(false);
01090         break;
01091     }
01092   }
01093 
01094   virtual void OnTimeout()
01095   {
01096     this->RaiseWidget(WID_TF_RANDOM_TOWN);
01097     this->RaiseWidget(WID_TF_MANY_RANDOM_TOWNS);
01098     this->SetDirty();
01099   }
01100 
01101   virtual void OnMouseLoop()
01102   {
01103     this->HandleEditBox(WID_TF_TOWN_NAME_EDITBOX);
01104   }
01105 
01106   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01107   {
01108     EventState state = ES_NOT_HANDLED;
01109     if (this->HandleEditBoxKey(WID_TF_TOWN_NAME_EDITBOX, key, keycode, state) == HEBR_CANCEL) {
01110       this->UnfocusFocusedWidget();
01111     }
01112     return state;
01113   }
01114 
01115   virtual void OnPlaceObject(Point pt, TileIndex tile)
01116   {
01117     this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
01118   }
01119 
01120   virtual void OnPlaceObjectAbort()
01121   {
01122     this->RaiseButtons();
01123     this->UpdateButtons(false);
01124   }
01125 
01131   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01132   {
01133     if (!gui_scope) return;
01134     this->UpdateButtons(true);
01135   }
01136 };
01137 
01138 static const WindowDesc _found_town_desc(
01139   WDP_AUTO, 160, 162,
01140   WC_FOUND_TOWN, WC_NONE,
01141   WDF_CONSTRUCTION,
01142   _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
01143 );
01144 
01145 void ShowFoundTownWindow()
01146 {
01147   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
01148   AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
01149 }