# 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 "gui.h"
    00014 #include "sound_func.h"
    00015 #include "window_func.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 "cheat_type.h"
    00023 #include "newgrf_industries.h"
    00024 #include "newgrf_text.h"
    00025 #include "newgrf_debug.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 #include "core/backup_type.hpp"
    00036 #include "genworld.h"
    00037 #include "smallmap_gui.h"
    00038
    00039 #include "table/strings.h"
    00040 #include "table/sprites.h"
    00041
    00042 bool _ignore_restrictions;
    00043 uint64 _displayed_industries;
    00044
    00045 assert_compile(NUM_INDUSTRYTYPES <= 64); // Make sure all industry types fit in _displayed_industries.
    00046
    00048 enum CargoSuffixType {
    00049   CST_FUND,
    00050   CST_VIEW,
    00051   CST_DIR,
    00052 };
    00053
    00054 static void ShowIndustryCargoesWindow(IndustryType id);
    00055
    00071 static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, char *suffix, const char *suffix_last)
    00072 {
    00073   suffix[0] = '\0';
    00074   if (HasBit(indspec->callback_mask, CBM_IND_CARGO_SUFFIX)) {
    00075     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);
    00076     if (GB(callback, 0, 8) != 0xFF) {
    00077       PrepareTextRefStackUsage(6);
    00078       GetString(suffix, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback), suffix_last);
    00079       StopTextRefStackUsage();
    00080     }
    00081   }
    00082 }
    00083
    00094 template <typename TC, typename TS>
    00095 static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, const TC &cargos, TS &suffixes)
    00096 {
    00097   assert_compile(lengthof(cargos) <= lengthof(suffixes));
    00098   for (uint j = 0; j < lengthof(cargos); j++) {
    00099     if (cargos[j] != CT_INVALID) {
    00100       GetCargoSuffix(cb_offset + j, cst, ind, ind_type, indspec, suffixes[j], lastof(suffixes[j]));
    00101     } else {
    00102       suffixes[j][0] = '\0';
    00103     }
    00104   }
    00105 }
    00106
    00107 IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES];
    00108
    00110 static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b)
    00111 {
    00112   static char industry_name[2][64];
    00113
    00114   const IndustrySpec *indsp1 = GetIndustrySpec(*a);
    00115   SetDParam(0, indsp1->name);
    00116   GetString(industry_name[0], STR_JUST_STRING, lastof(industry_name[0]));
    00117
    00118   const IndustrySpec *indsp2 = GetIndustrySpec(*b);
    00119   SetDParam(0, indsp2->name);
    00120   GetString(industry_name[1], STR_JUST_STRING, lastof(industry_name[1]));
    00121
    00122   int r = strnatcmp(industry_name[0], industry_name[1]); // Sort by name (natural sorting).
    00123
    00124   /* If the names are equal, sort by industry type. */
    00125   return (r != 0) ? r : (*a - *b);
    00126 }
    00127
    00131 void SortIndustryTypes()
    00132 {
    00133   /* Add each industry type to the list. */
    00134   for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
    00135     _sorted_industry_types[i] = i;
    00136   }
    00137
    00138   /* Sort industry types by name. */
    00139   QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter);
    00140 }
    00141
    00149 void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00150 {
    00151   if (result.Succeeded()) return;
    00152
    00153   uint8 indtype = GB(p1, 0, 8);
    00154   if (indtype < NUM_INDUSTRYTYPES) {
    00155     const IndustrySpec *indsp = GetIndustrySpec(indtype);
    00156     if (indsp->enabled) {
    00157       SetDParam(0, indsp->name);
    00158       ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, result.GetErrorMessage(), WL_INFO, TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE);
    00159     }
    00160   }
    00161 }
    00162
    00164 enum DynamicPlaceIndustriesWidgets {
    00165   DPIW_MATRIX_WIDGET,
    00166   DPIW_SCROLLBAR,
    00167   DPIW_INFOPANEL,
    00168   DPIW_DISPLAY_WIDGET,
    00169   DPIW_FUND_WIDGET,
    00170 };
    00171
    00172 static const NWidgetPart _nested_build_industry_widgets[] = {
    00173   NWidget(NWID_HORIZONTAL),
    00174     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00175     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FUND_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00176     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00177     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00178   EndContainer(),
    00179   NWidget(NWID_HORIZONTAL),
    00180     NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, DPIW_MATRIX_WIDGET), SetDataTip(0x801, STR_FUND_INDUSTRY_SELECTION_TOOLTIP), SetFill(1, 0), SetResize(1, 1), SetScrollbar(DPIW_SCROLLBAR),
    00181     NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, DPIW_SCROLLBAR),
    00182   EndContainer(),
    00183   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, DPIW_INFOPANEL), SetResize(1, 0),
    00184   EndContainer(),
    00185   NWidget(NWID_HORIZONTAL),
    00186     NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, DPIW_DISPLAY_WIDGET), SetFill(1, 0), SetResize(1, 0),
    00187         SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
    00188     NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, DPIW_FUND_WIDGET), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NULL),
    00189     NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    00190   EndContainer(),
    00191 };
    00192
    00194 static const WindowDesc _build_industry_desc(
    00195   WDP_AUTO, 170, 212,
    00196   WC_BUILD_INDUSTRY, WC_NONE,
    00197   WDF_CONSTRUCTION,
    00198   _nested_build_industry_widgets, lengthof(_nested_build_industry_widgets)
    00199 );
    00200
    00202 class BuildIndustryWindow : public Window {
    00203   int selected_index;
    00204   IndustryType selected_type;
    00205   uint16 callback_timer;
    00206   bool timer_enabled;
    00207   uint16 count;
    00208   IndustryType index[NUM_INDUSTRYTYPES + 1];
    00209   bool enabled[NUM_INDUSTRYTYPES + 1];
    00210   Scrollbar *vscroll;
    00211
    00213   static const int MATRIX_TEXT_OFFSET = 17;
    00214
    00215   void SetupArrays()
    00216   {
    00217     this->count = 0;
    00218
    00219     for (uint i = 0; i < lengthof(this->index); i++) {
    00220       this->index[i]   = INVALID_INDUSTRYTYPE;
    00221       this->enabled[i] = false;
    00222     }
    00223
    00224     if (_game_mode == GM_EDITOR) { // give room for the Many Random "button"
    00225       this->index[this->count] = INVALID_INDUSTRYTYPE;
    00226       this->enabled[this->count] = true;
    00227       this->count++;
    00228       this->timer_enabled = false;
    00229     }
    00230     /* Fill the arrays with industries.
    00231      * The tests performed after the enabled allow to load the industries
    00232      * In the same way they are inserted by grf (if any)
    00233      */
    00234     for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
    00235       IndustryType ind = _sorted_industry_types[i];
    00236       const IndustrySpec *indsp = GetIndustrySpec(ind);
    00237       if (indsp->enabled) {
    00238         /* Rule is that editor mode loads all industries.
    00239          * In game mode, all non raw industries are loaded too
    00240          * and raw ones are loaded only when setting allows it */
    00241         if (_game_mode != GM_EDITOR && indsp->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) {
    00242           /* Unselect if the industry is no longer in the list */
    00243           if (this->selected_type == ind) this->selected_index = -1;
    00244           continue;
    00245         }
    00246         this->index[this->count] = ind;
    00247         this->enabled[this->count] = (_game_mode == GM_EDITOR) || CheckIfCallBackAllowsAvailability(ind, IACT_USERCREATION);
    00248         /* Keep the selection to the correct line */
    00249         if (this->selected_type == ind) this->selected_index = this->count;
    00250         this->count++;
    00251       }
    00252     }
    00253
    00254     /* first indutry type is selected if the current selection is invalid.
    00255      * I'll be damned if there are none available ;) */
    00256     if (this->selected_index == -1) {
    00257       this->selected_index = 0;
    00258       this->selected_type = this->index[0];
    00259     }
    00260
    00261     this->vscroll->SetCount(this->count);
    00262   }
    00263
    00265   void SetButtons()
    00266   {
    00267     this->SetWidgetDisabledState(DPIW_FUND_WIDGET, this->selected_type != INVALID_INDUSTRYTYPE && !this->enabled[this->selected_index]);
    00268     this->SetWidgetDisabledState(DPIW_DISPLAY_WIDGET, this->selected_type == INVALID_INDUSTRYTYPE && this->enabled[this->selected_index]);
    00269   }
    00270
    00271 public:
    00272   BuildIndustryWindow() : Window()
    00273   {
    00274     this->timer_enabled = _loaded_newgrf_features.has_newindustries;
    00275
    00276     this->selected_index = -1;
    00277     this->selected_type = INVALID_INDUSTRYTYPE;
    00278
    00279     this->callback_timer = DAY_TICKS;
    00280
    00281     this->CreateNestedTree(&_build_industry_desc);
    00282     this->vscroll = this->GetScrollbar(DPIW_SCROLLBAR);
    00283     this->FinishInitNested(&_build_industry_desc, 0);
    00284
    00285     this->SetButtons();
    00286   }
    00287
    00288   virtual void OnInit()
    00289   {
    00290     this->SetupArrays();
    00291   }
    00292
    00293   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00294   {
    00295     switch (widget) {
    00296       case DPIW_MATRIX_WIDGET: {
    00297         Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES);
    00298         for (byte i = 0; i < this->count; i++) {
    00299           if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
    00300           d = maxdim(d, GetStringBoundingBox(GetIndustrySpec(this->index[i])->name));
    00301         }
    00302         resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00303         d.width += MATRIX_TEXT_OFFSET + padding.width;
    00304         d.height = 5 * resize->height;
    00305         *size = maxdim(*size, d);
    00306         break;
    00307       }
    00308
    00309       case DPIW_INFOPANEL: {
    00310         /* Extra line for cost outside of editor + extra lines for 'extra' information for NewGRFs. */
    00311         int height = 2 + (_game_mode == GM_EDITOR ? 0 : 1) + (_loaded_newgrf_features.has_newindustries ? 4 : 0);
    00312         Dimension d = {0, 0};
    00313         for (byte i = 0; i < this->count; i++) {
    00314           if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
    00315
    00316           const IndustrySpec *indsp = GetIndustrySpec(this->index[i]);
    00317
    00318           char cargo_suffix[3][512];
    00319           GetAllCargoSuffixes(0, CST_FUND, NULL, this->index[i], indsp, indsp->accepts_cargo, cargo_suffix);
    00320           StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00321           byte p = 0;
    00322           SetDParam(0, STR_JUST_NOTHING);
    00323           SetDParamStr(1, "");
    00324           for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
    00325             if (indsp->accepts_cargo[j] == CT_INVALID) continue;
    00326             if (p > 0) str++;
    00327             SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
    00328             SetDParamStr(p++, cargo_suffix[j]);
    00329           }
    00330           d = maxdim(d, GetStringBoundingBox(str));
    00331
    00332           /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
    00333           GetAllCargoSuffixes(3, CST_FUND, NULL, this->index[i], indsp, indsp->produced_cargo, cargo_suffix);
    00334           str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
    00335           p = 0;
    00336           SetDParam(0, STR_JUST_NOTHING);
    00337           SetDParamStr(1, "");
    00338           for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
    00339             if (indsp->produced_cargo[j] == CT_INVALID) continue;
    00340             if (p > 0) str++;
    00341             SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
    00342             SetDParamStr(p++, cargo_suffix[j]);
    00343           }
    00344           d = maxdim(d, GetStringBoundingBox(str));
    00345         }
    00346
    00347         /* Set it to something more sane :) */
    00348         size->height = height * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00349         size->width  = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00350         break;
    00351       }
    00352
    00353       case DPIW_FUND_WIDGET: {
    00354         Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
    00355         d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY));
    00356         d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY));
    00357         d.width += padding.width;
    00358         d.height += padding.height;
    00359         *size = maxdim(*size, d);
    00360         break;
    00361       }
    00362     }
    00363   }
    00364
    00365   virtual void SetStringParameters(int widget) const
    00366   {
    00367     switch (widget) {
    00368       case DPIW_FUND_WIDGET:
    00369         /* Raw industries might be prospected. Show this fact by changing the string
    00370          * In Editor, you just build, while ingame, or you fund or you prospect */
    00371         if (_game_mode == GM_EDITOR) {
    00372           /* We've chosen many random industries but no industries have been specified */
    00373           SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
    00374         } else {
    00375           const IndustrySpec *indsp = GetIndustrySpec(this->index[this->selected_index]);
    00376           SetDParam(0, (_settings_game.construction.raw_industry_construction == 2 && indsp->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY : STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY);
    00377         }
    00378         break;
    00379     }
    00380   }
    00381
    00382   virtual void DrawWidget(const Rect &r, int widget) const
    00383   {
    00384     switch (widget) {
    00385       case DPIW_MATRIX_WIDGET:
    00386         for (byte i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->count; i++) {
    00387           int x = r.left + WD_MATRIX_LEFT;
    00388           int y = r.top + WD_MATRIX_TOP + i * this->resize.step_height;
    00389           bool selected = this->selected_index == i + this->vscroll->GetPosition();
    00390
    00391           if (this->index[i + this->vscroll->GetPosition()] == INVALID_INDUSTRYTYPE) {
    00392             DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES, selected ? TC_WHITE : TC_ORANGE);
    00393             continue;
    00394           }
    00395           const IndustrySpec *indsp = GetIndustrySpec(this->index[i + this->vscroll->GetPosition()]);
    00396
    00397           /* Draw the name of the industry in white is selected, otherwise, in orange */
    00398           DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, indsp->name, selected ? TC_WHITE : TC_ORANGE);
    00399           GfxFillRect(x,     y + 1,  x + 10, y + 7, selected ? 15 : 0);
    00400           GfxFillRect(x + 1, y + 2,  x +  9, y + 6, indsp->map_colour);
    00401         }
    00402         break;
    00403
    00404       case DPIW_INFOPANEL: {
    00405         int y      = r.top    + WD_FRAMERECT_TOP;
    00406         int bottom = r.bottom - WD_FRAMERECT_BOTTOM;
    00407         int left   = r.left   + WD_FRAMERECT_LEFT;
    00408         int right  = r.right  - WD_FRAMERECT_RIGHT;
    00409
    00410         if (this->selected_type == INVALID_INDUSTRYTYPE) {
    00411           DrawStringMultiLine(left, right, y,  bottom, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP);
    00412           break;
    00413         }
    00414
    00415         const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00416
    00417         if (_game_mode != GM_EDITOR) {
    00418           SetDParam(0, indsp->GetConstructionCost());
    00419           DrawString(left, right, y, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST);
    00420           y += FONT_HEIGHT_NORMAL;
    00421         }
    00422
    00423         /* Draw the accepted cargoes, if any. Otherwise, will print "Nothing". */
    00424         char cargo_suffix[3][512];
    00425         GetAllCargoSuffixes(0, CST_FUND, NULL, this->selected_type, indsp, indsp->accepts_cargo, cargo_suffix);
    00426         StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00427         byte p = 0;
    00428         SetDParam(0, STR_JUST_NOTHING);
    00429         SetDParamStr(1, "");
    00430         for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
    00431           if (indsp->accepts_cargo[j] == CT_INVALID) continue;
    00432           if (p > 0) str++;
    00433           SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
    00434           SetDParamStr(p++, cargo_suffix[j]);
    00435         }
    00436         DrawString(left, right, y, str);
    00437         y += FONT_HEIGHT_NORMAL;
    00438
    00439         /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
    00440         GetAllCargoSuffixes(3, CST_FUND, NULL, this->selected_type, indsp, indsp->produced_cargo, cargo_suffix);
    00441         str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
    00442         p = 0;
    00443         SetDParam(0, STR_JUST_NOTHING);
    00444         SetDParamStr(1, "");
    00445         for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
    00446           if (indsp->produced_cargo[j] == CT_INVALID) continue;
    00447           if (p > 0) str++;
    00448           SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
    00449           SetDParamStr(p++, cargo_suffix[j]);
    00450         }
    00451         DrawString(left, right, y, str);
    00452         y += FONT_HEIGHT_NORMAL;
    00453
    00454         /* Get the additional purchase info text, if it has not already been queried. */
    00455         str = STR_NULL;
    00456         if (HasBit(indsp->callback_mask, CBM_IND_FUND_MORE_TEXT)) {
    00457           uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT, 0, 0, NULL, this->selected_type, INVALID_TILE);
    00458           if (callback_res != CALLBACK_FAILED) {  // Did it fail?
    00459             str = GetGRFStringID(indsp->grf_prop.grffile->grfid, 0xD000 + callback_res);  // No. here's the new string
    00460             if (str != STR_UNDEFINED) {
    00461               PrepareTextRefStackUsage(6);
    00462               DrawStringMultiLine(left, right, y, bottom, str);
    00463               StopTextRefStackUsage();
    00464             }
    00465           }
    00466         }
    00467         break;
    00468       }
    00469     }
    00470   }
    00471
    00472   virtual void OnClick(Point pt, int widget, int click_count)
    00473   {
    00474     switch (widget) {
    00475       case DPIW_MATRIX_WIDGET: {
    00476         int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, DPIW_MATRIX_WIDGET);
    00477         if (y < this->count) { // Is it within the boundaries of available data?
    00478           this->selected_index = y;
    00479           this->selected_type = this->index[y];
    00480           const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00481
    00482           this->SetDirty();
    00483
    00484           if (_thd.GetCallbackWnd() == this &&
    00485               ((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
    00486               this->selected_type == INVALID_INDUSTRYTYPE ||
    00487               !this->enabled[this->selected_index])) {
    00488             /* Reset the button state if going to prospecting or "build many industries" */
    00489             this->RaiseButtons();
    00490             ResetObjectToPlace();
    00491           }
    00492
    00493           this->SetButtons();
    00494           if (this->enabled[this->selected_index] && click_count > 1) this->OnClick(pt, DPIW_FUND_WIDGET, 1);
    00495         }
    00496         break;
    00497       }
    00498
    00499       case DPIW_DISPLAY_WIDGET:
    00500         if (this->selected_type != INVALID_INDUSTRYTYPE) ShowIndustryCargoesWindow(this->selected_type);
    00501         break;
    00502
    00503       case DPIW_FUND_WIDGET: {
    00504         if (this->selected_type == INVALID_INDUSTRYTYPE) {
    00505           this->HandleButtonClick(DPIW_FUND_WIDGET);
    00506
    00507           if (Town::GetNumItems() == 0) {
    00508             ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO);
    00509           } else {
    00510             extern void GenerateIndustries();
    00511             _generating_world = true;
    00512             GenerateIndustries();
    00513             _generating_world = false;
    00514           }
    00515         } else if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && GetIndustrySpec(this->selected_type)->IsRawIndustry()) {
    00516           DoCommandP(0, this->selected_type, InteractiveRandom(), CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00517           this->HandleButtonClick(DPIW_FUND_WIDGET);
    00518         } else {
    00519           HandlePlacePushButton(this, DPIW_FUND_WIDGET, SPR_CURSOR_INDUSTRY, HT_RECT);
    00520         }
    00521         break;
    00522       }
    00523     }
    00524   }
    00525
    00526   virtual void OnResize()
    00527   {
    00528     /* Adjust the number of items in the matrix depending of the resize */
    00529     this->vscroll->SetCapacityFromWidget(this, DPIW_MATRIX_WIDGET);
    00530     this->GetWidget<NWidgetCore>(DPIW_MATRIX_WIDGET)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00531   }
    00532
    00533   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00534   {
    00535     bool success = true;
    00536     /* We do not need to protect ourselves against "Random Many Industries" in this mode */
    00537     const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00538     uint32 seed = InteractiveRandom();
    00539
    00540     if (_game_mode == GM_EDITOR) {
    00541       /* Show error if no town exists at all */
    00542       if (Town::GetNumItems() == 0) {
    00543         SetDParam(0, indsp->name);
    00544         ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO, pt.x, pt.y);
    00545         return;
    00546       }
    00547
    00548       Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
    00549       _generating_world = true;
    00550       _ignore_restrictions = true;
    00551
    00552       DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed,
    00553           CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY), &CcBuildIndustry);
    00554
    00555       cur_company.Restore();
    00556       _ignore_restrictions = false;
    00557       _generating_world = false;
    00558     } else {
    00559       success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00560     }
    00561
    00562     /* If an industry has been built, just reset the cursor and the system */
    00563     if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00564   }
    00565
    00566   virtual void OnTick()
    00567   {
    00568     if (_pause_mode != PM_UNPAUSED) return;
    00569     if (!this->timer_enabled) return;
    00570     if (--this->callback_timer == 0) {
    00571       /* We have just passed another day.
    00572        * See if we need to update availability of currently selected industry */
    00573       this->callback_timer = DAY_TICKS; // restart counter
    00574
    00575       const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00576
    00577       if (indsp->enabled) {
    00578         bool call_back_result = CheckIfCallBackAllowsAvailability(this->selected_type, IACT_USERCREATION);
    00579
    00580         /* Only if result does match the previous state would it require a redraw. */
    00581         if (call_back_result != this->enabled[this->selected_index]) {
    00582           this->enabled[this->selected_index] = call_back_result;
    00583           this->SetButtons();
    00584           this->SetDirty();
    00585         }
    00586       }
    00587     }
    00588   }
    00589
    00590   virtual void OnTimeout()
    00591   {
    00592     this->RaiseButtons();
    00593   }
    00594
    00595   virtual void OnPlaceObjectAbort()
    00596   {
    00597     this->RaiseButtons();
    00598   }
    00599
    00605   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00606   {
    00607     if (!gui_scope) return;
    00608     this->SetupArrays();
    00609
    00610     const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00611     if (indsp == NULL) this->enabled[this->selected_index] = _settings_game.difficulty.industry_density != ID_FUND_ONLY;
    00612     this->SetButtons();
    00613   }
    00614 };
    00615
    00616 void ShowBuildIndustryWindow()
    00617 {
    00618   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
    00619   if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return;
    00620   new BuildIndustryWindow();
    00621 }
    00622
    00623 static void UpdateIndustryProduction(Industry *i);
    00624
    00625 static inline bool IsProductionAlterable(const Industry *i)
    00626 {
    00627   const IndustrySpec *is = GetIndustrySpec(i->type);
    00628   return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
    00629       (is->production_rate[0] != 0 || is->production_rate[1] != 0 || is->IsRawIndustry()));
    00630 }
    00631
    00633 enum IndustryViewWidgets {
    00634   IVW_CAPTION,
    00635   IVW_VIEWPORT,
    00636   IVW_INFO,
    00637   IVW_GOTO,
    00638   IVW_DISPLAY,
    00639 };
    00640
    00641 class IndustryViewWindow : public Window
    00642 {
    00644   enum Editability {
    00645     EA_NONE,
    00646     EA_MULTIPLIER,
    00647     EA_RATE,
    00648   };
    00649
    00651   enum InfoLine {
    00652     IL_NONE,
    00653     IL_MULTIPLIER,
    00654     IL_RATE1,
    00655     IL_RATE2,
    00656   };
    00657
    00658   Editability editable;
    00659   InfoLine editbox_line;
    00660   InfoLine clicked_line;
    00661   byte clicked_button;
    00662   int production_offset_y;
    00663   int info_height;
    00664
    00665 public:
    00666   IndustryViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00667   {
    00668     this->flags4 |= WF_DISABLE_VP_SCROLL;
    00669     this->editbox_line = IL_NONE;
    00670     this->clicked_line = IL_NONE;
    00671     this->clicked_button = 0;
    00672     this->info_height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + 1; // Info panel has at least two lines text.
    00673
    00674     this->InitNested(desc, window_number);
    00675     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
    00676     nvp->InitializeViewport(this, Industry::Get(window_number)->location.GetCenterTile(), ZOOM_LVL_INDUSTRY);
    00677
    00678     this->InvalidateData();
    00679   }
    00680
    00681   virtual void OnPaint()
    00682   {
    00683     this->DrawWidgets();
    00684
    00685     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
    00686
    00687     NWidgetBase *nwi = this->GetWidget<NWidgetBase>(IVW_INFO);
    00688     uint expected = this->DrawInfo(nwi->pos_x, nwi->pos_x + nwi->current_x - 1, nwi->pos_y) - nwi->pos_y;
    00689     if (expected > nwi->current_y - 1) {
    00690       this->info_height = expected + 1;
    00691       this->ReInit();
    00692       return;
    00693     }
    00694   }
    00695
    00703   int DrawInfo(uint left, uint right, uint top)
    00704   {
    00705     Industry *i = Industry::Get(this->window_number);
    00706     const IndustrySpec *ind = GetIndustrySpec(i->type);
    00707     int y = top + WD_FRAMERECT_TOP;
    00708     bool first = true;
    00709     bool has_accept = false;
    00710     char cargo_suffix[3][512];
    00711
    00712     if (HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) {
    00713       GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
    00714       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00715         if (i->accepts_cargo[j] == CT_INVALID) continue;
    00716         has_accept = true;
    00717         if (first) {
    00718           DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_FOR_PROCESSING);
    00719           y += FONT_HEIGHT_NORMAL;
    00720           first = false;
    00721         }
    00722         SetDParam(0, i->accepts_cargo[j]);
    00723         SetDParam(1, i->incoming_cargo_waiting[j]);
    00724         SetDParamStr(2, cargo_suffix[j]);
    00725         DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_STOCKPILE_CARGO);
    00726         y += FONT_HEIGHT_NORMAL;
    00727       }
    00728     } else {
    00729       GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
    00730       StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00731       byte p = 0;
    00732       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00733         if (i->accepts_cargo[j] == CT_INVALID) continue;
    00734         has_accept = true;
    00735         if (p > 0) str++;
    00736         SetDParam(p++, CargoSpec::Get(i->accepts_cargo[j])->name);
    00737         SetDParamStr(p++, cargo_suffix[j]);
    00738       }
    00739       if (has_accept) {
    00740         DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, str);
    00741         y += FONT_HEIGHT_NORMAL;
    00742       }
    00743     }
    00744
    00745     GetAllCargoSuffixes(3, CST_VIEW, i, i->type, ind, i->produced_cargo, cargo_suffix);
    00746     first = true;
    00747     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00748       if (i->produced_cargo[j] == CT_INVALID) continue;
    00749       if (first) {
    00750         if (has_accept) y += WD_PAR_VSEP_WIDE;
    00751         DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE);
    00752         y += FONT_HEIGHT_NORMAL;
    00753         if (this->editable == EA_RATE) this->production_offset_y = y;
    00754         first = false;
    00755       }
    00756
    00757       SetDParam(0, i->produced_cargo[j]);
    00758       SetDParam(1, i->last_month_production[j]);
    00759       SetDParamStr(2, cargo_suffix[j]);
    00760       SetDParam(3, ToPercent8(i->last_month_pct_transported[j]));
    00761       uint x = left + WD_FRAMETEXT_LEFT + (this->editable == EA_RATE ? 30 : 0);
    00762       DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_TRANSPORTED);
    00763       /* Let's put out those buttons.. */
    00764       if (this->editable == EA_RATE) {
    00765         DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + j) ? this->clicked_button : 0,
    00766             i->production_rate[j] > 0, i->production_rate[j] < 255);
    00767       }
    00768       y += FONT_HEIGHT_NORMAL;
    00769     }
    00770
    00771     /* Display production multiplier if editable */
    00772     if (this->editable == EA_MULTIPLIER) {
    00773       y += WD_PAR_VSEP_WIDE;
    00774       this->production_offset_y = y;
    00775       SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
    00776       uint x = left + WD_FRAMETEXT_LEFT + 30;
    00777       DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LEVEL);
    00778       DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0,
    00779           i->prod_level > PRODLEVEL_MINIMUM, i->prod_level < PRODLEVEL_MAXIMUM);
    00780       y += FONT_HEIGHT_NORMAL;
    00781     }
    00782
    00783     /* Get the extra message for the GUI */
    00784     if (HasBit(ind->callback_mask, CBM_IND_WINDOW_MORE_TEXT)) {
    00785       uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->location.tile);
    00786       if (callback_res != CALLBACK_FAILED) {
    00787         StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
    00788         if (message != STR_NULL && message != STR_UNDEFINED) {
    00789           y += WD_PAR_VSEP_WIDE;
    00790
    00791           PrepareTextRefStackUsage(6);
    00792           /* Use all the available space left from where we stand up to the
    00793            * end of the window. We ALSO enlarge the window if needed, so we
    00794            * can 'go' wild with the bottom of the window. */
    00795           y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, message);
    00796           StopTextRefStackUsage();
    00797         }
    00798       }
    00799     }
    00800     return y + WD_FRAMERECT_BOTTOM;
    00801   }
    00802
    00803   virtual void SetStringParameters(int widget) const
    00804   {
    00805     if (widget == IVW_CAPTION) SetDParam(0, this->window_number);
    00806   }
    00807
    00808   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00809   {
    00810     if (widget == IVW_INFO) size->height = this->info_height;
    00811   }
    00812
    00813   virtual void OnClick(Point pt, int widget, int click_count)
    00814   {
    00815     switch (widget) {
    00816       case IVW_INFO: {
    00817         Industry *i = Industry::Get(this->window_number);
    00818         InfoLine line = IL_NONE;
    00819
    00820         switch (this->editable) {
    00821           case EA_NONE: break;
    00822
    00823           case EA_MULTIPLIER:
    00824             if (IsInsideBS(pt.y, this->production_offset_y, FONT_HEIGHT_NORMAL)) line = IL_MULTIPLIER;
    00825             break;
    00826
    00827           case EA_RATE:
    00828             if (pt.y >= this->production_offset_y) {
    00829               int row = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
    00830               for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
    00831                 if (i->produced_cargo[j] == CT_INVALID) continue;
    00832                 row--;
    00833                 if (row < 0) {
    00834                   line = (InfoLine)(IL_RATE1 + j);
    00835                   break;
    00836                 }
    00837               }
    00838             }
    00839             break;
    00840         }
    00841         if (line == IL_NONE) return;
    00842
    00843         NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
    00844         int left = nwi->pos_x + WD_FRAMETEXT_LEFT;
    00845         int right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
    00846         if (IsInsideMM(pt.x, left, left + 20)) {
    00847           /* Clicked buttons, decrease or increase production */
    00848           byte button = (pt.x < left + 10) ? 1 : 2;
    00849           switch (this->editable) {
    00850             case EA_MULTIPLIER:
    00851               if (button == 1) {
    00852                 if (i->prod_level <= PRODLEVEL_MINIMUM) return;
    00853                 i->prod_level = max<uint>(i->prod_level / 2, PRODLEVEL_MINIMUM);
    00854               } else {
    00855                 if (i->prod_level >= PRODLEVEL_MAXIMUM) return;
    00856                 i->prod_level = minu(i->prod_level * 2, PRODLEVEL_MAXIMUM);
    00857               }
    00858               break;
    00859
    00860             case EA_RATE:
    00861               if (button == 1) {
    00862                 if (i->production_rate[line - IL_RATE1] <= 0) return;
    00863                 i->production_rate[line - IL_RATE1] = max(i->production_rate[line - IL_RATE1] / 2, 0);
    00864               } else {
    00865                 if (i->production_rate[line - IL_RATE1] >= 255) return;
    00866                 /* a zero production industry is unlikely to give anything but zero, so push it a little bit */
    00867                 int new_prod = i->production_rate[line - IL_RATE1] == 0 ? 1 : i->production_rate[line - IL_RATE1] * 2;
    00868                 i->production_rate[line - IL_RATE1] = minu(new_prod, 255);
    00869               }
    00870               break;
    00871
    00872             default: NOT_REACHED();
    00873           }
    00874
    00875           UpdateIndustryProduction(i);
    00876           this->SetDirty();
    00877           this->flags4 |= WF_TIMEOUT_BEGIN;
    00878           this->clicked_line = line;
    00879           this->clicked_button = button;
    00880         } else if (IsInsideMM(pt.x, left + 30, right)) {
    00881           /* clicked the text */
    00882           this->editbox_line = line;
    00883           switch (this->editable) {
    00884             case EA_MULTIPLIER:
    00885               SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
    00886               ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION_LEVEL, 10, 100, this, CS_ALPHANUMERAL, QSF_NONE);
    00887               break;
    00888
    00889             case EA_RATE:
    00890               SetDParam(0, i->production_rate[line - IL_RATE1] * 8);
    00891               ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION, 10, 100, this, CS_ALPHANUMERAL, QSF_NONE);
    00892               break;
    00893
    00894             default: NOT_REACHED();
    00895           }
    00896         }
    00897         break;
    00898       }
    00899
    00900       case IVW_GOTO: {
    00901         Industry *i = Industry::Get(this->window_number);
    00902         if (_ctrl_pressed) {
    00903           ShowExtraViewPortWindow(i->location.GetCenterTile());
    00904         } else {
    00905           ScrollMainWindowToTile(i->location.GetCenterTile());
    00906         }
    00907         break;
    00908       }
    00909
    00910       case IVW_DISPLAY: {
    00911         Industry *i = Industry::Get(this->window_number);
    00912         ShowIndustryCargoesWindow(i->type);
    00913         break;
    00914       }
    00915     }
    00916   }
    00917
    00918   virtual void OnTimeout()
    00919   {
    00920     this->clicked_line = IL_NONE;
    00921     this->clicked_button = 0;
    00922     this->SetDirty();
    00923   }
    00924
    00925   virtual void OnResize()
    00926   {
    00927     if (this->viewport != NULL) {
    00928       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
    00929       nvp->UpdateViewportCoordinates(this);
    00930
    00931       ScrollWindowToTile(Industry::Get(this->window_number)->location.GetCenterTile(), this, true); // Re-center viewport.
    00932     }
    00933   }
    00934
    00935   virtual void OnQueryTextFinished(char *str)
    00936   {
    00937     if (StrEmpty(str)) return;
    00938
    00939     Industry *i = Industry::Get(this->window_number);
    00940     uint value = atoi(str);
    00941     switch (this->editbox_line) {
    00942       case IL_NONE: NOT_REACHED();
    00943
    00944       case IL_MULTIPLIER:
    00945         i->prod_level = ClampU(RoundDivSU(value * PRODLEVEL_DEFAULT, 100), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
    00946         break;
    00947
    00948       default:
    00949         i->production_rate[this->editbox_line - IL_RATE1] = ClampU(RoundDivSU(value, 8), 0, 255);
    00950         break;
    00951     }
    00952     UpdateIndustryProduction(i);
    00953     this->SetDirty();
    00954   }
    00955
    00961   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00962   {
    00963     if (!gui_scope) return;
    00964     const Industry *i = Industry::Get(this->window_number);
    00965     if (IsProductionAlterable(i)) {
    00966       const IndustrySpec *ind = GetIndustrySpec(i->type);
    00967       this->editable = ind->UsesSmoothEconomy() ? EA_RATE : EA_MULTIPLIER;
    00968     } else {
    00969       this->editable = EA_NONE;
    00970     }
    00971   }
    00972
    00973   virtual bool IsNewGRFInspectable() const
    00974   {
    00975     return ::IsNewGRFInspectable(GSF_INDUSTRIES, this->window_number);
    00976   }
    00977
    00978   virtual void ShowNewGRFInspectWindow() const
    00979   {
    00980 		::ShowNewGRFInspectWindow(GSF_INDUSTRIES, this->window_number);
    00981   }
    00982 };
    00983
    00984 static void UpdateIndustryProduction(Industry *i)
    00985 {
    00986   const IndustrySpec *indspec = GetIndustrySpec(i->type);
    00987   if (!indspec->UsesSmoothEconomy()) i->RecomputeProductionMultipliers();
    00988
    00989   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00990     if (i->produced_cargo[j] != CT_INVALID) {
    00991       i->last_month_production[j] = 8 * i->production_rate[j];
    00992     }
    00993   }
    00994 }
    00995
    00997 static const NWidgetPart _nested_industry_view_widgets[] = {
    00998   NWidget(NWID_HORIZONTAL),
    00999     NWidget(WWT_CLOSEBOX, COLOUR_CREAM),
    01000     NWidget(WWT_CAPTION, COLOUR_CREAM, IVW_CAPTION), SetDataTip(STR_INDUSTRY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01001     NWidget(WWT_DEBUGBOX, COLOUR_CREAM),
    01002     NWidget(WWT_SHADEBOX, COLOUR_CREAM),
    01003     NWidget(WWT_STICKYBOX, COLOUR_CREAM),
    01004   EndContainer(),
    01005   NWidget(WWT_PANEL, COLOUR_CREAM),
    01006     NWidget(WWT_INSET, COLOUR_CREAM), SetPadding(2, 2, 2, 2),
    01007       NWidget(NWID_VIEWPORT, INVALID_COLOUR, IVW_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetPadding(1, 1, 1, 1), SetResize(1, 1),
    01008     EndContainer(),
    01009   EndContainer(),
    01010   NWidget(WWT_PANEL, COLOUR_CREAM, IVW_INFO), SetMinimalSize(260, 2), SetResize(1, 0),
    01011   EndContainer(),
    01012   NWidget(NWID_HORIZONTAL),
    01013     NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, IVW_GOTO), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP),
    01014     NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, IVW_DISPLAY), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
    01015     NWidget(WWT_RESIZEBOX, COLOUR_CREAM),
    01016   EndContainer(),
    01017 };
    01018
    01020 static const WindowDesc _industry_view_desc(
    01021   WDP_AUTO, 260, 120,
    01022   WC_INDUSTRY_VIEW, WC_NONE,
    01023   WDF_UNCLICK_BUTTONS,
    01024   _nested_industry_view_widgets, lengthof(_nested_industry_view_widgets)
    01025 );
    01026
    01027 void ShowIndustryViewWindow(int industry)
    01028 {
    01029   AllocateWindowDescFront<IndustryViewWindow>(&_industry_view_desc, industry);
    01030 }
    01031
    01033 enum IndustryDirectoryWidgets {
    01034   IDW_DROPDOWN_ORDER,
    01035   IDW_DROPDOWN_CRITERIA,
    01036   IDW_INDUSTRY_LIST,
    01037   IDW_SCROLLBAR,
    01038 };
    01039
    01041 static const NWidgetPart _nested_industry_directory_widgets[] = {
    01042   NWidget(NWID_HORIZONTAL),
    01043     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    01044     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01045     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    01046     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    01047   EndContainer(),
    01048   NWidget(NWID_HORIZONTAL),
    01049     NWidget(NWID_VERTICAL),
    01050       NWidget(NWID_HORIZONTAL),
    01051         NWidget(WWT_TEXTBTN, COLOUR_BROWN, IDW_DROPDOWN_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    01052         NWidget(WWT_DROPDOWN, COLOUR_BROWN, IDW_DROPDOWN_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
    01053         NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
    01054       EndContainer(),
    01055       NWidget(WWT_PANEL, COLOUR_BROWN, IDW_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), SetScrollbar(IDW_SCROLLBAR), EndContainer(),
    01056     EndContainer(),
    01057     NWidget(NWID_VERTICAL),
    01058       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, IDW_SCROLLBAR),
    01059       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    01060     EndContainer(),
    01061   EndContainer(),
    01062 };
    01063
    01064 typedef GUIList<const Industry*> GUIIndustryList;
    01065
    01066
    01070 class IndustryDirectoryWindow : public Window {
    01071 protected:
    01072   /* Runtime saved values */
    01073   static Listing last_sorting;
    01074   static const Industry *last_industry;
    01075
    01076   /* Constants for sorting stations */
    01077   static const StringID sorter_names[];
    01078   static GUIIndustryList::SortFunction * const sorter_funcs[];
    01079
    01080   GUIIndustryList industries;
    01081   Scrollbar *vscroll;
    01082
    01084   void BuildSortIndustriesList()
    01085   {
    01086     if (this->industries.NeedRebuild()) {
    01087       this->industries.Clear();
    01088
    01089       const Industry *i;
    01090       FOR_ALL_INDUSTRIES(i) {
    01091         *this->industries.Append() = i;
    01092       }
    01093
    01094       this->industries.Compact();
    01095       this->industries.RebuildDone();
    01096       this->vscroll->SetCount(this->industries.Length()); // Update scrollbar as well.
    01097     }
    01098
    01099     if (!this->industries.Sort()) return;
    01100     IndustryDirectoryWindow::last_industry = NULL; // Reset name sorter sort cache
    01101     this->SetWidgetDirty(IDW_INDUSTRY_LIST); // Set the modified widget dirty
    01102   }
    01103
    01111   static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id)
    01112   {
    01113     assert(id < lengthof(i->produced_cargo));
    01114
    01115     if (i->produced_cargo[id] == CT_INVALID) return 101;
    01116     return ToPercent8(i->last_month_pct_transported[id]);
    01117   }
    01118
    01126   static int GetCargoTransportedSortValue(const Industry *i)
    01127   {
    01128     int p1 = GetCargoTransportedPercentsIfValid(i, 0);
    01129     int p2 = GetCargoTransportedPercentsIfValid(i, 1);
    01130
    01131     if (p1 > p2) Swap(p1, p2); // lower value has higher priority
    01132
    01133     return (p1 << 8) + p2;
    01134   }
    01135
    01137   static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b)
    01138   {
    01139     static char buf_cache[96];
    01140     static char buf[96];
    01141
    01142     SetDParam(0, (*a)->index);
    01143     GetString(buf, STR_INDUSTRY_NAME, lastof(buf));
    01144
    01145     if (*b != last_industry) {
    01146       last_industry = *b;
    01147       SetDParam(0, (*b)->index);
    01148       GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache));
    01149     }
    01150
    01151     return strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
    01152   }
    01153
    01155   static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b)
    01156   {
    01157     int it_a = 0;
    01158     while (it_a != NUM_INDUSTRYTYPES && (*a)->type != _sorted_industry_types[it_a]) it_a++;
    01159     int it_b = 0;
    01160     while (it_b != NUM_INDUSTRYTYPES && (*b)->type != _sorted_industry_types[it_b]) it_b++;
    01161     int r = it_a - it_b;
    01162     return (r == 0) ? IndustryNameSorter(a, b) : r;
    01163   }
    01164
    01166   static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b)
    01167   {
    01168     uint prod_a = 0, prod_b = 0;
    01169     for (uint i = 0; i < lengthof((*a)->produced_cargo); i++) {
    01170       if ((*a)->produced_cargo[i] != CT_INVALID) prod_a += (*a)->last_month_production[i];
    01171       if ((*b)->produced_cargo[i] != CT_INVALID) prod_b += (*b)->last_month_production[i];
    01172     }
    01173     int r = prod_a - prod_b;
    01174
    01175     return (r == 0) ? IndustryTypeSorter(a, b) : r;
    01176   }
    01177
    01179   static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b)
    01180   {
    01181     int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b);
    01182     return (r == 0) ? IndustryNameSorter(a, b) : r;
    01183   }
    01184
    01190   StringID GetIndustryString(const Industry *i) const
    01191   {
    01192     const IndustrySpec *indsp = GetIndustrySpec(i->type);
    01193     byte p = 0;
    01194
    01195     /* Industry name */
    01196     SetDParam(p++, i->index);
    01197
    01198     static char cargo_suffix[lengthof(i->produced_cargo)][512];
    01199     GetAllCargoSuffixes(3, CST_DIR, i, i->type, indsp, i->produced_cargo, cargo_suffix);
    01200
    01201     /* Industry productions */
    01202     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01203       if (i->produced_cargo[j] == CT_INVALID) continue;
    01204       SetDParam(p++, i->produced_cargo[j]);
    01205       SetDParam(p++, i->last_month_production[j]);
    01206       SetDParamStr(p++, cargo_suffix[j]);
    01207     }
    01208
    01209     /* Transported productions */
    01210     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01211       if (i->produced_cargo[j] == CT_INVALID) continue;
    01212       SetDParam(p++, ToPercent8(i->last_month_pct_transported[j]));
    01213     }
    01214
    01215     /* Drawing the right string */
    01216     switch (p) {
    01217       case 1:  return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
    01218       case 5:  return STR_INDUSTRY_DIRECTORY_ITEM;
    01219       default: return STR_INDUSTRY_DIRECTORY_ITEM_TWO;
    01220     }
    01221   }
    01222
    01223 public:
    01224   IndustryDirectoryWindow(const WindowDesc *desc, WindowNumber number) : Window()
    01225   {
    01226     this->CreateNestedTree(desc);
    01227     this->vscroll = this->GetScrollbar(IDW_SCROLLBAR);
    01228
    01229     this->industries.SetListing(this->last_sorting);
    01230     this->industries.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs);
    01231     this->industries.ForceRebuild();
    01232     this->BuildSortIndustriesList();
    01233
    01234     this->FinishInitNested(desc, 0);
    01235   }
    01236
    01237   ~IndustryDirectoryWindow()
    01238   {
    01239     this->last_sorting = this->industries.GetListing();
    01240   }
    01241
    01242   virtual void SetStringParameters(int widget) const
    01243   {
    01244     if (widget == IDW_DROPDOWN_CRITERIA) SetDParam(0, IndustryDirectoryWindow::sorter_names[this->industries.SortType()]);
    01245   }
    01246
    01247   virtual void DrawWidget(const Rect &r, int widget) const
    01248   {
    01249     switch (widget) {
    01250       case IDW_DROPDOWN_ORDER:
    01251         this->DrawSortButtonState(widget, this->industries.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    01252         break;
    01253
    01254       case IDW_INDUSTRY_LIST: {
    01255         int n = 0;
    01256         int y = r.top + WD_FRAMERECT_TOP;
    01257         if (this->industries.Length() == 0) {
    01258           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_DIRECTORY_NONE);
    01259           break;
    01260         }
    01261         for (uint i = this->vscroll->GetPosition(); i < this->industries.Length(); i++) {
    01262           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, this->GetIndustryString(this->industries[i]));
    01263
    01264           y += this->resize.step_height;
    01265           if (++n == this->vscroll->GetCapacity()) break; // max number of industries in 1 window
    01266         }
    01267         break;
    01268       }
    01269     }
    01270   }
    01271
    01272   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01273   {
    01274     switch (widget) {
    01275       case IDW_DROPDOWN_ORDER: {
    01276         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    01277         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    01278         d.height += padding.height;
    01279         *size = maxdim(*size, d);
    01280         break;
    01281       }
    01282
    01283       case IDW_DROPDOWN_CRITERIA: {
    01284         Dimension d = {0, 0};
    01285         for (uint i = 0; IndustryDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
    01286           d = maxdim(d, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names[i]));
    01287         }
    01288         d.width += padding.width;
    01289         d.height += padding.height;
    01290         *size = maxdim(*size, d);
    01291         break;
    01292       }
    01293
    01294       case IDW_INDUSTRY_LIST: {
    01295         Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE);
    01296         for (uint i = 0; i < this->industries.Length(); i++) {
    01297           d = maxdim(d, GetStringBoundingBox(this->GetIndustryString(this->industries[i])));
    01298         }
    01299         resize->height = d.height;
    01300         d.height *= 5;
    01301         d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    01302         d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01303         *size = maxdim(*size, d);
    01304         break;
    01305       }
    01306     }
    01307   }
    01308
    01309
    01310   virtual void OnClick(Point pt, int widget, int click_count)
    01311   {
    01312     switch (widget) {
    01313       case IDW_DROPDOWN_ORDER:
    01314         this->industries.ToggleSortOrder();
    01315         this->SetDirty();
    01316         break;
    01317
    01318       case IDW_DROPDOWN_CRITERIA:
    01319         ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names, this->industries.SortType(), IDW_DROPDOWN_CRITERIA, 0, 0);
    01320         break;
    01321
    01322       case IDW_INDUSTRY_LIST: {
    01323         uint p = this->vscroll->GetScrolledRowFromWidget(pt.y, this, IDW_INDUSTRY_LIST, WD_FRAMERECT_TOP);
    01324         if (p < this->industries.Length()) {
    01325           if (_ctrl_pressed) {
    01326             ShowExtraViewPortWindow(this->industries[p]->location.tile);
    01327           } else {
    01328             ScrollMainWindowToTile(this->industries[p]->location.tile);
    01329           }
    01330         }
    01331         break;
    01332       }
    01333     }
    01334   }
    01335
    01336   virtual void OnDropdownSelect(int widget, int index)
    01337   {
    01338     if (this->industries.SortType() != index) {
    01339       this->industries.SetSortType(index);
    01340       this->BuildSortIndustriesList();
    01341     }
    01342   }
    01343
    01344   virtual void OnResize()
    01345   {
    01346     this->vscroll->SetCapacityFromWidget(this, IDW_INDUSTRY_LIST);
    01347   }
    01348
    01349   virtual void OnPaint()
    01350   {
    01351     if (this->industries.NeedRebuild()) this->BuildSortIndustriesList();
    01352     this->DrawWidgets();
    01353   }
    01354
    01355   virtual void OnHundredthTick()
    01356   {
    01357     this->industries.ForceResort();
    01358     this->BuildSortIndustriesList();
    01359   }
    01360
    01366   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01367   {
    01368     if (data == 0) {
    01369       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
    01370       this->industries.ForceRebuild();
    01371     } else {
    01372       this->industries.ForceResort();
    01373     }
    01374   }
    01375 };
    01376
    01377 Listing IndustryDirectoryWindow::last_sorting = {false, 0};
    01378 const Industry *IndustryDirectoryWindow::last_industry = NULL;
    01379
    01380 /* Available station sorting functions. */
    01381 GUIIndustryList::SortFunction * const IndustryDirectoryWindow::sorter_funcs[] = {
    01382   &IndustryNameSorter,
    01383   &IndustryTypeSorter,
    01384   &IndustryProductionSorter,
    01385   &IndustryTransportedCargoSorter
    01386 };
    01387
    01388 /* Names of the sorting functions */
    01389 const StringID IndustryDirectoryWindow::sorter_names[] = {
    01390   STR_SORT_BY_NAME,
    01391   STR_SORT_BY_TYPE,
    01392   STR_SORT_BY_PRODUCTION,
    01393   STR_SORT_BY_TRANSPORTED,
    01394   INVALID_STRING_ID
    01395 };
    01396
    01397
    01399 static const WindowDesc _industry_directory_desc(
    01400   WDP_AUTO, 428, 190,
    01401   WC_INDUSTRY_DIRECTORY, WC_NONE,
    01402   WDF_UNCLICK_BUTTONS,
    01403   _nested_industry_directory_widgets, lengthof(_nested_industry_directory_widgets)
    01404 );
    01405
    01406 void ShowIndustryDirectory()
    01407 {
    01408   AllocateWindowDescFront<IndustryDirectoryWindow>(&_industry_directory_desc, 0);
    01409 }
    01410
    01412 enum IndustryCargoesWidgets {
    01413   ICW_CAPTION,
    01414   ICW_NOTIFY,
    01415   ICW_PANEL,
    01416   ICW_SCROLLBAR,
    01417 };
    01418
    01420 static const NWidgetPart _nested_industry_cargoes_widgets[] = {
    01421   NWidget(NWID_HORIZONTAL),
    01422     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    01423     NWidget(WWT_CAPTION, COLOUR_BROWN, ICW_CAPTION), SetDataTip(STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01424     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    01425     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    01426   EndContainer(),
    01427   NWidget(NWID_HORIZONTAL),
    01428     NWidget(NWID_VERTICAL),
    01429       NWidget(WWT_PANEL, COLOUR_BROWN, ICW_PANEL), SetResize(1, 10), SetMinimalSize(200, 90), SetScrollbar(ICW_SCROLLBAR), EndContainer(),
    01430       NWidget(NWID_HORIZONTAL),
    01431         NWidget(WWT_TEXTBTN, COLOUR_BROWN, ICW_NOTIFY),
    01432           SetDataTip(STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP, STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP_TOOLTIP),
    01433         NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    01434       EndContainer(),
    01435     EndContainer(),
    01436     NWidget(NWID_VERTICAL),
    01437       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, ICW_SCROLLBAR),
    01438       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    01439     EndContainer(),
    01440   EndContainer(),
    01441 };
    01442
    01444 static const WindowDesc _industry_cargoes_desc(
    01445   WDP_AUTO, 300, 210,
    01446   WC_INDUSTRY_CARGOES, WC_NONE,
    01447   0,
    01448   _nested_industry_cargoes_widgets, lengthof(_nested_industry_cargoes_widgets)
    01449 );
    01450
    01452 enum CargoesFieldType {
    01453   CFT_EMPTY,
    01454   CFT_SMALL_EMPTY,
    01455   CFT_INDUSTRY,
    01456   CFT_CARGO,
    01457   CFT_CARGO_LABEL,
    01458   CFT_HEADER,
    01459 };
    01460
    01461 static const uint MAX_CARGOES = 3;
    01462
    01464 struct CargoesField {
    01465   static const int VERT_INTER_INDUSTRY_SPACE;
    01466   static const int HOR_CARGO_BORDER_SPACE;
    01467   static const int CARGO_STUB_WIDTH;
    01468   static const int HOR_CARGO_WIDTH, HOR_CARGO_SPACE;
    01469   static const int CARGO_FIELD_WIDTH;
    01470   static const int VERT_CARGO_SPACE, VERT_CARGO_EDGE;
    01471   static const int BLOB_DISTANCE, BLOB_WIDTH, BLOB_HEIGHT;
    01472
    01473   static const int INDUSTRY_LINE_COLOUR;
    01474   static const int CARGO_LINE_COLOUR;
    01475
    01476   static int small_height, normal_height;
    01477   static int industry_width;
    01478
    01479   CargoesFieldType type;
    01480   union {
    01481     struct {
    01482       IndustryType ind_type;
    01483       CargoID other_produced[MAX_CARGOES];
    01484       CargoID other_accepted[MAX_CARGOES];
    01485     } industry;
    01486     struct {
    01487       CargoID vertical_cargoes[MAX_CARGOES];
    01488       byte num_cargoes;
    01489       CargoID supp_cargoes[MAX_CARGOES];
    01490       byte top_end;
    01491       CargoID cust_cargoes[MAX_CARGOES];
    01492       byte bottom_end;
    01493     } cargo;
    01494     struct {
    01495       CargoID cargoes[MAX_CARGOES];
    01496       bool left_align;
    01497     } cargo_label;
    01498     StringID header;
    01499   } u; // Data for each type.
    01500
    01505   void MakeEmpty(CargoesFieldType type)
    01506   {
    01507     this->type = type;
    01508   }
    01509
    01515   void MakeIndustry(IndustryType ind_type)
    01516   {
    01517     this->type = CFT_INDUSTRY;
    01518     this->u.industry.ind_type = ind_type;
    01519     MemSetT(this->u.industry.other_accepted, INVALID_CARGO, MAX_CARGOES);
    01520     MemSetT(this->u.industry.other_produced, INVALID_CARGO, MAX_CARGOES);
    01521   }
    01522
    01529   int ConnectCargo(CargoID cargo, bool producer)
    01530   {
    01531     assert(this->type == CFT_CARGO);
    01532     if (cargo == INVALID_CARGO) return -1;
    01533
    01534     /* Find the vertical cargo column carrying the cargo. */
    01535     int column = -1;
    01536     for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
    01537       if (cargo == this->u.cargo.vertical_cargoes[i]) {
    01538         column = i;
    01539         break;
    01540       }
    01541     }
    01542     if (column < 0) return -1;
    01543
    01544     if (producer) {
    01545       assert(this->u.cargo.supp_cargoes[column] == INVALID_CARGO);
    01546       this->u.cargo.supp_cargoes[column]  = column;
    01547     } else {
    01548       assert(this->u.cargo.cust_cargoes[column] == INVALID_CARGO);
    01549       this->u.cargo.cust_cargoes[column] = column;
    01550     }
    01551     return column;
    01552   }
    01553
    01558   bool HasConnection()
    01559   {
    01560     assert(this->type == CFT_CARGO);
    01561
    01562     for (uint i = 0; i < MAX_CARGOES; i++) {
    01563       if (this->u.cargo.supp_cargoes[i] != INVALID_CARGO) return true;
    01564       if (this->u.cargo.cust_cargoes[i] != INVALID_CARGO) return true;
    01565     }
    01566     return false;
    01567   }
    01568
    01578   void MakeCargo(const CargoID *cargoes, uint length, int count = -1, bool top_end = false, bool bottom_end = false)
    01579   {
    01580     this->type = CFT_CARGO;
    01581     uint i;
    01582     uint num = 0;
    01583     for (i = 0; i < MAX_CARGOES && i < length; i++) {
    01584       if (cargoes[i] != INVALID_CARGO) {
    01585         this->u.cargo.vertical_cargoes[num] = cargoes[i];
    01586         num++;
    01587       }
    01588     }
    01589     this->u.cargo.num_cargoes = (count < 0) ? num : count;
    01590     for (; num < MAX_CARGOES; num++) this->u.cargo.vertical_cargoes[num] = INVALID_CARGO;
    01591     this->u.cargo.top_end = top_end;
    01592     this->u.cargo.bottom_end = bottom_end;
    01593     MemSetT(this->u.cargo.supp_cargoes, INVALID_CARGO, MAX_CARGOES);
    01594     MemSetT(this->u.cargo.cust_cargoes, INVALID_CARGO, MAX_CARGOES);
    01595   }
    01596
    01603   void MakeCargoLabel(const CargoID *cargoes, uint length, bool left_align)
    01604   {
    01605     this->type = CFT_CARGO_LABEL;
    01606     uint i;
    01607     for (i = 0; i < MAX_CARGOES && i < length; i++) this->u.cargo_label.cargoes[i] = cargoes[i];
    01608     for (; i < MAX_CARGOES; i++) this->u.cargo_label.cargoes[i] = INVALID_CARGO;
    01609     this->u.cargo_label.left_align = left_align;
    01610   }
    01611
    01616   void MakeHeader(StringID textid)
    01617   {
    01618     this->type = CFT_HEADER;
    01619     this->u.header = textid;
    01620   }
    01621
    01627   int GetCargoBase(int xpos) const
    01628   {
    01629     assert(this->type == CFT_CARGO);
    01630
    01631     switch (this->u.cargo.num_cargoes) {
    01632       case 0: return xpos + CARGO_FIELD_WIDTH / 2;
    01633       case 1: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH / 2;
    01634       case 2: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH - HOR_CARGO_SPACE / 2;
    01635       case 3: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH - HOR_CARGO_SPACE - HOR_CARGO_WIDTH / 2;
    01636       default: NOT_REACHED();
    01637     }
    01638   }
    01639
    01645   void Draw(int xpos, int ypos) const
    01646   {
    01647     switch (this->type) {
    01648       case CFT_EMPTY:
    01649       case CFT_SMALL_EMPTY:
    01650         break;
    01651
    01652       case CFT_HEADER:
    01653         ypos += (small_height - FONT_HEIGHT_NORMAL) / 2;
    01654         DrawString(xpos, xpos + industry_width, ypos, this->u.header, TC_WHITE, SA_HOR_CENTER);
    01655         break;
    01656
    01657       case CFT_INDUSTRY: {
    01658         int ypos1 = ypos + VERT_INTER_INDUSTRY_SPACE / 2;
    01659         int ypos2 = ypos + normal_height - 1 - VERT_INTER_INDUSTRY_SPACE / 2;
    01660         int xpos2 = xpos + industry_width - 1;
    01661         GfxDrawLine(xpos,  ypos1, xpos2, ypos1, INDUSTRY_LINE_COLOUR);
    01662         GfxDrawLine(xpos,  ypos1, xpos,  ypos2, INDUSTRY_LINE_COLOUR);
    01663         GfxDrawLine(xpos,  ypos2, xpos2, ypos2, INDUSTRY_LINE_COLOUR);
    01664         GfxDrawLine(xpos2, ypos1, xpos2, ypos2, INDUSTRY_LINE_COLOUR);
    01665         ypos += (normal_height - FONT_HEIGHT_NORMAL) / 2;
    01666         if (this->u.industry.ind_type < NUM_INDUSTRYTYPES) {
    01667           const IndustrySpec *indsp = GetIndustrySpec(this->u.industry.ind_type);
    01668           SetDParam(0, indsp->name);
    01669           DrawString(xpos, xpos2, ypos, STR_JUST_STRING, TC_WHITE, SA_HOR_CENTER);
    01670
    01671           /* Draw the industry legend. */
    01672           int blob_left, blob_right;
    01673           if (_current_text_dir == TD_RTL) {
    01674             blob_right = xpos2 - BLOB_DISTANCE;
    01675             blob_left  = blob_right - BLOB_WIDTH;
    01676           } else {
    01677             blob_left  = xpos + BLOB_DISTANCE;
    01678             blob_right = blob_left + BLOB_WIDTH;
    01679           }
    01680           GfxFillRect(blob_left,     ypos2 - BLOB_DISTANCE - BLOB_HEIGHT,     blob_right,     ypos2 - BLOB_DISTANCE,     0); // Border
    01681           GfxFillRect(blob_left + 1, ypos2 - BLOB_DISTANCE - BLOB_HEIGHT + 1, blob_right - 1, ypos2 - BLOB_DISTANCE - 1, indsp->map_colour);
    01682         } else {
    01683           DrawString(xpos, xpos2, ypos, STR_INDUSTRY_CARGOES_HOUSES, TC_FROMSTRING, SA_HOR_CENTER);
    01684         }
    01685
    01686         /* Draw the other_produced/other_accepted cargoes. */
    01687         const CargoID *other_right, *other_left;
    01688         if (_current_text_dir == TD_RTL) {
    01689           other_right = this->u.industry.other_accepted;
    01690           other_left  = this->u.industry.other_produced;
    01691         } else {
    01692           other_right = this->u.industry.other_produced;
    01693           other_left  = this->u.industry.other_accepted;
    01694         }
    01695         ypos1 += VERT_CARGO_EDGE;
    01696         for (uint i = 0; i < MAX_CARGOES; i++) {
    01697           if (other_right[i] != INVALID_CARGO) {
    01698             const CargoSpec *csp = CargoSpec::Get(other_right[i]);
    01699             int xp = xpos + industry_width + CARGO_STUB_WIDTH;
    01700             DrawHorConnection(xpos + industry_width, xp - 1, ypos1, csp);
    01701             GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
    01702           }
    01703           if (other_left[i] != INVALID_CARGO) {
    01704             const CargoSpec *csp = CargoSpec::Get(other_left[i]);
    01705             int xp = xpos - CARGO_STUB_WIDTH;
    01706             DrawHorConnection(xp + 1, xpos - 1, ypos1, csp);
    01707             GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
    01708           }
    01709           ypos1 += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01710         }
    01711         break;
    01712       }
    01713
    01714       case CFT_CARGO: {
    01715         int cargo_base = this->GetCargoBase(xpos);
    01716         int top = ypos + (this->u.cargo.top_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0);
    01717         int bot = ypos - (this->u.cargo.bottom_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0) + normal_height - 1;
    01718         int colpos = cargo_base;
    01719         for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
    01720           if (this->u.cargo.top_end) GfxDrawLine(colpos, top - 1, colpos + HOR_CARGO_WIDTH - 1, top - 1, CARGO_LINE_COLOUR);
    01721           if (this->u.cargo.bottom_end) GfxDrawLine(colpos, bot + 1, colpos + HOR_CARGO_WIDTH - 1, bot + 1, CARGO_LINE_COLOUR);
    01722           GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
    01723           colpos++;
    01724           const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[i]);
    01725           GfxFillRect(colpos, top, colpos + HOR_CARGO_WIDTH - 2, bot, csp->legend_colour, FILLRECT_OPAQUE);
    01726           colpos += HOR_CARGO_WIDTH - 2;
    01727           GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
    01728           colpos += 1 + HOR_CARGO_SPACE;
    01729         }
    01730
    01731         const CargoID *hor_left, *hor_right;
    01732         if (_current_text_dir == TD_RTL) {
    01733           hor_left  = this->u.cargo.cust_cargoes;
    01734           hor_right = this->u.cargo.supp_cargoes;
    01735         } else {
    01736           hor_left  = this->u.cargo.supp_cargoes;
    01737           hor_right = this->u.cargo.cust_cargoes;
    01738         }
    01739         ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
    01740         for (uint i = 0; i < MAX_CARGOES; i++) {
    01741           if (hor_left[i] != INVALID_CARGO) {
    01742             int col = hor_left[i];
    01743             int dx = 0;
    01744             const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
    01745             for (; col > 0; col--) {
    01746               int lf = cargo_base + col * HOR_CARGO_WIDTH + (col - 1) * HOR_CARGO_SPACE;
    01747               DrawHorConnection(lf, lf + HOR_CARGO_SPACE - dx, ypos, csp);
    01748               dx = 1;
    01749             }
    01750             DrawHorConnection(xpos, cargo_base - dx, ypos, csp);
    01751           }
    01752           if (hor_right[i] != INVALID_CARGO) {
    01753             int col = hor_right[i];
    01754             int dx = 0;
    01755             const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
    01756             for (; col < this->u.cargo.num_cargoes - 1; col++) {
    01757               int lf = cargo_base + (col + 1) * HOR_CARGO_WIDTH + col * HOR_CARGO_SPACE;
    01758               DrawHorConnection(lf + dx - 1, lf + HOR_CARGO_SPACE - 1, ypos, csp);
    01759               dx = 1;
    01760             }
    01761             DrawHorConnection(cargo_base + col * HOR_CARGO_SPACE + (col + 1) * HOR_CARGO_WIDTH - 1 + dx, xpos + CARGO_FIELD_WIDTH - 1, ypos, csp);
    01762           }
    01763           ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01764         }
    01765         break;
    01766       }
    01767
    01768       case CFT_CARGO_LABEL:
    01769         ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
    01770         for (uint i = 0; i < MAX_CARGOES; i++) {
    01771           if (this->u.cargo_label.cargoes[i] != INVALID_CARGO) {
    01772             const CargoSpec *csp = CargoSpec::Get(this->u.cargo_label.cargoes[i]);
    01773             DrawString(xpos + WD_FRAMERECT_LEFT, xpos + industry_width - 1 - WD_FRAMERECT_RIGHT, ypos, csp->name, TC_WHITE,
    01774                 (this->u.cargo_label.left_align) ? SA_LEFT : SA_RIGHT);
    01775           }
    01776           ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01777         }
    01778         break;
    01779
    01780       default:
    01781         NOT_REACHED();
    01782     }
    01783   }
    01784
    01792   CargoID CargoClickedAt(const CargoesField *left, const CargoesField *right, Point pt) const
    01793   {
    01794     assert(this->type == CFT_CARGO);
    01795
    01796     /* Vertical matching. */
    01797     int cpos = this->GetCargoBase(0);
    01798     uint col;
    01799     for (col = 0; col < this->u.cargo.num_cargoes; col++) {
    01800       if (pt.x < cpos) break;
    01801       if (pt.x < cpos + CargoesField::HOR_CARGO_WIDTH) return this->u.cargo.vertical_cargoes[col];
    01802       cpos += CargoesField::HOR_CARGO_WIDTH + CargoesField::HOR_CARGO_SPACE;
    01803     }
    01804     /* col = 0 -> left of first col, 1 -> left of 2nd col, ... this->u.cargo.num_cargoes right of last-col. */
    01805
    01806     int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
    01807     uint row;
    01808     for (row = 0; row < MAX_CARGOES; row++) {
    01809       if (pt.y < vpos) return INVALID_CARGO;
    01810       if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
    01811       vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01812     }
    01813     if (row == MAX_CARGOES) return INVALID_CARGO;
    01814
    01815     /* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */
    01816     if (col == 0) {
    01817       if (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]];
    01818       if (left != NULL) {
    01819         if (left->type == CFT_INDUSTRY) return left->u.industry.other_produced[row];
    01820         if (left->type == CFT_CARGO_LABEL && !left->u.cargo_label.left_align) return left->u.cargo_label.cargoes[row];
    01821       }
    01822       return INVALID_CARGO;
    01823     }
    01824     if (col == this->u.cargo.num_cargoes) {
    01825       if (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]];
    01826       if (right != NULL) {
    01827         if (right->type == CFT_INDUSTRY) return right->u.industry.other_accepted[row];
    01828         if (right->type == CFT_CARGO_LABEL && right->u.cargo_label.left_align) return right->u.cargo_label.cargoes[row];
    01829       }
    01830       return INVALID_CARGO;
    01831     }
    01832     if (row >= col) {
    01833       /* Clicked somewhere in-between vertical cargo connection.
    01834        * Since the horizontal connection is made in the same order as the vertical list, the above condition
    01835        * ensures we are left-below the main diagonal, thus at the supplying side.
    01836        */
    01837       return (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]] : INVALID_CARGO;
    01838     } else {
    01839       /* Clicked at a customer connection. */
    01840       return (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]] : INVALID_CARGO;
    01841     }
    01842   }
    01843
    01849   CargoID CargoLabelClickedAt(Point pt) const
    01850   {
    01851     assert(this->type == CFT_CARGO_LABEL);
    01852
    01853     int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
    01854     uint row;
    01855     for (row = 0; row < MAX_CARGOES; row++) {
    01856       if (pt.y < vpos) return INVALID_CARGO;
    01857       if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
    01858       vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01859     }
    01860     if (row == MAX_CARGOES) return INVALID_CARGO;
    01861     return this->u.cargo_label.cargoes[row];
    01862   }
    01863
    01864 private:
    01872   static void DrawHorConnection(int left, int right, int top, const CargoSpec *csp)
    01873   {
    01874     GfxDrawLine(left, top, right, top, CARGO_LINE_COLOUR);
    01875     GfxFillRect(left, top + 1, right, top + FONT_HEIGHT_NORMAL - 2, csp->legend_colour, FILLRECT_OPAQUE);
    01876     GfxDrawLine(left, top + FONT_HEIGHT_NORMAL - 1, right, top + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
    01877   }
    01878 };
    01879
    01880 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, produced_cargo));
    01881 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, accepts_cargo));
    01882
    01883 int CargoesField::small_height;
    01884 int CargoesField::normal_height;
    01885 int CargoesField::industry_width;
    01886 const int CargoesField::VERT_INTER_INDUSTRY_SPACE = 6;
    01887
    01888 const int CargoesField::HOR_CARGO_BORDER_SPACE = 15;
    01889 const int CargoesField::CARGO_STUB_WIDTH       = 10;
    01890 const int CargoesField::HOR_CARGO_WIDTH        = 15;
    01891 const int CargoesField::HOR_CARGO_SPACE        =  5;
    01892 const int CargoesField::VERT_CARGO_EDGE        =  4;
    01893 const int CargoesField::VERT_CARGO_SPACE       =  4;
    01894
    01895 const int CargoesField::BLOB_DISTANCE =  5;
    01896 const int CargoesField::BLOB_WIDTH    = 12;
    01897 const int CargoesField::BLOB_HEIGHT   =  9;
    01898
    01900 const int CargoesField::CARGO_FIELD_WIDTH = HOR_CARGO_BORDER_SPACE * 2 + HOR_CARGO_WIDTH * MAX_CARGOES + HOR_CARGO_SPACE * (MAX_CARGOES - 1);
    01901
    01902 const int CargoesField::INDUSTRY_LINE_COLOUR = 191;
    01903 const int CargoesField::CARGO_LINE_COLOUR    = 191;
    01904
    01906 struct CargoesRow {
    01907   CargoesField columns[5];
    01908
    01913   void ConnectIndustryProduced(int column)
    01914   {
    01915     CargoesField *ind_fld   = this->columns + column;
    01916     CargoesField *cargo_fld = this->columns + column + 1;
    01917     assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
    01918
    01919     MemSetT(ind_fld->u.industry.other_produced, INVALID_CARGO, MAX_CARGOES);
    01920
    01921     if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
    01922       CargoID others[MAX_CARGOES]; // Produced cargoes not carried in the cargo column.
    01923       int other_count = 0;
    01924
    01925       const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
    01926       for (uint i = 0; i < lengthof(indsp->produced_cargo); i++) {
    01927         int col = cargo_fld->ConnectCargo(indsp->produced_cargo[i], true);
    01928         if (col < 0) others[other_count++] = indsp->produced_cargo[i];
    01929       }
    01930
    01931       /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
    01932       for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
    01933         if (cargo_fld->u.cargo.supp_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_produced[i] = others[--other_count];
    01934       }
    01935     } else {
    01936       /* Houses only display what is demanded. */
    01937       for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
    01938         CargoID cid = cargo_fld->u.cargo.vertical_cargoes[i];
    01939         if (cid == CT_PASSENGERS || cid == CT_MAIL) cargo_fld->ConnectCargo(cid, true);
    01940       }
    01941     }
    01942   }
    01943
    01949   void MakeCargoLabel(int column, bool accepting)
    01950   {
    01951     CargoID cargoes[MAX_CARGOES];
    01952     MemSetT(cargoes, INVALID_CARGO, lengthof(cargoes));
    01953
    01954     CargoesField *label_fld = this->columns + column;
    01955     CargoesField *cargo_fld = this->columns + (accepting ? column - 1 : column + 1);
    01956
    01957     assert(cargo_fld->type == CFT_CARGO && label_fld->type == CFT_EMPTY);
    01958     for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
    01959       int col = cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], !accepting);
    01960       cargoes[col] = cargo_fld->u.cargo.vertical_cargoes[i];
    01961     }
    01962     label_fld->MakeCargoLabel(cargoes, lengthof(cargoes), accepting);
    01963   }
    01964
    01965
    01970   void ConnectIndustryAccepted(int column)
    01971   {
    01972     CargoesField *ind_fld   = this->columns + column;
    01973     CargoesField *cargo_fld = this->columns + column - 1;
    01974     assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
    01975
    01976     MemSetT(ind_fld->u.industry.other_accepted, INVALID_CARGO, MAX_CARGOES);
    01977
    01978     if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
    01979       CargoID others[MAX_CARGOES]; // Accepted cargoes not carried in the cargo column.
    01980       int other_count = 0;
    01981
    01982       const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
    01983       for (uint i = 0; i < lengthof(indsp->accepts_cargo); i++) {
    01984         int col = cargo_fld->ConnectCargo(indsp->accepts_cargo[i], false);
    01985         if (col < 0) others[other_count++] = indsp->accepts_cargo[i];
    01986       }
    01987
    01988       /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
    01989       for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
    01990         if (cargo_fld->u.cargo.cust_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_accepted[i] = others[--other_count];
    01991       }
    01992     } else {
    01993       /* Houses only display what is demanded. */
    01994       for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
    01995         for (uint h = 0; h < HOUSE_MAX; h++) {
    01996           HouseSpec *hs = HouseSpec::Get(h);
    01997           if (!hs->enabled) continue;
    01998
    01999           for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
    02000             if (cargo_fld->u.cargo.vertical_cargoes[i] == hs->accepts_cargo[j]) {
    02001               cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], false);
    02002               goto next_cargo;
    02003             }
    02004           }
    02005         }
    02006 next_cargo: ;
    02007       }
    02008     }
    02009   }
    02010 };
    02011
    02012
    02040 struct IndustryCargoesWindow : public Window {
    02041   static const int HOR_TEXT_PADDING, VERT_TEXT_PADDING;
    02042
    02043   typedef SmallVector<CargoesRow, 4> Fields;
    02044
    02045   Fields fields;
    02046   uint ind_cargo;
    02047
    02048   Scrollbar *vscroll;
    02049
    02050   IndustryCargoesWindow(int id) : Window()
    02051   {
    02052     this->OnInit();
    02053     this->CreateNestedTree(&_industry_cargoes_desc);
    02054     this->vscroll = this->GetScrollbar(ICW_SCROLLBAR);
    02055     this->FinishInitNested(&_industry_cargoes_desc, 0);
    02056     this->OnInvalidateData(id);
    02057   }
    02058
    02059   virtual void OnInit()
    02060   {
    02061     /* Initialize static CargoesField size variables. */
    02062     Dimension d = GetStringBoundingBox(STR_INDUSTRY_CARGOES_PRODUCERS);
    02063     d = maxdim(d, GetStringBoundingBox(STR_INDUSTRY_CARGOES_CUSTOMERS));
    02064     d.width  += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
    02065     d.height += WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM;
    02066     CargoesField::small_height = d.height;
    02067
    02068     /* Decide about the size of the box holding the text of an industry type. */
    02069     d.height = 0;
    02070     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02071       const IndustrySpec *indsp = GetIndustrySpec(it);
    02072       if (!indsp->enabled) continue;
    02073       SetDParam(0, indsp->name);
    02074       d = maxdim(d, GetStringBoundingBox(STR_JUST_STRING));
    02075     }
    02076     /* Box must also be wide enough to hold any cargo label. */
    02077     for (uint i = 0; i < NUM_CARGO; i++) {
    02078       const CargoSpec *csp = CargoSpec::Get(i);
    02079       if (!csp->IsValid()) continue;
    02080       d = maxdim(d, GetStringBoundingBox(csp->name));
    02081     }
    02082     d.width  += 2 * HOR_TEXT_PADDING;
    02083     /* Ensure the height is enough for the industry type text, for the horizontal connections, and for the cargo labels. */
    02084     uint min_ind_height = CargoesField::VERT_CARGO_EDGE * 2 + MAX_CARGOES * FONT_HEIGHT_NORMAL + (MAX_CARGOES - 1) *  CargoesField::VERT_CARGO_SPACE;
    02085     d.height = max(d.height + 2 * VERT_TEXT_PADDING, min_ind_height);
    02086
    02087     CargoesField::industry_width = d.width;
    02088     CargoesField::normal_height = d.height + CargoesField::VERT_INTER_INDUSTRY_SPACE;
    02089   }
    02090
    02091   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    02092   {
    02093     if (widget != ICW_PANEL) return;
    02094
    02095     size->width = WD_FRAMETEXT_LEFT + CargoesField::industry_width * 3 + CargoesField::CARGO_FIELD_WIDTH * 2 + WD_FRAMETEXT_RIGHT;
    02096   }
    02097
    02098
    02099   CargoesFieldType type;
    02100   virtual void SetStringParameters  (int widget) const
    02101   {
    02102     if (widget != ICW_CAPTION) return;
    02103
    02104     if (this->ind_cargo < NUM_INDUSTRYTYPES) {
    02105       const IndustrySpec *indsp = GetIndustrySpec(this->ind_cargo);
    02106       SetDParam(0, indsp->name);
    02107     } else {
    02108       const CargoSpec *csp = CargoSpec::Get(this->ind_cargo - NUM_INDUSTRYTYPES);
    02109       SetDParam(0, csp->name);
    02110     }
    02111   }
    02112
    02121   static bool HasCommonValidCargo(const CargoID *cargoes1, uint length1, const CargoID *cargoes2, uint length2)
    02122   {
    02123     while (length1 > 0) {
    02124       if (*cargoes1 != INVALID_CARGO) {
    02125         for (uint i = 0; i < length2; i++) if (*cargoes1 == cargoes2[i]) return true;
    02126       }
    02127       cargoes1++;
    02128       length1--;
    02129     }
    02130     return false;
    02131   }
    02132
    02139   static bool HousesCanSupply(const CargoID *cargoes, uint length)
    02140   {
    02141     for (uint i = 0; i < length; i++) {
    02142       if (cargoes[i] == INVALID_CARGO) continue;
    02143       if (cargoes[i] == CT_PASSENGERS || cargoes[i] == CT_MAIL) return true;
    02144     }
    02145     return false;
    02146   }
    02147
    02154   static bool HousesCanAccept(const CargoID *cargoes, uint length)
    02155   {
    02156     HouseZones climate_mask;
    02157     switch (_settings_game.game_creation.landscape) {
    02158       case LT_TEMPERATE: climate_mask = HZ_TEMP; break;
    02159       case LT_ARCTIC:    climate_mask = HZ_SUBARTC_ABOVE | HZ_SUBARTC_BELOW; break;
    02160       case LT_TROPIC:    climate_mask = HZ_SUBTROPIC; break;
    02161       case LT_TOYLAND:   climate_mask = HZ_TOYLND; break;
    02162       default: NOT_REACHED();
    02163     }
    02164     for (uint i = 0; i < length; i++) {
    02165       if (cargoes[i] == INVALID_CARGO) continue;
    02166
    02167       for (uint h = 0; h < HOUSE_MAX; h++) {
    02168         HouseSpec *hs = HouseSpec::Get(h);
    02169         if (!hs->enabled || !(hs->building_availability & climate_mask)) continue;
    02170
    02171         for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
    02172           if (cargoes[i] == hs->accepts_cargo[j]) return true;
    02173         }
    02174       }
    02175     }
    02176     return false;
    02177   }
    02178
    02185   static int CountMatchingAcceptingIndustries(const CargoID *cargoes, uint length)
    02186   {
    02187     int count = 0;
    02188     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02189       const IndustrySpec *indsp = GetIndustrySpec(it);
    02190       if (!indsp->enabled) continue;
    02191
    02192       if (HasCommonValidCargo(cargoes, length, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) count++;
    02193     }
    02194     return count;
    02195   }
    02196
    02203   static int CountMatchingProducingIndustries(const CargoID *cargoes, uint length)
    02204   {
    02205     int count = 0;
    02206     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02207       const IndustrySpec *indsp = GetIndustrySpec(it);
    02208       if (!indsp->enabled) continue;
    02209
    02210       if (HasCommonValidCargo(cargoes, length, indsp->produced_cargo, lengthof(indsp->produced_cargo))) count++;
    02211     }
    02212     return count;
    02213   }
    02214
    02221   void ShortenCargoColumn(int column, int top, int bottom)
    02222   {
    02223     while (top < bottom && !this->fields[top].columns[column].HasConnection()) {
    02224       this->fields[top].columns[column].MakeEmpty(CFT_EMPTY);
    02225       top++;
    02226     }
    02227     this->fields[top].columns[column].u.cargo.top_end = true;
    02228
    02229     while (bottom > top && !this->fields[bottom].columns[column].HasConnection()) {
    02230       this->fields[bottom].columns[column].MakeEmpty(CFT_EMPTY);
    02231       bottom--;
    02232     }
    02233     this->fields[bottom].columns[column].u.cargo.bottom_end = true;
    02234   }
    02235
    02242   void PlaceIndustry(int row, int col, IndustryType it)
    02243   {
    02244     assert(this->fields[row].columns[col].type == CFT_EMPTY);
    02245     this->fields[row].columns[col].MakeIndustry(it);
    02246     if (col == 0) {
    02247       this->fields[row].ConnectIndustryProduced(col);
    02248     } else {
    02249       this->fields[row].ConnectIndustryAccepted(col);
    02250     }
    02251   }
    02252
    02256   void NotifySmallmap()
    02257   {
    02258     if (!this->IsWidgetLowered(ICW_NOTIFY)) return;
    02259
    02260     /* Only notify the smallmap window if it exists. In particular, do not
    02261      * bring it to the front to prevent messing up any nice layout of the user. */
    02262     InvalidateWindowClassesData(WC_SMALLMAP, 0);
    02263   }
    02264
    02269   void ComputeIndustryDisplay(IndustryType it)
    02270   {
    02271     this->GetWidget<NWidgetCore>(ICW_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION;
    02272     this->ind_cargo = it;
    02273     _displayed_industries = 1ULL << it;
    02274
    02275     this->fields.Clear();
    02276     CargoesRow *row = this->fields.Append();
    02277     row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
    02278     row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
    02279     row->columns[2].MakeEmpty(CFT_SMALL_EMPTY);
    02280     row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
    02281     row->columns[4].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
    02282
    02283     const IndustrySpec *central_sp = GetIndustrySpec(it);
    02284     bool houses_supply = HousesCanSupply(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
    02285     bool houses_accept = HousesCanAccept(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
    02286     /* Make a field consisting of two cargo columns. */
    02287     int num_supp = CountMatchingProducingIndustries(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo)) + houses_supply;
    02288     int num_cust = CountMatchingAcceptingIndustries(central_sp->produced_cargo, lengthof(central_sp->produced_cargo)) + houses_accept;
    02289     int num_indrows = max(3, max(num_supp, num_cust)); // One is needed for the 'it' industry, and 2 for the cargo labels.
    02290     for (int i = 0; i < num_indrows; i++) {
    02291       CargoesRow *row = this->fields.Append();
    02292       row->columns[0].MakeEmpty(CFT_EMPTY);
    02293       row->columns[1].MakeCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
    02294       row->columns[2].MakeEmpty(CFT_EMPTY);
    02295       row->columns[3].MakeCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
    02296       row->columns[4].MakeEmpty(CFT_EMPTY);
    02297     }
    02298     /* Add central industry. */
    02299     int central_row = 1 + num_indrows / 2;
    02300     this->fields[central_row].columns[2].MakeIndustry(it);
    02301     this->fields[central_row].ConnectIndustryProduced(2);
    02302     this->fields[central_row].ConnectIndustryAccepted(2);
    02303
    02304     /* Add cargo labels. */
    02305     this->fields[central_row - 1].MakeCargoLabel(2, true);
    02306     this->fields[central_row + 1].MakeCargoLabel(2, false);
    02307
    02308     /* Add suppliers and customers of the 'it' industry. */
    02309     int supp_count = 0;
    02310     int cust_count = 0;
    02311     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02312       const IndustrySpec *indsp = GetIndustrySpec(it);
    02313       if (!indsp->enabled) continue;
    02314
    02315       if (HasCommonValidCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo), indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
    02316         this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
    02317         SetBit(_displayed_industries, it);
    02318         supp_count++;
    02319       }
    02320       if (HasCommonValidCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo), indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
    02321         this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, it);
    02322         SetBit(_displayed_industries, it);
    02323         cust_count++;
    02324       }
    02325     }
    02326     if (houses_supply) {
    02327       this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
    02328       supp_count++;
    02329     }
    02330     if (houses_accept) {
    02331       this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, NUM_INDUSTRYTYPES);
    02332       cust_count++;
    02333     }
    02334
    02335     this->ShortenCargoColumn(1, 1, num_indrows);
    02336     this->ShortenCargoColumn(3, 1, num_indrows);
    02337     const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(ICW_PANEL);
    02338     this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
    02339     this->SetDirty();
    02340     this->NotifySmallmap();
    02341   }
    02342
    02347   void ComputeCargoDisplay(CargoID cid)
    02348   {
    02349     this->GetWidget<NWidgetCore>(ICW_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_CARGO_CAPTION;
    02350     this->ind_cargo = cid + NUM_INDUSTRYTYPES;
    02351     _displayed_industries = 0;
    02352
    02353     this->fields.Clear();
    02354     CargoesRow *row = this->fields.Append();
    02355     row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
    02356     row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
    02357     row->columns[2].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
    02358     row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
    02359     row->columns[4].MakeEmpty(CFT_SMALL_EMPTY);
    02360
    02361     bool houses_supply = HousesCanSupply(&cid, 1);
    02362     bool houses_accept = HousesCanAccept(&cid, 1);
    02363     int num_supp = CountMatchingProducingIndustries(&cid, 1) + houses_supply + 1; // Ensure room for the cargo label.
    02364     int num_cust = CountMatchingAcceptingIndustries(&cid, 1) + houses_accept;
    02365     int num_indrows = max(num_supp, num_cust);
    02366     for (int i = 0; i < num_indrows; i++) {
    02367       CargoesRow *row = this->fields.Append();
    02368       row->columns[0].MakeEmpty(CFT_EMPTY);
    02369       row->columns[1].MakeCargo(&cid, 1);
    02370       row->columns[2].MakeEmpty(CFT_EMPTY);
    02371       row->columns[3].MakeEmpty(CFT_EMPTY);
    02372       row->columns[4].MakeEmpty(CFT_EMPTY);
    02373     }
    02374
    02375     this->fields[num_indrows].MakeCargoLabel(0, false); // Add cargo labels at the left bottom.
    02376
    02377     /* Add suppliers and customers of the cargo. */
    02378     int supp_count = 0;
    02379     int cust_count = 0;
    02380     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02381       const IndustrySpec *indsp = GetIndustrySpec(it);
    02382       if (!indsp->enabled) continue;
    02383
    02384       if (HasCommonValidCargo(&cid, 1, indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
    02385         this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
    02386         SetBit(_displayed_industries, it);
    02387         supp_count++;
    02388       }
    02389       if (HasCommonValidCargo(&cid, 1, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
    02390         this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, it);
    02391         SetBit(_displayed_industries, it);
    02392         cust_count++;
    02393       }
    02394     }
    02395     if (houses_supply) {
    02396       this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
    02397       supp_count++;
    02398     }
    02399     if (houses_accept) {
    02400       this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, NUM_INDUSTRYTYPES);
    02401       cust_count++;
    02402     }
    02403
    02404     this->ShortenCargoColumn(1, 1, num_indrows);
    02405     const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(ICW_PANEL);
    02406     this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
    02407     this->SetDirty();
    02408     this->NotifySmallmap();
    02409   }
    02410
    02418   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    02419   {
    02420     if (!gui_scope) return;
    02421     if (data == NUM_INDUSTRYTYPES) {
    02422       if (this->IsWidgetLowered(ICW_NOTIFY)) {
    02423         this->RaiseWidget(ICW_NOTIFY);
    02424         this->SetWidgetDirty(ICW_NOTIFY);
    02425       }
    02426       return;
    02427     }
    02428
    02429     assert(data >= 0 && data < NUM_INDUSTRYTYPES);
    02430     this->ComputeIndustryDisplay(data);
    02431   }
    02432
    02433   virtual void DrawWidget(const Rect &r, int widget) const
    02434   {
    02435     if (widget != ICW_PANEL) return;
    02436
    02437     DrawPixelInfo tmp_dpi, *old_dpi;
    02438     int width = r.right - r.left + 1;
    02439     int height = r.bottom - r.top + 1 - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM;
    02440     if (!FillDrawPixelInfo(&tmp_dpi, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, width, height)) return;
    02441     old_dpi = _cur_dpi;
    02442     _cur_dpi = &tmp_dpi;
    02443
    02444     int left_pos = WD_FRAMERECT_LEFT;
    02445     if (this->ind_cargo >= NUM_INDUSTRYTYPES) left_pos += (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2;
    02446     int last_column = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
    02447
    02448     const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(ICW_PANEL);
    02449     int vpos = -this->vscroll->GetPosition() * nwp->resize_y;
    02450     for (uint i = 0; i < this->fields.Length(); i++) {
    02451       int row_height = (i == 0) ? CargoesField::small_height : CargoesField::normal_height;
    02452       if (vpos + row_height >= 0) {
    02453         int xpos = left_pos;
    02454         int col, dir;
    02455         if (_current_text_dir == TD_RTL) {
    02456           col = last_column;
    02457           dir = -1;
    02458         } else {
    02459           col = 0;
    02460           dir = 1;
    02461         }
    02462         while (col >= 0 && col <= last_column) {
    02463           this->fields[i].columns[col].Draw(xpos, vpos);
    02464           xpos += (col & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
    02465           col += dir;
    02466         }
    02467       }
    02468       vpos += row_height;
    02469       if (vpos >= height) break;
    02470     }
    02471
    02472     _cur_dpi = old_dpi;
    02473   }
    02474
    02482   bool CalculatePositionInWidget(Point pt, Point *fieldxy, Point *xy)
    02483   {
    02484     const NWidgetBase *nw = this->GetWidget<NWidgetBase>(ICW_PANEL);
    02485     pt.x -= nw->pos_x;
    02486     pt.y -= nw->pos_y;
    02487
    02488     int vpos = WD_FRAMERECT_TOP + CargoesField::small_height - this->vscroll->GetPosition() * nw->resize_y;
    02489     if (pt.y < vpos) return false;
    02490
    02491     int row = (pt.y - vpos) / CargoesField::normal_height; // row is relative to row 1.
    02492     if (row + 1 >= (int)this->fields.Length()) return false;
    02493     vpos = pt.y - vpos - row * CargoesField::normal_height; // Position in the row + 1 field
    02494     row++; // rebase row to match index of this->fields.
    02495
    02496     int xpos = 2 * WD_FRAMERECT_LEFT + ((this->ind_cargo < NUM_INDUSTRYTYPES) ? 0 :  (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2);
    02497     if (pt.x < xpos) return false;
    02498     int column;
    02499     for (column = 0; column <= 5; column++) {
    02500       int width = (column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
    02501       if (pt.x < xpos + width) break;
    02502       xpos += width;
    02503     }
    02504     int num_columns = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
    02505     if (column > num_columns) return false;
    02506     xpos = pt.x - xpos;
    02507
    02508     /* Return both positions, compensating for RTL languages (which works due to the equal symmetry in both displays). */
    02509     fieldxy->y = row;
    02510     xy->y = vpos;
    02511     if (_current_text_dir == TD_RTL) {
    02512       fieldxy->x = num_columns - column;
    02513       xy->x = ((column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width) - xpos;
    02514     } else {
    02515       fieldxy->x = column;
    02516       xy->x = xpos;
    02517     }
    02518     return true;
    02519   }
    02520
    02521   virtual void OnClick(Point pt, int widget, int click_count)
    02522   {
    02523     switch (widget) {
    02524       case ICW_PANEL: {
    02525         Point fieldxy, xy;
    02526         if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
    02527
    02528         const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
    02529         switch (fld->type) {
    02530           case CFT_INDUSTRY:
    02531             if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES) this->ComputeIndustryDisplay(fld->u.industry.ind_type);
    02532             break;
    02533
    02534           case CFT_CARGO: {
    02535             CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
    02536             CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
    02537             CargoID cid = fld->CargoClickedAt(lft, rgt, xy);
    02538             if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
    02539             break;
    02540           }
    02541
    02542           case CFT_CARGO_LABEL: {
    02543             CargoID cid = fld->CargoLabelClickedAt(xy);
    02544             if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
    02545             break;
    02546           }
    02547
    02548           default:
    02549             break;
    02550         }
    02551         break;
    02552       }
    02553
    02554       case ICW_NOTIFY:
    02555         this->ToggleWidgetLoweredState(ICW_NOTIFY);
    02556         this->SetWidgetDirty(ICW_NOTIFY);
    02557         SndPlayFx(SND_15_BEEP);
    02558
    02559         if (this->IsWidgetLowered(ICW_NOTIFY)) {
    02560           if (FindWindowByClass(WC_SMALLMAP) == NULL) ShowSmallMap();
    02561           this->NotifySmallmap();
    02562         }
    02563         break;
    02564     }
    02565   }
    02566
    02567   virtual void OnHover(Point pt, int widget)
    02568   {
    02569     if (widget != ICW_PANEL) return;
    02570
    02571     Point fieldxy, xy;
    02572     if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
    02573
    02574     const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
    02575     CargoID cid = INVALID_CARGO;
    02576     switch (fld->type) {
    02577       case CFT_CARGO: {
    02578         CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
    02579         CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
    02580         cid = fld->CargoClickedAt(lft, rgt, xy);
    02581         break;
    02582       }
    02583
    02584       case CFT_CARGO_LABEL: {
    02585         cid = fld->CargoLabelClickedAt(xy);
    02586         break;
    02587       }
    02588
    02589       case CFT_INDUSTRY:
    02590         if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES && (this->ind_cargo >= NUM_INDUSTRYTYPES || fieldxy.x != 2)) {
    02591           GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP, 0, NULL, TCC_HOVER);
    02592         }
    02593         return;
    02594
    02595       default:
    02596         break;
    02597     }
    02598     if (cid != INVALID_CARGO && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) {
    02599       const CargoSpec *csp = CargoSpec::Get(cid);
    02600       uint64 params[5];
    02601       params[0] = csp->name;
    02602       GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, 1, params, TCC_HOVER);
    02603     }
    02604   }
    02605
    02606   virtual void OnResize()
    02607   {
    02608     this->vscroll->SetCapacityFromWidget(this, ICW_PANEL);
    02609   }
    02610 };
    02611
    02612 const int IndustryCargoesWindow::HOR_TEXT_PADDING  = 5;
    02613 const int IndustryCargoesWindow::VERT_TEXT_PADDING = 5;
    02614
    02619 static void ShowIndustryCargoesWindow(IndustryType id)
    02620 {
    02621   assert(id < NUM_INDUSTRYTYPES);
    02622
    02623   Window *w = BringWindowToFrontById(WC_INDUSTRY_CARGOES, 0);
    02624   if (w != NULL) {
    02625     w->InvalidateData(id);
    02626     return;
    02627   }
    02628   new IndustryCargoesWindow(id);
    02629 }