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, leftLine(j));
636 setRightLine(i, rightLine(j));
637 setTopLine(i, topLine(j));
638 if (topLine(j) && bottomLine(j)) {
639 setBottomLine(i, true);
640 setBottomLine(j, false);
642 // mark track changes
644 cellInfo(i).inset->setChange(Change(Change::INSERTED));
649 void Tabular::deleteRow(row_type const row)
651 // Not allowed to delete last row
655 row_info.erase(row_info.begin() + row);
656 cell_info.erase(cell_info.begin() + row);
661 void Tabular::copyRow(row_type const row)
663 row_info.insert(row_info.begin() + row, row_info[row]);
664 cell_info.insert(cell_info.begin() + row, cell_info[row]);
666 if (buffer().params().trackChanges)
667 for (col_type j = 0; j < columnCount(); ++j)
668 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
674 void Tabular::appendColumn(idx_type const cell)
676 col_type const c = cellColumn(cell);
677 column_vector::iterator cit = column_info.begin() + c + 1;
678 column_info.insert(cit, ColumnData());
679 row_type const nrows = rowCount();
680 //col_type const ncols = columnCount();
681 // set the column values of the column before
682 column_info[c + 1] = column_info[c];
684 for (row_type r = 0; r < nrows; ++r) {
685 cell_info[r].insert(cell_info[r].begin() + c + 1,
686 CellData(buffer(), *this));
687 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
688 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
690 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
693 for (row_type r = 0; r < nrows; ++r) {
694 // inherit line settings
695 idx_type const i = cellIndex(r, c + 1);
696 idx_type const j = cellIndex(r, c);
697 setBottomLine(i, bottomLine(j));
698 setTopLine(i, topLine(j));
699 setLeftLine(i, leftLine(j));
700 if (rightLine(j) && rightLine(j)) {
701 setRightLine(i, true);
702 setRightLine(j, false);
705 cellInfo(i).inset->clear();
706 if (buffer().params().trackChanges)
707 cellInfo(i).inset->setChange(Change(Change::INSERTED));
712 void Tabular::deleteColumn(col_type const column)
714 // Not allowed to delete last column
715 if (columnCount() == 1)
718 column_info.erase(column_info.begin() + column);
719 for (row_type i = 0; i < rowCount(); ++i) {
720 // Care about multicolumn cells
721 if (column + 1 < columnCount() &&
722 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
723 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
724 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
726 cell_info[i].erase(cell_info[i].begin() + column);
732 void Tabular::copyColumn(col_type const column)
734 BufferParams const & bp = buffer().params();
735 column_info.insert(column_info.begin() + column, column_info[column]);
737 for (row_type i = 0; i < rowCount(); ++i)
738 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
741 for (row_type i = 0; i < rowCount(); ++i)
742 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
747 void Tabular::updateIndexes()
749 col_type ncols = columnCount();
750 row_type nrows = rowCount();
752 for (row_type row = 0; row < nrows; ++row)
753 for (col_type column = 0; column < ncols; ++column) {
754 if (!isPartOfMultiColumn(row, column))
756 cell_info[row][column].cellno = numberofcells - 1;
759 rowofcell.resize(numberofcells);
760 columnofcell.resize(numberofcells);
762 for (row_type row = 0; row < nrows; ++row)
763 for (col_type column = 0; column < ncols; ++column) {
764 if (isPartOfMultiColumn(row, column))
767 columnofcell[i] = column;
773 Tabular::idx_type Tabular::cellCount() const
775 return numberofcells;
779 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
781 row_type const row = cellRow(cell);
783 for (col_type i = 0; i < columnCount(); ++i)
784 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
790 bool Tabular::topLine(idx_type const cell) const
792 return cellInfo(cell).top_line;
796 bool Tabular::bottomLine(idx_type const cell) const
798 return cellInfo(cell).bottom_line;
802 bool Tabular::leftLine(idx_type cell) const
806 return cellInfo(cell).left_line;
810 bool Tabular::rightLine(idx_type cell) const
814 return cellInfo(cell).right_line;
818 bool Tabular::topAlreadyDrawn(idx_type cell) const
820 row_type row = cellRow(cell);
823 idx_type i = cellIndex(row - 1, cellColumn(cell));
824 return !rowTopLine(row) && bottomLine(i);
828 bool Tabular::leftAlreadyDrawn(idx_type cell) const
830 col_type col = cellColumn(cell);
833 idx_type i = cellIndex(cellRow(cell), col - 1);
834 return rightLine(i) && !leftLine(cell);
838 bool Tabular::isLastRow(idx_type cell) const
840 return cellRow(cell) == rowCount() - 1;
844 int Tabular::getAdditionalHeight(row_type row) const
846 if (!row || row >= rowCount())
849 int const interline_space = row_info[row - 1].interline_space_default ?
851 row_info[row - 1].interline_space.inPixels(width());
852 if (rowTopLine(row) && rowBottomLine(row - 1))
853 return interline_space + WIDTH_OF_LINE;
854 return interline_space;
858 int Tabular::getAdditionalWidth(idx_type cell) const
860 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
862 && nextcol < columnCount() && leftLine(cellIndex(cellRow(cell), nextcol)))
863 return WIDTH_OF_LINE;
868 int Tabular::columnWidth(idx_type cell) const
871 col_type const span = columnSpan(cell);
872 col_type const col = cellColumn(cell);
873 for(col_type c = col; c < col + span ; ++c)
874 w += column_info[c].width;
879 bool Tabular::updateColumnWidths()
881 col_type const ncols = columnCount();
882 row_type const nrows = rowCount();
884 // for each col get max of single col cells
885 for(col_type c = 0; c < ncols; ++c) {
887 for(row_type r = 0; r < nrows; ++r) {
888 idx_type const i = cellIndex(r, c);
889 if (columnSpan(i) == 1)
890 new_width = max(new_width, cellInfo(i).width);
893 if (column_info[c].width != new_width) {
894 column_info[c].width = new_width;
898 // update col widths to fit merged cells
899 for(col_type c = 0; c < ncols; ++c)
900 for(row_type r = 0; r < nrows; ++r) {
901 idx_type const i = cellIndex(r, c);
902 int const span = columnSpan(i);
903 if (span == 1 || c > cellColumn(i))
907 for(col_type j = c; j < c + span ; ++j)
908 old_width += column_info[j].width;
910 if (cellInfo(i).width > old_width) {
911 column_info[c + span - 1].width += cellInfo(i).width - old_width;
920 int Tabular::width() const
922 col_type const ncols = columnCount();
924 for (col_type i = 0; i < ncols; ++i)
925 width += column_info[i].width;
930 void Tabular::setCellWidth(idx_type cell, int new_width)
932 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
933 + getAdditionalWidth(cell);
937 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
940 if (!isMultiColumn(cell) || onlycolumn)
941 column_info[cellColumn(cell)].alignment = align;
943 cellInfo(cell).alignment = align;
947 void Tabular::setVAlignment(idx_type cell, VAlignment align,
950 if (!isMultiColumn(cell) || onlycolumn)
951 column_info[cellColumn(cell)].valignment = align;
953 cellInfo(cell).valignment = align;
960 * Allow line and paragraph breaks for fixed width cells or disallow them,
961 * merge cell paragraphs and reset layout to standard for variable width
964 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
966 inset->setAutoBreakRows(fixedWidth);
970 // merge all paragraphs to one
971 BufferParams const & bp = cur.bv().buffer().params();
972 while (inset->paragraphs().size() > 1)
973 mergeParagraph(bp, inset->paragraphs(), 0);
977 // undo information has already been recorded
978 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
979 bp.documentClass().emptyLayoutName());
986 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
987 Length const & width)
989 col_type const j = cellColumn(cell);
991 column_info[j].p_width = width;
992 for (row_type i = 0; i < rowCount(); ++i) {
993 idx_type const cell = cellIndex(i, j);
994 // because of multicolumns
995 toggleFixedWidth(cur, cellInset(cell).get(),
996 !getPWidth(cell).zero());
998 // cur paragraph can become invalid after paragraphs were merged
999 if (cur.pit() > cur.lastpit())
1000 cur.pit() = cur.lastpit();
1001 // cur position can become invalid after newlines were removed
1002 if (cur.pos() > cur.lastpos())
1003 cur.pos() = cur.lastpos();
1007 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1008 Length const & width)
1010 if (!isMultiColumn(cell))
1013 cellInfo(cell).p_width = width;
1014 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1015 // cur paragraph can become invalid after paragraphs were merged
1016 if (cur.pit() > cur.lastpit())
1017 cur.pit() = cur.lastpit();
1018 // cur position can become invalid after newlines were removed
1019 if (cur.pos() > cur.lastpos())
1020 cur.pos() = cur.lastpos();
1025 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1026 Tabular::Feature what)
1028 if (what == SET_SPECIAL_MULTI)
1029 cellInfo(cell).align_special = special;
1031 column_info[cellColumn(cell)].align_special = special;
1035 void Tabular::setAllLines(idx_type cell, bool line)
1037 setTopLine(cell, line);
1038 setBottomLine(cell, line);
1039 setRightLine(cell, line);
1040 setLeftLine(cell, line);
1044 void Tabular::setTopLine(idx_type i, bool line)
1046 cellInfo(i).top_line = line;
1050 void Tabular::setBottomLine(idx_type i, bool line)
1052 cellInfo(i).bottom_line = line;
1056 void Tabular::setLeftLine(idx_type cell, bool line)
1058 cellInfo(cell).left_line = line;
1062 void Tabular::setRightLine(idx_type cell, bool line)
1064 cellInfo(cell).right_line = line;
1067 bool Tabular::rowTopLine(row_type r) const
1069 idx_type i0 = getFirstCellInRow(r);
1070 idx_type i1 = getLastCellInRow(r);
1071 bool all_rows_set = true;
1072 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1073 all_rows_set = cellInfo(j).top_line;
1074 return all_rows_set;
1078 bool Tabular::rowBottomLine(row_type r) const
1080 idx_type i0 = getFirstCellInRow(r);
1081 idx_type i1 = getLastCellInRow(r);
1082 bool all_rows_set = true;
1083 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1084 all_rows_set = cellInfo(j).bottom_line;
1085 return all_rows_set;
1089 bool Tabular::columnLeftLine(col_type c) const
1091 bool all_cols_set = true;
1092 row_type const nrows = rowCount();
1093 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1094 idx_type i = cellIndex(r, c);
1095 if (c == cellColumn(i))
1096 all_cols_set = cellInfo(i).left_line;
1098 return all_cols_set;
1102 bool Tabular::columnRightLine(col_type c) const
1104 bool all_cols_set = true;
1105 row_type const nrows = rowCount();
1106 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1107 idx_type i = cellIndex(r, c);
1108 if (c == cellColumn(i) + columnSpan(i) - 1)
1109 all_cols_set = cellInfo(i).right_line;
1111 return all_cols_set;
1115 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1117 if (!onlycolumn && isMultiColumn(cell))
1118 return cellInfo(cell).alignment;
1119 return column_info[cellColumn(cell)].alignment;
1124 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1126 if (!onlycolumn && isMultiColumn(cell))
1127 return cellInfo(cell).valignment;
1128 return column_info[cellColumn(cell)].valignment;
1132 Length const Tabular::getPWidth(idx_type cell) const
1134 if (isMultiColumn(cell))
1135 return cellInfo(cell).p_width;
1136 return column_info[cellColumn(cell)].p_width;
1140 Length const Tabular::getColumnPWidth(idx_type cell) const
1142 return column_info[cellColumn(cell)].p_width;
1146 Length const Tabular::getMColumnPWidth(idx_type cell) const
1148 if (isMultiColumn(cell))
1149 return cellInfo(cell).p_width;
1154 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1156 if (what == SET_SPECIAL_MULTI)
1157 return cellInfo(cell).align_special;
1158 return column_info[cellColumn(cell)].align_special;
1162 int Tabular::cellWidth(idx_type cell) const
1164 return cellInfo(cell).width;
1168 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1172 switch (getAlignment(cell)) {
1173 case LYX_ALIGN_CENTER:
1174 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1176 case LYX_ALIGN_RIGHT:
1177 x += columnWidth(cell) - cellWidth(cell);
1178 // + getAdditionalWidth(cell);
1181 // LYX_ALIGN_LEFT: nothing :-)
1185 // the LaTeX Way :-(
1191 bool Tabular::isFirstCellInRow(idx_type cell) const
1193 return cellColumn(cell) == 0;
1197 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1199 if (row > rowCount() - 1)
1200 row = rowCount() - 1;
1201 return cell_info[row][0].cellno;
1205 bool Tabular::isLastCellInRow(idx_type cell) const
1207 return cellRightColumn(cell) == columnCount() - 1;
1211 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1213 if (row > rowCount() - 1)
1214 row = rowCount() - 1;
1215 return cell_info[row][columnCount() - 1].cellno;
1219 Tabular::row_type Tabular::cellRow(idx_type cell) const
1221 if (cell >= cellCount())
1222 return rowCount() - 1;
1225 return rowofcell[cell];
1229 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1231 if (cell >= cellCount())
1232 return columnCount() - 1;
1235 return columnofcell[cell];
1239 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1241 row_type const row = cellRow(cell);
1242 col_type column = cellColumn(cell);
1243 while (column < columnCount() - 1 &&
1244 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1250 void Tabular::write(ostream & os) const
1254 << write_attribute("version", 3)
1255 << write_attribute("rows", rowCount())
1256 << write_attribute("columns", columnCount())
1258 // global longtable options
1260 << write_attribute("rotate", rotate)
1261 << write_attribute("booktabs", use_booktabs)
1262 << write_attribute("islongtable", is_long_tabular)
1263 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1264 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1265 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1266 << write_attribute("headTopDL", endhead.topDL)
1267 << write_attribute("headBottomDL", endhead.bottomDL)
1268 << write_attribute("footTopDL", endfoot.topDL)
1269 << write_attribute("footBottomDL", endfoot.bottomDL)
1270 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1271 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1272 << write_attribute("lastFootEmpty", endlastfoot.empty)
1274 for (col_type j = 0; j < columnCount(); ++j) {
1276 << write_attribute("alignment", column_info[j].alignment)
1277 << write_attribute("valignment", column_info[j].valignment)
1278 << write_attribute("width", column_info[j].p_width.asString())
1279 << write_attribute("special", column_info[j].align_special)
1282 for (row_type i = 0; i < rowCount(); ++i) {
1283 static const string def("default");
1285 if (row_info[i].top_space_default)
1286 os << write_attribute("topspace", def);
1288 os << write_attribute("topspace", row_info[i].top_space);
1289 if (row_info[i].bottom_space_default)
1290 os << write_attribute("bottomspace", def);
1292 os << write_attribute("bottomspace", row_info[i].bottom_space);
1293 if (row_info[i].interline_space_default)
1294 os << write_attribute("interlinespace", def);
1296 os << write_attribute("interlinespace", row_info[i].interline_space);
1297 os << write_attribute("endhead", row_info[i].endhead)
1298 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1299 << write_attribute("endfoot", row_info[i].endfoot)
1300 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1301 << write_attribute("newpage", row_info[i].newpage)
1303 for (col_type j = 0; j < columnCount(); ++j) {
1305 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1306 << write_attribute("alignment", cell_info[i][j].alignment)
1307 << write_attribute("valignment", cell_info[i][j].valignment)
1308 << write_attribute("topline", cell_info[i][j].top_line)
1309 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1310 << write_attribute("leftline", cell_info[i][j].left_line)
1311 << write_attribute("rightline", cell_info[i][j].right_line)
1312 << write_attribute("rotate", cell_info[i][j].rotate)
1313 << write_attribute("usebox", cell_info[i][j].usebox)
1314 << write_attribute("width", cell_info[i][j].p_width)
1315 << write_attribute("special", cell_info[i][j].align_special)
1317 os << "\\begin_inset ";
1318 cell_info[i][j].inset->write(os);
1319 os << "\n\\end_inset\n"
1324 os << "</lyxtabular>\n";
1328 void Tabular::read(Lexer & lex)
1331 istream & is = lex.getStream();
1333 l_getline(is, line);
1334 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1335 LASSERT(false, /**/);
1340 if (!getTokenValue(line, "version", version))
1342 LASSERT(version >= 2, /**/);
1345 if (!getTokenValue(line, "rows", rows_arg))
1348 if (!getTokenValue(line, "columns", columns_arg))
1350 init(buffer(), rows_arg, columns_arg);
1351 l_getline(is, line);
1352 if (!prefixIs(line, "<features")) {
1353 lyxerr << "Wrong tabular format (expected <features ...> got"
1354 << line << ')' << endl;
1357 getTokenValue(line, "rotate", rotate);
1358 getTokenValue(line, "booktabs", use_booktabs);
1359 getTokenValue(line, "islongtable", is_long_tabular);
1360 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1361 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1362 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1363 getTokenValue(line, "headTopDL", endhead.topDL);
1364 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1365 getTokenValue(line, "footTopDL", endfoot.topDL);
1366 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1367 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1368 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1369 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1371 for (col_type j = 0; j < columnCount(); ++j) {
1373 if (!prefixIs(line,"<column")) {
1374 lyxerr << "Wrong tabular format (expected <column ...> got"
1375 << line << ')' << endl;
1378 getTokenValue(line, "alignment", column_info[j].alignment);
1379 getTokenValue(line, "valignment", column_info[j].valignment);
1380 getTokenValue(line, "width", column_info[j].p_width);
1381 getTokenValue(line, "special", column_info[j].align_special);
1384 for (row_type i = 0; i < rowCount(); ++i) {
1385 l_getline(is, line);
1386 if (!prefixIs(line, "<row")) {
1387 lyxerr << "Wrong tabular format (expected <row ...> got"
1388 << line << ')' << endl;
1391 getTokenValue(line, "topspace", row_info[i].top_space,
1392 row_info[i].top_space_default);
1393 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1394 row_info[i].bottom_space_default);
1395 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1396 row_info[i].interline_space_default);
1397 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1398 getTokenValue(line, "endhead", row_info[i].endhead);
1399 getTokenValue(line, "endfoot", row_info[i].endfoot);
1400 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1401 getTokenValue(line, "newpage", row_info[i].newpage);
1402 for (col_type j = 0; j < columnCount(); ++j) {
1403 l_getline(is, line);
1404 if (!prefixIs(line, "<cell")) {
1405 lyxerr << "Wrong tabular format (expected <cell ...> got"
1406 << line << ')' << endl;
1409 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1410 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1411 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1412 getTokenValue(line, "topline", cell_info[i][j].top_line);
1413 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1414 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1415 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1416 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1417 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1418 getTokenValue(line, "width", cell_info[i][j].p_width);
1419 getTokenValue(line, "special", cell_info[i][j].align_special);
1420 l_getline(is, line);
1421 if (prefixIs(line, "\\begin_inset")) {
1422 cell_info[i][j].inset->read(lex);
1423 l_getline(is, line);
1425 if (!prefixIs(line, "</cell>")) {
1426 lyxerr << "Wrong tabular format (expected </cell> got"
1427 << line << ')' << endl;
1431 l_getline(is, line);
1432 if (!prefixIs(line, "</row>")) {
1433 lyxerr << "Wrong tabular format (expected </row> got"
1434 << line << ')' << endl;
1438 while (!prefixIs(line, "</lyxtabular>")) {
1439 l_getline(is, line);
1445 bool Tabular::isMultiColumn(idx_type cell) const
1447 return cellInfo(cell).multicolumn != CELL_NORMAL;
1451 bool Tabular::isMultiColumnReal(idx_type cell) const
1453 return cellColumn(cell) != cellRightColumn(cell) &&
1454 cellInfo(cell).multicolumn != CELL_NORMAL;
1458 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1460 return cell_info[cellRow(cell)][cellColumn(cell)];
1464 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1466 CellData & cs = cellInfo(cell);
1467 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1468 cs.alignment = column_info[cellColumn(cell)].alignment;
1469 setRightLine(cell, rightLine(cell + number - 1));
1470 for (idx_type i = 1; i < number; ++i) {
1471 CellData & cs1 = cellInfo(cell + i);
1472 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1473 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1480 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1482 row_type const row = cellRow(cell);
1483 col_type const ncols = columnCount();
1484 idx_type result = 1;
1485 col_type column = cellColumn(cell) + 1;
1486 while (column < ncols && isPartOfMultiColumn(row, column)) {
1494 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1496 row_type const row = cellRow(cell);
1497 col_type column = cellColumn(cell);
1499 idx_type result = 0;
1501 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1502 cell_info[row][column].multicolumn = CELL_NORMAL;
1504 while (column < columnCount() &&
1505 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1507 cell_info[row][column].multicolumn = CELL_NORMAL;
1517 void Tabular::setBookTabs(bool what)
1519 use_booktabs = what;
1523 bool Tabular::useBookTabs() const
1525 return use_booktabs;
1529 void Tabular::setLongTabular(bool what)
1531 is_long_tabular = what;
1535 bool Tabular::isLongTabular() const
1537 return is_long_tabular;
1541 void Tabular::setRotateTabular(bool flag)
1547 bool Tabular::getRotateTabular() const
1553 void Tabular::setRotateCell(idx_type cell, bool flag)
1555 cellInfo(cell).rotate = flag;
1559 bool Tabular::getRotateCell(idx_type cell) const
1561 return cellInfo(cell).rotate;
1565 bool Tabular::needRotating() const
1569 for (row_type i = 0; i < rowCount(); ++i)
1570 for (col_type j = 0; j < columnCount(); ++j)
1571 if (cell_info[i][j].rotate)
1577 bool Tabular::isLastCell(idx_type cell) const
1579 if (cell + 1 < cellCount())
1585 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1587 if (cellRow(cell) > 0)
1588 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1593 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1595 if (cellRow(cell) + 1 < rowCount())
1596 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1601 Tabular::idx_type Tabular::cellIndex(row_type row,
1602 col_type column) const
1604 BOOST_ASSERT(column != npos && column < columnCount() &&
1605 row != npos && row < rowCount());
1606 return cell_info[row][column].cellno;
1610 void Tabular::setUsebox(idx_type cell, BoxType type)
1612 cellInfo(cell).usebox = type;
1616 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1618 if (column_info[cellColumn(cell)].p_width.zero() &&
1619 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1621 if (cellInfo(cell).usebox > 1)
1622 return cellInfo(cell).usebox;
1623 return useParbox(cell);
1628 // This are functions used for the longtable support
1630 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1636 row_info[row].endfirsthead = flag;
1640 row_info[row].endhead = flag;
1645 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1648 hd.set = haveLTHead();
1649 return row_info[row].endhead;
1653 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1656 hd.set = haveLTFirstHead();
1657 return row_info[row].endfirsthead;
1661 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1667 row_info[row].endlastfoot = flag;
1671 row_info[row].endfoot = flag;
1676 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1679 fd.set = haveLTFoot();
1680 return row_info[row].endfoot;
1684 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1687 fd.set = haveLTLastFoot();
1688 return row_info[row].endlastfoot;
1692 void Tabular::setLTNewPage(row_type row, bool what)
1694 row_info[row].newpage = what;
1698 bool Tabular::getLTNewPage(row_type row) const
1700 return row_info[row].newpage;
1704 bool Tabular::haveLTHead() const
1706 for (row_type i = 0; i < rowCount(); ++i)
1707 if (row_info[i].endhead)
1713 bool Tabular::haveLTFirstHead() const
1715 if (endfirsthead.empty)
1717 for (row_type i = 0; i < rowCount(); ++i)
1718 if (row_info[i].endfirsthead)
1724 bool Tabular::haveLTFoot() const
1726 for (row_type i = 0; i < rowCount(); ++i)
1727 if (row_info[i].endfoot)
1733 bool Tabular::haveLTLastFoot() const
1735 if (endlastfoot.empty)
1737 for (row_type i = 0; i < rowCount(); ++i)
1738 if (row_info[i].endlastfoot)
1744 // end longtable support functions
1746 void Tabular::setRowAscent(row_type row, int height)
1748 if (row >= rowCount() || row_info[row].ascent == height)
1750 row_info[row].ascent = height;
1754 void Tabular::setRowDescent(row_type row, int height)
1756 if (row >= rowCount() || row_info[row].descent == height)
1758 row_info[row].descent = height;
1762 int Tabular::rowAscent(row_type row) const
1764 if (row >= rowCount())
1766 return row_info[row].ascent;
1770 int Tabular::rowDescent(row_type row) const
1772 LASSERT(row < rowCount(), /**/);
1773 return row_info[row].descent;
1777 int Tabular::height() const
1780 for (row_type row = 0; row < rowCount(); ++row)
1781 height += rowAscent(row) + rowDescent(row) +
1782 getAdditionalHeight(row);
1787 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1789 LASSERT(row < rowCount(), /**/);
1790 LASSERT(column < columnCount(), /**/);
1791 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1795 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1797 // FIXME: assert or return 0 as in TeXBottomHLine()?
1798 LASSERT(row != npos, /**/);
1799 LASSERT(row < rowCount(), /**/);
1801 idx_type const fcell = getFirstCellInRow(row);
1802 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1805 for (idx_type i = fcell; i < n; ++i) {
1809 if (use_booktabs && row == 0) {
1812 } else if (tmp == n - fcell) {
1813 os << (use_booktabs ? "\\midrule " : "\\hline ");
1815 for (idx_type i = fcell; i < n; ++i) {
1817 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1818 << cellColumn(i) + 1
1820 << cellRightColumn(i) + 1
1832 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1834 // FIXME: return 0 or assert as in TeXTopHLine()?
1835 if (row == npos || row >= rowCount())
1838 idx_type const fcell = getFirstCellInRow(row);
1839 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1842 for (idx_type i = fcell; i < n; ++i) {
1846 if (use_booktabs && row == rowCount() - 1) {
1847 if (bottomLine(fcell))
1848 os << "\\bottomrule";
1849 } else if (tmp == n - fcell) {
1850 os << (use_booktabs ? "\\midrule" : "\\hline");
1852 for (idx_type i = fcell; i < n; ++i) {
1853 if (bottomLine(i)) {
1854 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1855 << cellColumn(i) + 1
1857 << cellRightColumn(i) + 1
1869 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1873 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1874 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1875 // figure out how to set the lines
1876 // we always set double lines to the right of the cell
1877 row_type const r = cellRow(cell);
1878 col_type const c = cellColumn(cell);
1879 col_type const nextcol = c + columnSpan(cell);
1880 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1881 bool forceleft = ismulticol && !prevcellright && leftLine(cell);
1882 bool nextcolleft = nextcol < columnCount() && columnLeftLine(nextcol);
1883 bool coldouble = columnRightLine(c) && nextcolleft;
1884 bool nextcellleft = nextcol < columnCount()
1885 && leftLine(cellIndex(r, nextcol));
1886 bool celldouble = rightLine(cell) && nextcellleft;
1887 bool doubleright = celldouble && (isMultiColumn(cell) || !coldouble);
1888 ismulticol = isMultiColumn(cell)
1889 || (leftLine(cell) && !columnLeftLine(c))
1890 || (rightLine(cell) && !columnRightLine(c))
1891 || (!celldouble && coldouble) || doubleright || forceleft;
1893 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1894 if (leftLine(cell) || forceleft)
1896 if (!cellInfo(cell).align_special.empty()) {
1897 os << cellInfo(cell).align_special;
1899 if (!getPWidth(cell).zero()) {
1901 case LYX_VALIGN_TOP:
1904 case LYX_VALIGN_MIDDLE:
1907 case LYX_VALIGN_BOTTOM:
1912 << from_ascii(getPWidth(cell).asLatexString())
1916 case LYX_ALIGN_LEFT:
1919 case LYX_ALIGN_RIGHT:
1926 } // end if else !getPWidth
1927 } // end if else !cellinfo_of_cell
1928 if (rightLine(cell))
1931 // add extra vertical line if we want a double one
1935 if (getRotateCell(cell)) {
1936 os << "\\begin{sideways}\n";
1939 if (getUsebox(cell) == BOX_PARBOX) {
1942 case LYX_VALIGN_TOP:
1945 case LYX_VALIGN_MIDDLE:
1948 case LYX_VALIGN_BOTTOM:
1952 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1954 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1955 os << "\\begin{minipage}[";
1957 case LYX_VALIGN_TOP:
1960 case LYX_VALIGN_MIDDLE:
1963 case LYX_VALIGN_BOTTOM:
1967 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1975 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1980 if (getUsebox(cell) == BOX_PARBOX)
1982 else if (getUsebox(cell) == BOX_MINIPAGE) {
1983 os << "%\n\\end{minipage}";
1986 if (getRotateCell(cell)) {
1987 os << "%\n\\end{sideways}";
1997 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1998 OutputParams const & runparams) const
2000 if (!is_long_tabular)
2004 // output header info
2006 if (endhead.topDL) {
2010 for (row_type i = 0; i < rowCount(); ++i) {
2011 if (row_info[i].endhead) {
2012 ret += TeXRow(os, i, runparams);
2015 if (endhead.bottomDL) {
2019 os << "\\endhead\n";
2021 if (endfirsthead.empty) {
2022 os << "\\endfirsthead\n";
2026 // output firstheader info
2027 if (haveLTFirstHead()) {
2028 if (endfirsthead.topDL) {
2032 for (row_type i = 0; i < rowCount(); ++i) {
2033 if (row_info[i].endfirsthead) {
2034 ret += TeXRow(os, i, runparams);
2037 if (endfirsthead.bottomDL) {
2041 os << "\\endfirsthead\n";
2044 // output footer info
2046 if (endfoot.topDL) {
2050 for (row_type i = 0; i < rowCount(); ++i) {
2051 if (row_info[i].endfoot) {
2052 ret += TeXRow(os, i, runparams);
2055 if (endfoot.bottomDL) {
2059 os << "\\endfoot\n";
2061 if (endlastfoot.empty) {
2062 os << "\\endlastfoot\n";
2066 // output lastfooter info
2067 if (haveLTLastFoot()) {
2068 if (endlastfoot.topDL) {
2072 for (row_type i = 0; i < rowCount(); ++i) {
2073 if (row_info[i].endlastfoot) {
2074 ret += TeXRow(os, i, runparams);
2077 if (endlastfoot.bottomDL) {
2081 os << "\\endlastfoot\n";
2088 bool Tabular::isValidRow(row_type row) const
2090 if (!is_long_tabular)
2092 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2093 !row_info[row].endfoot && !row_info[row].endlastfoot;
2097 int Tabular::TeXRow(odocstream & os, row_type i,
2098 OutputParams const & runparams) const
2100 idx_type cell = cellIndex(i, 0);
2101 int ret = TeXTopHLine(os, i);
2102 if (row_info[i].top_space_default) {
2104 os << "\\addlinespace\n";
2106 os << "\\noalign{\\vskip\\doublerulesep}\n";
2108 } else if(!row_info[i].top_space.zero()) {
2110 os << "\\addlinespace["
2111 << from_ascii(row_info[i].top_space.asLatexString())
2114 os << "\\noalign{\\vskip"
2115 << from_ascii(row_info[i].top_space.asLatexString())
2120 bool ismulticol = false;
2121 for (col_type j = 0; j < columnCount(); ++j) {
2122 if (isPartOfMultiColumn(i, j))
2124 ret += TeXCellPreamble(os, cell, ismulticol);
2125 shared_ptr<InsetTableCell> inset = cellInset(cell);
2127 Paragraph const & par = inset->paragraphs().front();
2128 bool rtl = par.isRTL(buffer().params())
2130 && getPWidth(cell).zero();
2133 if (par.getParLanguage(buffer().params())->lang() ==
2136 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2138 // currently, remaning RTL languages are arabic_arabtex and hebrew
2142 ret += inset->latex(os, runparams);
2146 ret += TeXCellPostamble(os, cell, ismulticol);
2147 if (!isLastCellInRow(cell)) { // not last cell in row
2152 os << "\\tabularnewline";
2153 if (row_info[i].bottom_space_default) {
2155 os << "\\addlinespace";
2157 os << "[\\doublerulesep]";
2158 } else if (!row_info[i].bottom_space.zero()) {
2160 os << "\\addlinespace";
2162 << from_ascii(row_info[i].bottom_space.asLatexString())
2167 ret += TeXBottomHLine(os, i);
2168 if (row_info[i].interline_space_default) {
2170 os << "\\addlinespace\n";
2172 os << "\\noalign{\\vskip\\doublerulesep}\n";
2174 } else if (!row_info[i].interline_space.zero()) {
2176 os << "\\addlinespace["
2177 << from_ascii(row_info[i].interline_space.asLatexString())
2180 os << "\\noalign{\\vskip"
2181 << from_ascii(row_info[i].interline_space.asLatexString())
2189 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2193 //+---------------------------------------------------------------------
2194 //+ first the opening preamble +
2195 //+---------------------------------------------------------------------
2198 os << "\\begin{sideways}\n";
2201 if (is_long_tabular)
2202 os << "\\begin{longtable}{";
2204 os << "\\begin{tabular}{";
2206 for (col_type i = 0; i < columnCount(); ++i) {
2207 if (!use_booktabs && columnLeftLine(i))
2209 if (!column_info[i].align_special.empty()) {
2210 os << column_info[i].align_special;
2212 if (!column_info[i].p_width.zero()) {
2213 switch (column_info[i].alignment) {
2214 case LYX_ALIGN_LEFT:
2215 os << ">{\\raggedright}";
2217 case LYX_ALIGN_RIGHT:
2218 os << ">{\\raggedleft}";
2220 case LYX_ALIGN_CENTER:
2221 os << ">{\\centering}";
2223 case LYX_ALIGN_NONE:
2224 case LYX_ALIGN_BLOCK:
2225 case LYX_ALIGN_LAYOUT:
2226 case LYX_ALIGN_SPECIAL:
2230 switch (column_info[i].valignment) {
2231 case LYX_VALIGN_TOP:
2234 case LYX_VALIGN_MIDDLE:
2237 case LYX_VALIGN_BOTTOM:
2242 << from_ascii(column_info[i].p_width.asLatexString())
2245 switch (column_info[i].alignment) {
2246 case LYX_ALIGN_LEFT:
2249 case LYX_ALIGN_RIGHT:
2256 } // end if else !column_info[i].p_width
2257 } // end if else !column_info[i].align_special
2258 if (!use_booktabs && columnRightLine(i))
2264 ret += TeXLongtableHeaderFooter(os, runparams);
2266 //+---------------------------------------------------------------------
2267 //+ the single row and columns (cells) +
2268 //+---------------------------------------------------------------------
2270 for (row_type i = 0; i < rowCount(); ++i) {
2271 if (isValidRow(i)) {
2272 ret += TeXRow(os, i, runparams);
2273 if (is_long_tabular && row_info[i].newpage) {
2274 os << "\\newpage\n";
2280 //+---------------------------------------------------------------------
2281 //+ the closing of the tabular +
2282 //+---------------------------------------------------------------------
2284 if (is_long_tabular)
2285 os << "\\end{longtable}";
2287 os << "\\end{tabular}";
2289 os << "\n\\end{sideways}";
2297 int Tabular::docbookRow(odocstream & os, row_type row,
2298 OutputParams const & runparams) const
2301 idx_type cell = getFirstCellInRow(row);
2304 for (col_type j = 0; j < columnCount(); ++j) {
2305 if (isPartOfMultiColumn(row, j))
2308 os << "<entry align=\"";
2309 switch (getAlignment(cell)) {
2310 case LYX_ALIGN_LEFT:
2313 case LYX_ALIGN_RIGHT:
2321 os << "\" valign=\"";
2322 switch (getVAlignment(cell)) {
2323 case LYX_VALIGN_TOP:
2326 case LYX_VALIGN_BOTTOM:
2329 case LYX_VALIGN_MIDDLE:
2334 if (isMultiColumn(cell)) {
2335 os << " namest=\"col" << j << "\" ";
2336 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2340 ret += cellInset(cell)->docbook(os, runparams);
2349 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2353 //+---------------------------------------------------------------------
2354 //+ first the opening preamble +
2355 //+---------------------------------------------------------------------
2357 os << "<tgroup cols=\"" << columnCount()
2358 << "\" colsep=\"1\" rowsep=\"1\">\n";
2360 for (col_type i = 0; i < columnCount(); ++i) {
2361 os << "<colspec colname=\"col" << i << "\" align=\"";
2362 switch (column_info[i].alignment) {
2363 case LYX_ALIGN_LEFT:
2366 case LYX_ALIGN_RIGHT:
2374 if (runparams.flavor == OutputParams::XML)
2380 //+---------------------------------------------------------------------
2381 //+ Long Tabular case +
2382 //+---------------------------------------------------------------------
2384 // output header info
2385 if (haveLTHead() || haveLTFirstHead()) {
2388 for (row_type i = 0; i < rowCount(); ++i) {
2389 if (row_info[i].endhead || row_info[i].endfirsthead) {
2390 ret += docbookRow(os, i, runparams);
2396 // output footer info
2397 if (haveLTFoot() || haveLTLastFoot()) {
2400 for (row_type i = 0; i < rowCount(); ++i) {
2401 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2402 ret += docbookRow(os, i, runparams);
2409 //+---------------------------------------------------------------------
2410 //+ the single row and columns (cells) +
2411 //+---------------------------------------------------------------------
2415 for (row_type i = 0; i < rowCount(); ++i) {
2416 if (isValidRow(i)) {
2417 ret += docbookRow(os, i, runparams);
2422 //+---------------------------------------------------------------------
2423 //+ the closing of the tabular +
2424 //+---------------------------------------------------------------------
2433 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2434 vector<unsigned int> const & clen) const
2436 idx_type const fcell = getFirstCellInRow(row);
2437 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2440 for (idx_type i = fcell; i < n; ++i) {
2450 for (idx_type i = fcell; i < n; ++i) {
2461 col_type column = cellColumn(i);
2462 int len = clen[column];
2463 while (column < columnCount() - 1
2464 && isPartOfMultiColumn(row, ++column))
2465 len += clen[column] + 4;
2466 os << docstring(len, ch);
2481 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2482 vector<unsigned int> const & clen) const
2484 idx_type const fcell = getFirstCellInRow(row);
2485 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2488 for (idx_type i = fcell; i < n; ++i) {
2489 if (bottomLine(i)) {
2498 for (idx_type i = fcell; i < n; ++i) {
2499 if (bottomLine(i)) {
2509 col_type column = cellColumn(i);
2510 int len = clen[column];
2511 while (column < columnCount() -1
2512 && isPartOfMultiColumn(row, ++column))
2513 len += clen[column] + 4;
2514 os << docstring(len, ch);
2515 if (bottomLine(i)) {
2529 void Tabular::plaintextPrintCell(odocstream & os,
2530 OutputParams const & runparams,
2531 idx_type cell, row_type row, col_type column,
2532 vector<unsigned int> const & clen,
2533 bool onlydata) const
2535 odocstringstream sstr;
2536 cellInset(cell)->plaintext(sstr, runparams);
2548 unsigned int len1 = sstr.str().length();
2549 unsigned int len2 = clen[column];
2550 while (column < columnCount() -1
2551 && isPartOfMultiColumn(row, ++column))
2552 len2 += clen[column] + 4;
2555 switch (getAlignment(cell)) {
2557 case LYX_ALIGN_LEFT:
2560 case LYX_ALIGN_RIGHT:
2564 case LYX_ALIGN_CENTER:
2570 os << docstring(len1, ' ') << sstr.str()
2571 << docstring(len2, ' ');
2573 if (rightLine(cell))
2580 void Tabular::plaintext(odocstream & os,
2581 OutputParams const & runparams, int const depth,
2582 bool onlydata, char_type delim) const
2584 // first calculate the width of the single columns
2585 vector<unsigned int> clen(columnCount());
2588 // first all non (real) multicolumn cells!
2589 for (col_type j = 0; j < columnCount(); ++j) {
2591 for (row_type i = 0; i < rowCount(); ++i) {
2592 idx_type cell = cellIndex(i, j);
2593 if (isMultiColumnReal(cell))
2595 odocstringstream sstr;
2596 cellInset(cell)->plaintext(sstr, runparams);
2597 if (clen[j] < sstr.str().length())
2598 clen[j] = sstr.str().length();
2601 // then all (real) multicolumn cells!
2602 for (col_type j = 0; j < columnCount(); ++j) {
2603 for (row_type i = 0; i < rowCount(); ++i) {
2604 idx_type cell = cellIndex(i, j);
2605 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2607 odocstringstream sstr;
2608 cellInset(cell)->plaintext(sstr, runparams);
2609 int len = int(sstr.str().length());
2610 idx_type const n = columnSpan(cell);
2611 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2613 if (len > int(clen[j + n - 1]))
2614 clen[j + n - 1] = len;
2619 for (row_type i = 0; i < rowCount(); ++i) {
2620 if (!onlydata && plaintextTopHLine(os, i, clen))
2621 os << docstring(depth * 2, ' ');
2622 for (col_type j = 0; j < columnCount(); ++j) {
2623 if (isPartOfMultiColumn(i, j))
2625 if (onlydata && j > 0)
2626 // we don't use operator<< for single UCS4 character.
2627 // see explanation in docstream.h
2629 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2634 os << docstring(depth * 2, ' ');
2635 if (plaintextBottomHLine(os, i, clen))
2636 os << docstring(depth * 2, ' ');
2642 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2644 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2648 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2649 col_type column) const
2651 return cell_info[row][column].inset;
2655 void Tabular::setCellInset(row_type row, col_type column,
2656 shared_ptr<InsetTableCell> ins) const
2658 CellData & cd = cell_info[row][column];
2660 // reset the InsetTableCell's pointers
2661 ins->setCellData(&cd);
2662 ins->setTabular(this);
2667 Tabular::cellFromInset(Inset const * inset) const
2669 // is this inset part of the tabular?
2671 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2672 LASSERT(false, /**/);
2675 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2676 if (cellInset(cell).get() == inset) {
2677 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2678 << "cell=" << cell);
2682 // We should have found a cell at this point
2683 lyxerr << "Tabular::cellFromInset: Cell of inset "
2684 << inset << " not found!" << endl;
2685 LASSERT(false, /**/);
2691 void Tabular::validate(LaTeXFeatures & features) const
2693 features.require("NeedTabularnewline");
2695 features.require("booktabs");
2696 if (isLongTabular())
2697 features.require("longtable");
2699 features.require("rotating");
2700 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2701 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2702 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2703 features.require("array");
2704 cellInset(cell)->validate(features);
2709 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2711 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2712 ParagraphList::const_iterator cit = parlist.begin();
2713 ParagraphList::const_iterator end = parlist.end();
2715 for (; cit != end; ++cit)
2716 for (int i = 0; i < cit->size(); ++i)
2717 if (cit->isNewline(i))
2724 /////////////////////////////////////////////////////////////////////
2728 /////////////////////////////////////////////////////////////////////
2730 InsetTableCell::InsetTableCell(Buffer const & buf,
2731 Tabular::CellData const * cell, Tabular const * table)
2732 : InsetText(buf), cell_data_(cell), table_(table)
2736 bool InsetTableCell::forceEmptyLayout(idx_type) const
2738 LASSERT(table_, /**/);
2739 LASSERT(cell_data_, /**/);
2740 return table_->getPWidth(cell_data_->cellno).zero();
2743 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2745 LASSERT(table_, /**/);
2746 LASSERT(cell_data_, /**/);
2747 return !table_->getPWidth(cell_data_->cellno).zero();
2750 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2751 FuncStatus & status) const
2754 switch (cmd.action) {
2756 enabled = !forceEmptyLayout();
2758 case LFUN_LAYOUT_PARAGRAPH:
2759 enabled = allowParagraphCustomization();
2762 return InsetText::getStatus(cur, cmd, status);
2764 status.enabled(enabled);
2768 /////////////////////////////////////////////////////////////////////
2772 /////////////////////////////////////////////////////////////////////
2774 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2776 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2777 rowselect_(false), colselect_(false)
2779 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2783 InsetTabular::InsetTabular(InsetTabular const & tab)
2784 : Inset(tab), tabular(tab.tabular), scx_(0)
2786 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2790 InsetTabular::~InsetTabular()
2792 hideDialogs("tabular", this);
2796 bool InsetTabular::insetAllowed(InsetCode code) const
2798 if (code == MATHMACRO_CODE)
2805 void InsetTabular::write(ostream & os) const
2807 os << "Tabular" << endl;
2812 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2814 // FIXME: depending on the selection state, we could offer a different menu.
2815 return from_ascii("context-tabular");
2819 void InsetTabular::read(Lexer & lex)
2821 //bool const old_format = (lex.getString() == "\\LyXTable");
2829 string token = lex.getString();
2830 while (lex && token != "\\end_inset") {
2832 token = lex.getString();
2835 lex.printError("Missing \\end_inset at this point. ");
2839 int InsetTabular::rowFromY(Cursor & cur, int y) const
2841 // top y coordinate of tabular
2842 int h = yo(cur.bv()) - tabular.rowAscent(0);
2843 size_t nrows = tabular.rowCount();
2845 for (; r < nrows && y > h; ++r) {
2846 h += tabular.rowAscent(r);
2847 h += tabular.rowDescent(r);
2848 h += tabular.getAdditionalHeight(r);
2854 int InsetTabular::columnFromX(Cursor & cur, int x) const
2856 // left x coordinate of tabular
2857 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2858 size_t ncols = tabular.columnCount();
2860 for (; c < ncols && x > w; ++c)
2861 w += tabular.columnWidth(c);
2866 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2868 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2869 // mi.base.textwidth << "\n";
2872 LASSERT(false, /**/);
2876 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2879 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2880 if (tabular.isPartOfMultiColumn(i, j))
2881 // Multicolumn cell, but not first one
2886 if (tabular.cell_info[i][j].multicolumn ==
2887 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2888 p_width = tabular.cellInfo(cell).p_width;
2890 p_width = tabular.column_info[j].p_width;
2891 if (!p_width.zero())
2892 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2893 tabular.cellInset(cell)->metrics(m, dim);
2894 if (!p_width.zero())
2895 dim.wid = m.base.textwidth;
2896 tabular.setCellWidth(cell, dim.wid);
2897 maxAsc = max(maxAsc, dim.asc);
2898 maxDesc = max(maxDesc, dim.des);
2901 int const top_space = tabular.row_info[i].top_space_default ?
2902 default_line_space :
2903 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2904 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2905 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2906 default_line_space :
2907 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2908 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2910 tabular.updateColumnWidths();
2911 dim.asc = tabular.rowAscent(0);
2912 dim.des = tabular.height() - dim.asc;
2913 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2917 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2919 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2920 BufferView * bv = pi.base.bv;
2922 // FIXME: As the full backrgound is painted in drawSelection(),
2923 // we have no choice but to do a full repaint for the Text cells.
2924 pi.full_repaint = true;
2926 resetPos(bv->cursor());
2929 x += ADD_TO_TABULAR_WIDTH;
2931 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2934 first_visible_cell = Tabular::npos;
2935 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2937 int const a = tabular.rowAscent(i);
2938 int const d = tabular.rowDescent(i);
2939 idx = tabular.cellIndex(i, 0);
2940 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2941 if (tabular.isPartOfMultiColumn(i, j))
2943 if (first_visible_cell == Tabular::npos)
2944 first_visible_cell = idx;
2946 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2947 // Cache the Inset position.
2948 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2949 if (nx + tabular.columnWidth(idx) < 0
2950 || nx > bv->workWidth()
2952 || y - a > bv->workHeight()) {
2953 pi.pain.setDrawingEnabled(false);
2954 cell(idx)->draw(pi, cx, y);
2955 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2956 pi.pain.setDrawingEnabled(original_drawing_state);
2958 cell(idx)->draw(pi, cx, y);
2959 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2961 nx += tabular.columnWidth(idx);
2965 if (i + 1 < tabular.rowCount())
2966 y += d + tabular.rowAscent(i + 1) +
2967 tabular.getAdditionalHeight(i + 1);
2972 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2974 Cursor & cur = pi.base.bv->cursor();
2976 x += scx_ + ADD_TO_TABULAR_WIDTH;
2978 // FIXME: it is wrong to completely paint the background
2979 // if we want to do single row painting.
2981 // Paint background of current tabular
2982 int const w = tabular.width();
2983 int const h = tabular.height();
2984 int yy = y - tabular.rowAscent(0);
2985 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2987 if (!cur.selection())
2989 if (&cur.inset() != this)
2995 if (tablemode(cur)) {
2998 getSelection(cur, rs, re, cs, ce);
2999 y -= tabular.rowAscent(0);
3000 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3001 int const a = tabular.rowAscent(j);
3002 int const h = a + tabular.rowDescent(j);
3004 y += tabular.getAdditionalHeight(j);
3005 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3006 if (tabular.isPartOfMultiColumn(j, i))
3008 idx_type const cell =
3009 tabular.cellIndex(j, i);
3010 int const w = tabular.columnWidth(cell);
3011 if (i >= cs && i <= ce && j >= rs && j <= re)
3012 pi.pain.fillRectangle(xx, y, w, h,
3020 x += cellXPos(cur.idx());
3021 x += tabular.getBeginningOfTextInCell(cur.idx());
3022 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3027 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3028 row_type row, idx_type cell, bool erased) const
3030 int x2 = x + tabular.columnWidth(cell);
3031 bool on_off = false;
3032 ColorCode col = Color_tabularline;
3033 ColorCode onoffcol = Color_tabularonoffline;
3036 col = Color_deletedtext;
3037 onoffcol = Color_deletedtext;
3040 if (!tabular.topAlreadyDrawn(cell)) {
3041 on_off = !tabular.topLine(cell);
3042 pain.line(x, y - tabular.rowAscent(row),
3043 x2, y - tabular.rowAscent(row),
3044 on_off ? onoffcol : col,
3045 on_off ? Painter::line_onoffdash : Painter::line_solid);
3047 on_off = !tabular.bottomLine(cell);
3048 pain.line(x, y + tabular.rowDescent(row),
3049 x2, y + tabular.rowDescent(row),
3050 on_off ? onoffcol : col,
3051 on_off ? Painter::line_onoffdash : Painter::line_solid);
3052 if (!tabular.leftAlreadyDrawn(cell)) {
3053 on_off = !tabular.leftLine(cell);
3054 pain.line(x, y - tabular.rowAscent(row),
3055 x, y + tabular.rowDescent(row),
3056 on_off ? onoffcol : col,
3057 on_off ? Painter::line_onoffdash : Painter::line_solid);
3059 on_off = !tabular.rightLine(cell);
3060 pain.line(x2 - tabular.getAdditionalWidth(cell),
3061 y - tabular.rowAscent(row),
3062 x2 - tabular.getAdditionalWidth(cell),
3063 y + tabular.rowDescent(row),
3064 on_off ? onoffcol : col,
3065 on_off ? Painter::line_onoffdash : Painter::line_solid);
3069 docstring InsetTabular::editMessage() const
3071 return _("Opened table");
3075 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3077 //lyxerr << "InsetTabular::edit: " << this << endl;
3079 cur.selection() = false;
3082 if (isRightToLeft(cur))
3083 cur.idx() = tabular.getLastCellInRow(0);
3089 if (isRightToLeft(cur))
3090 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3092 cur.idx() = tabular.cellCount() - 1;
3094 cur.pos() = cur.lastpos(); // FIXME crude guess
3096 // FIXME: this accesses the position cache before it is initialized
3098 //cur.bv().fitCursor();
3102 void InsetTabular::updateLabels(ParIterator const & it)
3104 // In a longtable, tell captions what the current float is
3105 Counters & cnts = buffer().params().documentClass().counters();
3106 string const saveflt = cnts.current_float();
3107 if (tabular.isLongTabular())
3108 cnts.current_float("table");
3110 ParIterator it2 = it;
3112 size_t const end = it2.nargs();
3113 for ( ; it2.idx() < end; it2.top().forwardIdx())
3114 lyx::updateLabels(buffer(), it2);
3117 if (tabular.isLongTabular())
3118 cnts.current_float(saveflt);
3122 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3124 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3125 << "\n cur:" << cur);
3126 CursorSlice sl = cur.top();
3127 Cursor & bvcur = cur.bv().cursor();
3129 switch (cmd.action) {
3131 case LFUN_MOUSE_PRESS: {
3132 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3134 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3135 || cmd.x > xo(cur.bv()) + tabular.width()) {
3136 row_type r = rowFromY(cur, cmd.y);
3137 cur.idx() = tabular.getFirstCellInRow(r);
3140 cur.idx() = tabular.getLastCellInRow(r);
3141 cur.pos() = cur.lastpos();
3142 cur.selection() = true;
3148 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3149 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3150 || cmd.y > y0 + tabular.height()) {
3151 col_type c = columnFromX(cur, cmd.x);
3152 cur.idx() = tabular.cellIndex(0, c);
3155 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3156 cur.pos() = cur.lastpos();
3157 cur.selection() = true;
3162 // do not reset cursor/selection if we have selected
3163 // some cells (bug 2715).
3164 if (cmd.button() == mouse_button::button3
3165 && &bvcur.selBegin().inset() == this
3166 && tablemode(bvcur))
3169 // Let InsetTableCell do it
3170 cell(cur.idx())->dispatch(cur, cmd);
3173 case LFUN_MOUSE_MOTION:
3174 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3175 if (cmd.button() == mouse_button::button1) {
3176 // only accept motions to places not deeper nested than the real anchor
3177 if (!bvcur.anchor_.hasPart(cur)) {
3181 // select (additional) row
3183 row_type r = rowFromY(cur, cmd.y);
3184 cur.idx() = tabular.getLastCellInRow(r);
3185 bvcur.setCursor(cur);
3186 bvcur.selection() = true;
3189 // select (additional) column
3191 col_type c = columnFromX(cur, cmd.x);
3192 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3193 bvcur.setCursor(cur);
3194 bvcur.selection() = true;
3197 // only update if selection changes
3198 if (bvcur.idx() == cur.idx() &&
3199 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3201 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3202 bvcur.setCursor(cur);
3203 bvcur.selection() = true;
3207 case LFUN_MOUSE_RELEASE:
3212 case LFUN_CELL_BACKWARD:
3214 cur.selection() = false;
3217 case LFUN_CELL_FORWARD:
3219 cur.selection() = false;
3221 case LFUN_CHAR_FORWARD_SELECT:
3222 case LFUN_CHAR_FORWARD:
3223 case LFUN_CHAR_BACKWARD_SELECT:
3224 case LFUN_CHAR_BACKWARD:
3225 case LFUN_CHAR_RIGHT_SELECT:
3226 case LFUN_CHAR_RIGHT:
3227 case LFUN_CHAR_LEFT_SELECT:
3228 case LFUN_CHAR_LEFT: {
3230 // determine whether we move to next or previous cell, where to enter
3231 // the new cell from, and which command to "finish" (i.e., exit the
3234 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3235 FuncCode finish_lfun;
3237 if (cmd.action == LFUN_CHAR_FORWARD
3238 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3240 finish_lfun = LFUN_FINISHED_FORWARD;
3242 else if (cmd.action == LFUN_CHAR_BACKWARD
3243 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3245 finish_lfun = LFUN_FINISHED_BACKWARD;
3247 // LEFT or RIGHT commands --- the interpretation will depend on the
3248 // table's direction.
3250 bool right = (cmd.action == LFUN_CHAR_RIGHT
3251 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3252 next_cell = (isRightToLeft(cur) != right);
3254 if (lyxrc.visual_cursor) {
3255 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3259 finish_lfun = LFUN_FINISHED_RIGHT;
3261 finish_lfun = LFUN_FINISHED_LEFT;
3265 // finally, now that we know what we want to do, do it!
3266 cell(cur.idx())->dispatch(cur, cmd);
3267 if (!cur.result().dispatched()) {
3268 // move to next/prev cell, as appropriate
3269 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3270 << " cell from: " << int(entry_from));
3272 moveNextCell(cur, entry_from);
3274 movePrevCell(cur, entry_from);
3275 // if we're exiting the table, call the appropriate FINISHED lfun
3276 if (sl == cur.top())
3277 cmd = FuncRequest(finish_lfun);
3285 case LFUN_DOWN_SELECT:
3287 cell(cur.idx())->dispatch(cur, cmd);
3288 cur.dispatched(); // override the cell's decision
3289 if (sl == cur.top())
3290 // if our Text didn't do anything to the cursor
3291 // then we try to put the cursor into the cell below
3292 // setting also the right targetX.
3293 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3294 cur.idx() = tabular.cellBelow(cur.idx());
3296 TextMetrics const & tm =
3297 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3298 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3300 if (sl == cur.top()) {
3301 // we trick it to go to forward after leaving the
3303 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3308 case LFUN_UP_SELECT:
3310 cell(cur.idx())->dispatch(cur, cmd);
3311 cur.dispatched(); // override the cell's decision
3312 if (sl == cur.top())
3313 // if our Text didn't do anything to the cursor
3314 // then we try to put the cursor into the cell above
3315 // setting also the right targetX.
3316 if (tabular.cellRow(cur.idx()) != 0) {
3317 cur.idx() = tabular.cellAbove(cur.idx());
3318 cur.pit() = cur.lastpit();
3319 Text const * text = cell(cur.idx())->getText(0);
3320 TextMetrics const & tm = cur.bv().textMetrics(text);
3321 ParagraphMetrics const & pm =
3322 tm.parMetrics(cur.lastpit());
3323 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3325 if (sl == cur.top()) {
3326 cmd = FuncRequest(LFUN_UP);
3331 // case LFUN_SCREEN_DOWN: {
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();
3336 // if (t < yo() + tabular.getHeightOfTabular()) {
3337 // cur.bv().scrollDocView(t);
3338 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3340 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3347 // case LFUN_SCREEN_UP: {
3348 // //if (hasSelection())
3349 // // cur.selection() = false;
3350 // col_type const col = tabular.cellColumn(cur.idx());
3351 // int const t = cur.bv().top_y() + cur.bv().height();
3353 // cur.bv().scrollDocView(t);
3357 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3361 // cur.par() = cur.lastpar();
3362 // cur.pos() = cur.lastpos();
3366 case LFUN_LAYOUT_TABULAR:
3367 cur.bv().showDialog("tabular", params2string(*this), this);
3370 case LFUN_INSET_DIALOG_UPDATE:
3371 cur.bv().updateDialog("tabular", params2string(*this));
3374 case LFUN_TABULAR_FEATURE:
3375 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3379 // insert file functions
3380 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3381 case LFUN_FILE_INSERT_PLAINTEXT: {
3383 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3384 FileName(to_utf8(cmd.argument())));
3387 cur.recordUndoInset(INSERT_UNDO);
3388 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3389 // content has been replaced,
3390 // so cursor might be invalid
3391 cur.pos() = cur.lastpos();
3392 cur.pit() = cur.lastpit();
3393 bvcur.setCursor(cur);
3400 if (tablemode(cur)) {
3401 if (copySelection(cur)) {
3402 cur.recordUndoInset(DELETE_UNDO);
3407 cell(cur.idx())->dispatch(cur, cmd);
3410 case LFUN_CHAR_DELETE_BACKWARD:
3411 case LFUN_CHAR_DELETE_FORWARD:
3412 if (tablemode(cur)) {
3413 cur.recordUndoInset(DELETE_UNDO);
3417 cell(cur.idx())->dispatch(cur, cmd);
3421 if (!cur.selection())
3423 if (tablemode(cur)) {
3427 cell(cur.idx())->dispatch(cur, cmd);
3430 case LFUN_CLIPBOARD_PASTE:
3431 case LFUN_PRIMARY_SELECTION_PASTE: {
3432 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3433 theClipboard().getAsText() :
3434 theSelection().get();
3437 // pass to InsertPlaintextString, but
3438 // only if we have multi-cell content
3439 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3440 cur.recordUndoInset(INSERT_UNDO);
3441 if (insertPlaintextString(cur.bv(), clip, false)) {
3442 // content has been replaced,
3443 // so cursor might be invalid
3444 cur.pos() = cur.lastpos();
3445 cur.pit() = cur.lastpit();
3446 bvcur.setCursor(cur);
3450 // Let the cell handle normal text
3451 cell(cur.idx())->dispatch(cur, cmd);
3456 if (tabularStackDirty() && theClipboard().isInternal() ||
3457 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3458 cur.recordUndoInset(INSERT_UNDO);
3459 pasteClipboard(cur);
3462 cell(cur.idx())->dispatch(cur, cmd);
3465 case LFUN_FONT_EMPH:
3466 case LFUN_FONT_BOLD:
3467 case LFUN_FONT_ROMAN:
3468 case LFUN_FONT_NOUN:
3469 case LFUN_FONT_ITAL:
3470 case LFUN_FONT_FRAK:
3471 case LFUN_FONT_TYPEWRITER:
3472 case LFUN_FONT_SANS:
3473 case LFUN_FONT_FREE_APPLY:
3474 case LFUN_FONT_FREE_UPDATE:
3475 case LFUN_FONT_SIZE:
3476 case LFUN_FONT_UNDERLINE:
3478 case LFUN_WORD_CAPITALIZE:
3479 case LFUN_WORD_UPCASE:
3480 case LFUN_WORD_LOWCASE:
3481 case LFUN_CHARS_TRANSPOSE:
3482 if (tablemode(cur)) {
3485 getSelection(cur, rs, re, cs, ce);
3486 Cursor tmpcur = cur;
3487 for (row_type i = rs; i <= re; ++i) {
3488 for (col_type j = cs; j <= ce; ++j) {
3489 // cursor follows cell:
3490 tmpcur.idx() = tabular.cellIndex(i, j);
3491 // select this cell only:
3494 tmpcur.resetAnchor();
3495 tmpcur.pit() = tmpcur.lastpit();
3496 tmpcur.pos() = tmpcur.top().lastpos();
3497 tmpcur.setCursor(tmpcur);
3498 tmpcur.setSelection();
3499 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3504 cell(cur.idx())->dispatch(cur, cmd);
3508 // we try to handle this event in the insets dispatch function.
3509 cell(cur.idx())->dispatch(cur, cmd);
3515 // function sets an object as defined in func_status.h:
3516 // states OK, Unknown, Disabled, On, Off.
3517 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3518 FuncStatus & status) const
3520 switch (cmd.action) {
3521 case LFUN_TABULAR_FEATURE: {
3522 int action = Tabular::LAST_ACTION;
3524 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3525 string const tmp = tabularFeature[i].feature;
3526 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3527 action = tabularFeature[i].action;
3531 if (action == Tabular::LAST_ACTION) {
3533 status.unknown(true);
3537 string const argument
3538 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3540 row_type sel_row_start = 0;
3541 row_type sel_row_end = 0;
3542 col_type sel_col_start = 0;
3543 col_type sel_col_end = 0;
3544 Tabular::ltType dummyltt;
3547 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3550 case Tabular::SET_PWIDTH:
3551 case Tabular::SET_MPWIDTH:
3552 case Tabular::SET_SPECIAL_COLUMN:
3553 case Tabular::SET_SPECIAL_MULTI:
3554 case Tabular::APPEND_ROW:
3555 case Tabular::APPEND_COLUMN:
3556 case Tabular::DELETE_ROW:
3557 case Tabular::DELETE_COLUMN:
3558 case Tabular::COPY_ROW:
3559 case Tabular::COPY_COLUMN:
3560 case Tabular::SET_ALL_LINES:
3561 case Tabular::UNSET_ALL_LINES:
3562 case Tabular::SET_TOP_SPACE:
3563 case Tabular::SET_BOTTOM_SPACE:
3564 case Tabular::SET_INTERLINE_SPACE:
3565 case Tabular::SET_BORDER_LINES:
3569 case Tabular::MULTICOLUMN:
3570 status.enabled(sel_row_start == sel_row_end);
3571 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3574 case Tabular::TOGGLE_LINE_TOP:
3575 status.setOnOff(tabular.topLine(cur.idx()));
3578 case Tabular::TOGGLE_LINE_BOTTOM:
3579 status.setOnOff(tabular.bottomLine(cur.idx()));
3582 case Tabular::TOGGLE_LINE_LEFT:
3583 status.setOnOff(tabular.leftLine(cur.idx()));
3586 case Tabular::TOGGLE_LINE_RIGHT:
3587 status.setOnOff(tabular.rightLine(cur.idx()));
3590 case Tabular::M_ALIGN_LEFT:
3592 case Tabular::ALIGN_LEFT:
3593 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3596 case Tabular::M_ALIGN_RIGHT:
3598 case Tabular::ALIGN_RIGHT:
3599 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3602 case Tabular::M_ALIGN_CENTER:
3604 case Tabular::ALIGN_CENTER:
3605 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3608 case Tabular::ALIGN_BLOCK:
3609 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3610 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3613 case Tabular::M_VALIGN_TOP:
3615 case Tabular::VALIGN_TOP:
3617 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3620 case Tabular::M_VALIGN_BOTTOM:
3622 case Tabular::VALIGN_BOTTOM:
3624 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3627 case Tabular::M_VALIGN_MIDDLE:
3629 case Tabular::VALIGN_MIDDLE:
3631 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3634 case Tabular::SET_LONGTABULAR:
3635 status.setOnOff(tabular.isLongTabular());
3638 case Tabular::UNSET_LONGTABULAR:
3639 status.setOnOff(!tabular.isLongTabular());
3642 case Tabular::TOGGLE_ROTATE_TABULAR:
3643 case Tabular::SET_ROTATE_TABULAR:
3644 status.setOnOff(tabular.getRotateTabular());
3647 case Tabular::UNSET_ROTATE_TABULAR:
3648 status.setOnOff(!tabular.getRotateTabular());
3651 case Tabular::TOGGLE_ROTATE_CELL:
3652 case Tabular::SET_ROTATE_CELL:
3653 status.setOnOff(!oneCellHasRotationState(false,
3654 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3657 case Tabular::UNSET_ROTATE_CELL:
3658 status.setOnOff(!oneCellHasRotationState(true,
3659 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3662 case Tabular::SET_USEBOX:
3663 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3666 case Tabular::SET_LTFIRSTHEAD:
3667 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3670 case Tabular::UNSET_LTFIRSTHEAD:
3671 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3674 case Tabular::SET_LTHEAD:
3675 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3678 case Tabular::UNSET_LTHEAD:
3679 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3682 case Tabular::SET_LTFOOT:
3683 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3686 case Tabular::UNSET_LTFOOT:
3687 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3690 case Tabular::SET_LTLASTFOOT:
3691 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3694 case Tabular::UNSET_LTLASTFOOT:
3695 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3698 case Tabular::SET_LTNEWPAGE:
3699 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3702 case Tabular::SET_BOOKTABS:
3703 status.setOnOff(tabular.useBookTabs());
3706 case Tabular::UNSET_BOOKTABS:
3707 status.setOnOff(!tabular.useBookTabs());
3712 status.enabled(false);
3718 // These are only enabled inside tabular
3719 case LFUN_CELL_BACKWARD:
3720 case LFUN_CELL_FORWARD:
3721 status.enabled(true);
3724 // disable these with multiple cells selected
3725 case LFUN_INSET_INSERT:
3726 case LFUN_TABULAR_INSERT:
3727 case LFUN_FLEX_INSERT:
3728 case LFUN_FLOAT_INSERT:
3729 case LFUN_FLOAT_WIDE_INSERT:
3730 case LFUN_FOOTNOTE_INSERT:
3731 case LFUN_MARGINALNOTE_INSERT:
3732 case LFUN_MATH_INSERT:
3733 case LFUN_MATH_MODE:
3734 case LFUN_MATH_MUTATE:
3735 case LFUN_MATH_DISPLAY:
3736 case LFUN_NOTE_INSERT:
3737 case LFUN_OPTIONAL_INSERT:
3738 case LFUN_BOX_INSERT:
3739 case LFUN_BRANCH_INSERT:
3740 case LFUN_WRAP_INSERT:
3741 case LFUN_ERT_INSERT: {
3742 if (tablemode(cur)) {
3743 status.enabled(false);
3746 return cell(cur.idx())->getStatus(cur, cmd, status);
3749 // disable in non-fixed-width cells
3750 case LFUN_NEWLINE_INSERT:
3751 case LFUN_BREAK_PARAGRAPH:
3752 case LFUN_BREAK_PARAGRAPH_SKIP: {
3753 if (tabular.getPWidth(cur.idx()).zero()) {
3754 status.enabled(false);
3757 return cell(cur.idx())->getStatus(cur, cmd, status);
3761 if (tabularStackDirty() && theClipboard().isInternal()) {
3762 status.enabled(true);
3765 return cell(cur.idx())->getStatus(cur, cmd, status);
3767 case LFUN_INSET_MODIFY:
3768 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3769 status.enabled(true);
3775 // we try to handle this event in the insets dispatch function.
3776 return cell(cur.idx())->getStatus(cur, cmd, status);
3781 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3783 return tabular.latex(os, runparams);
3787 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3789 os << '\n'; // output table on a new line
3790 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3791 tabular.plaintext(os, runparams, dp, false, 0);
3792 return PLAINTEXT_NEWLINE;
3796 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3801 // FIXME: Why not pass a proper DocIterator here?
3803 // if the table is inside a float it doesn't need the informaltable
3804 // wrapper. Search for it.
3805 for (master = owner(); master; master = master->owner())
3806 if (master->lyxCode() == FLOAT_CODE)
3811 os << "<informaltable>";
3814 ret += tabular.docbook(os, runparams);
3816 os << "</informaltable>";
3823 void InsetTabular::validate(LaTeXFeatures & features) const
3825 tabular.validate(features);
3829 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3831 return tabular.cellInset(idx);
3835 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3837 return tabular.cellInset(idx);
3841 void InsetTabular::cursorPos(BufferView const & bv,
3842 CursorSlice const & sl, bool boundary, int & x, int & y) const
3844 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3846 // y offset correction
3847 int const row = tabular.cellRow(sl.idx());
3848 for (int i = 0; i <= row; ++i) {
3850 y += tabular.rowAscent(i);
3851 y += tabular.getAdditionalHeight(i);
3854 y += tabular.rowDescent(i);
3857 // x offset correction
3858 int const col = tabular.cellColumn(sl.idx());
3859 int idx = tabular.cellIndex(row, 0);
3860 for (int j = 0; j < col; ++j) {
3861 if (tabular.isPartOfMultiColumn(row, j))
3863 x += tabular.columnWidth(idx);
3866 x += tabular.getBeginningOfTextInCell(idx);
3867 x += ADD_TO_TABULAR_WIDTH;
3872 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3876 Inset const & inset = *tabular.cellInset(cell);
3877 Point o = bv.coordCache().getInsets().xy(&inset);
3878 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3879 int const xend = xbeg + tabular.columnWidth(cell);
3880 row_type const row = tabular.cellRow(cell);
3881 int const ybeg = o.y_ - tabular.rowAscent(row) -
3882 tabular.getAdditionalHeight(row);
3883 int const yend = o.y_ + tabular.rowDescent(row);
3895 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3896 // << " ybeg=" << ybeg << " yend=" << yend
3897 // << " xx=" << xx << " yy=" << yy
3898 // << " dist=" << xx + yy << endl;
3903 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3905 //lyxerr << "InsetTabular::editXY: " << this << endl;
3906 cur.selection() = false;
3908 cur.idx() = getNearestCell(cur.bv(), x, y);
3910 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3914 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3916 cur.idx() = getNearestCell(cur.bv(), x, y);
3917 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3921 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3923 idx_type idx_min = 0;
3924 int dist_min = numeric_limits<int>::max();
3925 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3926 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3927 int const d = dist(bv, i, x, y);
3938 int InsetTabular::cellXPos(idx_type const cell) const
3942 for (; !tabular.isFirstCellInRow(c); --c)
3945 for (; c < cell; ++c)
3946 lx += tabular.columnWidth(c);
3952 void InsetTabular::resetPos(Cursor & cur) const
3954 BufferView & bv = cur.bv();
3955 int const maxwidth = bv.workWidth();
3957 if (&cur.inset() != this) {
3961 int const X2 = maxwidth;
3962 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3963 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3964 int const x2 = x1 + tabular.columnWidth(cur.idx());
3967 scx_ = X1 + 20 - x1;
3969 scx_ = X2 - 20 - x2;
3974 cur.updateFlags(Update::Force | Update::FitCursor);
3978 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
3980 if (isRightToLeft(cur)) {
3981 if (tabular.isFirstCellInRow(cur.idx())) {
3982 row_type const row = tabular.cellRow(cur.idx());
3983 if (row == tabular.rowCount() - 1)
3985 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3992 if (tabular.isLastCell(cur.idx()))
3998 cur.boundary(false);
4000 // in visual mode, place cursor at extreme left or right
4002 switch(entry_from) {
4004 case ENTRY_DIRECTION_RIGHT:
4005 cur.posVisToRowExtremity(false /* !left */);
4007 case ENTRY_DIRECTION_LEFT:
4008 cur.posVisToRowExtremity(true /* left */);
4010 case ENTRY_DIRECTION_IGNORE:
4011 // nothing to do in this case
4020 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4022 if (isRightToLeft(cur)) {
4023 if (tabular.isLastCellInRow(cur.idx())) {
4024 row_type const row = tabular.cellRow(cur.idx());
4027 cur.idx() = tabular.getFirstCellInRow(row);
4028 cur.idx() = tabular.cellAbove(cur.idx());
4030 if (tabular.isLastCell(cur.idx()))
4035 if (cur.idx() == 0) // first cell
4039 cur.pit() = cur.lastpit();
4040 cur.pos() = cur.lastpos();
4042 // in visual mode, place cursor at extreme left or right
4044 switch(entry_from) {
4046 case ENTRY_DIRECTION_RIGHT:
4047 cur.posVisToRowExtremity(false /* !left */);
4049 case ENTRY_DIRECTION_LEFT:
4050 cur.posVisToRowExtremity(true /* left */);
4052 case ENTRY_DIRECTION_IGNORE:
4053 // nothing to do in this case
4058 // FIXME: this accesses the position cache before it is initialized
4063 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4065 Tabular::Feature action = Tabular::LAST_ACTION;
4068 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4069 string const tmp = tabularFeature[i].feature;
4071 if (tmp == what.substr(0, tmp.length())) {
4072 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4073 //tabularFeatures[i].feature.length()))
4074 action = tabularFeature[i].action;
4078 if (action == Tabular::LAST_ACTION)
4082 ltrim(what.substr(tabularFeature[i].feature.length()));
4083 tabularFeatures(cur, action, val);
4088 static void checkLongtableSpecial(Tabular::ltType & ltt,
4089 string const & special, bool & flag)
4091 if (special == "dl_above") {
4094 } else if (special == "dl_below") {
4095 ltt.bottomDL = flag;
4097 } else if (special == "empty") {
4106 bool InsetTabular::oneCellHasRotationState(bool rotated,
4107 row_type row_start, row_type row_end,
4108 col_type col_start, col_type col_end) const {
4110 for (row_type i = row_start; i <= row_end; ++i) {
4111 for (col_type j = col_start; j <= col_end; ++j) {
4112 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4121 void InsetTabular::tabularFeatures(Cursor & cur,
4122 Tabular::Feature feature, string const & value)
4124 col_type sel_col_start;
4125 col_type sel_col_end;
4126 row_type sel_row_start;
4127 row_type sel_row_end;
4128 bool setLines = false;
4129 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4130 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4134 case Tabular::M_ALIGN_LEFT:
4135 case Tabular::ALIGN_LEFT:
4136 setAlign = LYX_ALIGN_LEFT;
4139 case Tabular::M_ALIGN_RIGHT:
4140 case Tabular::ALIGN_RIGHT:
4141 setAlign = LYX_ALIGN_RIGHT;
4144 case Tabular::M_ALIGN_CENTER:
4145 case Tabular::ALIGN_CENTER:
4146 setAlign = LYX_ALIGN_CENTER;
4149 case Tabular::ALIGN_BLOCK:
4150 setAlign = LYX_ALIGN_BLOCK;
4153 case Tabular::M_VALIGN_TOP:
4154 case Tabular::VALIGN_TOP:
4155 setVAlign = Tabular::LYX_VALIGN_TOP;
4158 case Tabular::M_VALIGN_BOTTOM:
4159 case Tabular::VALIGN_BOTTOM:
4160 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4163 case Tabular::M_VALIGN_MIDDLE:
4164 case Tabular::VALIGN_MIDDLE:
4165 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4172 cur.recordUndoInset(ATOMIC_UNDO);
4174 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4175 row_type const row = tabular.cellRow(cur.idx());
4176 col_type const column = tabular.cellColumn(cur.idx());
4178 Tabular::ltType ltt;
4182 case Tabular::SET_PWIDTH: {
4183 Length const len(value);
4184 tabular.setColumnPWidth(cur, cur.idx(), len);
4186 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4187 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4191 case Tabular::SET_MPWIDTH:
4192 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4195 case Tabular::SET_SPECIAL_COLUMN:
4196 case Tabular::SET_SPECIAL_MULTI:
4197 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4200 case Tabular::APPEND_ROW:
4201 // append the row into the tabular
4202 tabular.appendRow(cur.idx());
4205 case Tabular::APPEND_COLUMN:
4206 // append the column into the tabular
4207 tabular.appendColumn(cur.idx());
4208 cur.idx() = tabular.cellIndex(row, column);
4211 case Tabular::DELETE_ROW:
4212 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4213 tabular.deleteRow(sel_row_start);
4214 if (sel_row_start >= tabular.rowCount())
4216 cur.idx() = tabular.cellIndex(sel_row_start, column);
4219 cur.selection() = false;
4222 case Tabular::DELETE_COLUMN:
4223 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4224 tabular.deleteColumn(sel_col_start);
4225 if (sel_col_start >= tabular.columnCount())
4227 cur.idx() = tabular.cellIndex(row, sel_col_start);
4230 cur.selection() = false;
4233 case Tabular::COPY_ROW:
4234 tabular.copyRow(row);
4237 case Tabular::COPY_COLUMN:
4238 tabular.copyColumn(column);
4239 cur.idx() = tabular.cellIndex(row, column);
4242 case Tabular::TOGGLE_LINE_TOP: {
4243 bool lineSet = !tabular.topLine(cur.idx());
4244 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4245 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4246 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4250 case Tabular::TOGGLE_LINE_BOTTOM: {
4251 bool lineSet = !tabular.bottomLine(cur.idx());
4252 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4253 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4254 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4258 case Tabular::TOGGLE_LINE_LEFT: {
4259 bool lineSet = !tabular.leftLine(cur.idx());
4260 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4261 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4262 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4266 case Tabular::TOGGLE_LINE_RIGHT: {
4267 bool lineSet = !tabular.rightLine(cur.idx());
4268 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4270 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4274 case Tabular::M_ALIGN_LEFT:
4275 case Tabular::M_ALIGN_RIGHT:
4276 case Tabular::M_ALIGN_CENTER:
4278 case Tabular::ALIGN_LEFT:
4279 case Tabular::ALIGN_RIGHT:
4280 case Tabular::ALIGN_CENTER:
4281 case Tabular::ALIGN_BLOCK:
4282 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4283 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4284 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4287 case Tabular::M_VALIGN_TOP:
4288 case Tabular::M_VALIGN_BOTTOM:
4289 case Tabular::M_VALIGN_MIDDLE:
4291 case Tabular::VALIGN_TOP:
4292 case Tabular::VALIGN_BOTTOM:
4293 case Tabular::VALIGN_MIDDLE:
4294 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4295 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4296 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4299 case Tabular::MULTICOLUMN: {
4300 if (!cur.selection()) {
4301 // just multicol for one single cell
4302 // check whether we are completely in a multicol
4303 if (tabular.isMultiColumn(cur.idx()))
4304 tabular.unsetMultiColumn(cur.idx());
4306 tabular.setMultiColumn(cur.idx(), 1);
4309 // we have a selection so this means we just add all this
4310 // cells to form a multicolumn cell
4311 idx_type const s_start = cur.selBegin().idx();
4312 idx_type const s_end = cur.selEnd().idx();
4313 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4314 cur.idx() = s_start;
4317 cur.selection() = false;
4321 case Tabular::SET_ALL_LINES:
4323 case Tabular::UNSET_ALL_LINES:
4324 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4325 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4326 tabular.setAllLines(
4327 tabular.cellIndex(i,j), setLines);
4330 case Tabular::SET_BORDER_LINES:
4331 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4332 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4333 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4335 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4336 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4337 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4341 case Tabular::SET_LONGTABULAR:
4342 tabular.setLongTabular(true);
4345 case Tabular::UNSET_LONGTABULAR:
4346 tabular.setLongTabular(false);
4349 case Tabular::SET_ROTATE_TABULAR:
4350 tabular.setRotateTabular(true);
4353 case Tabular::UNSET_ROTATE_TABULAR:
4354 tabular.setRotateTabular(false);
4357 case Tabular::TOGGLE_ROTATE_TABULAR:
4358 tabular.setRotateTabular(!tabular.getRotateTabular());
4361 case Tabular::SET_ROTATE_CELL:
4362 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4363 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4364 tabular.setRotateCell(
4365 tabular.cellIndex(i, j), true);
4368 case Tabular::UNSET_ROTATE_CELL:
4369 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4370 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4371 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4374 case Tabular::TOGGLE_ROTATE_CELL:
4376 bool oneNotRotated = oneCellHasRotationState(false,
4377 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4379 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4380 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4381 tabular.setRotateCell(tabular.cellIndex(i, j),
4386 case Tabular::SET_USEBOX: {
4387 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4388 if (val == tabular.getUsebox(cur.idx()))
4389 val = Tabular::BOX_NONE;
4390 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4391 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4392 tabular.setUsebox(tabular.cellIndex(i, j), val);
4396 case Tabular::UNSET_LTFIRSTHEAD:
4398 case Tabular::SET_LTFIRSTHEAD:
4399 tabular.getRowOfLTFirstHead(row, ltt);
4400 checkLongtableSpecial(ltt, value, flag);
4401 tabular.setLTHead(row, flag, ltt, true);
4404 case Tabular::UNSET_LTHEAD:
4406 case Tabular::SET_LTHEAD:
4407 tabular.getRowOfLTHead(row, ltt);
4408 checkLongtableSpecial(ltt, value, flag);
4409 tabular.setLTHead(row, flag, ltt, false);
4412 case Tabular::UNSET_LTFOOT:
4414 case Tabular::SET_LTFOOT:
4415 tabular.getRowOfLTFoot(row, ltt);
4416 checkLongtableSpecial(ltt, value, flag);
4417 tabular.setLTFoot(row, flag, ltt, false);
4420 case Tabular::UNSET_LTLASTFOOT:
4422 case Tabular::SET_LTLASTFOOT:
4423 tabular.getRowOfLTLastFoot(row, ltt);
4424 checkLongtableSpecial(ltt, value, flag);
4425 tabular.setLTFoot(row, flag, ltt, true);
4428 case Tabular::SET_LTNEWPAGE:
4429 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4432 case Tabular::SET_BOOKTABS:
4433 tabular.setBookTabs(true);
4436 case Tabular::UNSET_BOOKTABS:
4437 tabular.setBookTabs(false);
4440 case Tabular::SET_TOP_SPACE: {
4442 if (value == "default")
4443 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4444 tabular.row_info[i].top_space_default = true;
4445 else if (isValidLength(value, &len))
4446 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4447 tabular.row_info[i].top_space_default = false;
4448 tabular.row_info[i].top_space = len;
4451 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4452 tabular.row_info[i].top_space_default = false;
4453 tabular.row_info[i].top_space = len;
4458 case Tabular::SET_BOTTOM_SPACE: {
4460 if (value == "default")
4461 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4462 tabular.row_info[i].bottom_space_default = true;
4463 else if (isValidLength(value, &len))
4464 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4465 tabular.row_info[i].bottom_space_default = false;
4466 tabular.row_info[i].bottom_space = len;
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4470 tabular.row_info[i].bottom_space_default = false;
4471 tabular.row_info[i].bottom_space = len;
4476 case Tabular::SET_INTERLINE_SPACE: {
4478 if (value == "default")
4479 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4480 tabular.row_info[i].interline_space_default = true;
4481 else if (isValidLength(value, &len))
4482 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4483 tabular.row_info[i].interline_space_default = false;
4484 tabular.row_info[i].interline_space = len;
4487 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4488 tabular.row_info[i].interline_space_default = false;
4489 tabular.row_info[i].interline_space = len;
4494 // dummy stuff just to avoid warnings
4495 case Tabular::LAST_ACTION:
4501 bool InsetTabular::showInsetDialog(BufferView * bv) const
4503 bv->showDialog("tabular", params2string(*this),
4504 const_cast<InsetTabular *>(this));
4509 void InsetTabular::openLayoutDialog(BufferView * bv) const
4511 bv->showDialog("tabular", params2string(*this),
4512 const_cast<InsetTabular *>(this));
4516 bool InsetTabular::copySelection(Cursor & cur)
4518 if (!cur.selection())
4523 getSelection(cur, rs, re, cs, ce);
4525 paste_tabular.reset(new Tabular(tabular));
4527 for (row_type i = 0; i < rs; ++i)
4528 paste_tabular->deleteRow(0);
4530 row_type const rows = re - rs + 1;
4531 while (paste_tabular->rowCount() > rows)
4532 paste_tabular->deleteRow(rows);
4534 for (col_type i = 0; i < cs; ++i)
4535 paste_tabular->deleteColumn(0);
4537 col_type const columns = ce - cs + 1;
4538 while (paste_tabular->columnCount() > columns)
4539 paste_tabular->deleteColumn(columns);
4541 // We clear all the InsetTableCell pointers, since they
4542 // might now become invalid and there is no point in having
4543 // them point to temporary things in paste_tabular.
4544 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4545 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4546 paste_tabular->cellInset(i,j)->setCellData(0);
4547 paste_tabular->cellInset(i,j)->setTabular(0);
4550 odocstringstream os;
4551 OutputParams const runparams(0);
4552 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4553 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4554 cap::copySelection(cur, os.str());
4556 // mark tabular stack dirty
4557 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4558 // when we (hopefully) have a one-for-all paste mechanism.
4559 // This must be called after cap::copySelection.
4560 dirtyTabularStack(true);
4566 bool InsetTabular::pasteClipboard(Cursor & cur)
4570 col_type const actcol = tabular.cellColumn(cur.idx());
4571 row_type const actrow = tabular.cellRow(cur.idx());
4572 for (row_type r1 = 0, r2 = actrow;
4573 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4575 for (col_type c1 = 0, c2 = actcol;
4576 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4578 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4579 tabular.isPartOfMultiColumn(r2, c2))
4581 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4585 if (tabular.isPartOfMultiColumn(r2, c2)) {
4589 shared_ptr<InsetTableCell> inset(
4590 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4591 // note that setCellInset will call InsetTableCell::setCellData()
4592 // and InsetTableCell::setTabular()
4593 tabular.setCellInset(r2, c2, inset);
4594 // FIXME: change tracking (MG)
4595 inset->setChange(Change(cur.buffer().params().trackChanges ?
4596 Change::INSERTED : Change::UNCHANGED));
4604 void InsetTabular::cutSelection(Cursor & cur)
4606 if (!cur.selection())
4611 getSelection(cur, rs, re, cs, ce);
4612 for (row_type i = rs; i <= re; ++i) {
4613 for (col_type j = cs; j <= ce; ++j) {
4614 shared_ptr<InsetTableCell> t
4615 = cell(tabular.cellIndex(i, j));
4616 if (cur.buffer().params().trackChanges)
4617 // FIXME: Change tracking (MG)
4618 t->setChange(Change(Change::DELETED));
4624 // cursor position might be invalid now
4625 if (cur.pit() > cur.lastpit())
4626 cur.pit() = cur.lastpit();
4627 if (cur.pos() > cur.lastpos())
4628 cur.pos() = cur.lastpos();
4629 cur.clearSelection();
4633 bool InsetTabular::isRightToLeft(Cursor & cur) const
4635 LASSERT(cur.depth() > 1, /**/);
4636 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4637 pos_type const parentpos = cur[cur.depth() - 2].pos();
4638 return parentpar.getFontSettings(cur.bv().buffer().params(),
4639 parentpos).language()->rightToLeft();
4643 void InsetTabular::getSelection(Cursor & cur,
4644 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4646 CursorSlice const & beg = cur.selBegin();
4647 CursorSlice const & end = cur.selEnd();
4648 cs = tabular.cellColumn(beg.idx());
4649 ce = tabular.cellColumn(end.idx());
4652 cs = tabular.cellColumn(end.idx());
4654 ce = tabular.cellRightColumn(end.idx());
4657 rs = tabular.cellRow(beg.idx());
4658 re = tabular.cellRow(end.idx());
4664 Text * InsetTabular::getText(int idx) const
4666 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4670 void InsetTabular::setChange(Change const & change)
4672 for (idx_type idx = 0; idx < nargs(); ++idx)
4673 cell(idx)->setChange(change);
4677 void InsetTabular::acceptChanges(BufferParams const & bparams)
4679 for (idx_type idx = 0; idx < nargs(); ++idx)
4680 cell(idx)->acceptChanges(bparams);
4684 void InsetTabular::rejectChanges(BufferParams const & bparams)
4686 for (idx_type idx = 0; idx < nargs(); ++idx)
4687 cell(idx)->rejectChanges(bparams);
4691 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4693 return tabular.getPWidth(cell).zero();
4697 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4699 return !tabular.getPWidth(cell).zero();
4703 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4706 if (buf.length() <= 0)
4711 col_type maxCols = 1;
4712 size_t const len = buf.length();
4716 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4724 maxCols = max(cols, maxCols);
4730 maxCols = max(cols, maxCols);
4736 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4737 loctab = paste_tabular.get();
4739 dirtyTabularStack(true);
4742 cell = bv.cursor().idx();
4743 ocol = tabular.cellColumn(cell);
4744 row = tabular.cellRow(cell);
4748 idx_type const cells = loctab->cellCount();
4751 rows = loctab->rowCount();
4752 col_type const columns = loctab->columnCount();
4754 while (cell < cells && p < len && row < rows &&
4755 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4761 // we can only set this if we are not too far right
4762 if (cols < columns) {
4763 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4764 Font const font = bv.textMetrics(&inset->text_).
4766 inset->setText(buf.substr(op, p - op), font,
4767 buffer().params().trackChanges);
4773 // we can only set this if we are not too far right
4774 if (cols < columns) {
4775 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4776 Font const font = bv.textMetrics(&inset->text_).
4778 inset->setText(buf.substr(op, p - op), font,
4779 buffer().params().trackChanges);
4784 cell = loctab->cellIndex(row, cols);
4790 // check for the last cell if there is no trailing '\n'
4791 if (cell < cells && op < len) {
4792 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4793 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4794 inset->setText(buf.substr(op, len - op), font,
4795 buffer().params().trackChanges);
4801 void InsetTabular::addPreview(PreviewLoader & loader) const
4803 row_type const rows = tabular.rowCount();
4804 col_type const columns = tabular.columnCount();
4805 for (row_type i = 0; i < rows; ++i) {
4806 for (col_type j = 0; j < columns; ++j)
4807 tabular.cellInset(i, j)->addPreview(loader);
4812 bool InsetTabular::tablemode(Cursor & cur) const
4814 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4818 bool InsetTabular::completionSupported(Cursor const & cur) const
4820 Cursor const & bvCur = cur.bv().cursor();
4821 if (&bvCur.inset() != this)
4823 return cur.text()->completionSupported(cur);
4827 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4829 return completionSupported(cur);
4833 bool InsetTabular::automaticInlineCompletion() const
4835 return lyxrc.completion_inline_text;
4839 bool InsetTabular::automaticPopupCompletion() const
4841 return lyxrc.completion_popup_text;
4845 bool InsetTabular::showCompletionCursor() const
4847 return lyxrc.completion_cursor_text;
4851 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4853 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4857 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4859 if (!completionSupported(cur))
4861 return cur.text()->completionPrefix(cur);
4865 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4867 if (!completionSupported(cur))
4870 return cur.text()->insertCompletion(cur, s, finished);
4874 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4875 Dimension & dim) const
4877 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4878 tm.completionPosAndDim(cur, x, y, dim);
4882 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4884 istringstream data(in);
4886 lex.setStream(data);
4893 if (!lex || token != "tabular") {
4894 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4898 // This is part of the inset proper that is usually swallowed
4899 // by Buffer::readInset
4901 if (!lex || token != "Tabular") {
4902 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4910 string InsetTabular::params2string(InsetTabular const & inset)
4913 data << "tabular" << ' ';
4915 data << "\\end_inset\n";