Skip to content

Add custom right-click context menu #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion implot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3191,7 +3191,8 @@ void EndPlot() {


// main ctx menu
if (can_ctx && plot.Hovered)
// if (can_ctx && plot.Hovered) <-- old line // OdehM 2025-02-20
if (can_ctx && !ImHasFlag(plot.Flags, ImPlotFlags_NoCentralMenu) && plot.Hovered) // <-- new line // OdehM 2025-02-20
ImGui::OpenPopup("##PlotContext");
if (ImGui::BeginPopup("##PlotContext")) {
ShowPlotContextMenu(plot);
Expand Down Expand Up @@ -5860,6 +5861,37 @@ void StyleColorsLight(ImPlotStyle* dst) {
colors[ImPlotCol_Crosshairs] = ImVec4(0.00f, 0.00f, 0.00f, 0.50f);
}

//-----------------------------------------------------------------------------
// [SECTION] Context Menu // OdehM 2025-02-20
//-----------------------------------------------------------------------------

bool BeginCustomContext()
{
ImPlotContext& gp = *GImPlot;

if (gp.CurrentPlot == nullptr) return false;

ImPlotPlot &plot = *gp.CurrentPlot;

const bool can_ctx = plot.Hovered &&
!plot.Items.Legend.Hovered &&
!plot.ContextLocked && // <-- added
ImGui::IsMouseReleased(ImGuiMouseButton_Right);

// main ctx menu
if (can_ctx)
ImGui::OpenPopup("##CustomPlotContext");

return ImGui::BeginPopup("##CustomPlotContext");
}

void EndCustomContext(bool include_default)
{
if (include_default)
ShowPlotContextMenu(*(GImPlot->CurrentPlot));
ImGui::EndPopup();
}

//-----------------------------------------------------------------------------
// [SECTION] Obsolete Functions/Types
//-----------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions implot.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// [SECTION] Input Mapping
// [SECTION] Miscellaneous
// [SECTION] Demo
// [SECTION] Context Menu // OdehM 2025-02-20
// [SECTION] Obsolete API

#pragma once
Expand Down Expand Up @@ -139,6 +140,7 @@ enum ImPlotFlags_ {
ImPlotFlags_NoFrame = 1 << 6, // the ImGui frame will not be rendered
ImPlotFlags_Equal = 1 << 7, // x and y axes pairs will be constrained to have the same units/pixel
ImPlotFlags_Crosshairs = 1 << 8, // the default mouse cursor will be replaced with a crosshair when hovered
ImPlotFlags_NoCentralMenu = 1 << 9, // disable the central menu, but allow other menus (such as legends and axis) // OdehM 2025_02_20
ImPlotFlags_CanvasOnly = ImPlotFlags_NoTitle | ImPlotFlags_NoLegend | ImPlotFlags_NoMenus | ImPlotFlags_NoBoxSelect | ImPlotFlags_NoMouseText
};

Expand Down Expand Up @@ -1247,6 +1249,15 @@ IMPLOT_API void ShowMetricsWindow(bool* p_popen = nullptr);
// Shows the ImPlot demo window (add implot_demo.cpp to your sources!)
IMPLOT_API void ShowDemoWindow(bool* p_open = nullptr);

//-----------------------------------------------------------------------------
// [SECTION] Context Menu // OdehM 2025-02-20
//-----------------------------------------------------------------------------

// Begin a custom central plot context menu
IMPLOT_API bool BeginCustomContext();
// End a custom central plot context menu
IMPLOT_API void EndCustomContext(bool include_default = false); // if include_default is true, the normal context menu will be appended

} // namespace ImPlot

//-----------------------------------------------------------------------------
Expand Down