OpenTTD Source  1.10.0-RC1
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include <stdarg.h>
12 #include "company_func.h"
13 #include "gfx_func.h"
14 #include "console_func.h"
15 #include "console_gui.h"
16 #include "viewport_func.h"
17 #include "progress.h"
18 #include "blitter/factory.hpp"
19 #include "zoom_func.h"
20 #include "vehicle_base.h"
21 #include "window_func.h"
22 #include "tilehighlight_func.h"
23 #include "network/network.h"
24 #include "querystring_gui.h"
25 #include "widgets/dropdown_func.h"
26 #include "strings_func.h"
27 #include "settings_type.h"
28 #include "settings_func.h"
29 #include "ini_type.h"
30 #include "newgrf_debug.h"
31 #include "hotkeys.h"
32 #include "toolbar_gui.h"
33 #include "statusbar_gui.h"
34 #include "error.h"
35 #include "game/game.hpp"
36 #include "video/video_driver.hpp"
37 #include "framerate_type.h"
38 #include "network/network_func.h"
39 #include "guitimer_func.h"
40 #include "news_func.h"
41 
42 #include "safeguards.h"
43 
50 };
51 
53 static Window *_mouseover_last_w = nullptr;
54 static Window *_last_scroll_window = nullptr;
55 
60 
63 
64 /*
65  * Window that currently has focus. - The main purpose is to generate
66  * #FocusLost events, not to give next window in z-order focus when a
67  * window is closed.
68  */
69 Window *_focused_window;
70 
71 Point _cursorpos_drag_start;
72 
73 int _scrollbar_start_pos;
74 int _scrollbar_size;
75 byte _scroller_click_timeout = 0;
76 
79 
81 
86 static std::vector<WindowDesc*> *_window_descs = nullptr;
87 
90 
92 WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
93  WindowClass window_class, WindowClass parent_class, uint32 flags,
94  const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys) :
95  default_pos(def_pos),
96  cls(window_class),
97  parent_cls(parent_class),
98  ini_key(ini_key),
99  flags(flags),
100  nwid_parts(nwid_parts),
101  nwid_length(nwid_length),
102  hotkeys(hotkeys),
103  pref_sticky(false),
104  pref_width(0),
105  pref_height(0),
106  default_width_trad(def_width_trad),
107  default_height_trad(def_height_trad)
108 {
109  if (_window_descs == nullptr) _window_descs = new std::vector<WindowDesc*>();
110  _window_descs->push_back(this);
111 }
112 
113 WindowDesc::~WindowDesc()
114 {
115  _window_descs->erase(std::find(_window_descs->begin(), _window_descs->end(), this));
116 }
117 
124 {
125  return this->pref_width != 0 ? this->pref_width : ScaleGUITrad(this->default_width_trad);
126 }
127 
134 {
135  return this->pref_height != 0 ? this->pref_height : ScaleGUITrad(this->default_height_trad);
136 }
137 
142 {
143  IniFile *ini = new IniFile();
145  for (WindowDesc *wd : *_window_descs) {
146  if (wd->ini_key == nullptr) continue;
147  IniLoadWindowSettings(ini, wd->ini_key, wd);
148  }
149  delete ini;
150 }
151 
155 static bool DescSorter(WindowDesc* const &a, WindowDesc* const &b)
156 {
157  if (a->ini_key != nullptr && b->ini_key != nullptr) return strcmp(a->ini_key, b->ini_key) < 0;
158  return a->ini_key != nullptr;
159 }
160 
165 {
166  /* Sort the stuff to get a nice ini file on first write */
167  std::sort(_window_descs->begin(), _window_descs->end(), DescSorter);
168 
169  IniFile *ini = new IniFile();
170  ini->LoadFromDisk(_windows_file, NO_DIRECTORY);
171  for (WindowDesc *wd : *_window_descs) {
172  if (wd->ini_key == nullptr) continue;
173  IniSaveWindowSettings(ini, wd->ini_key, wd);
174  }
175  ini->SaveToDisk(_windows_file);
176  delete ini;
177 }
178 
183 {
184  if (this->nested_root != nullptr && this->nested_root->GetWidgetOfType(WWT_STICKYBOX) != nullptr) {
185  if (this->window_desc->pref_sticky) this->flags |= WF_STICKY;
186  } else {
187  /* There is no stickybox; clear the preference in case someone tried to be funny */
188  this->window_desc->pref_sticky = false;
189  }
190 }
191 
201 int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const
202 {
203  const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
204  if (line_height < 0) line_height = wid->resize_y;
205  if (clickpos < (int)wid->pos_y + padding) return INT_MAX;
206  return (clickpos - (int)wid->pos_y - padding) / line_height;
207 }
208 
213 {
214  for (uint i = 0; i < this->nested_array_size; i++) {
215  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
216  if (nwid == nullptr) continue;
217 
218  if (nwid->IsHighlighted()) {
219  nwid->SetHighlighted(TC_INVALID);
220  this->SetWidgetDirty(i);
221  }
222  }
223 
224  CLRBITS(this->flags, WF_HIGHLIGHTED);
225 }
226 
232 void Window::SetWidgetHighlight(byte widget_index, TextColour highlighted_colour)
233 {
234  assert(widget_index < this->nested_array_size);
235 
236  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
237  if (nwid == nullptr) return;
238 
239  nwid->SetHighlighted(highlighted_colour);
240  this->SetWidgetDirty(widget_index);
241 
242  if (highlighted_colour != TC_INVALID) {
243  /* If we set a highlight, the window has a highlight */
244  this->flags |= WF_HIGHLIGHTED;
245  } else {
246  /* If we disable a highlight, check all widgets if anyone still has a highlight */
247  bool valid = false;
248  for (uint i = 0; i < this->nested_array_size; i++) {
249  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
250  if (nwid == nullptr) continue;
251  if (!nwid->IsHighlighted()) continue;
252 
253  valid = true;
254  }
255  /* If nobody has a highlight, disable the flag on the window */
256  if (!valid) CLRBITS(this->flags, WF_HIGHLIGHTED);
257  }
258 }
259 
265 bool Window::IsWidgetHighlighted(byte widget_index) const
266 {
267  assert(widget_index < this->nested_array_size);
268 
269  const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
270  if (nwid == nullptr) return false;
271 
272  return nwid->IsHighlighted();
273 }
274 
282 void Window::OnDropdownClose(Point pt, int widget, int index, bool instant_close)
283 {
284  if (widget < 0) return;
285 
286  if (instant_close) {
287  /* Send event for selected option if we're still
288  * on the parent button of the dropdown (behaviour of the dropdowns in the main toolbar). */
289  if (GetWidgetFromPos(this, pt.x, pt.y) == widget) {
290  this->OnDropdownSelect(widget, index);
291  }
292  }
293 
294  /* Raise the dropdown button */
295  NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget);
296  if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
297  nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE;
298  } else {
299  this->RaiseWidget(widget);
300  }
301  this->SetWidgetDirty(widget);
302 }
303 
309 const Scrollbar *Window::GetScrollbar(uint widnum) const
310 {
311  return this->GetWidget<NWidgetScrollbar>(widnum);
312 }
313 
320 {
321  return this->GetWidget<NWidgetScrollbar>(widnum);
322 }
323 
329 const QueryString *Window::GetQueryString(uint widnum) const
330 {
331  auto query = this->querystrings.Find(widnum);
332  return query != this->querystrings.end() ? query->second : nullptr;
333 }
334 
341 {
342  SmallMap<int, QueryString*>::Pair *query = this->querystrings.Find(widnum);
343  return query != this->querystrings.End() ? query->second : nullptr;
344 }
345 
350 /* virtual */ const char *Window::GetFocusedText() const
351 {
352  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
353  return this->GetQueryString(this->nested_focus->index)->GetText();
354  }
355 
356  return nullptr;
357 }
358 
363 /* virtual */ const char *Window::GetCaret() const
364 {
365  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
366  return this->GetQueryString(this->nested_focus->index)->GetCaret();
367  }
368 
369  return nullptr;
370 }
371 
377 /* virtual */ const char *Window::GetMarkedText(size_t *length) const
378 {
379  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
380  return this->GetQueryString(this->nested_focus->index)->GetMarkedText(length);
381  }
382 
383  return nullptr;
384 }
385 
390 /* virtual */ Point Window::GetCaretPosition() const
391 {
392  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX && !this->querystrings.empty()) {
393  return this->GetQueryString(this->nested_focus->index)->GetCaretPosition(this, this->nested_focus->index);
394  }
395 
396  Point pt = {0, 0};
397  return pt;
398 }
399 
406 /* virtual */ Rect Window::GetTextBoundingRect(const char *from, const char *to) const
407 {
408  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
409  return this->GetQueryString(this->nested_focus->index)->GetBoundingRect(this, this->nested_focus->index, from, to);
410  }
411 
412  Rect r = {0, 0, 0, 0};
413  return r;
414 }
415 
421 /* virtual */ const char *Window::GetTextCharacterAtPosition(const Point &pt) const
422 {
423  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
424  return this->GetQueryString(this->nested_focus->index)->GetCharAtPosition(this, this->nested_focus->index, pt);
425  }
426 
427  return nullptr;
428 }
429 
435 {
436  if (_focused_window == w) return;
437 
438  /* Invalidate focused widget */
439  if (_focused_window != nullptr) {
440  if (_focused_window->nested_focus != nullptr) _focused_window->nested_focus->SetDirty(_focused_window);
441  }
442 
443  /* Remember which window was previously focused */
444  Window *old_focused = _focused_window;
445  _focused_window = w;
446 
447  /* So we can inform it that it lost focus */
448  if (old_focused != nullptr) old_focused->OnFocusLost();
449  if (_focused_window != nullptr) _focused_window->OnFocus();
450 }
451 
458 {
459  if (_focused_window == nullptr) return false;
460 
461  /* The console does not have an edit box so a special case is needed. */
462  if (_focused_window->window_class == WC_CONSOLE) return true;
463 
464  return _focused_window->nested_focus != nullptr && _focused_window->nested_focus->type == WWT_EDITBOX;
465 }
466 
472 {
473  return _focused_window && _focused_window->window_class == WC_CONSOLE;
474 }
475 
480 {
481  if (this->nested_focus != nullptr) {
482  if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
483 
484  /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
485  this->nested_focus->SetDirty(this);
486  this->nested_focus = nullptr;
487  }
488 }
489 
495 bool Window::SetFocusedWidget(int widget_index)
496 {
497  /* Do nothing if widget_index is already focused, or if it wasn't a valid widget. */
498  if ((uint)widget_index >= this->nested_array_size) return false;
499 
500  assert(this->nested_array[widget_index] != nullptr); // Setting focus to a non-existing widget is a bad idea.
501  if (this->nested_focus != nullptr) {
502  if (this->GetWidget<NWidgetCore>(widget_index) == this->nested_focus) return false;
503 
504  /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
505  this->nested_focus->SetDirty(this);
506  if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
507  }
508  this->nested_focus = this->GetWidget<NWidgetCore>(widget_index);
509  if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxGainedFocus();
510  return true;
511 }
512 
517 {
518  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxGainedFocus();
519 }
520 
525 {
526  if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
527 }
528 
536 void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...)
537 {
538  va_list wdg_list;
539 
540  va_start(wdg_list, widgets);
541 
542  while (widgets != WIDGET_LIST_END) {
543  SetWidgetDisabledState(widgets, disab_stat);
544  widgets = va_arg(wdg_list, int);
545  }
546 
547  va_end(wdg_list);
548 }
549 
555 void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...)
556 {
557  va_list wdg_list;
558 
559  va_start(wdg_list, widgets);
560 
561  while (widgets != WIDGET_LIST_END) {
562  SetWidgetLoweredState(widgets, lowered_stat);
563  widgets = va_arg(wdg_list, int);
564  }
565 
566  va_end(wdg_list);
567 }
568 
573 void Window::RaiseButtons(bool autoraise)
574 {
575  for (uint i = 0; i < this->nested_array_size; i++) {
576  if (this->nested_array[i] == nullptr) continue;
577  WidgetType type = this->nested_array[i]->type;
578  if (((type & ~WWB_PUSHBUTTON) < WWT_LAST || type == NWID_PUSHBUTTON_DROPDOWN) &&
579  (!autoraise || (type & WWB_PUSHBUTTON) || type == WWT_EDITBOX) && this->IsWidgetLowered(i)) {
580  this->RaiseWidget(i);
581  this->SetWidgetDirty(i);
582  }
583  }
584 
585  /* Special widgets without widget index */
586  NWidgetCore *wid = this->nested_root != nullptr ? (NWidgetCore*)this->nested_root->GetWidgetOfType(WWT_DEFSIZEBOX) : nullptr;
587  if (wid != nullptr) {
588  wid->SetLowered(false);
589  wid->SetDirty(this);
590  }
591 }
592 
597 void Window::SetWidgetDirty(byte widget_index) const
598 {
599  /* Sometimes this function is called before the window is even fully initialized */
600  if (this->nested_array == nullptr) return;
601 
602  this->nested_array[widget_index]->SetDirty(this);
603 }
604 
611 {
612  if (hotkey < 0) return ES_NOT_HANDLED;
613 
614  NWidgetCore *nw = this->GetWidget<NWidgetCore>(hotkey);
615  if (nw == nullptr || nw->IsDisabled()) return ES_NOT_HANDLED;
616 
617  if (nw->type == WWT_EDITBOX) {
618  if (this->IsShaded()) return ES_NOT_HANDLED;
619 
620  /* Focus editbox */
621  this->SetFocusedWidget(hotkey);
622  SetFocusedWindow(this);
623  } else {
624  /* Click button */
625  this->OnClick(Point(), hotkey, 1);
626  }
627  return ES_HANDLED;
628 }
629 
635 void Window::HandleButtonClick(byte widget)
636 {
637  this->LowerWidget(widget);
638  this->SetTimeout();
639  this->SetWidgetDirty(widget);
640 }
641 
642 static void StartWindowDrag(Window *w);
643 static void StartWindowSizing(Window *w, bool to_left);
644 
652 static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
653 {
654  NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
655  WidgetType widget_type = (nw != nullptr) ? nw->type : WWT_EMPTY;
656 
657  bool focused_widget_changed = false;
658  /* If clicked on a window that previously did dot have focus */
659  if (_focused_window != w && // We already have focus, right?
660  (w->window_desc->flags & WDF_NO_FOCUS) == 0 && // Don't lose focus to toolbars
661  widget_type != WWT_CLOSEBOX) { // Don't change focused window if 'X' (close button) was clicked
662  focused_widget_changed = true;
663  SetFocusedWindow(w);
664  }
665 
666  if (nw == nullptr) return; // exit if clicked outside of widgets
667 
668  /* don't allow any interaction if the button has been disabled */
669  if (nw->IsDisabled()) return;
670 
671  int widget_index = nw->index;
672 
673  /* Clicked on a widget that is not disabled.
674  * So unless the clicked widget is the caption bar, change focus to this widget.
675  * Exception: In the OSK we always want the editbox to stay focused. */
676  if (widget_type != WWT_CAPTION && w->window_class != WC_OSK) {
677  /* focused_widget_changed is 'now' only true if the window this widget
678  * is in gained focus. In that case it must remain true, also if the
679  * local widget focus did not change. As such it's the logical-or of
680  * both changed states.
681  *
682  * If this is not preserved, then the OSK window would be opened when
683  * a user has the edit box focused and then click on another window and
684  * then back again on the edit box (to type some text).
685  */
686  focused_widget_changed |= w->SetFocusedWidget(widget_index);
687  }
688 
689  /* Close any child drop down menus. If the button pressed was the drop down
690  * list's own button, then we should not process the click any further. */
691  if (HideDropDownMenu(w) == widget_index && widget_index >= 0) return;
692 
693  if ((widget_type & ~WWB_PUSHBUTTON) < WWT_LAST && (widget_type & WWB_PUSHBUTTON)) w->HandleButtonClick(widget_index);
694 
695  Point pt = { x, y };
696 
697  switch (widget_type) {
698  case NWID_VSCROLLBAR:
699  case NWID_HSCROLLBAR:
700  ScrollbarClickHandler(w, nw, x, y);
701  break;
702 
703  case WWT_EDITBOX: {
704  QueryString *query = w->GetQueryString(widget_index);
705  if (query != nullptr) query->ClickEditBox(w, pt, widget_index, click_count, focused_widget_changed);
706  break;
707  }
708 
709  case WWT_CLOSEBOX: // 'X'
710  delete w;
711  return;
712 
713  case WWT_CAPTION: // 'Title bar'
714  StartWindowDrag(w);
715  return;
716 
717  case WWT_RESIZEBOX:
718  /* When the resize widget is on the left size of the window
719  * we assume that that button is used to resize to the left. */
720  StartWindowSizing(w, (int)nw->pos_x < (w->width / 2));
721  nw->SetDirty(w);
722  return;
723 
724  case WWT_DEFSIZEBOX: {
725  if (_ctrl_pressed) {
726  w->window_desc->pref_width = w->width;
727  w->window_desc->pref_height = w->height;
728  } else {
729  int16 def_width = max<int16>(min(w->window_desc->GetDefaultWidth(), _screen.width), w->nested_root->smallest_x);
730  int16 def_height = max<int16>(min(w->window_desc->GetDefaultHeight(), _screen.height - 50), w->nested_root->smallest_y);
731 
732  int dx = (w->resize.step_width == 0) ? 0 : def_width - w->width;
733  int dy = (w->resize.step_height == 0) ? 0 : def_height - w->height;
734  /* dx and dy has to go by step.. calculate it.
735  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
736  if (w->resize.step_width > 1) dx -= dx % (int)w->resize.step_width;
737  if (w->resize.step_height > 1) dy -= dy % (int)w->resize.step_height;
738  ResizeWindow(w, dx, dy, false);
739  }
740 
741  nw->SetLowered(true);
742  nw->SetDirty(w);
743  w->SetTimeout();
744  break;
745  }
746 
747  case WWT_DEBUGBOX:
749  break;
750 
751  case WWT_SHADEBOX:
752  nw->SetDirty(w);
753  w->SetShaded(!w->IsShaded());
754  return;
755 
756  case WWT_STICKYBOX:
757  w->flags ^= WF_STICKY;
758  nw->SetDirty(w);
759  if (_ctrl_pressed) w->window_desc->pref_sticky = (w->flags & WF_STICKY) != 0;
760  return;
761 
762  default:
763  break;
764  }
765 
766  /* Widget has no index, so the window is not interested in it. */
767  if (widget_index < 0) return;
768 
769  /* Check if the widget is highlighted; if so, disable highlight and dispatch an event to the GameScript */
770  if (w->IsWidgetHighlighted(widget_index)) {
771  w->SetWidgetHighlight(widget_index, TC_INVALID);
772  Game::NewEvent(new ScriptEventWindowWidgetClick((ScriptWindow::WindowClass)w->window_class, w->window_number, widget_index));
773  }
774 
775  w->OnClick(pt, widget_index, click_count);
776 }
777 
784 static void DispatchRightClickEvent(Window *w, int x, int y)
785 {
786  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
787  if (wid == nullptr) return;
788 
789  Point pt = { x, y };
790 
791  /* No widget to handle, or the window is not interested in it. */
792  if (wid->index >= 0) {
793  if (w->OnRightClick(pt, wid->index)) return;
794  }
795 
796  /* Right-click close is enabled and there is a closebox */
798  delete w;
799  } else if (_settings_client.gui.hover_delay_ms == 0 && !w->OnTooltip(pt, wid->index, TCC_RIGHT_CLICK) && wid->tool_tip != 0) {
800  GuiShowTooltips(w, wid->tool_tip, 0, nullptr, TCC_RIGHT_CLICK);
801  }
802 }
803 
810 static void DispatchHoverEvent(Window *w, int x, int y)
811 {
812  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
813 
814  /* No widget to handle */
815  if (wid == nullptr) return;
816 
817  Point pt = { x, y };
818 
819  /* Show the tooltip if there is any */
820  if (!w->OnTooltip(pt, wid->index, TCC_HOVER) && wid->tool_tip != 0) {
821  GuiShowTooltips(w, wid->tool_tip);
822  return;
823  }
824 
825  /* Widget has no index, so the window is not interested in it. */
826  if (wid->index < 0) return;
827 
828  w->OnHover(pt, wid->index);
829 }
830 
838 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
839 {
840  if (nwid == nullptr) return;
841 
842  /* Using wheel on caption/shade-box shades or unshades the window. */
843  if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
844  w->SetShaded(wheel < 0);
845  return;
846  }
847 
848  /* Wheeling a vertical scrollbar. */
849  if (nwid->type == NWID_VSCROLLBAR) {
850  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
851  if (sb->GetCount() > sb->GetCapacity()) {
852  sb->UpdatePosition(wheel);
853  w->SetDirty();
854  }
855  return;
856  }
857 
858  /* Scroll the widget attached to the scrollbar. */
859  Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : nullptr);
860  if (sb != nullptr && sb->GetCount() > sb->GetCapacity()) {
861  sb->UpdatePosition(wheel);
862  w->SetDirty();
863  }
864 }
865 
871 static bool MayBeShown(const Window *w)
872 {
873  /* If we're not modal, everything is okay. */
874  if (!HasModalProgress()) return true;
875 
876  switch (w->window_class) {
877  case WC_MAIN_WINDOW:
878  case WC_MODAL_PROGRESS:
880  return true;
881 
882  default:
883  return false;
884  }
885 }
886 
899 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
900 {
901  const Window *v;
903  if (MayBeShown(v) &&
904  right > v->left &&
905  bottom > v->top &&
906  left < v->left + v->width &&
907  top < v->top + v->height) {
908  /* v and rectangle intersect with each other */
909  int x;
910 
911  if (left < (x = v->left)) {
912  DrawOverlappedWindow(w, left, top, x, bottom);
913  DrawOverlappedWindow(w, x, top, right, bottom);
914  return;
915  }
916 
917  if (right > (x = v->left + v->width)) {
918  DrawOverlappedWindow(w, left, top, x, bottom);
919  DrawOverlappedWindow(w, x, top, right, bottom);
920  return;
921  }
922 
923  if (top < (x = v->top)) {
924  DrawOverlappedWindow(w, left, top, right, x);
925  DrawOverlappedWindow(w, left, x, right, bottom);
926  return;
927  }
928 
929  if (bottom > (x = v->top + v->height)) {
930  DrawOverlappedWindow(w, left, top, right, x);
931  DrawOverlappedWindow(w, left, x, right, bottom);
932  return;
933  }
934 
935  return;
936  }
937  }
938 
939  /* Setup blitter, and dispatch a repaint event to window *wz */
940  DrawPixelInfo *dp = _cur_dpi;
941  dp->width = right - left;
942  dp->height = bottom - top;
943  dp->left = left - w->left;
944  dp->top = top - w->top;
945  dp->pitch = _screen.pitch;
946  dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
947  dp->zoom = ZOOM_LVL_NORMAL;
948  w->OnPaint();
949 }
950 
959 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
960 {
961  Window *w;
962 
963  DrawPixelInfo *old_dpi = _cur_dpi;
964  DrawPixelInfo bk;
965  _cur_dpi = &bk;
966 
967  FOR_ALL_WINDOWS_FROM_BACK(w) {
968  if (MayBeShown(w) &&
969  right > w->left &&
970  bottom > w->top &&
971  left < w->left + w->width &&
972  top < w->top + w->height) {
973  /* Window w intersects with the rectangle => needs repaint */
974  DrawOverlappedWindow(w, max(left, w->left), max(top, w->top), min(right, w->left + w->width), min(bottom, w->top + w->height));
975  }
976  }
977  _cur_dpi = old_dpi;
978 }
979 
984 void Window::SetDirty() const
985 {
986  SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
987 }
988 
995 void Window::ReInit(int rx, int ry)
996 {
997  this->SetDirty(); // Mark whole current window as dirty.
998 
999  /* Save current size. */
1000  int window_width = this->width;
1001  int window_height = this->height;
1002 
1003  this->OnInit();
1004  /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
1005  this->nested_root->SetupSmallestSize(this, false);
1006  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1007  this->width = this->nested_root->smallest_x;
1008  this->height = this->nested_root->smallest_y;
1009  this->resize.step_width = this->nested_root->resize_x;
1010  this->resize.step_height = this->nested_root->resize_y;
1011 
1012  /* Resize as close to the original size + requested resize as possible. */
1013  window_width = max(window_width + rx, this->width);
1014  window_height = max(window_height + ry, this->height);
1015  int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
1016  int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
1017  /* dx and dy has to go by step.. calculate it.
1018  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
1019  if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
1020  if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
1021 
1022  ResizeWindow(this, dx, dy);
1023  /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
1024 }
1025 
1031 void Window::SetShaded(bool make_shaded)
1032 {
1033  if (this->shade_select == nullptr) return;
1034 
1035  int desired = make_shaded ? SZSP_HORIZONTAL : 0;
1036  if (this->shade_select->shown_plane != desired) {
1037  if (make_shaded) {
1038  if (this->nested_focus != nullptr) this->UnfocusFocusedWidget();
1039  this->unshaded_size.width = this->width;
1040  this->unshaded_size.height = this->height;
1041  this->shade_select->SetDisplayedPlane(desired);
1042  this->ReInit(0, -this->height);
1043  } else {
1044  this->shade_select->SetDisplayedPlane(desired);
1045  int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
1046  int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
1047  this->ReInit(dx, dy);
1048  }
1049  }
1050 }
1051 
1059 {
1060  Window *v;
1061  FOR_ALL_WINDOWS_FROM_BACK(v) {
1062  if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
1063  }
1064 
1065  return nullptr;
1066 }
1067 
1073 {
1074  Window *child = FindChildWindow(this, wc);
1075  while (child != nullptr) {
1076  delete child;
1077  child = FindChildWindow(this, wc);
1078  }
1079 }
1080 
1085 {
1086  if (_thd.window_class == this->window_class &&
1087  _thd.window_number == this->window_number) {
1089  }
1090 
1091  /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
1092  if (_mouseover_last_w == this) _mouseover_last_w = nullptr;
1093 
1094  /* We can't scroll the window when it's closed. */
1095  if (_last_scroll_window == this) _last_scroll_window = nullptr;
1096 
1097  /* Make sure we don't try to access non-existing query strings. */
1098  this->querystrings.clear();
1099 
1100  /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
1101  if (_focused_window == this) {
1102  this->OnFocusLost();
1103  _focused_window = nullptr;
1104  }
1105 
1106  this->DeleteChildWindows();
1107 
1108  if (this->viewport != nullptr) DeleteWindowViewport(this);
1109 
1110  this->SetDirty();
1111 
1112  free(this->nested_array); // Contents is released through deletion of #nested_root.
1113  delete this->nested_root;
1114 
1115  /*
1116  * Make fairly sure that this is written, and not "optimized" away.
1117  * The delete operator is overwritten to not delete it; the deletion
1118  * happens at a later moment in time after the window has been
1119  * removed from the list of windows to prevent issues with items
1120  * being removed during the iteration as not one but more windows
1121  * may be removed by a single call to ~Window by means of the
1122  * DeleteChildWindows function.
1123  */
1124  const_cast<volatile WindowClass &>(this->window_class) = WC_INVALID;
1125 }
1126 
1134 {
1135  Window *w;
1136  FOR_ALL_WINDOWS_FROM_BACK(w) {
1137  if (w->window_class == cls && w->window_number == number) return w;
1138  }
1139 
1140  return nullptr;
1141 }
1142 
1150 {
1151  Window *w;
1152  FOR_ALL_WINDOWS_FROM_BACK(w) {
1153  if (w->window_class == cls) return w;
1154  }
1155 
1156  return nullptr;
1157 }
1158 
1166 {
1167  Window *w = FindWindowById(cls, number);
1168  if (force || w == nullptr ||
1169  (w->flags & WF_STICKY) == 0) {
1170  delete w;
1171  }
1172 }
1173 
1179 {
1180  Window *w;
1181 
1182 restart_search:
1183  /* When we find the window to delete, we need to restart the search
1184  * as deleting this window could cascade in deleting (many) others
1185  * anywhere in the z-array */
1186  FOR_ALL_WINDOWS_FROM_BACK(w) {
1187  if (w->window_class == cls) {
1188  delete w;
1189  goto restart_search;
1190  }
1191  }
1192 }
1193 
1201 {
1202  Window *w;
1203 
1204 restart_search:
1205  /* When we find the window to delete, we need to restart the search
1206  * as deleting this window could cascade in deleting (many) others
1207  * anywhere in the z-array */
1208  FOR_ALL_WINDOWS_FROM_BACK(w) {
1209  if (w->owner == id) {
1210  delete w;
1211  goto restart_search;
1212  }
1213  }
1214 
1215  /* Also delete the company specific windows that don't have a company-colour. */
1217 }
1218 
1226 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1227 {
1228  Window *w;
1229  FOR_ALL_WINDOWS_FROM_BACK(w) {
1230  if (w->owner != old_owner) continue;
1231 
1232  switch (w->window_class) {
1233  case WC_COMPANY_COLOUR:
1234  case WC_FINANCES:
1235  case WC_STATION_LIST:
1236  case WC_TRAINS_LIST:
1237  case WC_ROADVEH_LIST:
1238  case WC_SHIPS_LIST:
1239  case WC_AIRCRAFT_LIST:
1240  case WC_BUY_COMPANY:
1241  case WC_COMPANY:
1243  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().
1244  continue;
1245 
1246  default:
1247  w->owner = new_owner;
1248  break;
1249  }
1250  }
1251 }
1252 
1253 static void BringWindowToFront(Window *w);
1254 
1263 {
1264  Window *w = FindWindowById(cls, number);
1265 
1266  if (w != nullptr) {
1267  if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1268 
1269  w->SetWhiteBorder();
1270  BringWindowToFront(w);
1271  w->SetDirty();
1272  }
1273 
1274  return w;
1275 }
1276 
1277 static inline bool IsVitalWindow(const Window *w)
1278 {
1279  switch (w->window_class) {
1280  case WC_MAIN_TOOLBAR:
1281  case WC_STATUS_BAR:
1282  case WC_NEWS_WINDOW:
1283  case WC_SEND_NETWORK_MSG:
1284  return true;
1285 
1286  default:
1287  return false;
1288  }
1289 }
1290 
1300 {
1301  assert(wc != WC_INVALID);
1302 
1303  uint z_priority = 0;
1304 
1305  switch (wc) {
1306  case WC_ENDSCREEN:
1307  ++z_priority;
1308  FALLTHROUGH;
1309 
1310  case WC_HIGHSCORE:
1311  ++z_priority;
1312  FALLTHROUGH;
1313 
1314  case WC_TOOLTIPS:
1315  ++z_priority;
1316  FALLTHROUGH;
1317 
1318  case WC_DROPDOWN_MENU:
1319  ++z_priority;
1320  FALLTHROUGH;
1321 
1322  case WC_MAIN_TOOLBAR:
1323  case WC_STATUS_BAR:
1324  ++z_priority;
1325  FALLTHROUGH;
1326 
1327  case WC_OSK:
1328  ++z_priority;
1329  FALLTHROUGH;
1330 
1331  case WC_QUERY_STRING:
1332  case WC_SEND_NETWORK_MSG:
1333  ++z_priority;
1334  FALLTHROUGH;
1335 
1336  case WC_ERRMSG:
1338  case WC_MODAL_PROGRESS:
1340  case WC_SAVE_PRESET:
1341  ++z_priority;
1342  FALLTHROUGH;
1343 
1344  case WC_GENERATE_LANDSCAPE:
1345  case WC_SAVELOAD:
1346  case WC_GAME_OPTIONS:
1347  case WC_CUSTOM_CURRENCY:
1348  case WC_NETWORK_WINDOW:
1349  case WC_GRF_PARAMETERS:
1350  case WC_AI_LIST:
1351  case WC_AI_SETTINGS:
1352  case WC_TEXTFILE:
1353  ++z_priority;
1354  FALLTHROUGH;
1355 
1356  case WC_CONSOLE:
1357  ++z_priority;
1358  FALLTHROUGH;
1359 
1360  case WC_NEWS_WINDOW:
1361  ++z_priority;
1362  FALLTHROUGH;
1363 
1364  default:
1365  ++z_priority;
1366  FALLTHROUGH;
1367 
1368  case WC_MAIN_WINDOW:
1369  return z_priority;
1370  }
1371 }
1372 
1378 {
1379  assert(w->z_front == nullptr && w->z_back == nullptr);
1380 
1381  if (_z_front_window == nullptr) {
1382  /* It's the only window. */
1383  _z_front_window = _z_back_window = w;
1384  w->z_front = w->z_back = nullptr;
1385  } else {
1386  /* Search down the z-ordering for its location. */
1387  Window *v = _z_front_window;
1388  uint last_z_priority = UINT_MAX;
1389  while (v != nullptr && (v->window_class == WC_INVALID || GetWindowZPriority(v->window_class) > GetWindowZPriority(w->window_class))) {
1390  if (v->window_class != WC_INVALID) {
1391  /* Sanity check z-ordering, while we're at it. */
1392  assert(last_z_priority >= GetWindowZPriority(v->window_class));
1393  last_z_priority = GetWindowZPriority(v->window_class);
1394  }
1395 
1396  v = v->z_back;
1397  }
1398 
1399  if (v == nullptr) {
1400  /* It's the new back window. */
1401  w->z_front = _z_back_window;
1402  w->z_back = nullptr;
1403  _z_back_window->z_back = w;
1404  _z_back_window = w;
1405  } else if (v == _z_front_window) {
1406  /* It's the new front window. */
1407  w->z_front = nullptr;
1408  w->z_back = _z_front_window;
1409  _z_front_window->z_front = w;
1410  _z_front_window = w;
1411  } else {
1412  /* It's somewhere else in the z-ordering. */
1413  w->z_front = v->z_front;
1414  w->z_back = v;
1415  v->z_front->z_back = w;
1416  v->z_front = w;
1417  }
1418  }
1419 }
1420 
1421 
1427 {
1428  if (w->z_front == nullptr) {
1429  assert(_z_front_window == w);
1430  _z_front_window = w->z_back;
1431  } else {
1432  w->z_front->z_back = w->z_back;
1433  }
1434 
1435  if (w->z_back == nullptr) {
1436  assert(_z_back_window == w);
1437  _z_back_window = w->z_front;
1438  } else {
1439  w->z_back->z_front = w->z_front;
1440  }
1441 
1442  w->z_front = w->z_back = nullptr;
1443 }
1444 
1451 {
1454 
1455  w->SetDirty();
1456 }
1457 
1466 {
1467  /* Set up window properties; some of them are needed to set up smallest size below */
1468  this->window_class = this->window_desc->cls;
1469  this->SetWhiteBorder();
1470  if (this->window_desc->default_pos == WDP_CENTER) this->flags |= WF_CENTERED;
1471  this->owner = INVALID_OWNER;
1472  this->nested_focus = nullptr;
1473  this->window_number = window_number;
1474 
1475  this->OnInit();
1476  /* Initialize nested widget tree. */
1477  if (this->nested_array == nullptr) {
1478  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1479  this->nested_root->SetupSmallestSize(this, true);
1480  } else {
1481  this->nested_root->SetupSmallestSize(this, false);
1482  }
1483  /* Initialize to smallest size. */
1484  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1485 
1486  /* Further set up window properties,
1487  * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1488  this->resize.step_width = this->nested_root->resize_x;
1489  this->resize.step_height = this->nested_root->resize_y;
1490 
1491  /* Give focus to the opened window unless a text box
1492  * of focused window has focus (so we don't interrupt typing). But if the new
1493  * window has a text box, then take focus anyway. */
1494  if (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != nullptr) SetFocusedWindow(this);
1495 
1496  /* Insert the window into the correct location in the z-ordering. */
1497  AddWindowToZOrdering(this);
1498 }
1499 
1507 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1508 {
1509  this->left = x;
1510  this->top = y;
1511  this->width = sm_width;
1512  this->height = sm_height;
1513 }
1514 
1525 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
1526 {
1527  def_width = max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1528  def_height = max(def_height, this->height);
1529  /* Try to make windows smaller when our window is too small.
1530  * w->(width|height) is normally the same as min_(width|height),
1531  * but this way the GUIs can be made a little more dynamic;
1532  * one can use the same spec for multiple windows and those
1533  * can then determine the real minimum size of the window. */
1534  if (this->width != def_width || this->height != def_height) {
1535  /* Think about the overlapping toolbars when determining the minimum window size */
1536  int free_height = _screen.height;
1537  const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1538  if (wt != nullptr) free_height -= wt->height;
1540  if (wt != nullptr) free_height -= wt->height;
1541 
1542  int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
1543  int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
1544 
1545  /* X and Y has to go by step.. calculate it.
1546  * The cast to int is necessary else x/y are implicitly casted to
1547  * unsigned int, which won't work. */
1548  if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1549  if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1550 
1551  ResizeWindow(this, enlarge_x, enlarge_y);
1552  /* ResizeWindow() calls this->OnResize(). */
1553  } else {
1554  /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1555  this->OnResize();
1556  }
1557 
1558  int nx = this->left;
1559  int ny = this->top;
1560 
1561  if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1562 
1563  const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1564  ny = max(ny, (wt == nullptr || this == wt || this->top == 0) ? 0 : wt->height);
1565  nx = max(nx, 0);
1566 
1567  if (this->viewport != nullptr) {
1568  this->viewport->left += nx - this->left;
1569  this->viewport->top += ny - this->top;
1570  }
1571  this->left = nx;
1572  this->top = ny;
1573 
1574  this->SetDirty();
1575 }
1576 
1589 static bool IsGoodAutoPlace1(int left, int top, int width, int height, int toolbar_y, Point &pos)
1590 {
1591  int right = width + left;
1592  int bottom = height + top;
1593 
1594  if (left < 0 || top < toolbar_y || right > _screen.width || bottom > _screen.height) return false;
1595 
1596  /* Make sure it is not obscured by any window. */
1597  const Window *w;
1598  FOR_ALL_WINDOWS_FROM_BACK(w) {
1599  if (w->window_class == WC_MAIN_WINDOW) continue;
1600 
1601  if (right > w->left &&
1602  w->left + w->width > left &&
1603  bottom > w->top &&
1604  w->top + w->height > top) {
1605  return false;
1606  }
1607  }
1608 
1609  pos.x = left;
1610  pos.y = top;
1611  return true;
1612 }
1613 
1626 static bool IsGoodAutoPlace2(int left, int top, int width, int height, int toolbar_y, Point &pos)
1627 {
1628  bool rtl = _current_text_dir == TD_RTL;
1629 
1630  /* Left part of the rectangle may be at most 1/4 off-screen,
1631  * right part of the rectangle may be at most 1/2 off-screen
1632  */
1633  if (rtl) {
1634  if (left < -(width >> 1) || left > _screen.width - (width >> 2)) return false;
1635  } else {
1636  if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1637  }
1638 
1639  /* Bottom part of the rectangle may be at most 1/4 off-screen */
1640  if (top < toolbar_y || top > _screen.height - (height >> 2)) return false;
1641 
1642  /* Make sure it is not obscured by any window. */
1643  const Window *w;
1644  FOR_ALL_WINDOWS_FROM_BACK(w) {
1645  if (w->window_class == WC_MAIN_WINDOW) continue;
1646 
1647  if (left + width > w->left &&
1648  w->left + w->width > left &&
1649  top + height > w->top &&
1650  w->top + w->height > top) {
1651  return false;
1652  }
1653  }
1654 
1655  pos.x = left;
1656  pos.y = top;
1657  return true;
1658 }
1659 
1666 static Point GetAutoPlacePosition(int width, int height)
1667 {
1668  Point pt;
1669 
1670  bool rtl = _current_text_dir == TD_RTL;
1671 
1672  /* First attempt, try top-left of the screen */
1673  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1674  const int toolbar_y = main_toolbar != nullptr ? main_toolbar->height : 0;
1675  if (IsGoodAutoPlace1(rtl ? _screen.width - width : 0, toolbar_y, width, height, toolbar_y, pt)) return pt;
1676 
1677  /* Second attempt, try around all existing windows.
1678  * The new window must be entirely on-screen, and not overlap with an existing window.
1679  * Eight starting points are tried, two at each corner.
1680  */
1681  const Window *w;
1682  FOR_ALL_WINDOWS_FROM_BACK(w) {
1683  if (w->window_class == WC_MAIN_WINDOW) continue;
1684 
1685  if (IsGoodAutoPlace1(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1686  if (IsGoodAutoPlace1(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1687  if (IsGoodAutoPlace1(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1688  if (IsGoodAutoPlace1(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1689  if (IsGoodAutoPlace1(w->left + w->width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1690  if (IsGoodAutoPlace1(w->left - width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1691  if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1692  if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height, width, height, toolbar_y, pt)) return pt;
1693  }
1694 
1695  /* Third attempt, try around all existing windows.
1696  * The new window may be partly off-screen, and must not overlap with an existing window.
1697  * Only four starting points are tried.
1698  */
1699  FOR_ALL_WINDOWS_FROM_BACK(w) {
1700  if (w->window_class == WC_MAIN_WINDOW) continue;
1701 
1702  if (IsGoodAutoPlace2(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1703  if (IsGoodAutoPlace2(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1704  if (IsGoodAutoPlace2(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1705  if (IsGoodAutoPlace2(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1706  }
1707 
1708  /* Fourth and final attempt, put window at diagonal starting from (0, toolbar_y), try multiples
1709  * of the closebox
1710  */
1711  int left = rtl ? _screen.width - width : 0, top = toolbar_y;
1712  int offset_x = rtl ? -(int)NWidgetLeaf::closebox_dimension.width : (int)NWidgetLeaf::closebox_dimension.width;
1714 
1715 restart:
1716  FOR_ALL_WINDOWS_FROM_BACK(w) {
1717  if (w->left == left && w->top == top) {
1718  left += offset_x;
1719  top += offset_y;
1720  goto restart;
1721  }
1722  }
1723 
1724  pt.x = left;
1725  pt.y = top;
1726  return pt;
1727 }
1728 
1736 {
1737  const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1738  assert(w != nullptr);
1739  Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1740  return pt;
1741 }
1742 
1760 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1761 {
1762  Point pt;
1763  const Window *w;
1764 
1765  int16 default_width = max(desc->GetDefaultWidth(), sm_width);
1766  int16 default_height = max(desc->GetDefaultHeight(), sm_height);
1767 
1768  if (desc->parent_cls != WC_NONE && (w = FindWindowById(desc->parent_cls, window_number)) != nullptr) {
1769  bool rtl = _current_text_dir == TD_RTL;
1770  if (desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) {
1771  pt.x = w->left + (rtl ? w->width - default_width : 0);
1772  pt.y = w->top + w->height;
1773  return pt;
1774  } else {
1775  /* Position child window with offset of closebox, but make sure that either closebox or resizebox is visible
1776  * - Y position: closebox of parent + closebox of child + statusbar
1777  * - X position: closebox on left/right, resizebox on right/left (depending on ltr/rtl)
1778  */
1780  if (w->top + 3 * indent_y < _screen.height) {
1781  pt.y = w->top + indent_y;
1782  int indent_close = NWidgetLeaf::closebox_dimension.width;
1783  int indent_resize = NWidgetLeaf::resizebox_dimension.width;
1784  if (_current_text_dir == TD_RTL) {
1785  pt.x = max(w->left + w->width - default_width - indent_close, 0);
1786  if (pt.x + default_width >= indent_close && pt.x + indent_resize <= _screen.width) return pt;
1787  } else {
1788  pt.x = min(w->left + indent_close, _screen.width - default_width);
1789  if (pt.x + default_width >= indent_resize && pt.x + indent_close <= _screen.width) return pt;
1790  }
1791  }
1792  }
1793  }
1794 
1795  switch (desc->default_pos) {
1796  case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1797  return GetToolbarAlignedWindowPosition(default_width);
1798 
1799  case WDP_AUTO: // Find a good automatic position for the window
1800  return GetAutoPlacePosition(default_width, default_height);
1801 
1802  case WDP_CENTER: // Centre the window horizontally
1803  pt.x = (_screen.width - default_width) / 2;
1804  pt.y = (_screen.height - default_height) / 2;
1805  break;
1806 
1807  case WDP_MANUAL:
1808  pt.x = 0;
1809  pt.y = 0;
1810  break;
1811 
1812  default:
1813  NOT_REACHED();
1814  }
1815 
1816  return pt;
1817 }
1818 
1819 /* virtual */ Point Window::OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1820 {
1821  return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1822 }
1823 
1831 void Window::CreateNestedTree(bool fill_nested)
1832 {
1833  int biggest_index = -1;
1834  this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_parts, this->window_desc->nwid_length, &biggest_index, &this->shade_select);
1835  this->nested_array_size = (uint)(biggest_index + 1);
1836 
1837  if (fill_nested) {
1838  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1839  this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
1840  }
1841 }
1842 
1848 {
1849  this->InitializeData(window_number);
1850  this->ApplyDefaults();
1851  Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1852  this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1853  this->FindWindowPlacementAndResize(this->window_desc->GetDefaultWidth(), this->window_desc->GetDefaultHeight());
1854 }
1855 
1861 {
1862  this->CreateNestedTree(false);
1863  this->FinishInitNested(window_number);
1864 }
1865 
1870 Window::Window(WindowDesc *desc) : window_desc(desc), mouse_capture_widget(-1)
1871 {
1872 }
1873 
1881 Window *FindWindowFromPt(int x, int y)
1882 {
1883  Window *w;
1884  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1885  if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1886  return w;
1887  }
1888  }
1889 
1890  return nullptr;
1891 }
1892 
1897 {
1898  IConsoleClose();
1899 
1900  _z_back_window = nullptr;
1901  _z_front_window = nullptr;
1902  _focused_window = nullptr;
1903  _mouseover_last_w = nullptr;
1904  _last_scroll_window = nullptr;
1905  _scrolling_viewport = false;
1906  _mouse_hovering = false;
1907 
1908  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1909  NWidgetScrollbar::InvalidateDimensionCache();
1910 
1911  ShowFirstError();
1912 }
1913 
1918 {
1920 
1921  Window *w;
1922  FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
1923 
1924  for (w = _z_front_window; w != nullptr; /* nothing */) {
1925  Window *to_del = w;
1926  w = w->z_back;
1927  free(to_del);
1928  }
1929 
1930  _z_front_window = nullptr;
1931  _z_back_window = nullptr;
1932 }
1933 
1938 {
1940  InitWindowSystem();
1941  _thd.Reset();
1942 }
1943 
1944 static void DecreaseWindowCounters()
1945 {
1946  if (_scroller_click_timeout != 0) _scroller_click_timeout--;
1947 
1948  Window *w;
1949  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1950  if (_scroller_click_timeout == 0) {
1951  /* Unclick scrollbar buttons if they are pressed. */
1952  for (uint i = 0; i < w->nested_array_size; i++) {
1953  NWidgetBase *nwid = w->nested_array[i];
1954  if (nwid != nullptr && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
1955  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1958  w->mouse_capture_widget = -1;
1959  sb->SetDirty(w);
1960  }
1961  }
1962  }
1963  }
1964 
1965  /* Handle editboxes */
1967  pair.second->HandleEditBox(w, pair.first);
1968  }
1969 
1970  w->OnMouseLoop();
1971  }
1972 
1973  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1974  if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) {
1975  CLRBITS(w->flags, WF_TIMEOUT);
1976 
1977  w->OnTimeout();
1978  w->RaiseButtons(true);
1979  }
1980  }
1981 }
1982 
1983 static void HandlePlacePresize()
1984 {
1985  if (_special_mouse_mode != WSM_PRESIZE) return;
1986 
1987  Window *w = _thd.GetCallbackWnd();
1988  if (w == nullptr) return;
1989 
1990  Point pt = GetTileBelowCursor();
1991  if (pt.x == -1) {
1992  _thd.selend.x = -1;
1993  return;
1994  }
1995 
1996  w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1997 }
1998 
2004 {
2006 
2007  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
2008 
2009  Window *w = _thd.GetCallbackWnd();
2010  if (w != nullptr) {
2011  /* Send an event in client coordinates. */
2012  Point pt;
2013  pt.x = _cursor.pos.x - w->left;
2014  pt.y = _cursor.pos.y - w->top;
2015  if (_left_button_down) {
2016  w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
2017  } else {
2018  w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
2019  }
2020  }
2021 
2022  if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
2023  return ES_HANDLED;
2024 }
2025 
2027 static void HandleMouseOver()
2028 {
2029  Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2030 
2031  /* We changed window, put an OnMouseOver event to the last window */
2032  if (_mouseover_last_w != nullptr && _mouseover_last_w != w) {
2033  /* Reset mouse-over coordinates of previous window */
2034  Point pt = { -1, -1 };
2035  _mouseover_last_w->OnMouseOver(pt, 0);
2036  }
2037 
2038  /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
2039  _mouseover_last_w = w;
2040 
2041  if (w != nullptr) {
2042  /* send an event in client coordinates. */
2043  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2044  const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
2045  if (widget != nullptr) w->OnMouseOver(pt, widget->index);
2046  }
2047 }
2048 
2050 static const int MIN_VISIBLE_TITLE_BAR = 13;
2051 
2056 };
2057 
2068 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
2069 {
2070  if (v == nullptr) return;
2071 
2072  int v_bottom = v->top + v->height;
2073  int v_right = v->left + v->width;
2074  int safe_y = (dir == PHD_UP) ? (v->top - MIN_VISIBLE_TITLE_BAR - rect.top) : (v_bottom + MIN_VISIBLE_TITLE_BAR - rect.bottom); // Compute safe vertical position.
2075 
2076  if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
2077  if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
2078 
2079  /* Vertically, the rectangle is hidden behind v. */
2080  if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
2081  if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
2082  return;
2083  }
2084  if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
2085  if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
2086  return;
2087  }
2088 
2089  /* Horizontally also hidden, force movement to a safe area. */
2090  if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
2091  *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
2092  } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
2093  *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2094  } else {
2095  *ny = safe_y;
2096  }
2097 }
2098 
2106 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2107 {
2108  /* Search for the title bar rectangle. */
2109  Rect caption_rect;
2110  const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2111  if (caption != nullptr) {
2112  caption_rect.left = caption->pos_x;
2113  caption_rect.right = caption->pos_x + caption->current_x;
2114  caption_rect.top = caption->pos_y;
2115  caption_rect.bottom = caption->pos_y + caption->current_y;
2116 
2117  /* Make sure the window doesn't leave the screen */
2118  nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2119  ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2120 
2121  /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2122  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2123  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2124  }
2125 
2126  if (w->viewport != nullptr) {
2127  w->viewport->left += nx - w->left;
2128  w->viewport->top += ny - w->top;
2129  }
2130 
2131  w->left = nx;
2132  w->top = ny;
2133 }
2134 
2145 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
2146 {
2147  if (delta_x != 0 || delta_y != 0) {
2148  if (clamp_to_screen) {
2149  /* Determine the new right/bottom position. If that is outside of the bounds of
2150  * the resolution clamp it in such a manner that it stays within the bounds. */
2151  int new_right = w->left + w->width + delta_x;
2152  int new_bottom = w->top + w->height + delta_y;
2153  if (new_right >= (int)_cur_resolution.width) delta_x -= Ceil(new_right - _cur_resolution.width, max(1U, w->nested_root->resize_x));
2154  if (new_bottom >= (int)_cur_resolution.height) delta_y -= Ceil(new_bottom - _cur_resolution.height, max(1U, w->nested_root->resize_y));
2155  }
2156 
2157  w->SetDirty();
2158 
2159  uint new_xinc = max(0, (w->nested_root->resize_x == 0) ? 0 : (int)(w->nested_root->current_x - w->nested_root->smallest_x) + delta_x);
2160  uint new_yinc = max(0, (w->nested_root->resize_y == 0) ? 0 : (int)(w->nested_root->current_y - w->nested_root->smallest_y) + delta_y);
2161  assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2162  assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2163 
2165  w->width = w->nested_root->current_x;
2166  w->height = w->nested_root->current_y;
2167  }
2168 
2169  EnsureVisibleCaption(w, w->left, w->top);
2170 
2171  /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
2172  w->OnResize();
2173  w->SetDirty();
2174 }
2175 
2182 {
2184  return (w == nullptr) ? 0 : w->top + w->height;
2185 }
2186 
2193 {
2195  return (w == nullptr) ? _screen.height : w->top;
2196 }
2197 
2198 static bool _dragging_window;
2199 
2205 {
2206  /* Get out immediately if no window is being dragged at all. */
2207  if (!_dragging_window) return ES_NOT_HANDLED;
2208 
2209  /* If button still down, but cursor hasn't moved, there is nothing to do. */
2210  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2211 
2212  /* Otherwise find the window... */
2213  Window *w;
2214  FOR_ALL_WINDOWS_FROM_BACK(w) {
2215  if (w->flags & WF_DRAGGING) {
2216  /* Stop the dragging if the left mouse button was released */
2217  if (!_left_button_down) {
2218  w->flags &= ~WF_DRAGGING;
2219  break;
2220  }
2221 
2222  w->SetDirty();
2223 
2224  int x = _cursor.pos.x + _drag_delta.x;
2225  int y = _cursor.pos.y + _drag_delta.y;
2226  int nx = x;
2227  int ny = y;
2228 
2230  const Window *v;
2231 
2234  int delta;
2235 
2236  FOR_ALL_WINDOWS_FROM_BACK(v) {
2237  if (v == w) continue; // Don't snap at yourself
2238 
2239  if (y + w->height > v->top && y < v->top + v->height) {
2240  /* Your left border <-> other right border */
2241  delta = abs(v->left + v->width - x);
2242  if (delta <= hsnap) {
2243  nx = v->left + v->width;
2244  hsnap = delta;
2245  }
2246 
2247  /* Your right border <-> other left border */
2248  delta = abs(v->left - x - w->width);
2249  if (delta <= hsnap) {
2250  nx = v->left - w->width;
2251  hsnap = delta;
2252  }
2253  }
2254 
2255  if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2256  /* Your left border <-> other left border */
2257  delta = abs(v->left - x);
2258  if (delta <= hsnap) {
2259  nx = v->left;
2260  hsnap = delta;
2261  }
2262 
2263  /* Your right border <-> other right border */
2264  delta = abs(v->left + v->width - x - w->width);
2265  if (delta <= hsnap) {
2266  nx = v->left + v->width - w->width;
2267  hsnap = delta;
2268  }
2269  }
2270 
2271  if (x + w->width > v->left && x < v->left + v->width) {
2272  /* Your top border <-> other bottom border */
2273  delta = abs(v->top + v->height - y);
2274  if (delta <= vsnap) {
2275  ny = v->top + v->height;
2276  vsnap = delta;
2277  }
2278 
2279  /* Your bottom border <-> other top border */
2280  delta = abs(v->top - y - w->height);
2281  if (delta <= vsnap) {
2282  ny = v->top - w->height;
2283  vsnap = delta;
2284  }
2285  }
2286 
2287  if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2288  /* Your top border <-> other top border */
2289  delta = abs(v->top - y);
2290  if (delta <= vsnap) {
2291  ny = v->top;
2292  vsnap = delta;
2293  }
2294 
2295  /* Your bottom border <-> other bottom border */
2296  delta = abs(v->top + v->height - y - w->height);
2297  if (delta <= vsnap) {
2298  ny = v->top + v->height - w->height;
2299  vsnap = delta;
2300  }
2301  }
2302  }
2303  }
2304 
2305  EnsureVisibleCaption(w, nx, ny);
2306 
2307  w->SetDirty();
2308  return ES_HANDLED;
2309  } else if (w->flags & WF_SIZING) {
2310  /* Stop the sizing if the left mouse button was released */
2311  if (!_left_button_down) {
2312  w->flags &= ~WF_SIZING;
2313  w->SetDirty();
2314  break;
2315  }
2316 
2317  /* Compute difference in pixels between cursor position and reference point in the window.
2318  * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2319  */
2320  int x, y = _cursor.pos.y - _drag_delta.y;
2321  if (w->flags & WF_SIZING_LEFT) {
2322  x = _drag_delta.x - _cursor.pos.x;
2323  } else {
2324  x = _cursor.pos.x - _drag_delta.x;
2325  }
2326 
2327  /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2328  if (w->resize.step_width == 0) x = 0;
2329  if (w->resize.step_height == 0) y = 0;
2330 
2331  /* Check the resize button won't go past the bottom of the screen */
2332  if (w->top + w->height + y > _screen.height) {
2333  y = _screen.height - w->height - w->top;
2334  }
2335 
2336  /* X and Y has to go by step.. calculate it.
2337  * The cast to int is necessary else x/y are implicitly casted to
2338  * unsigned int, which won't work. */
2339  if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2340  if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2341 
2342  /* Check that we don't go below the minimum set size */
2343  if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2344  x = w->nested_root->smallest_x - w->width;
2345  }
2346  if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2347  y = w->nested_root->smallest_y - w->height;
2348  }
2349 
2350  /* Window already on size */
2351  if (x == 0 && y == 0) return ES_HANDLED;
2352 
2353  /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2354  _drag_delta.y += y;
2355  if ((w->flags & WF_SIZING_LEFT) && x != 0) {
2356  _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2357  w->SetDirty();
2358  w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2359  /* ResizeWindow() below ensures marking new position as dirty. */
2360  } else {
2361  _drag_delta.x += x;
2362  }
2363 
2364  /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
2365  ResizeWindow(w, x, y);
2366  return ES_HANDLED;
2367  }
2368  }
2369 
2370  _dragging_window = false;
2371  return ES_HANDLED;
2372 }
2373 
2378 static void StartWindowDrag(Window *w)
2379 {
2380  w->flags |= WF_DRAGGING;
2381  w->flags &= ~WF_CENTERED;
2382  _dragging_window = true;
2383 
2384  _drag_delta.x = w->left - _cursor.pos.x;
2385  _drag_delta.y = w->top - _cursor.pos.y;
2386 
2387  BringWindowToFront(w);
2389 }
2390 
2396 static void StartWindowSizing(Window *w, bool to_left)
2397 {
2398  w->flags |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
2399  w->flags &= ~WF_CENTERED;
2400  _dragging_window = true;
2401 
2402  _drag_delta.x = _cursor.pos.x;
2403  _drag_delta.y = _cursor.pos.y;
2404 
2405  BringWindowToFront(w);
2407 }
2408 
2414 {
2415  int i;
2417  bool rtl = false;
2418 
2419  if (sb->type == NWID_HSCROLLBAR) {
2420  i = _cursor.pos.x - _cursorpos_drag_start.x;
2421  rtl = _current_text_dir == TD_RTL;
2422  } else {
2423  i = _cursor.pos.y - _cursorpos_drag_start.y;
2424  }
2425 
2426  if (sb->disp_flags & ND_SCROLLBAR_BTN) {
2427  if (_scroller_click_timeout == 1) {
2428  _scroller_click_timeout = 3;
2429  sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
2430  w->SetDirty();
2431  }
2432  return;
2433  }
2434 
2435  /* Find the item we want to move to and make sure it's inside bounds. */
2436  int pos = min(RoundDivSU(max(0, i + _scrollbar_start_pos) * sb->GetCount(), _scrollbar_size), max(0, sb->GetCount() - sb->GetCapacity()));
2437  if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
2438  if (pos != sb->GetPosition()) {
2439  sb->SetPosition(pos);
2440  w->SetDirty();
2441  }
2442 }
2443 
2449 {
2450  Window *w;
2451  FOR_ALL_WINDOWS_FROM_BACK(w) {
2452  if (w->mouse_capture_widget >= 0) {
2453  /* Abort if no button is clicked any more. */
2454  if (!_left_button_down) {
2455  w->mouse_capture_widget = -1;
2456  w->SetDirty();
2457  return ES_HANDLED;
2458  }
2459 
2460  /* Handle scrollbar internally, or dispatch click event */
2462  if (type == NWID_VSCROLLBAR || type == NWID_HSCROLLBAR) {
2464  } else {
2465  /* If cursor hasn't moved, there is nothing to do. */
2466  if (_cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2467 
2468  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2469  w->OnClick(pt, w->mouse_capture_widget, 0);
2470  }
2471  return ES_HANDLED;
2472  }
2473  }
2474 
2475  return ES_NOT_HANDLED;
2476 }
2477 
2483 {
2484  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2485 
2486  if (!_scrolling_viewport) return ES_NOT_HANDLED;
2487 
2488  /* When we don't have a last scroll window we are starting to scroll.
2489  * When the last scroll window and this are not the same we went
2490  * outside of the window and should not left-mouse scroll anymore. */
2491  if (_last_scroll_window == nullptr) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2492 
2493  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))) {
2494  _cursor.fix_at = false;
2495  _scrolling_viewport = false;
2496  _last_scroll_window = nullptr;
2497  return ES_NOT_HANDLED;
2498  }
2499 
2500  if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
2501  /* If the main window is following a vehicle, then first let go of it! */
2502  const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2503  ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2504  return ES_NOT_HANDLED;
2505  }
2506 
2507  Point delta;
2508  if (scrollwheel_scrolling) {
2509  /* We are using scrollwheels for scrolling */
2510  delta.x = _cursor.h_wheel;
2511  delta.y = _cursor.v_wheel;
2512  _cursor.v_wheel = 0;
2513  _cursor.h_wheel = 0;
2514  } else {
2516  delta.x = -_cursor.delta.x;
2517  delta.y = -_cursor.delta.y;
2518  } else {
2519  delta.x = _cursor.delta.x;
2520  delta.y = _cursor.delta.y;
2521  }
2522  }
2523 
2524  /* Create a scroll-event and send it to the window */
2525  if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2526 
2527  _cursor.delta.x = 0;
2528  _cursor.delta.y = 0;
2529  return ES_HANDLED;
2530 }
2531 
2543 {
2544  bool bring_to_front = false;
2545 
2546  if (w->window_class == WC_MAIN_WINDOW ||
2547  IsVitalWindow(w) ||
2548  w->window_class == WC_TOOLTIPS ||
2550  return true;
2551  }
2552 
2553  /* Use unshaded window size rather than current size for shaded windows. */
2554  int w_width = w->width;
2555  int w_height = w->height;
2556  if (w->IsShaded()) {
2557  w_width = w->unshaded_size.width;
2558  w_height = w->unshaded_size.height;
2559  }
2560 
2561  Window *u;
2563  /* A modal child will prevent the activation of the parent window */
2564  if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) {
2565  u->SetWhiteBorder();
2566  u->SetDirty();
2567  return false;
2568  }
2569 
2570  if (u->window_class == WC_MAIN_WINDOW ||
2571  IsVitalWindow(u) ||
2572  u->window_class == WC_TOOLTIPS ||
2574  continue;
2575  }
2576 
2577  /* Window sizes don't interfere, leave z-order alone */
2578  if (w->left + w_width <= u->left ||
2579  u->left + u->width <= w->left ||
2580  w->top + w_height <= u->top ||
2581  u->top + u->height <= w->top) {
2582  continue;
2583  }
2584 
2585  bring_to_front = true;
2586  }
2587 
2588  if (bring_to_front) BringWindowToFront(w);
2589  return true;
2590 }
2591 
2600 EventState Window::HandleEditBoxKey(int wid, WChar key, uint16 keycode)
2601 {
2602  QueryString *query = this->GetQueryString(wid);
2603  if (query == nullptr) return ES_NOT_HANDLED;
2604 
2605  int action = QueryString::ACTION_NOTHING;
2606 
2607  switch (query->text.HandleKeyPress(key, keycode)) {
2608  case HKPR_EDITING:
2609  this->SetWidgetDirty(wid);
2610  this->OnEditboxChanged(wid);
2611  break;
2612 
2613  case HKPR_CURSOR:
2614  this->SetWidgetDirty(wid);
2615  /* For the OSK also invalidate the parent window */
2616  if (this->window_class == WC_OSK) this->InvalidateData();
2617  break;
2618 
2619  case HKPR_CONFIRM:
2620  if (this->window_class == WC_OSK) {
2621  this->OnClick(Point(), WID_OSK_OK, 1);
2622  } else if (query->ok_button >= 0) {
2623  this->OnClick(Point(), query->ok_button, 1);
2624  } else {
2625  action = query->ok_button;
2626  }
2627  break;
2628 
2629  case HKPR_CANCEL:
2630  if (this->window_class == WC_OSK) {
2631  this->OnClick(Point(), WID_OSK_CANCEL, 1);
2632  } else if (query->cancel_button >= 0) {
2633  this->OnClick(Point(), query->cancel_button, 1);
2634  } else {
2635  action = query->cancel_button;
2636  }
2637  break;
2638 
2639  case HKPR_NOT_HANDLED:
2640  return ES_NOT_HANDLED;
2641 
2642  default: break;
2643  }
2644 
2645  switch (action) {
2647  this->UnfocusFocusedWidget();
2648  break;
2649 
2651  if (query->text.bytes <= 1) {
2652  /* If already empty, unfocus instead */
2653  this->UnfocusFocusedWidget();
2654  } else {
2655  query->text.DeleteAll();
2656  this->SetWidgetDirty(wid);
2657  this->OnEditboxChanged(wid);
2658  }
2659  break;
2660 
2661  default:
2662  break;
2663  }
2664 
2665  return ES_HANDLED;
2666 }
2667 
2673 void HandleKeypress(uint keycode, WChar key)
2674 {
2675  /* World generation is multithreaded and messes with companies.
2676  * But there is no company related window open anyway, so _current_company is not used. */
2677  assert(HasModalProgress() || IsLocalCompany());
2678 
2679  /*
2680  * The Unicode standard defines an area called the private use area. Code points in this
2681  * area are reserved for private use and thus not portable between systems. For instance,
2682  * Apple defines code points for the arrow keys in this area, but these are only printable
2683  * on a system running OS X. We don't want these keys to show up in text fields and such,
2684  * and thus we have to clear the unicode character when we encounter such a key.
2685  */
2686  if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2687 
2688  /*
2689  * If both key and keycode is zero, we don't bother to process the event.
2690  */
2691  if (key == 0 && keycode == 0) return;
2692 
2693  /* Check if the focused window has a focused editbox */
2694  if (EditBoxInGlobalFocus()) {
2695  /* All input will in this case go to the focused editbox */
2696  if (_focused_window->window_class == WC_CONSOLE) {
2697  if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2698  } else {
2699  if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return;
2700  }
2701  }
2702 
2703  /* Call the event, start with the uppermost window, but ignore the toolbar. */
2704  Window *w;
2705  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2706  if (w->window_class == WC_MAIN_TOOLBAR) continue;
2707  if (w->window_desc->hotkeys != nullptr) {
2708  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2709  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2710  }
2711  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2712  }
2713 
2715  /* When there is no toolbar w is null, check for that */
2716  if (w != nullptr) {
2717  if (w->window_desc->hotkeys != nullptr) {
2718  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2719  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2720  }
2721  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2722  }
2723 
2724  HandleGlobalHotkeys(key, keycode);
2725 }
2726 
2731 {
2732  /* Call the event, start with the uppermost window. */
2733  Window *w;
2734  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2735  if (w->OnCTRLStateChange() == ES_HANDLED) return;
2736  }
2737 }
2738 
2744 /* virtual */ void Window::InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2745 {
2746  QueryString *query = this->GetQueryString(wid);
2747  if (query == nullptr) return;
2748 
2749  if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2750  this->SetWidgetDirty(wid);
2751  this->OnEditboxChanged(wid);
2752  }
2753 }
2754 
2761 void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2762 {
2763  if (!EditBoxInGlobalFocus()) return;
2764 
2765  _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end);
2766 }
2767 
2775 
2780 static void HandleAutoscroll()
2781 {
2782  if (_game_mode == GM_MENU || HasModalProgress()) return;
2784  if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2785 
2786  int x = _cursor.pos.x;
2787  int y = _cursor.pos.y;
2788  Window *w = FindWindowFromPt(x, y);
2789  if (w == nullptr || w->flags & WF_DISABLE_VP_SCROLL) return;
2791 
2792  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2793  if (vp == nullptr) return;
2794 
2795  x -= vp->left;
2796  y -= vp->top;
2797 
2798  /* here allows scrolling in both x and y axis */
2799  static const int SCROLLSPEED = 3;
2800  if (x - 15 < 0) {
2801  w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * SCROLLSPEED, vp->zoom);
2802  } else if (15 - (vp->width - x) > 0) {
2803  w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * SCROLLSPEED, vp->zoom);
2804  }
2805  if (y - 15 < 0) {
2806  w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * SCROLLSPEED, vp->zoom);
2807  } else if (15 - (vp->height - y) > 0) {
2808  w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * SCROLLSPEED, vp->zoom);
2809  }
2810 }
2811 
2813  MC_NONE = 0,
2814  MC_LEFT,
2815  MC_RIGHT,
2816  MC_DOUBLE_LEFT,
2817  MC_HOVER,
2818 
2822 };
2824 
2825 static void ScrollMainViewport(int x, int y)
2826 {
2827  if (_game_mode != GM_MENU) {
2829  assert(w);
2830 
2833  }
2834 }
2835 
2845 static const int8 scrollamt[16][2] = {
2846  { 0, 0},
2847  {-2, 0},
2848  { 0, -2},
2849  {-2, -1},
2850  { 2, 0},
2851  { 0, 0},
2852  { 2, -1},
2853  { 0, -2},
2854  { 0, 2},
2855  {-2, 1},
2856  { 0, 0},
2857  {-2, 0},
2858  { 2, 1},
2859  { 0, 2},
2860  { 2, 0},
2861  { 0, 0},
2862 };
2863 
2864 static void HandleKeyScrolling()
2865 {
2866  /*
2867  * Check that any of the dirkeys is pressed and that the focused window
2868  * doesn't have an edit-box as focused widget.
2869  */
2870  if (_dirkeys && !EditBoxInGlobalFocus()) {
2871  int factor = _shift_pressed ? 50 : 10;
2872  ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2873  }
2874 }
2875 
2876 static void MouseLoop(MouseClick click, int mousewheel)
2877 {
2878  /* World generation is multithreaded and messes with companies.
2879  * But there is no company related window open anyway, so _current_company is not used. */
2880  assert(HasModalProgress() || IsLocalCompany());
2881 
2882  HandlePlacePresize();
2884 
2885  if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2886  if (HandleMouseDragDrop() == ES_HANDLED) return;
2887  if (HandleWindowDragging() == ES_HANDLED) return;
2888  if (HandleActiveWidget() == ES_HANDLED) return;
2889  if (HandleViewportScroll() == ES_HANDLED) return;
2890 
2891  HandleMouseOver();
2892 
2893  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2894  if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2895 
2896  int x = _cursor.pos.x;
2897  int y = _cursor.pos.y;
2898  Window *w = FindWindowFromPt(x, y);
2899  if (w == nullptr) return;
2900 
2901  if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2902  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2903 
2904  /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2905  if (vp != nullptr && (_game_mode == GM_MENU || HasModalProgress())) return;
2906 
2907  if (mousewheel != 0) {
2908  /* Send mousewheel event to window, unless we're scrolling a viewport or the map */
2909  if (!scrollwheel_scrolling || (vp == nullptr && w->window_class != WC_SMALLMAP)) w->OnMouseWheel(mousewheel);
2910 
2911  /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2912  if (vp == nullptr) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2913  }
2914 
2915  if (vp != nullptr) {
2916  if (scrollwheel_scrolling && !(w->flags & WF_DISABLE_VP_SCROLL)) {
2917  _scrolling_viewport = true;
2918  _cursor.fix_at = true;
2919  return;
2920  }
2921 
2922  switch (click) {
2923  case MC_DOUBLE_LEFT:
2924  case MC_LEFT:
2925  if (HandleViewportClicked(vp, x, y)) return;
2926  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2928  _scrolling_viewport = true;
2929  _cursor.fix_at = false;
2930  return;
2931  }
2932  break;
2933 
2934  case MC_RIGHT:
2935  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2937  _scrolling_viewport = true;
2940  return;
2941  }
2942  break;
2943 
2944  default:
2945  break;
2946  }
2947  }
2948 
2949  if (vp == nullptr || (w->flags & WF_DISABLE_VP_SCROLL)) {
2950  switch (click) {
2951  case MC_LEFT:
2952  case MC_DOUBLE_LEFT:
2953  DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2954  return;
2955 
2956  default:
2957  if (!scrollwheel_scrolling || w == nullptr || w->window_class != WC_SMALLMAP) break;
2958  /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2959  * Simulate a right button click so we can get started. */
2960  FALLTHROUGH;
2961 
2962  case MC_RIGHT:
2963  DispatchRightClickEvent(w, x - w->left, y - w->top);
2964  return;
2965 
2966  case MC_HOVER:
2967  DispatchHoverEvent(w, x - w->left, y - w->top);
2968  break;
2969  }
2970  }
2971 
2972  /* We're not doing anything with 2D scrolling, so reset the value. */
2973  _cursor.h_wheel = 0;
2974  _cursor.v_wheel = 0;
2975 }
2976 
2981 {
2982  /* World generation is multithreaded and messes with companies.
2983  * But there is no company related window open anyway, so _current_company is not used. */
2984  assert(HasModalProgress() || IsLocalCompany());
2985 
2986  static int double_click_time = 0;
2987  static Point double_click_pos = {0, 0};
2988 
2989  /* Mouse event? */
2990  MouseClick click = MC_NONE;
2992  click = MC_LEFT;
2993  if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
2994  double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2995  double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2996  click = MC_DOUBLE_LEFT;
2997  }
2998  double_click_time = _realtime_tick;
2999  double_click_pos = _cursor.pos;
3000  _left_button_clicked = true;
3002  } else if (_right_button_clicked) {
3003  _right_button_clicked = false;
3004  click = MC_RIGHT;
3006  }
3007 
3008  int mousewheel = 0;
3009  if (_cursor.wheel) {
3010  mousewheel = _cursor.wheel;
3011  _cursor.wheel = 0;
3013  }
3014 
3015  static uint32 hover_time = 0;
3016  static Point hover_pos = {0, 0};
3017 
3019  if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
3020  hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
3021  hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
3022  hover_pos = _cursor.pos;
3023  hover_time = _realtime_tick;
3024  _mouse_hovering = false;
3025  } else {
3026  if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
3027  click = MC_HOVER;
3029  _mouse_hovering = true;
3030  }
3031  }
3032  }
3033 
3034  /* Handle sprite picker before any GUI interaction */
3036  /* Next realtime tick? Then redraw has finished */
3037  _newgrf_debug_sprite_picker.mode = SPM_NONE;
3039  }
3040 
3041  if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
3042  /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
3044  _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
3047  _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
3049  } else {
3050  MouseLoop(click, mousewheel);
3051  }
3052 
3053  /* We have moved the mouse the required distance,
3054  * no need to move it at any later time. */
3055  _cursor.delta.x = 0;
3056  _cursor.delta.y = 0;
3057 }
3058 
3062 static void CheckSoftLimit()
3063 {
3064  if (_settings_client.gui.window_soft_limit == 0) return;
3065 
3066  for (;;) {
3067  uint deletable_count = 0;
3068  Window *w, *last_deletable = nullptr;
3069  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3070  if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags & WF_STICKY)) continue;
3071 
3072  last_deletable = w;
3073  deletable_count++;
3074  }
3075 
3076  /* We've not reached the soft limit yet. */
3077  if (deletable_count <= _settings_client.gui.window_soft_limit) break;
3078 
3079  assert(last_deletable != nullptr);
3080  delete last_deletable;
3081  }
3082 }
3083 
3088 {
3089  /* World generation is multithreaded and messes with companies.
3090  * But there is no company related window open anyway, so _current_company is not used. */
3091  assert(HasModalProgress() || IsLocalCompany());
3092 
3093  CheckSoftLimit();
3094 
3095  /* Do the actual free of the deleted windows. */
3096  for (Window *v = _z_front_window; v != nullptr; /* nothing */) {
3097  Window *w = v;
3098  v = v->z_back;
3099 
3100  if (w->window_class != WC_INVALID) continue;
3101 
3103  free(w);
3104  }
3105 
3106  if (_input_events_this_tick != 0) {
3107  /* The input loop is called only once per GameLoop() - so we can clear the counter here */
3109  /* there were some inputs this tick, don't scroll ??? */
3110  return;
3111  }
3112 
3113  /* HandleMouseEvents was already called for this tick */
3115 }
3116 
3120 void CallWindowRealtimeTickEvent(uint delta_ms)
3121 {
3122  Window *w;
3123  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3124  w->OnRealtimeTick(delta_ms);
3125  }
3126 }
3127 
3132 {
3133  static uint32 last_realtime_tick = _realtime_tick;
3134  uint delta_ms = _realtime_tick - last_realtime_tick;
3135  last_realtime_tick = _realtime_tick;
3136 
3137  if (delta_ms == 0) return;
3138 
3139  PerformanceMeasurer framerate(PFE_DRAWING);
3141 
3142  CallWindowRealtimeTickEvent(delta_ms);
3143 
3144  static GUITimer network_message_timer = GUITimer(1);
3145  if (network_message_timer.Elapsed(delta_ms)) {
3146  network_message_timer.SetInterval(1000);
3148  }
3149 
3150  Window *w;
3151 
3152  static GUITimer window_timer = GUITimer(1);
3153  if (window_timer.Elapsed(delta_ms)) {
3154  if (_network_dedicated) window_timer.SetInterval(MILLISECONDS_PER_TICK);
3155 
3156  extern int _caret_timer;
3157  _caret_timer += 3;
3158  CursorTick();
3159 
3160  HandleKeyScrolling();
3161  HandleAutoscroll();
3162  DecreaseWindowCounters();
3163  }
3164 
3165  static GUITimer highlight_timer = GUITimer(1);
3166  if (highlight_timer.Elapsed(delta_ms)) {
3167  highlight_timer.SetInterval(450);
3169  }
3170 
3171  if (!_pause_mode || _game_mode == GM_EDITOR || _settings_game.construction.command_pause_level > CMDPL_NO_CONSTRUCTION) MoveAllTextEffects(delta_ms);
3172 
3173  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3176  }
3177 
3178  /* Skip the actual drawing on dedicated servers without screen.
3179  * But still empty the invalidation queues above. */
3180  if (_network_dedicated) return;
3181 
3182  static GUITimer hundredth_timer = GUITimer(1);
3183  if (hundredth_timer.Elapsed(delta_ms)) {
3184  hundredth_timer.SetInterval(3000); // Historical reason: 100 * MILLISECONDS_PER_TICK
3185 
3186  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3187  w->OnHundredthTick();
3188  }
3189  }
3190 
3191  if (window_timer.HasElapsed()) {
3192  window_timer.SetInterval(MILLISECONDS_PER_TICK);
3193 
3194  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3195  if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) {
3197  w->SetDirty();
3198  }
3199  }
3200  }
3201 
3202  DrawDirtyBlocks();
3203 
3204  FOR_ALL_WINDOWS_FROM_BACK(w) {
3205  /* Update viewport only if window is not shaded. */
3206  if (w->viewport != nullptr && !w->IsShaded()) UpdateViewportPosition(w);
3207  }
3209  /* Redraw mouse cursor in case it was hidden */
3210  DrawMouseCursor();
3211 }
3212 
3219 {
3220  const Window *w;
3221  FOR_ALL_WINDOWS_FROM_BACK(w) {
3222  if (w->window_class == cls && w->window_number == number) w->SetDirty();
3223  }
3224 }
3225 
3232 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
3233 {
3234  const Window *w;
3235  FOR_ALL_WINDOWS_FROM_BACK(w) {
3236  if (w->window_class == cls && w->window_number == number) {
3237  w->SetWidgetDirty(widget_index);
3238  }
3239  }
3240 }
3241 
3247 {
3248  Window *w;
3249  FOR_ALL_WINDOWS_FROM_BACK(w) {
3250  if (w->window_class == cls) w->SetDirty();
3251  }
3252 }
3253 
3259 void Window::InvalidateData(int data, bool gui_scope)
3260 {
3261  this->SetDirty();
3262  if (!gui_scope) {
3263  /* Schedule GUI-scope invalidation for next redraw. */
3264  this->scheduled_invalidation_data.push_back(data);
3265  }
3266  this->OnInvalidateData(data, gui_scope);
3267 }
3268 
3273 {
3274  for (int data : this->scheduled_invalidation_data) {
3275  if (this->window_class == WC_INVALID) break;
3276  this->OnInvalidateData(data, true);
3277  }
3278  this->scheduled_invalidation_data.clear();
3279 }
3280 
3285 {
3286  if ((this->flags & WF_HIGHLIGHTED) == 0) return;
3287 
3288  for (uint i = 0; i < this->nested_array_size; i++) {
3289  if (this->IsWidgetHighlighted(i)) this->SetWidgetDirty(i);
3290  }
3291 }
3292 
3319 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3320 {
3321  Window *w;
3322  FOR_ALL_WINDOWS_FROM_BACK(w) {
3323  if (w->window_class == cls && w->window_number == number) {
3324  w->InvalidateData(data, gui_scope);
3325  }
3326  }
3327 }
3328 
3337 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3338 {
3339  Window *w;
3340 
3341  FOR_ALL_WINDOWS_FROM_BACK(w) {
3342  if (w->window_class == cls) {
3343  w->InvalidateData(data, gui_scope);
3344  }
3345  }
3346 }
3347 
3352 {
3353  Window *w;
3354  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3355  w->OnGameTick();
3356  }
3357 }
3358 
3366 {
3367  Window *w;
3368 
3369 restart_search:
3370  /* When we find the window to delete, we need to restart the search
3371  * as deleting this window could cascade in deleting (many) others
3372  * anywhere in the z-array */
3373  FOR_ALL_WINDOWS_FROM_BACK(w) {
3374  if (w->window_class != WC_MAIN_WINDOW &&
3375  w->window_class != WC_SELECT_GAME &&
3376  w->window_class != WC_MAIN_TOOLBAR &&
3377  w->window_class != WC_STATUS_BAR &&
3378  w->window_class != WC_TOOLTIPS &&
3379  (w->flags & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
3380 
3381  delete w;
3382  goto restart_search;
3383  }
3384  }
3385 }
3386 
3395 {
3396  Window *w;
3397 
3398  /* Delete every window except for stickied ones, then sticky ones as well */
3400 
3401 restart_search:
3402  /* When we find the window to delete, we need to restart the search
3403  * as deleting this window could cascade in deleting (many) others
3404  * anywhere in the z-array */
3405  FOR_ALL_WINDOWS_FROM_BACK(w) {
3406  if (w->flags & WF_STICKY) {
3407  delete w;
3408  goto restart_search;
3409  }
3410  }
3411 }
3412 
3417 {
3419  InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
3420  InvalidateWindowData(WC_MESSAGE_HISTORY, 0); // invalidate the message history
3421  DeleteWindowById(WC_NEWS_WINDOW, 0); // close newspaper or general message window if shown
3422 }
3423 
3429 {
3430  Window *w;
3431 
3432 restart_search:
3433  /* When we find the window to delete, we need to restart the search
3434  * as deleting this window could cascade in deleting (many) others
3435  * anywhere in the z-array */
3436  FOR_ALL_WINDOWS_FROM_BACK(w) {
3437  if (w->window_desc->flags & WDF_CONSTRUCTION) {
3438  delete w;
3439  goto restart_search;
3440  }
3441  }
3442 
3443  FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
3444 }
3445 
3448 {
3451 }
3452 
3455 {
3456  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3457  NWidgetScrollbar::InvalidateDimensionCache();
3458 
3459  extern void InitDepotWindowBlockSizes();
3461 
3462  Window *w;
3463  FOR_ALL_WINDOWS_FROM_BACK(w) {
3464  w->ReInit();
3465  }
3466 
3467  void NetworkReInitChatBoxSize();
3469 
3470  /* Make sure essential parts of all windows are visible */
3473 }
3474 
3482 static int PositionWindow(Window *w, WindowClass clss, int setting)
3483 {
3484  if (w == nullptr || w->window_class != clss) {
3485  w = FindWindowById(clss, 0);
3486  }
3487  if (w == nullptr) return 0;
3488 
3489  int old_left = w->left;
3490  switch (setting) {
3491  case 1: w->left = (_screen.width - w->width) / 2; break;
3492  case 2: w->left = _screen.width - w->width; break;
3493  default: w->left = 0; break;
3494  }
3495  if (w->viewport != nullptr) w->viewport->left += w->left - old_left;
3496  SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3497  return w->left;
3498 }
3499 
3506 {
3507  DEBUG(misc, 5, "Repositioning Main Toolbar...");
3509 }
3510 
3517 {
3518  DEBUG(misc, 5, "Repositioning statusbar...");
3520 }
3521 
3528 {
3529  DEBUG(misc, 5, "Repositioning news message...");
3531 }
3532 
3539 {
3540  DEBUG(misc, 5, "Repositioning network chat window...");
3542 }
3543 
3544 
3550 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3551 {
3552  Window *w;
3553  FOR_ALL_WINDOWS_FROM_BACK(w) {
3554  if (w->viewport != nullptr && w->viewport->follow_vehicle == from_index) {
3555  w->viewport->follow_vehicle = to_index;
3556  w->SetDirty();
3557  }
3558  }
3559 }
3560 
3561 
3567 void RelocateAllWindows(int neww, int newh)
3568 {
3570 
3571  Window *w;
3572  FOR_ALL_WINDOWS_FROM_BACK(w) {
3573  int left, top;
3574  /* XXX - this probably needs something more sane. For example specifying
3575  * in a 'backup'-desc that the window should always be centered. */
3576  switch (w->window_class) {
3577  case WC_MAIN_WINDOW:
3578  case WC_BOOTSTRAP:
3579  ResizeWindow(w, neww, newh);
3580  continue;
3581 
3582  case WC_MAIN_TOOLBAR:
3583  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3584 
3585  top = w->top;
3586  left = PositionMainToolbar(w); // changes toolbar orientation
3587  break;
3588 
3589  case WC_NEWS_WINDOW:
3590  top = newh - w->height;
3591  left = PositionNewsMessage(w);
3592  break;
3593 
3594  case WC_STATUS_BAR:
3595  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3596 
3597  top = newh - w->height;
3598  left = PositionStatusbar(w);
3599  break;
3600 
3601  case WC_SEND_NETWORK_MSG:
3602  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3603 
3604  top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3605  left = PositionNetworkChatWindow(w);
3606  break;
3607 
3608  case WC_CONSOLE:
3609  IConsoleResize(w);
3610  continue;
3611 
3612  default: {
3613  if (w->flags & WF_CENTERED) {
3614  top = (newh - w->height) >> 1;
3615  left = (neww - w->width) >> 1;
3616  break;
3617  }
3618 
3619  left = w->left;
3620  if (left + (w->width >> 1) >= neww) left = neww - w->width;
3621  if (left < 0) left = 0;
3622 
3623  top = w->top;
3624  if (top + (w->height >> 1) >= newh) top = newh - w->height;
3625  break;
3626  }
3627  }
3628 
3629  EnsureVisibleCaption(w, left, top);
3630  }
3631 }
3632 
3639 {
3640  this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
3642 }
EventState
State of handling an event.
Definition: window_type.h:717
Window * _z_back_window
List of windows opened at the screen sorted from the back.
Definition: window.cpp:59
Functions related to OTTD&#39;s strings.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
static void CheckSoftLimit()
Check the soft limit of deletable (non vital, non sticky) windows.
Definition: window.cpp:3062
Generate landscape (newgame); Window numbers:
Definition: window_type.h:449
Empty widget, place holder to reserve space in widget array.
Definition: widget_type.h:46
SpecialMouseMode
Mouse modes.
Definition: window_gui.h:904
Functions/types related to NewGRF debugging.
void SetShaded(bool make_shaded)
Set the shaded state of the window to make_shaded.
Definition: window.cpp:1031
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:793
static Window * FindChildWindow(const Window *w, WindowClass wc)
Find the Window whose parent pointer points to this window.
Definition: window.cpp:1058
static const int ACTION_DESELECT
Deselect editbox.
static bool IsLocalCompany()
Is the current company the local company?
Definition: company_func.h:43
WindowNumber window_number
The WindowNumber of the window that is responsible for the selection mode.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:141
virtual void OnScroll(Point delta)
Handle the request for (viewport) scrolling.
Definition: window_gui.h:666
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:1626
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
Definition: window.cpp:1881
ViewportAutoscrolling
Values for _settings_client.gui.auto_scrolling.
Definition: window.cpp:45
Base of all video drivers.
static Window * _mouseover_last_w
Window of the last OnMouseOver event.
Definition: window.cpp:53
Data about how and where to blit pixels.
Definition: gfx_type.h:154
bool InsertString(const char *str, bool marked, const char *caret=nullptr, const char *insert_location=nullptr, const char *replacement_end=nullptr)
Insert a string into the text buffer.
Definition: textbuf.cpp:162
ResizeInfo resize
Resize information.
Definition: window_gui.h:322
virtual EventState OnHotkey(int hotkey)
A hotkey has been pressed.
Definition: window.cpp:610
const QueryString * GetQueryString(uint widnum) const
Return the querystring associated to a editbox.
Definition: window.cpp:329
virtual void ApplyDefaults()
Read default values from WindowDesc configuration an apply them to the window.
Definition: window.cpp:182
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1870
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:48
Window * _z_front_window
List of windows opened at the screen sorted from the front.
Definition: window.cpp:57
const NWidgetCore * nested_focus
Currently focused nested widget, or nullptr if no nested widget has focus.
Definition: window_gui.h:327
Point pos
logical mouse position
Definition: gfx_type.h:117
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3218
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:899
uint resize_x
Horizontal resize step (0 means not resizable).
Definition: widget_type.h:164
void SetFocusedWindow(Window *w)
Set the window that has the focus.
Definition: window.cpp:434
void SetTimeout()
Set the timeout flag of the window and initiate the timer.
Definition: window_gui.h:366
Window * parent
Parent window.
Definition: window_gui.h:337
void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
Handle text input.
Definition: window.cpp:2761
High level window description.
Definition: window_gui.h:166
An invalid owner.
Definition: company_type.h:29
Saveload window; Window numbers:
Definition: window_type.h:137
Bootstrap; Window numbers:
Definition: window_type.h:637
Landscape generation (in Scenario Editor); Window numbers:
Definition: window_type.h:442
virtual void OnDragDrop(Point pt, int widget)
A dragged &#39;object&#39; has been released.
Definition: window_gui.h:660
Window is being resized towards the right.
Definition: window_gui.h:235
StringID tool_tip
Tooltip of the widget.
Definition: widget_type.h:304
WindowFlags flags
Window flags.
Definition: window_gui.h:310
int left
x position of left edge of the window
Definition: window_gui.h:317
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:2068
SpecialMouseMode _special_mouse_mode
Mode of the mouse.
Definition: window.cpp:80
int height
Screen height of the viewport.
Definition: viewport_type.h:26
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
Hotkey related functions.
Cancel key.
Definition: osk_widget.h:17
virtual bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond)
Event to display a custom tooltip.
Definition: window_gui.h:646
int16 pref_height
User-preferred height of the window. Zero if unset.
Definition: window_gui.h:185
Scrollbar data structure.
Definition: widget_type.h:587
static void RemoveWindowFromZOrdering(Window *w)
Removes a window from the z-ordering.
Definition: window.cpp:1426
uint8 toolbar_pos
position of toolbars, 0=left, 1=center, 2=right
uint8 window_snap_radius
windows snap at each other if closer than this
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:40
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:597
static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
Dispatch left mouse-button (possibly double) click in window.
Definition: window.cpp:652
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
Progress report of landscape generation; Window numbers:
Definition: window_type.h:456
Nested widget to display and control a scrollbar in a window.
Definition: widget_type.h:748
void NetworkReInitChatBoxSize()
Initialize all font-dependent chat box sizes.
void CallWindowGameTickEvent()
Dispatch OnGameTick event over all windows.
Definition: window.cpp:3351
void DeleteConstructionWindows()
Delete all windows that are used for construction of vehicle etc.
Definition: window.cpp:3428
Dragging an object.
Definition: window_gui.h:906
textfile; Window numbers:
Definition: window_type.h:180
uint16 hover_delay_ms
time required to activate a hover event, in milliseconds
Definition: settings_type.h:93
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1133
The passed event is not handled.
Definition: window_type.h:719
bool Elapsed(uint delta)
Test if a timer has elapsed.
Definition: guitimer_func.h:55
std::vector< SpriteID > sprites
Sprites found.
Definition: newgrf_debug.h:30
void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets,...)
Sets the enabled/disabled status of a list of widgets.
Definition: window.cpp:536
NewGRF debug box (at top-right of a window, between WWT_CAPTION and WWT_SHADEBOX) ...
Definition: widget_type.h:61
int PositionMainToolbar(Window *w)
(Re)position main toolbar window at the screen.
Definition: window.cpp:3505
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:76
Dimension unshaded_size
Last known unshaded size (only valid while shaded).
Definition: window_gui.h:333
WidgetType type
Type of the widget / nested widget.
Definition: widget_type.h:161
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition: window.cpp:2730
Types for recording game performance data.
void InitWindowSystem()
(re)initialize the windowing system
Definition: window.cpp:1896
static Dimension closebox_dimension
Cached size of a closebox widget.
Definition: widget_type.h:781
void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
Shows a tooltip.
Definition: misc_gui.cpp:764
Time between 2 left clicks before it becoming a double click, in ms.
Definition: window.cpp:2820
virtual ~PickerWindowBase()
Destructor of the base class PickerWindowBase Main utility is to stop the base Window destructor from...
Definition: window.cpp:3638
void CDECL void DeleteAll()
Delete every character in the textbuffer.
Definition: textbuf.cpp:116
a textbox for typing
Definition: widget_type.h:69
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:66
PreventHideDirection
Direction for moving the window.
Definition: window.cpp:2053
static const int ACTION_CLEAR
Clear editbox.
Buyout company (merger); Window numbers:
Definition: window_type.h:577
void SetPosition(int position)
Sets the position of the first visible element.
Definition: widget_type.h:699
Vehicle data structure.
Definition: vehicle_base.h:210
int top
y position of top edge of the window
Definition: window_gui.h:318
void IConsoleResize(Window *w)
Change the size of the in-game console window after the screen size changed, or the window state chan...
Escape key pressed.
Definition: textbuf_type.h:25
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:22
virtual const char * GetFocusedText() const
Get the current input text if an edit box has the focus.
Definition: window.cpp:350
virtual void OnEditboxChanged(int widget)
The text in an editbox has been edited.
Definition: window_gui.h:728
static EventState HandleViewportScroll()
Handle viewport scrolling with the mouse.
Definition: window.cpp:2482
void * clicked_pixel
Clicked pixel (pointer to blitter buffer)
Definition: newgrf_debug.h:28
static EventState HandleWindowDragging()
Handle dragging/resizing of a window.
Definition: window.cpp:2204
virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
Compute the initial position of the window.
Definition: window.cpp:1819
void UpdateTileSelection()
Updates tile highlighting for all cases.
Definition: viewport.cpp:2483
Close box (at top-left of a window)
Definition: widget_type.h:67
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:21
virtual void OnMouseLoop()
Called for every mouse loop run, which is at least once per (game) tick.
Definition: window_gui.h:686
static void StartWindowDrag(Window *w)
Start window dragging.
Definition: window.cpp:2378
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3131
static bool _dragging_window
A window is being dragged or resized.
Definition: window.cpp:2198
static void StartWindowSizing(Window *w, bool to_left)
Start resizing a window.
Definition: window.cpp:2396
static Dimension resizebox_dimension
Cached size of a resizebox widget.
Definition: widget_type.h:780
void ReInit(int rx=0, int ry=0)
Re-initialize a window, and optionally change its size.
Definition: window.cpp:995
Scroll all viewports at their edges.
Definition: window.cpp:49
WindowClass
Window classes.
Definition: window_type.h:37
#define CLRBITS(x, y)
Clears several bits in a variable.
int16 default_width_trad
Preferred initial width of the window (pixels at 1x zoom).
Definition: window_gui.h:194
Invalid window.
Definition: window_type.h:700
void DeleteAllMessages()
Delete all messages and their corresponding window (if any).
Definition: window.cpp:3416
How all blitters should look like.
Definition: base.hpp:28
uint16 bytes
the current size of the string in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:35
uint smallest_x
Smallest horizontal size of the widget in a filled window.
Definition: widget_type.h:169
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Definition: vehicle_type.h:55
RAII class for measuring simple elements of performance.
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
Speed of drawing world and GUI.
Map moves with mouse movement on holding right mouse button, cursor position is fixed.
Definition: settings_type.h:75
WindowClass cls
Class of the window,.
Definition: window_gui.h:175
void DeleteAllNonVitalWindows()
It is possible that a stickied window gets to a position where the &#39;close&#39; button is outside the gami...
Definition: window.cpp:3394
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2673
Non-text change, e.g. cursor position.
Definition: textbuf_type.h:23
Window is made sticky by user.
Definition: window_gui.h:238
Presizing mode (docks, tunnels).
Definition: window_gui.h:908
static void Reset(PerformanceElement elem)
Store the previous accumulator value and reset for a new cycle of accumulating measurements.
Above v is a safe position.
Definition: window.cpp:2054
void InputLoop()
Regular call from the global game loop.
Definition: window.cpp:3087
virtual void OnTimeout()
Called when this window&#39;s timeout has been reached.
Definition: window_gui.h:706
Window is being resized towards the left.
Definition: window_gui.h:236
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1831
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
Window * GetCallbackWnd()
Get the window that started the current highlighting.
Definition: viewport.cpp:2469
bool IsDisabled() const
Return whether the widget is disabled.
Definition: widget_type.h:356
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
Definition: math_func.hpp:336
bool _left_button_clicked
Is left mouse button clicked?
Definition: gfx.cpp:39
static 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:248
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:55
Console; Window numbers:
Definition: window_type.h:631
virtual const char * GetMarkedText(size_t *length) const
Get the range of the currently marked input text.
Definition: window.cpp:377
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Definition: window.cpp:2192
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:908
Functions related to (drawing on) viewports.
Sprite aligner (debug); Window numbers:
Definition: window_type.h:668
static void HandleAutoscroll()
If needed and switched on, perform auto scrolling (automatically moving window contents when mouse is...
Definition: window.cpp:2780
void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
Switches viewports following vehicles, which get autoreplaced.
Definition: window.cpp:3550
Base for the GUIs that have an edit box in them.
const char * ini_key
Key to store window defaults in openttd.cfg. nullptr if nothing shall be stored.
Definition: window_gui.h:177
static void DispatchHoverEvent(Window *w, int x, int y)
Dispatch hover of the mouse over a window.
Definition: window.cpp:810
Data structure for an opened window.
Definition: window_gui.h:276
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1847
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:3337
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1860
int PositionStatusbar(Window *w)
(Re)position statusbar window at the screen.
Definition: window.cpp:3516
static const int ACTION_NOTHING
Nothing.
Main window; Window numbers:
Definition: window_type.h:44
Vehicle orders; Window numbers:
Definition: window_type.h:205
int16 GetDefaultHeight() const
Determine default height of window.
Definition: window.cpp:133
void NetworkChatMessageLoop()
Check if a message is expired.
uint8 scroll_mode
viewport scroll mode
Definition: settings_type.h:96
Point selend
The location where the drag currently ends.
int16 default_height_trad
Preferred initial height of the window (pixels at 1x zoom).
Definition: window_gui.h:195
NWidgetBase ** nested_array
Array of pointers into the tree. Do not access directly, use Window::GetWidget() instead.
Definition: window_gui.h:330
uint8 valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
EventState VpHandlePlaceSizingDrag()
Handle the mouse while dragging for placement/resizing.
Definition: viewport.cpp:3245
GUI Timers.
virtual void OnFocus()
Called when window gains focus.
Definition: window.cpp:516
Functions related to errors.
Bit value of the &#39;dropdown active&#39; flag.
Definition: widget_type.h:271
static EventState HandleActiveWidget()
Handle active widget (mouse draggin on widget) with the mouse.
Definition: window.cpp:2448
bool _right_button_clicked
Is right mouse button clicked?
Definition: gfx.cpp:41
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:63
virtual EventState OnKeyPress(WChar key, uint16 keycode)
A key has been pressed.
Definition: window_gui.h:604
static bool DescSorter(WindowDesc *const &a, WindowDesc *const &b)
Sort WindowDesc by ini_key.
Definition: window.cpp:155
Error message; Window numbers:
Definition: window_type.h:103
void HideVitalWindows()
Delete all always on-top windows to get an empty screen.
Definition: window.cpp:3447
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:959
This window is used for construction; close it whenever changing company.
Definition: window_gui.h:208
int PositionNewsMessage(Window *w)
(Re)position news message window at the screen.
Definition: window.cpp:3527
virtual void OnFocusLost()
Called when window loses focus.
Definition: window.cpp:524
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:1666
void SetLowered(bool lowered)
Lower or raise the widget.
Definition: widget_type.h:335
static Point _drag_delta
delta between mouse cursor and upper left corner of dragged window
Definition: window.cpp:52
Simple pair of data.
Chatbox; Window numbers:
Definition: window_type.h:491
Bit value of the &#39;scrollbar up&#39; flag.
Definition: widget_type.h:272
Tooltip window; Window numbers:
Definition: window_type.h:109
void IniLoadWindowSettings(IniFile *ini, const char *grpname, void *desc)
Load a WindowDesc from config.
Definition: settings.cpp:771
int16 GetDefaultWidth() const
Determine default width of window.
Definition: window.cpp:123
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
uint smallest_y
Smallest vertical size of the widget in a filled window.
Definition: widget_type.h:170
static std::vector< WindowDesc * > * _window_descs
List of all WindowDescs.
Definition: window.cpp:86
uint current_y
Current vertical size (after resizing).
Definition: widget_type.h:173
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
Window is being dragged.
Definition: window_gui.h:234
SmallMap< int, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition: window_gui.h:328
Small map; Window numbers:
Definition: window_type.h:97
On Screen Keyboard; Window numbers:
Definition: window_type.h:155
void RaiseButtons(bool autoraise=false)
Raise the buttons of the window.
Definition: window.cpp:573
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:38
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:177
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:1735
Functions related to the gfx engine.
abort current news display (active news were deleted)
Definition: statusbar_gui.h:19
void InitNewsItemStructs()
Initialize the news-items data structures.
Definition: news_gui.cpp:625
Functions related to setting/changing the settings.
void RelocateAllWindows(int neww, int newh)
Relocate all windows to fit the new size of the game application screen.
Definition: window.cpp:3567
Data stored about a string that can be modified in the GUI.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
Display plane with zero size vertically, and filling and resizing horizontally.
Definition: widget_type.h:387
Types related to global configuration settings.
uint8 scrollwheel_scrolling
scrolling using the scroll wheel?
void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour)
Sets the highlighted status of a widget.
Definition: window.cpp:232
void LoadFromDisk(const char *filename, Subdirectory subdir)
Load the Ini file&#39;s data from the disk.
Definition: ini_load.cpp:210
Functions related to modal progress.
A path without any base directory.
Definition: fileio_type.h:125
Definition of base types and functions in a cross-platform compatible way.
virtual NWidgetCore * GetWidgetFromPos(int x, int y)=0
Retrieve a widget by its position.
WindowPosition default_pos
Preferred position of the window.
Definition: window_gui.h:174
Window is centered and shall stay centered after ReInit.
Definition: window_gui.h:242
int GetMainViewTop()
Return the top of the main view available for general use.
Definition: window.cpp:2181
A number of safeguards to prevent using unsafe methods.
static void HandleMouseOver()
Report position of the mouse to the underlying window.
Definition: window.cpp:2027
void SetWhiteBorder()
Set the timeout flag of the window and initiate the timer.
Definition: window_gui.h:375
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:245
int wheel
mouse wheel movement
Definition: gfx_type.h:119
List of hotkeys for a window.
Definition: hotkeys.h:40
static const int8 scrollamt[16][2]
Describes all the different arrow key combinations the game allows when it is in scrolling mode...
Definition: window.cpp:2845
void DeleteCompanyWindows(CompanyID id)
Delete all windows of a company.
Definition: window.cpp:1200
uint32 click_time
Realtime tick when clicked to detect next frame.
Definition: newgrf_debug.h:29
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:305
static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
Dispatch the mousewheel-action to the window.
Definition: window.cpp:838
static void SaveToConfig()
Save all WindowDesc settings to _windows_file.
Definition: window.cpp:164
static void InvalidateDimensionCache()
Reset the cached dimensions.
Definition: widget.cpp:2084
void DeleteChildWindows(WindowClass wc=WC_INVALID) const
Delete all children a window might have in a head-recursive manner.
Definition: window.cpp:1072
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
Definition: window.cpp:3538
uint nested_array_size
Size of the nested array.
Definition: window_gui.h:331
bool right_mouse_wnd_close
close window with right click
Key does not affect editboxes.
Definition: textbuf_type.h:26
Custom currency; Window numbers:
Definition: window_type.h:612
virtual void OnHover(Point pt, int widget)
The mouse is hovering over a widget in the window, perform an action for it.
Definition: window_gui.h:638
Window timeout counter.
Definition: window_gui.h:232
Finances of a company; Window numbers:
Definition: window_type.h:516
EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode)
Process keypress for editbox widget.
Definition: window.cpp:2600
virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
Insert a text string at the cursor position into the edit box widget.
Definition: window.cpp:2744
Horizontal scrollbar.
Definition: widget_type.h:81
uint step_height
Step-size of height resize changes.
Definition: window_gui.h:218
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:309
bool _mouse_hovering
The mouse is hovering over the same point.
Definition: window.cpp:78
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition: widget_type.h:175
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:31
virtual void OnMouseOver(Point pt, int widget)
The mouse is currently moving over the window or has just moved outside of the window.
Definition: window_gui.h:674
Console functions used outside of the console code.
Company colour selection; Window numbers:
Definition: window_type.h:223
Highscore; Window numbers:
Definition: window_type.h:643
int GetRowFromWidget(int clickpos, int widget, int padding, int line_height=-1) const
Compute the row of a widget that a user clicked in.
Definition: window.cpp:201
Center the window.
Definition: window_gui.h:155
int 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:160
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition: gfx_type.h:120
GUI related functions in the console.
Road vehicle list; Window numbers:
Definition: window_type.h:307
int scrollbar_index
Index of an attached scrollbar.
Definition: widget_type.h:305
static bool MaybeBringWindowToFront(Window *w)
Check if a window can be made relative top-most window, and if so do it.
Definition: window.cpp:2542
WindowDesc(WindowPosition default_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad, WindowClass window_class, WindowClass parent_class, uint32 flags, const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys=nullptr)
Window description constructor.
Definition: window.cpp:92
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:2106
Baseclass for nested widgets.
Definition: widget_type.h:124
Button with a drop-down.
Definition: widget_type.h:80
uint8 white_border_timer
Timer value of the WF_WHITE_BORDER for flags.
Definition: window_gui.h:315
Basic functions/variables used all over the place.
Below v is a safe position.
Definition: window.cpp:2055
void InitializeData(WindowNumber window_number)
Initializes the data (except the position and initial size) of a new Window.
Definition: window.cpp:1465
void SetDirtyBlocks(int left, int top, int right, int bottom)
This function extends the internal _invalid_rect rectangle as it now contains the rectangle defined b...
Definition: gfx.cpp:1557
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as &#39;dirty&#39;.
Definition: gfx.cpp:1442
static void DispatchRightClickEvent(Window *w, int x, int y)
Dispatch right mouse-button click in window.
Definition: window.cpp:784
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:145
uint resize_y
Vertical resize step (0 means not resizable).
Definition: widget_type.h:165
VehicleID follow_vehicle
VehicleID to follow if following a vehicle, INVALID_VEHICLE otherwise.
Definition: window_gui.h:257
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
Types related to reading/writing &#39;*.ini&#39; files.
Window has a widget that has a highlight.
Definition: window_gui.h:241
Scroll main viewport at edge.
Definition: window.cpp:48
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition: window.cpp:2980
bool pref_sticky
Preferred stickyness.
Definition: window_gui.h:183
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1178
Window is being resized.
Definition: window_gui.h:237
ViewPort * IsPtInWindowViewport(const Window *w, int x, int y)
Is a xy position inside the viewport of the window?
Definition: viewport.cpp:393
int32 dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:261
void UnInitWindowSystem()
Close down the windowing system.
Definition: window.cpp:1917
Initialize nested widget tree to smallest size. Also updates current_x and current_y.
Definition: widget_type.h:110
Maximum mouse movement before stopping a hover event.
Definition: window.cpp:2821
static const int WIDGET_LIST_END
indicate the end of widgets&#39; list for vararg functions
Definition: widget_type.h:20
The window is a modal child of some other window, meaning the parent is &#39;inactive&#39;.
Definition: window_gui.h:209
Station list; Window numbers:
Definition: window_type.h:295
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:137
No construction actions may be executed.
Definition: command_type.h:421
uint32 flags
Flags.
Definition: window_gui.h:178
static const int MIN_VISIBLE_TITLE_BAR
The minimum number of pixels of the title bar must be visible in both the X or Y direction.
Definition: window.cpp:2050
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:984
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:36
Build toolbar; Window numbers:
Definition: window_type.h:66
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
Offset of the caption text at the top.
Definition: window_gui.h:127
Network status window; Window numbers:
Definition: window_type.h:485
News history list; Window numbers:
Definition: window_type.h:265
MouseClick
Definition: window.cpp:2812
uint16 GetCount() const
Gets the number of elements in the list.
Definition: widget_type.h:611
Select game window; Window numbers:
Definition: window_type.h:435
Scroll main viewport at edge when using fullscreen.
Definition: window.cpp:47
Window * z_back
The window behind us in z-order.
Definition: window_gui.h:339
int left
Screen coordinate left edge of the viewport.
Definition: viewport_type.h:23
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1165
void UnshowCriticalError()
Unshow the critical error.
Definition: error_gui.cpp:359
virtual bool OnRightClick(Point pt, int widget)
A click with the right mouse button has been made on the window.
Definition: window_gui.h:631
Company infrastructure overview; Window numbers:
Definition: window_type.h:570
virtual void OnPaint()
The window must be repainted.
Definition: window_gui.h:558
void DeleteNonVitalWindows()
Try to delete a non-vital window.
Definition: window.cpp:3365
Functions related to companies.
WidgetType
Window widget types, nested widget types, and nested widget part types.
Definition: widget_type.h:44
Return or enter key pressed.
Definition: textbuf_type.h:24
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:194
bool ScrollMainWindowTo(int x, int y, int z, bool instant)
Scrolls the main window to given coordinates.
int32 dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:260
virtual ~Window()
Remove window and all its child windows from the window stack.
Definition: window.cpp:1084
void ReInitAllWindows()
Re-initialize all windows.
Definition: window.cpp:3454
bool EditBoxInGlobalFocus()
Check if an edit box is in global focus.
Definition: window.cpp:457
Ini file that supports both loading and saving.
Definition: ini_type.h:86
virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)=0
Assign size and position to the widget.
virtual NWidgetBase * GetWidgetOfType(WidgetType tp)
Retrieve a widget by its type.
Definition: widget.cpp:793
void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
Special handling for the scrollbar widget type.
Definition: widget.cpp:136
Save preset; Window numbers:
Definition: window_type.h:680
GUISettings gui
settings related to the GUI
How much the mouse is allowed to move to call it a double click.
Definition: window.cpp:2819
Align toward the toolbar.
Definition: window_gui.h:156
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting)
Definition: window.cpp:3232
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
virtual const char * GetCaret() const
Get the string at the caret if an edit box has the focus.
Definition: window.cpp:363
Base class for all vehicles.
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:22
static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
Compute the position of the top-left corner of a new window that is opened.
Definition: window.cpp:1760
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
Definition: window.cpp:1507
Time spent drawing world viewports in GUI.
Offset of the caption text at the bottom.
Definition: window_gui.h:128
bool IsWidgetHighlighted(byte widget_index) const
Gets the highlighted status of a widget.
Definition: window.cpp:265
Ships list; Window numbers:
Definition: window_type.h:313
Window * z_front
The window in front of us in z-order.
Definition: window_gui.h:338
uint _toolbar_width
Width of the toolbar, shared by statusbar.
Definition: toolbar_gui.cpp:63
void ProcessScheduledInvalidations()
Process all scheduled invalidations.
Definition: window.cpp:3272
bool _window_highlight_colour
If false, highlight is white, otherwise the by the widget defined colour.
Definition: window.cpp:62
WindowClass parent_cls
Class of the parent window.
Definition: window_gui.h:176
virtual EventState OnCTRLStateChange()
The state of the control key has changed.
Definition: window_gui.h:613
virtual const char * GetTextCharacterAtPosition(const Point &pt) const
Get the character that is rendered at a position by the focused edit box.
Definition: window.cpp:421
void UpdatePosition(int difference, ScrollbarStepping unit=SS_SMALL)
Updates the position of the first visible element by the given amount.
Definition: widget_type.h:712
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
Last Item. use WIDGETS_END to fill up padding!!
Definition: widget_type.h:70
uint8 command_pause_level
level/amount of commands that can&#39;t be executed while paused
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Bit value of the &#39;scrollbar up&#39; or &#39;scrollbar down&#39; flag.
Definition: widget_type.h:274
virtual void ShowNewGRFInspectWindow() const
Show the NewGRF inspection window.
Definition: window_gui.h:809
This window won&#39;t get focus/make any other window lose focus when click.
Definition: window_gui.h:210
static int _input_events_this_tick
Local counter that is incremented each time an mouse input event is detected.
Definition: window.cpp:2774
static void LoadFromConfig()
Load all WindowDesc settings from _windows_file.
Definition: window.cpp:141
Up-button is lowered bit.
Definition: widget_type.h:260
void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets,...)
Sets the lowered/raised status of a list of widgets.
Definition: window.cpp:555
int index
Index of the nested widget in the widget array of the window (-1 means &#39;not used&#39;).
Definition: widget_type.h:302
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:81
virtual Rect GetTextBoundingRect(const char *from, const char *to) const
Get the bounding rectangle for a text range if an edit box has the focus.
Definition: window.cpp:406
void Reset()
Reset tile highlighting.
Definition: viewport.cpp:2448
Trains list; Window numbers:
Definition: window_type.h:301
void NetworkDrawChatMessage()
Draw the chat message-box.
Functions related to zooming.
virtual void OnGameTick()
Called once per (game) tick.
Definition: window_gui.h:691
static void HandleScrollbarScrolling(Window *w)
Handle scrollbar scrolling with the mouse.
Definition: window.cpp:2413
void UnfocusFocusedWidget()
Makes no widget on this window have focus.
Definition: window.cpp:479
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:215
Network window; Window numbers:
Definition: window_type.h:466
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:172
uint8 window_soft_limit
soft limit of maximum number of non-stickied non-vital windows (0 = no limit)
bool SetFocusedWidget(int widget_index)
Set focus within this window to the given widget.
Definition: window.cpp:495
bool in_window
mouse inside this window, determines drawing logic
Definition: gfx_type.h:141
Viewport moves with mouse movement on holding right mouse button, cursor position is fixed...
Definition: settings_type.h:74
Do not autoscroll when mouse is at edge of viewport.
Definition: window.cpp:46
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...
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
Definition: window.cpp:1149
NWidgetContainer * MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select)
Make a nested widget tree for a window from a parts array.
Definition: widget.cpp:2812
virtual void OnRealtimeTick(uint delta_ms)
Called periodically.
Definition: window_gui.h:701
The normal zoom level.
Definition: zoom_type.h:22
uint step_width
Step-size of width resize changes.
Definition: window_gui.h:217
HotkeyList * hotkeys
Hotkeys for the window.
Definition: window_gui.h:181
Aircraft list; Window numbers:
Definition: window_type.h:319
int32 z_pos
z coordinate.
Definition: vehicle_base.h:268
virtual void EditBoxLostFocus()
An edit box lost the input focus.
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
Base functions for all Games.
Network functions used by other parts of OpenTTD.
Main toolbar (the long bar at the top); Window numbers:
Definition: window_type.h:51
Coordinates of a point in 2D.
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:1589
AI list; Window numbers:
Definition: window_type.h:277
uint8 auto_scrolling
scroll when moving mouse to the edge (see ViewportAutoscrolling)
Definition: settings_type.h:91
static void BringWindowToFront(Window *w)
On clicking on a window, make it the frontmost window of all windows with an equal or lower z-priorit...
Definition: window.cpp:1450
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:620
int16 pref_width
User-preferred width of the window. Zero if unset.
Definition: window_gui.h:184
Map moves with mouse movement on holding left mouse button, cursor moves.
Definition: settings_type.h:77
Window does not do autoscroll,.
Definition: window_gui.h:239
ConstructionSettings construction
construction of things in-game
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable...
Definition: window_gui.h:324
Endscreen; Window numbers:
Definition: window_type.h:649
AI settings; Window numbers:
Definition: window_type.h:168
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:1226
virtual void OnMouseDrag(Point pt, int widget)
An &#39;object&#39; is being dragged at the provided position, highlight the target if possible.
Definition: window_gui.h:653
void HandleButtonClick(byte widget)
Do all things to make a button look clicked and mark it to be unclicked in a few ticks.
Definition: window.cpp:635
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
int CheckMatch(uint16 keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition: hotkeys.cpp:317
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.
Definition: window.cpp:1525
Popup with confirm question; Window numbers:
Definition: window_type.h:123
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:33
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
void DisableAllWidgetHighlight()
Disable the highlighted status of all widgets.
Definition: window.cpp:212
virtual void OnClick(Point pt, int widget, int click_count)
A click with the left mouse button has been made on the window.
Definition: window_gui.h:622
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:319
int32 x_pos
x coordinate.
Definition: vehicle_base.h:266
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static EventState HandleMouseDragDrop()
Handle dragging and dropping in mouse dragging mode (WSM_DRAGDROP).
Definition: window.cpp:2003
virtual void OnResize()
Called after the window got resized.
Definition: window_gui.h:713
NewGRF parameters; Window numbers:
Definition: window_type.h:174
static uint GetWindowZPriority(WindowClass wc)
Get the z-priority for a given window.
Definition: window.cpp:1299
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:711
static int PositionWindow(Window *w, WindowClass clss, int setting)
(Re)position a window at the screen.
Definition: window.cpp:3482
WindowClass window_class
Window class.
Definition: window_gui.h:311
virtual void OnMouseWheel(int wheel)
The mouse wheel has been turned.
Definition: window_gui.h:680
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3353
Specification of a rectangle with absolute coordinates of all edges.
Vertical scrollbar.
Definition: widget_type.h:82
WindowClass window_class
The WindowClass of the window that is responsible for the selection mode.
bool IsShaded() const
Is window shaded currently?
Definition: window_gui.h:524
The passed event is handled.
Definition: window_type.h:718
int32 y_pos
y coordinate.
Definition: vehicle_base.h:267
Text is written right-to-left by default.
Definition: strings_type.h:24
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:312
std::vector< int > scheduled_invalidation_data
Data of scheduled OnInvalidateData() calls.
Definition: window_gui.h:282
bool _scrolling_viewport
A viewport is being scrolled with the mouse.
Definition: window.cpp:77
bool FocusedWindowIsConsole()
Check if a console is focused.
Definition: window.cpp:471
Functions related to tile highlights.
void ResetWindowSystem()
Reset the windowing system, by means of shutting it down followed by re-initialization.
Definition: window.cpp:1937
static Window * _last_scroll_window
Window of the last scroll event.
Definition: window.cpp:54
Window functions not directly related to making/drawing windows.
Point delta
relative mouse movement in this tick
Definition: gfx_type.h:118
int top
Screen coordinate top edge of the viewport.
Definition: viewport_type.h:24
static uint Ceil(uint a, uint b)
Computes ceil(a / b) * b for non-negative a and b.
Definition: math_func.hpp:325
Resize the nested widget tree.
Definition: widget_type.h:111
Find a place automatically.
Definition: window_gui.h:154
void CallWindowRealtimeTickEvent(uint delta_ms)
Dispatch OnRealtimeTick event over all windows.
Definition: window.cpp:3120
Manually align the window (so no automatic location finding)
Definition: window_gui.h:153
NWidgetDisplay disp_flags
Flags that affect display and interaction with the widget.
Definition: widget_type.h:300
virtual void OnHundredthTick()
Called once every 100 (game) ticks.
Definition: window_gui.h:696
uint8 statusbar_pos
position of statusbar, 0=left, 1=center, 2=right
virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
A dropdown window associated to this window has been closed.
Definition: window.cpp:282
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:326
void IniSaveWindowSettings(IniFile *ini, const char *grpname, void *desc)
Save a WindowDesc to config.
Definition: settings.cpp:782
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3246
Functions related to news.
uint8 timeout_timer
Timer value of the WF_TIMEOUT for flags.
Definition: window_gui.h:314
Functions, definitions and such used only by the GUI.
virtual Point GetCaretPosition() const
Get the current caret position if an edit box has the focus.
Definition: window.cpp:390
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
const NWID * GetWidget(uint widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition: window_gui.h:842
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2145
Company view; Window numbers:
Definition: window_type.h:362
virtual void SetDirty(const Window *w) const
Mark the widget as &#39;dirty&#39; (in need of repaint).
Definition: widget.cpp:773
Window white border counter bit mask.
Definition: window_gui.h:240
WindowPosition
How do we the window to be placed?
Definition: window_gui.h:152
NWidgetBase * nested_root
Root of the nested tree.
Definition: window_gui.h:329
Query string window; Window numbers:
Definition: window_type.h:116
Bit value of the &#39;scrollbar down&#39; flag.
Definition: widget_type.h:273
NewGrfDebugSpritePickerMode mode
Current state.
Definition: newgrf_debug.h:27
Window * BringWindowToFrontById(WindowClass cls, WindowNumber number)
Find a window and make it the relative top-window on the screen.
Definition: window.cpp:1262
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62
Factory to &#39;query&#39; all available blitters.
Game options window; Window numbers:
Definition: window_type.h:606
Ok key.
Definition: osk_widget.h:18
WindowDesc * window_desc
Window description.
Definition: window_gui.h:309
int pos_y
Vertical position of top-left corner of the widget in the window.
Definition: widget_type.h:176
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window&#39;s data as invalid (in need of re-computing)
Definition: window.cpp:3259
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:21
Textbuf content changed.
Definition: textbuf_type.h:22
char * _windows_file
Config file to store WindowDesc.
Definition: window.cpp:89
int mouse_capture_widget
Widgetindex of current mouse capture widget (e.g. dragged scrollbar). -1 if no widget has mouse captu...
Definition: window_gui.h:335
static void AddWindowToZOrdering(Window *w)
Adds a window to the z-ordering, according to its z-priority.
Definition: window.cpp:1377
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:3319
Drop down menu; Window numbers:
Definition: window_type.h:149
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:320
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1600
News window; Window numbers:
Definition: window_type.h:241
virtual void OnInvalidateData(int data=0, bool gui_scope=true)
Some data on this window has become invalid.
Definition: window_gui.h:743
void ProcessHighlightedInvalidations()
Process all invalidation of highlighted widgets.
Definition: window.cpp:3284
void UpdateViewportPosition(Window *w)
Update the viewport position being displayed.
Definition: viewport.cpp:1820
static bool MayBeShown(const Window *w)
Returns whether a window may be shown or not.
Definition: window.cpp:871
void IConsoleClose()
Close the in-game console.
#define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)
Iterate over all windows.
Definition: window_gui.h:889
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:629
Stuff related to the (main) toolbar.
Base class for a &#39;real&#39; widget.
Definition: widget_type.h:282
void ShowFirstError()
Show the first error of the queue.
Definition: error_gui.cpp:345
int width
Screen width of the viewport.
Definition: viewport_type.h:25