3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
35 #include "frontends/Alert.h"
36 #include "frontends/font_metrics.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
40 #include "support/std_sstream.h"
44 using lyx::graphics::PreviewLoader;
46 using lyx::support::ltrim;
47 using lyx::support::strToInt;
48 using lyx::support::strToDbl;
54 using std::istringstream;
56 using std::ostringstream;
63 int const ADD_TO_HEIGHT = 2;
64 int const ADD_TO_TABULAR_WIDTH = 2;
67 boost::scoped_ptr<LyXTabular> paste_tabular;
70 struct TabularFeature {
71 LyXTabular::Feature action;
76 TabularFeature tabularFeature[] =
78 { LyXTabular::APPEND_ROW, "append-row" },
79 { LyXTabular::APPEND_COLUMN, "append-column" },
80 { LyXTabular::DELETE_ROW, "delete-row" },
81 { LyXTabular::DELETE_COLUMN, "delete-column" },
82 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
83 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
84 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
85 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
86 { LyXTabular::ALIGN_LEFT, "align-left" },
87 { LyXTabular::ALIGN_RIGHT, "align-right" },
88 { LyXTabular::ALIGN_CENTER, "align-center" },
89 { LyXTabular::ALIGN_BLOCK, "align-block" },
90 { LyXTabular::VALIGN_TOP, "valign-top" },
91 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
92 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
93 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
94 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
95 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
96 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
97 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
98 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
99 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
100 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
101 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
102 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
103 { LyXTabular::MULTICOLUMN, "multicolumn" },
104 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
105 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
106 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
107 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
108 { LyXTabular::SET_PWIDTH, "set-pwidth" },
109 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
110 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
111 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
112 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
113 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
114 { LyXTabular::SET_USEBOX, "set-usebox" },
115 { LyXTabular::SET_LTHEAD, "set-lthead" },
116 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
117 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
118 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
119 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
120 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
121 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
122 { LyXTabular::LAST_ACTION, "" }
126 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
128 FeatureEqual(LyXTabular::Feature feature)
129 : feature_(feature) {}
130 bool operator()(TabularFeature const & tf) const {
131 return tf.action == feature_;
134 LyXTabular::Feature feature_;
140 string const featureAsString(LyXTabular::Feature feature)
142 TabularFeature * end = tabularFeature +
143 sizeof(tabularFeature) / sizeof(TabularFeature);
144 TabularFeature * it = std::find_if(tabularFeature, end,
145 FeatureEqual(feature));
146 return (it == end) ? string() : it->feature;
150 bool InsetTabular::hasPasteBuffer() const
152 return (paste_tabular.get() != 0);
156 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
157 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
158 buffer_(&buf), cursorx_(0), cursory_(0)
160 tabular.setOwner(this);
165 InsetTabular::InsetTabular(InsetTabular const & tab)
166 : UpdatableInset(tab), tabular(tab.tabular),
167 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
169 tabular.setOwner(this);
174 InsetTabular::~InsetTabular()
176 InsetTabularMailer(*this).hideDialog();
180 auto_ptr<InsetBase> InsetTabular::clone() const
182 return auto_ptr<InsetBase>(new InsetTabular(*this));
186 Buffer const & InsetTabular::buffer() const
192 void InsetTabular::buffer(Buffer * b)
198 void InsetTabular::write(Buffer const & buf, ostream & os) const
200 os << "Tabular" << endl;
201 tabular.write(buf, os);
205 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
207 bool const old_format = (lex.getString() == "\\LyXTable");
209 tabular.read(buf, lex);
215 string token = lex.getString();
216 while (lex.isOK() && (token != "\\end_inset")) {
218 token = lex.getString();
220 if (token != "\\end_inset") {
221 lex.printError("Missing \\end_inset at this point. "
227 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
229 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
230 // mi.base.textwidth << "\n";
232 lyxerr << "InsetTabular::metrics: need bv" << endl;
236 calculate_dimensions_of_cells(mi);
238 dim.asc = tabular.getAscentOfRow(0);
239 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
240 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
245 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
247 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
249 BufferView * bv = pi.base.bv;
250 setPosCache(pi, x, y);
255 x += ADD_TO_TABULAR_WIDTH;
258 first_visible_cell = -1;
259 for (int i = 0; i < tabular.rows(); ++i) {
261 cell = tabular.getCellNumber(i, 0);
262 if (y + tabular.getDescentOfRow(i) <= 0 &&
263 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
265 y += tabular.getDescentOfRow(i) +
266 tabular.getAscentOfRow(i + 1) +
267 tabular.getAdditionalHeight(i + 1);
270 for (int j = 0; j < tabular.columns(); ++j) {
271 if (nx > bv->workWidth())
273 if (tabular.isPartOfMultiColumn(i, j))
275 if (first_visible_cell < 0)
276 first_visible_cell = cell;
277 if (bv->cursor().selection())
278 drawCellSelection(pi, nx, y, i, j, cell);
280 int const cx = nx + tabular.getBeginningOfTextInCell(cell);
281 tabular.getCellInset(cell).draw(pi, cx, y);
282 drawCellLines(pi.pain, nx, y, i, cell);
283 nx += tabular.getWidthOfColumn(cell);
287 // Would be nice, but for some completely unfathomable reason,
288 // on a col resize to a new fixed width, even though the insettexts
289 // are resized, the cell isn't, but drawing all cells in a tall table
290 // has the desired effect somehow. Complete dark magic.
292 // avoiding drawing the rest of a long table is
293 // a pretty big speedup
294 if (y > bv->workHeight())
298 y += tabular.getDescentOfRow(i) +
299 tabular.getAscentOfRow(i + 1) +
300 tabular.getAdditionalHeight(i + 1);
305 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
306 int row, int cell) const
308 int x2 = x + tabular.getWidthOfColumn(cell);
311 if (!tabular.topAlreadyDrawn(cell)) {
312 on_off = !tabular.topLine(cell);
313 pain.line(x, y - tabular.getAscentOfRow(row),
314 x2, y - tabular.getAscentOfRow(row),
315 on_off ? LColor::tabularonoffline : LColor::tabularline,
316 on_off ? Painter::line_onoffdash : Painter::line_solid);
318 on_off = !tabular.bottomLine(cell);
319 pain.line(x, y + tabular.getDescentOfRow(row),
320 x2, y + tabular.getDescentOfRow(row),
321 on_off ? LColor::tabularonoffline : LColor::tabularline,
322 on_off ? Painter::line_onoffdash : Painter::line_solid);
323 if (!tabular.leftAlreadyDrawn(cell)) {
324 on_off = !tabular.leftLine(cell);
325 pain.line(x, y - tabular.getAscentOfRow(row),
326 x, y + tabular.getDescentOfRow(row),
327 on_off ? LColor::tabularonoffline : LColor::tabularline,
328 on_off ? Painter::line_onoffdash : Painter::line_solid);
330 on_off = !tabular.rightLine(cell);
331 pain.line(x2 - tabular.getAdditionalWidth(cell),
332 y - tabular.getAscentOfRow(row),
333 x2 - tabular.getAdditionalWidth(cell),
334 y + tabular.getDescentOfRow(row),
335 on_off ? LColor::tabularonoffline : LColor::tabularline,
336 on_off ? Painter::line_onoffdash : Painter::line_solid);
340 void InsetTabular::drawCellSelection(PainterInfo & pi, int x, int y,
341 int row, int column, int cell) const
343 LCursor & cur = pi.base.bv->cursor();
344 BOOST_ASSERT(cur.selection());
345 if (tablemode(cur)) {
347 getSelection(cur, rs, re, cs, ce);
348 if (column >= cs && column <= ce && row >= rs && row <= re) {
349 int w = tabular.getWidthOfColumn(cell);
350 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
351 pi.pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
352 w, h, LColor::selection);
358 string const InsetTabular::editMessage() const
360 return _("Opened table");
364 void InsetTabular::updateLocal(LCursor & cur) const
371 void InsetTabular::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
373 if (cmd.button() == mouse_button::button3)
376 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
377 cur.selection() = false;
379 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
381 //cur.cursor_ = theTempCursor;
385 setPos(cur, cmd.x, cmd.y);
386 //cur.cursor_ = theTempCursor;
390 lyxerr << cur << endl;
392 if (cmd.button() == mouse_button::button2)
393 dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
397 void InsetTabular::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
399 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
400 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
401 if (cmd.button() == mouse_button::button3)
402 InsetTabularMailer(*this).showDialog(&cur.bv());
406 void InsetTabular::edit(LCursor & cur, bool left)
408 lyxerr << "InsetTabular::edit: " << this << endl;
412 if (isRightToLeft(cur))
413 cell = tabular.getLastCellInRow(0);
417 if (isRightToLeft(cur))
418 cell = tabular.getFirstCellInRow(tabular.rows()-1);
420 cell = tabular.getNumberOfCells() - 1;
422 cur.selection() = false;
424 cur.bv().fitCursor();
430 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
432 lyxerr << "InsetTabular::edit: " << this << endl;
433 cur.selection() = false;
435 return setPos(cur, x, y);
436 //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
438 // activateCellInset(bv, cell, x - xx, y - cursory_);
442 void InsetTabular::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
444 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
446 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
448 setPos(cur, cmd.x, cmd.y);
450 if (!hasSelection()) {
451 setSelection(actcell, actcell);
454 setSelection(sel_cell_start, actcell);
455 tablemode = (sel_cell_start != actcell);
462 InsetTabular::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
464 lyxerr << "# InsetTabular::dispatch: " << cmd << endl;
466 InsetText & cell = tabular.getCellInset(cur.idx());
468 DispatchResult result(true, true);
469 switch (cmd.action) {
471 case LFUN_MOUSE_PRESS:
472 if (cmd.button() == mouse_button::button3)
473 DispatchResult(true, true);
475 case LFUN_MOUSE_MOTION:
476 lfunMouseMotion(cur, cmd);
477 return DispatchResult(true, true);
479 case LFUN_MOUSE_RELEASE:
480 lfunMouseRelease(cur, cmd);
481 return DispatchResult(true, true);
484 int const cell = cur.idx();
485 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
486 result = cell.dispatch(cur, cmd);
488 switch (result.val()) {
490 if (movePrevCell(cur))
491 result = DispatchResult(true, true);
493 result = DispatchResult(false, FINISHED);
497 if (moveNextCell(cur))
498 result = DispatchResult(true, true);
500 result = DispatchResult(false, FINISHED_RIGHT);
505 result = DispatchResult(true, true);
507 result = DispatchResult(false, FINISHED_UP);
511 if (moveDownLock(cur))
512 result = DispatchResult(true, true);
514 result = DispatchResult(false, FINISHED_UP);
518 case LFUN_CELL_BACKWARD:
519 case LFUN_CELL_FORWARD:
520 if (cmd.action == LFUN_CELL_FORWARD)
524 cur.selection() = false;
527 case LFUN_SCROLL_INSET:
528 if (!cmd.argument.empty()) {
529 if (cmd.argument.find('.') != cmd.argument.npos)
530 scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
532 scroll(cur.bv(), strToInt(cmd.argument));
534 return DispatchResult(true, true);
539 CursorSlice sl = cur.current();
540 cell.dispatch(cur, cmd);
541 if (sl == cur.current())
542 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
543 if (sl == cur.current())
544 result = DispatchResult(true, true);
550 CursorSlice sl = cur.current();
551 cell.dispatch(cur, cmd);
552 if (sl == cur.current())
553 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
554 if (sl == cur.current())
555 result = DispatchResult(true, true);
561 int const start = hasSelection() ? sel_cell_start : cur.idx();
562 int const ocell = cur.idx();
563 // if we are starting a selection, only select
564 // the current cell at the beginning
565 if (hasSelection()) {
567 if (ocell == sel_cell_end ||
568 tabular.column_of_cell(ocell) >
569 tabular.column_of_cell(cur.idx()))
570 //setSelection(start, tabular.getCellBelow(sel_cell_end));
572 //setSelection(start, tabular.getLastCellBelow(sel_cell_end));
574 //setSelection(start, start);
582 result = DispatchResult(false, FINISHED_DOWN);
583 cur.selection() = false;
588 int const start = hasSelection() ? sel_cell_start : cur.idx();
589 int const ocell = cur.idx();
590 // if we are starting a selection, only select
591 // the current cell at the beginning
592 if (hasSelection()) {
594 if (ocell == sel_cell_end ||
595 tabular.column_of_cell(ocell) >
596 tabular.column_of_cell(cur.idx()))
597 //setSelection(start, tabular.getCellAbove(sel_cell_end));
599 //setSelection(start, tabular.getLastCellAbove(sel_cell_end));
601 //setSelection(start, start);
609 result = DispatchResult(false, FINISHED_DOWN);
610 cur.selection() = false;
614 //if (hasSelection())
615 // cur.selection() = false;
616 int actcell = cur.idx();
617 int actcol = tabular.column_of_cell(actcell);
619 if (cur.bv().top_y() + cur.bv().painter().paperHeight()
620 < yo_ + tabular.getHeightOfTabular())
622 cur.bv().scrollDocView(
623 cur.bv().top_y() + cur.bv().painter().paperHeight());
624 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
626 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
633 //if (hasSelection())
634 // cur.selection() = false;
635 int column = tabular.column_of_cell(cur.idx());
637 cur.bv().scrollDocView(
638 cur.bv().top_y() - cur.bv().painter().paperHeight());
642 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
650 case LFUN_LAYOUT_TABULAR:
651 InsetTabularMailer(*this).showDialog(&cur.bv());
654 case LFUN_INSET_DIALOG_UPDATE:
655 InsetTabularMailer(*this).updateDialog(&cur.bv());
658 case LFUN_TABULAR_FEATURE:
659 if (!tabularFeatures(cur, cmd.argument))
660 result = DispatchResult(false);
663 // insert file functions
664 case LFUN_FILE_INSERT_ASCII_PARA:
665 case LFUN_FILE_INSERT_ASCII: {
666 string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
667 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
668 result = DispatchResult(false);
682 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
686 if (copySelection(cur)) {
687 recordUndo(cur, Undo::DELETE);
694 recordUndo(cur, Undo::DELETE);
698 cell.dispatch(cur, cmd);
702 if (!cur.selection())
708 case LFUN_PASTESELECTION: {
709 string const clip = cur.bv().getClipboard();
712 if (clip.find('\t') != string::npos) {
716 string::size_type len = clip.length();
717 string::size_type p = 0;
720 (p = clip.find_first_of("\t\n", p)) != string::npos) {
728 maxCols = max(cols, maxCols);
734 maxCols = max(cols, maxCols);
737 new LyXTabular(cur.bv().buffer()->params(), rows, maxCols));
739 string::size_type op = 0;
741 int cells = paste_tabular->getNumberOfCells();
745 while (cell < cells && p < len &&
746 (p = clip.find_first_of("\t\n", p)) != string::npos) {
751 paste_tabular->getCellInset(cell).
752 setText(clip.substr(op, p-op), font);
757 paste_tabular->getCellInset(cell).
758 setText(clip.substr(op, p-op), font);
759 while (cols++ < maxCols)
767 // check for the last cell if there is no trailing '\n'
768 if (cell < cells && op < len)
769 paste_tabular->getCellInset(cell).
770 setText(clip.substr(op, len-op), font);
771 } else if (!insertAsciiString(cur.bv(), clip, true))
773 // so that the clipboard is used and it goes on
775 // and executes LFUN_PASTESELECTION in insettext!
776 paste_tabular.reset();
782 if (hasPasteBuffer()) {
783 recordUndo(cur, Undo::INSERT);
789 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
792 // handle font changing stuff on selection before we lock the inset
793 // in the default part!
794 result = cell.dispatch(cur, cmd);
795 // we try to activate the actual inset and put this event down to
796 // the insets dispatch function.
801 InsetTabularMailer(*this).updateDialog(&cur.bv());
807 int InsetTabular::latex(Buffer const & buf, ostream & os,
808 OutputParams const & runparams) const
810 return tabular.latex(buf, os, runparams);
814 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
815 OutputParams const & runparams) const
817 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
818 return tabular.plaintext(buf, os, runparams, dp, false, 0);
822 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
823 OutputParams const & runparams) const
825 return tabular.linuxdoc(buf,os, runparams);
829 int InsetTabular::docbook(Buffer const & buf, ostream & os,
830 OutputParams const & runparams) const
835 // if the table is inside a float it doesn't need the informaltable
836 // wrapper. Search for it.
837 for (master = owner(); master; master = master->owner())
838 if (master->lyxCode() == InsetOld::FLOAT_CODE)
842 os << "<informaltable>";
843 if (runparams.mixed_content)
847 ret += tabular.docbook(buf, os, runparams);
849 os << "</informaltable>";
850 if (runparams.mixed_content)
858 void InsetTabular::validate(LaTeXFeatures & features) const
860 tabular.validate(features);
864 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
866 for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
869 for (int j = 0; j < tabular.columns(); ++j) {
870 if (tabular.isPartOfMultiColumn(i, j))
876 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
877 tabular.getCellInset(cell).metrics(m, dim);
878 maxAsc = max(maxAsc, dim.asc);
879 maxDesc = max(maxDesc, dim.des);
880 tabular.setWidthOfCell(cell, dim.wid);
882 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
883 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
888 void InsetTabular::getCursorPos(CursorSlice const & cur, int & x, int & y) const
890 tabular.getCellInset(cur.idx()).getCursorPos(cur, x, y);
894 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
896 cur.idx() = getCell(x, y);
897 InsetBase * inset = tabular.getCellInset(cur.idx()).text_.editXY(cur, x, y);
898 lyxerr << "# InsetTabular::setPos() cursor: " << cur << endl;
903 int InsetTabular::getCellXPos(int cell) const
907 for (; !tabular.isFirstCellInRow(c); --c)
909 int lx = tabular.getWidthOfColumn(cell);
910 for (; c < cell; ++c)
911 lx += tabular.getWidthOfColumn(c);
913 return lx - tabular.getWidthOfColumn(cell) + xo_;
917 void InsetTabular::resetPos(LCursor &) const
921 #warning This should be fixed in the right manner (20011128 Jug)
923 // fast hack to fix infinite repaintings!
924 if (in_reset_pos > 0)
927 BufferView & bv = cur.bv();
929 int actcell = cur.idx();
930 int actcol = tabular.column_of_cell(actcell);
933 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
934 if (tabular.isLastCellInRow(cell)) {
935 cursory_ += tabular.getDescentOfRow(actrow) +
936 tabular.getAscentOfRow(actrow + 1) +
937 tabular.getAdditionalHeight(actrow + 1);
942 // we need this only from here on!!!
944 int const offset = ADD_TO_TABULAR_WIDTH + 2;
945 int new_x = getCellXPos(actcell) + offset;
946 int old_x = cursorx_;
948 // cursor.x(getCellXPos(actcell) + offset);
949 if (actcol < tabular.columns() - 1 && scroll(false) &&
950 tabular.getWidthOfTabular() < bv.workWidth()-20)
954 } else if (cursorx_ - offset > 20 &&
955 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
956 > bv.workWidth() - 20) {
957 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
959 } else if (cursorx_ - offset < 20) {
960 scroll(bv, 20 - cursorx_ + offset);
962 } else if (scroll() && xo_ > 20 &&
963 xo_ + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
964 scroll(bv, old_x - cursorx_);
967 InsetTabularMailer(*this).updateDialog(&bv);
973 bool InsetTabular::moveRight(LCursor & cur)
975 bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
983 bool InsetTabular::moveLeft(LCursor & cur)
985 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
993 bool InsetTabular::moveLeftLock(LCursor & cur)
995 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
998 activateCellInset(cur, cur.idx(), true);
1003 bool InsetTabular::moveUp(LCursor & cur)
1005 if (tabular.row_of_cell(cur.idx()) == 0)
1007 cur.idx() = tabular.getCellAbove(cur.idx());
1013 bool InsetTabular::moveUpLock(LCursor & cur)
1015 if (tabular.row_of_cell(cur.idx()) == 0)
1017 cur.idx() = tabular.getCellAbove(cur.idx());
1019 activateCellInset(cur, cur.idx(), cur.x_target(), 0);
1024 bool InsetTabular::moveDown(LCursor & cur)
1026 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1028 cur.idx() = tabular.getCellBelow(cur.idx());
1034 bool InsetTabular::moveDownLock(LCursor & cur)
1036 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1038 cur.idx() = tabular.getCellBelow(cur.idx());
1040 activateCellInset(cur, cur.idx(), cur.x_target());
1045 bool InsetTabular::moveNextCell(LCursor & cur)
1047 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1048 if (isRightToLeft(cur)) {
1049 if (tabular.isFirstCellInRow(cur.idx())) {
1050 int row = tabular.row_of_cell(cur.idx());
1051 if (row == tabular.rows() - 1)
1053 cur.idx() = tabular.getLastCellInRow(row);
1054 cur.idx() = tabular.getCellBelow(cur.idx());
1061 if (tabular.isLastCell(cur.idx()))
1067 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1073 bool InsetTabular::movePrevCell(LCursor & cur)
1075 if (isRightToLeft(cur)) {
1076 if (tabular.isLastCellInRow(cur.idx())) {
1077 int row = tabular.row_of_cell(cur.idx());
1080 cur.idx() = tabular.getFirstCellInRow(row);
1081 cur.idx() = tabular.getCellAbove(cur.idx());
1083 if (tabular.isLastCell(cur.idx()))
1088 if (cur.idx() == 0) // first cell
1099 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1101 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1104 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1105 string const tmp = tabularFeature[i].feature;
1107 if (tmp == what.substr(0, tmp.length())) {
1108 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1109 //tabularFeatures[i].feature.length())) {
1110 action = tabularFeature[i].action;
1114 if (action == LyXTabular::LAST_ACTION)
1118 ltrim(what.substr(tabularFeature[i].feature.length()));
1119 tabularFeatures(cur, action, val);
1126 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1127 string const & special, bool & flag)
1129 if (special == "dl_above") {
1132 } else if (special == "dl_below") {
1133 ltt.bottomDL = flag;
1135 } else if (special == "empty") {
1147 void InsetTabular::tabularFeatures(LCursor & cur,
1148 LyXTabular::Feature feature, string const & value)
1150 BufferView & bv = cur.bv();
1151 int actcell = cur.idx();
1156 bool setLines = false;
1157 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1158 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1162 case LyXTabular::M_ALIGN_LEFT:
1163 case LyXTabular::ALIGN_LEFT:
1164 setAlign = LYX_ALIGN_LEFT;
1167 case LyXTabular::M_ALIGN_RIGHT:
1168 case LyXTabular::ALIGN_RIGHT:
1169 setAlign = LYX_ALIGN_RIGHT;
1172 case LyXTabular::M_ALIGN_CENTER:
1173 case LyXTabular::ALIGN_CENTER:
1174 setAlign = LYX_ALIGN_CENTER;
1177 case LyXTabular::ALIGN_BLOCK:
1178 setAlign = LYX_ALIGN_BLOCK;
1181 case LyXTabular::M_VALIGN_TOP:
1182 case LyXTabular::VALIGN_TOP:
1183 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1186 case LyXTabular::M_VALIGN_BOTTOM:
1187 case LyXTabular::VALIGN_BOTTOM:
1188 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1191 case LyXTabular::M_VALIGN_MIDDLE:
1192 case LyXTabular::VALIGN_MIDDLE:
1193 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1200 recordUndo(cur, Undo::ATOMIC);
1202 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1203 int row = tabular.row_of_cell(actcell);
1204 int column = tabular.column_of_cell(actcell);
1206 LyXTabular::ltType ltt;
1210 case LyXTabular::SET_PWIDTH: {
1211 LyXLength const len(value);
1212 tabular.setColumnPWidth(actcell, len);
1214 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1215 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1216 else if (!len.zero()
1217 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1218 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1222 case LyXTabular::SET_MPWIDTH:
1223 tabular.setMColumnPWidth(actcell, LyXLength(value));
1226 case LyXTabular::SET_SPECIAL_COLUMN:
1227 case LyXTabular::SET_SPECIAL_MULTI:
1228 tabular.setAlignSpecial(actcell,value,feature);
1231 case LyXTabular::APPEND_ROW:
1232 // append the row into the tabular
1233 tabular.appendRow(bv.buffer()->params(), actcell);
1234 tabular.setOwner(this);
1237 case LyXTabular::APPEND_COLUMN:
1238 // append the column into the tabular
1239 tabular.appendColumn(bv.buffer()->params(), actcell);
1240 tabular.setOwner(this);
1241 actcell = tabular.getCellNumber(row, column);
1244 case LyXTabular::DELETE_ROW:
1245 for (int i = sel_row_start; i <= sel_row_end; ++i)
1246 tabular.deleteRow(sel_row_start);
1247 if (sel_row_start >= tabular.rows())
1249 actcell = tabular.getCellNumber(sel_row_start, column);
1250 cur.selection() = false;
1253 case LyXTabular::DELETE_COLUMN:
1254 for (int i = sel_col_start; i <= sel_col_end; ++i)
1255 tabular.deleteColumn(sel_col_start);
1256 if (sel_col_start >= tabular.columns())
1258 actcell = tabular.getCellNumber(row, sel_col_start);
1259 cur.selection() = false;
1262 case LyXTabular::M_TOGGLE_LINE_TOP:
1264 case LyXTabular::TOGGLE_LINE_TOP: {
1265 bool lineSet = !tabular.topLine(actcell, flag);
1266 for (int i = sel_row_start; i <= sel_row_end; ++i)
1267 for (int j = sel_col_start; j <= sel_col_end; ++j)
1269 tabular.getCellNumber(i, j),
1274 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1276 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1277 bool lineSet = !tabular.bottomLine(actcell, flag);
1278 for (int i = sel_row_start; i <= sel_row_end; ++i)
1279 for (int j = sel_col_start; j <= sel_col_end; ++j)
1280 tabular.setBottomLine(
1281 tabular.getCellNumber(i, j),
1287 case LyXTabular::M_TOGGLE_LINE_LEFT:
1289 case LyXTabular::TOGGLE_LINE_LEFT: {
1290 bool lineSet = !tabular.leftLine(actcell, flag);
1291 for (int i = sel_row_start; i <= sel_row_end; ++i)
1292 for (int j = sel_col_start; j <= sel_col_end; ++j)
1293 tabular.setLeftLine(
1294 tabular.getCellNumber(i,j),
1300 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1302 case LyXTabular::TOGGLE_LINE_RIGHT: {
1303 bool lineSet = !tabular.rightLine(actcell, flag);
1304 for (int i = sel_row_start; i <= sel_row_end; ++i)
1305 for (int j = sel_col_start; j <= sel_col_end; ++j)
1306 tabular.setRightLine(
1307 tabular.getCellNumber(i,j),
1313 case LyXTabular::M_ALIGN_LEFT:
1314 case LyXTabular::M_ALIGN_RIGHT:
1315 case LyXTabular::M_ALIGN_CENTER:
1317 case LyXTabular::ALIGN_LEFT:
1318 case LyXTabular::ALIGN_RIGHT:
1319 case LyXTabular::ALIGN_CENTER:
1320 case LyXTabular::ALIGN_BLOCK:
1321 for (int i = sel_row_start; i <= sel_row_end; ++i)
1322 for (int j = sel_col_start; j <= sel_col_end; ++j)
1323 tabular.setAlignment(
1324 tabular.getCellNumber(i, j),
1329 case LyXTabular::M_VALIGN_TOP:
1330 case LyXTabular::M_VALIGN_BOTTOM:
1331 case LyXTabular::M_VALIGN_MIDDLE:
1333 case LyXTabular::VALIGN_TOP:
1334 case LyXTabular::VALIGN_BOTTOM:
1335 case LyXTabular::VALIGN_MIDDLE:
1336 for (int i = sel_row_start; i <= sel_row_end; ++i)
1337 for (int j = sel_col_start; j <= sel_col_end; ++j)
1338 tabular.setVAlignment(
1339 tabular.getCellNumber(i, j),
1343 case LyXTabular::MULTICOLUMN: {
1344 if (sel_row_start != sel_row_end) {
1345 #ifdef WITH_WARNINGS
1346 #warning Need I say it ? This is horrible.
1348 Alert::error(_("Error setting multicolumn"),
1349 _("You cannot set multicolumn vertically."));
1353 // just multicol for one Single Cell
1354 if (!hasSelection()) {
1355 // check wether we are completly in a multicol
1356 if (tabular.isMultiColumn(actcell))
1357 tabular.unsetMultiColumn(actcell);
1359 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1362 // we have a selection so this means we just add all this
1363 // cells to form a multicolumn cell
1367 if (sel_cell_start > sel_cell_end) {
1368 s_start = sel_cell_end;
1369 s_end = sel_cell_start;
1371 s_start = sel_cell_start;
1372 s_end = sel_cell_end;
1374 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1377 cur.selection() = false;
1381 case LyXTabular::SET_ALL_LINES:
1383 case LyXTabular::UNSET_ALL_LINES:
1385 for (int i = sel_row_start; i <= sel_row_end; ++i)
1386 for (int j = sel_col_start; j <= sel_col_end; ++j)
1387 tabular.setAllLines(
1388 tabular.getCellNumber(i,j), setLines);
1392 case LyXTabular::SET_LONGTABULAR:
1393 tabular.setLongTabular(true);
1396 case LyXTabular::UNSET_LONGTABULAR:
1397 tabular.setLongTabular(false);
1400 case LyXTabular::SET_ROTATE_TABULAR:
1401 tabular.setRotateTabular(true);
1404 case LyXTabular::UNSET_ROTATE_TABULAR:
1405 tabular.setRotateTabular(false);
1408 case LyXTabular::SET_ROTATE_CELL:
1409 for (int i = sel_row_start; i <= sel_row_end; ++i)
1410 for (int j = sel_col_start; j <= sel_col_end; ++j)
1411 tabular.setRotateCell(
1412 tabular.getCellNumber(i, j), true);
1415 case LyXTabular::UNSET_ROTATE_CELL:
1416 for (int i = sel_row_start; i <= sel_row_end; ++i)
1417 for (int j = sel_col_start; j <= sel_col_end; ++j)
1418 tabular.setRotateCell(
1419 tabular.getCellNumber(i, j), false);
1422 case LyXTabular::SET_USEBOX: {
1423 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1424 if (val == tabular.getUsebox(actcell))
1425 val = LyXTabular::BOX_NONE;
1426 for (int i = sel_row_start; i <= sel_row_end; ++i)
1427 for (int j = sel_col_start; j <= sel_col_end; ++j)
1428 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1432 case LyXTabular::UNSET_LTFIRSTHEAD:
1434 case LyXTabular::SET_LTFIRSTHEAD:
1435 tabular.getRowOfLTFirstHead(row, ltt);
1436 checkLongtableSpecial(ltt, value, flag);
1437 tabular.setLTHead(row, flag, ltt, true);
1440 case LyXTabular::UNSET_LTHEAD:
1442 case LyXTabular::SET_LTHEAD:
1443 tabular.getRowOfLTHead(row, ltt);
1444 checkLongtableSpecial(ltt, value, flag);
1445 tabular.setLTHead(row, flag, ltt, false);
1448 case LyXTabular::UNSET_LTFOOT:
1450 case LyXTabular::SET_LTFOOT:
1451 tabular.getRowOfLTFoot(row, ltt);
1452 checkLongtableSpecial(ltt, value, flag);
1453 tabular.setLTFoot(row, flag, ltt, false);
1456 case LyXTabular::UNSET_LTLASTFOOT:
1458 case LyXTabular::SET_LTLASTFOOT:
1459 tabular.getRowOfLTLastFoot(row, ltt);
1460 checkLongtableSpecial(ltt, value, flag);
1461 tabular.setLTFoot(row, flag, ltt, true);
1464 case LyXTabular::SET_LTNEWPAGE:
1465 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1468 // dummy stuff just to avoid warnings
1469 case LyXTabular::LAST_ACTION:
1474 InsetTabularMailer(*this).updateDialog(&bv);
1478 void InsetTabular::activateCellInset(LCursor & cur, int cell, int x, int y)
1481 tabular.getCellInset(cell).editXY(cur, x, y);
1486 void InsetTabular::activateCellInset(LCursor & cur, int cell, bool behind)
1489 tabular.getCellInset(cell).edit(cur, behind);
1494 bool InsetTabular::showInsetDialog(BufferView * bv) const
1496 InsetTabularMailer(*this).showDialog(bv);
1501 void InsetTabular::openLayoutDialog(BufferView * bv) const
1503 InsetTabularMailer(*this).showDialog(bv);
1508 // function returns an object as defined in func_status.h:
1509 // states OK, Unknown, Disabled, On, Off.
1511 FuncStatus InsetTabular::getStatus(BufferView & bv,
1512 string const & what, int actcell) const
1515 int action = LyXTabular::LAST_ACTION;
1516 LCursor & cur = bv.cursor();
1519 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1520 string const tmp = tabularFeature[i].feature;
1521 if (tmp == what.substr(0, tmp.length())) {
1522 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1523 // tabularFeatures[i].feature.length()))
1524 action = tabularFeature[i].action;
1528 if (action == LyXTabular::LAST_ACTION) {
1530 status.unknown(true);
1534 string const argument
1535 = ltrim(what.substr(tabularFeature[i].feature.length()));
1537 int sel_row_start = 0;
1538 int sel_row_end = 0;
1540 LyXTabular::ltType dummyltt;
1543 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
1546 case LyXTabular::SET_PWIDTH:
1547 case LyXTabular::SET_MPWIDTH:
1548 case LyXTabular::SET_SPECIAL_COLUMN:
1549 case LyXTabular::SET_SPECIAL_MULTI:
1550 case LyXTabular::APPEND_ROW:
1551 case LyXTabular::APPEND_COLUMN:
1552 case LyXTabular::DELETE_ROW:
1553 case LyXTabular::DELETE_COLUMN:
1554 case LyXTabular::SET_ALL_LINES:
1555 case LyXTabular::UNSET_ALL_LINES:
1559 case LyXTabular::MULTICOLUMN:
1560 status.setOnOff(tabular.isMultiColumn(actcell));
1563 case LyXTabular::M_TOGGLE_LINE_TOP:
1565 case LyXTabular::TOGGLE_LINE_TOP:
1566 status.setOnOff(tabular.topLine(actcell, flag));
1569 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1571 case LyXTabular::TOGGLE_LINE_BOTTOM:
1572 status.setOnOff(tabular.bottomLine(actcell, flag));
1575 case LyXTabular::M_TOGGLE_LINE_LEFT:
1577 case LyXTabular::TOGGLE_LINE_LEFT:
1578 status.setOnOff(tabular.leftLine(actcell, flag));
1581 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1583 case LyXTabular::TOGGLE_LINE_RIGHT:
1584 status.setOnOff(tabular.rightLine(actcell, flag));
1587 case LyXTabular::M_ALIGN_LEFT:
1589 case LyXTabular::ALIGN_LEFT:
1590 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1593 case LyXTabular::M_ALIGN_RIGHT:
1595 case LyXTabular::ALIGN_RIGHT:
1596 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1599 case LyXTabular::M_ALIGN_CENTER:
1601 case LyXTabular::ALIGN_CENTER:
1602 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1605 case LyXTabular::ALIGN_BLOCK:
1606 status.disabled(tabular.getPWidth(actcell).zero());
1607 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1610 case LyXTabular::M_VALIGN_TOP:
1612 case LyXTabular::VALIGN_TOP:
1614 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1617 case LyXTabular::M_VALIGN_BOTTOM:
1619 case LyXTabular::VALIGN_BOTTOM:
1621 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1624 case LyXTabular::M_VALIGN_MIDDLE:
1626 case LyXTabular::VALIGN_MIDDLE:
1628 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1631 case LyXTabular::SET_LONGTABULAR:
1632 status.setOnOff(tabular.isLongTabular());
1635 case LyXTabular::UNSET_LONGTABULAR:
1636 status.setOnOff(!tabular.isLongTabular());
1639 case LyXTabular::SET_ROTATE_TABULAR:
1640 status.setOnOff(tabular.getRotateTabular());
1643 case LyXTabular::UNSET_ROTATE_TABULAR:
1644 status.setOnOff(!tabular.getRotateTabular());
1647 case LyXTabular::SET_ROTATE_CELL:
1648 status.setOnOff(tabular.getRotateCell(actcell));
1651 case LyXTabular::UNSET_ROTATE_CELL:
1652 status.setOnOff(!tabular.getRotateCell(actcell));
1655 case LyXTabular::SET_USEBOX:
1656 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1659 case LyXTabular::SET_LTFIRSTHEAD:
1660 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1663 case LyXTabular::SET_LTHEAD:
1664 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1667 case LyXTabular::SET_LTFOOT:
1668 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1671 case LyXTabular::SET_LTLASTFOOT:
1672 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1675 case LyXTabular::SET_LTNEWPAGE:
1676 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1681 status.disabled(true);
1688 void InsetTabular::getLabelList(Buffer const & buffer,
1689 vector<string> & list) const
1691 tabular.getLabelList(buffer, list);
1695 bool InsetTabular::copySelection(LCursor & cur)
1697 if (!cur.selection())
1701 getSelection(cur, rs, re, cs, ce);
1703 paste_tabular.reset(new LyXTabular(tabular));
1704 paste_tabular->setOwner(this);
1706 for (int i = 0; i < rs; ++i)
1707 paste_tabular->deleteRow(0);
1709 int const rows = re - rs + 1;
1710 while (paste_tabular->rows() > rows)
1711 paste_tabular->deleteRow(rows);
1713 paste_tabular->setTopLine(0, true, true);
1714 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1717 for (int i = 0; i < cs; ++i)
1718 paste_tabular->deleteColumn(0);
1720 int const columns = ce - cs + 1;
1721 while (paste_tabular->columns() > columns)
1722 paste_tabular->deleteColumn(columns);
1724 paste_tabular->setLeftLine(0, true, true);
1725 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1729 OutputParams const runparams;
1730 paste_tabular->plaintext(*cur.bv().buffer(), os, runparams, 0, true, '\t');
1731 cur.bv().stuffClipboard(os.str());
1736 bool InsetTabular::pasteSelection(LCursor & cur)
1740 int actcell = cur.idx();
1741 int actcol = tabular.column_of_cell(actcell);
1742 int actrow = tabular.row_of_cell(actcell);
1743 for (int r1 = 0, r2 = actrow;
1744 r1 < paste_tabular->rows() && r2 < tabular.rows();
1746 for (int c1 = 0, c2 = actcol;
1747 c1 < paste_tabular->columns() && c2 < tabular.columns();
1749 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1750 tabular.isPartOfMultiColumn(r2, c2))
1752 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1756 if (tabular.isPartOfMultiColumn(r2, c2)) {
1760 InsetText & inset = tabular.getCellInset(r2, c2);
1761 inset = paste_tabular->getCellInset(r1, c1);
1762 inset.setOwner(this);
1770 void InsetTabular::cutSelection(LCursor & cur)
1772 if (!cur.selection())
1775 bool const track = cur.bv().buffer()->params().tracking_changes;
1777 getSelection(cur, rs, re, cs, ce);
1778 for (int i = rs; i <= re; ++i)
1779 for (int j = cs; j <= ce; ++j)
1780 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(track);
1784 bool InsetTabular::isRightToLeft(LCursor & cur)
1786 return cur.bv().getParentLanguage(this)->RightToLeft();
1790 void InsetTabular::getSelection(LCursor & cur,
1791 int & rs, int & re, int & cs, int & ce) const
1793 CursorSlice & beg = cur.selBegin();
1794 CursorSlice & end = cur.selEnd();
1795 cs = tabular.column_of_cell(beg.idx());
1796 ce = tabular.column_of_cell(end.idx());
1799 cs = tabular.column_of_cell(end.idx());
1801 ce = tabular.right_column_of_cell(end.idx());
1804 rs = tabular.row_of_cell(beg.idx());
1805 re = tabular.row_of_cell(end.idx());
1811 int InsetTabular::numParagraphs() const
1813 return tabular.getNumberOfCells();
1817 LyXText * InsetTabular::getText(int i) const
1819 return i < tabular.getNumberOfCells()
1820 ? tabular.getCellInset(i).getText(0)
1825 void InsetTabular::markErased()
1827 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1828 tabular.getCellInset(cell).markErased();
1832 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1835 const int cell = tabular.getCellFromInset(in);
1838 return tabular.getPWidth(cell).zero();
1840 // this is a workaround for a crash (New, Insert->Tabular,
1841 // Insert->FootNote)
1845 // well we didn't obviously find it so maybe our owner knows more
1846 BOOST_ASSERT(owner());
1847 return owner()->forceDefaultParagraphs(in);
1853 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1856 if (buf.length() <= 0)
1862 string::size_type len = buf.length();
1863 string::size_type p = 0;
1865 int actcell = bv.cursor().idx();
1866 int actcol = tabular.column_of_cell(actcell);
1867 int actrow = tabular.row_of_cell(actcell);
1869 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1877 maxCols = max(cols, maxCols);
1883 maxCols = max(cols, maxCols);
1884 LyXTabular * loctab;
1889 paste_tabular.reset(
1890 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1891 paste_tabular->setOwner(this);
1892 loctab = paste_tabular.get();
1901 string::size_type op = 0;
1902 int cells = loctab->getNumberOfCells();
1905 rows = loctab->rows();
1906 int const columns = loctab->columns();
1908 while (cell < cells && p < len && row < rows &&
1909 (p = buf.find_first_of("\t\n", p)) != string::npos)
1915 // we can only set this if we are not too far right
1916 if (cols < columns) {
1917 InsetText & inset = loctab->getCellInset(cell);
1918 LyXFont const font = inset.text_.
1919 getFont(inset.paragraphs().begin(), 0);
1920 inset.setText(buf.substr(op, p - op), font);
1926 // we can only set this if we are not too far right
1927 if (cols < columns) {
1928 InsetText & inset = tabular.getCellInset(cell);
1929 LyXFont const font = inset.text_.
1930 getFont(inset.paragraphs().begin(), 0);
1931 inset.setText(buf.substr(op, p - op), font);
1936 cell = loctab->getCellNumber(row, cols);
1942 // check for the last cell if there is no trailing '\n'
1943 if (cell < cells && op < len) {
1944 InsetText & inset = loctab->getCellInset(cell);
1945 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
1946 inset.setText(buf.substr(op, len - op), font);
1952 void InsetTabular::addPreview(PreviewLoader & loader) const
1954 int const rows = tabular.rows();
1955 int const columns = tabular.columns();
1956 for (int i = 0; i < rows; ++i) {
1957 for (int j = 0; j < columns; ++j)
1958 tabular.getCellInset(i, j).addPreview(loader);
1963 bool InsetTabular::tablemode(LCursor & cur) const
1965 return cur.selBegin().idx() != cur.selEnd().idx();
1969 string const InsetTabularMailer::name_("tabular");
1971 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1972 : inset_(const_cast<InsetTabular &>(inset))
1976 string const InsetTabularMailer::inset2string(Buffer const &) const
1978 return params2string(inset_);
1982 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1984 istringstream data(in);
1986 lex.setStream(data);
1988 #warning CHECK verify that this is a sane value to return.
1994 string const token = lex.getString();
2002 string const token = lex.getString();
2003 if (token != "\\active_cell")
2006 cell = lex.getInteger();
2009 // This is part of the inset proper that is usually swallowed
2010 // by Buffer::readInset
2013 string const token = lex.getString();
2014 if (token != "Tabular")
2021 Buffer const & buffer = inset.buffer();
2022 inset.read(buffer, lex);
2024 // We can't set the active cell, but we can tell the frontend
2030 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2034 //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2035 data << name_ << " \\active_cell " << 0 << '\n';
2036 inset.write(inset.buffer(), data);
2037 data << "\\end_inset\n";