]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
fix #832
[lyx.git] / src / insets / insettabular.C
index 07cad76bc4d91bdfa076cee18682c9ae1facbec1..d13ee3a8a5b70f56891090745c13148aa576432d 100644 (file)
@@ -10,7 +10,6 @@
 
 #include <config.h>
 
-
 #include "insettabular.h"
 #include "insettext.h"
 
@@ -30,6 +29,7 @@
 #include "ParagraphParameters.h"
 #include "undo_funcs.h"
 #include "WordLangTuple.h"
+#include "Lsstream.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
@@ -437,58 +437,7 @@ void InsetTabular::update(BufferView * bv, bool reinit)
 
 string const InsetTabular::editMessage() const
 {
-       return _("Opened Tabular Inset");
-}
-
-
-void InsetTabular::edit(BufferView * bv, int x, int y, mouse_button::state button)
-{
-       UpdatableInset::edit(bv, x, y, button);
-
-       if (!bv->lockInset(this)) {
-               lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
-               return;
-       }
-       locked = true;
-       the_locking_inset = 0;
-       inset_x = 0;
-       inset_y = 0;
-       setPos(bv, x, y);
-       clearSelection();
-       finishUndo();
-       if (insetHit(bv, x, y) && (button != mouse_button::button3)) {
-               activateCellInsetAbs(bv, x, y, button);
-       }
-}
-
-
-void InsetTabular::edit(BufferView * bv, bool front)
-{
-       UpdatableInset::edit(bv, front);
-
-       if (!bv->lockInset(this)) {
-               lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
-               return;
-       }
-       finishUndo();
-       locked = true;
-       the_locking_inset = 0;
-       inset_x = 0;
-       inset_y = 0;
-       if (front) {
-               if (isRightToLeft(bv))
-                       actcell = tabular->GetLastCellInRow(0);
-               else
-                       actcell = 0;
-       } else {
-               if (isRightToLeft(bv))
-                       actcell = tabular->GetFirstCellInRow(tabular->rows()-1);
-               else
-                       actcell = tabular->GetNumberOfCells() - 1;
-       }
-       clearSelection();
-       resetPos(bv);
-       bv->fitCursor();
+       return _("Opened table");
 }
 
 
@@ -499,7 +448,6 @@ void InsetTabular::insetUnlock(BufferView * bv)
                updateLocal(bv, CELL);
                the_locking_inset = 0;
        }
-       hideInsetCursor(bv);
        actcell = 0;
        oldcell = -1;
        locked = false;
@@ -557,7 +505,7 @@ bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
                        }
                        if (in->getInsetFromID(id)) {
                                actcell = i;
-                               in->edit(bv);
+                               in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
                                return the_locking_inset->lockInsetInInset(bv, inset);
                        }
                }
@@ -590,7 +538,6 @@ bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
                updateLocal(bv, CELL);
                // this has to be here otherwise we don't redraw the cell!
                the_locking_inset = 0;
-//             showInsetCursor(bv, false);
                return true;
        }
        if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
@@ -672,7 +619,6 @@ void InsetTabular::lfunMousePress(FuncRequest const & cmd)
        int const orow = actrow;
        BufferView * bv = cmd.view();
 
-       hideInsetCursor(bv);
        if (!locked) {
                locked = true;
                the_locking_inset = 0;
@@ -690,7 +636,6 @@ void InsetTabular::lfunMousePress(FuncRequest const & cmd)
                        updateLocal(bv, CELL);
                        the_locking_inset = 0;
                }
-               showInsetCursor(bv);
                return;
        }
 #endif
@@ -730,7 +675,6 @@ void InsetTabular::lfunMousePress(FuncRequest const & cmd)
                the_locking_inset->localDispatch(cmd1);
                return;
        }
-       showInsetCursor(bv);
 }
 
 
@@ -763,7 +707,6 @@ void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
        }
 
        BufferView * bv = cmd.view();
