Translations update
[openttd/fttd.git] / src / signs_gui.cpp
blob1717432c8a0b9f11232e93baada417f94fa5c190
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.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 /** Filter function for the sign list. */
37 static bool SignFilter (const Sign *sign, StringFilter *filter)
39 Owner owner = sign->owner;
40 if (owner == OWNER_DEITY) {
41 /* You should never be able to edit signs of owner DEITY. */
42 if (_game_mode != GM_EDITOR) return false;
43 } else if (owner != _local_company) {
44 /* Hide sign if non-own signs are hidden in the viewport. */
45 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS)) {
46 return false;
50 /* Get sign string */
51 char buf[MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH];
52 SetDParam (0, sign->index);
53 GetString (buf, STR_SIGN_NAME);
55 filter->ResetState();
56 filter->AddLine (buf);
57 return filter->GetState();
60 struct SignList {
61 /**
62 * A GUIList contains signs and uses a StringFilter for filtering.
64 typedef GUIList <const Sign *> GUISignList;
66 static const Sign *last_sign;
67 GUISignList signs;
69 StringFilter string_filter; ///< The match string to be used when the GUIList is (re)-sorted.
70 static bool match_case; ///< Should case sensitive matching be used?
72 /**
73 * Creates a SignList with filtering disabled by default.
75 SignList() : string_filter(&match_case)
79 void BuildSignsList (void);
81 /** Sort signs by their name */
82 static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b)
84 static char buf_cache[64];
85 char buf[64];
87 SetDParam(0, (*a)->index);
88 GetString (buf, STR_SIGN_NAME);
90 if (*b != last_sign) {
91 last_sign = *b;
92 SetDParam(0, (*b)->index);
93 GetString (buf_cache, STR_SIGN_NAME);
96 int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
98 return r != 0 ? r : ((*a)->index - (*b)->index);
101 void SortSignsList()
103 if (!this->signs.Sort(&SignNameSorter)) return;
105 /* Reset the name sorter sort cache */
106 this->last_sign = NULL;
110 void SignList::BuildSignsList (void)
112 if (!this->signs.NeedRebuild()) return;
114 DEBUG(misc, 3, "Building sign list");
116 this->signs.Clear();
118 const Sign *si;
119 FOR_ALL_SIGNS(si) {
120 if (SignFilter (si, &this->string_filter)) {
121 *this->signs.Append() = si;
125 this->signs.SetFilterState (true);
126 this->signs.Compact();
127 this->signs.RebuildDone();
130 const Sign *SignList::last_sign = NULL;
131 bool SignList::match_case = false;
133 /** Enum referring to the Hotkeys in the sign list window */
134 enum SignListHotkeys {
135 SLHK_FOCUS_FILTER_BOX, ///< Focus the edit box for editing the filter string
138 struct SignListWindow : Window, SignList {
139 QueryStringC <MAX_LENGTH_SIGN_NAME_CHARS> filter_editbox; ///< Filter editbox;
140 int text_offset; ///< Offset of the sign text relative to the left edge of the WID_SIL_LIST widget.
141 Scrollbar *vscroll;
143 SignListWindow (const WindowDesc *desc, WindowNumber window_number) :
144 Window (desc), SignList(), filter_editbox (),
145 text_offset (0), vscroll (NULL)
147 this->CreateNestedTree();
148 this->vscroll = this->GetScrollbar(WID_SIL_SCROLLBAR);
149 this->InitNested(window_number);
150 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case);
152 /* Initialize the text edit widget */
153 this->querystrings[WID_SIL_FILTER_TEXT] = &this->filter_editbox;
154 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
156 /* Initialize the filtering variables */
157 this->SetFilterString("");
159 /* Create initial list. */
160 this->signs.ForceRebuild();
161 this->signs.ForceResort();
162 this->BuildSortSignList();
166 * This function sets the filter string of the sign list. The contents of
167 * the edit widget is not updated by this function. Depending on if the
168 * new string is zero-length or not the clear button is made
169 * disabled/enabled. The sign list is updated according to the new filter.
171 void SetFilterString(const char *new_filter_string)
173 /* check if there is a new filter string */
174 this->string_filter.SetFilterTerm(new_filter_string);
176 /* Rebuild the list of signs */
177 this->InvalidateData();
180 void OnPaint (BlitArea *dpi) OVERRIDE
182 if (this->signs.NeedRebuild()) this->BuildSortSignList();
183 this->DrawWidgets (dpi);
186 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
188 switch (widget) {
189 case WID_SIL_LIST: {
190 uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
191 /* No signs? */
192 if (this->vscroll->GetCount() == 0) {
193 DrawString (dpi, r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_STATION_LIST_NONE);
194 return;
197 bool rtl = _current_text_dir == TD_RTL;
198 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
199 uint icon_left = 4 + (rtl ? r.right - this->text_offset : r.left);
200 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
201 uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
203 /* At least one sign available. */
204 for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
205 const Sign *si = this->signs[i];
207 if (si->owner != OWNER_NONE) DrawCompanyIcon (dpi, si->owner, icon_left, y + sprite_offset_y);
209 SetDParam(0, si->index);
210 DrawString (dpi, text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
211 y += this->resize.step_height;
213 break;
218 virtual void SetStringParameters(int widget) const
220 if (widget == WID_SIL_CAPTION) SetDParam(0, this->vscroll->GetCount());
223 virtual void OnClick(Point pt, int widget, int click_count)
225 switch (widget) {
226 case WID_SIL_LIST: {
227 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SIL_LIST, WD_FRAMERECT_TOP);
228 if (id_v == INT_MAX) return;
230 const Sign *si = this->signs[id_v];
231 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
232 break;
235 case WID_SIL_FILTER_ENTER_BTN:
236 if (this->signs.Length() >= 1) {
237 const Sign *si = this->signs[0];
238 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
240 break;
242 case WID_SIL_FILTER_MATCH_CASE_BTN:
243 SignList::match_case = !SignList::match_case; // Toggle match case
244 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case); // Toggle button pushed state
245 this->InvalidateData(); // Rebuild the list of signs
246 break;
250 virtual void OnResize()
252 this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
255 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
257 switch (widget) {
258 case WID_SIL_LIST: {
259 Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
260 this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
261 resize->height = max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
262 Dimension d = {(uint)(this->text_offset + WD_FRAMETEXT_RIGHT), WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
263 *size = maxdim(*size, d);
264 break;
267 case WID_SIL_CAPTION:
268 SetDParamMaxValue(0, Sign::GetPoolSize(), 3);
269 *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
270 size->height += padding.height;
271 size->width += padding.width;
272 break;
276 virtual EventState OnHotkey(int hotkey)
278 switch (hotkey) {
279 case SLHK_FOCUS_FILTER_BOX:
280 this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
281 SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
282 break;
284 default:
285 return ES_NOT_HANDLED;
288 return ES_HANDLED;
291 virtual void OnEditboxChanged(int widget)
293 if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.GetText());
296 void BuildSortSignList()
298 if (this->signs.NeedRebuild()) {
299 this->BuildSignsList();
300 this->vscroll->SetCount(this->signs.Length());
301 this->SetWidgetDirty(WID_SIL_CAPTION);
303 this->SortSignsList();
306 virtual void OnHundredthTick()
308 this->BuildSortSignList();
309 this->SetDirty();
313 * Some data on this window has become invalid.
314 * @param data Information about the changed data.
315 * @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.
317 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
319 /* When there is a filter string, we always need to rebuild the list even if
320 * the amount of signs in total is unchanged, as the subset of signs that is
321 * accepted by the filter might has changed. */
322 if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) { // New or deleted sign, changed visibility setting or there is a filter string
323 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
324 this->signs.ForceRebuild();
325 } else { // Change of sign contents while there is no filter string
326 this->signs.ForceResort();
330 static HotkeyList hotkeys;
334 * Handler for global hotkeys of the SignListWindow.
335 * @param hotkey Hotkey
336 * @return ES_HANDLED if hotkey was accepted.
338 static EventState SignListGlobalHotkeys(int hotkey)
340 if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
341 Window *w = ShowSignList();
342 if (w == NULL) return ES_NOT_HANDLED;
343 return w->OnHotkey(hotkey);
346 static const Hotkey signlist_hotkeys[] = {
347 Hotkey ("focus_filter_box", SLHK_FOCUS_FILTER_BOX, 'F'),
349 HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys);
351 static const NWidgetPart _nested_sign_list_widgets[] = {
352 NWidget(NWID_HORIZONTAL),
353 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
354 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SIL_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
355 NWidget(WWT_SHADEBOX, COLOUR_GREY),
356 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
357 NWidget(WWT_STICKYBOX, COLOUR_GREY),
358 EndContainer(),
359 NWidget(NWID_HORIZONTAL),
360 NWidget(NWID_VERTICAL),
361 NWidget(WWT_PANEL, COLOUR_GREY, WID_SIL_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
362 SetResize(1, 10), SetFill(1, 0), SetScrollbar(WID_SIL_SCROLLBAR), EndContainer(),
363 NWidget(NWID_HORIZONTAL),
364 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1),
365 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SIL_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
366 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
367 EndContainer(),
368 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SIL_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
369 EndContainer(),
370 EndContainer(),
371 NWidget(NWID_VERTICAL),
372 NWidget(NWID_VERTICAL), SetFill(0, 1),
373 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SIL_SCROLLBAR),
374 EndContainer(),
375 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
376 EndContainer(),
377 EndContainer(),
380 static WindowDesc::Prefs _sign_list_prefs ("list_signs");
382 static const WindowDesc _sign_list_desc(
383 WDP_AUTO, 358, 138,
384 WC_SIGN_LIST, WC_NONE,
386 _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets),
387 &_sign_list_prefs, &SignListWindow::hotkeys
391 * Open the sign list window
393 * @return newly opened sign list window, or NULL if the window could not be opened.
395 Window *ShowSignList()
397 return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
400 StringID GetErrRenameSign (TileIndex tile, uint32 p1, uint32 p2, const char *text)
402 return StrEmpty(text) ? STR_ERROR_CAN_T_DELETE_SIGN : STR_ERROR_CAN_T_CHANGE_SIGN_NAME;
406 * Actually rename the sign.
407 * @param index the sign to rename.
408 * @param text the new name.
409 * @return true if the window will already be removed after returning.
411 static bool RenameSign(SignID index, const char *text)
413 bool remove = StrEmpty(text);
414 DoCommandP(0, index, 0, CMD_RENAME_SIGN, text);
415 return remove;
418 struct SignWindow : Window, SignList {
419 QueryStringC <MAX_LENGTH_SIGN_NAME_CHARS> name_editbox;
420 SignID cur_sign;
422 SignWindow (const WindowDesc *desc, const Sign *si) :
423 Window (desc), SignList(), name_editbox (), cur_sign (0)
425 this->querystrings[WID_QES_TEXT] = &this->name_editbox;
426 this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
427 this->name_editbox.cancel_button = WID_QES_CANCEL;
428 this->name_editbox.ok_button = WID_QES_OK;
430 this->InitNested(WN_QUERY_STRING_SIGN);
432 UpdateSignEditWindow(si);
433 this->SetFocusedWidget(WID_QES_TEXT);
436 void UpdateSignEditWindow(const Sign *si)
438 /* Display an empty string when the sign hasn't been edited yet */
439 if (si->name != NULL) {
440 SetDParam(0, si->index);
441 this->name_editbox.Assign(STR_SIGN_NAME);
442 } else {
443 this->name_editbox.DeleteAll();
446 this->cur_sign = si->index;
448 this->SetWidgetDirty(WID_QES_TEXT);
449 this->SetFocusedWidget(WID_QES_TEXT);
453 * Returns a pointer to the (alphabetically) previous or next sign of the current sign.
454 * @param next false if the previous sign is wanted, true if the next sign is wanted
455 * @return pointer to the previous/next sign
457 const Sign *PrevNextSign(bool next)
459 /* Rebuild the sign list */
460 this->signs.ForceRebuild();
461 this->signs.NeedResort();
462 this->BuildSignsList();
463 this->SortSignsList();
465 /* Search through the list for the current sign, excluding
466 * - the first sign if we want the previous sign or
467 * - the last sign if we want the next sign */
468 uint end = this->signs.Length() - (next ? 1 : 0);
469 for (uint i = next ? 0 : 1; i < end; i++) {
470 if (this->cur_sign == this->signs[i]->index) {
471 /* We've found the current sign, so return the sign before/after it */
472 return this->signs[i + (next ? 1 : -1)];
475 /* If we haven't found the current sign by now, return the last/first sign */
476 return this->signs[next ? 0 : this->signs.Length() - 1];
479 virtual void SetStringParameters(int widget) const
481 switch (widget) {
482 case WID_QES_CAPTION:
483 SetDParam(0, this->name_editbox.caption);
484 break;
488 virtual void OnClick(Point pt, int widget, int click_count)
490 switch (widget) {
491 case WID_QES_PREVIOUS:
492 case WID_QES_NEXT: {
493 const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
495 /* Rebuild the sign list */
496 this->signs.ForceRebuild();
497 this->signs.NeedResort();
498 this->BuildSignsList();
499 this->SortSignsList();
501 /* Scroll to sign and reopen window */
502 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
503 UpdateSignEditWindow(si);
504 break;
507 case WID_QES_DELETE:
508 /* Only need to set the buffer to null, the rest is handled as the OK button */
509 RenameSign(this->cur_sign, "");
510 /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
511 break;
513 case WID_QES_OK:
514 if (RenameSign(this->cur_sign, this->name_editbox.GetText())) break;
515 /* FALL THROUGH */
517 case WID_QES_CANCEL:
518 this->Delete();
519 break;
524 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
525 NWidget(NWID_HORIZONTAL),
526 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
527 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QES_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
528 EndContainer(),
529 NWidget(WWT_PANEL, COLOUR_GREY),
530 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QES_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
531 EndContainer(),
532 NWidget(NWID_HORIZONTAL),
533 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
534 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
535 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
536 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
537 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
538 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
539 EndContainer(),
542 static WindowDesc::Prefs _query_sign_edit_prefs ("query_sign");
544 static const WindowDesc _query_sign_edit_desc(
545 WDP_CENTER, 0, 0,
546 WC_QUERY_STRING, WC_NONE,
547 WDF_CONSTRUCTION,
548 _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets),
549 &_query_sign_edit_prefs
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, NULL);
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 != NULL && w->cur_sign == sign) w->Delete();