industry_gui.cpp

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

Generated on Sat Jun 19 17:14:49 2010 for OpenTTD by  doxygen 1.6.1