Old API, prefer using BeginCombo() nowadays if you can.
| 2080 | |
| 2081 | // Old API, prefer using BeginCombo() nowadays if you can. |
| 2082 | bool ImGui::Combo(const char* label, int* current_item, const char* (*getter)(void* user_data, int idx), void* user_data, int items_count, int popup_max_height_in_items) |
| 2083 | { |
| 2084 | ImGuiContext& g = *GImGui; |
| 2085 | |
| 2086 | // Call the getter to obtain the preview string which is a parameter to BeginCombo() |
| 2087 | const char* preview_value = NULL; |
| 2088 | if (*current_item >= 0 && *current_item < items_count) |
| 2089 | preview_value = getter(user_data, *current_item); |
| 2090 | |
| 2091 | // The old Combo() API exposed "popup_max_height_in_items". The new more general BeginCombo() API doesn't have/need it, but we emulate it here. |
| 2092 | if (popup_max_height_in_items != -1 && !(g.NextWindowData.HasFlags & ImGuiNextWindowDataFlags_HasSizeConstraint)) |
| 2093 | SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); |
| 2094 | |
| 2095 | if (!BeginCombo(label, preview_value, ImGuiComboFlags_None)) |
| 2096 | return false; |
| 2097 | |
| 2098 | // Display items |
| 2099 | bool value_changed = false; |
| 2100 | ImGuiListClipper clipper; |
| 2101 | clipper.Begin(items_count); |
| 2102 | clipper.IncludeItemByIndex(*current_item); |
| 2103 | while (clipper.Step()) |
| 2104 | for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) |
| 2105 | { |
| 2106 | const char* item_text = getter(user_data, i); |
| 2107 | if (item_text == NULL) |
| 2108 | item_text = "*Unknown item*"; |
| 2109 | |
| 2110 | PushID(i); |
| 2111 | const bool item_selected = (i == *current_item); |
| 2112 | if (Selectable(item_text, item_selected) && *current_item != i) |
| 2113 | { |
| 2114 | value_changed = true; |
| 2115 | *current_item = i; |
| 2116 | } |
| 2117 | if (item_selected) |
| 2118 | SetItemDefaultFocus(); |
| 2119 | PopID(); |
| 2120 | } |
| 2121 | |
| 2122 | EndCombo(); |
| 2123 | if (value_changed) |
| 2124 | MarkItemEdited(g.LastItemData.ID); |
| 2125 | |
| 2126 | return value_changed; |
| 2127 | } |
| 2128 | |
| 2129 | // Combo box helper allowing to pass an array of strings. |
| 2130 | bool ImGui::Combo(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items) |
nothing calls this directly
no test coverage detected