]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
compilation fix
[lyx.git] / src / text3.C
index 5f37f7de5b21d9e6c091cb5fb5455a17f0d5bdff..9ddee5cf60f2574b5ce4fd8a2e52492ee0aa6687 100644 (file)
@@ -32,6 +32,7 @@
 #include "gettext.h"
 #include "intl.h"
 #include "language.h"
+#include "LyXAction.h"
 #include "lyxfunc.h"
 #include "lyxlex.h"
 #include "lyxrc.h"
@@ -54,7 +55,7 @@
 
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
-#include "support/tostr.h"
+#include "support/convert.h"
 
 #include "mathed/math_hullinset.h"
 #include "mathed/math_macrotemplate.h"
@@ -70,8 +71,6 @@ using lyx::cap::pasteSelection;
 using lyx::cap::replaceSelection;
 
 using lyx::support::isStrUnsignedInt;
-using lyx::support::strToUnsignedInt;
-using lyx::support::atoi;
 using lyx::support::token;
 
 using std::endl;
@@ -83,10 +82,6 @@ using std::vector;
 
 extern string current_layout;
 
-// the selection possible is needed, that only motion events are
-// used, where the button press event was on the drawing area too
-bool selection_possible = false;
-
 
 namespace {
 
@@ -257,7 +252,7 @@ void LyXText::gotoInset(LCursor & cur, InsetBase_code code, bool same_content)
 }
 
 
-void LyXText::cursorPrevious(LCursor & cur)
+bool LyXText::cursorPrevious(LCursor & cur)
 {
        pos_type cpos = cur.pos();
        lyx::pit_type cpar = cur.pit();
@@ -265,36 +260,38 @@ void LyXText::cursorPrevious(LCursor & cur)
        int x = cur.x_target();
 
        setCursorFromCoordinates(cur, x, 0);
-       cursorUp(cur);
+       bool updated = cursorUp(cur);
 
        if (cpar == cur.pit() && cpos == cur.pos()) {
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the previous row instead.
-               cursorUp(cur);
+               updated |= cursorUp(cur);
        }
 
        cur.bv().updateScrollbar();
        finishUndo();
+       return updated;
 }
 
 
-void LyXText::cursorNext(LCursor & cur)
+bool LyXText::cursorNext(LCursor & cur)
 {
        pos_type cpos = cur.pos();
        lyx::pit_type cpar = cur.pit();
 
        int x = cur.x_target();
        setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
-       cursorDown(cur);
+       bool updated = cursorDown(cur);
 
        if (cpar == cur.pit() && cpos == cur.pos()) {
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the next row instead.
-               cursorDown(cur);
+               updated |= cursorDown(cur);
        }
 
        cur.bv().updateScrollbar();
        finishUndo();
+       return updated;
 }
 
 
@@ -359,13 +356,13 @@ bool LyXText::isRTL(Paragraph const & par) const
 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 {
        lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
-       //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
+       lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
 
        BOOST_ASSERT(cur.text() == this);
        BufferView * bv = &cur.bv();
-       CursorSlice sl = cur.top();
+       CursorSlice oldTopSlice = cur.top();
        bool sel = cur.selection();
-       bool moving = false;
+       bool needsUpdate = !lyxaction.funcHasFlag(cmd.action, LyXAction::NoUpdate);
 
        switch (cmd.action) {
 
@@ -409,29 +406,27 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_WORDRIGHT:
-               moving = true;
                if (!cur.mark())
                        cur.clearSelection();
                if (isRTL(cur.paragraph()))
-                       cursorLeftOneWord(cur);
+                       needsUpdate = cursorLeftOneWord(cur);
                else
-                       cursorRightOneWord(cur);
+                       needsUpdate = cursorRightOneWord(cur);
                finishChange(cur, false);
                break;
 
        case LFUN_WORDLEFT:
-               moving = true;
                if (!cur.mark())
                        cur.clearSelection();
                if (isRTL(cur.paragraph()))
-                       cursorRightOneWord(cur);
+                       needsUpdate = cursorRightOneWord(cur);
                else
-                       cursorLeftOneWord(cur);
+                       needsUpdate = cursorLeftOneWord(cur);
                finishChange(cur, false);
                break;
 
        case LFUN_BEGINNINGBUF:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.mark())
                                cur.clearSelection();
                        cursorTop(cur);
@@ -442,7 +437,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_BEGINNINGBUFSEL:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.selection())
                                cur.resetAnchor();
                        cursorTop(cur);
