# industry_gui.cpp

Go to the documentation of this file.

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