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()
568 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
570 init(buffer, rows_arg, columns_arg);
574 // activates all lines and sets all widths to 0
575 void Tabular::init(Buffer const & buf, row_type rows_arg,
576 col_type columns_arg)
579 row_info = row_vector(rows_arg);
580 column_info = column_vector(columns_arg);
581 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf, *this)));
582 row_info.reserve(10);
583 column_info.reserve(10);
584 cell_info.reserve(100);
586 is_long_tabular = false;
588 use_booktabs = false;
589 size_t row_count = row_info.size();
590 size_t column_count = column_info.size();
591 // set silly default lines
592 for (row_type i = 0; i < row_count; ++i)
593 for (col_type j = 0; j < column_count; ++j) {
594 cell_info[i][j].top_line = true;
595 cell_info[i][j].left_line = true;
596 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
597 cell_info[i][j].right_line = j == column_count - 1;
602 void Tabular::appendRow(idx_type const cell)
604 BufferParams const & bp = buffer().params();
605 row_type const row = cellRow(cell);
607 row_vector::iterator rit = row_info.begin() + row;
608 row_info.insert(rit, RowData());
609 // now set the values of the row before
610 row_info[row] = row_info[row + 1];
612 row_type const nrows = row_info.size();
613 col_type const ncols = column_info.size();
615 cell_vvector old(nrows - 1);
616 for (row_type i = 0; i < nrows - 1; ++i)
617 swap(cell_info[i], old[i]);
619 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer(), *this)));
621 for (row_type i = 0; i <= row; ++i)
622 swap(cell_info[i], old[i]);
623 for (row_type i = row + 2; i < nrows; ++i)
624 swap(cell_info[i], old[i - 1]);
627 for (col_type c = 0; c < ncols; ++c) {
628 // inherit line settings
629 idx_type const i = cellIndex(row + 1, c);
630 idx_type const j = cellIndex(row, c);
631 setLeftLine(i, leftLine(j));
632 setRightLine(i, rightLine(j));
633 setTopLine(i, topLine(j));
634 if (topLine(j) && bottomLine(j)) {
635 setBottomLine(i, true);
636 setBottomLine(j, false);
638 // mark track changes
640 cellInfo(i).inset->setChange(Change(Change::INSERTED));
645 void Tabular::deleteRow(row_type const row)
647 // Not allowed to delete last row
648 if (row_info.size() == 1)
651 row_info.erase(row_info.begin() + row);
652 cell_info.erase(cell_info.begin() + row);
657 void Tabular::copyRow(row_type const row)
659 row_info.insert(row_info.begin() + row, row_info[row]);
660 cell_info.insert(cell_info.begin() + row, cell_info[row]);
662 if (buffer().params().trackChanges)
663 for (col_type j = 0; j < column_info.size(); ++j)
664 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
670 void Tabular::appendColumn(idx_type const cell)
672 col_type const c = cellColumn(cell);
673 column_vector::iterator cit = column_info.begin() + c + 1;
674 column_info.insert(cit, ColumnData());
675 row_type const nrows = row_info.size();
676 // set the column values of the column before
677 column_info[c + 1] = column_info[c];
679 for (row_type r = 0; r < nrows; ++r) {
680 cell_info[r].insert(cell_info[r].begin() + c + 1,
681 CellData(buffer(), *this));
682 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
683 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
685 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
688 for (row_type r = 0; r < nrows; ++r) {
689 // inherit line settings
690 idx_type const i = cellIndex(r, c + 1);
691 idx_type const j = cellIndex(r, c);
692 setBottomLine(i, bottomLine(j));
693 setTopLine(i, topLine(j));
694 setLeftLine(i, leftLine(j));
695 if (rightLine(j) && rightLine(j)) {
696 setRightLine(i, true);
697 setRightLine(j, false);
700 cellInfo(i).inset->clear();
701 if (buffer().params().trackChanges)
702 cellInfo(i).inset->setChange(Change(Change::INSERTED));
707 void Tabular::deleteColumn(col_type const column)
709 // Not allowed to delete last column
710 if (column_info.size() == 1)
713 column_info.erase(column_info.begin() + column);
714 size_t row_count = row_info.size();
715 for (row_type i = 0; i < row_count; ++i) {
716 // Care about multicolumn cells
717 if (column + 1 < column_info.size() &&
718 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
719 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
720 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
722 cell_info[i].erase(cell_info[i].begin() + column);
728 void Tabular::copyColumn(col_type const column)
730 BufferParams const & bp = buffer().params();
731 column_info.insert(column_info.begin() + column, column_info[column]);
733 size_t row_count = row_info.size();
734 for (row_type i = 0; i < row_count; ++i)
735 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
738 for (row_type i = 0; i < row_count; ++i)
739 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
744 void Tabular::updateIndexes()
746 col_type ncols = column_info.size();
747 row_type nrows = row_info.size();
749 for (row_type row = 0; row < nrows; ++row)
750 for (col_type column = 0; column < ncols; ++column) {
751 if (!isPartOfMultiColumn(row, column))
753 cell_info[row][column].cellno = numberofcells - 1;
756 rowofcell.resize(numberofcells);
757 columnofcell.resize(numberofcells);
759 for (row_type row = 0; row < nrows; ++row)
760 for (col_type column = 0; column < ncols; ++column) {
761 if (isPartOfMultiColumn(row, column))
764 columnofcell[i] = column;
770 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
772 row_type const row = cellRow(cell);
774 for (col_type i = 0; i < column_info.size(); ++i)
775 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
781 bool Tabular::topLine(idx_type const cell) const
783 return cellInfo(cell).top_line;
787 bool Tabular::bottomLine(idx_type const cell) const
789 return cellInfo(cell).bottom_line;
793 bool Tabular::leftLine(idx_type cell) const
797 return cellInfo(cell).left_line;
801 bool Tabular::rightLine(idx_type cell) const
805 return cellInfo(cell).right_line;
809 bool Tabular::topAlreadyDrawn(idx_type cell) const
811 row_type row = cellRow(cell);
814 idx_type i = cellIndex(row - 1, cellColumn(cell));
815 return !rowTopLine(row) && bottomLine(i);
819 bool Tabular::leftAlreadyDrawn(idx_type cell) const
821 col_type col = cellColumn(cell);
824 idx_type i = cellIndex(cellRow(cell), col - 1);
825 return rightLine(i) && !leftLine(cell);
829 bool Tabular::isLastRow(idx_type cell) const
831 return cellRow(cell) == row_info.size() - 1;
835 int Tabular::getAdditionalHeight(row_type row) const
837 if (!row || row >= row_info.size())
840 int const interline_space = row_info[row - 1].interline_space_default ?
842 row_info[row - 1].interline_space.inPixels(width());
843 if (rowTopLine(row) && rowBottomLine(row - 1))
844 return interline_space + WIDTH_OF_LINE;
845 return interline_space;
849 int Tabular::getAdditionalWidth(idx_type cell) const
851 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
853 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
854 return WIDTH_OF_LINE;
859 int Tabular::columnWidth(idx_type cell) const
862 col_type const span = columnSpan(cell);
863 col_type const col = cellColumn(cell);
864 for(col_type c = col; c < col + span ; ++c)
865 w += column_info[c].width;
870 bool Tabular::updateColumnWidths()
872 col_type const ncols = column_info.size();
873 row_type const nrows = row_info.size();
875 // for each col get max of single col cells
876 for(col_type c = 0; c < ncols; ++c) {
878 for(row_type r = 0; r < nrows; ++r) {
879 idx_type const i = cellIndex(r, c);
880 if (columnSpan(i) == 1)
881 new_width = max(new_width, cellInfo(i).width);
884 if (column_info[c].width != new_width) {
885 column_info[c].width = new_width;
889 // update col widths to fit merged cells
890 for(col_type c = 0; c < ncols; ++c)
891 for(row_type r = 0; r < nrows; ++r) {
892 idx_type const i = cellIndex(r, c);
893 int const span = columnSpan(i);
894 if (span == 1 || c > cellColumn(i))
898 for(col_type j = c; j < c + span ; ++j)
899 old_width += column_info[j].width;
901 if (cellInfo(i).width > old_width) {
902 column_info[c + span - 1].width += cellInfo(i).width - old_width;
911 int Tabular::width() const
913 col_type const ncols = column_info.size();
915 for (col_type i = 0; i < ncols; ++i)
916 width += column_info[i].width;
921 void Tabular::setCellWidth(idx_type cell, int new_width)
923 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
924 + getAdditionalWidth(cell);
928 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
931 if (!isMultiColumn(cell) || onlycolumn)
932 column_info[cellColumn(cell)].alignment = align;
934 cellInfo(cell).alignment = align;
938 void Tabular::setVAlignment(idx_type cell, VAlignment align,
941 if (!isMultiColumn(cell) || onlycolumn)
942 column_info[cellColumn(cell)].valignment = align;
944 cellInfo(cell).valignment = align;
951 * Allow line and paragraph breaks for fixed width cells or disallow them,
952 * merge cell paragraphs and reset layout to standard for variable width
955 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
957 inset->setAutoBreakRows(fixedWidth);
961 // merge all paragraphs to one
962 BufferParams const & bp = cur.bv().buffer().params();
963 while (inset->paragraphs().size() > 1)
964 mergeParagraph(bp, inset->paragraphs(), 0);
968 // undo information has already been recorded
969 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
970 bp.documentClass().emptyLayoutName());
977 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
978 Length const & width)
980 col_type const j = cellColumn(cell);
982 column_info[j].p_width = width;
983 for (row_type i = 0; i < row_info.size(); ++i) {
984 idx_type const cell = cellIndex(i, j);
985 // because of multicolumns
986 toggleFixedWidth(cur, cellInset(cell).get(),
987 !getPWidth(cell).zero());
989 // cur paragraph can become invalid after paragraphs were merged
990 if (cur.pit() > cur.lastpit())
991 cur.pit() = cur.lastpit();
992 // cur position can become invalid after newlines were removed
993 if (cur.pos() > cur.lastpos())
994 cur.pos() = cur.lastpos();
998 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
999 Length const & width)
1001 if (!isMultiColumn(cell))
1004 cellInfo(cell).p_width = width;
1005 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1006 // cur paragraph can become invalid after paragraphs were merged
1007 if (cur.pit() > cur.lastpit())
1008 cur.pit() = cur.lastpit();
1009 // cur position can become invalid after newlines were removed
1010 if (cur.pos() > cur.lastpos())
1011 cur.pos() = cur.lastpos();
1016 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1017 Tabular::Feature what)
1019 if (what == SET_SPECIAL_MULTI)
1020 cellInfo(cell).align_special = special;
1022 column_info[cellColumn(cell)].align_special = special;
1026 void Tabular::setAllLines(idx_type cell, bool line)
1028 setTopLine(cell, line);
1029 setBottomLine(cell, line);
1030 setRightLine(cell, line);
1031 setLeftLine(cell, line);
1035 void Tabular::setTopLine(idx_type i, bool line)
1037 cellInfo(i).top_line = line;
1041 void Tabular::setBottomLine(idx_type i, bool line)
1043 cellInfo(i).bottom_line = line;
1047 void Tabular::setLeftLine(idx_type cell, bool line)
1049 cellInfo(cell).left_line = line;
1053 void Tabular::setRightLine(idx_type cell, bool line)
1055 cellInfo(cell).right_line = line;
1058 bool Tabular::rowTopLine(row_type r) const
1060 idx_type i0 = getFirstCellInRow(r);
1061 idx_type i1 = getLastCellInRow(r);
1062 bool all_rows_set = true;
1063 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1064 all_rows_set = cellInfo(j).top_line;
1065 return all_rows_set;
1069 bool Tabular::rowBottomLine(row_type r) const
1071 idx_type i0 = getFirstCellInRow(r);
1072 idx_type i1 = getLastCellInRow(r);
1073 bool all_rows_set = true;
1074 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1075 all_rows_set = cellInfo(j).bottom_line;
1076 return all_rows_set;
1080 bool Tabular::columnLeftLine(col_type c) const
1084 row_type const nrows = row_info.size();
1085 for (row_type r = 0; r < nrows; ++r) {
1086 idx_type i = cellIndex(r, c);
1087 if (c == cellColumn(i)) {
1089 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1090 if (cellInfo(i).left_line || right)
1094 return nrows_left > total / 2;
1098 bool Tabular::columnRightLine(col_type c) const
1100 int nrows_right = 0;
1102 row_type const nrows = row_info.size();
1103 for (row_type r = 0; r < nrows; ++r) {
1104 idx_type i = cellIndex(r, c);
1105 if (c == cellColumn(i) + columnSpan(i) - 1) {
1107 bool left = c + 1 < column_info.size()
1108 && cellInfo(cellIndex(r, c + 1)).left_line
1109 || c + 1 == column_info.size();
1110 if (cellInfo(i).right_line && left)
1114 return nrows_right > total / 2;
1118 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1120 if (!onlycolumn && isMultiColumn(cell))
1121 return cellInfo(cell).alignment;
1122 return column_info[cellColumn(cell)].alignment;
1127 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1129 if (!onlycolumn && isMultiColumn(cell))
1130 return cellInfo(cell).valignment;
1131 return column_info[cellColumn(cell)].valignment;
1135 Length const Tabular::getPWidth(idx_type cell) const
1137 if (isMultiColumn(cell))
1138 return cellInfo(cell).p_width;
1139 return column_info[cellColumn(cell)].p_width;
1143 int Tabular::cellWidth(idx_type cell) const
1145 return cellInfo(cell).width;
1149 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1153 switch (getAlignment(cell)) {
1154 case LYX_ALIGN_CENTER:
1155 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1157 case LYX_ALIGN_RIGHT:
1158 x += columnWidth(cell) - cellWidth(cell);
1159 // + getAdditionalWidth(cell);
1162 // LYX_ALIGN_LEFT: nothing :-)
1166 // the LaTeX Way :-(
1172 bool Tabular::isFirstCellInRow(idx_type cell) const
1174 return cellColumn(cell) == 0;
1178 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1180 if (row > row_info.size() - 1)
1181 row = row_info.size() - 1;
1182 return cell_info[row][0].cellno;
1186 bool Tabular::isLastCellInRow(idx_type cell) const
1188 return cellRightColumn(cell) == column_info.size() - 1;
1192 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1194 if (row > row_info.size() - 1)
1195 row = row_info.size() - 1;
1196 return cell_info[row][column_info.size() - 1].cellno;
1200 Tabular::row_type Tabular::cellRow(idx_type cell) const
1202 if (cell >= numberofcells)
1203 return row_info.size() - 1;
1206 return rowofcell[cell];
1210 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1212 if (cell >= numberofcells)
1213 return column_info.size() - 1;
1216 return columnofcell[cell];
1220 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1222 row_type const row = cellRow(cell);
1223 col_type column = cellColumn(cell);
1224 while (column < column_info.size() - 1 &&
1225 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1231 void Tabular::write(ostream & os) const
1235 << write_attribute("version", 3)
1236 << write_attribute("rows", row_info.size())
1237 << write_attribute("columns", column_info.size())
1239 // global longtable options
1241 << write_attribute("rotate", rotate)
1242 << write_attribute("booktabs", use_booktabs)
1243 << write_attribute("islongtable", is_long_tabular)
1244 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1245 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1246 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1247 << write_attribute("headTopDL", endhead.topDL)
1248 << write_attribute("headBottomDL", endhead.bottomDL)
1249 << write_attribute("footTopDL", endfoot.topDL)
1250 << write_attribute("footBottomDL", endfoot.bottomDL)
1251 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1252 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1253 << write_attribute("lastFootEmpty", endlastfoot.empty)
1255 for (col_type j = 0; j < column_info.size(); ++j) {
1257 << write_attribute("alignment", column_info[j].alignment)
1258 << write_attribute("valignment", column_info[j].valignment)
1259 << write_attribute("width", column_info[j].p_width.asString())
1260 << write_attribute("special", column_info[j].align_special)
1263 for (row_type i = 0; i < row_info.size(); ++i) {
1264 static const string def("default");
1266 if (row_info[i].top_space_default)
1267 os << write_attribute("topspace", def);
1269 os << write_attribute("topspace", row_info[i].top_space);
1270 if (row_info[i].bottom_space_default)
1271 os << write_attribute("bottomspace", def);
1273 os << write_attribute("bottomspace", row_info[i].bottom_space);
1274 if (row_info[i].interline_space_default)
1275 os << write_attribute("interlinespace", def);
1277 os << write_attribute("interlinespace", row_info[i].interline_space);
1278 os << write_attribute("endhead", row_info[i].endhead)
1279 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1280 << write_attribute("endfoot", row_info[i].endfoot)
1281 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1282 << write_attribute("newpage", row_info[i].newpage)
1284 for (col_type j = 0; j < column_info.size(); ++j) {
1286 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1287 << write_attribute("alignment", cell_info[i][j].alignment)
1288 << write_attribute("valignment", cell_info[i][j].valignment)
1289 << write_attribute("topline", cell_info[i][j].top_line)
1290 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1291 << write_attribute("leftline", cell_info[i][j].left_line)
1292 << write_attribute("rightline", cell_info[i][j].right_line)
1293 << write_attribute("rotate", cell_info[i][j].rotate)
1294 << write_attribute("usebox", cell_info[i][j].usebox)
1295 << write_attribute("width", cell_info[i][j].p_width)
1296 << write_attribute("special", cell_info[i][j].align_special)
1298 os << "\\begin_inset ";
1299 cell_info[i][j].inset->write(os);
1300 os << "\n\\end_inset\n"
1305 os << "</lyxtabular>\n";
1309 void Tabular::read(Lexer & lex)
1312 istream & is = lex.getStream();
1314 l_getline(is, line);
1315 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1316 LASSERT(false, /**/);
1321 if (!getTokenValue(line, "version", version))
1323 LASSERT(version >= 2, /**/);
1326 if (!getTokenValue(line, "rows", rows_arg))
1329 if (!getTokenValue(line, "columns", columns_arg))
1331 init(buffer(), rows_arg, columns_arg);
1332 l_getline(is, line);
1333 if (!prefixIs(line, "<features")) {
1334 lyxerr << "Wrong tabular format (expected <features ...> got"
1335 << line << ')' << endl;
1338 getTokenValue(line, "rotate", rotate);
1339 getTokenValue(line, "booktabs", use_booktabs);
1340 getTokenValue(line, "islongtable", is_long_tabular);
1341 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1342 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1343 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1344 getTokenValue(line, "headTopDL", endhead.topDL);
1345 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1346 getTokenValue(line, "footTopDL", endfoot.topDL);
1347 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1348 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1349 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1350 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1352 for (col_type j = 0; j < column_info.size(); ++j) {
1354 if (!prefixIs(line,"<column")) {
1355 lyxerr << "Wrong tabular format (expected <column ...> got"
1356 << line << ')' << endl;
1359 getTokenValue(line, "alignment", column_info[j].alignment);
1360 getTokenValue(line, "valignment", column_info[j].valignment);
1361 getTokenValue(line, "width", column_info[j].p_width);
1362 getTokenValue(line, "special", column_info[j].align_special);
1365 for (row_type i = 0; i < row_info.size(); ++i) {
1366 l_getline(is, line);
1367 if (!prefixIs(line, "<row")) {
1368 lyxerr << "Wrong tabular format (expected <row ...> got"
1369 << line << ')' << endl;
1372 getTokenValue(line, "topspace", row_info[i].top_space,
1373 row_info[i].top_space_default);
1374 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1375 row_info[i].bottom_space_default);
1376 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1377 row_info[i].interline_space_default);
1378 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1379 getTokenValue(line, "endhead", row_info[i].endhead);
1380 getTokenValue(line, "endfoot", row_info[i].endfoot);
1381 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1382 getTokenValue(line, "newpage", row_info[i].newpage);
1383 for (col_type j = 0; j < column_info.size(); ++j) {
1384 l_getline(is, line);
1385 if (!prefixIs(line, "<cell")) {
1386 lyxerr << "Wrong tabular format (expected <cell ...> got"
1387 << line << ')' << endl;
1390 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1391 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1392 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1393 getTokenValue(line, "topline", cell_info[i][j].top_line);
1394 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1395 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1396 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1397 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1398 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1399 getTokenValue(line, "width", cell_info[i][j].p_width);
1400 getTokenValue(line, "special", cell_info[i][j].align_special);
1401 l_getline(is, line);
1402 if (prefixIs(line, "\\begin_inset")) {
1403 cell_info[i][j].inset->read(lex);
1404 l_getline(is, line);
1406 if (!prefixIs(line, "</cell>")) {
1407 lyxerr << "Wrong tabular format (expected </cell> got"
1408 << line << ')' << endl;
1412 l_getline(is, line);
1413 if (!prefixIs(line, "</row>")) {
1414 lyxerr << "Wrong tabular format (expected </row> got"
1415 << line << ')' << endl;
1419 while (!prefixIs(line, "</lyxtabular>")) {
1420 l_getline(is, line);
1426 bool Tabular::isMultiColumn(idx_type cell) const
1428 return cellInfo(cell).multicolumn != CELL_NORMAL;
1432 bool Tabular::isMultiColumnReal(idx_type cell) const
1434 return cellColumn(cell) != cellRightColumn(cell) &&
1435 cellInfo(cell).multicolumn != CELL_NORMAL;
1439 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1441 return cell_info[cellRow(cell)][cellColumn(cell)];
1445 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1447 CellData & cs = cellInfo(cell);
1448 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1449 cs.alignment = column_info[cellColumn(cell)].alignment;
1450 setRightLine(cell, rightLine(cell + number - 1));
1451 for (idx_type i = 1; i < number; ++i) {
1452 CellData & cs1 = cellInfo(cell + i);
1453 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1454 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1461 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1463 row_type const row = cellRow(cell);
1464 col_type const ncols = column_info.size();
1465 idx_type result = 1;
1466 col_type column = cellColumn(cell) + 1;
1467 while (column < ncols && isPartOfMultiColumn(row, column)) {
1475 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1477 row_type const row = cellRow(cell);
1478 col_type column = cellColumn(cell);
1480 idx_type result = 0;
1482 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1483 cell_info[row][column].multicolumn = CELL_NORMAL;
1485 while (column < column_info.size() &&
1486 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1488 cell_info[row][column].multicolumn = CELL_NORMAL;
1498 void Tabular::setRotateCell(idx_type cell, bool flag)
1500 cellInfo(cell).rotate = flag;
1504 bool Tabular::getRotateCell(idx_type cell) const
1506 return cellInfo(cell).rotate;
1510 bool Tabular::needRotating() const
1514 for (row_type i = 0; i < row_info.size(); ++i)
1515 for (col_type j = 0; j < column_info.size(); ++j)
1516 if (cell_info[i][j].rotate)
1522 bool Tabular::isLastCell(idx_type cell) const
1524 if (cell + 1 < numberofcells)
1530 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1532 if (cellRow(cell) > 0)
1533 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1538 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1540 if (cellRow(cell) + 1 < row_info.size())
1541 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1546 Tabular::idx_type Tabular::cellIndex(row_type row,
1547 col_type column) const
1549 BOOST_ASSERT(column != npos && column < column_info.size()
1550 && row != npos && row < row_info.size());
1551 return cell_info[row][column].cellno;
1555 void Tabular::setUsebox(idx_type cell, BoxType type)
1557 cellInfo(cell).usebox = type;
1561 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1563 if (column_info[cellColumn(cell)].p_width.zero() &&
1564 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1566 if (cellInfo(cell).usebox > 1)
1567 return cellInfo(cell).usebox;
1568 return useParbox(cell);
1573 // This are functions used for the longtable support
1575 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1581 row_info[row].endfirsthead = flag;
1585 row_info[row].endhead = flag;
1590 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1593 hd.set = haveLTHead();
1594 return row_info[row].endhead;
1598 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1601 hd.set = haveLTFirstHead();
1602 return row_info[row].endfirsthead;
1606 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1612 row_info[row].endlastfoot = flag;
1616 row_info[row].endfoot = flag;
1621 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1624 fd.set = haveLTFoot();
1625 return row_info[row].endfoot;
1629 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1632 fd.set = haveLTLastFoot();
1633 return row_info[row].endlastfoot;
1637 void Tabular::setLTNewPage(row_type row, bool what)
1639 row_info[row].newpage = what;
1643 bool Tabular::getLTNewPage(row_type row) const
1645 return row_info[row].newpage;
1649 bool Tabular::haveLTHead() const
1651 for (row_type i = 0; i < row_info.size(); ++i)
1652 if (row_info[i].endhead)
1658 bool Tabular::haveLTFirstHead() const
1660 if (endfirsthead.empty)
1662 for (row_type i = 0; i < row_info.size(); ++i)
1663 if (row_info[i].endfirsthead)
1669 bool Tabular::haveLTFoot() const
1671 for (row_type i = 0; i < row_info.size(); ++i)
1672 if (row_info[i].endfoot)
1678 bool Tabular::haveLTLastFoot() const
1680 if (endlastfoot.empty)
1682 for (row_type i = 0; i < row_info.size(); ++i)
1683 if (row_info[i].endlastfoot)
1689 // end longtable support functions
1691 void Tabular::setRowAscent(row_type row, int height)
1693 if (row >= row_info.size() || row_info[row].ascent == height)
1695 row_info[row].ascent = height;
1699 void Tabular::setRowDescent(row_type row, int height)
1701 if (row >= row_info.size() || row_info[row].descent == height)
1703 row_info[row].descent = height;
1707 int Tabular::rowAscent(row_type row) const
1709 if (row >= row_info.size())
1711 return row_info[row].ascent;
1715 int Tabular::rowDescent(row_type row) const
1717 LASSERT(row < row_info.size(), /**/);
1718 return row_info[row].descent;
1722 int Tabular::height() const
1725 for (row_type row = 0; row < row_info.size(); ++row)
1726 height += rowAscent(row) + rowDescent(row) +
1727 getAdditionalHeight(row);
1732 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1734 LASSERT(row < row_info.size(), /**/);
1735 LASSERT(column < column_info.size(), /**/);
1736 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1740 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1742 // FIXME: assert or return 0 as in TeXBottomHLine()?
1743 LASSERT(row != npos, /**/);
1744 LASSERT(row < row_info.size(), /**/);
1746 idx_type const fcell = getFirstCellInRow(row);
1747 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1750 for (idx_type i = fcell; i < n; ++i) {
1754 if (use_booktabs && row == 0) {
1757 } else if (tmp == n - fcell) {
1758 os << (use_booktabs ? "\\midrule " : "\\hline ");
1760 for (idx_type i = fcell; i < n; ++i) {
1762 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1763 << cellColumn(i) + 1
1765 << cellRightColumn(i) + 1
1777 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1779 // FIXME: return 0 or assert as in TeXTopHLine()?
1780 if (row == npos || row >= row_info.size())
1783 idx_type const fcell = getFirstCellInRow(row);
1784 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1787 for (idx_type i = fcell; i < n; ++i) {
1791 if (use_booktabs && row == row_info.size() - 1) {
1792 if (bottomLine(fcell))
1793 os << "\\bottomrule";
1794 } else if (tmp == n - fcell) {
1795 os << (use_booktabs ? "\\midrule" : "\\hline");
1797 for (idx_type i = fcell; i < n; ++i) {
1798 if (bottomLine(i)) {
1799 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1800 << cellColumn(i) + 1
1802 << cellRightColumn(i) + 1
1814 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1818 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1819 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1820 // figure out how to set the lines
1821 // we always set double lines to the right of the cell
1822 row_type const r = cellRow(cell);
1823 col_type const c = cellColumn(cell);
1824 col_type const nextcol = c + columnSpan(cell);
1825 bool colright = columnRightLine(c);
1826 bool colleft = columnLeftLine(c);
1827 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1828 bool forceleft = ismulticol && !prevcellright && leftLine(cell);
1829 bool nextcolleft = nextcol < column_info.size() && colleft;
1830 bool coldouble = colright && nextcolleft;
1831 bool celldouble = rightLine(cell) && nextcol < column_info.size()
1832 && leftLine(cellIndex(r, nextcol));
1833 ismulticol = isMultiColumn(cell)
1834 || (leftLine(cell) && !colleft)
1835 || (rightLine(cell) && !(colright || nextcolleft))
1836 || (coldouble != celldouble) || forceleft;
1838 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1839 if (leftLine(cell) || forceleft)
1841 if (!cellInfo(cell).align_special.empty()) {
1842 os << cellInfo(cell).align_special;
1844 if (!getPWidth(cell).zero()) {
1846 case LYX_VALIGN_TOP:
1849 case LYX_VALIGN_MIDDLE:
1852 case LYX_VALIGN_BOTTOM:
1857 << from_ascii(getPWidth(cell).asLatexString())
1861 case LYX_ALIGN_LEFT:
1864 case LYX_ALIGN_RIGHT:
1871 } // end if else !getPWidth
1872 } // end if else !cellinfo_of_cell
1873 if (rightLine(cell))
1876 // add extra vertical line if we want a double one
1880 if (getRotateCell(cell)) {
1881 os << "\\begin{sideways}\n";
1884 if (getUsebox(cell) == BOX_PARBOX) {
1887 case LYX_VALIGN_TOP:
1890 case LYX_VALIGN_MIDDLE:
1893 case LYX_VALIGN_BOTTOM:
1897 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1899 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1900 os << "\\begin{minipage}[";
1902 case LYX_VALIGN_TOP:
1905 case LYX_VALIGN_MIDDLE:
1908 case LYX_VALIGN_BOTTOM:
1912 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1920 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1925 if (getUsebox(cell) == BOX_PARBOX)
1927 else if (getUsebox(cell) == BOX_MINIPAGE) {
1928 os << "%\n\\end{minipage}";
1931 if (getRotateCell(cell)) {
1932 os << "%\n\\end{sideways}";
1942 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1943 OutputParams const & runparams) const
1945 if (!is_long_tabular)
1949 // output header info
1951 if (endhead.topDL) {
1955 for (row_type i = 0; i < row_info.size(); ++i) {
1956 if (row_info[i].endhead) {
1957 ret += TeXRow(os, i, runparams);
1960 if (endhead.bottomDL) {
1964 os << "\\endhead\n";
1966 if (endfirsthead.empty) {
1967 os << "\\endfirsthead\n";
1971 // output firstheader info
1972 if (haveLTFirstHead()) {
1973 if (endfirsthead.topDL) {
1977 for (row_type i = 0; i < row_info.size(); ++i) {
1978 if (row_info[i].endfirsthead) {
1979 ret += TeXRow(os, i, runparams);
1982 if (endfirsthead.bottomDL) {
1986 os << "\\endfirsthead\n";
1989 // output footer info
1991 if (endfoot.topDL) {
1995 for (row_type i = 0; i < row_info.size(); ++i) {
1996 if (row_info[i].endfoot) {
1997 ret += TeXRow(os, i, runparams);
2000 if (endfoot.bottomDL) {
2004 os << "\\endfoot\n";
2006 if (endlastfoot.empty) {
2007 os << "\\endlastfoot\n";
2011 // output lastfooter info
2012 if (haveLTLastFoot()) {
2013 if (endlastfoot.topDL) {
2017 for (row_type i = 0; i < row_info.size(); ++i) {
2018 if (row_info[i].endlastfoot) {
2019 ret += TeXRow(os, i, runparams);
2022 if (endlastfoot.bottomDL) {
2026 os << "\\endlastfoot\n";
2033 bool Tabular::isValidRow(row_type row) const
2035 if (!is_long_tabular)
2037 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2038 !row_info[row].endfoot && !row_info[row].endlastfoot;
2042 int Tabular::TeXRow(odocstream & os, row_type i,
2043 OutputParams const & runparams) const
2045 idx_type cell = cellIndex(i, 0);
2046 int ret = TeXTopHLine(os, i);
2047 if (row_info[i].top_space_default) {
2049 os << "\\addlinespace\n";
2051 os << "\\noalign{\\vskip\\doublerulesep}\n";
2053 } else if(!row_info[i].top_space.zero()) {
2055 os << "\\addlinespace["
2056 << from_ascii(row_info[i].top_space.asLatexString())
2059 os << "\\noalign{\\vskip"
2060 << from_ascii(row_info[i].top_space.asLatexString())
2065 bool ismulticol = false;
2066 for (col_type j = 0; j < column_info.size(); ++j) {
2067 if (isPartOfMultiColumn(i, j))
2069 ret += TeXCellPreamble(os, cell, ismulticol);
2070 shared_ptr<InsetTableCell> inset = cellInset(cell);
2072 Paragraph const & par = inset->paragraphs().front();
2073 bool rtl = par.isRTL(buffer().params())
2075 && getPWidth(cell).zero();
2078 if (par.getParLanguage(buffer().params())->lang() ==
2081 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2083 // currently, remaning RTL languages are arabic_arabtex and hebrew
2087 ret += inset->latex(os, runparams);
2091 ret += TeXCellPostamble(os, cell, ismulticol);
2092 if (!isLastCellInRow(cell)) { // not last cell in row
2097 os << "\\tabularnewline";
2098 if (row_info[i].bottom_space_default) {
2100 os << "\\addlinespace";
2102 os << "[\\doublerulesep]";
2103 } else if (!row_info[i].bottom_space.zero()) {
2105 os << "\\addlinespace";
2107 << from_ascii(row_info[i].bottom_space.asLatexString())
2112 ret += TeXBottomHLine(os, i);
2113 if (row_info[i].interline_space_default) {
2115 os << "\\addlinespace\n";
2117 os << "\\noalign{\\vskip\\doublerulesep}\n";
2119 } else if (!row_info[i].interline_space.zero()) {
2121 os << "\\addlinespace["
2122 << from_ascii(row_info[i].interline_space.asLatexString())
2125 os << "\\noalign{\\vskip"
2126 << from_ascii(row_info[i].interline_space.asLatexString())
2134 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2138 //+---------------------------------------------------------------------
2139 //+ first the opening preamble +
2140 //+---------------------------------------------------------------------
2143 os << "\\begin{sideways}\n";
2146 if (is_long_tabular)
2147 os << "\\begin{longtable}{";
2149 os << "\\begin{tabular}{";
2151 for (col_type i = 0; i < column_info.size(); ++i) {
2152 if (!use_booktabs && columnLeftLine(i))
2154 if (!column_info[i].align_special.empty()) {
2155 os << column_info[i].align_special;
2157 if (!column_info[i].p_width.zero()) {
2158 switch (column_info[i].alignment) {
2159 case LYX_ALIGN_LEFT:
2160 os << ">{\\raggedright}";
2162 case LYX_ALIGN_RIGHT:
2163 os << ">{\\raggedleft}";
2165 case LYX_ALIGN_CENTER:
2166 os << ">{\\centering}";
2168 case LYX_ALIGN_NONE:
2169 case LYX_ALIGN_BLOCK:
2170 case LYX_ALIGN_LAYOUT:
2171 case LYX_ALIGN_SPECIAL:
2175 switch (column_info[i].valignment) {
2176 case LYX_VALIGN_TOP:
2179 case LYX_VALIGN_MIDDLE:
2182 case LYX_VALIGN_BOTTOM:
2187 << from_ascii(column_info[i].p_width.asLatexString())
2190 switch (column_info[i].alignment) {
2191 case LYX_ALIGN_LEFT:
2194 case LYX_ALIGN_RIGHT:
2201 } // end if else !column_info[i].p_width
2202 } // end if else !column_info[i].align_special
2203 if (!use_booktabs && columnRightLine(i))
2209 ret += TeXLongtableHeaderFooter(os, runparams);
2211 //+---------------------------------------------------------------------
2212 //+ the single row and columns (cells) +
2213 //+---------------------------------------------------------------------
2215 for (row_type i = 0; i < row_info.size(); ++i) {
2216 if (isValidRow(i)) {
2217 ret += TeXRow(os, i, runparams);
2218 if (is_long_tabular && row_info[i].newpage) {
2219 os << "\\newpage\n";
2225 //+---------------------------------------------------------------------
2226 //+ the closing of the tabular +
2227 //+---------------------------------------------------------------------
2229 if (is_long_tabular)
2230 os << "\\end{longtable}";
2232 os << "\\end{tabular}";
2234 os << "\n\\end{sideways}";
2242 int Tabular::docbookRow(odocstream & os, row_type row,
2243 OutputParams const & runparams) const
2246 idx_type cell = getFirstCellInRow(row);
2249 for (col_type j = 0; j < column_info.size(); ++j) {
2250 if (isPartOfMultiColumn(row, j))
2253 os << "<entry align=\"";
2254 switch (getAlignment(cell)) {
2255 case LYX_ALIGN_LEFT:
2258 case LYX_ALIGN_RIGHT:
2266 os << "\" valign=\"";
2267 switch (getVAlignment(cell)) {
2268 case LYX_VALIGN_TOP:
2271 case LYX_VALIGN_BOTTOM:
2274 case LYX_VALIGN_MIDDLE:
2279 if (isMultiColumn(cell)) {
2280 os << " namest=\"col" << j << "\" ";
2281 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2285 ret += cellInset(cell)->docbook(os, runparams);
2294 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2298 //+---------------------------------------------------------------------
2299 //+ first the opening preamble +
2300 //+---------------------------------------------------------------------
2302 os << "<tgroup cols=\"" << column_info.size()
2303 << "\" colsep=\"1\" rowsep=\"1\">\n";
2305 for (col_type i = 0; i < column_info.size(); ++i) {
2306 os << "<colspec colname=\"col" << i << "\" align=\"";
2307 switch (column_info[i].alignment) {
2308 case LYX_ALIGN_LEFT:
2311 case LYX_ALIGN_RIGHT:
2319 if (runparams.flavor == OutputParams::XML)
2325 //+---------------------------------------------------------------------
2326 //+ Long Tabular case +
2327 //+---------------------------------------------------------------------
2329 // output header info
2330 if (haveLTHead() || haveLTFirstHead()) {
2333 for (row_type i = 0; i < row_info.size(); ++i) {
2334 if (row_info[i].endhead || row_info[i].endfirsthead) {
2335 ret += docbookRow(os, i, runparams);
2341 // output footer info
2342 if (haveLTFoot() || haveLTLastFoot()) {
2345 for (row_type i = 0; i < row_info.size(); ++i) {
2346 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2347 ret += docbookRow(os, i, runparams);
2354 //+---------------------------------------------------------------------
2355 //+ the single row and columns (cells) +
2356 //+---------------------------------------------------------------------
2360 for (row_type i = 0; i < row_info.size(); ++i) {
2361 if (isValidRow(i)) {
2362 ret += docbookRow(os, i, runparams);
2367 //+---------------------------------------------------------------------
2368 //+ the closing of the tabular +
2369 //+---------------------------------------------------------------------
2378 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2379 vector<unsigned int> const & clen) const
2381 idx_type const fcell = getFirstCellInRow(row);
2382 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2385 for (idx_type i = fcell; i < n; ++i) {
2395 for (idx_type i = fcell; i < n; ++i) {
2406 col_type column = cellColumn(i);
2407 int len = clen[column];
2408 while (column < column_info.size() - 1
2409 && isPartOfMultiColumn(row, ++column))
2410 len += clen[column] + 4;
2411 os << docstring(len, ch);
2426 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2427 vector<unsigned int> const & clen) const
2429 idx_type const fcell = getFirstCellInRow(row);
2430 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2433 for (idx_type i = fcell; i < n; ++i) {
2434 if (bottomLine(i)) {
2443 for (idx_type i = fcell; i < n; ++i) {
2444 if (bottomLine(i)) {
2454 col_type column = cellColumn(i);
2455 int len = clen[column];
2456 while (column < column_info.size() -1
2457 && isPartOfMultiColumn(row, ++column))
2458 len += clen[column] + 4;
2459 os << docstring(len, ch);
2460 if (bottomLine(i)) {
2474 void Tabular::plaintextPrintCell(odocstream & os,
2475 OutputParams const & runparams,
2476 idx_type cell, row_type row, col_type column,
2477 vector<unsigned int> const & clen,
2478 bool onlydata) const
2480 odocstringstream sstr;
2481 cellInset(cell)->plaintext(sstr, runparams);
2493 unsigned int len1 = sstr.str().length();
2494 unsigned int len2 = clen[column];
2495 while (column < column_info.size() -1
2496 && isPartOfMultiColumn(row, ++column))
2497 len2 += clen[column] + 4;
2500 switch (getAlignment(cell)) {
2502 case LYX_ALIGN_LEFT:
2505 case LYX_ALIGN_RIGHT:
2509 case LYX_ALIGN_CENTER:
2515 os << docstring(len1, ' ') << sstr.str()
2516 << docstring(len2, ' ');
2518 if (rightLine(cell))
2525 void Tabular::plaintext(odocstream & os,
2526 OutputParams const & runparams, int const depth,
2527 bool onlydata, char_type delim) const
2529 // first calculate the width of the single columns
2530 vector<unsigned int> clen(column_info.size());
2533 // first all non (real) multicolumn cells!
2534 for (col_type j = 0; j < column_info.size(); ++j) {
2536 for (row_type i = 0; i < row_info.size(); ++i) {
2537 idx_type cell = cellIndex(i, j);
2538 if (isMultiColumnReal(cell))
2540 odocstringstream sstr;
2541 cellInset(cell)->plaintext(sstr, runparams);
2542 if (clen[j] < sstr.str().length())
2543 clen[j] = sstr.str().length();
2546 // then all (real) multicolumn cells!
2547 for (col_type j = 0; j < column_info.size(); ++j) {
2548 for (row_type i = 0; i < row_info.size(); ++i) {
2549 idx_type cell = cellIndex(i, j);
2550 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2552 odocstringstream sstr;
2553 cellInset(cell)->plaintext(sstr, runparams);
2554 int len = int(sstr.str().length());
2555 idx_type const n = columnSpan(cell);
2556 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2558 if (len > int(clen[j + n - 1]))
2559 clen[j + n - 1] = len;
2564 for (row_type i = 0; i < row_info.size(); ++i) {
2565 if (!onlydata && plaintextTopHLine(os, i, clen))
2566 os << docstring(depth * 2, ' ');
2567 for (col_type j = 0; j < column_info.size(); ++j) {
2568 if (isPartOfMultiColumn(i, j))
2570 if (onlydata && j > 0)
2571 // we don't use operator<< for single UCS4 character.
2572 // see explanation in docstream.h
2574 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2579 os << docstring(depth * 2, ' ');
2580 if (plaintextBottomHLine(os, i, clen))
2581 os << docstring(depth * 2, ' ');
2587 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2589 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2593 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2594 col_type column) const
2596 return cell_info[row][column].inset;
2600 void Tabular::setCellInset(row_type row, col_type column,
2601 shared_ptr<InsetTableCell> ins) const
2603 CellData & cd = cell_info[row][column];
2605 // reset the InsetTableCell's pointers
2606 ins->setCellData(&cd);
2607 ins->setTabular(this);
2611 Tabular::idx_type Tabular::cellFromInset(Inset const * inset) const
2613 // is this inset part of the tabular?
2615 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2616 LASSERT(false, /**/);
2619 for (idx_type cell = 0, n = numberofcells; cell < n; ++cell)
2620 if (cellInset(cell).get() == inset) {
2621 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2622 << "cell=" << cell);
2626 // We should have found a cell at this point
2627 lyxerr << "Tabular::cellFromInset: Cell of inset "
2628 << inset << " not found!" << endl;
2629 LASSERT(false, /**/);
2635 void Tabular::validate(LaTeXFeatures & features) const
2637 features.require("NeedTabularnewline");
2639 features.require("booktabs");
2640 if (is_long_tabular)
2641 features.require("longtable");
2643 features.require("rotating");
2644 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2645 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2646 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2647 features.require("array");
2648 cellInset(cell)->validate(features);
2653 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2655 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2656 ParagraphList::const_iterator cit = parlist.begin();
2657 ParagraphList::const_iterator end = parlist.end();
2659 for (; cit != end; ++cit)
2660 for (int i = 0; i < cit->size(); ++i)
2661 if (cit->isNewline(i))
2668 /////////////////////////////////////////////////////////////////////
2672 /////////////////////////////////////////////////////////////////////
2674 InsetTableCell::InsetTableCell(Buffer const & buf,
2675 Tabular::CellData const * cell, Tabular const * table)
2676 : InsetText(buf), cell_data_(cell), table_(table)
2680 bool InsetTableCell::forceEmptyLayout(idx_type) const
2682 LASSERT(table_, /**/);
2683 LASSERT(cell_data_, /**/);
2684 return table_->getPWidth(cell_data_->cellno).zero();
2687 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2689 LASSERT(table_, /**/);
2690 LASSERT(cell_data_, /**/);
2691 return !table_->getPWidth(cell_data_->cellno).zero();
2694 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2695 FuncStatus & status) const
2698 switch (cmd.action) {
2700 enabled = !forceEmptyLayout();
2702 case LFUN_LAYOUT_PARAGRAPH:
2703 enabled = allowParagraphCustomization();
2706 return InsetText::getStatus(cur, cmd, status);
2708 status.enabled(enabled);
2712 /////////////////////////////////////////////////////////////////////
2716 /////////////////////////////////////////////////////////////////////
2718 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2720 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2721 rowselect_(false), colselect_(false)
2723 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2727 InsetTabular::InsetTabular(InsetTabular const & tab)
2728 : Inset(tab), tabular(tab.tabular), scx_(0)
2730 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2734 InsetTabular::~InsetTabular()
2736 hideDialogs("tabular", this);
2740 bool InsetTabular::insetAllowed(InsetCode code) const
2742 if (code == MATHMACRO_CODE)
2749 void InsetTabular::write(ostream & os) const
2751 os << "Tabular" << endl;
2756 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2758 // FIXME: depending on the selection state, we could offer a different menu.
2759 return from_ascii("context-tabular");
2763 void InsetTabular::read(Lexer & lex)
2765 //bool const old_format = (lex.getString() == "\\LyXTable");
2773 string token = lex.getString();
2774 while (lex && token != "\\end_inset") {
2776 token = lex.getString();
2779 lex.printError("Missing \\end_inset at this point. ");
2783 int InsetTabular::rowFromY(Cursor & cur, int y) const
2785 // top y coordinate of tabular
2786 int h = yo(cur.bv()) - tabular.rowAscent(0);
2787 size_t nrows = tabular.row_info.size();
2789 for (; r < nrows && y > h; ++r) {
2790 h += tabular.rowAscent(r);
2791 h += tabular.rowDescent(r);
2792 h += tabular.getAdditionalHeight(r);
2798 int InsetTabular::columnFromX(Cursor & cur, int x) const
2800 // left x coordinate of tabular
2801 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2802 size_t ncols = tabular.column_info.size();
2804 for (; c < ncols && x > w; ++c)
2805 w += tabular.columnWidth(c);
2810 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2812 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2813 // mi.base.textwidth << "\n";
2816 LASSERT(false, /**/);
2820 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2823 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2824 if (tabular.isPartOfMultiColumn(i, j))
2825 // Multicolumn cell, but not first one
2830 if (tabular.cell_info[i][j].multicolumn ==
2831 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2832 p_width = tabular.cellInfo(cell).p_width;
2834 p_width = tabular.column_info[j].p_width;
2835 if (!p_width.zero())
2836 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2837 tabular.cellInset(cell)->metrics(m, dim);
2838 if (!p_width.zero())
2839 dim.wid = m.base.textwidth;
2840 tabular.setCellWidth(cell, dim.wid);
2841 maxAsc = max(maxAsc, dim.asc);
2842 maxDesc = max(maxDesc, dim.des);
2845 int const top_space = tabular.row_info[i].top_space_default ?
2846 default_line_space :
2847 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2848 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2849 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2850 default_line_space :
2851 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2852 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2854 tabular.updateColumnWidths();
2855 dim.asc = tabular.rowAscent(0);
2856 dim.des = tabular.height() - dim.asc;
2857 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2861 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2863 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2864 BufferView * bv = pi.base.bv;
2866 // FIXME: As the full backrgound is painted in drawSelection(),
2867 // we have no choice but to do a full repaint for the Text cells.
2868 pi.full_repaint = true;
2870 resetPos(bv->cursor());
2873 x += ADD_TO_TABULAR_WIDTH;
2875 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2878 first_visible_cell = Tabular::npos;
2879 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
2881 int const a = tabular.rowAscent(i);
2882 int const d = tabular.rowDescent(i);
2883 idx = tabular.cellIndex(i, 0);
2884 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2885 if (tabular.isPartOfMultiColumn(i, j))
2887 if (first_visible_cell == Tabular::npos)
2888 first_visible_cell = idx;
2890 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2891 // Cache the Inset position.
2892 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2893 if (nx + tabular.columnWidth(idx) < 0
2894 || nx > bv->workWidth()
2896 || y - a > bv->workHeight()) {
2897 pi.pain.setDrawingEnabled(false);
2898 cell(idx)->draw(pi, cx, y);
2899 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2900 pi.pain.setDrawingEnabled(original_drawing_state);
2902 cell(idx)->draw(pi, cx, y);
2903 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2905 nx += tabular.columnWidth(idx);
2909 if (i + 1 < tabular.row_info.size())
2910 y += d + tabular.rowAscent(i + 1) +
2911 tabular.getAdditionalHeight(i + 1);
2916 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2918 Cursor & cur = pi.base.bv->cursor();
2920 x += scx_ + ADD_TO_TABULAR_WIDTH;
2922 // FIXME: it is wrong to completely paint the background
2923 // if we want to do single row painting.
2925 // Paint background of current tabular
2926 int const w = tabular.width();
2927 int const h = tabular.height();
2928 int yy = y - tabular.rowAscent(0);
2929 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2931 if (!cur.selection())
2933 if (&cur.inset() != this)
2939 if (tablemode(cur)) {
2942 getSelection(cur, rs, re, cs, ce);
2943 y -= tabular.rowAscent(0);
2944 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
2945 int const a = tabular.rowAscent(j);
2946 int const h = a + tabular.rowDescent(j);
2948 y += tabular.getAdditionalHeight(j);
2949 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
2950 if (tabular.isPartOfMultiColumn(j, i))
2952 idx_type const cell =
2953 tabular.cellIndex(j, i);
2954 int const w = tabular.columnWidth(cell);
2955 if (i >= cs && i <= ce && j >= rs && j <= re)
2956 pi.pain.fillRectangle(xx, y, w, h,
2964 x += cellXPos(cur.idx());
2965 x += tabular.getBeginningOfTextInCell(cur.idx());
2966 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
2971 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
2972 row_type row, idx_type cell, bool erased) const
2974 int x2 = x + tabular.columnWidth(cell);
2975 bool on_off = false;
2976 ColorCode col = Color_tabularline;
2977 ColorCode onoffcol = Color_tabularonoffline;
2980 col = Color_deletedtext;
2981 onoffcol = Color_deletedtext;
2984 if (!tabular.topAlreadyDrawn(cell)) {
2985 on_off = !tabular.topLine(cell);
2986 pain.line(x, y - tabular.rowAscent(row),
2987 x2, y - tabular.rowAscent(row),
2988 on_off ? onoffcol : col,
2989 on_off ? Painter::line_onoffdash : Painter::line_solid);
2991 on_off = !tabular.bottomLine(cell);
2992 pain.line(x, y + tabular.rowDescent(row),
2993 x2, y + tabular.rowDescent(row),
2994 on_off ? onoffcol : col,
2995 on_off ? Painter::line_onoffdash : Painter::line_solid);
2996 if (!tabular.leftAlreadyDrawn(cell)) {
2997 on_off = !tabular.leftLine(cell);
2998 pain.line(x, y - tabular.rowAscent(row),
2999 x, y + tabular.rowDescent(row),
3000 on_off ? onoffcol : col,
3001 on_off ? Painter::line_onoffdash : Painter::line_solid);
3003 on_off = !tabular.rightLine(cell);
3004 pain.line(x2 - tabular.getAdditionalWidth(cell),
3005 y - tabular.rowAscent(row),
3006 x2 - tabular.getAdditionalWidth(cell),
3007 y + tabular.rowDescent(row),
3008 on_off ? onoffcol : col,
3009 on_off ? Painter::line_onoffdash : Painter::line_solid);
3013 docstring InsetTabular::editMessage() const
3015 return _("Opened table");
3019 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3021 //lyxerr << "InsetTabular::edit: " << this << endl;
3023 cur.selection() = false;
3026 if (isRightToLeft(cur))
3027 cur.idx() = tabular.getLastCellInRow(0);
3033 if (isRightToLeft(cur))
3034 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3036 cur.idx() = tabular.numberofcells - 1;
3038 cur.pos() = cur.lastpos(); // FIXME crude guess
3040 // FIXME: this accesses the position cache before it is initialized
3042 //cur.bv().fitCursor();
3046 void InsetTabular::updateLabels(ParIterator const & it)
3048 // In a longtable, tell captions what the current float is
3049 Counters & cnts = buffer().params().documentClass().counters();
3050 string const saveflt = cnts.current_float();
3051 if (tabular.is_long_tabular)
3052 cnts.current_float("table");
3054 ParIterator it2 = it;
3056 size_t const end = it2.nargs();
3057 for ( ; it2.idx() < end; it2.top().forwardIdx())
3058 lyx::updateLabels(buffer(), it2);
3061 if (tabular.is_long_tabular)
3062 cnts.current_float(saveflt);
3066 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3068 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3069 << "\n cur:" << cur);
3070 CursorSlice sl = cur.top();
3071 Cursor & bvcur = cur.bv().cursor();
3073 switch (cmd.action) {
3075 case LFUN_MOUSE_PRESS: {
3076 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3078 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3079 || cmd.x > xo(cur.bv()) + tabular.width()) {
3080 row_type r = rowFromY(cur, cmd.y);
3081 cur.idx() = tabular.getFirstCellInRow(r);
3084 cur.idx() = tabular.getLastCellInRow(r);
3085 cur.pos() = cur.lastpos();
3086 cur.selection() = true;
3092 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3093 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3094 || cmd.y > y0 + tabular.height()) {
3095 col_type c = columnFromX(cur, cmd.x);
3096 cur.idx() = tabular.cellIndex(0, c);
3099 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3100 cur.pos() = cur.lastpos();
3101 cur.selection() = true;
3106 // do not reset cursor/selection if we have selected
3107 // some cells (bug 2715).
3108 if (cmd.button() == mouse_button::button3
3109 && &bvcur.selBegin().inset() == this
3110 && tablemode(bvcur))
3113 // Let InsetTableCell do it
3114 cell(cur.idx())->dispatch(cur, cmd);
3117 case LFUN_MOUSE_MOTION:
3118 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3119 if (cmd.button() == mouse_button::button1) {
3120 // only accept motions to places not deeper nested than the real anchor
3121 if (!bvcur.anchor_.hasPart(cur)) {
3125 // select (additional) row
3127 row_type r = rowFromY(cur, cmd.y);
3128 cur.idx() = tabular.getLastCellInRow(r);
3129 bvcur.setCursor(cur);
3130 bvcur.selection() = true;
3133 // select (additional) column
3135 col_type c = columnFromX(cur, cmd.x);
3136 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3137 bvcur.setCursor(cur);
3138 bvcur.selection() = true;
3141 // only update if selection changes
3142 if (bvcur.idx() == cur.idx() &&
3143 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3145 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3146 bvcur.setCursor(cur);
3147 bvcur.selection() = true;
3151 case LFUN_MOUSE_RELEASE:
3156 case LFUN_CELL_BACKWARD:
3158 cur.selection() = false;
3161 case LFUN_CELL_FORWARD:
3163 cur.selection() = false;
3165 case LFUN_CHAR_FORWARD_SELECT:
3166 case LFUN_CHAR_FORWARD:
3167 case LFUN_CHAR_BACKWARD_SELECT:
3168 case LFUN_CHAR_BACKWARD:
3169 case LFUN_CHAR_RIGHT_SELECT:
3170 case LFUN_CHAR_RIGHT:
3171 case LFUN_CHAR_LEFT_SELECT:
3172 case LFUN_CHAR_LEFT: {
3174 // determine whether we move to next or previous cell, where to enter
3175 // the new cell from, and which command to "finish" (i.e., exit the
3178 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3179 FuncCode finish_lfun;
3181 if (cmd.action == LFUN_CHAR_FORWARD
3182 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3184 finish_lfun = LFUN_FINISHED_FORWARD;
3186 else if (cmd.action == LFUN_CHAR_BACKWARD
3187 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3189 finish_lfun = LFUN_FINISHED_BACKWARD;
3191 // LEFT or RIGHT commands --- the interpretation will depend on the
3192 // table's direction.
3194 bool right = (cmd.action == LFUN_CHAR_RIGHT
3195 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3196 next_cell = (isRightToLeft(cur) != right);
3198 if (lyxrc.visual_cursor) {
3199 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3203 finish_lfun = LFUN_FINISHED_RIGHT;
3205 finish_lfun = LFUN_FINISHED_LEFT;
3209 // finally, now that we know what we want to do, do it!
3210 cell(cur.idx())->dispatch(cur, cmd);
3211 if (!cur.result().dispatched()) {
3212 // move to next/prev cell, as appropriate
3213 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3214 << " cell from: " << int(entry_from));
3216 moveNextCell(cur, entry_from);
3218 movePrevCell(cur, entry_from);
3219 // if we're exiting the table, call the appropriate FINISHED lfun
3220 if (sl == cur.top())
3221 cmd = FuncRequest(finish_lfun);
3229 case LFUN_DOWN_SELECT:
3231 cell(cur.idx())->dispatch(cur, cmd);
3232 cur.dispatched(); // override the cell's decision
3233 if (sl == cur.top())
3234 // if our Text didn't do anything to the cursor
3235 // then we try to put the cursor into the cell below
3236 // setting also the right targetX.
3237 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3238 cur.idx() = tabular.cellBelow(cur.idx());
3240 TextMetrics const & tm =
3241 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3242 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3244 if (sl == cur.top()) {
3245 // we trick it to go to forward after leaving the
3247 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3252 case LFUN_UP_SELECT:
3254 cell(cur.idx())->dispatch(cur, cmd);
3255 cur.dispatched(); // override the cell's decision
3256 if (sl == cur.top())
3257 // if our Text didn't do anything to the cursor
3258 // then we try to put the cursor into the cell above
3259 // setting also the right targetX.
3260 if (tabular.cellRow(cur.idx()) != 0) {
3261 cur.idx() = tabular.cellAbove(cur.idx());
3262 cur.pit() = cur.lastpit();
3263 Text const * text = cell(cur.idx())->getText(0);
3264 TextMetrics const & tm = cur.bv().textMetrics(text);
3265 ParagraphMetrics const & pm =
3266 tm.parMetrics(cur.lastpit());
3267 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3269 if (sl == cur.top()) {
3270 cmd = FuncRequest(LFUN_UP);
3275 // case LFUN_SCREEN_DOWN: {
3276 // //if (hasSelection())
3277 // // cur.selection() = false;
3278 // col_type const col = tabular.cellColumn(cur.idx());
3279 // int const t = cur.bv().top_y() + cur.bv().height();
3280 // if (t < yo() + tabular.getHeightOfTabular()) {
3281 // cur.bv().scrollDocView(t);
3282 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3284 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3291 // case LFUN_SCREEN_UP: {
3292 // //if (hasSelection())
3293 // // cur.selection() = false;
3294 // col_type const col = tabular.cellColumn(cur.idx());
3295 // int const t = cur.bv().top_y() + cur.bv().height();
3297 // cur.bv().scrollDocView(t);
3301 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3305 // cur.par() = cur.lastpar();
3306 // cur.pos() = cur.lastpos();
3310 case LFUN_LAYOUT_TABULAR:
3311 cur.bv().showDialog("tabular", params2string(*this), this);
3314 case LFUN_INSET_DIALOG_UPDATE:
3315 cur.bv().updateDialog("tabular", params2string(*this));
3318 case LFUN_TABULAR_FEATURE:
3319 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3323 // insert file functions
3324 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3325 case LFUN_FILE_INSERT_PLAINTEXT: {
3327 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3328 FileName(to_utf8(cmd.argument())));
3331 cur.recordUndoInset(INSERT_UNDO);
3332 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3333 // content has been replaced,
3334 // so cursor might be invalid
3335 cur.pos() = cur.lastpos();
3336 cur.pit() = cur.lastpit();
3337 bvcur.setCursor(cur);
3344 if (tablemode(cur)) {
3345 if (copySelection(cur)) {
3346 cur.recordUndoInset(DELETE_UNDO);
3351 cell(cur.idx())->dispatch(cur, cmd);
3354 case LFUN_CHAR_DELETE_BACKWARD:
3355 case LFUN_CHAR_DELETE_FORWARD:
3356 if (tablemode(cur)) {
3357 cur.recordUndoInset(DELETE_UNDO);
3361 cell(cur.idx())->dispatch(cur, cmd);
3365 if (!cur.selection())
3367 if (tablemode(cur)) {
3371 cell(cur.idx())->dispatch(cur, cmd);
3374 case LFUN_CLIPBOARD_PASTE:
3375 case LFUN_PRIMARY_SELECTION_PASTE: {
3376 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3377 theClipboard().getAsText() :
3378 theSelection().get();
3381 // pass to InsertPlaintextString, but
3382 // only if we have multi-cell content
3383 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3384 cur.recordUndoInset(INSERT_UNDO);
3385 if (insertPlaintextString(cur.bv(), clip, false)) {
3386 // content has been replaced,
3387 // so cursor might be invalid
3388 cur.pos() = cur.lastpos();
3389 cur.pit() = cur.lastpit();
3390 bvcur.setCursor(cur);
3394 // Let the cell handle normal text
3395 cell(cur.idx())->dispatch(cur, cmd);
3400 if (tabularStackDirty() && theClipboard().isInternal() ||
3401 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3402 cur.recordUndoInset(INSERT_UNDO);
3403 pasteClipboard(cur);
3406 cell(cur.idx())->dispatch(cur, cmd);
3409 case LFUN_FONT_EMPH:
3410 case LFUN_FONT_BOLD:
3411 case LFUN_FONT_ROMAN:
3412 case LFUN_FONT_NOUN:
3413 case LFUN_FONT_ITAL:
3414 case LFUN_FONT_FRAK:
3415 case LFUN_FONT_TYPEWRITER:
3416 case LFUN_FONT_SANS:
3417 case LFUN_FONT_FREE_APPLY:
3418 case LFUN_FONT_FREE_UPDATE:
3419 case LFUN_FONT_SIZE:
3420 case LFUN_FONT_UNDERLINE:
3422 case LFUN_WORD_CAPITALIZE:
3423 case LFUN_WORD_UPCASE:
3424 case LFUN_WORD_LOWCASE:
3425 case LFUN_CHARS_TRANSPOSE:
3426 if (tablemode(cur)) {
3429 getSelection(cur, rs, re, cs, ce);
3430 Cursor tmpcur = cur;
3431 for (row_type i = rs; i <= re; ++i) {
3432 for (col_type j = cs; j <= ce; ++j) {
3433 // cursor follows cell:
3434 tmpcur.idx() = tabular.cellIndex(i, j);
3435 // select this cell only:
3438 tmpcur.resetAnchor();
3439 tmpcur.pit() = tmpcur.lastpit();
3440 tmpcur.pos() = tmpcur.top().lastpos();
3441 tmpcur.setCursor(tmpcur);
3442 tmpcur.setSelection();
3443 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3448 cell(cur.idx())->dispatch(cur, cmd);
3452 // we try to handle this event in the insets dispatch function.
3453 cell(cur.idx())->dispatch(cur, cmd);
3459 // function sets an object as defined in func_status.h:
3460 // states OK, Unknown, Disabled, On, Off.
3461 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3462 FuncStatus & status) const
3464 switch (cmd.action) {
3465 case LFUN_TABULAR_FEATURE: {
3466 int action = Tabular::LAST_ACTION;
3468 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3469 string const tmp = tabularFeature[i].feature;
3470 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3471 action = tabularFeature[i].action;
3475 if (action == Tabular::LAST_ACTION) {
3477 status.unknown(true);
3481 string const argument
3482 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3484 row_type sel_row_start = 0;
3485 row_type sel_row_end = 0;
3486 col_type sel_col_start = 0;
3487 col_type sel_col_end = 0;
3488 Tabular::ltType dummyltt;
3491 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3494 case Tabular::SET_PWIDTH:
3495 case Tabular::SET_MPWIDTH:
3496 case Tabular::SET_SPECIAL_COLUMN:
3497 case Tabular::SET_SPECIAL_MULTI:
3498 case Tabular::APPEND_ROW:
3499 case Tabular::APPEND_COLUMN:
3500 case Tabular::DELETE_ROW:
3501 case Tabular::DELETE_COLUMN:
3502 case Tabular::COPY_ROW:
3503 case Tabular::COPY_COLUMN:
3504 case Tabular::SET_ALL_LINES:
3505 case Tabular::UNSET_ALL_LINES:
3506 case Tabular::SET_TOP_SPACE:
3507 case Tabular::SET_BOTTOM_SPACE:
3508 case Tabular::SET_INTERLINE_SPACE:
3509 case Tabular::SET_BORDER_LINES:
3513 case Tabular::MULTICOLUMN:
3514 status.enabled(sel_row_start == sel_row_end);
3515 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3518 case Tabular::TOGGLE_LINE_TOP:
3519 status.setOnOff(tabular.topLine(cur.idx()));
3522 case Tabular::TOGGLE_LINE_BOTTOM:
3523 status.setOnOff(tabular.bottomLine(cur.idx()));
3526 case Tabular::TOGGLE_LINE_LEFT:
3527 status.setOnOff(tabular.leftLine(cur.idx()));
3530 case Tabular::TOGGLE_LINE_RIGHT:
3531 status.setOnOff(tabular.rightLine(cur.idx()));
3534 case Tabular::M_ALIGN_LEFT:
3536 case Tabular::ALIGN_LEFT:
3537 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3540 case Tabular::M_ALIGN_RIGHT:
3542 case Tabular::ALIGN_RIGHT:
3543 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3546 case Tabular::M_ALIGN_CENTER:
3548 case Tabular::ALIGN_CENTER:
3549 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3552 case Tabular::ALIGN_BLOCK:
3553 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3554 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3557 case Tabular::M_VALIGN_TOP:
3559 case Tabular::VALIGN_TOP:
3561 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3564 case Tabular::M_VALIGN_BOTTOM:
3566 case Tabular::VALIGN_BOTTOM:
3568 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3571 case Tabular::M_VALIGN_MIDDLE:
3573 case Tabular::VALIGN_MIDDLE:
3575 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3578 case Tabular::SET_LONGTABULAR:
3579 status.setOnOff(tabular.is_long_tabular);
3582 case Tabular::UNSET_LONGTABULAR:
3583 status.setOnOff(!tabular.is_long_tabular);
3586 case Tabular::TOGGLE_ROTATE_TABULAR:
3587 case Tabular::SET_ROTATE_TABULAR:
3588 status.setOnOff(tabular.rotate);
3591 case Tabular::UNSET_ROTATE_TABULAR:
3592 status.setOnOff(!tabular.rotate);
3595 case Tabular::TOGGLE_ROTATE_CELL:
3596 case Tabular::SET_ROTATE_CELL:
3597 status.setOnOff(!oneCellHasRotationState(false,
3598 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3601 case Tabular::UNSET_ROTATE_CELL:
3602 status.setOnOff(!oneCellHasRotationState(true,
3603 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3606 case Tabular::SET_USEBOX:
3607 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3610 case Tabular::SET_LTFIRSTHEAD:
3611 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3614 case Tabular::UNSET_LTFIRSTHEAD:
3615 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3618 case Tabular::SET_LTHEAD:
3619 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3622 case Tabular::UNSET_LTHEAD:
3623 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3626 case Tabular::SET_LTFOOT:
3627 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3630 case Tabular::UNSET_LTFOOT:
3631 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3634 case Tabular::SET_LTLASTFOOT:
3635 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3638 case Tabular::UNSET_LTLASTFOOT:
3639 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3642 case Tabular::SET_LTNEWPAGE:
3643 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3646 case Tabular::SET_BOOKTABS:
3647 status.setOnOff(tabular.use_booktabs);
3650 case Tabular::UNSET_BOOKTABS:
3651 status.setOnOff(!tabular.use_booktabs);
3656 status.enabled(false);
3662 // These are only enabled inside tabular
3663 case LFUN_CELL_BACKWARD:
3664 case LFUN_CELL_FORWARD:
3665 status.enabled(true);
3668 // disable these with multiple cells selected
3669 case LFUN_INSET_INSERT:
3670 case LFUN_TABULAR_INSERT:
3671 case LFUN_FLEX_INSERT:
3672 case LFUN_FLOAT_INSERT:
3673 case LFUN_FLOAT_WIDE_INSERT:
3674 case LFUN_FOOTNOTE_INSERT:
3675 case LFUN_MARGINALNOTE_INSERT:
3676 case LFUN_MATH_INSERT:
3677 case LFUN_MATH_MODE:
3678 case LFUN_MATH_MUTATE:
3679 case LFUN_MATH_DISPLAY:
3680 case LFUN_NOTE_INSERT:
3681 case LFUN_OPTIONAL_INSERT:
3682 case LFUN_BOX_INSERT:
3683 case LFUN_BRANCH_INSERT:
3684 case LFUN_WRAP_INSERT:
3685 case LFUN_ERT_INSERT: {
3686 if (tablemode(cur)) {
3687 status.enabled(false);
3690 return cell(cur.idx())->getStatus(cur, cmd, status);
3693 // disable in non-fixed-width cells
3694 case LFUN_NEWLINE_INSERT:
3695 case LFUN_BREAK_PARAGRAPH:
3696 case LFUN_BREAK_PARAGRAPH_SKIP: {
3697 if (tabular.getPWidth(cur.idx()).zero()) {
3698 status.enabled(false);
3701 return cell(cur.idx())->getStatus(cur, cmd, status);
3705 if (tabularStackDirty() && theClipboard().isInternal()) {
3706 status.enabled(true);
3709 return cell(cur.idx())->getStatus(cur, cmd, status);
3711 case LFUN_INSET_MODIFY:
3712 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3713 status.enabled(true);
3719 // we try to handle this event in the insets dispatch function.
3720 return cell(cur.idx())->getStatus(cur, cmd, status);
3725 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3727 return tabular.latex(os, runparams);
3731 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3733 os << '\n'; // output table on a new line
3734 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3735 tabular.plaintext(os, runparams, dp, false, 0);
3736 return PLAINTEXT_NEWLINE;
3740 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3745 // FIXME: Why not pass a proper DocIterator here?
3747 // if the table is inside a float it doesn't need the informaltable
3748 // wrapper. Search for it.
3749 for (master = owner(); master; master = master->owner())
3750 if (master->lyxCode() == FLOAT_CODE)
3755 os << "<informaltable>";
3758 ret += tabular.docbook(os, runparams);
3760 os << "</informaltable>";
3767 void InsetTabular::validate(LaTeXFeatures & features) const
3769 tabular.validate(features);
3773 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3775 return tabular.cellInset(idx);
3779 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3781 return tabular.cellInset(idx);
3785 void InsetTabular::cursorPos(BufferView const & bv,
3786 CursorSlice const & sl, bool boundary, int & x, int & y) const
3788 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3790 // y offset correction
3791 int const row = tabular.cellRow(sl.idx());
3792 for (int i = 0; i <= row; ++i) {
3794 y += tabular.rowAscent(i);
3795 y += tabular.getAdditionalHeight(i);
3798 y += tabular.rowDescent(i);
3801 // x offset correction
3802 int const col = tabular.cellColumn(sl.idx());
3803 int idx = tabular.cellIndex(row, 0);
3804 for (int j = 0; j < col; ++j) {
3805 if (tabular.isPartOfMultiColumn(row, j))
3807 x += tabular.columnWidth(idx);
3810 x += tabular.getBeginningOfTextInCell(idx);
3811 x += ADD_TO_TABULAR_WIDTH;
3816 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3820 Inset const & inset = *tabular.cellInset(cell);
3821 Point o = bv.coordCache().getInsets().xy(&inset);
3822 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3823 int const xend = xbeg + tabular.columnWidth(cell);
3824 row_type const row = tabular.cellRow(cell);
3825 int const ybeg = o.y_ - tabular.rowAscent(row) -
3826 tabular.getAdditionalHeight(row);
3827 int const yend = o.y_ + tabular.rowDescent(row);
3839 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3840 // << " ybeg=" << ybeg << " yend=" << yend
3841 // << " xx=" << xx << " yy=" << yy
3842 // << " dist=" << xx + yy << endl;
3847 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3849 //lyxerr << "InsetTabular::editXY: " << this << endl;
3850 cur.selection() = false;
3852 cur.idx() = getNearestCell(cur.bv(), x, y);
3854 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3858 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3860 cur.idx() = getNearestCell(cur.bv(), x, y);
3861 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3865 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3867 idx_type idx_min = 0;
3868 int dist_min = numeric_limits<int>::max();
3869 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3870 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3871 int const d = dist(bv, i, x, y);
3882 int InsetTabular::cellXPos(idx_type const cell) const
3886 for (; !tabular.isFirstCellInRow(c); --c)
3889 for (; c < cell; ++c)
3890 lx += tabular.columnWidth(c);
3896 void InsetTabular::resetPos(Cursor & cur) const
3898 BufferView & bv = cur.bv();
3899 int const maxwidth = bv.workWidth();
3901 if (&cur.inset() != this) {
3905 int const X2 = maxwidth;
3906 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3907 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3908 int const x2 = x1 + tabular.columnWidth(cur.idx());
3911 scx_ = X1 + 20 - x1;
3913 scx_ = X2 - 20 - x2;
3918 cur.updateFlags(Update::Force | Update::FitCursor);
3922 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
3924 if (isRightToLeft(cur)) {
3925 if (tabular.isFirstCellInRow(cur.idx())) {
3926 row_type const row = tabular.cellRow(cur.idx());
3927 if (row == tabular.row_info.size() - 1)
3929 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3936 if (tabular.isLastCell(cur.idx()))
3942 cur.boundary(false);
3944 // in visual mode, place cursor at extreme left or right
3946 switch(entry_from) {
3948 case ENTRY_DIRECTION_RIGHT:
3949 cur.posVisToRowExtremity(false /* !left */);
3951 case ENTRY_DIRECTION_LEFT:
3952 cur.posVisToRowExtremity(true /* left */);
3954 case ENTRY_DIRECTION_IGNORE:
3955 // nothing to do in this case
3964 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
3966 if (isRightToLeft(cur)) {
3967 if (tabular.isLastCellInRow(cur.idx())) {
3968 row_type const row = tabular.cellRow(cur.idx());
3971 cur.idx() = tabular.getFirstCellInRow(row);
3972 cur.idx() = tabular.cellAbove(cur.idx());
3974 if (tabular.isLastCell(cur.idx()))
3979 if (cur.idx() == 0) // first cell
3983 cur.pit() = cur.lastpit();
3984 cur.pos() = cur.lastpos();
3986 // in visual mode, place cursor at extreme left or right
3988 switch(entry_from) {
3990 case ENTRY_DIRECTION_RIGHT:
3991 cur.posVisToRowExtremity(false /* !left */);
3993 case ENTRY_DIRECTION_LEFT:
3994 cur.posVisToRowExtremity(true /* left */);
3996 case ENTRY_DIRECTION_IGNORE:
3997 // nothing to do in this case
4002 // FIXME: this accesses the position cache before it is initialized
4007 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4009 Tabular::Feature action = Tabular::LAST_ACTION;
4012 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4013 string const tmp = tabularFeature[i].feature;
4015 if (tmp == what.substr(0, tmp.length())) {
4016 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4017 //tabularFeatures[i].feature.length()))
4018 action = tabularFeature[i].action;
4022 if (action == Tabular::LAST_ACTION)
4026 ltrim(what.substr(tabularFeature[i].feature.length()));
4027 tabularFeatures(cur, action, val);
4032 static void checkLongtableSpecial(Tabular::ltType & ltt,
4033 string const & special, bool & flag)
4035 if (special == "dl_above") {
4038 } else if (special == "dl_below") {
4039 ltt.bottomDL = flag;
4041 } else if (special == "empty") {
4050 bool InsetTabular::oneCellHasRotationState(bool rotated,
4051 row_type row_start, row_type row_end,
4052 col_type col_start, col_type col_end) const {
4054 for (row_type i = row_start; i <= row_end; ++i) {
4055 for (col_type j = col_start; j <= col_end; ++j) {
4056 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4065 void InsetTabular::tabularFeatures(Cursor & cur,
4066 Tabular::Feature feature, string const & value)
4068 col_type sel_col_start;
4069 col_type sel_col_end;
4070 row_type sel_row_start;
4071 row_type sel_row_end;
4072 bool setLines = false;
4073 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4074 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4078 case Tabular::M_ALIGN_LEFT:
4079 case Tabular::ALIGN_LEFT:
4080 setAlign = LYX_ALIGN_LEFT;
4083 case Tabular::M_ALIGN_RIGHT:
4084 case Tabular::ALIGN_RIGHT:
4085 setAlign = LYX_ALIGN_RIGHT;
4088 case Tabular::M_ALIGN_CENTER:
4089 case Tabular::ALIGN_CENTER:
4090 setAlign = LYX_ALIGN_CENTER;
4093 case Tabular::ALIGN_BLOCK:
4094 setAlign = LYX_ALIGN_BLOCK;
4097 case Tabular::M_VALIGN_TOP:
4098 case Tabular::VALIGN_TOP:
4099 setVAlign = Tabular::LYX_VALIGN_TOP;
4102 case Tabular::M_VALIGN_BOTTOM:
4103 case Tabular::VALIGN_BOTTOM:
4104 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4107 case Tabular::M_VALIGN_MIDDLE:
4108 case Tabular::VALIGN_MIDDLE:
4109 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4116 cur.recordUndoInset(ATOMIC_UNDO);
4118 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4119 row_type const row = tabular.cellRow(cur.idx());
4120 col_type const column = tabular.cellColumn(cur.idx());
4122 Tabular::ltType ltt;
4126 case Tabular::SET_PWIDTH: {
4127 Length const len(value);
4128 tabular.setColumnPWidth(cur, cur.idx(), len);
4130 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4131 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4135 case Tabular::SET_MPWIDTH:
4136 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4139 case Tabular::SET_SPECIAL_COLUMN:
4140 case Tabular::SET_SPECIAL_MULTI:
4141 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4144 case Tabular::APPEND_ROW:
4145 // append the row into the tabular
4146 tabular.appendRow(cur.idx());
4149 case Tabular::APPEND_COLUMN:
4150 // append the column into the tabular
4151 tabular.appendColumn(cur.idx());
4152 cur.idx() = tabular.cellIndex(row, column);
4155 case Tabular::DELETE_ROW:
4156 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4157 tabular.deleteRow(sel_row_start);
4158 if (sel_row_start >= tabular.row_info.size())
4160 cur.idx() = tabular.cellIndex(sel_row_start, column);
4163 cur.selection() = false;
4166 case Tabular::DELETE_COLUMN:
4167 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4168 tabular.deleteColumn(sel_col_start);
4169 if (sel_col_start >= tabular.column_info.size())
4171 cur.idx() = tabular.cellIndex(row, sel_col_start);
4174 cur.selection() = false;
4177 case Tabular::COPY_ROW:
4178 tabular.copyRow(row);
4181 case Tabular::COPY_COLUMN:
4182 tabular.copyColumn(column);
4183 cur.idx() = tabular.cellIndex(row, column);
4186 case Tabular::TOGGLE_LINE_TOP: {
4187 bool lineSet = !tabular.topLine(cur.idx());
4188 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4189 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4190 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4194 case Tabular::TOGGLE_LINE_BOTTOM: {
4195 bool lineSet = !tabular.bottomLine(cur.idx());
4196 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4197 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4198 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4202 case Tabular::TOGGLE_LINE_LEFT: {
4203 bool lineSet = !tabular.leftLine(cur.idx());
4204 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4205 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4206 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4210 case Tabular::TOGGLE_LINE_RIGHT: {
4211 bool lineSet = !tabular.rightLine(cur.idx());
4212 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4213 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4214 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4218 case Tabular::M_ALIGN_LEFT:
4219 case Tabular::M_ALIGN_RIGHT:
4220 case Tabular::M_ALIGN_CENTER:
4222 case Tabular::ALIGN_LEFT:
4223 case Tabular::ALIGN_RIGHT:
4224 case Tabular::ALIGN_CENTER:
4225 case Tabular::ALIGN_BLOCK:
4226 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4227 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4228 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4231 case Tabular::M_VALIGN_TOP:
4232 case Tabular::M_VALIGN_BOTTOM:
4233 case Tabular::M_VALIGN_MIDDLE:
4235 case Tabular::VALIGN_TOP:
4236 case Tabular::VALIGN_BOTTOM:
4237 case Tabular::VALIGN_MIDDLE:
4238 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4239 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4240 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4243 case Tabular::MULTICOLUMN: {
4244 if (!cur.selection()) {
4245 // just multicol for one single cell
4246 // check whether we are completely in a multicol
4247 if (tabular.isMultiColumn(cur.idx()))
4248 tabular.unsetMultiColumn(cur.idx());
4250 tabular.setMultiColumn(cur.idx(), 1);
4253 // we have a selection so this means we just add all this
4254 // cells to form a multicolumn cell
4255 idx_type const s_start = cur.selBegin().idx();
4256 idx_type const s_end = cur.selEnd().idx();
4257 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4258 cur.idx() = s_start;
4261 cur.selection() = false;
4265 case Tabular::SET_ALL_LINES:
4267 case Tabular::UNSET_ALL_LINES:
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.setAllLines(
4271 tabular.cellIndex(i,j), setLines);
4274 case Tabular::SET_BORDER_LINES:
4275 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4276 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4277 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4279 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4280 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4281 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4285 case Tabular::SET_LONGTABULAR:
4286 tabular.is_long_tabular = true;
4289 case Tabular::UNSET_LONGTABULAR:
4290 tabular.is_long_tabular = false;
4293 case Tabular::SET_ROTATE_TABULAR:
4294 tabular.rotate = true;
4297 case Tabular::UNSET_ROTATE_TABULAR:
4298 tabular.rotate = false;
4301 case Tabular::TOGGLE_ROTATE_TABULAR:
4302 tabular.rotate = !tabular.rotate;
4305 case Tabular::SET_ROTATE_CELL:
4306 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4307 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4308 tabular.setRotateCell(
4309 tabular.cellIndex(i, j), true);
4312 case Tabular::UNSET_ROTATE_CELL:
4313 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4314 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4315 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4318 case Tabular::TOGGLE_ROTATE_CELL:
4320 bool oneNotRotated = oneCellHasRotationState(false,
4321 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4323 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4324 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4325 tabular.setRotateCell(tabular.cellIndex(i, j),
4330 case Tabular::SET_USEBOX: {
4331 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4332 if (val == tabular.getUsebox(cur.idx()))
4333 val = Tabular::BOX_NONE;
4334 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4335 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4336 tabular.setUsebox(tabular.cellIndex(i, j), val);
4340 case Tabular::UNSET_LTFIRSTHEAD:
4342 case Tabular::SET_LTFIRSTHEAD:
4343 tabular.getRowOfLTFirstHead(row, ltt);
4344 checkLongtableSpecial(ltt, value, flag);
4345 tabular.setLTHead(row, flag, ltt, true);
4348 case Tabular::UNSET_LTHEAD:
4350 case Tabular::SET_LTHEAD:
4351 tabular.getRowOfLTHead(row, ltt);
4352 checkLongtableSpecial(ltt, value, flag);
4353 tabular.setLTHead(row, flag, ltt, false);
4356 case Tabular::UNSET_LTFOOT:
4358 case Tabular::SET_LTFOOT:
4359 tabular.getRowOfLTFoot(row, ltt);
4360 checkLongtableSpecial(ltt, value, flag);
4361 tabular.setLTFoot(row, flag, ltt, false);
4364 case Tabular::UNSET_LTLASTFOOT:
4366 case Tabular::SET_LTLASTFOOT:
4367 tabular.getRowOfLTLastFoot(row, ltt);
4368 checkLongtableSpecial(ltt, value, flag);
4369 tabular.setLTFoot(row, flag, ltt, true);
4372 case Tabular::SET_LTNEWPAGE:
4373 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4376 case Tabular::SET_BOOKTABS:
4377 tabular.use_booktabs = true;
4380 case Tabular::UNSET_BOOKTABS:
4381 tabular.use_booktabs = false;
4384 case Tabular::SET_TOP_SPACE: {
4386 if (value == "default")
4387 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4388 tabular.row_info[i].top_space_default = true;
4389 else if (isValidLength(value, &len))
4390 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4391 tabular.row_info[i].top_space_default = false;
4392 tabular.row_info[i].top_space = len;
4395 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4396 tabular.row_info[i].top_space_default = false;
4397 tabular.row_info[i].top_space = len;
4402 case Tabular::SET_BOTTOM_SPACE: {
4404 if (value == "default")
4405 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4406 tabular.row_info[i].bottom_space_default = true;
4407 else if (isValidLength(value, &len))
4408 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4409 tabular.row_info[i].bottom_space_default = false;
4410 tabular.row_info[i].bottom_space = len;
4413 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4414 tabular.row_info[i].bottom_space_default = false;
4415 tabular.row_info[i].bottom_space = len;
4420 case Tabular::SET_INTERLINE_SPACE: {
4422 if (value == "default")
4423 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4424 tabular.row_info[i].interline_space_default = true;
4425 else if (isValidLength(value, &len))
4426 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4427 tabular.row_info[i].interline_space_default = false;
4428 tabular.row_info[i].interline_space = len;
4431 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4432 tabular.row_info[i].interline_space_default = false;
4433 tabular.row_info[i].interline_space = len;
4438 // dummy stuff just to avoid warnings
4439 case Tabular::LAST_ACTION:
4445 bool InsetTabular::showInsetDialog(BufferView * bv) const
4447 bv->showDialog("tabular", params2string(*this),
4448 const_cast<InsetTabular *>(this));
4453 void InsetTabular::openLayoutDialog(BufferView * bv) const
4455 bv->showDialog("tabular", params2string(*this),
4456 const_cast<InsetTabular *>(this));
4460 bool InsetTabular::copySelection(Cursor & cur)
4462 if (!cur.selection())
4467 getSelection(cur, rs, re, cs, ce);
4469 paste_tabular.reset(new Tabular(tabular));
4471 for (row_type i = 0; i < rs; ++i)
4472 paste_tabular->deleteRow(0);
4474 row_type const rows = re - rs + 1;
4475 while (paste_tabular->row_info.size() > rows)
4476 paste_tabular->deleteRow(rows);
4478 for (col_type i = 0; i < cs; ++i)
4479 paste_tabular->deleteColumn(0);
4481 col_type const columns = ce - cs + 1;
4482 while (paste_tabular->column_info.size() > columns)
4483 paste_tabular->deleteColumn(columns);
4485 // We clear all the InsetTableCell pointers, since they
4486 // might now become invalid and there is no point in having
4487 // them point to temporary things in paste_tabular.
4488 for (row_type i = 0; i < paste_tabular->row_info.size(); ++i)
4489 for (col_type j = 0; j < paste_tabular->column_info.size(); ++j) {
4490 paste_tabular->cellInset(i,j)->setCellData(0);
4491 paste_tabular->cellInset(i,j)->setTabular(0);
4494 odocstringstream os;
4495 OutputParams const runparams(0);
4496 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4497 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4498 cap::copySelection(cur, os.str());
4500 // mark tabular stack dirty
4501 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4502 // when we (hopefully) have a one-for-all paste mechanism.
4503 // This must be called after cap::copySelection.
4504 dirtyTabularStack(true);
4510 bool InsetTabular::pasteClipboard(Cursor & cur)
4514 col_type const actcol = tabular.cellColumn(cur.idx());
4515 row_type const actrow = tabular.cellRow(cur.idx());
4516 for (row_type r1 = 0, r2 = actrow;
4517 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4519 for (col_type c1 = 0, c2 = actcol;
4520 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4522 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4523 tabular.isPartOfMultiColumn(r2, c2))
4525 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4529 if (tabular.isPartOfMultiColumn(r2, c2)) {
4533 shared_ptr<InsetTableCell> inset(
4534 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4535 // note that setCellInset will call InsetTableCell::setCellData()
4536 // and InsetTableCell::setTabular()
4537 tabular.setCellInset(r2, c2, inset);
4538 // FIXME: change tracking (MG)
4539 inset->setChange(Change(cur.buffer().params().trackChanges ?
4540 Change::INSERTED : Change::UNCHANGED));
4548 void InsetTabular::cutSelection(Cursor & cur)
4550 if (!cur.selection())
4555 getSelection(cur, rs, re, cs, ce);
4556 for (row_type i = rs; i <= re; ++i) {
4557 for (col_type j = cs; j <= ce; ++j) {
4558 shared_ptr<InsetTableCell> t
4559 = cell(tabular.cellIndex(i, j));
4560 if (cur.buffer().params().trackChanges)
4561 // FIXME: Change tracking (MG)
4562 t->setChange(Change(Change::DELETED));
4568 // cursor position might be invalid now
4569 if (cur.pit() > cur.lastpit())
4570 cur.pit() = cur.lastpit();
4571 if (cur.pos() > cur.lastpos())
4572 cur.pos() = cur.lastpos();
4573 cur.clearSelection();
4577 bool InsetTabular::isRightToLeft(Cursor & cur) const
4579 LASSERT(cur.depth() > 1, /**/);
4580 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4581 pos_type const parentpos = cur[cur.depth() - 2].pos();
4582 return parentpar.getFontSettings(cur.bv().buffer().params(),
4583 parentpos).language()->rightToLeft();
4587 void InsetTabular::getSelection(Cursor & cur,
4588 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4590 CursorSlice const & beg = cur.selBegin();
4591 CursorSlice const & end = cur.selEnd();
4592 cs = tabular.cellColumn(beg.idx());
4593 ce = tabular.cellColumn(end.idx());
4596 cs = tabular.cellColumn(end.idx());
4598 ce = tabular.cellRightColumn(end.idx());
4601 rs = tabular.cellRow(beg.idx());
4602 re = tabular.cellRow(end.idx());
4608 Text * InsetTabular::getText(int idx) const
4610 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4614 void InsetTabular::setChange(Change const & change)
4616 for (idx_type idx = 0; idx < nargs(); ++idx)
4617 cell(idx)->setChange(change);
4621 void InsetTabular::acceptChanges(BufferParams const & bparams)
4623 for (idx_type idx = 0; idx < nargs(); ++idx)
4624 cell(idx)->acceptChanges(bparams);
4628 void InsetTabular::rejectChanges(BufferParams const & bparams)
4630 for (idx_type idx = 0; idx < nargs(); ++idx)
4631 cell(idx)->rejectChanges(bparams);
4635 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4637 return tabular.getPWidth(cell).zero();
4641 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4643 return !tabular.getPWidth(cell).zero();
4647 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4650 if (buf.length() <= 0)
4655 col_type maxCols = 1;
4656 size_t const len = buf.length();
4660 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4668 maxCols = max(cols, maxCols);
4674 maxCols = max(cols, maxCols);
4680 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4681 loctab = paste_tabular.get();
4683 dirtyTabularStack(true);
4686 cell = bv.cursor().idx();
4687 ocol = tabular.cellColumn(cell);
4688 row = tabular.cellRow(cell);
4692 idx_type const cells = loctab->numberofcells;
4695 rows = loctab->row_info.size();
4696 col_type const columns = loctab->column_info.size();
4698 while (cell < cells && p < len && row < rows &&
4699 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4705 // we can only set this if we are not too far right
4706 if (cols < columns) {
4707 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4708 Font const font = bv.textMetrics(&inset->text_).
4710 inset->setText(buf.substr(op, p - op), font,
4711 buffer().params().trackChanges);
4717 // we can only set this if we are not too far right
4718 if (cols < columns) {
4719 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4720 Font const font = bv.textMetrics(&inset->text_).
4722 inset->setText(buf.substr(op, p - op), font,
4723 buffer().params().trackChanges);
4728 cell = loctab->cellIndex(row, cols);
4734 // check for the last cell if there is no trailing '\n'
4735 if (cell < cells && op < len) {
4736 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4737 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4738 inset->setText(buf.substr(op, len - op), font,
4739 buffer().params().trackChanges);
4745 void InsetTabular::addPreview(PreviewLoader & loader) const
4747 row_type const rows = tabular.row_info.size();
4748 col_type const columns = tabular.column_info.size();
4749 for (row_type i = 0; i < rows; ++i) {
4750 for (col_type j = 0; j < columns; ++j)
4751 tabular.cellInset(i, j)->addPreview(loader);
4756 bool InsetTabular::tablemode(Cursor & cur) const
4758 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4762 bool InsetTabular::completionSupported(Cursor const & cur) const
4764 Cursor const & bvCur = cur.bv().cursor();
4765 if (&bvCur.inset() != this)
4767 return cur.text()->completionSupported(cur);
4771 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4773 return completionSupported(cur);
4777 bool InsetTabular::automaticInlineCompletion() const
4779 return lyxrc.completion_inline_text;
4783 bool InsetTabular::automaticPopupCompletion() const
4785 return lyxrc.completion_popup_text;
4789 bool InsetTabular::showCompletionCursor() const
4791 return lyxrc.completion_cursor_text;
4795 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4797 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4801 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4803 if (!completionSupported(cur))
4805 return cur.text()->completionPrefix(cur);
4809 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4811 if (!completionSupported(cur))
4814 return cur.text()->insertCompletion(cur, s, finished);
4818 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4819 Dimension & dim) const
4821 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4822 tm.completionPosAndDim(cur, x, y, dim);
4826 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4828 istringstream data(in);
4830 lex.setStream(data);
4837 if (!lex || token != "tabular") {
4838 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4842 // This is part of the inset proper that is usually swallowed
4843 // by Buffer::readInset
4845 if (!lex || token != "Tabular") {
4846 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4854 string InsetTabular::params2string(InsetTabular const & inset)
4857 data << "tabular" << ' ';
4859 data << "\\end_inset\n";