Codechange: Use window parent association for dropdowns.

This replaces the separate window class and number properties, and
allows the window system to close dropdowns automatically.
This commit is contained in:
Peter Nelson
2023-06-02 14:27:06 +01:00
committed by PeterN
parent dec7ff6b0c
commit 2511649938
8 changed files with 22 additions and 39 deletions

View File

@@ -117,8 +117,6 @@ static WindowDesc _dropdown_desc(
/** Drop-down menu window */
struct DropdownWindow : Window {
WindowClass parent_wnd_class; ///< Parent window class.
WindowNumber parent_wnd_num; ///< Parent window number.
int parent_button; ///< Parent widget number where the window is dropped from.
const DropDownList list; ///< List with dropdown menu items.
int selected_index; ///< Index of the selected item in the list.
@@ -175,8 +173,7 @@ struct DropdownWindow : Window {
this->vscroll->SetCapacity(size.height * this->list.size() / list_height);
this->vscroll->SetCount(this->list.size());
this->parent_wnd_class = parent->window_class;
this->parent_wnd_num = parent->window_number;
this->parent = parent;
this->parent_button = button;
this->selected_index = selected;
this->click_delay = 0;
@@ -190,13 +187,10 @@ struct DropdownWindow : Window {
* Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
this->Window::Close();
Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
if (w2 != nullptr) {
Point pt = _cursor.pos;
pt.x -= w2->left;
pt.y -= w2->top;
w2->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close);
}
Point pt = _cursor.pos;
pt.x -= this->parent->left;
pt.y -= this->parent->top;
this->parent->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close);
}
void OnFocusLost() override
@@ -292,17 +286,11 @@ struct DropdownWindow : Window {
void OnMouseLoop() override
{
Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
if (w2 == nullptr) {
this->Close();
return;
}
if (this->click_delay != 0 && --this->click_delay == 0) {
/* Close the dropdown, so it doesn't affect new window placement.
* Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
this->Close();
w2->OnDropdownSelect(this->parent_button, this->selected_index);
this->parent->OnDropdownSelect(this->parent_button, this->selected_index);
return;
}
@@ -495,16 +483,14 @@ void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int butt
int HideDropDownMenu(Window *pw)
{
for (Window *w : Window::Iterate()) {
if (w->parent != pw) continue;
if (w->window_class != WC_DROPDOWN_MENU) continue;
DropdownWindow *dw = dynamic_cast<DropdownWindow*>(w);
assert(dw != nullptr);
if (pw->window_class == dw->parent_wnd_class &&
pw->window_number == dw->parent_wnd_num) {
int parent_button = dw->parent_button;
dw->Close();
return parent_button;
}
int parent_button = dw->parent_button;
dw->Close();
return parent_button;
}
return -1;