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