]> git.lyx.org Git - features.git/blobdiff - src/insets/insettabular.C
Hopefully fixed the redo problems with insets!
[features.git] / src / insets / insettabular.C
index a343e41cd93c52344afd1a701967aa38b7698883..703c29742d7df88381a4166160ea3fd6acfcd7c0 100644 (file)
 #include "LyXView.h"
 #include "insets/insettext.h"
 #include "frontends/Dialogs.h"
+#include "frontends/Alert.h"
 #include "debug.h"
 #include "WorkArea.h"
 #include "gettext.h"
 #include "language.h"
 #include "BufferView.h"
 #include "undo_funcs.h"
+#include "support/LAssert.h"
+#include "support/lstrings.h"
 
 using std::ostream;
 using std::ifstream;
@@ -136,22 +139,24 @@ InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
        actrow = actcell = 0;
        clearSelection();
        need_update = INIT;
+       in_update = false;
+       in_reset_pos = false;
 }
 
 
 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
                                                   bool same_id)
-       : buffer(&buf)
+       : UpdatableInset(tab, same_id), buffer(&buf)
 {
-       tabular.reset(new LyXTabular(this, *(tab.tabular)));
+       tabular.reset(new LyXTabular(this, *(tab.tabular), same_id));
        the_locking_inset = 0;
        locked = no_selection = false;
        oldcell = -1;
        actrow = actcell = 0;
        sel_cell_start = sel_cell_end = 0;
        need_update = INIT;
-       if (same_id)
-               id_ = tab.id_;
+       in_update = false;
+       in_reset_pos = false;
 }
 
 
