00001
00002
00003
00004
00005
00006
00007
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
00047
00048
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
00108 if (_focused_window != NULL) {
00109 if (_focused_window->nested_focus != NULL) _focused_window->nested_focus->SetDirty(_focused_window);
00110 }
00111
00112
00113 Window *old_focused = _focused_window;
00114 _focused_window = w;
00115
00116
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
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
00143 this->nested_focus->SetDirty(this);
00144 this->nested_focus = NULL;
00145 }
00146 }
00147
00153 bool Window::SetFocusedWidget(byte widget_index)
00154 {
00155
00156 if (widget_index >= this->nested_array_size) return false;
00157
00158 assert(this->nested_array[widget_index] != NULL);
00159 if (this->nested_focus != NULL) {
00160 if (this->GetWidget<NWidgetCore>(widget_index) == this->nested_focus) return false;
00161
00162
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
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
00265 if (_focused_window != w &&
00266 (w->desc_flags & WDF_NO_FOCUS) == 0 &&
00267 widget_type != WWT_CLOSEBOX) {
00268 focused_widget_changed = true;
00269 if (_focused_window != NULL) {
00270 _focused_window->OnFocusLost();
00271
00272
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;
00280
00281
00282 if (nw->IsDisabled()) return;
00283
00284 int widget_index = nw->index;
00285
00286
00287
00288 if (widget_type != WWT_CAPTION) {
00289
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
00295
00296
00297
00298
00299
00300
00301
00302
00303 focused_widget_changed |= w->SetFocusedWidget(widget_index);
00304 }
00305
00306
00307
00308 if (HideDropDownMenu(w) == widget_index && widget_index >= 0) return;
00309
00310 switch (widget_type) {
00311
00312 case WWT_PANEL | WWB_PUSHBUTTON:
00313 case WWT_IMGBTN | WWB_PUSHBUTTON:
00314 case WWT_TEXTBTN | WWB_PUSHBUTTON:
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) {
00326
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:
00335 delete w;
00336 return;
00337
00338 case WWT_CAPTION:
00339 StartWindowDrag(w);
00340 return;
00341
00342 case WWT_RESIZEBOX:
00343
00344
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
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
00381 if (wid == NULL) return;
00382
00383
00384 if (wid->tool_tip != 0) {
00385 GuiShowTooltips(wid->tool_tip);
00386 return;
00387 }
00388
00389
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
00408 if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
00409 w->SetShaded(!w->IsShaded());
00410 return;
00411 }
00412
00413
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
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
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
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();
00526
00527
00528 int window_width = this->width;
00529 int window_height = this->height;
00530
00531 this->OnInit();
00532
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
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
00546
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
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
00618 if (_mouseover_last_w == this) _mouseover_last_w = NULL;
00619
00620
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);
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
00692
00693
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
00712
00713
00714 FOR_ALL_WINDOWS_FROM_BACK(w) {
00715 if (w->owner == id) {
00716 delete w;
00717 goto restart_search;
00718 }
00719 }
00720
00721
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);
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
00804 for (; v != NULL && v != w && IsVitalWindow(v); v = v->z_back) { }
00805
00806 if (v == NULL || w == v) return;
00807
00808
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
00839 void Window::InitializeData(const WindowDesc *desc, WindowNumber window_number)
00840 {
00841
00842 this->window_class = desc->cls;
00843 this->flags4 |= WF_WHITE_BORDER_MASK;
00844 if (desc->default_pos == WDP_CENTER) this->flags4 |= WF_CENTERED;
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
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
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
00862
00863 this->resize.step_width = this->nested_root->resize_x;
00864 this->resize.step_height = this->nested_root->resize_y;
00865
00866
00867
00868
00869 if (this->window_class != WC_OSK && (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != NULL)) SetFocusedWindow(this);
00870
00871
00872
00873
00874
00875
00876
00877
00878
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);
00940 def_height = max(def_height, this->height);
00941
00942
00943
00944
00945
00946 if (this->width != def_width || this->height != def_height) {
00947
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
00958
00959
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
00965 } else {
00966
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
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
01039
01040
01041 if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
01042
01043 if (top < 22 || top > _screen.height - (height >> 2)) return false;
01044
01045
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
01074 if (IsGoodAutoPlace1(0, 24, width, height, pt)) return pt;
01075
01076
01077
01078
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
01095
01096
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
01108
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 &&
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:
01179 return GetToolbarAlignedWindowPosition(default_width);
01180
01181 case WDP_AUTO:
01182 return GetAutoPlacePosition(default_width, default_height);
01183
01184 case WDP_CENTER:
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 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, window_number);
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();
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; ) {
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
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
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
01385 if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
01386
01387 Point pt = { -1, -1 };
01388 _mouseover_last_w->OnMouseOver(pt, 0);
01389 }
01390
01391
01392 _mouseover_last_w = w;
01393
01394 if (w != NULL) {
01395
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
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
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);
01480
01481 if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return;
01482 if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return;
01483
01484
01485 if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) {
01486 if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
01487 return;
01488 }
01489 if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) {
01490 if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
01491 return;
01492 }
01493
01494
01495 if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) {
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) {
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
01509 if (!_dragging_window) return true;
01510
01511
01512 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return false;
01513
01514
01515 Window *w;
01516 FOR_ALL_WINDOWS_FROM_BACK(w) {
01517 if (w->flags4 & WF_DRAGGING) {
01518
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;
01540
01541 if (y + w->height > v->top && y < v->top + v->height) {
01542
01543 delta = abs(v->left + v->width - x);
01544 if (delta <= hsnap) {
01545 nx = v->left + v->width;
01546 hsnap = delta;
01547 }
01548
01549
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
01559 delta = abs(v->left - x);
01560 if (delta <= hsnap) {
01561 nx = v->left;
01562 hsnap = delta;
01563 }
01564
01565
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
01575 delta = abs(v->top + v->height - y);
01576 if (delta <= vsnap) {
01577 ny = v->top + v->height;
01578 vsnap = delta;
01579 }
01580
01581
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
01591 delta = abs(v->top - y);
01592 if (delta <= vsnap) {
01593 ny = v->top;
01594 vsnap = delta;
01595 }
01596
01597
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
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
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
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
01635 if (!_left_button_down) {
01636 w->flags4 &= ~WF_SIZING;
01637 w->SetDirty();
01638 break;
01639 }
01640
01641
01642
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
01652 if (w->resize.step_width == 0) x = 0;
01653 if (w->resize.step_height == 0) y = 0;
01654
01655
01656
01657
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
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
01670 if (x == 0 && y == 0) return false;
01671
01672
01673 _drag_delta.y += y;
01674 if ((w->flags4 & WF_SIZING_LEFT) && x != 0) {
01675 _drag_delta.x -= x;
01676 w->SetDirty();
01677 w->left -= x;
01678
01679 } else {
01680 _drag_delta.x += x;
01681 }
01682
01683
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 w->flags4 &= ~WF_CENTERED;
01701 _dragging_window = true;
01702
01703 _drag_delta.x = w->left - _cursor.pos.x;
01704 _drag_delta.y = w->top - _cursor.pos.y;
01705
01706 BringWindowToFront(w);
01707 DeleteWindowById(WC_DROPDOWN_MENU, 0);
01708 }
01709
01715 static void StartWindowSizing(Window *w, bool to_left)
01716 {
01717 w->flags4 |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
01718 w->flags4 &= ~WF_CENTERED;
01719 _dragging_window = true;
01720
01721 _drag_delta.x = _cursor.pos.x;
01722 _drag_delta.y = _cursor.pos.y;
01723
01724 BringWindowToFront(w);
01725 DeleteWindowById(WC_DROPDOWN_MENU, 0);
01726 }
01727
01728
01729 static bool HandleScrollbarScrolling()
01730 {
01731 Window *w;
01732
01733
01734 if (!_scrolling_scrollbar) return true;
01735
01736
01737 FOR_ALL_WINDOWS_FROM_BACK(w) {
01738 if (w->flags4 & WF_SCROLL_MIDDLE) {
01739
01740 if (!_left_button_down) {
01741 w->flags4 &= ~WF_SCROLL_MIDDLE;
01742 w->SetDirty();
01743 break;
01744 }
01745
01746 int i;
01747 Scrollbar *sb;
01748 bool rtl = false;
01749
01750 if (w->flags4 & WF_HSCROLL) {
01751 sb = &w->hscroll;
01752 i = _cursor.pos.x - _cursorpos_drag_start.x;
01753 rtl = _dynlang.text_dir == TD_RTL;
01754 } else if (w->flags4 & WF_SCROLL2) {
01755 sb = &w->vscroll2;
01756 i = _cursor.pos.y - _cursorpos_drag_start.y;
01757 } else {
01758 sb = &w->vscroll;
01759 i = _cursor.pos.y - _cursorpos_drag_start.y;
01760 }
01761
01762
01763 int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
01764 if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
01765 if (pos != sb->GetPosition()) {
01766 sb->SetPosition(pos);
01767 w->SetDirty();
01768 }
01769 return false;
01770 }
01771 }
01772
01773 _scrolling_scrollbar = false;
01774 return false;
01775 }
01776
01777 static bool HandleViewportScroll()
01778 {
01779 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
01780
01781 if (!_scrolling_viewport) return true;
01782
01783 Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
01784
01785 if (!(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down)) || w == NULL) {
01786 _cursor.fix_at = false;
01787 _scrolling_viewport = false;
01788 return true;
01789 }
01790
01791 if (w == FindWindowById(WC_MAIN_WINDOW, 0) && w->viewport->follow_vehicle != INVALID_VEHICLE) {
01792
01793 const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
01794 ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true);
01795 return true;
01796 }
01797
01798 Point delta;
01799 if (_settings_client.gui.reverse_scroll || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down)) {
01800 delta.x = -_cursor.delta.x;
01801 delta.y = -_cursor.delta.y;
01802 } else {
01803 delta.x = _cursor.delta.x;
01804 delta.y = _cursor.delta.y;
01805 }
01806
01807 if (scrollwheel_scrolling) {
01808
01809 delta.x = _cursor.h_wheel;
01810 delta.y = _cursor.v_wheel;
01811 _cursor.v_wheel = 0;
01812 _cursor.h_wheel = 0;
01813 }
01814
01815
01816 if (delta.x != 0 || delta.y != 0) w->OnScroll(delta);
01817
01818 _cursor.delta.x = 0;
01819 _cursor.delta.y = 0;
01820 return false;
01821 }
01822
01831 static bool MaybeBringWindowToFront(Window *w)
01832 {
01833 bool bring_to_front = false;
01834
01835 if (w->window_class == WC_MAIN_WINDOW ||
01836 IsVitalWindow(w) ||
01837 w->window_class == WC_TOOLTIPS ||
01838 w->window_class == WC_DROPDOWN_MENU) {
01839 return true;
01840 }
01841
01842
01843 int w_width = w->width;
01844 int w_height = w->height;
01845 if (w->IsShaded()) {
01846 w_width = w->unshaded_size.width;
01847 w_height = w->unshaded_size.height;
01848 }
01849
01850 Window *u;
01851 FOR_ALL_WINDOWS_FROM_BACK_FROM(u, w->z_front) {
01852
01853 if (u->parent == w && (u->desc_flags & WDF_MODAL)) {
01854 u->flags4 |= WF_WHITE_BORDER_MASK;
01855 u->SetDirty();
01856 return false;
01857 }
01858
01859 if (u->window_class == WC_MAIN_WINDOW ||
01860 IsVitalWindow(u) ||
01861 u->window_class == WC_TOOLTIPS ||
01862 u->window_class == WC_DROPDOWN_MENU) {
01863 continue;
01864 }
01865
01866
01867 if (w->left + w_width <= u->left ||
01868 u->left + u->width <= w->left ||
01869 w->top + w_height <= u->top ||
01870 u->top + u->height <= w->top) {
01871 continue;
01872 }
01873
01874 bring_to_front = true;
01875 }
01876
01877 if (bring_to_front) BringWindowToFront(w);
01878 return true;
01879 }
01880
01884 void HandleKeypress(uint32 raw_key)
01885 {
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895 if (!IsGeneratingWorld()) _current_company = _local_company;
01896
01897
01898 uint16 key = GB(raw_key, 0, 16);
01899 uint16 keycode = GB(raw_key, 16, 16);
01900
01901
01902
01903
01904
01905
01906
01907
01908 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
01909
01910
01911
01912
01913 if (key == 0 && keycode == 0) return;
01914
01915
01916 if (EditBoxInGlobalFocus()) {
01917
01918 if (_focused_window->OnKeyPress(key, keycode) == Window::ES_HANDLED) return;
01919 }
01920
01921
01922 Window *w;
01923 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01924 if (w->window_class == WC_MAIN_TOOLBAR) continue;
01925 if (w->OnKeyPress(key, keycode) == Window::ES_HANDLED) return;
01926 }
01927
01928 w = FindWindowById(WC_MAIN_TOOLBAR, 0);
01929
01930 if (w != NULL) w->OnKeyPress(key, keycode);
01931 }
01932
01936 void HandleCtrlChanged()
01937 {
01938
01939 Window *w;
01940 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01941 if (w->OnCTRLStateChange() == Window::ES_HANDLED) return;
01942 }
01943 }
01944
01951 static int _input_events_this_tick = 0;
01952
01957 static void HandleAutoscroll()
01958 {
01959 if (_settings_client.gui.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) {
01960 int x = _cursor.pos.x;
01961 int y = _cursor.pos.y;
01962 Window *w = FindWindowFromPt(x, y);
01963 if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return;
01964 ViewPort *vp = IsPtInWindowViewport(w, x, y);
01965 if (vp != NULL) {
01966 x -= vp->left;
01967 y -= vp->top;
01968
01969
01970 #define scrollspeed 3
01971 if (x - 15 < 0) {
01972 w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
01973 } else if (15 - (vp->width - x) > 0) {
01974 w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
01975 }
01976 if (y - 15 < 0) {
01977 w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
01978 } else if (15 - (vp->height - y) > 0) {
01979 w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
01980 }
01981 #undef scrollspeed
01982 }
01983 }
01984 }
01985
01986 enum MouseClick {
01987 MC_NONE = 0,
01988 MC_LEFT,
01989 MC_RIGHT,
01990 MC_DOUBLE_LEFT,
01991
01992 MAX_OFFSET_DOUBLE_CLICK = 5,
01993 TIME_BETWEEN_DOUBLE_CLICK = 500,
01994 };
01995
01996 extern bool VpHandlePlaceSizingDrag();
01997
01998 static void ScrollMainViewport(int x, int y)
01999 {
02000 if (_game_mode != GM_MENU) {
02001 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
02002 assert(w);
02003
02004 w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
02005 w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
02006 }
02007 }
02008
02018 static const int8 scrollamt[16][2] = {
02019 { 0, 0},
02020 {-2, 0},
02021 { 0, -2},
02022 {-2, -1},
02023 { 2, 0},
02024 { 0, 0},
02025 { 2, -1},
02026 { 0, -2},
02027 { 0, 2},
02028 {-2, 1},
02029 { 0, 0},
02030 {-2, 0},
02031 { 2, 1},
02032 { 0, 2},
02033 { 2, 0},
02034 { 0, 0},
02035 };
02036
02037 static void HandleKeyScrolling()
02038 {
02039
02040
02041
02042
02043 if (_dirkeys && !EditBoxInGlobalFocus()) {
02044 int factor = _shift_pressed ? 50 : 10;
02045 ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
02046 }
02047 }
02048
02049 static void MouseLoop(MouseClick click, int mousewheel)
02050 {
02051 HandlePlacePresize();
02052 UpdateTileSelection();
02053
02054 if (!VpHandlePlaceSizingDrag()) return;
02055 if (!HandleDragDrop()) return;
02056 if (!HandleWindowDragging()) return;
02057 if (!HandleScrollbarScrolling()) return;
02058 if (!HandleViewportScroll()) return;
02059 if (!HandleMouseOver()) return;
02060
02061 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
02062 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
02063
02064 int x = _cursor.pos.x;
02065 int y = _cursor.pos.y;
02066 Window *w = FindWindowFromPt(x, y);
02067 if (w == NULL) return;
02068
02069 if (!MaybeBringWindowToFront(w)) return;
02070 ViewPort *vp = IsPtInWindowViewport(w, x, y);
02071
02072
02073 if (vp != NULL && (_game_mode == GM_MENU || IsGeneratingWorld())) return;
02074
02075 if (mousewheel != 0) {
02076 if (_settings_client.gui.scrollwheel_scrolling == 0) {
02077
02078 w->OnMouseWheel(mousewheel);
02079 }
02080
02081
02082 if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
02083 }
02084
02085 if (vp != NULL) {
02086 if (scrollwheel_scrolling) click = MC_RIGHT;
02087 switch (click) {
02088 case MC_DOUBLE_LEFT:
02089 case MC_LEFT:
02090 DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite);
02091 if (_thd.place_mode != HT_NONE &&
02092
02093 _cursor.sprite != SPR_CURSOR_QUERY &&
02094 _cursor.sprite != SPR_CURSOR_SIGN &&
02095 _pause_mode != PM_UNPAUSED &&
02096 !_cheats.build_in_pause.value) {
02097 return;
02098 }
02099
02100 if (_thd.place_mode == HT_NONE) {
02101 if (!HandleViewportClicked(vp, x, y) &&
02102 !(w->flags4 & WF_DISABLE_VP_SCROLL) &&
02103 _settings_client.gui.left_mouse_btn_scrolling) {
02104 _scrolling_viewport = true;
02105 _cursor.fix_at = false;
02106 }
02107 } else {
02108 PlaceObject();
02109 }
02110 break;
02111
02112 case MC_RIGHT:
02113 if (!(w->flags4 & WF_DISABLE_VP_SCROLL)) {
02114 _scrolling_viewport = true;
02115 _cursor.fix_at = true;
02116
02117
02118 _cursor.h_wheel = 0;
02119 _cursor.v_wheel = 0;
02120 }
02121 break;
02122
02123 default:
02124 break;
02125 }
02126 } else {
02127 switch (click) {
02128 case MC_LEFT:
02129 case MC_DOUBLE_LEFT:
02130 DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
02131 break;
02132
02133 default:
02134 if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
02135
02136
02137
02138
02139 case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
02140 }
02141 }
02142 }
02143
02147 void HandleMouseEvents()
02148 {
02149 static int double_click_time = 0;
02150 static int double_click_x = 0;
02151 static int double_click_y = 0;
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162 if (!IsGeneratingWorld()) _current_company = _local_company;
02163
02164
02165 MouseClick click = MC_NONE;
02166 if (_left_button_down && !_left_button_clicked) {
02167 click = MC_LEFT;
02168 if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
02169 double_click_x != 0 && abs(_cursor.pos.x - double_click_x) < MAX_OFFSET_DOUBLE_CLICK &&
02170 double_click_y != 0 && abs(_cursor.pos.y - double_click_y) < MAX_OFFSET_DOUBLE_CLICK) {
02171 click = MC_DOUBLE_LEFT;
02172 }
02173 double_click_time = _realtime_tick;
02174 double_click_x = _cursor.pos.x;
02175 double_click_y = _cursor.pos.y;
02176 _left_button_clicked = true;
02177 _input_events_this_tick++;
02178 } else if (_right_button_clicked) {
02179 _right_button_clicked = false;
02180 click = MC_RIGHT;
02181 _input_events_this_tick++;
02182 }
02183
02184 int mousewheel = 0;
02185 if (_cursor.wheel) {
02186 mousewheel = _cursor.wheel;
02187 _cursor.wheel = 0;
02188 _input_events_this_tick++;
02189 }
02190
02191 MouseLoop(click, mousewheel);
02192
02193
02194
02195 _cursor.delta.x = 0;
02196 _cursor.delta.y = 0;
02197 }
02198
02202 static void CheckSoftLimit()
02203 {
02204 if (_settings_client.gui.window_soft_limit == 0) return;
02205
02206 for (;;) {
02207 uint deletable_count = 0;
02208 Window *w, *last_deletable = NULL;
02209 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02210 if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags4 & WF_STICKY)) continue;
02211
02212 last_deletable = w;
02213 deletable_count++;
02214 }
02215
02216
02217 if (deletable_count <= _settings_client.gui.window_soft_limit) break;
02218
02219 assert(last_deletable != NULL);
02220 delete last_deletable;
02221 }
02222 }
02223
02227 void InputLoop()
02228 {
02229 CheckSoftLimit();
02230 HandleKeyScrolling();
02231
02232
02233 for (Window *v = _z_front_window; v != NULL; ) {
02234 Window *w = v;
02235 v = v->z_back;
02236
02237 if (w->window_class != WC_INVALID) continue;
02238
02239
02240
02241
02242
02243 if (w->z_front == NULL) {
02244 _z_front_window = w->z_back;
02245 } else {
02246 w->z_front->z_back = w->z_back;
02247 }
02248 if (w->z_back == NULL) {
02249 _z_back_window = w->z_front;
02250 } else {
02251 w->z_back->z_front = w->z_front;
02252 }
02253 free(w);
02254 }
02255
02256 DecreaseWindowCounters();
02257
02258 if (_input_events_this_tick != 0) {
02259
02260 _input_events_this_tick = 0;
02261
02262 return;
02263 }
02264
02265
02266 HandleMouseEvents();
02267 HandleAutoscroll();
02268 }
02269
02273 void UpdateWindows()
02274 {
02275 Window *w;
02276 static int we4_timer = 0;
02277 int t = we4_timer + 1;
02278
02279 if (t >= 100) {
02280 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02281 w->OnHundredthTick();
02282 }
02283 t = 0;
02284 }
02285 we4_timer = t;
02286
02287 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02288 if (w->flags4 & WF_WHITE_BORDER_MASK) {
02289 w->flags4 -= WF_WHITE_BORDER_ONE;
02290
02291 if (!(w->flags4 & WF_WHITE_BORDER_MASK)) w->SetDirty();
02292 }
02293 }
02294
02295 DrawDirtyBlocks();
02296
02297 FOR_ALL_WINDOWS_FROM_BACK(w) {
02298
02299 if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
02300 }
02301 NetworkDrawChatMessage();
02302
02303 DrawMouseCursor();
02304 }
02305
02311 void SetWindowDirty(WindowClass cls, WindowNumber number)
02312 {
02313 const Window *w;
02314 FOR_ALL_WINDOWS_FROM_BACK(w) {
02315 if (w->window_class == cls && w->window_number == number) w->SetDirty();
02316 }
02317 }
02318
02325 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
02326 {
02327 const Window *w;
02328 FOR_ALL_WINDOWS_FROM_BACK(w) {
02329 if (w->window_class == cls && w->window_number == number) {
02330 w->SetWidgetDirty(widget_index);
02331 }
02332 }
02333 }
02334
02339 void SetWindowClassesDirty(WindowClass cls)
02340 {
02341 Window *w;
02342 FOR_ALL_WINDOWS_FROM_BACK(w) {
02343 if (w->window_class == cls) w->SetDirty();
02344 }
02345 }
02346
02353 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data)
02354 {
02355 Window *w;
02356 FOR_ALL_WINDOWS_FROM_BACK(w) {
02357 if (w->window_class == cls && w->window_number == number) w->InvalidateData(data);
02358 }
02359 }
02360
02366 void InvalidateWindowClassesData(WindowClass cls, int data)
02367 {
02368 Window *w;
02369
02370 FOR_ALL_WINDOWS_FROM_BACK(w) {
02371 if (w->window_class == cls) w->InvalidateData(data);
02372 }
02373 }
02374
02378 void CallWindowTickEvent()
02379 {
02380 if (_scroller_click_timeout > 3) {
02381 _scroller_click_timeout -= 3;
02382 } else {
02383 _scroller_click_timeout = 0;
02384 }
02385
02386 Window *w;
02387 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02388 w->OnTick();
02389 }
02390 }
02391
02398 void DeleteNonVitalWindows()
02399 {
02400 Window *w;
02401
02402 restart_search:
02403
02404
02405
02406 FOR_ALL_WINDOWS_FROM_BACK(w) {
02407 if (w->window_class != WC_MAIN_WINDOW &&
02408 w->window_class != WC_SELECT_GAME &&
02409 w->window_class != WC_MAIN_TOOLBAR &&
02410 w->window_class != WC_STATUS_BAR &&
02411 w->window_class != WC_TOOLBAR_MENU &&
02412 w->window_class != WC_TOOLTIPS &&
02413 (w->flags4 & WF_STICKY) == 0) {
02414
02415 delete w;
02416 goto restart_search;
02417 }
02418 }
02419 }
02420
02426 void DeleteAllNonVitalWindows()
02427 {
02428 Window *w;
02429
02430
02431 DeleteNonVitalWindows();
02432
02433 restart_search:
02434
02435
02436
02437 FOR_ALL_WINDOWS_FROM_BACK(w) {
02438 if (w->flags4 & WF_STICKY) {
02439 delete w;
02440 goto restart_search;
02441 }
02442 }
02443 }
02444
02449 void DeleteConstructionWindows()
02450 {
02451 Window *w;
02452
02453 restart_search:
02454
02455
02456
02457 FOR_ALL_WINDOWS_FROM_BACK(w) {
02458 if (w->desc_flags & WDF_CONSTRUCTION) {
02459 delete w;
02460 goto restart_search;
02461 }
02462 }
02463
02464 FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
02465 }
02466
02468 void HideVitalWindows()
02469 {
02470 DeleteWindowById(WC_TOOLBAR_MENU, 0);
02471 DeleteWindowById(WC_MAIN_TOOLBAR, 0);
02472 DeleteWindowById(WC_STATUS_BAR, 0);
02473 }
02474
02476 void ReInitAllWindows()
02477 {
02478 NWidgetLeaf::InvalidateDimensionCache();
02479
02480 Window *w;
02481 FOR_ALL_WINDOWS_FROM_BACK(w) {
02482 w->ReInit();
02483 }
02484
02485
02486 RelocateAllWindows(_cur_resolution.width, _cur_resolution.height);
02487 MarkWholeScreenDirty();
02488 }
02489
02495 int PositionMainToolbar(Window *w)
02496 {
02497 DEBUG(misc, 5, "Repositioning Main Toolbar...");
02498
02499 if (w == NULL || w->window_class != WC_MAIN_TOOLBAR) {
02500 w = FindWindowById(WC_MAIN_TOOLBAR, 0);
02501 }
02502
02503 switch (_settings_client.gui.toolbar_pos) {
02504 case 1: w->left = (_screen.width - w->width) / 2; break;
02505 case 2: w->left = _screen.width - w->width; break;
02506 default: w->left = 0;
02507 }
02508 SetDirtyBlocks(0, 0, _screen.width, w->height);
02509 return w->left;
02510 }
02511
02512
02518 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
02519 {
02520 Window *w;
02521 FOR_ALL_WINDOWS_FROM_BACK(w) {
02522 if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
02523 w->viewport->follow_vehicle = to_index;
02524 w->SetDirty();
02525 }
02526 }
02527 }
02528
02529
02535 void RelocateAllWindows(int neww, int newh)
02536 {
02537 Window *w;
02538
02539 FOR_ALL_WINDOWS_FROM_BACK(w) {
02540 int left, top;
02541
02542 if (w->window_class == WC_MAIN_WINDOW) {
02543 ViewPort *vp = w->viewport;
02544 vp->width = w->width = neww;
02545 vp->height = w->height = newh;
02546 vp->virtual_width = ScaleByZoom(neww, vp->zoom);
02547 vp->virtual_height = ScaleByZoom(newh, vp->zoom);
02548 continue;
02549 }
02550
02551
02552
02553 switch (w->window_class) {
02554 case WC_MAIN_TOOLBAR:
02555 if (neww - w->width != 0) ResizeWindow(w, min(neww, 640) - w->width, 0);
02556
02557 top = w->top;
02558 left = PositionMainToolbar(w);
02559 break;
02560
02561 case WC_NEWS_WINDOW:
02562 top = newh - w->height;
02563 left = (neww - w->width) >> 1;
02564 break;
02565
02566 case WC_STATUS_BAR:
02567 ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
02568 top = newh - w->height;
02569 left = (neww - w->width) >> 1;
02570 break;
02571
02572 case WC_SEND_NETWORK_MSG:
02573 ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
02574 top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
02575 left = (neww - w->width) >> 1;
02576 break;
02577
02578 case WC_CONSOLE:
02579 IConsoleResize(w);
02580 continue;
02581
02582 default: {
02583 if (w->flags4 & WF_CENTERED) {
02584 top = (newh - w->height) >> 1;
02585 left = (neww - w->width) >> 1;
02586 break;
02587 }
02588
02589 left = w->left;
02590 if (left + (w->width >> 1) >= neww) left = neww - w->width;
02591 if (left < 0) left = 0;
02592
02593 top = w->top;
02594 if (top + (w->height >> 1) >= newh) top = newh - w->height;
02595
02596 const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
02597 if (wt != NULL) {
02598 if (top < wt->height && wt->left < (w->left + w->width) && (wt->left + wt->width) > w->left) top = wt->height;
02599 if (top >= newh) top = newh - 1;
02600 } else {
02601 if (top < 0) top = 0;
02602 }
02603 } break;
02604 }
02605
02606 if (w->viewport != NULL) {
02607 w->viewport->left += left - w->left;
02608 w->viewport->top += top - w->top;
02609 }
02610
02611 w->left = left;
02612 w->top = top;
02613 }
02614 }
02615
02620 PickerWindowBase::~PickerWindowBase()
02621 {
02622 this->window_class = WC_INVALID;
02623 ResetObjectToPlace();
02624 }