graph_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "graph_gui.h"
00014 #include "window_gui.h"
00015 #include "company_base.h"
00016 #include "company_gui.h"
00017 #include "economy_func.h"
00018 #include "cargotype.h"
00019 #include "strings_func.h"
00020 #include "window_func.h"
00021 #include "date_func.h"
00022 #include "gfx_func.h"
00023 #include "sortlist_type.h"
00024 #include "core/geometry_func.hpp"
00025 #include "currency.h"
00026 
00027 #include "widgets/graph_widget.h"
00028 
00029 #include "table/strings.h"
00030 #include "table/sprites.h"
00031 #include <math.h>
00032 
00033 /* Bitmasks of company and cargo indices that shouldn't be drawn. */
00034 static uint _legend_excluded_companies;
00035 static uint _legend_excluded_cargo;
00036 
00037 /* Apparently these don't play well with enums. */
00038 static const OverflowSafeInt64 INVALID_DATAPOINT(INT64_MAX); // Value used for a datapoint that shouldn't be drawn.
00039 static const uint INVALID_DATAPOINT_POS = UINT_MAX;  // Used to determine if the previous point was drawn.
00040 
00041 /****************/
00042 /* GRAPH LEGEND */
00043 /****************/
00044 
00045 struct GraphLegendWindow : Window {
00046   GraphLegendWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
00047   {
00048     this->InitNested(window_number);
00049 
00050     for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00051       if (!HasBit(_legend_excluded_companies, c)) this->LowerWidget(c + WID_GL_FIRST_COMPANY);
00052 
00053       this->OnInvalidateData(c);
00054     }
00055   }
00056 
00057   virtual void DrawWidget(const Rect &r, int widget) const
00058   {
00059     if (!IsInsideMM(widget, WID_GL_FIRST_COMPANY, MAX_COMPANIES + WID_GL_FIRST_COMPANY)) return;
00060 
00061     CompanyID cid = (CompanyID)(widget - WID_GL_FIRST_COMPANY);
00062 
00063     if (!Company::IsValidID(cid)) return;
00064 
00065     bool rtl = _current_text_dir == TD_RTL;
00066 
00067     Dimension d = GetSpriteSize(SPR_COMPANY_ICON);
00068     DrawCompanyIcon(cid, rtl ? r.right - d.width - 2 : r.left + 2, r.top + (r.bottom - r.top - d.height) / 2);
00069 
00070     SetDParam(0, cid);
00071     SetDParam(1, cid);
00072     DrawString(r.left + (rtl ? (uint)WD_FRAMERECT_LEFT : (d.width + 4)), r.right - (rtl ? (d.width + 4) : (uint)WD_FRAMERECT_RIGHT), r.top + (r.bottom - r.top + 1 - FONT_HEIGHT_NORMAL) / 2, STR_COMPANY_NAME_COMPANY_NUM, HasBit(_legend_excluded_companies, cid) ? TC_BLACK : TC_WHITE);
00073   }
00074 
00075   virtual void OnClick(Point pt, int widget, int click_count)
00076   {
00077     if (!IsInsideMM(widget, WID_GL_FIRST_COMPANY, MAX_COMPANIES + WID_GL_FIRST_COMPANY)) return;
00078 
00079     ToggleBit(_legend_excluded_companies, widget - WID_GL_FIRST_COMPANY);
00080     this->ToggleWidgetLoweredState(widget);
00081     this->SetDirty();
00082     InvalidateWindowData(WC_INCOME_GRAPH, 0);
00083     InvalidateWindowData(WC_OPERATING_PROFIT, 0);
00084     InvalidateWindowData(WC_DELIVERED_CARGO, 0);
00085     InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
00086     InvalidateWindowData(WC_COMPANY_VALUE, 0);
00087   }
00088 
00094   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00095   {
00096     if (!gui_scope) return;
00097     if (Company::IsValidID(data)) return;
00098 
00099     SetBit(_legend_excluded_companies, data);
00100     this->RaiseWidget(data + WID_GL_FIRST_COMPANY);
00101   }
00102 };
00103 
00110 static NWidgetBase *MakeNWidgetCompanyLines(int *biggest_index)
00111 {
00112   NWidgetVertical *vert = new NWidgetVertical();
00113   uint line_height = max<uint>(GetSpriteSize(SPR_COMPANY_ICON).height, FONT_HEIGHT_NORMAL) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00114 
00115   for (int widnum = WID_GL_FIRST_COMPANY; widnum <= WID_GL_LAST_COMPANY; widnum++) {
00116     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
00117     panel->SetMinimalSize(246, line_height);
00118     panel->SetFill(1, 0);
00119     panel->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP);
00120     vert->Add(panel);
00121   }
00122   *biggest_index = WID_GL_LAST_COMPANY;
00123   return vert;
00124 }
00125 
00126 static const NWidgetPart _nested_graph_legend_widgets[] = {
00127   NWidget(NWID_HORIZONTAL),
00128     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00129     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_KEY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00130     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00131     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00132   EndContainer(),
00133   NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_BACKGROUND),
00134     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00135     NWidget(NWID_HORIZONTAL),
00136       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00137       NWidgetFunction(MakeNWidgetCompanyLines),
00138       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00139     EndContainer(),
00140   EndContainer(),
00141 };
00142 
00143 static WindowDesc _graph_legend_desc(
00144   WDP_AUTO, "graph_legend", 0, 0,
00145   WC_GRAPH_LEGEND, WC_NONE,
00146   0,
00147   _nested_graph_legend_widgets, lengthof(_nested_graph_legend_widgets)
00148 );
00149 
00150 static void ShowGraphLegend()
00151 {
00152   AllocateWindowDescFront<GraphLegendWindow>(&_graph_legend_desc, 0);
00153 }
00154 
00156 struct ValuesInterval {
00157   OverflowSafeInt64 highest; 
00158   OverflowSafeInt64 lowest;  
00159 };
00160 
00161 /******************/
00162 /* BASE OF GRAPHS */
00163 /*****************/
00164 
00165 struct BaseGraphWindow : Window {
00166 protected:
00167   static const int GRAPH_MAX_DATASETS     =  32;
00168   static const int GRAPH_AXIS_LINE_COLOUR = PC_BLACK;
00169   static const int GRAPH_NUM_MONTHS       =  24; 
00170 
00171   static const int MIN_GRAPH_NUM_LINES_Y  =   9; 
00172   static const int MIN_GRID_PIXEL_SIZE    =  20; 
00173 
00174   uint excluded_data; 
00175   byte num_dataset;
00176   byte num_on_x_axis;
00177   byte num_vert_lines;
00178   static const TextColour graph_axis_label_colour = TC_BLACK; 
00179 
00180   /* The starting month and year that values are plotted against. If month is
00181    * 0xFF, use x_values_start and x_values_increment below instead. */
00182   byte month;
00183   Year year;
00184 
00185   /* These values are used if the graph is being plotted against values
00186    * rather than the dates specified by month and year. */
00187   uint16 x_values_start;
00188   uint16 x_values_increment;
00189 
00190   int graph_widget;
00191   StringID format_str_y_axis;
00192   byte colours[GRAPH_MAX_DATASETS];
00193   OverflowSafeInt64 cost[GRAPH_MAX_DATASETS][GRAPH_NUM_MONTHS]; 
00194 
00201   ValuesInterval GetValuesInterval(int num_hori_lines) const
00202   {
00203     ValuesInterval current_interval;
00204     current_interval.highest = INT64_MIN;
00205     current_interval.lowest  = INT64_MAX;
00206 
00207     for (int i = 0; i < this->num_dataset; i++) {
00208       if (HasBit(this->excluded_data, i)) continue;
00209       for (int j = 0; j < this->num_on_x_axis; j++) {
00210         OverflowSafeInt64 datapoint = this->cost[i][j];
00211 
00212         if (datapoint != INVALID_DATAPOINT) {
00213           current_interval.highest = max(current_interval.highest, datapoint);
00214           current_interval.lowest  = min(current_interval.lowest, datapoint);
00215         }
00216       }
00217     }
00218 
00219     /* Prevent showing values too close to the graph limits. */
00220     current_interval.highest = (11 * current_interval.highest) / 10;
00221     current_interval.lowest =  (11 * current_interval.lowest) / 10;
00222 
00223     /* Always include zero in the shown range. */
00224     double abs_lower  = (current_interval.lowest > 0) ? 0 : (double)abs(current_interval.lowest);
00225     double abs_higher = (current_interval.highest < 0) ? 0 : (double)current_interval.highest;
00226 
00227     int num_pos_grids;
00228     int64 grid_size;
00229 
00230     if (abs_lower != 0 || abs_higher != 0) {
00231       /* The number of grids to reserve for the positive part is: */
00232       num_pos_grids = (int)floor(0.5 + num_hori_lines * abs_higher / (abs_higher + abs_lower));
00233 
00234       /* If there are any positive or negative values, force that they have at least one grid. */
00235       if (num_pos_grids == 0 && abs_higher != 0) num_pos_grids++;
00236       if (num_pos_grids == num_hori_lines && abs_lower != 0) num_pos_grids--;
00237 
00238       /* Get the required grid size for each side and use the maximum one. */
00239       int64 grid_size_higher = (abs_higher > 0) ? ((int64)abs_higher + num_pos_grids - 1) / num_pos_grids : 0;
00240       int64 grid_size_lower = (abs_lower > 0) ? ((int64)abs_lower + num_hori_lines - num_pos_grids - 1) / (num_hori_lines - num_pos_grids) : 0;
00241       grid_size = max(grid_size_higher, grid_size_lower);
00242     } else {
00243       /* If both values are zero, show an empty graph. */
00244       num_pos_grids = num_hori_lines / 2;
00245       grid_size = 1;
00246     }
00247 
00248     current_interval.highest = num_pos_grids * grid_size;
00249     current_interval.lowest = -(num_hori_lines - num_pos_grids) * grid_size;
00250     return current_interval;
00251   }
00252 
00258   uint GetYLabelWidth(ValuesInterval current_interval, int num_hori_lines) const
00259   {
00260     /* draw text strings on the y axis */
00261     int64 y_label = current_interval.highest;
00262     int64 y_label_separation = (current_interval.highest - current_interval.lowest) / num_hori_lines;
00263 
00264     uint max_width = 0;
00265 
00266     for (int i = 0; i < (num_hori_lines + 1); i++) {
00267       SetDParam(0, this->format_str_y_axis);
00268       SetDParam(1, y_label);
00269       Dimension d = GetStringBoundingBox(STR_GRAPH_Y_LABEL);
00270       if (d.width > max_width) max_width = d.width;
00271 
00272       y_label -= y_label_separation;
00273     }
00274 
00275     return max_width;
00276   }
00277 
00282   void DrawGraph(Rect r) const
00283   {
00284     uint x, y;               
00285     ValuesInterval interval; 
00286     int x_axis_offset;       
00287 
00288     /* the colours and cost array of GraphDrawer must accommodate
00289      * both values for cargo and companies. So if any are higher, quit */
00290     assert_compile(GRAPH_MAX_DATASETS >= (int)NUM_CARGO && GRAPH_MAX_DATASETS >= (int)MAX_COMPANIES);
00291     assert(this->num_vert_lines > 0);
00292 
00293     byte grid_colour = _colour_gradient[COLOUR_GREY][4];
00294 
00295     /* Rect r will be adjusted to contain just the graph, with labels being
00296      * placed outside the area. */
00297     r.top    += 5 + GetCharacterHeight(FS_SMALL) / 2;
00298     r.bottom -= (this->month == 0xFF ? 1 : 3) * GetCharacterHeight(FS_SMALL) + 4;
00299     r.left   += 9;
00300     r.right  -= 5;
00301 
00302     /* Initial number of horizontal lines. */
00303     int num_hori_lines = 160 / MIN_GRID_PIXEL_SIZE;
00304     /* For the rest of the height, the number of horizontal lines will increase more slowly. */
00305     int resize = (r.bottom - r.top - 160) / (2 * MIN_GRID_PIXEL_SIZE);
00306     if (resize > 0) num_hori_lines += resize;
00307 
00308     interval = GetValuesInterval(num_hori_lines);
00309 
00310     int label_width = GetYLabelWidth(interval, num_hori_lines);
00311 
00312     r.left += label_width;
00313 
00314     int x_sep = (r.right - r.left) / this->num_vert_lines;
00315     int y_sep = (r.bottom - r.top) / num_hori_lines;
00316 
00317     /* Redetermine right and bottom edge of graph to fit with the integer
00318      * separation values. */
00319     r.right = r.left + x_sep * this->num_vert_lines;
00320     r.bottom = r.top + y_sep * num_hori_lines;
00321 
00322     OverflowSafeInt64 interval_size = interval.highest + abs(interval.lowest);
00323     /* Where to draw the X axis. Use floating point to avoid overflowing and results of zero. */
00324     x_axis_offset = (int)((r.bottom - r.top) * (double)interval.highest / (double)interval_size);
00325 
00326     /* Draw the vertical grid lines. */
00327 
00328     /* Don't draw the first line, as that's where the axis will be. */
00329     x = r.left + x_sep;
00330 
00331     for (int i = 0; i < this->num_vert_lines; i++) {
00332       GfxFillRect(x, r.top, x, r.bottom, grid_colour);
00333       x += x_sep;
00334     }
00335 
00336     /* Draw the horizontal grid lines. */
00337     y = r.bottom;
00338 
00339     for (int i = 0; i < (num_hori_lines + 1); i++) {
00340       GfxFillRect(r.left - 3, y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
00341       GfxFillRect(r.left, y, r.right, y, grid_colour);
00342       y -= y_sep;
00343     }
00344 
00345     /* Draw the y axis. */
00346     GfxFillRect(r.left, r.top, r.left, r.bottom, GRAPH_AXIS_LINE_COLOUR);
00347 
00348     /* Draw the x axis. */
00349     y = x_axis_offset + r.top;
00350     GfxFillRect(r.left, y, r.right, y, GRAPH_AXIS_LINE_COLOUR);
00351 
00352     /* Find the largest value that will be drawn. */
00353     if (this->num_on_x_axis == 0) return;
00354 
00355     assert(this->num_on_x_axis > 0);
00356     assert(this->num_dataset > 0);
00357 
00358     /* draw text strings on the y axis */
00359     int64 y_label = interval.highest;
00360     int64 y_label_separation = abs(interval.highest - interval.lowest) / num_hori_lines;
00361 
00362     y = r.top - GetCharacterHeight(FS_SMALL) / 2;
00363 
00364     for (int i = 0; i < (num_hori_lines + 1); i++) {
00365       SetDParam(0, this->format_str_y_axis);
00366       SetDParam(1, y_label);
00367       DrawString(r.left - label_width - 4, r.left - 4, y, STR_GRAPH_Y_LABEL, graph_axis_label_colour, SA_RIGHT);
00368 
00369       y_label -= y_label_separation;
00370       y += y_sep;
00371     }
00372 
00373     /* draw strings on the x axis */
00374     if (this->month != 0xFF) {
00375       x = r.left;
00376       y = r.bottom + 2;
00377       byte month = this->month;
00378       Year year  = this->year;
00379       for (int i = 0; i < this->num_on_x_axis; i++) {
00380         SetDParam(0, month + STR_MONTH_ABBREV_JAN);
00381         SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
00382         SetDParam(2, year);
00383         DrawStringMultiLine(x, x + x_sep, y, this->height, month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, graph_axis_label_colour);
00384 
00385         month += 3;
00386         if (month >= 12) {
00387           month = 0;
00388           year++;
00389         }
00390         x += x_sep;
00391       }
00392     } else {
00393       /* Draw the label under the data point rather than on the grid line. */
00394       x = r.left;
00395       y = r.bottom + 2;
00396       uint16 label = this->x_values_start;
00397 
00398       for (int i = 0; i < this->num_on_x_axis; i++) {
00399         SetDParam(0, label);
00400         DrawString(x + 1, x + x_sep - 1, y, STR_GRAPH_Y_LABEL_NUMBER, graph_axis_label_colour, SA_HOR_CENTER);
00401 
00402         label += this->x_values_increment;
00403         x += x_sep;
00404       }
00405     }
00406 
00407     /* draw lines and dots */
00408     uint linewidth = _settings_client.gui.graph_line_thickness;
00409     uint pointoffs1 = (linewidth + 1) / 2;
00410     uint pointoffs2 = linewidth + 1 - pointoffs1;
00411     for (int i = 0; i < this->num_dataset; i++) {
00412       if (!HasBit(this->excluded_data, i)) {
00413         /* Centre the dot between the grid lines. */
00414         x = r.left + (x_sep / 2);
00415 
00416         byte colour  = this->colours[i];
00417         uint prev_x = INVALID_DATAPOINT_POS;
00418         uint prev_y = INVALID_DATAPOINT_POS;
00419 
00420         for (int j = 0; j < this->num_on_x_axis; j++) {
00421           OverflowSafeInt64 datapoint = this->cost[i][j];
00422 
00423           if (datapoint != INVALID_DATAPOINT) {
00424             /*
00425              * Check whether we need to reduce the 'accuracy' of the
00426              * datapoint value and the highest value to split overflows.
00427              * And when 'drawing' 'one million' or 'one million and one'
00428              * there is no significant difference, so the least
00429              * significant bits can just be removed.
00430              *
00431              * If there are more bits needed than would fit in a 32 bits
00432              * integer, so at about 31 bits because of the sign bit, the
00433              * least significant bits are removed.
00434              */
00435             int mult_range = FindLastBit(x_axis_offset) + FindLastBit(abs(datapoint));
00436             int reduce_range = max(mult_range - 31, 0);
00437 
00438             /* Handle negative values differently (don't shift sign) */
00439             if (datapoint < 0) {
00440               datapoint = -(abs(datapoint) >> reduce_range);
00441             } else {
00442               datapoint >>= reduce_range;
00443             }
00444             y = r.top + x_axis_offset - ((r.bottom - r.top) * datapoint) / (interval_size >> reduce_range);
00445 
00446             /* Draw the point. */
00447             GfxFillRect(x - pointoffs1, y - pointoffs1, x + pointoffs2, y + pointoffs2, colour);
00448 
00449             /* Draw the line connected to the previous point. */
00450             if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, colour, linewidth);
00451 
00452             prev_x = x;
00453             prev_y = y;
00454           } else {
00455             prev_x = INVALID_DATAPOINT_POS;
00456             prev_y = INVALID_DATAPOINT_POS;
00457           }
00458 
00459           x += x_sep;
00460         }
00461       }
00462     }
00463   }
00464 
00465 
00466   BaseGraphWindow(WindowDesc *desc, int widget, StringID format_str_y_axis) :
00467       Window(desc),
00468       format_str_y_axis(format_str_y_axis)
00469   {
00470     SetWindowDirty(WC_GRAPH_LEGEND, 0);
00471     this->num_vert_lines = 24;
00472     this->graph_widget = widget;
00473   }
00474 
00475   void InitializeWindow(WindowNumber number)
00476   {
00477     /* Initialise the dataset */
00478     this->UpdateStatistics(true);
00479 
00480     this->InitNested(number);
00481   }
00482 
00483 public:
00484   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00485   {
00486     if (widget != this->graph_widget) return;
00487 
00488     uint x_label_width = 0;
00489 
00490     if (this->month != 0xFF) {
00491       byte month = this->month;
00492       Year year  = this->year;
00493       for (int i = 0; i < this->num_on_x_axis; i++) {
00494         SetDParam(0, month + STR_MONTH_ABBREV_JAN);
00495         SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
00496         SetDParam(2, year);
00497         x_label_width = max(x_label_width, GetStringBoundingBox(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH).width);
00498 
00499         month += 3;
00500         if (month >= 12) {
00501           month = 0;
00502           year++;
00503         }
00504       }
00505     } else {
00506       /* Draw the label under the data point rather than on the grid line. */
00507       SetDParamMaxValue(0, this->x_values_start + this->num_on_x_axis * this->x_values_increment);
00508       x_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL_NUMBER).width;
00509     }
00510 
00511     SetDParam(0, this->format_str_y_axis);
00512     SetDParam(1, INT64_MAX);
00513     uint y_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL).width;
00514 
00515     size->width  = max<uint>(size->width,  5 + y_label_width + this->num_on_x_axis * (x_label_width + 5) + 9);
00516     size->height = max<uint>(size->height, 5 + (1 + MIN_GRAPH_NUM_LINES_Y * 2 + (this->month != 0xFF ? 3 : 1)) * FONT_HEIGHT_SMALL + 4);
00517     size->height = max<uint>(size->height, size->width / 3);
00518   }
00519 
00520   virtual void DrawWidget(const Rect &r, int widget) const
00521   {
00522     if (widget != this->graph_widget) return;
00523 
00524     DrawGraph(r);
00525   }
00526 
00527   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00528   {
00529     return INVALID_DATAPOINT;
00530   }
00531 
00532   virtual void OnClick(Point pt, int widget, int click_count)
00533   {
00534     /* Clicked on legend? */
00535     if (widget == WID_CV_KEY_BUTTON) ShowGraphLegend();
00536   }
00537 
00538   virtual void OnTick()
00539   {
00540     this->UpdateStatistics(false);
00541   }
00542 
00548   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00549   {
00550     if (!gui_scope) return;
00551     this->UpdateStatistics(true);
00552   }
00553 
00558   void UpdateStatistics(bool initialize)
00559   {
00560     uint excluded_companies = _legend_excluded_companies;
00561 
00562     /* Exclude the companies which aren't valid */
00563     for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00564       if (!Company::IsValidID(c)) SetBit(excluded_companies, c);
00565     }
00566 
00567     byte nums = 0;
00568     const Company *c;
00569     FOR_ALL_COMPANIES(c) {
00570       nums = min(this->num_vert_lines, max(nums, c->num_valid_stat_ent));
00571     }
00572 
00573     int mo = (_cur_month / 3 - nums) * 3;
00574     int yr = _cur_year;
00575     while (mo < 0) {
00576       yr--;
00577       mo += 12;
00578     }
00579 
00580     if (!initialize && this->excluded_data == excluded_companies && this->num_on_x_axis == nums &&
00581         this->year == yr && this->month == mo) {
00582       /* There's no reason to get new stats */
00583       return;
00584     }
00585 
00586     this->excluded_data = excluded_companies;
00587     this->num_on_x_axis = nums;
00588     this->year = yr;
00589     this->month = mo;
00590 
00591     int numd = 0;
00592     for (CompanyID k = COMPANY_FIRST; k < MAX_COMPANIES; k++) {
00593       c = Company::GetIfValid(k);
00594       if (c != NULL) {
00595         this->colours[numd] = _colour_gradient[c->colour][6];
00596         for (int j = this->num_on_x_axis, i = 0; --j >= 0;) {
00597           this->cost[numd][i] = (j >= c->num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j);
00598           i++;
00599         }
00600       }
00601       numd++;
00602     }
00603 
00604     this->num_dataset = numd;
00605   }
00606 };
00607 
00608 
00609 /********************/
00610 /* OPERATING PROFIT */
00611 /********************/
00612 
00613 struct OperatingProfitGraphWindow : BaseGraphWindow {
00614   OperatingProfitGraphWindow(WindowDesc *desc, WindowNumber window_number) :
00615       BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_CURRENCY_SHORT)
00616   {
00617     this->InitializeWindow(window_number);
00618   }
00619 
00620   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00621   {
00622     return c->old_economy[j].income + c->old_economy[j].expenses;
00623   }
00624 };
00625 
00626 static const NWidgetPart _nested_operating_profit_widgets[] = {
00627   NWidget(NWID_HORIZONTAL),
00628     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00629     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_OPERATING_PROFIT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00630     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00631     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00632     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
00633     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00634   EndContainer(),
00635   NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
00636     NWidget(NWID_HORIZONTAL),
00637       NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
00638       NWidget(NWID_VERTICAL),
00639         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00640         NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
00641       EndContainer(),
00642     EndContainer(),
00643   EndContainer(),
00644 };
00645 
00646 static WindowDesc _operating_profit_desc(
00647   WDP_AUTO, "graph_operating_profit", 0, 0,
00648   WC_OPERATING_PROFIT, WC_NONE,
00649   0,
00650   _nested_operating_profit_widgets, lengthof(_nested_operating_profit_widgets)
00651 );
00652 
00653 
00654 void ShowOperatingProfitGraph()
00655 {
00656   AllocateWindowDescFront<OperatingProfitGraphWindow>(&_operating_profit_desc, 0);
00657 }
00658 
00659 
00660 /****************/
00661 /* INCOME GRAPH */
00662 /****************/
00663 
00664 struct IncomeGraphWindow : BaseGraphWindow {
00665   IncomeGraphWindow(WindowDesc *desc, WindowNumber window_number) :
00666       BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_CURRENCY_SHORT)
00667   {
00668     this->InitializeWindow(window_number);
00669   }
00670 
00671   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00672   {
00673     return c->old_economy[j].income;
00674   }
00675 };
00676 
00677 static const NWidgetPart _nested_income_graph_widgets[] = {
00678   NWidget(NWID_HORIZONTAL),
00679     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00680     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_INCOME_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00681     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00682     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00683     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
00684     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00685   EndContainer(),
00686   NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
00687     NWidget(NWID_HORIZONTAL),
00688       NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
00689       NWidget(NWID_VERTICAL),
00690         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00691         NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
00692       EndContainer(),
00693     EndContainer(),
00694   EndContainer(),
00695 };
00696 
00697 static WindowDesc _income_graph_desc(
00698   WDP_AUTO, "graph_income", 0, 0,
00699   WC_INCOME_GRAPH, WC_NONE,
00700   0,
00701   _nested_income_graph_widgets, lengthof(_nested_income_graph_widgets)
00702 );
00703 
00704 void ShowIncomeGraph()
00705 {
00706   AllocateWindowDescFront<IncomeGraphWindow>(&_income_graph_desc, 0);
00707 }
00708 
00709 /*******************/
00710 /* DELIVERED CARGO */
00711 /*******************/
00712 
00713 struct DeliveredCargoGraphWindow : BaseGraphWindow {
00714   DeliveredCargoGraphWindow(WindowDesc *desc, WindowNumber window_number) :
00715       BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_COMMA)
00716   {
00717     this->InitializeWindow(window_number);
00718   }
00719 
00720   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00721   {
00722     return c->old_economy[j].delivered_cargo.GetSum<OverflowSafeInt64>();
00723   }
00724 };
00725 
00726 static const NWidgetPart _nested_delivered_cargo_graph_widgets[] = {
00727   NWidget(NWID_HORIZONTAL),
00728     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00729     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_DELIVERED_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00730     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00731     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00732     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
00733     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00734   EndContainer(),
00735   NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
00736     NWidget(NWID_HORIZONTAL),
00737       NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
00738       NWidget(NWID_VERTICAL),
00739         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00740         NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
00741       EndContainer(),
00742     EndContainer(),
00743   EndContainer(),
00744 };
00745 
00746 static WindowDesc _delivered_cargo_graph_desc(
00747   WDP_AUTO, "graph_delivered_cargo", 0, 0,
00748   WC_DELIVERED_CARGO, WC_NONE,
00749   0,
00750   _nested_delivered_cargo_graph_widgets, lengthof(_nested_delivered_cargo_graph_widgets)
00751 );
00752 
00753 void ShowDeliveredCargoGraph()
00754 {
00755   AllocateWindowDescFront<DeliveredCargoGraphWindow>(&_delivered_cargo_graph_desc, 0);
00756 }
00757 
00758 /***********************/
00759 /* PERFORMANCE HISTORY */
00760 /***********************/
00761 
00762 struct PerformanceHistoryGraphWindow : BaseGraphWindow {
00763   PerformanceHistoryGraphWindow(WindowDesc *desc, WindowNumber window_number) :
00764       BaseGraphWindow(desc, WID_PHG_GRAPH, STR_JUST_COMMA)
00765   {
00766     this->InitializeWindow(window_number);
00767   }
00768 
00769   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00770   {
00771     return c->old_economy[j].performance_history;
00772   }
00773 
00774   virtual void OnClick(Point pt, int widget, int click_count)
00775   {
00776     if (widget == WID_PHG_DETAILED_PERFORMANCE) ShowPerformanceRatingDetail();
00777     this->BaseGraphWindow::OnClick(pt, widget, click_count);
00778   }
00779 };
00780 
00781 static const NWidgetPart _nested_performance_history_widgets[] = {
00782   NWidget(NWID_HORIZONTAL),
00783     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00784     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00785     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PHG_DETAILED_PERFORMANCE), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_PERFORMANCE_DETAIL_KEY, STR_GRAPH_PERFORMANCE_DETAIL_TOOLTIP),
00786     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PHG_KEY), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00787     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00788     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
00789     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00790   EndContainer(),
00791   NWidget(WWT_PANEL, COLOUR_GREY, WID_PHG_BACKGROUND),
00792     NWidget(NWID_HORIZONTAL),
00793       NWidget(WWT_EMPTY, COLOUR_GREY, WID_PHG_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
00794       NWidget(NWID_VERTICAL),
00795         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00796         NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_PHG_RESIZE),
00797       EndContainer(),
00798     EndContainer(),
00799   EndContainer(),
00800 };
00801 
00802 static WindowDesc _performance_history_desc(
00803   WDP_AUTO, "graph_performance", 0, 0,
00804   WC_PERFORMANCE_HISTORY, WC_NONE,
00805   0,
00806   _nested_performance_history_widgets, lengthof(_nested_performance_history_widgets)
00807 );
00808 
00809 void ShowPerformanceHistoryGraph()
00810 {
00811   AllocateWindowDescFront<PerformanceHistoryGraphWindow>(&_performance_history_desc, 0);
00812 }
00813 
00814 /*****************/
00815 /* COMPANY VALUE */
00816 /*****************/
00817 
00818 struct CompanyValueGraphWindow : BaseGraphWindow {
00819   CompanyValueGraphWindow(WindowDesc *desc, WindowNumber window_number) :
00820       BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_CURRENCY_SHORT)
00821   {
00822     this->InitializeWindow(window_number);
00823   }
00824 
00825   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00826   {
00827     return c->old_economy[j].company_value;
00828   }
00829 };
00830 
00831 static const NWidgetPart _nested_company_value_graph_widgets[] = {
00832   NWidget(NWID_HORIZONTAL),
00833     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00834     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_VALUES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00835     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00836     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00837     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
00838     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00839   EndContainer(),
00840   NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
00841     NWidget(NWID_HORIZONTAL),
00842       NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
00843       NWidget(NWID_VERTICAL),
00844         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00845         NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
00846       EndContainer(),
00847     EndContainer(),
00848   EndContainer(),
00849 };
00850 
00851 static WindowDesc _company_value_graph_desc(
00852   WDP_AUTO, "graph_company_value", 0, 0,
00853   WC_COMPANY_VALUE, WC_NONE,
00854   0,
00855   _nested_company_value_graph_widgets, lengthof(_nested_company_value_graph_widgets)
00856 );
00857 
00858 void ShowCompanyValueGraph()
00859 {
00860   AllocateWindowDescFront<CompanyValueGraphWindow>(&_company_value_graph_desc, 0);
00861 }
00862 
00863 /*****************/
00864 /* PAYMENT RATES */
00865 /*****************/
00866 
00867 struct PaymentRatesGraphWindow : BaseGraphWindow {
00868   bool first_init; 
00869   PaymentRatesGraphWindow(WindowDesc *desc, WindowNumber window_number) :
00870       BaseGraphWindow(desc, WID_CPR_GRAPH, STR_JUST_CURRENCY_SHORT)
00871   {
00872     this->first_init = true;
00873     this->num_on_x_axis = 20;
00874     this->num_vert_lines = 20;
00875     this->month = 0xFF;
00876     this->x_values_start     = 10;
00877     this->x_values_increment = 10;
00878 
00879     /* Initialise the dataset */
00880     this->OnHundredthTick();
00881 
00882     this->InitNested(window_number);
00883 
00884     this->UpdateLoweredWidgets();
00885   }
00886 
00887   virtual void OnInit()
00888   {
00889     /* UpdateLoweredWidgets needs to be called after a language or NewGRF change, but it can't be called before
00890      * InitNested is done. On the first init these functions are called in the correct order by the constructor. */
00891     if (!this->first_init) {
00892       /* Initialise the dataset */
00893       this->OnHundredthTick();
00894       this->UpdateLoweredWidgets();
00895     }
00896     this->first_init = false;
00897   }
00898 
00899   void UpdateExcludedData()
00900   {
00901     this->excluded_data = 0;
00902 
00903     int i = 0;
00904     const CargoSpec *cs;
00905     FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
00906       if (HasBit(_legend_excluded_cargo, cs->Index())) SetBit(this->excluded_data, i);
00907       i++;
00908     }
00909   }
00910 
00911   void UpdateLoweredWidgets()
00912   {
00913     for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
00914       this->SetWidgetLoweredState(WID_CPR_CARGO_FIRST + i, !HasBit(this->excluded_data, i));
00915     }
00916   }
00917 
00918   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00919   {
00920     if (widget < WID_CPR_CARGO_FIRST) {
00921       BaseGraphWindow::UpdateWidgetSize(widget, size, padding, fill, resize);
00922       return;
00923     }
00924 
00925     const CargoSpec *cs = _sorted_cargo_specs[widget - WID_CPR_CARGO_FIRST];
00926     SetDParam(0, cs->name);
00927     Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO);
00928     d.width += 14; // colour field
00929     d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00930     d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00931     *size = maxdim(d, *size);
00932   }
00933 
00934   virtual void DrawWidget(const Rect &r, int widget) const
00935   {
00936     if (widget < WID_CPR_CARGO_FIRST) {
00937       BaseGraphWindow::DrawWidget(r, widget);
00938       return;
00939     }
00940 
00941     const CargoSpec *cs = _sorted_cargo_specs[widget - WID_CPR_CARGO_FIRST];
00942     bool rtl = _current_text_dir == TD_RTL;
00943 
00944     /* Since the buttons have no text, no images,
00945      * both the text and the coloured box have to be manually painted.
00946      * clk_dif will move one pixel down and one pixel to the right
00947      * when the button is clicked */
00948     byte clk_dif = this->IsWidgetLowered(widget) ? 1 : 0;
00949     int x = r.left + WD_FRAMERECT_LEFT;
00950     int y = r.top;
00951 
00952     int rect_x = clk_dif + (rtl ? r.right - 12 : r.left + WD_FRAMERECT_LEFT);
00953 
00954     GfxFillRect(rect_x, y + clk_dif, rect_x + 8, y + 5 + clk_dif, PC_BLACK);
00955     GfxFillRect(rect_x + 1, y + 1 + clk_dif, rect_x + 7, y + 4 + clk_dif, cs->legend_colour);
00956     SetDParam(0, cs->name);
00957     DrawString(rtl ? r.left : x + 14 + clk_dif, (rtl ? r.right - 14 + clk_dif : r.right), y + clk_dif, STR_GRAPH_CARGO_PAYMENT_CARGO);
00958   }
00959 
00960   virtual void OnClick(Point pt, int widget, int click_count)
00961   {
00962     switch (widget) {
00963       case WID_CPR_ENABLE_CARGOES:
00964         /* Remove all cargoes from the excluded lists. */
00965         _legend_excluded_cargo = 0;
00966         this->excluded_data = 0;
00967         this->UpdateLoweredWidgets();
00968         this->SetDirty();
00969         break;
00970 
00971       case WID_CPR_DISABLE_CARGOES: {
00972         /* Add all cargoes to the excluded lists. */
00973         int i = 0;
00974         const CargoSpec *cs;
00975         FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
00976           SetBit(_legend_excluded_cargo, cs->Index());
00977           SetBit(this->excluded_data, i);
00978           i++;
00979         }
00980         this->UpdateLoweredWidgets();
00981         this->SetDirty();
00982         break;
00983       }
00984 
00985       default:
00986         if (widget >= WID_CPR_CARGO_FIRST) {
00987           int i = widget - WID_CPR_CARGO_FIRST;
00988           ToggleBit(_legend_excluded_cargo, _sorted_cargo_specs[i]->Index());
00989           this->ToggleWidgetLoweredState(widget);
00990           this->UpdateExcludedData();
00991           this->SetDirty();
00992         }
00993         break;
00994     }
00995   }
00996 
00997   virtual void OnTick()
00998   {
00999     /* Override default OnTick */
01000   }
01001 
01007   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01008   {
01009     if (!gui_scope) return;
01010     this->OnHundredthTick();
01011   }
01012 
01013   virtual void OnHundredthTick()
01014   {
01015     this->UpdateExcludedData();
01016 
01017     int i = 0;
01018     const CargoSpec *cs;
01019     FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
01020       this->colours[i] = cs->legend_colour;
01021       for (uint j = 0; j != 20; j++) {
01022         this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index());
01023       }
01024       i++;
01025     }
01026     this->num_dataset = i;
01027   }
01028 };
01029 
01031 static NWidgetBase *MakeCargoButtons(int *biggest_index)
01032 {
01033   NWidgetVertical *ver = new NWidgetVertical;
01034 
01035   for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
01036     NWidgetBackground *leaf = new NWidgetBackground(WWT_PANEL, COLOUR_ORANGE, WID_CPR_CARGO_FIRST + i, NULL);
01037     leaf->tool_tip = STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO;
01038     leaf->SetFill(1, 0);
01039     leaf->SetLowered(true);
01040     ver->Add(leaf);
01041   }
01042   *biggest_index = WID_CPR_CARGO_FIRST + _sorted_standard_cargo_specs_size - 1;
01043   return ver;
01044 }
01045 
01046 
01047 static const NWidgetPart _nested_cargo_payment_rates_widgets[] = {
01048   NWidget(NWID_HORIZONTAL),
01049     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01050     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01051     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01052     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
01053     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01054   EndContainer(),
01055   NWidget(WWT_PANEL, COLOUR_GREY, WID_CPR_BACKGROUND), SetMinimalSize(568, 128),
01056     NWidget(NWID_HORIZONTAL),
01057       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01058       NWidget(WWT_TEXT, COLOUR_GREY, WID_CPR_HEADER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE, STR_NULL),
01059       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01060     EndContainer(),
01061     NWidget(NWID_HORIZONTAL),
01062       NWidget(WWT_EMPTY, COLOUR_GREY, WID_CPR_GRAPH), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
01063       NWidget(NWID_VERTICAL),
01064         NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 0), SetResize(0, 1),
01065         NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CPR_ENABLE_CARGOES), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL), SetFill(1, 0),
01066         NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CPR_DISABLE_CARGOES), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL), SetFill(1, 0),
01067         NWidget(NWID_SPACER), SetMinimalSize(0, 4),
01068         NWidgetFunction(MakeCargoButtons),
01069         NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1), SetResize(0, 1),
01070       EndContainer(),
01071       NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
01072     EndContainer(),
01073     NWidget(NWID_HORIZONTAL),
01074       NWidget(NWID_SPACER), SetMinimalSize(WD_RESIZEBOX_WIDTH, 0), SetFill(1, 0), SetResize(1, 0),
01075       NWidget(WWT_TEXT, COLOUR_GREY, WID_CPR_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_X_LABEL, STR_NULL),
01076       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01077       NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CPR_RESIZE),
01078     EndContainer(),
01079   EndContainer(),
01080 };
01081 
01082 static WindowDesc _cargo_payment_rates_desc(
01083   WDP_AUTO, "graph_cargo_payment_rates", 0, 0,
01084   WC_PAYMENT_RATES, WC_NONE,
01085   0,
01086   _nested_cargo_payment_rates_widgets, lengthof(_nested_cargo_payment_rates_widgets)
01087 );
01088 
01089 
01090 void ShowCargoPaymentRates()
01091 {
01092   AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
01093 }
01094 
01095 /************************/
01096 /* COMPANY LEAGUE TABLE */
01097 /************************/
01098 
01099 static const StringID _performance_titles[] = {
01100   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
01101   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
01102   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
01103   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
01104   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
01105   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
01106   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
01107   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
01108   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
01109   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
01110   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
01111   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
01112   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
01113   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
01114   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT,
01115   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON,
01116 };
01117 
01118 static inline StringID GetPerformanceTitleFromValue(uint value)
01119 {
01120   return _performance_titles[minu(value, 1000) >> 6];
01121 }
01122 
01123 class CompanyLeagueWindow : public Window {
01124 private:
01125   GUIList<const Company*> companies;
01126   uint ordinal_width; 
01127   uint text_width;    
01128   uint icon_width;    
01129   int line_height;    
01130 
01134   void BuildCompanyList()
01135   {
01136     if (!this->companies.NeedRebuild()) return;
01137 
01138     this->companies.Clear();
01139 
01140     const Company *c;
01141     FOR_ALL_COMPANIES(c) {
01142       *this->companies.Append() = c;
01143     }
01144 
01145     this->companies.Compact();
01146     this->companies.RebuildDone();
01147   }
01148 
01150   static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2)
01151   {
01152     return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history;
01153   }
01154 
01155 public:
01156   CompanyLeagueWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
01157   {
01158     this->InitNested(window_number);
01159     this->companies.ForceRebuild();
01160     this->companies.NeedResort();
01161   }
01162 
01163   virtual void OnPaint()
01164   {
01165     this->BuildCompanyList();
01166     this->companies.Sort(&PerformanceSorter);
01167 
01168     this->DrawWidgets();
01169   }
01170 
01171   virtual void DrawWidget(const Rect &r, int widget) const
01172   {
01173     if (widget != WID_CL_BACKGROUND) return;
01174 
01175     int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - this->line_height) / 2;
01176     uint y = r.top + WD_FRAMERECT_TOP - icon_y_offset;
01177 
01178     bool rtl = _current_text_dir == TD_RTL;
01179     uint ordinal_left  = rtl ? r.right - WD_FRAMERECT_LEFT - this->ordinal_width : r.left + WD_FRAMERECT_LEFT;
01180     uint ordinal_right = rtl ? r.right - WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->ordinal_width;
01181     uint icon_left     = r.left + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + (rtl ? this->text_width : this->ordinal_width);
01182     uint text_left     = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_LEFT - this->text_width;
01183     uint text_right    = rtl ? r.left + WD_FRAMERECT_LEFT + this->text_width : r.right - WD_FRAMERECT_LEFT;
01184 
01185     for (uint i = 0; i != this->companies.Length(); i++) {
01186       const Company *c = this->companies[i];
01187       DrawString(ordinal_left, ordinal_right, y, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW);
01188 
01189       DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
01190 
01191       SetDParam(0, c->index);
01192       SetDParam(1, c->index);
01193       SetDParam(2, GetPerformanceTitleFromValue(c->old_economy[0].performance_history));
01194       DrawString(text_left, text_right, y, STR_COMPANY_LEAGUE_COMPANY_NAME);
01195       y += this->line_height;
01196     }
01197   }
01198 
01199   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01200   {
01201     if (widget != WID_CL_BACKGROUND) return;
01202 
01203     this->ordinal_width = 0;
01204     for (uint i = 0; i < MAX_COMPANIES; i++) {
01205       this->ordinal_width = max(this->ordinal_width, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST + i).width);
01206     }
01207     this->ordinal_width += 5; // Keep some extra spacing
01208 
01209     uint widest_width = 0;
01210     uint widest_title = 0;
01211     for (uint i = 0; i < lengthof(_performance_titles); i++) {
01212       uint width = GetStringBoundingBox(_performance_titles[i]).width;
01213       if (width > widest_width) {
01214         widest_title = i;
01215         widest_width = width;
01216       }
01217     }
01218 
01219     Dimension d = GetSpriteSize(SPR_COMPANY_ICON);
01220     this->icon_width = d.width + 2;
01221     this->line_height = max<int>(d.height + 2, FONT_HEIGHT_NORMAL);
01222 
01223     const Company *c;
01224     FOR_ALL_COMPANIES(c) {
01225       SetDParam(0, c->index);
01226       SetDParam(1, c->index);
01227       SetDParam(2, _performance_titles[widest_title]);
01228       widest_width = max(widest_width, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME).width);
01229     }
01230 
01231     this->text_width = widest_width + 30; // Keep some extra spacing
01232 
01233     size->width = WD_FRAMERECT_LEFT + this->ordinal_width + WD_FRAMERECT_RIGHT + this->icon_width + WD_FRAMERECT_LEFT + this->text_width + WD_FRAMERECT_RIGHT;
01234     size->height = WD_FRAMERECT_TOP + this->line_height * MAX_COMPANIES + WD_FRAMERECT_BOTTOM;
01235   }
01236 
01237 
01238   virtual void OnTick()
01239   {
01240     if (this->companies.NeedResort()) {
01241       this->SetDirty();
01242     }
01243   }
01244 
01250   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01251   {
01252     if (data == 0) {
01253       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
01254       this->companies.ForceRebuild();
01255     } else {
01256       this->companies.ForceResort();
01257     }
01258   }
01259 };
01260 
01261 static const NWidgetPart _nested_company_league_widgets[] = {
01262   NWidget(NWID_HORIZONTAL),
01263     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01264     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01265     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01266     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01267   EndContainer(),
01268   NWidget(WWT_PANEL, COLOUR_GREY, WID_CL_BACKGROUND), SetMinimalSize(400, 0), SetMinimalTextLines(15, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM),
01269 };
01270 
01271 static WindowDesc _company_league_desc(
01272   WDP_AUTO, "league", 0, 0,
01273   WC_COMPANY_LEAGUE, WC_NONE,
01274   0,
01275   _nested_company_league_widgets, lengthof(_nested_company_league_widgets)
01276 );
01277 
01278 void ShowCompanyLeagueTable()
01279 {
01280   AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
01281 }
01282 
01283 /*****************************/
01284 /* PERFORMANCE RATING DETAIL */
01285 /*****************************/
01286 
01287 struct PerformanceRatingDetailWindow : Window {
01288   static CompanyID company;
01289   int timeout;
01290 
01291   PerformanceRatingDetailWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
01292   {
01293     this->UpdateCompanyStats();
01294 
01295     this->InitNested(window_number);
01296     this->OnInvalidateData(INVALID_COMPANY);
01297   }
01298 
01299   void UpdateCompanyStats()
01300   {
01301     /* Update all company stats with the current data
01302      * (this is because _score_info is not saved to a savegame) */
01303     Company *c;
01304     FOR_ALL_COMPANIES(c) {
01305       UpdateCompanyRatingAndValue(c, false);
01306     }
01307 
01308     this->timeout = DAY_TICKS * 5;
01309   }
01310 
01311   uint score_info_left;
01312   uint score_info_right;
01313   uint bar_left;
01314   uint bar_right;
01315   uint bar_width;
01316   uint bar_height;
01317   uint score_detail_left;
01318   uint score_detail_right;
01319 
01320   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01321   {
01322     switch (widget) {
01323       case WID_PRD_SCORE_FIRST:
01324         this->bar_height = FONT_HEIGHT_NORMAL + 4;
01325         size->height = this->bar_height + 2 * WD_MATRIX_TOP;
01326 
01327         uint score_info_width = 0;
01328         for (uint i = SCORE_BEGIN; i < SCORE_END; i++) {
01329           score_info_width = max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + i).width);
01330         }
01331         SetDParamMaxValue(0, 1000);
01332         score_info_width += GetStringBoundingBox(STR_BLACK_COMMA).width + WD_FRAMERECT_LEFT;
01333 
01334         SetDParamMaxValue(0, 100);
01335         this->bar_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT).width + 20; // Wide bars!
01336 
01337         /* At this number we are roughly at the max; it can become wider,
01338          * but then you need at 1000 times more money. At that time you're
01339          * not that interested anymore in the last few digits anyway.
01340          * The 500 is because 999 999 500 to 999 999 999 are rounded to
01341          * 1 000 M, and not 999 999 k. Use negative numbers to account for
01342          * the negative income/amount of money etc. as well. */
01343         int max = -(999999999 - 500);
01344 
01345         /* Scale max for the display currency. Prior to rendering the value
01346          * is converted into the display currency, which may cause it to
01347          * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
01348          * is used, which can produce quite short renderings of very large
01349          * values. Otherwise the calculated width could be too narrow.
01350          * Note that it doesn't work if there was a currency with an exchange
01351          * rate greater than max.
01352          * When the currency rate is more than 1000, the 999 999 k becomes at
01353          * least 999 999 M which roughly is equally long. Furthermore if the
01354          * exchange rate is that high, 999 999 k is usually not enough anymore
01355          * to show the different currency numbers. */
01356         if (_currency->rate < 1000) max /= _currency->rate;
01357         SetDParam(0, max);
01358         SetDParam(1, max);
01359         uint score_detail_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY).width;
01360 
01361         size->width = 7 + score_info_width + 5 + this->bar_width + 5 + score_detail_width + 7;
01362         uint left  = 7;
01363         uint right = size->width - 7;
01364 
01365         bool rtl = _current_text_dir == TD_RTL;
01366         this->score_info_left  = rtl ? right - score_info_width : left;
01367         this->score_info_right = rtl ? right : left + score_info_width;
01368 
01369         this->score_detail_left  = rtl ? left : right - score_detail_width;
01370         this->score_detail_right = rtl ? left + score_detail_width : right;
01371 
01372         this->bar_left  = left + (rtl ? score_detail_width : score_info_width) + 5;
01373         this->bar_right = this->bar_left + this->bar_width;
01374         break;
01375     }
01376   }
01377 
01378   virtual void DrawWidget(const Rect &r, int widget) const
01379   {
01380     /* No need to draw when there's nothing to draw */
01381     if (this->company == INVALID_COMPANY) return;
01382 
01383     if (IsInsideMM(widget, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST + 1)) {
01384       if (this->IsWidgetDisabled(widget)) return;
01385       CompanyID cid = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
01386       int offset = (cid == this->company) ? 1 : 0;
01387       Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
01388       DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
01389       return;
01390     }
01391 
01392     if (!IsInsideMM(widget, WID_PRD_SCORE_FIRST, WID_PRD_SCORE_LAST + 1)) return;
01393 
01394     ScoreID score_type = (ScoreID)(widget - WID_PRD_SCORE_FIRST);
01395 
01396     /* The colours used to show how the progress is going */
01397     int colour_done = _colour_gradient[COLOUR_GREEN][4];
01398     int colour_notdone = _colour_gradient[COLOUR_RED][4];
01399 
01400     /* Draw all the score parts */
01401     int val    = _score_part[company][score_type];
01402     int needed = _score_info[score_type].needed;
01403     int score  = _score_info[score_type].score;
01404 
01405     /* SCORE_TOTAL has his own rules ;) */
01406     if (score_type == SCORE_TOTAL) {
01407       for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
01408       needed = SCORE_MAX;
01409     }
01410 
01411     uint bar_top  = r.top + WD_MATRIX_TOP;
01412     uint text_top = bar_top + 2;
01413 
01414     DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
01415 
01416     /* Draw the score */
01417     SetDParam(0, score);
01418     DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING, SA_RIGHT);
01419 
01420     /* Calculate the %-bar */
01421     uint x = Clamp(val, 0, needed) * this->bar_width / needed;
01422     bool rtl = _current_text_dir == TD_RTL;
01423     if (rtl) {
01424       x = this->bar_right - x;
01425     } else {
01426       x = this->bar_left + x;
01427     }
01428 
01429     /* Draw the bar */
01430     if (x != this->bar_left)  GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
01431     if (x != this->bar_right) GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
01432 
01433     /* Draw it */
01434     SetDParam(0, Clamp(val, 0, needed) * 100 / needed);
01435     DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING, SA_HOR_CENTER);
01436 
01437     /* SCORE_LOAN is inversed */
01438     if (score_type == SCORE_LOAN) val = needed - val;
01439 
01440     /* Draw the amount we have against what is needed
01441      * For some of them it is in currency format */
01442     SetDParam(0, val);
01443     SetDParam(1, needed);
01444     switch (score_type) {
01445       case SCORE_MIN_PROFIT:
01446       case SCORE_MIN_INCOME:
01447       case SCORE_MAX_INCOME:
01448       case SCORE_MONEY:
01449       case SCORE_LOAN:
01450         DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
01451         break;
01452       default:
01453         DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
01454     }
01455   }
01456 
01457   virtual void OnClick(Point pt, int widget, int click_count)
01458   {
01459     /* Check which button is clicked */
01460     if (IsInsideMM(widget, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST + 1)) {
01461       /* Is it no on disable? */
01462       if (!this->IsWidgetDisabled(widget)) {
01463         this->RaiseWidget(this->company + WID_PRD_COMPANY_FIRST);
01464         this->company = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
01465         this->LowerWidget(this->company + WID_PRD_COMPANY_FIRST);
01466         this->SetDirty();
01467       }
01468     }
01469   }
01470 
01471   virtual void OnTick()
01472   {
01473     if (_pause_mode != PM_UNPAUSED) return;
01474 
01475     /* Update the company score every 5 days */
01476     if (--this->timeout == 0) {
01477       this->UpdateCompanyStats();
01478       this->SetDirty();
01479     }
01480   }
01481 
01487   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01488   {
01489     if (!gui_scope) return;
01490     /* Disable the companies who are not active */
01491     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01492       this->SetWidgetDisabledState(i + WID_PRD_COMPANY_FIRST, !Company::IsValidID(i));
01493     }
01494 
01495     /* Check if the currently selected company is still active. */
01496     if (this->company != INVALID_COMPANY && !Company::IsValidID(this->company)) {
01497       /* Raise the widget for the previous selection. */
01498       this->RaiseWidget(this->company + WID_PRD_COMPANY_FIRST);
01499       this->company = INVALID_COMPANY;
01500     }
01501 
01502     if (this->company == INVALID_COMPANY) {
01503       const Company *c;
01504       FOR_ALL_COMPANIES(c) {
01505         this->company = c->index;
01506         break;
01507       }
01508     }
01509 
01510     /* Make sure the widget is lowered */
01511     this->LowerWidget(this->company + WID_PRD_COMPANY_FIRST);
01512   }
01513 };
01514 
01515 CompanyID PerformanceRatingDetailWindow::company = INVALID_COMPANY;
01516 
01523 static NWidgetBase *MakePerformanceDetailPanels(int *biggest_index)
01524 {
01525   const StringID performance_tips[] = {
01526     STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP,
01527     STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
01528     STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP,
01529     STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
01530     STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
01531     STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
01532     STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
01533     STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
01534     STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
01535     STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
01536   };
01537 
01538   assert_compile(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
01539 
01540   NWidgetVertical *vert = new NWidgetVertical(NC_EQUALSIZE);
01541   for (int widnum = WID_PRD_SCORE_FIRST; widnum <= WID_PRD_SCORE_LAST; widnum++) {
01542     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
01543     panel->SetFill(1, 1);
01544     panel->SetDataTip(0x0, performance_tips[widnum - WID_PRD_SCORE_FIRST]);
01545     vert->Add(panel);
01546   }
01547   *biggest_index = WID_PRD_SCORE_LAST;
01548   return vert;
01549 }
01550 
01552 NWidgetBase *MakeCompanyButtonRowsGraphGUI(int *biggest_index)
01553 {
01554   return MakeCompanyButtonRows(biggest_index, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
01555 }
01556 
01557 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
01558   NWidget(NWID_HORIZONTAL),
01559     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01560     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PERFORMANCE_DETAIL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01561     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01562     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01563   EndContainer(),
01564   NWidget(WWT_PANEL, COLOUR_GREY),
01565     NWidgetFunction(MakeCompanyButtonRowsGraphGUI), SetPadding(0, 1, 1, 2),
01566   EndContainer(),
01567   NWidgetFunction(MakePerformanceDetailPanels),
01568 };
01569 
01570 static WindowDesc _performance_rating_detail_desc(
01571   WDP_AUTO, "league_details", 0, 0,
01572   WC_PERFORMANCE_DETAIL, WC_NONE,
01573   0,
01574   _nested_performance_rating_detail_widgets, lengthof(_nested_performance_rating_detail_widgets)
01575 );
01576 
01577 void ShowPerformanceRatingDetail()
01578 {
01579   AllocateWindowDescFront<PerformanceRatingDetailWindow>(&_performance_rating_detail_desc, 0);
01580 }