Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / signs_gui.cpp
blob300fe6ac6d134730278387113b9584477bc31e35
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 signs_gui.cpp The GUI for signs. */
12 #include "stdafx.h"
13 #include "company_gui.h"
14 #include "company_func.h"
15 #include "signs_base.h"
16 #include "signs_func.h"
17 #include "debug.h"
18 #include "command_func.h"
19 #include "strings_func.h"
20 #include "window_func.h"
21 #include "viewport_func.h"
22 #include "querystring_gui.h"
23 #include "sortlist_type.h"
24 #include "stringfilter_type.h"
25 #include "string_func.h"
26 #include "core/geometry_func.hpp"
27 #include "hotkeys.h"
28 #include "transparency.h"
29 #include "map/subcoord.h"
31 #include "widgets/sign_widget.h"
33 #include "table/strings.h"
34 #include "table/sprites.h"
36 struct SignList {
37 /**
38 * A GUIList contains signs and uses a StringFilter for filtering.
40 typedef GUIList<const Sign *, StringFilter &> GUISignList;
42 static const Sign *last_sign;
43 GUISignList signs;
45 StringFilter string_filter; ///< The match string to be used when the GUIList is (re)-sorted.
46 static bool match_case; ///< Should case sensitive matching be used?
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 const Sign *si;
64 FOR_ALL_SIGNS(si) *this->signs.Append() = si;
66 this->signs.SetFilterState(true);
67 this->FilterSignList();
68 this->signs.Compact();
69 this->signs.RebuildDone();
72 /** Sort signs by their name */
73 static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b)
75 static char buf_cache[64];
76 char buf[64];
78 SetDParam(0, (*a)->index);
79 GetString(buf, STR_SIGN_NAME, lastof(buf));
81 if (*b != last_sign) {
82 last_sign = *b;
83 SetDParam(0, (*b)->index);
84 GetString(buf_cache, STR_SIGN_NAME, lastof(buf_cache));
87 int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
89 return r != 0 ? r : ((*a)->index - (*b)->index);
92 void SortSignsList()
94 if (!this->signs.Sort(&SignNameSorter)) return;
96 /* Reset the name sorter sort cache */
97 this->last_sign = NULL;
100 /** Filter sign list by sign name */
101 static bool CDECL SignNameFilter(const Sign * const *a, StringFilter &filter)
103 /* Get sign string */
104 char buf1[MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH];
105 SetDParam(0, (*a)->index);
106 GetString(buf1, STR_SIGN_NAME, lastof(buf1));
108 filter.ResetState();
109 filter.AddLine(buf1);
110 return filter.GetState();
113 /** Filter sign list excluding OWNER_DEITY */
114 static bool CDECL OwnerDeityFilter(const Sign * const *a, StringFilter &filter)
116 /* You should never be able to edit signs of owner DEITY */
117 return (*a)->owner != OWNER_DEITY;
120 /** Filter sign list by owner */
121 static bool CDECL OwnerVisibilityFilter(const Sign * const *a, StringFilter &filter)
123 assert(!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS));
124 /* Hide sign if non-own signs are hidden in the viewport */
125 return (*a)->owner == _local_company || (*a)->owner == OWNER_DEITY;
128 /** Filter out signs from the sign list that does not match the name filter */
129 void FilterSignList()
131 this->signs.Filter(&SignNameFilter, this->string_filter);
132 if (_game_mode != GM_EDITOR) this->signs.Filter(&OwnerDeityFilter, this->string_filter);
133 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS)) {
134 this->signs.Filter(&OwnerVisibilityFilter, this->string_filter);
139 const Sign *SignList::last_sign = NULL;
140 bool SignList::match_case = false;
142 /** Enum referring to the Hotkeys in the sign list window */
143 enum SignListHotkeys {
144 SLHK_FOCUS_FILTER_BOX, ///< Focus the edit box for editing the filter string
147 struct SignListWindow : Window, SignList {
148 QueryString filter_editbox; ///< Filter editbox;
149 int text_offset; ///< Offset of the sign text relative to the left edge of the WID_SIL_LIST widget.
150 Scrollbar *vscroll;
152 SignListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc), filter_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
154 this->CreateNestedTree();
155 this->vscroll = this->GetScrollbar(WID_SIL_SCROLLBAR);
156 this->FinishInitNested(window_number);
157 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case);
159 /* Initialize the text edit widget */
160 this->querystrings[WID_SIL_FILTER_TEXT] = &this->filter_editbox;
161 this->filter_editbox.ok_button = WID_SIL_FILTER_ENTER_BTN;
162 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
164 /* Initialize the filtering variables */
165 this->SetFilterString("");
167 /* Create initial list. */
168 this->signs.ForceRebuild();
169 this->signs.ForceResort();
170 this->BuildSortSignList();
174 * This function sets the filter string of the sign list. The contents of
175 * the edit widget is not updated by this function. Depending on if the
176 * new string is zero-length or not the clear button is made
177 * disabled/enabled. The sign list is updated according to the new filter.
179 void SetFilterString(const char *new_filter_string)
181 /* check if there is a new filter string */
182 this->string_filter.SetFilterTerm(new_filter_string);
184 /* Rebuild the list of signs */
185 this->InvalidateData();
188 virtual void OnPaint()
190 if (this->signs.NeedRebuild()) this->BuildSortSignList();
191 this->DrawWidgets();
194 virtual void DrawWidget(const Rect &r, int widget) const
196 switch (widget) {
197 case WID_SIL_LIST: {
198 uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
199 /* No signs? */
200 if (this->vscroll->GetCount() == 0) {
201 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_STATION_LIST_NONE);
202 return;
205 bool rtl = _current_text_dir == TD_RTL;
206 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
207 uint icon_left = 4 + (rtl ? r.right - this->text_offset : r.left);
208 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
209 uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
211 /* At least one sign available. */
212 for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
213 const Sign *si = this->signs[i];
215 if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
217 SetDParam(0, si->index);
218 DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
219 y += this->resize.step_height;
221 break;
226 virtual void SetStringParameters(int widget) const
228 if (widget == WID_SIL_CAPTION) SetDParam(0, this->vscroll->GetCount());
231 virtual void OnClick(Point pt, int widget, int click_count)
233 switch (widget) {
234 case WID_SIL_LIST: {
235 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SIL_LIST, WD_FRAMERECT_TOP);
236 if (id_v == INT_MAX) return;
238 const Sign *si = this->signs[id_v];
239 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
240 break;
243 case WID_SIL_FILTER_ENTER_BTN:
244 if (this->signs.Length() >= 1) {
245 const Sign *si = this->signs[0];
246 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
248 break;
250 case WID_SIL_FILTER_MATCH_CASE_BTN:
251 SignList::match_case = !SignList::match_case; // Toggle match case
252 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case); // Toggle button pushed state
253 this->InvalidateData(); // Rebuild the list of signs
254 break;
258 virtual void OnResize()
260 this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
263 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
265 switch (widget) {
266 case WID_SIL_LIST: {
267 Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
268 this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
269 resize->height = max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
270 Dimension d = {this->text_offset + WD_FRAMETEXT_RIGHT, WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
271 *size = maxdim(*size, d);
272 break;
275 case WID_SIL_CAPTION:
276 SetDParamMaxValue(0, Sign::GetPoolSize(), 3);
277 *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
278 size->height += padding.height;
279 size->width += padding.width;
280 break;
284 virtual EventState OnHotkey(int hotkey)
286 switch (hotkey) {
287 case SLHK_FOCUS_FILTER_BOX:
288 this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
289 SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
290 break;
292 default:
293 return ES_NOT_HANDLED;
296 return ES_HANDLED;
299 virtual void OnEditboxChanged(int widget)
301 if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.text.buf);
304 void BuildSortSignList()
306 if (this->signs.NeedRebuild()) {
307 this->BuildSignsList();
308 this->vscroll->SetCount(this->signs.Length());
309 this->SetWidgetDirty(WID_SIL_CAPTION);
311 this->SortSignsList();
314 virtual void OnHundredthTick()
316 this->BuildSortSignList();
317 this->SetDirty();
321 * Some data on this window has become invalid.
322 * @param data Information about the changed data.
323 * @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.
325 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
327 /* When there is a filter string, we always need to rebuild the list even if
328 * the amount of signs in total is unchanged, as the subset of signs that is
329 * accepted by the filter might has changed. */
330 if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) { // New or deleted sign, changed visibility setting or there is a filter string
331 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
332 this->signs.ForceRebuild();
333 } else { // Change of sign contents while there is no filter string
334 this->signs.ForceResort();
338 static HotkeyList hotkeys;
342 * Handler for global hotkeys of the SignListWindow.
343 * @param hotkey Hotkey
344 * @return ES_HANDLED if hotkey was accepted.
346 static EventState SignListGlobalHotkeys(int hotkey)
348 if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
349 Window *w = ShowSignList();
350 if (w == NULL) return ES_NOT_HANDLED;
351 return w->OnHotkey(hotkey);
354 static Hotkey signlist_hotkeys[] = {
355 Hotkey('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
356 HOTKEY_LIST_END
358 HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys);
360 static const NWidgetPart _nested_sign_list_widgets[] = {
361 NWidget(NWID_HORIZONTAL),
362 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
363 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SIL_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
364 NWidget(WWT_SHADEBOX, COLOUR_GREY),
365 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
366 NWidget(WWT_STICKYBOX, COLOUR_GREY),
367 EndContainer(),
368 NWidget(NWID_HORIZONTAL),
369 NWidget(NWID_VERTICAL),
370 NWidget(WWT_PANEL, COLOUR_GREY, WID_SIL_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
371 SetResize(1, 10), SetFill(1, 0), SetScrollbar(WID_SIL_SCROLLBAR), EndContainer(),
372 NWidget(NWID_HORIZONTAL),
373 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1),
374 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SIL_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
375 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
376 EndContainer(),
377 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SIL_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
378 EndContainer(),
379 EndContainer(),
380 NWidget(NWID_VERTICAL),
381 NWidget(NWID_VERTICAL), SetFill(0, 1),
382 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SIL_SCROLLBAR),
383 EndContainer(),
384 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
385 EndContainer(),
386 EndContainer(),
389 static WindowDesc _sign_list_desc(
390 WDP_AUTO, "list_signs", 358, 138,
391 WC_SIGN_LIST, WC_NONE,
393 _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets),
394 &SignListWindow::hotkeys
398 * Open the sign list window
400 * @return newly opened sign list window, or NULL if the window could not be opened.
402 Window *ShowSignList()
404 return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
408 * Actually rename the sign.
409 * @param index the sign to rename.
410 * @param text the new name.
411 * @return true if the window will already be removed after returning.
413 static bool RenameSign(SignID index, const char *text)
415 bool remove = StrEmpty(text);
416 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)), NULL, text);
417 return remove;
420 struct SignWindow : Window, SignList {
421 QueryString name_editbox;
422 SignID cur_sign;
424 SignWindow(WindowDesc *desc, const Sign *si) : Window(desc), name_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
426 this->querystrings[WID_QES_TEXT] = &this->name_editbox;
427 this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
428 this->name_editbox.cancel_button = WID_QES_CANCEL;
429 this->name_editbox.ok_button = WID_QES_OK;
431 this->InitNested(WN_QUERY_STRING_SIGN);
433 UpdateSignEditWindow(si);
434 this->SetFocusedWidget(WID_QES_TEXT);
437 void UpdateSignEditWindow(const Sign *si)
439 /* Display an empty string when the sign hasn't been edited yet */
440 if (si->name != NULL) {
441 SetDParam(0, si->index);
442 this->name_editbox.text.Assign(STR_SIGN_NAME);
443 } else {
444 this->name_editbox.text.DeleteAll();
447 this->cur_sign = si->index;
449 this->SetWidgetDirty(WID_QES_TEXT);
450 this->SetFocusedWidget(WID_QES_TEXT);
454 * Returns a pointer to the (alphabetically) previous or next sign of the current sign.
455 * @param next false if the previous sign is wanted, true if the next sign is wanted
456 * @return pointer to the previous/next sign
458 const Sign *PrevNextSign(bool next)
460 /* Rebuild the sign list */
461 this->signs.ForceRebuild();
462 this->signs.NeedResort();
463 this->BuildSignsList();
464 this->SortSignsList();
466 /* Search through the list for the current sign, excluding
467 * - the first sign if we want the previous sign or
468 * - the last sign if we want the next sign */
469 uint end = this->signs.Length() - (next ? 1 : 0);
470 for (uint i = next ? 0 : 1; i < end; i++) {
471 if (this->cur_sign == this->signs[i]->index) {
472 /* We've found the current sign, so return the sign before/after it */
473 return this->signs[i + (next ? 1 : -1)];
476 /* If we haven't found the current sign by now, return the last/first sign */
477 return this->signs[next ? 0 : this->signs.Length() - 1];
480 virtual void SetStringParameters(int widget) const
482 switch (widget) {
483 case WID_QES_CAPTION:
484 SetDParam(0, this->name_editbox.caption);
485 break;
489 virtual void OnClick(Point pt, int widget, int click_count)
491 switch (widget) {
492 case WID_QES_PREVIOUS:
493 case WID_QES_NEXT: {
494 const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
496 /* Rebuild the sign list */
497 this->signs.ForceRebuild();
498 this->signs.NeedResort();
499 this->BuildSignsList();
500 this->SortSignsList();
502 /* Scroll to sign and reopen window */
503 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
504 UpdateSignEditWindow(si);
505 break;
508 case WID_QES_DELETE:
509 /* Only need to set the buffer to null, the rest is handled as the OK button */
510 RenameSign(this->cur_sign, "");
511 /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
512 break;
514 case WID_QES_OK:
515 if (RenameSign(this->cur_sign, this->name_editbox.text.buf)) break;
516 /* FALL THROUGH */
518 case WID_QES_CANCEL:
519 delete this;
520 break;
525 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
526 NWidget(NWID_HORIZONTAL),
527 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
528 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QES_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
529 EndContainer(),
530 NWidget(WWT_PANEL, COLOUR_GREY),
531 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QES_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
532 EndContainer(),
533 NWidget(NWID_HORIZONTAL),
534 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
535 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
536 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
537 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
538 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
539 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
540 EndContainer(),
543 static WindowDesc _query_sign_edit_desc(
544 WDP_CENTER, "query_sign", 0, 0,
545 WC_QUERY_STRING, WC_NONE,
546 WDF_CONSTRUCTION,
547 _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
551 * Handle clicking on a sign.
552 * @param si The sign that was clicked on.
554 void HandleClickOnSign(const Sign *si)
556 if (_ctrl_pressed && (si->owner == _local_company || (si->owner == OWNER_DEITY && _game_mode == GM_EDITOR))) {
557 RenameSign(si->index, NULL);
558 return;
560 ShowRenameSignWindow(si);
564 * Show the window to change the text of a sign.
565 * @param si The sign to show the window for.
567 void ShowRenameSignWindow(const Sign *si)
569 /* Delete all other edit windows */
570 DeleteWindowByClass(WC_QUERY_STRING);
572 new SignWindow(&_query_sign_edit_desc, si);
576 * Close the sign window associated with the given sign.
577 * @param sign The sign to close the window for.
579 void DeleteRenameSignWindow(SignID sign)
581 SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, WN_QUERY_STRING_SIGN));
583 if (w != NULL && w->cur_sign == sign) delete w;