]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
Rename LatexRunParams::fragile as moving_arg.
[lyx.git] / src / insets / insettabular.C
index d7c59e82bf5ee72093e0234ab0f982a1443d606a..cec7de014a5e3d16e97a47c54161beccbf8c4a4b 100644 (file)
@@ -1,42 +1,43 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file insettabular.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
+ * \author Jürgen Vigna
  *
- *           Copyright 2001 The LyX Team.
- *
- * ======================================================
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "insettabular.h"
+#include "insettext.h"
 
-#include "lyx_cb.h"
 #include "buffer.h"
-#include "commandtags.h"
-#include "lyxfunc.h"
-#include "debug.h"
-#include "LaTeXFeatures.h"
-#include "Painter.h"
-#include "font.h"
-#include "lyxtext.h"
-#include "LyXView.h"
-#include "insets/insettext.h"
+#include "BufferView.h"
+#include "lfuns.h"
 #include "debug.h"
+#include "dimension.h"
+#include "funcrequest.h"
 #include "gettext.h"
 #include "language.h"
-#include "BufferView.h"
-#include "undo_funcs.h"
+#include "LaTeXFeatures.h"
+#include "Lsstream.h"
+#include "lyx_cb.h"
+#include "lyxfunc.h"
 #include "lyxlength.h"
+#include "lyxlex.h"
+#include "lyxtext.h"
 #include "ParagraphParameters.h"
+#include "undo_funcs.h"
+#include "WordLangTuple.h"
+#include "Lsstream.h"
 
-#include "frontends/Dialogs.h"
 #include "frontends/Alert.h"
+#include "frontends/Dialogs.h"
+#include "frontends/font_metrics.h"
+#include "frontends/LyXView.h"
+#include "frontends/Painter.h"
 
 #include "support/LAssert.h"
 #include "support/lstrings.h"
@@ -84,6 +85,7 @@ TabularFeature tabularFeature[] =
        { LyXTabular::ALIGN_LEFT, "align-left" },
        { LyXTabular::ALIGN_RIGHT, "align-right" },
        { LyXTabular::ALIGN_CENTER, "align-center" },
+       { LyXTabular::ALIGN_BLOCK, "align-block" },
        { LyXTabular::VALIGN_TOP, "valign-top" },
        { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
        { LyXTabular::VALIGN_CENTER, "valign-center" },
@@ -119,9 +121,29 @@ TabularFeature tabularFeature[] =
        { LyXTabular::LAST_ACTION, "" }
 };
 
+struct FindFeature {
+       FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
+       bool operator()(TabularFeature & tf)
+       {
+               return tf.action == feature_;
+       }
+private:
+       LyXTabular::Feature feature_;
+};
+
 } // namespace anon
 
 
+string const featureAsString(LyXTabular::Feature feature)
+{
+       TabularFeature * it  = tabularFeature;
+       TabularFeature * end = it +
+               sizeof(tabularFeature) / sizeof(TabularFeature);
+       it = std::find_if(it, end, FindFeature(feature));
+       return (it == end) ? string() : it->feature;
+}
+
+
 bool InsetTabular::hasPasteBuffer() const
 {
        return (paste_tabular != 0);
@@ -146,7 +168,7 @@ InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
        clearSelection();
        need_update = INIT;
        in_update = false;
-       in_reset_pos = false;
+       in_reset_pos = 0;
        inset_x = 0;
        inset_y = 0;
 }
@@ -166,7 +188,7 @@ InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
        clearSelection();
        need_update = INIT;
        in_update = false;
-       in_reset_pos = false;
+       in_reset_pos = 0;
        inset_x = 0;
        inset_y = 0;
 }
@@ -174,7 +196,8 @@ InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
 
 InsetTabular::~InsetTabular()
 {
-       hideDialog();
+       InsetTabularMailer mailer(*this);
+       mailer.hideDialog();
 }
 
 
@@ -184,6 +207,12 @@ Inset * InsetTabular::clone(Buffer const & buf, bool same_id) const
 }
 
 
+BufferView * InsetTabular::view() const
+{
+       return buffer->getUser();
+}
+
+
 void InsetTabular::write(Buffer const * buf, ostream & os) const
 {
        os << " Tabular" << endl;
@@ -215,39 +244,22 @@ void InsetTabular::read(Buffer const * buf, LyXLex & lex)
 }
 
 
-int InsetTabular::ascent(BufferView *, LyXFont const &) const
-{
-       return tabular->GetAscentOfRow(0);
-}
-
-
-int InsetTabular::descent(BufferView *, LyXFont const &) const
-{
-       return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
-}
-
-
-int InsetTabular::width(BufferView *, LyXFont const &) const
+void InsetTabular::dimension(BufferView *, LyXFont const &,
+       Dimension & dim) const
 {
-       return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
+       dim.a = tabular->GetAscentOfRow(0);
+       dim.d = tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
+       dim.w = tabular->GetWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
 }
 
 
 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
-                       float & x, bool cleared) const
+                       float & x) const
 {
        if (nodraw()) {
-               if (cleared)
-                       need_update = FULL;
-               return;
-       }
-#if 0
-       if (need_update == INIT) {
-               if (calculate_dimensions_of_cells(bv, font, true))
-                       bv->text->status = LyXText::CHANGED_IN_DRAW;
                need_update = FULL;
+               return;
        }
-#endif
 
        Painter & pain = bv->painter();
        int i;
@@ -255,184 +267,55 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
        int nx;
 
 #if 0
-       UpdatableInset::draw(bv, font, baseline, x, cleared);
+       UpdatableInset::draw(bv, font, baseline, x);
 #else
        if (!owner())
                x += static_cast<float>(scroll());
 #endif
-       if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
-                        (top_x != int(x)) || (top_baseline != baseline)))
-       {
-               int h = ascent(bv, font) + descent(bv, font);
-               int const tx = display() || !owner() ? 0 : top_x;
-               int w =  tx ? width(bv, font) : pain.paperWidth();
-               int ty = baseline - ascent(bv, font);
-
-               if (ty < 0)
-                       ty = 0;
-               if ((ty + h) > pain.paperHeight())
-                       h = pain.paperHeight();
-               if ((top_x + w) > pain.paperWidth())
-                       w = pain.paperWidth();
-               pain.fillRectangle(tx, ty, w, h, backgroundColor());
-               need_update = FULL;
-               cleared = true;
-       }
+
        top_x = int(x);
-       topx_set = true;
        top_baseline = baseline;
        x += ADD_TO_TABULAR_WIDTH;
