1
0
Fork 0

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.
pull/10915/head
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

@ -276,7 +276,7 @@ struct GSConfigWindow : public Window {
int num = it - this->visible_settings.begin(); int num = it - this->visible_settings.begin();
if (this->clicked_row != num) { if (this->clicked_row != num) {
this->CloseChildWindows(WC_QUERY_STRING); this->CloseChildWindows(WC_QUERY_STRING);
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->clicked_row = num; this->clicked_row = num;
this->clicked_dropdown = false; this->clicked_dropdown = false;
} }
@ -292,7 +292,7 @@ struct GSConfigWindow : public Window {
if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) {
if (this->clicked_dropdown) { if (this->clicked_dropdown) {
/* unclick the dropdown */ /* unclick the dropdown */
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->clicked_dropdown = false; this->clicked_dropdown = false;
this->closing_dropdown = false; this->closing_dropdown = false;
} else { } else {
@ -408,7 +408,7 @@ struct GSConfigWindow : public Window {
this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, !GameConfig::GetConfig()->GetTextfile(tft, (CompanyID)OWNER_DEITY).has_value()); this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, !GameConfig::GetConfig()->GetTextfile(tft, (CompanyID)OWNER_DEITY).has_value());
} }
this->RebuildVisibleSettings(); this->RebuildVisibleSettings();
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->CloseChildWindows(WC_QUERY_STRING); this->CloseChildWindows(WC_QUERY_STRING);
} }
private: private:

View File

@ -461,7 +461,7 @@ public:
if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) { if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
this->vli.index = ALL_GROUP; this->vli.index = ALL_GROUP;
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
} }
this->SetDirty(); this->SetDirty();
} }
@ -512,7 +512,7 @@ public:
/* The drop down menu is out, *but* it may not be used, retract it. */ /* The drop down menu is out, *but* it may not be used, retract it. */
if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) { if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN); this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
} }
/* Disable all lists management button when the list is empty */ /* Disable all lists management button when the list is empty */

View File

@ -363,7 +363,7 @@ struct NewGRFParametersWindow : public Window {
uint num = it - this->grf_config->param_info.begin(); uint num = it - this->grf_config->param_info.begin();
if (this->clicked_row != num) { if (this->clicked_row != num) {
this->CloseChildWindows(WC_QUERY_STRING); this->CloseChildWindows(WC_QUERY_STRING);
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->clicked_row = num; this->clicked_row = num;
this->clicked_dropdown = false; this->clicked_dropdown = false;
} }
@ -379,7 +379,7 @@ struct NewGRFParametersWindow : public Window {
if (par_info.type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info.complete_labels) { if (par_info.type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info.complete_labels) {
if (this->clicked_dropdown) { if (this->clicked_dropdown) {
/* unclick the dropdown */ /* unclick the dropdown */
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->clicked_dropdown = false; this->clicked_dropdown = false;
this->closing_dropdown = false; this->closing_dropdown = false;
} else { } else {

View File

@ -854,7 +854,6 @@ public:
if (this->selected_order == -1) break; if (this->selected_order == -1) break;
this->CloseChildWindows(); this->CloseChildWindows();
HideDropDownMenu(this);
this->selected_order = -1; this->selected_order = -1;
break; break;
@ -886,7 +885,6 @@ public:
if (to == INVALID_VEH_ORDER_ID) { if (to == INVALID_VEH_ORDER_ID) {
/* Deleting selected order */ /* Deleting selected order */
this->CloseChildWindows(); this->CloseChildWindows();
HideDropDownMenu(this);
this->selected_order = -1; this->selected_order = -1;
break; break;
} }
@ -1186,7 +1184,6 @@ public:
/* This order won't be selected any more, close all child windows and dropdowns */ /* This order won't be selected any more, close all child windows and dropdowns */
this->CloseChildWindows(); this->CloseChildWindows();
HideDropDownMenu(this);
if (sel == INVALID_VEH_ORDER_ID || this->vehicle->owner != _local_company) { if (sel == INVALID_VEH_ORDER_ID || this->vehicle->owner != _local_company) {
/* Deselect clicked order */ /* Deselect clicked order */

View File

@ -434,7 +434,7 @@ struct ScriptSettingsWindow : public Window {
int num = it - this->visible_settings.begin(); int num = it - this->visible_settings.begin();
if (this->clicked_row != num) { if (this->clicked_row != num) {
this->CloseChildWindows(WC_QUERY_STRING); this->CloseChildWindows(WC_QUERY_STRING);
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->clicked_row = num; this->clicked_row = num;
this->clicked_dropdown = false; this->clicked_dropdown = false;
} }
@ -450,7 +450,7 @@ struct ScriptSettingsWindow : public Window {
if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) {
if (this->clicked_dropdown) { if (this->clicked_dropdown) {
/* unclick the dropdown */ /* unclick the dropdown */
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->clicked_dropdown = false; this->clicked_dropdown = false;
this->closing_dropdown = false; this->closing_dropdown = false;
} else { } else {
@ -560,7 +560,7 @@ struct ScriptSettingsWindow : public Window {
void OnInvalidateData(int data = 0, bool gui_scope = true) override void OnInvalidateData(int data = 0, bool gui_scope = true) override
{ {
this->RebuildVisibleSettings(); this->RebuildVisibleSettings();
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->CloseChildWindows(WC_QUERY_STRING); this->CloseChildWindows(WC_QUERY_STRING);
} }

View File

@ -2349,7 +2349,7 @@ struct GameSettingsWindow : Window {
if (this->valuedropdown_entry == pe) { if (this->valuedropdown_entry == pe) {
/* unclick the dropdown */ /* unclick the dropdown */
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
this->closing_dropdown = false; this->closing_dropdown = false;
this->valuedropdown_entry->SetButtons(0); this->valuedropdown_entry->SetButtons(0);
this->valuedropdown_entry = nullptr; this->valuedropdown_entry = nullptr;

View File

@ -1966,7 +1966,7 @@ public:
this->SortVehicleList(); this->SortVehicleList();
if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_VL_MANAGE_VEHICLES_DROPDOWN)) { if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_VL_MANAGE_VEHICLES_DROPDOWN)) {
HideDropDownMenu(this); this->CloseChildWindows(WC_DROPDOWN_MENU);
} }
/* Hide the widgets that we will not use in this window /* Hide the widgets that we will not use in this window

View File

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