-       hideInsetCursor(bv);
        int const old_cell = actcell;
 
        setPos(bv, cmd.x, cmd.y);
@@ -774,7 +717,6 @@ void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
                setSelection(sel_cell_start, actcell);
                updateLocal(bv, SELECTION);
        }
-       showInsetCursor(bv);
 }
 
 
@@ -784,8 +726,49 @@ Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
        // the_locking_inset->localDispatch might unlock it.
        old_locking_inset = the_locking_inset;
        RESULT result = UpdatableInset::localDispatch(cmd);
-
        BufferView * bv = cmd.view();
+
+       if (cmd.action == LFUN_INSET_EDIT) {
+               
+               if (!bv->lockInset(this)) {
+                       lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
+                       return DISPATCHED;
+               }
+
+               finishUndo();
+               locked = true;
+               the_locking_inset = 0;
+               inset_x = 0;
+               inset_y = 0;
+
+               if (cmd.argument.size()) {
+                       if (cmd.argument == "left") {
+                               if (isRightToLeft(bv))
+                                       actcell = tabular->GetLastCellInRow(0);
+                               else
+                                       actcell = 0;
+                       } else {
+                               if (isRightToLeft(bv))
+                                       actcell = tabular->GetFirstCellInRow(tabular->rows()-1);
+                               else
+                                       actcell = tabular->GetNumberOfCells() - 1;
+                       }
+                       clearSelection();
+                       resetPos(bv);
+                       bv->fitCursor();
+               }
+
+               else {
+                       setPos(bv, cmd.x, cmd.y);
+                       clearSelection();
+                       finishUndo();
+                       if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
+                               activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
+                       }
+               }
+               return DISPATCHED;
+       }
+
        if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
                resetPos(bv);
                return result;
@@ -812,11 +795,10 @@ Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
                case LFUN_MOUSE_RELEASE:
                        return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
 
-               case LFUN_SHIFT_TAB:
-               case LFUN_TAB:
-                       hideInsetCursor(bv);
+               case LFUN_CELL_BACKWARD:
+               case LFUN_CELL_FORWARD:
                        unlockInsetInInset(bv, the_locking_inset);
-                       if (cmd.action == LFUN_TAB)
+                       if (cmd.action == LFUN_CELL_FORWARD)
                                moveNextCell(bv, old_locking_inset != 0);
                        else
                                movePrevCell(bv, old_locking_inset != 0);
@@ -824,7 +806,6 @@ Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
                        if (hs)
                                updateLocal(bv, SELECTION);
                        if (!the_locking_inset) {
-                               showInsetCursor(bv);
                                return DISPATCHED_NOUPDATE;
                        }
                        return result;
@@ -842,26 +823,28 @@ Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
                        int sc = scroll();
                        resetPos(bv);
                        if (sc != scroll()) { // inset has been scrolled
-                               the_locking_inset->toggleInsetCursor(bv);
                                updateLocal(bv, FULL);
-                               the_locking_inset->toggleInsetCursor(bv);
                        }
                        return result;
                } else if (result == DISPATCHED) {
-                       the_locking_inset->toggleInsetCursor(bv);
                        updateLocal(bv, CELL);
-                       the_locking_inset->toggleInsetCursor(bv);
                        return result;
                } else if (result == FINISHED_UP) {
                        action = LFUN_UP;
+                       // Make sure to reset status message after
+                       // exiting, e.g. math inset
+                       bv->owner()->clearMessage();
                } else if (result == FINISHED_DOWN) {
                        action = LFUN_DOWN;
+                       bv->owner()->clearMessage();
                } else if (result == FINISHED_RIGHT) {
                        action = LFUN_RIGHT;
+                       bv->owner()->clearMessage();
+               } else if (result == FINISHED) {
+                       bv->owner()->clearMessage();
                }
        }
 
