From 938528e5ee851cb3edd4737aa56b969ca4d838a1 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 27 May 2015 22:36:23 +0100 Subject: [PATCH] Added BeginPopupContextItem() / BeginPopupContextWindow() (#126) --- imgui.cpp | 20 ++++++++++++++++++++ imgui.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/imgui.cpp b/imgui.cpp index b5e5c5e12..ea70d5928 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3171,6 +3171,21 @@ void ImGui::EndPopup() ImGui::PopStyleVar(); } +bool ImGui::BeginPopupContextItem(const char* str_id, int button) +{ + if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(button)) + ImGui::OpenPopup(str_id); + return ImGui::BeginPopup(str_id); +} + +bool ImGui::BeginPopupContextWindow(const char* str_id, bool void_only, int button) +{ + if (ImGui::IsMouseHoveringWindow() && ImGui::IsMouseClicked(button)) + if (!void_only || !ImGui::IsAnyItemHovered()) + ImGui::OpenPopup(str_id); + return ImGui::BeginPopup(str_id); +} + bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags) { ImGuiState& g = *GImGui; @@ -11694,6 +11709,11 @@ struct ExampleAppConsole // NB- if you have thousands of entries this approach may be too inefficient. You can seek and display only the lines that are visible - CalcListClipping() is a helper to compute this information. // If your items are of variable size you may want to implement code similar to what CalcListClipping() does. Or split your data into fixed height items to allow random-seeking into your list. ImGui::BeginChild("ScrollingRegion", ImVec2(0,-ImGui::GetTextLineHeightWithSpacing()*2)); + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::Selectable("Clear")) ClearLog(); + ImGui::EndPopup(); + } ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4,1)); // Tighten spacing for (size_t i = 0; i < Items.size(); i++) { diff --git a/imgui.h b/imgui.h index 9e280841c..0cb0ad5a3 100644 --- a/imgui.h +++ b/imgui.h @@ -169,6 +169,8 @@ namespace ImGui // Popup IMGUI_API void OpenPopup(const char* str_id); // mark popup as open. popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). close childs popups if any. will close popup when user click outside, or activate a pressable item, or CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. IMGUI_API bool BeginPopup(const char* str_id); // return true if popup if opened and start outputting to it. only call EndPopup() if BeginPopup() returned true! + IMGUI_API bool BeginPopupContextItem(const char* str_id, int button = 1); // open popup when clicked on last item + IMGUI_API bool BeginPopupContextWindow(const char* str_id = "window_context_menu", bool void_only = false, int button = 1); // open popup when clicked on current window IMGUI_API void EndPopup(); IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup.