# subsidy_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: subsidy_gui.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "station_base.h"
    00007 #include "industry.h"
    00008 #include "town.h"
    00009 #include "economy_func.h"
    00010 #include "cargotype.h"
    00011 #include "window_gui.h"
    00012 #include "strings_func.h"
    00013 #include "date_func.h"
    00014 #include "viewport_func.h"
    00015 #include "gfx_func.h"
    00016 #include "gui.h"
    00017
    00018 #include "table/strings.h"
    00019
    00020 struct SubsidyListWindow : Window {
    00021   SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
    00022   {
    00023     this->FindWindowPlacementAndResize(desc);
    00024   }
    00025
    00026   virtual void OnClick(Point pt, int widget)
    00027   {
    00028     if (widget != 3) return;
    00029
    00030     int y = pt.y - 25;
    00031
    00032     if (y < 0) return;
    00033
    00034     uint num = 0;
    00035     for (const Subsidy *s = _subsidies; s != endof(_subsidies); s++) {
    00036       if (s->cargo_type != CT_INVALID && s->age < 12) {
    00037         y -= 10;
    00038         if (y < 0) {
    00039           this->HandleClick(s);
    00040           return;
    00041         }
    00042         num++;
    00043       }
    00044     }
    00045
    00046     if (num == 0) {
    00047       y -= 10; // "None"
    00048       if (y < 0) return;
    00049     }
    00050
    00051     y -= 11; // "Services already subsidised:"
    00052     if (y < 0) return;
    00053
    00054     for (const Subsidy *s = _subsidies; s != endof(_subsidies); s++) {
    00055       if (s->cargo_type != CT_INVALID && s->age >= 12) {
    00056         y -= 10;
    00057         if (y < 0) {
    00058           this->HandleClick(s);
    00059           return;
    00060         }
    00061       }
    00062     }
    00063   }
    00064
    00065   void HandleClick(const Subsidy *s)
    00066   {
    00067     TownEffect te = GetCargo(s->cargo_type)->town_effect;
    00068     TileIndex xy;
    00069
    00070     /* determine from coordinate for subsidy and try to scroll to it */
    00071     uint offs = s->from;
    00072     if (s->age >= 12) {
    00073       xy = GetStation(offs)->xy;
    00074     } else if (te == TE_PASSENGERS || te == TE_MAIL) {
    00075       xy = GetTown(offs)->xy;
    00076     } else {
    00077       xy = GetIndustry(offs)->xy;
    00078     }
    00079
    00080     if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
    00081       if (_ctrl_pressed) ShowExtraViewPortWindow(xy);
    00082
    00083       /* otherwise determine to coordinate for subsidy and scroll to it */
    00084       offs = s->to;
    00085       if (s->age >= 12) {
    00086         xy = GetStation(offs)->xy;
    00087       } else if (te == TE_PASSENGERS || te == TE_MAIL || te == TE_GOODS || te == TE_FOOD) {
    00088         xy = GetTown(offs)->xy;
    00089       } else {
    00090         xy = GetIndustry(offs)->xy;
    00091       }
    00092
    00093       if (_ctrl_pressed) {
    00094         ShowExtraViewPortWindow(xy);
    00095       } else {
    00096         ScrollMainWindowToTile(xy);
    00097       }
    00098     }
    00099   }
    00100
    00101   virtual void OnPaint()
    00102   {
    00103     YearMonthDay ymd;
    00104     const Subsidy *s;
    00105
    00106     this->DrawWidgets();
    00107
    00108     ConvertDateToYMD(_date, &ymd);
    00109
    00110     int width = this->width - 13;  // scroll bar = 11 + pixel each side
    00111     int y = 15;
    00112     int x = 1;
    00113
    00114     /* Section for drawing the offered subisidies */
    00115     DrawStringTruncated(x, y, STR_2026_SUBSIDIES_ON_OFFER_FOR, TC_FROMSTRING, width);
    00116     y += 10;
    00117     uint num = 0;
    00118
    00119     for (s = _subsidies; s != endof(_subsidies); s++) {
    00120       if (s->cargo_type != CT_INVALID && s->age < 12) {
    00121         int x2;
    00122
    00123         /* Displays the two offered towns */
    00124         SetupSubsidyDecodeParam(s, 1);
    00125         x2 = DrawStringTruncated(x + 2, y, STR_2027_FROM_TO, TC_FROMSTRING, width - 2);
    00126
    00127         if (width - x2 > 10) {
    00128           /* Displays the deadline before voiding the proposal */
    00129           SetDParam(0, _date - ymd.day + 384 - s->age * 32);
    00130           DrawStringTruncated(x2, y, STR_2028_BY, TC_FROMSTRING, width - x2);
    00131         }
    00132
    00133         y += 10;
    00134         num++;
    00135       }
    00136     }
    00137
    00138     if (num == 0) {
    00139       DrawStringTruncated(x + 2, y, STR_202A_NONE, TC_FROMSTRING, width - 2);
    00140       y += 10;
    00141     }
    00142
    00143     /* Section for drawing the already granted subisidies */
    00144     DrawStringTruncated(x, y + 1, STR_202B_SERVICES_ALREADY_SUBSIDISED, TC_FROMSTRING, width);
    00145     y += 10;
    00146     num = 0;
    00147
    00148     for (s = _subsidies; s != endof(_subsidies); s++) {
    00149       if (s->cargo_type != CT_INVALID && s->age >= 12) {
    00150         int xt;
    00151
    00152         SetupSubsidyDecodeParam(s, 1);
    00153
    00154         SetDParam(3, GetStation(s->to)->owner);
    00155
    00156         /* Displays the two connected stations */
    00157         xt = DrawStringTruncated(x + 2, y, STR_202C_FROM_TO, TC_FROMSTRING, width - 2);
    00158
    00159         /* Displays the date where the granted subsidy will end */
    00160         if ((xt > 3) && (width - xt) > 9 ) { // do not draw if previous drawing failed or if it will overlap on scrollbar
    00161           SetDParam(0, _date - ymd.day + 768 - s->age * 32);
    00162           DrawStringTruncated(xt, y, STR_202D_UNTIL, TC_FROMSTRING, width - xt);
    00163         }
    00164         y += 10;
    00165         num++;
    00166       }
    00167     }
    00168
    00169     if (num == 0) DrawStringTruncated(x + 2, y, STR_202A_NONE, TC_FROMSTRING, width - 2);
    00170   }
    00171 };
    00172
    00173 static const Widget _subsidies_list_widgets[] = {
    00174 {   WWT_CLOSEBOX, RESIZE_NONE,   COLOUR_BROWN,   0,  10,   0,  13, STR_00C5,           STR_018B_CLOSE_WINDOW},
    00175 {    WWT_CAPTION, RESIZE_RIGHT,  COLOUR_BROWN,  11, 307,   0,  13, STR_2025_SUBSIDIES, STR_018C_WINDOW_TITLE_DRAG_THIS},
    00176 {  WWT_STICKYBOX, RESIZE_LR,     COLOUR_BROWN, 308, 319,   0,  13, STR_NULL,           STR_STICKY_BUTTON},
    00177 {      WWT_PANEL, RESIZE_RB,     COLOUR_BROWN,   0, 307,  14, 126, 0x0,                STR_01FD_CLICK_ON_SERVICE_TO_CENTER},
    00178 {  WWT_SCROLLBAR, RESIZE_LRB,    COLOUR_BROWN, 308, 319,  14, 114, 0x0,                STR_0190_SCROLL_BAR_SCROLLS_LIST},
    00179 {  WWT_RESIZEBOX, RESIZE_LRTB,   COLOUR_BROWN, 308, 319, 115, 126, 0x0,                STR_RESIZE_BUTTON},
    00180
    00181 {   WIDGETS_END},
    00182 };
    00183
    00184 static const WindowDesc _subsidies_list_desc = {
    00185   WDP_AUTO, WDP_AUTO, 320, 127, 320, 127,
    00186   WC_SUBSIDIES_LIST, WC_NONE,
    00187   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    00188   _subsidies_list_widgets,
    00189 };
    00190
    00191
    00192 void ShowSubsidiesList()
    00193 {
    00194   AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
    00195 }