2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file subsidy_gui.cpp GUI for subsidies. */
|
2007-04-04 03:35:16 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "industry.h"
|
|
|
|
#include "town.h"
|
2007-12-19 20:44:29 +01:00
|
|
|
#include "window_gui.h"
|
2007-12-21 20:49:27 +01:00
|
|
|
#include "strings_func.h"
|
2007-12-26 14:50:40 +01:00
|
|
|
#include "date_func.h"
|
2008-01-09 10:45:45 +01:00
|
|
|
#include "viewport_func.h"
|
2008-05-05 13:36:43 +02:00
|
|
|
#include "gui.h"
|
2009-05-23 17:46:00 +02:00
|
|
|
#include "subsidy_func.h"
|
2009-07-01 20:45:05 +02:00
|
|
|
#include "subsidy_base.h"
|
2010-01-15 17:41:15 +01:00
|
|
|
#include "core/geometry_func.hpp"
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-12-15 23:22:55 +01:00
|
|
|
#include "widgets/subsidy_widget.h"
|
2008-01-13 02:21:35 +01:00
|
|
|
|
2011-12-15 23:22:55 +01:00
|
|
|
#include "table/strings.h"
|
2009-04-17 21:47:21 +02:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2008-05-18 10:12:29 +02:00
|
|
|
struct SubsidyListWindow : Window {
|
2010-08-12 10:37:01 +02:00
|
|
|
Scrollbar *vscroll;
|
|
|
|
|
2013-05-26 21:23:42 +02:00
|
|
|
SubsidyListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
|
2008-05-18 10:12:29 +02:00
|
|
|
{
|
2013-05-26 21:23:42 +02:00
|
|
|
this->CreateNestedTree();
|
2011-12-16 19:21:13 +01:00
|
|
|
this->vscroll = this->GetScrollbar(WID_SUL_SCROLLBAR);
|
2013-05-26 21:23:42 +02:00
|
|
|
this->FinishInitNested(window_number);
|
2009-09-01 22:42:12 +02:00
|
|
|
this->OnInvalidateData(0);
|
2008-05-18 10:12:29 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2010-01-30 19:34:48 +01:00
|
|
|
virtual void OnClick(Point pt, int widget, int click_count)
|
2008-05-18 10:12:29 +02:00
|
|
|
{
|
2011-12-16 19:21:13 +01:00
|
|
|
if (widget != WID_SUL_PANEL) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-12-16 19:21:13 +01:00
|
|
|
int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SUL_PANEL, WD_FRAMERECT_TOP);
|
2009-09-01 22:42:12 +02:00
|
|
|
int num = 0;
|
2009-07-01 19:43:26 +02:00
|
|
|
const Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 21:54:35 +02:00
|
|
|
if (!s->IsAwarded()) {
|
2009-09-01 22:42:12 +02:00
|
|
|
y--;
|
|
|
|
if (y == 0) {
|
2008-06-03 02:23:54 +02:00
|
|
|
this->HandleClick(s);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-18 10:12:29 +02:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2008-05-18 10:12:29 +02:00
|
|
|
if (num == 0) {
|
2009-09-01 22:42:12 +02:00
|
|
|
y--; // "None"
|
2008-05-18 10:12:29 +02:00
|
|
|
if (y < 0) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2009-09-01 22:42:12 +02:00
|
|
|
y -= 2; // "Services already subsidised:"
|
2008-05-18 10:12:29 +02:00
|
|
|
if (y < 0) return;
|
2007-03-18 22:58:03 +01:00
|
|
|
|
2009-07-01 19:43:26 +02:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 21:54:35 +02:00
|
|
|
if (s->IsAwarded()) {
|
2009-09-01 22:42:12 +02:00
|
|
|
y--;
|
|
|
|
if (y == 0) {
|
2008-06-03 02:23:54 +02:00
|
|
|
this->HandleClick(s);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-18 10:12:29 +02:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2007-03-18 22:58:03 +01:00
|
|
|
|
2008-05-18 10:12:29 +02:00
|
|
|
void HandleClick(const Subsidy *s)
|
|
|
|
{
|
2009-08-08 00:23:34 +02:00
|
|
|
/* determine src coordinate for subsidy and try to scroll to it */
|
2008-05-18 10:12:29 +02:00
|
|
|
TileIndex xy;
|
2009-08-08 00:23:34 +02:00
|
|
|
switch (s->src_type) {
|
2010-01-04 19:21:07 +01:00
|
|
|
case ST_INDUSTRY: xy = Industry::Get(s->src)->location.tile; break;
|
2009-08-08 00:23:34 +02:00
|
|
|
case ST_TOWN: xy = Town::Get(s->src)->xy; break;
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2008-05-05 13:36:43 +02:00
|
|
|
|
2008-05-18 10:12:29 +02:00
|
|
|
if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
|
|
|
|
if (_ctrl_pressed) ShowExtraViewPortWindow(xy);
|
|
|
|
|
2009-08-08 00:23:34 +02:00
|
|
|
/* otherwise determine dst coordinate for subsidy and scroll to it */
|
|
|
|
switch (s->dst_type) {
|
2010-01-04 19:21:07 +01:00
|
|
|
case ST_INDUSTRY: xy = Industry::Get(s->dst)->location.tile; break;
|
2009-08-08 00:23:34 +02:00
|
|
|
case ST_TOWN: xy = Town::Get(s->dst)->xy; break;
|
|
|
|
default: NOT_REACHED();
|
2008-05-18 10:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_ctrl_pressed) {
|
|
|
|
ShowExtraViewPortWindow(xy);
|
|
|
|
} else {
|
|
|
|
ScrollMainWindowToTile(xy);
|
|
|
|
}
|
2008-05-05 13:36:43 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2009-09-01 22:42:12 +02:00
|
|
|
/**
|
|
|
|
* Count the number of lines in this window.
|
|
|
|
* @return the number of lines
|
|
|
|
*/
|
|
|
|
uint CountLines()
|
2009-09-01 22:06:10 +02:00
|
|
|
{
|
|
|
|
/* Count number of (non) awarded subsidies */
|
|
|
|
uint num_awarded = 0;
|
|
|
|
uint num_not_awarded = 0;
|
|
|
|
const Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
|
|
|
if (!s->IsAwarded()) {
|
|
|
|
num_not_awarded++;
|
|
|
|
} else {
|
|
|
|
num_awarded++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count the 'none' lines */
|
|
|
|
if (num_awarded == 0) num_awarded = 1;
|
|
|
|
if (num_not_awarded == 0) num_not_awarded = 1;
|
|
|
|
|
2009-09-01 22:42:12 +02:00
|
|
|
/* Offered, accepted and an empty line before the accepted ones. */
|
|
|
|
return 3 + num_awarded + num_not_awarded;
|
|
|
|
}
|
2009-09-01 22:06:10 +02:00
|
|
|
|
2009-11-22 19:28:14 +01:00
|
|
|
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
|
2009-09-01 22:42:12 +02:00
|
|
|
{
|
2011-12-16 19:21:13 +01:00
|
|
|
if (widget != WID_SUL_PANEL) return;
|
2009-09-01 22:42:12 +02:00
|
|
|
Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
|
|
|
|
|
|
|
|
resize->height = d.height;
|
|
|
|
|
|
|
|
d.height *= 5;
|
2009-09-01 22:06:10 +02:00
|
|
|
d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
|
|
|
|
d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
|
|
|
|
*size = maxdim(*size, d);
|
|
|
|
}
|
|
|
|
|
2009-09-01 21:29:16 +02:00
|
|
|
virtual void DrawWidget(const Rect &r, int widget) const
|
|
|
|
{
|
2011-12-16 19:21:13 +01:00
|
|
|
if (widget != WID_SUL_PANEL) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-09-01 21:29:16 +02:00
|
|
|
YearMonthDay ymd;
|
2008-05-18 10:12:29 +02:00
|
|
|
ConvertDateToYMD(_date, &ymd);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-09-01 21:29:16 +02:00
|
|
|
int right = r.right - WD_FRAMERECT_RIGHT;
|
|
|
|
int y = r.top + WD_FRAMERECT_TOP;
|
|
|
|
int x = r.left + WD_FRAMERECT_LEFT;
|
2008-04-07 19:02:39 +02:00
|
|
|
|
2010-08-12 10:37:01 +02:00
|
|
|
int pos = -this->vscroll->GetPosition();
|
|
|
|
const int cap = this->vscroll->GetCapacity();
|
2009-09-01 22:42:12 +02:00
|
|
|
|
2013-01-08 23:46:42 +01:00
|
|
|
/* Section for drawing the offered subsidies */
|
2009-09-02 10:40:31 +02:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
|
2009-09-01 22:42:12 +02:00
|
|
|
pos++;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-09-01 22:42:12 +02:00
|
|
|
uint num = 0;
|
2009-09-01 21:29:16 +02:00
|
|
|
const Subsidy *s;
|
2009-07-01 19:43:26 +02:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 21:54:35 +02:00
|
|
|
if (!s->IsAwarded()) {
|
2009-09-02 10:40:31 +02:00
|
|
|
if (IsInsideMM(pos, 0, cap)) {
|
2009-09-01 22:42:12 +02:00
|
|
|
/* Displays the two offered towns */
|
2010-11-24 02:08:49 +01:00
|
|
|
SetupSubsidyDecodeParam(s, true);
|
2009-09-01 22:42:12 +02:00
|
|
|
SetDParam(7, _date - ymd.day + s->remaining * 32);
|
|
|
|
DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
|
|
|
|
}
|
|
|
|
pos++;
|
2008-05-18 10:12:29 +02:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num == 0) {
|
2009-09-02 10:40:31 +02:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
|
2009-09-01 22:42:12 +02:00
|
|
|
pos++;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 23:46:42 +01:00
|
|
|
/* Section for drawing the already granted subsidies */
|
2009-09-01 22:42:12 +02:00
|
|
|
pos++;
|
2009-09-02 10:40:31 +02:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
|
2009-09-01 22:42:12 +02:00
|
|
|
pos++;
|
2008-05-18 10:12:29 +02:00
|
|
|
num = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-07-01 19:43:26 +02:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 21:54:35 +02:00
|
|
|
if (s->IsAwarded()) {
|
2009-09-02 10:40:31 +02:00
|
|
|
if (IsInsideMM(pos, 0, cap)) {
|
2010-11-24 02:08:49 +01:00
|
|
|
SetupSubsidyDecodeParam(s, true);
|
2009-09-01 22:42:12 +02:00
|
|
|
SetDParam(7, s->awarded);
|
|
|
|
SetDParam(8, _date - ymd.day + s->remaining * 32);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-09-01 22:42:12 +02:00
|
|
|
/* Displays the two connected stations */
|
|
|
|
DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
|
|
|
|
}
|
|
|
|
pos++;
|
2008-05-18 10:12:29 +02:00
|
|
|
num++;
|
2008-04-04 04:24:44 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2009-09-01 22:42:12 +02:00
|
|
|
if (num == 0) {
|
2009-09-02 10:40:31 +02:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
|
2009-09-01 22:42:12 +02:00
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-24 16:53:55 +02:00
|
|
|
virtual void OnResize()
|
2009-09-01 22:42:12 +02:00
|
|
|
{
|
2011-12-16 19:21:13 +01:00
|
|
|
this->vscroll->SetCapacityFromWidget(this, WID_SUL_PANEL);
|
2009-09-01 22:42:12 +02:00
|
|
|
}
|
|
|
|
|
2011-03-13 22:31:29 +01:00
|
|
|
/**
|
|
|
|
* Some data on this window has become invalid.
|
|
|
|
* @param data Information about the changed data.
|
|
|
|
* @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
|
|
|
|
*/
|
|
|
|
virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
|
2009-09-01 22:42:12 +02:00
|
|
|
{
|
2011-03-13 22:31:29 +01:00
|
|
|
if (!gui_scope) return;
|
2010-08-12 10:37:01 +02:00
|
|
|
this->vscroll->SetCount(this->CountLines());
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2008-05-18 10:12:29 +02:00
|
|
|
};
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-04-17 22:03:44 +02:00
|
|
|
static const NWidgetPart _nested_subsidies_list_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-11-24 19:05:55 +01:00
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2009-12-21 17:24:29 +01:00
|
|
|
NWidget(WWT_SHADEBOX, COLOUR_BROWN),
|
2013-05-26 21:30:07 +02:00
|
|
|
NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
|
2009-11-24 19:05:55 +01:00
|
|
|
NWidget(WWT_STICKYBOX, COLOUR_BROWN),
|
2009-04-17 22:03:44 +02:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 19:21:13 +01:00
|
|
|
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(),
|
2009-04-17 22:03:44 +02:00
|
|
|
NWidget(NWID_VERTICAL),
|
2011-12-16 19:21:13 +01:00
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_SUL_SCROLLBAR),
|
2009-11-24 19:05:55 +01:00
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
|
2009-04-17 22:03:44 +02:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2013-05-26 21:23:42 +02:00
|
|
|
static WindowDesc _subsidies_list_desc(
|
2013-05-26 21:25:01 +02:00
|
|
|
WDP_AUTO, "list_subsidies", 500, 127,
|
2007-02-01 16:49:12 +01:00
|
|
|
WC_SUBSIDIES_LIST, WC_NONE,
|
2009-11-24 18:28:29 +01:00
|
|
|
0,
|
2009-11-15 11:26:01 +01:00
|
|
|
_nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
|
2009-03-15 16:12:06 +01:00
|
|
|
);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
void ShowSubsidiesList()
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2008-05-18 10:12:29 +02:00
|
|
|
AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|