@@ -176,7 +181,7 @@ void InsetTabular::write(Buffer const * buf, ostream & os) const
 
 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
 {
-       bool const old_format = (lex.GetString() == "\\LyXTable");
+       bool const old_format = (lex.getString() == "\\LyXTable");
 
        //if (tabular)
        //delete tabular;
@@ -189,10 +194,10 @@ void InsetTabular::read(Buffer const * buf, LyXLex & lex)
                return;
 
        lex.nextToken();
-       string token = lex.GetString();
-       while (lex.IsOK() && (token != "\\end_inset")) {
+       string token = lex.getString();
+       while (lex.isOK() && (token != "\\end_inset")) {
                lex.nextToken();
-               token = lex.GetString();
+               token = lex.getString();
        }
        if (token != "\\end_inset") {
                lex.printError("Missing \\end_inset at this point. "
@@ -247,7 +252,8 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                x += static_cast<float>(scroll());
 #endif
        if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
-                        (top_x != int(x)) || (top_baseline != baseline))) {
+                        (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();
@@ -259,11 +265,12 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                        h = pain.paperHeight();
                if ((top_x + w) > pain.paperWidth())
                        w = pain.paperWidth();
-               pain.fillRectangle(tx, ty, w, h);
+               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) {
@@ -311,11 +318,12 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                        Inset * inset = tabular->GetCellInset(cell);
                        for (i = 0;
                             inset != the_locking_inset && i < tabular->rows();
-                            ++i) {
+                            ++i)
+                       {
                                for (j = 0;
-                                    inset != the_locking_inset
-                                            && j < tabular->columns();
-                                    ++j) {
+                                    inset != the_locking_inset && j < tabular->columns();
+                                    ++j)
+                               {
                                        if (tabular->IsPartOfMultiColumn(i, j))
                                                continue;
                                        nx += tabular->GetWidthOfColumn(cell);
@@ -347,26 +355,22 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                }
                i = tabular->row_of_cell(cell);
                if (the_locking_inset != tabular->GetCellInset(cell)) {
-                       lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
+                       lyxerr[Debug::INSETTEXT] << "ERROR this shouldn't happen\n";
                        return;
                }
                float dx = nx + tabular->GetBeginningOfTextInCell(cell);
                float cx = dx;
-               //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
                tabular->GetCellInset(cell)->draw(bv, font, baseline, dx, false);
-#if 0
-               if (bv->text->status == LyXText::CHANGED_IN_DRAW)
-                       return;
-#endif
                // clear only if we didn't have a change
-               if (need_update == CELL) {
+               if (bv->text->status() != LyXText::CHANGED_IN_DRAW) {
                        // clear before the inset
                        pain.fillRectangle(
                                nx + 1,
                                baseline - tabular->GetAscentOfRow(i) + 1,
                                int(cx - nx - 1),
                                tabular->GetAscentOfRow(i) +
-                               tabular->GetDescentOfRow(i) - 1);
+                               tabular->GetDescentOfRow(i) - 1,
+                               backgroundColor());
                        // clear behind the inset
                        pain.fillRectangle(
                                int(cx + the_locking_inset->width(bv,font) + 1),
@@ -376,7 +380,20 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                                the_locking_inset->width(bv,font) -
                                tabular->GetAdditionalWidth(cell) - 1,
                                tabular->GetAscentOfRow(i) +
-                               tabular->GetDescentOfRow(i) - 1);
+                               tabular->GetDescentOfRow(i) - 1,
+                               backgroundColor());
+                       // clear below the inset
+                       pain.fillRectangle(
+                               nx + 1,
+                               baseline + the_locking_inset->descent(bv, font) + 1,
+                               tabular->GetWidthOfColumn(cell) -
+                               tabular->GetAdditionalWidth(cell) - 1,
+                               tabular->GetAscentOfRow(i) +
+                               tabular->GetDescentOfRow(i) -
+                               the_locking_inset->ascent(bv, font) -
+                               the_locking_inset->descent(bv, font) -
+                               TEXT_TO_INSET_OFFSET - 1,
+                               backgroundColor());
                }
        }
        x -= ADD_TO_TABULAR_WIDTH;
@@ -455,11 +472,22 @@ void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
 
 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
 {
+       if (in_update) {
+               if (reinit) {
+                       resetPos(bv);
+                       if (owner())
+                               owner()->update(bv, font, true);
+               }
+               return;
+       }
+       in_update = true;
        if (reinit) {
                need_update = INIT;
-               calculate_dimensions_of_cells(bv, font, true);
+               if (calculate_dimensions_of_cells(bv, font, true))
+                       resetPos(bv);
                if (owner())
                        owner()->update(bv, font, true);
+               in_update = false;
                return;
        }
        if (the_locking_inset)
@@ -483,6 +511,7 @@ void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
        default:
                break;
        }
+       in_update = false;
 }
 
 
@@ -497,7 +526,7 @@ void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int button)
        UpdatableInset::edit(bv, x, y, button);
        
        if (!bv->lockInset(this)) {
-               lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
+               lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
                return;
        }
        locked = true;
@@ -510,8 +539,28 @@ void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int button)
        if (insetHit(bv, x, y) && (button != 3)) {
                activateCellInsetAbs(bv, x, y, button);
        }
-//    UpdateLocal(bv, NONE, false);
-//    bv->getOwner()->getPopups().updateFormTabular();
+}
+
+
+void InsetTabular::edit(BufferView * bv, bool front)
+{
+       UpdatableInset::edit(bv, front);
+       
+       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;
+       if (front)
+               actcell = 0;
+       else
+               actcell = tabular->GetNumberOfCells() - 1;
+       sel_cell_start = sel_cell_end = actcell;
+       resetPos(bv);
+       finishUndo();
 }
 
 
@@ -553,28 +602,24 @@ void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
 
 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
 {
-       lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
+       lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
                              << inset << "): ";
        if (!inset)
                return false;
        oldcell = -1;
        if (inset == tabular->GetCellInset(actcell)) {
-               lyxerr[Debug::INSETS] << "OK" << endl;
+               lyxerr[Debug::INSETTEXT] << "OK" << endl;
                the_locking_inset = tabular->GetCellInset(actcell);
                resetPos(bv);
-               inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
-               inset_y = cursor_.y();
                return true;
        } else if (the_locking_inset && (the_locking_inset == inset)) {
-               lyxerr[Debug::INSETS] << "OK" << endl;
+               lyxerr[Debug::INSETTEXT] << "OK" << endl;
                resetPos(bv);
-               inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
-               inset_y = cursor_.y();
        } else if (the_locking_inset) {
-               lyxerr[Debug::INSETS] << "MAYBE" << endl;
+               lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
                return the_locking_inset->lockInsetInInset(bv, inset);
        }
-       lyxerr[Debug::INSETS] << "NOT OK" << endl;
+       lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
        return false;
 }
 
