# industry_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: industry_gui.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    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 "openttd.h"
    00014 #include "gui.h"
    00015 #include "textbuf_gui.h"
    00016 #include "command_func.h"
    00017 #include "viewport_func.h"
    00018 #include "gfx_func.h"
    00019 #include "industry.h"
    00020 #include "town.h"
    00021 #include "variables.h"
    00022 #include "cheat_type.h"
    00023 #include "newgrf.h"
    00024 #include "newgrf_industries.h"
    00025 #include "newgrf_text.h"
    00026 #include "strings_func.h"
    00027 #include "company_func.h"
    00028 #include "tilehighlight_func.h"
    00029 #include "string_func.h"
    00030 #include "sortlist_type.h"
    00031 #include "widgets/dropdown_func.h"
    00032 #include "company_base.h"
    00033 #include "core/geometry_func.hpp"
    00034 #include "core/random_func.hpp"
    00035
    00036 #include "table/strings.h"
    00037 #include "table/sprites.h"
    00038
    00039 bool _ignore_restrictions;
    00040
    00042 enum CargoSuffixType {
    00043   CST_FUND,
    00044   CST_VIEW,
    00045   CST_DIR,
    00046 };
    00047
    00063 static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, char *suffix, const char *suffix_last)
    00064 {
    00065   suffix[0] = '\0';
    00066   if (HasBit(indspec->callback_mask, CBM_IND_CARGO_SUFFIX)) {
    00067     uint16 callback = GetIndustryCallback(CBID_INDUSTRY_CARGO_SUFFIX, 0, (cst << 8) | cargo, const_cast<Industry *>(ind), ind_type, (cst != CST_FUND) ? ind->location.tile : INVALID_TILE);
    00068     if (GB(callback, 0, 8) != 0xFF) {
    00069       PrepareTextRefStackUsage(6);
    00070       GetString(suffix, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback), suffix_last);
    00071       StopTextRefStackUsage();
    00072     }
    00073   }
    00074 }
    00075
    00086 template <typename TC, typename TS>
    00087 static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, const TC &cargos, TS &suffixes)
    00088 {
    00089   assert_compile(lengthof(cargos) <= lengthof(suffixes));
    00090   for (uint j = 0; j < lengthof(cargos); j++) {
    00091     if (cargos[j] != CT_INVALID) {
    00092       GetCargoSuffix(cb_offset + j, cst, ind, ind_type, indspec, suffixes[j], lastof(suffixes[j]));
    00093     } else {
    00094       suffixes[j][0] = '\0';
    00095     }
    00096   }
    00097 }
    00098
    00100 enum DynamicPlaceIndustriesWidgets {
    00101   DPIW_MATRIX_WIDGET,
    00102   DPIW_SCROLLBAR,
    00103   DPIW_INFOPANEL,
    00104   DPIW_FUND_WIDGET,
    00105 };
    00106
    00107 static const NWidgetPart _nested_build_industry_widgets[] = {
    00108   NWidget(NWID_HORIZONTAL),
    00109     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00110     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FUND_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00111     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00112     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00113   EndContainer(),
    00114   NWidget(NWID_HORIZONTAL),
    00115     NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, DPIW_MATRIX_WIDGET), SetDataTip(0x801, STR_FUND_INDUSTRY_SELECTION_TOOLTIP), SetFill(1, 0), SetResize(1, 1),
    00116     NWidget(WWT_SCROLLBAR, COLOUR_DARK_GREEN, DPIW_SCROLLBAR),
    00117   EndContainer(),
    00118   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, DPIW_INFOPANEL), SetResize(1, 0),
    00119   EndContainer(),
    00120   NWidget(NWID_HORIZONTAL),
    00121     NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, DPIW_FUND_WIDGET), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NULL),
    00122     NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    00123   EndContainer(),
    00124 };
    00125
    00127 static const WindowDesc _build_industry_desc(
    00128   WDP_AUTO, 170, 212,
    00129   WC_BUILD_INDUSTRY, WC_NONE,
    00130   WDF_CONSTRUCTION,
    00131   _nested_build_industry_widgets, lengthof(_nested_build_industry_widgets)
    00132 );
    00133
    00135 class BuildIndustryWindow : public Window {
    00136   int selected_index;
    00137   IndustryType selected_type;
    00138   uint16 callback_timer;
    00139   bool timer_enabled;
    00140   uint16 count;
    00141   IndustryType index[NUM_INDUSTRYTYPES + 1];
    00142   bool enabled[NUM_INDUSTRYTYPES + 1];
    00143
    00145   static const int MATRIX_TEXT_OFFSET = 17;
    00146
    00147   void SetupArrays()
    00148   {
    00149     this->count = 0;
    00150
    00151     for (uint i = 0; i < lengthof(this->index); i++) {
    00152       this->index[i]   = INVALID_INDUSTRYTYPE;
    00153       this->enabled[i] = false;
    00154     }
    00155
    00156     if (_game_mode == GM_EDITOR) { // give room for the Many Random "button"
    00157       this->index[this->count] = INVALID_INDUSTRYTYPE;
    00158       this->count++;
    00159       this->timer_enabled = false;
    00160     }
    00161     /* Fill the arrays with industries.
    00162      * The tests performed after the enabled allow to load the industries
    00163      * In the same way they are inserted by grf (if any)
    00164      */
    00165     for (IndustryType ind = 0; ind < NUM_INDUSTRYTYPES; ind++) {
    00166       const IndustrySpec *indsp = GetIndustrySpec(ind);
    00167       if (indsp->enabled) {
    00168         /* Rule is that editor mode loads all industries.
    00169          * In game mode, all non raw industries are loaded too
    00170          * and raw ones are loaded only when setting allows it */
    00171         if (_game_mode != GM_EDITOR && indsp->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) {
    00172           /* Unselect if the industry is no longer in the list */
    00173           if (this->selected_type == ind) this->selected_index = -1;
    00174           continue;
    00175         }
    00176         this->index[this->count] = ind;
    00177         this->enabled[this->count] = (_game_mode == GM_EDITOR) || CheckIfCallBackAllowsAvailability(ind, IACT_USERCREATION);
    00178         /* Keep the selection to the correct line */
    00179         if (this->selected_type == ind) this->selected_index = this->count;
    00180         this->count++;
    00181       }
    00182     }
    00183
    00184     /* first indutry type is selected if the current selection is invalid.
    00185      * I'll be damned if there are none available ;) */
    00186     if (this->selected_index == -1) {
    00187       this->selected_index = 0;
    00188       this->selected_type = this->index[0];
    00189     }
    00190
    00191     this->vscroll.SetCount(this->count);
    00192   }
    00193
    00194 public:
    00195   BuildIndustryWindow() : Window()
    00196   {
    00197     this->timer_enabled = _loaded_newgrf_features.has_newindustries;
    00198
    00199     this->selected_index = -1;
    00200     this->selected_type = INVALID_INDUSTRYTYPE;
    00201
    00202     /* Initialize arrays */
    00203     this->SetupArrays();
    00204
    00205     this->callback_timer = DAY_TICKS;
    00206
    00207     this->InitNested(&_build_industry_desc, 0);
    00208   }
    00209
    00210   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00211   {
    00212     switch (widget) {
    00213       case DPIW_MATRIX_WIDGET: {
    00214         Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES);
    00215         for (byte i = 0; i < this->count; i++) {
    00216           if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
    00217           d = maxdim(d, GetStringBoundingBox(GetIndustrySpec(this->index[i])->name));
    00218         }
    00219         resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00220         d.width += MATRIX_TEXT_OFFSET + padding.width;
    00221         d.height = 5 * resize->height;
    00222         *size = maxdim(*size, d);
    00223         break;
    00224       }
    00225
    00226       case DPIW_INFOPANEL: {
    00227         /* Extra line for cost outside of editor + extra lines for 'extra' information for NewGRFs. */
    00228         int height = 2 + (_game_mode == GM_EDITOR ? 0 : 1) + (_loaded_newgrf_features.has_newindustries ? 4 : 0);
    00229         Dimension d = {0, 0};
    00230         for (byte i = 0; i < this->count; i++) {
    00231           if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
    00232
    00233           const IndustrySpec *indsp = GetIndustrySpec(this->index[i]);
    00234
    00235           char cargo_suffix[3][512];
    00236           GetAllCargoSuffixes(0, CST_FUND, NULL, this->index[i], indsp, indsp->accepts_cargo, cargo_suffix);
    00237           StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00238           byte p = 0;
    00239           SetDParam(0, STR_JUST_NOTHING);
    00240           SetDParamStr(1, "");
    00241           for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
    00242             if (indsp->accepts_cargo[j] == CT_INVALID) continue;
    00243             if (p > 0) str++;
    00244             SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
    00245             SetDParamStr(p++, cargo_suffix[j]);
    00246           }
    00247           d = maxdim(d, GetStringBoundingBox(str));
    00248
    00249           /* Draw the produced cargos, if any. Otherwhise, will print "Nothing" */
    00250           GetAllCargoSuffixes(3, CST_FUND, NULL, this->index[i], indsp, indsp->produced_cargo, cargo_suffix);
    00251           str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
    00252           p = 0;
    00253           SetDParam(0, STR_JUST_NOTHING);
    00254           SetDParamStr(1, "");
    00255           for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
    00256             if (indsp->produced_cargo[j] == CT_INVALID) continue;
    00257             if (p > 0) str++;
    00258             SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
    00259             SetDParamStr(p++, cargo_suffix[j]);
    00260           }
    00261           d = maxdim(d, GetStringBoundingBox(str));
    00262         }
    00263
    00264         /* Set it to something more sane :) */
    00265         size->height = height * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00266         size->width  = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00267       } break;
    00268
    00269       case DPIW_FUND_WIDGET: {
    00270         Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
    00271         d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY));
    00272         d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY));
    00273         d.width += padding.width;
    00274         d.height += padding.height;
    00275         *size = maxdim(*size, d);
    00276         break;
    00277       }
    00278     }
    00279   }
    00280
    00281   virtual void SetStringParameters(int widget) const
    00282   {
    00283     switch (widget) {
    00284       case DPIW_FUND_WIDGET:
    00285         /* Raw industries might be prospected. Show this fact by changing the string
    00286          * In Editor, you just build, while ingame, or you fund or you prospect */
    00287         if (_game_mode == GM_EDITOR) {
    00288           /* We've chosen many random industries but no industries have been specified */
    00289           SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
    00290         } else {
    00291           const IndustrySpec *indsp = GetIndustrySpec(this->index[this->selected_index]);
    00292           SetDParam(0, (_settings_game.construction.raw_industry_construction == 2 && indsp->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY : STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY);
    00293         }
    00294         break;
    00295     }
    00296   }
    00297
    00298   virtual void DrawWidget(const Rect &r, int widget) const
    00299   {
    00300     switch (widget) {
    00301       case DPIW_MATRIX_WIDGET:
    00302         for (byte i = 0; i < this->vscroll.GetCapacity() && i + this->vscroll.GetPosition() < this->count; i++) {
    00303           int x = r.left + WD_MATRIX_LEFT;
    00304           int y = r.top + WD_MATRIX_TOP + i * this->resize.step_height;
    00305           bool selected = this->selected_index == i + this->vscroll.GetPosition();
    00306
    00307           if (this->index[i + this->vscroll.GetPosition()] == INVALID_INDUSTRYTYPE) {
    00308             DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES, selected ? TC_WHITE : TC_ORANGE);
    00309             continue;
    00310           }
    00311           const IndustrySpec *indsp = GetIndustrySpec(this->index[i + this->vscroll.GetPosition()]);
    00312
    00313           /* Draw the name of the industry in white is selected, otherwise, in orange */
    00314           DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, indsp->name, selected ? TC_WHITE : TC_ORANGE);
    00315           GfxFillRect(x,     y + 1,  x + 10, y + 7, selected ? 15 : 0);
    00316           GfxFillRect(x + 1, y + 2,  x +  9, y + 6, indsp->map_colour);
    00317         }
    00318         break;
    00319
    00320       case DPIW_INFOPANEL: {
    00321         int y      = r.top    + WD_FRAMERECT_TOP;
    00322         int bottom = r.bottom - WD_FRAMERECT_BOTTOM;
    00323         int left   = r.left   + WD_FRAMERECT_LEFT;
    00324         int right  = r.right  - WD_FRAMERECT_RIGHT;
    00325
    00326         if (this->selected_type == INVALID_INDUSTRYTYPE) {
    00327           DrawStringMultiLine(left, right, y,  bottom, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP);
    00328           break;
    00329         }
    00330
    00331         const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00332
    00333         if (_game_mode != GM_EDITOR) {
    00334           SetDParam(0, indsp->GetConstructionCost());
    00335           DrawString(left, right, y, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST);
    00336           y += FONT_HEIGHT_NORMAL;
    00337         }
    00338
    00339         /* Draw the accepted cargos, if any. Otherwhise, will print "Nothing" */
    00340         char cargo_suffix[3][512];
    00341         GetAllCargoSuffixes(0, CST_FUND, NULL, this->selected_type, indsp, indsp->accepts_cargo, cargo_suffix);
    00342         StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00343         byte p = 0;
    00344         SetDParam(0, STR_JUST_NOTHING);
    00345         SetDParamStr(1, "");
    00346         for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
    00347           if (indsp->accepts_cargo[j] == CT_INVALID) continue;
    00348           if (p > 0) str++;
    00349           SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
    00350           SetDParamStr(p++, cargo_suffix[j]);
    00351         }
    00352         DrawString(left, right, y, str);
    00353         y += FONT_HEIGHT_NORMAL;
    00354
    00355         /* Draw the produced cargos, if any. Otherwhise, will print "Nothing" */
    00356         GetAllCargoSuffixes(3, CST_FUND, NULL, this->selected_type, indsp, indsp->produced_cargo, cargo_suffix);
    00357         str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
    00358         p = 0;
    00359         SetDParam(0, STR_JUST_NOTHING);
    00360         SetDParamStr(1, "");
    00361         for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
    00362           if (indsp->produced_cargo[j] == CT_INVALID) continue;
    00363           if (p > 0) str++;
    00364           SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
    00365           SetDParamStr(p++, cargo_suffix[j]);
    00366         }
    00367         DrawString(left, right, y, str);
    00368         y += FONT_HEIGHT_NORMAL;
    00369
    00370         /* Get the additional purchase info text, if it has not already been queried. */
    00371         str = STR_NULL;
    00372         if (HasBit(indsp->callback_mask, CBM_IND_FUND_MORE_TEXT)) {
    00373           uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT, 0, 0, NULL, this->selected_type, INVALID_TILE);
    00374           if (callback_res != CALLBACK_FAILED) {  // Did it fail?
    00375             str = GetGRFStringID(indsp->grf_prop.grffile->grfid, 0xD000 + callback_res);  // No. here's the new string
    00376           }
    00377         }
    00378
    00379         /* Draw the Additional purchase text, provided by newgrf callback, if any.
    00380          * Otherwhise, will print Nothing */
    00381         if (str != STR_NULL && str != STR_UNDEFINED) {
    00382           SetDParam(0, str);
    00383           DrawStringMultiLine(left, right, y, bottom, STR_JUST_STRING);
    00384         }
    00385       } break;
    00386     }
    00387   }
    00388
    00389   virtual void OnPaint()
    00390   {
    00391     this->DrawWidgets();
    00392   }
    00393
    00394   virtual void OnDoubleClick(Point pt, int widget)
    00395   {
    00396     if (widget != DPIW_MATRIX_WIDGET) return;
    00397     this->OnClick(pt, DPIW_FUND_WIDGET);
    00398   }
    00399
    00400   virtual void OnClick(Point pt, int widget)
    00401   {
    00402     switch (widget) {
    00403       case DPIW_MATRIX_WIDGET: {
    00404         const IndustrySpec *indsp;
    00405         int y = (pt.y - this->GetWidget<NWidgetBase>(DPIW_MATRIX_WIDGET)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
    00406
    00407         if (y >= 0 && y < count) { // Is it within the boundaries of available data?
    00408           this->selected_index = y;
    00409           this->selected_type = this->index[y];
    00410           indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00411
    00412           this->SetDirty();
    00413
    00414           if ((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
    00415               this->selected_type == INVALID_INDUSTRYTYPE) {
    00416             /* Reset the button state if going to prospecting or "build many industries" */
    00417             this->RaiseButtons();
    00418             ResetObjectToPlace();
    00419           }
    00420
    00421           this->SetWidgetDisabledState(DPIW_FUND_WIDGET, !this->enabled[this->selected_index]);
    00422         }
    00423       } break;
    00424
    00425       case DPIW_FUND_WIDGET: {
    00426         if (this->selected_type == INVALID_INDUSTRYTYPE) {
    00427           this->HandleButtonClick(DPIW_FUND_WIDGET);
    00428
    00429           if (Town::GetNumItems() == 0) {
    00430             ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES, STR_ERROR_MUST_FOUND_TOWN_FIRST, 0, 0);
    00431           } else {
    00432             extern void GenerateIndustries();
    00433             _generating_world = true;
    00434             GenerateIndustries();
    00435             _generating_world = false;
    00436           }
    00437         } else if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && GetIndustrySpec(this->selected_type)->IsRawIndustry()) {
    00438           DoCommandP(0, this->selected_type, InteractiveRandom(), CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00439           this->HandleButtonClick(DPIW_FUND_WIDGET);
    00440         } else {
    00441           HandlePlacePushButton(this, DPIW_FUND_WIDGET, SPR_CURSOR_INDUSTRY, HT_RECT, NULL);
    00442         }
    00443       } break;
    00444     }
    00445   }
    00446
    00447   virtual void OnResize()
    00448   {
    00449     /* Adjust the number of items in the matrix depending of the resize */
    00450     this->vscroll.SetCapacityFromWidget(this, DPIW_MATRIX_WIDGET);
    00451     this->GetWidget<NWidgetCore>(DPIW_MATRIX_WIDGET)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00452   }
    00453
    00454   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00455   {
    00456     bool success = true;
    00457     /* We do not need to protect ourselves against "Random Many Industries" in this mode */
    00458     const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00459     uint32 seed = InteractiveRandom();
    00460
    00461     if (_game_mode == GM_EDITOR) {
    00462       /* Show error if no town exists at all */
    00463       if (Town::GetNumItems() == 0) {
    00464         SetDParam(0, indsp->name);
    00465         ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, STR_ERROR_MUST_FOUND_TOWN_FIRST, pt.x, pt.y);
    00466         return;
    00467       }
    00468
    00469       _current_company = OWNER_NONE;
    00470       _generating_world = true;
    00471       _ignore_restrictions = true;
    00472       success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00473       if (!success) {
    00474         SetDParam(0, indsp->name);
    00475         ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, _error_message, pt.x, pt.y);
    00476       }
    00477
    00478       _ignore_restrictions = false;
    00479       _generating_world = false;
    00480     } else {
    00481       success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00482     }
    00483
    00484     /* If an industry has been built, just reset the cursor and the system */
    00485     if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00486   }
    00487
    00488   virtual void OnTick()
    00489   {
    00490     if (_pause_mode != PM_UNPAUSED) return;
    00491     if (!this->timer_enabled) return;
    00492     if (--this->callback_timer == 0) {
    00493       /* We have just passed another day.
    00494        * See if we need to update availability of currently selected industry */
    00495       this->callback_timer = DAY_TICKS; // restart counter
    00496
    00497       const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00498
    00499       if (indsp->enabled) {
    00500         bool call_back_result = CheckIfCallBackAllowsAvailability(this->selected_type, IACT_USERCREATION);
    00501
    00502         /* Only if result does match the previous state would it require a redraw. */
    00503         if (call_back_result != this->enabled[this->selected_index]) {
    00504           this->enabled[this->selected_index] = call_back_result;
    00505           this->SetWidgetDisabledState(DPIW_FUND_WIDGET, !this->enabled[this->selected_index]);
    00506           this->SetDirty();
    00507         }
    00508       }
    00509     }
    00510   }
    00511
    00512   virtual void OnTimeout()
    00513   {
    00514     this->RaiseButtons();
    00515   }
    00516
    00517   virtual void OnPlaceObjectAbort()
    00518   {
    00519     this->RaiseButtons();
    00520   }
    00521
    00522   virtual void OnInvalidateData(int data = 0)
    00523   {
    00524     this->SetupArrays();
    00525
    00526     const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00527     if (indsp == NULL) this->enabled[this->selected_index] = _settings_game.difficulty.number_industries != 0;
    00528     this->SetWidgetDisabledState(DPIW_FUND_WIDGET, !this->enabled[this->selected_index]);
    00529   }
    00530 };
    00531
    00532 void ShowBuildIndustryWindow()
    00533 {
    00534   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
    00535   if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return;
    00536   new BuildIndustryWindow();
    00537 }
    00538
    00539 static void UpdateIndustryProduction(Industry *i);
    00540
    00541 static inline bool IsProductionMinimum(const Industry *i, int pt)
    00542 {
    00543   return i->production_rate[pt] == 0;
    00544 }
    00545
    00546 static inline bool IsProductionMaximum(const Industry *i, int pt)
    00547 {
    00548   return i->production_rate[pt] >= 255;
    00549 }
    00550
    00551 static inline bool IsProductionAlterable(const Industry *i)
    00552 {
    00553   return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
    00554       (i->accepts_cargo[0] == CT_INVALID || i->accepts_cargo[0] == CT_VALUABLES));
    00555 }
    00556
    00558 enum IndustryViewWidgets {
    00559   IVW_CAPTION,
    00560   IVW_VIEWPORT,
    00561   IVW_INFO,
    00562   IVW_GOTO,
    00563 };
    00564
    00565 class IndustryViewWindow : public Window
    00566 {
    00567   byte editbox_line;
    00568   byte clicked_line;
    00569   byte clicked_button;
    00570   byte production_offset_y;
    00571   int info_height;
    00572
    00573 public:
    00574   IndustryViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00575   {
    00576     this->flags4 |= WF_DISABLE_VP_SCROLL;
    00577     this->editbox_line = 0;
    00578     this->clicked_line = 0;
    00579     this->clicked_button = 0;
    00580     this->info_height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + 1; // Info panel has at least two lines text.
    00581
    00582     this->InitNested(desc, window_number);
    00583     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
    00584     nvp->InitializeViewport(this, Industry::Get(window_number)->location.tile + TileDiffXY(1, 1), ZOOM_LVL_INDUSTRY);
    00585   }
    00586
    00587   virtual void OnPaint()
    00588   {
    00589     this->DrawWidgets();
    00590
    00591     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
    00592
    00593     NWidgetBase *nwi = this->GetWidget<NWidgetBase>(IVW_INFO);
    00594     uint expected = this->DrawInfo(nwi->pos_x, nwi->pos_x + nwi->current_x - 1, nwi->pos_y) - nwi->pos_y;
    00595     if (expected > nwi->current_y - 1) {
    00596       this->info_height = expected + 1;
    00597       this->ReInit();
    00598       return;
    00599     }
    00600   }
    00601
    00608   int DrawInfo(uint left, uint right, uint top)
    00609   {
    00610     Industry *i = Industry::Get(this->window_number);
    00611     const IndustrySpec *ind = GetIndustrySpec(i->type);
    00612     int y = top + WD_FRAMERECT_TOP;
    00613     bool first = true;
    00614     bool has_accept = false;
    00615     char cargo_suffix[3][512];
    00616
    00617     if (HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) {
    00618       GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
    00619       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00620         if (i->accepts_cargo[j] == CT_INVALID) continue;
    00621         has_accept = true;
    00622         if (first) {
    00623           DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_FOR_PROCESSING);
    00624           y += FONT_HEIGHT_NORMAL;
    00625           first = false;
    00626         }
    00627         SetDParam(0, i->accepts_cargo[j]);
    00628         SetDParam(1, i->incoming_cargo_waiting[j]);
    00629         SetDParamStr(2, cargo_suffix[j]);
    00630         DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_STOCKPILE_CARGO);
    00631         y += FONT_HEIGHT_NORMAL;
    00632       }
    00633     } else {
    00634       GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
    00635       StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00636       byte p = 0;
    00637       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00638         if (i->accepts_cargo[j] == CT_INVALID) continue;
    00639         has_accept = true;
    00640         if (p > 0) str++;
    00641         SetDParam(p++, CargoSpec::Get(i->accepts_cargo[j])->name);
    00642         SetDParamStr(p++, cargo_suffix[j]);
    00643       }
    00644       if (has_accept) {
    00645         DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, str);
    00646         y += FONT_HEIGHT_NORMAL;
    00647       }
    00648     }
    00649
    00650     GetAllCargoSuffixes(3, CST_VIEW, i, i->type, ind, i->produced_cargo, cargo_suffix);
    00651     first = true;
    00652     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00653       if (i->produced_cargo[j] == CT_INVALID) continue;
    00654       if (first) {
    00655         if (has_accept) y += WD_PAR_VSEP_WIDE;
    00656         DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE);
    00657         y += FONT_HEIGHT_NORMAL;
    00658         this->production_offset_y = y;
    00659         first = false;
    00660       }
    00661
    00662       SetDParam(0, i->produced_cargo[j]);
    00663       SetDParam(1, i->last_month_production[j]);
    00664       SetDParamStr(2, cargo_suffix[j]);
    00665       SetDParam(3, ToPercent8(i->last_month_pct_transported[j]));
    00666       uint x = left + WD_FRAMETEXT_LEFT + (IsProductionAlterable(i) ? 30 : 0);
    00667       DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_TRANSPORTED);
    00668       /* Let's put out those buttons.. */
    00669       if (IsProductionAlterable(i)) {
    00670         DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == j + 1) ? this->clicked_button : 0,
    00671             !IsProductionMinimum(i, j), !IsProductionMaximum(i, j));
    00672       }
    00673       y += FONT_HEIGHT_NORMAL;
    00674     }
    00675
    00676     /* Get the extra message for the GUI */
    00677     if (HasBit(ind->callback_mask, CBM_IND_WINDOW_MORE_TEXT)) {
    00678       uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->location.tile);
    00679       if (callback_res != CALLBACK_FAILED) {
    00680         StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
    00681         if (message != STR_NULL && message != STR_UNDEFINED) {
    00682           y += WD_PAR_VSEP_WIDE;
    00683
    00684           PrepareTextRefStackUsage(6);
    00685           /* Use all the available space left from where we stand up to the
    00686            * end of the window. We ALSO enlarge the window if needed, so we
    00687            * can 'go' wild with the bottom of the window. */
    00688           y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, message);
    00689           StopTextRefStackUsage();
    00690         }
    00691       }
    00692     }
    00693     return y + WD_FRAMERECT_BOTTOM;
    00694   }
    00695
    00696   virtual void SetStringParameters(int widget) const
    00697   {
    00698     if (widget== IVW_CAPTION) SetDParam(0, this->window_number);
    00699   }
    00700
    00701   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00702   {
    00703     if (widget == IVW_INFO) size->height = this->info_height;
    00704   }
    00705
    00706   virtual void OnClick(Point pt, int widget)
    00707   {
    00708     Industry *i;
    00709
    00710     switch (widget) {
    00711       case IVW_INFO: {
    00712         i = Industry::Get(this->window_number);
    00713
    00714         /* We should work if needed.. */
    00715         if (!IsProductionAlterable(i)) return;
    00716         uint x = pt.x;
    00717         int line = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
    00718         if (pt.y >= this->production_offset_y && IsInsideMM(line, 0, 2) && i->produced_cargo[line] != CT_INVALID) {
    00719           NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
    00720           uint left = nwi->pos_x + WD_FRAMETEXT_LEFT;
    00721           uint right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
    00722           if (IsInsideMM(x, left, left + 20) ) {
    00723             /* Clicked buttons, decrease or increase production */
    00724             if (x < left + 10) {
    00725               if (IsProductionMinimum(i, line)) return;
    00726               i->production_rate[line] = max(i->production_rate[line] / 2, 0);
    00727             } else {
    00728               /* a zero production industry is unlikely to give anything but zero, so push it a little bit */
    00729               int new_prod = i->production_rate[line] == 0 ? 1 : i->production_rate[line] * 2;
    00730               if (IsProductionMaximum(i, line)) return;
    00731               i->production_rate[line] = minu(new_prod, 255);
    00732             }
    00733
    00734             UpdateIndustryProduction(i);
    00735             this->SetDirty();
    00736             this->flags4 |= WF_TIMEOUT_BEGIN;
    00737             this->clicked_line = line + 1;
    00738             this->clicked_button = (x < left + 10 ? 1 : 2);
    00739           } else if (IsInsideMM(x, left + 30, right)) {
    00740             /* clicked the text */
    00741             this->editbox_line = line;
    00742             SetDParam(0, i->production_rate[line] * 8);
    00743             ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION, 10, 100, this, CS_ALPHANUMERAL, QSF_NONE);
    00744           }
    00745         }
    00746       } break;
    00747
    00748       case IVW_GOTO:
    00749         i = Industry::Get(this->window_number);
    00750         if (_ctrl_pressed) {
    00751           ShowExtraViewPortWindow(i->location.tile + TileDiffXY(1, 1));
    00752         } else {
    00753           ScrollMainWindowToTile(i->location.tile + TileDiffXY(1, 1));
    00754         }
    00755         break;
    00756     }
    00757   }
    00758
    00759   virtual void OnTimeout()
    00760   {
    00761     this->clicked_line = 0;
    00762     this->clicked_button = 0;
    00763     this->SetDirty();
    00764   }
    00765
    00766   virtual void OnResize()
    00767   {
    00768     if (this->viewport != NULL) {
    00769       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
    00770       nvp->UpdateViewportCoordinates(this);
    00771     }
    00772   }
    00773
    00774   virtual void OnQueryTextFinished(char *str)
    00775   {
    00776     if (StrEmpty(str)) return;
    00777
    00778     Industry *i = Industry::Get(this->window_number);
    00779     int line = this->editbox_line;
    00780
    00781     i->production_rate[line] = ClampU(atoi(str) / 8, 0, 255);
    00782     UpdateIndustryProduction(i);
    00783     this->SetDirty();
    00784   }
    00785 };
    00786
    00787 static void UpdateIndustryProduction(Industry *i)
    00788 {
    00789   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00790     if (i->produced_cargo[j] != CT_INVALID) {
    00791       i->last_month_production[j] = 8 * i->production_rate[j];
    00792     }
    00793   }
    00794 }
    00795
    00797 static const NWidgetPart _nested_industry_view_widgets[] = {
    00798   NWidget(NWID_HORIZONTAL),
    00799     NWidget(WWT_CLOSEBOX, COLOUR_CREAM),
    00800     NWidget(WWT_CAPTION, COLOUR_CREAM, IVW_CAPTION), SetDataTip(STR_INDUSTRY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00801     NWidget(WWT_SHADEBOX, COLOUR_CREAM),
    00802     NWidget(WWT_STICKYBOX, COLOUR_CREAM),
    00803   EndContainer(),
    00804   NWidget(WWT_PANEL, COLOUR_CREAM),
    00805     NWidget(WWT_INSET, COLOUR_CREAM), SetPadding(2, 2, 2, 2),
    00806       NWidget(NWID_VIEWPORT, INVALID_COLOUR, IVW_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetPadding(1, 1, 1, 1), SetResize(1, 1),
    00807     EndContainer(),
    00808   EndContainer(),
    00809   NWidget(WWT_PANEL, COLOUR_CREAM, IVW_INFO), SetMinimalSize(260, 2), SetResize(1, 0),
    00810   EndContainer(),
    00811   NWidget(NWID_HORIZONTAL),
    00812     NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, IVW_GOTO), SetMinimalSize(130, 12), SetDataTip(STR_BUTTON_LOCATION, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP),
    00813     NWidget(WWT_PANEL, COLOUR_CREAM), SetResize(1, 0), EndContainer(),
    00814     NWidget(WWT_RESIZEBOX, COLOUR_CREAM),
    00815   EndContainer(),
    00816 };
    00817
    00819 static const WindowDesc _industry_view_desc(
    00820   WDP_AUTO, 260, 120,
    00821   WC_INDUSTRY_VIEW, WC_NONE,
    00822   WDF_UNCLICK_BUTTONS,
    00823   _nested_industry_view_widgets, lengthof(_nested_industry_view_widgets)
    00824 );
    00825
    00826 void ShowIndustryViewWindow(int industry)
    00827 {
    00828   AllocateWindowDescFront<IndustryViewWindow>(&_industry_view_desc, industry);
    00829 }
    00830
    00832 enum IndustryDirectoryWidgets {
    00833   IDW_DROPDOWN_ORDER,
    00834   IDW_DROPDOWN_CRITERIA,
    00835   IDW_INDUSTRY_LIST,
    00836   IDW_SCROLLBAR,
    00837 };
    00838
    00840 static const NWidgetPart _nested_industry_directory_widgets[] = {
    00841   NWidget(NWID_HORIZONTAL),
    00842     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    00843     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00844     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    00845     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    00846   EndContainer(),
    00847   NWidget(NWID_HORIZONTAL),
    00848     NWidget(NWID_VERTICAL),
    00849       NWidget(NWID_HORIZONTAL),
    00850         NWidget(WWT_TEXTBTN, COLOUR_BROWN, IDW_DROPDOWN_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    00851         NWidget(WWT_DROPDOWN, COLOUR_BROWN, IDW_DROPDOWN_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIAP),
    00852         NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
    00853       EndContainer(),
    00854       NWidget(WWT_PANEL, COLOUR_BROWN, IDW_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), EndContainer(),
    00855     EndContainer(),
    00856     NWidget(NWID_VERTICAL),
    00857       NWidget(WWT_SCROLLBAR, COLOUR_BROWN, IDW_SCROLLBAR),
    00858       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    00859     EndContainer(),
    00860   EndContainer(),
    00861 };
    00862
    00863 typedef GUIList<const Industry*> GUIIndustryList;
    00864
    00865
    00869 class IndustryDirectoryWindow : public Window {
    00870 protected:
    00871   /* Runtime saved values */
    00872   static Listing last_sorting;
    00873   static const Industry *last_industry;
    00874
    00875   /* Constants for sorting stations */
    00876   static const StringID sorter_names[];
    00877   static GUIIndustryList::SortFunction * const sorter_funcs[];
    00878
    00879   GUIIndustryList industries;
    00880
    00882   void BuildSortIndustriesList()
    00883   {
    00884     if (this->industries.NeedRebuild()) {
    00885       this->industries.Clear();
    00886
    00887       const Industry *i;
    00888       FOR_ALL_INDUSTRIES(i) {
    00889         *this->industries.Append() = i;
    00890       }
    00891
    00892       this->industries.Compact();
    00893       this->industries.RebuildDone();
    00894       this->vscroll.SetCount(this->industries.Length()); // Update scrollbar as well.
    00895     }
    00896
    00897     if (!this->industries.Sort()) return;
    00898     IndustryDirectoryWindow::last_industry = NULL; // Reset name sorter sort cache
    00899     this->SetWidgetDirty(IDW_INDUSTRY_LIST); // Set the modified widget dirty
    00900   }
    00901
    00909   static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id)
    00910   {
    00911     assert(id < lengthof(i->produced_cargo));
    00912
    00913     if (i->produced_cargo[id] == CT_INVALID) return 101;
    00914     return ToPercent8(i->last_month_pct_transported[id]);
    00915   }
    00916
    00924   static int GetCargoTransportedSortValue(const Industry *i)
    00925   {
    00926     int p1 = GetCargoTransportedPercentsIfValid(i, 0);
    00927     int p2 = GetCargoTransportedPercentsIfValid(i, 1);
    00928
    00929     if (p1 > p2) Swap(p1, p2); // lower value has higher priority
    00930
    00931     return (p1 << 8) + p2;
    00932   }
    00933
    00935   static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b)
    00936   {
    00937     static char buf_cache[96];
    00938     static char buf[96];
    00939
    00940     SetDParam(0, (*a)->town->index);
    00941     GetString(buf, STR_TOWN_NAME, lastof(buf));
    00942
    00943     if (*b != last_industry) {
    00944       last_industry = *b;
    00945       SetDParam(0, (*b)->town->index);
    00946       GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
    00947     }
    00948
    00949     return strcmp(buf, buf_cache);
    00950   }
    00951
    00953   static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b)
    00954   {
    00955     int r = (*a)->type - (*b)->type;
    00956     return (r == 0) ? IndustryNameSorter(a, b) : r;
    00957   }
    00958
    00960   static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b)
    00961   {
    00962     int r = 0;
    00963
    00964     if ((*a)->produced_cargo[0] == CT_INVALID) {
    00965       if ((*b)->produced_cargo[0] != CT_INVALID) return -1;
    00966     } else {
    00967       if ((*b)->produced_cargo[0] == CT_INVALID) return 1;
    00968
    00969       r = ((*a)->last_month_production[0] + (*a)->last_month_production[1]) -
    00970           ((*b)->last_month_production[0] + (*b)->last_month_production[1]);
    00971     }
    00972
    00973     return (r == 0) ? IndustryNameSorter(a, b) : r;
    00974   }
    00975
    00977   static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b)
    00978   {
    00979     int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b);
    00980     return (r == 0) ? IndustryNameSorter(a, b) : r;
    00981   }
    00982
    00988   StringID GetIndustryString(const Industry *i) const
    00989   {
    00990     const IndustrySpec *indsp = GetIndustrySpec(i->type);
    00991     byte p = 0;
    00992
    00993     /* Industry name */
    00994     SetDParam(p++, i->index);
    00995
    00996     static char cargo_suffix[lengthof(i->produced_cargo)][512];
    00997     GetAllCargoSuffixes(3, CST_DIR, i, i->type, indsp, i->produced_cargo, cargo_suffix);
    00998
    00999     /* Industry productions */
    01000     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01001       if (i->produced_cargo[j] == CT_INVALID) continue;
    01002       SetDParam(p++, i->produced_cargo[j]);
    01003       SetDParam(p++, i->last_month_production[j]);
    01004       SetDParamStr(p++, cargo_suffix[j]);
    01005     }
    01006
    01007     /* Transported productions */
    01008     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01009       if (i->produced_cargo[j] == CT_INVALID) continue;
    01010       SetDParam(p++, ToPercent8(i->last_month_pct_transported[j]));
    01011     }
    01012
    01013     /* Drawing the right string */
    01014     switch (p) {
    01015       case 1:  return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
    01016       case 5:  return STR_INDUSTRY_DIRECTORY_ITEM;
    01017       default: return STR_INDUSTRY_DIRECTORY_ITEM_TWO;
    01018     }
    01019   }
    01020
    01021 public:
    01022   IndustryDirectoryWindow(const WindowDesc *desc, WindowNumber number) : Window()
    01023   {
    01024     this->industries.SetListing(this->last_sorting);
    01025     this->industries.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs);
    01026     this->industries.ForceRebuild();
    01027     this->BuildSortIndustriesList();
    01028
    01029     this->InitNested(desc, 0);
    01030   }
    01031
    01032   ~IndustryDirectoryWindow()
    01033   {
    01034     this->last_sorting = this->industries.GetListing();
    01035   }
    01036
    01037   virtual void SetStringParameters(int widget) const
    01038   {
    01039     if (widget == IDW_DROPDOWN_CRITERIA) SetDParam(0, IndustryDirectoryWindow::sorter_names[this->industries.SortType()]);
    01040   }
    01041
    01042   virtual void OnPaint()
    01043   {
    01044     this->DrawWidgets();
    01045   }
    01046
    01047   virtual void DrawWidget(const Rect &r, int widget) const
    01048   {
    01049     switch (widget) {
    01050       case IDW_DROPDOWN_ORDER:
    01051         this->DrawSortButtonState(widget, this->industries.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    01052         break;
    01053
    01054       case IDW_INDUSTRY_LIST: {
    01055         int n = 0;
    01056         int y = r.top + WD_FRAMERECT_TOP;
    01057         if (this->industries.Length() == 0) {
    01058           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_DIRECTORY_NONE);
    01059           break;
    01060         }
    01061         for (uint i = this->vscroll.GetPosition(); i < this->industries.Length(); i++) {
    01062           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, this->GetIndustryString(this->industries[i]));
    01063
    01064           y += this->resize.step_height;
    01065           if (++n == this->vscroll.GetCapacity()) break; // max number of industries in 1 window
    01066         }
    01067       } break;
    01068     }
    01069   }
    01070
    01071   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01072   {
    01073     switch (widget) {
    01074       case IDW_DROPDOWN_ORDER: {
    01075         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    01076         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the word is centered, also looks nice.
    01077         d.height += padding.height;
    01078         *size = maxdim(*size, d);
    01079         break;
    01080       }
    01081
    01082       case IDW_DROPDOWN_CRITERIA: {
    01083         Dimension d = {0, 0};
    01084         for (uint i = 0; IndustryDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
    01085           d = maxdim(d, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names[i]));
    01086         }
    01087         d.width += padding.width;
    01088         d.height += padding.height;
    01089         *size = maxdim(*size, d);
    01090         break;
    01091       }
    01092
    01093       case IDW_INDUSTRY_LIST: {
    01094         Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE);
    01095         for (uint i = 0; i < this->industries.Length(); i++) {
    01096           d = maxdim(d, GetStringBoundingBox(this->GetIndustryString(this->industries[i])));
    01097         }
    01098         resize->height = d.height;
    01099         d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    01100         d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01101         *size = maxdim(*size, d);
    01102         break;
    01103       }
    01104     }
    01105   }
    01106
    01107
    01108   virtual void OnClick(Point pt, int widget)
    01109   {
    01110     switch (widget) {
    01111       case IDW_DROPDOWN_ORDER:
    01112         this->industries.ToggleSortOrder();
    01113         this->SetDirty();
    01114         break;
    01115
    01116       case IDW_DROPDOWN_CRITERIA:
    01117         ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names, this->industries.SortType(), IDW_DROPDOWN_CRITERIA, 0, 0);
    01118         break;
    01119
    01120       case IDW_INDUSTRY_LIST: {
    01121         int y = (pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height;
    01122         uint16 p;
    01123
    01124         if (!IsInsideMM(y, 0, this->vscroll.GetCapacity())) return;
    01125         p = y + this->vscroll.GetPosition();
    01126         if (p < this->industries.Length()) {
    01127           if (_ctrl_pressed) {
    01128             ShowExtraViewPortWindow(this->industries[p]->location.tile);
    01129           } else {
    01130             ScrollMainWindowToTile(this->industries[p]->location.tile);
    01131           }
    01132         }
    01133       } break;
    01134     }
    01135   }
    01136
    01137   virtual void OnDropdownSelect(int widget, int index)
    01138   {
    01139     if (this->industries.SortType() != index) {
    01140       this->industries.SetSortType(index);
    01141       this->BuildSortIndustriesList();
    01142     }
    01143   }
    01144
    01145   virtual void OnResize()
    01146   {
    01147     this->vscroll.SetCapacityFromWidget(this, IDW_INDUSTRY_LIST);
    01148   }
    01149
    01150   virtual void OnHundredthTick()
    01151   {
    01152     this->industries.ForceResort();
    01153     this->BuildSortIndustriesList();
    01154   }
    01155
    01156   virtual void OnInvalidateData(int data)
    01157   {
    01158     if (data == 0) {
    01159       this->industries.ForceRebuild();
    01160     } else {
    01161       this->industries.ForceResort();
    01162     }
    01163     this->BuildSortIndustriesList();
    01164   }
    01165 };
    01166
    01167 Listing IndustryDirectoryWindow::last_sorting = {false, 0};
    01168 const Industry *IndustryDirectoryWindow::last_industry = NULL;
    01169
    01170 /* Availible station sorting functions */
    01171 GUIIndustryList::SortFunction * const IndustryDirectoryWindow::sorter_funcs[] = {
    01172   &IndustryNameSorter,
    01173   &IndustryTypeSorter,
    01174   &IndustryProductionSorter,
    01175   &IndustryTransportedCargoSorter
    01176 };
    01177
    01178 /* Names of the sorting functions */
    01179 const StringID IndustryDirectoryWindow::sorter_names[] = {
    01180   STR_SORT_BY_NAME,
    01181   STR_SORT_BY_TYPE,
    01182   STR_SORT_BY_PRODUCTION,
    01183   STR_SORT_BY_TRANSPORTED,
    01184   INVALID_STRING_ID
    01185 };
    01186
    01187
    01189 static const WindowDesc _industry_directory_desc(
    01190   WDP_AUTO, 428, 190,
    01191   WC_INDUSTRY_DIRECTORY, WC_NONE,
    01192   WDF_UNCLICK_BUTTONS,
    01193   _nested_industry_directory_widgets, lengthof(_nested_industry_directory_widgets)
    01194 );
    01195
    01196 void ShowIndustryDirectory()
    01197 {
    01198   AllocateWindowDescFront<IndustryDirectoryWindow>(&_industry_directory_desc, 0);
    01199 }