commit d703dba0496e01e6953ed314c2fe572f6974b955
Author: Koji Yokota <[email protected]>
Date:   Sun Jun 15 11:15:50 2025 +0900

    Dynamic toolbar for math texts
---
 .../math/{text.svgz => dynamic-math-texts.svgz}    | Bin
 lib/math_conflicts                                 |  27 +++++++++++--
 lib/ui/stdtoolbars.inc                             |   9 +----
 src/frontends/qt/GuiToolbar.cpp                    |  44 +++++++++++++++++++++
 src/frontends/qt/GuiToolbar.h                      |  20 ++++++++++
 src/mathed/InsetMathGrid.cpp                       |  20 ----------
 src/mathed/MathFactory.cpp                         |  30 +++++++-------
 src/mathed/MathFactory.h                           |   6 ++-
 8 files changed, 107 insertions(+), 49 deletions(-)

diff --git a/lib/images/math/text.svgz b/lib/images/math/dynamic-math-texts.svgz
similarity index 100%
rename from lib/images/math/text.svgz
rename to lib/images/math/dynamic-math-texts.svgz
diff --git a/lib/math_conflicts b/lib/math_conflicts
index 94473f461e..af13a44969 100644
--- a/lib/math_conflicts
+++ b/lib/math_conflicts
@@ -1,8 +1,27 @@
 #
 # List of incompatible commands and math environments
 #
-#                       incompatible math environments
-# command name          (incl. those with a trailing star)
+# The incompatible command names should be listed one by one delimited by a
+# carriage return under the headings of the math environment name surrounded by
+# brackets
+#
+# Rules are also applied to those environments that have a trailing star
+# to their names
+
+# Inline equation $...$
+[simple]
+intertext
+shortintertext
+
+# Display equation \[...\]
+[equation]
+intertext
+shortintertext
+
+[eqnarray]
+intertext
+shortintertext
 
-intertext               equation,eqnarray,multline,equation,simple,none
-shortintertext          equation,eqnarray,multline,equation,simple,none
+[multline]
+intertext
+shortintertext
diff --git a/lib/ui/stdtoolbars.inc b/lib/ui/stdtoolbars.inc
index f565979829..1f26c8c8bc 100644
--- a/lib/ui/stdtoolbars.inc
+++ b/lib/ui/stdtoolbars.inc
@@ -218,7 +218,7 @@ ToolbarSet
        Toolbar "math_panels" "Math Panels"
                PopupMenu "space" "Math spacings"
                PopupMenu "style" "Styles & classes"
-               PopupMenu "text" "Text"
+               DynamicMenu "dynamic-math-texts" "Text"
                PopupMenu "frac-square" "Fractions"
                PopupMenu "font" "Fonts"
                PopupMenu "functions" "Functions"
@@ -410,13 +410,6 @@ ToolbarSet
                Item "Right overlap     \\mathrlap" "math-insert \mathrlap"
        End
 
-       Toolbar "text" "Text"
-               Item "Mbox      \\mbox", "math-size \mbox"
-               Item "Text      \\text", "math-size \text"
-               Item "Intertext \\intertext", "math-size \intertext"
-               Item "Short intertext   \\shortintertext", "math-size 
\shortintertext"
-       End
-
        Toolbar "sqrt-square" "Roots"
                Item "Square root       \\sqrt" "math-insert \sqrt"
                Item "Other root        \\root" "math-insert \root"
diff --git a/src/frontends/qt/GuiToolbar.cpp b/src/frontends/qt/GuiToolbar.cpp
index 0bd8c6097c..a728edf792 100644
--- a/src/frontends/qt/GuiToolbar.cpp
+++ b/src/frontends/qt/GuiToolbar.cpp
@@ -42,6 +42,8 @@
 
 #include "insets/InsetText.h"
 
+#include "mathed/MathFactory.h"
+
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstring_list.h"
@@ -372,6 +374,7 @@ bool DynamicMenuButton::isMenuType(string const & s)
 {
        return s == "dynamic-custom-insets"
                || s == "dynamic-char-styles"
+               || s == "dynamic-math-texts"
                || s == "textstyle-apply"
                || s == "paste";
 }
