OpenTTD
window.cpp
Go to the documentation of this file.
1 /* $Id: window.cpp 27934 2017-12-09 19:21:41Z michi_cc $ */
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 
40 #include "safeguards.h"
41 
48 };
49 
51 static Window *_mouseover_last_w = NULL;
52 static Window *_last_scroll_window = NULL;
53 
58 
61 
62 /*
63  * Window that currently has focus. - The main purpose is to generate
64  * #FocusLost events, not to give next window in z-order focus when a
65  * window is closed.
66  */
67 Window *_focused_window;
68 
69 Point _cursorpos_drag_start;
70 
71 int _scrollbar_start_pos;
72 int _scrollbar_size;
73 byte _scroller_click_timeout = 0;
74 
77 
79 
85 
88 
90 WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
91  WindowClass window_class, WindowClass parent_class, uint32 flags,
92  const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys) :
93  default_pos(def_pos),
94  cls(window_class),
95  parent_cls(parent_class),
96  ini_key(ini_key),
97  flags(flags),
98  nwid_parts(nwid_parts),
99  nwid_length(nwid_length),
100  hotkeys(hotkeys),
101  pref_sticky(false),
102  pref_width(0),
103  pref_height(0),
104  default_width_trad(def_width_trad),
105  default_height_trad(def_height_trad)
106 {
107  if (_window_descs == NULL) _window_descs = new SmallVector<WindowDesc*, 16>();
108  *_window_descs->Append() = this;
109 }
110 
111 WindowDesc::~WindowDesc()
112 {
113  _window_descs->Erase(_window_descs->Find(this));
114 }
115 
122 {
123  return this->pref_width != 0 ? this->pref_width : ScaleGUITrad(this->default_width_trad);
124 }
125 
132 {
133  return this->pref_height != 0 ? this->pref_height : ScaleGUITrad(this->default_height_trad);
134 }
135 
140 {
141  IniFile *ini = new IniFile();
143  for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
144  if ((*it)->ini_key == NULL) continue;
145  IniLoadWindowSettings(ini, (*it)->ini_key, *it);
146  }
147  delete ini;
148 }
149 
153 static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b)
154 {
155  if ((*a)->ini_key != NULL && (*b)->ini_key != NULL) return strcmp((*a)->ini_key, (*b)->ini_key);
156  return ((*b)->ini_key != NULL ? 1 : 0) - ((*a)->ini_key != NULL ? 1 : 0);
157 }
158 
163 {
164  /* Sort the stuff to get a nice ini file on first write */
165  QSortT(_window_descs->Begin(), _window_descs->Length(), DescSorter);
166 
167  IniFile *ini = new IniFile();
168  ini->LoadFromDisk(_windows_file, NO_DIRECTORY);
169  for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
170  if ((*it)->ini_key == NULL) continue;
171  IniSaveWindowSettings(ini, (*it)->ini_key, *it);
172  }
173  ini->SaveToDisk(_windows_file);
174  delete ini;
175 }
176 
181 {
182  if (this->nested_root != NULL && this->nested_root->GetWidgetOfType(WWT_STICKYBOX) != NULL) {
183  if (this->window_desc->pref_sticky) this->flags |= WF_STICKY;
184  } else {
185  /* There is no stickybox; clear the preference in case someone tried to be funny */
186  this->window_desc->pref_sticky = false;
187  }
188 }
189 
199 int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const
200 {
201  const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
202  if (line_height < 0) line_height = wid->resize_y;
203  if (clickpos < (int)wid->pos_y + padding) return INT_MAX;
204  return (clickpos - (int)wid->pos_y - padding) / line_height;
205 }
206 
211 {
212  for (uint i = 0; i < this->nested_array_size; i++) {
213  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
214  if (nwid == NULL) continue;
215 
216  if (nwid->IsHighlighted()) {
217  nwid->SetHighlighted(TC_INVALID);
218  this->SetWidgetDirty(i);
219  }
220  }
221 
222  CLRBITS(this->flags, WF_HIGHLIGHTED);
223 }
224 
230 void Window::SetWidgetHighlight(byte widget_index, TextColour highlighted_colour)
231 {
232  assert(widget_index < this->nested_array_size);
233 
234  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
235  if (nwid == NULL) return;
236 
237  nwid->SetHighlighted(highlighted_colour);
238  this->SetWidgetDirty(widget_index);
239 
240  if (highlighted_colour != TC_INVALID) {
241  /* If we set a highlight, the window has a highlight */
242  this->flags |= WF_HIGHLIGHTED;
243  } else {
244  /* If we disable a highlight, check all widgets if anyone still has a highlight */
245  bool valid = false;
246  for (uint i = 0; i < this->nested_array_size; i++) {
247  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
248  if (nwid == NULL) continue;
249  if (!nwid->IsHighlighted()) continue;
250 
251  valid = true;
252  }
253  /* If nobody has a highlight, disable the flag on the window */
254  if (!valid) CLRBITS(this->flags, WF_HIGHLIGHTED);
255  }
256 }
257 
263 bool Window::IsWidgetHighlighted(byte widget_index) const
264 {
265  assert(widget_index < this->nested_array_size);
266 
267  const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
268  if (nwid == NULL) return false;
269 
270  return nwid->IsHighlighted();
271 }
272 
280 void Window::OnDropdownClose(Point pt, int widget, int index, bool instant_close)
281 {
282  if (widget < 0) return;
283 
284  if (instant_close) {
285  /* Send event for selected option if we're still
286  * on the parent button of the dropdown (behaviour of the dropdowns in the main toolbar). */
287  if (GetWidgetFromPos(this, pt.x, pt.y) == widget) {
288  this->OnDropdownSelect(widget, index);
289  }
290  }
291 
292  /* Raise the dropdown button */
293  NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget);
294  if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
295  nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE;
296  } else {
297  this->RaiseWidget(widget);
298  }
299  this->SetWidgetDirty(widget);
300 }
301 
307 const Scrollbar *Window::GetScrollbar(uint widnum) const
308 {
309  return this->GetWidget<NWidgetScrollbar>(widnum);
310 }
311 
318 {
319  return this->GetWidget<NWidgetScrollbar>(widnum);
320 }
321 
327 const QueryString *Window::GetQueryString(uint widnum) const
328 {
329  const SmallMap<int, QueryString*>::Pair *query = this->querystrings.Find(widnum);
330  return query != this->querystrings.End() ? query->second : NULL;
331 }
332 
339 {
340  SmallMap<int, QueryString*>::Pair *query = this->querystrings.Find(widnum);
341  return query != this->querystrings.End() ? query->second : NULL;
342 }
343 
348 /* virtual */ const char *Window::GetFocusedText() const
349 {
350  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
351  return this->GetQueryString(this->nested_focus->index)->GetText();
352  }
353 
354  return NULL;
355 }
356 
361 /* virtual */ const char *Window::GetCaret() const
362 {
363  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
364  return this->GetQueryString(this->nested_focus->index)->GetCaret();
365  }
366 
367  return NULL;
368 }
369 
375 /* virtual */ const char *Window::GetMarkedText(size_t *length) const
376 {
377  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
378  return this->GetQueryString(this->nested_focus->index)->GetMarkedText(length);
379  }
380 
381  return NULL;
382 }
383 
388 /* virtual */ Point Window::GetCaretPosition() const
389 {
390  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
391  return this->GetQueryString(this->nested_focus->index)->GetCaretPosition(this, this->nested_focus->index);
392  }
393 
394  Point pt = {0, 0};
395  return pt;
396 }
397 
404 /* virtual */ Rect Window::GetTextBoundingRect(const char *from, const char *to) const
405 {
406  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
407  return this->GetQueryString(this->nested_focus->index)->GetBoundingRect(this, this->nested_focus->index, from, to);
408  }
409 
410  Rect r = {0, 0, 0, 0};
411  return r;
412 }
413 
419 /* virtual */ const char *Window::GetTextCharacterAtPosition(const Point &pt) const
420 {
421  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
422  return this->GetQueryString(this->nested_focus->index)->GetCharAtPosition(this, this->nested_focus->index, pt);
423  }
424 
425  return NULL;
426 }
427 
433 {
434  if (_focused_window == w) return;
435 
436  /* Invalidate focused widget */
437  if (_focused_window != NULL) {
438  if (_focused_window->nested_focus != NULL) _focused_window->nested_focus->SetDirty(_focused_window);
439  }
440 
441  /* Remember which window was previously focused */
442  Window *old_focused = _focused_window;
443  _focused_window = w;
444 
445  /* So we can inform it that it lost focus */
446  if (old_focused != NULL) old_focused->OnFocusLost();
447  if (_focused_window != NULL) _focused_window->OnFocus();
448 }
449 
456 {
457  if (_focused_window == NULL) return false;
458 
459  /* The console does not have an edit box so a special case is needed. */
460  if (_focused_window->window_class == WC_CONSOLE) return true;
461 
462  return _focused_window->nested_focus != NULL && _focused_window->nested_focus->type == WWT_EDITBOX;
463 }
464 
469 {
470  if (this->nested_focus != NULL) {
471  if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
472 
473  /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
474  this->nested_focus->SetDirty(this);
475  this->nested_focus = NULL;
476  }
477 }
478 
484 bool Window::SetFocusedWidget(int widget_index)
485 {
486  /* Do nothing if widget_index is already focused, or if it wasn't a valid widget. */
487  if ((uint)widget_index >= this->nested_array_size) return false;
488 
489  assert(this->nested_array[widget_index] != NULL); // Setting focus to a non-existing widget is a bad idea.
490  if (this->nested_focus != NULL) {
491  if (this->GetWidget<NWidgetCore>(widget_index) == this->nested_focus) return false;
492 
493  /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
494  this->nested_focus->SetDirty(this);
495  if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
496  }
497  this->nested_focus = this->GetWidget<NWidgetCore>(widget_index);
498  return true;
499 }
500 
505 {
506  if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
507 }
508 
516 void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...)
517 {
518  va_list wdg_list;
519 
520  va_start(wdg_list, widgets);
521 
522  while (widgets != WIDGET_LIST_END) {
523  SetWidgetDisabledState(widgets, disab_stat);
524  widgets = va_arg(wdg_list, int);
525  }
526 
527  va_end(wdg_list);
528 }
529 
535 void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...)
536 {
537  va_list wdg_list;
538 
539  va_start(wdg_list, widgets);
540 
541  while (widgets != WIDGET_LIST_END) {
542  SetWidgetLoweredState(widgets, lowered_stat);
543  widgets = va_arg(wdg_list, int);
544  }
545 
546  va_end(wdg_list);
547 }
548 
553 void Window::RaiseButtons(bool autoraise)
554 {
555  for (uint i = 0; i < this->nested_array_size; i++) {
556  if (this->nested_array[i] == NULL) continue;
557  WidgetType type = this->nested_array[i]->type;
558  if (((type & ~WWB_PUSHBUTTON) < WWT_LAST || type == NWID_PUSHBUTTON_DROPDOWN) &&
559  (!autoraise || (type & WWB_PUSHBUTTON) || type == WWT_EDITBOX) && this->IsWidgetLowered(i)) {
560  this->RaiseWidget(i);
561  this->SetWidgetDirty(i);
562  }
563  }
564 
565  /* Special widgets without widget index */
566  NWidgetCore *wid = this->nested_root != NULL ? (NWidgetCore*)this->nested_root->GetWidgetOfType(WWT_DEFSIZEBOX) : NULL;
567  if (wid != NULL) {
568  wid->SetLowered(false);
569  wid->SetDirty(this);
570  }
571 }
572 
577 void Window::SetWidgetDirty(byte widget_index) const
578 {
579  /* Sometimes this function is called before the window is even fully initialized */
580  if (this->nested_array == NULL) return;
581 
582  this->nested_array[widget_index]->SetDirty(this);
583 }
584 
591 {
592  if (hotkey < 0) return ES_NOT_HANDLED;
593 
594  NWidgetCore *nw = this->GetWidget<NWidgetCore>(hotkey);
595  if (nw == NULL || nw->IsDisabled()) return ES_NOT_HANDLED;
596 
597  if (nw->type == WWT_EDITBOX) {
598  if (this->IsShaded()) return ES_NOT_HANDLED;
599 
600  /* Focus editbox */
601  this->SetFocusedWidget(hotkey);
602  SetFocusedWindow(this);
603  } else {
604  /* Click button */
605  this->OnClick(Point(), hotkey, 1);
606  }
607  return ES_HANDLED;
608 }
609 
615 void Window::HandleButtonClick(byte widget)
616 {
617  this->LowerWidget(widget);
618  this->SetTimeout();
619  this->SetWidgetDirty(widget);
620 }
621 
622 static void StartWindowDrag(Window *w);
623 static void StartWindowSizing(Window *w, bool to_left);
624 
632 static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
633 {
634  NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
635  WidgetType widget_type = (nw != NULL) ? nw->type : WWT_EMPTY;
636 
637  bool focused_widget_changed = false;
638  /* If clicked on a window that previously did dot have focus */
639  if (_focused_window != w && // We already have focus, right?
640  (w->window_desc->flags & WDF_NO_FOCUS) == 0 && // Don't lose focus to toolbars
641  widget_type != WWT_CLOSEBOX) { // Don't change focused window if 'X' (close button) was clicked
642  focused_widget_changed = true;
643  SetFocusedWindow(w);
644  }
645 
646  if (nw == NULL) return; // exit if clicked outside of widgets
647 
648  /* don't allow any interaction if the button has been disabled */
649  if (nw->IsDisabled()) return;
650 
651  int widget_index = nw->index;
652 
653  /* Clicked on a widget that is not disabled.
654  * So unless the clicked widget is the caption bar, change focus to this widget.
655  * Exception: In the OSK we always want the editbox to stay focussed. */
656  if (widget_type != WWT_CAPTION && w->window_class != WC_OSK) {
657  /* focused_widget_changed is 'now' only true if the window this widget
658  * is in gained focus. In that case it must remain true, also if the
659  * local widget focus did not change. As such it's the logical-or of
660  * both changed states.
661  *
662  * If this is not preserved, then the OSK window would be opened when
663  * a user has the edit box focused and then click on another window and
664  * then back again on the edit box (to type some text).
665  */
666  focused_widget_changed |= w->SetFocusedWidget(widget_index);
667  }
668 
669  /* Close any child drop down menus. If the button pressed was the drop down
670  * list's own button, then we should not process the click any further. */
671  if (HideDropDownMenu(w) == widget_index && widget_index >= 0) return;
672 
673  if ((widget_type & ~WWB_PUSHBUTTON) < WWT_LAST && (widget_type & WWB_PUSHBUTTON)) w->HandleButtonClick(widget_index);
674 
675  Point pt = { x, y };
676 
677  switch (widget_type) {
678  case NWID_VSCROLLBAR:
679  case NWID_HSCROLLBAR:
680  ScrollbarClickHandler(w, nw, x, y);
681  break;
682 
683  case WWT_EDITBOX: {
684  QueryString *query = w->GetQueryString(widget_index);
685  if (query != NULL) query->ClickEditBox(w, pt, widget_index, click_count, focused_widget_changed);
686  break;
687  }
688 
689  case WWT_CLOSEBOX: // 'X'
690  delete w;
691  return;
692 
693  case WWT_CAPTION: // 'Title bar'
694  StartWindowDrag(w);
695  return;
696 
697  case WWT_RESIZEBOX:
698  /* When the resize widget is on the left size of the window
699  * we assume that that button is used to resize to the left. */
700  StartWindowSizing(w, (int)nw->pos_x < (w->width / 2));
701  nw->SetDirty(w);
702  return;
703 
704  case WWT_DEFSIZEBOX: {
705  if (_ctrl_pressed) {
706  w->window_desc->pref_width = w->width;
707  w->window_desc->pref_height = w->height;
708  } else {
709  int16 def_width = max<int16>(min(w->window_desc->GetDefaultWidth(), _screen.width), w->nested_root->smallest_x);
710  int16 def_height = max<int16>(min(w->window_desc->GetDefaultHeight(), _screen.height - 50), w->nested_root->smallest_y);
711 
712  int dx = (w->resize.step_width == 0) ? 0 : def_width - w->width;
713  int dy = (w->resize.step_height == 0) ? 0 : def_height - w->height;
714  /* dx and dy has to go by step.. calculate it.
715  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
716  if (w->resize.step_width > 1) dx -= dx % (int)w->resize.step_width;
717  if (w->resize.step_height > 1) dy -= dy % (int)w->resize.step_height;
718  ResizeWindow(w, dx, dy, false);
719  }
720 
721  nw->SetLowered(true);
722  nw->SetDirty(w);
723  w->SetTimeout();
724  break;
725  }
726 
727  case WWT_DEBUGBOX:
729  break;
730 
731  case WWT_SHADEBOX:
732  nw->SetDirty(w);
733  w->SetShaded(!w->IsShaded());
734  return;
735 
736  case WWT_STICKYBOX:
737  w->flags ^= WF_STICKY;
738  nw->SetDirty(w);
739  if (_ctrl_pressed) w->window_desc->pref_sticky = (w->flags & WF_STICKY) != 0;
740  return;
741 
742  default:
743  break;
744  }
745 
746  /* Widget has no index, so the window is not interested in it. */
747  if (widget_index < 0) return;
748 
749  /* Check if the widget is highlighted; if so, disable highlight and dispatch an event to the GameScript */
750  if (w->IsWidgetHighlighted(widget_index)) {
751  w->SetWidgetHighlight(widget_index, TC_INVALID);
752  Game::NewEvent(new ScriptEventWindowWidgetClick((ScriptWindow::WindowClass)w->window_class, w->window_number, widget_index));
753  }
754 
755  w->OnClick(pt, widget_index, click_count);
756 }
757 
764 static void DispatchRightClickEvent(Window *w, int x, int y)
765 {
766  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
767  if (wid == NULL) return;
768 
769  /* No widget to handle, or the window is not interested in it. */
770  if (wid->index >= 0) {
771  Point pt = { x, y };
772  if (w->OnRightClick(pt, wid->index)) return;
773  }
774 
775  /* Right-click close is enabled and there is a closebox */
777  delete w;
778  } else if (_settings_client.gui.hover_delay_ms == 0 && wid->tool_tip != 0) {
779  GuiShowTooltips(w, wid->tool_tip, 0, NULL, TCC_RIGHT_CLICK);
780  }
781 }
782 
789 static void DispatchHoverEvent(Window *w, int x, int y)
790 {
791  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
792 
793  /* No widget to handle */
794  if (wid == NULL) return;
795 
796  /* Show the tooltip if there is any */
797  if (wid->tool_tip != 0) {
798  GuiShowTooltips(w, wid->tool_tip);
799  return;
800  }
801 
802  /* Widget has no index, so the window is not interested in it. */
803  if (wid->index < 0) return;
804 
805  Point pt = { x, y };
806  w->OnHover(pt, wid->index);
807 }
808 
816 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
817 {
818  if (nwid == NULL) return;
819 
820  /* Using wheel on caption/shade-box shades or unshades the window. */
821  if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
822  w->SetShaded(wheel < 0);
823  return;
824  }
825 
826  /* Wheeling a vertical scrollbar. */
827  if (nwid->type == NWID_VSCROLLBAR) {
828  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
829  if (sb->GetCount() > sb->GetCapacity()) {
830  sb->UpdatePosition(wheel);
831  w->SetDirty();
832  }
833  return;
834  }
835 
836  /* Scroll the widget attached to the scrollbar. */
837  Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : NULL);
838  if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
839  sb->UpdatePosition(wheel);
840  w->SetDirty();
841  }
842 }
843 
849 static bool MayBeShown(const Window *w)
850 {
851  /* If we're not modal, everything is okay. */
852  if (!HasModalProgress()) return true;
853 
854  switch (w->window_class) {
855  case WC_MAIN_WINDOW:
856  case WC_MODAL_PROGRESS:
858  return true;
859 
860  default:
861  return false;
862  }
863 }
864 
877 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
878 {
879  const Window *v;
881  if (MayBeShown(v) &&
882  right > v->left &&
883  bottom > v->top &&
884  left < v->left + v->width &&
885  top < v->top + v->height) {
886  /* v and rectangle intersect with each other */
887  int x;
888 
889  if (left < (x = v->left)) {
890  DrawOverlappedWindow(w, left, top, x, bottom);
891  DrawOverlappedWindow(w, x, top, right, bottom);
892  return;
893  }
894 
895  if (right > (x = v->left + v->width)) {
896  DrawOverlappedWindow(w, left, top, x, bottom);
897  DrawOverlappedWindow(w, x, top, right, bottom);
898  return;
899  }
900 
901  if (top < (x = v->top)) {
902  DrawOverlappedWindow(w, left, top, right, x);
903  DrawOverlappedWindow(w, left, x, right, bottom);
904  return;
905  }
906 
907  if (bottom > (x = v->top + v->height)) {
908  DrawOverlappedWindow(w, left, top, right, x);
909  DrawOverlappedWindow(w, left, x, right, bottom);
910  return;
911  }
912 
913  return;
914  }
915  }
916 
917  /* Setup blitter, and dispatch a repaint event to window *wz */
918  DrawPixelInfo *dp = _cur_dpi;
919  dp->width = right - left;
920  dp->height = bottom - top;
921  dp->left = left - w->left;
922  dp->top = top - w->top;
923  dp->pitch = _screen.pitch;
924  dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
925  dp->zoom = ZOOM_LVL_NORMAL;
926  w->OnPaint();
927 }
928 
937 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
938 {
939  Window *w;
940  DrawPixelInfo bk;
941  _cur_dpi = &bk;
942 
943  FOR_ALL_WINDOWS_FROM_BACK(w) {
944  if (MayBeShown(w) &&
945  right > w->left &&
946  bottom > w->top &&
947  left < w->left + w->width &&
948  top < w->top + w->height) {
949  /* Window w intersects with the rectangle => needs repaint */
950  DrawOverlappedWindow(w, max(left, w->left), max(top, w->top), min(right, w->left + w->width), min(bottom, w->top + w->height));
951  }
952  }
953 }
954 
959 void Window::SetDirty() const
960 {
961  SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
962 }
963 
970 void Window::ReInit(int rx, int ry)
971 {
972  this->SetDirty(); // Mark whole current window as dirty.
973 
974  /* Save current size. */
975  int window_width = this->width;
976  int window_height = this->height;
977 
978  this->OnInit();
979  /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
980  this->nested_root->SetupSmallestSize(this, false);
981  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
982  this->width = this->nested_root->smallest_x;
983  this->height = this->nested_root->smallest_y;
984  this->resize.step_width = this->nested_root->resize_x;
985  this->resize.step_height = this->nested_root->resize_y;
986 
987  /* Resize as close to the original size + requested resize as possible. */
988  window_width = max(window_width + rx, this->width);
989  window_height = max(window_height + ry, this->height);
990  int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
991  int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
992  /* dx and dy has to go by step.. calculate it.
993  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
994  if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
995  if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
996 
997  ResizeWindow(this, dx, dy);
998  /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
999 }
1000 
1006 void Window::SetShaded(bool make_shaded)
1007 {
1008  if (this->shade_select == NULL) return;
1009 
1010  int desired = make_shaded ? SZSP_HORIZONTAL : 0;
1011  if (this->shade_select->shown_plane != desired) {
1012  if (make_shaded) {
1013  if (this->nested_focus != NULL) this->UnfocusFocusedWidget();
1014  this->unshaded_size.width = this->width;
1015  this->unshaded_size.height = this->height;
1016  this->shade_select->SetDisplayedPlane(desired);
1017  this->ReInit(0, -this->height);
1018  } else {
1019  this->shade_select->SetDisplayedPlane(desired);
1020  int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
1021  int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
1022  this->ReInit(dx, dy);
1023  }
1024  }
1025 }
1026 
1034 {
1035  Window *v;
1036  FOR_ALL_WINDOWS_FROM_BACK(v) {
1037  if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
1038  }
1039 
1040  return NULL;
1041 }
1042 
1048 {
1049  Window *child = FindChildWindow(this, wc);
1050  while (child != NULL) {
1051  delete child;
1052  child = FindChildWindow(this, wc);
1053  }
1054 }
1055 
1060 {
1061  if (_thd.window_class == this->window_class &&
1062  _thd.window_number == this->window_number) {
1064  }
1065 
1066  /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
1067  if (_mouseover_last_w == this) _mouseover_last_w = NULL;
1068 
1069  /* We can't scroll the window when it's closed. */
1070  if (_last_scroll_window == this) _last_scroll_window = NULL;
1071 
1072  /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
1073  if (_focused_window == this) {
1074  this->OnFocusLost();
1075  _focused_window = NULL;
1076  }
1077 
1078  this->DeleteChildWindows();
1079 
1080  if (this->viewport != NULL) DeleteWindowViewport(this);
1081 
1082  this->SetDirty();
1083 
1084  free(this->nested_array); // Contents is released through deletion of #nested_root.
1085  delete this->nested_root;
1086 
1087  /*
1088  * Make fairly sure that this is written, and not "optimized" away.
1089  * The delete operator is overwritten to not delete it; the deletion
1090  * happens at a later moment in time after the window has been
1091  * removed from the list of windows to prevent issues with items
1092  * being removed during the iteration as not one but more windows
1093  * may be removed by a single call to ~Window by means of the
1094  * DeleteChildWindows function.
1095  */
1096  const_cast<volatile WindowClass &>(this->window_class) = WC_INVALID;
1097 }
1098 
1106 {
1107  Window *w;
1108  FOR_ALL_WINDOWS_FROM_BACK(w) {
1109  if (w->window_class == cls && w->window_number == number) return w;
1110  }
1111 
1112  return NULL;
1113 }
1114 
1122 {
1123  Window *w;
1124  FOR_ALL_WINDOWS_FROM_BACK(w) {
1125  if (w->window_class == cls) return w;
1126  }
1127 
1128  return NULL;
1129 }
1130 
1138 {
1139  Window *w = FindWindowById(cls, number);
1140  if (force || w == NULL ||
1141  (w->flags & WF_STICKY) == 0) {
1142  delete w;
1143  }
1144 }
1145 
1151 {
1152  Window *w;
1153 
1154 restart_search:
1155  /* When we find the window to delete, we need to restart the search
1156  * as deleting this window could cascade in deleting (many) others
1157  * anywhere in the z-array */
1158  FOR_ALL_WINDOWS_FROM_BACK(w) {
1159  if (w->window_class == cls) {
1160  delete w;
1161  goto restart_search;
1162  }
1163  }
1164 }
1165 
1173 {
1174  Window *w;
1175 
1176 restart_search:
1177  /* When we find the window to delete, we need to restart the search
1178  * as deleting this window could cascade in deleting (many) others
1179  * anywhere in the z-array */
1180  FOR_ALL_WINDOWS_FROM_BACK(w) {
1181  if (w->owner == id) {
1182  delete w;
1183  goto restart_search;
1184  }
1185  }
1186 
1187  /* Also delete the company specific windows that don't have a company-colour. */
1189 }
1190 
1198 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1199 {
1200  Window *w;
1201  FOR_ALL_WINDOWS_FROM_BACK(w) {
1202  if (w->owner != old_owner) continue;
1203 
1204  switch (w->window_class) {
1205  case WC_COMPANY_COLOUR:
1206  case WC_FINANCES:
1207  case WC_STATION_LIST:
1208  case WC_TRAINS_LIST:
1209  case WC_ROADVEH_LIST:
1210  case WC_SHIPS_LIST:
1211  case WC_AIRCRAFT_LIST:
1212  case WC_BUY_COMPANY:
1213  case WC_COMPANY:
1215  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().
1216  continue;
1217 
1218  default:
1219  w->owner = new_owner;
1220  break;
1221  }
1222  }
1223 }
1224 
1225 static void BringWindowToFront(Window *w);
1226 
1235 {
1236  Window *w = FindWindowById(cls, number);
1237 
1238  if (w != NULL) {
1239  if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1240 
1241  w->SetWhiteBorder();
1242  BringWindowToFront(w);
1243  w->SetDirty();
1244  }
1245 
1246  return w;
1247 }
1248 
1249 static inline bool IsVitalWindow(const Window *w)
1250 {
1251  switch (w->window_class) {
1252  case WC_MAIN_TOOLBAR:
1253  case WC_STATUS_BAR:
1254  case WC_NEWS_WINDOW:
1255  case WC_SEND_NETWORK_MSG:
1256  return true;
1257 
1258  default:
1259  return false;
1260  }
1261 }
1262 
1272 {
1273  assert(wc != WC_INVALID);
1274 
1275  uint z_priority = 0;
1276 
1277  switch (wc) {
1278  case WC_ENDSCREEN:
1279  ++z_priority;
1280  FALLTHROUGH;
1281 
1282  case WC_HIGHSCORE:
1283  ++z_priority;
1284  FALLTHROUGH;
1285 
1286  case WC_TOOLTIPS:
1287  ++z_priority;
1288  FALLTHROUGH;
1289 
1290  case WC_DROPDOWN_MENU:
1291  ++z_priority;
1292  FALLTHROUGH;
1293 
1294  case WC_MAIN_TOOLBAR:
1295  case WC_STATUS_BAR:
1296  ++z_priority;
1297  FALLTHROUGH;
1298 
1299  case WC_OSK:
1300  ++z_priority;
1301  FALLTHROUGH;
1302 
1303  case WC_QUERY_STRING:
1304  case WC_SEND_NETWORK_MSG:
1305  ++z_priority;
1306  FALLTHROUGH;
1307 
1308  case WC_ERRMSG:
1310  case WC_MODAL_PROGRESS:
1312  case WC_SAVE_PRESET:
1313  ++z_priority;
1314  FALLTHROUGH;
1315 
1316  case WC_GENERATE_LANDSCAPE:
1317  case WC_SAVELOAD:
1318  case WC_GAME_OPTIONS:
1319  case WC_CUSTOM_CURRENCY:
1320  case WC_NETWORK_WINDOW:
1321  case WC_GRF_PARAMETERS:
1322  case WC_AI_LIST:
1323  case WC_AI_SETTINGS:
1324  case WC_TEXTFILE:
1325  ++z_priority;
1326  FALLTHROUGH;
1327 
1328  case WC_CONSOLE:
1329  ++z_priority;
1330  FALLTHROUGH;
1331 
1332  case WC_NEWS_WINDOW:
1333  ++z_priority;
1334  FALLTHROUGH;
1335 
1336  default:
1337  ++z_priority;
1338  FALLTHROUGH;
1339 
1340  case WC_MAIN_WINDOW:
1341  return z_priority;
1342  }
1343 }
1344 
1350 {
1351  assert(w->z_front == NULL && w->z_back == NULL);
1352 
1353  if (_z_front_window == NULL) {
1354  /* It's the only window. */
1355  _z_front_window = _z_back_window = w;
1356  w->z_front = w->z_back = NULL;
1357  } else {
1358  /* Search down the z-ordering for its location. */
1359  Window *v = _z_front_window;
1360  uint last_z_priority = UINT_MAX;
1361  while (v != NULL && (v->window_class == WC_INVALID || GetWindowZPriority(v->window_class) > GetWindowZPriority(w->window_class))) {
1362  if (v->window_class != WC_INVALID) {
1363  /* Sanity check z-ordering, while we're at it. */
1364  assert(last_z_priority >= GetWindowZPriority(v->window_class));
1365  last_z_priority = GetWindowZPriority(v->window_class);
1366  }
1367 
1368  v = v->z_back;
1369  }
1370 
1371  if (v == NULL) {
1372  /* It's the new back window. */
1373  w->z_front = _z_back_window;
1374  w->z_back = NULL;
1375  _z_back_window->z_back = w;
1376  _z_back_window = w;
1377  } else if (v == _z_front_window) {
1378  /* It's the new front window. */
1379  w->z_front = NULL;
1380  w->z_back = _z_front_window;
1381  _z_front_window->z_front = w;
1382  _z_front_window = w;
1383  } else {
1384  /* It's somewhere else in the z-ordering. */
1385  w->z_front = v->z_front;
1386  w->z_back = v;
1387  v->z_front->z_back = w;
1388  v->z_front = w;
1389  }
1390  }
1391 }
1392 
1393 
1399 {
1400  if (w->z_front == NULL) {
1401  assert(_z_front_window == w);
1402  _z_front_window = w->z_back;
1403  } else {
1404  w->z_front->z_back = w->z_back;
1405  }
1406 
1407  if (w->z_back == NULL) {
1408  assert(_z_back_window == w);
1409  _z_back_window = w->z_front;
1410  } else {
1411  w->z_back->z_front = w->z_front;
1412  }
1413 
1414  w->z_front = w->z_back = NULL;
1415 }
1416 
1423 {
1426 
1427  w->SetDirty();
1428 }
1429 
1439 {
1440  /* Set up window properties; some of them are needed to set up smallest size below */
1441  this->window_class = this->window_desc->cls;
1442  this->SetWhiteBorder();
1443  if (this->window_desc->default_pos == WDP_CENTER) this->flags |= WF_CENTERED;
1444  this->owner = INVALID_OWNER;
1445  this->nested_focus = NULL;
1446  this->window_number = window_number;
1447 
1448  this->OnInit();
1449  /* Initialize nested widget tree. */
1450  if (this->nested_array == NULL) {
1451  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1452  this->nested_root->SetupSmallestSize(this, true);
1453  } else {
1454  this->nested_root->SetupSmallestSize(this, false);
1455  }
1456  /* Initialize to smallest size. */
1457  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1458 
1459  /* Further set up window properties,
1460  * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1461  this->resize.step_width = this->nested_root->resize_x;
1462  this->resize.step_height = this->nested_root->resize_y;
1463 
1464  /* Give focus to the opened window unless a text box
1465  * of focused window has focus (so we don't interrupt typing). But if the new
1466  * window has a text box, then take focus anyway. */
1467  if (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != NULL) SetFocusedWindow(this);
1468 
1469  /* Insert the window into the correct location in the z-ordering. */
1470  AddWindowToZOrdering(this);
1471 }
1472 
1480 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1481 {
1482  this->left = x;
1483  this->top = y;
1484  this->width = sm_width;
1485  this->height = sm_height;
1486 }
1487 
1498 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
1499 {
1500  def_width = max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1501  def_height = max(def_height, this->height);
1502  /* Try to make windows smaller when our window is too small.
1503  * w->(width|height) is normally the same as min_(width|height),
1504  * but this way the GUIs can be made a little more dynamic;
1505  * one can use the same spec for multiple windows and those
1506  * can then determine the real minimum size of the window. */
1507  if (this->width != def_width || this->height != def_height) {
1508  /* Think about the overlapping toolbars when determining the minimum window size */
1509  int free_height = _screen.height;
1510  const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1511  if (wt != NULL) free_height -= wt->height;
1513  if (wt != NULL) free_height -= wt->height;
1514 
1515  int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
1516  int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
1517 
1518  /* X and Y has to go by step.. calculate it.
1519  * The cast to int is necessary else x/y are implicitly casted to
1520  * unsigned int, which won't work. */
1521  if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1522  if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1523 
1524  ResizeWindow(this, enlarge_x, enlarge_y);
1525  /* ResizeWindow() calls this->OnResize(). */
1526  } else {
1527  /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1528  this->OnResize();
1529  }
1530 
1531  int nx = this->left;
1532  int ny = this->top;
1533 
1534  if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1535 
1536  const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1537  ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
1538  nx = max(nx, 0);
1539 
1540  if (this->viewport != NULL) {
1541  this->viewport->left += nx - this->left;
1542  this->viewport->top += ny - this->top;
1543  }
1544  this->left = nx;
1545  this->top = ny;
1546 
1547  this->SetDirty();
1548 }
1549 
1562 static bool IsGoodAutoPlace1(int left, int top, int width, int height, int toolbar_y, Point &pos)
1563 {
1564  int right = width + left;
1565  int bottom = height + top;
1566 
1567  if (left < 0 || top < toolbar_y || right > _screen.width || bottom > _screen.height) return false;
1568 
1569  /* Make sure it is not obscured by any window. */
1570  const Window *w;
1571  FOR_ALL_WINDOWS_FROM_BACK(w) {
1572  if (w->window_class == WC_MAIN_WINDOW) continue;
1573 
1574  if (right > w->left &&
1575  w->left + w->width > left &&
1576  bottom > w->top &&
1577  w->top + w->height > top) {
1578  return false;
1579  }
1580  }
1581 
1582  pos.x = left;
1583  pos.y = top;
1584  return true;
1585 }
1586 
1599 static bool IsGoodAutoPlace2(int left, int top, int width, int height, int toolbar_y, Point &pos)
1600 {
1601  bool rtl = _current_text_dir == TD_RTL;
1602 
1603  /* Left part of the rectangle may be at most 1/4 off-screen,
1604  * right part of the rectangle may be at most 1/2 off-screen
1605  */
1606  if (rtl) {
1607  if (left < -(width >> 1) || left > _screen.width - (width >> 2)) return false;
1608  } else {
1609  if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1610  }
1611 
1612  /* Bottom part of the rectangle may be at most 1/4 off-screen */
1613  if (top < toolbar_y || top > _screen.height - (height >> 2)) return false;
1614 
1615  /* Make sure it is not obscured by any window. */
1616  const Window *w;
1617  FOR_ALL_WINDOWS_FROM_BACK(w) {
1618  if (w->window_class == WC_MAIN_WINDOW) continue;
1619 
1620  if (left + width > w->left &&
1621  w->left + w->width > left &&
1622  top + height > w->top &&
1623  w->top + w->height > top) {
1624  return false;
1625  }
1626  }
1627 
1628  pos.x = left;
1629  pos.y = top;
1630  return true;
1631 }
1632 
1639 static Point GetAutoPlacePosition(int width, int height)
1640 {
1641  Point pt;
1642 
1643  bool rtl = _current_text_dir == TD_RTL;
1644 
1645  /* First attempt, try top-left of the screen */
1646  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1647  const int toolbar_y = main_toolbar != NULL ? main_toolbar->height : 0;
1648  if (IsGoodAutoPlace1(rtl ? _screen.width - width : 0, toolbar_y, width, height, toolbar_y, pt)) return pt;
1649 
1650  /* Second attempt, try around all existing windows.
1651  * The new window must be entirely on-screen, and not overlap with an existing window.
1652  * Eight starting points are tried, two at each corner.
1653  */
1654  const Window *w;
1655  FOR_ALL_WINDOWS_FROM_BACK(w) {
1656  if (w->window_class == WC_MAIN_WINDOW) continue;
1657 
1658  if (IsGoodAutoPlace1(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1659  if (IsGoodAutoPlace1(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1660  if (IsGoodAutoPlace1(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1661  if (IsGoodAutoPlace1(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1662  if (IsGoodAutoPlace1(w->left + w->width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1663  if (IsGoodAutoPlace1(w->left - width, w->top + w->height - height, width, height, toolbar_y, pt)) return pt;
1664  if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1665  if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height, width, height, toolbar_y, pt)) return pt;
1666  }
1667 
1668  /* Third attempt, try around all existing windows.
1669  * The new window may be partly off-screen, and must not overlap with an existing window.
1670  * Only four starting points are tried.
1671  */
1672  FOR_ALL_WINDOWS_FROM_BACK(w) {
1673  if (w->window_class == WC_MAIN_WINDOW) continue;
1674 
1675  if (IsGoodAutoPlace2(w->left + w->width, w->top, width, height, toolbar_y, pt)) return pt;
1676  if (IsGoodAutoPlace2(w->left - width, w->top, width, height, toolbar_y, pt)) return pt;
1677  if (IsGoodAutoPlace2(w->left, w->top + w->height, width, height, toolbar_y, pt)) return pt;
1678  if (IsGoodAutoPlace2(w->left, w->top - height, width, height, toolbar_y, pt)) return pt;
1679  }
1680 
1681  /* Fourth and final attempt, put window at diagonal starting from (0, toolbar_y), try multiples
1682  * of the closebox
1683  */
1684  int left = rtl ? _screen.width - width : 0, top = toolbar_y;
1685  int offset_x = rtl ? -(int)NWidgetLeaf::closebox_dimension.width : (int)NWidgetLeaf::closebox_dimension.width;
1687 
1688 restart:
1689  FOR_ALL_WINDOWS_FROM_BACK(w) {
1690  if (w->left == left && w->top == top) {
1691  left += offset_x;
1692  top += offset_y;
1693  goto restart;
1694  }
1695  }
1696 
1697  pt.x = left;
1698  pt.y = top;
1699  return pt;
1700 }
1701 
1709 {
1710  const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1711  assert(w != NULL);
1712  Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1713  return pt;
1714 }
1715 
1733 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1734 {
1735  Point pt;
1736  const Window *w;
1737 
1738  int16 default_width = max(desc->GetDefaultWidth(), sm_width);
1739  int16 default_height = max(desc->GetDefaultHeight(), sm_height);
1740 
1741  if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ && (w = FindWindowById(desc->parent_cls, window_number)) != NULL) {
1742  bool rtl = _current_text_dir == TD_RTL;
1743  if (desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) {
1744  pt.x = w->left + (rtl ? w->width - default_width : 0);
1745  pt.y = w->top + w->height;
1746  return pt;
1747  } else {
1748  /* Position child window with offset of closebox, but make sure that either closebox or resizebox is visible
1749  * - Y position: closebox of parent + closebox of child + statusbar
1750  * - X position: closebox on left/right, resizebox on right/left (depending on ltr/rtl)
1751  */
1753  if (w->top + 3 * indent_y < _screen.height) {
1754  pt.y = w->top + indent_y;
1755  int indent_close = NWidgetLeaf::closebox_dimension.width;
1756  int indent_resize = NWidgetLeaf::resizebox_dimension.width;
1757  if (_current_text_dir == TD_RTL) {
1758  pt.x = max(w->left + w->width - default_width - indent_close, 0);
1759  if (pt.x + default_width >= indent_close && pt.x + indent_resize <= _screen.width) return pt;
1760  } else {
1761  pt.x = min(w->left + indent_close, _screen.width - default_width);
1762  if (pt.x + default_width >= indent_resize && pt.x + indent_close <= _screen.width) return pt;
1763  }
1764  }
1765  }
1766  }
1767 
1768  switch (desc->default_pos) {
1769  case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1770  return GetToolbarAlignedWindowPosition(default_width);
1771 
1772  case WDP_AUTO: // Find a good automatic position for the window
1773  return GetAutoPlacePosition(default_width, default_height);
1774 
1775  case WDP_CENTER: // Centre the window horizontally
1776  pt.x = (_screen.width - default_width) / 2;
1777  pt.y = (_screen.height - default_height) / 2;
1778  break;
1779 
1780  case WDP_MANUAL:
1781  pt.x = 0;
1782  pt.y = 0;
1783  break;
1784 
1785  default:
1786  NOT_REACHED();
1787  }
1788 
1789  return pt;
1790 }
1791 
1792 /* virtual */ Point Window::OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1793 {
1794  return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1795 }
1796 
1804 void Window::CreateNestedTree(bool fill_nested)
1805 {
1806  int biggest_index = -1;
1807  this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_parts, this->window_desc->nwid_length, &biggest_index, &this->shade_select);
1808  this->nested_array_size = (uint)(biggest_index + 1);
1809 
1810  if (fill_nested) {
1811  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1812  this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
1813  }
1814 }
1815 
1821 {
1822  this->InitializeData(window_number);
1823  this->ApplyDefaults();
1824  Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1825  this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1826  this->FindWindowPlacementAndResize(this->window_desc->GetDefaultWidth(), this->window_desc->GetDefaultHeight());
1827 }
1828 
1834 {
1835  this->CreateNestedTree(false);
1836  this->FinishInitNested(window_number);
1837 }
1838 
1843 Window::Window(WindowDesc *desc) : window_desc(desc), scrolling_scrollbar(-1)
1844 {
1845 }
1846 
1854 Window *FindWindowFromPt(int x, int y)
1855 {
1856  Window *w;
1857  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1858  if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1859  return w;
1860  }
1861  }
1862 
1863  return NULL;
1864 }
1865 
1870 {
1871  IConsoleClose();
1872 
1873  _z_back_window = NULL;
1874  _z_front_window = NULL;
1875  _focused_window = NULL;
1876  _mouseover_last_w = NULL;
1877  _last_scroll_window = NULL;
1878  _scrolling_viewport = false;
1879  _mouse_hovering = false;
1880 
1881  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1882  NWidgetScrollbar::InvalidateDimensionCache();
1883 
1884  ShowFirstError();
1885 }
1886 
1891 {
1893 
1894  Window *w;
1895  FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
1896 
1897  for (w = _z_front_window; w != NULL; /* nothing */) {
1898  Window *to_del = w;
1899  w = w->z_back;
1900  free(to_del);
1901  }
1902 
1903  _z_front_window = NULL;
1904  _z_back_window = NULL;
1905 }
1906 
1911 {
1913  InitWindowSystem();
1914  _thd.Reset();
1915 }
1916 
1917 static void DecreaseWindowCounters()
1918 {
1919  Window *w;
1920  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1921  if (_scroller_click_timeout == 0) {
1922  /* Unclick scrollbar buttons if they are pressed. */
1923  for (uint i = 0; i < w->nested_array_size; i++) {
1924  NWidgetBase *nwid = w->nested_array[i];
1925  if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
1926  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1929  w->scrolling_scrollbar = -1;
1930  sb->SetDirty(w);
1931  }
1932  }
1933  }
1934  }
1935 
1936  /* Handle editboxes */
1937  for (SmallMap<int, QueryString*>::Pair *it = w->querystrings.Begin(); it != w->querystrings.End(); ++it) {
1938  it->second->HandleEditBox(w, it->first);
1939  }
1940 
1941  w->OnMouseLoop();
1942  }
1943 
1944  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1945  if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) {
1946  CLRBITS(w->flags, WF_TIMEOUT);
1947 
1948  w->OnTimeout();
1949  w->RaiseButtons(true);
1950  }
1951  }
1952 }
1953 
1954 static void HandlePlacePresize()
1955 {
1956  if (_special_mouse_mode != WSM_PRESIZE) return;
1957 
1958  Window *w = _thd.GetCallbackWnd();
1959  if (w == NULL) return;
1960 
1961  Point pt = GetTileBelowCursor();
1962  if (pt.x == -1) {
1963  _thd.selend.x = -1;
1964  return;
1965  }
1966 
1967  w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1968 }
1969 
1975 {
1977 
1978  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
1979 
1980  Window *w = _thd.GetCallbackWnd();
1981  if (w != NULL) {
1982  /* Send an event in client coordinates. */
1983  Point pt;
1984  pt.x = _cursor.pos.x - w->left;
1985  pt.y = _cursor.pos.y - w->top;
1986  if (_left_button_down) {
1987  w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
1988  } else {
1989  w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
1990  }
1991  }
1992 
1993  if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
1994  return ES_HANDLED;
1995 }
1996 
1998 static void HandleMouseOver()
1999 {
2000  Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2001 
2002  /* We changed window, put a MOUSEOVER event to the last window */
2003  if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
2004  /* Reset mouse-over coordinates of previous window */
2005  Point pt = { -1, -1 };
2006  _mouseover_last_w->OnMouseOver(pt, 0);
2007  }
2008 
2009  /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
2010  _mouseover_last_w = w;
2011 
2012  if (w != NULL) {
2013  /* send an event in client coordinates. */
2014  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
2015  const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
2016  if (widget != NULL) w->OnMouseOver(pt, widget->index);
2017  }
2018 }
2019 
2021 static const int MIN_VISIBLE_TITLE_BAR = 13;
2022 
2027 };
2028 
2039 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
2040 {
2041  if (v == NULL) return;
2042 
2043  int v_bottom = v->top + v->height;
2044  int v_right = v->left + v->width;
2045  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.
2046 
2047  if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
2048  if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
2049 
2050  /* Vertically, the rectangle is hidden behind v. */
2051  if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
2052  if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
2053  return;
2054  }
2055  if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
2056  if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
2057  return;
2058  }
2059 
2060  /* Horizontally also hidden, force movement to a safe area. */
2061  if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
2062  *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
2063  } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
2064  *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2065  } else {
2066  *ny = safe_y;
2067  }
2068 }
2069 
2077 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2078 {
2079  /* Search for the title bar rectangle. */
2080  Rect caption_rect;
2081  const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2082  if (caption != NULL) {
2083  caption_rect.left = caption->pos_x;
2084  caption_rect.right = caption->pos_x + caption->current_x;
2085  caption_rect.top = caption->pos_y;
2086  caption_rect.bottom = caption->pos_y + caption->current_y;
2087 
2088  /* Make sure the window doesn't leave the screen */
2089  nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2090  ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2091 
2092  /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2093  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2094  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2095  }
2096 
2097  if (w->viewport != NULL) {
2098  w->viewport->left += nx - w->left;
2099  w->viewport->top += ny - w->top;
2100  }
2101 
2102  w->left = nx;
2103  w->top = ny;
2104 }
2105 
2116 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
2117 {
2118  if (delta_x != 0 || delta_y != 0) {
2119  if (clamp_to_screen) {
2120  /* Determine the new right/bottom position. If that is outside of the bounds of
2121  * the resolution clamp it in such a manner that it stays within the bounds. */
2122  int new_right = w->left + w->width + delta_x;
2123  int new_bottom = w->top + w->height + delta_y;
2124  if (new_right >= (int)_cur_resolution.width) delta_x -= Ceil(new_right - _cur_resolution.width, max(1U, w->nested_root->resize_x));
2125  if (new_bottom >= (int)_cur_resolution.height) delta_y -= Ceil(new_bottom - _cur_resolution.height, max(1U, w->nested_root->resize_y));
2126  }
2127 
2128  w->SetDirty();
2129 
2130  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);
2131  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);
2132  assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2133  assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2134 
2136  w->width = w->nested_root->current_x;
2137  w->height = w->nested_root->current_y;
2138  }
2139 
2140  EnsureVisibleCaption(w, w->left, w->top);
2141 
2142  /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
2143  w->OnResize();
2144  w->SetDirty();
2145 }
2146 
2153 {
2155  return (w == NULL) ? 0 : w->top + w->height;
2156 }
2157 
2164 {
2166  return (w == NULL) ? _screen.height : w->top;
2167 }
2168 
2169 static bool _dragging_window;
2170 
2176 {
2177  /* Get out immediately if no window is being dragged at all. */
2178  if (!_dragging_window) return ES_NOT_HANDLED;
2179 
2180  /* If button still down, but cursor hasn't moved, there is nothing to do. */
2181  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2182 
2183  /* Otherwise find the window... */
2184  Window *w;
2185  FOR_ALL_WINDOWS_FROM_BACK(w) {
2186  if (w->flags & WF_DRAGGING) {
2187  /* Stop the dragging if the left mouse button was released */
2188  if (!_left_button_down) {
2189  w->flags &= ~WF_DRAGGING;
2190  break;
2191  }
2192 
2193  w->SetDirty();
2194 
2195  int x = _cursor.pos.x + _drag_delta.x;
2196  int y = _cursor.pos.y + _drag_delta.y;
2197  int nx = x;
2198  int ny = y;
2199 
2201  const Window *v;
2202 
2205  int delta;
2206 
2207  FOR_ALL_WINDOWS_FROM_BACK(v) {
2208  if (v == w) continue; // Don't snap at yourself
2209 
2210  if (y + w->height > v->top && y < v->top + v->height) {
2211  /* Your left border <-> other right border */
2212  delta = abs(v->left + v->width - x);
2213  if (delta <= hsnap) {
2214  nx = v->left + v->width;
2215  hsnap = delta;
2216  }
2217 
2218  /* Your right border <-> other left border */
2219  delta = abs(v->left - x - w->width);
2220  if (delta <= hsnap) {
2221  nx = v->left - w->width;
2222  hsnap = delta;
2223  }
2224  }
2225 
2226  if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2227  /* Your left border <-> other left border */
2228  delta = abs(v->left - x);
2229  if (delta <= hsnap) {
2230  nx = v->left;
2231  hsnap = delta;
2232  }
2233 
2234  /* Your right border <-> other right border */
2235  delta = abs(v->left + v->width - x - w->width);
2236  if (delta <= hsnap) {
2237  nx = v->left + v->width - w->width;
2238  hsnap = delta;
2239  }
2240  }
2241 
2242  if (x + w->width > v->left && x < v->left + v->width) {
2243  /* Your top border <-> other bottom border */
2244  delta = abs(v->top + v->height - y);
2245  if (delta <= vsnap) {
2246  ny = v->top + v->height;
2247  vsnap = delta;
2248  }
2249 
2250  /* Your bottom border <-> other top border */
2251  delta = abs(v->top - y - w->height);
2252  if (delta <= vsnap) {
2253  ny = v->top - w->height;
2254  vsnap = delta;
2255  }
2256  }
2257 
2258  if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2259  /* Your top border <-> other top border */
2260  delta = abs(v->top - y);
2261  if (delta <= vsnap) {
2262  ny = v->top;
2263  vsnap = delta;
2264  }
2265 
2266  /* Your bottom border <-> other bottom border */
2267  delta = abs(v->top + v->height - y - w->height);
2268  if (delta <= vsnap) {
2269  ny = v->top + v->height - w->height;
2270  vsnap = delta;
2271  }
2272  }
2273  }
2274  }
2275 
2276  EnsureVisibleCaption(w, nx, ny);
2277 
2278  w->SetDirty();
2279  return ES_HANDLED;
2280  } else if (w->flags & WF_SIZING) {
2281  /* Stop the sizing if the left mouse button was released */
2282  if (!_left_button_down) {
2283  w->flags &= ~WF_SIZING;
2284  w->SetDirty();
2285  break;
2286  }
2287 
2288  /* Compute difference in pixels between cursor position and reference point in the window.
2289  * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2290  */
2291  int x, y = _cursor.pos.y - _drag_delta.y;
2292  if (w->flags & WF_SIZING_LEFT) {
2293  x = _drag_delta.x - _cursor.pos.x;
2294  } else {
2295  x = _cursor.pos.x - _drag_delta.x;
2296  }
2297 
2298  /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2299  if (w->resize.step_width == 0) x = 0;
2300  if (w->resize.step_height == 0) y = 0;
2301 
2302  /* Check the resize button won't go past the bottom of the screen */
2303  if (w->top + w->height + y > _screen.height) {
2304  y = _screen.height - w->height - w->top;
2305  }
2306 
2307  /* X and Y has to go by step.. calculate it.
2308  * The cast to int is necessary else x/y are implicitly casted to
2309  * unsigned int, which won't work. */
2310  if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2311  if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2312 
2313  /* Check that we don't go below the minimum set size */
2314  if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2315  x = w->nested_root->smallest_x - w->width;
2316  }
2317  if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2318  y = w->nested_root->smallest_y - w->height;
2319  }
2320 
2321  /* Window already on size */
2322  if (x == 0 && y == 0) return ES_HANDLED;
2323 
2324  /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2325  _drag_delta.y += y;
2326  if ((w->flags & WF_SIZING_LEFT) && x != 0) {
2327  _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2328  w->SetDirty();
2329  w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2330  /* ResizeWindow() below ensures marking new position as dirty. */
2331  } else {
2332  _drag_delta.x += x;
2333  }
2334 
2335  /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
2336  ResizeWindow(w, x, y);
2337  return ES_HANDLED;
2338  }
2339  }
2340 
2341  _dragging_window = false;
2342  return ES_HANDLED;
2343 }
2344 
2349 static void StartWindowDrag(Window *w)
2350 {
2351  w->flags |= WF_DRAGGING;
2352  w->flags &= ~WF_CENTERED;
2353  _dragging_window = true;
2354 
2355  _drag_delta.x = w->left - _cursor.pos.x;
2356  _drag_delta.y = w->top - _cursor.pos.y;
2357 
2358  BringWindowToFront(w);
2360 }
2361 
2367 static void StartWindowSizing(Window *w, bool to_left)
2368 {
2369  w->flags |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
2370  w->flags &= ~WF_CENTERED;
2371  _dragging_window = true;
2372 
2373  _drag_delta.x = _cursor.pos.x;
2374  _drag_delta.y = _cursor.pos.y;
2375 
2376  BringWindowToFront(w);
2378 }
2379 
2385 {
2386  Window *w;
2387  FOR_ALL_WINDOWS_FROM_BACK(w) {
2388  if (w->scrolling_scrollbar >= 0) {
2389  /* Abort if no button is clicked any more. */
2390  if (!_left_button_down) {
2391  w->scrolling_scrollbar = -1;
2392  w->SetDirty();
2393  return ES_HANDLED;
2394  }
2395 
2396  int i;
2398  bool rtl = false;
2399 
2400  if (sb->type == NWID_HSCROLLBAR) {
2401  i = _cursor.pos.x - _cursorpos_drag_start.x;
2402  rtl = _current_text_dir == TD_RTL;
2403  } else {
2404  i = _cursor.pos.y - _cursorpos_drag_start.y;
2405  }
2406 
2407  if (sb->disp_flags & ND_SCROLLBAR_BTN) {
2408  if (_scroller_click_timeout == 1) {
2409  _scroller_click_timeout = 3;
2410  sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
2411  w->SetDirty();
2412  }
2413  return ES_HANDLED;
2414  }
2415 
2416  /* Find the item we want to move to and make sure it's inside bounds. */
2417  int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
2418  if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
2419  if (pos != sb->GetPosition()) {
2420  sb->SetPosition(pos);
2421  w->SetDirty();
2422  }
2423  return ES_HANDLED;
2424  }
2425  }
2426 
2427  return ES_NOT_HANDLED;
2428 }
2429 
2435 {
2436  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2437 
2438  if (!_scrolling_viewport) return ES_NOT_HANDLED;
2439 
2440  /* When we don't have a last scroll window we are starting to scroll.
2441  * When the last scroll window and this are not the same we went
2442  * outside of the window and should not left-mouse scroll anymore. */
2443  if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2444 
2445  if (_last_scroll_window == NULL || !(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down))) {
2446  _cursor.fix_at = false;
2447  _scrolling_viewport = false;
2448  _last_scroll_window = NULL;
2449  return ES_NOT_HANDLED;
2450  }
2451 
2452  if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
2453  /* If the main window is following a vehicle, then first let go of it! */
2454  const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2455  ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2456  return ES_NOT_HANDLED;
2457  }
2458 
2459  Point delta;
2461  delta.x = -_cursor.delta.x;
2462  delta.y = -_cursor.delta.y;
2463  } else {
2464  delta.x = _cursor.delta.x;
2465  delta.y = _cursor.delta.y;
2466  }
2467 
2468  if (scrollwheel_scrolling) {
2469  /* We are using scrollwheels for scrolling */
2470  delta.x = _cursor.h_wheel;
2471  delta.y = _cursor.v_wheel;
2472  _cursor.v_wheel = 0;
2473  _cursor.h_wheel = 0;
2474  }
2475 
2476  /* Create a scroll-event and send it to the window */
2477  if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2478 
2479  _cursor.delta.x = 0;
2480  _cursor.delta.y = 0;
2481  return ES_HANDLED;
2482 }
2483 
2495 {
2496  bool bring_to_front = false;
2497 
2498  if (w->window_class == WC_MAIN_WINDOW ||
2499  IsVitalWindow(w) ||
2500  w->window_class == WC_TOOLTIPS ||
2502  return true;
2503  }
2504 
2505  /* Use unshaded window size rather than current size for shaded windows. */
2506  int w_width = w->width;
2507  int w_height = w->height;
2508  if (w->IsShaded()) {
2509  w_width = w->unshaded_size.width;
2510  w_height = w->unshaded_size.height;
2511  }
2512 
2513  Window *u;
2515  /* A modal child will prevent the activation of the parent window */
2516  if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) {
2517  u->SetWhiteBorder();
2518  u->SetDirty();
2519  return false;
2520  }
2521 
2522  if (u->window_class == WC_MAIN_WINDOW ||
2523  IsVitalWindow(u) ||
2524  u->window_class == WC_TOOLTIPS ||
2526  continue;
2527  }
2528 
2529  /* Window sizes don't interfere, leave z-order alone */
2530  if (w->left + w_width <= u->left ||
2531  u->left + u->width <= w->left ||
2532  w->top + w_height <= u->top ||
2533  u->top + u->height <= w->top) {
2534  continue;
2535  }
2536 
2537  bring_to_front = true;
2538  }
2539 
2540  if (bring_to_front) BringWindowToFront(w);
2541  return true;
2542 }
2543 
2552 EventState Window::HandleEditBoxKey(int wid, WChar key, uint16 keycode)
2553 {
2554  QueryString *query = this->GetQueryString(wid);
2555  if (query == NULL) return ES_NOT_HANDLED;
2556 
2557  int action = QueryString::ACTION_NOTHING;
2558 
2559  switch (query->text.HandleKeyPress(key, keycode)) {
2560  case HKPR_EDITING:
2561  this->SetWidgetDirty(wid);
2562  this->OnEditboxChanged(wid);
2563  break;
2564 
2565  case HKPR_CURSOR:
2566  this->SetWidgetDirty(wid);
2567  /* For the OSK also invalidate the parent window */
2568  if (this->window_class == WC_OSK) this->InvalidateData();
2569  break;
2570 
2571  case HKPR_CONFIRM:
2572  if (this->window_class == WC_OSK) {
2573  this->OnClick(Point(), WID_OSK_OK, 1);
2574  } else if (query->ok_button >= 0) {
2575  this->OnClick(Point(), query->ok_button, 1);
2576  } else {
2577  action = query->ok_button;
2578  }
2579  break;
2580 
2581  case HKPR_CANCEL:
2582  if (this->window_class == WC_OSK) {
2583  this->OnClick(Point(), WID_OSK_CANCEL, 1);
2584  } else if (query->cancel_button >= 0) {
2585  this->OnClick(Point(), query->cancel_button, 1);
2586  } else {
2587  action = query->cancel_button;
2588  }
2589  break;
2590 
2591  case HKPR_NOT_HANDLED:
2592  return ES_NOT_HANDLED;
2593 
2594  default: break;
2595  }
2596 
2597  switch (action) {
2599  this->UnfocusFocusedWidget();
2600  break;
2601 
2603  if (query->text.bytes <= 1) {
2604  /* If already empty, unfocus instead */
2605  this->UnfocusFocusedWidget();
2606  } else {
2607  query->text.DeleteAll();
2608  this->SetWidgetDirty(wid);
2609  this->OnEditboxChanged(wid);
2610  }
2611  break;
2612 
2613  default:
2614  break;
2615  }
2616 
2617  return ES_HANDLED;
2618 }
2619 
2625 void HandleKeypress(uint keycode, WChar key)
2626 {
2627  /* World generation is multithreaded and messes with companies.
2628  * But there is no company related window open anyway, so _current_company is not used. */
2629  assert(HasModalProgress() || IsLocalCompany());
2630 
2631  /*
2632  * The Unicode standard defines an area called the private use area. Code points in this
2633  * area are reserved for private use and thus not portable between systems. For instance,
2634  * Apple defines code points for the arrow keys in this area, but these are only printable
2635  * on a system running OS X. We don't want these keys to show up in text fields and such,
2636  * and thus we have to clear the unicode character when we encounter such a key.
2637  */
2638  if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2639 
2640  /*
2641  * If both key and keycode is zero, we don't bother to process the event.
2642  */
2643  if (key == 0 && keycode == 0) return;
2644 
2645  /* Check if the focused window has a focused editbox */
2646  if (EditBoxInGlobalFocus()) {
2647  /* All input will in this case go to the focused editbox */
2648  if (_focused_window->window_class == WC_CONSOLE) {
2649  if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2650  } else {
2651  if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return;
2652  }
2653  }
2654 
2655  /* Call the event, start with the uppermost window, but ignore the toolbar. */
2656  Window *w;
2657  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2658  if (w->window_class == WC_MAIN_TOOLBAR) continue;
2659  if (w->window_desc->hotkeys != NULL) {
2660  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2661  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2662  }
2663  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2664  }
2665 
2667  /* When there is no toolbar w is null, check for that */
2668  if (w != NULL) {
2669  if (w->window_desc->hotkeys != NULL) {
2670  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2671  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2672  }
2673  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2674  }
2675 
2676  HandleGlobalHotkeys(key, keycode);
2677 }
2678 
2683 {
2684  /* Call the event, start with the uppermost window. */
2685  Window *w;
2686  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2687  if (w->OnCTRLStateChange() == ES_HANDLED) return;
2688  }
2689 }
2690 
2696 /* virtual */ void Window::InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2697 {
2698  QueryString *query = this->GetQueryString(wid);
2699  if (query == NULL) return;
2700 
2701  if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2702  this->SetWidgetDirty(wid);
2703  this->OnEditboxChanged(wid);
2704  }
2705 }
2706 
2713 void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2714 {
2715  if (!EditBoxInGlobalFocus()) return;
2716 
2717  _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end);
2718 }
2719 
2727 
2732 static void HandleAutoscroll()
2733 {
2734  if (_game_mode == GM_MENU || HasModalProgress()) return;
2736  if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2737 
2738  int x = _cursor.pos.x;
2739  int y = _cursor.pos.y;
2740  Window *w = FindWindowFromPt(x, y);
2741  if (w == NULL || w->flags & WF_DISABLE_VP_SCROLL) return;
2743 
2744  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2745  if (vp == NULL) return;
2746 
2747  x -= vp->left;
2748  y -= vp->top;
2749 
2750  /* here allows scrolling in both x and y axis */
2751 #define scrollspeed 3
2752  if (x - 15 < 0) {
2753  w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
2754  } else if (15 - (vp->width - x) > 0) {
2755  w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
2756  }
2757  if (y - 15 < 0) {
2758  w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
2759  } else if (15 - (vp->height - y) > 0) {
2760  w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
2761  }
2762 #undef scrollspeed
2763 }
2764 
2766  MC_NONE = 0,
2767  MC_LEFT,
2768  MC_RIGHT,
2769  MC_DOUBLE_LEFT,
2770  MC_HOVER,
2771 
2775 };
2777 
2778 static void ScrollMainViewport(int x, int y)
2779 {
2780  if (_game_mode != GM_MENU) {
2782  assert(w);
2783 
2786  }
2787 }
2788 
2798 static const int8 scrollamt[16][2] = {
2799  { 0, 0},
2800  {-2, 0},
2801  { 0, -2},
2802  {-2, -1},
2803  { 2, 0},
2804  { 0, 0},
2805  { 2, -1},
2806  { 0, -2},
2807  { 0, 2},
2808  {-2, 1},
2809  { 0, 0},
2810  {-2, 0},
2811  { 2, 1},
2812  { 0, 2},
2813  { 2, 0},
2814  { 0, 0},
2815 };
2816 
2817 static void HandleKeyScrolling()
2818 {
2819  /*
2820  * Check that any of the dirkeys is pressed and that the focused window
2821  * doesn't have an edit-box as focused widget.
2822  */
2823  if (_dirkeys && !EditBoxInGlobalFocus()) {
2824  int factor = _shift_pressed ? 50 : 10;
2825  ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2826  }
2827 }
2828 
2829 static void MouseLoop(MouseClick click, int mousewheel)
2830 {
2831  /* World generation is multithreaded and messes with companies.
2832  * But there is no company related window open anyway, so _current_company is not used. */
2833  assert(HasModalProgress() || IsLocalCompany());
2834 
2835  HandlePlacePresize();
2837 
2838  if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2839  if (HandleMouseDragDrop() == ES_HANDLED) return;
2840  if (HandleWindowDragging() == ES_HANDLED) return;
2841  if (HandleScrollbarScrolling() == ES_HANDLED) return;
2842  if (HandleViewportScroll() == ES_HANDLED) return;
2843 
2844  HandleMouseOver();
2845 
2846  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2847  if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2848 
2849  int x = _cursor.pos.x;
2850  int y = _cursor.pos.y;
2851  Window *w = FindWindowFromPt(x, y);
2852  if (w == NULL) return;
2853 
2854  if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2855  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2856 
2857  /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2858  if (vp != NULL && (_game_mode == GM_MENU || HasModalProgress())) return;
2859 
2860  if (mousewheel != 0) {
2861  /* Send mousewheel event to window */
2862  w->OnMouseWheel(mousewheel);
2863 
2864  /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2865  if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2866  }
2867 
2868  if (vp != NULL) {
2869  if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button
2870  switch (click) {
2871  case MC_DOUBLE_LEFT:
2872  case MC_LEFT:
2873  if (HandleViewportClicked(vp, x, y)) return;
2874  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2876  _scrolling_viewport = true;
2877  _cursor.fix_at = false;
2878  return;
2879  }
2880  break;
2881 
2882  case MC_RIGHT:
2883  if (!(w->flags & WF_DISABLE_VP_SCROLL)) {
2884  _scrolling_viewport = true;
2885  _cursor.fix_at = true;
2886 
2887  /* clear 2D scrolling caches before we start a 2D scroll */
2888  _cursor.h_wheel = 0;
2889  _cursor.v_wheel = 0;
2890  return;
2891  }
2892  break;
2893 
2894  default:
2895  break;
2896  }
2897  }
2898 
2899  if (vp == NULL || (w->flags & WF_DISABLE_VP_SCROLL)) {
2900  switch (click) {
2901  case MC_LEFT:
2902  case MC_DOUBLE_LEFT:
2903  DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2904  break;
2905 
2906  default:
2907  if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
2908  /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2909  * Simulate a right button click so we can get started. */
2910  FALLTHROUGH;
2911 
2912  case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
2913 
2914  case MC_HOVER: DispatchHoverEvent(w, x - w->left, y - w->top); break;
2915  }
2916  }
2917 }
2918 
2923 {
2924  /* World generation is multithreaded and messes with companies.
2925  * But there is no company related window open anyway, so _current_company is not used. */
2926  assert(HasModalProgress() || IsLocalCompany());
2927 
2928  static int double_click_time = 0;
2929  static Point double_click_pos = {0, 0};
2930 
2931  /* Mouse event? */
2932  MouseClick click = MC_NONE;
2934  click = MC_LEFT;
2935  if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
2936  double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2937  double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2938  click = MC_DOUBLE_LEFT;
2939  }
2940  double_click_time = _realtime_tick;
2941  double_click_pos = _cursor.pos;
2942  _left_button_clicked = true;
2944  } else if (_right_button_clicked) {
2945  _right_button_clicked = false;
2946  click = MC_RIGHT;
2948  }
2949 
2950  int mousewheel = 0;
2951  if (_cursor.wheel) {
2952  mousewheel = _cursor.wheel;
2953  _cursor.wheel = 0;
2955  }
2956 
2957  static uint32 hover_time = 0;
2958  static Point hover_pos = {0, 0};
2959 
2961  if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
2962  hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
2963  hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
2964  hover_pos = _cursor.pos;
2965  hover_time = _realtime_tick;
2966  _mouse_hovering = false;
2967  } else {
2968  if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
2969  click = MC_HOVER;
2971  _mouse_hovering = true;
2972  }
2973  }
2974  }
2975 
2976  /* Handle sprite picker before any GUI interaction */
2978  /* Next realtime tick? Then redraw has finished */
2979  _newgrf_debug_sprite_picker.mode = SPM_NONE;
2981  }
2982 
2983  if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
2984  /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
2986  _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
2989  _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
2991  } else {
2992  MouseLoop(click, mousewheel);
2993  }
2994 
2995  /* We have moved the mouse the required distance,
2996  * no need to move it at any later time. */
2997  _cursor.delta.x = 0;
2998  _cursor.delta.y = 0;
2999 }
3000 
3004 static void CheckSoftLimit()
3005 {
3006  if (_settings_client.gui.window_soft_limit == 0) return;
3007 
3008  for (;;) {
3009  uint deletable_count = 0;
3010  Window *w, *last_deletable = NULL;
3011  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3012  if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags & WF_STICKY)) continue;
3013 
3014  last_deletable = w;
3015  deletable_count++;
3016  }
3017 
3018  /* We've not reached the soft limit yet. */
3019  if (deletable_count <= _settings_client.gui.window_soft_limit) break;
3020 
3021  assert(last_deletable != NULL);
3022  delete last_deletable;
3023  }
3024 }
3025 
3030 {
3031  /* World generation is multithreaded and messes with companies.
3032  * But there is no company related window open anyway, so _current_company is not used. */
3033  assert(HasModalProgress() || IsLocalCompany());
3034 
3035  CheckSoftLimit();
3036  HandleKeyScrolling();
3037 
3038  /* Do the actual free of the deleted windows. */
3039  for (Window *v = _z_front_window; v != NULL; /* nothing */) {
3040  Window *w = v;
3041  v = v->z_back;
3042 
3043  if (w->window_class != WC_INVALID) continue;
3044 
3046  free(w);
3047  }
3048 
3049  if (_scroller_click_timeout != 0) _scroller_click_timeout--;
3050  DecreaseWindowCounters();
3051 
3052  if (_input_events_this_tick != 0) {
3053  /* The input loop is called only once per GameLoop() - so we can clear the counter here */
3055  /* there were some inputs this tick, don't scroll ??? */
3056  return;
3057  }
3058 
3059  /* HandleMouseEvents was already called for this tick */
3061  HandleAutoscroll();
3062 }
3063 
3068 {
3069  Window *w;
3070 
3071  static int highlight_timer = 1;
3072  if (--highlight_timer == 0) {
3073  highlight_timer = 15;
3075  }
3076 
3077  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3080  }
3081 
3082  /* Skip the actual drawing on dedicated servers without screen.
3083  * But still empty the invalidation queues above. */
3084  if (_network_dedicated) return;
3085 
3086  static int we4_timer = 0;
3087  int t = we4_timer + 1;
3088 
3089  if (t >= 100) {
3090  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3091  w->OnHundredthTick();
3092  }
3093  t = 0;
3094  }
3095  we4_timer = t;
3096 
3097  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3098  if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) {
3100  w->SetDirty();
3101  }
3102  }
3103 
3104  DrawDirtyBlocks();
3105 
3106  FOR_ALL_WINDOWS_FROM_BACK(w) {
3107  /* Update viewport only if window is not shaded. */
3108  if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
3109  }
3111  /* Redraw mouse cursor in case it was hidden */
3112  DrawMouseCursor();
3113 }
3114 
3121 {
3122  const Window *w;
3123  FOR_ALL_WINDOWS_FROM_BACK(w) {
3124  if (w->window_class == cls && w->window_number == number) w->SetDirty();
3125  }
3126 }
3127 
3134 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
3135 {
3136  const Window *w;
3137  FOR_ALL_WINDOWS_FROM_BACK(w) {
3138  if (w->window_class == cls && w->window_number == number) {
3139  w->SetWidgetDirty(widget_index);
3140  }
3141  }
3142 }
3143 
3149 {
3150  Window *w;
3151  FOR_ALL_WINDOWS_FROM_BACK(w) {
3152  if (w->window_class == cls) w->SetDirty();
3153  }
3154 }
3155 
3161 void Window::InvalidateData(int data, bool gui_scope)
3162 {
3163  this->SetDirty();
3164  if (!gui_scope) {
3165  /* Schedule GUI-scope invalidation for next redraw. */
3166  *this->scheduled_invalidation_data.Append() = data;
3167  }
3168  this->OnInvalidateData(data, gui_scope);
3169 }
3170 
3175 {
3176  for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
3177  this->OnInvalidateData(*data, true);
3178  }
3180 }
3181 
3186 {
3187  if ((this->flags & WF_HIGHLIGHTED) == 0) return;
3188 
3189  for (uint i = 0; i < this->nested_array_size; i++) {
3190  if (this->IsWidgetHighlighted(i)) this->SetWidgetDirty(i);
3191  }
3192 }
3193 
3220 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3221 {
3222  Window *w;
3223  FOR_ALL_WINDOWS_FROM_BACK(w) {
3224  if (w->window_class == cls && w->window_number == number) {
3225  w->InvalidateData(data, gui_scope);
3226  }
3227  }
3228 }
3229 
3238 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3239 {
3240  Window *w;
3241 
3242  FOR_ALL_WINDOWS_FROM_BACK(w) {
3243  if (w->window_class == cls) {
3244  w->InvalidateData(data, gui_scope);
3245  }
3246  }
3247 }
3248 
3253 {
3254  Window *w;
3255  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3256  w->OnTick();
3257  }
3258 }
3259 
3267 {
3268  Window *w;
3269 
3270 restart_search:
3271  /* When we find the window to delete, we need to restart the search
3272  * as deleting this window could cascade in deleting (many) others
3273  * anywhere in the z-array */
3274  FOR_ALL_WINDOWS_FROM_BACK(w) {
3275  if (w->window_class != WC_MAIN_WINDOW &&
3276  w->window_class != WC_SELECT_GAME &&
3277  w->window_class != WC_MAIN_TOOLBAR &&
3278  w->window_class != WC_STATUS_BAR &&
3279  w->window_class != WC_TOOLTIPS &&
3280  (w->flags & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
3281 
3282  delete w;
3283  goto restart_search;
3284  }
3285  }
3286 }
3287 
3296 {
3297  Window *w;
3298 
3299  /* Delete every window except for stickied ones, then sticky ones as well */
3301 
3302 restart_search:
3303  /* When we find the window to delete, we need to restart the search
3304  * as deleting this window could cascade in deleting (many) others
3305  * anywhere in the z-array */
3306  FOR_ALL_WINDOWS_FROM_BACK(w) {
3307  if (w->flags & WF_STICKY) {
3308  delete w;
3309  goto restart_search;
3310  }
3311  }
3312 }
3313 
3319 {
3320  Window *w;
3321 
3322 restart_search:
3323  /* When we find the window to delete, we need to restart the search
3324  * as deleting this window could cascade in deleting (many) others
3325  * anywhere in the z-array */
3326  FOR_ALL_WINDOWS_FROM_BACK(w) {
3327  if (w->window_desc->flags & WDF_CONSTRUCTION) {
3328  delete w;
3329  goto restart_search;
3330  }
3331  }
3332 
3333  FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
3334 }
3335 
3338 {
3341 }
3342 
3345 {
3346  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3347  NWidgetScrollbar::InvalidateDimensionCache();
3348 
3349  extern void InitDepotWindowBlockSizes();
3351 
3352  Window *w;
3353  FOR_ALL_WINDOWS_FROM_BACK(w) {
3354  w->ReInit();
3355  }
3356 #ifdef ENABLE_NETWORK
3357  void NetworkReInitChatBoxSize();
3359 #endif
3360 
3361  /* Make sure essential parts of all windows are visible */
3364 }
3365 
3373 static int PositionWindow(Window *w, WindowClass clss, int setting)
3374 {
3375  if (w == NULL || w->window_class != clss) {
3376  w = FindWindowById(clss, 0);
3377  }
3378  if (w == NULL) return 0;
3379 
3380  int old_left = w->left;
3381  switch (setting) {
3382  case 1: w->left = (_screen.width - w->width) / 2; break;
3383  case 2: w->left = _screen.width - w->width; break;
3384  default: w->left = 0; break;
3385  }
3386  if (w->viewport != NULL) w->viewport->left += w->left - old_left;
3387  SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3388  return w->left;
3389 }
3390 
3397 {
3398  DEBUG(misc, 5, "Repositioning Main Toolbar...");
3400 }
3401 
3408 {
3409  DEBUG(misc, 5, "Repositioning statusbar...");
3411 }
3412 
3419 {
3420  DEBUG(misc, 5, "Repositioning news message...");
3422 }
3423 
3430 {
3431  DEBUG(misc, 5, "Repositioning network chat window...");
3433 }
3434 
3435 
3441 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3442 {
3443  Window *w;
3444  FOR_ALL_WINDOWS_FROM_BACK(w) {
3445  if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
3446  w->viewport->follow_vehicle = to_index;
3447  w->SetDirty();
3448  }
3449  }
3450 }
3451 
3452 
3458 void RelocateAllWindows(int neww, int newh)
3459 {
3460  Window *w;
3461 
3462  FOR_ALL_WINDOWS_FROM_BACK(w) {
3463  int left, top;
3464  /* XXX - this probably needs something more sane. For example specifying
3465  * in a 'backup'-desc that the window should always be centered. */
3466  switch (w->window_class) {
3467  case WC_MAIN_WINDOW:
3468  case WC_BOOTSTRAP:
3469  ResizeWindow(w, neww, newh);
3470  continue;
3471 
3472  case WC_MAIN_TOOLBAR:
3473  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3474 
3475  top = w->top;
3476  left = PositionMainToolbar(w); // changes toolbar orientation
3477  break;
3478 
3479  case WC_NEWS_WINDOW:
3480  top = newh - w->height;
3481  left = PositionNewsMessage(w);
3482  break;
3483 
3484  case WC_STATUS_BAR:
3485  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3486 
3487  top = newh - w->height;
3488  left = PositionStatusbar(w);
3489  break;
3490 
3491  case WC_SEND_NETWORK_MSG:
3492  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3493 
3494  top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3495  left = PositionNetworkChatWindow(w);
3496  break;
3497 
3498  case WC_CONSOLE:
3499  IConsoleResize(w);
3500  continue;
3501 
3502  default: {
3503  if (w->flags & WF_CENTERED) {
3504  top = (newh - w->height) >> 1;
3505  left = (neww - w->width) >> 1;
3506  break;
3507  }
3508 
3509  left = w->left;
3510  if (left + (w->width >> 1) >= neww) left = neww - w->width;
3511  if (left < 0) left = 0;
3512 
3513  top = w->top;
3514  if (top + (w->height >> 1) >= newh) top = newh - w->height;
3515  break;
3516  }
3517  }
3518 
3519  EnsureVisibleCaption(w, left, top);
3520  }
3521 }
3522 
3529 {
3530  this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
3532 }
EventState
State of handling an event.
Definition: window_type.h:701
Window * _z_back_window
List of windows opened at the screen sorted from the back.
Definition: window.cpp:57
Functions related to OTTD&#39;s strings.
static void CheckSoftLimit()
Check the soft limit of deletable (non vital, non sticky) windows.
Definition: window.cpp:3004
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:896
Functions/types related to NewGRF debugging.
void SetShaded(bool make_shaded)
Set the shaded state of the window to make_shaded.
Definition: window.cpp:1006
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:778
static Window * FindChildWindow(const Window *w, WindowClass wc)
Find the Window whose parent pointer points to this window.
Definition: window.cpp:1033
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:153
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:1599
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
Definition: window.cpp:1854
ViewportAutoscrolling
Values for _settings_client.gui.auto_scrolling.
Definition: window.cpp:43
Base of all video drivers.
static Window * _mouseover_last_w
Window of the last #MOUSEOVER event.
Definition: window.cpp:51
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:590
const QueryString * GetQueryString(uint widnum) const
Return the querystring associated to a editbox.
Definition: window.cpp:327
virtual void ApplyDefaults()
Read default values from WindowDesc configuration an apply them to the window.
Definition: window.cpp:180
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1843
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:48
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:90
Window * _z_front_window
List of windows opened at the screen sorted from the front.
Definition: window.cpp:55
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:3120
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:877
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:432
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:2713
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:2039
SpecialMouseMode _special_mouse_mode
Mode of the mouse.
Definition: window.cpp:78
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:1398
uint8 toolbar_pos
position of toolbars, 0=left, 1=center, 2=right
Definition: settings_type.h:97
uint8 window_snap_radius
windows snap at each other if closer than this
Definition: settings_type.h:99
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:577
static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
Dispatch left mouse-button (possibly double) click in window.
Definition: window.cpp:632
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 DeleteConstructionWindows()
Delete all windows that are used for construction of vehicle etc.
Definition: window.cpp:3318
Dragging an object.
Definition: window_gui.h:898
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:86
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1105
The passed event is not handled.
Definition: window_type.h:703
void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets,...)
Sets the enabled/disabled status of a list of widgets.
Definition: window.cpp:516
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:3396
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:2682
void InitWindowSystem()
(re)initialize the windowing system
Definition: window.cpp:1869
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:742
Time between 2 left clicks before it becoming a double click, in ms.
Definition: window.cpp:2773
virtual ~PickerWindowBase()
Destructor of the base class PickerWindowBase Main utility is to stop the base Window destructor from...
Definition: window.cpp:3528
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:2024
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.
virtual void OnTick()
Called once per (game) tick.
Definition: window_gui.h:681
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:348
virtual void OnEditboxChanged(int widget)
The text in an editbox has been edited.
Definition: window_gui.h:713
static EventState HandleViewportScroll()
Handle viewport scrolling with the mouse.
Definition: window.cpp:2434
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:2175
virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
Compute the initial position of the window.
Definition: window.cpp:1792
void UpdateTileSelection()
Updates tile highlighting for all cases.
Definition: viewport.cpp:2355
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:2349
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3067
static bool _dragging_window
A window is being dragged or resized.
Definition: window.cpp:2169
static void StartWindowSizing(Window *w, bool to_left)
Start resizing a window.
Definition: window.cpp:2367
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:970
Simple vector template class.
Scroll all viewports at their edges.
Definition: window.cpp:47
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:684
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:57
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
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:3295
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2625
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:900
Above v is a safe position.
Definition: window.cpp:2025
void InputLoop()
Regular call from the global game loop.
Definition: window.cpp:3029
virtual void OnTimeout()
Called when this window&#39;s timeout has been reached.
Definition: window_gui.h:691
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:1804
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
Window * GetCallbackWnd()
Get the window that started the current highlighting.
Definition: viewport.cpp:2341
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:375
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Definition: window.cpp:2163
static SmallVector< WindowDesc *, 16 > * _window_descs
List of all WindowDescs.
Definition: window.cpp:84
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:2732
void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
Switches viewports following vehicles, which get autoreplaced.
Definition: window.cpp:3441
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:789
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:1820
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:3238
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1833
int PositionStatusbar(Window *w)
(Re)position statusbar window at the screen.
Definition: window.cpp:3407
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:131
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:3107
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:3337
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:937
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:3418
virtual void OnFocusLost()
Called when window looses focus.
Definition: window.cpp:504
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:1639
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:50
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:121
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:553
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:1708
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:3458
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:230
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
void CallWindowTickEvent()
Dispatch WE_TICK event over all windows.
Definition: window.cpp:3252
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:2152
A number of safeguards to prevent using unsafe methods.
static void HandleMouseOver()
Report position of the mouse to the underlying window.
Definition: window.cpp:1998
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:2798
bool left_mouse_btn_scrolling
left mouse button scroll
void DeleteCompanyWindows(CompanyID id)
Delete all windows of a company.
Definition: window.cpp:1172
uint32 click_time
Realtime tick when clicked to detect next frame.
Definition: newgrf_debug.h:31
static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
Dispatch the mousewheel-action to the window.
Definition: window.cpp:816
static void SaveToConfig()
Save all WindowDesc settings to _windows_file.
Definition: window.cpp:162
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:1047
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
Definition: window.cpp:3429
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:2552
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:2696
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:307
bool _mouse_hovering
The mouse is hovering over the same point.
Definition: window.cpp:76
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:199
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:2494
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:2077
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:2026
void InitializeData(WindowNumber window_number)
Initializes the data (except the position and initial size) of a new Window.
Definition: window.cpp:1438
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
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as &#39;dirty&#39;.
Definition: gfx.cpp:1307
static void DispatchRightClickEvent(Window *w, int x, int y)
Dispatch right mouse-button click in window.
Definition: window.cpp:764
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:46
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:2922
bool pref_sticky
Preferred stickyness.
Definition: window_gui.h:185
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1150
Window is being resized.
Definition: window_gui.h:239
static EventState HandleScrollbarScrolling()
handle scrollbar scrolling with the mouse.
Definition: window.cpp:2384
ViewPort * IsPtInWindowViewport(const Window *w, int x, int y)
Is a xy position inside the viewport of the window?
Definition: viewport.cpp:385
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:1890
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:2774
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
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:2021
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:959
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:39
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:2765
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:45
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
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1137
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:3266
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:1059
void ReInitAllWindows()
Re-initialize all windows.
Definition: window.cpp:3344
bool EditBoxInGlobalFocus()
Check if an edit box is in global focus.
Definition: window.cpp:455
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:2772
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:3134
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:361
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:1733
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
Definition: window.cpp:1480
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:263
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:61
void ProcessScheduledInvalidations()
Process all scheduled invalidations.
Definition: window.cpp:3174
bool _window_highlight_colour
If false, highlight is white, otherwise the by the widget defined colour.
Definition: window.cpp:60
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:419
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
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:794
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:2726
static void LoadFromConfig()
Load all WindowDesc settings from _windows_file.
Definition: window.cpp:139
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:535
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:404
void Reset()
Reset tile highlighting.
Definition: viewport.cpp:2320
Trains list; Window numbers:
Definition: window_type.h:303
void NetworkDrawChatMessage()
Draw the chat message-box.
Functions related to zooming.
void UnfocusFocusedWidget()
Makes no widget on this window have focus.
Definition: window.cpp:468
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:484
bool in_window
mouse inside this window, determines drawing logic
Definition: gfx_type.h:143
Do not autoscroll when mouse is at edge of viewport.
Definition: window.cpp:44
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:1121
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
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.
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:1562
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:84
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:1422
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
Window does not do autoscroll,.
Definition: window_gui.h:241
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:1198
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:615
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:300
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.
Definition: window.cpp:1498
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:210
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:1974
virtual void OnResize()
Called after the window got resized.
Definition: window_gui.h:698
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:1271
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:695
static int PositionWindow(Window *w, WindowClass clss, int setting)
(Re)position a window at the screen.
Definition: window.cpp:3373
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:3211
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:702
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:75
Functions related to tile highlights.
bool reverse_scroll
right-Click-Scrolling scrolls in the opposite direction
Definition: settings_type.h:89
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:1910
static Window * _last_scroll_window
Window of the last scroll event.
Definition: window.cpp:52
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
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
Definition: settings_type.h:98
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:280
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:3148
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:388
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:827
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2116
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:1234
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:3161
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:87
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:1349
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:3220
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:728
void ProcessHighlightedInvalidations()
Process all invalidation of highlighted widgets.
Definition: window.cpp:3185
void UpdateViewportPosition(Window *w)
Update the viewport position being displayed.
Definition: viewport.cpp:1787
static bool MayBeShown(const Window *w)
Returns whether a window may be shown or not.
Definition: window.cpp:849
void IConsoleClose()
Close the in-game console.
#define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)
Iterate over all windows.
Definition: window_gui.h:881
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