]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
more cleanup:
[lyx.git] / src / insets / insettabular.C
index ba2e241cfc78541b6cf5c7fb20f88824cf3dafa3..849bf95079b7966784bcc9eb737913a072acdc6f 100644 (file)
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
 #include "frontends/Selection.h"
-#include "frontends/nullpainter.h"
 
 #include <sstream>
 #include <iostream>
 #include <limits>
 
-using lyx::docstring;
-using lyx::odocstream;
-using lyx::Point;
 
-using lyx::cap::dirtyTabularStack;
-using lyx::cap::tabularStackDirty;
+namespace lyx {
 
-using lyx::graphics::PreviewLoader;
+using cap::dirtyTabularStack;
+using cap::tabularStackDirty;
 
-using lyx::support::ltrim;
+using graphics::PreviewLoader;
 
-using lyx::frontend::Painter;
-using lyx::frontend::Clipboard;
+using support::ltrim;
+
+using frontend::Painter;
+using frontend::Clipboard;
 
 using boost::shared_ptr;
 
@@ -72,7 +70,7 @@ using std::ostringstream;
 using std::swap;
 using std::vector;
 
-namespace Alert = lyx::frontend::Alert;
+namespace Alert = frontend::Alert;
 
 
 namespace {
@@ -248,7 +246,7 @@ void InsetTabular::read(Buffer const & buf, LyXLex & lex)
 }
 
 
-void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
+bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
        //      mi.base.textwidth << "\n";
@@ -302,7 +300,9 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.asc = tabular.getAscentOfRow(0);
        dim.des = tabular.getHeightOfTabular() - dim.asc;
        dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
+       bool const changed = dim_ != dim;
        dim_ = dim;
+       return changed;
 }
 
 
@@ -313,9 +313,6 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
        //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
        BufferView * bv = pi.base.bv;
 
-       static lyx::frontend::NullPainter nop;
-       static PainterInfo nullpi(bv, nop);
-
        resetPos(bv->cursor());
 
        x += scx_;
@@ -339,8 +336,10 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
                            || nx > bv->workWidth()
                            || y + d < 0
                            || y - a > bv->workHeight()) {
-                               cell(idx)->draw(nullpi, cx, y);
-                               drawCellLines(nop, nx, y, i, idx, pi.erased_);
+                               pi.pain.setDrawingEnabled(false);
+                               cell(idx)->draw(pi, cx, y);
+                               drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
+                               pi.pain.setDrawingEnabled(true);
                        } else {
                                cell(idx)->draw(pi, cx, y);
                                drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
@@ -496,12 +495,12 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_MOUSE_PRESS:
                //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
 
-               if (cmd.button() == mouse_button::button1
-                   || cmd.button() == mouse_button::button3) {
+               if (cmd.button() == mouse_button::button1 
+                   || (cmd.button() == mouse_button::button3 
+                       && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
                        cur.selection() = false;
                        setCursorFromCoordinates(cur, cmd.x, cmd.y);
-                       cur.resetAnchor();
-                       bvcur = cur;
+                       cur.bv().mouseSetCursor(cur);
                        break;
                }
 
@@ -576,7 +575,7 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
                                cur.idx() = tabular.getCellBelow(cur.idx());
                                cur.pit() = 0;
                                cur.pos() = cell(cur.idx())->getText(0)->x2pos(
-                                       cur.pit(), 0, cur.targetX());
+                                       cur.bv(), cur.pit(), 0, cur.targetX());
                        }
                if (sl == cur.top()) {
                        // we trick it to go to the RIGHT after leaving the
@@ -599,6 +598,7 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
                                cur.pit() = cur.lastpit();
                                LyXText const * text = cell(cur.idx())->getText(0);
                                cur.pos() = text->x2pos(
+                                       cur.bv(),
                                        cur.pit(),
                                        text->paragraphs().back().rows().size()-1,
                                        cur.targetX());
@@ -653,17 +653,17 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_TABULAR_FEATURE:
-               if (!tabularFeatures(cur, lyx::to_utf8(cmd.argument())))
+               if (!tabularFeatures(cur, to_utf8(cmd.argument())))
                        cur.undispatched();
                break;
 
        // insert file functions
        case LFUN_FILE_INSERT_ASCII_PARA:
        case LFUN_FILE_INSERT_ASCII: {
-               // FIXME: We don't know the encoding of filenames
-               string const tmpstr = getContentsOfAsciiFile(&cur.bv(), lyx::to_utf8(cmd.argument()), false);
+               // FIXME UNICODE
+               string const tmpstr = getContentsOfAsciiFile(&cur.bv(), to_utf8(cmd.argument()), false);
                // FIXME: We don't know the encoding of the file
-               if (!tmpstr.empty() && !insertAsciiString(cur.bv(), lyx::from_utf8(tmpstr), false))
+               if (!tmpstr.empty() && !insertAsciiString(cur.bv(), from_utf8(tmpstr), false))
                        cur.undispatched();
                break;
        }
@@ -708,7 +708,7 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
                        break;
                // pass to InsertAsciiString, but
                // only if we have multi-cell content
-               if (clip.find_first_of(lyx::from_ascii("\t\n")) != docstring::npos) {
+               if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
                        if (insertAsciiString(cur.bv(), clip, false)) {
                                // content has been replaced,
                                // so cursor might be invalid
@@ -800,7 +800,7 @@ bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
                int i = 0;
                for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
                        string const tmp = tabularFeature[i].feature;
-                       if (tmp == lyx::to_utf8(cmd.argument()).substr(0, tmp.length())) {
+                       if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
                                action = tabularFeature[i].action;
                                break;
                        }
@@ -812,7 +812,7 @@ bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
                }
 
                string const argument
-                       = ltrim(lyx::to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
+                       = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
 
                row_type sel_row_start = 0;
                row_type sel_row_end = 0;
@@ -1252,7 +1252,7 @@ void InsetTabular::resetPos(LCursor & cur) const
                        scx_ = 0;
        }
 
-       cur.needsUpdate();
+       cur.updateFlags(Update::Force | Update::FitCursor);
 
        InsetTabularMailer(*this).updateDialog(&bv);
 }
@@ -1793,14 +1793,14 @@ bool InsetTabular::copySelection(LCursor & cur)
        paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
                                    true, true);
 
-       lyx::odocstringstream os;
+       odocstringstream os;
        OutputParams const runparams;
        paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
        theClipboard().put(os.str());
        // mark tabular stack dirty
        // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
        // when we (hopefully) have a one-for-all paste mechanism.
-       lyx::cap::dirtyTabularStack(true);
+       cap::dirtyTabularStack(true);
 
        return true;
 }
