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 "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     StringID str;
00366     uint y = r.top + WD_FRAMERECT_TOP;
00367 
00368 /* needed for old style gui 1/2 */
00369     /* Required delivered cargos for growth in artic and tropic climate. */
00370     StringID required_text = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
00371     uint cargo_needed_for_growth = 0;
00372     switch (_settings_game.game_creation.landscape) {
00373       case LT_ARCTIC:
00374         if (TilePixelHeight(this->town->xy) >= LowestSnowLine()) cargo_needed_for_growth = 1;
00375         if (TilePixelHeight(this->town->xy) < GetSnowLine()) required_text = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
00376         break;
00377 
00378       case LT_TROPIC:
00379         if (GetTropicZone(this->town->xy) == TROPICZONE_DESERT) cargo_needed_for_growth = 2;
00380         break;
00381 
00382       default: break;
00383     }
00384 /* needed for old style gui 1/2 end*/
00385 
00386     /* Needed for cases where the towneffect of cargos is changed by means of GRF. */
00387     const CargoSpec *passengers = FindFirstCargoWithTownEffect(TE_PASSENGERS);
00388     StringID passengers_name    = (passengers != NULL) ? passengers->name    : STR_CARGO_PLURAL_PASSENGERS;
00389 
00390     const CargoSpec *mail       = FindFirstCargoWithTownEffect(TE_MAIL);
00391     StringID mail_name          = (mail != NULL) ? mail->name    : STR_CARGO_PLURAL_MAIL;
00392 
00393     const CargoSpec *food = FindFirstCargoWithTownEffect(TE_FOOD);
00394     CargoID first_food_cargo = (food != NULL) ? food->Index() : (CargoID)CT_INVALID;
00395     StringID food_name       = (food != NULL) ? food->name    : STR_CARGO_PLURAL_FOOD;
00396 
00397     const CargoSpec *water = FindFirstCargoWithTownEffect(TE_WATER);
00398     CargoID first_water_cargo = (water != NULL) ? water->Index() : (CargoID)CT_INVALID;
00399     StringID water_name       = (water != NULL) ? water->name    : STR_CARGO_PLURAL_WATER;
00400 
00401     const CargoSpec *goods      = FindFirstCargoWithTownEffect(TE_GOODS);
00402     StringID goods_name         = (goods != NULL) ? goods->name    : STR_CARGO_PLURAL_GOODS;
00403 
00404     /* Population and number of houses. */
00405     SetDParam(0, this->town->population);
00406     SetDParam(1, this->town->num_houses);
00407     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
00408 
00409     /* Empty line. */
00410     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_EMPTY);
00411 
00412     /* Generated cargo titel. */
00413     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_GENERATED_CARGO_LAST_MONTH);
00414 
00415     /* Generated and max possible generated passengers. */
00416     SetDParam(0, this->town->gen_act_pass);
00417     SetDParam(1, this->town->gen_max_pass);
00418     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_GENERATED_PASS_LAST_MONTH_MAX);
00419 
00420     /* Generated and max possible generated mail. */
00421     SetDParam(0, this->town->gen_act_mail);
00422     SetDParam(1, this->town->gen_max_mail);
00423     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_GENERATED_MAIL_LAST_MONTH_MAX);
00424 
00425     /* Empty line. */
00426     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_EMPTY);
00427 
00428     /* Transported goods rate of this town. Only displayed if we have enabled the town_growth_patch and the population is big enough. */
00429     if (((!this->town->larger_town && _settings_game.economy.town_growth_cargo) || (this->town->larger_town && _settings_game.economy.larger_town_growth_cargo)) &&
00430         ((!this->town->larger_town && this->town->population > _settings_game.economy.town_pop_need_goods) ||
00431         (this->town->larger_town && this->town->population > _settings_game.economy.larger_town_pop_need_goods))) {
00432       /* Growth rate last month. */
00433       SetDParam(0, this->town->GrowthRatePercent());
00434       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_GROWTH_RATE_LAST_MONTH);
00435 
00436       /* Transported goods ratio last month. */
00437       SetDParam(0, this->town->transported_goods_percent);
00438       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TRANSPORTED_RATE_LAST_MONTH);
00439 
00440       /* Most demanded for growth last month. */
00441       str = STR_TOWN_VIEW_GROWTH_STANDARD;
00442       if (_settings_game.economy.town_growth_rate == 0) {
00443         str = STR_TOWN_VIEW_GROWTH_SPEED_DISABLED;
00444       } else if (this->town->transported_goods_percent <= 99) {
00445         str += this->town->needed_for_growth;
00446         // long if else ... need to fetch the correct cago name -> grfs changing TE_xxx
00447         if (this->town->needed_for_growth == GROWTH_NEED_STATION) {
00448           str = STR_TOWN_VIEW_GROWTH_NEED_STATION;
00449         } else if (this->town->needed_for_growth == GROWTH_NEED_MORE_PASS) {
00450           SetDParam(1 , passengers_name);
00451           str = STR_TOWN_VIEW_GROWTH_NEED_MORE_PASS;
00452         } else if (this->town->needed_for_growth == GROWTH_NEED_MORE_MAIL) {
00453           SetDParam(1 , mail_name);
00454           str = STR_TOWN_VIEW_GROWTH_NEED_MORE_MAIL;
00455         } else if (this->town->needed_for_growth == GROWTH_NEED_MORE_FOOD) {
00456           SetDParam(1 , food_name);
00457           str = STR_TOWN_VIEW_GROWTH_NEED_MORE_FOOD;
00458         } else if (this->town->needed_for_growth == GROWTH_NEED_MORE_WATER) {
00459           SetDParam(1 , water_name);
00460           str = STR_TOWN_VIEW_GROWTH_NEED_MORE_WATER;
00461         } else if (this->town->needed_for_growth == GROWTH_NEED_MORE_GOODS) {
00462           SetDParam(1 , goods_name);
00463           str = STR_TOWN_VIEW_GROWTH_NEED_MORE_GOODS;
00464         } else if (this->town->needed_for_growth == GROWTH_NEED_PASS) {
00465           SetDParam(1 , passengers_name);
00466           str = STR_TOWN_VIEW_GROWTH_NEED_PASS;
00467         } else if (this->town->needed_for_growth == GROWTH_NEED_MAIL) {
00468           SetDParam(1 , mail_name);
00469           str = STR_TOWN_VIEW_GROWTH_NEED_MAIL;
00470         } else if (this->town->needed_for_growth == GROWTH_NEED_FOOD) {
00471           SetDParam(1 , food_name);
00472           str = STR_TOWN_VIEW_GROWTH_NEED_FOOD;}
00473         else if (this->town->needed_for_growth == GROWTH_NEED_WATER) {
00474           SetDParam(1 , water_name);
00475           str = STR_TOWN_VIEW_GROWTH_NEED_WATER;
00476         } else if (this->town->needed_for_growth == GROWTH_NEED_GOODS) {
00477           SetDParam(1 , goods_name);
00478           str = STR_TOWN_VIEW_GROWTH_NEED_GOODS;
00479         } else {
00480           SetDParam(1, STR_EMPTY);
00481         }
00482       } else if (this->town->transported_goods_percent >= 100) {
00483         str = STR_TOWN_VIEW_GROWTH_SATISFIED;
00484       }
00485       SetDParam(0, str);
00486       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_DEMAND_LAST_MONTH);
00487 
00488       /* Empty line. */
00489       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_EMPTY);
00490 
00491       /* Requirements "titel" (Needed to clarify that the values are this month's). */
00492       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_THIS_MONTH);
00493  
00494       /* Delivered and needed passengers. */
00495       SetDParam(0, passengers_name);
00496       SetDParam(1, this->town->act_pass);
00497       SetDParam(2, this->town->max_pass);
00498       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_PASSENGERS_THIS_MONTH_MAX);
00499 
00500       /* Delivered and needed mail. */
00501       SetDParam(0, mail_name);
00502       SetDParam(1, this->town->act_mail);
00503       SetDParam(2, this->town->max_mail);
00504       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_MAIL_THIS_MONTH_MAX);
00505 
00506       /* Delivered and needed food (Fizzy Drinks if we are playing in toyland). */
00507       if ((_settings_game.game_creation.landscape == LT_ARCTIC && TilePixelHeight(this->town->xy) >= LowestSnowLine()) ||
00508           (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(this->town->xy) == TROPICZONE_DESERT) ||
00509           _settings_game.game_creation.landscape == LT_TOYLAND) {
00510         SetDParam(0, food_name);
00511         SetDParam(1, this->town->act_food);
00512         SetDParam(2, this->town->max_food);
00513         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_FOOD_THIS_MONTH_MAX);
00514       }
00515 
00516       /* Delivered and needed water in desert. */
00517       if (_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(this->town->xy) == TROPICZONE_DESERT) {
00518         SetDParam(0, water_name);
00519         SetDParam(1, this->town->act_water);
00520         SetDParam(2, this->town->max_water);
00521         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_WATER_THIS_MONTH_MAX);
00522       }
00523 
00524       /* Delivered and needed goods (Sweets if we play in toyland climate). */
00525       SetDParam(0, goods_name);
00526       SetDParam(1, this->town->act_goods);
00527       SetDParam(2, this->town->max_goods);
00528       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_GOODS_THIS_MONTH_MAX);
00529     }
00530 
00531 /* Old style gui 2/2 */
00532     /* What is the town needing to be able to grow. */
00533     if ((cargo_needed_for_growth > 0 &&
00534         (((!this->town->larger_town && !_settings_game.economy.town_growth_cargo) || (this->town->larger_town && !_settings_game.economy.larger_town_growth_cargo)) ||
00535         ((!this->town->larger_town && _settings_game.economy.town_growth_cargo) || (this->town->larger_town && _settings_game.economy.larger_town_growth_cargo))) &&
00536         ((!this->town->larger_town && this->town->population < _settings_game.economy.town_pop_need_goods) ||
00537         (this->town->larger_town && this->town->population < _settings_game.economy.larger_town_pop_need_goods)))) {
00538       if (_settings_game.game_creation.landscape != LT_TEMPERATE && _settings_game.game_creation.landscape != LT_TOYLAND) {
00539         bool rtl = _current_text_dir == TD_RTL;
00540         uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
00541         uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
00542 
00543         if (first_food_cargo != CT_INVALID && this->town->act_food > 0) {
00544           SetDParam(0, first_food_cargo);
00545           SetDParam(1, this->town->act_food);
00546           DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_LAST_MONTH);
00547         } else {
00548           SetDParam(0, food_name);
00549           DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, required_text);
00550         }
00551 
00552         if (cargo_needed_for_growth > 1) {
00553           if (first_water_cargo != CT_INVALID && this->town->act_water > 0) {
00554             SetDParam(0, first_water_cargo);
00555             SetDParam(1, this->town->act_water);
00556             DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_LAST_MONTH);
00557           } else {
00558             SetDParam(0, water_name);
00559             DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, required_text);
00560           }
00561         }
00562       }
00563 /* end old style gui 2/2 */
00564     }
00565 
00566     /* only show the town noise, if the noise option is activated. */
00567     if (_settings_game.economy.station_noise_level) {
00568       /* Empty line. */
00569       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_EMPTY);
00570 
00571       SetDParam(0, this->town->noise_reached);
00572       SetDParam(1, this->town->MaxTownNoise());
00573       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
00574     }
00575   }
00576 
00577   virtual void OnClick(Point pt, int widget, int click_count)
00578   {
00579     switch (widget) {
00580       case TVW_CENTERVIEW: // scroll to location
00581         if (_ctrl_pressed) {
00582           ShowExtraViewPortWindow(this->town->xy);
00583         } else {
00584           ScrollMainWindowToTile(this->town->xy);
00585         }
00586         break;
00587 
00588       case TVW_SHOWAUTHORITY: // town authority
00589         ShowTownAuthorityWindow(this->window_number);
00590         break;
00591 
00592       case TVW_CHANGENAME: // rename
00593         SetDParam(0, this->window_number);
00594         ShowQueryString(STR_TOWN_NAME, STR_TOWN_VIEW_RENAME_TOWN_BUTTON, MAX_LENGTH_TOWN_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00595         break;
00596 
00597       case TVW_EXPAND: { // expand town - only available on Scenario editor
00598         /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
00599         static bool _warn_town_no_roads = false;
00600 
00601         if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
00602           ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
00603           _warn_town_no_roads = true;
00604         }
00605 
00606         DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
00607         break;
00608       }
00609 
00610       case TVW_DELETE: // delete town - only available on Scenario editor
00611         DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
00612         break;
00613     }
00614   }
00615 
00616   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00617   {
00618     switch (widget) {
00619       case TVW_INFOPANEL:
00620         size->height = GetDesiredInfoHeight();
00621         break;
00622     }
00623   }
00624 
00629   uint GetDesiredInfoHeight() const
00630   {
00631     uint aimed_height;
00632     if (((!this->town->larger_town && _settings_game.economy.town_growth_cargo) || (this->town->larger_town && _settings_game.economy.larger_town_growth_cargo)) &&
00633         ((!this->town->larger_town && this->town->population > _settings_game.economy.town_pop_need_goods) ||
00634         (this->town->larger_town && this->town->population > _settings_game.economy.larger_town_pop_need_goods))) {
00635       if (_settings_game.game_creation.landscape != LT_TOYLAND) {     
00636         aimed_height = 14 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00637       } else{
00638         aimed_height = 15 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00639       }
00640     } else {
00641       aimed_height = 7 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00642     }
00643 
00644     switch (_settings_game.game_creation.landscape) {
00645       case LT_ARCTIC:
00646         aimed_height += FONT_HEIGHT_NORMAL;
00647         if (TilePixelHeight(this->town->xy) < LowestSnowLine() &&
00648             ((!this->town->larger_town && this->town->population > _settings_game.economy.town_pop_need_goods) ||
00649             (this->town->larger_town && this->town->population > _settings_game.economy.larger_town_pop_need_goods))) {
00650           aimed_height -= FONT_HEIGHT_NORMAL;
00651         }
00652         break;
00653 
00654       case LT_TROPIC:
00655         if (GetTropicZone(this->town->xy) != TROPICZONE_DESERT &&
00656             ((!this->town->larger_town && this->town->population < _settings_game.economy.town_pop_need_goods) ||
00657             (this->town->larger_town && this->town->population < _settings_game.economy.larger_town_pop_need_goods))) {
00658           aimed_height += FONT_HEIGHT_NORMAL;
00659         }
00660         if (GetTropicZone(this->town->xy) == TROPICZONE_DESERT) aimed_height += 2 * FONT_HEIGHT_NORMAL;
00661         break;
00662 
00663       default: break;
00664     }
00665 
00666     if (_settings_game.economy.station_noise_level) aimed_height += 2 * FONT_HEIGHT_NORMAL;
00667 
00668     return aimed_height;
00669   }
00670 
00671   void ResizeWindowAsNeeded()
00672   {
00673     const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(TVW_INFOPANEL);
00674     uint aimed_height = GetDesiredInfoHeight();
00675     if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
00676       this->ReInit();
00677     }
00678   }
00679 
00680   virtual void OnResize()
00681   {
00682     if (this->viewport != NULL) {
00683       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(TVW_VIEWPORT);
00684       nvp->UpdateViewportCoordinates(this);
00685 
00686       ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
00687     }
00688   }
00689 
00695   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00696   {
00697     if (!gui_scope) return;
00698     /* Called when setting station noise or required cargos have changed, in order to resize the window */
00699     this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
00700     this->ResizeWindowAsNeeded();
00701   }
00702 
00703   virtual void OnQueryTextFinished(char *str)
00704   {
00705     if (str == NULL) return;
00706 
00707     DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), NULL, str);
00708   }
00709 };
00710 
00711 static const NWidgetPart _nested_town_game_view_widgets[] = {
00712   NWidget(NWID_HORIZONTAL),
00713     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00714     NWidget(WWT_CAPTION, COLOUR_BROWN, TVW_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00715     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00716     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00717   EndContainer(),
00718   NWidget(WWT_PANEL, COLOUR_BROWN),
00719     NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00720       NWidget(NWID_VIEWPORT, INVALID_COLOUR, TVW_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00721     EndContainer(),
00722   EndContainer(),
00723   NWidget(WWT_PANEL, COLOUR_BROWN, TVW_INFOPANEL), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00724   NWidget(NWID_HORIZONTAL),
00725     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00726       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),
00727       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),
00728       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),
00729     EndContainer(),
00730     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00731   EndContainer(),
00732 };
00733 
00734 static const WindowDesc _town_game_view_desc(
00735   WDP_AUTO, 340, TownViewWindow::TVW_HEIGHT_NORMAL,
00736   WC_TOWN_VIEW, WC_NONE,
00737   WDF_UNCLICK_BUTTONS,
00738   _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
00739 );
00740 
00741 static const NWidgetPart _nested_town_editor_view_widgets[] = {
00742   NWidget(NWID_HORIZONTAL),
00743     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00744     NWidget(WWT_CAPTION, COLOUR_BROWN, TVW_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00745     NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_CHANGENAME), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
00746     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00747     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00748   EndContainer(),
00749   NWidget(WWT_PANEL, COLOUR_BROWN),
00750     NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00751       NWidget(NWID_VIEWPORT, INVALID_COLOUR, TVW_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00752     EndContainer(),
00753   EndContainer(),
00754   NWidget(WWT_PANEL, COLOUR_BROWN, TVW_INFOPANEL), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00755   NWidget(NWID_HORIZONTAL),
00756     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00757       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),
00758       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),
00759       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),
00760     EndContainer(),
00761     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00762   EndContainer(),
00763 };
00764 
00765 static const WindowDesc _town_editor_view_desc(
00766   WDP_AUTO, 260, TownViewWindow::TVW_HEIGHT_NORMAL,
00767   WC_TOWN_VIEW, WC_NONE,
00768   WDF_UNCLICK_BUTTONS,
00769   _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
00770 );
00771 
00772 void ShowTownViewWindow(TownID town)
00773 {
00774   if (_game_mode == GM_EDITOR) {
00775     AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
00776   } else {
00777     AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
00778   }
00779 }
00780 
00782 enum TownDirectoryWidgets {
00783   TDW_SORTNAME,
00784   TDW_SORTPOPULATION,
00785   TDW_CENTERTOWN,
00786   TDW_SCROLLBAR,
00787   TDW_BOTTOM_PANEL,
00788   TDW_BOTTOM_TEXT,
00789 };
00790 
00791 static const NWidgetPart _nested_town_directory_widgets[] = {
00792   NWidget(NWID_HORIZONTAL),
00793     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00794     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00795     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00796     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00797   EndContainer(),
00798   NWidget(NWID_HORIZONTAL),
00799     NWidget(NWID_VERTICAL),
00800       NWidget(NWID_HORIZONTAL),
00801         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TDW_SORTNAME), SetMinimalSize(99, 12), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00802         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TDW_SORTPOPULATION), SetMinimalSize(97, 12), SetDataTip(STR_SORT_BY_CAPTION_POPULATION, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00803       EndContainer(),
00804       NWidget(WWT_PANEL, COLOUR_BROWN, TDW_CENTERTOWN), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
00805               SetFill(1, 0), SetResize(0, 10), SetScrollbar(TDW_SCROLLBAR), EndContainer(),
00806       NWidget(WWT_PANEL, COLOUR_BROWN, TDW_BOTTOM_PANEL),
00807         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),
00808       EndContainer(),
00809     EndContainer(),
00810     NWidget(NWID_VERTICAL),
00811       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, TDW_SCROLLBAR),
00812       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00813     EndContainer(),
00814   EndContainer(),
00815 };
00816 
00818 struct TownDirectoryWindow : public Window {
00819 private:
00820   /* Runtime saved values */
00821   static Listing last_sorting;
00822   static const Town *last_town;
00823 
00824   /* Constants for sorting towns */
00825   static GUITownList::SortFunction * const sorter_funcs[];
00826 
00827   GUITownList towns;
00828 
00829   Scrollbar *vscroll;
00830 
00831   void BuildSortTownList()
00832   {
00833     if (this->towns.NeedRebuild()) {
00834       this->towns.Clear();
00835 
00836       const Town *t;
00837       FOR_ALL_TOWNS(t) {
00838         *this->towns.Append() = t;
00839       }
00840 
00841       this->towns.Compact();
00842       this->towns.RebuildDone();
00843       this->vscroll->SetCount(this->towns.Length()); // Update scrollbar as well.
00844     }
00845     /* Always sort the towns. */
00846     this->last_town = NULL;
00847     this->towns.Sort();
00848   }
00849 
00851   static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
00852   {
00853     static char buf_cache[64];
00854     const Town *ta = *a;
00855     const Town *tb = *b;
00856     char buf[64];
00857 
00858     SetDParam(0, ta->index);
00859     GetString(buf, STR_TOWN_NAME, lastof(buf));
00860 
00861     /* If 'b' is the same town as in the last round, use the cached value
00862      * We do this to speed stuff up ('b' is called with the same value a lot of
00863      * times after eachother) */
00864     if (tb != last_town) {
00865       last_town = tb;
00866       SetDParam(0, tb->index);
00867       GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
00868     }
00869 
00870     return strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
00871   }
00872 
00874   static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
00875   {
00876     return (*a)->population - (*b)->population;
00877   }
00878 
00879 public:
00880   TownDirectoryWindow(const WindowDesc *desc) : Window()
00881   {
00882     this->CreateNestedTree(desc);
00883 
00884     this->vscroll = this->GetScrollbar(TDW_SCROLLBAR);
00885 
00886     this->towns.SetListing(this->last_sorting);
00887     this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
00888     this->towns.ForceRebuild();
00889     this->BuildSortTownList();
00890 
00891     this->FinishInitNested(desc, 0);
00892   }
00893 
00894   ~TownDirectoryWindow()
00895   {
00896     this->last_sorting = this->towns.GetListing();
00897   }
00898 
00899   virtual void SetStringParameters(int widget) const
00900   {
00901     if (widget == TDW_BOTTOM_TEXT) SetDParam(0, GetWorldPopulation());
00902   }
00903 
00904   virtual void DrawWidget(const Rect &r, int widget) const
00905   {
00906     switch (widget) {
00907       case TDW_SORTNAME:
00908         if (this->towns.SortType() == 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00909         break;
00910 
00911       case TDW_SORTPOPULATION:
00912         if (this->towns.SortType() != 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00913         break;
00914 
00915       case TDW_CENTERTOWN: {
00916         int n = 0;
00917         int y = r.top + WD_FRAMERECT_TOP;
00918         if (this->towns.Length() == 0) { // No towns available.
00919           DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
00920           break;
00921         }
00922         /* At least one town available. */
00923         for (uint i = this->vscroll->GetPosition(); i < this->towns.Length(); i++) {
00924           const Town *t = this->towns[i];
00925 
00926           assert(t->xy != INVALID_TILE);
00927 
00928           SetDParam(0, t->index);
00929           SetDParam(1, t->population);
00930           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TOWN_DIRECTORY_TOWN);
00931 
00932           y += this->resize.step_height;
00933           if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
00934         }
00935         break;
00936       }
00937     }
00938   }
00939 
00940   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00941   {
00942     switch (widget) {
00943       case TDW_SORTNAME:
00944       case TDW_SORTPOPULATION: {
00945         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00946         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
00947         d.height += padding.height;
00948         *size = maxdim(*size, d);
00949         break;
00950       }
00951       case TDW_CENTERTOWN: {
00952         Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
00953         for (uint i = 0; i < this->towns.Length(); i++) {
00954           const Town *t = this->towns[i];
00955 
00956           assert(t != NULL);
00957 
00958           SetDParam(0, t->index);
00959           SetDParam(1, 10000000); // 10^7
00960           d = maxdim(d, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN));
00961         }
00962         resize->height = d.height;
00963         d.height *= 5;
00964         d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00965         d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00966         *size = maxdim(*size, d);
00967         break;
00968       }
00969       case TDW_BOTTOM_TEXT: {
00970         SetDParam(0, 1000000000); // 10^9
00971         Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
00972         d.width += padding.width;
00973         d.height += padding.height;
00974         *size = maxdim(*size, d);
00975         break;
00976       }
00977     }
00978   }
00979 
00980   virtual void OnClick(Point pt, int widget, int click_count)
00981   {
00982     switch (widget) {
00983       case TDW_SORTNAME: // Sort by Name ascending/descending
00984         if (this->towns.SortType() == 0) {
00985           this->towns.ToggleSortOrder();
00986         } else {
00987           this->towns.SetSortType(0);
00988         }
00989         this->BuildSortTownList();
00990         this->SetDirty();
00991         break;
00992 
00993       case TDW_SORTPOPULATION: // Sort by Population ascending/descending
00994         if (this->towns.SortType() == 1) {
00995           this->towns.ToggleSortOrder();
00996         } else {
00997           this->towns.SetSortType(1);
00998         }
00999         this->BuildSortTownList();
01000         this->SetDirty();
01001         break;
01002 
01003       case TDW_CENTERTOWN: { // Click on Town Matrix
01004         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, TDW_CENTERTOWN, WD_FRAMERECT_TOP);
01005         if (id_v >= this->towns.Length()) return; // click out of town bounds
01006 
01007         const Town *t = this->towns[id_v];
01008         assert(t != NULL);
01009         if (_ctrl_pressed) {
01010           ShowExtraViewPortWindow(t->xy);
01011         } else {
01012           ScrollMainWindowToTile(t->xy);
01013         }
01014         break;
01015       }
01016     }
01017   }
01018 
01019   virtual void OnPaint()
01020   {
01021     if (this->towns.NeedRebuild()) this->BuildSortTownList();
01022     this->DrawWidgets();
01023   }
01024 
01025   virtual void OnHundredthTick()
01026   {
01027     this->BuildSortTownList();
01028     this->SetDirty();
01029   }
01030 
01031   virtual void OnResize()
01032   {
01033     this->vscroll->SetCapacityFromWidget(this, TDW_CENTERTOWN);
01034   }
01035 
01041   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01042   {
01043     if (data == 0) {
01044       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
01045       this->towns.ForceRebuild();
01046     } else {
01047       this->towns.ForceResort();
01048     }
01049   }
01050 };
01051 
01052 Listing TownDirectoryWindow::last_sorting = {false, 0};
01053 const Town *TownDirectoryWindow::last_town = NULL;
01054 
01055 /* Available town directory sorting functions */
01056 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
01057   &TownNameSorter,
01058   &TownPopulationSorter,
01059 };
01060 
01061 static const WindowDesc _town_directory_desc(
01062   WDP_AUTO, 208, 202,
01063   WC_TOWN_DIRECTORY, WC_NONE,
01064   WDF_UNCLICK_BUTTONS,
01065   _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
01066 );
01067 
01068 void ShowTownDirectory()
01069 {
01070   if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
01071   new TownDirectoryWindow(&_town_directory_desc);
01072 }
01073 
01074 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
01075 {
01076   if (result.Failed()) return;
01077 
01078   SndPlayTileFx(SND_1F_SPLAT, tile);
01079   if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
01080 }
01081 
01082 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
01083 {
01084   if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
01085 }
01086 
01088 enum TownScenarioEditorWidgets {
01089   TSEW_BACKGROUND,
01090   TSEW_NEWTOWN,
01091   TSEW_RANDOMTOWN,
01092   TSEW_MANYRANDOMTOWNS,
01093   TSEW_TOWNNAME_TEXT,
01094   TSEW_TOWNNAME_EDITBOX,
01095   TSEW_TOWNNAME_RANDOM,
01096   TSEW_TOWNSIZE,
01097   TSEW_SIZE_SMALL,
01098   TSEW_SIZE_MEDIUM,
01099   TSEW_SIZE_LARGE,
01100   TSEW_SIZE_RANDOM,
01101   TSEW_CITY,
01102   TSEW_TOWNLAYOUT,
01103   TSEW_LAYOUT_ORIGINAL,
01104   TSEW_LAYOUT_BETTER,
01105   TSEW_LAYOUT_GRID2,
01106   TSEW_LAYOUT_GRID3,
01107   TSEW_LAYOUT_RANDOM,
01108 };
01109 
01110 static const NWidgetPart _nested_found_town_widgets[] = {
01111   NWidget(NWID_HORIZONTAL),
01112     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
01113     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01114     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
01115     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
01116   EndContainer(),
01117   /* Construct new town(s) buttons. */
01118   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, TSEW_BACKGROUND),
01119     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
01120     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_NEWTOWN), SetMinimalSize(156, 12), SetFill(1, 0),
01121                     SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
01122     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_RANDOMTOWN), SetMinimalSize(156, 12), SetFill(1, 0),
01123                     SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
01124     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_MANYRANDOMTOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
01125                     SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
01126     /* Town name selection. */
01127     NWidget(WWT_LABEL, COLOUR_DARK_GREEN, TSEW_TOWNSIZE), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
01128     NWidget(WWT_EDITBOX, COLOUR_WHITE, TSEW_TOWNNAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
01129                     SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
01130     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_TOWNNAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
01131                     SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
01132     /* Town size selection. */
01133     NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
01134       NWidget(NWID_SPACER), SetFill(1, 0),
01135       NWidget(WWT_LABEL, COLOUR_DARK_GREEN, TSEW_TOWNSIZE), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
01136       NWidget(NWID_SPACER), SetFill(1, 0),
01137     EndContainer(),
01138     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
01139       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
01140                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
01141       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
01142                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
01143     EndContainer(),
01144     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
01145     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
01146       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
01147                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
01148       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
01149                     SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
01150     EndContainer(),
01151     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
01152     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
01153                     SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
01154     /* Town roads selection. */
01155     NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
01156       NWidget(NWID_SPACER), SetFill(1, 0),
01157       NWidget(WWT_LABEL, COLOUR_DARK_GREEN, TSEW_TOWNLAYOUT), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
01158       NWidget(NWID_SPACER), SetFill(1, 0),
01159     EndContainer(),
01160     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
01161       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),
01162       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),
01163     EndContainer(),
01164     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
01165     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
01166       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),
01167       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),
01168     EndContainer(),
01169     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
01170     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
01171                     SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
01172     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
01173   EndContainer(),
01174 };
01175 
01177 struct FoundTownWindow : QueryStringBaseWindow {
01178 private:
01179   TownSize town_size;     
01180   TownLayout town_layout; 
01181   bool city;              
01182   bool townnamevalid;     
01183   uint32 townnameparts;   
01184   TownNameParams params;  
01185 
01186 public:
01187   FoundTownWindow(const WindowDesc *desc, WindowNumber window_number) :
01188       QueryStringBaseWindow(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
01189       town_size(TSZ_MEDIUM),
01190       town_layout(_settings_game.economy.town_layout),
01191       params(_settings_game.game_creation.town_name)
01192   {
01193     this->InitNested(desc, window_number);
01194     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, this->max_chars);
01195     this->RandomTownName();
01196     this->UpdateButtons(true);
01197   }
01198 
01199   void RandomTownName()
01200   {
01201     this->townnamevalid = GenerateTownName(&this->townnameparts);
01202 
01203     if (!this->townnamevalid) {
01204       this->edit_str_buf[0] = '\0';
01205     } else {
01206       GetTownName(this->edit_str_buf, &this->params, this->townnameparts, &this->edit_str_buf[this->edit_str_size - 1]);
01207     }
01208     UpdateTextBufferSize(&this->text);
01209     UpdateOSKOriginalText(this, TSEW_TOWNNAME_EDITBOX);
01210 
01211     this->SetWidgetDirty(TSEW_TOWNNAME_EDITBOX);
01212   }
01213 
01214   void UpdateButtons(bool check_availability)
01215   {
01216     if (check_availability && _game_mode != GM_EDITOR) {
01217       this->SetWidgetsDisabledState(true, TSEW_RANDOMTOWN, TSEW_MANYRANDOMTOWNS, TSEW_SIZE_LARGE, WIDGET_LIST_END);
01218       this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
01219           TSEW_LAYOUT_ORIGINAL, TSEW_LAYOUT_BETTER, TSEW_LAYOUT_GRID2, TSEW_LAYOUT_GRID3, TSEW_LAYOUT_RANDOM, WIDGET_LIST_END);
01220       if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
01221     }
01222 
01223     for (int i = TSEW_SIZE_SMALL; i <= TSEW_SIZE_RANDOM; i++) {
01224       this->SetWidgetLoweredState(i, i == TSEW_SIZE_SMALL + this->town_size);
01225     }
01226 
01227     this->SetWidgetLoweredState(TSEW_CITY, this->city);
01228 
01229     for (int i = TSEW_LAYOUT_ORIGINAL; i <= TSEW_LAYOUT_RANDOM; i++) {
01230       this->SetWidgetLoweredState(i, i == TSEW_LAYOUT_ORIGINAL + this->town_layout);
01231     }
01232 
01233     this->SetDirty();
01234   }
01235 
01236   void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
01237   {
01238     const char *name = NULL;
01239 
01240     if (!this->townnamevalid) {
01241       name = this->edit_str_buf;
01242     } else {
01243       /* If user changed the name, send it */
01244       char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
01245       GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
01246       if (strcmp(buf, this->edit_str_buf) != 0) name = this->edit_str_buf;
01247     }
01248 
01249     bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
01250         townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
01251 
01252     if (success) this->RandomTownName();
01253   }
01254 
01255   virtual void OnPaint()
01256   {
01257     this->DrawWidgets();
01258     if (!this->IsShaded()) this->DrawEditBox(TSEW_TOWNNAME_EDITBOX);
01259   }
01260 
01261   virtual void OnClick(Point pt, int widget, int click_count)
01262   {
01263     switch (widget) {
01264       case TSEW_NEWTOWN:
01265         HandlePlacePushButton(this, TSEW_NEWTOWN, SPR_CURSOR_TOWN, HT_RECT);
01266         break;
01267 
01268       case TSEW_RANDOMTOWN:
01269         this->HandleButtonClick(TSEW_RANDOMTOWN);
01270         this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
01271         break;
01272 
01273       case TSEW_TOWNNAME_RANDOM:
01274         this->RandomTownName();
01275         this->SetFocusedWidget(TSEW_TOWNNAME_EDITBOX);
01276         break;
01277 
01278       case TSEW_MANYRANDOMTOWNS:
01279         this->HandleButtonClick(TSEW_MANYRANDOMTOWNS);
01280 
01281         _generating_world = true;
01282         UpdateNearestTownForRoadTiles(true);
01283         if (!GenerateTowns(this->town_layout)) {
01284           ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
01285         }
01286         UpdateNearestTownForRoadTiles(false);
01287         _generating_world = false;
01288         break;
01289 
01290       case TSEW_SIZE_SMALL: case TSEW_SIZE_MEDIUM: case TSEW_SIZE_LARGE: case TSEW_SIZE_RANDOM:
01291         this->town_size = (TownSize)(widget - TSEW_SIZE_SMALL);
01292         this->UpdateButtons(false);
01293         break;
01294 
01295       case TSEW_CITY:
01296         this->city ^= true;
01297         this->SetWidgetLoweredState(TSEW_CITY, this->city);
01298         this->SetDirty();
01299         break;
01300 
01301       case TSEW_LAYOUT_ORIGINAL: case TSEW_LAYOUT_BETTER: case TSEW_LAYOUT_GRID2:
01302       case TSEW_LAYOUT_GRID3: case TSEW_LAYOUT_RANDOM:
01303         this->town_layout = (TownLayout)(widget - TSEW_LAYOUT_ORIGINAL);
01304         this->UpdateButtons(false);
01305         break;
01306     }
01307   }
01308 
01309   virtual void OnTimeout()
01310   {
01311     this->RaiseWidget(TSEW_RANDOMTOWN);
01312     this->RaiseWidget(TSEW_MANYRANDOMTOWNS);
01313     this->SetDirty();
01314   }
01315 
01316   virtual void OnMouseLoop()
01317   {
01318     this->HandleEditBox(TSEW_TOWNNAME_EDITBOX);
01319   }
01320 
01321   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01322   {
01323     EventState state = ES_NOT_HANDLED;
01324     if (this->HandleEditBoxKey(TSEW_TOWNNAME_EDITBOX, key, keycode, state) == HEBR_CANCEL) {
01325       this->UnfocusFocusedWidget();
01326     }
01327     return state;
01328   }
01329 
01330   virtual void OnPlaceObject(Point pt, TileIndex tile)
01331   {
01332     this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
01333   }
01334 
01335   virtual void OnPlaceObjectAbort()
01336   {
01337     this->RaiseButtons();
01338     this->UpdateButtons(false);
01339   }
01340 
01346   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01347   {
01348     if (!gui_scope) return;
01349     this->UpdateButtons(true);
01350   }
01351 };
01352 
01353 static const WindowDesc _found_town_desc(
01354   WDP_AUTO, 160, 162,
01355   WC_FOUND_TOWN, WC_NONE,
01356   WDF_CONSTRUCTION,
01357   _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
01358 );
01359 
01360 void ShowFoundTownWindow()
01361 {
01362   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
01363   AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
01364 }