-       if (cleared) {
-               int cell = 0;
-               float cx;
-               first_visible_cell = -1;
-               for (i = 0; i < tabular->rows(); ++i) {
-                       nx = int(x);
-                       cell = tabular->GetCellNumber(i, 0);
-                       if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
-                               (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
-                       {
-                               baseline += tabular->GetDescentOfRow(i) +
-                                       tabular->GetAscentOfRow(i + 1) +
-                                       tabular->GetAdditionalHeight(i + 1);
-                               continue;
-                       }
-                       for (j = 0; j < tabular->columns(); ++j) {
-                               if (nx > bv->workWidth())
-                                       break;
-                               if (tabular->IsPartOfMultiColumn(i, j))
-                                       continue;
-                               cx = nx + tabular->GetBeginningOfTextInCell(cell);
-                               if (first_visible_cell < 0)
-                                       first_visible_cell = cell;
-                               if (hasSelection()) {
-                                       drawCellSelection(pain, nx, baseline, i, j, cell);
-                               }
 
-                               tabular->GetCellInset(cell)->draw(bv, font, baseline, cx, cleared);
-                               drawCellLines(pain, nx, baseline, i, cell);
-                               nx += tabular->GetWidthOfColumn(cell);
-                               ++cell;
-                       }
-                       baseline += tabular->GetDescentOfRow(i) +
+       int cell = 0;
+       float cx;
+       first_visible_cell = -1;
+       for (i = 0; i < tabular->rows(); ++i) {
+               nx = int(x);
+               cell = tabular->GetCellNumber(i, 0);
+               if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
+                       (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
+               {
+               baseline += tabular->GetDescentOfRow(i) +
                                tabular->GetAscentOfRow(i + 1) +
                                tabular->GetAdditionalHeight(i + 1);
+                       continue;
                }
-       } else if (need_update == CELL) {
-               int cell = 0;
-               nx = int(x);
-               if (the_locking_inset &&
-                       tabular->GetCellInset(actcell) != the_locking_inset)
-               {
-                       Inset * inset = tabular->GetCellInset(cell);
-                       for (i = 0;
-                            inset != the_locking_inset && i < tabular->rows();
-                            ++i)
-                       {
-                               for (j = 0;
-                                    inset != the_locking_inset && j < tabular->columns();
-                                    ++j)
-                               {
-                                       if (tabular->IsPartOfMultiColumn(i, j))
-                                               continue;
-                                       nx += tabular->GetWidthOfColumn(cell);
-                                       ++cell;
-                                       inset = tabular->GetCellInset(cell);
-                               }
-                               if (tabular->row_of_cell(cell) > i) {
-                                       nx = int(x);
-                                       baseline += tabular->GetDescentOfRow(i) +
-                                               tabular->GetAscentOfRow(i + 1) +
-                                               tabular->GetAdditionalHeight(i + 1);
-                               }
-                       }
-               } else {
-                       // compute baseline for actual row
-                       for (i = 0; i < actrow; ++i) {
-                               baseline += tabular->GetDescentOfRow(i) +
-                                       tabular->GetAscentOfRow(i + 1) +
-                                       tabular->GetAdditionalHeight(i + 1);
-                       }
-                       // now compute the right x position
-                       cell = tabular->GetCellNumber(actrow, 0);
-                       for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
-                                       if (tabular->IsPartOfMultiColumn(actrow, j))
-                                               continue;
-                                       nx += tabular->GetWidthOfColumn(cell);
-                                       ++cell;
+               for (j = 0; j < tabular->columns(); ++j) {
+                       if (nx > bv->workWidth())
+                               break;
+                       if (tabular->IsPartOfMultiColumn(i, j))
+                               continue;
+                       cx = nx + tabular->GetBeginningOfTextInCell(cell);
+                       if (first_visible_cell < 0)
+                               first_visible_cell = cell;
+                       if (hasSelection()) {
+                               drawCellSelection(pain, nx, baseline, i, j, cell);
                        }
+
+                       tabular->GetCellInset(cell)->draw(bv, font, baseline, cx);
+                       drawCellLines(pain, nx, baseline, i, cell);
+                       nx += tabular->GetWidthOfColumn(cell);
+                       ++cell;
                }
-               i = tabular->row_of_cell(cell);
-               if (the_locking_inset != tabular->GetCellInset(cell)) {
-                       lyxerr[Debug::INSETTEXT] << "ERROR this shouldn't happen\n";
-                       return;
-               }
-               float dx = nx + tabular->GetBeginningOfTextInCell(cell);
-               float cx = dx;
-               tabular->GetCellInset(cell)->draw(bv, font, baseline, dx, false);
-               //
-               // Here we use rectangular backgroundColor patches to clean up
-               // within a cell around the cell's red inset box. As follows:
-               //
-               //  +--------------------+
-               //  |         C          |   The rectangles are A, B and C
-               //  | A |------------| B |   below, origin top left (tx, ty),
-               //  |   |  inset box |   |   dimensions w(idth), h(eight).
-               //  +---+------------+---+   x grows rightward, y downward
-               //  |         D          |
-               //  +--------------------+
-               //
-#if 0
-               // clear only if we didn't have a change
-               if (bv->text->status() != LyXText::CHANGED_IN_DRAW) {
-#endif
-                       // clear before the inset
-                       int tx, ty, w, h;
-                       tx = nx + 1;
-                       ty = baseline - tabular->GetAscentOfRow(i) + 1;
-                       w = int(cx - nx - 1);
-                       h = tabular->GetAscentOfRow(i) +
-                               tabular->GetDescentOfRow(i) - 1;
-                       pain.fillRectangle(tx, ty, w, h, backgroundColor());
-                       // clear behind the inset
-                       tx = int(cx + the_locking_inset->width(bv,font) + 1);
-                       ty = baseline - tabular->GetAscentOfRow(i) + 1;
-                       w = tabular->GetWidthOfColumn(cell) -
-                               tabular->GetBeginningOfTextInCell(cell) -
-                               the_locking_inset->width(bv,font) -
-                               tabular->GetAdditionalWidth(cell) - 1;
-                       h = tabular->GetAscentOfRow(i) + tabular->GetDescentOfRow(i) - 1;
-                       pain.fillRectangle(tx, ty, w, h, backgroundColor());
-                       // clear below the inset
-                       tx = nx + 1;
-                       ty = baseline + the_locking_inset->descent(bv, font) + 1;
-                       w = tabular->GetWidthOfColumn(cell) -
-                               tabular->GetAdditionalWidth(cell) - 1;
-                       h = tabular->GetDescentOfRow(i) -
-                               the_locking_inset->descent(bv, font) - 1;
-                       pain.fillRectangle(tx, ty, w, h, backgroundColor());
-                       // clear above the inset
-                       tx = nx + 1;
-                       ty = baseline - tabular->GetAscentOfRow(i) + 1;
-                       w = tabular->GetWidthOfColumn(cell) -
-                               tabular->GetAdditionalWidth(cell) - 1;
-                       h = tabular->GetAscentOfRow(i) - the_locking_inset->ascent(bv, font);
-                       pain.fillRectangle(tx, ty, w, h, backgroundColor());
-#if 0
-               }
-#endif
+               baseline += tabular->GetDescentOfRow(i) +
+                       tabular->GetAscentOfRow(i + 1) +
+                       tabular->GetAdditionalHeight(i + 1);
        }
+
        x -= ADD_TO_TABULAR_WIDTH;
        x += width(bv, font);
-       if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
-               int i = 0;
-               for(Inset * inset = owner(); inset; ++i)
-                       inset = inset->owner();
-               if (calculate_dimensions_of_cells(bv, font, false))
-                       need_update = INIT;
-       } else {
-               need_update = NONE;
-       }
+       need_update = NONE;
 }
 
 
@@ -442,7 +325,7 @@ void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
        int x2 = x + tabular->GetWidthOfColumn(cell);
        bool on_off;
 
-       if (!tabular->TopAlreadyDrawed(cell)) {
+       if (!tabular->topAlreadyDrawn(cell)) {
                on_off = !tabular->TopLine(cell);
                pain.line(x, baseline - tabular->GetAscentOfRow(row),
                          x2, baseline -  tabular->GetAscentOfRow(row),
@@ -454,7 +337,7 @@ void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
                  x2, baseline + tabular->GetDescentOfRow(row),
                  on_off ? LColor::tabularonoffline : LColor::tabularline,
                  on_off ? Painter::line_onoffdash : Painter::line_solid);
-       if (!tabular->LeftAlreadyDrawed(cell)) {
+       if (!tabular->leftAlreadyDrawn(cell)) {
                on_off = !tabular->LeftLine(cell);
                pain.line(x, baseline -  tabular->GetAscentOfRow(row),
                          x, baseline +  tabular->GetDescentOfRow(row),
@@ -498,30 +381,30 @@ void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
 }
 
 
-void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
+void InsetTabular::update(BufferView * bv, bool reinit)
 {
        if (in_update) {
                if (reinit) {
                        resetPos(bv);
                        if (owner())
-                               owner()->update(bv, font, true);
+                               owner()->update(bv, true);
                }
                return;
        }
        in_update = true;
        if (reinit) {
                need_update = INIT;
-               if (calculate_dimensions_of_cells(bv, font, true))
+               if (calculate_dimensions_of_cells(bv, true))
                        resetPos(bv);
                if (owner())
-                       owner()->update(bv, font, true);
+                       owner()->update(bv, true);
                in_update = false;
                return;
        }
        if (the_locking_inset)
-               the_locking_inset->update(bv, font, reinit);
+               the_locking_inset->update(bv, reinit);
        if (need_update < FULL &&
-               bv->text->status() == LyXText::NEED_MORE_REFRESH)
+               bv->text->refreshStatus() == LyXText::REFRESH_AREA)
        {
                need_update = FULL;
        }
@@ -530,7 +413,7 @@ void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
        case INIT:
        case FULL:
        case CELL:
-               if (calculate_dimensions_of_cells(bv, font, false)) {
+               if (calculate_dimensions_of_cells(bv, false)) {
                        need_update = INIT;
                        resetPos(bv);
                }
@@ -547,51 +430,7 @@ void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
 
 string const InsetTabular::editMessage() const
 {
-       return _("Opened Tabular Inset");
-}
-
-
-void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int 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 != 3)) {
-               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)
-               actcell = 0;
-       else
-               actcell = tabular->GetNumberOfCells() - 1;
-       clearSelection();
-       resetPos(bv);
-       bv->fitCursor();
+       return _("Opened table");
 }
 
 
@@ -599,10 +438,10 @@ void InsetTabular::insetUnlock(BufferView * bv)
 {
        if (the_locking_inset) {
                the_locking_inset->insetUnlock(bv);
-               updateLocal(bv, CELL, false);
+               updateLocal(bv, CELL);
                the_locking_inset = 0;
        }
-       hideInsetCursor(bv);
+       actcell = 0;
        oldcell = -1;
        locked = false;
        if (scroll(false) || hasSelection()) {
@@ -610,31 +449,24 @@ void InsetTabular::insetUnlock(BufferView * bv)
                if (scroll(false)) {
                        scroll(bv, 0.0F);
                }
-               updateLocal(bv, FULL, false);
+               updateLocal(bv, FULL);
        }
 }
 
 
-void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
-                              bool mark_dirty) const
+void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what) const
 {
        if (what == INIT) {
-               LyXFont font;
-               calculate_dimensions_of_cells(bv, font, true);
+               calculate_dimensions_of_cells(bv, true);
        }
        if (!locked && what == CELL)
                what = FULL;
        if (need_update < what) // only set this if it has greater update
                need_update = what;
-#if 0 // maybe this should not be done!
-       if ((what == INIT) && hasSelection()) {
-               clearSelection();
-       }
-#endif
        // Dirty Cast! (Lgb)
        if (need_update != NONE) {
-               bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
-               if (locked) // && (what != NONE))
+               bv->updateInset(const_cast<InsetTabular *>(this));
+               if (locked)
                        resetPos(bv);
        }
 }
@@ -666,7 +498,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);
                        }
                }
