Allow context menus in mathed

It is now possible to get a context menu for math insets.
InsetMathSpace was already providing a specific context menu,
but it was never triggered because the math hull inset is not
descendable. It is still so, but now when a context menu is
requested all the insets inside the math hull are examined.
If the inset under the cursor provides a context menu, it
is shown instead of the general math one.

Fixes #12100.
This commit is contained in:
Enrico Forestieri 2021-03-05 21:21:41 +01:00
parent 410fe4fac1
commit 5a43b86141
4 changed files with 55 additions and 3 deletions

View File

@ -50,7 +50,7 @@
#include "insets/InsetRef.h" #include "insets/InsetRef.h"
#include "insets/InsetText.h" #include "insets/InsetText.h"
#include "mathed/InsetMath.h" #include "mathed/InsetMathNest.h"
#include "mathed/MathData.h" #include "mathed/MathData.h"
#include "mathed/MathRow.h" #include "mathed/MathRow.h"
@ -669,13 +669,53 @@ string BufferView::contextMenu(int x, int y) const
// Get inset under mouse, if there is one. // Get inset under mouse, if there is one.
Inset const * covering_inset = getCoveringInset(buffer_.text(), x, y); Inset const * covering_inset = getCoveringInset(buffer_.text(), x, y);
if (covering_inset) if (covering_inset) {
if (covering_inset->asInsetMath()) {
CoordCache::Insets const & inset_cache =
coordCache().getInsets();
Inset const * inner_inset = mathContextMenu(
covering_inset->asInsetMath()->asNestInset(),
inset_cache, x, y);
if (inner_inset)
return inner_inset->contextMenu(*this, x, y);
}
return covering_inset->contextMenu(*this, x, y); return covering_inset->contextMenu(*this, x, y);
}
return buffer_.inset().contextMenu(*this, x, y); return buffer_.inset().contextMenu(*this, x, y);
} }
Inset const * BufferView::mathContextMenu(InsetMathNest const * inset,
CoordCache::Insets const & inset_cache, int x, int y) const
{
for (size_t i = 0; i < inset->nargs(); ++i) {
MathData const & ar = inset->cell(i);
for (size_t j = 0; j < ar.size(); ++j) {
string const name = lyxerr.debugging(Debug::MATHED)
? insetName(ar[j].nucleus()->lyxCode())
: string();
LYXERR(Debug::MATHED, "Examining inset: " << name);
if (!ar[j].nucleus()->contextMenuName().empty()) {
if (inset_cache.covers(ar[j].nucleus(), x, y)) {
LYXERR(Debug::MATHED, "Hit inset: "
<< name);
return ar[j].nucleus();
}
}
InsetMathNest const * imn =
ar[j].nucleus()->asNestInset();
if (imn) {
Inset const * inner =
mathContextMenu(imn, inset_cache, x, y);
if (inner)
return inner;
}
}
}
return nullptr;
}
void BufferView::scrollDocView(int const pixels, bool update) void BufferView::scrollDocView(int const pixels, bool update)
{ {

View File

@ -15,6 +15,7 @@
#ifndef BUFFER_VIEW_H #ifndef BUFFER_VIEW_H
#define BUFFER_VIEW_H #define BUFFER_VIEW_H
#include "CoordCache.h"
#include "DocumentClassPtr.h" #include "DocumentClassPtr.h"
#include "TexRow.h" #include "TexRow.h"
#include "update_flags.h" #include "update_flags.h"
@ -32,7 +33,6 @@ namespace frontend { class GuiBufferViewDelegate; }
class Buffer; class Buffer;
class Change; class Change;
class CoordCache;
class Cursor; class Cursor;
class CursorSlice; class CursorSlice;
class Dimension; class Dimension;
@ -42,6 +42,7 @@ class FuncRequest;
class FuncStatus; class FuncStatus;
class Intl; class Intl;
class Inset; class Inset;
class InsetMathNest;
class Length; class Length;
class MathData; class MathData;
class MathRow; class MathRow;
@ -160,6 +161,9 @@ public:
docstring toolTip(int x, int y) const; docstring toolTip(int x, int y) const;
/// \return the context menu for the given position. /// \return the context menu for the given position.
std::string contextMenu(int x, int y) const; std::string contextMenu(int x, int y) const;
/// \return the math inset with a context menu for the given position
Inset const * mathContextMenu(InsetMathNest const * inset,
CoordCache::Insets const & inset_cache, int x, int y) const;
/// Save the current position as bookmark. /// Save the current position as bookmark.
/// if idx == 0, save to temp_bookmark /// if idx == 0, save to temp_bookmark

View File

@ -33,6 +33,12 @@ public:
/// ///
void infoize(odocstream & os) const override; void infoize(odocstream & os) const override;
/// ///
bool hasSettings() const override { return true; }
///
bool clickable(BufferView const &, int, int) const override { return true; }
///
std::string contextMenuName() const override { return "context-ref"; }
///
mode_type currentMode() const override { return TEXT_MODE; } mode_type currentMode() const override { return TEXT_MODE; }
/// ///
bool lockedMode() const override { return true; } bool lockedMode() const override { return true; }

View File

@ -62,6 +62,8 @@ public:
/// ///
bool hasSettings() const override { return true; } bool hasSettings() const override { return true; }
/// ///
bool clickable(BufferView const &, int, int) const override { return true; }
///
std::string contextMenuName() const override; std::string contextMenuName() const override;
/// ///
bool getStatus(Cursor &, FuncRequest const &, FuncStatus &) const override; bool getStatus(Cursor &, FuncRequest const &, FuncStatus &) const override;