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     uint linewidth = _settings_client.gui.graph_line_thickness;
00423     uint pointoffs1 = (linewidth + 1) / 2;
00424     uint pointoffs2 = linewidth + 1 - pointoffs1;
00425     for (int i = 0; i < this->num_dataset; i++) {
00426       if (!HasBit(this->excluded_data, i)) {
00427         /* Centre the dot between the grid lines. */
00428         x = r.left + (x_sep / 2);
00429 
00430         byte colour  = this->colours[i];
00431         uint prev_x = INVALID_DATAPOINT_POS;
00432         uint prev_y = INVALID_DATAPOINT_POS;
00433 
00434         for (int j = 0; j < this->num_on_x_axis; j++) {
00435           OverflowSafeInt64 datapoint = this->cost[i][j];
00436 
00437           if (datapoint != INVALID_DATAPOINT) {
00438             /*
00439              * Check whether we need to reduce the 'accuracy' of the
00440              * datapoint value and the highest value to splut overflows.
00441              * And when 'drawing' 'one million' or 'one million and one'
00442              * there is no significant difference, so the least
00443              * significant bits can just be removed.
00444              *
00445              * If there are more bits needed than would fit in a 32 bits
00446              * integer, so at about 31 bits because of the sign bit, the
00447              * least significant bits are removed.
00448              */
00449             int mult_range = FindLastBit(x_axis_offset) + FindLastBit(abs(datapoint));
00450             int reduce_range = max(mult_range - 31, 0);
00451 
00452             /* Handle negative values differently (don't shift sign) */
00453             if (datapoint < 0) {
00454               datapoint = -(abs(datapoint) >> reduce_range);
00455             } else {
00456               datapoint >>= reduce_range;
00457             }
00458             y = r.top + x_axis_offset - ((r.bottom - r.top) * datapoint) / (interval_size >> reduce_range);
00459 
00460             /* Draw the point. */
00461             GfxFillRect(x - pointoffs1, y - pointoffs1, x + pointoffs2, y + pointoffs2, colour);
00462 
00463             /* Draw the line connected to the previous point. */
00464             if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, colour, linewidth);
00465 
00466             prev_x = x;
00467             prev_y = y;
00468           } else {
00469             prev_x = INVALID_DATAPOINT_POS;
00470             prev_y = INVALID_DATAPOINT_POS;
00471           }
00472 
00473           x += x_sep;
00474         }
00475       }
00476     }
00477   }
00478 
00479 
00480   BaseGraphWindow(int widget, StringID format_str_y_axis) :
00481       Window(),
00482       format_str_y_axis(format_str_y_axis)
00483   {
00484     SetWindowDirty(WC_GRAPH_LEGEND, 0);
00485     this->num_vert_lines = 24;
00486     this->graph_widget = widget;
00487   }
00488 
00489   void InitializeWindow(const WindowDesc *desc, WindowNumber number)
00490   {
00491     /* Initialise the dataset */
00492     this->UpdateStatistics(true);
00493 
00494     this->InitNested(desc, number);
00495   }
00496 
00497 public:
00498   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00499   {
00500     if (widget != this->graph_widget) return;
00501 
00502     uint x_label_width = 0;
00503 
00504     if (this->month != 0xFF) {
00505       byte month = this->month;
00506       Year year  = this->year;
00507       for (int i = 0; i < this->num_on_x_axis; i++) {
00508         SetDParam(0, month + STR_MONTH_ABBREV_JAN);
00509         SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
00510         SetDParam(2, year);
00511         x_label_width = max(x_label_width, GetStringBoundingBox(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH).width);
00512 
00513         month += 3;
00514         if (month >= 12) {
00515           month = 0;
00516           year++;
00517         }
00518       }
00519     } else {
00520       /* Draw the label under the data point rather than on the grid line. */
00521       SetDParam(0, this->x_values_start + this->num_on_x_axis * this->x_values_increment);
00522       x_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL_NUMBER).width;
00523     }
00524 
00525     SetDParam(0, this->format_str_y_axis);
00526     SetDParam(1, INT64_MAX);
00527     uint y_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL).width;
00528 
00529     size->width  = max<uint>(size->width,  5 + y_label_width + this->num_on_x_axis * (x_label_width + 5) + 9);
00530     size->height = max<uint>(size->height, 5 + (1 + MIN_GRAPH_NUM_LINES_Y * 2 + (this->month != 0xFF ? 3 : 1)) * FONT_HEIGHT_SMALL + 4);
00531     size->height = max<uint>(size->height, size->width / 3);
00532   }
00533 
00534   virtual void DrawWidget(const Rect &r, int widget) const
00535   {
00536     if (widget != this->graph_widget) return;
00537 
00538     DrawGraph(r);
00539   }
00540 
00541   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00542   {
00543     return INVALID_DATAPOINT;
00544   }
00545 
00546   virtual void OnClick(Point pt, int widget, int click_count)
00547   {
00548     /* Clicked on legend? */
00549     if (widget == BGW_KEY_BUTTON) ShowGraphLegend();
00550   }
00551 
00552   virtual void OnTick()
00553   {
00554     this->UpdateStatistics(false);
00555   }
00556 
00562   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00563   {
00564     if (!gui_scope) return;
00565     this->UpdateStatistics(true);
00566   }
00567 
00572   void UpdateStatistics(bool initialize)
00573   {
00574     uint excluded_companies = _legend_excluded_companies;
00575 
00576     /* Exclude the companies which aren't valid */
00577     for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00578       if (!Company::IsValidID(c)) SetBit(excluded_companies, c);
00579     }
00580 
00581     byte nums = 0;
00582     const Company *c;
00583     FOR_ALL_COMPANIES(c) {
00584       nums = min(this->num_vert_lines, max(nums, c->num_valid_stat_ent));
00585     }
00586 
00587     int mo = (_cur_month / 3 - nums) * 3;
00588     int yr = _cur_year;
00589     while (mo < 0) {
00590       yr--;
00591       mo += 12;
00592     }
00593 
00594     if (!initialize && this->excluded_data == excluded_companies && this->num_on_x_axis == nums &&
00595         this->year == yr && this->month == mo) {
00596       /* There's no reason to get new stats */
00597       return;
00598     }
00599 
00600     this->excluded_data = excluded_companies;
00601     this->num_on_x_axis = nums;
00602     this->year = yr;
00603     this->month = mo;
00604 
00605     int numd = 0;
00606     for (CompanyID k = COMPANY_FIRST; k < MAX_COMPANIES; k++) {
00607       c = Company::GetIfValid(k);
00608       if (c != NULL) {
00609         this->colours[numd] = _colour_gradient[c->colour][6];
00610         for (int j = this->num_on_x_axis, i = 0; --j >= 0;) {
00611           this->cost[numd][i] = (j >= c->num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j);
00612           i++;
00613         }
00614       }
00615       numd++;
00616     }
00617 
00618     this->num_dataset = numd;
00619   }
00620 };
00621 
00622 
00623 /********************/
00624 /* OPERATING PROFIT */
00625 /********************/
00626 
00627 struct OperatingProfitGraphWindow : BaseGraphWindow {
00628   OperatingProfitGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00629       BaseGraphWindow(BGW_GRAPH, STR_JUST_CURRENCY_SHORT)
00630   {
00631     this->InitializeWindow(desc, window_number);
00632   }
00633 
00634   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00635   {
00636     return c->old_economy[j].income + c->old_economy[j].expenses;
00637   }
00638 };
00639 
00640 static const NWidgetPart _nested_operating_profit_widgets[] = {
00641   NWidget(NWID_HORIZONTAL),
00642     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00643     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_OPERATING_PROFIT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00644     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),
00645     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00646     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00647   EndContainer(),
00648   NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00649     NWidget(NWID_HORIZONTAL),
00650       NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
00651       NWidget(NWID_VERTICAL),
00652         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00653         NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00654       EndContainer(),
00655     EndContainer(),
00656   EndContainer(),
00657 };
00658 
00659 static const WindowDesc _operating_profit_desc(
00660   WDP_AUTO, 0, 0,
00661   WC_OPERATING_PROFIT, WC_NONE,
00662   WDF_UNCLICK_BUTTONS,
00663   _nested_operating_profit_widgets, lengthof(_nested_operating_profit_widgets)
00664 );
00665 
00666 
00667 void ShowOperatingProfitGraph()
00668 {
00669   AllocateWindowDescFront<OperatingProfitGraphWindow>(&_operating_profit_desc, 0);
00670 }
00671 
00672 
00673 /****************/
00674 /* INCOME GRAPH */
00675 /****************/
00676 
00677 struct IncomeGraphWindow : BaseGraphWindow {
00678   IncomeGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00679       BaseGraphWindow(BGW_GRAPH, STR_JUST_CURRENCY_SHORT)
00680   {
00681     this->InitializeWindow(desc, window_number);
00682   }
00683 
00684   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00685   {
00686     return c->old_economy[j].income;
00687   }
00688 };
00689 
00690 static const NWidgetPart _nested_income_graph_widgets[] = {
00691   NWidget(NWID_HORIZONTAL),
00692     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00693     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_INCOME_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00694     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),
00695     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00696     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00697   EndContainer(),
00698   NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00699     NWidget(NWID_HORIZONTAL),
00700       NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
00701       NWidget(NWID_VERTICAL),
00702         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00703         NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00704       EndContainer(),
00705     EndContainer(),
00706   EndContainer(),
00707 };
00708 
00709 
00710 static const WindowDesc _income_graph_desc(
00711   WDP_AUTO, 0, 0,
00712   WC_INCOME_GRAPH, WC_NONE,
00713   WDF_UNCLICK_BUTTONS,
00714   _nested_income_graph_widgets, lengthof(_nested_income_graph_widgets)
00715 );
00716 
00717 void ShowIncomeGraph()
00718 {
00719   AllocateWindowDescFront<IncomeGraphWindow>(&_income_graph_desc, 0);
00720 }
00721 
00722 /*******************/
00723 /* DELIVERED CARGO */
00724 /*******************/
00725 
00726 struct DeliveredCargoGraphWindow : BaseGraphWindow {
00727   DeliveredCargoGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00728       BaseGraphWindow(BGW_GRAPH, STR_JUST_COMMA)
00729   {
00730     this->InitializeWindow(desc, window_number);
00731   }
00732 
00733   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00734   {
00735     return c->old_economy[j].delivered_cargo;
00736   }
00737 };
00738 
00739 static const NWidgetPart _nested_delivered_cargo_graph_widgets[] = {
00740   NWidget(NWID_HORIZONTAL),
00741     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00742     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_DELIVERED_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00743     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),
00744     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00745     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00746   EndContainer(),
00747   NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00748     NWidget(NWID_HORIZONTAL),
00749       NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
00750       NWidget(NWID_VERTICAL),
00751         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00752         NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00753       EndContainer(),
00754     EndContainer(),
00755   EndContainer(),
00756 };
00757 
00758 static const WindowDesc _delivered_cargo_graph_desc(
00759   WDP_AUTO, 0, 0,
00760   WC_DELIVERED_CARGO, WC_NONE,
00761   WDF_UNCLICK_BUTTONS,
00762   _nested_delivered_cargo_graph_widgets, lengthof(_nested_delivered_cargo_graph_widgets)
00763 );
00764 
00765 void ShowDeliveredCargoGraph()
00766 {
00767   AllocateWindowDescFront<DeliveredCargoGraphWindow>(&_delivered_cargo_graph_desc, 0);
00768 }
00769 
00770 /***********************/
00771 /* PERFORMANCE HISTORY */
00772 /***********************/
00773 
00775 enum PerformanceHistoryGraphWidgets {
00776   PHW_KEY,
00777   PHW_DETAILED_PERFORMANCE,
00778   PHW_BACKGROUND,
00779   PHW_GRAPH,
00780   PHW_RESIZE,
00781 };
00782 
00783 struct PerformanceHistoryGraphWindow : BaseGraphWindow {
00784   PerformanceHistoryGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00785       BaseGraphWindow(PHW_GRAPH, STR_JUST_COMMA)
00786   {
00787     this->InitializeWindow(desc, window_number);
00788   }
00789 
00790   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00791   {
00792     return c->old_economy[j].performance_history;
00793   }
00794 
00795   virtual void OnClick(Point pt, int widget, int click_count)
00796   {
00797     if (widget == PHW_DETAILED_PERFORMANCE) ShowPerformanceRatingDetail();
00798     this->BaseGraphWindow::OnClick(pt, widget, click_count);
00799   }
00800 };
00801 
00802 static const NWidgetPart _nested_performance_history_widgets[] = {
00803   NWidget(NWID_HORIZONTAL),
00804     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00805     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00806     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),
00807     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),
00808     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00809     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00810   EndContainer(),
00811   NWidget(WWT_PANEL, COLOUR_GREY, PHW_BACKGROUND),
00812     NWidget(NWID_HORIZONTAL),
00813       NWidget(WWT_EMPTY, COLOUR_GREY, PHW_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
00814       NWidget(NWID_VERTICAL),
00815         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00816         NWidget(WWT_RESIZEBOX, COLOUR_GREY, PHW_RESIZE),
00817       EndContainer(),
00818     EndContainer(),
00819   EndContainer(),
00820 };
00821 
00822 static const WindowDesc _performance_history_desc(
00823   WDP_AUTO, 0, 0,
00824   WC_PERFORMANCE_HISTORY, WC_NONE,
00825   WDF_UNCLICK_BUTTONS,
00826   _nested_performance_history_widgets, lengthof(_nested_performance_history_widgets)
00827 );
00828 
00829 void ShowPerformanceHistoryGraph()
00830 {
00831   AllocateWindowDescFront<PerformanceHistoryGraphWindow>(&_performance_history_desc, 0);
00832 }
00833 
00834 /*****************/
00835 /* COMPANY VALUE */
00836 /*****************/
00837 
00838 struct CompanyValueGraphWindow : BaseGraphWindow {
00839   CompanyValueGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00840       BaseGraphWindow(BGW_GRAPH, STR_JUST_CURRENCY_SHORT)
00841   {
00842     this->InitializeWindow(desc, window_number);
00843   }
00844 
00845   virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00846   {
00847     return c->old_economy[j].company_value;
00848   }
00849 };
00850 
00851 static const NWidgetPart _nested_company_value_graph_widgets[] = {
00852   NWidget(NWID_HORIZONTAL),
00853     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00854     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_VALUES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00855     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),
00856     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00857     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00858   EndContainer(),
00859   NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00860     NWidget(NWID_HORIZONTAL),
00861       NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
00862       NWidget(NWID_VERTICAL),
00863         NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00864         NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00865       EndContainer(),
00866     EndContainer(),
00867   EndContainer(),
00868 };
00869 
00870 static const WindowDesc _company_value_graph_desc(
00871   WDP_AUTO, 0, 0,
00872   WC_COMPANY_VALUE, WC_NONE,
00873   WDF_UNCLICK_BUTTONS,
00874   _nested_company_value_graph_widgets, lengthof(_nested_company_value_graph_widgets)
00875 );
00876 
00877 void ShowCompanyValueGraph()
00878 {
00879   AllocateWindowDescFront<CompanyValueGraphWindow>(&_company_value_graph_desc, 0);
00880 }
00881 
00882 /*****************/
00883 /* PAYMENT RATES */
00884 /*****************/
00885 
00887 enum CargoPaymentRatesWidgets {
00888   CPW_BACKGROUND,
00889   CPW_HEADER,
00890   CPW_GRAPH,
00891   CPW_RESIZE,
00892   CPW_FOOTER,
00893   CPW_ENABLE_CARGOS,
00894   CPW_DISABLE_CARGOS,
00895   CPW_CARGO_FIRST,
00896 };
00897 
00898 struct PaymentRatesGraphWindow : BaseGraphWindow {
00899   bool first_init; 
00900   PaymentRatesGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00901       BaseGraphWindow(CPW_GRAPH, STR_JUST_CURRENCY_SHORT)
00902   {
00903     this->first_init = true;
00904     this->num_on_x_axis = 20;
00905     this->num_vert_lines = 20;
00906     this->month = 0xFF;
00907     this->x_values_start     = 10;
00908     this->x_values_increment = 10;
00909 
00910     /* Initialise the dataset */
00911     this->OnHundredthTick();
00912 
00913     this->InitNested(desc, window_number);
00914 
00915     this->UpdateLoweredWidgets();
00916   }
00917 
00918   virtual void OnInit()
00919   {
00920     /* UpdateLoweredWidgets needs to be called after a language or NewGRF change, but it can't be called before
00921      * InitNested is done. On the first init these functions are called in the correct order by the constructor. */
00922     if (!this->first_init) {
00923       /* Initialise the dataset */
00924       this->OnHundredthTick();
00925       this->UpdateLoweredWidgets();
00926     }
00927     this->first_init = false;
00928   }
00929 
00930   void UpdateExcludedData()
00931   {
00932     this->excluded_data = 0;
00933 
00934     int i = 0;
00935     const CargoSpec *cs;
00936     FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
00937       if (HasBit(_legend_excluded_cargo, cs->Index())) SetBit(this->excluded_data, i);
00938       i++;
00939     }
00940   }
00941 
00942   void UpdateLoweredWidgets()
00943   {
00944     for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
00945       this->SetWidgetLoweredState(CPW_CARGO_FIRST + i, !HasBit(this->excluded_data, i));
00946     }
00947   }
00948 
00949   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00950   {
00951     if (widget < CPW_CARGO_FIRST) {
00952       BaseGraphWindow::UpdateWidgetSize(widget, size, padding, fill, resize);
00953       return;
00954     }
00955 
00956     const CargoSpec *cs = _sorted_cargo_specs[widget - CPW_CARGO_FIRST];
00957     SetDParam(0, cs->name);
00958     Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO);
00959     d.width += 14; // colour field
00960     d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00961     d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00962     *size = maxdim(d, *size);
00963   }
00964 
00965   virtual void DrawWidget(const Rect &r, int widget) const
00966   {
00967     if (widget < CPW_CARGO_FIRST) {
00968       BaseGraphWindow::DrawWidget(r, widget);
00969       return;
00970     }
00971 
00972     const CargoSpec *cs = _sorted_cargo_specs[widget - CPW_CARGO_FIRST];
00973     bool rtl = _current_text_dir == TD_RTL;
00974 
00975     /* Since the buttons have no text, no images,
00976      * both the text and the coloured box have to be manually painted.
00977      * clk_dif will move one pixel down and one pixel to the right
00978      * when the button is clicked */
00979     byte clk_dif = this->IsWidgetLowered(widget) ? 1 : 0;
00980     int x = r.left + WD_FRAMERECT_LEFT;
00981     int y = r.top;
00982 
00983     int rect_x = clk_dif + (rtl ? r.right - 12 : r.left + WD_FRAMERECT_LEFT);
00984 
00985     GfxFillRect(rect_x, y + clk_dif, rect_x + 8, y + 5 + clk_dif, PC_BLACK);
00986     GfxFillRect(rect_x + 1, y + 1 + clk_dif, rect_x + 7, y + 4 + clk_dif, cs->legend_colour);
00987     SetDParam(0, cs->name);
00988     DrawString(rtl ? r.left : x + 14 + clk_dif, (rtl ? r.right - 14 + clk_dif : r.right), y + clk_dif, STR_GRAPH_CARGO_PAYMENT_CARGO);
00989   }
00990 
00991   virtual void OnClick(Point pt, int widget, int click_count)
00992   {
00993     switch (widget) {
00994       case CPW_ENABLE_CARGOS:
00995         /* Remove all cargos from the excluded lists. */
00996         _legend_excluded_cargo = 0;
00997         this->excluded_data = 0;
00998         this->UpdateLoweredWidgets();
00999         this->SetDirty();
01000         break;
01001 
01002       case CPW_DISABLE_CARGOS: {
01003         /* Add all cargos to the excluded lists. */
01004         int i = 0;
01005         const CargoSpec *cs;
01006         FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
01007           SetBit(_legend_excluded_cargo, cs->Index());
01008           SetBit(this->excluded_data, i);
01009           i++;
01010         }
01011         this->UpdateLoweredWidgets();
01012         this->SetDirty();
01013         break;
01014       }
01015 
01016       default:
01017         if (widget >= CPW_CARGO_FIRST) {
01018           int i = widget - CPW_CARGO_FIRST;
01019           ToggleBit(_legend_excluded_cargo, _sorted_cargo_specs[i]->Index());
01020           this->ToggleWidgetLoweredState(widget);
01021           this->UpdateExcludedData();
01022           this->SetDirty();
01023         }
01024         break;
01025     }
01026   }
01027 
01028   virtual void OnTick()
01029   {
01030     /* Override default OnTick */
01031   }
01032 
01038   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01039   {
01040     if (!gui_scope) return;
01041     this->OnHundredthTick();
01042   }
01043 
01044   virtual void OnHundredthTick()
01045   {
01046     this->UpdateExcludedData();
01047 
01048     int i = 0;
01049     const CargoSpec *cs;
01050     FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
01051       this->colours[i] = cs->legend_colour;
01052       for (uint j = 0; j != 20; j++) {
01053         this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index());
01054       }
01055       i++;
01056     }
01057     this->num_dataset = i;
01058   }
01059 };
01060 
01062 static NWidgetBase *MakeCargoButtons(int *biggest_index)
01063 {
01064   NWidgetVertical *ver = new NWidgetVertical;
01065 
01066   for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
01067     NWidgetBackground *leaf = new NWidgetBackground(WWT_PANEL, COLOUR_ORANGE, CPW_CARGO_FIRST + i, NULL);
01068     leaf->tool_tip = STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO;
01069     leaf->SetFill(1, 0);
01070     leaf->SetLowered(true);
01071     ver->Add(leaf);
01072   }
01073   *biggest_index = CPW_CARGO_FIRST + _sorted_standard_cargo_specs_size - 1;
01074   return ver;
01075 }
01076 
01077 
01078 static const NWidgetPart _nested_cargo_payment_rates_widgets[] = {
01079   NWidget(NWID_HORIZONTAL),
01080     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01081     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01082     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01083     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01084   EndContainer(),
01085   NWidget(WWT_PANEL, COLOUR_GREY, CPW_BACKGROUND), SetMinimalSize(568, 128),
01086     NWidget(NWID_HORIZONTAL),
01087       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01088       NWidget(WWT_TEXT, COLOUR_GREY, CPW_HEADER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE, STR_NULL),
01089       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01090     EndContainer(),
01091     NWidget(NWID_HORIZONTAL),
01092       NWidget(WWT_EMPTY, COLOUR_GREY, CPW_GRAPH), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
01093       NWidget(NWID_VERTICAL),
01094         NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 0), SetResize(0, 1),
01095         NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, CPW_ENABLE_CARGOS), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL), SetFill(1, 0),
01096         NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, CPW_DISABLE_CARGOS), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL), SetFill(1, 0),
01097         NWidget(NWID_SPACER), SetMinimalSize(0, 4),
01098         NWidgetFunction(MakeCargoButtons),
01099         NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1), SetResize(0, 1),
01100       EndContainer(),
01101       NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
01102     EndContainer(),
01103     NWidget(NWID_HORIZONTAL),
01104       NWidget(NWID_SPACER), SetMinimalSize(WD_RESIZEBOX_WIDTH, 0), SetFill(1, 0), SetResize(1, 0),
01105       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),
01106       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01107       NWidget(WWT_RESIZEBOX, COLOUR_GREY, CPW_RESIZE),
01108     EndContainer(),
01109   EndContainer(),
01110 };
01111 
01112 static const WindowDesc _cargo_payment_rates_desc(
01113   WDP_AUTO, 0, 0,
01114   WC_PAYMENT_RATES, WC_NONE,
01115   WDF_UNCLICK_BUTTONS,
01116   _nested_cargo_payment_rates_widgets, lengthof(_nested_cargo_payment_rates_widgets)
01117 );
01118 
01119 
01120 void ShowCargoPaymentRates()
01121 {
01122   AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
01123 }
01124 
01125 /************************/
01126 /* COMPANY LEAGUE TABLE */
01127 /************************/
01128 
01130 enum CompanyLeagueWidgets {
01131   CLW_BACKGROUND,
01132 };
01133 
01134 static const StringID _performance_titles[] = {
01135   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
01136   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
01137   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
01138   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
01139   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
01140   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
01141   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
01142   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
01143   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
01144   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
01145   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
01146   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
01147   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
01148   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
01149   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT,
01150   STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON,
01151 };
01152 
01153 static inline StringID GetPerformanceTitleFromValue(uint value)
01154 {
01155   return _performance_titles[minu(value, 1000) >> 6];
01156 }
01157 
01158 class CompanyLeagueWindow : public Window {
01159 private:
01160   GUIList<const Company*> companies;
01161   uint ordinal_width; 
01162   uint text_width;    
01163   uint icon_width;    
01164   int line_height;    
01165 
01169   void BuildCompanyList()
01170   {
01171     if (!this->companies.NeedRebuild()) return;
01172 
01173     this->companies.Clear();
01174 
01175     const Company *c;
01176     FOR_ALL_COMPANIES(c) {
01177       *this->companies.Append() = c;
01178     }
01179 
01180     this->companies.Compact();
01181     this->companies.RebuildDone();
01182   }
01183 
01185   static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2)
01186   {
01187     return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history;
01188   }
01189 
01190 public:
01191   CompanyLeagueWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
01192   {
01193     this->InitNested(desc, window_number);
01194     this->companies.ForceRebuild();
01195     this->companies.NeedResort();
01196   }
01197 
01198   virtual void OnPaint()
01199   {
01200     this->BuildCompanyList();
01201     this->companies.Sort(&PerformanceSorter);
01202 
01203     this->DrawWidgets();
01204   }
01205 
01206   virtual void DrawWidget(const Rect &r, int widget) const
01207   {
01208     if (widget != CLW_BACKGROUND) return;
01209 
01210     int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - this->line_height) / 2;
01211     uint y = r.top + WD_FRAMERECT_TOP - icon_y_offset;
01212 
01213     bool rtl = _current_text_dir == TD_RTL;
01214     uint ordinal_left  = rtl ? r.right - WD_FRAMERECT_LEFT - this->ordinal_width : r.left + WD_FRAMERECT_LEFT;
01215     uint ordinal_right = rtl ? r.right - WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->ordinal_width;
01216     uint icon_left     = r.left + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + (rtl ? this->text_width : this->ordinal_width);
01217     uint text_left     = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_LEFT - this->text_width;
01218     uint text_right    = rtl ? r.left + WD_FRAMERECT_LEFT + this->text_width : r.right - WD_FRAMERECT_LEFT;
01219 
01220     for (uint i = 0; i != this->companies.Length(); i++) {
01221       const Company *c = this->companies[i];
01222       DrawString(ordinal_left, ordinal_right, y, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW);
01223 
01224       DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
01225 
01226       SetDParam(0, c->index);
01227       SetDParam(1, c->index);
01228       SetDParam(2, GetPerformanceTitleFromValue(c->old_economy[0].performance_history));
01229       DrawString(text_left, text_right, y, STR_COMPANY_LEAGUE_COMPANY_NAME);
01230       y += this->line_height;
01231     }
01232   }
01233 
01234   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01235   {
01236     if (widget != CLW_BACKGROUND) return;
01237 
01238     this->ordinal_width = 0;
01239     for (uint i = 0; i < MAX_COMPANIES; i++) {
01240       this->ordinal_width = max(this->ordinal_width, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST + i).width);
01241     }
01242     this->ordinal_width += 5; // Keep some extra spacing
01243 
01244     uint widest_width = 0;
01245     uint widest_title = 0;
01246     for (uint i = 0; i < lengthof(_performance_titles); i++) {
01247       uint width = GetStringBoundingBox(_performance_titles[i]).width;
01248       if (width > widest_width) {
01249         widest_title = i;
01250         widest_width = width;
01251       }
01252     }
01253 
01254     Dimension d = GetSpriteSize(SPR_COMPANY_ICON);
01255     this->icon_width = d.width + 2;
01256     this->line_height = max<int>(d.height + 2, FONT_HEIGHT_NORMAL);
01257 
01258     const Company *c;
01259     FOR_ALL_COMPANIES(c) {
01260       SetDParam(0, c->index);
01261       SetDParam(1, c->index);
01262       SetDParam(2, _performance_titles[widest_title]);
01263       widest_width = max(widest_width, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME).width);
01264     }
01265 
01266     this->text_width = widest_width + 30; // Keep some extra spacing
01267 
01268     size->width = WD_FRAMERECT_LEFT + this->ordinal_width + WD_FRAMERECT_RIGHT + this->icon_width + WD_FRAMERECT_LEFT + this->text_width + WD_FRAMERECT_RIGHT;
01269     size->height = WD_FRAMERECT_TOP + this->line_height * MAX_COMPANIES + WD_FRAMERECT_BOTTOM;
01270   }
01271 
01272 
01273   virtual void OnTick()
01274   {
01275     if (this->companies.NeedResort()) {
01276       this->SetDirty();
01277     }
01278   }
01279 
01285   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01286   {
01287     if (data == 0) {
01288       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
01289       this->companies.ForceRebuild();
01290     } else {
01291       this->companies.ForceResort();
01292     }
01293   }
01294 };
01295 
01296 static const NWidgetPart _nested_company_league_widgets[] = {
01297   NWidget(NWID_HORIZONTAL),
01298     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01299     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01300     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01301     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01302   EndContainer(),
01303   NWidget(WWT_PANEL, COLOUR_GREY, CLW_BACKGROUND), SetMinimalSize(400, 0), SetMinimalTextLines(15, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM),
01304 };
01305 
01306 static const WindowDesc _company_league_desc(
01307   WDP_AUTO, 0, 0,
01308   WC_COMPANY_LEAGUE, WC_NONE,
01309   0,
01310   _nested_company_league_widgets, lengthof(_nested_company_league_widgets)
01311 );
01312 
01313 void ShowCompanyLeagueTable()
01314 {
01315   AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
01316 }
01317 
01318 /*****************************/
01319 /* PERFORMANCE RATING DETAIL */
01320 /*****************************/
01321 
01323 enum PerformanceRatingDetailsWidgets {
01324   PRW_SCORE_FIRST,
01325   PRW_SCORE_LAST = PRW_SCORE_FIRST + (SCORE_END - SCORE_BEGIN) - 1,
01326 
01327   PRW_COMPANY_FIRST,
01328   PRW_COMPANY_LAST  = PRW_COMPANY_FIRST + MAX_COMPANIES - 1,
01329 };
01330 
01331 struct PerformanceRatingDetailWindow : Window {
01332   static CompanyID company;
01333   int timeout;
01334 
01335   PerformanceRatingDetailWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
01336   {
01337     this->UpdateCompanyStats();
01338 
01339     this->InitNested(desc, window_number);
01340     this->OnInvalidateData(INVALID_COMPANY);
01341   }
01342 
01343   void UpdateCompanyStats()
01344   {
01345     /* Update all company stats with the current data
01346      * (this is because _score_info is not saved to a savegame) */
01347     Company *c;
01348     FOR_ALL_COMPANIES(c) {
01349       UpdateCompanyRatingAndValue(c, false);
01350     }
01351 
01352     this->timeout = DAY_TICKS * 5;
01353   }
01354 
01355   uint score_info_left;
01356   uint score_info_right;
01357   uint bar_left;
01358   uint bar_right;
01359   uint bar_width;
01360   uint bar_height;
01361   uint score_detail_left;
01362   uint score_detail_right;
01363 
01364   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01365   {
01366     switch (widget) {
01367       case PRW_SCORE_FIRST:
01368         this->bar_height = FONT_HEIGHT_NORMAL + 4;
01369         size->height = this->bar_height + 2 * WD_MATRIX_TOP;
01370 
01371         uint score_info_width = 0;
01372         for (uint i = SCORE_BEGIN; i < SCORE_END; i++) {
01373           score_info_width = max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + i).width);
01374         }
01375         SetDParam(0, 1000);
01376         score_info_width += GetStringBoundingBox(STR_BLACK_COMMA).width + WD_FRAMERECT_LEFT;
01377 
01378         SetDParam(0, 100);
01379         this->bar_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT).width + 20; // Wide bars!
01380 
01381         /* At this number we are roughly at the max; it can become wider,
01382          * but then you need at 1000 times more money. At that time you're
01383          * not that interested anymore in the last few digits anyway.
01384          * The 500 is because 999 999 500 to 999 999 999 are rounded to
01385          * 1 000 M, and not 999 999 k. Use negative numbers to account for
01386          * the negative income/amount of money etc. as well. */
01387         int max = -(999999999 - 500);
01388 
01389         /* Scale max for the display currency. Prior to rendering the value
01390          * is converted into the display currency, which may cause it to
01391          * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
01392          * is used, which can produce quite short renderings of very large
01393          * values. Otherwise the calculated width could be too narrow.
01394          * Note that it doesn't work if there was a currency with an exchange
01395          * rate greater than max.
01396          * When the currency rate is more than 1000, the 999 999 k becomes at
01397          * least 999 999 M which roughly is equally long. Furthermore if the
01398          * exchange rate is that high, 999 999 k is usually not enough anymore
01399          * to show the different currency numbers. */
01400         if (_currency->rate < 1000) max /= _currency->rate;
01401         SetDParam(0, max);
01402         SetDParam(1, max);
01403         uint score_detail_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY).width;
01404 
01405         size->width = 7 + score_info_width + 5 + this->bar_width + 5 + score_detail_width + 7;
01406         uint left  = 7;
01407         uint right = size->width - 7;
01408 
01409         bool rtl = _current_text_dir == TD_RTL;
01410         this->score_info_left  = rtl ? right - score_info_width : left;
01411         this->score_info_right = rtl ? right : left + score_info_width;
01412 
01413         this->score_detail_left  = rtl ? left : right - score_detail_width;
01414         this->score_detail_right = rtl ? left + score_detail_width : right;
01415 
01416         this->bar_left  = left + (rtl ? score_detail_width : score_info_width) + 5;
01417         this->bar_right = this->bar_left + this->bar_width;
01418         break;
01419     }
01420   }
01421 
01422   virtual void DrawWidget(const Rect &r, int widget) const
01423   {
01424     /* No need to draw when there's nothing to draw */
01425     if (this->company == INVALID_COMPANY) return;
01426 
01427     if (IsInsideMM(widget, PRW_COMPANY_FIRST, PRW_COMPANY_LAST + 1)) {
01428       if (this->IsWidgetDisabled(widget)) return;
01429       CompanyID cid = (CompanyID)(widget - PRW_COMPANY_FIRST);
01430       int offset = (cid == this->company) ? 1 : 0;
01431       Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
01432       DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
01433       return;
01434     }
01435 
01436     if (!IsInsideMM(widget, PRW_SCORE_FIRST, PRW_SCORE_LAST + 1)) return;
01437 
01438     ScoreID score_type = (ScoreID)(widget - PRW_SCORE_FIRST);
01439 
01440     /* The colours used to show how the progress is going */
01441     int colour_done = _colour_gradient[COLOUR_GREEN][4];
01442     int colour_notdone = _colour_gradient[COLOUR_RED][4];
01443 
01444     /* Draw all the score parts */
01445     int val    = _score_part[company][score_type];
01446     int needed = _score_info[score_type].needed;
01447     int score  = _score_info[score_type].score;
01448 
01449     /* SCORE_TOTAL has his own rules ;) */
01450     if (score_type == SCORE_TOTAL) {
01451       for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
01452       needed = SCORE_MAX;
01453     }
01454 
01455     uint bar_top  = r.top + WD_MATRIX_TOP;
01456     uint text_top = bar_top + 2;
01457 
01458     DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
01459 
01460     /* Draw the score */
01461     SetDParam(0, score);
01462     DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING, SA_RIGHT);
01463 
01464     /* Calculate the %-bar */
01465     uint x = Clamp(val, 0, needed) * this->bar_width / needed;
01466     bool rtl = _current_text_dir == TD_RTL;
01467     if (rtl) {
01468       x = this->bar_right - x;
01469     } else {
01470       x = this->bar_left + x;
01471     }
01472 
01473     /* Draw the bar */
01474     if (x != this->bar_left)  GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
01475     if (x != this->bar_right) GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
01476 
01477     /* Draw it */
01478     SetDParam(0, Clamp(val, 0, needed) * 100 / needed);
01479     DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING, SA_HOR_CENTER);
01480 
01481     /* SCORE_LOAN is inversed */
01482     if (score_type == SCORE_LOAN) val = needed - val;
01483 
01484     /* Draw the amount we have against what is needed
01485      * For some of them it is in currency format */
01486     SetDParam(0, val);
01487     SetDParam(1, needed);
01488     switch (score_type) {
01489       case SCORE_MIN_PROFIT:
01490       case SCORE_MIN_INCOME:
01491       case SCORE_MAX_INCOME:
01492       case SCORE_MONEY:
01493       case SCORE_LOAN:
01494         DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
01495         break;
01496       default:
01497         DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
01498     }
01499   }
01500 
01501   virtual void OnClick(Point pt, int widget, int click_count)
01502   {
01503     /* Check which button is clicked */
01504     if (IsInsideMM(widget, PRW_COMPANY_FIRST, PRW_COMPANY_LAST + 1)) {
01505       /* Is it no on disable? */
01506       if (!this->IsWidgetDisabled(widget)) {
01507         this->RaiseWidget(this->company + PRW_COMPANY_FIRST);
01508         this->company = (CompanyID)(widget - PRW_COMPANY_FIRST);
01509         this->LowerWidget(this->company + PRW_COMPANY_FIRST);
01510         this->SetDirty();
01511       }
01512     }
01513   }
01514 
01515   virtual void OnTick()
01516   {
01517     if (_pause_mode != PM_UNPAUSED) return;
01518 
01519     /* Update the company score every 5 days */
01520     if (--this->timeout == 0) {
01521       this->UpdateCompanyStats();
01522       this->SetDirty();
01523     }
01524   }
01525 
01531   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01532   {
01533     if (!gui_scope) return;
01534     /* Disable the companies who are not active */
01535     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01536       this->SetWidgetDisabledState(i + PRW_COMPANY_FIRST, !Company::IsValidID(i));
01537     }
01538 
01539     /* Check if the currently selected company is still active. */
01540     if (this->company != INVALID_COMPANY && !Company::IsValidID(this->company)) {
01541       /* Raise the widget for the previous selection. */
01542       this->RaiseWidget(this->company + PRW_COMPANY_FIRST);
01543       this->company = INVALID_COMPANY;
01544     }
01545 
01546     if (this->company == INVALID_COMPANY) {
01547       const Company *c;
01548       FOR_ALL_COMPANIES(c) {
01549         this->company = c->index;
01550         break;
01551       }
01552     }
01553 
01554     /* Make sure the widget is lowered */
01555     this->LowerWidget(this->company + PRW_COMPANY_FIRST);
01556   }
01557 };
01558 
01559 CompanyID PerformanceRatingDetailWindow::company = INVALID_COMPANY;
01560 
01567 static NWidgetBase *MakePerformanceDetailPanels(int *biggest_index)
01568 {
01569   const StringID performance_tips[] = {
01570     STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP,
01571     STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
01572     STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP,
01573     STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
01574     STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
01575     STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
01576     STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
01577     STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
01578     STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
01579     STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
01580   };
01581 
01582   assert_compile(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
01583 
01584   NWidgetVertical *vert = new NWidgetVertical(NC_EQUALSIZE);
01585   for (int widnum = PRW_SCORE_FIRST; widnum <= PRW_SCORE_LAST; widnum++) {
01586     NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
01587     panel->SetFill(1, 1);
01588     panel->SetDataTip(0x0, performance_tips[widnum - PRW_SCORE_FIRST]);
01589     vert->Add(panel);
01590   }
01591   *biggest_index = PRW_SCORE_LAST;
01592   return vert;
01593 }
01594 
01596 NWidgetBase *MakeCompanyButtonRowsGraphGUI(int *biggest_index)
01597 {
01598   return MakeCompanyButtonRows(biggest_index, PRW_COMPANY_FIRST, PRW_COMPANY_LAST, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
01599 }
01600 
01601 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
01602   NWidget(NWID_HORIZONTAL),
01603     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01604     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PERFORMANCE_DETAIL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01605     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01606     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01607   EndContainer(),
01608   NWidget(WWT_PANEL, COLOUR_GREY),
01609     NWidgetFunction(MakeCompanyButtonRowsGraphGUI), SetPadding(0, 1, 1, 2),
01610   EndContainer(),
01611   NWidgetFunction(MakePerformanceDetailPanels),
01612 };
01613 
01614 static const WindowDesc _performance_rating_detail_desc(
01615   WDP_AUTO, 0, 0,
01616   WC_PERFORMANCE_DETAIL, WC_NONE,
01617   0,
01618   _nested_performance_rating_detail_widgets, lengthof(_nested_performance_rating_detail_widgets)
01619 );
01620 
01621 void ShowPerformanceRatingDetail()
01622 {
01623   AllocateWindowDescFront<PerformanceRatingDetailWindow>(&_performance_rating_detail_desc, 0);
01624 }