2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/Application.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #include "frontends/Selection.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/gettext.h"
58 #include "support/lassert.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::TOGGLE_LTCAPTION, "toggle-ltcaption" },
155 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
156 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
157 { Tabular::SET_BOOKTABS, "set-booktabs" },
158 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
159 { Tabular::SET_TOP_SPACE, "set-top-space" },
160 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
161 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
162 { Tabular::SET_BORDER_LINES, "set-border-lines" },
163 { Tabular::LAST_ACTION, "" }
167 class FeatureEqual : public unary_function<TabularFeature, bool> {
169 FeatureEqual(Tabular::Feature feature)
170 : feature_(feature) {}
171 bool operator()(TabularFeature const & tf) const {
172 return tf.action == feature_;
175 Tabular::Feature feature_;
180 string const write_attribute(string const & name, T const & t)
182 string const s = tostr(t);
183 return s.empty() ? s : " " + name + "=\"" + s + "\"";
187 string const write_attribute(string const & name, string const & t)
189 return t.empty() ? t : " " + name + "=\"" + t + "\"";
194 string const write_attribute(string const & name, docstring const & t)
196 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
201 string const write_attribute(string const & name, bool const & b)
203 // we write only true attribute values so we remove a bit of the
204 // file format bloat for tabulars.
205 return b ? write_attribute(name, convert<string>(b)) : string();
210 string const write_attribute(string const & name, int const & i)
212 // we write only true attribute values so we remove a bit of the
213 // file format bloat for tabulars.
214 return i ? write_attribute(name, convert<string>(i)) : string();
219 string const write_attribute(string const & name, Tabular::idx_type const & i)
221 // we write only true attribute values so we remove a bit of the
222 // file format bloat for tabulars.
223 return i ? write_attribute(name, convert<string>(i)) : string();
228 string const write_attribute(string const & name, Length const & value)
230 // we write only the value if we really have one same reson as above.
231 return value.zero() ? string() : write_attribute(name, value.asString());
235 string const tostr(LyXAlignment const & num)
240 case LYX_ALIGN_BLOCK:
244 case LYX_ALIGN_CENTER:
246 case LYX_ALIGN_RIGHT:
248 case LYX_ALIGN_LAYOUT:
250 case LYX_ALIGN_SPECIAL:
257 string const tostr(Tabular::VAlignment const & num)
260 case Tabular::LYX_VALIGN_TOP:
262 case Tabular::LYX_VALIGN_MIDDLE:
264 case Tabular::LYX_VALIGN_BOTTOM:
271 string const tostr(Tabular::BoxType const & num)
274 case Tabular::BOX_NONE:
276 case Tabular::BOX_PARBOX:
278 case Tabular::BOX_MINIPAGE:
285 // I would have liked a fromstr template a lot better. (Lgb)
286 bool string2type(string const str, LyXAlignment & num)
289 num = LYX_ALIGN_NONE;
290 else if (str == "block")
291 num = LYX_ALIGN_BLOCK;
292 else if (str == "left")
293 num = LYX_ALIGN_LEFT;
294 else if (str == "center")
295 num = LYX_ALIGN_CENTER;
296 else if (str == "right")
297 num = LYX_ALIGN_RIGHT;
304 bool string2type(string const str, Tabular::VAlignment & num)
307 num = Tabular::LYX_VALIGN_TOP;
308 else if (str == "middle" )
309 num = Tabular::LYX_VALIGN_MIDDLE;
310 else if (str == "bottom")
311 num = Tabular::LYX_VALIGN_BOTTOM;
318 bool string2type(string const str, Tabular::BoxType & num)
321 num = Tabular::BOX_NONE;
322 else if (str == "parbox")
323 num = Tabular::BOX_PARBOX;
324 else if (str == "minipage")
325 num = Tabular::BOX_MINIPAGE;
332 bool string2type(string const str, bool & num)
336 else if (str == "false")
344 bool getTokenValue(string const & str, char const * token, string & ret)
347 size_t token_length = strlen(token);
348 size_t pos = str.find(token);
350 if (pos == string::npos || pos + token_length + 1 >= str.length()
351 || str[pos + token_length] != '=')
353 pos += token_length + 1;
355 if (ch != '"' && ch != '\'') { // only read till next space
359 while (pos < str.length() - 1 && str[++pos] != ch)
366 bool getTokenValue(string const & str, char const * token, docstring & ret)
369 bool const success = getTokenValue(str, token, tmp);
370 ret = from_utf8(tmp);
375 bool getTokenValue(string const & str, char const * token, int & num)
379 if (!getTokenValue(str, token, tmp))
381 num = convert<int>(tmp);
386 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
389 return getTokenValue(str, token, tmp) && string2type(tmp, num);
393 bool getTokenValue(string const & str, char const * token,
394 Tabular::VAlignment & num)
397 return getTokenValue(str, token, tmp) && string2type(tmp, num);
401 bool getTokenValue(string const & str, char const * token,
402 Tabular::BoxType & num)
405 return getTokenValue(str, token, tmp) && string2type(tmp, num);
409 bool getTokenValue(string const & str, char const * token, bool & flag)
411 // set the flag always to false as this should be the default for bools
412 // not in the file-format.
415 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
419 bool getTokenValue(string const & str, char const * token, Length & len)
421 // set the length to be zero() as default as this it should be if not
422 // in the file format.
425 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
429 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
434 if (!getTokenValue(str, token, tmp))
436 if (tmp == "default") {
440 return isValidLength(tmp, &len);
444 void l_getline(istream & is, string & str)
447 while (str.empty()) {
449 if (!str.empty() && str[str.length() - 1] == '\r')
450 str.erase(str.length() - 1);
457 string const featureAsString(Tabular::Feature feature)
459 TabularFeature * end = tabularFeature +
460 sizeof(tabularFeature) / sizeof(TabularFeature);
461 TabularFeature * it = find_if(tabularFeature, end,
462 FeatureEqual(feature));
463 return (it == end) ? string() : it->feature;
468 /////////////////////////////////////////////////////////////////////
472 /////////////////////////////////////////////////////////////////////
475 Tabular::CellData::CellData(Buffer & buf)
478 multicolumn(Tabular::CELL_NORMAL),
479 alignment(LYX_ALIGN_CENTER),
480 valignment(LYX_VALIGN_TOP),
487 inset(new InsetTableCell(buf))
489 inset->setBuffer(const_cast<Buffer &>(buf));
490 inset->paragraphs().back().setLayout(buf.params().documentClass().plainLayout());
494 Tabular::CellData::CellData(CellData const & cs)
497 multicolumn(cs.multicolumn),
498 alignment(cs.alignment),
499 valignment(cs.valignment),
500 top_line(cs.top_line),
501 bottom_line(cs.bottom_line),
502 left_line(cs.left_line),
503 right_line(cs.right_line),
506 align_special(cs.align_special),
508 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
512 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
518 void Tabular::CellData::swap(CellData & rhs)
520 std::swap(cellno, rhs.cellno);
521 std::swap(width, rhs.width);
522 std::swap(multicolumn, rhs.multicolumn);
523 std::swap(alignment, rhs.alignment);
524 std::swap(valignment, rhs.valignment);
525 std::swap(top_line, rhs.top_line);
526 std::swap(bottom_line, rhs.bottom_line);
527 std::swap(left_line, rhs.left_line);
528 std::swap(right_line, rhs.right_line);
529 std::swap(usebox, rhs.usebox);
530 std::swap(rotate, rhs.rotate);
531 std::swap(align_special, rhs.align_special);
532 p_width.swap(rhs.p_width);
533 inset.swap(rhs.inset);
537 Tabular::RowData::RowData()
540 top_space_default(false),
541 bottom_space_default(false),
542 interline_space_default(false),
552 Tabular::ColumnData::ColumnData()
553 : alignment(LYX_ALIGN_CENTER),
554 valignment(LYX_VALIGN_TOP),
560 Tabular::ltType::ltType()
567 Tabular::Tabular(Buffer & buffer, row_type rows_arg, col_type columns_arg)
569 init(buffer, rows_arg, columns_arg);
573 void Tabular::setBuffer(Buffer & buffer)
576 size_t row_count = row_info.size();
577 size_t column_count = column_info.size();
578 // set silly default lines
579 for (row_type i = 0; i < row_count; ++i)
580 for (col_type j = 0; j < column_count; ++j)
581 cell_info[i][j].inset->setBuffer(*buffer_);
585 // activates all lines and sets all widths to 0
586 void Tabular::init(Buffer & buf, row_type rows_arg,
587 col_type columns_arg)
590 row_info = row_vector(rows_arg);
591 column_info = column_vector(columns_arg);
592 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
593 row_info.reserve(10);
594 column_info.reserve(10);
595 cell_info.reserve(100);
597 is_long_tabular = false;
599 use_booktabs = false;
600 size_t row_count = row_info.size();
601 size_t column_count = column_info.size();
602 // set silly default lines
603 for (row_type i = 0; i < row_count; ++i)
604 for (col_type j = 0; j < column_count; ++j) {
605 cell_info[i][j].inset->setBuffer(*buffer_);
606 cell_info[i][j].top_line = true;
607 cell_info[i][j].left_line = true;
608 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
609 cell_info[i][j].right_line = j == column_count - 1;
614 void Tabular::appendRow(idx_type const cell)
616 BufferParams const & bp = buffer().params();
617 row_type const row = cellRow(cell);
619 row_vector::iterator rit = row_info.begin() + row;
620 row_info.insert(rit, RowData());
621 // now set the values of the row before
622 row_info[row] = row_info[row + 1];
624 row_type const nrows = row_info.size();
625 col_type const ncols = column_info.size();
627 cell_vvector old(nrows - 1);
628 for (row_type i = 0; i < nrows - 1; ++i)
629 swap(cell_info[i], old[i]);
631 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer())));
633 for (row_type i = 0; i <= row; ++i)
634 swap(cell_info[i], old[i]);
635 for (row_type i = row + 2; i < nrows; ++i)
636 swap(cell_info[i], old[i - 1]);
639 for (col_type c = 0; c < ncols; ++c) {
640 // inherit line settings
641 idx_type const i = cellIndex(row + 1, c);
642 idx_type const j = cellIndex(row, c);
643 setLeftLine(i, leftLine(j));
644 setRightLine(i, rightLine(j));
645 setTopLine(i, topLine(j));
646 if (topLine(j) && bottomLine(j)) {
647 setBottomLine(i, true);
648 setBottomLine(j, false);
650 // mark track changes
652 cellInfo(i).inset->setChange(Change(Change::INSERTED));
657 void Tabular::deleteRow(row_type const row)
659 // Not allowed to delete last row
660 if (row_info.size() == 1)
663 row_info.erase(row_info.begin() + row);
664 cell_info.erase(cell_info.begin() + row);
669 void Tabular::copyRow(row_type const row)
671 row_info.insert(row_info.begin() + row, row_info[row]);
672 cell_info.insert(cell_info.begin() + row, cell_info[row]);
674 if (buffer().params().trackChanges)
675 for (col_type j = 0; j < column_info.size(); ++j)
676 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
682 void Tabular::appendColumn(idx_type const cell)
684 col_type const c = cellColumn(cell);
685 column_vector::iterator cit = column_info.begin() + c + 1;
686 column_info.insert(cit, ColumnData());
687 row_type const nrows = row_info.size();
688 // set the column values of the column before
689 column_info[c + 1] = column_info[c];
691 for (row_type r = 0; r < nrows; ++r) {
692 cell_info[r].insert(cell_info[r].begin() + c + 1,
694 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
695 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
697 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
700 for (row_type r = 0; r < nrows; ++r) {
701 // inherit line settings
702 idx_type const i = cellIndex(r, c + 1);
703 idx_type const j = cellIndex(r, c);
704 setBottomLine(i, bottomLine(j));
705 setTopLine(i, topLine(j));
706 setLeftLine(i, leftLine(j));
707 if (rightLine(j) && rightLine(j)) {
708 setRightLine(i, true);
709 setRightLine(j, false);
712 cellInfo(i).inset->clear();
713 if (buffer().params().trackChanges)
714 cellInfo(i).inset->setChange(Change(Change::INSERTED));
719 void Tabular::deleteColumn(col_type const column)
721 // Not allowed to delete last column
722 if (column_info.size() == 1)
725 column_info.erase(column_info.begin() + column);
726 size_t row_count = row_info.size();
727 for (row_type i = 0; i < row_count; ++i) {
728 // Care about multicolumn cells
729 if (column + 1 < column_info.size() &&
730 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
731 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
732 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
734 cell_info[i].erase(cell_info[i].begin() + column);
740 void Tabular::copyColumn(col_type const column)
742 BufferParams const & bp = buffer().params();
743 column_info.insert(column_info.begin() + column, column_info[column]);
745 size_t row_count = row_info.size();
746 for (row_type i = 0; i < row_count; ++i)
747 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
750 for (row_type i = 0; i < row_count; ++i)
751 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
756 void Tabular::updateIndexes()
758 col_type ncols = column_info.size();
759 row_type nrows = row_info.size();
761 for (row_type row = 0; row < nrows; ++row)
762 for (col_type column = 0; column < ncols; ++column) {
763 if (!isPartOfMultiColumn(row, column))
765 cell_info[row][column].cellno = numberofcells - 1;
768 rowofcell.resize(numberofcells);
769 columnofcell.resize(numberofcells);
771 for (row_type row = 0; row < nrows; ++row)
772 for (col_type column = 0; column < ncols; ++column) {
773 if (isPartOfMultiColumn(row, column))
776 columnofcell[i] = column;
777 setFixedWidth(row, column);
783 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
785 row_type const row = cellRow(cell);
787 for (col_type i = 0; i < column_info.size(); ++i)
788 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
794 bool Tabular::topLine(idx_type const cell) const
796 return cellInfo(cell).top_line;
800 bool Tabular::bottomLine(idx_type const cell) const
802 return cellInfo(cell).bottom_line;
806 bool Tabular::leftLine(idx_type cell) const
810 return cellInfo(cell).left_line;
814 bool Tabular::rightLine(idx_type cell) const
818 return cellInfo(cell).right_line;
822 bool Tabular::topAlreadyDrawn(idx_type cell) const
824 row_type row = cellRow(cell);
827 idx_type i = cellIndex(row - 1, cellColumn(cell));
828 return !rowTopLine(row) && bottomLine(i);
832 bool Tabular::leftAlreadyDrawn(idx_type cell) const
834 col_type col = cellColumn(cell);
837 idx_type i = cellIndex(cellRow(cell), col - 1);
838 return rightLine(i) && !leftLine(cell);
842 bool Tabular::isLastRow(idx_type cell) const
844 return cellRow(cell) == row_info.size() - 1;
848 int Tabular::getAdditionalHeight(row_type row) const
850 if (!row || row >= row_info.size())
853 int const interline_space = row_info[row - 1].interline_space_default ?
855 row_info[row - 1].interline_space.inPixels(width());
856 if (rowTopLine(row) && rowBottomLine(row - 1))
857 return interline_space + WIDTH_OF_LINE;
858 return interline_space;
862 int Tabular::getAdditionalWidth(idx_type cell) const
864 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
866 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
867 return WIDTH_OF_LINE;
872 int Tabular::columnWidth(idx_type cell) const
875 col_type const span = columnSpan(cell);
876 col_type const col = cellColumn(cell);
877 for(col_type c = col; c < col + span ; ++c)
878 w += column_info[c].width;
883 bool Tabular::updateColumnWidths()
885 col_type const ncols = column_info.size();
886 row_type const nrows = row_info.size();
888 // for each col get max of single col cells
889 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 if (columnSpan(i) == 1)
894 new_width = max(new_width, cellInfo(i).width);
897 if (column_info[c].width != new_width) {
898 column_info[c].width = new_width;
902 // update col widths to fit merged cells
903 for(col_type c = 0; c < ncols; ++c)
904 for(row_type r = 0; r < nrows; ++r) {
905 idx_type const i = cellIndex(r, c);
906 int const span = columnSpan(i);
907 if (span == 1 || c > cellColumn(i))
911 for(col_type j = c; j < c + span ; ++j)
912 old_width += column_info[j].width;
914 if (cellInfo(i).width > old_width) {
915 column_info[c + span - 1].width += cellInfo(i).width - old_width;
924 int Tabular::width() const
926 col_type const ncols = column_info.size();
928 for (col_type i = 0; i < ncols; ++i)
929 width += column_info[i].width;
934 void Tabular::setCellWidth(idx_type cell, int new_width)
936 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
937 + getAdditionalWidth(cell);
941 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
944 if (!isMultiColumn(cell) || onlycolumn)
945 column_info[cellColumn(cell)].alignment = align;
947 cellInfo(cell).alignment = align;
951 void Tabular::setVAlignment(idx_type cell, VAlignment align,
954 if (!isMultiColumn(cell) || onlycolumn)
955 column_info[cellColumn(cell)].valignment = align;
957 cellInfo(cell).valignment = align;
964 * Allow line and paragraph breaks for fixed width cells or disallow them,
965 * merge cell paragraphs and reset layout to standard for variable width
968 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
970 inset->setAutoBreakRows(fixedWidth);
971 inset->toggleFixedWidth(fixedWidth);
975 // merge all paragraphs to one
976 BufferParams const & bp = cur.bv().buffer().params();
977 while (inset->paragraphs().size() > 1)
978 mergeParagraph(bp, inset->paragraphs(), 0);
982 // undo information has already been recorded
983 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
984 bp.documentClass().plainLayoutName());
991 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
992 Length const & width)
994 col_type const j = cellColumn(cell);
996 column_info[j].p_width = width;
997 for (row_type i = 0; i < row_info.size(); ++i) {
998 idx_type const cell = cellIndex(i, j);
999 // because of multicolumns
1000 toggleFixedWidth(cur, cellInset(cell).get(),
1001 !getPWidth(cell).zero());
1003 // cur paragraph can become invalid after paragraphs were merged
1004 if (cur.pit() > cur.lastpit())
1005 cur.pit() = cur.lastpit();
1006 // cur position can become invalid after newlines were removed
1007 if (cur.pos() > cur.lastpos())
1008 cur.pos() = cur.lastpos();
1012 bool Tabular::setFixedWidth(row_type r, col_type c)
1014 if (!column_info[c].p_width.zero() ||
1015 (cell_info[r][c].multicolumn != CELL_NORMAL &&
1016 !cell_info[r][c].p_width.zero())) {
1017 cell_info[r][c].inset->toggleFixedWidth(true);
1024 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1025 Length const & width)
1027 if (!isMultiColumn(cell))
1030 cellInfo(cell).p_width = width;
1031 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1032 // cur paragraph can become invalid after paragraphs were merged
1033 if (cur.pit() > cur.lastpit())
1034 cur.pit() = cur.lastpit();
1035 // cur position can become invalid after newlines were removed
1036 if (cur.pos() > cur.lastpos())
1037 cur.pos() = cur.lastpos();
1042 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1043 Tabular::Feature what)
1045 if (what == SET_SPECIAL_MULTI)
1046 cellInfo(cell).align_special = special;
1048 column_info[cellColumn(cell)].align_special = special;
1052 void Tabular::setAllLines(idx_type cell, bool line)
1054 setTopLine(cell, line);
1055 setBottomLine(cell, line);
1056 setRightLine(cell, line);
1057 setLeftLine(cell, line);
1061 void Tabular::setTopLine(idx_type i, bool line)
1063 cellInfo(i).top_line = line;
1067 void Tabular::setBottomLine(idx_type i, bool line)
1069 cellInfo(i).bottom_line = line;
1073 void Tabular::setLeftLine(idx_type cell, bool line)
1075 cellInfo(cell).left_line = line;
1079 void Tabular::setRightLine(idx_type cell, bool line)
1081 cellInfo(cell).right_line = line;
1084 bool Tabular::rowTopLine(row_type r) const
1086 idx_type i0 = getFirstCellInRow(r);
1087 idx_type i1 = getLastCellInRow(r);
1088 bool all_rows_set = true;
1089 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1090 all_rows_set = cellInfo(j).top_line;
1091 return all_rows_set;
1095 bool Tabular::rowBottomLine(row_type r) const
1097 idx_type i0 = getFirstCellInRow(r);
1098 idx_type i1 = getLastCellInRow(r);
1099 bool all_rows_set = true;
1100 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1101 all_rows_set = cellInfo(j).bottom_line;
1102 return all_rows_set;
1106 bool Tabular::columnLeftLine(col_type c) const
1113 row_type const nrows = row_info.size();
1114 for (row_type r = 0; r < nrows; ++r) {
1115 idx_type i = cellIndex(r, c);
1116 if (c == cellColumn(i)) {
1118 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1119 if (cellInfo(i).left_line || right)
1123 return 2 * nrows_left >= total;
1127 bool Tabular::columnRightLine(col_type c) const
1132 int nrows_right = 0;
1134 row_type const nrows = row_info.size();
1135 for (row_type r = 0; r < nrows; ++r) {
1136 idx_type i = cellIndex(r, c);
1137 if (c == cellColumn(i) + columnSpan(i) - 1) {
1139 bool left = c + 1 < column_info.size()
1140 && cellInfo(cellIndex(r, c + 1)).left_line
1141 || c + 1 == column_info.size();
1142 if (cellInfo(i).right_line && left)
1146 return 2 * nrows_right >= total;
1150 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1152 if (!onlycolumn && isMultiColumn(cell))
1153 return cellInfo(cell).alignment;
1154 return column_info[cellColumn(cell)].alignment;
1159 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1161 if (!onlycolumn && isMultiColumn(cell))
1162 return cellInfo(cell).valignment;
1163 return column_info[cellColumn(cell)].valignment;
1167 Length const Tabular::getPWidth(idx_type cell) const
1169 if (isMultiColumn(cell))
1170 return cellInfo(cell).p_width;
1171 return column_info[cellColumn(cell)].p_width;
1175 int Tabular::cellWidth(idx_type cell) const
1177 return cellInfo(cell).width;
1181 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1185 switch (getAlignment(cell)) {
1186 case LYX_ALIGN_CENTER:
1187 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1189 case LYX_ALIGN_RIGHT:
1190 x += columnWidth(cell) - cellWidth(cell);
1191 // + getAdditionalWidth(cell);
1194 // LYX_ALIGN_LEFT: nothing :-)
1198 // the LaTeX Way :-(
1204 bool Tabular::isFirstCellInRow(idx_type cell) const
1206 return cellColumn(cell) == 0;
1210 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1212 if (row > row_info.size() - 1)
1213 row = row_info.size() - 1;
1214 return cell_info[row][0].cellno;
1218 bool Tabular::isLastCellInRow(idx_type cell) const
1220 return cellRightColumn(cell) == column_info.size() - 1;
1224 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1226 if (row > row_info.size() - 1)
1227 row = row_info.size() - 1;
1228 return cell_info[row][column_info.size() - 1].cellno;
1232 Tabular::row_type Tabular::cellRow(idx_type cell) const
1234 if (cell >= numberofcells)
1235 return row_info.size() - 1;
1238 return rowofcell[cell];
1242 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1244 if (cell >= numberofcells)
1245 return column_info.size() - 1;
1248 return columnofcell[cell];
1252 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1254 row_type const row = cellRow(cell);
1255 col_type column = cellColumn(cell);
1256 while (column < column_info.size() - 1 &&
1257 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1263 void Tabular::write(ostream & os) const
1267 << write_attribute("version", 3)
1268 << write_attribute("rows", row_info.size())
1269 << write_attribute("columns", column_info.size())
1271 // global longtable options
1273 << write_attribute("rotate", rotate)
1274 << write_attribute("booktabs", use_booktabs)
1275 << write_attribute("islongtable", is_long_tabular)
1276 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1277 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1278 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1279 << write_attribute("headTopDL", endhead.topDL)
1280 << write_attribute("headBottomDL", endhead.bottomDL)
1281 << write_attribute("footTopDL", endfoot.topDL)
1282 << write_attribute("footBottomDL", endfoot.bottomDL)
1283 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1284 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1285 << write_attribute("lastFootEmpty", endlastfoot.empty)
1287 for (col_type j = 0; j < column_info.size(); ++j) {
1289 << write_attribute("alignment", column_info[j].alignment)
1290 << write_attribute("valignment", column_info[j].valignment)
1291 << write_attribute("width", column_info[j].p_width.asString())
1292 << write_attribute("special", column_info[j].align_special)
1295 for (row_type i = 0; i < row_info.size(); ++i) {
1296 static const string def("default");
1298 if (row_info[i].top_space_default)
1299 os << write_attribute("topspace", def);
1301 os << write_attribute("topspace", row_info[i].top_space);
1302 if (row_info[i].bottom_space_default)
1303 os << write_attribute("bottomspace", def);
1305 os << write_attribute("bottomspace", row_info[i].bottom_space);
1306 if (row_info[i].interline_space_default)
1307 os << write_attribute("interlinespace", def);
1309 os << write_attribute("interlinespace", row_info[i].interline_space);
1310 os << write_attribute("endhead", row_info[i].endhead)
1311 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1312 << write_attribute("endfoot", row_info[i].endfoot)
1313 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1314 << write_attribute("newpage", row_info[i].newpage)
1315 << write_attribute("caption", row_info[i].caption)
1317 for (col_type j = 0; j < column_info.size(); ++j) {
1319 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1320 << write_attribute("alignment", cell_info[i][j].alignment)
1321 << write_attribute("valignment", cell_info[i][j].valignment)
1322 << write_attribute("topline", cell_info[i][j].top_line)
1323 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1324 << write_attribute("leftline", cell_info[i][j].left_line)
1325 << write_attribute("rightline", cell_info[i][j].right_line)
1326 << write_attribute("rotate", cell_info[i][j].rotate)
1327 << write_attribute("usebox", cell_info[i][j].usebox)
1328 << write_attribute("width", cell_info[i][j].p_width)
1329 << write_attribute("special", cell_info[i][j].align_special)
1331 os << "\\begin_inset ";
1332 cell_info[i][j].inset->write(os);
1333 os << "\n\\end_inset\n"
1338 os << "</lyxtabular>\n";
1342 void Tabular::read(Lexer & lex)
1345 istream & is = lex.getStream();
1347 l_getline(is, line);
1348 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1349 LASSERT(false, /**/);
1354 if (!getTokenValue(line, "version", version))
1356 LASSERT(version >= 2, /**/);
1359 if (!getTokenValue(line, "rows", rows_arg))
1362 if (!getTokenValue(line, "columns", columns_arg))
1364 init(buffer(), rows_arg, columns_arg);
1365 l_getline(is, line);
1366 if (!prefixIs(line, "<features")) {
1367 lyxerr << "Wrong tabular format (expected <features ...> got"
1368 << line << ')' << endl;
1371 getTokenValue(line, "rotate", rotate);
1372 getTokenValue(line, "booktabs", use_booktabs);
1373 getTokenValue(line, "islongtable", is_long_tabular);
1374 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1375 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1376 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1377 getTokenValue(line, "headTopDL", endhead.topDL);
1378 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1379 getTokenValue(line, "footTopDL", endfoot.topDL);
1380 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1381 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1382 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1383 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1385 for (col_type j = 0; j < column_info.size(); ++j) {
1387 if (!prefixIs(line,"<column")) {
1388 lyxerr << "Wrong tabular format (expected <column ...> got"
1389 << line << ')' << endl;
1392 getTokenValue(line, "alignment", column_info[j].alignment);
1393 getTokenValue(line, "valignment", column_info[j].valignment);
1394 getTokenValue(line, "width", column_info[j].p_width);
1395 getTokenValue(line, "special", column_info[j].align_special);
1398 for (row_type i = 0; i < row_info.size(); ++i) {
1399 l_getline(is, line);
1400 if (!prefixIs(line, "<row")) {
1401 lyxerr << "Wrong tabular format (expected <row ...> got"
1402 << line << ')' << endl;
1405 getTokenValue(line, "topspace", row_info[i].top_space,
1406 row_info[i].top_space_default);
1407 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1408 row_info[i].bottom_space_default);
1409 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1410 row_info[i].interline_space_default);
1411 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1412 getTokenValue(line, "endhead", row_info[i].endhead);
1413 getTokenValue(line, "endfoot", row_info[i].endfoot);
1414 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1415 getTokenValue(line, "newpage", row_info[i].newpage);
1416 getTokenValue(line, "caption", row_info[i].caption);
1417 for (col_type j = 0; j < column_info.size(); ++j) {
1418 l_getline(is, line);
1419 if (!prefixIs(line, "<cell")) {
1420 lyxerr << "Wrong tabular format (expected <cell ...> got"
1421 << line << ')' << endl;
1424 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1425 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1426 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1427 getTokenValue(line, "topline", cell_info[i][j].top_line);
1428 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1429 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1430 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1431 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1432 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1433 getTokenValue(line, "width", cell_info[i][j].p_width);
1435 getTokenValue(line, "special", cell_info[i][j].align_special);
1436 l_getline(is, line);
1437 if (prefixIs(line, "\\begin_inset")) {
1438 cell_info[i][j].inset->setBuffer(*buffer_);
1439 cell_info[i][j].inset->read(lex);
1440 l_getline(is, line);
1442 if (!prefixIs(line, "</cell>")) {
1443 lyxerr << "Wrong tabular format (expected </cell> got"
1444 << line << ')' << endl;
1448 l_getline(is, line);
1449 if (!prefixIs(line, "</row>")) {
1450 lyxerr << "Wrong tabular format (expected </row> got"
1451 << line << ')' << endl;
1455 while (!prefixIs(line, "</lyxtabular>")) {
1456 l_getline(is, line);
1462 bool Tabular::isMultiColumn(idx_type cell) const
1464 return cellInfo(cell).multicolumn != CELL_NORMAL;
1468 bool Tabular::isMultiColumnReal(idx_type cell) const
1470 return cellColumn(cell) != cellRightColumn(cell) &&
1471 cellInfo(cell).multicolumn != CELL_NORMAL;
1475 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1477 return cell_info[cellRow(cell)][cellColumn(cell)];
1481 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1483 CellData & cs = cellInfo(cell);
1484 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1485 cs.alignment = column_info[cellColumn(cell)].alignment;
1486 setRightLine(cell, rightLine(cell + number - 1));
1487 for (idx_type i = 1; i < number; ++i) {
1488 CellData & cs1 = cellInfo(cell + i);
1489 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1490 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1497 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1499 row_type const row = cellRow(cell);
1500 col_type const ncols = column_info.size();
1501 idx_type result = 1;
1502 col_type column = cellColumn(cell) + 1;
1503 while (column < ncols && isPartOfMultiColumn(row, column)) {
1511 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1513 row_type const row = cellRow(cell);
1514 col_type column = cellColumn(cell);
1516 idx_type result = 0;
1518 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1519 cell_info[row][column].multicolumn = CELL_NORMAL;
1521 while (column < column_info.size() &&
1522 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1524 cell_info[row][column].multicolumn = CELL_NORMAL;
1534 void Tabular::setRotateCell(idx_type cell, bool flag)
1536 cellInfo(cell).rotate = flag;
1540 bool Tabular::getRotateCell(idx_type cell) const
1542 return cellInfo(cell).rotate;
1546 bool Tabular::needRotating() const
1550 for (row_type i = 0; i < row_info.size(); ++i)
1551 for (col_type j = 0; j < column_info.size(); ++j)
1552 if (cell_info[i][j].rotate)
1558 bool Tabular::isLastCell(idx_type cell) const
1560 if (cell + 1 < numberofcells)
1566 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1568 if (cellRow(cell) > 0)
1569 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1574 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1576 if (cellRow(cell) + 1 < row_info.size())
1577 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1582 Tabular::idx_type Tabular::cellIndex(row_type row,
1583 col_type column) const
1585 BOOST_ASSERT(column != npos && column < column_info.size()
1586 && row != npos && row < row_info.size());
1587 return cell_info[row][column].cellno;
1591 void Tabular::setUsebox(idx_type cell, BoxType type)
1593 cellInfo(cell).usebox = type;
1597 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1598 // adds line breaks, see bug 4886.
1599 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1601 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1602 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1604 if (cellInfo(cell).usebox > 1)
1605 return cellInfo(cell).usebox;
1606 return useParbox(cell);
1611 // This are functions used for the longtable support
1613 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1619 row_info[row].endfirsthead = flag;
1623 row_info[row].endhead = flag;
1628 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1631 hd.set = haveLTHead();
1632 return row_info[row].endhead;
1636 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1639 hd.set = haveLTFirstHead();
1640 return row_info[row].endfirsthead;
1644 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1650 row_info[row].endlastfoot = flag;
1654 row_info[row].endfoot = flag;
1659 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1662 fd.set = haveLTFoot();
1663 return row_info[row].endfoot;
1667 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1670 fd.set = haveLTLastFoot();
1671 return row_info[row].endlastfoot;
1675 void Tabular::setLTNewPage(row_type row, bool what)
1677 row_info[row].newpage = what;
1681 bool Tabular::getLTNewPage(row_type row) const
1683 return row_info[row].newpage;
1687 bool Tabular::haveLTHead() const
1689 for (row_type i = 0; i < row_info.size(); ++i)
1690 if (row_info[i].endhead)
1696 bool Tabular::haveLTFirstHead() const
1698 if (endfirsthead.empty)
1700 for (row_type i = 0; i < row_info.size(); ++i)
1701 if (row_info[i].endfirsthead)
1707 bool Tabular::haveLTFoot() const
1709 for (row_type i = 0; i < row_info.size(); ++i)
1710 if (row_info[i].endfoot)
1716 bool Tabular::haveLTLastFoot() const
1718 if (endlastfoot.empty)
1720 for (row_type i = 0; i < row_info.size(); ++i)
1721 if (row_info[i].endlastfoot)
1727 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1729 idx_type i = getFirstCellInRow(row);
1731 setMultiColumn(i, column_info.size());
1732 setTopLine(i, false);
1733 setBottomLine(i, false);
1734 setLeftLine(i, false);
1735 setRightLine(i, false);
1737 unsetMultiColumn(i);
1738 row_info[row].caption = what;
1743 bool Tabular::ltCaption(row_type row) const
1745 return row_info[row].caption;
1749 // end longtable support functions
1751 void Tabular::setRowAscent(row_type row, int height)
1753 if (row >= row_info.size() || row_info[row].ascent == height)
1755 row_info[row].ascent = height;
1759 void Tabular::setRowDescent(row_type row, int height)
1761 if (row >= row_info.size() || row_info[row].descent == height)
1763 row_info[row].descent = height;
1767 int Tabular::rowAscent(row_type row) const
1769 if (row >= row_info.size())
1771 return row_info[row].ascent;
1775 int Tabular::rowDescent(row_type row) const
1777 LASSERT(row < row_info.size(), /**/);
1778 return row_info[row].descent;
1782 int Tabular::height() const
1785 for (row_type row = 0; row < row_info.size(); ++row)
1786 height += rowAscent(row) + rowDescent(row) +
1787 getAdditionalHeight(row);
1792 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1794 LASSERT(row < row_info.size(), /**/);
1795 LASSERT(column < column_info.size(), /**/);
1796 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1800 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1802 // FIXME: assert or return 0 as in TeXBottomHLine()?
1803 LASSERT(row != npos, /**/);
1804 LASSERT(row < row_info.size(), /**/);
1806 idx_type const fcell = getFirstCellInRow(row);
1807 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1810 for (idx_type i = fcell; i < n; ++i) {
1814 if (use_booktabs && row == 0) {
1817 } else if (tmp == n - fcell) {
1818 os << (use_booktabs ? "\\midrule " : "\\hline ");
1820 for (idx_type i = fcell; i < n; ++i) {
1822 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1823 << cellColumn(i) + 1
1825 << cellRightColumn(i) + 1
1837 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1839 // FIXME: return 0 or assert as in TeXTopHLine()?
1840 if (row == npos || row >= row_info.size())
1843 idx_type const fcell = getFirstCellInRow(row);
1844 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1847 for (idx_type i = fcell; i < n; ++i) {
1851 if (use_booktabs && row == row_info.size() - 1) {
1852 if (bottomLine(fcell))
1853 os << "\\bottomrule";
1854 } else if (tmp == n - fcell) {
1855 os << (use_booktabs ? "\\midrule" : "\\hline");
1857 for (idx_type i = fcell; i < n; ++i) {
1858 if (bottomLine(i)) {
1859 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1860 << cellColumn(i) + 1
1862 << cellRightColumn(i) + 1
1874 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1877 row_type const r = cellRow(cell);
1878 if (is_long_tabular && row_info[r].caption)
1881 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1882 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1883 // figure out how to set the lines
1884 // we always set double lines to the right of the cell
1885 col_type const c = cellColumn(cell);
1886 col_type const nextcol = c + columnSpan(cell);
1887 bool colright = columnRightLine(c);
1888 bool colleft = columnLeftLine(c);
1889 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1890 bool nextcellleft = nextcol < column_info.size()
1891 && leftLine(cellIndex(r, nextcol));
1892 bool coldouble = colright && nextcolleft;
1893 bool celldouble = rightLine(cell) && nextcellleft;
1894 ismulticol = isMultiColumn(cell)
1895 || (c == 0 && colleft != leftLine(cell))
1896 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1897 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1898 || (coldouble != celldouble);
1900 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1901 if (c ==0 && leftLine(cell))
1903 if (!cellInfo(cell).align_special.empty()) {
1904 os << cellInfo(cell).align_special;
1906 if (!getPWidth(cell).zero()) {
1908 case LYX_VALIGN_TOP:
1911 case LYX_VALIGN_MIDDLE:
1914 case LYX_VALIGN_BOTTOM:
1919 << from_ascii(getPWidth(cell).asLatexString())
1923 case LYX_ALIGN_LEFT:
1926 case LYX_ALIGN_RIGHT:
1933 } // end if else !getPWidth
1934 } // end if else !cellinfo_of_cell
1935 if (rightLine(cell) || nextcellleft)
1938 // add extra vertical line if we want a double one
1942 if (getRotateCell(cell)) {
1943 os << "\\begin{sideways}\n";
1946 if (getUsebox(cell) == BOX_PARBOX) {
1949 case LYX_VALIGN_TOP:
1952 case LYX_VALIGN_MIDDLE:
1955 case LYX_VALIGN_BOTTOM:
1959 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1961 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1962 os << "\\begin{minipage}[";
1964 case LYX_VALIGN_TOP:
1967 case LYX_VALIGN_MIDDLE:
1970 case LYX_VALIGN_BOTTOM:
1974 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1982 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1985 row_type const r = cellRow(cell);
1986 if (is_long_tabular && row_info[r].caption)
1990 if (getUsebox(cell) == BOX_PARBOX)
1992 else if (getUsebox(cell) == BOX_MINIPAGE) {
1993 os << "%\n\\end{minipage}";
1996 if (getRotateCell(cell)) {
1997 os << "%\n\\end{sideways}";
2007 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2008 OutputParams const & runparams) const
2010 if (!is_long_tabular)
2014 // output header info
2016 if (endhead.topDL) {
2020 for (row_type i = 0; i < row_info.size(); ++i) {
2021 if (row_info[i].endhead) {
2022 ret += TeXRow(os, i, runparams);
2025 if (endhead.bottomDL) {
2029 os << "\\endhead\n";
2031 if (endfirsthead.empty) {
2032 os << "\\endfirsthead\n";
2036 // output firstheader info
2037 if (haveLTFirstHead()) {
2038 if (endfirsthead.topDL) {
2042 for (row_type i = 0; i < row_info.size(); ++i) {
2043 if (row_info[i].endfirsthead) {
2044 ret += TeXRow(os, i, runparams);
2047 if (endfirsthead.bottomDL) {
2051 os << "\\endfirsthead\n";
2054 // output footer info
2056 if (endfoot.topDL) {
2060 for (row_type i = 0; i < row_info.size(); ++i) {
2061 if (row_info[i].endfoot) {
2062 ret += TeXRow(os, i, runparams);
2065 if (endfoot.bottomDL) {
2069 os << "\\endfoot\n";
2071 if (endlastfoot.empty) {
2072 os << "\\endlastfoot\n";
2076 // output lastfooter info
2077 if (haveLTLastFoot()) {
2078 if (endlastfoot.topDL) {
2082 for (row_type i = 0; i < row_info.size(); ++i) {
2083 if (row_info[i].endlastfoot) {
2084 ret += TeXRow(os, i, runparams);
2087 if (endlastfoot.bottomDL) {
2091 os << "\\endlastfoot\n";
2098 bool Tabular::isValidRow(row_type row) const
2100 if (!is_long_tabular)
2102 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2103 !row_info[row].endfoot && !row_info[row].endlastfoot;
2107 int Tabular::TeXRow(odocstream & os, row_type i,
2108 OutputParams const & runparams) const
2110 idx_type cell = cellIndex(i, 0);
2111 int ret = TeXTopHLine(os, i);
2112 if (row_info[i].top_space_default) {
2114 os << "\\addlinespace\n";
2116 os << "\\noalign{\\vskip\\doublerulesep}\n";
2118 } else if(!row_info[i].top_space.zero()) {
2120 os << "\\addlinespace["
2121 << from_ascii(row_info[i].top_space.asLatexString())
2124 os << "\\noalign{\\vskip"
2125 << from_ascii(row_info[i].top_space.asLatexString())
2130 bool ismulticol = false;
2131 for (col_type j = 0; j < column_info.size(); ++j) {
2132 if (isPartOfMultiColumn(i, j))
2134 ret += TeXCellPreamble(os, cell, ismulticol);
2135 shared_ptr<InsetTableCell> inset = cellInset(cell);
2137 Paragraph const & par = inset->paragraphs().front();
2138 bool rtl = par.isRTL(buffer().params())
2140 && getPWidth(cell).zero();
2143 if (par.getParLanguage(buffer().params())->lang() ==
2146 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2148 // currently, remaning RTL languages are arabic_arabtex and hebrew
2152 ret += inset->latex(os, runparams);
2156 ret += TeXCellPostamble(os, cell, ismulticol);
2157 if (!isLastCellInRow(cell)) { // not last cell in row
2162 os << "\\tabularnewline";
2163 if (row_info[i].bottom_space_default) {
2165 os << "\\addlinespace";
2167 os << "[\\doublerulesep]";
2168 } else if (!row_info[i].bottom_space.zero()) {
2170 os << "\\addlinespace";
2172 << from_ascii(row_info[i].bottom_space.asLatexString())
2177 ret += TeXBottomHLine(os, i);
2178 if (row_info[i].interline_space_default) {
2180 os << "\\addlinespace\n";
2182 os << "\\noalign{\\vskip\\doublerulesep}\n";
2184 } else if (!row_info[i].interline_space.zero()) {
2186 os << "\\addlinespace["
2187 << from_ascii(row_info[i].interline_space.asLatexString())
2190 os << "\\noalign{\\vskip"
2191 << from_ascii(row_info[i].interline_space.asLatexString())
2199 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2203 //+---------------------------------------------------------------------
2204 //+ first the opening preamble +
2205 //+---------------------------------------------------------------------
2208 os << "\\begin{sideways}\n";
2211 if (is_long_tabular)
2212 os << "\\begin{longtable}{";
2214 os << "\\begin{tabular}{";
2216 for (col_type i = 0; i < column_info.size(); ++i) {
2217 if (columnLeftLine(i))
2219 if (!column_info[i].align_special.empty()) {
2220 os << column_info[i].align_special;
2222 if (!column_info[i].p_width.zero()) {
2223 switch (column_info[i].alignment) {
2224 case LYX_ALIGN_LEFT:
2225 os << ">{\\raggedright}";
2227 case LYX_ALIGN_RIGHT:
2228 os << ">{\\raggedleft}";
2230 case LYX_ALIGN_CENTER:
2231 os << ">{\\centering}";
2233 case LYX_ALIGN_NONE:
2234 case LYX_ALIGN_BLOCK:
2235 case LYX_ALIGN_LAYOUT:
2236 case LYX_ALIGN_SPECIAL:
2240 switch (column_info[i].valignment) {
2241 case LYX_VALIGN_TOP:
2244 case LYX_VALIGN_MIDDLE:
2247 case LYX_VALIGN_BOTTOM:
2252 << from_ascii(column_info[i].p_width.asLatexString())
2255 switch (column_info[i].alignment) {
2256 case LYX_ALIGN_LEFT:
2259 case LYX_ALIGN_RIGHT:
2266 } // end if else !column_info[i].p_width
2267 } // end if else !column_info[i].align_special
2268 if (columnRightLine(i))
2274 ret += TeXLongtableHeaderFooter(os, runparams);
2276 //+---------------------------------------------------------------------
2277 //+ the single row and columns (cells) +
2278 //+---------------------------------------------------------------------
2280 for (row_type i = 0; i < row_info.size(); ++i) {
2281 if (isValidRow(i)) {
2282 ret += TeXRow(os, i, runparams);
2283 if (is_long_tabular && row_info[i].newpage) {
2284 os << "\\newpage\n";
2290 //+---------------------------------------------------------------------
2291 //+ the closing of the tabular +
2292 //+---------------------------------------------------------------------
2294 if (is_long_tabular)
2295 os << "\\end{longtable}";
2297 os << "\\end{tabular}";
2299 os << "\n\\end{sideways}";
2307 int Tabular::docbookRow(odocstream & os, row_type row,
2308 OutputParams const & runparams) const
2311 idx_type cell = getFirstCellInRow(row);
2314 for (col_type j = 0; j < column_info.size(); ++j) {
2315 if (isPartOfMultiColumn(row, j))
2318 os << "<entry align=\"";
2319 switch (getAlignment(cell)) {
2320 case LYX_ALIGN_LEFT:
2323 case LYX_ALIGN_RIGHT:
2331 os << "\" valign=\"";
2332 switch (getVAlignment(cell)) {
2333 case LYX_VALIGN_TOP:
2336 case LYX_VALIGN_BOTTOM:
2339 case LYX_VALIGN_MIDDLE:
2344 if (isMultiColumn(cell)) {
2345 os << " namest=\"col" << j << "\" ";
2346 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2350 ret += cellInset(cell)->docbook(os, runparams);
2359 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2363 //+---------------------------------------------------------------------
2364 //+ first the opening preamble +
2365 //+---------------------------------------------------------------------
2367 os << "<tgroup cols=\"" << column_info.size()
2368 << "\" colsep=\"1\" rowsep=\"1\">\n";
2370 for (col_type i = 0; i < column_info.size(); ++i) {
2371 os << "<colspec colname=\"col" << i << "\" align=\"";
2372 switch (column_info[i].alignment) {
2373 case LYX_ALIGN_LEFT:
2376 case LYX_ALIGN_RIGHT:
2384 if (runparams.flavor == OutputParams::XML)
2390 //+---------------------------------------------------------------------
2391 //+ Long Tabular case +
2392 //+---------------------------------------------------------------------
2394 // output header info
2395 if (haveLTHead() || haveLTFirstHead()) {
2398 for (row_type i = 0; i < row_info.size(); ++i) {
2399 if (row_info[i].endhead || row_info[i].endfirsthead) {
2400 ret += docbookRow(os, i, runparams);
2406 // output footer info
2407 if (haveLTFoot() || haveLTLastFoot()) {
2410 for (row_type i = 0; i < row_info.size(); ++i) {
2411 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2412 ret += docbookRow(os, i, runparams);
2419 //+---------------------------------------------------------------------
2420 //+ the single row and columns (cells) +
2421 //+---------------------------------------------------------------------
2425 for (row_type i = 0; i < row_info.size(); ++i) {
2426 if (isValidRow(i)) {
2427 ret += docbookRow(os, i, runparams);
2432 //+---------------------------------------------------------------------
2433 //+ the closing of the tabular +
2434 //+---------------------------------------------------------------------
2443 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2444 vector<unsigned int> const & clen) const
2446 idx_type const fcell = getFirstCellInRow(row);
2447 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2450 for (idx_type i = fcell; i < n; ++i) {
2460 for (idx_type i = fcell; i < n; ++i) {
2471 col_type column = cellColumn(i);
2472 int len = clen[column];
2473 while (column < column_info.size() - 1
2474 && isPartOfMultiColumn(row, ++column))
2475 len += clen[column] + 4;
2476 os << docstring(len, ch);
2491 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2492 vector<unsigned int> const & clen) const
2494 idx_type const fcell = getFirstCellInRow(row);
2495 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2498 for (idx_type i = fcell; i < n; ++i) {
2499 if (bottomLine(i)) {
2508 for (idx_type i = fcell; i < n; ++i) {
2509 if (bottomLine(i)) {
2519 col_type column = cellColumn(i);
2520 int len = clen[column];
2521 while (column < column_info.size() -1
2522 && isPartOfMultiColumn(row, ++column))
2523 len += clen[column] + 4;
2524 os << docstring(len, ch);
2525 if (bottomLine(i)) {
2539 void Tabular::plaintextPrintCell(odocstream & os,
2540 OutputParams const & runparams,
2541 idx_type cell, row_type row, col_type column,
2542 vector<unsigned int> const & clen,
2543 bool onlydata) const
2545 odocstringstream sstr;
2546 cellInset(cell)->plaintext(sstr, runparams);
2558 unsigned int len1 = sstr.str().length();
2559 unsigned int len2 = clen[column];
2560 while (column < column_info.size() -1
2561 && isPartOfMultiColumn(row, ++column))
2562 len2 += clen[column] + 4;
2565 switch (getAlignment(cell)) {
2567 case LYX_ALIGN_LEFT:
2570 case LYX_ALIGN_RIGHT:
2574 case LYX_ALIGN_CENTER:
2580 os << docstring(len1, ' ') << sstr.str()
2581 << docstring(len2, ' ');
2583 if (rightLine(cell))
2590 void Tabular::plaintext(odocstream & os,
2591 OutputParams const & runparams, int const depth,
2592 bool onlydata, char_type delim) const
2594 // first calculate the width of the single columns
2595 vector<unsigned int> clen(column_info.size());
2598 // first all non (real) multicolumn cells!
2599 for (col_type j = 0; j < column_info.size(); ++j) {
2601 for (row_type i = 0; i < row_info.size(); ++i) {
2602 idx_type cell = cellIndex(i, j);
2603 if (isMultiColumnReal(cell))
2605 odocstringstream sstr;
2606 cellInset(cell)->plaintext(sstr, runparams);
2607 if (clen[j] < sstr.str().length())
2608 clen[j] = sstr.str().length();
2611 // then all (real) multicolumn cells!
2612 for (col_type j = 0; j < column_info.size(); ++j) {
2613 for (row_type i = 0; i < row_info.size(); ++i) {
2614 idx_type cell = cellIndex(i, j);
2615 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2617 odocstringstream sstr;
2618 cellInset(cell)->plaintext(sstr, runparams);
2619 int len = int(sstr.str().length());
2620 idx_type const n = columnSpan(cell);
2621 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2623 if (len > int(clen[j + n - 1]))
2624 clen[j + n - 1] = len;
2629 for (row_type i = 0; i < row_info.size(); ++i) {
2630 if (!onlydata && plaintextTopHLine(os, i, clen))
2631 os << docstring(depth * 2, ' ');
2632 for (col_type j = 0; j < column_info.size(); ++j) {
2633 if (isPartOfMultiColumn(i, j))
2635 if (onlydata && j > 0)
2636 // we don't use operator<< for single UCS4 character.
2637 // see explanation in docstream.h
2639 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2644 os << docstring(depth * 2, ' ');
2645 if (plaintextBottomHLine(os, i, clen))
2646 os << docstring(depth * 2, ' ');
2652 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2654 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2658 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2659 col_type column) const
2661 return cell_info[row][column].inset;
2665 void Tabular::setCellInset(row_type row, col_type column,
2666 shared_ptr<InsetTableCell> ins) const
2668 CellData & cd = cell_info[row][column];
2673 void Tabular::validate(LaTeXFeatures & features) const
2675 features.require("NeedTabularnewline");
2677 features.require("booktabs");
2678 if (is_long_tabular)
2679 features.require("longtable");
2681 features.require("rotating");
2682 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2683 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2684 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2685 features.require("array");
2686 cellInset(cell)->validate(features);
2691 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2693 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2694 ParagraphList::const_iterator cit = parlist.begin();
2695 ParagraphList::const_iterator end = parlist.end();
2697 for (; cit != end; ++cit)
2698 for (int i = 0; i < cit->size(); ++i)
2699 if (cit->isNewline(i))
2706 /////////////////////////////////////////////////////////////////////
2710 /////////////////////////////////////////////////////////////////////
2712 InsetTableCell::InsetTableCell(Buffer & buf)
2713 : InsetText(buf), isFixedWidth(false)
2717 bool InsetTableCell::forcePlainLayout(idx_type) const
2719 return !isFixedWidth;
2722 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2724 return isFixedWidth;
2727 int InsetTableCell::latex(odocstream & od, OutputParams const & rp) const
2729 OutputParams newrp = rp;
2730 newrp.inTableCell = true;
2731 return InsetText::latex(od, newrp);
2734 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2735 FuncStatus & status) const
2738 switch (cmd.action) {
2740 enabled = !forcePlainLayout();
2742 case LFUN_LAYOUT_PARAGRAPH:
2743 enabled = allowParagraphCustomization();
2746 return InsetText::getStatus(cur, cmd, status);
2748 status.setEnabled(enabled);
2752 docstring InsetTableCell::asString(bool intoInsets)
2755 if (paragraphs().empty())
2757 ParagraphList::const_iterator it = paragraphs().begin();
2758 ParagraphList::const_iterator en = paragraphs().end();
2760 for (; it != en; ++it) {
2765 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2772 /////////////////////////////////////////////////////////////////////
2776 /////////////////////////////////////////////////////////////////////
2778 InsetTabular::InsetTabular(Buffer & buf, row_type rows,
2780 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2781 rowselect_(false), colselect_(false)
2783 setBuffer(buf); // FIXME: remove later
2787 InsetTabular::InsetTabular(InsetTabular const & tab)
2788 : Inset(tab), tabular(tab.tabular), scx_(0)
2793 InsetTabular::~InsetTabular()
2795 hideDialogs("tabular", this);
2799 void InsetTabular::setBuffer(Buffer & buf)
2801 tabular.setBuffer(buf);
2802 Inset::setBuffer(buf);
2806 bool InsetTabular::insetAllowed(InsetCode code) const
2808 if (code == MATHMACRO_CODE)
2815 void InsetTabular::write(ostream & os) const
2817 os << "Tabular" << endl;
2822 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2824 // FIXME: depending on the selection state, we could offer a different menu.
2825 return from_ascii("context-tabular");
2829 void InsetTabular::read(Lexer & lex)
2831 //bool const old_format = (lex.getString() == "\\LyXTable");
2839 string token = lex.getString();
2840 while (lex && token != "\\end_inset") {
2842 token = lex.getString();
2845 lex.printError("Missing \\end_inset at this point. ");
2849 int InsetTabular::rowFromY(Cursor & cur, int y) const
2851 // top y coordinate of tabular
2852 int h = yo(cur.bv()) - tabular.rowAscent(0);
2853 size_t nrows = tabular.row_info.size();
2855 for (; r < nrows && y > h; ++r) {
2856 h += tabular.rowAscent(r);
2857 h += tabular.rowDescent(r);
2858 h += tabular.getAdditionalHeight(r);
2864 int InsetTabular::columnFromX(Cursor & cur, int x) const
2866 // left x coordinate of tabular
2867 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2868 size_t ncols = tabular.column_info.size();
2870 for (; c < ncols && x > w; ++c)
2871 w += tabular.columnWidth(c);
2876 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2878 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2879 // mi.base.textwidth << "\n";
2882 LASSERT(false, /**/);
2886 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2889 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2890 if (tabular.isPartOfMultiColumn(i, j))
2891 // Multicolumn cell, but not first one
2896 if (tabular.cell_info[i][j].multicolumn ==
2897 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2898 p_width = tabular.cellInfo(cell).p_width;
2900 p_width = tabular.column_info[j].p_width;
2901 if (!p_width.zero())
2902 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2903 tabular.cellInset(cell)->metrics(m, dim);
2904 if (!p_width.zero())
2905 dim.wid = m.base.textwidth;
2906 tabular.setCellWidth(cell, dim.wid);
2907 maxAsc = max(maxAsc, dim.asc);
2908 maxDesc = max(maxDesc, dim.des);
2911 int const top_space = tabular.row_info[i].top_space_default ?
2912 default_line_space :
2913 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2914 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2915 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2916 default_line_space :
2917 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2918 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2920 tabular.updateColumnWidths();
2921 dim.asc = tabular.rowAscent(0);
2922 dim.des = tabular.height() - dim.asc;
2923 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2927 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2929 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2930 BufferView * bv = pi.base.bv;
2932 // FIXME: As the full backrgound is painted in drawSelection(),
2933 // we have no choice but to do a full repaint for the Text cells.
2934 pi.full_repaint = true;
2936 resetPos(bv->cursor());
2939 x += ADD_TO_TABULAR_WIDTH;
2941 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2944 first_visible_cell = Tabular::npos;
2945 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
2947 int const a = tabular.rowAscent(i);
2948 int const d = tabular.rowDescent(i);
2949 idx = tabular.cellIndex(i, 0);
2950 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2951 if (tabular.isPartOfMultiColumn(i, j))
2953 if (first_visible_cell == Tabular::npos)
2954 first_visible_cell = idx;
2956 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2957 // Cache the Inset position.
2958 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2959 if (nx + tabular.columnWidth(idx) < 0
2960 || nx > bv->workWidth()
2962 || y - a > bv->workHeight()) {
2963 pi.pain.setDrawingEnabled(false);
2964 cell(idx)->draw(pi, cx, y);
2965 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2966 pi.pain.setDrawingEnabled(original_drawing_state);
2968 cell(idx)->draw(pi, cx, y);
2969 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2971 nx += tabular.columnWidth(idx);
2975 if (i + 1 < tabular.row_info.size())
2976 y += d + tabular.rowAscent(i + 1) +
2977 tabular.getAdditionalHeight(i + 1);
2982 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2984 Cursor & cur = pi.base.bv->cursor();
2986 x += scx_ + ADD_TO_TABULAR_WIDTH;
2988 // FIXME: it is wrong to completely paint the background
2989 // if we want to do single row painting.
2991 // Paint background of current tabular
2992 int const w = tabular.width();
2993 int const h = tabular.height();
2994 int yy = y - tabular.rowAscent(0);
2995 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2997 if (!cur.selection())
2999 if (&cur.inset() != this)
3005 if (cur.selIsMultiCell()) {
3008 getSelection(cur, rs, re, cs, ce);
3009 y -= tabular.rowAscent(0);
3010 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3011 int const a = tabular.rowAscent(j);
3012 int const h = a + tabular.rowDescent(j);
3014 y += tabular.getAdditionalHeight(j);
3015 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3016 if (tabular.isPartOfMultiColumn(j, i))
3018 idx_type const cell =
3019 tabular.cellIndex(j, i);
3020 int const w = tabular.columnWidth(cell);
3021 if (i >= cs && i <= ce && j >= rs && j <= re)
3022 pi.pain.fillRectangle(xx, y, w, h,
3030 x += cellXPos(cur.idx());
3031 x += tabular.getBeginningOfTextInCell(cur.idx());
3032 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3037 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3038 row_type row, idx_type cell, bool erased) const
3040 int x2 = x + tabular.columnWidth(cell);
3041 bool on_off = false;
3042 ColorCode col = Color_tabularline;
3043 ColorCode onoffcol = Color_tabularonoffline;
3046 col = Color_deletedtext;
3047 onoffcol = Color_deletedtext;
3050 if (!tabular.topAlreadyDrawn(cell)) {
3051 on_off = !tabular.topLine(cell);
3052 pain.line(x, y - tabular.rowAscent(row),
3053 x2, y - tabular.rowAscent(row),
3054 on_off ? onoffcol : col,
3055 on_off ? Painter::line_onoffdash : Painter::line_solid);
3057 on_off = !tabular.bottomLine(cell);
3058 pain.line(x, y + tabular.rowDescent(row),
3059 x2, y + tabular.rowDescent(row),
3060 on_off ? onoffcol : col,
3061 on_off ? Painter::line_onoffdash : Painter::line_solid);
3062 if (!tabular.leftAlreadyDrawn(cell)) {
3063 on_off = !tabular.leftLine(cell);
3064 pain.line(x, y - tabular.rowAscent(row),
3065 x, y + tabular.rowDescent(row),
3066 on_off ? onoffcol : col,
3067 on_off ? Painter::line_onoffdash : Painter::line_solid);
3069 on_off = !tabular.rightLine(cell);
3070 pain.line(x2 - tabular.getAdditionalWidth(cell),
3071 y - tabular.rowAscent(row),
3072 x2 - tabular.getAdditionalWidth(cell),
3073 y + tabular.rowDescent(row),
3074 on_off ? onoffcol : col,
3075 on_off ? Painter::line_onoffdash : Painter::line_solid);
3079 docstring InsetTabular::editMessage() const
3081 return _("Opened table");
3085 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3087 //lyxerr << "InsetTabular::edit: " << this << endl;
3089 cur.selection() = false;
3092 if (isRightToLeft(cur))
3093 cur.idx() = tabular.getLastCellInRow(0);
3099 if (isRightToLeft(cur))
3100 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3102 cur.idx() = tabular.numberofcells - 1;
3104 cur.pos() = cur.lastpos(); // FIXME crude guess
3106 // FIXME: this accesses the position cache before it is initialized
3108 //cur.bv().fitCursor();
3112 void InsetTabular::updateLabels(ParIterator const & it)
3114 // In a longtable, tell captions what the current float is
3115 Counters & cnts = buffer().params().documentClass().counters();
3116 string const saveflt = cnts.current_float();
3117 if (tabular.is_long_tabular)
3118 cnts.current_float("table");
3120 ParIterator it2 = it;
3122 size_t const end = it2.nargs();
3123 for ( ; it2.idx() < end; it2.top().forwardIdx())
3124 lyx::updateLabels(buffer(), it2);
3127 if (tabular.is_long_tabular)
3128 cnts.current_float(saveflt);
3132 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3134 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3135 << "\n cur:" << cur);
3136 CursorSlice sl = cur.top();
3137 Cursor & bvcur = cur.bv().cursor();
3139 switch (cmd.action) {
3141 case LFUN_MOUSE_PRESS: {
3142 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3144 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3145 || cmd.x > xo(cur.bv()) + tabular.width()) {
3146 row_type r = rowFromY(cur, cmd.y);
3147 cur.idx() = tabular.getFirstCellInRow(r);
3150 cur.idx() = tabular.getLastCellInRow(r);
3151 cur.pos() = cur.lastpos();
3152 cur.selection() = true;
3158 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3159 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3160 || cmd.y > y0 + tabular.height()) {
3161 col_type c = columnFromX(cur, cmd.x);
3162 cur.idx() = tabular.cellIndex(0, c);
3165 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3166 cur.pos() = cur.lastpos();
3167 cur.selection() = true;
3172 // do not reset cursor/selection if we have selected
3173 // some cells (bug 2715).
3174 if (cmd.button() == mouse_button::button3
3175 && &bvcur.selBegin().inset() == this
3176 && bvcur.selIsMultiCell())
3179 // Let InsetTableCell do it
3180 cell(cur.idx())->dispatch(cur, cmd);
3183 case LFUN_MOUSE_MOTION:
3184 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3185 if (cmd.button() == mouse_button::button1) {
3186 // only accept motions to places not deeper nested than the real anchor
3187 if (!bvcur.anchor_.hasPart(cur)) {
3191 // select (additional) row
3193 row_type r = rowFromY(cur, cmd.y);
3194 cur.idx() = tabular.getLastCellInRow(r);
3195 // we need to reset the cursor's pit and pos now, as the old ones
3196 // may no longer be valid.
3199 bvcur.setCursor(cur);
3200 bvcur.selection() = true;
3203 // select (additional) column
3205 col_type c = columnFromX(cur, cmd.x);
3206 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3207 // we need to reset the cursor's pit and pos now, as the old ones
3208 // may no longer be valid.
3211 bvcur.setCursor(cur);
3212 bvcur.selection() = true;
3215 // only update if selection changes
3216 if (bvcur.idx() == cur.idx() &&
3217 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3219 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3220 bvcur.setCursor(cur);
3221 bvcur.selection() = true;
3222 // if this is a multicell selection, we just set the cursor to
3223 // the beginning of the cell's text.
3224 if (bvcur.selIsMultiCell()) {
3231 case LFUN_MOUSE_RELEASE:
3236 case LFUN_CELL_BACKWARD:
3238 cur.selection() = false;
3241 case LFUN_CELL_FORWARD:
3243 cur.selection() = false;
3246 case LFUN_CHAR_FORWARD_SELECT:
3247 case LFUN_CHAR_FORWARD:
3248 case LFUN_CHAR_BACKWARD_SELECT:
3249 case LFUN_CHAR_BACKWARD:
3250 case LFUN_CHAR_RIGHT_SELECT:
3251 case LFUN_CHAR_RIGHT:
3252 case LFUN_CHAR_LEFT_SELECT:
3253 case LFUN_CHAR_LEFT: {
3254 // determine whether we move to next or previous cell, where to enter
3255 // the new cell from, and which command to "finish" (i.e., exit the
3258 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3259 FuncCode finish_lfun;
3261 if (cmd.action == LFUN_CHAR_FORWARD
3262 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3264 finish_lfun = LFUN_FINISHED_FORWARD;
3266 else if (cmd.action == LFUN_CHAR_BACKWARD
3267 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3269 finish_lfun = LFUN_FINISHED_BACKWARD;
3271 // LEFT or RIGHT commands --- the interpretation will depend on the
3272 // table's direction.
3274 bool right = (cmd.action == LFUN_CHAR_RIGHT
3275 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3276 next_cell = (isRightToLeft(cur) != right);
3278 if (lyxrc.visual_cursor) {
3279 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3283 finish_lfun = LFUN_FINISHED_RIGHT;
3285 finish_lfun = LFUN_FINISHED_LEFT;
3288 // if we don't have a multicell selection...
3289 if (!cur.selIsMultiCell() ||
3290 // ...or we're not doing some LFUN_*_SELECT thing, anyway...
3291 (cmd.action != LFUN_CHAR_FORWARD_SELECT &&
3292 cmd.action != LFUN_CHAR_BACKWARD_SELECT &&
3293 cmd.action != LFUN_CHAR_RIGHT_SELECT &&
3294 cmd.action != LFUN_CHAR_LEFT_SELECT)) {
3295 // ...try to dispatch to the cell's inset.
3296 cell(cur.idx())->dispatch(cur, cmd);
3297 if (cur.result().dispatched())
3300 // move to next/prev cell, as appropriate
3301 // note that we will always do this if we're selecting and we have
3302 // a multicell selection
3303 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3304 << " cell from: " << int(entry_from));
3306 moveNextCell(cur, entry_from);
3308 movePrevCell(cur, entry_from);
3309 // if we're exiting the table, call the appropriate FINISHED lfun
3310 if (sl == cur.top())
3311 cmd = FuncRequest(finish_lfun);
3318 case LFUN_DOWN_SELECT:
3320 cell(cur.idx())->dispatch(cur, cmd);
3321 cur.dispatched(); // override the cell's decision
3322 if (sl == cur.top())
3323 // if our Text didn't do anything to the cursor
3324 // then we try to put the cursor into the cell below
3325 // setting also the right targetX.
3326 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3327 cur.idx() = tabular.cellBelow(cur.idx());
3329 TextMetrics const & tm =
3330 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3331 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3333 if (sl == cur.top()) {
3334 // we trick it to go to forward after leaving the
3336 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3341 case LFUN_UP_SELECT:
3343 cell(cur.idx())->dispatch(cur, cmd);
3344 cur.dispatched(); // override the cell's decision
3345 if (sl == cur.top())
3346 // if our Text didn't do anything to the cursor
3347 // then we try to put the cursor into the cell above
3348 // setting also the right targetX.
3349 if (tabular.cellRow(cur.idx()) != 0) {
3350 cur.idx() = tabular.cellAbove(cur.idx());
3351 cur.pit() = cur.lastpit();
3352 Text const * text = cell(cur.idx())->getText(0);
3353 TextMetrics const & tm = cur.bv().textMetrics(text);
3354 ParagraphMetrics const & pm =
3355 tm.parMetrics(cur.lastpit());
3356 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3358 if (sl == cur.top()) {
3359 cmd = FuncRequest(LFUN_UP);
3364 // case LFUN_SCREEN_DOWN: {
3365 // //if (hasSelection())
3366 // // cur.selection() = false;
3367 // col_type const col = tabular.cellColumn(cur.idx());
3368 // int const t = cur.bv().top_y() + cur.bv().height();
3369 // if (t < yo() + tabular.getHeightOfTabular()) {
3370 // cur.bv().scrollDocView(t);
3371 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3373 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3380 // case LFUN_SCREEN_UP: {
3381 // //if (hasSelection())
3382 // // cur.selection() = false;
3383 // col_type const col = tabular.cellColumn(cur.idx());
3384 // int const t = cur.bv().top_y() + cur.bv().height();
3386 // cur.bv().scrollDocView(t);
3390 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3394 // cur.par() = cur.lastpar();
3395 // cur.pos() = cur.lastpos();
3399 case LFUN_LAYOUT_TABULAR:
3400 cur.bv().showDialog("tabular", params2string(*this), this);
3403 case LFUN_INSET_DIALOG_UPDATE:
3404 cur.bv().updateDialog("tabular", params2string(*this));
3407 case LFUN_TABULAR_FEATURE:
3408 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3412 // insert file functions
3413 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3414 case LFUN_FILE_INSERT_PLAINTEXT: {
3416 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3417 FileName(to_utf8(cmd.argument())));
3420 cur.recordUndoInset(INSERT_UNDO);
3421 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3422 // content has been replaced,
3423 // so cursor might be invalid
3424 cur.pos() = cur.lastpos();
3425 cur.pit() = cur.lastpit();
3426 bvcur.setCursor(cur);
3433 if (cur.selIsMultiCell()) {
3434 if (copySelection(cur)) {
3435 cur.recordUndoInset(DELETE_UNDO);
3440 cell(cur.idx())->dispatch(cur, cmd);
3443 case LFUN_CHAR_DELETE_BACKWARD:
3444 case LFUN_CHAR_DELETE_FORWARD:
3445 if (cur.selIsMultiCell()) {
3446 cur.recordUndoInset(DELETE_UNDO);
3450 cell(cur.idx())->dispatch(cur, cmd);
3454 if (!cur.selection())
3456 if (cur.selIsMultiCell()) {
3460 cell(cur.idx())->dispatch(cur, cmd);
3463 case LFUN_CLIPBOARD_PASTE:
3464 case LFUN_PRIMARY_SELECTION_PASTE: {
3465 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3466 theClipboard().getAsText() :
3467 theSelection().get();
3470 // pass to InsertPlaintextString, but
3471 // only if we have multi-cell content
3472 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3473 cur.recordUndoInset(INSERT_UNDO);
3474 if (insertPlaintextString(cur.bv(), clip, false)) {
3475 // content has been replaced,
3476 // so cursor might be invalid
3477 cur.pos() = cur.lastpos();
3478 cur.pit() = cur.lastpit();
3479 bvcur.setCursor(cur);
3483 // Let the cell handle normal text
3484 cell(cur.idx())->dispatch(cur, cmd);
3489 if (!tabularStackDirty()) {
3490 cell(cur.idx())->dispatch(cur, cmd);
3493 if (theClipboard().isInternal() ||
3494 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3495 cur.recordUndoInset(INSERT_UNDO);
3496 pasteClipboard(cur);
3500 case LFUN_FONT_EMPH:
3501 case LFUN_FONT_BOLD:
3502 case LFUN_FONT_ROMAN:
3503 case LFUN_FONT_NOUN:
3504 case LFUN_FONT_ITAL:
3505 case LFUN_FONT_FRAK:
3506 case LFUN_FONT_TYPEWRITER:
3507 case LFUN_FONT_SANS:
3508 case LFUN_TEXTSTYLE_APPLY:
3509 case LFUN_TEXTSTYLE_UPDATE:
3510 case LFUN_FONT_SIZE:
3511 case LFUN_FONT_UNDERLINE:
3513 case LFUN_WORD_CAPITALIZE:
3514 case LFUN_WORD_UPCASE:
3515 case LFUN_WORD_LOWCASE:
3516 case LFUN_CHARS_TRANSPOSE:
3517 if (cur.selIsMultiCell()) {
3520 getSelection(cur, rs, re, cs, ce);
3521 Cursor tmpcur = cur;
3522 for (row_type i = rs; i <= re; ++i) {
3523 for (col_type j = cs; j <= ce; ++j) {
3524 // cursor follows cell:
3525 tmpcur.idx() = tabular.cellIndex(i, j);
3526 // select this cell only:
3529 tmpcur.resetAnchor();
3530 tmpcur.pit() = tmpcur.lastpit();
3531 tmpcur.pos() = tmpcur.top().lastpos();
3532 tmpcur.setCursor(tmpcur);
3533 tmpcur.setSelection();
3534 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3539 cell(cur.idx())->dispatch(cur, cmd);
3543 // we try to handle this event in the insets dispatch function.
3544 cell(cur.idx())->dispatch(cur, cmd);
3550 // function sets an object as defined in func_status.h:
3551 // states OK, Unknown, Disabled, On, Off.
3552 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3553 FuncStatus & status) const
3555 switch (cmd.action) {
3556 case LFUN_TABULAR_FEATURE: {
3557 int action = Tabular::LAST_ACTION;
3559 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3560 string const tmp = tabularFeature[i].feature;
3561 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3562 action = tabularFeature[i].action;
3566 if (action == Tabular::LAST_ACTION) {
3568 status.unknown(true);
3572 string const argument
3573 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3575 row_type sel_row_start = 0;
3576 row_type sel_row_end = 0;
3577 col_type sel_col_start = 0;
3578 col_type sel_col_end = 0;
3579 Tabular::ltType dummyltt;
3582 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3585 case Tabular::SET_PWIDTH:
3586 case Tabular::SET_MPWIDTH:
3587 case Tabular::SET_SPECIAL_COLUMN:
3588 case Tabular::SET_SPECIAL_MULTI:
3589 case Tabular::APPEND_ROW:
3590 case Tabular::APPEND_COLUMN:
3591 case Tabular::DELETE_ROW:
3592 case Tabular::DELETE_COLUMN:
3593 case Tabular::COPY_ROW:
3594 case Tabular::COPY_COLUMN:
3595 case Tabular::SET_TOP_SPACE:
3596 case Tabular::SET_BOTTOM_SPACE:
3597 case Tabular::SET_INTERLINE_SPACE:
3601 case Tabular::MULTICOLUMN:
3602 status.setEnabled(sel_row_start == sel_row_end);
3603 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3606 case Tabular::SET_ALL_LINES:
3607 case Tabular::UNSET_ALL_LINES:
3608 case Tabular::SET_BORDER_LINES:
3609 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3612 case Tabular::TOGGLE_LINE_TOP:
3613 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3614 status.setOnOff(tabular.topLine(cur.idx()));
3617 case Tabular::TOGGLE_LINE_BOTTOM:
3618 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3619 status.setOnOff(tabular.bottomLine(cur.idx()));
3622 case Tabular::TOGGLE_LINE_LEFT:
3623 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3624 status.setOnOff(tabular.leftLine(cur.idx()));
3627 case Tabular::TOGGLE_LINE_RIGHT:
3628 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3629 status.setOnOff(tabular.rightLine(cur.idx()));
3632 case Tabular::M_ALIGN_LEFT:
3634 case Tabular::ALIGN_LEFT:
3635 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3638 case Tabular::M_ALIGN_RIGHT:
3640 case Tabular::ALIGN_RIGHT:
3641 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3644 case Tabular::M_ALIGN_CENTER:
3646 case Tabular::ALIGN_CENTER:
3647 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3650 case Tabular::ALIGN_BLOCK:
3651 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3652 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3655 case Tabular::M_VALIGN_TOP:
3657 case Tabular::VALIGN_TOP:
3659 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3662 case Tabular::M_VALIGN_BOTTOM:
3664 case Tabular::VALIGN_BOTTOM:
3666 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3669 case Tabular::M_VALIGN_MIDDLE:
3671 case Tabular::VALIGN_MIDDLE:
3673 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3676 case Tabular::SET_LONGTABULAR:
3677 status.setOnOff(tabular.is_long_tabular);
3680 case Tabular::UNSET_LONGTABULAR:
3681 status.setOnOff(!tabular.is_long_tabular);
3684 case Tabular::TOGGLE_ROTATE_TABULAR:
3685 case Tabular::SET_ROTATE_TABULAR:
3686 status.setOnOff(tabular.rotate);
3689 case Tabular::UNSET_ROTATE_TABULAR:
3690 status.setOnOff(!tabular.rotate);
3693 case Tabular::TOGGLE_ROTATE_CELL:
3694 case Tabular::SET_ROTATE_CELL:
3695 status.setOnOff(!oneCellHasRotationState(false,
3696 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3699 case Tabular::UNSET_ROTATE_CELL:
3700 status.setOnOff(!oneCellHasRotationState(true,
3701 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3704 case Tabular::SET_USEBOX:
3705 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3708 case Tabular::SET_LTFIRSTHEAD:
3709 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3712 case Tabular::UNSET_LTFIRSTHEAD:
3713 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3716 case Tabular::SET_LTHEAD:
3717 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3720 case Tabular::UNSET_LTHEAD:
3721 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3724 case Tabular::SET_LTFOOT:
3725 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3728 case Tabular::UNSET_LTFOOT:
3729 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3732 case Tabular::SET_LTLASTFOOT:
3733 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3736 case Tabular::UNSET_LTLASTFOOT:
3737 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3740 case Tabular::SET_LTNEWPAGE:
3741 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3744 case Tabular::TOGGLE_LTCAPTION:
3745 status.setEnabled(sel_row_start == sel_row_end);
3746 status.setOnOff(tabular.ltCaption(sel_row_start));
3749 case Tabular::SET_BOOKTABS:
3750 status.setOnOff(tabular.use_booktabs);
3753 case Tabular::UNSET_BOOKTABS:
3754 status.setOnOff(!tabular.use_booktabs);
3759 status.setEnabled(false);
3765 // These are only enabled inside tabular
3766 case LFUN_CELL_BACKWARD:
3767 case LFUN_CELL_FORWARD:
3768 status.setEnabled(true);
3771 // disable these with multiple cells selected
3772 case LFUN_INSET_INSERT:
3773 case LFUN_TABULAR_INSERT:
3774 case LFUN_FLEX_INSERT:
3775 case LFUN_FLOAT_INSERT:
3776 case LFUN_FLOAT_WIDE_INSERT:
3777 case LFUN_FOOTNOTE_INSERT:
3778 case LFUN_MARGINALNOTE_INSERT:
3779 case LFUN_MATH_INSERT:
3780 case LFUN_MATH_MODE:
3781 case LFUN_MATH_MUTATE:
3782 case LFUN_MATH_DISPLAY:
3783 case LFUN_NOTE_INSERT:
3784 case LFUN_OPTIONAL_INSERT:
3785 case LFUN_BOX_INSERT:
3786 case LFUN_BRANCH_INSERT:
3787 case LFUN_WRAP_INSERT:
3788 case LFUN_ERT_INSERT: {
3789 if (cur.selIsMultiCell()) {
3790 status.setEnabled(false);
3793 return cell(cur.idx())->getStatus(cur, cmd, status);
3796 // disable in non-fixed-width cells
3797 case LFUN_NEWLINE_INSERT:
3798 case LFUN_BREAK_PARAGRAPH: {
3799 if (tabular.getPWidth(cur.idx()).zero()) {
3800 status.setEnabled(false);
3803 return cell(cur.idx())->getStatus(cur, cmd, status);
3806 case LFUN_NEWPAGE_INSERT:
3807 status.setEnabled(false);
3811 if (cur.selIsMultiCell()) {
3812 status.setEnabled(false);
3813 status.message(_("You cannot paste into a multicell selection."));
3816 if (tabularStackDirty() && theClipboard().isInternal()) {
3817 status.setEnabled(true);
3820 return cell(cur.idx())->getStatus(cur, cmd, status);
3822 case LFUN_INSET_MODIFY:
3823 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3824 status.setEnabled(true);
3830 // we try to handle this event in the insets dispatch function.
3831 return cell(cur.idx())->getStatus(cur, cmd, status);
3836 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3838 return tabular.latex(os, runparams);
3842 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3844 os << '\n'; // output table on a new line
3845 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3846 tabular.plaintext(os, runparams, dp, false, 0);
3847 return PLAINTEXT_NEWLINE;
3851 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3856 // FIXME: Why not pass a proper DocIterator here?
3858 // if the table is inside a float it doesn't need the informaltable
3859 // wrapper. Search for it.
3860 for (master = owner(); master; master = master->owner())
3861 if (master->lyxCode() == FLOAT_CODE)
3866 os << "<informaltable>";
3869 ret += tabular.docbook(os, runparams);
3871 os << "</informaltable>";
3878 void InsetTabular::validate(LaTeXFeatures & features) const
3880 tabular.validate(features);
3884 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3886 return tabular.cellInset(idx);
3890 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3892 return tabular.cellInset(idx);
3896 void InsetTabular::cursorPos(BufferView const & bv,
3897 CursorSlice const & sl, bool boundary, int & x, int & y) const
3899 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3901 // y offset correction
3902 int const row = tabular.cellRow(sl.idx());
3903 for (int i = 0; i <= row; ++i) {
3905 y += tabular.rowAscent(i);
3906 y += tabular.getAdditionalHeight(i);
3909 y += tabular.rowDescent(i);
3912 // x offset correction
3913 int const col = tabular.cellColumn(sl.idx());
3914 int idx = tabular.cellIndex(row, 0);
3915 for (int j = 0; j < col; ++j) {
3916 if (tabular.isPartOfMultiColumn(row, j))
3918 x += tabular.columnWidth(idx);
3921 x += tabular.getBeginningOfTextInCell(idx);
3922 x += ADD_TO_TABULAR_WIDTH;
3927 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3931 Inset const & inset = *tabular.cellInset(cell);
3932 Point o = bv.coordCache().getInsets().xy(&inset);
3933 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3934 int const xend = xbeg + tabular.columnWidth(cell);
3935 row_type const row = tabular.cellRow(cell);
3936 int const ybeg = o.y_ - tabular.rowAscent(row) -
3937 tabular.getAdditionalHeight(row);
3938 int const yend = o.y_ + tabular.rowDescent(row);
3950 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3951 // << " ybeg=" << ybeg << " yend=" << yend
3952 // << " xx=" << xx << " yy=" << yy
3953 // << " dist=" << xx + yy << endl;
3958 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3960 //lyxerr << "InsetTabular::editXY: " << this << endl;
3961 cur.selection() = false;
3963 cur.idx() = getNearestCell(cur.bv(), x, y);
3965 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
3969 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3971 cur.idx() = getNearestCell(cur.bv(), x, y);
3972 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
3976 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3978 idx_type idx_min = 0;
3979 int dist_min = numeric_limits<int>::max();
3980 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3981 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3982 int const d = dist(bv, i, x, y);
3993 int InsetTabular::cellXPos(idx_type const cell) const
3997 for (; !tabular.isFirstCellInRow(c); --c)
4000 for (; c < cell; ++c)
4001 lx += tabular.columnWidth(c);
4007 void InsetTabular::resetPos(Cursor & cur) const
4009 BufferView & bv = cur.bv();
4010 int const maxwidth = bv.workWidth();
4012 int const scx_old = scx_;
4013 int const i = cur.find(this);
4018 int const X2 = maxwidth;
4019 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4020 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4021 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4024 scx_ = X1 + 20 - x1;
4026 scx_ = X2 - 20 - x2;
4031 // only update if offset changed
4032 if (scx_ != scx_old)
4033 cur.updateFlags(Update::Force | Update::FitCursor);
4037 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4039 if (isRightToLeft(cur)) {
4040 if (tabular.isFirstCellInRow(cur.idx())) {
4041 row_type const row = tabular.cellRow(cur.idx());
4042 if (row == tabular.row_info.size() - 1)
4044 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4051 if (tabular.isLastCell(cur.idx()))
4057 cur.boundary(false);
4059 // in visual mode, place cursor at extreme left or right
4061 switch(entry_from) {
4063 case ENTRY_DIRECTION_RIGHT:
4064 cur.posVisToRowExtremity(false /* !left */);
4066 case ENTRY_DIRECTION_LEFT:
4067 cur.posVisToRowExtremity(true /* left */);
4069 case ENTRY_DIRECTION_IGNORE:
4070 // nothing to do in this case
4079 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4081 if (isRightToLeft(cur)) {
4082 if (tabular.isLastCellInRow(cur.idx())) {
4083 row_type const row = tabular.cellRow(cur.idx());
4086 cur.idx() = tabular.getFirstCellInRow(row);
4087 cur.idx() = tabular.cellAbove(cur.idx());
4089 if (tabular.isLastCell(cur.idx()))
4094 if (cur.idx() == 0) // first cell
4098 cur.pit() = cur.lastpit();
4099 cur.pos() = cur.lastpos();
4101 // in visual mode, place cursor at extreme left or right
4103 switch(entry_from) {
4105 case ENTRY_DIRECTION_RIGHT:
4106 cur.posVisToRowExtremity(false /* !left */);
4108 case ENTRY_DIRECTION_LEFT:
4109 cur.posVisToRowExtremity(true /* left */);
4111 case ENTRY_DIRECTION_IGNORE:
4112 // nothing to do in this case
4121 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4123 Tabular::Feature action = Tabular::LAST_ACTION;
4126 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4127 string const tmp = tabularFeature[i].feature;
4129 if (tmp == what.substr(0, tmp.length())) {
4130 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4131 //tabularFeatures[i].feature.length()))
4132 action = tabularFeature[i].action;
4136 if (action == Tabular::LAST_ACTION)
4140 ltrim(what.substr(tabularFeature[i].feature.length()));
4141 tabularFeatures(cur, action, val);
4146 static void checkLongtableSpecial(Tabular::ltType & ltt,
4147 string const & special, bool & flag)
4149 if (special == "dl_above") {
4152 } else if (special == "dl_below") {
4153 ltt.bottomDL = flag;
4155 } else if (special == "empty") {
4164 bool InsetTabular::oneCellHasRotationState(bool rotated,
4165 row_type row_start, row_type row_end,
4166 col_type col_start, col_type col_end) const {
4168 for (row_type i = row_start; i <= row_end; ++i) {
4169 for (col_type j = col_start; j <= col_end; ++j) {
4170 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4179 void InsetTabular::tabularFeatures(Cursor & cur,
4180 Tabular::Feature feature, string const & value)
4182 col_type sel_col_start;
4183 col_type sel_col_end;
4184 row_type sel_row_start;
4185 row_type sel_row_end;
4186 bool setLines = false;
4187 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4188 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4192 case Tabular::M_ALIGN_LEFT:
4193 case Tabular::ALIGN_LEFT:
4194 setAlign = LYX_ALIGN_LEFT;
4197 case Tabular::M_ALIGN_RIGHT:
4198 case Tabular::ALIGN_RIGHT:
4199 setAlign = LYX_ALIGN_RIGHT;
4202 case Tabular::M_ALIGN_CENTER:
4203 case Tabular::ALIGN_CENTER:
4204 setAlign = LYX_ALIGN_CENTER;
4207 case Tabular::ALIGN_BLOCK:
4208 setAlign = LYX_ALIGN_BLOCK;
4211 case Tabular::M_VALIGN_TOP:
4212 case Tabular::VALIGN_TOP:
4213 setVAlign = Tabular::LYX_VALIGN_TOP;
4216 case Tabular::M_VALIGN_BOTTOM:
4217 case Tabular::VALIGN_BOTTOM:
4218 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4221 case Tabular::M_VALIGN_MIDDLE:
4222 case Tabular::VALIGN_MIDDLE:
4223 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4230 cur.recordUndoInset(ATOMIC_UNDO);
4232 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4233 row_type const row = tabular.cellRow(cur.idx());
4234 col_type const column = tabular.cellColumn(cur.idx());
4236 Tabular::ltType ltt;
4240 case Tabular::SET_PWIDTH: {
4241 Length const len(value);
4242 tabular.setColumnPWidth(cur, cur.idx(), len);
4244 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4245 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4249 case Tabular::SET_MPWIDTH:
4250 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4253 case Tabular::SET_SPECIAL_COLUMN:
4254 case Tabular::SET_SPECIAL_MULTI:
4255 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4258 case Tabular::APPEND_ROW:
4259 // append the row into the tabular
4260 tabular.appendRow(cur.idx());
4263 case Tabular::APPEND_COLUMN:
4264 // append the column into the tabular
4265 tabular.appendColumn(cur.idx());
4266 cur.idx() = tabular.cellIndex(row, column);
4269 case Tabular::DELETE_ROW:
4270 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4271 tabular.deleteRow(sel_row_start);
4272 if (sel_row_start >= tabular.row_info.size())
4274 cur.idx() = tabular.cellIndex(sel_row_start, column);
4277 cur.selection() = false;
4280 case Tabular::DELETE_COLUMN:
4281 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4282 tabular.deleteColumn(sel_col_start);
4283 if (sel_col_start >= tabular.column_info.size())
4285 cur.idx() = tabular.cellIndex(row, sel_col_start);
4288 cur.selection() = false;
4291 case Tabular::COPY_ROW:
4292 tabular.copyRow(row);
4295 case Tabular::COPY_COLUMN:
4296 tabular.copyColumn(column);
4297 cur.idx() = tabular.cellIndex(row, column);
4300 case Tabular::TOGGLE_LINE_TOP: {
4301 bool lineSet = !tabular.topLine(cur.idx());
4302 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4303 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4304 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4308 case Tabular::TOGGLE_LINE_BOTTOM: {
4309 bool lineSet = !tabular.bottomLine(cur.idx());
4310 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4311 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4312 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4316 case Tabular::TOGGLE_LINE_LEFT: {
4317 bool lineSet = !tabular.leftLine(cur.idx());
4318 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4319 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4320 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4324 case Tabular::TOGGLE_LINE_RIGHT: {
4325 bool lineSet = !tabular.rightLine(cur.idx());
4326 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4327 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4328 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4332 case Tabular::M_ALIGN_LEFT:
4333 case Tabular::M_ALIGN_RIGHT:
4334 case Tabular::M_ALIGN_CENTER:
4336 case Tabular::ALIGN_LEFT:
4337 case Tabular::ALIGN_RIGHT:
4338 case Tabular::ALIGN_CENTER:
4339 case Tabular::ALIGN_BLOCK:
4340 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4341 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4342 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4345 case Tabular::M_VALIGN_TOP:
4346 case Tabular::M_VALIGN_BOTTOM:
4347 case Tabular::M_VALIGN_MIDDLE:
4349 case Tabular::VALIGN_TOP:
4350 case Tabular::VALIGN_BOTTOM:
4351 case Tabular::VALIGN_MIDDLE:
4352 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4353 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4354 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4357 case Tabular::MULTICOLUMN: {
4358 if (!cur.selection()) {
4359 // just multicol for one single cell
4360 // check whether we are completely in a multicol
4361 if (tabular.isMultiColumn(cur.idx()))
4362 tabular.unsetMultiColumn(cur.idx());
4364 tabular.setMultiColumn(cur.idx(), 1);
4367 // we have a selection so this means we just add all this
4368 // cells to form a multicolumn cell
4369 idx_type const s_start = cur.selBegin().idx();
4370 idx_type const s_end = cur.selEnd().idx();
4371 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4372 cur.idx() = s_start;
4375 cur.selection() = false;
4379 case Tabular::SET_ALL_LINES:
4381 case Tabular::UNSET_ALL_LINES:
4382 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4383 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4384 tabular.setAllLines(
4385 tabular.cellIndex(i,j), setLines);
4388 case Tabular::SET_BORDER_LINES:
4389 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4390 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4391 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4393 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4394 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4395 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4399 case Tabular::SET_LONGTABULAR:
4400 tabular.is_long_tabular = true;
4403 case Tabular::UNSET_LONGTABULAR:
4404 tabular.is_long_tabular = false;
4407 case Tabular::SET_ROTATE_TABULAR:
4408 tabular.rotate = true;
4411 case Tabular::UNSET_ROTATE_TABULAR:
4412 tabular.rotate = false;
4415 case Tabular::TOGGLE_ROTATE_TABULAR:
4416 tabular.rotate = !tabular.rotate;
4419 case Tabular::SET_ROTATE_CELL:
4420 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4421 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4422 tabular.setRotateCell(
4423 tabular.cellIndex(i, j), true);
4426 case Tabular::UNSET_ROTATE_CELL:
4427 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4428 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4429 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4432 case Tabular::TOGGLE_ROTATE_CELL:
4434 bool oneNotRotated = oneCellHasRotationState(false,
4435 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4437 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4438 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4439 tabular.setRotateCell(tabular.cellIndex(i, j),
4444 case Tabular::SET_USEBOX: {
4445 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4446 if (val == tabular.getUsebox(cur.idx()))
4447 val = Tabular::BOX_NONE;
4448 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4449 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4450 tabular.setUsebox(tabular.cellIndex(i, j), val);
4454 case Tabular::UNSET_LTFIRSTHEAD:
4456 case Tabular::SET_LTFIRSTHEAD:
4457 tabular.getRowOfLTFirstHead(row, ltt);
4458 checkLongtableSpecial(ltt, value, flag);
4459 tabular.setLTHead(row, flag, ltt, true);
4462 case Tabular::UNSET_LTHEAD:
4464 case Tabular::SET_LTHEAD:
4465 tabular.getRowOfLTHead(row, ltt);
4466 checkLongtableSpecial(ltt, value, flag);
4467 tabular.setLTHead(row, flag, ltt, false);
4470 case Tabular::UNSET_LTFOOT:
4472 case Tabular::SET_LTFOOT:
4473 tabular.getRowOfLTFoot(row, ltt);
4474 checkLongtableSpecial(ltt, value, flag);
4475 tabular.setLTFoot(row, flag, ltt, false);
4478 case Tabular::UNSET_LTLASTFOOT:
4480 case Tabular::SET_LTLASTFOOT:
4481 tabular.getRowOfLTLastFoot(row, ltt);
4482 checkLongtableSpecial(ltt, value, flag);
4483 tabular.setLTFoot(row, flag, ltt, true);
4486 case Tabular::SET_LTNEWPAGE:
4487 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4490 case Tabular::TOGGLE_LTCAPTION:
4491 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4494 cur.selection() = false;
4497 case Tabular::SET_BOOKTABS:
4498 tabular.use_booktabs = true;
4501 case Tabular::UNSET_BOOKTABS:
4502 tabular.use_booktabs = false;
4505 case Tabular::SET_TOP_SPACE: {
4507 if (value == "default")
4508 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4509 tabular.row_info[i].top_space_default = true;
4510 else if (isValidLength(value, &len))
4511 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4512 tabular.row_info[i].top_space_default = false;
4513 tabular.row_info[i].top_space = len;
4516 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4517 tabular.row_info[i].top_space_default = false;
4518 tabular.row_info[i].top_space = len;
4523 case Tabular::SET_BOTTOM_SPACE: {
4525 if (value == "default")
4526 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4527 tabular.row_info[i].bottom_space_default = true;
4528 else if (isValidLength(value, &len))
4529 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4530 tabular.row_info[i].bottom_space_default = false;
4531 tabular.row_info[i].bottom_space = len;
4534 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4535 tabular.row_info[i].bottom_space_default = false;
4536 tabular.row_info[i].bottom_space = len;
4541 case Tabular::SET_INTERLINE_SPACE: {
4543 if (value == "default")
4544 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4545 tabular.row_info[i].interline_space_default = true;
4546 else if (isValidLength(value, &len))
4547 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4548 tabular.row_info[i].interline_space_default = false;
4549 tabular.row_info[i].interline_space = len;
4552 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4553 tabular.row_info[i].interline_space_default = false;
4554 tabular.row_info[i].interline_space = len;
4559 // dummy stuff just to avoid warnings
4560 case Tabular::LAST_ACTION:
4566 bool InsetTabular::showInsetDialog(BufferView * bv) const
4568 bv->showDialog("tabular", params2string(*this),
4569 const_cast<InsetTabular *>(this));
4574 void InsetTabular::openLayoutDialog(BufferView * bv) const
4576 bv->showDialog("tabular", params2string(*this),
4577 const_cast<InsetTabular *>(this));
4581 bool InsetTabular::copySelection(Cursor & cur)
4583 if (!cur.selection())
4588 getSelection(cur, rs, re, cs, ce);
4590 paste_tabular.reset(new Tabular(tabular));
4592 for (row_type i = 0; i < rs; ++i)
4593 paste_tabular->deleteRow(0);
4595 row_type const rows = re - rs + 1;
4596 while (paste_tabular->row_info.size() > rows)
4597 paste_tabular->deleteRow(rows);
4599 for (col_type i = 0; i < cs; ++i)
4600 paste_tabular->deleteColumn(0);
4602 col_type const columns = ce - cs + 1;
4603 while (paste_tabular->column_info.size() > columns)
4604 paste_tabular->deleteColumn(columns);
4606 paste_tabular->setBuffer(tabular.buffer());
4608 odocstringstream os;
4609 OutputParams const runparams(0);
4610 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4611 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4612 cap::copySelection(cur, os.str());
4614 // mark tabular stack dirty
4615 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4616 // when we (hopefully) have a one-for-all paste mechanism.
4617 // This must be called after cap::copySelection.
4618 dirtyTabularStack(true);
4624 bool InsetTabular::pasteClipboard(Cursor & cur)
4628 col_type const actcol = tabular.cellColumn(cur.idx());
4629 row_type const actrow = tabular.cellRow(cur.idx());
4630 for (row_type r1 = 0, r2 = actrow;
4631 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4633 for (col_type c1 = 0, c2 = actcol;
4634 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4636 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4637 tabular.isPartOfMultiColumn(r2, c2))
4639 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4643 if (tabular.isPartOfMultiColumn(r2, c2)) {
4647 shared_ptr<InsetTableCell> inset(
4648 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4649 tabular.setCellInset(r2, c2, inset);
4650 // FIXME: change tracking (MG)
4651 inset->setChange(Change(cur.buffer().params().trackChanges ?
4652 Change::INSERTED : Change::UNCHANGED));
4660 void InsetTabular::cutSelection(Cursor & cur)
4662 if (!cur.selection())
4667 getSelection(cur, rs, re, cs, ce);
4668 for (row_type i = rs; i <= re; ++i) {
4669 for (col_type j = cs; j <= ce; ++j) {
4670 shared_ptr<InsetTableCell> t
4671 = cell(tabular.cellIndex(i, j));
4672 if (cur.buffer().params().trackChanges)
4673 // FIXME: Change tracking (MG)
4674 t->setChange(Change(Change::DELETED));
4680 // cursor position might be invalid now
4681 if (cur.pit() > cur.lastpit())
4682 cur.pit() = cur.lastpit();
4683 if (cur.pos() > cur.lastpos())
4684 cur.pos() = cur.lastpos();
4685 cur.clearSelection();
4689 bool InsetTabular::isRightToLeft(Cursor & cur) const
4691 LASSERT(cur.depth() > 1, /**/);
4692 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4693 pos_type const parentpos = cur[cur.depth() - 2].pos();
4694 return parentpar.getFontSettings(cur.bv().buffer().params(),
4695 parentpos).language()->rightToLeft();
4698 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
4701 LASSERT(stidx <= enidx, return docstring());
4703 col_type const col1 = tabular.cellColumn(stidx);
4704 col_type const col2 = tabular.cellColumn(enidx);
4705 row_type const row1 = tabular.cellRow(stidx);
4706 row_type const row2 = tabular.cellRow(enidx);
4708 for (col_type col = col1; col <= col2; col++)
4709 for (row_type row = row1; row <= row2; row++) {
4714 retval += tabular.cellInset(row, col)->asString(intoInsets);
4720 void InsetTabular::getSelection(Cursor & cur,
4721 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4723 CursorSlice const & beg = cur.selBegin();
4724 CursorSlice const & end = cur.selEnd();
4725 cs = tabular.cellColumn(beg.idx());
4726 ce = tabular.cellColumn(end.idx());
4729 cs = tabular.cellColumn(end.idx());
4731 ce = tabular.cellRightColumn(end.idx());
4734 rs = tabular.cellRow(beg.idx());
4735 re = tabular.cellRow(end.idx());
4741 Text * InsetTabular::getText(int idx) const
4743 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4747 void InsetTabular::setChange(Change const & change)
4749 for (idx_type idx = 0; idx < nargs(); ++idx)
4750 cell(idx)->setChange(change);
4754 void InsetTabular::acceptChanges(BufferParams const & bparams)
4756 for (idx_type idx = 0; idx < nargs(); ++idx)
4757 cell(idx)->acceptChanges(bparams);
4761 void InsetTabular::rejectChanges(BufferParams const & bparams)
4763 for (idx_type idx = 0; idx < nargs(); ++idx)
4764 cell(idx)->rejectChanges(bparams);
4768 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4770 return tabular.getPWidth(cell).zero();
4774 bool InsetTabular::forcePlainLayout(idx_type cell) const
4776 return !tabular.getPWidth(cell).zero();
4780 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4783 if (buf.length() <= 0)
4788 col_type maxCols = 1;
4789 size_t const len = buf.length();
4793 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4801 maxCols = max(cols, maxCols);
4807 maxCols = max(cols, maxCols);
4813 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4814 loctab = paste_tabular.get();
4816 dirtyTabularStack(true);
4819 cell = bv.cursor().idx();
4820 ocol = tabular.cellColumn(cell);
4821 row = tabular.cellRow(cell);
4825 idx_type const cells = loctab->numberofcells;
4828 rows = loctab->row_info.size();
4829 col_type const columns = loctab->column_info.size();
4831 while (cell < cells && p < len && row < rows &&
4832 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4838 // we can only set this if we are not too far right
4839 if (cols < columns) {
4840 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4841 Font const font = bv.textMetrics(&inset->text()).
4843 inset->setText(buf.substr(op, p - op), font,
4844 buffer().params().trackChanges);
4850 // we can only set this if we are not too far right
4851 if (cols < columns) {
4852 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4853 Font const font = bv.textMetrics(&inset->text()).
4855 inset->setText(buf.substr(op, p - op), font,
4856 buffer().params().trackChanges);
4861 cell = loctab->cellIndex(row, cols);
4867 // check for the last cell if there is no trailing '\n'
4868 if (cell < cells && op < len) {
4869 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4870 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
4871 inset->setText(buf.substr(op, len - op), font,
4872 buffer().params().trackChanges);
4878 void InsetTabular::addPreview(PreviewLoader & loader) const
4880 row_type const rows = tabular.row_info.size();
4881 col_type const columns = tabular.column_info.size();
4882 for (row_type i = 0; i < rows; ++i) {
4883 for (col_type j = 0; j < columns; ++j)
4884 tabular.cellInset(i, j)->addPreview(loader);
4889 bool InsetTabular::completionSupported(Cursor const & cur) const
4891 Cursor const & bvCur = cur.bv().cursor();
4892 if (&bvCur.inset() != this)
4894 return cur.text()->completionSupported(cur);
4898 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4900 return completionSupported(cur);
4904 bool InsetTabular::automaticInlineCompletion() const
4906 return lyxrc.completion_inline_text;
4910 bool InsetTabular::automaticPopupCompletion() const
4912 return lyxrc.completion_popup_text;
4916 bool InsetTabular::showCompletionCursor() const
4918 return lyxrc.completion_cursor_text;
4922 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4924 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4928 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4930 if (!completionSupported(cur))
4932 return cur.text()->completionPrefix(cur);
4936 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4938 if (!completionSupported(cur))
4941 return cur.text()->insertCompletion(cur, s, finished);
4945 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4946 Dimension & dim) const
4948 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4949 tm.completionPosAndDim(cur, x, y, dim);
4953 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4955 istringstream data(in);
4957 lex.setStream(data);
4964 if (!lex || token != "tabular") {
4965 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4969 // This is part of the inset proper that is usually swallowed
4970 // by Buffer::readInset
4972 if (!lex || token != "Tabular") {
4973 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4981 string InsetTabular::params2string(InsetTabular const & inset)
4984 data << "tabular" << ' ';
4986 data << "\\end_inset\n";