@@ -632,9 +677,10 @@ unsigned int InsetTabular::insetInInsetY()
 }
 
 
-UpdatableInset * InsetTabular::getLockingInset()
+UpdatableInset * InsetTabular::getLockingInset() const
 {
-       return the_locking_inset ? the_locking_inset->getLockingInset() : this;
+       return the_locking_inset ? the_locking_inset->getLockingInset() :
+               const_cast<InsetTabular *>(this);
 }
 
 
@@ -770,9 +816,8 @@ void InsetTabular::insetKeyPress(XKeyEvent * xke)
 
 
 UpdatableInset::RESULT
-InsetTabular::localDispatch(BufferView * bv,
-                           kb_action action,
-                           string const & arg)
+InsetTabular::localDispatch(BufferView * bv, kb_action action,
+                            string const & arg)
 {
        // We need to save the value of the_locking_inset as the call to 
        // the_locking_inset->LocalDispatch might unlock it.
@@ -791,34 +836,33 @@ InsetTabular::localDispatch(BufferView * bv,
        bool hs = hasSelection();
 
        result = DISPATCHED;
-       // this one have priority over the locked InsetText!
-       switch (action) {
-       case LFUN_SHIFT_TAB:
-       case LFUN_TAB:
-       {
-               if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) != this)
+       // 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);
+                       if (the_locking_inset) {
+                               unlockInsetInInset(bv, the_locking_inset);
+                               the_locking_inset = 0;
+                       }
+                       if (action == LFUN_TAB)
+                               moveNextCell(bv, old_locking_inset != 0);
+                       else
+                               movePrevCell(bv, old_locking_inset != 0);
+                       sel_cell_start = sel_cell_end = actcell;
+                       if (hs)
+                               updateLocal(bv, SELECTION, false);
+                       if (!the_locking_inset) {
+                               showInsetCursor(bv);
+                               return DISPATCHED_NOUPDATE;
+                       }
+                       return result;
+               // this to avoid compiler warnings.
+               default:
                        break;
-               hideInsetCursor(bv);
-               if (the_locking_inset) {
-                       unlockInsetInInset(bv, the_locking_inset);
-                       the_locking_inset = 0;
-               }
-               if (action == LFUN_TAB)
-                       moveNextCell(bv, old_locking_inset != 0);
-               else
-                       movePrevCell(bv, old_locking_inset != 0);
-               sel_cell_start = sel_cell_end = actcell;
-               if (hs)
-                       updateLocal(bv, SELECTION, false);
-               if (!the_locking_inset) {
-                       showInsetCursor(bv);
-                       return DISPATCHED_NOUPDATE;
                }
-               return result;
-       }
-       // this to avoid compiler warnings.
-       default:
-               break;
        }
 
        if (the_locking_inset) {
@@ -837,7 +881,12 @@ InsetTabular::localDispatch(BufferView * bv,
                        updateLocal(bv, CELL, false);
                        the_locking_inset->toggleInsetCursor(bv);
                        return result;
-               } else if (result == FINISHED) {
+               } else if (result == FINISHED_UP) {
+                       action = LFUN_UP;
+               } else if (result == FINISHED_DOWN) {
+                       action = LFUN_DOWN;
+               } else if (result == FINISHED_RIGHT) {
+                       action = LFUN_RIGHT;
                }
        }
 
