# industry_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: industry_gui.cpp 19087 2010-02-10 20:20:18Z 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 OnClick(Point pt, int widget, int click_count)
    00395   {
    00396     switch (widget) {
    00397       case DPIW_MATRIX_WIDGET: {
    00398         const IndustrySpec *indsp;
    00399         int y = (pt.y - this->GetWidget<NWidgetBase>(DPIW_MATRIX_WIDGET)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
    00400
    00401         if (y >= 0 && y < count) { // Is it within the boundaries of available data?
    00402           this->selected_index = y;
    00403           this->selected_type = this->index[y];
    00404           indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00405
    00406           this->SetDirty();
    00407
    00408           if ((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
    00409               this->selected_type == INVALID_INDUSTRYTYPE) {
    00410             /* Reset the button state if going to prospecting or "build many industries" */
    00411             this->RaiseButtons();
    00412             ResetObjectToPlace();
    00413           }
    00414
    00415           this->SetWidgetDisabledState(DPIW_FUND_WIDGET, !this->enabled[this->selected_index]);
    00416           if (this->enabled[this->selected_index] && click_count > 1) this->OnClick(pt, DPIW_FUND_WIDGET, 1);
    00417         }
    00418       } break;
    00419
    00420       case DPIW_FUND_WIDGET: {
    00421         if (this->selected_type == INVALID_INDUSTRYTYPE) {
    00422           this->HandleButtonClick(DPIW_FUND_WIDGET);
    00423
    00424           if (Town::GetNumItems() == 0) {
    00425             ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES, STR_ERROR_MUST_FOUND_TOWN_FIRST, 0, 0);
    00426           } else {
    00427             extern void GenerateIndustries();
    00428             _generating_world = true;
    00429             GenerateIndustries();
    00430             _generating_world = false;
    00431           }
    00432         } else if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && GetIndustrySpec(this->selected_type)->IsRawIndustry()) {
    00433           DoCommandP(0, this->selected_type, InteractiveRandom(), CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00434           this->HandleButtonClick(DPIW_FUND_WIDGET);
    00435         } else {
    00436           HandlePlacePushButton(this, DPIW_FUND_WIDGET, SPR_CURSOR_INDUSTRY, HT_RECT, NULL);
    00437         }
    00438       } break;
    00439     }
    00440   }
    00441
    00442   virtual void OnResize()
    00443   {
    00444     /* Adjust the number of items in the matrix depending of the resize */
    00445     this->vscroll.SetCapacityFromWidget(this, DPIW_MATRIX_WIDGET);
    00446     this->GetWidget<NWidgetCore>(DPIW_MATRIX_WIDGET)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00447   }
    00448
    00449   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00450   {
    00451     bool success = true;
    00452     /* We do not need to protect ourselves against "Random Many Industries" in this mode */
    00453     const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00454     uint32 seed = InteractiveRandom();
    00455
    00456     if (_game_mode == GM_EDITOR) {
    00457       /* Show error if no town exists at all */
    00458       if (Town::GetNumItems() == 0) {
    00459         SetDParam(0, indsp->name);
    00460         ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, STR_ERROR_MUST_FOUND_TOWN_FIRST, pt.x, pt.y);
    00461         return;
    00462       }
    00463
    00464       _current_company = OWNER_NONE;
    00465       _generating_world = true;
    00466       _ignore_restrictions = true;
    00467       success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00468       if (!success) {
    00469         SetDParam(0, indsp->name);
    00470         ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, _error_message, pt.x, pt.y);
    00471       }
    00472
    00473       _ignore_restrictions = false;
    00474       _generating_world = false;
    00475     } else {
    00476       success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00477     }
    00478
    00479     /* If an industry has been built, just reset the cursor and the system */
    00480     if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00481   }
    00482
    00483   virtual void OnTick()
    00484   {
    00485     if (_pause_mode != PM_UNPAUSED) return;
    00486     if (!this->timer_enabled) return;
    00487     if (--this->callback_timer == 0) {
    00488       /* We have just passed another day.
    00489        * See if we need to update availability of currently selected industry */
    00490       this->callback_timer = DAY_TICKS; // restart counter
    00491
    00492       const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00493
    00494       if (indsp->enabled) {
    00495         bool call_back_result = CheckIfCallBackAllowsAvailability(this->selected_type, IACT_USERCREATION);
    00496
    00497         /* Only if result does match the previous state would it require a redraw. */
    00498         if (call_back_result != this->enabled[this->selected_index]) {
    00499           this->enabled[this->selected_index] = call_back_result;
    00500           this->SetWidgetDisabledState(DPIW_FUND_WIDGET, !this->enabled[this->selected_index]);
    00501           this->SetDirty();
    00502         }
    00503       }
    00504     }
    00505   }
    00506
    00507   virtual void OnTimeout()
    00508   {
    00509     this->RaiseButtons();
    00510   }
    00511
    00512   virtual void OnPlaceObjectAbort()
    00513   {
    00514     this->RaiseButtons();
    00515   }
    00516
    00517   virtual void OnInvalidateData(int data = 0)
    00518   {
    00519     this->SetupArrays();
    00520
    00521     const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00522     if (indsp == NULL) this->enabled[this->selected_index] = _settings_game.difficulty.number_industries != 0;
    00523     this->SetWidgetDisabledState(DPIW_FUND_WIDGET, !this->enabled[this->selected_index]);
    00524   }
    00525 };
    00526
    00527 void ShowBuildIndustryWindow()
    00528 {
    00529   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
    00530   if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return;
    00531   new BuildIndustryWindow();
    00532 }
    00533
    00534 static void UpdateIndustryProduction(Industry *i);
    00535
    00536 static inline bool IsProductionMinimum(const Industry *i, int pt)
    00537 {
    00538   return i->production_rate[pt] == 0;
    00539 }
    00540
    00541 static inline bool IsProductionMaximum(const Industry *i, int pt)
    00542 {
    00543   return i->production_rate[pt] >= 255;
    00544 }
    00545
    00546 static inline bool IsProductionAlterable(const Industry *i)
    00547 {
    00548   return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
    00549       (i->accepts_cargo[0] == CT_INVALID || i->accepts_cargo[0] == CT_VALUABLES));
    00550 }
    00551
    00553 enum IndustryViewWidgets {
    00554   IVW_CAPTION,
    00555   IVW_VIEWPORT,
    00556   IVW_INFO,
    00557   IVW_GOTO,
    00558 };
    00559
    00560 class IndustryViewWindow : public Window
    00561 {
    00562   byte editbox_line;
    00563   byte clicked_line;
    00564   byte clicked_button;
    00565   byte production_offset_y;
    00566   int info_height;
    00567
    00568 public:
    00569   IndustryViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00570   {
    00571     this->flags4 |= WF_DISABLE_VP_SCROLL;
    00572     this->editbox_line = 0;
    00573     this->clicked_line = 0;
    00574     this->clicked_button = 0;
    00575     this->info_height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + 1; // Info panel has at least two lines text.
    00576
    00577     this->InitNested(desc, window_number);
    00578     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
    00579     nvp->InitializeViewport(this, Industry::Get(window_number)->location.tile + TileDiffXY(1, 1), ZOOM_LVL_INDUSTRY);
    00580   }
    00581
    00582   virtual void OnPaint()
    00583   {
    00584     this->DrawWidgets();
    00585
    00586     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
    00587
    00588     NWidgetBase *nwi = this->GetWidget<NWidgetBase>(IVW_INFO);
    00589     uint expected = this->DrawInfo(nwi->pos_x, nwi->pos_x + nwi->current_x - 1, nwi->pos_y) - nwi->pos_y;
    00590     if (expected > nwi->current_y - 1) {
    00591       this->info_height = expected + 1;
    00592       this->ReInit();
    00593       return;
    00594     }
    00595   }
    00596
    00603   int DrawInfo(uint left, uint right, uint top)
    00604   {
    00605     Industry *i = Industry::Get(this->window_number);
    00606     const IndustrySpec *ind = GetIndustrySpec(i->type);
    00607     int y = top + WD_FRAMERECT_TOP;
    00608     bool first = true;
    00609     bool has_accept = false;
    00610     char cargo_suffix[3][512];
    00611
    00612     if (HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) {
    00613       GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
    00614       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00615         if (i->accepts_cargo[j] == CT_INVALID) continue;
    00616         has_accept = true;
    00617         if (first) {
    00618           DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_FOR_PROCESSING);
    00619           y += FONT_HEIGHT_NORMAL;
    00620           first = false;
    00621         }
    00622         SetDParam(0, i->accepts_cargo[j]);
    00623         SetDParam(1, i->incoming_cargo_waiting[j]);
    00624         SetDParamStr(2, cargo_suffix[j]);
    00625         DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_STOCKPILE_CARGO);
    00626         y += FONT_HEIGHT_NORMAL;
    00627       }
    00628     } else {
    00629       GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
    00630       StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00631       byte p = 0;
    00632       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00633         if (i->accepts_cargo[j] == CT_INVALID) continue;
    00634         has_accept = true;
    00635         if (p > 0) str++;
    00636         SetDParam(p++, CargoSpec::Get(i->accepts_cargo[j])->name);
    00637         SetDParamStr(p++, cargo_suffix[j]);
    00638       }
    00639       if (has_accept) {
    00640         DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, str);
    00641         y += FONT_HEIGHT_NORMAL;
    00642       }
    00643     }
    00644
    00645     GetAllCargoSuffixes(3, CST_VIEW, i, i->type, ind, i->produced_cargo, cargo_suffix);
    00646     first = true;
    00647     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00648       if (i->produced_cargo[j] == CT_INVALID) continue;
    00649       if (first) {
    00650         if (has_accept) y += WD_PAR_VSEP_WIDE;
    00651         DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE);
    00652         y += FONT_HEIGHT_NORMAL;
    00653         this->production_offset_y = y;
    00654         first = false;
    00655       }
    00656
    00657       SetDParam(0, i->produced_cargo[j]);
    00658       SetDParam(1, i->last_month_production[j]);
    00659       SetDParamStr(2, cargo_suffix[j]);
    00660       SetDParam(3, ToPercent8(i->last_month_pct_transported[j]));
    00661       uint x = left + WD_FRAMETEXT_LEFT + (IsProductionAlterable(i) ? 30 : 0);
    00662       DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_TRANSPORTED);
    00663       /* Let's put out those buttons.. */
    00664       if (IsProductionAlterable(i)) {
    00665         DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == j + 1) ? this->clicked_button : 0,
    00666             !IsProductionMinimum(i, j), !IsProductionMaximum(i, j));
    00667       }
    00668       y += FONT_HEIGHT_NORMAL;
    00669     }
    00670
    00671     /* Get the extra message for the GUI */
    00672     if (HasBit(ind->callback_mask, CBM_IND_WINDOW_MORE_TEXT)) {
    00673       uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->location.tile);
    00674       if (callback_res != CALLBACK_FAILED) {
    00675         StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
    00676         if (message != STR_NULL && message != STR_UNDEFINED) {
    00677           y += WD_PAR_VSEP_WIDE;
    00678
    00679           PrepareTextRefStackUsage(6);
    00680           /* Use all the available space left from where we stand up to the
    00681            * end of the window. We ALSO enlarge the window if needed, so we
    00682            * can 'go' wild with the bottom of the window. */
    00683           y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, message);
    00684           StopTextRefStackUsage();
    00685         }
    00686       }
    00687     }
    00688     return y + WD_FRAMERECT_BOTTOM;
    00689   }
    00690
    00691   virtual void SetStringParameters(int widget) const
    00692   {
    00693     if (widget== IVW_CAPTION) SetDParam(0, this->window_number);
    00694   }
    00695
    00696   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00697   {
    00698     if (widget == IVW_INFO) size->height = this->info_height;
    00699   }
    00700
    00701   virtual void OnClick(Point pt, int widget, int click_count)
    00702   {
    00703     Industry *i;
    00704
    00705     switch (widget) {
    00706       case IVW_INFO: {
    00707         i = Industry::Get(this->window_number);
    00708
    00709         /* We should work if needed.. */
    00710         if (!IsProductionAlterable(i)) return;
    00711         uint x = pt.x;
    00712         int line = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
    00713         if (pt.y >= this->production_offset_y && IsInsideMM(line, 0, 2) && i->produced_cargo[line] != CT_INVALID) {
    00714           NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
    00715           uint left = nwi->pos_x + WD_FRAMETEXT_LEFT;
    00716           uint right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
    00717           if (IsInsideMM(x, left, left + 20) ) {
    00718             /* Clicked buttons, decrease or increase production */
    00719             if (x < left + 10) {
    00720               if (IsProductionMinimum(i, line)) return;
    00721               i->production_rate[line] = max(i->production_rate[line] / 2, 0);
    00722             } else {
    00723               /* a zero production industry is unlikely to give anything but zero, so push it a little bit */
    00724               int new_prod = i->production_rate[line] == 0 ? 1 : i->production_rate[line] * 2;
    00725               if (IsProductionMaximum(i, line)) return;
    00726               i->production_rate[line] = minu(new_prod, 255);
    00727             }
    00728
    00729             UpdateIndustryProduction(i);
    00730             this->SetDirty();
    00731             this->flags4 |= WF_TIMEOUT_BEGIN;
    00732             this->clicked_line = line + 1;
    00733             this->clicked_button = (x < left + 10 ? 1 : 2);
    00734           } else if (IsInsideMM(x, left + 30, right)) {
    00735             /* clicked the text */
    00736             this->editbox_line = line;
    00737             SetDParam(0, i->production_rate[line] * 8);
    00738             ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION, 10, 100, this, CS_ALPHANUMERAL, QSF_NONE);
    00739           }
    00740         }
    00741       } break;
    00742
    00743       case IVW_GOTO:
    00744         i = Industry::Get(this->window_number);
    00745         if (_ctrl_pressed) {
    00746           ShowExtraViewPortWindow(i->location.tile + TileDiffXY(1, 1));
    00747         } else {
    00748           ScrollMainWindowToTile(i->location.tile + TileDiffXY(1, 1));
    00749         }
    00750         break;
    00751     }
    00752   }
    00753
    00754   virtual void OnTimeout()
    00755   {
    00756     this->clicked_line = 0;
    00757     this->clicked_button = 0;
    00758     this->SetDirty();
    00759   }
    00760
    00761   virtual void OnResize()
    00762   {
    00763     if (this->viewport != NULL) {
    00764       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
    00765       nvp->UpdateViewportCoordinates(this);
    00766     }
    00767   }
    00768
    00769   virtual void OnQueryTextFinished(char *str)
    00770   {
    00771     if (StrEmpty(str)) return;
    00772
    00773     Industry *i = Industry::Get(this->window_number);
    00774     int line = this->editbox_line;
    00775
    00776     i->production_rate[line] = ClampU(atoi(str) / 8, 0, 255);
    00777     UpdateIndustryProduction(i);
    00778     this->SetDirty();
    00779   }
    00780 };
    00781
    00782 static void UpdateIndustryProduction(Industry *i)
    00783 {
    00784   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00785     if (i->produced_cargo[j] != CT_INVALID) {
    00786       i->last_month_production[j] = 8 * i->production_rate[j];
    00787     }
    00788   }
    00789 }
    00790
    00792 static const NWidgetPart _nested_industry_view_widgets[] = {
    00793   NWidget(NWID_HORIZONTAL),
    00794     NWidget(WWT_CLOSEBOX, COLOUR_CREAM),
    00795     NWidget(WWT_CAPTION, COLOUR_CREAM, IVW_CAPTION), SetDataTip(STR_INDUSTRY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00796     NWidget(WWT_SHADEBOX, COLOUR_CREAM),
    00797     NWidget(WWT_STICKYBOX, COLOUR_CREAM),
    00798   EndContainer(),
    00799   NWidget(WWT_PANEL, COLOUR_CREAM),
    00800     NWidget(WWT_INSET, COLOUR_CREAM), SetPadding(2, 2, 2, 2),
    00801       NWidget(NWID_VIEWPORT, INVALID_COLOUR, IVW_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetPadding(1, 1, 1, 1), SetResize(1, 1),
    00802     EndContainer(),
    00803   EndContainer(),
    00804   NWidget(WWT_PANEL, COLOUR_CREAM, IVW_INFO), SetMinimalSize(260, 2), SetResize(1, 0),
    00805   EndContainer(),
    00806   NWidget(NWID_HORIZONTAL),
    00807     NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, IVW_GOTO), SetMinimalSize(130, 12), SetDataTip(STR_BUTTON_LOCATION, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP),
    00808     NWidget(WWT_PANEL, COLOUR_CREAM), SetResize(1, 0), EndContainer(),
    00809     NWidget(WWT_RESIZEBOX, COLOUR_CREAM),
    00810   EndContainer(),
    00811 };
    00812
    00814 static const WindowDesc _industry_view_desc(
    00815   WDP_AUTO, 260, 120,
    00816   WC_INDUSTRY_VIEW, WC_NONE,
    00817   WDF_UNCLICK_BUTTONS,
    00818   _nested_industry_view_widgets, lengthof(_nested_industry_view_widgets)
    00819 );
    00820
    00821 void ShowIndustryViewWindow(int industry)
    00822 {
    00823   AllocateWindowDescFront<IndustryViewWindow>(&_industry_view_desc, industry);
    00824 }
    00825
    00827 enum IndustryDirectoryWidgets {
    00828   IDW_DROPDOWN_ORDER,
    00829   IDW_DROPDOWN_CRITERIA,
    00830   IDW_INDUSTRY_LIST,
    00831   IDW_SCROLLBAR,
    00832 };
    00833
    00835 static const NWidgetPart _nested_industry_directory_widgets[] = {
    00836   NWidget(NWID_HORIZONTAL),
    00837     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    00838     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00839     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    00840     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    00841   EndContainer(),
    00842   NWidget(NWID_HORIZONTAL),
    00843     NWidget(NWID_VERTICAL),
    00844       NWidget(NWID_HORIZONTAL),
    00845         NWidget(WWT_TEXTBTN, COLOUR_BROWN, IDW_DROPDOWN_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    00846         NWidget(WWT_DROPDOWN, COLOUR_BROWN, IDW_DROPDOWN_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
    00847         NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
    00848       EndContainer(),
    00849       NWidget(WWT_PANEL, COLOUR_BROWN, IDW_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), EndContainer(),
    00850     EndContainer(),
    00851     NWidget(NWID_VERTICAL),
    00852       NWidget(WWT_SCROLLBAR, COLOUR_BROWN, IDW_SCROLLBAR),
    00853       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    00854     EndContainer(),
    00855   EndContainer(),
    00856 };
    00857
    00858 typedef GUIList<const Industry*> GUIIndustryList;
    00859
    00860
    00864 class IndustryDirectoryWindow : public Window {
    00865 protected:
    00866   /* Runtime saved values */
    00867   static Listing last_sorting;
    00868   static const Industry *last_industry;
    00869
    00870   /* Constants for sorting stations */
    00871   static const StringID sorter_names[];
    00872   static GUIIndustryList::SortFunction * const sorter_funcs[];
    00873
    00874   GUIIndustryList industries;
    00875
    00877   void BuildSortIndustriesList()
    00878   {
    00879     if (this->industries.NeedRebuild()) {
    00880       this->industries.Clear();
    00881
    00882       const Industry *i;
    00883       FOR_ALL_INDUSTRIES(i) {
    00884         *this->industries.Append() = i;
    00885       }
    00886
    00887       this->industries.Compact();
    00888       this->industries.RebuildDone();
    00889       this->vscroll.SetCount(this->industries.Length()); // Update scrollbar as well.
    00890     }
    00891
    00892     if (!this->industries.Sort()) return;
    00893     IndustryDirectoryWindow::last_industry = NULL; // Reset name sorter sort cache
    00894     this->SetWidgetDirty(IDW_INDUSTRY_LIST); // Set the modified widget dirty
    00895   }
    00896
    00904   static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id)
    00905   {
    00906     assert(id < lengthof(i->produced_cargo));
    00907
    00908     if (i->produced_cargo[id] == CT_INVALID) return 101;
    00909     return ToPercent8(i->last_month_pct_transported[id]);
    00910   }
    00911
    00919   static int GetCargoTransportedSortValue(const Industry *i)
    00920   {
    00921     int p1 = GetCargoTransportedPercentsIfValid(i, 0);
    00922     int p2 = GetCargoTransportedPercentsIfValid(i, 1);
    00923
    00924     if (p1 > p2) Swap(p1, p2); // lower value has higher priority
    00925
    00926     return (p1 << 8) + p2;
    00927   }
    00928
    00930   static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b)
    00931   {
    00932     static char buf_cache[96];
    00933     static char buf[96];
    00934
    00935     SetDParam(0, (*a)->town->index);
    00936     GetString(buf, STR_TOWN_NAME, lastof(buf));
    00937
    00938     if (*b != last_industry) {
    00939       last_industry = *b;
    00940       SetDParam(0, (*b)->town->index);
    00941       GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
    00942     }
    00943
    00944     return strcmp(buf, buf_cache);
    00945   }
    00946
    00948   static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b)
    00949   {
    00950     int r = (*a)->type - (*b)->type;
    00951     return (r == 0) ? IndustryNameSorter(a, b) : r;
    00952   }
    00953
    00955   static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b)
    00956   {
    00957     int r = 0;
    00958
    00959     if ((*a)->produced_cargo[0] == CT_INVALID) {
    00960       if ((*b)->produced_cargo[0] != CT_INVALID) return -1;
    00961     } else {
    00962       if ((*b)->produced_cargo[0] == CT_INVALID) return 1;
    00963
    00964       r = ((*a)->last_month_production[0] + (*a)->last_month_production[1]) -
    00965           ((*b)->last_month_production[0] + (*b)->last_month_production[1]);
    00966     }
    00967
    00968     return (r == 0) ? IndustryNameSorter(a, b) : r;
    00969   }
    00970
    00972   static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b)
    00973   {
    00974     int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b);
    00975     return (r == 0) ? IndustryNameSorter(a, b) : r;
    00976   }
    00977
    00983   StringID GetIndustryString(const Industry *i) const
    00984   {
    00985     const IndustrySpec *indsp = GetIndustrySpec(i->type);
    00986     byte p = 0;
    00987
    00988     /* Industry name */
    00989     SetDParam(p++, i->index);
    00990
    00991     static char cargo_suffix[lengthof(i->produced_cargo)][512];
    00992     GetAllCargoSuffixes(3, CST_DIR, i, i->type, indsp, i->produced_cargo, cargo_suffix);
    00993
    00994     /* Industry productions */
    00995     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00996       if (i->produced_cargo[j] == CT_INVALID) continue;
    00997       SetDParam(p++, i->produced_cargo[j]);
    00998       SetDParam(p++, i->last_month_production[j]);
    00999       SetDParamStr(p++, cargo_suffix[j]);
    01000     }
    01001
    01002     /* Transported productions */
    01003     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01004       if (i->produced_cargo[j] == CT_INVALID) continue;
    01005       SetDParam(p++, ToPercent8(i->last_month_pct_transported[j]));
    01006     }
    01007
    01008     /* Drawing the right string */
    01009     switch (p) {
    01010       case 1:  return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
    01011       case 5:  return STR_INDUSTRY_DIRECTORY_ITEM;
    01012       default: return STR_INDUSTRY_DIRECTORY_ITEM_TWO;
    01013     }
    01014   }
    01015
    01016 public:
    01017   IndustryDirectoryWindow(const WindowDesc *desc, WindowNumber number) : Window()
    01018   {
    01019     this->industries.SetListing(this->last_sorting);
    01020     this->industries.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs);
    01021     this->industries.ForceRebuild();
    01022     this->BuildSortIndustriesList();
    01023
    01024     this->InitNested(desc, 0);
    01025   }
    01026
    01027   ~IndustryDirectoryWindow()
    01028   {
    01029     this->last_sorting = this->industries.GetListing();
    01030   }
    01031
    01032   virtual void SetStringParameters(int widget) const
    01033   {
    01034     if (widget == IDW_DROPDOWN_CRITERIA) SetDParam(0, IndustryDirectoryWindow::sorter_names[this->industries.SortType()]);
    01035   }
    01036
    01037   virtual void OnPaint()
    01038   {
    01039     this->DrawWidgets();
    01040   }
    01041
    01042   virtual void DrawWidget(const Rect &r, int widget) const
    01043   {
    01044     switch (widget) {
    01045       case IDW_DROPDOWN_ORDER:
    01046         this->DrawSortButtonState(widget, this->industries.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    01047         break;
    01048
    01049       case IDW_INDUSTRY_LIST: {
    01050         int n = 0;
    01051         int y = r.top + WD_FRAMERECT_TOP;
    01052         if (this->industries.Length() == 0) {
    01053           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_DIRECTORY_NONE);
    01054           break;
    01055         }
    01056         for (uint i = this->vscroll.GetPosition(); i < this->industries.Length(); i++) {
    01057           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, this->GetIndustryString(this->industries[i]));
    01058
    01059           y += this->resize.step_height;
    01060           if (++n == this->vscroll.GetCapacity()) break; // max number of industries in 1 window
    01061         }
    01062       } break;
    01063     }
    01064   }
    01065
    01066   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01067   {
    01068     switch (widget) {
    01069       case IDW_DROPDOWN_ORDER: {
    01070         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    01071         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the word is centered, also looks nice.
    01072         d.height += padding.height;
    01073         *size = maxdim(*size, d);
    01074         break;
    01075       }
    01076
    01077       case IDW_DROPDOWN_CRITERIA: {
    01078         Dimension d = {0, 0};
    01079         for (uint i = 0; IndustryDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
    01080           d = maxdim(d, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names[i]));
    01081         }
    01082         d.width += padding.width;
    01083         d.height += padding.height;
    01084         *size = maxdim(*size, d);
    01085         break;
    01086       }
    01087
    01088       case IDW_INDUSTRY_LIST: {
    01089         Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE);
    01090         for (uint i = 0; i < this->industries.Length(); i++) {
    01091           d = maxdim(d, GetStringBoundingBox(this->GetIndustryString(this->industries[i])));
    01092         }
    01093         resize->height = d.height;
    01094         d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    01095         d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01096         *size = maxdim(*size, d);
    01097         break;
    01098       }
    01099     }
    01100   }
    01101
    01102
    01103   virtual void OnClick(Point pt, int widget, int click_count)
    01104   {
    01105     switch (widget) {
    01106       case IDW_DROPDOWN_ORDER:
    01107         this->industries.ToggleSortOrder();
    01108         this->SetDirty();
    01109         break;
    01110
    01111       case IDW_DROPDOWN_CRITERIA:
    01112         ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names, this->industries.SortType(), IDW_DROPDOWN_CRITERIA, 0, 0);
    01113         break;
    01114
    01115       case IDW_INDUSTRY_LIST: {
    01116         int y = (pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height;
    01117         uint16 p;
    01118
    01119         if (!IsInsideMM(y, 0, this->vscroll.GetCapacity())) return;
    01120         p = y + this->vscroll.GetPosition();
    01121         if (p < this->industries.Length()) {
    01122           if (_ctrl_pressed) {
    01123             ShowExtraViewPortWindow(this->industries[p]->location.tile);
    01124           } else {
    01125             ScrollMainWindowToTile(this->industries[p]->location.tile);
    01126           }
    01127         }
    01128       } break;
    01129     }
    01130   }
    01131
    01132   virtual void OnDropdownSelect(int widget, int index)
    01133   {
    01134     if (this->industries.SortType() != index) {
    01135       this->industries.SetSortType(index);
    01136       this->BuildSortIndustriesList();
    01137     }
    01138   }
    01139
    01140   virtual void OnResize()
    01141   {
    01142     this->vscroll.SetCapacityFromWidget(this, IDW_INDUSTRY_LIST);
    01143   }
    01144
    01145   virtual void OnHundredthTick()
    01146   {
    01147     this->industries.ForceResort();
    01148     this->BuildSortIndustriesList();
    01149   }
    01150
    01151   virtual void OnInvalidateData(int data)
    01152   {
    01153     if (data == 0) {
    01154       this->industries.ForceRebuild();
    01155     } else {
    01156       this->industries.ForceResort();
    01157     }
    01158     this->BuildSortIndustriesList();
    01159   }
    01160 };
    01161
    01162 Listing IndustryDirectoryWindow::last_sorting = {false, 0};
    01163 const Industry *IndustryDirectoryWindow::last_industry = NULL;
    01164
    01165 /* Availible station sorting functions */
    01166 GUIIndustryList::SortFunction * const IndustryDirectoryWindow::sorter_funcs[] = {
    01167   &IndustryNameSorter,
    01168   &IndustryTypeSorter,
    01169   &IndustryProductionSorter,
    01170   &IndustryTransportedCargoSorter
    01171 };
    01172
    01173 /* Names of the sorting functions */
    01174 const StringID IndustryDirectoryWindow::sorter_names[] = {
    01175   STR_SORT_BY_NAME,
    01176   STR_SORT_BY_TYPE,
    01177   STR_SORT_BY_PRODUCTION,
    01178   STR_SORT_BY_TRANSPORTED,
    01179   INVALID_STRING_ID
    01180 };
    01181
    01182
    01184 static const WindowDesc _industry_directory_desc(
    01185   WDP_AUTO, 428, 190,
    01186   WC_INDUSTRY_DIRECTORY, WC_NONE,
    01187   WDF_UNCLICK_BUTTONS,
    01188   _nested_industry_directory_widgets, lengthof(_nested_industry_directory_widgets)
    01189 );
    01190
    01191 void ShowIndustryDirectory()
    01192 {
    01193   AllocateWindowDescFront<IndustryDirectoryWindow>(&_industry_directory_desc, 0);
    01194 }