@@ -422,6 +425,12 @@ void DynamicMenuButton::updateTriggered()
                setEnabled(!bv->buffer().isReadonly()
                           && !m->isEmpty()
                           && inset->insetAllowed(FLEX_CODE));
+       } else if (menutype == "dynamic-math-texts") {
+               InsetMathHull * hull = 
bv->cursor().inset().asInsetMath()->asHullInset();
+               if (bv->cursor().inMathed() && hull != nullptr) {
+                       setEnabled(true);
+                       loadMathTexts(hull);
+               }
        } else if (menutype == "textstyle-apply") {
                m->clear();
                setPopupMode(QToolButton::MenuButtonPopup);
@@ -527,6 +536,41 @@ void DynamicMenuButton::loadFlexInsets()
 }
 
 
+void DynamicMenuButton::loadMathTexts(InsetMathHull * hull)
+{
+       string const & menutype = tbitem_.name;
+       if (menutype != "dynamic-math-texts")
+               return;
+
+       QMenu * m = menu();
+       m->clear();
+
+       HullType hullType = hull->getType();
+       bool skippedMenu = false;
+       for (int i=0; i<mathTextMenuSize_; i++) {
+               for (docstring const & command : mathedConflictList(hullType)) {
+                       if (from_utf8(mathTextMenu[i][0]) == command) {
+                               skippedMenu = true;
+                               break;
+                       }
+               }
+               if (!skippedMenu) {
+                       FuncRequest func(LFUN_MATH_INSERT,
+                                        "\"\\" + mathTextMenu[i][0] + "\"",
+                                        FuncRequest::TOOLBAR);
+                       QString menuText = toqstr(from_utf8(mathTextMenu[i][1] 
+ "\t\\" +
+                                                   mathTextMenu[i][0]));
+                       Action * act =
+                               new Action(func, getIcon(func, false),
+                                          menuText, menuText, this);
+                       m->addAction(act);
+               } else {
+                       skippedMenu = false;
+               }
+       }
+}
+
+
 void GuiToolbar::add(ToolbarItem const & item)
 {
        switch (item.type) {
diff --git a/src/frontends/qt/GuiToolbar.h b/src/frontends/qt/GuiToolbar.h
index 1b780b6bbb..487d5a112f 100644
--- a/src/frontends/qt/GuiToolbar.h
+++ b/src/frontends/qt/GuiToolbar.h
@@ -16,6 +16,8 @@
 #ifndef GUITOOLBAR_H
 #define GUITOOLBAR_H
 
+#include "mathed/InsetMathHull.h"
+
 #include <QList>
 #include <QToolBar>
 #include <QToolButton>
@@ -101,6 +103,8 @@ protected:
        void initialize() override;
        ///
        void loadFlexInsets();
+       ///
+       void loadMathTexts(InsetMathHull * hull);
        /// pimpl so we don't have to include big files
        class Private;
        Private * d;
@@ -113,6 +117,22 @@ private:
        static QIcon icon_textstyle_apply_;
        static QIcon icon_undo_;
        static QIcon icon_paste_;
+
+       /// Size of the menu items of math texts
+       // Make this number sync with the row number of mathTextMenu
+       int const mathTextMenuSize_ = 6;
+       /// Dynamic menu items of math texts
+       // As of June 2025, only applicable to "Text" toolbar menu
+       std::string mathTextMenu[6][2] =
+       {
+           {"mbox",           "LR Text"},
+               {"fbox",           "Framed Text"},
+               {"framebox",       "Framed Text with sizes"},
+               {"text",           "AMS Text"},
+               {"intertext",      "Intertext"},
+               {"shortintertext", "Short intertext"}
+       };
+
 };
 
 
diff --git a/src/mathed/InsetMathGrid.cpp b/src/mathed/InsetMathGrid.cpp
index 4cd964a1ea..02f0aac654 100644
--- a/src/mathed/InsetMathGrid.cpp
+++ b/src/mathed/InsetMathGrid.cpp
@@ -1299,7 +1299,6 @@ void InsetMathGrid::write(TeXMathStream & os,
                                   << "}{" << cellinfo_[idx].align
                                   << "}{";
                        }
-                       checkMathCommandConflict(idx);
                        os << cell(idx);
                        if (os.pendingBrace())
                                ModeSpecifier specifier(os, TEXT_MODE);
@@ -1327,25 +1326,6 @@ void InsetMathGrid::write(TeXMathStream & os,
        }
 }
 
