# industry_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: industry_gui.cpp 26789 2014-09-07 15:07:22Z 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 "network/network.h"
    00028 #include "strings_func.h"
    00029 #include "company_func.h"
    00030 #include "tilehighlight_func.h"
    00031 #include "string_func.h"
    00032 #include "sortlist_type.h"
    00033 #include "widgets/dropdown_func.h"
    00034 #include "company_base.h"
    00035 #include "core/geometry_func.hpp"
    00036 #include "core/random_func.hpp"
    00037 #include "core/backup_type.hpp"
    00038 #include "genworld.h"
    00039 #include "smallmap_gui.h"
    00040 #include "widgets/dropdown_type.h"
    00041 #include "widgets/industry_widget.h"
    00042
    00043 #include "table/strings.h"
    00044
    00045 bool _ignore_restrictions;
    00046 uint64 _displayed_industries;
    00047
    00048 assert_compile(NUM_INDUSTRYTYPES <= 64); // Make sure all industry types fit in _displayed_industries.
    00049
    00051 enum CargoSuffixType {
    00052   CST_FUND,
    00053   CST_VIEW,
    00054   CST_DIR,
    00055 };
    00056
    00057 static void ShowIndustryCargoesWindow(IndustryType id);
    00058
    00074 static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, char *suffix, const char *suffix_last)
    00075 {
    00076   suffix[0] = '\0';
    00077   if (HasBit(indspec->callback_mask, CBM_IND_CARGO_SUFFIX)) {
    00078     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);
    00079     if (callback == CALLBACK_FAILED || callback == 0x400) return;
    00080     if (callback > 0x400) {
    00081       ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_CARGO_SUFFIX, callback);
    00082     } else if (indspec->grf_prop.grffile->grf_version >= 8 || GB(callback, 0, 8) != 0xFF) {
    00083       StartTextRefStackUsage(indspec->grf_prop.grffile, 6);
    00084       GetString(suffix, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback), suffix_last);
    00085       StopTextRefStackUsage();
    00086     }
    00087   }
    00088 }
    00089
    00100 template <typename TC, typename TS>
    00101 static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, const TC &cargoes, TS &suffixes)
    00102 {
    00103   assert_compile(lengthof(cargoes) <= lengthof(suffixes));
    00104   for (uint j = 0; j < lengthof(cargoes); j++) {
    00105     if (cargoes[j] != CT_INVALID) {
    00106       GetCargoSuffix(cb_offset + j, cst, ind, ind_type, indspec, suffixes[j], lastof(suffixes[j]));
    00107     } else {
    00108       suffixes[j][0] = '\0';
    00109     }
    00110   }
    00111 }
    00112
    00113 IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES];
    00114
    00116 static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b)
    00117 {
    00118   static char industry_name[2][64];
    00119
    00120   const IndustrySpec *indsp1 = GetIndustrySpec(*a);
    00121   GetString(industry_name[0], indsp1->name, lastof(industry_name[0]));
    00122
    00123   const IndustrySpec *indsp2 = GetIndustrySpec(*b);
    00124   GetString(industry_name[1], indsp2->name, lastof(industry_name[1]));
    00125
    00126   int r = strnatcmp(industry_name[0], industry_name[1]); // Sort by name (natural sorting).
    00127
    00128   /* If the names are equal, sort by industry type. */
    00129   return (r != 0) ? r : (*a - *b);
    00130 }
    00131
    00135 void SortIndustryTypes()
    00136 {
    00137   /* Add each industry type to the list. */
    00138   for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
    00139     _sorted_industry_types[i] = i;
    00140   }
    00141
    00142   /* Sort industry types by name. */
    00143   QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter);
    00144 }
    00145
    00153 void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00154 {
    00155   if (result.Succeeded()) return;
    00156
    00157   uint8 indtype = GB(p1, 0, 8);
    00158   if (indtype < NUM_INDUSTRYTYPES) {
    00159     const IndustrySpec *indsp = GetIndustrySpec(indtype);
    00160     if (indsp->enabled) {
    00161       SetDParam(0, indsp->name);
    00162       ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, result.GetErrorMessage(), WL_INFO, TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE);
    00163     }
    00164   }
    00165 }
    00166
    00167 static const NWidgetPart _nested_build_industry_widgets[] = {
    00168   NWidget(NWID_HORIZONTAL),
    00169     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00170     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FUND_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00171     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00172     NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
    00173     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00174   EndContainer(),
    00175   NWidget(NWID_HORIZONTAL),
    00176     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),
    00177     NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_DPI_SCROLLBAR),
    00178   EndContainer(),
    00179   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_DPI_INFOPANEL), SetResize(1, 0),
    00180   EndContainer(),
    00181   NWidget(NWID_HORIZONTAL),
    00182     NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_DPI_DISPLAY_WIDGET), SetFill(1, 0), SetResize(1, 0),
    00183         SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
    00184     NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_DPI_FUND_WIDGET), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NULL),
    00185     NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    00186   EndContainer(),
    00187 };
    00188
    00190 static WindowDesc _build_industry_desc(
    00191   WDP_AUTO, "build_industry", 170, 212,
    00192   WC_BUILD_INDUSTRY, WC_NONE,
    00193   WDF_CONSTRUCTION,
    00194   _nested_build_industry_widgets, lengthof(_nested_build_industry_widgets)
    00195 );
    00196
    00198 class BuildIndustryWindow : public Window {
    00199   int selected_index;
    00200   IndustryType selected_type;
    00201   uint16 callback_timer;
    00202   bool timer_enabled;
    00203   uint16 count;
    00204   IndustryType index[NUM_INDUSTRYTYPES + 1];
    00205   bool enabled[NUM_INDUSTRYTYPES + 1];
    00206   Scrollbar *vscroll;
    00207
    00209   static const int MATRIX_TEXT_OFFSET = 17;
    00210
    00211   void SetupArrays()
    00212   {
    00213     this->count = 0;
    00214
    00215     for (uint i = 0; i < lengthof(this->index); i++) {
    00216       this->index[i]   = INVALID_INDUSTRYTYPE;
    00217       this->enabled[i] = false;
    00218     }
    00219
    00220     if (_game_mode == GM_EDITOR) { // give room for the Many Random "button"
    00221       this->index[this->count] = INVALID_INDUSTRYTYPE;
    00222       this->enabled[this->count] = true;
    00223       this->count++;
    00224       this->timer_enabled = false;
    00225     }
    00226     /* Fill the arrays with industries.
    00227      * The tests performed after the enabled allow to load the industries
    00228      * In the same way they are inserted by grf (if any)
    00229      */
    00230     for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
    00231       IndustryType ind = _sorted_industry_types[i];
    00232       const IndustrySpec *indsp = GetIndustrySpec(ind);
    00233       if (indsp->enabled) {
    00234         /* Rule is that editor mode loads all industries.
    00235          * In game mode, all non raw industries are loaded too
    00236          * and raw ones are loaded only when setting allows it */
    00237         if (_game_mode != GM_EDITOR && indsp->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) {
    00238           /* Unselect if the industry is no longer in the list */
    00239           if (this->selected_type == ind) this->selected_index = -1;
    00240           continue;
    00241         }
    00242         this->index[this->count] = ind;
    00243         this->enabled[this->count] = (_game_mode == GM_EDITOR) || GetIndustryProbabilityCallback(ind, IACT_USERCREATION, 1) > 0;
    00244         /* Keep the selection to the correct line */
    00245         if (this->selected_type == ind) this->selected_index = this->count;
    00246         this->count++;
    00247       }
    00248     }
    00249
    00250     /* first industry type is selected if the current selection is invalid.
    00251      * I'll be damned if there are none available ;) */
    00252     if (this->selected_index == -1) {
    00253       this->selected_index = 0;
    00254       this->selected_type = this->index[0];
    00255     }
    00256
    00257     this->vscroll->SetCount(this->count);
    00258   }
    00259
    00261   void SetButtons()
    00262   {
    00263     this->SetWidgetDisabledState(WID_DPI_FUND_WIDGET, this->selected_type != INVALID_INDUSTRYTYPE && !this->enabled[this->selected_index]);
    00264     this->SetWidgetDisabledState(WID_DPI_DISPLAY_WIDGET, this->selected_type == INVALID_INDUSTRYTYPE && this->enabled[this->selected_index]);
    00265   }
    00266
    00267 public:
    00268   BuildIndustryWindow() : Window(&_build_industry_desc)
    00269   {
    00270     this->timer_enabled = _loaded_newgrf_features.has_newindustries;
    00271
    00272     this->selected_index = -1;
    00273     this->selected_type = INVALID_INDUSTRYTYPE;
    00274
    00275     this->callback_timer = DAY_TICKS;
    00276
    00277     this->CreateNestedTree();
    00278     this->vscroll = this->GetScrollbar(WID_DPI_SCROLLBAR);
    00279     this->FinishInitNested(0);
    00280
    00281     this->SetButtons();
    00282   }
    00283
    00284   virtual void OnInit()
    00285   {
    00286     this->SetupArrays();
    00287   }
    00288
    00289   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00290   {
    00291     switch (widget) {
    00292       case WID_DPI_MATRIX_WIDGET: {
    00293         Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES);
    00294         for (byte i = 0; i < this->count; i++) {
    00295           if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
    00296           d = maxdim(d, GetStringBoundingBox(GetIndustrySpec(this->index[i])->name));
    00297         }
    00298         resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00299         d.width += MATRIX_TEXT_OFFSET + padding.width;
    00300         d.height = 5 * resize->height;
    00301         *size = maxdim(*size, d);
    00302         break;
    00303       }
    00304
    00305       case WID_DPI_INFOPANEL: {
    00306         /* Extra line for cost outside of editor + extra lines for 'extra' information for NewGRFs. */
    00307         int height = 2 + (_game_mode == GM_EDITOR ? 0 : 1) + (_loaded_newgrf_features.has_newindustries ? 4 : 0);
    00308         Dimension d = {0, 0};
    00309         for (byte i = 0; i < this->count; i++) {
    00310           if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
    00311
    00312           const IndustrySpec *indsp = GetIndustrySpec(this->index[i]);
    00313
    00314           char cargo_suffix[3][512];
    00315           GetAllCargoSuffixes(0, CST_FUND, NULL, this->index[i], indsp, indsp->accepts_cargo, cargo_suffix);
    00316           StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00317           byte p = 0;
    00318           SetDParam(0, STR_JUST_NOTHING);
    00319           SetDParamStr(1, "");
    00320           for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
    00321             if (indsp->accepts_cargo[j] == CT_INVALID) continue;
    00322             if (p > 0) str++;
    00323             SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
    00324             SetDParamStr(p++, cargo_suffix[j]);
    00325           }
    00326           d = maxdim(d, GetStringBoundingBox(str));
    00327
    00328           /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
    00329           GetAllCargoSuffixes(3, CST_FUND, NULL, this->index[i], indsp, indsp->produced_cargo, cargo_suffix);
    00330           str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
    00331           p = 0;
    00332           SetDParam(0, STR_JUST_NOTHING);
    00333           SetDParamStr(1, "");
    00334           for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
    00335             if (indsp->produced_cargo[j] == CT_INVALID) continue;
    00336             if (p > 0) str++;
    00337             SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
    00338             SetDParamStr(p++, cargo_suffix[j]);
    00339           }
    00340           d = maxdim(d, GetStringBoundingBox(str));
    00341         }
    00342
    00343         /* Set it to something more sane :) */
    00344         size->height = height * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00345         size->width  = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00346         break;
    00347       }
    00348
    00349       case WID_DPI_FUND_WIDGET: {
    00350         Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
    00351         d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY));
    00352         d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY));
    00353         d.width += padding.width;
    00354         d.height += padding.height;
    00355         *size = maxdim(*size, d);
    00356         break;
    00357       }
    00358     }
    00359   }
    00360
    00361   virtual void SetStringParameters(int widget) const
    00362   {
    00363     switch (widget) {
    00364       case WID_DPI_FUND_WIDGET:
    00365         /* Raw industries might be prospected. Show this fact by changing the string
    00366          * In Editor, you just build, while ingame, or you fund or you prospect */
    00367         if (_game_mode == GM_EDITOR) {
    00368           /* We've chosen many random industries but no industries have been specified */
    00369           SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
    00370         } else {
    00371           const IndustrySpec *indsp = GetIndustrySpec(this->index[this->selected_index]);
    00372           SetDParam(0, (_settings_game.construction.raw_industry_construction == 2 && indsp->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY : STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY);
    00373         }
    00374         break;
    00375     }
    00376   }
    00377
    00378   virtual void DrawWidget(const Rect &r, int widget) const
    00379   {
    00380     switch (widget) {
    00381       case WID_DPI_MATRIX_WIDGET: {
    00382         uint text_left, text_right, icon_left, icon_right;
    00383         if (_current_text_dir == TD_RTL) {
    00384           icon_right = r.right    - WD_MATRIX_RIGHT;
    00385           icon_left  = icon_right - 10;
    00386           text_right = icon_right - BuildIndustryWindow::MATRIX_TEXT_OFFSET;
    00387           text_left  = r.left     + WD_MATRIX_LEFT;
    00388         } else {
    00389           icon_left  = r.left     + WD_MATRIX_LEFT;
    00390           icon_right = icon_left  + 10;
    00391           text_left  = icon_left  + BuildIndustryWindow::MATRIX_TEXT_OFFSET;
    00392           text_right = r.right    - WD_MATRIX_RIGHT;
    00393         }
    00394
    00395         for (byte i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->count; i++) {
    00396           int y = r.top + WD_MATRIX_TOP + i * this->resize.step_height;
    00397           bool selected = this->selected_index == i + this->vscroll->GetPosition();
    00398
    00399           if (this->index[i + this->vscroll->GetPosition()] == INVALID_INDUSTRYTYPE) {
    00400             DrawString(text_left, text_right, y, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES, selected ? TC_WHITE : TC_ORANGE);
    00401             continue;
    00402           }
    00403           const IndustrySpec *indsp = GetIndustrySpec(this->index[i + this->vscroll->GetPosition()]);
    00404
    00405           /* Draw the name of the industry in white is selected, otherwise, in orange */
    00406           DrawString(text_left, text_right, y, indsp->name, selected ? TC_WHITE : TC_ORANGE);
    00407           GfxFillRect(icon_left,     y + 1, icon_right,     y + 7, selected ? PC_WHITE : PC_BLACK);
    00408           GfxFillRect(icon_left + 1, y + 2, icon_right - 1, y + 6, indsp->map_colour);
    00409         }
    00410         break;
    00411       }
    00412
    00413       case WID_DPI_INFOPANEL: {
    00414         int y      = r.top    + WD_FRAMERECT_TOP;
    00415         int bottom = r.bottom - WD_FRAMERECT_BOTTOM;
    00416         int left   = r.left   + WD_FRAMERECT_LEFT;
    00417         int right  = r.right  - WD_FRAMERECT_RIGHT;
    00418
    00419         if (this->selected_type == INVALID_INDUSTRYTYPE) {
    00420           DrawStringMultiLine(left, right, y,  bottom, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP);
    00421           break;
    00422         }
    00423
    00424         const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00425
    00426         if (_game_mode != GM_EDITOR) {
    00427           SetDParam(0, indsp->GetConstructionCost());
    00428           DrawString(left, right, y, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST);
    00429           y += FONT_HEIGHT_NORMAL;
    00430         }
    00431
    00432         /* Draw the accepted cargoes, if any. Otherwise, will print "Nothing". */
    00433         char cargo_suffix[3][512];
    00434         GetAllCargoSuffixes(0, CST_FUND, NULL, this->selected_type, indsp, indsp->accepts_cargo, cargo_suffix);
    00435         StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
    00436         byte p = 0;
    00437         SetDParam(0, STR_JUST_NOTHING);
    00438         SetDParamStr(1, "");
    00439         for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
    00440           if (indsp->accepts_cargo[j] == CT_INVALID) continue;
    00441           if (p > 0) str++;
    00442           SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
    00443           SetDParamStr(p++, cargo_suffix[j]);
    00444         }
    00445         DrawString(left, right, y, str);
    00446         y += FONT_HEIGHT_NORMAL;
    00447
    00448         /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
    00449         GetAllCargoSuffixes(3, CST_FUND, NULL, this->selected_type, indsp, indsp->produced_cargo, cargo_suffix);
    00450         str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
    00451         p = 0;
    00452         SetDParam(0, STR_JUST_NOTHING);
    00453         SetDParamStr(1, "");
    00454         for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
    00455           if (indsp->produced_cargo[j] == CT_INVALID) continue;
    00456           if (p > 0) str++;
    00457           SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
    00458           SetDParamStr(p++, cargo_suffix[j]);
    00459         }
    00460         DrawString(left, right, y, str);
    00461         y += FONT_HEIGHT_NORMAL;
    00462
    00463         /* Get the additional purchase info text, if it has not already been queried. */
    00464         str = STR_NULL;
    00465         if (HasBit(indsp->callback_mask, CBM_IND_FUND_MORE_TEXT)) {
    00466           uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT, 0, 0, NULL, this->selected_type, INVALID_TILE);
    00467           if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
    00468             if (callback_res > 0x400) {
    00469               ErrorUnknownCallbackResult(indsp->grf_prop.grffile->grfid, CBID_INDUSTRY_FUND_MORE_TEXT, callback_res);
    00470             } else {
    00471               str = GetGRFStringID(indsp->grf_prop.grffile->grfid, 0xD000 + callback_res);  // No. here's the new string
    00472               if (str != STR_UNDEFINED) {
    00473                 StartTextRefStackUsage(indsp->grf_prop.grffile, 6);
    00474                 DrawStringMultiLine(left, right, y, bottom, str, TC_YELLOW);
    00475                 StopTextRefStackUsage();
    00476               }
    00477             }
    00478           }
    00479         }
    00480         break;
    00481       }
    00482     }
    00483   }
    00484
    00485   virtual void OnClick(Point pt, int widget, int click_count)
    00486   {
    00487     switch (widget) {
    00488       case WID_DPI_MATRIX_WIDGET: {
    00489         int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_DPI_MATRIX_WIDGET);
    00490         if (y < this->count) { // Is it within the boundaries of available data?
    00491           this->selected_index = y;
    00492           this->selected_type = this->index[y];
    00493           const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00494
    00495           this->SetDirty();
    00496
    00497           if (_thd.GetCallbackWnd() == this &&
    00498               ((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
    00499               this->selected_type == INVALID_INDUSTRYTYPE ||
    00500               !this->enabled[this->selected_index])) {
    00501             /* Reset the button state if going to prospecting or "build many industries" */
    00502             this->RaiseButtons();
    00503             ResetObjectToPlace();
    00504           }
    00505
    00506           this->SetButtons();
    00507           if (this->enabled[this->selected_index] && click_count > 1) this->OnClick(pt, WID_DPI_FUND_WIDGET, 1);
    00508         }
    00509         break;
    00510       }
    00511
    00512       case WID_DPI_DISPLAY_WIDGET:
    00513         if (this->selected_type != INVALID_INDUSTRYTYPE) ShowIndustryCargoesWindow(this->selected_type);
    00514         break;
    00515
    00516       case WID_DPI_FUND_WIDGET: {
    00517         if (this->selected_type == INVALID_INDUSTRYTYPE) {
    00518           this->HandleButtonClick(WID_DPI_FUND_WIDGET);
    00519
    00520           if (Town::GetNumItems() == 0) {
    00521             ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO);
    00522           } else {
    00523             extern void GenerateIndustries();
    00524             _generating_world = true;
    00525             GenerateIndustries();
    00526             _generating_world = false;
    00527           }
    00528         } else if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && GetIndustrySpec(this->selected_type)->IsRawIndustry()) {
    00529           DoCommandP(0, this->selected_type, InteractiveRandom(), CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00530           this->HandleButtonClick(WID_DPI_FUND_WIDGET);
    00531         } else {
    00532           HandlePlacePushButton(this, WID_DPI_FUND_WIDGET, SPR_CURSOR_INDUSTRY, HT_RECT);
    00533         }
    00534         break;
    00535       }
    00536     }
    00537   }
    00538
    00539   virtual void OnResize()
    00540   {
    00541     /* Adjust the number of items in the matrix depending of the resize */
    00542     this->vscroll->SetCapacityFromWidget(this, WID_DPI_MATRIX_WIDGET);
    00543   }
    00544
    00545   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00546   {
    00547     bool success = true;
    00548     /* We do not need to protect ourselves against "Random Many Industries" in this mode */
    00549     const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00550     uint32 seed = InteractiveRandom();
    00551
    00552     if (_game_mode == GM_EDITOR) {
    00553       /* Show error if no town exists at all */
    00554       if (Town::GetNumItems() == 0) {
    00555         SetDParam(0, indsp->name);
    00556         ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO, pt.x, pt.y);
    00557         return;
    00558       }
    00559
    00560       Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
    00561       _generating_world = true;
    00562       _ignore_restrictions = true;
    00563
    00564       DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed,
    00565           CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY), &CcBuildIndustry);
    00566
    00567       cur_company.Restore();
    00568       _ignore_restrictions = false;
    00569       _generating_world = false;
    00570     } else {
    00571       success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
    00572     }
    00573
    00574     /* If an industry has been built, just reset the cursor and the system */
    00575     if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00576   }
    00577
    00578   virtual void OnTick()
    00579   {
    00580     if (_pause_mode != PM_UNPAUSED) return;
    00581     if (!this->timer_enabled) return;
    00582     if (--this->callback_timer == 0) {
    00583       /* We have just passed another day.
    00584        * See if we need to update availability of currently selected industry */
    00585       this->callback_timer = DAY_TICKS; // restart counter
    00586
    00587       const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
    00588
    00589       if (indsp->enabled) {
    00590         bool call_back_result = GetIndustryProbabilityCallback(this->selected_type, IACT_USERCREATION, 1) > 0;
    00591
    00592         /* Only if result does match the previous state would it require a redraw. */
    00593         if (call_back_result != this->enabled[this->selected_index]) {
    00594           this->enabled[this->selected_index] = call_back_result;
    00595           this->SetButtons();
    00596           this->SetDirty();
    00597         }
    00598       }
    00599     }
    00600   }
    00601
    00602   virtual void OnTimeout()
    00603   {
    00604     this->RaiseButtons();
    00605   }
    00606
    00607   virtual void OnPlaceObjectAbort()
    00608   {
    00609     this->RaiseButtons();
    00610   }
    00611
    00617   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00618   {
    00619     if (!gui_scope) return;
    00620     this->SetupArrays();
    00621
    00622     const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
    00623     if (indsp == NULL) this->enabled[this->selected_index] = _settings_game.difficulty.industry_density != ID_FUND_ONLY;
    00624     this->SetButtons();
    00625   }
    00626 };
    00627
    00628 void ShowBuildIndustryWindow()
    00629 {
    00630   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
    00631   if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return;
    00632   new BuildIndustryWindow();
    00633 }
    00634
    00635 static void UpdateIndustryProduction(Industry *i);
    00636
    00637 static inline bool IsProductionAlterable(const Industry *i)
    00638 {
    00639   const IndustrySpec *is = GetIndustrySpec(i->type);
    00640   return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
    00641       (is->production_rate[0] != 0 || is->production_rate[1] != 0 || is->IsRawIndustry()) &&
    00642       !_networking);
    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           DrawString(xpos, xpos2, ypos, indsp->name, TC_WHITE, SA_HOR_CENTER);
    01673
    01674           /* Draw the industry legend. */
    01675           int blob_left, blob_right;
    01676           if (_current_text_dir == TD_RTL) {
    01677             blob_right = xpos2 - BLOB_DISTANCE;
    01678             blob_left  = blob_right - BLOB_WIDTH;
    01679           } else {
    01680             blob_left  = xpos + BLOB_DISTANCE;
    01681             blob_right = blob_left + BLOB_WIDTH;
    01682           }
    01683           GfxFillRect(blob_left,     ypos2 - BLOB_DISTANCE - BLOB_HEIGHT,     blob_right,     ypos2 - BLOB_DISTANCE,     PC_BLACK); // Border
    01684           GfxFillRect(blob_left + 1, ypos2 - BLOB_DISTANCE - BLOB_HEIGHT + 1, blob_right - 1, ypos2 - BLOB_DISTANCE - 1, indsp->map_colour);
    01685         } else {
    01686           DrawString(xpos, xpos2, ypos, STR_INDUSTRY_CARGOES_HOUSES, TC_FROMSTRING, SA_HOR_CENTER);
    01687         }
    01688
    01689         /* Draw the other_produced/other_accepted cargoes. */
    01690         const CargoID *other_right, *other_left;
    01691         if (_current_text_dir == TD_RTL) {
    01692           other_right = this->u.industry.other_accepted;
    01693           other_left  = this->u.industry.other_produced;
    01694         } else {
    01695           other_right = this->u.industry.other_produced;
    01696           other_left  = this->u.industry.other_accepted;
    01697         }
    01698         ypos1 += VERT_CARGO_EDGE;
    01699         for (uint i = 0; i < MAX_CARGOES; i++) {
    01700           if (other_right[i] != INVALID_CARGO) {
    01701             const CargoSpec *csp = CargoSpec::Get(other_right[i]);
    01702             int xp = xpos + industry_width + CARGO_STUB_WIDTH;
    01703             DrawHorConnection(xpos + industry_width, xp - 1, ypos1, csp);
    01704             GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
    01705           }
    01706           if (other_left[i] != INVALID_CARGO) {
    01707             const CargoSpec *csp = CargoSpec::Get(other_left[i]);
    01708             int xp = xpos - CARGO_STUB_WIDTH;
    01709             DrawHorConnection(xp + 1, xpos - 1, ypos1, csp);
    01710             GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
    01711           }
    01712           ypos1 += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01713         }
    01714         break;
    01715       }
    01716
    01717       case CFT_CARGO: {
    01718         int cargo_base = this->GetCargoBase(xpos);
    01719         int top = ypos + (this->u.cargo.top_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0);
    01720         int bot = ypos - (this->u.cargo.bottom_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0) + normal_height - 1;
    01721         int colpos = cargo_base;
    01722         for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
    01723           if (this->u.cargo.top_end) GfxDrawLine(colpos, top - 1, colpos + HOR_CARGO_WIDTH - 1, top - 1, CARGO_LINE_COLOUR);
    01724           if (this->u.cargo.bottom_end) GfxDrawLine(colpos, bot + 1, colpos + HOR_CARGO_WIDTH - 1, bot + 1, CARGO_LINE_COLOUR);
    01725           GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
    01726           colpos++;
    01727           const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[i]);
    01728           GfxFillRect(colpos, top, colpos + HOR_CARGO_WIDTH - 2, bot, csp->legend_colour, FILLRECT_OPAQUE);
    01729           colpos += HOR_CARGO_WIDTH - 2;
    01730           GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
    01731           colpos += 1 + HOR_CARGO_SPACE;
    01732         }
    01733
    01734         const CargoID *hor_left, *hor_right;
    01735         if (_current_text_dir == TD_RTL) {
    01736           hor_left  = this->u.cargo.cust_cargoes;
    01737           hor_right = this->u.cargo.supp_cargoes;
    01738         } else {
    01739           hor_left  = this->u.cargo.supp_cargoes;
    01740           hor_right = this->u.cargo.cust_cargoes;
    01741         }
    01742         ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
    01743         for (uint i = 0; i < MAX_CARGOES; i++) {
    01744           if (hor_left[i] != INVALID_CARGO) {
    01745             int col = hor_left[i];
    01746             int dx = 0;
    01747             const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
    01748             for (; col > 0; col--) {
    01749               int lf = cargo_base + col * HOR_CARGO_WIDTH + (col - 1) * HOR_CARGO_SPACE;
    01750               DrawHorConnection(lf, lf + HOR_CARGO_SPACE - dx, ypos, csp);
    01751               dx = 1;
    01752             }
    01753             DrawHorConnection(xpos, cargo_base - dx, ypos, csp);
    01754           }
    01755           if (hor_right[i] != INVALID_CARGO) {
    01756             int col = hor_right[i];
    01757             int dx = 0;
    01758             const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
    01759             for (; col < this->u.cargo.num_cargoes - 1; col++) {
    01760               int lf = cargo_base + (col + 1) * HOR_CARGO_WIDTH + col * HOR_CARGO_SPACE;
    01761               DrawHorConnection(lf + dx - 1, lf + HOR_CARGO_SPACE - 1, ypos, csp);
    01762               dx = 1;
    01763             }
    01764             DrawHorConnection(cargo_base + col * HOR_CARGO_SPACE + (col + 1) * HOR_CARGO_WIDTH - 1 + dx, xpos + CARGO_FIELD_WIDTH - 1, ypos, csp);
    01765           }
    01766           ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01767         }
    01768         break;
    01769       }
    01770
    01771       case CFT_CARGO_LABEL:
    01772         ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
    01773         for (uint i = 0; i < MAX_CARGOES; i++) {
    01774           if (this->u.cargo_label.cargoes[i] != INVALID_CARGO) {
    01775             const CargoSpec *csp = CargoSpec::Get(this->u.cargo_label.cargoes[i]);
    01776             DrawString(xpos + WD_FRAMERECT_LEFT, xpos + industry_width - 1 - WD_FRAMERECT_RIGHT, ypos, csp->name, TC_WHITE,
    01777                 (this->u.cargo_label.left_align) ? SA_LEFT : SA_RIGHT);
    01778           }
    01779           ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01780         }
    01781         break;
    01782
    01783       default:
    01784         NOT_REACHED();
    01785     }
    01786   }
    01787
    01795   CargoID CargoClickedAt(const CargoesField *left, const CargoesField *right, Point pt) const
    01796   {
    01797     assert(this->type == CFT_CARGO);
    01798
    01799     /* Vertical matching. */
    01800     int cpos = this->GetCargoBase(0);
    01801     uint col;
    01802     for (col = 0; col < this->u.cargo.num_cargoes; col++) {
    01803       if (pt.x < cpos) break;
    01804       if (pt.x < cpos + CargoesField::HOR_CARGO_WIDTH) return this->u.cargo.vertical_cargoes[col];
    01805       cpos += CargoesField::HOR_CARGO_WIDTH + CargoesField::HOR_CARGO_SPACE;
    01806     }
    01807     /* col = 0 -> left of first col, 1 -> left of 2nd col, ... this->u.cargo.num_cargoes right of last-col. */
    01808
    01809     int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
    01810     uint row;
    01811     for (row = 0; row < MAX_CARGOES; row++) {
    01812       if (pt.y < vpos) return INVALID_CARGO;
    01813       if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
    01814       vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01815     }
    01816     if (row == MAX_CARGOES) return INVALID_CARGO;
    01817
    01818     /* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */
    01819     if (col == 0) {
    01820       if (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]];
    01821       if (left != NULL) {
    01822         if (left->type == CFT_INDUSTRY) return left->u.industry.other_produced[row];
    01823         if (left->type == CFT_CARGO_LABEL && !left->u.cargo_label.left_align) return left->u.cargo_label.cargoes[row];
    01824       }
    01825       return INVALID_CARGO;
    01826     }
    01827     if (col == this->u.cargo.num_cargoes) {
    01828       if (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]];
    01829       if (right != NULL) {
    01830         if (right->type == CFT_INDUSTRY) return right->u.industry.other_accepted[row];
    01831         if (right->type == CFT_CARGO_LABEL && right->u.cargo_label.left_align) return right->u.cargo_label.cargoes[row];
    01832       }
    01833       return INVALID_CARGO;
    01834     }
    01835     if (row >= col) {
    01836       /* Clicked somewhere in-between vertical cargo connection.
    01837        * Since the horizontal connection is made in the same order as the vertical list, the above condition
    01838        * ensures we are left-below the main diagonal, thus at the supplying side.
    01839        */
    01840       return (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]] : INVALID_CARGO;
    01841     } else {
    01842       /* Clicked at a customer connection. */
    01843       return (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]] : INVALID_CARGO;
    01844     }
    01845   }
    01846
    01852   CargoID CargoLabelClickedAt(Point pt) const
    01853   {
    01854     assert(this->type == CFT_CARGO_LABEL);
    01855
    01856     int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
    01857     uint row;
    01858     for (row = 0; row < MAX_CARGOES; row++) {
    01859       if (pt.y < vpos) return INVALID_CARGO;
    01860       if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
    01861       vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
    01862     }
    01863     if (row == MAX_CARGOES) return INVALID_CARGO;
    01864     return this->u.cargo_label.cargoes[row];
    01865   }
    01866
    01867 private:
    01875   static void DrawHorConnection(int left, int right, int top, const CargoSpec *csp)
    01876   {
    01877     GfxDrawLine(left, top, right, top, CARGO_LINE_COLOUR);
    01878     GfxFillRect(left, top + 1, right, top + FONT_HEIGHT_NORMAL - 2, csp->legend_colour, FILLRECT_OPAQUE);
    01879     GfxDrawLine(left, top + FONT_HEIGHT_NORMAL - 1, right, top + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
    01880   }
    01881 };
    01882
    01883 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, produced_cargo));
    01884 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, accepts_cargo));
    01885
    01886 int CargoesField::small_height;
    01887 int CargoesField::normal_height;
    01888 int CargoesField::industry_width;
    01889 const int CargoesField::VERT_INTER_INDUSTRY_SPACE = 6;
    01890
    01891 const int CargoesField::HOR_CARGO_BORDER_SPACE = 15;
    01892 const int CargoesField::CARGO_STUB_WIDTH       = 10;
    01893 const int CargoesField::HOR_CARGO_WIDTH        = 15;
    01894 const int CargoesField::HOR_CARGO_SPACE        =  5;
    01895 const int CargoesField::VERT_CARGO_EDGE        =  4;
    01896 const int CargoesField::VERT_CARGO_SPACE       =  4;
    01897
    01898 const int CargoesField::BLOB_DISTANCE =  5;
    01899 const int CargoesField::BLOB_WIDTH    = 12;
    01900 const int CargoesField::BLOB_HEIGHT   =  9;
    01901
    01903 const int CargoesField::CARGO_FIELD_WIDTH = HOR_CARGO_BORDER_SPACE * 2 + HOR_CARGO_WIDTH * MAX_CARGOES + HOR_CARGO_SPACE * (MAX_CARGOES - 1);
    01904
    01905 const int CargoesField::INDUSTRY_LINE_COLOUR = PC_YELLOW;
    01906 const int CargoesField::CARGO_LINE_COLOUR    = PC_YELLOW;
    01907
    01909 struct CargoesRow {
    01910   CargoesField columns[5];
    01911
    01916   void ConnectIndustryProduced(int column)
    01917   {
    01918     CargoesField *ind_fld   = this->columns + column;
    01919     CargoesField *cargo_fld = this->columns + column + 1;
    01920     assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
    01921
    01922     MemSetT(ind_fld->u.industry.other_produced, INVALID_CARGO, MAX_CARGOES);
    01923
    01924     if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
    01925       CargoID others[MAX_CARGOES]; // Produced cargoes not carried in the cargo column.
    01926       int other_count = 0;
    01927
    01928       const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
    01929       for (uint i = 0; i < lengthof(indsp->produced_cargo); i++) {
    01930         int col = cargo_fld->ConnectCargo(indsp->produced_cargo[i], true);
    01931         if (col < 0) others[other_count++] = indsp->produced_cargo[i];
    01932       }
    01933
    01934       /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
    01935       for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
    01936         if (cargo_fld->u.cargo.supp_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_produced[i] = others[--other_count];
    01937       }
    01938     } else {
    01939       /* Houses only display what is demanded. */
    01940       for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
    01941         CargoID cid = cargo_fld->u.cargo.vertical_cargoes[i];
    01942         if (cid == CT_PASSENGERS || cid == CT_MAIL) cargo_fld->ConnectCargo(cid, true);
    01943       }
    01944     }
    01945   }
    01946
    01952   void MakeCargoLabel(int column, bool accepting)
    01953   {
    01954     CargoID cargoes[MAX_CARGOES];
    01955     MemSetT(cargoes, INVALID_CARGO, lengthof(cargoes));
    01956
    01957     CargoesField *label_fld = this->columns + column;
    01958     CargoesField *cargo_fld = this->columns + (accepting ? column - 1 : column + 1);
    01959
    01960     assert(cargo_fld->type == CFT_CARGO && label_fld->type == CFT_EMPTY);
    01961     for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
    01962       int col = cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], !accepting);
    01963       if (col >= 0) cargoes[col] = cargo_fld->u.cargo.vertical_cargoes[i];
    01964     }
    01965     label_fld->MakeCargoLabel(cargoes, lengthof(cargoes), accepting);
    01966   }
    01967
    01968
    01973   void ConnectIndustryAccepted(int column)
    01974   {
    01975     CargoesField *ind_fld   = this->columns + column;
    01976     CargoesField *cargo_fld = this->columns + column - 1;
    01977     assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
    01978
    01979     MemSetT(ind_fld->u.industry.other_accepted, INVALID_CARGO, MAX_CARGOES);
    01980
    01981     if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
    01982       CargoID others[MAX_CARGOES]; // Accepted cargoes not carried in the cargo column.
    01983       int other_count = 0;
    01984
    01985       const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
    01986       for (uint i = 0; i < lengthof(indsp->accepts_cargo); i++) {
    01987         int col = cargo_fld->ConnectCargo(indsp->accepts_cargo[i], false);
    01988         if (col < 0) others[other_count++] = indsp->accepts_cargo[i];
    01989       }
    01990
    01991       /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
    01992       for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
    01993         if (cargo_fld->u.cargo.cust_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_accepted[i] = others[--other_count];
    01994       }
    01995     } else {
    01996       /* Houses only display what is demanded. */
    01997       for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
    01998         for (uint h = 0; h < NUM_HOUSES; h++) {
    01999           HouseSpec *hs = HouseSpec::Get(h);
    02000           if (!hs->enabled) continue;
    02001
    02002           for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
    02003             if (hs->cargo_acceptance[j] > 0 && cargo_fld->u.cargo.vertical_cargoes[i] == hs->accepts_cargo[j]) {
    02004               cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], false);
    02005               goto next_cargo;
    02006             }
    02007           }
    02008         }
    02009 next_cargo: ;
    02010       }
    02011     }
    02012   }
    02013 };
    02014
    02015
    02043 struct IndustryCargoesWindow : public Window {
    02044   static const int HOR_TEXT_PADDING, VERT_TEXT_PADDING;
    02045
    02046   typedef SmallVector<CargoesRow, 4> Fields;
    02047
    02048   Fields fields;
    02049   uint ind_cargo;
    02050   Dimension cargo_textsize;
    02051   Dimension ind_textsize;
    02052   Scrollbar *vscroll;
    02053
    02054   IndustryCargoesWindow(int id) : Window(&_industry_cargoes_desc)
    02055   {
    02056     this->OnInit();
    02057     this->CreateNestedTree();
    02058     this->vscroll = this->GetScrollbar(WID_IC_SCROLLBAR);
    02059     this->FinishInitNested(0);
    02060     this->OnInvalidateData(id);
    02061   }
    02062
    02063   virtual void OnInit()
    02064   {
    02065     /* Initialize static CargoesField size variables. */
    02066     Dimension d = GetStringBoundingBox(STR_INDUSTRY_CARGOES_PRODUCERS);
    02067     d = maxdim(d, GetStringBoundingBox(STR_INDUSTRY_CARGOES_CUSTOMERS));
    02068     d.width  += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
    02069     d.height += WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM;
    02070     CargoesField::small_height = d.height;
    02071
    02072     /* Decide about the size of the box holding the text of an industry type. */
    02073     this->ind_textsize.width = 0;
    02074     this->ind_textsize.height = 0;
    02075     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02076       const IndustrySpec *indsp = GetIndustrySpec(it);
    02077       if (!indsp->enabled) continue;
    02078       this->ind_textsize = maxdim(this->ind_textsize, GetStringBoundingBox(indsp->name));
    02079     }
    02080     d.width = max(d.width, this->ind_textsize.width);
    02081     d.height = this->ind_textsize.height;
    02082     this->ind_textsize = maxdim(this->ind_textsize, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY));
    02083
    02084     /* Compute max size of the cargo texts. */
    02085     this->cargo_textsize.width = 0;
    02086     this->cargo_textsize.height = 0;
    02087     for (uint i = 0; i < NUM_CARGO; i++) {
    02088       const CargoSpec *csp = CargoSpec::Get(i);
    02089       if (!csp->IsValid()) continue;
    02090       this->cargo_textsize = maxdim(this->cargo_textsize, GetStringBoundingBox(csp->name));
    02091     }
    02092     d = maxdim(d, this->cargo_textsize); // Box must also be wide enough to hold any cargo label.
    02093     this->cargo_textsize = maxdim(this->cargo_textsize, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_CARGO));
    02094
    02095     d.width  += 2 * HOR_TEXT_PADDING;
    02096     /* Ensure the height is enough for the industry type text, for the horizontal connections, and for the cargo labels. */
    02097     uint min_ind_height = CargoesField::VERT_CARGO_EDGE * 2 + MAX_CARGOES * FONT_HEIGHT_NORMAL + (MAX_CARGOES - 1) *  CargoesField::VERT_CARGO_SPACE;
    02098     d.height = max(d.height + 2 * VERT_TEXT_PADDING, min_ind_height);
    02099
    02100     CargoesField::industry_width = d.width;
    02101     CargoesField::normal_height = d.height + CargoesField::VERT_INTER_INDUSTRY_SPACE;
    02102   }
    02103
    02104   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    02105   {
    02106     switch (widget) {
    02107       case WID_IC_PANEL:
    02108         size->width = WD_FRAMETEXT_LEFT + CargoesField::industry_width * 3 + CargoesField::CARGO_FIELD_WIDTH * 2 + WD_FRAMETEXT_RIGHT;
    02109         break;
    02110
    02111       case WID_IC_IND_DROPDOWN:
    02112         size->width = max(size->width, this->ind_textsize.width + padding.width);
    02113         break;
    02114
    02115       case WID_IC_CARGO_DROPDOWN:
    02116         size->width = max(size->width, this->cargo_textsize.width + padding.width);
    02117         break;
    02118     }
    02119   }
    02120
    02121
    02122   CargoesFieldType type;
    02123   virtual void SetStringParameters  (int widget) const
    02124   {
    02125     if (widget != WID_IC_CAPTION) return;
    02126
    02127     if (this->ind_cargo < NUM_INDUSTRYTYPES) {
    02128       const IndustrySpec *indsp = GetIndustrySpec(this->ind_cargo);
    02129       SetDParam(0, indsp->name);
    02130     } else {
    02131       const CargoSpec *csp = CargoSpec::Get(this->ind_cargo - NUM_INDUSTRYTYPES);
    02132       SetDParam(0, csp->name);
    02133     }
    02134   }
    02135
    02144   static bool HasCommonValidCargo(const CargoID *cargoes1, uint length1, const CargoID *cargoes2, uint length2)
    02145   {
    02146     while (length1 > 0) {
    02147       if (*cargoes1 != INVALID_CARGO) {
    02148         for (uint i = 0; i < length2; i++) if (*cargoes1 == cargoes2[i]) return true;
    02149       }
    02150       cargoes1++;
    02151       length1--;
    02152     }
    02153     return false;
    02154   }
    02155
    02162   static bool HousesCanSupply(const CargoID *cargoes, uint length)
    02163   {
    02164     for (uint i = 0; i < length; i++) {
    02165       if (cargoes[i] == INVALID_CARGO) continue;
    02166       if (cargoes[i] == CT_PASSENGERS || cargoes[i] == CT_MAIL) return true;
    02167     }
    02168     return false;
    02169   }
    02170
    02177   static bool HousesCanAccept(const CargoID *cargoes, uint length)
    02178   {
    02179     HouseZones climate_mask;
    02180     switch (_settings_game.game_creation.landscape) {
    02181       case LT_TEMPERATE: climate_mask = HZ_TEMP; break;
    02182       case LT_ARCTIC:    climate_mask = HZ_SUBARTC_ABOVE | HZ_SUBARTC_BELOW; break;
    02183       case LT_TROPIC:    climate_mask = HZ_SUBTROPIC; break;
    02184       case LT_TOYLAND:   climate_mask = HZ_TOYLND; break;
    02185       default: NOT_REACHED();
    02186     }
    02187     for (uint i = 0; i < length; i++) {
    02188       if (cargoes[i] == INVALID_CARGO) continue;
    02189
    02190       for (uint h = 0; h < NUM_HOUSES; h++) {
    02191         HouseSpec *hs = HouseSpec::Get(h);
    02192         if (!hs->enabled || !(hs->building_availability & climate_mask)) continue;
    02193
    02194         for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
    02195           if (hs->cargo_acceptance[j] > 0 && cargoes[i] == hs->accepts_cargo[j]) return true;
    02196         }
    02197       }
    02198     }
    02199     return false;
    02200   }
    02201
    02208   static int CountMatchingAcceptingIndustries(const CargoID *cargoes, uint length)
    02209   {
    02210     int count = 0;
    02211     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02212       const IndustrySpec *indsp = GetIndustrySpec(it);
    02213       if (!indsp->enabled) continue;
    02214
    02215       if (HasCommonValidCargo(cargoes, length, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) count++;
    02216     }
    02217     return count;
    02218   }
    02219
    02226   static int CountMatchingProducingIndustries(const CargoID *cargoes, uint length)
    02227   {
    02228     int count = 0;
    02229     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02230       const IndustrySpec *indsp = GetIndustrySpec(it);
    02231       if (!indsp->enabled) continue;
    02232
    02233       if (HasCommonValidCargo(cargoes, length, indsp->produced_cargo, lengthof(indsp->produced_cargo))) count++;
    02234     }
    02235     return count;
    02236   }
    02237
    02244   void ShortenCargoColumn(int column, int top, int bottom)
    02245   {
    02246     while (top < bottom && !this->fields[top].columns[column].HasConnection()) {
    02247       this->fields[top].columns[column].MakeEmpty(CFT_EMPTY);
    02248       top++;
    02249     }
    02250     this->fields[top].columns[column].u.cargo.top_end = true;
    02251
    02252     while (bottom > top && !this->fields[bottom].columns[column].HasConnection()) {
    02253       this->fields[bottom].columns[column].MakeEmpty(CFT_EMPTY);
    02254       bottom--;
    02255     }
    02256     this->fields[bottom].columns[column].u.cargo.bottom_end = true;
    02257   }
    02258
    02265   void PlaceIndustry(int row, int col, IndustryType it)
    02266   {
    02267     assert(this->fields[row].columns[col].type == CFT_EMPTY);
    02268     this->fields[row].columns[col].MakeIndustry(it);
    02269     if (col == 0) {
    02270       this->fields[row].ConnectIndustryProduced(col);
    02271     } else {
    02272       this->fields[row].ConnectIndustryAccepted(col);
    02273     }
    02274   }
    02275
    02279   void NotifySmallmap()
    02280   {
    02281     if (!this->IsWidgetLowered(WID_IC_NOTIFY)) return;
    02282
    02283     /* Only notify the smallmap window if it exists. In particular, do not
    02284      * bring it to the front to prevent messing up any nice layout of the user. */
    02285     InvalidateWindowClassesData(WC_SMALLMAP, 0);
    02286   }
    02287
    02292   void ComputeIndustryDisplay(IndustryType it)
    02293   {
    02294     this->GetWidget<NWidgetCore>(WID_IC_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION;
    02295     this->ind_cargo = it;
    02296     _displayed_industries = 1ULL << it;
    02297
    02298     this->fields.Clear();
    02299     CargoesRow *row = this->fields.Append();
    02300     row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
    02301     row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
    02302     row->columns[2].MakeEmpty(CFT_SMALL_EMPTY);
    02303     row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
    02304     row->columns[4].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
    02305
    02306     const IndustrySpec *central_sp = GetIndustrySpec(it);
    02307     bool houses_supply = HousesCanSupply(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
    02308     bool houses_accept = HousesCanAccept(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
    02309     /* Make a field consisting of two cargo columns. */
    02310     int num_supp = CountMatchingProducingIndustries(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo)) + houses_supply;
    02311     int num_cust = CountMatchingAcceptingIndustries(central_sp->produced_cargo, lengthof(central_sp->produced_cargo)) + houses_accept;
    02312     int num_indrows = max(3, max(num_supp, num_cust)); // One is needed for the 'it' industry, and 2 for the cargo labels.
    02313     for (int i = 0; i < num_indrows; i++) {
    02314       CargoesRow *row = this->fields.Append();
    02315       row->columns[0].MakeEmpty(CFT_EMPTY);
    02316       row->columns[1].MakeCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
    02317       row->columns[2].MakeEmpty(CFT_EMPTY);
    02318       row->columns[3].MakeCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
    02319       row->columns[4].MakeEmpty(CFT_EMPTY);
    02320     }
    02321     /* Add central industry. */
    02322     int central_row = 1 + num_indrows / 2;
    02323     this->fields[central_row].columns[2].MakeIndustry(it);
    02324     this->fields[central_row].ConnectIndustryProduced(2);
    02325     this->fields[central_row].ConnectIndustryAccepted(2);
    02326
    02327     /* Add cargo labels. */
    02328     this->fields[central_row - 1].MakeCargoLabel(2, true);
    02329     this->fields[central_row + 1].MakeCargoLabel(2, false);
    02330
    02331     /* Add suppliers and customers of the 'it' industry. */
    02332     int supp_count = 0;
    02333     int cust_count = 0;
    02334     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02335       const IndustrySpec *indsp = GetIndustrySpec(it);
    02336       if (!indsp->enabled) continue;
    02337
    02338       if (HasCommonValidCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo), indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
    02339         this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
    02340         SetBit(_displayed_industries, it);
    02341         supp_count++;
    02342       }
    02343       if (HasCommonValidCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo), indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
    02344         this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, it);
    02345         SetBit(_displayed_industries, it);
    02346         cust_count++;
    02347       }
    02348     }
    02349     if (houses_supply) {
    02350       this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
    02351       supp_count++;
    02352     }
    02353     if (houses_accept) {
    02354       this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, NUM_INDUSTRYTYPES);
    02355       cust_count++;
    02356     }
    02357
    02358     this->ShortenCargoColumn(1, 1, num_indrows);
    02359     this->ShortenCargoColumn(3, 1, num_indrows);
    02360     const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
    02361     this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
    02362     this->SetDirty();
    02363     this->NotifySmallmap();
    02364   }
    02365
    02370   void ComputeCargoDisplay(CargoID cid)
    02371   {
    02372     this->GetWidget<NWidgetCore>(WID_IC_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_CARGO_CAPTION;
    02373     this->ind_cargo = cid + NUM_INDUSTRYTYPES;
    02374     _displayed_industries = 0;
    02375
    02376     this->fields.Clear();
    02377     CargoesRow *row = this->fields.Append();
    02378     row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
    02379     row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
    02380     row->columns[2].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
    02381     row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
    02382     row->columns[4].MakeEmpty(CFT_SMALL_EMPTY);
    02383
    02384     bool houses_supply = HousesCanSupply(&cid, 1);
    02385     bool houses_accept = HousesCanAccept(&cid, 1);
    02386     int num_supp = CountMatchingProducingIndustries(&cid, 1) + houses_supply + 1; // Ensure room for the cargo label.
    02387     int num_cust = CountMatchingAcceptingIndustries(&cid, 1) + houses_accept;
    02388     int num_indrows = max(num_supp, num_cust);
    02389     for (int i = 0; i < num_indrows; i++) {
    02390       CargoesRow *row = this->fields.Append();
    02391       row->columns[0].MakeEmpty(CFT_EMPTY);
    02392       row->columns[1].MakeCargo(&cid, 1);
    02393       row->columns[2].MakeEmpty(CFT_EMPTY);
    02394       row->columns[3].MakeEmpty(CFT_EMPTY);
    02395       row->columns[4].MakeEmpty(CFT_EMPTY);
    02396     }
    02397
    02398     this->fields[num_indrows].MakeCargoLabel(0, false); // Add cargo labels at the left bottom.
    02399
    02400     /* Add suppliers and customers of the cargo. */
    02401     int supp_count = 0;
    02402     int cust_count = 0;
    02403     for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    02404       const IndustrySpec *indsp = GetIndustrySpec(it);
    02405       if (!indsp->enabled) continue;
    02406
    02407       if (HasCommonValidCargo(&cid, 1, indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
    02408         this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
    02409         SetBit(_displayed_industries, it);
    02410         supp_count++;
    02411       }
    02412       if (HasCommonValidCargo(&cid, 1, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
    02413         this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, it);
    02414         SetBit(_displayed_industries, it);
    02415         cust_count++;
    02416       }
    02417     }
    02418     if (houses_supply) {
    02419       this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
    02420       supp_count++;
    02421     }
    02422     if (houses_accept) {
    02423       this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, NUM_INDUSTRYTYPES);
    02424       cust_count++;
    02425     }
    02426
    02427     this->ShortenCargoColumn(1, 1, num_indrows);
    02428     const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
    02429     this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
    02430     this->SetDirty();
    02431     this->NotifySmallmap();
    02432   }
    02433
    02441   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    02442   {
    02443     if (!gui_scope) return;
    02444     if (data == NUM_INDUSTRYTYPES) {
    02445       if (this->IsWidgetLowered(WID_IC_NOTIFY)) {
    02446         this->RaiseWidget(WID_IC_NOTIFY);
    02447         this->SetWidgetDirty(WID_IC_NOTIFY);
    02448       }
    02449       return;
    02450     }
    02451
    02452     assert(data >= 0 && data < NUM_INDUSTRYTYPES);
    02453     this->ComputeIndustryDisplay(data);
    02454   }
    02455
    02456   virtual void DrawWidget(const Rect &r, int widget) const
    02457   {
    02458     if (widget != WID_IC_PANEL) return;
    02459
    02460     DrawPixelInfo tmp_dpi, *old_dpi;
    02461     int width = r.right - r.left + 1;
    02462     int height = r.bottom - r.top + 1 - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM;
    02463     if (!FillDrawPixelInfo(&tmp_dpi, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, width, height)) return;
    02464     old_dpi = _cur_dpi;
    02465     _cur_dpi = &tmp_dpi;
    02466
    02467     int left_pos = WD_FRAMERECT_LEFT;
    02468     if (this->ind_cargo >= NUM_INDUSTRYTYPES) left_pos += (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2;
    02469     int last_column = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
    02470
    02471     const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
    02472     int vpos = -this->vscroll->GetPosition() * nwp->resize_y;
    02473     for (uint i = 0; i < this->fields.Length(); i++) {
    02474       int row_height = (i == 0) ? CargoesField::small_height : CargoesField::normal_height;
    02475       if (vpos + row_height >= 0) {
    02476         int xpos = left_pos;
    02477         int col, dir;
    02478         if (_current_text_dir == TD_RTL) {
    02479           col = last_column;
    02480           dir = -1;
    02481         } else {
    02482           col = 0;
    02483           dir = 1;
    02484         }
    02485         while (col >= 0 && col <= last_column) {
    02486           this->fields[i].columns[col].Draw(xpos, vpos);
    02487           xpos += (col & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
    02488           col += dir;
    02489         }
    02490       }
    02491       vpos += row_height;
    02492       if (vpos >= height) break;
    02493     }
    02494
    02495     _cur_dpi = old_dpi;
    02496   }
    02497
    02505   bool CalculatePositionInWidget(Point pt, Point *fieldxy, Point *xy)
    02506   {
    02507     const NWidgetBase *nw = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
    02508     pt.x -= nw->pos_x;
    02509     pt.y -= nw->pos_y;
    02510
    02511     int vpos = WD_FRAMERECT_TOP + CargoesField::small_height - this->vscroll->GetPosition() * nw->resize_y;
    02512     if (pt.y < vpos) return false;
    02513
    02514     int row = (pt.y - vpos) / CargoesField::normal_height; // row is relative to row 1.
    02515     if (row + 1 >= (int)this->fields.Length()) return false;
    02516     vpos = pt.y - vpos - row * CargoesField::normal_height; // Position in the row + 1 field
    02517     row++; // rebase row to match index of this->fields.
    02518
    02519     int xpos = 2 * WD_FRAMERECT_LEFT + ((this->ind_cargo < NUM_INDUSTRYTYPES) ? 0 :  (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2);
    02520     if (pt.x < xpos) return false;
    02521     int column;
    02522     for (column = 0; column <= 5; column++) {
    02523       int width = (column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
    02524       if (pt.x < xpos + width) break;
    02525       xpos += width;
    02526     }
    02527     int num_columns = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
    02528     if (column > num_columns) return false;
    02529     xpos = pt.x - xpos;
    02530
    02531     /* Return both positions, compensating for RTL languages (which works due to the equal symmetry in both displays). */
    02532     fieldxy->y = row;
    02533     xy->y = vpos;
    02534     if (_current_text_dir == TD_RTL) {
    02535       fieldxy->x = num_columns - column;
    02536       xy->x = ((column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width) - xpos;
    02537     } else {
    02538       fieldxy->x = column;
    02539       xy->x = xpos;
    02540     }
    02541     return true;
    02542   }
    02543
    02544   virtual void OnClick(Point pt, int widget, int click_count)
    02545   {
    02546     switch (widget) {
    02547       case WID_IC_PANEL: {
    02548         Point fieldxy, xy;
    02549         if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
    02550
    02551         const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
    02552         switch (fld->type) {
    02553           case CFT_INDUSTRY:
    02554             if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES) this->ComputeIndustryDisplay(fld->u.industry.ind_type);
    02555             break;
    02556
    02557           case CFT_CARGO: {
    02558             CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
    02559             CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
    02560             CargoID cid = fld->CargoClickedAt(lft, rgt, xy);
    02561             if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
    02562             break;
    02563           }
    02564
    02565           case CFT_CARGO_LABEL: {
    02566             CargoID cid = fld->CargoLabelClickedAt(xy);
    02567             if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
    02568             break;
    02569           }
    02570
    02571           default:
    02572             break;
    02573         }
    02574         break;
    02575       }
    02576
    02577       case WID_IC_NOTIFY:
    02578         this->ToggleWidgetLoweredState(WID_IC_NOTIFY);
    02579         this->SetWidgetDirty(WID_IC_NOTIFY);
    02580         if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
    02581
    02582         if (this->IsWidgetLowered(WID_IC_NOTIFY)) {
    02583           if (FindWindowByClass(WC_SMALLMAP) == NULL) ShowSmallMap();
    02584           this->NotifySmallmap();
    02585         }
    02586         break;
    02587
    02588       case WID_IC_CARGO_DROPDOWN: {
    02589         DropDownList *lst = new DropDownList;
    02590         const CargoSpec *cs;
    02591         FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
    02592           *lst->Append() = new DropDownListStringItem(cs->name, cs->Index(), false);
    02593         }
    02594         if (lst->Length() == 0) {
    02595           delete lst;
    02596           break;
    02597         }
    02598         int selected = (this->ind_cargo >= NUM_INDUSTRYTYPES) ? (int)(this->ind_cargo - NUM_INDUSTRYTYPES) : -1;
    02599         ShowDropDownList(this, lst, selected, WID_IC_CARGO_DROPDOWN, 0, true);
    02600         break;
    02601       }
    02602
    02603       case WID_IC_IND_DROPDOWN: {
    02604         DropDownList *lst = new DropDownList;
    02605         for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
    02606           IndustryType ind = _sorted_industry_types[i];
    02607           const IndustrySpec *indsp = GetIndustrySpec(ind);
    02608           if (!indsp->enabled) continue;
    02609           *lst->Append() = new DropDownListStringItem(indsp->name, ind, false);
    02610         }
    02611         if (lst->Length() == 0) {
    02612           delete lst;
    02613           break;
    02614         }
    02615         int selected = (this->ind_cargo < NUM_INDUSTRYTYPES) ? (int)this->ind_cargo : -1;
    02616         ShowDropDownList(this, lst, selected, WID_IC_IND_DROPDOWN, 0, true);
    02617         break;
    02618       }
    02619     }
    02620   }
    02621
    02622   virtual void OnDropdownSelect(int widget, int index)
    02623   {
    02624     if (index < 0) return;
    02625
    02626     switch (widget) {
    02627       case WID_IC_CARGO_DROPDOWN:
    02628         this->ComputeCargoDisplay(index);
    02629         break;
    02630
    02631       case WID_IC_IND_DROPDOWN:
    02632         this->ComputeIndustryDisplay(index);
    02633         break;
    02634     }
    02635   }
    02636
    02637   virtual void OnHover(Point pt, int widget)
    02638   {
    02639     if (widget != WID_IC_PANEL) return;
    02640
    02641     Point fieldxy, xy;
    02642     if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
    02643
    02644     const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
    02645     CargoID cid = INVALID_CARGO;
    02646     switch (fld->type) {
    02647       case CFT_CARGO: {
    02648         CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
    02649         CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
    02650         cid = fld->CargoClickedAt(lft, rgt, xy);
    02651         break;
    02652       }
    02653
    02654       case CFT_CARGO_LABEL: {
    02655         cid = fld->CargoLabelClickedAt(xy);
    02656         break;
    02657       }
    02658
    02659       case CFT_INDUSTRY:
    02660         if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES && (this->ind_cargo >= NUM_INDUSTRYTYPES || fieldxy.x != 2)) {
    02661           GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP, 0, NULL, TCC_HOVER);
    02662         }
    02663         return;
    02664
    02665       default:
    02666         break;
    02667     }
    02668     if (cid != INVALID_CARGO && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) {
    02669       const CargoSpec *csp = CargoSpec::Get(cid);
    02670       uint64 params[5];
    02671       params[0] = csp->name;
    02672       GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, 1, params, TCC_HOVER);
    02673     }
    02674   }
    02675
    02676   virtual void OnResize()
    02677   {
    02678     this->vscroll->SetCapacityFromWidget(this, WID_IC_PANEL);
    02679   }
    02680 };
    02681
    02682 const int IndustryCargoesWindow::HOR_TEXT_PADDING  = 5;
    02683 const int IndustryCargoesWindow::VERT_TEXT_PADDING = 5;
    02684
    02689 static void ShowIndustryCargoesWindow(IndustryType id)
    02690 {
    02691   if (id >= NUM_INDUSTRYTYPES) {
    02692     for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
    02693       const IndustrySpec *indsp = GetIndustrySpec(_sorted_industry_types[i]);
    02694       if (indsp->enabled) {
    02695         id = _sorted_industry_types[i];
    02696         break;
    02697       }
    02698     }
    02699     if (id >= NUM_INDUSTRYTYPES) return;
    02700   }
    02701
    02702   Window *w = BringWindowToFrontById(WC_INDUSTRY_CARGOES, 0);
    02703   if (w != NULL) {
    02704     w->InvalidateData(id);
    02705     return;
    02706   }
    02707   new IndustryCargoesWindow(id);
    02708 }
    02709
    02711 void ShowIndustryCargoesWindow()
    02712 {
    02713   ShowIndustryCargoesWindow(NUM_INDUSTRYTYPES);
    02714 }