# town_gui.cpp

Go to the documentation of this file.

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