-void InsetMathGrid::checkMathCommandConflict(idx_type idx) const
-{
-       for (docstring const &command : mathedConflictCommands()) {
-               if (!cell(idx).empty() && cell(idx).front()->name() == command) 
{
-                       for (docstring const & hull : 
mathedConflictList().at(command)) {
-                               if (getType() == hullType(hull)) {
-                                       docstring const & warnTitle =
-                                               from_utf8(N_("Command 
conflict"));
-                                       docstring const & warnMessage =
-                                               bformat(_("TeX command '%1$s' 
cannot be used in math environment '%2$s'. Please change the environment or the 
command."),
-                                                       command, hull);
-                                       frontend::Alert::warning(warnTitle, 
warnMessage);
-                                       return;
-                               }
-                       }
-               }
-       }
-}
-
 
 int InsetMathGrid::colsep() const
 {
diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp
index e9f1aa2496..7838c28a95 100644
--- a/src/mathed/MathFactory.cpp
+++ b/src/mathed/MathFactory.cpp
@@ -92,7 +92,7 @@ namespace {
 MathWordList theMathWordList;
 MathVariantList theMathVariantList;
 MathConflictList theMathConflictList;
-std::vector<docstring> theMathConflictCommands;
+std::vector<HullType> theMathConflictEnvs;
 
 
 bool isMathFontAvailable(string & name)
@@ -443,24 +443,18 @@ void initConflictList() {
        fs >> setw(65636);
        string line;
 
+       HullType currentMathHull;
        while (getline(fs, line)) {
 
                if (line.empty() || line[0] == '#')
                        continue;
 
-               docstring command;
-               docstring mathHulls;
-               idocstringstream is(from_utf8(line));
-               is >> command >> mathHulls;
-               docstring hul;
-               vector<docstring> mathHullList;
-               idocstringstream is2(subst(mathHulls, ',', '\n'));
-               while (getline(is2, hul)) {
-                       mathHullList.push_back(hul);
+               if (line[0] == '[') {
+                       currentMathHull = hullType(from_utf8(trim(line, "[]")));
+                       theMathConflictEnvs.push_back(currentMathHull);
+               } else {
+                       
theMathConflictList[currentMathHull].push_back(from_utf8(line));
                }
-
-               theMathConflictList[command] = mathHullList;
-               theMathConflictCommands.push_back(command);
        }
 }
 
@@ -497,9 +491,15 @@ MathConflictList const & mathedConflictList()
 }
 
 
-std::vector<docstring> const & mathedConflictCommands()
+std::vector<docstring> const & mathedConflictList(HullType hull)
+{
+       return theMathConflictList[hull];
+}
+
+
+std::vector<HullType> const & mathedConflictEnvs()
 {
-       return theMathConflictCommands;
+       return theMathConflictEnvs;
 }
 
 
diff --git a/src/mathed/MathFactory.h b/src/mathed/MathFactory.h
index b92f5562ae..0f06ff6e85 100644
--- a/src/mathed/MathFactory.h
+++ b/src/mathed/MathFactory.h
@@ -12,6 +12,7 @@
 #ifndef MATH_FACTORY_H
 #define MATH_FACTORY_H
 
+#include "InsetMath.h"
 #include "MathParser.h"
 
 #include <map>
@@ -37,10 +38,11 @@ MathWordList const & mathedWordList();
 typedef std::map<docstring, UnicodeVariants> MathVariantList;
 MathVariantList const & mathedVariantList();
 
-typedef std::map<docstring, std::vector<docstring>> MathConflictList;
+typedef std::map<HullType, std::vector<docstring>> MathConflictList;
 MathConflictList const & mathedConflictList();
+std::vector<docstring> const & mathedConflictList(HullType hull);
 
-std::vector<docstring> const & mathedConflictCommands();
+std::vector<HullType> const & mathedConflictEnvs();
 
 } // namespace lyx
 
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to