This is essentially a thin wrapper to using BeginChild/EndChild with the ImGuiChildFlags_FrameStyle flag for stylistic changes + displaying a label. This handle some subtleties with capturing info from the label. If you don't need a label you can pretty much directly use ImGui::BeginChild() with ImGuiChildFlags_FrameStyle. Tip: To have a list filling the entire window width, use size.x = -FLT_MIN
| 8565 | // Tip: To have a list filling the entire window width, use size.x = -FLT_MIN and pass an non-visible label e.g. "##empty" |
| 8566 | // Tip: If your vertical size is calculated from an item count (e.g. 10 * item_height) consider adding a fractional part to facilitate seeing scrolling boundaries (e.g. 10.5f * item_height). |
| 8567 | bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) |
| 8568 | { |
| 8569 | ImGuiContext& g = *GImGui; |
| 8570 | ImGuiWindow* window = GetCurrentWindow(); |
| 8571 | if (window->SkipItems) |
| 8572 | return false; |
| 8573 | |
| 8574 | const ImGuiStyle& style = g.Style; |
| 8575 | const ImGuiID id = GetID(label); |
| 8576 | const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| 8577 | |
| 8578 | // Size default to hold ~7.25 items. |
| 8579 | // Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar. |
| 8580 | ImVec2 size = ImTrunc(CalcItemSize(size_arg, CalcItemWidth(), GetTextLineHeightWithSpacing() * 7.25f + style.FramePadding.y * 2.0f)); |
| 8581 | ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y)); |
| 8582 | ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); |
| 8583 | ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); |
| 8584 | g.NextItemData.ClearFlags(); |
| 8585 | |
| 8586 | if (!IsRectVisible(bb.Min, bb.Max)) |
| 8587 | { |
| 8588 | ItemSize(bb.GetSize(), style.FramePadding.y); |
| 8589 | ItemAdd(bb, 0, &frame_bb); |
| 8590 | g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values |
| 8591 | return false; |
| 8592 | } |
| 8593 | |
| 8594 | // FIXME-OPT: We could omit the BeginGroup() if label_size.x == 0.0f but would need to omit the EndGroup() as well. |
| 8595 | BeginGroup(); |
| 8596 | if (label_size.x > 0.0f) |
| 8597 | { |
| 8598 | ImVec2 label_pos = ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y); |
| 8599 | RenderText(label_pos, label); |
| 8600 | window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, label_pos + label_size); |
| 8601 | AlignTextToFramePadding(); |
| 8602 | } |
| 8603 | |
| 8604 | BeginChild(id, frame_bb.GetSize(), ImGuiChildFlags_FrameStyle); |
| 8605 | return true; |
| 8606 | } |
| 8607 | |
| 8608 | void ImGui::EndListBox() |
| 8609 | { |
nothing calls this directly
no test coverage detected