OpenTTD Source: src/window.cpp Source File

OpenTTD Source 15.3

window.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "company_func.h"
12#include "gfx_func.h"
13#include "console_func.h"
14#include "console_gui.h"
15#include "viewport_func.h"
16#include "progress.h"
17#include "blitter/factory.hpp"
18#include "zoom_func.h"
19#include "vehicle_base.h"
20#include "depot_func.h"
21#include "window_func.h"
22#include "tilehighlight_func.h"
23#include "network/network.h"
24#include "querystring_gui.h"
25#include "strings_func.h"
26#include "settings_type.h"
27#include "settings_func.h"
28#include "ini_type.h"
29#include "newgrf_debug.h"
30#include "hotkeys.h"
31#include "toolbar_gui.h"
32#include "statusbar_gui.h"
33#include "error.h"
34#include "game/game.hpp"
35#include "video/video_driver.hpp"
36#include "framerate_type.h"
37#include "network/network_func.h"
38#include "news_func.h"
39#include "sound_func.h"
40#include "timer/timer.h"
41#include "timer/timer_window.h"
42
43#include "table/strings.h"
44
45#include "safeguards.h"
46
48 enum ViewportAutoscrolling : uint8_t {
49 VA_DISABLED,
50 VA_MAIN_VIEWPORT_FULLSCREEN,
51 VA_MAIN_VIEWPORT,
52 VA_EVERY_VIEWPORT,
53};
54
55 static Point _drag_delta;
56 static Window *_mouseover_last_w = nullptr;
57 static Window *_last_scroll_window = nullptr;
58
60WindowList _z_windows;
61
63/* static */ std::vector<Window *> Window::closed_windows;
64
68/* static */ void Window::DeleteClosedWindows()
69{
70 for (Window *w : Window::closed_windows) delete w;
71 Window::closed_windows.clear();
72
73 /* Remove dead entries from the window list */
74 _z_windows.remove(nullptr);
75}
76
78 bool _window_highlight_colour = false;
79
80/*
81 * Window that currently has focus. - The main purpose is to generate
82 * #FocusLost events, not to give next window in z-order focus when a
83 * window is closed.
84 */
85 Window *_focused_window;
86
87 Point _cursorpos_drag_start;
88
89int _scrollbar_start_pos;
90int _scrollbar_size;
91uint8_t _scroller_click_timeout = 0;
92
93 bool _scrolling_viewport;
94 bool _mouse_hovering;
95
96 SpecialMouseMode _special_mouse_mode;
97
102std::vector<WindowDesc*> *_window_descs = nullptr;
103
105std::string _windows_file;
106
108WindowDesc::WindowDesc(WindowPosition def_pos, std::string_view ini_key, int16_t def_width_trad, int16_t def_height_trad,
109 WindowClass window_class, WindowClass parent_class, WindowDefaultFlags flags,
110 const std::span<const NWidgetPart> nwid_parts, HotkeyList *hotkeys,
111 const std::source_location location) :
112 source_location(location),
113 default_pos(def_pos),
114 cls(window_class),
115 parent_cls(parent_class),
116 ini_key(ini_key),
117 flags(flags),
118 nwid_parts(nwid_parts),
119 hotkeys(hotkeys),
120 default_width_trad(def_width_trad),
121 default_height_trad(def_height_trad)
122{
123 if (_window_descs == nullptr) _window_descs = new std::vector<WindowDesc*>();
124 _window_descs->push_back(this);
125}
126
127WindowDesc::~WindowDesc()
128{
129 _window_descs->erase(std::ranges::find(*_window_descs, this));
130}
131
137int16_t WindowDesc::GetDefaultWidth() const
138{
139 return this->pref_width != 0 ? this->pref_width : ScaleGUITrad(this->default_width_trad);
140}
141
147int16_t WindowDesc::GetDefaultHeight() const
148{
149 return this->pref_height != 0 ? this->pref_height : ScaleGUITrad(this->default_height_trad);
150}
151
155 void WindowDesc::LoadFromConfig()
156{
157 IniFile ini;
158 ini.LoadFromDisk(_windows_file, NO_DIRECTORY);
159 for (WindowDesc *wd : *_window_descs) {
160 if (wd->ini_key.empty()) continue;
161 IniLoadWindowSettings(ini, wd->ini_key, wd);
162 }
163}
164
168 static bool DescSorter(WindowDesc * const &a, WindowDesc * const &b)
169{
170 return a->ini_key < b->ini_key;
171}
172
176 void WindowDesc::SaveToConfig()
177{
178 /* Sort the stuff to get a nice ini file on first write */
179 std::sort(_window_descs->begin(), _window_descs->end(), DescSorter);
180
181 IniFile ini;
182 ini.LoadFromDisk(_windows_file, NO_DIRECTORY);
183 for (WindowDesc *wd : *_window_descs) {
184 if (wd->ini_key.empty()) continue;
185 IniSaveWindowSettings(ini, wd->ini_key, wd);
186 }
187 ini.SaveToDisk(_windows_file);
188}
189
193 void Window::ApplyDefaults()
194{
195 if (this->nested_root != nullptr && this->nested_root->GetWidgetOfType(WWT_STICKYBOX) != nullptr) {
196 if (this->window_desc.pref_sticky) this->flags.Set(WindowFlag::Sticky);
197 } else {
198 /* There is no stickybox; clear the preference in case someone tried to be funny */
199 this->window_desc.pref_sticky = false;
200 }
201}
202
212 int Window::GetRowFromWidget(int clickpos, WidgetID widget, int padding, int line_height) const
213{
214 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
215 if (line_height < 0) line_height = wid->resize_y;
216 if (clickpos < wid->pos_y + padding) return INT_MAX;
217 return (clickpos - wid->pos_y - padding) / line_height;
218}
219
223 void Window::DisableAllWidgetHighlight()
224{
225 for (auto &pair : this->widget_lookup) {
226 NWidgetBase *nwid = pair.second;
227 if (nwid->IsHighlighted()) {
228 nwid->SetHighlighted(TC_INVALID);
229 nwid->SetDirty(this);
230 }
231 }
232
233 this->flags.Reset(WindowFlag::Highlighted);
234}
235
241 void Window::SetWidgetHighlight(WidgetID widget_index, TextColour highlighted_colour)
242{
243 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
244 if (nwid == nullptr) return;
245
246 nwid->SetHighlighted(highlighted_colour);
247 nwid->SetDirty(this);
248
249 if (highlighted_colour != TC_INVALID) {
250 /* If we set a highlight, the window has a highlight */
251 this->flags.Set(WindowFlag::Highlighted);
252 } else {
253 /* If we disable a highlight, check all widgets if anyone still has a highlight */
254 bool valid = false;
255 for (const auto &pair : this->widget_lookup) {
256 nwid = pair.second;
257 if (!nwid->IsHighlighted()) continue;
258
259 valid = true;
260 }
261 /* If nobody has a highlight, disable the flag on the window */
262 if (!valid) this->flags.Reset(WindowFlag::Highlighted);
263 }
264}
265
271 bool Window::IsWidgetHighlighted(WidgetID widget_index) const
272{
273 const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
274 if (nwid == nullptr) return false;
275
276 return nwid->IsHighlighted();
277}
278
286 void Window::OnDropdownClose(Point pt, WidgetID widget, int index, int click_result, bool instant_close)
287{
288 if (widget < 0) return;
289
290 /* Many dropdown selections depend on the position of the main toolbar,
291 * so if it doesn't exist (e.g. the end screen has appeared), just skip the instant close behaviour. */
292 if (instant_close && FindWindowById(WC_MAIN_TOOLBAR, 0) != nullptr) {
293 /* Send event for selected option if we're still
294 * on the parent button of the dropdown (behaviour of the dropdowns in the main toolbar). */
295 if (GetWidgetFromPos(this, pt.x, pt.y) == widget) {
296 this->OnDropdownSelect(widget, index, click_result);
297 }
298 }
299
300 /* Raise the dropdown button */
301 NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget);
302 if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
303 nwi2->disp_flags.Reset(NWidgetDisplayFlag::DropdownActive);
304 } else {
305 this->RaiseWidget(widget);
306 }
307 this->SetWidgetDirty(widget);
308}
309
315 const Scrollbar *Window::GetScrollbar(WidgetID widnum) const
316{
317 return this->GetWidget<NWidgetScrollbar>(widnum);
318}
319
325 Scrollbar *Window::GetScrollbar(WidgetID widnum)
326{
327 return this->GetWidget<NWidgetScrollbar>(widnum);
328}
329
335 const QueryString *Window::GetQueryString(WidgetID widnum) const
336{
337 auto query = this->querystrings.find(widnum);
338 return query != this->querystrings.end() ? query->second : nullptr;
339}
340
346 QueryString *Window::GetQueryString(WidgetID widnum)
347{
348 auto query = this->querystrings.find(widnum);
349 return query != this->querystrings.end() ? query->second : nullptr;
350}
351
355 void Window::UpdateQueryStringSize()
356{
357 for (auto &qs : this->querystrings) {
358 qs.second->text.UpdateSize();
359 }
360}
361
366/* virtual */ const Textbuf *Window::GetFocusedTextbuf() const
367{
368 if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
369 return &this->GetQueryString(this->nested_focus->GetIndex())->text;
370 }
371
372 return nullptr;
373}
374
379/* virtual */ Point Window::GetCaretPosition() const
380{
381 if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX && !this->querystrings.empty()) {
382 return this->GetQueryString(this->nested_focus->GetIndex())->GetCaretPosition(this, this->nested_focus->GetIndex());
383 }
384
385 Point pt = {0, 0};
386 return pt;
387}
388
395/* virtual */ Rect Window::GetTextBoundingRect(size_t from, size_t to) const
396{
397 if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
398 return this->GetQueryString(this->nested_focus->GetIndex())->GetBoundingRect(this, this->nested_focus->GetIndex(), from, to);
399 }
400
401 Rect r = {0, 0, 0, 0};
402 return r;
403}
404
410/* virtual */ ptrdiff_t Window::GetTextCharacterAtPosition(const Point &pt) const
411{
412 if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
413 return this->GetQueryString(this->nested_focus->GetIndex())->GetCharAtPosition(this, this->nested_focus->GetIndex(), pt);
414 }
415
416 return -1;
417}
418
423 void SetFocusedWindow(Window *w)
424{
425 if (_focused_window == w) return;
426
427 /* Don't focus a tooltip */
428 if (w != nullptr && w->window_class == WC_TOOLTIPS) return;
429
430 /* Invalidate focused widget */
431 if (_focused_window != nullptr) {
432 if (_focused_window->nested_focus != nullptr) _focused_window->nested_focus->SetDirty(_focused_window);
433 }
434
435 /* Remember which window was previously focused */
436 Window *old_focused = _focused_window;
437 _focused_window = w;
438
439 /* So we can inform it that it lost focus */
440 if (old_focused != nullptr) old_focused->OnFocusLost(false);
441 if (_focused_window != nullptr) _focused_window->OnFocus();
442}
443
449 bool EditBoxInGlobalFocus()
450{
451 if (_focused_window == nullptr) return false;
452
453 /* The console does not have an edit box so a special case is needed. */
454 if (_focused_window->window_class == WC_CONSOLE) return true;
455
456 return _focused_window->nested_focus != nullptr && _focused_window->nested_focus->type == WWT_EDITBOX;
457}
458
463 bool FocusedWindowIsConsole()
464{
465 return _focused_window && _focused_window->window_class == WC_CONSOLE;
466}
467
471 void Window::UnfocusFocusedWidget()
472{
473 if (this->nested_focus != nullptr) {
474 if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
475
476 /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
477 this->nested_focus->SetDirty(this);
478 this->nested_focus = nullptr;
479 }
480}
481
487 bool Window::SetFocusedWidget(WidgetID widget_index)
488{
489 NWidgetCore *widget = this->GetWidget<NWidgetCore>(widget_index);
490 assert(widget != nullptr); /* Setting focus to a non-existing widget is a bad idea. */
491
492 if (this->nested_focus != nullptr) {
493 /* Do nothing if widget_index is already focused. */
494 if (widget == this->nested_focus) return false;
495
496 /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
497 this->nested_focus->SetDirty(this);
498 if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
499 }
500
501 this->nested_focus = widget;
502 if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxGainedFocus();
503 return true;
504}
505
506std::string Window::GetWidgetString([[maybe_unused]] WidgetID widget, StringID stringid) const
507{
508 if (stringid == STR_NULL) return {};
509 return GetString(stringid);
510}
511
515 void Window::OnFocus()
516{
517 if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxGainedFocus();
518}
519
523 void Window::OnFocusLost(bool)
524{
525 if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
526}
527
532 void Window::RaiseButtons(bool autoraise)
533{
534 for (auto &pair : this->widget_lookup) {
535 WidgetType type = pair.second->type;
536 NWidgetCore *wid = dynamic_cast<NWidgetCore *>(pair.second);
537 if (wid != nullptr && ((type & ~WWB_PUSHBUTTON) < WWT_LAST || type == NWID_PUSHBUTTON_DROPDOWN) &&
538 (!autoraise || (type & WWB_PUSHBUTTON) || type == WWT_EDITBOX) && wid->IsLowered()) {
539 wid->SetLowered(false);
540 wid->SetDirty(this);
541 }
542 }
543
544 /* Special widgets without widget index */
545 {
546 NWidgetCore *wid = this->nested_root != nullptr ? dynamic_cast<NWidgetCore *>(this->nested_root->GetWidgetOfType(WWT_DEFSIZEBOX)) : nullptr;
547 if (wid != nullptr) {
548 wid->SetLowered(false);
549 wid->SetDirty(this);
550 }
551 }
552}
553
558 void Window::SetWidgetDirty(WidgetID widget_index) const
559{
560 /* Sometimes this function is called before the window is even fully initialized */
561 auto it = this->widget_lookup.find(widget_index);
562 if (it == std::end(this->widget_lookup)) return;
563
564 it->second->SetDirty(this);
565}
566
572 EventState Window::OnHotkey(int hotkey)
573{
574 if (hotkey < 0) return ES_NOT_HANDLED;
575
576 NWidgetCore *nw = this->GetWidget<NWidgetCore>(hotkey);
577 if (nw == nullptr || nw->IsDisabled()) return ES_NOT_HANDLED;
578
579 if (nw->type == WWT_EDITBOX) {
580 if (this->IsShaded()) return ES_NOT_HANDLED;
581
582 /* Focus editbox */
583 this->SetFocusedWidget(hotkey);
584 SetFocusedWindow(this);
585 } else {
586 /* Click button */
587 this->OnClick(Point(), hotkey, 1);
588 }
589 return ES_HANDLED;
590}
591
597 void Window::HandleButtonClick(WidgetID widget)
598{
599 /* Button click for this widget may already have been handled. */
600 if (this->IsWidgetLowered(widget) && this->timeout_timer == TIMEOUT_DURATION) return;
601
602 this->LowerWidget(widget);
603 this->SetTimeout();
604 this->SetWidgetDirty(widget);
605 SndClickBeep();
606}
607
608static void StartWindowDrag(Window *w);
609static void StartWindowSizing(Window *w, bool to_left);
610
618 static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
619{
620 NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
621 WidgetType widget_type = (nw != nullptr) ? nw->type : WWT_EMPTY;
622
623 /* Allow dropdown close flag detection to work. */
624 if (nw != nullptr) nw->disp_flags.Reset(NWidgetDisplayFlag::DropdownClosed);
625
626 bool focused_widget_changed = false;
627
628 /* If clicked on a window that previously did not have focus */
629 if (_focused_window != w) {
630 /* Don't switch focus to an unfocusable window, or if the 'X' (close button) was clicked. */
631 if (!w->window_desc.flags.Test(WindowDefaultFlag::NoFocus) && widget_type != WWT_CLOSEBOX) {
632 focused_widget_changed = true;
633 SetFocusedWindow(w);
634 } else if (_focused_window != nullptr && _focused_window->window_class == WC_DROPDOWN_MENU) {
635 /* The previously focused window was a dropdown menu, but the user clicked on another window that
636 * isn't focusable. Close the dropdown menu anyway. */
637 SetFocusedWindow(nullptr);
638 }
639 }
640
641 if (nw == nullptr) return; // exit if clicked outside of widgets
642
643 /* don't allow any interaction if the button has been disabled */
644 if (nw->IsDisabled()) return;
645
646 WidgetID widget_index = nw->GetIndex();
647
648 /* Clicked on a widget that is not disabled.
649 * So unless the clicked widget is the caption bar, change focus to this widget.
650 * Exception: In the OSK we always want the editbox to stay focused. */
651 if (widget_index >= 0 && widget_type != WWT_CAPTION && w->window_class != WC_OSK) {
652 /* focused_widget_changed is 'now' only true if the window this widget
653 * is in gained focus. In that case it must remain true, also if the
654 * local widget focus did not change. As such it's the logical-or of
655 * both changed states.
656 *
657 * If this is not preserved, then the OSK window would be opened when
658 * a user has the edit box focused and then click on another window and
659 * then back again on the edit box (to type some text).
660 */
661 focused_widget_changed |= w->SetFocusedWidget(widget_index);
662 }
663
664 /* Dropdown window of this widget was closed so don't process click this time. */
665 if (nw->disp_flags.Test(NWidgetDisplayFlag::DropdownClosed)) return;
666
667 if ((widget_type & ~WWB_PUSHBUTTON) < WWT_LAST && (widget_type & WWB_PUSHBUTTON)) w->HandleButtonClick(widget_index);
668
669 Point pt = { x, y };
670
671 switch (widget_type) {
672 case NWID_VSCROLLBAR:
673 case NWID_HSCROLLBAR:
674 ScrollbarClickHandler(w, nw, x, y);
675 break;
676
677 case WWT_EDITBOX: {
678 QueryString *query = w->GetQueryString(widget_index);
679 if (query != nullptr) query->ClickEditBox(w, pt, widget_index, click_count, focused_widget_changed);
680 break;
681 }
682
683 case WWT_CLOSEBOX: // 'X'
684 w->Close();
685 return;
686
687 case WWT_CAPTION: // 'Title bar'
688 StartWindowDrag(w);
689 return;
690
691 case WWT_RESIZEBOX:
692 /* When the resize widget is on the left size of the window
693 * we assume that that button is used to resize to the left. */
694 StartWindowSizing(w, nw->pos_x < (w->width / 2));
695 nw->SetDirty(w);
696 return;
697
698 case WWT_DEFSIZEBOX: {
699 if (_ctrl_pressed) {
700 if (click_count > 1) {
701 w->window_desc.pref_width = 0;
702 w->window_desc.pref_height = 0;
703 } else {
704 w->window_desc.pref_width = w->width;
705 w->window_desc.pref_height = w->height;
706 }
707 } else {
708 int16_t def_width = std::max<int16_t>(std::min<int16_t>(w->window_desc.GetDefaultWidth(), _screen.width), w->nested_root->smallest_x);
709 int16_t def_height = std::max<int16_t>(std::min<int16_t>(w->window_desc.GetDefaultHeight(), _screen.height - 50), w->nested_root->smallest_y);
710
711 int dx = (w->resize.step_width == 0) ? 0 : def_width - w->width;
712 int dy = (w->resize.step_height == 0) ? 0 : def_height - w->height;
713 /* dx and dy has to go by step.. calculate it.
714 * The cast to int is necessary else dx/dy are implicitly cast to unsigned int, which won't work. */
715 if (w->resize.step_width > 1) dx -= dx % (int)w->resize.step_width;
716 if (w->resize.step_height > 1) dy -= dy % (int)w->resize.step_height;
717 ResizeWindow(w, dx, dy, false);
718 }
719
720 nw->SetLowered(true);
721 nw->SetDirty(w);
722 w->SetTimeout();
723 break;
724 }
725
726 case WWT_DEBUGBOX:
727 w->ShowNewGRFInspectWindow();
728 break;
729
730 case WWT_SHADEBOX:
731 nw->SetDirty(w);
732 w->SetShaded(!w->IsShaded());
733 return;
734
735 case WWT_STICKYBOX:
736 w->flags.Flip(WindowFlag::Sticky);
737 nw->SetDirty(w);
738 if (_ctrl_pressed) w->window_desc.pref_sticky = w->flags.Test(WindowFlag::Sticky);
739 return;
740
741 default:
742 break;
743 }
744
745 /* Widget has no index, so the window is not interested in it. */
746 if (widget_index < 0) return;
747
748 /* Check if the widget is highlighted; if so, disable highlight and dispatch an event to the GameScript */
749 if (w->IsWidgetHighlighted(widget_index)) {
750 w->SetWidgetHighlight(widget_index, TC_INVALID);
751 Game::NewEvent(new ScriptEventWindowWidgetClick((ScriptWindow::WindowClass)w->window_class, w->window_number, widget_index));
752 }
753
754 w->OnClick(pt, widget_index, click_count);
755}
756
763 static void DispatchRightClickEvent(Window *w, int x, int y)
764{
765 NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
766 if (wid == nullptr) return;
767
768 Point pt = { x, y };
769
770 /* No widget to handle, or the window is not interested in it. */
771 if (wid->GetIndex() >= 0) {
772 if (w->OnRightClick(pt, wid->GetIndex())) return;
773 }
774
775 /* Right-click close is enabled and there is a closebox. */
776 if (_settings_client.gui.right_click_wnd_close == RCC_YES && !w->window_desc.flags.Test(WindowDefaultFlag::NoClose)) {
777 w->Close();
778 } else if (_settings_client.gui.right_click_wnd_close == RCC_YES_EXCEPT_STICKY && !w->flags.Test(WindowFlag::Sticky) && !w->window_desc.flags.Test(WindowDefaultFlag::NoClose)) {
779 /* Right-click close is enabled, but excluding sticky windows. */
780 w->Close();
781 } else if (_settings_client.gui.hover_delay_ms == 0 && !w->OnTooltip(pt, wid->GetIndex(), TCC_RIGHT_CLICK) && wid->GetToolTip() != STR_NULL) {
782 GuiShowTooltips(w, GetEncodedString(wid->GetToolTip()), TCC_RIGHT_CLICK);
783 }
784}
785
792 static void DispatchHoverEvent(Window *w, int x, int y)
793{
794 NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
795
796 /* No widget to handle */
797 if (wid == nullptr) return;
798
799 Point pt = { x, y };
800
801 /* Show the tooltip if there is any */
802 if (!w->OnTooltip(pt, wid->GetIndex(), TCC_HOVER) && wid->GetToolTip() != STR_NULL) {
803 GuiShowTooltips(w, GetEncodedString(wid->GetToolTip()), TCC_HOVER);
804 return;
805 }
806
807 /* Widget has no index, so the window is not interested in it. */
808 if (wid->GetIndex() < 0) return;
809
810 w->OnHover(pt, wid->GetIndex());
811}
812
820 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
821{
822 if (nwid == nullptr) return;
823
824 /* Using wheel on caption/shade-box shades or unshades the window. */
825 if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
826 w->SetShaded(wheel < 0);
827 return;
828 }
829
830 /* Wheeling a vertical scrollbar. */
831 if (nwid->type == NWID_VSCROLLBAR) {
832 NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
833 if (sb->GetCount() > sb->GetCapacity()) {
834 if (sb->UpdatePosition(wheel)) {
835 w->OnScrollbarScroll(nwid->GetIndex());
836 w->SetDirty();
837 }
838 }
839 return;
840 }
841
842 /* Scroll the widget attached to the scrollbar. */
843 Scrollbar *sb = (nwid->GetScrollbarIndex() >= 0 ? w->GetScrollbar(nwid->GetScrollbarIndex()) : nullptr);
844 if (sb != nullptr && sb->GetCount() > sb->GetCapacity()) {
845 if (sb->UpdatePosition(wheel)) {
846 w->OnScrollbarScroll(nwid->GetScrollbarIndex());
847 w->SetDirty();
848 }
849 }
850}
851
857 static bool MayBeShown(const Window *w)
858{
859 /* If we're not modal, everything is okay. */
860 if (!HasModalProgress()) return true;
861
862 switch (w->window_class) {
863 case WC_MAIN_WINDOW:
864 case WC_MODAL_PROGRESS:
865 case WC_CONFIRM_POPUP_QUERY:
866 return true;
867
868 default:
869 return false;
870 }
871}
872
885 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
886{
887 Window::IteratorToFront it(w);
888 ++it;
889 for (; !it.IsEnd(); ++it) {
890 const Window *v = *it;
891 if (MayBeShown(v) &&
892 right > v->left &&
893 bottom > v->top &&
894 left < v->left + v->width &&
895 top < v->top + v->height) {
896 /* v and rectangle intersect with each other */
897 int x;
898
899 if (left < (x = v->left)) {
900 DrawOverlappedWindow(w, left, top, x, bottom);
901 DrawOverlappedWindow(w, x, top, right, bottom);
902 return;
903 }
904
905 if (right > (x = v->left + v->width)) {
906 DrawOverlappedWindow(w, left, top, x, bottom);
907 DrawOverlappedWindow(w, x, top, right, bottom);
908 return;
909 }
910
911 if (top < (x = v->top)) {
912 DrawOverlappedWindow(w, left, top, right, x);
913 DrawOverlappedWindow(w, left, x, right, bottom);
914 return;
915 }
916
917 if (bottom > (x = v->top + v->height)) {
918 DrawOverlappedWindow(w, left, top, right, x);
919 DrawOverlappedWindow(w, left, x, right, bottom);
920 return;
921 }
922
923 return;
924 }
925 }
926
927 /* Setup blitter, and dispatch a repaint event to window *wz */
928 DrawPixelInfo *dp = _cur_dpi;
929 dp->width = right - left;
930 dp->height = bottom - top;
931 dp->left = left - w->left;
932 dp->top = top - w->top;
933 dp->pitch = _screen.pitch;
934 dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
935 dp->zoom = ZoomLevel::Min;
936 w->OnPaint();
937}
938
947 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
948{
949 DrawPixelInfo bk;
950 AutoRestoreBackup dpi_backup(_cur_dpi, &bk);
951
952 for (Window *w : Window::IterateFromBack()) {
953 if (MayBeShown(w) &&
954 right > w->left &&
955 bottom > w->top &&
956 left < w->left + w->width &&
957 top < w->top + w->height) {
958 /* Window w intersects with the rectangle => needs repaint */
959 DrawOverlappedWindow(w, std::max(left, w->left), std::max(top, w->top), std::min(right, w->left + w->width), std::min(bottom, w->top + w->height));
960 }
961 }
962}
963
968 void Window::SetDirty() const
969{
970 AddDirtyBlock(this->left, this->top, this->left + this->width, this->top + this->height);
971}
972
980 void Window::ReInit(int rx, int ry, bool reposition)
981{
982 this->SetDirty(); // Mark whole current window as dirty.
983
984 /* Save current size. */
985 int window_width = this->width * _gui_scale / this->scale;
986 int window_height = this->height * _gui_scale / this->scale;
987 this->scale = _gui_scale;
988
989 this->OnInit();
990 /* Re-initialize window smallest size. */
991 this->nested_root->SetupSmallestSize(this);
992 this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
993 this->width = this->nested_root->smallest_x;
994 this->height = this->nested_root->smallest_y;
995 this->resize.step_width = this->nested_root->resize_x;
996 this->resize.step_height = this->nested_root->resize_y;
997
998 /* Resize as close to the original size + requested resize as possible. */
999 window_width = std::max(window_width + rx, this->width);
1000 window_height = std::max(window_height + ry, this->height);
1001 int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
1002 int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
1003 /* dx and dy has to go by step.. calculate it.
1004 * The cast to int is necessary else dx/dy are implicitly cast to unsigned int, which won't work. */
1005 if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
1006 if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
1007
1008 if (reposition) {
1009 Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1010 this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1011 this->FindWindowPlacementAndResize(this->window_desc.GetDefaultWidth(), this->window_desc.GetDefaultHeight(), false);
1012 }
1013
1014 ResizeWindow(this, dx, dy, true, false);
1015 /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
1016}
1017
1023 void Window::SetShaded(bool make_shaded)
1024{
1025 if (this->shade_select == nullptr) return;
1026
1027 int desired = make_shaded ? SZSP_HORIZONTAL : 0;
1028 if (this->shade_select->shown_plane != desired) {
1029 if (make_shaded) {
1030 if (this->nested_focus != nullptr) this->UnfocusFocusedWidget();
1031 this->unshaded_size.width = this->width;
1032 this->unshaded_size.height = this->height;
1033 this->shade_select->SetDisplayedPlane(desired);
1034 this->ReInit(0, -this->height);
1035 } else {
1036 this->shade_select->SetDisplayedPlane(desired);
1037 int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
1038 int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
1039 this->ReInit(dx, dy);
1040 }
1041 }
1042}
1043
1049 Window *Window::FindChildWindow(WindowClass wc) const
1050{
1051 for (Window *v : Window::Iterate()) {
1052 if ((wc == WC_INVALID || wc == v->window_class) && v->parent == this) return v;
1053 }
1054
1055 return nullptr;
1056}
1057
1064 Window *Window::FindChildWindowById(WindowClass wc, WindowNumber number) const
1065{
1066 for (Window *v : Window::Iterate()) {
1067 if (wc == v->window_class && number == v->window_number && v->parent == this) return v;
1068 }
1069
1070 return nullptr;
1071}
1072
1077 void Window::CloseChildWindows(WindowClass wc) const
1078{
1079 Window *child = this->FindChildWindow(wc);
1080 while (child != nullptr) {
1081 child->Close();
1082 child = this->FindChildWindow(wc);
1083 }
1084}
1085
1086
1092 void Window::CloseChildWindowById(WindowClass wc, WindowNumber number) const
1093{
1094 Window *child = this->FindChildWindowById(wc, number);
1095 while (child != nullptr) {
1096 child->Close();
1097 child = this->FindChildWindowById(wc, number);
1098 }
1099}
1100
1104 void Window::Close([[maybe_unused]] int data)
1105{
1106 /* Don't close twice. */
1107 if (*this->z_position == nullptr) return;
1108
1109 *this->z_position = nullptr;
1110
1111 if (_thd.window_class == this->window_class &&
1112 _thd.window_number == this->window_number) {
1113 ResetObjectToPlace();
1114 }
1115
1116 /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
1117 if (_mouseover_last_w == this) _mouseover_last_w = nullptr;
1118
1119 /* We can't scroll the window when it's closed. */
1120 if (_last_scroll_window == this) _last_scroll_window = nullptr;
1121
1122 /* Make sure we don't try to access non-existing query strings. */
1123 this->querystrings.clear();
1124
1125 /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
1126 if (_focused_window == this) {
1127 this->OnFocusLost(true);
1128 _focused_window = nullptr;
1129 }
1130
1131 this->CloseChildWindows();
1132
1133 this->SetDirty();
1134
1135 Window::closed_windows.push_back(this);
1136}
1137
1141Window::~Window()
1142{
1143 /* Make sure the window is closed, deletion is allowed only in Window::DeleteClosedWindows(). */
1144 assert(*this->z_position == nullptr);
1145}
1146
1153 Window *FindWindowById(WindowClass cls, WindowNumber number)
1154{
1155 for (Window *w : Window::Iterate()) {
1156 if (w->window_class == cls && w->window_number == number) return w;
1157 }
1158
1159 return nullptr;
1160}
1161
1168 Window *FindWindowByClass(WindowClass cls)
1169{
1170 for (Window *w : Window::Iterate()) {
1171 if (w->window_class == cls) return w;
1172 }
1173
1174 return nullptr;
1175}
1176
1182 Window *GetMainWindow()
1183{
1184 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1185 assert(w != nullptr);
1186 return w;
1187}
1188
1195 void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
1196{
1197 Window *w = FindWindowById(cls, number);
1198 if (w != nullptr && (force || !w->flags.Test(WindowFlag::Sticky))) {
1199 w->Close(data);
1200 }
1201}
1202
1207 void CloseWindowByClass(WindowClass cls, int data)
1208{
1209 /* Note: the container remains stable, even when deleting windows. */
1210 for (Window *w : Window::Iterate()) {
1211 if (w->window_class == cls) {
1212 w->Close(data);
1213 }
1214 }
1215}
1216
1223 void CloseCompanyWindows(CompanyID id)
1224{
1225 /* Note: the container remains stable, even when deleting windows. */
1226 for (Window *w : Window::Iterate()) {
1227 if (w->owner == id) {
1228 w->Close();
1229 }
1230 }
1231
1232 /* Also delete the company specific windows that don't have a company-colour. */
1233 CloseWindowById(WC_BUY_COMPANY, id);
1234}
1235
1243 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1244{
1245 for (Window *w : Window::Iterate()) {
1246 if (w->owner != old_owner) continue;
1247
1248 switch (w->window_class) {
1249 case WC_COMPANY_COLOUR:
1250 case WC_FINANCES:
1251 case WC_STATION_LIST:
1252 case WC_TRAINS_LIST:
1253 case WC_ROADVEH_LIST:
1254 case WC_SHIPS_LIST:
1255 case WC_AIRCRAFT_LIST:
1256 case WC_BUY_COMPANY:
1257 case WC_COMPANY:
1258 case WC_COMPANY_INFRASTRUCTURE:
1259 case WC_VEHICLE_ORDERS: // Changing owner would also require changing WindowDesc, which is not possible; however keeping the old one crashes because of missing widgets etc.. See ShowOrdersWindow().
1260 continue;
1261
1262 default:
1263 w->owner = new_owner;
1264 break;
1265 }
1266 }
1267}
1268
1269static void BringWindowToFront(Window *w, bool dirty = true);
1270
1278 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number)
1279{
1280 Window *w = FindWindowById(cls, number);
1281
1282 if (w != nullptr) {
1283 if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1284
1285 w->SetWhiteBorder();
1286 BringWindowToFront(w);
1287 w->SetDirty();
1288 }
1289
1290 return w;
1291}
1292
1293static inline bool IsVitalWindow(const Window *w)
1294{
1295 switch (w->window_class) {
1296 case WC_MAIN_TOOLBAR:
1297 case WC_STATUS_BAR:
1298 case WC_NEWS_WINDOW:
1299 case WC_SEND_NETWORK_MSG:
1300 return true;
1301
1302 default:
1303 return false;
1304 }
1305}
1306
1315 static uint GetWindowZPriority(WindowClass wc)
1316{
1317 assert(wc != WC_INVALID);
1318
1319 uint z_priority = 0;
1320
1321 switch (wc) {
1322 case WC_TOOLTIPS:
1323 ++z_priority;
1324 [[fallthrough]];
1325
1326 case WC_ERRMSG:
1327 case WC_CONFIRM_POPUP_QUERY:
1328 ++z_priority;
1329 [[fallthrough]];
1330
1331 case WC_ENDSCREEN:
1332 ++z_priority;
1333 [[fallthrough]];
1334
1335 case WC_HIGHSCORE:
1336 ++z_priority;
1337 [[fallthrough]];
1338
1339 case WC_DROPDOWN_MENU:
1340 ++z_priority;
1341 [[fallthrough]];
1342
1343 case WC_MAIN_TOOLBAR:
1344 case WC_STATUS_BAR:
1345 ++z_priority;
1346 [[fallthrough]];
1347
1348 case WC_OSK:
1349 ++z_priority;
1350 [[fallthrough]];
1351
1352 case WC_QUERY_STRING:
1353 case WC_SEND_NETWORK_MSG:
1354 ++z_priority;
1355 [[fallthrough]];
1356
1357 case WC_NETWORK_ASK_RELAY:
1358 case WC_MODAL_PROGRESS:
1359 case WC_NETWORK_STATUS_WINDOW:
1360 case WC_SAVE_PRESET:
1361 ++z_priority;
1362 [[fallthrough]];
1363
1364 case WC_GENERATE_LANDSCAPE:
1365 case WC_SAVELOAD:
1366 case WC_GAME_OPTIONS:
1367 case WC_CUSTOM_CURRENCY:
1368 case WC_NETWORK_WINDOW:
1369 case WC_GRF_PARAMETERS:
1370 case WC_SCRIPT_LIST:
1371 case WC_SCRIPT_SETTINGS:
1372 case WC_TEXTFILE:
1373 ++z_priority;
1374 [[fallthrough]];
1375
1376 case WC_CONSOLE:
1377 ++z_priority;
1378 [[fallthrough]];
1379
1380 case WC_NEWS_WINDOW:
1381 ++z_priority;
1382 [[fallthrough]];
1383
1384 default:
1385 ++z_priority;
1386 [[fallthrough]];
1387
1388 case WC_MAIN_WINDOW:
1389 return z_priority;
1390 }
1391}
1392
1399 static void BringWindowToFront(Window *w, bool dirty)
1400{
1401 auto priority = GetWindowZPriority(w->window_class);
1402 WindowList::iterator dest = _z_windows.begin();
1403 while (dest != _z_windows.end() && (*dest == nullptr || GetWindowZPriority((*dest)->window_class) <= priority)) ++dest;
1404
1405 if (dest != w->z_position) {
1406 _z_windows.splice(dest, _z_windows, w->z_position);
1407 }
1408
1409 if (dirty) w->SetDirty();
1410}
1411
1419 void Window::InitializeData(WindowNumber window_number)
1420{
1421 /* Set up window properties; some of them are needed to set up smallest size below */
1422 this->window_class = this->window_desc.cls;
1423 this->SetWhiteBorder();
1424 if (this->window_desc.default_pos == WDP_CENTER) this->flags.Set(WindowFlag::Centred);
1425 this->owner = INVALID_OWNER;
1426 this->nested_focus = nullptr;
1427 this->window_number = window_number;
1428
1429 this->OnInit();
1430 /* Initialize smallest size. */
1431 this->nested_root->SetupSmallestSize(this);
1432 /* Initialize to smallest size. */
1433 this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1434
1435 /* Further set up window properties,
1436 * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1437 this->resize.step_width = this->nested_root->resize_x;
1438 this->resize.step_height = this->nested_root->resize_y;
1439
1440 /* Give focus to the opened window unless a dropdown menu has focus or a text box of the focused window has focus
1441 * (so we don't interrupt typing) unless the new window has a text box. */
1442 bool dropdown_active = _focused_window != nullptr && _focused_window->window_class == WC_DROPDOWN_MENU;
1443 bool editbox_active = EditBoxInGlobalFocus() && this->nested_root->GetWidgetOfType(WWT_EDITBOX) == nullptr;
1444 if (!dropdown_active && !editbox_active) SetFocusedWindow(this);
1445
1446 /* Insert the window into the correct location in the z-ordering. */
1447 BringWindowToFront(this, false);
1448}
1449
1457 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1458{
1459 this->left = x;
1460 this->top = y;
1461 this->width = sm_width;
1462 this->height = sm_height;
1463}
1464
1476 void Window::FindWindowPlacementAndResize(int def_width, int def_height, bool allow_resize)
1477{
1478 if (allow_resize) {
1479 def_width = std::max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1480 def_height = std::max(def_height, this->height);
1481 /* Try to make windows smaller when our window is too small.
1482 * w->(width|height) is normally the same as min_(width|height),
1483 * but this way the GUIs can be made a little more dynamic;
1484 * one can use the same spec for multiple windows and those
1485 * can then determine the real minimum size of the window. */
1486 if (this->width != def_width || this->height != def_height) {
1487 /* Think about the overlapping toolbars when determining the minimum window size */
1488 int free_height = _screen.height;
1489 const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1490 if (wt != nullptr) free_height -= wt->height;
1491 wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1492 if (wt != nullptr) free_height -= wt->height;
1493
1494 int enlarge_x = std::max(std::min(def_width - this->width, _screen.width - this->width), 0);
1495 int enlarge_y = std::max(std::min(def_height - this->height, free_height - this->height), 0);
1496
1497 /* X and Y has to go by step.. calculate it.
1498 * The cast to int is necessary else x/y are implicitly cast to
1499 * unsigned int, which won't work. */
1500 if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1501 if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1502
1503 ResizeWindow(this, enlarge_x, enlarge_y, true, false);
1504 /* ResizeWindow() calls this->OnResize(). */
1505 } else {
1506 /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1507 this->OnResize();
1508 }
1509 }
1510
1511 int nx = this->left;
1512 int ny = this->top;
1513
1514 if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1515
1516 const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1517 ny = std::max(ny, (wt == nullptr || this == wt || this->top == 0) ? 0 : wt->height);
1518 nx = std::max(nx, 0);
1519
1520 if (this->viewport != nullptr) {
1521 this->viewport->left += nx - this->left;
1522 this->viewport->top += ny - this->top;
1523 }
1524 this->left = nx;
1525 this->top = ny;
1526
1527 this->SetDirty();
1528}
1529
1542 static bool IsGoodAutoPlace1(int left, int top, int width, int height, int toolbar_y, Point &pos)
1543{
1544 int right = width + left;
1545 int bottom = height + top;
1546
1547 if (left < 0 || top < toolbar_y || right > _screen.width || bottom > _screen.height) return false;
1548
1549 /* Make sure it is not obscured by any window. */
1550 for (const Window *w : Window::Iterate()) {
1551 if (w->window_class == WC_MAIN_WINDOW) continue;
1552
1553 if (right > w->left &&
1554 w->left + w->width > left &&
1555 bottom > w->top &&
1556 w->top + w->height > top) {
1557 return false;
1558 }
1559 }
1560
1561 pos.x = left;
1562 pos.y = top;
1563 return true;
1564}
1565
1578 static bool IsGoodAutoPlace2(int left, int top, int width, int height, int toolbar_y, Point &pos)
1579{
1580 bool rtl = _current_text_dir == TD_RTL;
1581
1582 /* Left part of the rectangle may be at most 1/4 off-screen,
1583 * right part of the rectangle may be at most 1/2 off-screen
1584 */
1585 if (rtl) {
1586 if (left < -(width >> 1) || left > _screen.width - (width >> 2)) return false;
1587 } else {
1588 if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1589 }
1590
1591 /* Bottom part of the rectangle may be at most 1/4 off-screen */
1592 if (top < toolbar_y || top > _screen.height - (height >> 2)) return false;
1593
1594 /* Make sure it is not obscured by any window. */
1595 for (const Window *w : Window::Iterate()) {
1596 if (w->window_class == WC_MAIN_WINDOW) continue;
1597
1598 if (left + width > w->left &&
1599 w->left + w->width > left &&
1600 top + height > w->top &&
1601 w->top + w->height > top) {
1602 return false;
1603 }
1604 }
1605
1606 pos.x = left;
1607 pos.y = top;
1608 return true;
1609}
1610
1617 static Point GetAutoPlacePosition(int width, int height)
1618{
1619 Point pt;
1620
1621 bool rtl = _current_text_dir == TD_RTL;
1622
1623 /* First attempt, try top-left of the screen */
1624 const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1625 const int toolbar_y = main_toolbar != nullptr ? main_toolbar->height : 0;
1626 if (IsGoodAutoPlace1(rtl ? _screen.width - width : 0, toolbar_y, width, height, toolbar_y, pt)) return pt;
1627
1628 /* Second attempt, try around all existing windows.
1629 * The new window must be entirely on-screen, and not overlap with an existing window.
1630 * Eight starting points are tried, two at each corner.
1631 */
1632 for (const Window *w : Window::Iterate()) {
1633 if (w->window_class == WC_MAIN_WINDOW) continue;
1634
1635 if (IsGoodAutoPlace1(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1636 if (IsGoodAutoPlace1(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1637 if (IsGoodAutoPlace1(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1638 if (IsGoodAutoPlace1(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1639 if (IsGoodAutoPlace1(w->left + w->width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1640 if (IsGoodAutoPlace1(w->left - width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1641 if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1642 if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height, width, height, toolbar_y, pt)) return pt;
1643 }
1644
1645 /* Third attempt, try around all existing windows.
1646 * The new window may be partly off-screen, and must not overlap with an existing window.
1647 * Only four starting points are tried.
1648 */
1649 for (const Window *w : Window::Iterate()) {
1650 if (w->window_class == WC_MAIN_WINDOW) continue;
1651
1652 if (IsGoodAutoPlace2(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1653 if (IsGoodAutoPlace2(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1654 if (IsGoodAutoPlace2(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1655 if (IsGoodAutoPlace2(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1656 }
1657
1658 /* Fourth and final attempt, put window at diagonal starting from (0, toolbar_y), try multiples
1659 * of the closebox
1660 */
1661 int left = rtl ? _screen.width - width : 0, top = toolbar_y;
1662 int offset_x = rtl ? -(int)NWidgetLeaf::closebox_dimension.width : (int)NWidgetLeaf::closebox_dimension.width;
1663 int offset_y = std::max<int>(NWidgetLeaf::closebox_dimension.height, GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.captiontext.Vertical());
1664
1665restart:
1666 for (const Window *w : Window::Iterate()) {
1667 if (w->left == left && w->top == top) {
1668 left += offset_x;
1669 top += offset_y;
1670 goto restart;
1671 }
1672 }
1673
1674 pt.x = left;
1675 pt.y = top;
1676 return pt;
1677}
1678
1685 Point GetToolbarAlignedWindowPosition(int window_width)
1686{
1687 const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1688 assert(w != nullptr);
1689 Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1690 return pt;
1691}
1692
1701 Point AlignInitialConstructionToolbar(int window_width)
1702{
1703 Point pt = GetToolbarAlignedWindowPosition(window_width);
1704 const Window *w = FindWindowByClass(WC_SCEN_LAND_GEN);
1705 if (w != nullptr && w->top == pt.y && !_settings_client.gui.link_terraform_toolbar) {
1706 pt.x = w->left + (_current_text_dir == TD_RTL ? w->width : - window_width);
1707 }
1708 return pt;
1709}
1710
1728 static Point LocalGetWindowPlacement(const WindowDesc &desc, int16_t sm_width, int16_t sm_height, int window_number)
1729{
1730 Point pt;
1731 const Window *w;
1732
1733 int16_t default_width = std::max(desc.GetDefaultWidth(), sm_width);
1734 int16_t default_height = std::max(desc.GetDefaultHeight(), sm_height);
1735
1736 if (desc.parent_cls != WC_NONE && (w = FindWindowById(desc.parent_cls, window_number)) != nullptr) {
1737 bool rtl = _current_text_dir == TD_RTL;
1738 if (desc.parent_cls == WC_BUILD_TOOLBAR || desc.parent_cls == WC_SCEN_LAND_GEN) {
1739 pt.x = w->left + (rtl ? w->width - default_width : 0);
1740 pt.y = w->top + w->height;
1741 return pt;
1742 } else {
1743 /* Position child window with offset of closebox, but make sure that either closebox or resizebox is visible
1744 * - Y position: closebox of parent + closebox of child + statusbar
1745 * - X position: closebox on left/right, resizebox on right/left (depending on ltr/rtl)
1746 */
1747 int indent_y = std::max<int>(NWidgetLeaf::closebox_dimension.height, GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.captiontext.Vertical());
1748 if (w->top + 3 * indent_y < _screen.height) {
1749 pt.y = w->top + indent_y;
1750 int indent_close = NWidgetLeaf::closebox_dimension.width;
1751 int indent_resize = NWidgetLeaf::resizebox_dimension.width;
1752 if (_current_text_dir == TD_RTL) {
1753 pt.x = std::max(w->left + w->width - default_width - indent_close, 0);
1754 if (pt.x + default_width >= indent_close && pt.x + indent_resize <= _screen.width) return pt;
1755 } else {
1756 pt.x = std::min(w->left + indent_close, _screen.width - default_width);
1757 if (pt.x + default_width >= indent_resize && pt.x + indent_close <= _screen.width) return pt;
1758 }
1759 }
1760 }
1761 }
1762
1763 switch (desc.default_pos) {
1764 case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1765 return GetToolbarAlignedWindowPosition(default_width);
1766
1767 case WDP_AUTO: // Find a good automatic position for the window
1768 return GetAutoPlacePosition(default_width, default_height);
1769
1770 case WDP_CENTER: // Centre the window horizontally
1771 pt.x = (_screen.width - default_width) / 2;
1772 pt.y = (_screen.height - default_height) / 2;
1773 break;
1774
1775 case WDP_MANUAL:
1776 pt.x = 0;
1777 pt.y = 0;
1778 break;
1779
1780 default:
1781 NOT_REACHED();
1782 }
1783
1784 return pt;
1785}
1786
1787/* virtual */ Point Window::OnInitialPosition([[maybe_unused]]int16_t sm_width, [[maybe_unused]]int16_t sm_height, [[maybe_unused]]int window_number)
1788{
1789 return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1790}
1791
1799 void Window::CreateNestedTree()
1800{
1801 this->nested_root = MakeWindowNWidgetTree(this->window_desc.nwid_parts, &this->shade_select);
1802 this->nested_root->FillWidgetLookup(this->widget_lookup);
1803}
1804
1809 void Window::FinishInitNested(WindowNumber window_number)
1810{
1811 this->nested_root->AdjustPaddingForZoom();
1812 this->InitializeData(window_number);
1813 this->ApplyDefaults();
1814 Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1815 this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1816 this->FindWindowPlacementAndResize(this->window_desc.GetDefaultWidth(), this->window_desc.GetDefaultHeight(), true);
1817}
1818
1823 void Window::InitNested(WindowNumber window_number)
1824{
1825 this->CreateNestedTree();
1826 this->FinishInitNested(window_number);
1827}
1828
1833Window::Window(WindowDesc &desc) : window_desc(desc), scale(_gui_scale), mouse_capture_widget(INVALID_WIDGET)
1834{
1835 this->z_position = _z_windows.insert(_z_windows.end(), this);
1836}
1837
1845 Window *FindWindowFromPt(int x, int y)
1846{
1847 for (Window *w : Window::IterateFromFront()) {
1848 if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1849 return w;
1850 }
1851 }
1852
1853 return nullptr;
1854}
1855
1859 void InitWindowSystem()
1860{
1861 IConsoleClose();
1862
1863 _focused_window = nullptr;
1864 _mouseover_last_w = nullptr;
1865 _last_scroll_window = nullptr;
1866 _scrolling_viewport = false;
1867 _mouse_hovering = false;
1868
1869 SetupWidgetDimensions();
1870 NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1871 NWidgetScrollbar::InvalidateDimensionCache();
1872
1873 InitDepotWindowBlockSizes();
1874
1875 ShowFirstError();
1876}
1877
1881 void UnInitWindowSystem()
1882{
1883 UnshowCriticalError();
1884
1885 for (Window *w : Window::Iterate()) w->Close();
1886
1887 Window::DeleteClosedWindows();
1888
1889 assert(_z_windows.empty());
1890}
1891
1895 void ResetWindowSystem()
1896{
1897 UnInitWindowSystem();
1898 InitWindowSystem();
1899 _thd.Reset();
1900}
1901
1902static void DecreaseWindowCounters()
1903{
1904 if (_scroller_click_timeout != 0) _scroller_click_timeout--;
1905
1906 for (Window *w : Window::Iterate()) {
1907 if (_scroller_click_timeout == 0) {
1908 /* Unclick scrollbar buttons if they are pressed. */
1909 for (auto &pair : w->widget_lookup) {
1910 NWidgetBase *nwid = pair.second;
1911 if (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR) {
1912 NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1913 if (sb->disp_flags.Any({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown})) {
1914 sb->disp_flags.Reset({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown});
1915 w->mouse_capture_widget = INVALID_WIDGET;
1916 sb->SetDirty(w);
1917 }
1918 }
1919 }
1920 }
1921
1922 /* Handle editboxes */
1923 for (auto &pair : w->querystrings) {
1924 pair.second->HandleEditBox(w, pair.first);
1925 }
1926
1927 w->OnMouseLoop();
1928 }
1929
1930 for (Window *w : Window::Iterate()) {
1931 if (w->flags.Test(WindowFlag::Timeout) && --w->timeout_timer == 0) {
1932 w->flags.Reset(WindowFlag::Timeout);
1933
1934 w->OnTimeout();
1935 w->RaiseButtons(true);
1936 }
1937 }
1938}
1939
1940static void HandlePlacePresize()
1941{
1942 if (_special_mouse_mode != WSM_PRESIZE) return;
1943
1944 Window *w = _thd.GetCallbackWnd();
1945 if (w == nullptr) return;
1946
1947 Point pt = GetTileBelowCursor();
1948 if (pt.x == -1) {
1949 _thd.selend.x = -1;
1950 return;
1951 }
1952
1953 w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1954}
1955
1960 static EventState HandleMouseDragDrop()
1961{
1962 if (_special_mouse_mode != WSM_DRAGDROP) return ES_NOT_HANDLED;
1963
1964 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
1965
1966 Window *w = _thd.GetCallbackWnd();
1967 if (w != nullptr) {
1968 /* Send an event in client coordinates. */
1969 Point pt;
1970 pt.x = _cursor.pos.x - w->left;
1971 pt.y = _cursor.pos.y - w->top;
1972 if (_left_button_down) {
1973 w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
1974 } else {
1975 w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
1976 }
1977 }
1978
1979 if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
1980 return ES_HANDLED;
1981}
1982
1984 static void HandleMouseOver()
1985{
1986 Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
1987
1988 /* We changed window, put an OnMouseOver event to the last window */
1989 if (_mouseover_last_w != nullptr && _mouseover_last_w != w) {
1990 /* Reset mouse-over coordinates of previous window */
1991 Point pt = { -1, -1 };
1992 _mouseover_last_w->OnMouseOver(pt, 0);
1993 }
1994
1995 /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
1996 _mouseover_last_w = w;
1997
1998 if (w != nullptr) {
1999 /* send an event in client coordinates. */
2000 Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2001 const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
2002 if (widget != nullptr) w->OnMouseOver(pt, widget->GetIndex());
2003 }
2004}
2005
2007 enum PreventHideDirection : uint8_t {
2008 PHD_UP,
2009 PHD_DOWN,
2010};
2011
2022 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
2023{
2024 if (v == nullptr) return;
2025
2026 const int min_visible = rect.Height();
2027
2028 int v_bottom = v->top + v->height - 1;
2029 int v_right = v->left + v->width - 1;
2030 int safe_y = (dir == PHD_UP) ? (v->top - min_visible - rect.top) : (v_bottom + min_visible - rect.bottom); // Compute safe vertical position.
2031
2032 if (*ny + rect.top <= v->top - min_visible) return; // Above v is enough space
2033 if (*ny + rect.bottom >= v_bottom + min_visible) return; // Below v is enough space
2034
2035 /* Vertically, the rectangle is hidden behind v. */
2036 if (*nx + rect.left + min_visible < v->left) { // At left of v.
2037 if (v->left < min_visible) *ny = safe_y; // But enough room, force it to a safe position.
2038 return;
2039 }
2040 if (*nx + rect.right - min_visible > v_right) { // At right of v.
2041 if (v_right > _screen.width - min_visible) *ny = safe_y; // Not enough room, force it to a safe position.
2042 return;
2043 }
2044
2045 /* Horizontally also hidden, force movement to a safe area. */
2046 if (px + rect.left < v->left && v->left >= min_visible) { // Coming from the left, and enough room there.
2047 *nx = v->left - min_visible - rect.left;
2048 } else if (px + rect.right > v_right && v_right <= _screen.width - min_visible) { // Coming from the right, and enough room there.
2049 *nx = v_right + min_visible - rect.right;
2050 } else {
2051 *ny = safe_y;
2052 }
2053}
2054
2062 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2063{
2064 /* Search for the title bar rectangle. */
2065 const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2066 if (caption != nullptr) {
2067 const Rect caption_rect = caption->GetCurrentRect();
2068
2069 const int min_visible = caption_rect.Height();
2070
2071 /* Make sure the window doesn't leave the screen */
2072 nx = Clamp(nx, min_visible - caption_rect.right, _screen.width - min_visible - caption_rect.left);
2073 ny = Clamp(ny, 0, _screen.height - min_visible);
2074
2075 /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2076 PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2077 PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2078 }
2079
2080 if (w->viewport != nullptr) {
2081 w->viewport->left += nx - w->left;
2082 w->viewport->top += ny - w->top;
2083 }
2084
2085 w->left = nx;
2086 w->top = ny;
2087}
2088
2099 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen, bool schedule_resize)
2100{
2101 if (delta_x != 0 || delta_y != 0) {
2102 if (clamp_to_screen) {
2103 /* Determine the new right/bottom position. If that is outside of the bounds of
2104 * the resolution clamp it in such a manner that it stays within the bounds. */
2105 int new_right = w->left + w->width + delta_x;
2106 int new_bottom = w->top + w->height + delta_y;
2107 if (new_right >= (int)_screen.width) delta_x -= Ceil(new_right - _screen.width, std::max(1U, w->nested_root->resize_x));
2108 if (new_bottom >= (int)_screen.height) delta_y -= Ceil(new_bottom - _screen.height, std::max(1U, w->nested_root->resize_y));
2109 }
2110
2111 w->SetDirty();
2112
2113 uint new_xinc = std::max(0, (w->nested_root->resize_x == 0) ? 0 : (int)(w->nested_root->current_x - w->nested_root->smallest_x) + delta_x);
2114 uint new_yinc = std::max(0, (w->nested_root->resize_y == 0) ? 0 : (int)(w->nested_root->current_y - w->nested_root->smallest_y) + delta_y);
2115 assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2116 assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2117
2118 w->nested_root->AssignSizePosition(ST_RESIZE, 0, 0, w->nested_root->smallest_x + new_xinc, w->nested_root->smallest_y + new_yinc, _current_text_dir == TD_RTL);
2119 w->width = w->nested_root->current_x;
2120 w->height = w->nested_root->current_y;
2121 }
2122
2123 EnsureVisibleCaption(w, w->left, w->top);
2124
2125 /* Schedule OnResize to make sure everything is initialised correctly if it needs to be. */
2126 if (schedule_resize) {
2127 w->ScheduleResize();
2128 } else {
2129 w->OnResize();
2130 }
2131 w->SetDirty();
2132}
2133
2139 int GetMainViewTop()
2140{
2141 Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
2142 return (w == nullptr) ? 0 : w->top + w->height;
2143}
2144
2150 int GetMainViewBottom()
2151{
2152 Window *w = FindWindowById(WC_STATUS_BAR, 0);
2153 return (w == nullptr) ? _screen.height : w->top;
2154}
2155
2156 static bool _dragging_window;
2157
2162 static EventState HandleWindowDragging()
2163{
2164 /* Get out immediately if no window is being dragged at all. */
2165 if (!_dragging_window) return ES_NOT_HANDLED;
2166
2167 /* If button still down, but cursor hasn't moved, there is nothing to do. */
2168 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2169
2170 /* Otherwise find the window... */
2171 for (Window *w : Window::Iterate()) {
2172 if (w->flags.Test(WindowFlag::Dragging)) {
2173 /* Stop the dragging if the left mouse button was released */
2174 if (!_left_button_down) {
2175 w->flags.Reset(WindowFlag::Dragging);
2176 break;
2177 }
2178
2179 w->SetDirty();
2180
2181 int x = _cursor.pos.x + _drag_delta.x;
2182 int y = _cursor.pos.y + _drag_delta.y;
2183 int nx = x;
2184 int ny = y;
2185
2186 if (_settings_client.gui.window_snap_radius != 0) {
2187 int hsnap = ScaleGUITrad(_settings_client.gui.window_snap_radius);
2188 int vsnap = ScaleGUITrad(_settings_client.gui.window_snap_radius);
2189 int delta;
2190
2191 for (const Window *v : Window::Iterate()) {
2192 if (v == w) continue; // Don't snap at yourself
2193
2194 if (y + w->height > v->top && y < v->top + v->height) {
2195 /* Your left border <-> other right border */
2196 delta = abs(v->left + v->width - x);
2197 if (delta <= hsnap) {
2198 nx = v->left + v->width;
2199 hsnap = delta;
2200 }
2201
2202 /* Your right border <-> other left border */
2203 delta = abs(v->left - x - w->width);
2204 if (delta <= hsnap) {
2205 nx = v->left - w->width;
2206 hsnap = delta;
2207 }
2208 }
2209
2210 if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2211 /* Your left border <-> other left border */
2212 delta = abs(v->left - x);
2213 if (delta <= hsnap) {
2214 nx = v->left;
2215 hsnap = delta;
2216 }
2217
2218 /* Your right border <-> other right border */
2219 delta = abs(v->left + v->width - x - w->width);
2220 if (delta <= hsnap) {
2221 nx = v->left + v->width - w->width;
2222 hsnap = delta;
2223 }
2224 }
2225
2226 if (x + w->width > v->left && x < v->left + v->width) {
2227 /* Your top border <-> other bottom border */
2228 delta = abs(v->top + v->height - y);
2229 if (delta <= vsnap) {
2230 ny = v->top + v->height;
2231 vsnap = delta;
2232 }
2233
2234 /* Your bottom border <-> other top border */
2235 delta = abs(v->top - y - w->height);
2236 if (delta <= vsnap) {
2237 ny = v->top - w->height;
2238 vsnap = delta;
2239 }
2240 }
2241
2242 if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2243 /* Your top border <-> other top border */
2244 delta = abs(v->top - y);
2245 if (delta <= vsnap) {
2246 ny = v->top;
2247 vsnap = delta;
2248 }
2249
2250 /* Your bottom border <-> other bottom border */
2251 delta = abs(v->top + v->height - y - w->height);
2252 if (delta <= vsnap) {
2253 ny = v->top + v->height - w->height;
2254 vsnap = delta;
2255 }
2256 }
2257 }
2258 }
2259
2260 EnsureVisibleCaption(w, nx, ny);
2261
2262 w->SetDirty();
2263 return ES_HANDLED;
2264 } else if (w->flags.Test(WindowFlag::SizingLeft) || w->flags.Test(WindowFlag::SizingRight)) {
2265 /* Stop the sizing if the left mouse button was released */
2266 if (!_left_button_down) {
2267 w->flags.Reset(WindowFlag::SizingLeft);
2268 w->flags.Reset(WindowFlag::SizingRight);
2269 w->SetDirty();
2270 break;
2271 }
2272
2273 /* Compute difference in pixels between cursor position and reference point in the window.
2274 * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2275 */
2276 int x, y = _cursor.pos.y - _drag_delta.y;
2277 if (w->flags.Test(WindowFlag::SizingLeft)) {
2278 x = _drag_delta.x - _cursor.pos.x;
2279 } else {
2280 x = _cursor.pos.x - _drag_delta.x;
2281 }
2282
2283 /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2284 if (w->resize.step_width == 0) x = 0;
2285 if (w->resize.step_height == 0) y = 0;
2286
2287 /* Check the resize button won't go past the bottom of the screen */
2288 if (w->top + w->height + y > _screen.height) {
2289 y = _screen.height - w->height - w->top;
2290 }
2291
2292 /* X and Y has to go by step.. calculate it.
2293 * The cast to int is necessary else x/y are implicitly cast to
2294 * unsigned int, which won't work. */
2295 if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2296 if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2297
2298 /* Check that we don't go below the minimum set size */
2299 if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2300 x = w->nested_root->smallest_x - w->width;
2301 }
2302 if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2303 y = w->nested_root->smallest_y - w->height;
2304 }
2305
2306 /* Window already on size */
2307 if (x == 0 && y == 0) return ES_HANDLED;
2308
2309 /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2310 _drag_delta.y += y;
2311 if (w->flags.Test(WindowFlag::SizingLeft) && x != 0) {
2312 _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2313 w->SetDirty();
2314 w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2315 /* ResizeWindow() below ensures marking new position as dirty. */
2316 } else {
2317 _drag_delta.x += x;
2318 }
2319
2320 /* ResizeWindow sets both pre- and after-size to dirty for redrawing */
2321 ResizeWindow(w, x, y);
2322 return ES_HANDLED;
2323 }
2324 }
2325
2326 _dragging_window = false;
2327 return ES_HANDLED;
2328}
2329
2334 static void StartWindowDrag(Window *w)
2335{
2336 w->flags.Set(WindowFlag::Dragging);
2337 w->flags.Reset(WindowFlag::Centred);
2338 _dragging_window = true;
2339
2340 _drag_delta.x = w->left - _cursor.pos.x;
2341 _drag_delta.y = w->top - _cursor.pos.y;
2342
2343 BringWindowToFront(w);
2344}
2345
2351 static void StartWindowSizing(Window *w, bool to_left)
2352{
2353 w->flags.Set(to_left ? WindowFlag::SizingLeft : WindowFlag::SizingRight);
2354 w->flags.Reset(WindowFlag::Centred);
2355 _dragging_window = true;
2356
2357 _drag_delta.x = _cursor.pos.x;
2358 _drag_delta.y = _cursor.pos.y;
2359
2360 BringWindowToFront(w);
2361}
2362
2367 static void HandleScrollbarScrolling(Window *w)
2368{
2369 int i;
2370 NWidgetScrollbar *sb = w->GetWidget<NWidgetScrollbar>(w->mouse_capture_widget);
2371 bool rtl = false;
2372
2373 if (sb->type == NWID_HSCROLLBAR) {
2374 i = _cursor.pos.x - _cursorpos_drag_start.x;
2375 rtl = _current_text_dir == TD_RTL;
2376 } else {
2377 i = _cursor.pos.y - _cursorpos_drag_start.y;
2378 }
2379
2380 if (sb->disp_flags.Any({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown})) {
2381 if (_scroller_click_timeout == 1) {
2382 _scroller_click_timeout = 3;
2383 if (sb->UpdatePosition(rtl == sb->disp_flags.Test(NWidgetDisplayFlag::ScrollbarUp) ? 1 : -1)) {
2384 w->OnScrollbarScroll(w->mouse_capture_widget);
2385 w->SetDirty();
2386 }
2387 }
2388 return;
2389 }
2390
2391 /* Find the item we want to move to. SetPosition will make sure it's inside bounds. */
2392 int range = sb->GetCount() - sb->GetCapacity();
2393 if (range <= 0) return;
2394
2395 int pos = RoundDivSU((i + _scrollbar_start_pos) * range, std::max(1, _scrollbar_size));
2396 if (rtl) pos = range - pos;
2397 if (sb->SetPosition(pos)) {
2398 w->OnScrollbarScroll(w->mouse_capture_widget);
2399 w->SetDirty();
2400 }
2401}
2402
2407 static EventState HandleActiveWidget()
2408{
2409 for (Window *w : Window::Iterate()) {
2410 if (w->mouse_capture_widget >= 0) {
2411 /* Abort if no button is clicked any more. */
2412 if (!_left_button_down) {
2413 w->SetWidgetDirty(w->mouse_capture_widget);
2414 w->mouse_capture_widget = INVALID_WIDGET;
2415 return ES_HANDLED;
2416 }
2417
2418 /* Handle scrollbar internally, or dispatch click event */
2419 WidgetType type = w->GetWidget<NWidgetBase>(w->mouse_capture_widget)->type;
2420 if (type == NWID_VSCROLLBAR || type == NWID_HSCROLLBAR) {
2421 HandleScrollbarScrolling(w);
2422 } else {
2423 /* If cursor hasn't moved, there is nothing to do. */
2424 if (_cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2425
2426 Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2427 w->OnClick(pt, w->mouse_capture_widget, 0);
2428 }
2429 return ES_HANDLED;
2430 }
2431 }
2432
2433 return ES_NOT_HANDLED;
2434}
2435
2440 static EventState HandleViewportScroll()
2441{
2442 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == SWS_SCROLL_MAP && _cursor.wheel_moved;
2443
2444 if (!_scrolling_viewport) return ES_NOT_HANDLED;
2445
2446 /* When we don't have a last scroll window we are starting to scroll.
2447 * When the last scroll window and this are not the same we went
2448 * outside of the window and should not left-mouse scroll anymore. */
2449 if (_last_scroll_window == nullptr) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2450
2451 if (_last_scroll_window == nullptr || !((_settings_client.gui.scroll_mode != VSM_MAP_LMB && _right_button_down) || scrollwheel_scrolling || (_settings_client.gui.scroll_mode == VSM_MAP_LMB && _left_button_down))) {
2452 _cursor.fix_at = false;
2453 _scrolling_viewport = false;
2454 _last_scroll_window = nullptr;
2455 return ES_NOT_HANDLED;
2456 }
2457
2458 if (_last_scroll_window == GetMainWindow() && _last_scroll_window->viewport->follow_vehicle != VehicleID::Invalid()) {
2459 /* If the main window is following a vehicle, then first let go of it! */
2460 const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2461 ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2462 return ES_NOT_HANDLED;
2463 }
2464
2465 Point delta;
2466 if (scrollwheel_scrolling) {
2467 /* We are using scrollwheels for scrolling */
2468 /* Use the integer part for movement */
2469 delta.x = static_cast<int>(_cursor.h_wheel);
2470 delta.y = static_cast<int>(_cursor.v_wheel);
2471 /* Keep the fractional part so that subtle movement is accumulated */
2472 float temp;
2473 _cursor.v_wheel = std::modf(_cursor.v_wheel, &temp);
2474 _cursor.h_wheel = std::modf(_cursor.h_wheel, &temp);
2475 } else {
2476 if (_settings_client.gui.scroll_mode != VSM_VIEWPORT_RMB_FIXED) {
2477 delta.x = -_cursor.delta.x;
2478 delta.y = -_cursor.delta.y;
2479 } else {
2480 delta.x = _cursor.delta.x;
2481 delta.y = _cursor.delta.y;
2482 }
2483 }
2484
2485 /* Create a scroll-event and send it to the window */
2486 if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2487
2488 _cursor.delta.x = 0;
2489 _cursor.delta.y = 0;
2490 _cursor.wheel_moved = false;
2491 return ES_HANDLED;
2492}
2493
2504 static bool MaybeBringWindowToFront(Window *w)
2505{
2506 bool bring_to_front = false;
2507
2508 if (w->window_class == WC_MAIN_WINDOW ||
2509 IsVitalWindow(w) ||
2510 w->window_class == WC_TOOLTIPS ||
2511 w->window_class == WC_DROPDOWN_MENU) {
2512 return true;
2513 }
2514
2515 /* Use unshaded window size rather than current size for shaded windows. */
2516 int w_width = w->width;
2517 int w_height = w->height;
2518 if (w->IsShaded()) {
2519 w_width = w->unshaded_size.width;
2520 w_height = w->unshaded_size.height;
2521 }
2522
2523 Window::IteratorToFront it(w);
2524 ++it;
2525 for (; !it.IsEnd(); ++it) {
2526 Window *u = *it;
2527 /* A modal child will prevent the activation of the parent window */
2528 if (u->parent == w && u->window_desc.flags.Test(WindowDefaultFlag::Modal)) {
2529 u->SetWhiteBorder();
2530 u->SetDirty();
2531 return false;
2532 }
2533
2534 if (u->window_class == WC_MAIN_WINDOW ||
2535 IsVitalWindow(u) ||
2536 u->window_class == WC_TOOLTIPS ||
2537 u->window_class == WC_DROPDOWN_MENU) {
2538 continue;
2539 }
2540
2541 /* Window sizes don't interfere, leave z-order alone */
2542 if (w->left + w_width <= u->left ||
2543 u->left + u->width <= w->left ||
2544 w->top + w_height <= u->top ||
2545 u->top + u->height <= w->top) {
2546 continue;
2547 }
2548
2549 bring_to_front = true;
2550 }
2551
2552 if (bring_to_front) BringWindowToFront(w);
2553 return true;
2554}
2555
2564 EventState Window::HandleEditBoxKey(WidgetID wid, char32_t key, uint16_t keycode)
2565{
2566 QueryString *query = this->GetQueryString(wid);
2567 if (query == nullptr) return ES_NOT_HANDLED;
2568
2569 int action = QueryString::ACTION_NOTHING;
2570
2571 switch (query->text.HandleKeyPress(key, keycode)) {
2572 case HKPR_EDITING:
2573 this->SetWidgetDirty(wid);
2574 this->OnEditboxChanged(wid);
2575 break;
2576
2577 case HKPR_CURSOR:
2578 this->SetWidgetDirty(wid);
2579 /* For the OSK also invalidate the parent window */
2580 if (this->window_class == WC_OSK) this->InvalidateData();
2581 break;
2582
2583 case HKPR_CONFIRM:
2584 if (this->window_class == WC_OSK) {
2585 this->OnClick(Point(), WID_OSK_OK, 1);
2586 } else if (query->ok_button >= 0) {
2587 this->OnClick(Point(), query->ok_button, 1);
2588 } else {
2589 action = query->ok_button;
2590 }
2591 break;
2592
2593 case HKPR_CANCEL:
2594 if (this->window_class == WC_OSK) {
2595 this->OnClick(Point(), WID_OSK_CANCEL, 1);
2596 } else if (query->cancel_button >= 0) {
2597 this->OnClick(Point(), query->cancel_button, 1);
2598 } else {
2599 action = query->cancel_button;
2600 }
2601 break;
2602
2603 case HKPR_NOT_HANDLED:
2604 return ES_NOT_HANDLED;
2605
2606 default: break;
2607 }
2608
2609 switch (action) {
2610 case QueryString::ACTION_DESELECT:
2611 this->UnfocusFocusedWidget();
2612 break;
2613
2614 case QueryString::ACTION_CLEAR:
2615 if (query->text.GetText().empty()) {
2616 /* If already empty, unfocus instead */
2617 this->UnfocusFocusedWidget();
2618 } else {
2619 query->text.DeleteAll();
2620 this->SetWidgetDirty(wid);
2621 this->OnEditboxChanged(wid);
2622 }
2623 break;
2624
2625 default:
2626 break;
2627 }
2628
2629 return ES_HANDLED;
2630}
2631
2636 void HandleToolbarHotkey(int hotkey)
2637{
2638 assert(HasModalProgress() || IsLocalCompany());
2639
2640 Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
2641 if (w != nullptr) {
2642 if (w->window_desc.hotkeys != nullptr) {
2643 if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2644 }
2645 }
2646}
2647
2653 void HandleKeypress(uint keycode, char32_t key)
2654{
2655 /* World generation is multithreaded and messes with companies.
2656 * But there is no company related window open anyway, so _current_company is not used. */
2657 assert(HasModalProgress() || IsLocalCompany());
2658
2659 /*
2660 * The Unicode standard defines an area called the private use area. Code points in this
2661 * area are reserved for private use and thus not portable between systems. For instance,
2662 * Apple defines code points for the arrow keys in this area, but these are only printable
2663 * on a system running OS X. We don't want these keys to show up in text fields and such,
2664 * and thus we have to clear the unicode character when we encounter such a key.
2665 */
2666 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2667
2668 /*
2669 * If both key and keycode is zero, we don't bother to process the event.
2670 */
2671 if (key == 0 && keycode == 0) return;
2672
2673 /* Check if the focused window has a focused editbox */
2674 if (EditBoxInGlobalFocus()) {
2675 /* All input will in this case go to the focused editbox */
2676 if (_focused_window->window_class == WC_CONSOLE) {
2677 if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2678 } else {
2679 if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->GetIndex(), key, keycode) == ES_HANDLED) return;
2680 }
2681 }
2682
2683 /* Call the event, start with the uppermost window, but ignore the toolbar. */
2684 for (Window *w : Window::IterateFromFront()) {
2685 if (w->window_class == WC_MAIN_TOOLBAR) continue;
2686 if (w->window_desc.hotkeys != nullptr) {
2687 int hotkey = w->window_desc.hotkeys->CheckMatch(keycode);
2688 if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2689 }
2690 if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2691 }
2692
2693 Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
2694 /* When there is no toolbar w is null, check for that */
2695 if (w != nullptr) {
2696 if (w->window_desc.hotkeys != nullptr) {
2697 int hotkey = w->window_desc.hotkeys->CheckMatch(keycode);
2698 if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2699 }
2700 if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2701 }
2702
2703 HandleGlobalHotkeys(key, keycode);
2704}
2705
2709 void HandleCtrlChanged()
2710{
2711 /* Call the event, start with the uppermost window. */
2712 for (Window *w : Window::IterateFromFront()) {
2713 if (w->OnCTRLStateChange() == ES_HANDLED) return;
2714 }
2715}
2716
2722/* virtual */ void Window::InsertTextString(WidgetID wid, std::string_view str, bool marked, std::optional<size_t> caret, std::optional<size_t> insert_location, std::optional<size_t> replacement_end)
2723{
2724 QueryString *query = this->GetQueryString(wid);
2725 if (query == nullptr) return;
2726
2727 if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2728 this->SetWidgetDirty(wid);
2729 this->OnEditboxChanged(wid);
2730 }
2731}
2732
2739 void HandleTextInput(std::string_view str, bool marked, std::optional<size_t> caret, std::optional<size_t> insert_location, std::optional<size_t> replacement_end)
2740{
2741 if (!EditBoxInGlobalFocus()) return;
2742
2743 _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->GetIndex(), str, marked, caret, insert_location, replacement_end);
2744}
2745
2750 static void HandleAutoscroll()
2751{
2752 if (_game_mode == GM_MENU || HasModalProgress()) return;
2753 if (_settings_client.gui.auto_scrolling == VA_DISABLED) return;
2754 if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2755
2756 int x = _cursor.pos.x;
2757 int y = _cursor.pos.y;
2758 Window *w = FindWindowFromPt(x, y);
2759 if (w == nullptr || w->flags.Test(WindowFlag::DisableVpScroll)) return;
2760 if (_settings_client.gui.auto_scrolling != VA_EVERY_VIEWPORT && w->window_class != WC_MAIN_WINDOW) return;
2761
2762 Viewport *vp = IsPtInWindowViewport(w, x, y);
2763 if (vp == nullptr) return;
2764
2765 x -= vp->left;
2766 y -= vp->top;
2767
2768 /* here allows scrolling in both x and y axis */
2769 /* If we succeed at scrolling in any direction, stop following a vehicle. */
2770 static const int SCROLLSPEED = 3;
2771 if (x - 15 < 0) {
2772 w->viewport->CancelFollow(*w);
2773 w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * SCROLLSPEED, vp->zoom);
2774 } else if (15 - (vp->width - x) > 0) {
2775 w->viewport->CancelFollow(*w);
2776 w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * SCROLLSPEED, vp->zoom);
2777 }
2778 if (y - 15 < 0) {
2779 w->viewport->CancelFollow(*w);
2780 w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * SCROLLSPEED, vp->zoom);
2781 } else if (15 - (vp->height - y) > 0) {
2782 w->viewport->CancelFollow(*w);
2783 w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * SCROLLSPEED, vp->zoom);
2784 }
2785}
2786
2787enum MouseClick : uint8_t {
2788 MC_NONE = 0,
2789 MC_LEFT,
2790 MC_RIGHT,
2791 MC_DOUBLE_LEFT,
2792 MC_HOVER,
2793};
2794
2795 static constexpr int MAX_OFFSET_DOUBLE_CLICK = 5;
2796 static constexpr int MAX_OFFSET_HOVER = 5;
2797
2798extern EventState VpHandlePlaceSizingDrag();
2799
2800 const std::chrono::milliseconds TIME_BETWEEN_DOUBLE_CLICK(500);
2801
2802static void ScrollMainViewport(int x, int y)
2803{
2804 if (_game_mode != GM_MENU && _game_mode != GM_BOOTSTRAP) {
2805 Window *w = GetMainWindow();
2806 w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
2807 w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
2808 }
2809}
2810
2820 static const int8_t scrollamt[16][2] = {
2821 { 0, 0},
2822 {-2, 0},
2823 { 0, -2},
2824 {-2, -1},
2825 { 2, 0},
2826 { 0, 0},
2827 { 2, -1},
2828 { 0, -2},
2829 { 0, 2},
2830 {-2, 1},
2831 { 0, 0},
2832 {-2, 0},
2833 { 2, 1},
2834 { 0, 2},
2835 { 2, 0},
2836 { 0, 0},
2837};
2838
2839static void HandleKeyScrolling()
2840{
2841 /*
2842 * Check that any of the dirkeys is pressed and that the focused window
2843 * doesn't have an edit-box as focused widget.
2844 */
2845 if (_dirkeys && !EditBoxInGlobalFocus()) {
2846 int factor = _shift_pressed ? 50 : 10;
2847
2848 if (_game_mode != GM_MENU && _game_mode != GM_BOOTSTRAP) {
2849 /* Key scrolling stops following a vehicle. */
2850 Window *main_window = GetMainWindow();
2851 main_window->viewport->CancelFollow(*main_window);
2852 }
2853
2854 ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2855 }
2856}
2857
2858static void MouseLoop(MouseClick click, int mousewheel)
2859{
2860 /* World generation is multithreaded and messes with companies.
2861 * But there is no company related window open anyway, so _current_company is not used. */
2862 assert(HasModalProgress() || IsLocalCompany());
2863
2864 HandlePlacePresize();
2865 UpdateTileSelection();
2866
2867 if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2868 if (HandleMouseDragDrop() == ES_HANDLED) return;
2869 if (HandleWindowDragging() == ES_HANDLED) return;
2870 if (HandleActiveWidget() == ES_HANDLED) return;
2871 if (HandleViewportScroll() == ES_HANDLED) return;
2872
2873 HandleMouseOver();
2874
2875 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == SWS_SCROLL_MAP && _cursor.wheel_moved;
2876 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2877
2878 int x = _cursor.pos.x;
2879 int y = _cursor.pos.y;
2880 Window *w = FindWindowFromPt(x, y);
2881 if (w == nullptr) return;
2882
2883 if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2884 Viewport *vp = IsPtInWindowViewport(w, x, y);
2885
2886 /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2887 if (vp != nullptr && (_game_mode == GM_MENU || HasModalProgress())) return;
2888
2889 if (mousewheel != 0) {
2890 /* Send mousewheel event to window, unless we're scrolling a viewport or the map */
2891 if (!scrollwheel_scrolling || (vp == nullptr && w->window_class != WC_SMALLMAP)) {
2892 if (NWidgetCore *nwid = w->nested_root->GetWidgetFromPos(x - w->left, y - w->top); nwid != nullptr) {
2893 w->OnMouseWheel(mousewheel, nwid->GetIndex());
2894 }
2895 }
2896
2897 /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2898 if (vp == nullptr) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2899 }
2900
2901 if (vp != nullptr) {
2902 if (scrollwheel_scrolling && !w->flags.Test(WindowFlag::DisableVpScroll)) {
2903 _scrolling_viewport = true;
2904 _cursor.fix_at = true;
2905 return;
2906 }
2907
2908 switch (click) {
2909 case MC_DOUBLE_LEFT:
2910 case MC_LEFT:
2911 if (HandleViewportClicked(*vp, x, y)) return;
2912 if (!w->flags.Test(WindowFlag::DisableVpScroll) &&
2913 _settings_client.gui.scroll_mode == VSM_MAP_LMB) {
2914 _scrolling_viewport = true;
2915 _cursor.fix_at = false;
2916 return;
2917 }
2918 break;
2919
2920 case MC_RIGHT:
2921 if (!w->flags.Test(WindowFlag::DisableVpScroll) &&
2922 _settings_client.gui.scroll_mode != VSM_MAP_LMB) {
2923 _scrolling_viewport = true;
2924 _cursor.fix_at = (_settings_client.gui.scroll_mode == VSM_VIEWPORT_RMB_FIXED ||
2925 _settings_client.gui.scroll_mode == VSM_MAP_RMB_FIXED);
2926 DispatchRightClickEvent(w, x - w->left, y - w->top);
2927 return;
2928 }
2929 break;
2930
2931 default:
2932 break;
2933 }
2934 }
2935
2936 switch (click) {
2937 case MC_LEFT:
2938 case MC_DOUBLE_LEFT:
2939 DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2940 return;
2941
2942 default:
2943 if (!scrollwheel_scrolling || w == nullptr || w->window_class != WC_SMALLMAP) break;
2944 /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2945 * Simulate a right button click so we can get started. */
2946 [[fallthrough]];
2947
2948 case MC_RIGHT:
2949 DispatchRightClickEvent(w, x - w->left, y - w->top);
2950 return;
2951
2952 case MC_HOVER:
2953 DispatchHoverEvent(w, x - w->left, y - w->top);
2954 break;
2955 }
2956
2957 /* We're not doing anything with 2D scrolling, so reset the value. */
2958 _cursor.h_wheel = 0.0f;
2959 _cursor.v_wheel = 0.0f;
2960 _cursor.wheel_moved = false;
2961}
2962
2966 void HandleMouseEvents()
2967{
2968 /* World generation is multithreaded and messes with companies.
2969 * But there is no company related window open anyway, so _current_company is not used. */
2970 assert(HasModalProgress() || IsLocalCompany());
2971
2972 static std::chrono::steady_clock::time_point double_click_time = {};
2973 static Point double_click_pos = {0, 0};
2974
2975 /* Mouse event? */
2976 MouseClick click = MC_NONE;
2977 if (_left_button_down && !_left_button_clicked) {
2978 click = MC_LEFT;
2979 if (std::chrono::steady_clock::now() <= double_click_time + TIME_BETWEEN_DOUBLE_CLICK &&
2980 double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2981 double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2982 click = MC_DOUBLE_LEFT;
2983 }
2984 double_click_time = std::chrono::steady_clock::now();
2985 double_click_pos = _cursor.pos;
2986 _left_button_clicked = true;
2987 } else if (_right_button_clicked) {
2988 _right_button_clicked = false;
2989 click = MC_RIGHT;
2990 }
2991
2992 int mousewheel = 0;
2993 if (_cursor.wheel) {
2994 mousewheel = _cursor.wheel;
2995 _cursor.wheel = 0;
2996 }
2997
2998 static std::chrono::steady_clock::time_point hover_time = {};
2999 static Point hover_pos = {0, 0};
3000
3001 if (_settings_client.gui.hover_delay_ms > 0) {
3002 if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
3003 hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
3004 hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
3005 hover_pos = _cursor.pos;
3006 hover_time = std::chrono::steady_clock::now();
3007 _mouse_hovering = false;
3008 } else if (!_mouse_hovering) {
3009 if (std::chrono::steady_clock::now() > hover_time + std::chrono::milliseconds(_settings_client.gui.hover_delay_ms)) {
3010 click = MC_HOVER;
3011 _mouse_hovering = true;
3012 hover_time = std::chrono::steady_clock::now();
3013 }
3014 }
3015 }
3016
3017 if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
3018 /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
3019 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
3020 _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
3021 _newgrf_debug_sprite_picker.sprites.clear();
3022 _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
3023 MarkWholeScreenDirty();
3024 } else {
3025 MouseLoop(click, mousewheel);
3026 }
3027
3028 /* We have moved the mouse the required distance,
3029 * no need to move it at any later time. */
3030 _cursor.delta.x = 0;
3031 _cursor.delta.y = 0;
3032}
3033
3037 static void CheckSoftLimit()
3038{
3039 if (_settings_client.gui.window_soft_limit == 0) return;
3040
3041 for (;;) {
3042 uint deletable_count = 0;
3043 Window *last_deletable = nullptr;
3044 for (Window *w : Window::IterateFromFront()) {
3045 if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || w->flags.Test(WindowFlag::Sticky)) continue;
3046
3047 last_deletable = w;
3048 deletable_count++;
3049 }
3050
3051 /* We've not reached the soft limit yet. */
3052 if (deletable_count <= _settings_client.gui.window_soft_limit) break;
3053
3054 assert(last_deletable != nullptr);
3055 last_deletable->Close();
3056 }
3057}
3058
3062 void InputLoop()
3063{
3064 /* World generation is multithreaded and messes with companies.
3065 * But there is no company related window open anyway, so _current_company is not used. */
3066 assert(HasModalProgress() || IsLocalCompany());
3067
3068 CheckSoftLimit();
3069
3070 /* Process scheduled window deletion. */
3071 Window::DeleteClosedWindows();
3072
3073 /* HandleMouseEvents was already called for this tick */
3074 HandleMouseEvents();
3075}
3076
3077static std::chrono::time_point<std::chrono::steady_clock> _realtime_tick_start;
3078
3079bool CanContinueRealtimeTick()
3080{
3081 auto now = std::chrono::steady_clock::now();
3082 return std::chrono::duration_cast<std::chrono::milliseconds>(now - _realtime_tick_start).count() < (MILLISECONDS_PER_TICK * 3 / 4);
3083}
3084
3088 void CallWindowRealtimeTickEvent(uint delta_ms)
3089{
3090 _realtime_tick_start = std::chrono::steady_clock::now();
3091 for (Window *w : Window::Iterate()) {
3092 w->OnRealtimeTick(delta_ms);
3093 }
3094}
3095
3097 static const IntervalTimer<TimerWindow> window_interval(std::chrono::milliseconds(30), [](auto) {
3098 extern int _caret_timer;
3099 _caret_timer += 3;
3100 CursorTick();
3101
3102 HandleKeyScrolling();
3103 HandleAutoscroll();
3104 DecreaseWindowCounters();
3105});
3106
3108 static const IntervalTimer<TimerWindow> highlight_interval(TIMER_BLINK_INTERVAL, [](auto) {
3109 _window_highlight_colour = !_window_highlight_colour;
3110});
3111
3113 static const IntervalTimer<TimerWindow> white_border_interval(std::chrono::milliseconds(30), [](auto) {
3114 if (_network_dedicated) return;
3115
3116 for (Window *w : Window::Iterate()) {
3117 if (w->flags.Test(WindowFlag::WhiteBorder) && --w->white_border_timer == 0) {
3118 w->flags.Reset(WindowFlag::WhiteBorder);
3119 w->SetDirty();
3120 }
3121 }
3122});
3123
3127 void UpdateWindows()
3128{
3129 static auto last_time = std::chrono::steady_clock::now();
3130 auto now = std::chrono::steady_clock::now();
3131 auto delta_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_time);
3132
3133 if (delta_ms.count() == 0) return;
3134
3135 last_time = now;
3136
3137 PerformanceMeasurer framerate(PFE_DRAWING);
3138 PerformanceAccumulator::Reset(PFE_DRAWWORLD);
3139
3140 ProcessPendingPerformanceMeasurements();
3141
3142 TimerManager<TimerWindow>::Elapsed(delta_ms);
3143 CallWindowRealtimeTickEvent(delta_ms.count());
3144
3145 /* Process invalidations before anything else. */
3146 for (Window *w : Window::Iterate()) {
3147 w->ProcessScheduledResize();
3148 w->ProcessScheduledInvalidations();
3149 w->ProcessHighlightedInvalidations();
3150 }
3151
3152 /* Skip the actual drawing on dedicated servers without screen.
3153 * But still empty the invalidation queues above. */
3154 if (_network_dedicated) return;
3155
3156 DrawDirtyBlocks();
3157
3158 for (Window *w : Window::Iterate()) {
3159 /* Update viewport only if window is not shaded. */
3160 if (w->viewport != nullptr && !w->IsShaded()) UpdateViewportPosition(w, delta_ms.count());
3161 }
3162 NetworkDrawChatMessage();
3163 /* Redraw mouse cursor in case it was hidden */
3164 DrawMouseCursor();
3165
3166 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW) {
3167 /* We are done with the last draw-frame, so we know what sprites we
3168 * clicked on. Reset the picker mode and invalidate the window. */
3169 _newgrf_debug_sprite_picker.mode = SPM_NONE;
3170 InvalidateWindowData(WC_SPRITE_ALIGNER, 0, 1);
3171 }
3172}
3173
3179 void SetWindowDirty(WindowClass cls, WindowNumber number)
3180{
3181 for (const Window *w : Window::Iterate()) {
3182 if (w->window_class == cls && w->window_number == number) w->SetDirty();
3183 }
3184}
3185
3192 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, WidgetID widget_index)
3193{
3194 for (const Window *w : Window::Iterate()) {
3195 if (w->window_class == cls && w->window_number == number) {
3196 w->SetWidgetDirty(widget_index);
3197 }
3198 }
3199}
3200
3205 void SetWindowClassesDirty(WindowClass cls)
3206{
3207 for (const Window *w : Window::Iterate()) {
3208 if (w->window_class == cls) w->SetDirty();
3209 }
3210}
3211
3215 void Window::ScheduleResize()
3216{
3217 this->scheduled_resize = true;
3218}
3219
3223 void Window::ProcessScheduledResize()
3224{
3225 /* Sometimes OnResize() resizes the window again, in which case we can reprocess immediately. */
3226 while (this->scheduled_resize) {
3227 this->scheduled_resize = false;
3228 this->OnResize();
3229 }
3230}
3231
3237 void Window::InvalidateData(int data, bool gui_scope)
3238{
3239 this->SetDirty();
3240 if (!gui_scope) {
3241 /* Schedule GUI-scope invalidation for next redraw. */
3242 this->scheduled_invalidation_data.push_back(data);
3243 }
3244 this->OnInvalidateData(data, gui_scope);
3245}
3246
3250 void Window::ProcessScheduledInvalidations()
3251{
3252 for (int data : this->scheduled_invalidation_data) {
3253 if (this->window_class == WC_INVALID) break;
3254 this->OnInvalidateData(data, true);
3255 }
3256 this->scheduled_invalidation_data.clear();
3257}
3258
3262 void Window::ProcessHighlightedInvalidations()
3263{
3264 if (!this->flags.Test(WindowFlag::Highlighted)) return;
3265
3266 for (const auto &pair : this->widget_lookup) {
3267 if (pair.second->IsHighlighted()) pair.second->SetDirty(this);
3268 }
3269}
3270
3297 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3298{
3299 for (Window *w : Window::Iterate()) {
3300 if (w->window_class == cls && w->window_number == number) {
3301 w->InvalidateData(data, gui_scope);
3302 }
3303 }
3304}
3305
3314 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3315{
3316 for (Window *w : Window::Iterate()) {
3317 if (w->window_class == cls) {
3318 w->InvalidateData(data, gui_scope);
3319 }
3320 }
3321}
3322
3326 void CallWindowGameTickEvent()
3327{
3328 for (Window *w : Window::Iterate()) {
3329 w->OnGameTick();
3330 }
3331}
3332
3339 void CloseNonVitalWindows()
3340{
3341 /* Note: the container remains stable, even when deleting windows. */
3342 for (Window *w : Window::Iterate()) {
3343 if (!w->window_desc.flags.Test(WindowDefaultFlag::NoClose) &&
3344 !w->flags.Test(WindowFlag::Sticky)) { // do not delete windows which are 'pinned'
3345
3346 w->Close();
3347 }
3348 }
3349}
3350
3358 void CloseAllNonVitalWindows()
3359{
3360 /* Note: the container remains stable, even when closing windows. */
3361 for (Window *w : Window::Iterate()) {
3362 if (!w->window_desc.flags.Test(WindowDefaultFlag::NoClose)) {
3363 w->Close();
3364 }
3365 }
3366}
3367
3371 void DeleteAllMessages()
3372{
3373 InitNewsItemStructs();
3374 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
3375 InvalidateWindowData(WC_MESSAGE_HISTORY, 0); // invalidate the message history
3376 CloseWindowById(WC_NEWS_WINDOW, 0); // close newspaper or general message window if shown
3377}
3378
3383 void CloseConstructionWindows()
3384{
3385 /* Note: the container remains stable, even when deleting windows. */
3386 for (Window *w : Window::Iterate()) {
3387 if (w->window_desc.flags.Test(WindowDefaultFlag::Construction)) {
3388 w->Close();
3389 }
3390 }
3391
3392 for (const Window *w : Window::Iterate()) w->SetDirty();
3393}
3394
3396 void HideVitalWindows()
3397{
3398 CloseWindowById(WC_MAIN_TOOLBAR, 0);
3399 CloseWindowById(WC_STATUS_BAR, 0);
3400}
3401
3402void ReInitWindow(Window *w, bool zoom_changed)
3403{
3404 if (w == nullptr) return;
3405 if (zoom_changed) {
3406 w->nested_root->AdjustPaddingForZoom();
3407 w->UpdateQueryStringSize();
3408 }
3409 w->ReInit();
3410}
3411
3413 void ReInitAllWindows(bool zoom_changed)
3414{
3415 SetupWidgetDimensions();
3416 NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3417 NWidgetScrollbar::InvalidateDimensionCache();
3418
3419 InitDepotWindowBlockSizes();
3420
3421 /* When _gui_zoom has changed, we need to resize toolbar and statusbar first,
3422 * so EnsureVisibleCaption uses the updated size information. */
3423 ReInitWindow(FindWindowById(WC_MAIN_TOOLBAR, 0), zoom_changed);
3424 ReInitWindow(FindWindowById(WC_STATUS_BAR, 0), zoom_changed);
3425 for (Window *w : Window::Iterate()) {
3426 if (w->window_class == WC_MAIN_TOOLBAR || w->window_class == WC_STATUS_BAR) continue;
3427 ReInitWindow(w, zoom_changed);
3428 }
3429
3430 if (_networking) NetworkUndrawChatMessage();
3431 NetworkReInitChatBoxSize();
3432
3433 /* Make sure essential parts of all windows are visible */
3434 RelocateAllWindows(_screen.width, _screen.height);
3435 MarkWholeScreenDirty();
3436}
3437
3445 static int PositionWindow(Window *w, WindowClass clss, int setting)
3446{
3447 if (w == nullptr || w->window_class != clss) {
3448 w = FindWindowById(clss, 0);
3449 }
3450 if (w == nullptr) return 0;
3451
3452 int old_left = w->left;
3453 switch (setting) {
3454 case 1: w->left = (_screen.width - w->width) / 2; break;
3455 case 2: w->left = _screen.width - w->width; break;
3456 default: w->left = 0; break;
3457 }
3458 if (w->viewport != nullptr) w->viewport->left += w->left - old_left;
3459 AddDirtyBlock(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3460 return w->left;
3461}
3462
3468 int PositionMainToolbar(Window *w)
3469{
3470 Debug(misc, 5, "Repositioning Main Toolbar...");
3471 return PositionWindow(w, WC_MAIN_TOOLBAR, _settings_client.gui.toolbar_pos);
3472}
3473
3479 int PositionStatusbar(Window *w)
3480{
3481 Debug(misc, 5, "Repositioning statusbar...");
3482 return PositionWindow(w, WC_STATUS_BAR, _settings_client.gui.statusbar_pos);
3483}
3484
3490 int PositionNewsMessage(Window *w)
3491{
3492 Debug(misc, 5, "Repositioning news message...");
3493 return PositionWindow(w, WC_NEWS_WINDOW, _settings_client.gui.statusbar_pos);
3494}
3495
3501 int PositionNetworkChatWindow(Window *w)
3502{
3503 Debug(misc, 5, "Repositioning network chat window...");
3504 return PositionWindow(w, WC_SEND_NETWORK_MSG, _settings_client.gui.statusbar_pos);
3505}
3506
3507
3513 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3514{
3515 for (const Window *w : Window::Iterate()) {
3516 if (w->viewport != nullptr && w->viewport->follow_vehicle == from_index) {
3517 w->viewport->follow_vehicle = to_index;
3518 w->SetDirty();
3519 }
3520 }
3521}
3522
3523
3529 void RelocateAllWindows(int neww, int newh)
3530{
3531 CloseWindowByClass(WC_DROPDOWN_MENU);
3532
3533 /* Reposition toolbar then status bar before other all windows. */
3534 if (Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0); wt != nullptr) {
3535 ResizeWindow(wt, std::min<uint>(neww, _toolbar_width) - wt->width, 0, false);
3536 wt->left = PositionMainToolbar(wt);
3537 }
3538
3539 if (Window *ws = FindWindowById(WC_STATUS_BAR, 0); ws != nullptr) {
3540 ResizeWindow(ws, std::min<uint>(neww, _toolbar_width) - ws->width, 0, false);
3541 ws->top = newh - ws->height;
3542 ws->left = PositionStatusbar(ws);
3543 }
3544
3545 for (Window *w : Window::Iterate()) {
3546 int left, top;
3547 /* XXX - this probably needs something more sane. For example specifying
3548 * in a 'backup'-desc that the window should always be centered. */
3549 switch (w->window_class) {
3550 case WC_MAIN_WINDOW:
3551 case WC_BOOTSTRAP:
3552 case WC_HIGHSCORE:
3553 case WC_ENDSCREEN:
3554 ResizeWindow(w, neww, newh);
3555 continue;
3556
3557 case WC_MAIN_TOOLBAR:
3558 case WC_STATUS_BAR:
3559 continue;
3560
3561 case WC_NEWS_WINDOW:
3562 top = newh - w->height;
3563 left = PositionNewsMessage(w);
3564 break;
3565
3566 case WC_SEND_NETWORK_MSG:
3567 ResizeWindow(w, std::min<uint>(neww, _toolbar_width) - w->width, 0, false);
3568
3569 top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3570 left = PositionNetworkChatWindow(w);
3571 break;
3572
3573 case WC_CONSOLE:
3574 IConsoleResize(w);
3575 continue;
3576
3577 default: {
3578 if (w->flags.Test(WindowFlag::Centred)) {
3579 top = (newh - w->height) >> 1;
3580 left = (neww - w->width) >> 1;
3581 break;
3582 }
3583
3584 left = w->left;
3585 if (left + (w->width >> 1) >= neww) left = neww - w->width;
3586 if (left < 0) left = 0;
3587
3588 top = w->top;
3589 if (top + (w->height >> 1) >= newh) top = newh - w->height;
3590 break;
3591 }
3592 }
3593
3594 EnsureVisibleCaption(w, left, top);
3595 }
3596}
3597
3602 void PickerWindowBase::Close([[maybe_unused]] int data)
3603{
3604 ResetObjectToPlace();
3605 this->Window::Close();
3606}
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Flip
constexpr Timpl & Flip(Tvalue_type value)
Flip the value-th bit.
Definition base_bitset_type.hpp:116
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
BaseBitSet::Any
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Definition base_bitset_type.hpp:170
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
Blitter
How all blitters should look like.
Definition base.hpp:29
Blitter::MoveTo
virtual void * MoveTo(void *video, int x, int y)=0
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
Game::NewEvent
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition game_core.cpp:136
IntervalTimer
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
NWidgetBase
Baseclass for nested widgets.
Definition widget_type.h:137
NWidgetBase::SetDirty
virtual void SetDirty(const Window *w) const
Mark the widget as 'dirty' (in need of repaint).
Definition widget.cpp:949
NWidgetBase::type
WidgetType type
Type of the widget / nested widget.
Definition widget_type.h:227
NWidgetBase::pos_y
int pos_y
Vertical position of top-left corner of the widget in the window.
Definition widget_type.h:244
NWidgetBase::pos_x
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition widget_type.h:243
NWidgetBase::resize_y
uint resize_y
Vertical resize step (0 means not resizable).
Definition widget_type.h:231
NWidgetCore
Base class for a 'real' widget.
Definition widget_type.h:371
NWidgetCore::IsDisabled
bool IsDisabled() const
Return whether the widget is disabled.
Definition widget_type.h:464
NWidgetCore::disp_flags
NWidgetDisplayFlags disp_flags
Flags that affect display and interaction with the widget.
Definition widget_type.h:402
NWidgetCore::GetScrollbarIndex
WidgetID GetScrollbarIndex() const
Get the WidgetID of this nested widget's scrollbar.
Definition widget.cpp:1288
NWidgetCore::GetToolTip
StringID GetToolTip() const
Get the tool tip of the nested widget.
Definition widget.cpp:1261
NWidgetCore::SetLowered
void SetLowered(bool lowered)
Lower or raise the widget.
Definition widget_type.h:443
NWidgetCore::IsLowered
bool IsLowered() const
Return whether the widget is lowered.
Definition widget_type.h:449
NWidgetLeaf::InvalidateDimensionCache
static void InvalidateDimensionCache()
Reset the cached dimensions.
Definition widget.cpp:2670
NWidgetLeaf::resizebox_dimension
static Dimension resizebox_dimension
Cached size of a resizebox widget.
Definition widget_type.h:2685
NWidgetLeaf::closebox_dimension
static Dimension closebox_dimension
Cached size of a closebox widget.
Definition widget_type.h:2686
NWidgetScrollbar
Nested widget to display and control a scrollbar in a window.
Definition widget_type.h:911
NWidgetStacked::shown_plane
int shown_plane
Plane being displayed (for NWID_SELECTION only).
Definition widget_type.h:537
NWidgetStacked::SetDisplayedPlane
bool SetDisplayedPlane(int plane)
Select which plane to show (for NWID_SELECTION only).
Definition widget.cpp:1450
PerformanceAccumulator::Reset
static void Reset(PerformanceElement elem)
Store the previous accumulator value and reset for a new cycle of accumulating measurements.
Definition framerate_gui.cpp:332
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition framerate_type.h:91
PickerWindowBase::Close
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:3602
Scrollbar
Scrollbar data structure.
Definition widget_type.h:711
Scrollbar::GetCapacity
size_type GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition widget_type.h:746
Scrollbar::UpdatePosition
bool UpdatePosition(int difference, ScrollbarStepping unit=SS_SMALL)
Updates the position of the first visible element by the given amount.
Definition widget_type.h:839
Scrollbar::SetPosition
bool SetPosition(size_type position)
Sets the position of the first visible element.
Definition widget_type.h:825
Scrollbar::GetCount
size_type GetCount() const
Gets the number of elements in the list.
Definition widget_type.h:737
TimerManager::Elapsed
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
VideoDriver::EditBoxLostFocus
virtual void EditBoxLostFocus()
An edit box lost the input focus.
Definition video_driver.hpp:153
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition video_driver.hpp:192
VideoDriver::EditBoxGainedFocus
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
Definition video_driver.hpp:158
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
company_func.h
Functions related to companies.
IsLocalCompany
bool IsLocalCompany()
Is the current company the local company?
Definition company_func.h:47
INVALID_OWNER
static constexpr Owner INVALID_OWNER
An invalid owner.
Definition company_type.h:30
console_func.h
Console functions used outside of the console code.
IConsoleClose
void IConsoleClose()
Close the in-game console.
Definition console_gui.cpp:442
IConsoleResize
void IConsoleResize(Window *w)
Change the size of the in-game console window after the screen size changed, or the window state chan...
Definition console_gui.cpp:408
console_gui.h
GUI related functions in the console.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
depot_func.h
Functions related to depots.
InitDepotWindowBlockSizes
void InitDepotWindowBlockSizes()
Set the size of the blocks in the window so we can be sure that they are big enough for the vehicle s...
Definition depot_gui.cpp:219
error.h
Functions related to errors.
UnshowCriticalError
void UnshowCriticalError()
Unshow the critical error.
Definition error_gui.cpp:278
ShowFirstError
void ShowFirstError()
Show the first error of the queue.
Definition error_gui.cpp:264
factory.hpp
Factory to 'query' all available blitters.
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition fileio_type.h:107
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
ProcessPendingPerformanceMeasurements
void ProcessPendingPerformanceMeasurements()
This drains the PFE_SOUND measurement data queue into _pf_data.
Definition framerate_gui.cpp:1067
framerate_type.h
Types for recording game performance data.
PFE_DRAWING
@ PFE_DRAWING
Speed of drawing world and GUI.
Definition framerate_type.h:56
PFE_DRAWWORLD
@ PFE_DRAWWORLD
Time spent drawing world viewports in GUI.
Definition framerate_type.h:57
game.hpp
Base functions for all Games.
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition gfx.cpp:39
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition gfx.cpp:41
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
_dirkeys
uint8_t _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition gfx.cpp:34
_left_button_clicked
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:42
_right_button_clicked
bool _right_button_clicked
Is right mouse button clicked?
Definition gfx.cpp:44
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:43
_gui_scale
int _gui_scale
GUI scale, 100 is 100%.
Definition gfx.cpp:63
gfx_func.h
Functions related to the gfx engine.
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
MILLISECONDS_PER_TICK
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition gfx_type.h:370
MakeWindowNWidgetTree
std::unique_ptr< NWidgetBase > MakeWindowNWidgetTree(std::span< const NWidgetPart > nwid_parts, NWidgetStacked **shade_select)
Make a nested widget tree for a window from a parts array.
Definition widget.cpp:3429
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:968
AddDirtyBlock
void AddDirtyBlock(int left, int top, int right, int bottom)
Extend the internal _invalid_rect rectangle to contain the rectangle defined by the given parameters.
Definition gfx.cpp:1516
DrawDirtyBlocks
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition gfx.cpp:1452
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1549
hotkeys.h
Hotkey related functions.
ini_type.h
Types related to reading/writing '*.ini' files.
TileVirtXY
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:416
IsInsideBS
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition math_func.hpp:252
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
RoundDivSU
constexpr int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
Definition math_func.hpp:332
Ceil
constexpr uint Ceil(uint a, uint b)
Computes ceil(a / b) * b for non-negative a and b.
Definition math_func.hpp:321
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
GuiShowTooltips
void GuiShowTooltips(Window *parent, EncodedString &&text, TooltipCloseCondition close_tooltip)
Shows a tooltip.
Definition misc_gui.cpp:693
_networking
bool _networking
are we in networking mode?
Definition network.cpp:67
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition network.cpp:70
network.h
Basic functions/variables used all over the place.
NetworkDrawChatMessage
void NetworkDrawChatMessage()
Draw the chat message-box.
Definition network_chat_gui.cpp:180
NetworkReInitChatBoxSize
void NetworkReInitChatBoxSize()
Initialize all font-dependent chat box sizes.
Definition network_chat_gui.cpp:101
NetworkUndrawChatMessage
void NetworkUndrawChatMessage()
Hide the chatbox.
Definition network_chat_gui.cpp:120
network_func.h
Network functions used by other parts of OpenTTD.
newgrf_debug.h
Functions/types related to NewGRF debugging.
_newgrf_debug_sprite_picker
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
Definition newgrf_debug_gui.cpp:53
news_func.h
Functions related to news.
InitNewsItemStructs
void InitNewsItemStructs()
Initialize the news-items data structures.
Definition news_gui.cpp:714
WID_OSK_CANCEL
@ WID_OSK_CANCEL
Cancel key.
Definition osk_widget.h:17
WID_OSK_OK
@ WID_OSK_OK
Ok key.
Definition osk_widget.h:18
progress.h
Functions related to modal progress.
HasModalProgress
bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition progress.h:17
querystring_gui.h
Base for the GUIs that have an edit box in them.
safeguards.h
A number of safeguards to prevent using unsafe methods.
IniLoadWindowSettings
void IniLoadWindowSettings(IniFile &ini, std::string_view grpname, WindowDesc *desc)
Load a WindowDesc from config.
Definition settings.cpp:873
IniSaveWindowSettings
void IniSaveWindowSettings(IniFile &ini, std::string_view grpname, WindowDesc *desc)
Save a WindowDesc to config.
Definition settings.cpp:884
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
settings_func.h
Functions related to setting/changing the settings.
settings_type.h
Types related to global configuration settings.
VSM_VIEWPORT_RMB_FIXED
@ VSM_VIEWPORT_RMB_FIXED
Viewport moves with mouse movement on holding right mouse button, cursor position is fixed.
Definition settings_type.h:148
VSM_MAP_RMB_FIXED
@ VSM_MAP_RMB_FIXED
Map moves with mouse movement on holding right mouse button, cursor position is fixed.
Definition settings_type.h:149
VSM_MAP_LMB
@ VSM_MAP_LMB
Map moves with mouse movement on holding left mouse button, cursor moves.
Definition settings_type.h:151
SWS_SCROLL_MAP
@ SWS_SCROLL_MAP
Scroll wheel scrolls the map.
Definition settings_type.h:158
ScrollMainWindowTo
bool ScrollMainWindowTo(int x, int y, int z, bool instant)
Scrolls the main window to given coordinates.
Definition smallmap_gui.cpp:2078
SndClickBeep
void SndClickBeep()
Play a beep sound for a click event if enabled in settings.
Definition sound.cpp:253
sound_func.h
Functions related to sound.
statusbar_gui.h
Functions, definitions and such used only by the GUI.
SBI_NEWS_DELETED
@ SBI_NEWS_DELETED
abort current news display (active news were deleted)
Definition statusbar_gui.h:19
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
GetEncodedString
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
strings_func.h
Functions related to OTTD's strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition strings_type.h:27
AutoRestoreBackup
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
Definition backup_type.hpp:150
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition settings_type.h:650
Coord2D< int >
Coord2D::y
T y
Y coordinate.
Definition geometry_type.hpp:35
Coord2D::x
T x
X coordinate.
Definition geometry_type.hpp:34
CursorVars::fix_at
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition gfx_type.h:128
CursorVars::pos
Point pos
logical mouse position
Definition gfx_type.h:125
CursorVars::in_window
bool in_window
mouse inside this window, determines drawing logic
Definition gfx_type.h:147
CursorVars::wheel
int wheel
mouse wheel movement
Definition gfx_type.h:127
CursorVars::delta
Point delta
relative mouse movement in this tick
Definition gfx_type.h:126
DrawPixelInfo
Data about how and where to blit pixels.
Definition gfx_type.h:157
GUISettings::window_snap_radius
uint8_t window_snap_radius
windows snap at each other if closer than this
Definition settings_type.h:189
GUISettings::hover_delay_ms
uint16_t hover_delay_ms
time required to activate a hover event, in milliseconds
Definition settings_type.h:175
GUISettings::auto_scrolling
uint8_t auto_scrolling
scroll when moving mouse to the edge (see ViewportAutoscrolling)
Definition settings_type.h:173
GUISettings::scroll_mode
uint8_t scroll_mode
viewport scroll mode
Definition settings_type.h:179
GUISettings::right_click_wnd_close
RightClickClose right_click_wnd_close
close window with right click
Definition settings_type.h:206
GUISettings::window_soft_limit
uint8_t window_soft_limit
soft limit of maximum number of non-stickied non-vital windows (0 = no limit)
Definition settings_type.h:190
GUISettings::toolbar_pos
uint8_t toolbar_pos
position of toolbars, 0=left, 1=center, 2=right
Definition settings_type.h:187
GUISettings::statusbar_pos
uint8_t statusbar_pos
position of statusbar, 0=left, 1=center, 2=right
Definition settings_type.h:188
GUISettings::scrollwheel_scrolling
uint8_t scrollwheel_scrolling
scrolling using the scroll wheel?
Definition settings_type.h:203
GUISettings::link_terraform_toolbar
bool link_terraform_toolbar
display terraform toolbar when displaying rail, road, water and airport toolbars
Definition settings_type.h:176
HotkeyList
List of hotkeys for a window.
Definition hotkeys.h:37
HotkeyList::CheckMatch
int CheckMatch(uint16_t keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition hotkeys.cpp:301
IniFile
Ini file that supports both loading and saving.
Definition ini_type.h:86
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition ini.cpp:42
IniLoadFile::LoadFromDisk
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:185
NewGrfDebugSpritePicker::mode
NewGrfDebugSpritePickerMode mode
Current state.
Definition newgrf_debug.h:27
NewGrfDebugSpritePicker::clicked_pixel
void * clicked_pixel
Clicked pixel (pointer to blitter buffer)
Definition newgrf_debug.h:28
NewGrfDebugSpritePicker::sprites
FlatSet< SpriteID > sprites
Sprites found.
Definition newgrf_debug.h:29
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::PoolItem<&_vehicle_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
QueryString
Data stored about a string that can be modified in the GUI.
Definition querystring_gui.h:20
QueryString::ok_button
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
Definition querystring_gui.h:27
QueryString::ACTION_DESELECT
static const int ACTION_DESELECT
Deselect editbox.
Definition querystring_gui.h:23
QueryString::cancel_button
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
Definition querystring_gui.h:28
QueryString::GetCharAtPosition
ptrdiff_t GetCharAtPosition(const Window *w, WidgetID wid, const Point &pt) const
Get the character that is rendered at a position.
Definition misc_gui.cpp:851
QueryString::ACTION_NOTHING
static const int ACTION_NOTHING
Nothing.
Definition querystring_gui.h:22
QueryString::ACTION_CLEAR
static const int ACTION_CLEAR
Clear editbox.
Definition querystring_gui.h:24
QueryString::GetCaretPosition
Point GetCaretPosition(const Window *w, WidgetID wid) const
Get the current caret position.
Definition misc_gui.cpp:793
QueryString::GetBoundingRect
Rect GetBoundingRect(const Window *w, WidgetID wid, size_t from, size_t to) const
Get the bounding rectangle for a range of the query string.
Definition misc_gui.cpp:821
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Rect::Height
int Height() const
Get height of Rect.
Definition geometry_type.hpp:117
ResizeInfo::step_height
uint step_height
Step-size of height resize changes.
Definition window_gui.h:213
ResizeInfo::step_width
uint step_width
Step-size of width resize changes.
Definition window_gui.h:212
Textbuf
Helper/buffer for input fields.
Definition textbuf_type.h:30
Textbuf::UpdateSize
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition textbuf.cpp:445
Textbuf::DeleteAll
void DeleteAll()
Delete every character in the textbuffer.
Definition textbuf.cpp:112
Textbuf::GetText
std::string_view GetText() const
Get the current text.
Definition textbuf.cpp:284
Textbuf::InsertString
bool InsertString(std::string_view str, bool marked, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Insert a string into the text buffer.
Definition textbuf.cpp:157
TileHighlightData::GetCallbackWnd
Window * GetCallbackWnd()
Get the window that started the current highlighting.
Definition viewport.cpp:2665
TileHighlightData::window_class
WindowClass window_class
The WindowClass of the window that is responsible for the selection mode.
Definition tilehighlight_type.h:68
TileHighlightData::Reset
void Reset()
Reset tile highlighting.
Definition viewport.cpp:2644
TileHighlightData::selend
Point selend
The location where the drag currently ends.
Definition tilehighlight_type.h:61
TileHighlightData::window_number
WindowNumber window_number
The WindowNumber of the window that is responsible for the selection mode.
Definition tilehighlight_type.h:69
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::z_pos
int32_t z_pos
z coordinate.
Definition vehicle_base.h:267
Vehicle::y_pos
int32_t y_pos
y coordinate.
Definition vehicle_base.h:266
Vehicle::x_pos
int32_t x_pos
x coordinate.
Definition vehicle_base.h:265
Viewport
Data structure for viewport, display of a part of the world.
Definition viewport_type.h:31
Viewport::top
int top
Screen coordinate top edge of the viewport.
Definition viewport_type.h:33
Viewport::width
int width
Screen width of the viewport.
Definition viewport_type.h:34
Viewport::zoom
ZoomLevel zoom
The zoom level of the viewport.
Definition viewport_type.h:42
Viewport::left
int left
Screen coordinate left edge of the viewport.
Definition viewport_type.h:32
Viewport::height
int height
Screen height of the viewport.
Definition viewport_type.h:35
WindowDesc
High level window description.
Definition window_gui.h:168
WindowDesc::GetDefaultWidth
int16_t GetDefaultWidth() const
Determine default width of window.
Definition window.cpp:137
WindowDesc::SaveToConfig
static void SaveToConfig()
Save all WindowDesc settings to _windows_file.
Definition window.cpp:176
WindowDesc::pref_width
int16_t pref_width
User-preferred width of the window. Zero if unset.
Definition window_gui.h:187
WindowDesc::default_pos
const WindowPosition default_pos
Preferred position of the window.
Definition window_gui.h:178
WindowDesc::pref_sticky
bool pref_sticky
Preferred stickyness.
Definition window_gui.h:186
WindowDesc::pref_height
int16_t pref_height
User-preferred height of the window. Zero if unset.
Definition window_gui.h:188
WindowDesc::GetDefaultHeight
int16_t GetDefaultHeight() const
Determine default height of window.
Definition window.cpp:147
WindowDesc::default_height_trad
const int16_t default_height_trad
Preferred initial height of the window (pixels at 1x zoom).
Definition window_gui.h:198
WindowDesc::default_width_trad
const int16_t default_width_trad
Preferred initial width of the window (pixels at 1x zoom).
Definition window_gui.h:197
WindowDesc::cls
const WindowClass cls
Class of the window,.
Definition window_gui.h:179
WindowDesc::ini_key
const std::string_view ini_key
Key to store window defaults in openttd.cfg. An empty string if nothing shall be stored.
Definition window_gui.h:181
WindowDesc::flags
const WindowDefaultFlags flags
Flags.
Definition window_gui.h:182
WindowDesc::LoadFromConfig
static void LoadFromConfig()
Load all WindowDesc settings from _windows_file.
Definition window.cpp:155
WindowDesc::parent_cls
const WindowClass parent_cls
Class of the parent window.
Definition window_gui.h:180
WindowDesc::hotkeys
const HotkeyList * hotkeys
Hotkeys for the window.
Definition window_gui.h:184
WindowDesc::WindowDesc
WindowDesc(WindowPosition default_pos, std::string_view ini_key, int16_t def_width_trad, int16_t def_height_trad, WindowClass window_class, WindowClass parent_class, WindowDefaultFlags flags, const std::span< const NWidgetPart > nwid_parts, HotkeyList *hotkeys=nullptr, const std::source_location location=std::source_location::current())
Window description constructor.
Definition window.cpp:108
WindowDesc::nwid_parts
const std::span< const NWidgetPart > nwid_parts
Span of nested widget parts describing the window.
Definition window_gui.h:183
WindowNumber
Number to differentiate different windows of the same class.
Definition window_type.h:760
Window::WindowIterator
Iterator to iterate all valid Windows.
Definition window_gui.h:875
Window
Data structure for an opened window.
Definition window_gui.h:274
Window::GetFocusedTextbuf
virtual const struct Textbuf * GetFocusedTextbuf() const
Get the current input text buffer.
Definition window.cpp:366
Window::SetWidgetHighlight
void SetWidgetHighlight(WidgetID widget_index, TextColour highlighted_colour)
Sets the highlighted status of a widget.
Definition window.cpp:241
Window::ReInit
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:980
Window::Close
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1104
Window::OnInvalidateData
virtual void OnInvalidateData(int data=0, bool gui_scope=true)
Some data on this window has become invalid.
Definition window_gui.h:793
Window::FinishInitNested
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1809
Window::querystrings
std::map< WidgetID, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition window_gui.h:321
Window::ApplyDefaults
virtual void ApplyDefaults()
Read default values from WindowDesc configuration an apply them to the window.
Definition window.cpp:193
Window::white_border_timer
uint8_t white_border_timer
Timer value of the WindowFlag::WhiteBorder for flags.
Definition window_gui.h:308
Window::shade_select
NWidgetStacked * shade_select
Selection widget (NWID_SELECTION) to use for shading the window. If nullptr, window cannot shade.
Definition window_gui.h:324
Window::InitializePositionSize
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
Definition window.cpp:1457
Window::unshaded_size
Dimension unshaded_size
Last known unshaded size (only valid while shaded).
Definition window_gui.h:325
Window::InvalidateData
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
Definition window.cpp:3237
Window::OnKeyPress
virtual EventState OnKeyPress(char32_t key, uint16_t keycode)
A key has been pressed.
Definition window_gui.h:651
Window::parent
Window * parent
Parent window.
Definition window_gui.h:329
Window::IterateFromBack
AllWindows< false > IterateFromBack
Iterate all windows in Z order from back to front.
Definition window_gui.h:934
Window::~Window
virtual ~Window()
Remove window and all its child windows from the window stack.
Definition window.cpp:1141
Window::RaiseWidget
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
Definition window_gui.h:470
Window::SetWidgetDirty
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition window.cpp:558
Window::timeout_timer
uint8_t timeout_timer
Timer value of the WindowFlag::Timeout for flags.
Definition window_gui.h:307
Window::viewport
std::unique_ptr< ViewportData > viewport
Pointer to viewport data, if present.
Definition window_gui.h:319
Window::GetWidgetString
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:506
Window::mouse_capture_widget
WidgetID mouse_capture_widget
ID of current mouse capture widget (e.g. dragged scrollbar). INVALID_WIDGET if no widget has mouse ca...
Definition window_gui.h:327
Window::OnGameTick
virtual void OnGameTick()
Called once per (game) tick.
Definition window_gui.h:746
Window::ShowNewGRFInspectWindow
virtual void ShowNewGRFInspectWindow() const
Show the NewGRF inspection window.
Definition window_gui.h:868
Window::OnRightClick
virtual bool OnRightClick(Point pt, WidgetID widget)
A click with the right mouse button has been made on the window.
Definition window_gui.h:678
Window::OnScrollbarScroll
virtual void OnScrollbarScroll(WidgetID widget)
Notify window that a scrollbar position has been updated.
Definition window_gui.h:720
Window::OnDropdownSelect
virtual void OnDropdownSelect(WidgetID widget, int index, int click_result)
A dropdown option associated to this window has been selected.
Definition window_gui.h:770
Window::ProcessScheduledInvalidations
void ProcessScheduledInvalidations()
Process all scheduled invalidations.
Definition window.cpp:3250
Window::CloseChildWindows
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
Definition window.cpp:1077
Window::resize
ResizeInfo resize
Resize information.
Definition window_gui.h:315
Window::UnfocusFocusedWidget
void UnfocusFocusedWidget()
Makes no widget on this window have focus.
Definition window.cpp:471
Window::OnMouseLoop
virtual void OnMouseLoop()
Called for every mouse loop run, which is at least once per (game) tick.
Definition window_gui.h:741
Window::SetShaded
void SetShaded(bool make_shaded)
Set the shaded state of the window to make_shaded.
Definition window.cpp:1023
Window::scale
int scale
Scale of this window – used to determine how to resize.
Definition window_gui.h:305
Window::ScheduleResize
void ScheduleResize()
Mark this window as resized and in need of OnResize() event.
Definition window.cpp:3215
Window::OnPaint
virtual void OnPaint()
The window must be repainted.
Definition window_gui.h:596
Window::OnDragDrop
virtual void OnDragDrop(Point pt, WidgetID widget)
A dragged 'object' has been released.
Definition window_gui.h:707
Window::CreateNestedTree
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1799
Window::window_desc
WindowDesc & window_desc
Window description.
Definition window_gui.h:300
Window::window_class
WindowClass window_class
Window class.
Definition window_gui.h:302
Window::OnRealtimeTick
virtual void OnRealtimeTick(uint delta_ms)
Called periodically.
Definition window_gui.h:751
Window::OnMouseWheel
virtual void OnMouseWheel(int wheel, WidgetID widget)
The mouse wheel has been turned.
Definition window_gui.h:735
Window::IterateFromFront
AllWindows< true > IterateFromFront
Iterate all windows in Z order from front to back.
Definition window_gui.h:935
Window::CloseChildWindowById
void CloseChildWindowById(WindowClass wc, WindowNumber number) const
Close all children a window might have in a head-recursive manner.
Definition window.cpp:1092
Window::SetWhiteBorder
void SetWhiteBorder()
Set the timeout flag of the window and initiate the timer.
Definition window_gui.h:365
Window::SetFocusedWidget
bool SetFocusedWidget(WidgetID widget_index)
Set focus within this window to the given widget.
Definition window.cpp:487
Window::OnFocusLost
virtual void OnFocusLost(bool closing)
The window has lost focus.
Definition window.cpp:523
Window::IsWidgetLowered
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
Definition window_gui.h:492
Window::closed_windows
static std::vector< Window * > closed_windows
List of closed windows to delete.
Definition window_gui.h:276
Window::RaiseButtons
void RaiseButtons(bool autoraise=false)
Raise the buttons of the window.
Definition window.cpp:532
Window::OnInitialPosition
virtual Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number)
Compute the initial position of the window.
Definition window.cpp:1787
Window::owner
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition window_gui.h:317
Window::GetCaretPosition
virtual Point GetCaretPosition() const
Get the current caret position if an edit box has the focus.
Definition window.cpp:379
Window::FindWindowPlacementAndResize
virtual void FindWindowPlacementAndResize(int def_width, int def_height, bool allow_resize)
Resize window towards the default size.
Definition window.cpp:1476
Window::OnDropdownClose
virtual void OnDropdownClose(Point pt, WidgetID widget, int index, int click_result, bool instant_close)
A dropdown window associated to this window has been closed.
Definition window.cpp:286
Window::InsertTextString
virtual void InsertTextString(WidgetID wid, std::string_view str, bool marked, std::optional< size_t > caret, std::optional< size_t > insert_location, std::optional< size_t > replacement_end)
Insert a text string at the cursor position into the edit box widget.
Definition window.cpp:2722
Window::left
int left
x position of left edge of the window
Definition window_gui.h:310
Window::IsShaded
bool IsShaded() const
Is window shaded currently?
Definition window_gui.h:560
Window::FindChildWindow
Window * FindChildWindow(WindowClass wc=WC_INVALID) const
Find the Window whose parent pointer points to this window.
Definition window.cpp:1049
Window::SetTimeout
void SetTimeout()
Set the timeout flag of the window and initiate the timer.
Definition window_gui.h:356
Window::nested_focus
const NWidgetCore * nested_focus
Currently focused nested widget, or nullptr if no nested widget has focus.
Definition window_gui.h:320
Window::OnEditboxChanged
virtual void OnEditboxChanged(WidgetID widget)
The text in an editbox has been edited.
Definition window_gui.h:778
Window::UpdateQueryStringSize
void UpdateQueryStringSize()
Update size of all QueryStrings of this window.
Definition window.cpp:355
Window::OnScroll
virtual void OnScroll(Point delta)
Handle the request for (viewport) scrolling.
Definition window_gui.h:713
Window::top
int top
y position of top edge of the window
Definition window_gui.h:311
Window::OnClick
virtual void OnClick(Point pt, WidgetID widget, int click_count)
A click with the left mouse button has been made on the window.
Definition window_gui.h:669
Window::GetQueryString
const QueryString * GetQueryString(WidgetID widnum) const
Return the querystring associated to a editbox.
Definition window.cpp:335
Window::widget_lookup
WidgetLookup widget_lookup
Indexed access to the nested widget tree. Do not access directly, use Window::GetWidget() instead.
Definition window_gui.h:323
Window::GetTextCharacterAtPosition
virtual ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const
Get the character that is rendered at a position by the focused edit box.
Definition window.cpp:410
Window::scheduled_invalidation_data
std::vector< int > scheduled_invalidation_data
Data of scheduled OnInvalidateData() calls.
Definition window_gui.h:283
Window::InitializeData
void InitializeData(WindowNumber window_number)
Initializes the data (except the position and initial size) of a new Window.
Definition window.cpp:1419
Window::OnMouseOver
virtual void OnMouseOver(Point pt, WidgetID widget)
The mouse is currently moving over the window or has just moved outside of the window.
Definition window_gui.h:728
Window::Window
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1833
Window::GetRowFromWidget
int GetRowFromWidget(int clickpos, WidgetID widget, int padding, int line_height=-1) const
Compute the row of a widget that a user clicked in.
Definition window.cpp:212
Window::GetWidget
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:983
Window::OnTooltip
virtual bool OnTooltip(Point pt, WidgetID widget, TooltipCloseCondition close_cond)
Event to display a custom tooltip.
Definition window_gui.h:693
Window::LowerWidget
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
Definition window_gui.h:461
Window::OnCTRLStateChange
virtual EventState OnCTRLStateChange()
The state of the control key has changed.
Definition window_gui.h:660
Window::ProcessScheduledResize
void ProcessScheduledResize()
Process scheduled OnResize() event.
Definition window.cpp:3223
Window::HandleEditBoxKey
EventState HandleEditBoxKey(WidgetID wid, char32_t key, uint16_t keycode)
Process keypress for editbox widget.
Definition window.cpp:2564
Window::OnMouseDrag
virtual void OnMouseDrag(Point pt, WidgetID widget)
An 'object' is being dragged at the provided position, highlight the target if possible.
Definition window_gui.h:700
Window::HandleButtonClick
void HandleButtonClick(WidgetID widget)
Do all things to make a button look clicked and mark it to be unclicked in a few ticks.
Definition window.cpp:597
Window::OnResize
virtual void OnResize()
Called after the window got resized.
Definition window_gui.h:763
Window::FindChildWindowById
Window * FindChildWindowById(WindowClass wc, WindowNumber number) const
Find the Window whose parent pointer points to this window.
Definition window.cpp:1064
Window::OnFocus
virtual void OnFocus()
The window has gained focus.
Definition window.cpp:515
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1823
Window::OnTimeout
virtual void OnTimeout()
Called when this window's timeout has been reached.
Definition window_gui.h:756
Window::flags
WindowFlags flags
Window flags.
Definition window_gui.h:301
Window::GetScrollbar
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:315
Window::ProcessHighlightedInvalidations
void ProcessHighlightedInvalidations()
Process all invalidation of highlighted widgets.
Definition window.cpp:3262
Window::OnHotkey
virtual EventState OnHotkey(int hotkey)
A hotkey has been pressed.
Definition window.cpp:572
Window::DeleteClosedWindows
static void DeleteClosedWindows()
Delete all closed windows.
Definition window.cpp:68
Window::nested_root
std::unique_ptr< NWidgetBase > nested_root
Root of the nested tree.
Definition window_gui.h:322
Window::scheduled_resize
bool scheduled_resize
Set if window has been resized.
Definition window_gui.h:284
Window::GetTextBoundingRect
virtual Rect GetTextBoundingRect(size_t from, size_t to) const
Get the bounding rectangle for a text range if an edit box has the focus.
Definition window.cpp:395
Window::IsWidgetHighlighted
bool IsWidgetHighlighted(WidgetID widget_index) const
Gets the highlighted status of a widget.
Definition window.cpp:271
Window::DisableAllWidgetHighlight
void DisableAllWidgetHighlight()
Disable the highlighted status of all widgets.
Definition window.cpp:223
Window::OnPlacePresize
virtual void OnPlacePresize(Point pt, TileIndex tile)
The user moves over the map when a tile highlight mode has been set when the special mouse mode has b...
Definition window_gui.h:852
Window::Iterate
AllWindows< false > Iterate
Iterate all windows in whatever order is easiest.
Definition window_gui.h:933
Window::height
int height
Height of the window (number of pixels down in y direction)
Definition window_gui.h:313
Window::OnHover
virtual void OnHover(Point pt, WidgetID widget)
The mouse is hovering over a widget in the window, perform an action for it.
Definition window_gui.h:685
Window::width
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
Window::OnInit
virtual void OnInit()
Notification that the nested widget tree gets initialized.
Definition window_gui.h:579
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
_window_descs
std::vector< WindowDesc * > * _window_descs
List of WindowDescs.
Definition window.cpp:102
HKPR_NOT_HANDLED
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
Definition textbuf_type.h:26
HKPR_CANCEL
@ HKPR_CANCEL
Escape key pressed.
Definition textbuf_type.h:25
HKPR_EDITING
@ HKPR_EDITING
Textbuf content changed.
Definition textbuf_type.h:22
HKPR_CONFIRM
@ HKPR_CONFIRM
Return or enter key pressed.
Definition textbuf_type.h:24
HKPR_CURSOR
@ HKPR_CURSOR
Non-text change, e.g. cursor position.
Definition textbuf_type.h:23
tilehighlight_func.h
Functions related to tile highlights.
ResetObjectToPlace
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition viewport.cpp:3573
UpdateTileSelection
void UpdateTileSelection()
Updates tile highlighting for all cases.
Definition viewport.cpp:2679
timer.h
Definition of Interval and OneShot timers.
timer_window.h
Definition of the Window system.
TIMER_BLINK_INTERVAL
static constexpr std::chrono::milliseconds TIMER_BLINK_INTERVAL
Interval used by blinking interface elements.
Definition timer_window.h:36
_toolbar_width
uint _toolbar_width
Width of the toolbar, shared by statusbar.
Definition toolbar_gui.cpp:80
toolbar_gui.h
Stuff related to the (main) toolbar.
vehicle_base.h
Base class for all vehicles.
video_driver.hpp
Base of all video drivers.
IsPtInWindowViewport
Viewport * IsPtInWindowViewport(const Window *w, int x, int y)
Is a xy position inside the viewport of the window?
Definition viewport.cpp:408
UpdateViewportPosition
void UpdateViewportPosition(Window *w, uint32_t delta_ms)
Update the viewport position being displayed.
Definition viewport.cpp:1988
viewport_func.h
Functions related to (drawing on) viewports.
SetupWidgetDimensions
void SetupWidgetDimensions()
Set up pre-scaled versions of Widget Dimensions.
Definition widget.cpp:96
GetWidgetFromPos
WidgetID GetWidgetFromPos(const Window *w, int x, int y)
Returns the index for the widget located at the given position relative to the window.
Definition widget.cpp:292
ScrollbarClickHandler
void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
Special handling for the scrollbar widget type.
Definition widget.cpp:268
ScaleGUITrad
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
WidgetType
WidgetType
Window widget types, nested widget types, and nested widget part types.
Definition widget_type.h:35
NWID_BUTTON_DROPDOWN
@ NWID_BUTTON_DROPDOWN
Button with a drop-down.
Definition widget_type.h:74
WWT_EDITBOX
@ WWT_EDITBOX
a textbox for typing
Definition widget_type.h:62
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:57
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:55
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
NWID_VSCROLLBAR
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
WWT_EMPTY
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:37
WWT_LAST
@ WWT_LAST
Last Item. use WIDGETS_END to fill up padding!!
Definition widget_type.h:63
NWID_HSCROLLBAR
@ NWID_HSCROLLBAR
Horizontal scrollbar.
Definition widget_type.h:75
WWT_RESIZEBOX
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:59
WWT_DEFSIZEBOX
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:56
WWT_DEBUGBOX
@ WWT_DEBUGBOX
NewGRF debug box (at top-right of a window, between WWT_CAPTION and WWT_SHADEBOX)
Definition widget_type.h:54
DropdownClosed
@ DropdownClosed
Dropdown menu of the dropdown widget has closed.
Definition widget_type.h:353
ScrollbarDown
@ ScrollbarDown
Down-button is lowered bit.
Definition widget_type.h:349
ScrollbarUp
@ ScrollbarUp
Up-button is lowered bit.
Definition widget_type.h:348
DropdownActive
@ DropdownActive
Dropdown menu of the button dropdown widget is active.
Definition widget_type.h:345
SZSP_HORIZONTAL
@ SZSP_HORIZONTAL
Display plane with zero size vertically, and filling and resizing horizontally.
Definition widget_type.h:508
ST_RESIZE
@ ST_RESIZE
Resize the nested widget tree.
Definition widget_type.h:115
ST_SMALLEST
@ ST_SMALLEST
Initialize nested widget tree to smallest size. Also updates current_x and current_y.
Definition widget_type.h:114
PositionStatusbar
int PositionStatusbar(Window *w)
(Re)position statusbar window at the screen.
Definition window.cpp:3479
PreventHiding
static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
Do not allow hiding of the rectangle with base coordinates nx and ny behind window v.
Definition window.cpp:2022
_dragging_window
static bool _dragging_window
A window is being dragged or resized.
Definition window.cpp:2156
white_border_interval
static const IntervalTimer< TimerWindow > white_border_interval(std::chrono::milliseconds(30), [](auto) { if(_network_dedicated) return;for(Window *w :Window::Iterate()) { if(w->flags.Test(WindowFlag::WhiteBorder) &&--w->white_border_timer==0) { w->flags.Reset(WindowFlag::WhiteBorder);w->SetDirty();} } })
Blink all windows marked with a white border.
CloseConstructionWindows
void CloseConstructionWindows()
Close all windows that are used for construction of vehicle etc.
Definition window.cpp:3383
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1195
LocalGetWindowPlacement
static Point LocalGetWindowPlacement(const WindowDesc &desc, int16_t sm_width, int16_t sm_height, int window_number)
Compute the position of the top-left corner of a new window that is opened.
Definition window.cpp:1728
GetMainWindow
Window * GetMainWindow()
Get the main window, i.e.
Definition window.cpp:1182
_drag_delta
static Point _drag_delta
delta between mouse cursor and upper left corner of dragged window
Definition window.cpp:55
MayBeShown
static bool MayBeShown(const Window *w)
Returns whether a window may be shown or not.
Definition window.cpp:857
CloseCompanyWindows
void CloseCompanyWindows(CompanyID id)
Close all windows of a company.
Definition window.cpp:1223
HandleCtrlChanged
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition window.cpp:2709
DrawOverlappedWindow
static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
Generate repaint events for the visible part of window w within the rectangle.
Definition window.cpp:885
_scrolling_viewport
bool _scrolling_viewport
A viewport is being scrolled with the mouse.
Definition window.cpp:93
InputLoop
void InputLoop()
Regular call from the global game loop.
Definition window.cpp:3062
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition window.cpp:3127
PositionMainToolbar
int PositionMainToolbar(Window *w)
(Re)position main toolbar window at the screen.
Definition window.cpp:3468
CloseNonVitalWindows
void CloseNonVitalWindows()
Try to close a non-vital window.
Definition window.cpp:3339
DrawOverlappedWindowForAll
void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
From a rectangle that needs redrawing, find the windows that intersect with the rectangle.
Definition window.cpp:947
ResizeWindow
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen, bool schedule_resize)
Resize the window.
Definition window.cpp:2099
IsGoodAutoPlace2
static bool IsGoodAutoPlace2(int left, int top, int width, int height, int toolbar_y, Point &pos)
Decide whether a given rectangle is a good place to open a mostly visible new window.
Definition window.cpp:1578
PositionNetworkChatWindow
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
Definition window.cpp:3501
StartWindowSizing
static void StartWindowSizing(Window *w, bool to_left)
Start resizing a window.
Definition window.cpp:2351
GetAutoPlacePosition
static Point GetAutoPlacePosition(int width, int height)
Find a good place for opening a new window of a given width and height.
Definition window.cpp:1617
PreventHideDirection
PreventHideDirection
Direction for moving the window.
Definition window.cpp:2007
PHD_DOWN
@ PHD_DOWN
Below v is a safe position.
Definition window.cpp:2009
PHD_UP
@ PHD_UP
Above v is a safe position.
Definition window.cpp:2008
HandleAutoscroll
static void HandleAutoscroll()
If needed and switched on, perform auto scrolling (automatically moving window contents when mouse is...
Definition window.cpp:2750
_window_highlight_colour
bool _window_highlight_colour
If false, highlight is white, otherwise the by the widget defined colour.
Definition window.cpp:78
HandleToolbarHotkey
void HandleToolbarHotkey(int hotkey)
Handle Toolbar hotkey events - can come from a source like the MacBook Touch Bar.
Definition window.cpp:2636
PositionWindow
static int PositionWindow(Window *w, WindowClass clss, int setting)
(Re)position a window at the screen.
Definition window.cpp:3445
SetFocusedWindow
void SetFocusedWindow(Window *w)
Set the window that has the focus.
Definition window.cpp:423
IsGoodAutoPlace1
static bool IsGoodAutoPlace1(int left, int top, int width, int height, int toolbar_y, Point &pos)
Decide whether a given rectangle is a good place to open a completely visible new window.
Definition window.cpp:1542
MAX_OFFSET_DOUBLE_CLICK
static constexpr int MAX_OFFSET_DOUBLE_CLICK
How much the mouse is allowed to move to call it a double click.
Definition window.cpp:2795
FindWindowByClass
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
Definition window.cpp:1168
ChangeVehicleViewports
void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
Switches viewports following vehicles, which get autoreplaced.
Definition window.cpp:3513
TIME_BETWEEN_DOUBLE_CLICK
const std::chrono::milliseconds TIME_BETWEEN_DOUBLE_CLICK(500)
Time between 2 left clicks before it becoming a double click.
HandleViewportScroll
static EventState HandleViewportScroll()
Handle viewport scrolling with the mouse.
Definition window.cpp:2440
DispatchMouseWheelEvent
static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
Dispatch the mousewheel-action to the window.
Definition window.cpp:820
DispatchLeftClickEvent
static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
Dispatch left mouse-button (possibly double) click in window.
Definition window.cpp:618
FindWindowFromPt
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
Definition window.cpp:1845
DeleteAllMessages
void DeleteAllMessages()
Delete all messages and close their corresponding window (if any).
Definition window.cpp:3371
_last_scroll_window
static Window * _last_scroll_window
Window of the last scroll event.
Definition window.cpp:57
GetMainViewTop
int GetMainViewTop()
Return the top of the main view available for general use.
Definition window.cpp:2139
BringWindowToFront
static void BringWindowToFront(Window *w, bool dirty=true)
On clicking on a window, make it the frontmost window of all windows with an equal or lower z-priorit...
Definition window.cpp:1399
ReInitAllWindows
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3413
EnsureVisibleCaption
static void EnsureVisibleCaption(Window *w, int nx, int ny)
Make sure at least a part of the caption bar is still visible by moving the window if necessary.
Definition window.cpp:2062
CloseWindowByClass
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition window.cpp:1207
HandleMouseEvents
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition window.cpp:2966
GetToolbarAlignedWindowPosition
Point GetToolbarAlignedWindowPosition(int window_width)
Computer the position of the top-left corner of a window to be opened right under the toolbar.
Definition window.cpp:1685
HandleWindowDragging
static EventState HandleWindowDragging()
Handle dragging/resizing of a window.
Definition window.cpp:2162
PositionNewsMessage
int PositionNewsMessage(Window *w)
(Re)position news message window at the screen.
Definition window.cpp:3490
GetMainViewBottom
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Definition window.cpp:2150
DispatchHoverEvent
static void DispatchHoverEvent(Window *w, int x, int y)
Dispatch hover of the mouse over a window.
Definition window.cpp:792
ChangeWindowOwner
void ChangeWindowOwner(Owner old_owner, Owner new_owner)
Change the owner of all the windows one company can take over from another company in the case of a c...
Definition window.cpp:1243
HandleKeypress
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
Definition window.cpp:2653
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition window.cpp:3205
VpHandlePlaceSizingDrag
EventState VpHandlePlaceSizingDrag()
Handle the mouse while dragging for placement/resizing.
Definition viewport.cpp:3456
MAX_OFFSET_HOVER
static constexpr int MAX_OFFSET_HOVER
Maximum mouse movement before stopping a hover event.
Definition window.cpp:2796
FocusedWindowIsConsole
bool FocusedWindowIsConsole()
Check if a console is focused.
Definition window.cpp:463
ResetWindowSystem
void ResetWindowSystem()
Reset the windowing system, by means of shutting it down followed by re-initialization.
Definition window.cpp:1895
GetWindowZPriority
static uint GetWindowZPriority(WindowClass wc)
Get the z-priority for a given window.
Definition window.cpp:1315
EditBoxInGlobalFocus
bool EditBoxInGlobalFocus()
Check if an edit box is in global focus.
Definition window.cpp:449
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3297
HideVitalWindows
void HideVitalWindows()
Close all always on-top windows to get an empty screen.
Definition window.cpp:3396
highlight_interval
static const IntervalTimer< TimerWindow > highlight_interval(TIMER_BLINK_INTERVAL, [](auto) { _window_highlight_colour=!_window_highlight_colour;})
Blink the window highlight colour constantly.
DescSorter
static bool DescSorter(WindowDesc *const &a, WindowDesc *const &b)
Sort WindowDesc by ini_key.
Definition window.cpp:168
BringWindowToFrontById
Window * BringWindowToFrontById(WindowClass cls, WindowNumber number)
Find a window and make it the relative top-window on the screen.
Definition window.cpp:1278
AlignInitialConstructionToolbar
Point AlignInitialConstructionToolbar(int window_width)
Compute the position of the construction toolbars.
Definition window.cpp:1701
RelocateAllWindows
void RelocateAllWindows(int neww, int newh)
Relocate all windows to fit the new size of the game application screen.
Definition window.cpp:3529
_windows_file
std::string _windows_file
Config file to store WindowDesc.
Definition window.cpp:105
HandleScrollbarScrolling
static void HandleScrollbarScrolling(Window *w)
Handle scrollbar scrolling with the mouse.
Definition window.cpp:2367
_mouse_hovering
bool _mouse_hovering
The mouse is hovering over the same point.
Definition window.cpp:94
StartWindowDrag
static void StartWindowDrag(Window *w)
Start window dragging.
Definition window.cpp:2334
CallWindowGameTickEvent
void CallWindowGameTickEvent()
Dispatch OnGameTick event over all windows.
Definition window.cpp:3326
HandleActiveWidget
static EventState HandleActiveWidget()
Handle active widget (mouse dragging on widget) with the mouse.
Definition window.cpp:2407
_window_descs
std::vector< WindowDesc * > * _window_descs
List of all WindowDescs.
Definition window.cpp:102
DispatchRightClickEvent
static void DispatchRightClickEvent(Window *w, int x, int y)
Dispatch right mouse-button click in window.
Definition window.cpp:763
ViewportAutoscrolling
ViewportAutoscrolling
Values for _settings_client.gui.auto_scrolling.
Definition window.cpp:48
VA_MAIN_VIEWPORT_FULLSCREEN
@ VA_MAIN_VIEWPORT_FULLSCREEN
Scroll main viewport at edge when using fullscreen.
Definition window.cpp:50
VA_MAIN_VIEWPORT
@ VA_MAIN_VIEWPORT
Scroll main viewport at edge.
Definition window.cpp:51
VA_EVERY_VIEWPORT
@ VA_EVERY_VIEWPORT
Scroll all viewports at their edges.
Definition window.cpp:52
VA_DISABLED
@ VA_DISABLED
Do not autoscroll when mouse is at edge of viewport.
Definition window.cpp:49
scrollamt
static const int8_t scrollamt[16][2]
Describes all the different arrow key combinations the game allows when it is in scrolling mode.
Definition window.cpp:2820
MaybeBringWindowToFront
static bool MaybeBringWindowToFront(Window *w)
Check if a window can be made relative top-most window, and if so do it.
Definition window.cpp:2504
_z_windows
WindowList _z_windows
List of windows opened at the screen sorted from the front to back.
Definition window.cpp:60
CallWindowRealtimeTickEvent
void CallWindowRealtimeTickEvent(uint delta_ms)
Dispatch OnRealtimeTick event over all windows.
Definition window.cpp:3088
_special_mouse_mode
SpecialMouseMode _special_mouse_mode
Mode of the mouse.
Definition window.cpp:96
HandleMouseDragDrop
static EventState HandleMouseDragDrop()
Handle dragging and dropping in mouse dragging mode (WSM_DRAGDROP).
Definition window.cpp:1960
CloseAllNonVitalWindows
void CloseAllNonVitalWindows()
It is possible that a stickied window gets to a position where the 'close' button is outside the gami...
Definition window.cpp:3358
HandleMouseOver
static void HandleMouseOver()
Report position of the mouse to the underlying window.
Definition window.cpp:1984
window_interval
static const IntervalTimer< TimerWindow > window_interval(std::chrono::milliseconds(30), [](auto) { extern int _caret_timer;_caret_timer+=3;CursorTick();HandleKeyScrolling();HandleAutoscroll();DecreaseWindowCounters();})
Update various of window-related information on a regular interval.
_mouseover_last_w
static Window * _mouseover_last_w
Window of the last OnMouseOver event.
Definition window.cpp:56
CheckSoftLimit
static void CheckSoftLimit()
Check the soft limit of deletable (non vital, non sticky) windows.
Definition window.cpp:3037
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1153
UnInitWindowSystem
void UnInitWindowSystem()
Close down the windowing system.
Definition window.cpp:1881
InitWindowSystem
void InitWindowSystem()
(re)initialize the windowing system
Definition window.cpp:1859
SetWindowWidgetDirty
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, WidgetID widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting)
Definition window.cpp:3192
HandleTextInput
void HandleTextInput(std::string_view str, bool marked, std::optional< size_t > caret, std::optional< size_t > insert_location, std::optional< size_t > replacement_end)
Handle text input.
Definition window.cpp:2739
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition window.cpp:3179
InvalidateWindowClassesData
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3314
window_func.h
Window functions not directly related to making/drawing windows.
WindowDefaultFlag::Construction
@ Construction
This window is used for construction; close it whenever changing company.
WindowDefaultFlag::NoClose
@ NoClose
This window can't be interactively closed.
WindowDefaultFlag::NoFocus
@ NoFocus
This window won't get focus/make any other window lose focus when click.
WindowDefaultFlag::Modal
@ Modal
The window is a modal child of some other window, meaning the parent is 'inactive'.
SetFocusedWindow
void SetFocusedWindow(Window *w)
Set the window that has the focus.
Definition window.cpp:423
WindowFlag::SizingLeft
@ SizingLeft
Window is being resized towards the left.
WindowFlag::DisableVpScroll
@ DisableVpScroll
Window does not do autoscroll,.
WindowFlag::Highlighted
@ Highlighted
Window has a widget that has a highlight.
WindowFlag::Centred
@ Centred
Window is centered and shall stay centered after ReInit.
WindowFlag::Dragging
@ Dragging
Window is being dragged.
WindowFlag::SizingRight
@ SizingRight
Window is being resized towards the right.
WindowFlag::WhiteBorder
@ WhiteBorder
Window white border counter bit mask.
WindowFlag::Timeout
@ Timeout
Window timeout counter.
WindowFlag::Sticky
@ Sticky
Window is made sticky by user.
TIMEOUT_DURATION
static const int TIMEOUT_DURATION
The initial timeout value for WindowFlag::Timeout.
Definition window_gui.h:241
GetWidgetFromPos
WidgetID GetWidgetFromPos(const Window *w, int x, int y)
Returns the index for the widget located at the given position relative to the window.
Definition widget.cpp:292
SpecialMouseMode
SpecialMouseMode
Mouse modes.
Definition window_gui.h:1040
WSM_DRAGDROP
@ WSM_DRAGDROP
Drag&drop an object.
Definition window_gui.h:1042
WSM_PRESIZE
@ WSM_PRESIZE
Presizing mode (docks, tunnels).
Definition window_gui.h:1044
WindowPosition
WindowPosition
How do we the window to be placed?
Definition window_gui.h:142
WDP_CENTER
@ WDP_CENTER
Center the window.
Definition window_gui.h:145
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:144
WDP_ALIGN_TOOLBAR
@ WDP_ALIGN_TOOLBAR
Align toward the toolbar.
Definition window_gui.h:146
WDP_MANUAL
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition window_gui.h:143
_z_windows
WindowList _z_windows
List of windows opened at the screen sorted from the front to back.
Definition window.cpp:60
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:20
EventState
EventState
State of handling an event.
Definition window_type.h:780
ES_HANDLED
@ ES_HANDLED
The passed event is handled.
Definition window_type.h:781
ES_NOT_HANDLED
@ ES_NOT_HANDLED
The passed event is not handled.
Definition window_type.h:782
INVALID_WIDGET
static constexpr WidgetID INVALID_WIDGET
An invalid widget index.
Definition window_type.h:23
WindowClass
WindowClass
Window classes.
Definition window_type.h:49
WC_INVALID
@ WC_INVALID
Invalid window.
Definition window_type.h:735
WC_OSK
@ WC_OSK
On Screen Keyboard; Window numbers:
Definition window_type.h:167
WC_CONSOLE
@ WC_CONSOLE
Console; Window numbers:
Definition window_type.h:660
WC_NEWS_WINDOW
@ WC_NEWS_WINDOW
News window; Window numbers:
Definition window_type.h:253
WC_HIGHSCORE
@ WC_HIGHSCORE
Highscore; Window numbers:
Definition window_type.h:672
WC_BUY_COMPANY
@ WC_BUY_COMPANY
Buyout company (merger); Window numbers:
Definition window_type.h:605
WC_SPRITE_ALIGNER
@ WC_SPRITE_ALIGNER
Sprite aligner (debug); Window numbers:
Definition window_type.h:697
WC_COMPANY_INFRASTRUCTURE
@ WC_COMPANY_INFRASTRUCTURE
Company infrastructure overview; Window numbers:
Definition window_type.h:598
WC_ENDSCREEN
@ WC_ENDSCREEN
Endscreen; Window numbers:
Definition window_type.h:678
WC_COMPANY_COLOUR
@ WC_COMPANY_COLOUR
Company colour selection; Window numbers:
Definition window_type.h:235
WC_STATION_LIST
@ WC_STATION_LIST
Station list; Window numbers:
Definition window_type.h:307
WC_ROADVEH_LIST
@ WC_ROADVEH_LIST
Road vehicle list; Window numbers:
Definition window_type.h:319
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition window_type.h:69
WC_VEHICLE_ORDERS
@ WC_VEHICLE_ORDERS
Vehicle orders; Window numbers:
Definition window_type.h:217
WC_SEND_NETWORK_MSG
@ WC_SEND_NETWORK_MSG
Chatbox; Window numbers:
Definition window_type.h:514
WC_ERRMSG
@ WC_ERRMSG
Error message; Window numbers:
Definition window_type.h:115
WC_BUILD_TOOLBAR
@ WC_BUILD_TOOLBAR
Build toolbar; Window numbers:
Definition window_type.h:78
WC_NETWORK_ASK_RELAY
@ WC_NETWORK_ASK_RELAY
Network ask relay window; Window numbers:
Definition window_type.h:502
WC_SCRIPT_SETTINGS
@ WC_SCRIPT_SETTINGS
Script settings; Window numbers:
Definition window_type.h:180
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:50
WC_SCEN_LAND_GEN
@ WC_SCEN_LAND_GEN
Landscape generation (in Scenario Editor); Window numbers:
Definition window_type.h:460
WC_SCRIPT_LIST
@ WC_SCRIPT_LIST
Scripts list; Window numbers:
Definition window_type.h:289
WC_SAVE_PRESET
@ WC_SAVE_PRESET
Save preset; Window numbers:
Definition window_type.h:709
WC_SHIPS_LIST
@ WC_SHIPS_LIST
Ships list; Window numbers:
Definition window_type.h:325
WC_MESSAGE_HISTORY
@ WC_MESSAGE_HISTORY
News history list; Window numbers:
Definition window_type.h:277
WC_CONFIRM_POPUP_QUERY
@ WC_CONFIRM_POPUP_QUERY
Popup with confirm question; Window numbers:
Definition window_type.h:135
WC_GENERATE_LANDSCAPE
@ WC_GENERATE_LANDSCAPE
Generate landscape (newgame); Window numbers:
Definition window_type.h:467
WC_CUSTOM_CURRENCY
@ WC_CUSTOM_CURRENCY
Custom currency; Window numbers:
Definition window_type.h:641
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition window_type.h:56
WC_TRAINS_LIST
@ WC_TRAINS_LIST
Trains list; Window numbers:
Definition window_type.h:313
WC_GAME_OPTIONS
@ WC_GAME_OPTIONS
Game options window; Window numbers:
Definition window_type.h:635
WC_NETWORK_WINDOW
@ WC_NETWORK_WINDOW
Network window; Window numbers:
Definition window_type.h:483
WC_FINANCES
@ WC_FINANCES
Finances of a company; Window numbers:
Definition window_type.h:532
WC_TEXTFILE
@ WC_TEXTFILE
textfile; Window numbers:
Definition window_type.h:192
WC_DROPDOWN_MENU
@ WC_DROPDOWN_MENU
Drop down menu; Window numbers:
Definition window_type.h:161
WC_QUERY_STRING
@ WC_QUERY_STRING
Query string window; Window numbers:
Definition window_type.h:128
WC_SMALLMAP
@ WC_SMALLMAP
Small map; Window numbers:
Definition window_type.h:109
WC_BOOTSTRAP
@ WC_BOOTSTRAP
Bootstrap; Window numbers:
Definition window_type.h:666
WC_SAVELOAD
@ WC_SAVELOAD
Saveload window; Window numbers:
Definition window_type.h:149
WC_GRF_PARAMETERS
@ WC_GRF_PARAMETERS
NewGRF parameters; Window numbers:
Definition window_type.h:186
WC_MODAL_PROGRESS
@ WC_MODAL_PROGRESS
Progress report of landscape generation; Window numbers:
Definition window_type.h:474
WC_MAIN_TOOLBAR
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition window_type.h:63
WC_TOOLTIPS
@ WC_TOOLTIPS
Tooltip window; Window numbers:
Definition window_type.h:121
WC_COMPANY
@ WC_COMPANY
Company view; Window numbers:
Definition window_type.h:374
WC_NETWORK_STATUS_WINDOW
@ WC_NETWORK_STATUS_WINDOW
Network status window; Window numbers:
Definition window_type.h:496
WC_AIRCRAFT_LIST
@ WC_AIRCRAFT_LIST
Aircraft list; Window numbers:
Definition window_type.h:331
zoom_func.h
Functions related to zooming.
ScaleByZoom
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:22
ZoomLevel::Min
@ Min
Minimum zoom level.