town_gui.cpp

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