]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
Enable the external inset to handle unknown templates gracefully.
[lyx.git] / src / insets / insettabular.C
index b92763c79f8262c48d6c2ffe137ffa3c9128d46a..0007786eb3eedbcb404262801403fe7acd21153e 100644 (file)
@@ -31,7 +31,7 @@
 #include "ParagraphParameters.h"
 #include "undo_funcs.h"
 #include "WordLangTuple.h"
-#include "Lsstream.h"
+#include "metricsinfo.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
@@ -151,7 +151,7 @@ bool InsetTabular::hasPasteBuffer() const
 
 
 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
-       : buffer(&buf)
+       : buffer_(&buf)
 {
        if (rows <= 0)
                rows = 1;
@@ -174,12 +174,11 @@ InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
 }
 
 
-InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
-                                                  bool same_id)
-       : UpdatableInset(tab, same_id), buffer(&buf)
+InsetTabular::InsetTabular(InsetTabular const & tab)
+       : UpdatableInset(tab), buffer_(tab.buffer_)
 {
-       tabular.reset(new LyXTabular(buf.params,
-                                    this, *(tab.tabular), same_id));
+       tabular.reset(new LyXTabular(buffer_->params,
+                                    this, *(tab.tabular)));
        the_locking_inset = 0;
        old_locking_inset = 0;
        locked = false;
@@ -201,15 +200,21 @@ InsetTabular::~InsetTabular()
 }
 
 
-Inset * InsetTabular::clone(Buffer const & buf, bool same_id) const
+Inset * InsetTabular::clone() const
 {
-       return new InsetTabular(*this, buf, same_id);
+       return new InsetTabular(*this);
 }
 
 
 BufferView * InsetTabular::view() const
 {
-       return buffer->getUser();
+       return buffer_->getUser();
+}
+
+
+void InsetTabular::buffer(Buffer * b)
+{
+       buffer_ = b;
 }
 
 
@@ -244,49 +249,48 @@ void InsetTabular::read(Buffer const * buf, LyXLex & lex)
 }
 
 
-void InsetTabular::dimension(BufferView *, LyXFont const &,
+void InsetTabular::metrics(MetricsInfo &,
        Dimension & dim) const
 {
-       dim.a = tabular->GetAscentOfRow(0);
-       dim.d = tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
-       dim.w = tabular->GetWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
+       dim.asc = tabular->GetAscentOfRow(0);
+       dim.des = tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
+       dim.wid = tabular->GetWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
 }
 
 
-void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
-                       float & x) const
+void InsetTabular::draw(PainterInfo & pi, int x, int y) const
 {
        if (nodraw()) {
                need_update = FULL;
                return;
        }
 
-       Painter & pain = bv->painter();
+       BufferView * bv = pi.base.bv;
        int i;
        int j;
        int nx;
 
 #if 0
-       UpdatableInset::draw(bv, font, baseline, x);
+       UpdatableInset::draw(pi, x, y);
 #else
        if (!owner())
-               x += static_cast<float>(scroll());
+               x += scroll();
 #endif
 
-       top_x = int(x);
-       top_baseline = baseline;
+       top_x = x;
+       top_baseline = y;
        x += ADD_TO_TABULAR_WIDTH;
 
        int cell = 0;
-       float cx;
+       int cx;
        first_visible_cell = -1;
        for (i = 0; i < tabular->rows(); ++i) {
-               nx = int(x);
+               nx = x;
                cell = tabular->GetCellNumber(i, 0);
-               if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
-                       (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
+               if (!((y + tabular->GetDescentOfRow(i)) > 0) &&
+                       (y - tabular->GetAscentOfRow(i)) < pi.pain.paperHeight())
                {
-               baseline += tabular->GetDescentOfRow(i) +
+               y += tabular->GetDescentOfRow(i) +
                                tabular->GetAscentOfRow(i + 1) +
                                tabular->GetAdditionalHeight(i + 1);
                        continue;
@@ -300,26 +304,36 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                        if (first_visible_cell < 0)
                                first_visible_cell = cell;
                        if (hasSelection()) {
-                               drawCellSelection(pain, nx, baseline, i, j, cell);
+                               drawCellSelection(pi.pain, nx, y, i, j, cell);
                        }
 
-                       tabular->GetCellInset(cell)->draw(bv, font, baseline, cx);
-                       drawCellLines(pain, nx, baseline, i, cell);
+                       tabular->GetCellInset(cell)->draw(pi, cx, y);
+                       drawCellLines(pi.pain, nx, y, i, cell);
                        nx += tabular->GetWidthOfColumn(cell);
                        ++cell;
                }
-               baseline += tabular->GetDescentOfRow(i) +
+
+// Would be nice, but for some completely unfathomable reason,
+// on a col resize to a new fixed width, even though the insettexts
+// are resized, the cell isn't, but drawing all cells in a tall table
+// has the desired effect somehow. Complete dark magic.
+#if 0
+               // avoiding drawing the rest of a long table is
+               // a pretty big speedup
+               if (y > bv->workHeight())
+                       break;
+#endif
+
+               y += tabular->GetDescentOfRow(i) +
                        tabular->GetAscentOfRow(i + 1) +
                        tabular->GetAdditionalHeight(i + 1);
        }
 
-       x -= ADD_TO_TABULAR_WIDTH;
-       x += width(bv, font);
        need_update = NONE;
 }
 
 
-void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
+void InsetTabular::drawCellLines(Painter & pain, int x, int y,
                                 int row, int cell) const
 {
        int x2 = x + tabular->GetWidthOfColumn(cell);
@@ -327,34 +341,34 @@ void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
 
        if (!tabular->topAlreadyDrawn(cell)) {
                on_off = !tabular->TopLine(cell);
-               pain.line(x, baseline - tabular->GetAscentOfRow(row),
-                         x2, baseline -  tabular->GetAscentOfRow(row),
+               pain.line(x, y - tabular->GetAscentOfRow(row),
+                         x2, y -  tabular->GetAscentOfRow(row),
                          on_off ? LColor::tabularonoffline : LColor::tabularline,
                          on_off ? Painter::line_onoffdash : Painter::line_solid);
        }
        on_off = !tabular->BottomLine(cell);
-       pain.line(x, baseline + tabular->GetDescentOfRow(row),
-                 x2, baseline + tabular->GetDescentOfRow(row),
+       pain.line(x, y + tabular->GetDescentOfRow(row),
+                 x2, y + tabular->GetDescentOfRow(row),
                  on_off ? LColor::tabularonoffline : LColor::tabularline,
                  on_off ? Painter::line_onoffdash : Painter::line_solid);
        if (!tabular->leftAlreadyDrawn(cell)) {
                on_off = !tabular->LeftLine(cell);
-               pain.line(x, baseline -  tabular->GetAscentOfRow(row),
-                         x, baseline +  tabular->GetDescentOfRow(row),
+               pain.line(x, y -  tabular->GetAscentOfRow(row),
+                         x, y +  tabular->GetDescentOfRow(row),
                          on_off ? LColor::tabularonoffline : LColor::tabularline,
                          on_off ? Painter::line_onoffdash : Painter::line_solid);
        }
        on_off = !tabular->RightLine(cell);
        pain.line(x2 - tabular->GetAdditionalWidth(cell),
-                 baseline -  tabular->GetAscentOfRow(row),
+                 y -  tabular->GetAscentOfRow(row),
                  x2 - tabular->GetAdditionalWidth(cell),
-                 baseline +  tabular->GetDescentOfRow(row),
+                 y +  tabular->GetDescentOfRow(row),
                  on_off ? LColor::tabularonoffline : LColor::tabularline,
                  on_off ? Painter::line_onoffdash : Painter::line_solid);
 }
 
 
-void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
+void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
                                     int row, int column, int cell) const
 {
        lyx::Assert(hasSelection());
@@ -375,7 +389,7 @@ void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
        if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
                int w = tabular->GetWidthOfColumn(cell);
                int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row)-1;
-               pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row) + 1,
+               pain.fillRectangle(x, y - tabular->GetAscentOfRow(row) + 1,
                                   w, h, LColor::selection);
        }
 }