-       hideInsetCursor(bv);
        result = DISPATCHED;
        switch (action) {
                // --- Cursor Movements ----------------------------------
@@ -1064,7 +1047,7 @@ Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
        case LFUN_DELETE:
                setUndo(bv, Undo::DELETE,
                        bv->text->cursor.par(),
-                       bv->text->cursor.par()->next());
+                       boost::next(bv->text->cursor.par()));
                cutSelection(bv->buffer()->params);
                updateLocal(bv, INIT);
                break;
@@ -1149,7 +1132,7 @@ Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
                if (hasPasteBuffer()) {
                        setUndo(bv, Undo::INSERT,
                                bv->text->cursor.par(),
-                               bv->text->cursor.par()->next());
+                               boost::next(bv->text->cursor.par()));
                        pasteSelection(bv);
                        updateLocal(bv, INIT);
                        break;
@@ -1209,8 +1192,6 @@ Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
                if (!the_locking_inset) {
                        if (bv->fitCursor())
                                updateLocal(bv, FULL);
-                       if (locked)
-                               showInsetCursor(bv);
                }
        } else
                bv->unlockInset(this);
@@ -1329,65 +1310,34 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv, bool reinit) c
 }
 
 
-void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
-{
-       if (the_locking_inset) {
-               the_locking_inset->getCursorPos(bv, x, y);
-               return;
-       }
-       x = cursor_.x() - top_x;
-       y = cursor_.y();
-}
-
-
-void InsetTabular::toggleInsetCursor(BufferView * bv)
+void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
 {
-       if (nodraw()) {
-               if (isCursorVisible())
-                       bv->hideLockedInsetCursor();
-               return;
-       }
        if (the_locking_inset) {
-               the_locking_inset->toggleInsetCursor(bv);
+               the_locking_inset->getCursor(bv, x, y);
                return;
        }
 
-       LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
+       x = cursor_.x();
+       y = cursor_.y() + InsetTabular::y();
 
-       int const asc = font_metrics::maxAscent(font);
-       int const desc = font_metrics::maxDescent(font);
-
-       if (isCursorVisible())
-               bv->hideLockedInsetCursor();
-       else
-               bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
-       toggleCursorVisible();
+       // Fun stuff
+       int desc = tabular->GetDescentOfRow(actrow);
+       y += desc;
+       int ascdesc = tabular->GetAscentOfRow(actrow) + desc;
+       y -= ascdesc / 2;
+       y += ADD_TO_HEIGHT * 2;
+       y += TEXT_TO_INSET_OFFSET;
 }
 
 
-void InsetTabular::showInsetCursor(BufferView * bv, bool show)
+void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
 {
-       if (nodraw())
+       if (the_locking_inset) {
+               the_locking_inset->getCursorPos(bv, x, y);
                return;
-       if (!isCursorVisible()) {
-               LyXFont font; // = GetFont(par, cursor.pos);
-
-               int const asc = font_metrics::maxAscent(font);
-               int const desc = font_metrics::maxDescent(font);
-               bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
-               if (show)
-                       bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
-               setCursorVisible(true);
-       }
-}
-
-
-void InsetTabular::hideInsetCursor(BufferView * bv)
-{
-       if (isCursorVisible()) {
-               bv->hideLockedInsetCursor();
-               setCursorVisible(false);
        }
+       x = cursor_.x() - top_x;
+       y = cursor_.y();
 }
 
 
@@ -1631,7 +1581,7 @@ bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
                ++actcell;
        }
        if (lock) {
-               bool rtl = tabular->GetCellInset(actcell)->paragraph()->
+               bool rtl = tabular->GetCellInset(actcell)->paragraphs.begin()->
                        isRightToLeftPar(bv->buffer()->params);
                activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
        }
@@ -1660,7 +1610,7 @@ bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
                --actcell;
        }
        if (lock) {
-               bool rtl = tabular->GetCellInset(actcell)->paragraph()->
+               bool rtl = tabular->GetCellInset(actcell)->paragraphs.begin()->
                        isRightToLeftPar(bv->buffer()->params);
                activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
        }
