# industry_gui.cpp

Go to the documentation of this file.

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