@@ -1752,6 +1766,11 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                bool const update = (tmplen != vallen);
                tabular->SetColumnPWidth(actcell, vallen);
                if (update) {
+                       // We need this otherwise we won't resize
+                       // the insettext of the active cell (if any)
+                       // until later (see InsetText::do_resize)
+                       unlockInsetInInset(bv, the_locking_inset);
+
                        int cell;
                        for (int i = 0; i < tabular->rows(); ++i) {
                                cell = tabular->GetCellNumber(i,column);
@@ -1776,6 +1795,11 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                bool const update = (tmplen != vallen);
                tabular->SetMColumnPWidth(actcell, vallen);
                if (update) {
+                       // We need this otherwise we won't resize
+                       // the insettext of the active cell (if any)
+                       // until later (see InsetText::do_resize)
+                       unlockInsetInInset(bv, the_locking_inset);
+
                        for (int i = 0; i < tabular->rows(); ++i) {
                                tabular->GetCellInset(tabular->GetCellNumber(i, column))->
                                        resizeLyXText(bv);
@@ -2780,7 +2804,7 @@ 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->paragraphs.begin(), 0);
+                                       getFont(bv->buffer(), ti->paragraphs.begin(), 0);
                                ti->setText(buf.substr(op, p - op), font);
                                ++cols;
                                ++cell;
@@ -2791,7 +2815,7 @@ 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->paragraphs.begin(), 0);
+                                       getFont(bv->buffer(), ti->paragraphs.begin(), 0);
                                ti->setText(buf.substr(op, p - op), font);
                        }
                        cols = ocol;
@@ -2807,7 +2831,7 @@ 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->paragraphs.begin(), 0);
+                       getFont(bv->buffer(), ti->paragraphs.begin(), 0);
                ti->setText(buf.substr(op, len - op), font);
        }
 
@@ -2879,6 +2903,7 @@ int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
        if (!lex.isOK())
                return -1;
 
+       // FIXME: even current_view would be better than this.
        BufferView * const bv = inset.view();
        Buffer const * const buffer = bv ? bv->buffer() : 0;
        if (buffer)
@@ -2892,6 +2917,7 @@ int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
 
 string const InsetTabularMailer::params2string(InsetTabular const & inset)
 {
+       // FIXME: even current_view would be better than this.
        BufferView * const bv = inset.view();
        Buffer const * const buffer = bv ? bv->buffer() : 0;
        if (!buffer)