@@ -696,16 +528,16 @@ bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
                if (scroll(false))
                        scroll(bv, 0.0F);
 #endif
-               updateLocal(bv, CELL, false);
+               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)) {
                if (inset->lyxCode() == TABULAR_CODE &&
                    !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
-                       bv->owner()->getDialogs()->updateTabular(this);
+                       InsetTabularMailer mailer(*this);
+                       mailer.updateDialog(bv);
                        oldcell = actcell;
                }
                return true;
@@ -728,7 +560,7 @@ bool InsetTabular::updateInsetInInset(BufferView * bv, Inset * inset)
                if (!static_cast<InsetText *>(tl_inset)->updateInsetInInset(bv, inset))
                        return false;
        }
-       updateLocal(bv, CELL, false);
+       updateLocal(bv, CELL);
        return true;
 }
 
@@ -766,135 +598,176 @@ bool InsetTabular::insertInset(BufferView * bv, Inset * inset)
 }
 
 
-void InsetTabular::insetButtonPress(BufferView * bv, int x, int y, int button)
+void InsetTabular::lfunMousePress(FuncRequest const & cmd)
 {
-       if (hasSelection() && (button == 3))
+       if (hasSelection() && cmd.button() == mouse_button::button3)
                return;
 
        if (hasSelection()) {
                clearSelection();
-               updateLocal(bv, SELECTION, false);
+               updateLocal(cmd.view(), SELECTION);
        }
 
        int const ocell = actcell;
        int const orow = actrow;
+       BufferView * bv = cmd.view();
 
-       hideInsetCursor(bv);
        if (!locked) {
                locked = true;
                the_locking_inset = 0;
                inset_x = 0;
                inset_y = 0;
        }
-       setPos(bv, x, y);
+       setPos(bv, cmd.x, cmd.y);
        if (actrow != orow)
-               updateLocal(bv, NONE, false);
+               updateLocal(bv, NONE);
        clearSelection();
 #if 0
-       if (button == 3) {
+       if (cmd.button() == mouse_button::button3) {
                if ((ocell != actcell) && the_locking_inset) {
                        the_locking_inset->insetUnlock(bv);
-                       updateLocal(bv, CELL, false);
+                       updateLocal(bv, CELL);
                        the_locking_inset = 0;
                }
-               showInsetCursor(bv);
                return;
        }
 #endif
 
-       bool const inset_hit = insetHit(bv, x, y);
+       bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
 
        if ((ocell == actcell) && the_locking_inset && inset_hit) {
                resetPos(bv);
-               the_locking_inset->insetButtonPress(bv,
-                                                   x - inset_x, y - inset_y,
-                                                   button);
+               FuncRequest cmd1 = cmd;
+               cmd1.x -= inset_x;
+               cmd1.y -= inset_y;
+               the_locking_inset->localDispatch(cmd1);
                return;
-       } else if (the_locking_inset) {
+       }
+
+       if (the_locking_inset) {
                the_locking_inset->insetUnlock(bv);
-               updateLocal(bv, CELL, false);
+               updateLocal(bv, CELL);
                the_locking_inset = 0;
        }
-       if (button == 2) {
-               localDispatch(bv, LFUN_PASTESELECTION, "paragraph");
+
+       if (cmd.button() == mouse_button::button2) {
+               localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
                return;
        }
+
        if (inset_hit && bv->theLockingInset()) {
-               if (!bv->lockInset(static_cast<UpdatableInset*>(tabular->GetCellInset(actcell)))) {
+               if (!bv->lockInset(static_cast<UpdatableInset*>
+                               (tabular->GetCellInset(actcell))))
+               {
                        lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
                        return;
                }
-               the_locking_inset->insetButtonPress(
-                       bv, x - inset_x, y - inset_y, button);
+               FuncRequest cmd1 = cmd;
+               cmd1.x -= inset_x;
+               cmd1.y -= inset_y;
+               the_locking_inset->localDispatch(cmd1);
                return;
        }
-       showInsetCursor(bv);
 }
 
 
-bool InsetTabular::insetButtonRelease(BufferView * bv,
-                                     int x, int y, int button)
+bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
 {
        bool ret = false;
-       if (the_locking_inset)
-               ret = the_locking_inset->insetButtonRelease(bv, x - inset_x,
-                                                                                                       y - inset_y, button);
-       if (button == 3 && !ret) {
-               bv->owner()->getDialogs()->showTabular(this);
+       if (the_locking_inset) {
+               FuncRequest cmd1 = cmd;
+               cmd1.x -= inset_x;
+               cmd1.y -= inset_y;
+               ret = the_locking_inset->localDispatch(cmd1);
+       }
+       if (cmd.button() == mouse_button::button3 && !ret) {
+               InsetTabularMailer mailer(*this);
+               mailer.showDialog(cmd.view());
                return true;
        }
        return ret;
 }
 
 
-void InsetTabular::insetMotionNotify(BufferView * bv, int x, int y, int button)
+void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
 {
        if (the_locking_inset) {
-               the_locking_inset->insetMotionNotify(bv,
-                                                    x - inset_x,
-                                                    y - inset_y,
-                                                    button);
+               FuncRequest cmd1 = cmd;
+               cmd1.x -= inset_x;
+               cmd1.y -= inset_y;
+               the_locking_inset->localDispatch(cmd1);
                return;
        }
 
-       hideInsetCursor(bv);
-//     int const old_cell = actcell;
+       BufferView * bv = cmd.view();
+       int const old_cell = actcell;
 
-       setPos(bv, x, y);
+       setPos(bv, cmd.x, cmd.y);
        if (!hasSelection()) {
                setSelection(actcell, actcell);
-       } else {
+               updateLocal(bv, SELECTION);
+       } else if (old_cell != actcell) {
                setSelection(sel_cell_start, actcell);
+               updateLocal(bv, SELECTION);
        }
-       updateLocal(bv, SELECTION, false);
-       showInsetCursor(bv);
 }
 
 
-void InsetTabular::insetKeyPress(XKeyEvent * xke)
-{
-       if (the_locking_inset) {
-               the_locking_inset->insetKeyPress(xke);
-               return;
-       }
-}
-
-
-UpdatableInset::RESULT
-InsetTabular::localDispatch(BufferView * bv, kb_action action,
-                           string const & arg)
+Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
 {
        // We need to save the value of the_locking_inset as the call to
-       // the_locking_inset->LocalDispatch might unlock it.
+       // the_locking_inset->localDispatch might unlock it.
        old_locking_inset = the_locking_inset;
-       UpdatableInset::RESULT result =
-               UpdatableInset::localDispatch(bv, action, arg);
+       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;
        }
 
-       if ((action < 0) && arg.empty())
+       if (cmd.action < 0 && cmd.argument.empty())
                return FINISHED;
 
        bool hs = hasSelection();
@@ -903,20 +776,29 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
        // this one have priority over the locked InsetText, if we're not already
        // inside another tabular then that one get's priority!
        if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) == this) {
-               switch (action) {
-               case LFUN_SHIFT_TAB:
-               case LFUN_TAB:
-                       hideInsetCursor(bv);
+               switch (cmd.action) {
+               case LFUN_MOUSE_PRESS:
+                       lfunMousePress(cmd);
+                       return DISPATCHED;
+
+               case LFUN_MOUSE_MOTION:
+                       lfunMouseMotion(cmd);
+                       return DISPATCHED;
+
+               case LFUN_MOUSE_RELEASE:
+                       return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
+
+               case LFUN_CELL_BACKWARD:
+               case LFUN_CELL_FORWARD:
                        unlockInsetInInset(bv, the_locking_inset);
-                       if (action == LFUN_TAB)
+                       if (cmd.action == LFUN_CELL_FORWARD)
                                moveNextCell(bv, old_locking_inset != 0);
                        else
                                movePrevCell(bv, old_locking_inset != 0);
                        clearSelection();
                        if (hs)
-                               updateLocal(bv, SELECTION, false);
+                               updateLocal(bv, SELECTION);
                        if (!the_locking_inset) {
-                               showInsetCursor(bv);
                                return DISPATCHED_NOUPDATE;
                        }
                        return result;
@@ -926,32 +808,36 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                }
        }
 
+       kb_action action = cmd.action;
+       string    arg    = cmd.argument;
        if (the_locking_inset) {
-               result = the_locking_inset->localDispatch(bv, action, arg);
+               result = the_locking_inset->localDispatch(cmd);
                if (result == DISPATCHED_NOUPDATE) {
                        int sc = scroll();
                        resetPos(bv);
                        if (sc != scroll()) { // inset has been scrolled
-                               the_locking_inset->toggleInsetCursor(bv);
-                               updateLocal(bv, FULL, false);
-                               the_locking_inset->toggleInsetCursor(bv);
+                               updateLocal(bv, FULL);
                        }
                        return result;
                } else if (result == DISPATCHED) {
-                       the_locking_inset->toggleInsetCursor(bv);
-                       updateLocal(bv, CELL, false);
-                       the_locking_inset->toggleInsetCursor(bv);
+                       updateLocal(bv, CELL);
                        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 ----------------------------------
@@ -970,14 +856,14 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                        end = actcell;
                }
                setSelection(start, end);
-               updateLocal(bv, SELECTION, false);
+               updateLocal(bv, SELECTION);
                break;
        }
        case LFUN_RIGHT:
                result = moveRight(bv);
                clearSelection();
                if (hs)
-                       updateLocal(bv, SELECTION, false);
+                       updateLocal(bv, SELECTION);
                break;
        case LFUN_LEFTSEL: {
                int const start = hasSelection() ? sel_cell_start : actcell;
@@ -994,14 +880,14 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                        end = actcell;
                }
                setSelection(start, end);
-               updateLocal(bv, SELECTION, false);
+               updateLocal(bv, SELECTION);
                break;
        }
        case LFUN_LEFT:
                result = moveLeft(bv);
                clearSelection();
                if (hs)
-                       updateLocal(bv, SELECTION, false);
+                       updateLocal(bv, SELECTION);
                break;
        case LFUN_DOWNSEL: {
                int const start = hasSelection() ? sel_cell_start : actcell;
@@ -1018,14 +904,14 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                } else {
                        setSelection(start, start);
                }
-               updateLocal(bv, SELECTION, false);
+               updateLocal(bv, SELECTION);
        }
        break;
        case LFUN_DOWN:
                result = moveDown(bv, old_locking_inset != 0);
                clearSelection();
                if (hs) {
-                       updateLocal(bv, SELECTION, false);
+                       updateLocal(bv, SELECTION);
                }
                break;
        case LFUN_UPSEL: {
@@ -1043,14 +929,14 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                } else {
                        setSelection(start, start);
                }
-               updateLocal(bv, SELECTION, false);
+               updateLocal(bv, SELECTION);
        }
        break;
        case LFUN_UP:
                result = moveUp(bv, old_locking_inset != 0);
                clearSelection();
                if (hs)
