]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettabular.C
Implement copying of rows and columns in tables
[lyx.git] / src / insets / insettabular.C
index 35912ba4e3454a42b180f0a3bf01bb534cbcc20f..0774f66c3fd38245f1c2ff6437992557ffc5e80d 100644 (file)
@@ -37,9 +37,8 @@
 #include "support/convert.h"
 
 #include "frontends/Alert.h"
+#include "frontends/Application.h"
 #include "frontends/font_metrics.h"
-#include "frontends/Gui.h"
-#include "frontends/LyXView.h"
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
 #include "frontends/Selection.h"
@@ -59,7 +58,6 @@ using lyx::graphics::PreviewLoader;
 using lyx::support::ltrim;
 
 using lyx::frontend::Painter;
-using lyx::frontend::Gui;
 using lyx::frontend::Clipboard;
 
 using boost::shared_ptr;
@@ -97,6 +95,8 @@ TabularFeature tabularFeature[] =
        { LyXTabular::APPEND_COLUMN, "append-column" },
        { LyXTabular::DELETE_ROW, "delete-row" },
        { LyXTabular::DELETE_COLUMN, "delete-column" },
+       { LyXTabular::COPY_ROW, "copy-row" },
+       { LyXTabular::COPY_COLUMN, "copy-column" },
        { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
        { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
        { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
@@ -279,7 +279,7 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                        tabular.setWidthOfCell(cell, dim.wid);
                        if (p_width.zero()) {
                                m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
-                               // FIXME there must be a way to get rid of 
+                               // FIXME there must be a way to get rid of
                                // the second metrics call
                                tabular.getCellInset(cell)->metrics(m, dim);
                        }
@@ -449,7 +449,7 @@ void InsetTabular::drawCellLines(Painter & pain, int x, int y,
 }
 
 
-string const InsetTabular::editMessage() const
+docstring const InsetTabular::editMessage() const
 {
        return _("Opened table");
 }
@@ -494,7 +494,7 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_MOUSE_PRESS:
                //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
 
-               if (cmd.button() == mouse_button::button1 
+               if (cmd.button() == mouse_button::button1
                    || cmd.button() == mouse_button::button3) {
                        cur.selection() = false;
                        setCursorFromCoordinates(cur, cmd.x, cmd.y);
@@ -700,8 +700,8 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_CLIPBOARD_PASTE:
        case LFUN_PRIMARY_SELECTION_PASTE: {
                docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
-                       cur.bv().owner()->gui().clipboard().get() :
-                       cur.bv().owner()->gui().selection().get();
+                       theApp->clipboard().get() :
+                       theApp->selection().get();
                if (clip.empty())
                        break;
                // pass to InsertAsciiString, but
@@ -731,9 +731,6 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
                        break;
                }
                cell(cur.idx())->dispatch(cur, cmd);
-               // Reset pasted paragraphs:
-               if (tabular.getPWidth(cur.idx()).zero())
-                       cell(cur.idx())->forceParagraphsToDefault(cur);
                break;
 
        case LFUN_FONT_EMPH:
@@ -757,27 +754,22 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
                        row_type rs, re;
                        col_type cs, ce;
                        getSelection(cur, rs, re, cs, ce);
+                       LCursor tmpcur = cur;
                        for (row_type i = rs; i <= re; ++i) {
                                for (col_type j = cs; j <= ce; ++j) {
                                        // cursor follows cell:
-                                       cur.idx() = tabular.getCellNumber(i, j);
+                                       tmpcur.idx() = tabular.getCellNumber(i, j);
                                        // select this cell only:
-                                       cur.pos() = 0;
-                                       cur.resetAnchor();
-                                       cur.pos() = cur.top().lastpos();
-                                       cur.setCursor(cur);
-                                       cur.setSelection();
-                                       cell(cur.idx())->dispatch(cur, cmd);
+                                       tmpcur.pit() = 0;
+                                       tmpcur.pos() = 0;
+                                       tmpcur.resetAnchor();
+                                       tmpcur.pit() = tmpcur.lastpit();
+                                       tmpcur.pos() = tmpcur.top().lastpos();
+                                       tmpcur.setCursor(tmpcur);
+                                       tmpcur.setSelection();
+                                       cell(tmpcur.idx())->dispatch(tmpcur, cmd);
                                }
                        }
-                       // Restore original selection
-                       cur.idx() = tabular.getCellNumber(rs, cs);
-                       cur.pos() = 0;
-                       cur.resetAnchor();
-                       cur.idx() = tabular.getCellNumber(re, ce);
-                       cur.pos() = cur.top().lastpos();
-                       cur.setCursor(cur);
-                       cur.setSelection();
                        break;
                } else {
                        cell(cur.idx())->dispatch(cur, cmd);
@@ -836,6 +828,8 @@ bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
                case LyXTabular::APPEND_COLUMN:
                case LyXTabular::DELETE_ROW:
                case LyXTabular::DELETE_COLUMN:
+               case LyXTabular::COPY_ROW:
+               case LyXTabular::COPY_COLUMN:
                case LyXTabular::SET_ALL_LINES:
                case LyXTabular::UNSET_ALL_LINES:
                case LyXTabular::SET_TOP_SPACE:
@@ -979,11 +973,11 @@ bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
                case LyXTabular::SET_LTNEWPAGE:
                        status.setOnOff(tabular.getLTNewPage(sel_row_start));
                        break;
-                       
+
                case LyXTabular::SET_BOOKTABS:
                        status.setOnOff(tabular.useBookTabs());
                        break;
-                       
+
                case LyXTabular::UNSET_BOOKTABS:
                        status.setOnOff(!tabular.useBookTabs());
                        break;
@@ -1045,28 +1039,6 @@ bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
                        return true;
                }
 
-       case LFUN_INSET_DISSOLVE: {
-               status.enabled(false);
-               return true;
-       }
-
-       // because of the dissolve handling in insettext:
-       case LFUN_CHAR_DELETE_FORWARD:
-               if (!cur.selection() && cur.depth() > 1
-                   && cur.pit() == cur.lastpit()
-                   && cur.pos() == cur.lastpos()) {
-               status.enabled(false);
-               return true;
-               }
-               // Fall through
-
-       case LFUN_CHAR_DELETE_BACKWARD:
-               if (cur.depth() > 1 && cur.pit() == 0 && cur.pos() == 0) {
-               status.enabled(false);
-               return true;
-               }
-               // Fall through
-
        case LFUN_INSET_MODIFY:
                if (translate(cmd.getArg(0)) == TABULAR_CODE) {
                        status.enabled(true);
@@ -1185,7 +1157,7 @@ int InsetTabular::dist(idx_type const cell, int x, int y) const
        int const xend = xbeg + tabular.getWidthOfColumn(cell);
        row_type const row = tabular.row_of_cell(cell);
        int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
-                        tabular.getAdditionalHeight(row);
+                        tabular.getAdditionalHeight(row);
        int const yend = o.y_ + tabular.getDescentOfRow(row);
 
        if (x < xbeg)
@@ -1493,6 +1465,15 @@ void InsetTabular::tabularFeatures(LCursor & cur,
                cur.selection() = false;
                break;
 
+       case LyXTabular::COPY_ROW:
+               tabular.copyRow(bv.buffer()->params(), row);
+               break;
+
+       case LyXTabular::COPY_COLUMN:
+               tabular.copyColumn(bv.buffer()->params(), column);
+               cur.idx() = tabular.getCellNumber(row, column);
+               break;
+
        case LyXTabular::M_TOGGLE_LINE_TOP:
                flag = false;
        case LyXTabular::TOGGLE_LINE_TOP: {
@@ -1579,8 +1560,9 @@ void InsetTabular::tabularFeatures(LCursor & cur,
 #ifdef WITH_WARNINGS
 #warning Need I say it ? This is horrible.
 #endif
+                       // FIXME UNICODE
                        Alert::error(_("Error setting multicolumn"),
-                                  _("You cannot set multicolumn vertically."));
+                                    _("You cannot set multicolumn vertically."));
                        return;
                }
                if (!cur.selection()) {
@@ -1809,7 +1791,7 @@ bool InsetTabular::copySelection(LCursor & cur)
        ostringstream os;
        OutputParams const runparams;
        paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
-       cur.bv().owner()->gui().clipboard().put(lyx::from_utf8(os.str()));
+       theApp->clipboard().put(lyx::from_utf8(os.str()));
        // mark tabular stack dirty
        // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
        // when we (hopefully) have a one-for-all paste mechanism.
@@ -1873,7 +1855,10 @@ void InsetTabular::cutSelection(LCursor & cur)
        }
 
        // cursor position might be invalid now
-       cur.pos() = cur.lastpos();
+       if (cur.pit() > cur.lastpit())
+               cur.pit() = cur.lastpit();
+       if (cur.pos() > cur.lastpos())
+               cur.pos() = cur.lastpos();
        cur.clearSelection();
 }