]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
Make it compile when USE_BOOST_FORMAT is unset
[lyx.git] / src / insets / insettabular.C
index d7c59e82bf5ee72093e0234ab0f982a1443d606a..fbe8c938e1247cca0100f21a48aabd902dc14082 100644 (file)
@@ -1,11 +1,11 @@
-/* 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>
 #endif
 
 #include "insettabular.h"
+#include "insettext.h"
 
-#include "lyx_cb.h"
 #include "buffer.h"
+#include "BufferView.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 "debug.h"
+#include "funcrequest.h"
 #include "gettext.h"
 #include "language.h"
-#include "BufferView.h"
-#include "undo_funcs.h"
+#include "LaTeXFeatures.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 "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 +86,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" },
@@ -146,7 +149,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 +169,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;
 }
@@ -551,7 +554,7 @@ string const InsetTabular::editMessage() const
 }
 
 
-void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int button)
+void InsetTabular::edit(BufferView * bv, int x, int y, mouse_button::state button)
 {
        UpdatableInset::edit(bv, x, y, button);
 
@@ -566,7 +569,7 @@ void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int button)
        setPos(bv, x, y);
        clearSelection();
        finishUndo();
-       if (insetHit(bv, x, y) && (button != 3)) {
+       if (insetHit(bv, x, y) && (button != mouse_button::button3)) {
                activateCellInsetAbs(bv, x, y, button);
        }
 }
@@ -585,10 +588,17 @@ void InsetTabular::edit(BufferView * bv, bool front)
        the_locking_inset = 0;
        inset_x = 0;
        inset_y = 0;
-       if (front)
-               actcell = 0;
-       else
-               actcell = tabular->GetNumberOfCells() - 1;
+       if (front) {
+               if (isRightToLeft(bv))
+                       actcell = tabular->GetLastCellInRow(0);
+               else
+                       actcell = 0;
+       } else {
+               if (isRightToLeft(bv))
+                       actcell = tabular->GetFirstCellInRow(tabular->rows()-1);
+               else
+                       actcell = tabular->GetNumberOfCells() - 1;
+       }
        clearSelection();
        resetPos(bv);
        bv->fitCursor();
@@ -603,6 +613,7 @@ void InsetTabular::insetUnlock(BufferView * bv)
                the_locking_inset = 0;
        }
        hideInsetCursor(bv);
+       actcell = 0;
        oldcell = -1;
        locked = false;
        if (scroll(false) || hasSelection()) {
@@ -626,15 +637,10 @@ void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
                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))
+               if (locked)
                        resetPos(bv);
        }
 }
@@ -705,7 +711,7 @@ bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
        if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
                if (inset->lyxCode() == TABULAR_CODE &&
                    !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
-                       bv->owner()->getDialogs()->updateTabular(this);
+                       bv->owner()->getDialogs().updateTabular(this);
                        oldcell = actcell;
                }
                return true;
@@ -766,18 +772,19 @@ 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, false);
        }
 
        int const ocell = actcell;
        int const orow = actrow;
+       BufferView * bv = cmd.view();
 
        hideInsetCursor(bv);
        if (!locked) {
@@ -786,12 +793,12 @@ void InsetTabular::insetButtonPress(BufferView * bv, int x, int y, int button)
                inset_x = 0;
                inset_y = 0;
        }
-       setPos(bv, x, y);
+       setPos(bv, cmd.x, cmd.y);
        if (actrow != orow)
                updateLocal(bv, NONE, false);
        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);
@@ -802,99 +809,102 @@ void InsetTabular::insetButtonPress(BufferView * bv, int x, int y, int button)
        }
 #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);
                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) {
+               cmd.view()->owner()->getDialogs().showTabular(this);
                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;
        }
 
+       BufferView * bv = cmd.view();
        hideInsetCursor(bv);
-//     int const old_cell = actcell;
+       int const old_cell = actcell;
 
-       setPos(bv, x, y);
+       setPos(bv, cmd.x, cmd.y);
        if (!hasSelection()) {
                setSelection(actcell, actcell);
-       } else {
+               updateLocal(bv, SELECTION, false);
+       } else if (old_cell != actcell) {
                setSelection(sel_cell_start, actcell);
+               updateLocal(bv, SELECTION, false);
        }
-       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 (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,12 +913,23 @@ 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) {
+               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_SHIFT_TAB:
                case LFUN_TAB:
                        hideInsetCursor(bv);
                        unlockInsetInInset(bv, the_locking_inset);
-                       if (action == LFUN_TAB)
+                       if (cmd.action == LFUN_TAB)
                                moveNextCell(bv, old_locking_inset != 0);
                        else
                                movePrevCell(bv, old_locking_inset != 0);
@@ -926,8 +947,10 @@ 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);
@@ -1063,7 +1086,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                if (bv->text->first_y + bv->painter().paperHeight() <
                    (top_baseline + tabular->GetHeightOfTabular()))
                        {
-                               bv->scrollCB(bv->text->first_y + bv->painter().paperHeight());
+                               bv->scrollDocView(bv->text->first_y + bv->painter().paperHeight());
                                code = FULL;
                                actcell = tabular->GetCellBelow(first_visible_cell) + column;
                        } else {
@@ -1082,7 +1105,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->first_y - bv->painter().paperHeight());
                        code = FULL;
                        if (top_baseline > 0)
                                actcell = column;
@@ -1104,12 +1127,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:
@@ -1120,7 +1143,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
        case LFUN_ENDBUFSEL:
                break;
        case LFUN_LAYOUT_TABULAR:
-               bv->owner()->getDialogs()->showTabular(this);
+               bv->owner()->getDialogs().showTabular(this);
                break;
        case LFUN_TABULAR_FEATURE:
                if (!tabularFeatures(bv, arg))
@@ -1143,6 +1166,8 @@ 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());
@@ -1252,7 +1277,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,7 +1292,7 @@ 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);
@@ -1326,7 +1351,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 +1363,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;
@@ -1429,8 +1458,8 @@ void InsetTabular::toggleInsetCursor(BufferView * bv)
 
        LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
 
-       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);
 
        if (isCursorVisible())
                bv->hideLockedInsetCursor();
@@ -1447,8 +1476,8 @@ void InsetTabular::showInsetCursor(BufferView * bv, bool show)
        if (!isCursorVisible()) {
                LyXFont font; // = GetFont(par, cursor.pos);
 
-               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);
                bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
                if (show)
                        bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
@@ -1477,9 +1506,10 @@ void InsetTabular::fitInsetCursor(BufferView * bv) const
        }
        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 +1564,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,14 +1585,14 @@ 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);
@@ -1590,7 +1620,7 @@ void InsetTabular::resetPos(BufferView * bv) const
        } else if ((cursor_.x() - offset) < 20) {
                scroll(bv, 20 - cursor_.x() + offset);
                updateLocal(bv, FULL, false);
-       } else if (scroll(false) && top_x > 20 &&
+       } else if (scroll() && top_x > 20 &&
                   (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
                scroll(bv, old_x - cursor_.x());
                updateLocal(bv, FULL, false);
@@ -1603,14 +1633,14 @@ 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);
+               bv->owner()->getDialogs().updateTabular(inset);
                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 +1658,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 +1672,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 +1693,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);
@@ -1706,7 +1736,7 @@ bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
        if (lock) {
                bool rtl = tabular->GetCellInset(actcell)->paragraph()->
                        isRightToLeftPar(bv->buffer()->params);
-               activateCellInset(bv, 0, 0, 0, !rtl);
+               activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
        }
        resetPos(bv);
        return true;
@@ -1735,7 +1765,7 @@ bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
        if (lock) {
                bool rtl = tabular->GetCellInset(actcell)->paragraph()->
                        isRightToLeftPar(bv->buffer()->params);
-               activateCellInset(bv, 0, 0, 0, !rtl);
+               activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
        }
        resetPos(bv);
        return true;
@@ -1758,7 +1788,7 @@ void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
                setUndo(bv, Undo::EDIT,
                        bv->text->cursor.par(),
                        bv->text->cursor.par()->next());
-               bool frozen = undo_frozen;
+               bool const frozen = undo_frozen;
                if (!frozen)
                        freezeUndo();
                // apply the fontchange on the whole selection
@@ -1802,7 +1832,7 @@ 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;
 }
@@ -1851,6 +1881,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;
@@ -1897,6 +1930,13 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                        }
                        updateLocal(bv, INIT, true);
                }
+
+               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:
@@ -2021,6 +2061,7 @@ 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(
@@ -2169,7 +2210,7 @@ void InsetTabular::tabularFeatures(BufferView * 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 =
@@ -2190,7 +2231,7 @@ bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
 
 
 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 +2255,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));
 }
 
 
@@ -2279,8 +2320,8 @@ 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));
+               bv->owner()->getDialogs().
+                       showTabular(const_cast<InsetTabular *>(this));
        return true;
 }
 
@@ -2295,7 +2336,7 @@ void InsetTabular::openLayoutDialog(BufferView * bv) const
                        return;
                }
        }
-       bv->owner()->getDialogs()->showTabular(
+       bv->owner()->getDialogs().showTabular(
                const_cast<InsetTabular *>(this));
 }
 
@@ -2324,7 +2365,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;
@@ -2391,6 +2432,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 +2545,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;
 }
 
@@ -2674,47 +2719,47 @@ 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->edit(bv, 0,  0, mouse_button::none);
+       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
@@ -2746,56 +2791,53 @@ void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
 bool InsetTabular::searchForward(BufferView * bv, string const & str,
                                 bool cs, bool mw)
 {
-       nodraw(true);
        if (the_locking_inset) {
                if (the_locking_inset->searchForward(bv, str, cs, mw)) {
-                       nodraw(false);
                        updateLocal(bv, CELL, false);
                        return true;
                }
-               if (tabular->IsLastCell(actcell)) {
-                       nodraw(false);
-                       bv->unlockInset(const_cast<InsetTabular *>(this));
+               if (tabular->IsLastCell(actcell))
                        return false;
-               }
                ++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;
+       InsetText * inset = tabular->GetCellInset(actcell);
+       if (inset->searchForward(bv, str, cs, mw)) {
+               updateLocal(bv, FULL, false);
+               return true;
+       }
+       while (!tabular->IsLastCell(actcell)) {
+               ++actcell;
+               inset = tabular->GetCellInset(actcell);
+               if (inset->searchForward(bv, str, cs, mw)) {
+                       updateLocal(bv, FULL, false);
+                       return true;
+               }
+       }
+       return false;
 }
 
 
 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
                               bool cs, bool mw)
 {
-       nodraw(true);
        if (the_locking_inset) {
                if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
-                       nodraw(false);
                        updateLocal(bv, CELL, false);
                        return true;
                }
-               if (!actcell) { // we are already in the first cell
-                       nodraw(false);
-                       bv->unlockInset(const_cast<InsetTabular *>(this));
-                       return false;
-               }
+       }
+       if (!locked)
+               actcell = tabular->GetNumberOfCells();
+
+       while (actcell) {
                --actcell;
+               InsetText * inset = tabular->GetCellInset(actcell);
+               if (inset->searchBackward(bv, str, cs, mw)) {
+                       updateLocal(bv, CELL, false);
+                       return true;
+               }
        }
-       // 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;
 }
 
 
@@ -2919,3 +2961,15 @@ bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
 
        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);
+               }
+       }
+}