@@ -1678,7 +1628,7 @@ void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
        if (hasSelection()) {
                setUndo(bv, Undo::EDIT,
                        bv->text->cursor.par(),
-                       bv->text->cursor.par()->next());
+                       boost::next(bv->text->cursor.par()));
                bool const frozen = undo_frozen;
                if (!frozen)
                        freezeUndo();
@@ -1802,7 +1752,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
        }
        setUndo(bv, Undo::FINISH,
                bv->text->cursor.par(),
-               bv->text->cursor.par()->next());
+               boost::next(bv->text->cursor.par()));
 
        int row =  tabular->row_of_cell(actcell);
        int column = tabular->column_of_cell(actcell);
@@ -1999,7 +1949,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                tabular->SetMultiColumn(bv->buffer(), actcell, 1);
                                updateLocal(bv, CELL);
                        }
-                       return;
+                       break;
                }
                // we have a selection so this means we just add all this
                // cells to form a multicolumn cell
@@ -2104,6 +2054,9 @@ void InsetTabular::tabularFeatures(BufferView * bv,
        case LyXTabular::LAST_ACTION:
                break;
        }
+
+       InsetTabularMailer mailer(*this);
+       mailer.updateDialog(bv);
 }
 
 
@@ -2119,7 +2072,7 @@ bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, mouse_button
        }
        //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
        //inset_y = cursor.y();
-       inset->edit(bv, x,  y, button);
+       inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x,  y, button));
        if (!the_locking_inset)
                return false;
        updateLocal(bv, CELL);
@@ -2284,8 +2237,6 @@ FuncStatus InsetTabular::getStatus(string const & what) const
        case LyXTabular::SET_MPWIDTH:
        case LyXTabular::SET_SPECIAL_COLUMN:
        case LyXTabular::SET_SPECIAL_MULTI:
-               return status.disabled(true);
-
        case LyXTabular::APPEND_ROW:
        case LyXTabular::APPEND_COLUMN:
        case LyXTabular::DELETE_ROW:
@@ -2561,18 +2512,10 @@ void InsetTabular::getSelection(int & srow, int & erow,
 }
 
 
-Paragraph * InsetTabular::firstParagraph() const
-{
-       if (the_locking_inset)
-               return the_locking_inset->firstParagraph();
-       return 0;
-}
-
-
-Paragraph * InsetTabular::getFirstParagraph(int i) const
+ParagraphList * InsetTabular::getParagraphs(int i) const
 {
        return (i < tabular->GetNumberOfCells())
-               ? tabular->GetCellInset(i)->getFirstParagraph(0)
+               ? tabular->GetCellInset(i)->getParagraphs(0)
                : 0;
 }
 
@@ -2621,7 +2564,7 @@ InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
        // otherwise we have to lock the next inset and ask for it's selecttion
        UpdatableInset * inset =
                static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
-       inset->edit(bv, 0,  0, mouse_button::none);
+       inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
        WordLangTuple word(selectNextWordInt(bv, value));
        nodraw(false);
        if (!word.word().empty())
@@ -2647,7 +2590,7 @@ WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) co
        // otherwise we have to lock the next inset and ask for it's selecttion
        UpdatableInset * inset =
                static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
-       inset->edit(bv);
+       inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
        return selectNextWordInt(bv, value);
 }
 
