Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / statusbar_gui.cpp
blobc1090f6ca9a97c238ce99f3e7a59f2ad5c8dc644
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 statusbar_gui.cpp The GUI for the bottom status bar. */
12 #include "stdafx.h"
13 #include "date_func.h"
14 #include "gfx_func.h"
15 #include "news_func.h"
16 #include "company_func.h"
17 #include "string.h"
18 #include "strings_func.h"
19 #include "company_base.h"
20 #include "tilehighlight_func.h"
21 #include "news_gui.h"
22 #include "company_gui.h"
23 #include "window_gui.h"
24 #include "saveload/saveload.h"
25 #include "window_func.h"
26 #include "statusbar_gui.h"
27 #include "toolbar_gui.h"
28 #include "core/geometry_func.hpp"
30 #include "widgets/statusbar_widget.h"
32 #include "table/strings.h"
33 #include "table/sprites.h"
35 static bool DrawScrollingStatusText (BlitArea *dpi, const NewsItem *ni,
36 int scroll_pos, int left, int right, int top, int bottom)
38 CopyInDParam(0, ni->params, lengthof(ni->params));
39 StringID str = ni->string_id;
41 char buf[512];
42 GetString (buf, str);
43 const char *s = buf;
45 char buffer[256];
46 char *d = buffer;
47 const char *last = lastof(buffer);
49 for (;;) {
50 WChar c = Utf8Consume(&s);
51 if (c == 0) {
52 break;
53 } else if (c == '\n') {
54 if (d + 4 >= last) break;
55 d[0] = d[1] = d[2] = d[3] = ' ';
56 d += 4;
57 } else if (IsPrintable(c)) {
58 if (d + Utf8CharLen(c) >= last) break;
59 d += Utf8Encode(d, c);
62 *d = '\0';
64 BlitArea tmp_dpi;
65 if (!InitBlitArea (dpi, &tmp_dpi, left, top, right - left, bottom)) return true;
67 int width = GetStringBoundingBox(buffer).width;
68 int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
70 DrawString (&tmp_dpi, pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
72 return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
75 struct StatusBarWindow : Window {
76 bool saving;
77 int ticker_scroll;
78 int reminder_timeout;
80 static const int TICKER_STOP = 1640; ///< scrolling is finished when counter reaches this value
81 static const int REMINDER_START = 91; ///< initial value of the reminder counter (right dot on the right)
82 static const int REMINDER_STOP = 0; ///< reminder disappears when counter reaches this value
83 static const int COUNTER_STEP = 2; ///< this is subtracted from active counters every tick
85 StatusBarWindow (const WindowDesc *desc) :
86 Window (desc), saving (false),
87 ticker_scroll (TICKER_STOP), reminder_timeout (REMINDER_STOP)
89 this->InitNested();
90 CLRBITS(this->flags, WF_WHITE_BORDER);
91 PositionStatusbar(this);
94 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
96 Point pt = { 0, _screen_height - sm_height };
97 return pt;
100 virtual void FindWindowPlacementAndResize(int def_width, int def_height)
102 Window::FindWindowPlacementAndResize(_toolbar_width, def_height);
105 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
107 Dimension d;
108 switch (widget) {
109 case WID_S_LEFT:
110 SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR);
111 d = GetStringBoundingBox(STR_WHITE_DATE_LONG);
112 break;
114 case WID_S_RIGHT: {
115 int64 max_money = UINT32_MAX;
116 const Company *c;
117 FOR_ALL_COMPANIES(c) max_money = max<int64>(c->money, max_money);
118 SetDParam(0, 100LL * max_money);
119 d = GetStringBoundingBox(STR_COMPANY_MONEY);
120 break;
123 default:
124 return;
127 d.width += padding.width;
128 d.height += padding.height;
129 *size = maxdim(d, *size);
132 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
134 switch (widget) {
135 case WID_S_LEFT:
136 /* Draw the date */
137 SetDParam(0, _date);
138 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_WHITE_DATE_LONG, TC_FROMSTRING, SA_HOR_CENTER);
139 break;
141 case WID_S_RIGHT: {
142 /* Draw company money, if any */
143 const Company *c = Company::GetIfValid(_local_company);
144 if (c != NULL) {
145 SetDParam(0, c->money);
146 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_COMPANY_MONEY, TC_FROMSTRING, SA_HOR_CENTER);
148 break;
151 case WID_S_MIDDLE:
152 /* Draw status bar */
153 if (this->saving) { // true when saving is active
154 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_SAVING_GAME, TC_FROMSTRING, SA_HOR_CENTER);
155 } else if (_do_autosave) {
156 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
157 } else if (_pause_mode != PM_UNPAUSED) {
158 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_PAUSED, TC_FROMSTRING, SA_HOR_CENTER);
159 } else if (this->ticker_scroll < TICKER_STOP && FindWindowById(WC_NEWS_WINDOW, 0) == NULL && _statusbar_news_item != NULL && _statusbar_news_item->string_id != 0) {
160 /* Draw the scrolling news text */
161 if (!DrawScrollingStatusText (dpi, _statusbar_news_item, this->ticker_scroll, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom)) {
162 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
163 if (Company::IsValidID(_local_company)) {
164 /* This is the default text */
165 SetDParam(0, _local_company);
166 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
169 } else {
170 if (Company::IsValidID(_local_company)) {
171 /* This is the default text */
172 SetDParam(0, _local_company);
173 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
177 if (this->reminder_timeout > 0) {
178 Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
179 DrawSprite (dpi, SPR_UNREAD_NEWS, PAL_NONE, r.right - WD_FRAMERECT_RIGHT - icon_size.width, r.top + WD_FRAMERECT_TOP + (int)(FONT_HEIGHT_NORMAL - icon_size.height) / 2);
181 break;
186 * Some data on this window has become invalid.
187 * @param data Information about the changed data.
188 * @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.
190 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
192 if (!gui_scope) return;
193 switch (data) {
194 default: NOT_REACHED();
195 case SBI_SAVELOAD_START: this->saving = true; break;
196 case SBI_SAVELOAD_FINISH: this->saving = false; break;
197 case SBI_SHOW_TICKER: this->ticker_scroll = 0; break;
198 case SBI_SHOW_REMINDER: this->reminder_timeout = REMINDER_START; break;
199 case SBI_NEWS_DELETED:
200 this->ticker_scroll = TICKER_STOP; // reset ticker ...
201 this->reminder_timeout = REMINDER_STOP; // ... and reminder
202 break;
206 virtual void OnClick(Point pt, int widget, int click_count)
208 switch (widget) {
209 case WID_S_MIDDLE: ShowLastNewsMessage(); break;
210 case WID_S_RIGHT: if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
211 default: ResetPointerMode();
215 virtual void OnTick()
217 if (_pause_mode != PM_UNPAUSED) return;
219 if (this->ticker_scroll < TICKER_STOP) { // Scrolling text
220 this->ticker_scroll += COUNTER_STEP;
221 this->SetWidgetDirty(WID_S_MIDDLE);
224 if (this->reminder_timeout > REMINDER_STOP) { // Red blot to show there are new unread newsmessages
225 this->reminder_timeout -= COUNTER_STEP;
226 } else if (this->reminder_timeout < REMINDER_STOP) {
227 this->reminder_timeout = REMINDER_STOP;
228 this->SetWidgetDirty(WID_S_MIDDLE);
233 static const NWidgetPart _nested_main_status_widgets[] = {
234 NWidget(NWID_HORIZONTAL),
235 NWidget(WWT_PANEL, COLOUR_GREY, WID_S_LEFT), SetMinimalSize(140, 12), EndContainer(),
236 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_MIDDLE), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS), SetResize(1, 0),
237 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_RIGHT), SetMinimalSize(140, 12),
238 EndContainer(),
241 static const WindowDesc _main_status_desc(
242 WDP_MANUAL, 0, 0,
243 WC_STATUS_BAR, WC_NONE,
244 WDF_NO_FOCUS,
245 _nested_main_status_widgets, lengthof(_nested_main_status_widgets)
249 * Checks whether the news ticker is currently being used.
251 bool IsNewsTickerShown()
253 const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
254 return w != NULL && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
258 * Show our status bar.
260 void ShowStatusBar()
262 new StatusBarWindow(&_main_status_desc);