industry_gui.cpp

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