-                       updateLocal(bv, SELECTION, false);
+                       updateLocal(bv, SELECTION);
                break;
        case LFUN_NEXT: {
                UpdateCodes code = CURSOR;
@@ -1060,17 +946,17 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                }
                int column = actcol;
                unlockInsetInInset(bv, the_locking_inset);
-               if (bv->text->first_y + bv->painter().paperHeight() <
+               if (bv->text->top_y() + bv->painter().paperHeight() <
                    (top_baseline + tabular->GetHeightOfTabular()))
                        {
-                               bv->scrollCB(bv->text->first_y + bv->painter().paperHeight());
+                               bv->scrollDocView(bv->text->top_y() + bv->painter().paperHeight());
                                code = FULL;
                                actcell = tabular->GetCellBelow(first_visible_cell) + column;
                        } else {
                                actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
                        }
                resetPos(bv);
-               updateLocal(bv, code, false);
+               updateLocal(bv, code);
                break;
        }
        case LFUN_PRIOR: {
@@ -1082,7 +968,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                int column = actcol;
                unlockInsetInInset(bv, the_locking_inset);
                if (top_baseline < 0) {
-                       bv->scrollCB(bv->text->first_y - bv->painter().paperHeight());
+                       bv->scrollDocView(bv->text->top_y() - bv->painter().paperHeight());
                        code = FULL;
                        if (top_baseline > 0)
                                actcell = column;
@@ -1092,7 +978,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                        actcell = column;
                }
                resetPos(bv);
-               updateLocal(bv, code, false);
+               updateLocal(bv, code);
                break;
        }
        // none of these make sense for insettabular,
@@ -1104,12 +990,12 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
        case LFUN_WORDLEFTSEL:
        case LFUN_WORDRIGHT:
        case LFUN_WORDRIGHTSEL:
+       case LFUN_WORDSEL:
        case LFUN_DOWN_PARAGRAPH:
        case LFUN_DOWN_PARAGRAPHSEL:
        case LFUN_UP_PARAGRAPH:
        case LFUN_UP_PARAGRAPHSEL:
        case LFUN_BACKSPACE:
