OpenTTD
window.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include <stdarg.h>
14 #include "company_func.h"
15 #include "gfx_func.h"
16 #include "console_func.h"
17 #include "console_gui.h"
18 #include "viewport_func.h"
19 #include "progress.h"
20 #include "blitter/factory.hpp"
21 #include "zoom_func.h"
22 #include "vehicle_base.h"
23 #include "window_func.h"
24 #include "tilehighlight_func.h"
25 #include "network/network.h"
26 #include "querystring_gui.h"
27 #include "widgets/dropdown_func.h"
28 #include "strings_func.h"
29 #include "settings_type.h"
30 #include "settings_func.h"
31 #include "ini_type.h"
32 #include "newgrf_debug.h"
33 #include "hotkeys.h"
34 #include "toolbar_gui.h"
35 #include "statusbar_gui.h"
36 #include "error.h"
37 #include "game/game.hpp"
38 #include "video/video_driver.hpp"
39 
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, BASE_DIR);
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  if (_settings_client.gui.hover_delay_ms == 0 && wid->tool_tip != 0) GuiShowTooltips(w, wid->tool_tip, 0, NULL, TCC_RIGHT_CLICK);
776 }
777 
784 static void DispatchHoverEvent(Window *w, int x, int y)
785 {
786  NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
787 
788  /* No widget to handle */
789  if (wid == NULL) return;
790 
791  /* Show the tooltip if there is any */
792  if (wid->tool_tip != 0) {
793  GuiShowTooltips(w, wid->tool_tip);
794  return;
795  }
796 
797  /* Widget has no index, so the window is not interested in it. */
798  if (wid->index < 0) return;
799 
800  Point pt = { x, y };
801  w->OnHover(pt, wid->index);
802 }
803 
811 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
812 {
813  if (nwid == NULL) return;
814 
815  /* Using wheel on caption/shade-box shades or unshades the window. */
816  if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
817  w->SetShaded(wheel < 0);
818  return;
819  }
820 
821  /* Wheeling a vertical scrollbar. */
822  if (nwid->type == NWID_VSCROLLBAR) {
823  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
824  if (sb->GetCount() > sb->GetCapacity()) {
825  sb->UpdatePosition(wheel);
826  w->SetDirty();
827  }
828  return;
829  }
830 
831  /* Scroll the widget attached to the scrollbar. */
832  Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : NULL);
833  if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
834  sb->UpdatePosition(wheel);
835  w->SetDirty();
836  }
837 }
838 
844 static bool MayBeShown(const Window *w)
845 {
846  /* If we're not modal, everything is okay. */
847  if (!HasModalProgress()) return true;
848 
849  switch (w->window_class) {
850  case WC_MAIN_WINDOW:
851  case WC_MODAL_PROGRESS:
853  return true;
854 
855  default:
856  return false;
857  }
858 }
859 
872 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
873 {
874  const Window *v;
876  if (MayBeShown(v) &&
877  right > v->left &&
878  bottom > v->top &&
879  left < v->left + v->width &&
880  top < v->top + v->height) {
881  /* v and rectangle intersect with each other */
882  int x;
883 
884  if (left < (x = v->left)) {
885  DrawOverlappedWindow(w, left, top, x, bottom);
886  DrawOverlappedWindow(w, x, top, right, bottom);
887  return;
888  }
889 
890  if (right > (x = v->left + v->width)) {
891  DrawOverlappedWindow(w, left, top, x, bottom);
892  DrawOverlappedWindow(w, x, top, right, bottom);
893  return;
894  }
895 
896  if (top < (x = v->top)) {
897  DrawOverlappedWindow(w, left, top, right, x);
898  DrawOverlappedWindow(w, left, x, right, bottom);
899  return;
900  }
901 
902  if (bottom > (x = v->top + v->height)) {
903  DrawOverlappedWindow(w, left, top, right, x);
904  DrawOverlappedWindow(w, left, x, right, bottom);
905  return;
906  }
907 
908  return;
909  }
910  }
911 
912  /* Setup blitter, and dispatch a repaint event to window *wz */
913  DrawPixelInfo *dp = _cur_dpi;
914  dp->width = right - left;
915  dp->height = bottom - top;
916  dp->left = left - w->left;
917  dp->top = top - w->top;
918  dp->pitch = _screen.pitch;
919  dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
920  dp->zoom = ZOOM_LVL_NORMAL;
921  w->OnPaint();
922 }
923 
932 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
933 {
934  Window *w;
935  DrawPixelInfo bk;
936  _cur_dpi = &bk;
937 
938  FOR_ALL_WINDOWS_FROM_BACK(w) {
939  if (MayBeShown(w) &&
940  right > w->left &&
941  bottom > w->top &&
942  left < w->left + w->width &&
943  top < w->top + w->height) {
944  /* Window w intersects with the rectangle => needs repaint */
945  DrawOverlappedWindow(w, left, top, right, bottom);
946  }
947  }
948 }
949 
954 void Window::SetDirty() const
955 {
956  SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
957 }
958 
965 void Window::ReInit(int rx, int ry)
966 {
967  this->SetDirty(); // Mark whole current window as dirty.
968 
969  /* Save current size. */
970  int window_width = this->width;
971  int window_height = this->height;
972 
973  this->OnInit();
974  /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
975  this->nested_root->SetupSmallestSize(this, false);
976  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
977  this->width = this->nested_root->smallest_x;
978  this->height = this->nested_root->smallest_y;
979  this->resize.step_width = this->nested_root->resize_x;
980  this->resize.step_height = this->nested_root->resize_y;
981 
982  /* Resize as close to the original size + requested resize as possible. */
983  window_width = max(window_width + rx, this->width);
984  window_height = max(window_height + ry, this->height);
985  int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
986  int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
987  /* dx and dy has to go by step.. calculate it.
988  * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
989  if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
990  if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
991 
992  ResizeWindow(this, dx, dy);
993  /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
994 }
995 
1001 void Window::SetShaded(bool make_shaded)
1002 {
1003  if (this->shade_select == NULL) return;
1004 
1005  int desired = make_shaded ? SZSP_HORIZONTAL : 0;
1006  if (this->shade_select->shown_plane != desired) {
1007  if (make_shaded) {
1008  if (this->nested_focus != NULL) this->UnfocusFocusedWidget();
1009  this->unshaded_size.width = this->width;
1010  this->unshaded_size.height = this->height;
1011  this->shade_select->SetDisplayedPlane(desired);
1012  this->ReInit(0, -this->height);
1013  } else {
1014  this->shade_select->SetDisplayedPlane(desired);
1015  int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
1016  int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
1017  this->ReInit(dx, dy);
1018  }
1019  }
1020 }
1021 
1029 {
1030  Window *v;
1031  FOR_ALL_WINDOWS_FROM_BACK(v) {
1032  if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
1033  }
1034 
1035  return NULL;
1036 }
1037 
1043 {
1044  Window *child = FindChildWindow(this, wc);
1045  while (child != NULL) {
1046  delete child;
1047  child = FindChildWindow(this, wc);
1048  }
1049 }
1050 
1055 {
1056  if (_thd.window_class == this->window_class &&
1057  _thd.window_number == this->window_number) {
1058  ResetObjectToPlace();
1059  }
1060 
1061  /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
1062  if (_mouseover_last_w == this) _mouseover_last_w = NULL;
1063 
1064  /* We can't scroll the window when it's closed. */
1065  if (_last_scroll_window == this) _last_scroll_window = NULL;
1066 
1067  /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
1068  if (_focused_window == this) {
1069  this->OnFocusLost();
1070  _focused_window = NULL;
1071  }
1072 
1073  this->DeleteChildWindows();
1074 
1075  if (this->viewport != NULL) DeleteWindowViewport(this);
1076 
1077  this->SetDirty();
1078 
1079  free(this->nested_array); // Contents is released through deletion of #nested_root.
1080  delete this->nested_root;
1081 
1082  /*
1083  * Make fairly sure that this is written, and not "optimized" away.
1084  * The delete operator is overwritten to not delete it; the deletion
1085  * happens at a later moment in time after the window has been
1086  * removed from the list of windows to prevent issues with items
1087  * being removed during the iteration as not one but more windows
1088  * may be removed by a single call to ~Window by means of the
1089  * DeleteChildWindows function.
1090  */
1091  const_cast<volatile WindowClass &>(this->window_class) = WC_INVALID;
1092 }
1093 
1101 {
1102  Window *w;
1103  FOR_ALL_WINDOWS_FROM_BACK(w) {
1104  if (w->window_class == cls && w->window_number == number) return w;
1105  }
1106 
1107  return NULL;
1108 }
1109 
1117 {
1118  Window *w;
1119  FOR_ALL_WINDOWS_FROM_BACK(w) {
1120  if (w->window_class == cls) return w;
1121  }
1122 
1123  return NULL;
1124 }
1125 
1132 void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
1133 {
1134  Window *w = FindWindowById(cls, number);
1135  if (force || w == NULL ||
1136  (w->flags & WF_STICKY) == 0) {
1137  delete w;
1138  }
1139 }
1140 
1146 {
1147  Window *w;
1148 
1149 restart_search:
1150  /* When we find the window to delete, we need to restart the search
1151  * as deleting this window could cascade in deleting (many) others
1152  * anywhere in the z-array */
1153  FOR_ALL_WINDOWS_FROM_BACK(w) {
1154  if (w->window_class == cls) {
1155  delete w;
1156  goto restart_search;
1157  }
1158  }
1159 }
1160 
1168 {
1169  Window *w;
1170 
1171 restart_search:
1172  /* When we find the window to delete, we need to restart the search
1173  * as deleting this window could cascade in deleting (many) others
1174  * anywhere in the z-array */
1175  FOR_ALL_WINDOWS_FROM_BACK(w) {
1176  if (w->owner == id) {
1177  delete w;
1178  goto restart_search;
1179  }
1180  }
1181 
1182  /* Also delete the company specific windows that don't have a company-colour. */
1184 }
1185 
1193 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1194 {
1195  Window *w;
1196  FOR_ALL_WINDOWS_FROM_BACK(w) {
1197  if (w->owner != old_owner) continue;
1198 
1199  switch (w->window_class) {
1200  case WC_COMPANY_COLOUR:
1201  case WC_FINANCES:
1202  case WC_STATION_LIST:
1203  case WC_TRAINS_LIST:
1204  case WC_ROADVEH_LIST:
1205  case WC_SHIPS_LIST:
1206  case WC_AIRCRAFT_LIST:
1207  case WC_BUY_COMPANY:
1208  case WC_COMPANY:
1210  continue;
1211 
1212  default:
1213  w->owner = new_owner;
1214  break;
1215  }
1216  }
1217 }
1218 
1219 static void BringWindowToFront(Window *w);
1220 
1229 {
1230  Window *w = FindWindowById(cls, number);
1231 
1232  if (w != NULL) {
1233  if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1234 
1235  w->SetWhiteBorder();
1236  BringWindowToFront(w);
1237  w->SetDirty();
1238  }
1239 
1240  return w;
1241 }
1242 
1243 static inline bool IsVitalWindow(const Window *w)
1244 {
1245  switch (w->window_class) {
1246  case WC_MAIN_TOOLBAR:
1247  case WC_STATUS_BAR:
1248  case WC_NEWS_WINDOW:
1249  case WC_SEND_NETWORK_MSG:
1250  return true;
1251 
1252  default:
1253  return false;
1254  }
1255 }
1256 
1265 static uint GetWindowZPriority(const Window *w)
1266 {
1267  assert(w->window_class != WC_INVALID);
1268 
1269  uint z_priority = 0;
1270 
1271  switch (w->window_class) {
1272  case WC_ENDSCREEN:
1273  ++z_priority;
1274 
1275  case WC_HIGHSCORE:
1276  ++z_priority;
1277 
1278  case WC_TOOLTIPS:
1279  ++z_priority;
1280 
1281  case WC_DROPDOWN_MENU:
1282  ++z_priority;
1283 
1284  case WC_MAIN_TOOLBAR:
1285  case WC_STATUS_BAR:
1286  ++z_priority;
1287 
1288  case WC_OSK:
1289  ++z_priority;
1290 
1291  case WC_QUERY_STRING:
1292  case WC_SEND_NETWORK_MSG:
1293  ++z_priority;
1294 
1295  case WC_ERRMSG:
1297  case WC_MODAL_PROGRESS:
1299  case WC_SAVE_PRESET:
1300  ++z_priority;
1301 
1302  case WC_GENERATE_LANDSCAPE:
1303  case WC_SAVELOAD:
1304  case WC_GAME_OPTIONS:
1305  case WC_CUSTOM_CURRENCY:
1306  case WC_NETWORK_WINDOW:
1307  case WC_GRF_PARAMETERS:
1308  case WC_AI_LIST:
1309  case WC_AI_SETTINGS:
1310  case WC_TEXTFILE:
1311  ++z_priority;
1312 
1313  case WC_CONSOLE:
1314  ++z_priority;
1315 
1316  case WC_NEWS_WINDOW:
1317  ++z_priority;
1318 
1319  default:
1320  ++z_priority;
1321 
1322  case WC_MAIN_WINDOW:
1323  return z_priority;
1324  }
1325 }
1326 
1332 {
1333  assert(w->z_front == NULL && w->z_back == NULL);
1334 
1335  if (_z_front_window == NULL) {
1336  /* It's the only window. */
1337  _z_front_window = _z_back_window = w;
1338  w->z_front = w->z_back = NULL;
1339  } else {
1340  /* Search down the z-ordering for its location. */
1341  Window *v = _z_front_window;
1342  uint last_z_priority = UINT_MAX;
1343  while (v != NULL && (v->window_class == WC_INVALID || GetWindowZPriority(v) > GetWindowZPriority(w))) {
1344  if (v->window_class != WC_INVALID) {
1345  /* Sanity check z-ordering, while we're at it. */
1346  assert(last_z_priority >= GetWindowZPriority(v));
1347  last_z_priority = GetWindowZPriority(v);
1348  }
1349 
1350  v = v->z_back;
1351  }
1352 
1353  if (v == NULL) {
1354  /* It's the new back window. */
1355  w->z_front = _z_back_window;
1356  w->z_back = NULL;
1357  _z_back_window->z_back = w;
1358  _z_back_window = w;
1359  } else if (v == _z_front_window) {
1360  /* It's the new front window. */
1361  w->z_front = NULL;
1362  w->z_back = _z_front_window;
1363  _z_front_window->z_front = w;
1364  _z_front_window = w;
1365  } else {
1366  /* It's somewhere else in the z-ordering. */
1367  w->z_front = v->z_front;
1368  w->z_back = v;
1369  v->z_front->z_back = w;
1370  v->z_front = w;
1371  }
1372  }
1373 }
1374 
1375 
1381 {
1382  if (w->z_front == NULL) {
1383  assert(_z_front_window == w);
1384  _z_front_window = w->z_back;
1385  } else {
1386  w->z_front->z_back = w->z_back;
1387  }
1388 
1389  if (w->z_back == NULL) {
1390  assert(_z_back_window == w);
1391  _z_back_window = w->z_front;
1392  } else {
1393  w->z_back->z_front = w->z_front;
1394  }
1395 
1396  w->z_front = w->z_back = NULL;
1397 }
1398 
1405 {
1408 
1409  w->SetDirty();
1410 }
1411 
1421 {
1422  /* Set up window properties; some of them are needed to set up smallest size below */
1423  this->window_class = this->window_desc->cls;
1424  this->SetWhiteBorder();
1425  if (this->window_desc->default_pos == WDP_CENTER) this->flags |= WF_CENTERED;
1426  this->owner = INVALID_OWNER;
1427  this->nested_focus = NULL;
1428  this->window_number = window_number;
1429 
1430  this->OnInit();
1431  /* Initialize nested widget tree. */
1432  if (this->nested_array == NULL) {
1433  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1434  this->nested_root->SetupSmallestSize(this, true);
1435  } else {
1436  this->nested_root->SetupSmallestSize(this, false);
1437  }
1438  /* Initialize to smallest size. */
1439  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1440 
1441  /* Further set up window properties,
1442  * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1443  this->resize.step_width = this->nested_root->resize_x;
1444  this->resize.step_height = this->nested_root->resize_y;
1445 
1446  /* Give focus to the opened window unless a text box
1447  * of focused window has focus (so we don't interrupt typing). But if the new
1448  * window has a text box, then take focus anyway. */
1450 
1451  /* Insert the window into the correct location in the z-ordering. */
1452  AddWindowToZOrdering(this);
1453 }
1454 
1462 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1463 {
1464  this->left = x;
1465  this->top = y;
1466  this->width = sm_width;
1467  this->height = sm_height;
1468 }
1469 
1480 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
1481 {
1482  def_width = max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1483  def_height = max(def_height, this->height);
1484  /* Try to make windows smaller when our window is too small.
1485  * w->(width|height) is normally the same as min_(width|height),
1486  * but this way the GUIs can be made a little more dynamic;
1487  * one can use the same spec for multiple windows and those
1488  * can then determine the real minimum size of the window. */
1489  if (this->width != def_width || this->height != def_height) {
1490  /* Think about the overlapping toolbars when determining the minimum window size */
1491  int free_height = _screen.height;
1492  const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1493  if (wt != NULL) free_height -= wt->height;
1495  if (wt != NULL) free_height -= wt->height;
1496 
1497  int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
1498  int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
1499 
1500  /* X and Y has to go by step.. calculate it.
1501  * The cast to int is necessary else x/y are implicitly casted to
1502  * unsigned int, which won't work. */
1503  if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1504  if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1505 
1506  ResizeWindow(this, enlarge_x, enlarge_y);
1507  /* ResizeWindow() calls this->OnResize(). */
1508  } else {
1509  /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1510  this->OnResize();
1511  }
1512 
1513  int nx = this->left;
1514  int ny = this->top;
1515 
1516  if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1517 
1518  const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1519  ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
1520  nx = max(nx, 0);
1521 
1522  if (this->viewport != NULL) {
1523  this->viewport->left += nx - this->left;
1524  this->viewport->top += ny - this->top;
1525  }
1526  this->left = nx;
1527  this->top = ny;
1528 
1529  this->SetDirty();
1530 }
1531 
1543 static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos)
1544 {
1545  int right = width + left;
1546  int bottom = height + top;
1547 
1548  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1549  if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height) return false;
1550 
1551  /* Make sure it is not obscured by any window. */
1552  const Window *w;
1553  FOR_ALL_WINDOWS_FROM_BACK(w) {
1554  if (w->window_class == WC_MAIN_WINDOW) continue;
1555 
1556  if (right > w->left &&
1557  w->left + w->width > left &&
1558  bottom > w->top &&
1559  w->top + w->height > top) {
1560  return false;
1561  }
1562  }
1563 
1564  pos.x = left;
1565  pos.y = top;
1566  return true;
1567 }
1568 
1580 static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos)
1581 {
1582  /* Left part of the rectangle may be at most 1/4 off-screen,
1583  * right part of the rectangle may be at most 1/2 off-screen
1584  */
1585  if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1586  /* Bottom part of the rectangle may be at most 1/4 off-screen */
1587  if (top < 22 || top > _screen.height - (height >> 2)) return false;
1588 
1589  /* Make sure it is not obscured by any window. */
1590  const Window *w;
1591  FOR_ALL_WINDOWS_FROM_BACK(w) {
1592  if (w->window_class == WC_MAIN_WINDOW) continue;
1593 
1594  if (left + width > w->left &&
1595  w->left + w->width > left &&
1596  top + height > w->top &&
1597  w->top + w->height > top) {
1598  return false;
1599  }
1600  }
1601 
1602  pos.x = left;
1603  pos.y = top;
1604  return true;
1605 }
1606 
1613 static Point GetAutoPlacePosition(int width, int height)
1614 {
1615  Point pt;
1616 
1617  /* First attempt, try top-left of the screen */
1618  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1619  if (IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->height + 2 : 2, width, height, pt)) return pt;
1620 
1621  /* Second attempt, try around all existing windows with a distance of 2 pixels.
1622  * The new window must be entirely on-screen, and not overlap with an existing window.
1623  * Eight starting points are tried, two at each corner.
1624  */
1625  const Window *w;
1626  FOR_ALL_WINDOWS_FROM_BACK(w) {
1627  if (w->window_class == WC_MAIN_WINDOW) continue;
1628 
1629  if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt;
1630  if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt;
1631  if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt;
1632  if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt;
1633  if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt;
1634  if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt;
1635  if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt;
1636  if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt;
1637  }
1638 
1639  /* Third attempt, try around all existing windows with a distance of 2 pixels.
1640  * The new window may be partly off-screen, and must not overlap with an existing window.
1641  * Only four starting points are tried.
1642  */
1643  FOR_ALL_WINDOWS_FROM_BACK(w) {
1644  if (w->window_class == WC_MAIN_WINDOW) continue;
1645 
1646  if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt;
1647  if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt;
1648  if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt;
1649  if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt;
1650  }
1651 
1652  /* Fourth and final attempt, put window at diagonal starting from (0, 24), try multiples
1653  * of (+5, +5)
1654  */
1655  int left = 0, top = 24;
1656 
1657 restart:
1658  FOR_ALL_WINDOWS_FROM_BACK(w) {
1659  if (w->left == left && w->top == top) {
1660  left += 5;
1661  top += 5;
1662  goto restart;
1663  }
1664  }
1665 
1666  pt.x = left;
1667  pt.y = top;
1668  return pt;
1669 }
1670 
1678 {
1679  const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1680  assert(w != NULL);
1681  Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1682  return pt;
1683 }
1684 
1702 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1703 {
1704  Point pt;
1705  const Window *w;
1706 
1707  int16 default_width = max(desc->GetDefaultWidth(), sm_width);
1708  int16 default_height = max(desc->GetDefaultHeight(), sm_height);
1709 
1710  if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ &&
1711  (w = FindWindowById(desc->parent_cls, window_number)) != NULL &&
1712  w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) {
1713 
1714  pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10);
1715  if (pt.x > _screen.width + 10 - default_width) {
1716  pt.x = (_screen.width + 10 - default_width) - 20;
1717  }
1718  pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? w->height : 10);
1719  return pt;
1720  }
1721 
1722  switch (desc->default_pos) {
1723  case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1724  return GetToolbarAlignedWindowPosition(default_width);
1725 
1726  case WDP_AUTO: // Find a good automatic position for the window
1727  return GetAutoPlacePosition(default_width, default_height);
1728 
1729  case WDP_CENTER: // Centre the window horizontally
1730  pt.x = (_screen.width - default_width) / 2;
1731  pt.y = (_screen.height - default_height) / 2;
1732  break;
1733 
1734  case WDP_MANUAL:
1735  pt.x = 0;
1736  pt.y = 0;
1737  break;
1738 
1739  default:
1740  NOT_REACHED();
1741  }
1742 
1743  return pt;
1744 }
1745 
1746 /* virtual */ Point Window::OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1747 {
1748  return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1749 }
1750 
1758 void Window::CreateNestedTree(bool fill_nested)
1759 {
1760  int biggest_index = -1;
1761  this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_parts, this->window_desc->nwid_length, &biggest_index, &this->shade_select);
1762  this->nested_array_size = (uint)(biggest_index + 1);
1763 
1764  if (fill_nested) {
1765  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1766  this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
1767  }
1768 }
1769 
1775 {
1776  this->InitializeData(window_number);
1777  this->ApplyDefaults();
1778  Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1779  this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1781 }
1782 
1788 {
1789  this->CreateNestedTree(false);
1790  this->FinishInitNested(window_number);
1791 }
1792 
1797 Window::Window(WindowDesc *desc) : window_desc(desc), scrolling_scrollbar(-1)
1798 {
1799 }
1800 
1808 Window *FindWindowFromPt(int x, int y)
1809 {
1810  Window *w;
1811  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1812  if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1813  return w;
1814  }
1815  }
1816 
1817  return NULL;
1818 }
1819 
1824 {
1825  IConsoleClose();
1826 
1827  _z_back_window = NULL;
1828  _z_front_window = NULL;
1829  _focused_window = NULL;
1830  _mouseover_last_w = NULL;
1831  _last_scroll_window = NULL;
1832  _scrolling_viewport = false;
1833  _mouse_hovering = false;
1834 
1835  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1836  NWidgetScrollbar::InvalidateDimensionCache();
1837 
1838  ShowFirstError();
1839 }
1840 
1845 {
1847 
1848  Window *w;
1849  FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
1850 
1851  for (w = _z_front_window; w != NULL; /* nothing */) {
1852  Window *to_del = w;
1853  w = w->z_back;
1854  free(to_del);
1855  }
1856 
1857  _z_front_window = NULL;
1858  _z_back_window = NULL;
1859 }
1860 
1865 {
1867  InitWindowSystem();
1868  _thd.Reset();
1869 }
1870 
1871 static void DecreaseWindowCounters()
1872 {
1873  Window *w;
1874  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1875  if (_scroller_click_timeout == 0) {
1876  /* Unclick scrollbar buttons if they are pressed. */
1877  for (uint i = 0; i < w->nested_array_size; i++) {
1878  NWidgetBase *nwid = w->nested_array[i];
1879  if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
1880  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1883  w->scrolling_scrollbar = -1;
1884  sb->SetDirty(w);
1885  }
1886  }
1887  }
1888  }
1889 
1890  /* Handle editboxes */
1891  for (SmallMap<int, QueryString*>::Pair *it = w->querystrings.Begin(); it != w->querystrings.End(); ++it) {
1892  it->second->HandleEditBox(w, it->first);
1893  }
1894 
1895  w->OnMouseLoop();
1896  }
1897 
1898  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1899  if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) {
1900  CLRBITS(w->flags, WF_TIMEOUT);
1901 
1902  w->OnTimeout();
1903  w->RaiseButtons(true);
1904  }
1905  }
1906 }
1907 
1908 static void HandlePlacePresize()
1909 {
1910  if (_special_mouse_mode != WSM_PRESIZE) return;
1911 
1912  Window *w = _thd.GetCallbackWnd();
1913  if (w == NULL) return;
1914 
1915  Point pt = GetTileBelowCursor();
1916  if (pt.x == -1) {
1917  _thd.selend.x = -1;
1918  return;
1919  }
1920 
1921  w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1922 }
1923 
1929 {
1931 
1932  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
1933 
1934  Window *w = _thd.GetCallbackWnd();
1935  if (w != NULL) {
1936  /* Send an event in client coordinates. */
1937  Point pt;
1938  pt.x = _cursor.pos.x - w->left;
1939  pt.y = _cursor.pos.y - w->top;
1940  if (_left_button_down) {
1941  w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
1942  } else {
1943  w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
1944  }
1945  }
1946 
1947  if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
1948  return ES_HANDLED;
1949 }
1950 
1952 static void HandleMouseOver()
1953 {
1954  Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
1955 
1956  /* We changed window, put a MOUSEOVER event to the last window */
1957  if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
1958  /* Reset mouse-over coordinates of previous window */
1959  Point pt = { -1, -1 };
1960  _mouseover_last_w->OnMouseOver(pt, 0);
1961  }
1962 
1963  /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
1964  _mouseover_last_w = w;
1965 
1966  if (w != NULL) {
1967  /* send an event in client coordinates. */
1968  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
1969  const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
1970  if (widget != NULL) w->OnMouseOver(pt, widget->index);
1971  }
1972 }
1973 
1975 static const int MIN_VISIBLE_TITLE_BAR = 13;
1976 
1981 };
1982 
1993 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
1994 {
1995  if (v == NULL) return;
1996 
1997  int v_bottom = v->top + v->height;
1998  int v_right = v->left + v->width;
1999  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.
2000 
2001  if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
2002  if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
2003 
2004  /* Vertically, the rectangle is hidden behind v. */
2005  if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
2006  if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
2007  return;
2008  }
2009  if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
2010  if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
2011  return;
2012  }
2013 
2014  /* Horizontally also hidden, force movement to a safe area. */
2015  if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
2016  *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
2017  } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
2018  *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2019  } else {
2020  *ny = safe_y;
2021  }
2022 }
2023 
2031 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2032 {
2033  /* Search for the title bar rectangle. */
2034  Rect caption_rect;
2035  const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2036  if (caption != NULL) {
2037  caption_rect.left = caption->pos_x;
2038  caption_rect.right = caption->pos_x + caption->current_x;
2039  caption_rect.top = caption->pos_y;
2040  caption_rect.bottom = caption->pos_y + caption->current_y;
2041 
2042  /* Make sure the window doesn't leave the screen */
2043  nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2044  ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2045 
2046  /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2047  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2048  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2049  }
2050 
2051  if (w->viewport != NULL) {
2052  w->viewport->left += nx - w->left;
2053  w->viewport->top += ny - w->top;
2054  }
2055 
2056  w->left = nx;
2057  w->top = ny;
2058 }
2059 
2070 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
2071 {
2072  if (delta_x != 0 || delta_y != 0) {
2073  if (clamp_to_screen) {
2074  /* Determine the new right/bottom position. If that is outside of the bounds of
2075  * the resolution clamp it in such a manner that it stays within the bounds. */
2076  int new_right = w->left + w->width + delta_x;
2077  int new_bottom = w->top + w->height + delta_y;
2078  if (new_right >= (int)_cur_resolution.width) delta_x -= Ceil(new_right - _cur_resolution.width, max(1U, w->nested_root->resize_x));
2079  if (new_bottom >= (int)_cur_resolution.height) delta_y -= Ceil(new_bottom - _cur_resolution.height, max(1U, w->nested_root->resize_y));
2080  }
2081 
2082  w->SetDirty();
2083 
2084  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);
2085  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);
2086  assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2087  assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2088 
2090  w->width = w->nested_root->current_x;
2091  w->height = w->nested_root->current_y;
2092  }
2093 
2094  EnsureVisibleCaption(w, w->left, w->top);
2095 
2096  /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
2097  w->OnResize();
2098  w->SetDirty();
2099 }
2100 
2107 {
2109  return (w == NULL) ? 0 : w->top + w->height;
2110 }
2111 
2118 {
2120  return (w == NULL) ? _screen.height : w->top;
2121 }
2122 
2123 static bool _dragging_window;
2124 
2130 {
2131  /* Get out immediately if no window is being dragged at all. */
2132  if (!_dragging_window) return ES_NOT_HANDLED;
2133 
2134  /* If button still down, but cursor hasn't moved, there is nothing to do. */
2135  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2136 
2137  /* Otherwise find the window... */
2138  Window *w;
2139  FOR_ALL_WINDOWS_FROM_BACK(w) {
2140  if (w->flags & WF_DRAGGING) {
2141  /* Stop the dragging if the left mouse button was released */
2142  if (!_left_button_down) {
2143  w->flags &= ~WF_DRAGGING;
2144  break;
2145  }
2146 
2147  w->SetDirty();
2148 
2149  int x = _cursor.pos.x + _drag_delta.x;
2150  int y = _cursor.pos.y + _drag_delta.y;
2151  int nx = x;
2152  int ny = y;
2153 
2155  const Window *v;
2156 
2159  int delta;
2160 
2161  FOR_ALL_WINDOWS_FROM_BACK(v) {
2162  if (v == w) continue; // Don't snap at yourself
2163 
2164  if (y + w->height > v->top && y < v->top + v->height) {
2165  /* Your left border <-> other right border */
2166  delta = abs(v->left + v->width - x);
2167  if (delta <= hsnap) {
2168  nx = v->left + v->width;
2169  hsnap = delta;
2170  }
2171 
2172  /* Your right border <-> other left border */
2173  delta = abs(v->left - x - w->width);
2174  if (delta <= hsnap) {
2175  nx = v->left - w->width;
2176  hsnap = delta;
2177  }
2178  }
2179 
2180  if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2181  /* Your left border <-> other left border */
2182  delta = abs(v->left - x);
2183  if (delta <= hsnap) {
2184  nx = v->left;
2185  hsnap = delta;
2186  }
2187 
2188  /* Your right border <-> other right border */
2189  delta = abs(v->left + v->width - x - w->width);
2190  if (delta <= hsnap) {
2191  nx = v->left + v->width - w->width;
2192  hsnap = delta;
2193  }
2194  }
2195 
2196  if (x + w->width > v->left && x < v->left + v->width) {
2197  /* Your top border <-> other bottom border */
2198  delta = abs(v->top + v->height - y);
2199  if (delta <= vsnap) {
2200  ny = v->top + v->height;
2201  vsnap = delta;
2202  }
2203 
2204  /* Your bottom border <-> other top border */
2205  delta = abs(v->top - y - w->height);
2206  if (delta <= vsnap) {
2207  ny = v->top - w->height;
2208  vsnap = delta;
2209  }
2210  }
2211 
2212  if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2213  /* Your top border <-> other top border */
2214  delta = abs(v->top - y);
2215  if (delta <= vsnap) {
2216  ny = v->top;
2217  vsnap = delta;
2218  }
2219 
2220  /* Your bottom border <-> other bottom border */
2221  delta = abs(v->top + v->height - y - w->height);
2222  if (delta <= vsnap) {
2223  ny = v->top + v->height - w->height;
2224  vsnap = delta;
2225  }
2226  }
2227  }
2228  }
2229 
2230  EnsureVisibleCaption(w, nx, ny);
2231 
2232  w->SetDirty();
2233  return ES_HANDLED;
2234  } else if (w->flags & WF_SIZING) {
2235  /* Stop the sizing if the left mouse button was released */
2236  if (!_left_button_down) {
2237  w->flags &= ~WF_SIZING;
2238  w->SetDirty();
2239  break;
2240  }
2241 
2242  /* Compute difference in pixels between cursor position and reference point in the window.
2243  * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2244  */
2245  int x, y = _cursor.pos.y - _drag_delta.y;
2246  if (w->flags & WF_SIZING_LEFT) {
2247  x = _drag_delta.x - _cursor.pos.x;
2248  } else {
2249  x = _cursor.pos.x - _drag_delta.x;
2250  }
2251 
2252  /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2253  if (w->resize.step_width == 0) x = 0;
2254  if (w->resize.step_height == 0) y = 0;
2255 
2256  /* Check the resize button won't go past the bottom of the screen */
2257  if (w->top + w->height + y > _screen.height) {
2258  y = _screen.height - w->height - w->top;
2259  }
2260 
2261  /* X and Y has to go by step.. calculate it.
2262  * The cast to int is necessary else x/y are implicitly casted to
2263  * unsigned int, which won't work. */
2264  if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2265  if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2266 
2267  /* Check that we don't go below the minimum set size */
2268  if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2269  x = w->nested_root->smallest_x - w->width;
2270  }
2271  if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2272  y = w->nested_root->smallest_y - w->height;
2273  }
2274 
2275  /* Window already on size */
2276  if (x == 0 && y == 0) return ES_HANDLED;
2277 
2278  /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2279  _drag_delta.y += y;
2280  if ((w->flags & WF_SIZING_LEFT) && x != 0) {
2281  _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2282  w->SetDirty();
2283  w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2284  /* ResizeWindow() below ensures marking new position as dirty. */
2285  } else {
2286  _drag_delta.x += x;
2287  }
2288 
2289  /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
2290  ResizeWindow(w, x, y);
2291  return ES_HANDLED;
2292  }
2293  }
2294 
2295  _dragging_window = false;
2296  return ES_HANDLED;
2297 }
2298 
2303 static void StartWindowDrag(Window *w)
2304 {
2305  w->flags |= WF_DRAGGING;
2306  w->flags &= ~WF_CENTERED;
2307  _dragging_window = true;
2308 
2309  _drag_delta.x = w->left - _cursor.pos.x;
2310  _drag_delta.y = w->top - _cursor.pos.y;
2311 
2312  BringWindowToFront(w);
2314 }
2315 
2321 static void StartWindowSizing(Window *w, bool to_left)
2322 {
2323  w->flags |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
2324  w->flags &= ~WF_CENTERED;
2325  _dragging_window = true;
2326 
2327  _drag_delta.x = _cursor.pos.x;
2328  _drag_delta.y = _cursor.pos.y;
2329 
2330  BringWindowToFront(w);
2332 }
2333 
2339 {
2340  Window *w;
2341  FOR_ALL_WINDOWS_FROM_BACK(w) {
2342  if (w->scrolling_scrollbar >= 0) {
2343  /* Abort if no button is clicked any more. */
2344  if (!_left_button_down) {
2345  w->scrolling_scrollbar = -1;
2346  w->SetDirty();
2347  return ES_HANDLED;
2348  }
2349 
2350  int i;
2352  bool rtl = false;
2353 
2354  if (sb->type == NWID_HSCROLLBAR) {
2355  i = _cursor.pos.x - _cursorpos_drag_start.x;
2356  rtl = _current_text_dir == TD_RTL;
2357  } else {
2358  i = _cursor.pos.y - _cursorpos_drag_start.y;
2359  }
2360 
2361  if (sb->disp_flags & ND_SCROLLBAR_BTN) {
2362  if (_scroller_click_timeout == 1) {
2363  _scroller_click_timeout = 3;
2364  sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
2365  w->SetDirty();
2366  }
2367  return ES_HANDLED;
2368  }
2369 
2370  /* Find the item we want to move to and make sure it's inside bounds. */
2371  int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
2372  if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
2373  if (pos != sb->GetPosition()) {
2374  sb->SetPosition(pos);
2375  w->SetDirty();
2376  }
2377  return ES_HANDLED;
2378  }
2379  }
2380 
2381  return ES_NOT_HANDLED;
2382 }
2383 
2389 {
2390  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2391 
2392  if (!_scrolling_viewport) return ES_NOT_HANDLED;
2393 
2394  /* When we don't have a last scroll window we are starting to scroll.
2395  * When the last scroll window and this are not the same we went
2396  * outside of the window and should not left-mouse scroll anymore. */
2397  if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2398 
2399  if (_last_scroll_window == NULL || !(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down))) {
2400  _cursor.fix_at = false;
2401  _scrolling_viewport = false;
2402  _last_scroll_window = NULL;
2403  return ES_NOT_HANDLED;
2404  }
2405 
2406  if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
2407  /* If the main window is following a vehicle, then first let go of it! */
2408  const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2409  ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2410  return ES_NOT_HANDLED;
2411  }
2412 
2413  Point delta;
2415  delta.x = -_cursor.delta.x;
2416  delta.y = -_cursor.delta.y;
2417  } else {
2418  delta.x = _cursor.delta.x;
2419  delta.y = _cursor.delta.y;
2420  }
2421 
2422  if (scrollwheel_scrolling) {
2423  /* We are using scrollwheels for scrolling */
2424  delta.x = _cursor.h_wheel;
2425  delta.y = _cursor.v_wheel;
2426  _cursor.v_wheel = 0;
2427  _cursor.h_wheel = 0;
2428  }
2429 
2430  /* Create a scroll-event and send it to the window */
2431  if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2432 
2433  _cursor.delta.x = 0;
2434  _cursor.delta.y = 0;
2435  return ES_HANDLED;
2436 }
2437 
2449 {
2450  bool bring_to_front = false;
2451 
2452  if (w->window_class == WC_MAIN_WINDOW ||
2453  IsVitalWindow(w) ||
2454  w->window_class == WC_TOOLTIPS ||
2456  return true;
2457  }
2458 
2459  /* Use unshaded window size rather than current size for shaded windows. */
2460  int w_width = w->width;
2461  int w_height = w->height;
2462  if (w->IsShaded()) {
2463  w_width = w->unshaded_size.width;
2464  w_height = w->unshaded_size.height;
2465  }
2466 
2467  Window *u;
2469  /* A modal child will prevent the activation of the parent window */
2470  if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) {
2471  u->SetWhiteBorder();
2472  u->SetDirty();
2473  return false;
2474  }
2475 
2476  if (u->window_class == WC_MAIN_WINDOW ||
2477  IsVitalWindow(u) ||
2478  u->window_class == WC_TOOLTIPS ||
2480  continue;
2481  }
2482 
2483  /* Window sizes don't interfere, leave z-order alone */
2484  if (w->left + w_width <= u->left ||
2485  u->left + u->width <= w->left ||
2486  w->top + w_height <= u->top ||
2487  u->top + u->height <= w->top) {
2488  continue;
2489  }
2490 
2491  bring_to_front = true;
2492  }
2493 
2494  if (bring_to_front) BringWindowToFront(w);
2495  return true;
2496 }
2497 
2506 EventState Window::HandleEditBoxKey(int wid, WChar key, uint16 keycode)
2507 {
2508  QueryString *query = this->GetQueryString(wid);
2509  if (query == NULL) return ES_NOT_HANDLED;
2510 
2511  int action = QueryString::ACTION_NOTHING;
2512 
2513  switch (query->text.HandleKeyPress(key, keycode)) {
2514  case HKPR_EDITING:
2515  this->SetWidgetDirty(wid);
2516  this->OnEditboxChanged(wid);
2517  break;
2518 
2519  case HKPR_CURSOR:
2520  this->SetWidgetDirty(wid);
2521  /* For the OSK also invalidate the parent window */
2522  if (this->window_class == WC_OSK) this->InvalidateData();
2523  break;
2524 
2525  case HKPR_CONFIRM:
2526  if (this->window_class == WC_OSK) {
2527  this->OnClick(Point(), WID_OSK_OK, 1);
2528  } else if (query->ok_button >= 0) {
2529  this->OnClick(Point(), query->ok_button, 1);
2530  } else {
2531  action = query->ok_button;
2532  }
2533  break;
2534 
2535  case HKPR_CANCEL:
2536  if (this->window_class == WC_OSK) {
2537  this->OnClick(Point(), WID_OSK_CANCEL, 1);
2538  } else if (query->cancel_button >= 0) {
2539  this->OnClick(Point(), query->cancel_button, 1);
2540  } else {
2541  action = query->cancel_button;
2542  }
2543  break;
2544 
2545  case HKPR_NOT_HANDLED:
2546  return ES_NOT_HANDLED;
2547 
2548  default: break;
2549  }
2550 
2551  switch (action) {
2553  this->UnfocusFocusedWidget();
2554  break;
2555 
2557  if (query->text.bytes <= 1) {
2558  /* If already empty, unfocus instead */
2559  this->UnfocusFocusedWidget();
2560  } else {
2561  query->text.DeleteAll();
2562  this->SetWidgetDirty(wid);
2563  this->OnEditboxChanged(wid);
2564  }
2565  break;
2566 
2567  default:
2568  break;
2569  }
2570 
2571  return ES_HANDLED;
2572 }
2573 
2579 void HandleKeypress(uint keycode, WChar key)
2580 {
2581  /* World generation is multithreaded and messes with companies.
2582  * But there is no company related window open anyway, so _current_company is not used. */
2583  assert(HasModalProgress() || IsLocalCompany());
2584 
2585  /*
2586  * The Unicode standard defines an area called the private use area. Code points in this
2587  * area are reserved for private use and thus not portable between systems. For instance,
2588  * Apple defines code points for the arrow keys in this area, but these are only printable
2589  * on a system running OS X. We don't want these keys to show up in text fields and such,
2590  * and thus we have to clear the unicode character when we encounter such a key.
2591  */
2592  if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2593 
2594  /*
2595  * If both key and keycode is zero, we don't bother to process the event.
2596  */
2597  if (key == 0 && keycode == 0) return;
2598 
2599  /* Check if the focused window has a focused editbox */
2600  if (EditBoxInGlobalFocus()) {
2601  /* All input will in this case go to the focused editbox */
2602  if (_focused_window->window_class == WC_CONSOLE) {
2603  if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2604  } else {
2605  if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return;
2606  }
2607  }
2608 
2609  /* Call the event, start with the uppermost window, but ignore the toolbar. */
2610  Window *w;
2611  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2612  if (w->window_class == WC_MAIN_TOOLBAR) continue;
2613  if (w->window_desc->hotkeys != NULL) {
2614  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2615  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2616  }
2617  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2618  }
2619 
2621  /* When there is no toolbar w is null, check for that */
2622  if (w != NULL) {
2623  if (w->window_desc->hotkeys != NULL) {
2624  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2625  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2626  }
2627  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2628  }
2629 
2630  HandleGlobalHotkeys(key, keycode);
2631 }
2632 
2637 {
2638  /* Call the event, start with the uppermost window. */
2639  Window *w;
2640  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2641  if (w->OnCTRLStateChange() == ES_HANDLED) return;
2642  }
2643 }
2644 
2650 /* virtual */ void Window::InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2651 {
2652  QueryString *query = this->GetQueryString(wid);
2653  if (query == NULL) return;
2654 
2655  if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2656  this->SetWidgetDirty(wid);
2657  this->OnEditboxChanged(wid);
2658  }
2659 }
2660 
2667 void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2668 {
2669  if (!EditBoxInGlobalFocus()) return;
2670 
2671  _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end);
2672 }
2673 
2681 
2686 static void HandleAutoscroll()
2687 {
2688  if (_game_mode == GM_MENU || HasModalProgress()) return;
2690  if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2691 
2692  int x = _cursor.pos.x;
2693  int y = _cursor.pos.y;
2694  Window *w = FindWindowFromPt(x, y);
2695  if (w == NULL || w->flags & WF_DISABLE_VP_SCROLL) return;
2697 
2698  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2699  if (vp == NULL) return;
2700 
2701  x -= vp->left;
2702  y -= vp->top;
2703 
2704  /* here allows scrolling in both x and y axis */
2705 #define scrollspeed 3
2706  if (x - 15 < 0) {
2707  w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
2708  } else if (15 - (vp->width - x) > 0) {
2709  w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
2710  }
2711  if (y - 15 < 0) {
2712  w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
2713  } else if (15 - (vp->height - y) > 0) {
2714  w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
2715  }
2716 #undef scrollspeed
2717 }
2718 
2720  MC_NONE = 0,
2721  MC_LEFT,
2722  MC_RIGHT,
2723  MC_DOUBLE_LEFT,
2724  MC_HOVER,
2725 
2729 };
2731 
2732 static void ScrollMainViewport(int x, int y)
2733 {
2734  if (_game_mode != GM_MENU) {
2736  assert(w);
2737 
2740  }
2741 }
2742 
2752 static const int8 scrollamt[16][2] = {
2753  { 0, 0},
2754  {-2, 0},
2755  { 0, -2},
2756  {-2, -1},
2757  { 2, 0},
2758  { 0, 0},
2759  { 2, -1},
2760  { 0, -2},
2761  { 0, 2},
2762  {-2, 1},
2763  { 0, 0},
2764  {-2, 0},
2765  { 2, 1},
2766  { 0, 2},
2767  { 2, 0},
2768  { 0, 0},
2769 };
2770 
2771 static void HandleKeyScrolling()
2772 {
2773  /*
2774  * Check that any of the dirkeys is pressed and that the focused window
2775  * doesn't have an edit-box as focused widget.
2776  */
2777  if (_dirkeys && !EditBoxInGlobalFocus()) {
2778  int factor = _shift_pressed ? 50 : 10;
2779  ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2780  }
2781 }
2782 
2783 static void MouseLoop(MouseClick click, int mousewheel)
2784 {
2785  /* World generation is multithreaded and messes with companies.
2786  * But there is no company related window open anyway, so _current_company is not used. */
2787  assert(HasModalProgress() || IsLocalCompany());
2788 
2789  HandlePlacePresize();
2791 
2792  if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2793  if (HandleMouseDragDrop() == ES_HANDLED) return;
2794  if (HandleWindowDragging() == ES_HANDLED) return;
2795  if (HandleScrollbarScrolling() == ES_HANDLED) return;
2796  if (HandleViewportScroll() == ES_HANDLED) return;
2797 
2798  HandleMouseOver();
2799 
2800  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2801  if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2802 
2803  int x = _cursor.pos.x;
2804  int y = _cursor.pos.y;
2805  Window *w = FindWindowFromPt(x, y);
2806  if (w == NULL) return;
2807 
2808  if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2809  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2810 
2811  /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2812  if (vp != NULL && (_game_mode == GM_MENU || HasModalProgress())) return;
2813 
2814  if (mousewheel != 0) {
2815  /* Send mousewheel event to window */
2816  w->OnMouseWheel(mousewheel);
2817 
2818  /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2819  if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2820  }
2821 
2822  if (vp != NULL) {
2823  if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button
2824  switch (click) {
2825  case MC_DOUBLE_LEFT:
2826  case MC_LEFT:
2827  DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite);
2828  if (!HandleViewportClicked(vp, x, y) &&
2829  !(w->flags & WF_DISABLE_VP_SCROLL) &&
2831  _scrolling_viewport = true;
2832  _cursor.fix_at = false;
2833  }
2834  break;
2835 
2836  case MC_RIGHT:
2837  if (!(w->flags & WF_DISABLE_VP_SCROLL)) {
2838  _scrolling_viewport = true;
2839  _cursor.fix_at = true;
2840 
2841  /* clear 2D scrolling caches before we start a 2D scroll */
2842  _cursor.h_wheel = 0;
2843  _cursor.v_wheel = 0;
2844  }
2845  break;
2846 
2847  default:
2848  break;
2849  }
2850  } else {
2851  switch (click) {
2852  case MC_LEFT:
2853  case MC_DOUBLE_LEFT:
2854  DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2855  break;
2856 
2857  default:
2858  if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
2859  /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2860  * Simulate a right button click so we can get started. */
2861  /* FALL THROUGH */
2862 
2863  case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
2864 
2865  case MC_HOVER: DispatchHoverEvent(w, x - w->left, y - w->top); break;
2866  }
2867  }
2868 }
2869 
2874 {
2875  /* World generation is multithreaded and messes with companies.
2876  * But there is no company related window open anyway, so _current_company is not used. */
2877  assert(HasModalProgress() || IsLocalCompany());
2878 
2879  static int double_click_time = 0;
2880  static Point double_click_pos = {0, 0};
2881 
2882  /* Mouse event? */
2883  MouseClick click = MC_NONE;
2885  click = MC_LEFT;
2886  if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
2887  double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2888  double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2889  click = MC_DOUBLE_LEFT;
2890  }
2891  double_click_time = _realtime_tick;
2892  double_click_pos = _cursor.pos;
2893  _left_button_clicked = true;
2894  _input_events_this_tick++;
2895  } else if (_right_button_clicked) {
2896  _right_button_clicked = false;
2897  click = MC_RIGHT;
2898  _input_events_this_tick++;
2899  }
2900 
2901  int mousewheel = 0;
2902  if (_cursor.wheel) {
2903  mousewheel = _cursor.wheel;
2904  _cursor.wheel = 0;
2905  _input_events_this_tick++;
2906  }
2907 
2908  static uint32 hover_time = 0;
2909  static Point hover_pos = {0, 0};
2910 
2912  if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
2913  hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
2914  hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
2915  hover_pos = _cursor.pos;
2916  hover_time = _realtime_tick;
2917  _mouse_hovering = false;
2918  } else {
2919  if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
2920  click = MC_HOVER;
2921  _input_events_this_tick++;
2922  _mouse_hovering = true;
2923  }
2924  }
2925  }
2926 
2927  /* Handle sprite picker before any GUI interaction */
2929  /* Next realtime tick? Then redraw has finished */
2930  _newgrf_debug_sprite_picker.mode = SPM_NONE;
2932  }
2933 
2934  if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
2935  /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
2937  _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
2940  _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
2942  } else {
2943  MouseLoop(click, mousewheel);
2944  }
2945 
2946  /* We have moved the mouse the required distance,
2947  * no need to move it at any later time. */
2948  _cursor.delta.x = 0;
2949  _cursor.delta.y = 0;
2950 }
2951 
2955 static void CheckSoftLimit()
2956 {
2957  if (_settings_client.gui.window_soft_limit == 0) return;
2958 
2959  for (;;) {
2960  uint deletable_count = 0;
2961  Window *w, *last_deletable = NULL;
2962  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2963  if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags & WF_STICKY)) continue;
2964 
2965  last_deletable = w;
2966  deletable_count++;
2967  }
2968 
2969  /* We've not reached the soft limit yet. */
2970  if (deletable_count <= _settings_client.gui.window_soft_limit) break;
2971 
2972  assert(last_deletable != NULL);
2973  delete last_deletable;
2974  }
2975 }
2976 
2981 {
2982  /* World generation is multithreaded and messes with companies.
2983  * But there is no company related window open anyway, so _current_company is not used. */
2984  assert(HasModalProgress() || IsLocalCompany());
2985 
2986  CheckSoftLimit();
2987  HandleKeyScrolling();
2988 
2989  /* Do the actual free of the deleted windows. */
2990  for (Window *v = _z_front_window; v != NULL; /* nothing */) {
2991  Window *w = v;
2992  v = v->z_back;
2993 
2994  if (w->window_class != WC_INVALID) continue;
2995 
2997  free(w);
2998  }
2999 
3000  if (_scroller_click_timeout != 0) _scroller_click_timeout--;
3001  DecreaseWindowCounters();
3002 
3003  if (_input_events_this_tick != 0) {
3004  /* The input loop is called only once per GameLoop() - so we can clear the counter here */
3005  _input_events_this_tick = 0;
3006  /* there were some inputs this tick, don't scroll ??? */
3007  return;
3008  }
3009 
3010  /* HandleMouseEvents was already called for this tick */
3012  HandleAutoscroll();
3013 }
3014 
3019 {
3020  Window *w;
3021 
3022  static int highlight_timer = 1;
3023  if (--highlight_timer == 0) {
3024  highlight_timer = 15;
3026  }
3027 
3028  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3031  }
3032 
3033  static int we4_timer = 0;
3034  int t = we4_timer + 1;
3035 
3036  if (t >= 100) {
3037  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3038  w->OnHundredthTick();
3039  }
3040  t = 0;
3041  }
3042  we4_timer = t;
3043 
3044  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3045  if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) {
3047  w->SetDirty();
3048  }
3049  }
3050 
3051  DrawDirtyBlocks();
3052 
3053  FOR_ALL_WINDOWS_FROM_BACK(w) {
3054  /* Update viewport only if window is not shaded. */
3055  if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
3056  }
3058  /* Redraw mouse cursor in case it was hidden */
3059  DrawMouseCursor();
3060 }
3061 
3068 {
3069  const Window *w;
3070  FOR_ALL_WINDOWS_FROM_BACK(w) {
3071  if (w->window_class == cls && w->window_number == number) w->SetDirty();
3072  }
3073 }
3074 
3081 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
3082 {
3083  const Window *w;
3084  FOR_ALL_WINDOWS_FROM_BACK(w) {
3085  if (w->window_class == cls && w->window_number == number) {
3086  w->SetWidgetDirty(widget_index);
3087  }
3088  }
3089 }
3090 
3096 {
3097  Window *w;
3098  FOR_ALL_WINDOWS_FROM_BACK(w) {
3099  if (w->window_class == cls) w->SetDirty();
3100  }
3101 }
3102 
3108 void Window::InvalidateData(int data, bool gui_scope)
3109 {
3110  this->SetDirty();
3111  if (!gui_scope) {
3112  /* Schedule GUI-scope invalidation for next redraw. */
3113  *this->scheduled_invalidation_data.Append() = data;
3114  }
3115  this->OnInvalidateData(data, gui_scope);
3116 }
3117 
3122 {
3123  for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
3124  this->OnInvalidateData(*data, true);
3125  }
3127 }
3128 
3133 {
3134  if ((this->flags & WF_HIGHLIGHTED) == 0) return;
3135 
3136  for (uint i = 0; i < this->nested_array_size; i++) {
3137  if (this->IsWidgetHighlighted(i)) this->SetWidgetDirty(i);
3138  }
3139 }
3140 
3167 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3168 {
3169  Window *w;
3170  FOR_ALL_WINDOWS_FROM_BACK(w) {
3171  if (w->window_class == cls && w->window_number == number) {
3172  w->InvalidateData(data, gui_scope);
3173  }
3174  }
3175 }
3176 
3185 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3186 {
3187  Window *w;
3188 
3189  FOR_ALL_WINDOWS_FROM_BACK(w) {
3190  if (w->window_class == cls) {
3191  w->InvalidateData(data, gui_scope);
3192  }
3193  }
3194 }
3195 
3200 {
3201  Window *w;
3202  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3203  w->OnTick();
3204  }
3205 }
3206 
3214 {
3215  Window *w;
3216 
3217 restart_search:
3218  /* When we find the window to delete, we need to restart the search
3219  * as deleting this window could cascade in deleting (many) others
3220  * anywhere in the z-array */
3221  FOR_ALL_WINDOWS_FROM_BACK(w) {
3222  if (w->window_class != WC_MAIN_WINDOW &&
3223  w->window_class != WC_SELECT_GAME &&
3224  w->window_class != WC_MAIN_TOOLBAR &&
3225  w->window_class != WC_STATUS_BAR &&
3226  w->window_class != WC_TOOLTIPS &&
3227  (w->flags & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
3228 
3229  delete w;
3230  goto restart_search;
3231  }
3232  }
3233 }
3234 
3243 {
3244  Window *w;
3245 
3246  /* Delete every window except for stickied ones, then sticky ones as well */
3248 
3249 restart_search:
3250  /* When we find the window to delete, we need to restart the search
3251  * as deleting this window could cascade in deleting (many) others
3252  * anywhere in the z-array */
3253  FOR_ALL_WINDOWS_FROM_BACK(w) {
3254  if (w->flags & WF_STICKY) {
3255  delete w;
3256  goto restart_search;
3257  }
3258  }
3259 }
3260 
3266 {
3267  Window *w;
3268 
3269 restart_search:
3270  /* When we find the window to delete, we need to restart the search
3271  * as deleting this window could cascade in deleting (many) others
3272  * anywhere in the z-array */
3273  FOR_ALL_WINDOWS_FROM_BACK(w) {
3274  if (w->window_desc->flags & WDF_CONSTRUCTION) {
3275  delete w;
3276  goto restart_search;
3277  }
3278  }
3279 
3280  FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
3281 }
3282 
3285 {
3288 }
3289 
3292 {
3293  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3294  NWidgetScrollbar::InvalidateDimensionCache();
3295 
3296  extern void InitDepotWindowBlockSizes();
3298 
3299  Window *w;
3300  FOR_ALL_WINDOWS_FROM_BACK(w) {
3301  w->ReInit();
3302  }
3303 #ifdef ENABLE_NETWORK
3304  void NetworkReInitChatBoxSize();
3306 #endif
3307 
3308  /* Make sure essential parts of all windows are visible */
3311 }
3312 
3320 static int PositionWindow(Window *w, WindowClass clss, int setting)
3321 {
3322  if (w == NULL || w->window_class != clss) {
3323  w = FindWindowById(clss, 0);
3324  }
3325  if (w == NULL) return 0;
3326 
3327  int old_left = w->left;
3328  switch (setting) {
3329  case 1: w->left = (_screen.width - w->width) / 2; break;
3330  case 2: w->left = _screen.width - w->width; break;
3331  default: w->left = 0; break;
3332  }
3333  if (w->viewport != NULL) w->viewport->left += w->left - old_left;
3334  SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3335  return w->left;
3336 }
3337 
3344 {
3345  DEBUG(misc, 5, "Repositioning Main Toolbar...");
3347 }
3348 
3355 {
3356  DEBUG(misc, 5, "Repositioning statusbar...");
3358 }
3359 
3366 {
3367  DEBUG(misc, 5, "Repositioning news message...");
3369 }
3370 
3377 {
3378  DEBUG(misc, 5, "Repositioning network chat window...");
3380 }
3381 
3382 
3388 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3389 {
3390  Window *w;
3391  FOR_ALL_WINDOWS_FROM_BACK(w) {
3392  if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
3393  w->viewport->follow_vehicle = to_index;
3394  w->SetDirty();
3395  }
3396  }
3397 }
3398 
3399 
3405 void RelocateAllWindows(int neww, int newh)
3406 {
3407  Window *w;
3408 
3409  FOR_ALL_WINDOWS_FROM_BACK(w) {
3410  int left, top;
3411  /* XXX - this probably needs something more sane. For example specifying
3412  * in a 'backup'-desc that the window should always be centered. */
3413  switch (w->window_class) {
3414  case WC_MAIN_WINDOW:
3415  case WC_BOOTSTRAP:
3416  ResizeWindow(w, neww, newh);
3417  continue;
3418 
3419  case WC_MAIN_TOOLBAR:
3420  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3421 
3422  top = w->top;
3423  left = PositionMainToolbar(w); // changes toolbar orientation
3424  break;
3425 
3426  case WC_NEWS_WINDOW:
3427  top = newh - w->height;
3428  left = PositionNewsMessage(w);
3429  break;
3430 
3431  case WC_STATUS_BAR:
3432  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3433 
3434  top = newh - w->height;
3435  left = PositionStatusbar(w);
3436  break;
3437 
3438  case WC_SEND_NETWORK_MSG:
3439  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3440 
3441  top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3442  left = PositionNetworkChatWindow(w);
3443  break;
3444 
3445  case WC_CONSOLE:
3446  IConsoleResize(w);
3447  continue;
3448 
3449  default: {
3450  if (w->flags & WF_CENTERED) {
3451  top = (newh - w->height) >> 1;
3452  left = (neww - w->width) >> 1;
3453  break;
3454  }
3455 
3456  left = w->left;
3457  if (left + (w->width >> 1) >= neww) left = neww - w->width;
3458  if (left < 0) left = 0;
3459 
3460  top = w->top;
3461  if (top + (w->height >> 1) >= newh) top = newh - w->height;
3462  break;
3463  }
3464  }
3465 
3466  EnsureVisibleCaption(w, left, top);
3467  }
3468 }
3469 
3476 {
3477  this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
3478  ResetObjectToPlace();
3479 }