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/assert.h"
54 #include "support/convert.h"
55 #include "support/debug.h"
56 #include "support/docstream.h"
57 #include "support/FileName.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
61 #include <boost/scoped_ptr.hpp>
69 using namespace lyx::support;
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
106 TabularFeature tabularFeature[] =
108 { Tabular::APPEND_ROW, "append-row" },
109 { Tabular::APPEND_COLUMN, "append-column" },
110 { Tabular::DELETE_ROW, "delete-row" },
111 { Tabular::DELETE_COLUMN, "delete-column" },
112 { Tabular::COPY_ROW, "copy-row" },
113 { Tabular::COPY_COLUMN, "copy-column" },
114 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
115 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
116 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
117 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
118 { Tabular::ALIGN_LEFT, "align-left" },
119 { Tabular::ALIGN_RIGHT, "align-right" },
120 { Tabular::ALIGN_CENTER, "align-center" },
121 { Tabular::ALIGN_BLOCK, "align-block" },
122 { Tabular::VALIGN_TOP, "valign-top" },
123 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
124 { Tabular::VALIGN_MIDDLE, "valign-middle" },
125 { Tabular::M_ALIGN_LEFT, "m-align-left" },
126 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
127 { Tabular::M_ALIGN_CENTER, "m-align-center" },
128 { Tabular::M_VALIGN_TOP, "m-valign-top" },
129 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
130 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
131 { Tabular::MULTICOLUMN, "multicolumn" },
132 { Tabular::SET_ALL_LINES, "set-all-lines" },
133 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
134 { Tabular::SET_LONGTABULAR, "set-longtabular" },
135 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
136 { Tabular::SET_PWIDTH, "set-pwidth" },
137 { Tabular::SET_MPWIDTH, "set-mpwidth" },
138 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
139 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
140 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
141 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
142 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
143 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
144 { Tabular::SET_USEBOX, "set-usebox" },
145 { Tabular::SET_LTHEAD, "set-lthead" },
146 { Tabular::UNSET_LTHEAD, "unset-lthead" },
147 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
148 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
149 { Tabular::SET_LTFOOT, "set-ltfoot" },
150 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
151 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
152 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
153 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
154 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
155 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
156 { Tabular::SET_BOOKTABS, "set-booktabs" },
157 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
158 { Tabular::SET_TOP_SPACE, "set-top-space" },
159 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
160 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
161 { Tabular::SET_BORDER_LINES, "set-border-lines" },
162 { Tabular::LAST_ACTION, "" }
166 class FeatureEqual : public unary_function<TabularFeature, bool> {
168 FeatureEqual(Tabular::Feature feature)
169 : feature_(feature) {}
170 bool operator()(TabularFeature const & tf) const {
171 return tf.action == feature_;
174 Tabular::Feature feature_;
179 string const write_attribute(string const & name, T const & t)
181 string const s = tostr(t);
182 return s.empty() ? s : " " + name + "=\"" + s + "\"";
186 string const write_attribute(string const & name, string const & t)
188 return t.empty() ? t : " " + name + "=\"" + t + "\"";
193 string const write_attribute(string const & name, docstring const & t)
195 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
200 string const write_attribute(string const & name, bool const & b)
202 // we write only true attribute values so we remove a bit of the
203 // file format bloat for tabulars.
204 return b ? write_attribute(name, convert<string>(b)) : string();
209 string const write_attribute(string const & name, int const & i)
211 // we write only true attribute values so we remove a bit of the
212 // file format bloat for tabulars.
213 return i ? write_attribute(name, convert<string>(i)) : string();
218 string const write_attribute(string const & name, Tabular::idx_type const & i)
220 // we write only true attribute values so we remove a bit of the
221 // file format bloat for tabulars.
222 return i ? write_attribute(name, convert<string>(i)) : string();
227 string const write_attribute(string const & name, Length const & value)
229 // we write only the value if we really have one same reson as above.
230 return value.zero() ? string() : write_attribute(name, value.asString());
234 string const tostr(LyXAlignment const & num)
239 case LYX_ALIGN_BLOCK:
243 case LYX_ALIGN_CENTER:
245 case LYX_ALIGN_RIGHT:
247 case LYX_ALIGN_LAYOUT:
249 case LYX_ALIGN_SPECIAL:
256 string const tostr(Tabular::VAlignment const & num)
259 case Tabular::LYX_VALIGN_TOP:
261 case Tabular::LYX_VALIGN_MIDDLE:
263 case Tabular::LYX_VALIGN_BOTTOM:
270 string const tostr(Tabular::BoxType const & num)
273 case Tabular::BOX_NONE:
275 case Tabular::BOX_PARBOX:
277 case Tabular::BOX_MINIPAGE:
284 // I would have liked a fromstr template a lot better. (Lgb)
285 bool string2type(string const str, LyXAlignment & num)
288 num = LYX_ALIGN_NONE;
289 else if (str == "block")
290 num = LYX_ALIGN_BLOCK;
291 else if (str == "left")
292 num = LYX_ALIGN_LEFT;
293 else if (str == "center")
294 num = LYX_ALIGN_CENTER;
295 else if (str == "right")
296 num = LYX_ALIGN_RIGHT;
303 bool string2type(string const str, Tabular::VAlignment & num)
306 num = Tabular::LYX_VALIGN_TOP;
307 else if (str == "middle" )
308 num = Tabular::LYX_VALIGN_MIDDLE;
309 else if (str == "bottom")
310 num = Tabular::LYX_VALIGN_BOTTOM;
317 bool string2type(string const str, Tabular::BoxType & num)
320 num = Tabular::BOX_NONE;
321 else if (str == "parbox")
322 num = Tabular::BOX_PARBOX;
323 else if (str == "minipage")
324 num = Tabular::BOX_MINIPAGE;
331 bool string2type(string const str, bool & num)
335 else if (str == "false")
343 bool getTokenValue(string const & str, char const * token, string & ret)
346 size_t token_length = strlen(token);
347 size_t pos = str.find(token);
349 if (pos == string::npos || pos + token_length + 1 >= str.length()
350 || str[pos + token_length] != '=')
352 pos += token_length + 1;
354 if (ch != '"' && ch != '\'') { // only read till next space
358 while (pos < str.length() - 1 && str[++pos] != ch)
365 bool getTokenValue(string const & str, char const * token, docstring & ret)
368 bool const success = getTokenValue(str, token, tmp);
369 ret = from_utf8(tmp);
374 bool getTokenValue(string const & str, char const * token, int & num)
378 if (!getTokenValue(str, token, tmp))
380 num = convert<int>(tmp);
385 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
388 return getTokenValue(str, token, tmp) && string2type(tmp, num);
392 bool getTokenValue(string const & str, char const * token,
393 Tabular::VAlignment & num)
396 return getTokenValue(str, token, tmp) && string2type(tmp, num);
400 bool getTokenValue(string const & str, char const * token,
401 Tabular::BoxType & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token, bool & flag)
410 // set the flag always to false as this should be the default for bools
411 // not in the file-format.
414 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
418 bool getTokenValue(string const & str, char const * token, Length & len)
420 // set the length to be zero() as default as this it should be if not
421 // in the file format.
424 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
428 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
433 if (!getTokenValue(str, token, tmp))
435 if (tmp == "default") {
439 return isValidLength(tmp, &len);
443 void l_getline(istream & is, string & str)
446 while (str.empty()) {
448 if (!str.empty() && str[str.length() - 1] == '\r')
449 str.erase(str.length() - 1);
456 string const featureAsString(Tabular::Feature feature)
458 TabularFeature * end = tabularFeature +
459 sizeof(tabularFeature) / sizeof(TabularFeature);
460 TabularFeature * it = find_if(tabularFeature, end,
461 FeatureEqual(feature));
462 return (it == end) ? string() : it->feature;
467 /////////////////////////////////////////////////////////////////////
471 /////////////////////////////////////////////////////////////////////
474 Tabular::CellData::CellData(Buffer const & buf, Tabular const & table)
477 multicolumn(Tabular::CELL_NORMAL),
478 alignment(LYX_ALIGN_CENTER),
479 valignment(LYX_VALIGN_TOP),
486 inset(new InsetTableCell(buf, this, &table))
488 inset->setBuffer(const_cast<Buffer &>(buf));
489 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
493 Tabular::CellData::CellData(CellData const & cs)
496 multicolumn(cs.multicolumn),
497 alignment(cs.alignment),
498 valignment(cs.valignment),
499 top_line(cs.top_line),
500 bottom_line(cs.bottom_line),
501 left_line(cs.left_line),
502 right_line(cs.right_line),
505 align_special(cs.align_special),
507 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
509 inset->setCellData(this);
513 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
520 void Tabular::CellData::swap(CellData & rhs)
522 std::swap(cellno, rhs.cellno);
523 std::swap(width, rhs.width);
524 std::swap(multicolumn, rhs.multicolumn);
525 std::swap(alignment, rhs.alignment);
526 std::swap(valignment, rhs.valignment);
527 std::swap(top_line, rhs.top_line);
528 std::swap(bottom_line, rhs.bottom_line);
529 std::swap(left_line, rhs.left_line);
530 std::swap(right_line, rhs.right_line);
531 std::swap(usebox, rhs.usebox);
532 std::swap(rotate, rhs.rotate);
533 std::swap(align_special, rhs.align_special);
534 p_width.swap(rhs.p_width);
535 inset.swap(rhs.inset);
539 Tabular::RowData::RowData()
542 top_space_default(false),
543 bottom_space_default(false),
544 interline_space_default(false),
553 Tabular::ColumnData::ColumnData()
554 : alignment(LYX_ALIGN_CENTER),
555 valignment(LYX_VALIGN_TOP),
561 Tabular::ltType::ltType()
574 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
576 init(buffer, rows_arg, columns_arg);
580 // activates all lines and sets all widths to 0
581 void Tabular::init(Buffer const & buf, row_type rows_arg,
582 col_type columns_arg)
585 row_info = row_vector(rows_arg);
586 column_info = column_vector(columns_arg);
587 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf, *this)));
588 row_info.reserve(10);
589 column_info.reserve(10);
590 cell_info.reserve(100);
592 is_long_tabular = false;
594 use_booktabs = false;
595 // set silly default lines
596 for (row_type i = 0; i < rowCount(); ++i)
597 for (col_type j = 0; j < columnCount(); ++j) {
598 cell_info[i][j].top_line = true;
599 cell_info[i][j].left_line = true;
600 cell_info[i][j].bottom_line = i == 0 || i == rowCount() - 1;
601 cell_info[i][j].right_line = j == columnCount() - 1;
606 void Tabular::appendRow(idx_type const cell)
608 BufferParams const & bp = buffer().params();
609 row_type const row = cellRow(cell);
611 row_vector::iterator rit = row_info.begin() + row;
612 row_info.insert(rit, RowData());
613 // now set the values of the row before
614 row_info[row] = row_info[row + 1];
616 row_type const nrows = rowCount();
617 col_type const ncols = columnCount();
619 cell_vvector old(nrows - 1);
620 for (row_type i = 0; i < nrows - 1; ++i)
621 swap(cell_info[i], old[i]);
623 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer(), *this)));
625 for (row_type i = 0; i <= row; ++i)
626 swap(cell_info[i], old[i]);
627 for (row_type i = row + 2; i < nrows; ++i)
628 swap(cell_info[i], old[i - 1]);
631 for (col_type c = 0; c < ncols; ++c) {
632 // inherit line settings
633 idx_type const i = cellIndex(row + 1, c);
634 idx_type const j = cellIndex(row, c);
635 setLeftLine(i, isPartOfMultiColumn(row, c) ? false : leftLine(j));
636 if (cell_info[row][c].multicolumn == CELL_NORMAL || c == ncols - 1
638 && cell_info[row][c].multicolumn != CELL_NORMAL
639 && cell_info[row][c + 1].multicolumn == CELL_NORMAL))
640 setRightLine(i, rightLine(j));
642 setRightLine(i, false);
643 // mark track changes
645 cellInfo(i).inset->setChange(Change(Change::INSERTED));
650 void Tabular::deleteRow(row_type const row)
652 // Not allowed to delete last row
656 row_info.erase(row_info.begin() + row);
657 cell_info.erase(cell_info.begin() + row);
662 void Tabular::copyRow(row_type const row)
664 row_info.insert(row_info.begin() + row, row_info[row]);
665 cell_info.insert(cell_info.begin() + row, cell_info[row]);
667 if (buffer().params().trackChanges)
668 for (col_type j = 0; j < columnCount(); ++j)
669 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
675 void Tabular::appendColumn(idx_type const cell)
677 col_type const column = cellColumn(cell);
678 column_vector::iterator cit = column_info.begin() + column + 1;
679 column_info.insert(cit, ColumnData());
680 row_type const nrows = rowCount();
681 col_type const ncols = columnCount();
682 // set the column values of the column before
683 column_info[column + 1] = column_info[column];
685 for (row_type r = 0; r < nrows; ++r) {
686 cell_info[r].insert(cell_info[r].begin() + column + 1,
687 CellData(buffer(), *this));
688 col_type c = column + 2;
690 && cell_info[r][c].multicolumn == CELL_PART_OF_MULTICOLUMN) {
691 cell_info[r][c].multicolumn = CELL_NORMAL;
696 for (row_type r = 0; r < nrows; ++r) {
697 // inherit line settings
698 idx_type const i = cellIndex(r, column + 1);
699 idx_type const j = cellIndex(r, column);
700 setBottomLine(i, bottomLine(j));
701 setTopLine(i, topLine(j));
703 cellInfo(i).inset->clear();
704 if (buffer().params().trackChanges)
705 cellInfo(i).inset->setChange(Change(Change::INSERTED));
710 void Tabular::deleteColumn(col_type const column)
712 // Not allowed to delete last column
713 if (columnCount() == 1)
716 column_info.erase(column_info.begin() + column);
717 for (row_type i = 0; i < rowCount(); ++i) {
718 // Care about multicolumn cells
719 if (column + 1 < columnCount() &&
720 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
721 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
722 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
724 cell_info[i].erase(cell_info[i].begin() + column);
730 void Tabular::copyColumn(col_type const column)
732 BufferParams const & bp = buffer().params();
733 column_info.insert(column_info.begin() + column, column_info[column]);
735 for (row_type i = 0; i < rowCount(); ++i)
736 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
739 for (row_type i = 0; i < rowCount(); ++i)
740 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
745 void Tabular::updateIndexes()
747 col_type ncols = columnCount();
748 row_type nrows = rowCount();
750 for (row_type row = 0; row < nrows; ++row)
751 for (col_type column = 0; column < ncols; ++column) {
752 if (!isPartOfMultiColumn(row, column))
754 cell_info[row][column].cellno = numberofcells - 1;
757 rowofcell.resize(numberofcells);
758 columnofcell.resize(numberofcells);
760 for (row_type row = 0; row < nrows; ++row)
761 for (col_type column = 0; column < ncols; ++column) {
762 if (isPartOfMultiColumn(row, column))
765 columnofcell[i] = column;
771 Tabular::idx_type Tabular::cellCount() const
773 return numberofcells;
777 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
779 row_type const row = cellRow(cell);
781 for (col_type i = 0; i < columnCount(); ++i)
782 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
788 bool Tabular::topLine(idx_type const cell) const
790 return cellInfo(cell).top_line;
794 bool Tabular::bottomLine(idx_type const cell) const
796 return cellInfo(cell).bottom_line;
800 bool Tabular::leftLine(idx_type cell) const
804 return cellInfo(cell).left_line;
808 bool Tabular::rightLine(idx_type cell) const
812 return cellInfo(cell).right_line;
816 bool Tabular::topAlreadyDrawn(idx_type cell) const
818 row_type row = cellRow(cell);
821 idx_type i = cellIndex(row - 1, cellColumn(cell));
822 return !rowBottomLine(row - 1) && bottomLine(i);
826 bool Tabular::leftAlreadyDrawn(idx_type cell) const
828 col_type col = cellColumn(cell);
831 idx_type i = cellIndex(cellRow(cell), col - 1);
832 return rightLine(i) && !leftLine(cell);
836 bool Tabular::isLastRow(idx_type cell) const
838 return cellRow(cell) == rowCount() - 1;
842 int Tabular::getAdditionalHeight(row_type row) const
844 if (!row || row >= rowCount())
847 int const interline_space = row_info[row - 1].interline_space_default ?
849 row_info[row - 1].interline_space.inPixels(width());
850 if (rowTopLine(row) && rowBottomLine(row - 1))
851 return interline_space + WIDTH_OF_LINE;
852 return interline_space;
856 int Tabular::getAdditionalWidth(idx_type cell) const
858 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
860 && nextcol < columnCount() && leftLine(cellIndex(cellRow(cell), nextcol)))
861 return WIDTH_OF_LINE;
866 int Tabular::columnWidth(idx_type cell) const
869 col_type const span = columnSpan(cell);
870 col_type const col = cellColumn(cell);
871 for(col_type c = col; c < col + span ; ++c)
872 w += column_info[c].width;
877 bool Tabular::updateColumnWidths()
879 col_type const ncols = columnCount();
880 row_type const nrows = rowCount();
882 // for each col get max of single col cells
883 for(col_type c = 0; c < ncols; ++c) {
885 for(row_type r = 0; r < nrows; ++r) {
886 idx_type const i = cellIndex(r, c);
887 if (columnSpan(i) == 1)
888 new_width = max(new_width, cellInfo(i).width);
891 if (column_info[c].width != new_width) {
892 column_info[c].width = new_width;
896 // update col widths to fit merged cells
897 for(col_type c = 0; c < ncols; ++c)
898 for(row_type r = 0; r < nrows; ++r) {
899 idx_type const i = cellIndex(r, c);
900 int const span = columnSpan(i);
901 if (span == 1 || c > cellColumn(i))
905 for(col_type j = c; j < c + span ; ++j)
906 old_width += column_info[j].width;
908 if (cellInfo(i).width > old_width) {
909 column_info[c + span - 1].width += cellInfo(i).width - old_width;
918 int Tabular::width() const
920 col_type const ncols = columnCount();
922 for (col_type i = 0; i < ncols; ++i)
923 width += column_info[i].width;
928 void Tabular::setCellWidth(idx_type cell, int new_width)
930 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
931 + getAdditionalWidth(cell);
935 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
938 if (!isMultiColumn(cell) || onlycolumn)
939 column_info[cellColumn(cell)].alignment = align;
941 cellInfo(cell).alignment = align;
945 void Tabular::setVAlignment(idx_type cell, VAlignment align,
948 if (!isMultiColumn(cell) || onlycolumn)
949 column_info[cellColumn(cell)].valignment = align;
951 cellInfo(cell).valignment = align;
958 * Allow line and paragraph breaks for fixed width cells or disallow them,
959 * merge cell paragraphs and reset layout to standard for variable width
962 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
964 inset->setAutoBreakRows(fixedWidth);
968 // merge all paragraphs to one
969 BufferParams const & bp = cur.bv().buffer().params();
970 while (inset->paragraphs().size() > 1)
971 mergeParagraph(bp, inset->paragraphs(), 0);
975 // undo information has already been recorded
976 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
977 bp.documentClass().emptyLayoutName());
984 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
985 Length const & width)
987 col_type const j = cellColumn(cell);
989 column_info[j].p_width = width;
990 for (row_type i = 0; i < rowCount(); ++i) {
991 idx_type const cell = cellIndex(i, j);
992 // because of multicolumns
993 toggleFixedWidth(cur, cellInset(cell).get(),
994 !getPWidth(cell).zero());
996 // cur paragraph can become invalid after paragraphs were merged
997 if (cur.pit() > cur.lastpit())
998 cur.pit() = cur.lastpit();
999 // cur position can become invalid after newlines were removed
1000 if (cur.pos() > cur.lastpos())
1001 cur.pos() = cur.lastpos();
1005 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1006 Length const & width)
1008 if (!isMultiColumn(cell))
1011 cellInfo(cell).p_width = width;
1012 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1013 // cur paragraph can become invalid after paragraphs were merged
1014 if (cur.pit() > cur.lastpit())
1015 cur.pit() = cur.lastpit();
1016 // cur position can become invalid after newlines were removed
1017 if (cur.pos() > cur.lastpos())
1018 cur.pos() = cur.lastpos();
1023 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1024 Tabular::Feature what)
1026 if (what == SET_SPECIAL_MULTI)
1027 cellInfo(cell).align_special = special;
1029 column_info[cellColumn(cell)].align_special = special;
1033 void Tabular::setAllLines(idx_type cell, bool line)
1035 setTopLine(cell, line);
1036 setBottomLine(cell, line);
1037 setRightLine(cell, line);
1038 setLeftLine(cell, line);
1042 void Tabular::setTopLine(idx_type i, bool line)
1044 cellInfo(i).top_line = line;
1048 void Tabular::setBottomLine(idx_type i, bool line)
1050 cellInfo(i).bottom_line = line;
1054 void Tabular::setLeftLine(idx_type cell, bool line)
1056 cellInfo(cell).left_line = line;
1060 void Tabular::setRightLine(idx_type cell, bool line)
1062 cellInfo(cell).right_line = line;
1065 bool Tabular::rowTopLine(row_type r) const
1067 idx_type i0 = getFirstCellInRow(r);
1068 idx_type i1 = getLastCellInRow(r);
1069 bool all_rows_set = true;
1070 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1071 all_rows_set = cellInfo(j).top_line;
1072 return all_rows_set;
1076 bool Tabular::rowBottomLine(row_type r) const
1078 idx_type i0 = getFirstCellInRow(r);
1079 idx_type i1 = getLastCellInRow(r);
1080 bool all_rows_set = true;
1081 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1082 all_rows_set = cellInfo(j).bottom_line;
1083 return all_rows_set;
1087 bool Tabular::columnLeftLine(col_type c) const
1089 bool all_cols_set = true;
1090 row_type const nrows = rowCount();
1091 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1092 idx_type i = cellIndex(r, c);
1093 if (c == cellColumn(i))
1094 all_cols_set = cellInfo(i).left_line;
1096 return all_cols_set;
1100 bool Tabular::columnRightLine(col_type c) const
1102 bool all_cols_set = true;
1103 row_type const nrows = rowCount();
1104 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1105 idx_type i = cellIndex(r, c);
1106 if (c == cellColumn(i) + columnSpan(i) - 1)
1107 all_cols_set = cellInfo(i).right_line;
1109 return all_cols_set;
1113 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1115 if (!onlycolumn && isMultiColumn(cell))
1116 return cellInfo(cell).alignment;
1117 return column_info[cellColumn(cell)].alignment;
1122 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1124 if (!onlycolumn && isMultiColumn(cell))
1125 return cellInfo(cell).valignment;
1126 return column_info[cellColumn(cell)].valignment;
1130 Length const Tabular::getPWidth(idx_type cell) const
1132 if (isMultiColumn(cell))
1133 return cellInfo(cell).p_width;
1134 return column_info[cellColumn(cell)].p_width;
1138 Length const Tabular::getColumnPWidth(idx_type cell) const
1140 return column_info[cellColumn(cell)].p_width;
1144 Length const Tabular::getMColumnPWidth(idx_type cell) const
1146 if (isMultiColumn(cell))
1147 return cellInfo(cell).p_width;
1152 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1154 if (what == SET_SPECIAL_MULTI)
1155 return cellInfo(cell).align_special;
1156 return column_info[cellColumn(cell)].align_special;
1160 int Tabular::cellWidth(idx_type cell) const
1162 return cellInfo(cell).width;
1166 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1170 switch (getAlignment(cell)) {
1171 case LYX_ALIGN_CENTER:
1172 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1174 case LYX_ALIGN_RIGHT:
1175 x += columnWidth(cell) - cellWidth(cell);
1176 // + getAdditionalWidth(cell);
1179 // LYX_ALIGN_LEFT: nothing :-)
1183 // the LaTeX Way :-(
1189 bool Tabular::isFirstCellInRow(idx_type cell) const
1191 return cellColumn(cell) == 0;
1195 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1197 if (row > rowCount() - 1)
1198 row = rowCount() - 1;
1199 return cell_info[row][0].cellno;
1203 bool Tabular::isLastCellInRow(idx_type cell) const
1205 return cellRightColumn(cell) == columnCount() - 1;
1209 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1211 if (row > rowCount() - 1)
1212 row = rowCount() - 1;
1213 return cell_info[row][columnCount() - 1].cellno;
1217 Tabular::row_type Tabular::cellRow(idx_type cell) const
1219 if (cell >= cellCount())
1220 return rowCount() - 1;
1223 return rowofcell[cell];
1227 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1229 if (cell >= cellCount())
1230 return columnCount() - 1;
1233 return columnofcell[cell];
1237 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1239 row_type const row = cellRow(cell);
1240 col_type column = cellColumn(cell);
1241 while (column < columnCount() - 1 &&
1242 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1248 void Tabular::write(ostream & os) const
1252 << write_attribute("version", 3)
1253 << write_attribute("rows", rowCount())
1254 << write_attribute("columns", columnCount())
1256 // global longtable options
1258 << write_attribute("rotate", rotate)
1259 << write_attribute("booktabs", use_booktabs)
1260 << write_attribute("islongtable", is_long_tabular)
1261 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1262 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1263 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1264 << write_attribute("headTopDL", endhead.topDL)
1265 << write_attribute("headBottomDL", endhead.bottomDL)
1266 << write_attribute("footTopDL", endfoot.topDL)
1267 << write_attribute("footBottomDL", endfoot.bottomDL)
1268 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1269 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1270 << write_attribute("lastFootEmpty", endlastfoot.empty)
1272 for (col_type j = 0; j < columnCount(); ++j) {
1274 << write_attribute("alignment", column_info[j].alignment)
1275 << write_attribute("valignment", column_info[j].valignment)
1276 << write_attribute("width", column_info[j].p_width.asString())
1277 << write_attribute("special", column_info[j].align_special)
1280 for (row_type i = 0; i < rowCount(); ++i) {
1281 static const string def("default");
1283 if (row_info[i].top_space_default)
1284 os << write_attribute("topspace", def);
1286 os << write_attribute("topspace", row_info[i].top_space);
1287 if (row_info[i].bottom_space_default)
1288 os << write_attribute("bottomspace", def);
1290 os << write_attribute("bottomspace", row_info[i].bottom_space);
1291 if (row_info[i].interline_space_default)
1292 os << write_attribute("interlinespace", def);
1294 os << write_attribute("interlinespace", row_info[i].interline_space);
1295 os << write_attribute("endhead", row_info[i].endhead)
1296 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1297 << write_attribute("endfoot", row_info[i].endfoot)
1298 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1299 << write_attribute("newpage", row_info[i].newpage)
1301 for (col_type j = 0; j < columnCount(); ++j) {
1303 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1304 << write_attribute("alignment", cell_info[i][j].alignment)
1305 << write_attribute("valignment", cell_info[i][j].valignment)
1306 << write_attribute("topline", cell_info[i][j].top_line)
1307 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1308 << write_attribute("leftline", cell_info[i][j].left_line)
1309 << write_attribute("rightline", cell_info[i][j].right_line)
1310 << write_attribute("rotate", cell_info[i][j].rotate)
1311 << write_attribute("usebox", cell_info[i][j].usebox)
1312 << write_attribute("width", cell_info[i][j].p_width)
1313 << write_attribute("special", cell_info[i][j].align_special)
1315 os << "\\begin_inset ";
1316 cell_info[i][j].inset->write(os);
1317 os << "\n\\end_inset\n"
1322 os << "</lyxtabular>\n";
1326 void Tabular::read(Lexer & lex)
1329 istream & is = lex.getStream();
1331 l_getline(is, line);
1332 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1333 LASSERT(false, /**/);
1338 if (!getTokenValue(line, "version", version))
1340 LASSERT(version >= 2, /**/);
1343 if (!getTokenValue(line, "rows", rows_arg))
1346 if (!getTokenValue(line, "columns", columns_arg))
1348 init(buffer(), rows_arg, columns_arg);
1349 l_getline(is, line);
1350 if (!prefixIs(line, "<features")) {
1351 lyxerr << "Wrong tabular format (expected <features ...> got"
1352 << line << ')' << endl;
1355 getTokenValue(line, "rotate", rotate);
1356 getTokenValue(line, "booktabs", use_booktabs);
1357 getTokenValue(line, "islongtable", is_long_tabular);
1358 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1359 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1360 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1361 getTokenValue(line, "headTopDL", endhead.topDL);
1362 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1363 getTokenValue(line, "footTopDL", endfoot.topDL);
1364 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1365 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1366 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1367 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1369 for (col_type j = 0; j < columnCount(); ++j) {
1371 if (!prefixIs(line,"<column")) {
1372 lyxerr << "Wrong tabular format (expected <column ...> got"
1373 << line << ')' << endl;
1376 getTokenValue(line, "alignment", column_info[j].alignment);
1377 getTokenValue(line, "valignment", column_info[j].valignment);
1378 getTokenValue(line, "width", column_info[j].p_width);
1379 getTokenValue(line, "special", column_info[j].align_special);
1382 for (row_type i = 0; i < rowCount(); ++i) {
1383 l_getline(is, line);
1384 if (!prefixIs(line, "<row")) {
1385 lyxerr << "Wrong tabular format (expected <row ...> got"
1386 << line << ')' << endl;
1389 getTokenValue(line, "topspace", row_info[i].top_space,
1390 row_info[i].top_space_default);
1391 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1392 row_info[i].bottom_space_default);
1393 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1394 row_info[i].interline_space_default);
1395 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1396 getTokenValue(line, "endhead", row_info[i].endhead);
1397 getTokenValue(line, "endfoot", row_info[i].endfoot);
1398 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1399 getTokenValue(line, "newpage", row_info[i].newpage);
1400 for (col_type j = 0; j < columnCount(); ++j) {
1401 l_getline(is, line);
1402 if (!prefixIs(line, "<cell")) {
1403 lyxerr << "Wrong tabular format (expected <cell ...> got"
1404 << line << ')' << endl;
1407 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1408 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1409 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1410 getTokenValue(line, "topline", cell_info[i][j].top_line);
1411 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1412 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1413 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1414 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1415 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1416 getTokenValue(line, "width", cell_info[i][j].p_width);
1417 getTokenValue(line, "special", cell_info[i][j].align_special);
1418 l_getline(is, line);
1419 if (prefixIs(line, "\\begin_inset")) {
1420 cell_info[i][j].inset->read(lex);
1421 l_getline(is, line);
1423 if (!prefixIs(line, "</cell>")) {
1424 lyxerr << "Wrong tabular format (expected </cell> got"
1425 << line << ')' << endl;
1429 l_getline(is, line);
1430 if (!prefixIs(line, "</row>")) {
1431 lyxerr << "Wrong tabular format (expected </row> got"
1432 << line << ')' << endl;
1436 while (!prefixIs(line, "</lyxtabular>")) {
1437 l_getline(is, line);
1443 bool Tabular::isMultiColumn(idx_type cell) const
1445 return cellInfo(cell).multicolumn != CELL_NORMAL;
1449 bool Tabular::isMultiColumnReal(idx_type cell) const
1451 return cellColumn(cell) != cellRightColumn(cell) &&
1452 cellInfo(cell).multicolumn != CELL_NORMAL;
1456 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1458 return cell_info[cellRow(cell)][cellColumn(cell)];
1462 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1464 CellData & cs = cellInfo(cell);
1465 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1466 cs.alignment = column_info[cellColumn(cell)].alignment;
1467 setRightLine(cell, rightLine(cell + number - 1));
1468 for (idx_type i = 1; i < number; ++i) {
1469 CellData & cs1 = cellInfo(cell + i);
1470 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1471 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1478 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1480 row_type const row = cellRow(cell);
1481 col_type const ncols = columnCount();
1482 idx_type result = 1;
1483 col_type column = cellColumn(cell) + 1;
1484 while (column < ncols && isPartOfMultiColumn(row, column)) {
1492 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1494 row_type const row = cellRow(cell);
1495 col_type column = cellColumn(cell);
1497 idx_type result = 0;
1499 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1500 cell_info[row][column].multicolumn = CELL_NORMAL;
1502 while (column < columnCount() &&
1503 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1505 cell_info[row][column].multicolumn = CELL_NORMAL;
1515 void Tabular::setBookTabs(bool what)
1517 use_booktabs = what;
1521 bool Tabular::useBookTabs() const
1523 return use_booktabs;
1527 void Tabular::setLongTabular(bool what)
1529 is_long_tabular = what;
1533 bool Tabular::isLongTabular() const
1535 return is_long_tabular;
1539 void Tabular::setRotateTabular(bool flag)
1545 bool Tabular::getRotateTabular() const
1551 void Tabular::setRotateCell(idx_type cell, bool flag)
1553 cellInfo(cell).rotate = flag;
1557 bool Tabular::getRotateCell(idx_type cell) const
1559 return cellInfo(cell).rotate;
1563 bool Tabular::needRotating() const
1567 for (row_type i = 0; i < rowCount(); ++i)
1568 for (col_type j = 0; j < columnCount(); ++j)
1569 if (cell_info[i][j].rotate)
1575 bool Tabular::isLastCell(idx_type cell) const
1577 if (cell + 1 < cellCount())
1583 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1585 if (cellRow(cell) > 0)
1586 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1591 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1593 if (cellRow(cell) + 1 < rowCount())
1594 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1599 Tabular::idx_type Tabular::cellIndex(row_type row,
1600 col_type column) const
1602 BOOST_ASSERT(column != npos && column < columnCount() &&
1603 row != npos && row < rowCount());
1604 return cell_info[row][column].cellno;
1608 void Tabular::setUsebox(idx_type cell, BoxType type)
1610 cellInfo(cell).usebox = type;
1614 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1616 if (column_info[cellColumn(cell)].p_width.zero() &&
1617 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1619 if (cellInfo(cell).usebox > 1)
1620 return cellInfo(cell).usebox;
1621 return useParbox(cell);
1626 // This are functions used for the longtable support
1628 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1634 row_info[row].endfirsthead = flag;
1638 row_info[row].endhead = flag;
1643 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1646 hd.set = haveLTHead();
1647 return row_info[row].endhead;
1651 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1654 hd.set = haveLTFirstHead();
1655 return row_info[row].endfirsthead;
1659 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1665 row_info[row].endlastfoot = flag;
1669 row_info[row].endfoot = flag;
1674 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1677 fd.set = haveLTFoot();
1678 return row_info[row].endfoot;
1682 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1685 fd.set = haveLTLastFoot();
1686 return row_info[row].endlastfoot;
1690 void Tabular::setLTNewPage(row_type row, bool what)
1692 row_info[row].newpage = what;
1696 bool Tabular::getLTNewPage(row_type row) const
1698 return row_info[row].newpage;
1702 bool Tabular::haveLTHead() const
1704 for (row_type i = 0; i < rowCount(); ++i)
1705 if (row_info[i].endhead)
1711 bool Tabular::haveLTFirstHead() const
1713 if (endfirsthead.empty)
1715 for (row_type i = 0; i < rowCount(); ++i)
1716 if (row_info[i].endfirsthead)
1722 bool Tabular::haveLTFoot() const
1724 for (row_type i = 0; i < rowCount(); ++i)
1725 if (row_info[i].endfoot)
1731 bool Tabular::haveLTLastFoot() const
1733 if (endlastfoot.empty)
1735 for (row_type i = 0; i < rowCount(); ++i)
1736 if (row_info[i].endlastfoot)
1742 // end longtable support functions
1744 void Tabular::setRowAscent(row_type row, int height)
1746 if (row >= rowCount() || row_info[row].ascent == height)
1748 row_info[row].ascent = height;
1752 void Tabular::setRowDescent(row_type row, int height)
1754 if (row >= rowCount() || row_info[row].descent == height)
1756 row_info[row].descent = height;
1760 int Tabular::rowAscent(row_type row) const
1762 if (row >= rowCount())
1764 return row_info[row].ascent;
1768 int Tabular::rowDescent(row_type row) const
1770 LASSERT(row < rowCount(), /**/);
1771 return row_info[row].descent;
1775 int Tabular::height() const
1778 for (row_type row = 0; row < rowCount(); ++row)
1779 height += rowAscent(row) + rowDescent(row) +
1780 getAdditionalHeight(row);
1785 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1787 LASSERT(row < rowCount(), /**/);
1788 LASSERT(column < columnCount(), /**/);
1789 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1793 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1795 // FIXME: assert or return 0 as in TeXBottomHLine()?
1796 LASSERT(row != npos, /**/);
1797 LASSERT(row < rowCount(), /**/);
1799 idx_type const fcell = getFirstCellInRow(row);
1800 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1803 for (idx_type i = fcell; i < n; ++i) {
1807 if (use_booktabs && row == 0) {
1810 } else if (tmp == n - fcell) {
1811 os << (use_booktabs ? "\\midrule " : "\\hline ");
1813 for (idx_type i = fcell; i < n; ++i) {
1815 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1816 << cellColumn(i) + 1
1818 << cellRightColumn(i) + 1
1830 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1832 // FIXME: return 0 or assert as in TeXTopHLine()?
1833 if (row == npos || row >= rowCount())
1836 idx_type const fcell = getFirstCellInRow(row);
1837 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1840 for (idx_type i = fcell; i < n; ++i) {
1844 if (use_booktabs && row == rowCount() - 1) {
1845 if (bottomLine(fcell))
1846 os << "\\bottomrule";
1847 } else if (tmp == n - fcell) {
1848 os << (use_booktabs ? "\\midrule" : "\\hline");
1850 for (idx_type i = fcell; i < n; ++i) {
1851 if (bottomLine(i)) {
1852 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1853 << cellColumn(i) + 1
1855 << cellRightColumn(i) + 1
1867 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1871 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1872 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1873 // figure out how to set the lines
1874 // we always set double lines to the right of the cell
1875 row_type const r = cellRow(cell);
1876 col_type const c = cellColumn(cell);
1877 col_type const nextcol = c + columnSpan(cell);
1878 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1879 bool forceleft = ismulticol && !prevcellright && leftLine(cell);
1880 bool nextcolleft = nextcol < columnCount() && columnLeftLine(nextcol);
1881 bool coldouble = columnRightLine(c) && nextcolleft;
1882 bool nextcellleft = nextcol < columnCount()
1883 && leftLine(cellIndex(r, nextcol));
1884 bool celldouble = rightLine(cell) && nextcellleft;
1885 bool doubleright = celldouble && (isMultiColumn(cell) || !coldouble);
1886 ismulticol = isMultiColumn(cell)
1887 || (leftLine(cell) && !columnLeftLine(c))
1888 || (rightLine(cell) && !columnRightLine(c))
1889 || (!celldouble && coldouble) || doubleright || forceleft;
1891 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1892 if (leftLine(cell) || forceleft)
1894 if (!cellInfo(cell).align_special.empty()) {
1895 os << cellInfo(cell).align_special;
1897 if (!getPWidth(cell).zero()) {
1899 case LYX_VALIGN_TOP:
1902 case LYX_VALIGN_MIDDLE:
1905 case LYX_VALIGN_BOTTOM:
1910 << from_ascii(getPWidth(cell).asLatexString())
1914 case LYX_ALIGN_LEFT:
1917 case LYX_ALIGN_RIGHT:
1924 } // end if else !getPWidth
1925 } // end if else !cellinfo_of_cell
1926 if (rightLine(cell))
1929 // add extra vertical line if we want a double one
1933 if (getRotateCell(cell)) {
1934 os << "\\begin{sideways}\n";
1937 if (getUsebox(cell) == BOX_PARBOX) {
1940 case LYX_VALIGN_TOP:
1943 case LYX_VALIGN_MIDDLE:
1946 case LYX_VALIGN_BOTTOM:
1950 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1952 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1953 os << "\\begin{minipage}[";
1955 case LYX_VALIGN_TOP:
1958 case LYX_VALIGN_MIDDLE:
1961 case LYX_VALIGN_BOTTOM:
1965 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1973 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1978 if (getUsebox(cell) == BOX_PARBOX)
1980 else if (getUsebox(cell) == BOX_MINIPAGE) {
1981 os << "%\n\\end{minipage}";
1984 if (getRotateCell(cell)) {
1985 os << "%\n\\end{sideways}";
1995 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1996 OutputParams const & runparams) const
1998 if (!is_long_tabular)
2002 // output header info
2004 if (endhead.topDL) {
2008 for (row_type i = 0; i < rowCount(); ++i) {
2009 if (row_info[i].endhead) {
2010 ret += TeXRow(os, i, runparams);
2013 if (endhead.bottomDL) {
2017 os << "\\endhead\n";
2019 if (endfirsthead.empty) {
2020 os << "\\endfirsthead\n";
2024 // output firstheader info
2025 if (haveLTFirstHead()) {
2026 if (endfirsthead.topDL) {
2030 for (row_type i = 0; i < rowCount(); ++i) {
2031 if (row_info[i].endfirsthead) {
2032 ret += TeXRow(os, i, runparams);
2035 if (endfirsthead.bottomDL) {
2039 os << "\\endfirsthead\n";
2042 // output footer info
2044 if (endfoot.topDL) {
2048 for (row_type i = 0; i < rowCount(); ++i) {
2049 if (row_info[i].endfoot) {
2050 ret += TeXRow(os, i, runparams);
2053 if (endfoot.bottomDL) {
2057 os << "\\endfoot\n";
2059 if (endlastfoot.empty) {
2060 os << "\\endlastfoot\n";
2064 // output lastfooter info
2065 if (haveLTLastFoot()) {
2066 if (endlastfoot.topDL) {
2070 for (row_type i = 0; i < rowCount(); ++i) {
2071 if (row_info[i].endlastfoot) {
2072 ret += TeXRow(os, i, runparams);
2075 if (endlastfoot.bottomDL) {
2079 os << "\\endlastfoot\n";
2086 bool Tabular::isValidRow(row_type row) const
2088 if (!is_long_tabular)
2090 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2091 !row_info[row].endfoot && !row_info[row].endlastfoot;
2095 int Tabular::TeXRow(odocstream & os, row_type i,
2096 OutputParams const & runparams) const
2098 idx_type cell = cellIndex(i, 0);
2099 int ret = TeXTopHLine(os, i);
2100 if (row_info[i].top_space_default) {
2102 os << "\\addlinespace\n";
2104 os << "\\noalign{\\vskip\\doublerulesep}\n";
2106 } else if(!row_info[i].top_space.zero()) {
2108 os << "\\addlinespace["
2109 << from_ascii(row_info[i].top_space.asLatexString())
2112 os << "\\noalign{\\vskip"
2113 << from_ascii(row_info[i].top_space.asLatexString())
2118 bool ismulticol = false;
2119 for (col_type j = 0; j < columnCount(); ++j) {
2120 if (isPartOfMultiColumn(i, j))
2122 ret += TeXCellPreamble(os, cell, ismulticol);
2123 shared_ptr<InsetTableCell> inset = cellInset(cell);
2125 Paragraph const & par = inset->paragraphs().front();
2126 bool rtl = par.isRTL(buffer().params())
2128 && getPWidth(cell).zero();
2131 if (par.getParLanguage(buffer().params())->lang() ==
2134 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2136 // currently, remaning RTL languages are arabic_arabtex and hebrew
2140 ret += inset->latex(os, runparams);
2144 ret += TeXCellPostamble(os, cell, ismulticol);
2145 if (!isLastCellInRow(cell)) { // not last cell in row
2150 os << "\\tabularnewline";
2151 if (row_info[i].bottom_space_default) {
2153 os << "\\addlinespace";
2155 os << "[\\doublerulesep]";
2156 } else if (!row_info[i].bottom_space.zero()) {
2158 os << "\\addlinespace";
2160 << from_ascii(row_info[i].bottom_space.asLatexString())
2165 ret += TeXBottomHLine(os, i);
2166 if (row_info[i].interline_space_default) {
2168 os << "\\addlinespace\n";
2170 os << "\\noalign{\\vskip\\doublerulesep}\n";
2172 } else if (!row_info[i].interline_space.zero()) {
2174 os << "\\addlinespace["
2175 << from_ascii(row_info[i].interline_space.asLatexString())
2178 os << "\\noalign{\\vskip"
2179 << from_ascii(row_info[i].interline_space.asLatexString())
2187 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2191 //+---------------------------------------------------------------------
2192 //+ first the opening preamble +
2193 //+---------------------------------------------------------------------
2196 os << "\\begin{sideways}\n";
2199 if (is_long_tabular)
2200 os << "\\begin{longtable}{";
2202 os << "\\begin{tabular}{";
2204 for (col_type i = 0; i < columnCount(); ++i) {
2205 if (!use_booktabs && columnLeftLine(i))
2207 if (!column_info[i].align_special.empty()) {
2208 os << column_info[i].align_special;
2210 if (!column_info[i].p_width.zero()) {
2211 switch (column_info[i].alignment) {
2212 case LYX_ALIGN_LEFT:
2213 os << ">{\\raggedright}";
2215 case LYX_ALIGN_RIGHT:
2216 os << ">{\\raggedleft}";
2218 case LYX_ALIGN_CENTER:
2219 os << ">{\\centering}";
2221 case LYX_ALIGN_NONE:
2222 case LYX_ALIGN_BLOCK:
2223 case LYX_ALIGN_LAYOUT:
2224 case LYX_ALIGN_SPECIAL:
2228 switch (column_info[i].valignment) {
2229 case LYX_VALIGN_TOP:
2232 case LYX_VALIGN_MIDDLE:
2235 case LYX_VALIGN_BOTTOM:
2240 << from_ascii(column_info[i].p_width.asLatexString())
2243 switch (column_info[i].alignment) {
2244 case LYX_ALIGN_LEFT:
2247 case LYX_ALIGN_RIGHT:
2254 } // end if else !column_info[i].p_width
2255 } // end if else !column_info[i].align_special
2256 if (!use_booktabs && columnRightLine(i))
2262 ret += TeXLongtableHeaderFooter(os, runparams);
2264 //+---------------------------------------------------------------------
2265 //+ the single row and columns (cells) +
2266 //+---------------------------------------------------------------------
2268 for (row_type i = 0; i < rowCount(); ++i) {
2269 if (isValidRow(i)) {
2270 ret += TeXRow(os, i, runparams);
2271 if (is_long_tabular && row_info[i].newpage) {
2272 os << "\\newpage\n";
2278 //+---------------------------------------------------------------------
2279 //+ the closing of the tabular +
2280 //+---------------------------------------------------------------------
2282 if (is_long_tabular)
2283 os << "\\end{longtable}";
2285 os << "\\end{tabular}";
2287 os << "\n\\end{sideways}";
2295 int Tabular::docbookRow(odocstream & os, row_type row,
2296 OutputParams const & runparams) const
2299 idx_type cell = getFirstCellInRow(row);
2302 for (col_type j = 0; j < columnCount(); ++j) {
2303 if (isPartOfMultiColumn(row, j))
2306 os << "<entry align=\"";
2307 switch (getAlignment(cell)) {
2308 case LYX_ALIGN_LEFT:
2311 case LYX_ALIGN_RIGHT:
2319 os << "\" valign=\"";
2320 switch (getVAlignment(cell)) {
2321 case LYX_VALIGN_TOP:
2324 case LYX_VALIGN_BOTTOM:
2327 case LYX_VALIGN_MIDDLE:
2332 if (isMultiColumn(cell)) {
2333 os << " namest=\"col" << j << "\" ";
2334 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2338 ret += cellInset(cell)->docbook(os, runparams);
2347 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2351 //+---------------------------------------------------------------------
2352 //+ first the opening preamble +
2353 //+---------------------------------------------------------------------
2355 os << "<tgroup cols=\"" << columnCount()
2356 << "\" colsep=\"1\" rowsep=\"1\">\n";
2358 for (col_type i = 0; i < columnCount(); ++i) {
2359 os << "<colspec colname=\"col" << i << "\" align=\"";
2360 switch (column_info[i].alignment) {
2361 case LYX_ALIGN_LEFT:
2364 case LYX_ALIGN_RIGHT:
2372 if (runparams.flavor == OutputParams::XML)
2378 //+---------------------------------------------------------------------
2379 //+ Long Tabular case +
2380 //+---------------------------------------------------------------------
2382 // output header info
2383 if (haveLTHead() || haveLTFirstHead()) {
2386 for (row_type i = 0; i < rowCount(); ++i) {
2387 if (row_info[i].endhead || row_info[i].endfirsthead) {
2388 ret += docbookRow(os, i, runparams);
2394 // output footer info
2395 if (haveLTFoot() || haveLTLastFoot()) {
2398 for (row_type i = 0; i < rowCount(); ++i) {
2399 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2400 ret += docbookRow(os, i, runparams);
2407 //+---------------------------------------------------------------------
2408 //+ the single row and columns (cells) +
2409 //+---------------------------------------------------------------------
2413 for (row_type i = 0; i < rowCount(); ++i) {
2414 if (isValidRow(i)) {
2415 ret += docbookRow(os, i, runparams);
2420 //+---------------------------------------------------------------------
2421 //+ the closing of the tabular +
2422 //+---------------------------------------------------------------------
2431 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2432 vector<unsigned int> const & clen) const
2434 idx_type const fcell = getFirstCellInRow(row);
2435 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2438 for (idx_type i = fcell; i < n; ++i) {
2448 for (idx_type i = fcell; i < n; ++i) {
2459 col_type column = cellColumn(i);
2460 int len = clen[column];
2461 while (column < columnCount() - 1
2462 && isPartOfMultiColumn(row, ++column))
2463 len += clen[column] + 4;
2464 os << docstring(len, ch);
2479 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2480 vector<unsigned int> const & clen) const
2482 idx_type const fcell = getFirstCellInRow(row);
2483 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2486 for (idx_type i = fcell; i < n; ++i) {
2487 if (bottomLine(i)) {
2496 for (idx_type i = fcell; i < n; ++i) {
2497 if (bottomLine(i)) {
2507 col_type column = cellColumn(i);
2508 int len = clen[column];
2509 while (column < columnCount() -1
2510 && isPartOfMultiColumn(row, ++column))
2511 len += clen[column] + 4;
2512 os << docstring(len, ch);
2513 if (bottomLine(i)) {
2527 void Tabular::plaintextPrintCell(odocstream & os,
2528 OutputParams const & runparams,
2529 idx_type cell, row_type row, col_type column,
2530 vector<unsigned int> const & clen,
2531 bool onlydata) const
2533 odocstringstream sstr;
2534 cellInset(cell)->plaintext(sstr, runparams);
2546 unsigned int len1 = sstr.str().length();
2547 unsigned int len2 = clen[column];
2548 while (column < columnCount() -1
2549 && isPartOfMultiColumn(row, ++column))
2550 len2 += clen[column] + 4;
2553 switch (getAlignment(cell)) {
2555 case LYX_ALIGN_LEFT:
2558 case LYX_ALIGN_RIGHT:
2562 case LYX_ALIGN_CENTER:
2568 os << docstring(len1, ' ') << sstr.str()
2569 << docstring(len2, ' ');
2571 if (rightLine(cell))
2578 void Tabular::plaintext(odocstream & os,
2579 OutputParams const & runparams, int const depth,
2580 bool onlydata, char_type delim) const
2582 // first calculate the width of the single columns
2583 vector<unsigned int> clen(columnCount());
2586 // first all non (real) multicolumn cells!
2587 for (col_type j = 0; j < columnCount(); ++j) {
2589 for (row_type i = 0; i < rowCount(); ++i) {
2590 idx_type cell = cellIndex(i, j);
2591 if (isMultiColumnReal(cell))
2593 odocstringstream sstr;
2594 cellInset(cell)->plaintext(sstr, runparams);
2595 if (clen[j] < sstr.str().length())
2596 clen[j] = sstr.str().length();
2599 // then all (real) multicolumn cells!
2600 for (col_type j = 0; j < columnCount(); ++j) {
2601 for (row_type i = 0; i < rowCount(); ++i) {
2602 idx_type cell = cellIndex(i, j);
2603 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2605 odocstringstream sstr;
2606 cellInset(cell)->plaintext(sstr, runparams);
2607 int len = int(sstr.str().length());
2608 idx_type const n = columnSpan(cell);
2609 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2611 if (len > int(clen[j + n - 1]))
2612 clen[j + n - 1] = len;
2617 for (row_type i = 0; i < rowCount(); ++i) {
2618 if (!onlydata && plaintextTopHLine(os, i, clen))
2619 os << docstring(depth * 2, ' ');
2620 for (col_type j = 0; j < columnCount(); ++j) {
2621 if (isPartOfMultiColumn(i, j))
2623 if (onlydata && j > 0)
2624 // we don't use operator<< for single UCS4 character.
2625 // see explanation in docstream.h
2627 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2632 os << docstring(depth * 2, ' ');
2633 if (plaintextBottomHLine(os, i, clen))
2634 os << docstring(depth * 2, ' ');
2640 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2642 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2646 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2647 col_type column) const
2649 return cell_info[row][column].inset;
2653 void Tabular::setCellInset(row_type row, col_type column,
2654 shared_ptr<InsetTableCell> ins) const
2656 CellData & cd = cell_info[row][column];
2658 // reset the InsetTableCell's pointers
2659 ins->setCellData(&cd);
2660 ins->setTabular(this);
2665 Tabular::cellFromInset(Inset const * inset) const
2667 // is this inset part of the tabular?
2669 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2670 LASSERT(false, /**/);
2673 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2674 if (cellInset(cell).get() == inset) {
2675 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2676 << "cell=" << cell);
2680 // We should have found a cell at this point
2681 lyxerr << "Tabular::cellFromInset: Cell of inset "
2682 << inset << " not found!" << endl;
2683 LASSERT(false, /**/);
2689 void Tabular::validate(LaTeXFeatures & features) const
2691 features.require("NeedTabularnewline");
2693 features.require("booktabs");
2694 if (isLongTabular())
2695 features.require("longtable");
2697 features.require("rotating");
2698 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2699 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2700 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2701 features.require("array");
2702 cellInset(cell)->validate(features);
2707 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2709 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2710 ParagraphList::const_iterator cit = parlist.begin();
2711 ParagraphList::const_iterator end = parlist.end();
2713 for (; cit != end; ++cit)
2714 for (int i = 0; i < cit->size(); ++i)
2715 if (cit->isNewline(i))
2722 /////////////////////////////////////////////////////////////////////
2726 /////////////////////////////////////////////////////////////////////
2728 InsetTableCell::InsetTableCell(Buffer const & buf,
2729 Tabular::CellData const * cell, Tabular const * table)
2730 : InsetText(buf), cell_data_(cell), table_(table)
2734 bool InsetTableCell::forceEmptyLayout(idx_type) const
2736 LASSERT(table_, /**/);
2737 LASSERT(cell_data_, /**/);
2738 return table_->getPWidth(cell_data_->cellno).zero();
2741 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2743 LASSERT(table_, /**/);
2744 LASSERT(cell_data_, /**/);
2745 return !table_->getPWidth(cell_data_->cellno).zero();
2748 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2749 FuncStatus & status) const
2752 switch (cmd.action) {
2754 enabled = !forceEmptyLayout();
2756 case LFUN_LAYOUT_PARAGRAPH:
2757 enabled = allowParagraphCustomization();
2760 return InsetText::getStatus(cur, cmd, status);
2762 status.enabled(enabled);
2766 /////////////////////////////////////////////////////////////////////
2770 /////////////////////////////////////////////////////////////////////
2772 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2774 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2775 rowselect_(false), colselect_(false)
2777 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2781 InsetTabular::InsetTabular(InsetTabular const & tab)
2782 : Inset(tab), tabular(tab.tabular), scx_(0)
2784 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2788 InsetTabular::~InsetTabular()
2790 hideDialogs("tabular", this);
2794 bool InsetTabular::insetAllowed(InsetCode code) const
2796 if (code == MATHMACRO_CODE)
2803 void InsetTabular::write(ostream & os) const
2805 os << "Tabular" << endl;
2810 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2812 // FIXME: depending on the selection state, we could offer a different menu.
2813 return from_ascii("context-tabular");
2817 void InsetTabular::read(Lexer & lex)
2819 //bool const old_format = (lex.getString() == "\\LyXTable");
2827 string token = lex.getString();
2828 while (lex && token != "\\end_inset") {
2830 token = lex.getString();
2833 lex.printError("Missing \\end_inset at this point. ");
2837 int InsetTabular::rowFromY(Cursor & cur, int y) const
2839 // top y coordinate of tabular
2840 int h = yo(cur.bv()) - tabular.rowAscent(0);
2841 size_t nrows = tabular.rowCount();
2843 for (; r < nrows && y > h; ++r) {
2844 h += tabular.rowAscent(r);
2845 h += tabular.rowDescent(r);
2846 h += tabular.getAdditionalHeight(r);
2852 int InsetTabular::columnFromX(Cursor & cur, int x) const
2854 // left x coordinate of tabular
2855 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2856 size_t ncols = tabular.columnCount();
2858 for (; c < ncols && x > w; ++c)
2859 w += tabular.columnWidth(c);
2864 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2866 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2867 // mi.base.textwidth << "\n";
2870 LASSERT(false, /**/);
2874 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2877 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2878 if (tabular.isPartOfMultiColumn(i, j))
2879 // Multicolumn cell, but not first one
2884 if (tabular.cell_info[i][j].multicolumn ==
2885 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2886 p_width = tabular.cellInfo(cell).p_width;
2888 p_width = tabular.column_info[j].p_width;
2889 if (!p_width.zero())
2890 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2891 tabular.cellInset(cell)->metrics(m, dim);
2892 if (!p_width.zero())
2893 dim.wid = m.base.textwidth;
2894 tabular.setCellWidth(cell, dim.wid);
2895 maxAsc = max(maxAsc, dim.asc);
2896 maxDesc = max(maxDesc, dim.des);
2899 int const top_space = tabular.row_info[i].top_space_default ?
2900 default_line_space :
2901 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2902 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2903 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2904 default_line_space :
2905 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2906 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2908 tabular.updateColumnWidths();
2909 dim.asc = tabular.rowAscent(0);
2910 dim.des = tabular.height() - dim.asc;
2911 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2915 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2917 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2918 BufferView * bv = pi.base.bv;
2920 // FIXME: As the full backrgound is painted in drawSelection(),
2921 // we have no choice but to do a full repaint for the Text cells.
2922 pi.full_repaint = true;
2924 resetPos(bv->cursor());
2927 x += ADD_TO_TABULAR_WIDTH;
2929 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2932 first_visible_cell = Tabular::npos;
2933 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2935 int const a = tabular.rowAscent(i);
2936 int const d = tabular.rowDescent(i);
2937 idx = tabular.cellIndex(i, 0);
2938 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2939 if (tabular.isPartOfMultiColumn(i, j))
2941 if (first_visible_cell == Tabular::npos)
2942 first_visible_cell = idx;
2944 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2945 // Cache the Inset position.
2946 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2947 if (nx + tabular.columnWidth(idx) < 0
2948 || nx > bv->workWidth()
2950 || y - a > bv->workHeight()) {
2951 pi.pain.setDrawingEnabled(false);
2952 cell(idx)->draw(pi, cx, y);
2953 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2954 pi.pain.setDrawingEnabled(original_drawing_state);
2956 cell(idx)->draw(pi, cx, y);
2957 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2959 nx += tabular.columnWidth(idx);
2963 if (i + 1 < tabular.rowCount())
2964 y += d + tabular.rowAscent(i + 1) +
2965 tabular.getAdditionalHeight(i + 1);
2970 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2972 Cursor & cur = pi.base.bv->cursor();
2974 x += scx_ + ADD_TO_TABULAR_WIDTH;
2976 // FIXME: it is wrong to completely paint the background
2977 // if we want to do single row painting.
2979 // Paint background of current tabular
2980 int const w = tabular.width();
2981 int const h = tabular.height();
2982 int yy = y - tabular.rowAscent(0);
2983 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2985 if (!cur.selection())
2987 if (&cur.inset() != this)
2993 if (tablemode(cur)) {
2996 getSelection(cur, rs, re, cs, ce);
2997 y -= tabular.rowAscent(0);
2998 for (row_type j = 0; j < tabular.rowCount(); ++j) {
2999 int const a = tabular.rowAscent(j);
3000 int const h = a + tabular.rowDescent(j);
3002 y += tabular.getAdditionalHeight(j);
3003 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3004 if (tabular.isPartOfMultiColumn(j, i))
3006 idx_type const cell =
3007 tabular.cellIndex(j, i);
3008 int const w = tabular.columnWidth(cell);
3009 if (i >= cs && i <= ce && j >= rs && j <= re)
3010 pi.pain.fillRectangle(xx, y, w, h,
3018 x += cellXPos(cur.idx());
3019 x += tabular.getBeginningOfTextInCell(cur.idx());
3020 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3025 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3026 row_type row, idx_type cell, bool erased) const
3028 int x2 = x + tabular.columnWidth(cell);
3029 bool on_off = false;
3030 ColorCode col = Color_tabularline;
3031 ColorCode onoffcol = Color_tabularonoffline;
3034 col = Color_deletedtext;
3035 onoffcol = Color_deletedtext;
3038 if (!tabular.topAlreadyDrawn(cell)) {
3039 on_off = !tabular.topLine(cell);
3040 pain.line(x, y - tabular.rowAscent(row),
3041 x2, y - tabular.rowAscent(row),
3042 on_off ? onoffcol : col,
3043 on_off ? Painter::line_onoffdash : Painter::line_solid);
3045 on_off = !tabular.bottomLine(cell);
3046 pain.line(x, y + tabular.rowDescent(row),
3047 x2, y + tabular.rowDescent(row),
3048 on_off ? onoffcol : col,
3049 on_off ? Painter::line_onoffdash : Painter::line_solid);
3050 if (!tabular.leftAlreadyDrawn(cell)) {
3051 on_off = !tabular.leftLine(cell);
3052 pain.line(x, y - tabular.rowAscent(row),
3053 x, y + tabular.rowDescent(row),
3054 on_off ? onoffcol : col,
3055 on_off ? Painter::line_onoffdash : Painter::line_solid);
3057 on_off = !tabular.rightLine(cell);
3058 pain.line(x2 - tabular.getAdditionalWidth(cell),
3059 y - tabular.rowAscent(row),
3060 x2 - tabular.getAdditionalWidth(cell),
3061 y + tabular.rowDescent(row),
3062 on_off ? onoffcol : col,
3063 on_off ? Painter::line_onoffdash : Painter::line_solid);
3067 docstring InsetTabular::editMessage() const
3069 return _("Opened table");
3073 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3075 //lyxerr << "InsetTabular::edit: " << this << endl;
3077 cur.selection() = false;
3080 if (isRightToLeft(cur))
3081 cur.idx() = tabular.getLastCellInRow(0);
3087 if (isRightToLeft(cur))
3088 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3090 cur.idx() = tabular.cellCount() - 1;
3092 cur.pos() = cur.lastpos(); // FIXME crude guess
3094 // FIXME: this accesses the position cache before it is initialized
3096 //cur.bv().fitCursor();
3100 void InsetTabular::updateLabels(ParIterator const & it)
3102 // In a longtable, tell captions what the current float is
3103 Counters & cnts = buffer().params().documentClass().counters();
3104 string const saveflt = cnts.current_float();
3105 if (tabular.isLongTabular())
3106 cnts.current_float("table");
3108 ParIterator it2 = it;
3110 size_t const end = it2.nargs();
3111 for ( ; it2.idx() < end; it2.top().forwardIdx())
3112 lyx::updateLabels(buffer(), it2);
3115 if (tabular.isLongTabular())
3116 cnts.current_float(saveflt);
3120 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3122 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3123 << "\n cur:" << cur);
3124 CursorSlice sl = cur.top();
3125 Cursor & bvcur = cur.bv().cursor();
3127 switch (cmd.action) {
3129 case LFUN_MOUSE_PRESS: {
3130 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3132 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3133 || cmd.x > xo(cur.bv()) + tabular.width()) {
3134 row_type r = rowFromY(cur, cmd.y);
3135 cur.idx() = tabular.getFirstCellInRow(r);
3138 cur.idx() = tabular.getLastCellInRow(r);
3139 cur.pos() = cur.lastpos();
3140 cur.selection() = true;
3146 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3147 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3148 || cmd.y > y0 + tabular.height()) {
3149 col_type c = columnFromX(cur, cmd.x);
3150 cur.idx() = tabular.cellIndex(0, c);
3153 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3154 cur.pos() = cur.lastpos();
3155 cur.selection() = true;
3160 // do not reset cursor/selection if we have selected
3161 // some cells (bug 2715).
3162 if (cmd.button() == mouse_button::button3
3163 && &bvcur.selBegin().inset() == this
3164 && tablemode(bvcur))
3167 // Let InsetTableCell do it
3168 cell(cur.idx())->dispatch(cur, cmd);
3171 case LFUN_MOUSE_MOTION:
3172 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3173 if (cmd.button() == mouse_button::button1) {
3174 // only accept motions to places not deeper nested than the real anchor
3175 if (!bvcur.anchor_.hasPart(cur)) {
3179 // select (additional) row
3181 row_type r = rowFromY(cur, cmd.y);
3182 cur.idx() = tabular.getLastCellInRow(r);
3183 bvcur.setCursor(cur);
3184 bvcur.selection() = true;
3187 // select (additional) column
3189 col_type c = columnFromX(cur, cmd.x);
3190 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3191 bvcur.setCursor(cur);
3192 bvcur.selection() = true;
3195 // only update if selection changes
3196 if (bvcur.idx() == cur.idx() &&
3197 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3199 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3200 bvcur.setCursor(cur);
3201 bvcur.selection() = true;
3205 case LFUN_MOUSE_RELEASE:
3210 case LFUN_CELL_BACKWARD:
3212 cur.selection() = false;
3215 case LFUN_CELL_FORWARD:
3217 cur.selection() = false;
3219 case LFUN_CHAR_FORWARD_SELECT:
3220 case LFUN_CHAR_FORWARD:
3221 cell(cur.idx())->dispatch(cur, cmd);
3222 if (!cur.result().dispatched()) {
3224 if (sl == cur.top())
3225 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3231 case LFUN_CHAR_BACKWARD_SELECT:
3232 case LFUN_CHAR_BACKWARD:
3233 cell(cur.idx())->dispatch(cur, cmd);
3234 if (!cur.result().dispatched()) {
3236 if (sl == cur.top())
3237 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3243 case LFUN_CHAR_RIGHT_SELECT:
3244 case LFUN_CHAR_RIGHT:
3245 //FIXME: for visual cursor, really move right
3246 if (isRightToLeft(cur))
3247 lyx::dispatch(FuncRequest(
3248 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3249 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3251 lyx::dispatch(FuncRequest(
3252 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3253 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3256 case LFUN_CHAR_LEFT_SELECT:
3257 case LFUN_CHAR_LEFT:
3258 //FIXME: for visual cursor, really move left
3259 if (isRightToLeft(cur))
3260 lyx::dispatch(FuncRequest(
3261 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3262 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3264 lyx::dispatch(FuncRequest(
3265 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3266 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3269 case LFUN_DOWN_SELECT:
3271 cell(cur.idx())->dispatch(cur, cmd);
3272 cur.dispatched(); // override the cell's decision
3273 if (sl == cur.top())
3274 // if our Text didn't do anything to the cursor
3275 // then we try to put the cursor into the cell below
3276 // setting also the right targetX.
3277 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3278 cur.idx() = tabular.cellBelow(cur.idx());
3280 TextMetrics const & tm =
3281 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3282 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3284 if (sl == cur.top()) {
3285 // we trick it to go to forward after leaving the
3287 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3292 case LFUN_UP_SELECT:
3294 cell(cur.idx())->dispatch(cur, cmd);
3295 cur.dispatched(); // override the cell's decision
3296 if (sl == cur.top())
3297 // if our Text didn't do anything to the cursor
3298 // then we try to put the cursor into the cell above
3299 // setting also the right targetX.
3300 if (tabular.cellRow(cur.idx()) != 0) {
3301 cur.idx() = tabular.cellAbove(cur.idx());
3302 cur.pit() = cur.lastpit();
3303 Text const * text = cell(cur.idx())->getText(0);
3304 TextMetrics const & tm = cur.bv().textMetrics(text);
3305 ParagraphMetrics const & pm =
3306 tm.parMetrics(cur.lastpit());
3307 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3309 if (sl == cur.top()) {
3310 cmd = FuncRequest(LFUN_UP);
3315 // case LFUN_SCREEN_DOWN: {
3316 // //if (hasSelection())
3317 // // cur.selection() = false;
3318 // col_type const col = tabular.cellColumn(cur.idx());
3319 // int const t = cur.bv().top_y() + cur.bv().height();
3320 // if (t < yo() + tabular.getHeightOfTabular()) {
3321 // cur.bv().scrollDocView(t);
3322 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3324 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3331 // case LFUN_SCREEN_UP: {
3332 // //if (hasSelection())
3333 // // cur.selection() = false;
3334 // col_type const col = tabular.cellColumn(cur.idx());
3335 // int const t = cur.bv().top_y() + cur.bv().height();
3337 // cur.bv().scrollDocView(t);
3341 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3345 // cur.par() = cur.lastpar();
3346 // cur.pos() = cur.lastpos();
3350 case LFUN_LAYOUT_TABULAR:
3351 cur.bv().showDialog("tabular", params2string(*this), this);
3354 case LFUN_INSET_DIALOG_UPDATE:
3355 cur.bv().updateDialog("tabular", params2string(*this));
3358 case LFUN_TABULAR_FEATURE:
3359 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3363 // insert file functions
3364 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3365 case LFUN_FILE_INSERT_PLAINTEXT: {
3367 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3368 FileName(to_utf8(cmd.argument())));
3371 cur.recordUndoInset(INSERT_UNDO);
3372 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3373 // content has been replaced,
3374 // so cursor might be invalid
3375 cur.pos() = cur.lastpos();
3376 cur.pit() = cur.lastpit();
3377 bvcur.setCursor(cur);
3384 if (tablemode(cur)) {
3385 if (copySelection(cur)) {
3386 cur.recordUndoInset(DELETE_UNDO);
3391 cell(cur.idx())->dispatch(cur, cmd);
3394 case LFUN_CHAR_DELETE_BACKWARD:
3395 case LFUN_CHAR_DELETE_FORWARD:
3396 if (tablemode(cur)) {
3397 cur.recordUndoInset(DELETE_UNDO);
3401 cell(cur.idx())->dispatch(cur, cmd);
3405 if (!cur.selection())
3407 if (tablemode(cur)) {
3411 cell(cur.idx())->dispatch(cur, cmd);
3414 case LFUN_CLIPBOARD_PASTE:
3415 case LFUN_PRIMARY_SELECTION_PASTE: {
3416 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3417 theClipboard().getAsText() :
3418 theSelection().get();
3421 // pass to InsertPlaintextString, but
3422 // only if we have multi-cell content
3423 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3424 cur.recordUndoInset(INSERT_UNDO);
3425 if (insertPlaintextString(cur.bv(), clip, false)) {
3426 // content has been replaced,
3427 // so cursor might be invalid
3428 cur.pos() = cur.lastpos();
3429 cur.pit() = cur.lastpit();
3430 bvcur.setCursor(cur);
3434 // Let the cell handle normal text
3435 cell(cur.idx())->dispatch(cur, cmd);
3440 if (tabularStackDirty() && theClipboard().isInternal() ||
3441 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3442 cur.recordUndoInset(INSERT_UNDO);
3443 pasteClipboard(cur);
3446 cell(cur.idx())->dispatch(cur, cmd);
3449 case LFUN_FONT_EMPH:
3450 case LFUN_FONT_BOLD:
3451 case LFUN_FONT_ROMAN:
3452 case LFUN_FONT_NOUN:
3453 case LFUN_FONT_ITAL:
3454 case LFUN_FONT_FRAK:
3455 case LFUN_FONT_TYPEWRITER:
3456 case LFUN_FONT_SANS:
3457 case LFUN_FONT_FREE_APPLY:
3458 case LFUN_FONT_FREE_UPDATE:
3459 case LFUN_FONT_SIZE:
3460 case LFUN_FONT_UNDERLINE:
3462 case LFUN_WORD_CAPITALIZE:
3463 case LFUN_WORD_UPCASE:
3464 case LFUN_WORD_LOWCASE:
3465 case LFUN_CHARS_TRANSPOSE:
3466 if (tablemode(cur)) {
3469 getSelection(cur, rs, re, cs, ce);
3470 Cursor tmpcur = cur;
3471 for (row_type i = rs; i <= re; ++i) {
3472 for (col_type j = cs; j <= ce; ++j) {
3473 // cursor follows cell:
3474 tmpcur.idx() = tabular.cellIndex(i, j);
3475 // select this cell only:
3478 tmpcur.resetAnchor();
3479 tmpcur.pit() = tmpcur.lastpit();
3480 tmpcur.pos() = tmpcur.top().lastpos();
3481 tmpcur.setCursor(tmpcur);
3482 tmpcur.setSelection();
3483 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3488 cell(cur.idx())->dispatch(cur, cmd);
3492 // we try to handle this event in the insets dispatch function.
3493 cell(cur.idx())->dispatch(cur, cmd);
3499 // function sets an object as defined in func_status.h:
3500 // states OK, Unknown, Disabled, On, Off.
3501 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3502 FuncStatus & status) const
3504 switch (cmd.action) {
3505 case LFUN_TABULAR_FEATURE: {
3506 int action = Tabular::LAST_ACTION;
3508 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3509 string const tmp = tabularFeature[i].feature;
3510 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3511 action = tabularFeature[i].action;
3515 if (action == Tabular::LAST_ACTION) {
3517 status.unknown(true);
3521 string const argument
3522 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3524 row_type sel_row_start = 0;
3525 row_type sel_row_end = 0;
3526 col_type sel_col_start = 0;
3527 col_type sel_col_end = 0;
3528 Tabular::ltType dummyltt;
3531 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3534 case Tabular::SET_PWIDTH:
3535 case Tabular::SET_MPWIDTH:
3536 case Tabular::SET_SPECIAL_COLUMN:
3537 case Tabular::SET_SPECIAL_MULTI:
3538 case Tabular::APPEND_ROW:
3539 case Tabular::APPEND_COLUMN:
3540 case Tabular::DELETE_ROW:
3541 case Tabular::DELETE_COLUMN:
3542 case Tabular::COPY_ROW:
3543 case Tabular::COPY_COLUMN:
3544 case Tabular::SET_ALL_LINES:
3545 case Tabular::UNSET_ALL_LINES:
3546 case Tabular::SET_TOP_SPACE:
3547 case Tabular::SET_BOTTOM_SPACE:
3548 case Tabular::SET_INTERLINE_SPACE:
3549 case Tabular::SET_BORDER_LINES:
3553 case Tabular::MULTICOLUMN:
3554 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3557 case Tabular::TOGGLE_LINE_TOP:
3558 status.setOnOff(tabular.topLine(cur.idx()));
3561 case Tabular::TOGGLE_LINE_BOTTOM:
3562 status.setOnOff(tabular.bottomLine(cur.idx()));
3565 case Tabular::TOGGLE_LINE_LEFT:
3566 status.setOnOff(tabular.leftLine(cur.idx()));
3569 case Tabular::TOGGLE_LINE_RIGHT:
3570 status.setOnOff(tabular.rightLine(cur.idx()));
3573 case Tabular::M_ALIGN_LEFT:
3575 case Tabular::ALIGN_LEFT:
3576 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3579 case Tabular::M_ALIGN_RIGHT:
3581 case Tabular::ALIGN_RIGHT:
3582 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3585 case Tabular::M_ALIGN_CENTER:
3587 case Tabular::ALIGN_CENTER:
3588 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3591 case Tabular::ALIGN_BLOCK:
3592 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3593 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3596 case Tabular::M_VALIGN_TOP:
3598 case Tabular::VALIGN_TOP:
3600 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3603 case Tabular::M_VALIGN_BOTTOM:
3605 case Tabular::VALIGN_BOTTOM:
3607 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3610 case Tabular::M_VALIGN_MIDDLE:
3612 case Tabular::VALIGN_MIDDLE:
3614 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3617 case Tabular::SET_LONGTABULAR:
3618 status.setOnOff(tabular.isLongTabular());
3621 case Tabular::UNSET_LONGTABULAR:
3622 status.setOnOff(!tabular.isLongTabular());
3625 case Tabular::TOGGLE_ROTATE_TABULAR:
3626 case Tabular::SET_ROTATE_TABULAR:
3627 status.setOnOff(tabular.getRotateTabular());
3630 case Tabular::UNSET_ROTATE_TABULAR:
3631 status.setOnOff(!tabular.getRotateTabular());
3634 case Tabular::TOGGLE_ROTATE_CELL:
3635 case Tabular::SET_ROTATE_CELL:
3636 status.setOnOff(!oneCellHasRotationState(false,
3637 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3640 case Tabular::UNSET_ROTATE_CELL:
3641 status.setOnOff(!oneCellHasRotationState(true,
3642 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3645 case Tabular::SET_USEBOX:
3646 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3649 case Tabular::SET_LTFIRSTHEAD:
3650 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3653 case Tabular::UNSET_LTFIRSTHEAD:
3654 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3657 case Tabular::SET_LTHEAD:
3658 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3661 case Tabular::UNSET_LTHEAD:
3662 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3665 case Tabular::SET_LTFOOT:
3666 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3669 case Tabular::UNSET_LTFOOT:
3670 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3673 case Tabular::SET_LTLASTFOOT:
3674 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3677 case Tabular::UNSET_LTLASTFOOT:
3678 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3681 case Tabular::SET_LTNEWPAGE:
3682 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3685 case Tabular::SET_BOOKTABS:
3686 status.setOnOff(tabular.useBookTabs());
3689 case Tabular::UNSET_BOOKTABS:
3690 status.setOnOff(!tabular.useBookTabs());
3695 status.enabled(false);
3701 // These are only enabled inside tabular
3702 case LFUN_CELL_BACKWARD:
3703 case LFUN_CELL_FORWARD:
3704 status.enabled(true);
3707 // disable these with multiple cells selected
3708 case LFUN_INSET_INSERT:
3709 case LFUN_TABULAR_INSERT:
3710 case LFUN_FLEX_INSERT:
3711 case LFUN_FLOAT_INSERT:
3712 case LFUN_FLOAT_WIDE_INSERT:
3713 case LFUN_FOOTNOTE_INSERT:
3714 case LFUN_MARGINALNOTE_INSERT:
3715 case LFUN_MATH_INSERT:
3716 case LFUN_MATH_MODE:
3717 case LFUN_MATH_MUTATE:
3718 case LFUN_MATH_DISPLAY:
3719 case LFUN_NOTE_INSERT:
3720 case LFUN_OPTIONAL_INSERT:
3721 case LFUN_BOX_INSERT:
3722 case LFUN_BRANCH_INSERT:
3723 case LFUN_WRAP_INSERT:
3724 case LFUN_ERT_INSERT: {
3725 if (tablemode(cur)) {
3726 status.enabled(false);
3729 return cell(cur.idx())->getStatus(cur, cmd, status);
3732 // disable in non-fixed-width cells
3733 case LFUN_NEWLINE_INSERT:
3734 case LFUN_BREAK_PARAGRAPH:
3735 case LFUN_BREAK_PARAGRAPH_SKIP: {
3736 if (tabular.getPWidth(cur.idx()).zero()) {
3737 status.enabled(false);
3740 return cell(cur.idx())->getStatus(cur, cmd, status);
3744 if (tabularStackDirty() && theClipboard().isInternal()) {
3745 status.enabled(true);
3748 return cell(cur.idx())->getStatus(cur, cmd, status);
3750 case LFUN_INSET_MODIFY:
3751 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3752 status.enabled(true);
3758 // we try to handle this event in the insets dispatch function.
3759 return cell(cur.idx())->getStatus(cur, cmd, status);
3764 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3766 return tabular.latex(os, runparams);
3770 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3772 os << '\n'; // output table on a new line
3773 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3774 tabular.plaintext(os, runparams, dp, false, 0);
3775 return PLAINTEXT_NEWLINE;
3779 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3784 // FIXME: Why not pass a proper DocIterator here?
3786 // if the table is inside a float it doesn't need the informaltable
3787 // wrapper. Search for it.
3788 for (master = owner(); master; master = master->owner())
3789 if (master->lyxCode() == FLOAT_CODE)
3794 os << "<informaltable>";
3797 ret += tabular.docbook(os, runparams);
3799 os << "</informaltable>";
3806 void InsetTabular::validate(LaTeXFeatures & features) const
3808 tabular.validate(features);
3812 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3814 return tabular.cellInset(idx);
3818 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3820 return tabular.cellInset(idx);
3824 void InsetTabular::cursorPos(BufferView const & bv,
3825 CursorSlice const & sl, bool boundary, int & x, int & y) const
3827 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3829 // y offset correction
3830 int const row = tabular.cellRow(sl.idx());
3831 for (int i = 0; i <= row; ++i) {
3833 y += tabular.rowAscent(i);
3834 y += tabular.getAdditionalHeight(i);
3837 y += tabular.rowDescent(i);
3840 // x offset correction
3841 int const col = tabular.cellColumn(sl.idx());
3842 int idx = tabular.cellIndex(row, 0);
3843 for (int j = 0; j < col; ++j) {
3844 if (tabular.isPartOfMultiColumn(row, j))
3846 x += tabular.columnWidth(idx);
3849 x += tabular.getBeginningOfTextInCell(idx);
3850 x += ADD_TO_TABULAR_WIDTH;
3855 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3859 Inset const & inset = *tabular.cellInset(cell);
3860 Point o = bv.coordCache().getInsets().xy(&inset);
3861 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3862 int const xend = xbeg + tabular.columnWidth(cell);
3863 row_type const row = tabular.cellRow(cell);
3864 int const ybeg = o.y_ - tabular.rowAscent(row) -
3865 tabular.getAdditionalHeight(row);
3866 int const yend = o.y_ + tabular.rowDescent(row);
3878 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3879 // << " ybeg=" << ybeg << " yend=" << yend
3880 // << " xx=" << xx << " yy=" << yy
3881 // << " dist=" << xx + yy << endl;
3886 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3888 //lyxerr << "InsetTabular::editXY: " << this << endl;
3889 cur.selection() = false;
3891 cur.idx() = getNearestCell(cur.bv(), x, y);
3893 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3897 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3899 cur.idx() = getNearestCell(cur.bv(), x, y);
3900 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3904 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3906 idx_type idx_min = 0;
3907 int dist_min = numeric_limits<int>::max();
3908 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3909 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3910 int const d = dist(bv, i, x, y);
3921 int InsetTabular::cellXPos(idx_type const cell) const
3925 for (; !tabular.isFirstCellInRow(c); --c)
3928 for (; c < cell; ++c)
3929 lx += tabular.columnWidth(c);
3935 void InsetTabular::resetPos(Cursor & cur) const
3937 BufferView & bv = cur.bv();
3938 int const maxwidth = bv.workWidth();
3940 if (&cur.inset() != this) {
3944 int const X2 = maxwidth;
3945 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3946 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3947 int const x2 = x1 + tabular.columnWidth(cur.idx());
3950 scx_ = X1 + 20 - x1;
3952 scx_ = X2 - 20 - x2;
3957 cur.updateFlags(Update::Force | Update::FitCursor);
3961 void InsetTabular::moveNextCell(Cursor & cur)
3963 if (isRightToLeft(cur)) {
3964 if (tabular.isFirstCellInRow(cur.idx())) {
3965 row_type const row = tabular.cellRow(cur.idx());
3966 if (row == tabular.rowCount() - 1)
3968 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3975 if (tabular.isLastCell(cur.idx()))
3985 void InsetTabular::movePrevCell(Cursor & cur)
3987 if (isRightToLeft(cur)) {
3988 if (tabular.isLastCellInRow(cur.idx())) {
3989 row_type const row = tabular.cellRow(cur.idx());
3992 cur.idx() = tabular.getFirstCellInRow(row);
3993 cur.idx() = tabular.cellAbove(cur.idx());
3995 if (tabular.isLastCell(cur.idx()))
4000 if (cur.idx() == 0) // first cell
4004 cur.pit() = cur.lastpit();
4005 cur.pos() = cur.lastpos();
4007 // FIXME: this accesses the position cache before it is initialized
4012 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4014 Tabular::Feature action = Tabular::LAST_ACTION;
4017 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4018 string const tmp = tabularFeature[i].feature;
4020 if (tmp == what.substr(0, tmp.length())) {
4021 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4022 //tabularFeatures[i].feature.length()))
4023 action = tabularFeature[i].action;
4027 if (action == Tabular::LAST_ACTION)
4031 ltrim(what.substr(tabularFeature[i].feature.length()));
4032 tabularFeatures(cur, action, val);
4037 static void checkLongtableSpecial(Tabular::ltType & ltt,
4038 string const & special, bool & flag)
4040 if (special == "dl_above") {
4043 } else if (special == "dl_below") {
4044 ltt.bottomDL = flag;
4046 } else if (special == "empty") {
4055 bool InsetTabular::oneCellHasRotationState(bool rotated,
4056 row_type row_start, row_type row_end,
4057 col_type col_start, col_type col_end) const {
4059 for (row_type i = row_start; i <= row_end; ++i) {
4060 for (col_type j = col_start; j <= col_end; ++j) {
4061 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4070 void InsetTabular::tabularFeatures(Cursor & cur,
4071 Tabular::Feature feature, string const & value)
4073 col_type sel_col_start;
4074 col_type sel_col_end;
4075 row_type sel_row_start;
4076 row_type sel_row_end;
4077 bool setLines = false;
4078 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4079 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4083 case Tabular::M_ALIGN_LEFT:
4084 case Tabular::ALIGN_LEFT:
4085 setAlign = LYX_ALIGN_LEFT;
4088 case Tabular::M_ALIGN_RIGHT:
4089 case Tabular::ALIGN_RIGHT:
4090 setAlign = LYX_ALIGN_RIGHT;
4093 case Tabular::M_ALIGN_CENTER:
4094 case Tabular::ALIGN_CENTER:
4095 setAlign = LYX_ALIGN_CENTER;
4098 case Tabular::ALIGN_BLOCK:
4099 setAlign = LYX_ALIGN_BLOCK;
4102 case Tabular::M_VALIGN_TOP:
4103 case Tabular::VALIGN_TOP:
4104 setVAlign = Tabular::LYX_VALIGN_TOP;
4107 case Tabular::M_VALIGN_BOTTOM:
4108 case Tabular::VALIGN_BOTTOM:
4109 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4112 case Tabular::M_VALIGN_MIDDLE:
4113 case Tabular::VALIGN_MIDDLE:
4114 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4121 cur.recordUndoInset(ATOMIC_UNDO);
4123 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4124 row_type const row = tabular.cellRow(cur.idx());
4125 col_type const column = tabular.cellColumn(cur.idx());
4127 Tabular::ltType ltt;
4131 case Tabular::SET_PWIDTH: {
4132 Length const len(value);
4133 tabular.setColumnPWidth(cur, cur.idx(), len);
4135 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4136 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4140 case Tabular::SET_MPWIDTH:
4141 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4144 case Tabular::SET_SPECIAL_COLUMN:
4145 case Tabular::SET_SPECIAL_MULTI:
4146 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4149 case Tabular::APPEND_ROW:
4150 // append the row into the tabular
4151 tabular.appendRow(cur.idx());
4154 case Tabular::APPEND_COLUMN:
4155 // append the column into the tabular
4156 tabular.appendColumn(cur.idx());
4157 cur.idx() = tabular.cellIndex(row, column);
4160 case Tabular::DELETE_ROW:
4161 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4162 tabular.deleteRow(sel_row_start);
4163 if (sel_row_start >= tabular.rowCount())
4165 cur.idx() = tabular.cellIndex(sel_row_start, column);
4168 cur.selection() = false;
4171 case Tabular::DELETE_COLUMN:
4172 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4173 tabular.deleteColumn(sel_col_start);
4174 if (sel_col_start >= tabular.columnCount())
4176 cur.idx() = tabular.cellIndex(row, sel_col_start);
4179 cur.selection() = false;
4182 case Tabular::COPY_ROW:
4183 tabular.copyRow(row);
4186 case Tabular::COPY_COLUMN:
4187 tabular.copyColumn(column);
4188 cur.idx() = tabular.cellIndex(row, column);
4191 case Tabular::TOGGLE_LINE_TOP: {
4192 bool lineSet = !tabular.topLine(cur.idx());
4193 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4194 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4195 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4199 case Tabular::TOGGLE_LINE_BOTTOM: {
4200 bool lineSet = !tabular.bottomLine(cur.idx());
4201 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4202 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4203 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4207 case Tabular::TOGGLE_LINE_LEFT: {
4208 bool lineSet = !tabular.leftLine(cur.idx());
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.setLeftLine(tabular.cellIndex(i, j), lineSet);
4215 case Tabular::TOGGLE_LINE_RIGHT: {
4216 bool lineSet = !tabular.rightLine(cur.idx());
4217 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4218 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4219 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4223 case Tabular::M_ALIGN_LEFT:
4224 case Tabular::M_ALIGN_RIGHT:
4225 case Tabular::M_ALIGN_CENTER:
4227 case Tabular::ALIGN_LEFT:
4228 case Tabular::ALIGN_RIGHT:
4229 case Tabular::ALIGN_CENTER:
4230 case Tabular::ALIGN_BLOCK:
4231 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4232 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4233 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4236 case Tabular::M_VALIGN_TOP:
4237 case Tabular::M_VALIGN_BOTTOM:
4238 case Tabular::M_VALIGN_MIDDLE:
4240 case Tabular::VALIGN_TOP:
4241 case Tabular::VALIGN_BOTTOM:
4242 case Tabular::VALIGN_MIDDLE:
4243 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4244 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4245 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4248 case Tabular::MULTICOLUMN: {
4249 if (sel_row_start != sel_row_end) {
4250 // FIXME: Need I say it ? This is horrible.
4252 Alert::error(_("Error setting multicolumn"),
4253 _("You cannot set multicolumn vertically."));
4256 if (!cur.selection()) {
4257 // just multicol for one single cell
4258 // check whether we are completely in a multicol
4259 if (tabular.isMultiColumn(cur.idx()))
4260 tabular.unsetMultiColumn(cur.idx());
4262 tabular.setMultiColumn(cur.idx(), 1);
4265 // we have a selection so this means we just add all this
4266 // cells to form a multicolumn cell
4267 idx_type const s_start = cur.selBegin().idx();
4268 idx_type const s_end = cur.selEnd().idx();
4269 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4270 cur.idx() = s_start;
4273 cur.selection() = false;
4277 case Tabular::SET_ALL_LINES:
4279 case Tabular::UNSET_ALL_LINES:
4280 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4281 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4282 tabular.setAllLines(
4283 tabular.cellIndex(i,j), setLines);
4286 case Tabular::SET_BORDER_LINES:
4287 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4288 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4289 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4291 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4292 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4293 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4297 case Tabular::SET_LONGTABULAR:
4298 tabular.setLongTabular(true);
4301 case Tabular::UNSET_LONGTABULAR:
4302 tabular.setLongTabular(false);
4305 case Tabular::SET_ROTATE_TABULAR:
4306 tabular.setRotateTabular(true);
4309 case Tabular::UNSET_ROTATE_TABULAR:
4310 tabular.setRotateTabular(false);
4313 case Tabular::TOGGLE_ROTATE_TABULAR:
4314 tabular.setRotateTabular(!tabular.getRotateTabular());
4317 case Tabular::SET_ROTATE_CELL:
4318 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4319 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4320 tabular.setRotateCell(
4321 tabular.cellIndex(i, j), true);
4324 case Tabular::UNSET_ROTATE_CELL:
4325 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4326 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4327 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4330 case Tabular::TOGGLE_ROTATE_CELL:
4332 bool oneNotRotated = oneCellHasRotationState(false,
4333 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4335 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4336 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4337 tabular.setRotateCell(tabular.cellIndex(i, j),
4342 case Tabular::SET_USEBOX: {
4343 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4344 if (val == tabular.getUsebox(cur.idx()))
4345 val = Tabular::BOX_NONE;
4346 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4347 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4348 tabular.setUsebox(tabular.cellIndex(i, j), val);
4352 case Tabular::UNSET_LTFIRSTHEAD:
4354 case Tabular::SET_LTFIRSTHEAD:
4355 tabular.getRowOfLTFirstHead(row, ltt);
4356 checkLongtableSpecial(ltt, value, flag);
4357 tabular.setLTHead(row, flag, ltt, true);
4360 case Tabular::UNSET_LTHEAD:
4362 case Tabular::SET_LTHEAD:
4363 tabular.getRowOfLTHead(row, ltt);
4364 checkLongtableSpecial(ltt, value, flag);
4365 tabular.setLTHead(row, flag, ltt, false);
4368 case Tabular::UNSET_LTFOOT:
4370 case Tabular::SET_LTFOOT:
4371 tabular.getRowOfLTFoot(row, ltt);
4372 checkLongtableSpecial(ltt, value, flag);
4373 tabular.setLTFoot(row, flag, ltt, false);
4376 case Tabular::UNSET_LTLASTFOOT:
4378 case Tabular::SET_LTLASTFOOT:
4379 tabular.getRowOfLTLastFoot(row, ltt);
4380 checkLongtableSpecial(ltt, value, flag);
4381 tabular.setLTFoot(row, flag, ltt, true);
4384 case Tabular::SET_LTNEWPAGE:
4385 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4388 case Tabular::SET_BOOKTABS:
4389 tabular.setBookTabs(true);
4392 case Tabular::UNSET_BOOKTABS:
4393 tabular.setBookTabs(false);
4396 case Tabular::SET_TOP_SPACE: {
4398 if (value == "default")
4399 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4400 tabular.row_info[i].top_space_default = true;
4401 else if (isValidLength(value, &len))
4402 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4403 tabular.row_info[i].top_space_default = false;
4404 tabular.row_info[i].top_space = len;
4407 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4408 tabular.row_info[i].top_space_default = false;
4409 tabular.row_info[i].top_space = len;
4414 case Tabular::SET_BOTTOM_SPACE: {
4416 if (value == "default")
4417 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4418 tabular.row_info[i].bottom_space_default = true;
4419 else if (isValidLength(value, &len))
4420 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4421 tabular.row_info[i].bottom_space_default = false;
4422 tabular.row_info[i].bottom_space = len;
4425 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4426 tabular.row_info[i].bottom_space_default = false;
4427 tabular.row_info[i].bottom_space = len;
4432 case Tabular::SET_INTERLINE_SPACE: {
4434 if (value == "default")
4435 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4436 tabular.row_info[i].interline_space_default = true;
4437 else if (isValidLength(value, &len))
4438 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4439 tabular.row_info[i].interline_space_default = false;
4440 tabular.row_info[i].interline_space = len;
4443 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4444 tabular.row_info[i].interline_space_default = false;
4445 tabular.row_info[i].interline_space = len;
4450 // dummy stuff just to avoid warnings
4451 case Tabular::LAST_ACTION:
4457 bool InsetTabular::showInsetDialog(BufferView * bv) const
4459 bv->showDialog("tabular", params2string(*this),
4460 const_cast<InsetTabular *>(this));
4465 void InsetTabular::openLayoutDialog(BufferView * bv) const
4467 bv->showDialog("tabular", params2string(*this),
4468 const_cast<InsetTabular *>(this));
4472 bool InsetTabular::copySelection(Cursor & cur)
4474 if (!cur.selection())
4479 getSelection(cur, rs, re, cs, ce);
4481 paste_tabular.reset(new Tabular(tabular));
4483 for (row_type i = 0; i < rs; ++i)
4484 paste_tabular->deleteRow(0);
4486 row_type const rows = re - rs + 1;
4487 while (paste_tabular->rowCount() > rows)
4488 paste_tabular->deleteRow(rows);
4490 for (col_type i = 0; i < cs; ++i)
4491 paste_tabular->deleteColumn(0);
4493 col_type const columns = ce - cs + 1;
4494 while (paste_tabular->columnCount() > columns)
4495 paste_tabular->deleteColumn(columns);
4497 // We clear all the InsetTableCell pointers, since they
4498 // might now become invalid and there is no point in having
4499 // them point to temporary things in paste_tabular.
4500 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4501 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4502 paste_tabular->cellInset(i,j)->setCellData(0);
4503 paste_tabular->cellInset(i,j)->setTabular(0);
4506 odocstringstream os;
4507 OutputParams const runparams(0);
4508 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4509 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4510 cap::copySelection(cur, os.str());
4512 // mark tabular stack dirty
4513 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4514 // when we (hopefully) have a one-for-all paste mechanism.
4515 // This must be called after cap::copySelection.
4516 dirtyTabularStack(true);
4522 bool InsetTabular::pasteClipboard(Cursor & cur)
4526 col_type const actcol = tabular.cellColumn(cur.idx());
4527 row_type const actrow = tabular.cellRow(cur.idx());
4528 for (row_type r1 = 0, r2 = actrow;
4529 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4531 for (col_type c1 = 0, c2 = actcol;
4532 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4534 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4535 tabular.isPartOfMultiColumn(r2, c2))
4537 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4541 if (tabular.isPartOfMultiColumn(r2, c2)) {
4545 shared_ptr<InsetTableCell> inset(
4546 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4547 // note that setCellInset will call InsetTableCell::setCellData()
4548 // and InsetTableCell::setTabular()
4549 tabular.setCellInset(r2, c2, inset);
4550 // FIXME: change tracking (MG)
4551 inset->setChange(Change(cur.buffer().params().trackChanges ?
4552 Change::INSERTED : Change::UNCHANGED));
4560 void InsetTabular::cutSelection(Cursor & cur)
4562 if (!cur.selection())
4567 getSelection(cur, rs, re, cs, ce);
4568 for (row_type i = rs; i <= re; ++i) {
4569 for (col_type j = cs; j <= ce; ++j) {
4570 shared_ptr<InsetTableCell> t
4571 = cell(tabular.cellIndex(i, j));
4572 if (cur.buffer().params().trackChanges)
4573 // FIXME: Change tracking (MG)
4574 t->setChange(Change(Change::DELETED));
4580 // cursor position might be invalid now
4581 if (cur.pit() > cur.lastpit())
4582 cur.pit() = cur.lastpit();
4583 if (cur.pos() > cur.lastpos())
4584 cur.pos() = cur.lastpos();
4585 cur.clearSelection();
4589 bool InsetTabular::isRightToLeft(Cursor & cur) const
4591 LASSERT(cur.depth() > 1, /**/);
4592 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4593 pos_type const parentpos = cur[cur.depth() - 2].pos();
4594 return parentpar.getFontSettings(cur.bv().buffer().params(),
4595 parentpos).language()->rightToLeft();
4599 void InsetTabular::getSelection(Cursor & cur,
4600 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4602 CursorSlice const & beg = cur.selBegin();
4603 CursorSlice const & end = cur.selEnd();
4604 cs = tabular.cellColumn(beg.idx());
4605 ce = tabular.cellColumn(end.idx());
4608 cs = tabular.cellColumn(end.idx());
4610 ce = tabular.cellRightColumn(end.idx());
4613 rs = tabular.cellRow(beg.idx());
4614 re = tabular.cellRow(end.idx());
4620 Text * InsetTabular::getText(int idx) const
4622 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4626 void InsetTabular::setChange(Change const & change)
4628 for (idx_type idx = 0; idx < nargs(); ++idx)
4629 cell(idx)->setChange(change);
4633 void InsetTabular::acceptChanges(BufferParams const & bparams)
4635 for (idx_type idx = 0; idx < nargs(); ++idx)
4636 cell(idx)->acceptChanges(bparams);
4640 void InsetTabular::rejectChanges(BufferParams const & bparams)
4642 for (idx_type idx = 0; idx < nargs(); ++idx)
4643 cell(idx)->rejectChanges(bparams);
4647 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4649 return tabular.getPWidth(cell).zero();
4653 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4655 return !tabular.getPWidth(cell).zero();
4659 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4662 if (buf.length() <= 0)
4667 col_type maxCols = 1;
4668 size_t const len = buf.length();
4672 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4680 maxCols = max(cols, maxCols);
4686 maxCols = max(cols, maxCols);
4692 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4693 loctab = paste_tabular.get();
4695 dirtyTabularStack(true);
4698 cell = bv.cursor().idx();
4699 ocol = tabular.cellColumn(cell);
4700 row = tabular.cellRow(cell);
4704 idx_type const cells = loctab->cellCount();
4707 rows = loctab->rowCount();
4708 col_type const columns = loctab->columnCount();
4710 while (cell < cells && p < len && row < rows &&
4711 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4717 // we can only set this if we are not too far right
4718 if (cols < columns) {
4719 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4720 Font const font = bv.textMetrics(&inset->text_).
4722 inset->setText(buf.substr(op, p - op), font,
4723 buffer().params().trackChanges);
4729 // we can only set this if we are not too far right
4730 if (cols < columns) {
4731 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4732 Font const font = bv.textMetrics(&inset->text_).
4734 inset->setText(buf.substr(op, p - op), font,
4735 buffer().params().trackChanges);
4740 cell = loctab->cellIndex(row, cols);
4746 // check for the last cell if there is no trailing '\n'
4747 if (cell < cells && op < len) {
4748 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4749 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4750 inset->setText(buf.substr(op, len - op), font,
4751 buffer().params().trackChanges);
4757 void InsetTabular::addPreview(PreviewLoader & loader) const
4759 row_type const rows = tabular.rowCount();
4760 col_type const columns = tabular.columnCount();
4761 for (row_type i = 0; i < rows; ++i) {
4762 for (col_type j = 0; j < columns; ++j)
4763 tabular.cellInset(i, j)->addPreview(loader);
4768 bool InsetTabular::tablemode(Cursor & cur) const
4770 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4774 bool InsetTabular::completionSupported(Cursor const & cur) const
4776 Cursor const & bvCur = cur.bv().cursor();
4777 if (&bvCur.inset() != this)
4779 return cur.text()->completionSupported(cur);
4783 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4785 return completionSupported(cur);
4789 bool InsetTabular::automaticInlineCompletion() const
4791 return lyxrc.completion_inline_text;
4795 bool InsetTabular::automaticPopupCompletion() const
4797 return lyxrc.completion_popup_text;
4801 bool InsetTabular::showCompletionCursor() const
4803 return lyxrc.completion_cursor_text;
4807 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4809 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4813 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4815 if (!completionSupported(cur))
4817 return cur.text()->completionPrefix(cur);
4821 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4823 if (!completionSupported(cur))
4826 return cur.text()->insertCompletion(cur, s, finished);
4830 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4831 Dimension & dim) const
4833 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4834 tm.completionPosAndDim(cur, x, y, dim);
4838 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4840 istringstream data(in);
4842 lex.setStream(data);
4849 if (!lex || token != "tabular") {
4850 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4854 // This is part of the inset proper that is usually swallowed
4855 // by Buffer::readInset
4857 if (!lex || token != "Tabular") {
4858 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4866 string InsetTabular::params2string(InsetTabular const & inset)
4869 data << "tabular" << ' ';
4871 data << "\\end_inset\n";