(svn r25652) -Fix: Improve text caret movement for complex scripts.
[openttd/fttd.git] / src / subsidy_gui.cpp
blob298a8bb571af52c77781ff22a702ff32ae037c58
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file subsidy_gui.cpp GUI for subsidies. */
12 #include "stdafx.h"
13 #include "industry.h"
14 #include "town.h"
15 #include "window_gui.h"
16 #include "strings_func.h"
17 #include "date_func.h"
18 #include "viewport_func.h"
19 #include "gui.h"
20 #include "subsidy_func.h"
21 #include "subsidy_base.h"
22 #include "core/geometry_func.hpp"
24 #include "widgets/subsidy_widget.h"
26 #include "table/strings.h"
28 struct SubsidyListWindow : Window {
29 Scrollbar *vscroll;
31 SubsidyListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
33 this->CreateNestedTree();
34 this->vscroll = this->GetScrollbar(WID_SUL_SCROLLBAR);
35 this->FinishInitNested(window_number);
36 this->OnInvalidateData(0);
39 virtual void OnClick(Point pt, int widget, int click_count)
41 if (widget != WID_SUL_PANEL) return;
43 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SUL_PANEL, WD_FRAMERECT_TOP);
44 int num = 0;
45 const Subsidy *s;
46 FOR_ALL_SUBSIDIES(s) {
47 if (!s->IsAwarded()) {
48 y--;
49 if (y == 0) {
50 this->HandleClick(s);
51 return;
53 num++;
57 if (num == 0) {
58 y--; // "None"
59 if (y < 0) return;
62 y -= 2; // "Services already subsidised:"
63 if (y < 0) return;
65 FOR_ALL_SUBSIDIES(s) {
66 if (s->IsAwarded()) {
67 y--;
68 if (y == 0) {
69 this->HandleClick(s);
70 return;
76 void HandleClick(const Subsidy *s)
78 /* determine src coordinate for subsidy and try to scroll to it */
79 TileIndex xy;
80 switch (s->src_type) {
81 case ST_INDUSTRY: xy = Industry::Get(s->src)->location.tile; break;
82 case ST_TOWN: xy = Town::Get(s->src)->xy; break;
83 default: NOT_REACHED();
86 if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
87 if (_ctrl_pressed) ShowExtraViewPortWindow(xy);
89 /* otherwise determine dst coordinate for subsidy and scroll to it */
90 switch (s->dst_type) {
91 case ST_INDUSTRY: xy = Industry::Get(s->dst)->location.tile; break;
92 case ST_TOWN: xy = Town::Get(s->dst)->xy; break;
93 default: NOT_REACHED();
96 if (_ctrl_pressed) {
97 ShowExtraViewPortWindow(xy);
98 } else {
99 ScrollMainWindowToTile(xy);
105 * Count the number of lines in this window.
106 * @return the number of lines
108 uint CountLines()
110 /* Count number of (non) awarded subsidies */
111 uint num_awarded = 0;
112 uint num_not_awarded = 0;
113 const Subsidy *s;
114 FOR_ALL_SUBSIDIES(s) {
115 if (!s->IsAwarded()) {
116 num_not_awarded++;
117 } else {
118 num_awarded++;
122 /* Count the 'none' lines */
123 if (num_awarded == 0) num_awarded = 1;
124 if (num_not_awarded == 0) num_not_awarded = 1;
126 /* Offered, accepted and an empty line before the accepted ones. */
127 return 3 + num_awarded + num_not_awarded;
130 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
132 if (widget != WID_SUL_PANEL) return;
133 Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
135 resize->height = d.height;
137 d.height *= 5;
138 d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
139 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
140 *size = maxdim(*size, d);
143 virtual void DrawWidget(const Rect &r, int widget) const
145 if (widget != WID_SUL_PANEL) return;
147 YearMonthDay ymd;
148 ConvertDateToYMD(_date, &ymd);
150 int right = r.right - WD_FRAMERECT_RIGHT;
151 int y = r.top + WD_FRAMERECT_TOP;
152 int x = r.left + WD_FRAMERECT_LEFT;
154 int pos = -this->vscroll->GetPosition();
155 const int cap = this->vscroll->GetCapacity();
157 /* Section for drawing the offered subsidies */
158 if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
159 pos++;
161 uint num = 0;
162 const Subsidy *s;
163 FOR_ALL_SUBSIDIES(s) {
164 if (!s->IsAwarded()) {
165 if (IsInsideMM(pos, 0, cap)) {
166 /* Displays the two offered towns */
167 SetupSubsidyDecodeParam(s, true);
168 SetDParam(7, _date - ymd.day + s->remaining * 32);
169 DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
171 pos++;
172 num++;
176 if (num == 0) {
177 if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
178 pos++;
181 /* Section for drawing the already granted subsidies */
182 pos++;
183 if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
184 pos++;
185 num = 0;
187 FOR_ALL_SUBSIDIES(s) {
188 if (s->IsAwarded()) {
189 if (IsInsideMM(pos, 0, cap)) {
190 SetupSubsidyDecodeParam(s, true);
191 SetDParam(7, s->awarded);
192 SetDParam(8, _date - ymd.day + s->remaining * 32);
194 /* Displays the two connected stations */
195 DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
197 pos++;
198 num++;
202 if (num == 0) {
203 if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
204 pos++;
208 virtual void OnResize()
210 this->vscroll->SetCapacityFromWidget(this, WID_SUL_PANEL);
214 * Some data on this window has become invalid.
215 * @param data Information about the changed data.
216 * @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.
218 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
220 if (!gui_scope) return;
221 this->vscroll->SetCount(this->CountLines());
225 static const NWidgetPart _nested_subsidies_list_widgets[] = {
226 NWidget(NWID_HORIZONTAL),
227 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
228 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
229 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
230 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
231 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
232 EndContainer(),
233 NWidget(NWID_HORIZONTAL),
234 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(),
235 NWidget(NWID_VERTICAL),
236 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_SUL_SCROLLBAR),
237 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
238 EndContainer(),
239 EndContainer(),
242 static WindowDesc _subsidies_list_desc(
243 WDP_AUTO, "list_subsidies", 500, 127,
244 WC_SUBSIDIES_LIST, WC_NONE,
246 _nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
250 void ShowSubsidiesList()
252 AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);