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) {
789 if (wid == NULL)
return;
798 if (wid->
index < 0)
return;
813 if (nwid == NULL)
return;
879 left < v->left + v->
width &&
880 top < v->top + v->
height) {
884 if (left < (x = v->
left)) {
896 if (top < (x = v->
top)) {
914 dp->width = right - left;
915 dp->height = bottom - top;
916 dp->left = left - w->
left;
917 dp->top = top - w->
top;
918 dp->pitch = _screen.pitch;
938 FOR_ALL_WINDOWS_FROM_BACK(w) {
942 left < w->left + w->
width &&
943 top < w->top + w->
height) {
970 int window_width = this->
width;
971 int window_height = this->
height;
983 window_width =
max(window_width + rx, this->
width);
984 window_height =
max(window_height + ry, this->
height);
1031 FOR_ALL_WINDOWS_FROM_BACK(v) {
1045 while (child != NULL) {
1058 ResetObjectToPlace();
1062 if (_mouseover_last_w ==
this) _mouseover_last_w = NULL;
1065 if (_last_scroll_window ==
this) _last_scroll_window = NULL;
1068 if (_focused_window ==
this) {
1070 _focused_window = NULL;
1075 if (this->
viewport != NULL) DeleteWindowViewport(
this);
1103 FOR_ALL_WINDOWS_FROM_BACK(w) {
1119 FOR_ALL_WINDOWS_FROM_BACK(w) {
1135 if (force || w == NULL ||
1153 FOR_ALL_WINDOWS_FROM_BACK(w) {
1156 goto restart_search;
1175 FOR_ALL_WINDOWS_FROM_BACK(w) {
1176 if (w->
owner ==
id) {
1178 goto restart_search;
1196 FOR_ALL_WINDOWS_FROM_BACK(w) {
1197 if (w->
owner != old_owner)
continue;
1213 w->
owner = new_owner;
1243 static inline bool IsVitalWindow(
const Window *w)
1269 uint z_priority = 0;
1335 if (_z_front_window == NULL) {
1337 _z_front_window = _z_back_window = w;
1342 uint last_z_priority = UINT_MAX;
1357 _z_back_window->
z_back = w;
1359 }
else if (v == _z_front_window) {
1364 _z_front_window = w;
1383 assert(_z_front_window == w);
1384 _z_front_window = w->
z_back;
1390 assert(_z_back_window == w);
1466 this->
width = sm_width;
1467 this->
height = sm_height;
1482 def_width =
max(def_width, this->
width);
1483 def_height =
max(def_height, this->
height);
1489 if (this->
width != def_width || this->
height != def_height) {
1491 int free_height = _screen.height;
1493 if (wt != NULL) free_height -= wt->
height;
1495 if (wt != NULL) free_height -= wt->
height;
1497 int enlarge_x =
max(
min(def_width - this->
width, _screen.width - this->width), 0);
1513 int nx = this->
left;
1516 if (nx + this->
width > _screen.width) nx -= (nx + this->
width - _screen.width);
1519 ny =
max(ny, (wt == NULL ||
this == wt || this->
top == 0) ? 0 : wt->
height);
1545 int right = width + left;
1546 int bottom = height + top;
1549 if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height)
return false;
1553 FOR_ALL_WINDOWS_FROM_BACK(w) {
1556 if (right > w->
left &&
1585 if (left < -(width >> 2) || left > _screen.width - (width >> 1))
return false;
1587 if (top < 22 || top > _screen.height - (height >> 2))
return false;
1591 FOR_ALL_WINDOWS_FROM_BACK(w) {
1594 if (left + width > w->
left &&
1596 top + height > w->
top &&
1619 if (
IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->
height + 2 : 2, width, height, pt))
return pt;
1626 FOR_ALL_WINDOWS_FROM_BACK(w) {
1643 FOR_ALL_WINDOWS_FROM_BACK(w) {
1655 int left = 0, top = 24;
1658 FOR_ALL_WINDOWS_FROM_BACK(w) {
1659 if (w->
left == left && w->
top == top) {
1712 w->
left < _screen.width - 20 && w->
left > -60 && w->
top < _screen.height - 20) {
1715 if (pt.x > _screen.width + 10 - default_width) {
1716 pt.x = (_screen.width + 10 - default_width) - 20;
1730 pt.x = (_screen.width - default_width) / 2;
1731 pt.y = (_screen.height - default_height) / 2;
1760 int biggest_index = -1;
1811 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1827 _z_back_window = NULL;
1828 _z_front_window = NULL;
1829 _focused_window = NULL;
1830 _mouseover_last_w = NULL;
1831 _last_scroll_window = NULL;
1836 NWidgetScrollbar::InvalidateDimensionCache();
1849 FOR_ALL_WINDOWS_FROM_FRONT(w)
delete w;
1851 for (w = _z_front_window; w != NULL; ) {
1857 _z_front_window = NULL;
1858 _z_back_window = NULL;
1871 static void DecreaseWindowCounters()
1874 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1875 if (_scroller_click_timeout == 0) {
1892 it->second->HandleEditBox(w, it->first);
1898 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1908 static void HandlePlacePresize()
1913 if (w == NULL)
return;
1915 Point pt = GetTileBelowCursor();
1938 pt.x = _cursor.pos.x - w->
left;
1939 pt.y = _cursor.pos.y - w->
top;
1957 if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
1959 Point pt = { -1, -1 };
1964 _mouseover_last_w = w;
1968 Point pt = { _cursor.pos.x - w->
left, _cursor.pos.y - w->
top };
1995 if (v == NULL)
return;
1999 int safe_y = (dir ==
PHD_UP) ? (v->
top - MIN_VISIBLE_TITLE_BAR - rect.top) : (v_bottom + MIN_VISIBLE_TITLE_BAR - rect.bottom);
2001 if (*ny + rect.top <= v->
top - MIN_VISIBLE_TITLE_BAR)
return;
2002 if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR)
return;
2005 if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) {
2006 if (v->
left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2009 if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) {
2010 if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2015 if (px + rect.left < v->
left && v->
left >= MIN_VISIBLE_TITLE_BAR) {
2016 *nx = v->
left - MIN_VISIBLE_TITLE_BAR - rect.left;
2017 }
else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) {
2018 *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2036 if (caption != NULL) {
2037 caption_rect.left = caption->
pos_x;
2039 caption_rect.top = caption->
pos_y;
2043 nx =
Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2044 ny =
Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2072 if (delta_x != 0 || delta_y != 0) {
2073 if (clamp_to_screen) {
2076 int new_right = w->
left + w->
width + delta_x;
2077 int new_bottom = w->
top + w->
height + delta_y;
2109 return (w == NULL) ? 0 : w->
top + w->
height;
2120 return (w == NULL) ? _screen.height : w->
top;
2139 FOR_ALL_WINDOWS_FROM_BACK(w) {
2149 int x = _cursor.pos.x + _drag_delta.x;
2150 int y = _cursor.pos.y + _drag_delta.y;
2161 FOR_ALL_WINDOWS_FROM_BACK(v) {
2162 if (v == w)
continue;
2167 if (delta <= hsnap) {
2174 if (delta <= hsnap) {
2183 if (delta <= hsnap) {
2190 if (delta <= hsnap) {
2199 if (delta <= vsnap) {
2206 if (delta <= vsnap) {
2215 if (delta <= vsnap) {
2222 if (delta <= vsnap) {
2245 int x, y = _cursor.pos.y - _drag_delta.y;
2247 x = _drag_delta.x - _cursor.pos.x;
2249 x = _cursor.pos.x - _drag_delta.x;
2257 if (w->
top + w->
height + y > _screen.height) {
2258 y = _screen.height - w->
height - w->
top;
2295 _dragging_window =
false;
2307 _dragging_window =
true;
2309 _drag_delta.x = w->
left - _cursor.pos.x;
2310 _drag_delta.y = w->
top - _cursor.pos.y;
2325 _dragging_window =
true;
2327 _drag_delta.x = _cursor.pos.x;
2328 _drag_delta.y = _cursor.pos.y;
2341 FOR_ALL_WINDOWS_FROM_BACK(w) {
2355 i = _cursor.pos.x - _cursorpos_drag_start.x;
2358 i = _cursor.pos.y - _cursorpos_drag_start.y;
2362 if (_scroller_click_timeout == 1) {
2363 _scroller_click_timeout = 3;
2397 if (_last_scroll_window == NULL) _last_scroll_window =
FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2402 _last_scroll_window = NULL;
2415 delta.x = -_cursor.
delta.x;
2416 delta.y = -_cursor.
delta.y;
2418 delta.x = _cursor.
delta.x;
2419 delta.y = _cursor.
delta.y;
2422 if (scrollwheel_scrolling) {
2424 delta.x = _cursor.h_wheel;
2425 delta.y = _cursor.v_wheel;
2426 _cursor.v_wheel = 0;
2427 _cursor.h_wheel = 0;
2431 if (delta.x != 0 || delta.y != 0) _last_scroll_window->
OnScroll(delta);
2433 _cursor.
delta.x = 0;
2434 _cursor.
delta.y = 0;
2450 bool bring_to_front =
false;
2460 int w_width = w->
width;
2461 int w_height = w->
height;
2484 if (w->
left + w_width <= u->left ||
2486 w->
top + w_height <= u->top ||
2491 bring_to_front =
true;
2513 switch (query->text.HandleKeyPress(key, keycode)) {
2557 if (query->text.
bytes <= 1) {
2592 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2597 if (key == 0 && keycode == 0)
return;
2611 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2630 HandleGlobalHotkeys(key, keycode);
2640 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2650 void Window::InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2653 if (query == NULL)
return;
2655 if (query->text.
InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2667 void HandleTextInput(
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2692 int x = _cursor.pos.x;
2693 int y = _cursor.pos.y;
2699 if (vp == NULL)
return;
2705 #define scrollspeed 3
2708 }
else if (15 - (vp->
width - x) > 0) {
2713 }
else if (15 - (vp->
height - y) > 0) {
2732 static void ScrollMainViewport(
int x,
int y)
2734 if (_game_mode != GM_MENU) {
2771 static void HandleKeyScrolling()
2779 ScrollMainViewport(scrollamt[
_dirkeys][0] * factor, scrollamt[
_dirkeys][1] * factor);
2783 static void MouseLoop(
MouseClick click,
int mousewheel)
2789 HandlePlacePresize();
2801 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling)
return;
2803 int x = _cursor.pos.x;
2804 int y = _cursor.pos.y;
2806 if (w == NULL)
return;
2814 if (mousewheel != 0) {
2823 if (scrollwheel_scrolling) click = MC_RIGHT;
2825 case MC_DOUBLE_LEFT:
2828 if (!HandleViewportClicked(vp, x, y) &&
2842 _cursor.h_wheel = 0;
2843 _cursor.v_wheel = 0;
2853 case MC_DOUBLE_LEFT:
2879 static int double_click_time = 0;
2880 static Point double_click_pos = {0, 0};
2889 click = MC_DOUBLE_LEFT;
2892 double_click_pos = _cursor.pos;
2894 _input_events_this_tick++;
2898 _input_events_this_tick++;
2902 if (_cursor.
wheel) {
2903 mousewheel = _cursor.
wheel;
2905 _input_events_this_tick++;
2908 static uint32 hover_time = 0;
2909 static Point hover_pos = {0, 0};
2915 hover_pos = _cursor.pos;
2921 _input_events_this_tick++;
2943 MouseLoop(click, mousewheel);
2948 _cursor.
delta.x = 0;
2949 _cursor.
delta.y = 0;
2960 uint deletable_count = 0;
2961 Window *w, *last_deletable = NULL;
2962 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2972 assert(last_deletable != NULL);
2973 delete last_deletable;
2987 HandleKeyScrolling();
2990 for (
Window *v = _z_front_window; v != NULL; ) {
3000 if (_scroller_click_timeout != 0) _scroller_click_timeout--;
3001 DecreaseWindowCounters();
3003 if (_input_events_this_tick != 0) {
3005 _input_events_this_tick = 0;
3022 static int highlight_timer = 1;
3023 if (--highlight_timer == 0) {
3024 highlight_timer = 15;
3028 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3033 static int we4_timer = 0;
3034 int t = we4_timer + 1;
3037 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3044 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3053 FOR_ALL_WINDOWS_FROM_BACK(w) {
3070 FOR_ALL_WINDOWS_FROM_BACK(w) {
3084 FOR_ALL_WINDOWS_FROM_BACK(w) {
3098 FOR_ALL_WINDOWS_FROM_BACK(w) {
3170 FOR_ALL_WINDOWS_FROM_BACK(w) {
3189 FOR_ALL_WINDOWS_FROM_BACK(w) {
3202 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3221 FOR_ALL_WINDOWS_FROM_BACK(w) {
3230 goto restart_search;
3253 FOR_ALL_WINDOWS_FROM_BACK(w) {
3256 goto restart_search;
3273 FOR_ALL_WINDOWS_FROM_BACK(w) {
3276 goto restart_search;
3280 FOR_ALL_WINDOWS_FROM_BACK(w) w->
SetDirty();
3294 NWidgetScrollbar::InvalidateDimensionCache();
3300 FOR_ALL_WINDOWS_FROM_BACK(w) {
3303 #ifdef ENABLE_NETWORK
3325 if (w == NULL)
return 0;
3327 int old_left = w->
left;
3329 case 1: w->
left = (_screen.width - w->
width) / 2;
break;
3330 case 2: w->
left = _screen.width - w->
width;
break;
3331 default: w->
left = 0;
break;
3345 DEBUG(misc, 5,
"Repositioning Main Toolbar...");
3356 DEBUG(misc, 5,
"Repositioning statusbar...");
3367 DEBUG(misc, 5,
"Repositioning news message...");
3378 DEBUG(misc, 5,
"Repositioning network chat window...");
3391 FOR_ALL_WINDOWS_FROM_BACK(w) {
3409 FOR_ALL_WINDOWS_FROM_BACK(w) {
3451 top = (newh - w->
height) >> 1;
3452 left = (neww - w->
width) >> 1;
3457 if (left + (w->
width >> 1) >= neww) left = neww - w->
width;
3458 if (left < 0) left = 0;
3461 if (top + (w->
height >> 1) >= newh) top = newh - w->
height;
3478 ResetObjectToPlace();