-       case LFUN_DELETE:
        case LFUN_HOME:
        case LFUN_HOMESEL:
        case LFUN_END:
@@ -1119,9 +1005,16 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
        case LFUN_ENDBUF:
        case LFUN_ENDBUFSEL:
                break;
-       case LFUN_LAYOUT_TABULAR:
-               bv->owner()->getDialogs()->showTabular(this);
+       case LFUN_LAYOUT_TABULAR: {
+               InsetTabularMailer mailer(*this);
+               mailer.showDialog(bv);
+               break;
+       }
+       case LFUN_INSET_DIALOG_UPDATE: {
+               InsetTabularMailer mailer(*this);
+               mailer.updateDialog(bv);
                break;
+       }
        case LFUN_TABULAR_FEATURE:
                if (!tabularFeatures(bv, arg))
                        result = UNDISPATCHED;
@@ -1134,7 +1027,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                if (tmpstr.empty())
                        break;
                if (insertAsciiString(bv, tmpstr, false))
-                       updateLocal(bv, INIT, true);
+                       updateLocal(bv, INIT);
                else
                        result = UNDISPATCHED;
                break;
@@ -1143,11 +1036,13 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
        case LFUN_CUT:
                if (!copySelection(bv))
                        break;
+               // no break here!
+       case LFUN_DELETE:
                setUndo(bv, Undo::DELETE,
                        bv->text->cursor.par(),
-                       bv->text->cursor.par()->next());
-               cutSelection();
-               updateLocal(bv, INIT, true);
+                       boost::next(bv->text->cursor.par()));
+               cutSelection(bv->buffer()->params);
+               updateLocal(bv, INIT);
                break;
        case LFUN_COPY:
                if (!hasSelection())
@@ -1230,9 +1125,9 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                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, true);
+                       updateLocal(bv, INIT);
                        break;
                }
                // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
@@ -1252,7 +1147,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                        case LFUN_DEFAULT:
                        case LFUN_UNDERLINE:
                        case LFUN_FONT_SIZE:
-                               if (bv->Dispatch(action, arg))
+                               if (bv->dispatch(FuncRequest(bv, action, arg)))
                                        result = DISPATCHED;
                                break;
                        default:
@@ -1267,12 +1162,12 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                if (activateCellInset(bv)) {
                        // reset need_update setted in above function!
                        need_update = NONE;
-                       result = the_locking_inset->localDispatch(bv, action, arg);
+                       result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
                        if ((result == UNDISPATCHED) || (result >= FINISHED)) {
                                unlockInsetInInset(bv, the_locking_inset);
                                nodraw(false);
                                // we need to update if this was requested before
-                               updateLocal(bv, NONE, false);
+                               updateLocal(bv, NONE);
                                return UNDISPATCHED;
                        } else if (hs) {
                                clearSelection();
@@ -1281,7 +1176,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                                need_update = SELECTION;
                        }
                        nodraw(false);
-                       updateLocal(bv, CELL, false);
+                       updateLocal(bv, CELL);
                        return result;
                }
                break;
@@ -1289,9 +1184,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
        if (result < FINISHED) {
                if (!the_locking_inset) {
                        if (bv->fitCursor())
-                               updateLocal(bv, FULL, false);
-                       if (locked)
-                               showInsetCursor(bv);
+                               updateLocal(bv, FULL);
                }
        } else
                bv->unlockInset(this);
@@ -1300,9 +1193,9 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
 
 
 int InsetTabular::latex(Buffer const * buf, ostream & os,
-                       bool fragile, bool fp) const
+                       LatexRunParams const & runparams) const
 {
-       return tabular->latex(buf, os, fragile, fp);
+       return tabular->latex(buf, os, runparams);
 }
 
 
@@ -1326,7 +1219,7 @@ int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
 }
 
 
-int InsetTabular::docbook(Buffer const * buf, ostream & os) const
+int InsetTabular::docbook(Buffer const * buf, ostream & os, bool mixcont) const
 {
        int ret = 0;
        Inset * master;
@@ -1338,12 +1231,16 @@ int InsetTabular::docbook(Buffer const * buf, ostream & os) const
            master = master->owner());
 
        if (!master) {
-               os << "<informaltable>\n";
+               os << "<informaltable>";
+               if (mixcont)
+                       os << endl;
                ret++;
        }
-       ret+= tabular->docBook(buf,os);
+       ret+= tabular->docbook(buf, os, mixcont);
        if (!master) {
-               os << "</informaltable>\n";
+               os << "</informaltable>";
+               if (mixcont)
+                       os << endl;
                ret++;
        }
        return ret;
@@ -1356,9 +1253,7 @@ void InsetTabular::validate(LaTeXFeatures & features) const
 }
 
 
-bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
-                                                LyXFont const & font,
-                                                bool reinit) const
+bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv, bool reinit) const
 {
        int cell = -1;
        int maxAsc = 0;
@@ -1366,6 +1261,10 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
        InsetText * inset;
        bool changed = false;
 
+       // FIXME: since InsetText ignores this anyway, it doesn't
+       // matter what we pass it. Ugly
+       LyXFont font;
+
        // if we have a locking_inset we should have to check only this cell for
        // change so I'll try this to have a boost, but who knows ;)
        if ((need_update != INIT) &&
@@ -1390,7 +1289,7 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
                        ++cell;
                        inset = tabular->GetCellInset(cell);
                        if (!reinit && !tabular->GetPWidth(cell).zero())
-                               inset->update(bv, font, false);
+                               inset->update(bv, false);
                        maxAsc = max(maxAsc, inset->ascent(bv, font));
                        maxDesc = max(maxDesc, inset->descent(bv, font));
                        changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
@@ -1404,82 +1303,52 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
 }
 
 
-void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
+void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
 {
        if (the_locking_inset) {
-               the_locking_inset->getCursorPos(bv, x, y);
+               the_locking_inset->getCursor(bv, x, y);
                return;
        }
-       x = cursor_.x() - top_x;
-       y = cursor_.y();
-}
 
+       x = cursor_.x();
+       y = cursor_.y() + InsetTabular::y();
 
-void InsetTabular::toggleInsetCursor(BufferView * bv)
-{
-       if (nodraw()) {
-               if (isCursorVisible())
-                       bv->hideLockedInsetCursor();
-               return;
-       }
-       if (the_locking_inset) {
-               the_locking_inset->toggleInsetCursor(bv);
-               return;
-       }
-
-       LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
-
-       int const asc = lyxfont::maxAscent(font);
-       int const desc = lyxfont::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 = lyxfont::maxAscent(font);
-               int const desc = lyxfont::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();
 }
 
 
 void InsetTabular::fitInsetCursor(BufferView * bv) const
 {
        if (the_locking_inset) {
-               int old_first_y = bv->text->first_y;
+               int old_top_y = bv->text->top_y();
                the_locking_inset->fitInsetCursor(bv);
-               if (old_first_y != bv->text->first_y)
+               if (old_top_y != bv->text->top_y())
                        need_update = FULL;
                return;
        }
        LyXFont font;
 
-       int const asc = lyxfont::maxAscent(font);
-       int const desc = lyxfont::maxDescent(font);
+       int const asc = font_metrics::maxAscent(font);
+       int const desc = font_metrics::maxDescent(font);
        resetPos(bv);
+
        if (bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc))
                need_update = FULL;
 }
@@ -1534,7 +1403,7 @@ void InsetTabular::resetPos(BufferView * bv) const
 #warning This should be fixed in the right manner (20011128 Jug)
 #endif
        // fast hack to fix infinite repaintings!
-       if (in_reset_pos)
+       if (in_reset_pos > 0)
                return;
 
        int cell = 0;
@@ -1555,45 +1424,45 @@ void InsetTabular::resetPos(BufferView * bv) const
                return;
        }
        // we need this only from here on!!!
