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::TOGGLE_LINE_TOP, "toggle-line-top", false },
117 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
118 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
119 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
120 { Tabular::ALIGN_LEFT, "align-left", false },
121 { Tabular::ALIGN_RIGHT, "align-right", false },
122 { Tabular::ALIGN_CENTER, "align-center", false },
123 { Tabular::ALIGN_BLOCK, "align-block", false },
124 { Tabular::VALIGN_TOP, "valign-top", false },
125 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
126 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
127 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
128 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
129 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
130 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
131 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
132 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
133 { Tabular::MULTICOLUMN, "multicolumn", false },
134 { Tabular::MULTIROW, "multirow", false },
135 { Tabular::SET_ALL_LINES, "set-all-lines", false },
136 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
137 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
138 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
139 { Tabular::SET_PWIDTH, "set-pwidth", true },
140 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
141 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
142 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
143 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
144 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
145 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
146 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
147 { Tabular::SET_USEBOX, "set-usebox", true },
148 { Tabular::SET_LTHEAD, "set-lthead", true },
149 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
150 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
151 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
152 { Tabular::SET_LTFOOT, "set-ltfoot", true },
153 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
154 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
155 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
156 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
157 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
158 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
159 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
160 { Tabular::SET_SPECIAL_MULTIROW, "set-special-multirow", false },
161 { Tabular::SET_BOOKTABS, "set-booktabs", false },
162 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
163 { Tabular::SET_TOP_SPACE, "set-top-space", true },
164 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
165 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
166 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
167 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top"},
168 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle"},
169 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom"},
170 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
171 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
172 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
173 { Tabular::LAST_ACTION, "", false }
178 string const write_attribute(string const & name, T const & t)
180 string const s = tostr(t);
181 return s.empty() ? s : " " + name + "=\"" + s + "\"";
185 string const write_attribute(string const & name, string const & t)
187 return t.empty() ? t : " " + name + "=\"" + t + "\"";
192 string const write_attribute(string const & name, docstring const & t)
194 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
199 string const write_attribute(string const & name, bool const & b)
201 // we write only true attribute values so we remove a bit of the
202 // file format bloat for tabulars.
203 return b ? write_attribute(name, convert<string>(b)) : string();
208 string const write_attribute(string const & name, int const & i)
210 // we write only true attribute values so we remove a bit of the
211 // file format bloat for tabulars.
212 return i ? write_attribute(name, convert<string>(i)) : string();
217 string const write_attribute(string const & name, Tabular::idx_type const & i)
219 // we write only true attribute values so we remove a bit of the
220 // file format bloat for tabulars.
221 return i ? write_attribute(name, convert<string>(i)) : string();
226 string const write_attribute(string const & name, Length const & value)
228 // we write only the value if we really have one same reson as above.
229 return value.zero() ? string() : write_attribute(name, value.asString());
233 string const tostr(LyXAlignment const & num)
238 case LYX_ALIGN_BLOCK:
242 case LYX_ALIGN_CENTER:
244 case LYX_ALIGN_RIGHT:
246 case LYX_ALIGN_LAYOUT:
248 case LYX_ALIGN_SPECIAL:
255 string const tostr(Tabular::HAlignment const & num)
258 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
260 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
262 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
269 string const tostr(Tabular::VAlignment const & num)
272 case Tabular::LYX_VALIGN_TOP:
274 case Tabular::LYX_VALIGN_MIDDLE:
276 case Tabular::LYX_VALIGN_BOTTOM:
283 string const tostr(Tabular::BoxType const & num)
286 case Tabular::BOX_NONE:
288 case Tabular::BOX_PARBOX:
290 case Tabular::BOX_MINIPAGE:
297 // I would have liked a fromstr template a lot better. (Lgb)
298 bool string2type(string const str, LyXAlignment & num)
301 num = LYX_ALIGN_NONE;
302 else if (str == "block")
303 num = LYX_ALIGN_BLOCK;
304 else if (str == "left")
305 num = LYX_ALIGN_LEFT;
306 else if (str == "center")
307 num = LYX_ALIGN_CENTER;
308 else if (str == "right")
309 num = LYX_ALIGN_RIGHT;
316 bool string2type(string const str, Tabular::HAlignment & num)
319 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
320 else if (str == "center" )
321 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
322 else if (str == "right")
323 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
330 bool string2type(string const str, Tabular::VAlignment & num)
333 num = Tabular::LYX_VALIGN_TOP;
334 else if (str == "middle" )
335 num = Tabular::LYX_VALIGN_MIDDLE;
336 else if (str == "bottom")
337 num = Tabular::LYX_VALIGN_BOTTOM;
344 bool string2type(string const str, Tabular::BoxType & num)
347 num = Tabular::BOX_NONE;
348 else if (str == "parbox")
349 num = Tabular::BOX_PARBOX;
350 else if (str == "minipage")
351 num = Tabular::BOX_MINIPAGE;
358 bool string2type(string const str, bool & num)
362 else if (str == "false")
370 bool getTokenValue(string const & str, char const * token, string & ret)
373 size_t token_length = strlen(token);
374 size_t pos = str.find(token);
376 if (pos == string::npos || pos + token_length + 1 >= str.length()
377 || str[pos + token_length] != '=')
379 pos += token_length + 1;
381 if (ch != '"' && ch != '\'') { // only read till next space
385 while (pos < str.length() - 1 && str[++pos] != ch)
392 bool getTokenValue(string const & str, char const * token, docstring & ret)
395 bool const success = getTokenValue(str, token, tmp);
396 ret = from_utf8(tmp);
401 bool getTokenValue(string const & str, char const * token, int & num)
405 if (!getTokenValue(str, token, tmp))
407 num = convert<int>(tmp);
412 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
415 return getTokenValue(str, token, tmp) && string2type(tmp, num);
419 bool getTokenValue(string const & str, char const * token,
420 Tabular::HAlignment & num)
423 return getTokenValue(str, token, tmp) && string2type(tmp, num);
427 bool getTokenValue(string const & str, char const * token,
428 Tabular::VAlignment & num)
431 return getTokenValue(str, token, tmp) && string2type(tmp, num);
435 bool getTokenValue(string const & str, char const * token,
436 Tabular::BoxType & num)
439 return getTokenValue(str, token, tmp) && string2type(tmp, num);
443 bool getTokenValue(string const & str, char const * token, bool & flag)
445 // set the flag always to false as this should be the default for bools
446 // not in the file-format.
449 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
453 bool getTokenValue(string const & str, char const * token, Length & len)
455 // set the length to be zero() as default as this it should be if not
456 // in the file format.
459 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
463 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
468 if (!getTokenValue(str, token, tmp))
470 if (tmp == "default") {
474 return isValidLength(tmp, &len);
478 void l_getline(istream & is, string & str)
481 while (str.empty()) {
483 if (!str.empty() && str[str.length() - 1] == '\r')
484 str.erase(str.length() - 1);
491 string const featureAsString(Tabular::Feature action)
493 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
494 if (tabularFeature[i].action == action)
495 return tabularFeature[i].feature;
502 /////////////////////////////////////////////////////////////////////
506 /////////////////////////////////////////////////////////////////////
509 Tabular::CellData::CellData(Buffer * buf)
512 multicolumn(Tabular::CELL_NORMAL),
513 multirow(Tabular::CELL_NORMAL),
514 alignment(LYX_ALIGN_CENTER),
515 valignment(LYX_VALIGN_TOP),
522 inset(new InsetTableCell(buf))
524 inset->setBuffer(*buf);
528 Tabular::CellData::CellData(CellData const & cs)
531 multicolumn(cs.multicolumn),
532 multirow(cs.multirow),
533 alignment(cs.alignment),
534 valignment(cs.valignment),
535 top_line(cs.top_line),
536 bottom_line(cs.bottom_line),
537 left_line(cs.left_line),
538 right_line(cs.right_line),
541 align_special(cs.align_special),
543 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
547 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
553 void Tabular::CellData::swap(CellData & rhs)
555 std::swap(cellno, rhs.cellno);
556 std::swap(width, rhs.width);
557 std::swap(multicolumn, rhs.multicolumn);
558 std::swap(multirow, rhs.multirow);
559 std::swap(alignment, rhs.alignment);
560 std::swap(valignment, rhs.valignment);
561 std::swap(top_line, rhs.top_line);
562 std::swap(bottom_line, rhs.bottom_line);
563 std::swap(left_line, rhs.left_line);
564 std::swap(right_line, rhs.right_line);
565 std::swap(usebox, rhs.usebox);
566 std::swap(rotate, rhs.rotate);
567 std::swap(align_special, rhs.align_special);
568 p_width.swap(rhs.p_width);
569 inset.swap(rhs.inset);
573 Tabular::RowData::RowData()
576 top_space_default(false),
577 bottom_space_default(false),
578 interline_space_default(false),
588 Tabular::ColumnData::ColumnData()
589 : alignment(LYX_ALIGN_CENTER),
590 valignment(LYX_VALIGN_TOP),
596 Tabular::ltType::ltType()
603 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
605 init(buffer, rows_arg, columns_arg);
609 void Tabular::setBuffer(Buffer & buffer)
612 row_type const nrows = row_info.size();
613 col_type const ncols = column_info.size();
614 for (row_type i = 0; i < nrows; ++i)
615 for (col_type j = 0; j < ncols; ++j)
616 cell_info[i][j].inset->setBuffer(*buffer_);
620 // activates all lines and sets all widths to 0
621 void Tabular::init(Buffer * buf, row_type rows_arg,
622 col_type columns_arg)
625 row_info = row_vector(rows_arg);
626 column_info = column_vector(columns_arg);
627 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
628 row_info.reserve(10);
629 column_info.reserve(10);
630 cell_info.reserve(100);
632 is_long_tabular = false;
633 tabular_valignment = LYX_VALIGN_MIDDLE;
634 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
636 use_booktabs = false;
637 size_t row_count = row_info.size();
638 size_t column_count = column_info.size();
639 // set silly default lines
640 for (row_type i = 0; i < row_count; ++i)
641 for (col_type j = 0; j < column_count; ++j) {
642 cell_info[i][j].inset->setBuffer(*buffer_);
643 cell_info[i][j].top_line = true;
644 cell_info[i][j].left_line = true;
645 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
646 cell_info[i][j].right_line = j == column_count - 1;
651 void Tabular::appendRow(idx_type const cell)
653 row_type const row = cellRow(cell);
655 row_info.insert(row_info.begin() + row + 1, RowData());
656 row_info[row + 1] = row_info[row];
658 col_type const ncols = column_info.size();
659 cell_info.insert(cell_info.begin() + row + 1,
660 cell_vector(ncols, CellData(buffer_)));
661 for (col_type c = 0; c < ncols; ++c) {
662 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
663 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
665 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
669 for (col_type c = 0; c < ncols; ++c) {
670 if (isPartOfMultiRow(row, c))
672 // inherit line settings
673 idx_type const i = cellIndex(row + 1, c);
674 idx_type const j = cellIndex(row, c);
675 setLeftLine(i, leftLine(j));
676 setRightLine(i, rightLine(j));
677 setTopLine(i, topLine(j));
678 if (topLine(j) && bottomLine(j)) {
679 setBottomLine(i, true);
680 setBottomLine(j, false);
682 // mark track changes
683 if (buffer().params().trackChanges)
684 cellInfo(i).inset->setChange(Change(Change::INSERTED));
689 void Tabular::deleteRow(row_type const row)
691 // Not allowed to delete last row
692 if (row_info.size() == 1)
695 size_t const column_count = column_info.size();
696 for (col_type i = 0; i < column_count; ++i) {
697 // Care about multirow cells
698 if (row + 1 < row_info.size() &&
699 cell_info[row][i].multirow == CELL_BEGIN_OF_MULTIROW &&
700 cell_info[row][i + 1].multirow == CELL_PART_OF_MULTIROW) {
701 cell_info[row][i + 1].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 j = 0; j < column_info.size(); ++j)
717 cell_info[row + 1][j].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 row_type const nrows = row_info.size();
731 for (row_type r = 0; r < nrows; ++r) {
732 cell_info[r].insert(cell_info[r].begin() + c + 1,
734 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
735 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
737 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
740 for (row_type r = 0; r < nrows; ++r) {
741 // inherit line settings
742 idx_type const i = cellIndex(r, c + 1);
743 idx_type const j = cellIndex(r, c);
744 setBottomLine(i, bottomLine(j));
745 setTopLine(i, topLine(j));
746 setLeftLine(i, leftLine(j));
747 if (rightLine(j) && rightLine(j)) {
748 setRightLine(i, true);
749 setRightLine(j, false);
751 if (buffer().params().trackChanges)
752 cellInfo(i).inset->setChange(Change(Change::INSERTED));
757 void Tabular::deleteColumn(col_type const col)
759 // Not allowed to delete last column
760 if (column_info.size() == 1)
763 row_type const nrows = row_info.size();
764 for (row_type r = 0; r < nrows; ++r) {
765 // Care about multicolumn cells
766 if (col + 1 < column_info.size() &&
767 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
768 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
769 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
771 cell_info[r].erase(cell_info[r].begin() + col);
773 column_info.erase(column_info.begin() + col);
778 void Tabular::copyColumn(col_type const col)
780 BufferParams const & bp = buffer().params();
781 column_info.insert(column_info.begin() + col, column_info[col]);
783 row_type nrows = row_info.size();
784 for (row_type r = 0; r < nrows; ++r) {
785 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
787 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
793 void Tabular::updateIndexes()
796 col_type ncols = column_info.size();
797 row_type nrows = row_info.size();
799 for (row_type row = 0; row < nrows; ++row)
800 for (col_type column = 0; column < ncols; ++column) {
801 if (!isPartOfMultiColumn(row, column)
802 && !isPartOfMultiRow(row, column))
804 if (isPartOfMultiRow(row, column))
805 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
807 cell_info[row][column].cellno = numberofcells - 1;
810 rowofcell.resize(numberofcells);
811 columnofcell.resize(numberofcells);
813 for (row_type row = 0; row < nrows; ++row)
814 for (col_type column = 0; column < ncols; ++column) {
815 if (isPartOfMultiColumn(row, column)
816 || isPartOfMultiRow(row, column))
819 columnofcell[i] = column;
820 setFixedWidth(row, column);
821 updateContentAlignment(row, column);
827 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
830 for (col_type i = 0; i < column_info.size(); ++i)
831 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
837 bool Tabular::topLine(idx_type const cell) const
839 return cellInfo(cell).top_line;
843 bool Tabular::bottomLine(idx_type const cell) const
845 return cellInfo(cell).bottom_line;
849 bool Tabular::leftLine(idx_type cell) const
853 return cellInfo(cell).left_line;
857 bool Tabular::rightLine(idx_type cell) const
861 return cellInfo(cell).right_line;
865 bool Tabular::topAlreadyDrawn(idx_type cell) const
867 row_type const row = cellRow(cell);
871 return !rowTopLine(row) && bottomLine(cellAbove(cell));
875 bool Tabular::leftAlreadyDrawn(idx_type cell) const
877 col_type col = cellColumn(cell);
880 idx_type i = cellIndex(cellRow(cell), col - 1);
881 return rightLine(i) && !leftLine(cell);
885 bool Tabular::isLastRow(idx_type cell) const
887 return cellRow(cell) == row_info.size() - 1;
891 int Tabular::interRowSpace(row_type row) const
893 if (!row || row >= row_info.size())
896 int const interline_space = row_info[row - 1].interline_space_default ?
898 row_info[row - 1].interline_space.inPixels(width());
899 if (rowTopLine(row) && rowBottomLine(row - 1))
900 return interline_space + WIDTH_OF_LINE;
901 return interline_space;
905 int Tabular::interColumnSpace(idx_type cell) const
907 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
908 if (rightLine(cell) && nextcol < column_info.size()
909 && leftLine(cellIndex(cellRow(cell), nextcol)))
910 return WIDTH_OF_LINE;
915 int Tabular::columnWidth(idx_type cell) const
918 col_type const span = columnSpan(cell);
919 col_type const col = cellColumn(cell);
920 for(col_type c = col; c < col + span ; ++c)
921 w += column_info[c].width;
926 int Tabular::rowHeight(idx_type cell) const
928 row_type const span = rowSpan(cell);
929 row_type const row = cellRow(cell);
930 int h = rowAscent(row) + rowDescent(row);
932 for(row_type r = row; r < row + span ; ++r) {
935 h += interRowSpace(r);
937 if (r < row + span - 1)
944 bool Tabular::updateColumnWidths()
946 col_type const ncols = column_info.size();
947 row_type const nrows = row_info.size();
949 // for each col get max of single col cells
950 for(col_type c = 0; c < ncols; ++c) {
952 for(row_type r = 0; r < nrows; ++r) {
953 idx_type const i = cellIndex(r, c);
954 if (columnSpan(i) == 1)
955 new_width = max(new_width, cellInfo(i).width);
958 if (column_info[c].width != new_width) {
959 column_info[c].width = new_width;
963 // update col widths to fit merged cells
964 for(col_type c = 0; c < ncols; ++c)
965 for(row_type r = 0; r < nrows; ++r) {
966 idx_type const i = cellIndex(r, c);
967 int const span = columnSpan(i);
968 if (span == 1 || c > cellColumn(i))
972 for(col_type j = c; j < c + span ; ++j)
973 old_width += column_info[j].width;
975 if (cellInfo(i).width > old_width) {
976 column_info[c + span - 1].width += cellInfo(i).width - old_width;
985 int Tabular::width() const
987 col_type const ncols = column_info.size();
989 for (col_type i = 0; i < ncols; ++i)
990 width += column_info[i].width;
995 void Tabular::setCellWidth(idx_type cell, int new_width)
997 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
998 + interColumnSpace(cell);
1002 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1005 if (!isMultiColumn(cell) || onlycolumn)
1006 column_info[cellColumn(cell)].alignment = align;
1008 cellInfo(cell).alignment = align;
1009 cellInset(cell).get()->setContentAlignment(align);
1013 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1016 if (!isMultiColumn(cell) || onlycolumn)
1017 column_info[cellColumn(cell)].valignment = align;
1019 cellInfo(cell).valignment = align;
1026 * Allow line and paragraph breaks for fixed width cells or disallow them,
1027 * merge cell paragraphs and reset layout to standard for variable width
1030 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1032 inset->setAutoBreakRows(fixedWidth);
1033 inset->toggleFixedWidth(fixedWidth);
1037 // merge all paragraphs to one
1038 BufferParams const & bp = cur.bv().buffer().params();
1039 while (inset->paragraphs().size() > 1)
1040 mergeParagraph(bp, inset->paragraphs(), 0);
1044 // undo information has already been recorded
1045 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1046 bp.documentClass().plainLayoutName());
1053 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1054 Length const & width)
1056 col_type const j = cellColumn(cell);
1058 column_info[j].p_width = width;
1059 for (row_type i = 0; i < row_info.size(); ++i) {
1060 idx_type const cell = cellIndex(i, j);
1061 // because of multicolumns
1062 toggleFixedWidth(cur, cellInset(cell).get(),
1063 !getPWidth(cell).zero());
1065 // cur paragraph can become invalid after paragraphs were merged
1066 if (cur.pit() > cur.lastpit())
1067 cur.pit() = cur.lastpit();
1068 // cur position can become invalid after newlines were removed
1069 if (cur.pos() > cur.lastpos())
1070 cur.pos() = cur.lastpos();
1074 bool Tabular::setFixedWidth(row_type r, col_type c)
1076 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1077 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1078 || (multicol && !cell_info[r][c].p_width.zero());
1079 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1084 void Tabular::updateContentAlignment(row_type r, col_type c)
1086 cell_info[r][c].inset->setContentAlignment(
1087 getAlignment(cellIndex(r, c)));
1091 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1092 Length const & width)
1094 if (!isMultiColumn(cell))
1097 cellInfo(cell).p_width = width;
1098 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1099 // cur paragraph can become invalid after paragraphs were merged
1100 if (cur.pit() > cur.lastpit())
1101 cur.pit() = cur.lastpit();
1102 // cur position can become invalid after newlines were removed
1103 if (cur.pos() > cur.lastpos())
1104 cur.pos() = cur.lastpos();
1109 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1110 Tabular::Feature what)
1112 if (what == SET_SPECIAL_MULTICOLUMN)
1113 cellInfo(cell).align_special = special;
1115 column_info[cellColumn(cell)].align_special = special;
1119 void Tabular::setAllLines(idx_type cell, bool line)
1121 setTopLine(cell, line);
1122 setBottomLine(cell, line);
1123 setRightLine(cell, line);
1124 setLeftLine(cell, line);
1128 void Tabular::setTopLine(idx_type i, bool line)
1130 cellInfo(i).top_line = line;
1134 void Tabular::setBottomLine(idx_type i, bool line)
1136 cellInfo(i).bottom_line = line;
1140 void Tabular::setLeftLine(idx_type cell, bool line)
1142 cellInfo(cell).left_line = line;
1146 void Tabular::setRightLine(idx_type cell, bool line)
1148 cellInfo(cell).right_line = line;
1151 bool Tabular::rowTopLine(row_type r) const
1153 col_type const ncols = column_info.size();
1154 bool all_rows_set = true;
1155 for (col_type c = 0; all_rows_set && c < ncols; ++c) {
1156 idx_type const i = cellIndex(r, c);
1157 all_rows_set = cellInfo(i).top_line;
1159 return all_rows_set;
1163 bool Tabular::rowBottomLine(row_type r) const
1165 col_type const ncols = column_info.size();
1166 bool all_rows_set = true;
1167 for (col_type c = 0; all_rows_set && c < ncols; ++c) {
1168 idx_type const i = cellIndex(r, c);
1169 all_rows_set = cellInfo(i).bottom_line;
1171 return all_rows_set;
1175 bool Tabular::columnLeftLine(col_type c) const
1182 row_type const nrows = row_info.size();
1183 for (row_type r = 0; r < nrows; ++r) {
1184 idx_type i = cellIndex(r, c);
1185 if (c == cellColumn(i)) {
1187 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1188 if (cellInfo(i).left_line || right)
1192 return 2 * nrows_left >= total;
1196 bool Tabular::columnRightLine(col_type c) const
1201 int nrows_right = 0;
1203 row_type const nrows = row_info.size();
1204 for (row_type r = 0; r < nrows; ++r) {
1205 idx_type i = cellIndex(r, c);
1206 if (c == cellColumn(i) + columnSpan(i) - 1) {
1208 bool left = (c + 1 < column_info.size()
1209 && cellInfo(cellIndex(r, c + 1)).left_line)
1210 || c + 1 == column_info.size();
1211 if (cellInfo(i).right_line && left)
1215 return 2 * nrows_right >= total;
1219 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1221 if (!onlycolumn && isMultiColumn(cell))
1222 return cellInfo(cell).alignment;
1223 return column_info[cellColumn(cell)].alignment;
1228 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1230 if (!onlycolumn && isMultiColumn(cell))
1231 return cellInfo(cell).valignment;
1232 return column_info[cellColumn(cell)].valignment;
1236 Length const Tabular::getPWidth(idx_type cell) const
1238 if (isMultiColumn(cell))
1239 return cellInfo(cell).p_width;
1240 return column_info[cellColumn(cell)].p_width;
1244 int Tabular::cellWidth(idx_type cell) const
1246 return cellInfo(cell).width;
1250 int Tabular::textHOffset(idx_type cell) const
1252 // the LaTeX Way :-(
1253 int x = WIDTH_OF_LINE;
1255 switch (getAlignment(cell)) {
1256 case LYX_ALIGN_CENTER:
1257 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1259 case LYX_ALIGN_RIGHT:
1260 x += columnWidth(cell) - cellWidth(cell);
1261 // + interColumnSpace(cell);
1264 // LYX_ALIGN_LEFT: nothing :-)
1272 int Tabular::textVOffset(idx_type cell) const
1274 int h = rowHeight(cell);
1277 switch (getVAlignment(cell)) {
1278 case LYX_VALIGN_TOP:
1280 case LYX_VALIGN_MIDDLE:
1283 case LYX_VALIGN_BOTTOM:
1292 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1295 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1297 return cell_info[row][c].cellno;
1301 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1303 col_type c = column_info.size() - 1;
1304 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1306 return cell_info[row][c].cellno;
1310 Tabular::row_type Tabular::cellRow(idx_type cell) const
1312 if (cell >= numberofcells)
1313 return row_info.size() - 1;
1316 return rowofcell[cell];
1320 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1322 if (cell >= numberofcells)
1323 return column_info.size() - 1;
1326 return columnofcell[cell];
1330 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1332 row_type const row = cellRow(cell);
1333 col_type column = cellColumn(cell);
1334 while (column < column_info.size() - 1 &&
1335 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1341 void Tabular::write(ostream & os) const
1345 << write_attribute("version", 3)
1346 << write_attribute("rows", row_info.size())
1347 << write_attribute("columns", column_info.size())
1349 // global longtable options
1351 << write_attribute("rotate", rotate)
1352 << write_attribute("booktabs", use_booktabs)
1353 << write_attribute("islongtable", is_long_tabular)
1354 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1355 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1356 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1357 << write_attribute("headTopDL", endhead.topDL)
1358 << write_attribute("headBottomDL", endhead.bottomDL)
1359 << write_attribute("footTopDL", endfoot.topDL)
1360 << write_attribute("footBottomDL", endfoot.bottomDL)
1361 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1362 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1363 << write_attribute("lastFootEmpty", endlastfoot.empty);
1364 // longtables cannot be aligned vertically
1365 if (!is_long_tabular)
1366 os << write_attribute("tabularvalignment", tabular_valignment);
1367 if (is_long_tabular)
1368 os << write_attribute("longtabularalignment",
1369 longtabular_alignment);
1371 for (col_type j = 0; j < column_info.size(); ++j) {
1373 << write_attribute("alignment", column_info[j].alignment)
1374 << write_attribute("valignment", column_info[j].valignment)
1375 << write_attribute("width", column_info[j].p_width.asString())
1376 << write_attribute("special", column_info[j].align_special)
1379 for (row_type i = 0; i < row_info.size(); ++i) {
1380 static const string def("default");
1382 if (row_info[i].top_space_default)
1383 os << write_attribute("topspace", def);
1385 os << write_attribute("topspace", row_info[i].top_space);
1386 if (row_info[i].bottom_space_default)
1387 os << write_attribute("bottomspace", def);
1389 os << write_attribute("bottomspace", row_info[i].bottom_space);
1390 if (row_info[i].interline_space_default)
1391 os << write_attribute("interlinespace", def);
1393 os << write_attribute("interlinespace", row_info[i].interline_space);
1394 os << write_attribute("endhead", row_info[i].endhead)
1395 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1396 << write_attribute("endfoot", row_info[i].endfoot)
1397 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1398 << write_attribute("newpage", row_info[i].newpage)
1399 << write_attribute("caption", row_info[i].caption)
1401 for (col_type j = 0; j < column_info.size(); ++j) {
1403 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1404 << write_attribute("multirow", cell_info[i][j].multirow)
1405 << write_attribute("alignment", cell_info[i][j].alignment)
1406 << write_attribute("valignment", cell_info[i][j].valignment)
1407 << write_attribute("topline", cell_info[i][j].top_line)
1408 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1409 << write_attribute("leftline", cell_info[i][j].left_line)
1410 << write_attribute("rightline", cell_info[i][j].right_line)
1411 << write_attribute("rotate", cell_info[i][j].rotate)
1412 << write_attribute("usebox", cell_info[i][j].usebox)
1413 << write_attribute("width", cell_info[i][j].p_width)
1414 << write_attribute("special", cell_info[i][j].align_special)
1416 os << "\\begin_inset ";
1417 cell_info[i][j].inset->write(os);
1418 os << "\n\\end_inset\n"
1423 os << "</lyxtabular>\n";
1427 void Tabular::read(Lexer & lex)
1430 istream & is = lex.getStream();
1432 l_getline(is, line);
1433 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1434 LASSERT(false, /**/);
1439 if (!getTokenValue(line, "version", version))
1441 LASSERT(version >= 2, /**/);
1444 if (!getTokenValue(line, "rows", rows_arg))
1447 if (!getTokenValue(line, "columns", columns_arg))
1449 init(buffer_, rows_arg, columns_arg);
1450 l_getline(is, line);
1451 if (!prefixIs(line, "<features")) {
1452 lyxerr << "Wrong tabular format (expected <features ...> got"
1453 << line << ')' << endl;
1456 getTokenValue(line, "rotate", rotate);
1457 getTokenValue(line, "booktabs", use_booktabs);
1458 getTokenValue(line, "islongtable", is_long_tabular);
1459 getTokenValue(line, "tabularvalignment", tabular_valignment);
1460 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1461 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1462 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1463 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1464 getTokenValue(line, "headTopDL", endhead.topDL);
1465 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1466 getTokenValue(line, "footTopDL", endfoot.topDL);
1467 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1468 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1469 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1470 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1472 for (col_type j = 0; j < column_info.size(); ++j) {
1474 if (!prefixIs(line,"<column")) {
1475 lyxerr << "Wrong tabular format (expected <column ...> got"
1476 << line << ')' << endl;
1479 getTokenValue(line, "alignment", column_info[j].alignment);
1480 getTokenValue(line, "valignment", column_info[j].valignment);
1481 getTokenValue(line, "width", column_info[j].p_width);
1482 getTokenValue(line, "special", column_info[j].align_special);
1485 for (row_type i = 0; i < row_info.size(); ++i) {
1486 l_getline(is, line);
1487 if (!prefixIs(line, "<row")) {
1488 lyxerr << "Wrong tabular format (expected <row ...> got"
1489 << line << ')' << endl;
1492 getTokenValue(line, "topspace", row_info[i].top_space,
1493 row_info[i].top_space_default);
1494 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1495 row_info[i].bottom_space_default);
1496 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1497 row_info[i].interline_space_default);
1498 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1499 getTokenValue(line, "endhead", row_info[i].endhead);
1500 getTokenValue(line, "endfoot", row_info[i].endfoot);
1501 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1502 getTokenValue(line, "newpage", row_info[i].newpage);
1503 getTokenValue(line, "caption", row_info[i].caption);
1504 for (col_type j = 0; j < column_info.size(); ++j) {
1505 l_getline(is, line);
1506 if (!prefixIs(line, "<cell")) {
1507 lyxerr << "Wrong tabular format (expected <cell ...> got"
1508 << line << ')' << endl;
1511 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1512 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1513 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1514 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1515 getTokenValue(line, "topline", cell_info[i][j].top_line);
1516 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1517 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1518 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1519 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1520 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1521 getTokenValue(line, "width", cell_info[i][j].p_width);
1523 getTokenValue(line, "special", cell_info[i][j].align_special);
1524 l_getline(is, line);
1525 if (prefixIs(line, "\\begin_inset")) {
1526 cell_info[i][j].inset->setBuffer(*buffer_);
1527 cell_info[i][j].inset->read(lex);
1528 l_getline(is, line);
1530 if (!prefixIs(line, "</cell>")) {
1531 lyxerr << "Wrong tabular format (expected </cell> got"
1532 << line << ')' << endl;
1536 l_getline(is, line);
1537 if (!prefixIs(line, "</row>")) {
1538 lyxerr << "Wrong tabular format (expected </row> got"
1539 << line << ')' << endl;
1543 while (!prefixIs(line, "</lyxtabular>")) {
1544 l_getline(is, line);
1550 bool Tabular::isMultiColumn(idx_type cell) const
1552 if (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1553 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN)
1560 bool Tabular::isMultiColumnReal(idx_type cell) const
1562 return cellColumn(cell) != cellRightColumn(cell) &&
1563 (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1564 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1568 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1570 return cell_info[cellRow(cell)][cellColumn(cell)];
1574 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1576 CellData & cs = cellInfo(cell);
1577 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1578 cs.alignment = column_info[cellColumn(cell)].alignment;
1579 setRightLine(cell, rightLine(cell + number - 1));
1580 for (idx_type i = 1; i < number; ++i) {
1581 CellData & cs1 = cellInfo(cell + i);
1582 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1583 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1590 bool Tabular::isMultiRow(idx_type cell) const
1592 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1593 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1597 void Tabular::setMultiRow(idx_type cell, idx_type number)
1599 idx_type const column = cellColumn(cell);
1600 idx_type row = cellRow(cell);
1601 idx_type const ncolumns = column_info.size();
1602 CellData & cs = cellInfo(cell);
1603 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1604 // FIXME: the horizontal alignment can only be changed for
1605 // the whole table, support for this needs to be implemented
1606 // assigning this to uwestoehr
1607 cs.valignment = LYX_VALIGN_MIDDLE;
1608 // set the bottom row of the last selected cell
1609 setBottomLine(cell, bottomLine(cell + (number - 1)*ncolumns));
1610 for (idx_type i = 1; i < number; ++i) {
1611 CellData & cs1 = cell_info[row + i][column];
1612 cs1.multirow = CELL_PART_OF_MULTIROW;
1613 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1620 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1622 row_type const row = cellRow(cell);
1623 col_type const ncols = column_info.size();
1624 col_type column = cellColumn(cell) + 1;
1625 while (column < ncols && isPartOfMultiColumn(row, column))
1628 return column - cellColumn(cell);
1632 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1634 row_type const nrows = row_info.size();
1635 col_type const column = cellColumn(cell);
1636 col_type row = cellRow(cell) + 1;
1637 while (row < nrows && isPartOfMultiRow(row, column))
1640 return row - cellRow(cell);
1644 void Tabular::unsetMultiColumn(idx_type cell)
1646 row_type const row = cellRow(cell);
1647 col_type const column = cellColumn(cell);
1648 row_type const span = columnSpan(cell);
1649 for (col_type c = 0; c < span; ++c)
1650 cell_info[row][column + c].multicolumn = CELL_NORMAL;
1655 void Tabular::unsetMultiRow(idx_type cell)
1657 row_type const row = cellRow(cell);
1658 col_type const column = cellColumn(cell);
1659 row_type const span = rowSpan(cell);
1660 for (row_type r = 0; r < span; ++r)
1661 cell_info[row + r][column].multirow = CELL_NORMAL;
1666 void Tabular::setRotateCell(idx_type cell, bool flag)
1668 cellInfo(cell).rotate = flag;
1672 bool Tabular::getRotateCell(idx_type cell) const
1674 return cellInfo(cell).rotate;
1678 bool Tabular::needRotating() const
1682 for (row_type i = 0; i < row_info.size(); ++i)
1683 for (col_type j = 0; j < column_info.size(); ++j)
1684 if (cell_info[i][j].rotate)
1690 bool Tabular::isLastCell(idx_type cell) const
1692 if (cell + 1 < numberofcells)
1698 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1700 if (cellRow(cell) == 0)
1703 col_type const col = cellColumn(cell);
1704 row_type r = cellRow(cell) - 1;
1705 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1708 return cell_info[r][col].cellno;
1712 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1714 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1715 if (nextrow < row_info.size())
1716 return cell_info[nextrow][cellColumn(cell)].cellno;
1721 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1723 LASSERT(column != npos && column < column_info.size()
1724 && row != npos && row < row_info.size(), /**/);
1725 return cell_info[row][column].cellno;
1729 void Tabular::setUsebox(idx_type cell, BoxType type)
1731 cellInfo(cell).usebox = type;
1735 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1736 // adds line breaks, see bug 4886.
1737 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1739 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1740 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1742 if (cellInfo(cell).usebox > 1)
1743 return cellInfo(cell).usebox;
1744 return useParbox(cell);
1749 // This are functions used for the longtable support
1751 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1757 row_info[row].endfirsthead = flag;
1761 row_info[row].endhead = flag;
1766 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1769 hd.set = haveLTHead();
1770 return row_info[row].endhead;
1774 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1777 hd.set = haveLTFirstHead();
1778 return row_info[row].endfirsthead;
1782 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1788 row_info[row].endlastfoot = flag;
1792 row_info[row].endfoot = flag;
1797 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1800 fd.set = haveLTFoot();
1801 return row_info[row].endfoot;
1805 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1808 fd.set = haveLTLastFoot();
1809 return row_info[row].endlastfoot;
1813 void Tabular::setLTNewPage(row_type row, bool what)
1815 row_info[row].newpage = what;
1819 bool Tabular::getLTNewPage(row_type row) const
1821 return row_info[row].newpage;
1825 bool Tabular::haveLTHead() const
1827 for (row_type i = 0; i < row_info.size(); ++i)
1828 if (row_info[i].endhead)
1834 bool Tabular::haveLTFirstHead() const
1836 if (endfirsthead.empty)
1838 for (row_type i = 0; i < row_info.size(); ++i)
1839 if (row_info[i].endfirsthead)
1845 bool Tabular::haveLTFoot() const
1847 for (row_type i = 0; i < row_info.size(); ++i)
1848 if (row_info[i].endfoot)
1854 bool Tabular::haveLTLastFoot() const
1856 if (endlastfoot.empty)
1858 for (row_type i = 0; i < row_info.size(); ++i)
1859 if (row_info[i].endlastfoot)
1865 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1867 idx_type i = getFirstCellInRow(row);
1869 setMultiColumn(i, numberOfCellsInRow(row));
1870 setTopLine(i, false);
1871 setBottomLine(i, false);
1872 setLeftLine(i, false);
1873 setRightLine(i, false);
1875 unsetMultiColumn(i);
1876 // When unsetting a caption row, also all existing
1877 // captions in this row must be dissolved.
1879 row_info[row].caption = what;
1884 bool Tabular::ltCaption(row_type row) const
1886 return row_info[row].caption;
1890 bool Tabular::haveLTCaption() const
1892 for (row_type i = 0; i < row_info.size(); ++i)
1893 if (row_info[i].caption)
1899 // end longtable support functions
1901 void Tabular::setRowAscent(row_type row, int height)
1903 if (row >= row_info.size() || row_info[row].ascent == height)
1905 row_info[row].ascent = height;
1909 void Tabular::setRowDescent(row_type row, int height)
1911 if (row >= row_info.size() || row_info[row].descent == height)
1913 row_info[row].descent = height;
1917 int Tabular::rowAscent(row_type row) const
1919 LASSERT(row < row_info.size(), /**/);
1920 return row_info[row].ascent;
1924 int Tabular::rowDescent(row_type row) const
1926 LASSERT(row < row_info.size(), /**/);
1927 return row_info[row].descent;
1931 int Tabular::height() const
1934 for (row_type row = 0; row < row_info.size(); ++row)
1935 height += rowAscent(row) + rowDescent(row) +
1941 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1943 LASSERT(row < row_info.size(), /**/);
1944 LASSERT(column < column_info.size(), /**/);
1945 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1949 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1951 LASSERT(row < row_info.size(), /**/);
1952 LASSERT(column < column_info.size(), /**/);
1953 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1957 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1959 // we only output complete row lines and the 1st row here, the rest
1960 // is done in Tabular::TeXBottomHLine(...)
1962 // get for each column the topline (if any)
1963 col_type const ncols = column_info.size();
1964 vector<bool> topline;
1966 for (col_type c = 0; c < ncols; ++c) {
1967 topline.push_back(topLine(cellIndex(row, c)));
1968 // If cell is part of a multirow and not the first cell of the
1969 // multirow, no line must be drawn.
1971 if (isMultiRow(cellIndex(row, c))
1972 && isMultiRow(cellIndex(row - 1, c)))
1978 // do nothing if empty first row, or incomplete row line after
1979 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1982 // only output complete row lines and the 1st row's clines
1983 if (nset == ncols) {
1985 os << (row == 0 ? "\\toprule " : "\\midrule ");
1989 } else if (row == 0) {
1990 for (col_type c = 0; c < ncols; ++c) {
1992 //babel makes the "-" character an active one, so we have to suppress this here
1993 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1994 if (lang == "slovak" || lang == "czech")
1995 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1996 "\\expandafter\\cline\\expandafter{\\expandafter")
1997 << c + 1 << "\\string-";
1999 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2000 // get to last column of line span
2001 while (c < ncols && topline[c])
2012 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2014 // we output bottomlines of row r and the toplines of row r+1
2015 // if the latter do not span the whole tabular
2017 // get the bottomlines of row r, and toplines in next row
2018 bool lastrow = row == row_info.size() - 1;
2019 col_type const ncols = column_info.size();
2020 vector<bool> bottomline, topline;
2021 bool nextrowset = true;
2022 for (col_type c = 0; c < ncols; ++c) {
2023 bottomline.push_back(bottomLine(cellIndex(row, c)));
2024 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2025 // If cell is part of a multirow and not the last or first cell of the
2026 // multirow, no line must be drawn.
2028 if (isMultiRow(cellIndex(row, c))
2029 && isMultiRow(cellIndex(row + 1, c))) {
2030 bottomline[c] = false;
2033 nextrowset &= topline[c];
2036 // combine this row's bottom lines and next row's toplines if necessary
2038 for (col_type c = 0; c < ncols; ++c) {
2040 bottomline[c] = bottomline[c] || topline[c];
2045 // do nothing if empty, OR incomplete row line with a topline in next row
2046 if (nset == 0 || (nextrowset && nset != ncols))
2049 if (nset == ncols) {
2051 os << (lastrow ? "\\bottomrule" : "\\midrule");
2055 for (col_type c = 0; c < ncols; ++c) {
2056 if (bottomline[c]) {
2057 //babel makes the "-" character an active one, so we have to suppress this here
2058 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2059 if (lang == "slovak" || lang == "czech")
2060 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2061 "\\expandafter\\cline\\expandafter{\\expandafter")
2062 << c + 1 << "\\string-";
2064 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2065 // get to last column of line span
2066 while (c < ncols && bottomline[c])
2077 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2078 bool & ismulticol, bool & ismultirow) const
2081 row_type const r = cellRow(cell);
2082 if (is_long_tabular && row_info[r].caption)
2085 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2086 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2087 // figure out how to set the lines
2088 // we always set double lines to the right of the cell
2089 col_type const c = cellColumn(cell);
2090 col_type const nextcol = c + columnSpan(cell);
2091 bool colright = columnRightLine(c);
2092 bool colleft = columnLeftLine(c);
2093 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
2094 bool nextcellleft = nextcol < column_info.size()
2095 && leftLine(cellIndex(r, nextcol));
2096 bool coldouble = colright && nextcolleft;
2097 bool celldouble = rightLine(cell) && nextcellleft;
2099 ismulticol = isMultiColumn(cell)
2100 || (c == 0 && colleft != leftLine(cell))
2101 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2102 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2103 || (coldouble != celldouble);
2105 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2106 if (c ==0 && leftLine(cell))
2108 if (!cellInfo(cell).align_special.empty()) {
2109 os << cellInfo(cell).align_special;
2111 if (!getPWidth(cell).zero()) {
2113 case LYX_ALIGN_LEFT:
2114 os << ">{\\raggedright}";
2116 case LYX_ALIGN_RIGHT:
2117 os << ">{\\raggedleft}";
2119 case LYX_ALIGN_CENTER:
2120 os << ">{\\centering}";
2126 case LYX_VALIGN_TOP:
2129 case LYX_VALIGN_MIDDLE:
2132 case LYX_VALIGN_BOTTOM:
2137 << from_ascii(getPWidth(cell).asLatexString())
2141 case LYX_ALIGN_LEFT:
2144 case LYX_ALIGN_RIGHT:
2151 } // end if else !getPWidth
2152 } // end if else !cellinfo_of_cell
2153 if (rightLine(cell) || nextcellleft)
2156 // add extra vertical line if we want a double one
2159 } // end if ismulticol
2161 // we only need code for the first multirow cell
2162 ismultirow = isMultiRow(cell);
2164 os << "\\multirow{" << rowSpan(cell) << "}{";
2165 if (!getPWidth(cell).zero())
2166 os << from_ascii(getPWidth(cell).asLatexString());
2168 // we need to set a default value
2169 // needs to be discussed
2172 } // end if ismultirow
2174 if (getRotateCell(cell)) {
2175 os << "\\begin{sideways}\n";
2178 if (getUsebox(cell) == BOX_PARBOX) {
2181 case LYX_VALIGN_TOP:
2184 case LYX_VALIGN_MIDDLE:
2187 case LYX_VALIGN_BOTTOM:
2191 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2193 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2194 os << "\\begin{minipage}[";
2196 case LYX_VALIGN_TOP:
2199 case LYX_VALIGN_MIDDLE:
2202 case LYX_VALIGN_BOTTOM:
2206 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2214 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2215 bool ismulticol, bool ismultirow) const
2218 row_type const r = cellRow(cell);
2219 if (is_long_tabular && row_info[r].caption)
2223 if (getUsebox(cell) == BOX_PARBOX)
2225 else if (getUsebox(cell) == BOX_MINIPAGE) {
2226 os << "%\n\\end{minipage}";
2229 if (getRotateCell(cell)) {
2230 os << "%\n\\end{sideways}";
2233 if (ismulticol || ismultirow) {
2240 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2241 OutputParams const & runparams) const
2243 if (!is_long_tabular)
2248 // the caption must be output before the headers
2249 if (haveLTCaption()) {
2250 for (row_type i = 0; i < row_info.size(); ++i) {
2251 if (row_info[i].caption) {
2252 ret += TeXRow(os, i, runparams);
2256 // output first header info
2257 // first header must be output before the header, otherwise the
2258 // correct caption placement becomes really weird
2259 if (haveLTFirstHead()) {
2260 if (endfirsthead.topDL) {
2264 for (row_type i = 0; i < row_info.size(); ++i) {
2265 if (row_info[i].endfirsthead) {
2266 ret += TeXRow(os, i, runparams);
2269 if (endfirsthead.bottomDL) {
2273 os << "\\endfirsthead\n";
2276 // output header info
2278 if (endfirsthead.empty && !haveLTFirstHead()) {
2279 os << "\\endfirsthead\n";
2282 if (endhead.topDL) {
2286 for (row_type i = 0; i < row_info.size(); ++i) {
2287 if (row_info[i].endhead) {
2288 ret += TeXRow(os, i, runparams);
2291 if (endhead.bottomDL) {
2295 os << "\\endhead\n";
2298 // output footer info
2300 if (endfoot.topDL) {
2304 for (row_type i = 0; i < row_info.size(); ++i) {
2305 if (row_info[i].endfoot) {
2306 ret += TeXRow(os, i, runparams);
2309 if (endfoot.bottomDL) {
2313 os << "\\endfoot\n";
2315 if (endlastfoot.empty && !haveLTLastFoot()) {
2316 os << "\\endlastfoot\n";
2320 // output lastfooter info
2321 if (haveLTLastFoot()) {
2322 if (endlastfoot.topDL) {
2326 for (row_type i = 0; i < row_info.size(); ++i) {
2327 if (row_info[i].endlastfoot) {
2328 ret += TeXRow(os, i, runparams);
2331 if (endlastfoot.bottomDL) {
2335 os << "\\endlastfoot\n";
2342 bool Tabular::isValidRow(row_type row) const
2344 if (!is_long_tabular)
2346 return !row_info[row].endhead && !row_info[row].endfirsthead
2347 && !row_info[row].endfoot && !row_info[row].endlastfoot
2348 && !row_info[row].caption;
2352 int Tabular::TeXRow(odocstream & os, row_type i,
2353 OutputParams const & runparams) const
2355 idx_type cell = cellIndex(i, 0);
2356 shared_ptr<InsetTableCell> inset = cellInset(cell);
2357 Paragraph const & par = inset->paragraphs().front();
2358 string const lang = par.getParLanguage(buffer().params())->lang();
2360 //output the top line
2361 int ret = TeXTopHLine(os, i, lang);
2363 if (row_info[i].top_space_default) {
2365 os << "\\addlinespace\n";
2367 os << "\\noalign{\\vskip\\doublerulesep}\n";
2369 } else if(!row_info[i].top_space.zero()) {
2371 os << "\\addlinespace["
2372 << from_ascii(row_info[i].top_space.asLatexString())
2375 os << "\\noalign{\\vskip"
2376 << from_ascii(row_info[i].top_space.asLatexString())
2381 bool ismulticol = false;
2382 bool ismultirow = false;
2383 for (col_type j = 0; j < column_info.size(); ++j) {
2384 if (isPartOfMultiRow(i, j))
2385 os << " & "; // we need to add a further column
2386 if (isPartOfMultiColumn(i, j) || isPartOfMultiRow(i, j))
2388 cell = cellIndex(i, j);
2389 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2390 shared_ptr<InsetTableCell> inset = cellInset(cell);
2392 Paragraph const & par = inset->paragraphs().front();
2393 bool rtl = par.isRTL(buffer().params())
2395 && getPWidth(cell).zero();
2399 par.getParLanguage(buffer().params())->lang();
2400 if (lang == "farsi")
2402 else if (lang == "arabic_arabi")
2404 // currently, remaning RTL languages are
2405 // arabic_arabtex and hebrew
2409 // pass to the OutputParams that we are in a cell and
2410 // which alignment we have set.
2411 // InsetNewline needs this context information.
2412 OutputParams newrp(runparams);
2413 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2414 ? OutputParams::PLAIN
2415 : OutputParams::ALIGNED;
2416 ret += inset->latex(os, newrp);
2417 runparams.encoding = newrp.encoding;
2421 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2422 if (cell != getLastCellInRow(i)) { // not last cell in row
2426 if (row_info[i].caption && !endfirsthead.empty && !haveLTFirstHead())
2427 // if no first header and no empty first header is used,
2428 // the caption needs to be terminated by \endfirsthead
2430 os << "\\endfirsthead";
2432 os << "\\tabularnewline";
2433 if (row_info[i].bottom_space_default) {
2435 os << "\\addlinespace";
2437 os << "[\\doublerulesep]";
2438 } else if (!row_info[i].bottom_space.zero()) {
2440 os << "\\addlinespace";
2442 << from_ascii(row_info[i].bottom_space.asLatexString())
2448 //output the bottom line
2449 ret += TeXBottomHLine(os, i, lang);
2451 if (row_info[i].interline_space_default) {
2453 os << "\\addlinespace\n";
2455 os << "\\noalign{\\vskip\\doublerulesep}\n";
2457 } else if (!row_info[i].interline_space.zero()) {
2459 os << "\\addlinespace["
2460 << from_ascii(row_info[i].interline_space.asLatexString())
2463 os << "\\noalign{\\vskip"
2464 << from_ascii(row_info[i].interline_space.asLatexString())
2472 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2476 //+---------------------------------------------------------------------
2477 //+ first the opening preamble +
2478 //+---------------------------------------------------------------------
2481 os << "\\begin{sideways}\n";
2484 if (is_long_tabular) {
2485 os << "\\begin{longtable}";
2486 switch (longtabular_alignment) {
2487 case LYX_LONGTABULAR_ALIGN_LEFT:
2490 case LYX_LONGTABULAR_ALIGN_CENTER:
2492 case LYX_LONGTABULAR_ALIGN_RIGHT:
2497 os << "\\begin{tabular}";
2498 switch (tabular_valignment) {
2499 case LYX_VALIGN_TOP:
2502 case LYX_VALIGN_MIDDLE:
2504 case LYX_VALIGN_BOTTOM:
2512 for (col_type i = 0; i < column_info.size(); ++i) {
2513 if (columnLeftLine(i))
2515 if (!column_info[i].align_special.empty()) {
2516 os << column_info[i].align_special;
2518 if (!column_info[i].p_width.zero()) {
2519 switch (column_info[i].alignment) {
2520 case LYX_ALIGN_LEFT:
2521 os << ">{\\raggedright}";
2523 case LYX_ALIGN_RIGHT:
2524 os << ">{\\raggedleft}";
2526 case LYX_ALIGN_CENTER:
2527 os << ">{\\centering}";
2529 case LYX_ALIGN_NONE:
2530 case LYX_ALIGN_BLOCK:
2531 case LYX_ALIGN_LAYOUT:
2532 case LYX_ALIGN_SPECIAL:
2536 switch (column_info[i].valignment) {
2537 case LYX_VALIGN_TOP:
2540 case LYX_VALIGN_MIDDLE:
2543 case LYX_VALIGN_BOTTOM:
2548 << from_ascii(column_info[i].p_width.asLatexString())
2551 switch (column_info[i].alignment) {
2552 case LYX_ALIGN_LEFT:
2555 case LYX_ALIGN_RIGHT:
2562 } // end if else !column_info[i].p_width
2563 } // end if else !column_info[i].align_special
2564 if (columnRightLine(i))
2570 ret += TeXLongtableHeaderFooter(os, runparams);
2572 //+---------------------------------------------------------------------
2573 //+ the single row and columns (cells) +
2574 //+---------------------------------------------------------------------
2576 for (row_type i = 0; i < row_info.size(); ++i) {
2577 if (isValidRow(i)) {
2578 ret += TeXRow(os, i, runparams);
2579 if (is_long_tabular && row_info[i].newpage) {
2580 os << "\\newpage\n";
2586 //+---------------------------------------------------------------------
2587 //+ the closing of the tabular +
2588 //+---------------------------------------------------------------------
2590 if (is_long_tabular)
2591 os << "\\end{longtable}";
2593 os << "\\end{tabular}";
2595 os << "\n\\end{sideways}";
2603 int Tabular::docbookRow(odocstream & os, row_type row,
2604 OutputParams const & runparams) const
2607 idx_type cell = getFirstCellInRow(row);
2610 for (col_type j = 0; j < column_info.size(); ++j) {
2611 if (isPartOfMultiColumn(row, j))
2614 os << "<entry align=\"";
2615 switch (getAlignment(cell)) {
2616 case LYX_ALIGN_LEFT:
2619 case LYX_ALIGN_RIGHT:
2627 os << "\" valign=\"";
2628 switch (getVAlignment(cell)) {
2629 case LYX_VALIGN_TOP:
2632 case LYX_VALIGN_BOTTOM:
2635 case LYX_VALIGN_MIDDLE:
2640 if (isMultiColumn(cell)) {
2641 os << " namest=\"col" << j << "\" ";
2642 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2646 ret += cellInset(cell)->docbook(os, runparams);
2655 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2659 //+---------------------------------------------------------------------
2660 //+ first the opening preamble +
2661 //+---------------------------------------------------------------------
2663 os << "<tgroup cols=\"" << column_info.size()
2664 << "\" colsep=\"1\" rowsep=\"1\">\n";
2666 for (col_type i = 0; i < column_info.size(); ++i) {
2667 os << "<colspec colname=\"col" << i << "\" align=\"";
2668 switch (column_info[i].alignment) {
2669 case LYX_ALIGN_LEFT:
2672 case LYX_ALIGN_RIGHT:
2680 if (runparams.flavor == OutputParams::XML)
2686 //+---------------------------------------------------------------------
2687 //+ Long Tabular case +
2688 //+---------------------------------------------------------------------
2690 // output caption info
2691 if (haveLTCaption()) {
2692 os << "<caption>\n";
2694 for (row_type i = 0; i < row_info.size(); ++i) {
2695 if (row_info[i].caption) {
2696 ret += docbookRow(os, i, runparams);
2699 os << "</caption>\n";
2702 // output header info
2703 if (haveLTHead() || haveLTFirstHead()) {
2706 for (row_type i = 0; i < row_info.size(); ++i) {
2707 if (row_info[i].endhead || row_info[i].endfirsthead) {
2708 ret += docbookRow(os, i, runparams);
2714 // output footer info
2715 if (haveLTFoot() || haveLTLastFoot()) {
2718 for (row_type i = 0; i < row_info.size(); ++i) {
2719 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2720 ret += docbookRow(os, i, runparams);
2727 //+---------------------------------------------------------------------
2728 //+ the single row and columns (cells) +
2729 //+---------------------------------------------------------------------
2733 for (row_type i = 0; i < row_info.size(); ++i) {
2734 if (isValidRow(i)) {
2735 ret += docbookRow(os, i, runparams);
2740 //+---------------------------------------------------------------------
2741 //+ the closing of the tabular +
2742 //+---------------------------------------------------------------------
2751 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2752 OutputParams const & runparams) const
2755 idx_type cell = getFirstCellInRow(row);
2757 xs << html::StartTag("tr");
2758 for (col_type j = 0; j < column_info.size(); ++j) {
2759 if (isPartOfMultiColumn(row, j))
2764 switch (getAlignment(cell)) {
2765 case LYX_ALIGN_LEFT:
2768 case LYX_ALIGN_RIGHT:
2776 attr << " valign='";
2777 switch (getVAlignment(cell)) {
2778 case LYX_VALIGN_TOP:
2781 case LYX_VALIGN_BOTTOM:
2784 case LYX_VALIGN_MIDDLE:
2789 if (isMultiColumn(cell))
2790 attr << " colspan='" << columnSpan(cell) << "'";
2792 xs << html::StartTag("td", attr.str());
2793 ret += cellInset(cell)->xhtml(xs, runparams);
2794 xs << html::EndTag("td");
2797 xs << html::EndTag("tr");
2802 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2805 // It's unclear to me if we need to mess with the long table stuff.
2806 // We can borrow that too from docbook, if so.
2808 xs << html::StartTag("tbody");
2809 for (row_type i = 0; i < row_info.size(); ++i) {
2810 if (isValidRow(i)) {
2811 ret += xhtmlRow(xs, i, runparams);
2814 xs << html::EndTag("tbody");
2819 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2820 vector<unsigned int> const & clen) const
2822 idx_type const fcell = getFirstCellInRow(row);
2823 idx_type const n = numberOfCellsInRow(row) + fcell;
2826 for (idx_type i = fcell; i < n; ++i) {
2836 for (idx_type i = fcell; i < n; ++i) {
2847 col_type column = cellColumn(i);
2848 int len = clen[column];
2849 while (column < column_info.size() - 1
2850 && isPartOfMultiColumn(row, ++column))
2851 len += clen[column] + 4;
2852 os << docstring(len, ch);
2867 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2868 vector<unsigned int> const & clen) const
2870 idx_type const fcell = getFirstCellInRow(row);
2871 idx_type const n = numberOfCellsInRow(row) + fcell;
2874 for (idx_type i = fcell; i < n; ++i) {
2875 if (bottomLine(i)) {
2884 for (idx_type i = fcell; i < n; ++i) {
2885 if (bottomLine(i)) {
2895 col_type column = cellColumn(i);
2896 int len = clen[column];
2897 while (column < column_info.size() -1
2898 && isPartOfMultiColumn(row, ++column))
2899 len += clen[column] + 4;
2900 os << docstring(len, ch);
2901 if (bottomLine(i)) {
2915 void Tabular::plaintextPrintCell(odocstream & os,
2916 OutputParams const & runparams,
2917 idx_type cell, row_type row, col_type column,
2918 vector<unsigned int> const & clen,
2919 bool onlydata) const
2921 odocstringstream sstr;
2922 cellInset(cell)->plaintext(sstr, runparams);
2934 unsigned int len1 = sstr.str().length();
2935 unsigned int len2 = clen[column];
2936 while (column < column_info.size() -1
2937 && isPartOfMultiColumn(row, ++column))
2938 len2 += clen[column] + 4;
2941 switch (getAlignment(cell)) {
2943 case LYX_ALIGN_LEFT:
2946 case LYX_ALIGN_RIGHT:
2950 case LYX_ALIGN_CENTER:
2956 os << docstring(len1, ' ') << sstr.str()
2957 << docstring(len2, ' ');
2959 if (rightLine(cell))
2966 void Tabular::plaintext(odocstream & os,
2967 OutputParams const & runparams, int const depth,
2968 bool onlydata, char_type delim) const
2970 // first calculate the width of the single columns
2971 vector<unsigned int> clen(column_info.size());
2974 // first all non (real) multicolumn cells!
2975 for (col_type j = 0; j < column_info.size(); ++j) {
2977 for (row_type i = 0; i < row_info.size(); ++i) {
2978 idx_type cell = cellIndex(i, j);
2979 if (isMultiColumnReal(cell))
2981 odocstringstream sstr;
2982 cellInset(cell)->plaintext(sstr, runparams);
2983 if (clen[j] < sstr.str().length())
2984 clen[j] = sstr.str().length();
2987 // then all (real) multicolumn cells!
2988 for (col_type j = 0; j < column_info.size(); ++j) {
2989 for (row_type i = 0; i < row_info.size(); ++i) {
2990 idx_type cell = cellIndex(i, j);
2991 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2993 odocstringstream sstr;
2994 cellInset(cell)->plaintext(sstr, runparams);
2995 int len = int(sstr.str().length());
2996 idx_type const n = columnSpan(cell);
2997 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2999 if (len > int(clen[j + n - 1]))
3000 clen[j + n - 1] = len;
3005 for (row_type i = 0; i < row_info.size(); ++i) {
3006 if (!onlydata && plaintextTopHLine(os, i, clen))
3007 os << docstring(depth * 2, ' ');
3008 for (col_type j = 0; j < column_info.size(); ++j) {
3009 if (isPartOfMultiColumn(i, j))
3011 if (onlydata && j > 0)
3012 // we don't use operator<< for single UCS4 character.
3013 // see explanation in docstream.h
3015 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
3020 os << docstring(depth * 2, ' ');
3021 if (plaintextBottomHLine(os, i, clen))
3022 os << docstring(depth * 2, ' ');
3028 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3030 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3034 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3035 col_type column) const
3037 return cell_info[row][column].inset;
3041 void Tabular::setCellInset(row_type row, col_type column,
3042 shared_ptr<InsetTableCell> ins) const
3044 CellData & cd = cell_info[row][column];
3049 void Tabular::validate(LaTeXFeatures & features) const
3051 features.require("NeedTabularnewline");
3053 features.require("booktabs");
3054 if (is_long_tabular)
3055 features.require("longtable");
3057 features.require("rotating");
3058 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3059 if (isMultiRow(cell))
3060 features.require("multirow");
3061 if (getVAlignment(cell) != LYX_VALIGN_TOP
3062 || !getPWidth(cell).zero())
3063 features.require("array");
3064 cellInset(cell)->validate(features);
3069 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3071 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3072 ParagraphList::const_iterator cit = parlist.begin();
3073 ParagraphList::const_iterator end = parlist.end();
3075 for (; cit != end; ++cit)
3076 for (int i = 0; i < cit->size(); ++i)
3077 if (cit->isNewline(i))
3084 /////////////////////////////////////////////////////////////////////
3088 /////////////////////////////////////////////////////////////////////
3090 InsetTableCell::InsetTableCell(Buffer * buf)
3091 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3092 contentAlign(LYX_ALIGN_CENTER)
3096 bool InsetTableCell::forcePlainLayout(idx_type) const
3098 return !isFixedWidth;
3102 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3104 return isFixedWidth;
3108 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3109 FuncStatus & status) const
3112 switch (cmd.action) {
3114 enabled = !forcePlainLayout();
3116 case LFUN_LAYOUT_PARAGRAPH:
3117 enabled = allowParagraphCustomization();
3120 return InsetText::getStatus(cur, cmd, status);
3122 status.setEnabled(enabled);
3126 docstring InsetTableCell::asString(bool intoInsets)
3129 if (paragraphs().empty())
3131 ParagraphList::const_iterator it = paragraphs().begin();
3132 ParagraphList::const_iterator en = paragraphs().end();
3134 for (; it != en; ++it) {
3139 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3145 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3148 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3149 return InsetText::xhtml(xs, rp);
3154 /////////////////////////////////////////////////////////////////////
3158 /////////////////////////////////////////////////////////////////////
3160 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3162 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3163 rowselect_(false), colselect_(false)
3168 InsetTabular::InsetTabular(InsetTabular const & tab)
3169 : Inset(tab), tabular(tab.tabular), scx_(0)
3174 InsetTabular::~InsetTabular()
3176 hideDialogs("tabular", this);
3180 void InsetTabular::setBuffer(Buffer & buf)
3182 tabular.setBuffer(buf);
3183 Inset::setBuffer(buf);
3187 bool InsetTabular::insetAllowed(InsetCode code) const
3189 if (code == MATHMACRO_CODE
3190 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3197 void InsetTabular::write(ostream & os) const
3199 os << "Tabular" << endl;
3204 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3206 // FIXME: depending on the selection state, we could offer a different menu.
3207 return from_ascii("context-tabular");
3211 void InsetTabular::read(Lexer & lex)
3213 //bool const old_format = (lex.getString() == "\\LyXTable");
3221 string token = lex.getString();
3222 while (lex && token != "\\end_inset") {
3224 token = lex.getString();
3227 lex.printError("Missing \\end_inset at this point. ");
3231 int InsetTabular::rowFromY(Cursor & cur, int y) const
3233 // top y coordinate of tabular
3234 int h = yo(cur.bv()) - tabular.rowAscent(0);
3235 size_t nrows = tabular.row_info.size();
3237 for (; r < nrows && y > h; ++r)
3238 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3239 + tabular.interRowSpace(r);
3245 int InsetTabular::columnFromX(Cursor & cur, int x) const
3247 // left x coordinate of tabular
3248 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3249 size_t ncols = tabular.column_info.size();
3251 for (; c < ncols && x > w; ++c)
3252 w += tabular.columnWidth(c);
3257 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3259 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3260 // mi.base.textwidth << "\n";
3263 LASSERT(false, /**/);
3266 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3269 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3270 if (tabular.isPartOfMultiColumn(i, j)
3271 || tabular.isPartOfMultiRow(i, j))
3272 // multicolumn or multirow cell, but not first one
3274 idx_type const cell = tabular.cellIndex(i, j);
3278 if (tabular.cell_info[i][j].multicolumn ==
3279 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3280 p_width = tabular.cellInfo(cell).p_width;
3282 p_width = tabular.column_info[j].p_width;
3283 if (!p_width.zero())
3284 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3285 tabular.cellInset(cell)->metrics(m, dim);
3286 if (!p_width.zero())
3287 dim.wid = m.base.textwidth;
3288 tabular.setCellWidth(cell, dim.wid);
3289 maxAsc = max(maxAsc, dim.asc);
3290 maxDesc = max(maxDesc, dim.des);
3292 int const top_space = tabular.row_info[i].top_space_default ?
3293 default_line_space :
3294 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
3295 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
3296 int const bottom_space = tabular.row_info[i].bottom_space_default ?
3297 default_line_space :
3298 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3299 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3301 tabular.updateColumnWidths();
3302 dim.asc = tabular.rowAscent(0);
3303 dim.des = tabular.height() - dim.asc;
3304 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3307 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3310 if (&cur.inset() == this && cur.selection()) {
3311 if (cur.selIsMultiCell()) {
3314 getSelection(cur, rs, re, cs, ce);
3316 if (col >= cs && col <= ce && row >= rs && row <= re)
3319 if (col == tabular.cellColumn(cur.idx())
3320 && row == tabular.cellRow(cur.idx())) {
3321 CursorSlice const & beg = cur.selBegin();
3322 CursorSlice const & end = cur.selEnd();
3324 if ((end.lastpos() > 0 || end.lastpit() > 0)
3325 && end.pos() == end.lastpos() && beg.pos() == 0
3326 && end.pit() == end.lastpit() && beg.pit() == 0)
3334 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3336 x += scx_ + ADD_TO_TABULAR_WIDTH;
3338 BufferView * bv = pi.base.bv;
3339 Cursor & cur = pi.base.bv->cursor();
3342 // FIXME: As the full background is painted in drawSelection(),
3343 // we have no choice but to do a full repaint for the Text cells.
3344 pi.full_repaint = true;
3346 bool const original_selection_state = pi.selected;
3349 first_visible_cell = Tabular::npos;
3350 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3352 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3353 if (tabular.isPartOfMultiColumn(i, j))
3356 idx = tabular.cellIndex(i, j);
3358 if (tabular.isPartOfMultiRow(i, j)) {
3359 nx += tabular.columnWidth(idx);
3363 if (first_visible_cell == Tabular::npos)
3364 first_visible_cell = idx;
3366 pi.selected |= isCellSelected(cur, i, j);
3367 int const cx = nx + tabular.textHOffset(idx);
3368 int const cy = y + tabular.textVOffset(idx);
3369 // Cache the Inset position.
3370 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3371 cell(idx)->draw(pi, cx, cy);
3372 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3373 nx += tabular.columnWidth(idx);
3374 pi.selected = original_selection_state;
3377 if (i + 1 < tabular.row_info.size())
3378 y += tabular.rowDescent(i) + tabular.rowAscent(i + 1)
3379 + tabular.interRowSpace(i + 1);
3384 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3386 Cursor & cur = pi.base.bv->cursor();
3389 x += scx_ + ADD_TO_TABULAR_WIDTH;
3391 // FIXME: it is wrong to completely paint the background
3392 // if we want to do single row painting.
3394 // Paint background of current tabular
3395 int const w = tabular.width();
3396 int const h = tabular.height();
3397 int yy = y - tabular.rowAscent(0);
3398 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3400 if (!cur.selection())
3402 if (&cur.inset() != this)
3407 bool const full_cell_selected = isCellSelected(cur,
3408 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3410 if (cur.selIsMultiCell() || full_cell_selected) {
3411 y -= tabular.rowAscent(0);
3412 for (row_type r = 0; r < tabular.row_info.size(); ++r) {
3414 for (col_type c = 0; c < tabular.column_info.size(); ++c) {
3415 if (tabular.isPartOfMultiColumn(r, c))
3418 idx_type const cell = tabular.cellIndex(r, c);
3420 if (tabular.isPartOfMultiRow(r, c)) {
3421 xx += tabular.columnWidth(cell);
3424 int const w = tabular.columnWidth(cell);
3425 int const h = tabular.rowHeight(cell);
3426 if (isCellSelected(cur, r, c))
3427 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3430 if (r + 1 < tabular.row_info.size())
3431 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3432 + tabular.interRowSpace(r + 1);
3436 x += cellXPos(cur.idx());
3437 x += tabular.textHOffset(cur.idx());
3438 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3443 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3444 row_type row, idx_type cell, Change const & change) const
3446 y = y - tabular.rowAscent(row);
3447 int const w = tabular.columnWidth(cell);
3448 int const h = tabular.rowHeight(cell);
3449 bool on_off = false;
3450 Color col = Color_tabularline;
3451 Color onoffcol = Color_tabularonoffline;
3453 if (change.changed()) {
3454 col = change.color();
3455 onoffcol = change.color();
3458 if (!tabular.topAlreadyDrawn(cell)) {
3459 on_off = !tabular.topLine(cell);
3460 pain.line(x, y, x + w, y,
3461 on_off ? onoffcol : col,
3462 on_off ? Painter::line_onoffdash : Painter::line_solid);
3464 on_off = !tabular.bottomLine(cell);
3465 pain.line(x, y + h, x + w, y + h,
3466 on_off ? onoffcol : col,
3467 on_off ? Painter::line_onoffdash : Painter::line_solid);
3468 if (!tabular.leftAlreadyDrawn(cell)) {
3469 on_off = !tabular.leftLine(cell);
3470 pain.line(x, y, x, y + h,
3471 on_off ? onoffcol : col,
3472 on_off ? Painter::line_onoffdash : Painter::line_solid);
3474 on_off = !tabular.rightLine(cell);
3475 pain.line(x + w - tabular.interColumnSpace(cell), y,
3476 x + w - tabular.interColumnSpace(cell), y + h,
3477 on_off ? onoffcol : col,
3478 on_off ? Painter::line_onoffdash : Painter::line_solid);
3482 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3484 //lyxerr << "InsetTabular::edit: " << this << endl;
3486 cur.setSelection(false);
3489 if (isRightToLeft(cur))
3490 cur.idx() = tabular.getLastCellInRow(0);
3496 if (isRightToLeft(cur))
3497 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3499 cur.idx() = tabular.numberofcells - 1;
3501 cur.pos() = cur.lastpos(); // FIXME crude guess
3503 cur.setCurrentFont();
3504 // FIXME: this accesses the position cache before it is initialized
3506 //cur.bv().fitCursor();
3510 void InsetTabular::updateLabels(ParIterator const & it, UpdateType utype)
3512 // In a longtable, tell captions what the current float is
3513 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3514 string const saveflt = cnts.current_float();
3515 if (tabular.is_long_tabular)
3516 cnts.current_float("table");
3518 ParIterator it2 = it;
3520 size_t const end = it2.nargs();
3521 for ( ; it2.idx() < end; it2.top().forwardIdx())
3522 buffer().updateLabels(it2, utype);
3525 if (tabular.is_long_tabular)
3526 cnts.current_float(saveflt);
3530 void InsetTabular::addToToc(DocIterator const & cpit)
3532 DocIterator dit = cpit;
3534 size_t const end = dit.nargs();
3535 for ( ; dit.idx() < end; dit.top().forwardIdx())
3536 cell(dit.idx())->addToToc(dit);
3540 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3542 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3543 << "\n cur:" << cur);
3544 CursorSlice sl = cur.top();
3545 Cursor & bvcur = cur.bv().cursor();
3547 switch (cmd.action) {
3549 case LFUN_MOUSE_PRESS: {
3550 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3552 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3553 || cmd.x > xo(cur.bv()) + tabular.width()) {
3554 row_type r = rowFromY(cur, cmd.y);
3555 cur.idx() = tabular.getFirstCellInRow(r);
3558 cur.idx() = tabular.getLastCellInRow(r);
3559 cur.pos() = cur.lastpos();
3560 cur.setSelection(true);
3566 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3567 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3568 || cmd.y > y0 + tabular.height()) {
3569 col_type c = columnFromX(cur, cmd.x);
3570 cur.idx() = tabular.cellIndex(0, c);
3573 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3574 cur.pos() = cur.lastpos();
3575 cur.setSelection(true);
3580 // do not reset cursor/selection if we have selected
3581 // some cells (bug 2715).
3582 if (cmd.button() == mouse_button::button3
3583 && &bvcur.selBegin().inset() == this
3584 && bvcur.selIsMultiCell())
3587 // Let InsetTableCell do it
3588 cell(cur.idx())->dispatch(cur, cmd);
3591 case LFUN_MOUSE_MOTION:
3592 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3593 if (cmd.button() == mouse_button::button1) {
3594 // only accept motions to places not deeper nested than the real anchor
3595 if (!bvcur.anchor_.hasPart(cur)) {
3599 // select (additional) row
3601 row_type r = rowFromY(cur, cmd.y);
3602 cur.idx() = tabular.getLastCellInRow(r);
3603 // we need to reset the cursor's pit and pos now, as the old ones
3604 // may no longer be valid.
3607 bvcur.setCursor(cur);
3608 bvcur.setSelection(true);
3611 // select (additional) column
3613 col_type c = columnFromX(cur, cmd.x);
3614 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3615 // we need to reset the cursor's pit and pos now, as the old ones
3616 // may no longer be valid.
3619 bvcur.setCursor(cur);
3620 bvcur.setSelection(true);
3623 // only update if selection changes
3624 if (bvcur.idx() == cur.idx() &&
3625 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3627 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3628 bvcur.setCursor(cur);
3629 bvcur.setSelection(true);
3630 // if this is a multicell selection, we just set the cursor to
3631 // the beginning of the cell's text.
3632 if (bvcur.selIsMultiCell()) {
3633 bvcur.pit() = bvcur.lastpit();
3634 bvcur.pos() = bvcur.lastpos();
3639 case LFUN_MOUSE_RELEASE:
3644 case LFUN_CELL_BACKWARD:
3646 cur.setSelection(false);
3649 case LFUN_CELL_FORWARD:
3651 cur.setSelection(false);
3654 case LFUN_CHAR_FORWARD_SELECT:
3655 case LFUN_CHAR_FORWARD:
3656 case LFUN_CHAR_BACKWARD_SELECT:
3657 case LFUN_CHAR_BACKWARD:
3658 case LFUN_CHAR_RIGHT_SELECT:
3659 case LFUN_CHAR_RIGHT:
3660 case LFUN_CHAR_LEFT_SELECT:
3661 case LFUN_CHAR_LEFT: {
3662 // determine whether we move to next or previous cell, where to enter
3663 // the new cell from, and which command to "finish" (i.e., exit the
3666 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3667 FuncCode finish_lfun;
3669 if (cmd.action == LFUN_CHAR_FORWARD
3670 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3672 finish_lfun = LFUN_FINISHED_FORWARD;
3674 else if (cmd.action == LFUN_CHAR_BACKWARD
3675 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3677 finish_lfun = LFUN_FINISHED_BACKWARD;
3679 // LEFT or RIGHT commands --- the interpretation will depend on the
3680 // table's direction.
3682 bool const right = cmd.action == LFUN_CHAR_RIGHT
3683 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3684 next_cell = isRightToLeft(cur) != right;
3686 if (lyxrc.visual_cursor)
3687 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3689 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3692 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3693 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3694 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3695 cmd.action == LFUN_CHAR_LEFT_SELECT;
3697 // If we have a multicell selection or we're
3698 // not doing some LFUN_*_SELECT thing anyway...
3699 if (!cur.selIsMultiCell() || !select) {
3700 col_type const c = tabular.cellColumn(cur.idx());
3701 row_type const r = tabular.cellRow(cur.idx());
3702 // Are we trying to select the whole cell and is the whole cell
3703 // not yet selected?
3704 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3705 ((next_cell && cur.pit() == cur.lastpit()
3706 && cur.pos() == cur.lastpos())
3707 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3709 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3711 // ...try to dispatch to the cell's inset.
3712 cell(cur.idx())->dispatch(cur, cmd);
3714 // When we already have a selection we want to select the whole cell
3715 // before going to the next cell.
3716 if (select_whole && !empty_cell){
3717 getText(cur.idx())->selectAll(cur);
3722 // FIXME: When we support the selection of an empty cell, remove
3723 // the !empty_cell from this condition. For now we jump to the next
3724 // cell if the current cell is empty.
3725 if (cur.result().dispatched() && !empty_cell)
3729 // move to next/prev cell, as appropriate
3730 // note that we will always do this if we're selecting and we have
3731 // a multicell selection
3732 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3733 << " cell from: " << int(entry_from));
3735 moveNextCell(cur, entry_from);
3737 movePrevCell(cur, entry_from);
3738 // if we're exiting the table, call the appropriate FINISHED lfun
3739 if (sl == cur.top())
3740 cmd = FuncRequest(finish_lfun);
3747 case LFUN_DOWN_SELECT:
3749 if (!(cur.selection() && cur.selIsMultiCell()))
3750 cell(cur.idx())->dispatch(cur, cmd);
3752 cur.dispatched(); // override the cell's decision
3753 if (sl == cur.top()) {
3754 // if our Text didn't do anything to the cursor
3755 // then we try to put the cursor into the cell below
3756 // setting also the right targetX.
3757 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3758 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3759 cur.idx() = tabular.cellBelow(cur.idx());
3761 TextMetrics const & tm =
3762 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3763 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3764 cur.setCurrentFont();
3767 if (sl == cur.top()) {
3768 // we trick it to go to forward after leaving the
3770 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3773 if (cur.selIsMultiCell()) {
3774 cur.pit() = cur.lastpit();
3775 cur.pos() = cur.lastpos();
3776 cur.setCurrentFont();
3781 case LFUN_UP_SELECT:
3783 if (!(cur.selection() && cur.selIsMultiCell()))
3784 cell(cur.idx())->dispatch(cur, cmd);
3785 cur.dispatched(); // override the cell's decision
3786 if (sl == cur.top()) {
3787 // if our Text didn't do anything to the cursor
3788 // then we try to put the cursor into the cell above
3789 // setting also the right targetX.
3790 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3791 if (tabular.cellRow(cur.idx()) != 0) {
3792 cur.idx() = tabular.cellAbove(cur.idx());
3793 cur.pit() = cur.lastpit();
3794 Text const * text = cell(cur.idx())->getText(0);
3795 TextMetrics const & tm = cur.bv().textMetrics(text);
3796 ParagraphMetrics const & pm =
3797 tm.parMetrics(cur.lastpit());
3798 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3799 cur.setCurrentFont();
3802 if (sl == cur.top()) {
3803 cmd = FuncRequest(LFUN_UP);
3806 if (cur.selIsMultiCell()) {
3808 cur.pos() = cur.lastpos();
3809 cur.setCurrentFont();
3814 // case LFUN_SCREEN_DOWN: {
3815 // //if (hasSelection())
3816 // // cur.selection() = false;
3817 // col_type const col = tabular.cellColumn(cur.idx());
3818 // int const t = cur.bv().top_y() + cur.bv().height();
3819 // if (t < yo() + tabular.getHeightOfTabular()) {
3820 // cur.bv().scrollDocView(t);
3821 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3823 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3830 // case LFUN_SCREEN_UP: {
3831 // //if (hasSelection())
3832 // // cur.selection() = false;
3833 // col_type const col = tabular.cellColumn(cur.idx());
3834 // int const t = cur.bv().top_y() + cur.bv().height();
3836 // cur.bv().scrollDocView(t);
3840 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3844 // cur.par() = cur.lastpar();
3845 // cur.pos() = cur.lastpos();
3849 case LFUN_LAYOUT_TABULAR:
3850 cur.bv().showDialog("tabular");
3853 case LFUN_INSET_MODIFY:
3854 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3858 // insert file functions
3859 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3860 case LFUN_FILE_INSERT_PLAINTEXT:
3862 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3863 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3864 FileName(to_utf8(cmd.argument())));
3867 cur.recordUndoInset(INSERT_UNDO);
3868 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3869 // content has been replaced,
3870 // so cursor might be invalid
3871 cur.pos() = cur.lastpos();
3872 cur.pit() = cur.lastpit();
3873 bvcur.setCursor(cur);
3880 if (cur.selIsMultiCell()) {
3881 if (copySelection(cur)) {
3882 cur.recordUndoInset(DELETE_UNDO);
3886 cell(cur.idx())->dispatch(cur, cmd);
3889 case LFUN_SELF_INSERT:
3890 if (cur.selIsMultiCell()) {
3891 cur.recordUndoInset(DELETE_UNDO);
3894 cell(cur.idx())->dispatch(cur, cmd);
3897 case LFUN_CHAR_DELETE_BACKWARD:
3898 case LFUN_CHAR_DELETE_FORWARD:
3899 if (cur.selIsMultiCell()) {
3900 cur.recordUndoInset(DELETE_UNDO);
3903 cell(cur.idx())->dispatch(cur, cmd);
3907 if (!cur.selection())
3909 if (cur.selIsMultiCell()) {
3913 cell(cur.idx())->dispatch(cur, cmd);
3916 case LFUN_CLIPBOARD_PASTE:
3917 case LFUN_PRIMARY_SELECTION_PASTE: {
3918 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3919 theClipboard().getAsText() :
3920 theSelection().get();
3923 // pass to InsertPlaintextString, but
3924 // only if we have multi-cell content
3925 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3926 cur.recordUndoInset(INSERT_UNDO);
3927 if (insertPlaintextString(cur.bv(), clip, false)) {
3928 // content has been replaced,
3929 // so cursor might be invalid
3930 cur.pos() = cur.lastpos();
3931 cur.pit() = cur.lastpit();
3932 bvcur.setCursor(cur);
3936 // Let the cell handle normal text
3937 cell(cur.idx())->dispatch(cur, cmd);
3942 if (!tabularStackDirty()) {
3943 if (!cur.selIsMultiCell())
3944 cell(cur.idx())->dispatch(cur, cmd);
3947 if (theClipboard().isInternal() ||
3948 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3949 cur.recordUndoInset(INSERT_UNDO);
3950 pasteClipboard(cur);
3954 case LFUN_FONT_EMPH:
3955 case LFUN_FONT_BOLD:
3956 case LFUN_FONT_BOLDSYMBOL:
3957 case LFUN_FONT_ROMAN:
3958 case LFUN_FONT_NOUN:
3959 case LFUN_FONT_ITAL:
3960 case LFUN_FONT_FRAK:
3961 case LFUN_FONT_TYPEWRITER:
3962 case LFUN_FONT_SANS:
3963 case LFUN_TEXTSTYLE_APPLY:
3964 case LFUN_TEXTSTYLE_UPDATE:
3965 case LFUN_FONT_SIZE:
3966 case LFUN_FONT_UNDERLINE:
3967 case LFUN_FONT_STRIKEOUT:
3968 case LFUN_FONT_UULINE:
3969 case LFUN_FONT_UWAVE:
3971 case LFUN_WORD_CAPITALIZE:
3972 case LFUN_WORD_UPCASE:
3973 case LFUN_WORD_LOWCASE:
3974 case LFUN_CHARS_TRANSPOSE:
3975 if (cur.selIsMultiCell()) {
3978 getSelection(cur, rs, re, cs, ce);
3979 Cursor tmpcur = cur;
3980 for (row_type i = rs; i <= re; ++i) {
3981 for (col_type j = cs; j <= ce; ++j) {
3982 // cursor follows cell:
3983 tmpcur.idx() = tabular.cellIndex(i, j);
3984 // select this cell only:
3987 tmpcur.resetAnchor();
3988 tmpcur.pit() = tmpcur.lastpit();
3989 tmpcur.pos() = tmpcur.top().lastpos();
3990 tmpcur.setCursor(tmpcur);
3991 tmpcur.setSelection();
3992 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3997 cell(cur.idx())->dispatch(cur, cmd);
4001 case LFUN_INSET_SETTINGS:
4002 // relay this lfun to Inset, not to the cell.
4003 Inset::doDispatch(cur, cmd);
4007 // we try to handle this event in the insets dispatch function.
4008 cell(cur.idx())->dispatch(cur, cmd);
4014 // function sets an object as defined in func_status.h:
4015 // states OK, Unknown, Disabled, On, Off.
4016 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4017 FuncStatus & status) const
4019 switch (cmd.action) {
4020 case LFUN_INSET_MODIFY: {
4021 istringstream is(to_utf8(cmd.argument()));
4024 if (insetCode(s) != TABULAR_CODE) {
4026 status.setEnabled(false);
4030 // FIXME: We only check for the very first argument...
4031 int action = Tabular::LAST_ACTION;
4033 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4034 string const tmp = tabularFeature[i].feature;
4035 if (tmp == s.substr(0, tmp.length())) {
4036 action = tabularFeature[i].action;
4040 if (action == Tabular::LAST_ACTION) {
4042 status.unknown(true);
4046 string const argument
4047 = ltrim(s.substr(tabularFeature[i].feature.length()));
4049 row_type sel_row_start = 0;
4050 row_type sel_row_end = 0;
4051 col_type sel_col_start = 0;
4052 col_type sel_col_end = 0;
4053 Tabular::ltType dummyltt;
4056 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4059 case Tabular::SET_PWIDTH:
4060 case Tabular::SET_MPWIDTH:
4061 case Tabular::SET_SPECIAL_COLUMN:
4062 case Tabular::SET_SPECIAL_MULTICOLUMN:
4063 case Tabular::SET_SPECIAL_MULTIROW:
4064 case Tabular::APPEND_ROW:
4065 case Tabular::APPEND_COLUMN:
4066 case Tabular::DELETE_ROW:
4067 case Tabular::DELETE_COLUMN:
4068 case Tabular::COPY_ROW:
4069 case Tabular::COPY_COLUMN:
4070 case Tabular::SET_TOP_SPACE:
4071 case Tabular::SET_BOTTOM_SPACE:
4072 case Tabular::SET_INTERLINE_SPACE:
4076 case Tabular::MULTICOLUMN:
4077 // If a row is set as longtable caption, it must not be allowed
4078 // to unset that this row is a multicolumn.
4079 status.setEnabled(sel_row_start == sel_row_end
4080 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4081 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4084 case Tabular::MULTIROW:
4085 // If a row is set as longtable caption, it must not be allowed
4086 // to unset that this row is a multirow.
4087 status.setEnabled(sel_col_start == sel_col_end
4088 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4089 status.setOnOff(tabular.isMultiRow(cur.idx()));
4092 case Tabular::SET_ALL_LINES:
4093 case Tabular::UNSET_ALL_LINES:
4094 case Tabular::SET_BORDER_LINES:
4095 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4098 case Tabular::TOGGLE_LINE_TOP:
4099 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4100 status.setOnOff(tabular.topLine(cur.idx()));
4103 case Tabular::TOGGLE_LINE_BOTTOM:
4104 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4105 status.setOnOff(tabular.bottomLine(cur.idx()));
4108 case Tabular::TOGGLE_LINE_LEFT:
4109 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4110 status.setOnOff(tabular.leftLine(cur.idx()));
4113 case Tabular::TOGGLE_LINE_RIGHT:
4114 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4115 status.setOnOff(tabular.rightLine(cur.idx()));
4118 case Tabular::M_ALIGN_LEFT:
4120 case Tabular::ALIGN_LEFT:
4121 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4124 case Tabular::M_ALIGN_RIGHT:
4126 case Tabular::ALIGN_RIGHT:
4127 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4130 case Tabular::M_ALIGN_CENTER:
4132 case Tabular::ALIGN_CENTER:
4133 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4136 case Tabular::ALIGN_BLOCK:
4137 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
4138 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4141 case Tabular::M_VALIGN_TOP:
4143 case Tabular::VALIGN_TOP:
4145 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4148 case Tabular::M_VALIGN_BOTTOM:
4150 case Tabular::VALIGN_BOTTOM:
4152 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4155 case Tabular::M_VALIGN_MIDDLE:
4157 case Tabular::VALIGN_MIDDLE:
4159 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4162 case Tabular::SET_LONGTABULAR:
4163 status.setOnOff(tabular.is_long_tabular);
4166 case Tabular::UNSET_LONGTABULAR:
4167 status.setOnOff(!tabular.is_long_tabular);
4170 case Tabular::TOGGLE_ROTATE_TABULAR:
4171 case Tabular::SET_ROTATE_TABULAR:
4172 status.setOnOff(tabular.rotate);
4175 case Tabular::TABULAR_VALIGN_TOP:
4176 status.setOnOff(tabular.tabular_valignment
4177 == Tabular::LYX_VALIGN_TOP);
4179 case Tabular::TABULAR_VALIGN_MIDDLE:
4180 status.setOnOff(tabular.tabular_valignment
4181 == Tabular::LYX_VALIGN_MIDDLE);
4183 case Tabular::TABULAR_VALIGN_BOTTOM:
4184 status.setOnOff(tabular.tabular_valignment
4185 == Tabular::LYX_VALIGN_BOTTOM);
4188 case Tabular::LONGTABULAR_ALIGN_LEFT:
4189 status.setOnOff(tabular.longtabular_alignment
4190 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4192 case Tabular::LONGTABULAR_ALIGN_CENTER:
4193 status.setOnOff(tabular.longtabular_alignment
4194 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4196 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4197 status.setOnOff(tabular.longtabular_alignment
4198 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4201 case Tabular::UNSET_ROTATE_TABULAR:
4202 status.setOnOff(!tabular.rotate);
4205 case Tabular::TOGGLE_ROTATE_CELL:
4206 case Tabular::SET_ROTATE_CELL:
4207 status.setOnOff(!oneCellHasRotationState(false,
4208 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4211 case Tabular::UNSET_ROTATE_CELL:
4212 status.setOnOff(!oneCellHasRotationState(true,
4213 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4216 case Tabular::SET_USEBOX:
4217 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4220 // every row can only be one thing:
4221 // either a footer or header or caption
4222 case Tabular::SET_LTFIRSTHEAD:
4223 status.setEnabled(sel_row_start == sel_row_end
4224 && !tabular.ltCaption(sel_row_start));
4225 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4228 case Tabular::UNSET_LTFIRSTHEAD:
4229 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4232 case Tabular::SET_LTHEAD:
4233 status.setEnabled(sel_row_start == sel_row_end
4234 && !tabular.ltCaption(sel_row_start));
4235 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4238 case Tabular::UNSET_LTHEAD:
4239 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4242 case Tabular::SET_LTFOOT:
4243 status.setEnabled(sel_row_start == sel_row_end
4244 && !tabular.ltCaption(sel_row_start));
4245 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4248 case Tabular::UNSET_LTFOOT:
4249 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4252 case Tabular::SET_LTLASTFOOT:
4253 status.setEnabled(sel_row_start == sel_row_end
4254 && !tabular.ltCaption(sel_row_start));
4255 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4258 case Tabular::UNSET_LTLASTFOOT:
4259 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4262 case Tabular::SET_LTNEWPAGE:
4263 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4266 // only one row can be the caption
4267 case Tabular::TOGGLE_LTCAPTION:
4268 status.setEnabled(sel_row_start == sel_row_end
4269 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4270 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4271 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4272 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4273 && (!tabular.haveLTCaption()
4274 || tabular.ltCaption(sel_row_start)));
4275 status.setOnOff(tabular.ltCaption(sel_row_start));
4278 case Tabular::SET_BOOKTABS:
4279 status.setOnOff(tabular.use_booktabs);
4282 case Tabular::UNSET_BOOKTABS:
4283 status.setOnOff(!tabular.use_booktabs);
4288 status.setEnabled(false);
4294 // These are only enabled inside tabular
4295 case LFUN_CELL_BACKWARD:
4296 case LFUN_CELL_FORWARD:
4297 status.setEnabled(true);
4300 // disable these with multiple cells selected
4301 case LFUN_INSET_INSERT:
4302 case LFUN_TABULAR_INSERT:
4303 case LFUN_FLEX_INSERT:
4304 case LFUN_FLOAT_INSERT:
4305 case LFUN_FLOAT_WIDE_INSERT:
4306 case LFUN_FOOTNOTE_INSERT:
4307 case LFUN_MARGINALNOTE_INSERT:
4308 case LFUN_MATH_INSERT:
4309 case LFUN_MATH_MODE:
4310 case LFUN_MATH_MUTATE:
4311 case LFUN_MATH_DISPLAY:
4312 case LFUN_NOTE_INSERT:
4313 case LFUN_OPTIONAL_INSERT:
4314 case LFUN_BOX_INSERT:
4315 case LFUN_BRANCH_INSERT:
4316 case LFUN_PHANTOM_INSERT:
4317 case LFUN_WRAP_INSERT:
4318 case LFUN_ERT_INSERT: {
4319 if (cur.selIsMultiCell()) {
4320 status.setEnabled(false);
4323 return cell(cur.idx())->getStatus(cur, cmd, status);
4326 // disable in non-fixed-width cells
4327 case LFUN_NEWLINE_INSERT:
4328 case LFUN_BREAK_PARAGRAPH: {
4329 if (tabular.getPWidth(cur.idx()).zero()) {
4330 status.setEnabled(false);
4333 return cell(cur.idx())->getStatus(cur, cmd, status);
4336 case LFUN_NEWPAGE_INSERT:
4337 status.setEnabled(false);
4341 if (tabularStackDirty() && theClipboard().isInternal()) {
4342 if (cur.selIsMultiCell()) {
4345 getSelection(cur, rs, re, cs, ce);
4346 if (paste_tabular && paste_tabular->column_info.size() == ce - cs + 1
4347 && paste_tabular->row_info.size() == re - rs + 1)
4348 status.setEnabled(true);
4350 status.setEnabled(false);
4351 status.message(_("Selection size should match clipboard content."));
4354 status.setEnabled(true);
4357 return cell(cur.idx())->getStatus(cur, cmd, status);
4359 case LFUN_INSET_SETTINGS:
4360 // relay this lfun to Inset, not to the cell.
4361 return Inset::getStatus(cur, cmd, status);
4364 // we try to handle this event in the insets dispatch function.
4365 return cell(cur.idx())->getStatus(cur, cmd, status);
4371 Inset::DisplayType InsetTabular::display() const
4373 if (tabular.is_long_tabular) {
4374 switch (tabular.longtabular_alignment) {
4375 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4377 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4379 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4389 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4391 return tabular.latex(os, runparams);
4395 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4397 os << '\n'; // output table on a new line
4398 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4399 tabular.plaintext(os, runparams, dp, false, 0);
4400 return PLAINTEXT_NEWLINE;
4404 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4409 // FIXME: Why not pass a proper DocIterator here?
4411 // if the table is inside a float it doesn't need the informaltable
4412 // wrapper. Search for it.
4413 for (master = owner(); master; master = master->owner())
4414 if (master->lyxCode() == FLOAT_CODE)
4419 os << "<informaltable>";
4422 ret += tabular.docbook(os, runparams);
4424 os << "</informaltable>";
4431 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4434 // It'd be better to be able to get this from an InsetLayout, but at present
4435 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4436 xs << html::StartTag("table");
4437 docstring ret = tabular.xhtml(xs, rp);
4438 xs << html::EndTag("table");
4443 void InsetTabular::validate(LaTeXFeatures & features) const
4445 tabular.validate(features);
4447 // It'd be better to be able to get this from an InsetLayout, but at present
4448 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4449 if (features.runparams().flavor == OutputParams::HTML)
4450 features.addPreambleSnippet("<style type=\"text/css\">\n"
4451 "table { border: 1px solid black; display: inline-block; }\n"
4452 "td { border: 1px solid black; padding: 0.5ex; }\n"
4457 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4459 return tabular.cellInset(idx);
4463 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4465 return tabular.cellInset(idx);
4469 void InsetTabular::cursorPos(BufferView const & bv,
4470 CursorSlice const & sl, bool boundary, int & x, int & y) const
4472 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4474 int const row = tabular.cellRow(sl.idx());
4475 int const col = tabular.cellColumn(sl.idx());
4477 // y offset correction
4478 for (int i = 0; i <= row; ++i) {
4479 if (tabular.isPartOfMultiRow(i, col))
4482 y += tabular.rowAscent(i);
4483 y += tabular.interRowSpace(i);
4486 y += tabular.rowDescent(i);
4488 y += tabular.textVOffset(sl.idx());
4490 // x offset correction
4491 for (int c = 0; c < col; ++c)
4492 x += tabular.column_info[c].width;
4494 x += tabular.textHOffset(sl.idx());
4495 x += ADD_TO_TABULAR_WIDTH;
4500 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4504 Inset const & inset = *tabular.cellInset(cell);
4505 Point o = bv.coordCache().getInsets().xy(&inset);
4506 int const xbeg = o.x_ - tabular.textHOffset(cell);
4507 int const xend = xbeg + tabular.columnWidth(cell);
4508 row_type const row = tabular.cellRow(cell);
4509 int const ybeg = o.y_ - tabular.rowAscent(row)
4510 - tabular.interRowSpace(row);
4511 int const yend = o.y_ + tabular.rowDescent(row);
4523 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4524 // << " ybeg=" << ybeg << " yend=" << yend
4525 // << " xx=" << xx << " yy=" << yy
4526 // << " dist=" << xx + yy << endl;
4531 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4533 //lyxerr << "InsetTabular::editXY: " << this << endl;
4534 cur.setSelection(false);
4536 cur.idx() = getNearestCell(cur.bv(), x, y);
4538 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4542 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4544 cur.idx() = getNearestCell(cur.bv(), x, y);
4545 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4549 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4551 idx_type idx_min = 0;
4552 int dist_min = numeric_limits<int>::max();
4553 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4554 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4555 int const d = dist(bv, i, x, y);
4566 int InsetTabular::cellXPos(idx_type const cell) const
4568 col_type col = tabular.cellColumn(cell);
4570 for (col_type c = 0; c < col; ++c)
4571 lx += tabular.columnWidth(c);
4577 void InsetTabular::resetPos(Cursor & cur) const
4579 BufferView & bv = cur.bv();
4580 int const maxwidth = bv.workWidth();
4582 int const scx_old = scx_;
4583 int const i = cur.find(this);
4588 int const X2 = maxwidth;
4589 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4590 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4591 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4594 scx_ = X1 + 20 - x1;
4596 scx_ = X2 - 20 - x2;
4601 // only update if offset changed
4602 if (scx_ != scx_old)
4603 cur.updateFlags(Update::Force | Update::FitCursor);
4607 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4609 row_type const row = tabular.cellRow(cur.idx());
4610 col_type const col = tabular.cellColumn(cur.idx());
4612 if (isRightToLeft(cur)) {
4613 if (tabular.cellColumn(cur.idx()) == 0) {
4614 if (row == tabular.row_info.size() - 1)
4616 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4621 cur.idx() = tabular.getLastCellInRow(row - 1);
4623 cur.idx() = tabular.cellIndex(row, col - 1);
4626 if (tabular.isLastCell(cur.idx()))
4628 if (cur.idx() == tabular.getLastCellInRow(row))
4629 cur.idx() = tabular.cellIndex(row + 1, 0);
4631 cur.idx() = tabular.cellIndex(row, col + 1);
4634 cur.boundary(false);
4636 if (cur.selIsMultiCell()) {
4637 cur.pit() = cur.lastpit();
4638 cur.pos() = cur.lastpos();
4646 // in visual mode, place cursor at extreme left or right
4648 switch(entry_from) {
4650 case ENTRY_DIRECTION_RIGHT:
4651 cur.posVisToRowExtremity(false /* !left */);
4653 case ENTRY_DIRECTION_LEFT:
4654 cur.posVisToRowExtremity(true /* left */);
4656 case ENTRY_DIRECTION_IGNORE:
4657 // nothing to do in this case
4661 cur.setCurrentFont();
4666 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4668 row_type const row = tabular.cellRow(cur.idx());
4669 col_type const col = tabular.cellColumn(cur.idx());
4671 if (isRightToLeft(cur)) {
4672 if (cur.idx() == tabular.getLastCellInRow(row)) {
4675 cur.idx() = tabular.getFirstCellInRow(row);
4676 cur.idx() = tabular.cellAbove(cur.idx());
4678 if (tabular.isLastCell(cur.idx()))
4680 if (cur.idx() == tabular.getLastCellInRow(row))
4681 cur.idx() = tabular.cellIndex(row + 1, 0);
4683 cur.idx() = tabular.cellIndex(row, col + 1);
4686 if (cur.idx() == 0) // first cell
4689 cur.idx() = tabular.getLastCellInRow(row - 1);
4691 cur.idx() = tabular.cellIndex(row, col - 1);
4694 if (cur.selIsMultiCell()) {
4695 cur.pit() = cur.lastpit();
4696 cur.pos() = cur.lastpos();
4701 cur.pit() = cur.lastpit();
4702 cur.pos() = cur.lastpos();
4704 // in visual mode, place cursor at extreme left or right
4706 switch(entry_from) {
4708 case ENTRY_DIRECTION_RIGHT:
4709 cur.posVisToRowExtremity(false /* !left */);
4711 case ENTRY_DIRECTION_LEFT:
4712 cur.posVisToRowExtremity(true /* left */);
4714 case ENTRY_DIRECTION_IGNORE:
4715 // nothing to do in this case
4719 cur.setCurrentFont();
4724 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4726 istringstream is(argument);
4729 if (insetCode(s) != TABULAR_CODE)
4733 size_t safe_guard = 0;
4736 if (safe_guard > 1000) {
4737 LYXERR0("parameter max count reached!");
4743 Tabular::Feature action = Tabular::LAST_ACTION;
4746 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4747 if (s != tabularFeature[i].feature)
4750 action = tabularFeature[i].action;
4753 if (action == Tabular::LAST_ACTION) {
4754 LYXERR0("Feature not found " << s);
4758 if (tabularFeature[i].need_value)
4760 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4761 tabularFeatures(cur, action, val);
4767 static void checkLongtableSpecial(Tabular::ltType & ltt,
4768 string const & special, bool & flag)
4770 if (special == "dl_above") {
4773 } else if (special == "dl_below") {
4774 ltt.bottomDL = flag;
4776 } else if (special == "empty") {
4785 bool InsetTabular::oneCellHasRotationState(bool rotated,
4786 row_type row_start, row_type row_end,
4787 col_type col_start, col_type col_end) const {
4789 for (row_type i = row_start; i <= row_end; ++i) {
4790 for (col_type j = col_start; j <= col_end; ++j) {
4791 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4800 void InsetTabular::tabularFeatures(Cursor & cur,
4801 Tabular::Feature feature, string const & value)
4803 col_type sel_col_start;
4804 col_type sel_col_end;
4805 row_type sel_row_start;
4806 row_type sel_row_end;
4807 bool setLines = false;
4808 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4809 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4813 case Tabular::M_ALIGN_LEFT:
4814 case Tabular::ALIGN_LEFT:
4815 setAlign = LYX_ALIGN_LEFT;
4818 case Tabular::M_ALIGN_RIGHT:
4819 case Tabular::ALIGN_RIGHT:
4820 setAlign = LYX_ALIGN_RIGHT;
4823 case Tabular::M_ALIGN_CENTER:
4824 case Tabular::ALIGN_CENTER:
4825 setAlign = LYX_ALIGN_CENTER;
4828 case Tabular::ALIGN_BLOCK:
4829 setAlign = LYX_ALIGN_BLOCK;
4832 case Tabular::M_VALIGN_TOP:
4833 case Tabular::VALIGN_TOP:
4834 setVAlign = Tabular::LYX_VALIGN_TOP;
4837 case Tabular::M_VALIGN_BOTTOM:
4838 case Tabular::VALIGN_BOTTOM:
4839 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4842 case Tabular::M_VALIGN_MIDDLE:
4843 case Tabular::VALIGN_MIDDLE:
4844 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4851 cur.recordUndoInset(ATOMIC_UNDO);
4853 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4854 row_type const row = tabular.cellRow(cur.idx());
4855 col_type const column = tabular.cellColumn(cur.idx());
4857 Tabular::ltType ltt;
4861 case Tabular::SET_PWIDTH: {
4862 Length const len(value);
4863 tabular.setColumnPWidth(cur, cur.idx(), len);
4865 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4866 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4870 case Tabular::SET_MPWIDTH:
4871 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4874 case Tabular::SET_SPECIAL_COLUMN:
4875 case Tabular::SET_SPECIAL_MULTICOLUMN:
4876 if (value == "none")
4877 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
4879 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4882 case Tabular::SET_SPECIAL_MULTIROW:
4883 //FIXME: noting to do here?
4886 case Tabular::APPEND_ROW:
4887 // append the row into the tabular
4888 tabular.appendRow(cur.idx());
4891 case Tabular::APPEND_COLUMN:
4892 // append the column into the tabular
4893 tabular.appendColumn(cur.idx());
4894 cur.idx() = tabular.cellIndex(row, column);
4897 case Tabular::DELETE_ROW:
4898 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4899 tabular.deleteRow(sel_row_start);
4900 if (sel_row_start >= tabular.row_info.size())
4902 cur.idx() = tabular.cellIndex(sel_row_start, column);
4905 cur.setSelection(false);
4908 case Tabular::DELETE_COLUMN:
4909 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4910 tabular.deleteColumn(sel_col_start);
4911 if (sel_col_start >= tabular.column_info.size())
4913 cur.idx() = tabular.cellIndex(row, sel_col_start);
4916 cur.setSelection(false);
4919 case Tabular::COPY_ROW:
4920 tabular.copyRow(row);
4923 case Tabular::COPY_COLUMN:
4924 tabular.copyColumn(column);
4925 cur.idx() = tabular.cellIndex(row, column);
4928 case Tabular::TOGGLE_LINE_TOP: {
4929 bool lineSet = !tabular.topLine(cur.idx());
4930 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4931 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4932 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4936 case Tabular::TOGGLE_LINE_BOTTOM: {
4937 bool lineSet = !tabular.bottomLine(cur.idx());
4938 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4939 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4940 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4944 case Tabular::TOGGLE_LINE_LEFT: {
4945 bool lineSet = !tabular.leftLine(cur.idx());
4946 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4947 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4948 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4952 case Tabular::TOGGLE_LINE_RIGHT: {
4953 bool lineSet = !tabular.rightLine(cur.idx());
4954 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4955 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4956 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4960 case Tabular::M_ALIGN_LEFT:
4961 case Tabular::M_ALIGN_RIGHT:
4962 case Tabular::M_ALIGN_CENTER:
4964 case Tabular::ALIGN_LEFT:
4965 case Tabular::ALIGN_RIGHT:
4966 case Tabular::ALIGN_CENTER:
4967 case Tabular::ALIGN_BLOCK:
4968 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4969 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4970 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4973 case Tabular::M_VALIGN_TOP:
4974 case Tabular::M_VALIGN_BOTTOM:
4975 case Tabular::M_VALIGN_MIDDLE:
4977 case Tabular::VALIGN_TOP:
4978 case Tabular::VALIGN_BOTTOM:
4979 case Tabular::VALIGN_MIDDLE:
4980 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4981 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4982 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4985 case Tabular::MULTICOLUMN: {
4986 if (!cur.selection()) {
4987 // just multicol for one single cell
4988 // check whether we are completely in a multicol
4989 if (tabular.isMultiColumn(cur.idx()))
4990 tabular.unsetMultiColumn(cur.idx());
4992 tabular.setMultiColumn(cur.idx(), 1);
4995 // we have a selection so this means we just add all this
4996 // cells to form a multicolumn cell
4997 idx_type const s_start = cur.selBegin().idx();
4998 row_type const col_start = tabular.cellColumn(s_start);
4999 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5000 tabular.setMultiColumn(s_start, col_end - col_start + 1);
5001 cur.idx() = s_start;
5004 cur.setSelection(false);
5008 case Tabular::MULTIROW: {
5009 if (!cur.selection()) {
5010 // just multirow for one single cell
5011 // check whether we are completely in a multirow
5012 if (tabular.isMultiRow(cur.idx()))
5013 tabular.unsetMultiRow(cur.idx());
5015 tabular.setMultiRow(cur.idx(), 1);
5018 // we have a selection so this means we just add all this
5019 // cells to form a multirow cell
5020 idx_type const s_start = cur.selBegin().idx();
5021 row_type const row_start = tabular.cellRow(s_start);
5022 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5023 tabular.setMultiRow(s_start, row_end - row_start + 1);
5024 cur.idx() = s_start;
5027 cur.setSelection(false);
5031 case Tabular::SET_ALL_LINES:
5033 case Tabular::UNSET_ALL_LINES:
5034 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5035 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5036 tabular.setAllLines(
5037 tabular.cellIndex(i,j), setLines);
5040 case Tabular::SET_BORDER_LINES:
5041 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5042 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
5043 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
5045 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
5046 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
5047 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
5051 case Tabular::SET_LONGTABULAR:
5052 tabular.is_long_tabular = true;
5055 case Tabular::UNSET_LONGTABULAR:
5056 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
5057 if (tabular.ltCaption(i)) {
5058 cur.idx() = tabular.cellIndex(i, 0);
5061 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5064 tabular.is_long_tabular = false;
5067 case Tabular::SET_ROTATE_TABULAR:
5068 tabular.rotate = true;
5071 case Tabular::UNSET_ROTATE_TABULAR:
5072 tabular.rotate = false;
5075 case Tabular::TOGGLE_ROTATE_TABULAR:
5076 tabular.rotate = !tabular.rotate;
5079 case Tabular::TABULAR_VALIGN_TOP:
5080 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5083 case Tabular::TABULAR_VALIGN_MIDDLE:
5084 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5087 case Tabular::TABULAR_VALIGN_BOTTOM:
5088 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5091 case Tabular::LONGTABULAR_ALIGN_LEFT:
5092 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5095 case Tabular::LONGTABULAR_ALIGN_CENTER:
5096 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5099 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5100 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5105 case Tabular::SET_ROTATE_CELL:
5106 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5107 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5108 tabular.setRotateCell(
5109 tabular.cellIndex(i, j), true);
5112 case Tabular::UNSET_ROTATE_CELL:
5113 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5114 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5115 tabular.setRotateCell(tabular.cellIndex(i, j), false);
5118 case Tabular::TOGGLE_ROTATE_CELL:
5120 bool oneNotRotated = oneCellHasRotationState(false,
5121 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5123 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5124 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5125 tabular.setRotateCell(tabular.cellIndex(i, j),
5130 case Tabular::SET_USEBOX: {
5131 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5132 if (val == tabular.getUsebox(cur.idx()))
5133 val = Tabular::BOX_NONE;
5134 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5135 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5136 tabular.setUsebox(tabular.cellIndex(i, j), val);
5140 case Tabular::UNSET_LTFIRSTHEAD:
5142 case Tabular::SET_LTFIRSTHEAD:
5143 tabular.getRowOfLTFirstHead(row, ltt);
5144 checkLongtableSpecial(ltt, value, flag);
5145 tabular.setLTHead(row, flag, ltt, true);
5148 case Tabular::UNSET_LTHEAD:
5150 case Tabular::SET_LTHEAD:
5151 tabular.getRowOfLTHead(row, ltt);
5152 checkLongtableSpecial(ltt, value, flag);
5153 tabular.setLTHead(row, flag, ltt, false);
5156 case Tabular::UNSET_LTFOOT:
5158 case Tabular::SET_LTFOOT:
5159 tabular.getRowOfLTFoot(row, ltt);
5160 checkLongtableSpecial(ltt, value, flag);
5161 tabular.setLTFoot(row, flag, ltt, false);
5164 case Tabular::UNSET_LTLASTFOOT:
5166 case Tabular::SET_LTLASTFOOT:
5167 tabular.getRowOfLTLastFoot(row, ltt);
5168 checkLongtableSpecial(ltt, value, flag);
5169 tabular.setLTFoot(row, flag, ltt, true);
5172 case Tabular::SET_LTNEWPAGE:
5173 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5176 case Tabular::TOGGLE_LTCAPTION: {
5177 bool const set = !tabular.ltCaption(row);
5178 cur.idx() = tabular.setLTCaption(row, set);
5181 cur.setSelection(false);
5184 // When a row is set as caption, then also insert
5185 // a caption. Otherwise the LaTeX output is broken.
5186 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5187 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5189 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5190 if (lyx::getStatus(fr).enabled())
5196 case Tabular::SET_BOOKTABS:
5197 tabular.use_booktabs = true;
5200 case Tabular::UNSET_BOOKTABS:
5201 tabular.use_booktabs = false;
5204 case Tabular::SET_TOP_SPACE: {
5206 if (value == "default")
5207 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5208 tabular.row_info[i].top_space_default = true;
5209 else if (value == "none")
5210 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5211 tabular.row_info[i].top_space_default = false;
5212 tabular.row_info[i].top_space = len;
5214 else if (isValidLength(value, &len))
5215 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5216 tabular.row_info[i].top_space_default = false;
5217 tabular.row_info[i].top_space = len;
5222 case Tabular::SET_BOTTOM_SPACE: {
5224 if (value == "default")
5225 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5226 tabular.row_info[i].bottom_space_default = true;
5227 else if (value == "none")
5228 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5229 tabular.row_info[i].bottom_space_default = false;
5230 tabular.row_info[i].bottom_space = len;
5232 else if (isValidLength(value, &len))
5233 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5234 tabular.row_info[i].bottom_space_default = false;
5235 tabular.row_info[i].bottom_space = len;
5240 case Tabular::SET_INTERLINE_SPACE: {
5242 if (value == "default")
5243 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5244 tabular.row_info[i].interline_space_default = true;
5245 else if (value == "none")
5246 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5247 tabular.row_info[i].interline_space_default = false;
5248 tabular.row_info[i].interline_space = len;
5250 else if (isValidLength(value, &len))
5251 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5252 tabular.row_info[i].interline_space_default = false;
5253 tabular.row_info[i].interline_space = len;
5258 // dummy stuff just to avoid warnings
5259 case Tabular::LAST_ACTION:
5265 bool InsetTabular::copySelection(Cursor & cur)
5267 if (!cur.selection())
5272 getSelection(cur, rs, re, cs, ce);
5274 paste_tabular.reset(new Tabular(tabular));
5276 for (row_type i = 0; i < rs; ++i)
5277 paste_tabular->deleteRow(0);
5279 row_type const rows = re - rs + 1;
5280 while (paste_tabular->row_info.size() > rows)
5281 paste_tabular->deleteRow(rows);
5283 for (col_type i = 0; i < cs; ++i)
5284 paste_tabular->deleteColumn(0);
5286 col_type const columns = ce - cs + 1;
5287 while (paste_tabular->column_info.size() > columns)
5288 paste_tabular->deleteColumn(columns);
5290 paste_tabular->setBuffer(tabular.buffer());
5292 odocstringstream os;
5293 OutputParams const runparams(0);
5294 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5295 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5296 cap::copySelection(cur, os.str());
5298 // mark tabular stack dirty
5299 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5300 // when we (hopefully) have a one-for-all paste mechanism.
5301 // This must be called after cap::copySelection.
5302 dirtyTabularStack(true);
5308 bool InsetTabular::pasteClipboard(Cursor & cur)
5312 col_type actcol = tabular.cellColumn(cur.idx());
5313 row_type actrow = tabular.cellRow(cur.idx());
5315 if (cur.selIsMultiCell()) {
5318 getSelection(cur, actrow, re, actcol, ce);
5321 for (row_type r1 = 0, r2 = actrow;
5322 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
5324 for (col_type c1 = 0, c2 = actcol;
5325 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
5327 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5328 tabular.isPartOfMultiColumn(r2, c2))
5330 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5334 if (tabular.isPartOfMultiColumn(r2, c2)) {
5338 shared_ptr<InsetTableCell> inset(
5339 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5340 tabular.setCellInset(r2, c2, inset);
5341 // FIXME?: why do we need to do this explicitly? (EL)
5342 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5344 // FIXME: change tracking (MG)
5345 inset->setChange(Change(buffer().params().trackChanges ?
5346 Change::INSERTED : Change::UNCHANGED));
5354 void InsetTabular::cutSelection(Cursor & cur)
5356 if (!cur.selection())
5361 getSelection(cur, rs, re, cs, ce);
5362 for (row_type i = rs; i <= re; ++i) {
5363 for (col_type j = cs; j <= ce; ++j) {
5364 shared_ptr<InsetTableCell> t
5365 = cell(tabular.cellIndex(i, j));
5366 if (buffer().params().trackChanges)
5367 // FIXME: Change tracking (MG)
5368 t->setChange(Change(Change::DELETED));
5374 // cursor position might be invalid now
5375 if (cur.pit() > cur.lastpit())
5376 cur.pit() = cur.lastpit();
5377 if (cur.pos() > cur.lastpos())
5378 cur.pos() = cur.lastpos();
5379 cur.clearSelection();
5383 bool InsetTabular::isRightToLeft(Cursor & cur) const
5385 LASSERT(cur.depth() > 1, /**/);
5386 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5387 pos_type const parentpos = cur[cur.depth() - 2].pos();
5388 return parentpar.getFontSettings(buffer().params(),
5389 parentpos).language()->rightToLeft();
5392 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5395 LASSERT(stidx <= enidx, return docstring());
5397 col_type const col1 = tabular.cellColumn(stidx);
5398 col_type const col2 = tabular.cellColumn(enidx);
5399 row_type const row1 = tabular.cellRow(stidx);
5400 row_type const row2 = tabular.cellRow(enidx);
5402 for (col_type col = col1; col <= col2; col++)
5403 for (row_type row = row1; row <= row2; row++) {
5408 retval += tabular.cellInset(row, col)->asString(intoInsets);
5414 void InsetTabular::getSelection(Cursor & cur,
5415 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5417 CursorSlice const & beg = cur.selBegin();
5418 CursorSlice const & end = cur.selEnd();
5419 cs = tabular.cellColumn(beg.idx());
5420 ce = tabular.cellColumn(end.idx());
5423 cs = tabular.cellColumn(end.idx());
5425 ce = tabular.cellRightColumn(end.idx());
5428 rs = tabular.cellRow(beg.idx());
5429 re = tabular.cellRow(end.idx());
5435 Text * InsetTabular::getText(int idx) const
5437 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5441 void InsetTabular::setChange(Change const & change)
5443 for (idx_type idx = 0; idx < nargs(); ++idx)
5444 cell(idx)->setChange(change);
5448 void InsetTabular::acceptChanges()
5450 for (idx_type idx = 0; idx < nargs(); ++idx)
5451 cell(idx)->acceptChanges();
5455 void InsetTabular::rejectChanges()
5457 for (idx_type idx = 0; idx < nargs(); ++idx)
5458 cell(idx)->rejectChanges();
5462 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5464 return tabular.getPWidth(cell).zero();
5468 bool InsetTabular::forcePlainLayout(idx_type cell) const
5470 return !tabular.getPWidth(cell).zero();
5474 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5477 if (buf.length() <= 0)
5482 col_type maxCols = 1;
5483 size_t const len = buf.length();
5487 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5495 maxCols = max(cols, maxCols);
5501 maxCols = max(cols, maxCols);
5507 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5508 loctab = paste_tabular.get();
5510 dirtyTabularStack(true);
5513 cell = bv.cursor().idx();
5514 ocol = tabular.cellColumn(cell);
5515 row = tabular.cellRow(cell);
5519 idx_type const cells = loctab->numberofcells;
5522 rows = loctab->row_info.size();
5523 col_type const columns = loctab->column_info.size();
5525 while (cell < cells && p < len && row < rows &&
5526 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5532 // we can only set this if we are not too far right
5533 if (cols < columns) {
5534 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5535 Font const font = bv.textMetrics(&inset->text()).
5537 inset->setText(buf.substr(op, p - op), font,
5538 buffer().params().trackChanges);
5544 // we can only set this if we are not too far right
5545 if (cols < columns) {
5546 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5547 Font const font = bv.textMetrics(&inset->text()).
5549 inset->setText(buf.substr(op, p - op), font,
5550 buffer().params().trackChanges);
5555 cell = loctab->cellIndex(row, cols);
5561 // check for the last cell if there is no trailing '\n'
5562 if (cell < cells && op < len) {
5563 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5564 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5565 inset->setText(buf.substr(op, len - op), font,
5566 buffer().params().trackChanges);
5572 void InsetTabular::addPreview(DocIterator const & inset_pos,
5573 PreviewLoader & loader) const
5575 row_type const rows = tabular.row_info.size();
5576 col_type const columns = tabular.column_info.size();
5577 DocIterator cell_pos = inset_pos;
5579 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5580 for (row_type i = 0; i < rows; ++i) {
5581 for (col_type j = 0; j < columns; ++j) {
5582 cell_pos.top().idx() = tabular.cellIndex(i, j);
5583 tabular.cellInset(i, j)->addPreview(cell_pos, loader);
5589 bool InsetTabular::completionSupported(Cursor const & cur) const
5591 Cursor const & bvCur = cur.bv().cursor();
5592 if (&bvCur.inset() != this)
5594 return cur.text()->completionSupported(cur);
5598 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5600 return completionSupported(cur);
5604 bool InsetTabular::automaticInlineCompletion() const
5606 return lyxrc.completion_inline_text;
5610 bool InsetTabular::automaticPopupCompletion() const
5612 return lyxrc.completion_popup_text;
5616 bool InsetTabular::showCompletionCursor() const
5618 return lyxrc.completion_cursor_text;
5622 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5624 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5628 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5630 if (!completionSupported(cur))
5632 return cur.text()->completionPrefix(cur);
5636 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5638 if (!completionSupported(cur))
5641 return cur.text()->insertCompletion(cur, s, finished);
5645 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5646 Dimension & dim) const
5648 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5649 tm.completionPosAndDim(cur, x, y, dim);
5653 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5655 istringstream data(in);
5657 lex.setStream(data);
5664 if (!lex || token != "tabular") {
5665 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5669 // This is part of the inset proper that is usually swallowed
5670 // by Buffer::readInset
5672 if (!lex || token != "Tabular") {
5673 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5681 string InsetTabular::params2string(InsetTabular const & inset)
5684 data << "tabular" << ' ';
5686 data << "\\end_inset\n";