@@ -453,7 +448,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_ENDBUF:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.mark())
                                cur.clearSelection();
                        cursorBottom(cur);
@@ -462,9 +457,9 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                }
                break;
-               
+
        case LFUN_ENDBUFSEL:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.selection())
                                cur.resetAnchor();
                        cursorBottom(cur);
@@ -475,61 +470,65 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_RIGHT:
-               moving = true;
        case LFUN_RIGHTSEL:
-               //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
+               lyxerr << BOOST_CURRENT_FUNCTION
+                      << " LFUN_RIGHT[SEL]:\n" << cur << endl;
                cur.selHandle(cmd.action == LFUN_RIGHTSEL);
                if (isRTL(cur.paragraph()))
-                       cursorLeft(cur);
+                       needsUpdate = cursorLeft(cur);
                else
-                       cursorRight(cur);
-               if (sl == cur.top()) {
+                       needsUpdate = cursorRight(cur);
+               if (!needsUpdate && oldTopSlice == cur.top()) {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
                }
                break;
 
        case LFUN_LEFT:
-               moving = true;
        case LFUN_LEFTSEL:
                //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
                cur.selHandle(cmd.action == LFUN_LEFTSEL);
                if (isRTL(cur.paragraph()))
-                       cursorRight(cur);
+                       needsUpdate = cursorRight(cur);
                else
-                       cursorLeft(cur);
-               if (sl == cur.top()) {
+                       needsUpdate = cursorLeft(cur);
+               if (oldTopSlice == cur.top()) {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_LEFT);
                }
                break;
 
        case LFUN_UP:
-               moving = true;
        case LFUN_UPSEL:
                update(cur);
                //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
                cur.selHandle(cmd.action == LFUN_UPSEL);
-               cursorUp(cur);
-               if (sl == cur.top()) {
+               needsUpdate = cursorUp(cur);
+               if (oldTopSlice == cur.top()) {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_UP);
                }
                break;
 
        case LFUN_DOWN:
-               moving = true;
        case LFUN_DOWNSEL:
                update(cur);
                //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
                cur.selHandle(cmd.action == LFUN_DOWNSEL);
-               cursorDown(cur);
-               if (sl == cur.top()) {
+               needsUpdate = cursorDown(cur);
+               if (oldTopSlice == cur.top()) {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_DOWN);
                }
                break;
 
+       case LFUN_UP_PARAGRAPH:
+               if (!cur.mark())
+                       cur.clearSelection();
+               needsUpdate = cursorUpParagraph(cur);
+               finishChange(cur, false);
+               break;
+
        case LFUN_UP_PARAGRAPHSEL:
                if (!cur.selection())
                        cur.resetAnchor();
@@ -537,6 +536,13 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                finishChange(cur, true);
                break;
 
+       case LFUN_DOWN_PARAGRAPH:
+               if (!cur.mark())
+                       cur.clearSelection();
+               needsUpdate = cursorDownParagraph(cur);
+               finishChange(cur, false);
+               break;
+
        case LFUN_DOWN_PARAGRAPHSEL:
                if (!cur.selection())
                        cur.resetAnchor();
@@ -602,25 +608,8 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
        }
 
-       case LFUN_UP_PARAGRAPH:
-               moving = true;
-               if (!cur.mark())
-                       cur.clearSelection();
-               cursorUpParagraph(cur);
-               finishChange(cur, false);
-               break;
-
-       case LFUN_DOWN_PARAGRAPH:
-               moving = true;
-               if (!cur.mark())
-                       cur.clearSelection();
-               cursorDownParagraph(cur);
-               finishChange(cur, false);
-               break;
-
        case LFUN_PRIOR:
                update(cur);
