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);
1094 FOR_ALL_WINDOWS_FROM_BACK(w) {
1110 FOR_ALL_WINDOWS_FROM_BACK(w) {
1126 if (force || w == NULL ||
1144 FOR_ALL_WINDOWS_FROM_BACK(w) {
1147 goto restart_search;
1166 FOR_ALL_WINDOWS_FROM_BACK(w) {
1167 if (w->
owner ==
id) {
1169 goto restart_search;
1187 FOR_ALL_WINDOWS_FROM_BACK(w) {
1188 if (w->
owner != old_owner)
continue;
1204 w->
owner = new_owner;
1234 static inline bool IsVitalWindow(
const Window *w)
1260 uint z_priority = 0;
1326 if (_z_front_window == NULL) {
1328 _z_front_window = _z_back_window = w;
1333 uint last_z_priority = UINT_MAX;
1348 _z_back_window->
z_back = w;
1350 }
else if (v == _z_front_window) {
1355 _z_front_window = w;
1374 assert(_z_front_window == w);
1375 _z_front_window = w->
z_back;
1381 assert(_z_back_window == w);
1457 this->
width = sm_width;
1458 this->
height = sm_height;
1473 def_width =
max(def_width, this->
width);
1474 def_height =
max(def_height, this->
height);
1480 if (this->
width != def_width || this->
height != def_height) {
1482 int free_height = _screen.height;
1484 if (wt != NULL) free_height -= wt->
height;
1486 if (wt != NULL) free_height -= wt->
height;
1488 int enlarge_x =
max(
min(def_width - this->
width, _screen.width - this->width), 0);
1504 int nx = this->
left;
1507 if (nx + this->
width > _screen.width) nx -= (nx + this->
width - _screen.width);
1510 ny =
max(ny, (wt == NULL ||
this == wt || this->
top == 0) ? 0 : wt->
height);
1536 int right = width + left;
1537 int bottom = height + top;
1540 if (left < 0 || (main_toolbar != NULL && top < main_toolbar->height) || right > _screen.width || bottom > _screen.height)
return false;
1544 FOR_ALL_WINDOWS_FROM_BACK(w) {
1547 if (right > w->
left &&
1576 if (left < -(width >> 2) || left > _screen.width - (width >> 1))
return false;
1578 if (top < 22 || top > _screen.height - (height >> 2))
return false;
1582 FOR_ALL_WINDOWS_FROM_BACK(w) {
1585 if (left + width > w->
left &&
1587 top + height > w->
top &&
1610 if (
IsGoodAutoPlace1(0, main_toolbar != NULL ? main_toolbar->
height + 2 : 2, width, height, pt))
return pt;
1617 FOR_ALL_WINDOWS_FROM_BACK(w) {
1634 FOR_ALL_WINDOWS_FROM_BACK(w) {
1646 int left = 0, top = 24;
1649 FOR_ALL_WINDOWS_FROM_BACK(w) {
1650 if (w->
left == left && w->
top == top) {
1703 w->
left < _screen.width - 20 && w->
left > -60 && w->
top < _screen.height - 20) {
1706 if (pt.x > _screen.width + 10 - default_width) {
1707 pt.x = (_screen.width + 10 - default_width) - 20;
1721 pt.x = (_screen.width - default_width) / 2;
1722 pt.y = (_screen.height - default_height) / 2;
1751 int biggest_index = -1;
1802 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1818 _z_back_window = NULL;
1819 _z_front_window = NULL;
1820 _focused_window = NULL;
1821 _mouseover_last_w = NULL;
1822 _last_scroll_window = NULL;
1827 NWidgetScrollbar::InvalidateDimensionCache();
1840 FOR_ALL_WINDOWS_FROM_FRONT(w)
delete w;
1842 for (w = _z_front_window; w != NULL; ) {
1848 _z_front_window = NULL;
1849 _z_back_window = NULL;
1862 static void DecreaseWindowCounters()
1865 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1866 if (_scroller_click_timeout == 0) {
1883 it->second->HandleEditBox(w, it->first);
1889 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1899 static void HandlePlacePresize()
1904 if (w == NULL)
return;
1906 Point pt = GetTileBelowCursor();
1929 pt.x = _cursor.pos.x - w->
left;
1930 pt.y = _cursor.pos.y - w->
top;
1948 if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
1950 Point pt = { -1, -1 };
1955 _mouseover_last_w = w;
1959 Point pt = { _cursor.pos.x - w->
left, _cursor.pos.y - w->
top };
1986 if (v == NULL)
return;
1990 int safe_y = (dir ==
PHD_UP) ? (v->
top - MIN_VISIBLE_TITLE_BAR - rect.top) : (v_bottom + MIN_VISIBLE_TITLE_BAR - rect.bottom);
1992 if (*ny + rect.top <= v->
top - MIN_VISIBLE_TITLE_BAR)
return;
1993 if (*ny + rect.bottom >= v_bottom + MIN_VISIBLE_TITLE_BAR)
return;
1996 if (*nx + rect.left + MIN_VISIBLE_TITLE_BAR < v->left) {
1997 if (v->
left < MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2000 if (*nx + rect.right - MIN_VISIBLE_TITLE_BAR > v_right) {
2001 if (v_right > _screen.width - MIN_VISIBLE_TITLE_BAR) *ny = safe_y;
2006 if (px + rect.left < v->
left && v->
left >= MIN_VISIBLE_TITLE_BAR) {
2007 *nx = v->
left - MIN_VISIBLE_TITLE_BAR - rect.left;
2008 }
else if (px + rect.right > v_right && v_right <= _screen.width - MIN_VISIBLE_TITLE_BAR) {
2009 *nx = v_right + MIN_VISIBLE_TITLE_BAR - rect.right;
2027 if (caption != NULL) {
2028 caption_rect.left = caption->
pos_x;
2030 caption_rect.top = caption->
pos_y;
2034 nx =
Clamp(nx, MIN_VISIBLE_TITLE_BAR - caption_rect.right, _screen.width - MIN_VISIBLE_TITLE_BAR - caption_rect.left);
2035 ny =
Clamp(ny, 0, _screen.height - MIN_VISIBLE_TITLE_BAR);
2063 if (delta_x != 0 || delta_y != 0) {
2064 if (clamp_to_screen) {
2067 int new_right = w->
left + w->
width + delta_x;
2068 int new_bottom = w->
top + w->
height + delta_y;
2100 return (w == NULL) ? 0 : w->
top + w->
height;
2111 return (w == NULL) ? _screen.height : w->
top;
2130 FOR_ALL_WINDOWS_FROM_BACK(w) {
2140 int x = _cursor.pos.x + _drag_delta.x;
2141 int y = _cursor.pos.y + _drag_delta.y;
2152 FOR_ALL_WINDOWS_FROM_BACK(v) {
2153 if (v == w)
continue;
2158 if (delta <= hsnap) {
2165 if (delta <= hsnap) {
2174 if (delta <= hsnap) {
2181 if (delta <= hsnap) {
2190 if (delta <= vsnap) {
2197 if (delta <= vsnap) {
2206 if (delta <= vsnap) {
2213 if (delta <= vsnap) {
2236 int x, y = _cursor.pos.y - _drag_delta.y;
2238 x = _drag_delta.x - _cursor.pos.x;
2240 x = _cursor.pos.x - _drag_delta.x;
2248 if (w->
top + w->
height + y > _screen.height) {
2249 y = _screen.height - w->
height - w->
top;
2286 _dragging_window =
false;
2298 _dragging_window =
true;
2300 _drag_delta.x = w->
left - _cursor.pos.x;
2301 _drag_delta.y = w->
top - _cursor.pos.y;
2316 _dragging_window =
true;
2318 _drag_delta.x = _cursor.pos.x;
2319 _drag_delta.y = _cursor.pos.y;
2332 FOR_ALL_WINDOWS_FROM_BACK(w) {
2346 i = _cursor.pos.x - _cursorpos_drag_start.x;
2349 i = _cursor.pos.y - _cursorpos_drag_start.y;
2353 if (_scroller_click_timeout == 1) {
2354 _scroller_click_timeout = 3;
2388 if (_last_scroll_window == NULL) _last_scroll_window =
FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
2393 _last_scroll_window = NULL;
2406 delta.x = -_cursor.
delta.x;
2407 delta.y = -_cursor.
delta.y;
2409 delta.x = _cursor.
delta.x;
2410 delta.y = _cursor.
delta.y;
2413 if (scrollwheel_scrolling) {
2415 delta.x = _cursor.h_wheel;
2416 delta.y = _cursor.v_wheel;
2417 _cursor.v_wheel = 0;
2418 _cursor.h_wheel = 0;
2422 if (delta.x != 0 || delta.y != 0) _last_scroll_window->
OnScroll(delta);
2424 _cursor.
delta.x = 0;
2425 _cursor.
delta.y = 0;
2441 bool bring_to_front =
false;
2451 int w_width = w->
width;
2452 int w_height = w->
height;
2475 if (w->
left + w_width <= u->left ||
2477 w->
top + w_height <= u->top ||
2482 bring_to_front =
true;
2504 switch (query->text.HandleKeyPress(key, keycode)) {
2548 if (query->text.
bytes <= 1) {
2583 if (key >= 0xE000 && key <= 0xF8FF) key = 0;
2588 if (key == 0 && keycode == 0)
return;
2602 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2621 HandleGlobalHotkeys(key, keycode);
2631 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2641 void Window::InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2644 if (query == NULL)
return;
2646 if (query->text.
InsertString(str, marked, caret, insert_location, replacement_end) || marked) {
2658 void HandleTextInput(
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
2683 int x = _cursor.pos.x;
2684 int y = _cursor.pos.y;
2690 if (vp == NULL)
return;
2696 #define scrollspeed 3
2699 }
else if (15 - (vp->
width - x) > 0) {
2704 }
else if (15 - (vp->
height - y) > 0) {
2723 static void ScrollMainViewport(
int x,
int y)
2725 if (_game_mode != GM_MENU) {
2762 static void HandleKeyScrolling()
2770 ScrollMainViewport(scrollamt[
_dirkeys][0] * factor, scrollamt[
_dirkeys][1] * factor);
2774 static void MouseLoop(
MouseClick click,
int mousewheel)
2780 HandlePlacePresize();
2792 if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling)
return;
2794 int x = _cursor.pos.x;
2795 int y = _cursor.pos.y;
2797 if (w == NULL)
return;
2805 if (mousewheel != 0) {
2814 if (scrollwheel_scrolling) click = MC_RIGHT;
2816 case MC_DOUBLE_LEFT:
2819 if (!HandleViewportClicked(vp, x, y) &&
2833 _cursor.h_wheel = 0;
2834 _cursor.v_wheel = 0;
2844 case MC_DOUBLE_LEFT:
2870 static int double_click_time = 0;
2871 static Point double_click_pos = {0, 0};
2880 click = MC_DOUBLE_LEFT;
2883 double_click_pos = _cursor.pos;
2885 _input_events_this_tick++;
2889 _input_events_this_tick++;
2893 if (_cursor.
wheel) {
2894 mousewheel = _cursor.
wheel;
2896 _input_events_this_tick++;
2899 static uint32 hover_time = 0;
2900 static Point hover_pos = {0, 0};
2906 hover_pos = _cursor.pos;
2912 _input_events_this_tick++;
2934 MouseLoop(click, mousewheel);
2939 _cursor.
delta.x = 0;
2940 _cursor.
delta.y = 0;
2951 uint deletable_count = 0;
2952 Window *w, *last_deletable = NULL;
2953 FOR_ALL_WINDOWS_FROM_FRONT(w) {
2963 assert(last_deletable != NULL);
2964 delete last_deletable;
2978 HandleKeyScrolling();
2981 for (
Window *v = _z_front_window; v != NULL; ) {
2991 if (_scroller_click_timeout != 0) _scroller_click_timeout--;
2992 DecreaseWindowCounters();
2994 if (_input_events_this_tick != 0) {
2996 _input_events_this_tick = 0;
3013 static int highlight_timer = 1;
3014 if (--highlight_timer == 0) {
3015 highlight_timer = 15;
3019 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3024 static int we4_timer = 0;
3025 int t = we4_timer + 1;
3028 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3035 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3044 FOR_ALL_WINDOWS_FROM_BACK(w) {
3061 FOR_ALL_WINDOWS_FROM_BACK(w) {
3075 FOR_ALL_WINDOWS_FROM_BACK(w) {
3089 FOR_ALL_WINDOWS_FROM_BACK(w) {
3161 FOR_ALL_WINDOWS_FROM_BACK(w) {
3180 FOR_ALL_WINDOWS_FROM_BACK(w) {
3193 FOR_ALL_WINDOWS_FROM_FRONT(w) {
3212 FOR_ALL_WINDOWS_FROM_BACK(w) {
3221 goto restart_search;
3244 FOR_ALL_WINDOWS_FROM_BACK(w) {
3247 goto restart_search;
3264 FOR_ALL_WINDOWS_FROM_BACK(w) {
3267 goto restart_search;
3271 FOR_ALL_WINDOWS_FROM_BACK(w) w->
SetDirty();
3285 NWidgetScrollbar::InvalidateDimensionCache();
3291 FOR_ALL_WINDOWS_FROM_BACK(w) {
3294 #ifdef ENABLE_NETWORK
3316 if (w == NULL)
return 0;
3318 int old_left = w->
left;
3320 case 1: w->
left = (_screen.width - w->
width) / 2;
break;
3321 case 2: w->
left = _screen.width - w->
width;
break;
3322 default: w->
left = 0;
break;
3336 DEBUG(misc, 5,
"Repositioning Main Toolbar...");
3347 DEBUG(misc, 5,
"Repositioning statusbar...");
3358 DEBUG(misc, 5,
"Repositioning news message...");
3369 DEBUG(misc, 5,
"Repositioning network chat window...");
3382 FOR_ALL_WINDOWS_FROM_BACK(w) {
3400 FOR_ALL_WINDOWS_FROM_BACK(w) {
3442 top = (newh - w->
height) >> 1;
3443 left = (neww - w->
width) >> 1;
3448 if (left + (w->
width >> 1) >= neww) left = neww - w->
width;
3449 if (left < 0) left = 0;
3452 if (top + (w->
height >> 1) >= newh) top = newh - w->
height;
3469 ResetObjectToPlace();