-       in_reset_pos = true;
+       ++in_reset_pos;
        static int const offset = ADD_TO_TABULAR_WIDTH + 2;
        int new_x = getCellXPos(actcell);
        int old_x = cursor_.x();
        new_x += offset;
        cursor_.x(new_x);
 //    cursor.x(getCellXPos(actcell) + offset);
-       if ((actcol < tabular->columns()-1) && scroll(false) &&
+       if ((actcol < tabular->columns() - 1) && scroll(false) &&
                (tabular->GetWidthOfTabular() < bv->workWidth()-20))
        {
                scroll(bv, 0.0F);
-               updateLocal(bv, FULL, false);
+               updateLocal(bv, FULL);
        } else if (the_locking_inset &&
                 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
        {
-               int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
+               int xx = cursor_.x() - offset + bv->text->getRealCursorX();
                if (xx > (bv->workWidth()-20)) {
                        scroll(bv, -(xx - bv->workWidth() + 60));
-                       updateLocal(bv, FULL, false);
+                       updateLocal(bv, FULL);
                } else if (xx < 20) {
                        if (xx < 0)
                                xx = -xx + 60;
                        else
                                xx = 60;
                        scroll(bv, xx);
-                       updateLocal(bv, FULL, false);
+                       updateLocal(bv, FULL);
                }
        } else if ((cursor_.x() - offset) > 20 &&
                   (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
                   > (bv->workWidth() - 20)) {
                scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
-               updateLocal(bv, FULL, false);
+               updateLocal(bv, FULL);
        } else if ((cursor_.x() - offset) < 20) {
                scroll(bv, 20 - cursor_.x() + offset);
-               updateLocal(bv, FULL, false);
-       } else if (scroll(false) && top_x > 20 &&
+               updateLocal(bv, FULL);
+       } else if (scroll() && top_x > 20 &&
                   (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
                scroll(bv, old_x - cursor_.x());
-               updateLocal(bv, FULL, false);
+               updateLocal(bv, FULL);
        }
        if (the_locking_inset) {
                inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
@@ -1603,14 +1472,15 @@ void InsetTabular::resetPos(BufferView * bv) const
             !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
            actcell != oldcell) {
                InsetTabular * inset = const_cast<InsetTabular *>(this);
-               bv->owner()->getDialogs()->updateTabular(inset);
+               InsetTabularMailer mailer(*inset);
+               mailer.updateDialog(bv);
                oldcell = actcell;
        }
-       in_reset_pos = false;
+       in_reset_pos = 0;
 }
 
 
-UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
+Inset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
 {
        if (lock && !old_locking_inset) {
                if (activateCellInset(bv))
@@ -1628,13 +1498,13 @@ UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
 }
 
 
-UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
+Inset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
 {
        bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
        if (!moved)
                return FINISHED;
        if (lock) {       // behind the inset
-               if (activateCellInset(bv, 0, 0, 0, true))
+               if (activateCellInset(bv, 0, 0, mouse_button::none, true))
                        return DISPATCHED;
        }
        resetPos(bv);
@@ -1642,7 +1512,7 @@ UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
 }
 
 
-UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
+Inset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
 {
        int const ocell = actcell;
        actcell = tabular->GetCellAbove(actcell);
@@ -1663,7 +1533,7 @@ UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
 }
 
 
-UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
+Inset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
 {
        int const ocell = actcell;
        actcell = tabular->GetCellBelow(actcell);
@@ -1704,9 +1574,9 @@ 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, 0, !rtl);
+               activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
        }
        resetPos(bv);
        return true;
@@ -1733,21 +1603,15 @@ 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, 0, !rtl);
+               activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
        }
        resetPos(bv);
        return true;
 }
 
 
-bool InsetTabular::deletable() const
-{
-       return true;
-}
-
-
 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
                           bool selectall)
 {
@@ -1757,8 +1621,8 @@ 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());
-               bool frozen = undo_frozen;
+                       boost::next(bv->text->cursor.par()));
+               bool const frozen = undo_frozen;
                if (!frozen)
                        freezeUndo();
                // apply the fontchange on the whole selection
@@ -1776,7 +1640,7 @@ void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
                        unFreezeUndo();
                if (selectall)
                        clearSelection();
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
        }
        if (the_locking_inset)
                the_locking_inset->setFont(bv, font, tall);
@@ -1802,13 +1666,15 @@ bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
                return false;
 
        string const val =
-               frontStrip(what.substr(tabularFeature[i].feature.length()));
+               ltrim(what.substr(tabularFeature[i].feature.length()));
        tabularFeatures(bv, action, val);
        return true;
 }
 
-static void checkLongtableSpecial(LyXTabular::ltType & ltt,
-                                 string const & special, bool & flag)
+namespace {
+
+void checkLongtableSpecial(LyXTabular::ltType & ltt,
+                         string const & special, bool & flag)
 {
        if (special == "dl_above") {
                ltt.topDL = flag;
@@ -1825,6 +1691,8 @@ static void checkLongtableSpecial(LyXTabular::ltType & ltt,
        }
 }
 
+}
+
 
 void InsetTabular::tabularFeatures(BufferView * bv,
                                   LyXTabular::Feature feature,
@@ -1851,6 +1719,9 @@ void InsetTabular::tabularFeatures(BufferView * bv,
        case LyXTabular::ALIGN_CENTER:
                setAlign = LYX_ALIGN_CENTER;
                break;
+       case LyXTabular::ALIGN_BLOCK:
+               setAlign = LYX_ALIGN_BLOCK;
+               break;
        case LyXTabular::M_VALIGN_TOP:
        case LyXTabular::VALIGN_TOP:
                setVAlign = LyXTabular::LYX_VALIGN_TOP;
@@ -1874,7 +1745,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);
@@ -1895,8 +1766,15 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                cell = tabular->GetCellNumber(i,column);
                                tabular->GetCellInset(cell)->resizeLyXText(bv);
                        }
-                       updateLocal(bv, INIT, true);
+                       updateLocal(bv, INIT);
                }
+
+               if (vallen.zero()
+                   && tabular->GetAlignment(actcell, true) == LYX_ALIGN_BLOCK)
+                       tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
+               else if (!vallen.zero()
+                        && tabular->GetAlignment(actcell, true) != LYX_ALIGN_BLOCK)
+                       tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
        }
        break;
        case LyXTabular::SET_MPWIDTH:
@@ -1911,27 +1789,27 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                tabular->GetCellInset(tabular->GetCellNumber(i, column))->
                                        resizeLyXText(bv);
                        }
-                       updateLocal(bv, INIT, true);
+                       updateLocal(bv, INIT);
                }
        }
        break;
        case LyXTabular::SET_SPECIAL_COLUMN:
        case LyXTabular::SET_SPECIAL_MULTI:
                tabular->SetAlignSpecial(actcell,value,feature);
-               updateLocal(bv, FULL, true);
+               updateLocal(bv, FULL);
                break;
        case LyXTabular::APPEND_ROW:
                // append the row into the tabular
                unlockInsetInInset(bv, the_locking_inset);
                tabular->AppendRow(bv->buffer()->params, actcell);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        case LyXTabular::APPEND_COLUMN:
                // append the column into the tabular
                unlockInsetInInset(bv, the_locking_inset);
                tabular->AppendColumn(bv->buffer()->params, actcell);
                actcell = tabular->GetCellNumber(row, column);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        case LyXTabular::DELETE_ROW:
                unlockInsetInInset(bv, the_locking_inset);
@@ -1942,7 +1820,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                        --sel_row_start;
                actcell = tabular->GetCellNumber(sel_row_start, column);
                clearSelection();
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        case LyXTabular::DELETE_COLUMN:
                unlockInsetInInset(bv, the_locking_inset);
@@ -1953,7 +1831,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                        --sel_col_start;
                actcell = tabular->GetCellNumber(row, sel_col_start);
                clearSelection();
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        case LyXTabular::M_TOGGLE_LINE_TOP:
                flag = false;
@@ -1965,7 +1843,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                tabular->SetTopLine(
                                        tabular->GetCellNumber(i, j),
                                        lineSet, flag);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        }
 
