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 column_vector::iterator cit = column_info.begin() + column + 1;
685 column_info.insert(cit, ColumnData());
686 col_type const ncols = columnCount();
687 // set the column values of the column before
688 column_info[column + 1] = column_info[column];
690 for (row_type i = 0; i < rowCount(); ++i) {
691 cell_info[i].insert(cell_info[i].begin() + column + 1, CellData(buffer()));
692 col_type c = column + 2;
694 && cell_info[i][c].multicolumn == CELL_PART_OF_MULTICOLUMN) {
695 cell_info[i][c].multicolumn = CELL_NORMAL;
700 for (row_type i = 0; i < rowCount(); ++i) {
701 cell_info[i][column + 1].inset->clear();
702 if (buffer().params().trackChanges)
703 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
709 void Tabular::deleteColumn(col_type const column)
711 // Not allowed to delete last column
712 if (columnCount() == 1)
715 column_info.erase(column_info.begin() + column);
716 for (row_type i = 0; i < rowCount(); ++i) {
717 // Care about multicolumn cells
718 if (column + 1 < columnCount() &&
719 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
720 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
721 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
723 cell_info[i].erase(cell_info[i].begin() + column);
729 void Tabular::copyColumn(col_type const column)
731 BufferParams const & bp = buffer().params();
732 column_info.insert(column_info.begin() + column, column_info[column]);
734 for (row_type i = 0; i < rowCount(); ++i)
735 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
738 for (row_type i = 0; i < rowCount(); ++i)
739 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
744 void Tabular::updateIndexes()
747 // Count only non-multicol cells plus begin multicol
748 // cells, ignore non-begin multicol cells:
749 for (row_type row = 0; row < rowCount(); ++row) {
750 for (col_type column = 0; column < columnCount(); ++column) {
751 if (cell_info[row][column].multicolumn
752 != Tabular::CELL_PART_OF_MULTICOLUMN)
754 BOOST_ASSERT(numberofcells != 0);
755 cell_info[row][column].cellno =
760 rowofcell.resize(numberofcells);
761 columnofcell.resize(numberofcells);
766 c < numberofcells && row < rowCount() && column < columnCount();) {
768 columnofcell[c] = column;
772 } while (column < columnCount() &&
773 cell_info[row][column].multicolumn
774 == Tabular::CELL_PART_OF_MULTICOLUMN);
776 if (column == columnCount()) {
782 for (row_type row = 0; row < rowCount(); ++row) {
783 for (col_type column = 0; column < columnCount(); ++column) {
784 if (isPartOfMultiColumn(row,column))
786 cell_info[row][column].inset->setAutoBreakRows(
787 !getPWidth(cellIndex(row, column)).zero());
793 Tabular::idx_type Tabular::cellCount() const
795 return numberofcells;
799 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
801 row_type const row = cellRow(cell);
803 for (col_type i = 0; i < columnCount(); ++i)
804 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
810 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
812 if (!wholerow && isMultiColumn(cell) &&
813 !(use_booktabs && cellRow(cell) == 0))
814 return cellinfo_of_cell(cell).top_line;
815 return row_info[cellRow(cell)].top_line;
819 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
821 if (!wholerow && isMultiColumn(cell) &&
822 !(use_booktabs && isLastRow(cell)))
823 return cellinfo_of_cell(cell).bottom_line;
824 return row_info[cellRow(cell)].bottom_line;
828 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
832 if (!wholecolumn && isMultiColumn(cell) &&
833 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
834 return cellinfo_of_cell(cell).left_line;
835 return column_info[cellColumn(cell)].left_line;
839 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
843 if (!wholecolumn && isMultiColumn(cell) &&
844 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
845 return cellinfo_of_cell(cell).right_line;
846 return column_info[cellRightColumn(cell)].right_line;
850 bool Tabular::topAlreadyDrawn(idx_type cell) const
852 row_type row = cellRow(cell);
853 if (row > 0 && !getAdditionalHeight(row)) {
854 col_type column = cellColumn(cell);
857 && cell_info[row][column].multicolumn
858 == Tabular::CELL_PART_OF_MULTICOLUMN)
860 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
861 return row_info[row].bottom_line;
863 return cell_info[row][column].bottom_line;
869 bool Tabular::leftAlreadyDrawn(idx_type cell) const
871 col_type column = cellColumn(cell);
873 row_type row = cellRow(cell);
875 (cell_info[row][column].multicolumn ==
876 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
877 if (getAdditionalWidth(cell_info[row][column].cellno))
879 return rightLine(cell_info[row][column].cellno);
885 bool Tabular::isLastRow(idx_type cell) const
887 return cellRow(cell) == rowCount() - 1;
891 int Tabular::getAdditionalHeight(row_type row) const
893 if (!row || row >= rowCount())
899 for (col_type column = 0; column < columnCount() && bottom; ++column) {
900 switch (cell_info[row - 1][column].multicolumn) {
901 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
902 bottom = cell_info[row - 1][column].bottom_line;
904 case Tabular::CELL_NORMAL:
905 bottom = row_info[row - 1].bottom_line;
908 for (col_type column = 0; column < columnCount() && top; ++column) {
909 switch (cell_info[row][column].multicolumn) {
910 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
911 top = cell_info[row][column].top_line;
913 case Tabular::CELL_NORMAL:
914 top = row_info[row].top_line;
917 int const interline_space = row_info[row - 1].interline_space_default ?
919 row_info[row - 1].interline_space.inPixels(width());
921 return interline_space + WIDTH_OF_LINE;
922 return interline_space;
926 int Tabular::getAdditionalWidth(idx_type cell) const
928 // internally already set in setCellWidth
929 // used to get it back in text.cpp
930 col_type const col = cellRightColumn(cell);
931 row_type const row = cellRow(cell);
932 if (col < columnCount() - 1 && rightLine(cell) &&
933 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
935 return WIDTH_OF_LINE;
941 // returns the maximum over all rows
942 int Tabular::columnWidth(idx_type cell) const
944 col_type const column1 = cellColumn(cell);
945 col_type const column2 = cellRightColumn(cell);
947 for (col_type i = column1; i <= column2; ++i)
948 result += column_info[i].width;
953 int Tabular::width() const
956 for (col_type i = 0; i < columnCount(); ++i)
957 width += column_info[i].width;
962 // returns true if a complete update is necessary, otherwise false
963 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
965 if (!isMultiColumn(cell))
968 row_type const row = cellRow(cell);
969 col_type const column1 = cellColumn(cell);
970 col_type const column2 = cellRightColumn(cell);
971 int const old_val = cell_info[row][column2].width;
973 // first set columns to 0 so we can calculate the right width
974 for (col_type i = column1; i <= column2; ++i) {
975 cell_info[row][i].width = 0;
977 // set the width to MAX_WIDTH until width > 0
978 int width = new_width + 2 * WIDTH_OF_LINE;
979 col_type i = column1;
980 for (; i < column2 && width > column_info[i].width; ++i) {
981 cell_info[row][i].width = column_info[i].width;
982 width -= column_info[i].width;
985 cell_info[row][i].width = width;
987 if (old_val != cell_info[row][column2].width) {
988 // in this case we have to recalculate all multicolumn cells which
989 // have this column as one of theirs but not as last one
990 calculate_width_of_column_NMC(i);
991 recalculateMulticolumnsOfColumn(i);
992 calculate_width_of_column(i);
998 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1000 // the last column does not have to be recalculated because all
1001 // multicolumns will have here there last multicolumn cell which
1002 // always will have the whole rest of the width of the cell.
1003 if (columnCount() < 2 || column > (columnCount() - 2))
1005 for (row_type row = 0; row < rowCount(); ++row) {
1006 int mc = cell_info[row][column].multicolumn;
1007 int nmc = cell_info[row][column+1].multicolumn;
1008 // we only have to update multicolumns which do not have this
1009 // column as their last column!
1010 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1011 (mc == CELL_PART_OF_MULTICOLUMN &&
1012 nmc == CELL_PART_OF_MULTICOLUMN))
1014 idx_type const cellno = cell_info[row][column].cellno;
1015 setWidthOfMulticolCell(cellno,
1016 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1022 void Tabular::setCellWidth(idx_type cell, int new_width)
1024 row_type const row = cellRow(cell);
1025 col_type const column1 = cellColumn(cell);
1030 if (rightLine(cell_info[row][column1].cellno, true) &&
1031 column1 < columnCount() - 1 &&
1032 leftLine(cell_info[row][column1+1].cellno, true))
1034 add_width = WIDTH_OF_LINE;
1037 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1040 if (isMultiColumnReal(cell)) {
1041 tmp = setWidthOfMulticolCell(cell, new_width);
1043 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1044 cell_info[row][column1].width = width;
1045 tmp = calculate_width_of_column_NMC(column1);
1047 recalculateMulticolumnsOfColumn(column1);
1050 for (col_type i = 0; i < columnCount(); ++i)
1051 calculate_width_of_column(i);
1056 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1059 if (!isMultiColumn(cell) || onlycolumn)
1060 column_info[cellColumn(cell)].alignment = align;
1062 cellinfo_of_cell(cell).alignment = align;
1066 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1069 if (!isMultiColumn(cell) || onlycolumn)
1070 column_info[cellColumn(cell)].valignment = align;
1072 cellinfo_of_cell(cell).valignment = align;
1079 * Allow line and paragraph breaks for fixed width cells or disallow them,
1080 * merge cell paragraphs and reset layout to standard for variable width
1083 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1085 inset->setAutoBreakRows(fixedWidth);
1089 // merge all paragraphs to one
1090 BufferParams const & bp = cur.bv().buffer().params();
1091 while (inset->paragraphs().size() > 1)
1092 mergeParagraph(bp, inset->paragraphs(), 0);
1096 // undo information has already been recorded
1097 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1098 bp.documentClass().emptyLayoutName());
1105 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1106 Length const & width)
1108 col_type const j = cellColumn(cell);
1110 column_info[j].p_width = width;
1111 for (row_type i = 0; i < rowCount(); ++i) {
1112 idx_type const cell = cellIndex(i, j);
1113 // because of multicolumns
1114 toggleFixedWidth(cur, getCellInset(cell).get(),
1115 !getPWidth(cell).zero());
1117 // cur paragraph can become invalid after paragraphs were merged
1118 if (cur.pit() > cur.lastpit())
1119 cur.pit() = cur.lastpit();
1120 // cur position can become invalid after newlines were removed
1121 if (cur.pos() > cur.lastpos())
1122 cur.pos() = cur.lastpos();
1126 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1127 Length const & width)
1129 if (!isMultiColumn(cell))
1132 cellinfo_of_cell(cell).p_width = width;
1133 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1134 // cur paragraph can become invalid after paragraphs were merged
1135 if (cur.pit() > cur.lastpit())
1136 cur.pit() = cur.lastpit();
1137 // cur position can become invalid after newlines were removed
1138 if (cur.pos() > cur.lastpos())
1139 cur.pos() = cur.lastpos();
1144 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1145 Tabular::Feature what)
1147 if (what == SET_SPECIAL_MULTI)
1148 cellinfo_of_cell(cell).align_special = special;
1150 column_info[cellColumn(cell)].align_special = special;
1154 void Tabular::setAllLines(idx_type cell, bool line)
1156 setTopLine(cell, line);
1157 setBottomLine(cell, line);
1158 setRightLine(cell, line);
1159 setLeftLine(cell, line);
1163 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1165 row_type const row = cellRow(cell);
1166 if (wholerow || !isMultiColumn(cell))
1167 row_info[row].top_line = line;
1169 cellinfo_of_cell(cell).top_line = line;
1173 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1175 if (wholerow || !isMultiColumn(cell))
1176 row_info[cellRow(cell)].bottom_line = line;
1178 cellinfo_of_cell(cell).bottom_line = line;
1182 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1184 if (wholecolumn || !isMultiColumn(cell))
1185 column_info[cellColumn(cell)].left_line = line;
1187 cellinfo_of_cell(cell).left_line = line;
1191 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1193 if (wholecolumn || !isMultiColumn(cell))
1194 column_info[cellRightColumn(cell)].right_line = line;
1196 cellinfo_of_cell(cell).right_line = line;
1200 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1202 if (!onlycolumn && isMultiColumn(cell))
1203 return cellinfo_of_cell(cell).alignment;
1204 return column_info[cellColumn(cell)].alignment;
1209 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1211 if (!onlycolumn && isMultiColumn(cell))
1212 return cellinfo_of_cell(cell).valignment;
1213 return column_info[cellColumn(cell)].valignment;
1217 Length const Tabular::getPWidth(idx_type cell) const
1219 if (isMultiColumn(cell))
1220 return cellinfo_of_cell(cell).p_width;
1221 return column_info[cellColumn(cell)].p_width;
1225 Length const Tabular::getColumnPWidth(idx_type cell) const
1227 return column_info[cellColumn(cell)].p_width;
1231 Length const Tabular::getMColumnPWidth(idx_type cell) const
1233 if (isMultiColumn(cell))
1234 return cellinfo_of_cell(cell).p_width;
1239 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1241 if (what == SET_SPECIAL_MULTI)
1242 return cellinfo_of_cell(cell).align_special;
1243 return column_info[cellColumn(cell)].align_special;
1247 int Tabular::cellWidth(idx_type cell) const
1249 row_type const row = cellRow(cell);
1250 col_type const column1 = cellColumn(cell);
1251 col_type const column2 = cellRightColumn(cell);
1253 for (col_type i = column1; i <= column2; ++i)
1254 result += cell_info[row][i].width;
1259 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1263 switch (getAlignment(cell)) {
1264 case LYX_ALIGN_CENTER:
1265 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1267 case LYX_ALIGN_RIGHT:
1268 x += columnWidth(cell) - cellWidth(cell);
1269 // + getAdditionalWidth(cell);
1272 // LYX_ALIGN_LEFT: nothing :-)
1276 // the LaTeX Way :-(
1282 bool Tabular::isFirstCellInRow(idx_type cell) const
1284 return cellColumn(cell) == 0;
1288 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1290 if (row > rowCount() - 1)
1291 row = rowCount() - 1;
1292 return cell_info[row][0].cellno;
1296 bool Tabular::isLastCellInRow(idx_type cell) const
1298 return cellRightColumn(cell) == columnCount() - 1;
1302 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1304 if (row > rowCount() - 1)
1305 row = rowCount() - 1;
1306 return cell_info[row][columnCount() - 1].cellno;
1310 void Tabular::calculate_width_of_column(col_type column)
1313 for (row_type i = 0; i < rowCount(); ++i)
1314 maximum = max(cell_info[i][column].width, maximum);
1315 column_info[column].width = maximum;
1320 // Calculate the columns regarding ONLY the normal cells and if this
1321 // column is inside a multicolumn cell then use it only if its the last
1322 // column of this multicolumn cell as this gives an added width to the
1323 // column, all the rest should be adapted!
1325 bool Tabular::calculate_width_of_column_NMC(col_type column)
1327 int const old_column_width = column_info[column].width;
1329 for (row_type i = 0; i < rowCount(); ++i) {
1330 idx_type cell = cellIndex(i, column);
1331 bool ismulti = isMultiColumnReal(cell);
1332 if ((!ismulti || column == cellRightColumn(cell)) &&
1333 cell_info[i][column].width > max)
1335 max = cell_info[i][column].width;
1338 column_info[column].width = max;
1339 return column_info[column].width != old_column_width;
1343 Tabular::row_type Tabular::cellRow(idx_type cell) const
1345 if (cell >= cellCount())
1346 return rowCount() - 1;
1349 return rowofcell[cell];
1353 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1355 if (cell >= cellCount())
1356 return columnCount() - 1;
1359 return columnofcell[cell];
1363 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1365 row_type const row = cellRow(cell);
1366 col_type column = cellColumn(cell);
1367 while (column < columnCount() - 1 &&
1368 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1374 void Tabular::write(ostream & os) const
1378 << write_attribute("version", 3)
1379 << write_attribute("rows", rowCount())
1380 << write_attribute("columns", columnCount())
1382 // global longtable options
1384 << write_attribute("rotate", rotate)
1385 << write_attribute("booktabs", use_booktabs)
1386 << write_attribute("islongtable", is_long_tabular)
1387 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1388 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1389 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1390 << write_attribute("headTopDL", endhead.topDL)
1391 << write_attribute("headBottomDL", endhead.bottomDL)
1392 << write_attribute("footTopDL", endfoot.topDL)
1393 << write_attribute("footBottomDL", endfoot.bottomDL)
1394 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1395 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1396 << write_attribute("lastFootEmpty", endlastfoot.empty)
1398 for (col_type j = 0; j < columnCount(); ++j) {
1400 << write_attribute("alignment", column_info[j].alignment)
1401 << write_attribute("valignment", column_info[j].valignment)
1402 << write_attribute("leftline", column_info[j].left_line)
1403 << write_attribute("rightline", column_info[j].right_line)
1404 << write_attribute("width", column_info[j].p_width.asString())
1405 << write_attribute("special", column_info[j].align_special)
1408 for (row_type i = 0; i < rowCount(); ++i) {
1410 << write_attribute("topline", row_info[i].top_line)
1411 << write_attribute("bottomline", row_info[i].bottom_line);
1412 static const string def("default");
1413 if (row_info[i].top_space_default)
1414 os << write_attribute("topspace", def);
1416 os << write_attribute("topspace", row_info[i].top_space);
1417 if (row_info[i].bottom_space_default)
1418 os << write_attribute("bottomspace", def);
1420 os << write_attribute("bottomspace", row_info[i].bottom_space);
1421 if (row_info[i].interline_space_default)
1422 os << write_attribute("interlinespace", def);
1424 os << write_attribute("interlinespace", row_info[i].interline_space);
1425 os << write_attribute("endhead", row_info[i].endhead)
1426 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1427 << write_attribute("endfoot", row_info[i].endfoot)
1428 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1429 << write_attribute("newpage", row_info[i].newpage)
1431 for (col_type j = 0; j < columnCount(); ++j) {
1433 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1434 << write_attribute("alignment", cell_info[i][j].alignment)
1435 << write_attribute("valignment", cell_info[i][j].valignment)
1436 << write_attribute("topline", cell_info[i][j].top_line)
1437 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1438 << write_attribute("leftline", cell_info[i][j].left_line)
1439 << write_attribute("rightline", cell_info[i][j].right_line)
1440 << write_attribute("rotate", cell_info[i][j].rotate)
1441 << write_attribute("usebox", cell_info[i][j].usebox)
1442 << write_attribute("width", cell_info[i][j].p_width)
1443 << write_attribute("special", cell_info[i][j].align_special)
1445 os << "\\begin_inset ";
1446 cell_info[i][j].inset->write(os);
1447 os << "\n\\end_inset\n"
1452 os << "</lyxtabular>\n";
1456 void Tabular::read(Lexer & lex)
1459 istream & is = lex.getStream();
1461 l_getline(is, line);
1462 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1463 BOOST_ASSERT(false);
1468 if (!getTokenValue(line, "version", version))
1470 BOOST_ASSERT(version >= 2);
1473 if (!getTokenValue(line, "rows", rows_arg))
1476 if (!getTokenValue(line, "columns", columns_arg))
1478 init(buffer(), rows_arg, columns_arg);
1479 l_getline(is, line);
1480 if (!prefixIs(line, "<features")) {
1481 lyxerr << "Wrong tabular format (expected <features ...> got"
1482 << line << ')' << endl;
1485 getTokenValue(line, "rotate", rotate);
1486 getTokenValue(line, "booktabs", use_booktabs);
1487 getTokenValue(line, "islongtable", is_long_tabular);
1488 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1489 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1490 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1491 getTokenValue(line, "headTopDL", endhead.topDL);
1492 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1493 getTokenValue(line, "footTopDL", endfoot.topDL);
1494 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1495 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1496 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1497 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1499 for (col_type j = 0; j < columnCount(); ++j) {
1501 if (!prefixIs(line,"<column")) {
1502 lyxerr << "Wrong tabular format (expected <column ...> got"
1503 << line << ')' << endl;
1506 getTokenValue(line, "alignment", column_info[j].alignment);
1507 getTokenValue(line, "valignment", column_info[j].valignment);
1508 getTokenValue(line, "leftline", column_info[j].left_line);
1509 getTokenValue(line, "rightline", column_info[j].right_line);
1510 getTokenValue(line, "width", column_info[j].p_width);
1511 getTokenValue(line, "special", column_info[j].align_special);
1514 for (row_type i = 0; i < rowCount(); ++i) {
1515 l_getline(is, line);
1516 if (!prefixIs(line, "<row")) {
1517 lyxerr << "Wrong tabular format (expected <row ...> got"
1518 << line << ')' << endl;
1521 getTokenValue(line, "topline", row_info[i].top_line);
1522 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1523 getTokenValue(line, "topspace", row_info[i].top_space,
1524 row_info[i].top_space_default);
1525 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1526 row_info[i].bottom_space_default);
1527 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1528 row_info[i].interline_space_default);
1529 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1530 getTokenValue(line, "endhead", row_info[i].endhead);
1531 getTokenValue(line, "endfoot", row_info[i].endfoot);
1532 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1533 getTokenValue(line, "newpage", row_info[i].newpage);
1534 for (col_type j = 0; j < columnCount(); ++j) {
1535 l_getline(is, line);
1536 if (!prefixIs(line, "<cell")) {
1537 lyxerr << "Wrong tabular format (expected <cell ...> got"
1538 << line << ')' << endl;
1541 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1542 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1543 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1544 getTokenValue(line, "topline", cell_info[i][j].top_line);
1545 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1546 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1547 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1548 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1549 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1550 getTokenValue(line, "width", cell_info[i][j].p_width);
1551 getTokenValue(line, "special", cell_info[i][j].align_special);
1552 l_getline(is, line);
1553 if (prefixIs(line, "\\begin_inset")) {
1554 cell_info[i][j].inset->read(lex);
1555 l_getline(is, line);
1557 if (!prefixIs(line, "</cell>")) {
1558 lyxerr << "Wrong tabular format (expected </cell> got"
1559 << line << ')' << endl;
1563 l_getline(is, line);
1564 if (!prefixIs(line, "</row>")) {
1565 lyxerr << "Wrong tabular format (expected </row> got"
1566 << line << ')' << endl;
1570 while (!prefixIs(line, "</lyxtabular>")) {
1571 l_getline(is, line);
1577 bool Tabular::isMultiColumn(idx_type cell) const
1579 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1583 bool Tabular::isMultiColumnReal(idx_type cell) const
1585 return cellColumn(cell) != cellRightColumn(cell) &&
1586 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1590 Tabular::CellData & Tabular::cellinfo_of_cell(idx_type cell) const
1592 return cell_info[cellRow(cell)][cellColumn(cell)];
1596 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1598 CellData & cs = cellinfo_of_cell(cell);
1599 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1600 cs.alignment = column_info[cellColumn(cell)].alignment;
1601 cs.top_line = row_info[cellRow(cell)].top_line;
1602 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1603 cs.left_line = column_info[cellColumn(cell)].left_line;
1604 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1605 for (idx_type i = 1; i < number; ++i) {
1606 CellData & cs1 = cellinfo_of_cell(cell + i);
1607 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1608 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1615 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1617 row_type const row = cellRow(cell);
1618 col_type column = cellColumn(cell);
1619 idx_type result = 1;
1621 while (column < columnCount() &&
1622 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1631 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1633 row_type const row = cellRow(cell);
1634 col_type column = cellColumn(cell);
1636 idx_type result = 0;
1638 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1639 cell_info[row][column].multicolumn = CELL_NORMAL;
1641 while (column < columnCount() &&
1642 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1644 cell_info[row][column].multicolumn = CELL_NORMAL;
1654 void Tabular::setBookTabs(bool what)
1656 use_booktabs = what;
1660 bool Tabular::useBookTabs() const
1662 return use_booktabs;
1666 void Tabular::setLongTabular(bool what)
1668 is_long_tabular = what;
1672 bool Tabular::isLongTabular() const
1674 return is_long_tabular;
1678 void Tabular::setRotateTabular(bool flag)
1684 bool Tabular::getRotateTabular() const
1690 void Tabular::setRotateCell(idx_type cell, bool flag)
1692 cellinfo_of_cell(cell).rotate = flag;
1696 bool Tabular::getRotateCell(idx_type cell) const
1698 return cellinfo_of_cell(cell).rotate;
1702 bool Tabular::needRotating() const
1706 for (row_type i = 0; i < rowCount(); ++i)
1707 for (col_type j = 0; j < columnCount(); ++j)
1708 if (cell_info[i][j].rotate)
1714 bool Tabular::isLastCell(idx_type cell) const
1716 if (cell + 1 < cellCount())
1722 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1724 if (cellRow(cell) > 0)
1725 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1730 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1732 if (cellRow(cell) + 1 < rowCount())
1733 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1738 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1740 if (cellRow(cell) == 0)
1742 if (!isMultiColumn(cell))
1743 return getCellAbove(cell);
1744 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1748 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1750 if (cellRow(cell) + 1 >= rowCount())
1752 if (!isMultiColumn(cell))
1753 return getCellBelow(cell);
1754 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1758 Tabular::idx_type Tabular::cellIndex(row_type row,
1759 col_type column) const
1761 BOOST_ASSERT(column != npos && column < columnCount() &&
1762 row != npos && row < rowCount());
1763 return cell_info[row][column].cellno;
1767 void Tabular::setUsebox(idx_type cell, BoxType type)
1769 cellinfo_of_cell(cell).usebox = type;
1773 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1775 if (column_info[cellColumn(cell)].p_width.zero() &&
1776 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1778 if (cellinfo_of_cell(cell).usebox > 1)
1779 return cellinfo_of_cell(cell).usebox;
1780 return useParbox(cell);
1785 // This are functions used for the longtable support
1787 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1793 row_info[row].endfirsthead = flag;
1797 row_info[row].endhead = flag;
1802 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1805 hd.set = haveLTHead();
1806 return row_info[row].endhead;
1810 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1813 hd.set = haveLTFirstHead();
1814 return row_info[row].endfirsthead;
1818 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1824 row_info[row].endlastfoot = flag;
1828 row_info[row].endfoot = flag;
1833 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1836 fd.set = haveLTFoot();
1837 return row_info[row].endfoot;
1841 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1844 fd.set = haveLTLastFoot();
1845 return row_info[row].endlastfoot;
1849 void Tabular::setLTNewPage(row_type row, bool what)
1851 row_info[row].newpage = what;
1855 bool Tabular::getLTNewPage(row_type row) const
1857 return row_info[row].newpage;
1861 bool Tabular::haveLTHead() const
1863 for (row_type i = 0; i < rowCount(); ++i)
1864 if (row_info[i].endhead)
1870 bool Tabular::haveLTFirstHead() const
1872 if (endfirsthead.empty)
1874 for (row_type i = 0; i < rowCount(); ++i)
1875 if (row_info[i].endfirsthead)
1881 bool Tabular::haveLTFoot() const
1883 for (row_type i = 0; i < rowCount(); ++i)
1884 if (row_info[i].endfoot)
1890 bool Tabular::haveLTLastFoot() const
1892 if (endlastfoot.empty)
1894 for (row_type i = 0; i < rowCount(); ++i)
1895 if (row_info[i].endlastfoot)
1901 // end longtable support functions
1903 void Tabular::setRowAscent(row_type row, int height)
1905 if (row >= rowCount() || row_info[row].ascent == height)
1907 row_info[row].ascent = height;
1911 void Tabular::setRowDescent(row_type row, int height)
1913 if (row >= rowCount() || row_info[row].descent == height)
1915 row_info[row].descent = height;
1919 int Tabular::rowAscent(row_type row) const
1921 if (row >= rowCount())
1923 return row_info[row].ascent;
1927 int Tabular::rowDescent(row_type row) const
1929 BOOST_ASSERT(row < rowCount());
1930 return row_info[row].descent;
1934 int Tabular::height() const
1937 for (row_type row = 0; row < rowCount(); ++row)
1938 height += rowAscent(row) + rowDescent(row) +
1939 getAdditionalHeight(row);
1944 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1946 BOOST_ASSERT(row < rowCount());
1947 BOOST_ASSERT(column < columnCount());
1948 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1952 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1954 // FIXME: assert or return 0 as in TeXBottomHLine()?
1955 BOOST_ASSERT(row != npos);
1956 BOOST_ASSERT(row < rowCount());
1958 idx_type const fcell = getFirstCellInRow(row);
1959 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1962 for (idx_type i = fcell; i < n; ++i) {
1966 if (use_booktabs && row == 0) {
1969 } else if (tmp == n - fcell) {
1970 os << (use_booktabs ? "\\midrule " : "\\hline ");
1972 for (idx_type i = fcell; i < n; ++i) {
1974 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1975 << cellColumn(i) + 1
1977 << cellRightColumn(i) + 1
1989 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1991 // FIXME: return 0 or assert as in TeXTopHLine()?
1992 if (row == npos || row >= rowCount())
1995 idx_type const fcell = getFirstCellInRow(row);
1996 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1999 for (idx_type i = fcell; i < n; ++i) {
2003 if (use_booktabs && row == rowCount() - 1) {
2004 if (bottomLine(fcell))
2005 os << "\\bottomrule";
2006 } else if (tmp == n - fcell) {
2007 os << (use_booktabs ? "\\midrule" : "\\hline");
2009 for (idx_type i = fcell; i < n; ++i) {
2010 if (bottomLine(i)) {
2011 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2012 << cellColumn(i) + 1
2014 << cellRightColumn(i) + 1
2026 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2030 if (getRotateCell(cell)) {
2031 os << "\\begin{sideways}\n";
2034 if (isMultiColumn(cell)) {
2035 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2036 if (leftLine(cell) &&
2037 (isFirstCellInRow(cell) ||
2038 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2039 !rightLine(cell - 1, true))))
2041 if (!cellinfo_of_cell(cell).align_special.empty()) {
2042 os << cellinfo_of_cell(cell).align_special;
2044 if (!getPWidth(cell).zero()) {
2045 switch (getVAlignment(cell)) {
2046 case LYX_VALIGN_TOP:
2049 case LYX_VALIGN_MIDDLE:
2052 case LYX_VALIGN_BOTTOM:
2057 << from_ascii(getPWidth(cell).asLatexString())
2060 switch (getAlignment(cell)) {
2061 case LYX_ALIGN_LEFT:
2064 case LYX_ALIGN_RIGHT:
2071 } // end if else !getPWidth
2072 } // end if else !cellinfo_of_cell
2073 if (rightLine(cell))
2075 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2080 if (getUsebox(cell) == BOX_PARBOX) {
2082 switch (getVAlignment(cell)) {
2083 case LYX_VALIGN_TOP:
2086 case LYX_VALIGN_MIDDLE:
2089 case LYX_VALIGN_BOTTOM:
2093 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2095 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2096 os << "\\begin{minipage}[";
2097 switch (getVAlignment(cell)) {
2098 case LYX_VALIGN_TOP:
2101 case LYX_VALIGN_MIDDLE:
2104 case LYX_VALIGN_BOTTOM:
2108 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2116 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2121 if (getUsebox(cell) == BOX_PARBOX)
2123 else if (getUsebox(cell) == BOX_MINIPAGE) {
2124 os << "%\n\\end{minipage}";
2127 if (isMultiColumn(cell)) {
2130 if (getRotateCell(cell)) {
2131 os << "%\n\\end{sideways}";
2138 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2139 OutputParams const & runparams) const
2141 if (!is_long_tabular)
2145 // output header info
2147 if (endhead.topDL) {
2151 for (row_type i = 0; i < rowCount(); ++i) {
2152 if (row_info[i].endhead) {
2153 ret += TeXRow(os, i, runparams);
2156 if (endhead.bottomDL) {
2160 os << "\\endhead\n";
2162 if (endfirsthead.empty) {
2163 os << "\\endfirsthead\n";
2167 // output firstheader info
2168 if (haveLTFirstHead()) {
2169 if (endfirsthead.topDL) {
2173 for (row_type i = 0; i < rowCount(); ++i) {
2174 if (row_info[i].endfirsthead) {
2175 ret += TeXRow(os, i, runparams);
2178 if (endfirsthead.bottomDL) {
2182 os << "\\endfirsthead\n";
2185 // output footer info
2187 if (endfoot.topDL) {
2191 for (row_type i = 0; i < rowCount(); ++i) {
2192 if (row_info[i].endfoot) {
2193 ret += TeXRow(os, i, runparams);
2196 if (endfoot.bottomDL) {
2200 os << "\\endfoot\n";
2202 if (endlastfoot.empty) {
2203 os << "\\endlastfoot\n";
2207 // output lastfooter info
2208 if (haveLTLastFoot()) {
2209 if (endlastfoot.topDL) {
2213 for (row_type i = 0; i < rowCount(); ++i) {
2214 if (row_info[i].endlastfoot) {
2215 ret += TeXRow(os, i, runparams);
2218 if (endlastfoot.bottomDL) {
2222 os << "\\endlastfoot\n";
2229 bool Tabular::isValidRow(row_type row) const
2231 if (!is_long_tabular)
2233 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2234 !row_info[row].endfoot && !row_info[row].endlastfoot;
2238 int Tabular::TeXRow(odocstream & os, row_type i,
2239 OutputParams const & runparams) const
2241 idx_type cell = cellIndex(i, 0);
2242 int ret = TeXTopHLine(os, i);
2243 if (row_info[i].top_space_default) {
2245 os << "\\addlinespace\n";
2247 os << "\\noalign{\\vskip\\doublerulesep}\n";
2249 } else if(!row_info[i].top_space.zero()) {
2251 os << "\\addlinespace["
2252 << from_ascii(row_info[i].top_space.asLatexString())
2255 os << "\\noalign{\\vskip"
2256 << from_ascii(row_info[i].top_space.asLatexString())
2262 for (col_type j = 0; j < columnCount(); ++j) {
2263 if (isPartOfMultiColumn(i, j))
2265 ret += TeXCellPreamble(os, cell);
2266 shared_ptr<InsetText> inset = getCellInset(cell);
2268 Paragraph const & par = inset->paragraphs().front();
2269 bool rtl = par.isRTL(buffer().params())
2271 && getPWidth(cell).zero();
2274 if (par.getParLanguage(buffer().params())->lang() ==
2277 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2279 // currently, remaning RTL languages are arabic_arabtex and hebrew
2283 ret += inset->latex(os, runparams);
2287 ret += TeXCellPostamble(os, cell);
2288 if (!isLastCellInRow(cell)) { // not last cell in row
2293 os << "\\tabularnewline";
2294 if (row_info[i].bottom_space_default) {
2296 os << "\\addlinespace";
2298 os << "[\\doublerulesep]";
2299 } else if (!row_info[i].bottom_space.zero()) {
2301 os << "\\addlinespace";
2303 << from_ascii(row_info[i].bottom_space.asLatexString())
2308 ret += TeXBottomHLine(os, i);
2309 if (row_info[i].interline_space_default) {
2311 os << "\\addlinespace\n";
2313 os << "\\noalign{\\vskip\\doublerulesep}\n";
2315 } else if (!row_info[i].interline_space.zero()) {
2317 os << "\\addlinespace["
2318 << from_ascii(row_info[i].interline_space.asLatexString())
2321 os << "\\noalign{\\vskip"
2322 << from_ascii(row_info[i].interline_space.asLatexString())
2330 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2334 //+---------------------------------------------------------------------
2335 //+ first the opening preamble +
2336 //+---------------------------------------------------------------------
2339 os << "\\begin{sideways}\n";
2342 if (is_long_tabular)
2343 os << "\\begin{longtable}{";
2345 os << "\\begin{tabular}{";
2346 for (col_type i = 0; i < columnCount(); ++i) {
2347 if (!use_booktabs && column_info[i].left_line)
2349 if (!column_info[i].align_special.empty()) {
2350 os << column_info[i].align_special;
2352 if (!column_info[i].p_width.zero()) {
2353 switch (column_info[i].alignment) {
2354 case LYX_ALIGN_LEFT:
2355 os << ">{\\raggedright}";
2357 case LYX_ALIGN_RIGHT:
2358 os << ">{\\raggedleft}";
2360 case LYX_ALIGN_CENTER:
2361 os << ">{\\centering}";
2363 case LYX_ALIGN_NONE:
2364 case LYX_ALIGN_BLOCK:
2365 case LYX_ALIGN_LAYOUT:
2366 case LYX_ALIGN_SPECIAL:
2370 switch (column_info[i].valignment) {
2371 case LYX_VALIGN_TOP:
2374 case LYX_VALIGN_MIDDLE:
2377 case LYX_VALIGN_BOTTOM:
2382 << from_ascii(column_info[i].p_width.asLatexString())
2385 switch (column_info[i].alignment) {
2386 case LYX_ALIGN_LEFT:
2389 case LYX_ALIGN_RIGHT:
2396 } // end if else !column_info[i].p_width
2397 } // end if else !column_info[i].align_special
2398 if (!use_booktabs && column_info[i].right_line)
2404 ret += TeXLongtableHeaderFooter(os, runparams);
2406 //+---------------------------------------------------------------------
2407 //+ the single row and columns (cells) +
2408 //+---------------------------------------------------------------------
2410 for (row_type i = 0; i < rowCount(); ++i) {
2411 if (isValidRow(i)) {
2412 ret += TeXRow(os, i, runparams);
2413 if (is_long_tabular && row_info[i].newpage) {
2414 os << "\\newpage\n";
2420 //+---------------------------------------------------------------------
2421 //+ the closing of the tabular +
2422 //+---------------------------------------------------------------------
2424 if (is_long_tabular)
2425 os << "\\end{longtable}";
2427 os << "\\end{tabular}";
2429 os << "\n\\end{sideways}";
2437 int Tabular::docbookRow(odocstream & os, row_type row,
2438 OutputParams const & runparams) const
2441 idx_type cell = getFirstCellInRow(row);
2444 for (col_type j = 0; j < columnCount(); ++j) {
2445 if (isPartOfMultiColumn(row, j))
2448 os << "<entry align=\"";
2449 switch (getAlignment(cell)) {
2450 case LYX_ALIGN_LEFT:
2453 case LYX_ALIGN_RIGHT:
2461 os << "\" valign=\"";
2462 switch (getVAlignment(cell)) {
2463 case LYX_VALIGN_TOP:
2466 case LYX_VALIGN_BOTTOM:
2469 case LYX_VALIGN_MIDDLE:
2474 if (isMultiColumn(cell)) {
2475 os << " namest=\"col" << j << "\" ";
2476 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2480 ret += getCellInset(cell)->docbook(os, runparams);
2489 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2493 //+---------------------------------------------------------------------
2494 //+ first the opening preamble +
2495 //+---------------------------------------------------------------------
2497 os << "<tgroup cols=\"" << columnCount()
2498 << "\" colsep=\"1\" rowsep=\"1\">\n";
2500 for (col_type i = 0; i < columnCount(); ++i) {
2501 os << "<colspec colname=\"col" << i << "\" align=\"";
2502 switch (column_info[i].alignment) {
2503 case LYX_ALIGN_LEFT:
2506 case LYX_ALIGN_RIGHT:
2514 if (runparams.flavor == OutputParams::XML)
2520 //+---------------------------------------------------------------------
2521 //+ Long Tabular case +
2522 //+---------------------------------------------------------------------
2524 // output header info
2525 if (haveLTHead() || haveLTFirstHead()) {
2528 for (row_type i = 0; i < rowCount(); ++i) {
2529 if (row_info[i].endhead || row_info[i].endfirsthead) {
2530 ret += docbookRow(os, i, runparams);
2536 // output footer info
2537 if (haveLTFoot() || haveLTLastFoot()) {
2540 for (row_type i = 0; i < rowCount(); ++i) {
2541 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2542 ret += docbookRow(os, i, runparams);
2549 //+---------------------------------------------------------------------
2550 //+ the single row and columns (cells) +
2551 //+---------------------------------------------------------------------
2555 for (row_type i = 0; i < rowCount(); ++i) {
2556 if (isValidRow(i)) {
2557 ret += docbookRow(os, i, runparams);
2562 //+---------------------------------------------------------------------
2563 //+ the closing of the tabular +
2564 //+---------------------------------------------------------------------
2573 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2574 vector<unsigned int> const & clen) const
2576 idx_type const fcell = getFirstCellInRow(row);
2577 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2580 for (idx_type i = fcell; i < n; ++i) {
2590 for (idx_type i = fcell; i < n; ++i) {
2601 col_type column = cellColumn(i);
2602 int len = clen[column];
2603 while (column < columnCount() - 1
2604 && isPartOfMultiColumn(row, ++column))
2605 len += clen[column] + 4;
2606 os << docstring(len, ch);
2621 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2622 vector<unsigned int> const & clen) const
2624 idx_type const fcell = getFirstCellInRow(row);
2625 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2628 for (idx_type i = fcell; i < n; ++i) {
2629 if (bottomLine(i)) {
2638 for (idx_type i = fcell; i < n; ++i) {
2639 if (bottomLine(i)) {
2649 col_type column = cellColumn(i);
2650 int len = clen[column];
2651 while (column < columnCount() -1
2652 && isPartOfMultiColumn(row, ++column))
2653 len += clen[column] + 4;
2654 os << docstring(len, ch);
2655 if (bottomLine(i)) {
2669 void Tabular::plaintextPrintCell(odocstream & os,
2670 OutputParams const & runparams,
2671 idx_type cell, row_type row, col_type column,
2672 vector<unsigned int> const & clen,
2673 bool onlydata) const
2675 odocstringstream sstr;
2676 getCellInset(cell)->plaintext(sstr, runparams);
2688 unsigned int len1 = sstr.str().length();
2689 unsigned int len2 = clen[column];
2690 while (column < columnCount() -1
2691 && isPartOfMultiColumn(row, ++column))
2692 len2 += clen[column] + 4;
2695 switch (getAlignment(cell)) {
2697 case LYX_ALIGN_LEFT:
2700 case LYX_ALIGN_RIGHT:
2704 case LYX_ALIGN_CENTER:
2710 os << docstring(len1, ' ') << sstr.str()
2711 << docstring(len2, ' ');
2713 if (rightLine(cell))
2720 void Tabular::plaintext(odocstream & os,
2721 OutputParams const & runparams, int const depth,
2722 bool onlydata, char_type delim) const
2724 // first calculate the width of the single columns
2725 vector<unsigned int> clen(columnCount());
2728 // first all non (real) multicolumn cells!
2729 for (col_type j = 0; j < columnCount(); ++j) {
2731 for (row_type i = 0; i < rowCount(); ++i) {
2732 idx_type cell = cellIndex(i, j);
2733 if (isMultiColumnReal(cell))
2735 odocstringstream sstr;
2736 getCellInset(cell)->plaintext(sstr, runparams);
2737 if (clen[j] < sstr.str().length())
2738 clen[j] = sstr.str().length();
2741 // then all (real) multicolumn cells!
2742 for (col_type j = 0; j < columnCount(); ++j) {
2743 for (row_type i = 0; i < rowCount(); ++i) {
2744 idx_type cell = cellIndex(i, j);
2745 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2747 odocstringstream sstr;
2748 getCellInset(cell)->plaintext(sstr, runparams);
2749 int len = int(sstr.str().length());
2750 idx_type const n = cells_in_multicolumn(cell);
2751 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2753 if (len > int(clen[j + n - 1]))
2754 clen[j + n - 1] = len;
2759 for (row_type i = 0; i < rowCount(); ++i) {
2760 if (!onlydata && plaintextTopHLine(os, i, clen))
2761 os << docstring(depth * 2, ' ');
2762 for (col_type j = 0; j < columnCount(); ++j) {
2763 if (isPartOfMultiColumn(i, j))
2765 if (onlydata && j > 0)
2766 // we don't use operator<< for single UCS4 character.
2767 // see explanation in docstream.h
2769 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2774 os << docstring(depth * 2, ' ');
2775 if (plaintextBottomHLine(os, i, clen))
2776 os << docstring(depth * 2, ' ');
2782 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2784 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2788 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2789 col_type column) const
2791 return cell_info[row][column].inset;
2795 void Tabular::setCellInset(row_type row, col_type column,
2796 shared_ptr<InsetText> ins) const
2798 cell_info[row][column].inset = ins;
2803 Tabular::getCellFromInset(Inset const * inset) const
2805 // is this inset part of the tabular?
2807 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2808 BOOST_ASSERT(false);
2811 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2812 if (getCellInset(cell).get() == inset) {
2813 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2814 << "cell=" << cell);
2818 // We should have found a cell at this point
2819 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2820 << inset << " not found!" << endl;
2821 BOOST_ASSERT(false);
2827 void Tabular::validate(LaTeXFeatures & features) const
2829 features.require("NeedTabularnewline");
2831 features.require("booktabs");
2832 if (isLongTabular())
2833 features.require("longtable");
2835 features.require("rotating");
2836 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2837 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2838 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2839 features.require("array");
2840 getCellInset(cell)->validate(features);
2845 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2847 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2848 ParagraphList::const_iterator cit = parlist.begin();
2849 ParagraphList::const_iterator end = parlist.end();
2851 for (; cit != end; ++cit)
2852 for (int i = 0; i < cit->size(); ++i)
2853 if (cit->isNewline(i))
2860 /////////////////////////////////////////////////////////////////////
2864 /////////////////////////////////////////////////////////////////////
2866 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2868 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0)
2870 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2874 InsetTabular::InsetTabular(InsetTabular const & tab)
2875 : Inset(tab), tabular(tab.tabular), scx_(0)
2877 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2881 InsetTabular::~InsetTabular()
2883 InsetTabularMailer(*this).hideDialog();
2887 bool InsetTabular::insetAllowed(InsetCode code) const
2889 if (code == MATHMACRO_CODE)
2896 void InsetTabular::write(ostream & os) const
2898 os << "Tabular" << endl;
2903 void InsetTabular::read(Lexer & lex)
2905 bool const old_format = (lex.getString() == "\\LyXTable");
2913 string token = lex.getString();
2914 while (lex && token != "\\end_inset") {
2916 token = lex.getString();
2919 lex.printError("Missing \\end_inset at this point. "
2925 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2927 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2928 // mi.base.textwidth << "\n";
2930 lyxerr << "InsetTabular::metrics: need bv" << endl;
2931 BOOST_ASSERT(false);
2935 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2938 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2939 if (tabular.isPartOfMultiColumn(i, j))
2940 // Multicolumn cell, but not first one
2945 if (tabular.cell_info[i][j].multicolumn ==
2946 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2947 p_width = tabular.cellinfo_of_cell(cell).p_width;
2949 p_width = tabular.column_info[j].p_width;
2950 if (!p_width.zero())
2951 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2952 tabular.getCellInset(cell)->metrics(m, dim);
2953 if (!p_width.zero())
2954 dim.wid = m.base.textwidth;
2955 tabular.setCellWidth(cell, dim.wid);
2956 if (p_width.zero()) {
2957 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2958 // FIXME there must be a way to get rid of
2959 // the second metrics call
2960 tabular.getCellInset(cell)->metrics(m, dim);
2962 maxAsc = max(maxAsc, dim.asc);
2963 maxDesc = max(maxDesc, dim.des);
2966 int const top_space = tabular.row_info[i].top_space_default ?
2967 default_line_space :
2968 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2969 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2970 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2971 default_line_space :
2972 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2973 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2976 dim.asc = tabular.rowAscent(0);
2977 dim.des = tabular.height() - dim.asc;
2978 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2982 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2984 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2985 BufferView * bv = pi.base.bv;
2987 // FIXME: As the full backrgound is painted in drawSelection(),
2988 // we have no choice but to do a full repaint for the Text cells.
2989 pi.full_repaint = true;
2991 resetPos(bv->cursor());
2994 x += ADD_TO_TABULAR_WIDTH;
2996 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2999 first_visible_cell = Tabular::npos;
3000 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3002 int const a = tabular.rowAscent(i);
3003 int const d = tabular.rowDescent(i);
3004 idx = tabular.cellIndex(i, 0);
3005 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3006 if (tabular.isPartOfMultiColumn(i, j))
3008 if (first_visible_cell == Tabular::npos)
3009 first_visible_cell = idx;
3011 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3012 // Cache the Inset position.
3013 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3014 if (nx + tabular.columnWidth(idx) < 0
3015 || nx > bv->workWidth()
3017 || y - a > bv->workHeight()) {
3018 pi.pain.setDrawingEnabled(false);
3019 cell(idx)->draw(pi, cx, y);
3020 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3021 pi.pain.setDrawingEnabled(original_drawing_state);
3023 cell(idx)->draw(pi, cx, y);
3024 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3026 nx += tabular.columnWidth(idx);
3030 if (i + 1 < tabular.rowCount())
3031 y += d + tabular.rowAscent(i + 1) +
3032 tabular.getAdditionalHeight(i + 1);
3037 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3039 Cursor & cur = pi.base.bv->cursor();
3041 x += scx_ + ADD_TO_TABULAR_WIDTH;
3043 // FIXME: it is wrong to completely paint the background
3044 // if we want to do single row painting.
3046 // Paint background of current tabular
3047 int const w = tabular.width();
3048 int const h = tabular.height();
3049 int yy = y - tabular.rowAscent(0);
3050 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3052 if (!cur.selection())
3054 if (&cur.inset() != this)
3060 if (tablemode(cur)) {
3063 getSelection(cur, rs, re, cs, ce);
3064 y -= tabular.rowAscent(0);
3065 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3066 int const a = tabular.rowAscent(j);
3067 int const h = a + tabular.rowDescent(j);
3069 y += tabular.getAdditionalHeight(j);
3070 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3071 if (tabular.isPartOfMultiColumn(j, i))
3073 idx_type const cell =
3074 tabular.cellIndex(j, i);
3075 int const w = tabular.columnWidth(cell);
3076 if (i >= cs && i <= ce && j >= rs && j <= re)
3077 pi.pain.fillRectangle(xx, y, w, h,
3085 x += getCellXPos(cur.idx());
3086 x += tabular.getBeginningOfTextInCell(cur.idx());
3087 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3092 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3093 row_type row, idx_type cell, bool erased) const
3095 int x2 = x + tabular.columnWidth(cell);
3096 bool on_off = false;
3097 ColorCode col = Color_tabularline;
3098 ColorCode onoffcol = Color_tabularonoffline;
3101 col = Color_deletedtext;
3102 onoffcol = Color_deletedtext;
3105 if (!tabular.topAlreadyDrawn(cell)) {
3106 on_off = !tabular.topLine(cell);
3107 pain.line(x, y - tabular.rowAscent(row),
3108 x2, y - tabular.rowAscent(row),
3109 on_off ? onoffcol : col,
3110 on_off ? Painter::line_onoffdash : Painter::line_solid);
3112 on_off = !tabular.bottomLine(cell);
3113 pain.line(x, y + tabular.rowDescent(row),
3114 x2, y + tabular.rowDescent(row),
3115 on_off ? onoffcol : col,
3116 on_off ? Painter::line_onoffdash : Painter::line_solid);
3117 if (!tabular.leftAlreadyDrawn(cell)) {
3118 on_off = !tabular.leftLine(cell);
3119 pain.line(x, y - tabular.rowAscent(row),
3120 x, y + tabular.rowDescent(row),
3121 on_off ? onoffcol : col,
3122 on_off ? Painter::line_onoffdash : Painter::line_solid);
3124 on_off = !tabular.rightLine(cell);
3125 pain.line(x2 - tabular.getAdditionalWidth(cell),
3126 y - tabular.rowAscent(row),
3127 x2 - tabular.getAdditionalWidth(cell),
3128 y + tabular.rowDescent(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3134 docstring InsetTabular::editMessage() const
3136 return _("Opened table");
3140 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3142 //lyxerr << "InsetTabular::edit: " << this << endl;
3144 cur.selection() = false;
3147 if (isRightToLeft(cur))
3148 cur.idx() = tabular.getLastCellInRow(0);
3154 if (isRightToLeft(cur))
3155 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3157 cur.idx() = tabular.cellCount() - 1;
3159 cur.pos() = cur.lastpos(); // FIXME crude guess
3161 // FIXME: this accesses the position cache before it is initialized
3163 //cur.bv().fitCursor();
3167 void InsetTabular::updateLabels(ParIterator const & it)
3169 // In a longtable, tell captions what the current float is
3170 Counters & cnts = buffer().params().documentClass().counters();
3171 string const saveflt = cnts.current_float();
3172 if (tabular.isLongTabular())
3173 cnts.current_float("table");
3175 ParIterator it2 = it;
3177 size_t const end = it2.nargs();
3178 for ( ; it2.idx() < end; it2.top().forwardIdx())
3179 lyx::updateLabels(buffer(), it2);
3182 if (tabular.isLongTabular())
3183 cnts.current_float(saveflt);
3187 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3189 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3190 << "\n cur:" << cur);
3191 CursorSlice sl = cur.top();
3192 Cursor & bvcur = cur.bv().cursor();
3194 switch (cmd.action) {
3196 case LFUN_MOUSE_PRESS:
3197 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3199 // do not reset cursor/selection if we have selected
3200 // some cells (bug 2715).
3201 if (cmd.button() == mouse_button::button3
3202 && &bvcur.selBegin().inset() == this
3203 && tablemode(bvcur))
3206 // Let InsetText do it
3207 cell(cur.idx())->dispatch(cur, cmd);
3209 case LFUN_MOUSE_MOTION:
3210 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3211 if (cmd.button() == mouse_button::button1) {
3212 // only accept motions to places not deeper nested than the real anchor
3213 if (bvcur.anchor_.hasPart(cur)) {
3214 // only update if selection changes
3215 if (bvcur.idx() == cur.idx() &&
3216 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3218 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3219 bvcur.setCursor(cur);
3220 bvcur.selection() = true;
3226 case LFUN_MOUSE_RELEASE:
3227 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3228 if (cmd.button() == mouse_button::button3)
3229 InsetTabularMailer(*this).showDialog(&cur.bv());
3232 case LFUN_CELL_BACKWARD:
3234 cur.selection() = false;
3237 case LFUN_CELL_FORWARD:
3239 cur.selection() = false;
3242 case LFUN_CHAR_FORWARD_SELECT:
3243 case LFUN_CHAR_FORWARD:
3244 cell(cur.idx())->dispatch(cur, cmd);
3245 if (!cur.result().dispatched()) {
3247 if (sl == cur.top())
3248 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3254 case LFUN_CHAR_BACKWARD_SELECT:
3255 case LFUN_CHAR_BACKWARD:
3256 cell(cur.idx())->dispatch(cur, cmd);
3257 if (!cur.result().dispatched()) {
3259 if (sl == cur.top())
3260 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3266 case LFUN_CHAR_RIGHT_SELECT:
3267 case LFUN_CHAR_RIGHT:
3268 //FIXME: for visual cursor, really move right
3269 if (isRightToLeft(cur))
3270 lyx::dispatch(FuncRequest(
3271 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3272 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3274 lyx::dispatch(FuncRequest(
3275 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3276 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3279 case LFUN_CHAR_LEFT_SELECT:
3280 case LFUN_CHAR_LEFT:
3281 //FIXME: for visual cursor, really move left
3282 if (isRightToLeft(cur))
3283 lyx::dispatch(FuncRequest(
3284 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3285 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3287 lyx::dispatch(FuncRequest(
3288 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3289 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3292 case LFUN_DOWN_SELECT:
3294 cell(cur.idx())->dispatch(cur, cmd);
3295 cur.dispatched(); // override the cell's decision
3296 if (sl == cur.top())
3297 // if our Text didn't do anything to the cursor
3298 // then we try to put the cursor into the cell below
3299 // setting also the right targetX.
3300 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3301 cur.idx() = tabular.getCellBelow(cur.idx());
3303 TextMetrics const & tm =
3304 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3305 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3307 if (sl == cur.top()) {
3308 // we trick it to go to forward after leaving the
3310 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3315 case LFUN_UP_SELECT:
3317 cell(cur.idx())->dispatch(cur, cmd);
3318 cur.dispatched(); // override the cell's decision
3319 if (sl == cur.top())
3320 // if our Text didn't do anything to the cursor
3321 // then we try to put the cursor into the cell above
3322 // setting also the right targetX.
3323 if (tabular.cellRow(cur.idx()) != 0) {
3324 cur.idx() = tabular.getCellAbove(cur.idx());
3325 cur.pit() = cur.lastpit();
3326 Text const * text = cell(cur.idx())->getText(0);
3327 TextMetrics const & tm = cur.bv().textMetrics(text);
3328 ParagraphMetrics const & pm =
3329 tm.parMetrics(cur.lastpit());
3330 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3332 if (sl == cur.top()) {
3333 cmd = FuncRequest(LFUN_UP);
3338 // case LFUN_SCREEN_DOWN: {
3339 // //if (hasSelection())
3340 // // cur.selection() = false;
3341 // col_type const col = tabular.cellColumn(cur.idx());
3342 // int const t = cur.bv().top_y() + cur.bv().height();
3343 // if (t < yo() + tabular.getHeightOfTabular()) {
3344 // cur.bv().scrollDocView(t);
3345 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3347 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3354 // case LFUN_SCREEN_UP: {
3355 // //if (hasSelection())
3356 // // cur.selection() = false;
3357 // col_type const col = tabular.cellColumn(cur.idx());
3358 // int const t = cur.bv().top_y() + cur.bv().height();
3360 // cur.bv().scrollDocView(t);
3364 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3368 // cur.par() = cur.lastpar();
3369 // cur.pos() = cur.lastpos();
3373 case LFUN_LAYOUT_TABULAR:
3374 InsetTabularMailer(*this).showDialog(&cur.bv());
3377 case LFUN_INSET_DIALOG_UPDATE:
3378 InsetTabularMailer(*this).updateDialog(&cur.bv());
3381 case LFUN_TABULAR_FEATURE:
3382 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3386 // insert file functions
3387 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3388 case LFUN_FILE_INSERT_PLAINTEXT: {
3390 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3391 FileName(to_utf8(cmd.argument())));
3394 cur.recordUndoInset(INSERT_UNDO);
3395 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3396 // content has been replaced,
3397 // so cursor might be invalid
3398 cur.pos() = cur.lastpos();
3399 cur.pit() = cur.lastpit();
3400 bvcur.setCursor(cur);
3407 if (tablemode(cur)) {
3408 if (copySelection(cur)) {
3409 cur.recordUndoInset(DELETE_UNDO);
3414 cell(cur.idx())->dispatch(cur, cmd);
3417 case LFUN_CHAR_DELETE_BACKWARD:
3418 case LFUN_CHAR_DELETE_FORWARD:
3419 if (tablemode(cur)) {
3420 cur.recordUndoInset(DELETE_UNDO);
3424 cell(cur.idx())->dispatch(cur, cmd);
3428 if (!cur.selection())
3430 if (tablemode(cur)) {
3434 cell(cur.idx())->dispatch(cur, cmd);
3437 case LFUN_CLIPBOARD_PASTE:
3438 case LFUN_PRIMARY_SELECTION_PASTE: {
3439 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3440 theClipboard().getAsText() :
3441 theSelection().get();
3444 // pass to InsertPlaintextString, but
3445 // only if we have multi-cell content
3446 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3447 cur.recordUndoInset(INSERT_UNDO);
3448 if (insertPlaintextString(cur.bv(), clip, false)) {
3449 // content has been replaced,
3450 // so cursor might be invalid
3451 cur.pos() = cur.lastpos();
3452 cur.pit() = cur.lastpit();
3453 bvcur.setCursor(cur);
3457 // Let the cell handle normal text
3458 cell(cur.idx())->dispatch(cur, cmd);
3463 if (tabularStackDirty() && theClipboard().isInternal() ||
3464 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3465 cur.recordUndoInset(INSERT_UNDO);
3466 pasteClipboard(cur);
3469 cell(cur.idx())->dispatch(cur, cmd);
3472 case LFUN_FONT_EMPH:
3473 case LFUN_FONT_BOLD:
3474 case LFUN_FONT_ROMAN:
3475 case LFUN_FONT_NOUN:
3476 case LFUN_FONT_ITAL:
3477 case LFUN_FONT_FRAK:
3478 case LFUN_FONT_TYPEWRITER:
3479 case LFUN_FONT_SANS:
3480 case LFUN_FONT_FREE_APPLY:
3481 case LFUN_FONT_FREE_UPDATE:
3482 case LFUN_FONT_SIZE:
3483 case LFUN_FONT_UNDERLINE:
3485 case LFUN_WORD_CAPITALIZE:
3486 case LFUN_WORD_UPCASE:
3487 case LFUN_WORD_LOWCASE:
3488 case LFUN_CHARS_TRANSPOSE:
3489 if (tablemode(cur)) {
3492 getSelection(cur, rs, re, cs, ce);
3493 Cursor tmpcur = cur;
3494 for (row_type i = rs; i <= re; ++i) {
3495 for (col_type j = cs; j <= ce; ++j) {
3496 // cursor follows cell:
3497 tmpcur.idx() = tabular.cellIndex(i, j);
3498 // select this cell only:
3501 tmpcur.resetAnchor();
3502 tmpcur.pit() = tmpcur.lastpit();
3503 tmpcur.pos() = tmpcur.top().lastpos();
3504 tmpcur.setCursor(tmpcur);
3505 tmpcur.setSelection();
3506 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3511 cell(cur.idx())->dispatch(cur, cmd);
3515 // we try to handle this event in the insets dispatch function.
3516 cell(cur.idx())->dispatch(cur, cmd);
3522 // function sets an object as defined in func_status.h:
3523 // states OK, Unknown, Disabled, On, Off.
3524 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3525 FuncStatus & status) const
3527 switch (cmd.action) {
3528 case LFUN_TABULAR_FEATURE: {
3529 int action = Tabular::LAST_ACTION;
3531 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3532 string const tmp = tabularFeature[i].feature;
3533 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3534 action = tabularFeature[i].action;
3538 if (action == Tabular::LAST_ACTION) {
3540 status.unknown(true);
3544 string const argument
3545 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3547 row_type sel_row_start = 0;
3548 row_type sel_row_end = 0;
3549 col_type sel_col_start = 0;
3550 col_type sel_col_end = 0;
3551 Tabular::ltType dummyltt;
3554 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3557 case Tabular::SET_PWIDTH:
3558 case Tabular::SET_MPWIDTH:
3559 case Tabular::SET_SPECIAL_COLUMN:
3560 case Tabular::SET_SPECIAL_MULTI:
3561 case Tabular::APPEND_ROW:
3562 case Tabular::APPEND_COLUMN:
3563 case Tabular::DELETE_ROW:
3564 case Tabular::DELETE_COLUMN:
3565 case Tabular::COPY_ROW:
3566 case Tabular::COPY_COLUMN:
3567 case Tabular::SET_ALL_LINES:
3568 case Tabular::UNSET_ALL_LINES:
3569 case Tabular::SET_TOP_SPACE:
3570 case Tabular::SET_BOTTOM_SPACE:
3571 case Tabular::SET_INTERLINE_SPACE:
3575 case Tabular::MULTICOLUMN:
3576 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3579 case Tabular::M_TOGGLE_LINE_TOP:
3581 case Tabular::TOGGLE_LINE_TOP:
3582 status.setOnOff(tabular.topLine(cur.idx(), flag));
3585 case Tabular::M_TOGGLE_LINE_BOTTOM:
3587 case Tabular::TOGGLE_LINE_BOTTOM:
3588 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3591 case Tabular::M_TOGGLE_LINE_LEFT:
3593 case Tabular::TOGGLE_LINE_LEFT:
3594 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3597 case Tabular::M_TOGGLE_LINE_RIGHT:
3599 case Tabular::TOGGLE_LINE_RIGHT:
3600 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3603 case Tabular::M_ALIGN_LEFT:
3605 case Tabular::ALIGN_LEFT:
3606 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3609 case Tabular::M_ALIGN_RIGHT:
3611 case Tabular::ALIGN_RIGHT:
3612 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3615 case Tabular::M_ALIGN_CENTER:
3617 case Tabular::ALIGN_CENTER:
3618 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3621 case Tabular::ALIGN_BLOCK:
3622 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3623 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3626 case Tabular::M_VALIGN_TOP:
3628 case Tabular::VALIGN_TOP:
3630 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3633 case Tabular::M_VALIGN_BOTTOM:
3635 case Tabular::VALIGN_BOTTOM:
3637 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3640 case Tabular::M_VALIGN_MIDDLE:
3642 case Tabular::VALIGN_MIDDLE:
3644 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3647 case Tabular::SET_LONGTABULAR:
3648 status.setOnOff(tabular.isLongTabular());
3651 case Tabular::UNSET_LONGTABULAR:
3652 status.setOnOff(!tabular.isLongTabular());
3655 case Tabular::TOGGLE_ROTATE_TABULAR:
3656 case Tabular::SET_ROTATE_TABULAR:
3657 status.setOnOff(tabular.getRotateTabular());
3660 case Tabular::UNSET_ROTATE_TABULAR:
3661 status.setOnOff(!tabular.getRotateTabular());
3664 case Tabular::TOGGLE_ROTATE_CELL:
3665 case Tabular::SET_ROTATE_CELL:
3666 status.setOnOff(!oneCellHasRotationState(false,
3667 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3670 case Tabular::UNSET_ROTATE_CELL:
3671 status.setOnOff(!oneCellHasRotationState(true,
3672 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3675 case Tabular::SET_USEBOX:
3676 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3679 case Tabular::SET_LTFIRSTHEAD:
3680 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3683 case Tabular::UNSET_LTFIRSTHEAD:
3684 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3687 case Tabular::SET_LTHEAD:
3688 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3691 case Tabular::UNSET_LTHEAD:
3692 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3695 case Tabular::SET_LTFOOT:
3696 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3699 case Tabular::UNSET_LTFOOT:
3700 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3703 case Tabular::SET_LTLASTFOOT:
3704 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3707 case Tabular::UNSET_LTLASTFOOT:
3708 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3711 case Tabular::SET_LTNEWPAGE:
3712 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3715 case Tabular::SET_BOOKTABS:
3716 status.setOnOff(tabular.useBookTabs());
3719 case Tabular::UNSET_BOOKTABS:
3720 status.setOnOff(!tabular.useBookTabs());
3725 status.enabled(false);
3731 // These are only enabled inside tabular
3732 case LFUN_CELL_BACKWARD:
3733 case LFUN_CELL_FORWARD:
3734 status.enabled(true);
3737 // disable these with multiple cells selected
3738 case LFUN_INSET_INSERT:
3739 case LFUN_TABULAR_INSERT:
3740 case LFUN_FLEX_INSERT:
3741 case LFUN_FLOAT_INSERT:
3742 case LFUN_FLOAT_WIDE_INSERT:
3743 case LFUN_FOOTNOTE_INSERT:
3744 case LFUN_MARGINALNOTE_INSERT:
3745 case LFUN_MATH_INSERT:
3746 case LFUN_MATH_MODE:
3747 case LFUN_MATH_MUTATE:
3748 case LFUN_MATH_DISPLAY:
3749 case LFUN_NOTE_INSERT:
3750 case LFUN_OPTIONAL_INSERT:
3751 case LFUN_BOX_INSERT:
3752 case LFUN_BRANCH_INSERT:
3753 case LFUN_WRAP_INSERT:
3754 case LFUN_ERT_INSERT: {
3755 if (tablemode(cur)) {
3756 status.enabled(false);
3759 return cell(cur.idx())->getStatus(cur, cmd, status);
3762 // disable in non-fixed-width cells
3764 case LFUN_BREAK_PARAGRAPH:
3765 case LFUN_BREAK_PARAGRAPH_SKIP: {
3766 if (tabular.getPWidth(cur.idx()).zero()) {
3767 status.enabled(false);
3770 return cell(cur.idx())->getStatus(cur, cmd, status);
3774 if (tabularStackDirty() && theClipboard().isInternal()) {
3775 status.enabled(true);
3778 return cell(cur.idx())->getStatus(cur, cmd, status);
3780 case LFUN_INSET_MODIFY:
3781 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3782 status.enabled(true);
3788 // we try to handle this event in the insets dispatch function.
3789 return cell(cur.idx())->getStatus(cur, cmd, status);
3794 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3796 return tabular.latex(os, runparams);
3800 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3802 os << '\n'; // output table on a new line
3803 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3804 tabular.plaintext(os, runparams, dp, false, 0);
3805 return PLAINTEXT_NEWLINE;
3809 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3814 // FIXME: Why not pass a proper DocIterator here?
3816 // if the table is inside a float it doesn't need the informaltable
3817 // wrapper. Search for it.
3818 for (master = owner(); master; master = master->owner())
3819 if (master->lyxCode() == FLOAT_CODE)
3824 os << "<informaltable>";
3827 ret += tabular.docbook(os, runparams);
3829 os << "</informaltable>";
3836 void InsetTabular::validate(LaTeXFeatures & features) const
3838 tabular.validate(features);
3842 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3844 return tabular.getCellInset(idx);
3848 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3850 return tabular.getCellInset(idx);
3854 void InsetTabular::cursorPos(BufferView const & bv,
3855 CursorSlice const & sl, bool boundary, int & x, int & y) const
3857 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3859 // y offset correction
3860 int const row = tabular.cellRow(sl.idx());
3861 for (int i = 0; i <= row; ++i) {
3863 y += tabular.rowAscent(i);
3864 y += tabular.getAdditionalHeight(i);
3867 y += tabular.rowDescent(i);
3870 // x offset correction
3871 int const col = tabular.cellColumn(sl.idx());
3872 int idx = tabular.cellIndex(row, 0);
3873 for (int j = 0; j < col; ++j) {
3874 if (tabular.isPartOfMultiColumn(row, j))
3876 x += tabular.columnWidth(idx);
3879 x += tabular.getBeginningOfTextInCell(idx);
3880 x += ADD_TO_TABULAR_WIDTH;
3885 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3889 Inset const & inset = *tabular.getCellInset(cell);
3890 Point o = bv.coordCache().getInsets().xy(&inset);
3891 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3892 int const xend = xbeg + tabular.columnWidth(cell);
3893 row_type const row = tabular.cellRow(cell);
3894 int const ybeg = o.y_ - tabular.rowAscent(row) -
3895 tabular.getAdditionalHeight(row);
3896 int const yend = o.y_ + tabular.rowDescent(row);
3908 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3909 // << " ybeg=" << ybeg << " yend=" << yend
3910 // << " xx=" << xx << " yy=" << yy
3911 // << " dist=" << xx + yy << endl;
3916 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3918 //lyxerr << "InsetTabular::editXY: " << this << endl;
3919 cur.selection() = false;
3921 cur.idx() = getNearestCell(cur.bv(), x, y);
3923 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3927 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3929 cur.idx() = getNearestCell(cur.bv(), x, y);
3930 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3934 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3936 idx_type idx_min = 0;
3937 int dist_min = numeric_limits<int>::max();
3938 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3939 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3940 int const d = dist(bv, i, x, y);
3951 int InsetTabular::getCellXPos(idx_type const cell) const
3955 for (; !tabular.isFirstCellInRow(c); --c)
3958 for (; c < cell; ++c)
3959 lx += tabular.columnWidth(c);
3965 void InsetTabular::resetPos(Cursor & cur) const
3967 BufferView & bv = cur.bv();
3968 int const maxwidth = bv.workWidth();
3970 if (&cur.inset() != this) {
3974 int const X2 = maxwidth;
3975 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3976 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3977 int const x2 = x1 + tabular.columnWidth(cur.idx());
3980 scx_ = X1 + 20 - x1;
3982 scx_ = X2 - 20 - x2;
3987 cur.updateFlags(Update::Force | Update::FitCursor);
3991 void InsetTabular::moveNextCell(Cursor & cur)
3993 if (isRightToLeft(cur)) {
3994 if (tabular.isFirstCellInRow(cur.idx())) {
3995 row_type const row = tabular.cellRow(cur.idx());
3996 if (row == tabular.rowCount() - 1)
3998 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4005 if (tabular.isLastCell(cur.idx()))
4015 void InsetTabular::movePrevCell(Cursor & cur)
4017 if (isRightToLeft(cur)) {
4018 if (tabular.isLastCellInRow(cur.idx())) {
4019 row_type const row = tabular.cellRow(cur.idx());
4022 cur.idx() = tabular.getFirstCellInRow(row);
4023 cur.idx() = tabular.getCellAbove(cur.idx());
4025 if (tabular.isLastCell(cur.idx()))
4030 if (cur.idx() == 0) // first cell
4034 cur.pit() = cur.lastpit();
4035 cur.pos() = cur.lastpos();
4037 // FIXME: this accesses the position cache before it is initialized
4042 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4044 Tabular::Feature action = Tabular::LAST_ACTION;
4047 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4048 string const tmp = tabularFeature[i].feature;
4050 if (tmp == what.substr(0, tmp.length())) {
4051 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4052 //tabularFeatures[i].feature.length()))
4053 action = tabularFeature[i].action;
4057 if (action == Tabular::LAST_ACTION)
4061 ltrim(what.substr(tabularFeature[i].feature.length()));
4062 tabularFeatures(cur, action, val);
4067 static void checkLongtableSpecial(Tabular::ltType & ltt,
4068 string const & special, bool & flag)
4070 if (special == "dl_above") {
4073 } else if (special == "dl_below") {
4074 ltt.bottomDL = flag;
4076 } else if (special == "empty") {
4085 bool InsetTabular::oneCellHasRotationState(bool rotated,
4086 row_type row_start, row_type row_end,
4087 col_type col_start, col_type col_end) const {
4089 for (row_type i = row_start; i <= row_end; ++i) {
4090 for (col_type j = col_start; j <= col_end; ++j) {
4091 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4100 void InsetTabular::tabularFeatures(Cursor & cur,
4101 Tabular::Feature feature, string const & value)
4103 col_type sel_col_start;
4104 col_type sel_col_end;
4105 row_type sel_row_start;
4106 row_type sel_row_end;
4107 bool setLines = false;
4108 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4109 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4113 case Tabular::M_ALIGN_LEFT:
4114 case Tabular::ALIGN_LEFT:
4115 setAlign = LYX_ALIGN_LEFT;
4118 case Tabular::M_ALIGN_RIGHT:
4119 case Tabular::ALIGN_RIGHT:
4120 setAlign = LYX_ALIGN_RIGHT;
4123 case Tabular::M_ALIGN_CENTER:
4124 case Tabular::ALIGN_CENTER:
4125 setAlign = LYX_ALIGN_CENTER;
4128 case Tabular::ALIGN_BLOCK:
4129 setAlign = LYX_ALIGN_BLOCK;
4132 case Tabular::M_VALIGN_TOP:
4133 case Tabular::VALIGN_TOP:
4134 setVAlign = Tabular::LYX_VALIGN_TOP;
4137 case Tabular::M_VALIGN_BOTTOM:
4138 case Tabular::VALIGN_BOTTOM:
4139 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4142 case Tabular::M_VALIGN_MIDDLE:
4143 case Tabular::VALIGN_MIDDLE:
4144 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4151 cur.recordUndoInset(ATOMIC_UNDO);
4153 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4154 row_type const row = tabular.cellRow(cur.idx());
4155 col_type const column = tabular.cellColumn(cur.idx());
4157 Tabular::ltType ltt;
4161 case Tabular::SET_PWIDTH: {
4162 Length const len(value);
4163 tabular.setColumnPWidth(cur, cur.idx(), len);
4165 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4166 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4170 case Tabular::SET_MPWIDTH:
4171 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4174 case Tabular::SET_SPECIAL_COLUMN:
4175 case Tabular::SET_SPECIAL_MULTI:
4176 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4179 case Tabular::APPEND_ROW:
4180 // append the row into the tabular
4181 tabular.appendRow(cur.idx());
4184 case Tabular::APPEND_COLUMN:
4185 // append the column into the tabular
4186 tabular.appendColumn(cur.idx());
4187 cur.idx() = tabular.cellIndex(row, column);
4190 case Tabular::DELETE_ROW:
4191 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4192 tabular.deleteRow(sel_row_start);
4193 if (sel_row_start >= tabular.rowCount())
4195 cur.idx() = tabular.cellIndex(sel_row_start, column);
4198 cur.selection() = false;
4201 case Tabular::DELETE_COLUMN:
4202 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4203 tabular.deleteColumn(sel_col_start);
4204 if (sel_col_start >= tabular.columnCount())
4206 cur.idx() = tabular.cellIndex(row, sel_col_start);
4209 cur.selection() = false;
4212 case Tabular::COPY_ROW:
4213 tabular.copyRow(row);
4216 case Tabular::COPY_COLUMN:
4217 tabular.copyColumn(column);
4218 cur.idx() = tabular.cellIndex(row, column);
4221 case Tabular::M_TOGGLE_LINE_TOP:
4223 case Tabular::TOGGLE_LINE_TOP: {
4224 bool lineSet = !tabular.topLine(cur.idx(), flag);
4225 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4226 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4228 tabular.cellIndex(i, j),
4233 case Tabular::M_TOGGLE_LINE_BOTTOM:
4235 case Tabular::TOGGLE_LINE_BOTTOM: {
4236 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4237 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4238 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4239 tabular.setBottomLine(
4240 tabular.cellIndex(i, j),
4246 case Tabular::M_TOGGLE_LINE_LEFT:
4248 case Tabular::TOGGLE_LINE_LEFT: {
4249 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4250 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4251 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4252 tabular.setLeftLine(
4253 tabular.cellIndex(i,j),
4259 case Tabular::M_TOGGLE_LINE_RIGHT:
4261 case Tabular::TOGGLE_LINE_RIGHT: {
4262 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4263 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4264 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4265 tabular.setRightLine(tabular.cellIndex(i,j), lineSet, flag);
4269 case Tabular::M_ALIGN_LEFT:
4270 case Tabular::M_ALIGN_RIGHT:
4271 case Tabular::M_ALIGN_CENTER:
4273 case Tabular::ALIGN_LEFT:
4274 case Tabular::ALIGN_RIGHT:
4275 case Tabular::ALIGN_CENTER:
4276 case Tabular::ALIGN_BLOCK:
4277 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4278 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4279 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4282 case Tabular::M_VALIGN_TOP:
4283 case Tabular::M_VALIGN_BOTTOM:
4284 case Tabular::M_VALIGN_MIDDLE:
4286 case Tabular::VALIGN_TOP:
4287 case Tabular::VALIGN_BOTTOM:
4288 case Tabular::VALIGN_MIDDLE:
4289 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4290 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4291 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4294 case Tabular::MULTICOLUMN: {
4295 if (sel_row_start != sel_row_end) {
4296 // FIXME: Need I say it ? This is horrible.
4298 Alert::error(_("Error setting multicolumn"),
4299 _("You cannot set multicolumn vertically."));
4302 if (!cur.selection()) {
4303 // just multicol for one single cell
4304 // check whether we are completely in a multicol
4305 if (tabular.isMultiColumn(cur.idx()))
4306 tabular.unsetMultiColumn(cur.idx());
4308 tabular.setMultiColumn(cur.idx(), 1);
4311 // we have a selection so this means we just add all this
4312 // cells to form a multicolumn cell
4313 idx_type const s_start = cur.selBegin().idx();
4314 idx_type const s_end = cur.selEnd().idx();
4315 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4316 cur.idx() = s_start;
4319 cur.selection() = false;
4323 case Tabular::SET_ALL_LINES:
4325 case Tabular::UNSET_ALL_LINES:
4326 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4327 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4328 tabular.setAllLines(
4329 tabular.cellIndex(i,j), setLines);
4332 case Tabular::SET_LONGTABULAR:
4333 tabular.setLongTabular(true);
4336 case Tabular::UNSET_LONGTABULAR:
4337 tabular.setLongTabular(false);
4340 case Tabular::SET_ROTATE_TABULAR:
4341 tabular.setRotateTabular(true);
4344 case Tabular::UNSET_ROTATE_TABULAR:
4345 tabular.setRotateTabular(false);
4348 case Tabular::TOGGLE_ROTATE_TABULAR:
4349 tabular.setRotateTabular(!tabular.getRotateTabular());
4352 case Tabular::SET_ROTATE_CELL:
4353 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4354 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4355 tabular.setRotateCell(
4356 tabular.cellIndex(i, j), true);
4359 case Tabular::UNSET_ROTATE_CELL:
4360 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4361 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4362 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4365 case Tabular::TOGGLE_ROTATE_CELL:
4367 bool oneNotRotated = oneCellHasRotationState(false,
4368 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4370 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4371 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4372 tabular.setRotateCell(tabular.cellIndex(i, j),
4377 case Tabular::SET_USEBOX: {
4378 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4379 if (val == tabular.getUsebox(cur.idx()))
4380 val = Tabular::BOX_NONE;
4381 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4382 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4383 tabular.setUsebox(tabular.cellIndex(i, j), val);
4387 case Tabular::UNSET_LTFIRSTHEAD:
4389 case Tabular::SET_LTFIRSTHEAD:
4390 tabular.getRowOfLTFirstHead(row, ltt);
4391 checkLongtableSpecial(ltt, value, flag);
4392 tabular.setLTHead(row, flag, ltt, true);
4395 case Tabular::UNSET_LTHEAD:
4397 case Tabular::SET_LTHEAD:
4398 tabular.getRowOfLTHead(row, ltt);
4399 checkLongtableSpecial(ltt, value, flag);
4400 tabular.setLTHead(row, flag, ltt, false);
4403 case Tabular::UNSET_LTFOOT:
4405 case Tabular::SET_LTFOOT:
4406 tabular.getRowOfLTFoot(row, ltt);
4407 checkLongtableSpecial(ltt, value, flag);
4408 tabular.setLTFoot(row, flag, ltt, false);
4411 case Tabular::UNSET_LTLASTFOOT:
4413 case Tabular::SET_LTLASTFOOT:
4414 tabular.getRowOfLTLastFoot(row, ltt);
4415 checkLongtableSpecial(ltt, value, flag);
4416 tabular.setLTFoot(row, flag, ltt, true);
4419 case Tabular::SET_LTNEWPAGE:
4420 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4423 case Tabular::SET_BOOKTABS:
4424 tabular.setBookTabs(true);
4427 case Tabular::UNSET_BOOKTABS:
4428 tabular.setBookTabs(false);
4431 case Tabular::SET_TOP_SPACE: {
4433 if (value == "default")
4434 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4435 tabular.row_info[i].top_space_default = true;
4436 else if (isValidLength(value, &len))
4437 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4438 tabular.row_info[i].top_space_default = false;
4439 tabular.row_info[i].top_space = len;
4442 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4443 tabular.row_info[i].top_space_default = false;
4444 tabular.row_info[i].top_space = len;
4449 case Tabular::SET_BOTTOM_SPACE: {
4451 if (value == "default")
4452 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4453 tabular.row_info[i].bottom_space_default = true;
4454 else if (isValidLength(value, &len))
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4456 tabular.row_info[i].bottom_space_default = false;
4457 tabular.row_info[i].bottom_space = len;
4460 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4461 tabular.row_info[i].bottom_space_default = false;
4462 tabular.row_info[i].bottom_space = len;
4467 case Tabular::SET_INTERLINE_SPACE: {
4469 if (value == "default")
4470 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4471 tabular.row_info[i].interline_space_default = true;
4472 else if (isValidLength(value, &len))
4473 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4474 tabular.row_info[i].interline_space_default = false;
4475 tabular.row_info[i].interline_space = len;
4478 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4479 tabular.row_info[i].interline_space_default = false;
4480 tabular.row_info[i].interline_space = len;
4485 // dummy stuff just to avoid warnings
4486 case Tabular::LAST_ACTION:
4492 bool InsetTabular::showInsetDialog(BufferView * bv) const
4494 InsetTabularMailer(*this).showDialog(bv);
4499 void InsetTabular::openLayoutDialog(BufferView * bv) const
4501 InsetTabularMailer(*this).showDialog(bv);
4505 bool InsetTabular::copySelection(Cursor & cur)
4507 if (!cur.selection())
4512 getSelection(cur, rs, re, cs, ce);
4514 paste_tabular.reset(new Tabular(tabular));
4516 for (row_type i = 0; i < rs; ++i)
4517 paste_tabular->deleteRow(0);
4519 row_type const rows = re - rs + 1;
4520 while (paste_tabular->rowCount() > rows)
4521 paste_tabular->deleteRow(rows);
4523 paste_tabular->setTopLine(0, true, true);
4524 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4527 for (col_type i = 0; i < cs; ++i)
4528 paste_tabular->deleteColumn(0);
4530 col_type const columns = ce - cs + 1;
4531 while (paste_tabular->columnCount() > columns)
4532 paste_tabular->deleteColumn(columns);
4534 paste_tabular->setLeftLine(0, true, true);
4535 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4538 odocstringstream os;
4539 OutputParams const runparams(0);
4540 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4541 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4542 cap::copySelection(cur, os.str());
4544 // mark tabular stack dirty
4545 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4546 // when we (hopefully) have a one-for-all paste mechanism.
4547 // This must be called after cap::copySelection.
4548 dirtyTabularStack(true);
4554 bool InsetTabular::pasteClipboard(Cursor & cur)
4558 col_type const actcol = tabular.cellColumn(cur.idx());
4559 row_type const actrow = tabular.cellRow(cur.idx());
4560 for (row_type r1 = 0, r2 = actrow;
4561 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4563 for (col_type c1 = 0, c2 = actcol;
4564 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4566 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4567 tabular.isPartOfMultiColumn(r2, c2))
4569 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4573 if (tabular.isPartOfMultiColumn(r2, c2)) {
4577 shared_ptr<InsetText> inset(
4578 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4579 tabular.setCellInset(r2, c2, inset);
4580 // FIXME: change tracking (MG)
4581 inset->setChange(Change(cur.buffer().params().trackChanges ?
4582 Change::INSERTED : Change::UNCHANGED));
4590 void InsetTabular::cutSelection(Cursor & cur)
4592 if (!cur.selection())
4597 getSelection(cur, rs, re, cs, ce);
4598 for (row_type i = rs; i <= re; ++i) {
4599 for (col_type j = cs; j <= ce; ++j) {
4600 shared_ptr<InsetText> t
4601 = cell(tabular.cellIndex(i, j));
4602 if (cur.buffer().params().trackChanges)
4603 // FIXME: Change tracking (MG)
4604 t->setChange(Change(Change::DELETED));
4610 // cursor position might be invalid now
4611 if (cur.pit() > cur.lastpit())
4612 cur.pit() = cur.lastpit();
4613 if (cur.pos() > cur.lastpos())
4614 cur.pos() = cur.lastpos();
4615 cur.clearSelection();
4619 bool InsetTabular::isRightToLeft(Cursor & cur) const
4621 BOOST_ASSERT(cur.depth() > 1);
4622 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4623 pos_type const parentpos = cur[cur.depth() - 2].pos();
4624 return parentpar.getFontSettings(cur.bv().buffer().params(),
4625 parentpos).language()->rightToLeft();
4629 void InsetTabular::getSelection(Cursor & cur,
4630 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4632 CursorSlice const & beg = cur.selBegin();
4633 CursorSlice const & end = cur.selEnd();
4634 cs = tabular.cellColumn(beg.idx());
4635 ce = tabular.cellColumn(end.idx());
4638 cs = tabular.cellColumn(end.idx());
4640 ce = tabular.cellRightColumn(end.idx());
4643 rs = tabular.cellRow(beg.idx());
4644 re = tabular.cellRow(end.idx());
4650 Text * InsetTabular::getText(int idx) const
4652 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4656 void InsetTabular::setChange(Change const & change)
4658 for (idx_type idx = 0; idx < nargs(); ++idx)
4659 cell(idx)->setChange(change);
4663 void InsetTabular::acceptChanges(BufferParams const & bparams)
4665 for (idx_type idx = 0; idx < nargs(); ++idx)
4666 cell(idx)->acceptChanges(bparams);
4670 void InsetTabular::rejectChanges(BufferParams const & bparams)
4672 for (idx_type idx = 0; idx < nargs(); ++idx)
4673 cell(idx)->rejectChanges(bparams);
4677 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4679 return tabular.getPWidth(cell).zero();
4683 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4686 if (buf.length() <= 0)
4691 col_type maxCols = 1;
4692 size_t const len = buf.length();
4696 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4704 maxCols = max(cols, maxCols);
4710 maxCols = max(cols, maxCols);
4716 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4717 loctab = paste_tabular.get();
4719 dirtyTabularStack(true);
4722 cell = bv.cursor().idx();
4723 ocol = tabular.cellColumn(cell);
4724 row = tabular.cellRow(cell);
4728 idx_type const cells = loctab->cellCount();
4731 rows = loctab->rowCount();
4732 col_type const columns = loctab->columnCount();
4734 while (cell < cells && p < len && row < rows &&
4735 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4741 // we can only set this if we are not too far right
4742 if (cols < columns) {
4743 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4744 Font const font = bv.textMetrics(&inset->text_).
4746 inset->setText(buf.substr(op, p - op), font,
4747 buffer().params().trackChanges);
4753 // we can only set this if we are not too far right
4754 if (cols < columns) {
4755 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4756 Font const font = bv.textMetrics(&inset->text_).
4758 inset->setText(buf.substr(op, p - op), font,
4759 buffer().params().trackChanges);
4764 cell = loctab->cellIndex(row, cols);
4770 // check for the last cell if there is no trailing '\n'
4771 if (cell < cells && op < len) {
4772 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4773 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4774 inset->setText(buf.substr(op, len - op), font,
4775 buffer().params().trackChanges);
4781 void InsetTabular::addPreview(PreviewLoader & loader) const
4783 row_type const rows = tabular.rowCount();
4784 col_type const columns = tabular.columnCount();
4785 for (row_type i = 0; i < rows; ++i) {
4786 for (col_type j = 0; j < columns; ++j)
4787 tabular.getCellInset(i, j)->addPreview(loader);
4792 bool InsetTabular::tablemode(Cursor & cur) const
4794 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4798 bool InsetTabular::completionSupported(Cursor const & cur) const
4800 Cursor const & bvCur = cur.bv().cursor();
4801 if (&bvCur.inset() != this)
4803 return cur.text()->completionSupported(cur);
4807 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4809 return completionSupported(cur);
4813 bool InsetTabular::automaticInlineCompletion() const
4815 return lyxrc.completion_inline_text;
4819 bool InsetTabular::automaticPopupCompletion() const
4821 return lyxrc.completion_popup_text;
4825 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4827 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4831 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4833 if (!completionSupported(cur))
4835 return cur.text()->completionPrefix(cur);
4839 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4841 if (!completionSupported(cur))
4844 return cur.text()->insertCompletion(cur, s, finished);
4848 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4849 Dimension & dim) const
4851 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4852 tm.completionPosAndDim(cur, x, y, dim);
4856 string const InsetTabularMailer::name_("tabular");
4858 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4859 : inset_(const_cast<InsetTabular &>(inset))
4863 string const InsetTabularMailer::inset2string(Buffer const &) const
4865 return params2string(inset_);
4869 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4871 istringstream data(in);
4873 lex.setStream(data);
4880 if (!lex || token != name_)
4881 return print_mailer_error("InsetTabularMailer", in, 1,
4884 // This is part of the inset proper that is usually swallowed
4885 // by Buffer::readInset
4887 if (!lex || token != "Tabular")
4888 return print_mailer_error("InsetTabularMailer", in, 2, "Tabular");
4894 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4897 data << name_ << ' ';
4899 data << "\\end_inset\n";