]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
updates to minipage inset
[lyx.git] / src / insets / insettabular.C
index 067071bbbe2348b6819981d027ee606910a8c809..e77fbe3bfeb07eb9bfaad0422326c3c8b4496898 100644 (file)
@@ -121,11 +121,11 @@ InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
     // just for test!!!
     the_locking_inset = 0;
     locked = no_selection = cursor_visible = false;
-    cursor.x_fix(-1);
     oldcell = -1;
     actrow = actcell = 0;
     clearSelection();
     need_update = INIT;
+    no_draw = false;
 }
 
 
@@ -135,11 +135,11 @@ InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
     tabular = new LyXTabular(this, *(tab.tabular));
     the_locking_inset = 0;
     locked = no_selection = cursor_visible = false;
-    cursor.x_fix(-1);
     oldcell = -1;
     actrow = actcell = 0;
     sel_cell_start = sel_cell_end = 0;
     need_update = INIT;
+    no_draw = false;
 }
 
 
@@ -214,8 +214,12 @@ int InsetTabular::width(BufferView *, LyXFont const &) const
 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                        float & x, bool cleared) const
 {
+    if (no_draw)
+       return;
+
     Painter & pain = bv->painter();
-    int i, j;
+    int i;
+    int j;
     int nx;
 
     UpdatableInset::draw(bv, font, baseline, x, cleared);
@@ -245,6 +249,7 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
     if (cleared) {
        int cell = 0;
        float cx;
+       first_visible_cell = -1;
        for (i = 0; i < tabular->rows(); ++i) {
            nx = int(x);
            dodraw = ((baseline + tabular->GetDescentOfRow(i)) > 0) &&
@@ -254,6 +259,8 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                    continue;
                cx = nx + tabular->GetBeginningOfTextInCell(cell);
                if (dodraw) {
+                   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,
@@ -311,33 +318,6 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
            lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
            return;
        }
-//     LyXText::text_status st = bv->text->status;
-#if 0
-       do {
-           float cx = nx + tabular->GetBeginningOfTextInCell(cell);
-           bv->text->status = st;
-           if (need_update == CELL) {
-               // clear before the inset
-               pain.fillRectangle(
-                   nx + 1,
-                   baseline - tabular->GetAscentOfRow(i) + 1,
-                   int(cx - nx - 1),
-                   tabular->GetAscentOfRow(i) +
-                   tabular->GetDescentOfRow(i) - 1);
-               // clear behind the inset
-               pain.fillRectangle(
-                   int(cx + the_locking_inset->width(bv,font) + 1),
-                   baseline - tabular->GetAscentOfRow(i) + 1,
-                   tabular->GetWidthOfColumn(cell) -
-                   tabular->GetBeginningOfTextInCell(cell) -
-                   the_locking_inset->width(bv,font) -
-                   tabular->GetAdditionalWidth(cell) - 1,
-                   tabular->GetAscentOfRow(i) +
-                   tabular->GetDescentOfRow(i) - 1);
-           }
-           tabular->GetCellInset(cell)->draw(bv,font,baseline, cx, false);
-       } while(bv->text->status == LyXText::CHANGED_IN_DRAW);
-#else
        float dx;
        float cx;
        cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
@@ -364,7 +344,6 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
                tabular->GetAscentOfRow(i) +
                tabular->GetDescentOfRow(i) - 1);
        }
-#endif
     }
     x -= ADD_TO_TABULAR_WIDTH;
     x += width(bv, font);
