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  Point pt = { x, y };
774 
775  /* No widget to handle, or the window is not interested in it. */
776  if (wid->index >= 0) {
777  if (w->OnRightClick(pt, wid->index)) return;
778  }
779 
780  /* Right-click close is enabled and there is a closebox */
782  delete w;
783  } else if (_settings_client.gui.hover_delay_ms == 0 && !w->OnTooltip(pt, wid->index, TCC_RIGHT_CLICK) && wid->tool_tip != 0) {
784  GuiShowTooltips(w, wid->tool_tip, 0, NULL, TCC_RIGHT_CLICK);
785  }
786 }
787 
794 static void DispatchHoverEvent(Window *w, int x, int y)
795 {
796  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
797 
798  /* No widget to handle */
799  if (wid == NULL) return;
800 
801  Point pt = { x, y };
802 
803  /* Show the tooltip if there is any */
804  if (!w->OnTooltip(pt, wid->index, TCC_HOVER) && wid->tool_tip != 0) {
805  GuiShowTooltips(w, wid->tool_tip);
806  return;
807  }
808 
809  /* Widget has no index, so the window is not interested in it. */
810  if (wid->index < 0) return;
811 
812  w->OnHover(pt, wid->index);
813 }
814 
822 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
823 {
824  if (nwid == NULL) return;
825 
826  /* Using wheel on caption/shade-box shades or unshades the window. */
827  if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
828  w->SetShaded(wheel < 0);
829  return;
830  }
831 
832  /* Wheeling a vertical scrollbar. */
833  if (nwid->type == NWID_VSCROLLBAR) {
834  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
835  if (sb->GetCount() > sb->GetCapacity()) {
836  sb->UpdatePosition(wheel);
837  w->SetDirty();
838  }
839  return;
840  }
841 
842  /* Scroll the widget attached to the scrollbar. */
843  Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : NULL);
844  if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
845  sb->UpdatePosition(wheel);
846  w->SetDirty();
847  }
848 }
849 
855 static bool MayBeShown(const Window *w)
856 {
857  /* If we're not modal, everything is okay. */
858  if (!HasModalProgress()) return true;
859 
860  switch (w->window_class) {
861  case WC_MAIN_WINDOW:
862  case WC_MODAL_PROGRESS:
864  return true;
865 
866  default:
867  return false;
868  }
869 }
870 
883 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
884 {
885  const Window *v;
887  if (MayBeShown(v) &&
888  right > v->left &&
889  bottom > v->top &&
890  left < v->left + v->width &&
891  top < v->top + v->height) {
892  /* v and rectangle intersect with each other */
893  int x;
894 
895  if (left < (x = v->left)) {
896  DrawOverlappedWindow(w, left, top, x, bottom);
897  DrawOverlappedWindow(w, x, top, right, bottom);
898  return;
899  }
900 
901  if (right > (x = v->left + v->width)) {
902  DrawOverlappedWindow(w, left, top, x, bottom);
903  DrawOverlappedWindow(w, x, top, right, bottom);
904  return;
905  }
906 
907  if (top < (x = v->top)) {
908  DrawOverlappedWindow(w, left, top, right, x);
909  DrawOverlappedWindow(w, left, x, right, bottom);
910  return;
911  }
912 
913  if (bottom > (x = v->top + v->height)) {
914  DrawOverlappedWindow(w, left, top, right, x);
915  DrawOverlappedWindow(w, left, x, right, bottom);
916  return;
917  }
918 
919  return;
920  }
921  }
922 
923  /* Setup blitter, and dispatch a repaint event to window *wz */
924  DrawPixelInfo *dp = _cur_dpi;
925  dp->width = right - left;
926  dp->height = bottom - top;
927  dp->left = left - w->left;
928  dp->top = top - w->top;
929  dp->pitch = _screen.pitch;
930  dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
931  dp->zoom = ZOOM_LVL_NORMAL;
932  w->OnPaint();
933 }
934 
943 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
944 {
945  Window *w;
946 
947  DrawPixelInfo *old_dpi = _cur_dpi;
948  DrawPixelInfo bk;
949  _cur_dpi = &bk;
950 
951  FOR_ALL_WINDOWS_FROM_BACK(w) {
952  if (MayBeShown(w) &&
953  right > w->left &&
954  bottom > w->top &&
955  left < w->left + w->width &&
956  top < w->top + w->height) {
957  /* Window w intersects with the rectangle => needs repaint */
958  DrawOverlappedWindow(w, max(left, w->left), max(top, w->top), min(right, w->left + w->width), min(bottom, w->top + w->height));
959  }
960  }
961  _cur_dpi = old_dpi;
962 }
963 
968 void Window::SetDirty() const
969 {
970  SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
971 }
972 
979 void Window::ReInit(int rx, int ry)
980 {
981  this->SetDirty(); // Mark whole current window as dirty.
982 
983  /* Save current size. */
984  int window_width = this->width;
985  int window_height = this->height;
986 
987  this->OnInit();
988  /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
989  this->nested_root->SetupSmallestSize(this, false);
990  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
991  this->width = this->nested_root->smallest_x;
992  this->height = this->nested_root->smallest_y;
993  this->resize.step_width = this->nested_root->resize_x;
994  this->resize.step_height = this->nested_root->resize_y;
995 
996  /* Resize as close to the original size + requested resize as possible. */
997  window_width = max(window_width + rx, this->width);
998  window_height = max(window_height + ry, this->height);
999  int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
1000  int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
1001  /* dx and dy has to go by step.. calculate it.
1002  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
1003  if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
1004  if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
1005 
1006  ResizeWindow(this, dx, dy);
1007  /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
1008 }
1009 
1015 void Window::SetShaded(bool make_shaded)
1016 {
1017  if (this->shade_select == NULL) return;
1018 
1019  int desired = make_shaded ? SZSP_HORIZONTAL : 0;
1020  if (this->shade_select->shown_plane != desired) {
1021  if (make_shaded) {
1022  if (this->nested_focus != NULL) this->UnfocusFocusedWidget();
1023  this->unshaded_size.width = this->width;
1024  this->unshaded_size.height = this->height;
1025  this->shade_select->SetDisplayedPlane(desired);
1026  this->ReInit(0, -this->height);
1027  } else {
1028  this->shade_select->SetDisplayedPlane(desired);
1029  int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
1030  int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
1031  this->ReInit(dx, dy);
1032  }
1033  }
1034 }
1035 
1043 {
1044  Window *v;
1045  FOR_ALL_WINDOWS_FROM_BACK(v) {
1046  if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
1047  }
1048 
1049  return NULL;
1050 }
1051 
1057 {
1058  Window *child = FindChildWindow(this, wc);
1059  while (child != NULL) {
1060  delete child;
1061  child = FindChildWindow(this, wc);
1062  }
1063 }
1064 
1069 {
1070  if (_thd.window_class == this->window_class &&
1071  _thd.window_number == this->window_number) {
1073  }
1074 
1075  /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
1076  if (_mouseover_last_w == this) _mouseover_last_w = NULL;
1077 
1078  /* We can't scroll the window when it's closed. */
1079  if (_last_scroll_window == this) _last_scroll_window = NULL;
1080 
1081  /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
1082  if (_focused_window == this) {
1083  this->OnFocusLost();
1084  _focused_window = NULL;
1085  }
1086 
1087  this->DeleteChildWindows();
1088 
1089  if (this->viewport != NULL) DeleteWindowViewport(this);
1090 
1091  this->SetDirty();
1092 
1093  free(this->nested_array); // Contents is released through deletion of #nested_root.
1094  delete this->nested_root;
1095 
1096  /*
1097  * Make fairly sure that this is written, and not "optimized" away.
1098  * The delete operator is overwritten to not delete it; the deletion
1099  * happens at a later moment in time after the window has been
1100  * removed from the list of windows to prevent issues with items
1101  * being removed during the iteration as not one but more windows
1102  * may be removed by a single call to ~Window by means of the
1103  * DeleteChildWindows function.
1104  */
1105  const_cast<volatile WindowClass &>(this->window_class) = WC_INVALID;
1106 }
1107 
1115 {
1116  Window *w;
1117  FOR_ALL_WINDOWS_FROM_BACK(w) {
1118  if (w->window_class == cls && w->window_number == number) return w;
1119  }
1120 
1121  return NULL;
1122 }
1123 
1131 {
1132  Window *w;
1133  FOR_ALL_WINDOWS_FROM_BACK(w) {
1134  if (w->window_class == cls) return w;
1135  }
1136 
1137  return NULL;
1138 }
1139 
1147 {
1148  Window *w = FindWindowById(cls, number);
1149  if (force || w == NULL ||
1150  (w->flags & WF_STICKY) == 0) {
1151  delete w;
1152  }
1153 }
1154 
1160 {
1161  Window *w;
1162 
1163 restart_search:
1164  /* When we find the window to delete, we need to restart the search
1165  * as deleting this window could cascade in deleting (many) others
1166  * anywhere in the z-array */
1167  FOR_ALL_WINDOWS_FROM_BACK(w) {
1168  if (w->window_class == cls) {
1169  delete w;
1170  goto restart_search;
1171  }
1172  }
1173 }
1174 
1182 {
1183  Window *w;
1184 
1185 restart_search:
1186  /* When we find the window to delete, we need to restart the search
1187  * as deleting this window could cascade in deleting (many) others
1188  * anywhere in the z-array */
1189  FOR_ALL_WINDOWS_FROM_BACK(w) {
1190  if (w->owner == id) {
1191  delete w;
1192  goto restart_search;
1193  }
1194  }
1195 
1196  /* Also delete the company specific windows that don't have a company-colour. */
1198 }
1199 
1207 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1208 {
1209  Window *w;
1210  FOR_ALL_WINDOWS_FROM_BACK(w) {
1211  if (w->owner != old_owner) continue;
1212 
1213  switch (w->window_class) {
1214  case WC_COMPANY_COLOUR:
1215  case WC_FINANCES:
1216  case WC_STATION_LIST:
1217  case WC_TRAINS_LIST:
1218  case WC_ROADVEH_LIST:
1219  case WC_SHIPS_LIST:
1220  case WC_AIRCRAFT_LIST:
1221  case WC_BUY_COMPANY:
1222  case WC_COMPANY:
1224  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().
1225  continue;
1226 
1227  default:
1228  w->owner = new_owner;
1229  break;
1230  }
1231  }
1232 }
1233 
1234 static void BringWindowToFront(Window *w);
1235 
1244 {
1245  Window *w = FindWindowById(cls, number);
1246 
1247  if (w != NULL) {
1248  if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1249 
1250  w->SetWhiteBorder();
1251  BringWindowToFront(w);
1252  w->SetDirty();
1253  }
1254 
1255  return w;
1256 }
1257 
1258 static inline bool IsVitalWindow(const Window *w)
1259 {
1260  switch (w->window_class) {
1261  case WC_MAIN_TOOLBAR:
1262  case WC_STATUS_BAR:
1263  case WC_NEWS_WINDOW:
1264  case WC_SEND_NETWORK_MSG:
1265  return true;
1266 
1267  default:
1268  return false;
1269  }
1270 }
1271 
1281 {
1282  assert(wc != WC_INVALID);
1283 
1284  uint z_priority = 0;
1285 
1286  switch (wc) {
1287  case WC_ENDSCREEN:
1288  ++z_priority;
1289  FALLTHROUGH;
1290 
1291  case WC_HIGHSCORE:
1292  ++z_priority;
1293  FALLTHROUGH;
1294 
1295  case WC_TOOLTIPS:
1296  ++z_priority;
1297  FALLTHROUGH;
1298 
1299  case WC_DROPDOWN_MENU:
1300  ++z_priority;
1301  FALLTHROUGH;
1302 
1303  case WC_MAIN_TOOLBAR:
1304  case WC_STATUS_BAR:
1305  ++z_priority;
1306  FALLTHROUGH;
1307 
1308  case WC_OSK:
1309  ++z_priority;
1310  FALLTHROUGH;
1311 
1312  case WC_QUERY_STRING:
1313  case WC_SEND_NETWORK_MSG:
1314  ++z_priority;
1315  FALLTHROUGH;
1316 
1317  case WC_ERRMSG:
1319  case WC_MODAL_PROGRESS:
1321  case WC_SAVE_PRESET:
1322  ++z_priority;
1323  FALLTHROUGH;
1324 
1325  case WC_GENERATE_LANDSCAPE:
1326  case WC_SAVELOAD:
1327  case WC_GAME_OPTIONS:
1328  case WC_CUSTOM_CURRENCY:
1329  case WC_NETWORK_WINDOW:
1330  case WC_GRF_PARAMETERS:
1331  case WC_AI_LIST:
1332  case WC_AI_SETTINGS:
1333  case WC_TEXTFILE:
1334  ++z_priority;
1335  FALLTHROUGH;
1336 
1337  case WC_CONSOLE:
1338  ++z_priority;
1339  FALLTHROUGH;
1340 
1341  case WC_NEWS_WINDOW:
1342  ++z_priority;
1343  FALLTHROUGH;
1344 
1345  default:
1346  ++z_priority;
1347  FALLTHROUGH;
1348 
1349  case WC_MAIN_WINDOW:
1350  return z_priority;
1351  }
1352 }
1353 
1359 {
1360  assert(w->z_front == NULL && w->z_back == NULL);
1361 
1362  if (_z_front_window == NULL) {
1363  /* It's the only window. */
1364  _z_front_window = _z_back_window = w;
1365  w->z_front = w->z_back = NULL;
1366  } else {
1367  /* Search down the z-ordering for its location. */
1368  Window *v = _z_front_window;
1369  uint last_z_priority = UINT_MAX;
1370  while (v != NULL && (v->window_class == WC_INVALID || GetWindowZPriority(v->window_class) > GetWindowZPriority(w->window_class))) {
1371  if (v->window_class != WC_INVALID) {
1372  /* Sanity check z-ordering, while we're at it. */
1373  assert(last_z_priority >= GetWindowZPriority(v->window_class));
1374  last_z_priority = GetWindowZPriority(v->window_class);
1375  }
1376 
1377  v = v->z_back;
1378  }
1379 
1380  if (v == NULL) {
1381  /* It's the new back window. */
1382  w->z_front = _z_back_window;
1383  w->z_back = NULL;
1384  _z_back_window->z_back = w;
1385  _z_back_window = w;
1386  } else if (v == _z_front_window) {
1387  /* It's the new front window. */
1388  w->z_front = NULL;
1389  w->z_back = _z_front_window;
1390  _z_front_window->z_front = w;
1391  _z_front_window = w;
1392  } else {
1393  /* It's somewhere else in the z-ordering. */
1394  w->z_front = v->z_front;
1395  w->z_back = v;
1396  v->z_front->z_back = w;
1397  v->z_front = w;
1398  }
1399  }
1400 }
1401 
1402 
1408 {
1409  if (w->z_front == NULL) {
1410  assert(_z_front_window == w);
1411  _z_front_window = w->z_back;
1412  } else {
1413  w->z_front->z_back = w->z_back;
1414  }
1415 
1416  if (w->z_back == NULL) {
1417  assert(_z_back_window == w);
1418  _z_back_window = w->z_front;
1419  } else {
1420  w->z_back->z_front = w->z_front;
1421  }
1422 
1423  w->z_front = w->z_back = NULL;
1424 }
1425 
1432 {
1435 
1436  w->SetDirty();
1437 }
1438 
1447 {
1448  /* Set up window properties; some of them are needed to set up smallest size below */
1449  this->window_class = this->window_desc->cls;
1450  this->SetWhiteBorder();
1451  if (this->window_desc->default_pos == WDP_CENTER) this->flags |= WF_CENTERED;
1452  this->owner = INVALID_OWNER;
1453  this->nested_focus = NULL;
1454  this->window_number = window_number;
1455 
1456  this->OnInit();
1457  /* Initialize nested widget tree. */
1458  if (this->nested_array == NULL) {
1459  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1460  this->nested_root->SetupSmallestSize(this, true);
1461  } else {
1462  this->nested_root->SetupSmallestSize(this, false);
1463  }
1464  /* Initialize to smallest size. */
1465  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1466 
1467  /* Further set up window properties,
1468  * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1469  this->resize.step_width = this->nested_root->resize_x;
1470  this->resize.step_height = this->nested_root->resize_y;
1471 
1472  /* Give focus to the opened window unless a text box
1473  * of focused window has focus (so we don't interrupt typing). But if the new
1474  * window has a text box, then take focus anyway. */
1475  if (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != NULL) SetFocusedWindow(this);
1476 
1477  /* Insert the window into the correct location in the z-ordering. */
1478  AddWindowToZOrdering(this);
1479 }
1480 
1488 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1489 {
1490  this->left = x;
1491  this->top = y;
1492  this->width = sm_width;
1493  this->height = sm_height;
1494 }
1495 
1506 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
1507 {
1508  def_width = max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1509  def_height = max(def_height, this->height);
1510  /* Try to make windows smaller when our window is too small.
1511  * w->(width|height) is normally the same as min_(width|height),
1512  * but this way the GUIs can be made a little more dynamic;
1513  * one can use the same spec for multiple windows and those
1514  * can then determine the real minimum size of the window. */
1515  if (this->width != def_width || this->height != def_height) {
1516  /* Think about the overlapping toolbars when determining the minimum window size */
1517  int free_height = _screen.height;
1518  const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1519  if (wt != NULL) free_height -= wt->height;
1521  if (wt != NULL) free_height -= wt->height;
1522 
1523  int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
1524  int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
1525 
1526  /* X and Y has to go by step.. calculate it.
1527  * The cast to int is necessary else x/y are implicitly casted to
1528  * unsigned int, which won't work. */
1529  if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1530  if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1531 
1532  ResizeWindow(this, enlarge_x, enlarge_y);
1533  /* ResizeWindow() calls this->OnResize(). */
1534  } else {
1535  /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1536  this->OnResize();
1537  }
1538 
1539  int nx = this->left;
1540  int ny = this->top;
1541 
1542  if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1543 
1544  const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1545  ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
1546  nx = max(nx, 0);
1547 
1548  if (this->viewport != NULL) {
1549  this->viewport->left += nx - this->left;
1550  this->viewport->top += ny - this->top;
1551  }
1552  this->left = nx;
1553  this->top = ny;
1554 
1555  this->SetDirty();
1556 }
1557 
1570 static bool IsGoodAutoPlace1(int left, int top, int width, int height, int toolbar_y, Point &pos)
1571 {
1572  int right = width + left;
1573  int bottom = height + top;
1574 
1575  if (left < 0 || top < toolbar_y || right > _screen.width || bottom > _screen.height) return false;
1576 
1577  /* Make sure it is not obscured by any window. */
1578  const Window *w;
1579  FOR_ALL_WINDOWS_FROM_BACK(w) {
1580  if (w->window_class == WC_MAIN_WINDOW) continue;
1581 
1582  if (right > w->left &&
1583  w->left + w->width > left &&
1584  bottom > w->top &&
1585  w->top + w->height > top) {
1586  return false;
1587  }
1588  }
1589 
1590  pos.x = left;
1591  pos.y = top;
1592  return true;
1593 }
1594 
1607 static bool IsGoodAutoPlace2(int left, int top, int width, int height, int toolbar_y, Point &pos)
1608 {
1609  bool rtl = _current_text_dir == TD_RTL;
1610 
1611  /* Left part of the rectangle may be at most 1/4 off-screen,
1612  * right part of the rectangle may be at most 1/2 off-screen
1613  */
1614  if (rtl) {
1615  if (left < -(width >> 1) || left > _screen.width - (width >> 2)) return false;
1616  } else {
1617  if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1618  }
1619 
1620  /* Bottom part of the rectangle may be at most 1/4 off-screen */
1621  if (top < toolbar_y || top > _screen.height - (height >> 2)) return false;
1622 
1623  /* Make sure it is not obscured by any window. */
1624  const Window *w;
1625  FOR_ALL_WINDOWS_FROM_BACK(w) {
1626  if (w->window_class == WC_MAIN_WINDOW) continue;
1627 
1628  if (left + width > w->left &&
1629  w->left + w->width > left &&
1630  top + height > w->top &&
1631  w->top + w->height > top) {
1632  return false;
1633  }
1634  }
1635 
1636  pos.x = left;
1637  pos.y = top;
1638  return true;
1639 }
1640 
1647 static Point GetAutoPlacePosition(int width, int height)
1648 {
1649  Point pt;
1650 
1651  bool rtl = _current_text_dir == TD_RTL;
1652 
1653  /* First attempt, try top-left of the screen */
1654  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1655  const int toolbar_y = main_toolbar != NULL ? main_toolbar->height : 0;
1656  if (IsGoodAutoPlace1(rtl ? _screen.width - width : 0, toolbar_y, width, height, toolbar_y, pt)) return pt;
1657 
1658  /* Second attempt, try around all existing windows.
1659  * The new window must be entirely on-screen, and not overlap with an existing window.
1660  * Eight starting points are tried, two at each corner.
1661  */
1662  const Window *w;
1663  FOR_ALL_WINDOWS_FROM_BACK(w) {
1664  if (w->window_class == WC_MAIN_WINDOW) continue;
1665 
1666  if (IsGoodAutoPlace1(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1667  if (IsGoodAutoPlace1(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1668  if (IsGoodAutoPlace1(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1669  if (IsGoodAutoPlace1(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1670  if (IsGoodAutoPlace1(w->left + w->width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1671  if (IsGoodAutoPlace1(w->left - width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1672  if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1673  if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height, width, height, toolbar_y, pt)) return pt;
1674  }
1675 
1676  /* Third attempt, try around all existing windows.
1677  * The new window may be partly off-screen, and must not overlap with an existing window.
1678  * Only four starting points are tried.
1679  */
1680  FOR_ALL_WINDOWS_FROM_BACK(w) {
1681  if (w->window_class == WC_MAIN_WINDOW) continue;
1682 
1683  if (IsGoodAutoPlace2(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1684  if (IsGoodAutoPlace2(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1685  if (IsGoodAutoPlace2(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1686  if (IsGoodAutoPlace2(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1687  }
1688 
1689  /* Fourth and final attempt, put window at diagonal starting from (0, toolbar_y), try multiples
1690  * of the closebox
1691  */
1692  int left = rtl ? _screen.width - width : 0, top = toolbar_y;
1693  int offset_x = rtl ? -(int)NWidgetLeaf::closebox_dimension.width : (int)NWidgetLeaf::closebox_dimension.width;
1695 
1696 restart:
1697  FOR_ALL_WINDOWS_FROM_BACK(w) {
1698  if (w->left == left && w->top == top) {
1699  left += offset_x;
1700  top += offset_y;
1701  goto restart;
1702  }
1703  }
1704 
1705  pt.x = left;
1706  pt.y = top;
1707  return pt;
1708 }
1709 
1717 {
1718  const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1719  assert(w != NULL);
1720  Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1721  return pt;
1722 }
1723 
1741 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1742 {
1743  Point pt;
1744  const Window *w;
1745 
1746  int16 default_width = max(desc->GetDefaultWidth(), sm_width);
1747  int16 default_height = max(desc->GetDefaultHeight(), sm_height);
1748 
1749  if (desc->parent_cls != WC_NONE && (w = FindWindowById(desc->parent_cls, window_number)) != NULL) {
1750  bool rtl = _current_text_dir == TD_RTL;
1751  if (desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) {
1752  pt.x = w->left + (rtl ? w->width - default_width : 0);
1753  pt.y = w->top + w->height;
1754  return pt;
1755  } else {
1756  /* Position child window with offset of closebox, but make sure that either closebox or resizebox is visible
1757  * - Y position: closebox of parent + closebox of child + statusbar
1758  * - X position: closebox on left/right, resizebox on right/left (depending on ltr/rtl)
1759  */
1761  if (w->top + 3 * indent_y < _screen.height) {
1762  pt.y = w->top + indent_y;
1763  int indent_close = NWidgetLeaf::closebox_dimension.width;
1764  int indent_resize = NWidgetLeaf::resizebox_dimension.width;
1765  if (_current_text_dir == TD_RTL) {
1766  pt.x = max(w->left + w->width - default_width - indent_close, 0);
1767  if (pt.x + default_width >= indent_close && pt.x + indent_resize <= _screen.width) return pt;
1768  } else {
1769  pt.x = min(w->left + indent_close, _screen.width - default_width);
1770  if (pt.x + default_width >= indent_resize && pt.x + indent_close <= _screen.width) return pt;
1771  }
1772  }
1773  }
1774  }
1775 
1776  switch (desc->default_pos) {
1777  case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1778  return GetToolbarAlignedWindowPosition(default_width);
1779 
1780  case WDP_AUTO: // Find a good automatic position for the window
1781  return GetAutoPlacePosition(default_width, default_height);
1782 
1783  case WDP_CENTER: // Centre the window horizontally
1784  pt.x = (_screen.width - default_width) / 2;
1785  pt.y = (_screen.height - default_height) / 2;
1786  break;
1787 
1788  case WDP_MANUAL:
1789  pt.x = 0;
1790  pt.y = 0;
1791  break;
1792 
1793  default:
1794  NOT_REACHED();
1795  }
1796 
1797  return pt;
1798 }
1799 
1800 /* virtual */ Point Window::OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1801 {
1802  return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1803 }
1804 
1812 void Window::CreateNestedTree(bool fill_nested)
1813 {
1814  int biggest_index = -1;
1815  this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_parts, this->window_desc->nwid_length, &biggest_index, &this->shade_select);
1816  this->nested_array_size = (uint)(biggest_index + 1);
1817 
1818  if (fill_nested) {
1819  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1820  this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
1821  }
1822 }
1823 
1829 {
1830  this->InitializeData(window_number);
1831  this->ApplyDefaults();
1832  Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1833  this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1834  this->FindWindowPlacementAndResize(this->window_desc->GetDefaultWidth(), this->window_desc->GetDefaultHeight());
1835 }
1836 
1842 {
1843  this->CreateNestedTree(false);
1844  this->FinishInitNested(window_number);
1845 }
1846 
1851 Window::Window(WindowDesc *desc) : window_desc(desc), mouse_capture_widget(-1)
1852 {
1853 }
1854 
1862 Window *FindWindowFromPt(int x, int y)
1863 {
1864  Window *w;
1865  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1866  if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1867  return w;
1868  }
1869  }
1870 
1871  return NULL;
1872 }
1873 
1878 {
1879  IConsoleClose();
1880 
1881  _z_back_window = NULL;
1882  _z_front_window = NULL;
1883  _focused_window = NULL;
1884  _mouseover_last_w = NULL;
1885  _last_scroll_window = NULL;
1886  _scrolling_viewport = false;
1887  _mouse_hovering = false;
1888 
1889  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1890  NWidgetScrollbar::InvalidateDimensionCache();
1891 
1892  ShowFirstError();
1893 }
1894 
1899 {
1901 
1902  Window *w;
1903  FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
1904 
1905  for (w = _z_front_window; w != NULL; /* nothing */) {
1906  Window *to_del = w;
1907  w = w->z_back;
1908  free(to_del);
1909  }
1910 
1911  _z_front_window = NULL;
1912  _z_back_window = NULL;
1913 }
1914 
1919 {
1921  InitWindowSystem();
1922  _thd.Reset();
1923 }
1924 
1925 static void DecreaseWindowCounters()
1926 {
1927  if (_scroller_click_timeout != 0) _scroller_click_timeout--;
1928 
1929  Window *w;
1930  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1931  if (_scroller_click_timeout == 0) {
1932  /* Unclick scrollbar buttons if they are pressed. */
1933  for (uint i = 0; i < w->nested_array_size; i++) {
1934  NWidgetBase *nwid = w->nested_array[i];
1935  if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
1936  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1939  w->mouse_capture_widget = -1;
1940  sb->SetDirty(w);
1941  }
1942  }
1943  }
1944  }
1945 
1946  /* Handle editboxes */
1947  for (SmallMap<int, QueryString*>::Pair *it = w->querystrings.Begin(); it != w->querystrings.End(); ++it) {
1948  it->second->HandleEditBox(w, it->first);
1949  }
1950 
1951  w->OnMouseLoop();
1952  }
1953 
1954  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1955  if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) {
1956  CLRBITS(w->flags, WF_TIMEOUT);
1957 
1958  w->OnTimeout();
1959  w->RaiseButtons(true);
1960  }
1961  }
1962 }
1963 
1964 static void HandlePlacePresize()
1965 {
1966  if (_special_mouse_mode != WSM_PRESIZE) return;
1967 
1968  Window *w = _thd.GetCallbackWnd();
1969  if (w == NULL) return;
1970 
1971  Point pt = GetTileBelowCursor();
1972  if (pt.x == -1) {
1973  _thd.selend.x = -1;
1974  return;
1975  }
1976 
1977  w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1978 }
1979 
1985 {
1987 
1988  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
1989 
1990  Window *w = _thd.GetCallbackWnd();
1991  if (w != NULL) {
1992  /* Send an event in client coordinates. */
1993  Point pt;
1994  pt.x = _cursor.pos.x - w->left;
1995  pt.y = _cursor.pos.y - w->top;
1996  if (_left_button_down) {
1997  w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
1998  } else {
1999  w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
2000  }
2001  }
2002 
2003  if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
2004  return ES_HANDLED;
2005 }
2006 
2008 static void HandleMouseOver()
2009 {
2010  Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2011 
2012  /* We changed window, put an OnMouseOver event to the last window */
2013  if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
2014  /* Reset mouse-over coordinates of previous window */
2015  Point pt = { -1, -1 };
2016  _mouseover_last_w->OnMouseOver(pt, 0);
2017  }
2018 
2019  /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
2020  _mouseover_last_w = w;
2021 
2022  if (w != NULL) {
2023  /* send an event in client coordinates. */
2024  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2025  const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
2026  if (widget != NULL) w->OnMouseOver(pt, widget->index);
2027  }
2028 }
2029 
2031 static const int MIN_VISIBLE_TITLE_BAR = 13;
2032 
2037 };
2038 
2049 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
2050 {
2051  if (v == NULL) return;
2052 
2053  int v_bottom = v->top + v->height;
2054  int v_right = v->left + v->width;
2055  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.
2056 
2057  if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
2058  if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
2059 
2060  /* Vertically, the rectangle is hidden behind v. */
2061  if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
2062  if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
2063  return;
2064  }
2065  if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
2066  if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
2067  return;
2068  }
2069 
2070  /* Horizontally also hidden, force movement to a safe area. */
2071  if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
2072  *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
2073  } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
2074  *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2075  } else {
2076  *ny = safe_y;
2077  }
2078 }
2079 
2087 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2088 {
2089  /* Search for the title bar rectangle. */
2090  Rect caption_rect;
2091  const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2092  if (caption != NULL) {
2093  caption_rect.left = caption->pos_x;
2094  caption_rect.right = caption->pos_x + caption->current_x;
2095  caption_rect.top = caption->pos_y;
2096  caption_rect.bottom = caption->pos_y + caption->current_y;
2097 
2098  /* Make sure the window doesn't leave the screen */
2099  nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2100  ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2101 
2102  /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2103  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2104  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2105  }
2106 
2107  if (w->viewport != NULL) {
2108  w->viewport->left += nx - w->left;
2109  w->viewport->top += ny - w->top;
2110  }
2111 
2112  w->left = nx;
2113  w->top = ny;
2114 }
2115 
2126 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
2127 {
2128  if (delta_x != 0 || delta_y != 0) {
2129  if (clamp_to_screen) {
2130  /* Determine the new right/bottom position. If that is outside of the bounds of
2131  * the resolution clamp it in such a manner that it stays within the bounds. */
2132  int new_right = w->left + w->width + delta_x;
2133  int new_bottom = w->top + w->height + delta_y;
2134  if (new_right >= (int)_cur_resolution.width) delta_x -= Ceil(new_right - _cur_resolution.width, max(1U, w->nested_root->resize_x));
2135  if (new_bottom >= (int)_cur_resolution.height) delta_y -= Ceil(new_bottom - _cur_resolution.height, max(1U, w->nested_root->resize_y));
2136  }
2137 
2138  w->SetDirty();
2139 
2140  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);
2141  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);
2142  assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2143  assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2144 
2146  w->width = w->nested_root->current_x;
2147  w->height = w->nested_root->current_y;
2148  }
2149 
2150  EnsureVisibleCaption(w, w->left, w->top);
2151 
2152  /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
2153  w->OnResize();
2154  w->SetDirty();
2155 }
2156 
2163 {
2165  return (w == NULL) ? 0 : w->top + w->height;
2166 }
2167 
2174 {
2176  return (w == NULL) ? _screen.height : w->top;
2177 }
2178 
2179 static bool _dragging_window;
2180 
2186 {
2187  /* Get out immediately if no window is being dragged at all. */
2188  if (!_dragging_window) return ES_NOT_HANDLED;
2189 
2190  /* If button still down, but cursor hasn't moved, there is nothing to do. */
2191  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2192 
2193  /* Otherwise find the window... */
2194  Window *w;
2195  FOR_ALL_WINDOWS_FROM_BACK(w) {
2196  if (w->flags & WF_DRAGGING) {
2197  /* Stop the dragging if the left mouse button was released */
2198  if (!_left_button_down) {
2199  w->flags &= ~WF_DRAGGING;
2200  break;
2201  }
2202 
2203  w->SetDirty();
2204 
2205  int x = _cursor.pos.x + _drag_delta.x;
2206  int y = _cursor.pos.y + _drag_delta.y;
2207  int nx = x;
2208  int ny = y;
2209 
2211  const Window *v;
2212 
2215  int delta;
2216 
2217  FOR_ALL_WINDOWS_FROM_BACK(v) {
2218  if (v == w) continue; // Don't snap at yourself
2219 
2220  if (y + w->height > v->top && y < v->top + v->height) {
2221  /* Your left border <-> other right border */
2222  delta = abs(v->left + v->width - x);
2223  if (delta <= hsnap) {
2224  nx = v->left + v->width;
2225  hsnap = delta;
2226  }
2227 
2228  /* Your right border <-> other left border */
2229  delta = abs(v->left - x - w->width);
2230  if (delta <= hsnap) {
2231  nx = v->left - w->width;
2232  hsnap = delta;
2233  }
2234  }
2235 
2236  if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2237  /* Your left border <-> other left border */
2238  delta = abs(v->left - x);
2239  if (delta <= hsnap) {
2240  nx = v->left;
2241  hsnap = delta;
2242  }
2243 
2244  /* Your right border <-> other right border */
2245  delta = abs(v->left + v->width - x - w->width);
2246  if (delta <= hsnap) {
2247  nx = v->left + v->width - w->width;
2248  hsnap = delta;
2249  }
2250  }
2251 
2252  if (x + w->width > v->left && x < v->left + v->width) {
2253  /* Your top border <-> other bottom border */
2254  delta = abs(v->top + v->height - y);
2255  if (delta <= vsnap) {
2256  ny = v->top + v->height;
2257  vsnap = delta;
2258  }
2259 
2260  /* Your bottom border <-> other top border */
2261  delta = abs(v->top - y - w->height);
2262  if (delta <= vsnap) {
2263  ny = v->top - w->height;
2264  vsnap = delta;
2265  }
2266  }
2267 
2268  if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2269  /* Your top border <-> other top border */
2270  delta = abs(v->top - y);
2271  if (delta <= vsnap) {
2272  ny = v->top;
2273  vsnap = delta;
2274  }
2275 
2276  /* Your bottom border <-> other bottom border */
2277  delta = abs(v->top + v->height - y - w->height);
2278  if (delta <= vsnap) {
2279  ny = v->top + v->height - w->height;
2280  vsnap = delta;
2281  }
2282  }
2283  }
2284  }
2285 
2286  EnsureVisibleCaption(w, nx, ny);
2287 
2288  w->SetDirty();
2289  return ES_HANDLED;
2290  } else if (w->flags & WF_SIZING) {
2291  /* Stop the sizing if the left mouse button was released */
2292  if (!_left_button_down) {
2293  w->flags &= ~WF_SIZING;
2294  w->SetDirty();
2295  break;
2296  }
2297 
2298  /* Compute difference in pixels between cursor position and reference point in the window.
2299  * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2300  */
2301  int x, y = _cursor.pos.y - _drag_delta.y;
2302  if (w->flags & WF_SIZING_LEFT) {
2303  x = _drag_delta.x - _cursor.pos.x;
2304  } else {
2305  x = _cursor.pos.x - _drag_delta.x;
2306  }
2307 
2308  /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2309  if (w->resize.step_width == 0) x = 0;
2310  if (w->resize.step_height == 0) y = 0;
2311 
2312  /* Check the resize button won't go past the bottom of the screen */
2313  if (w->top + w->height + y > _screen.height) {
2314  y = _screen.height - w->height - w->top;
2315  }
2316 
2317  /* X and Y has to go by step.. calculate it.
2318  * The cast to int is necessary else x/y are implicitly casted to
2319  * unsigned int, which won't work. */
2320  if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2321  if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2322 
2323  /* Check that we don't go below the minimum set size */
2324  if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2325  x = w->nested_root->smallest_x - w->width;
2326  }
2327  if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2328  y = w->nested_root->smallest_y - w->height;
2329  }
2330 
2331  /* Window already on size */
2332  if (x == 0 && y == 0) return ES_HANDLED;
2333 
2334  /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2335  _drag_delta.y += y;
2336  if ((w->flags & WF_SIZING_LEFT) && x != 0) {
2337  _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2338  w->SetDirty();
2339  w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2340  /* ResizeWindow() below ensures marking new position as dirty. */
2341  } else {
2342  _drag_delta.x += x;
2343  }
2344 
2345  /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
2346  ResizeWindow(w, x, y);
2347  return ES_HANDLED;
2348  }
2349  }
2350 
2351  _dragging_window = false;
2352  return ES_HANDLED;
2353 }
2354 
2359 static void StartWindowDrag(Window *w)
2360 {
2361  w->flags |= WF_DRAGGING;
2362  w->flags &= ~WF_CENTERED;
2363  _dragging_window = true;
2364 
2365  _drag_delta.x = w->left - _cursor.pos.x;
2366  _drag_delta.y = w->top - _cursor.pos.y;
2367 
2368  BringWindowToFront(w);
2370 }
2371 
2377 static void StartWindowSizing(Window *w, bool to_left)
2378 {
2379  w->flags |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
2380  w->flags &= ~WF_CENTERED;
2381  _dragging_window = true;
2382 
2383  _drag_delta.x = _cursor.pos.x;
2384  _drag_delta.y = _cursor.pos.y;
2385 
2386  BringWindowToFront(w);
2388 }
2389 
2395 {
2396  int i;
2398  bool rtl = false;
2399 
2400  if (sb->type == NWID_HSCROLLBAR) {
2401  i = _cursor.pos.x - _cursorpos_drag_start.x;
2402  rtl = _current_text_dir == TD_RTL;
2403  } else {
2404  i = _cursor.pos.y - _cursorpos_drag_start.y;
2405  }
2406 
2407  if (sb->disp_flags & ND_SCROLLBAR_BTN) {
2408  if (_scroller_click_timeout == 1) {
2409  _scroller_click_timeout = 3;
2410  sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
2411  w->SetDirty();
2412  }
2413  return;
2414  }
2415 
2416  /* Find the item we want to move to and make sure it's inside bounds. */
2417  int pos = min(RoundDivSU(max(0, i + _scrollbar_start_pos) * sb->GetCount(), _scrollbar_size), max(0, sb->GetCount() - sb->GetCapacity()));
2418  if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
2419  if (pos != sb->GetPosition()) {
2420  sb->SetPosition(pos);
2421  w->SetDirty();
2422  }
2423 }
2424 
2430 {
2431  Window *w;
2432  FOR_ALL_WINDOWS_FROM_BACK(w) {
2433  if (w->mouse_capture_widget >= 0) {
2434  /* Abort if no button is clicked any more. */
2435  if (!_left_button_down) {
2436  w->mouse_capture_widget = -1;
2437  w->SetDirty();
2438  return ES_HANDLED;
2439  }
2440 
2441  /* Handle scrollbar internally, or dispatch click event */
2443  if (type == NWID_VSCROLLBAR || type == NWID_HSCROLLBAR) {
2445  } else {
2446  /* If cursor hasn't moved, there is nothing to do. */
2447  if (_cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2448 
2449  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2450  w->OnClick(pt, w->mouse_capture_widget, 0);
2451  }
2452  return ES_HANDLED;
2453  }
2454  }
2455 
2456  return ES_NOT_HANDLED;
2457 }
2458 
2464 {
2465  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2466 
2467  if (!_scrolling_viewport) return ES_NOT_HANDLED;
2468 
2469  /* When we don't have a last scroll window we are starting to scroll.
2470  * When the last scroll window and this are not the same we went
2471  * outside of the window and should not left-mouse scroll anymore. */
2472  if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2473 
2474  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))) {
2475  _cursor.fix_at = false;
2476  _scrolling_viewport = false;
2477  _last_scroll_window = NULL;
2478  return ES_NOT_HANDLED;
2479  }
2480 
2481  if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
2482  /* If the main window is following a vehicle, then first let go of it! */
2483  const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2484  ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2485  return ES_NOT_HANDLED;
2486  }
2487 
2488  Point delta;
2489  if (scrollwheel_scrolling) {
2490  /* We are using scrollwheels for scrolling */
2491  delta.x = _cursor.h_wheel;
2492  delta.y = _cursor.v_wheel;
2493  _cursor.v_wheel = 0;
2494  _cursor.h_wheel = 0;
2495  } else {
2497  delta.x = -_cursor.delta.x;
2498  delta.y = -_cursor.delta.y;
2499  } else {
2500  delta.x = _cursor.delta.x;
2501  delta.y = _cursor.delta.y;
2502  }
2503  }
2504 
2505  /* Create a scroll-event and send it to the window */
2506  if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2507 
2508  _cursor.delta.x = 0;
2509  _cursor.delta.y = 0;
2510  return ES_HANDLED;
2511 }
2512 
2524 {
2525  bool bring_to_front = false;
2526 
2527  if (w->window_class == WC_MAIN_WINDOW ||
2528  IsVitalWindow(w) ||
2529  w->window_class == WC_TOOLTIPS ||
2531  return true;
2532  }
2533 
2534  /* Use unshaded window size rather than current size for shaded windows. */
2535  int w_width = w->width;
2536  int w_height = w->height;
2537  if (w->IsShaded()) {
2538  w_width = w->unshaded_size.width;
2539  w_height = w->unshaded_size.height;
2540  }
2541 
2542  Window *u;
2544  /* A modal child will prevent the activation of the parent window */
2545  if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) {
2546  u->SetWhiteBorder();
2547  u->SetDirty();
2548  return false;
2549  }
2550 
2551  if (u->window_class == WC_MAIN_WINDOW ||
2552  IsVitalWindow(u) ||
2553  u->window_class == WC_TOOLTIPS ||
2555  continue;
2556  }
2557 
2558  /* Window sizes don't interfere, leave z-order alone */
2559  if (w->left + w_width <= u->left ||
2560  u->left + u->width <= w->left ||
2561  w->top + w_height <= u->top ||
2562  u->top + u->height <= w->top) {
2563  continue;
2564  }
2565 
2566  bring_to_front = true;
2567  }
2568 
2569  if (bring_to_front) BringWindowToFront(w);
2570  return true;
2571 }
2572 
2581 EventState Window::HandleEditBoxKey(int wid, WChar key, uint16 keycode)
2582 {
2583  QueryString *query = this->GetQueryString(wid);
2584  if (query == NULL) return ES_NOT_HANDLED;
2585 
2586  int action = QueryString::ACTION_NOTHING;
2587 
2588  switch (query->text.HandleKeyPress(key, keycode)) {
2589  case HKPR_EDITING:
2590  this->SetWidgetDirty(wid);
2591  this->OnEditboxChanged(wid);
2592  break;
2593 
2594  case HKPR_CURSOR:
2595  this->SetWidgetDirty(wid);
2596  /* For the OSK also invalidate the parent window */
2597  if (this->window_class == WC_OSK) this->InvalidateData();
2598  break;
2599 
2600  case HKPR_CONFIRM:
2601  if (this->window_class == WC_OSK) {
2602  this->OnClick(Point(), WID_OSK_OK, 1);
2603  } else if (query->ok_button >= 0) {
2604  this->OnClick(Point(), query->ok_button, 1);
2605  } else {
2606  action = query->ok_button;
2607  }
2608  break;
2609 
2610  case HKPR_CANCEL:
2611  if (this->window_class == WC_OSK) {
2612  this->OnClick(Point(), WID_OSK_CANCEL, 1);
2613  } else if (query->cancel_button >= 0) {
2614  this->OnClick(Point(), query->cancel_button, 1);
2615  } else {
2616  action = query->cancel_button;
2617  }
2618  break;
2619 
2620  case HKPR_NOT_HANDLED:
2621  return ES_NOT_HANDLED;
2622 
2623  default: break;
2624  }
2625 
2626  switch (action) {
2628  this->UnfocusFocusedWidget();
2629  break;
2630 
2632  if (query->text.bytes <= 1) {
2633  /* If already empty, unfocus instead */
2634  this->UnfocusFocusedWidget();
2635  } else {
2636  query->text.DeleteAll();
2637  this->SetWidgetDirty(wid);
2638  this->OnEditboxChanged(wid);
2639  }
2640  break;
2641 
2642  default:
2643  break;
2644  }
2645 
2646  return ES_HANDLED;
2647 }
2648 
2654 void HandleKeypress(uint keycode, WChar key)
2655 {
2656  /* World generation is multithreaded and messes with companies.
2657  * But there is no company related window open anyway, so _current_company is not used. */
2658  assert(HasModalProgress() || IsLocalCompany());
2659 
2660  /*
2661  * The Unicode standard defines an area called the private use area. Code points in this
2662  * area are reserved for private use and thus not portable between systems. For instance,
2663  * Apple defines code points for the arrow keys in this area, but these are only printable
2664  * on a system running OS X. We don't want these keys to show up in text fields and such,
2665  * and thus we have to clear the unicode character when we encounter such a key.
2666  */
2667  if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2668 
2669  /*
2670  * If both key and keycode is zero, we don't bother to process the event.
2671  */
2672  if (key == 0 && keycode == 0) return;
2673 
2674  /* Check if the focused window has a focused editbox */
2675  if (EditBoxInGlobalFocus()) {
2676  /* All input will in this case go to the focused editbox */
2677  if (_focused_window->window_class == WC_CONSOLE) {
2678  if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2679  } else {
2680  if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return;
2681  }
2682  }
2683 
2684  /* Call the event, start with the uppermost window, but ignore the toolbar. */
2685  Window *w;
2686  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2687  if (w->window_class == WC_MAIN_TOOLBAR) continue;
2688  if (w->window_desc->hotkeys != NULL) {
2689  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2690  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2691  }
2692  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2693  }
2694 
2696  /* When there is no toolbar w is null, check for that */
2697  if (w != NULL) {
2698  if (w->window_desc->hotkeys != NULL) {
2699  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2700  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2701  }
2702  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2703  }
2704 
2705  HandleGlobalHotkeys(key, keycode);
2706 }
2707 
2712 {
2713  /* Call the event, start with the uppermost window. */
2714  Window *w;
2715  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2716  if (w->OnCTRLStateChange() == ES_HANDLED) return;
2717  }
2718 }
2719 
2725 /* virtual */ void Window::InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2726 {
2727  QueryString *query = this->GetQueryString(wid);
2728  if (query == NULL) return;
2729 
2730  if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2731  this->SetWidgetDirty(wid);
2732  this->OnEditboxChanged(wid);
2733  }
2734 }
2735 
2742 void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2743 {
2744  if (!EditBoxInGlobalFocus()) return;
2745 
2746  _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end);
2747 }
2748 
2756 
2761 static void HandleAutoscroll()
2762 {
2763  if (_game_mode == GM_MENU || HasModalProgress()) return;
2765  if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2766 
2767  int x = _cursor.pos.x;
2768  int y = _cursor.pos.y;
2769  Window *w = FindWindowFromPt(x, y);
2770  if (w == NULL || w->flags & WF_DISABLE_VP_SCROLL) return;
2772 
2773  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2774  if (vp == NULL) return;
2775 
2776  x -= vp->left;
2777  y -= vp->top;
2778 
2779  /* here allows scrolling in both x and y axis */
2780  static const int SCROLLSPEED = 3;
2781  if (x - 15 < 0) {
2782  w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * SCROLLSPEED, vp->zoom);
2783  } else if (15 - (vp->width - x) > 0) {
2784  w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * SCROLLSPEED, vp->zoom);
2785  }
2786  if (y - 15 < 0) {
2787  w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * SCROLLSPEED, vp->zoom);
2788  } else if (15 - (vp->height - y) > 0) {
2789  w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * SCROLLSPEED, vp->zoom);
2790  }
2791 }
2792 
2794  MC_NONE = 0,
2795  MC_LEFT,
2796  MC_RIGHT,
2797  MC_DOUBLE_LEFT,
2798  MC_HOVER,
2799 
2803 };
2805 
2806 static void ScrollMainViewport(int x, int y)
2807 {
2808  if (_game_mode != GM_MENU) {
2810  assert(w);
2811 
2814  }
2815 }
2816 
2826 static const int8 scrollamt[16][2] = {
2827  { 0, 0},
2828  {-2, 0},
2829  { 0, -2},
2830  {-2, -1},
2831  { 2, 0},
2832  { 0, 0},
2833  { 2, -1},
2834  { 0, -2},
2835  { 0, 2},
2836  {-2, 1},
2837  { 0, 0},
2838  {-2, 0},
2839  { 2, 1},
2840  { 0, 2},
2841  { 2, 0},
2842  { 0, 0},
2843 };
2844 
2845 static void HandleKeyScrolling()
2846 {
2847  /*
2848  * Check that any of the dirkeys is pressed and that the focused window
2849  * doesn't have an edit-box as focused widget.
2850  */
2851  if (_dirkeys && !EditBoxInGlobalFocus()) {
2852  int factor = _shift_pressed ? 50 : 10;
2853  ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2854  }
2855 }
2856 
2857 static void MouseLoop(MouseClick click, int mousewheel)
2858 {
2859  /* World generation is multithreaded and messes with companies.
2860  * But there is no company related window open anyway, so _current_company is not used. */
2861  assert(HasModalProgress() || IsLocalCompany());
2862 
2863  HandlePlacePresize();
2865 
2866  if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2867  if (HandleMouseDragDrop() == ES_HANDLED) return;
2868  if (HandleWindowDragging() == ES_HANDLED) return;
2869  if (HandleActiveWidget() == ES_HANDLED) return;
2870  if (HandleViewportScroll() == ES_HANDLED) return;
2871 
2872  HandleMouseOver();
2873 
2874  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2875  if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2876 
2877  int x = _cursor.pos.x;
2878  int y = _cursor.pos.y;
2879  Window *w = FindWindowFromPt(x, y);
2880  if (w == NULL) return;
2881 
2882  if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2883  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2884 
2885  /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2886  if (vp != NULL && (_game_mode == GM_MENU || HasModalProgress())) return;
2887 
2888  if (mousewheel != 0) {
2889  /* Send mousewheel event to window, unless we're scrolling a viewport or the map */
2890  if (!scrollwheel_scrolling || (vp == NULL && w->window_class != WC_SMALLMAP)) w->OnMouseWheel(mousewheel);
2891 
2892  /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2893  if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2894  }
2895 
2896  if (vp != NULL) {
2897  if (scrollwheel_scrolling && !(w->flags & WF_DISABLE_VP_SCROLL)) {
2898  _scrolling_viewport = true;
2899  _cursor.fix_at = true;
2900  return;
2901  }
2902 
2903  switch (click) {
2904  case MC_DOUBLE_LEFT:
2905  case MC_LEFT:
2906  if (HandleViewportClicked(vp, x, y)) return;
2907  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2909  _scrolling_viewport = true;
2910  _cursor.fix_at = false;
2911  return;
2912  }
2913  break;
2914 
2915  case MC_RIGHT:
2916  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2918  _scrolling_viewport = true;
2921  return;
2922  }
2923  break;
2924 
2925  default:
2926  break;
2927  }
2928  }
2929 
2930  if (vp == NULL || (w->flags & WF_DISABLE_VP_SCROLL)) {
2931  switch (click) {
2932  case MC_LEFT:
2933  case MC_DOUBLE_LEFT:
2934  DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2935  return;
2936 
2937  default:
2938  if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
2939  /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2940  * Simulate a right button click so we can get started. */
2941  FALLTHROUGH;
2942 
2943  case MC_RIGHT:
2944  DispatchRightClickEvent(w, x - w->left, y - w->top);
2945  return;
2946 
2947  case MC_HOVER:
2948  DispatchHoverEvent(w, x - w->left, y - w->top);
2949  break;
2950  }
2951  }
2952 
2953  /* We're not doing anything with 2D scrolling, so reset the value. */
2954  _cursor.h_wheel = 0;
2955  _cursor.v_wheel = 0;
2956 }
2957 
2962 {
2963  /* World generation is multithreaded and messes with companies.
2964  * But there is no company related window open anyway, so _current_company is not used. */
2965  assert(HasModalProgress() || IsLocalCompany());
2966 
2967  static int double_click_time = 0;
2968  static Point double_click_pos = {0, 0};
2969 
2970  /* Mouse event? */
2971  MouseClick click = MC_NONE;
2973  click = MC_LEFT;
2974  if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
2975  double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2976  double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2977  click = MC_DOUBLE_LEFT;
2978  }
2979  double_click_time = _realtime_tick;
2980  double_click_pos = _cursor.pos;
2981  _left_button_clicked = true;
2983  } else if (_right_button_clicked) {
2984  _right_button_clicked = false;
2985  click = MC_RIGHT;
2987  }
2988 
2989  int mousewheel = 0;
2990  if (_cursor.wheel) {
2991  mousewheel = _cursor.wheel;
2992  _cursor.wheel = 0;
2994  }
2995 
2996  static uint32 hover_time = 0;
2997  static Point hover_pos = {0, 0};
2998 
3000  if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
3001  hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
3002  hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
3003  hover_pos = _cursor.pos;
3004  hover_time = _realtime_tick;
3005  _mouse_hovering = false;
3006  } else {
3007  if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
3008  click = MC_HOVER;
3010  _mouse_hovering = true;
3011  }
3012  }
3013  }
3014 
3015  /* Handle sprite picker before any GUI interaction */
3017  /* Next realtime tick? Then redraw has finished */
3018  _newgrf_debug_sprite_picker.mode = SPM_NONE;
3020  }
3021 
3022  if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
3023  /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
3025  _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
3028  _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
3030  } else {
3031  MouseLoop(click, mousewheel);
3032  }
3033 
3034  /* We have moved the mouse the required distance,
3035  * no need to move it at any later time. */
3036  _cursor.delta.x = 0;
3037  _cursor.delta.y = 0;
3038 }
3039 
3043 static void CheckSoftLimit()
3044 {
3045  if (_settings_client.gui.window_soft_limit == 0) return;
3046 
3047  for (;;) {
3048  uint deletable_count = 0;
3049  Window *w, *last_deletable = NULL;
3050  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3051  if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags & WF_STICKY)) continue;
3052 
3053  last_deletable = w;
3054  deletable_count++;
3055  }
3056 
3057  /* We've not reached the soft limit yet. */
3058  if (deletable_count <= _settings_client.gui.window_soft_limit) break;
3059 
3060  assert(last_deletable != NULL);
3061  delete last_deletable;
3062  }
3063 }
3064 
3069 {
3070  /* World generation is multithreaded and messes with companies.
3071  * But there is no company related window open anyway, so _current_company is not used. */
3072  assert(HasModalProgress() || IsLocalCompany());
3073 
3074  CheckSoftLimit();
3075 
3076  /* Do the actual free of the deleted windows. */
3077  for (Window *v = _z_front_window; v != NULL; /* nothing */) {
3078  Window *w = v;
3079  v = v->z_back;
3080 
3081  if (w->window_class != WC_INVALID) continue;
3082 
3084  free(w);
3085  }
3086 
3087  if (_input_events_this_tick != 0) {
3088  /* The input loop is called only once per GameLoop() - so we can clear the counter here */
3090  /* there were some inputs this tick, don't scroll ??? */
3091  return;
3092  }
3093 
3094  /* HandleMouseEvents was already called for this tick */
3096 }
3097 
3101 void CallWindowRealtimeTickEvent(uint delta_ms)
3102 {
3103  Window *w;
3104  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3105  w->OnRealtimeTick(delta_ms);
3106  }
3107 }
3108 
3113 {
3114  static uint32 last_realtime_tick = _realtime_tick;
3115  uint delta_ms = _realtime_tick - last_realtime_tick;
3116  last_realtime_tick = _realtime_tick;
3117 
3118  if (delta_ms == 0) return;
3119 
3120  PerformanceMeasurer framerate(PFE_DRAWING);
3122 
3123  CallWindowRealtimeTickEvent(delta_ms);
3124 
3125 #ifdef ENABLE_NETWORK
3126  static GUITimer network_message_timer = GUITimer(1);
3127  if (network_message_timer.Elapsed(delta_ms)) {
3128  network_message_timer.SetInterval(1000);
3130  }
3131 #endif
3132 
3133  Window *w;
3134 
3135  static GUITimer window_timer = GUITimer(1);
3136  if (window_timer.Elapsed(delta_ms)) {
3137  if (_network_dedicated) window_timer.SetInterval(MILLISECONDS_PER_TICK);
3138 
3139  extern int _caret_timer;
3140  _caret_timer += 3;
3141  CursorTick();
3142 
3143  HandleKeyScrolling();
3144  HandleAutoscroll();
3145  DecreaseWindowCounters();
3146  }
3147 
3148  static GUITimer highlight_timer = GUITimer(1);
3149  if (highlight_timer.Elapsed(delta_ms)) {
3150  highlight_timer.SetInterval(450);
3152  }
3153 
3154  if (!_pause_mode || _game_mode == GM_EDITOR || _settings_game.construction.command_pause_level > CMDPL_NO_CONSTRUCTION) MoveAllTextEffects(delta_ms);
3155 
3156  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3159  }
3160 
3161  /* Skip the actual drawing on dedicated servers without screen.
3162  * But still empty the invalidation queues above. */
3163  if (_network_dedicated) return;
3164 
3165  static GUITimer hundredth_timer = GUITimer(1);
3166  if (hundredth_timer.Elapsed(delta_ms)) {
3167  hundredth_timer.SetInterval(3000); // Historical reason: 100 * MILLISECONDS_PER_TICK
3168 
3169  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3170  w->OnHundredthTick();
3171  }
3172  }
3173 
3174  if (window_timer.HasElapsed()) {
3175  window_timer.SetInterval(MILLISECONDS_PER_TICK);
3176 
3177  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3178  if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) {
3180  w->SetDirty();
3181  }
3182  }
3183  }
3184 
3185  DrawDirtyBlocks();
3186 
3187  FOR_ALL_WINDOWS_FROM_BACK(w) {
3188  /* Update viewport only if window is not shaded. */
3189  if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
3190  }
3192  /* Redraw mouse cursor in case it was hidden */
3193  DrawMouseCursor();
3194 }
3195 
3202 {
3203  const Window *w;
3204  FOR_ALL_WINDOWS_FROM_BACK(w) {
3205  if (w->window_class == cls && w->window_number == number) w->SetDirty();
3206  }
3207 }
3208 
3215 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
3216 {
3217  const Window *w;
3218  FOR_ALL_WINDOWS_FROM_BACK(w) {
3219  if (w->window_class == cls && w->window_number == number) {
3220  w->SetWidgetDirty(widget_index);
3221  }
3222  }
3223 }
3224 
3230 {
3231  Window *w;
3232  FOR_ALL_WINDOWS_FROM_BACK(w) {
3233  if (w->window_class == cls) w->SetDirty();
3234  }
3235 }
3236 
3242 void Window::InvalidateData(int data, bool gui_scope)
3243 {
3244  this->SetDirty();
3245  if (!gui_scope) {
3246  /* Schedule GUI-scope invalidation for next redraw. */
3247  *this->scheduled_invalidation_data.Append() = data;
3248  }
3249  this->OnInvalidateData(data, gui_scope);
3250 }
3251 
3256 {
3257  for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
3258  this->OnInvalidateData(*data, true);
3259  }
3261 }
3262 
3267 {
3268  if ((this->flags & WF_HIGHLIGHTED) == 0) return;
3269 
3270  for (uint i = 0; i < this->nested_array_size; i++) {
3271  if (this->IsWidgetHighlighted(i)) this->SetWidgetDirty(i);
3272  }
3273 }
3274 
3301 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3302 {
3303  Window *w;
3304  FOR_ALL_WINDOWS_FROM_BACK(w) {
3305  if (w->window_class == cls && w->window_number == number) {
3306  w->InvalidateData(data, gui_scope);
3307  }
3308  }
3309 }
3310 
3319 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3320 {
3321  Window *w;
3322 
3323  FOR_ALL_WINDOWS_FROM_BACK(w) {
3324  if (w->window_class == cls) {
3325  w->InvalidateData(data, gui_scope);
3326  }
3327  }
3328 }
3329 
3334 {
3335  Window *w;
3336  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3337  w->OnGameTick();
3338  }
3339 }
3340 
3348 {
3349  Window *w;
3350 
3351 restart_search:
3352  /* When we find the window to delete, we need to restart the search
3353  * as deleting this window could cascade in deleting (many) others
3354  * anywhere in the z-array */
3355  FOR_ALL_WINDOWS_FROM_BACK(w) {
3356  if (w->window_class != WC_MAIN_WINDOW &&
3357  w->window_class != WC_SELECT_GAME &&
3358  w->window_class != WC_MAIN_TOOLBAR &&
3359  w->window_class != WC_STATUS_BAR &&
3360  w->window_class != WC_TOOLTIPS &&
3361  (w->flags & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
3362 
3363  delete w;
3364  goto restart_search;
3365  }
3366  }
3367 }
3368 
3377 {
3378  Window *w;
3379 
3380  /* Delete every window except for stickied ones, then sticky ones as well */
3382 
3383 restart_search:
3384  /* When we find the window to delete, we need to restart the search
3385  * as deleting this window could cascade in deleting (many) others
3386  * anywhere in the z-array */
3387  FOR_ALL_WINDOWS_FROM_BACK(w) {
3388  if (w->flags & WF_STICKY) {
3389  delete w;
3390  goto restart_search;
3391  }
3392  }
3393 }
3394 
3399 {
3401  InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
3402  InvalidateWindowData(WC_MESSAGE_HISTORY, 0); // invalidate the message history
3403  DeleteWindowById(WC_NEWS_WINDOW, 0); // close newspaper or general message window if shown
3404 }
3405 
3411 {
3412  Window *w;
3413 
3414 restart_search:
3415  /* When we find the window to delete, we need to restart the search
3416  * as deleting this window could cascade in deleting (many) others
3417  * anywhere in the z-array */
3418  FOR_ALL_WINDOWS_FROM_BACK(w) {
3419  if (w->window_desc->flags & WDF_CONSTRUCTION) {
3420  delete w;
3421  goto restart_search;
3422  }
3423  }
3424 
3425  FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
3426 }
3427 
3430 {
3433 }
3434 
3437 {
3438  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3439  NWidgetScrollbar::InvalidateDimensionCache();
3440 
3441  extern void InitDepotWindowBlockSizes();
3443 
3444  Window *w;
3445  FOR_ALL_WINDOWS_FROM_BACK(w) {
3446  w->ReInit();
3447  }
3448 #ifdef ENABLE_NETWORK
3449  void NetworkReInitChatBoxSize();
3451 #endif
3452 
3453  /* Make sure essential parts of all windows are visible */
3456 }
3457 
3465 static int PositionWindow(Window *w, WindowClass clss, int setting)
3466 {
3467  if (w == NULL || w->window_class != clss) {
3468  w = FindWindowById(clss, 0);
3469  }
3470  if (w == NULL) return 0;
3471 
3472  int old_left = w->left;
3473  switch (setting) {
3474  case 1: w->left = (_screen.width - w->width) / 2; break;
3475  case 2: w->left = _screen.width - w->width; break;
3476  default: w->left = 0; break;
3477  }
3478  if (w->viewport != NULL) w->viewport->left += w->left - old_left;
3479  SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3480  return w->left;
3481 }
3482 
3489 {
3490  DEBUG(misc, 5, "Repositioning Main Toolbar...");
3492 }
3493 
3500 {
3501  DEBUG(misc, 5, "Repositioning statusbar...");
3503 }
3504 
3511 {
3512  DEBUG(misc, 5, "Repositioning news message...");
3514 }
3515 
3522 {
3523  DEBUG(misc, 5, "Repositioning network chat window...");
3525 }
3526 
3527 
3533 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3534 {
3535  Window *w;
3536  FOR_ALL_WINDOWS_FROM_BACK(w) {
3537  if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
3538  w->viewport->follow_vehicle = to_index;
3539  w->SetDirty();
3540  }
3541  }
3542 }
3543 
3544 
3550 void RelocateAllWindows(int neww, int newh)
3551 {
3553 
3554  Window *w;
3555  FOR_ALL_WINDOWS_FROM_BACK(w) {
3556  int left, top;
3557  /* XXX - this probably needs something more sane. For example specifying
3558  * in a 'backup'-desc that the window should always be centered. */
3559  switch (w->window_class) {
3560  case WC_MAIN_WINDOW:
3561  case WC_BOOTSTRAP:
3562  ResizeWindow(w, neww, newh);
3563  continue;
3564 
3565  case WC_MAIN_TOOLBAR:
3566  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3567 
3568  top = w->top;
3569  left = PositionMainToolbar(w); // changes toolbar orientation
3570  break;
3571 
3572  case WC_NEWS_WINDOW:
3573  top = newh - w->height;
3574  left = PositionNewsMessage(w);
3575  break;
3576 
3577  case WC_STATUS_BAR:
3578  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3579 
3580  top = newh - w->height;
3581  left = PositionStatusbar(w);
3582  break;
3583 
3584  case WC_SEND_NETWORK_MSG:
3585  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3586 
3587  top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3588  left = PositionNetworkChatWindow(w);
3589  break;
3590 
3591  case WC_CONSOLE:
3592  IConsoleResize(w);
3593  continue;
3594 
3595  default: {
3596  if (w->flags & WF_CENTERED) {
3597  top = (newh - w->height) >> 1;
3598  left = (neww - w->width) >> 1;
3599  break;
3600  }
3601 
3602  left = w->left;
3603  if (left + (w->width >> 1) >= neww) left = neww - w->width;
3604  if (left < 0) left = 0;
3605 
3606  top = w->top;
3607  if (top + (w->height >> 1) >= newh) top = newh - w->height;
3608  break;
3609  }
3610  }
3611 
3612  EnsureVisibleCaption(w, left, top);
3613  }
3614 }
3615 
3622 {
3623  this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
3625 }
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:3043
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:909
Functions/types related to NewGRF debugging.
void SetShaded(bool make_shaded)
Set the shaded state of the window to make_shaded.
Definition: window.cpp:1015
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:798
static Window * FindChildWindow(const Window *w, WindowClass wc)
Find the Window whose parent pointer points to this window.
Definition: window.cpp:1042
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:671
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:1607
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
Definition: window.cpp:1862
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:324
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:1851
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:329
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:3201
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:883
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:368
Window * parent
Parent window.
Definition: window_gui.h:339
void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
Handle text input.
Definition: window.cpp:2742
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:665
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:312
int left
x position of left edge of the window
Definition: window_gui.h:319
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:2049
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
virtual bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond)
Event to display a custom tooltip.
Definition: window_gui.h:651
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:1407
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:3333
void DeleteConstructionWindows()
Delete all windows that are used for construction of vehicle etc.
Definition: window.cpp:3410
Dragging an object.
Definition: window_gui.h:911
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:1114
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:3488
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:335
WidgetType type
Type of the widget / nested widget.
Definition: widget_type.h:163
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition: window.cpp:2711
Types for recording game performance data.
void InitWindowSystem()
(re)initialize the windowing system
Definition: window.cpp:1877
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:2801
virtual ~PickerWindowBase()
Destructor of the base class PickerWindowBase Main utility is to stop the base Window destructor from...
Definition: window.cpp:3621
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:2034
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:320
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:733
static EventState HandleViewportScroll()
Handle viewport scrolling with the mouse.
Definition: window.cpp:2463
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:2185
virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
Compute the initial position of the window.
Definition: window.cpp:1800
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:691
static void StartWindowDrag(Window *w)
Start window dragging.
Definition: window.cpp:2359
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3112
static bool _dragging_window
A window is being dragged or resized.
Definition: window.cpp:2179
static void StartWindowSizing(Window *w, bool to_left)
Start resizing a window.
Definition: window.cpp:2377
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:979
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:3398
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:3376
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2654
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:913
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:2035
void InputLoop()
Regular call from the global game loop.
Definition: window.cpp:3068
virtual void OnTimeout()
Called when this window&#39;s timeout has been reached.
Definition: window_gui.h:711
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:1812
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
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
Definition: math_func.hpp:338
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:2173
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:2761
void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
Switches viewports following vehicles, which get autoreplaced.
Definition: window.cpp:3533
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:794
Data structure for an opened window.
Definition: window_gui.h:278
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:1828
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:3319
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1841
int PositionStatusbar(Window *w)
(Re)position statusbar window at the screen.
Definition: window.cpp:3499
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:332
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:2980
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:2429
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:609
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:3429
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:943
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:3510
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:1647
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:598
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:330
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:1716
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:578
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:3550
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:284
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:2162
A number of safeguards to prevent using unsafe methods.
static void HandleMouseOver()
Report position of the mouse to the underlying window.
Definition: window.cpp:2008
void SetWhiteBorder()
Set the timeout flag of the window and initiate the timer.
Definition: window_gui.h:377
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:2826
void DeleteCompanyWindows(CompanyID id)
Delete all windows of a company.
Definition: window.cpp:1181
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:822
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:1056
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
Definition: window.cpp:3521
uint nested_array_size
Size of the nested array.
Definition: window_gui.h:333
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.
Definition: window_gui.h:643
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:2581
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:2725
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:679
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:2523
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:2087
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:317
Basic functions/variables used all over the place.
Below v is a safe position.
Definition: window.cpp:2036
void InitializeData(WindowNumber window_number)
Initializes the data (except the position and initial size) of a new Window.
Definition: window.cpp:1446
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:2961
bool pref_sticky
Preferred stickyness.
Definition: window_gui.h:185
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1159
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:1898
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:2802
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:419
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:2031
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:968
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:2793
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:341
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:1146
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:636
Company infrastructure overview; Window numbers:
Definition: window_type.h:572
virtual void OnPaint()
The window must be repainted.
Definition: window_gui.h:560
void DeleteNonVitalWindows()
Try to delete a non-vital window.
Definition: window.cpp:3347
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:1068
void ReInitAllWindows()
Re-initialize all windows.
Definition: window.cpp:3436
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:2800
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:3215
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:1741
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
Definition: window.cpp:1488
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:340
uint _toolbar_width
Width of the toolbar, shared by statusbar.
Definition: toolbar_gui.cpp:63
void ProcessScheduledInvalidations()
Process all scheduled invalidations.
Definition: window.cpp:3255
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:618
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:814
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:2755
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:696
static void HandleScrollbarScrolling(Window *w)
Handle scrollbar scrolling with the mouse.
Definition: window.cpp:2394
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:218
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:1130
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:706
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:1570
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:1431
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:326
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:1207
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:658
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:1506
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:627
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:321
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:1984
virtual void OnResize()
Called after the window got resized.
Definition: window_gui.h:718
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:1280
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:3465
WindowClass window_class
Window class.
Definition: window_gui.h:313
virtual void OnMouseWheel(int wheel)
The mouse wheel has been turned.
Definition: window_gui.h:685
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3088
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:526
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:314
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:1918
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:3101
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:701
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:328
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:3229
Functions related to news.
uint8 timeout_timer
Timer value of the WF_TIMEOUT for flags.
Definition: window_gui.h:316
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:847
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2126
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:331
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:1243
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:311
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:3242
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:337
static void AddWindowToZOrdering(Window *w)
Adds a window to the z-ordering, according to its z-priority.
Definition: window.cpp:1358
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:3301
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:322
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:748
void ProcessHighlightedInvalidations()
Process all invalidation of highlighted widgets.
Definition: window.cpp:3266
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:855
void IConsoleClose()
Close the in-game console.
#define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)
Iterate over all windows.
Definition: window_gui.h:894
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