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) {
797 if (wid == NULL)
return;
806 if (wid->
index < 0)
return;
821 if (nwid == NULL)
return;
887 left < v->left + v->
width &&
888 top < v->top + v->
height) {
892 if (left < (x = v->
left)) {
904 if (top < (x = v->
top)) {
922 dp->width = right - left;
923 dp->height = bottom - top;
924 dp->left = left - w->
left;
925 dp->top = top - w->
top;
926 dp->pitch = _screen.pitch;
946 FOR_ALL_WINDOWS_FROM_BACK(w) {
950 left < w->left + w->
width &&
951 top < w->top + w->
height) {
978 int window_width = this->
width;
979 int window_height = this->
height;
991 window_width =
max(window_width + rx, this->
width);
992 window_height =
max(window_height + ry, this->
height);
1039 FOR_ALL_WINDOWS_FROM_BACK(v) {
1053 while (child != NULL) {
1070 if (_mouseover_last_w ==
this) _mouseover_last_w = NULL;
1073 if (_last_scroll_window ==
this) _last_scroll_window = NULL;
1076 if (_focused_window ==
this) {
1078 _focused_window = NULL;
1083 if (this->
viewport != NULL) DeleteWindowViewport(
this);
1111 FOR_ALL_WINDOWS_FROM_BACK(w) {
1127 FOR_ALL_WINDOWS_FROM_BACK(w) {
1143 if (force || w == NULL ||
1161 FOR_ALL_WINDOWS_FROM_BACK(w) {
1164 goto restart_search;
1183 FOR_ALL_WINDOWS_FROM_BACK(w) {
1184 if (w->
owner ==
id) {
1186 goto restart_search;
1204 FOR_ALL_WINDOWS_FROM_BACK(w) {
1205 if (w->
owner != old_owner)
continue;
1222 w->
owner = new_owner;
1252 static inline bool IsVitalWindow(
const Window *w)
1278 uint z_priority = 0;
1344 if (_z_front_window == NULL) {
1346 _z_front_window = _z_back_window = w;
1351 uint last_z_priority = UINT_MAX;
1366 _z_back_window->
z_back = w;
1368 }
else if (v == _z_front_window) {
1373 _z_front_window = w;
1392 assert(_z_front_window == w);
1393 _z_front_window = w->
z_back;
1399 assert(_z_back_window == w);
1475 this->
width = sm_width;
1476 this->
height = sm_height;
1491 def_width =
max(def_width, this->
width);
1492 def_height =
max(def_height, this->
height);
1498 if (this->
width != def_width || this->
height != def_height) {
1500 int free_height = _screen.height;
1502 if (wt != NULL) free_height -= wt->
height;
1504 if (wt != NULL) free_height -= wt->
height;
1506 int enlarge_x =
max(
min(def_width - this->
width, _screen.width - this->width), 0);
1522 int nx = this->
left;
1525 if (nx + this->
width > _screen.width) nx -= (nx + this->
width - _screen.width);
1528 ny =
max(ny, (wt == NULL ||
this == wt || this->
top == 0) ? 0 : wt->
height);
1554 int right = width + left;
1555 int bottom = height + top;
1558 if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height)
return false;
1562 FOR_ALL_WINDOWS_FROM_BACK(w) {
1565 if (right > w->
left &&
1594 if (left < -(width >> 2) || left > _screen.width - (width >> 1))
return false;
1596 if (top < 22 || top > _screen.height - (height >> 2))
return false;
1600 FOR_ALL_WINDOWS_FROM_BACK(w) {
1603 if (left + width > w->
left &&
1605 top + height > w->
top &&
1628 if (
IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->
height + 2 : 2, width, height, pt))
return pt;
1635 FOR_ALL_WINDOWS_FROM_BACK(w) {
1652 FOR_ALL_WINDOWS_FROM_BACK(w) {
1664 int left = 0, top = 24;
1667 FOR_ALL_WINDOWS_FROM_BACK(w) {
1668 if (w->
left == left && w->
top == top) {
1721 w->
left < _screen.width - 20 && w->
left > -60 && w->
top < _screen.height - 20) {
1724 if (pt.x > _screen.width + 10 - default_width) {
1725 pt.x = (_screen.width + 10 - default_width) - 20;
1739 pt.x = (_screen.width - default_width) / 2;
1740 pt.y = (_screen.height - default_height) / 2;
1769 int biggest_index = -1;
1820 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1836 _z_back_window = NULL;
1837 _z_front_window = NULL;
1838 _focused_window = NULL;
1839 _mouseover_last_w = NULL;
1840 _last_scroll_window = NULL;
1845 NWidgetScrollbar::InvalidateDimensionCache();
1858 FOR_ALL_WINDOWS_FROM_FRONT(w)
delete w;
1860 for (w = _z_front_window; w != NULL; ) {
1866 _z_front_window = NULL;
1867 _z_back_window = NULL;
1880 static void DecreaseWindowCounters()
1883 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1884 if (_scroller_click_timeout == 0) {
1901 it->second->HandleEditBox(w, it->first);
1907 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1917 static void HandlePlacePresize()
1922 if (w == NULL)
return;
1924 Point pt = GetTileBelowCursor();
1947 pt.x = _cursor.
pos.x - w->
left;
1948 pt.y = _cursor.
pos.y - w->
top;
1966 if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
1968 Point pt = { -1, -1 };
1973 _mouseover_last_w = w;
2004 if (v == NULL)
return;
2008 int safe_y = (dir ==
PHD_UP) ? (v->
top - MIN_VISIBLE_TITLE_BAR - rect.top) : (v_bottom + MIN_VISIBLE_TITLE_BAR - rect.bottom);
2010 if (*ny + rect.top <= v->
top - MIN_VISIBLE_TITLE_BAR)
return;
2011 if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR)
return;
2014 if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) {
2015 if (v->
left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2018 if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) {
2019 if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2024 if (px + rect.left < v->
left && v->
left >= MIN_VISIBLE_TITLE_BAR) {
2025 *nx = v->
left - MIN_VISIBLE_TITLE_BAR - rect.left;
2026 }
else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) {
2027 *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2045 if (caption != NULL) {
2046 caption_rect.left = caption->
pos_x;
2048 caption_rect.top = caption->
pos_y;
2052 nx =
Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2053 ny =
Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2081 if (delta_x != 0 || delta_y != 0) {
2082 if (clamp_to_screen) {
2085 int new_right = w->
left + w->
width + delta_x;
2086 int new_bottom = w->
top + w->
height + delta_y;
2118 return (w == NULL) ? 0 : w->
top + w->
height;
2129 return (w == NULL) ? _screen.height : w->
top;
2148 FOR_ALL_WINDOWS_FROM_BACK(w) {
2158 int x = _cursor.
pos.x + _drag_delta.x;
2159 int y = _cursor.
pos.y + _drag_delta.y;
2170 FOR_ALL_WINDOWS_FROM_BACK(v) {
2171 if (v == w)
continue;
2176 if (delta <= hsnap) {
2183 if (delta <= hsnap) {
2192 if (delta <= hsnap) {
2199 if (delta <= hsnap) {
2208 if (delta <= vsnap) {
2215 if (delta <= vsnap) {
2224 if (delta <= vsnap) {
2231 if (delta <= vsnap) {
2254 int x, y = _cursor.
pos.y - _drag_delta.y;
2256 x = _drag_delta.x - _cursor.
pos.x;
2258 x = _cursor.
pos.x - _drag_delta.x;
2266 if (w->
top + w->
height + y > _screen.height) {
2267 y = _screen.height - w->
height - w->
top;
2304 _dragging_window =
false;
2316 _dragging_window =
true;
2318 _drag_delta.x = w->
left - _cursor.
pos.x;
2319 _drag_delta.y = w->
top - _cursor.
pos.y;
2334 _dragging_window =
true;
2336 _drag_delta.x = _cursor.
pos.x;
2337 _drag_delta.y = _cursor.
pos.y;
2350 FOR_ALL_WINDOWS_FROM_BACK(w) {
2364 i = _cursor.
pos.x - _cursorpos_drag_start.x;
2367 i = _cursor.
pos.y - _cursorpos_drag_start.y;
2371 if (_scroller_click_timeout == 1) {
2372 _scroller_click_timeout = 3;
2411 _last_scroll_window = NULL;
2424 delta.x = -_cursor.
delta.x;
2425 delta.y = -_cursor.
delta.y;
2427 delta.x = _cursor.
delta.x;
2428 delta.y = _cursor.
delta.y;
2431 if (scrollwheel_scrolling) {
2433 delta.x = _cursor.h_wheel;
2434 delta.y = _cursor.v_wheel;
2435 _cursor.v_wheel = 0;
2436 _cursor.h_wheel = 0;
2440 if (delta.x != 0 || delta.y != 0) _last_scroll_window->
OnScroll(delta);
2442 _cursor.
delta.x = 0;
2443 _cursor.
delta.y = 0;
2459 bool bring_to_front =
false;
2469 int w_width = w->
width;
2470 int w_height = w->
height;
2493 if (w->
left + w_width <= u->left ||
2495 w->
top + w_height <= u->top ||
2500 bring_to_front =
true;
2522 switch (query->text.HandleKeyPress(key, keycode)) {
2566 if (query->text.
bytes <= 1) {
2601 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2606 if (key == 0 && keycode == 0)
return;
2620 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2639 HandleGlobalHotkeys(key, keycode);
2649 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2659 void Window::InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2662 if (query == NULL)
return;
2664 if (query->text.
InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2676 void HandleTextInput(
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2701 int x = _cursor.
pos.x;
2702 int y = _cursor.
pos.y;
2708 if (vp == NULL)
return;
2714 #define scrollspeed 3
2717 }
else if (15 - (vp->
width - x) > 0) {
2722 }
else if (15 - (vp->
height - y) > 0) {
2741 static void ScrollMainViewport(
int x,
int y)
2743 if (_game_mode != GM_MENU) {
2780 static void HandleKeyScrolling()
2788 ScrollMainViewport(scrollamt[
_dirkeys][0] * factor, scrollamt[
_dirkeys][1] * factor);
2792 static void MouseLoop(
MouseClick click,
int mousewheel)
2798 HandlePlacePresize();
2810 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling)
return;
2812 int x = _cursor.
pos.x;
2813 int y = _cursor.
pos.y;
2815 if (w == NULL)
return;
2823 if (mousewheel != 0) {
2832 if (scrollwheel_scrolling) click = MC_RIGHT;
2834 case MC_DOUBLE_LEFT:
2836 if (HandleViewportClicked(vp, x, y))
return;
2851 _cursor.h_wheel = 0;
2852 _cursor.v_wheel = 0;
2865 case MC_DOUBLE_LEFT:
2891 static int double_click_time = 0;
2892 static Point double_click_pos = {0, 0};
2901 click = MC_DOUBLE_LEFT;
2904 double_click_pos = _cursor.
pos;
2906 _input_events_this_tick++;
2910 _input_events_this_tick++;
2914 if (_cursor.
wheel) {
2915 mousewheel = _cursor.
wheel;
2917 _input_events_this_tick++;
2920 static uint32 hover_time = 0;
2921 static Point hover_pos = {0, 0};
2927 hover_pos = _cursor.
pos;
2933 _input_events_this_tick++;
2955 MouseLoop(click, mousewheel);
2960 _cursor.
delta.x = 0;
2961 _cursor.
delta.y = 0;
2972 uint deletable_count = 0;
2973 Window *w, *last_deletable = NULL;
2974 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2984 assert(last_deletable != NULL);
2985 delete last_deletable;
2999 HandleKeyScrolling();
3002 for (
Window *v = _z_front_window; v != NULL; ) {
3012 if (_scroller_click_timeout != 0) _scroller_click_timeout--;
3013 DecreaseWindowCounters();
3015 if (_input_events_this_tick != 0) {
3017 _input_events_this_tick = 0;
3034 static int highlight_timer = 1;
3035 if (--highlight_timer == 0) {
3036 highlight_timer = 15;
3040 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3049 static int we4_timer = 0;
3050 int t = we4_timer + 1;
3053 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3060 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3069 FOR_ALL_WINDOWS_FROM_BACK(w) {
3086 FOR_ALL_WINDOWS_FROM_BACK(w) {
3100 FOR_ALL_WINDOWS_FROM_BACK(w) {
3114 FOR_ALL_WINDOWS_FROM_BACK(w) {
3186 FOR_ALL_WINDOWS_FROM_BACK(w) {
3205 FOR_ALL_WINDOWS_FROM_BACK(w) {
3218 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3237 FOR_ALL_WINDOWS_FROM_BACK(w) {
3246 goto restart_search;
3269 FOR_ALL_WINDOWS_FROM_BACK(w) {
3272 goto restart_search;
3289 FOR_ALL_WINDOWS_FROM_BACK(w) {
3292 goto restart_search;
3296 FOR_ALL_WINDOWS_FROM_BACK(w) w->
SetDirty();
3310 NWidgetScrollbar::InvalidateDimensionCache();
3316 FOR_ALL_WINDOWS_FROM_BACK(w) {
3319 #ifdef ENABLE_NETWORK
3341 if (w == NULL)
return 0;
3343 int old_left = w->
left;
3345 case 1: w->
left = (_screen.width - w->
width) / 2;
break;
3346 case 2: w->
left = _screen.width - w->
width;
break;
3347 default: w->
left = 0;
break;
3361 DEBUG(misc, 5,
"Repositioning Main Toolbar...");
3372 DEBUG(misc, 5,
"Repositioning statusbar...");
3383 DEBUG(misc, 5,
"Repositioning news message...");
3394 DEBUG(misc, 5,
"Repositioning network chat window...");
3407 FOR_ALL_WINDOWS_FROM_BACK(w) {
3425 FOR_ALL_WINDOWS_FROM_BACK(w) {
3467 top = (newh - w->
height) >> 1;
3468 left = (neww - w->
width) >> 1;
3473 if (left + (w->
width >> 1) >= neww) left = neww - w->
width;
3474 if (left < 0) left = 0;
3477 if (top + (w->
height >> 1) >= newh) top = newh - w->
height;