@@ -1875,7 +1875,7 @@ bool InsetTabular::isRightToLeft(LCursor & cur) const
 {
        BOOST_ASSERT(cur.depth() > 1);
        Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
-       LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
+       pos_type const parentpos = cur[cur.depth() - 2].pos();
        return parentpar.getFontSettings(cur.bv().buffer()->params(),
                                         parentpos).language()->rightToLeft();
 }
@@ -1915,6 +1915,20 @@ void InsetTabular::setChange(Change const & change)
 }
 
 
+void InsetTabular::acceptChanges()
+{
+       for (idx_type idx = 0; idx < nargs(); ++idx)
+               cell(idx)->acceptChanges();
+}
+
+
+void InsetTabular::rejectChanges()
+{
+       for (idx_type idx = 0; idx < nargs(); ++idx)
+               cell(idx)->rejectChanges();
+}
+
+
 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
 {
        return tabular.getPWidth(cell).zero();
@@ -1927,6 +1941,8 @@ bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
        if (buf.length() <= 0)
                return true;
 
+       Buffer const & buffer = *bv.buffer();
+
        col_type cols = 1;
        row_type rows = 1;
        col_type maxCols = 1;
@@ -1934,7 +1950,7 @@ bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
        docstring::size_type p = 0;
 
        while (p < len &&
-              (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos) {
+              (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
                switch (buf[p]) {
                case '\t':
                        ++cols;
@@ -1955,7 +1971,7 @@ bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
        row_type row = 0;
        if (usePaste) {
                paste_tabular.reset(
-                       new LyXTabular(bv.buffer()->params(), rows, maxCols));
+                       new LyXTabular(buffer.params(), rows, maxCols));
                loctab = paste_tabular.get();
                cols = 0;
                dirtyTabularStack(true);
@@ -1974,7 +1990,7 @@ bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
        col_type const columns = loctab->columns();
 
        while (cell < cells && p < len && row < rows &&
-              (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos)
+              (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
        {
                if (p >= len)
                        break;
@@ -1983,11 +1999,10 @@ bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
                        // we can only set this if we are not too far right
                        if (cols < columns) {
                                shared_ptr<InsetText> inset = loctab->getCellInset(cell);
-                               inset->setViewCache(&bv);
                                Paragraph & par = inset->text_.getPar(0);
-                               LyXFont const font = inset->text_.getFont(par, 0);
+                               LyXFont const font = inset->text_.getFont(buffer, par, 0);
                                inset->setText(buf.substr(op, p - op), font,
-                                              bv.buffer()->params().trackChanges);
+                                              buffer.params().trackChanges);
                                ++cols;
                                ++cell;
                        }
@@ -1996,11 +2011,10 @@ bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
                        // we can only set this if we are not too far right
                        if (cols < columns) {
                                shared_ptr<InsetText> inset = tabular.getCellInset(cell);
-                               inset->setViewCache(&bv);
                                Paragraph & par = inset->text_.getPar(0);
-                               LyXFont const font = inset->text_.getFont(par, 0);
+                               LyXFont const font = inset->text_.getFont(buffer, par, 0);
                                inset->setText(buf.substr(op, p - op), font,
-                                              bv.buffer()->params().trackChanges);
+                                              buffer.params().trackChanges);
                        }
                        cols = ocol;
                        ++row;
@@ -2014,11 +2028,10 @@ bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
        // check for the last cell if there is no trailing '\n'
        if (cell < cells && op < len) {
                shared_ptr<InsetText> inset = loctab->getCellInset(cell);
-               inset->setViewCache(&bv);
                Paragraph & par = inset->text_.getPar(0);
-               LyXFont const font = inset->text_.getFont(par, 0);
+               LyXFont const font = inset->text_.getFont(buffer, par, 0);
                inset->setText(buf.substr(op, len - op), font,
-                              bv.buffer()->params().trackChanges);
+                       buffer.params().trackChanges);
        }
        return true;
 }
@@ -2092,3 +2105,6 @@ string const InsetTabularMailer::params2string(InsetTabular const & inset)
        data << "\\end_inset\n";
        return data.str();
 }
+
+
+} // namespace lyx