# industry_gui.cpp

Go to the documentation of this file.

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