@@ -1980,7 +1858,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                        tabular->GetCellNumber(i, j),
                                        lineSet,
                                        flag);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        }
 
@@ -1995,7 +1873,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                        tabular->GetCellNumber(i,j),
                                        lineSet,
                                        flag);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        }
 
@@ -2010,7 +1888,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                        tabular->GetCellNumber(i,j),
                                        lineSet,
                                        flag);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        }
 
@@ -2021,13 +1899,14 @@ void InsetTabular::tabularFeatures(BufferView * bv,
        case LyXTabular::ALIGN_LEFT:
        case LyXTabular::ALIGN_RIGHT:
        case LyXTabular::ALIGN_CENTER:
+       case LyXTabular::ALIGN_BLOCK:
                for (int i = sel_row_start; i <= sel_row_end; ++i)
                        for (int j = sel_col_start; j <= sel_col_end; ++j)
                                tabular->SetAlignment(
                                        tabular->GetCellNumber(i, j),
                                        setAlign,
                                        flag);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        case LyXTabular::M_VALIGN_TOP:
        case LyXTabular::M_VALIGN_BOTTOM:
@@ -2041,14 +1920,16 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                tabular->SetVAlignment(
                                        tabular->GetCellNumber(i, j),
                                        setVAlign, flag);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        case LyXTabular::MULTICOLUMN:
        {
                if (sel_row_start != sel_row_end) {
-                       Alert::alert(_("Impossible Operation!"),
-                                  _("Multicolumns can only be horizontally."),
-                                  _("Sorry."));
+#ifdef WITH_WARNINGS
+#warning Need I say it ? This is horrible.
+#endif
+                       Alert::error(_("Error setting multicolumn"),
+                                  _("You cannot set multicolumn vertically."));
                        return;
                }
                // just multicol for one Single Cell
@@ -2056,12 +1937,12 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                        // check wether we are completly in a multicol
                        if (tabular->IsMultiColumn(actcell)) {
                                tabular->UnsetMultiColumn(actcell);
-                               updateLocal(bv, INIT, true);
+                               updateLocal(bv, INIT);
                        } else {
                                tabular->SetMultiColumn(bv->buffer(), actcell, 1);
-                               updateLocal(bv, CELL, true);
+                               updateLocal(bv, CELL);
                        }
-                       return;
+                       break;
                }
                // we have a selection so this means we just add all this
                // cells to form a multicolumn cell
@@ -2078,7 +1959,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                tabular->SetMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
                actcell = s_start;
                clearSelection();
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        }
        case LyXTabular::SET_ALL_LINES:
@@ -2088,15 +1969,15 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                        for (int j = sel_col_start; j <= sel_col_end; ++j)
                                tabular->SetAllLines(
                                        tabular->GetCellNumber(i,j), setLines);
-               updateLocal(bv, INIT, true);
+               updateLocal(bv, INIT);
                break;
        case LyXTabular::SET_LONGTABULAR:
                tabular->SetLongTabular(true);
-               updateLocal(bv, INIT, true); // because this toggles displayed
+               updateLocal(bv, INIT); // because this toggles displayed
                break;
        case LyXTabular::UNSET_LONGTABULAR:
                tabular->SetLongTabular(false);
-               updateLocal(bv, INIT, true); // because this toggles displayed
+               updateLocal(bv, INIT); // because this toggles displayed
                break;
        case LyXTabular::SET_ROTATE_TABULAR:
                tabular->SetRotateTabular(true);
@@ -2166,10 +2047,13 @@ void InsetTabular::tabularFeatures(BufferView * bv,
        case LyXTabular::LAST_ACTION:
                break;
        }
+
+       InsetTabularMailer mailer(*this);
+       mailer.updateDialog(bv);
 }
 
 
-bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
+bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, mouse_button::state button,
                                     bool behind)
 {
        UpdatableInset * inset =
@@ -2181,16 +2065,16 @@ bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int 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, false);
+       updateLocal(bv, CELL);
        return (the_locking_inset != 0);
 }
 
 
 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
-                                       int button)
+                                       mouse_button::state button)
 {
        inset_x = cursor_.x()
                - top_x + tabular->GetBeginningOfTextInCell(actcell);
@@ -2214,7 +2098,7 @@ int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
 
        if (len.zero())
                return -1;
-       return len.inPixels(latexTextWidth(bv), bv->text->defaultHeight());
+       return len.inPixels(latexTextWidth(bv));
 }
 
 
@@ -2278,9 +2162,11 @@ LyXText * InsetTabular::getLyXText(BufferView const * bv,
 
 bool InsetTabular::showInsetDialog(BufferView * bv) const
 {
-       if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
-               bv->owner()->getDialogs()
-                       ->showTabular(const_cast<InsetTabular *>(this));
+       if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv)) {
+               InsetTabular * tmp = const_cast<InsetTabular *>(this);
+               InsetTabularMailer mailer(*tmp);
+               mailer.showDialog(bv);
+       }
        return true;
 }
 
@@ -2295,8 +2181,9 @@ void InsetTabular::openLayoutDialog(BufferView * bv) const
                        return;
                }
        }
-       bv->owner()->getDialogs()->showTabular(
-               const_cast<InsetTabular *>(this));
+       InsetTabular * tmp = const_cast<InsetTabular *>(this);
+       InsetTabularMailer mailer(*tmp);
+       mailer.showDialog(bv);
 }
 
 
@@ -2324,7 +2211,7 @@ FuncStatus InsetTabular::getStatus(string const & what) const
                return status.unknown(true);
        }
 
-       string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
+       string const argument = ltrim(what.substr(tabularFeature[i].feature.length()));
 
        int sel_row_start;
        int sel_row_end;
@@ -2343,8 +2230,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:
@@ -2391,6 +2276,10 @@ FuncStatus InsetTabular::getStatus(string const & what) const
        case LyXTabular::ALIGN_CENTER:
                status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
                break;
+       case LyXTabular::ALIGN_BLOCK:
+               status.disabled(tabular->GetPWidth(actcell).zero());
+               status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
+               break;
        case LyXTabular::M_VALIGN_TOP:
                flag = false;
        case LyXTabular::VALIGN_TOP:
@@ -2500,7 +2389,7 @@ bool InsetTabular::copySelection(BufferView * bv)
        ostringstream sstr;
        paste_tabular->ascii(bv->buffer(), sstr,
                             (int)parOwner()->params().depth(), true, '\t');
-       bv->stuffClipboard(sstr.str().c_str());
+       bv->stuffClipboard(STRCONV(sstr.str()));
        return true;
 }
 
@@ -2532,13 +2421,14 @@ bool InsetTabular::pasteSelection(BufferView * bv)
                        *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
                        tabular->GetCellInset(n2)->setOwner(this);
                        tabular->GetCellInset(n2)->deleteLyXText(bv);
+                       tabular->GetCellInset(n2)->markNew();
                }
        }
        return true;
 }
 
 
-bool InsetTabular::cutSelection()
+bool InsetTabular::cutSelection(BufferParams const & bp)
 {
        if (!hasSelection())
                return false;
@@ -2561,7 +2451,7 @@ bool InsetTabular::cutSelection()
        }
        for (int i = sel_row_start; i <= sel_row_end; ++i) {
                for (int j = sel_col_start; j <= sel_col_end; ++j) {
-                       tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
+                       tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear(bp.tracking_changes);
                }
        }
        return true;
@@ -2593,12 +2483,6 @@ int InsetTabular::scroll(bool recursive) const
 }
 
 
-bool InsetTabular::doClearArea() const
-{
-       return !locked || (need_update & (FULL|INIT));
-}
-
-
 void InsetTabular::getSelection(int & srow, int & erow,
                                int & scol, int & ecol) const
 {
@@ -2621,31 +2505,10 @@ void InsetTabular::getSelection(int & srow, int & erow,
 }
 
 
-Paragraph * InsetTabular::getParFromID(int id) const
-{
-       Paragraph * result;
-       for(int i = 0; i < tabular->rows(); ++i) {
-               for(int j = 0; j < tabular->columns(); ++j) {
-                       if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
-                               return result;
-               }
-       }
-       return 0;
-}
-
-
-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;
 }
 
