Skip to content

Commit

Permalink
MenuItem() draft for popups, with Selected option (wip #126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed May 7, 2015
1 parent c36172e commit f46557d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
85 changes: 69 additions & 16 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7220,6 +7220,41 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v
return value_changed;
}

bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected)
{
(void)shortcut; // FIXME-MENU: Shortcut are not supported yet. Argument is reserved.

ImGuiState& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;

ImVec2 pos = ImGui::GetCursorScreenPos();
const ImVec2 label_size = CalcTextSize(label, NULL, true);
const float symbol_spacing = (float)(int)(g.FontSize * 1.50f + 0.5f);
const float w = ImMax(label_size.x + symbol_spacing, window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x); // Feedback to next frame
bool ret = ImGui::Selectable(label, false, ImVec2(w, 0.0f));

if (selected)
{
pos.x = window->Pos.x + ImGui::GetContentRegionMax().x - g.FontSize;
RenderCheckMark(pos, window->Color(ImGuiCol_Text));
}

return ret;
}

bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected)
{
if (ImGui::MenuItem(label, shortcut, p_selected ? *p_selected : false))
{
if (p_selected)
*p_selected = !*p_selected;
return true;
}
return false;
}

// A little colored square. Return true when clicked.
bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_border)
{
Expand Down Expand Up @@ -10007,30 +10042,48 @@ void ImGui::ShowTestWindow(bool* opened)
ImGui::TreePop();
}

if (ImGui::TreeNode("Popup"))
if (ImGui::TreeNode("Popup, Menus"))
{
static bool popup_open = false;
static int selected_fish = -1;
const char* fishes[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" };
if (ImGui::Button("Select.."))
popup_open = true;
ImGui::SameLine();
ImGui::Text(selected_fish == -1 ? "<None>" : fishes[selected_fish]);
if (popup_open)
const char* names[] = { "BreamXYZA", "Haddk", "Mackerel", "Pollock", "Tilefish" };
static bool toggles[] = { true, false, false, false, false };

{
ImGui::BeginPopup(&popup_open);
ImGui::Text("Aquarium");
ImGui::Separator();
for (int i = 0; i < IM_ARRAYSIZE(fishes); i++)
static bool popup_open = false;
if (ImGui::Button("Select.."))
popup_open = true;
ImGui::SameLine();
ImGui::Text(selected_fish == -1 ? "<None>" : names[selected_fish]);
if (popup_open)
{
if (ImGui::Selectable(fishes[i], false))
ImGui::BeginPopup(&popup_open);
ImGui::Text("Aquarium");
ImGui::Separator();
for (int i = 0; i < IM_ARRAYSIZE(names); i++)
{
selected_fish = i;
popup_open = false;
if (ImGui::Selectable(names[i]))
{
selected_fish = i;
popup_open = false;
}
}
ImGui::EndPopup();
}
}
{
static bool popup_open = false;
if (ImGui::Button("Toggle.."))
popup_open = true;
if (popup_open)
{
ImGui::BeginPopup(&popup_open);
for (int i = 0; i < IM_ARRAYSIZE(names); i++)
if (ImGui::MenuItem(names[i], "", &toggles[i]))
popup_open = false;
ImGui::EndPopup();
}
ImGui::EndPopup();
}

ImGui::TreePop();
}

Expand Down
5 changes: 5 additions & 0 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ namespace ImGui
IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // "
IMGUI_API void ListBoxFooter(); // terminate the scrolling region

// Widgets: Menus
// FIXME-WIP: v1.39 in development
IMGUI_API bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = NULL); // bool enabled = true
IMGUI_API bool MenuItem(const char* label, const char* shortcut, bool* p_selected); // bool enabled = true

// Widgets: Value() Helpers. Output single value in "name: value" format (tip: freely declare your own within the ImGui namespace!)
IMGUI_API void Value(const char* prefix, bool b);
IMGUI_API void Value(const char* prefix, int v);
Expand Down

0 comments on commit f46557d

Please sign in to comment.