]> git.lyx.org Git - lyx.git/commitdiff
* LFUN handler for folding/unfolding of math macros
authorStefan Schimanski <sts@lyx.org>
Thu, 1 Nov 2007 11:14:14 +0000 (11:14 +0000)
committerStefan Schimanski <sts@lyx.org>
Thu, 1 Nov 2007 11:14:14 +0000 (11:14 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21329 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/InsetMathNest.cpp
src/mathed/InsetMathNest.h

index a6f9e7165a197e3f1c2f734a056ef4be9f2c19c4..d8c83652798ff1ed45ea172773c38dafaf8b70cb 100644 (file)
@@ -28,6 +28,7 @@
 #include "InsetMathUnknown.h"
 #include "MathData.h"
 #include "MathFactory.h"
+#include "MathMacro.h"
 #include "MathMacroArgument.h"
 #include "MathParser.h"
 #include "MathStream.h"
@@ -987,6 +988,21 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.recordUndo();
                interpretChar(cur, '^');
                break;
+               
+       case LFUN_MATH_MACRO_FOLD:
+       case LFUN_MATH_MACRO_UNFOLD: {
+               Cursor it = cur;
+               bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
+               bool found = findMacroToFoldUnfold(it, fold);
+               if (found) {
+                       cur.recordUndo();
+                       if (fold)
+                               it.nextInset()->asInsetMath()->asMacro()->fold(cur);
+                       else
+                               it.nextInset()->asInsetMath()->asMacro()->unfold(cur);
+               }\v
+               break;
+       }
 
        case LFUN_QUOTE_INSERT:
                // interpret this as if a straight " was typed
@@ -1040,6 +1056,37 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
+bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
+       // look for macro to open/close, but stay in mathed
+       for (; !it.empty(); it.pop_back()) {
+                       
+               // go backward through the current cell
+               Inset * inset = it.nextInset();
+               while (inset && inset->asInsetMath()) {
+                       MathMacro * macro = inset->asInsetMath()->asMacro();
+                       if (macro) {
+                               // found the an macro to open/close?
+                               if (macro->folded() != fold)
+                                       return true;
+                               
+                               // wrong folding state -> go up one level
+                               break;
+                       }
+                       
+                       // go up if this was the left most position
+                       if (it.pos() == 0)
+                               break;
+                       
+                       // go left
+                       it.pos()--;
+                       inset = it.nextInset();
+               }
+       }
+       
+       return false;
+}
+
+
 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & flag) const
 {
@@ -1131,7 +1178,15 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                // Don't do this with multi-cell selections
                flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
                break;
-
+               
+       case LFUN_MATH_MACRO_FOLD:
+       case LFUN_MATH_MACRO_UNFOLD: {
+               Cursor it = cur;
+               bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
+               flag.enabled(found);
+               break;
+       }
+               
        case LFUN_HYPHENATION_POINT_INSERT:
        case LFUN_LIGATURE_BREAK_INSERT:
        case LFUN_MENU_SEPARATOR_INSERT:
index 3e8e1a7087904a898e6504930d9fc5dae39eea67..db4cfa74149cbe18a8c1882d49cc4d7890e438d0 100644 (file)
@@ -149,6 +149,9 @@ private:
        void lfunMouseRelease(Cursor &, FuncRequest &);
        ///
        void lfunMouseMotion(Cursor &, FuncRequest &);
+       /// Find a macro to fold or unfold, starting at searchCur and searchCur.nextInset() pointing to a macro
+       /// afterwards if found
+       bool findMacroToFoldUnfold(Cursor & searchCur, bool fold) const;
 
 protected:
        /// we store the cells in a vector