@@ -378,7 +357,7 @@ void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
                                  int row, int cell) const
 {
-    int  x2 = x + tabular->GetWidthOfColumn(cell);
+    int x2 = x + tabular->GetWidthOfColumn(cell);
     bool on_off;
 
     if (!tabular->TopAlreadyDrawed(cell)) {
@@ -506,10 +485,9 @@ void InsetTabular::InsetUnlock(BufferView * bv)
     oldcell = -1;
     locked = false;
     if (scroll() || hasSelection()) {
+       sel_cell_start = sel_cell_end = 0;
        if (scroll()) {
            scroll(bv, 0.0F);
-       } else {
-           sel_cell_start = sel_cell_end = 0;
        }
        UpdateLocal(bv, FULL, false);
     }
@@ -567,8 +545,8 @@ bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
     if (the_locking_inset == inset) {
         the_locking_inset->InsetUnlock(bv);
         the_locking_inset = 0;
-       ShowInsetCursor(bv, false);
        UpdateLocal(bv, CELL, false);
+       ShowInsetCursor(bv, false);
         return true;
     }
     if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
@@ -736,7 +714,8 @@ void InsetTabular::InsetKeyPress(XKeyEvent * xke)
 }
 
 
-UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
+UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv,
+                                                  kb_action action,
                                                   string const & arg)
 {
     // We need to save the value of the_locking_inset as the call to 
@@ -753,14 +732,18 @@ UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
     if ((action < 0) && arg.empty())
         return FINISHED;
 
-    if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
-        (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
-        cursor.x_fix(-1);
     if (the_locking_inset) {
         result=the_locking_inset->LocalDispatch(bv, action, arg);
-       if (result == DISPATCHED_NOUPDATE)
+       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);
+           }
            return result;
-       else if (result == DISPATCHED) {
+       else if (result == DISPATCHED) {
            the_locking_inset->ToggleInsetCursor(bv);
            UpdateLocal(bv, CELL, false);
            the_locking_inset->ToggleInsetCursor(bv);
@@ -836,6 +819,45 @@ UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
        if (hs)
            UpdateLocal(bv, SELECTION, false);
        break;
+    case LFUN_NEXT: {
+       int column = actcol;
+       if (the_locking_inset) {
+           UnlockInsetInInset(bv, the_locking_inset);
+           the_locking_inset = 0;
+       }
+       if (bv->text->first + bv->painter().paperHeight() <
+           (top_baseline + tabular->GetHeightOfTabular()))
+       {
+           bv->scrollCB(bv->text->first + bv->painter().paperHeight());
+           UpdateLocal(bv, FULL, false);
+           actcell = tabular->GetCellBelow(first_visible_cell) + column;
+       } else {
+           actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
+       }
+       resetPos(bv);
+       UpdateLocal(bv, CURSOR, false);
+       break;
+    }
+    case LFUN_PRIOR: {
+       int column = actcol;
+       if (the_locking_inset) {
+           UnlockInsetInInset(bv, the_locking_inset);
+           the_locking_inset = 0;
+       }
+       if (top_baseline < 0) {
+           bv->scrollCB(bv->text->first - bv->painter().paperHeight());
+           UpdateLocal(bv, FULL, false);
+           if (top_baseline > 0)
+               actcell = column;
+           else
+               actcell = tabular->GetCellBelow(first_visible_cell) + column;
+       } else {
+           actcell = column;
+       }
+       resetPos(bv);
+       UpdateLocal(bv, CURSOR, false);
+       break;
+    }
     case LFUN_BACKSPACE:
        break;
     case LFUN_DELETE:
@@ -874,11 +896,11 @@ UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
            break;
        bv->text->SetUndo(bv->buffer(), Undo::DELETE,
 #ifndef NEW_INSETS
-         bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
-         bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
+         bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
+                         bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
 #else
-         bv->text->cursor.par()->previous,
-         bv->text->cursor.par()->next
+         bv->text->cursor.par()->previous(),
+         bv->text->cursor.par()->next()
 #endif
                );
        cutSelection();
@@ -961,11 +983,11 @@ UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
        if (hasPasteBuffer()) {
            bv->text->SetUndo(bv->buffer(), Undo::INSERT,
 #ifndef NEW_INSETS
-             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
-             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
+             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
+                             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
 #else
-             bv->text->cursor.par()->previous,
-             bv->text->cursor.par()->next
+             bv->text->cursor.par()->previous(),
+             bv->text->cursor.par()->next()
 #endif
                );
            pasteSelection(bv);
@@ -979,18 +1001,20 @@ UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
        result = UNDISPATCHED;
        if (the_locking_inset)
            break;
+       no_draw = true;
        if (ActivateCellInset(bv)) {
            result=the_locking_inset->LocalDispatch(bv, action, arg);
-           if (result == DISPATCHED_NOUPDATE)
-               return result;
-           else if (result == DISPATCHED) {
-               the_locking_inset->ToggleInsetCursor(bv);
-               UpdateLocal(bv, CELL, false);
-               the_locking_inset->ToggleInsetCursor(bv);
-               return result;
-           } else {
-               result = UNDISPATCHED;
+           if ((result == UNDISPATCHED) || (result == FINISHED)) {
+               UnlockInsetInInset(bv, the_locking_inset);
+               no_draw = false;
+               the_locking_inset = 0;
+               return UNDISPATCHED;
            }
+           no_draw = false;
+           the_locking_inset->ToggleInsetCursor(bv);
+           UpdateLocal(bv, CELL, false);
+           the_locking_inset->ToggleInsetCursor(bv);
+           return result;
        }
        break;
     }
@@ -1212,9 +1236,24 @@ void InsetTabular::resetPos(BufferView * bv) const
     new_x += offset;
     cursor.x(new_x);
 //    cursor.x(getCellXPos(actcell) + offset);
-    if (((cursor.x() - offset) > 20) &&
-       ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
-        (bv->workWidth()-20)))
+    if (scroll() && (tabular->GetWidthOfTabular() < bv->workWidth()-20))
+       scroll(bv, 0.0F);
+    else if (the_locking_inset &&
+            (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
+    {
+           int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
+           if (xx > (bv->workWidth()-20))
+               scroll(bv, -(xx - bv->workWidth() + 60));
+           else if (xx < 20) {
+               if (xx < 0)
+                   xx = -xx + 60;
+               else
+                   xx = 60;
+               scroll(bv, xx);
+           }
+    } 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);
@@ -1228,9 +1267,9 @@ void InsetTabular::resetPos(BufferView * bv) const
     if ((!the_locking_inset ||
         !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
        (actcell != oldcell)) {
-           InsetTabular * inset = const_cast<InsetTabular *>(this);
-           bv->owner()->getDialogs()->updateTabular(inset);
-           oldcell = actcell;
+       InsetTabular * inset = const_cast<InsetTabular *>(this);
+       bv->owner()->getDialogs()->updateTabular(inset);
+       oldcell = actcell;
     }
 }
 
@@ -1388,7 +1427,7 @@ bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
        string const tmp = tabularFeatures[i].feature;
            
        if (tmp == what.substr(0, tmp.length())) {
-       //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
+       //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
              //tabularFeatures[i].feature.length())) {
            action = tabularFeatures[i].action;
            break;
@@ -1472,11 +1511,11 @@ void InsetTabular::TabularFeatures(BufferView * bv,
     }
     bv->text->SetUndo(bv->buffer(), Undo::FINISH,
 #ifndef NEW_INSETS
-             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
-             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
+             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
+                     bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
 #else
-             bv->text->cursor.par()->previous,
-             bv->text->cursor.par()->next
+             bv->text->cursor.par()->previous(),
+             bv->text->cursor.par()->next()
 #endif
            );
 
@@ -1599,10 +1638,7 @@ void InsetTabular::TabularFeatures(BufferView * bv,
            for (j = sel_col_start; j <= sel_col_end; ++j)
                tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
                                      flag);
-       if (hasSelection())
-           UpdateLocal(bv, INIT, true);
-       else
-           UpdateLocal(bv, CELL, true);
+       UpdateLocal(bv, INIT, true);
        break;
     case LyXTabular::M_VALIGN_TOP:
     case LyXTabular::M_VALIGN_BOTTOM:
@@ -1615,10 +1651,7 @@ void InsetTabular::TabularFeatures(BufferView * bv,
            for (j = sel_col_start; j <= sel_col_end; ++j)
                tabular->SetVAlignment(tabular->GetCellNumber(i, j),
                                       setVAlign, flag);
-       if (hasSelection())
-           UpdateLocal(bv, INIT, true);
-       else
-           UpdateLocal(bv, CELL, true);
+       UpdateLocal(bv, INIT, true);
        break;
     case LyXTabular::MULTICOLUMN:
     {
@@ -1795,11 +1828,11 @@ void InsetTabular::resizeLyXText(BufferView *) const
 }
 
 
-LyXText * InsetTabular::getLyXText(BufferView const * bv) const
+LyXText * InsetTabular::getLyXText(BufferView const * bv, bool const recursive) const
 {
     if (the_locking_inset)
-       return the_locking_inset->getLyXText(bv);
-    return Inset::getLyXText(bv);
+       return the_locking_inset->getLyXText(bv, recursive);
+    return Inset::getLyXText(bv, recursive);
 }
 
 
@@ -1832,7 +1865,7 @@ LyXFunc::func_status InsetTabular::getStatus(string const & what) const
     for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
        string const tmp = tabularFeatures[i].feature;
        if (tmp == what.substr(0, tmp.length())) {                  
-       //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
+       //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
        //   tabularFeatures[i].feature.length())) {
            action = tabularFeatures[i].action;
            break;
@@ -1843,7 +1876,8 @@ LyXFunc::func_status InsetTabular::getStatus(string const & what) const
 
     string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
 
-    int sel_row_start, sel_row_end;
+    int sel_row_start;
+    int sel_row_end;
     int dummy;
     bool flag = true;
 
@@ -2049,8 +2083,10 @@ bool InsetTabular::copySelection(BufferView * bv)
        return false;
     delete paste_tabular;
 
-    int sel_col_start, sel_col_end;
-    int sel_row_start, sel_row_end;
+    int sel_col_start;
+    int sel_col_end;
+    int sel_row_start;
+    int sel_row_end;
 
     sel_col_start = tabular->column_of_cell(sel_cell_start);
     sel_col_end = tabular->column_of_cell(sel_cell_end);
@@ -2135,8 +2171,10 @@ bool InsetTabular::cutSelection()
     if (!hasSelection())
        return false;
 
-    int sel_col_start, sel_col_end;
-    int sel_row_start, sel_row_end;
+    int sel_col_start;
+    int sel_col_end;
+    int sel_row_start;
+    int sel_row_end;
 
     sel_col_start = tabular->column_of_cell(sel_cell_start);
     sel_col_end = tabular->column_of_cell(sel_cell_end);