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, *old_dpi;
00545 old_dpi = _cur_dpi;
00546
00547 _cur_dpi = &bk;
00548
00549 FOR_ALL_WINDOWS_FROM_BACK(w) {
00550 if (right > w->left &&
00551 bottom > w->top &&
00552 left < w->left + w->width &&
00553 top < w->top + w->height) {
00554
00555 DrawOverlappedWindow(w, left, top, right, bottom);
00556 }
00557 }
00558 _cur_dpi = old_dpi;
00559 }
00560
00565 void Window::SetDirty() const
00566 {
00567 SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
00568 }
00569
00576 void Window::ReInit(int rx, int ry)
00577 {
00578 this->SetDirty();
00579
00580
00581 int window_width = this->width;
00582 int window_height = this->height;
00583
00584 this->OnInit();
00585
00586 this->nested_root->SetupSmallestSize(this, false);
00587 this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
00588 this->width = this->nested_root->smallest_x;
00589 this->height = this->nested_root->smallest_y;
00590 this->resize.step_width = this->nested_root->resize_x;
00591 this->resize.step_height = this->nested_root->resize_y;
00592
00593
00594 window_width = max(window_width + rx, this->width);
00595 window_height = max(window_height + ry, this->height);
00596 int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
00597 int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
00598
00599
00600 if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
00601 if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
00602
00603 ResizeWindow(this, dx, dy);
00604
00605 }
00606
00612 void Window::SetShaded(bool make_shaded)
00613 {
00614 if (this->shade_select == NULL) return;
00615
00616 int desired = make_shaded ? SZSP_HORIZONTAL : 0;
00617 if (this->shade_select->shown_plane != desired) {
00618 if (make_shaded) {
00619 this->unshaded_size.width = this->width;
00620 this->unshaded_size.height = this->height;
00621 this->shade_select->SetDisplayedPlane(desired);
00622 this->ReInit(0, -this->height);
00623 } else {
00624 this->shade_select->SetDisplayedPlane(desired);
00625 int dx = ((int)this->unshaded_size.width > this->width) ? (int)this->unshaded_size.width - this->width : 0;
00626 int dy = ((int)this->unshaded_size.height > this->height) ? (int)this->unshaded_size.height - this->height : 0;
00627 this->ReInit(dx, dy);
00628 }
00629 }
00630 }
00631
00638 static Window *FindChildWindow(const Window *w, WindowClass wc)
00639 {
00640 Window *v;
00641 FOR_ALL_WINDOWS_FROM_BACK(v) {
00642 if ((wc == WC_INVALID || wc == v->window_class) && v->parent == w) return v;
00643 }
00644
00645 return NULL;
00646 }
00647
00652 void Window::DeleteChildWindows(WindowClass wc) const
00653 {
00654 Window *child = FindChildWindow(this, wc);
00655 while (child != NULL) {
00656 delete child;
00657 child = FindChildWindow(this, wc);
00658 }
00659 }
00660
00664 Window::~Window()
00665 {
00666 if (_thd.window_class == this->window_class &&
00667 _thd.window_number == this->window_number) {
00668 ResetObjectToPlace();
00669 }
00670
00671
00672 if (_mouseover_last_w == this) _mouseover_last_w = NULL;
00673
00674
00675 if (_last_scroll_window == this) _last_scroll_window = NULL;
00676
00677
00678 if (_focused_window == this) _focused_window = NULL;
00679
00680 this->DeleteChildWindows();
00681
00682 if (this->viewport != NULL) DeleteWindowViewport(this);
00683
00684 this->SetDirty();
00685
00686 free(this->nested_array);
00687 delete this->nested_root;
00688
00689 this->window_class = WC_INVALID;
00690 }
00691
00698 Window *FindWindowById(WindowClass cls, WindowNumber number)
00699 {
00700 Window *w;
00701 FOR_ALL_WINDOWS_FROM_BACK(w) {
00702 if (w->window_class == cls && w->window_number == number) return w;
00703 }
00704
00705 return NULL;
00706 }
00707
00714 Window *FindWindowByClass(WindowClass cls)
00715 {
00716 Window *w;
00717 FOR_ALL_WINDOWS_FROM_BACK(w) {
00718 if (w->window_class == cls) return w;
00719 }
00720
00721 return NULL;
00722 }
00723
00730 void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
00731 {
00732 Window *w = FindWindowById(cls, number);
00733 if (force || w == NULL ||
00734 (w->flags4 & WF_STICKY) == 0) {
00735 delete w;
00736 }
00737 }
00738
00743 void DeleteWindowByClass(WindowClass cls)
00744 {
00745 Window *w;
00746
00747 restart_search:
00748
00749
00750
00751 FOR_ALL_WINDOWS_FROM_BACK(w) {
00752 if (w->window_class == cls) {
00753 delete w;
00754 goto restart_search;
00755 }
00756 }
00757 }
00758
00765 void DeleteCompanyWindows(CompanyID id)
00766 {
00767 Window *w;
00768
00769 restart_search:
00770
00771
00772
00773 FOR_ALL_WINDOWS_FROM_BACK(w) {
00774 if (w->owner == id) {
00775 delete w;
00776 goto restart_search;
00777 }
00778 }
00779
00780
00781 DeleteWindowById(WC_BUY_COMPANY, id);
00782 }
00783
00791 void ChangeWindowOwner(Owner old_owner, Owner new_owner)
00792 {
00793 Window *w;
00794 FOR_ALL_WINDOWS_FROM_BACK(w) {
00795 if (w->owner != old_owner) continue;
00796
00797 switch (w->window_class) {
00798 case WC_COMPANY_COLOUR:
00799 case WC_FINANCES:
00800 case WC_STATION_LIST:
00801 case WC_TRAINS_LIST:
00802 case WC_ROADVEH_LIST:
00803 case WC_SHIPS_LIST:
00804 case WC_AIRCRAFT_LIST:
00805 case WC_BUY_COMPANY:
00806 case WC_COMPANY:
00807 continue;
00808
00809 default:
00810 w->owner = new_owner;
00811 break;
00812 }
00813 }
00814 }
00815
00816 static void BringWindowToFront(Window *w);
00817
00825 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number)
00826 {
00827 Window *w = FindWindowById(cls, number);
00828
00829 if (w != NULL) {
00830 if (w->IsShaded()) w->SetShaded(false);
00831
00832 w->flags4 |= WF_WHITE_BORDER_MASK;
00833 BringWindowToFront(w);
00834 w->SetDirty();
00835 }
00836
00837 return w;
00838 }
00839
00840 static inline bool IsVitalWindow(const Window *w)
00841 {
00842 switch (w->window_class) {
00843 case WC_MAIN_TOOLBAR:
00844 case WC_STATUS_BAR:
00845 case WC_NEWS_WINDOW:
00846 case WC_SEND_NETWORK_MSG:
00847 return true;
00848
00849 default:
00850 return false;
00851 }
00852 }
00853
00863 static void BringWindowToFront(Window *w)
00864 {
00865 Window *v = _z_front_window;
00866
00867
00868 for (; v != NULL && v != w && IsVitalWindow(v); v = v->z_back) { }
00869
00870 if (v == NULL || w == v) return;
00871
00872
00873 assert(w != _z_front_window);
00874
00875 if (w->z_back == NULL) {
00876 _z_back_window = w->z_front;
00877 } else {
00878 w->z_back->z_front = w->z_front;
00879 }
00880 w->z_front->z_back = w->z_back;
00881
00882 w->z_front = v->z_front;
00883 w->z_back = v;
00884
00885 if (v->z_front == NULL) {
00886 _z_front_window = w;
00887 } else {
00888 v->z_front->z_back = w;
00889 }
00890 v->z_front = w;
00891
00892 w->SetDirty();
00893 }
00894
00903 void Window::InitializeData(const WindowDesc *desc, WindowNumber window_number)
00904 {
00905
00906 this->window_class = desc->cls;
00907 this->flags4 |= WF_WHITE_BORDER_MASK;
00908 if (desc->default_pos == WDP_CENTER) this->flags4 |= WF_CENTERED;
00909 this->owner = INVALID_OWNER;
00910 this->nested_focus = NULL;
00911 this->window_number = window_number;
00912 this->desc_flags = desc->flags;
00913
00914 this->OnInit();
00915
00916 if (this->nested_array == NULL) {
00917 this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
00918 this->nested_root->SetupSmallestSize(this, true);
00919 } else {
00920 this->nested_root->SetupSmallestSize(this, false);
00921 }
00922
00923 this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
00924
00925
00926
00927 this->resize.step_width = this->nested_root->resize_x;
00928 this->resize.step_height = this->nested_root->resize_y;
00929
00930
00931
00932
00933 if (this->window_class != WC_OSK && (!EditBoxInGlobalFocus() || this->nested_root->GetWidgetOfType(WWT_EDITBOX) != NULL)) SetFocusedWindow(this);
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943 Window *w = _z_front_window;
00944 if (w != NULL && this->window_class != WC_SEND_NETWORK_MSG && this->window_class != WC_HIGHSCORE && this->window_class != WC_ENDSCREEN) {
00945 if (FindWindowById(WC_MAIN_TOOLBAR, 0) != NULL) w = w->z_back;
00946 if (FindWindowById(WC_STATUS_BAR, 0) != NULL) w = w->z_back;
00947 if (FindWindowById(WC_NEWS_WINDOW, 0) != NULL) w = w->z_back;
00948 if (FindWindowByClass(WC_SEND_NETWORK_MSG) != NULL) w = w->z_back;
00949
00950 if (w == NULL) {
00951 _z_back_window->z_front = this;
00952 this->z_back = _z_back_window;
00953 _z_back_window = this;
00954 } else {
00955 if (w->z_front == NULL) {
00956 _z_front_window = this;
00957 } else {
00958 this->z_front = w->z_front;
00959 w->z_front->z_back = this;
00960 }
00961
00962 this->z_back = w;
00963 w->z_front = this;
00964 }
00965 } else {
00966 this->z_back = _z_front_window;
00967 if (_z_front_window != NULL) {
00968 _z_front_window->z_front = this;
00969 } else {
00970 _z_back_window = this;
00971 }
00972 _z_front_window = this;
00973 }
00974 }
00975
00983 void Window::InitializePositionSize(int x, int y, int sm_width, int sm_height)
00984 {
00985 this->left = x;
00986 this->top = y;
00987 this->width = sm_width;
00988 this->height = sm_height;
00989 }
00990
01001 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
01002 {
01003 def_width = max(def_width, this->width);
01004 def_height = max(def_height, this->height);
01005
01006
01007
01008
01009
01010 if (this->width != def_width || this->height != def_height) {
01011
01012 int free_height = _screen.height;
01013 const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
01014 if (wt != NULL) free_height -= wt->height;
01015 wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
01016 if (wt != NULL) free_height -= wt->height;
01017
01018 int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
01019 int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
01020
01021
01022
01023
01024 if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
01025 if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
01026
01027 ResizeWindow(this, enlarge_x, enlarge_y);
01028
01029 } else {
01030
01031 this->OnResize();
01032 }
01033
01034 int nx = this->left;
01035 int ny = this->top;
01036
01037 if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
01038
01039 const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
01040 ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
01041 nx = max(nx, 0);
01042
01043 if (this->viewport != NULL) {
01044 this->viewport->left += nx - this->left;
01045 this->viewport->top += ny - this->top;
01046 }
01047 this->left = nx;
01048 this->top = ny;
01049
01050 this->SetDirty();
01051 }
01052
01064 static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos)
01065 {
01066 int right = width + left;
01067 int bottom = height + top;
01068
01069 const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
01070 if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height) return false;
01071
01072
01073 const Window *w;
01074 FOR_ALL_WINDOWS_FROM_BACK(w) {
01075 if (w->window_class == WC_MAIN_WINDOW) continue;
01076
01077 if (right > w->left &&
01078 w->left + w->width > left &&
01079 bottom > w->top &&
01080 w->top + w->height > top) {
01081 return false;
01082 }
01083 }
01084
01085 pos.x = left;
01086 pos.y = top;
01087 return true;
01088 }
01089
01101 static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos)
01102 {
01103
01104
01105
01106 if (left < -(width >> 2) || left > _screen.width - (width >> 1)) return false;
01107
01108 if (top < 22 || top > _screen.height - (height >> 2)) return false;
01109
01110
01111 const Window *w;
01112 FOR_ALL_WINDOWS_FROM_BACK(w) {
01113 if (w->window_class == WC_MAIN_WINDOW) continue;
01114
01115 if (left + width > w->left &&
01116 w->left + w->width > left &&
01117 top + height > w->top &&
01118 w->top + w->height > top) {
01119 return false;
01120 }
01121 }
01122
01123 pos.x = left;
01124 pos.y = top;
01125 return true;
01126 }
01127
01134 static Point GetAutoPlacePosition(int width, int height)
01135 {
01136 Point pt;
01137
01138
01139 const Window *main_toolbar = FindWindowByClass(WC_MAIN_TOOLBAR);
01140 if (IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->height + 2 : 2, width, height, pt)) return pt;
01141
01142
01143
01144
01145
01146 const Window *w;
01147 FOR_ALL_WINDOWS_FROM_BACK(w) {
01148 if (w->window_class == WC_MAIN_WINDOW) continue;
01149
01150 if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt;
01151 if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt;
01152 if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt;
01153 if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt;
01154 if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt;
01155 if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt;
01156 if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt;
01157 if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt;
01158 }
01159
01160
01161
01162
01163
01164 FOR_ALL_WINDOWS_FROM_BACK(w) {
01165 if (w->window_class == WC_MAIN_WINDOW) continue;
01166
01167 if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt;
01168 if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt;
01169 if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt;
01170 if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt;
01171 }
01172
01173
01174
01175
01176 int left = 0, top = 24;
01177
01178 restart:
01179 FOR_ALL_WINDOWS_FROM_BACK(w) {
01180 if (w->left == left && w->top == top) {
01181 left += 5;
01182 top += 5;
01183 goto restart;
01184 }
01185 }
01186
01187 pt.x = left;
01188 pt.y = top;
01189 return pt;
01190 }
01191
01198 Point GetToolbarAlignedWindowPosition(int window_width)
01199 {
01200 const Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
01201 assert(w != NULL);
01202 Point pt = { _current_text_dir == TD_RTL ? w->left : (w->left + w->width) - window_width, w->top + w->height };
01203 return pt;
01204 }
01205
01223 static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
01224 {
01225 Point pt;
01226 const Window *w;
01227
01228 int16 default_width = max(desc->default_width, sm_width);
01229 int16 default_height = max(desc->default_height, sm_height);
01230
01231 if (desc->parent_cls != 0 &&
01232 (w = FindWindowById(desc->parent_cls, window_number)) != NULL &&
01233 w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) {
01234
01235 pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10);
01236 if (pt.x > _screen.width + 10 - default_width) {
01237 pt.x = (_screen.width + 10 - default_width) - 20;
01238 }
01239 pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? w->height : 10);
01240 return pt;
01241 }
01242
01243 switch (desc->default_pos) {
01244 case WDP_ALIGN_TOOLBAR:
01245 return GetToolbarAlignedWindowPosition(default_width);
01246
01247 case WDP_AUTO:
01248 return GetAutoPlacePosition(default_width, default_height);
01249
01250 case WDP_CENTER:
01251 pt.x = (_screen.width - default_width) / 2;
01252 pt.y = (_screen.height - default_height) / 2;
01253 break;
01254
01255 case WDP_MANUAL:
01256 pt.x = 0;
01257 pt.y = 0;
01258 break;
01259
01260 default:
01261 NOT_REACHED();
01262 }
01263
01264 return pt;
01265 }
01266
01267 Point Window::OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
01268 {
01269 return LocalGetWindowPlacement(desc, sm_width, sm_height, window_number);
01270 }
01271
01280 void Window::CreateNestedTree(const WindowDesc *desc, bool fill_nested)
01281 {
01282 int biggest_index = -1;
01283 this->nested_root = MakeWindowNWidgetTree(desc->nwid_parts, desc->nwid_length, &biggest_index, &this->shade_select);
01284 this->nested_array_size = (uint)(biggest_index + 1);
01285
01286 if (fill_nested) {
01287 this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
01288 this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
01289 }
01290 }
01291
01297 void Window::FinishInitNested(const WindowDesc *desc, WindowNumber window_number)
01298 {
01299 this->InitializeData(desc, window_number);
01300 Point pt = this->OnInitialPosition(desc, this->nested_root->smallest_x, this->nested_root->smallest_y, window_number);
01301 this->InitializePositionSize(pt.x, pt.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
01302 this->FindWindowPlacementAndResize(desc->default_width, desc->default_height);
01303 }
01304
01310 void Window::InitNested(const WindowDesc *desc, WindowNumber window_number)
01311 {
01312 this->CreateNestedTree(desc, false);
01313 this->FinishInitNested(desc, window_number);
01314 }
01315
01317 Window::Window() : scrolling_scrollbar(-1)
01318 {
01319 }
01320
01328 Window *FindWindowFromPt(int x, int y)
01329 {
01330 Window *w;
01331 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01332 if (IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
01333 return w;
01334 }
01335 }
01336
01337 return NULL;
01338 }
01339
01343 void InitWindowSystem()
01344 {
01345 IConsoleClose();
01346
01347 _z_back_window = NULL;
01348 _z_front_window = NULL;
01349 _focused_window = NULL;
01350 _mouseover_last_w = NULL;
01351 _last_scroll_window = NULL;
01352 _scrolling_viewport = false;
01353 _mouse_hovering = false;
01354
01355 NWidgetLeaf::InvalidateDimensionCache();
01356 }
01357
01361 void UnInitWindowSystem()
01362 {
01363 Window *w;
01364 FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;
01365
01366 for (w = _z_front_window; w != NULL; ) {
01367 Window *to_del = w;
01368 w = w->z_back;
01369 free(to_del);
01370 }
01371
01372 _z_front_window = NULL;
01373 _z_back_window = NULL;
01374 }
01375
01379 void ResetWindowSystem()
01380 {
01381 UnInitWindowSystem();
01382 InitWindowSystem();
01383 _thd.Reset();
01384 }
01385
01386 static void DecreaseWindowCounters()
01387 {
01388 Window *w;
01389 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01390 if (_scroller_click_timeout == 0) {
01391
01392 for (uint i = 0; i < w->nested_array_size; i++) {
01393 NWidgetBase *nwid = w->nested_array[i];
01394 if (nwid != NULL && (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR)) {
01395 NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
01396 if (sb->disp_flags & (ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN)) {
01397 sb->disp_flags &= ~(ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN);
01398 sb->SetDirty(w);
01399 }
01400 }
01401 }
01402 }
01403 w->OnMouseLoop();
01404 }
01405
01406 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01407 if ((w->flags4 & WF_TIMEOUT_MASK) && !(--w->flags4 & WF_TIMEOUT_MASK)) {
01408 w->OnTimeout();
01409 if (w->desc_flags & WDF_UNCLICK_BUTTONS) w->RaiseButtons(true);
01410 }
01411 }
01412 }
01413
01414 static void HandlePlacePresize()
01415 {
01416 if (_special_mouse_mode != WSM_PRESIZE) return;
01417
01418 Window *w = _thd.GetCallbackWnd();
01419 if (w == NULL) return;
01420
01421 Point pt = GetTileBelowCursor();
01422 if (pt.x == -1) {
01423 _thd.selend.x = -1;
01424 return;
01425 }
01426
01427 w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
01428 }
01429
01434 static EventState HandleMouseDragDrop()
01435 {
01436 if (_special_mouse_mode != WSM_DRAGDROP) return ES_NOT_HANDLED;
01437
01438 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
01439
01440 Window *w = _thd.GetCallbackWnd();
01441 if (w != NULL) {
01442
01443 Point pt;
01444 pt.x = _cursor.pos.x - w->left;
01445 pt.y = _cursor.pos.y - w->top;
01446 if (_left_button_down) {
01447 w->OnMouseDrag(pt, GetWidgetFromPos(w, pt.x, pt.y));
01448 } else {
01449 w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
01450 }
01451 }
01452
01453 if (!_left_button_down) ResetObjectToPlace();
01454 return ES_HANDLED;
01455 }
01456
01458 static void HandleMouseOver()
01459 {
01460 Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
01461
01462
01463 if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
01464
01465 Point pt = { -1, -1 };
01466 _mouseover_last_w->OnMouseOver(pt, 0);
01467 }
01468
01469
01470 _mouseover_last_w = w;
01471
01472 if (w != NULL) {
01473
01474 Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
01475 const NWidgetCore *widget = w->nested_root->GetWidgetFromPos(pt.x, pt.y);
01476 if (widget != NULL) w->OnMouseOver(pt, widget->index);
01477 }
01478 }
01479
01481 static const int MIN_VISIBLE_TITLE_BAR = 13;
01482
01484 enum PreventHideDirection {
01485 PHD_UP,
01486 PHD_DOWN,
01487 };
01488
01499 static void PreventHiding(int *nx, int *ny, const Rect &rect, const Window *v, int px, PreventHideDirection dir)
01500 {
01501 if (v == NULL) return;
01502
01503 int v_bottom = v->top + v->height;
01504 int v_right = v->left + v->width;
01505 int safe_y = (dir == PHD_UP) ? (v->top - MIN_VISIBLE_TITLE_BAR - rect.top) : (v_bottom + MIN_VISIBLE_TITLE_BAR - rect.bottom);
01506
01507 if (*ny + rect.top <= v->top - MIN_VISIBLE_TITLE_BAR) return;
01508 if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR) return;
01509
01510
01511 if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) {
01512 if (v->left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
01513 return;
01514 }
01515 if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) {
01516 if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
01517 return;
01518 }
01519
01520
01521 if (px + rect.left < v->left && v->left >= MIN_VISIBLE_TITLE_BAR) {
01522 *nx = v->left - MIN_VISIBLE_TITLE_BAR - rect.left;
01523 } else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) {
01524 *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
01525 } else {
01526 *ny = safe_y;
01527 }
01528 }
01529
01537 static void EnsureVisibleCaption(Window *w, int nx, int ny)
01538 {
01539
01540 Rect caption_rect;
01541 const NWidgetBase *caption = w->nested_root->GetWidgetOfType(WWT_CAPTION);
01542 if (caption != NULL) {
01543 caption_rect.left = caption->pos_x;
01544 caption_rect.right = caption->pos_x + caption->current_x;
01545 caption_rect.top = caption->pos_y;
01546 caption_rect.bottom = caption->pos_y + caption->current_y;
01547
01548
01549 nx = Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
01550 ny = Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
01551
01552
01553 PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_MAIN_TOOLBAR, 0), w->left, PHD_DOWN);
01554 PreventHiding(&nx, &ny, caption_rect, FindWindowById(WC_STATUS_BAR, 0), w->left, PHD_UP);
01555
01556 if (w->viewport != NULL) {
01557 w->viewport->left += nx - w->left;
01558 w->viewport->top += ny - w->top;
01559 }
01560 }
01561 w->left = nx;
01562 w->top = ny;
01563 }
01564
01574 void ResizeWindow(Window *w, int delta_x, int delta_y)
01575 {
01576 if (delta_x != 0 || delta_y != 0) {
01577 w->SetDirty();
01578
01579 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);
01580 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);
01581 assert(w->nested_root->resize_x == 0 || new_xinc % w->nested_root->resize_x == 0);
01582 assert(w->nested_root->resize_y == 0 || new_yinc % w->nested_root->resize_y == 0);
01583
01584 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);
01585 w->width = w->nested_root->current_x;
01586 w->height = w->nested_root->current_y;
01587 }
01588
01589 EnsureVisibleCaption(w, w->left, w->top);
01590
01591
01592 w->OnResize();
01593 w->SetDirty();
01594 }
01595
01601 int GetMainViewTop()
01602 {
01603 Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
01604 return (w == NULL) ? 0 : w->top + w->height;
01605 }
01606
01612 int GetMainViewBottom()
01613 {
01614 Window *w = FindWindowById(WC_STATUS_BAR, 0);
01615 return (w == NULL) ? _screen.height : w->top;
01616 }
01617
01618 static bool _dragging_window;
01619
01624 static EventState HandleWindowDragging()
01625 {
01626
01627 if (!_dragging_window) return ES_NOT_HANDLED;
01628
01629
01630 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
01631
01632
01633 Window *w;
01634 FOR_ALL_WINDOWS_FROM_BACK(w) {
01635 if (w->flags4 & WF_DRAGGING) {
01636
01637 if (!_left_button_down) {
01638 w->flags4 &= ~WF_DRAGGING;
01639 break;
01640 }
01641
01642 w->SetDirty();
01643
01644 int x = _cursor.pos.x + _drag_delta.x;
01645 int y = _cursor.pos.y + _drag_delta.y;
01646 int nx = x;
01647 int ny = y;
01648
01649 if (_settings_client.gui.window_snap_radius != 0) {
01650 const Window *v;
01651
01652 int hsnap = _settings_client.gui.window_snap_radius;
01653 int vsnap = _settings_client.gui.window_snap_radius;
01654 int delta;
01655
01656 FOR_ALL_WINDOWS_FROM_BACK(v) {
01657 if (v == w) continue;
01658
01659 if (y + w->height > v->top && y < v->top + v->height) {
01660
01661 delta = abs(v->left + v->width - x);
01662 if (delta <= hsnap) {
01663 nx = v->left + v->width;
01664 hsnap = delta;
01665 }
01666
01667
01668 delta = abs(v->left - x - w->width);
01669 if (delta <= hsnap) {
01670 nx = v->left - w->width;
01671 hsnap = delta;
01672 }
01673 }
01674
01675 if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
01676
01677 delta = abs(v->left - x);
01678 if (delta <= hsnap) {
01679 nx = v->left;
01680 hsnap = delta;
01681 }
01682
01683
01684 delta = abs(v->left + v->width - x - w->width);
01685 if (delta <= hsnap) {
01686 nx = v->left + v->width - w->width;
01687 hsnap = delta;
01688 }
01689 }
01690
01691 if (x + w->width > v->left && x < v->left + v->width) {
01692
01693 delta = abs(v->top + v->height - y);
01694 if (delta <= vsnap) {
01695 ny = v->top + v->height;
01696 vsnap = delta;
01697 }
01698
01699
01700 delta = abs(v->top - y - w->height);
01701 if (delta <= vsnap) {
01702 ny = v->top - w->height;
01703 vsnap = delta;
01704 }
01705 }
01706
01707 if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
01708
01709 delta = abs(v->top - y);
01710 if (delta <= vsnap) {
01711 ny = v->top;
01712 vsnap = delta;
01713 }
01714
01715
01716 delta = abs(v->top + v->height - y - w->height);
01717 if (delta <= vsnap) {
01718 ny = v->top + v->height - w->height;
01719 vsnap = delta;
01720 }
01721 }
01722 }
01723 }
01724
01725 EnsureVisibleCaption(w, nx, ny);
01726
01727 w->SetDirty();
01728 return ES_HANDLED;
01729 } else if (w->flags4 & WF_SIZING) {
01730
01731 if (!_left_button_down) {
01732 w->flags4 &= ~WF_SIZING;
01733 w->SetDirty();
01734 break;
01735 }
01736
01737
01738
01739
01740 int x, y = _cursor.pos.y - _drag_delta.y;
01741 if (w->flags4 & WF_SIZING_LEFT) {
01742 x = _drag_delta.x - _cursor.pos.x;
01743 } else {
01744 x = _cursor.pos.x - _drag_delta.x;
01745 }
01746
01747
01748 if (w->resize.step_width == 0) x = 0;
01749 if (w->resize.step_height == 0) y = 0;
01750
01751
01752 if (w->top + w->height + y > _screen.height) {
01753 y = _screen.height - w->height - w->top;
01754 }
01755
01756
01757
01758
01759 if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
01760 if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
01761
01762
01763 if ((int)w->width + x < (int)w->nested_root->smallest_x) {
01764 x = w->nested_root->smallest_x - w->width;
01765 }
01766 if ((int)w->height + y < (int)w->nested_root->smallest_y) {
01767 y = w->nested_root->smallest_y - w->height;
01768 }
01769
01770
01771 if (x == 0 && y == 0) return ES_HANDLED;
01772
01773
01774 _drag_delta.y += y;
01775 if ((w->flags4 & WF_SIZING_LEFT) && x != 0) {
01776 _drag_delta.x -= x;
01777 w->SetDirty();
01778 w->left -= x;
01779
01780 } else {
01781 _drag_delta.x += x;
01782 }
01783
01784
01785 ResizeWindow(w, x, y);
01786 return ES_HANDLED;
01787 }
01788 }
01789
01790 _dragging_window = false;
01791 return ES_HANDLED;
01792 }
01793
01798 static void StartWindowDrag(Window *w)
01799 {
01800 w->flags4 |= WF_DRAGGING;
01801 w->flags4 &= ~WF_CENTERED;
01802 _dragging_window = true;
01803
01804 _drag_delta.x = w->left - _cursor.pos.x;
01805 _drag_delta.y = w->top - _cursor.pos.y;
01806
01807 BringWindowToFront(w);
01808 DeleteWindowById(WC_DROPDOWN_MENU, 0);
01809 }
01810
01816 static void StartWindowSizing(Window *w, bool to_left)
01817 {
01818 w->flags4 |= to_left ? WF_SIZING_LEFT : WF_SIZING_RIGHT;
01819 w->flags4 &= ~WF_CENTERED;
01820 _dragging_window = true;
01821
01822 _drag_delta.x = _cursor.pos.x;
01823 _drag_delta.y = _cursor.pos.y;
01824
01825 BringWindowToFront(w);
01826 DeleteWindowById(WC_DROPDOWN_MENU, 0);
01827 }
01828
01833 static EventState HandleScrollbarScrolling()
01834 {
01835 Window *w;
01836 FOR_ALL_WINDOWS_FROM_BACK(w) {
01837 if (w->scrolling_scrollbar >= 0) {
01838
01839 if (!_left_button_down) {
01840 w->scrolling_scrollbar = -1;
01841 w->SetDirty();
01842 return ES_HANDLED;
01843 }
01844
01845 int i;
01846 NWidgetScrollbar *sb = w->GetWidget<NWidgetScrollbar>(w->scrolling_scrollbar);
01847 bool rtl = false;
01848
01849 if (sb->type == NWID_HSCROLLBAR) {
01850 i = _cursor.pos.x - _cursorpos_drag_start.x;
01851 rtl = _current_text_dir == TD_RTL;
01852 } else {
01853 i = _cursor.pos.y - _cursorpos_drag_start.y;
01854 }
01855
01856 if (sb->disp_flags & ND_SCROLLBAR_BTN) {
01857 if (_scroller_click_timeout == 1) {
01858 _scroller_click_timeout = 3;
01859 sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
01860 w->SetDirty();
01861 }
01862 return ES_HANDLED;
01863 }
01864
01865
01866 int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
01867 if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos);
01868 if (pos != sb->GetPosition()) {
01869 sb->SetPosition(pos);
01870 w->SetDirty();
01871 }
01872 return ES_HANDLED;
01873 }
01874 }
01875
01876 return ES_NOT_HANDLED;
01877 }
01878
01883 static EventState HandleViewportScroll()
01884 {
01885 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
01886
01887 if (!_scrolling_viewport) return ES_NOT_HANDLED;
01888
01889
01890
01891
01892 if (_last_scroll_window == NULL) _last_scroll_window = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
01893
01894 if (_last_scroll_window == NULL || !(_right_button_down || scrollwheel_scrolling || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down))) {
01895 _cursor.fix_at = false;
01896 _scrolling_viewport = false;
01897 _last_scroll_window = NULL;
01898 return ES_NOT_HANDLED;
01899 }
01900
01901 if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
01902
01903 const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
01904 ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true);
01905 return ES_NOT_HANDLED;
01906 }
01907
01908 Point delta;
01909 if (_settings_client.gui.reverse_scroll || (_settings_client.gui.left_mouse_btn_scrolling && _left_button_down)) {
01910 delta.x = -_cursor.delta.x;
01911 delta.y = -_cursor.delta.y;
01912 } else {
01913 delta.x = _cursor.delta.x;
01914 delta.y = _cursor.delta.y;
01915 }
01916
01917 if (scrollwheel_scrolling) {
01918
01919 delta.x = _cursor.h_wheel;
01920 delta.y = _cursor.v_wheel;
01921 _cursor.v_wheel = 0;
01922 _cursor.h_wheel = 0;
01923 }
01924
01925
01926 if (delta.x != 0 || delta.y != 0) _last_scroll_window->OnScroll(delta);
01927
01928 _cursor.delta.x = 0;
01929 _cursor.delta.y = 0;
01930 return ES_HANDLED;
01931 }
01932
01943 static bool MaybeBringWindowToFront(Window *w)
01944 {
01945 bool bring_to_front = false;
01946
01947 if (w->window_class == WC_MAIN_WINDOW ||
01948 IsVitalWindow(w) ||
01949 w->window_class == WC_TOOLTIPS ||
01950 w->window_class == WC_DROPDOWN_MENU) {
01951 return true;
01952 }
01953
01954
01955 int w_width = w->width;
01956 int w_height = w->height;
01957 if (w->IsShaded()) {
01958 w_width = w->unshaded_size.width;
01959 w_height = w->unshaded_size.height;
01960 }
01961
01962 Window *u;
01963 FOR_ALL_WINDOWS_FROM_BACK_FROM(u, w->z_front) {
01964
01965 if (u->parent == w && (u->desc_flags & WDF_MODAL)) {
01966 u->flags4 |= WF_WHITE_BORDER_MASK;
01967 u->SetDirty();
01968 return false;
01969 }
01970
01971 if (u->window_class == WC_MAIN_WINDOW ||
01972 IsVitalWindow(u) ||
01973 u->window_class == WC_TOOLTIPS ||
01974 u->window_class == WC_DROPDOWN_MENU) {
01975 continue;
01976 }
01977
01978
01979 if (w->left + w_width <= u->left ||
01980 u->left + u->width <= w->left ||
01981 w->top + w_height <= u->top ||
01982 u->top + u->height <= w->top) {
01983 continue;
01984 }
01985
01986 bring_to_front = true;
01987 }
01988
01989 if (bring_to_front) BringWindowToFront(w);
01990 return true;
01991 }
01992
01997 void HandleKeypress(uint32 raw_key)
01998 {
01999
02000
02001 assert(IsGeneratingWorld() || IsLocalCompany());
02002
02003
02004 uint16 key = GB(raw_key, 0, 16);
02005 uint16 keycode = GB(raw_key, 16, 16);
02006
02007
02008
02009
02010
02011
02012
02013
02014 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
02015
02016
02017
02018
02019 if (key == 0 && keycode == 0) return;
02020
02021
02022 if (EditBoxInGlobalFocus()) {
02023
02024 if (_focused_window->OnKeyPress(key, keycode) == ES_HANDLED) return;
02025 }
02026
02027
02028 Window *w;
02029 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02030 if (w->window_class == WC_MAIN_TOOLBAR) continue;
02031 if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
02032 }
02033
02034 w = FindWindowById(WC_MAIN_TOOLBAR, 0);
02035
02036 if (w != NULL && w->OnKeyPress(key, keycode) == ES_HANDLED) return;
02037
02038 HandleGlobalHotkeys(key, keycode);
02039 }
02040
02044 void HandleCtrlChanged()
02045 {
02046
02047 Window *w;
02048 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02049 if (w->OnCTRLStateChange() == ES_HANDLED) return;
02050 }
02051 }
02052
02059 static int _input_events_this_tick = 0;
02060
02065 static void HandleAutoscroll()
02066 {
02067 if (_settings_client.gui.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) {
02068 int x = _cursor.pos.x;
02069 int y = _cursor.pos.y;
02070 Window *w = FindWindowFromPt(x, y);
02071 if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return;
02072 ViewPort *vp = IsPtInWindowViewport(w, x, y);
02073 if (vp != NULL) {
02074 x -= vp->left;
02075 y -= vp->top;
02076
02077
02078 #define scrollspeed 3
02079 if (x - 15 < 0) {
02080 w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
02081 } else if (15 - (vp->width - x) > 0) {
02082 w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
02083 }
02084 if (y - 15 < 0) {
02085 w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
02086 } else if (15 - (vp->height - y) > 0) {
02087 w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
02088 }
02089 #undef scrollspeed
02090 }
02091 }
02092 }
02093
02094 enum MouseClick {
02095 MC_NONE = 0,
02096 MC_LEFT,
02097 MC_RIGHT,
02098 MC_DOUBLE_LEFT,
02099 MC_HOVER,
02100
02101 MAX_OFFSET_DOUBLE_CLICK = 5,
02102 TIME_BETWEEN_DOUBLE_CLICK = 500,
02103 MAX_OFFSET_HOVER = 5,
02104 };
02105 extern EventState VpHandlePlaceSizingDrag();
02106
02107 static void ScrollMainViewport(int x, int y)
02108 {
02109 if (_game_mode != GM_MENU) {
02110 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
02111 assert(w);
02112
02113 w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
02114 w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
02115 }
02116 }
02117
02127 static const int8 scrollamt[16][2] = {
02128 { 0, 0},
02129 {-2, 0},
02130 { 0, -2},
02131 {-2, -1},
02132 { 2, 0},
02133 { 0, 0},
02134 { 2, -1},
02135 { 0, -2},
02136 { 0, 2},
02137 {-2, 1},
02138 { 0, 0},
02139 {-2, 0},
02140 { 2, 1},
02141 { 0, 2},
02142 { 2, 0},
02143 { 0, 0},
02144 };
02145
02146 static void HandleKeyScrolling()
02147 {
02148
02149
02150
02151
02152 if (_dirkeys && !EditBoxInGlobalFocus()) {
02153 int factor = _shift_pressed ? 50 : 10;
02154 ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
02155 }
02156 }
02157
02158 static void MouseLoop(MouseClick click, int mousewheel)
02159 {
02160
02161
02162 assert(IsGeneratingWorld() || IsLocalCompany());
02163
02164 HandlePlacePresize();
02165 UpdateTileSelection();
02166
02167 if (VpHandlePlaceSizingDrag() == ES_HANDLED) return;
02168 if (HandleMouseDragDrop() == ES_HANDLED) return;
02169 if (HandleWindowDragging() == ES_HANDLED) return;
02170 if (HandleScrollbarScrolling() == ES_HANDLED) return;
02171 if (HandleViewportScroll() == ES_HANDLED) return;
02172
02173 HandleMouseOver();
02174
02175 bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
02176 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
02177
02178 int x = _cursor.pos.x;
02179 int y = _cursor.pos.y;
02180 Window *w = FindWindowFromPt(x, y);
02181 if (w == NULL) return;
02182
02183 if (click != MC_HOVER && !MaybeBringWindowToFront(w)) return;
02184 ViewPort *vp = IsPtInWindowViewport(w, x, y);
02185
02186
02187 if (vp != NULL && (_game_mode == GM_MENU || IsGeneratingWorld())) return;
02188
02189 if (mousewheel != 0) {
02190 if (_settings_client.gui.scrollwheel_scrolling == 0) {
02191
02192 w->OnMouseWheel(mousewheel);
02193 }
02194
02195
02196 if (vp == NULL) DispatchMouseWheelEvent(w, w->nested_root->GetWidgetFromPos(x - w->left, y - w->top), mousewheel);
02197 }
02198
02199 if (vp != NULL) {
02200 if (scrollwheel_scrolling) click = MC_RIGHT;
02201 switch (click) {
02202 case MC_DOUBLE_LEFT:
02203 case MC_LEFT:
02204 DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite);
02205 if (!HandleViewportClicked(vp, x, y) &&
02206 !(w->flags4 & WF_DISABLE_VP_SCROLL) &&
02207 _settings_client.gui.left_mouse_btn_scrolling) {
02208 _scrolling_viewport = true;
02209 _cursor.fix_at = false;
02210 }
02211 break;
02212
02213 case MC_RIGHT:
02214 if (!(w->flags4 & WF_DISABLE_VP_SCROLL)) {
02215 _scrolling_viewport = true;
02216 _cursor.fix_at = true;
02217
02218
02219 _cursor.h_wheel = 0;
02220 _cursor.v_wheel = 0;
02221 }
02222 break;
02223
02224 default:
02225 break;
02226 }
02227 } else {
02228 switch (click) {
02229 case MC_LEFT:
02230 case MC_DOUBLE_LEFT:
02231 DispatchLeftClickEvent(w, x - w->left, y - w->top, click == MC_DOUBLE_LEFT ? 2 : 1);
02232 break;
02233
02234 default:
02235 if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
02236
02237
02238
02239
02240 case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
02241
02242 case MC_HOVER: DispatchHoverEvent(w, x - w->left, y - w->top); break;
02243 }
02244 }
02245 }
02246
02250 void HandleMouseEvents()
02251 {
02252
02253
02254 assert(IsGeneratingWorld() || IsLocalCompany());
02255
02256 static int double_click_time = 0;
02257 static Point double_click_pos = {0, 0};
02258
02259
02260 MouseClick click = MC_NONE;
02261 if (_left_button_down && !_left_button_clicked) {
02262 click = MC_LEFT;
02263 if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
02264 double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
02265 double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
02266 click = MC_DOUBLE_LEFT;
02267 }
02268 double_click_time = _realtime_tick;
02269 double_click_pos = _cursor.pos;
02270 _left_button_clicked = true;
02271 _input_events_this_tick++;
02272 } else if (_right_button_clicked) {
02273 _right_button_clicked = false;
02274 click = MC_RIGHT;
02275 _input_events_this_tick++;
02276 }
02277
02278 int mousewheel = 0;
02279 if (_cursor.wheel) {
02280 mousewheel = _cursor.wheel;
02281 _cursor.wheel = 0;
02282 _input_events_this_tick++;
02283 }
02284
02285 static uint32 hover_time = 0;
02286 static Point hover_pos = {0, 0};
02287
02288 if (_settings_client.gui.hover_delay > 0) {
02289 if (!_cursor.in_window || click != MC_NONE || mousewheel != 0 || _left_button_down || _right_button_down ||
02290 hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
02291 hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
02292 hover_pos = _cursor.pos;
02293 hover_time = _realtime_tick;
02294 _mouse_hovering = false;
02295 } else {
02296 if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay * 1000) {
02297 click = MC_HOVER;
02298 _input_events_this_tick++;
02299 _mouse_hovering = true;
02300 }
02301 }
02302 }
02303
02304
02305 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && _newgrf_debug_sprite_picker.click_time != _realtime_tick) {
02306
02307 _newgrf_debug_sprite_picker.mode = SPM_NONE;
02308 InvalidateWindowData(WC_SPRITE_ALIGNER, 0, 1);
02309 }
02310
02311 if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
02312
02313 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
02314 _newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
02315 _newgrf_debug_sprite_picker.click_time = _realtime_tick;
02316 _newgrf_debug_sprite_picker.sprites.Clear();
02317 _newgrf_debug_sprite_picker.mode = SPM_REDRAW;
02318 MarkWholeScreenDirty();
02319 } else {
02320 MouseLoop(click, mousewheel);
02321 }
02322
02323
02324
02325 _cursor.delta.x = 0;
02326 _cursor.delta.y = 0;
02327 }
02328
02332 static void CheckSoftLimit()
02333 {
02334 if (_settings_client.gui.window_soft_limit == 0) return;
02335
02336 for (;;) {
02337 uint deletable_count = 0;
02338 Window *w, *last_deletable = NULL;
02339 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02340 if (w->window_class == WC_MAIN_WINDOW || IsVitalWindow(w) || (w->flags4 & WF_STICKY)) continue;
02341
02342 last_deletable = w;
02343 deletable_count++;
02344 }
02345
02346
02347 if (deletable_count <= _settings_client.gui.window_soft_limit) break;
02348
02349 assert(last_deletable != NULL);
02350 delete last_deletable;
02351 }
02352 }
02353
02357 void InputLoop()
02358 {
02359
02360
02361 assert(IsGeneratingWorld() || IsLocalCompany());
02362
02363 CheckSoftLimit();
02364 HandleKeyScrolling();
02365
02366
02367 for (Window *v = _z_front_window; v != NULL; ) {
02368 Window *w = v;
02369 v = v->z_back;
02370
02371 if (w->window_class != WC_INVALID) continue;
02372
02373
02374
02375
02376
02377 if (w->z_front == NULL) {
02378 _z_front_window = w->z_back;
02379 } else {
02380 w->z_front->z_back = w->z_back;
02381 }
02382 if (w->z_back == NULL) {
02383 _z_back_window = w->z_front;
02384 } else {
02385 w->z_back->z_front = w->z_front;
02386 }
02387 free(w);
02388 }
02389
02390 if (_scroller_click_timeout != 0) _scroller_click_timeout--;
02391 DecreaseWindowCounters();
02392
02393 if (_input_events_this_tick != 0) {
02394
02395 _input_events_this_tick = 0;
02396
02397 return;
02398 }
02399
02400
02401 HandleMouseEvents();
02402 HandleAutoscroll();
02403 }
02404
02408 void UpdateWindows()
02409 {
02410 Window *w;
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
02488 void InvalidateWindowData(WindowClass cls, WindowNumber number, int data)
02489 {
02490 Window *w;
02491 FOR_ALL_WINDOWS_FROM_BACK(w) {
02492 if (w->window_class == cls && w->window_number == number) w->InvalidateData(data);
02493 }
02494 }
02495
02501 void InvalidateWindowClassesData(WindowClass cls, int data)
02502 {
02503 Window *w;
02504
02505 FOR_ALL_WINDOWS_FROM_BACK(w) {
02506 if (w->window_class == cls) w->InvalidateData(data);
02507 }
02508 }
02509
02513 void CallWindowTickEvent()
02514 {
02515 Window *w;
02516 FOR_ALL_WINDOWS_FROM_FRONT(w) {
02517 w->OnTick();
02518 }
02519 }
02520
02527 void DeleteNonVitalWindows()
02528 {
02529 Window *w;
02530
02531 restart_search:
02532
02533
02534
02535 FOR_ALL_WINDOWS_FROM_BACK(w) {
02536 if (w->window_class != WC_MAIN_WINDOW &&
02537 w->window_class != WC_SELECT_GAME &&
02538 w->window_class != WC_MAIN_TOOLBAR &&
02539 w->window_class != WC_STATUS_BAR &&
02540 w->window_class != WC_TOOLBAR_MENU &&
02541 w->window_class != WC_TOOLTIPS &&
02542 (w->flags4 & WF_STICKY) == 0) {
02543
02544 delete w;
02545 goto restart_search;
02546 }
02547 }
02548 }
02549
02557 void DeleteAllNonVitalWindows()
02558 {
02559 Window *w;
02560
02561
02562 DeleteNonVitalWindows();
02563
02564 restart_search:
02565
02566
02567
02568 FOR_ALL_WINDOWS_FROM_BACK(w) {
02569 if (w->flags4 & WF_STICKY) {
02570 delete w;
02571 goto restart_search;
02572 }
02573 }
02574 }
02575
02580 void DeleteConstructionWindows()
02581 {
02582 Window *w;
02583
02584 restart_search:
02585
02586
02587
02588 FOR_ALL_WINDOWS_FROM_BACK(w) {
02589 if (w->desc_flags & WDF_CONSTRUCTION) {
02590 delete w;
02591 goto restart_search;
02592 }
02593 }
02594
02595 FOR_ALL_WINDOWS_FROM_BACK(w) w->SetDirty();
02596 }
02597
02599 void HideVitalWindows()
02600 {
02601 DeleteWindowById(WC_TOOLBAR_MENU, 0);
02602 DeleteWindowById(WC_MAIN_TOOLBAR, 0);
02603 DeleteWindowById(WC_STATUS_BAR, 0);
02604 }
02605
02607 void ReInitAllWindows()
02608 {
02609 NWidgetLeaf::InvalidateDimensionCache();
02610
02611 Window *w;
02612 FOR_ALL_WINDOWS_FROM_BACK(w) {
02613 w->ReInit();
02614 }
02615 #ifdef ENABLE_NETWORK
02616 void NetworkReInitChatBoxSize();
02617 NetworkReInitChatBoxSize();
02618 #endif
02619
02620
02621 RelocateAllWindows(_cur_resolution.width, _cur_resolution.height);
02622 MarkWholeScreenDirty();
02623 }
02624
02632 static int PositionWindow(Window *w, WindowClass clss, int setting)
02633 {
02634 if (w == NULL || w->window_class != clss) {
02635 w = FindWindowById(clss, 0);
02636 }
02637 if (w == NULL) return 0;
02638
02639 int old_left = w->left;
02640 switch (setting) {
02641 case 1: w->left = (_screen.width - w->width) / 2; break;
02642 case 2: w->left = _screen.width - w->width; break;
02643 default: w->left = 0; break;
02644 }
02645 if (w->viewport != NULL) w->viewport->left += w->left - old_left;
02646 SetDirtyBlocks(0, w->top, _screen.width, w->top + w->height);
02647 return w->left;
02648 }
02649
02655 int PositionMainToolbar(Window *w)
02656 {
02657 DEBUG(misc, 5, "Repositioning Main Toolbar...");
02658 return PositionWindow(w, WC_MAIN_TOOLBAR, _settings_client.gui.toolbar_pos);
02659 }
02660
02666 int PositionStatusbar(Window *w)
02667 {
02668 DEBUG(misc, 5, "Repositioning statusbar...");
02669 return PositionWindow(w, WC_STATUS_BAR, _settings_client.gui.statusbar_pos);
02670 }
02671
02677 int PositionNewsMessage(Window *w)
02678 {
02679 DEBUG(misc, 5, "Repositioning news message...");
02680 return PositionWindow(w, WC_NEWS_WINDOW, _settings_client.gui.statusbar_pos);
02681 }
02682
02683
02689 void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
02690 {
02691 Window *w;
02692 FOR_ALL_WINDOWS_FROM_BACK(w) {
02693 if (w->viewport != NULL && w->viewport->follow_vehicle == from_index) {
02694 w->viewport->follow_vehicle = to_index;
02695 w->SetDirty();
02696 }
02697 }
02698 }
02699
02700
02706 void RelocateAllWindows(int neww, int newh)
02707 {
02708 Window *w;
02709
02710 FOR_ALL_WINDOWS_FROM_BACK(w) {
02711 int left, top;
02712
02713 if (w->window_class == WC_MAIN_WINDOW) {
02714 ViewPort *vp = w->viewport;
02715 vp->width = w->width = neww;
02716 vp->height = w->height = newh;
02717 vp->virtual_width = ScaleByZoom(neww, vp->zoom);
02718 vp->virtual_height = ScaleByZoom(newh, vp->zoom);
02719 continue;
02720 }
02721
02722
02723
02724 switch (w->window_class) {
02725 case WC_MAIN_TOOLBAR:
02726 ResizeWindow(w, min(neww, *_preferred_toolbar_size) - w->width, 0);
02727
02728 top = w->top;
02729 left = PositionMainToolbar(w);
02730 break;
02731
02732 case WC_NEWS_WINDOW:
02733 top = newh - w->height;
02734 left = PositionNewsMessage(w);
02735 break;
02736
02737 case WC_STATUS_BAR:
02738 ResizeWindow(w, min(neww, *_preferred_statusbar_size) - w->width, 0);
02739
02740 top = newh - w->height;
02741 left = PositionStatusbar(w);
02742 break;
02743
02744 case WC_SEND_NETWORK_MSG:
02745 ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
02746 top = newh - w->height - FindWindowById(WC_STATUS_BAR, 0)->height;
02747 left = (neww - w->width) >> 1;
02748 break;
02749
02750 case WC_CONSOLE:
02751 IConsoleResize(w);
02752 continue;
02753
02754 default: {
02755 if (w->flags4 & WF_CENTERED) {
02756 top = (newh - w->height) >> 1;
02757 left = (neww - w->width) >> 1;
02758 break;
02759 }
02760
02761 left = w->left;
02762 if (left + (w->width >> 1) >= neww) left = neww - w->width;
02763 if (left < 0) left = 0;
02764
02765 top = w->top;
02766 if (top + (w->height >> 1) >= newh) top = newh - w->height;
02767
02768 const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
02769 if (wt != NULL) {
02770 if (top < wt->height && wt->left < (w->left + w->width) && (wt->left + wt->width) > w->left) top = wt->height;
02771 if (top >= newh) top = newh - 1;
02772 } else {
02773 if (top < 0) top = 0;
02774 }
02775 break;
02776 }
02777 }
02778
02779 if (w->viewport != NULL) {
02780 w->viewport->left += left - w->left;
02781 w->viewport->top += top - w->top;
02782 }
02783
02784 w->left = left;
02785 w->top = top;
02786 }
02787 }
02788
02794 PickerWindowBase::~PickerWindowBase()
02795 {
02796 this->window_class = WC_INVALID;
02797 ResetObjectToPlace();
02798 }