# town_gui.cpp

Go to the documentation of this file.

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