# window.cpp

Go to the documentation of this file.

    00001 /* $Id: window.cpp 19567 2010-04-05 21:34:47Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include <stdarg.h>
    00014 #include "openttd.h"
    00015 #include "company_func.h"
    00016 #include "gfx_func.h"
    00017 #include "console_func.h"
    00018 #include "console_gui.h"
    00019 #include "viewport_func.h"
    00020 #include "variables.h"
    00021 #include "genworld.h"
    00022 #include "blitter/factory.hpp"
    00023 #include "zoom_func.h"
    00024 #include "map_func.h"
    00025 #include "vehicle_base.h"
    00026 #include "cheat_type.h"
    00027 #include "window_func.h"
    00028 #include "tilehighlight_func.h"
    00029 #include "network/network.h"
    00030 #include "querystring_gui.h"
    00031 #include "widgets/dropdown_func.h"
    00032 #include "strings_func.h"
    00033 #include "settings_type.h"
    00034
    00035 #include "table/sprites.h"
    00036
    00037 static Point _drag_delta;
    00038 static Window *_mouseover_last_w = NULL;
    00039
    00041 Window *_z_front_window = NULL;
    00043 Window *_z_back_window  = NULL;
    00044
    00045 /*
    00046  * Window that currently have focus. - The main purpose is to generate
    00047  * FocusLost events, not to give next window in z-order focus when a
    00048  * window is closed.
    00049  */
    00050 Window *_focused_window;
    00051
    00052 Point _cursorpos_drag_start;
    00053
    00054 int _scrollbar_start_pos;
    00055 int _scrollbar_size;
    00056 byte _scroller_click_timeout;
    00057
    00058 bool _scrolling_scrollbar;
    00059 bool _scrolling_viewport;
    00060
    00061 byte _special_mouse_mode;
    00062
    00064 WindowDesc::WindowDesc(WindowPosition def_pos, int16 def_width, int16 def_height,
    00065       WindowClass window_class, WindowClass parent_class, uint32 flags,
    00066       const NWidgetPart *nwid_parts, int16 nwid_length) :
    00067   default_pos(def_pos),
    00068   default_width(def_width),
    00069   default_height(def_height),
    00070   cls(window_class),
    00071   parent_cls(parent_class),
    00072   flags(flags),
    00073   nwid_parts(nwid_parts),
    00074   nwid_length(nwid_length)
    00075 {
    00076 }
    00077
    00078 WindowDesc::~WindowDesc()
    00079 {
    00080 }
    00081
    00089 void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
    00090 {
    00091   NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
    00092   if (this->is_vertical) {
    00093     this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
    00094   } else {
    00095     this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
    00096   }
    00097 }
    00098
    00103 void SetFocusedWindow(Window *w)
    00104 {
    00105   if (_focused_window == w) return;
    00106
    00107   /* Invalidate focused widget */
    00108   if (_focused_window != NULL) {
    00109     if (_focused_window->nested_focus != NULL) _focused_window->nested_focus->SetDirty(_focused_window);
    00110   }
    00111
    00112   /* Remember which window was previously focused */
    00113   Window *old_focused = _focused_window;
    00114   _focused_window = w;
    00115
    00116   /* So we can inform it that it lost focus */
    00117   if (old_focused != NULL) old_focused->OnFocusLost();
    00118   if (_focused_window != NULL) _focused_window->OnFocus();
    00119 }
    00120
    00126 bool EditBoxInGlobalFocus()
    00127 {
    00128   if (_focused_window == NULL) return false;
    00129
    00130   /* The console does not have an edit box so a special case is needed. */
    00131   if (_focused_window->window_class == WC_CONSOLE) return true;
    00132
    00133   return _focused_window->nested_focus != NULL && _focused_window->nested_focus->type == WWT_EDITBOX;
    00134 }
    00135
    00139 void Window::UnfocusFocusedWidget()
    00140 {
    00141   if (this->nested_focus != NULL) {
    00142     /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
    00143     this->nested_focus->SetDirty(this);
    00144     this->nested_focus = NULL;
    00145   }
    00146 }
    00147
    00153 bool Window::SetFocusedWidget(byte widget_index)
    00154 {
    00155   /* Do nothing if widget_index is already focused, or if it wasn't a valid widget. */
    00156   if (widget_index >= this->nested_array_size) return false;
    00157
    00158   assert(this->nested_array[widget_index] != NULL); // Setting focus to a non-existing widget is a bad idea.
    00159   if (this->nested_focus != NULL) {
    00160     if (this->GetWidget<NWidgetCore>(widget_index) == this->nested_focus) return false;
    00161
    00162     /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
    00163     this->nested_focus->SetDirty(this);
    00164   }
    00165   this->nested_focus = this->GetWidget<NWidgetCore>(widget_index);
    00166   return true;
    00167 }
    00168
    00176 void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...)
    00177 {
    00178   va_list wdg_list;
    00179
    00180   va_start(wdg_list, widgets);
    00181
    00182   while (widgets != WIDGET_LIST_END) {
    00183     SetWidgetDisabledState(widgets, disab_stat);
    00184     widgets = va_arg(wdg_list, int);
    00185   }
    00186
    00187   va_end(wdg_list);
    00188 }
    00189
    00195 void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...)
    00196 {
    00197   va_list wdg_list;
    00198
    00199   va_start(wdg_list, widgets);
    00200
    00201   while (widgets != WIDGET_LIST_END) {
    00202     SetWidgetLoweredState(widgets, lowered_stat);
    00203     widgets = va_arg(wdg_list, int);
    00204   }
    00205
    00206   va_end(wdg_list);
    00207 }
    00208
    00213 void Window::RaiseButtons(bool autoraise)
    00214 {
    00215   for (uint i = 0; i < this->nested_array_size; i++) {
    00216     if (this->nested_array[i] != NULL && (this->nested_array[i]->type & ~WWB_PUSHBUTTON) < WWT_LAST &&
    00217         (!autoraise || (this->nested_array[i]->type & WWB_PUSHBUTTON)) && this->IsWidgetLowered(i)) {
    00218       this->RaiseWidget(i);
    00219       this->SetWidgetDirty(i);
    00220     }
    00221   }
    00222 }
    00223
    00228 void Window::SetWidgetDirty(byte widget_index) const
    00229 {
    00230   /* Sometimes this function is called before the window is even fully initialized */
    00231   if (this->nested_array == NULL) return;
    00232
    00233   this->nested_array[widget_index]->SetDirty(this);
    00234 }
    00235
    00241 void Window::HandleButtonClick(byte widget)
    00242 {
    00243   this->LowerWidget(widget);
    00244   this->flags4 |= WF_TIMEOUT_BEGIN;
    00245   this->SetWidgetDirty(widget);
    00246 }
    00247
    00248 static void StartWindowDrag(Window *w);
    00249 static void StartWindowSizing(Window *w, bool to_left);
    00250
    00258 static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
    00259 {
    00260   const NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
    00261   WidgetType widget_type = (nw != NULL) ? nw->type : WWT_EMPTY;
    00262
    00263   bool focused_widget_changed = false;
    00264   /* If clicked on a window that previously did dot have focus */
    00265   if (_focused_window != w &&                 // We already have focus, right?
    00266       (w->desc_flags & WDF_NO_FOCUS) == 0 &&  // Don't lose focus to toolbars
    00267       widget_type != WWT_CLOSEBOX) {          // Don't change focused window if 'X' (close button) was clicked
    00268     focused_widget_changed = true;
    00269     if (_focused_window != NULL) {
    00270       _focused_window->OnFocusLost();
    00271
    00272       /* The window that lost focus may have had opened a OSK, window so close it, unless the user has clicked on the OSK window. */
    00273       if (w->window_class != WC_OSK) DeleteWindowById(WC_OSK, 0);
    00274     }
    00275     SetFocusedWindow(w);
    00276     w->OnFocus();
    00277   }
    00278
    00279   if (nw == NULL) return; // exit if clicked outside of widgets
    00280
    00281   /* don't allow any interaction if the button has been disabled */
    00282   if (nw->IsDisabled()) return;
    00283
    00284   int widget_index = nw->index;
    00285
    00286   /* Clicked on a widget that is not disabled.
    00287    * So unless the clicked widget is the caption bar, change focus to this widget */
    00288   if (widget_type != WWT_CAPTION) {
    00289     /* Close the OSK window if a edit box loses focus */
    00290     if (w->nested_focus != NULL &&  w->nested_focus->type == WWT_EDITBOX && w->nested_focus != nw && w->window_class != WC_OSK) {
    00291       DeleteWindowById(WC_OSK, 0);
    00292     }
    00293
    00294     /* focused_widget_changed is 'now' only true if the window this widget
    00295      * is in gained focus. In that case it must remain true, also if the
    00296      * local widget focus did not change. As such it's the logical-or of
    00297      * both changed states.
    00298      *
    00299      * If this is not preserved, then the OSK window would be opened when
    00300      * a user has the edit box focused and then click on another window and
    00301      * then back again on the edit box (to type some text).
    00302      */
    00303     focused_widget_changed |= w->SetFocusedWidget(widget_index);
    00304   }
    00305
    00306   /* Close any child drop down menus. If the button pressed was the drop down
    00307    * list's own button, then we should not process the click any further. */
    00308   if (HideDropDownMenu(w) == widget_index && widget_index >= 0) return;
    00309
    00310   switch (widget_type) {
    00311     /* special widget handling for buttons*/
    00312     case WWT_PANEL   | WWB_PUSHBUTTON: // WWT_PUSHBTN
    00313     case WWT_IMGBTN  | WWB_PUSHBUTTON: // WWT_PUSHIMGBTN
    00314     case WWT_TEXTBTN | WWB_PUSHBUTTON: // WWT_PUSHTXTBTN
    00315       w->HandleButtonClick(widget_index);
    00316       break;
    00317
    00318     case WWT_SCROLLBAR:
    00319     case WWT_SCROLL2BAR:
    00320     case WWT_HSCROLLBAR:
    00321       ScrollbarClickHandler(w, nw, x, y);
    00322       break;
    00323
    00324     case WWT_EDITBOX:
    00325       if (!focused_widget_changed) { // Only open the OSK window if clicking on an already focused edit box
    00326         /* Open the OSK window if clicked on an edit box */
    00327         QueryStringBaseWindow *qs = dynamic_cast<QueryStringBaseWindow *>(w);
    00328         if (qs != NULL) {
    00329           qs->OnOpenOSKWindow(widget_index);
    00330         }
    00331       }
    00332       break;
    00333
    00334     case WWT_CLOSEBOX: // 'X'
    00335       delete w;
    00336       return;
    00337
    00338     case WWT_CAPTION: // 'Title bar'
    00339       StartWindowDrag(w);
    00340       return;
    00341
    00342     case WWT_RESIZEBOX:
    00343       /* When the resize widget is on the left size of the window
    00344        * we assume that that button is used to resize to the left. */
    00345       StartWindowSizing(w, (int)nw->pos_x < (w->width / 2));
    00346       nw->SetDirty(w);
    00347       return;
    00348
    00349     case WWT_SHADEBOX:
    00350       nw->SetDirty(w);
    00351       w->SetShaded(!w->IsShaded());
    00352       return;
    00353
    00354     case WWT_STICKYBOX:
    00355       w->flags4 ^= WF_STICKY;
    00356       nw->SetDirty(w);
    00357       return;
    00358
    00359     default:
    00360       break;
    00361   }
    00362
    00363   /* Widget has no index, so the window is not interested in it. */
    00364   if (widget_index < 0) return;
    00365
    00366   Point pt = { x, y };
    00367   w->OnClick(pt, widget_index, click_count);
    00368 }
    00369
    00376 static void DispatchRightClickEvent(Window *w, int x, int y)
    00377 {
    00378   NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
    00379
    00380   /* No widget to handle */
    00381   if (wid == NULL) return;
    00382
    00383   /* Show the tooltip if there is any */
    00384   if (wid->tool_tip != 0) {
    00385     GuiShowTooltips(wid->tool_tip);
    00386     return;
    00387   }
    00388
    00389   /* Widget has no index, so the window is not interested in it. */
    00390   if (wid->index < 0) return;
    00391
    00392   Point pt = { x, y };
    00393   w->OnRightClick(pt, wid->index);
    00394 }
    00395
    00403 static void DispatchMouseWheelEvent(Window *w, const NWidgetCore *nwid, int wheel)
    00404 {
    00405   if (nwid == NULL) return;
    00406
    00407   /* Using wheel on caption/shade-box shades or unshades the window. */
    00408   if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
    00409     w->SetShaded(!w->IsShaded());
    00410     return;
    00411   }
    00412
    00413   /* Scroll the widget attached to the scrollbar. */
    00414   Scrollbar *sb = nwid->FindScrollbar(w);
    00415   if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
    00416     sb->UpdatePosition(wheel);
    00417     w->SetDirty();
    00418   }
    00419 }
    00420
    00433 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
    00434 {
    00435   const Window *v;
    00436   FOR_ALL_WINDOWS_FROM_BACK_FROM(v, w->z_front) {
    00437     if (right > v->left &&
    00438         bottom > v->top &&
    00439         left < v->left + v->width &&
    00440         top < v->top + v->height) {
    00441       /* v and rectangle intersect with eeach other */
    00442       int x;
    00443
    00444       if (left < (x = v->left)) {
    00445         DrawOverlappedWindow(w, left, top, x, bottom);
    00446         DrawOverlappedWindow(w, x, top, right, bottom);
    00447         return;
    00448       }
    00449
    00450       if (right > (x = v->left + v->width)) {
    00451         DrawOverlappedWindow(w, left, top, x, bottom);
    00452         DrawOverlappedWindow(w, x, top, right, bottom);
    00453         return;
    00454       }
    00455
    00456       if (top < (x = v->top)) {
    00457         DrawOverlappedWindow(w, left, top, right, x);
    00458         DrawOverlappedWindow(w, left, x, right, bottom);
    00459         return;
    00460       }
    00461
    00462       if (bottom > (x = v->top + v->height)) {
    00463         DrawOverlappedWindow(w, left, top, right, x);
    00464         DrawOverlappedWindow(w, left, x, right, bottom);
    00465         return;
    00466       }
    00467
    00468       return;
    00469     }
    00470   }
    00471
    00472   /* Setup blitter, and dispatch a repaint event to window *wz */
    00473   DrawPixelInfo *dp = _cur_dpi;
    00474   dp->width = right - left;
    00475   dp->height = bottom - top;
    00476   dp->left = left - w->left;
    00477   dp->top = top - w->top;
    00478   dp->pitch = _screen.pitch;
    00479   dp->dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
    00480   dp->zoom = ZOOM_LVL_NORMAL;
    00481   w->OnPaint();
    00482 }
    00483
    00492 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
    00493 {
    00494   Window *w;
    00495   DrawPixelInfo bk;
    00496   _cur_dpi = &bk;
    00497
    00498   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00499     if (right > w->left &&
    00500         bottom > w->top &&
    00501         left < w->left + w->width &&
    00502         top < w->top + w->height) {
    00503       /* Window w intersects with the rectangle => needs repaint */
    00504       DrawOverlappedWindow(w, left, top, right, bottom);
    00505     }
    00506   }
    00507 }
    00508
    00513 void Window::SetDirty() const
    00514 {
    00515   SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
    00516 }
    00517
    00523 void Window::ReInit(int rx, int ry)
    00524 {
    00525   this->SetDirty(); // Mark whole current window as dirty.
    00526
    00527   /* Save current size. */
    00528   int window_width  = this->width;
    00529   int window_height = this->height;
    00530
    00531   this->OnInit();
    00532   /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
    00533   this->nested_root->SetupSmallestSize(this, false);
    00534   this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _dynlang.text_dir == TD_RTL);
    00535   this->width  = this->nested_root->smallest_x;
    00536   this->height = this->nested_root->smallest_y;
    00537   this->resize.step_width  = this->nested_root->resize_x;
    00538   this->resize.step_height = this->nested_root->resize_y;
    00539
    00540   /* Resize as close to the original size + requested resize as possible. */
    00541   window_width  = max(window_width  + rx, this->width);
    00542   window_height = max(window_height + ry, this->height);
    00543   int dx = (this->resize.step_width  == 0) ? 0 : window_width  - this->width;
    00544   int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
    00545   /* dx and dy has to go by step.. calculate it.
    00546    * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
    00547   if (this->resize.step_width  > 1) dx -= dx % (int)this->resize.step_width;
    00548   if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
    00549
    00550   ResizeWindow(this, dx, dy);
    00551   /* ResizeWindow() does this->SetDirty() already, no need to do it again here. */
    00552 }
    00553
    00558 void Window::SetShaded(bool make_shaded)
    00559 {
    00560   if (this->shade_select == NULL) return;
    00561
    00562   int desired = make_shaded ? SZSP_HORIZONTAL : 0;
    00563   if (this->shade_select->shown_plane != desired) {
    00564     if (make_shaded) {
    00565       this->unshaded_size.width  = this->width;
    00566       this->unshaded_size.height = this->height;
    00567       this->shade_select->SetDisplayedPlane(desired);
    00568       this->ReInit(0, -this->height);
    00569     } else {
    00570       this->shade_select->SetDisplayedPlane(desired);
    00571       int dx = ((int)this->unshaded_size.width  > this->width)  ? (int)this->unshaded_size.width  - this->width  : 0;
    00572       int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
    00573       this->ReInit(dx, dy);
    00574     }
    00575   }
    00576 }
    00577
    00583 static Window *FindChildWindow(const Window *w, WindowClass wc)
    00584 {
    00585   Window *v;
    00586   FOR_ALL_WINDOWS_FROM_BACK(v) {
    00587     if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
    00588   }
    00589
    00590   return NULL;
    00591 }
    00592
    00597 void Window::DeleteChildWindows(WindowClass wc) const
    00598 {
    00599   Window *child = FindChildWindow(this, wc);
    00600   while (child != NULL) {
    00601     delete child;
    00602     child = FindChildWindow(this, wc);
    00603   }
    00604 }
    00605
    00609 Window::~Window()
    00610 {
    00611   if (_thd.place_mode != HT_NONE &&
    00612       _thd.window_class == this->window_class &&
    00613       _thd.window_number == this->window_number) {
    00614     ResetObjectToPlace();
    00615   }
    00616
    00617   /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
    00618   if (_mouseover_last_w == this) _mouseover_last_w = NULL;
    00619
    00620   /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */
    00621   if (_focused_window == this) _focused_window = NULL;
    00622
    00623   this->DeleteChildWindows();
    00624
    00625   if (this->viewport != NULL) DeleteWindowViewport(this);
    00626
    00627   this->SetDirty();
    00628
    00629   free(this->nested_array); // Contents is released through deletion of #nested_root.
    00630   delete this->nested_root;
    00631
    00632   this->window_class = WC_INVALID;
    00633 }
    00634
    00641 Window *FindWindowById(WindowClass cls, WindowNumber number)
    00642 {
    00643   Window *w;
    00644   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00645     if (w->window_class == cls && w->window_number == number) return w;
    00646   }
    00647
    00648   return NULL;
    00649 }
    00650
    00657 Window *FindWindowByClass(WindowClass cls)
    00658 {
    00659   Window *w;
    00660   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00661     if (w->window_class == cls) return w;
    00662   }
    00663
    00664   return NULL;
    00665 }
    00666
    00673 void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
    00674 {
    00675   Window *w = FindWindowById(cls, number);
    00676   if (force || w == NULL ||
    00677       (w->flags4 & WF_STICKY) == 0) {
    00678     delete w;
    00679   }
    00680 }
    00681
    00686 void DeleteWindowByClass(WindowClass cls)
    00687 {
    00688   Window *w;
    00689
    00690 restart_search:
    00691   /* When we find the window to delete, we need to restart the search
    00692    * as deleting this window could cascade in deleting (many) others
    00693    * anywhere in the z-array */
    00694   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00695     if (w->window_class == cls) {
    00696       delete w;
    00697       goto restart_search;
    00698     }
    00699   }
    00700 }
    00701
    00706 void DeleteCompanyWindows(CompanyID id)
    00707 {
    00708   Window *w;
    00709
    00710 restart_search:
    00711   /* When we find the window to delete, we need to restart the search
    00712    * as deleting this window could cascade in deleting (many) others
    00713    * anywhere in the z-array */
    00714   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00715     if (w->owner == id) {
    00716       delete w;
    00717       goto restart_search;
    00718     }
    00719   }
    00720
    00721   /* Also delete the company specific windows, that don't have a company-colour */
    00722   DeleteWindowById(WC_BUY_COMPANY, id);
    00723 }
    00724
    00730 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
    00731 {
    00732   Window *w;
    00733   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00734     if (w->owner != old_owner) continue;
    00735
    00736     switch (w->window_class) {
    00737       case WC_COMPANY_COLOUR:
    00738       case WC_FINANCES:
    00739       case WC_STATION_LIST:
    00740       case WC_TRAINS_LIST:
    00741       case WC_ROADVEH_LIST:
    00742       case WC_SHIPS_LIST:
    00743       case WC_AIRCRAFT_LIST:
    00744       case WC_BUY_COMPANY:
    00745       case WC_COMPANY:
    00746         continue;
    00747
    00748       default:
    00749         w->owner = new_owner;
    00750         break;
    00751     }
    00752   }
    00753 }
    00754
    00755 static void BringWindowToFront(Window *w);
    00756
    00762 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number)
    00763 {
    00764   Window *w = FindWindowById(cls, number);
    00765
    00766   if (w != NULL) {
    00767     if (w->IsShaded()) w->SetShaded(false); // Restore original window size if it was shaded.
    00768
    00769     w->flags4 |= WF_WHITE_BORDER_MASK;
    00770     BringWindowToFront(w);
    00771     w->SetDirty();
    00772   }
    00773
    00774   return w;
    00775 }
    00776
    00777 static inline bool IsVitalWindow(const Window *w)
    00778 {
    00779   switch (w->window_class) {
    00780     case WC_MAIN_TOOLBAR:
    00781     case WC_STATUS_BAR:
    00782     case WC_NEWS_WINDOW:
    00783     case WC_SEND_NETWORK_MSG:
    00784       return true;
    00785
    00786     default:
    00787       return false;
    00788   }
    00789 }
    00790
    00799 static void BringWindowToFront(Window *w)
    00800 {
    00801   Window *v = _z_front_window;
    00802
    00803   /* Bring the window just below the vital windows */
    00804   for (; v != NULL && v != w && IsVitalWindow(v); v = v->z_back) { }
    00805
    00806   if (v == NULL || w == v) return; // window is already in the right position
    00807
    00808   /* w cannot be at the top already! */
    00809   assert(w != _z_front_window);
    00810
    00811   if (w->z_back == NULL) {
    00812     _z_back_window = w->z_front;
    00813   } else {
    00814     w->z_back->z_front = w->z_front;
    00815   }
    00816   w->z_front->z_back = w->z_back;
    00817
    00818   w->z_front = v->z_front;
    00819   w->z_back = v;
    00820
    00821   if (v->z_front == NULL) {
    00822     _z_front_window = w;
    00823   } else {
    00824     v->z_front->z_back = w;
    00825   }
    00826   v->z_front = w;
    00827
    00828   w->SetDirty();
    00829 }
    00830
    00840 void Window::InitializeData(WindowClass cls, int window_number, uint32 desc_flags)
    00841 {
    00842   /* Set up window properties; some of them are needed to set up smallest size below */
    00843   this->window_class = cls;
    00844   this->flags4 |= WF_WHITE_BORDER_MASK; // just opened windows have a white border
    00845   this->owner = INVALID_OWNER;
    00846   this->nested_focus = NULL;
    00847   this->window_number = window_number;
    00848   this->desc_flags = desc_flags;
    00849
    00850   this->OnInit();
    00851   /* Initialize nested widget tree. */
    00852   if (this->nested_array == NULL) {
    00853     this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
    00854     this->nested_root->SetupSmallestSize(this, true);
    00855   } else {
    00856     this->nested_root->SetupSmallestSize(this, false);
    00857   }
    00858   /* Initialize to smallest size. */
    00859   this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _dynlang.text_dir == TD_RTL);
    00860
    00861   /* Further set up window properties,
    00862    * this->left, this->top, this->width, this->height, this->resize.width, and this->resize.height are initialized later. */
    00863   this->resize.step_width  = this->nested_root->resize_x;
    00864   this->resize.step_height = this->nested_root->resize_y;
    00865
    00866   /* Give focus to the opened window unless it is the OSK window or a text box
    00867    * of focused window has focus (so we don't interrupt typing). But if the new
    00868    * window has a text box, then take focus anyway. */
    00869   if (this->window_class != WC_OSK && (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != NULL)) SetFocusedWindow(this);
    00870
    00871   /* Hacky way of specifying always-on-top windows. These windows are
    00872    * always above other windows because they are moved below them.
    00873    * status-bar is above news-window because it has been created earlier.
    00874    * Also, as the chat-window is excluded from this, it will always be
    00875    * the last window, thus always on top.
    00876    * XXX - Yes, ugly, probably needs something like w->always_on_top flag
    00877    * to implement correctly, but even then you need some kind of distinction
    00878    * between on-top of chat/news and status windows, because these conflict */
    00879   Window *w = _z_front_window;
    00880   if (w != NULL && this->window_class != WC_SEND_NETWORK_MSG && this->window_class != WC_HIGHSCORE && this->window_class != WC_ENDSCREEN) {
    00881     if (FindWindowById(WC_MAIN_TOOLBAR, 0)     != NULL) w = w->z_back;
    00882     if (FindWindowById(WC_STATUS_BAR, 0)       != NULL) w = w->z_back;
    00883     if (FindWindowById(WC_NEWS_WINDOW, 0)      != NULL) w = w->z_back;
    00884     if (FindWindowByClass(WC_SEND_NETWORK_MSG) != NULL) w = w->z_back;
    00885
    00886     if (w == NULL) {
    00887       _z_back_window->z_front = this;
    00888       this->z_back = _z_back_window;
    00889       _z_back_window = this;
    00890     } else {
    00891       if (w->z_front == NULL) {
    00892         _z_front_window = this;
    00893       } else {
    00894         this->z_front = w->z_front;
    00895         w->z_front->z_back = this;
    00896       }
    00897
    00898       this->z_back = w;
    00899       w->z_front = this;
    00900     }
    00901   } else {
    00902     this->z_back = _z_front_window;
    00903     if (_z_front_window != NULL) {
    00904       _z_front_window->z_front = this;
    00905     } else {
    00906       _z_back_window = this;
    00907     }
    00908     _z_front_window = this;
    00909   }
    00910 }
    00911
    00919 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
    00920 {
    00921   this->left = x;
    00922   this->top = y;
    00923   this->width = sm_width;
    00924   this->height = sm_height;
    00925 }
    00926
    00937 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
    00938 {
    00939   def_width  = max(def_width,  this->width); // Don't allow default size to be smaller than smallest size
    00940   def_height = max(def_height, this->height);
    00941   /* Try to make windows smaller when our window is too small.
    00942    * w->(width|height) is normally the same as min_(width|height),
    00943    * but this way the GUIs can be made a little more dynamic;
    00944    * one can use the same spec for multiple windows and those
    00945    * can then determine the real minimum size of the window. */
    00946   if (this->width != def_width || this->height != def_height) {
    00947     /* Think about the overlapping toolbars when determining the minimum window size */
    00948     int free_height = _screen.height;
    00949     const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
    00950     if (wt != NULL) free_height -= wt->height;
    00951     wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
    00952     if (wt != NULL) free_height -= wt->height;
    00953
    00954     int enlarge_x = max(min(def_width  - this->width,  _screen.width - this->width),  0);
    00955     int enlarge_y = max(min(def_height - this->height, free_height   - this->height), 0);
    00956
    00957     /* X and Y has to go by step.. calculate it.
    00958      * The cast to int is necessary else x/y are implicitly casted to
    00959      * unsigned int, which won't work. */
    00960     if (this->resize.step_width  > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
    00961     if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
    00962
    00963     ResizeWindow(this, enlarge_x, enlarge_y);
    00964     /* ResizeWindow() calls this->OnResize(). */
    00965   } else {
    00966     /* Always call OnResize; that way the scrollbars and matrices get initialized. */
    00967     this->OnResize();
    00968   }
    00969
    00970   int nx = this->left;
    00971   int ny = this->top;
    00972
    00973   if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
    00974
    00975   const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
    00976   ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
    00977   nx = max(nx, 0);
    00978
    00979   if (this->viewport != NULL) {
    00980     this->viewport->left += nx - this->left;
    00981     this->viewport->top  += ny - this->top;
    00982   }
    00983   this->left = nx;
    00984   this->top = ny;
    00985
    00986   this->SetDirty();
    00987 }
    00988
    01000 static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos)
    01001 {
    01002   int right  = width + left;
    01003   int bottom = height + top;
    01004
    01005   if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height) return false;
    01006
    01007   /* Make sure it is not obscured by any window. */
    01008   const Window *w;
    01009   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01010     if (w->window_class == WC_MAIN_WINDOW) continue;
    01011
    01012     if (right > w->left &&
    01013         w->left + w->width > left &&
    01014         bottom > w->top &&
    01015         w->top + w->height > top) {
    01016       return false;
    01017     }
    01018   }
    01019
    01020   pos.x = left;
    01021   pos.y = top;
    01022   return true;
    01023 }
    01024
    01036 static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos)
    01037 {
    01038   /* Left part of the rectangle may be at most 1/4 off-screen,
    01039    * right part of the rectangle may be at most 1/2 off-screen
    01040    */
    01041   if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
    01042   /* Bottom part of the rectangle may be at most 1/4 off-screen */
    01043   if (top < 22 || top > _screen.height - (height >> 2)) return false;
    01044
    01045   /* Make sure it is not obscured by any window. */
    01046   const Window *w;
    01047   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01048     if (w->window_class == WC_MAIN_WINDOW) continue;
    01049
    01050     if (left + width > w->left &&
    01051         w->left + w->width > left &&
    01052         top + height > w->top &&
    01053         w->top + w->height > top) {
    01054       return false;
    01055     }
    01056   }
    01057
    01058   pos.x = left;
    01059   pos.y = top;
    01060   return true;
    01061 }
    01062
    01069 static Point GetAutoPlacePosition(int width, int height)
    01070 {
    01071   Point pt;
    01072
    01073   /* First attempt, try top-left of the screen */
    01074   if (IsGoodAutoPlace1(0, 24, width, height, pt)) return pt;
    01075
    01076   /* Second attempt, try around all existing windows with a distance of 2 pixels.
    01077    * The new window must be entirely on-screen, and not overlap with an existing window.
    01078    * Eight starting points are tried, two at each corner.
    01079    */
    01080   const Window *w;
    01081   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01082     if (w->window_class == WC_MAIN_WINDOW) continue;
    01083
    01084     if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt;
    01085     if (IsGoodAutoPlace1(w->left - width - 2,    w->top, width, height, pt)) return pt;
    01086     if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt;
    01087     if (IsGoodAutoPlace1(w->left, w->top - height - 2,    width, height, pt)) return pt;
    01088     if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt;
    01089     if (IsGoodAutoPlace1(w->left - width - 2,    w->top + w->height - height, width, height, pt)) return pt;
    01090     if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt;
    01091     if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2,    width, height, pt)) return pt;
    01092   }
    01093
    01094   /* Third attempt, try around all existing windows with a distance of 2 pixels.
    01095    * The new window may be partly off-screen, and must not overlap with an existing window.
    01096    * Only four starting points are tried.
    01097    */
    01098   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01099     if (w->window_class == WC_MAIN_WINDOW) continue;
    01100
    01101     if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt;
    01102     if (IsGoodAutoPlace2(w->left - width - 2,    w->top, width, height, pt)) return pt;
    01103     if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt;
    01104     if (IsGoodAutoPlace2(w->left, w->top - height - 2,    width, height, pt)) return pt;
    01105   }
    01106
    01107   /* Fourth and final attempt, put window at diagonal starting from (0, 24), try multiples
    01108    * of (+5, +5)
    01109    */
    01110   int left = 0, top = 24;
    01111
    01112 restart:
    01113   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01114     if (w->left == left && w->top == top) {
    01115       left += 5;
    01116       top += 5;
    01117       goto restart;
    01118     }
    01119   }
    01120
    01121   pt.x = left;
    01122   pt.y = top;
    01123   return pt;
    01124 }
    01125
    01132 Point GetToolbarAlignedWindowPosition(int window_width)
    01133 {
    01134   const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
    01135   assert(w != NULL);
    01136   Point pt = { _dynlang.text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
    01137   return pt;
    01138 }
    01139
    01157 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    01158 {
    01159   Point pt;
    01160   const Window *w;
    01161
    01162   int16 default_width  = max(desc->default_width,  sm_width);
    01163   int16 default_height = max(desc->default_height, sm_height);
    01164
    01165   if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ &&
    01166       (w = FindWindowById(desc->parent_cls, window_number)) != NULL &&
    01167       w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) {
    01168
    01169     pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10);
    01170     if (pt.x > _screen.width + 10 - default_width) {
    01171       pt.x = (_screen.width + 10 - default_width) - 20;
    01172     }
    01173     pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? w->height : 10);
    01174     return pt;
    01175   }
    01176
    01177   switch (desc->default_pos) {
    01178     case WDP_ALIGN_TOOLBAR: // Align to the toolbar
    01179       return GetToolbarAlignedWindowPosition(default_width);
    01180
    01181     case WDP_AUTO: // Find a good automatic position for the window
    01182       return GetAutoPlacePosition(default_width, default_height);
    01183
    01184     case WDP_CENTER: // Centre the window horizontally
    01185       pt.x = (_screen.width - default_width) / 2;
    01186       pt.y = (_screen.height - default_height) / 2;
    01187       break;
    01188
    01189     case WDP_MANUAL:
    01190       pt.x = 0;
    01191       pt.y = 0;
    01192       break;
    01193
    01194     default:
    01195       NOT_REACHED();
    01196   }
    01197
    01198   return pt;
    01199 }
    01200
    01201 /* virtual */ Point Window::OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    01202 {
    01203   return LocalGetWindowPlacement(desc, sm_width, sm_height, window_number);
    01204 }
    01205
    01214 void Window::CreateNestedTree(const WindowDesc *desc, bool fill_nested)
    01215 {
    01216   int biggest_index = -1;
    01217   this->nested_root = MakeWindowNWidgetTree(desc->nwid_parts, desc->nwid_length, &biggest_index, &this->shade_select);
    01218   this->nested_array_size = (uint)(biggest_index + 1);
    01219
    01220   if (fill_nested) {
    01221     this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
    01222     this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
    01223   }
    01224 }
    01225
    01231 void Window::FinishInitNested(const WindowDesc *desc, WindowNumber window_number)
    01232 {
    01233   this->InitializeData(desc->cls, window_number, desc->flags);
    01234   Point pt = this->OnInitialPosition(desc, this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
    01235   this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
    01236   this->FindWindowPlacementAndResize(desc->default_width, desc->default_height);
    01237 }
    01238
    01244 void Window::InitNested(const WindowDesc *desc, WindowNumber window_number)
    01245 {
    01246   this->CreateNestedTree(desc, false);
    01247   this->FinishInitNested(desc, window_number);
    01248 }
    01249
    01251 Window::Window() : hscroll(false), vscroll(true), vscroll2(true)
    01252 {
    01253 }
    01254
    01260 Window *FindWindowFromPt(int x, int y)
    01261 {
    01262   Window *w;
    01263   FOR_ALL_WINDOWS_FROM_FRONT(w) {
    01264     if (IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
    01265       return w;
    01266     }
    01267   }
    01268
    01269   return NULL;
    01270 }
    01271
    01275 void InitWindowSystem()
    01276 {
    01277   IConsoleClose();
    01278
    01279   _z_back_window = NULL;
    01280   _z_front_window = NULL;
    01281   _focused_window = NULL;
    01282   _mouseover_last_w = NULL;
    01283   _scrolling_viewport = 0;
    01284
    01285   NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
    01286 }
    01287
    01291 void UnInitWindowSystem()
    01292 {
    01293   Window *w;
    01294   FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
    01295
    01296   for (w = _z_front_window; w != NULL; /* nothing */) {
    01297     Window *to_del = w;
    01298     w = w->z_back;
    01299     free(to_del);
    01300   }
    01301
    01302   _z_front_window = NULL;
    01303   _z_back_window = NULL;
    01304 }
    01305
    01309 void ResetWindowSystem()
    01310 {
    01311   UnInitWindowSystem();
    01312   InitWindowSystem();
    01313   _thd.pos.x = 0;
    01314   _thd.pos.y = 0;
    01315   _thd.new_pos.x = 0;
    01316   _thd.new_pos.y = 0;
    01317 }
    01318
    01319 static void DecreaseWindowCounters()
    01320 {
    01321   Window *w;
    01322   FOR_ALL_WINDOWS_FROM_FRONT(w) {
    01323     /* Unclick scrollbar buttons if they are pressed. */
    01324     if (w->flags4 & (WF_SCROLL_DOWN | WF_SCROLL_UP)) {
    01325       w->flags4 &= ~(WF_SCROLL_DOWN | WF_SCROLL_UP);
    01326       w->SetDirty();
    01327     }
    01328     w->OnMouseLoop();
    01329   }
    01330
    01331   FOR_ALL_WINDOWS_FROM_FRONT(w) {
    01332     if ((w->flags4 & WF_TIMEOUT_MASK) && !(--w->flags4 & WF_TIMEOUT_MASK)) {
    01333       w->OnTimeout();
    01334       if (w->desc_flags & WDF_UNCLICK_BUTTONS) w->RaiseButtons(true);
    01335     }
    01336   }
    01337 }
    01338
    01339 Window *GetCallbackWnd()
    01340 {
    01341   return FindWindowById(_thd.window_class, _thd.window_number);
    01342 }
    01343
    01344 static void HandlePlacePresize()
    01345 {
    01346   if (_special_mouse_mode != WSM_PRESIZE) return;
    01347
    01348   Window *w = GetCallbackWnd();
    01349   if (w == NULL) return;
    01350
    01351   Point pt = GetTileBelowCursor();
    01352   if (pt.x == -1) {
    01353     _thd.selend.x = -1;
    01354     return;
    01355   }
    01356
    01357   w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
    01358 }
    01359
    01360 static bool HandleDragDrop()
    01361 {
    01362   if (_special_mouse_mode != WSM_DRAGDROP) return true;
    01363   if (_left_button_down) return false;
    01364
    01365   Window *w = GetCallbackWnd();
    01366
    01367   if (w != NULL) {
    01368     /* send an event in client coordinates. */
    01369     Point pt;
    01370     pt.x = _cursor.pos.x - w->left;
    01371     pt.y = _cursor.pos.y - w->top;
    01372     w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
    01373   }
    01374
    01375   ResetObjectToPlace();
    01376
    01377   return false;
    01378 }
    01379
    01380 static bool HandleMouseOver()
    01381 {
    01382   Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
    01383
    01384   /* We changed window, put a MOUSEOVER event to the last window */
    01385   if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
    01386     /* Reset mouse-over coordinates of previous window */
    01387     Point pt = { -1, -1 };
    01388     _mouseover_last_w->OnMouseOver(pt, 0);
    01389   }
    01390
    01391   /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
    01392   _mouseover_last_w = w;
    01393
    01394   if (w != NULL) {
    01395     /* send an event in client coordinates. */
    01396     Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
    01397     const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
    01398     if (widget != NULL) w->OnMouseOver(pt, widget->index);
    01399   }
    01400
    01401   /* Mouseover never stops execution */
    01402   return true;
    01403 }
    01404
    01414 void ResizeWindow(Window *w, int delta_x, int delta_y)
    01415 {
    01416   if (delta_x != 0 || delta_y != 0) {
    01417     w->SetDirty();
    01418
    01419     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);
    01420     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);
    01421     assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
    01422     assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
    01423
    01424     w->nested_root->AssignSizePosition(ST_RESIZE, 0, 0, w->nested_root->smallest_x + new_xinc, w->nested_root->smallest_y + new_yinc, _dynlang.text_dir == TD_RTL);
    01425     w->width  = w->nested_root->current_x;
    01426     w->height = w->nested_root->current_y;
    01427   }
    01428
    01429   /* Always call OnResize to make sure everything is initialised correctly if it needs to be. */
    01430   w->OnResize();
    01431   w->SetDirty();
    01432 }
    01433
    01438 int GetMainViewTop()
    01439 {
    01440   Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
    01441   return (w == NULL) ? 0 : w->top + w->height;
    01442 }
    01443
    01448 int GetMainViewBottom()
    01449 {
    01450   Window *w = FindWindowById(WC_STATUS_BAR, 0);
    01451   return (w == NULL) ? _screen.height : w->top;
    01452 }
    01453
    01455 static const int MIN_VISIBLE_TITLE_BAR = 13;
    01456
    01458 enum PreventHideDirection {
    01459   PHD_UP,
    01460   PHD_DOWN,
    01461 };
    01462
    01473 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
    01474 {
    01475   if (v == NULL) return;
    01476
    01477   int v_bottom = v->top + v->height;
    01478   int v_right = v->left + v->width;
    01479   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.
    01480
    01481   if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return; // Above v is enough space
    01482   if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return; // Below v is enough space
    01483
    01484   /* Vertically, the rectangle is hidden behind v. */
    01485   if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) { // At left of v.
    01486     if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // But enough room, force it to a safe position.
    01487     return;
    01488   }
    01489   if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) { // At right of v.
    01490     if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y; // Not enough room, force it to a safe position.
    01491     return;
    01492   }
    01493
    01494   /* Horizontally also hidden, force movement to a safe area. */
    01495   if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) { // Coming from the left, and enough room there.
    01496     *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
    01497   } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) { // Coming from the right, and enough room there.
    01498     *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
    01499   } else {
    01500     *ny = safe_y;
    01501   }
    01502 }
    01503
    01504 static bool _dragging_window;
    01505
    01506 static bool HandleWindowDragging()
    01507 {
    01508   /* Get out immediately if no window is being dragged at all. */
    01509   if (!_dragging_window) return true;
    01510
    01511   /* If button still down, but cursor hasn't moved, there is nothing to do. */
    01512   if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return false;
    01513
    01514   /* Otherwise find the window... */
    01515   Window *w;
    01516   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01517     if (w->flags4 & WF_DRAGGING) {
    01518       /* Stop the dragging if the left mouse button was released */
    01519       if (!_left_button_down) {
    01520         w->flags4 &= ~WF_DRAGGING;
    01521         break;
    01522       }
    01523
    01524       w->SetDirty();
    01525
    01526       int x = _cursor.pos.x + _drag_delta.x;
    01527       int y = _cursor.pos.y + _drag_delta.y;
    01528       int nx = x;
    01529       int ny = y;
    01530
    01531       if (_settings_client.gui.window_snap_radius != 0) {
    01532         const Window *v;
    01533
    01534         int hsnap = _settings_client.gui.window_snap_radius;
    01535         int vsnap = _settings_client.gui.window_snap_radius;
    01536         int delta;
    01537
    01538         FOR_ALL_WINDOWS_FROM_BACK(v) {
    01539           if (v == w) continue; // Don't snap at yourself
    01540
    01541           if (y + w->height > v->top && y < v->top + v->height) {
    01542             /* Your left border <-> other right border */
    01543             delta = abs(v->left + v->width - x);
    01544             if (delta <= hsnap) {
    01545               nx = v->left + v->width;
    01546               hsnap = delta;
    01547             }
    01548
    01549             /* Your right border <-> other left border */
    01550             delta = abs(v->left - x - w->width);
    01551             if (delta <= hsnap) {
    01552               nx = v->left - w->width;
    01553               hsnap = delta;
    01554             }
    01555           }
    01556
    01557           if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
    01558             /* Your left border <-> other left border */
    01559             delta = abs(v->left - x);
    01560             if (delta <= hsnap) {
    01561               nx = v->left;
    01562               hsnap = delta;
    01563             }
    01564
    01565             /* Your right border <-> other right border */
    01566             delta = abs(v->left + v->width - x - w->width);
    01567             if (delta <= hsnap) {
    01568               nx = v->left + v->width - w->width;
    01569               hsnap = delta;
    01570             }
    01571           }
    01572
    01573           if (x + w->width > v->left && x < v->left + v->width) {
    01574             /* Your top border <-> other bottom border */
    01575             delta = abs(v->top + v->height - y);
    01576             if (delta <= vsnap) {
    01577               ny = v->top + v->height;
    01578               vsnap = delta;
    01579             }
    01580
    01581             /* Your bottom border <-> other top border */
    01582             delta = abs(v->top - y - w->height);
    01583             if (delta <= vsnap) {
    01584               ny = v->top - w->height;
    01585               vsnap = delta;
    01586             }
    01587           }
    01588
    01589           if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
    01590             /* Your top border <-> other top border */
    01591             delta = abs(v->top - y);
    01592             if (delta <= vsnap) {
    01593               ny = v->top;
    01594               vsnap = delta;
    01595             }
    01596
    01597             /* Your bottom border <-> other bottom border */
    01598             delta = abs(v->top + v->height - y - w->height);
    01599             if (delta <= vsnap) {
    01600               ny = v->top + v->height - w->height;
    01601               vsnap = delta;
    01602             }
    01603           }
    01604         }
    01605       }
    01606
    01607       /* Search for the title bar rectangle. */
    01608       Rect caption_rect;
    01609       const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
    01610       assert(caption != NULL);
    01611       caption_rect.left   = caption->pos_x;
    01612       caption_rect.right  = caption->pos_x + caption->current_x;
    01613       caption_rect.top    = caption->pos_y;
    01614       caption_rect.bottom = caption->pos_y + caption->current_y;
    01615
    01616       /* Make sure the window doesn't leave the screen */
    01617       nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
    01618       ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
    01619
    01620       /* Make sure the title bar isn't hidden behind the main tool bar or the status bar. */
    01621       PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
    01622       PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR,   0), w->left, PHD_UP);
    01623
    01624       if (w->viewport != NULL) {
    01625         w->viewport->left += nx - w->left;
    01626         w->viewport->top  += ny - w->top;
    01627       }
    01628       w->left = nx;
    01629       w->top  = ny;
    01630
    01631       w->SetDirty();
    01632       return false;
    01633     } else if (w->flags4 & WF_SIZING) {
    01634       /* Stop the sizing if the left mouse button was released */
    01635       if (!_left_button_down) {
    01636         w->flags4 &= ~WF_SIZING;
    01637         w->SetDirty();
    01638         break;
    01639       }
    01640
    01641       /* Compute difference in pixels between cursor position and reference point in the window.
    01642        * If resizing the left edge of the window, moving to the left makes the window bigger not smaller.
    01643        */
    01644       int x, y = _cursor.pos.y - _drag_delta.y;
    01645       if (w->flags4 & WF_SIZING_LEFT) {
    01646         x = _drag_delta.x - _cursor.pos.x;
    01647       } else {
    01648         x = _cursor.pos.x - _drag_delta.x;
    01649       }
    01650
    01651       /* resize.step_width and/or resize.step_height may be 0, which means no resize is possible. */
    01652       if (w->resize.step_width  == 0) x = 0;
    01653       if (w->resize.step_height == 0) y = 0;
    01654
    01655       /* X and Y has to go by step.. calculate it.
    01656        * The cast to int is necessary else x/y are implicitly casted to
    01657        * unsigned int, which won't work. */
    01658       if (w->resize.step_width  > 1) x -= x % (int)w->resize.step_width;
    01659       if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
    01660
    01661       /* Check that we don't go below the minimum set size */
    01662       if ((int)w->width + x < (int)w->nested_root->smallest_x) {
    01663         x = w->nested_root->smallest_x - w->width;
    01664       }
    01665       if ((int)w->height + y < (int)w->nested_root->smallest_y) {
    01666         y = w->nested_root->smallest_y - w->height;
    01667       }
    01668
    01669       /* Window already on size */
    01670       if (x == 0 && y == 0) return false;
    01671
    01672       /* Now find the new cursor pos.. this is NOT _cursor, because we move in steps. */
    01673       _drag_delta.y += y;
    01674       if ((w->flags4 & WF_SIZING_LEFT) && x != 0) {
    01675         _drag_delta.x -= x; // x > 0 -> window gets longer -> left-edge moves to left -> subtract x to get new position.
    01676         w->SetDirty();
    01677         w->left -= x;  // If dragging left edge, move left window edge in opposite direction by the same amount.
    01678         /* ResizeWindow() below ensures marking new position as dirty. */
    01679       } else {
    01680         _drag_delta.x += x;
    01681       }
    01682
    01683       /* ResizeWindow sets both pre- and after-size to dirty for redrawal */
    01684       ResizeWindow(w, x, y);
    01685       return false;
    01686     }
    01687   }
    01688
    01689   _dragging_window = false;
    01690   return false;
    01691 }
    01692
    01697 static void StartWindowDrag(Window *w)
    01698 {
    01699   w->flags4 |= WF_DRAGGING;
    01700   _dragging_window = true;
    01701
    01702   _drag_delta.x = w->left - _cursor.pos.x;
    01703   _drag_delta.y = w->top  - _cursor.pos.y;
    01704
    01705   BringWindowToFront(w);
    01706   DeleteWindowById(WC_DROPDOWN_MENU, 0);
    01707 }
    01708
    01714 static void StartWindowSizing(Window *w, bool to_left)
    01715 {
    01716   w->flags4 |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
    01717   _dragging_window = true;
    01718
    01719   _drag_delta.x = _cursor.pos.x;
    01720   _drag_delta.y = _cursor.pos.y;
    01721
    01722   BringWindowToFront(w);
    01723   DeleteWindowById(WC_DROPDOWN_MENU, 0);
    01724 }
    01725
    01726
    01727 static bool HandleScrollbarScrolling()
    01728 {
    01729   Window *w;
    01730
    01731   /* Get out quickly if no item is being scrolled */
    01732   if (!_scrolling_scrollbar) return true;
    01733
    01734   /* Find the scrolling window */
    01735   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01736     if (w->flags4 & WF_SCROLL_MIDDLE) {
    01737       /* Abort if no button is clicked any more. */
    01738       if (!_left_button_down) {
    01739         w->flags4 &= ~WF_SCROLL_MIDDLE;
    01740         w->SetDirty();
    01741         break;
    01742       }
    01743
    01744       int i;
    01745       Scrollbar *sb;
    01746       bool rtl = false;
    01747
    01748       if (w->flags4 & WF_HSCROLL) {
    01749         sb = &w->hscroll;
    01750         i = _cursor.pos.x - _cursorpos_drag_start.x;
    01751         rtl = _dynlang.text_dir == TD_RTL;
    01752       } else if (w->flags4 & WF_SCROLL2) {
    01753         sb = &w->vscroll2;
    01754         i = _cursor.pos.y - _cursorpos_drag_start.y;
    01755       } else {
    01756         sb = &w->vscroll;
    01757         i = _cursor.pos.y - _cursorpos_drag_start.y;
    01758       }
    01759
    01760       /* Find the item we want to move to and make sure it's inside bounds. */
    01761       int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
    01762       if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
    01763       if (pos != sb->GetPosition()) {
    01764         sb->SetPosition(pos);
    01765         w->SetDirty();
    01766       }
    01767       return false;
    01768     }
    01769   }
    01770
    01771   _scrolling_scrollbar = false;
    01772   return false;
    01773 }
    01774
    01775 static bool HandleViewportScroll()
    01776 {
    01777   bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
    01778
    01779   if (!_scrolling_viewport) return true;
    01780
    01781   Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
    01782
    01783   if (!(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down)) || w == NULL) {
    01784     _cursor.fix_at = false;
    01785     _scrolling_viewport = false;
    01786     return true;
    01787   }
    01788
    01789   if (w == FindWindowById(WC_MAIN_WINDOW, 0) && w->viewport->follow_vehicle != INVALID_VEHICLE) {
    01790     /* If the main window is following a vehicle, then first let go of it! */
    01791     const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
    01792     ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
    01793     return true;
    01794   }
    01795
    01796   Point delta;
    01797   if (_settings_client.gui.reverse_scroll || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down)) {
    01798     delta.x = -_cursor.delta.x;
    01799     delta.y = -_cursor.delta.y;
    01800   } else {
    01801     delta.x = _cursor.delta.x;
    01802     delta.y = _cursor.delta.y;
    01803   }
    01804
    01805   if (scrollwheel_scrolling) {
    01806     /* We are using scrollwheels for scrolling */
    01807     delta.x = _cursor.h_wheel;
    01808     delta.y = _cursor.v_wheel;
    01809     _cursor.v_wheel = 0;
    01810     _cursor.h_wheel = 0;
    01811   }
    01812
    01813   /* Create a scroll-event and send it to the window */
    01814   if (delta.x != 0 || delta.y != 0) w->OnScroll(delta);
    01815
    01816   _cursor.delta.x = 0;
    01817   _cursor.delta.y = 0;
    01818   return false;
    01819 }
    01820
    01829 static bool MaybeBringWindowToFront(Window *w)
    01830 {
    01831   bool bring_to_front = false;
    01832
    01833   if (w->window_class == WC_MAIN_WINDOW ||
    01834       IsVitalWindow(w) ||
    01835       w->window_class == WC_TOOLTIPS ||
    01836       w->window_class == WC_DROPDOWN_MENU) {
    01837     return true;
    01838   }
    01839
    01840   /* Use unshaded window size rather than current size for shaded windows. */
    01841   int w_width  = w->width;
    01842   int w_height = w->height;
    01843   if (w->IsShaded()) {
    01844     w_width  = w->unshaded_size.width;
    01845     w_height = w->unshaded_size.height;
    01846   }
    01847
    01848   Window *u;
    01849   FOR_ALL_WINDOWS_FROM_BACK_FROM(u, w->z_front) {
    01850     /* A modal child will prevent the activation of the parent window */
    01851     if (u->parent == w && (u->desc_flags & WDF_MODAL)) {
    01852       u->flags4 |= WF_WHITE_BORDER_MASK;
    01853       u->SetDirty();
    01854       return false;
    01855     }
    01856
    01857     if (u->window_class == WC_MAIN_WINDOW ||
    01858         IsVitalWindow(u) ||
    01859         u->window_class == WC_TOOLTIPS ||
    01860         u->window_class == WC_DROPDOWN_MENU) {
    01861       continue;
    01862     }
    01863
    01864     /* Window sizes don't interfere, leave z-order alone */
    01865     if (w->left + w_width <= u->left ||
    01866         u->left + u->width <= w->left ||
    01867         w->top  + w_height <= u->top ||
    01868         u->top + u->height <= w->top) {
    01869       continue;
    01870     }
    01871
    01872     bring_to_front = true;
    01873   }
    01874
    01875   if (bring_to_front) BringWindowToFront(w);
    01876   return true;
    01877 }
    01878
    01882 void HandleKeypress(uint32 raw_key)
    01883 {
    01884   /*
    01885    * During the generation of the world, there might be
    01886    * another thread that is currently building for example
    01887    * a road. To not interfere with those tasks, we should
    01888    * NOT change the _current_company here.
    01889    *
    01890    * This is not necessary either, as the only events that
    01891    * can be handled are the 'close application' events
    01892    */
    01893   if (!IsGeneratingWorld()) _current_company = _local_company;
    01894
    01895   /* Setup event */
    01896   uint16 key     = GB(raw_key,  0, 16);
    01897   uint16 keycode = GB(raw_key, 16, 16);
    01898
    01899   /*
    01900    * The Unicode standard defines an area called the private use area. Code points in this
    01901    * area are reserved for private use and thus not portable between systems. For instance,
    01902    * Apple defines code points for the arrow keys in this area, but these are only printable
    01903    * on a system running OS X. We don't want these keys to show up in text fields and such,
    01904    * and thus we have to clear the unicode character when we encounter such a key.
    01905    */
    01906   if (key >= 0xE000 && key <= 0xF8FF) key = 0;
    01907
    01908   /*
    01909    * If both key and keycode is zero, we don't bother to process the event.
    01910    */
    01911   if (key == 0 && keycode == 0) return;
    01912
    01913   /* Check if the focused window has a focused editbox */
    01914   if (EditBoxInGlobalFocus()) {
    01915     /* All input will in this case go to the focused window */
    01916     if (_focused_window->OnKeyPress(key, keycode) == Window::ES_HANDLED) return;
    01917   }
    01918
    01919   /* Call the event, start with the uppermost window, but ignore the toolbar. */
    01920   Window *w;
    01921   FOR_ALL_WINDOWS_FROM_FRONT(w) {
    01922     if (w->window_class == WC_MAIN_TOOLBAR) continue;
    01923     if (w->OnKeyPress(key, keycode) == Window::ES_HANDLED) return;
    01924   }
    01925
    01926   w = FindWindowById(WC_MAIN_TOOLBAR, 0);
    01927   /* When there is no toolbar w is null, check for that */
    01928   if (w != NULL) w->OnKeyPress(key, keycode);
    01929 }
    01930
    01934 void HandleCtrlChanged()
    01935 {
    01936   /* Call the event, start with the uppermost window. */
    01937   Window *w;
    01938   FOR_ALL_WINDOWS_FROM_FRONT(w) {
    01939     if (w->OnCTRLStateChange() == Window::ES_HANDLED) return;
    01940   }
    01941 }
    01942
    01949 static int _input_events_this_tick = 0;
    01950
    01955 static void HandleAutoscroll()
    01956 {
    01957   if (_settings_client.gui.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) {
    01958     int x = _cursor.pos.x;
    01959     int y = _cursor.pos.y;
    01960     Window *w = FindWindowFromPt(x, y);
    01961     if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return;
    01962     ViewPort *vp = IsPtInWindowViewport(w, x, y);
    01963     if (vp != NULL) {
    01964       x -= vp->left;
    01965       y -= vp->top;
    01966
    01967       /* here allows scrolling in both x and y axis */
    01968 #define scrollspeed 3
    01969       if (x - 15 < 0) {
    01970         w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
    01971       } else if (15 - (vp->width - x) > 0) {
    01972         w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
    01973       }
    01974       if (y - 15 < 0) {
    01975         w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
    01976       } else if (15 - (vp->height - y) > 0) {
    01977         w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
    01978       }
    01979 #undef scrollspeed
    01980     }
    01981   }
    01982 }
    01983
    01984 enum MouseClick {
    01985   MC_NONE = 0,
    01986   MC_LEFT,
    01987   MC_RIGHT,
    01988   MC_DOUBLE_LEFT,
    01989
    01990   MAX_OFFSET_DOUBLE_CLICK = 5,
    01991   TIME_BETWEEN_DOUBLE_CLICK = 500,
    01992 };
    01993
    01994 extern bool VpHandlePlaceSizingDrag();
    01995
    01996 static void ScrollMainViewport(int x, int y)
    01997 {
    01998   if (_game_mode != GM_MENU) {
    01999     Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
    02000     assert(w);
    02001
    02002     w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
    02003     w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
    02004   }
    02005 }
    02006
    02016 static const int8 scrollamt[16][2] = {
    02017   { 0,  0},
    02018   {-2,  0},
    02019   { 0, -2},
    02020   {-2, -1},
    02021   { 2,  0},
    02022   { 0,  0},
    02023   { 2, -1},
    02024   { 0, -2},
    02025   { 0,  2},
    02026   {-2,  1},
    02027   { 0,  0},
    02028   {-2,  0},
    02029   { 2,  1},
    02030   { 0,  2},
    02031   { 2,  0},
    02032   { 0,  0},
    02033 };
    02034
    02035 static void HandleKeyScrolling()
    02036 {
    02037   /*
    02038    * Check that any of the dirkeys is pressed and that the focused window
    02039    * dont has an edit-box as focused widget.
    02040    */
    02041   if (_dirkeys && !EditBoxInGlobalFocus()) {
    02042     int factor = _shift_pressed ? 50 : 10;
    02043     ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
    02044   }
    02045 }
    02046
    02047 static void MouseLoop(MouseClick click, int mousewheel)
    02048 {
    02049   HandlePlacePresize();
    02050   UpdateTileSelection();
    02051
    02052   if (!VpHandlePlaceSizingDrag())  return;
    02053   if (!HandleDragDrop())           return;
    02054   if (!HandleWindowDragging())     return;
    02055   if (!HandleScrollbarScrolling()) return;
    02056   if (!HandleViewportScroll())     return;
    02057   if (!HandleMouseOver())          return;
    02058
    02059   bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
    02060   if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
    02061
    02062   int x = _cursor.pos.x;
    02063   int y = _cursor.pos.y;
    02064   Window *w = FindWindowFromPt(x, y);
    02065   if (w == NULL) return;
    02066
    02067   if (!MaybeBringWindowToFront(w)) return;
    02068   ViewPort *vp = IsPtInWindowViewport(w, x, y);
    02069
    02070   /* Don't allow any action in a viewport if either in menu of in generating world */
    02071   if (vp != NULL && (_game_mode == GM_MENU || IsGeneratingWorld())) return;
    02072
    02073   if (mousewheel != 0) {
    02074     if (_settings_client.gui.scrollwheel_scrolling == 0) {
    02075       /* Send mousewheel event to window */
    02076       w->OnMouseWheel(mousewheel);
    02077     }
    02078
    02079     /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
    02080     if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
    02081   }
    02082
    02083   if (vp != NULL) {
    02084     if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button
    02085     switch (click) {
    02086       case MC_DOUBLE_LEFT:
    02087       case MC_LEFT:
    02088         DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite);
    02089         if (_thd.place_mode != HT_NONE &&
    02090             /* query button and place sign button work in pause mode */
    02091             _cursor.sprite != SPR_CURSOR_QUERY &&
    02092             _cursor.sprite != SPR_CURSOR_SIGN &&
    02093             _pause_mode != PM_UNPAUSED &&
    02094             !_cheats.build_in_pause.value) {
    02095           return;
    02096         }
    02097
    02098         if (_thd.place_mode == HT_NONE) {
    02099           if (!HandleViewportClicked(vp, x, y) &&
    02100               !(w->flags4 & WF_DISABLE_VP_SCROLL) &&
    02101               _settings_client.gui.left_mouse_btn_scrolling) {
    02102             _scrolling_viewport = true;
    02103             _cursor.fix_at = false;
    02104           }
    02105         } else {
    02106           PlaceObject();
    02107         }
    02108         break;
    02109
    02110       case MC_RIGHT:
    02111         if (!(w->flags4 & WF_DISABLE_VP_SCROLL)) {
    02112           _scrolling_viewport = true;
    02113           _cursor.fix_at = true;
    02114
    02115           /* clear 2D scrolling caches before we start a 2D scroll */
    02116           _cursor.h_wheel = 0;
    02117           _cursor.v_wheel = 0;
    02118         }
    02119         break;
    02120
    02121       default:
    02122         break;
    02123     }
    02124   } else {
    02125     switch (click) {
    02126       case MC_LEFT:
    02127       case MC_DOUBLE_LEFT:
    02128         DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
    02129         break;
    02130
    02131       default:
    02132         if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
    02133         /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
    02134          * Simulate a right button click so we can get started. */
    02135
    02136         /* fallthough */
    02137       case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
    02138     }
    02139   }
    02140 }
    02141
    02145 void HandleMouseEvents()
    02146 {
    02147   static int double_click_time = 0;
    02148   static int double_click_x = 0;
    02149   static int double_click_y = 0;
    02150
    02151   /*
    02152    * During the generation of the world, there might be
    02153    * another thread that is currently building for example
    02154    * a road. To not interfere with those tasks, we should
    02155    * NOT change the _current_company here.
    02156    *
    02157    * This is not necessary either, as the only events that
    02158    * can be handled are the 'close application' events
    02159    */
    02160   if (!IsGeneratingWorld()) _current_company = _local_company;
    02161
    02162   /* Mouse event? */
    02163   MouseClick click = MC_NONE;
    02164   if (_left_button_down && !_left_button_clicked) {
    02165     click = MC_LEFT;
    02166     if (double_click_time != 0 && _realtime_tick - double_click_time   < TIME_BETWEEN_DOUBLE_CLICK &&
    02167         double_click_x != 0    && abs(_cursor.pos.x - double_click_x) < MAX_OFFSET_DOUBLE_CLICK  &&
    02168         double_click_y != 0    && abs(_cursor.pos.y - double_click_y) < MAX_OFFSET_DOUBLE_CLICK) {
    02169       click = MC_DOUBLE_LEFT;
    02170     }
    02171     double_click_time = _realtime_tick;
    02172     double_click_x = _cursor.pos.x;
    02173     double_click_y = _cursor.pos.y;
    02174     _left_button_clicked = true;
    02175     _input_events_this_tick++;
    02176   } else if (_right_button_clicked) {
    02177     _right_button_clicked = false;
    02178     click = MC_RIGHT;
    02179     _input_events_this_tick++;
    02180   }
    02181
    02182   int mousewheel = 0;
    02183   if (_cursor.wheel) {
    02184     mousewheel = _cursor.wheel;
    02185     _cursor.wheel = 0;
    02186     _input_events_this_tick++;
    02187   }
    02188
    02189   MouseLoop(click, mousewheel);
    02190
    02191   /* We have moved the mouse the required distance,
    02192    * no need to move it at any later time. */
    02193   _cursor.delta.x = 0;
    02194   _cursor.delta.y = 0;
    02195 }
    02196
    02200 static void CheckSoftLimit()
    02201 {
    02202   if (_settings_client.gui.window_soft_limit == 0) return;
    02203
    02204   for (;;) {
    02205     uint deletable_count = 0;
    02206     Window *w, *last_deletable = NULL;
    02207     FOR_ALL_WINDOWS_FROM_FRONT(w) {
    02208       if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags4 & WF_STICKY)) continue;
    02209
    02210       last_deletable = w;
    02211       deletable_count++;
    02212     }
    02213
    02214     /* We've ot reached the soft limit yet */
    02215     if (deletable_count <= _settings_client.gui.window_soft_limit) break;
    02216
    02217     assert(last_deletable != NULL);
    02218     delete last_deletable;
    02219   }
    02220 }
    02221
    02225 void InputLoop()
    02226 {
    02227   CheckSoftLimit();
    02228   HandleKeyScrolling();
    02229
    02230   /* Do the actual free of the deleted windows. */
    02231   for (Window *v = _z_front_window; v != NULL; /* nothing */) {
    02232     Window *w = v;
    02233     v = v->z_back;
    02234
    02235     if (w->window_class != WC_INVALID) continue;
    02236
    02237     /* Find the window in the z-array, and effectively remove it
    02238      * by moving all windows after it one to the left. This must be
    02239      * done before removing the child so we cannot cause recursion
    02240      * between the deletion of the parent and the child. */
    02241     if (w->z_front == NULL) {
    02242       _z_front_window = w->z_back;
    02243     } else {
    02244       w->z_front->z_back = w->z_back;
    02245     }
    02246     if (w->z_back == NULL) {
    02247       _z_back_window  = w->z_front;
    02248     } else {
    02249       w->z_back->z_front = w->z_front;
    02250     }
    02251     free(w);
    02252   }
    02253
    02254   DecreaseWindowCounters();
    02255
    02256   if (_input_events_this_tick != 0) {
    02257     /* The input loop is called only once per GameLoop() - so we can clear the counter here */
    02258     _input_events_this_tick = 0;
    02259     /* there were some inputs this tick, don't scroll ??? */
    02260     return;
    02261   }
    02262
    02263   /* HandleMouseEvents was already called for this tick */
    02264   HandleMouseEvents();
    02265   HandleAutoscroll();
    02266 }
    02267
    02271 void UpdateWindows()
    02272 {
    02273   Window *w;
    02274   static int we4_timer = 0;
    02275   int t = we4_timer + 1;
    02276
    02277   if (t >= 100) {
    02278     FOR_ALL_WINDOWS_FROM_FRONT(w) {
    02279       w->OnHundredthTick();
    02280     }
    02281     t = 0;
    02282   }
    02283   we4_timer = t;
    02284
    02285   FOR_ALL_WINDOWS_FROM_FRONT(w) {
    02286     if (w->flags4 & WF_WHITE_BORDER_MASK) {
    02287       w->flags4 -= WF_WHITE_BORDER_ONE;
    02288
    02289       if (!(w->flags4 & WF_WHITE_BORDER_MASK)) w->SetDirty();
    02290     }
    02291   }
    02292
    02293   DrawDirtyBlocks();
    02294
    02295   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02296     /* Update viewport only if window is not shaded. */
    02297     if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
    02298   }
    02299   NetworkDrawChatMessage();
    02300   /* Redraw mouse cursor in case it was hidden */
    02301   DrawMouseCursor();
    02302 }
    02303
    02309 void SetWindowDirty(WindowClass cls, WindowNumber number)
    02310 {
    02311   const Window *w;
    02312   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02313     if (w->window_class == cls && w->window_number == number) w->SetDirty();
    02314   }
    02315 }
    02316
    02323 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
    02324 {
    02325   const Window *w;
    02326   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02327     if (w->window_class == cls && w->window_number == number) {
    02328       w->SetWidgetDirty(widget_index);
    02329     }
    02330   }
    02331 }
    02332
    02337 void SetWindowClassesDirty(WindowClass cls)
    02338 {
    02339   Window *w;
    02340   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02341     if (w->window_class == cls) w->SetDirty();
    02342   }
    02343 }
    02344
    02351 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data)
    02352 {
    02353   Window *w;
    02354   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02355     if (w->window_class == cls && w->window_number == number) w->InvalidateData(data);
    02356   }
    02357 }
    02358
    02364 void InvalidateWindowClassesData(WindowClass cls, int data)
    02365 {
    02366   Window *w;
    02367
    02368   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02369     if (w->window_class == cls) w->InvalidateData(data);
    02370   }
    02371 }
    02372
    02376 void CallWindowTickEvent()
    02377 {
    02378   if (_scroller_click_timeout > 3) {
    02379     _scroller_click_timeout -= 3;
    02380   } else {
    02381     _scroller_click_timeout = 0;
    02382   }
    02383
    02384   Window *w;
    02385   FOR_ALL_WINDOWS_FROM_FRONT(w) {
    02386     w->OnTick();
    02387   }
    02388 }
    02389
    02396 void DeleteNonVitalWindows()
    02397 {
    02398   Window *w;
    02399
    02400 restart_search:
    02401   /* When we find the window to delete, we need to restart the search
    02402    * as deleting this window could cascade in deleting (many) others
    02403    * anywhere in the z-array */
    02404   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02405     if (w->window_class != WC_MAIN_WINDOW &&
    02406         w->window_class != WC_SELECT_GAME &&
    02407         w->window_class != WC_MAIN_TOOLBAR &&
    02408         w->window_class != WC_STATUS_BAR &&
    02409         w->window_class != WC_TOOLBAR_MENU &&
    02410         w->window_class != WC_TOOLTIPS &&
    02411         (w->flags4 & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
    02412
    02413       delete w;
    02414       goto restart_search;
    02415     }
    02416   }
    02417 }
    02418
    02424 void DeleteAllNonVitalWindows()
    02425 {
    02426   Window *w;
    02427
    02428   /* Delete every window except for stickied ones, then sticky ones as well */
    02429   DeleteNonVitalWindows();
    02430
    02431 restart_search:
    02432   /* When we find the window to delete, we need to restart the search
    02433    * as deleting this window could cascade in deleting (many) others
    02434    * anywhere in the z-array */
    02435   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02436     if (w->flags4 & WF_STICKY) {
    02437       delete w;
    02438       goto restart_search;
    02439     }
    02440   }
    02441 }
    02442
    02447 void DeleteConstructionWindows()
    02448 {
    02449   Window *w;
    02450
    02451 restart_search:
    02452   /* When we find the window to delete, we need to restart the search
    02453    * as deleting this window could cascade in deleting (many) others
    02454    * anywhere in the z-array */
    02455   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02456     if (w->desc_flags & WDF_CONSTRUCTION) {
    02457       delete w;
    02458       goto restart_search;
    02459     }
    02460   }
    02461
    02462   FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
    02463 }
    02464
    02466 void HideVitalWindows()
    02467 {
    02468   DeleteWindowById(WC_TOOLBAR_MENU, 0);
    02469   DeleteWindowById(WC_MAIN_TOOLBAR, 0);
    02470   DeleteWindowById(WC_STATUS_BAR, 0);
    02471 }
    02472
    02474 void ReInitAllWindows()
    02475 {
    02476   NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
    02477
    02478   Window *w;
    02479   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02480     w->ReInit();
    02481   }
    02482
    02483   /* Make sure essential parts of all windows are visible */
    02484   RelocateAllWindows(_cur_resolution.width, _cur_resolution.height);
    02485   MarkWholeScreenDirty();
    02486 }
    02487
    02493 int PositionMainToolbar(Window *w)
    02494 {
    02495   DEBUG(misc, 5, "Repositioning Main Toolbar...");
    02496
    02497   if (w == NULL || w->window_class != WC_MAIN_TOOLBAR) {
    02498     w = FindWindowById(WC_MAIN_TOOLBAR, 0);
    02499   }
    02500
    02501   switch (_settings_client.gui.toolbar_pos) {
    02502     case 1:  w->left = (_screen.width - w->width) / 2; break;
    02503     case 2:  w->left = _screen.width - w->width; break;
    02504     default: w->left = 0;
    02505   }
    02506   SetDirtyBlocks(0, 0, _screen.width, w->height); // invalidate the whole top part
    02507   return w->left;
    02508 }
    02509
    02510
    02516 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
    02517 {
    02518   Window *w;
    02519   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02520     if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
    02521       w->viewport->follow_vehicle = to_index;
    02522       w->SetDirty();
    02523     }
    02524   }
    02525 }
    02526
    02527
    02533 void RelocateAllWindows(int neww, int newh)
    02534 {
    02535   Window *w;
    02536
    02537   FOR_ALL_WINDOWS_FROM_BACK(w) {
    02538     int left, top;
    02539
    02540     if (w->window_class == WC_MAIN_WINDOW) {
    02541       ViewPort *vp = w->viewport;
    02542       vp->width = w->width = neww;
    02543       vp->height = w->height = newh;
    02544       vp->virtual_width = ScaleByZoom(neww, vp->zoom);
    02545       vp->virtual_height = ScaleByZoom(newh, vp->zoom);
    02546       continue; // don't modify top,left
    02547     }
    02548
    02549     /* XXX - this probably needs something more sane. For example specying
    02550      * in a 'backup'-desc that the window should always be centred. */
    02551     switch (w->window_class) {
    02552       case WC_MAIN_TOOLBAR:
    02553         if (neww - w->width != 0) ResizeWindow(w, min(neww, 640) - w->width, 0);
    02554
    02555         top = w->top;
    02556         left = PositionMainToolbar(w); // changes toolbar orientation
    02557         break;
    02558
    02559       case WC_SELECT_GAME:
    02560       case WC_GAME_OPTIONS:
    02561       case WC_NETWORK_WINDOW:
    02562         top = (newh - w->height) >> 1;
    02563         left = (neww - w->width) >> 1;
    02564         break;
    02565
    02566       case WC_NEWS_WINDOW:
    02567         top = newh - w->height;
    02568         left = (neww - w->width) >> 1;
    02569         break;
    02570
    02571       case WC_STATUS_BAR:
    02572         ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
    02573         top = newh - w->height;
    02574         left = (neww - w->width) >> 1;
    02575         break;
    02576
    02577       case WC_SEND_NETWORK_MSG:
    02578         ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
    02579         top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
    02580         left = (neww - w->width) >> 1;
    02581         break;
    02582
    02583       case WC_CONSOLE:
    02584         IConsoleResize(w);
    02585         continue;
    02586
    02587       default: {
    02588         left = w->left;
    02589         if (left + (w->width >> 1) >= neww) left = neww - w->width;
    02590         if (left < 0) left = 0;
    02591
    02592         top = w->top;
    02593         if (top + (w->height >> 1) >= newh) top = newh - w->height;
    02594
    02595         const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
    02596         if (wt != NULL) {
    02597           if (top < wt->height && wt->left < (w->left + w->width) && (wt->left + wt->width) > w->left) top = wt->height;
    02598           if (top >= newh) top = newh - 1;
    02599         } else {
    02600           if (top < 0) top = 0;
    02601         }
    02602       } break;
    02603     }
    02604
    02605     if (w->viewport != NULL) {
    02606       w->viewport->left += left - w->left;
    02607       w->viewport->top += top - w->top;
    02608     }
    02609
    02610     w->left = left;
    02611     w->top = top;
    02612   }
    02613 }
    02614
    02619 PickerWindowBase::~PickerWindowBase()
    02620 {
    02621   this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child
    02622   ResetObjectToPlace();
    02623 }