OpenTTD
window.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include <stdarg.h>
14 #include "company_func.h"
15 #include "gfx_func.h"
16 #include "console_func.h"
17 #include "console_gui.h"
18 #include "viewport_func.h"
19 #include "progress.h"
20 #include "blitter/factory.hpp"
21 #include "zoom_func.h"
22 #include "vehicle_base.h"
23 #include "window_func.h"
24 #include "tilehighlight_func.h"
25 #include "network/network.h"
26 #include "querystring_gui.h"
27 #include "widgets/dropdown_func.h"
28 #include "strings_func.h"
29 #include "settings_type.h"
30 #include "settings_func.h"
31 #include "ini_type.h"
32 #include "newgrf_debug.h"
33 #include "hotkeys.h"
34 #include "toolbar_gui.h"
35 #include "statusbar_gui.h"
36 #include "error.h"
37 #include "game/game.hpp"
38 #include "video/video_driver.hpp"
39 #include "framerate_type.h"
40 #include "network/network_func.h"
41 #include "guitimer_func.h"
42 #include "news_func.h"
43 
44 #include "safeguards.h"
45 
52 };
53 
55 static Window *_mouseover_last_w = NULL;
56 static Window *_last_scroll_window = NULL;
57 
62 
65 
66 /*
67  * Window that currently has focus. - The main purpose is to generate
68  * #FocusLost events, not to give next window in z-order focus when a
69  * window is closed.
70  */
71 Window *_focused_window;
72 
73 Point _cursorpos_drag_start;
74 
75 int _scrollbar_start_pos;
76 int _scrollbar_size;
77 byte _scroller_click_timeout = 0;
78 
81 
83 
89 
92 
94 WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
95  WindowClass window_class, WindowClass parent_class, uint32 flags,
96  const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys) :
97  default_pos(def_pos),
98  cls(window_class),
99  parent_cls(parent_class),
100  ini_key(ini_key),
101  flags(flags),
102  nwid_parts(nwid_parts),
103  nwid_length(nwid_length),
104  hotkeys(hotkeys),
105  pref_sticky(false),
106  pref_width(0),
107  pref_height(0),
108  default_width_trad(def_width_trad),
109  default_height_trad(def_height_trad)
110 {
111  if (_window_descs == NULL) _window_descs = new SmallVector<WindowDesc*, 16>();
112  *_window_descs->Append() = this;
113 }
114 
115 WindowDesc::~WindowDesc()
116 {
117  _window_descs->Erase(_window_descs->Find(this));
118 }
119 
126 {
127  return this->pref_width != 0 ? this->pref_width : ScaleGUITrad(this->default_width_trad);
128 }
129 
136 {
137  return this->pref_height != 0 ? this->pref_height : ScaleGUITrad(this->default_height_trad);
138 }
139 
144 {
145  IniFile *ini = new IniFile();
147  for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
148  if ((*it)->ini_key == NULL) continue;
149  IniLoadWindowSettings(ini, (*it)->ini_key, *it);
150  }
151  delete ini;
152 }
153 
157 static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b)
158 {
159  if ((*a)->ini_key != NULL && (*b)->ini_key != NULL) return strcmp((*a)->ini_key, (*b)->ini_key);
160  return ((*b)->ini_key != NULL ? 1 : 0) - ((*a)->ini_key != NULL ? 1 : 0);
161 }
162 
167 {
168  /* Sort the stuff to get a nice ini file on first write */
169  QSortT(_window_descs->Begin(), _window_descs->Length(), DescSorter);
170 
171  IniFile *ini = new IniFile();
172  ini->LoadFromDisk(_windows_file, NO_DIRECTORY);
173  for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
174  if ((*it)->ini_key == NULL) continue;
175  IniSaveWindowSettings(ini, (*it)->ini_key, *it);
176  }
177  ini->SaveToDisk(_windows_file);
178  delete ini;
179 }
180 
185 {
186  if (this->nested_root != NULL && this->nested_root->GetWidgetOfType(WWT_STICKYBOX) != NULL) {
187  if (this->window_desc->pref_sticky) this->flags |= WF_STICKY;
188  } else {
189  /* There is no stickybox; clear the preference in case someone tried to be funny */
190  this->window_desc->pref_sticky = false;
191  }
192 }
193 
203 int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const
204 {
205  const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
206  if (line_height < 0) line_height = wid->resize_y;
207  if (clickpos < (int)wid->pos_y + padding) return INT_MAX;
208  return (clickpos - (int)wid->pos_y - padding) / line_height;
209 }
210 
215 {
216  for (uint i = 0; i < this->nested_array_size; i++) {
217  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
218  if (nwid == NULL) continue;
219 
220  if (nwid->IsHighlighted()) {
221  nwid->SetHighlighted(TC_INVALID);
222  this->SetWidgetDirty(i);
223  }
224  }
225 
226  CLRBITS(this->flags, WF_HIGHLIGHTED);
227 }
228 
234 void Window::SetWidgetHighlight(byte widget_index, TextColour highlighted_colour)
235 {
236  assert(widget_index < this->nested_array_size);
237 
238  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
239  if (nwid == NULL) return;
240 
241  nwid->SetHighlighted(highlighted_colour);
242  this->SetWidgetDirty(widget_index);
243 
244  if (highlighted_colour != TC_INVALID) {
245  /* If we set a highlight, the window has a highlight */
246  this->flags |= WF_HIGHLIGHTED;
247  } else {
248  /* If we disable a highlight, check all widgets if anyone still has a highlight */
249  bool valid = false;
250  for (uint i = 0; i < this->nested_array_size; i++) {
251  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
252  if (nwid == NULL) continue;
253  if (!nwid->IsHighlighted()) continue;
254 
255  valid = true;
256  }
257  /* If nobody has a highlight, disable the flag on the window */
258  if (!valid) CLRBITS(this->flags, WF_HIGHLIGHTED);
259  }
260 }
261 
267 bool Window::IsWidgetHighlighted(byte widget_index) const
268 {
269  assert(widget_index < this->nested_array_size);
270 
271  const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
272  if (nwid == NULL) return false;
273 
274  return nwid->IsHighlighted();
275 }
276 
284 void Window::OnDropdownClose(Point pt, int widget, int index, bool instant_close)
285 {
286  if (widget < 0) return;
287 
288  if (instant_close) {
289  /* Send event for selected option if we're still
290  * on the parent button of the dropdown (behaviour of the dropdowns in the main toolbar). */
291  if (GetWidgetFromPos(this, pt.x, pt.y) == widget) {
292  this->OnDropdownSelect(widget, index);
293  }
294  }
295 
296  /* Raise the dropdown button */
297  NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget);
298  if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
299  nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE;
300  } else {
301  this->RaiseWidget(widget);
302  }
303  this->SetWidgetDirty(widget);
304 }
305 
311 const Scrollbar *Window::GetScrollbar(uint widnum) const
312 {
313  return this->GetWidget<NWidgetScrollbar>(widnum);
314 }
315 
322 {
323  return this->GetWidget<NWidgetScrollbar>(widnum);
324 }
325 
331 const QueryString *Window::GetQueryString(uint widnum) const
332 {
333  const SmallMap<int, QueryString*>::Pair *query = this->querystrings.Find(widnum);
334  return query != this->querystrings.End() ? query->second : NULL;
335 }
336 
343 {
344  SmallMap<int, QueryString*>::Pair *query = this->querystrings.Find(widnum);
345  return query != this->querystrings.End() ? query->second : NULL;
346 }
347 
352 /* virtual */ const char *Window::GetFocusedText() const
353 {
354  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
355  return this->GetQueryString(this->nested_focus->index)->GetText();
356  }
357 
358  return NULL;
359 }
360 
365 /* virtual */ const char *Window::GetCaret() const
366 {
367  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
368  return this->GetQueryString(this->nested_focus->index)->GetCaret();
369  }
370 
371  return NULL;
372 }
373 
379 /* virtual */ const char *Window::GetMarkedText(size_t *length) const
380 {
381  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
382  return this->GetQueryString(this->nested_focus->index)->GetMarkedText(length);
383  }
384 
385  return NULL;
386 }
387 
392 /* virtual */ Point Window::GetCaretPosition() const
393 {
394  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
395  return this->GetQueryString(this->nested_focus->index)->GetCaretPosition(this, this->nested_focus->index);
396  }
397 
398  Point pt = {0, 0};
399  return pt;
400 }
401 
408 /* virtual */ Rect Window::GetTextBoundingRect(const char *from, const char *to) const
409 {
410  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
411  return this->GetQueryString(this->nested_focus->index)->GetBoundingRect(this, this->nested_focus->index, from, to);
412  }
413 
414  Rect r = {0, 0, 0, 0};
415  return r;
416 }
417 
423 /* virtual */ const char *Window::GetTextCharacterAtPosition(const Point &pt) const
424 {
425  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
426  return this->GetQueryString(this->nested_focus->index)->GetCharAtPosition(this, this->nested_focus->index, pt);
427  }
428 
429  return NULL;
430 }
431 
437 {
438  if (_focused_window == w) return;
439 
440  /* Invalidate focused widget */
441  if (_focused_window != NULL) {
442  if (_focused_window->nested_focus != NULL) _focused_window->nested_focus->SetDirty(_focused_window);
443  }
444 
445  /* Remember which window was previously focused */
446  Window *old_focused = _focused_window;
447  _focused_window = w;
448 
449  /* So we can inform it that it lost focus */
450  if (old_focused != NULL) old_focused->OnFocusLost();
451  if (_focused_window != NULL) _focused_window->OnFocus();
452 }
453 
460 {
461  if (_focused_window == NULL) return false;
462 
463  /* The console does not have an edit box so a special case is needed. */
464  if (_focused_window->window_class == WC_CONSOLE) return true;
465 
466  return _focused_window->nested_focus != NULL && _focused_window->nested_focus->type == WWT_EDITBOX;
467 }
468 
473 {
474  if (this->nested_focus != NULL) {
475  if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
476 
477  /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
478  this->nested_focus->SetDirty(this);
479  this->nested_focus = NULL;
480  }
481 }
482 
488 bool Window::SetFocusedWidget(int widget_index)
489 {
490  /* Do nothing if widget_index is already focused, or if it wasn't a valid widget. */
491  if ((uint)widget_index >= this->nested_array_size) return false;
492 
493  assert(this->nested_array[widget_index] != NULL); // Setting focus to a non-existing widget is a bad idea.
494  if (this->nested_focus != NULL) {
495  if (this->GetWidget<NWidgetCore>(widget_index) == this->nested_focus) return false;
496 
497  /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
498  this->nested_focus->SetDirty(this);
499  if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
500  }
501  this->nested_focus = this->GetWidget<NWidgetCore>(widget_index);
502  return true;
503 }
504 
509 {
510  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
511 }
512 
520 void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...)
521 {
522  va_list wdg_list;
523 
524  va_start(wdg_list, widgets);
525 
526  while (widgets != WIDGET_LIST_END) {
527  SetWidgetDisabledState(widgets, disab_stat);
528  widgets = va_arg(wdg_list, int);
529  }
530 
531  va_end(wdg_list);
532 }
533 
539 void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...)
540 {
541  va_list wdg_list;
542 
543  va_start(wdg_list, widgets);
544 
545  while (widgets != WIDGET_LIST_END) {
546  SetWidgetLoweredState(widgets, lowered_stat);
547  widgets = va_arg(wdg_list, int);
548  }
549 
550  va_end(wdg_list);
551 }
552 
557 void Window::RaiseButtons(bool autoraise)
558 {
559  for (uint i = 0; i < this->nested_array_size; i++) {
560  if (this->nested_array[i] == NULL) continue;
561  WidgetType type = this->nested_array[i]->type;
562  if (((type & ~WWB_PUSHBUTTON) < WWT_LAST || type == NWID_PUSHBUTTON_DROPDOWN) &&
563  (!autoraise || (type & WWB_PUSHBUTTON) || type == WWT_EDITBOX) && this->IsWidgetLowered(i)) {
564  this->RaiseWidget(i);
565  this->SetWidgetDirty(i);
566  }
567  }
568 
569  /* Special widgets without widget index */
570  NWidgetCore *wid = this->nested_root != NULL ? (NWidgetCore*)this->nested_root->GetWidgetOfType(WWT_DEFSIZEBOX) : NULL;
571  if (wid != NULL) {
572  wid->SetLowered(false);
573  wid->SetDirty(this);
574  }
575 }
576 
581 void Window::SetWidgetDirty(byte widget_index) const
582 {
583  /* Sometimes this function is called before the window is even fully initialized */
584  if (this->nested_array == NULL) return;
585 
586  this->nested_array[widget_index]->SetDirty(this);
587 }
588 
595 {
596  if (hotkey < 0) return ES_NOT_HANDLED;
597 
598  NWidgetCore *nw = this->GetWidget<NWidgetCore>(hotkey);
599  if (nw == NULL || nw->IsDisabled()) return ES_NOT_HANDLED;
600 
601  if (nw->type == WWT_EDITBOX) {
602  if (this->IsShaded()) return ES_NOT_HANDLED;
603 
604  /* Focus editbox */
605  this->SetFocusedWidget(hotkey);
606  SetFocusedWindow(this);
607  } else {
608  /* Click button */
609  this->OnClick(Point(), hotkey, 1);
610  }
611  return ES_HANDLED;
612 }
613 
619 void Window::HandleButtonClick(byte widget)
620 {
621  this->LowerWidget(widget);
622  this->SetTimeout();
623  this->SetWidgetDirty(widget);
624 }
625 
626 static void StartWindowDrag(Window *w);
627 static void StartWindowSizing(Window *w, bool to_left);
628 
636 static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
637 {
638  NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
639  WidgetType widget_type = (nw != NULL) ? nw->type : WWT_EMPTY;
640 
641  bool focused_widget_changed = false;
642  /* If clicked on a window that previously did dot have focus */
643  if (_focused_window != w && // We already have focus, right?
644  (w->window_desc->flags & WDF_NO_FOCUS) == 0 && // Don't lose focus to toolbars
645  widget_type != WWT_CLOSEBOX) { // Don't change focused window if 'X' (close button) was clicked
646  focused_widget_changed = true;
647  SetFocusedWindow(w);
648  }
649 
650  if (nw == NULL) return; // exit if clicked outside of widgets
651 
652  /* don't allow any interaction if the button has been disabled */
653  if (nw->IsDisabled()) return;
654 
655  int widget_index = nw->index;
656 
657  /* Clicked on a widget that is not disabled.
658  * So unless the clicked widget is the caption bar, change focus to this widget.
659  * Exception: In the OSK we always want the editbox to stay focused. */
660  if (widget_type != WWT_CAPTION && w->window_class != WC_OSK) {
661  /* focused_widget_changed is 'now' only true if the window this widget
662  * is in gained focus. In that case it must remain true, also if the
663  * local widget focus did not change. As such it's the logical-or of
664  * both changed states.
665  *
666  * If this is not preserved, then the OSK window would be opened when
667  * a user has the edit box focused and then click on another window and
668  * then back again on the edit box (to type some text).
669  */
670  focused_widget_changed |= w->SetFocusedWidget(widget_index);
671  }
672 
673  /* Close any child drop down menus. If the button pressed was the drop down
674  * list's own button, then we should not process the click any further. */
675  if (HideDropDownMenu(w) == widget_index && widget_index >= 0) return;
676 
677  if ((widget_type & ~WWB_PUSHBUTTON) < WWT_LAST && (widget_type & WWB_PUSHBUTTON)) w->HandleButtonClick(widget_index);
678 
679  Point pt = { x, y };
680 
681  switch (widget_type) {
682  case NWID_VSCROLLBAR:
683  case NWID_HSCROLLBAR:
684  ScrollbarClickHandler(w, nw, x, y);
685  break;
686 
687  case WWT_EDITBOX: {
688  QueryString *query = w->GetQueryString(widget_index);
689  if (query != NULL) query->ClickEditBox(w, pt, widget_index, click_count, focused_widget_changed);
690  break;
691  }
692 
693  case WWT_CLOSEBOX: // 'X'
694  delete w;
695  return;
696 
697  case WWT_CAPTION: // 'Title bar'
698  StartWindowDrag(w);
699  return;
700 
701  case WWT_RESIZEBOX:
702  /* When the resize widget is on the left size of the window
703  * we assume that that button is used to resize to the left. */
704  StartWindowSizing(w, (int)nw->pos_x < (w->width / 2));
705  nw->SetDirty(w);
706  return;
707 
708  case WWT_DEFSIZEBOX: {
709  if (_ctrl_pressed) {
710  w->window_desc->pref_width = w->width;
711  w->window_desc->pref_height = w->height;
712  } else {
713  int16 def_width = max<int16>(min(w->window_desc->GetDefaultWidth(), _screen.width), w->nested_root->smallest_x);
714  int16 def_height = max<int16>(min(w->window_desc->GetDefaultHeight(), _screen.height - 50), w->nested_root->smallest_y);
715 
716  int dx = (w->resize.step_width == 0) ? 0 : def_width - w->width;
717  int dy = (w->resize.step_height == 0) ? 0 : def_height - w->height;
718  /* dx and dy has to go by step.. calculate it.
719  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
720  if (w->resize.step_width > 1) dx -= dx % (int)w->resize.step_width;
721  if (w->resize.step_height > 1) dy -= dy % (int)w->resize.step_height;
722  ResizeWindow(w, dx, dy, false);
723  }
724 
725  nw->SetLowered(true);
726  nw->SetDirty(w);
727  w->SetTimeout();
728  break;
729  }
730 
731  case WWT_DEBUGBOX:
733  break;
734 
735  case WWT_SHADEBOX:
736  nw->SetDirty(w);
737  w->SetShaded(!w->IsShaded());
738  return;
739 
740  case WWT_STICKYBOX:
741  w->flags ^= WF_STICKY;
742  nw->SetDirty(w);
743  if (_ctrl_pressed) w->window_desc->pref_sticky = (w->flags & WF_STICKY) != 0;
744  return;
745 
746  default:
747  break;
748  }
749 
750  /* Widget has no index, so the window is not interested in it. */
751  if (widget_index < 0) return;
752 
753  /* Check if the widget is highlighted; if so, disable highlight and dispatch an event to the GameScript */
754  if (w->IsWidgetHighlighted(widget_index)) {
755  w->SetWidgetHighlight(widget_index, TC_INVALID);
756  Game::NewEvent(new ScriptEventWindowWidgetClick((ScriptWindow::WindowClass)w->window_class, w->window_number, widget_index));
757  }
758 
759  w->OnClick(pt, widget_index, click_count);
760 }
761 
768 static void DispatchRightClickEvent(Window *w, int x, int y)
769 {
770  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
771  if (wid == NULL) return;
772 
773  /* No widget to handle, or the window is not interested in it. */
774  if (wid->index >= 0) {
775  Point pt = { x, y };
776  if (w->OnRightClick(pt, wid->index)) return;
777  }
778 
779  /* Right-click close is enabled and there is a closebox */
781  delete w;
782  } else if (_settings_client.gui.hover_delay_ms == 0 && wid->tool_tip != 0) {
783  GuiShowTooltips(w, wid->tool_tip, 0, NULL, TCC_RIGHT_CLICK);
784  }
785 }
786 
793 static void DispatchHoverEvent(Window *w, int x, int y)
794 {
795  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
796 
797  /* No widget to handle */
798  if (wid == NULL) return;
799 
800  /* Show the tooltip if there is any */
801  if (wid->tool_tip != 0) {
802  GuiShowTooltips(w, wid->tool_tip);
803  return;
804  }
805 
806  /* Widget has no index, so the window is not interested in it. */
807  if (wid->index < 0) return;
808 
809  Point pt = { x, y };
810  w->OnHover(pt, wid->index);
811 }
812 
820 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
821 {
822  if (nwid == NULL) return;
823 
824  /* Using wheel on caption/shade-box shades or unshades the window. */
825  if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
826  w->SetShaded(wheel < 0);
827  return;
828  }
829 
830  /* Wheeling a vertical scrollbar. */
831  if (nwid->type == NWID_VSCROLLBAR) {
832  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
833  if (sb->GetCount() > sb->GetCapacity()) {
834  sb->UpdatePosition(wheel);
835  w->SetDirty();
836  }
837  return;
838  }
839 
840  /* Scroll the widget attached to the scrollbar. */
841  Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : NULL);
842  if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
843  sb->UpdatePosition(wheel);
844  w->SetDirty();
845  }
846 }
847 
853 static bool MayBeShown(const Window *w)
854 {
855  /* If we're not modal, everything is okay. */
856  if (!HasModalProgress()) return true;
857 
858  switch (w->window_class) {
859  case WC_MAIN_WINDOW:
860  case WC_MODAL_PROGRESS:
862  return true;
863 
864  default:
865  return false;
866  }
867 }
868 
881 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
882 {
883  const Window *v;
885  if (MayBeShown(v) &&
886  right > v->left &&
887  bottom > v->top &&
888  left < v->left + v->width &&
889  top < v->top + v->height) {
890  /* v and rectangle intersect with each other */
891  int x;
892 
893  if (left < (x = v->left)) {
894  DrawOverlappedWindow(w, left, top, x, bottom);
895  DrawOverlappedWindow(w, x, top, right, bottom);
896  return;
897  }
898 
899  if (right > (x = v->left + v->width)) {
900  DrawOverlappedWindow(w, left, top, x, bottom);
901  DrawOverlappedWindow(w, x, top, right, bottom);
902  return;
903  }
904 
905  if (top < (x = v->top)) {
906  DrawOverlappedWindow(w, left, top, right, x);
907  DrawOverlappedWindow(w, left, x, right, bottom);
908  return;
909  }
910 
911  if (bottom > (x = v->top + v->height)) {
912  DrawOverlappedWindow(w, left, top, right, x);
913  DrawOverlappedWindow(w, left, x, right, bottom);
914  return;
915  }
916 
917  return;
918  }
919  }
920 
921  /* Setup blitter, and dispatch a repaint event to window *wz */
922  DrawPixelInfo *dp = _cur_dpi;
923  dp->width = right - left;
924  dp->height = bottom - top;
925  dp->left = left - w->left;
926  dp->top = top - w->top;
927  dp->pitch = _screen.pitch;
928  dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
929  dp->zoom = ZOOM_LVL_NORMAL;
930  w->OnPaint();
931 }
932 
941 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
942 {
943  Window *w;
944 
945  DrawPixelInfo *old_dpi = _cur_dpi;
946  DrawPixelInfo bk;
947  _cur_dpi = &bk;
948 
949  FOR_ALL_WINDOWS_FROM_BACK(w) {
950  if (MayBeShown(w) &&
951  right > w->left &&
952  bottom > w->top &&
953  left < w->left + w->width &&
954  top < w->top + w->height) {
955  /* Window w intersects with the rectangle => needs repaint */
956  DrawOverlappedWindow(w, max(left, w->left), max(top, w->top), min(right, w->left + w->width), min(bottom, w->top + w->height));
957  }
958  }
959  _cur_dpi = old_dpi;
960 }
961 
966 void Window::SetDirty() const
967 {
968  SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
969 }
970 
977 void Window::ReInit(int rx, int ry)
978 {
979  this->SetDirty(); // Mark whole current window as dirty.
980 
981  /* Save current size. */
982  int window_width = this->width;
983  int window_height = this->height;
984 
985  this->OnInit();
986  /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
987  this->nested_root->SetupSmallestSize(this, false);
988  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
989  this->width = this->nested_root->smallest_x;
990  this->height = this->nested_root->smallest_y;
991  this->resize.step_width = this->nested_root->resize_x;
992  this->resize.step_height = this->nested_root->resize_y;
993 
994  /* Resize as close to the original size + requested resize as possible. */
995  window_width = max(window_width + rx, this->width);
996  window_height = max(window_height + ry, this->height);
997  int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
998  int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
999  /* dx and dy has to go by step.. calculate it.
1000  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
1001  if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
1002  if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
1003 
1004  ResizeWindow(this, dx, dy);
1005  /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
1006 }
1007 
1013 void Window::SetShaded(bool make_shaded)
1014 {
1015  if (this->shade_select == NULL) return;
1016 
1017  int desired = make_shaded ? SZSP_HORIZONTAL : 0;
1018  if (this->shade_select->shown_plane != desired) {
1019  if (make_shaded) {
1020  if (this->nested_focus != NULL) this->UnfocusFocusedWidget();
1021  this->unshaded_size.width = this->width;
1022  this->unshaded_size.height = this->height;
1023  this->shade_select->SetDisplayedPlane(desired);
1024  this->ReInit(0, -this->height);
1025  } else {
1026  this->shade_select->SetDisplayedPlane(desired);
1027  int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
1028  int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
1029  this->ReInit(dx, dy);
1030  }
1031  }
1032 }
1033 
1041 {
1042  Window *v;
1043  FOR_ALL_WINDOWS_FROM_BACK(v) {
1044  if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
1045  }
1046 
1047  return NULL;
1048 }
1049 
1055 {
1056  Window *child = FindChildWindow(this, wc);
1057  while (child != NULL) {
1058  delete child;
1059  child = FindChildWindow(this, wc);
1060  }
1061 }
1062 
1067 {
1068  if (_thd.window_class == this->window_class &&
1069  _thd.window_number == this->window_number) {
1071  }
1072 
1073  /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
1074  if (_mouseover_last_w == this) _mouseover_last_w = NULL;
1075 
1076  /* We can't scroll the window when it's closed. */
1077  if (_last_scroll_window == this) _last_scroll_window = NULL;
1078 
1079  /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
1080  if (_focused_window == this) {
1081  this->OnFocusLost();
1082  _focused_window = NULL;
1083  }
1084 
1085  this->DeleteChildWindows();
1086 
1087  if (this->viewport != NULL) DeleteWindowViewport(this);
1088 
1089  this->SetDirty();
1090 
1091  free(this->nested_array); // Contents is released through deletion of #nested_root.
1092  delete this->nested_root;
1093 
1094  /*
1095  * Make fairly sure that this is written, and not "optimized" away.
1096  * The delete operator is overwritten to not delete it; the deletion
1097  * happens at a later moment in time after the window has been
1098  * removed from the list of windows to prevent issues with items
1099  * being removed during the iteration as not one but more windows
1100  * may be removed by a single call to ~Window by means of the
1101  * DeleteChildWindows function.
1102  */
1103  const_cast<volatile WindowClass &>(this->window_class) = WC_INVALID;
1104 }
1105 
1113 {
1114  Window *w;
1115  FOR_ALL_WINDOWS_FROM_BACK(w) {
1116  if (w->window_class == cls && w->window_number == number) return w;
1117  }
1118 
1119  return NULL;
1120 }
1121 
1129 {
1130  Window *w;
1131  FOR_ALL_WINDOWS_FROM_BACK(w) {
1132  if (w->window_class == cls) return w;
1133  }
1134 
1135  return NULL;
1136 }
1137 
1145 {
1146  Window *w = FindWindowById(cls, number);
1147  if (force || w == NULL ||
1148  (w->flags & WF_STICKY) == 0) {
1149  delete w;
1150  }
1151 }
1152 
1158 {
1159  Window *w;
1160 
1161 restart_search:
1162  /* When we find the window to delete, we need to restart the search
1163  * as deleting this window could cascade in deleting (many) others
1164  * anywhere in the z-array */
1165  FOR_ALL_WINDOWS_FROM_BACK(w) {
1166  if (w->window_class == cls) {
1167  delete w;
1168  goto restart_search;
1169  }
1170  }
1171 }
1172 
1180 {
1181  Window *w;
1182 
1183 restart_search:
1184  /* When we find the window to delete, we need to restart the search
1185  * as deleting this window could cascade in deleting (many) others
1186  * anywhere in the z-array */
1187  FOR_ALL_WINDOWS_FROM_BACK(w) {
1188  if (w->owner == id) {
1189  delete w;
1190  goto restart_search;
1191  }
1192  }
1193 
1194  /* Also delete the company specific windows that don't have a company-colour. */
1196 }
1197 
1205 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1206 {
1207  Window *w;
1208  FOR_ALL_WINDOWS_FROM_BACK(w) {
1209  if (w->owner != old_owner) continue;
1210 
1211  switch (w->window_class) {
1212  case WC_COMPANY_COLOUR:
1213  case WC_FINANCES:
1214  case WC_STATION_LIST:
1215  case WC_TRAINS_LIST:
1216  case WC_ROADVEH_LIST:
1217  case WC_SHIPS_LIST:
1218  case WC_AIRCRAFT_LIST:
1219  case WC_BUY_COMPANY:
1220  case WC_COMPANY:
1222  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().
1223  continue;
1224 
1225  default:
1226  w->owner = new_owner;
1227  break;
1228  }
1229  }
1230 }
1231 
1232 static void BringWindowToFront(Window *w);
1233 
1242 {
1243  Window *w = FindWindowById(cls, number);
1244 
1245  if (w != NULL) {
1246  if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1247 
1248  w->SetWhiteBorder();
1249  BringWindowToFront(w);
1250  w->SetDirty();
1251  }
1252 
1253  return w;
1254 }
1255 
1256 static inline bool IsVitalWindow(const Window *w)
1257 {
1258  switch (w->window_class) {
1259  case WC_MAIN_TOOLBAR:
1260  case WC_STATUS_BAR:
1261  case WC_NEWS_WINDOW:
1262  case WC_SEND_NETWORK_MSG:
1263  return true;
1264 
1265  default:
1266  return false;
1267  }
1268 }
1269 
1279 {
1280  assert(wc != WC_INVALID);
1281 
1282  uint z_priority = 0;
1283 
1284  switch (wc) {
1285  case WC_ENDSCREEN:
1286  ++z_priority;
1287  FALLTHROUGH;
1288 
1289  case WC_HIGHSCORE:
1290  ++z_priority;
1291  FALLTHROUGH;
1292 
1293  case WC_TOOLTIPS:
1294  ++z_priority;
1295  FALLTHROUGH;
1296 
1297  case WC_DROPDOWN_MENU:
1298  ++z_priority;
1299  FALLTHROUGH;
1300 
1301  case WC_MAIN_TOOLBAR:
1302  case WC_STATUS_BAR:
1303  ++z_priority;
1304  FALLTHROUGH;
1305 
1306  case WC_OSK:
1307  ++z_priority;
1308  FALLTHROUGH;
1309 
1310  case WC_QUERY_STRING:
1311  case WC_SEND_NETWORK_MSG:
1312  ++z_priority;
1313  FALLTHROUGH;
1314 
1315  case WC_ERRMSG:
1317  case WC_MODAL_PROGRESS:
1319  case WC_SAVE_PRESET:
1320  ++z_priority;
1321  FALLTHROUGH;
1322 
1323  case WC_GENERATE_LANDSCAPE:
1324  case WC_SAVELOAD:
1325  case WC_GAME_OPTIONS:
1326  case WC_CUSTOM_CURRENCY:
1327  case WC_NETWORK_WINDOW:
1328  case WC_GRF_PARAMETERS:
1329  case WC_AI_LIST:
1330  case WC_AI_SETTINGS:
1331  case WC_TEXTFILE:
1332  ++z_priority;
1333  FALLTHROUGH;
1334 
1335  case WC_CONSOLE:
1336  ++z_priority;
1337  FALLTHROUGH;
1338 
1339  case WC_NEWS_WINDOW:
1340  ++z_priority;
1341  FALLTHROUGH;
1342 
1343  default:
1344  ++z_priority;
1345  FALLTHROUGH;
1346 
1347  case WC_MAIN_WINDOW:
1348  return z_priority;
1349  }
1350 }
1351 
1357 {
1358  assert(w->z_front == NULL && w->z_back == NULL);
1359 
1360  if (_z_front_window == NULL) {
1361  /* It's the only window. */
1362  _z_front_window = _z_back_window = w;
1363  w->z_front = w->z_back = NULL;
1364  } else {
1365  /* Search down the z-ordering for its location. */
1366  Window *v = _z_front_window;
1367  uint last_z_priority = UINT_MAX;
1368  while (v != NULL && (v->window_class == WC_INVALID || GetWindowZPriority(v->window_class) > GetWindowZPriority(w->window_class))) {
1369  if (v->window_class != WC_INVALID) {
1370  /* Sanity check z-ordering, while we're at it. */
1371  assert(last_z_priority >= GetWindowZPriority(v->window_class));
1372  last_z_priority = GetWindowZPriority(v->window_class);
1373  }
1374 
1375  v = v->z_back;
1376  }
1377 
1378  if (v == NULL) {
1379  /* It's the new back window. */
1380  w->z_front = _z_back_window;
1381  w->z_back = NULL;
1382  _z_back_window->z_back = w;
1383  _z_back_window = w;
1384  } else if (v == _z_front_window) {
1385  /* It's the new front window. */
1386  w->z_front = NULL;
1387  w->z_back = _z_front_window;
1388  _z_front_window->z_front = w;
1389  _z_front_window = w;
1390  } else {
1391  /* It's somewhere else in the z-ordering. */
1392  w->z_front = v->z_front;
1393  w->z_back = v;
1394  v->z_front->z_back = w;
1395  v->z_front = w;
1396  }
1397  }
1398 }
1399 
1400 
1406 {
1407  if (w->z_front == NULL) {
1408  assert(_z_front_window == w);
1409  _z_front_window = w->z_back;
1410  } else {
1411  w->z_front->z_back = w->z_back;
1412  }
1413 
1414  if (w->z_back == NULL) {
1415  assert(_z_back_window == w);
1416  _z_back_window = w->z_front;
1417  } else {
1418  w->z_back->z_front = w->z_front;
1419  }
1420 
1421  w->z_front = w->z_back = NULL;
1422 }
1423 
1430 {
1433 
1434  w->SetDirty();
1435 }
1436 
1445 {
1446  /* Set up window properties; some of them are needed to set up smallest size below */
1447  this->window_class = this->window_desc->cls;
1448  this->SetWhiteBorder();
1449  if (this->window_desc->default_pos == WDP_CENTER) this->flags |= WF_CENTERED;
1450  this->owner = INVALID_OWNER;
1451  this->nested_focus = NULL;
1452  this->window_number = window_number;
1453 
1454  this->OnInit();
1455  /* Initialize nested widget tree. */
1456  if (this->nested_array == NULL) {
1457  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1458  this->nested_root->SetupSmallestSize(this, true);
1459  } else {
1460  this->nested_root->SetupSmallestSize(this, false);
1461  }
1462  /* Initialize to smallest size. */
1463  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1464 
1465  /* Further set up window properties,
1466  * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1467  this->resize.step_width = this->nested_root->resize_x;
1468  this->resize.step_height = this->nested_root->resize_y;
1469 
1470  /* Give focus to the opened window unless a text box
1471  * of focused window has focus (so we don't interrupt typing). But if the new
1472  * window has a text box, then take focus anyway. */
1473  if (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != NULL) SetFocusedWindow(this);
1474 
1475  /* Insert the window into the correct location in the z-ordering. */
1476  AddWindowToZOrdering(this);
1477 }
1478 
1486 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1487 {
1488  this->left = x;
1489  this->top = y;
1490  this->width = sm_width;
1491  this->height = sm_height;
1492 }
1493 
1504 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
1505 {
1506  def_width = max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1507  def_height = max(def_height, this->height);
1508  /* Try to make windows smaller when our window is too small.
1509  * w->(width|height) is normally the same as min_(width|height),
1510  * but this way the GUIs can be made a little more dynamic;
1511  * one can use the same spec for multiple windows and those
1512  * can then determine the real minimum size of the window. */
1513  if (this->width != def_width || this->height != def_height) {
1514  /* Think about the overlapping toolbars when determining the minimum window size */
1515  int free_height = _screen.height;
1516  const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1517  if (wt != NULL) free_height -= wt->height;
1519  if (wt != NULL) free_height -= wt->height;
1520 
1521  int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
1522  int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
1523 
1524  /* X and Y has to go by step.. calculate it.
1525  * The cast to int is necessary else x/y are implicitly casted to
1526  * unsigned int, which won't work. */
1527  if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1528  if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1529 
1530  ResizeWindow(this, enlarge_x, enlarge_y);
1531  /* ResizeWindow() calls this->OnResize(). */
1532  } else {
1533  /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1534  this->OnResize();
1535  }
1536 
1537  int nx = this->left;
1538  int ny = this->top;
1539 
1540  if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1541 
1542  const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1543  ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
1544  nx = max(nx, 0);
1545 
1546  if (this->viewport != NULL) {
1547  this->viewport->left += nx - this->left;
1548  this->viewport->top += ny - this->top;
1549  }
1550  this->left = nx;
1551  this->top = ny;
1552 
1553  this->SetDirty();
1554 }
1555 
1568 static bool IsGoodAutoPlace1(int left, int top, int width, int height, int toolbar_y, Point &pos)
1569 {
1570  int right = width + left;
1571  int bottom = height + top;
1572 
1573  if (left < 0 || top < toolbar_y || right > _screen.width || bottom > _screen.height) return false;
1574 
1575  /* Make sure it is not obscured by any window. */
1576  const Window *w;
1577  FOR_ALL_WINDOWS_FROM_BACK(w) {
1578  if (w->window_class == WC_MAIN_WINDOW) continue;
1579 
1580  if (right > w->left &&
1581  w->left + w->width > left &&
1582  bottom > w->top &&
1583  w->top + w->height > top) {
1584  return false;
1585  }
1586  }
1587 
1588  pos.x = left;
1589  pos.y = top;
1590  return true;
1591 }
1592 
1605 static bool IsGoodAutoPlace2(int left, int top, int width, int height, int toolbar_y, Point &pos)
1606 {
1607  bool rtl = _current_text_dir == TD_RTL;
1608 
1609  /* Left part of the rectangle may be at most 1/4 off-screen,
1610  * right part of the rectangle may be at most 1/2 off-screen
1611  */
1612  if (rtl) {
1613  if (left < -(width >> 1) || left > _screen.width - (width >> 2)) return false;
1614  } else {
1615  if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1616  }
1617 
1618  /* Bottom part of the rectangle may be at most 1/4 off-screen */
1619  if (top < toolbar_y || top > _screen.height - (height >> 2)) return false;
1620 
1621  /* Make sure it is not obscured by any window. */
1622  const Window *w;
1623  FOR_ALL_WINDOWS_FROM_BACK(w) {
1624  if (w->window_class == WC_MAIN_WINDOW) continue;
1625 
1626  if (left + width > w->left &&
1627  w->left + w->width > left &&
1628  top + height > w->top &&
1629  w->top + w->height > top) {
1630  return false;
1631  }
1632  }
1633 
1634  pos.x = left;
1635  pos.y = top;
1636  return true;
1637 }
1638 
1645 static Point GetAutoPlacePosition(int width, int height)
1646 {
1647  Point pt;
1648 
1649  bool rtl = _current_text_dir == TD_RTL;
1650 
1651  /* First attempt, try top-left of the screen */
1652  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1653  const int toolbar_y = main_toolbar != NULL ? main_toolbar->height : 0;
1654  if (IsGoodAutoPlace1(rtl ? _screen.width - width : 0, toolbar_y, width, height, toolbar_y, pt)) return pt;
1655 
1656  /* Second attempt, try around all existing windows.
1657  * The new window must be entirely on-screen, and not overlap with an existing window.
1658  * Eight starting points are tried, two at each corner.
1659  */
1660  const Window *w;
1661  FOR_ALL_WINDOWS_FROM_BACK(w) {
1662  if (w->window_class == WC_MAIN_WINDOW) continue;
1663 
1664  if (IsGoodAutoPlace1(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1665  if (IsGoodAutoPlace1(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1666  if (IsGoodAutoPlace1(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1667  if (IsGoodAutoPlace1(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1668  if (IsGoodAutoPlace1(w->left + w->width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1669  if (IsGoodAutoPlace1(w->left - width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1670  if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1671  if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height, width, height, toolbar_y, pt)) return pt;
1672  }
1673 
1674  /* Third attempt, try around all existing windows.
1675  * The new window may be partly off-screen, and must not overlap with an existing window.
1676  * Only four starting points are tried.
1677  */
1678  FOR_ALL_WINDOWS_FROM_BACK(w) {
1679  if (w->window_class == WC_MAIN_WINDOW) continue;
1680 
1681  if (IsGoodAutoPlace2(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1682  if (IsGoodAutoPlace2(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1683  if (IsGoodAutoPlace2(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1684  if (IsGoodAutoPlace2(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1685  }
1686 
1687  /* Fourth and final attempt, put window at diagonal starting from (0, toolbar_y), try multiples
1688  * of the closebox
1689  */
1690  int left = rtl ? _screen.width - width : 0, top = toolbar_y;
1691  int offset_x = rtl ? -(int)NWidgetLeaf::closebox_dimension.width : (int)NWidgetLeaf::closebox_dimension.width;
1693 
1694 restart:
1695  FOR_ALL_WINDOWS_FROM_BACK(w) {
1696  if (w->left == left && w->top == top) {
1697  left += offset_x;
1698  top += offset_y;
1699  goto restart;
1700  }
1701  }
1702 
1703  pt.x = left;
1704  pt.y = top;
1705  return pt;
1706 }
1707 
1715 {
1716  const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1717  assert(w != NULL);
1718  Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1719  return pt;
1720 }
1721 
1739 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1740 {
1741  Point pt;
1742  const Window *w;
1743 
1744  int16 default_width = max(desc->GetDefaultWidth(), sm_width);
1745  int16 default_height = max(desc->GetDefaultHeight(), sm_height);
1746 
1747  if (desc->parent_cls != WC_NONE && (w = FindWindowById(desc->parent_cls, window_number)) != NULL) {
1748  bool rtl = _current_text_dir == TD_RTL;
1749  if (desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) {
1750  pt.x = w->left + (rtl ? w->width - default_width : 0);
1751  pt.y = w->top + w->height;
1752  return pt;
1753  } else {
1754  /* Position child window with offset of closebox, but make sure that either closebox or resizebox is visible
1755  * - Y position: closebox of parent + closebox of child + statusbar
1756  * - X position: closebox on left/right, resizebox on right/left (depending on ltr/rtl)
1757  */
1759  if (w->top + 3 * indent_y < _screen.height) {
1760  pt.y = w->top + indent_y;
1761  int indent_close = NWidgetLeaf::closebox_dimension.width;
1762  int indent_resize = NWidgetLeaf::resizebox_dimension.width;
1763  if (_current_text_dir == TD_RTL) {
1764  pt.x = max(w->left + w->width - default_width - indent_close, 0);
1765  if (pt.x + default_width >= indent_close && pt.x + indent_resize <= _screen.width) return pt;
1766  } else {
1767  pt.x = min(w->left + indent_close, _screen.width - default_width);
1768  if (pt.x + default_width >= indent_resize && pt.x + indent_close <= _screen.width) return pt;
1769  }
1770  }
1771  }
1772  }
1773 
1774  switch (desc->default_pos) {
1775  case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1776  return GetToolbarAlignedWindowPosition(default_width);
1777 
1778  case WDP_AUTO: // Find a good automatic position for the window
1779  return GetAutoPlacePosition(default_width, default_height);
1780 
1781  case WDP_CENTER: // Centre the window horizontally
1782  pt.x = (_screen.width - default_width) / 2;
1783  pt.y = (_screen.height - default_height) / 2;
1784  break;
1785 
1786  case WDP_MANUAL:
1787  pt.x = 0;
1788  pt.y = 0;
1789  break;
1790 
1791  default:
1792  NOT_REACHED();
1793  }
1794 
1795  return pt;
1796 }
1797 
1798 /* virtual */ Point Window::OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1799 {
1800  return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1801 }
1802 
1810 void Window::CreateNestedTree(bool fill_nested)
1811 {
1812  int biggest_index = -1;
1813  this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_parts, this->window_desc->nwid_length, &biggest_index, &this->shade_select);
1814  this->nested_array_size = (uint)(biggest_index + 1);
1815 
1816  if (fill_nested) {
1817  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1818  this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
1819  }
1820 }
1821 
1827 {
1828  this->InitializeData(window_number);
1829  this->ApplyDefaults();
1830  Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1831  this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1832  this->FindWindowPlacementAndResize(this->window_desc->GetDefaultWidth(), this->window_desc->GetDefaultHeight());
1833 }
1834 
1840 {
1841  this->CreateNestedTree(false);
1842  this->FinishInitNested(window_number);
1843 }
1844 
1849 Window::Window(WindowDesc *desc) : window_desc(desc), mouse_capture_widget(-1)
1850 {
1851 }
1852 
1860 Window *FindWindowFromPt(int x, int y)
1861 {
1862  Window *w;
1863  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1864  if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1865  return w;
1866  }
1867  }
1868 
1869  return NULL;
1870 }
1871 
1876 {
1877  IConsoleClose();
1878 
1879  _z_back_window = NULL;
1880  _z_front_window = NULL;
1881  _focused_window = NULL;
1882  _mouseover_last_w = NULL;
1883  _last_scroll_window = NULL;
1884  _scrolling_viewport = false;
1885  _mouse_hovering = false;
1886 
1887  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1888  NWidgetScrollbar::InvalidateDimensionCache();
1889 
1890  ShowFirstError();
1891 }
1892 
1897 {
1899 
1900  Window *w;
1901  FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
1902 
1903  for (w = _z_front_window; w != NULL; /* nothing */) {
1904  Window *to_del = w;
1905  w = w->z_back;
1906  free(to_del);
1907  }
1908 
1909  _z_front_window = NULL;
1910  _z_back_window = NULL;
1911 }
1912 
1917 {
1919  InitWindowSystem();
1920  _thd.Reset();
1921 }
1922 
1923 static void DecreaseWindowCounters()
1924 {
1925  if (_scroller_click_timeout != 0) _scroller_click_timeout--;
1926 
1927  Window *w;
1928  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1929  if (_scroller_click_timeout == 0) {
1930  /* Unclick scrollbar buttons if they are pressed. */
1931  for (uint i = 0; i < w->nested_array_size; i++) {
1932  NWidgetBase *nwid = w->nested_array[i];
1933  if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
1934  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1937  w->mouse_capture_widget = -1;
1938  sb->SetDirty(w);
1939  }
1940  }
1941  }
1942  }
1943 
1944  /* Handle editboxes */
1945  for (SmallMap<int, QueryString*>::Pair *it = w->querystrings.Begin(); it != w->querystrings.End(); ++it) {
1946  it->second->HandleEditBox(w, it->first);
1947  }
1948 
1949  w->OnMouseLoop();
1950  }
1951 
1952  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1953  if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) {
1954  CLRBITS(w->flags, WF_TIMEOUT);
1955 
1956  w->OnTimeout();
1957  w->RaiseButtons(true);
1958  }
1959  }
1960 }
1961 
1962 static void HandlePlacePresize()
1963 {
1964  if (_special_mouse_mode != WSM_PRESIZE) return;
1965 
1966  Window *w = _thd.GetCallbackWnd();
1967  if (w == NULL) return;
1968 
1969  Point pt = GetTileBelowCursor();
1970  if (pt.x == -1) {
1971  _thd.selend.x = -1;
1972  return;
1973  }
1974 
1975  w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1976 }
1977 
1983 {
1985 
1986  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
1987 
1988  Window *w = _thd.GetCallbackWnd();
1989  if (w != NULL) {
1990  /* Send an event in client coordinates. */
1991  Point pt;
1992  pt.x = _cursor.pos.x - w->left;
1993  pt.y = _cursor.pos.y - w->top;
1994  if (_left_button_down) {
1995  w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
1996  } else {
1997  w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
1998  }
1999  }
2000 
2001  if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
2002  return ES_HANDLED;
2003 }
2004 
2006 static void HandleMouseOver()
2007 {
2008  Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2009 
2010  /* We changed window, put an OnMouseOver event to the last window */
2011  if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
2012  /* Reset mouse-over coordinates of previous window */
2013  Point pt = { -1, -1 };
2014  _mouseover_last_w->OnMouseOver(pt, 0);
2015  }
2016 
2017  /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
2018  _mouseover_last_w = w;
2019 
2020  if (w != NULL) {
2021  /* send an event in client coordinates. */
2022  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2023  const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
2024  if (widget != NULL) w->OnMouseOver(pt, widget->index);
2025  }
2026 }
2027 
2029 static const int MIN_VISIBLE_TITLE_BAR = 13;
2030 
2035 };
2036 
2047 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
2048 {
2049  if (v == NULL) return;
2050 
2051  int v_bottom = v->top + v->height;
2052  int v_right = v->left + v->width;
2053  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.
2054 
2055  if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
2056  if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
2057 
2058  /* Vertically, the rectangle is hidden behind v. */
2059  if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
2060  if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
2061  return;
2062  }
2063  if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
2064  if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
2065  return;
2066  }
2067 
2068  /* Horizontally also hidden, force movement to a safe area. */
2069  if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
2070  *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
2071  } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
2072  *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2073  } else {
2074  *ny = safe_y;
2075  }
2076 }
2077 
2085 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2086 {
2087  /* Search for the title bar rectangle. */
2088  Rect caption_rect;
2089  const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2090  if (caption != NULL) {
2091  caption_rect.left = caption->pos_x;
2092  caption_rect.right = caption->pos_x + caption->current_x;
2093  caption_rect.top = caption->pos_y;
2094  caption_rect.bottom = caption->pos_y + caption->current_y;
2095 
2096  /* Make sure the window doesn't leave the screen */
2097  nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2098  ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2099 
2100  /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2101  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2102  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2103  }
2104 
2105  if (w->viewport != NULL) {
2106  w->viewport->left += nx - w->left;
2107  w->viewport->top += ny - w->top;
2108  }
2109 
2110  w->left = nx;
2111  w->top = ny;
2112 }
2113 
2124 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
2125 {
2126  if (delta_x != 0 || delta_y != 0) {
2127  if (clamp_to_screen) {
2128  /* Determine the new right/bottom position. If that is outside of the bounds of
2129  * the resolution clamp it in such a manner that it stays within the bounds. */
2130  int new_right = w->left + w->width + delta_x;
2131  int new_bottom = w->top + w->height + delta_y;
2132  if (new_right >= (int)_cur_resolution.width) delta_x -= Ceil(new_right - _cur_resolution.width, max(1U, w->nested_root->resize_x));
2133  if (new_bottom >= (int)_cur_resolution.height) delta_y -= Ceil(new_bottom - _cur_resolution.height, max(1U, w->nested_root->resize_y));
2134  }
2135 
2136  w->SetDirty();
2137 
2138  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);
2139  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);
2140  assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2141  assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2142 
2144  w->width = w->nested_root->current_x;
2145  w->height = w->nested_root->current_y;
2146  }
2147 
2148  EnsureVisibleCaption(w, w->left, w->top);
2149 
2150  /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
2151  w->OnResize();
2152  w->SetDirty();
2153 }
2154 
2161 {
2163  return (w == NULL) ? 0 : w->top + w->height;
2164 }
2165 
2172 {
2174  return (w == NULL) ? _screen.height : w->top;
2175 }
2176 
2177 static bool _dragging_window;
2178 
2184 {
2185  /* Get out immediately if no window is being dragged at all. */
2186  if (!_dragging_window) return ES_NOT_HANDLED;
2187 
2188  /* If button still down, but cursor hasn't moved, there is nothing to do. */
2189  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2190 
2191  /* Otherwise find the window... */
2192  Window *w;
2193  FOR_ALL_WINDOWS_FROM_BACK(w) {
2194  if (w->flags & WF_DRAGGING) {
2195  /* Stop the dragging if the left mouse button was released */
2196  if (!_left_button_down) {
2197  w->flags &= ~WF_DRAGGING;
2198  break;
2199  }
2200 
2201  w->SetDirty();
2202 
2203  int x = _cursor.pos.x + _drag_delta.x;
2204  int y = _cursor.pos.y + _drag_delta.y;
2205  int nx = x;
2206  int ny = y;
2207 
2209  const Window *v;
2210 
2213  int delta;
2214 
2215  FOR_ALL_WINDOWS_FROM_BACK(v) {
2216  if (v == w) continue; // Don't snap at yourself
2217 
2218  if (y + w->height > v->top && y < v->top + v->height) {
2219  /* Your left border <-> other right border */
2220  delta = abs(v->left + v->width - x);
2221  if (delta <= hsnap) {
2222  nx = v->left + v->width;
2223  hsnap = delta;
2224  }
2225 
2226  /* Your right border <-> other left border */
2227  delta = abs(v->left - x - w->width);
2228  if (delta <= hsnap) {
2229  nx = v->left - w->width;
2230  hsnap = delta;
2231  }
2232  }
2233 
2234  if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2235  /* Your left border <-> other left border */
2236  delta = abs(v->left - x);
2237  if (delta <= hsnap) {
2238  nx = v->left;
2239  hsnap = delta;
2240  }
2241 
2242  /* Your right border <-> other right border */
2243  delta = abs(v->left + v->width - x - w->width);
2244  if (delta <= hsnap) {
2245  nx = v->left + v->width - w->width;
2246  hsnap = delta;
2247  }
2248  }
2249 
2250  if (x + w->width > v->left && x < v->left + v->width) {
2251  /* Your top border <-> other bottom border */
2252  delta = abs(v->top + v->height - y);
2253  if (delta <= vsnap) {
2254  ny = v->top + v->height;
2255  vsnap = delta;
2256  }
2257 
2258  /* Your bottom border <-> other top border */
2259  delta = abs(v->top - y - w->height);
2260  if (delta <= vsnap) {
2261  ny = v->top - w->height;
2262  vsnap = delta;
2263  }
2264  }
2265 
2266  if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2267  /* Your top border <-> other top border */
2268  delta = abs(v->top - y);
2269  if (delta <= vsnap) {
2270  ny = v->top;
2271  vsnap = delta;
2272  }
2273 
2274  /* Your bottom border <-> other bottom border */
2275  delta = abs(v->top + v->height - y - w->height);
2276  if (delta <= vsnap) {
2277  ny = v->top + v->height - w->height;
2278  vsnap = delta;
2279  }
2280  }
2281  }
2282  }
2283 
2284  EnsureVisibleCaption(w, nx, ny);
2285 
2286  w->SetDirty();
2287  return ES_HANDLED;
2288  } else if (w->flags & WF_SIZING) {
2289  /* Stop the sizing if the left mouse button was released */
2290  if (!_left_button_down) {
2291  w->flags &= ~WF_SIZING;
2292  w->SetDirty();
2293  break;
2294  }
2295 
2296  /* Compute difference in pixels between cursor position and reference point in the window.
2297  * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2298  */
2299  int x, y = _cursor.pos.y - _drag_delta.y;
2300  if (w->flags & WF_SIZING_LEFT) {
2301  x = _drag_delta.x - _cursor.pos.x;
2302  } else {
2303  x = _cursor.pos.x - _drag_delta.x;
2304  }
2305 
2306  /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2307  if (w->resize.step_width == 0) x = 0;
2308  if (w->resize.step_height == 0) y = 0;
2309 
2310  /* Check the resize button won't go past the bottom of the screen */
2311  if (w->top + w->height + y > _screen.height) {
2312  y = _screen.height - w->height - w->top;
2313  }
2314 
2315  /* X and Y has to go by step.. calculate it.
2316  * The cast to int is necessary else x/y are implicitly casted to
2317  * unsigned int, which won't work. */
2318  if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2319  if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2320 
2321  /* Check that we don't go below the minimum set size */
2322  if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2323  x = w->nested_root->smallest_x - w->width;
2324  }
2325  if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2326  y = w->nested_root->smallest_y - w->height;
2327  }
2328 
2329  /* Window already on size */
2330  if (x == 0 && y == 0) return ES_HANDLED;
2331 
2332  /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2333  _drag_delta.y += y;
2334  if ((w->flags & WF_SIZING_LEFT) && x != 0) {
2335  _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2336  w->SetDirty();
2337  w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2338  /* ResizeWindow() below ensures marking new position as dirty. */
2339  } else {
2340  _drag_delta.x += x;
2341  }
2342 
2343  /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
2344  ResizeWindow(w, x, y);
2345  return ES_HANDLED;
2346  }
2347  }
2348 
2349  _dragging_window = false;
2350  return ES_HANDLED;
2351 }
2352 
2357 static void StartWindowDrag(Window *w)
2358 {
2359  w->flags |= WF_DRAGGING;
2360  w->flags &= ~WF_CENTERED;
2361  _dragging_window = true;
2362 
2363  _drag_delta.x = w->left - _cursor.pos.x;
2364  _drag_delta.y = w->top - _cursor.pos.y;
2365 
2366  BringWindowToFront(w);
2368 }
2369 
2375 static void StartWindowSizing(Window *w, bool to_left)
2376 {
2377  w->flags |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
2378  w->flags &= ~WF_CENTERED;
2379  _dragging_window = true;
2380 
2381  _drag_delta.x = _cursor.pos.x;
2382  _drag_delta.y = _cursor.pos.y;
2383 
2384  BringWindowToFront(w);
2386 }
2387 
2393 {
2394  int i;
2396  bool rtl = false;
2397 
2398  if (sb->type == NWID_HSCROLLBAR) {
2399  i = _cursor.pos.x - _cursorpos_drag_start.x;
2400  rtl = _current_text_dir == TD_RTL;
2401  } else {
2402  i = _cursor.pos.y - _cursorpos_drag_start.y;
2403  }
2404 
2405  if (sb->disp_flags & ND_SCROLLBAR_BTN) {
2406  if (_scroller_click_timeout == 1) {
2407  _scroller_click_timeout = 3;
2408  sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
2409  w->SetDirty();
2410  }
2411  return;
2412  }
2413 
2414  /* Find the item we want to move to and make sure it's inside bounds. */
2415  int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
2416  if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
2417  if (pos != sb->GetPosition()) {
2418  sb->SetPosition(pos);
2419  w->SetDirty();
2420  }
2421 }
2422 
2428 {
2429  Window *w;
2430  FOR_ALL_WINDOWS_FROM_BACK(w) {
2431  if (w->mouse_capture_widget >= 0) {
2432  /* Abort if no button is clicked any more. */
2433  if (!_left_button_down) {
2434  w->mouse_capture_widget = -1;
2435  w->SetDirty();
2436  return ES_HANDLED;
2437  }
2438 
2439  /* Handle scrollbar internally, or dispatch click event */
2441  if (type == NWID_VSCROLLBAR || type == NWID_HSCROLLBAR) {
2443  } else {
2444  /* If cursor hasn't moved, there is nothing to do. */
2445  if (_cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2446 
2447  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2448  w->OnClick(pt, w->mouse_capture_widget, 0);
2449  }
2450  return ES_HANDLED;
2451  }
2452  }
2453 
2454  return ES_NOT_HANDLED;
2455 }
2456 
2462 {
2463  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2464 
2465  if (!_scrolling_viewport) return ES_NOT_HANDLED;
2466 
2467  /* When we don't have a last scroll window we are starting to scroll.
2468  * When the last scroll window and this are not the same we went
2469  * outside of the window and should not left-mouse scroll anymore. */
2470  if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2471 
2472  if (_last_scroll_window == NULL || !((_settings_client.gui.scroll_mode != VSM_MAP_LMB && _right_button_down) || scrollwheel_scrolling || (_settings_client.gui.scroll_mode == VSM_MAP_LMB && _left_button_down))) {
2473  _cursor.fix_at = false;
2474  _scrolling_viewport = false;
2475  _last_scroll_window = NULL;
2476  return ES_NOT_HANDLED;
2477  }
2478 
2479  if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
2480  /* If the main window is following a vehicle, then first let go of it! */
2481  const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2482  ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2483  return ES_NOT_HANDLED;
2484  }
2485 
2486  Point delta;
2487  if (scrollwheel_scrolling) {
2488  /* We are using scrollwheels for scrolling */
2489  delta.x = _cursor.h_wheel;
2490  delta.y = _cursor.v_wheel;
2491  _cursor.v_wheel = 0;
2492  _cursor.h_wheel = 0;
2493  } else {
2495  delta.x = -_cursor.delta.x;
2496  delta.y = -_cursor.delta.y;
2497  } else {
2498  delta.x = _cursor.delta.x;
2499  delta.y = _cursor.delta.y;
2500  }
2501  }
2502 
2503  /* Create a scroll-event and send it to the window */
2504  if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2505 
2506  _cursor.delta.x = 0;
2507  _cursor.delta.y = 0;
2508  return ES_HANDLED;
2509 }
2510 
2522 {
2523  bool bring_to_front = false;
2524 
2525  if (w->window_class == WC_MAIN_WINDOW ||
2526  IsVitalWindow(w) ||
2527  w->window_class == WC_TOOLTIPS ||
2529  return true;
2530  }
2531 
2532  /* Use unshaded window size rather than current size for shaded windows. */
2533  int w_width = w->width;
2534  int w_height = w->height;
2535  if (w->IsShaded()) {
2536  w_width = w->unshaded_size.width;
2537  w_height = w->unshaded_size.height;
2538  }
2539 
2540  Window *u;
2542  /* A modal child will prevent the activation of the parent window */
2543  if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) {
2544  u->SetWhiteBorder();
2545  u->SetDirty();
2546  return false;
2547  }
2548 
2549  if (u->window_class == WC_MAIN_WINDOW ||
2550  IsVitalWindow(u) ||
2551  u->window_class == WC_TOOLTIPS ||
2553  continue;
2554  }
2555 
2556  /* Window sizes don't interfere, leave z-order alone */
2557  if (w->left + w_width <= u->left ||
2558  u->left + u->width <= w->left ||
2559  w->top + w_height <= u->top ||
2560  u->top + u->height <= w->top) {
2561  continue;
2562  }
2563 
2564  bring_to_front = true;
2565  }
2566 
2567  if (bring_to_front) BringWindowToFront(w);
2568  return true;
2569 }
2570 
2579 EventState Window::HandleEditBoxKey(int wid, WChar key, uint16 keycode)
2580 {
2581  QueryString *query = this->GetQueryString(wid);
2582  if (query == NULL) return ES_NOT_HANDLED;
2583 
2584  int action = QueryString::ACTION_NOTHING;
2585 
2586  switch (query->text.HandleKeyPress(key, keycode)) {
2587  case HKPR_EDITING:
2588  this->SetWidgetDirty(wid);
2589  this->OnEditboxChanged(wid);
2590  break;
2591 
2592  case HKPR_CURSOR:
2593  this->SetWidgetDirty(wid);
2594  /* For the OSK also invalidate the parent window */
2595  if (this->window_class == WC_OSK) this->InvalidateData();
2596  break;
2597 
2598  case HKPR_CONFIRM:
2599  if (this->window_class == WC_OSK) {
2600  this->OnClick(Point(), WID_OSK_OK, 1);
2601  } else if (query->ok_button >= 0) {
2602  this->OnClick(Point(), query->ok_button, 1);
2603  } else {
2604  action = query->ok_button;
2605  }
2606  break;
2607 
2608  case HKPR_CANCEL:
2609  if (this->window_class == WC_OSK) {
2610  this->OnClick(Point(), WID_OSK_CANCEL, 1);
2611  } else if (query->cancel_button >= 0) {
2612  this->OnClick(Point(), query->cancel_button, 1);
2613  } else {
2614  action = query->cancel_button;
2615  }
2616  break;
2617 
2618  case HKPR_NOT_HANDLED:
2619  return ES_NOT_HANDLED;
2620 
2621  default: break;
2622  }
2623 
2624  switch (action) {
2626  this->UnfocusFocusedWidget();
2627  break;
2628 
2630  if (query->text.bytes <= 1) {
2631  /* If already empty, unfocus instead */
2632  this->UnfocusFocusedWidget();
2633  } else {
2634  query->text.DeleteAll();
2635  this->SetWidgetDirty(wid);
2636  this->OnEditboxChanged(wid);
2637  }
2638  break;
2639 
2640  default:
2641  break;
2642  }
2643 
2644  return ES_HANDLED;
2645 }
2646 
2652 void HandleKeypress(uint keycode, WChar key)
2653 {
2654  /* World generation is multithreaded and messes with companies.
2655  * But there is no company related window open anyway, so _current_company is not used. */
2656  assert(HasModalProgress() || IsLocalCompany());
2657 
2658  /*
2659  * The Unicode standard defines an area called the private use area. Code points in this
2660  * area are reserved for private use and thus not portable between systems. For instance,
2661  * Apple defines code points for the arrow keys in this area, but these are only printable
2662  * on a system running OS X. We don't want these keys to show up in text fields and such,
2663  * and thus we have to clear the unicode character when we encounter such a key.
2664  */
2665  if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2666 
2667  /*
2668  * If both key and keycode is zero, we don't bother to process the event.
2669  */
2670  if (key == 0 && keycode == 0) return;
2671 
2672  /* Check if the focused window has a focused editbox */
2673  if (EditBoxInGlobalFocus()) {
2674  /* All input will in this case go to the focused editbox */
2675  if (_focused_window->window_class == WC_CONSOLE) {
2676  if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2677  } else {
2678  if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return;
2679  }
2680  }
2681 
2682  /* Call the event, start with the uppermost window, but ignore the toolbar. */
2683  Window *w;
2684  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2685  if (w->window_class == WC_MAIN_TOOLBAR) continue;
2686  if (w->window_desc->hotkeys != NULL) {
2687  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2688  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2689  }
2690  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2691  }
2692 
2694  /* When there is no toolbar w is null, check for that */
2695  if (w != NULL) {
2696  if (w->window_desc->hotkeys != NULL) {
2697  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2698  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2699  }
2700  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2701  }
2702 
2703  HandleGlobalHotkeys(key, keycode);
2704 }
2705 
2710 {
2711  /* Call the event, start with the uppermost window. */
2712  Window *w;
2713  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2714  if (w->OnCTRLStateChange() == ES_HANDLED) return;
2715  }
2716 }
2717 
2723 /* virtual */ void Window::InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2724 {
2725  QueryString *query = this->GetQueryString(wid);
2726  if (query == NULL) return;
2727 
2728  if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2729  this->SetWidgetDirty(wid);
2730  this->OnEditboxChanged(wid);
2731  }
2732 }
2733 
2740 void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2741 {
2742  if (!EditBoxInGlobalFocus()) return;
2743 
2744  _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end);
2745 }
2746 
2754 
2759 static void HandleAutoscroll()
2760 {
2761  if (_game_mode == GM_MENU || HasModalProgress()) return;
2763  if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2764 
2765  int x = _cursor.pos.x;
2766  int y = _cursor.pos.y;
2767  Window *w = FindWindowFromPt(x, y);
2768  if (w == NULL || w->flags & WF_DISABLE_VP_SCROLL) return;
2770 
2771  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2772  if (vp == NULL) return;
2773 
2774  x -= vp->left;
2775  y -= vp->top;
2776 
2777  /* here allows scrolling in both x and y axis */
2778  static const int SCROLLSPEED = 3;
2779  if (x - 15 < 0) {
2780  w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * SCROLLSPEED, vp->zoom);
2781  } else if (15 - (vp->width - x) > 0) {
2782  w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * SCROLLSPEED, vp->zoom);
2783  }
2784  if (y - 15 < 0) {
2785  w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * SCROLLSPEED, vp->zoom);
2786  } else if (15 - (vp->height - y) > 0) {
2787  w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * SCROLLSPEED, vp->zoom);
2788  }
2789 }
2790 
2792  MC_NONE = 0,
2793  MC_LEFT,
2794  MC_RIGHT,
2795  MC_DOUBLE_LEFT,
2796  MC_HOVER,
2797 
2801 };
2803 
2804 static void ScrollMainViewport(int x, int y)
2805 {
2806  if (_game_mode != GM_MENU) {
2808  assert(w);
2809 
2812  }
2813 }
2814 
2824 static const int8 scrollamt[16][2] = {
2825  { 0, 0},
2826  {-2, 0},
2827  { 0, -2},
2828  {-2, -1},
2829  { 2, 0},
2830  { 0, 0},
2831  { 2, -1},
2832  { 0, -2},
2833  { 0, 2},
2834  {-2, 1},
2835  { 0, 0},
2836  {-2, 0},
2837  { 2, 1},
2838  { 0, 2},
2839  { 2, 0},
2840  { 0, 0},
2841 };
2842 
2843 static void HandleKeyScrolling()
2844 {
2845  /*
2846  * Check that any of the dirkeys is pressed and that the focused window
2847  * doesn't have an edit-box as focused widget.
2848  */
2849  if (_dirkeys && !EditBoxInGlobalFocus()) {
2850  int factor = _shift_pressed ? 50 : 10;
2851  ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2852  }
2853 }
2854 
2855 static void MouseLoop(MouseClick click, int mousewheel)
2856 {
2857  /* World generation is multithreaded and messes with companies.
2858  * But there is no company related window open anyway, so _current_company is not used. */
2859  assert(HasModalProgress() || IsLocalCompany());
2860 
2861  HandlePlacePresize();
2863 
2864  if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2865  if (HandleMouseDragDrop() == ES_HANDLED) return;
2866  if (HandleWindowDragging() == ES_HANDLED) return;
2867  if (HandleActiveWidget() == ES_HANDLED) return;
2868  if (HandleViewportScroll() == ES_HANDLED) return;
2869 
2870  HandleMouseOver();
2871 
2872  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2873  if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2874 
2875  int x = _cursor.pos.x;
2876  int y = _cursor.pos.y;
2877  Window *w = FindWindowFromPt(x, y);
2878  if (w == NULL) return;
2879 
2880  if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2881  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2882 
2883  /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2884  if (vp != NULL && (_game_mode == GM_MENU || HasModalProgress())) return;
2885 
2886  if (mousewheel != 0) {
2887  /* Send mousewheel event to window, unless we're scrolling a viewport or the map */
2888  if (!scrollwheel_scrolling || (vp == NULL && w->window_class != WC_SMALLMAP)) w->OnMouseWheel(mousewheel);
2889 
2890  /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2891  if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2892  }
2893 
2894  if (vp != NULL) {
2895  if (scrollwheel_scrolling && !(w->flags & WF_DISABLE_VP_SCROLL)) {
2896  _scrolling_viewport = true;
2897  _cursor.fix_at = true;
2898  return;
2899  }
2900 
2901  switch (click) {
2902  case MC_DOUBLE_LEFT:
2903  case MC_LEFT:
2904  if (HandleViewportClicked(vp, x, y)) return;
2905  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2907  _scrolling_viewport = true;
2908  _cursor.fix_at = false;
2909  return;
2910  }
2911  break;
2912 
2913  case MC_RIGHT:
2914  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2916  _scrolling_viewport = true;
2919  return;
2920  }
2921  break;
2922 
2923  default:
2924  break;
2925  }
2926  }
2927 
2928  if (vp == NULL || (w->flags & WF_DISABLE_VP_SCROLL)) {
2929  switch (click) {
2930  case MC_LEFT:
2931  case MC_DOUBLE_LEFT:
2932  DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2933  return;
2934 
2935  default:
2936  if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
2937  /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2938  * Simulate a right button click so we can get started. */
2939  FALLTHROUGH;
2940 
2941  case MC_RIGHT:
2942  DispatchRightClickEvent(w, x - w->left, y - w->top);
2943  return;
2944 
2945  case MC_HOVER:
2946  DispatchHoverEvent(w, x - w->left, y - w->top);
2947  break;
2948  }
2949  }
2950 
2951  /* We're not doing anything with 2D scrolling, so reset the value. */
2952  _cursor.h_wheel = 0;
2953  _cursor.v_wheel = 0;
2954 }
2955 
2960 {
2961  /* World generation is multithreaded and messes with companies.
2962  * But there is no company related window open anyway, so _current_company is not used. */
2963  assert(HasModalProgress() || IsLocalCompany());
2964 
2965  static int double_click_time = 0;
2966  static Point double_click_pos = {0, 0};
2967 
2968  /* Mouse event? */
2969  MouseClick click = MC_NONE;
2971  click = MC_LEFT;
2972  if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
2973  double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2974  double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2975  click = MC_DOUBLE_LEFT;
2976  }
2977  double_click_time = _realtime_tick;
2978  double_click_pos = _cursor.pos;
2979  _left_button_clicked = true;
2981  } else if (_right_button_clicked) {
2982  _right_button_clicked = false;
2983  click = MC_RIGHT;
2985  }
2986 
2987  int mousewheel = 0;
2988  if (_cursor.wheel) {
2989  mousewheel = _cursor.wheel;
2990  _cursor.wheel = 0;
2992  }
2993 
2994  static uint32 hover_time = 0;
2995  static Point hover_pos = {0, 0};
2996 
2998  if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
2999  hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
3000  hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
3001  hover_pos = _cursor.pos;
3002  hover_time = _realtime_tick;
3003  _mouse_hovering = false;
3004  } else {
3005  if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
3006  click = MC_HOVER;
3008  _mouse_hovering = true;
3009  }
3010  }
3011  }
3012 
3013  /* Handle sprite picker before any GUI interaction */
3015  /* Next realtime tick? Then redraw has finished */
3016  _newgrf_debug_sprite_picker.mode = SPM_NONE;
3018  }
3019 
3020  if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
3021  /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
3023  _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
3026  _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
3028  } else {
3029  MouseLoop(click, mousewheel);
3030  }
3031 
3032  /* We have moved the mouse the required distance,
3033  * no need to move it at any later time. */
3034  _cursor.delta.x = 0;
3035  _cursor.delta.y = 0;
3036 }
3037 
3041 static void CheckSoftLimit()
3042 {
3043  if (_settings_client.gui.window_soft_limit == 0) return;
3044 
3045  for (;;) {
3046  uint deletable_count = 0;
3047  Window *w, *last_deletable = NULL;
3048  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3049  if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags & WF_STICKY)) continue;
3050 
3051  last_deletable = w;
3052  deletable_count++;
3053  }
3054 
3055  /* We've not reached the soft limit yet. */
3056  if (deletable_count <= _settings_client.gui.window_soft_limit) break;
3057 
3058  assert(last_deletable != NULL);
3059  delete last_deletable;
3060  }
3061 }
3062 
3067 {
3068  /* World generation is multithreaded and messes with companies.
3069  * But there is no company related window open anyway, so _current_company is not used. */
3070  assert(HasModalProgress() || IsLocalCompany());
3071 
3072  CheckSoftLimit();
3073 
3074  /* Do the actual free of the deleted windows. */
3075  for (Window *v = _z_front_window; v != NULL; /* nothing */) {
3076  Window *w = v;
3077  v = v->z_back;
3078 
3079  if (w->window_class != WC_INVALID) continue;
3080 
3082  free(w);
3083  }
3084 
3085  if (_input_events_this_tick != 0) {
3086  /* The input loop is called only once per GameLoop() - so we can clear the counter here */
3088  /* there were some inputs this tick, don't scroll ??? */
3089  return;
3090  }
3091 
3092  /* HandleMouseEvents was already called for this tick */
3094 }
3095 
3099 void CallWindowRealtimeTickEvent(uint delta_ms)
3100 {
3101  Window *w;
3102  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3103  w->OnRealtimeTick(delta_ms);
3104  }
3105 }
3106 
3111 {
3112  static uint32 last_realtime_tick = _realtime_tick;
3113  uint delta_ms = _realtime_tick - last_realtime_tick;
3114  last_realtime_tick = _realtime_tick;
3115 
3116  if (delta_ms == 0) return;
3117 
3118  PerformanceMeasurer framerate(PFE_DRAWING);
3120 
3121  CallWindowRealtimeTickEvent(delta_ms);
3122 
3123 #ifdef ENABLE_NETWORK
3124  static GUITimer network_message_timer = GUITimer(1);
3125  if (network_message_timer.Elapsed(delta_ms)) {
3126  network_message_timer.SetInterval(1000);
3128  }
3129 #endif
3130 
3131  Window *w;
3132 
3133  static GUITimer window_timer = GUITimer(1);
3134  if (window_timer.Elapsed(delta_ms)) {
3135  if (_network_dedicated) window_timer.SetInterval(MILLISECONDS_PER_TICK);
3136 
3137  extern int _caret_timer;
3138  _caret_timer += 3;
3139  CursorTick();
3140 
3141  HandleKeyScrolling();
3142  HandleAutoscroll();
3143  DecreaseWindowCounters();
3144  }
3145 
3146  static GUITimer highlight_timer = GUITimer(1);
3147  if (highlight_timer.Elapsed(delta_ms)) {
3148  highlight_timer.SetInterval(450);
3150  }
3151 
3152  if (!_pause_mode || _game_mode == GM_EDITOR || _settings_game.construction.command_pause_level > CMDPL_NO_CONSTRUCTION) MoveAllTextEffects(delta_ms);
3153 
3154  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3157  }
3158 
3159  /* Skip the actual drawing on dedicated servers without screen.
3160  * But still empty the invalidation queues above. */
3161  if (_network_dedicated) return;
3162 
3163  static GUITimer hundredth_timer = GUITimer(1);
3164  if (hundredth_timer.Elapsed(delta_ms)) {
3165  hundredth_timer.SetInterval(3000); // Historical reason: 100 * MILLISECONDS_PER_TICK
3166 
3167  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3168  w->OnHundredthTick();
3169  }
3170  }
3171 
3172  if (window_timer.HasElapsed()) {
3173  window_timer.SetInterval(MILLISECONDS_PER_TICK);
3174 
3175  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3176  if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) {
3178  w->SetDirty();
3179  }
3180  }
3181  }
3182 
3183  DrawDirtyBlocks();
3184 
3185  FOR_ALL_WINDOWS_FROM_BACK(w) {
3186  /* Update viewport only if window is not shaded. */
3187  if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
3188  }
3190  /* Redraw mouse cursor in case it was hidden */
3191  DrawMouseCursor();
3192 }
3193 
3200 {
3201  const Window *w;
3202  FOR_ALL_WINDOWS_FROM_BACK(w) {
3203  if (w->window_class == cls && w->window_number == number) w->SetDirty();
3204  }
3205 }
3206 
3213 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
3214 {
3215  const Window *w;
3216  FOR_ALL_WINDOWS_FROM_BACK(w) {
3217  if (w->window_class == cls && w->window_number == number) {
3218  w->SetWidgetDirty(widget_index);
3219  }
3220  }
3221 }
3222 
3228 {
3229  Window *w;
3230  FOR_ALL_WINDOWS_FROM_BACK(w) {
3231  if (w->window_class == cls) w->SetDirty();
3232  }
3233 }
3234 
3240 void Window::InvalidateData(int data, bool gui_scope)
3241 {
3242  this->SetDirty();
3243  if (!gui_scope) {
3244  /* Schedule GUI-scope invalidation for next redraw. */
3245  *this->scheduled_invalidation_data.Append() = data;
3246  }
3247  this->OnInvalidateData(data, gui_scope);
3248 }
3249 
3254 {
3255  for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
3256  this->OnInvalidateData(*data, true);
3257  }
3259 }
3260 
3265 {
3266  if ((this->flags & WF_HIGHLIGHTED) == 0) return;
3267 
3268  for (uint i = 0; i < this->nested_array_size; i++) {
3269  if (this->IsWidgetHighlighted(i)) this->SetWidgetDirty(i);
3270  }
3271 }
3272 
3299 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3300 {
3301  Window *w;
3302  FOR_ALL_WINDOWS_FROM_BACK(w) {
3303  if (w->window_class == cls && w->window_number == number) {
3304  w->InvalidateData(data, gui_scope);
3305  }
3306  }
3307 }
3308 
3317 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3318 {
3319  Window *w;
3320 
3321  FOR_ALL_WINDOWS_FROM_BACK(w) {
3322  if (w->window_class == cls) {
3323  w->InvalidateData(data, gui_scope);
3324  }
3325  }
3326 }
3327 
3332 {
3333  Window *w;
3334  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3335  w->OnGameTick();
3336  }
3337 }
3338 
3346 {
3347  Window *w;
3348 
3349 restart_search:
3350  /* When we find the window to delete, we need to restart the search
3351  * as deleting this window could cascade in deleting (many) others
3352  * anywhere in the z-array */
3353  FOR_ALL_WINDOWS_FROM_BACK(w) {
3354  if (w->window_class != WC_MAIN_WINDOW &&
3355  w->window_class != WC_SELECT_GAME &&
3356  w->window_class != WC_MAIN_TOOLBAR &&
3357  w->window_class != WC_STATUS_BAR &&
3358  w->window_class != WC_TOOLTIPS &&
3359  (w->flags & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
3360 
3361  delete w;
3362  goto restart_search;
3363  }
3364  }
3365 }
3366 
3375 {
3376  Window *w;
3377 
3378  /* Delete every window except for stickied ones, then sticky ones as well */
3380 
3381 restart_search:
3382  /* When we find the window to delete, we need to restart the search
3383  * as deleting this window could cascade in deleting (many) others
3384  * anywhere in the z-array */
3385  FOR_ALL_WINDOWS_FROM_BACK(w) {
3386  if (w->flags & WF_STICKY) {
3387  delete w;
3388  goto restart_search;
3389  }
3390  }
3391 }
3392 
3397 {
3399  InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
3400  InvalidateWindowData(WC_MESSAGE_HISTORY, 0); // invalidate the message history
3401  DeleteWindowById(WC_NEWS_WINDOW, 0); // close newspaper or general message window if shown
3402 }
3403 
3409 {
3410  Window *w;
3411 
3412 restart_search:
3413  /* When we find the window to delete, we need to restart the search
3414  * as deleting this window could cascade in deleting (many) others
3415  * anywhere in the z-array */
3416  FOR_ALL_WINDOWS_FROM_BACK(w) {
3417  if (w->window_desc->flags & WDF_CONSTRUCTION) {
3418  delete w;
3419  goto restart_search;
3420  }
3421  }
3422 
3423  FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
3424 }
3425 
3428 {
3431 }
3432 
3435 {
3436  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3437  NWidgetScrollbar::InvalidateDimensionCache();
3438 
3439  extern void InitDepotWindowBlockSizes();
3441 
3442  Window *w;
3443  FOR_ALL_WINDOWS_FROM_BACK(w) {
3444  w->ReInit();
3445  }
3446 #ifdef ENABLE_NETWORK
3447  void NetworkReInitChatBoxSize();
3449 #endif
3450 
3451  /* Make sure essential parts of all windows are visible */
3454 }
3455 
3463 static int PositionWindow(Window *w, WindowClass clss, int setting)
3464 {
3465  if (w == NULL || w->window_class != clss) {
3466  w = FindWindowById(clss, 0);
3467  }
3468  if (w == NULL) return 0;
3469 
3470  int old_left = w->left;
3471  switch (setting) {
3472  case 1: w->left = (_screen.width - w->width) / 2; break;
3473  case 2: w->left = _screen.width - w->width; break;
3474  default: w->left = 0; break;
3475  }
3476  if (w->viewport != NULL) w->viewport->left += w->left - old_left;
3477  SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3478  return w->left;
3479 }
3480 
3487 {
3488  DEBUG(misc, 5, "Repositioning Main Toolbar...");
3490 }
3491 
3498 {
3499  DEBUG(misc, 5, "Repositioning statusbar...");
3501 }
3502 
3509 {
3510  DEBUG(misc, 5, "Repositioning news message...");
3512 }
3513 
3520 {
3521  DEBUG(misc, 5, "Repositioning network chat window...");
3523 }
3524 
3525 
3531 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3532 {
3533  Window *w;
3534  FOR_ALL_WINDOWS_FROM_BACK(w) {
3535  if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
3536  w->viewport->follow_vehicle = to_index;
3537  w->SetDirty();
3538  }
3539  }
3540 }
3541 
3542 
3548 void RelocateAllWindows(int neww, int newh)
3549 {
3551 
3552  Window *w;
3553  FOR_ALL_WINDOWS_FROM_BACK(w) {
3554  int left, top;
3555  /* XXX - this probably needs something more sane. For example specifying
3556  * in a 'backup'-desc that the window should always be centered. */
3557  switch (w->window_class) {
3558  case WC_MAIN_WINDOW:
3559  case WC_BOOTSTRAP:
3560  ResizeWindow(w, neww, newh);
3561  continue;
3562 
3563  case WC_MAIN_TOOLBAR:
3564  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3565 
3566  top = w->top;
3567  left = PositionMainToolbar(w); // changes toolbar orientation
3568  break;
3569 
3570  case WC_NEWS_WINDOW:
3571  top = newh - w->height;
3572  left = PositionNewsMessage(w);
3573  break;
3574 
3575  case WC_STATUS_BAR:
3576  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3577 
3578  top = newh - w->height;
3579  left = PositionStatusbar(w);
3580  break;
3581 
3582  case WC_SEND_NETWORK_MSG:
3583  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3584 
3585  top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3586  left = PositionNetworkChatWindow(w);
3587  break;
3588 
3589  case WC_CONSOLE:
3590  IConsoleResize(w);
3591  continue;
3592 
3593  default: {
3594  if (w->flags & WF_CENTERED) {
3595  top = (newh - w->height) >> 1;
3596  left = (neww - w->width) >> 1;
3597  break;
3598  }
3599 
3600  left = w->left;
3601  if (left + (w->width >> 1) >= neww) left = neww - w->width;
3602  if (left < 0) left = 0;
3603 
3604  top = w->top;
3605  if (top + (w->height >> 1) >= newh) top = newh - w->height;
3606  break;
3607  }
3608  }
3609 
3610  EnsureVisibleCaption(w, left, top);
3611  }
3612 }
3613 
3620 {
3621  this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
3623 }
EventState
State of handling an event.
Definition: window_type.h:713
Window * _z_back_window
List of windows opened at the screen sorted from the back.
Definition: window.cpp:61
Functions related to OTTD&#39;s strings.
static void CheckSoftLimit()
Check the soft limit of deletable (non vital, non sticky) windows.
Definition: window.cpp:3041
Generate landscape (newgame); Window numbers:
Definition: window_type.h:451
Empty widget, place holder to reserve space in widget array.
Definition: widget_type.h:48
SpecialMouseMode
Mouse modes.
Definition: window_gui.h:901
Functions/types related to NewGRF debugging.
void SetShaded(bool make_shaded)
Set the shaded state of the window to make_shaded.
Definition: window.cpp:1013
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:783
static Window * FindChildWindow(const Window *w, WindowClass wc)
Find the Window whose parent pointer points to this window.
Definition: window.cpp:1040
static const int ACTION_DESELECT
Deselect editbox.
static bool IsLocalCompany()
Is the current company the local company?
Definition: company_func.h:45
WindowNumber window_number
The WindowNumber of the window that is responsible for the selection mode.
static int CDECL DescSorter(WindowDesc *const *a, WindowDesc *const *b)
Sort WindowDesc by ini_key.
Definition: window.cpp:157
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:143
virtual void OnScroll(Point delta)
Handle the request for (viewport) scrolling.
Definition: window_gui.h:656
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:1605
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
Definition: window.cpp:1860
ViewportAutoscrolling
Values for _settings_client.gui.auto_scrolling.
Definition: window.cpp:47
Base of all video drivers.
static Window * _mouseover_last_w
Window of the last OnMouseOver event.
Definition: window.cpp:55
Data about how and where to blit pixels.
Definition: gfx_type.h:156
ResizeInfo resize
Resize information.
Definition: window_gui.h:317
virtual EventState OnHotkey(int hotkey)
A hotkey has been pressed.
Definition: window.cpp:594
const QueryString * GetQueryString(uint widnum) const
Return the querystring associated to a editbox.
Definition: window.cpp:331
virtual void ApplyDefaults()
Read default values from WindowDesc configuration an apply them to the window.
Definition: window.cpp:184
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1849
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:52
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=NULL)
Window description constructor.
Definition: window.cpp:94
Window * _z_front_window
List of windows opened at the screen sorted from the front.
Definition: window.cpp:59
const NWidgetCore * nested_focus
Currently focused nested widget, or NULL if no nested widget has focus.
Definition: window_gui.h:322
Point pos
logical mouse position
Definition: gfx_type.h:119
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3199
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:881
uint resize_x
Horizontal resize step (0 means not resizable).
Definition: widget_type.h:166
void SetFocusedWindow(Window *w)
Set the window that has the focus.
Definition: window.cpp:436
void SetTimeout()
Set the timeout flag of the window and initiate the timer.
Definition: window_gui.h:361
Window * parent
Parent window.
Definition: window_gui.h:332
void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
Handle text input.
Definition: window.cpp:2740
High level window description.
Definition: window_gui.h:168
Saveload window; Window numbers:
Definition: window_type.h:139
Bootstrap; Window numbers:
Definition: window_type.h:639
Landscape generation (in Scenario Editor); Window numbers:
Definition: window_type.h:444
virtual void OnDragDrop(Point pt, int widget)
A dragged &#39;object&#39; has been released.
Definition: window_gui.h:650
Window is being resized towards the right.
Definition: window_gui.h:237
StringID tool_tip
Tooltip of the widget.
Definition: widget_type.h:306
WindowFlags flags
Window flags.
Definition: window_gui.h:305
int left
x position of left edge of the window
Definition: window_gui.h:312
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:2047
SpecialMouseMode _special_mouse_mode
Mode of the mouse.
Definition: window.cpp:82
int height
Screen height of the viewport.
Definition: viewport_type.h:28
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Hotkey related functions.
Cancel key.
Definition: osk_widget.h:19
int16 pref_height
User-preferred height of the window. Zero if unset.
Definition: window_gui.h:187
Scrollbar data structure.
Definition: widget_type.h:589
static void RemoveWindowFromZOrdering(Window *w)
Removes a window from the z-ordering.
Definition: window.cpp:1405
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:41
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:581
static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
Dispatch left mouse-button (possibly double) click in window.
Definition: window.cpp:636
Progress report of landscape generation; Window numbers:
Definition: window_type.h:458
Nested widget to display and control a scrollbar in a window.
Definition: widget_type.h:750
void NetworkReInitChatBoxSize()
Initialize all font-dependent chat box sizes.
void CallWindowGameTickEvent()
Dispatch OnGameTick event over all windows.
Definition: window.cpp:3331
void DeleteConstructionWindows()
Delete all windows that are used for construction of vehicle etc.
Definition: window.cpp:3408
Dragging an object.
Definition: window_gui.h:903
textfile; Window numbers:
Definition: window_type.h:182
uint16 hover_delay_ms
time required to activate a hover event, in milliseconds
Definition: settings_type.h:95
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1112
The passed event is not handled.
Definition: window_type.h:715
bool Elapsed(uint delta)
Test if a timer has elapsed.
Definition: guitimer_func.h:57
void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets,...)
Sets the enabled/disabled status of a list of widgets.
Definition: window.cpp:520
NewGRF debug box (at top-right of a window, between WWT_CAPTION and WWT_SHADEBOX) ...
Definition: widget_type.h:63
int PositionMainToolbar(Window *w)
(Re)position main toolbar window at the screen.
Definition: window.cpp:3486
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:82
Dimension unshaded_size
Last known unshaded size (only valid while shaded).
Definition: window_gui.h:328
WidgetType type
Type of the widget / nested widget.
Definition: widget_type.h:163
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition: window.cpp:2709
Types for recording game performance data.
void InitWindowSystem()
(re)initialize the windowing system
Definition: window.cpp:1875
static Dimension closebox_dimension
Cached size of a closebox widget.
Definition: widget_type.h:783
void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
Shows a tooltip.
Definition: misc_gui.cpp:741
Time between 2 left clicks before it becoming a double click, in ms.
Definition: window.cpp:2799
virtual ~PickerWindowBase()
Destructor of the base class PickerWindowBase Main utility is to stop the base Window destructor from...
Definition: window.cpp:3619
void CDECL void DeleteAll()
Delete every character in the textbuffer.
Definition: textbuf.cpp:118
a textbox for typing
Definition: widget_type.h:71
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:68
PreventHideDirection
Direction for moving the window.
Definition: window.cpp:2032
static const int ACTION_CLEAR
Clear editbox.
Buyout company (merger); Window numbers:
Definition: window_type.h:579
void SetPosition(int position)
Sets the position of the first visible element.
Definition: widget_type.h:701
Vehicle data structure.
Definition: vehicle_base.h:212
int top
y position of top edge of the window
Definition: window_gui.h:313
static bool IsInsideBS(const T x, const uint base, const uint size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:250
void Clear()
Remove all items from the list.
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:27
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:24
const T * Begin() const
Get the pointer to the first item (const)
virtual const char * GetFocusedText() const
Get the current input text if an edit box has the focus.
Definition: window.cpp:352
virtual void OnEditboxChanged(int widget)
The text in an editbox has been edited.
Definition: window_gui.h:718
static EventState HandleViewportScroll()
Handle viewport scrolling with the mouse.
Definition: window.cpp:2461
void * clicked_pixel
Clicked pixel (pointer to blitter buffer)
Definition: newgrf_debug.h:30
static EventState HandleWindowDragging()
Handle dragging/resizing of a window.
Definition: window.cpp:2183
virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
Compute the initial position of the window.
Definition: window.cpp:1798
void UpdateTileSelection()
Updates tile highlighting for all cases.
Definition: viewport.cpp:2220
Close box (at top-left of a window)
Definition: widget_type.h:69
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:24
virtual void OnMouseLoop()
Called for every mouse loop run, which is at least once per (game) tick.
Definition: window_gui.h:676
static void StartWindowDrag(Window *w)
Start window dragging.
Definition: window.cpp:2357
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3110
static bool _dragging_window
A window is being dragged or resized.
Definition: window.cpp:2177
static void StartWindowSizing(Window *w, bool to_left)
Start resizing a window.
Definition: window.cpp:2375
static Dimension resizebox_dimension
Cached size of a resizebox widget.
Definition: widget_type.h:782
void ReInit(int rx=0, int ry=0)
Re-initialize a window, and optionally change its size.
Definition: window.cpp:977
Simple vector template class.
Scroll all viewports at their edges.
Definition: window.cpp:51
WindowClass
Window classes.
Definition: window_type.h:39
#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:196
Invalid window.
Definition: window_type.h:696
void DeleteAllMessages()
Delete all messages and their corresponding window (if any).
Definition: window.cpp:3396
How all blitters should look like.
Definition: base.hpp:30
uint16 bytes
the current size of the string in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:37
uint smallest_x
Smallest horizontal size of the widget in a filled window.
Definition: widget_type.h:171
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Definition: vehicle_type.h:59
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:26
Speed of drawing world and GUI.
Map moves with mouse movement on holding right mouse button, cursor position is fixed.
Definition: settings_type.h:77
WindowClass cls
Class of the window,.
Definition: window_gui.h:177
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:3374
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2652
Non-text change, e.g. cursor position.
Definition: textbuf_type.h:25
Window is made sticky by user.
Definition: window_gui.h:240
Presizing mode (docks, tunnels).
Definition: window_gui.h:905
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:2033
void InputLoop()
Regular call from the global game loop.
Definition: window.cpp:3066
virtual void OnTimeout()
Called when this window&#39;s timeout has been reached.
Definition: window_gui.h:696
Window is being resized towards the left.
Definition: window_gui.h:238
const T * End() const
Get the pointer behind the last valid item (const)
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1810
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
Window * GetCallbackWnd()
Get the window that started the current highlighting.
Definition: viewport.cpp:2206
bool IsDisabled() const
Return whether the widget is disabled.
Definition: widget_type.h:358
bool _left_button_clicked
Is left mouse button clicked?
Definition: gfx.cpp:40
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:59
T * Append(uint to_add=1)
Append an item and return it.
Console; Window numbers:
Definition: window_type.h:633
virtual const char * GetMarkedText(size_t *length) const
Get the range of the currently marked input text.
Definition: window.cpp:379
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Definition: window.cpp:2171
static SmallVector< WindowDesc *, 16 > * _window_descs
List of all WindowDescs.
Definition: window.cpp:88
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Functions related to (drawing on) viewports.
Sprite aligner (debug); Window numbers:
Definition: window_type.h:670
static void HandleAutoscroll()
If needed and switched on, perform auto scrolling (automatically moving window contents when mouse is...
Definition: window.cpp:2759
void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
Switches viewports following vehicles, which get autoreplaced.
Definition: window.cpp:3531
Base for the GUIs that have an edit box in them.
static void DispatchHoverEvent(Window *w, int x, int y)
Dispatch hover of the mouse over a window.
Definition: window.cpp:793
Data structure for an opened window.
Definition: window_gui.h:271
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:36
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1826
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:3317
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1839
int PositionStatusbar(Window *w)
(Re)position statusbar window at the screen.
Definition: window.cpp:3497
bool InsertString(const char *str, bool marked, const char *caret=NULL, const char *insert_location=NULL, const char *replacement_end=NULL)
Insert a string into the text buffer.
Definition: textbuf.cpp:164
static const int ACTION_NOTHING
Nothing.
Main window; Window numbers:
Definition: window_type.h:46
Vehicle orders; Window numbers:
Definition: window_type.h:207
int16 GetDefaultHeight() const
Determine default height of window.
Definition: window.cpp:135
void NetworkChatMessageLoop()
Check if a message is expired.
uint8 scroll_mode
viewport scroll mode
Definition: settings_type.h:98
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:197
NWidgetBase ** nested_array
Array of pointers into the tree. Do not access directly, use Window::GetWidget() instead.
Definition: window_gui.h:325
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:2971
GUI Timers.
Functions related to errors.
Bit value of the &#39;dropdown active&#39; flag.
Definition: widget_type.h:273
static EventState HandleActiveWidget()
Handle active widget (mouse draggin on widget) with the mouse.
Definition: window.cpp:2427
bool _right_button_clicked
Is right mouse button clicked?
Definition: gfx.cpp:42
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:65
uint Length() const
Get the number of items in the list.
virtual EventState OnKeyPress(WChar key, uint16 keycode)
A key has been pressed.
Definition: window_gui.h:602
Error message; Window numbers:
Definition: window_type.h:105
void HideVitalWindows()
Delete all always on-top windows to get an empty screen.
Definition: window.cpp:3427
uint pos_y
Vertical position of top-left corner of the widget in the window.
Definition: widget_type.h:178
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:941
This window is used for construction; close it whenever changing company.
Definition: window_gui.h:210
int PositionNewsMessage(Window *w)
(Re)position news message window at the screen.
Definition: window.cpp:3508
virtual void OnFocusLost()
Called when window looses focus.
Definition: window.cpp:508
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:1645
void SetLowered(bool lowered)
Lower or raise the widget.
Definition: widget_type.h:337
static Point _drag_delta
delta between mouse cursor and upper left corner of dragged window
Definition: window.cpp:54
Simple pair of data.
Chatbox; Window numbers:
Definition: window_type.h:493
Bit value of the &#39;scrollbar up&#39; flag.
Definition: widget_type.h:274
Tooltip window; Window numbers:
Definition: window_type.h:111
void IniLoadWindowSettings(IniFile *ini, const char *grpname, void *desc)
Load a WindowDesc from config.
Definition: settings.cpp:744
int16 GetDefaultWidth() const
Determine default width of window.
Definition: window.cpp:125
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:172
virtual void OnFocus()
Called when window gains focus.
Definition: window_gui.h:591
uint current_y
Current vertical size (after resizing).
Definition: widget_type.h:175
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:18
Window is being dragged.
Definition: window_gui.h:236
SmallMap< int, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition: window_gui.h:323
Small map; Window numbers:
Definition: window_type.h:99
On Screen Keyboard; Window numbers:
Definition: window_type.h:157
void RaiseButtons(bool autoraise=false)
Raise the buttons of the window.
Definition: window.cpp:557
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:39
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:180
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:1714
Functions related to the gfx engine.
abort current news display (active news were deleted)
Definition: statusbar_gui.h:21
void InitNewsItemStructs()
Initialize the news-items data structures.
Definition: news_gui.cpp:573
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:3548
Data stored about a string that can be modified in the GUI.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
Display plane with zero size vertically, and filling and resizing horizontally.
Definition: widget_type.h:389
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:234
void LoadFromDisk(const char *filename, Subdirectory subdir)
Load the Ini file&#39;s data from the disk.
Definition: ini_load.cpp:212
Functions related to modal progress.
A path without any base directory.
Definition: fileio_type.h:127
Definition of base types and functions in a cross-platform compatible way.
SmallVector< int, 4 > scheduled_invalidation_data
Data of scheduled OnInvalidateData() calls.
Definition: window_gui.h:277
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:176
Window is centered and shall stay centered after ReInit.
Definition: window_gui.h:244
int GetMainViewTop()
Return the top of the main view available for general use.
Definition: window.cpp:2160
A number of safeguards to prevent using unsafe methods.
static void HandleMouseOver()
Report position of the mouse to the underlying window.
Definition: window.cpp:2006
void SetWhiteBorder()
Set the timeout flag of the window and initiate the timer.
Definition: window_gui.h:370
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
int wheel
mouse wheel movement
Definition: gfx_type.h:121
List of hotkeys for a window.
Definition: hotkeys.h:42
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:2824
void DeleteCompanyWindows(CompanyID id)
Delete all windows of a company.
Definition: window.cpp:1179
uint32 click_time
Realtime tick when clicked to detect next frame.
Definition: newgrf_debug.h:31
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:306
static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
Dispatch the mousewheel-action to the window.
Definition: window.cpp:820
static void SaveToConfig()
Save all WindowDesc settings to _windows_file.
Definition: window.cpp:166
static void InvalidateDimensionCache()
Reset the cached dimensions.
Definition: widget.cpp:2086
void DeleteChildWindows(WindowClass wc=WC_INVALID) const
Delete all children a window might have in a head-recursive manner.
Definition: window.cpp:1054
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
Definition: window.cpp:3519
uint nested_array_size
Size of the nested array.
Definition: window_gui.h:326
bool right_mouse_wnd_close
close window with right click
Key does not affect editboxes.
Definition: textbuf_type.h:28
Custom currency; Window numbers:
Definition: window_type.h:614
virtual void OnHover(Point pt, int widget)
The mouse is hovering over a widget in the window, perform an action for it, like opening a custom to...
Definition: window_gui.h:636
Window timeout counter.
Definition: window_gui.h:234
Finances of a company; Window numbers:
Definition: window_type.h:518
EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode)
Process keypress for editbox widget.
Definition: window.cpp:2579
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:2723
Horizontal scrollbar.
Definition: widget_type.h:83
uint step_height
Step-size of height resize changes.
Definition: window_gui.h:220
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:311
bool _mouse_hovering
The mouse is hovering over the same point.
Definition: window.cpp:80
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:32
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:664
Console functions used outside of the console code.
Company colour selection; Window numbers:
Definition: window_type.h:225
Highscore; Window numbers:
Definition: window_type.h:645
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:203
Center the window.
Definition: window_gui.h:157
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:162
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition: gfx_type.h:122
GUI related functions in the console.
Road vehicle list; Window numbers:
Definition: window_type.h:309
int scrollbar_index
Index of an attached scrollbar.
Definition: widget_type.h:307
static bool MaybeBringWindowToFront(Window *w)
Check if a window can be made relative top-most window, and if so do it.
Definition: window.cpp:2521
const T * Find(const T &item) const
Search for the first occurrence of an item.
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:2085
Baseclass for nested widgets.
Definition: widget_type.h:126
Button with a drop-down.
Definition: widget_type.h:82
uint8 white_border_timer
Timer value of the WF_WHITE_BORDER for flags.
Definition: window_gui.h:310
Basic functions/variables used all over the place.
Below v is a safe position.
Definition: window.cpp:2034
void InitializeData(WindowNumber window_number)
Initializes the data (except the position and initial size) of a new Window.
Definition: window.cpp:1444
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:1420
PauseModeByte _pause_mode
The current pause mode.
Definition: gfx.cpp:48
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as &#39;dirty&#39;.
Definition: gfx.cpp:1305
static void DispatchRightClickEvent(Window *w, int x, int y)
Dispatch right mouse-button click in window.
Definition: window.cpp:768
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:147
uint resize_y
Vertical resize step (0 means not resizable).
Definition: widget_type.h:167
VehicleID follow_vehicle
VehicleID to follow if following a vehicle, INVALID_VEHICLE otherwise.
Definition: window_gui.h:259
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
uint pos_x
Horizontal position of top-left corner of the widget in the window.
Definition: widget_type.h:177
Types related to reading/writing &#39;*.ini&#39; files.
Window has a widget that has a highlight.
Definition: window_gui.h:243
Scroll main viewport at edge.
Definition: window.cpp:50
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:2959
bool pref_sticky
Preferred stickyness.
Definition: window_gui.h:185
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1157
Window is being resized.
Definition: window_gui.h:239
ViewPort * IsPtInWindowViewport(const Window *w, int x, int y)
Is a xy position inside the viewport of the window?
Definition: viewport.cpp:389
int32 dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:263
void UnInitWindowSystem()
Close down the windowing system.
Definition: window.cpp:1896
Initialize nested widget tree to smallest size. Also updates current_x and current_y.
Definition: widget_type.h:112
Maximum mouse movement before stopping a hover event.
Definition: window.cpp:2800
static const int WIDGET_LIST_END
indicate the end of widgets&#39; list for vararg functions
Definition: widget_type.h:22
The window is a modal child of some other window, meaning the parent is &#39;inactive&#39;.
Definition: window_gui.h:211
Station list; Window numbers:
Definition: window_type.h:297
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
No construction actions may be executed.
Definition: command_type.h:420
uint32 flags
Flags.
Definition: window_gui.h:180
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:2029
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:966
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:37
Build toolbar; Window numbers:
Definition: window_type.h:68
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
Offset of the caption text at the top.
Definition: window_gui.h:129
Network status window; Window numbers:
Definition: window_type.h:487
News history list; Window numbers:
Definition: window_type.h:267
MouseClick
Definition: window.cpp:2791
uint16 GetCount() const
Gets the number of elements in the list.
Definition: widget_type.h:613
Select game window; Window numbers:
Definition: window_type.h:437
Scroll main viewport at edge when using fullscreen.
Definition: window.cpp:49
Window * z_back
The window behind us in z-order.
Definition: window_gui.h:334
int left
Screen coordinate left egde of the viewport.
Definition: viewport_type.h:25
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1144
void UnshowCriticalError()
Unshow the critical error.
Definition: error_gui.cpp:357
virtual bool OnRightClick(Point pt, int widget)
A click with the right mouse button has been made on the window.
Definition: window_gui.h:629
Company infrastructure overview; Window numbers:
Definition: window_type.h:572
virtual void OnPaint()
The window must be repainted.
Definition: window_gui.h:553
void DeleteNonVitalWindows()
Try to delete a non-vital window.
Definition: window.cpp:3345
Functions related to companies.
WidgetType
Window widget types, nested widget types, and nested widget part types.
Definition: widget_type.h:46
Return or enter key pressed.
Definition: textbuf_type.h:26
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:196
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:262
virtual ~Window()
Remove window and all its child windows from the window stack.
Definition: window.cpp:1066
void ReInitAllWindows()
Re-initialize all windows.
Definition: window.cpp:3434
bool EditBoxInGlobalFocus()
Check if an edit box is in global focus.
Definition: window.cpp:459
Ini file that supports both loading and saving.
Definition: ini_type.h:88
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:795
void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y)
Special handling for the scrollbar widget type.
Definition: widget.cpp:138
Save preset; Window numbers:
Definition: window_type.h:682
GUISettings gui
settings related to the GUI
How much the mouse is allowed to move to call it a double click.
Definition: window.cpp:2798
Align toward the toolbar.
Definition: window_gui.h:158
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:3213
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
virtual const char * GetCaret() const
Get the string at the caret if an edit box has the focus.
Definition: window.cpp:365
Base class for all vehicles.
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:24
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:1739
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
Definition: window.cpp:1486
Time spent drawing world viewports in GUI.
Offset of the caption text at the bottom.
Definition: window_gui.h:130
bool IsWidgetHighlighted(byte widget_index) const
Gets the highlighted status of a widget.
Definition: window.cpp:267
Ships list; Window numbers:
Definition: window_type.h:315
Window * z_front
The window in front of us in z-order.
Definition: window_gui.h:333
uint _toolbar_width
Width of the toolbar, shared by statusbar.
Definition: toolbar_gui.cpp:63
void ProcessScheduledInvalidations()
Process all scheduled invalidations.
Definition: window.cpp:3253
bool _window_highlight_colour
If false, highlight is white, otherwise the by the widget defined colour.
Definition: window.cpp:64
WindowClass parent_cls
Class of the parent window.
Definition: window_gui.h:178
virtual EventState OnCTRLStateChange()
The state of the control key has changed.
Definition: window_gui.h:611
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:423
void UpdatePosition(int difference, ScrollbarStepping unit=SS_SMALL)
Updates the position of the first visible element by the given amount.
Definition: widget_type.h:714
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:52
Last Item. use WIDGETS_END to fill up padding!!
Definition: widget_type.h:72
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:276
virtual void ShowNewGRFInspectWindow() const
Show the NewGRF inspection window.
Definition: window_gui.h:799
This window won&#39;t get focus/make any other window lose focus when click.
Definition: window_gui.h:212
static int _input_events_this_tick
Local counter that is incremented each time an mouse input event is detected.
Definition: window.cpp:2753
static void LoadFromConfig()
Load all WindowDesc settings from _windows_file.
Definition: window.cpp:143
Up-button is lowered bit.
Definition: widget_type.h:262
void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets,...)
Sets the lowered/raised status of a list of widgets.
Definition: window.cpp:539
int index
Index of the nested widget in the widget array of the window (-1 means &#39;not used&#39;).
Definition: widget_type.h:304
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:83
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:408
void Reset()
Reset tile highlighting.
Definition: viewport.cpp:2185
Trains list; Window numbers:
Definition: window_type.h:303
void NetworkDrawChatMessage()
Draw the chat message-box.
Functions related to zooming.
virtual void OnGameTick()
Called once per (game) tick.
Definition: window_gui.h:681
static void HandleScrollbarScrolling(Window *w)
Handle scrollbar scrolling with the mouse.
Definition: window.cpp:2392
void UnfocusFocusedWidget()
Makes no widget on this window have focus.
Definition: window.cpp:472
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:217
Network window; Window numbers:
Definition: window_type.h:468
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:174
uint8 window_soft_limit
soft limit of maximum number of non-stickied non-vital windows (0 = no limit)
void Erase(T *item)
Removes given item from this vector.
bool SetFocusedWidget(int widget_index)
Set focus within this window to the given widget.
Definition: window.cpp:488
bool in_window
mouse inside this window, determines drawing logic
Definition: gfx_type.h:143
Viewport moves with mouse movement on holding right mouse button, cursor position is fixed...
Definition: settings_type.h:76
Do not autoscroll when mouse is at edge of viewport.
Definition: window.cpp:48
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:1128
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:2814
virtual void OnRealtimeTick(uint delta_ms)
Called periodically.
Definition: window_gui.h:691
uint step_width
Step-size of width resize changes.
Definition: window_gui.h:219
HotkeyList * hotkeys
Hotkeys for the window.
Definition: window_gui.h:183
Aircraft list; Window numbers:
Definition: window_type.h:321
int32 z_pos
z coordinate.
Definition: vehicle_base.h:270
virtual void EditBoxLostFocus()
An edit box lost the input focus.
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:59
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:53
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:1568
AI list; Window numbers:
Definition: window_type.h:279
uint8 auto_scrolling
scroll when moving mouse to the edge (see ViewportAutoscrolling)
Definition: settings_type.h:93
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:1429
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:622
int16 pref_width
User-preferred width of the window. Zero if unset.
Definition: window_gui.h:186
Map moves with mouse movement on holding left mouse button, cursor moves.
Definition: settings_type.h:79
Window does not do autoscroll,.
Definition: window_gui.h:241
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:319
Endscreen; Window numbers:
Definition: window_type.h:651
AI settings; Window numbers:
Definition: window_type.h:170
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:1205
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:643
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:619
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
int CheckMatch(uint16 keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition: hotkeys.cpp:301
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.
Definition: window.cpp:1504
Popup with confirm question; Window numbers:
Definition: window_type.h:125
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:35
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:66
void DisableAllWidgetHighlight()
Disable the highlighted status of all widgets.
Definition: window.cpp:214
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:620
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:314
int32 x_pos
x coordinate.
Definition: vehicle_base.h:268
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:1982
virtual void OnResize()
Called after the window got resized.
Definition: window_gui.h:703
NewGRF parameters; Window numbers:
Definition: window_type.h:176
The normal zoom level.
Definition: zoom_type.h:24
static uint GetWindowZPriority(WindowClass wc)
Get the z-priority for a given window.
Definition: window.cpp:1278
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:707
static int PositionWindow(Window *w, WindowClass clss, int setting)
(Re)position a window at the screen.
Definition: window.cpp:3463
WindowClass window_class
Window class.
Definition: window_gui.h:306
virtual void OnMouseWheel(int wheel)
The mouse wheel has been turned.
Definition: window_gui.h:670
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3075
Specification of a rectangle with absolute coordinates of all edges.
Vertical scrollbar.
Definition: widget_type.h:84
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:519
The passed event is handled.
Definition: window_type.h:714
int32 y_pos
y coordinate.
Definition: vehicle_base.h:269
Text is written right-to-left by default.
Definition: strings_type.h:26
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:307
bool _scrolling_viewport
A viewport is being scrolled with the mouse.
Definition: window.cpp:79
Functions related to tile highlights.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
void ResetWindowSystem()
Reset the windowing system, by means of shutting it down followed by re-initialization.
Definition: window.cpp:1916
static Window * _last_scroll_window
Window of the last scroll event.
Definition: window.cpp:56
Window functions not directly related to making/drawing windows.
Point delta
relative mouse movement in this tick
Definition: gfx_type.h:120
int top
Screen coordinate top edge of the viewport.
Definition: viewport_type.h:26
static uint Ceil(uint a, uint b)
Computes ceil(a / b) * b for non-negative a and b.
Definition: math_func.hpp:327
Resize the nested widget tree.
Definition: widget_type.h:113
Find a place automatically.
Definition: window_gui.h:156
void CallWindowRealtimeTickEvent(uint delta_ms)
Dispatch OnRealtimeTick event over all windows.
Definition: window.cpp:3099
Manually align the window (so no automatic location finding)
Definition: window_gui.h:155
NWidgetDisplay disp_flags
Flags that affect display and interaction with the widget.
Definition: widget_type.h:302
virtual void OnHundredthTick()
Called once every 100 (game) ticks.
Definition: window_gui.h:686
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:284
SmallVector< SpriteID, 256 > sprites
Sprites found.
Definition: newgrf_debug.h:32
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:321
void IniSaveWindowSettings(IniFile *ini, const char *grpname, void *desc)
Save a WindowDesc to config.
Definition: settings.cpp:755
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3227
Functions related to news.
uint8 timeout_timer
Timer value of the WF_TIMEOUT for flags.
Definition: window_gui.h:309
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:392
static void QSortT(T *base, uint num, int(CDECL *comparator)(const T *, const T *), bool desc=false)
Type safe qsort()
Definition: sort_func.hpp:28
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:832
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2124
Company view; Window numbers:
Definition: window_type.h:364
virtual void SetDirty(const Window *w) const
Mark the widget as &#39;dirty&#39; (in need of repaint).
Definition: widget.cpp:775
Window white border counter bit mask.
Definition: window_gui.h:242
WindowPosition
How do we the window to be placed?
Definition: window_gui.h:154
NWidgetBase * nested_root
Root of the nested tree.
Definition: window_gui.h:324
Query string window; Window numbers:
Definition: window_type.h:118
Bit value of the &#39;scrollbar down&#39; flag.
Definition: widget_type.h:275
NewGrfDebugSpritePickerMode mode
Current state.
Definition: newgrf_debug.h:29
Window * BringWindowToFrontById(WindowClass cls, WindowNumber number)
Find a window and make it the relative top-window on the screen.
Definition: window.cpp:1241
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:64
Factory to &#39;query&#39; all available blitters.
Game options window; Window numbers:
Definition: window_type.h:608
Ok key.
Definition: osk_widget.h:20
WindowDesc * window_desc
Window description.
Definition: window_gui.h:304
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:3240
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:23
Textbuf content changed.
Definition: textbuf_type.h:24
char * _windows_file
Config file to store WindowDesc.
Definition: window.cpp:91
An invalid owner.
Definition: company_type.h:31
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:330
static void AddWindowToZOrdering(Window *w)
Adds a window to the z-ordering, according to its z-priority.
Definition: window.cpp:1356
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:3299
Drop down menu; Window numbers:
Definition: window_type.h:151
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:315
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1463
News window; Window numbers:
Definition: window_type.h:243
virtual void OnInvalidateData(int data=0, bool gui_scope=true)
Some data on this window has become invalid.
Definition: window_gui.h:733
void ProcessHighlightedInvalidations()
Process all invalidation of highlighted widgets.
Definition: window.cpp:3264
void UpdateViewportPosition(Window *w)
Update the viewport position being displayed.
Definition: viewport.cpp:1667
static bool MayBeShown(const Window *w)
Returns whether a window may be shown or not.
Definition: window.cpp:853
void IConsoleClose()
Close the in-game console.
#define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)
Iterate over all windows.
Definition: window_gui.h:886
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:631
Stuff related to the (main) toolbar.
Base class for a &#39;real&#39; widget.
Definition: widget_type.h:284
void ShowFirstError()
Show the first error of the queue.
Definition: error_gui.cpp:343
int width
Screen width of the viewport.
Definition: viewport_type.h:27