OpenTTD
window.cpp
Go to the documentation of this file.
1 /* $Id: window.cpp 27825 2017-03-24 18:55:16Z peter1138 $ */
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) {
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);
496  }
497  this->nested_focus = this->GetWidget<NWidgetCore>(widget_index);
498  return true;
499 }
500 
505 {
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  {
778  delete w;
779  }
780  else if (_settings_client.gui.hover_delay_ms == 0 && wid->tool_tip != 0)
781  {
782  GuiShowTooltips(w, wid->tool_tip, 0, NULL, TCC_RIGHT_CLICK);
783  }
784 }
785 
792 static void DispatchHoverEvent(Window *w, int x, int y)
793 {
794  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
795 
796  /* No widget to handle */
797  if (wid == NULL) return;
798 
799  /* Show the tooltip if there is any */
800  if (wid->tool_tip != 0) {
801  GuiShowTooltips(w, wid->tool_tip);
802  return;
803  }
804 
805  /* Widget has no index, so the window is not interested in it. */
806  if (wid->index < 0) return;
807 
808  Point pt = { x, y };
809  w->OnHover(pt, wid->index);
810 }
811 
819 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
820 {
821  if (nwid == NULL) return;
822 
823  /* Using wheel on caption/shade-box shades or unshades the window. */
824  if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
825  w->SetShaded(wheel < 0);
826  return;
827  }
828 
829  /* Wheeling a vertical scrollbar. */
830  if (nwid->type == NWID_VSCROLLBAR) {
831  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
832  if (sb->GetCount() > sb->GetCapacity()) {
833  sb->UpdatePosition(wheel);
834  w->SetDirty();
835  }
836  return;
837  }
838 
839  /* Scroll the widget attached to the scrollbar. */
840  Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : NULL);
841  if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
842  sb->UpdatePosition(wheel);
843  w->SetDirty();
844  }
845 }
846 
852 static bool MayBeShown(const Window *w)
853 {
854  /* If we're not modal, everything is okay. */
855  if (!HasModalProgress()) return true;
856 
857  switch (w->window_class) {
858  case WC_MAIN_WINDOW:
859  case WC_MODAL_PROGRESS:
861  return true;
862 
863  default:
864  return false;
865  }
866 }
867 
880 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
881 {
882  const Window *v;
884  if (MayBeShown(v) &&
885  right > v->left &&
886  bottom > v->top &&
887  left < v->left + v->width &&
888  top < v->top + v->height) {
889  /* v and rectangle intersect with each other */
890  int x;
891 
892  if (left < (x = v->left)) {
893  DrawOverlappedWindow(w, left, top, x, bottom);
894  DrawOverlappedWindow(w, x, top, right, bottom);
895  return;
896  }
897 
898  if (right > (x = v->left + v->width)) {
899  DrawOverlappedWindow(w, left, top, x, bottom);
900  DrawOverlappedWindow(w, x, top, right, bottom);
901  return;
902  }
903 
904  if (top < (x = v->top)) {
905  DrawOverlappedWindow(w, left, top, right, x);
906  DrawOverlappedWindow(w, left, x, right, bottom);
907  return;
908  }
909 
910  if (bottom > (x = v->top + v->height)) {
911  DrawOverlappedWindow(w, left, top, right, x);
912  DrawOverlappedWindow(w, left, x, right, bottom);
913  return;
914  }
915 
916  return;
917  }
918  }
919 
920  /* Setup blitter, and dispatch a repaint event to window *wz */
921  DrawPixelInfo *dp = _cur_dpi;
922  dp->width = right - left;
923  dp->height = bottom - top;
924  dp->left = left - w->left;
925  dp->top = top - w->top;
926  dp->pitch = _screen.pitch;
927  dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
928  dp->zoom = ZOOM_LVL_NORMAL;
929  w->OnPaint();
930 }
931 
940 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
941 {
942  Window *w;
943  DrawPixelInfo bk;
944  _cur_dpi = &bk;
945 
946  FOR_ALL_WINDOWS_FROM_BACK(w) {
947  if (MayBeShown(w) &&
948  right > w->left &&
949  bottom > w->top &&
950  left < w->left + w->width &&
951  top < w->top + w->height) {
952  /* Window w intersects with the rectangle => needs repaint */
953  DrawOverlappedWindow(w, max(left, w->left), max(top, w->top), min(right, w->left + w->width), min(bottom, w->top + w->height));
954  }
955  }
956 }
957 
962 void Window::SetDirty() const
963 {
964  SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
965 }
966 
973 void Window::ReInit(int rx, int ry)
974 {
975  this->SetDirty(); // Mark whole current window as dirty.
976 
977  /* Save current size. */
978  int window_width = this->width;
979  int window_height = this->height;
980 
981  this->OnInit();
982  /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
983  this->nested_root->SetupSmallestSize(this, false);
984  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
985  this->width = this->nested_root->smallest_x;
986  this->height = this->nested_root->smallest_y;
987  this->resize.step_width = this->nested_root->resize_x;
988  this->resize.step_height = this->nested_root->resize_y;
989 
990  /* Resize as close to the original size + requested resize as possible. */
991  window_width = max(window_width + rx, this->width);
992  window_height = max(window_height + ry, this->height);
993  int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
994  int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
995  /* dx and dy has to go by step.. calculate it.
996  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
997  if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
998  if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
999 
1000  ResizeWindow(this, dx, dy);
1001  /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
1002 }
1003 
1009 void Window::SetShaded(bool make_shaded)
1010 {
1011  if (this->shade_select == NULL) return;
1012 
1013  int desired = make_shaded ? SZSP_HORIZONTAL : 0;
1014  if (this->shade_select->shown_plane != desired) {
1015  if (make_shaded) {
1016  if (this->nested_focus != NULL) this->UnfocusFocusedWidget();
1017  this->unshaded_size.width = this->width;
1018  this->unshaded_size.height = this->height;
1019  this->shade_select->SetDisplayedPlane(desired);
1020  this->ReInit(0, -this->height);
1021  } else {
1022  this->shade_select->SetDisplayedPlane(desired);
1023  int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
1024  int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
1025  this->ReInit(dx, dy);
1026  }
1027  }
1028 }
1029 
1037 {
1038  Window *v;
1039  FOR_ALL_WINDOWS_FROM_BACK(v) {
1040  if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
1041  }
1042 
1043  return NULL;
1044 }
1045 
1051 {
1052  Window *child = FindChildWindow(this, wc);
1053  while (child != NULL) {
1054  delete child;
1055  child = FindChildWindow(this, wc);
1056  }
1057 }
1058 
1063 {
1064  if (_thd.window_class == this->window_class &&
1065  _thd.window_number == this->window_number) {
1067  }
1068 
1069  /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
1070  if (_mouseover_last_w == this) _mouseover_last_w = NULL;
1071 
1072  /* We can't scroll the window when it's closed. */
1073  if (_last_scroll_window == this) _last_scroll_window = NULL;
1074 
1075  /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
1076  if (_focused_window == this) {
1077  this->OnFocusLost();
1078  _focused_window = NULL;
1079  }
1080 
1081  this->DeleteChildWindows();
1082 
1083  if (this->viewport != NULL) DeleteWindowViewport(this);
1084 
1085  this->SetDirty();
1086 
1087  free(this->nested_array); // Contents is released through deletion of #nested_root.
1088  delete this->nested_root;
1089 
1090  /*
1091  * Make fairly sure that this is written, and not "optimized" away.
1092  * The delete operator is overwritten to not delete it; the deletion
1093  * happens at a later moment in time after the window has been
1094  * removed from the list of windows to prevent issues with items
1095  * being removed during the iteration as not one but more windows
1096  * may be removed by a single call to ~Window by means of the
1097  * DeleteChildWindows function.
1098  */
1099  const_cast<volatile WindowClass &>(this->window_class) = WC_INVALID;
1100 }
1101 
1109 {
1110  Window *w;
1111  FOR_ALL_WINDOWS_FROM_BACK(w) {
1112  if (w->window_class == cls && w->window_number == number) return w;
1113  }
1114 
1115  return NULL;
1116 }
1117 
1125 {
1126  Window *w;
1127  FOR_ALL_WINDOWS_FROM_BACK(w) {
1128  if (w->window_class == cls) return w;
1129  }
1130 
1131  return NULL;
1132 }
1133 
1140 void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
1141 {
1142  Window *w = FindWindowById(cls, number);
1143  if (force || w == NULL ||
1144  (w->flags & WF_STICKY) == 0) {
1145  delete w;
1146  }
1147 }
1148 
1154 {
1155  Window *w;
1156 
1157 restart_search:
1158  /* When we find the window to delete, we need to restart the search
1159  * as deleting this window could cascade in deleting (many) others
1160  * anywhere in the z-array */
1161  FOR_ALL_WINDOWS_FROM_BACK(w) {
1162  if (w->window_class == cls) {
1163  delete w;
1164  goto restart_search;
1165  }
1166  }
1167 }
1168 
1176 {
1177  Window *w;
1178 
1179 restart_search:
1180  /* When we find the window to delete, we need to restart the search
1181  * as deleting this window could cascade in deleting (many) others
1182  * anywhere in the z-array */
1183  FOR_ALL_WINDOWS_FROM_BACK(w) {
1184  if (w->owner == id) {
1185  delete w;
1186  goto restart_search;
1187  }
1188  }
1189 
1190  /* Also delete the company specific windows that don't have a company-colour. */
1192 }
1193 
1201 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1202 {
1203  Window *w;
1204  FOR_ALL_WINDOWS_FROM_BACK(w) {
1205  if (w->owner != old_owner) continue;
1206 
1207  switch (w->window_class) {
1208  case WC_COMPANY_COLOUR:
1209  case WC_FINANCES:
1210  case WC_STATION_LIST:
1211  case WC_TRAINS_LIST:
1212  case WC_ROADVEH_LIST:
1213  case WC_SHIPS_LIST:
1214  case WC_AIRCRAFT_LIST:
1215  case WC_BUY_COMPANY:
1216  case WC_COMPANY:
1218  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().
1219  continue;
1220 
1221  default:
1222  w->owner = new_owner;
1223  break;
1224  }
1225  }
1226 }
1227 
1228 static void BringWindowToFront(Window *w);
1229 
1238 {
1239  Window *w = FindWindowById(cls, number);
1240 
1241  if (w != NULL) {
1242  if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1243 
1244  w->SetWhiteBorder();
1245  BringWindowToFront(w);
1246  w->SetDirty();
1247  }
1248 
1249  return w;
1250 }
1251 
1252 static inline bool IsVitalWindow(const Window *w)
1253 {
1254  switch (w->window_class) {
1255  case WC_MAIN_TOOLBAR:
1256  case WC_STATUS_BAR:
1257  case WC_NEWS_WINDOW:
1258  case WC_SEND_NETWORK_MSG:
1259  return true;
1260 
1261  default:
1262  return false;
1263  }
1264 }
1265 
1274 static uint GetWindowZPriority(const Window *w)
1275 {
1276  assert(w->window_class != WC_INVALID);
1277 
1278  uint z_priority = 0;
1279 
1280  switch (w->window_class) {
1281  case WC_ENDSCREEN:
1282  ++z_priority;
1283 
1284  case WC_HIGHSCORE:
1285  ++z_priority;
1286 
1287  case WC_TOOLTIPS:
1288  ++z_priority;
1289 
1290  case WC_DROPDOWN_MENU:
1291  ++z_priority;
1292 
1293  case WC_MAIN_TOOLBAR:
1294  case WC_STATUS_BAR:
1295  ++z_priority;
1296 
1297  case WC_OSK:
1298  ++z_priority;
1299 
1300  case WC_QUERY_STRING:
1301  case WC_SEND_NETWORK_MSG:
1302  ++z_priority;
1303 
1304  case WC_ERRMSG:
1306  case WC_MODAL_PROGRESS:
1308  case WC_SAVE_PRESET:
1309  ++z_priority;
1310 
1311  case WC_GENERATE_LANDSCAPE:
1312  case WC_SAVELOAD:
1313  case WC_GAME_OPTIONS:
1314  case WC_CUSTOM_CURRENCY:
1315  case WC_NETWORK_WINDOW:
1316  case WC_GRF_PARAMETERS:
1317  case WC_AI_LIST:
1318  case WC_AI_SETTINGS:
1319  case WC_TEXTFILE:
1320  ++z_priority;
1321 
1322  case WC_CONSOLE:
1323  ++z_priority;
1324 
1325  case WC_NEWS_WINDOW:
1326  ++z_priority;
1327 
1328  default:
1329  ++z_priority;
1330 
1331  case WC_MAIN_WINDOW:
1332  return z_priority;
1333  }
1334 }
1335 
1341 {
1342  assert(w->z_front == NULL && w->z_back == NULL);
1343 
1344  if (_z_front_window == NULL) {
1345  /* It's the only window. */
1346  _z_front_window = _z_back_window = w;
1347  w->z_front = w->z_back = NULL;
1348  } else {
1349  /* Search down the z-ordering for its location. */
1350  Window *v = _z_front_window;
1351  uint last_z_priority = UINT_MAX;
1352  while (v != NULL && (v->window_class == WC_INVALID || GetWindowZPriority(v) > GetWindowZPriority(w))) {
1353  if (v->window_class != WC_INVALID) {
1354  /* Sanity check z-ordering, while we're at it. */
1355  assert(last_z_priority >= GetWindowZPriority(v));
1356  last_z_priority = GetWindowZPriority(v);
1357  }
1358 
1359  v = v->z_back;
1360  }
1361 
1362  if (v == NULL) {
1363  /* It's the new back window. */
1364  w->z_front = _z_back_window;
1365  w->z_back = NULL;
1366  _z_back_window->z_back = w;
1367  _z_back_window = w;
1368  } else if (v == _z_front_window) {
1369  /* It's the new front window. */
1370  w->z_front = NULL;
1371  w->z_back = _z_front_window;
1372  _z_front_window->z_front = w;
1373  _z_front_window = w;
1374  } else {
1375  /* It's somewhere else in the z-ordering. */
1376  w->z_front = v->z_front;
1377  w->z_back = v;
1378  v->z_front->z_back = w;
1379  v->z_front = w;
1380  }
1381  }
1382 }
1383 
1384 
1390 {
1391  if (w->z_front == NULL) {
1392  assert(_z_front_window == w);
1393  _z_front_window = w->z_back;
1394  } else {
1395  w->z_front->z_back = w->z_back;
1396  }
1397 
1398  if (w->z_back == NULL) {
1399  assert(_z_back_window == w);
1400  _z_back_window = w->z_front;
1401  } else {
1402  w->z_back->z_front = w->z_front;
1403  }
1404 
1405  w->z_front = w->z_back = NULL;
1406 }
1407 
1414 {
1417 
1418  w->SetDirty();
1419 }
1420 
1430 {
1431  /* Set up window properties; some of them are needed to set up smallest size below */
1432  this->window_class = this->window_desc->cls;
1433  this->SetWhiteBorder();
1434  if (this->window_desc->default_pos == WDP_CENTER) this->flags |= WF_CENTERED;
1435  this->owner = INVALID_OWNER;
1436  this->nested_focus = NULL;
1437  this->window_number = window_number;
1438 
1439  this->OnInit();
1440  /* Initialize nested widget tree. */
1441  if (this->nested_array == NULL) {
1442  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1443  this->nested_root->SetupSmallestSize(this, true);
1444  } else {
1445  this->nested_root->SetupSmallestSize(this, false);
1446  }
1447  /* Initialize to smallest size. */
1448  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1449 
1450  /* Further set up window properties,
1451  * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1452  this->resize.step_width = this->nested_root->resize_x;
1453  this->resize.step_height = this->nested_root->resize_y;
1454 
1455  /* Give focus to the opened window unless a text box
1456  * of focused window has focus (so we don't interrupt typing). But if the new
1457  * window has a text box, then take focus anyway. */
1459 
1460  /* Insert the window into the correct location in the z-ordering. */
1461  AddWindowToZOrdering(this);
1462 }
1463 
1471 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1472 {
1473  this->left = x;
1474  this->top = y;
1475  this->width = sm_width;
1476  this->height = sm_height;
1477 }
1478 
1489 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
1490 {
1491  def_width = max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1492  def_height = max(def_height, this->height);
1493  /* Try to make windows smaller when our window is too small.
1494  * w->(width|height) is normally the same as min_(width|height),
1495  * but this way the GUIs can be made a little more dynamic;
1496  * one can use the same spec for multiple windows and those
1497  * can then determine the real minimum size of the window. */
1498  if (this->width != def_width || this->height != def_height) {
1499  /* Think about the overlapping toolbars when determining the minimum window size */
1500  int free_height = _screen.height;
1501  const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1502  if (wt != NULL) free_height -= wt->height;
1504  if (wt != NULL) free_height -= wt->height;
1505 
1506  int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
1507  int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
1508 
1509  /* X and Y has to go by step.. calculate it.
1510  * The cast to int is necessary else x/y are implicitly casted to
1511  * unsigned int, which won't work. */
1512  if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1513  if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1514 
1515  ResizeWindow(this, enlarge_x, enlarge_y);
1516  /* ResizeWindow() calls this->OnResize(). */
1517  } else {
1518  /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1519  this->OnResize();
1520  }
1521 
1522  int nx = this->left;
1523  int ny = this->top;
1524 
1525  if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1526 
1527  const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1528  ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
1529  nx = max(nx, 0);
1530 
1531  if (this->viewport != NULL) {
1532  this->viewport->left += nx - this->left;
1533  this->viewport->top += ny - this->top;
1534  }
1535  this->left = nx;
1536  this->top = ny;
1537 
1538  this->SetDirty();
1539 }
1540 
1552 static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos)
1553 {
1554  int right = width + left;
1555  int bottom = height + top;
1556 
1557  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1558  if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height) return false;
1559 
1560  /* Make sure it is not obscured by any window. */
1561  const Window *w;
1562  FOR_ALL_WINDOWS_FROM_BACK(w) {
1563  if (w->window_class == WC_MAIN_WINDOW) continue;
1564 
1565  if (right > w->left &&
1566  w->left + w->width > left &&
1567  bottom > w->top &&
1568  w->top + w->height > top) {
1569  return false;
1570  }
1571  }
1572 
1573  pos.x = left;
1574  pos.y = top;
1575  return true;
1576 }
1577 
1589 static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos)
1590 {
1591  /* Left part of the rectangle may be at most 1/4 off-screen,
1592  * right part of the rectangle may be at most 1/2 off-screen
1593  */
1594  if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1595  /* Bottom part of the rectangle may be at most 1/4 off-screen */
1596  if (top < 22 || top > _screen.height - (height >> 2)) return false;
1597 
1598  /* Make sure it is not obscured by any window. */
1599  const Window *w;
1600  FOR_ALL_WINDOWS_FROM_BACK(w) {
1601  if (w->window_class == WC_MAIN_WINDOW) continue;
1602 
1603  if (left + width > w->left &&
1604  w->left + w->width > left &&
1605  top + height > w->top &&
1606  w->top + w->height > top) {
1607  return false;
1608  }
1609  }
1610 
1611  pos.x = left;
1612  pos.y = top;
1613  return true;
1614 }
1615 
1622 static Point GetAutoPlacePosition(int width, int height)
1623 {
1624  Point pt;
1625 
1626  /* First attempt, try top-left of the screen */
1627  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1628  if (IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->height + 2 : 2, width, height, pt)) return pt;
1629 
1630  /* Second attempt, try around all existing windows with a distance of 2 pixels.
1631  * The new window must be entirely on-screen, and not overlap with an existing window.
1632  * Eight starting points are tried, two at each corner.
1633  */
1634  const Window *w;
1635  FOR_ALL_WINDOWS_FROM_BACK(w) {
1636  if (w->window_class == WC_MAIN_WINDOW) continue;
1637 
1638  if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt;
1639  if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt;
1640  if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt;
1641  if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt;
1642  if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt;
1643  if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt;
1644  if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt;
1645  if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt;
1646  }
1647 
1648  /* Third attempt, try around all existing windows with a distance of 2 pixels.
1649  * The new window may be partly off-screen, and must not overlap with an existing window.
1650  * Only four starting points are tried.
1651  */
1652  FOR_ALL_WINDOWS_FROM_BACK(w) {
1653  if (w->window_class == WC_MAIN_WINDOW) continue;
1654 
1655  if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt;
1656  if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt;
1657  if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt;
1658  if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt;
1659  }
1660 
1661  /* Fourth and final attempt, put window at diagonal starting from (0, 24), try multiples
1662  * of (+5, +5)
1663  */
1664  int left = 0, top = 24;
1665 
1666 restart:
1667  FOR_ALL_WINDOWS_FROM_BACK(w) {
1668  if (w->left == left && w->top == top) {
1669  left += 5;
1670  top += 5;
1671  goto restart;
1672  }
1673  }
1674 
1675  pt.x = left;
1676  pt.y = top;
1677  return pt;
1678 }
1679 
1687 {
1688  const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1689  assert(w != NULL);
1690  Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1691  return pt;
1692 }
1693 
1711 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1712 {
1713  Point pt;
1714  const Window *w;
1715 
1716  int16 default_width = max(desc->GetDefaultWidth(), sm_width);
1717  int16 default_height = max(desc->GetDefaultHeight(), sm_height);
1718 
1719  if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ &&
1720  (w = FindWindowById(desc->parent_cls, window_number)) != NULL &&
1721  w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) {
1722 
1723  pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10);
1724  if (pt.x > _screen.width + 10 - default_width) {
1725  pt.x = (_screen.width + 10 - default_width) - 20;
1726  }
1727  pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? w->height : 10);
1728  return pt;
1729  }
1730 
1731  switch (desc->default_pos) {
1732  case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1733  return GetToolbarAlignedWindowPosition(default_width);
1734 
1735  case WDP_AUTO: // Find a good automatic position for the window
1736  return GetAutoPlacePosition(default_width, default_height);
1737 
1738  case WDP_CENTER: // Centre the window horizontally
1739  pt.x = (_screen.width - default_width) / 2;
1740  pt.y = (_screen.height - default_height) / 2;
1741  break;
1742 
1743  case WDP_MANUAL:
1744  pt.x = 0;
1745  pt.y = 0;
1746  break;
1747 
1748  default:
1749  NOT_REACHED();
1750  }
1751 
1752  return pt;
1753 }
1754 
1755 /* virtual */ Point Window::OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1756 {
1757  return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1758 }
1759 
1767 void Window::CreateNestedTree(bool fill_nested)
1768 {
1769  int biggest_index = -1;
1770  this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_parts, this->window_desc->nwid_length, &biggest_index, &this->shade_select);
1771  this->nested_array_size = (uint)(biggest_index + 1);
1772 
1773  if (fill_nested) {
1774  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1775  this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
1776  }
1777 }
1778 
1784 {
1785  this->InitializeData(window_number);
1786  this->ApplyDefaults();
1787  Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1788  this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1790 }
1791 
1797 {
1798  this->CreateNestedTree(false);
1799  this->FinishInitNested(window_number);
1800 }
1801 
1806 Window::Window(WindowDesc *desc) : window_desc(desc), scrolling_scrollbar(-1)
1807 {
1808 }
1809 
1817 Window *FindWindowFromPt(int x, int y)
1818 {
1819  Window *w;
1820  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1821  if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1822  return w;
1823  }
1824  }
1825 
1826  return NULL;
1827 }
1828 
1833 {
1834  IConsoleClose();
1835 
1836  _z_back_window = NULL;
1837  _z_front_window = NULL;
1838  _focused_window = NULL;
1839  _mouseover_last_w = NULL;
1840  _last_scroll_window = NULL;
1841  _scrolling_viewport = false;
1842  _mouse_hovering = false;
1843 
1844  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1845  NWidgetScrollbar::InvalidateDimensionCache();
1846 
1847  ShowFirstError();
1848 }
1849 
1854 {
1856 
1857  Window *w;
1858  FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
1859 
1860  for (w = _z_front_window; w != NULL; /* nothing */) {
1861  Window *to_del = w;
1862  w = w->z_back;
1863  free(to_del);
1864  }
1865 
1866  _z_front_window = NULL;
1867  _z_back_window = NULL;
1868 }
1869 
1874 {
1876  InitWindowSystem();
1877  _thd.Reset();
1878 }
1879 
1880 static void DecreaseWindowCounters()
1881 {
1882  Window *w;
1883  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1884  if (_scroller_click_timeout == 0) {
1885  /* Unclick scrollbar buttons if they are pressed. */
1886  for (uint i = 0; i < w->nested_array_size; i++) {
1887  NWidgetBase *nwid = w->nested_array[i];
1888  if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
1889  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1892  w->scrolling_scrollbar = -1;
1893  sb->SetDirty(w);
1894  }
1895  }
1896  }
1897  }
1898 
1899  /* Handle editboxes */
1900  for (SmallMap<int, QueryString*>::Pair *it = w->querystrings.Begin(); it != w->querystrings.End(); ++it) {
1901  it->second->HandleEditBox(w, it->first);
1902  }
1903 
1904  w->OnMouseLoop();
1905  }
1906 
1907  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1908  if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) {
1909  CLRBITS(w->flags, WF_TIMEOUT);
1910 
1911  w->OnTimeout();
1912  w->RaiseButtons(true);
1913  }
1914  }
1915 }
1916 
1917 static void HandlePlacePresize()
1918 {
1919  if (_special_mouse_mode != WSM_PRESIZE) return;
1920 
1921  Window *w = _thd.GetCallbackWnd();
1922  if (w == NULL) return;
1923 
1924  Point pt = GetTileBelowCursor();
1925  if (pt.x == -1) {
1926  _thd.selend.x = -1;
1927  return;
1928  }
1929 
1930  w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1931 }
1932 
1938 {
1940 
1941  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
1942 
1943  Window *w = _thd.GetCallbackWnd();
1944  if (w != NULL) {
1945  /* Send an event in client coordinates. */
1946  Point pt;
1947  pt.x = _cursor.pos.x - w->left;
1948  pt.y = _cursor.pos.y - w->top;
1949  if (_left_button_down) {
1950  w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
1951  } else {
1952  w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
1953  }
1954  }
1955 
1956  if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
1957  return ES_HANDLED;
1958 }
1959 
1961 static void HandleMouseOver()
1962 {
1963  Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
1964 
1965  /* We changed window, put a MOUSEOVER event to the last window */
1966  if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
1967  /* Reset mouse-over coordinates of previous window */
1968  Point pt = { -1, -1 };
1969  _mouseover_last_w->OnMouseOver(pt, 0);
1970  }
1971 
1972  /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
1973  _mouseover_last_w = w;
1974 
1975  if (w != NULL) {
1976  /* send an event in client coordinates. */
1977  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
1978  const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
1979  if (widget != NULL) w->OnMouseOver(pt, widget->index);
1980  }
1981 }
1982 
1984 static const int MIN_VISIBLE_TITLE_BAR = 13;
1985 
1990 };
1991 
2002 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
2003 {
2004  if (v == NULL) return;
2005 
2006  int v_bottom = v->top + v->height;
2007  int v_right = v->left + v->width;
2008  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.
2009 
2010  if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
2011  if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
2012 
2013  /* Vertically, the rectangle is hidden behind v. */
2014  if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
2015  if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
2016  return;
2017  }
2018  if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
2019  if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
2020  return;
2021  }
2022 
2023  /* Horizontally also hidden, force movement to a safe area. */
2024  if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
2025  *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
2026  } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
2027  *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2028  } else {
2029  *ny = safe_y;
2030  }
2031 }
2032 
2040 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2041 {
2042  /* Search for the title bar rectangle. */
2043  Rect caption_rect;
2044  const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2045  if (caption != NULL) {
2046  caption_rect.left = caption->pos_x;
2047  caption_rect.right = caption->pos_x + caption->current_x;
2048  caption_rect.top = caption->pos_y;
2049  caption_rect.bottom = caption->pos_y + caption->current_y;
2050 
2051  /* Make sure the window doesn't leave the screen */
2052  nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2053  ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2054 
2055  /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2056  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2057  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2058  }
2059 
2060  if (w->viewport != NULL) {
2061  w->viewport->left += nx - w->left;
2062  w->viewport->top += ny - w->top;
2063  }
2064 
2065  w->left = nx;
2066  w->top = ny;
2067 }
2068 
2079 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
2080 {
2081  if (delta_x != 0 || delta_y != 0) {
2082  if (clamp_to_screen) {
2083  /* Determine the new right/bottom position. If that is outside of the bounds of
2084  * the resolution clamp it in such a manner that it stays within the bounds. */
2085  int new_right = w->left + w->width + delta_x;
2086  int new_bottom = w->top + w->height + delta_y;
2087  if (new_right >= (int)_cur_resolution.width) delta_x -= Ceil(new_right - _cur_resolution.width, max(1U, w->nested_root->resize_x));
2088  if (new_bottom >= (int)_cur_resolution.height) delta_y -= Ceil(new_bottom - _cur_resolution.height, max(1U, w->nested_root->resize_y));
2089  }
2090 
2091  w->SetDirty();
2092 
2093  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);
2094  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);
2095  assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2096  assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2097 
2099  w->width = w->nested_root->current_x;
2100  w->height = w->nested_root->current_y;
2101  }
2102 
2103  EnsureVisibleCaption(w, w->left, w->top);
2104 
2105  /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
2106  w->OnResize();
2107  w->SetDirty();
2108 }
2109 
2116 {
2118  return (w == NULL) ? 0 : w->top + w->height;
2119 }
2120 
2127 {
2129  return (w == NULL) ? _screen.height : w->top;
2130 }
2131 
2132 static bool _dragging_window;
2133 
2139 {
2140  /* Get out immediately if no window is being dragged at all. */
2141  if (!_dragging_window) return ES_NOT_HANDLED;
2142 
2143  /* If button still down, but cursor hasn't moved, there is nothing to do. */
2144  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2145 
2146  /* Otherwise find the window... */
2147  Window *w;
2148  FOR_ALL_WINDOWS_FROM_BACK(w) {
2149  if (w->flags & WF_DRAGGING) {
2150  /* Stop the dragging if the left mouse button was released */
2151  if (!_left_button_down) {
2152  w->flags &= ~WF_DRAGGING;
2153  break;
2154  }
2155 
2156  w->SetDirty();
2157 
2158  int x = _cursor.pos.x + _drag_delta.x;
2159  int y = _cursor.pos.y + _drag_delta.y;
2160  int nx = x;
2161  int ny = y;
2162 
2164  const Window *v;
2165 
2168  int delta;
2169 
2170  FOR_ALL_WINDOWS_FROM_BACK(v) {
2171  if (v == w) continue; // Don't snap at yourself
2172 
2173  if (y + w->height > v->top && y < v->top + v->height) {
2174  /* Your left border <-> other right border */
2175  delta = abs(v->left + v->width - x);
2176  if (delta <= hsnap) {
2177  nx = v->left + v->width;
2178  hsnap = delta;
2179  }
2180 
2181  /* Your right border <-> other left border */
2182  delta = abs(v->left - x - w->width);
2183  if (delta <= hsnap) {
2184  nx = v->left - w->width;
2185  hsnap = delta;
2186  }
2187  }
2188 
2189  if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2190  /* Your left border <-> other left border */
2191  delta = abs(v->left - x);
2192  if (delta <= hsnap) {
2193  nx = v->left;
2194  hsnap = delta;
2195  }
2196 
2197  /* Your right border <-> other right border */
2198  delta = abs(v->left + v->width - x - w->width);
2199  if (delta <= hsnap) {
2200  nx = v->left + v->width - w->width;
2201  hsnap = delta;
2202  }
2203  }
2204 
2205  if (x + w->width > v->left && x < v->left + v->width) {
2206  /* Your top border <-> other bottom border */
2207  delta = abs(v->top + v->height - y);
2208  if (delta <= vsnap) {
2209  ny = v->top + v->height;
2210  vsnap = delta;
2211  }
2212 
2213  /* Your bottom border <-> other top border */
2214  delta = abs(v->top - y - w->height);
2215  if (delta <= vsnap) {
2216  ny = v->top - w->height;
2217  vsnap = delta;
2218  }
2219  }
2220 
2221  if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2222  /* Your top border <-> other top border */
2223  delta = abs(v->top - y);
2224  if (delta <= vsnap) {
2225  ny = v->top;
2226  vsnap = delta;
2227  }
2228 
2229  /* Your bottom border <-> other bottom border */
2230  delta = abs(v->top + v->height - y - w->height);
2231  if (delta <= vsnap) {
2232  ny = v->top + v->height - w->height;
2233  vsnap = delta;
2234  }
2235  }
2236  }
2237  }
2238 
2239  EnsureVisibleCaption(w, nx, ny);
2240 
2241  w->SetDirty();
2242  return ES_HANDLED;
2243  } else if (w->flags & WF_SIZING) {
2244  /* Stop the sizing if the left mouse button was released */
2245  if (!_left_button_down) {
2246  w->flags &= ~WF_SIZING;
2247  w->SetDirty();
2248  break;
2249  }
2250 
2251  /* Compute difference in pixels between cursor position and reference point in the window.
2252  * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2253  */
2254  int x, y = _cursor.pos.y - _drag_delta.y;
2255  if (w->flags & WF_SIZING_LEFT) {
2256  x = _drag_delta.x - _cursor.pos.x;
2257  } else {
2258  x = _cursor.pos.x - _drag_delta.x;
2259  }
2260 
2261  /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2262  if (w->resize.step_width == 0) x = 0;
2263  if (w->resize.step_height == 0) y = 0;
2264 
2265  /* Check the resize button won't go past the bottom of the screen */
2266  if (w->top + w->height + y > _screen.height) {
2267  y = _screen.height - w->height - w->top;
2268  }
2269 
2270  /* X and Y has to go by step.. calculate it.
2271  * The cast to int is necessary else x/y are implicitly casted to
2272  * unsigned int, which won't work. */
2273  if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2274  if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2275 
2276  /* Check that we don't go below the minimum set size */
2277  if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2278  x = w->nested_root->smallest_x - w->width;
2279  }
2280  if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2281  y = w->nested_root->smallest_y - w->height;
2282  }
2283 
2284  /* Window already on size */
2285  if (x == 0 && y == 0) return ES_HANDLED;
2286 
2287  /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2288  _drag_delta.y += y;
2289  if ((w->flags & WF_SIZING_LEFT) && x != 0) {
2290  _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2291  w->SetDirty();
2292  w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2293  /* ResizeWindow() below ensures marking new position as dirty. */
2294  } else {
2295  _drag_delta.x += x;
2296  }
2297 
2298  /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
2299  ResizeWindow(w, x, y);
2300  return ES_HANDLED;
2301  }
2302  }
2303 
2304  _dragging_window = false;
2305  return ES_HANDLED;
2306 }
2307 
2312 static void StartWindowDrag(Window *w)
2313 {
2314  w->flags |= WF_DRAGGING;
2315  w->flags &= ~WF_CENTERED;
2316  _dragging_window = true;
2317 
2318  _drag_delta.x = w->left - _cursor.pos.x;
2319  _drag_delta.y = w->top - _cursor.pos.y;
2320 
2321  BringWindowToFront(w);
2323 }
2324 
2330 static void StartWindowSizing(Window *w, bool to_left)
2331 {
2332  w->flags |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
2333  w->flags &= ~WF_CENTERED;
2334  _dragging_window = true;
2335 
2336  _drag_delta.x = _cursor.pos.x;
2337  _drag_delta.y = _cursor.pos.y;
2338 
2339  BringWindowToFront(w);
2341 }
2342 
2348 {
2349  Window *w;
2350  FOR_ALL_WINDOWS_FROM_BACK(w) {
2351  if (w->scrolling_scrollbar >= 0) {
2352  /* Abort if no button is clicked any more. */
2353  if (!_left_button_down) {
2354  w->scrolling_scrollbar = -1;
2355  w->SetDirty();
2356  return ES_HANDLED;
2357  }
2358 
2359  int i;
2361  bool rtl = false;
2362 
2363  if (sb->type == NWID_HSCROLLBAR) {
2364  i = _cursor.pos.x - _cursorpos_drag_start.x;
2365  rtl = _current_text_dir == TD_RTL;
2366  } else {
2367  i = _cursor.pos.y - _cursorpos_drag_start.y;
2368  }
2369 
2370  if (sb->disp_flags & ND_SCROLLBAR_BTN) {
2371  if (_scroller_click_timeout == 1) {
2372  _scroller_click_timeout = 3;
2373  sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
2374  w->SetDirty();
2375  }
2376  return ES_HANDLED;
2377  }
2378 
2379  /* Find the item we want to move to and make sure it's inside bounds. */
2380  int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
2381  if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
2382  if (pos != sb->GetPosition()) {
2383  sb->SetPosition(pos);
2384  w->SetDirty();
2385  }
2386  return ES_HANDLED;
2387  }
2388  }
2389 
2390  return ES_NOT_HANDLED;
2391 }
2392 
2398 {
2399  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2400 
2401  if (!_scrolling_viewport) return ES_NOT_HANDLED;
2402 
2403  /* When we don't have a last scroll window we are starting to scroll.
2404  * When the last scroll window and this are not the same we went
2405  * outside of the window and should not left-mouse scroll anymore. */
2406  if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2407 
2408  if (_last_scroll_window == NULL || !(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down))) {
2409  _cursor.fix_at = false;
2410  _scrolling_viewport = false;
2411  _last_scroll_window = NULL;
2412  return ES_NOT_HANDLED;
2413  }
2414 
2415  if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
2416  /* If the main window is following a vehicle, then first let go of it! */
2417  const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2418  ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2419  return ES_NOT_HANDLED;
2420  }
2421 
2422  Point delta;
2424  delta.x = -_cursor.delta.x;
2425  delta.y = -_cursor.delta.y;
2426  } else {
2427  delta.x = _cursor.delta.x;
2428  delta.y = _cursor.delta.y;
2429  }
2430 
2431  if (scrollwheel_scrolling) {
2432  /* We are using scrollwheels for scrolling */
2433  delta.x = _cursor.h_wheel;
2434  delta.y = _cursor.v_wheel;
2435  _cursor.v_wheel = 0;
2436  _cursor.h_wheel = 0;
2437  }
2438 
2439  /* Create a scroll-event and send it to the window */
2440  if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2441 
2442  _cursor.delta.x = 0;
2443  _cursor.delta.y = 0;
2444  return ES_HANDLED;
2445 }
2446 
2458 {
2459  bool bring_to_front = false;
2460 
2461  if (w->window_class == WC_MAIN_WINDOW ||
2462  IsVitalWindow(w) ||
2463  w->window_class == WC_TOOLTIPS ||
2465  return true;
2466  }
2467 
2468  /* Use unshaded window size rather than current size for shaded windows. */
2469  int w_width = w->width;
2470  int w_height = w->height;
2471  if (w->IsShaded()) {
2472  w_width = w->unshaded_size.width;
2473  w_height = w->unshaded_size.height;
2474  }
2475 
2476  Window *u;
2478  /* A modal child will prevent the activation of the parent window */
2479  if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) {
2480  u->SetWhiteBorder();
2481  u->SetDirty();
2482  return false;
2483  }
2484 
2485  if (u->window_class == WC_MAIN_WINDOW ||
2486  IsVitalWindow(u) ||
2487  u->window_class == WC_TOOLTIPS ||
2489  continue;
2490  }
2491 
2492  /* Window sizes don't interfere, leave z-order alone */
2493  if (w->left + w_width <= u->left ||
2494  u->left + u->width <= w->left ||
2495  w->top + w_height <= u->top ||
2496  u->top + u->height <= w->top) {
2497  continue;
2498  }
2499 
2500  bring_to_front = true;
2501  }
2502 
2503  if (bring_to_front) BringWindowToFront(w);
2504  return true;
2505 }
2506 
2515 EventState Window::HandleEditBoxKey(int wid, WChar key, uint16 keycode)
2516 {
2517  QueryString *query = this->GetQueryString(wid);
2518  if (query == NULL) return ES_NOT_HANDLED;
2519 
2520  int action = QueryString::ACTION_NOTHING;
2521 
2522  switch (query->text.HandleKeyPress(key, keycode)) {
2523  case HKPR_EDITING:
2524  this->SetWidgetDirty(wid);
2525  this->OnEditboxChanged(wid);
2526  break;
2527 
2528  case HKPR_CURSOR:
2529  this->SetWidgetDirty(wid);
2530  /* For the OSK also invalidate the parent window */
2531  if (this->window_class == WC_OSK) this->InvalidateData();
2532  break;
2533 
2534  case HKPR_CONFIRM:
2535  if (this->window_class == WC_OSK) {
2536  this->OnClick(Point(), WID_OSK_OK, 1);
2537  } else if (query->ok_button >= 0) {
2538  this->OnClick(Point(), query->ok_button, 1);
2539  } else {
2540  action = query->ok_button;
2541  }
2542  break;
2543 
2544  case HKPR_CANCEL:
2545  if (this->window_class == WC_OSK) {
2546  this->OnClick(Point(), WID_OSK_CANCEL, 1);
2547  } else if (query->cancel_button >= 0) {
2548  this->OnClick(Point(), query->cancel_button, 1);
2549  } else {
2550  action = query->cancel_button;
2551  }
2552  break;
2553 
2554  case HKPR_NOT_HANDLED:
2555  return ES_NOT_HANDLED;
2556 
2557  default: break;
2558  }
2559 
2560  switch (action) {
2562  this->UnfocusFocusedWidget();
2563  break;
2564 
2566  if (query->text.bytes <= 1) {
2567  /* If already empty, unfocus instead */
2568  this->UnfocusFocusedWidget();
2569  } else {
2570  query->text.DeleteAll();
2571  this->SetWidgetDirty(wid);
2572  this->OnEditboxChanged(wid);
2573  }
2574  break;
2575 
2576  default:
2577  break;
2578  }
2579 
2580  return ES_HANDLED;
2581 }
2582 
2588 void HandleKeypress(uint keycode, WChar key)
2589 {
2590  /* World generation is multithreaded and messes with companies.
2591  * But there is no company related window open anyway, so _current_company is not used. */
2592  assert(HasModalProgress() || IsLocalCompany());
2593 
2594  /*
2595  * The Unicode standard defines an area called the private use area. Code points in this
2596  * area are reserved for private use and thus not portable between systems. For instance,
2597  * Apple defines code points for the arrow keys in this area, but these are only printable
2598  * on a system running OS X. We don't want these keys to show up in text fields and such,
2599  * and thus we have to clear the unicode character when we encounter such a key.
2600  */
2601  if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2602 
2603  /*
2604  * If both key and keycode is zero, we don't bother to process the event.
2605  */
2606  if (key == 0 && keycode == 0) return;
2607 
2608  /* Check if the focused window has a focused editbox */
2609  if (EditBoxInGlobalFocus()) {
2610  /* All input will in this case go to the focused editbox */
2611  if (_focused_window->window_class == WC_CONSOLE) {
2612  if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2613  } else {
2614  if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return;
2615  }
2616  }
2617 
2618  /* Call the event, start with the uppermost window, but ignore the toolbar. */
2619  Window *w;
2620  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2621  if (w->window_class == WC_MAIN_TOOLBAR) continue;
2622  if (w->window_desc->hotkeys != NULL) {
2623  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2624  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2625  }
2626  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2627  }
2628 
2630  /* When there is no toolbar w is null, check for that */
2631  if (w != NULL) {
2632  if (w->window_desc->hotkeys != NULL) {
2633  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2634  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2635  }
2636  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2637  }
2638 
2639  HandleGlobalHotkeys(key, keycode);
2640 }
2641 
2646 {
2647  /* Call the event, start with the uppermost window. */
2648  Window *w;
2649  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2650  if (w->OnCTRLStateChange() == ES_HANDLED) return;
2651  }
2652 }
2653 
2659 /* virtual */ void Window::InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2660 {
2661  QueryString *query = this->GetQueryString(wid);
2662  if (query == NULL) return;
2663 
2664  if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2665  this->SetWidgetDirty(wid);
2666  this->OnEditboxChanged(wid);
2667  }
2668 }
2669 
2676 void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2677 {
2678  if (!EditBoxInGlobalFocus()) return;
2679 
2680  _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end);
2681 }
2682 
2690 
2695 static void HandleAutoscroll()
2696 {
2697  if (_game_mode == GM_MENU || HasModalProgress()) return;
2699  if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2700 
2701  int x = _cursor.pos.x;
2702  int y = _cursor.pos.y;
2703  Window *w = FindWindowFromPt(x, y);
2704  if (w == NULL || w->flags & WF_DISABLE_VP_SCROLL) return;
2706 
2707  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2708  if (vp == NULL) return;
2709 
2710  x -= vp->left;
2711  y -= vp->top;
2712 
2713  /* here allows scrolling in both x and y axis */
2714 #define scrollspeed 3
2715  if (x - 15 < 0) {
2716  w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
2717  } else if (15 - (vp->width - x) > 0) {
2718  w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
2719  }
2720  if (y - 15 < 0) {
2721  w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
2722  } else if (15 - (vp->height - y) > 0) {
2723  w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
2724  }
2725 #undef scrollspeed
2726 }
2727 
2729  MC_NONE = 0,
2730  MC_LEFT,
2731  MC_RIGHT,
2732  MC_DOUBLE_LEFT,
2733  MC_HOVER,
2734 
2738 };
2740 
2741 static void ScrollMainViewport(int x, int y)
2742 {
2743  if (_game_mode != GM_MENU) {
2745  assert(w);
2746 
2749  }
2750 }
2751 
2761 static const int8 scrollamt[16][2] = {
2762  { 0, 0},
2763  {-2, 0},
2764  { 0, -2},
2765  {-2, -1},
2766  { 2, 0},
2767  { 0, 0},
2768  { 2, -1},
2769  { 0, -2},
2770  { 0, 2},
2771  {-2, 1},
2772  { 0, 0},
2773  {-2, 0},
2774  { 2, 1},
2775  { 0, 2},
2776  { 2, 0},
2777  { 0, 0},
2778 };
2779 
2780 static void HandleKeyScrolling()
2781 {
2782  /*
2783  * Check that any of the dirkeys is pressed and that the focused window
2784  * doesn't have an edit-box as focused widget.
2785  */
2786  if (_dirkeys && !EditBoxInGlobalFocus()) {
2787  int factor = _shift_pressed ? 50 : 10;
2788  ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2789  }
2790 }
2791 
2792 static void MouseLoop(MouseClick click, int mousewheel)
2793 {
2794  /* World generation is multithreaded and messes with companies.
2795  * But there is no company related window open anyway, so _current_company is not used. */
2796  assert(HasModalProgress() || IsLocalCompany());
2797 
2798  HandlePlacePresize();
2800 
2801  if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2802  if (HandleMouseDragDrop() == ES_HANDLED) return;
2803  if (HandleWindowDragging() == ES_HANDLED) return;
2804  if (HandleScrollbarScrolling() == ES_HANDLED) return;
2805  if (HandleViewportScroll() == ES_HANDLED) return;
2806 
2807  HandleMouseOver();
2808 
2809  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2810  if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2811 
2812  int x = _cursor.pos.x;
2813  int y = _cursor.pos.y;
2814  Window *w = FindWindowFromPt(x, y);
2815  if (w == NULL) return;
2816 
2817  if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2818  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2819 
2820  /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2821  if (vp != NULL && (_game_mode == GM_MENU || HasModalProgress())) return;
2822 
2823  if (mousewheel != 0) {
2824  /* Send mousewheel event to window */
2825  w->OnMouseWheel(mousewheel);
2826 
2827  /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2828  if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2829  }
2830 
2831  if (vp != NULL) {
2832  if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button
2833  switch (click) {
2834  case MC_DOUBLE_LEFT:
2835  case MC_LEFT:
2836  if (HandleViewportClicked(vp, x, y)) return;
2837  if (!(w->flags & WF_DISABLE_VP_SCROLL) &&
2839  _scrolling_viewport = true;
2840  _cursor.fix_at = false;
2841  return;
2842  }
2843  break;
2844 
2845  case MC_RIGHT:
2846  if (!(w->flags & WF_DISABLE_VP_SCROLL)) {
2847  _scrolling_viewport = true;
2848  _cursor.fix_at = true;
2849 
2850  /* clear 2D scrolling caches before we start a 2D scroll */
2851  _cursor.h_wheel = 0;
2852  _cursor.v_wheel = 0;
2853  return;
2854  }
2855  break;
2856 
2857  default:
2858  break;
2859  }
2860  }
2861 
2862  if (vp == NULL || (w->flags & WF_DISABLE_VP_SCROLL)) {
2863  switch (click) {
2864  case MC_LEFT:
2865  case MC_DOUBLE_LEFT:
2866  DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2867  break;
2868 
2869  default:
2870  if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
2871  /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2872  * Simulate a right button click so we can get started. */
2873  /* FALL THROUGH */
2874 
2875  case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
2876 
2877  case MC_HOVER: DispatchHoverEvent(w, x - w->left, y - w->top); break;
2878  }
2879  }
2880 }
2881 
2886 {
2887  /* World generation is multithreaded and messes with companies.
2888  * But there is no company related window open anyway, so _current_company is not used. */
2889  assert(HasModalProgress() || IsLocalCompany());
2890 
2891  static int double_click_time = 0;
2892  static Point double_click_pos = {0, 0};
2893 
2894  /* Mouse event? */
2895  MouseClick click = MC_NONE;
2897  click = MC_LEFT;
2898  if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
2899  double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2900  double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2901  click = MC_DOUBLE_LEFT;
2902  }
2903  double_click_time = _realtime_tick;
2904  double_click_pos = _cursor.pos;
2905  _left_button_clicked = true;
2906  _input_events_this_tick++;
2907  } else if (_right_button_clicked) {
2908  _right_button_clicked = false;
2909  click = MC_RIGHT;
2910  _input_events_this_tick++;
2911  }
2912 
2913  int mousewheel = 0;
2914  if (_cursor.wheel) {
2915  mousewheel = _cursor.wheel;
2916  _cursor.wheel = 0;
2917  _input_events_this_tick++;
2918  }
2919 
2920  static uint32 hover_time = 0;
2921  static Point hover_pos = {0, 0};
2922 
2924  if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
2925  hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
2926  hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
2927  hover_pos = _cursor.pos;
2928  hover_time = _realtime_tick;
2929  _mouse_hovering = false;
2930  } else {
2931  if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
2932  click = MC_HOVER;
2933  _input_events_this_tick++;
2934  _mouse_hovering = true;
2935  }
2936  }
2937  }
2938 
2939  /* Handle sprite picker before any GUI interaction */
2941  /* Next realtime tick? Then redraw has finished */
2942  _newgrf_debug_sprite_picker.mode = SPM_NONE;
2944  }
2945 
2946  if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
2947  /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
2949  _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
2952  _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
2954  } else {
2955  MouseLoop(click, mousewheel);
2956  }
2957 
2958  /* We have moved the mouse the required distance,
2959  * no need to move it at any later time. */
2960  _cursor.delta.x = 0;
2961  _cursor.delta.y = 0;
2962 }
2963 
2967 static void CheckSoftLimit()
2968 {
2969  if (_settings_client.gui.window_soft_limit == 0) return;
2970 
2971  for (;;) {
2972  uint deletable_count = 0;
2973  Window *w, *last_deletable = NULL;
2974  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2975  if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags & WF_STICKY)) continue;
2976 
2977  last_deletable = w;
2978  deletable_count++;
2979  }
2980 
2981  /* We've not reached the soft limit yet. */
2982  if (deletable_count <= _settings_client.gui.window_soft_limit) break;
2983 
2984  assert(last_deletable != NULL);
2985  delete last_deletable;
2986  }
2987 }
2988 
2993 {
2994  /* World generation is multithreaded and messes with companies.
2995  * But there is no company related window open anyway, so _current_company is not used. */
2996  assert(HasModalProgress() || IsLocalCompany());
2997 
2998  CheckSoftLimit();
2999  HandleKeyScrolling();
3000 
3001  /* Do the actual free of the deleted windows. */
3002  for (Window *v = _z_front_window; v != NULL; /* nothing */) {
3003  Window *w = v;
3004  v = v->z_back;
3005 
3006  if (w->window_class != WC_INVALID) continue;
3007 
3009  free(w);
3010  }
3011 
3012  if (_scroller_click_timeout != 0) _scroller_click_timeout--;
3013  DecreaseWindowCounters();
3014 
3015  if (_input_events_this_tick != 0) {
3016  /* The input loop is called only once per GameLoop() - so we can clear the counter here */
3017  _input_events_this_tick = 0;
3018  /* there were some inputs this tick, don't scroll ??? */
3019  return;
3020  }
3021 
3022  /* HandleMouseEvents was already called for this tick */
3024  HandleAutoscroll();
3025 }
3026 
3031 {
3032  Window *w;
3033 
3034  static int highlight_timer = 1;
3035  if (--highlight_timer == 0) {
3036  highlight_timer = 15;
3038  }
3039 
3040  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3043  }
3044 
3045  /* Skip the actual drawing on dedicated servers without screen.
3046  * But still empty the invalidation queues above. */
3047  if (_network_dedicated) return;
3048 
3049  static int we4_timer = 0;
3050  int t = we4_timer + 1;
3051 
3052  if (t >= 100) {
3053  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3054  w->OnHundredthTick();
3055  }
3056  t = 0;
3057  }
3058  we4_timer = t;
3059 
3060  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3061  if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) {
3063  w->SetDirty();
3064  }
3065  }
3066 
3067  DrawDirtyBlocks();
3068 
3069  FOR_ALL_WINDOWS_FROM_BACK(w) {
3070  /* Update viewport only if window is not shaded. */
3071  if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
3072  }
3074  /* Redraw mouse cursor in case it was hidden */
3075  DrawMouseCursor();
3076 }
3077 
3084 {
3085  const Window *w;
3086  FOR_ALL_WINDOWS_FROM_BACK(w) {
3087  if (w->window_class == cls && w->window_number == number) w->SetDirty();
3088  }
3089 }
3090 
3097 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
3098 {
3099  const Window *w;
3100  FOR_ALL_WINDOWS_FROM_BACK(w) {
3101  if (w->window_class == cls && w->window_number == number) {
3102  w->SetWidgetDirty(widget_index);
3103  }
3104  }
3105 }
3106 
3112 {
3113  Window *w;
3114  FOR_ALL_WINDOWS_FROM_BACK(w) {
3115  if (w->window_class == cls) w->SetDirty();
3116  }
3117 }
3118 
3124 void Window::InvalidateData(int data, bool gui_scope)
3125 {
3126  this->SetDirty();
3127  if (!gui_scope) {
3128  /* Schedule GUI-scope invalidation for next redraw. */
3129  *this->scheduled_invalidation_data.Append() = data;
3130  }
3131  this->OnInvalidateData(data, gui_scope);
3132 }
3133 
3138 {
3139  for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
3140  this->OnInvalidateData(*data, true);
3141  }
3143 }
3144 
3149 {
3150  if ((this->flags & WF_HIGHLIGHTED) == 0) return;
3151 
3152  for (uint i = 0; i < this->nested_array_size; i++) {
3153  if (this->IsWidgetHighlighted(i)) this->SetWidgetDirty(i);
3154  }
3155 }
3156 
3183 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3184 {
3185  Window *w;
3186  FOR_ALL_WINDOWS_FROM_BACK(w) {
3187  if (w->window_class == cls && w->window_number == number) {
3188  w->InvalidateData(data, gui_scope);
3189  }
3190  }
3191 }
3192 
3201 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3202 {
3203  Window *w;
3204 
3205  FOR_ALL_WINDOWS_FROM_BACK(w) {
3206  if (w->window_class == cls) {
3207  w->InvalidateData(data, gui_scope);
3208  }
3209  }
3210 }
3211 
3216 {
3217  Window *w;
3218  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3219  w->OnTick();
3220  }
3221 }
3222 
3230 {
3231  Window *w;
3232 
3233 restart_search:
3234  /* When we find the window to delete, we need to restart the search
3235  * as deleting this window could cascade in deleting (many) others
3236  * anywhere in the z-array */
3237  FOR_ALL_WINDOWS_FROM_BACK(w) {
3238  if (w->window_class != WC_MAIN_WINDOW &&
3239  w->window_class != WC_SELECT_GAME &&
3240  w->window_class != WC_MAIN_TOOLBAR &&
3241  w->window_class != WC_STATUS_BAR &&
3242  w->window_class != WC_TOOLTIPS &&
3243  (w->flags & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
3244 
3245  delete w;
3246  goto restart_search;
3247  }
3248  }
3249 }
3250 
3259 {
3260  Window *w;
3261 
3262  /* Delete every window except for stickied ones, then sticky ones as well */
3264 
3265 restart_search:
3266  /* When we find the window to delete, we need to restart the search
3267  * as deleting this window could cascade in deleting (many) others
3268  * anywhere in the z-array */
3269  FOR_ALL_WINDOWS_FROM_BACK(w) {
3270  if (w->flags & WF_STICKY) {
3271  delete w;
3272  goto restart_search;
3273  }
3274  }
3275 }
3276 
3282 {
3283  Window *w;
3284 
3285 restart_search:
3286  /* When we find the window to delete, we need to restart the search
3287  * as deleting this window could cascade in deleting (many) others
3288  * anywhere in the z-array */
3289  FOR_ALL_WINDOWS_FROM_BACK(w) {
3290  if (w->window_desc->flags & WDF_CONSTRUCTION) {
3291  delete w;
3292  goto restart_search;
3293  }
3294  }
3295 
3296  FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
3297 }
3298 
3301 {
3304 }
3305 
3308 {
3309  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3310  NWidgetScrollbar::InvalidateDimensionCache();
3311 
3312  extern void InitDepotWindowBlockSizes();
3314 
3315  Window *w;
3316  FOR_ALL_WINDOWS_FROM_BACK(w) {
3317  w->ReInit();
3318  }
3319 #ifdef ENABLE_NETWORK
3320  void NetworkReInitChatBoxSize();
3322 #endif
3323 
3324  /* Make sure essential parts of all windows are visible */
3327 }
3328 
3336 static int PositionWindow(Window *w, WindowClass clss, int setting)
3337 {
3338  if (w == NULL || w->window_class != clss) {
3339  w = FindWindowById(clss, 0);
3340  }
3341  if (w == NULL) return 0;
3342 
3343  int old_left = w->left;
3344  switch (setting) {
3345  case 1: w->left = (_screen.width - w->width) / 2; break;
3346  case 2: w->left = _screen.width - w->width; break;
3347  default: w->left = 0; break;
3348  }
3349  if (w->viewport != NULL) w->viewport->left += w->left - old_left;
3350  SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3351  return w->left;
3352 }
3353 
3360 {
3361  DEBUG(misc, 5, "Repositioning Main Toolbar...");
3363 }
3364 
3371 {
3372  DEBUG(misc, 5, "Repositioning statusbar...");
3374 }
3375 
3382 {
3383  DEBUG(misc, 5, "Repositioning news message...");
3385 }
3386 
3393 {
3394  DEBUG(misc, 5, "Repositioning network chat window...");
3396 }
3397 
3398 
3404 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3405 {
3406  Window *w;
3407  FOR_ALL_WINDOWS_FROM_BACK(w) {
3408  if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
3409  w->viewport->follow_vehicle = to_index;
3410  w->SetDirty();
3411  }
3412  }
3413 }
3414 
3415 
3421 void RelocateAllWindows(int neww, int newh)
3422 {
3423  Window *w;
3424 
3425  FOR_ALL_WINDOWS_FROM_BACK(w) {
3426  int left, top;
3427  /* XXX - this probably needs something more sane. For example specifying
3428  * in a 'backup'-desc that the window should always be centered. */
3429  switch (w->window_class) {
3430  case WC_MAIN_WINDOW:
3431  case WC_BOOTSTRAP:
3432  ResizeWindow(w, neww, newh);
3433  continue;
3434 
3435  case WC_MAIN_TOOLBAR:
3436  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3437 
3438  top = w->top;
3439  left = PositionMainToolbar(w); // changes toolbar orientation
3440  break;
3441 
3442  case WC_NEWS_WINDOW:
3443  top = newh - w->height;
3444  left = PositionNewsMessage(w);
3445  break;
3446 
3447  case WC_STATUS_BAR:
3448  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3449 
3450  top = newh - w->height;
3451  left = PositionStatusbar(w);
3452  break;
3453 
3454  case WC_SEND_NETWORK_MSG:
3455  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3456 
3457  top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3458  left = PositionNetworkChatWindow(w);
3459  break;
3460 
3461  case WC_CONSOLE:
3462  IConsoleResize(w);
3463  continue;
3464 
3465  default: {
3466  if (w->flags & WF_CENTERED) {
3467  top = (newh - w->height) >> 1;
3468  left = (neww - w->width) >> 1;
3469  break;
3470  }
3471 
3472  left = w->left;
3473  if (left + (w->width >> 1) >= neww) left = neww - w->width;
3474  if (left < 0) left = 0;
3475 
3476  top = w->top;
3477  if (top + (w->height >> 1) >= newh) top = newh - w->height;
3478  break;
3479  }
3480  }
3481 
3482  EnsureVisibleCaption(w, left, top);
3483  }
3484 }
3485 
3492 {
3493  this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
3495 }