@@ -2713,6 +2656,7 @@ bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
 bool InsetTabular::searchForward(BufferView * bv, string const & str,
                                 bool cs, bool mw)
 {
+       int cell = 0;
        if (the_locking_inset) {
                if (the_locking_inset->searchForward(bv, str, cs, mw)) {
                        updateLocal(bv, CELL);
@@ -2720,16 +2664,16 @@ bool InsetTabular::searchForward(BufferView * bv, string const & str,
                }
                if (tabular->IsLastCell(actcell))
                        return false;
-               ++actcell;
+               cell = actcell + 1;
        }
-       InsetText * inset = tabular->GetCellInset(actcell);
+       InsetText * inset = tabular->GetCellInset(cell);
        if (inset->searchForward(bv, str, cs, mw)) {
                updateLocal(bv, FULL);
                return true;
        }
-       while (!tabular->IsLastCell(actcell)) {
-               ++actcell;
-               inset = tabular->GetCellInset(actcell);
+       while (!tabular->IsLastCell(cell)) {
+               ++cell;
+               inset = tabular->GetCellInset(cell);
                if (inset->searchForward(bv, str, cs, mw)) {
                        updateLocal(bv, FULL);
                        return true;
@@ -2742,18 +2686,18 @@ bool InsetTabular::searchForward(BufferView * bv, string const & str,
 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
                               bool cs, bool mw)
 {
+       int cell = tabular->GetNumberOfCells();
        if (the_locking_inset) {
                if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
                        updateLocal(bv, CELL);
                        return true;
                }
+               cell = actcell;
        }
-       if (!locked)
-               actcell = tabular->GetNumberOfCells();
 
-       while (actcell) {
-               --actcell;
-               InsetText * inset = tabular->GetCellInset(actcell);
+       while (cell) {
+               --cell;
+               InsetText * inset = tabular->GetCellInset(cell);
                if (inset->searchBackward(bv, str, cs, mw)) {
                        updateLocal(bv, CELL);
                        return true;
@@ -2852,8 +2796,8 @@ bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
                        if (cols < columns) {
                                InsetText * ti = loctab->GetCellInset(cell);
                                LyXFont const font = ti->getLyXText(bv)->
-                                       getFont(bv->buffer(), ti->paragraph(), 0);
-                               ti->setText(buf.substr(op, p-op), font);
+                                       getFont(bv->buffer(), &*ti->paragraphs.begin(), 0);
+                               ti->setText(buf.substr(op, p - op), font);
                                ++cols;
                                ++cell;
                        }
@@ -2863,8 +2807,8 @@ bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
                        if (cols < columns) {
                                InsetText * ti = loctab->GetCellInset(cell);
                                LyXFont const font = ti->getLyXText(bv)->
-                                       getFont(bv->buffer(), ti->paragraph(), 0);
-                               ti->setText(buf.substr(op, p-op), font);
+                                       getFont(bv->buffer(), &*ti->paragraphs.begin(), 0);
+                               ti->setText(buf.substr(op, p - op), font);
                        }
                        cols = ocol;
                        ++row;
@@ -2879,8 +2823,8 @@ bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
        if ((cell < cells) && (op < len)) {
                InsetText * ti = loctab->GetCellInset(cell);
                LyXFont const font = ti->getLyXText(bv)->
-                       getFont(bv->buffer(), ti->paragraph(), 0);
-               ti->setText(buf.substr(op, len-op), font);
+                       getFont(bv->buffer(), &*ti->paragraphs.begin(), 0);
+               ti->setText(buf.substr(op, len - op), font);
        }
 
        return true;
@@ -2914,10 +2858,14 @@ string const InsetTabularMailer::inset2string() const
 
 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
 {
-       istringstream data(in);
+       istringstream data(STRCONV(in));
        LyXLex lex(0,0);
        lex.setStream(data);
 
+#warning CHECK verify that this is a sane value to return.
+       if (in.empty())
+               return -1;
+
        if (lex.isOK()) {
                lex.next();
                string const token = lex.getString();
@@ -2958,8 +2906,7 @@ int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
 }
 
 
-string const
-InsetTabularMailer::params2string(InsetTabular const & inset)
+string const InsetTabularMailer::params2string(InsetTabular const & inset)
 {
        BufferView * const bv = inset.view();
        Buffer const * const buffer = bv ? bv->buffer() : 0;
@@ -2970,6 +2917,5 @@ InsetTabularMailer::params2string(InsetTabular const & inset)
        data << name_ << " \\active_cell " << inset.getActCell() << '\n';
        inset.write(buffer, data);
        data << "\\end_inset\n";
-
-       return data.str();
+       return STRCONV(data.str());
 }