@@ -988,7 +1037,7 @@ InsetTabular::localDispatch(BufferView * bv,
                        int cols = 1;
                        int rows = 1;
                        int maxCols = 1;
-                       unsigned int len = clip.length();
+                       string::size_type len = clip.length();
                        string::size_type p = 0;
 
                        while(p < len &&
@@ -1065,23 +1114,23 @@ InsetTabular::localDispatch(BufferView * bv,
                        // reset need_update setted in above function!
                        need_update = NONE;
                        result = the_locking_inset->localDispatch(bv, action, arg);
-                       if ((result == UNDISPATCHED) || (result == FINISHED)) {
+                       if ((result == UNDISPATCHED) || (result >= FINISHED)) {
                                unlockInsetInInset(bv, the_locking_inset);
                                nodraw(false);
                                the_locking_inset = 0;
                                // we need to update if this was requested before
                                updateLocal(bv, NONE, false);
                                return UNDISPATCHED;
+                       } else if (hs) {
+                               clearSelection();
                        }
                        nodraw(false);
-//                     the_locking_inset->ToggleInsetCursor(bv);
                        updateLocal(bv, CELL, false);
-//                     the_locking_inset->ToggleInsetCursor(bv);
                        return result;
                }
                break;
        }
-       if (result!=FINISHED) {
+       if (result < FINISHED) {
                if (!the_locking_inset) {
                        showInsetCursor(bv);
                }
@@ -1105,15 +1154,33 @@ int InsetTabular::ascii(Buffer const * buf, ostream & os, int) const
 }
 
 
-int InsetTabular::linuxdoc(Buffer const *, ostream &) const
+int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
 {
-       return 0;
+       return tabular->Ascii(buf,os);
 }
 
 
-int InsetTabular::docBook(Buffer const * buf, ostream & os) const
+int InsetTabular::docbook(Buffer const * buf, ostream & os) const
 {
-       return tabular->DocBook(buf,os);
+       int ret = 0;
+       Inset * master;
+
+       // if the table is inside a float it doesn't need the informaltable
+       // wrapper. Search for it.
+       for(master = owner();
+           master && master->lyxCode() != Inset::FLOAT_CODE;
+           master = master->owner());
+
+       if (!master) {
+               os << "<informaltable>\n";
+               ret++;
+       }
+       ret+= tabular->DocBook(buf,os);
+       if (!master) {
+               os << "</informaltable>\n";
+               ret++;
+       }
+       return ret;
 }
 
 
@@ -1124,8 +1191,8 @@ void InsetTabular::validate(LaTeXFeatures & features) const
 
 
 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
-                                                LyXFont const & font,
-                                                bool reinit) const
+                                                 LyXFont const & font,
+                                                 bool reinit) const
 {
        int cell = -1;
        int maxAsc = 0;
@@ -1148,9 +1215,6 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
                changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
                return changed;
        }
-#if 0
-       cur_cell = -1;
-#endif
        for (int i = 0; i < tabular->rows(); ++i) {
                maxAsc = 0;
                maxDesc = 0;
@@ -1159,7 +1223,7 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
                                continue;
                        ++cell;
                        inset = tabular->GetCellInset(cell);
-                       if (!reinit)
+                       if (!reinit && !tabular->GetPWidth(cell).empty())
                                inset->update(bv, font, false);
                        maxAsc = max(maxAsc, inset->ascent(bv, font));
                        maxDesc = max(maxDesc, inset->descent(bv, font));
@@ -1172,9 +1236,12 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
 }
 
 
-void InsetTabular::getCursorPos(BufferView *,
-                               int & x, int & y) const
+void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
 {
+       if (the_locking_inset) {
+               the_locking_inset->getCursorPos(bv, x, y);
+               return;
+       }
        x = cursor_.x() - top_x;
        y = cursor_.y();
 }
@@ -1221,8 +1288,20 @@ void InsetTabular::hideInsetCursor(BufferView * bv)
                bv->hideLockedInsetCursor();
                setCursorVisible(false);
        }
