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