]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
remove redundant code
[lyx.git] / src / insets / insettabular.C
index 2510d179c07314cb2705880c45515509a4fae819..5e3c7fd2d5415ba6470325fec27356165a0a99b0 100644 (file)
@@ -34,6 +34,7 @@
 #include "LyXView.h"
 #include "insets/insettext.h"
 #include "frontends/Dialogs.h"
+#include "frontends/Alert.h"
 #include "debug.h"
 #include "WorkArea.h"
 #include "gettext.h"
@@ -139,6 +140,7 @@ InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
        clearSelection();
        need_update = INIT;
        in_update = false;
+       in_reset_pos = false;
 }
 
 
@@ -146,7 +148,7 @@ InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
                                                   bool same_id)
        : 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;
@@ -154,6 +156,7 @@ InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
        sel_cell_start = sel_cell_end = 0;
        need_update = INIT;
        in_update = false;
+       in_reset_pos = false;
 }
 
 
@@ -352,7 +355,7 @@ 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);
@@ -523,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;
@@ -544,7 +547,7 @@ void InsetTabular::edit(BufferView * bv, bool front)
        UpdatableInset::edit(bv, front);
        
        if (!bv->lockInset(this)) {
-               lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
+               lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
                return;
        }
        locked = true;
@@ -584,6 +587,10 @@ void InsetTabular::insetUnlock(BufferView * bv)
 void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
                               bool mark_dirty) const
 {
+       if (what == INIT) {
+               LyXFont font;
+               calculate_dimensions_of_cells(bv, font, true);
+       }
        if (need_update < what) // only set this if it has greater update
                need_update = what;
        if ((what == INIT) && hasSelection())
@@ -599,24 +606,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);
                return true;
        } else if (the_locking_inset && (the_locking_inset == inset)) {
-               lyxerr[Debug::INSETS] << "OK" << endl;
+               lyxerr[Debug::INSETTEXT] << "OK" << endl;
                resetPos(bv);
        } 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;
 }
 
@@ -840,10 +847,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                case LFUN_SHIFT_TAB:
                case LFUN_TAB:
                        hideInsetCursor(bv);
-                       if (the_locking_inset) {
-                               unlockInsetInInset(bv, the_locking_inset);
-                               the_locking_inset = 0;
-                       }
+                       unlockInsetInInset(bv, the_locking_inset);
                        if (action == LFUN_TAB)
                                moveNextCell(bv, old_locking_inset != 0);
                        else
@@ -955,10 +959,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                break;
        case LFUN_NEXT: {
                int column = actcol;
-               if (the_locking_inset) {
-                       unlockInsetInInset(bv, the_locking_inset);
-                       the_locking_inset = 0;
-               }
+               unlockInsetInInset(bv, the_locking_inset);
                if (bv->text->first + bv->painter().paperHeight() <
                    (top_baseline + tabular->GetHeightOfTabular()))
                        {
@@ -974,10 +975,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
        }
        case LFUN_PRIOR: {
                int column = actcol;
-               if (the_locking_inset) {
-                       unlockInsetInInset(bv, the_locking_inset);
-                       the_locking_inset = 0;
-               }
+               unlockInsetInInset(bv, the_locking_inset);
                if (top_baseline < 0) {
                        bv->scrollCB(bv->text->first - bv->painter().paperHeight());
                        updateLocal(bv, FULL, false);
@@ -992,13 +990,25 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                updateLocal(bv, CURSOR, false);
                break;
        }
+       // none of these make sense for insettabular,
+       // but we must catch them to prevent any
+       // selection from being confused
+       case LFUN_PRIORSEL:
+       case LFUN_NEXTSEL:
+       case LFUN_WORDLEFT:
+       case LFUN_WORDLEFTSEL:
+       case LFUN_WORDRIGHT: 
+       case LFUN_WORDRIGHTSEL:
+       case LFUN_DOWN_PARAGRAPH:
+       case LFUN_DOWN_PARAGRAPHSEL:
+       case LFUN_UP_PARAGRAPH:
+       case LFUN_UP_PARAGRAPHSEL:
        case LFUN_BACKSPACE:
-               break;
        case LFUN_DELETE:
-               break;
        case LFUN_HOME:
-               break;
+       case LFUN_HOMESEL:
        case LFUN_END:
+       case LFUN_ENDSEL:
                break;
        case LFUN_LAYOUT_TABULAR:
        {
@@ -1037,7 +1047,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                        string::size_type len = clip.length();
                        string::size_type p = 0;
 
-                       while(p < len &&
+                       while (p < len &&
                              ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
                                switch(clip[p]) {
                                case '\t':
@@ -1059,7 +1069,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                        int cell = 0;
                        int cells = paste_tabular->GetNumberOfCells();
                        p = cols = 0;
-                       while((cell < cells) && (p < len) &&
+                       while ((cell < cells) && (p < len) &&
                              (p = clip.find_first_of("\t\n", p)) != string::npos) {
                                if (p >= len)
                                        break;
@@ -1071,7 +1081,7 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                                        break;
                                case '\n':
                                        paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
-                                       while(cols++ < maxCols)
+                                       while (cols++ < maxCols)
                                                ++cell;
                                        cols = 0;
                                        break;
@@ -1114,7 +1124,6 @@ InsetTabular::localDispatch(BufferView * bv, kb_action action,
                        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;
@@ -1151,15 +1160,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
 {
-       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;
 }
 
 
@@ -1211,6 +1238,8 @@ bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
                changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
                changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
        }
+       if (changed)
+               tabular->reinit();
        return changed;
 }
 
@@ -1292,7 +1321,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+1) < tabular->rows())) {
+       while ((ly < y) && ((actrow+1) < tabular->rows())) {
                cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
                                 tabular->GetAscentOfRow(actrow + 1) +
                                 tabular->GetAdditionalHeight(actrow + 1));
@@ -1331,6 +1360,12 @@ void InsetTabular::resetPos(BufferView * bv) const
 {
        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;
@@ -1394,6 +1429,7 @@ void InsetTabular::resetPos(BufferView * bv) const
                bv->owner()->getDialogs()->updateTabular(inset);
                oldcell = actcell;
        }
+       in_reset_pos = false;
 }
 
 
@@ -1644,10 +1680,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:
        {
@@ -1806,7 +1843,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;
@@ -1888,17 +1925,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:
        {
@@ -1970,7 +2015,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;
                }
        }
@@ -2090,6 +2135,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()) {
@@ -2244,25 +2290,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;
@@ -2313,7 +2359,7 @@ bool InsetTabular::copySelection(BufferView * bv)
        paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
        for (int i = 0; i < sel_row_start; ++i)
                paste_tabular->DeleteRow(0);
-       while(paste_tabular->rows() > rows)
+       while (paste_tabular->rows() > rows)
                paste_tabular->DeleteRow(rows);
        paste_tabular->SetTopLine(0, true, true);
        paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),