town_gui.cpp

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