00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include <stdarg.h>
00014 #include "company_func.h"
00015 #include "gfx_func.h"
00016 #include "console_func.h"
00017 #include "console_gui.h"
00018 #include "viewport_func.h"
00019 #include "genworld.h"
00020 #include "blitter/factory.hpp"
00021 #include "zoom_func.h"
00022 #include "vehicle_base.h"
00023 #include "window_func.h"
00024 #include "tilehighlight_func.h"
00025 #include "network/network.h"
00026 #include "querystring_gui.h"
00027 #include "widgets/dropdown_func.h"
00028 #include "strings_func.h"
00029 #include "settings_type.h"
00030 #include "newgrf_debug.h"
00031 #include "hotkeys.h"
00032 #include "toolbar_gui.h"
00033 #include "statusbar_gui.h"
00034
00035
00036 static Point _drag_delta;
00037 static Window *_mouseover_last_w = NULL;
00038 static Window *_last_scroll_window = 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 = 0;
00057
00058 bool _scrolling_viewport;
00059 bool _mouse_hovering;
00060
00061 SpecialMouseMode _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
00091 int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const
00092 {
00093 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00094 if (line_height < 0) line_height = wid->resize_y;
00095 if (clickpos < (int)wid->pos_y + padding) return INT_MAX;
00096 return (clickpos - (int)wid->pos_y - padding) / line_height;
00097 }
00098
00104 const Scrollbar *Window::GetScrollbar(uint widnum) const
00105 {
00106 return this->GetWidget<NWidgetScrollbar>(widnum);
00107 }
00108
00114 Scrollbar *Window::GetScrollbar(uint widnum)
00115 {
00116 return this->GetWidget<NWidgetScrollbar>(widnum);
00117 }
00118
00119
00124 void SetFocusedWindow(Window *w)
00125 {
00126 if (_focused_window == w) return;
00127
00128
00129 if (_focused_window != NULL) {
00130 if (_focused_window->nested_focus != NULL) _focused_window->nested_focus->SetDirty(_focused_window);
00131 }
00132
00133
00134 Window *old_focused = _focused_window;
00135 _focused_window = w;
00136
00137
00138 if (old_focused != NULL) old_focused->OnFocusLost();
00139 if (_focused_window != NULL) _focused_window->OnFocus();
00140 }
00141
00147 static bool EditBoxInGlobalFocus()
00148 {
00149 if (_focused_window == NULL) return false;
00150
00151
00152 if (_focused_window->window_class == WC_CONSOLE) return true;
00153
00154 return _focused_window->nested_focus != NULL && _focused_window->nested_focus->type == WWT_EDITBOX;
00155 }
00156
00160 void Window::UnfocusFocusedWidget()
00161 {
00162 if (this->nested_focus != NULL) {
00163
00164 this->nested_focus->SetDirty(this);
00165 this->nested_focus = NULL;
00166 }
00167 }
00168
00174 bool Window::SetFocusedWidget(byte widget_index)
00175 {
00176
00177 if (widget_index >= this->nested_array_size) return false;
00178
00179 assert(this->nested_array[widget_index] != NULL);
00180 if (this->nested_focus != NULL) {
00181 if (this->GetWidget<NWidgetCore>(widget_index) == this->nested_focus) return false;
00182
00183
00184 this->nested_focus->SetDirty(this);
00185 }
00186 this->nested_focus = this->GetWidget<NWidgetCore>(widget_index);
00187 return true;
00188 }
00189
00197 void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...)
00198 {
00199 va_list wdg_list;
00200
00201 va_start(wdg_list, widgets);
00202
00203 while (widgets != WIDGET_LIST_END) {
00204 SetWidgetDisabledState(widgets, disab_stat);
00205 widgets = va_arg(wdg_list, int);
00206 }
00207
00208 va_end(wdg_list);
00209 }
00210
00216 void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...)
00217 {
00218 va_list wdg_list;
00219
00220 va_start(wdg_list, widgets);
00221
00222 while (widgets != WIDGET_LIST_END) {
00223 SetWidgetLoweredState(widgets, lowered_stat);
00224 widgets = va_arg(wdg_list, int);
00225 }
00226
00227 va_end(wdg_list);
00228 }
00229
00234 void Window::RaiseButtons(bool autoraise)
00235 {
00236 for (uint i = 0; i < this->nested_array_size; i++) {
00237 if (this->nested_array[i] != NULL && (this->nested_array[i]->type & ~WWB_PUSHBUTTON) < WWT_LAST &&
00238 (!autoraise || (this->nested_array[i]->type & WWB_PUSHBUTTON)) && this->IsWidgetLowered(i)) {
00239 this->RaiseWidget(i);
00240 this->SetWidgetDirty(i);
00241 }
00242 }
00243 }
00244
00249 void Window::SetWidgetDirty(byte widget_index) const
00250 {
00251
00252 if (this->nested_array == NULL) return;
00253
00254 this->nested_array[widget_index]->SetDirty(this);
00255 }
00256
00262 void Window::HandleButtonClick(byte widget)
00263 {
00264 this->LowerWidget(widget);
00265 this->flags4 |= WF_TIMEOUT_BEGIN;
00266 this->SetWidgetDirty(widget);
00267 }
00268
00269 static void StartWindowDrag(Window *w);
00270 static void StartWindowSizing(Window *w, bool to_left);
00271
00279 static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
00280 {
00281 NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
00282 WidgetType widget_type = (nw != NULL) ? nw->type : WWT_EMPTY;
00283
00284 bool focused_widget_changed = false;
00285
00286 if (_focused_window != w &&
00287 (w->desc_flags & WDF_NO_FOCUS) == 0 &&
00288 widget_type != WWT_CLOSEBOX) {
00289 focused_widget_changed = true;
00290 if (_focused_window != NULL) {
00291 _focused_window->OnFocusLost();
00292
00293
00294 if (w->window_class != WC_OSK) DeleteWindowById(WC_OSK, 0);
00295 }
00296 SetFocusedWindow(w);
00297 w->OnFocus();
00298 }
00299
00300 if (nw == NULL) return;
00301
00302
00303 if (nw->IsDisabled()) return;
00304
00305 int widget_index = nw->index;
00306
00307
00308
00309 if (widget_type != WWT_CAPTION) {
00310
00311 if (w->nested_focus != NULL && w->nested_focus->type == WWT_EDITBOX && w->nested_focus != nw && w->window_class != WC_OSK) {
00312 DeleteWindowById(WC_OSK, 0);
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 focused_widget_changed |= w->SetFocusedWidget(widget_index);
00325 }
00326
00327
00328
00329 if (HideDropDownMenu(w) == widget_index && widget_index >= 0) return;
00330
00331 if ((widget_type & ~WWB_PUSHBUTTON) < WWT_LAST && (widget_type & WWB_PUSHBUTTON)) w->HandleButtonClick(widget_index);
00332
00333 switch (widget_type) {
00334 case NWID_VSCROLLBAR:
00335 case NWID_HSCROLLBAR:
00336 ScrollbarClickHandler(w, nw, x, y);
00337 break;
00338
00339 case WWT_EDITBOX:
00340 if (!focused_widget_changed) {
00341
00342 QueryStringBaseWindow *qs = dynamic_cast<QueryStringBaseWindow *>(w);
00343 if (qs != NULL) {
00344 qs->OnOpenOSKWindow(widget_index);
00345 }
00346 }
00347 break;
00348
00349 case WWT_CLOSEBOX:
00350 delete w;
00351 return;
00352
00353 case WWT_CAPTION:
00354 StartWindowDrag(w);
00355 return;
00356
00357 case WWT_RESIZEBOX:
00358
00359
00360 StartWindowSizing(w, (int)nw->pos_x < (w->width / 2));
00361 nw->SetDirty(w);
00362 return;
00363
00364 case WWT_DEBUGBOX:
00365 w->ShowNewGRFInspectWindow();
00366 break;
00367
00368 case WWT_SHADEBOX:
00369 nw->SetDirty(w);
00370 w->SetShaded(!w->IsShaded());
00371 return;
00372
00373 case WWT_STICKYBOX:
00374 w->flags4 ^= WF_STICKY;
00375 nw->SetDirty(w);
00376 return;
00377
00378 default:
00379 break;
00380 }
00381
00382
00383 if (widget_index < 0) return;
00384
00385 Point pt = { x, y };
00386 w->OnClick(pt, widget_index, click_count);
00387 }
00388
00395 static void DispatchRightClickEvent(Window *w, int x, int y)
00396 {
00397 NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
00398 if (wid == NULL) return;
00399
00400
00401 if (wid->index >= 0) {
00402 Point pt = { x, y };
00403 if (w->OnRightClick(pt, wid->index)) return;
00404 }
00405
00406 if (_settings_client.gui.hover_delay == 0 && wid->tool_tip != 0) GuiShowTooltips(w, wid->tool_tip, 0, NULL, TCC_RIGHT_CLICK);
00407 }
00408
00415 static void DispatchHoverEvent(Window *w, int x, int y)
00416 {
00417 NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
00418
00419
00420 if (wid == NULL) return;
00421
00422
00423 if (wid->tool_tip != 0) {
00424 GuiShowTooltips(w, wid->tool_tip);
00425 return;
00426 }
00427
00428
00429 if (wid->index < 0) return;
00430
00431 Point pt = { x, y };
00432 w->OnHover(pt, wid->index);
00433 }
00434
00442 static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
00443 {
00444 if (nwid == NULL) return;
00445
00446
00447 if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
00448 w->SetShaded(wheel < 0);
00449 return;
00450 }
00451
00452
00453 if (nwid->type == NWID_VSCROLLBAR) {
00454 NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
00455 if (sb->GetCount() > sb->GetCapacity()) {
00456 sb->UpdatePosition(wheel);
00457 w->SetDirty();
00458 }
00459 return;
00460 }
00461
00462
00463 Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : NULL);
00464 if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
00465 sb->UpdatePosition(wheel);
00466 w->SetDirty();
00467 }
00468 }
00469
00482 static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
00483 {
00484 const Window *v;
00485 FOR_ALL_WINDOWS_FROM_BACK_FROM(v, w->z_front) {
00486 if (right > v->left &&
00487 bottom > v->top &&
00488 left < v->left + v->width &&
00489 top < v->top + v->height) {
00490
00491 int x;
00492
00493 if (left < (x = v->left)) {
00494 DrawOverlappedWindow(w, left, top, x, bottom);
00495 DrawOverlappedWindow(w, x, top, right, bottom);
00496 return;
00497 }
00498
00499 if (right > (x = v->left + v->width)) {
00500 DrawOverlappedWindow(w, left, top, x, bottom);
00501 DrawOverlappedWindow(w, x, top, right, bottom);
00502 return;
00503 }
00504
00505 if (top < (x = v->top)) {
00506 DrawOverlappedWindow(w, left, top, right, x);
00507 DrawOverlappedWindow(w, left, x, right, bottom);
00508 return;
00509 }
00510
00511 if (bottom > (x = v->top + v->height)) {
00512 DrawOverlappedWindow(w, left, top, right, x);
00513 DrawOverlappedWindow(w, left, x, right, bottom);
00514 return;
00515 }
00516
00517 return;
00518 }
00519 }
00520
00521
00522 DrawPixelInfo *dp = _cur_dpi;
00523 dp->width = right - left;
00524 dp->height = bottom - top;
00525 dp->left = left - w->left;
00526 dp->top = top - w->top;
00527 dp->pitch = _screen.pitch;
00528 dp->dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
00529 dp->zoom = ZOOM_LVL_NORMAL;
00530 w->OnPaint();
00531 }
00532
00541 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
00542 {
00543 Window *w;
00544 DrawPixelInfo bk;
00545 _cur_dpi = &bk;
00546
00547 FOR_ALL_WINDOWS_FROM_BACK(w) {
00548 if (right > w->left &&
00549 bottom > w->top &&
00550 left < w->left + w->width &&
00551 top < w->top + w->height) {
00552
00553 DrawOverlappedWindow(w, left, top, right, bottom);
00554 }
00555 }
00556 }
00557
00562 void Window::SetDirty() const
00563 {
00564 SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
00565 }
00566
00573 void Window::ReInit(int rx, int ry)
00574 {
00575 this->SetDirty();
00576
00577
00578 int window_width = this->width;
00579 int window_height = this->height;
00580
00581 this->OnInit();
00582
00583 this->nested_root->SetupSmallestSize(this, false);
00584 this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
00585 this->width = this->nested_root->smallest_x;
00586 this->height = this->nested_root->smallest_y;
00587 this->resize.step_width = this->nested_root->resize_x;
00588 this->resize.step_height = this->nested_root->resize_y;
00589
00590
00591 window_width = max(window_width + rx, this->width);
00592 window_height = max(window_height + ry, this->height);
00593 int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
00594 int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
00595
00596
00597 if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
00598 if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
00599
00600 ResizeWindow(this, dx, dy);
00601
00602 }
00603
00609 void Window::SetShaded(bool make_shaded)
00610 {
00611 if (this->shade_select == NULL) return;
00612
00613 int desired = make_shaded ? SZSP_HORIZONTAL : 0;
00614 if (this->shade_select->shown_plane != desired) {
00615 if (make_shaded) {
00616 this->unshaded_size.width = this->width;
00617 this->unshaded_size.height = this->height;
00618 this->shade_select->SetDisplayedPlane(desired);
00619 this->ReInit(0, -this->height);
00620 } else {
00621 this->shade_select->SetDisplayedPlane(desired);
00622 int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
00623 int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
00624 this->ReInit(dx, dy);
00625 }
00626 }
00627 }
00628
00635 static Window *FindChildWindow(const Window *w, WindowClass wc)
00636 {
00637 Window *v;
00638 FOR_ALL_WINDOWS_FROM_BACK(v) {
00639 if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
00640 }
00641
00642 return NULL;
00643 }
00644
00649 void Window::DeleteChildWindows(WindowClass wc) const
00650 {
00651 Window *child = FindChildWindow(this, wc);
00652 while (child != NULL) {
00653 delete child;
00654 child = FindChildWindow(this, wc);
00655 }
00656 }
00657
00661 Window::~Window()
00662 {
00663 if (_thd.window_class == this->window_class &&
00664 _thd.window_number == this->window_number) {
00665 ResetObjectToPlace();
00666 }
00667
00668
00669 if (_mouseover_last_w == this) _mouseover_last_w = NULL;
00670
00671
00672 if (_last_scroll_window == this) _last_scroll_window = NULL;
00673
00674
00675 if (_focused_window == this) _focused_window = NULL;
00676
00677 this->DeleteChildWindows();
00678
00679 if (this->viewport != NULL) DeleteWindowViewport(this);
00680
00681 this->SetDirty();
00682
00683 free(this->nested_array);
00684 delete this->nested_root;
00685
00686 this->window_class = WC_INVALID;
00687 }
00688
00695 Window *FindWindowById(WindowClass cls, WindowNumber number)
00696 {
00697 Window *w;
00698 FOR_ALL_WINDOWS_FROM_BACK(w) {
00699 if (w->window_class == cls && w->window_number == number) return w;
00700 }
00701
00702 return NULL;
00703 }
00704
00711 Window *FindWindowByClass(WindowClass cls)
00712 {
00713 Window *w;
00714 FOR_ALL_WINDOWS_FROM_BACK(w) {
00715 if (w->window_class == cls) return w;
00716 }
00717
00718 return NULL;
00719 }
00720
00727 void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
00728 {
00729 Window *w = FindWindowById(cls, number);
00730 if (force || w == NULL ||
00731 (w->flags4 & WF_STICKY) == 0) {
00732 delete w;
00733 }
00734 }
00735
00740 void DeleteWindowByClass(WindowClass cls)
00741 {
00742 Window *w;
00743
00744 restart_search:
00745
00746
00747
00748 FOR_ALL_WINDOWS_FROM_BACK(w) {
00749 if (w->window_class == cls) {
00750 delete w;
00751 goto restart_search;
00752 }
00753 }
00754 }
00755
00762 void DeleteCompanyWindows(CompanyID id)
00763 {
00764 Window *w;
00765
00766 restart_search:
00767
00768
00769
00770 FOR_ALL_WINDOWS_FROM_BACK(w) {
00771 if (w->owner == id) {
00772 delete w;
00773 goto restart_search;
00774 }
00775 }
00776
00777
00778 DeleteWindowById(WC_BUY_COMPANY, id);
00779 }
00780
00788 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
00789 {
00790 Window *w;
00791 FOR_ALL_WINDOWS_FROM_BACK(w) {
00792 if (w->owner != old_owner) continue;
00793
00794 switch (w->window_class) {
00795 case WC_COMPANY_COLOUR:
00796 case WC_FINANCES:
00797 case WC_STATION_LIST:
00798 case WC_TRAINS_LIST:
00799 case WC_ROADVEH_LIST:
00800 case WC_SHIPS_LIST:
00801 case WC_AIRCRAFT_LIST:
00802 case WC_BUY_COMPANY:
00803 case WC_COMPANY:
00804 continue;
00805
00806 default:
00807 w->owner = new_owner;
00808 break;
00809 }
00810 }
00811 }
00812
00813 static void BringWindowToFront(Window *w);
00814
00822 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number)
00823 {
00824 Window *w = FindWindowById(cls, number);
00825
00826 if (w != NULL) {
00827 if (w->IsShaded()) w->SetShaded(false);
00828
00829 w->flags4 |= WF_WHITE_BORDER_MASK;
00830 BringWindowToFront(w);
00831 w->SetDirty();
00832 }
00833
00834 return w;
00835 }
00836
00837 static inline bool IsVitalWindow(const Window *w)
00838 {
00839 switch (w->window_class) {
00840 case WC_MAIN_TOOLBAR:
00841 case WC_STATUS_BAR:
00842 case WC_NEWS_WINDOW:
00843 case WC_SEND_NETWORK_MSG:
00844 return true;
00845
00846 default:
00847 return false;
00848 }
00849 }
00850
00860 static void BringWindowToFront(Window *w)
00861 {
00862 Window *v = _z_front_window;
00863
00864
00865 for (; v != NULL && v != w && IsVitalWindow(v); v = v->z_back) { }
00866
00867 if (v == NULL || w == v) return;
00868
00869
00870 assert(w != _z_front_window);
00871
00872 if (w->z_back == NULL) {
00873 _z_back_window = w->z_front;
00874 } else {
00875 w->z_back->z_front = w->z_front;
00876 }
00877 w->z_front->z_back = w->z_back;
00878
00879 w->z_front = v->z_front;
00880 w->z_back = v;
00881
00882 if (v->z_front == NULL) {
00883 _z_front_window = w;
00884 } else {
00885 v->z_front->z_back = w;
00886 }
00887 v->z_front = w;
00888
00889 w->SetDirty();
00890 }
00891
00900 void Window::InitializeData(const WindowDesc *desc, WindowNumber window_number)
00901 {
00902
00903 this->window_class = desc->cls;
00904 this->flags4 |= WF_WHITE_BORDER_MASK;
00905 if (desc->default_pos == WDP_CENTER) this->flags4 |= WF_CENTERED;
00906 this->owner = INVALID_OWNER;
00907 this->nested_focus = NULL;
00908 this->window_number = window_number;
00909 this->desc_flags = desc->flags;
00910
00911 this->OnInit();
00912
00913 if (this->nested_array == NULL) {
00914 this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
00915 this->nested_root->SetupSmallestSize(this, true);
00916 } else {
00917 this->nested_root->SetupSmallestSize(this, false);
00918 }
00919
00920 this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
00921
00922
00923
00924 this->resize.step_width = this->nested_root->resize_x;
00925 this->resize.step_height = this->nested_root->resize_y;
00926
00927
00928
00929
00930 if (this->window_class != WC_OSK && (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != NULL)) SetFocusedWindow(this);
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940 Window *w = _z_front_window;
00941 if (w != NULL && this->window_class != WC_SEND_NETWORK_MSG && this->window_class != WC_HIGHSCORE && this->window_class != WC_ENDSCREEN) {
00942 if (FindWindowById(WC_MAIN_TOOLBAR, 0) != NULL) w = w->z_back;
00943 if (FindWindowById(WC_STATUS_BAR, 0) != NULL) w = w->z_back;
00944 if (FindWindowById(WC_NEWS_WINDOW, 0) != NULL) w = w->z_back;
00945 if (FindWindowByClass(WC_SEND_NETWORK_MSG) != NULL) w = w->z_back;
00946
00947 if (w == NULL) {
00948 _z_back_window->z_front = this;
00949 this->z_back = _z_back_window;
00950 _z_back_window = this;
00951 } else {
00952 if (w->z_front == NULL) {
00953 _z_front_window = this;
00954 } else {
00955 this->z_front = w->z_front;
00956 w->z_front->z_back = this;
00957 }
00958
00959 this->z_back = w;
00960 w->z_front = this;
00961 }
00962 } else {
00963 this->z_back = _z_front_window;
00964 if (_z_front_window != NULL) {
00965 _z_front_window->z_front = this;
00966 } else {
00967 _z_back_window = this;
00968 }
00969 _z_front_window = this;
00970 }
00971 }
00972
00980 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
00981 {
00982 this->left = x;
00983 this->top = y;
00984 this->width = sm_width;
00985 this->height = sm_height;
00986 }
00987
00998 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
00999 {
01000 def_width = max(def_width, this->width);
01001 def_height = max(def_height, this->height);
01002
01003
01004
01005
01006
01007 if (this->width != def_width || this->height != def_height) {
01008
01009 int free_height = _screen.height;
01010 const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
01011 if (wt != NULL) free_height -= wt->height;
01012 wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
01013 if (wt != NULL) free_height -= wt->height;
01014
01015 int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
01016 int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
01017
01018
01019
01020
01021 if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
01022 if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
01023
01024 ResizeWindow(this, enlarge_x, enlarge_y);
01025
01026 } else {
01027
01028 this->OnResize();
01029 }
01030
01031 int nx = this->left;
01032 int ny = this->top;
01033
01034 if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
01035
01036 const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
01037 ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
01038 nx = max(nx, 0);
01039
01040 if (this->viewport != NULL) {
01041 this->viewport->left += nx - this->left;
01042 this->viewport->top += ny - this->top;
01043 }
01044 this->left = nx;
01045 this->top = ny;
01046
01047 this->SetDirty();
01048 }
01049
01061 static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos)
01062 {
01063 int right = width + left;
01064 int bottom = height + top;
01065
01066 const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
01067 if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height) return false;
01068
01069
01070 const Window *w;
01071 FOR_ALL_WINDOWS_FROM_BACK(w) {
01072 if (w->window_class == WC_MAIN_WINDOW) continue;
01073
01074 if (right > w->left &&
01075 w->left + w->width > left &&
01076 bottom > w->top &&
01077 w->top + w->height > top) {
01078 return false;
01079 }
01080 }
01081
01082 pos.x = left;
01083 pos.y = top;
01084 return true;
01085 }
01086
01098 static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos)
01099 {
01100
01101
01102
01103 if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
01104
01105 if (top < 22 || top > _screen.height - (height >> 2)) return false;
01106
01107
01108 const Window *w;
01109 FOR_ALL_WINDOWS_FROM_BACK(w) {
01110 if (w->window_class == WC_MAIN_WINDOW) continue;
01111
01112 if (left + width > w->left &&
01113 w->left + w->width > left &&
01114 top + height > w->top &&
01115 w->top + w->height > top) {
01116 return false;
01117 }
01118 }
01119
01120 pos.x = left;
01121 pos.y = top;
01122 return true;
01123 }
01124
01131 static Point GetAutoPlacePosition(int width, int height)
01132 {
01133 Point pt;
01134
01135
01136 const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
01137 if (IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->height + 2 : 2, width, height, pt)) return pt;
01138
01139
01140
01141
01142
01143 const Window *w;
01144 FOR_ALL_WINDOWS_FROM_BACK(w) {
01145 if (w->window_class == WC_MAIN_WINDOW) continue;
01146
01147 if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt;
01148 if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt;
01149 if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt;
01150 if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt;
01151 if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt;
01152 if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt;
01153 if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt;
01154 if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt;
01155 }
01156
01157
01158
01159
01160
01161 FOR_ALL_WINDOWS_FROM_BACK(w) {
01162 if (w->window_class == WC_MAIN_WINDOW) continue;
01163
01164 if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt;
01165 if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt;
01166 if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt;
01167 if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt;
01168 }
01169
01170
01171
01172
01173 int left = 0, top = 24;
01174
01175 restart:
01176 FOR_ALL_WINDOWS_FROM_BACK(w) {
01177 if (w->left == left && w->top == top) {
01178 left += 5;
01179 top += 5;
01180 goto restart;
01181 }
01182 }
01183
01184 pt.x = left;
01185 pt.y = top;
01186 return pt;
01187 }
01188
01195 Point GetToolbarAlignedWindowPosition(int window_width)
01196 {
01197 const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
01198 assert(w != NULL);
01199 Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
01200 return pt;
01201 }
01202
01220 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
01221 {
01222 Point pt;
01223 const Window *w;
01224
01225 int16 default_width = max(desc->default_width, sm_width);
01226 int16 default_height = max(desc->default_height, sm_height);
01227
01228 if (desc->parent_cls != 0 &&
01229 (w = FindWindowById(desc->parent_cls, window_number)) != NULL &&
01230 w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) {
01231
01232 pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10);
01233 if (pt.x > _screen.width + 10 - default_width) {
01234 pt.x = (_screen.width + 10 - default_width) - 20;
01235 }
01236 pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? w->height : 10);
01237 return pt;
01238 }
01239
01240 switch (desc->default_pos) {
01241 case WDP_ALIGN_TOOLBAR:
01242 return GetToolbarAlignedWindowPosition(default_width);
01243
01244 case WDP_AUTO:
01245 return GetAutoPlacePosition(default_width, default_height);
01246
01247 case WDP_CENTER:
01248 pt.x = (_screen.width - default_width) / 2;
01249 pt.y = (_screen.height - default_height) / 2;
01250 break;
01251
01252 case WDP_MANUAL:
01253 pt.x = 0;
01254 pt.y = 0;
01255 break;
01256
01257 default:
01258 NOT_REACHED();
01259 }
01260
01261 return pt;
01262 }
01263
01264 Point Window::OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
01265 {
01266 return LocalGetWindowPlacement(desc, sm_width, sm_height, window_number);
01267 }
01268
01277 void Window::CreateNestedTree(const WindowDesc *desc, bool fill_nested)
01278 {
01279 int biggest_index = -1;
01280 this->nested_root = MakeWindowNWidgetTree(desc->nwid_parts, desc->nwid_length, &biggest_index, &this->shade_select);
01281 this->nested_array_size = (uint)(biggest_index + 1);
01282
01283 if (fill_nested) {
01284 this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
01285 this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
01286 }
01287 }
01288
01294 void Window::FinishInitNested(const WindowDesc *desc, WindowNumber window_number)
01295 {
01296 this->InitializeData(desc, window_number);
01297 Point pt = this->OnInitialPosition(desc, this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
01298 this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
01299 this->FindWindowPlacementAndResize(desc->default_width, desc->default_height);
01300 }
01301
01307 void Window::InitNested(const WindowDesc *desc, WindowNumber window_number)
01308 {
01309 this->CreateNestedTree(desc, false);
01310 this->FinishInitNested(desc, window_number);
01311 }
01312
01314 Window::Window() : scrolling_scrollbar(-1)
01315 {
01316 }
01317
01325 Window *FindWindowFromPt(int x, int y)
01326 {
01327 Window *w;
01328 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01329 if (IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
01330 return w;
01331 }
01332 }
01333
01334 return NULL;
01335 }
01336
01340 void InitWindowSystem()
01341 {
01342 IConsoleClose();
01343
01344 _z_back_window = NULL;
01345 _z_front_window = NULL;
01346 _focused_window = NULL;
01347 _mouseover_last_w = NULL;
01348 _last_scroll_window = NULL;
01349 _scrolling_viewport = false;
01350 _mouse_hovering = false;
01351
01352 NWidgetLeaf::InvalidateDimensionCache();
01353 }
01354
01358 void UnInitWindowSystem()
01359 {
01360 Window *w;
01361 FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
01362
01363 for (w = _z_front_window; w != NULL; ) {
01364 Window *to_del = w;
01365 w = w->z_back;
01366 free(to_del);
01367 }
01368
01369 _z_front_window = NULL;
01370 _z_back_window = NULL;
01371 }
01372
01376 void ResetWindowSystem()
01377 {
01378 UnInitWindowSystem();
01379 InitWindowSystem();
01380 _thd.Reset();
01381 }
01382
01383 static void DecreaseWindowCounters()
01384 {
01385 Window *w;
01386 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01387 if (_scroller_click_timeout == 0) {
01388
01389 for (uint i = 0; i < w->nested_array_size; i++) {
01390 NWidgetBase *nwid = w->nested_array[i];
01391 if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
01392 NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
01393 if (sb->disp_flags & (ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN)) {
01394 sb->disp_flags &= ~(ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN);
01395 sb->SetDirty(w);
01396 }
01397 }
01398 }
01399 }
01400 w->OnMouseLoop();
01401 }
01402
01403 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01404 if ((w->flags4 & WF_TIMEOUT_MASK) && !(--w->flags4 & WF_TIMEOUT_MASK)) {
01405 w->OnTimeout();
01406 if (w->desc_flags & WDF_UNCLICK_BUTTONS) w->RaiseButtons(true);
01407 }
01408 }
01409 }
01410
01411 static void HandlePlacePresize()
01412 {
01413 if (_special_mouse_mode != WSM_PRESIZE) return;
01414
01415 Window *w = _thd.GetCallbackWnd();
01416 if (w == NULL) return;
01417
01418 Point pt = GetTileBelowCursor();
01419 if (pt.x == -1) {
01420 _thd.selend.x = -1;
01421 return;
01422 }
01423
01424 w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
01425 }
01426
01431 static EventState HandleMouseDragDrop()
01432 {
01433 if (_special_mouse_mode != WSM_DRAGDROP) return ES_NOT_HANDLED;
01434
01435 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
01436
01437 Window *w = _thd.GetCallbackWnd();
01438 if (w != NULL) {
01439
01440 Point pt;
01441 pt.x = _cursor.pos.x - w->left;
01442 pt.y = _cursor.pos.y - w->top;
01443 if (_left_button_down) {
01444 w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
01445 } else {
01446 w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
01447 }
01448 }
01449
01450 if (!_left_button_down) ResetObjectToPlace();
01451 return ES_HANDLED;
01452 }
01453
01455 static void HandleMouseOver()
01456 {
01457 Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
01458
01459
01460 if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
01461
01462 Point pt = { -1, -1 };
01463 _mouseover_last_w->OnMouseOver(pt, 0);
01464 }
01465
01466
01467 _mouseover_last_w = w;
01468
01469 if (w != NULL) {
01470
01471 Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
01472 const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
01473 if (widget != NULL) w->OnMouseOver(pt, widget->index);
01474 }
01475 }
01476
01478 static const int MIN_VISIBLE_TITLE_BAR = 13;
01479
01481 enum PreventHideDirection {
01482 PHD_UP,
01483 PHD_DOWN,
01484 };
01485
01496 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
01497 {
01498 if (v == NULL) return;
01499
01500 int v_bottom = v->top + v->height;
01501 int v_right = v->left + v->width;
01502 int safe_y = (dir == PHD_UP) ? (v->top - MIN_VISIBLE_TITLE_BAR - rect.top) : (v_bottom + MIN_VISIBLE_TITLE_BAR - rect.bottom);
01503
01504 if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return;
01505 if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return;
01506
01507
01508 if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) {
01509 if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
01510 return;
01511 }
01512 if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) {
01513 if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
01514 return;
01515 }
01516
01517
01518 if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) {
01519 *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
01520 } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) {
01521 *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
01522 } else {
01523 *ny = safe_y;
01524 }
01525 }
01526
01534 static void EnsureVisibleCaption(Window *w, int nx, int ny)
01535 {
01536
01537 Rect caption_rect;
01538 const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
01539 if (caption != NULL) {
01540 caption_rect.left = caption->pos_x;
01541 caption_rect.right = caption->pos_x + caption->current_x;
01542 caption_rect.top = caption->pos_y;
01543 caption_rect.bottom = caption->pos_y + caption->current_y;
01544
01545
01546 nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
01547 ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
01548
01549
01550 PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
01551 PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
01552
01553 if (w->viewport != NULL) {
01554 w->viewport->left += nx - w->left;
01555 w->viewport->top += ny - w->top;
01556 }
01557 }
01558 w->left = nx;
01559 w->top = ny;
01560 }
01561
01571 void ResizeWindow(Window *w, int delta_x, int delta_y)
01572 {
01573 if (delta_x != 0 || delta_y != 0) {
01574 w->SetDirty();
01575
01576 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);
01577 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);
01578 assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
01579 assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
01580
01581 w->nested_root->AssignSizePosition(ST_RESIZE, 0, 0, w->nested_root->smallest_x + new_xinc, w->nested_root->smallest_y + new_yinc, _current_text_dir == TD_RTL);
01582 w->width = w->nested_root->current_x;
01583 w->height = w->nested_root->current_y;
01584 }
01585
01586 EnsureVisibleCaption(w, w->left, w->top);
01587
01588
01589 w->OnResize();
01590 w->SetDirty();
01591 }
01592
01598 int GetMainViewTop()
01599 {
01600 Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
01601 return (w == NULL) ? 0 : w->top + w->height;
01602 }
01603
01609 int GetMainViewBottom()
01610 {
01611 Window *w = FindWindowById(WC_STATUS_BAR, 0);
01612 return (w == NULL) ? _screen.height : w->top;
01613 }
01614
01615 static bool _dragging_window;
01616
01621 static EventState HandleWindowDragging()
01622 {
01623
01624 if (!_dragging_window) return ES_NOT_HANDLED;
01625
01626
01627 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
01628
01629
01630 Window *w;
01631 FOR_ALL_WINDOWS_FROM_BACK(w) {
01632 if (w->flags4 & WF_DRAGGING) {
01633
01634 if (!_left_button_down) {
01635 w->flags4 &= ~WF_DRAGGING;
01636 break;
01637 }
01638
01639 w->SetDirty();
01640
01641 int x = _cursor.pos.x + _drag_delta.x;
01642 int y = _cursor.pos.y + _drag_delta.y;
01643 int nx = x;
01644 int ny = y;
01645
01646 if (_settings_client.gui.window_snap_radius != 0) {
01647 const Window *v;
01648
01649 int hsnap = _settings_client.gui.window_snap_radius;
01650 int vsnap = _settings_client.gui.window_snap_radius;
01651 int delta;
01652
01653 FOR_ALL_WINDOWS_FROM_BACK(v) {
01654 if (v == w) continue;
01655
01656 if (y + w->height > v->top && y < v->top + v->height) {
01657
01658 delta = abs(v->left + v->width - x);
01659 if (delta <= hsnap) {
01660 nx = v->left + v->width;
01661 hsnap = delta;
01662 }
01663
01664
01665 delta = abs(v->left - x - w->width);
01666 if (delta <= hsnap) {
01667 nx = v->left - w->width;
01668 hsnap = delta;
01669 }
01670 }
01671
01672 if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
01673
01674 delta = abs(v->left - x);
01675 if (delta <= hsnap) {
01676 nx = v->left;
01677 hsnap = delta;
01678 }
01679
01680
01681 delta = abs(v->left + v->width - x - w->width);
01682 if (delta <= hsnap) {
01683 nx = v->left + v->width - w->width;
01684 hsnap = delta;
01685 }
01686 }
01687
01688 if (x + w->width > v->left && x < v->left + v->width) {
01689
01690 delta = abs(v->top + v->height - y);
01691 if (delta <= vsnap) {
01692 ny = v->top + v->height;
01693 vsnap = delta;
01694 }
01695
01696
01697 delta = abs(v->top - y - w->height);
01698 if (delta <= vsnap) {
01699 ny = v->top - w->height;
01700 vsnap = delta;
01701 }
01702 }
01703
01704 if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
01705
01706 delta = abs(v->top - y);
01707 if (delta <= vsnap) {
01708 ny = v->top;
01709 vsnap = delta;
01710 }
01711
01712
01713 delta = abs(v->top + v->height - y - w->height);
01714 if (delta <= vsnap) {
01715 ny = v->top + v->height - w->height;
01716 vsnap = delta;
01717 }
01718 }
01719 }
01720 }
01721
01722 EnsureVisibleCaption(w, nx, ny);
01723
01724 w->SetDirty();
01725 return ES_HANDLED;
01726 } else if (w->flags4 & WF_SIZING) {
01727
01728 if (!_left_button_down) {
01729 w->flags4 &= ~WF_SIZING;
01730 w->SetDirty();
01731 break;
01732 }
01733
01734
01735
01736
01737 int x, y = _cursor.pos.y - _drag_delta.y;
01738 if (w->flags4 & WF_SIZING_LEFT) {
01739 x = _drag_delta.x - _cursor.pos.x;
01740 } else {
01741 x = _cursor.pos.x - _drag_delta.x;
01742 }
01743
01744
01745 if (w->resize.step_width == 0) x = 0;
01746 if (w->resize.step_height == 0) y = 0;
01747
01748
01749 if (w->top + w->height + y > _screen.height) {
01750 y = _screen.height - w->height - w->top;
01751 }
01752
01753
01754
01755
01756 if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
01757 if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
01758
01759
01760 if ((int)w->width + x < (int)w->nested_root->smallest_x) {
01761 x = w->nested_root->smallest_x - w->width;
01762 }
01763 if ((int)w->height + y < (int)w->nested_root->smallest_y) {
01764 y = w->nested_root->smallest_y - w->height;
01765 }
01766
01767
01768 if (x == 0 && y == 0) return ES_HANDLED;
01769
01770
01771 _drag_delta.y += y;
01772 if ((w->flags4 & WF_SIZING_LEFT) && x != 0) {
01773 _drag_delta.x -= x;
01774 w->SetDirty();
01775 w->left -= x;
01776
01777 } else {
01778 _drag_delta.x += x;
01779 }
01780
01781
01782 ResizeWindow(w, x, y);
01783 return ES_HANDLED;
01784 }
01785 }
01786
01787 _dragging_window = false;
01788 return ES_HANDLED;
01789 }
01790
01795 static void StartWindowDrag(Window *w)
01796 {
01797 w->flags4 |= WF_DRAGGING;
01798 w->flags4 &= ~WF_CENTERED;
01799 _dragging_window = true;
01800
01801 _drag_delta.x = w->left - _cursor.pos.x;
01802 _drag_delta.y = w->top - _cursor.pos.y;
01803
01804 BringWindowToFront(w);
01805 DeleteWindowById(WC_DROPDOWN_MENU, 0);
01806 }
01807
01813 static void StartWindowSizing(Window *w, bool to_left)
01814 {
01815 w->flags4 |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
01816 w->flags4 &= ~WF_CENTERED;
01817 _dragging_window = true;
01818
01819 _drag_delta.x = _cursor.pos.x;
01820 _drag_delta.y = _cursor.pos.y;
01821
01822 BringWindowToFront(w);
01823 DeleteWindowById(WC_DROPDOWN_MENU, 0);
01824 }
01825
01830 static EventState HandleScrollbarScrolling()
01831 {
01832 Window *w;
01833 FOR_ALL_WINDOWS_FROM_BACK(w) {
01834 if (w->scrolling_scrollbar >= 0) {
01835
01836 if (!_left_button_down) {
01837 w->scrolling_scrollbar = -1;
01838 w->SetDirty();
01839 return ES_HANDLED;
01840 }
01841
01842 int i;
01843 NWidgetScrollbar *sb = w->GetWidget<NWidgetScrollbar>(w->scrolling_scrollbar);
01844 bool rtl = false;
01845
01846 if (sb->type == NWID_HSCROLLBAR) {
01847 i = _cursor.pos.x - _cursorpos_drag_start.x;
01848 rtl = _current_text_dir == TD_RTL;
01849 } else {
01850 i = _cursor.pos.y - _cursorpos_drag_start.y;
01851 }
01852
01853 if (sb->disp_flags & ND_SCROLLBAR_BTN) {
01854 if (_scroller_click_timeout == 1) {
01855 _scroller_click_timeout = 3;
01856 sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
01857 w->SetDirty();
01858 }
01859 return ES_HANDLED;
01860 }
01861
01862
01863 int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
01864 if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
01865 if (pos != sb->GetPosition()) {
01866 sb->SetPosition(pos);
01867 w->SetDirty();
01868 }
01869 return ES_HANDLED;
01870 }
01871 }
01872
01873 return ES_NOT_HANDLED;
01874 }
01875
01880 static EventState HandleViewportScroll()
01881 {
01882 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
01883
01884 if (!_scrolling_viewport) return ES_NOT_HANDLED;
01885
01886
01887
01888
01889 if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
01890
01891 if (_last_scroll_window == NULL || !(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down))) {
01892 _cursor.fix_at = false;
01893 _scrolling_viewport = false;
01894 _last_scroll_window = NULL;
01895 return ES_NOT_HANDLED;
01896 }
01897
01898 if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
01899
01900 const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
01901 ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true);
01902 return ES_NOT_HANDLED;
01903 }
01904
01905 Point delta;
01906 if (_settings_client.gui.reverse_scroll || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down)) {
01907 delta.x = -_cursor.delta.x;
01908 delta.y = -_cursor.delta.y;
01909 } else {
01910 delta.x = _cursor.delta.x;
01911 delta.y = _cursor.delta.y;
01912 }
01913
01914 if (scrollwheel_scrolling) {
01915
01916 delta.x = _cursor.h_wheel;
01917 delta.y = _cursor.v_wheel;
01918 _cursor.v_wheel = 0;
01919 _cursor.h_wheel = 0;
01920 }
01921
01922
01923 if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
01924
01925 _cursor.delta.x = 0;
01926 _cursor.delta.y = 0;
01927 return ES_HANDLED;
01928 }
01929
01940 static bool MaybeBringWindowToFront(Window *w)
01941 {
01942 bool bring_to_front = false;
01943
01944 if (w->window_class == WC_MAIN_WINDOW ||
01945 IsVitalWindow(w) ||
01946 w->window_class == WC_TOOLTIPS ||
01947 w->window_class == WC_DROPDOWN_MENU) {
01948 return true;
01949 }
01950
01951
01952 int w_width = w->width;
01953 int w_height = w->height;
01954 if (w->IsShaded()) {
01955 w_width = w->unshaded_size.width;
01956 w_height = w->unshaded_size.height;
01957 }
01958
01959 Window *u;
01960 FOR_ALL_WINDOWS_FROM_BACK_FROM(u, w->z_front) {
01961
01962 if (u->parent == w && (u->desc_flags & WDF_MODAL)) {
01963 u->flags4 |= WF_WHITE_BORDER_MASK;
01964 u->SetDirty();
01965 return false;
01966 }
01967
01968 if (u->window_class == WC_MAIN_WINDOW ||
01969 IsVitalWindow(u) ||
01970 u->window_class == WC_TOOLTIPS ||
01971 u->window_class == WC_DROPDOWN_MENU) {
01972 continue;
01973 }
01974
01975
01976 if (w->left + w_width <= u->left ||
01977 u->left + u->width <= w->left ||
01978 w->top + w_height <= u->top ||
01979 u->top + u->height <= w->top) {
01980 continue;
01981 }
01982
01983 bring_to_front = true;
01984 }
01985
01986 if (bring_to_front) BringWindowToFront(w);
01987 return true;
01988 }
01989
01994 void HandleKeypress(uint32 raw_key)
01995 {
01996
01997
01998 assert(IsGeneratingWorld() || IsLocalCompany());
01999
02000
02001 uint16 key = GB(raw_key, 0, 16);
02002 uint16 keycode = GB(raw_key, 16, 16);
02003
02004
02005
02006
02007
02008
02009
02010
02011 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
02012
02013
02014
02015
02016 if (key == 0 && keycode == 0) return;
02017
02018
02019 if (EditBoxInGlobalFocus()) {
02020
02021 if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
02022 }
02023
02024
02025 Window *w;
02026 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02027 if (w->window_class == WC_MAIN_TOOLBAR) continue;
02028 if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
02029 }
02030
02031 w = FindWindowById(WC_MAIN_TOOLBAR, 0);
02032
02033 if (w != NULL && w->OnKeyPress(key, keycode) == ES_HANDLED) return;
02034
02035 HandleGlobalHotkeys(key, keycode);
02036 }
02037
02041 void HandleCtrlChanged()
02042 {
02043
02044 Window *w;
02045 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02046 if (w->OnCTRLStateChange() == ES_HANDLED) return;
02047 }
02048 }
02049
02056 static int _input_events_this_tick = 0;
02057
02062 static void HandleAutoscroll()
02063 {
02064 if (_settings_client.gui.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) {
02065 int x = _cursor.pos.x;
02066 int y = _cursor.pos.y;
02067 Window *w = FindWindowFromPt(x, y);
02068 if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return;
02069 ViewPort *vp = IsPtInWindowViewport(w, x, y);
02070 if (vp != NULL) {
02071 x -= vp->left;
02072 y -= vp->top;
02073
02074
02075 #define scrollspeed 3
02076 if (x - 15 < 0) {
02077 w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
02078 } else if (15 - (vp->width - x) > 0) {
02079 w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
02080 }
02081 if (y - 15 < 0) {
02082 w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
02083 } else if (15 - (vp->height - y) > 0) {
02084 w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
02085 }
02086 #undef scrollspeed
02087 }
02088 }
02089 }
02090
02091 enum MouseClick {
02092 MC_NONE = 0,
02093 MC_LEFT,
02094 MC_RIGHT,
02095 MC_DOUBLE_LEFT,
02096 MC_HOVER,
02097
02098 MAX_OFFSET_DOUBLE_CLICK = 5,
02099 TIME_BETWEEN_DOUBLE_CLICK = 500,
02100 MAX_OFFSET_HOVER = 5,
02101 };
02102 extern EventState VpHandlePlaceSizingDrag();
02103
02104 static void ScrollMainViewport(int x, int y)
02105 {
02106 if (_game_mode != GM_MENU) {
02107 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
02108 assert(w);
02109
02110 w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
02111 w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
02112 }
02113 }
02114
02124 static const int8 scrollamt[16][2] = {
02125 { 0, 0},
02126 {-2, 0},
02127 { 0, -2},
02128 {-2, -1},
02129 { 2, 0},
02130 { 0, 0},
02131 { 2, -1},
02132 { 0, -2},
02133 { 0, 2},
02134 {-2, 1},
02135 { 0, 0},
02136 {-2, 0},
02137 { 2, 1},
02138 { 0, 2},
02139 { 2, 0},
02140 { 0, 0},
02141 };
02142
02143 static void HandleKeyScrolling()
02144 {
02145
02146
02147
02148
02149 if (_dirkeys && !EditBoxInGlobalFocus()) {
02150 int factor = _shift_pressed ? 50 : 10;
02151 ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
02152 }
02153 }
02154
02155 static void MouseLoop(MouseClick click, int mousewheel)
02156 {
02157
02158
02159 assert(IsGeneratingWorld() || IsLocalCompany());
02160
02161 HandlePlacePresize();
02162 UpdateTileSelection();
02163
02164 if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
02165 if (HandleMouseDragDrop() == ES_HANDLED) return;
02166 if (HandleWindowDragging() == ES_HANDLED) return;
02167 if (HandleScrollbarScrolling() == ES_HANDLED) return;
02168 if (HandleViewportScroll() == ES_HANDLED) return;
02169
02170 HandleMouseOver();
02171
02172 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
02173 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
02174
02175 int x = _cursor.pos.x;
02176 int y = _cursor.pos.y;
02177 Window *w = FindWindowFromPt(x, y);
02178 if (w == NULL) return;
02179
02180 if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
02181 ViewPort *vp = IsPtInWindowViewport(w, x, y);
02182
02183
02184 if (vp != NULL && (_game_mode == GM_MENU || IsGeneratingWorld())) return;
02185
02186 if (mousewheel != 0) {
02187
02188 w->OnMouseWheel(mousewheel);
02189
02190
02191 if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
02192 }
02193
02194 if (vp != NULL) {
02195 if (scrollwheel_scrolling) click = MC_RIGHT;
02196 switch (click) {
02197 case MC_DOUBLE_LEFT:
02198 case MC_LEFT:
02199 DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite);
02200 if (!HandleViewportClicked(vp, x, y) &&
02201 !(w->flags4 & WF_DISABLE_VP_SCROLL) &&
02202 _settings_client.gui.left_mouse_btn_scrolling) {
02203 _scrolling_viewport = true;
02204 _cursor.fix_at = false;
02205 }
02206 break;
02207
02208 case MC_RIGHT:
02209 if (!(w->flags4 & WF_DISABLE_VP_SCROLL)) {
02210 _scrolling_viewport = true;
02211 _cursor.fix_at = true;
02212
02213
02214 _cursor.h_wheel = 0;
02215 _cursor.v_wheel = 0;
02216 }
02217 break;
02218
02219 default:
02220 break;
02221 }
02222 } else {
02223 switch (click) {
02224 case MC_LEFT:
02225 case MC_DOUBLE_LEFT:
02226 DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
02227 break;
02228
02229 default:
02230 if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
02231
02232
02233
02234
02235 case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
02236
02237 case MC_HOVER: DispatchHoverEvent(w, x - w->left, y - w->top); break;
02238 }
02239 }
02240 }
02241
02245 void HandleMouseEvents()
02246 {
02247
02248
02249 assert(IsGeneratingWorld() || IsLocalCompany());
02250
02251 static int double_click_time = 0;
02252 static Point double_click_pos = {0, 0};
02253
02254
02255 MouseClick click = MC_NONE;
02256 if (_left_button_down && !_left_button_clicked) {
02257 click = MC_LEFT;
02258 if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
02259 double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
02260 double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
02261 click = MC_DOUBLE_LEFT;
02262 }
02263 double_click_time = _realtime_tick;
02264 double_click_pos = _cursor.pos;
02265 _left_button_clicked = true;
02266 _input_events_this_tick++;
02267 } else if (_right_button_clicked) {
02268 _right_button_clicked = false;
02269 click = MC_RIGHT;
02270 _input_events_this_tick++;
02271 }
02272
02273 int mousewheel = 0;
02274 if (_cursor.wheel) {
02275 mousewheel = _cursor.wheel;
02276 _cursor.wheel = 0;
02277 _input_events_this_tick++;
02278 }
02279
02280 static uint32 hover_time = 0;
02281 static Point hover_pos = {0, 0};
02282
02283 if (_settings_client.gui.hover_delay > 0) {
02284 if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
02285 hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
02286 hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
02287 hover_pos = _cursor.pos;
02288 hover_time = _realtime_tick;
02289 _mouse_hovering = false;
02290 } else {
02291 if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay * 1000) {
02292 click = MC_HOVER;
02293 _input_events_this_tick++;
02294 _mouse_hovering = true;
02295 }
02296 }
02297 }
02298
02299
02300 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && _newgrf_debug_sprite_picker.click_time != _realtime_tick) {
02301
02302 _newgrf_debug_sprite_picker.mode = SPM_NONE;
02303 InvalidateWindowData(WC_SPRITE_ALIGNER, 0, 1);
02304 }
02305
02306 if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
02307
02308 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
02309 _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
02310 _newgrf_debug_sprite_picker.click_time = _realtime_tick;
02311 _newgrf_debug_sprite_picker.sprites.Clear();
02312 _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
02313 MarkWholeScreenDirty();
02314 } else {
02315 MouseLoop(click, mousewheel);
02316 }
02317
02318
02319
02320 _cursor.delta.x = 0;
02321 _cursor.delta.y = 0;
02322 }
02323
02327 static void CheckSoftLimit()
02328 {
02329 if (_settings_client.gui.window_soft_limit == 0) return;
02330
02331 for (;;) {
02332 uint deletable_count = 0;
02333 Window *w, *last_deletable = NULL;
02334 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02335 if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags4 & WF_STICKY)) continue;
02336
02337 last_deletable = w;
02338 deletable_count++;
02339 }
02340
02341
02342 if (deletable_count <= _settings_client.gui.window_soft_limit) break;
02343
02344 assert(last_deletable != NULL);
02345 delete last_deletable;
02346 }
02347 }
02348
02352 void InputLoop()
02353 {
02354
02355
02356 assert(IsGeneratingWorld() || IsLocalCompany());
02357
02358 CheckSoftLimit();
02359 HandleKeyScrolling();
02360
02361
02362 for (Window *v = _z_front_window; v != NULL; ) {
02363 Window *w = v;
02364 v = v->z_back;
02365
02366 if (w->window_class != WC_INVALID) continue;
02367
02368
02369
02370
02371
02372 if (w->z_front == NULL) {
02373 _z_front_window = w->z_back;
02374 } else {
02375 w->z_front->z_back = w->z_back;
02376 }
02377 if (w->z_back == NULL) {
02378 _z_back_window = w->z_front;
02379 } else {
02380 w->z_back->z_front = w->z_front;
02381 }
02382 free(w);
02383 }
02384
02385 if (_scroller_click_timeout != 0) _scroller_click_timeout--;
02386 DecreaseWindowCounters();
02387
02388 if (_input_events_this_tick != 0) {
02389
02390 _input_events_this_tick = 0;
02391
02392 return;
02393 }
02394
02395
02396 HandleMouseEvents();
02397 HandleAutoscroll();
02398 }
02399
02403 void UpdateWindows()
02404 {
02405 Window *w;
02406
02407 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02408 w->ProcessScheduledInvalidations();
02409 }
02410
02411 static int we4_timer = 0;
02412 int t = we4_timer + 1;
02413
02414 if (t >= 100) {
02415 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02416 w->OnHundredthTick();
02417 }
02418 t = 0;
02419 }
02420 we4_timer = t;
02421
02422 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02423 if (w->flags4 & WF_WHITE_BORDER_MASK) {
02424 w->flags4 -= WF_WHITE_BORDER_ONE;
02425
02426 if (!(w->flags4 & WF_WHITE_BORDER_MASK)) w->SetDirty();
02427 }
02428 }
02429
02430 DrawDirtyBlocks();
02431
02432 FOR_ALL_WINDOWS_FROM_BACK(w) {
02433
02434 if (w->viewport != NULL && !w->IsShaded()) UpdateViewportPosition(w);
02435 }
02436 NetworkDrawChatMessage();
02437
02438 DrawMouseCursor();
02439 }
02440
02446 void SetWindowDirty(WindowClass cls, WindowNumber number)
02447 {
02448 const Window *w;
02449 FOR_ALL_WINDOWS_FROM_BACK(w) {
02450 if (w->window_class == cls && w->window_number == number) w->SetDirty();
02451 }
02452 }
02453
02460 void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
02461 {
02462 const Window *w;
02463 FOR_ALL_WINDOWS_FROM_BACK(w) {
02464 if (w->window_class == cls && w->window_number == number) {
02465 w->SetWidgetDirty(widget_index);
02466 }
02467 }
02468 }
02469
02474 void SetWindowClassesDirty(WindowClass cls)
02475 {
02476 Window *w;
02477 FOR_ALL_WINDOWS_FROM_BACK(w) {
02478 if (w->window_class == cls) w->SetDirty();
02479 }
02480 }
02481
02508 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
02509 {
02510 Window *w;
02511 FOR_ALL_WINDOWS_FROM_BACK(w) {
02512 if (w->window_class == cls && w->window_number == number) {
02513 w->InvalidateData(data, gui_scope);
02514 }
02515 }
02516 }
02517
02526 void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
02527 {
02528 Window *w;
02529
02530 FOR_ALL_WINDOWS_FROM_BACK(w) {
02531 if (w->window_class == cls) {
02532 w->InvalidateData(data, gui_scope);
02533 }
02534 }
02535 }
02536
02540 void CallWindowTickEvent()
02541 {
02542 Window *w;
02543 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02544 w->OnTick();
02545 }
02546 }
02547
02554 void DeleteNonVitalWindows()
02555 {
02556 Window *w;
02557
02558 restart_search:
02559
02560
02561
02562 FOR_ALL_WINDOWS_FROM_BACK(w) {
02563 if (w->window_class != WC_MAIN_WINDOW &&
02564 w->window_class != WC_SELECT_GAME &&
02565 w->window_class != WC_MAIN_TOOLBAR &&
02566 w->window_class != WC_STATUS_BAR &&
02567 w->window_class != WC_TOOLBAR_MENU &&
02568 w->window_class != WC_TOOLTIPS &&
02569 (w->flags4 & WF_STICKY) == 0) {
02570
02571 delete w;
02572 goto restart_search;
02573 }
02574 }
02575 }
02576
02584 void DeleteAllNonVitalWindows()
02585 {
02586 Window *w;
02587
02588
02589 DeleteNonVitalWindows();
02590
02591 restart_search:
02592
02593
02594
02595 FOR_ALL_WINDOWS_FROM_BACK(w) {
02596 if (w->flags4 & WF_STICKY) {
02597 delete w;
02598 goto restart_search;
02599 }
02600 }
02601 }
02602
02607 void DeleteConstructionWindows()
02608 {
02609 Window *w;
02610
02611 restart_search:
02612
02613
02614
02615 FOR_ALL_WINDOWS_FROM_BACK(w) {
02616 if (w->desc_flags & WDF_CONSTRUCTION) {
02617 delete w;
02618 goto restart_search;
02619 }
02620 }
02621
02622 FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
02623 }
02624
02626 void HideVitalWindows()
02627 {
02628 DeleteWindowById(WC_TOOLBAR_MENU, 0);
02629 DeleteWindowById(WC_MAIN_TOOLBAR, 0);
02630 DeleteWindowById(WC_STATUS_BAR, 0);
02631 }
02632
02634 void ReInitAllWindows()
02635 {
02636 NWidgetLeaf::InvalidateDimensionCache();
02637
02638 Window *w;
02639 FOR_ALL_WINDOWS_FROM_BACK(w) {
02640 w->ReInit();
02641 }
02642 #ifdef ENABLE_NETWORK
02643 void NetworkReInitChatBoxSize();
02644 NetworkReInitChatBoxSize();
02645 #endif
02646
02647
02648 RelocateAllWindows(_cur_resolution.width, _cur_resolution.height);
02649 MarkWholeScreenDirty();
02650 }
02651
02659 static int PositionWindow(Window *w, WindowClass clss, int setting)
02660 {
02661 if (w == NULL || w->window_class != clss) {
02662 w = FindWindowById(clss, 0);
02663 }
02664 if (w == NULL) return 0;
02665
02666 int old_left = w->left;
02667 switch (setting) {
02668 case 1: w->left = (_screen.width - w->width) / 2; break;
02669 case 2: w->left = _screen.width - w->width; break;
02670 default: w->left = 0; break;
02671 }
02672 if (w->viewport != NULL) w->viewport->left += w->left - old_left;
02673 SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height);
02674 return w->left;
02675 }
02676
02682 int PositionMainToolbar(Window *w)
02683 {
02684 DEBUG(misc, 5, "Repositioning Main Toolbar...");
02685 return PositionWindow(w, WC_MAIN_TOOLBAR, _settings_client.gui.toolbar_pos);
02686 }
02687
02693 int PositionStatusbar(Window *w)
02694 {
02695 DEBUG(misc, 5, "Repositioning statusbar...");
02696 return PositionWindow(w, WC_STATUS_BAR, _settings_client.gui.statusbar_pos);
02697 }
02698
02704 int PositionNewsMessage(Window *w)
02705 {
02706 DEBUG(misc, 5, "Repositioning news message...");
02707 return PositionWindow(w, WC_NEWS_WINDOW, _settings_client.gui.statusbar_pos);
02708 }
02709
02715 int PositionNetworkChatWindow(Window *w)
02716 {
02717 DEBUG(misc, 5, "Repositioning network chat window...");
02718 return PositionWindow(w, WC_SEND_NETWORK_MSG, _settings_client.gui.statusbar_pos);
02719 }
02720
02721
02727 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
02728 {
02729 Window *w;
02730 FOR_ALL_WINDOWS_FROM_BACK(w) {
02731 if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
02732 w->viewport->follow_vehicle = to_index;
02733 w->SetDirty();
02734 }
02735 }
02736 }
02737
02738
02744 void RelocateAllWindows(int neww, int newh)
02745 {
02746 Window *w;
02747
02748 FOR_ALL_WINDOWS_FROM_BACK(w) {
02749 int left, top;
02750
02751 if (w->window_class == WC_MAIN_WINDOW) {
02752 ViewPort *vp = w->viewport;
02753 vp->width = w->width = neww;
02754 vp->height = w->height = newh;
02755 vp->virtual_width = ScaleByZoom(neww, vp->zoom);
02756 vp->virtual_height = ScaleByZoom(newh, vp->zoom);
02757 continue;
02758 }
02759
02760
02761
02762 switch (w->window_class) {
02763 case WC_MAIN_TOOLBAR:
02764 ResizeWindow(w, min(neww, *_preferred_toolbar_size) - w->width, 0);
02765
02766 top = w->top;
02767 left = PositionMainToolbar(w);
02768 break;
02769
02770 case WC_NEWS_WINDOW:
02771 top = newh - w->height;
02772 left = PositionNewsMessage(w);
02773 break;
02774
02775 case WC_STATUS_BAR:
02776 ResizeWindow(w, min(neww, *_preferred_statusbar_size) - w->width, 0);
02777
02778 top = newh - w->height;
02779 left = PositionStatusbar(w);
02780 break;
02781
02782 case WC_SEND_NETWORK_MSG:
02783 ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
02784 top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
02785 left = PositionNetworkChatWindow(w);
02786 break;
02787
02788 case WC_CONSOLE:
02789 IConsoleResize(w);
02790 continue;
02791
02792 default: {
02793 if (w->flags4 & WF_CENTERED) {
02794 top = (newh - w->height) >> 1;
02795 left = (neww - w->width) >> 1;
02796 break;
02797 }
02798
02799 left = w->left;
02800 if (left + (w->width >> 1) >= neww) left = neww - w->width;
02801 if (left < 0) left = 0;
02802
02803 top = w->top;
02804 if (top + (w->height >> 1) >= newh) top = newh - w->height;
02805
02806 const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
02807 if (wt != NULL) {
02808 if (top < wt->height && wt->left < (w->left + w->width) && (wt->left + wt->width) > w->left) top = wt->height;
02809 if (top >= newh) top = newh - 1;
02810 } else {
02811 if (top < 0) top = 0;
02812 }
02813 break;
02814 }
02815 }
02816
02817 if (w->viewport != NULL) {
02818 w->viewport->left += left - w->left;
02819 w->viewport->top += top - w->top;
02820 }
02821
02822 w->left = left;
02823 w->top = top;
02824 }
02825 }
02826
02832 PickerWindowBase::~PickerWindowBase()
02833 {
02834 this->window_class = WC_INVALID;
02835 ResetObjectToPlace();
02836 }