# town_gui.cpp

Go to the documentation of this file.

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