-//    if (cursor_visible)
-//        ToggleInsetCursor(bv);
+}
+
+
+void InsetTabular::fitInsetCursor(BufferView * bv) const
+{
+       if (the_locking_inset) {
+               the_locking_inset->fitInsetCursor(bv);
+               return;
+       }
+       LyXFont font;
+       
+       int const asc = lyxfont::maxAscent(font);
+       int const desc = lyxfont::maxDescent(font);
+       bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
 }
 
 
@@ -1234,7 +1313,7 @@ void InsetTabular::setPos(BufferView * bv, int x, int y) const
        int ly = tabular->GetDescentOfRow(actrow);
 
        // first search the right row
-       while((ly < y) && (actrow < tabular->rows())) {
+       while((ly < y) && ((actrow+1) < tabular->rows())) {
                cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
                                 tabular->GetAscentOfRow(actrow + 1) +
                                 tabular->GetAdditionalHeight(actrow + 1));
@@ -1246,21 +1325,10 @@ void InsetTabular::setPos(BufferView * bv, int x, int y) const
        // now search the right column
        int lx = tabular->GetWidthOfColumn(actcell) -
                tabular->GetAdditionalWidth(actcell);
-#if 0
-#ifdef WITH_WARNINGS
-#warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
-#endif
-       for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
-            ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
-                    tabular->GetAdditionalWidth(actcell - 1));
-#else
-       // Jürgen, you should check that this is correct. (Lgb)
        for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
                lx += tabular->GetWidthOfColumn(actcell + 1)
                        + tabular->GetAdditionalWidth(actcell);
        }
-       
-#endif
        cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
        resetPos(bv);
 }
@@ -1282,8 +1350,14 @@ int InsetTabular::getCellXPos(int cell) const
 
 void InsetTabular::resetPos(BufferView * bv) const
 {
-       if (!locked)
+       if (!locked || nodraw())
+               return;
+#warning This should be fixed in the right manner (20011128 Jug)
+       // fast hack to fix infinite repaintings!
+       if (in_reset_pos)
                return;
+       in_reset_pos = true;
+
        actcol = tabular->column_of_cell(actcell);
 
        int cell = 0;
@@ -1336,6 +1410,10 @@ void InsetTabular::resetPos(BufferView * bv) const
                scroll(bv, old_x - cursor_.x());
                updateLocal(bv, FULL, false);
        }
+       if (the_locking_inset) {
+               inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
+               inset_y = cursor_.y();
+       }
        if ((!the_locking_inset ||
             !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
            actcell != oldcell) {
@@ -1343,6 +1421,7 @@ void InsetTabular::resetPos(BufferView * bv) const
                bv->owner()->getDialogs()->updateTabular(inset);
                oldcell = actcell;
        }
+       in_reset_pos = false;
 }
 
 
@@ -1355,7 +1434,7 @@ UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
                bool moved = isRightToLeft(bv)
                        ? movePrevCell(bv) : moveNextCell(bv);
                if (!moved)
-                       return FINISHED;
+                       return FINISHED_RIGHT;
                if (lock && activateCellInset(bv))
                        return DISPATCHED;
        }
@@ -1383,7 +1462,7 @@ UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
        int const ocell = actcell;
        actcell = tabular->GetCellAbove(actcell);
        if (actcell == ocell) // we moved out of the inset
-               return FINISHED;
+               return FINISHED_UP;
        resetPos(bv);
        if (lock) {
                int x = 0;
@@ -1404,7 +1483,7 @@ UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
        int const ocell = actcell;
        actcell = tabular->GetCellBelow(actcell);
        if (actcell == ocell) // we moved out of the inset
-               return FINISHED;
+               return FINISHED_DOWN;
        resetPos(bv);
        if (lock) {
                int x = 0;
@@ -1478,10 +1557,8 @@ bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
 }
 
 
-bool InsetTabular::Delete()
+bool InsetTabular::deletable() const
 {
-#warning Is this func correctly named? Or should it be "deletable"? (Lgb?)
-#warning I guess this could be 'deletable'! (Jug)
        return true;
 }
 
@@ -1546,8 +1623,8 @@ bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
 
 
 void InsetTabular::tabularFeatures(BufferView * bv,
-                                  LyXTabular::Feature feature,
-                                  string const & value)
+                                   LyXTabular::Feature feature,
+                                   string const & value)
 {
        int sel_col_start;
        int sel_col_end;
@@ -1595,10 +1672,11 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                bv->text->cursor.par(),
                bv->text->cursor.par()->next());
 
