69 Point _cursorpos_drag_start;
71 int _scrollbar_start_pos;
73 byte _scroller_click_timeout = 0;
95 parent_cls(parent_class),
98 nwid_parts(nwid_parts),
99 nwid_length(nwid_length),
104 default_width_trad(def_width_trad),
105 default_height_trad(def_height_trad)
108 *_window_descs->
Append() =
this;
111 WindowDesc::~WindowDesc()
113 _window_descs->
Erase(_window_descs->
Find(
this));
144 if ((*it)->ini_key == NULL)
continue;
155 if ((*a)->ini_key != NULL && (*b)->ini_key != NULL)
return strcmp((*a)->ini_key, (*b)->ini_key);
156 return ((*b)->ini_key != NULL ? 1 : 0) - ((*a)->ini_key != NULL ? 1 : 0);
170 if ((*it)->ini_key == NULL)
continue;
201 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
202 if (line_height < 0) line_height = wid->
resize_y;
203 if (clickpos < (
int)wid->
pos_y + padding)
return INT_MAX;
204 return (clickpos - (
int)wid->
pos_y - padding) / line_height;
213 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
214 if (nwid == NULL)
continue;
216 if (nwid->IsHighlighted()) {
217 nwid->SetHighlighted(TC_INVALID);
234 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
235 if (nwid == NULL)
return;
237 nwid->SetHighlighted(highlighted_colour);
240 if (highlighted_colour != TC_INVALID) {
247 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
248 if (nwid == NULL)
continue;
249 if (!nwid->IsHighlighted())
continue;
267 const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
268 if (nwid == NULL)
return false;
270 return nwid->IsHighlighted();
282 if (widget < 0)
return;
293 NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget);
309 return this->GetWidget<NWidgetScrollbar>(widnum);
319 return this->GetWidget<NWidgetScrollbar>(widnum);
410 Rect r = {0, 0, 0, 0};
434 if (_focused_window == w)
return;
437 if (_focused_window != NULL) {
442 Window *old_focused = _focused_window;
446 if (old_focused != NULL) old_focused->
OnFocusLost();
447 if (_focused_window != NULL) _focused_window->
OnFocus();
457 if (_focused_window == NULL)
return false;
491 if (this->GetWidget<NWidgetCore>(widget_index) == this->
nested_focus)
return false;
497 this->
nested_focus = this->GetWidget<NWidgetCore>(widget_index);
520 va_start(wdg_list, widgets);
524 widgets = va_arg(wdg_list,
int);
539 va_start(wdg_list, widgets);
543 widgets = va_arg(wdg_list,
int);
558 if (((type & ~WWB_PUSHBUTTON) <
WWT_LAST || type == NWID_PUSHBUTTON_DROPDOWN) &&
594 NWidgetCore *nw = this->GetWidget<NWidgetCore>(hotkey);
637 bool focused_widget_changed =
false;
639 if (_focused_window != w &&
642 focused_widget_changed =
true;
646 if (nw == NULL)
return;
651 int widget_index = nw->
index;
677 switch (widget_type) {
685 if (query != NULL) query->ClickEditBox(w, pt, widget_index, click_count, focused_widget_changed);
747 if (widget_index < 0)
return;
755 w->
OnClick(pt, widget_index, click_count);
767 if (wid == NULL)
return;
770 if (wid->
index >= 0) {
794 if (wid == NULL)
return;
803 if (wid->
index < 0)
return;
818 if (nwid == NULL)
return;
884 left < v->left + v->
width &&
885 top < v->top + v->
height) {
889 if (left < (x = v->
left)) {
901 if (top < (x = v->
top)) {
919 dp->width = right - left;
920 dp->height = bottom - top;
921 dp->left = left - w->
left;
922 dp->top = top - w->
top;
923 dp->pitch = _screen.pitch;
943 FOR_ALL_WINDOWS_FROM_BACK(w) {
947 left < w->left + w->
width &&
948 top < w->top + w->
height) {
975 int window_width = this->
width;
976 int window_height = this->
height;
988 window_width =
max(window_width + rx, this->
width);
989 window_height =
max(window_height + ry, this->
height);
1036 FOR_ALL_WINDOWS_FROM_BACK(v) {
1050 while (child != NULL) {
1067 if (_mouseover_last_w ==
this) _mouseover_last_w = NULL;
1070 if (_last_scroll_window ==
this) _last_scroll_window = NULL;
1073 if (_focused_window ==
this) {
1075 _focused_window = NULL;
1080 if (this->
viewport != NULL) DeleteWindowViewport(
this);
1108 FOR_ALL_WINDOWS_FROM_BACK(w) {
1124 FOR_ALL_WINDOWS_FROM_BACK(w) {
1140 if (force || w == NULL ||
1158 FOR_ALL_WINDOWS_FROM_BACK(w) {
1161 goto restart_search;
1180 FOR_ALL_WINDOWS_FROM_BACK(w) {
1181 if (w->
owner ==
id) {
1183 goto restart_search;
1201 FOR_ALL_WINDOWS_FROM_BACK(w) {
1202 if (w->
owner != old_owner)
continue;
1219 w->
owner = new_owner;
1249 static inline bool IsVitalWindow(
const Window *w)
1275 uint z_priority = 0;
1353 if (_z_front_window == NULL) {
1355 _z_front_window = _z_back_window = w;
1360 uint last_z_priority = UINT_MAX;
1375 _z_back_window->
z_back = w;
1377 }
else if (v == _z_front_window) {
1382 _z_front_window = w;
1401 assert(_z_front_window == w);
1402 _z_front_window = w->
z_back;
1408 assert(_z_back_window == w);
1484 this->
width = sm_width;
1485 this->
height = sm_height;
1500 def_width =
max(def_width, this->
width);
1501 def_height =
max(def_height, this->
height);
1507 if (this->
width != def_width || this->
height != def_height) {
1509 int free_height = _screen.height;
1511 if (wt != NULL) free_height -= wt->
height;
1513 if (wt != NULL) free_height -= wt->
height;
1515 int enlarge_x =
max(
min(def_width - this->
width, _screen.width - this->width), 0);
1531 int nx = this->
left;
1534 if (nx + this->
width > _screen.width) nx -= (nx + this->
width - _screen.width);
1537 ny =
max(ny, (wt == NULL ||
this == wt || this->
top == 0) ? 0 : wt->
height);
1563 int right = width + left;
1564 int bottom = height + top;
1567 if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height)
return false;
1571 FOR_ALL_WINDOWS_FROM_BACK(w) {
1574 if (right > w->
left &&
1603 if (left < -(width >> 2) || left > _screen.width - (width >> 1))
return false;
1605 if (top < 22 || top > _screen.height - (height >> 2))
return false;
1609 FOR_ALL_WINDOWS_FROM_BACK(w) {
1612 if (left + width > w->
left &&
1614 top + height > w->
top &&
1637 if (
IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->
height + 2 : 2, width, height, pt))
return pt;
1644 FOR_ALL_WINDOWS_FROM_BACK(w) {
1661 FOR_ALL_WINDOWS_FROM_BACK(w) {
1673 int left = 0, top = 24;
1676 FOR_ALL_WINDOWS_FROM_BACK(w) {
1677 if (w->
left == left && w->
top == top) {
1730 w->
left < _screen.width - 20 && w->
left > -60 && w->
top < _screen.height - 20) {
1733 if (pt.x > _screen.width + 10 - default_width) {
1734 pt.x = (_screen.width + 10 - default_width) - 20;
1748 pt.x = (_screen.width - default_width) / 2;
1749 pt.y = (_screen.height - default_height) / 2;
1778 int biggest_index = -1;
1829 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1845 _z_back_window = NULL;
1846 _z_front_window = NULL;
1847 _focused_window = NULL;
1848 _mouseover_last_w = NULL;
1849 _last_scroll_window = NULL;
1854 NWidgetScrollbar::InvalidateDimensionCache();
1867 FOR_ALL_WINDOWS_FROM_FRONT(w)
delete w;
1869 for (w = _z_front_window; w != NULL; ) {
1875 _z_front_window = NULL;
1876 _z_back_window = NULL;
1889 static void DecreaseWindowCounters()
1892 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1893 if (_scroller_click_timeout == 0) {
1910 it->second->HandleEditBox(w, it->first);
1916 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1926 static void HandlePlacePresize()
1931 if (w == NULL)
return;
1933 Point pt = GetTileBelowCursor();
1956 pt.x = _cursor.
pos.x - w->
left;
1957 pt.y = _cursor.
pos.y - w->
top;
1975 if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
1977 Point pt = { -1, -1 };
1982 _mouseover_last_w = w;
2013 if (v == NULL)
return;
2017 int safe_y = (dir ==
PHD_UP) ? (v->
top - MIN_VISIBLE_TITLE_BAR - rect.top) : (v_bottom + MIN_VISIBLE_TITLE_BAR - rect.bottom);
2019 if (*ny + rect.top <= v->
top - MIN_VISIBLE_TITLE_BAR)
return;
2020 if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR)
return;
2023 if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) {
2024 if (v->
left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2027 if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) {
2028 if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2033 if (px + rect.left < v->
left && v->
left >= MIN_VISIBLE_TITLE_BAR) {
2034 *nx = v->
left - MIN_VISIBLE_TITLE_BAR - rect.left;
2035 }
else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) {
2036 *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2054 if (caption != NULL) {
2055 caption_rect.left = caption->
pos_x;
2057 caption_rect.top = caption->
pos_y;
2061 nx =
Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2062 ny =
Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2090 if (delta_x != 0 || delta_y != 0) {
2091 if (clamp_to_screen) {
2094 int new_right = w->
left + w->
width + delta_x;
2095 int new_bottom = w->
top + w->
height + delta_y;
2127 return (w == NULL) ? 0 : w->
top + w->
height;
2138 return (w == NULL) ? _screen.height : w->
top;
2157 FOR_ALL_WINDOWS_FROM_BACK(w) {
2167 int x = _cursor.
pos.x + _drag_delta.x;
2168 int y = _cursor.
pos.y + _drag_delta.y;
2179 FOR_ALL_WINDOWS_FROM_BACK(v) {
2180 if (v == w)
continue;
2185 if (delta <= hsnap) {
2192 if (delta <= hsnap) {
2201 if (delta <= hsnap) {
2208 if (delta <= hsnap) {
2217 if (delta <= vsnap) {
2224 if (delta <= vsnap) {
2233 if (delta <= vsnap) {
2240 if (delta <= vsnap) {
2263 int x, y = _cursor.
pos.y - _drag_delta.y;
2265 x = _drag_delta.x - _cursor.
pos.x;
2267 x = _cursor.
pos.x - _drag_delta.x;
2275 if (w->
top + w->
height + y > _screen.height) {
2276 y = _screen.height - w->
height - w->
top;
2313 _dragging_window =
false;
2325 _dragging_window =
true;
2327 _drag_delta.x = w->
left - _cursor.
pos.x;
2328 _drag_delta.y = w->
top - _cursor.
pos.y;
2343 _dragging_window =
true;
2345 _drag_delta.x = _cursor.
pos.x;
2346 _drag_delta.y = _cursor.
pos.y;
2359 FOR_ALL_WINDOWS_FROM_BACK(w) {
2373 i = _cursor.
pos.x - _cursorpos_drag_start.x;
2376 i = _cursor.
pos.y - _cursorpos_drag_start.y;
2380 if (_scroller_click_timeout == 1) {
2381 _scroller_click_timeout = 3;
2420 _last_scroll_window = NULL;
2433 delta.x = -_cursor.
delta.x;
2434 delta.y = -_cursor.
delta.y;
2436 delta.x = _cursor.
delta.x;
2437 delta.y = _cursor.
delta.y;
2440 if (scrollwheel_scrolling) {
2442 delta.x = _cursor.h_wheel;
2443 delta.y = _cursor.v_wheel;
2444 _cursor.v_wheel = 0;
2445 _cursor.h_wheel = 0;
2449 if (delta.x != 0 || delta.y != 0) _last_scroll_window->
OnScroll(delta);
2451 _cursor.
delta.x = 0;
2452 _cursor.
delta.y = 0;
2468 bool bring_to_front =
false;
2478 int w_width = w->
width;
2479 int w_height = w->
height;
2502 if (w->
left + w_width <= u->left ||
2504 w->
top + w_height <= u->top ||
2509 bring_to_front =
true;
2531 switch (query->text.HandleKeyPress(key, keycode)) {
2575 if (query->text.
bytes <= 1) {
2610 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2615 if (key == 0 && keycode == 0)
return;
2629 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2648 HandleGlobalHotkeys(key, keycode);
2658 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2668 void Window::InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2671 if (query == NULL)
return;
2673 if (query->text.
InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2685 void HandleTextInput(
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2710 int x = _cursor.
pos.x;
2711 int y = _cursor.
pos.y;
2717 if (vp == NULL)
return;
2723 #define scrollspeed 3
2726 }
else if (15 - (vp->
width - x) > 0) {
2731 }
else if (15 - (vp->
height - y) > 0) {
2750 static void ScrollMainViewport(
int x,
int y)
2752 if (_game_mode != GM_MENU) {
2789 static void HandleKeyScrolling()
2797 ScrollMainViewport(scrollamt[
_dirkeys][0] * factor, scrollamt[
_dirkeys][1] * factor);
2801 static void MouseLoop(
MouseClick click,
int mousewheel)
2807 HandlePlacePresize();
2819 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling)
return;
2821 int x = _cursor.
pos.x;
2822 int y = _cursor.
pos.y;
2824 if (w == NULL)
return;
2832 if (mousewheel != 0) {
2841 if (scrollwheel_scrolling) click = MC_RIGHT;
2843 case MC_DOUBLE_LEFT:
2845 if (HandleViewportClicked(vp, x, y))
return;
2860 _cursor.h_wheel = 0;
2861 _cursor.v_wheel = 0;
2874 case MC_DOUBLE_LEFT:
2900 static int double_click_time = 0;
2901 static Point double_click_pos = {0, 0};
2910 click = MC_DOUBLE_LEFT;
2913 double_click_pos = _cursor.
pos;
2915 _input_events_this_tick++;
2919 _input_events_this_tick++;
2923 if (_cursor.
wheel) {
2924 mousewheel = _cursor.
wheel;
2926 _input_events_this_tick++;
2929 static uint32 hover_time = 0;
2930 static Point hover_pos = {0, 0};
2936 hover_pos = _cursor.
pos;
2942 _input_events_this_tick++;
2964 MouseLoop(click, mousewheel);
2969 _cursor.
delta.x = 0;
2970 _cursor.
delta.y = 0;
2981 uint deletable_count = 0;
2982 Window *w, *last_deletable = NULL;
2983 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2993 assert(last_deletable != NULL);
2994 delete last_deletable;
3008 HandleKeyScrolling();
3011 for (
Window *v = _z_front_window; v != NULL; ) {
3021 if (_scroller_click_timeout != 0) _scroller_click_timeout--;
3022 DecreaseWindowCounters();
3024 if (_input_events_this_tick != 0) {
3026 _input_events_this_tick = 0;
3043 static int highlight_timer = 1;
3044 if (--highlight_timer == 0) {
3045 highlight_timer = 15;
3049 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3058 static int we4_timer = 0;
3059 int t = we4_timer + 1;
3062 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3069 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3078 FOR_ALL_WINDOWS_FROM_BACK(w) {
3095 FOR_ALL_WINDOWS_FROM_BACK(w) {
3109 FOR_ALL_WINDOWS_FROM_BACK(w) {
3123 FOR_ALL_WINDOWS_FROM_BACK(w) {
3195 FOR_ALL_WINDOWS_FROM_BACK(w) {
3214 FOR_ALL_WINDOWS_FROM_BACK(w) {
3227 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3246 FOR_ALL_WINDOWS_FROM_BACK(w) {
3255 goto restart_search;
3278 FOR_ALL_WINDOWS_FROM_BACK(w) {
3281 goto restart_search;
3298 FOR_ALL_WINDOWS_FROM_BACK(w) {
3301 goto restart_search;
3305 FOR_ALL_WINDOWS_FROM_BACK(w) w->
SetDirty();
3319 NWidgetScrollbar::InvalidateDimensionCache();
3325 FOR_ALL_WINDOWS_FROM_BACK(w) {
3328 #ifdef ENABLE_NETWORK
3350 if (w == NULL)
return 0;
3352 int old_left = w->
left;
3354 case 1: w->
left = (_screen.width - w->
width) / 2;
break;
3355 case 2: w->
left = _screen.width - w->
width;
break;
3356 default: w->
left = 0;
break;
3370 DEBUG(misc, 5,
"Repositioning Main Toolbar...");
3381 DEBUG(misc, 5,
"Repositioning statusbar...");
3392 DEBUG(misc, 5,
"Repositioning news message...");
3403 DEBUG(misc, 5,
"Repositioning network chat window...");
3416 FOR_ALL_WINDOWS_FROM_BACK(w) {
3434 FOR_ALL_WINDOWS_FROM_BACK(w) {
3476 top = (newh - w->
height) >> 1;
3477 left = (neww - w->
width) >> 1;
3482 if (left + (w->
width >> 1) >= neww) left = neww - w->
width;
3483 if (left < 0) left = 0;
3486 if (top + (w->
height >> 1) >= newh) top = newh - w->
height;