mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-16 19:11:13 +00:00
Combos with different labels behave like one combo in tabbar #6718
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#6718
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Version/Branch of Dear ImGui:
Version: 1.89.8
Branch: docking
Back-end/Renderer/Compiler/OS
Back-ends: imgui_impl_dx12.cpp + imgui_impl_win32.cpp
Compiler: MSVC
Operating System: windows 11 22H2
Before you go on:
There are Chinese in my code, and I replace them with some meaningless words, to make it easy for anyone to copy and run. But I do not use Chinese in labels of widgets, so that should not be the problem. Screenshots still have those Chinese.
My Issue/Question:
I create a tabbar in a window, each tab shows a different file. And I want each tab to have a combo, which decides the edit mode of the file. Call them mode1 and mode2.
I am aware that widgets with same name can be labeled with "###", but this does not work.
I label combos in tab n as "###n". So the combo in the first tab is "###0", and "###1" in the next tab, etc. Now each combo has a different label, so I expect them to be independent. But no, they behave like they are the same combo.If you choose mode1 in the combo in tab3, then tab0 , tab1 and tab2 are all set to mode1.
I open the debugger window of the ImGui Demo Window, and it tells that no matter how many combos I create, there is only one labeled as "##Combo_00". Then what happened to the labels I give to ImGui?
Here is a screenshot of the debugger window.

Here is a screen shot of the window I create.

Then I went through
ImGui::BeginCombo()function, and I did not find my answers. InsideImGui::BeginCombo()function, it callsBeginComboPopup(popup_id, bb, flags)when it returns. So I look intoBeginComboPopup()function, and I tried to rewrite it.Well, now I do have different combos.Here is a screenshot of the debugger window.

4 tabs, 4 combos. But no, they are still one same combo! Whatever the user do to one of these combos, the other combos all behave like they are changed too. If you choose mode1 in tab3, the combos in tab0, tab1 and tab2 are all set to mode1!
I truly hope someone can help me with this.
Here is the code of drawing the window, in main function. They are copied from imgui_demo.cpp, and I think there should not be major problems.
Here is a video.
https://github.com/ocornut/imgui/assets/141321330/6cc9a577-1e9f-496c-8f65-3b54c027d4fe
Solution
Great thanks to omar and Daniel Cremers for helping me. The correct code is pasted below.
This is your issue:
static int item_current_idx = 0;Not a problem with ImGui, but your use of static variables. The
staticmakes the integer into basically a global variable that is known only in the scope it is defined in. But all tabs use the same scope and therefore share the same variable. If you want a different one per tab, try a static array with enough entries and index it with the current tab.Edit: you have a lot of variables unnecessarily declared
static. So that looks less like a one time accident and more like a basic misunderstanding of how that modifier works. Best to familiarize yourself with it:https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables
Closing as kindly answered by @GamingMinds-DanielC , thank you.
In Dear ImGui by design your app code owns the meaningful state, in your case you have shared this state between all combos so all combos reflect the same state.
I admit it may be odd and misleading how we use
staticin the Demo, there is a paragraph at the top of imgui_demo.cpp to explain why we do that:https://github.com/ocornut/imgui/blob/master/imgui_demo.cpp#L29
I cannot think of a better solution unfortunately.
If the demo was designed to carry data in neat little data structures, the demo would be both twice longer, and much harder to learn things from. So it is mostly a case for people getting past third
staticconfusion.Pasted wrong comments in earlier message, should be:
It crossed my mind to use custom keyword e.g.
#define shared_global staticor#define global staticto better convey the meaning of static (especially because static has different meaning for "local" variables and function), but I really worry about our demo looking like a weird language involving preprocessing).