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
| 8669 | // Tip: To have a list filling the entire window width, use size.x = -FLT_MIN and pass an non-visible label e.g. "##empty" |
| 8670 | // 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). |
| 8671 | bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) |
| 8672 | { |
| 8673 | ImGuiContext& g = *GImGui; |
| 8674 | ImGuiWindow* window = GetCurrentWindow(); |
| 8675 | if (window->SkipItems) |
| 8676 | return false; |
| 8677 | |
| 8678 | const ImGuiStyle& style = g.Style; |
| 8679 | const ImGuiID id = GetID(label); |
| 8680 | const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| 8681 | |
| 8682 | // Size default to hold ~7.25 items. |
| 8683 | // Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar. |
| 8684 | ImVec2 size = ImTrunc(CalcItemSize(size_arg, CalcItemWidth(), GetTextLineHeightWithSpacing() * 7.25f + style.FramePadding.y * 2.0f)); |
| 8685 | ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y)); |
| 8686 | ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); |
| 8687 | ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); |
| 8688 | g.NextItemData.ClearFlags(); |
| 8689 | |
| 8690 | if (!IsRectVisible(bb.Min, bb.Max)) |
| 8691 | { |
| 8692 | ItemSize(bb.GetSize(), style.FramePadding.y); |
| 8693 | ItemAdd(bb, 0, &frame_bb); |
| 8694 | g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values |
| 8695 | return false; |
| 8696 | } |
| 8697 | |
| 8698 | // FIXME-OPT: We could omit the BeginGroup() if label_size.x == 0.0f but would need to omit the EndGroup() as well. |
| 8699 | BeginGroup(); |
| 8700 | if (label_size.x > 0.0f) |
| 8701 | { |
| 8702 | ImVec2 label_pos = ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y); |
| 8703 | RenderText(label_pos, label); |
| 8704 | window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, label_pos + label_size); |
| 8705 | AlignTextToFramePadding(); |
| 8706 | } |
| 8707 | |
| 8708 | BeginChild(id, frame_bb.GetSize(), ImGuiChildFlags_FrameStyle); |
| 8709 | return true; |
| 8710 | } |
| 8711 | |
| 8712 | void ImGui::EndListBox() |
| 8713 | { |
nothing calls this directly
no test coverage detected