-       int row = tabular->row_of_cell(actcell);
+       LyXTabular::ltType ltt;
+       int row = ltt.row = tabular->row_of_cell(actcell);
        int column = tabular->column_of_cell(actcell);
        bool flag = true;
-       
+
        switch (feature) {
        case LyXTabular::SET_PWIDTH:
        {
@@ -1629,6 +1707,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
        case LyXTabular::SET_SPECIAL_COLUMN:
        case LyXTabular::SET_SPECIAL_MULTI:
                tabular->SetAlignSpecial(actcell,value,feature);
+               updateLocal(bv, FULL, true);
                break;
        case LyXTabular::APPEND_ROW:
                // append the row into the tabular
@@ -1645,19 +1724,23 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                break;
        case LyXTabular::DELETE_ROW:
                unlockInsetInInset(bv, the_locking_inset);
-               tabular->DeleteRow(tabular->row_of_cell(actcell));
-               if ((row+1) > tabular->rows())
-                       --row;
-               actcell = tabular->GetCellNumber(row, column);
+               for(int i = sel_row_start; i <= sel_row_end; ++i) {
+                       tabular->DeleteRow(sel_row_start);
+               }
+               if (sel_row_start >= tabular->rows())
+                       --sel_row_start;
+               actcell = tabular->GetCellNumber(sel_row_start, column);
                clearSelection();
                updateLocal(bv, INIT, true);
                break;
        case LyXTabular::DELETE_COLUMN:
                unlockInsetInInset(bv, the_locking_inset);
-               tabular->DeleteColumn(tabular->column_of_cell(actcell));
-               if ((column+1) > tabular->columns())
-                       --column;
-               actcell = tabular->GetCellNumber(row, column);
+               for(int i = sel_col_start; i <= sel_col_end; ++i) {
+                       tabular->DeleteColumn(sel_col_start);
+               }
+               if (sel_col_start >= tabular->columns())
+                       --sel_col_start;
+               actcell = tabular->GetCellNumber(row, sel_col_start);
                clearSelection();
                updateLocal(bv, INIT, true);
                break;
@@ -1752,7 +1835,7 @@ void InsetTabular::tabularFeatures(BufferView * bv,
        case LyXTabular::MULTICOLUMN:
        {
                if (sel_row_start != sel_row_end) {
-                       WriteAlert(_("Impossible Operation!"), 
+                       Alert::alert(_("Impossible Operation!"), 
                                   _("Multicolumns can only be horizontally."), 
                                   _("Sorry."));
                        return;
@@ -1834,17 +1917,25 @@ void InsetTabular::tabularFeatures(BufferView * bv,
                                        tabular->GetCellNumber(i, j), val);
                break;
        }
+       case LyXTabular::UNSET_LTFIRSTHEAD:
+               ltt.row = 0;
        case LyXTabular::SET_LTFIRSTHEAD:
-               tabular->SetLTHead(actcell, true);
+               tabular->SetLTHead(ltt, true);
                break;
+       case LyXTabular::UNSET_LTHEAD:
+               ltt.row = 0;
        case LyXTabular::SET_LTHEAD:
-               tabular->SetLTHead(actcell, false);
+               tabular->SetLTHead(ltt, false);
                break;
+       case LyXTabular::UNSET_LTFOOT:
+               ltt.row = 0;
        case LyXTabular::SET_LTFOOT:
-               tabular->SetLTFoot(actcell, false);
+               tabular->SetLTFoot(ltt, false);
                break;
+       case LyXTabular::UNSET_LTLASTFOOT:
+               ltt.row = 0;
        case LyXTabular::SET_LTLASTFOOT:
-               tabular->SetLTFoot(actcell, true);
+               tabular->SetLTFoot(ltt, true);
                break;
        case LyXTabular::SET_LTNEWPAGE:
        {
@@ -1916,7 +2007,7 @@ int InsetTabular::getMaxWidth(BufferView * bv,
                cell = actcell;
                if (tabular->GetCellInset(cell) != inset) {
                        
-                       lyxerr << "Actcell not equal to actual cell!\n";
+                       lyxerr[Debug::INSETTEXT] << "Actcell not equal to actual cell!\n";
                        cell = -1;
                }
        }
@@ -1965,10 +2056,19 @@ void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
 
 
 LyXText * InsetTabular::getLyXText(BufferView const * bv,
-                                  bool const recursive) const
+                                   bool const recursive) const
 {
        if (the_locking_inset)
                return the_locking_inset->getLyXText(bv, recursive);
+#if 0
+       // if we're locked lock the actual insettext and return it's LyXText!!!
+       if (locked) {
+               UpdatableInset * inset =
+                       static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
+               inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
+               return the_locking_inset->getLyXText(bv, recursive);
+       }
+#endif
        return Inset::getLyXText(bv, recursive);
 }
 
@@ -1996,6 +2096,7 @@ void InsetTabular::openLayoutDialog(BufferView * bv) const
                const_cast<InsetTabular *>(this));
 }
 
+
 //
 // functions returns:
 // 0 ... disabled
@@ -2026,6 +2127,7 @@ func_status::value_type InsetTabular::getStatus(string const & what) const
        int sel_row_start;
        int sel_row_end;
        int dummy;
+       LyXTabular::ltType dummyltt;
        bool flag = true;
 
        if (hasSelection()) {
@@ -2180,25 +2282,25 @@ func_status::value_type InsetTabular::getStatus(string const & what) const
                        status |= func_status::ToggleOff;
                break;
        case LyXTabular::SET_LTFIRSTHEAD:
-               if (tabular->GetRowOfLTHead(actcell, dummy))
+               if (tabular->GetRowOfLTHead(actcell, dummyltt))
                        status |= func_status::ToggleOn;
                else
                        status |= func_status::ToggleOff;
                break;
        case LyXTabular::SET_LTHEAD:
-               if (tabular->GetRowOfLTHead(actcell, dummy))
+               if (tabular->GetRowOfLTHead(actcell, dummyltt))
                        status |= func_status::ToggleOn;
                else
                        status |= func_status::ToggleOff;
                break;
        case LyXTabular::SET_LTFOOT:
-               if (tabular->GetRowOfLTFoot(actcell, dummy))
+               if (tabular->GetRowOfLTFoot(actcell, dummyltt))
                        status |= func_status::ToggleOn;
                else
                        status |= func_status::ToggleOff;
                break;
        case LyXTabular::SET_LTLASTFOOT:
-               if (tabular->GetRowOfLTFoot(actcell, dummy))
+               if (tabular->GetRowOfLTFoot(actcell, dummyltt))
                        status |= func_status::ToggleOn;
                else
                        status |= func_status::ToggleOff;
@@ -2307,11 +2409,6 @@ bool InsetTabular::cutSelection()
        if (!hasSelection())
                return false;
 
-       //int sel_col_start;
-       //int sel_col_end;
-       //int sel_row_start;
-       //int sel_row_end;
-
        int sel_col_start = tabular->column_of_cell(sel_cell_start);
        int sel_col_end = tabular->column_of_cell(sel_cell_end);
        if (sel_col_start > sel_col_end) {
@@ -2323,15 +2420,9 @@ bool InsetTabular::cutSelection()
        int sel_row_start = tabular->row_of_cell(sel_cell_start);
        int sel_row_end = tabular->row_of_cell(sel_cell_end);
        if (sel_row_start > sel_row_end) {
-               //int tmp = sel_row_start;
-               //sel_row_start = sel_row_end;
-               //sel_row_end = tmp;
                swap(sel_row_start, sel_row_end);
        }
        if (sel_cell_start > sel_cell_end) {
-               //int tmp = sel_cell_start;
-               //sel_cell_start = sel_cell_end;
-               //sel_cell_end = tmp;
                swap(sel_cell_start, sel_cell_end);
        }
        for (int i = sel_row_start; i <= sel_row_end; ++i) {
@@ -2389,11 +2480,12 @@ void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol)
                        ecol = tabular->right_column_of_cell(sel_cell_end);
 }
 
+
 Paragraph * InsetTabular::getParFromID(int id) const
 {
        Paragraph * result;
-       for(int i=0; i < tabular->rows(); ++i) {
-               for(int j=0; j < tabular->columns(); ++j) {
+       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;
                }
@@ -2401,6 +2493,7 @@ Paragraph * InsetTabular::getParFromID(int id) const
        return 0;
 }
 
+
 Paragraph * InsetTabular::firstParagraph() const
 {
        if (the_locking_inset)
@@ -2408,6 +2501,15 @@ Paragraph * InsetTabular::firstParagraph() const
        return 0;
 }
 
+
+Paragraph * InsetTabular::getFirstParagraph(int i) const
+{
+       return (i < tabular->GetNumberOfCells())
+               ? tabular->GetCellInset(i)->getFirstParagraph(0)
+               : 0;
+}
+
+
 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
 {
        if (the_locking_inset)
@@ -2430,3 +2532,129 @@ Inset * InsetTabular::getInsetFromID(int id_arg) const
        }
        return 0;
 }
+
+
+string const InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
+{
+       if (the_locking_inset) {
+               string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
+               if (!str.empty())
+                       return str;
+               if (tabular->IsLastCell(actcell)) {
+                       bv->unlockInset(const_cast<InsetTabular *>(this));
+                       return string();
+               }
+               ++actcell;
+       }
+       nodraw(true);
+       // 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));
+       nodraw(false);
+       if (!str.empty())
+               resetPos(bv);
+       return str;
+}
+
+
+string 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;
+
+       if (tabular->IsLastCell(actcell)) {
+               bv->unlockInset(const_cast<InsetTabular *>(this));
+               return string();
+       }
+       
+       // 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);
+       return selectNextWordInt(bv, value);
+}
+
+
+void InsetTabular::selectSelectedWord(BufferView * bv)
+{
+       if (the_locking_inset) {
+               the_locking_inset->selectSelectedWord(bv);
+               return;
+       }
+       return;
+}
+
+
+void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
+{
+       if (the_locking_inset) {
+               the_locking_inset->toggleSelection(bv, kill_selection);
+       }
+}
+
+
+bool InsetTabular::searchForward(BufferView * bv, string const & str,
+                                 bool const & cs, bool const & mw)
+{
+       if (the_locking_inset) {
+               if (the_locking_inset->searchForward(bv, str, cs, mw))
+                       return true;
+               if (tabular->IsLastCell(actcell)) {
+                       bv->unlockInset(const_cast<InsetTabular *>(this));
+                       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);
+#if 0
+       bool const res = searchForward(bv, str, cs, mw);
+       updateLocal(bv, NONE, false);
+       bv->updateInset(const_cast<InsetTabular *>(this), false);
+       return res;
+#else
+       return searchForward(bv, str, cs, mw);
+#endif
+}
+
+
+bool InsetTabular::searchBackward(BufferView * bv, string const & str,
+                               bool const & cs, bool const & mw)
+{
+       if (the_locking_inset) {
+               if (the_locking_inset->searchBackward(bv, str, cs, mw))
+                       return true;
+               if (!actcell) { // we are already in the first cell
+                       bv->unlockInset(const_cast<InsetTabular *>(this));
+                       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, false);
+#if 0
+       bool const res = searchBackward(bv, str, cs, mw);
+       bv->updateInset(const_cast<InsetTabular *>(this), false);
+       return res;
+#else
+       return searchBackward(bv, str, cs, mw);
+#endif
+}
+
+
+bool InsetTabular::insetAllowed(Inset::Code code) const
+{
+       if (the_locking_inset)
+               return the_locking_inset->insetAllowed(code);
+       return false;
+}