]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
replace nextToken by more appropriate lex methods.
[lyx.git] / src / insets / insettabular.C
index a0c75ba21bec3bb7464bb13827f451dd7572e431..fd5c283bf5ec97c75b41791d0ca0e9d53f7248d0 100644 (file)
@@ -204,10 +204,10 @@ void InsetTabular::read(Buffer const & buf, LyXLex & lex)
        if (old_format)
                return;
 
-       lex.nextToken();
+       lex.next();
        string token = lex.getString();
        while (lex.isOK() && (token != "\\end_inset")) {
-               lex.nextToken();
+               lex.next();
                token = lex.getString();
        }
        if (token != "\\end_inset") {
@@ -235,8 +235,10 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                        ++cell;
                        Dimension dim;
                        MetricsInfo m = mi;
-                       m.base.textwidth =
-                               tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
+                       LyXLength p_width = tabular.column_info[j].p_width;
+                       if (!p_width.zero()) {
+                               m.base.textwidth = p_width.inPixels(mi.base.textwidth);
+                       }
                        tabular.getCellInset(cell).metrics(m, dim);
                        maxAsc  = max(maxAsc, dim.asc);
                        maxDesc = max(maxDesc, dim.des);
@@ -268,12 +270,11 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
        for (int i = 0; i < tabular.rows(); ++i) {
                int nx = x;
                idx = tabular.getCellNumber(i, 0);
-               if (y + tabular.getDescentOfRow(i) <= 0 &&
-                         y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
-               {
-                       y += tabular.getDescentOfRow(i) +
-                                       tabular.getAscentOfRow(i + 1) +
-                                       tabular.getAdditionalHeight(i + 1);
+               if (y + tabular.getDescentOfRow(i) <= 0
+                   && y - tabular.getAscentOfRow(i) < pi.pain.paperHeight()) {
+                       y += tabular.getDescentOfRow(i)
+                               + tabular.getAscentOfRow(i + 1)
+                               + tabular.getAdditionalHeight(i + 1);
                        continue;
                }
                for (int j = 0; j < tabular.columns(); ++j) {
@@ -375,6 +376,7 @@ void InsetTabular::edit(LCursor & cur, bool left)
        lyxerr << "InsetTabular::edit: " << this << endl;
        finishUndo();
        int cell;
+       cur.push(*this);
        if (left) {
                if (isRightToLeft(cur))
                        cell = tabular.getLastCellInRow(0);
@@ -387,9 +389,9 @@ void InsetTabular::edit(LCursor & cur, bool left)
                        cell = tabular.getNumberOfCells() - 1;
        }
        cur.selection() = false;
-       resetPos(cur);
-       cur.bv().fitCursor();
-       cur.push(*this);
+       // this accesses the position cache before it is initialized
+       //resetPos(cur);
+       //cur.bv().fitCursor();
        cur.idx() = cell;
 }
 
@@ -400,7 +402,7 @@ InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
        cur.selection() = false;
        cur.push(const_cast<InsetTabular&>(*this));
        return setPos(cur, x, y);
-       //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
+       //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(actcell);
 }
 
 
@@ -414,31 +416,38 @@ void InsetTabular::priv_dispatch(LCursor & cur, FuncRequest & cmd)
        switch (cmd.action) {
 
        case LFUN_MOUSE_PRESS:
-               // we'll pop up the table dialog on release
-               if (cmd.button() == mouse_button::button3)
+               lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
+
+               if (cmd.button() == mouse_button::button1) {
+                       cur.selection() = false;
+                       setPos(cur, cmd.x, cmd.y);
+                       cur.resetAnchor();
+                       bvcur = cur;
                        break;
-               cur.selection() = false;
-               setPos(cur, cmd.x, cmd.y);
-               cur.resetAnchor();
-               bvcur = cur;
+               }
+
                //if (cmd.button() == mouse_button::button2)
                //      dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
-               //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
+
+               // we'll pop up the table dialog on release
+               if (cmd.button() == mouse_button::button3)
+                       break;
                break;
 
        case LFUN_MOUSE_MOTION:
-               if (cmd.button() != mouse_button::button1)
-                       break;
-               // ignore motions deeper nested than the real anchor
-               if (bvcur.selection() && bvcur.anchor_.size() < cur.size())
-                       break;
-               setPos(cur, cmd.x, cmd.y);
-               bvcur.setCursor(cur, true);
-               //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
+               lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
+               if (cmd.button() == mouse_button::button1) {
+                       // only accept motions to places not deeper nested than the real anchor
+                       if (bvcur.anchor_.hasPart(cur)) {
+                               setPos(cur, cmd.x, cmd.y);
+                               bvcur.setCursor(cur);
+                               bvcur.selection() = true;
+                       }
+               }
                break;
 
        case LFUN_MOUSE_RELEASE:
-               //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
+               lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
                if (cmd.button() == mouse_button::button3)
                        InsetTabularMailer(*this).showDialog(&cur.bv());
                break;
@@ -527,7 +536,7 @@ void InsetTabular::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                int actcol = tabular.column_of_cell(actcell);
                int column = actcol;
                if (cur.bv().top_y() + cur.bv().painter().paperHeight()
-                               < yo_ + tabular.getHeightOfTabular())
+                               < yo() + tabular.getHeightOfTabular())
                {
                        cur.bv().scrollDocView(
                                cur.bv().top_y() + cur.bv().painter().paperHeight());
@@ -543,10 +552,10 @@ void InsetTabular::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                //if (hasSelection())
                //      cur.selection() = false;
                int column = tabular.column_of_cell(cur.idx());
-               if (yo_ < 0) {
+               if (yo() < 0) {
                        cur.bv().scrollDocView(
                                cur.bv().top_y() - cur.bv().painter().paperHeight());
-                       if (yo_ > 0)
+                       if (yo() > 0)
                                cur.idx() = column;
                        else
                                cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
@@ -883,7 +892,7 @@ int InsetTabular::latex(Buffer const & buf, ostream & os,
 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
                        OutputParams const & runparams) const
 {
-       int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
+       int dp = runparams.linelen ? runparams.depth : 0;
        return tabular.plaintext(buf, os, runparams, dp, false, 0);
 }
 
@@ -955,6 +964,7 @@ void InsetTabular::getCursorPos(LCursor const & cur, int & x, int & y) const
 
 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
 {
+       lyxerr << "# InsetTabular::setPos()  x=" << x << " y=" << y << endl;
        int idx_min = 0;
        int dist_min = 1000000;
        for (idx_type i = 0; i < nargs(); ++i) {
@@ -981,7 +991,7 @@ int InsetTabular::getCellXPos(int cell) const
        for (; c < cell; ++c)
                lx += tabular.getWidthOfColumn(c);
 
-       return lx - tabular.getWidthOfColumn(cell) + xo_;
+       return lx - tabular.getWidthOfColumn(cell) + xo();
 }
 
 
@@ -1006,8 +1016,8 @@ void InsetTabular::resetPos(LCursor & cur) const
                scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
        } else if (cursorx_ - offset < 20) {
                scroll(bv, 20 - cursorx_ + offset);
-       } else if (scroll() && xo_ > 20 &&
-                  xo_ + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
+       } else if (scroll() && xo() > 20 &&
+                  xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
                scroll(bv, old_x - cursorx_);
        }
 
@@ -1552,9 +1562,13 @@ void InsetTabular::cutSelection(LCursor & cur)
 }
 
 
-bool InsetTabular::isRightToLeft(LCursor & cur)
+bool InsetTabular::isRightToLeft(LCursor & cur) const
 {
-       return cur.bv().getParentLanguage(this)->RightToLeft();
+       BOOST_ASSERT(cur.size() > 1);
+       Paragraph const & parentpar = cur[cur.size() - 2].paragraph();
+       LCursor::pos_type const parentpos = cur[cur.size() - 2].pos();
+       return parentpar.getFontSettings(cur.bv().buffer()->params(),
+                                        parentpos).language()->RightToLeft();
 }
 
 
@@ -1683,7 +1697,8 @@ bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
                        // we can only set this if we are not too far right
                        if (cols < columns) {
                                InsetText & inset = loctab->getCellInset(cell);
-                               LyXFont const font = inset.text_.getFont(0, 0);
+                               Paragraph & par = inset.text_.getPar(0);
+                               LyXFont const font = inset.text_.getFont(par, 0);
                                inset.setText(buf.substr(op, p - op), font);
                                ++cols;
                                ++cell;
@@ -1693,7 +1708,8 @@ bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
                        // we can only set this if we are not too far right
                        if (cols < columns) {
                                InsetText & inset = tabular.getCellInset(cell);
-                               LyXFont const font = inset.text_.getFont(0, 0);
+                               Paragraph & par = inset.text_.getPar(0);
+                               LyXFont const font = inset.text_.getFont(par, 0);
                                inset.setText(buf.substr(op, p - op), font);
                        }
                        cols = ocol;
@@ -1708,7 +1724,8 @@ bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
        // check for the last cell if there is no trailing '\n'
        if (cell < cells && op < len) {
                InsetText & inset = loctab->getCellInset(cell);
-               LyXFont const font = inset.text_.getFont(0, 0);
+               Paragraph & par = inset.text_.getPar(0);
+               LyXFont const font = inset.text_.getFont(par, 0);
                inset.setText(buf.substr(op, len - op), font);
        }
        return true;