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 j = 0; j < ncols; ++j)
631 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
637 void Tabular::deleteRow(row_type const row)
639 // Not allowed to delete last row
643 row_info.erase(row_info.begin() + row);
644 cell_info.erase(cell_info.begin() + row);
649 void Tabular::copyRow(row_type const row)
651 row_info.insert(row_info.begin() + row, row_info[row]);
652 cell_info.insert(cell_info.begin() + row, cell_info[row]);
654 if (buffer().params().trackChanges)
655 for (col_type j = 0; j < columnCount(); ++j)
656 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
662 void Tabular::appendColumn(idx_type const cell)
664 col_type const column = cellColumn(cell);
665 column_vector::iterator cit = column_info.begin() + column + 1;
666 column_info.insert(cit, ColumnData());
667 col_type const ncols = columnCount();
668 // set the column values of the column before
669 column_info[column + 1] = column_info[column];
671 for (row_type i = 0; i < rowCount(); ++i) {
672 cell_info[i].insert(cell_info[i].begin() + column + 1, CellData(buffer(), *this));
673 col_type c = column + 2;
675 && cell_info[i][c].multicolumn == CELL_PART_OF_MULTICOLUMN) {
676 cell_info[i][c].multicolumn = CELL_NORMAL;
681 for (row_type i = 0; i < rowCount(); ++i) {
682 cell_info[i][column + 1].inset->clear();
683 if (buffer().params().trackChanges)
684 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
690 void Tabular::deleteColumn(col_type const column)
692 // Not allowed to delete last column
693 if (columnCount() == 1)
696 column_info.erase(column_info.begin() + column);
697 for (row_type i = 0; i < rowCount(); ++i) {
698 // Care about multicolumn cells
699 if (column + 1 < columnCount() &&
700 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
701 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
702 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
704 cell_info[i].erase(cell_info[i].begin() + column);
710 void Tabular::copyColumn(col_type const column)
712 BufferParams const & bp = buffer().params();
713 column_info.insert(column_info.begin() + column, column_info[column]);
715 for (row_type i = 0; i < rowCount(); ++i)
716 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
719 for (row_type i = 0; i < rowCount(); ++i)
720 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
725 void Tabular::updateIndexes()
727 col_type ncols = columnCount();
728 row_type nrows = rowCount();
730 for (row_type row = 0; row < nrows; ++row)
731 for (col_type column = 0; column < ncols; ++column) {
732 if (!isPartOfMultiColumn(row, column))
734 cell_info[row][column].cellno = numberofcells - 1;
737 rowofcell.resize(numberofcells);
738 columnofcell.resize(numberofcells);
740 for (row_type row = 0; row < nrows; ++row)
741 for (col_type column = 0; column < ncols; ++column) {
742 if (isPartOfMultiColumn(row, column))
745 columnofcell[i] = column;
751 Tabular::idx_type Tabular::cellCount() const
753 return numberofcells;
757 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
759 row_type const row = cellRow(cell);
761 for (col_type i = 0; i < columnCount(); ++i)
762 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
768 bool Tabular::topLine(idx_type const cell) const
770 return cellInfo(cell).top_line;
774 bool Tabular::bottomLine(idx_type const cell) const
776 return cellInfo(cell).bottom_line;
780 bool Tabular::leftLine(idx_type cell) const
784 return cellInfo(cell).left_line;
788 bool Tabular::rightLine(idx_type cell) const
792 return cellInfo(cell).right_line;
796 bool Tabular::topAlreadyDrawn(idx_type cell) const
798 row_type row = cellRow(cell);
801 idx_type i = cellIndex(row - 1, cellColumn(cell));
802 return !rowBottomLine(row - 1) && bottomLine(i);
806 bool Tabular::leftAlreadyDrawn(idx_type cell) const
808 col_type col = cellColumn(cell);
811 idx_type i = cellIndex(cellRow(cell), col - 1);
812 return rightLine(i) && !leftLine(cell);
816 bool Tabular::isLastRow(idx_type cell) const
818 return cellRow(cell) == rowCount() - 1;
822 int Tabular::getAdditionalHeight(row_type row) const
824 if (!row || row >= rowCount())
827 int const interline_space = row_info[row - 1].interline_space_default ?
829 row_info[row - 1].interline_space.inPixels(width());
830 if (rowTopLine(row) && rowBottomLine(row - 1))
831 return interline_space + WIDTH_OF_LINE;
832 return interline_space;
836 int Tabular::getAdditionalWidth(idx_type cell) const
838 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
840 && nextcol < columnCount() && leftLine(cellIndex(cellRow(cell), nextcol)))
841 return WIDTH_OF_LINE;
846 int Tabular::columnWidth(idx_type cell) const
849 col_type const span = columnSpan(cell);
850 col_type const col = cellColumn(cell);
851 for(col_type c = col; c < col + span ; ++c)
852 w += column_info[c].width;
857 bool Tabular::updateColumnWidths()
859 col_type const ncols = columnCount();
860 row_type const nrows = rowCount();
862 // for each col get max of single col cells
863 for(col_type c = 0; c < ncols; ++c) {
865 for(row_type r = 0; r < nrows; ++r) {
866 idx_type const i = cellIndex(r, c);
867 if (columnSpan(i) == 1)
868 new_width = max(new_width, cellInfo(i).width);
871 if (column_info[c].width != new_width) {
872 column_info[c].width = new_width;
876 // update col widths to fit merged cells
877 for(col_type c = 0; c < ncols; ++c)
878 for(row_type r = 0; r < nrows; ++r) {
879 idx_type const i = cellIndex(r, c);
880 int const span = columnSpan(i);
881 if (span == 1 || c > cellColumn(i))
885 for(col_type j = c; j < c + span ; ++j)
886 old_width += column_info[j].width;
888 if (cellInfo(i).width > old_width) {
889 column_info[c + span - 1].width += cellInfo(i).width - old_width;
898 int Tabular::width() const
900 col_type const ncols = columnCount();
902 for (col_type i = 0; i < ncols; ++i)
903 width += column_info[i].width;
908 void Tabular::setCellWidth(idx_type cell, int new_width)
910 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
911 + getAdditionalWidth(cell);
915 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
918 if (!isMultiColumn(cell) || onlycolumn)
919 column_info[cellColumn(cell)].alignment = align;
921 cellInfo(cell).alignment = align;
925 void Tabular::setVAlignment(idx_type cell, VAlignment align,
928 if (!isMultiColumn(cell) || onlycolumn)
929 column_info[cellColumn(cell)].valignment = align;
931 cellInfo(cell).valignment = align;
938 * Allow line and paragraph breaks for fixed width cells or disallow them,
939 * merge cell paragraphs and reset layout to standard for variable width
942 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
944 inset->setAutoBreakRows(fixedWidth);
948 // merge all paragraphs to one
949 BufferParams const & bp = cur.bv().buffer().params();
950 while (inset->paragraphs().size() > 1)
951 mergeParagraph(bp, inset->paragraphs(), 0);
955 // undo information has already been recorded
956 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
957 bp.documentClass().emptyLayoutName());
964 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
965 Length const & width)
967 col_type const j = cellColumn(cell);
969 column_info[j].p_width = width;
970 for (row_type i = 0; i < rowCount(); ++i) {
971 idx_type const cell = cellIndex(i, j);
972 // because of multicolumns
973 toggleFixedWidth(cur, cellInset(cell).get(),
974 !getPWidth(cell).zero());
976 // cur paragraph can become invalid after paragraphs were merged
977 if (cur.pit() > cur.lastpit())
978 cur.pit() = cur.lastpit();
979 // cur position can become invalid after newlines were removed
980 if (cur.pos() > cur.lastpos())
981 cur.pos() = cur.lastpos();
985 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
986 Length const & width)
988 if (!isMultiColumn(cell))
991 cellInfo(cell).p_width = width;
992 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
993 // cur paragraph can become invalid after paragraphs were merged
994 if (cur.pit() > cur.lastpit())
995 cur.pit() = cur.lastpit();
996 // cur position can become invalid after newlines were removed
997 if (cur.pos() > cur.lastpos())
998 cur.pos() = cur.lastpos();
1003 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1004 Tabular::Feature what)
1006 if (what == SET_SPECIAL_MULTI)
1007 cellInfo(cell).align_special = special;
1009 column_info[cellColumn(cell)].align_special = special;
1013 void Tabular::setAllLines(idx_type cell, bool line)
1015 setTopLine(cell, line);
1016 setBottomLine(cell, line);
1017 setRightLine(cell, line);
1018 setLeftLine(cell, line);
1022 void Tabular::setTopLine(idx_type i, bool line)
1024 cellInfo(i).top_line = line;
1028 void Tabular::setBottomLine(idx_type i, bool line)
1030 cellInfo(i).bottom_line = line;
1034 void Tabular::setLeftLine(idx_type cell, bool line)
1036 cellInfo(cell).left_line = line;
1040 void Tabular::setRightLine(idx_type cell, bool line)
1042 cellInfo(cell).right_line = line;
1045 bool Tabular::rowTopLine(row_type r) const
1047 idx_type i0 = getFirstCellInRow(r);
1048 idx_type i1 = getLastCellInRow(r);
1049 bool all_rows_set = true;
1050 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1051 all_rows_set = cellInfo(j).top_line;
1052 return all_rows_set;
1056 bool Tabular::rowBottomLine(row_type r) const
1058 idx_type i0 = getFirstCellInRow(r);
1059 idx_type i1 = getLastCellInRow(r);
1060 bool all_rows_set = true;
1061 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1062 all_rows_set = cellInfo(j).bottom_line;
1063 return all_rows_set;
1067 bool Tabular::columnLeftLine(col_type c) const
1069 bool all_cols_set = true;
1070 row_type const nrows = rowCount();
1071 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1072 idx_type i = cellIndex(r, c);
1073 if (c == cellColumn(i))
1074 all_cols_set = cellInfo(i).left_line;
1076 return all_cols_set;
1080 bool Tabular::columnRightLine(col_type c) const
1082 bool all_cols_set = true;
1083 row_type const nrows = rowCount();
1084 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1085 idx_type i = cellIndex(r, c);
1086 if (c == cellColumn(i) + columnSpan(i) - 1)
1087 all_cols_set = cellInfo(i).right_line;
1089 return all_cols_set;
1093 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1095 if (!onlycolumn && isMultiColumn(cell))
1096 return cellInfo(cell).alignment;
1097 return column_info[cellColumn(cell)].alignment;
1102 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1104 if (!onlycolumn && isMultiColumn(cell))
1105 return cellInfo(cell).valignment;
1106 return column_info[cellColumn(cell)].valignment;
1110 Length const Tabular::getPWidth(idx_type cell) const
1112 if (isMultiColumn(cell))
1113 return cellInfo(cell).p_width;
1114 return column_info[cellColumn(cell)].p_width;
1118 Length const Tabular::getColumnPWidth(idx_type cell) const
1120 return column_info[cellColumn(cell)].p_width;
1124 Length const Tabular::getMColumnPWidth(idx_type cell) const
1126 if (isMultiColumn(cell))
1127 return cellInfo(cell).p_width;
1132 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1134 if (what == SET_SPECIAL_MULTI)
1135 return cellInfo(cell).align_special;
1136 return column_info[cellColumn(cell)].align_special;
1140 int Tabular::cellWidth(idx_type cell) const
1142 return cellInfo(cell).width;
1146 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1150 switch (getAlignment(cell)) {
1151 case LYX_ALIGN_CENTER:
1152 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1154 case LYX_ALIGN_RIGHT:
1155 x += columnWidth(cell) - cellWidth(cell);
1156 // + getAdditionalWidth(cell);
1159 // LYX_ALIGN_LEFT: nothing :-)
1163 // the LaTeX Way :-(
1169 bool Tabular::isFirstCellInRow(idx_type cell) const
1171 return cellColumn(cell) == 0;
1175 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1177 if (row > rowCount() - 1)
1178 row = rowCount() - 1;
1179 return cell_info[row][0].cellno;
1183 bool Tabular::isLastCellInRow(idx_type cell) const
1185 return cellRightColumn(cell) == columnCount() - 1;
1189 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1191 if (row > rowCount() - 1)
1192 row = rowCount() - 1;
1193 return cell_info[row][columnCount() - 1].cellno;
1197 Tabular::row_type Tabular::cellRow(idx_type cell) const
1199 if (cell >= cellCount())
1200 return rowCount() - 1;
1203 return rowofcell[cell];
1207 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1209 if (cell >= cellCount())
1210 return columnCount() - 1;
1213 return columnofcell[cell];
1217 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1219 row_type const row = cellRow(cell);
1220 col_type column = cellColumn(cell);
1221 while (column < columnCount() - 1 &&
1222 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1228 void Tabular::write(ostream & os) const
1232 << write_attribute("version", 3)
1233 << write_attribute("rows", rowCount())
1234 << write_attribute("columns", columnCount())
1236 // global longtable options
1238 << write_attribute("rotate", rotate)
1239 << write_attribute("booktabs", use_booktabs)
1240 << write_attribute("islongtable", is_long_tabular)
1241 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1242 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1243 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1244 << write_attribute("headTopDL", endhead.topDL)
1245 << write_attribute("headBottomDL", endhead.bottomDL)
1246 << write_attribute("footTopDL", endfoot.topDL)
1247 << write_attribute("footBottomDL", endfoot.bottomDL)
1248 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1249 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1250 << write_attribute("lastFootEmpty", endlastfoot.empty)
1252 for (col_type j = 0; j < columnCount(); ++j) {
1254 << write_attribute("alignment", column_info[j].alignment)
1255 << write_attribute("valignment", column_info[j].valignment)
1256 << write_attribute("width", column_info[j].p_width.asString())
1257 << write_attribute("special", column_info[j].align_special)
1260 for (row_type i = 0; i < rowCount(); ++i) {
1261 static const string def("default");
1263 if (row_info[i].top_space_default)
1264 os << write_attribute("topspace", def);
1266 os << write_attribute("topspace", row_info[i].top_space);
1267 if (row_info[i].bottom_space_default)
1268 os << write_attribute("bottomspace", def);
1270 os << write_attribute("bottomspace", row_info[i].bottom_space);
1271 if (row_info[i].interline_space_default)
1272 os << write_attribute("interlinespace", def);
1274 os << write_attribute("interlinespace", row_info[i].interline_space);
1275 os << write_attribute("endhead", row_info[i].endhead)
1276 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1277 << write_attribute("endfoot", row_info[i].endfoot)
1278 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1279 << write_attribute("newpage", row_info[i].newpage)
1281 for (col_type j = 0; j < columnCount(); ++j) {
1283 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1284 << write_attribute("alignment", cell_info[i][j].alignment)
1285 << write_attribute("valignment", cell_info[i][j].valignment)
1286 << write_attribute("topline", cell_info[i][j].top_line)
1287 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1288 << write_attribute("leftline", cell_info[i][j].left_line)
1289 << write_attribute("rightline", cell_info[i][j].right_line)
1290 << write_attribute("rotate", cell_info[i][j].rotate)
1291 << write_attribute("usebox", cell_info[i][j].usebox)
1292 << write_attribute("width", cell_info[i][j].p_width)
1293 << write_attribute("special", cell_info[i][j].align_special)
1295 os << "\\begin_inset ";
1296 cell_info[i][j].inset->write(os);
1297 os << "\n\\end_inset\n"
1302 os << "</lyxtabular>\n";
1306 void Tabular::read(Lexer & lex)
1309 istream & is = lex.getStream();
1311 l_getline(is, line);
1312 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1313 BOOST_ASSERT(false);
1318 if (!getTokenValue(line, "version", version))
1320 BOOST_ASSERT(version >= 2);
1323 if (!getTokenValue(line, "rows", rows_arg))
1326 if (!getTokenValue(line, "columns", columns_arg))
1328 init(buffer(), rows_arg, columns_arg);
1329 l_getline(is, line);
1330 if (!prefixIs(line, "<features")) {
1331 lyxerr << "Wrong tabular format (expected <features ...> got"
1332 << line << ')' << endl;
1335 getTokenValue(line, "rotate", rotate);
1336 getTokenValue(line, "booktabs", use_booktabs);
1337 getTokenValue(line, "islongtable", is_long_tabular);
1338 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1339 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1340 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1341 getTokenValue(line, "headTopDL", endhead.topDL);
1342 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1343 getTokenValue(line, "footTopDL", endfoot.topDL);
1344 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1345 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1346 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1347 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1349 for (col_type j = 0; j < columnCount(); ++j) {
1351 if (!prefixIs(line,"<column")) {
1352 lyxerr << "Wrong tabular format (expected <column ...> got"
1353 << line << ')' << endl;
1356 getTokenValue(line, "alignment", column_info[j].alignment);
1357 getTokenValue(line, "valignment", column_info[j].valignment);
1358 getTokenValue(line, "width", column_info[j].p_width);
1359 getTokenValue(line, "special", column_info[j].align_special);
1362 for (row_type i = 0; i < rowCount(); ++i) {
1363 l_getline(is, line);
1364 if (!prefixIs(line, "<row")) {
1365 lyxerr << "Wrong tabular format (expected <row ...> got"
1366 << line << ')' << endl;
1369 getTokenValue(line, "topspace", row_info[i].top_space,
1370 row_info[i].top_space_default);
1371 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1372 row_info[i].bottom_space_default);
1373 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1374 row_info[i].interline_space_default);
1375 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1376 getTokenValue(line, "endhead", row_info[i].endhead);
1377 getTokenValue(line, "endfoot", row_info[i].endfoot);
1378 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1379 getTokenValue(line, "newpage", row_info[i].newpage);
1380 for (col_type j = 0; j < columnCount(); ++j) {
1381 l_getline(is, line);
1382 if (!prefixIs(line, "<cell")) {
1383 lyxerr << "Wrong tabular format (expected <cell ...> got"
1384 << line << ')' << endl;
1387 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1388 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1389 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1390 getTokenValue(line, "topline", cell_info[i][j].top_line);
1391 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1392 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1393 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1394 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1395 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1396 getTokenValue(line, "width", cell_info[i][j].p_width);
1397 getTokenValue(line, "special", cell_info[i][j].align_special);
1398 l_getline(is, line);
1399 if (prefixIs(line, "\\begin_inset")) {
1400 cell_info[i][j].inset->read(lex);
1401 l_getline(is, line);
1403 if (!prefixIs(line, "</cell>")) {
1404 lyxerr << "Wrong tabular format (expected </cell> got"
1405 << line << ')' << endl;
1409 l_getline(is, line);
1410 if (!prefixIs(line, "</row>")) {
1411 lyxerr << "Wrong tabular format (expected </row> got"
1412 << line << ')' << endl;
1416 while (!prefixIs(line, "</lyxtabular>")) {
1417 l_getline(is, line);
1423 bool Tabular::isMultiColumn(idx_type cell) const
1425 return cellInfo(cell).multicolumn != CELL_NORMAL;
1429 bool Tabular::isMultiColumnReal(idx_type cell) const
1431 return cellColumn(cell) != cellRightColumn(cell) &&
1432 cellInfo(cell).multicolumn != CELL_NORMAL;
1436 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1438 return cell_info[cellRow(cell)][cellColumn(cell)];
1442 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1444 CellData & cs = cellInfo(cell);
1445 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1446 cs.alignment = column_info[cellColumn(cell)].alignment;
1447 for (idx_type i = 1; i < number; ++i) {
1448 CellData & cs1 = cellInfo(cell + i);
1449 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1450 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1453 setRightLine(cell, rightLine(cell + number - 1));
1458 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1460 row_type const row = cellRow(cell);
1461 col_type const ncols = columnCount();
1462 idx_type result = 1;
1463 col_type column = cellColumn(cell) + 1;
1464 while (column < ncols && isPartOfMultiColumn(row, column)) {
1472 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1474 row_type const row = cellRow(cell);
1475 col_type column = cellColumn(cell);
1477 idx_type result = 0;
1479 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1480 cell_info[row][column].multicolumn = CELL_NORMAL;
1482 while (column < columnCount() &&
1483 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1485 cell_info[row][column].multicolumn = CELL_NORMAL;
1495 void Tabular::setBookTabs(bool what)
1497 use_booktabs = what;
1501 bool Tabular::useBookTabs() const
1503 return use_booktabs;
1507 void Tabular::setLongTabular(bool what)
1509 is_long_tabular = what;
1513 bool Tabular::isLongTabular() const
1515 return is_long_tabular;
1519 void Tabular::setRotateTabular(bool flag)
1525 bool Tabular::getRotateTabular() const
1531 void Tabular::setRotateCell(idx_type cell, bool flag)
1533 cellInfo(cell).rotate = flag;
1537 bool Tabular::getRotateCell(idx_type cell) const
1539 return cellInfo(cell).rotate;
1543 bool Tabular::needRotating() const
1547 for (row_type i = 0; i < rowCount(); ++i)
1548 for (col_type j = 0; j < columnCount(); ++j)
1549 if (cell_info[i][j].rotate)
1555 bool Tabular::isLastCell(idx_type cell) const
1557 if (cell + 1 < cellCount())
1563 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1565 if (cellRow(cell) > 0)
1566 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1571 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1573 if (cellRow(cell) + 1 < rowCount())
1574 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1579 Tabular::idx_type Tabular::cellIndex(row_type row,
1580 col_type column) const
1582 BOOST_ASSERT(column != npos && column < columnCount() &&
1583 row != npos && row < rowCount());
1584 return cell_info[row][column].cellno;
1588 void Tabular::setUsebox(idx_type cell, BoxType type)
1590 cellInfo(cell).usebox = type;
1594 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1596 if (column_info[cellColumn(cell)].p_width.zero() &&
1597 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1599 if (cellInfo(cell).usebox > 1)
1600 return cellInfo(cell).usebox;
1601 return useParbox(cell);
1606 // This are functions used for the longtable support
1608 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1614 row_info[row].endfirsthead = flag;
1618 row_info[row].endhead = flag;
1623 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1626 hd.set = haveLTHead();
1627 return row_info[row].endhead;
1631 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1634 hd.set = haveLTFirstHead();
1635 return row_info[row].endfirsthead;
1639 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1645 row_info[row].endlastfoot = flag;
1649 row_info[row].endfoot = flag;
1654 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1657 fd.set = haveLTFoot();
1658 return row_info[row].endfoot;
1662 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1665 fd.set = haveLTLastFoot();
1666 return row_info[row].endlastfoot;
1670 void Tabular::setLTNewPage(row_type row, bool what)
1672 row_info[row].newpage = what;
1676 bool Tabular::getLTNewPage(row_type row) const
1678 return row_info[row].newpage;
1682 bool Tabular::haveLTHead() const
1684 for (row_type i = 0; i < rowCount(); ++i)
1685 if (row_info[i].endhead)
1691 bool Tabular::haveLTFirstHead() const
1693 if (endfirsthead.empty)
1695 for (row_type i = 0; i < rowCount(); ++i)
1696 if (row_info[i].endfirsthead)
1702 bool Tabular::haveLTFoot() const
1704 for (row_type i = 0; i < rowCount(); ++i)
1705 if (row_info[i].endfoot)
1711 bool Tabular::haveLTLastFoot() const
1713 if (endlastfoot.empty)
1715 for (row_type i = 0; i < rowCount(); ++i)
1716 if (row_info[i].endlastfoot)
1722 // end longtable support functions
1724 void Tabular::setRowAscent(row_type row, int height)
1726 if (row >= rowCount() || row_info[row].ascent == height)
1728 row_info[row].ascent = height;
1732 void Tabular::setRowDescent(row_type row, int height)
1734 if (row >= rowCount() || row_info[row].descent == height)
1736 row_info[row].descent = height;
1740 int Tabular::rowAscent(row_type row) const
1742 if (row >= rowCount())
1744 return row_info[row].ascent;
1748 int Tabular::rowDescent(row_type row) const
1750 BOOST_ASSERT(row < rowCount());
1751 return row_info[row].descent;
1755 int Tabular::height() const
1758 for (row_type row = 0; row < rowCount(); ++row)
1759 height += rowAscent(row) + rowDescent(row) +
1760 getAdditionalHeight(row);
1765 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1767 BOOST_ASSERT(row < rowCount());
1768 BOOST_ASSERT(column < columnCount());
1769 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1773 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1775 // FIXME: assert or return 0 as in TeXBottomHLine()?
1776 BOOST_ASSERT(row != npos);
1777 BOOST_ASSERT(row < rowCount());
1779 idx_type const fcell = getFirstCellInRow(row);
1780 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1783 for (idx_type i = fcell; i < n; ++i) {
1787 if (use_booktabs && row == 0) {
1790 } else if (tmp == n - fcell) {
1791 os << (use_booktabs ? "\\midrule " : "\\hline ");
1793 for (idx_type i = fcell; i < n; ++i) {
1795 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1796 << cellColumn(i) + 1
1798 << cellRightColumn(i) + 1
1810 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1812 // FIXME: return 0 or assert as in TeXTopHLine()?
1813 if (row == npos || row >= rowCount())
1816 idx_type const fcell = getFirstCellInRow(row);
1817 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1820 for (idx_type i = fcell; i < n; ++i) {
1824 if (use_booktabs && row == rowCount() - 1) {
1825 if (bottomLine(fcell))
1826 os << "\\bottomrule";
1827 } else if (tmp == n - fcell) {
1828 os << (use_booktabs ? "\\midrule" : "\\hline");
1830 for (idx_type i = fcell; i < n; ++i) {
1831 if (bottomLine(i)) {
1832 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1833 << cellColumn(i) + 1
1835 << cellRightColumn(i) + 1
1847 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1851 if (getRotateCell(cell)) {
1852 os << "\\begin{sideways}\n";
1855 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1856 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1857 // figure out how to set the lines
1858 // we always set double lines to the right of the cell
1859 row_type const r = cellRow(cell);
1860 col_type const c = cellColumn(cell);
1861 col_type const nextcol = c + columnSpan(cell);
1862 bool prevmulticol = ismulticol;
1863 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1864 bool nextcellleft = nextcol < columnCount()
1865 && leftLine(cellIndex(r, nextcol));
1866 bool nextcolleft = nextcol < columnCount() && columnLeftLine(nextcol);
1867 bool coldouble = columnRightLine(c) && nextcolleft;
1868 bool celldouble = rightLine(cell) && nextcellleft;
1869 bool forceleft = prevmulticol && !prevcellright && leftLine(cell);
1870 bool doubleright = celldouble && (isMultiColumn(cell) || !coldouble);
1871 ismulticol = isMultiColumn(cell)
1872 || (leftLine(cell) && !columnLeftLine(c))
1873 || (rightLine(cell) && !columnRightLine(c))
1874 || (!celldouble && coldouble)
1875 || doubleright || forceleft;
1877 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1878 if (leftLine(cell) || forceleft)
1880 if (!cellInfo(cell).align_special.empty()) {
1881 os << cellInfo(cell).align_special;
1883 if (!getPWidth(cell).zero()) {
1885 case LYX_VALIGN_TOP:
1888 case LYX_VALIGN_MIDDLE:
1891 case LYX_VALIGN_BOTTOM:
1896 << from_ascii(getPWidth(cell).asLatexString())
1900 case LYX_ALIGN_LEFT:
1903 case LYX_ALIGN_RIGHT:
1910 } // end if else !getPWidth
1911 } // end if else !cellinfo_of_cell
1912 if (rightLine(cell))
1915 // add extra vertical line if we want a double one
1919 if (getUsebox(cell) == BOX_PARBOX) {
1922 case LYX_VALIGN_TOP:
1925 case LYX_VALIGN_MIDDLE:
1928 case LYX_VALIGN_BOTTOM:
1932 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1934 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1935 os << "\\begin{minipage}[";
1937 case LYX_VALIGN_TOP:
1940 case LYX_VALIGN_MIDDLE:
1943 case LYX_VALIGN_BOTTOM:
1947 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1955 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1960 if (getUsebox(cell) == BOX_PARBOX)
1962 else if (getUsebox(cell) == BOX_MINIPAGE) {
1963 os << "%\n\\end{minipage}";
1969 if (getRotateCell(cell)) {
1970 os << "%\n\\end{sideways}";
1977 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1978 OutputParams const & runparams) const
1980 if (!is_long_tabular)
1984 // output header info
1986 if (endhead.topDL) {
1990 for (row_type i = 0; i < rowCount(); ++i) {
1991 if (row_info[i].endhead) {
1992 ret += TeXRow(os, i, runparams);
1995 if (endhead.bottomDL) {
1999 os << "\\endhead\n";
2001 if (endfirsthead.empty) {
2002 os << "\\endfirsthead\n";
2006 // output firstheader info
2007 if (haveLTFirstHead()) {
2008 if (endfirsthead.topDL) {
2012 for (row_type i = 0; i < rowCount(); ++i) {
2013 if (row_info[i].endfirsthead) {
2014 ret += TeXRow(os, i, runparams);
2017 if (endfirsthead.bottomDL) {
2021 os << "\\endfirsthead\n";
2024 // output footer info
2026 if (endfoot.topDL) {
2030 for (row_type i = 0; i < rowCount(); ++i) {
2031 if (row_info[i].endfoot) {
2032 ret += TeXRow(os, i, runparams);
2035 if (endfoot.bottomDL) {
2039 os << "\\endfoot\n";
2041 if (endlastfoot.empty) {
2042 os << "\\endlastfoot\n";
2046 // output lastfooter info
2047 if (haveLTLastFoot()) {
2048 if (endlastfoot.topDL) {
2052 for (row_type i = 0; i < rowCount(); ++i) {
2053 if (row_info[i].endlastfoot) {
2054 ret += TeXRow(os, i, runparams);
2057 if (endlastfoot.bottomDL) {
2061 os << "\\endlastfoot\n";
2068 bool Tabular::isValidRow(row_type row) const
2070 if (!is_long_tabular)
2072 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2073 !row_info[row].endfoot && !row_info[row].endlastfoot;
2077 int Tabular::TeXRow(odocstream & os, row_type i,
2078 OutputParams const & runparams) const
2080 idx_type cell = cellIndex(i, 0);
2081 int ret = TeXTopHLine(os, i);
2082 if (row_info[i].top_space_default) {
2084 os << "\\addlinespace\n";
2086 os << "\\noalign{\\vskip\\doublerulesep}\n";
2088 } else if(!row_info[i].top_space.zero()) {
2090 os << "\\addlinespace["
2091 << from_ascii(row_info[i].top_space.asLatexString())
2094 os << "\\noalign{\\vskip"
2095 << from_ascii(row_info[i].top_space.asLatexString())
2100 bool ismulticol = false;
2101 for (col_type j = 0; j < columnCount(); ++j) {
2102 if (isPartOfMultiColumn(i, j))
2104 ret += TeXCellPreamble(os, cell, ismulticol);
2105 shared_ptr<InsetTableCell> inset = cellInset(cell);
2107 Paragraph const & par = inset->paragraphs().front();
2108 bool rtl = par.isRTL(buffer().params())
2110 && getPWidth(cell).zero();
2113 if (par.getParLanguage(buffer().params())->lang() ==
2116 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2118 // currently, remaning RTL languages are arabic_arabtex and hebrew
2122 ret += inset->latex(os, runparams);
2126 ret += TeXCellPostamble(os, cell, ismulticol);
2127 if (!isLastCellInRow(cell)) { // not last cell in row
2132 os << "\\tabularnewline";
2133 if (row_info[i].bottom_space_default) {
2135 os << "\\addlinespace";
2137 os << "[\\doublerulesep]";
2138 } else if (!row_info[i].bottom_space.zero()) {
2140 os << "\\addlinespace";
2142 << from_ascii(row_info[i].bottom_space.asLatexString())
2147 ret += TeXBottomHLine(os, i);
2148 if (row_info[i].interline_space_default) {
2150 os << "\\addlinespace\n";
2152 os << "\\noalign{\\vskip\\doublerulesep}\n";
2154 } else if (!row_info[i].interline_space.zero()) {
2156 os << "\\addlinespace["
2157 << from_ascii(row_info[i].interline_space.asLatexString())
2160 os << "\\noalign{\\vskip"
2161 << from_ascii(row_info[i].interline_space.asLatexString())
2169 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2173 //+---------------------------------------------------------------------
2174 //+ first the opening preamble +
2175 //+---------------------------------------------------------------------
2178 os << "\\begin{sideways}\n";
2181 if (is_long_tabular)
2182 os << "\\begin{longtable}{";
2184 os << "\\begin{tabular}{";
2186 for (col_type i = 0; i < columnCount(); ++i) {
2187 if (!use_booktabs && columnLeftLine(i))
2189 if (!column_info[i].align_special.empty()) {
2190 os << column_info[i].align_special;
2192 if (!column_info[i].p_width.zero()) {
2193 switch (column_info[i].alignment) {
2194 case LYX_ALIGN_LEFT:
2195 os << ">{\\raggedright}";
2197 case LYX_ALIGN_RIGHT:
2198 os << ">{\\raggedleft}";
2200 case LYX_ALIGN_CENTER:
2201 os << ">{\\centering}";
2203 case LYX_ALIGN_NONE:
2204 case LYX_ALIGN_BLOCK:
2205 case LYX_ALIGN_LAYOUT:
2206 case LYX_ALIGN_SPECIAL:
2210 switch (column_info[i].valignment) {
2211 case LYX_VALIGN_TOP:
2214 case LYX_VALIGN_MIDDLE:
2217 case LYX_VALIGN_BOTTOM:
2222 << from_ascii(column_info[i].p_width.asLatexString())
2225 switch (column_info[i].alignment) {
2226 case LYX_ALIGN_LEFT:
2229 case LYX_ALIGN_RIGHT:
2236 } // end if else !column_info[i].p_width
2237 } // end if else !column_info[i].align_special
2238 if (!use_booktabs && columnRightLine(i))
2244 ret += TeXLongtableHeaderFooter(os, runparams);
2246 //+---------------------------------------------------------------------
2247 //+ the single row and columns (cells) +
2248 //+---------------------------------------------------------------------
2250 for (row_type i = 0; i < rowCount(); ++i) {
2251 if (isValidRow(i)) {
2252 ret += TeXRow(os, i, runparams);
2253 if (is_long_tabular && row_info[i].newpage) {
2254 os << "\\newpage\n";
2260 //+---------------------------------------------------------------------
2261 //+ the closing of the tabular +
2262 //+---------------------------------------------------------------------
2264 if (is_long_tabular)
2265 os << "\\end{longtable}";
2267 os << "\\end{tabular}";
2269 os << "\n\\end{sideways}";
2277 int Tabular::docbookRow(odocstream & os, row_type row,
2278 OutputParams const & runparams) const
2281 idx_type cell = getFirstCellInRow(row);
2284 for (col_type j = 0; j < columnCount(); ++j) {
2285 if (isPartOfMultiColumn(row, j))
2288 os << "<entry align=\"";
2289 switch (getAlignment(cell)) {
2290 case LYX_ALIGN_LEFT:
2293 case LYX_ALIGN_RIGHT:
2301 os << "\" valign=\"";
2302 switch (getVAlignment(cell)) {
2303 case LYX_VALIGN_TOP:
2306 case LYX_VALIGN_BOTTOM:
2309 case LYX_VALIGN_MIDDLE:
2314 if (isMultiColumn(cell)) {
2315 os << " namest=\"col" << j << "\" ";
2316 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2320 ret += cellInset(cell)->docbook(os, runparams);
2329 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2333 //+---------------------------------------------------------------------
2334 //+ first the opening preamble +
2335 //+---------------------------------------------------------------------
2337 os << "<tgroup cols=\"" << columnCount()
2338 << "\" colsep=\"1\" rowsep=\"1\">\n";
2340 for (col_type i = 0; i < columnCount(); ++i) {
2341 os << "<colspec colname=\"col" << i << "\" align=\"";
2342 switch (column_info[i].alignment) {
2343 case LYX_ALIGN_LEFT:
2346 case LYX_ALIGN_RIGHT:
2354 if (runparams.flavor == OutputParams::XML)
2360 //+---------------------------------------------------------------------
2361 //+ Long Tabular case +
2362 //+---------------------------------------------------------------------
2364 // output header info
2365 if (haveLTHead() || haveLTFirstHead()) {
2368 for (row_type i = 0; i < rowCount(); ++i) {
2369 if (row_info[i].endhead || row_info[i].endfirsthead) {
2370 ret += docbookRow(os, i, runparams);
2376 // output footer info
2377 if (haveLTFoot() || haveLTLastFoot()) {
2380 for (row_type i = 0; i < rowCount(); ++i) {
2381 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2382 ret += docbookRow(os, i, runparams);
2389 //+---------------------------------------------------------------------
2390 //+ the single row and columns (cells) +
2391 //+---------------------------------------------------------------------
2395 for (row_type i = 0; i < rowCount(); ++i) {
2396 if (isValidRow(i)) {
2397 ret += docbookRow(os, i, runparams);
2402 //+---------------------------------------------------------------------
2403 //+ the closing of the tabular +
2404 //+---------------------------------------------------------------------
2413 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2414 vector<unsigned int> const & clen) const
2416 idx_type const fcell = getFirstCellInRow(row);
2417 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2420 for (idx_type i = fcell; i < n; ++i) {
2430 for (idx_type i = fcell; i < n; ++i) {
2441 col_type column = cellColumn(i);
2442 int len = clen[column];
2443 while (column < columnCount() - 1
2444 && isPartOfMultiColumn(row, ++column))
2445 len += clen[column] + 4;
2446 os << docstring(len, ch);
2461 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2462 vector<unsigned int> const & clen) const
2464 idx_type const fcell = getFirstCellInRow(row);
2465 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2468 for (idx_type i = fcell; i < n; ++i) {
2469 if (bottomLine(i)) {
2478 for (idx_type i = fcell; i < n; ++i) {
2479 if (bottomLine(i)) {
2489 col_type column = cellColumn(i);
2490 int len = clen[column];
2491 while (column < columnCount() -1
2492 && isPartOfMultiColumn(row, ++column))
2493 len += clen[column] + 4;
2494 os << docstring(len, ch);
2495 if (bottomLine(i)) {
2509 void Tabular::plaintextPrintCell(odocstream & os,
2510 OutputParams const & runparams,
2511 idx_type cell, row_type row, col_type column,
2512 vector<unsigned int> const & clen,
2513 bool onlydata) const
2515 odocstringstream sstr;
2516 cellInset(cell)->plaintext(sstr, runparams);
2528 unsigned int len1 = sstr.str().length();
2529 unsigned int len2 = clen[column];
2530 while (column < columnCount() -1
2531 && isPartOfMultiColumn(row, ++column))
2532 len2 += clen[column] + 4;
2535 switch (getAlignment(cell)) {
2537 case LYX_ALIGN_LEFT:
2540 case LYX_ALIGN_RIGHT:
2544 case LYX_ALIGN_CENTER:
2550 os << docstring(len1, ' ') << sstr.str()
2551 << docstring(len2, ' ');
2553 if (rightLine(cell))
2560 void Tabular::plaintext(odocstream & os,
2561 OutputParams const & runparams, int const depth,
2562 bool onlydata, char_type delim) const
2564 // first calculate the width of the single columns
2565 vector<unsigned int> clen(columnCount());
2568 // first all non (real) multicolumn cells!
2569 for (col_type j = 0; j < columnCount(); ++j) {
2571 for (row_type i = 0; i < rowCount(); ++i) {
2572 idx_type cell = cellIndex(i, j);
2573 if (isMultiColumnReal(cell))
2575 odocstringstream sstr;
2576 cellInset(cell)->plaintext(sstr, runparams);
2577 if (clen[j] < sstr.str().length())
2578 clen[j] = sstr.str().length();
2581 // then all (real) multicolumn cells!
2582 for (col_type j = 0; j < columnCount(); ++j) {
2583 for (row_type i = 0; i < rowCount(); ++i) {
2584 idx_type cell = cellIndex(i, j);
2585 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2587 odocstringstream sstr;
2588 cellInset(cell)->plaintext(sstr, runparams);
2589 int len = int(sstr.str().length());
2590 idx_type const n = columnSpan(cell);
2591 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2593 if (len > int(clen[j + n - 1]))
2594 clen[j + n - 1] = len;
2599 for (row_type i = 0; i < rowCount(); ++i) {
2600 if (!onlydata && plaintextTopHLine(os, i, clen))
2601 os << docstring(depth * 2, ' ');
2602 for (col_type j = 0; j < columnCount(); ++j) {
2603 if (isPartOfMultiColumn(i, j))
2605 if (onlydata && j > 0)
2606 // we don't use operator<< for single UCS4 character.
2607 // see explanation in docstream.h
2609 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2614 os << docstring(depth * 2, ' ');
2615 if (plaintextBottomHLine(os, i, clen))
2616 os << docstring(depth * 2, ' ');
2622 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2624 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2628 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2629 col_type column) const
2631 return cell_info[row][column].inset;
2635 void Tabular::setCellInset(row_type row, col_type column,
2636 shared_ptr<InsetTableCell> ins) const
2638 CellData & cd = cell_info[row][column];
2640 // reset the InsetTableCell's pointers
2641 ins->setCellData(&cd);
2642 ins->setTabular(this);
2647 Tabular::cellFromInset(Inset const * inset) const
2649 // is this inset part of the tabular?
2651 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2652 BOOST_ASSERT(false);
2655 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2656 if (cellInset(cell).get() == inset) {
2657 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2658 << "cell=" << cell);
2662 // We should have found a cell at this point
2663 lyxerr << "Tabular::cellFromInset: Cell of inset "
2664 << inset << " not found!" << endl;
2665 BOOST_ASSERT(false);
2671 void Tabular::validate(LaTeXFeatures & features) const
2673 features.require("NeedTabularnewline");
2675 features.require("booktabs");
2676 if (isLongTabular())
2677 features.require("longtable");
2679 features.require("rotating");
2680 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2681 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2682 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2683 features.require("array");
2684 cellInset(cell)->validate(features);
2689 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2691 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2692 ParagraphList::const_iterator cit = parlist.begin();
2693 ParagraphList::const_iterator end = parlist.end();
2695 for (; cit != end; ++cit)
2696 for (int i = 0; i < cit->size(); ++i)
2697 if (cit->isNewline(i))
2704 /////////////////////////////////////////////////////////////////////
2708 /////////////////////////////////////////////////////////////////////
2710 InsetTableCell::InsetTableCell(Buffer const & buf,
2711 Tabular::CellData const * cell, Tabular const * table)
2712 : InsetText(buf), cell_data_(cell), table_(table)
2716 bool InsetTableCell::forceEmptyLayout(idx_type) const
2718 BOOST_ASSERT(table_);
2719 BOOST_ASSERT(cell_data_);
2720 return table_->getPWidth(cell_data_->cellno).zero();
2723 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2725 BOOST_ASSERT(table_);
2726 BOOST_ASSERT(cell_data_);
2727 return !table_->getPWidth(cell_data_->cellno).zero();
2730 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2731 FuncStatus & status) const
2734 switch (cmd.action) {
2736 enabled = !forceEmptyLayout();
2738 case LFUN_LAYOUT_PARAGRAPH:
2739 enabled = allowParagraphCustomization();
2742 return InsetText::getStatus(cur, cmd, status);
2744 status.enabled(enabled);
2748 /////////////////////////////////////////////////////////////////////
2752 /////////////////////////////////////////////////////////////////////
2754 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2756 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2757 rowselect_(false), colselect_(false)
2759 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2763 InsetTabular::InsetTabular(InsetTabular const & tab)
2764 : Inset(tab), tabular(tab.tabular), scx_(0)
2766 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2770 InsetTabular::~InsetTabular()
2772 hideDialogs("tabular", this);
2776 bool InsetTabular::insetAllowed(InsetCode code) const
2778 if (code == MATHMACRO_CODE)
2785 void InsetTabular::write(ostream & os) const
2787 os << "Tabular" << endl;
2792 void InsetTabular::read(Lexer & lex)
2794 bool const old_format = (lex.getString() == "\\LyXTable");
2802 string token = lex.getString();
2803 while (lex && token != "\\end_inset") {
2805 token = lex.getString();
2808 lex.printError("Missing \\end_inset at this point. "
2814 int InsetTabular::rowFromY(Cursor & cur, int y) const
2816 // top y coordinate of tabular
2817 int h = yo(cur.bv()) - tabular.rowAscent(0);
2818 size_t nrows = tabular.rowCount();
2820 for (;r < nrows && y > h; ++r) {
2821 h += tabular.rowAscent(r);
2822 h += tabular.rowDescent(r);
2823 h += tabular.getAdditionalHeight(r);
2829 int InsetTabular::columnFromX(Cursor & cur, int x) const
2831 // left x coordinate of tabular
2832 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2833 size_t ncols = tabular.columnCount();
2835 for (;c < ncols && x > w; ++c) {
2836 w += tabular.columnWidth(c);
2842 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2844 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2845 // mi.base.textwidth << "\n";
2847 lyxerr << "InsetTabular::metrics: need bv" << endl;
2848 BOOST_ASSERT(false);
2852 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2855 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2856 if (tabular.isPartOfMultiColumn(i, j))
2857 // Multicolumn cell, but not first one
2862 if (tabular.cell_info[i][j].multicolumn ==
2863 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2864 p_width = tabular.cellInfo(cell).p_width;
2866 p_width = tabular.column_info[j].p_width;
2867 if (!p_width.zero())
2868 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2869 tabular.cellInset(cell)->metrics(m, dim);
2870 if (!p_width.zero())
2871 dim.wid = m.base.textwidth;
2872 tabular.setCellWidth(cell, dim.wid);
2873 maxAsc = max(maxAsc, dim.asc);
2874 maxDesc = max(maxDesc, dim.des);
2877 int const top_space = tabular.row_info[i].top_space_default ?
2878 default_line_space :
2879 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2880 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2881 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2882 default_line_space :
2883 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2884 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2886 tabular.updateColumnWidths();
2887 dim.asc = tabular.rowAscent(0);
2888 dim.des = tabular.height() - dim.asc;
2889 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2893 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2895 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2896 BufferView * bv = pi.base.bv;
2898 // FIXME: As the full backrgound is painted in drawSelection(),
2899 // we have no choice but to do a full repaint for the Text cells.
2900 pi.full_repaint = true;
2902 resetPos(bv->cursor());
2905 x += ADD_TO_TABULAR_WIDTH;
2907 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2910 first_visible_cell = Tabular::npos;
2911 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2913 int const a = tabular.rowAscent(i);
2914 int const d = tabular.rowDescent(i);
2915 idx = tabular.cellIndex(i, 0);
2916 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2917 if (tabular.isPartOfMultiColumn(i, j))
2919 if (first_visible_cell == Tabular::npos)
2920 first_visible_cell = idx;
2922 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2923 // Cache the Inset position.
2924 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2925 if (nx + tabular.columnWidth(idx) < 0
2926 || nx > bv->workWidth()
2928 || y - a > bv->workHeight()) {
2929 pi.pain.setDrawingEnabled(false);
2930 cell(idx)->draw(pi, cx, y);
2931 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2932 pi.pain.setDrawingEnabled(original_drawing_state);
2934 cell(idx)->draw(pi, cx, y);
2935 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2937 nx += tabular.columnWidth(idx);
2941 if (i + 1 < tabular.rowCount())
2942 y += d + tabular.rowAscent(i + 1) +
2943 tabular.getAdditionalHeight(i + 1);
2948 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2950 Cursor & cur = pi.base.bv->cursor();
2952 x += scx_ + ADD_TO_TABULAR_WIDTH;
2954 // FIXME: it is wrong to completely paint the background
2955 // if we want to do single row painting.
2957 // Paint background of current tabular
2958 int const w = tabular.width();
2959 int const h = tabular.height();
2960 int yy = y - tabular.rowAscent(0);
2961 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2963 if (!cur.selection())
2965 if (&cur.inset() != this)
2971 if (tablemode(cur)) {
2974 getSelection(cur, rs, re, cs, ce);
2975 y -= tabular.rowAscent(0);
2976 for (row_type j = 0; j < tabular.rowCount(); ++j) {
2977 int const a = tabular.rowAscent(j);
2978 int const h = a + tabular.rowDescent(j);
2980 y += tabular.getAdditionalHeight(j);
2981 for (col_type i = 0; i < tabular.columnCount(); ++i) {
2982 if (tabular.isPartOfMultiColumn(j, i))
2984 idx_type const cell =
2985 tabular.cellIndex(j, i);
2986 int const w = tabular.columnWidth(cell);
2987 if (i >= cs && i <= ce && j >= rs && j <= re)
2988 pi.pain.fillRectangle(xx, y, w, h,
2996 x += cellXPos(cur.idx());
2997 x += tabular.getBeginningOfTextInCell(cur.idx());
2998 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3003 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3004 row_type row, idx_type cell, bool erased) const
3006 int x2 = x + tabular.columnWidth(cell);
3007 bool on_off = false;
3008 ColorCode col = Color_tabularline;
3009 ColorCode onoffcol = Color_tabularonoffline;
3012 col = Color_deletedtext;
3013 onoffcol = Color_deletedtext;
3016 if (!tabular.topAlreadyDrawn(cell)) {
3017 on_off = !tabular.topLine(cell);
3018 pain.line(x, y - tabular.rowAscent(row),
3019 x2, y - tabular.rowAscent(row),
3020 on_off ? onoffcol : col,
3021 on_off ? Painter::line_onoffdash : Painter::line_solid);
3023 on_off = !tabular.bottomLine(cell);
3024 pain.line(x, y + tabular.rowDescent(row),
3025 x2, y + tabular.rowDescent(row),
3026 on_off ? onoffcol : col,
3027 on_off ? Painter::line_onoffdash : Painter::line_solid);
3028 if (!tabular.leftAlreadyDrawn(cell)) {
3029 on_off = !tabular.leftLine(cell);
3030 pain.line(x, y - tabular.rowAscent(row),
3031 x, y + tabular.rowDescent(row),
3032 on_off ? onoffcol : col,
3033 on_off ? Painter::line_onoffdash : Painter::line_solid);
3035 on_off = !tabular.rightLine(cell);
3036 pain.line(x2 - tabular.getAdditionalWidth(cell),
3037 y - tabular.rowAscent(row),
3038 x2 - tabular.getAdditionalWidth(cell),
3039 y + tabular.rowDescent(row),
3040 on_off ? onoffcol : col,
3041 on_off ? Painter::line_onoffdash : Painter::line_solid);
3045 docstring InsetTabular::editMessage() const
3047 return _("Opened table");
3051 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3053 //lyxerr << "InsetTabular::edit: " << this << endl;
3055 cur.selection() = false;
3058 if (isRightToLeft(cur))
3059 cur.idx() = tabular.getLastCellInRow(0);
3065 if (isRightToLeft(cur))
3066 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3068 cur.idx() = tabular.cellCount() - 1;
3070 cur.pos() = cur.lastpos(); // FIXME crude guess
3072 // FIXME: this accesses the position cache before it is initialized
3074 //cur.bv().fitCursor();
3078 void InsetTabular::updateLabels(ParIterator const & it)
3080 // In a longtable, tell captions what the current float is
3081 Counters & cnts = buffer().params().documentClass().counters();
3082 string const saveflt = cnts.current_float();
3083 if (tabular.isLongTabular())
3084 cnts.current_float("table");
3086 ParIterator it2 = it;
3088 size_t const end = it2.nargs();
3089 for ( ; it2.idx() < end; it2.top().forwardIdx())
3090 lyx::updateLabels(buffer(), it2);
3093 if (tabular.isLongTabular())
3094 cnts.current_float(saveflt);
3098 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3100 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3101 << "\n cur:" << cur);
3102 CursorSlice sl = cur.top();
3103 Cursor & bvcur = cur.bv().cursor();
3105 switch (cmd.action) {
3107 case LFUN_MOUSE_PRESS: {
3108 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3110 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3111 || cmd.x > xo(cur.bv()) + tabular.width()) {
3112 row_type r = rowFromY(cur, cmd.y);
3113 cur.idx() = tabular.getFirstCellInRow(r);
3116 cur.idx() = tabular.getLastCellInRow(r);
3117 cur.pos() = cur.lastpos();
3118 cur.selection() = true;
3124 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3125 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3126 || cmd.y > y0 + tabular.height()) {
3127 col_type c = columnFromX(cur, cmd.x);
3128 cur.idx() = tabular.cellIndex(0, c);
3131 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3132 cur.pos() = cur.lastpos();
3133 cur.selection() = true;
3138 // do not reset cursor/selection if we have selected
3139 // some cells (bug 2715).
3140 if (cmd.button() == mouse_button::button3
3141 && &bvcur.selBegin().inset() == this
3142 && tablemode(bvcur))
3145 // Let InsetTableCell do it
3146 cell(cur.idx())->dispatch(cur, cmd);
3149 case LFUN_MOUSE_MOTION:
3150 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3151 if (cmd.button() == mouse_button::button1) {
3152 // only accept motions to places not deeper nested than the real anchor
3153 if (!bvcur.anchor_.hasPart(cur)) {
3157 // select (additional) row
3159 row_type r = rowFromY(cur, cmd.y);
3160 cur.idx() = tabular.getLastCellInRow(r);
3161 bvcur.setCursor(cur);
3162 bvcur.selection() = true;
3165 // select (additional) column
3167 col_type c = columnFromX(cur, cmd.x);
3168 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3169 bvcur.setCursor(cur);
3170 bvcur.selection() = true;
3173 // only update if selection changes
3174 if (bvcur.idx() == cur.idx() &&
3175 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3177 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3178 bvcur.setCursor(cur);
3179 bvcur.selection() = true;
3183 case LFUN_MOUSE_RELEASE:
3188 case LFUN_CELL_BACKWARD:
3190 cur.selection() = false;
3193 case LFUN_CELL_FORWARD:
3195 cur.selection() = false;
3197 case LFUN_CHAR_FORWARD_SELECT:
3198 case LFUN_CHAR_FORWARD:
3199 cell(cur.idx())->dispatch(cur, cmd);
3200 if (!cur.result().dispatched()) {
3202 if (sl == cur.top())
3203 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3209 case LFUN_CHAR_BACKWARD_SELECT:
3210 case LFUN_CHAR_BACKWARD:
3211 cell(cur.idx())->dispatch(cur, cmd);
3212 if (!cur.result().dispatched()) {
3214 if (sl == cur.top())
3215 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3221 case LFUN_CHAR_RIGHT_SELECT:
3222 case LFUN_CHAR_RIGHT:
3223 //FIXME: for visual cursor, really move right
3224 if (isRightToLeft(cur))
3225 lyx::dispatch(FuncRequest(
3226 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3227 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3229 lyx::dispatch(FuncRequest(
3230 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3231 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3234 case LFUN_CHAR_LEFT_SELECT:
3235 case LFUN_CHAR_LEFT:
3236 //FIXME: for visual cursor, really move left
3237 if (isRightToLeft(cur))
3238 lyx::dispatch(FuncRequest(
3239 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3240 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3242 lyx::dispatch(FuncRequest(
3243 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3244 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3247 case LFUN_DOWN_SELECT:
3249 cell(cur.idx())->dispatch(cur, cmd);
3250 cur.dispatched(); // override the cell's decision
3251 if (sl == cur.top())
3252 // if our Text didn't do anything to the cursor
3253 // then we try to put the cursor into the cell below
3254 // setting also the right targetX.
3255 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3256 cur.idx() = tabular.cellBelow(cur.idx());
3258 TextMetrics const & tm =
3259 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3260 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3262 if (sl == cur.top()) {
3263 // we trick it to go to forward after leaving the
3265 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3270 case LFUN_UP_SELECT:
3272 cell(cur.idx())->dispatch(cur, cmd);
3273 cur.dispatched(); // override the cell's decision
3274 if (sl == cur.top())
3275 // if our Text didn't do anything to the cursor
3276 // then we try to put the cursor into the cell above
3277 // setting also the right targetX.
3278 if (tabular.cellRow(cur.idx()) != 0) {
3279 cur.idx() = tabular.cellAbove(cur.idx());
3280 cur.pit() = cur.lastpit();
3281 Text const * text = cell(cur.idx())->getText(0);
3282 TextMetrics const & tm = cur.bv().textMetrics(text);
3283 ParagraphMetrics const & pm =
3284 tm.parMetrics(cur.lastpit());
3285 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3287 if (sl == cur.top()) {
3288 cmd = FuncRequest(LFUN_UP);
3293 // case LFUN_SCREEN_DOWN: {
3294 // //if (hasSelection())
3295 // // cur.selection() = false;
3296 // col_type const col = tabular.cellColumn(cur.idx());
3297 // int const t = cur.bv().top_y() + cur.bv().height();
3298 // if (t < yo() + tabular.getHeightOfTabular()) {
3299 // cur.bv().scrollDocView(t);
3300 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3302 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3309 // case LFUN_SCREEN_UP: {
3310 // //if (hasSelection())
3311 // // cur.selection() = false;
3312 // col_type const col = tabular.cellColumn(cur.idx());
3313 // int const t = cur.bv().top_y() + cur.bv().height();
3315 // cur.bv().scrollDocView(t);
3319 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3323 // cur.par() = cur.lastpar();
3324 // cur.pos() = cur.lastpos();
3328 case LFUN_LAYOUT_TABULAR:
3329 cur.bv().showDialog("tabular", params2string(*this), this);
3332 case LFUN_INSET_DIALOG_UPDATE:
3333 cur.bv().updateDialog("tabular", params2string(*this));
3336 case LFUN_TABULAR_FEATURE:
3337 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3341 // insert file functions
3342 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3343 case LFUN_FILE_INSERT_PLAINTEXT: {
3345 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3346 FileName(to_utf8(cmd.argument())));
3349 cur.recordUndoInset(INSERT_UNDO);
3350 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3351 // content has been replaced,
3352 // so cursor might be invalid
3353 cur.pos() = cur.lastpos();
3354 cur.pit() = cur.lastpit();
3355 bvcur.setCursor(cur);
3362 if (tablemode(cur)) {
3363 if (copySelection(cur)) {
3364 cur.recordUndoInset(DELETE_UNDO);
3369 cell(cur.idx())->dispatch(cur, cmd);
3372 case LFUN_CHAR_DELETE_BACKWARD:
3373 case LFUN_CHAR_DELETE_FORWARD:
3374 if (tablemode(cur)) {
3375 cur.recordUndoInset(DELETE_UNDO);
3379 cell(cur.idx())->dispatch(cur, cmd);
3383 if (!cur.selection())
3385 if (tablemode(cur)) {
3389 cell(cur.idx())->dispatch(cur, cmd);
3392 case LFUN_CLIPBOARD_PASTE:
3393 case LFUN_PRIMARY_SELECTION_PASTE: {
3394 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3395 theClipboard().getAsText() :
3396 theSelection().get();
3399 // pass to InsertPlaintextString, but
3400 // only if we have multi-cell content
3401 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3402 cur.recordUndoInset(INSERT_UNDO);
3403 if (insertPlaintextString(cur.bv(), clip, false)) {
3404 // content has been replaced,
3405 // so cursor might be invalid
3406 cur.pos() = cur.lastpos();
3407 cur.pit() = cur.lastpit();
3408 bvcur.setCursor(cur);
3412 // Let the cell handle normal text
3413 cell(cur.idx())->dispatch(cur, cmd);
3418 if (tabularStackDirty() && theClipboard().isInternal() ||
3419 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3420 cur.recordUndoInset(INSERT_UNDO);
3421 pasteClipboard(cur);
3424 cell(cur.idx())->dispatch(cur, cmd);
3427 case LFUN_FONT_EMPH:
3428 case LFUN_FONT_BOLD:
3429 case LFUN_FONT_ROMAN:
3430 case LFUN_FONT_NOUN:
3431 case LFUN_FONT_ITAL:
3432 case LFUN_FONT_FRAK:
3433 case LFUN_FONT_TYPEWRITER:
3434 case LFUN_FONT_SANS:
3435 case LFUN_FONT_FREE_APPLY:
3436 case LFUN_FONT_FREE_UPDATE:
3437 case LFUN_FONT_SIZE:
3438 case LFUN_FONT_UNDERLINE:
3440 case LFUN_WORD_CAPITALIZE:
3441 case LFUN_WORD_UPCASE:
3442 case LFUN_WORD_LOWCASE:
3443 case LFUN_CHARS_TRANSPOSE:
3444 if (tablemode(cur)) {
3447 getSelection(cur, rs, re, cs, ce);
3448 Cursor tmpcur = cur;
3449 for (row_type i = rs; i <= re; ++i) {
3450 for (col_type j = cs; j <= ce; ++j) {
3451 // cursor follows cell:
3452 tmpcur.idx() = tabular.cellIndex(i, j);
3453 // select this cell only:
3456 tmpcur.resetAnchor();
3457 tmpcur.pit() = tmpcur.lastpit();
3458 tmpcur.pos() = tmpcur.top().lastpos();
3459 tmpcur.setCursor(tmpcur);
3460 tmpcur.setSelection();
3461 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3466 cell(cur.idx())->dispatch(cur, cmd);
3470 // we try to handle this event in the insets dispatch function.
3471 cell(cur.idx())->dispatch(cur, cmd);
3477 // function sets an object as defined in func_status.h:
3478 // states OK, Unknown, Disabled, On, Off.
3479 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3480 FuncStatus & status) const
3482 switch (cmd.action) {
3483 case LFUN_TABULAR_FEATURE: {
3484 int action = Tabular::LAST_ACTION;
3486 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3487 string const tmp = tabularFeature[i].feature;
3488 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3489 action = tabularFeature[i].action;
3493 if (action == Tabular::LAST_ACTION) {
3495 status.unknown(true);
3499 string const argument
3500 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3502 row_type sel_row_start = 0;
3503 row_type sel_row_end = 0;
3504 col_type sel_col_start = 0;
3505 col_type sel_col_end = 0;
3506 Tabular::ltType dummyltt;
3509 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3512 case Tabular::SET_PWIDTH:
3513 case Tabular::SET_MPWIDTH:
3514 case Tabular::SET_SPECIAL_COLUMN:
3515 case Tabular::SET_SPECIAL_MULTI:
3516 case Tabular::APPEND_ROW:
3517 case Tabular::APPEND_COLUMN:
3518 case Tabular::DELETE_ROW:
3519 case Tabular::DELETE_COLUMN:
3520 case Tabular::COPY_ROW:
3521 case Tabular::COPY_COLUMN:
3522 case Tabular::SET_ALL_LINES:
3523 case Tabular::UNSET_ALL_LINES:
3524 case Tabular::SET_TOP_SPACE:
3525 case Tabular::SET_BOTTOM_SPACE:
3526 case Tabular::SET_INTERLINE_SPACE:
3527 case Tabular::SET_BORDER_LINES:
3531 case Tabular::MULTICOLUMN:
3532 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3535 case Tabular::TOGGLE_LINE_TOP:
3536 status.setOnOff(tabular.topLine(cur.idx()));
3539 case Tabular::TOGGLE_LINE_BOTTOM:
3540 status.setOnOff(tabular.bottomLine(cur.idx()));
3543 case Tabular::TOGGLE_LINE_LEFT:
3544 status.setOnOff(tabular.leftLine(cur.idx()));
3547 case Tabular::TOGGLE_LINE_RIGHT:
3548 status.setOnOff(tabular.rightLine(cur.idx()));
3551 case Tabular::M_ALIGN_LEFT:
3553 case Tabular::ALIGN_LEFT:
3554 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3557 case Tabular::M_ALIGN_RIGHT:
3559 case Tabular::ALIGN_RIGHT:
3560 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3563 case Tabular::M_ALIGN_CENTER:
3565 case Tabular::ALIGN_CENTER:
3566 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3569 case Tabular::ALIGN_BLOCK:
3570 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3571 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3574 case Tabular::M_VALIGN_TOP:
3576 case Tabular::VALIGN_TOP:
3578 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3581 case Tabular::M_VALIGN_BOTTOM:
3583 case Tabular::VALIGN_BOTTOM:
3585 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3588 case Tabular::M_VALIGN_MIDDLE:
3590 case Tabular::VALIGN_MIDDLE:
3592 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3595 case Tabular::SET_LONGTABULAR:
3596 status.setOnOff(tabular.isLongTabular());
3599 case Tabular::UNSET_LONGTABULAR:
3600 status.setOnOff(!tabular.isLongTabular());
3603 case Tabular::TOGGLE_ROTATE_TABULAR:
3604 case Tabular::SET_ROTATE_TABULAR:
3605 status.setOnOff(tabular.getRotateTabular());
3608 case Tabular::UNSET_ROTATE_TABULAR:
3609 status.setOnOff(!tabular.getRotateTabular());
3612 case Tabular::TOGGLE_ROTATE_CELL:
3613 case Tabular::SET_ROTATE_CELL:
3614 status.setOnOff(!oneCellHasRotationState(false,
3615 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3618 case Tabular::UNSET_ROTATE_CELL:
3619 status.setOnOff(!oneCellHasRotationState(true,
3620 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3623 case Tabular::SET_USEBOX:
3624 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3627 case Tabular::SET_LTFIRSTHEAD:
3628 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3631 case Tabular::UNSET_LTFIRSTHEAD:
3632 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3635 case Tabular::SET_LTHEAD:
3636 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3639 case Tabular::UNSET_LTHEAD:
3640 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3643 case Tabular::SET_LTFOOT:
3644 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3647 case Tabular::UNSET_LTFOOT:
3648 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3651 case Tabular::SET_LTLASTFOOT:
3652 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3655 case Tabular::UNSET_LTLASTFOOT:
3656 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3659 case Tabular::SET_LTNEWPAGE:
3660 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3663 case Tabular::SET_BOOKTABS:
3664 status.setOnOff(tabular.useBookTabs());
3667 case Tabular::UNSET_BOOKTABS:
3668 status.setOnOff(!tabular.useBookTabs());
3673 status.enabled(false);
3679 // These are only enabled inside tabular
3680 case LFUN_CELL_BACKWARD:
3681 case LFUN_CELL_FORWARD:
3682 status.enabled(true);
3685 // disable these with multiple cells selected
3686 case LFUN_INSET_INSERT:
3687 case LFUN_TABULAR_INSERT:
3688 case LFUN_FLEX_INSERT:
3689 case LFUN_FLOAT_INSERT:
3690 case LFUN_FLOAT_WIDE_INSERT:
3691 case LFUN_FOOTNOTE_INSERT:
3692 case LFUN_MARGINALNOTE_INSERT:
3693 case LFUN_MATH_INSERT:
3694 case LFUN_MATH_MODE:
3695 case LFUN_MATH_MUTATE:
3696 case LFUN_MATH_DISPLAY:
3697 case LFUN_NOTE_INSERT:
3698 case LFUN_OPTIONAL_INSERT:
3699 case LFUN_BOX_INSERT:
3700 case LFUN_BRANCH_INSERT:
3701 case LFUN_WRAP_INSERT:
3702 case LFUN_ERT_INSERT: {
3703 if (tablemode(cur)) {
3704 status.enabled(false);
3707 return cell(cur.idx())->getStatus(cur, cmd, status);
3710 // disable in non-fixed-width cells
3711 case LFUN_NEWLINE_INSERT:
3712 case LFUN_BREAK_PARAGRAPH:
3713 case LFUN_BREAK_PARAGRAPH_SKIP: {
3714 if (tabular.getPWidth(cur.idx()).zero()) {
3715 status.enabled(false);
3718 return cell(cur.idx())->getStatus(cur, cmd, status);
3722 if (tabularStackDirty() && theClipboard().isInternal()) {
3723 status.enabled(true);
3726 return cell(cur.idx())->getStatus(cur, cmd, status);
3728 case LFUN_INSET_MODIFY:
3729 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3730 status.enabled(true);
3736 // we try to handle this event in the insets dispatch function.
3737 return cell(cur.idx())->getStatus(cur, cmd, status);
3742 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3744 return tabular.latex(os, runparams);
3748 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3750 os << '\n'; // output table on a new line
3751 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3752 tabular.plaintext(os, runparams, dp, false, 0);
3753 return PLAINTEXT_NEWLINE;
3757 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3762 // FIXME: Why not pass a proper DocIterator here?
3764 // if the table is inside a float it doesn't need the informaltable
3765 // wrapper. Search for it.
3766 for (master = owner(); master; master = master->owner())
3767 if (master->lyxCode() == FLOAT_CODE)
3772 os << "<informaltable>";
3775 ret += tabular.docbook(os, runparams);
3777 os << "</informaltable>";
3784 void InsetTabular::validate(LaTeXFeatures & features) const
3786 tabular.validate(features);
3790 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3792 return tabular.cellInset(idx);
3796 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3798 return tabular.cellInset(idx);
3802 void InsetTabular::cursorPos(BufferView const & bv,
3803 CursorSlice const & sl, bool boundary, int & x, int & y) const
3805 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3807 // y offset correction
3808 int const row = tabular.cellRow(sl.idx());
3809 for (int i = 0; i <= row; ++i) {
3811 y += tabular.rowAscent(i);
3812 y += tabular.getAdditionalHeight(i);
3815 y += tabular.rowDescent(i);
3818 // x offset correction
3819 int const col = tabular.cellColumn(sl.idx());
3820 int idx = tabular.cellIndex(row, 0);
3821 for (int j = 0; j < col; ++j) {
3822 if (tabular.isPartOfMultiColumn(row, j))
3824 x += tabular.columnWidth(idx);
3827 x += tabular.getBeginningOfTextInCell(idx);
3828 x += ADD_TO_TABULAR_WIDTH;
3833 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3837 Inset const & inset = *tabular.cellInset(cell);
3838 Point o = bv.coordCache().getInsets().xy(&inset);
3839 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3840 int const xend = xbeg + tabular.columnWidth(cell);
3841 row_type const row = tabular.cellRow(cell);
3842 int const ybeg = o.y_ - tabular.rowAscent(row) -
3843 tabular.getAdditionalHeight(row);
3844 int const yend = o.y_ + tabular.rowDescent(row);
3856 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3857 // << " ybeg=" << ybeg << " yend=" << yend
3858 // << " xx=" << xx << " yy=" << yy
3859 // << " dist=" << xx + yy << endl;
3864 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3866 //lyxerr << "InsetTabular::editXY: " << this << endl;
3867 cur.selection() = false;
3869 cur.idx() = getNearestCell(cur.bv(), x, y);
3871 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3875 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3877 cur.idx() = getNearestCell(cur.bv(), x, y);
3878 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3882 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3884 idx_type idx_min = 0;
3885 int dist_min = numeric_limits<int>::max();
3886 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3887 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3888 int const d = dist(bv, i, x, y);
3899 int InsetTabular::cellXPos(idx_type const cell) const
3903 for (; !tabular.isFirstCellInRow(c); --c)
3906 for (; c < cell; ++c)
3907 lx += tabular.columnWidth(c);
3913 void InsetTabular::resetPos(Cursor & cur) const
3915 BufferView & bv = cur.bv();
3916 int const maxwidth = bv.workWidth();
3918 if (&cur.inset() != this) {
3922 int const X2 = maxwidth;
3923 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3924 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3925 int const x2 = x1 + tabular.columnWidth(cur.idx());
3928 scx_ = X1 + 20 - x1;
3930 scx_ = X2 - 20 - x2;
3935 cur.updateFlags(Update::Force | Update::FitCursor);
3939 void InsetTabular::moveNextCell(Cursor & cur)
3941 if (isRightToLeft(cur)) {
3942 if (tabular.isFirstCellInRow(cur.idx())) {
3943 row_type const row = tabular.cellRow(cur.idx());
3944 if (row == tabular.rowCount() - 1)
3946 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3953 if (tabular.isLastCell(cur.idx()))
3963 void InsetTabular::movePrevCell(Cursor & cur)
3965 if (isRightToLeft(cur)) {
3966 if (tabular.isLastCellInRow(cur.idx())) {
3967 row_type const row = tabular.cellRow(cur.idx());
3970 cur.idx() = tabular.getFirstCellInRow(row);
3971 cur.idx() = tabular.cellAbove(cur.idx());
3973 if (tabular.isLastCell(cur.idx()))
3978 if (cur.idx() == 0) // first cell
3982 cur.pit() = cur.lastpit();
3983 cur.pos() = cur.lastpos();
3985 // FIXME: this accesses the position cache before it is initialized
3990 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
3992 Tabular::Feature action = Tabular::LAST_ACTION;
3995 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3996 string const tmp = tabularFeature[i].feature;
3998 if (tmp == what.substr(0, tmp.length())) {
3999 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4000 //tabularFeatures[i].feature.length()))
4001 action = tabularFeature[i].action;
4005 if (action == Tabular::LAST_ACTION)
4009 ltrim(what.substr(tabularFeature[i].feature.length()));
4010 tabularFeatures(cur, action, val);
4015 static void checkLongtableSpecial(Tabular::ltType & ltt,
4016 string const & special, bool & flag)
4018 if (special == "dl_above") {
4021 } else if (special == "dl_below") {
4022 ltt.bottomDL = flag;
4024 } else if (special == "empty") {
4033 bool InsetTabular::oneCellHasRotationState(bool rotated,
4034 row_type row_start, row_type row_end,
4035 col_type col_start, col_type col_end) const {
4037 for (row_type i = row_start; i <= row_end; ++i) {
4038 for (col_type j = col_start; j <= col_end; ++j) {
4039 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4048 void InsetTabular::tabularFeatures(Cursor & cur,
4049 Tabular::Feature feature, string const & value)
4051 col_type sel_col_start;
4052 col_type sel_col_end;
4053 row_type sel_row_start;
4054 row_type sel_row_end;
4055 bool setLines = false;
4056 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4057 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4061 case Tabular::M_ALIGN_LEFT:
4062 case Tabular::ALIGN_LEFT:
4063 setAlign = LYX_ALIGN_LEFT;
4066 case Tabular::M_ALIGN_RIGHT:
4067 case Tabular::ALIGN_RIGHT:
4068 setAlign = LYX_ALIGN_RIGHT;
4071 case Tabular::M_ALIGN_CENTER:
4072 case Tabular::ALIGN_CENTER:
4073 setAlign = LYX_ALIGN_CENTER;
4076 case Tabular::ALIGN_BLOCK:
4077 setAlign = LYX_ALIGN_BLOCK;
4080 case Tabular::M_VALIGN_TOP:
4081 case Tabular::VALIGN_TOP:
4082 setVAlign = Tabular::LYX_VALIGN_TOP;
4085 case Tabular::M_VALIGN_BOTTOM:
4086 case Tabular::VALIGN_BOTTOM:
4087 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4090 case Tabular::M_VALIGN_MIDDLE:
4091 case Tabular::VALIGN_MIDDLE:
4092 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4099 cur.recordUndoInset(ATOMIC_UNDO);
4101 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4102 row_type const row = tabular.cellRow(cur.idx());
4103 col_type const column = tabular.cellColumn(cur.idx());
4105 Tabular::ltType ltt;
4109 case Tabular::SET_PWIDTH: {
4110 Length const len(value);
4111 tabular.setColumnPWidth(cur, cur.idx(), len);
4113 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4114 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4118 case Tabular::SET_MPWIDTH:
4119 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4122 case Tabular::SET_SPECIAL_COLUMN:
4123 case Tabular::SET_SPECIAL_MULTI:
4124 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4127 case Tabular::APPEND_ROW:
4128 // append the row into the tabular
4129 tabular.appendRow(cur.idx());
4132 case Tabular::APPEND_COLUMN:
4133 // append the column into the tabular
4134 tabular.appendColumn(cur.idx());
4135 cur.idx() = tabular.cellIndex(row, column);
4138 case Tabular::DELETE_ROW:
4139 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4140 tabular.deleteRow(sel_row_start);
4141 if (sel_row_start >= tabular.rowCount())
4143 cur.idx() = tabular.cellIndex(sel_row_start, column);
4146 cur.selection() = false;
4149 case Tabular::DELETE_COLUMN:
4150 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4151 tabular.deleteColumn(sel_col_start);
4152 if (sel_col_start >= tabular.columnCount())
4154 cur.idx() = tabular.cellIndex(row, sel_col_start);
4157 cur.selection() = false;
4160 case Tabular::COPY_ROW:
4161 tabular.copyRow(row);
4164 case Tabular::COPY_COLUMN:
4165 tabular.copyColumn(column);
4166 cur.idx() = tabular.cellIndex(row, column);
4169 case Tabular::TOGGLE_LINE_TOP: {
4170 bool lineSet = !tabular.topLine(cur.idx());
4171 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4172 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4173 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4177 case Tabular::TOGGLE_LINE_BOTTOM: {
4178 bool lineSet = !tabular.bottomLine(cur.idx());
4179 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4180 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4181 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4185 case Tabular::TOGGLE_LINE_LEFT: {
4186 bool lineSet = !tabular.leftLine(cur.idx());
4187 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4188 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4189 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4193 case Tabular::TOGGLE_LINE_RIGHT: {
4194 bool lineSet = !tabular.rightLine(cur.idx());
4195 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4196 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4197 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4201 case Tabular::M_ALIGN_LEFT:
4202 case Tabular::M_ALIGN_RIGHT:
4203 case Tabular::M_ALIGN_CENTER:
4205 case Tabular::ALIGN_LEFT:
4206 case Tabular::ALIGN_RIGHT:
4207 case Tabular::ALIGN_CENTER:
4208 case Tabular::ALIGN_BLOCK:
4209 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4210 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4211 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4214 case Tabular::M_VALIGN_TOP:
4215 case Tabular::M_VALIGN_BOTTOM:
4216 case Tabular::M_VALIGN_MIDDLE:
4218 case Tabular::VALIGN_TOP:
4219 case Tabular::VALIGN_BOTTOM:
4220 case Tabular::VALIGN_MIDDLE:
4221 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4222 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4223 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4226 case Tabular::MULTICOLUMN: {
4227 if (sel_row_start != sel_row_end) {
4228 // FIXME: Need I say it ? This is horrible.
4230 Alert::error(_("Error setting multicolumn"),
4231 _("You cannot set multicolumn vertically."));
4234 if (!cur.selection()) {
4235 // just multicol for one single cell
4236 // check whether we are completely in a multicol
4237 if (tabular.isMultiColumn(cur.idx()))
4238 tabular.unsetMultiColumn(cur.idx());
4240 tabular.setMultiColumn(cur.idx(), 1);
4243 // we have a selection so this means we just add all this
4244 // cells to form a multicolumn cell
4245 idx_type const s_start = cur.selBegin().idx();
4246 idx_type const s_end = cur.selEnd().idx();
4247 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4248 cur.idx() = s_start;
4251 cur.selection() = false;
4255 case Tabular::SET_ALL_LINES:
4257 case Tabular::UNSET_ALL_LINES:
4258 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4259 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4260 tabular.setAllLines(
4261 tabular.cellIndex(i,j), setLines);
4264 case Tabular::SET_BORDER_LINES:
4265 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4266 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4267 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4270 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4271 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4275 case Tabular::SET_LONGTABULAR:
4276 tabular.setLongTabular(true);
4279 case Tabular::UNSET_LONGTABULAR:
4280 tabular.setLongTabular(false);
4283 case Tabular::SET_ROTATE_TABULAR:
4284 tabular.setRotateTabular(true);
4287 case Tabular::UNSET_ROTATE_TABULAR:
4288 tabular.setRotateTabular(false);
4291 case Tabular::TOGGLE_ROTATE_TABULAR:
4292 tabular.setRotateTabular(!tabular.getRotateTabular());
4295 case Tabular::SET_ROTATE_CELL:
4296 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4297 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4298 tabular.setRotateCell(
4299 tabular.cellIndex(i, j), true);
4302 case Tabular::UNSET_ROTATE_CELL:
4303 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4304 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4305 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4308 case Tabular::TOGGLE_ROTATE_CELL:
4310 bool oneNotRotated = oneCellHasRotationState(false,
4311 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
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(tabular.cellIndex(i, j),
4320 case Tabular::SET_USEBOX: {
4321 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4322 if (val == tabular.getUsebox(cur.idx()))
4323 val = Tabular::BOX_NONE;
4324 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4325 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4326 tabular.setUsebox(tabular.cellIndex(i, j), val);
4330 case Tabular::UNSET_LTFIRSTHEAD:
4332 case Tabular::SET_LTFIRSTHEAD:
4333 tabular.getRowOfLTFirstHead(row, ltt);
4334 checkLongtableSpecial(ltt, value, flag);
4335 tabular.setLTHead(row, flag, ltt, true);
4338 case Tabular::UNSET_LTHEAD:
4340 case Tabular::SET_LTHEAD:
4341 tabular.getRowOfLTHead(row, ltt);
4342 checkLongtableSpecial(ltt, value, flag);
4343 tabular.setLTHead(row, flag, ltt, false);
4346 case Tabular::UNSET_LTFOOT:
4348 case Tabular::SET_LTFOOT:
4349 tabular.getRowOfLTFoot(row, ltt);
4350 checkLongtableSpecial(ltt, value, flag);
4351 tabular.setLTFoot(row, flag, ltt, false);
4354 case Tabular::UNSET_LTLASTFOOT:
4356 case Tabular::SET_LTLASTFOOT:
4357 tabular.getRowOfLTLastFoot(row, ltt);
4358 checkLongtableSpecial(ltt, value, flag);
4359 tabular.setLTFoot(row, flag, ltt, true);
4362 case Tabular::SET_LTNEWPAGE:
4363 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4366 case Tabular::SET_BOOKTABS:
4367 tabular.setBookTabs(true);
4370 case Tabular::UNSET_BOOKTABS:
4371 tabular.setBookTabs(false);
4374 case Tabular::SET_TOP_SPACE: {
4376 if (value == "default")
4377 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4378 tabular.row_info[i].top_space_default = true;
4379 else if (isValidLength(value, &len))
4380 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4381 tabular.row_info[i].top_space_default = false;
4382 tabular.row_info[i].top_space = len;
4385 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4386 tabular.row_info[i].top_space_default = false;
4387 tabular.row_info[i].top_space = len;
4392 case Tabular::SET_BOTTOM_SPACE: {
4394 if (value == "default")
4395 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4396 tabular.row_info[i].bottom_space_default = true;
4397 else if (isValidLength(value, &len))
4398 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4399 tabular.row_info[i].bottom_space_default = false;
4400 tabular.row_info[i].bottom_space = len;
4403 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4404 tabular.row_info[i].bottom_space_default = false;
4405 tabular.row_info[i].bottom_space = len;
4410 case Tabular::SET_INTERLINE_SPACE: {
4412 if (value == "default")
4413 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4414 tabular.row_info[i].interline_space_default = true;
4415 else if (isValidLength(value, &len))
4416 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4417 tabular.row_info[i].interline_space_default = false;
4418 tabular.row_info[i].interline_space = len;
4421 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4422 tabular.row_info[i].interline_space_default = false;
4423 tabular.row_info[i].interline_space = len;
4428 // dummy stuff just to avoid warnings
4429 case Tabular::LAST_ACTION:
4435 bool InsetTabular::showInsetDialog(BufferView * bv) const
4437 bv->showDialog("tabular", params2string(*this),
4438 const_cast<InsetTabular *>(this));
4443 void InsetTabular::openLayoutDialog(BufferView * bv) const
4445 bv->showDialog("tabular", params2string(*this),
4446 const_cast<InsetTabular *>(this));
4450 bool InsetTabular::copySelection(Cursor & cur)
4452 if (!cur.selection())
4457 getSelection(cur, rs, re, cs, ce);
4459 paste_tabular.reset(new Tabular(tabular));
4461 for (row_type i = 0; i < rs; ++i)
4462 paste_tabular->deleteRow(0);
4464 row_type const rows = re - rs + 1;
4465 while (paste_tabular->rowCount() > rows)
4466 paste_tabular->deleteRow(rows);
4468 for (col_type i = 0; i < cs; ++i)
4469 paste_tabular->deleteColumn(0);
4471 col_type const columns = ce - cs + 1;
4472 while (paste_tabular->columnCount() > columns)
4473 paste_tabular->deleteColumn(columns);
4475 // We clear all the InsetTableCell pointers, since they
4476 // might now become invalid and there is no point in having
4477 // them point to temporary things in paste_tabular.
4478 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4479 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4480 paste_tabular->cellInset(i,j)->setCellData(0);
4481 paste_tabular->cellInset(i,j)->setTabular(0);
4484 odocstringstream os;
4485 OutputParams const runparams(0);
4486 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4487 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4488 cap::copySelection(cur, os.str());
4490 // mark tabular stack dirty
4491 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4492 // when we (hopefully) have a one-for-all paste mechanism.
4493 // This must be called after cap::copySelection.
4494 dirtyTabularStack(true);
4500 bool InsetTabular::pasteClipboard(Cursor & cur)
4504 col_type const actcol = tabular.cellColumn(cur.idx());
4505 row_type const actrow = tabular.cellRow(cur.idx());
4506 for (row_type r1 = 0, r2 = actrow;
4507 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4509 for (col_type c1 = 0, c2 = actcol;
4510 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4512 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4513 tabular.isPartOfMultiColumn(r2, c2))
4515 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4519 if (tabular.isPartOfMultiColumn(r2, c2)) {
4523 shared_ptr<InsetTableCell> inset(
4524 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4525 // note that setCellInset will call InsetTableCell::setCellData()
4526 // and InsetTableCell::setTabular()
4527 tabular.setCellInset(r2, c2, inset);
4528 // FIXME: change tracking (MG)
4529 inset->setChange(Change(cur.buffer().params().trackChanges ?
4530 Change::INSERTED : Change::UNCHANGED));
4538 void InsetTabular::cutSelection(Cursor & cur)
4540 if (!cur.selection())
4545 getSelection(cur, rs, re, cs, ce);
4546 for (row_type i = rs; i <= re; ++i) {
4547 for (col_type j = cs; j <= ce; ++j) {
4548 shared_ptr<InsetTableCell> t
4549 = cell(tabular.cellIndex(i, j));
4550 if (cur.buffer().params().trackChanges)
4551 // FIXME: Change tracking (MG)
4552 t->setChange(Change(Change::DELETED));
4558 // cursor position might be invalid now
4559 if (cur.pit() > cur.lastpit())
4560 cur.pit() = cur.lastpit();
4561 if (cur.pos() > cur.lastpos())
4562 cur.pos() = cur.lastpos();
4563 cur.clearSelection();
4567 bool InsetTabular::isRightToLeft(Cursor & cur) const
4569 BOOST_ASSERT(cur.depth() > 1);
4570 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4571 pos_type const parentpos = cur[cur.depth() - 2].pos();
4572 return parentpar.getFontSettings(cur.bv().buffer().params(),
4573 parentpos).language()->rightToLeft();
4577 void InsetTabular::getSelection(Cursor & cur,
4578 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4580 CursorSlice const & beg = cur.selBegin();
4581 CursorSlice const & end = cur.selEnd();
4582 cs = tabular.cellColumn(beg.idx());
4583 ce = tabular.cellColumn(end.idx());
4586 cs = tabular.cellColumn(end.idx());
4588 ce = tabular.cellRightColumn(end.idx());
4591 rs = tabular.cellRow(beg.idx());
4592 re = tabular.cellRow(end.idx());
4598 Text * InsetTabular::getText(int idx) const
4600 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4604 void InsetTabular::setChange(Change const & change)
4606 for (idx_type idx = 0; idx < nargs(); ++idx)
4607 cell(idx)->setChange(change);
4611 void InsetTabular::acceptChanges(BufferParams const & bparams)
4613 for (idx_type idx = 0; idx < nargs(); ++idx)
4614 cell(idx)->acceptChanges(bparams);
4618 void InsetTabular::rejectChanges(BufferParams const & bparams)
4620 for (idx_type idx = 0; idx < nargs(); ++idx)
4621 cell(idx)->rejectChanges(bparams);
4625 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4627 return tabular.getPWidth(cell).zero();
4631 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4633 return !tabular.getPWidth(cell).zero();
4637 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4640 if (buf.length() <= 0)
4645 col_type maxCols = 1;
4646 size_t const len = buf.length();
4650 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4658 maxCols = max(cols, maxCols);
4664 maxCols = max(cols, maxCols);
4670 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4671 loctab = paste_tabular.get();
4673 dirtyTabularStack(true);
4676 cell = bv.cursor().idx();
4677 ocol = tabular.cellColumn(cell);
4678 row = tabular.cellRow(cell);
4682 idx_type const cells = loctab->cellCount();
4685 rows = loctab->rowCount();
4686 col_type const columns = loctab->columnCount();
4688 while (cell < cells && p < len && row < rows &&
4689 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4695 // we can only set this if we are not too far right
4696 if (cols < columns) {
4697 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4698 Font const font = bv.textMetrics(&inset->text_).
4700 inset->setText(buf.substr(op, p - op), font,
4701 buffer().params().trackChanges);
4707 // we can only set this if we are not too far right
4708 if (cols < columns) {
4709 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4710 Font const font = bv.textMetrics(&inset->text_).
4712 inset->setText(buf.substr(op, p - op), font,
4713 buffer().params().trackChanges);
4718 cell = loctab->cellIndex(row, cols);
4724 // check for the last cell if there is no trailing '\n'
4725 if (cell < cells && op < len) {
4726 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4727 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4728 inset->setText(buf.substr(op, len - op), font,
4729 buffer().params().trackChanges);
4735 void InsetTabular::addPreview(PreviewLoader & loader) const
4737 row_type const rows = tabular.rowCount();
4738 col_type const columns = tabular.columnCount();
4739 for (row_type i = 0; i < rows; ++i) {
4740 for (col_type j = 0; j < columns; ++j)
4741 tabular.cellInset(i, j)->addPreview(loader);
4746 bool InsetTabular::tablemode(Cursor & cur) const
4748 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4752 bool InsetTabular::completionSupported(Cursor const & cur) const
4754 Cursor const & bvCur = cur.bv().cursor();
4755 if (&bvCur.inset() != this)
4757 return cur.text()->completionSupported(cur);
4761 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4763 return completionSupported(cur);
4767 bool InsetTabular::automaticInlineCompletion() const
4769 return lyxrc.completion_inline_text;
4773 bool InsetTabular::automaticPopupCompletion() const
4775 return lyxrc.completion_popup_text;
4779 bool InsetTabular::showCompletionCursor() const
4781 return lyxrc.completion_cursor_text;
4785 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4787 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4791 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4793 if (!completionSupported(cur))
4795 return cur.text()->completionPrefix(cur);
4799 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4801 if (!completionSupported(cur))
4804 return cur.text()->insertCompletion(cur, s, finished);
4808 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4809 Dimension & dim) const
4811 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4812 tm.completionPosAndDim(cur, x, y, dim);
4816 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4818 istringstream data(in);
4820 lex.setStream(data);
4827 if (!lex || token != "tabular") {
4828 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4832 // This is part of the inset proper that is usually swallowed
4833 // by Buffer::readInset
4835 if (!lex || token != "Tabular") {
4836 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4844 string InsetTabular::params2string(InsetTabular const & inset)
4847 data << "tabular" << ' ';
4849 data << "\\end_inset\n";