# graph_gui.cpp

Go to the documentation of this file.

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