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