@@ -2674,53 +2537,53 @@ Inset * InsetTabular::getInsetFromID(int id_arg) const
 }
 
 
-string const
+WordLangTuple const
 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
 {
        nodraw(true);
        if (the_locking_inset) {
-               string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
-               if (!str.empty()) {
+               WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
+               if (!word.word().empty()) {
                        nodraw(false);
-                       return str;
+                       return word;
                }
                if (tabular->IsLastCell(actcell)) {
                        bv->unlockInset(const_cast<InsetTabular *>(this));
                        nodraw(false);
-                       return string();
+                       return WordLangTuple();
                }
                ++actcell;
        }
        // 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, 0);
-       string const str(selectNextWordInt(bv, value));
+       inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
+       WordLangTuple word(selectNextWordInt(bv, value));
        nodraw(false);
-       if (!str.empty())
+       if (!word.word().empty())
                resetPos(bv);
-       return str;
+       return word;
 }
 
 
-string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
+WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
 {
        // when entering this function the inset should be ALWAYS locked!
        lyx::Assert(the_locking_inset);
 
-       string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
-       if (!str.empty())
-               return str;
+       WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
+       if (!word.word().empty())
+               return word;
 
        if (tabular->IsLastCell(actcell)) {
                bv->unlockInset(const_cast<InsetTabular *>(this));
-               return string();
+               return WordLangTuple();
        }
 
        // 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);
 }
 
@@ -2743,59 +2606,97 @@ void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
 }
 
 
+void InsetTabular::markErased()
+{
+       int cell = 0;
+
+       while (!tabular->IsLastCell(cell)) {
+               ++cell;
+               InsetText * inset = tabular->GetCellInset(cell);
+               inset->markErased();
+       }
+}
+
+
+bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
+{
+       if (the_locking_inset) {
+               if (the_locking_inset->nextChange(bv, length)) {
+                       updateLocal(bv, CELL);
+                       return true;
+               }
+               if (tabular->IsLastCell(actcell))
+                       return false;
+               ++actcell;
+       }
+       InsetText * inset = tabular->GetCellInset(actcell);
+       if (inset->nextChange(bv, length)) {
+               updateLocal(bv, FULL);
+               return true;
+       }
+       while (!tabular->IsLastCell(actcell)) {
+               ++actcell;
+               inset = tabular->GetCellInset(actcell);
+               if (inset->nextChange(bv, length)) {
+                       updateLocal(bv, FULL);
+                       return true;
+               }
+       }
+       return false;
+}
+
+
 bool InsetTabular::searchForward(BufferView * bv, string const & str,
                                 bool cs, bool mw)
 {
-       nodraw(true);
+       int cell = 0;
        if (the_locking_inset) {
                if (the_locking_inset->searchForward(bv, str, cs, mw)) {
-                       nodraw(false);
-                       updateLocal(bv, CELL, false);
+                       updateLocal(bv, CELL);
                        return true;
                }
-               if (tabular->IsLastCell(actcell)) {
-                       nodraw(false);
-                       bv->unlockInset(const_cast<InsetTabular *>(this));
+               if (tabular->IsLastCell(actcell))
                        return false;
+               cell = actcell + 1;
+       }
+       InsetText * inset = tabular->GetCellInset(cell);
+       if (inset->searchForward(bv, str, cs, mw)) {
+               updateLocal(bv, FULL);
+               return true;
+       }
+       while (!tabular->IsLastCell(cell)) {
+               ++cell;
+               inset = tabular->GetCellInset(cell);
+               if (inset->searchForward(bv, str, cs, mw)) {
+                       updateLocal(bv, FULL);
+                       return true;
                }
-               ++actcell;
        }
-       // otherwise we have to lock the next inset and search there
-       UpdatableInset * inset =
-               static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
-       inset->edit(bv);
-       bool const ret = searchForward(bv, str, cs, mw);
-       nodraw(false);
-       updateLocal(bv, CELL, false);
-       return ret;
+       return false;
 }
 
 
 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
                               bool cs, bool mw)
 {
-       nodraw(true);
+       int cell = tabular->GetNumberOfCells();
        if (the_locking_inset) {
                if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
-                       nodraw(false);
-                       updateLocal(bv, CELL, false);
+                       updateLocal(bv, CELL);
                        return true;
                }
-               if (!actcell) { // we are already in the first cell
-                       nodraw(false);
-                       bv->unlockInset(const_cast<InsetTabular *>(this));
-                       return false;
+               cell = actcell;
+       }
+
+       while (cell) {
+               --cell;
+               InsetText * inset = tabular->GetCellInset(cell);
+               if (inset->searchBackward(bv, str, cs, mw)) {
+                       updateLocal(bv, CELL);
+                       return true;
                }
-               --actcell;
        }
-       // otherwise we have to lock the next inset and search there
-       UpdatableInset * inset =
-               static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
-       inset->edit(bv, false);
-       bool const ret = searchBackward(bv, str, cs, mw);
-       nodraw(false);
-       updateLocal(bv, CELL, false);
-       return ret;
+       return false;
 }
 
 
@@ -2869,12 +2770,14 @@ bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
                ocol = actcol;
                row = actrow;
        }
+
        string::size_type op = 0;
        int cells = loctab->GetNumberOfCells();
        p = 0;
        cols = ocol;
        rows = loctab->rows();
        int const columns = loctab->columns();
+
        while ((cell < cells) && (p < len) && (row < rows) &&
               (p = buf.find_first_of("\t\n", p)) != string::npos)
        {
@@ -2886,8 +2789,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;
                        }
@@ -2897,8 +2800,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;
@@ -2913,9 +2816,99 @@ 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;
 }
+
+
+void InsetTabular::addPreview(grfx::PreviewLoader & loader) const
+{
+       int const rows = tabular->rows();
+       int const columns = tabular->columns();
+       for (int i = 0; i < rows; ++i) {
+               for (int j = 0; j < columns; ++j) {
+                       tabular->GetCellInset(i,j)->addPreview(loader);
+               }
+       }
+}
+
+
+string const InsetTabularMailer:: name_("tabular");
+
+InsetTabularMailer::InsetTabularMailer(InsetTabular & inset)
+       : inset_(inset)
+{}
+
+
+string const InsetTabularMailer::inset2string() const
+{
+       return params2string(inset_);
+}
+
+
+int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
+{
+       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();
+               if (token != name_)
+                       return -1;
+       }
+
+       int cell = -1;
+       if (lex.isOK()) {
+               lex.next();
+               string const token = lex.getString();
+               if (token != "\\active_cell")
+                       return -1;
+               lex.next();
+               cell = lex.getInteger();
+       }
+
+       // This is part of the inset proper that is usually swallowed
+       // by Buffer::readInset
+       if (lex.isOK()) {
+               lex.next();
+               string const token = lex.getString();
+               if (token != "Tabular")
+                       return -1;
+       }
+
+       if (!lex.isOK())
+               return -1;
+
+       BufferView * const bv = inset.view();
+       Buffer const * const buffer = bv ? bv->buffer() : 0;
+       if (buffer)
+               inset.read(buffer, lex);
+
+       // We can't set the active cell, but we can tell the frontend
+       // what it is.
+       return cell;
+}
+
+
+string const InsetTabularMailer::params2string(InsetTabular const & inset)
+{
+       BufferView * const bv = inset.view();
+       Buffer const * const buffer = bv ? bv->buffer() : 0;
+       if (!buffer)
+               return string();
+
+       ostringstream data;
+       data << name_ << " \\active_cell " << inset.getActCell() << '\n';
+       inset.write(buffer, data);
+       data << "\\end_inset\n";
+       return STRCONV(data.str());
+}