-               moving = true;
                if (!cur.mark())
                        cur.clearSelection();
                finishChange(cur, false);
@@ -628,13 +617,12 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_UP);
                } else {
-                       cursorPrevious(cur);
+                       needsUpdate = cursorPrevious(cur);
                }
                break;
 
        case LFUN_NEXT:
                update(cur);
-               moving = true;
                if (!cur.mark())
                        cur.clearSelection();
                finishChange(cur, false);
@@ -643,7 +631,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_DOWN);
                } else {
-                       cursorNext(cur);
+                       needsUpdate = cursorNext(cur);
                }
                break;
 
@@ -757,15 +745,15 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_PARAGRAPH_SPACING: {
                Paragraph & par = cur.paragraph();
                Spacing::Space cur_spacing = par.params().spacing().getSpace();
-               float cur_value = 1.0;
+               string cur_value = "1.0";
                if (cur_spacing == Spacing::Other)
-                       cur_value = par.params().spacing().getValue();
+                       cur_value = par.params().spacing().getValueAsString();
 
                istringstream is(cmd.argument);
                string tmp;
                is >> tmp;
                Spacing::Space new_spacing = cur_spacing;
-               float new_value = cur_value;
+               string new_value = cur_value;
                if (tmp.empty()) {
                        lyxerr << "Missing argument to `paragraph-spacing'"
                               << endl;
@@ -777,10 +765,10 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        new_spacing = Spacing::Double;
                } else if (tmp == "other") {
                        new_spacing = Spacing::Other;
-                       float tmpval = 0.0;
+                       string tmpval = "0.0";
                        is >> tmpval;
                        lyxerr << "new_value = " << tmpval << endl;
-                       if (tmpval != 0.0)
+                       if (tmpval != "0.0")
                                new_value = tmpval;
                } else if (tmp == "default") {
                        new_spacing = Spacing::Default;
@@ -788,7 +776,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        lyxerr << _("Unknown spacing argument: ")
                               << cmd.argument << endl;
                }
-               if (cur_spacing != new_spacing || cur_value != new_value) 
+               if (cur_spacing != new_spacing || cur_value != new_value)
                        par.params().spacing(Spacing(new_spacing, new_value));
                break;
        }
@@ -890,7 +878,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 #warning FIXME Check if the arg is in the domain of available selections.
 #endif
                if (isStrUnsignedInt(cmd.argument))
-                       pasteSelection(cur, strToUnsignedInt(cmd.argument));
+                       pasteSelection(cur, convert<unsigned int>(cmd.argument));
                else
                        pasteSelection(cur, 0);
                cur.clearSelection(); // bug 393
@@ -909,8 +897,8 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_GETXY:
-               cur.message(tostr(cursorX(cur.top())) + ' '
-                         + tostr(cursorY(cur.top())));
+               cur.message(convert<string>(cursorX(cur.top())) + ' '
+                         + convert<string>(cursorY(cur.top())));
                break;
 
        case LFUN_SETXY: {
@@ -1084,7 +1072,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_MOUSE_TRIPLE:
                if (cmd.button() == mouse_button::button1) {
-                       selection_possible = true;
                        cursorHome(cur);
                        cur.resetAnchor();
                        cursorEnd(cur);
@@ -1095,7 +1082,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_MOUSE_DOUBLE:
                if (cmd.button() == mouse_button::button1) {
-                       selection_possible = true;
                        selectWord(cur, lyx::WHOLE_WORD_STRICT);
                        bv->haveSelection(cur.selection());
                }
@@ -1106,7 +1092,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                // Right click on a footnote flag opens float menu
                if (cmd.button() == mouse_button::button3) {
                        cur.clearSelection();
-                       selection_possible = false;
                        break;
                }
 
@@ -1120,8 +1105,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        paste_internally = true;
                }
 
-               selection_possible = true;
-
                // Clear the selection
                cur.clearSelection();
 
@@ -1137,10 +1120,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                // Set cursor here.
                bv->cursor() = cur;
 
-               // Don't allow selection after a big jump.
-               //if (bv->fitCursor())
-               //      selection_possible = false;
-
                // Insert primary selection with middle mouse
                // if there is a local selection in the current buffer,
                // insert this
@@ -1149,7 +1128,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                                bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
                        else
                                bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
-                       selection_possible = false;
                }
 
                break;
