Combos with different labels behave like one combo in tabbar #6718

Closed
opened 2023-08-14 13:45:52 +00:00 by OKOKDragon · 3 comments
OKOKDragon commented 2023-08-14 13:45:52 +00:00 (Migrated from github.com)

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.
2

Here is a screen shot of the window I create.
1

Then I went through ImGui::BeginCombo() function, and I did not find my answers. Inside ImGui::BeginCombo() function, it calls BeginComboPopup(popup_id, bb, flags) when it returns. So I look into BeginComboPopup() function, and I tried to rewrite it.

bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags flags,
unsigned int custom_id)//I expect this to be the id of a combo
{
    ImGuiContext& g = *GImGui;
    if (!IsPopupOpen(popup_id, ImGuiPopupFlags_None))
    {
        g.NextWindowData.ClearFlags();
        return false;
    }

    // Set popup size
    float w = bb.GetWidth();
    if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint)
    {
        g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w);
    }
    else
    {
        if ((flags & ImGuiComboFlags_HeightMask_) == 0)
            flags |= ImGuiComboFlags_HeightRegular;
        IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_)); // Only one
        int popup_max_height_in_items = -1;
        if (flags & ImGuiComboFlags_HeightRegular)     popup_max_height_in_items = 8;
        else if (flags & ImGuiComboFlags_HeightSmall)  popup_max_height_in_items = 4;
        else if (flags & ImGuiComboFlags_HeightLarge)  popup_max_height_in_items = 20;
        ImVec2 constraint_min(0.0f, 0.0f), constraint_max(FLT_MAX, FLT_MAX);
        if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0 || g.NextWindowData.SizeVal.x <= 0.0f) // Don't apply constraints if user specified a size
            constraint_min.x = w;
        if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0 || g.NextWindowData.SizeVal.y <= 0.0f)
            constraint_max.y = CalcMaxPopupHeightFromItemCount(popup_max_height_in_items);
        SetNextWindowSizeConstraints(constraint_min, constraint_max);
    }

    // This is essentially a specialized version of BeginPopupEx()
    char name[30];

//here is the only line I changed.
    ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%03d", custom_id); // Recycle windows based on depth

    // Set position given a custom constraint (peak into expected window size so we can position it)
    // FIXME: This might be easier to express with an hypothetical SetNextWindowPosConstraints() function?
    // FIXME: This might be moved to Begin() or at least around the same spot where Tooltips and other Popups are calling FindBestWindowPosForPopupEx()?
    if (ImGuiWindow* popup_window = FindWindowByName(name))
        if (popup_window->WasActive)
        {
            // Always override 'AutoPosLastDirection' to not leave a chance for a past value to affect us.
            ImVec2 size_expected = CalcWindowNextAutoFitSize(popup_window);
            popup_window->AutoPosLastDirection = (flags & ImGuiComboFlags_PopupAlignLeft) ? ImGuiDir_Left : ImGuiDir_Down; // Left = "Below, Toward Left", Down = "Below, Toward Right (default)"
            ImRect r_outer = GetPopupAllowedExtentRect(popup_window);
            ImVec2 pos = FindBestWindowPosForPopupEx(bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, bb, ImGuiPopupPositionPolicy_ComboBox);
            SetNextWindowPos(pos);
        }

    // We don't use BeginPopupEx() solely because we have a custom name string, which we could make an argument to BeginPopupEx()
    ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove;
    PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(g.Style.FramePadding.x, g.Style.WindowPadding.y)); // Horizontally align ourselves with the framed text
    bool ret = Begin(name, NULL, window_flags);
    PopStyleVar();
    if (!ret)
    {
        EndPopup();
        IM_ASSERT(0);   // This should never happen as we tested for IsPopupOpen() above
        return false;
    }
    return true;
}

Well, now I do have different combos.Here is a screenshot of the debugger window.
3
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.

//in main function
        if (show_window)
        {
            ImGuiWindowFlags window_flags = 0;
            ImGui::Begin(u8"main window", &show_window, window_flags); 
            OKps_ImGui::set_min_size(450.0f, 300.0f, window_flags);

            static ImVector<int> active_tabs;
            static int next_tab_id = 0;
            if (next_tab_id == 0) 
                for (int i = 0; i < 1; i++)
                    active_tabs.push_back(next_tab_id++);

            static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown| ImGuiTabBarFlags_TabListPopupButton| ImGuiTabBarFlags_FittingPolicyScroll;

                tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);

            if (ImGui::BeginTabBar("main_tabbar", tab_bar_flags))
            {

                    if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip))
                        active_tabs.push_back(next_tab_id++); 

                for (int n = 0; n < active_tabs.Size; )
                {
                    bool open = true;
                    char name[30];
                    snprintf(name, IM_ARRAYSIZE(name), "file%d", active_tabs[n]);
                    if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None))
                    {

                        static ImGuiComboFlags flags = 0;
                      
                        const char* items[] = { "mode1", "mode2"};
                        static int item_current_idx = 0; // Here we store our selection data as an index.
                        const char* combo_preview_value = items[item_current_idx];  

//combo is labeled here!
                    char combo_label[30];
                    snprintf(combo_label, IM_ARRAYSIZE(combo_label), "###%d", n);
//now, combo in tab n is labeled as "###n"
                        if (ImGui::BeginCombo(combo_label, combo_preview_value, flags))
                        {
                            for (int n1 = 0; n1 < IM_ARRAYSIZE(items); n1++)
                            {
                                const bool is_selected = (item_current_idx == n1);
                                if (ImGui::Selectable(items[n1], is_selected))
                                    item_current_idx = n1;

                                if (is_selected)
                                    ImGui::SetItemDefaultFocus();
                            }

                            ImGui::EndCombo();
                        }
                        free(combo_id);
                        free(combo_label);
                        if (item_current_idx == 0)
                            //mode1
                        {
                            ImGui::Text(u8"mode1");
//and other codes to deal with files
                        }
                        else
                            //mode2
                        {
                            ImGui::Text(u8"mode2");
//and other codes to deal with files
                        }

                        ImGui::EndTabItem();
                    }

                    if (!open)
                        active_tabs.erase(active_tabs.Data + n);
                    else
                        n++;
                }

                ImGui::EndTabBar();
            }
            

            ImGui::End();
        }

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.

        //in main function
        if (show_aes_window)
        {
            ImGuiWindowFlags window_flags = 0;
            ImGui::Begin(u8"aes加密", &show_aes_window, window_flags);   
            OKps_ImGui::set_min_size(450.0f, 300.0f, window_flags);

            static ImVector<int> active_tabs;
            static ImVector<int> current_mode;
            static int next_tab_id = 1;
            if (next_tab_id == 1) // Initialize with some default tabs
            {
                active_tabs.push_back(next_tab_id++);
                current_mode.push_back(0);
            }
                    
            ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown| ImGuiTabBarFlags_TabListPopupButton| ImGuiTabBarFlags_FittingPolicyScroll;

                tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);
                
            if (ImGui::BeginTabBar("aes_window_tabbar", tab_bar_flags))
            {
                if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip))
                {
                    active_tabs.push_back(next_tab_id++);
                    current_mode.push_back(0);
                } 

                // Submit our regular tabs
                for (int n = 0; n < active_tabs.Size; )
                {
                    bool open = true;
                    char name[40];
                    snprintf(name, IM_ARRAYSIZE(name), u8"文件%d", active_tabs[n]);
                    if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None))
                    {

                        ImGuiComboFlags flags = 0;
                      
                        const char* modes[] = { u8"加密", u8"解密"};

                        const char* combo_preview_value = modes[current_mode[n]];  
                        char* combo_id = global::int_string(n);
                        char* combo_label = (char*)global::secure_malloc((strlen(combo_id) + 4) * sizeof(char));
                        strcpy(combo_label, "###");

                        strcat(combo_label, combo_id);

                        if (ImGui::BeginCombo(combo_label, combo_preview_value, flags))
                        {
                            for (int n1 = 0; n1 < IM_ARRAYSIZE(modes); n1++)
                            {
                                const bool is_selected = (current_mode[n] == n1);
                                if (ImGui::Selectable(modes[n1], is_selected))
                                    current_mode[n] = n1;

                           
                                if (is_selected)
                                    ImGui::SetItemDefaultFocus();
                            }

                            ImGui::EndCombo();
                        }
                        free(combo_id);
                        free(combo_label);
                        if (current_mode[n] == 0)
                      
                        {
                            ImGui::Text(u8"加密模式");
                        }
                        else
                        
                        {
                            ImGui::Text(u8"解密模式");
                        }


                        ImGui::EndTabItem();
                    }

                    if (!open)
                    {
                        active_tabs.erase(active_tabs.Data + n);
                        current_mode.erase(current_mode.Data + n);
                    }
                        
                    else
                        n++;
                }

                ImGui::EndTabBar();
            }
            

            ImGui::End();
        }
**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. ![2](https://github.com/ocornut/imgui/assets/141321330/1bed37e9-3d47-416d-809a-6b1396c2637c) Here is a screen shot of the window I create. ![1](https://github.com/ocornut/imgui/assets/141321330/65f4763a-9ef9-41a7-a8c6-f1d403fe2e6c) Then I went through `ImGui::BeginCombo()` function, and I did not find my answers. Inside `ImGui::BeginCombo()` function, it calls `BeginComboPopup(popup_id, bb, flags)` when it returns. So I look into `BeginComboPopup()` function, and I tried to rewrite it. ```c++ bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags flags, unsigned int custom_id)//I expect this to be the id of a combo { ImGuiContext& g = *GImGui; if (!IsPopupOpen(popup_id, ImGuiPopupFlags_None)) { g.NextWindowData.ClearFlags(); return false; } // Set popup size float w = bb.GetWidth(); if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) { g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w); } else { if ((flags & ImGuiComboFlags_HeightMask_) == 0) flags |= ImGuiComboFlags_HeightRegular; IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_)); // Only one int popup_max_height_in_items = -1; if (flags & ImGuiComboFlags_HeightRegular) popup_max_height_in_items = 8; else if (flags & ImGuiComboFlags_HeightSmall) popup_max_height_in_items = 4; else if (flags & ImGuiComboFlags_HeightLarge) popup_max_height_in_items = 20; ImVec2 constraint_min(0.0f, 0.0f), constraint_max(FLT_MAX, FLT_MAX); if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0 || g.NextWindowData.SizeVal.x <= 0.0f) // Don't apply constraints if user specified a size constraint_min.x = w; if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0 || g.NextWindowData.SizeVal.y <= 0.0f) constraint_max.y = CalcMaxPopupHeightFromItemCount(popup_max_height_in_items); SetNextWindowSizeConstraints(constraint_min, constraint_max); } // This is essentially a specialized version of BeginPopupEx() char name[30]; //here is the only line I changed. ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%03d", custom_id); // Recycle windows based on depth // Set position given a custom constraint (peak into expected window size so we can position it) // FIXME: This might be easier to express with an hypothetical SetNextWindowPosConstraints() function? // FIXME: This might be moved to Begin() or at least around the same spot where Tooltips and other Popups are calling FindBestWindowPosForPopupEx()? if (ImGuiWindow* popup_window = FindWindowByName(name)) if (popup_window->WasActive) { // Always override 'AutoPosLastDirection' to not leave a chance for a past value to affect us. ImVec2 size_expected = CalcWindowNextAutoFitSize(popup_window); popup_window->AutoPosLastDirection = (flags & ImGuiComboFlags_PopupAlignLeft) ? ImGuiDir_Left : ImGuiDir_Down; // Left = "Below, Toward Left", Down = "Below, Toward Right (default)" ImRect r_outer = GetPopupAllowedExtentRect(popup_window); ImVec2 pos = FindBestWindowPosForPopupEx(bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, bb, ImGuiPopupPositionPolicy_ComboBox); SetNextWindowPos(pos); } // We don't use BeginPopupEx() solely because we have a custom name string, which we could make an argument to BeginPopupEx() ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove; PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(g.Style.FramePadding.x, g.Style.WindowPadding.y)); // Horizontally align ourselves with the framed text bool ret = Begin(name, NULL, window_flags); PopStyleVar(); if (!ret) { EndPopup(); IM_ASSERT(0); // This should never happen as we tested for IsPopupOpen() above return false; } return true; } ``` Well, now I do have different combos.Here is a screenshot of the debugger window. ![3](https://github.com/ocornut/imgui/assets/141321330/bee4a2c6-5bde-489b-8329-23ce0b0ce516) 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. ```c++ //in main function if (show_window) { ImGuiWindowFlags window_flags = 0; ImGui::Begin(u8"main window", &show_window, window_flags); OKps_ImGui::set_min_size(450.0f, 300.0f, window_flags); static ImVector<int> active_tabs; static int next_tab_id = 0; if (next_tab_id == 0) for (int i = 0; i < 1; i++) active_tabs.push_back(next_tab_id++); static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown| ImGuiTabBarFlags_TabListPopupButton| ImGuiTabBarFlags_FittingPolicyScroll; tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); if (ImGui::BeginTabBar("main_tabbar", tab_bar_flags)) { if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip)) active_tabs.push_back(next_tab_id++); for (int n = 0; n < active_tabs.Size; ) { bool open = true; char name[30]; snprintf(name, IM_ARRAYSIZE(name), "file%d", active_tabs[n]); if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None)) { static ImGuiComboFlags flags = 0; const char* items[] = { "mode1", "mode2"}; static int item_current_idx = 0; // Here we store our selection data as an index. const char* combo_preview_value = items[item_current_idx]; //combo is labeled here! char combo_label[30]; snprintf(combo_label, IM_ARRAYSIZE(combo_label), "###%d", n); //now, combo in tab n is labeled as "###n" if (ImGui::BeginCombo(combo_label, combo_preview_value, flags)) { for (int n1 = 0; n1 < IM_ARRAYSIZE(items); n1++) { const bool is_selected = (item_current_idx == n1); if (ImGui::Selectable(items[n1], is_selected)) item_current_idx = n1; if (is_selected) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } free(combo_id); free(combo_label); if (item_current_idx == 0) //mode1 { ImGui::Text(u8"mode1"); //and other codes to deal with files } else //mode2 { ImGui::Text(u8"mode2"); //and other codes to deal with files } ImGui::EndTabItem(); } if (!open) active_tabs.erase(active_tabs.Data + n); else n++; } ImGui::EndTabBar(); } ImGui::End(); } ``` 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. ```c++ //in main function if (show_aes_window) { ImGuiWindowFlags window_flags = 0; ImGui::Begin(u8"aes加密", &show_aes_window, window_flags); OKps_ImGui::set_min_size(450.0f, 300.0f, window_flags); static ImVector<int> active_tabs; static ImVector<int> current_mode; static int next_tab_id = 1; if (next_tab_id == 1) // Initialize with some default tabs { active_tabs.push_back(next_tab_id++); current_mode.push_back(0); } ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown| ImGuiTabBarFlags_TabListPopupButton| ImGuiTabBarFlags_FittingPolicyScroll; tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); if (ImGui::BeginTabBar("aes_window_tabbar", tab_bar_flags)) { if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip)) { active_tabs.push_back(next_tab_id++); current_mode.push_back(0); } // Submit our regular tabs for (int n = 0; n < active_tabs.Size; ) { bool open = true; char name[40]; snprintf(name, IM_ARRAYSIZE(name), u8"文件%d", active_tabs[n]); if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None)) { ImGuiComboFlags flags = 0; const char* modes[] = { u8"加密", u8"解密"}; const char* combo_preview_value = modes[current_mode[n]]; char* combo_id = global::int_string(n); char* combo_label = (char*)global::secure_malloc((strlen(combo_id) + 4) * sizeof(char)); strcpy(combo_label, "###"); strcat(combo_label, combo_id); if (ImGui::BeginCombo(combo_label, combo_preview_value, flags)) { for (int n1 = 0; n1 < IM_ARRAYSIZE(modes); n1++) { const bool is_selected = (current_mode[n] == n1); if (ImGui::Selectable(modes[n1], is_selected)) current_mode[n] = n1; if (is_selected) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } free(combo_id); free(combo_label); if (current_mode[n] == 0) { ImGui::Text(u8"加密模式"); } else { ImGui::Text(u8"解密模式"); } ImGui::EndTabItem(); } if (!open) { active_tabs.erase(active_tabs.Data + n); current_mode.erase(current_mode.Data + n); } else n++; } ImGui::EndTabBar(); } ImGui::End(); } ```
GamingMinds-DanielC commented 2023-08-14 14:20:52 +00:00 (Migrated from github.com)

This is your issue:
static int item_current_idx = 0;

Not a problem with ImGui, but your use of static variables. The static makes 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

This is your issue: `static int item_current_idx = 0;` Not a problem with ImGui, but your use of static variables. The `static` makes 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
ocornut commented 2023-08-14 14:42:30 +00:00 (Migrated from github.com)

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 static in 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

// Message to beginner C/C++ programmers about the meaning of the 'static' keyword:
// In this demo code, we frequently use 'static' variables inside functions. A static variable persists across calls,
// so it is essentially like a global variable but declared inside the scope of the function. We do this as a way to
// gather code and data in the same place, to make the demo source code faster to read, faster to write, and smaller
// in size. It also happens to be a convenient way of storing simple UI related information as long as your function
// doesn't need to be reentrant or used in multiple threads. This might be a pattern you will want to use in your code,
// but most of the real data you would be editing is likely going to be stored outside your functions.

// The Demo code in this file is designed to be easy to copy-and-paste into your application!
// Because of this:
// - We never omit the ImGui:: prefix when calling functions, even though most code here is in the same namespace.
// - We try to declare static variables in the local scope, as close as possible to the code using them.
// - We never use any of the helpers/facilities used internally by Dear ImGui, unless available in the public API.
// - We never use maths operators on ImVec2/ImVec4. For our other sources files we use them, and they are provided
//   by imgui.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional
//   and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h.
//   Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp.

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 static confusion.

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 `static` in 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 ```cpp // Message to beginner C/C++ programmers about the meaning of the 'static' keyword: // In this demo code, we frequently use 'static' variables inside functions. A static variable persists across calls, // so it is essentially like a global variable but declared inside the scope of the function. We do this as a way to // gather code and data in the same place, to make the demo source code faster to read, faster to write, and smaller // in size. It also happens to be a convenient way of storing simple UI related information as long as your function // doesn't need to be reentrant or used in multiple threads. This might be a pattern you will want to use in your code, // but most of the real data you would be editing is likely going to be stored outside your functions. // The Demo code in this file is designed to be easy to copy-and-paste into your application! // Because of this: // - We never omit the ImGui:: prefix when calling functions, even though most code here is in the same namespace. // - We try to declare static variables in the local scope, as close as possible to the code using them. // - We never use any of the helpers/facilities used internally by Dear ImGui, unless available in the public API. // - We never use maths operators on ImVec2/ImVec4. For our other sources files we use them, and they are provided // by imgui.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional // and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. // Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp. ``` 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 `static` confusion.
ocornut commented 2023-08-14 14:45:04 +00:00 (Migrated from github.com)

Pasted wrong comments in earlier message, should be:


// Message to beginner C/C++ programmers about the meaning of the 'static' keyword:
// In this demo code, we frequently use 'static' variables inside functions. A static variable persists across calls,
// so it is essentially like a global variable but declared inside the scope of the function. We do this as a way to
// gather code and data in the same place, to make the demo source code faster to read, faster to write, and smaller
// in size. It also happens to be a convenient way of storing simple UI related information as long as your function
// doesn't need to be reentrant or used in multiple threads. This might be a pattern you will want to use in your code,
// but most of the real data you would be editing is likely going to be stored outside your functions.

It crossed my mind to use custom keyword e.g. #define shared_global static or #define global static to 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).

Pasted wrong comments in earlier message, should be: ```cpp // Message to beginner C/C++ programmers about the meaning of the 'static' keyword: // In this demo code, we frequently use 'static' variables inside functions. A static variable persists across calls, // so it is essentially like a global variable but declared inside the scope of the function. We do this as a way to // gather code and data in the same place, to make the demo source code faster to read, faster to write, and smaller // in size. It also happens to be a convenient way of storing simple UI related information as long as your function // doesn't need to be reentrant or used in multiple threads. This might be a pattern you will want to use in your code, // but most of the real data you would be editing is likely going to be stored outside your functions. ``` It crossed my mind to use custom keyword e.g. `#define shared_global static` or `#define global static` to 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).
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#6718
No description provided.