2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "CoordCache.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "FuncRequest.h"
33 #include "FuncStatus.h"
35 #include "LaTeXFeatures.h"
39 #include "MetricsInfo.h"
40 #include "OutputParams.h"
41 #include "paragraph_funcs.h"
42 #include "Paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "ParIterator.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #include "frontends/Selection.h"
54 #include "support/convert.h"
55 #include "support/debug.h"
56 #include "support/docstream.h"
57 #include "support/FileName.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
61 #include <boost/scoped_ptr.hpp>
69 using namespace lyx::support;
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
76 using cap::dirtyTabularStack;
77 using cap::tabularStackDirty;
79 using graphics::PreviewLoader;
81 using frontend::Painter;
82 using frontend::Clipboard;
84 namespace Alert = frontend::Alert;
89 int const ADD_TO_HEIGHT = 2;
90 int const ADD_TO_TABULAR_WIDTH = 2;
91 int const default_line_space = 10;
92 int const WIDTH_OF_LINE = 5;
96 boost::scoped_ptr<Tabular> paste_tabular;
99 struct TabularFeature {
100 Tabular::Feature action;
105 TabularFeature tabularFeature[] =
107 { Tabular::APPEND_ROW, "append-row" },
108 { Tabular::APPEND_COLUMN, "append-column" },
109 { Tabular::DELETE_ROW, "delete-row" },
110 { Tabular::DELETE_COLUMN, "delete-column" },
111 { Tabular::COPY_ROW, "copy-row" },
112 { Tabular::COPY_COLUMN, "copy-column" },
113 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
114 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
115 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
116 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
117 { Tabular::ALIGN_LEFT, "align-left" },
118 { Tabular::ALIGN_RIGHT, "align-right" },
119 { Tabular::ALIGN_CENTER, "align-center" },
120 { Tabular::ALIGN_BLOCK, "align-block" },
121 { Tabular::VALIGN_TOP, "valign-top" },
122 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
123 { Tabular::VALIGN_MIDDLE, "valign-middle" },
124 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
125 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
126 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
127 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
128 { Tabular::M_ALIGN_LEFT, "m-align-left" },
129 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
130 { Tabular::M_ALIGN_CENTER, "m-align-center" },
131 { Tabular::M_VALIGN_TOP, "m-valign-top" },
132 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
133 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
134 { Tabular::MULTICOLUMN, "multicolumn" },
135 { Tabular::SET_ALL_LINES, "set-all-lines" },
136 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
137 { Tabular::SET_LONGTABULAR, "set-longtabular" },
138 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
139 { Tabular::SET_PWIDTH, "set-pwidth" },
140 { Tabular::SET_MPWIDTH, "set-mpwidth" },
141 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
142 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
143 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
144 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
145 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
146 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
147 { Tabular::SET_USEBOX, "set-usebox" },
148 { Tabular::SET_LTHEAD, "set-lthead" },
149 { Tabular::UNSET_LTHEAD, "unset-lthead" },
150 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
151 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
152 { Tabular::SET_LTFOOT, "set-ltfoot" },
153 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
154 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
155 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
156 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
157 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
158 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
159 { Tabular::SET_BOOKTABS, "set-booktabs" },
160 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
161 { Tabular::SET_TOP_SPACE, "set-top-space" },
162 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
163 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
164 { Tabular::LAST_ACTION, "" }
168 class FeatureEqual : public unary_function<TabularFeature, bool> {
170 FeatureEqual(Tabular::Feature feature)
171 : feature_(feature) {}
172 bool operator()(TabularFeature const & tf) const {
173 return tf.action == feature_;
176 Tabular::Feature feature_;
181 string const write_attribute(string const & name, T const & t)
183 string const s = tostr(t);
184 return s.empty() ? s : " " + name + "=\"" + s + "\"";
188 string const write_attribute(string const & name, string const & t)
190 return t.empty() ? t : " " + name + "=\"" + t + "\"";
195 string const write_attribute(string const & name, docstring const & t)
197 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
202 string const write_attribute(string const & name, bool const & b)
204 // we write only true attribute values so we remove a bit of the
205 // file format bloat for tabulars.
206 return b ? write_attribute(name, convert<string>(b)) : string();
211 string const write_attribute(string const & name, int const & i)
213 // we write only true attribute values so we remove a bit of the
214 // file format bloat for tabulars.
215 return i ? write_attribute(name, convert<string>(i)) : string();
220 string const write_attribute(string const & name, Tabular::idx_type const & i)
222 // we write only true attribute values so we remove a bit of the
223 // file format bloat for tabulars.
224 return i ? write_attribute(name, convert<string>(i)) : string();
229 string const write_attribute(string const & name, Length const & value)
231 // we write only the value if we really have one same reson as above.
232 return value.zero() ? string() : write_attribute(name, value.asString());
236 string const tostr(LyXAlignment const & num)
241 case LYX_ALIGN_BLOCK:
245 case LYX_ALIGN_CENTER:
247 case LYX_ALIGN_RIGHT:
249 case LYX_ALIGN_LAYOUT:
251 case LYX_ALIGN_SPECIAL:
258 string const tostr(Tabular::VAlignment const & num)
261 case Tabular::LYX_VALIGN_TOP:
263 case Tabular::LYX_VALIGN_MIDDLE:
265 case Tabular::LYX_VALIGN_BOTTOM:
272 string const tostr(Tabular::BoxType const & num)
275 case Tabular::BOX_NONE:
277 case Tabular::BOX_PARBOX:
279 case Tabular::BOX_MINIPAGE:
286 // I would have liked a fromstr template a lot better. (Lgb)
287 bool string2type(string const str, LyXAlignment & num)
290 num = LYX_ALIGN_NONE;
291 else if (str == "block")
292 num = LYX_ALIGN_BLOCK;
293 else if (str == "left")
294 num = LYX_ALIGN_LEFT;
295 else if (str == "center")
296 num = LYX_ALIGN_CENTER;
297 else if (str == "right")
298 num = LYX_ALIGN_RIGHT;
305 bool string2type(string const str, Tabular::VAlignment & num)
308 num = Tabular::LYX_VALIGN_TOP;
309 else if (str == "middle" )
310 num = Tabular::LYX_VALIGN_MIDDLE;
311 else if (str == "bottom")
312 num = Tabular::LYX_VALIGN_BOTTOM;
319 bool string2type(string const str, Tabular::BoxType & num)
322 num = Tabular::BOX_NONE;
323 else if (str == "parbox")
324 num = Tabular::BOX_PARBOX;
325 else if (str == "minipage")
326 num = Tabular::BOX_MINIPAGE;
333 bool string2type(string const str, bool & num)
337 else if (str == "false")
345 bool getTokenValue(string const & str, char const * token, string & ret)
348 size_t token_length = strlen(token);
349 size_t pos = str.find(token);
351 if (pos == string::npos || pos + token_length + 1 >= str.length()
352 || str[pos + token_length] != '=')
354 pos += token_length + 1;
356 if (ch != '"' && ch != '\'') { // only read till next space
360 while (pos < str.length() - 1 && str[++pos] != ch)
367 bool getTokenValue(string const & str, char const * token, docstring & ret)
370 bool const success = getTokenValue(str, token, tmp);
371 ret = from_utf8(tmp);
376 bool getTokenValue(string const & str, char const * token, int & num)
380 if (!getTokenValue(str, token, tmp))
382 num = convert<int>(tmp);
387 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
390 return getTokenValue(str, token, tmp) && string2type(tmp, num);
394 bool getTokenValue(string const & str, char const * token,
395 Tabular::VAlignment & num)
398 return getTokenValue(str, token, tmp) && string2type(tmp, num);
402 bool getTokenValue(string const & str, char const * token,
403 Tabular::BoxType & num)
406 return getTokenValue(str, token, tmp) && string2type(tmp, num);
410 bool getTokenValue(string const & str, char const * token, bool & flag)
412 // set the flag always to false as this should be the default for bools
413 // not in the file-format.
416 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
420 bool getTokenValue(string const & str, char const * token, Length & len)
422 // set the length to be zero() as default as this it should be if not
423 // in the file format.
426 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
430 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
435 if (!getTokenValue(str, token, tmp))
437 if (tmp == "default") {
441 return isValidLength(tmp, &len);
445 void l_getline(istream & is, string & str)
448 while (str.empty()) {
450 if (!str.empty() && str[str.length() - 1] == '\r')
451 str.erase(str.length() - 1);
458 string const featureAsString(Tabular::Feature feature)
460 TabularFeature * end = tabularFeature +
461 sizeof(tabularFeature) / sizeof(TabularFeature);
462 TabularFeature * it = find_if(tabularFeature, end,
463 FeatureEqual(feature));
464 return (it == end) ? string() : it->feature;
469 /////////////////////////////////////////////////////////////////////
473 /////////////////////////////////////////////////////////////////////
476 Tabular::CellData::CellData(Buffer const & buf)
479 multicolumn(Tabular::CELL_NORMAL),
480 alignment(LYX_ALIGN_CENTER),
481 valignment(LYX_VALIGN_TOP),
488 inset(new InsetText(buf))
490 inset->setBuffer(const_cast<Buffer &>(buf));
491 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
495 Tabular::CellData::CellData(CellData const & cs)
498 multicolumn(cs.multicolumn),
499 alignment(cs.alignment),
500 valignment(cs.valignment),
501 top_line(cs.top_line),
502 bottom_line(cs.bottom_line),
503 left_line(cs.left_line),
504 right_line(cs.right_line),
507 align_special(cs.align_special),
509 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
513 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
520 void Tabular::CellData::swap(CellData & rhs)
522 std::swap(cellno, rhs.cellno);
523 std::swap(width, rhs.width);
524 std::swap(multicolumn, rhs.multicolumn);
525 std::swap(alignment, rhs.alignment);
526 std::swap(valignment, rhs.valignment);
527 std::swap(top_line, rhs.top_line);
528 std::swap(bottom_line, rhs.bottom_line);
529 std::swap(left_line, rhs.left_line);
530 std::swap(right_line, rhs.right_line);
531 std::swap(usebox, rhs.usebox);
532 std::swap(rotate, rhs.rotate);
533 std::swap(align_special, rhs.align_special);
534 p_width.swap(rhs.p_width);
535 inset.swap(rhs.inset);
539 Tabular::RowData::RowData()
544 top_space_default(false),
545 bottom_space_default(false),
546 interline_space_default(false),
555 Tabular::ColumnData::ColumnData()
556 : alignment(LYX_ALIGN_CENTER),
557 valignment(LYX_VALIGN_TOP),
565 Tabular::ltType::ltType()
578 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
580 init(buffer, rows_arg, columns_arg);
584 // activates all lines and sets all widths to 0
585 void Tabular::init(Buffer const & buf, row_type rows_arg,
586 col_type columns_arg)
589 row_info = row_vector(rows_arg);
590 column_info = column_vector(columns_arg);
591 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
592 row_info.reserve(10);
593 column_info.reserve(10);
594 cell_info.reserve(100);
596 for (row_type i = 0; i < rows_arg; ++i)
597 cell_info[i].back().right_line = true;
598 row_info.back().bottom_line = true;
599 row_info.front().bottom_line = true;
600 column_info.back().right_line = true;
601 is_long_tabular = false;
603 use_booktabs = false;
607 void Tabular::fixCellNums()
610 for (row_type i = 0; i < rowCount(); ++i) {
611 for (col_type j = 0; j < columnCount(); ++j) {
612 // When debugging it can be nice to set
614 cell_info[i][j].inset->setDrawFrame(false);
615 cell_info[i][j].cellno = cellno++;
617 cell_info[i].back().right_line = true;
624 void Tabular::appendRow(idx_type const cell)
626 BufferParams const & bp = buffer().params();
627 row_type const row = cellRow(cell);
629 row_vector::iterator rit = row_info.begin() + row;
630 row_info.insert(rit, RowData());
631 // now set the values of the row before
632 row_info[row] = row_info[row + 1];
634 row_type const nrows = rowCount();
635 col_type const ncols = columnCount();
637 cell_vvector old(nrows - 1);
638 for (row_type i = 0; i < nrows - 1; ++i)
639 swap(cell_info[i], old[i]);
641 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer())));
643 for (row_type i = 0; i <= row; ++i)
644 swap(cell_info[i], old[i]);
645 for (row_type i = row + 2; i < nrows; ++i)
646 swap(cell_info[i], old[i - 1]);
649 for (col_type j = 0; j < ncols; ++j)
650 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
656 void Tabular::deleteRow(row_type const row)
658 // Not allowed to delete last row
662 row_info.erase(row_info.begin() + row);
663 cell_info.erase(cell_info.begin() + row);
668 void Tabular::copyRow(row_type const row)
670 row_info.insert(row_info.begin() + row, row_info[row]);
671 cell_info.insert(cell_info.begin() + row, cell_info[row]);
673 if (buffer().params().trackChanges)
674 for (col_type j = 0; j < columnCount(); ++j)
675 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
681 void Tabular::appendColumn(idx_type const cell)
683 col_type const column = cellColumn(cell);
684 col_type const ncols = columnCount();
685 column_vector::iterator cit = column_info.begin() + column + 1;
686 column_info.insert(cit, ColumnData());
687 // set the column values of the column before
688 column_info[column + 1] = column_info[column];
690 BufferParams const & bp = buffer().params();
691 for (row_type i = 0; i < rowCount(); ++i) {
692 cell_info[i].insert(cell_info[i].begin() + column + 1, CellData(buffer()));
694 // care about multicolumns
695 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
696 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
698 if (column + 2 >= ncols
699 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
700 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
703 for (row_type i = 0; i < rowCount(); ++i) {
704 cell_info[i][column + 1].inset->clear();
706 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
712 void Tabular::deleteColumn(col_type const column)
714 // Not allowed to delete last column
715 if (columnCount() == 1)
718 column_info.erase(column_info.begin() + column);
719 for (row_type i = 0; i < rowCount(); ++i) {
720 // Care about multicolumn cells
721 if (column + 1 < columnCount() &&
722 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
723 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
724 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
726 cell_info[i].erase(cell_info[i].begin() + column);
732 void Tabular::copyColumn(col_type const column)
734 BufferParams const & bp = buffer().params();
735 column_info.insert(column_info.begin() + column, column_info[column]);
737 for (row_type i = 0; i < rowCount(); ++i)
738 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
741 for (row_type i = 0; i < rowCount(); ++i)
742 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
747 void Tabular::updateIndexes()
750 // Count only non-multicol cells plus begin multicol
751 // cells, ignore non-begin multicol cells:
752 for (row_type row = 0; row < rowCount(); ++row) {
753 for (col_type column = 0; column < columnCount(); ++column) {
754 if (cell_info[row][column].multicolumn
755 != Tabular::CELL_PART_OF_MULTICOLUMN)
757 BOOST_ASSERT(numberofcells != 0);
758 cell_info[row][column].cellno =
763 rowofcell.resize(numberofcells);
764 columnofcell.resize(numberofcells);
769 c < numberofcells && row < rowCount() && column < columnCount();) {
771 columnofcell[c] = column;
775 } while (column < columnCount() &&
776 cell_info[row][column].multicolumn
777 == Tabular::CELL_PART_OF_MULTICOLUMN);
779 if (column == columnCount()) {
785 for (row_type row = 0; row < rowCount(); ++row) {
786 for (col_type column = 0; column < columnCount(); ++column) {
787 if (isPartOfMultiColumn(row,column))
789 cell_info[row][column].inset->setAutoBreakRows(
790 !getPWidth(cellIndex(row, column)).zero());
796 Tabular::idx_type Tabular::cellCount() const
798 return numberofcells;
802 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
804 row_type const row = cellRow(cell);
806 for (col_type i = 0; i < columnCount(); ++i)
807 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
813 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
815 if (!wholerow && isMultiColumn(cell) &&
816 !(use_booktabs && cellRow(cell) == 0))
817 return cellinfo_of_cell(cell).top_line;
818 return row_info[cellRow(cell)].top_line;
822 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
824 if (!wholerow && isMultiColumn(cell) &&
825 !(use_booktabs && isLastRow(cell)))
826 return cellinfo_of_cell(cell).bottom_line;
827 return row_info[cellRow(cell)].bottom_line;
831 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
835 if (!wholecolumn && isMultiColumn(cell) &&
836 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
837 return cellinfo_of_cell(cell).left_line;
838 return column_info[cellColumn(cell)].left_line;
842 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
846 if (!wholecolumn && isMultiColumn(cell) &&
847 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
848 return cellinfo_of_cell(cell).right_line;
849 return column_info[cellRightColumn(cell)].right_line;
853 bool Tabular::topAlreadyDrawn(idx_type cell) const
855 row_type row = cellRow(cell);
856 if (row > 0 && !getAdditionalHeight(row)) {
857 col_type column = cellColumn(cell);
860 && cell_info[row][column].multicolumn
861 == Tabular::CELL_PART_OF_MULTICOLUMN)
863 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
864 return row_info[row].bottom_line;
866 return cell_info[row][column].bottom_line;
872 bool Tabular::leftAlreadyDrawn(idx_type cell) const
874 col_type column = cellColumn(cell);
876 row_type row = cellRow(cell);
878 (cell_info[row][column].multicolumn ==
879 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
880 if (getAdditionalWidth(cell_info[row][column].cellno))
882 return rightLine(cell_info[row][column].cellno);
888 bool Tabular::isLastRow(idx_type cell) const
890 return cellRow(cell) == rowCount() - 1;
894 int Tabular::getAdditionalHeight(row_type row) const
896 if (!row || row >= rowCount())
902 for (col_type column = 0; column < columnCount() && bottom; ++column) {
903 switch (cell_info[row - 1][column].multicolumn) {
904 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
905 bottom = cell_info[row - 1][column].bottom_line;
907 case Tabular::CELL_NORMAL:
908 bottom = row_info[row - 1].bottom_line;
911 for (col_type column = 0; column < columnCount() && top; ++column) {
912 switch (cell_info[row][column].multicolumn) {
913 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
914 top = cell_info[row][column].top_line;
916 case Tabular::CELL_NORMAL:
917 top = row_info[row].top_line;
920 int const interline_space = row_info[row - 1].interline_space_default ?
922 row_info[row - 1].interline_space.inPixels(width());
924 return interline_space + WIDTH_OF_LINE;
925 return interline_space;
929 int Tabular::getAdditionalWidth(idx_type cell) const
931 // internally already set in setCellWidth
932 // used to get it back in text.cpp
933 col_type const col = cellRightColumn(cell);
934 row_type const row = cellRow(cell);
935 if (col < columnCount() - 1 && rightLine(cell) &&
936 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
938 return WIDTH_OF_LINE;
944 // returns the maximum over all rows
945 int Tabular::columnWidth(idx_type cell) const
947 col_type const column1 = cellColumn(cell);
948 col_type const column2 = cellRightColumn(cell);
950 for (col_type i = column1; i <= column2; ++i)
951 result += column_info[i].width;
956 int Tabular::width() const
959 for (col_type i = 0; i < columnCount(); ++i)
960 width += column_info[i].width;
965 // returns true if a complete update is necessary, otherwise false
966 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
968 if (!isMultiColumn(cell))
971 row_type const row = cellRow(cell);
972 col_type const column1 = cellColumn(cell);
973 col_type const column2 = cellRightColumn(cell);
974 int const old_val = cell_info[row][column2].width;
976 // first set columns to 0 so we can calculate the right width
977 for (col_type i = column1; i <= column2; ++i) {
978 cell_info[row][i].width = 0;
980 // set the width to MAX_WIDTH until width > 0
981 int width = new_width + 2 * WIDTH_OF_LINE;
982 col_type i = column1;
983 for (; i < column2 && width > column_info[i].width; ++i) {
984 cell_info[row][i].width = column_info[i].width;
985 width -= column_info[i].width;
988 cell_info[row][i].width = width;
990 if (old_val != cell_info[row][column2].width) {
991 // in this case we have to recalculate all multicolumn cells which
992 // have this column as one of theirs but not as last one
993 calculate_width_of_column_NMC(i);
994 recalculateMulticolumnsOfColumn(i);
995 calculate_width_of_column(i);
1001 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1003 // the last column does not have to be recalculated because all
1004 // multicolumns will have here there last multicolumn cell which
1005 // always will have the whole rest of the width of the cell.
1006 if (columnCount() < 2 || column > (columnCount() - 2))
1008 for (row_type row = 0; row < rowCount(); ++row) {
1009 int mc = cell_info[row][column].multicolumn;
1010 int nmc = cell_info[row][column+1].multicolumn;
1011 // we only have to update multicolumns which do not have this
1012 // column as their last column!
1013 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1014 (mc == CELL_PART_OF_MULTICOLUMN &&
1015 nmc == CELL_PART_OF_MULTICOLUMN))
1017 idx_type const cellno = cell_info[row][column].cellno;
1018 setWidthOfMulticolCell(cellno,
1019 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1025 void Tabular::setCellWidth(idx_type cell, int new_width)
1027 row_type const row = cellRow(cell);
1028 col_type const column1 = cellColumn(cell);
1033 if (rightLine(cell_info[row][column1].cellno, true) &&
1034 column1 < columnCount() - 1 &&
1035 leftLine(cell_info[row][column1+1].cellno, true))
1037 add_width = WIDTH_OF_LINE;
1040 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1043 if (isMultiColumnReal(cell)) {
1044 tmp = setWidthOfMulticolCell(cell, new_width);
1046 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1047 cell_info[row][column1].width = width;
1048 tmp = calculate_width_of_column_NMC(column1);
1050 recalculateMulticolumnsOfColumn(column1);
1053 for (col_type i = 0; i < columnCount(); ++i)
1054 calculate_width_of_column(i);
1059 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1062 if (!isMultiColumn(cell) || onlycolumn)
1063 column_info[cellColumn(cell)].alignment = align;
1065 cellinfo_of_cell(cell).alignment = align;
1069 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1072 if (!isMultiColumn(cell) || onlycolumn)
1073 column_info[cellColumn(cell)].valignment = align;
1075 cellinfo_of_cell(cell).valignment = align;
1082 * Allow line and paragraph breaks for fixed width cells or disallow them,
1083 * merge cell paragraphs and reset layout to standard for variable width
1086 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1088 inset->setAutoBreakRows(fixedWidth);
1092 // merge all paragraphs to one
1093 BufferParams const & bp = cur.bv().buffer().params();
1094 while (inset->paragraphs().size() > 1)
1095 mergeParagraph(bp, inset->paragraphs(), 0);
1099 // undo information has already been recorded
1100 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1101 bp.documentClass().emptyLayoutName());
1108 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1109 Length const & width)
1111 col_type const j = cellColumn(cell);
1113 column_info[j].p_width = width;
1114 for (row_type i = 0; i < rowCount(); ++i) {
1115 idx_type const cell = cellIndex(i, j);
1116 // because of multicolumns
1117 toggleFixedWidth(cur, getCellInset(cell).get(),
1118 !getPWidth(cell).zero());
1120 // cur paragraph can become invalid after paragraphs were merged
1121 if (cur.pit() > cur.lastpit())
1122 cur.pit() = cur.lastpit();
1123 // cur position can become invalid after newlines were removed
1124 if (cur.pos() > cur.lastpos())
1125 cur.pos() = cur.lastpos();
1129 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1130 Length const & width)
1132 if (!isMultiColumn(cell))
1135 cellinfo_of_cell(cell).p_width = width;
1136 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1137 // cur paragraph can become invalid after paragraphs were merged
1138 if (cur.pit() > cur.lastpit())
1139 cur.pit() = cur.lastpit();
1140 // cur position can become invalid after newlines were removed
1141 if (cur.pos() > cur.lastpos())
1142 cur.pos() = cur.lastpos();
1147 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1148 Tabular::Feature what)
1150 if (what == SET_SPECIAL_MULTI)
1151 cellinfo_of_cell(cell).align_special = special;
1153 column_info[cellColumn(cell)].align_special = special;
1157 void Tabular::setAllLines(idx_type cell, bool line)
1159 setTopLine(cell, line);
1160 setBottomLine(cell, line);
1161 setRightLine(cell, line);
1162 setLeftLine(cell, line);
1166 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1168 row_type const row = cellRow(cell);
1169 if (wholerow || !isMultiColumn(cell))
1170 row_info[row].top_line = line;
1172 cellinfo_of_cell(cell).top_line = line;
1176 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1178 if (wholerow || !isMultiColumn(cell))
1179 row_info[cellRow(cell)].bottom_line = line;
1181 cellinfo_of_cell(cell).bottom_line = line;
1185 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1187 if (wholecolumn || !isMultiColumn(cell))
1188 column_info[cellColumn(cell)].left_line = line;
1190 cellinfo_of_cell(cell).left_line = line;
1194 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1196 if (wholecolumn || !isMultiColumn(cell))
1197 column_info[cellRightColumn(cell)].right_line = line;
1199 cellinfo_of_cell(cell).right_line = line;
1203 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1205 if (!onlycolumn && isMultiColumn(cell))
1206 return cellinfo_of_cell(cell).alignment;
1207 return column_info[cellColumn(cell)].alignment;
1212 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1214 if (!onlycolumn && isMultiColumn(cell))
1215 return cellinfo_of_cell(cell).valignment;
1216 return column_info[cellColumn(cell)].valignment;
1220 Length const Tabular::getPWidth(idx_type cell) const
1222 if (isMultiColumn(cell))
1223 return cellinfo_of_cell(cell).p_width;
1224 return column_info[cellColumn(cell)].p_width;
1228 Length const Tabular::getColumnPWidth(idx_type cell) const
1230 return column_info[cellColumn(cell)].p_width;
1234 Length const Tabular::getMColumnPWidth(idx_type cell) const
1236 if (isMultiColumn(cell))
1237 return cellinfo_of_cell(cell).p_width;
1242 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1244 if (what == SET_SPECIAL_MULTI)
1245 return cellinfo_of_cell(cell).align_special;
1246 return column_info[cellColumn(cell)].align_special;
1250 int Tabular::cellWidth(idx_type cell) const
1252 row_type const row = cellRow(cell);
1253 col_type const column1 = cellColumn(cell);
1254 col_type const column2 = cellRightColumn(cell);
1256 for (col_type i = column1; i <= column2; ++i)
1257 result += cell_info[row][i].width;
1262 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1266 switch (getAlignment(cell)) {
1267 case LYX_ALIGN_CENTER:
1268 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1270 case LYX_ALIGN_RIGHT:
1271 x += columnWidth(cell) - cellWidth(cell);
1272 // + getAdditionalWidth(cell);
1275 // LYX_ALIGN_LEFT: nothing :-)
1279 // the LaTeX Way :-(
1285 bool Tabular::isFirstCellInRow(idx_type cell) const
1287 return cellColumn(cell) == 0;
1291 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1293 if (row > rowCount() - 1)
1294 row = rowCount() - 1;
1295 return cell_info[row][0].cellno;
1299 bool Tabular::isLastCellInRow(idx_type cell) const
1301 return cellRightColumn(cell) == columnCount() - 1;
1305 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1307 if (row > rowCount() - 1)
1308 row = rowCount() - 1;
1309 return cell_info[row][columnCount() - 1].cellno;
1313 void Tabular::calculate_width_of_column(col_type column)
1316 for (row_type i = 0; i < rowCount(); ++i)
1317 maximum = max(cell_info[i][column].width, maximum);
1318 column_info[column].width = maximum;
1323 // Calculate the columns regarding ONLY the normal cells and if this
1324 // column is inside a multicolumn cell then use it only if its the last
1325 // column of this multicolumn cell as this gives an added width to the
1326 // column, all the rest should be adapted!
1328 bool Tabular::calculate_width_of_column_NMC(col_type column)
1330 int const old_column_width = column_info[column].width;
1332 for (row_type i = 0; i < rowCount(); ++i) {
1333 idx_type cell = cellIndex(i, column);
1334 bool ismulti = isMultiColumnReal(cell);
1335 if ((!ismulti || column == cellRightColumn(cell)) &&
1336 cell_info[i][column].width > max)
1338 max = cell_info[i][column].width;
1341 column_info[column].width = max;
1342 return column_info[column].width != old_column_width;
1346 Tabular::row_type Tabular::cellRow(idx_type cell) const
1348 if (cell >= cellCount())
1349 return rowCount() - 1;
1352 return rowofcell[cell];
1356 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1358 if (cell >= cellCount())
1359 return columnCount() - 1;
1362 return columnofcell[cell];
1366 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1368 row_type const row = cellRow(cell);
1369 col_type column = cellColumn(cell);
1370 while (column < columnCount() - 1 &&
1371 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1377 void Tabular::write(ostream & os) const
1381 << write_attribute("version", 3)
1382 << write_attribute("rows", rowCount())
1383 << write_attribute("columns", columnCount())
1385 // global longtable options
1387 << write_attribute("rotate", rotate)
1388 << write_attribute("booktabs", use_booktabs)
1389 << write_attribute("islongtable", is_long_tabular)
1390 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1391 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1392 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1393 << write_attribute("headTopDL", endhead.topDL)
1394 << write_attribute("headBottomDL", endhead.bottomDL)
1395 << write_attribute("footTopDL", endfoot.topDL)
1396 << write_attribute("footBottomDL", endfoot.bottomDL)
1397 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1398 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1399 << write_attribute("lastFootEmpty", endlastfoot.empty)
1401 for (col_type j = 0; j < columnCount(); ++j) {
1403 << write_attribute("alignment", column_info[j].alignment)
1404 << write_attribute("valignment", column_info[j].valignment)
1405 << write_attribute("leftline", column_info[j].left_line)
1406 << write_attribute("rightline", column_info[j].right_line)
1407 << write_attribute("width", column_info[j].p_width.asString())
1408 << write_attribute("special", column_info[j].align_special)
1411 for (row_type i = 0; i < rowCount(); ++i) {
1413 << write_attribute("topline", row_info[i].top_line)
1414 << write_attribute("bottomline", row_info[i].bottom_line);
1415 static const string def("default");
1416 if (row_info[i].top_space_default)
1417 os << write_attribute("topspace", def);
1419 os << write_attribute("topspace", row_info[i].top_space);
1420 if (row_info[i].bottom_space_default)
1421 os << write_attribute("bottomspace", def);
1423 os << write_attribute("bottomspace", row_info[i].bottom_space);
1424 if (row_info[i].interline_space_default)
1425 os << write_attribute("interlinespace", def);
1427 os << write_attribute("interlinespace", row_info[i].interline_space);
1428 os << write_attribute("endhead", row_info[i].endhead)
1429 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1430 << write_attribute("endfoot", row_info[i].endfoot)
1431 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1432 << write_attribute("newpage", row_info[i].newpage)
1434 for (col_type j = 0; j < columnCount(); ++j) {
1436 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1437 << write_attribute("alignment", cell_info[i][j].alignment)
1438 << write_attribute("valignment", cell_info[i][j].valignment)
1439 << write_attribute("topline", cell_info[i][j].top_line)
1440 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1441 << write_attribute("leftline", cell_info[i][j].left_line)
1442 << write_attribute("rightline", cell_info[i][j].right_line)
1443 << write_attribute("rotate", cell_info[i][j].rotate)
1444 << write_attribute("usebox", cell_info[i][j].usebox)
1445 << write_attribute("width", cell_info[i][j].p_width)
1446 << write_attribute("special", cell_info[i][j].align_special)
1448 os << "\\begin_inset ";
1449 cell_info[i][j].inset->write(os);
1450 os << "\n\\end_inset\n"
1455 os << "</lyxtabular>\n";
1459 void Tabular::read(Lexer & lex)
1462 istream & is = lex.getStream();
1464 l_getline(is, line);
1465 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1466 BOOST_ASSERT(false);
1471 if (!getTokenValue(line, "version", version))
1473 BOOST_ASSERT(version >= 2);
1476 if (!getTokenValue(line, "rows", rows_arg))
1479 if (!getTokenValue(line, "columns", columns_arg))
1481 init(buffer(), rows_arg, columns_arg);
1482 l_getline(is, line);
1483 if (!prefixIs(line, "<features")) {
1484 lyxerr << "Wrong tabular format (expected <features ...> got"
1485 << line << ')' << endl;
1488 getTokenValue(line, "rotate", rotate);
1489 getTokenValue(line, "booktabs", use_booktabs);
1490 getTokenValue(line, "islongtable", is_long_tabular);
1491 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1492 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1493 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1494 getTokenValue(line, "headTopDL", endhead.topDL);
1495 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1496 getTokenValue(line, "footTopDL", endfoot.topDL);
1497 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1498 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1499 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1500 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1502 for (col_type j = 0; j < columnCount(); ++j) {
1504 if (!prefixIs(line,"<column")) {
1505 lyxerr << "Wrong tabular format (expected <column ...> got"
1506 << line << ')' << endl;
1509 getTokenValue(line, "alignment", column_info[j].alignment);
1510 getTokenValue(line, "valignment", column_info[j].valignment);
1511 getTokenValue(line, "leftline", column_info[j].left_line);
1512 getTokenValue(line, "rightline", column_info[j].right_line);
1513 getTokenValue(line, "width", column_info[j].p_width);
1514 getTokenValue(line, "special", column_info[j].align_special);
1517 for (row_type i = 0; i < rowCount(); ++i) {
1518 l_getline(is, line);
1519 if (!prefixIs(line, "<row")) {
1520 lyxerr << "Wrong tabular format (expected <row ...> got"
1521 << line << ')' << endl;
1524 getTokenValue(line, "topline", row_info[i].top_line);
1525 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1526 getTokenValue(line, "topspace", row_info[i].top_space,
1527 row_info[i].top_space_default);
1528 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1529 row_info[i].bottom_space_default);
1530 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1531 row_info[i].interline_space_default);
1532 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1533 getTokenValue(line, "endhead", row_info[i].endhead);
1534 getTokenValue(line, "endfoot", row_info[i].endfoot);
1535 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1536 getTokenValue(line, "newpage", row_info[i].newpage);
1537 for (col_type j = 0; j < columnCount(); ++j) {
1538 l_getline(is, line);
1539 if (!prefixIs(line, "<cell")) {
1540 lyxerr << "Wrong tabular format (expected <cell ...> got"
1541 << line << ')' << endl;
1544 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1545 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1546 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1547 getTokenValue(line, "topline", cell_info[i][j].top_line);
1548 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1549 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1550 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1551 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1552 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1553 getTokenValue(line, "width", cell_info[i][j].p_width);
1554 getTokenValue(line, "special", cell_info[i][j].align_special);
1555 l_getline(is, line);
1556 if (prefixIs(line, "\\begin_inset")) {
1557 cell_info[i][j].inset->read(lex);
1558 l_getline(is, line);
1560 if (!prefixIs(line, "</cell>")) {
1561 lyxerr << "Wrong tabular format (expected </cell> got"
1562 << line << ')' << endl;
1566 l_getline(is, line);
1567 if (!prefixIs(line, "</row>")) {
1568 lyxerr << "Wrong tabular format (expected </row> got"
1569 << line << ')' << endl;
1573 while (!prefixIs(line, "</lyxtabular>")) {
1574 l_getline(is, line);
1580 bool Tabular::isMultiColumn(idx_type cell) const
1582 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1586 bool Tabular::isMultiColumnReal(idx_type cell) const
1588 return cellColumn(cell) != cellRightColumn(cell) &&
1589 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1593 Tabular::CellData & Tabular::cellinfo_of_cell(idx_type cell) const
1595 return cell_info[cellRow(cell)][cellColumn(cell)];
1599 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1601 CellData & cs = cellinfo_of_cell(cell);
1602 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1603 cs.alignment = column_info[cellColumn(cell)].alignment;
1604 cs.top_line = row_info[cellRow(cell)].top_line;
1605 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1606 cs.left_line = column_info[cellColumn(cell)].left_line;
1607 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1608 for (idx_type i = 1; i < number; ++i) {
1609 CellData & cs1 = cellinfo_of_cell(cell + i);
1610 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1611 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1618 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1620 row_type const row = cellRow(cell);
1621 col_type column = cellColumn(cell);
1622 idx_type result = 1;
1624 while (column < columnCount() &&
1625 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1634 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1636 row_type const row = cellRow(cell);
1637 col_type column = cellColumn(cell);
1639 idx_type result = 0;
1641 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1642 cell_info[row][column].multicolumn = CELL_NORMAL;
1644 while (column < columnCount() &&
1645 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1647 cell_info[row][column].multicolumn = CELL_NORMAL;
1657 void Tabular::setBookTabs(bool what)
1659 use_booktabs = what;
1663 bool Tabular::useBookTabs() const
1665 return use_booktabs;
1669 void Tabular::setLongTabular(bool what)
1671 is_long_tabular = what;
1675 bool Tabular::isLongTabular() const
1677 return is_long_tabular;
1681 void Tabular::setRotateTabular(bool flag)
1687 bool Tabular::getRotateTabular() const
1693 void Tabular::setRotateCell(idx_type cell, bool flag)
1695 cellinfo_of_cell(cell).rotate = flag;
1699 bool Tabular::getRotateCell(idx_type cell) const
1701 return cellinfo_of_cell(cell).rotate;
1705 bool Tabular::needRotating() const
1709 for (row_type i = 0; i < rowCount(); ++i)
1710 for (col_type j = 0; j < columnCount(); ++j)
1711 if (cell_info[i][j].rotate)
1717 bool Tabular::isLastCell(idx_type cell) const
1719 if (cell + 1 < cellCount())
1725 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1727 if (cellRow(cell) > 0)
1728 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1733 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1735 if (cellRow(cell) + 1 < rowCount())
1736 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1741 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1743 if (cellRow(cell) == 0)
1745 if (!isMultiColumn(cell))
1746 return getCellAbove(cell);
1747 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1751 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1753 if (cellRow(cell) + 1 >= rowCount())
1755 if (!isMultiColumn(cell))
1756 return getCellBelow(cell);
1757 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1761 Tabular::idx_type Tabular::cellIndex(row_type row,
1762 col_type column) const
1764 BOOST_ASSERT(column != npos && column < columnCount() &&
1765 row != npos && row < rowCount());
1766 return cell_info[row][column].cellno;
1770 void Tabular::setUsebox(idx_type cell, BoxType type)
1772 cellinfo_of_cell(cell).usebox = type;
1776 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1778 if (column_info[cellColumn(cell)].p_width.zero() &&
1779 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1781 if (cellinfo_of_cell(cell).usebox > 1)
1782 return cellinfo_of_cell(cell).usebox;
1783 return useParbox(cell);
1788 // This are functions used for the longtable support
1790 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1796 row_info[row].endfirsthead = flag;
1800 row_info[row].endhead = flag;
1805 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1808 hd.set = haveLTHead();
1809 return row_info[row].endhead;
1813 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1816 hd.set = haveLTFirstHead();
1817 return row_info[row].endfirsthead;
1821 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1827 row_info[row].endlastfoot = flag;
1831 row_info[row].endfoot = flag;
1836 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1839 fd.set = haveLTFoot();
1840 return row_info[row].endfoot;
1844 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1847 fd.set = haveLTLastFoot();
1848 return row_info[row].endlastfoot;
1852 void Tabular::setLTNewPage(row_type row, bool what)
1854 row_info[row].newpage = what;
1858 bool Tabular::getLTNewPage(row_type row) const
1860 return row_info[row].newpage;
1864 bool Tabular::haveLTHead() const
1866 for (row_type i = 0; i < rowCount(); ++i)
1867 if (row_info[i].endhead)
1873 bool Tabular::haveLTFirstHead() const
1875 if (endfirsthead.empty)
1877 for (row_type i = 0; i < rowCount(); ++i)
1878 if (row_info[i].endfirsthead)
1884 bool Tabular::haveLTFoot() const
1886 for (row_type i = 0; i < rowCount(); ++i)
1887 if (row_info[i].endfoot)
1893 bool Tabular::haveLTLastFoot() const
1895 if (endlastfoot.empty)
1897 for (row_type i = 0; i < rowCount(); ++i)
1898 if (row_info[i].endlastfoot)
1904 // end longtable support functions
1906 void Tabular::setRowAscent(row_type row, int height)
1908 if (row >= rowCount() || row_info[row].ascent == height)
1910 row_info[row].ascent = height;
1914 void Tabular::setRowDescent(row_type row, int height)
1916 if (row >= rowCount() || row_info[row].descent == height)
1918 row_info[row].descent = height;
1922 int Tabular::rowAscent(row_type row) const
1924 if (row >= rowCount())
1926 return row_info[row].ascent;
1930 int Tabular::rowDescent(row_type row) const
1932 BOOST_ASSERT(row < rowCount());
1933 return row_info[row].descent;
1937 int Tabular::height() const
1940 for (row_type row = 0; row < rowCount(); ++row)
1941 height += rowAscent(row) + rowDescent(row) +
1942 getAdditionalHeight(row);
1947 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1949 BOOST_ASSERT(row < rowCount());
1950 BOOST_ASSERT(column < columnCount());
1951 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1955 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1957 // FIXME: assert or return 0 as in TeXBottomHLine()?
1958 BOOST_ASSERT(row != npos);
1959 BOOST_ASSERT(row < rowCount());
1961 idx_type const fcell = getFirstCellInRow(row);
1962 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1965 for (idx_type i = fcell; i < n; ++i) {
1969 if (use_booktabs && row == 0) {
1972 } else if (tmp == n - fcell) {
1973 os << (use_booktabs ? "\\midrule " : "\\hline ");
1975 for (idx_type i = fcell; i < n; ++i) {
1977 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1978 << cellColumn(i) + 1
1980 << cellRightColumn(i) + 1
1992 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1994 // FIXME: return 0 or assert as in TeXTopHLine()?
1995 if (row == npos || row >= rowCount())
1998 idx_type const fcell = getFirstCellInRow(row);
1999 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2002 for (idx_type i = fcell; i < n; ++i) {
2006 if (use_booktabs && row == rowCount() - 1) {
2007 if (bottomLine(fcell))
2008 os << "\\bottomrule";
2009 } else if (tmp == n - fcell) {
2010 os << (use_booktabs ? "\\midrule" : "\\hline");
2012 for (idx_type i = fcell; i < n; ++i) {
2013 if (bottomLine(i)) {
2014 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2015 << cellColumn(i) + 1
2017 << cellRightColumn(i) + 1
2029 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2033 if (getRotateCell(cell)) {
2034 os << "\\begin{sideways}\n";
2037 if (isMultiColumn(cell)) {
2038 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2039 if (leftLine(cell) &&
2040 (isFirstCellInRow(cell) ||
2041 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2042 !rightLine(cell - 1, true))))
2044 if (!cellinfo_of_cell(cell).align_special.empty()) {
2045 os << cellinfo_of_cell(cell).align_special;
2047 if (!getPWidth(cell).zero()) {
2048 switch (getVAlignment(cell)) {
2049 case LYX_VALIGN_TOP:
2052 case LYX_VALIGN_MIDDLE:
2055 case LYX_VALIGN_BOTTOM:
2060 << from_ascii(getPWidth(cell).asLatexString())
2063 switch (getAlignment(cell)) {
2064 case LYX_ALIGN_LEFT:
2067 case LYX_ALIGN_RIGHT:
2074 } // end if else !getPWidth
2075 } // end if else !cellinfo_of_cell
2076 if (rightLine(cell))
2078 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2083 if (getUsebox(cell) == BOX_PARBOX) {
2085 switch (getVAlignment(cell)) {
2086 case LYX_VALIGN_TOP:
2089 case LYX_VALIGN_MIDDLE:
2092 case LYX_VALIGN_BOTTOM:
2096 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2098 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2099 os << "\\begin{minipage}[";
2100 switch (getVAlignment(cell)) {
2101 case LYX_VALIGN_TOP:
2104 case LYX_VALIGN_MIDDLE:
2107 case LYX_VALIGN_BOTTOM:
2111 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2119 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2124 if (getUsebox(cell) == BOX_PARBOX)
2126 else if (getUsebox(cell) == BOX_MINIPAGE) {
2127 os << "%\n\\end{minipage}";
2130 if (isMultiColumn(cell)) {
2133 if (getRotateCell(cell)) {
2134 os << "%\n\\end{sideways}";
2141 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2142 OutputParams const & runparams) const
2144 if (!is_long_tabular)
2148 // output header info
2150 if (endhead.topDL) {
2154 for (row_type i = 0; i < rowCount(); ++i) {
2155 if (row_info[i].endhead) {
2156 ret += TeXRow(os, i, runparams);
2159 if (endhead.bottomDL) {
2163 os << "\\endhead\n";
2165 if (endfirsthead.empty) {
2166 os << "\\endfirsthead\n";
2170 // output firstheader info
2171 if (haveLTFirstHead()) {
2172 if (endfirsthead.topDL) {
2176 for (row_type i = 0; i < rowCount(); ++i) {
2177 if (row_info[i].endfirsthead) {
2178 ret += TeXRow(os, i, runparams);
2181 if (endfirsthead.bottomDL) {
2185 os << "\\endfirsthead\n";
2188 // output footer info
2190 if (endfoot.topDL) {
2194 for (row_type i = 0; i < rowCount(); ++i) {
2195 if (row_info[i].endfoot) {
2196 ret += TeXRow(os, i, runparams);
2199 if (endfoot.bottomDL) {
2203 os << "\\endfoot\n";
2205 if (endlastfoot.empty) {
2206 os << "\\endlastfoot\n";
2210 // output lastfooter info
2211 if (haveLTLastFoot()) {
2212 if (endlastfoot.topDL) {
2216 for (row_type i = 0; i < rowCount(); ++i) {
2217 if (row_info[i].endlastfoot) {
2218 ret += TeXRow(os, i, runparams);
2221 if (endlastfoot.bottomDL) {
2225 os << "\\endlastfoot\n";
2232 bool Tabular::isValidRow(row_type row) const
2234 if (!is_long_tabular)
2236 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2237 !row_info[row].endfoot && !row_info[row].endlastfoot;
2241 int Tabular::TeXRow(odocstream & os, row_type i,
2242 OutputParams const & runparams) const
2244 idx_type cell = cellIndex(i, 0);
2245 int ret = TeXTopHLine(os, i);
2246 if (row_info[i].top_space_default) {
2248 os << "\\addlinespace\n";
2250 os << "\\noalign{\\vskip\\doublerulesep}\n";
2252 } else if(!row_info[i].top_space.zero()) {
2254 os << "\\addlinespace["
2255 << from_ascii(row_info[i].top_space.asLatexString())
2258 os << "\\noalign{\\vskip"
2259 << from_ascii(row_info[i].top_space.asLatexString())
2265 for (col_type j = 0; j < columnCount(); ++j) {
2266 if (isPartOfMultiColumn(i, j))
2268 ret += TeXCellPreamble(os, cell);
2269 shared_ptr<InsetText> inset = getCellInset(cell);
2271 Paragraph const & par = inset->paragraphs().front();
2272 bool rtl = par.isRTL(buffer().params())
2274 && getPWidth(cell).zero();
2277 if (par.getParLanguage(buffer().params())->lang() ==
2280 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2282 // currently, remaning RTL languages are arabic_arabtex and hebrew
2286 ret += inset->latex(os, runparams);
2290 ret += TeXCellPostamble(os, cell);
2291 if (!isLastCellInRow(cell)) { // not last cell in row
2296 os << "\\tabularnewline";
2297 if (row_info[i].bottom_space_default) {
2299 os << "\\addlinespace";
2301 os << "[\\doublerulesep]";
2302 } else if (!row_info[i].bottom_space.zero()) {
2304 os << "\\addlinespace";
2306 << from_ascii(row_info[i].bottom_space.asLatexString())
2311 ret += TeXBottomHLine(os, i);
2312 if (row_info[i].interline_space_default) {
2314 os << "\\addlinespace\n";
2316 os << "\\noalign{\\vskip\\doublerulesep}\n";
2318 } else if (!row_info[i].interline_space.zero()) {
2320 os << "\\addlinespace["
2321 << from_ascii(row_info[i].interline_space.asLatexString())
2324 os << "\\noalign{\\vskip"
2325 << from_ascii(row_info[i].interline_space.asLatexString())
2333 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2337 //+---------------------------------------------------------------------
2338 //+ first the opening preamble +
2339 //+---------------------------------------------------------------------
2342 os << "\\begin{sideways}\n";
2345 if (is_long_tabular)
2346 os << "\\begin{longtable}{";
2348 os << "\\begin{tabular}{";
2349 for (col_type i = 0; i < columnCount(); ++i) {
2350 if (!use_booktabs && column_info[i].left_line)
2352 if (!column_info[i].align_special.empty()) {
2353 os << column_info[i].align_special;
2355 if (!column_info[i].p_width.zero()) {
2356 switch (column_info[i].alignment) {
2357 case LYX_ALIGN_LEFT:
2358 os << ">{\\raggedright}";
2360 case LYX_ALIGN_RIGHT:
2361 os << ">{\\raggedleft}";
2363 case LYX_ALIGN_CENTER:
2364 os << ">{\\centering}";
2366 case LYX_ALIGN_NONE:
2367 case LYX_ALIGN_BLOCK:
2368 case LYX_ALIGN_LAYOUT:
2369 case LYX_ALIGN_SPECIAL:
2373 switch (column_info[i].valignment) {
2374 case LYX_VALIGN_TOP:
2377 case LYX_VALIGN_MIDDLE:
2380 case LYX_VALIGN_BOTTOM:
2385 << from_ascii(column_info[i].p_width.asLatexString())
2388 switch (column_info[i].alignment) {
2389 case LYX_ALIGN_LEFT:
2392 case LYX_ALIGN_RIGHT:
2399 } // end if else !column_info[i].p_width
2400 } // end if else !column_info[i].align_special
2401 if (!use_booktabs && column_info[i].right_line)
2407 ret += TeXLongtableHeaderFooter(os, runparams);
2409 //+---------------------------------------------------------------------
2410 //+ the single row and columns (cells) +
2411 //+---------------------------------------------------------------------
2413 for (row_type i = 0; i < rowCount(); ++i) {
2414 if (isValidRow(i)) {
2415 ret += TeXRow(os, i, runparams);
2416 if (is_long_tabular && row_info[i].newpage) {
2417 os << "\\newpage\n";
2423 //+---------------------------------------------------------------------
2424 //+ the closing of the tabular +
2425 //+---------------------------------------------------------------------
2427 if (is_long_tabular)
2428 os << "\\end{longtable}";
2430 os << "\\end{tabular}";
2432 os << "\n\\end{sideways}";
2440 int Tabular::docbookRow(odocstream & os, row_type row,
2441 OutputParams const & runparams) const
2444 idx_type cell = getFirstCellInRow(row);
2447 for (col_type j = 0; j < columnCount(); ++j) {
2448 if (isPartOfMultiColumn(row, j))
2451 os << "<entry align=\"";
2452 switch (getAlignment(cell)) {
2453 case LYX_ALIGN_LEFT:
2456 case LYX_ALIGN_RIGHT:
2464 os << "\" valign=\"";
2465 switch (getVAlignment(cell)) {
2466 case LYX_VALIGN_TOP:
2469 case LYX_VALIGN_BOTTOM:
2472 case LYX_VALIGN_MIDDLE:
2477 if (isMultiColumn(cell)) {
2478 os << " namest=\"col" << j << "\" ";
2479 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2483 ret += getCellInset(cell)->docbook(os, runparams);
2492 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2496 //+---------------------------------------------------------------------
2497 //+ first the opening preamble +
2498 //+---------------------------------------------------------------------
2500 os << "<tgroup cols=\"" << columnCount()
2501 << "\" colsep=\"1\" rowsep=\"1\">\n";
2503 for (col_type i = 0; i < columnCount(); ++i) {
2504 os << "<colspec colname=\"col" << i << "\" align=\"";
2505 switch (column_info[i].alignment) {
2506 case LYX_ALIGN_LEFT:
2509 case LYX_ALIGN_RIGHT:
2517 if (runparams.flavor == OutputParams::XML)
2523 //+---------------------------------------------------------------------
2524 //+ Long Tabular case +
2525 //+---------------------------------------------------------------------
2527 // output header info
2528 if (haveLTHead() || haveLTFirstHead()) {
2531 for (row_type i = 0; i < rowCount(); ++i) {
2532 if (row_info[i].endhead || row_info[i].endfirsthead) {
2533 ret += docbookRow(os, i, runparams);
2539 // output footer info
2540 if (haveLTFoot() || haveLTLastFoot()) {
2543 for (row_type i = 0; i < rowCount(); ++i) {
2544 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2545 ret += docbookRow(os, i, runparams);
2552 //+---------------------------------------------------------------------
2553 //+ the single row and columns (cells) +
2554 //+---------------------------------------------------------------------
2558 for (row_type i = 0; i < rowCount(); ++i) {
2559 if (isValidRow(i)) {
2560 ret += docbookRow(os, i, runparams);
2565 //+---------------------------------------------------------------------
2566 //+ the closing of the tabular +
2567 //+---------------------------------------------------------------------
2576 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2577 vector<unsigned int> const & clen) const
2579 idx_type const fcell = getFirstCellInRow(row);
2580 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2583 for (idx_type i = fcell; i < n; ++i) {
2593 for (idx_type i = fcell; i < n; ++i) {
2604 col_type column = cellColumn(i);
2605 int len = clen[column];
2606 while (column < columnCount() - 1
2607 && isPartOfMultiColumn(row, ++column))
2608 len += clen[column] + 4;
2609 os << docstring(len, ch);
2624 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2625 vector<unsigned int> const & clen) const
2627 idx_type const fcell = getFirstCellInRow(row);
2628 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2631 for (idx_type i = fcell; i < n; ++i) {
2632 if (bottomLine(i)) {
2641 for (idx_type i = fcell; i < n; ++i) {
2642 if (bottomLine(i)) {
2652 col_type column = cellColumn(i);
2653 int len = clen[column];
2654 while (column < columnCount() -1
2655 && isPartOfMultiColumn(row, ++column))
2656 len += clen[column] + 4;
2657 os << docstring(len, ch);
2658 if (bottomLine(i)) {
2672 void Tabular::plaintextPrintCell(odocstream & os,
2673 OutputParams const & runparams,
2674 idx_type cell, row_type row, col_type column,
2675 vector<unsigned int> const & clen,
2676 bool onlydata) const
2678 odocstringstream sstr;
2679 getCellInset(cell)->plaintext(sstr, runparams);
2691 unsigned int len1 = sstr.str().length();
2692 unsigned int len2 = clen[column];
2693 while (column < columnCount() -1
2694 && isPartOfMultiColumn(row, ++column))
2695 len2 += clen[column] + 4;
2698 switch (getAlignment(cell)) {
2700 case LYX_ALIGN_LEFT:
2703 case LYX_ALIGN_RIGHT:
2707 case LYX_ALIGN_CENTER:
2713 os << docstring(len1, ' ') << sstr.str()
2714 << docstring(len2, ' ');
2716 if (rightLine(cell))
2723 void Tabular::plaintext(odocstream & os,
2724 OutputParams const & runparams, int const depth,
2725 bool onlydata, char_type delim) const
2727 // first calculate the width of the single columns
2728 vector<unsigned int> clen(columnCount());
2731 // first all non (real) multicolumn cells!
2732 for (col_type j = 0; j < columnCount(); ++j) {
2734 for (row_type i = 0; i < rowCount(); ++i) {
2735 idx_type cell = cellIndex(i, j);
2736 if (isMultiColumnReal(cell))
2738 odocstringstream sstr;
2739 getCellInset(cell)->plaintext(sstr, runparams);
2740 if (clen[j] < sstr.str().length())
2741 clen[j] = sstr.str().length();
2744 // then all (real) multicolumn cells!
2745 for (col_type j = 0; j < columnCount(); ++j) {
2746 for (row_type i = 0; i < rowCount(); ++i) {
2747 idx_type cell = cellIndex(i, j);
2748 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2750 odocstringstream sstr;
2751 getCellInset(cell)->plaintext(sstr, runparams);
2752 int len = int(sstr.str().length());
2753 idx_type const n = cells_in_multicolumn(cell);
2754 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2756 if (len > int(clen[j + n - 1]))
2757 clen[j + n - 1] = len;
2762 for (row_type i = 0; i < rowCount(); ++i) {
2763 if (!onlydata && plaintextTopHLine(os, i, clen))
2764 os << docstring(depth * 2, ' ');
2765 for (col_type j = 0; j < columnCount(); ++j) {
2766 if (isPartOfMultiColumn(i, j))
2768 if (onlydata && j > 0)
2769 // we don't use operator<< for single UCS4 character.
2770 // see explanation in docstream.h
2772 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2777 os << docstring(depth * 2, ' ');
2778 if (plaintextBottomHLine(os, i, clen))
2779 os << docstring(depth * 2, ' ');
2785 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2787 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2791 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2792 col_type column) const
2794 return cell_info[row][column].inset;
2798 void Tabular::setCellInset(row_type row, col_type column,
2799 shared_ptr<InsetText> ins) const
2801 cell_info[row][column].inset = ins;
2806 Tabular::getCellFromInset(Inset const * inset) const
2808 // is this inset part of the tabular?
2810 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2811 BOOST_ASSERT(false);
2814 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2815 if (getCellInset(cell).get() == inset) {
2816 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2817 << "cell=" << cell);
2821 // We should have found a cell at this point
2822 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2823 << inset << " not found!" << endl;
2824 BOOST_ASSERT(false);
2830 void Tabular::validate(LaTeXFeatures & features) const
2832 features.require("NeedTabularnewline");
2834 features.require("booktabs");
2835 if (isLongTabular())
2836 features.require("longtable");
2838 features.require("rotating");
2839 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2840 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2841 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2842 features.require("array");
2843 getCellInset(cell)->validate(features);
2848 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2850 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2851 ParagraphList::const_iterator cit = parlist.begin();
2852 ParagraphList::const_iterator end = parlist.end();
2854 for (; cit != end; ++cit)
2855 for (int i = 0; i < cit->size(); ++i)
2856 if (cit->isNewline(i))
2863 /////////////////////////////////////////////////////////////////////
2867 /////////////////////////////////////////////////////////////////////
2869 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2871 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0)
2873 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2877 InsetTabular::InsetTabular(InsetTabular const & tab)
2878 : Inset(tab), tabular(tab.tabular), scx_(0)
2880 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2884 InsetTabular::~InsetTabular()
2886 InsetTabularMailer(*this).hideDialog();
2890 bool InsetTabular::insetAllowed(InsetCode code) const
2892 if (code == MATHMACRO_CODE)
2899 void InsetTabular::write(ostream & os) const
2901 os << "Tabular" << endl;
2906 void InsetTabular::read(Lexer & lex)
2908 bool const old_format = (lex.getString() == "\\LyXTable");
2916 string token = lex.getString();
2917 while (lex && token != "\\end_inset") {
2919 token = lex.getString();
2922 lex.printError("Missing \\end_inset at this point. "
2928 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2930 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2931 // mi.base.textwidth << "\n";
2933 lyxerr << "InsetTabular::metrics: need bv" << endl;
2934 BOOST_ASSERT(false);
2938 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2941 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2942 if (tabular.isPartOfMultiColumn(i, j))
2943 // Multicolumn cell, but not first one
2948 if (tabular.cell_info[i][j].multicolumn ==
2949 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2950 p_width = tabular.cellinfo_of_cell(cell).p_width;
2952 p_width = tabular.column_info[j].p_width;
2953 if (!p_width.zero())
2954 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2955 tabular.getCellInset(cell)->metrics(m, dim);
2956 if (!p_width.zero())
2957 dim.wid = m.base.textwidth;
2958 tabular.setCellWidth(cell, dim.wid);
2959 if (p_width.zero()) {
2960 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2961 // FIXME there must be a way to get rid of
2962 // the second metrics call
2963 tabular.getCellInset(cell)->metrics(m, dim);
2965 maxAsc = max(maxAsc, dim.asc);
2966 maxDesc = max(maxDesc, dim.des);
2969 int const top_space = tabular.row_info[i].top_space_default ?
2970 default_line_space :
2971 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2972 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2973 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2974 default_line_space :
2975 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2976 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2979 dim.asc = tabular.rowAscent(0);
2980 dim.des = tabular.height() - dim.asc;
2981 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2985 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2987 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2988 BufferView * bv = pi.base.bv;
2990 // FIXME: As the full backrgound is painted in drawSelection(),
2991 // we have no choice but to do a full repaint for the Text cells.
2992 pi.full_repaint = true;
2994 resetPos(bv->cursor());
2997 x += ADD_TO_TABULAR_WIDTH;
2999 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3002 first_visible_cell = Tabular::npos;
3003 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3005 int const a = tabular.rowAscent(i);
3006 int const d = tabular.rowDescent(i);
3007 idx = tabular.cellIndex(i, 0);
3008 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3009 if (tabular.isPartOfMultiColumn(i, j))
3011 if (first_visible_cell == Tabular::npos)
3012 first_visible_cell = idx;
3014 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3015 // Cache the Inset position.
3016 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3017 if (nx + tabular.columnWidth(idx) < 0
3018 || nx > bv->workWidth()
3020 || y - a > bv->workHeight()) {
3021 pi.pain.setDrawingEnabled(false);
3022 cell(idx)->draw(pi, cx, y);
3023 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3024 pi.pain.setDrawingEnabled(original_drawing_state);
3026 cell(idx)->draw(pi, cx, y);
3027 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3029 nx += tabular.columnWidth(idx);
3033 if (i + 1 < tabular.rowCount())
3034 y += d + tabular.rowAscent(i + 1) +
3035 tabular.getAdditionalHeight(i + 1);
3040 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3042 Cursor & cur = pi.base.bv->cursor();
3044 x += scx_ + ADD_TO_TABULAR_WIDTH;
3046 // FIXME: it is wrong to completely paint the background
3047 // if we want to do single row painting.
3049 // Paint background of current tabular
3050 int const w = tabular.width();
3051 int const h = tabular.height();
3052 int yy = y - tabular.rowAscent(0);
3053 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3055 if (!cur.selection())
3057 if (&cur.inset() != this)
3063 if (tablemode(cur)) {
3066 getSelection(cur, rs, re, cs, ce);
3067 y -= tabular.rowAscent(0);
3068 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3069 int const a = tabular.rowAscent(j);
3070 int const h = a + tabular.rowDescent(j);
3072 y += tabular.getAdditionalHeight(j);
3073 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3074 if (tabular.isPartOfMultiColumn(j, i))
3076 idx_type const cell =
3077 tabular.cellIndex(j, i);
3078 int const w = tabular.columnWidth(cell);
3079 if (i >= cs && i <= ce && j >= rs && j <= re)
3080 pi.pain.fillRectangle(xx, y, w, h,
3088 x += getCellXPos(cur.idx());
3089 x += tabular.getBeginningOfTextInCell(cur.idx());
3090 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3095 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3096 row_type row, idx_type cell, bool erased) const
3098 int x2 = x + tabular.columnWidth(cell);
3099 bool on_off = false;
3100 ColorCode col = Color_tabularline;
3101 ColorCode onoffcol = Color_tabularonoffline;
3104 col = Color_deletedtext;
3105 onoffcol = Color_deletedtext;
3108 if (!tabular.topAlreadyDrawn(cell)) {
3109 on_off = !tabular.topLine(cell);
3110 pain.line(x, y - tabular.rowAscent(row),
3111 x2, y - tabular.rowAscent(row),
3112 on_off ? onoffcol : col,
3113 on_off ? Painter::line_onoffdash : Painter::line_solid);
3115 on_off = !tabular.bottomLine(cell);
3116 pain.line(x, y + tabular.rowDescent(row),
3117 x2, y + tabular.rowDescent(row),
3118 on_off ? onoffcol : col,
3119 on_off ? Painter::line_onoffdash : Painter::line_solid);
3120 if (!tabular.leftAlreadyDrawn(cell)) {
3121 on_off = !tabular.leftLine(cell);
3122 pain.line(x, y - tabular.rowAscent(row),
3123 x, y + tabular.rowDescent(row),
3124 on_off ? onoffcol : col,
3125 on_off ? Painter::line_onoffdash : Painter::line_solid);
3127 on_off = !tabular.rightLine(cell);
3128 pain.line(x2 - tabular.getAdditionalWidth(cell),
3129 y - tabular.rowAscent(row),
3130 x2 - tabular.getAdditionalWidth(cell),
3131 y + tabular.rowDescent(row),
3132 on_off ? onoffcol : col,
3133 on_off ? Painter::line_onoffdash : Painter::line_solid);
3137 docstring InsetTabular::editMessage() const
3139 return _("Opened table");
3143 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3145 //lyxerr << "InsetTabular::edit: " << this << endl;
3147 cur.selection() = false;
3150 if (isRightToLeft(cur))
3151 cur.idx() = tabular.getLastCellInRow(0);
3157 if (isRightToLeft(cur))
3158 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3160 cur.idx() = tabular.cellCount() - 1;
3162 cur.pos() = cur.lastpos(); // FIXME crude guess
3164 // FIXME: this accesses the position cache before it is initialized
3166 //cur.bv().fitCursor();
3170 void InsetTabular::updateLabels(ParIterator const & it)
3172 // In a longtable, tell captions what the current float is
3173 Counters & cnts = buffer().params().documentClass().counters();
3174 string const saveflt = cnts.current_float();
3175 if (tabular.isLongTabular())
3176 cnts.current_float("table");
3178 ParIterator it2 = it;
3180 size_t const end = it2.nargs();
3181 for ( ; it2.idx() < end; it2.top().forwardIdx())
3182 lyx::updateLabels(buffer(), it2);
3185 if (tabular.isLongTabular())
3186 cnts.current_float(saveflt);
3190 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3192 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3193 << "\n cur:" << cur);
3194 CursorSlice sl = cur.top();
3195 Cursor & bvcur = cur.bv().cursor();
3197 switch (cmd.action) {
3199 case LFUN_MOUSE_PRESS:
3200 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3202 // do not reset cursor/selection if we have selected
3203 // some cells (bug 2715).
3204 if (cmd.button() == mouse_button::button3
3205 && &bvcur.selBegin().inset() == this
3206 && tablemode(bvcur))
3209 // Let InsetText do it
3210 cell(cur.idx())->dispatch(cur, cmd);
3212 case LFUN_MOUSE_MOTION:
3213 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3214 if (cmd.button() == mouse_button::button1) {
3215 // only accept motions to places not deeper nested than the real anchor
3216 if (bvcur.anchor_.hasPart(cur)) {
3217 // only update if selection changes
3218 if (bvcur.idx() == cur.idx() &&
3219 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3221 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3222 bvcur.setCursor(cur);
3223 bvcur.selection() = true;
3229 case LFUN_MOUSE_RELEASE:
3230 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3231 if (cmd.button() == mouse_button::button3)
3232 InsetTabularMailer(*this).showDialog(&cur.bv());
3235 case LFUN_CELL_BACKWARD:
3237 cur.selection() = false;
3240 case LFUN_CELL_FORWARD:
3242 cur.selection() = false;
3245 case LFUN_CHAR_FORWARD_SELECT:
3246 case LFUN_CHAR_FORWARD:
3247 cell(cur.idx())->dispatch(cur, cmd);
3248 if (!cur.result().dispatched()) {
3250 if (sl == cur.top())
3251 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3257 case LFUN_CHAR_BACKWARD_SELECT:
3258 case LFUN_CHAR_BACKWARD:
3259 cell(cur.idx())->dispatch(cur, cmd);
3260 if (!cur.result().dispatched()) {
3262 if (sl == cur.top())
3263 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3269 case LFUN_CHAR_RIGHT_SELECT:
3270 case LFUN_CHAR_RIGHT:
3271 //FIXME: for visual cursor, really move right
3272 if (isRightToLeft(cur))
3273 lyx::dispatch(FuncRequest(
3274 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3275 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3277 lyx::dispatch(FuncRequest(
3278 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3279 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3282 case LFUN_CHAR_LEFT_SELECT:
3283 case LFUN_CHAR_LEFT:
3284 //FIXME: for visual cursor, really move left
3285 if (isRightToLeft(cur))
3286 lyx::dispatch(FuncRequest(
3287 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3288 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3290 lyx::dispatch(FuncRequest(
3291 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3292 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3295 case LFUN_DOWN_SELECT:
3297 cell(cur.idx())->dispatch(cur, cmd);
3298 cur.dispatched(); // override the cell's decision
3299 if (sl == cur.top())
3300 // if our Text didn't do anything to the cursor
3301 // then we try to put the cursor into the cell below
3302 // setting also the right targetX.
3303 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3304 cur.idx() = tabular.getCellBelow(cur.idx());
3306 TextMetrics const & tm =
3307 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3308 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3310 if (sl == cur.top()) {
3311 // we trick it to go to forward after leaving the
3313 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3318 case LFUN_UP_SELECT:
3320 cell(cur.idx())->dispatch(cur, cmd);
3321 cur.dispatched(); // override the cell's decision
3322 if (sl == cur.top())
3323 // if our Text didn't do anything to the cursor
3324 // then we try to put the cursor into the cell above
3325 // setting also the right targetX.
3326 if (tabular.cellRow(cur.idx()) != 0) {
3327 cur.idx() = tabular.getCellAbove(cur.idx());
3328 cur.pit() = cur.lastpit();
3329 Text const * text = cell(cur.idx())->getText(0);
3330 TextMetrics const & tm = cur.bv().textMetrics(text);
3331 ParagraphMetrics const & pm =
3332 tm.parMetrics(cur.lastpit());
3333 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3335 if (sl == cur.top()) {
3336 cmd = FuncRequest(LFUN_UP);
3341 // case LFUN_SCREEN_DOWN: {
3342 // //if (hasSelection())
3343 // // cur.selection() = false;
3344 // col_type const col = tabular.cellColumn(cur.idx());
3345 // int const t = cur.bv().top_y() + cur.bv().height();
3346 // if (t < yo() + tabular.getHeightOfTabular()) {
3347 // cur.bv().scrollDocView(t);
3348 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3350 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3357 // case LFUN_SCREEN_UP: {
3358 // //if (hasSelection())
3359 // // cur.selection() = false;
3360 // col_type const col = tabular.cellColumn(cur.idx());
3361 // int const t = cur.bv().top_y() + cur.bv().height();
3363 // cur.bv().scrollDocView(t);
3367 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3371 // cur.par() = cur.lastpar();
3372 // cur.pos() = cur.lastpos();
3376 case LFUN_LAYOUT_TABULAR:
3377 InsetTabularMailer(*this).showDialog(&cur.bv());
3380 case LFUN_INSET_DIALOG_UPDATE:
3381 InsetTabularMailer(*this).updateDialog(&cur.bv());
3384 case LFUN_TABULAR_FEATURE:
3385 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3389 // insert file functions
3390 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3391 case LFUN_FILE_INSERT_PLAINTEXT: {
3393 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3394 FileName(to_utf8(cmd.argument())));
3397 cur.recordUndoInset(INSERT_UNDO);
3398 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3399 // content has been replaced,
3400 // so cursor might be invalid
3401 cur.pos() = cur.lastpos();
3402 cur.pit() = cur.lastpit();
3403 bvcur.setCursor(cur);
3410 if (tablemode(cur)) {
3411 if (copySelection(cur)) {
3412 cur.recordUndoInset(DELETE_UNDO);
3417 cell(cur.idx())->dispatch(cur, cmd);
3420 case LFUN_CHAR_DELETE_BACKWARD:
3421 case LFUN_CHAR_DELETE_FORWARD:
3422 if (tablemode(cur)) {
3423 cur.recordUndoInset(DELETE_UNDO);
3427 cell(cur.idx())->dispatch(cur, cmd);
3431 if (!cur.selection())
3433 if (tablemode(cur)) {
3437 cell(cur.idx())->dispatch(cur, cmd);
3440 case LFUN_CLIPBOARD_PASTE:
3441 case LFUN_PRIMARY_SELECTION_PASTE: {
3442 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3443 theClipboard().getAsText() :
3444 theSelection().get();
3447 // pass to InsertPlaintextString, but
3448 // only if we have multi-cell content
3449 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3450 cur.recordUndoInset(INSERT_UNDO);
3451 if (insertPlaintextString(cur.bv(), clip, false)) {
3452 // content has been replaced,
3453 // so cursor might be invalid
3454 cur.pos() = cur.lastpos();
3455 cur.pit() = cur.lastpit();
3456 bvcur.setCursor(cur);
3460 // Let the cell handle normal text
3461 cell(cur.idx())->dispatch(cur, cmd);
3466 if (tabularStackDirty() && theClipboard().isInternal() ||
3467 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3468 cur.recordUndoInset(INSERT_UNDO);
3469 pasteClipboard(cur);
3472 cell(cur.idx())->dispatch(cur, cmd);
3475 case LFUN_FONT_EMPH:
3476 case LFUN_FONT_BOLD:
3477 case LFUN_FONT_ROMAN:
3478 case LFUN_FONT_NOUN:
3479 case LFUN_FONT_ITAL:
3480 case LFUN_FONT_FRAK:
3481 case LFUN_FONT_TYPEWRITER:
3482 case LFUN_FONT_SANS:
3483 case LFUN_FONT_FREE_APPLY:
3484 case LFUN_FONT_FREE_UPDATE:
3485 case LFUN_FONT_SIZE:
3486 case LFUN_FONT_UNDERLINE:
3488 case LFUN_WORD_CAPITALIZE:
3489 case LFUN_WORD_UPCASE:
3490 case LFUN_WORD_LOWCASE:
3491 case LFUN_CHARS_TRANSPOSE:
3492 if (tablemode(cur)) {
3495 getSelection(cur, rs, re, cs, ce);
3496 Cursor tmpcur = cur;
3497 for (row_type i = rs; i <= re; ++i) {
3498 for (col_type j = cs; j <= ce; ++j) {
3499 // cursor follows cell:
3500 tmpcur.idx() = tabular.cellIndex(i, j);
3501 // select this cell only:
3504 tmpcur.resetAnchor();
3505 tmpcur.pit() = tmpcur.lastpit();
3506 tmpcur.pos() = tmpcur.top().lastpos();
3507 tmpcur.setCursor(tmpcur);
3508 tmpcur.setSelection();
3509 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3514 cell(cur.idx())->dispatch(cur, cmd);
3518 // we try to handle this event in the insets dispatch function.
3519 cell(cur.idx())->dispatch(cur, cmd);
3525 // function sets an object as defined in func_status.h:
3526 // states OK, Unknown, Disabled, On, Off.
3527 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3528 FuncStatus & status) const
3530 switch (cmd.action) {
3531 case LFUN_TABULAR_FEATURE: {
3532 int action = Tabular::LAST_ACTION;
3534 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3535 string const tmp = tabularFeature[i].feature;
3536 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3537 action = tabularFeature[i].action;
3541 if (action == Tabular::LAST_ACTION) {
3543 status.unknown(true);
3547 string const argument
3548 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3550 row_type sel_row_start = 0;
3551 row_type sel_row_end = 0;
3552 col_type sel_col_start = 0;
3553 col_type sel_col_end = 0;
3554 Tabular::ltType dummyltt;
3557 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3560 case Tabular::SET_PWIDTH:
3561 case Tabular::SET_MPWIDTH:
3562 case Tabular::SET_SPECIAL_COLUMN:
3563 case Tabular::SET_SPECIAL_MULTI:
3564 case Tabular::APPEND_ROW:
3565 case Tabular::APPEND_COLUMN:
3566 case Tabular::DELETE_ROW:
3567 case Tabular::DELETE_COLUMN:
3568 case Tabular::COPY_ROW:
3569 case Tabular::COPY_COLUMN:
3570 case Tabular::SET_ALL_LINES:
3571 case Tabular::UNSET_ALL_LINES:
3572 case Tabular::SET_TOP_SPACE:
3573 case Tabular::SET_BOTTOM_SPACE:
3574 case Tabular::SET_INTERLINE_SPACE:
3578 case Tabular::MULTICOLUMN:
3579 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3582 case Tabular::M_TOGGLE_LINE_TOP:
3584 case Tabular::TOGGLE_LINE_TOP:
3585 status.setOnOff(tabular.topLine(cur.idx(), flag));
3588 case Tabular::M_TOGGLE_LINE_BOTTOM:
3590 case Tabular::TOGGLE_LINE_BOTTOM:
3591 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3594 case Tabular::M_TOGGLE_LINE_LEFT:
3596 case Tabular::TOGGLE_LINE_LEFT:
3597 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3600 case Tabular::M_TOGGLE_LINE_RIGHT:
3602 case Tabular::TOGGLE_LINE_RIGHT:
3603 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3606 case Tabular::M_ALIGN_LEFT:
3608 case Tabular::ALIGN_LEFT:
3609 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3612 case Tabular::M_ALIGN_RIGHT:
3614 case Tabular::ALIGN_RIGHT:
3615 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3618 case Tabular::M_ALIGN_CENTER:
3620 case Tabular::ALIGN_CENTER:
3621 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3624 case Tabular::ALIGN_BLOCK:
3625 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3626 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3629 case Tabular::M_VALIGN_TOP:
3631 case Tabular::VALIGN_TOP:
3633 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3636 case Tabular::M_VALIGN_BOTTOM:
3638 case Tabular::VALIGN_BOTTOM:
3640 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3643 case Tabular::M_VALIGN_MIDDLE:
3645 case Tabular::VALIGN_MIDDLE:
3647 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3650 case Tabular::SET_LONGTABULAR:
3651 status.setOnOff(tabular.isLongTabular());
3654 case Tabular::UNSET_LONGTABULAR:
3655 status.setOnOff(!tabular.isLongTabular());
3658 case Tabular::TOGGLE_ROTATE_TABULAR:
3659 case Tabular::SET_ROTATE_TABULAR:
3660 status.setOnOff(tabular.getRotateTabular());
3663 case Tabular::UNSET_ROTATE_TABULAR:
3664 status.setOnOff(!tabular.getRotateTabular());
3667 case Tabular::TOGGLE_ROTATE_CELL:
3668 case Tabular::SET_ROTATE_CELL:
3669 status.setOnOff(!oneCellHasRotationState(false,
3670 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3673 case Tabular::UNSET_ROTATE_CELL:
3674 status.setOnOff(!oneCellHasRotationState(true,
3675 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3678 case Tabular::SET_USEBOX:
3679 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3682 case Tabular::SET_LTFIRSTHEAD:
3683 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3686 case Tabular::UNSET_LTFIRSTHEAD:
3687 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3690 case Tabular::SET_LTHEAD:
3691 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3694 case Tabular::UNSET_LTHEAD:
3695 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3698 case Tabular::SET_LTFOOT:
3699 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3702 case Tabular::UNSET_LTFOOT:
3703 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3706 case Tabular::SET_LTLASTFOOT:
3707 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3710 case Tabular::UNSET_LTLASTFOOT:
3711 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3714 case Tabular::SET_LTNEWPAGE:
3715 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3718 case Tabular::SET_BOOKTABS:
3719 status.setOnOff(tabular.useBookTabs());
3722 case Tabular::UNSET_BOOKTABS:
3723 status.setOnOff(!tabular.useBookTabs());
3728 status.enabled(false);
3734 // These are only enabled inside tabular
3735 case LFUN_CELL_BACKWARD:
3736 case LFUN_CELL_FORWARD:
3737 status.enabled(true);
3740 // disable these with multiple cells selected
3741 case LFUN_INSET_INSERT:
3742 case LFUN_TABULAR_INSERT:
3743 case LFUN_FLEX_INSERT:
3744 case LFUN_FLOAT_INSERT:
3745 case LFUN_FLOAT_WIDE_INSERT:
3746 case LFUN_FOOTNOTE_INSERT:
3747 case LFUN_MARGINALNOTE_INSERT:
3748 case LFUN_MATH_INSERT:
3749 case LFUN_MATH_MODE:
3750 case LFUN_MATH_MUTATE:
3751 case LFUN_MATH_DISPLAY:
3752 case LFUN_NOTE_INSERT:
3753 case LFUN_OPTIONAL_INSERT:
3754 case LFUN_BOX_INSERT:
3755 case LFUN_BRANCH_INSERT:
3756 case LFUN_WRAP_INSERT:
3757 case LFUN_ERT_INSERT: {
3758 if (tablemode(cur)) {
3759 status.enabled(false);
3762 return cell(cur.idx())->getStatus(cur, cmd, status);
3765 // disable in non-fixed-width cells
3767 case LFUN_BREAK_PARAGRAPH:
3768 case LFUN_BREAK_PARAGRAPH_SKIP: {
3769 if (tabular.getPWidth(cur.idx()).zero()) {
3770 status.enabled(false);
3773 return cell(cur.idx())->getStatus(cur, cmd, status);
3777 if (tabularStackDirty() && theClipboard().isInternal()) {
3778 status.enabled(true);
3781 return cell(cur.idx())->getStatus(cur, cmd, status);
3783 case LFUN_INSET_MODIFY:
3784 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3785 status.enabled(true);
3791 // we try to handle this event in the insets dispatch function.
3792 return cell(cur.idx())->getStatus(cur, cmd, status);
3797 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3799 return tabular.latex(os, runparams);
3803 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3805 os << '\n'; // output table on a new line
3806 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3807 tabular.plaintext(os, runparams, dp, false, 0);
3808 return PLAINTEXT_NEWLINE;
3812 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3817 // FIXME: Why not pass a proper DocIterator here?
3819 // if the table is inside a float it doesn't need the informaltable
3820 // wrapper. Search for it.
3821 for (master = owner(); master; master = master->owner())
3822 if (master->lyxCode() == FLOAT_CODE)
3827 os << "<informaltable>";
3830 ret += tabular.docbook(os, runparams);
3832 os << "</informaltable>";
3839 void InsetTabular::validate(LaTeXFeatures & features) const
3841 tabular.validate(features);
3845 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3847 return tabular.getCellInset(idx);
3851 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3853 return tabular.getCellInset(idx);
3857 void InsetTabular::cursorPos(BufferView const & bv,
3858 CursorSlice const & sl, bool boundary, int & x, int & y) const
3860 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3862 // y offset correction
3863 int const row = tabular.cellRow(sl.idx());
3864 for (int i = 0; i <= row; ++i) {
3866 y += tabular.rowAscent(i);
3867 y += tabular.getAdditionalHeight(i);
3870 y += tabular.rowDescent(i);
3873 // x offset correction
3874 int const col = tabular.cellColumn(sl.idx());
3875 int idx = tabular.cellIndex(row, 0);
3876 for (int j = 0; j < col; ++j) {
3877 if (tabular.isPartOfMultiColumn(row, j))
3879 x += tabular.columnWidth(idx);
3882 x += tabular.getBeginningOfTextInCell(idx);
3883 x += ADD_TO_TABULAR_WIDTH;
3888 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3892 Inset const & inset = *tabular.getCellInset(cell);
3893 Point o = bv.coordCache().getInsets().xy(&inset);
3894 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3895 int const xend = xbeg + tabular.columnWidth(cell);
3896 row_type const row = tabular.cellRow(cell);
3897 int const ybeg = o.y_ - tabular.rowAscent(row) -
3898 tabular.getAdditionalHeight(row);
3899 int const yend = o.y_ + tabular.rowDescent(row);
3911 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3912 // << " ybeg=" << ybeg << " yend=" << yend
3913 // << " xx=" << xx << " yy=" << yy
3914 // << " dist=" << xx + yy << endl;
3919 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3921 //lyxerr << "InsetTabular::editXY: " << this << endl;
3922 cur.selection() = false;
3924 cur.idx() = getNearestCell(cur.bv(), x, y);
3926 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3930 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3932 cur.idx() = getNearestCell(cur.bv(), x, y);
3933 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3937 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3939 idx_type idx_min = 0;
3940 int dist_min = numeric_limits<int>::max();
3941 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3942 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3943 int const d = dist(bv, i, x, y);
3954 int InsetTabular::getCellXPos(idx_type const cell) const
3958 for (; !tabular.isFirstCellInRow(c); --c)
3961 for (; c < cell; ++c)
3962 lx += tabular.columnWidth(c);
3968 void InsetTabular::resetPos(Cursor & cur) const
3970 BufferView & bv = cur.bv();
3971 int const maxwidth = bv.workWidth();
3973 if (&cur.inset() != this) {
3977 int const X2 = maxwidth;
3978 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3979 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3980 int const x2 = x1 + tabular.columnWidth(cur.idx());
3983 scx_ = X1 + 20 - x1;
3985 scx_ = X2 - 20 - x2;
3990 cur.updateFlags(Update::Force | Update::FitCursor);
3994 void InsetTabular::moveNextCell(Cursor & cur)
3996 if (isRightToLeft(cur)) {
3997 if (tabular.isFirstCellInRow(cur.idx())) {
3998 row_type const row = tabular.cellRow(cur.idx());
3999 if (row == tabular.rowCount() - 1)
4001 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4008 if (tabular.isLastCell(cur.idx()))
4018 void InsetTabular::movePrevCell(Cursor & cur)
4020 if (isRightToLeft(cur)) {
4021 if (tabular.isLastCellInRow(cur.idx())) {
4022 row_type const row = tabular.cellRow(cur.idx());
4025 cur.idx() = tabular.getFirstCellInRow(row);
4026 cur.idx() = tabular.getCellAbove(cur.idx());
4028 if (tabular.isLastCell(cur.idx()))
4033 if (cur.idx() == 0) // first cell
4037 cur.pit() = cur.lastpit();
4038 cur.pos() = cur.lastpos();
4040 // FIXME: this accesses the position cache before it is initialized
4045 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4047 Tabular::Feature action = Tabular::LAST_ACTION;
4050 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4051 string const tmp = tabularFeature[i].feature;
4053 if (tmp == what.substr(0, tmp.length())) {
4054 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4055 //tabularFeatures[i].feature.length()))
4056 action = tabularFeature[i].action;
4060 if (action == Tabular::LAST_ACTION)
4064 ltrim(what.substr(tabularFeature[i].feature.length()));
4065 tabularFeatures(cur, action, val);
4070 static void checkLongtableSpecial(Tabular::ltType & ltt,
4071 string const & special, bool & flag)
4073 if (special == "dl_above") {
4076 } else if (special == "dl_below") {
4077 ltt.bottomDL = flag;
4079 } else if (special == "empty") {
4088 bool InsetTabular::oneCellHasRotationState(bool rotated,
4089 row_type row_start, row_type row_end,
4090 col_type col_start, col_type col_end) const {
4092 for (row_type i = row_start; i <= row_end; ++i) {
4093 for (col_type j = col_start; j <= col_end; ++j) {
4094 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4103 void InsetTabular::tabularFeatures(Cursor & cur,
4104 Tabular::Feature feature, string const & value)
4106 col_type sel_col_start;
4107 col_type sel_col_end;
4108 row_type sel_row_start;
4109 row_type sel_row_end;
4110 bool setLines = false;
4111 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4112 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4116 case Tabular::M_ALIGN_LEFT:
4117 case Tabular::ALIGN_LEFT:
4118 setAlign = LYX_ALIGN_LEFT;
4121 case Tabular::M_ALIGN_RIGHT:
4122 case Tabular::ALIGN_RIGHT:
4123 setAlign = LYX_ALIGN_RIGHT;
4126 case Tabular::M_ALIGN_CENTER:
4127 case Tabular::ALIGN_CENTER:
4128 setAlign = LYX_ALIGN_CENTER;
4131 case Tabular::ALIGN_BLOCK:
4132 setAlign = LYX_ALIGN_BLOCK;
4135 case Tabular::M_VALIGN_TOP:
4136 case Tabular::VALIGN_TOP:
4137 setVAlign = Tabular::LYX_VALIGN_TOP;
4140 case Tabular::M_VALIGN_BOTTOM:
4141 case Tabular::VALIGN_BOTTOM:
4142 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4145 case Tabular::M_VALIGN_MIDDLE:
4146 case Tabular::VALIGN_MIDDLE:
4147 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4154 cur.recordUndoInset(ATOMIC_UNDO);
4156 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4157 row_type const row = tabular.cellRow(cur.idx());
4158 col_type const column = tabular.cellColumn(cur.idx());
4160 Tabular::ltType ltt;
4164 case Tabular::SET_PWIDTH: {
4165 Length const len(value);
4166 tabular.setColumnPWidth(cur, cur.idx(), len);
4168 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4169 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4173 case Tabular::SET_MPWIDTH:
4174 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4177 case Tabular::SET_SPECIAL_COLUMN:
4178 case Tabular::SET_SPECIAL_MULTI:
4179 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4182 case Tabular::APPEND_ROW:
4183 // append the row into the tabular
4184 tabular.appendRow(cur.idx());
4187 case Tabular::APPEND_COLUMN:
4188 // append the column into the tabular
4189 tabular.appendColumn(cur.idx());
4190 cur.idx() = tabular.cellIndex(row, column);
4193 case Tabular::DELETE_ROW:
4194 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4195 tabular.deleteRow(sel_row_start);
4196 if (sel_row_start >= tabular.rowCount())
4198 cur.idx() = tabular.cellIndex(sel_row_start, column);
4201 cur.selection() = false;
4204 case Tabular::DELETE_COLUMN:
4205 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4206 tabular.deleteColumn(sel_col_start);
4207 if (sel_col_start >= tabular.columnCount())
4209 cur.idx() = tabular.cellIndex(row, sel_col_start);
4212 cur.selection() = false;
4215 case Tabular::COPY_ROW:
4216 tabular.copyRow(row);
4219 case Tabular::COPY_COLUMN:
4220 tabular.copyColumn(column);
4221 cur.idx() = tabular.cellIndex(row, column);
4224 case Tabular::M_TOGGLE_LINE_TOP:
4226 case Tabular::TOGGLE_LINE_TOP: {
4227 bool lineSet = !tabular.topLine(cur.idx(), flag);
4228 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4229 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4231 tabular.cellIndex(i, j),
4236 case Tabular::M_TOGGLE_LINE_BOTTOM:
4238 case Tabular::TOGGLE_LINE_BOTTOM: {
4239 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4240 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4241 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4242 tabular.setBottomLine(
4243 tabular.cellIndex(i, j),
4249 case Tabular::M_TOGGLE_LINE_LEFT:
4251 case Tabular::TOGGLE_LINE_LEFT: {
4252 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4253 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4254 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4255 tabular.setLeftLine(
4256 tabular.cellIndex(i,j),
4262 case Tabular::M_TOGGLE_LINE_RIGHT:
4264 case Tabular::TOGGLE_LINE_RIGHT: {
4265 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4266 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4267 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4268 tabular.setRightLine(tabular.cellIndex(i,j), lineSet, flag);
4272 case Tabular::M_ALIGN_LEFT:
4273 case Tabular::M_ALIGN_RIGHT:
4274 case Tabular::M_ALIGN_CENTER:
4276 case Tabular::ALIGN_LEFT:
4277 case Tabular::ALIGN_RIGHT:
4278 case Tabular::ALIGN_CENTER:
4279 case Tabular::ALIGN_BLOCK:
4280 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4281 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4282 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4285 case Tabular::M_VALIGN_TOP:
4286 case Tabular::M_VALIGN_BOTTOM:
4287 case Tabular::M_VALIGN_MIDDLE:
4289 case Tabular::VALIGN_TOP:
4290 case Tabular::VALIGN_BOTTOM:
4291 case Tabular::VALIGN_MIDDLE:
4292 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4293 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4294 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4297 case Tabular::MULTICOLUMN: {
4298 if (sel_row_start != sel_row_end) {
4299 // FIXME: Need I say it ? This is horrible.
4301 Alert::error(_("Error setting multicolumn"),
4302 _("You cannot set multicolumn vertically."));
4305 if (!cur.selection()) {
4306 // just multicol for one single cell
4307 // check whether we are completely in a multicol
4308 if (tabular.isMultiColumn(cur.idx()))
4309 tabular.unsetMultiColumn(cur.idx());
4311 tabular.setMultiColumn(cur.idx(), 1);
4314 // we have a selection so this means we just add all this
4315 // cells to form a multicolumn cell
4316 idx_type const s_start = cur.selBegin().idx();
4317 idx_type const s_end = cur.selEnd().idx();
4318 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4319 cur.idx() = s_start;
4322 cur.selection() = false;
4326 case Tabular::SET_ALL_LINES:
4328 case Tabular::UNSET_ALL_LINES:
4329 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4330 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4331 tabular.setAllLines(
4332 tabular.cellIndex(i,j), setLines);
4335 case Tabular::SET_LONGTABULAR:
4336 tabular.setLongTabular(true);
4339 case Tabular::UNSET_LONGTABULAR:
4340 tabular.setLongTabular(false);
4343 case Tabular::SET_ROTATE_TABULAR:
4344 tabular.setRotateTabular(true);
4347 case Tabular::UNSET_ROTATE_TABULAR:
4348 tabular.setRotateTabular(false);
4351 case Tabular::TOGGLE_ROTATE_TABULAR:
4352 tabular.setRotateTabular(!tabular.getRotateTabular());
4355 case Tabular::SET_ROTATE_CELL:
4356 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4357 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4358 tabular.setRotateCell(
4359 tabular.cellIndex(i, j), true);
4362 case Tabular::UNSET_ROTATE_CELL:
4363 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4364 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4365 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4368 case Tabular::TOGGLE_ROTATE_CELL:
4370 bool oneNotRotated = oneCellHasRotationState(false,
4371 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4373 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4374 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4375 tabular.setRotateCell(tabular.cellIndex(i, j),
4380 case Tabular::SET_USEBOX: {
4381 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4382 if (val == tabular.getUsebox(cur.idx()))
4383 val = Tabular::BOX_NONE;
4384 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4385 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4386 tabular.setUsebox(tabular.cellIndex(i, j), val);
4390 case Tabular::UNSET_LTFIRSTHEAD:
4392 case Tabular::SET_LTFIRSTHEAD:
4393 tabular.getRowOfLTFirstHead(row, ltt);
4394 checkLongtableSpecial(ltt, value, flag);
4395 tabular.setLTHead(row, flag, ltt, true);
4398 case Tabular::UNSET_LTHEAD:
4400 case Tabular::SET_LTHEAD:
4401 tabular.getRowOfLTHead(row, ltt);
4402 checkLongtableSpecial(ltt, value, flag);
4403 tabular.setLTHead(row, flag, ltt, false);
4406 case Tabular::UNSET_LTFOOT:
4408 case Tabular::SET_LTFOOT:
4409 tabular.getRowOfLTFoot(row, ltt);
4410 checkLongtableSpecial(ltt, value, flag);
4411 tabular.setLTFoot(row, flag, ltt, false);
4414 case Tabular::UNSET_LTLASTFOOT:
4416 case Tabular::SET_LTLASTFOOT:
4417 tabular.getRowOfLTLastFoot(row, ltt);
4418 checkLongtableSpecial(ltt, value, flag);
4419 tabular.setLTFoot(row, flag, ltt, true);
4422 case Tabular::SET_LTNEWPAGE:
4423 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4426 case Tabular::SET_BOOKTABS:
4427 tabular.setBookTabs(true);
4430 case Tabular::UNSET_BOOKTABS:
4431 tabular.setBookTabs(false);
4434 case Tabular::SET_TOP_SPACE: {
4436 if (value == "default")
4437 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4438 tabular.row_info[i].top_space_default = true;
4439 else if (isValidLength(value, &len))
4440 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4441 tabular.row_info[i].top_space_default = false;
4442 tabular.row_info[i].top_space = len;
4445 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4446 tabular.row_info[i].top_space_default = false;
4447 tabular.row_info[i].top_space = len;
4452 case Tabular::SET_BOTTOM_SPACE: {
4454 if (value == "default")
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4456 tabular.row_info[i].bottom_space_default = true;
4457 else if (isValidLength(value, &len))
4458 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4459 tabular.row_info[i].bottom_space_default = false;
4460 tabular.row_info[i].bottom_space = len;
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4464 tabular.row_info[i].bottom_space_default = false;
4465 tabular.row_info[i].bottom_space = len;
4470 case Tabular::SET_INTERLINE_SPACE: {
4472 if (value == "default")
4473 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4474 tabular.row_info[i].interline_space_default = true;
4475 else if (isValidLength(value, &len))
4476 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4477 tabular.row_info[i].interline_space_default = false;
4478 tabular.row_info[i].interline_space = len;
4481 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4482 tabular.row_info[i].interline_space_default = false;
4483 tabular.row_info[i].interline_space = len;
4488 // dummy stuff just to avoid warnings
4489 case Tabular::LAST_ACTION:
4495 bool InsetTabular::showInsetDialog(BufferView * bv) const
4497 InsetTabularMailer(*this).showDialog(bv);
4502 void InsetTabular::openLayoutDialog(BufferView * bv) const
4504 InsetTabularMailer(*this).showDialog(bv);
4508 bool InsetTabular::copySelection(Cursor & cur)
4510 if (!cur.selection())
4515 getSelection(cur, rs, re, cs, ce);
4517 paste_tabular.reset(new Tabular(tabular));
4519 for (row_type i = 0; i < rs; ++i)
4520 paste_tabular->deleteRow(0);
4522 row_type const rows = re - rs + 1;
4523 while (paste_tabular->rowCount() > rows)
4524 paste_tabular->deleteRow(rows);
4526 paste_tabular->setTopLine(0, true, true);
4527 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4530 for (col_type i = 0; i < cs; ++i)
4531 paste_tabular->deleteColumn(0);
4533 col_type const columns = ce - cs + 1;
4534 while (paste_tabular->columnCount() > columns)
4535 paste_tabular->deleteColumn(columns);
4537 paste_tabular->setLeftLine(0, true, true);
4538 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4541 odocstringstream os;
4542 OutputParams const runparams(0);
4543 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4544 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4545 cap::copySelection(cur, os.str());
4547 // mark tabular stack dirty
4548 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4549 // when we (hopefully) have a one-for-all paste mechanism.
4550 // This must be called after cap::copySelection.
4551 dirtyTabularStack(true);
4557 bool InsetTabular::pasteClipboard(Cursor & cur)
4561 col_type const actcol = tabular.cellColumn(cur.idx());
4562 row_type const actrow = tabular.cellRow(cur.idx());
4563 for (row_type r1 = 0, r2 = actrow;
4564 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4566 for (col_type c1 = 0, c2 = actcol;
4567 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4569 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4570 tabular.isPartOfMultiColumn(r2, c2))
4572 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4576 if (tabular.isPartOfMultiColumn(r2, c2)) {
4580 shared_ptr<InsetText> inset(
4581 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4582 tabular.setCellInset(r2, c2, inset);
4583 // FIXME: change tracking (MG)
4584 inset->setChange(Change(cur.buffer().params().trackChanges ?
4585 Change::INSERTED : Change::UNCHANGED));
4593 void InsetTabular::cutSelection(Cursor & cur)
4595 if (!cur.selection())
4600 getSelection(cur, rs, re, cs, ce);
4601 for (row_type i = rs; i <= re; ++i) {
4602 for (col_type j = cs; j <= ce; ++j) {
4603 shared_ptr<InsetText> t
4604 = cell(tabular.cellIndex(i, j));
4605 if (cur.buffer().params().trackChanges)
4606 // FIXME: Change tracking (MG)
4607 t->setChange(Change(Change::DELETED));
4613 // cursor position might be invalid now
4614 if (cur.pit() > cur.lastpit())
4615 cur.pit() = cur.lastpit();
4616 if (cur.pos() > cur.lastpos())
4617 cur.pos() = cur.lastpos();
4618 cur.clearSelection();
4622 bool InsetTabular::isRightToLeft(Cursor & cur) const
4624 BOOST_ASSERT(cur.depth() > 1);
4625 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4626 pos_type const parentpos = cur[cur.depth() - 2].pos();
4627 return parentpar.getFontSettings(cur.bv().buffer().params(),
4628 parentpos).language()->rightToLeft();
4632 void InsetTabular::getSelection(Cursor & cur,
4633 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4635 CursorSlice const & beg = cur.selBegin();
4636 CursorSlice const & end = cur.selEnd();
4637 cs = tabular.cellColumn(beg.idx());
4638 ce = tabular.cellColumn(end.idx());
4641 cs = tabular.cellColumn(end.idx());
4643 ce = tabular.cellRightColumn(end.idx());
4646 rs = tabular.cellRow(beg.idx());
4647 re = tabular.cellRow(end.idx());
4653 Text * InsetTabular::getText(int idx) const
4655 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4659 void InsetTabular::setChange(Change const & change)
4661 for (idx_type idx = 0; idx < nargs(); ++idx)
4662 cell(idx)->setChange(change);
4666 void InsetTabular::acceptChanges(BufferParams const & bparams)
4668 for (idx_type idx = 0; idx < nargs(); ++idx)
4669 cell(idx)->acceptChanges(bparams);
4673 void InsetTabular::rejectChanges(BufferParams const & bparams)
4675 for (idx_type idx = 0; idx < nargs(); ++idx)
4676 cell(idx)->rejectChanges(bparams);
4680 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4682 return tabular.getPWidth(cell).zero();
4686 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4689 if (buf.length() <= 0)
4694 col_type maxCols = 1;
4695 size_t const len = buf.length();
4699 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4707 maxCols = max(cols, maxCols);
4713 maxCols = max(cols, maxCols);
4719 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4720 loctab = paste_tabular.get();
4722 dirtyTabularStack(true);
4725 cell = bv.cursor().idx();
4726 ocol = tabular.cellColumn(cell);
4727 row = tabular.cellRow(cell);
4731 idx_type const cells = loctab->cellCount();
4734 rows = loctab->rowCount();
4735 col_type const columns = loctab->columnCount();
4737 while (cell < cells && p < len && row < rows &&
4738 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4744 // we can only set this if we are not too far right
4745 if (cols < columns) {
4746 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4747 Font const font = bv.textMetrics(&inset->text_).
4749 inset->setText(buf.substr(op, p - op), font,
4750 buffer().params().trackChanges);
4756 // we can only set this if we are not too far right
4757 if (cols < columns) {
4758 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4759 Font const font = bv.textMetrics(&inset->text_).
4761 inset->setText(buf.substr(op, p - op), font,
4762 buffer().params().trackChanges);
4767 cell = loctab->cellIndex(row, cols);
4773 // check for the last cell if there is no trailing '\n'
4774 if (cell < cells && op < len) {
4775 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4776 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4777 inset->setText(buf.substr(op, len - op), font,
4778 buffer().params().trackChanges);
4784 void InsetTabular::addPreview(PreviewLoader & loader) const
4786 row_type const rows = tabular.rowCount();
4787 col_type const columns = tabular.columnCount();
4788 for (row_type i = 0; i < rows; ++i) {
4789 for (col_type j = 0; j < columns; ++j)
4790 tabular.getCellInset(i, j)->addPreview(loader);
4795 bool InsetTabular::tablemode(Cursor & cur) const
4797 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4801 bool InsetTabular::completionSupported(Cursor const & cur) const
4803 Cursor const & bvCur = cur.bv().cursor();
4804 if (&bvCur.inset() != this)
4806 return cur.text()->completionSupported(cur);
4810 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4812 return completionSupported(cur);
4816 bool InsetTabular::automaticInlineCompletion() const
4818 return lyxrc.completion_inline_text;
4822 bool InsetTabular::automaticPopupCompletion() const
4824 return lyxrc.completion_popup_text;
4828 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4830 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4834 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4836 if (!completionSupported(cur))
4838 return cur.text()->completionPrefix(cur);
4842 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4844 if (!completionSupported(cur))
4847 return cur.text()->insertCompletion(cur, s, finished);
4851 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4852 Dimension & dim) const
4854 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4855 tm.completionPosAndDim(cur, x, y, dim);
4859 string const InsetTabularMailer::name_("tabular");
4861 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4862 : inset_(const_cast<InsetTabular &>(inset))
4866 string const InsetTabularMailer::inset2string(Buffer const &) const
4868 return params2string(inset_);
4872 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4874 istringstream data(in);
4876 lex.setStream(data);
4883 if (!lex || token != name_)
4884 return print_mailer_error("InsetTabularMailer", in, 1,
4887 // This is part of the inset proper that is usually swallowed
4888 // by Buffer::readInset
4890 if (!lex || token != "Tabular")
4891 return print_mailer_error("InsetTabularMailer", in, 2, "Tabular");
4897 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4900 data << name_ << ' ';
4902 data << "\\end_inset\n";