@@ -1159,14 +1137,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                // Only use motion with button 1
                //if (cmd.button() != mouse_button::button1)
                //      return false;
-               // We want to use only motion events for which
-               // the button press event was on the drawing area too.
-               if (!selection_possible) {
-                       lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
-                               "dispatch: no selection possible\n";
-                       lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
-                       break;
-               }
 
                // ignore motions deeper nested than the real anchor
                LCursor & bvcur = cur.bv().cursor();
@@ -1175,7 +1145,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 
                        int const wh = bv->workHeight();
                        int const y = std::max(0, std::min(wh - 1, cmd.y));
-                                         
+
                        setCursorFromCoordinates(cur, cmd.x, y);
                        cur.x_target() = cmd.x;
                        if (cmd.y >= wh)
@@ -1198,15 +1168,13 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                                bvcur.selection() = true;
                                lyxerr << "MOTION: " << bv->cursor() << endl;
                        }
-                       
+
                } else
                        cur.undispatched();
                break;
        }
 
        case LFUN_MOUSE_RELEASE: {
-               selection_possible = false;
-
                if (cmd.button() == mouse_button::button2)
                        break;
 
@@ -1342,7 +1310,11 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_IMPORT_SELECTION:
        case LFUN_MATH_MODE:
-               mathDispatch(cur, cmd, false);
+               if (cmd.argument == "on")
+                       // don't pass "on" as argument
+                       mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
+               else
+                       mathDispatch(cur, cmd, false);
                break;
 
        case LFUN_MATH_MACRO:
@@ -1351,7 +1323,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                else {
                        string s = cmd.argument;
                        string const s1 = token(s, ' ', 1);
-                       int const nargs = s1.empty() ? 0 : atoi(s1);
+                       int const nargs = s1.empty() ? 0 : convert<int>(s1);
                        string const s2 = token(s, ' ', 2);
                        string const type = s2.empty() ? "newcommand" : s2;
                        cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
@@ -1359,6 +1331,14 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                }
                break;
 
+       // passthrough hat and underscore outside mathed:
+       case LFUN_SUBSCRIPT:
+               mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
+               break;
+       case LFUN_SUPERSCRIPT:
+               mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
+               break;
+
        case LFUN_INSERT_MATH:
        case LFUN_INSERT_MATRIX:
        case LFUN_MATH_DELIM: {
@@ -1498,7 +1478,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                InsetBase & inset = cur.inset();
                bool const accept = !inset.forceDefaultParagraphs(&inset);
 
-               data = "update " + tostr(accept) + '\n' + data;
+               data = "update " + convert<string>(accept) + '\n' + data;
                bv->owner()->getDialogs().update("paragraph", data);
                break;
        }
@@ -1612,17 +1592,20 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        default:
+               lyxerr << BOOST_CURRENT_FUNCTION
+                      << " Not DISPATCHED by LyXText" << endl;
                cur.undispatched();
                break;
        }
 
-       // avoid to update when navigating
-       if (moving
-           && &sl.inset() == &cur.inset()
-           && sl.idx() == cur.idx()
-           && sel == false
-           && cur.selection() == false)
+       if (!needsUpdate
+           && &oldTopSlice.inset() == &cur.inset()
+           && oldTopSlice.idx() == cur.idx()
+           && !sel
+           && !cur.selection())
                cur.noUpdate();
+       else
+               cur.needsUpdate();
 }
 
 
@@ -1928,6 +1911,8 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
        case LFUN_INSERT_MATH:
        case LFUN_INSERT_MATRIX:
        case LFUN_MATH_DELIM:
+       case LFUN_SUBSCRIPT:
+       case LFUN_SUPERSCRIPT:
        case LFUN_DEFAULT:
        case LFUN_UNDERLINE:
        case LFUN_FONT_SIZE: