graph_gui.cpp

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

Generated on Tue Jan 5 21:02:54 2010 for OpenTTD by  doxygen 1.5.6