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 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
73 using boost::shared_ptr;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2; // in cell
92 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
93 int const default_line_space = 10; // ?
94 int const WIDTH_OF_LINE = 5; // space between double lines
98 boost::scoped_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
108 TabularFeature tabularFeature[] =
110 { Tabular::APPEND_ROW, "append-row", false },
111 { Tabular::APPEND_COLUMN, "append-column", false },
112 { Tabular::DELETE_ROW, "delete-row", false },
113 { Tabular::DELETE_COLUMN, "delete-column", false },
114 { Tabular::COPY_ROW, "copy-row", false },
115 { Tabular::COPY_COLUMN, "copy-column", false },
116 { Tabular::SET_LINE_TOP, "set-line-top", true },
117 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
118 { Tabular::SET_LINE_LEFT, "set-line-left", true },
119 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
120 //FIXME: get rid of those 4 TOGGLE actions in favor of the 4 above.
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::VALIGN_TOP, "valign-top", false },
130 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
131 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
132 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
133 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
134 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
135 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
136 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
137 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
138 { Tabular::MULTICOLUMN, "multicolumn", false },
139 { Tabular::MULTIROW, "multirow", false },
140 { Tabular::SET_ALL_LINES, "set-all-lines", false },
141 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
142 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
143 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
144 { Tabular::SET_PWIDTH, "set-pwidth", true },
145 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
146 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
147 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
148 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
149 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
150 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
151 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
152 { Tabular::SET_USEBOX, "set-usebox", true },
153 { Tabular::SET_LTHEAD, "set-lthead", true },
154 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
155 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
156 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
157 { Tabular::SET_LTFOOT, "set-ltfoot", true },
158 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
159 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
160 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
161 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
162 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
163 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
164 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
165 { Tabular::SET_SPECIAL_MULTIROW, "set-special-multirow", false },
166 { Tabular::SET_BOOKTABS, "set-booktabs", false },
167 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
168 { Tabular::SET_TOP_SPACE, "set-top-space", true },
169 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
170 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
171 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
172 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
173 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
174 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
175 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
176 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
177 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
178 { Tabular::LAST_ACTION, "", false }
183 string const write_attribute(string const & name, T const & t)
185 string const s = tostr(t);
186 return s.empty() ? s : " " + name + "=\"" + s + "\"";
190 string const write_attribute(string const & name, string const & t)
192 return t.empty() ? t : " " + name + "=\"" + t + "\"";
197 string const write_attribute(string const & name, docstring const & t)
199 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
204 string const write_attribute(string const & name, bool const & b)
206 // we write only true attribute values so we remove a bit of the
207 // file format bloat for tabulars.
208 return b ? write_attribute(name, convert<string>(b)) : string();
213 string const write_attribute(string const & name, int const & i)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return i ? write_attribute(name, convert<string>(i)) : string();
222 string const write_attribute(string const & name, Tabular::idx_type const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Length const & value)
233 // we write only the value if we really have one same reson as above.
234 return value.zero() ? string() : write_attribute(name, value.asString());
238 string const tostr(LyXAlignment const & num)
243 case LYX_ALIGN_BLOCK:
247 case LYX_ALIGN_CENTER:
249 case LYX_ALIGN_RIGHT:
251 case LYX_ALIGN_LAYOUT:
253 case LYX_ALIGN_SPECIAL:
260 string const tostr(Tabular::HAlignment const & num)
263 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
265 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
267 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
274 string const tostr(Tabular::VAlignment const & num)
277 case Tabular::LYX_VALIGN_TOP:
279 case Tabular::LYX_VALIGN_MIDDLE:
281 case Tabular::LYX_VALIGN_BOTTOM:
288 string const tostr(Tabular::BoxType const & num)
291 case Tabular::BOX_NONE:
293 case Tabular::BOX_PARBOX:
295 case Tabular::BOX_MINIPAGE:
302 // I would have liked a fromstr template a lot better. (Lgb)
303 bool string2type(string const str, LyXAlignment & num)
306 num = LYX_ALIGN_NONE;
307 else if (str == "block")
308 num = LYX_ALIGN_BLOCK;
309 else if (str == "left")
310 num = LYX_ALIGN_LEFT;
311 else if (str == "center")
312 num = LYX_ALIGN_CENTER;
313 else if (str == "right")
314 num = LYX_ALIGN_RIGHT;
321 bool string2type(string const str, Tabular::HAlignment & num)
324 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
325 else if (str == "center" )
326 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
327 else if (str == "right")
328 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
335 bool string2type(string const str, Tabular::VAlignment & num)
338 num = Tabular::LYX_VALIGN_TOP;
339 else if (str == "middle" )
340 num = Tabular::LYX_VALIGN_MIDDLE;
341 else if (str == "bottom")
342 num = Tabular::LYX_VALIGN_BOTTOM;
349 bool string2type(string const str, Tabular::BoxType & num)
352 num = Tabular::BOX_NONE;
353 else if (str == "parbox")
354 num = Tabular::BOX_PARBOX;
355 else if (str == "minipage")
356 num = Tabular::BOX_MINIPAGE;
363 bool string2type(string const str, bool & num)
367 else if (str == "false")
375 bool getTokenValue(string const & str, char const * token, string & ret)
378 size_t token_length = strlen(token);
379 size_t pos = str.find(token);
381 if (pos == string::npos || pos + token_length + 1 >= str.length()
382 || str[pos + token_length] != '=')
384 pos += token_length + 1;
386 if (ch != '"' && ch != '\'') { // only read till next space
390 while (pos < str.length() - 1 && str[++pos] != ch)
397 bool getTokenValue(string const & str, char const * token, docstring & ret)
400 bool const success = getTokenValue(str, token, tmp);
401 ret = from_utf8(tmp);
406 bool getTokenValue(string const & str, char const * token, int & num)
410 if (!getTokenValue(str, token, tmp))
412 num = convert<int>(tmp);
417 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
420 return getTokenValue(str, token, tmp) && string2type(tmp, num);
424 bool getTokenValue(string const & str, char const * token,
425 Tabular::HAlignment & num)
428 return getTokenValue(str, token, tmp) && string2type(tmp, num);
432 bool getTokenValue(string const & str, char const * token,
433 Tabular::VAlignment & num)
436 return getTokenValue(str, token, tmp) && string2type(tmp, num);
440 bool getTokenValue(string const & str, char const * token,
441 Tabular::BoxType & num)
444 return getTokenValue(str, token, tmp) && string2type(tmp, num);
448 bool getTokenValue(string const & str, char const * token, bool & flag)
450 // set the flag always to false as this should be the default for bools
451 // not in the file-format.
454 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
458 bool getTokenValue(string const & str, char const * token, Length & len)
460 // set the length to be zero() as default as this it should be if not
461 // in the file format.
464 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
468 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
473 if (!getTokenValue(str, token, tmp))
475 if (tmp == "default") {
479 return isValidLength(tmp, &len);
483 void l_getline(istream & is, string & str)
486 while (str.empty()) {
488 if (!str.empty() && str[str.length() - 1] == '\r')
489 str.erase(str.length() - 1);
496 string const featureAsString(Tabular::Feature action)
498 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
499 if (tabularFeature[i].action == action)
500 return tabularFeature[i].feature;
507 /////////////////////////////////////////////////////////////////////
511 /////////////////////////////////////////////////////////////////////
514 Tabular::CellData::CellData(Buffer * buf)
517 multicolumn(Tabular::CELL_NORMAL),
518 multirow(Tabular::CELL_NORMAL),
519 alignment(LYX_ALIGN_CENTER),
520 valignment(LYX_VALIGN_TOP),
528 inset(new InsetTableCell(buf))
530 inset->setBuffer(*buf);
534 Tabular::CellData::CellData(CellData const & cs)
537 multicolumn(cs.multicolumn),
538 multirow(cs.multirow),
539 alignment(cs.alignment),
540 valignment(cs.valignment),
541 top_line(cs.top_line),
542 bottom_line(cs.bottom_line),
543 left_line(cs.left_line),
544 right_line(cs.right_line),
547 align_special(cs.align_special),
549 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
553 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
559 void Tabular::CellData::swap(CellData & rhs)
561 std::swap(cellno, rhs.cellno);
562 std::swap(width, rhs.width);
563 std::swap(multicolumn, rhs.multicolumn);
564 std::swap(multirow, rhs.multirow);
565 std::swap(alignment, rhs.alignment);
566 std::swap(valignment, rhs.valignment);
567 std::swap(top_line, rhs.top_line);
568 std::swap(bottom_line, rhs.bottom_line);
569 std::swap(left_line, rhs.left_line);
570 std::swap(right_line, rhs.right_line);
571 std::swap(usebox, rhs.usebox);
572 std::swap(rotate, rhs.rotate);
573 std::swap(align_special, rhs.align_special);
574 p_width.swap(rhs.p_width);
575 inset.swap(rhs.inset);
579 Tabular::RowData::RowData()
582 top_space_default(false),
583 bottom_space_default(false),
584 interline_space_default(false),
594 Tabular::ColumnData::ColumnData()
595 : alignment(LYX_ALIGN_CENTER),
596 valignment(LYX_VALIGN_TOP),
602 Tabular::ltType::ltType()
609 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
611 init(buffer, rows_arg, columns_arg);
615 void Tabular::setBuffer(Buffer & buffer)
618 for (row_type i = 0; i < nrows(); ++i)
619 for (col_type j = 0; j < ncols(); ++j)
620 cell_info[i][j].inset->setBuffer(*buffer_);
624 // activates all lines and sets all widths to 0
625 void Tabular::init(Buffer * buf, row_type rows_arg,
626 col_type columns_arg)
629 row_info = row_vector(rows_arg);
630 column_info = column_vector(columns_arg);
631 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
632 row_info.reserve(10);
633 column_info.reserve(10);
634 cell_info.reserve(100);
636 is_long_tabular = false;
637 tabular_valignment = LYX_VALIGN_MIDDLE;
638 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
640 use_booktabs = false;
641 // set silly default lines
642 for (row_type r = 0; r < nrows(); ++r)
643 for (col_type c = 0; c < ncols(); ++c) {
644 cell_info[r][c].inset->setBuffer(*buffer_);
645 cell_info[r][c].top_line = true;
646 cell_info[r][c].left_line = true;
647 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
648 cell_info[r][c].right_line = c == ncols() - 1;
653 void Tabular::appendRow(idx_type const cell)
655 row_type const row = cellRow(cell);
657 row_info.insert(row_info.begin() + row + 1, RowData());
658 row_info[row + 1] = row_info[row];
660 cell_info.insert(cell_info.begin() + row + 1,
661 cell_vector(ncols(), CellData(buffer_)));
662 for (col_type c = 0; c < ncols(); ++c) {
663 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
664 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
666 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
670 for (col_type c = 0; c < ncols(); ++c) {
671 if (isPartOfMultiRow(row, c))
673 // inherit line settings
674 idx_type const i = cellIndex(row + 1, c);
675 idx_type const j = cellIndex(row, c);
676 setLeftLine(i, leftLine(j));
677 setRightLine(i, rightLine(j));
678 setTopLine(i, topLine(j));
679 if (topLine(j) && bottomLine(j)) {
680 setBottomLine(i, true);
681 setBottomLine(j, false);
683 // mark track changes
684 if (buffer().params().trackChanges)
685 cellInfo(i).inset->setChange(Change(Change::INSERTED));
690 void Tabular::deleteRow(row_type const row)
692 // Not allowed to delete last row
696 for (col_type c = 0; c < ncols(); ++c) {
697 // Care about multirow cells
698 if (row + 1 < nrows() &&
699 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
700 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
701 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
704 row_info.erase(row_info.begin() + row);
705 cell_info.erase(cell_info.begin() + row);
710 void Tabular::copyRow(row_type const row)
712 row_info.insert(row_info.begin() + row, row_info[row]);
713 cell_info.insert(cell_info.begin() + row, cell_info[row]);
715 if (buffer().params().trackChanges)
716 for (col_type c = 0; c < ncols(); ++c)
717 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
723 void Tabular::appendColumn(idx_type const cell)
725 col_type const c = cellColumn(cell);
727 column_info.insert(column_info.begin() + c + 1, ColumnData());
728 column_info[c + 1] = column_info[c];
730 for (row_type r = 0; r < nrows(); ++r) {
731 cell_info[r].insert(cell_info[r].begin() + c + 1,
733 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
734 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
736 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
739 for (row_type r = 0; r < nrows(); ++r) {
740 // inherit line settings
741 idx_type const i = cellIndex(r, c + 1);
742 idx_type const j = cellIndex(r, c);
743 setBottomLine(i, bottomLine(j));
744 setTopLine(i, topLine(j));
745 setLeftLine(i, leftLine(j));
746 if (rightLine(j) && rightLine(j)) {
747 setRightLine(i, true);
748 setRightLine(j, false);
750 if (buffer().params().trackChanges)
751 cellInfo(i).inset->setChange(Change(Change::INSERTED));
756 void Tabular::deleteColumn(col_type const col)
758 // Not allowed to delete last column
762 for (row_type r = 0; r < nrows(); ++r) {
763 // Care about multicolumn cells
764 if (col + 1 < ncols() &&
765 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
766 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
767 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
769 cell_info[r].erase(cell_info[r].begin() + col);
771 column_info.erase(column_info.begin() + col);
776 void Tabular::copyColumn(col_type const col)
778 BufferParams const & bp = buffer().params();
779 column_info.insert(column_info.begin() + col, column_info[col]);
781 for (row_type r = 0; r < nrows(); ++r) {
782 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
784 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
790 void Tabular::updateIndexes()
794 for (row_type row = 0; row < nrows(); ++row)
795 for (col_type column = 0; column < ncols(); ++column) {
796 if (!isPartOfMultiColumn(row, column)
797 && !isPartOfMultiRow(row, column))
799 if (isPartOfMultiRow(row, column))
800 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
802 cell_info[row][column].cellno = numberofcells - 1;
805 rowofcell.resize(numberofcells);
806 columnofcell.resize(numberofcells);
808 for (row_type row = 0; row < nrows(); ++row)
809 for (col_type column = 0; column < ncols(); ++column) {
810 if (isPartOfMultiColumn(row, column)
811 || isPartOfMultiRow(row, column))
814 columnofcell[i] = column;
815 setFixedWidth(row, column);
816 updateContentAlignment(row, column);
822 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
825 for (col_type c = 0; c < ncols(); ++c)
826 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
832 bool Tabular::topLine(idx_type const cell) const
834 return cellInfo(cell).top_line;
838 bool Tabular::bottomLine(idx_type const cell) const
840 return cellInfo(cell).bottom_line;
844 bool Tabular::leftLine(idx_type cell) const
848 return cellInfo(cell).left_line;
852 bool Tabular::rightLine(idx_type cell) const
856 return cellInfo(cell).right_line;
860 int Tabular::interRowSpace(row_type row) const
862 if (!row || row >= nrows())
865 int const interline_space = row_info[row - 1].interline_space_default ?
867 row_info[row - 1].interline_space.inPixels(width());
868 if (rowTopLine(row) && rowBottomLine(row - 1))
869 return interline_space + WIDTH_OF_LINE;
870 return interline_space;
874 int Tabular::interColumnSpace(idx_type cell) const
876 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
877 if (rightLine(cell) && nextcol < ncols()
878 && leftLine(cellIndex(cellRow(cell), nextcol)))
879 return WIDTH_OF_LINE;
884 int Tabular::columnWidth(idx_type cell) const
887 col_type const span = columnSpan(cell);
888 col_type const col = cellColumn(cell);
889 for(col_type c = col; c < col + span ; ++c)
890 w += column_info[c].width;
895 int Tabular::cellHeight(idx_type cell) const
897 row_type const span = rowSpan(cell);
898 row_type const row = cellRow(cell);
900 for(row_type r = row; r < row + span ; ++r) {
901 h += rowAscent(r) + rowDescent(r);
902 if (r != row + span - 1)
903 h += interRowSpace(r + 1);
910 bool Tabular::updateColumnWidths()
913 // for each col get max of single col cells
914 for(col_type c = 0; c < ncols(); ++c) {
916 for(row_type r = 0; r < nrows(); ++r) {
917 idx_type const i = cellIndex(r, c);
918 if (columnSpan(i) == 1)
919 new_width = max(new_width, cellInfo(i).width);
922 if (column_info[c].width != new_width) {
923 column_info[c].width = new_width;
927 // update col widths to fit merged cells
928 for(col_type c = 0; c < ncols(); ++c)
929 for(row_type r = 0; r < nrows(); ++r) {
930 idx_type const i = cellIndex(r, c);
931 int const span = columnSpan(i);
932 if (span == 1 || c > cellColumn(i))
936 for(col_type j = c; j < c + span ; ++j)
937 old_width += column_info[j].width;
939 if (cellInfo(i).width > old_width) {
940 column_info[c + span - 1].width += cellInfo(i).width - old_width;
949 int Tabular::width() const
952 for (col_type c = 0; c < ncols(); ++c)
953 width += column_info[c].width;
958 void Tabular::setCellWidth(idx_type cell, int new_width)
960 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
961 + interColumnSpace(cell);
965 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
968 if (!isMultiColumn(cell) || onlycolumn)
969 column_info[cellColumn(cell)].alignment = align;
971 cellInfo(cell).alignment = align;
972 cellInset(cell).get()->setContentAlignment(align);
976 void Tabular::setVAlignment(idx_type cell, VAlignment align,
979 if (!isMultiColumn(cell) || onlycolumn)
980 column_info[cellColumn(cell)].valignment = align;
982 cellInfo(cell).valignment = align;
989 * Allow line and paragraph breaks for fixed width cells or disallow them,
990 * merge cell paragraphs and reset layout to standard for variable width
993 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
995 inset->setAutoBreakRows(fixedWidth);
996 inset->toggleFixedWidth(fixedWidth);
1000 // merge all paragraphs to one
1001 BufferParams const & bp = cur.bv().buffer().params();
1002 while (inset->paragraphs().size() > 1)
1003 mergeParagraph(bp, inset->paragraphs(), 0);
1007 // undo information has already been recorded
1008 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1009 bp.documentClass().plainLayoutName());
1016 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1017 Length const & width)
1019 col_type const c = cellColumn(cell);
1021 column_info[c].p_width = width;
1022 // reset the vertical alignment to top if the fixed with
1023 // is removed or zero because only fixed width columns can
1024 // have a vertical alignment
1025 if (column_info[c].p_width.zero())
1026 column_info[c].valignment = LYX_VALIGN_TOP;
1027 for (row_type r = 0; r < nrows(); ++r) {
1028 idx_type const cell = cellIndex(r, c);
1029 // because of multicolumns
1030 toggleFixedWidth(cur, cellInset(cell).get(),
1031 !getPWidth(cell).zero());
1033 // cur paragraph can become invalid after paragraphs were merged
1034 if (cur.pit() > cur.lastpit())
1035 cur.pit() = cur.lastpit();
1036 // cur position can become invalid after newlines were removed
1037 if (cur.pos() > cur.lastpos())
1038 cur.pos() = cur.lastpos();
1042 bool Tabular::setFixedWidth(row_type r, col_type c)
1044 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1045 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1046 || (multicol && !cell_info[r][c].p_width.zero());
1047 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1052 void Tabular::updateContentAlignment(row_type r, col_type c)
1054 cell_info[r][c].inset->setContentAlignment(
1055 getAlignment(cellIndex(r, c)));
1059 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1060 Length const & width)
1062 if (!isMultiColumn(cell))
1065 cellInfo(cell).p_width = width;
1066 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1067 // cur paragraph can become invalid after paragraphs were merged
1068 if (cur.pit() > cur.lastpit())
1069 cur.pit() = cur.lastpit();
1070 // cur position can become invalid after newlines were removed
1071 if (cur.pos() > cur.lastpos())
1072 cur.pos() = cur.lastpos();
1077 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1078 Tabular::Feature what)
1080 if (what == SET_SPECIAL_MULTICOLUMN)
1081 cellInfo(cell).align_special = special;
1083 column_info[cellColumn(cell)].align_special = special;
1087 void Tabular::setTopLine(idx_type i, bool line)
1089 cellInfo(i).top_line = line;
1093 void Tabular::setBottomLine(idx_type i, bool line)
1095 cellInfo(i).bottom_line = line;
1099 void Tabular::setLeftLine(idx_type cell, bool line)
1101 cellInfo(cell).left_line = line;
1105 void Tabular::setRightLine(idx_type cell, bool line)
1107 cellInfo(cell).right_line = line;
1110 bool Tabular::rowTopLine(row_type r) const
1112 bool all_rows_set = true;
1113 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1114 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1115 return all_rows_set;
1119 bool Tabular::rowBottomLine(row_type r) const
1121 bool all_rows_set = true;
1122 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1123 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1124 return all_rows_set;
1128 bool Tabular::columnLeftLine(col_type c) const
1135 for (row_type r = 0; r < nrows(); ++r) {
1136 idx_type const i = cellIndex(r, c);
1137 if (c == cellColumn(i)) {
1139 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1140 if (cellInfo(i).left_line || right)
1144 return 2 * nrows_left >= total;
1148 bool Tabular::columnRightLine(col_type c) const
1153 int nrows_right = 0;
1155 for (row_type r = 0; r < nrows(); ++r) {
1156 idx_type i = cellIndex(r, c);
1157 if (c == cellColumn(i) + columnSpan(i) - 1) {
1159 bool left = (c + 1 < ncols()
1160 && cellInfo(cellIndex(r, c + 1)).left_line)
1161 || c + 1 == ncols();
1162 if (cellInfo(i).right_line && left)
1166 return 2 * nrows_right >= total;
1170 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1172 if (!onlycolumn && isMultiColumn(cell))
1173 return cellInfo(cell).alignment;
1174 return column_info[cellColumn(cell)].alignment;
1179 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1181 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1182 return cellInfo(cell).valignment;
1183 return column_info[cellColumn(cell)].valignment;
1187 Length const Tabular::getPWidth(idx_type cell) const
1189 if (isMultiColumn(cell))
1190 return cellInfo(cell).p_width;
1191 return column_info[cellColumn(cell)].p_width;
1195 int Tabular::cellWidth(idx_type cell) const
1197 return cellInfo(cell).width;
1201 int Tabular::textHOffset(idx_type cell) const
1203 // the LaTeX Way :-(
1204 int x = WIDTH_OF_LINE;
1206 switch (getAlignment(cell)) {
1207 case LYX_ALIGN_CENTER:
1208 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1210 case LYX_ALIGN_RIGHT:
1211 x += columnWidth(cell) - cellWidth(cell);
1212 // + interColumnSpace(cell);
1215 // LYX_ALIGN_LEFT: nothing :-)
1223 int Tabular::textVOffset(idx_type cell) const
1225 return cellInfo(cell).voffset;
1229 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1232 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1234 return cell_info[row][c].cellno;
1238 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1240 col_type c = ncols() - 1;
1241 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1242 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1244 return cell_info[row][c].cellno;
1248 Tabular::row_type Tabular::cellRow(idx_type cell) const
1250 if (cell >= numberofcells)
1254 return rowofcell[cell];
1258 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1260 if (cell >= numberofcells)
1264 return columnofcell[cell];
1268 void Tabular::write(ostream & os) const
1272 << write_attribute("version", 3)
1273 << write_attribute("rows", nrows())
1274 << write_attribute("columns", ncols())
1276 // global longtable options
1278 << write_attribute("rotate", rotate)
1279 << write_attribute("booktabs", use_booktabs)
1280 << write_attribute("islongtable", is_long_tabular)
1281 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1282 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1283 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1284 << write_attribute("headTopDL", endhead.topDL)
1285 << write_attribute("headBottomDL", endhead.bottomDL)
1286 << write_attribute("footTopDL", endfoot.topDL)
1287 << write_attribute("footBottomDL", endfoot.bottomDL)
1288 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1289 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1290 << write_attribute("lastFootEmpty", endlastfoot.empty);
1291 // longtables cannot be aligned vertically
1292 if (!is_long_tabular)
1293 os << write_attribute("tabularvalignment", tabular_valignment);
1294 if (is_long_tabular)
1295 os << write_attribute("longtabularalignment",
1296 longtabular_alignment);
1298 for (col_type c = 0; c < ncols(); ++c) {
1300 << write_attribute("alignment", column_info[c].alignment)
1301 << write_attribute("valignment", column_info[c].valignment)
1302 << write_attribute("width", column_info[c].p_width.asString())
1303 << write_attribute("special", column_info[c].align_special)
1306 for (row_type r = 0; r < nrows(); ++r) {
1307 static const string def("default");
1309 if (row_info[r].top_space_default)
1310 os << write_attribute("topspace", def);
1312 os << write_attribute("topspace", row_info[r].top_space);
1313 if (row_info[r].bottom_space_default)
1314 os << write_attribute("bottomspace", def);
1316 os << write_attribute("bottomspace", row_info[r].bottom_space);
1317 if (row_info[r].interline_space_default)
1318 os << write_attribute("interlinespace", def);
1320 os << write_attribute("interlinespace", row_info[r].interline_space);
1321 os << write_attribute("endhead", row_info[r].endhead)
1322 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1323 << write_attribute("endfoot", row_info[r].endfoot)
1324 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1325 << write_attribute("newpage", row_info[r].newpage)
1326 << write_attribute("caption", row_info[r].caption)
1328 for (col_type c = 0; c < ncols(); ++c) {
1330 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1331 << write_attribute("multirow", cell_info[r][c].multirow)
1332 << write_attribute("alignment", cell_info[r][c].alignment)
1333 << write_attribute("valignment", cell_info[r][c].valignment)
1334 << write_attribute("topline", cell_info[r][c].top_line)
1335 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1336 << write_attribute("leftline", cell_info[r][c].left_line)
1337 << write_attribute("rightline", cell_info[r][c].right_line)
1338 << write_attribute("rotate", cell_info[r][c].rotate)
1339 << write_attribute("usebox", cell_info[r][c].usebox)
1340 << write_attribute("width", cell_info[r][c].p_width)
1341 << write_attribute("special", cell_info[r][c].align_special)
1343 os << "\\begin_inset ";
1344 cell_info[r][c].inset->write(os);
1345 os << "\n\\end_inset\n"
1350 os << "</lyxtabular>\n";
1354 void Tabular::read(Lexer & lex)
1357 istream & is = lex.getStream();
1359 l_getline(is, line);
1360 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1361 LASSERT(false, /**/);
1366 if (!getTokenValue(line, "version", version))
1368 LASSERT(version >= 2, /**/);
1371 if (!getTokenValue(line, "rows", rows_arg))
1374 if (!getTokenValue(line, "columns", columns_arg))
1376 init(buffer_, rows_arg, columns_arg);
1377 l_getline(is, line);
1378 if (!prefixIs(line, "<features")) {
1379 lyxerr << "Wrong tabular format (expected <features ...> got"
1380 << line << ')' << endl;
1383 getTokenValue(line, "rotate", rotate);
1384 getTokenValue(line, "booktabs", use_booktabs);
1385 getTokenValue(line, "islongtable", is_long_tabular);
1386 getTokenValue(line, "tabularvalignment", tabular_valignment);
1387 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1388 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1389 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1390 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1391 getTokenValue(line, "headTopDL", endhead.topDL);
1392 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1393 getTokenValue(line, "footTopDL", endfoot.topDL);
1394 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1395 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1396 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1397 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1399 for (col_type c = 0; c < ncols(); ++c) {
1401 if (!prefixIs(line,"<column")) {
1402 lyxerr << "Wrong tabular format (expected <column ...> got"
1403 << line << ')' << endl;
1406 getTokenValue(line, "alignment", column_info[c].alignment);
1407 getTokenValue(line, "valignment", column_info[c].valignment);
1408 getTokenValue(line, "width", column_info[c].p_width);
1409 getTokenValue(line, "special", column_info[c].align_special);
1412 for (row_type i = 0; i < nrows(); ++i) {
1413 l_getline(is, line);
1414 if (!prefixIs(line, "<row")) {
1415 lyxerr << "Wrong tabular format (expected <row ...> got"
1416 << line << ')' << endl;
1419 getTokenValue(line, "topspace", row_info[i].top_space,
1420 row_info[i].top_space_default);
1421 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1422 row_info[i].bottom_space_default);
1423 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1424 row_info[i].interline_space_default);
1425 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1426 getTokenValue(line, "endhead", row_info[i].endhead);
1427 getTokenValue(line, "endfoot", row_info[i].endfoot);
1428 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1429 getTokenValue(line, "newpage", row_info[i].newpage);
1430 getTokenValue(line, "caption", row_info[i].caption);
1431 for (col_type j = 0; j < ncols(); ++j) {
1432 l_getline(is, line);
1433 if (!prefixIs(line, "<cell")) {
1434 lyxerr << "Wrong tabular format (expected <cell ...> got"
1435 << line << ')' << endl;
1438 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1439 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1440 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1441 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1442 getTokenValue(line, "topline", cell_info[i][j].top_line);
1443 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1444 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1445 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1446 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1447 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1448 getTokenValue(line, "width", cell_info[i][j].p_width);
1450 getTokenValue(line, "special", cell_info[i][j].align_special);
1451 l_getline(is, line);
1452 if (prefixIs(line, "\\begin_inset")) {
1453 cell_info[i][j].inset->setBuffer(*buffer_);
1454 cell_info[i][j].inset->read(lex);
1455 l_getline(is, line);
1457 if (!prefixIs(line, "</cell>")) {
1458 lyxerr << "Wrong tabular format (expected </cell> got"
1459 << line << ')' << endl;
1463 l_getline(is, line);
1464 if (!prefixIs(line, "</row>")) {
1465 lyxerr << "Wrong tabular format (expected </row> got"
1466 << line << ')' << endl;
1470 while (!prefixIs(line, "</lyxtabular>")) {
1471 l_getline(is, line);
1477 bool Tabular::isMultiColumn(idx_type cell) const
1479 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1480 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1484 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1486 return cell_info[cellRow(cell)][cellColumn(cell)];
1490 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1492 idx_type const col = cellColumn(cell);
1493 idx_type const row = cellRow(cell);
1494 for (idx_type i = 0; i < number; ++i)
1495 unsetMultiRow(cellIndex(row, col + i));
1497 // unsetting of multirow may have invalidated cell index
1498 cell = cellIndex(row, col);
1499 CellData & cs = cellInfo(cell);
1500 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1501 cs.alignment = column_info[col].alignment;
1503 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1505 for (idx_type i = 1; i < number; ++i) {
1506 CellData & cs1 = cellInfo(cell + i);
1507 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1508 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1516 bool Tabular::isMultiRow(idx_type cell) const
1518 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1519 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1523 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1525 idx_type const col = cellColumn(cell);
1526 idx_type const row = cellRow(cell);
1527 for (idx_type i = 0; i < number; ++i)
1528 unsetMultiColumn(cellIndex(row + i, col));
1530 // unsetting of multicol may have invalidated cell index
1531 cell = cellIndex(row, col);
1532 CellData & cs = cellInfo(cell);
1533 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1534 cs.valignment = LYX_VALIGN_MIDDLE;
1535 // FIXME: the horizontal alignment of multirow cells can only
1536 // be changed for the whole table,
1537 // support for this needs to be implemented but us a fileformat
1538 // change (assigning this to uwestoehr)
1539 // until LyX supports this, use the deault alignment of multirow
1541 cs.alignment = LYX_ALIGN_LEFT;
1543 // set the bottom row of the last selected cell
1544 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1546 for (idx_type i = 1; i < number; ++i) {
1547 CellData & cs1 = cell_info[row + i][col];
1548 cs1.multirow = CELL_PART_OF_MULTIROW;
1549 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1557 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1559 row_type const row = cellRow(cell);
1560 col_type column = cellColumn(cell) + 1;
1561 while (column < ncols() && isPartOfMultiColumn(row, column))
1564 return column - cellColumn(cell);
1568 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1570 col_type const column = cellColumn(cell);
1571 col_type row = cellRow(cell) + 1;
1572 while (row < nrows() && isPartOfMultiRow(row, column))
1575 return row - cellRow(cell);
1579 void Tabular::unsetMultiColumn(idx_type cell)
1581 if (!isMultiColumn(cell))
1584 row_type const row = cellRow(cell);
1585 col_type const col = cellColumn(cell);
1586 row_type const span = columnSpan(cell);
1587 for (col_type c = 0; c < span; ++c)
1588 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1593 void Tabular::unsetMultiRow(idx_type cell)
1595 if (!isMultiRow(cell))
1598 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1599 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1600 row_type const row = cellRow(cell);
1601 col_type const col = cellColumn(cell);
1602 row_type const span = rowSpan(cell);
1603 for (row_type r = 0; r < span; ++r)
1604 cell_info[row + r][col].multirow = CELL_NORMAL;
1609 void Tabular::setRotateCell(idx_type cell, bool flag)
1611 cellInfo(cell).rotate = flag;
1615 bool Tabular::getRotateCell(idx_type cell) const
1617 return cellInfo(cell).rotate;
1621 bool Tabular::needRotating() const
1625 for (row_type r = 0; r < nrows(); ++r)
1626 for (col_type c = 0; c < ncols(); ++c)
1627 if (cell_info[r][c].rotate)
1633 bool Tabular::isLastCell(idx_type cell) const
1635 if (cell + 1 < numberofcells)
1641 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1643 if (cellRow(cell) == 0)
1646 col_type const col = cellColumn(cell);
1647 row_type r = cellRow(cell) - 1;
1648 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1651 return cell_info[r][col].cellno;
1655 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1657 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1658 if (nextrow < nrows())
1659 return cell_info[nextrow][cellColumn(cell)].cellno;
1664 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1666 LASSERT(column != npos && column < ncols()
1667 && row != npos && row < nrows(), /**/);
1668 return cell_info[row][column].cellno;
1672 void Tabular::setUsebox(idx_type cell, BoxType type)
1674 cellInfo(cell).usebox = type;
1678 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1679 // adds line breaks, see bug 4886.
1680 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1682 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1683 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1685 if (cellInfo(cell).usebox > 1)
1686 return cellInfo(cell).usebox;
1687 return useParbox(cell);
1692 // This are functions used for the longtable support
1694 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1700 row_info[row].endfirsthead = flag;
1704 row_info[row].endhead = flag;
1709 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1712 hd.set = haveLTHead();
1713 return row_info[row].endhead;
1717 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1720 hd.set = haveLTFirstHead();
1721 return row_info[row].endfirsthead;
1725 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1731 row_info[row].endlastfoot = flag;
1735 row_info[row].endfoot = flag;
1740 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1743 fd.set = haveLTFoot();
1744 return row_info[row].endfoot;
1748 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1751 fd.set = haveLTLastFoot();
1752 return row_info[row].endlastfoot;
1756 void Tabular::setLTNewPage(row_type row, bool what)
1758 row_info[row].newpage = what;
1762 bool Tabular::getLTNewPage(row_type row) const
1764 return row_info[row].newpage;
1768 bool Tabular::haveLTHead() const
1770 for (row_type i = 0; i < nrows(); ++i)
1771 if (row_info[i].endhead)
1777 bool Tabular::haveLTFirstHead() const
1779 if (endfirsthead.empty)
1781 for (row_type r = 0; r < nrows(); ++r)
1782 if (row_info[r].endfirsthead)
1788 bool Tabular::haveLTFoot() const
1790 for (row_type r = 0; r < nrows(); ++r)
1791 if (row_info[r].endfoot)
1797 bool Tabular::haveLTLastFoot() const
1799 if (endlastfoot.empty)
1801 for (row_type r = 0; r < nrows(); ++r)
1802 if (row_info[r].endlastfoot)
1808 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1810 idx_type i = getFirstCellInRow(row);
1812 setMultiColumn(i, numberOfCellsInRow(row));
1813 setTopLine(i, false);
1814 setBottomLine(i, false);
1815 setLeftLine(i, false);
1816 setRightLine(i, false);
1818 unsetMultiColumn(i);
1819 // When unsetting a caption row, also all existing
1820 // captions in this row must be dissolved.
1822 row_info[row].caption = what;
1827 bool Tabular::ltCaption(row_type row) const
1829 return row_info[row].caption;
1833 bool Tabular::haveLTCaption() const
1835 for (row_type r = 0; r < nrows(); ++r)
1836 if (row_info[r].caption)
1842 // end longtable support functions
1844 void Tabular::setRowAscent(row_type row, int height)
1846 if (row >= nrows() || row_info[row].ascent == height)
1848 row_info[row].ascent = height;
1852 void Tabular::setRowDescent(row_type row, int height)
1854 if (row >= nrows() || row_info[row].descent == height)
1856 row_info[row].descent = height;
1860 int Tabular::rowAscent(row_type row) const
1862 LASSERT(row < nrows(), /**/);
1863 return row_info[row].ascent;
1867 int Tabular::rowDescent(row_type row) const
1869 LASSERT(row < nrows(), /**/);
1870 return row_info[row].descent;
1874 int Tabular::height() const
1877 for (row_type row = 0; row < nrows(); ++row)
1878 height += rowAscent(row) + rowDescent(row) +
1884 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1886 LASSERT(row < nrows(), /**/);
1887 LASSERT(column < ncols(), /**/);
1888 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1892 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1894 LASSERT(row < nrows(), /**/);
1895 LASSERT(column < ncols(), /**/);
1896 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1900 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1902 // we only output complete row lines and the 1st row here, the rest
1903 // is done in Tabular::TeXBottomHLine(...)
1905 // get for each column the topline (if any)
1906 vector<bool> topline;
1908 for (col_type c = 0; c < ncols(); ++c) {
1909 topline.push_back(topLine(cellIndex(row, c)));
1910 // If cell is part of a multirow and not the first cell of the
1911 // multirow, no line must be drawn.
1913 if (isMultiRow(cellIndex(row, c))
1914 && isMultiRow(cellIndex(row - 1, c)))
1920 // do nothing if empty first row, or incomplete row line after
1921 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
1924 // only output complete row lines and the 1st row's clines
1925 if (nset == ncols()) {
1927 os << (row == 0 ? "\\toprule " : "\\midrule ");
1931 } else if (row == 0) {
1932 for (col_type c = 0; c < ncols(); ++c) {
1934 //babel makes the "-" character an active one, so we have to suppress this here
1935 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1936 if (lang == "slovak" || lang == "czech")
1937 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1938 "\\expandafter\\cline\\expandafter{\\expandafter")
1939 << c + 1 << "\\string-";
1941 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1942 // get to last column of line span
1943 while (c < ncols() && topline[c])
1954 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1956 // we output bottomlines of row r and the toplines of row r+1
1957 // if the latter do not span the whole tabular
1959 // get the bottomlines of row r, and toplines in next row
1960 bool lastrow = row == nrows() - 1;
1961 vector<bool> bottomline, topline;
1962 bool nextrowset = true;
1963 for (col_type c = 0; c < ncols(); ++c) {
1964 bottomline.push_back(bottomLine(cellIndex(row, c)));
1965 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1966 // If cell is part of a multirow and not the last or first cell of the
1967 // multirow, no line must be drawn.
1969 if (isMultiRow(cellIndex(row, c))
1970 && isMultiRow(cellIndex(row + 1, c))) {
1971 bottomline[c] = false;
1974 nextrowset &= topline[c];
1977 // combine this row's bottom lines and next row's toplines if necessary
1979 for (col_type c = 0; c < ncols(); ++c) {
1981 bottomline[c] = bottomline[c] || topline[c];
1986 // do nothing if empty, OR incomplete row line with a topline in next row
1987 if (nset == 0 || (nextrowset && nset != ncols()))
1990 if (nset == ncols()) {
1992 os << (lastrow ? "\\bottomrule" : "\\midrule");
1996 for (col_type c = 0; c < ncols(); ++c) {
1997 if (bottomline[c]) {
1998 //babel makes the "-" character an active one, so we have to suppress this here
1999 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2000 if (lang == "slovak" || lang == "czech")
2001 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2002 "\\expandafter\\cline\\expandafter{\\expandafter")
2003 << c + 1 << "\\string-";
2005 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2006 // get to last column of line span
2007 while (c < ncols() && bottomline[c])
2018 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2019 bool & ismulticol, bool & ismultirow) const
2022 row_type const r = cellRow(cell);
2023 if (is_long_tabular && row_info[r].caption)
2026 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2027 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2028 // figure out how to set the lines
2029 // we always set double lines to the right of the cell
2030 col_type const c = cellColumn(cell);
2031 col_type const nextcol = c + columnSpan(cell);
2032 bool colright = columnRightLine(c);
2033 bool colleft = columnLeftLine(c);
2034 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2035 bool nextcellleft = nextcol < ncols()
2036 && leftLine(cellIndex(r, nextcol));
2037 bool coldouble = colright && nextcolleft;
2038 bool celldouble = rightLine(cell) && nextcellleft;
2040 ismulticol = isMultiColumn(cell)
2041 || (c == 0 && colleft != leftLine(cell))
2042 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2043 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2044 || (coldouble != celldouble);
2046 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2047 if (c ==0 && leftLine(cell))
2049 if (!cellInfo(cell).align_special.empty()) {
2050 os << cellInfo(cell).align_special;
2052 if (!getPWidth(cell).zero()) {
2054 case LYX_ALIGN_LEFT:
2055 os << ">{\\raggedright}";
2057 case LYX_ALIGN_RIGHT:
2058 os << ">{\\raggedleft}";
2060 case LYX_ALIGN_CENTER:
2061 os << ">{\\centering}";
2067 case LYX_VALIGN_TOP:
2070 case LYX_VALIGN_MIDDLE:
2073 case LYX_VALIGN_BOTTOM:
2078 << from_ascii(getPWidth(cell).asLatexString())
2082 case LYX_ALIGN_LEFT:
2085 case LYX_ALIGN_RIGHT:
2092 } // end if else !getPWidth
2093 } // end if else !cellinfo_of_cell
2094 if (rightLine(cell) || nextcellleft)
2097 // add extra vertical line if we want a double one
2100 } // end if ismulticol
2102 // we only need code for the first multirow cell
2103 ismultirow = isMultiRow(cell);
2105 os << "\\multirow{" << rowSpan(cell) << "}{";
2106 if (!getPWidth(cell).zero())
2107 os << from_ascii(getPWidth(cell).asLatexString());
2109 // we need to set a default value
2110 // needs to be discussed
2113 } // end if ismultirow
2115 if (getRotateCell(cell)) {
2116 os << "\\begin{sideways}\n";
2119 if (getUsebox(cell) == BOX_PARBOX) {
2122 case LYX_VALIGN_TOP:
2125 case LYX_VALIGN_MIDDLE:
2128 case LYX_VALIGN_BOTTOM:
2132 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2134 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2135 os << "\\begin{minipage}[";
2137 case LYX_VALIGN_TOP:
2140 case LYX_VALIGN_MIDDLE:
2143 case LYX_VALIGN_BOTTOM:
2147 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2155 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2156 bool ismulticol, bool ismultirow) const
2159 row_type const r = cellRow(cell);
2160 if (is_long_tabular && row_info[r].caption)
2164 if (getUsebox(cell) == BOX_PARBOX)
2166 else if (getUsebox(cell) == BOX_MINIPAGE) {
2167 os << "%\n\\end{minipage}";
2170 if (getRotateCell(cell)) {
2171 os << "%\n\\end{sideways}";
2174 if (ismulticol || ismultirow) {
2181 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2182 OutputParams const & runparams) const
2184 if (!is_long_tabular)
2189 // the caption must be output before the headers
2190 if (haveLTCaption()) {
2191 for (row_type r = 0; r < nrows(); ++r) {
2192 if (row_info[r].caption) {
2193 ret += TeXRow(os, r, runparams);
2197 // output first header info
2198 // first header must be output before the header, otherwise the
2199 // correct caption placement becomes really weird
2200 if (haveLTFirstHead()) {
2201 if (endfirsthead.topDL) {
2205 for (row_type r = 0; r < nrows(); ++r) {
2206 if (row_info[r].endfirsthead) {
2207 ret += TeXRow(os, r, runparams);
2210 if (endfirsthead.bottomDL) {
2214 os << "\\endfirsthead\n";
2217 // output header info
2219 if (endfirsthead.empty && !haveLTFirstHead()) {
2220 os << "\\endfirsthead\n";
2223 if (endhead.topDL) {
2227 for (row_type r = 0; r < nrows(); ++r) {
2228 if (row_info[r].endhead) {
2229 ret += TeXRow(os, r, runparams);
2232 if (endhead.bottomDL) {
2236 os << "\\endhead\n";
2239 // output footer info
2241 if (endfoot.topDL) {
2245 for (row_type r = 0; r < nrows(); ++r) {
2246 if (row_info[r].endfoot) {
2247 ret += TeXRow(os, r, runparams);
2250 if (endfoot.bottomDL) {
2254 os << "\\endfoot\n";
2256 if (endlastfoot.empty && !haveLTLastFoot()) {
2257 os << "\\endlastfoot\n";
2261 // output lastfooter info
2262 if (haveLTLastFoot()) {
2263 if (endlastfoot.topDL) {
2267 for (row_type r = 0; r < nrows(); ++r) {
2268 if (row_info[r].endlastfoot) {
2269 ret += TeXRow(os, r, runparams);
2272 if (endlastfoot.bottomDL) {
2276 os << "\\endlastfoot\n";
2283 bool Tabular::isValidRow(row_type row) const
2285 if (!is_long_tabular)
2287 return !row_info[row].endhead && !row_info[row].endfirsthead
2288 && !row_info[row].endfoot && !row_info[row].endlastfoot
2289 && !row_info[row].caption;
2293 int Tabular::TeXRow(odocstream & os, row_type row,
2294 OutputParams const & runparams) const
2296 idx_type cell = cellIndex(row, 0);
2297 shared_ptr<InsetTableCell> inset = cellInset(cell);
2298 Paragraph const & par = inset->paragraphs().front();
2299 string const lang = par.getParLanguage(buffer().params())->lang();
2301 //output the top line
2302 int ret = TeXTopHLine(os, row, lang);
2304 if (row_info[row].top_space_default) {
2306 os << "\\addlinespace\n";
2308 os << "\\noalign{\\vskip\\doublerulesep}\n";
2310 } else if(!row_info[row].top_space.zero()) {
2312 os << "\\addlinespace["
2313 << from_ascii(row_info[row].top_space.asLatexString())
2316 os << "\\noalign{\\vskip"
2317 << from_ascii(row_info[row].top_space.asLatexString())
2322 bool ismulticol = false;
2323 bool ismultirow = false;
2324 for (col_type c = 0; c < ncols(); ++c) {
2325 if (isPartOfMultiRow(row, c))
2326 os << " & "; // we need to add a further column
2327 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2329 cell = cellIndex(row, c);
2330 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2331 shared_ptr<InsetTableCell> inset = cellInset(cell);
2333 Paragraph const & par = inset->paragraphs().front();
2334 bool rtl = par.isRTL(buffer().params())
2336 && getPWidth(cell).zero();
2340 par.getParLanguage(buffer().params())->lang();
2341 if (lang == "farsi")
2343 else if (lang == "arabic_arabi")
2345 // currently, remaning RTL languages are
2346 // arabic_arabtex and hebrew
2350 // pass to the OutputParams that we are in a cell and
2351 // which alignment we have set.
2352 // InsetNewline needs this context information.
2353 OutputParams newrp(runparams);
2354 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2355 ? OutputParams::PLAIN
2356 : OutputParams::ALIGNED;
2357 ret += inset->latex(os, newrp);
2358 runparams.encoding = newrp.encoding;
2362 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2363 if (cell != getLastCellInRow(row)) { // not last cell in row
2367 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2368 // if no first header and no empty first header is used,
2369 // the caption needs to be terminated by \endfirsthead
2371 os << "\\endfirsthead";
2373 os << "\\tabularnewline";
2374 if (row_info[row].bottom_space_default) {
2376 os << "\\addlinespace";
2378 os << "[\\doublerulesep]";
2379 } else if (!row_info[row].bottom_space.zero()) {
2381 os << "\\addlinespace";
2383 << from_ascii(row_info[row].bottom_space.asLatexString())
2389 //output the bottom line
2390 ret += TeXBottomHLine(os, row, lang);
2392 if (row_info[row].interline_space_default) {
2394 os << "\\addlinespace\n";
2396 os << "\\noalign{\\vskip\\doublerulesep}\n";
2398 } else if (!row_info[row].interline_space.zero()) {
2400 os << "\\addlinespace["
2401 << from_ascii(row_info[row].interline_space.asLatexString())
2404 os << "\\noalign{\\vskip"
2405 << from_ascii(row_info[row].interline_space.asLatexString())
2413 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2417 //+---------------------------------------------------------------------
2418 //+ first the opening preamble +
2419 //+---------------------------------------------------------------------
2422 os << "\\begin{sideways}\n";
2425 if (is_long_tabular) {
2426 os << "\\begin{longtable}";
2427 switch (longtabular_alignment) {
2428 case LYX_LONGTABULAR_ALIGN_LEFT:
2431 case LYX_LONGTABULAR_ALIGN_CENTER:
2433 case LYX_LONGTABULAR_ALIGN_RIGHT:
2438 os << "\\begin{tabular}";
2439 switch (tabular_valignment) {
2440 case LYX_VALIGN_TOP:
2443 case LYX_VALIGN_MIDDLE:
2445 case LYX_VALIGN_BOTTOM:
2453 for (col_type c = 0; c < ncols(); ++c) {
2454 if (columnLeftLine(c))
2456 if (!column_info[c].align_special.empty()) {
2457 os << column_info[c].align_special;
2459 if (!column_info[c].p_width.zero()) {
2460 switch (column_info[c].alignment) {
2461 case LYX_ALIGN_LEFT:
2462 os << ">{\\raggedright}";
2464 case LYX_ALIGN_RIGHT:
2465 os << ">{\\raggedleft}";
2467 case LYX_ALIGN_CENTER:
2468 os << ">{\\centering}";
2470 case LYX_ALIGN_NONE:
2471 case LYX_ALIGN_BLOCK:
2472 case LYX_ALIGN_LAYOUT:
2473 case LYX_ALIGN_SPECIAL:
2477 switch (column_info[c].valignment) {
2478 case LYX_VALIGN_TOP:
2481 case LYX_VALIGN_MIDDLE:
2484 case LYX_VALIGN_BOTTOM:
2489 << from_ascii(column_info[c].p_width.asLatexString())
2492 switch (column_info[c].alignment) {
2493 case LYX_ALIGN_LEFT:
2496 case LYX_ALIGN_RIGHT:
2503 } // end if else !column_info[i].p_width
2504 } // end if else !column_info[i].align_special
2505 if (columnRightLine(c))
2511 ret += TeXLongtableHeaderFooter(os, runparams);
2513 //+---------------------------------------------------------------------
2514 //+ the single row and columns (cells) +
2515 //+---------------------------------------------------------------------
2517 for (row_type r = 0; r < nrows(); ++r) {
2518 if (isValidRow(r)) {
2519 ret += TeXRow(os, r, runparams);
2520 if (is_long_tabular && row_info[r].newpage) {
2521 os << "\\newpage\n";
2527 //+---------------------------------------------------------------------
2528 //+ the closing of the tabular +
2529 //+---------------------------------------------------------------------
2531 if (is_long_tabular)
2532 os << "\\end{longtable}";
2534 os << "\\end{tabular}";
2536 os << "\n\\end{sideways}";
2544 int Tabular::docbookRow(odocstream & os, row_type row,
2545 OutputParams const & runparams) const
2548 idx_type cell = getFirstCellInRow(row);
2551 for (col_type c = 0; c < ncols(); ++c) {
2552 if (isPartOfMultiColumn(row, c))
2555 os << "<entry align=\"";
2556 switch (getAlignment(cell)) {
2557 case LYX_ALIGN_LEFT:
2560 case LYX_ALIGN_RIGHT:
2568 os << "\" valign=\"";
2569 switch (getVAlignment(cell)) {
2570 case LYX_VALIGN_TOP:
2573 case LYX_VALIGN_BOTTOM:
2576 case LYX_VALIGN_MIDDLE:
2581 if (isMultiColumn(cell)) {
2582 os << " namest=\"col" << c << "\" ";
2583 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2587 ret += cellInset(cell)->docbook(os, runparams);
2596 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2600 //+---------------------------------------------------------------------
2601 //+ first the opening preamble +
2602 //+---------------------------------------------------------------------
2604 os << "<tgroup cols=\"" << ncols()
2605 << "\" colsep=\"1\" rowsep=\"1\">\n";
2607 for (col_type c = 0; c < ncols(); ++c) {
2608 os << "<colspec colname=\"col" << c << "\" align=\"";
2609 switch (column_info[c].alignment) {
2610 case LYX_ALIGN_LEFT:
2613 case LYX_ALIGN_RIGHT:
2621 if (runparams.flavor == OutputParams::XML)
2627 //+---------------------------------------------------------------------
2628 //+ Long Tabular case +
2629 //+---------------------------------------------------------------------
2631 // output caption info
2632 if (haveLTCaption()) {
2633 os << "<caption>\n";
2635 for (row_type r = 0; r < nrows(); ++r) {
2636 if (row_info[r].caption) {
2637 ret += docbookRow(os, r, runparams);
2640 os << "</caption>\n";
2643 // output header info
2644 if (haveLTHead() || haveLTFirstHead()) {
2647 for (row_type r = 0; r < nrows(); ++r) {
2648 if (row_info[r].endhead || row_info[r].endfirsthead) {
2649 ret += docbookRow(os, r, runparams);
2655 // output footer info
2656 if (haveLTFoot() || haveLTLastFoot()) {
2659 for (row_type r = 0; r < nrows(); ++r) {
2660 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2661 ret += docbookRow(os, r, runparams);
2668 //+---------------------------------------------------------------------
2669 //+ the single row and columns (cells) +
2670 //+---------------------------------------------------------------------
2674 for (row_type r = 0; r < nrows(); ++r) {
2675 if (isValidRow(r)) {
2676 ret += docbookRow(os, r, runparams);
2681 //+---------------------------------------------------------------------
2682 //+ the closing of the tabular +
2683 //+---------------------------------------------------------------------
2692 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2693 OutputParams const & runparams) const
2696 idx_type cell = getFirstCellInRow(row);
2698 xs << html::StartTag("tr");
2699 for (col_type c = 0; c < ncols(); ++c) {
2700 if (isPartOfMultiColumn(row, c))
2705 switch (getAlignment(cell)) {
2706 case LYX_ALIGN_LEFT:
2709 case LYX_ALIGN_RIGHT:
2717 attr << " valign='";
2718 switch (getVAlignment(cell)) {
2719 case LYX_VALIGN_TOP:
2722 case LYX_VALIGN_BOTTOM:
2725 case LYX_VALIGN_MIDDLE:
2730 if (isMultiColumn(cell))
2731 attr << " colspan='" << columnSpan(cell) << "'";
2733 xs << html::StartTag("td", attr.str());
2734 ret += cellInset(cell)->xhtml(xs, runparams);
2735 xs << html::EndTag("td");
2738 xs << html::EndTag("tr");
2743 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2746 // It's unclear to me if we need to mess with the long table stuff.
2747 // We can borrow that too from docbook, if so.
2749 xs << html::StartTag("tbody");
2750 for (row_type r = 0; r < nrows(); ++r) {
2751 if (isValidRow(r)) {
2752 ret += xhtmlRow(xs, r, runparams);
2755 xs << html::EndTag("tbody");
2760 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2761 vector<unsigned int> const & clen) const
2763 idx_type const fcell = getFirstCellInRow(row);
2764 idx_type const n = numberOfCellsInRow(row) + fcell;
2767 for (idx_type i = fcell; i < n; ++i) {
2777 for (idx_type i = fcell; i < n; ++i) {
2788 col_type column = cellColumn(i);
2789 int len = clen[column];
2790 while (column < ncols() - 1
2791 && isPartOfMultiColumn(row, ++column))
2792 len += clen[column] + 4;
2793 os << docstring(len, ch);
2808 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2809 vector<unsigned int> const & clen) const
2811 idx_type const fcell = getFirstCellInRow(row);
2812 idx_type const n = numberOfCellsInRow(row) + fcell;
2815 for (idx_type i = fcell; i < n; ++i) {
2816 if (bottomLine(i)) {
2825 for (idx_type i = fcell; i < n; ++i) {
2826 if (bottomLine(i)) {
2836 col_type column = cellColumn(i);
2837 int len = clen[column];
2838 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2839 len += clen[column] + 4;
2840 os << docstring(len, ch);
2841 if (bottomLine(i)) {
2855 void Tabular::plaintextPrintCell(odocstream & os,
2856 OutputParams const & runparams,
2857 idx_type cell, row_type row, col_type column,
2858 vector<unsigned int> const & clen,
2859 bool onlydata) const
2861 odocstringstream sstr;
2862 cellInset(cell)->plaintext(sstr, runparams);
2874 unsigned int len1 = sstr.str().length();
2875 unsigned int len2 = clen[column];
2876 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2877 len2 += clen[column] + 4;
2880 switch (getAlignment(cell)) {
2882 case LYX_ALIGN_LEFT:
2885 case LYX_ALIGN_RIGHT:
2889 case LYX_ALIGN_CENTER:
2895 os << docstring(len1, ' ') << sstr.str()
2896 << docstring(len2, ' ');
2898 if (rightLine(cell))
2905 void Tabular::plaintext(odocstream & os,
2906 OutputParams const & runparams, int const depth,
2907 bool onlydata, char_type delim) const
2909 // first calculate the width of the single columns
2910 vector<unsigned int> clen(ncols());
2913 // first all non multicolumn cells!
2914 for (col_type c = 0; c < ncols(); ++c) {
2916 for (row_type r = 0; r < nrows(); ++r) {
2917 idx_type cell = cellIndex(r, c);
2918 if (isMultiColumn(cell))
2920 odocstringstream sstr;
2921 cellInset(cell)->plaintext(sstr, runparams);
2922 if (clen[c] < sstr.str().length())
2923 clen[c] = sstr.str().length();
2926 // then all multicolumn cells!
2927 for (col_type c = 0; c < ncols(); ++c) {
2928 for (row_type r = 0; r < nrows(); ++r) {
2929 idx_type cell = cellIndex(r, c);
2930 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
2932 odocstringstream sstr;
2933 cellInset(cell)->plaintext(sstr, runparams);
2934 int len = int(sstr.str().length());
2935 idx_type const n = columnSpan(cell);
2936 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
2938 if (len > int(clen[c + n - 1]))
2939 clen[c + n - 1] = len;
2944 for (row_type r = 0; r < nrows(); ++r) {
2945 if (!onlydata && plaintextTopHLine(os, r, clen))
2946 os << docstring(depth * 2, ' ');
2947 for (col_type c = 0; c < ncols(); ++c) {
2948 if (isPartOfMultiColumn(r, c))
2950 if (onlydata && c > 0)
2951 // we don't use operator<< for single UCS4 character.
2952 // see explanation in docstream.h
2954 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
2959 os << docstring(depth * 2, ' ');
2960 if (plaintextBottomHLine(os, r, clen))
2961 os << docstring(depth * 2, ' ');
2967 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2969 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2973 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2974 col_type column) const
2976 return cell_info[row][column].inset;
2980 void Tabular::setCellInset(row_type row, col_type column,
2981 shared_ptr<InsetTableCell> ins) const
2983 CellData & cd = cell_info[row][column];
2988 void Tabular::validate(LaTeXFeatures & features) const
2990 features.require("NeedTabularnewline");
2992 features.require("booktabs");
2993 if (is_long_tabular)
2994 features.require("longtable");
2996 features.require("rotating");
2997 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2998 if (isMultiRow(cell))
2999 features.require("multirow");
3000 if (getVAlignment(cell) != LYX_VALIGN_TOP
3001 || !getPWidth(cell).zero())
3002 features.require("array");
3003 cellInset(cell)->validate(features);
3008 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3010 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3011 ParagraphList::const_iterator cit = parlist.begin();
3012 ParagraphList::const_iterator end = parlist.end();
3014 for (; cit != end; ++cit)
3015 for (int i = 0; i < cit->size(); ++i)
3016 if (cit->isNewline(i))
3023 /////////////////////////////////////////////////////////////////////
3027 /////////////////////////////////////////////////////////////////////
3029 InsetTableCell::InsetTableCell(Buffer * buf)
3030 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3031 contentAlign(LYX_ALIGN_CENTER)
3035 bool InsetTableCell::forcePlainLayout(idx_type) const
3037 return !isFixedWidth;
3041 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3043 return isFixedWidth;
3047 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3048 FuncStatus & status) const
3051 switch (cmd.action) {
3053 enabled = !forcePlainLayout();
3055 case LFUN_LAYOUT_PARAGRAPH:
3056 enabled = allowParagraphCustomization();
3059 return InsetText::getStatus(cur, cmd, status);
3061 status.setEnabled(enabled);
3065 docstring InsetTableCell::asString(bool intoInsets)
3068 if (paragraphs().empty())
3070 ParagraphList::const_iterator it = paragraphs().begin();
3071 ParagraphList::const_iterator en = paragraphs().end();
3073 for (; it != en; ++it) {
3078 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3084 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3087 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3088 return InsetText::xhtml(xs, rp);
3093 /////////////////////////////////////////////////////////////////////
3097 /////////////////////////////////////////////////////////////////////
3099 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3101 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3102 rowselect_(false), colselect_(false)
3107 InsetTabular::InsetTabular(InsetTabular const & tab)
3108 : Inset(tab), tabular(tab.tabular), scx_(0)
3113 InsetTabular::~InsetTabular()
3115 hideDialogs("tabular", this);
3119 void InsetTabular::setBuffer(Buffer & buf)
3121 tabular.setBuffer(buf);
3122 Inset::setBuffer(buf);
3126 bool InsetTabular::insetAllowed(InsetCode code) const
3128 if (code == MATHMACRO_CODE
3129 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3136 void InsetTabular::write(ostream & os) const
3138 os << "Tabular" << endl;
3143 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3145 // FIXME: depending on the selection state, we could offer a different menu.
3146 return from_ascii("context-tabular");
3150 void InsetTabular::read(Lexer & lex)
3152 //bool const old_format = (lex.getString() == "\\LyXTable");
3160 string token = lex.getString();
3161 while (lex && token != "\\end_inset") {
3163 token = lex.getString();
3166 lex.printError("Missing \\end_inset at this point. ");
3170 int InsetTabular::rowFromY(Cursor & cur, int y) const
3172 // top y coordinate of tabular
3173 int h = yo(cur.bv()) - tabular.rowAscent(0);
3175 for (; r < tabular.nrows() && y > h; ++r)
3176 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3177 + tabular.interRowSpace(r);
3183 int InsetTabular::columnFromX(Cursor & cur, int x) const
3185 // left x coordinate of tabular
3186 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3188 for (; c < tabular.ncols() && x > w; ++c)
3189 w += tabular.columnWidth(c);
3194 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3196 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3197 // mi.base.textwidth << "\n";
3200 LASSERT(false, /**/);
3203 for (row_type r = 0; r < tabular.nrows(); ++r) {
3206 for (col_type c = 0; c < tabular.ncols(); ++c) {
3207 if (tabular.isPartOfMultiColumn(r, c)
3208 || tabular.isPartOfMultiRow(r, c))
3209 // multicolumn or multirow cell, but not first one
3211 idx_type const cell = tabular.cellIndex(r, c);
3215 if (tabular.cell_info[r][c].multicolumn ==
3216 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3217 p_width = tabular.cellInfo(cell).p_width;
3219 p_width = tabular.column_info[c].p_width;
3220 if (!p_width.zero())
3221 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3222 tabular.cellInset(cell)->metrics(m, dim);
3223 if (!p_width.zero())
3224 dim.wid = m.base.textwidth;
3225 tabular.setCellWidth(cell, dim.wid);
3227 // FIXME?: do we need a second metrics call
3228 // to get the descent of the text in last par?
3229 TextMetrics const & tm =
3230 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3231 int const backdes = tm.last().second->descent();
3233 switch (tabular.getVAlignment(cell)) {
3234 case Tabular::LYX_VALIGN_TOP:
3235 tabular.cell_info[r][c].voffset = 0;
3237 case Tabular::LYX_VALIGN_MIDDLE:
3238 tabular.cell_info[r][c].voffset = -(dim.des - backdes - TEXT_TO_INSET_OFFSET)/2;
3240 case Tabular::LYX_VALIGN_BOTTOM:
3241 tabular.cell_info[r][c].voffset = -(dim.des - backdes - TEXT_TO_INSET_OFFSET);
3244 maxAsc = max(maxAsc, dim.asc - tabular.cell_info[r][c].voffset);
3245 maxDesc = max(maxDesc, dim.des + tabular.cell_info[r][c].voffset);
3247 int const top_space = tabular.row_info[r].top_space_default ?
3248 default_line_space :
3249 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3250 tabular.setRowAscent(r, maxAsc + ADD_TO_HEIGHT + top_space);
3251 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3252 default_line_space :
3253 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3254 tabular.setRowDescent(r, maxDesc + ADD_TO_HEIGHT + bottom_space);
3256 tabular.updateColumnWidths();
3257 dim.asc = tabular.rowAscent(0);
3258 dim.des = tabular.height() - dim.asc;
3259 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3262 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3265 if (&cur.inset() == this && cur.selection()) {
3266 if (cur.selIsMultiCell()) {
3269 getSelection(cur, rs, re, cs, ce);
3271 idx_type const cell = tabular.cellIndex(row, col);
3272 col_type const cspan = tabular.columnSpan(cell);
3273 row_type const rspan = tabular.rowSpan(cell);
3274 if (col + cspan - 1 >= cs && col <= ce
3275 && row + rspan - 1 >= rs && row <= re)
3278 if (col == tabular.cellColumn(cur.idx())
3279 && row == tabular.cellRow(cur.idx())) {
3280 CursorSlice const & beg = cur.selBegin();
3281 CursorSlice const & end = cur.selEnd();
3283 if ((end.lastpos() > 0 || end.lastpit() > 0)
3284 && end.pos() == end.lastpos() && beg.pos() == 0
3285 && end.pit() == end.lastpit() && beg.pit() == 0)
3293 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3295 x += scx_ + ADD_TO_TABULAR_WIDTH;
3297 BufferView * bv = pi.base.bv;
3298 Cursor & cur = pi.base.bv->cursor();
3301 // FIXME: As the full background is painted in drawSelection(),
3302 // we have no choice but to do a full repaint for the Text cells.
3303 pi.full_repaint = true;
3305 bool const original_selection_state = pi.selected;
3308 first_visible_cell = Tabular::npos;
3309 for (row_type r = 0; r < tabular.nrows(); ++r) {
3311 for (col_type c = 0; c < tabular.ncols(); ++c) {
3312 if (tabular.isPartOfMultiColumn(r, c))
3315 idx = tabular.cellIndex(r, c);
3317 if (tabular.isPartOfMultiRow(r, c)) {
3318 nx += tabular.columnWidth(idx);
3322 if (first_visible_cell == Tabular::npos)
3323 first_visible_cell = idx;
3325 pi.selected |= isCellSelected(cur, r, c);
3326 int const cx = nx + tabular.textHOffset(idx);
3327 int const cy = y + tabular.textVOffset(idx);
3328 // Cache the Inset position.
3329 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3330 cell(idx)->draw(pi, cx, cy);
3331 drawCellLines(pi.pain, nx, y, r, idx, pi.change_);
3332 nx += tabular.columnWidth(idx);
3333 pi.selected = original_selection_state;
3336 if (r + 1 < tabular.nrows())
3337 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3338 + tabular.interRowSpace(r + 1);
3343 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3345 Cursor & cur = pi.base.bv->cursor();
3348 x += scx_ + ADD_TO_TABULAR_WIDTH;
3350 // FIXME: it is wrong to completely paint the background
3351 // if we want to do single row painting.
3353 // Paint background of current tabular
3354 int const w = tabular.width();
3355 int const h = tabular.height();
3356 int yy = y - tabular.rowAscent(0);
3357 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3359 if (!cur.selection())
3361 if (&cur.inset() != this)
3366 bool const full_cell_selected = isCellSelected(cur,
3367 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3369 if (cur.selIsMultiCell() || full_cell_selected) {
3370 for (row_type r = 0; r < tabular.nrows(); ++r) {
3372 for (col_type c = 0; c < tabular.ncols(); ++c) {
3373 if (tabular.isPartOfMultiColumn(r, c))
3376 idx_type const cell = tabular.cellIndex(r, c);
3378 if (tabular.isPartOfMultiRow(r, c)) {
3379 xx += tabular.columnWidth(cell);
3382 int const w = tabular.columnWidth(cell);
3383 int const h = tabular.cellHeight(cell);
3384 int const yy = y - tabular.rowAscent(r);
3385 if (isCellSelected(cur, r, c))
3386 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3389 if (r + 1 < tabular.nrows())
3390 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3391 + tabular.interRowSpace(r + 1);
3395 x += cellXPos(cur.idx());
3396 x += tabular.textHOffset(cur.idx());
3397 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3402 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3403 row_type row, idx_type cell, Change const & change) const
3405 y -= tabular.rowAscent(row);
3406 int const w = tabular.columnWidth(cell);
3407 int const h = tabular.cellHeight(cell);
3408 Color linecolor = change.changed() ? change.color() : Color_tabularline;
3409 Color gridcolor = change.changed() ? change.color() : Color_tabularonoffline;
3412 bool drawline = tabular.topLine(cell)
3413 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3414 pain.line(x, y, x + w, y,
3415 drawline ? linecolor : gridcolor,
3416 drawline ? Painter::line_solid : Painter::line_onoffdash);
3419 drawline = tabular.bottomLine(cell);
3420 pain.line(x, y + h, x + w, y + h,
3421 drawline ? linecolor : gridcolor,
3422 drawline ? Painter::line_solid : Painter::line_onoffdash);
3425 col_type const col = tabular.cellColumn(cell);
3426 drawline = tabular.leftLine(cell)
3427 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3428 pain.line(x, y, x, y + h,
3429 drawline ? linecolor : gridcolor,
3430 drawline ? Painter::line_solid : Painter::line_onoffdash);
3433 x -= tabular.interColumnSpace(cell);
3434 drawline = tabular.rightLine(cell)
3435 || (col + 1 < tabular.ncols()
3436 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3437 pain.line(x + w, y, x + w, y + h,
3438 drawline ? linecolor : gridcolor,
3439 drawline ? Painter::line_solid : Painter::line_onoffdash);
3443 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3445 //lyxerr << "InsetTabular::edit: " << this << endl;
3447 cur.setSelection(false);
3450 if (isRightToLeft(cur))
3451 cur.idx() = tabular.getLastCellInRow(0);
3457 if (isRightToLeft(cur))
3458 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3460 cur.idx() = tabular.numberofcells - 1;
3462 cur.pos() = cur.lastpos(); // FIXME crude guess
3464 cur.setCurrentFont();
3465 // FIXME: this accesses the position cache before it is initialized
3467 //cur.bv().fitCursor();
3471 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3473 // In a longtable, tell captions what the current float is
3474 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3475 string const saveflt = cnts.current_float();
3476 if (tabular.is_long_tabular)
3477 cnts.current_float("table");
3479 ParIterator it2 = it;
3481 size_t const end = it2.nargs();
3482 for ( ; it2.idx() < end; it2.top().forwardIdx())
3483 buffer().updateBuffer(it2, utype);
3486 if (tabular.is_long_tabular)
3487 cnts.current_float(saveflt);
3491 void InsetTabular::addToToc(DocIterator const & cpit)
3493 DocIterator dit = cpit;
3495 size_t const end = dit.nargs();
3496 for ( ; dit.idx() < end; dit.top().forwardIdx())
3497 cell(dit.idx())->addToToc(dit);
3501 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3503 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3504 << "\n cur:" << cur);
3505 CursorSlice sl = cur.top();
3506 Cursor & bvcur = cur.bv().cursor();
3508 switch (cmd.action) {
3510 case LFUN_MOUSE_PRESS: {
3511 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3513 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3514 || cmd.x > xo(cur.bv()) + tabular.width()) {
3515 row_type r = rowFromY(cur, cmd.y);
3516 cur.idx() = tabular.getFirstCellInRow(r);
3519 cur.idx() = tabular.getLastCellInRow(r);
3520 cur.pos() = cur.lastpos();
3521 cur.setSelection(true);
3527 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3528 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3529 || cmd.y > y0 + tabular.height()) {
3530 col_type c = columnFromX(cur, cmd.x);
3531 cur.idx() = tabular.cellIndex(0, c);
3534 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3535 cur.pos() = cur.lastpos();
3536 cur.setSelection(true);
3541 // do not reset cursor/selection if we have selected
3542 // some cells (bug 2715).
3543 if (cmd.button() == mouse_button::button3
3544 && &bvcur.selBegin().inset() == this
3545 && bvcur.selIsMultiCell())
3548 // Let InsetTableCell do it
3549 cell(cur.idx())->dispatch(cur, cmd);
3552 case LFUN_MOUSE_MOTION:
3553 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3554 if (cmd.button() == mouse_button::button1) {
3555 // only accept motions to places not deeper nested than the real anchor
3556 if (!bvcur.anchor_.hasPart(cur)) {
3560 // select (additional) row
3562 row_type r = rowFromY(cur, cmd.y);
3563 cur.idx() = tabular.getLastCellInRow(r);
3564 // we need to reset the cursor's pit and pos now, as the old ones
3565 // may no longer be valid.
3568 bvcur.setCursor(cur);
3569 bvcur.setSelection(true);
3572 // select (additional) column
3574 col_type c = columnFromX(cur, cmd.x);
3575 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3576 // we need to reset the cursor's pit and pos now, as the old ones
3577 // may no longer be valid.
3580 bvcur.setCursor(cur);
3581 bvcur.setSelection(true);
3584 // only update if selection changes
3585 if (bvcur.idx() == cur.idx() &&
3586 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3588 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3589 bvcur.setCursor(cur);
3590 bvcur.setSelection(true);
3591 // if this is a multicell selection, we just set the cursor to
3592 // the beginning of the cell's text.
3593 if (bvcur.selIsMultiCell()) {
3594 bvcur.pit() = bvcur.lastpit();
3595 bvcur.pos() = bvcur.lastpos();
3600 case LFUN_MOUSE_RELEASE:
3605 case LFUN_CELL_BACKWARD:
3607 cur.setSelection(false);
3610 case LFUN_CELL_FORWARD:
3612 cur.setSelection(false);
3615 case LFUN_CHAR_FORWARD_SELECT:
3616 case LFUN_CHAR_FORWARD:
3617 case LFUN_CHAR_BACKWARD_SELECT:
3618 case LFUN_CHAR_BACKWARD:
3619 case LFUN_CHAR_RIGHT_SELECT:
3620 case LFUN_CHAR_RIGHT:
3621 case LFUN_CHAR_LEFT_SELECT:
3622 case LFUN_CHAR_LEFT: {
3623 // determine whether we move to next or previous cell, where to enter
3624 // the new cell from, and which command to "finish" (i.e., exit the
3627 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3628 FuncCode finish_lfun;
3630 if (cmd.action == LFUN_CHAR_FORWARD
3631 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3633 finish_lfun = LFUN_FINISHED_FORWARD;
3635 else if (cmd.action == LFUN_CHAR_BACKWARD
3636 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3638 finish_lfun = LFUN_FINISHED_BACKWARD;
3640 // LEFT or RIGHT commands --- the interpretation will depend on the
3641 // table's direction.
3643 bool const right = cmd.action == LFUN_CHAR_RIGHT
3644 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3645 next_cell = isRightToLeft(cur) != right;
3647 if (lyxrc.visual_cursor)
3648 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3650 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3653 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3654 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3655 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3656 cmd.action == LFUN_CHAR_LEFT_SELECT;
3658 // If we have a multicell selection or we're
3659 // not doing some LFUN_*_SELECT thing anyway...
3660 if (!cur.selIsMultiCell() || !select) {
3661 col_type const c = tabular.cellColumn(cur.idx());
3662 row_type const r = tabular.cellRow(cur.idx());
3663 // Are we trying to select the whole cell and is the whole cell
3664 // not yet selected?
3665 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3666 ((next_cell && cur.pit() == cur.lastpit()
3667 && cur.pos() == cur.lastpos())
3668 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3670 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3672 // ...try to dispatch to the cell's inset.
3673 cell(cur.idx())->dispatch(cur, cmd);
3675 // When we already have a selection we want to select the whole cell
3676 // before going to the next cell.
3677 if (select_whole && !empty_cell){
3678 getText(cur.idx())->selectAll(cur);
3683 // FIXME: When we support the selection of an empty cell, remove
3684 // the !empty_cell from this condition. For now we jump to the next
3685 // cell if the current cell is empty.
3686 if (cur.result().dispatched() && !empty_cell)
3690 // move to next/prev cell, as appropriate
3691 // note that we will always do this if we're selecting and we have
3692 // a multicell selection
3693 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3694 << " cell from: " << int(entry_from));
3696 moveNextCell(cur, entry_from);
3698 movePrevCell(cur, entry_from);
3699 // if we're exiting the table, call the appropriate FINISHED lfun
3700 if (sl == cur.top())
3701 cmd = FuncRequest(finish_lfun);
3708 case LFUN_DOWN_SELECT:
3710 if (!(cur.selection() && cur.selIsMultiCell()))
3711 cell(cur.idx())->dispatch(cur, cmd);
3713 cur.dispatched(); // override the cell's decision
3714 if (sl == cur.top()) {
3715 // if our Text didn't do anything to the cursor
3716 // then we try to put the cursor into the cell below
3717 // setting also the right targetX.
3718 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3719 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3720 cur.idx() = tabular.cellBelow(cur.idx());
3722 TextMetrics const & tm =
3723 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3724 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3725 cur.setCurrentFont();
3728 if (sl == cur.top()) {
3729 // we trick it to go to forward after leaving the
3731 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3734 if (cur.selIsMultiCell()) {
3735 cur.pit() = cur.lastpit();
3736 cur.pos() = cur.lastpos();
3737 cur.setCurrentFont();
3742 case LFUN_UP_SELECT:
3744 if (!(cur.selection() && cur.selIsMultiCell()))
3745 cell(cur.idx())->dispatch(cur, cmd);
3746 cur.dispatched(); // override the cell's decision
3747 if (sl == cur.top()) {
3748 // if our Text didn't do anything to the cursor
3749 // then we try to put the cursor into the cell above
3750 // setting also the right targetX.
3751 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3752 if (tabular.cellRow(cur.idx()) != 0) {
3753 cur.idx() = tabular.cellAbove(cur.idx());
3754 cur.pit() = cur.lastpit();
3755 Text const * text = cell(cur.idx())->getText(0);
3756 TextMetrics const & tm = cur.bv().textMetrics(text);
3757 ParagraphMetrics const & pm =
3758 tm.parMetrics(cur.lastpit());
3759 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3760 cur.setCurrentFont();
3763 if (sl == cur.top()) {
3764 cmd = FuncRequest(LFUN_UP);
3767 if (cur.selIsMultiCell()) {
3769 cur.pos() = cur.lastpos();
3770 cur.setCurrentFont();
3775 // case LFUN_SCREEN_DOWN: {
3776 // //if (hasSelection())
3777 // // cur.selection() = false;
3778 // col_type const col = tabular.cellColumn(cur.idx());
3779 // int const t = cur.bv().top_y() + cur.bv().height();
3780 // if (t < yo() + tabular.getHeightOfTabular()) {
3781 // cur.bv().scrollDocView(t);
3782 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3784 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3791 // case LFUN_SCREEN_UP: {
3792 // //if (hasSelection())
3793 // // cur.selection() = false;
3794 // col_type const col = tabular.cellColumn(cur.idx());
3795 // int const t = cur.bv().top_y() + cur.bv().height();
3797 // cur.bv().scrollDocView(t);
3801 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3805 // cur.par() = cur.lastpar();
3806 // cur.pos() = cur.lastpos();
3810 case LFUN_LAYOUT_TABULAR:
3811 cur.bv().showDialog("tabular");
3814 case LFUN_INSET_MODIFY:
3815 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3819 // insert file functions
3820 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3821 case LFUN_FILE_INSERT_PLAINTEXT:
3823 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3824 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3825 FileName(to_utf8(cmd.argument())));
3828 cur.recordUndoInset(INSERT_UNDO);
3829 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3830 // content has been replaced,
3831 // so cursor might be invalid
3832 cur.pos() = cur.lastpos();
3833 cur.pit() = cur.lastpit();
3834 bvcur.setCursor(cur);
3841 if (cur.selIsMultiCell()) {
3842 if (copySelection(cur)) {
3843 cur.recordUndoInset(DELETE_UNDO);
3847 cell(cur.idx())->dispatch(cur, cmd);
3850 case LFUN_SELF_INSERT:
3851 if (cur.selIsMultiCell()) {
3852 cur.recordUndoInset(DELETE_UNDO);
3855 cell(cur.idx())->dispatch(cur, cmd);
3858 case LFUN_CHAR_DELETE_BACKWARD:
3859 case LFUN_CHAR_DELETE_FORWARD:
3860 if (cur.selIsMultiCell()) {
3861 cur.recordUndoInset(DELETE_UNDO);
3864 cell(cur.idx())->dispatch(cur, cmd);
3868 if (!cur.selection())
3870 if (cur.selIsMultiCell()) {
3874 cell(cur.idx())->dispatch(cur, cmd);
3877 case LFUN_CLIPBOARD_PASTE:
3878 case LFUN_PRIMARY_SELECTION_PASTE: {
3879 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3880 theClipboard().getAsText() :
3881 theSelection().get();
3884 // pass to InsertPlaintextString, but
3885 // only if we have multi-cell content
3886 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3887 cur.recordUndoInset(INSERT_UNDO);
3888 if (insertPlaintextString(cur.bv(), clip, false)) {
3889 // content has been replaced,
3890 // so cursor might be invalid
3891 cur.pos() = cur.lastpos();
3892 cur.pit() = cur.lastpit();
3893 bvcur.setCursor(cur);
3897 // Let the cell handle normal text
3898 cell(cur.idx())->dispatch(cur, cmd);
3903 if (!tabularStackDirty()) {
3904 if (!cur.selIsMultiCell())
3905 cell(cur.idx())->dispatch(cur, cmd);
3908 if (theClipboard().isInternal() ||
3909 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3910 cur.recordUndoInset(INSERT_UNDO);
3911 pasteClipboard(cur);
3915 case LFUN_FONT_EMPH:
3916 case LFUN_FONT_BOLD:
3917 case LFUN_FONT_BOLDSYMBOL:
3918 case LFUN_FONT_ROMAN:
3919 case LFUN_FONT_NOUN:
3920 case LFUN_FONT_ITAL:
3921 case LFUN_FONT_FRAK:
3922 case LFUN_FONT_TYPEWRITER:
3923 case LFUN_FONT_SANS:
3924 case LFUN_TEXTSTYLE_APPLY:
3925 case LFUN_TEXTSTYLE_UPDATE:
3926 case LFUN_FONT_SIZE:
3927 case LFUN_FONT_UNDERLINE:
3928 case LFUN_FONT_STRIKEOUT:
3929 case LFUN_FONT_UULINE:
3930 case LFUN_FONT_UWAVE:
3932 case LFUN_WORD_CAPITALIZE:
3933 case LFUN_WORD_UPCASE:
3934 case LFUN_WORD_LOWCASE:
3935 case LFUN_CHARS_TRANSPOSE:
3936 if (cur.selIsMultiCell()) {
3939 getSelection(cur, rs, re, cs, ce);
3940 Cursor tmpcur = cur;
3941 for (row_type r = rs; r <= re; ++r) {
3942 for (col_type c = cs; c <= ce; ++c) {
3943 // cursor follows cell:
3944 tmpcur.idx() = tabular.cellIndex(r, c);
3945 // select this cell only:
3948 tmpcur.resetAnchor();
3949 tmpcur.pit() = tmpcur.lastpit();
3950 tmpcur.pos() = tmpcur.top().lastpos();
3951 tmpcur.setCursor(tmpcur);
3952 tmpcur.setSelection();
3953 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3958 cell(cur.idx())->dispatch(cur, cmd);
3962 case LFUN_INSET_SETTINGS:
3963 // relay this lfun to Inset, not to the cell.
3964 Inset::doDispatch(cur, cmd);
3968 // we try to handle this event in the insets dispatch function.
3969 cell(cur.idx())->dispatch(cur, cmd);
3975 // function sets an object as defined in func_status.h:
3976 // states OK, Unknown, Disabled, On, Off.
3977 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3978 FuncStatus & status) const
3980 switch (cmd.action) {
3981 case LFUN_INSET_MODIFY: {
3982 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
3985 string const s = cmd.getArg(1);
3986 // FIXME: We only check for the very first argument...
3987 int action = Tabular::LAST_ACTION;
3989 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3990 if (tabularFeature[i].feature == s) {
3991 action = tabularFeature[i].action;
3995 if (action == Tabular::LAST_ACTION) {
3997 status.unknown(true);
4001 string const argument = cmd.getLongArg(2);
4003 row_type sel_row_start = 0;
4004 row_type sel_row_end = 0;
4005 col_type sel_col_start = 0;
4006 col_type sel_col_end = 0;
4007 Tabular::ltType dummyltt;
4010 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4013 case Tabular::SET_PWIDTH:
4014 case Tabular::SET_MPWIDTH:
4015 case Tabular::SET_SPECIAL_COLUMN:
4016 case Tabular::SET_SPECIAL_MULTICOLUMN:
4017 case Tabular::SET_SPECIAL_MULTIROW:
4018 case Tabular::APPEND_ROW:
4019 case Tabular::APPEND_COLUMN:
4020 case Tabular::DELETE_ROW:
4021 case Tabular::DELETE_COLUMN:
4022 case Tabular::COPY_ROW:
4023 case Tabular::COPY_COLUMN:
4024 case Tabular::SET_TOP_SPACE:
4025 case Tabular::SET_BOTTOM_SPACE:
4026 case Tabular::SET_INTERLINE_SPACE:
4030 case Tabular::MULTICOLUMN:
4031 // If a row is set as longtable caption, it must not be allowed
4032 // to unset that this row is a multicolumn.
4033 status.setEnabled(sel_row_start == sel_row_end
4034 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4035 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4038 case Tabular::MULTIROW:
4039 // If a row is set as longtable caption, it must not be allowed
4040 // to unset that this row is a multirow.
4041 status.setEnabled(sel_col_start == sel_col_end
4042 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4043 status.setOnOff(tabular.isMultiRow(cur.idx()));
4046 case Tabular::SET_ALL_LINES:
4047 case Tabular::UNSET_ALL_LINES:
4048 case Tabular::SET_BORDER_LINES:
4049 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4052 case Tabular::SET_LINE_TOP:
4053 case Tabular::SET_LINE_BOTTOM:
4054 case Tabular::SET_LINE_LEFT:
4055 case Tabular::SET_LINE_RIGHT:
4056 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4059 case Tabular::TOGGLE_LINE_TOP:
4060 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4061 status.setOnOff(tabular.topLine(cur.idx()));
4064 case Tabular::TOGGLE_LINE_BOTTOM:
4065 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4066 status.setOnOff(tabular.bottomLine(cur.idx()));
4069 case Tabular::TOGGLE_LINE_LEFT:
4070 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4071 status.setOnOff(tabular.leftLine(cur.idx()));
4074 case Tabular::TOGGLE_LINE_RIGHT:
4075 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4076 status.setOnOff(tabular.rightLine(cur.idx()));
4079 case Tabular::M_ALIGN_LEFT:
4081 case Tabular::ALIGN_LEFT:
4082 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4083 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4086 case Tabular::M_ALIGN_RIGHT:
4088 case Tabular::ALIGN_RIGHT:
4089 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4090 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4093 case Tabular::M_ALIGN_CENTER:
4095 case Tabular::ALIGN_CENTER:
4096 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4097 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4100 case Tabular::ALIGN_BLOCK:
4101 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4102 && !tabular.isMultiRow(cur.idx()));
4103 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4106 case Tabular::M_VALIGN_TOP:
4108 case Tabular::VALIGN_TOP:
4109 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4110 && !tabular.isMultiRow(cur.idx()));
4112 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4115 case Tabular::M_VALIGN_BOTTOM:
4117 case Tabular::VALIGN_BOTTOM:
4118 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4119 && !tabular.isMultiRow(cur.idx()));
4121 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4124 case Tabular::M_VALIGN_MIDDLE:
4126 case Tabular::VALIGN_MIDDLE:
4127 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4128 && !tabular.isMultiRow(cur.idx()));
4130 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4133 case Tabular::SET_LONGTABULAR:
4134 status.setOnOff(tabular.is_long_tabular);
4137 case Tabular::UNSET_LONGTABULAR:
4138 status.setOnOff(!tabular.is_long_tabular);
4141 case Tabular::TOGGLE_ROTATE_TABULAR:
4142 case Tabular::SET_ROTATE_TABULAR:
4143 status.setOnOff(tabular.rotate);
4146 case Tabular::TABULAR_VALIGN_TOP:
4147 status.setOnOff(tabular.tabular_valignment
4148 == Tabular::LYX_VALIGN_TOP);
4150 case Tabular::TABULAR_VALIGN_MIDDLE:
4151 status.setOnOff(tabular.tabular_valignment
4152 == Tabular::LYX_VALIGN_MIDDLE);
4154 case Tabular::TABULAR_VALIGN_BOTTOM:
4155 status.setOnOff(tabular.tabular_valignment
4156 == Tabular::LYX_VALIGN_BOTTOM);
4159 case Tabular::LONGTABULAR_ALIGN_LEFT:
4160 status.setOnOff(tabular.longtabular_alignment
4161 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4163 case Tabular::LONGTABULAR_ALIGN_CENTER:
4164 status.setOnOff(tabular.longtabular_alignment
4165 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4167 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4168 status.setOnOff(tabular.longtabular_alignment
4169 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4172 case Tabular::UNSET_ROTATE_TABULAR:
4173 status.setOnOff(!tabular.rotate);
4176 case Tabular::TOGGLE_ROTATE_CELL:
4177 case Tabular::SET_ROTATE_CELL:
4178 status.setOnOff(!oneCellHasRotationState(false,
4179 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4182 case Tabular::UNSET_ROTATE_CELL:
4183 status.setOnOff(!oneCellHasRotationState(true,
4184 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4187 case Tabular::SET_USEBOX:
4188 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4191 // every row can only be one thing:
4192 // either a footer or header or caption
4193 case Tabular::SET_LTFIRSTHEAD:
4194 status.setEnabled(sel_row_start == sel_row_end
4195 && !tabular.ltCaption(sel_row_start));
4196 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4199 case Tabular::UNSET_LTFIRSTHEAD:
4200 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4203 case Tabular::SET_LTHEAD:
4204 status.setEnabled(sel_row_start == sel_row_end
4205 && !tabular.ltCaption(sel_row_start));
4206 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4209 case Tabular::UNSET_LTHEAD:
4210 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4213 case Tabular::SET_LTFOOT:
4214 status.setEnabled(sel_row_start == sel_row_end
4215 && !tabular.ltCaption(sel_row_start));
4216 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4219 case Tabular::UNSET_LTFOOT:
4220 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4223 case Tabular::SET_LTLASTFOOT:
4224 status.setEnabled(sel_row_start == sel_row_end
4225 && !tabular.ltCaption(sel_row_start));
4226 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4229 case Tabular::UNSET_LTLASTFOOT:
4230 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4233 case Tabular::SET_LTNEWPAGE:
4234 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4237 // only one row can be the caption
4238 // and a multirow cannot be set as caption
4239 case Tabular::TOGGLE_LTCAPTION:
4240 status.setEnabled(sel_row_start == sel_row_end
4241 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4242 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4243 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4244 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4245 && (!tabular.haveLTCaption()
4246 || tabular.ltCaption(sel_row_start))
4247 && !tabular.isMultiRow(sel_row_start));
4248 status.setOnOff(tabular.ltCaption(sel_row_start));
4251 case Tabular::SET_BOOKTABS:
4252 status.setOnOff(tabular.use_booktabs);
4255 case Tabular::UNSET_BOOKTABS:
4256 status.setOnOff(!tabular.use_booktabs);
4261 status.setEnabled(false);
4267 // These are only enabled inside tabular
4268 case LFUN_CELL_BACKWARD:
4269 case LFUN_CELL_FORWARD:
4270 status.setEnabled(true);
4273 // disable these with multiple cells selected
4274 case LFUN_INSET_INSERT:
4275 case LFUN_TABULAR_INSERT:
4276 case LFUN_FLEX_INSERT:
4277 case LFUN_FLOAT_INSERT:
4278 case LFUN_FLOAT_WIDE_INSERT:
4279 case LFUN_FOOTNOTE_INSERT:
4280 case LFUN_MARGINALNOTE_INSERT:
4281 case LFUN_MATH_INSERT:
4282 case LFUN_MATH_MODE:
4283 case LFUN_MATH_MUTATE:
4284 case LFUN_MATH_DISPLAY:
4285 case LFUN_NOTE_INSERT:
4286 case LFUN_OPTIONAL_INSERT:
4287 case LFUN_BOX_INSERT:
4288 case LFUN_BRANCH_INSERT:
4289 case LFUN_PHANTOM_INSERT:
4290 case LFUN_WRAP_INSERT:
4291 case LFUN_ERT_INSERT: {
4292 if (cur.selIsMultiCell()) {
4293 status.setEnabled(false);
4296 return cell(cur.idx())->getStatus(cur, cmd, status);
4299 // disable in non-fixed-width cells
4300 case LFUN_NEWLINE_INSERT:
4301 case LFUN_BREAK_PARAGRAPH: {
4302 if (tabular.getPWidth(cur.idx()).zero()) {
4303 status.setEnabled(false);
4306 return cell(cur.idx())->getStatus(cur, cmd, status);
4309 case LFUN_NEWPAGE_INSERT:
4310 status.setEnabled(false);
4314 if (tabularStackDirty() && theClipboard().isInternal()) {
4315 if (cur.selIsMultiCell()) {
4318 getSelection(cur, rs, re, cs, ce);
4319 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4320 && paste_tabular->nrows() == re - rs + 1)
4321 status.setEnabled(true);
4323 status.setEnabled(false);
4324 status.message(_("Selection size should match clipboard content."));
4327 status.setEnabled(true);
4330 return cell(cur.idx())->getStatus(cur, cmd, status);
4332 case LFUN_INSET_SETTINGS:
4333 // relay this lfun to Inset, not to the cell.
4334 return Inset::getStatus(cur, cmd, status);
4337 // we try to handle this event in the insets dispatch function.
4338 return cell(cur.idx())->getStatus(cur, cmd, status);
4344 Inset::DisplayType InsetTabular::display() const
4346 if (tabular.is_long_tabular) {
4347 switch (tabular.longtabular_alignment) {
4348 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4350 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4352 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4362 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4364 return tabular.latex(os, runparams);
4368 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4370 os << '\n'; // output table on a new line
4371 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4372 tabular.plaintext(os, runparams, dp, false, 0);
4373 return PLAINTEXT_NEWLINE;
4377 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4382 // FIXME: Why not pass a proper DocIterator here?
4384 // if the table is inside a float it doesn't need the informaltable
4385 // wrapper. Search for it.
4386 for (master = owner(); master; master = master->owner())
4387 if (master->lyxCode() == FLOAT_CODE)
4392 os << "<informaltable>";
4395 ret += tabular.docbook(os, runparams);
4397 os << "</informaltable>";
4404 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4407 // It'd be better to be able to get this from an InsetLayout, but at present
4408 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4409 xs << html::StartTag("table");
4410 docstring ret = tabular.xhtml(xs, rp);
4411 xs << html::EndTag("table");
4416 void InsetTabular::validate(LaTeXFeatures & features) const
4418 tabular.validate(features);
4420 // It'd be better to be able to get this from an InsetLayout, but at present
4421 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4422 if (features.runparams().flavor == OutputParams::HTML)
4423 features.addPreambleSnippet("<style type=\"text/css\">\n"
4424 "table { border: 1px solid black; display: inline-block; }\n"
4425 "td { border: 1px solid black; padding: 0.5ex; }\n"
4430 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4432 return tabular.cellInset(idx);
4436 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4438 return tabular.cellInset(idx);
4442 void InsetTabular::cursorPos(BufferView const & bv,
4443 CursorSlice const & sl, bool boundary, int & x, int & y) const
4445 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4447 int const row = tabular.cellRow(sl.idx());
4448 int const col = tabular.cellColumn(sl.idx());
4450 // y offset correction
4451 for (int r = 0; r < row; ++r)
4452 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4453 + tabular.interRowSpace(r + 1);
4455 y += tabular.textVOffset(sl.idx());
4457 // x offset correction
4458 for (int c = 0; c < col; ++c)
4459 x += tabular.column_info[c].width;
4461 x += tabular.textHOffset(sl.idx());
4462 x += ADD_TO_TABULAR_WIDTH;
4467 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4471 Inset const & inset = *tabular.cellInset(cell);
4472 Point o = bv.coordCache().getInsets().xy(&inset);
4473 int const xbeg = o.x_ - tabular.textHOffset(cell);
4474 int const xend = xbeg + tabular.columnWidth(cell);
4475 row_type const row = tabular.cellRow(cell);
4476 int const ybeg = o.y_ - tabular.rowAscent(row)
4477 - tabular.interRowSpace(row);
4478 int const yend = ybeg + tabular.cellHeight(cell);
4490 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4491 // << " ybeg=" << ybeg << " yend=" << yend
4492 // << " xx=" << xx << " yy=" << yy
4493 // << " dist=" << xx + yy << endl;
4498 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4500 //lyxerr << "InsetTabular::editXY: " << this << endl;
4501 cur.setSelection(false);
4503 cur.idx() = getNearestCell(cur.bv(), x, y);
4505 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4509 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4511 cur.idx() = getNearestCell(cur.bv(), x, y);
4512 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4516 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4518 idx_type idx_min = 0;
4519 int dist_min = numeric_limits<int>::max();
4520 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4521 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4522 int const d = dist(bv, i, x, y);
4533 int InsetTabular::cellXPos(idx_type const cell) const
4535 col_type col = tabular.cellColumn(cell);
4537 for (col_type c = 0; c < col; ++c)
4538 lx += tabular.columnWidth(c);
4544 void InsetTabular::resetPos(Cursor & cur) const
4546 BufferView & bv = cur.bv();
4547 int const maxwidth = bv.workWidth();
4549 int const scx_old = scx_;
4550 int const i = cur.find(this);
4555 int const X2 = maxwidth;
4556 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4557 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4558 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4561 scx_ = X1 + 20 - x1;
4563 scx_ = X2 - 20 - x2;
4568 // only update if offset changed
4569 if (scx_ != scx_old)
4570 cur.updateFlags(Update::Force | Update::FitCursor);
4574 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4576 row_type const row = tabular.cellRow(cur.idx());
4577 col_type const col = tabular.cellColumn(cur.idx());
4579 if (isRightToLeft(cur)) {
4580 if (tabular.cellColumn(cur.idx()) == 0) {
4581 if (row == tabular.nrows() - 1)
4583 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4588 cur.idx() = tabular.getLastCellInRow(row - 1);
4590 cur.idx() = tabular.cellIndex(row, col - 1);
4593 if (tabular.isLastCell(cur.idx()))
4595 if (cur.idx() == tabular.getLastCellInRow(row))
4596 cur.idx() = tabular.cellIndex(row + 1, 0);
4598 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4599 cur.idx() = tabular.cellIndex(row, colnextcell);
4603 cur.boundary(false);
4605 if (cur.selIsMultiCell()) {
4606 cur.pit() = cur.lastpit();
4607 cur.pos() = cur.lastpos();
4615 // in visual mode, place cursor at extreme left or right
4617 switch(entry_from) {
4619 case ENTRY_DIRECTION_RIGHT:
4620 cur.posVisToRowExtremity(false /* !left */);
4622 case ENTRY_DIRECTION_LEFT:
4623 cur.posVisToRowExtremity(true /* left */);
4625 case ENTRY_DIRECTION_IGNORE:
4626 // nothing to do in this case
4630 cur.setCurrentFont();
4635 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4637 row_type const row = tabular.cellRow(cur.idx());
4638 col_type const col = tabular.cellColumn(cur.idx());
4640 if (isRightToLeft(cur)) {
4641 if (cur.idx() == tabular.getLastCellInRow(row)) {
4644 cur.idx() = tabular.getFirstCellInRow(row);
4645 cur.idx() = tabular.cellAbove(cur.idx());
4647 if (tabular.isLastCell(cur.idx()))
4649 if (cur.idx() == tabular.getLastCellInRow(row))
4650 cur.idx() = tabular.cellIndex(row + 1, 0);
4652 cur.idx() = tabular.cellIndex(row, col + 1);
4655 if (cur.idx() == 0) // first cell
4658 cur.idx() = tabular.getLastCellInRow(row - 1);
4660 cur.idx() = tabular.cellIndex(row, col - 1);
4663 if (cur.selIsMultiCell()) {
4664 cur.pit() = cur.lastpit();
4665 cur.pos() = cur.lastpos();
4670 cur.pit() = cur.lastpit();
4671 cur.pos() = cur.lastpos();
4673 // in visual mode, place cursor at extreme left or right
4675 switch(entry_from) {
4677 case ENTRY_DIRECTION_RIGHT:
4678 cur.posVisToRowExtremity(false /* !left */);
4680 case ENTRY_DIRECTION_LEFT:
4681 cur.posVisToRowExtremity(true /* left */);
4683 case ENTRY_DIRECTION_IGNORE:
4684 // nothing to do in this case
4688 cur.setCurrentFont();
4693 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4695 istringstream is(argument);
4698 if (insetCode(s) != TABULAR_CODE)
4702 size_t safe_guard = 0;
4707 if (safe_guard > 1000) {
4708 LYXERR0("parameter max count reached!");
4712 Tabular::Feature action = Tabular::LAST_ACTION;
4715 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4716 if (s != tabularFeature[i].feature)
4719 action = tabularFeature[i].action;
4722 if (action == Tabular::LAST_ACTION) {
4723 LYXERR0("Feature not found " << s);
4727 if (tabularFeature[i].need_value)
4729 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4730 tabularFeatures(cur, action, val);
4736 static void checkLongtableSpecial(Tabular::ltType & ltt,
4737 string const & special, bool & flag)
4739 if (special == "dl_above") {
4742 } else if (special == "dl_below") {
4743 ltt.bottomDL = flag;
4745 } else if (special == "empty") {
4755 bool InsetTabular::oneCellHasRotationState(bool rotated,
4756 row_type row_start, row_type row_end,
4757 col_type col_start, col_type col_end) const
4759 for (row_type r = row_start; r <= row_end; ++r)
4760 for (col_type c = col_start; c <= col_end; ++c)
4761 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4767 void InsetTabular::tabularFeatures(Cursor & cur,
4768 Tabular::Feature feature, string const & value)
4770 col_type sel_col_start;
4771 col_type sel_col_end;
4772 row_type sel_row_start;
4773 row_type sel_row_end;
4774 bool setLines = false;
4775 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4776 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4780 case Tabular::M_ALIGN_LEFT:
4781 case Tabular::ALIGN_LEFT:
4782 setAlign = LYX_ALIGN_LEFT;
4785 case Tabular::M_ALIGN_RIGHT:
4786 case Tabular::ALIGN_RIGHT:
4787 setAlign = LYX_ALIGN_RIGHT;
4790 case Tabular::M_ALIGN_CENTER:
4791 case Tabular::ALIGN_CENTER:
4792 setAlign = LYX_ALIGN_CENTER;
4795 case Tabular::ALIGN_BLOCK:
4796 setAlign = LYX_ALIGN_BLOCK;
4799 case Tabular::M_VALIGN_TOP:
4800 case Tabular::VALIGN_TOP:
4801 setVAlign = Tabular::LYX_VALIGN_TOP;
4804 case Tabular::M_VALIGN_BOTTOM:
4805 case Tabular::VALIGN_BOTTOM:
4806 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4809 case Tabular::M_VALIGN_MIDDLE:
4810 case Tabular::VALIGN_MIDDLE:
4811 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4818 cur.recordUndoInset(ATOMIC_UNDO);
4820 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4821 row_type const row = tabular.cellRow(cur.idx());
4822 col_type const column = tabular.cellColumn(cur.idx());
4824 Tabular::ltType ltt;
4828 case Tabular::SET_PWIDTH: {
4829 Length const len(value);
4830 tabular.setColumnPWidth(cur, cur.idx(), len);
4832 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4833 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4837 case Tabular::SET_MPWIDTH:
4838 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4841 case Tabular::SET_SPECIAL_COLUMN:
4842 case Tabular::SET_SPECIAL_MULTICOLUMN:
4843 if (value == "none")
4844 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
4846 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4849 case Tabular::SET_SPECIAL_MULTIROW:
4850 //FIXME: noting to do here?
4853 case Tabular::APPEND_ROW:
4854 // append the row into the tabular
4855 tabular.appendRow(cur.idx());
4858 case Tabular::APPEND_COLUMN:
4859 // append the column into the tabular
4860 tabular.appendColumn(cur.idx());
4861 cur.idx() = tabular.cellIndex(row, column);
4864 case Tabular::DELETE_ROW:
4865 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4866 tabular.deleteRow(sel_row_start);
4867 if (sel_row_start >= tabular.nrows())
4869 cur.idx() = tabular.cellIndex(sel_row_start, column);
4872 cur.setSelection(false);
4875 case Tabular::DELETE_COLUMN:
4876 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4877 tabular.deleteColumn(sel_col_start);
4878 if (sel_col_start >= tabular.ncols())
4880 cur.idx() = tabular.cellIndex(row, sel_col_start);
4883 cur.setSelection(false);
4886 case Tabular::COPY_ROW:
4887 tabular.copyRow(row);
4890 case Tabular::COPY_COLUMN:
4891 tabular.copyColumn(column);
4892 cur.idx() = tabular.cellIndex(row, column);
4895 case Tabular::SET_LINE_TOP:
4896 case Tabular::TOGGLE_LINE_TOP: {
4897 bool lineSet = (feature == Tabular::SET_LINE_TOP)
4898 ? (value == "true") : !tabular.topLine(cur.idx());
4899 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4900 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4901 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
4905 case Tabular::SET_LINE_BOTTOM:
4906 case Tabular::TOGGLE_LINE_BOTTOM: {
4907 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
4908 ? (value == "true") : !tabular.bottomLine(cur.idx());
4909 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4910 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4911 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
4915 case Tabular::SET_LINE_LEFT:
4916 case Tabular::TOGGLE_LINE_LEFT: {
4917 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
4918 ? (value == "true") : !tabular.leftLine(cur.idx());
4919 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4920 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4921 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
4925 case Tabular::SET_LINE_RIGHT:
4926 case Tabular::TOGGLE_LINE_RIGHT: {
4927 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
4928 ? (value == "true") : !tabular.rightLine(cur.idx());
4929 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4930 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4931 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
4935 case Tabular::M_ALIGN_LEFT:
4936 case Tabular::M_ALIGN_RIGHT:
4937 case Tabular::M_ALIGN_CENTER:
4939 case Tabular::ALIGN_LEFT:
4940 case Tabular::ALIGN_RIGHT:
4941 case Tabular::ALIGN_CENTER:
4942 case Tabular::ALIGN_BLOCK:
4943 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4944 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4945 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
4948 case Tabular::M_VALIGN_TOP:
4949 case Tabular::M_VALIGN_BOTTOM:
4950 case Tabular::M_VALIGN_MIDDLE:
4952 case Tabular::VALIGN_TOP:
4953 case Tabular::VALIGN_BOTTOM:
4954 case Tabular::VALIGN_MIDDLE:
4955 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4956 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4957 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
4960 case Tabular::MULTICOLUMN: {
4961 if (!cur.selection()) {
4962 // just multicol for one single cell
4963 // check whether we are completely in a multicol
4964 if (tabular.isMultiColumn(cur.idx()))
4965 tabular.unsetMultiColumn(cur.idx());
4967 tabular.setMultiColumn(cur.idx(), 1);
4970 // we have a selection so this means we just add all this
4971 // cells to form a multicolumn cell
4972 idx_type const s_start = cur.selBegin().idx();
4973 row_type const col_start = tabular.cellColumn(s_start);
4974 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
4975 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
4978 cur.setSelection(false);
4982 case Tabular::MULTIROW: {
4983 if (!cur.selection()) {
4984 // just multirow for one single cell
4985 // check whether we are completely in a multirow
4986 if (tabular.isMultiRow(cur.idx()))
4987 tabular.unsetMultiRow(cur.idx());
4989 tabular.setMultiRow(cur.idx(), 1);
4992 // we have a selection so this means we just add all this
4993 // cells to form a multirow cell
4994 idx_type const s_start = cur.selBegin().idx();
4995 row_type const row_start = tabular.cellRow(s_start);
4996 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
4997 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5000 cur.setSelection(false);
5004 case Tabular::SET_ALL_LINES:
5006 case Tabular::UNSET_ALL_LINES:
5007 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5008 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5009 idx_type const cell = tabular.cellIndex(r, c);
5010 tabular.setTopLine(cell, setLines);
5011 tabular.setBottomLine(cell, setLines);
5012 tabular.setRightLine(cell, setLines);
5013 tabular.setLeftLine(cell, setLines);
5017 case Tabular::SET_BORDER_LINES:
5018 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5019 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5020 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5022 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5023 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5024 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5028 case Tabular::SET_LONGTABULAR:
5029 tabular.is_long_tabular = true;
5032 case Tabular::UNSET_LONGTABULAR:
5033 for (row_type r = 0; r < tabular.nrows(); ++r) {
5034 if (tabular.ltCaption(r)) {
5035 cur.idx() = tabular.cellIndex(r, 0);
5038 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5041 tabular.is_long_tabular = false;
5044 case Tabular::SET_ROTATE_TABULAR:
5045 tabular.rotate = true;
5048 case Tabular::UNSET_ROTATE_TABULAR:
5049 tabular.rotate = false;
5052 case Tabular::TOGGLE_ROTATE_TABULAR:
5053 tabular.rotate = !tabular.rotate;
5056 case Tabular::TABULAR_VALIGN_TOP:
5057 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5060 case Tabular::TABULAR_VALIGN_MIDDLE:
5061 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5064 case Tabular::TABULAR_VALIGN_BOTTOM:
5065 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5068 case Tabular::LONGTABULAR_ALIGN_LEFT:
5069 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5072 case Tabular::LONGTABULAR_ALIGN_CENTER:
5073 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5076 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5077 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5082 case Tabular::SET_ROTATE_CELL:
5083 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5084 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5085 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5088 case Tabular::UNSET_ROTATE_CELL:
5089 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5090 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5091 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5094 case Tabular::TOGGLE_ROTATE_CELL:
5096 bool oneNotRotated = oneCellHasRotationState(false,
5097 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5099 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5100 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5101 tabular.setRotateCell(tabular.cellIndex(r, c),
5106 case Tabular::SET_USEBOX: {
5107 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5108 if (val == tabular.getUsebox(cur.idx()))
5109 val = Tabular::BOX_NONE;
5110 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5111 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5112 tabular.setUsebox(tabular.cellIndex(r, c), val);
5116 case Tabular::UNSET_LTFIRSTHEAD:
5118 case Tabular::SET_LTFIRSTHEAD:
5119 tabular.getRowOfLTFirstHead(row, ltt);
5120 checkLongtableSpecial(ltt, value, flag);
5121 tabular.setLTHead(row, flag, ltt, true);
5124 case Tabular::UNSET_LTHEAD:
5126 case Tabular::SET_LTHEAD:
5127 tabular.getRowOfLTHead(row, ltt);
5128 checkLongtableSpecial(ltt, value, flag);
5129 tabular.setLTHead(row, flag, ltt, false);
5132 case Tabular::UNSET_LTFOOT:
5134 case Tabular::SET_LTFOOT:
5135 tabular.getRowOfLTFoot(row, ltt);
5136 checkLongtableSpecial(ltt, value, flag);
5137 tabular.setLTFoot(row, flag, ltt, false);
5140 case Tabular::UNSET_LTLASTFOOT:
5142 case Tabular::SET_LTLASTFOOT:
5143 tabular.getRowOfLTLastFoot(row, ltt);
5144 checkLongtableSpecial(ltt, value, flag);
5145 tabular.setLTFoot(row, flag, ltt, true);
5148 case Tabular::SET_LTNEWPAGE:
5149 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5152 case Tabular::TOGGLE_LTCAPTION: {
5153 bool const set = !tabular.ltCaption(row);
5154 cur.idx() = tabular.setLTCaption(row, set);
5157 cur.setSelection(false);
5160 // When a row is set as caption, then also insert
5161 // a caption. Otherwise the LaTeX output is broken.
5162 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5163 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5165 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5166 if (lyx::getStatus(fr).enabled())
5172 case Tabular::SET_BOOKTABS:
5173 tabular.use_booktabs = true;
5176 case Tabular::UNSET_BOOKTABS:
5177 tabular.use_booktabs = false;
5180 case Tabular::SET_TOP_SPACE: {
5182 if (value == "default")
5183 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5184 tabular.row_info[r].top_space_default = true;
5185 else if (value == "none")
5186 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5187 tabular.row_info[r].top_space_default = false;
5188 tabular.row_info[r].top_space = len;
5190 else if (isValidLength(value, &len))
5191 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5192 tabular.row_info[r].top_space_default = false;
5193 tabular.row_info[r].top_space = len;
5198 case Tabular::SET_BOTTOM_SPACE: {
5200 if (value == "default")
5201 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5202 tabular.row_info[r].bottom_space_default = true;
5203 else if (value == "none")
5204 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5205 tabular.row_info[r].bottom_space_default = false;
5206 tabular.row_info[r].bottom_space = len;
5208 else if (isValidLength(value, &len))
5209 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5210 tabular.row_info[r].bottom_space_default = false;
5211 tabular.row_info[r].bottom_space = len;
5216 case Tabular::SET_INTERLINE_SPACE: {
5218 if (value == "default")
5219 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5220 tabular.row_info[r].interline_space_default = true;
5221 else if (value == "none")
5222 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5223 tabular.row_info[r].interline_space_default = false;
5224 tabular.row_info[r].interline_space = len;
5226 else if (isValidLength(value, &len))
5227 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5228 tabular.row_info[r].interline_space_default = false;
5229 tabular.row_info[r].interline_space = len;
5234 // dummy stuff just to avoid warnings
5235 case Tabular::LAST_ACTION:
5241 bool InsetTabular::copySelection(Cursor & cur)
5243 if (!cur.selection())
5248 getSelection(cur, rs, re, cs, ce);
5250 paste_tabular.reset(new Tabular(tabular));
5252 for (row_type r = 0; r < rs; ++r)
5253 paste_tabular->deleteRow(0);
5255 row_type const rows = re - rs + 1;
5256 while (paste_tabular->nrows() > rows)
5257 paste_tabular->deleteRow(rows);
5259 for (col_type c = 0; c < cs; ++c)
5260 paste_tabular->deleteColumn(0);
5262 col_type const columns = ce - cs + 1;
5263 while (paste_tabular->ncols() > columns)
5264 paste_tabular->deleteColumn(columns);
5266 paste_tabular->setBuffer(tabular.buffer());
5268 odocstringstream os;
5269 OutputParams const runparams(0);
5270 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5271 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5272 cap::copySelection(cur, os.str());
5274 // mark tabular stack dirty
5275 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5276 // when we (hopefully) have a one-for-all paste mechanism.
5277 // This must be called after cap::copySelection.
5278 dirtyTabularStack(true);
5284 bool InsetTabular::pasteClipboard(Cursor & cur)
5288 col_type actcol = tabular.cellColumn(cur.idx());
5289 row_type actrow = tabular.cellRow(cur.idx());
5291 if (cur.selIsMultiCell()) {
5294 getSelection(cur, actrow, re, actcol, ce);
5297 for (row_type r1 = 0, r2 = actrow;
5298 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5300 for (col_type c1 = 0, c2 = actcol;
5301 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5303 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5304 tabular.isPartOfMultiColumn(r2, c2))
5306 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5310 if (tabular.isPartOfMultiColumn(r2, c2)) {
5314 shared_ptr<InsetTableCell> inset(
5315 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5316 tabular.setCellInset(r2, c2, inset);
5317 // FIXME?: why do we need to do this explicitly? (EL)
5318 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5320 // FIXME: change tracking (MG)
5321 inset->setChange(Change(buffer().params().trackChanges ?
5322 Change::INSERTED : Change::UNCHANGED));
5330 void InsetTabular::cutSelection(Cursor & cur)
5332 if (!cur.selection())
5337 getSelection(cur, rs, re, cs, ce);
5338 for (row_type r = rs; r <= re; ++r) {
5339 for (col_type c = cs; c <= ce; ++c) {
5340 shared_ptr<InsetTableCell> t
5341 = cell(tabular.cellIndex(r, c));
5342 if (buffer().params().trackChanges)
5343 // FIXME: Change tracking (MG)
5344 t->setChange(Change(Change::DELETED));
5350 // cursor position might be invalid now
5351 if (cur.pit() > cur.lastpit())
5352 cur.pit() = cur.lastpit();
5353 if (cur.pos() > cur.lastpos())
5354 cur.pos() = cur.lastpos();
5355 cur.clearSelection();
5359 bool InsetTabular::isRightToLeft(Cursor & cur) const
5361 LASSERT(cur.depth() > 1, /**/);
5362 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5363 pos_type const parentpos = cur[cur.depth() - 2].pos();
5364 return parentpar.getFontSettings(buffer().params(),
5365 parentpos).language()->rightToLeft();
5369 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5372 LASSERT(stidx <= enidx, return docstring());
5374 col_type const col1 = tabular.cellColumn(stidx);
5375 col_type const col2 = tabular.cellColumn(enidx);
5376 row_type const row1 = tabular.cellRow(stidx);
5377 row_type const row2 = tabular.cellRow(enidx);
5379 for (col_type col = col1; col <= col2; col++)
5380 for (row_type row = row1; row <= row2; row++) {
5385 retval += tabular.cellInset(row, col)->asString(intoInsets);
5391 void InsetTabular::getSelection(Cursor & cur,
5392 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5394 CursorSlice const & beg = cur.selBegin();
5395 CursorSlice const & end = cur.selEnd();
5396 cs = tabular.cellColumn(beg.idx());
5397 ce = tabular.cellColumn(end.idx());
5401 rs = tabular.cellRow(beg.idx());
5402 re = tabular.cellRow(end.idx());
5408 Text * InsetTabular::getText(int idx) const
5410 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5414 void InsetTabular::setChange(Change const & change)
5416 for (idx_type idx = 0; idx < nargs(); ++idx)
5417 cell(idx)->setChange(change);
5421 void InsetTabular::acceptChanges()
5423 for (idx_type idx = 0; idx < nargs(); ++idx)
5424 cell(idx)->acceptChanges();
5428 void InsetTabular::rejectChanges()
5430 for (idx_type idx = 0; idx < nargs(); ++idx)
5431 cell(idx)->rejectChanges();
5435 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5437 return tabular.getPWidth(cell).zero();
5441 bool InsetTabular::forcePlainLayout(idx_type cell) const
5443 return !tabular.getPWidth(cell).zero();
5447 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5450 if (buf.length() <= 0)
5455 col_type maxCols = 1;
5456 size_t const len = buf.length();
5460 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5468 maxCols = max(cols, maxCols);
5474 maxCols = max(cols, maxCols);
5480 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5481 loctab = paste_tabular.get();
5483 dirtyTabularStack(true);
5486 cell = bv.cursor().idx();
5487 ocol = tabular.cellColumn(cell);
5488 row = tabular.cellRow(cell);
5492 idx_type const cells = loctab->numberofcells;
5495 rows = loctab->nrows();
5496 col_type const columns = loctab->ncols();
5498 while (cell < cells && p < len && row < rows &&
5499 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5505 // we can only set this if we are not too far right
5506 if (cols < columns) {
5507 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5508 Font const font = bv.textMetrics(&inset->text()).
5510 inset->setText(buf.substr(op, p - op), font,
5511 buffer().params().trackChanges);
5517 // we can only set this if we are not too far right
5518 if (cols < columns) {
5519 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5520 Font const font = bv.textMetrics(&inset->text()).
5522 inset->setText(buf.substr(op, p - op), font,
5523 buffer().params().trackChanges);
5528 cell = loctab->cellIndex(row, cols);
5534 // check for the last cell if there is no trailing '\n'
5535 if (cell < cells && op < len) {
5536 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5537 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5538 inset->setText(buf.substr(op, len - op), font,
5539 buffer().params().trackChanges);
5545 void InsetTabular::addPreview(DocIterator const & inset_pos,
5546 PreviewLoader & loader) const
5548 DocIterator cell_pos = inset_pos;
5550 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5551 for (row_type r = 0; r < tabular.nrows(); ++r) {
5552 for (col_type c = 0; c < tabular.ncols(); ++c) {
5553 cell_pos.top().idx() = tabular.cellIndex(r, c);
5554 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5560 bool InsetTabular::completionSupported(Cursor const & cur) const
5562 Cursor const & bvCur = cur.bv().cursor();
5563 if (&bvCur.inset() != this)
5565 return cur.text()->completionSupported(cur);
5569 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5571 return completionSupported(cur);
5575 bool InsetTabular::automaticInlineCompletion() const
5577 return lyxrc.completion_inline_text;
5581 bool InsetTabular::automaticPopupCompletion() const
5583 return lyxrc.completion_popup_text;
5587 bool InsetTabular::showCompletionCursor() const
5589 return lyxrc.completion_cursor_text;
5593 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5595 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5599 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5601 if (!completionSupported(cur))
5603 return cur.text()->completionPrefix(cur);
5607 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5609 if (!completionSupported(cur))
5612 return cur.text()->insertCompletion(cur, s, finished);
5616 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5617 Dimension & dim) const
5619 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5620 tm.completionPosAndDim(cur, x, y, dim);
5624 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5626 istringstream data(in);
5628 lex.setStream(data);
5635 if (!lex || token != "tabular") {
5636 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5640 // This is part of the inset proper that is usually swallowed
5641 // by Buffer::readInset
5643 if (!lex || token != "Tabular") {
5644 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5652 string InsetTabular::params2string(InsetTabular const & inset)
5655 data << "tabular" << ' ';
5657 data << "\\end_inset\n";