# subsidy_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 "industry.h"
    00014 #include "town.h"
    00015 #include "window_gui.h"
    00016 #include "strings_func.h"
    00017 #include "date_func.h"
    00018 #include "viewport_func.h"
    00019 #include "gui.h"
    00020 #include "subsidy_func.h"
    00021 #include "subsidy_base.h"
    00022 #include "core/geometry_func.hpp"
    00023
    00024 #include "widgets/subsidy_widget.h"
    00025
    00026 #include "table/strings.h"
    00027
    00028 struct SubsidyListWindow : Window {
    00029   Scrollbar *vscroll;
    00030
    00031   SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00032   {
    00033     this->CreateNestedTree(desc);
    00034     this->vscroll = this->GetScrollbar(WID_SUL_SCROLLBAR);
    00035     this->FinishInitNested(desc, window_number);
    00036     this->OnInvalidateData(0);
    00037   }
    00038
    00039   virtual void OnClick(Point pt, int widget, int click_count)
    00040   {
    00041     if (widget != WID_SUL_PANEL) return;
    00042
    00043     int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SUL_PANEL, WD_FRAMERECT_TOP);
    00044     int num = 0;
    00045     const Subsidy *s;
    00046     FOR_ALL_SUBSIDIES(s) {
    00047       if (!s->IsAwarded()) {
    00048         y--;
    00049         if (y == 0) {
    00050           this->HandleClick(s);
    00051           return;
    00052         }
    00053         num++;
    00054       }
    00055     }
    00056
    00057     if (num == 0) {
    00058       y--; // "None"
    00059       if (y < 0) return;
    00060     }
    00061
    00062     y -= 2; // "Services already subsidised:"
    00063     if (y < 0) return;
    00064
    00065     FOR_ALL_SUBSIDIES(s) {
    00066       if (s->IsAwarded()) {
    00067         y--;
    00068         if (y == 0) {
    00069           this->HandleClick(s);
    00070           return;
    00071         }
    00072       }
    00073     }
    00074   }
    00075
    00076   void HandleClick(const Subsidy *s)
    00077   {
    00078     /* determine src coordinate for subsidy and try to scroll to it */
    00079     TileIndex xy;
    00080     switch (s->src_type) {
    00081       case ST_INDUSTRY: xy = Industry::Get(s->src)->location.tile; break;
    00082       case ST_TOWN:     xy =     Town::Get(s->src)->xy; break;
    00083       default: NOT_REACHED();
    00084     }
    00085
    00086     if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
    00087       if (_ctrl_pressed) ShowExtraViewPortWindow(xy);
    00088
    00089       /* otherwise determine dst coordinate for subsidy and scroll to it */
    00090       switch (s->dst_type) {
    00091         case ST_INDUSTRY: xy = Industry::Get(s->dst)->location.tile; break;
    00092         case ST_TOWN:     xy =     Town::Get(s->dst)->xy; break;
    00093         default: NOT_REACHED();
    00094       }
    00095
    00096       if (_ctrl_pressed) {
    00097         ShowExtraViewPortWindow(xy);
    00098       } else {
    00099         ScrollMainWindowToTile(xy);
    00100       }
    00101     }
    00102   }
    00103
    00108   uint CountLines()
    00109   {
    00110     /* Count number of (non) awarded subsidies */
    00111     uint num_awarded = 0;
    00112     uint num_not_awarded = 0;
    00113     const Subsidy *s;
    00114     FOR_ALL_SUBSIDIES(s) {
    00115       if (!s->IsAwarded()) {
    00116         num_not_awarded++;
    00117       } else {
    00118         num_awarded++;
    00119       }
    00120     }
    00121
    00122     /* Count the 'none' lines */
    00123     if (num_awarded     == 0) num_awarded = 1;
    00124     if (num_not_awarded == 0) num_not_awarded = 1;
    00125
    00126     /* Offered, accepted and an empty line before the accepted ones. */
    00127     return 3 + num_awarded + num_not_awarded;
    00128   }
    00129
    00130   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00131   {
    00132     if (widget != WID_SUL_PANEL) return;
    00133     Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
    00134
    00135     resize->height = d.height;
    00136
    00137     d.height *= 5;
    00138     d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
    00139     d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00140     *size = maxdim(*size, d);
    00141   }
    00142
    00143   virtual void DrawWidget(const Rect &r, int widget) const
    00144   {
    00145     if (widget != WID_SUL_PANEL) return;
    00146
    00147     YearMonthDay ymd;
    00148     ConvertDateToYMD(_date, &ymd);
    00149
    00150     int right = r.right - WD_FRAMERECT_RIGHT;
    00151     int y = r.top + WD_FRAMERECT_TOP;
    00152     int x = r.left + WD_FRAMERECT_LEFT;
    00153
    00154     int pos = -this->vscroll->GetPosition();
    00155     const int cap = this->vscroll->GetCapacity();
    00156
    00157     /* Section for drawing the offered subsidies */
    00158     if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
    00159     pos++;
    00160
    00161     uint num = 0;
    00162     const Subsidy *s;
    00163     FOR_ALL_SUBSIDIES(s) {
    00164       if (!s->IsAwarded()) {
    00165         if (IsInsideMM(pos, 0, cap)) {
    00166           /* Displays the two offered towns */
    00167           SetupSubsidyDecodeParam(s, true);
    00168           SetDParam(7, _date - ymd.day + s->remaining * 32);
    00169           DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
    00170         }
    00171         pos++;
    00172         num++;
    00173       }
    00174     }
    00175
    00176     if (num == 0) {
    00177       if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
    00178       pos++;
    00179     }
    00180
    00181     /* Section for drawing the already granted subsidies */
    00182     pos++;
    00183     if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
    00184     pos++;
    00185     num = 0;
    00186
    00187     FOR_ALL_SUBSIDIES(s) {
    00188       if (s->IsAwarded()) {
    00189         if (IsInsideMM(pos, 0, cap)) {
    00190           SetupSubsidyDecodeParam(s, true);
    00191           SetDParam(7, s->awarded);
    00192           SetDParam(8, _date - ymd.day + s->remaining * 32);
    00193
    00194           /* Displays the two connected stations */
    00195           DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
    00196         }
    00197         pos++;
    00198         num++;
    00199       }
    00200     }
    00201
    00202     if (num == 0) {
    00203       if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
    00204       pos++;
    00205     }
    00206   }
    00207
    00208   virtual void OnResize()
    00209   {
    00210     this->vscroll->SetCapacityFromWidget(this, WID_SUL_PANEL);
    00211   }
    00212
    00218   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00219   {
    00220     if (!gui_scope) return;
    00221     this->vscroll->SetCount(this->CountLines());
    00222   }
    00223 };
    00224
    00225 static const NWidgetPart _nested_subsidies_list_widgets[] = {
    00226   NWidget(NWID_HORIZONTAL),
    00227     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    00228     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00229     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    00230     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    00231   EndContainer(),
    00232   NWidget(NWID_HORIZONTAL),
    00233     NWidget(WWT_PANEL, COLOUR_BROWN, WID_SUL_PANEL), SetDataTip(0x0, STR_SUBSIDIES_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), SetScrollbar(WID_SUL_SCROLLBAR), EndContainer(),
    00234     NWidget(NWID_VERTICAL),
    00235       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_SUL_SCROLLBAR),
    00236       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    00237     EndContainer(),
    00238   EndContainer(),
    00239 };
    00240
    00241 static const WindowDesc _subsidies_list_desc(
    00242   WDP_AUTO, 500, 127,
    00243   WC_SUBSIDIES_LIST, WC_NONE,
    00244   0,
    00245   _nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
    00246 );
    00247
    00248
    00249 void ShowSubsidiesList()
    00250 {
    00251   AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
    00252 }