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