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