Add: INR currency (#8136)
[openttd-github.git] / src / signs_gui.cpp
blobea3de70085e9488fea47dac4cec6e8bc9d537bac
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file signs_gui.cpp The GUI for signs. */
10 #include "stdafx.h"
11 #include "company_gui.h"
12 #include "company_func.h"
13 #include "signs_base.h"
14 #include "signs_func.h"
15 #include "debug.h"
16 #include "command_func.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "map_func.h"
20 #include "viewport_func.h"
21 #include "querystring_gui.h"
22 #include "sortlist_type.h"
23 #include "stringfilter_type.h"
24 #include "string_func.h"
25 #include "core/geometry_func.hpp"
26 #include "hotkeys.h"
27 #include "transparency.h"
29 #include "widgets/sign_widget.h"
31 #include "table/strings.h"
32 #include "table/sprites.h"
34 #include "safeguards.h"
36 struct SignList {
37 /**
38 * A GUIList contains signs and uses a StringFilter for filtering.
40 typedef GUIList<const Sign *, StringFilter &> GUISignList;
42 GUISignList signs;
44 StringFilter string_filter; ///< The match string to be used when the GUIList is (re)-sorted.
45 static bool match_case; ///< Should case sensitive matching be used?
46 static char default_name[64]; ///< Default sign name, used if Sign::name is nullptr.
48 /**
49 * Creates a SignList with filtering disabled by default.
51 SignList() : string_filter(&match_case)
55 void BuildSignsList()
57 if (!this->signs.NeedRebuild()) return;
59 DEBUG(misc, 3, "Building sign list");
61 this->signs.clear();
63 for (const Sign *si : Sign::Iterate()) this->signs.push_back(si);
65 this->signs.SetFilterState(true);
66 this->FilterSignList();
67 this->signs.shrink_to_fit();
68 this->signs.RebuildDone();
71 /** Sort signs by their name */
72 static bool SignNameSorter(const Sign * const &a, const Sign * const &b)
74 /* Signs are very very rarely using the default text, but there can also be
75 * a lot of them. Therefore a worthwhile performance gain can be made by
76 * directly comparing Sign::name instead of going through the string
77 * system for each comparison. */
78 const char *a_name = a->name;
79 const char *b_name = b->name;
81 if (a_name == nullptr) a_name = SignList::default_name;
82 if (b_name == nullptr) b_name = SignList::default_name;
84 int r = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
86 return r != 0 ? r < 0 : (a->index < b->index);
89 void SortSignsList()
91 if (!this->signs.Sort(&SignNameSorter)) return;
94 /** Filter sign list by sign name */
95 static bool CDECL SignNameFilter(const Sign * const *a, StringFilter &filter)
97 /* Same performance benefit as above for sorting. */
98 const char *a_name = (*a)->name;
100 if (a_name == nullptr) a_name = SignList::default_name;
102 filter.ResetState();
103 filter.AddLine(a_name);
104 return filter.GetState();
107 /** Filter sign list excluding OWNER_DEITY */
108 static bool CDECL OwnerDeityFilter(const Sign * const *a, StringFilter &filter)
110 /* You should never be able to edit signs of owner DEITY */
111 return (*a)->owner != OWNER_DEITY;
114 /** Filter sign list by owner */
115 static bool CDECL OwnerVisibilityFilter(const Sign * const *a, StringFilter &filter)
117 assert(!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS));
118 /* Hide sign if non-own signs are hidden in the viewport */
119 return (*a)->owner == _local_company || (*a)->owner == OWNER_DEITY;
122 /** Filter out signs from the sign list that does not match the name filter */
123 void FilterSignList()
125 this->signs.Filter(&SignNameFilter, this->string_filter);
126 if (_game_mode != GM_EDITOR) this->signs.Filter(&OwnerDeityFilter, this->string_filter);
127 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS)) {
128 this->signs.Filter(&OwnerVisibilityFilter, this->string_filter);
133 bool SignList::match_case = false;
134 char SignList::default_name[64];
136 /** Enum referring to the Hotkeys in the sign list window */
137 enum SignListHotkeys {
138 SLHK_FOCUS_FILTER_BOX, ///< Focus the edit box for editing the filter string
141 struct SignListWindow : Window, SignList {
142 QueryString filter_editbox; ///< Filter editbox;
143 int text_offset; ///< Offset of the sign text relative to the left edge of the WID_SIL_LIST widget.
144 Scrollbar *vscroll;
146 SignListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc), filter_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
148 this->CreateNestedTree();
149 this->vscroll = this->GetScrollbar(WID_SIL_SCROLLBAR);
150 this->FinishInitNested(window_number);
151 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case);
153 /* Initialize the text edit widget */
154 this->querystrings[WID_SIL_FILTER_TEXT] = &this->filter_editbox;
155 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
157 /* Initialize the filtering variables */
158 this->SetFilterString("");
160 /* Create initial list. */
161 this->signs.ForceRebuild();
162 this->signs.ForceResort();
163 this->BuildSortSignList();
166 void OnInit() override
168 /* Default sign name, used if Sign::name is nullptr. */
169 GetString(SignList::default_name, STR_DEFAULT_SIGN_NAME, lastof(SignList::default_name));
170 this->signs.ForceResort();
171 this->SortSignsList();
172 this->SetDirty();
176 * This function sets the filter string of the sign list. The contents of
177 * the edit widget is not updated by this function. Depending on if the
178 * new string is zero-length or not the clear button is made
179 * disabled/enabled. The sign list is updated according to the new filter.
181 void SetFilterString(const char *new_filter_string)
183 /* check if there is a new filter string */
184 this->string_filter.SetFilterTerm(new_filter_string);
186 /* Rebuild the list of signs */
187 this->InvalidateData();
190 void OnPaint() override
192 if (!this->IsShaded() && this->signs.NeedRebuild()) this->BuildSortSignList();
193 this->DrawWidgets();
196 void DrawWidget(const Rect &r, int widget) const override
198 switch (widget) {
199 case WID_SIL_LIST: {
200 uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
201 /* No signs? */
202 if (this->vscroll->GetCount() == 0) {
203 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_STATION_LIST_NONE);
204 return;
207 bool rtl = _current_text_dir == TD_RTL;
208 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
209 uint icon_left = 4 + (rtl ? r.right - this->text_offset : r.left);
210 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
211 uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
213 /* At least one sign available. */
214 for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
215 const Sign *si = this->signs[i];
217 if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
219 SetDParam(0, si->index);
220 DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
221 y += this->resize.step_height;
223 break;
228 void SetStringParameters(int widget) const override
230 if (widget == WID_SIL_CAPTION) SetDParam(0, this->vscroll->GetCount());
233 void OnClick(Point pt, int widget, int click_count) override
235 switch (widget) {
236 case WID_SIL_LIST: {
237 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SIL_LIST, WD_FRAMERECT_TOP);
238 if (id_v == INT_MAX) return;
240 const Sign *si = this->signs[id_v];
241 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
242 break;
245 case WID_SIL_FILTER_ENTER_BTN:
246 if (this->signs.size() >= 1) {
247 const Sign *si = this->signs[0];
248 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
250 break;
252 case WID_SIL_FILTER_MATCH_CASE_BTN:
253 SignList::match_case = !SignList::match_case; // Toggle match case
254 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case); // Toggle button pushed state
255 this->InvalidateData(); // Rebuild the list of signs
256 break;
260 void OnResize() override
262 this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
265 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
267 switch (widget) {
268 case WID_SIL_LIST: {
269 Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
270 this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
271 resize->height = max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
272 Dimension d = {(uint)(this->text_offset + WD_FRAMETEXT_RIGHT), WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
273 *size = maxdim(*size, d);
274 break;
277 case WID_SIL_CAPTION:
278 SetDParamMaxValue(0, Sign::GetPoolSize(), 3);
279 *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
280 size->height += padding.height;
281 size->width += padding.width;
282 break;
286 EventState OnHotkey(int hotkey) override
288 switch (hotkey) {
289 case SLHK_FOCUS_FILTER_BOX:
290 this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
291 SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
292 break;
294 default:
295 return ES_NOT_HANDLED;
298 return ES_HANDLED;
301 void OnEditboxChanged(int widget) override
303 if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.text.buf);
306 void BuildSortSignList()
308 if (this->signs.NeedRebuild()) {
309 this->BuildSignsList();
310 this->vscroll->SetCount((uint)this->signs.size());
311 this->SetWidgetDirty(WID_SIL_CAPTION);
313 this->SortSignsList();
316 void OnHundredthTick() override
318 this->BuildSortSignList();
319 this->SetDirty();
323 * Some data on this window has become invalid.
324 * @param data Information about the changed data.
325 * @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.
327 void OnInvalidateData(int data = 0, bool gui_scope = true) override
329 /* When there is a filter string, we always need to rebuild the list even if
330 * the amount of signs in total is unchanged, as the subset of signs that is
331 * accepted by the filter might has changed. */
332 if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) { // New or deleted sign, changed visibility setting or there is a filter string
333 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
334 this->signs.ForceRebuild();
335 } else { // Change of sign contents while there is no filter string
336 this->signs.ForceResort();
340 static HotkeyList hotkeys;
344 * Handler for global hotkeys of the SignListWindow.
345 * @param hotkey Hotkey
346 * @return ES_HANDLED if hotkey was accepted.
348 static EventState SignListGlobalHotkeys(int hotkey)
350 if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
351 Window *w = ShowSignList();
352 if (w == nullptr) return ES_NOT_HANDLED;
353 return w->OnHotkey(hotkey);
356 static Hotkey signlist_hotkeys[] = {
357 Hotkey('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
358 HOTKEY_LIST_END
360 HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys);
362 static const NWidgetPart _nested_sign_list_widgets[] = {
363 NWidget(NWID_HORIZONTAL),
364 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
365 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SIL_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
366 NWidget(WWT_SHADEBOX, COLOUR_GREY),
367 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
368 NWidget(WWT_STICKYBOX, COLOUR_GREY),
369 EndContainer(),
370 NWidget(NWID_HORIZONTAL),
371 NWidget(NWID_VERTICAL),
372 NWidget(WWT_PANEL, COLOUR_GREY, WID_SIL_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
373 SetResize(1, 10), SetFill(1, 0), SetScrollbar(WID_SIL_SCROLLBAR), EndContainer(),
374 NWidget(NWID_HORIZONTAL),
375 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1),
376 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SIL_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
377 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
378 EndContainer(),
379 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SIL_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
380 EndContainer(),
381 EndContainer(),
382 NWidget(NWID_VERTICAL),
383 NWidget(NWID_VERTICAL), SetFill(0, 1),
384 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SIL_SCROLLBAR),
385 EndContainer(),
386 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
387 EndContainer(),
388 EndContainer(),
391 static WindowDesc _sign_list_desc(
392 WDP_AUTO, "list_signs", 358, 138,
393 WC_SIGN_LIST, WC_NONE,
395 _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets),
396 &SignListWindow::hotkeys
400 * Open the sign list window
402 * @return newly opened sign list window, or nullptr if the window could not be opened.
404 Window *ShowSignList()
406 return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
410 * Actually rename the sign.
411 * @param index the sign to rename.
412 * @param text the new name.
413 * @return true if the window will already be removed after returning.
415 static bool RenameSign(SignID index, const char *text)
417 bool remove = StrEmpty(text);
418 DoCommandP(0, index, 0, CMD_RENAME_SIGN | (StrEmpty(text) ? CMD_MSG(STR_ERROR_CAN_T_DELETE_SIGN) : CMD_MSG(STR_ERROR_CAN_T_CHANGE_SIGN_NAME)), nullptr, text);
419 return remove;
422 struct SignWindow : Window, SignList {
423 QueryString name_editbox;
424 SignID cur_sign;
426 SignWindow(WindowDesc *desc, const Sign *si) : Window(desc), name_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
428 this->querystrings[WID_QES_TEXT] = &this->name_editbox;
429 this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
430 this->name_editbox.cancel_button = WID_QES_CANCEL;
431 this->name_editbox.ok_button = WID_QES_OK;
433 this->InitNested(WN_QUERY_STRING_SIGN);
435 UpdateSignEditWindow(si);
436 this->SetFocusedWidget(WID_QES_TEXT);
439 void UpdateSignEditWindow(const Sign *si)
441 /* Display an empty string when the sign hasn't been edited yet */
442 if (si->name != nullptr) {
443 SetDParam(0, si->index);
444 this->name_editbox.text.Assign(STR_SIGN_NAME);
445 } else {
446 this->name_editbox.text.DeleteAll();
449 this->cur_sign = si->index;
451 this->SetWidgetDirty(WID_QES_TEXT);
452 this->SetFocusedWidget(WID_QES_TEXT);
456 * Returns a pointer to the (alphabetically) previous or next sign of the current sign.
457 * @param next false if the previous sign is wanted, true if the next sign is wanted
458 * @return pointer to the previous/next sign
460 const Sign *PrevNextSign(bool next)
462 /* Rebuild the sign list */
463 this->signs.ForceRebuild();
464 this->signs.NeedResort();
465 this->BuildSignsList();
466 this->SortSignsList();
468 /* Search through the list for the current sign, excluding
469 * - the first sign if we want the previous sign or
470 * - the last sign if we want the next sign */
471 size_t end = this->signs.size() - (next ? 1 : 0);
472 for (uint i = next ? 0 : 1; i < end; i++) {
473 if (this->cur_sign == this->signs[i]->index) {
474 /* We've found the current sign, so return the sign before/after it */
475 return this->signs[i + (next ? 1 : -1)];
478 /* If we haven't found the current sign by now, return the last/first sign */
479 return next ? this->signs.front() : this->signs.back();
482 void SetStringParameters(int widget) const override
484 switch (widget) {
485 case WID_QES_CAPTION:
486 SetDParam(0, this->name_editbox.caption);
487 break;
491 void OnClick(Point pt, int widget, int click_count) override
493 switch (widget) {
494 case WID_QES_PREVIOUS:
495 case WID_QES_NEXT: {
496 const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
498 /* Rebuild the sign list */
499 this->signs.ForceRebuild();
500 this->signs.NeedResort();
501 this->BuildSignsList();
502 this->SortSignsList();
504 /* Scroll to sign and reopen window */
505 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
506 UpdateSignEditWindow(si);
507 break;
510 case WID_QES_DELETE:
511 /* Only need to set the buffer to null, the rest is handled as the OK button */
512 RenameSign(this->cur_sign, "");
513 /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
514 break;
516 case WID_QES_OK:
517 if (RenameSign(this->cur_sign, this->name_editbox.text.buf)) break;
518 FALLTHROUGH;
520 case WID_QES_CANCEL:
521 delete this;
522 break;
527 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
528 NWidget(NWID_HORIZONTAL),
529 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
530 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QES_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
531 EndContainer(),
532 NWidget(WWT_PANEL, COLOUR_GREY),
533 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QES_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
534 EndContainer(),
535 NWidget(NWID_HORIZONTAL),
536 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
537 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
538 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
539 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
540 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
541 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
542 EndContainer(),
545 static WindowDesc _query_sign_edit_desc(
546 WDP_CENTER, "query_sign", 0, 0,
547 WC_QUERY_STRING, WC_NONE,
548 WDF_CONSTRUCTION,
549 _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
553 * Handle clicking on a sign.
554 * @param si The sign that was clicked on.
556 void HandleClickOnSign(const Sign *si)
558 if (_ctrl_pressed && (si->owner == _local_company || (si->owner == OWNER_DEITY && _game_mode == GM_EDITOR))) {
559 RenameSign(si->index, nullptr);
560 return;
562 ShowRenameSignWindow(si);
566 * Show the window to change the text of a sign.
567 * @param si The sign to show the window for.
569 void ShowRenameSignWindow(const Sign *si)
571 /* Delete all other edit windows */
572 DeleteWindowByClass(WC_QUERY_STRING);
574 new SignWindow(&_query_sign_edit_desc, si);
578 * Close the sign window associated with the given sign.
579 * @param sign The sign to close the window for.
581 void DeleteRenameSignWindow(SignID sign)
583 SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, WN_QUERY_STRING_SIGN));
585 if (w != nullptr && w->cur_sign == sign) delete w;