OpenTTD
window.cpp
Go to the documentation of this file.
1 /* $Id: window.cpp 27147 2015-02-13 21:25:48Z frosch $ */
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  this->window_class = WC_INVALID;
1083 }
1084 
1092 {
1093  Window *w;
1094  FOR_ALL_WINDOWS_FROM_BACK(w) {
1095  if (w->window_class == cls && w->window_number == number) return w;
1096  }
1097 
1098  return NULL;
1099 }
1100 
1108 {
1109  Window *w;
1110  FOR_ALL_WINDOWS_FROM_BACK(w) {
1111  if (w->window_class == cls) return w;
1112  }
1113 
1114  return NULL;
1115 }
1116 
1123 void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
1124 {
1125  Window *w = FindWindowById(cls, number);
1126  if (force || w == NULL ||
1127  (w->flags & WF_STICKY) == 0) {
1128  delete w;
1129  }
1130 }
1131 
1137 {
1138  Window *w;
1139 
1140 restart_search:
1141  /* When we find the window to delete, we need to restart the search
1142  * as deleting this window could cascade in deleting (many) others
1143  * anywhere in the z-array */
1144  FOR_ALL_WINDOWS_FROM_BACK(w) {
1145  if (w->window_class == cls) {
1146  delete w;
1147  goto restart_search;
1148  }
1149  }
1150 }
1151 
1159 {
1160  Window *w;
1161 
1162 restart_search:
1163  /* When we find the window to delete, we need to restart the search
1164  * as deleting this window could cascade in deleting (many) others
1165  * anywhere in the z-array */
1166  FOR_ALL_WINDOWS_FROM_BACK(w) {
1167  if (w->owner == id) {
1168  delete w;
1169  goto restart_search;
1170  }
1171  }
1172 
1173  /* Also delete the company specific windows that don't have a company-colour. */
1175 }
1176 
1184 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
1185 {
1186  Window *w;
1187  FOR_ALL_WINDOWS_FROM_BACK(w) {
1188  if (w->owner != old_owner) continue;
1189 
1190  switch (w->window_class) {
1191  case WC_COMPANY_COLOUR:
1192  case WC_FINANCES:
1193  case WC_STATION_LIST:
1194  case WC_TRAINS_LIST:
1195  case WC_ROADVEH_LIST:
1196  case WC_SHIPS_LIST:
1197  case WC_AIRCRAFT_LIST:
1198  case WC_BUY_COMPANY:
1199  case WC_COMPANY:
1201  continue;
1202 
1203  default:
1204  w->owner = new_owner;
1205  break;
1206  }
1207  }
1208 }
1209 
1210 static void BringWindowToFront(Window *w);
1211 
1220 {
1221  Window *w = FindWindowById(cls, number);
1222 
1223  if (w != NULL) {
1224  if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
1225 
1226  w->SetWhiteBorder();
1227  BringWindowToFront(w);
1228  w->SetDirty();
1229  }
1230 
1231  return w;
1232 }
1233 
1234 static inline bool IsVitalWindow(const Window *w)
1235 {
1236  switch (w->window_class) {
1237  case WC_MAIN_TOOLBAR:
1238  case WC_STATUS_BAR:
1239  case WC_NEWS_WINDOW:
1240  case WC_SEND_NETWORK_MSG:
1241  return true;
1242 
1243  default:
1244  return false;
1245  }
1246 }
1247 
1256 static uint GetWindowZPriority(const Window *w)
1257 {
1258  assert(w->window_class != WC_INVALID);
1259 
1260  uint z_priority = 0;
1261 
1262  switch (w->window_class) {
1263  case WC_ENDSCREEN:
1264  ++z_priority;
1265 
1266  case WC_HIGHSCORE:
1267  ++z_priority;
1268 
1269  case WC_TOOLTIPS:
1270  ++z_priority;
1271 
1272  case WC_DROPDOWN_MENU:
1273  ++z_priority;
1274 
1275  case WC_MAIN_TOOLBAR:
1276  case WC_STATUS_BAR:
1277  ++z_priority;
1278 
1279  case WC_OSK:
1280  ++z_priority;
1281 
1282  case WC_QUERY_STRING:
1283  case WC_SEND_NETWORK_MSG:
1284  ++z_priority;
1285 
1286  case WC_ERRMSG:
1288  case WC_MODAL_PROGRESS:
1290  case WC_SAVE_PRESET:
1291  ++z_priority;
1292 
1293  case WC_GENERATE_LANDSCAPE:
1294  case WC_SAVELOAD:
1295  case WC_GAME_OPTIONS:
1296  case WC_CUSTOM_CURRENCY:
1297  case WC_NETWORK_WINDOW:
1298  case WC_GRF_PARAMETERS:
1299  case WC_AI_LIST:
1300  case WC_AI_SETTINGS:
1301  case WC_TEXTFILE:
1302  ++z_priority;
1303 
1304  case WC_CONSOLE:
1305  ++z_priority;
1306 
1307  case WC_NEWS_WINDOW:
1308  ++z_priority;
1309 
1310  default:
1311  ++z_priority;
1312 
1313  case WC_MAIN_WINDOW:
1314  return z_priority;
1315  }
1316 }
1317 
1323 {
1324  assert(w->z_front == NULL && w->z_back == NULL);
1325 
1326  if (_z_front_window == NULL) {
1327  /* It's the only window. */
1328  _z_front_window = _z_back_window = w;
1329  w->z_front = w->z_back = NULL;
1330  } else {
1331  /* Search down the z-ordering for its location. */
1332  Window *v = _z_front_window;
1333  uint last_z_priority = UINT_MAX;
1334  while (v != NULL && (v->window_class == WC_INVALID || GetWindowZPriority(v) > GetWindowZPriority(w))) {
1335  if (v->window_class != WC_INVALID) {
1336  /* Sanity check z-ordering, while we're at it. */
1337  assert(last_z_priority >= GetWindowZPriority(v));
1338  last_z_priority = GetWindowZPriority(v);
1339  }
1340 
1341  v = v->z_back;
1342  }
1343 
1344  if (v == NULL) {
1345  /* It's the new back window. */
1346  w->z_front = _z_back_window;
1347  w->z_back = NULL;
1348  _z_back_window->z_back = w;
1349  _z_back_window = w;
1350  } else if (v == _z_front_window) {
1351  /* It's the new front window. */
1352  w->z_front = NULL;
1353  w->z_back = _z_front_window;
1354  _z_front_window->z_front = w;
1355  _z_front_window = w;
1356  } else {
1357  /* It's somewhere else in the z-ordering. */
1358  w->z_front = v->z_front;
1359  w->z_back = v;
1360  v->z_front->z_back = w;
1361  v->z_front = w;
1362  }
1363  }
1364 }
1365 
1366 
1372 {
1373  if (w->z_front == NULL) {
1374  assert(_z_front_window == w);
1375  _z_front_window = w->z_back;
1376  } else {
1377  w->z_front->z_back = w->z_back;
1378  }
1379 
1380  if (w->z_back == NULL) {
1381  assert(_z_back_window == w);
1382  _z_back_window = w->z_front;
1383  } else {
1384  w->z_back->z_front = w->z_front;
1385  }
1386 
1387  w->z_front = w->z_back = NULL;
1388 }
1389 
1396 {
1399 
1400  w->SetDirty();
1401 }
1402 
1412 {
1413  /* Set up window properties; some of them are needed to set up smallest size below */
1414  this->window_class = this->window_desc->cls;
1415  this->SetWhiteBorder();
1416  if (this->window_desc->default_pos == WDP_CENTER) this->flags |= WF_CENTERED;
1417  this->owner = INVALID_OWNER;
1418  this->nested_focus = NULL;
1419  this->window_number = window_number;
1420 
1421  this->OnInit();
1422  /* Initialize nested widget tree. */
1423  if (this->nested_array == NULL) {
1424  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1425  this->nested_root->SetupSmallestSize(this, true);
1426  } else {
1427  this->nested_root->SetupSmallestSize(this, false);
1428  }
1429  /* Initialize to smallest size. */
1430  this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
1431 
1432  /* Further set up window properties,
1433  * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
1434  this->resize.step_width = this->nested_root->resize_x;
1435  this->resize.step_height = this->nested_root->resize_y;
1436 
1437  /* Give focus to the opened window unless a text box
1438  * of focused window has focus (so we don't interrupt typing). But if the new
1439  * window has a text box, then take focus anyway. */
1441 
1442  /* Insert the window into the correct location in the z-ordering. */
1443  AddWindowToZOrdering(this);
1444 }
1445 
1453 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
1454 {
1455  this->left = x;
1456  this->top = y;
1457  this->width = sm_width;
1458  this->height = sm_height;
1459 }
1460 
1471 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
1472 {
1473  def_width = max(def_width, this->width); // Don't allow default size to be smaller than smallest size
1474  def_height = max(def_height, this->height);
1475  /* Try to make windows smaller when our window is too small.
1476  * w->(width|height) is normally the same as min_(width|height),
1477  * but this way the GUIs can be made a little more dynamic;
1478  * one can use the same spec for multiple windows and those
1479  * can then determine the real minimum size of the window. */
1480  if (this->width != def_width || this->height != def_height) {
1481  /* Think about the overlapping toolbars when determining the minimum window size */
1482  int free_height = _screen.height;
1483  const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
1484  if (wt != NULL) free_height -= wt->height;
1486  if (wt != NULL) free_height -= wt->height;
1487 
1488  int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
1489  int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
1490 
1491  /* X and Y has to go by step.. calculate it.
1492  * The cast to int is necessary else x/y are implicitly casted to
1493  * unsigned int, which won't work. */
1494  if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
1495  if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
1496 
1497  ResizeWindow(this, enlarge_x, enlarge_y);
1498  /* ResizeWindow() calls this->OnResize(). */
1499  } else {
1500  /* Always call OnResize; that way the scrollbars and matrices get initialized. */
1501  this->OnResize();
1502  }
1503 
1504  int nx = this->left;
1505  int ny = this->top;
1506 
1507  if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
1508 
1509  const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
1510  ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
1511  nx = max(nx, 0);
1512 
1513  if (this->viewport != NULL) {
1514  this->viewport->left += nx - this->left;
1515  this->viewport->top += ny - this->top;
1516  }
1517  this->left = nx;
1518  this->top = ny;
1519 
1520  this->SetDirty();
1521 }
1522 
1534 static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos)
1535 {
1536  int right = width + left;
1537  int bottom = height + top;
1538 
1539  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1540  if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height) return false;
1541 
1542  /* Make sure it is not obscured by any window. */
1543  const Window *w;
1544  FOR_ALL_WINDOWS_FROM_BACK(w) {
1545  if (w->window_class == WC_MAIN_WINDOW) continue;
1546 
1547  if (right > w->left &&
1548  w->left + w->width > left &&
1549  bottom > w->top &&
1550  w->top + w->height > top) {
1551  return false;
1552  }
1553  }
1554 
1555  pos.x = left;
1556  pos.y = top;
1557  return true;
1558 }
1559 
1571 static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos)
1572 {
1573  /* Left part of the rectangle may be at most 1/4 off-screen,
1574  * right part of the rectangle may be at most 1/2 off-screen
1575  */
1576  if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
1577  /* Bottom part of the rectangle may be at most 1/4 off-screen */
1578  if (top < 22 || top > _screen.height - (height >> 2)) return false;
1579 
1580  /* Make sure it is not obscured by any window. */
1581  const Window *w;
1582  FOR_ALL_WINDOWS_FROM_BACK(w) {
1583  if (w->window_class == WC_MAIN_WINDOW) continue;
1584 
1585  if (left + width > w->left &&
1586  w->left + w->width > left &&
1587  top + height > w->top &&
1588  w->top + w->height > top) {
1589  return false;
1590  }
1591  }
1592 
1593  pos.x = left;
1594  pos.y = top;
1595  return true;
1596 }
1597 
1604 static Point GetAutoPlacePosition(int width, int height)
1605 {
1606  Point pt;
1607 
1608  /* First attempt, try top-left of the screen */
1609  const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
1610  if (IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->height + 2 : 2, width, height, pt)) return pt;
1611 
1612  /* Second attempt, try around all existing windows with a distance of 2 pixels.
1613  * The new window must be entirely on-screen, and not overlap with an existing window.
1614  * Eight starting points are tried, two at each corner.
1615  */
1616  const Window *w;
1617  FOR_ALL_WINDOWS_FROM_BACK(w) {
1618  if (w->window_class == WC_MAIN_WINDOW) continue;
1619 
1620  if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt;
1621  if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt;
1622  if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt;
1623  if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt;
1624  if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt;
1625  if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt;
1626  if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt;
1627  if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt;
1628  }
1629 
1630  /* Third attempt, try around all existing windows with a distance of 2 pixels.
1631  * The new window may be partly off-screen, and must not overlap with an existing window.
1632  * Only four starting points are tried.
1633  */
1634  FOR_ALL_WINDOWS_FROM_BACK(w) {
1635  if (w->window_class == WC_MAIN_WINDOW) continue;
1636 
1637  if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt;
1638  if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt;
1639  if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt;
1640  if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt;
1641  }
1642 
1643  /* Fourth and final attempt, put window at diagonal starting from (0, 24), try multiples
1644  * of (+5, +5)
1645  */
1646  int left = 0, top = 24;
1647 
1648 restart:
1649  FOR_ALL_WINDOWS_FROM_BACK(w) {
1650  if (w->left == left && w->top == top) {
1651  left += 5;
1652  top += 5;
1653  goto restart;
1654  }
1655  }
1656 
1657  pt.x = left;
1658  pt.y = top;
1659  return pt;
1660 }
1661 
1669 {
1670  const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
1671  assert(w != NULL);
1672  Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
1673  return pt;
1674 }
1675 
1693 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1694 {
1695  Point pt;
1696  const Window *w;
1697 
1698  int16 default_width = max(desc->GetDefaultWidth(), sm_width);
1699  int16 default_height = max(desc->GetDefaultHeight(), sm_height);
1700 
1701  if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ &&
1702  (w = FindWindowById(desc->parent_cls, window_number)) != NULL &&
1703  w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) {
1704 
1705  pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10);
1706  if (pt.x > _screen.width + 10 - default_width) {
1707  pt.x = (_screen.width + 10 - default_width) - 20;
1708  }
1709  pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? w->height : 10);
1710  return pt;
1711  }
1712 
1713  switch (desc->default_pos) {
1714  case WDP_ALIGN_TOOLBAR: // Align to the toolbar
1715  return GetToolbarAlignedWindowPosition(default_width);
1716 
1717  case WDP_AUTO: // Find a good automatic position for the window
1718  return GetAutoPlacePosition(default_width, default_height);
1719 
1720  case WDP_CENTER: // Centre the window horizontally
1721  pt.x = (_screen.width - default_width) / 2;
1722  pt.y = (_screen.height - default_height) / 2;
1723  break;
1724 
1725  case WDP_MANUAL:
1726  pt.x = 0;
1727  pt.y = 0;
1728  break;
1729 
1730  default:
1731  NOT_REACHED();
1732  }
1733 
1734  return pt;
1735 }
1736 
1737 /* virtual */ Point Window::OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1738 {
1739  return LocalGetWindowPlacement(this->window_desc, sm_width, sm_height, window_number);
1740 }
1741 
1749 void Window::CreateNestedTree(bool fill_nested)
1750 {
1751  int biggest_index = -1;
1752  this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_parts, this->window_desc->nwid_length, &biggest_index, &this->shade_select);
1753  this->nested_array_size = (uint)(biggest_index + 1);
1754 
1755  if (fill_nested) {
1756  this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
1757  this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
1758  }
1759 }
1760 
1766 {
1767  this->InitializeData(window_number);
1768  this->ApplyDefaults();
1769  Point pt = this->OnInitialPosition(this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
1770  this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
1772 }
1773 
1779 {
1780  this->CreateNestedTree(false);
1781  this->FinishInitNested(window_number);
1782 }
1783 
1788 Window::Window(WindowDesc *desc) : window_desc(desc), scrolling_scrollbar(-1)
1789 {
1790 }
1791 
1799 Window *FindWindowFromPt(int x, int y)
1800 {
1801  Window *w;
1802  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1803  if (MayBeShown(w) && IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
1804  return w;
1805  }
1806  }
1807 
1808  return NULL;
1809 }
1810 
1815 {
1816  IConsoleClose();
1817 
1818  _z_back_window = NULL;
1819  _z_front_window = NULL;
1820  _focused_window = NULL;
1821  _mouseover_last_w = NULL;
1822  _last_scroll_window = NULL;
1823  _scrolling_viewport = false;
1824  _mouse_hovering = false;
1825 
1826  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
1827  NWidgetScrollbar::InvalidateDimensionCache();
1828 
1829  ShowFirstError();
1830 }
1831 
1836 {
1838 
1839  Window *w;
1840  FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
1841 
1842  for (w = _z_front_window; w != NULL; /* nothing */) {
1843  Window *to_del = w;
1844  w = w->z_back;
1845  free(to_del);
1846  }
1847 
1848  _z_front_window = NULL;
1849  _z_back_window = NULL;
1850 }
1851 
1856 {
1858  InitWindowSystem();
1859  _thd.Reset();
1860 }
1861 
1862 static void DecreaseWindowCounters()
1863 {
1864  Window *w;
1865  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1866  if (_scroller_click_timeout == 0) {
1867  /* Unclick scrollbar buttons if they are pressed. */
1868  for (uint i = 0; i < w->nested_array_size; i++) {
1869  NWidgetBase *nwid = w->nested_array[i];
1870  if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
1871  NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
1874  w->scrolling_scrollbar = -1;
1875  sb->SetDirty(w);
1876  }
1877  }
1878  }
1879  }
1880 
1881  /* Handle editboxes */
1882  for (SmallMap<int, QueryString*>::Pair *it = w->querystrings.Begin(); it != w->querystrings.End(); ++it) {
1883  it->second->HandleEditBox(w, it->first);
1884  }
1885 
1886  w->OnMouseLoop();
1887  }
1888 
1889  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1890  if ((w->flags & WF_TIMEOUT) && --w->timeout_timer == 0) {
1891  CLRBITS(w->flags, WF_TIMEOUT);
1892 
1893  w->OnTimeout();
1894  w->RaiseButtons(true);
1895  }
1896  }
1897 }
1898 
1899 static void HandlePlacePresize()
1900 {
1901  if (_special_mouse_mode != WSM_PRESIZE) return;
1902 
1903  Window *w = _thd.GetCallbackWnd();
1904  if (w == NULL) return;
1905 
1906  Point pt = GetTileBelowCursor();
1907  if (pt.x == -1) {
1908  _thd.selend.x = -1;
1909  return;
1910  }
1911 
1912  w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
1913 }
1914 
1920 {
1922 
1923  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED; // Dragging, but the mouse did not move.
1924 
1925  Window *w = _thd.GetCallbackWnd();
1926  if (w != NULL) {
1927  /* Send an event in client coordinates. */
1928  Point pt;
1929  pt.x = _cursor.pos.x - w->left;
1930  pt.y = _cursor.pos.y - w->top;
1931  if (_left_button_down) {
1932  w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
1933  } else {
1934  w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
1935  }
1936  }
1937 
1938  if (!_left_button_down) ResetObjectToPlace(); // Button released, finished dragging.
1939  return ES_HANDLED;
1940 }
1941 
1943 static void HandleMouseOver()
1944 {
1945  Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
1946 
1947  /* We changed window, put a MOUSEOVER event to the last window */
1948  if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
1949  /* Reset mouse-over coordinates of previous window */
1950  Point pt = { -1, -1 };
1951  _mouseover_last_w->OnMouseOver(pt, 0);
1952  }
1953 
1954  /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
1955  _mouseover_last_w = w;
1956 
1957  if (w != NULL) {
1958  /* send an event in client coordinates. */
1959  Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
1960  const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
1961  if (widget != NULL) w->OnMouseOver(pt, widget->index);
1962  }
1963 }
1964 
1966 static const int MIN_VISIBLE_TITLE_BAR = 13;
1967 
1972 };
1973 
1984 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
1985 {
1986  if (v == NULL) return;
1987 
1988  int v_bottom = v->top + v->height;
1989  int v_right = v->left + v->width;
1990  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.
1991 
1992  if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
1993  if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
1994 
1995  /* Vertically, the rectangle is hidden behind v. */
1996  if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
1997  if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
1998  return;
1999  }
2000  if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
2001  if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
2002  return;
2003  }
2004 
2005  /* Horizontally also hidden, force movement to a safe area. */
2006  if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
2007  *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
2008  } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
2009  *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2010  } else {
2011  *ny = safe_y;
2012  }
2013 }
2014 
2022 static void EnsureVisibleCaption(Window *w, int nx, int ny)
2023 {
2024  /* Search for the title bar rectangle. */
2025  Rect caption_rect;
2026  const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
2027  if (caption != NULL) {
2028  caption_rect.left = caption->pos_x;
2029  caption_rect.right = caption->pos_x + caption->current_x;
2030  caption_rect.top = caption->pos_y;
2031  caption_rect.bottom = caption->pos_y + caption->current_y;
2032 
2033  /* Make sure the window doesn't leave the screen */
2034  nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2035  ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2036 
2037  /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
2038  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
2039  PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
2040  }
2041 
2042  if (w->viewport != NULL) {
2043  w->viewport->left += nx - w->left;
2044  w->viewport->top += ny - w->top;
2045  }
2046 
2047  w->left = nx;
2048  w->top = ny;
2049 }
2050 
2061 void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
2062 {
2063  if (delta_x != 0 || delta_y != 0) {
2064  if (clamp_to_screen) {
2065  /* Determine the new right/bottom position. If that is outside of the bounds of
2066  * the resolution clamp it in such a manner that it stays within the bounds. */
2067  int new_right = w->left + w->width + delta_x;
2068  int new_bottom = w->top + w->height + delta_y;
2069  if (new_right >= (int)_cur_resolution.width) delta_x -= Ceil(new_right - _cur_resolution.width, max(1U, w->nested_root->resize_x));
2070  if (new_bottom >= (int)_cur_resolution.height) delta_y -= Ceil(new_bottom - _cur_resolution.height, max(1U, w->nested_root->resize_y));
2071  }
2072 
2073  w->SetDirty();
2074 
2075  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);
2076  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);
2077  assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
2078  assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
2079 
2081  w->width = w->nested_root->current_x;
2082  w->height = w->nested_root->current_y;
2083  }
2084 
2085  EnsureVisibleCaption(w, w->left, w->top);
2086 
2087  /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
2088  w->OnResize();
2089  w->SetDirty();
2090 }
2091 
2098 {
2100  return (w == NULL) ? 0 : w->top + w->height;
2101 }
2102 
2109 {
2111  return (w == NULL) ? _screen.height : w->top;
2112 }
2113 
2114 static bool _dragging_window;
2115 
2121 {
2122  /* Get out immediately if no window is being dragged at all. */
2123  if (!_dragging_window) return ES_NOT_HANDLED;
2124 
2125  /* If button still down, but cursor hasn't moved, there is nothing to do. */
2126  if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2127 
2128  /* Otherwise find the window... */
2129  Window *w;
2130  FOR_ALL_WINDOWS_FROM_BACK(w) {
2131  if (w->flags & WF_DRAGGING) {
2132  /* Stop the dragging if the left mouse button was released */
2133  if (!_left_button_down) {
2134  w->flags &= ~WF_DRAGGING;
2135  break;
2136  }
2137 
2138  w->SetDirty();
2139 
2140  int x = _cursor.pos.x + _drag_delta.x;
2141  int y = _cursor.pos.y + _drag_delta.y;
2142  int nx = x;
2143  int ny = y;
2144 
2146  const Window *v;
2147 
2150  int delta;
2151 
2152  FOR_ALL_WINDOWS_FROM_BACK(v) {
2153  if (v == w) continue; // Don't snap at yourself
2154 
2155  if (y + w->height > v->top && y < v->top + v->height) {
2156  /* Your left border <-> other right border */
2157  delta = abs(v->left + v->width - x);
2158  if (delta <= hsnap) {
2159  nx = v->left + v->width;
2160  hsnap = delta;
2161  }
2162 
2163  /* Your right border <-> other left border */
2164  delta = abs(v->left - x - w->width);
2165  if (delta <= hsnap) {
2166  nx = v->left - w->width;
2167  hsnap = delta;
2168  }
2169  }
2170 
2171  if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2172  /* Your left border <-> other left border */
2173  delta = abs(v->left - x);
2174  if (delta <= hsnap) {
2175  nx = v->left;
2176  hsnap = delta;
2177  }
2178 
2179  /* Your right border <-> other right border */
2180  delta = abs(v->left + v->width - x - w->width);
2181  if (delta <= hsnap) {
2182  nx = v->left + v->width - w->width;
2183  hsnap = delta;
2184  }
2185  }
2186 
2187  if (x + w->width > v->left && x < v->left + v->width) {
2188  /* Your top border <-> other bottom border */
2189  delta = abs(v->top + v->height - y);
2190  if (delta <= vsnap) {
2191  ny = v->top + v->height;
2192  vsnap = delta;
2193  }
2194 
2195  /* Your bottom border <-> other top border */
2196  delta = abs(v->top - y - w->height);
2197  if (delta <= vsnap) {
2198  ny = v->top - w->height;
2199  vsnap = delta;
2200  }
2201  }
2202 
2203  if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
2204  /* Your top border <-> other top border */
2205  delta = abs(v->top - y);
2206  if (delta <= vsnap) {
2207  ny = v->top;
2208  vsnap = delta;
2209  }
2210 
2211  /* Your bottom border <-> other bottom border */
2212  delta = abs(v->top + v->height - y - w->height);
2213  if (delta <= vsnap) {
2214  ny = v->top + v->height - w->height;
2215  vsnap = delta;
2216  }
2217  }
2218  }
2219  }
2220 
2221  EnsureVisibleCaption(w, nx, ny);
2222 
2223  w->SetDirty();
2224  return ES_HANDLED;
2225  } else if (w->flags & WF_SIZING) {
2226  /* Stop the sizing if the left mouse button was released */
2227  if (!_left_button_down) {
2228  w->flags &= ~WF_SIZING;
2229  w->SetDirty();
2230  break;
2231  }
2232 
2233  /* Compute difference in pixels between cursor position and reference point in the window.
2234  * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
2235  */
2236  int x, y = _cursor.pos.y - _drag_delta.y;
2237  if (w->flags & WF_SIZING_LEFT) {
2238  x = _drag_delta.x - _cursor.pos.x;
2239  } else {
2240  x = _cursor.pos.x - _drag_delta.x;
2241  }
2242 
2243  /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
2244  if (w->resize.step_width == 0) x = 0;
2245  if (w->resize.step_height == 0) y = 0;
2246 
2247  /* Check the resize button won't go past the bottom of the screen */
2248  if (w->top + w->height + y > _screen.height) {
2249  y = _screen.height - w->height - w->top;
2250  }
2251 
2252  /* X and Y has to go by step.. calculate it.
2253  * The cast to int is necessary else x/y are implicitly casted to
2254  * unsigned int, which won't work. */
2255  if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
2256  if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
2257 
2258  /* Check that we don't go below the minimum set size */
2259  if ((int)w->width + x < (int)w->nested_root->smallest_x) {
2260  x = w->nested_root->smallest_x - w->width;
2261  }
2262  if ((int)w->height + y < (int)w->nested_root->smallest_y) {
2263  y = w->nested_root->smallest_y - w->height;
2264  }
2265 
2266  /* Window already on size */
2267  if (x == 0 && y == 0) return ES_HANDLED;
2268 
2269  /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
2270  _drag_delta.y += y;
2271  if ((w->flags & WF_SIZING_LEFT) && x != 0) {
2272  _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
2273  w->SetDirty();
2274  w->left -= x; // If dragging left edge, move left window edge in opposite direction by the same amount.
2275  /* ResizeWindow() below ensures marking new position as dirty. */
2276  } else {
2277  _drag_delta.x += x;
2278  }
2279 
2280  /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
2281  ResizeWindow(w, x, y);
2282  return ES_HANDLED;
2283  }
2284  }
2285 
2286  _dragging_window = false;
2287  return ES_HANDLED;
2288 }
2289 
2294 static void StartWindowDrag(Window *w)
2295 {
2296  w->flags |= WF_DRAGGING;
2297  w->flags &= ~WF_CENTERED;
2298  _dragging_window = true;
2299 
2300  _drag_delta.x = w->left - _cursor.pos.x;
2301  _drag_delta.y = w->top - _cursor.pos.y;
2302 
2303  BringWindowToFront(w);
2305 }
2306 
2312 static void StartWindowSizing(Window *w, bool to_left)
2313 {
2314  w->flags |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
2315  w->flags &= ~WF_CENTERED;
2316  _dragging_window = true;
2317 
2318  _drag_delta.x = _cursor.pos.x;
2319  _drag_delta.y = _cursor.pos.y;
2320 
2321  BringWindowToFront(w);
2323 }
2324 
2330 {
2331  Window *w;
2332  FOR_ALL_WINDOWS_FROM_BACK(w) {
2333  if (w->scrolling_scrollbar >= 0) {
2334  /* Abort if no button is clicked any more. */
2335  if (!_left_button_down) {
2336  w->scrolling_scrollbar = -1;
2337  w->SetDirty();
2338  return ES_HANDLED;
2339  }
2340 
2341  int i;
2343  bool rtl = false;
2344 
2345  if (sb->type == NWID_HSCROLLBAR) {
2346  i = _cursor.pos.x - _cursorpos_drag_start.x;
2347  rtl = _current_text_dir == TD_RTL;
2348  } else {
2349  i = _cursor.pos.y - _cursorpos_drag_start.y;
2350  }
2351 
2352  if (sb->disp_flags & ND_SCROLLBAR_BTN) {
2353  if (_scroller_click_timeout == 1) {
2354  _scroller_click_timeout = 3;
2355  sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
2356  w->SetDirty();
2357  }
2358  return ES_HANDLED;
2359  }
2360 
2361  /* Find the item we want to move to and make sure it's inside bounds. */
2362  int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
2363  if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
2364  if (pos != sb->GetPosition()) {
2365  sb->SetPosition(pos);
2366  w->SetDirty();
2367  }
2368  return ES_HANDLED;
2369  }
2370  }
2371 
2372  return ES_NOT_HANDLED;
2373 }
2374 
2380 {
2381  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2382 
2383  if (!_scrolling_viewport) return ES_NOT_HANDLED;
2384 
2385  /* When we don't have a last scroll window we are starting to scroll.
2386  * When the last scroll window and this are not the same we went
2387  * outside of the window and should not left-mouse scroll anymore. */
2388  if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2389 
2390  if (_last_scroll_window == NULL || !(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down))) {
2391  _cursor.fix_at = false;
2392  _scrolling_viewport = false;
2393  _last_scroll_window = NULL;
2394  return ES_NOT_HANDLED;
2395  }
2396 
2397  if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
2398  /* If the main window is following a vehicle, then first let go of it! */
2399  const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
2400  ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
2401  return ES_NOT_HANDLED;
2402  }
2403 
2404  Point delta;
2406  delta.x = -_cursor.delta.x;
2407  delta.y = -_cursor.delta.y;
2408  } else {
2409  delta.x = _cursor.delta.x;
2410  delta.y = _cursor.delta.y;
2411  }
2412 
2413  if (scrollwheel_scrolling) {
2414  /* We are using scrollwheels for scrolling */
2415  delta.x = _cursor.h_wheel;
2416  delta.y = _cursor.v_wheel;
2417  _cursor.v_wheel = 0;
2418  _cursor.h_wheel = 0;
2419  }
2420 
2421  /* Create a scroll-event and send it to the window */
2422  if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
2423 
2424  _cursor.delta.x = 0;
2425  _cursor.delta.y = 0;
2426  return ES_HANDLED;
2427 }
2428 
2440 {
2441  bool bring_to_front = false;
2442 
2443  if (w->window_class == WC_MAIN_WINDOW ||
2444  IsVitalWindow(w) ||
2445  w->window_class == WC_TOOLTIPS ||
2447  return true;
2448  }
2449 
2450  /* Use unshaded window size rather than current size for shaded windows. */
2451  int w_width = w->width;
2452  int w_height = w->height;
2453  if (w->IsShaded()) {
2454  w_width = w->unshaded_size.width;
2455  w_height = w->unshaded_size.height;
2456  }
2457 
2458  Window *u;
2460  /* A modal child will prevent the activation of the parent window */
2461  if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) {
2462  u->SetWhiteBorder();
2463  u->SetDirty();
2464  return false;
2465  }
2466 
2467  if (u->window_class == WC_MAIN_WINDOW ||
2468  IsVitalWindow(u) ||
2469  u->window_class == WC_TOOLTIPS ||
2471  continue;
2472  }
2473 
2474  /* Window sizes don't interfere, leave z-order alone */
2475  if (w->left + w_width <= u->left ||
2476  u->left + u->width <= w->left ||
2477  w->top + w_height <= u->top ||
2478  u->top + u->height <= w->top) {
2479  continue;
2480  }
2481 
2482  bring_to_front = true;
2483  }
2484 
2485  if (bring_to_front) BringWindowToFront(w);
2486  return true;
2487 }
2488 
2497 EventState Window::HandleEditBoxKey(int wid, WChar key, uint16 keycode)
2498 {
2499  QueryString *query = this->GetQueryString(wid);
2500  if (query == NULL) return ES_NOT_HANDLED;
2501 
2502  int action = QueryString::ACTION_NOTHING;
2503 
2504  switch (query->text.HandleKeyPress(key, keycode)) {
2505  case HKPR_EDITING:
2506  this->SetWidgetDirty(wid);
2507  this->OnEditboxChanged(wid);
2508  break;
2509 
2510  case HKPR_CURSOR:
2511  this->SetWidgetDirty(wid);
2512  /* For the OSK also invalidate the parent window */
2513  if (this->window_class == WC_OSK) this->InvalidateData();
2514  break;
2515 
2516  case HKPR_CONFIRM:
2517  if (this->window_class == WC_OSK) {
2518  this->OnClick(Point(), WID_OSK_OK, 1);
2519  } else if (query->ok_button >= 0) {
2520  this->OnClick(Point(), query->ok_button, 1);
2521  } else {
2522  action = query->ok_button;
2523  }
2524  break;
2525 
2526  case HKPR_CANCEL:
2527  if (this->window_class == WC_OSK) {
2528  this->OnClick(Point(), WID_OSK_CANCEL, 1);
2529  } else if (query->cancel_button >= 0) {
2530  this->OnClick(Point(), query->cancel_button, 1);
2531  } else {
2532  action = query->cancel_button;
2533  }
2534  break;
2535 
2536  case HKPR_NOT_HANDLED:
2537  return ES_NOT_HANDLED;
2538 
2539  default: break;
2540  }
2541 
2542  switch (action) {
2544  this->UnfocusFocusedWidget();
2545  break;
2546 
2548  if (query->text.bytes <= 1) {
2549  /* If already empty, unfocus instead */
2550  this->UnfocusFocusedWidget();
2551  } else {
2552  query->text.DeleteAll();
2553  this->SetWidgetDirty(wid);
2554  this->OnEditboxChanged(wid);
2555  }
2556  break;
2557 
2558  default:
2559  break;
2560  }
2561 
2562  return ES_HANDLED;
2563 }
2564 
2570 void HandleKeypress(uint keycode, WChar key)
2571 {
2572  /* World generation is multithreaded and messes with companies.
2573  * But there is no company related window open anyway, so _current_company is not used. */
2574  assert(HasModalProgress() || IsLocalCompany());
2575 
2576  /*
2577  * The Unicode standard defines an area called the private use area. Code points in this
2578  * area are reserved for private use and thus not portable between systems. For instance,
2579  * Apple defines code points for the arrow keys in this area, but these are only printable
2580  * on a system running OS X. We don't want these keys to show up in text fields and such,
2581  * and thus we have to clear the unicode character when we encounter such a key.
2582  */
2583  if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2584 
2585  /*
2586  * If both key and keycode is zero, we don't bother to process the event.
2587  */
2588  if (key == 0 && keycode == 0) return;
2589 
2590  /* Check if the focused window has a focused editbox */
2591  if (EditBoxInGlobalFocus()) {
2592  /* All input will in this case go to the focused editbox */
2593  if (_focused_window->window_class == WC_CONSOLE) {
2594  if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
2595  } else {
2596  if (_focused_window->HandleEditBoxKey(_focused_window->nested_focus->index, key, keycode) == ES_HANDLED) return;
2597  }
2598  }
2599 
2600  /* Call the event, start with the uppermost window, but ignore the toolbar. */
2601  Window *w;
2602  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2603  if (w->window_class == WC_MAIN_TOOLBAR) continue;
2604  if (w->window_desc->hotkeys != NULL) {
2605  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2606  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2607  }
2608  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2609  }
2610 
2612  /* When there is no toolbar w is null, check for that */
2613  if (w != NULL) {
2614  if (w->window_desc->hotkeys != NULL) {
2615  int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
2616  if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
2617  }
2618  if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
2619  }
2620 
2621  HandleGlobalHotkeys(key, keycode);
2622 }
2623 
2628 {
2629  /* Call the event, start with the uppermost window. */
2630  Window *w;
2631  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2632  if (w->OnCTRLStateChange() == ES_HANDLED) return;
2633  }
2634 }
2635 
2641 /* virtual */ void Window::InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2642 {
2643  QueryString *query = this->GetQueryString(wid);
2644  if (query == NULL) return;
2645 
2646  if (query->text.InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2647  this->SetWidgetDirty(wid);
2648  this->OnEditboxChanged(wid);
2649  }
2650 }
2651 
2658 void HandleTextInput(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
2659 {
2660  if (!EditBoxInGlobalFocus()) return;
2661 
2662  _focused_window->InsertTextString(_focused_window->window_class == WC_CONSOLE ? 0 : _focused_window->nested_focus->index, str, marked, caret, insert_location, replacement_end);
2663 }
2664 
2672 
2677 static void HandleAutoscroll()
2678 {
2679  if (_game_mode == GM_MENU || HasModalProgress()) return;
2681  if (_settings_client.gui.auto_scrolling == VA_MAIN_VIEWPORT_FULLSCREEN && !_fullscreen) return;
2682 
2683  int x = _cursor.pos.x;
2684  int y = _cursor.pos.y;
2685  Window *w = FindWindowFromPt(x, y);
2686  if (w == NULL || w->flags & WF_DISABLE_VP_SCROLL) return;
2688 
2689  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2690  if (vp == NULL) return;
2691 
2692  x -= vp->left;
2693  y -= vp->top;
2694 
2695  /* here allows scrolling in both x and y axis */
2696 #define scrollspeed 3
2697  if (x - 15 < 0) {
2698  w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
2699  } else if (15 - (vp->width - x) > 0) {
2700  w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
2701  }
2702  if (y - 15 < 0) {
2703  w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
2704  } else if (15 - (vp->height - y) > 0) {
2705  w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
2706  }
2707 #undef scrollspeed
2708 }
2709 
2711  MC_NONE = 0,
2712  MC_LEFT,
2713  MC_RIGHT,
2714  MC_DOUBLE_LEFT,
2715  MC_HOVER,
2716 
2720 };
2722 
2723 static void ScrollMainViewport(int x, int y)
2724 {
2725  if (_game_mode != GM_MENU) {
2727  assert(w);
2728 
2731  }
2732 }
2733 
2743 static const int8 scrollamt[16][2] = {
2744  { 0, 0},
2745  {-2, 0},
2746  { 0, -2},
2747  {-2, -1},
2748  { 2, 0},
2749  { 0, 0},
2750  { 2, -1},
2751  { 0, -2},
2752  { 0, 2},
2753  {-2, 1},
2754  { 0, 0},
2755  {-2, 0},
2756  { 2, 1},
2757  { 0, 2},
2758  { 2, 0},
2759  { 0, 0},
2760 };
2761 
2762 static void HandleKeyScrolling()
2763 {
2764  /*
2765  * Check that any of the dirkeys is pressed and that the focused window
2766  * doesn't have an edit-box as focused widget.
2767  */
2768  if (_dirkeys && !EditBoxInGlobalFocus()) {
2769  int factor = _shift_pressed ? 50 : 10;
2770  ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
2771  }
2772 }
2773 
2774 static void MouseLoop(MouseClick click, int mousewheel)
2775 {
2776  /* World generation is multithreaded and messes with companies.
2777  * But there is no company related window open anyway, so _current_company is not used. */
2778  assert(HasModalProgress() || IsLocalCompany());
2779 
2780  HandlePlacePresize();
2782 
2783  if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
2784  if (HandleMouseDragDrop() == ES_HANDLED) return;
2785  if (HandleWindowDragging() == ES_HANDLED) return;
2786  if (HandleScrollbarScrolling() == ES_HANDLED) return;
2787  if (HandleViewportScroll() == ES_HANDLED) return;
2788 
2789  HandleMouseOver();
2790 
2791  bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
2792  if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
2793 
2794  int x = _cursor.pos.x;
2795  int y = _cursor.pos.y;
2796  Window *w = FindWindowFromPt(x, y);
2797  if (w == NULL) return;
2798 
2799  if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
2800  ViewPort *vp = IsPtInWindowViewport(w, x, y);
2801 
2802  /* Don't allow any action in a viewport if either in menu or when having a modal progress window */
2803  if (vp != NULL && (_game_mode == GM_MENU || HasModalProgress())) return;
2804 
2805  if (mousewheel != 0) {
2806  /* Send mousewheel event to window */
2807  w->OnMouseWheel(mousewheel);
2808 
2809  /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
2810  if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
2811  }
2812 
2813  if (vp != NULL) {
2814  if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button
2815  switch (click) {
2816  case MC_DOUBLE_LEFT:
2817  case MC_LEFT:
2818  DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite);
2819  if (!HandleViewportClicked(vp, x, y) &&
2820  !(w->flags & WF_DISABLE_VP_SCROLL) &&
2822  _scrolling_viewport = true;
2823  _cursor.fix_at = false;
2824  }
2825  break;
2826 
2827  case MC_RIGHT:
2828  if (!(w->flags & WF_DISABLE_VP_SCROLL)) {
2829  _scrolling_viewport = true;
2830  _cursor.fix_at = true;
2831 
2832  /* clear 2D scrolling caches before we start a 2D scroll */
2833  _cursor.h_wheel = 0;
2834  _cursor.v_wheel = 0;
2835  }
2836  break;
2837 
2838  default:
2839  break;
2840  }
2841  } else {
2842  switch (click) {
2843  case MC_LEFT:
2844  case MC_DOUBLE_LEFT:
2845  DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
2846  break;
2847 
2848  default:
2849  if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
2850  /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
2851  * Simulate a right button click so we can get started. */
2852  /* FALL THROUGH */
2853 
2854  case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
2855 
2856  case MC_HOVER: DispatchHoverEvent(w, x - w->left, y - w->top); break;
2857  }
2858  }
2859 }
2860 
2865 {
2866  /* World generation is multithreaded and messes with companies.
2867  * But there is no company related window open anyway, so _current_company is not used. */
2868  assert(HasModalProgress() || IsLocalCompany());
2869 
2870  static int double_click_time = 0;
2871  static Point double_click_pos = {0, 0};
2872 
2873  /* Mouse event? */
2874  MouseClick click = MC_NONE;
2876  click = MC_LEFT;
2877  if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
2878  double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
2879  double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
2880  click = MC_DOUBLE_LEFT;
2881  }
2882  double_click_time = _realtime_tick;
2883  double_click_pos = _cursor.pos;
2884  _left_button_clicked = true;
2885  _input_events_this_tick++;
2886  } else if (_right_button_clicked) {
2887  _right_button_clicked = false;
2888  click = MC_RIGHT;
2889  _input_events_this_tick++;
2890  }
2891 
2892  int mousewheel = 0;
2893  if (_cursor.wheel) {
2894  mousewheel = _cursor.wheel;
2895  _cursor.wheel = 0;
2896  _input_events_this_tick++;
2897  }
2898 
2899  static uint32 hover_time = 0;
2900  static Point hover_pos = {0, 0};
2901 
2903  if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
2904  hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
2905  hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
2906  hover_pos = _cursor.pos;
2907  hover_time = _realtime_tick;
2908  _mouse_hovering = false;
2909  } else {
2910  if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
2911  click = MC_HOVER;
2912  _input_events_this_tick++;
2913  _mouse_hovering = true;
2914  }
2915  }
2916  }
2917 
2918  /* Handle sprite picker before any GUI interaction */
2920  /* Next realtime tick? Then redraw has finished */
2921  _newgrf_debug_sprite_picker.mode = SPM_NONE;
2923  }
2924 
2925  if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
2926  /* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
2928  _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
2931  _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
2933  } else {
2934  MouseLoop(click, mousewheel);
2935  }
2936 
2937  /* We have moved the mouse the required distance,
2938  * no need to move it at any later time. */
2939  _cursor.delta.x = 0;
2940  _cursor.delta.y = 0;
2941 }
2942 
2946 static void CheckSoftLimit()
2947 {
2948  if (_settings_client.gui.window_soft_limit == 0) return;
2949 
2950  for (;;) {
2951  uint deletable_count = 0;
2952  Window *w, *last_deletable = NULL;
2953  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2954  if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags & WF_STICKY)) continue;
2955 
2956  last_deletable = w;
2957  deletable_count++;
2958  }
2959 
2960  /* We've not reached the soft limit yet. */
2961  if (deletable_count <= _settings_client.gui.window_soft_limit) break;
2962 
2963  assert(last_deletable != NULL);
2964  delete last_deletable;
2965  }
2966 }
2967 
2972 {
2973  /* World generation is multithreaded and messes with companies.
2974  * But there is no company related window open anyway, so _current_company is not used. */
2975  assert(HasModalProgress() || IsLocalCompany());
2976 
2977  CheckSoftLimit();
2978  HandleKeyScrolling();
2979 
2980  /* Do the actual free of the deleted windows. */
2981  for (Window *v = _z_front_window; v != NULL; /* nothing */) {
2982  Window *w = v;
2983  v = v->z_back;
2984 
2985  if (w->window_class != WC_INVALID) continue;
2986 
2988  free(w);
2989  }
2990 
2991  if (_scroller_click_timeout != 0) _scroller_click_timeout--;
2992  DecreaseWindowCounters();
2993 
2994  if (_input_events_this_tick != 0) {
2995  /* The input loop is called only once per GameLoop() - so we can clear the counter here */
2996  _input_events_this_tick = 0;
2997  /* there were some inputs this tick, don't scroll ??? */
2998  return;
2999  }
3000 
3001  /* HandleMouseEvents was already called for this tick */
3003  HandleAutoscroll();
3004 }
3005 
3010 {
3011  Window *w;
3012 
3013  static int highlight_timer = 1;
3014  if (--highlight_timer == 0) {
3015  highlight_timer = 15;
3017  }
3018 
3019  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3022  }
3023 
3024  static int we4_timer = 0;
3025  int t = we4_timer + 1;
3026 
3027  if (t >= 100) {
3028  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3029  w->OnHundredthTick();
3030  }
3031  t = 0;
3032  }
3033  we4_timer = t;
3034 
3035  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3036  if ((w->flags & WF_WHITE_BORDER) && --w->white_border_timer == 0) {
3038  w->SetDirty();
3039  }
3040  }
3041 
3042  DrawDirtyBlocks();
3043 
3044  FOR_ALL_WINDOWS_FROM_BACK(w) {
3045  /* Update viewport only if window is not shaded. */
3046  if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
3047  }
3049  /* Redraw mouse cursor in case it was hidden */
3050  DrawMouseCursor();
3051 }
3052 
3059 {
3060  const Window *w;
3061  FOR_ALL_WINDOWS_FROM_BACK(w) {
3062  if (w->window_class == cls && w->window_number == number) w->SetDirty();
3063  }
3064 }
3065 
3072 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
3073 {
3074  const Window *w;
3075  FOR_ALL_WINDOWS_FROM_BACK(w) {
3076  if (w->window_class == cls && w->window_number == number) {
3077  w->SetWidgetDirty(widget_index);
3078  }
3079  }
3080 }
3081 
3087 {
3088  Window *w;
3089  FOR_ALL_WINDOWS_FROM_BACK(w) {
3090  if (w->window_class == cls) w->SetDirty();
3091  }
3092 }
3093 
3099 void Window::InvalidateData(int data, bool gui_scope)
3100 {
3101  this->SetDirty();
3102  if (!gui_scope) {
3103  /* Schedule GUI-scope invalidation for next redraw. */
3104  *this->scheduled_invalidation_data.Append() = data;
3105  }
3106  this->OnInvalidateData(data, gui_scope);
3107 }
3108 
3113 {
3114  for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
3115  this->OnInvalidateData(*data, true);
3116  }
3118 }
3119 
3124 {
3125  if ((this->flags & WF_HIGHLIGHTED) == 0) return;
3126 
3127  for (uint i = 0; i < this->nested_array_size; i++) {
3128  if (this->IsWidgetHighlighted(i)) this->SetWidgetDirty(i);
3129  }
3130 }
3131 
3158 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
3159 {
3160  Window *w;
3161  FOR_ALL_WINDOWS_FROM_BACK(w) {
3162  if (w->window_class == cls && w->window_number == number) {
3163  w->InvalidateData(data, gui_scope);
3164  }
3165  }
3166 }
3167 
3176 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
3177 {
3178  Window *w;
3179 
3180  FOR_ALL_WINDOWS_FROM_BACK(w) {
3181  if (w->window_class == cls) {
3182  w->InvalidateData(data, gui_scope);
3183  }
3184  }
3185 }
3186 
3191 {
3192  Window *w;
3193  FOR_ALL_WINDOWS_FROM_FRONT(w) {
3194  w->OnTick();
3195  }
3196 }
3197 
3205 {
3206  Window *w;
3207 
3208 restart_search:
3209  /* When we find the window to delete, we need to restart the search
3210  * as deleting this window could cascade in deleting (many) others
3211  * anywhere in the z-array */
3212  FOR_ALL_WINDOWS_FROM_BACK(w) {
3213  if (w->window_class != WC_MAIN_WINDOW &&
3214  w->window_class != WC_SELECT_GAME &&
3215  w->window_class != WC_MAIN_TOOLBAR &&
3216  w->window_class != WC_STATUS_BAR &&
3217  w->window_class != WC_TOOLTIPS &&
3218  (w->flags & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
3219 
3220  delete w;
3221  goto restart_search;
3222  }
3223  }
3224 }
3225 
3234 {
3235  Window *w;
3236 
3237  /* Delete every window except for stickied ones, then sticky ones as well */
3239 
3240 restart_search:
3241  /* When we find the window to delete, we need to restart the search
3242  * as deleting this window could cascade in deleting (many) others
3243  * anywhere in the z-array */
3244  FOR_ALL_WINDOWS_FROM_BACK(w) {
3245  if (w->flags & WF_STICKY) {
3246  delete w;
3247  goto restart_search;
3248  }
3249  }
3250 }
3251 
3257 {
3258  Window *w;
3259 
3260 restart_search:
3261  /* When we find the window to delete, we need to restart the search
3262  * as deleting this window could cascade in deleting (many) others
3263  * anywhere in the z-array */
3264  FOR_ALL_WINDOWS_FROM_BACK(w) {
3265  if (w->window_desc->flags & WDF_CONSTRUCTION) {
3266  delete w;
3267  goto restart_search;
3268  }
3269  }
3270 
3271  FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
3272 }
3273 
3276 {
3279 }
3280 
3283 {
3284  NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
3285  NWidgetScrollbar::InvalidateDimensionCache();
3286 
3287  extern void InitDepotWindowBlockSizes();
3289 
3290  Window *w;
3291  FOR_ALL_WINDOWS_FROM_BACK(w) {
3292  w->ReInit();
3293  }
3294 #ifdef ENABLE_NETWORK
3295  void NetworkReInitChatBoxSize();
3297 #endif
3298 
3299  /* Make sure essential parts of all windows are visible */
3302 }
3303 
3311 static int PositionWindow(Window *w, WindowClass clss, int setting)
3312 {
3313  if (w == NULL || w->window_class != clss) {
3314  w = FindWindowById(clss, 0);
3315  }
3316  if (w == NULL) return 0;
3317 
3318  int old_left = w->left;
3319  switch (setting) {
3320  case 1: w->left = (_screen.width - w->width) / 2; break;
3321  case 2: w->left = _screen.width - w->width; break;
3322  default: w->left = 0; break;
3323  }
3324  if (w->viewport != NULL) w->viewport->left += w->left - old_left;
3325  SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height); // invalidate the whole row
3326  return w->left;
3327 }
3328 
3335 {
3336  DEBUG(misc, 5, "Repositioning Main Toolbar...");
3338 }
3339 
3346 {
3347  DEBUG(misc, 5, "Repositioning statusbar...");
3349 }
3350 
3357 {
3358  DEBUG(misc, 5, "Repositioning news message...");
3360 }
3361 
3368 {
3369  DEBUG(misc, 5, "Repositioning network chat window...");
3371 }
3372 
3373 
3379 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
3380 {
3381  Window *w;
3382  FOR_ALL_WINDOWS_FROM_BACK(w) {
3383  if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
3384  w->viewport->follow_vehicle = to_index;
3385  w->SetDirty();
3386  }
3387  }
3388 }
3389 
3390 
3396 void RelocateAllWindows(int neww, int newh)
3397 {
3398  Window *w;
3399 
3400  FOR_ALL_WINDOWS_FROM_BACK(w) {
3401  int left, top;
3402  /* XXX - this probably needs something more sane. For example specifying
3403  * in a 'backup'-desc that the window should always be centered. */
3404  switch (w->window_class) {
3405  case WC_MAIN_WINDOW:
3406  case WC_BOOTSTRAP:
3407  ResizeWindow(w, neww, newh);
3408  continue;
3409 
3410  case WC_MAIN_TOOLBAR:
3411  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3412 
3413  top = w->top;
3414  left = PositionMainToolbar(w); // changes toolbar orientation
3415  break;
3416 
3417  case WC_NEWS_WINDOW:
3418  top = newh - w->height;
3419  left = PositionNewsMessage(w);
3420  break;
3421 
3422  case WC_STATUS_BAR:
3423  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3424 
3425  top = newh - w->height;
3426  left = PositionStatusbar(w);
3427  break;
3428 
3429  case WC_SEND_NETWORK_MSG:
3430  ResizeWindow(w, min(neww, _toolbar_width) - w->width, 0, false);
3431 
3432  top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
3433  left = PositionNetworkChatWindow(w);
3434  break;
3435 
3436  case WC_CONSOLE:
3437  IConsoleResize(w);
3438  continue;
3439 
3440  default: {
3441  if (w->flags & WF_CENTERED) {
3442  top = (newh - w->height) >> 1;
3443  left = (neww - w->width) >> 1;
3444  break;
3445  }
3446 
3447  left = w->left;
3448  if (left + (w->width >> 1) >= neww) left = neww - w->width;
3449  if (left < 0) left = 0;
3450 
3451  top = w->top;
3452  if (top + (w->height >> 1) >= newh) top = newh - w->height;
3453  break;
3454  }
3455  }
3456 
3457  EnsureVisibleCaption(w, left, top);
3458  }
3459 }
3460 
3467 {
3468  this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
3469  ResetObjectToPlace();
3470 }