2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/Application.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #include "frontends/Selection.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/gettext.h"
58 #include "support/lstrings.h"
60 #include <boost/scoped_ptr.hpp>
68 using namespace lyx::support;
70 using boost::shared_ptr;
71 using boost::dynamic_pointer_cast;
76 using cap::dirtyTabularStack;
77 using cap::tabularStackDirty;
79 using graphics::PreviewLoader;
81 using frontend::Painter;
82 using frontend::Clipboard;
84 namespace Alert = frontend::Alert;
89 int const ADD_TO_HEIGHT = 2; // in cell
90 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
91 int const default_line_space = 10; // ?
92 int const WIDTH_OF_LINE = 5; // space between double lines
96 boost::scoped_ptr<Tabular> paste_tabular;
99 struct TabularFeature {
100 Tabular::Feature action;
105 TabularFeature tabularFeature[] =
107 { Tabular::APPEND_ROW, "append-row" },
108 { Tabular::APPEND_COLUMN, "append-column" },
109 { Tabular::DELETE_ROW, "delete-row" },
110 { Tabular::DELETE_COLUMN, "delete-column" },
111 { Tabular::COPY_ROW, "copy-row" },
112 { Tabular::COPY_COLUMN, "copy-column" },
113 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
114 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
115 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
116 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
117 { Tabular::ALIGN_LEFT, "align-left" },
118 { Tabular::ALIGN_RIGHT, "align-right" },
119 { Tabular::ALIGN_CENTER, "align-center" },
120 { Tabular::ALIGN_BLOCK, "align-block" },
121 { Tabular::VALIGN_TOP, "valign-top" },
122 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
123 { Tabular::VALIGN_MIDDLE, "valign-middle" },
124 { Tabular::M_ALIGN_LEFT, "m-align-left" },
125 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
126 { Tabular::M_ALIGN_CENTER, "m-align-center" },
127 { Tabular::M_VALIGN_TOP, "m-valign-top" },
128 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
129 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
130 { Tabular::MULTICOLUMN, "multicolumn" },
131 { Tabular::SET_ALL_LINES, "set-all-lines" },
132 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
133 { Tabular::SET_LONGTABULAR, "set-longtabular" },
134 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
135 { Tabular::SET_PWIDTH, "set-pwidth" },
136 { Tabular::SET_MPWIDTH, "set-mpwidth" },
137 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
138 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
139 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
140 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
141 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
142 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
143 { Tabular::SET_USEBOX, "set-usebox" },
144 { Tabular::SET_LTHEAD, "set-lthead" },
145 { Tabular::UNSET_LTHEAD, "unset-lthead" },
146 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
147 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
148 { Tabular::SET_LTFOOT, "set-ltfoot" },
149 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
150 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
151 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
152 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
153 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
154 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
155 { Tabular::SET_BOOKTABS, "set-booktabs" },
156 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
157 { Tabular::SET_TOP_SPACE, "set-top-space" },
158 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
159 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
160 { Tabular::SET_BORDER_LINES, "set-border-lines" },
161 { Tabular::LAST_ACTION, "" }
165 class FeatureEqual : public unary_function<TabularFeature, bool> {
167 FeatureEqual(Tabular::Feature feature)
168 : feature_(feature) {}
169 bool operator()(TabularFeature const & tf) const {
170 return tf.action == feature_;
173 Tabular::Feature feature_;
178 string const write_attribute(string const & name, T const & t)
180 string const s = tostr(t);
181 return s.empty() ? s : " " + name + "=\"" + s + "\"";
185 string const write_attribute(string const & name, string const & t)
187 return t.empty() ? t : " " + name + "=\"" + t + "\"";
192 string const write_attribute(string const & name, docstring const & t)
194 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
199 string const write_attribute(string const & name, bool const & b)
201 // we write only true attribute values so we remove a bit of the
202 // file format bloat for tabulars.
203 return b ? write_attribute(name, convert<string>(b)) : string();
208 string const write_attribute(string const & name, int const & i)
210 // we write only true attribute values so we remove a bit of the
211 // file format bloat for tabulars.
212 return i ? write_attribute(name, convert<string>(i)) : string();
217 string const write_attribute(string const & name, Tabular::idx_type const & i)
219 // we write only true attribute values so we remove a bit of the
220 // file format bloat for tabulars.
221 return i ? write_attribute(name, convert<string>(i)) : string();
226 string const write_attribute(string const & name, Length const & value)
228 // we write only the value if we really have one same reson as above.
229 return value.zero() ? string() : write_attribute(name, value.asString());
233 string const tostr(LyXAlignment const & num)
238 case LYX_ALIGN_BLOCK:
242 case LYX_ALIGN_CENTER:
244 case LYX_ALIGN_RIGHT:
246 case LYX_ALIGN_LAYOUT:
248 case LYX_ALIGN_SPECIAL:
255 string const tostr(Tabular::VAlignment const & num)
258 case Tabular::LYX_VALIGN_TOP:
260 case Tabular::LYX_VALIGN_MIDDLE:
262 case Tabular::LYX_VALIGN_BOTTOM:
269 string const tostr(Tabular::BoxType const & num)
272 case Tabular::BOX_NONE:
274 case Tabular::BOX_PARBOX:
276 case Tabular::BOX_MINIPAGE:
283 // I would have liked a fromstr template a lot better. (Lgb)
284 bool string2type(string const str, LyXAlignment & num)
287 num = LYX_ALIGN_NONE;
288 else if (str == "block")
289 num = LYX_ALIGN_BLOCK;
290 else if (str == "left")
291 num = LYX_ALIGN_LEFT;
292 else if (str == "center")
293 num = LYX_ALIGN_CENTER;
294 else if (str == "right")
295 num = LYX_ALIGN_RIGHT;
302 bool string2type(string const str, Tabular::VAlignment & num)
305 num = Tabular::LYX_VALIGN_TOP;
306 else if (str == "middle" )
307 num = Tabular::LYX_VALIGN_MIDDLE;
308 else if (str == "bottom")
309 num = Tabular::LYX_VALIGN_BOTTOM;
316 bool string2type(string const str, Tabular::BoxType & num)
319 num = Tabular::BOX_NONE;
320 else if (str == "parbox")
321 num = Tabular::BOX_PARBOX;
322 else if (str == "minipage")
323 num = Tabular::BOX_MINIPAGE;
330 bool string2type(string const str, bool & num)
334 else if (str == "false")
342 bool getTokenValue(string const & str, char const * token, string & ret)
345 size_t token_length = strlen(token);
346 size_t pos = str.find(token);
348 if (pos == string::npos || pos + token_length + 1 >= str.length()
349 || str[pos + token_length] != '=')
351 pos += token_length + 1;
353 if (ch != '"' && ch != '\'') { // only read till next space
357 while (pos < str.length() - 1 && str[++pos] != ch)
364 bool getTokenValue(string const & str, char const * token, docstring & ret)
367 bool const success = getTokenValue(str, token, tmp);
368 ret = from_utf8(tmp);
373 bool getTokenValue(string const & str, char const * token, int & num)
377 if (!getTokenValue(str, token, tmp))
379 num = convert<int>(tmp);
384 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
387 return getTokenValue(str, token, tmp) && string2type(tmp, num);
391 bool getTokenValue(string const & str, char const * token,
392 Tabular::VAlignment & num)
395 return getTokenValue(str, token, tmp) && string2type(tmp, num);
399 bool getTokenValue(string const & str, char const * token,
400 Tabular::BoxType & num)
403 return getTokenValue(str, token, tmp) && string2type(tmp, num);
407 bool getTokenValue(string const & str, char const * token, bool & flag)
409 // set the flag always to false as this should be the default for bools
410 // not in the file-format.
413 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
417 bool getTokenValue(string const & str, char const * token, Length & len)
419 // set the length to be zero() as default as this it should be if not
420 // in the file format.
423 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
427 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
432 if (!getTokenValue(str, token, tmp))
434 if (tmp == "default") {
438 return isValidLength(tmp, &len);
442 void l_getline(istream & is, string & str)
445 while (str.empty()) {
447 if (!str.empty() && str[str.length() - 1] == '\r')
448 str.erase(str.length() - 1);
455 string const featureAsString(Tabular::Feature feature)
457 TabularFeature * end = tabularFeature +
458 sizeof(tabularFeature) / sizeof(TabularFeature);
459 TabularFeature * it = find_if(tabularFeature, end,
460 FeatureEqual(feature));
461 return (it == end) ? string() : it->feature;
466 /////////////////////////////////////////////////////////////////////
470 /////////////////////////////////////////////////////////////////////
473 Tabular::CellData::CellData(Buffer const & buf, Tabular const & table)
476 multicolumn(Tabular::CELL_NORMAL),
477 alignment(LYX_ALIGN_CENTER),
478 valignment(LYX_VALIGN_TOP),
485 inset(new InsetTableCell(buf, this, &table))
487 inset->setBuffer(const_cast<Buffer &>(buf));
488 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
492 Tabular::CellData::CellData(CellData const & cs)
495 multicolumn(cs.multicolumn),
496 alignment(cs.alignment),
497 valignment(cs.valignment),
498 top_line(cs.top_line),
499 bottom_line(cs.bottom_line),
500 left_line(cs.left_line),
501 right_line(cs.right_line),
504 align_special(cs.align_special),
506 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
508 inset->setCellData(this);
512 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
519 void Tabular::CellData::swap(CellData & rhs)
521 std::swap(cellno, rhs.cellno);
522 std::swap(width, rhs.width);
523 std::swap(multicolumn, rhs.multicolumn);
524 std::swap(alignment, rhs.alignment);
525 std::swap(valignment, rhs.valignment);
526 std::swap(top_line, rhs.top_line);
527 std::swap(bottom_line, rhs.bottom_line);
528 std::swap(left_line, rhs.left_line);
529 std::swap(right_line, rhs.right_line);
530 std::swap(usebox, rhs.usebox);
531 std::swap(rotate, rhs.rotate);
532 std::swap(align_special, rhs.align_special);
533 p_width.swap(rhs.p_width);
534 inset.swap(rhs.inset);
538 Tabular::RowData::RowData()
541 top_space_default(false),
542 bottom_space_default(false),
543 interline_space_default(false),
552 Tabular::ColumnData::ColumnData()
553 : alignment(LYX_ALIGN_CENTER),
554 valignment(LYX_VALIGN_TOP),
560 Tabular::ltType::ltType()
573 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
575 init(buffer, rows_arg, columns_arg);
579 // activates all lines and sets all widths to 0
580 void Tabular::init(Buffer const & buf, row_type rows_arg,
581 col_type columns_arg)
584 row_info = row_vector(rows_arg);
585 column_info = column_vector(columns_arg);
586 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf, *this)));
587 row_info.reserve(10);
588 column_info.reserve(10);
589 cell_info.reserve(100);
591 is_long_tabular = false;
593 use_booktabs = false;
594 // set silly default lines
595 for (row_type i = 0; i < rowCount(); ++i)
596 for (col_type j = 0; j < columnCount(); ++j) {
597 cell_info[i][j].top_line = true;
598 cell_info[i][j].left_line = true;
599 cell_info[i][j].bottom_line = i == 0 || i == rowCount() - 1;
600 cell_info[i][j].right_line = j == columnCount() - 1;
605 void Tabular::appendRow(idx_type const cell)
607 BufferParams const & bp = buffer().params();
608 row_type const row = cellRow(cell);
610 row_vector::iterator rit = row_info.begin() + row;
611 row_info.insert(rit, RowData());
612 // now set the values of the row before
613 row_info[row] = row_info[row + 1];
615 row_type const nrows = rowCount();
616 col_type const ncols = columnCount();
618 cell_vvector old(nrows - 1);
619 for (row_type i = 0; i < nrows - 1; ++i)
620 swap(cell_info[i], old[i]);
622 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer(), *this)));
624 for (row_type i = 0; i <= row; ++i)
625 swap(cell_info[i], old[i]);
626 for (row_type i = row + 2; i < nrows; ++i)
627 swap(cell_info[i], old[i - 1]);
630 for (col_type c = 0; c < ncols; ++c) {
631 // inherit line settings
632 idx_type const i = cellIndex(row + 1, c);
633 idx_type const j = cellIndex(row, c);
634 setLeftLine(i, isPartOfMultiColumn(row, c) ? false : leftLine(j));
635 if (cell_info[row][c].multicolumn == CELL_NORMAL || c == ncols - 1
637 && cell_info[row][c].multicolumn != CELL_NORMAL
638 && cell_info[row][c + 1].multicolumn == CELL_NORMAL))
639 setRightLine(i, rightLine(j));
641 setRightLine(i, false);
642 // mark track changes
644 cellInfo(i).inset->setChange(Change(Change::INSERTED));
649 void Tabular::deleteRow(row_type const row)
651 // Not allowed to delete last row
655 row_info.erase(row_info.begin() + row);
656 cell_info.erase(cell_info.begin() + row);
661 void Tabular::copyRow(row_type const row)
663 row_info.insert(row_info.begin() + row, row_info[row]);
664 cell_info.insert(cell_info.begin() + row, cell_info[row]);
666 if (buffer().params().trackChanges)
667 for (col_type j = 0; j < columnCount(); ++j)
668 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
674 void Tabular::appendColumn(idx_type const cell)
676 col_type const column = cellColumn(cell);
677 column_vector::iterator cit = column_info.begin() + column + 1;
678 column_info.insert(cit, ColumnData());
679 row_type const nrows = rowCount();
680 col_type const ncols = columnCount();
681 // set the column values of the column before
682 column_info[column + 1] = column_info[column];
684 for (row_type r = 0; r < nrows; ++r) {
685 cell_info[r].insert(cell_info[r].begin() + column + 1,
686 CellData(buffer(), *this));
687 col_type c = column + 2;
689 && cell_info[r][c].multicolumn == CELL_PART_OF_MULTICOLUMN) {
690 cell_info[r][c].multicolumn = CELL_NORMAL;
695 for (row_type r = 0; r < nrows; ++r) {
696 // inherit line settings
697 idx_type const i = cellIndex(r, column + 1);
698 idx_type const j = cellIndex(r, column);
699 setBottomLine(i, bottomLine(j));
700 setTopLine(i, topLine(j));
702 cellInfo(i).inset->clear();
703 if (buffer().params().trackChanges)
704 cellInfo(i).inset->setChange(Change(Change::INSERTED));
709 void Tabular::deleteColumn(col_type const column)
711 // Not allowed to delete last column
712 if (columnCount() == 1)
715 column_info.erase(column_info.begin() + column);
716 for (row_type i = 0; i < rowCount(); ++i) {
717 // Care about multicolumn cells
718 if (column + 1 < columnCount() &&
719 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
720 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
721 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
723 cell_info[i].erase(cell_info[i].begin() + column);
729 void Tabular::copyColumn(col_type const column)
731 BufferParams const & bp = buffer().params();
732 column_info.insert(column_info.begin() + column, column_info[column]);
734 for (row_type i = 0; i < rowCount(); ++i)
735 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
738 for (row_type i = 0; i < rowCount(); ++i)
739 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
744 void Tabular::updateIndexes()
746 col_type ncols = columnCount();
747 row_type nrows = rowCount();
749 for (row_type row = 0; row < nrows; ++row)
750 for (col_type column = 0; column < ncols; ++column) {
751 if (!isPartOfMultiColumn(row, column))
753 cell_info[row][column].cellno = numberofcells - 1;
756 rowofcell.resize(numberofcells);
757 columnofcell.resize(numberofcells);
759 for (row_type row = 0; row < nrows; ++row)
760 for (col_type column = 0; column < ncols; ++column) {
761 if (isPartOfMultiColumn(row, column))
764 columnofcell[i] = column;
770 Tabular::idx_type Tabular::cellCount() const
772 return numberofcells;
776 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
778 row_type const row = cellRow(cell);
780 for (col_type i = 0; i < columnCount(); ++i)
781 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
787 bool Tabular::topLine(idx_type const cell) const
789 return cellInfo(cell).top_line;
793 bool Tabular::bottomLine(idx_type const cell) const
795 return cellInfo(cell).bottom_line;
799 bool Tabular::leftLine(idx_type cell) const
803 return cellInfo(cell).left_line;
807 bool Tabular::rightLine(idx_type cell) const
811 return cellInfo(cell).right_line;
815 bool Tabular::topAlreadyDrawn(idx_type cell) const
817 row_type row = cellRow(cell);
820 idx_type i = cellIndex(row - 1, cellColumn(cell));
821 return !rowBottomLine(row - 1) && bottomLine(i);
825 bool Tabular::leftAlreadyDrawn(idx_type cell) const
827 col_type col = cellColumn(cell);
830 idx_type i = cellIndex(cellRow(cell), col - 1);
831 return rightLine(i) && !leftLine(cell);
835 bool Tabular::isLastRow(idx_type cell) const
837 return cellRow(cell) == rowCount() - 1;
841 int Tabular::getAdditionalHeight(row_type row) const
843 if (!row || row >= rowCount())
846 int const interline_space = row_info[row - 1].interline_space_default ?
848 row_info[row - 1].interline_space.inPixels(width());
849 if (rowTopLine(row) && rowBottomLine(row - 1))
850 return interline_space + WIDTH_OF_LINE;
851 return interline_space;
855 int Tabular::getAdditionalWidth(idx_type cell) const
857 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
859 && nextcol < columnCount() && leftLine(cellIndex(cellRow(cell), nextcol)))
860 return WIDTH_OF_LINE;
865 int Tabular::columnWidth(idx_type cell) const
868 col_type const span = columnSpan(cell);
869 col_type const col = cellColumn(cell);
870 for(col_type c = col; c < col + span ; ++c)
871 w += column_info[c].width;
876 bool Tabular::updateColumnWidths()
878 col_type const ncols = columnCount();
879 row_type const nrows = rowCount();
881 // for each col get max of single col cells
882 for(col_type c = 0; c < ncols; ++c) {
884 for(row_type r = 0; r < nrows; ++r) {
885 idx_type const i = cellIndex(r, c);
886 if (columnSpan(i) == 1)
887 new_width = max(new_width, cellInfo(i).width);
890 if (column_info[c].width != new_width) {
891 column_info[c].width = new_width;
895 // update col widths to fit merged cells
896 for(col_type c = 0; c < ncols; ++c)
897 for(row_type r = 0; r < nrows; ++r) {
898 idx_type const i = cellIndex(r, c);
899 int const span = columnSpan(i);
900 if (span == 1 || c > cellColumn(i))
904 for(col_type j = c; j < c + span ; ++j)
905 old_width += column_info[j].width;
907 if (cellInfo(i).width > old_width) {
908 column_info[c + span - 1].width += cellInfo(i).width - old_width;
917 int Tabular::width() const
919 col_type const ncols = columnCount();
921 for (col_type i = 0; i < ncols; ++i)
922 width += column_info[i].width;
927 void Tabular::setCellWidth(idx_type cell, int new_width)
929 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
930 + getAdditionalWidth(cell);
934 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
937 if (!isMultiColumn(cell) || onlycolumn)
938 column_info[cellColumn(cell)].alignment = align;
940 cellInfo(cell).alignment = align;
944 void Tabular::setVAlignment(idx_type cell, VAlignment align,
947 if (!isMultiColumn(cell) || onlycolumn)
948 column_info[cellColumn(cell)].valignment = align;
950 cellInfo(cell).valignment = align;
957 * Allow line and paragraph breaks for fixed width cells or disallow them,
958 * merge cell paragraphs and reset layout to standard for variable width
961 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
963 inset->setAutoBreakRows(fixedWidth);
967 // merge all paragraphs to one
968 BufferParams const & bp = cur.bv().buffer().params();
969 while (inset->paragraphs().size() > 1)
970 mergeParagraph(bp, inset->paragraphs(), 0);
974 // undo information has already been recorded
975 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
976 bp.documentClass().emptyLayoutName());
983 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
984 Length const & width)
986 col_type const j = cellColumn(cell);
988 column_info[j].p_width = width;
989 for (row_type i = 0; i < rowCount(); ++i) {
990 idx_type const cell = cellIndex(i, j);
991 // because of multicolumns
992 toggleFixedWidth(cur, cellInset(cell).get(),
993 !getPWidth(cell).zero());
995 // cur paragraph can become invalid after paragraphs were merged
996 if (cur.pit() > cur.lastpit())
997 cur.pit() = cur.lastpit();
998 // cur position can become invalid after newlines were removed
999 if (cur.pos() > cur.lastpos())
1000 cur.pos() = cur.lastpos();
1004 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1005 Length const & width)
1007 if (!isMultiColumn(cell))
1010 cellInfo(cell).p_width = width;
1011 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1012 // cur paragraph can become invalid after paragraphs were merged
1013 if (cur.pit() > cur.lastpit())
1014 cur.pit() = cur.lastpit();
1015 // cur position can become invalid after newlines were removed
1016 if (cur.pos() > cur.lastpos())
1017 cur.pos() = cur.lastpos();
1022 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1023 Tabular::Feature what)
1025 if (what == SET_SPECIAL_MULTI)
1026 cellInfo(cell).align_special = special;
1028 column_info[cellColumn(cell)].align_special = special;
1032 void Tabular::setAllLines(idx_type cell, bool line)
1034 setTopLine(cell, line);
1035 setBottomLine(cell, line);
1036 setRightLine(cell, line);
1037 setLeftLine(cell, line);
1041 void Tabular::setTopLine(idx_type i, bool line)
1043 cellInfo(i).top_line = line;
1047 void Tabular::setBottomLine(idx_type i, bool line)
1049 cellInfo(i).bottom_line = line;
1053 void Tabular::setLeftLine(idx_type cell, bool line)
1055 cellInfo(cell).left_line = line;
1059 void Tabular::setRightLine(idx_type cell, bool line)
1061 cellInfo(cell).right_line = line;
1064 bool Tabular::rowTopLine(row_type r) const
1066 idx_type i0 = getFirstCellInRow(r);
1067 idx_type i1 = getLastCellInRow(r);
1068 bool all_rows_set = true;
1069 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1070 all_rows_set = cellInfo(j).top_line;
1071 return all_rows_set;
1075 bool Tabular::rowBottomLine(row_type r) const
1077 idx_type i0 = getFirstCellInRow(r);
1078 idx_type i1 = getLastCellInRow(r);
1079 bool all_rows_set = true;
1080 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1081 all_rows_set = cellInfo(j).bottom_line;
1082 return all_rows_set;
1086 bool Tabular::columnLeftLine(col_type c) const
1088 bool all_cols_set = true;
1089 row_type const nrows = rowCount();
1090 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1091 idx_type i = cellIndex(r, c);
1092 if (c == cellColumn(i))
1093 all_cols_set = cellInfo(i).left_line;
1095 return all_cols_set;
1099 bool Tabular::columnRightLine(col_type c) const
1101 bool all_cols_set = true;
1102 row_type const nrows = rowCount();
1103 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1104 idx_type i = cellIndex(r, c);
1105 if (c == cellColumn(i) + columnSpan(i) - 1)
1106 all_cols_set = cellInfo(i).right_line;
1108 return all_cols_set;
1112 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1114 if (!onlycolumn && isMultiColumn(cell))
1115 return cellInfo(cell).alignment;
1116 return column_info[cellColumn(cell)].alignment;
1121 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1123 if (!onlycolumn && isMultiColumn(cell))
1124 return cellInfo(cell).valignment;
1125 return column_info[cellColumn(cell)].valignment;
1129 Length const Tabular::getPWidth(idx_type cell) const
1131 if (isMultiColumn(cell))
1132 return cellInfo(cell).p_width;
1133 return column_info[cellColumn(cell)].p_width;
1137 Length const Tabular::getColumnPWidth(idx_type cell) const
1139 return column_info[cellColumn(cell)].p_width;
1143 Length const Tabular::getMColumnPWidth(idx_type cell) const
1145 if (isMultiColumn(cell))
1146 return cellInfo(cell).p_width;
1151 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1153 if (what == SET_SPECIAL_MULTI)
1154 return cellInfo(cell).align_special;
1155 return column_info[cellColumn(cell)].align_special;
1159 int Tabular::cellWidth(idx_type cell) const
1161 return cellInfo(cell).width;
1165 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1169 switch (getAlignment(cell)) {
1170 case LYX_ALIGN_CENTER:
1171 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1173 case LYX_ALIGN_RIGHT:
1174 x += columnWidth(cell) - cellWidth(cell);
1175 // + getAdditionalWidth(cell);
1178 // LYX_ALIGN_LEFT: nothing :-)
1182 // the LaTeX Way :-(
1188 bool Tabular::isFirstCellInRow(idx_type cell) const
1190 return cellColumn(cell) == 0;
1194 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1196 if (row > rowCount() - 1)
1197 row = rowCount() - 1;
1198 return cell_info[row][0].cellno;
1202 bool Tabular::isLastCellInRow(idx_type cell) const
1204 return cellRightColumn(cell) == columnCount() - 1;
1208 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1210 if (row > rowCount() - 1)
1211 row = rowCount() - 1;
1212 return cell_info[row][columnCount() - 1].cellno;
1216 Tabular::row_type Tabular::cellRow(idx_type cell) const
1218 if (cell >= cellCount())
1219 return rowCount() - 1;
1222 return rowofcell[cell];
1226 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1228 if (cell >= cellCount())
1229 return columnCount() - 1;
1232 return columnofcell[cell];
1236 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1238 row_type const row = cellRow(cell);
1239 col_type column = cellColumn(cell);
1240 while (column < columnCount() - 1 &&
1241 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1247 void Tabular::write(ostream & os) const
1251 << write_attribute("version", 3)
1252 << write_attribute("rows", rowCount())
1253 << write_attribute("columns", columnCount())
1255 // global longtable options
1257 << write_attribute("rotate", rotate)
1258 << write_attribute("booktabs", use_booktabs)
1259 << write_attribute("islongtable", is_long_tabular)
1260 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1261 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1262 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1263 << write_attribute("headTopDL", endhead.topDL)
1264 << write_attribute("headBottomDL", endhead.bottomDL)
1265 << write_attribute("footTopDL", endfoot.topDL)
1266 << write_attribute("footBottomDL", endfoot.bottomDL)
1267 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1268 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1269 << write_attribute("lastFootEmpty", endlastfoot.empty)
1271 for (col_type j = 0; j < columnCount(); ++j) {
1273 << write_attribute("alignment", column_info[j].alignment)
1274 << write_attribute("valignment", column_info[j].valignment)
1275 << write_attribute("width", column_info[j].p_width.asString())
1276 << write_attribute("special", column_info[j].align_special)
1279 for (row_type i = 0; i < rowCount(); ++i) {
1280 static const string def("default");
1282 if (row_info[i].top_space_default)
1283 os << write_attribute("topspace", def);
1285 os << write_attribute("topspace", row_info[i].top_space);
1286 if (row_info[i].bottom_space_default)
1287 os << write_attribute("bottomspace", def);
1289 os << write_attribute("bottomspace", row_info[i].bottom_space);
1290 if (row_info[i].interline_space_default)
1291 os << write_attribute("interlinespace", def);
1293 os << write_attribute("interlinespace", row_info[i].interline_space);
1294 os << write_attribute("endhead", row_info[i].endhead)
1295 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1296 << write_attribute("endfoot", row_info[i].endfoot)
1297 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1298 << write_attribute("newpage", row_info[i].newpage)
1300 for (col_type j = 0; j < columnCount(); ++j) {
1302 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1303 << write_attribute("alignment", cell_info[i][j].alignment)
1304 << write_attribute("valignment", cell_info[i][j].valignment)
1305 << write_attribute("topline", cell_info[i][j].top_line)
1306 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1307 << write_attribute("leftline", cell_info[i][j].left_line)
1308 << write_attribute("rightline", cell_info[i][j].right_line)
1309 << write_attribute("rotate", cell_info[i][j].rotate)
1310 << write_attribute("usebox", cell_info[i][j].usebox)
1311 << write_attribute("width", cell_info[i][j].p_width)
1312 << write_attribute("special", cell_info[i][j].align_special)
1314 os << "\\begin_inset ";
1315 cell_info[i][j].inset->write(os);
1316 os << "\n\\end_inset\n"
1321 os << "</lyxtabular>\n";
1325 void Tabular::read(Lexer & lex)
1328 istream & is = lex.getStream();
1330 l_getline(is, line);
1331 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1332 BOOST_ASSERT(false);
1337 if (!getTokenValue(line, "version", version))
1339 BOOST_ASSERT(version >= 2);
1342 if (!getTokenValue(line, "rows", rows_arg))
1345 if (!getTokenValue(line, "columns", columns_arg))
1347 init(buffer(), rows_arg, columns_arg);
1348 l_getline(is, line);
1349 if (!prefixIs(line, "<features")) {
1350 lyxerr << "Wrong tabular format (expected <features ...> got"
1351 << line << ')' << endl;
1354 getTokenValue(line, "rotate", rotate);
1355 getTokenValue(line, "booktabs", use_booktabs);
1356 getTokenValue(line, "islongtable", is_long_tabular);
1357 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1358 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1359 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1360 getTokenValue(line, "headTopDL", endhead.topDL);
1361 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1362 getTokenValue(line, "footTopDL", endfoot.topDL);
1363 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1364 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1365 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1366 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1368 for (col_type j = 0; j < columnCount(); ++j) {
1370 if (!prefixIs(line,"<column")) {
1371 lyxerr << "Wrong tabular format (expected <column ...> got"
1372 << line << ')' << endl;
1375 getTokenValue(line, "alignment", column_info[j].alignment);
1376 getTokenValue(line, "valignment", column_info[j].valignment);
1377 getTokenValue(line, "width", column_info[j].p_width);
1378 getTokenValue(line, "special", column_info[j].align_special);
1381 for (row_type i = 0; i < rowCount(); ++i) {
1382 l_getline(is, line);
1383 if (!prefixIs(line, "<row")) {
1384 lyxerr << "Wrong tabular format (expected <row ...> got"
1385 << line << ')' << endl;
1388 getTokenValue(line, "topspace", row_info[i].top_space,
1389 row_info[i].top_space_default);
1390 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1391 row_info[i].bottom_space_default);
1392 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1393 row_info[i].interline_space_default);
1394 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1395 getTokenValue(line, "endhead", row_info[i].endhead);
1396 getTokenValue(line, "endfoot", row_info[i].endfoot);
1397 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1398 getTokenValue(line, "newpage", row_info[i].newpage);
1399 for (col_type j = 0; j < columnCount(); ++j) {
1400 l_getline(is, line);
1401 if (!prefixIs(line, "<cell")) {
1402 lyxerr << "Wrong tabular format (expected <cell ...> got"
1403 << line << ')' << endl;
1406 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1407 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1408 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1409 getTokenValue(line, "topline", cell_info[i][j].top_line);
1410 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1411 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1412 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1413 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1414 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1415 getTokenValue(line, "width", cell_info[i][j].p_width);
1416 getTokenValue(line, "special", cell_info[i][j].align_special);
1417 l_getline(is, line);
1418 if (prefixIs(line, "\\begin_inset")) {
1419 cell_info[i][j].inset->read(lex);
1420 l_getline(is, line);
1422 if (!prefixIs(line, "</cell>")) {
1423 lyxerr << "Wrong tabular format (expected </cell> got"
1424 << line << ')' << endl;
1428 l_getline(is, line);
1429 if (!prefixIs(line, "</row>")) {
1430 lyxerr << "Wrong tabular format (expected </row> got"
1431 << line << ')' << endl;
1435 while (!prefixIs(line, "</lyxtabular>")) {
1436 l_getline(is, line);
1442 bool Tabular::isMultiColumn(idx_type cell) const
1444 return cellInfo(cell).multicolumn != CELL_NORMAL;
1448 bool Tabular::isMultiColumnReal(idx_type cell) const
1450 return cellColumn(cell) != cellRightColumn(cell) &&
1451 cellInfo(cell).multicolumn != CELL_NORMAL;
1455 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1457 return cell_info[cellRow(cell)][cellColumn(cell)];
1461 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1463 CellData & cs = cellInfo(cell);
1464 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1465 cs.alignment = column_info[cellColumn(cell)].alignment;
1466 setRightLine(cell, rightLine(cell + number - 1));
1467 for (idx_type i = 1; i < number; ++i) {
1468 CellData & cs1 = cellInfo(cell + i);
1469 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1470 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1477 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1479 row_type const row = cellRow(cell);
1480 col_type const ncols = columnCount();
1481 idx_type result = 1;
1482 col_type column = cellColumn(cell) + 1;
1483 while (column < ncols && isPartOfMultiColumn(row, column)) {
1491 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1493 row_type const row = cellRow(cell);
1494 col_type column = cellColumn(cell);
1496 idx_type result = 0;
1498 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1499 cell_info[row][column].multicolumn = CELL_NORMAL;
1501 while (column < columnCount() &&
1502 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1504 cell_info[row][column].multicolumn = CELL_NORMAL;
1514 void Tabular::setBookTabs(bool what)
1516 use_booktabs = what;
1520 bool Tabular::useBookTabs() const
1522 return use_booktabs;
1526 void Tabular::setLongTabular(bool what)
1528 is_long_tabular = what;
1532 bool Tabular::isLongTabular() const
1534 return is_long_tabular;
1538 void Tabular::setRotateTabular(bool flag)
1544 bool Tabular::getRotateTabular() const
1550 void Tabular::setRotateCell(idx_type cell, bool flag)
1552 cellInfo(cell).rotate = flag;
1556 bool Tabular::getRotateCell(idx_type cell) const
1558 return cellInfo(cell).rotate;
1562 bool Tabular::needRotating() const
1566 for (row_type i = 0; i < rowCount(); ++i)
1567 for (col_type j = 0; j < columnCount(); ++j)
1568 if (cell_info[i][j].rotate)
1574 bool Tabular::isLastCell(idx_type cell) const
1576 if (cell + 1 < cellCount())
1582 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1584 if (cellRow(cell) > 0)
1585 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1590 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1592 if (cellRow(cell) + 1 < rowCount())
1593 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1598 Tabular::idx_type Tabular::cellIndex(row_type row,
1599 col_type column) const
1601 BOOST_ASSERT(column != npos && column < columnCount() &&
1602 row != npos && row < rowCount());
1603 return cell_info[row][column].cellno;
1607 void Tabular::setUsebox(idx_type cell, BoxType type)
1609 cellInfo(cell).usebox = type;
1613 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1615 if (column_info[cellColumn(cell)].p_width.zero() &&
1616 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1618 if (cellInfo(cell).usebox > 1)
1619 return cellInfo(cell).usebox;
1620 return useParbox(cell);
1625 // This are functions used for the longtable support
1627 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1633 row_info[row].endfirsthead = flag;
1637 row_info[row].endhead = flag;
1642 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1645 hd.set = haveLTHead();
1646 return row_info[row].endhead;
1650 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1653 hd.set = haveLTFirstHead();
1654 return row_info[row].endfirsthead;
1658 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1664 row_info[row].endlastfoot = flag;
1668 row_info[row].endfoot = flag;
1673 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1676 fd.set = haveLTFoot();
1677 return row_info[row].endfoot;
1681 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1684 fd.set = haveLTLastFoot();
1685 return row_info[row].endlastfoot;
1689 void Tabular::setLTNewPage(row_type row, bool what)
1691 row_info[row].newpage = what;
1695 bool Tabular::getLTNewPage(row_type row) const
1697 return row_info[row].newpage;
1701 bool Tabular::haveLTHead() const
1703 for (row_type i = 0; i < rowCount(); ++i)
1704 if (row_info[i].endhead)
1710 bool Tabular::haveLTFirstHead() const
1712 if (endfirsthead.empty)
1714 for (row_type i = 0; i < rowCount(); ++i)
1715 if (row_info[i].endfirsthead)
1721 bool Tabular::haveLTFoot() const
1723 for (row_type i = 0; i < rowCount(); ++i)
1724 if (row_info[i].endfoot)
1730 bool Tabular::haveLTLastFoot() const
1732 if (endlastfoot.empty)
1734 for (row_type i = 0; i < rowCount(); ++i)
1735 if (row_info[i].endlastfoot)
1741 // end longtable support functions
1743 void Tabular::setRowAscent(row_type row, int height)
1745 if (row >= rowCount() || row_info[row].ascent == height)
1747 row_info[row].ascent = height;
1751 void Tabular::setRowDescent(row_type row, int height)
1753 if (row >= rowCount() || row_info[row].descent == height)
1755 row_info[row].descent = height;
1759 int Tabular::rowAscent(row_type row) const
1761 if (row >= rowCount())
1763 return row_info[row].ascent;
1767 int Tabular::rowDescent(row_type row) const
1769 BOOST_ASSERT(row < rowCount());
1770 return row_info[row].descent;
1774 int Tabular::height() const
1777 for (row_type row = 0; row < rowCount(); ++row)
1778 height += rowAscent(row) + rowDescent(row) +
1779 getAdditionalHeight(row);
1784 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1786 BOOST_ASSERT(row < rowCount());
1787 BOOST_ASSERT(column < columnCount());
1788 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1792 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1794 // FIXME: assert or return 0 as in TeXBottomHLine()?
1795 BOOST_ASSERT(row != npos);
1796 BOOST_ASSERT(row < rowCount());
1798 idx_type const fcell = getFirstCellInRow(row);
1799 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1802 for (idx_type i = fcell; i < n; ++i) {
1806 if (use_booktabs && row == 0) {
1809 } else if (tmp == n - fcell) {
1810 os << (use_booktabs ? "\\midrule " : "\\hline ");
1812 for (idx_type i = fcell; i < n; ++i) {
1814 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1815 << cellColumn(i) + 1
1817 << cellRightColumn(i) + 1
1829 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1831 // FIXME: return 0 or assert as in TeXTopHLine()?
1832 if (row == npos || row >= rowCount())
1835 idx_type const fcell = getFirstCellInRow(row);
1836 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1839 for (idx_type i = fcell; i < n; ++i) {
1843 if (use_booktabs && row == rowCount() - 1) {
1844 if (bottomLine(fcell))
1845 os << "\\bottomrule";
1846 } else if (tmp == n - fcell) {
1847 os << (use_booktabs ? "\\midrule" : "\\hline");
1849 for (idx_type i = fcell; i < n; ++i) {
1850 if (bottomLine(i)) {
1851 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1852 << cellColumn(i) + 1
1854 << cellRightColumn(i) + 1
1866 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1870 if (getRotateCell(cell)) {
1871 os << "\\begin{sideways}\n";
1874 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1875 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1876 // figure out how to set the lines
1877 // we always set double lines to the right of the cell
1878 row_type const r = cellRow(cell);
1879 col_type const c = cellColumn(cell);
1880 col_type const nextcol = c + columnSpan(cell);
1881 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1882 bool forceleft = ismulticol && !prevcellright && leftLine(cell);
1883 bool nextcolleft = nextcol < columnCount() && columnLeftLine(nextcol);
1884 bool coldouble = columnRightLine(c) && nextcolleft;
1885 bool nextcellleft = nextcol < columnCount()
1886 && leftLine(cellIndex(r, nextcol));
1887 bool celldouble = rightLine(cell) && nextcellleft;
1888 bool doubleright = celldouble && (isMultiColumn(cell) || !coldouble);
1889 ismulticol = isMultiColumn(cell)
1890 || (leftLine(cell) && !columnLeftLine(c))
1891 || (rightLine(cell) && !columnRightLine(c))
1892 || (!celldouble && coldouble) || doubleright || forceleft;
1894 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1895 if (leftLine(cell) || forceleft)
1897 if (!cellInfo(cell).align_special.empty()) {
1898 os << cellInfo(cell).align_special;
1900 if (!getPWidth(cell).zero()) {
1902 case LYX_VALIGN_TOP:
1905 case LYX_VALIGN_MIDDLE:
1908 case LYX_VALIGN_BOTTOM:
1913 << from_ascii(getPWidth(cell).asLatexString())
1917 case LYX_ALIGN_LEFT:
1920 case LYX_ALIGN_RIGHT:
1927 } // end if else !getPWidth
1928 } // end if else !cellinfo_of_cell
1929 if (rightLine(cell))
1932 // add extra vertical line if we want a double one
1936 if (getUsebox(cell) == BOX_PARBOX) {
1939 case LYX_VALIGN_TOP:
1942 case LYX_VALIGN_MIDDLE:
1945 case LYX_VALIGN_BOTTOM:
1949 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1951 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1952 os << "\\begin{minipage}[";
1954 case LYX_VALIGN_TOP:
1957 case LYX_VALIGN_MIDDLE:
1960 case LYX_VALIGN_BOTTOM:
1964 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1972 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1977 if (getUsebox(cell) == BOX_PARBOX)
1979 else if (getUsebox(cell) == BOX_MINIPAGE) {
1980 os << "%\n\\end{minipage}";
1986 if (getRotateCell(cell)) {
1987 os << "%\n\\end{sideways}";
1994 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1995 OutputParams const & runparams) const
1997 if (!is_long_tabular)
2001 // output header info
2003 if (endhead.topDL) {
2007 for (row_type i = 0; i < rowCount(); ++i) {
2008 if (row_info[i].endhead) {
2009 ret += TeXRow(os, i, runparams);
2012 if (endhead.bottomDL) {
2016 os << "\\endhead\n";
2018 if (endfirsthead.empty) {
2019 os << "\\endfirsthead\n";
2023 // output firstheader info
2024 if (haveLTFirstHead()) {
2025 if (endfirsthead.topDL) {
2029 for (row_type i = 0; i < rowCount(); ++i) {
2030 if (row_info[i].endfirsthead) {
2031 ret += TeXRow(os, i, runparams);
2034 if (endfirsthead.bottomDL) {
2038 os << "\\endfirsthead\n";
2041 // output footer info
2043 if (endfoot.topDL) {
2047 for (row_type i = 0; i < rowCount(); ++i) {
2048 if (row_info[i].endfoot) {
2049 ret += TeXRow(os, i, runparams);
2052 if (endfoot.bottomDL) {
2056 os << "\\endfoot\n";
2058 if (endlastfoot.empty) {
2059 os << "\\endlastfoot\n";
2063 // output lastfooter info
2064 if (haveLTLastFoot()) {
2065 if (endlastfoot.topDL) {
2069 for (row_type i = 0; i < rowCount(); ++i) {
2070 if (row_info[i].endlastfoot) {
2071 ret += TeXRow(os, i, runparams);
2074 if (endlastfoot.bottomDL) {
2078 os << "\\endlastfoot\n";
2085 bool Tabular::isValidRow(row_type row) const
2087 if (!is_long_tabular)
2089 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2090 !row_info[row].endfoot && !row_info[row].endlastfoot;
2094 int Tabular::TeXRow(odocstream & os, row_type i,
2095 OutputParams const & runparams) const
2097 idx_type cell = cellIndex(i, 0);
2098 int ret = TeXTopHLine(os, i);
2099 if (row_info[i].top_space_default) {
2101 os << "\\addlinespace\n";
2103 os << "\\noalign{\\vskip\\doublerulesep}\n";
2105 } else if(!row_info[i].top_space.zero()) {
2107 os << "\\addlinespace["
2108 << from_ascii(row_info[i].top_space.asLatexString())
2111 os << "\\noalign{\\vskip"
2112 << from_ascii(row_info[i].top_space.asLatexString())
2117 bool ismulticol = false;
2118 for (col_type j = 0; j < columnCount(); ++j) {
2119 if (isPartOfMultiColumn(i, j))
2121 ret += TeXCellPreamble(os, cell, ismulticol);
2122 shared_ptr<InsetTableCell> inset = cellInset(cell);
2124 Paragraph const & par = inset->paragraphs().front();
2125 bool rtl = par.isRTL(buffer().params())
2127 && getPWidth(cell).zero();
2130 if (par.getParLanguage(buffer().params())->lang() ==
2133 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2135 // currently, remaning RTL languages are arabic_arabtex and hebrew
2139 ret += inset->latex(os, runparams);
2143 ret += TeXCellPostamble(os, cell, ismulticol);
2144 if (!isLastCellInRow(cell)) { // not last cell in row
2149 os << "\\tabularnewline";
2150 if (row_info[i].bottom_space_default) {
2152 os << "\\addlinespace";
2154 os << "[\\doublerulesep]";
2155 } else if (!row_info[i].bottom_space.zero()) {
2157 os << "\\addlinespace";
2159 << from_ascii(row_info[i].bottom_space.asLatexString())
2164 ret += TeXBottomHLine(os, i);
2165 if (row_info[i].interline_space_default) {
2167 os << "\\addlinespace\n";
2169 os << "\\noalign{\\vskip\\doublerulesep}\n";
2171 } else if (!row_info[i].interline_space.zero()) {
2173 os << "\\addlinespace["
2174 << from_ascii(row_info[i].interline_space.asLatexString())
2177 os << "\\noalign{\\vskip"
2178 << from_ascii(row_info[i].interline_space.asLatexString())
2186 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2190 //+---------------------------------------------------------------------
2191 //+ first the opening preamble +
2192 //+---------------------------------------------------------------------
2195 os << "\\begin{sideways}\n";
2198 if (is_long_tabular)
2199 os << "\\begin{longtable}{";
2201 os << "\\begin{tabular}{";
2203 for (col_type i = 0; i < columnCount(); ++i) {
2204 if (!use_booktabs && columnLeftLine(i))
2206 if (!column_info[i].align_special.empty()) {
2207 os << column_info[i].align_special;
2209 if (!column_info[i].p_width.zero()) {
2210 switch (column_info[i].alignment) {
2211 case LYX_ALIGN_LEFT:
2212 os << ">{\\raggedright}";
2214 case LYX_ALIGN_RIGHT:
2215 os << ">{\\raggedleft}";
2217 case LYX_ALIGN_CENTER:
2218 os << ">{\\centering}";
2220 case LYX_ALIGN_NONE:
2221 case LYX_ALIGN_BLOCK:
2222 case LYX_ALIGN_LAYOUT:
2223 case LYX_ALIGN_SPECIAL:
2227 switch (column_info[i].valignment) {
2228 case LYX_VALIGN_TOP:
2231 case LYX_VALIGN_MIDDLE:
2234 case LYX_VALIGN_BOTTOM:
2239 << from_ascii(column_info[i].p_width.asLatexString())
2242 switch (column_info[i].alignment) {
2243 case LYX_ALIGN_LEFT:
2246 case LYX_ALIGN_RIGHT:
2253 } // end if else !column_info[i].p_width
2254 } // end if else !column_info[i].align_special
2255 if (!use_booktabs && columnRightLine(i))
2261 ret += TeXLongtableHeaderFooter(os, runparams);
2263 //+---------------------------------------------------------------------
2264 //+ the single row and columns (cells) +
2265 //+---------------------------------------------------------------------
2267 for (row_type i = 0; i < rowCount(); ++i) {
2268 if (isValidRow(i)) {
2269 ret += TeXRow(os, i, runparams);
2270 if (is_long_tabular && row_info[i].newpage) {
2271 os << "\\newpage\n";
2277 //+---------------------------------------------------------------------
2278 //+ the closing of the tabular +
2279 //+---------------------------------------------------------------------
2281 if (is_long_tabular)
2282 os << "\\end{longtable}";
2284 os << "\\end{tabular}";
2286 os << "\n\\end{sideways}";
2294 int Tabular::docbookRow(odocstream & os, row_type row,
2295 OutputParams const & runparams) const
2298 idx_type cell = getFirstCellInRow(row);
2301 for (col_type j = 0; j < columnCount(); ++j) {
2302 if (isPartOfMultiColumn(row, j))
2305 os << "<entry align=\"";
2306 switch (getAlignment(cell)) {
2307 case LYX_ALIGN_LEFT:
2310 case LYX_ALIGN_RIGHT:
2318 os << "\" valign=\"";
2319 switch (getVAlignment(cell)) {
2320 case LYX_VALIGN_TOP:
2323 case LYX_VALIGN_BOTTOM:
2326 case LYX_VALIGN_MIDDLE:
2331 if (isMultiColumn(cell)) {
2332 os << " namest=\"col" << j << "\" ";
2333 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2337 ret += cellInset(cell)->docbook(os, runparams);
2346 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2350 //+---------------------------------------------------------------------
2351 //+ first the opening preamble +
2352 //+---------------------------------------------------------------------
2354 os << "<tgroup cols=\"" << columnCount()
2355 << "\" colsep=\"1\" rowsep=\"1\">\n";
2357 for (col_type i = 0; i < columnCount(); ++i) {
2358 os << "<colspec colname=\"col" << i << "\" align=\"";
2359 switch (column_info[i].alignment) {
2360 case LYX_ALIGN_LEFT:
2363 case LYX_ALIGN_RIGHT:
2371 if (runparams.flavor == OutputParams::XML)
2377 //+---------------------------------------------------------------------
2378 //+ Long Tabular case +
2379 //+---------------------------------------------------------------------
2381 // output header info
2382 if (haveLTHead() || haveLTFirstHead()) {
2385 for (row_type i = 0; i < rowCount(); ++i) {
2386 if (row_info[i].endhead || row_info[i].endfirsthead) {
2387 ret += docbookRow(os, i, runparams);
2393 // output footer info
2394 if (haveLTFoot() || haveLTLastFoot()) {
2397 for (row_type i = 0; i < rowCount(); ++i) {
2398 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2399 ret += docbookRow(os, i, runparams);
2406 //+---------------------------------------------------------------------
2407 //+ the single row and columns (cells) +
2408 //+---------------------------------------------------------------------
2412 for (row_type i = 0; i < rowCount(); ++i) {
2413 if (isValidRow(i)) {
2414 ret += docbookRow(os, i, runparams);
2419 //+---------------------------------------------------------------------
2420 //+ the closing of the tabular +
2421 //+---------------------------------------------------------------------
2430 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2431 vector<unsigned int> const & clen) const
2433 idx_type const fcell = getFirstCellInRow(row);
2434 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2437 for (idx_type i = fcell; i < n; ++i) {
2447 for (idx_type i = fcell; i < n; ++i) {
2458 col_type column = cellColumn(i);
2459 int len = clen[column];
2460 while (column < columnCount() - 1
2461 && isPartOfMultiColumn(row, ++column))
2462 len += clen[column] + 4;
2463 os << docstring(len, ch);
2478 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2479 vector<unsigned int> const & clen) const
2481 idx_type const fcell = getFirstCellInRow(row);
2482 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2485 for (idx_type i = fcell; i < n; ++i) {
2486 if (bottomLine(i)) {
2495 for (idx_type i = fcell; i < n; ++i) {
2496 if (bottomLine(i)) {
2506 col_type column = cellColumn(i);
2507 int len = clen[column];
2508 while (column < columnCount() -1
2509 && isPartOfMultiColumn(row, ++column))
2510 len += clen[column] + 4;
2511 os << docstring(len, ch);
2512 if (bottomLine(i)) {
2526 void Tabular::plaintextPrintCell(odocstream & os,
2527 OutputParams const & runparams,
2528 idx_type cell, row_type row, col_type column,
2529 vector<unsigned int> const & clen,
2530 bool onlydata) const
2532 odocstringstream sstr;
2533 cellInset(cell)->plaintext(sstr, runparams);
2545 unsigned int len1 = sstr.str().length();
2546 unsigned int len2 = clen[column];
2547 while (column < columnCount() -1
2548 && isPartOfMultiColumn(row, ++column))
2549 len2 += clen[column] + 4;
2552 switch (getAlignment(cell)) {
2554 case LYX_ALIGN_LEFT:
2557 case LYX_ALIGN_RIGHT:
2561 case LYX_ALIGN_CENTER:
2567 os << docstring(len1, ' ') << sstr.str()
2568 << docstring(len2, ' ');
2570 if (rightLine(cell))
2577 void Tabular::plaintext(odocstream & os,
2578 OutputParams const & runparams, int const depth,
2579 bool onlydata, char_type delim) const
2581 // first calculate the width of the single columns
2582 vector<unsigned int> clen(columnCount());
2585 // first all non (real) multicolumn cells!
2586 for (col_type j = 0; j < columnCount(); ++j) {
2588 for (row_type i = 0; i < rowCount(); ++i) {
2589 idx_type cell = cellIndex(i, j);
2590 if (isMultiColumnReal(cell))
2592 odocstringstream sstr;
2593 cellInset(cell)->plaintext(sstr, runparams);
2594 if (clen[j] < sstr.str().length())
2595 clen[j] = sstr.str().length();
2598 // then all (real) multicolumn cells!
2599 for (col_type j = 0; j < columnCount(); ++j) {
2600 for (row_type i = 0; i < rowCount(); ++i) {
2601 idx_type cell = cellIndex(i, j);
2602 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2604 odocstringstream sstr;
2605 cellInset(cell)->plaintext(sstr, runparams);
2606 int len = int(sstr.str().length());
2607 idx_type const n = columnSpan(cell);
2608 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2610 if (len > int(clen[j + n - 1]))
2611 clen[j + n - 1] = len;
2616 for (row_type i = 0; i < rowCount(); ++i) {
2617 if (!onlydata && plaintextTopHLine(os, i, clen))
2618 os << docstring(depth * 2, ' ');
2619 for (col_type j = 0; j < columnCount(); ++j) {
2620 if (isPartOfMultiColumn(i, j))
2622 if (onlydata && j > 0)
2623 // we don't use operator<< for single UCS4 character.
2624 // see explanation in docstream.h
2626 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2631 os << docstring(depth * 2, ' ');
2632 if (plaintextBottomHLine(os, i, clen))
2633 os << docstring(depth * 2, ' ');
2639 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2641 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2645 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2646 col_type column) const
2648 return cell_info[row][column].inset;
2652 void Tabular::setCellInset(row_type row, col_type column,
2653 shared_ptr<InsetTableCell> ins) const
2655 CellData & cd = cell_info[row][column];
2657 // reset the InsetTableCell's pointers
2658 ins->setCellData(&cd);
2659 ins->setTabular(this);
2664 Tabular::cellFromInset(Inset const * inset) const
2666 // is this inset part of the tabular?
2668 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2669 BOOST_ASSERT(false);
2672 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2673 if (cellInset(cell).get() == inset) {
2674 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2675 << "cell=" << cell);
2679 // We should have found a cell at this point
2680 lyxerr << "Tabular::cellFromInset: Cell of inset "
2681 << inset << " not found!" << endl;
2682 BOOST_ASSERT(false);
2688 void Tabular::validate(LaTeXFeatures & features) const
2690 features.require("NeedTabularnewline");
2692 features.require("booktabs");
2693 if (isLongTabular())
2694 features.require("longtable");
2696 features.require("rotating");
2697 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2698 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2699 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2700 features.require("array");
2701 cellInset(cell)->validate(features);
2706 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2708 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2709 ParagraphList::const_iterator cit = parlist.begin();
2710 ParagraphList::const_iterator end = parlist.end();
2712 for (; cit != end; ++cit)
2713 for (int i = 0; i < cit->size(); ++i)
2714 if (cit->isNewline(i))
2721 /////////////////////////////////////////////////////////////////////
2725 /////////////////////////////////////////////////////////////////////
2727 InsetTableCell::InsetTableCell(Buffer const & buf,
2728 Tabular::CellData const * cell, Tabular const * table)
2729 : InsetText(buf), cell_data_(cell), table_(table)
2733 bool InsetTableCell::forceEmptyLayout(idx_type) const
2735 BOOST_ASSERT(table_);
2736 BOOST_ASSERT(cell_data_);
2737 return table_->getPWidth(cell_data_->cellno).zero();
2740 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2742 BOOST_ASSERT(table_);
2743 BOOST_ASSERT(cell_data_);
2744 return !table_->getPWidth(cell_data_->cellno).zero();
2747 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2748 FuncStatus & status) const
2751 switch (cmd.action) {
2753 enabled = !forceEmptyLayout();
2755 case LFUN_LAYOUT_PARAGRAPH:
2756 enabled = allowParagraphCustomization();
2759 return InsetText::getStatus(cur, cmd, status);
2761 status.enabled(enabled);
2765 /////////////////////////////////////////////////////////////////////
2769 /////////////////////////////////////////////////////////////////////
2771 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2773 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2774 rowselect_(false), colselect_(false)
2776 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2780 InsetTabular::InsetTabular(InsetTabular const & tab)
2781 : Inset(tab), tabular(tab.tabular), scx_(0)
2783 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2787 InsetTabular::~InsetTabular()
2789 hideDialogs("tabular", this);
2793 bool InsetTabular::insetAllowed(InsetCode code) const
2795 if (code == MATHMACRO_CODE)
2802 void InsetTabular::write(ostream & os) const
2804 os << "Tabular" << endl;
2809 void InsetTabular::read(Lexer & lex)
2811 bool const old_format = (lex.getString() == "\\LyXTable");
2819 string token = lex.getString();
2820 while (lex && token != "\\end_inset") {
2822 token = lex.getString();
2825 lex.printError("Missing \\end_inset at this point. "
2831 int InsetTabular::rowFromY(Cursor & cur, int y) const
2833 // top y coordinate of tabular
2834 int h = yo(cur.bv()) - tabular.rowAscent(0);
2835 size_t nrows = tabular.rowCount();
2837 for (;r < nrows && y > h; ++r) {
2838 h += tabular.rowAscent(r);
2839 h += tabular.rowDescent(r);
2840 h += tabular.getAdditionalHeight(r);
2846 int InsetTabular::columnFromX(Cursor & cur, int x) const
2848 // left x coordinate of tabular
2849 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2850 size_t ncols = tabular.columnCount();
2852 for (;c < ncols && x > w; ++c) {
2853 w += tabular.columnWidth(c);
2859 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2861 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2862 // mi.base.textwidth << "\n";
2864 lyxerr << "InsetTabular::metrics: need bv" << endl;
2865 BOOST_ASSERT(false);
2869 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2872 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2873 if (tabular.isPartOfMultiColumn(i, j))
2874 // Multicolumn cell, but not first one
2879 if (tabular.cell_info[i][j].multicolumn ==
2880 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2881 p_width = tabular.cellInfo(cell).p_width;
2883 p_width = tabular.column_info[j].p_width;
2884 if (!p_width.zero())
2885 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2886 tabular.cellInset(cell)->metrics(m, dim);
2887 if (!p_width.zero())
2888 dim.wid = m.base.textwidth;
2889 tabular.setCellWidth(cell, dim.wid);
2890 maxAsc = max(maxAsc, dim.asc);
2891 maxDesc = max(maxDesc, dim.des);
2894 int const top_space = tabular.row_info[i].top_space_default ?
2895 default_line_space :
2896 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2897 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2898 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2899 default_line_space :
2900 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2901 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2903 tabular.updateColumnWidths();
2904 dim.asc = tabular.rowAscent(0);
2905 dim.des = tabular.height() - dim.asc;
2906 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2910 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2912 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2913 BufferView * bv = pi.base.bv;
2915 // FIXME: As the full backrgound is painted in drawSelection(),
2916 // we have no choice but to do a full repaint for the Text cells.
2917 pi.full_repaint = true;
2919 resetPos(bv->cursor());
2922 x += ADD_TO_TABULAR_WIDTH;
2924 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2927 first_visible_cell = Tabular::npos;
2928 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2930 int const a = tabular.rowAscent(i);
2931 int const d = tabular.rowDescent(i);
2932 idx = tabular.cellIndex(i, 0);
2933 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2934 if (tabular.isPartOfMultiColumn(i, j))
2936 if (first_visible_cell == Tabular::npos)
2937 first_visible_cell = idx;
2939 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2940 // Cache the Inset position.
2941 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2942 if (nx + tabular.columnWidth(idx) < 0
2943 || nx > bv->workWidth()
2945 || y - a > bv->workHeight()) {
2946 pi.pain.setDrawingEnabled(false);
2947 cell(idx)->draw(pi, cx, y);
2948 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2949 pi.pain.setDrawingEnabled(original_drawing_state);
2951 cell(idx)->draw(pi, cx, y);
2952 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2954 nx += tabular.columnWidth(idx);
2958 if (i + 1 < tabular.rowCount())
2959 y += d + tabular.rowAscent(i + 1) +
2960 tabular.getAdditionalHeight(i + 1);
2965 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2967 Cursor & cur = pi.base.bv->cursor();
2969 x += scx_ + ADD_TO_TABULAR_WIDTH;
2971 // FIXME: it is wrong to completely paint the background
2972 // if we want to do single row painting.
2974 // Paint background of current tabular
2975 int const w = tabular.width();
2976 int const h = tabular.height();
2977 int yy = y - tabular.rowAscent(0);
2978 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2980 if (!cur.selection())
2982 if (&cur.inset() != this)
2988 if (tablemode(cur)) {
2991 getSelection(cur, rs, re, cs, ce);
2992 y -= tabular.rowAscent(0);
2993 for (row_type j = 0; j < tabular.rowCount(); ++j) {
2994 int const a = tabular.rowAscent(j);
2995 int const h = a + tabular.rowDescent(j);
2997 y += tabular.getAdditionalHeight(j);
2998 for (col_type i = 0; i < tabular.columnCount(); ++i) {
2999 if (tabular.isPartOfMultiColumn(j, i))
3001 idx_type const cell =
3002 tabular.cellIndex(j, i);
3003 int const w = tabular.columnWidth(cell);
3004 if (i >= cs && i <= ce && j >= rs && j <= re)
3005 pi.pain.fillRectangle(xx, y, w, h,
3013 x += cellXPos(cur.idx());
3014 x += tabular.getBeginningOfTextInCell(cur.idx());
3015 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3020 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3021 row_type row, idx_type cell, bool erased) const
3023 int x2 = x + tabular.columnWidth(cell);
3024 bool on_off = false;
3025 ColorCode col = Color_tabularline;
3026 ColorCode onoffcol = Color_tabularonoffline;
3029 col = Color_deletedtext;
3030 onoffcol = Color_deletedtext;
3033 if (!tabular.topAlreadyDrawn(cell)) {
3034 on_off = !tabular.topLine(cell);
3035 pain.line(x, y - tabular.rowAscent(row),
3036 x2, y - tabular.rowAscent(row),
3037 on_off ? onoffcol : col,
3038 on_off ? Painter::line_onoffdash : Painter::line_solid);
3040 on_off = !tabular.bottomLine(cell);
3041 pain.line(x, y + tabular.rowDescent(row),
3042 x2, y + tabular.rowDescent(row),
3043 on_off ? onoffcol : col,
3044 on_off ? Painter::line_onoffdash : Painter::line_solid);
3045 if (!tabular.leftAlreadyDrawn(cell)) {
3046 on_off = !tabular.leftLine(cell);
3047 pain.line(x, y - tabular.rowAscent(row),
3048 x, y + tabular.rowDescent(row),
3049 on_off ? onoffcol : col,
3050 on_off ? Painter::line_onoffdash : Painter::line_solid);
3052 on_off = !tabular.rightLine(cell);
3053 pain.line(x2 - tabular.getAdditionalWidth(cell),
3054 y - tabular.rowAscent(row),
3055 x2 - tabular.getAdditionalWidth(cell),
3056 y + tabular.rowDescent(row),
3057 on_off ? onoffcol : col,
3058 on_off ? Painter::line_onoffdash : Painter::line_solid);
3062 docstring InsetTabular::editMessage() const
3064 return _("Opened table");
3068 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3070 //lyxerr << "InsetTabular::edit: " << this << endl;
3072 cur.selection() = false;
3075 if (isRightToLeft(cur))
3076 cur.idx() = tabular.getLastCellInRow(0);
3082 if (isRightToLeft(cur))
3083 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3085 cur.idx() = tabular.cellCount() - 1;
3087 cur.pos() = cur.lastpos(); // FIXME crude guess
3089 // FIXME: this accesses the position cache before it is initialized
3091 //cur.bv().fitCursor();
3095 void InsetTabular::updateLabels(ParIterator const & it)
3097 // In a longtable, tell captions what the current float is
3098 Counters & cnts = buffer().params().documentClass().counters();
3099 string const saveflt = cnts.current_float();
3100 if (tabular.isLongTabular())
3101 cnts.current_float("table");
3103 ParIterator it2 = it;
3105 size_t const end = it2.nargs();
3106 for ( ; it2.idx() < end; it2.top().forwardIdx())
3107 lyx::updateLabels(buffer(), it2);
3110 if (tabular.isLongTabular())
3111 cnts.current_float(saveflt);
3115 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3117 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3118 << "\n cur:" << cur);
3119 CursorSlice sl = cur.top();
3120 Cursor & bvcur = cur.bv().cursor();
3122 switch (cmd.action) {
3124 case LFUN_MOUSE_PRESS: {
3125 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3127 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3128 || cmd.x > xo(cur.bv()) + tabular.width()) {
3129 row_type r = rowFromY(cur, cmd.y);
3130 cur.idx() = tabular.getFirstCellInRow(r);
3133 cur.idx() = tabular.getLastCellInRow(r);
3134 cur.pos() = cur.lastpos();
3135 cur.selection() = true;
3141 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3142 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3143 || cmd.y > y0 + tabular.height()) {
3144 col_type c = columnFromX(cur, cmd.x);
3145 cur.idx() = tabular.cellIndex(0, c);
3148 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3149 cur.pos() = cur.lastpos();
3150 cur.selection() = true;
3155 // do not reset cursor/selection if we have selected
3156 // some cells (bug 2715).
3157 if (cmd.button() == mouse_button::button3
3158 && &bvcur.selBegin().inset() == this
3159 && tablemode(bvcur))
3162 // Let InsetTableCell do it
3163 cell(cur.idx())->dispatch(cur, cmd);
3166 case LFUN_MOUSE_MOTION:
3167 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3168 if (cmd.button() == mouse_button::button1) {
3169 // only accept motions to places not deeper nested than the real anchor
3170 if (!bvcur.anchor_.hasPart(cur)) {
3174 // select (additional) row
3176 row_type r = rowFromY(cur, cmd.y);
3177 cur.idx() = tabular.getLastCellInRow(r);
3178 bvcur.setCursor(cur);
3179 bvcur.selection() = true;
3182 // select (additional) column
3184 col_type c = columnFromX(cur, cmd.x);
3185 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3186 bvcur.setCursor(cur);
3187 bvcur.selection() = true;
3190 // only update if selection changes
3191 if (bvcur.idx() == cur.idx() &&
3192 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3194 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3195 bvcur.setCursor(cur);
3196 bvcur.selection() = true;
3200 case LFUN_MOUSE_RELEASE:
3205 case LFUN_CELL_BACKWARD:
3207 cur.selection() = false;
3210 case LFUN_CELL_FORWARD:
3212 cur.selection() = false;
3214 case LFUN_CHAR_FORWARD_SELECT:
3215 case LFUN_CHAR_FORWARD:
3216 cell(cur.idx())->dispatch(cur, cmd);
3217 if (!cur.result().dispatched()) {
3219 if (sl == cur.top())
3220 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3226 case LFUN_CHAR_BACKWARD_SELECT:
3227 case LFUN_CHAR_BACKWARD:
3228 cell(cur.idx())->dispatch(cur, cmd);
3229 if (!cur.result().dispatched()) {
3231 if (sl == cur.top())
3232 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3238 case LFUN_CHAR_RIGHT_SELECT:
3239 case LFUN_CHAR_RIGHT:
3240 //FIXME: for visual cursor, really move right
3241 if (isRightToLeft(cur))
3242 lyx::dispatch(FuncRequest(
3243 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3244 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3246 lyx::dispatch(FuncRequest(
3247 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3248 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3251 case LFUN_CHAR_LEFT_SELECT:
3252 case LFUN_CHAR_LEFT:
3253 //FIXME: for visual cursor, really move left
3254 if (isRightToLeft(cur))
3255 lyx::dispatch(FuncRequest(
3256 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3257 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3259 lyx::dispatch(FuncRequest(
3260 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3261 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3264 case LFUN_DOWN_SELECT:
3266 cell(cur.idx())->dispatch(cur, cmd);
3267 cur.dispatched(); // override the cell's decision
3268 if (sl == cur.top())
3269 // if our Text didn't do anything to the cursor
3270 // then we try to put the cursor into the cell below
3271 // setting also the right targetX.
3272 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3273 cur.idx() = tabular.cellBelow(cur.idx());
3275 TextMetrics const & tm =
3276 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3277 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3279 if (sl == cur.top()) {
3280 // we trick it to go to forward after leaving the
3282 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3287 case LFUN_UP_SELECT:
3289 cell(cur.idx())->dispatch(cur, cmd);
3290 cur.dispatched(); // override the cell's decision
3291 if (sl == cur.top())
3292 // if our Text didn't do anything to the cursor
3293 // then we try to put the cursor into the cell above
3294 // setting also the right targetX.
3295 if (tabular.cellRow(cur.idx()) != 0) {
3296 cur.idx() = tabular.cellAbove(cur.idx());
3297 cur.pit() = cur.lastpit();
3298 Text const * text = cell(cur.idx())->getText(0);
3299 TextMetrics const & tm = cur.bv().textMetrics(text);
3300 ParagraphMetrics const & pm =
3301 tm.parMetrics(cur.lastpit());
3302 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3304 if (sl == cur.top()) {
3305 cmd = FuncRequest(LFUN_UP);
3310 // case LFUN_SCREEN_DOWN: {
3311 // //if (hasSelection())
3312 // // cur.selection() = false;
3313 // col_type const col = tabular.cellColumn(cur.idx());
3314 // int const t = cur.bv().top_y() + cur.bv().height();
3315 // if (t < yo() + tabular.getHeightOfTabular()) {
3316 // cur.bv().scrollDocView(t);
3317 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3319 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3326 // case LFUN_SCREEN_UP: {
3327 // //if (hasSelection())
3328 // // cur.selection() = false;
3329 // col_type const col = tabular.cellColumn(cur.idx());
3330 // int const t = cur.bv().top_y() + cur.bv().height();
3332 // cur.bv().scrollDocView(t);
3336 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3340 // cur.par() = cur.lastpar();
3341 // cur.pos() = cur.lastpos();
3345 case LFUN_LAYOUT_TABULAR:
3346 cur.bv().showDialog("tabular", params2string(*this), this);
3349 case LFUN_INSET_DIALOG_UPDATE:
3350 cur.bv().updateDialog("tabular", params2string(*this));
3353 case LFUN_TABULAR_FEATURE:
3354 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3358 // insert file functions
3359 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3360 case LFUN_FILE_INSERT_PLAINTEXT: {
3362 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3363 FileName(to_utf8(cmd.argument())));
3366 cur.recordUndoInset(INSERT_UNDO);
3367 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3368 // content has been replaced,
3369 // so cursor might be invalid
3370 cur.pos() = cur.lastpos();
3371 cur.pit() = cur.lastpit();
3372 bvcur.setCursor(cur);
3379 if (tablemode(cur)) {
3380 if (copySelection(cur)) {
3381 cur.recordUndoInset(DELETE_UNDO);
3386 cell(cur.idx())->dispatch(cur, cmd);
3389 case LFUN_CHAR_DELETE_BACKWARD:
3390 case LFUN_CHAR_DELETE_FORWARD:
3391 if (tablemode(cur)) {
3392 cur.recordUndoInset(DELETE_UNDO);
3396 cell(cur.idx())->dispatch(cur, cmd);
3400 if (!cur.selection())
3402 if (tablemode(cur)) {
3406 cell(cur.idx())->dispatch(cur, cmd);
3409 case LFUN_CLIPBOARD_PASTE:
3410 case LFUN_PRIMARY_SELECTION_PASTE: {
3411 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3412 theClipboard().getAsText() :
3413 theSelection().get();
3416 // pass to InsertPlaintextString, but
3417 // only if we have multi-cell content
3418 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3419 cur.recordUndoInset(INSERT_UNDO);
3420 if (insertPlaintextString(cur.bv(), clip, false)) {
3421 // content has been replaced,
3422 // so cursor might be invalid
3423 cur.pos() = cur.lastpos();
3424 cur.pit() = cur.lastpit();
3425 bvcur.setCursor(cur);
3429 // Let the cell handle normal text
3430 cell(cur.idx())->dispatch(cur, cmd);
3435 if (tabularStackDirty() && theClipboard().isInternal() ||
3436 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3437 cur.recordUndoInset(INSERT_UNDO);
3438 pasteClipboard(cur);
3441 cell(cur.idx())->dispatch(cur, cmd);
3444 case LFUN_FONT_EMPH:
3445 case LFUN_FONT_BOLD:
3446 case LFUN_FONT_ROMAN:
3447 case LFUN_FONT_NOUN:
3448 case LFUN_FONT_ITAL:
3449 case LFUN_FONT_FRAK:
3450 case LFUN_FONT_TYPEWRITER:
3451 case LFUN_FONT_SANS:
3452 case LFUN_FONT_FREE_APPLY:
3453 case LFUN_FONT_FREE_UPDATE:
3454 case LFUN_FONT_SIZE:
3455 case LFUN_FONT_UNDERLINE:
3457 case LFUN_WORD_CAPITALIZE:
3458 case LFUN_WORD_UPCASE:
3459 case LFUN_WORD_LOWCASE:
3460 case LFUN_CHARS_TRANSPOSE:
3461 if (tablemode(cur)) {
3464 getSelection(cur, rs, re, cs, ce);
3465 Cursor tmpcur = cur;
3466 for (row_type i = rs; i <= re; ++i) {
3467 for (col_type j = cs; j <= ce; ++j) {
3468 // cursor follows cell:
3469 tmpcur.idx() = tabular.cellIndex(i, j);
3470 // select this cell only:
3473 tmpcur.resetAnchor();
3474 tmpcur.pit() = tmpcur.lastpit();
3475 tmpcur.pos() = tmpcur.top().lastpos();
3476 tmpcur.setCursor(tmpcur);
3477 tmpcur.setSelection();
3478 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3483 cell(cur.idx())->dispatch(cur, cmd);
3487 // we try to handle this event in the insets dispatch function.
3488 cell(cur.idx())->dispatch(cur, cmd);
3494 // function sets an object as defined in func_status.h:
3495 // states OK, Unknown, Disabled, On, Off.
3496 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3497 FuncStatus & status) const
3499 switch (cmd.action) {
3500 case LFUN_TABULAR_FEATURE: {
3501 int action = Tabular::LAST_ACTION;
3503 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3504 string const tmp = tabularFeature[i].feature;
3505 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3506 action = tabularFeature[i].action;
3510 if (action == Tabular::LAST_ACTION) {
3512 status.unknown(true);
3516 string const argument
3517 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3519 row_type sel_row_start = 0;
3520 row_type sel_row_end = 0;
3521 col_type sel_col_start = 0;
3522 col_type sel_col_end = 0;
3523 Tabular::ltType dummyltt;
3526 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3529 case Tabular::SET_PWIDTH:
3530 case Tabular::SET_MPWIDTH:
3531 case Tabular::SET_SPECIAL_COLUMN:
3532 case Tabular::SET_SPECIAL_MULTI:
3533 case Tabular::APPEND_ROW:
3534 case Tabular::APPEND_COLUMN:
3535 case Tabular::DELETE_ROW:
3536 case Tabular::DELETE_COLUMN:
3537 case Tabular::COPY_ROW:
3538 case Tabular::COPY_COLUMN:
3539 case Tabular::SET_ALL_LINES:
3540 case Tabular::UNSET_ALL_LINES:
3541 case Tabular::SET_TOP_SPACE:
3542 case Tabular::SET_BOTTOM_SPACE:
3543 case Tabular::SET_INTERLINE_SPACE:
3544 case Tabular::SET_BORDER_LINES:
3548 case Tabular::MULTICOLUMN:
3549 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3552 case Tabular::TOGGLE_LINE_TOP:
3553 status.setOnOff(tabular.topLine(cur.idx()));
3556 case Tabular::TOGGLE_LINE_BOTTOM:
3557 status.setOnOff(tabular.bottomLine(cur.idx()));
3560 case Tabular::TOGGLE_LINE_LEFT:
3561 status.setOnOff(tabular.leftLine(cur.idx()));
3564 case Tabular::TOGGLE_LINE_RIGHT:
3565 status.setOnOff(tabular.rightLine(cur.idx()));
3568 case Tabular::M_ALIGN_LEFT:
3570 case Tabular::ALIGN_LEFT:
3571 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3574 case Tabular::M_ALIGN_RIGHT:
3576 case Tabular::ALIGN_RIGHT:
3577 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3580 case Tabular::M_ALIGN_CENTER:
3582 case Tabular::ALIGN_CENTER:
3583 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3586 case Tabular::ALIGN_BLOCK:
3587 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3588 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3591 case Tabular::M_VALIGN_TOP:
3593 case Tabular::VALIGN_TOP:
3595 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3598 case Tabular::M_VALIGN_BOTTOM:
3600 case Tabular::VALIGN_BOTTOM:
3602 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3605 case Tabular::M_VALIGN_MIDDLE:
3607 case Tabular::VALIGN_MIDDLE:
3609 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3612 case Tabular::SET_LONGTABULAR:
3613 status.setOnOff(tabular.isLongTabular());
3616 case Tabular::UNSET_LONGTABULAR:
3617 status.setOnOff(!tabular.isLongTabular());
3620 case Tabular::TOGGLE_ROTATE_TABULAR:
3621 case Tabular::SET_ROTATE_TABULAR:
3622 status.setOnOff(tabular.getRotateTabular());
3625 case Tabular::UNSET_ROTATE_TABULAR:
3626 status.setOnOff(!tabular.getRotateTabular());
3629 case Tabular::TOGGLE_ROTATE_CELL:
3630 case Tabular::SET_ROTATE_CELL:
3631 status.setOnOff(!oneCellHasRotationState(false,
3632 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3635 case Tabular::UNSET_ROTATE_CELL:
3636 status.setOnOff(!oneCellHasRotationState(true,
3637 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3640 case Tabular::SET_USEBOX:
3641 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3644 case Tabular::SET_LTFIRSTHEAD:
3645 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3648 case Tabular::UNSET_LTFIRSTHEAD:
3649 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3652 case Tabular::SET_LTHEAD:
3653 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3656 case Tabular::UNSET_LTHEAD:
3657 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3660 case Tabular::SET_LTFOOT:
3661 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3664 case Tabular::UNSET_LTFOOT:
3665 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3668 case Tabular::SET_LTLASTFOOT:
3669 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3672 case Tabular::UNSET_LTLASTFOOT:
3673 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3676 case Tabular::SET_LTNEWPAGE:
3677 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3680 case Tabular::SET_BOOKTABS:
3681 status.setOnOff(tabular.useBookTabs());
3684 case Tabular::UNSET_BOOKTABS:
3685 status.setOnOff(!tabular.useBookTabs());
3690 status.enabled(false);
3696 // These are only enabled inside tabular
3697 case LFUN_CELL_BACKWARD:
3698 case LFUN_CELL_FORWARD:
3699 status.enabled(true);
3702 // disable these with multiple cells selected
3703 case LFUN_INSET_INSERT:
3704 case LFUN_TABULAR_INSERT:
3705 case LFUN_FLEX_INSERT:
3706 case LFUN_FLOAT_INSERT:
3707 case LFUN_FLOAT_WIDE_INSERT:
3708 case LFUN_FOOTNOTE_INSERT:
3709 case LFUN_MARGINALNOTE_INSERT:
3710 case LFUN_MATH_INSERT:
3711 case LFUN_MATH_MODE:
3712 case LFUN_MATH_MUTATE:
3713 case LFUN_MATH_DISPLAY:
3714 case LFUN_NOTE_INSERT:
3715 case LFUN_OPTIONAL_INSERT:
3716 case LFUN_BOX_INSERT:
3717 case LFUN_BRANCH_INSERT:
3718 case LFUN_WRAP_INSERT:
3719 case LFUN_ERT_INSERT: {
3720 if (tablemode(cur)) {
3721 status.enabled(false);
3724 return cell(cur.idx())->getStatus(cur, cmd, status);
3727 // disable in non-fixed-width cells
3728 case LFUN_NEWLINE_INSERT:
3729 case LFUN_BREAK_PARAGRAPH:
3730 case LFUN_BREAK_PARAGRAPH_SKIP: {
3731 if (tabular.getPWidth(cur.idx()).zero()) {
3732 status.enabled(false);
3735 return cell(cur.idx())->getStatus(cur, cmd, status);
3739 if (tabularStackDirty() && theClipboard().isInternal()) {
3740 status.enabled(true);
3743 return cell(cur.idx())->getStatus(cur, cmd, status);
3745 case LFUN_INSET_MODIFY:
3746 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3747 status.enabled(true);
3753 // we try to handle this event in the insets dispatch function.
3754 return cell(cur.idx())->getStatus(cur, cmd, status);
3759 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3761 return tabular.latex(os, runparams);
3765 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3767 os << '\n'; // output table on a new line
3768 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3769 tabular.plaintext(os, runparams, dp, false, 0);
3770 return PLAINTEXT_NEWLINE;
3774 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3779 // FIXME: Why not pass a proper DocIterator here?
3781 // if the table is inside a float it doesn't need the informaltable
3782 // wrapper. Search for it.
3783 for (master = owner(); master; master = master->owner())
3784 if (master->lyxCode() == FLOAT_CODE)
3789 os << "<informaltable>";
3792 ret += tabular.docbook(os, runparams);
3794 os << "</informaltable>";
3801 void InsetTabular::validate(LaTeXFeatures & features) const
3803 tabular.validate(features);
3807 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3809 return tabular.cellInset(idx);
3813 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3815 return tabular.cellInset(idx);
3819 void InsetTabular::cursorPos(BufferView const & bv,
3820 CursorSlice const & sl, bool boundary, int & x, int & y) const
3822 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3824 // y offset correction
3825 int const row = tabular.cellRow(sl.idx());
3826 for (int i = 0; i <= row; ++i) {
3828 y += tabular.rowAscent(i);
3829 y += tabular.getAdditionalHeight(i);
3832 y += tabular.rowDescent(i);
3835 // x offset correction
3836 int const col = tabular.cellColumn(sl.idx());
3837 int idx = tabular.cellIndex(row, 0);
3838 for (int j = 0; j < col; ++j) {
3839 if (tabular.isPartOfMultiColumn(row, j))
3841 x += tabular.columnWidth(idx);
3844 x += tabular.getBeginningOfTextInCell(idx);
3845 x += ADD_TO_TABULAR_WIDTH;
3850 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3854 Inset const & inset = *tabular.cellInset(cell);
3855 Point o = bv.coordCache().getInsets().xy(&inset);
3856 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3857 int const xend = xbeg + tabular.columnWidth(cell);
3858 row_type const row = tabular.cellRow(cell);
3859 int const ybeg = o.y_ - tabular.rowAscent(row) -
3860 tabular.getAdditionalHeight(row);
3861 int const yend = o.y_ + tabular.rowDescent(row);
3873 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3874 // << " ybeg=" << ybeg << " yend=" << yend
3875 // << " xx=" << xx << " yy=" << yy
3876 // << " dist=" << xx + yy << endl;
3881 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3883 //lyxerr << "InsetTabular::editXY: " << this << endl;
3884 cur.selection() = false;
3886 cur.idx() = getNearestCell(cur.bv(), x, y);
3888 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3892 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3894 cur.idx() = getNearestCell(cur.bv(), x, y);
3895 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3899 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3901 idx_type idx_min = 0;
3902 int dist_min = numeric_limits<int>::max();
3903 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3904 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3905 int const d = dist(bv, i, x, y);
3916 int InsetTabular::cellXPos(idx_type const cell) const
3920 for (; !tabular.isFirstCellInRow(c); --c)
3923 for (; c < cell; ++c)
3924 lx += tabular.columnWidth(c);
3930 void InsetTabular::resetPos(Cursor & cur) const
3932 BufferView & bv = cur.bv();
3933 int const maxwidth = bv.workWidth();
3935 if (&cur.inset() != this) {
3939 int const X2 = maxwidth;
3940 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3941 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3942 int const x2 = x1 + tabular.columnWidth(cur.idx());
3945 scx_ = X1 + 20 - x1;
3947 scx_ = X2 - 20 - x2;
3952 cur.updateFlags(Update::Force | Update::FitCursor);
3956 void InsetTabular::moveNextCell(Cursor & cur)
3958 if (isRightToLeft(cur)) {
3959 if (tabular.isFirstCellInRow(cur.idx())) {
3960 row_type const row = tabular.cellRow(cur.idx());
3961 if (row == tabular.rowCount() - 1)
3963 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3970 if (tabular.isLastCell(cur.idx()))
3980 void InsetTabular::movePrevCell(Cursor & cur)
3982 if (isRightToLeft(cur)) {
3983 if (tabular.isLastCellInRow(cur.idx())) {
3984 row_type const row = tabular.cellRow(cur.idx());
3987 cur.idx() = tabular.getFirstCellInRow(row);
3988 cur.idx() = tabular.cellAbove(cur.idx());
3990 if (tabular.isLastCell(cur.idx()))
3995 if (cur.idx() == 0) // first cell
3999 cur.pit() = cur.lastpit();
4000 cur.pos() = cur.lastpos();
4002 // FIXME: this accesses the position cache before it is initialized
4007 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4009 Tabular::Feature action = Tabular::LAST_ACTION;
4012 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4013 string const tmp = tabularFeature[i].feature;
4015 if (tmp == what.substr(0, tmp.length())) {
4016 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4017 //tabularFeatures[i].feature.length()))
4018 action = tabularFeature[i].action;
4022 if (action == Tabular::LAST_ACTION)
4026 ltrim(what.substr(tabularFeature[i].feature.length()));
4027 tabularFeatures(cur, action, val);
4032 static void checkLongtableSpecial(Tabular::ltType & ltt,
4033 string const & special, bool & flag)
4035 if (special == "dl_above") {
4038 } else if (special == "dl_below") {
4039 ltt.bottomDL = flag;
4041 } else if (special == "empty") {
4050 bool InsetTabular::oneCellHasRotationState(bool rotated,
4051 row_type row_start, row_type row_end,
4052 col_type col_start, col_type col_end) const {
4054 for (row_type i = row_start; i <= row_end; ++i) {
4055 for (col_type j = col_start; j <= col_end; ++j) {
4056 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4065 void InsetTabular::tabularFeatures(Cursor & cur,
4066 Tabular::Feature feature, string const & value)
4068 col_type sel_col_start;
4069 col_type sel_col_end;
4070 row_type sel_row_start;
4071 row_type sel_row_end;
4072 bool setLines = false;
4073 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4074 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4078 case Tabular::M_ALIGN_LEFT:
4079 case Tabular::ALIGN_LEFT:
4080 setAlign = LYX_ALIGN_LEFT;
4083 case Tabular::M_ALIGN_RIGHT:
4084 case Tabular::ALIGN_RIGHT:
4085 setAlign = LYX_ALIGN_RIGHT;
4088 case Tabular::M_ALIGN_CENTER:
4089 case Tabular::ALIGN_CENTER:
4090 setAlign = LYX_ALIGN_CENTER;
4093 case Tabular::ALIGN_BLOCK:
4094 setAlign = LYX_ALIGN_BLOCK;
4097 case Tabular::M_VALIGN_TOP:
4098 case Tabular::VALIGN_TOP:
4099 setVAlign = Tabular::LYX_VALIGN_TOP;
4102 case Tabular::M_VALIGN_BOTTOM:
4103 case Tabular::VALIGN_BOTTOM:
4104 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4107 case Tabular::M_VALIGN_MIDDLE:
4108 case Tabular::VALIGN_MIDDLE:
4109 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4116 cur.recordUndoInset(ATOMIC_UNDO);
4118 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4119 row_type const row = tabular.cellRow(cur.idx());
4120 col_type const column = tabular.cellColumn(cur.idx());
4122 Tabular::ltType ltt;
4126 case Tabular::SET_PWIDTH: {
4127 Length const len(value);
4128 tabular.setColumnPWidth(cur, cur.idx(), len);
4130 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4131 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4135 case Tabular::SET_MPWIDTH:
4136 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4139 case Tabular::SET_SPECIAL_COLUMN:
4140 case Tabular::SET_SPECIAL_MULTI:
4141 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4144 case Tabular::APPEND_ROW:
4145 // append the row into the tabular
4146 tabular.appendRow(cur.idx());
4149 case Tabular::APPEND_COLUMN:
4150 // append the column into the tabular
4151 tabular.appendColumn(cur.idx());
4152 cur.idx() = tabular.cellIndex(row, column);
4155 case Tabular::DELETE_ROW:
4156 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4157 tabular.deleteRow(sel_row_start);
4158 if (sel_row_start >= tabular.rowCount())
4160 cur.idx() = tabular.cellIndex(sel_row_start, column);
4163 cur.selection() = false;
4166 case Tabular::DELETE_COLUMN:
4167 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4168 tabular.deleteColumn(sel_col_start);
4169 if (sel_col_start >= tabular.columnCount())
4171 cur.idx() = tabular.cellIndex(row, sel_col_start);
4174 cur.selection() = false;
4177 case Tabular::COPY_ROW:
4178 tabular.copyRow(row);
4181 case Tabular::COPY_COLUMN:
4182 tabular.copyColumn(column);
4183 cur.idx() = tabular.cellIndex(row, column);
4186 case Tabular::TOGGLE_LINE_TOP: {
4187 bool lineSet = !tabular.topLine(cur.idx());
4188 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4189 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4190 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4194 case Tabular::TOGGLE_LINE_BOTTOM: {
4195 bool lineSet = !tabular.bottomLine(cur.idx());
4196 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4197 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4198 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4202 case Tabular::TOGGLE_LINE_LEFT: {
4203 bool lineSet = !tabular.leftLine(cur.idx());
4204 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4205 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4206 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4210 case Tabular::TOGGLE_LINE_RIGHT: {
4211 bool lineSet = !tabular.rightLine(cur.idx());
4212 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4213 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4214 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4218 case Tabular::M_ALIGN_LEFT:
4219 case Tabular::M_ALIGN_RIGHT:
4220 case Tabular::M_ALIGN_CENTER:
4222 case Tabular::ALIGN_LEFT:
4223 case Tabular::ALIGN_RIGHT:
4224 case Tabular::ALIGN_CENTER:
4225 case Tabular::ALIGN_BLOCK:
4226 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4227 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4228 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4231 case Tabular::M_VALIGN_TOP:
4232 case Tabular::M_VALIGN_BOTTOM:
4233 case Tabular::M_VALIGN_MIDDLE:
4235 case Tabular::VALIGN_TOP:
4236 case Tabular::VALIGN_BOTTOM:
4237 case Tabular::VALIGN_MIDDLE:
4238 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4239 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4240 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4243 case Tabular::MULTICOLUMN: {
4244 if (sel_row_start != sel_row_end) {
4245 // FIXME: Need I say it ? This is horrible.
4247 Alert::error(_("Error setting multicolumn"),
4248 _("You cannot set multicolumn vertically."));
4251 if (!cur.selection()) {
4252 // just multicol for one single cell
4253 // check whether we are completely in a multicol
4254 if (tabular.isMultiColumn(cur.idx()))
4255 tabular.unsetMultiColumn(cur.idx());
4257 tabular.setMultiColumn(cur.idx(), 1);
4260 // we have a selection so this means we just add all this
4261 // cells to form a multicolumn cell
4262 idx_type const s_start = cur.selBegin().idx();
4263 idx_type const s_end = cur.selEnd().idx();
4264 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4265 cur.idx() = s_start;
4268 cur.selection() = false;
4272 case Tabular::SET_ALL_LINES:
4274 case Tabular::UNSET_ALL_LINES:
4275 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4276 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4277 tabular.setAllLines(
4278 tabular.cellIndex(i,j), setLines);
4281 case Tabular::SET_BORDER_LINES:
4282 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4283 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4284 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4286 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4287 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4288 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4292 case Tabular::SET_LONGTABULAR:
4293 tabular.setLongTabular(true);
4296 case Tabular::UNSET_LONGTABULAR:
4297 tabular.setLongTabular(false);
4300 case Tabular::SET_ROTATE_TABULAR:
4301 tabular.setRotateTabular(true);
4304 case Tabular::UNSET_ROTATE_TABULAR:
4305 tabular.setRotateTabular(false);
4308 case Tabular::TOGGLE_ROTATE_TABULAR:
4309 tabular.setRotateTabular(!tabular.getRotateTabular());
4312 case Tabular::SET_ROTATE_CELL:
4313 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4314 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4315 tabular.setRotateCell(
4316 tabular.cellIndex(i, j), true);
4319 case Tabular::UNSET_ROTATE_CELL:
4320 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4321 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4322 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4325 case Tabular::TOGGLE_ROTATE_CELL:
4327 bool oneNotRotated = oneCellHasRotationState(false,
4328 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4330 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4331 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4332 tabular.setRotateCell(tabular.cellIndex(i, j),
4337 case Tabular::SET_USEBOX: {
4338 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4339 if (val == tabular.getUsebox(cur.idx()))
4340 val = Tabular::BOX_NONE;
4341 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4342 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4343 tabular.setUsebox(tabular.cellIndex(i, j), val);
4347 case Tabular::UNSET_LTFIRSTHEAD:
4349 case Tabular::SET_LTFIRSTHEAD:
4350 tabular.getRowOfLTFirstHead(row, ltt);
4351 checkLongtableSpecial(ltt, value, flag);
4352 tabular.setLTHead(row, flag, ltt, true);
4355 case Tabular::UNSET_LTHEAD:
4357 case Tabular::SET_LTHEAD:
4358 tabular.getRowOfLTHead(row, ltt);
4359 checkLongtableSpecial(ltt, value, flag);
4360 tabular.setLTHead(row, flag, ltt, false);
4363 case Tabular::UNSET_LTFOOT:
4365 case Tabular::SET_LTFOOT:
4366 tabular.getRowOfLTFoot(row, ltt);
4367 checkLongtableSpecial(ltt, value, flag);
4368 tabular.setLTFoot(row, flag, ltt, false);
4371 case Tabular::UNSET_LTLASTFOOT:
4373 case Tabular::SET_LTLASTFOOT:
4374 tabular.getRowOfLTLastFoot(row, ltt);
4375 checkLongtableSpecial(ltt, value, flag);
4376 tabular.setLTFoot(row, flag, ltt, true);
4379 case Tabular::SET_LTNEWPAGE:
4380 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4383 case Tabular::SET_BOOKTABS:
4384 tabular.setBookTabs(true);
4387 case Tabular::UNSET_BOOKTABS:
4388 tabular.setBookTabs(false);
4391 case Tabular::SET_TOP_SPACE: {
4393 if (value == "default")
4394 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4395 tabular.row_info[i].top_space_default = true;
4396 else if (isValidLength(value, &len))
4397 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4398 tabular.row_info[i].top_space_default = false;
4399 tabular.row_info[i].top_space = len;
4402 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4403 tabular.row_info[i].top_space_default = false;
4404 tabular.row_info[i].top_space = len;
4409 case Tabular::SET_BOTTOM_SPACE: {
4411 if (value == "default")
4412 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4413 tabular.row_info[i].bottom_space_default = true;
4414 else if (isValidLength(value, &len))
4415 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4416 tabular.row_info[i].bottom_space_default = false;
4417 tabular.row_info[i].bottom_space = len;
4420 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4421 tabular.row_info[i].bottom_space_default = false;
4422 tabular.row_info[i].bottom_space = len;
4427 case Tabular::SET_INTERLINE_SPACE: {
4429 if (value == "default")
4430 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4431 tabular.row_info[i].interline_space_default = true;
4432 else if (isValidLength(value, &len))
4433 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4434 tabular.row_info[i].interline_space_default = false;
4435 tabular.row_info[i].interline_space = len;
4438 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4439 tabular.row_info[i].interline_space_default = false;
4440 tabular.row_info[i].interline_space = len;
4445 // dummy stuff just to avoid warnings
4446 case Tabular::LAST_ACTION:
4452 bool InsetTabular::showInsetDialog(BufferView * bv) const
4454 bv->showDialog("tabular", params2string(*this),
4455 const_cast<InsetTabular *>(this));
4460 void InsetTabular::openLayoutDialog(BufferView * bv) const
4462 bv->showDialog("tabular", params2string(*this),
4463 const_cast<InsetTabular *>(this));
4467 bool InsetTabular::copySelection(Cursor & cur)
4469 if (!cur.selection())
4474 getSelection(cur, rs, re, cs, ce);
4476 paste_tabular.reset(new Tabular(tabular));
4478 for (row_type i = 0; i < rs; ++i)
4479 paste_tabular->deleteRow(0);
4481 row_type const rows = re - rs + 1;
4482 while (paste_tabular->rowCount() > rows)
4483 paste_tabular->deleteRow(rows);
4485 for (col_type i = 0; i < cs; ++i)
4486 paste_tabular->deleteColumn(0);
4488 col_type const columns = ce - cs + 1;
4489 while (paste_tabular->columnCount() > columns)
4490 paste_tabular->deleteColumn(columns);
4492 // We clear all the InsetTableCell pointers, since they
4493 // might now become invalid and there is no point in having
4494 // them point to temporary things in paste_tabular.
4495 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4496 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4497 paste_tabular->cellInset(i,j)->setCellData(0);
4498 paste_tabular->cellInset(i,j)->setTabular(0);
4501 odocstringstream os;
4502 OutputParams const runparams(0);
4503 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4504 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4505 cap::copySelection(cur, os.str());
4507 // mark tabular stack dirty
4508 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4509 // when we (hopefully) have a one-for-all paste mechanism.
4510 // This must be called after cap::copySelection.
4511 dirtyTabularStack(true);
4517 bool InsetTabular::pasteClipboard(Cursor & cur)
4521 col_type const actcol = tabular.cellColumn(cur.idx());
4522 row_type const actrow = tabular.cellRow(cur.idx());
4523 for (row_type r1 = 0, r2 = actrow;
4524 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4526 for (col_type c1 = 0, c2 = actcol;
4527 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4529 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4530 tabular.isPartOfMultiColumn(r2, c2))
4532 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4536 if (tabular.isPartOfMultiColumn(r2, c2)) {
4540 shared_ptr<InsetTableCell> inset(
4541 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4542 // note that setCellInset will call InsetTableCell::setCellData()
4543 // and InsetTableCell::setTabular()
4544 tabular.setCellInset(r2, c2, inset);
4545 // FIXME: change tracking (MG)
4546 inset->setChange(Change(cur.buffer().params().trackChanges ?
4547 Change::INSERTED : Change::UNCHANGED));
4555 void InsetTabular::cutSelection(Cursor & cur)
4557 if (!cur.selection())
4562 getSelection(cur, rs, re, cs, ce);
4563 for (row_type i = rs; i <= re; ++i) {
4564 for (col_type j = cs; j <= ce; ++j) {
4565 shared_ptr<InsetTableCell> t
4566 = cell(tabular.cellIndex(i, j));
4567 if (cur.buffer().params().trackChanges)
4568 // FIXME: Change tracking (MG)
4569 t->setChange(Change(Change::DELETED));
4575 // cursor position might be invalid now
4576 if (cur.pit() > cur.lastpit())
4577 cur.pit() = cur.lastpit();
4578 if (cur.pos() > cur.lastpos())
4579 cur.pos() = cur.lastpos();
4580 cur.clearSelection();
4584 bool InsetTabular::isRightToLeft(Cursor & cur) const
4586 BOOST_ASSERT(cur.depth() > 1);
4587 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4588 pos_type const parentpos = cur[cur.depth() - 2].pos();
4589 return parentpar.getFontSettings(cur.bv().buffer().params(),
4590 parentpos).language()->rightToLeft();
4594 void InsetTabular::getSelection(Cursor & cur,
4595 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4597 CursorSlice const & beg = cur.selBegin();
4598 CursorSlice const & end = cur.selEnd();
4599 cs = tabular.cellColumn(beg.idx());
4600 ce = tabular.cellColumn(end.idx());
4603 cs = tabular.cellColumn(end.idx());
4605 ce = tabular.cellRightColumn(end.idx());
4608 rs = tabular.cellRow(beg.idx());
4609 re = tabular.cellRow(end.idx());
4615 Text * InsetTabular::getText(int idx) const
4617 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4621 void InsetTabular::setChange(Change const & change)
4623 for (idx_type idx = 0; idx < nargs(); ++idx)
4624 cell(idx)->setChange(change);
4628 void InsetTabular::acceptChanges(BufferParams const & bparams)
4630 for (idx_type idx = 0; idx < nargs(); ++idx)
4631 cell(idx)->acceptChanges(bparams);
4635 void InsetTabular::rejectChanges(BufferParams const & bparams)
4637 for (idx_type idx = 0; idx < nargs(); ++idx)
4638 cell(idx)->rejectChanges(bparams);
4642 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4644 return tabular.getPWidth(cell).zero();
4648 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4650 return !tabular.getPWidth(cell).zero();
4654 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4657 if (buf.length() <= 0)
4662 col_type maxCols = 1;
4663 size_t const len = buf.length();
4667 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4675 maxCols = max(cols, maxCols);
4681 maxCols = max(cols, maxCols);
4687 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4688 loctab = paste_tabular.get();
4690 dirtyTabularStack(true);
4693 cell = bv.cursor().idx();
4694 ocol = tabular.cellColumn(cell);
4695 row = tabular.cellRow(cell);
4699 idx_type const cells = loctab->cellCount();
4702 rows = loctab->rowCount();
4703 col_type const columns = loctab->columnCount();
4705 while (cell < cells && p < len && row < rows &&
4706 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4712 // we can only set this if we are not too far right
4713 if (cols < columns) {
4714 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4715 Font const font = bv.textMetrics(&inset->text_).
4717 inset->setText(buf.substr(op, p - op), font,
4718 buffer().params().trackChanges);
4724 // we can only set this if we are not too far right
4725 if (cols < columns) {
4726 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4727 Font const font = bv.textMetrics(&inset->text_).
4729 inset->setText(buf.substr(op, p - op), font,
4730 buffer().params().trackChanges);
4735 cell = loctab->cellIndex(row, cols);
4741 // check for the last cell if there is no trailing '\n'
4742 if (cell < cells && op < len) {
4743 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4744 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4745 inset->setText(buf.substr(op, len - op), font,
4746 buffer().params().trackChanges);
4752 void InsetTabular::addPreview(PreviewLoader & loader) const
4754 row_type const rows = tabular.rowCount();
4755 col_type const columns = tabular.columnCount();
4756 for (row_type i = 0; i < rows; ++i) {
4757 for (col_type j = 0; j < columns; ++j)
4758 tabular.cellInset(i, j)->addPreview(loader);
4763 bool InsetTabular::tablemode(Cursor & cur) const
4765 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4769 bool InsetTabular::completionSupported(Cursor const & cur) const
4771 Cursor const & bvCur = cur.bv().cursor();
4772 if (&bvCur.inset() != this)
4774 return cur.text()->completionSupported(cur);
4778 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4780 return completionSupported(cur);
4784 bool InsetTabular::automaticInlineCompletion() const
4786 return lyxrc.completion_inline_text;
4790 bool InsetTabular::automaticPopupCompletion() const
4792 return lyxrc.completion_popup_text;
4796 bool InsetTabular::showCompletionCursor() const
4798 return lyxrc.completion_cursor_text;
4802 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4804 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4808 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4810 if (!completionSupported(cur))
4812 return cur.text()->completionPrefix(cur);
4816 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4818 if (!completionSupported(cur))
4821 return cur.text()->insertCompletion(cur, s, finished);
4825 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4826 Dimension & dim) const
4828 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4829 tm.completionPosAndDim(cur, x, y, dim);
4833 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4835 istringstream data(in);
4837 lex.setStream(data);
4844 if (!lex || token != "tabular") {
4845 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4849 // This is part of the inset proper that is usually swallowed
4850 // by Buffer::readInset
4852 if (!lex || token != "Tabular") {
4853 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4861 string InsetTabular::params2string(InsetTabular const & inset)
4864 data << "tabular" << ' ';
4866 data << "\\end_inset\n";