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"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
38 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "paragraph_funcs.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
48 #include "support/convert.h"
49 #include "support/lstrings.h"
51 #include "frontends/alert.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
64 using std::istringstream;
67 using std::ostringstream;
72 #ifndef CXX_GLOBAL_CSTD
76 using boost::shared_ptr;
77 using boost::dynamic_pointer_cast;
81 using support::prefixIs;
84 using support::suffixIs;
86 using cap::dirtyTabularStack;
87 using cap::tabularStackDirty;
89 using graphics::PreviewLoader;
91 using frontend::Painter;
92 using frontend::Clipboard;
94 namespace Alert = frontend::Alert;
99 int const ADD_TO_HEIGHT = 2;
100 int const ADD_TO_TABULAR_WIDTH = 2;
101 int const default_line_space = 10;
102 int const WIDTH_OF_LINE = 5;
106 boost::scoped_ptr<Tabular> paste_tabular;
109 struct TabularFeature {
110 Tabular::Feature action;
115 TabularFeature tabularFeature[] =
117 { Tabular::APPEND_ROW, "append-row" },
118 { Tabular::APPEND_COLUMN, "append-column" },
119 { Tabular::DELETE_ROW, "delete-row" },
120 { Tabular::DELETE_COLUMN, "delete-column" },
121 { Tabular::COPY_ROW, "copy-row" },
122 { Tabular::COPY_COLUMN, "copy-column" },
123 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
124 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
125 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
126 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
127 { Tabular::ALIGN_LEFT, "align-left" },
128 { Tabular::ALIGN_RIGHT, "align-right" },
129 { Tabular::ALIGN_CENTER, "align-center" },
130 { Tabular::ALIGN_BLOCK, "align-block" },
131 { Tabular::VALIGN_TOP, "valign-top" },
132 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
133 { Tabular::VALIGN_MIDDLE, "valign-middle" },
134 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
135 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
136 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
137 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
138 { Tabular::M_ALIGN_LEFT, "m-align-left" },
139 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
140 { Tabular::M_ALIGN_CENTER, "m-align-center" },
141 { Tabular::M_VALIGN_TOP, "m-valign-top" },
142 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
143 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
144 { Tabular::MULTICOLUMN, "multicolumn" },
145 { Tabular::SET_ALL_LINES, "set-all-lines" },
146 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
147 { Tabular::SET_LONGTABULAR, "set-longtabular" },
148 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
149 { Tabular::SET_PWIDTH, "set-pwidth" },
150 { Tabular::SET_MPWIDTH, "set-mpwidth" },
151 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
152 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
153 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
154 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
155 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
156 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
157 { Tabular::SET_USEBOX, "set-usebox" },
158 { Tabular::SET_LTHEAD, "set-lthead" },
159 { Tabular::UNSET_LTHEAD, "unset-lthead" },
160 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
161 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
162 { Tabular::SET_LTFOOT, "set-ltfoot" },
163 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
164 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
165 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
166 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
167 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
168 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
169 { Tabular::SET_BOOKTABS, "set-booktabs" },
170 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
171 { Tabular::SET_TOP_SPACE, "set-top-space" },
172 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
173 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
174 { Tabular::LAST_ACTION, "" }
178 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
180 FeatureEqual(Tabular::Feature feature)
181 : feature_(feature) {}
182 bool operator()(TabularFeature const & tf) const {
183 return tf.action == feature_;
186 Tabular::Feature feature_;
191 string const write_attribute(string const & name, T const & t)
193 string const s = tostr(t);
194 return s.empty() ? s : " " + name + "=\"" + s + "\"";
198 string const write_attribute(string const & name, string const & t)
200 return t.empty() ? t : " " + name + "=\"" + t + "\"";
205 string const write_attribute(string const & name, docstring const & t)
207 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
212 string const write_attribute(string const & name, bool const & b)
214 // we write only true attribute values so we remove a bit of the
215 // file format bloat for tabulars.
216 return b ? write_attribute(name, convert<string>(b)) : string();
221 string const write_attribute(string const & name, int const & i)
223 // we write only true attribute values so we remove a bit of the
224 // file format bloat for tabulars.
225 return i ? write_attribute(name, convert<string>(i)) : string();
230 string const write_attribute(string const & name, Tabular::idx_type const & i)
232 // we write only true attribute values so we remove a bit of the
233 // file format bloat for tabulars.
234 return i ? write_attribute(name, convert<string>(i)) : string();
239 string const write_attribute(string const & name, Length const & value)
241 // we write only the value if we really have one same reson as above.
242 return value.zero() ? string() : write_attribute(name, value.asString());
246 string const tostr(LyXAlignment const & num)
251 case LYX_ALIGN_BLOCK:
255 case LYX_ALIGN_CENTER:
257 case LYX_ALIGN_RIGHT:
259 case LYX_ALIGN_LAYOUT:
261 case LYX_ALIGN_SPECIAL:
268 string const tostr(Tabular::VAlignment const & num)
271 case Tabular::LYX_VALIGN_TOP:
273 case Tabular::LYX_VALIGN_MIDDLE:
275 case Tabular::LYX_VALIGN_BOTTOM:
282 string const tostr(Tabular::BoxType const & num)
285 case Tabular::BOX_NONE:
287 case Tabular::BOX_PARBOX:
289 case Tabular::BOX_MINIPAGE:
296 // I would have liked a fromstr template a lot better. (Lgb)
297 bool string2type(string const str, LyXAlignment & num)
300 num = LYX_ALIGN_NONE;
301 else if (str == "block")
302 num = LYX_ALIGN_BLOCK;
303 else if (str == "left")
304 num = LYX_ALIGN_LEFT;
305 else if (str == "center")
306 num = LYX_ALIGN_CENTER;
307 else if (str == "right")
308 num = LYX_ALIGN_RIGHT;
315 bool string2type(string const str, Tabular::VAlignment & num)
318 num = Tabular::LYX_VALIGN_TOP;
319 else if (str == "middle" )
320 num = Tabular::LYX_VALIGN_MIDDLE;
321 else if (str == "bottom")
322 num = Tabular::LYX_VALIGN_BOTTOM;
329 bool string2type(string const str, Tabular::BoxType & num)
332 num = Tabular::BOX_NONE;
333 else if (str == "parbox")
334 num = Tabular::BOX_PARBOX;
335 else if (str == "minipage")
336 num = Tabular::BOX_MINIPAGE;
343 bool string2type(string const str, bool & num)
347 else if (str == "false")
355 bool getTokenValue(string const & str, char const * token, string & ret)
358 size_t token_length = strlen(token);
359 string::size_type pos = str.find(token);
361 if (pos == string::npos || pos + token_length + 1 >= str.length()
362 || str[pos + token_length] != '=')
364 pos += token_length + 1;
366 if (ch != '"' && ch != '\'') { // only read till next space
370 while (pos < str.length() - 1 && str[++pos] != ch)
377 bool getTokenValue(string const & str, char const * token, docstring & ret)
380 bool const success = getTokenValue(str, token, tmp);
381 ret = from_utf8(tmp);
386 bool getTokenValue(string const & str, char const * token, int & num)
390 if (!getTokenValue(str, token, tmp))
392 num = convert<int>(tmp);
397 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::VAlignment & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token,
413 Tabular::BoxType & num)
416 return getTokenValue(str, token, tmp) && string2type(tmp, num);
420 bool getTokenValue(string const & str, char const * token, bool & flag)
422 // set the flag always to false as this should be the default for bools
423 // not in the file-format.
426 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
430 bool getTokenValue(string const & str, char const * token, Length & len)
432 // set the length to be zero() as default as this it should be if not
433 // in the file format.
436 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
440 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
445 if (!getTokenValue(str, token, tmp))
447 if (tmp == "default") {
451 return isValidLength(tmp, &len);
455 void l_getline(istream & is, string & str)
458 while (str.empty()) {
460 if (!str.empty() && str[str.length() - 1] == '\r')
461 str.erase(str.length() - 1);
468 string const featureAsString(Tabular::Feature feature)
470 TabularFeature * end = tabularFeature +
471 sizeof(tabularFeature) / sizeof(TabularFeature);
472 TabularFeature * it = std::find_if(tabularFeature, end,
473 FeatureEqual(feature));
474 return (it == end) ? string() : it->feature;
479 /////////////////////////////////////////////////////////////////////
483 /////////////////////////////////////////////////////////////////////
486 Tabular::cellstruct::cellstruct(BufferParams const & bp)
489 multicolumn(Tabular::CELL_NORMAL),
490 alignment(LYX_ALIGN_CENTER),
491 valignment(LYX_VALIGN_TOP),
498 inset(new InsetText(bp))
502 Tabular::cellstruct::cellstruct(cellstruct const & cs)
505 multicolumn(cs.multicolumn),
506 alignment(cs.alignment),
507 valignment(cs.valignment),
508 top_line(cs.top_line),
509 bottom_line(cs.bottom_line),
510 left_line(cs.left_line),
511 right_line(cs.right_line),
514 align_special(cs.align_special),
516 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
520 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
527 void Tabular::cellstruct::swap(cellstruct & rhs)
529 std::swap(cellno, rhs.cellno);
530 std::swap(width, rhs.width);
531 std::swap(multicolumn, rhs.multicolumn);
532 std::swap(alignment, rhs.alignment);
533 std::swap(valignment, rhs.valignment);
534 std::swap(top_line, rhs.top_line);
535 std::swap(bottom_line, rhs.bottom_line);
536 std::swap(left_line, rhs.left_line);
537 std::swap(right_line, rhs.right_line);
538 std::swap(usebox, rhs.usebox);
539 std::swap(rotate, rhs.rotate);
540 std::swap(align_special, rhs.align_special);
541 p_width.swap(rhs.p_width);
542 inset.swap(rhs.inset);
546 Tabular::rowstruct::rowstruct()
551 top_space_default(false),
552 bottom_space_default(false),
553 interline_space_default(false),
562 Tabular::columnstruct::columnstruct()
563 : alignment(LYX_ALIGN_CENTER),
564 valignment(LYX_VALIGN_TOP),
572 Tabular::ltType::ltType()
579 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
580 col_type columns_arg)
582 init(bp, rows_arg, columns_arg);
586 // activates all lines and sets all widths to 0
587 void Tabular::init(BufferParams const & bp, row_type rows_arg,
588 col_type columns_arg)
590 row_info = row_vector(rows_arg);
591 column_info = column_vector(columns_arg);
592 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
593 row_info.reserve(10);
594 column_info.reserve(10);
595 cell_info.reserve(100);
597 for (row_type i = 0; i < rows_arg; ++i)
598 cell_info[i].back().right_line = true;
599 row_info.back().bottom_line = true;
600 row_info.front().bottom_line = true;
601 column_info.back().right_line = true;
602 is_long_tabular = false;
604 use_booktabs = false;
608 void Tabular::fixCellNums()
611 for (row_type i = 0; i < rowCount(); ++i) {
612 for (col_type j = 0; j < columnCount(); ++j) {
613 // When debugging it can be nice to set
615 cell_info[i][j].inset->setDrawFrame(false);
616 cell_info[i][j].cellno = cellno++;
618 cell_info[i].back().right_line = true;
625 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
627 row_type const row = cellRow(cell);
629 row_vector::iterator rit = row_info.begin() + row;
630 row_info.insert(rit, rowstruct());
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, cellstruct(bp)));
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(BufferParams const & bp, 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]);
674 for (col_type j = 0; j < columnCount(); ++j)
675 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
681 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
683 col_type const column = cellColumn(cell);
684 col_type const ncols = columnCount();
685 column_vector::iterator cit = column_info.begin() + column + 1;
686 column_info.insert(cit, columnstruct());
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, cellstruct(bp));
693 // care about multicolumns
694 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
695 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
697 if (column + 2 >= ncols
698 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
699 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
702 for (row_type i = 0; i < rowCount(); ++i) {
703 cell_info[i][column + 1].inset->clear();
705 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
711 void Tabular::deleteColumn(col_type const column)
713 // Not allowed to delete last column
714 if (columnCount() == 1)
717 column_info.erase(column_info.begin() + column);
718 for (row_type i = 0; i < rowCount(); ++i) {
719 // Care about multicolumn cells
720 if (column + 1 < columnCount() &&
721 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
722 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
723 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
725 cell_info[i].erase(cell_info[i].begin() + column);
731 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
733 column_info.insert(column_info.begin() + column, column_info[column]);
735 for (row_type i = 0; i < rowCount(); ++i)
736 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
739 for (row_type i = 0; i < rowCount(); ++i)
740 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
745 void Tabular::updateIndexes()
748 // Count only non-multicol cells plus begin multicol
749 // cells, ignore non-begin multicol cells:
750 for (row_type row = 0; row < rowCount(); ++row) {
751 for (col_type column = 0; column < columnCount(); ++column) {
752 if (cell_info[row][column].multicolumn
753 != Tabular::CELL_PART_OF_MULTICOLUMN)
755 BOOST_ASSERT(numberofcells != 0);
756 cell_info[row][column].cellno =
761 rowofcell.resize(numberofcells);
762 columnofcell.resize(numberofcells);
767 c < numberofcells && row < rowCount() && column < columnCount();) {
769 columnofcell[c] = column;
773 } while (column < columnCount() &&
774 cell_info[row][column].multicolumn
775 == Tabular::CELL_PART_OF_MULTICOLUMN);
777 if (column == columnCount()) {
783 for (row_type row = 0; row < rowCount(); ++row) {
784 for (col_type column = 0; column < columnCount(); ++column) {
785 if (isPartOfMultiColumn(row,column))
787 cell_info[row][column].inset->setAutoBreakRows(
788 !getPWidth(cellIndex(row, column)).zero());
794 Tabular::idx_type Tabular::cellCount() const
796 return numberofcells;
800 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
802 row_type const row = cellRow(cell);
804 for (col_type i = 0; i < columnCount(); ++i)
805 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
811 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
813 if (!wholerow && isMultiColumn(cell) &&
814 !(use_booktabs && cellRow(cell) == 0))
815 return cellinfo_of_cell(cell).top_line;
816 return row_info[cellRow(cell)].top_line;
820 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
822 if (!wholerow && isMultiColumn(cell) &&
823 !(use_booktabs && isLastRow(cell)))
824 return cellinfo_of_cell(cell).bottom_line;
825 return row_info[cellRow(cell)].bottom_line;
829 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
833 if (!wholecolumn && isMultiColumn(cell) &&
834 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
835 return cellinfo_of_cell(cell).left_line;
836 return column_info[cellColumn(cell)].left_line;
840 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
844 if (!wholecolumn && isMultiColumn(cell) &&
845 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
846 return cellinfo_of_cell(cell).right_line;
847 return column_info[cellRightColumn(cell)].right_line;
851 bool Tabular::topAlreadyDrawn(idx_type cell) const
853 row_type row = cellRow(cell);
854 if (row > 0 && !getAdditionalHeight(row)) {
855 col_type column = cellColumn(cell);
858 && cell_info[row][column].multicolumn
859 == Tabular::CELL_PART_OF_MULTICOLUMN)
861 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
862 return row_info[row].bottom_line;
864 return cell_info[row][column].bottom_line;
870 bool Tabular::leftAlreadyDrawn(idx_type cell) const
872 col_type column = cellColumn(cell);
874 row_type row = cellRow(cell);
876 (cell_info[row][column].multicolumn ==
877 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
878 if (getAdditionalWidth(cell_info[row][column].cellno))
880 return rightLine(cell_info[row][column].cellno);
886 bool Tabular::isLastRow(idx_type cell) const
888 return cellRow(cell) == rowCount() - 1;
892 int Tabular::getAdditionalHeight(row_type row) const
894 if (!row || row >= rowCount())
900 for (col_type column = 0; column < columnCount() && bottom; ++column) {
901 switch (cell_info[row - 1][column].multicolumn) {
902 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
903 bottom = cell_info[row - 1][column].bottom_line;
905 case Tabular::CELL_NORMAL:
906 bottom = row_info[row - 1].bottom_line;
909 for (col_type column = 0; column < columnCount() && top; ++column) {
910 switch (cell_info[row][column].multicolumn) {
911 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
912 top = cell_info[row][column].top_line;
914 case Tabular::CELL_NORMAL:
915 top = row_info[row].top_line;
918 int const interline_space = row_info[row - 1].interline_space_default ?
920 row_info[row - 1].interline_space.inPixels(width());
922 return interline_space + WIDTH_OF_LINE;
923 return interline_space;
927 int Tabular::getAdditionalWidth(idx_type cell) const
929 // internally already set in setCellWidth
930 // used to get it back in text.cpp
931 col_type const col = cellRightColumn(cell);
932 row_type const row = cellRow(cell);
933 if (col < columnCount() - 1 && rightLine(cell) &&
934 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
936 return WIDTH_OF_LINE;
942 // returns the maximum over all rows
943 int Tabular::columnWidth(idx_type cell) const
945 col_type const column1 = cellColumn(cell);
946 col_type const column2 = cellRightColumn(cell);
948 for (col_type i = column1; i <= column2; ++i)
949 result += column_info[i].width;
954 int Tabular::width() const
957 for (col_type i = 0; i < columnCount(); ++i)
958 width += column_info[i].width;
963 // returns true if a complete update is necessary, otherwise false
964 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
966 if (!isMultiColumn(cell))
969 row_type const row = cellRow(cell);
970 col_type const column1 = cellColumn(cell);
971 col_type const column2 = cellRightColumn(cell);
972 int const old_val = cell_info[row][column2].width;
974 // first set columns to 0 so we can calculate the right width
975 for (col_type i = column1; i <= column2; ++i) {
976 cell_info[row][i].width = 0;
978 // set the width to MAX_WIDTH until width > 0
979 int width = new_width + 2 * WIDTH_OF_LINE;
980 col_type i = column1;
981 for (; i < column2 && width > column_info[i].width; ++i) {
982 cell_info[row][i].width = column_info[i].width;
983 width -= column_info[i].width;
986 cell_info[row][i].width = width;
988 if (old_val != cell_info[row][column2].width) {
989 // in this case we have to recalculate all multicolumn cells which
990 // have this column as one of theirs but not as last one
991 calculate_width_of_column_NMC(i);
992 recalculateMulticolumnsOfColumn(i);
993 calculate_width_of_column(i);
999 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1001 // the last column does not have to be recalculated because all
1002 // multicolumns will have here there last multicolumn cell which
1003 // always will have the whole rest of the width of the cell.
1004 if (columnCount() < 2 || column > (columnCount() - 2))
1006 for (row_type row = 0; row < rowCount(); ++row) {
1007 int mc = cell_info[row][column].multicolumn;
1008 int nmc = cell_info[row][column+1].multicolumn;
1009 // we only have to update multicolumns which do not have this
1010 // column as their last column!
1011 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1012 (mc == CELL_PART_OF_MULTICOLUMN &&
1013 nmc == CELL_PART_OF_MULTICOLUMN))
1015 idx_type const cellno = cell_info[row][column].cellno;
1016 setWidthOfMulticolCell(cellno,
1017 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1023 void Tabular::setCellWidth(idx_type cell, int new_width)
1025 row_type const row = cellRow(cell);
1026 col_type const column1 = cellColumn(cell);
1031 if (rightLine(cell_info[row][column1].cellno, true) &&
1032 column1 < columnCount() - 1 &&
1033 leftLine(cell_info[row][column1+1].cellno, true))
1035 add_width = WIDTH_OF_LINE;
1038 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1041 if (isMultiColumnReal(cell)) {
1042 tmp = setWidthOfMulticolCell(cell, new_width);
1044 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1045 cell_info[row][column1].width = width;
1046 tmp = calculate_width_of_column_NMC(column1);
1048 recalculateMulticolumnsOfColumn(column1);
1051 for (col_type i = 0; i < columnCount(); ++i)
1052 calculate_width_of_column(i);
1057 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1060 if (!isMultiColumn(cell) || onlycolumn)
1061 column_info[cellColumn(cell)].alignment = align;
1063 cellinfo_of_cell(cell).alignment = align;
1067 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1070 if (!isMultiColumn(cell) || onlycolumn)
1071 column_info[cellColumn(cell)].valignment = align;
1073 cellinfo_of_cell(cell).valignment = align;
1080 * Allow line and paragraph breaks for fixed width cells or disallow them,
1081 * merge cell paragraphs and reset layout to standard for variable width
1084 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1086 inset->setAutoBreakRows(fixedWidth);
1090 // merge all paragraphs to one
1091 BufferParams const & bp = cur.bv().buffer().params();
1092 while (inset->paragraphs().size() > 1)
1093 mergeParagraph(bp, inset->paragraphs(), 0);
1097 // undo information has already been recorded
1098 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1099 bp.getTextClass().defaultLayoutName());
1106 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1107 Length const & width)
1109 col_type const j = cellColumn(cell);
1111 column_info[j].p_width = width;
1112 for (row_type i = 0; i < rowCount(); ++i) {
1113 idx_type const cell = cellIndex(i, j);
1114 // because of multicolumns
1115 toggleFixedWidth(cur, getCellInset(cell).get(),
1116 !getPWidth(cell).zero());
1118 // cur paragraph can become invalid after paragraphs were merged
1119 if (cur.pit() > cur.lastpit())
1120 cur.pit() = cur.lastpit();
1121 // cur position can become invalid after newlines were removed
1122 if (cur.pos() > cur.lastpos())
1123 cur.pos() = cur.lastpos();
1127 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1128 Length const & width)
1130 if (!isMultiColumn(cell))
1133 cellinfo_of_cell(cell).p_width = width;
1134 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1135 // cur paragraph can become invalid after paragraphs were merged
1136 if (cur.pit() > cur.lastpit())
1137 cur.pit() = cur.lastpit();
1138 // cur position can become invalid after newlines were removed
1139 if (cur.pos() > cur.lastpos())
1140 cur.pos() = cur.lastpos();
1145 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1146 Tabular::Feature what)
1148 if (what == SET_SPECIAL_MULTI)
1149 cellinfo_of_cell(cell).align_special = special;
1151 column_info[cellColumn(cell)].align_special = special;
1155 void Tabular::setAllLines(idx_type cell, bool line)
1157 setTopLine(cell, line);
1158 setBottomLine(cell, line);
1159 setRightLine(cell, line);
1160 setLeftLine(cell, line);
1164 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1166 row_type const row = cellRow(cell);
1167 if (wholerow || !isMultiColumn(cell))
1168 row_info[row].top_line = line;
1170 cellinfo_of_cell(cell).top_line = line;
1174 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1176 if (wholerow || !isMultiColumn(cell))
1177 row_info[cellRow(cell)].bottom_line = line;
1179 cellinfo_of_cell(cell).bottom_line = line;
1183 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1185 if (wholecolumn || !isMultiColumn(cell))
1186 column_info[cellColumn(cell)].left_line = line;
1188 cellinfo_of_cell(cell).left_line = line;
1192 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1194 if (wholecolumn || !isMultiColumn(cell))
1195 column_info[cellRightColumn(cell)].right_line = line;
1197 cellinfo_of_cell(cell).right_line = line;
1201 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1203 if (!onlycolumn && isMultiColumn(cell))
1204 return cellinfo_of_cell(cell).alignment;
1205 return column_info[cellColumn(cell)].alignment;
1210 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1212 if (!onlycolumn && isMultiColumn(cell))
1213 return cellinfo_of_cell(cell).valignment;
1214 return column_info[cellColumn(cell)].valignment;
1218 Length const Tabular::getPWidth(idx_type cell) const
1220 if (isMultiColumn(cell))
1221 return cellinfo_of_cell(cell).p_width;
1222 return column_info[cellColumn(cell)].p_width;
1226 Length const Tabular::getColumnPWidth(idx_type cell) const
1228 return column_info[cellColumn(cell)].p_width;
1232 Length const Tabular::getMColumnPWidth(idx_type cell) const
1234 if (isMultiColumn(cell))
1235 return cellinfo_of_cell(cell).p_width;
1240 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1242 if (what == SET_SPECIAL_MULTI)
1243 return cellinfo_of_cell(cell).align_special;
1244 return column_info[cellColumn(cell)].align_special;
1248 int Tabular::cellWidth(idx_type cell) const
1250 row_type const row = cellRow(cell);
1251 col_type const column1 = cellColumn(cell);
1252 col_type const column2 = cellRightColumn(cell);
1254 for (col_type i = column1; i <= column2; ++i)
1255 result += cell_info[row][i].width;
1260 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1264 switch (getAlignment(cell)) {
1265 case LYX_ALIGN_CENTER:
1266 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1268 case LYX_ALIGN_RIGHT:
1269 x += columnWidth(cell) - cellWidth(cell);
1270 // + getAdditionalWidth(cell);
1273 // LYX_ALIGN_LEFT: nothing :-)
1277 // the LaTeX Way :-(
1283 bool Tabular::isFirstCellInRow(idx_type cell) const
1285 return cellColumn(cell) == 0;
1289 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1291 if (row > rowCount() - 1)
1292 row = rowCount() - 1;
1293 return cell_info[row][0].cellno;
1297 bool Tabular::isLastCellInRow(idx_type cell) const
1299 return cellRightColumn(cell) == columnCount() - 1;
1303 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1305 if (row > rowCount() - 1)
1306 row = rowCount() - 1;
1307 return cell_info[row][columnCount() - 1].cellno;
1311 void Tabular::calculate_width_of_column(col_type column)
1314 for (row_type i = 0; i < rowCount(); ++i)
1315 maximum = max(cell_info[i][column].width, maximum);
1316 column_info[column].width = maximum;
1321 // Calculate the columns regarding ONLY the normal cells and if this
1322 // column is inside a multicolumn cell then use it only if its the last
1323 // column of this multicolumn cell as this gives an added width to the
1324 // column, all the rest should be adapted!
1326 bool Tabular::calculate_width_of_column_NMC(col_type column)
1328 int const old_column_width = column_info[column].width;
1330 for (row_type i = 0; i < rowCount(); ++i) {
1331 idx_type cell = cellIndex(i, column);
1332 bool ismulti = isMultiColumnReal(cell);
1333 if ((!ismulti || column == cellRightColumn(cell)) &&
1334 cell_info[i][column].width > max)
1336 max = cell_info[i][column].width;
1339 column_info[column].width = max;
1340 return column_info[column].width != old_column_width;
1344 Tabular::row_type Tabular::cellRow(idx_type cell) const
1346 if (cell >= cellCount())
1347 return rowCount() - 1;
1350 return rowofcell[cell];
1354 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1356 if (cell >= cellCount())
1357 return columnCount() - 1;
1360 return columnofcell[cell];
1364 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1366 row_type const row = cellRow(cell);
1367 col_type column = cellColumn(cell);
1368 while (column < columnCount() - 1 &&
1369 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1375 void Tabular::write(Buffer const & buf, ostream & os) const
1379 << write_attribute("version", 3)
1380 << write_attribute("rows", rowCount())
1381 << write_attribute("columns", columnCount())
1383 // global longtable options
1385 << write_attribute("rotate", rotate)
1386 << write_attribute("booktabs", use_booktabs)
1387 << write_attribute("islongtable", is_long_tabular)
1388 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1389 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1390 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1391 << write_attribute("headTopDL", endhead.topDL)
1392 << write_attribute("headBottomDL", endhead.bottomDL)
1393 << write_attribute("footTopDL", endfoot.topDL)
1394 << write_attribute("footBottomDL", endfoot.bottomDL)
1395 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1396 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1397 << write_attribute("lastFootEmpty", endlastfoot.empty)
1399 for (col_type j = 0; j < columnCount(); ++j) {
1401 << write_attribute("alignment", column_info[j].alignment)
1402 << write_attribute("valignment", column_info[j].valignment)
1403 << write_attribute("leftline", column_info[j].left_line)
1404 << write_attribute("rightline", column_info[j].right_line)
1405 << write_attribute("width", column_info[j].p_width.asString())
1406 << write_attribute("special", column_info[j].align_special)
1409 for (row_type i = 0; i < rowCount(); ++i) {
1411 << write_attribute("topline", row_info[i].top_line)
1412 << write_attribute("bottomline", row_info[i].bottom_line);
1413 static const string def("default");
1414 if (row_info[i].top_space_default)
1415 os << write_attribute("topspace", def);
1417 os << write_attribute("topspace", row_info[i].top_space);
1418 if (row_info[i].bottom_space_default)
1419 os << write_attribute("bottomspace", def);
1421 os << write_attribute("bottomspace", row_info[i].bottom_space);
1422 if (row_info[i].interline_space_default)
1423 os << write_attribute("interlinespace", def);
1425 os << write_attribute("interlinespace", row_info[i].interline_space);
1426 os << write_attribute("endhead", row_info[i].endhead)
1427 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1428 << write_attribute("endfoot", row_info[i].endfoot)
1429 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1430 << write_attribute("newpage", row_info[i].newpage)
1432 for (col_type j = 0; j < columnCount(); ++j) {
1434 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1435 << write_attribute("alignment", cell_info[i][j].alignment)
1436 << write_attribute("valignment", cell_info[i][j].valignment)
1437 << write_attribute("topline", cell_info[i][j].top_line)
1438 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1439 << write_attribute("leftline", cell_info[i][j].left_line)
1440 << write_attribute("rightline", cell_info[i][j].right_line)
1441 << write_attribute("rotate", cell_info[i][j].rotate)
1442 << write_attribute("usebox", cell_info[i][j].usebox)
1443 << write_attribute("width", cell_info[i][j].p_width)
1444 << write_attribute("special", cell_info[i][j].align_special)
1446 os << "\\begin_inset ";
1447 cell_info[i][j].inset->write(buf, os);
1448 os << "\n\\end_inset\n"
1453 os << "</lyxtabular>\n";
1457 void Tabular::read(Buffer const & buf, Lexer & lex)
1460 istream & is = lex.getStream();
1462 l_getline(is, line);
1463 if (!prefixIs(line, "<lyxtabular ")
1464 && !prefixIs(line, "<Tabular ")) {
1465 BOOST_ASSERT(false);
1470 if (!getTokenValue(line, "version", version))
1472 BOOST_ASSERT(version >= 2);
1475 if (!getTokenValue(line, "rows", rows_arg))
1478 if (!getTokenValue(line, "columns", columns_arg))
1480 init(buf.params(), rows_arg, columns_arg);
1481 l_getline(is, line);
1482 if (!prefixIs(line, "<features")) {
1483 lyxerr << "Wrong tabular format (expected <features ...> got"
1484 << line << ')' << endl;
1487 getTokenValue(line, "rotate", rotate);
1488 getTokenValue(line, "booktabs", use_booktabs);
1489 getTokenValue(line, "islongtable", is_long_tabular);
1490 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1491 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1492 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1493 getTokenValue(line, "headTopDL", endhead.topDL);
1494 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1495 getTokenValue(line, "footTopDL", endfoot.topDL);
1496 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1497 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1498 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1499 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1501 for (col_type j = 0; j < columnCount(); ++j) {
1503 if (!prefixIs(line,"<column")) {
1504 lyxerr << "Wrong tabular format (expected <column ...> got"
1505 << line << ')' << endl;
1508 getTokenValue(line, "alignment", column_info[j].alignment);
1509 getTokenValue(line, "valignment", column_info[j].valignment);
1510 getTokenValue(line, "leftline", column_info[j].left_line);
1511 getTokenValue(line, "rightline", column_info[j].right_line);
1512 getTokenValue(line, "width", column_info[j].p_width);
1513 getTokenValue(line, "special", column_info[j].align_special);
1516 for (row_type i = 0; i < rowCount(); ++i) {
1517 l_getline(is, line);
1518 if (!prefixIs(line, "<row")) {
1519 lyxerr << "Wrong tabular format (expected <row ...> got"
1520 << line << ')' << endl;
1523 getTokenValue(line, "topline", row_info[i].top_line);
1524 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1525 getTokenValue(line, "topspace", row_info[i].top_space,
1526 row_info[i].top_space_default);
1527 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1528 row_info[i].bottom_space_default);
1529 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1530 row_info[i].interline_space_default);
1531 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1532 getTokenValue(line, "endhead", row_info[i].endhead);
1533 getTokenValue(line, "endfoot", row_info[i].endfoot);
1534 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1535 getTokenValue(line, "newpage", row_info[i].newpage);
1536 for (col_type j = 0; j < columnCount(); ++j) {
1537 l_getline(is, line);
1538 if (!prefixIs(line, "<cell")) {
1539 lyxerr << "Wrong tabular format (expected <cell ...> got"
1540 << line << ')' << endl;
1543 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1544 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1545 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1546 getTokenValue(line, "topline", cell_info[i][j].top_line);
1547 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1548 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1549 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1550 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1551 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1552 getTokenValue(line, "width", cell_info[i][j].p_width);
1553 getTokenValue(line, "special", cell_info[i][j].align_special);
1554 l_getline(is, line);
1555 if (prefixIs(line, "\\begin_inset")) {
1556 cell_info[i][j].inset->read(buf, lex);
1557 l_getline(is, line);
1559 if (!prefixIs(line, "</cell>")) {
1560 lyxerr << "Wrong tabular format (expected </cell> got"
1561 << line << ')' << endl;
1565 l_getline(is, line);
1566 if (!prefixIs(line, "</row>")) {
1567 lyxerr << "Wrong tabular format (expected </row> got"
1568 << line << ')' << endl;
1572 while (!prefixIs(line, "</lyxtabular>")) {
1573 l_getline(is, line);
1579 bool Tabular::isMultiColumn(idx_type cell) const
1581 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1585 bool Tabular::isMultiColumnReal(idx_type cell) const
1587 return cellColumn(cell) != cellRightColumn(cell) &&
1588 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1592 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1594 return cell_info[cellRow(cell)][cellColumn(cell)];
1598 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1601 cellstruct & cs = cellinfo_of_cell(cell);
1602 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1603 cs.alignment = column_info[cellColumn(cell)].alignment;
1604 cs.top_line = row_info[cellRow(cell)].top_line;
1605 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1606 cs.left_line = column_info[cellColumn(cell)].left_line;
1607 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1608 for (idx_type i = 1; i < number; ++i) {
1609 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1610 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1611 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1618 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1620 row_type const row = cellRow(cell);
1621 col_type column = cellColumn(cell);
1622 idx_type result = 1;
1624 while (column < columnCount() &&
1625 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1634 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1636 row_type const row = cellRow(cell);
1637 col_type column = cellColumn(cell);
1639 idx_type result = 0;
1641 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1642 cell_info[row][column].multicolumn = CELL_NORMAL;
1644 while (column < columnCount() &&
1645 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1647 cell_info[row][column].multicolumn = CELL_NORMAL;
1657 void Tabular::setBookTabs(bool what)
1659 use_booktabs = what;
1663 bool Tabular::useBookTabs() const
1665 return use_booktabs;
1669 void Tabular::setLongTabular(bool what)
1671 is_long_tabular = what;
1675 bool Tabular::isLongTabular() const
1677 return is_long_tabular;
1681 void Tabular::setRotateTabular(bool flag)
1687 bool Tabular::getRotateTabular() const
1693 void Tabular::setRotateCell(idx_type cell, bool flag)
1695 cellinfo_of_cell(cell).rotate = flag;
1699 bool Tabular::getRotateCell(idx_type cell) const
1701 return cellinfo_of_cell(cell).rotate;
1705 bool Tabular::needRotating() const
1709 for (row_type i = 0; i < rowCount(); ++i)
1710 for (col_type j = 0; j < columnCount(); ++j)
1711 if (cell_info[i][j].rotate)
1717 bool Tabular::isLastCell(idx_type cell) const
1719 if (cell + 1 < cellCount())
1725 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1727 if (cellRow(cell) > 0)
1728 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1733 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1735 if (cellRow(cell) + 1 < rowCount())
1736 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1741 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1743 if (cellRow(cell) == 0)
1745 if (!isMultiColumn(cell))
1746 return getCellAbove(cell);
1747 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1751 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1753 if (cellRow(cell) + 1 >= rowCount())
1755 if (!isMultiColumn(cell))
1756 return getCellBelow(cell);
1757 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1761 Tabular::idx_type Tabular::cellIndex(row_type row,
1762 col_type column) const
1764 BOOST_ASSERT(column != npos && column < columnCount() &&
1765 row != npos && row < rowCount());
1766 return cell_info[row][column].cellno;
1770 void Tabular::setUsebox(idx_type cell, BoxType type)
1772 cellinfo_of_cell(cell).usebox = type;
1776 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1778 if (column_info[cellColumn(cell)].p_width.zero() &&
1779 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1781 if (cellinfo_of_cell(cell).usebox > 1)
1782 return cellinfo_of_cell(cell).usebox;
1783 return useParbox(cell);
1788 // This are functions used for the longtable support
1790 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1796 row_info[row].endfirsthead = flag;
1800 row_info[row].endhead = flag;
1805 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1808 hd.set = haveLTHead();
1809 return row_info[row].endhead;
1813 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1816 hd.set = haveLTFirstHead();
1817 return row_info[row].endfirsthead;
1821 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1827 row_info[row].endlastfoot = flag;
1831 row_info[row].endfoot = flag;
1836 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1839 fd.set = haveLTFoot();
1840 return row_info[row].endfoot;
1844 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1847 fd.set = haveLTLastFoot();
1848 return row_info[row].endlastfoot;
1852 void Tabular::setLTNewPage(row_type row, bool what)
1854 row_info[row].newpage = what;
1858 bool Tabular::getLTNewPage(row_type row) const
1860 return row_info[row].newpage;
1864 bool Tabular::haveLTHead() const
1866 for (row_type i = 0; i < rowCount(); ++i)
1867 if (row_info[i].endhead)
1873 bool Tabular::haveLTFirstHead() const
1875 if (endfirsthead.empty)
1877 for (row_type i = 0; i < rowCount(); ++i)
1878 if (row_info[i].endfirsthead)
1884 bool Tabular::haveLTFoot() const
1886 for (row_type i = 0; i < rowCount(); ++i)
1887 if (row_info[i].endfoot)
1893 bool Tabular::haveLTLastFoot() const
1895 if (endlastfoot.empty)
1897 for (row_type i = 0; i < rowCount(); ++i)
1898 if (row_info[i].endlastfoot)
1904 // end longtable support functions
1906 void Tabular::setRowAscent(row_type row, int height)
1908 if (row >= rowCount() || row_info[row].ascent == height)
1910 row_info[row].ascent = height;
1914 void Tabular::setRowDescent(row_type row, int height)
1916 if (row >= rowCount() || row_info[row].descent == height)
1918 row_info[row].descent = height;
1922 int Tabular::rowAscent(row_type row) const
1924 if (row >= rowCount())
1926 return row_info[row].ascent;
1930 int Tabular::rowDescent(row_type row) const
1932 BOOST_ASSERT(row < rowCount());
1933 return row_info[row].descent;
1937 int Tabular::height() const
1940 for (row_type row = 0; row < rowCount(); ++row)
1941 height += rowAscent(row) + rowDescent(row) +
1942 getAdditionalHeight(row);
1947 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1949 BOOST_ASSERT(row < rowCount());
1950 BOOST_ASSERT(column < columnCount());
1951 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1955 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1957 // FIXME: assert or return 0 as in TeXBottomHLine()?
1958 BOOST_ASSERT(row != npos);
1959 BOOST_ASSERT(row < rowCount());
1961 idx_type const fcell = getFirstCellInRow(row);
1962 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1965 for (idx_type i = fcell; i < n; ++i) {
1969 if (use_booktabs && row == 0) {
1971 } else if (tmp == n - fcell) {
1972 os << (use_booktabs ? "\\midrule " : "\\hline ");
1974 for (idx_type i = fcell; i < n; ++i) {
1976 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1977 << cellColumn(i) + 1
1979 << cellRightColumn(i) + 1
1991 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1993 // FIXME: return 0 or assert as in TeXTopHLine()?
1994 if (row == npos || row >= rowCount())
1997 idx_type const fcell = getFirstCellInRow(row);
1998 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2001 for (idx_type i = fcell; i < n; ++i) {
2005 if (use_booktabs && row == rowCount() - 1) {
2006 os << "\\bottomrule";
2007 } else if (tmp == n - fcell) {
2008 os << (use_booktabs ? "\\midrule" : "\\hline");
2010 for (idx_type i = fcell; i < n; ++i) {
2011 if (bottomLine(i)) {
2012 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2013 << cellColumn(i) + 1
2015 << cellRightColumn(i) + 1
2027 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2031 if (getRotateCell(cell)) {
2032 os << "\\begin{sideways}\n";
2035 if (isMultiColumn(cell)) {
2036 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2037 if (leftLine(cell) &&
2038 (isFirstCellInRow(cell) ||
2039 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2040 !rightLine(cell - 1, true))))
2042 if (!cellinfo_of_cell(cell).align_special.empty()) {
2043 os << cellinfo_of_cell(cell).align_special;
2045 if (!getPWidth(cell).zero()) {
2046 switch (getVAlignment(cell)) {
2047 case LYX_VALIGN_TOP:
2050 case LYX_VALIGN_MIDDLE:
2053 case LYX_VALIGN_BOTTOM:
2058 << from_ascii(getPWidth(cell).asLatexString())
2061 switch (getAlignment(cell)) {
2062 case LYX_ALIGN_LEFT:
2065 case LYX_ALIGN_RIGHT:
2072 } // end if else !getPWidth
2073 } // end if else !cellinfo_of_cell
2074 if (rightLine(cell))
2076 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2081 if (getUsebox(cell) == BOX_PARBOX) {
2083 switch (getVAlignment(cell)) {
2084 case LYX_VALIGN_TOP:
2087 case LYX_VALIGN_MIDDLE:
2090 case LYX_VALIGN_BOTTOM:
2094 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2096 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2097 os << "\\begin{minipage}[";
2098 switch (getVAlignment(cell)) {
2099 case LYX_VALIGN_TOP:
2102 case LYX_VALIGN_MIDDLE:
2105 case LYX_VALIGN_BOTTOM:
2109 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2117 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2122 if (getUsebox(cell) == BOX_PARBOX)
2124 else if (getUsebox(cell) == BOX_MINIPAGE) {
2125 os << "%\n\\end{minipage}";
2128 if (isMultiColumn(cell)) {
2131 if (getRotateCell(cell)) {
2132 os << "%\n\\end{sideways}";
2139 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2140 OutputParams const & runparams) const
2142 if (!is_long_tabular)
2146 // output header info
2148 if (endhead.topDL) {
2152 for (row_type i = 0; i < rowCount(); ++i) {
2153 if (row_info[i].endhead) {
2154 ret += TeXRow(os, i, buf, runparams);
2157 if (endhead.bottomDL) {
2161 os << "\\endhead\n";
2163 if (endfirsthead.empty) {
2164 os << "\\endfirsthead\n";
2168 // output firstheader info
2169 if (haveLTFirstHead()) {
2170 if (endfirsthead.topDL) {
2174 for (row_type i = 0; i < rowCount(); ++i) {
2175 if (row_info[i].endfirsthead) {
2176 ret += TeXRow(os, i, buf, runparams);
2179 if (endfirsthead.bottomDL) {
2183 os << "\\endfirsthead\n";
2186 // output footer info
2188 if (endfoot.topDL) {
2192 for (row_type i = 0; i < rowCount(); ++i) {
2193 if (row_info[i].endfoot) {
2194 ret += TeXRow(os, i, buf, runparams);
2197 if (endfoot.bottomDL) {
2201 os << "\\endfoot\n";
2203 if (endlastfoot.empty) {
2204 os << "\\endlastfoot\n";
2208 // output lastfooter info
2209 if (haveLTLastFoot()) {
2210 if (endlastfoot.topDL) {
2214 for (row_type i = 0; i < rowCount(); ++i) {
2215 if (row_info[i].endlastfoot) {
2216 ret += TeXRow(os, i, buf, runparams);
2219 if (endlastfoot.bottomDL) {
2223 os << "\\endlastfoot\n";
2230 bool Tabular::isValidRow(row_type row) const
2232 if (!is_long_tabular)
2234 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2235 !row_info[row].endfoot && !row_info[row].endlastfoot;
2239 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2240 OutputParams const & runparams) const
2242 idx_type cell = cellIndex(i, 0);
2243 int ret = TeXTopHLine(os, i);
2244 if (row_info[i].top_space_default) {
2246 os << "\\addlinespace\n";
2248 os << "\\noalign{\\vskip\\doublerulesep}\n";
2250 } else if(!row_info[i].top_space.zero()) {
2252 os << "\\addlinespace["
2253 << from_ascii(row_info[i].top_space.asLatexString())
2256 os << "\\noalign{\\vskip"
2257 << from_ascii(row_info[i].top_space.asLatexString())
2263 for (col_type j = 0; j < columnCount(); ++j) {
2264 if (isPartOfMultiColumn(i, j))
2266 ret += TeXCellPreamble(os, cell);
2267 shared_ptr<InsetText> inset = getCellInset(cell);
2269 Paragraph const & par = inset->paragraphs().front();
2270 bool rtl = par.isRTL(buf.params())
2272 && getPWidth(cell).zero();
2275 if (par.getParLanguage(buf.params())->lang() ==
2278 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2280 // currently, remaning RTL languages are arabic_arabtex and hebrew
2284 ret += inset->latex(buf, os, runparams);
2288 ret += TeXCellPostamble(os, cell);
2289 if (!isLastCellInRow(cell)) { // not last cell in row
2294 os << "\\tabularnewline";
2295 if (row_info[i].bottom_space_default) {
2297 os << "\\addlinespace";
2299 os << "[\\doublerulesep]";
2300 } else if (!row_info[i].bottom_space.zero()) {
2302 os << "\\addlinespace";
2304 << from_ascii(row_info[i].bottom_space.asLatexString())
2309 ret += TeXBottomHLine(os, i);
2310 if (row_info[i].interline_space_default) {
2312 os << "\\addlinespace\n";
2314 os << "\\noalign{\\vskip\\doublerulesep}\n";
2316 } else if (!row_info[i].interline_space.zero()) {
2318 os << "\\addlinespace["
2319 << from_ascii(row_info[i].interline_space.asLatexString())
2322 os << "\\noalign{\\vskip"
2323 << from_ascii(row_info[i].interline_space.asLatexString())
2331 int Tabular::latex(Buffer const & buf, odocstream & os,
2332 OutputParams const & runparams) const
2336 //+---------------------------------------------------------------------
2337 //+ first the opening preamble +
2338 //+---------------------------------------------------------------------
2341 os << "\\begin{sideways}\n";
2344 if (is_long_tabular)
2345 os << "\\begin{longtable}{";
2347 os << "\\begin{tabular}{";
2348 for (col_type i = 0; i < columnCount(); ++i) {
2349 if (!use_booktabs && column_info[i].left_line)
2351 if (!column_info[i].align_special.empty()) {
2352 os << column_info[i].align_special;
2354 if (!column_info[i].p_width.zero()) {
2355 switch (column_info[i].alignment) {
2356 case LYX_ALIGN_LEFT:
2357 os << ">{\\raggedright}";
2359 case LYX_ALIGN_RIGHT:
2360 os << ">{\\raggedleft}";
2362 case LYX_ALIGN_CENTER:
2363 os << ">{\\centering}";
2365 case LYX_ALIGN_NONE:
2366 case LYX_ALIGN_BLOCK:
2367 case LYX_ALIGN_LAYOUT:
2368 case LYX_ALIGN_SPECIAL:
2372 switch (column_info[i].valignment) {
2373 case LYX_VALIGN_TOP:
2376 case LYX_VALIGN_MIDDLE:
2379 case LYX_VALIGN_BOTTOM:
2384 << from_ascii(column_info[i].p_width.asLatexString())
2387 switch (column_info[i].alignment) {
2388 case LYX_ALIGN_LEFT:
2391 case LYX_ALIGN_RIGHT:
2398 } // end if else !column_info[i].p_width
2399 } // end if else !column_info[i].align_special
2400 if (!use_booktabs && column_info[i].right_line)
2406 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2408 //+---------------------------------------------------------------------
2409 //+ the single row and columns (cells) +
2410 //+---------------------------------------------------------------------
2412 for (row_type i = 0; i < rowCount(); ++i) {
2413 if (isValidRow(i)) {
2414 ret += TeXRow(os, i, buf, runparams);
2415 if (is_long_tabular && row_info[i].newpage) {
2416 os << "\\newpage\n";
2422 //+---------------------------------------------------------------------
2423 //+ the closing of the tabular +
2424 //+---------------------------------------------------------------------
2426 if (is_long_tabular)
2427 os << "\\end{longtable}";
2429 os << "\\end{tabular}";
2431 os << "\n\\end{sideways}";
2439 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2440 OutputParams const & runparams) const
2443 idx_type cell = getFirstCellInRow(row);
2446 for (col_type j = 0; j < columnCount(); ++j) {
2447 if (isPartOfMultiColumn(row, j))
2450 os << "<entry align=\"";
2451 switch (getAlignment(cell)) {
2452 case LYX_ALIGN_LEFT:
2455 case LYX_ALIGN_RIGHT:
2463 os << "\" valign=\"";
2464 switch (getVAlignment(cell)) {
2465 case LYX_VALIGN_TOP:
2468 case LYX_VALIGN_BOTTOM:
2471 case LYX_VALIGN_MIDDLE:
2476 if (isMultiColumn(cell)) {
2477 os << " namest=\"col" << j << "\" ";
2478 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2482 ret += getCellInset(cell)->docbook(buf, os, runparams);
2491 int Tabular::docbook(Buffer const & buf, odocstream & os,
2492 OutputParams const & runparams) const
2496 //+---------------------------------------------------------------------
2497 //+ first the opening preamble +
2498 //+---------------------------------------------------------------------
2500 os << "<tgroup cols=\"" << columnCount()
2501 << "\" colsep=\"1\" rowsep=\"1\">\n";
2503 for (col_type i = 0; i < columnCount(); ++i) {
2504 os << "<colspec colname=\"col" << i << "\" align=\"";
2505 switch (column_info[i].alignment) {
2506 case LYX_ALIGN_LEFT:
2509 case LYX_ALIGN_RIGHT:
2517 if (runparams.flavor == OutputParams::XML)
2523 //+---------------------------------------------------------------------
2524 //+ Long Tabular case +
2525 //+---------------------------------------------------------------------
2527 // output header info
2528 if (haveLTHead() || haveLTFirstHead()) {
2531 for (row_type i = 0; i < rowCount(); ++i) {
2532 if (row_info[i].endhead || row_info[i].endfirsthead) {
2533 ret += docbookRow(buf, os, i, runparams);
2539 // output footer info
2540 if (haveLTFoot() || haveLTLastFoot()) {
2543 for (row_type i = 0; i < rowCount(); ++i) {
2544 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2545 ret += docbookRow(buf, os, i, runparams);
2552 //+---------------------------------------------------------------------
2553 //+ the single row and columns (cells) +
2554 //+---------------------------------------------------------------------
2558 for (row_type i = 0; i < rowCount(); ++i) {
2559 if (isValidRow(i)) {
2560 ret += docbookRow(buf, os, i, runparams);
2565 //+---------------------------------------------------------------------
2566 //+ the closing of the tabular +
2567 //+---------------------------------------------------------------------
2576 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2577 vector<unsigned int> const & clen) const
2579 idx_type const fcell = getFirstCellInRow(row);
2580 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2583 for (idx_type i = fcell; i < n; ++i) {
2593 for (idx_type i = fcell; i < n; ++i) {
2604 col_type column = cellColumn(i);
2605 int len = clen[column];
2606 while (column < columnCount() - 1
2607 && isPartOfMultiColumn(row, ++column))
2608 len += clen[column] + 4;
2609 os << docstring(len, ch);
2624 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2625 vector<unsigned int> const & clen) const
2627 idx_type const fcell = getFirstCellInRow(row);
2628 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2631 for (idx_type i = fcell; i < n; ++i) {
2632 if (bottomLine(i)) {
2641 for (idx_type i = fcell; i < n; ++i) {
2642 if (bottomLine(i)) {
2652 col_type column = cellColumn(i);
2653 int len = clen[column];
2654 while (column < columnCount() -1
2655 && isPartOfMultiColumn(row, ++column))
2656 len += clen[column] + 4;
2657 os << docstring(len, ch);
2658 if (bottomLine(i)) {
2672 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2673 OutputParams const & runparams,
2674 idx_type cell, row_type row, col_type column,
2675 vector<unsigned int> const & clen,
2676 bool onlydata) const
2678 odocstringstream sstr;
2679 getCellInset(cell)->plaintext(buf, sstr, runparams);
2691 unsigned int len1 = sstr.str().length();
2692 unsigned int len2 = clen[column];
2693 while (column < columnCount() -1
2694 && isPartOfMultiColumn(row, ++column))
2695 len2 += clen[column] + 4;
2698 switch (getAlignment(cell)) {
2700 case LYX_ALIGN_LEFT:
2703 case LYX_ALIGN_RIGHT:
2707 case LYX_ALIGN_CENTER:
2713 os << docstring(len1, ' ') << sstr.str()
2714 << docstring(len2, ' ');
2716 if (rightLine(cell))
2723 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2724 OutputParams const & runparams, int const depth,
2725 bool onlydata, unsigned char delim) const
2727 // first calculate the width of the single columns
2728 vector<unsigned int> clen(columnCount());
2731 // first all non (real) multicolumn cells!
2732 for (col_type j = 0; j < columnCount(); ++j) {
2734 for (row_type i = 0; i < rowCount(); ++i) {
2735 idx_type cell = cellIndex(i, j);
2736 if (isMultiColumnReal(cell))
2738 odocstringstream sstr;
2739 getCellInset(cell)->plaintext(buf, sstr, runparams);
2740 if (clen[j] < sstr.str().length())
2741 clen[j] = sstr.str().length();
2744 // then all (real) multicolumn cells!
2745 for (col_type j = 0; j < columnCount(); ++j) {
2746 for (row_type i = 0; i < rowCount(); ++i) {
2747 idx_type cell = cellIndex(i, j);
2748 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2750 odocstringstream sstr;
2751 getCellInset(cell)->plaintext(buf, sstr, runparams);
2752 int len = int(sstr.str().length());
2753 idx_type const n = cells_in_multicolumn(cell);
2754 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2756 if (len > int(clen[j + n - 1]))
2757 clen[j + n - 1] = len;
2762 for (row_type i = 0; i < rowCount(); ++i) {
2763 if (!onlydata && plaintextTopHLine(os, i, clen))
2764 os << docstring(depth * 2, ' ');
2765 for (col_type j = 0; j < columnCount(); ++j) {
2766 if (isPartOfMultiColumn(i, j))
2768 if (onlydata && j > 0)
2770 plaintextPrintCell(buf, os, runparams,
2771 cell, i, j, clen, onlydata);
2776 os << docstring(depth * 2, ' ');
2777 if (plaintextBottomHLine(os, i, clen))
2778 os << docstring(depth * 2, ' ');
2784 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2786 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2790 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2791 col_type column) const
2793 return cell_info[row][column].inset;
2797 void Tabular::setCellInset(row_type row, col_type column,
2798 shared_ptr<InsetText> ins) const
2800 cell_info[row][column].inset = ins;
2805 Tabular::getCellFromInset(Inset const * inset) const
2807 // is this inset part of the tabular?
2809 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2810 BOOST_ASSERT(false);
2813 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2814 if (getCellInset(cell).get() == inset) {
2815 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2816 << "cell=" << cell << endl;
2820 // We should have found a cell at this point
2821 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2822 << inset << " not found!" << endl;
2823 BOOST_ASSERT(false);
2829 void Tabular::validate(LaTeXFeatures & features) const
2831 features.require("NeedTabularnewline");
2833 features.require("booktabs");
2834 if (isLongTabular())
2835 features.require("longtable");
2837 features.require("rotating");
2838 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2839 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2840 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2841 features.require("array");
2842 getCellInset(cell)->validate(features);
2847 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2849 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2850 ParagraphList::const_iterator cit = parlist.begin();
2851 ParagraphList::const_iterator end = parlist.end();
2853 for (; cit != end; ++cit)
2854 for (int i = 0; i < cit->size(); ++i)
2855 if (cit->isNewline(i))
2862 /////////////////////////////////////////////////////////////////////
2866 /////////////////////////////////////////////////////////////////////
2868 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2870 : tabular(buf.params(), max(rows, row_type(1)),
2871 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2875 InsetTabular::InsetTabular(InsetTabular const & tab)
2876 : Inset(tab), tabular(tab.tabular),
2877 buffer_(tab.buffer_), scx_(0)
2881 InsetTabular::~InsetTabular()
2883 InsetTabularMailer(*this).hideDialog();
2887 Inset * InsetTabular::clone() const
2889 return new InsetTabular(*this);
2893 Buffer const & InsetTabular::buffer() const
2899 void InsetTabular::buffer(Buffer const * b)
2905 void InsetTabular::write(Buffer const & buf, ostream & os) const
2907 os << "Tabular" << endl;
2908 tabular.write(buf, os);
2912 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2914 bool const old_format = (lex.getString() == "\\LyXTable");
2916 tabular.read(buf, lex);
2922 string token = lex.getString();
2923 while (lex && token != "\\end_inset") {
2925 token = lex.getString();
2928 lex.printError("Missing \\end_inset at this point. "
2934 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2936 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2937 // mi.base.textwidth << "\n";
2939 lyxerr << "InsetTabular::metrics: need bv" << endl;
2940 BOOST_ASSERT(false);
2944 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2947 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2948 if (tabular.isPartOfMultiColumn(i, j))
2949 // Multicolumn cell, but not first one
2954 if (tabular.cell_info[i][j].multicolumn ==
2955 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2956 p_width = tabular.cellinfo_of_cell(cell).p_width;
2958 p_width = tabular.column_info[j].p_width;
2959 if (!p_width.zero())
2960 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2961 tabular.getCellInset(cell)->metrics(m, dim);
2962 if (!p_width.zero())
2963 dim.wid = m.base.textwidth;
2964 tabular.setCellWidth(cell, dim.wid);
2965 if (p_width.zero()) {
2966 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2967 // FIXME there must be a way to get rid of
2968 // the second metrics call
2969 tabular.getCellInset(cell)->metrics(m, dim);
2971 maxAsc = max(maxAsc, dim.asc);
2972 maxDesc = max(maxDesc, dim.des);
2975 int const top_space = tabular.row_info[i].top_space_default ?
2976 default_line_space :
2977 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2978 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2979 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2980 default_line_space :
2981 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2982 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2985 dim.asc = tabular.rowAscent(0);
2986 dim.des = tabular.height() - dim.asc;
2987 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2991 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2993 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2994 BufferView * bv = pi.base.bv;
2996 // FIXME: As the full backrgound is painted in drawSelection(),
2997 // we have no choice but to do a full repaint for the Text cells.
2998 pi.full_repaint = true;
3000 resetPos(bv->cursor());
3003 x += ADD_TO_TABULAR_WIDTH;
3006 first_visible_cell = Tabular::npos;
3007 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3009 int const a = tabular.rowAscent(i);
3010 int const d = tabular.rowDescent(i);
3011 idx = tabular.cellIndex(i, 0);
3012 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3013 if (tabular.isPartOfMultiColumn(i, j))
3015 if (first_visible_cell == Tabular::npos)
3016 first_visible_cell = idx;
3018 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3019 // Cache the Inset position.
3020 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3021 if (nx + tabular.columnWidth(idx) < 0
3022 || nx > bv->workWidth()
3024 || y - a > bv->workHeight()) {
3025 pi.pain.setDrawingEnabled(false);
3026 cell(idx)->draw(pi, cx, y);
3027 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3028 pi.pain.setDrawingEnabled(true);
3030 cell(idx)->draw(pi, cx, y);
3031 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3033 nx += tabular.columnWidth(idx);
3037 if (i + 1 < tabular.rowCount())
3038 y += d + tabular.rowAscent(i + 1) +
3039 tabular.getAdditionalHeight(i + 1);
3044 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3046 Cursor & cur = pi.base.bv->cursor();
3048 x += scx_ + ADD_TO_TABULAR_WIDTH;
3050 // FIXME: it is wrong to completely paint the background
3051 // if we want to do single row painting.
3053 // Paint background of current tabular
3054 int const w = tabular.width();
3055 int const h = tabular.height();
3056 int yy = y - tabular.rowAscent(0);
3057 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3059 if (!cur.selection())
3061 if (!ptr_cmp(&cur.inset(), this))
3067 if (tablemode(cur)) {
3070 getSelection(cur, rs, re, cs, ce);
3071 y -= tabular.rowAscent(0);
3072 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3073 int const a = tabular.rowAscent(j);
3074 int const h = a + tabular.rowDescent(j);
3076 y += tabular.getAdditionalHeight(j);
3077 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3078 if (tabular.isPartOfMultiColumn(j, i))
3080 idx_type const cell =
3081 tabular.cellIndex(j, i);
3082 int const w = tabular.columnWidth(cell);
3083 if (i >= cs && i <= ce && j >= rs && j <= re)
3084 pi.pain.fillRectangle(xx, y, w, h,
3092 x += getCellXPos(cur.idx());
3093 x += tabular.getBeginningOfTextInCell(cur.idx());
3094 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3099 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3100 row_type row, idx_type cell, bool erased) const
3102 int x2 = x + tabular.columnWidth(cell);
3103 bool on_off = false;
3104 Color::color col = Color::tabularline;
3105 Color::color onoffcol = Color::tabularonoffline;
3108 col = Color::deletedtext;
3109 onoffcol = Color::deletedtext;
3112 if (!tabular.topAlreadyDrawn(cell)) {
3113 on_off = !tabular.topLine(cell);
3114 pain.line(x, y - tabular.rowAscent(row),
3115 x2, y - tabular.rowAscent(row),
3116 on_off ? onoffcol : col,
3117 on_off ? Painter::line_onoffdash : Painter::line_solid);
3119 on_off = !tabular.bottomLine(cell);
3120 pain.line(x, y + tabular.rowDescent(row),
3121 x2, y + tabular.rowDescent(row),
3122 on_off ? onoffcol : col,
3123 on_off ? Painter::line_onoffdash : Painter::line_solid);
3124 if (!tabular.leftAlreadyDrawn(cell)) {
3125 on_off = !tabular.leftLine(cell);
3126 pain.line(x, y - tabular.rowAscent(row),
3127 x, y + tabular.rowDescent(row),
3128 on_off ? onoffcol : col,
3129 on_off ? Painter::line_onoffdash : Painter::line_solid);
3131 on_off = !tabular.rightLine(cell);
3132 pain.line(x2 - tabular.getAdditionalWidth(cell),
3133 y - tabular.rowAscent(row),
3134 x2 - tabular.getAdditionalWidth(cell),
3135 y + tabular.rowDescent(row),
3136 on_off ? onoffcol : col,
3137 on_off ? Painter::line_onoffdash : Painter::line_solid);
3141 docstring const InsetTabular::editMessage() const
3143 return _("Opened table");
3147 void InsetTabular::edit(Cursor & cur, bool left)
3149 //lyxerr << "InsetTabular::edit: " << this << endl;
3151 cur.selection() = false;
3154 if (isRightToLeft(cur))
3155 cur.idx() = tabular.getLastCellInRow(0);
3161 if (isRightToLeft(cur))
3162 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3164 cur.idx() = tabular.cellCount() - 1;
3166 cur.pos() = cur.lastpos(); // FIXME crude guess
3168 // FIXME: this accesses the position cache before it is initialized
3170 //cur.bv().fitCursor();
3174 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3176 // In a longtable, tell captions what the current float is
3177 Counters & cnts = buf.params().getTextClass().counters();
3178 string const saveflt = cnts.current_float();
3179 if (tabular.isLongTabular())
3180 cnts.current_float("table");
3182 ParIterator it2 = it;
3184 size_t const end = it2.nargs();
3185 for ( ; it2.idx() < end; it2.top().forwardIdx())
3186 lyx::updateLabels(buf, it2);
3189 if (tabular.isLongTabular())
3190 cnts.current_float(saveflt);
3194 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3196 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3197 << "\n cur:" << cur << endl;
3198 CursorSlice sl = cur.top();
3199 Cursor & bvcur = cur.bv().cursor();
3201 switch (cmd.action) {
3203 case LFUN_MOUSE_PRESS:
3204 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3206 // do not reset cursor/selection if we have selected
3207 // some cells (bug 2715).
3208 if (cmd.button() == mouse_button::button3
3209 && &bvcur.selBegin().inset() == this
3210 && tablemode(bvcur))
3213 // Let InsetText do it
3214 cell(cur.idx())->dispatch(cur, cmd);
3216 case LFUN_MOUSE_MOTION:
3217 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3218 if (cmd.button() == mouse_button::button1) {
3219 // only accept motions to places not deeper nested than the real anchor
3220 if (bvcur.anchor_.hasPart(cur)) {
3221 // only update if selection changes
3222 if (bvcur.idx() == cur.idx() &&
3223 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3225 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3226 bvcur.setCursor(cur);
3227 bvcur.selection() = true;
3233 case LFUN_MOUSE_RELEASE:
3234 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3235 if (cmd.button() == mouse_button::button3)
3236 InsetTabularMailer(*this).showDialog(&cur.bv());
3239 case LFUN_CELL_BACKWARD:
3241 cur.selection() = false;
3244 case LFUN_CELL_FORWARD:
3246 cur.selection() = false;
3249 case LFUN_CHAR_FORWARD_SELECT:
3250 case LFUN_CHAR_FORWARD:
3251 cell(cur.idx())->dispatch(cur, cmd);
3252 if (!cur.result().dispatched()) {
3253 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3254 if (sl == cur.top())
3255 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3261 case LFUN_CHAR_BACKWARD_SELECT:
3262 case LFUN_CHAR_BACKWARD:
3263 cell(cur.idx())->dispatch(cur, cmd);
3264 if (!cur.result().dispatched()) {
3265 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3266 if (sl == cur.top())
3267 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3273 case LFUN_DOWN_SELECT:
3275 cell(cur.idx())->dispatch(cur, cmd);
3276 cur.dispatched(); // override the cell's decision
3277 if (sl == cur.top())
3278 // if our Text didn't do anything to the cursor
3279 // then we try to put the cursor into the cell below
3280 // setting also the right targetX.
3281 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3282 cur.idx() = tabular.getCellBelow(cur.idx());
3284 TextMetrics const & tm =
3285 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3286 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3288 if (sl == cur.top()) {
3289 // we trick it to go to the RIGHT after leaving the
3291 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3296 case LFUN_UP_SELECT:
3298 cell(cur.idx())->dispatch(cur, cmd);
3299 cur.dispatched(); // override the cell's decision
3300 if (sl == cur.top())
3301 // if our Text didn't do anything to the cursor
3302 // then we try to put the cursor into the cell above
3303 // setting also the right targetX.
3304 if (tabular.cellRow(cur.idx()) != 0) {
3305 cur.idx() = tabular.getCellAbove(cur.idx());
3306 cur.pit() = cur.lastpit();
3307 Text const * text = cell(cur.idx())->getText(0);
3308 TextMetrics const & tm = cur.bv().textMetrics(text);
3309 ParagraphMetrics const & pm =
3310 tm.parMetrics(cur.lastpit());
3311 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3313 if (sl == cur.top()) {
3314 cmd = FuncRequest(LFUN_UP);
3319 // case LFUN_SCREEN_DOWN: {
3320 // //if (hasSelection())
3321 // // cur.selection() = false;
3322 // col_type const col = tabular.cellColumn(cur.idx());
3323 // int const t = cur.bv().top_y() + cur.bv().height();
3324 // if (t < yo() + tabular.getHeightOfTabular()) {
3325 // cur.bv().scrollDocView(t);
3326 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3328 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3335 // case LFUN_SCREEN_UP: {
3336 // //if (hasSelection())
3337 // // cur.selection() = false;
3338 // col_type const col = tabular.cellColumn(cur.idx());
3339 // int const t = cur.bv().top_y() + cur.bv().height();
3341 // cur.bv().scrollDocView(t);
3345 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3349 // cur.par() = cur.lastpar();
3350 // cur.pos() = cur.lastpos();
3354 case LFUN_LAYOUT_TABULAR:
3355 InsetTabularMailer(*this).showDialog(&cur.bv());
3358 case LFUN_INSET_DIALOG_UPDATE:
3359 InsetTabularMailer(*this).updateDialog(&cur.bv());
3362 case LFUN_TABULAR_FEATURE:
3363 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3367 // insert file functions
3368 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3369 case LFUN_FILE_INSERT_PLAINTEXT: {
3371 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
3372 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3378 if (tablemode(cur)) {
3379 if (copySelection(cur)) {
3380 recordUndoInset(cur, Undo::DELETE);
3385 cell(cur.idx())->dispatch(cur, cmd);
3388 case LFUN_CHAR_DELETE_BACKWARD:
3389 case LFUN_CHAR_DELETE_FORWARD:
3390 if (tablemode(cur)) {
3391 recordUndoInset(cur, Undo::DELETE);
3395 cell(cur.idx())->dispatch(cur, cmd);
3399 if (!cur.selection())
3401 if (tablemode(cur)) {
3405 cell(cur.idx())->dispatch(cur, cmd);
3408 case LFUN_CLIPBOARD_PASTE:
3409 case LFUN_PRIMARY_SELECTION_PASTE: {
3410 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3411 theClipboard().getAsText() :
3412 theSelection().get();
3415 // pass to InsertPlaintextString, but
3416 // only if we have multi-cell content
3417 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3418 if (insertPlaintextString(cur.bv(), clip, false)) {
3419 // content has been replaced,
3420 // so cursor might be invalid
3421 cur.pos() = cur.lastpos();
3422 bvcur.setCursor(cur);
3426 // Let the cell handle normal text
3427 cell(cur.idx())->dispatch(cur, cmd);
3432 if (tabularStackDirty() && theClipboard().isInternal()) {
3433 recordUndoInset(cur, Undo::INSERT);
3434 pasteClipboard(cur);
3437 cell(cur.idx())->dispatch(cur, cmd);
3440 case LFUN_FONT_EMPH:
3441 case LFUN_FONT_BOLD:
3442 case LFUN_FONT_ROMAN:
3443 case LFUN_FONT_NOUN:
3444 case LFUN_FONT_ITAL:
3445 case LFUN_FONT_FRAK:
3446 case LFUN_FONT_TYPEWRITER:
3447 case LFUN_FONT_SANS:
3448 case LFUN_FONT_FREE_APPLY:
3449 case LFUN_FONT_FREE_UPDATE:
3450 case LFUN_FONT_SIZE:
3451 case LFUN_FONT_UNDERLINE:
3453 case LFUN_WORD_CAPITALIZE:
3454 case LFUN_WORD_UPCASE:
3455 case LFUN_WORD_LOWCASE:
3456 case LFUN_CHARS_TRANSPOSE:
3457 if (tablemode(cur)) {
3460 getSelection(cur, rs, re, cs, ce);
3461 Cursor tmpcur = cur;
3462 for (row_type i = rs; i <= re; ++i) {
3463 for (col_type j = cs; j <= ce; ++j) {
3464 // cursor follows cell:
3465 tmpcur.idx() = tabular.cellIndex(i, j);
3466 // select this cell only:
3469 tmpcur.resetAnchor();
3470 tmpcur.pit() = tmpcur.lastpit();
3471 tmpcur.pos() = tmpcur.top().lastpos();
3472 tmpcur.setCursor(tmpcur);
3473 tmpcur.setSelection();
3474 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3479 cell(cur.idx())->dispatch(cur, cmd);
3483 // we try to handle this event in the insets dispatch function.
3484 cell(cur.idx())->dispatch(cur, cmd);
3490 // function sets an object as defined in func_status.h:
3491 // states OK, Unknown, Disabled, On, Off.
3492 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3493 FuncStatus & status) const
3495 switch (cmd.action) {
3496 case LFUN_TABULAR_FEATURE: {
3497 int action = Tabular::LAST_ACTION;
3499 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3500 string const tmp = tabularFeature[i].feature;
3501 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3502 action = tabularFeature[i].action;
3506 if (action == Tabular::LAST_ACTION) {
3508 status.unknown(true);
3512 string const argument
3513 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3515 row_type sel_row_start = 0;
3516 row_type sel_row_end = 0;
3517 col_type sel_col_start = 0;
3518 col_type sel_col_end = 0;
3519 Tabular::ltType dummyltt;
3522 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3525 case Tabular::SET_PWIDTH:
3526 case Tabular::SET_MPWIDTH:
3527 case Tabular::SET_SPECIAL_COLUMN:
3528 case Tabular::SET_SPECIAL_MULTI:
3529 case Tabular::APPEND_ROW:
3530 case Tabular::APPEND_COLUMN:
3531 case Tabular::DELETE_ROW:
3532 case Tabular::DELETE_COLUMN:
3533 case Tabular::COPY_ROW:
3534 case Tabular::COPY_COLUMN:
3535 case Tabular::SET_ALL_LINES:
3536 case Tabular::UNSET_ALL_LINES:
3537 case Tabular::SET_TOP_SPACE:
3538 case Tabular::SET_BOTTOM_SPACE:
3539 case Tabular::SET_INTERLINE_SPACE:
3543 case Tabular::MULTICOLUMN:
3544 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3547 case Tabular::M_TOGGLE_LINE_TOP:
3549 case Tabular::TOGGLE_LINE_TOP:
3550 status.setOnOff(tabular.topLine(cur.idx(), flag));
3553 case Tabular::M_TOGGLE_LINE_BOTTOM:
3555 case Tabular::TOGGLE_LINE_BOTTOM:
3556 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3559 case Tabular::M_TOGGLE_LINE_LEFT:
3561 case Tabular::TOGGLE_LINE_LEFT:
3562 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3565 case Tabular::M_TOGGLE_LINE_RIGHT:
3567 case Tabular::TOGGLE_LINE_RIGHT:
3568 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3571 case Tabular::M_ALIGN_LEFT:
3573 case Tabular::ALIGN_LEFT:
3574 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3577 case Tabular::M_ALIGN_RIGHT:
3579 case Tabular::ALIGN_RIGHT:
3580 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3583 case Tabular::M_ALIGN_CENTER:
3585 case Tabular::ALIGN_CENTER:
3586 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3589 case Tabular::ALIGN_BLOCK:
3590 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3591 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3594 case Tabular::M_VALIGN_TOP:
3596 case Tabular::VALIGN_TOP:
3598 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3601 case Tabular::M_VALIGN_BOTTOM:
3603 case Tabular::VALIGN_BOTTOM:
3605 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3608 case Tabular::M_VALIGN_MIDDLE:
3610 case Tabular::VALIGN_MIDDLE:
3612 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3615 case Tabular::SET_LONGTABULAR:
3616 status.setOnOff(tabular.isLongTabular());
3619 case Tabular::UNSET_LONGTABULAR:
3620 status.setOnOff(!tabular.isLongTabular());
3623 case Tabular::TOGGLE_ROTATE_TABULAR:
3624 case Tabular::SET_ROTATE_TABULAR:
3625 status.setOnOff(tabular.getRotateTabular());
3628 case Tabular::UNSET_ROTATE_TABULAR:
3629 status.setOnOff(!tabular.getRotateTabular());
3632 case Tabular::TOGGLE_ROTATE_CELL:
3633 case Tabular::SET_ROTATE_CELL:
3634 status.setOnOff(!oneCellHasRotationState(false,
3635 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3638 case Tabular::UNSET_ROTATE_CELL:
3639 status.setOnOff(!oneCellHasRotationState(true,
3640 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3643 case Tabular::SET_USEBOX:
3644 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3647 case Tabular::SET_LTFIRSTHEAD:
3648 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3651 case Tabular::UNSET_LTFIRSTHEAD:
3652 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3655 case Tabular::SET_LTHEAD:
3656 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3659 case Tabular::UNSET_LTHEAD:
3660 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3663 case Tabular::SET_LTFOOT:
3664 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3667 case Tabular::UNSET_LTFOOT:
3668 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3671 case Tabular::SET_LTLASTFOOT:
3672 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3675 case Tabular::UNSET_LTLASTFOOT:
3676 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3679 case Tabular::SET_LTNEWPAGE:
3680 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3683 case Tabular::SET_BOOKTABS:
3684 status.setOnOff(tabular.useBookTabs());
3687 case Tabular::UNSET_BOOKTABS:
3688 status.setOnOff(!tabular.useBookTabs());
3693 status.enabled(false);
3699 // These are only enabled inside tabular
3700 case LFUN_CELL_BACKWARD:
3701 case LFUN_CELL_FORWARD:
3702 status.enabled(true);
3705 // disable these with multiple cells selected
3706 case LFUN_INSET_INSERT:
3707 case LFUN_TABULAR_INSERT:
3708 case LFUN_FLEX_INSERT:
3709 case LFUN_FLOAT_INSERT:
3710 case LFUN_FLOAT_WIDE_INSERT:
3711 case LFUN_FOOTNOTE_INSERT:
3712 case LFUN_MARGINALNOTE_INSERT:
3713 case LFUN_MATH_INSERT:
3714 case LFUN_MATH_MODE:
3715 case LFUN_MATH_MUTATE:
3716 case LFUN_MATH_DISPLAY:
3717 case LFUN_NOTE_INSERT:
3718 case LFUN_OPTIONAL_INSERT:
3719 case LFUN_BOX_INSERT:
3720 case LFUN_BRANCH_INSERT:
3721 case LFUN_WRAP_INSERT:
3722 case LFUN_ERT_INSERT: {
3723 if (tablemode(cur)) {
3724 status.enabled(false);
3727 return cell(cur.idx())->getStatus(cur, cmd, status);
3730 // disable in non-fixed-width cells
3731 case LFUN_BREAK_LINE:
3732 case LFUN_BREAK_PARAGRAPH:
3733 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3734 case LFUN_BREAK_PARAGRAPH_SKIP: {
3735 if (tabular.getPWidth(cur.idx()).zero()) {
3736 status.enabled(false);
3739 return cell(cur.idx())->getStatus(cur, cmd, status);
3743 if (tabularStackDirty() && theClipboard().isInternal()) {
3744 status.enabled(true);
3747 return cell(cur.idx())->getStatus(cur, cmd, status);
3749 case LFUN_INSET_MODIFY:
3750 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3751 status.enabled(true);
3757 // we try to handle this event in the insets dispatch function.
3758 return cell(cur.idx())->getStatus(cur, cmd, status);
3763 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3764 OutputParams const & runparams) const
3766 return tabular.latex(buf, os, runparams);
3770 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3771 OutputParams const & runparams) const
3773 os << '\n'; // output table on a new line
3774 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3775 tabular.plaintext(buf, os, runparams, dp, false, 0);
3776 return PLAINTEXT_NEWLINE;
3780 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3781 OutputParams const & runparams) const
3786 // FIXME: Why not pass a proper DocIterator here?
3788 // if the table is inside a float it doesn't need the informaltable
3789 // wrapper. Search for it.
3790 for (master = owner(); master; master = master->owner())
3791 if (master->lyxCode() == Inset::FLOAT_CODE)
3796 os << "<informaltable>";
3799 ret += tabular.docbook(buf, os, runparams);
3801 os << "</informaltable>";
3808 void InsetTabular::validate(LaTeXFeatures & features) const
3810 tabular.validate(features);
3814 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3816 return tabular.getCellInset(idx);
3820 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3822 return tabular.getCellInset(idx);
3826 void InsetTabular::cursorPos(BufferView const & bv,
3827 CursorSlice const & sl, bool boundary, int & x, int & y) const
3829 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3831 // y offset correction
3832 int const row = tabular.cellRow(sl.idx());
3833 for (int i = 0; i <= row; ++i) {
3835 y += tabular.rowAscent(i);
3836 y += tabular.getAdditionalHeight(i);
3839 y += tabular.rowDescent(i);
3842 // x offset correction
3843 int const col = tabular.cellColumn(sl.idx());
3844 int idx = tabular.cellIndex(row, 0);
3845 for (int j = 0; j < col; ++j) {
3846 if (tabular.isPartOfMultiColumn(row, j))
3848 x += tabular.columnWidth(idx);
3851 x += tabular.getBeginningOfTextInCell(idx);
3852 x += ADD_TO_TABULAR_WIDTH;
3857 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3861 Inset const & inset = *tabular.getCellInset(cell);
3862 Point o = bv.coordCache().getInsets().xy(&inset);
3863 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3864 int const xend = xbeg + tabular.columnWidth(cell);
3865 row_type const row = tabular.cellRow(cell);
3866 int const ybeg = o.y_ - tabular.rowAscent(row) -
3867 tabular.getAdditionalHeight(row);
3868 int const yend = o.y_ + tabular.rowDescent(row);
3880 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3881 // << " ybeg=" << ybeg << " yend=" << yend
3882 // << " xx=" << xx << " yy=" << yy
3883 // << " dist=" << xx + yy << endl;
3888 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3890 //lyxerr << "InsetTabular::editXY: " << this << endl;
3891 cur.selection() = false;
3893 cur.idx() = getNearestCell(cur.bv(), x, y);
3895 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3899 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3901 cur.idx() = getNearestCell(cur.bv(), x, y);
3902 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3906 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3908 idx_type idx_min = 0;
3909 int dist_min = std::numeric_limits<int>::max();
3910 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3911 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3912 int const d = dist(bv, i, x, y);
3923 int InsetTabular::getCellXPos(idx_type const cell) const
3927 for (; !tabular.isFirstCellInRow(c); --c)
3930 for (; c < cell; ++c)
3931 lx += tabular.columnWidth(c);
3937 void InsetTabular::resetPos(Cursor & cur) const
3939 BufferView & bv = cur.bv();
3940 int const maxwidth = bv.workWidth();
3942 if (&cur.inset() != this) {
3946 int const X2 = maxwidth;
3947 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3948 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3949 int const x2 = x1 + tabular.columnWidth(cur.idx());
3952 scx_ = X1 + 20 - x1;
3954 scx_ = X2 - 20 - x2;
3959 cur.updateFlags(Update::Force | Update::FitCursor);
3963 void InsetTabular::moveNextCell(Cursor & cur)
3965 if (isRightToLeft(cur)) {
3966 if (tabular.isFirstCellInRow(cur.idx())) {
3967 row_type const row = tabular.cellRow(cur.idx());
3968 if (row == tabular.rowCount() - 1)
3970 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3977 if (tabular.isLastCell(cur.idx()))
3987 void InsetTabular::movePrevCell(Cursor & cur)
3989 if (isRightToLeft(cur)) {
3990 if (tabular.isLastCellInRow(cur.idx())) {
3991 row_type const row = tabular.cellRow(cur.idx());
3994 cur.idx() = tabular.getFirstCellInRow(row);
3995 cur.idx() = tabular.getCellAbove(cur.idx());
3997 if (tabular.isLastCell(cur.idx()))
4002 if (cur.idx() == 0) // first cell
4006 cur.pit() = cur.lastpit();
4007 cur.pos() = cur.lastpos();
4009 // FIXME: this accesses the position cache before it is initialized
4014 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4016 Tabular::Feature action = Tabular::LAST_ACTION;
4019 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4020 string const tmp = tabularFeature[i].feature;
4022 if (tmp == what.substr(0, tmp.length())) {
4023 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4024 //tabularFeatures[i].feature.length()))
4025 action = tabularFeature[i].action;
4029 if (action == Tabular::LAST_ACTION)
4033 ltrim(what.substr(tabularFeature[i].feature.length()));
4034 tabularFeatures(cur, action, val);
4039 static void checkLongtableSpecial(Tabular::ltType & ltt,
4040 string const & special, bool & flag)
4042 if (special == "dl_above") {
4045 } else if (special == "dl_below") {
4046 ltt.bottomDL = flag;
4048 } else if (special == "empty") {
4057 bool InsetTabular::oneCellHasRotationState(bool rotated,
4058 row_type row_start, row_type row_end,
4059 col_type col_start, col_type col_end) const {
4061 for (row_type i = row_start; i <= row_end; ++i) {
4062 for (col_type j = col_start; j <= col_end; ++j) {
4063 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4072 void InsetTabular::tabularFeatures(Cursor & cur,
4073 Tabular::Feature feature, string const & value)
4075 BufferView & bv = cur.bv();
4076 col_type sel_col_start;
4077 col_type sel_col_end;
4078 row_type sel_row_start;
4079 row_type sel_row_end;
4080 bool setLines = false;
4081 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4082 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4086 case Tabular::M_ALIGN_LEFT:
4087 case Tabular::ALIGN_LEFT:
4088 setAlign = LYX_ALIGN_LEFT;
4091 case Tabular::M_ALIGN_RIGHT:
4092 case Tabular::ALIGN_RIGHT:
4093 setAlign = LYX_ALIGN_RIGHT;
4096 case Tabular::M_ALIGN_CENTER:
4097 case Tabular::ALIGN_CENTER:
4098 setAlign = LYX_ALIGN_CENTER;
4101 case Tabular::ALIGN_BLOCK:
4102 setAlign = LYX_ALIGN_BLOCK;
4105 case Tabular::M_VALIGN_TOP:
4106 case Tabular::VALIGN_TOP:
4107 setVAlign = Tabular::LYX_VALIGN_TOP;
4110 case Tabular::M_VALIGN_BOTTOM:
4111 case Tabular::VALIGN_BOTTOM:
4112 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4115 case Tabular::M_VALIGN_MIDDLE:
4116 case Tabular::VALIGN_MIDDLE:
4117 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4124 recordUndoInset(cur, Undo::ATOMIC);
4126 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4127 row_type const row = tabular.cellRow(cur.idx());
4128 col_type const column = tabular.cellColumn(cur.idx());
4130 Tabular::ltType ltt;
4134 case Tabular::SET_PWIDTH: {
4135 Length const len(value);
4136 tabular.setColumnPWidth(cur, cur.idx(), len);
4138 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4139 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4143 case Tabular::SET_MPWIDTH:
4144 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4147 case Tabular::SET_SPECIAL_COLUMN:
4148 case Tabular::SET_SPECIAL_MULTI:
4149 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4152 case Tabular::APPEND_ROW:
4153 // append the row into the tabular
4154 tabular.appendRow(bv.buffer().params(), cur.idx());
4157 case Tabular::APPEND_COLUMN:
4158 // append the column into the tabular
4159 tabular.appendColumn(bv.buffer().params(), cur.idx());
4160 cur.idx() = tabular.cellIndex(row, column);
4163 case Tabular::DELETE_ROW:
4164 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4165 tabular.deleteRow(sel_row_start);
4166 if (sel_row_start >= tabular.rowCount())
4168 cur.idx() = tabular.cellIndex(sel_row_start, column);
4171 cur.selection() = false;
4174 case Tabular::DELETE_COLUMN:
4175 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4176 tabular.deleteColumn(sel_col_start);
4177 if (sel_col_start >= tabular.columnCount())
4179 cur.idx() = tabular.cellIndex(row, sel_col_start);
4182 cur.selection() = false;
4185 case Tabular::COPY_ROW:
4186 tabular.copyRow(bv.buffer().params(), row);
4189 case Tabular::COPY_COLUMN:
4190 tabular.copyColumn(bv.buffer().params(), column);
4191 cur.idx() = tabular.cellIndex(row, column);
4194 case Tabular::M_TOGGLE_LINE_TOP:
4196 case Tabular::TOGGLE_LINE_TOP: {
4197 bool lineSet = !tabular.topLine(cur.idx(), flag);
4198 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4199 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4201 tabular.cellIndex(i, j),
4206 case Tabular::M_TOGGLE_LINE_BOTTOM:
4208 case Tabular::TOGGLE_LINE_BOTTOM: {
4209 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4210 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4211 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4212 tabular.setBottomLine(
4213 tabular.cellIndex(i, j),
4219 case Tabular::M_TOGGLE_LINE_LEFT:
4221 case Tabular::TOGGLE_LINE_LEFT: {
4222 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4223 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4224 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4225 tabular.setLeftLine(
4226 tabular.cellIndex(i,j),
4232 case Tabular::M_TOGGLE_LINE_RIGHT:
4234 case Tabular::TOGGLE_LINE_RIGHT: {
4235 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4236 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4237 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4238 tabular.setRightLine(
4239 tabular.cellIndex(i,j),
4245 case Tabular::M_ALIGN_LEFT:
4246 case Tabular::M_ALIGN_RIGHT:
4247 case Tabular::M_ALIGN_CENTER:
4249 case Tabular::ALIGN_LEFT:
4250 case Tabular::ALIGN_RIGHT:
4251 case Tabular::ALIGN_CENTER:
4252 case Tabular::ALIGN_BLOCK:
4253 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4254 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4255 tabular.setAlignment(
4256 tabular.cellIndex(i, j),
4261 case Tabular::M_VALIGN_TOP:
4262 case Tabular::M_VALIGN_BOTTOM:
4263 case Tabular::M_VALIGN_MIDDLE:
4265 case Tabular::VALIGN_TOP:
4266 case Tabular::VALIGN_BOTTOM:
4267 case Tabular::VALIGN_MIDDLE:
4268 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4270 tabular.setVAlignment(
4271 tabular.cellIndex(i, j),
4275 case Tabular::MULTICOLUMN: {
4276 if (sel_row_start != sel_row_end) {
4277 // FIXME: Need I say it ? This is horrible.
4279 Alert::error(_("Error setting multicolumn"),
4280 _("You cannot set multicolumn vertically."));
4283 if (!cur.selection()) {
4284 // just multicol for one single cell
4285 // check whether we are completely in a multicol
4286 if (tabular.isMultiColumn(cur.idx()))
4287 tabular.unsetMultiColumn(cur.idx());
4289 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4292 // we have a selection so this means we just add all this
4293 // cells to form a multicolumn cell
4294 idx_type const s_start = cur.selBegin().idx();
4295 idx_type const s_end = cur.selEnd().idx();
4296 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4297 cur.idx() = s_start;
4300 cur.selection() = false;
4304 case Tabular::SET_ALL_LINES:
4306 case Tabular::UNSET_ALL_LINES:
4307 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4308 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4309 tabular.setAllLines(
4310 tabular.cellIndex(i,j), setLines);
4313 case Tabular::SET_LONGTABULAR:
4314 tabular.setLongTabular(true);
4317 case Tabular::UNSET_LONGTABULAR:
4318 tabular.setLongTabular(false);
4321 case Tabular::SET_ROTATE_TABULAR:
4322 tabular.setRotateTabular(true);
4325 case Tabular::UNSET_ROTATE_TABULAR:
4326 tabular.setRotateTabular(false);
4329 case Tabular::TOGGLE_ROTATE_TABULAR:
4330 tabular.setRotateTabular(!tabular.getRotateTabular());
4333 case Tabular::SET_ROTATE_CELL:
4334 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4335 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4336 tabular.setRotateCell(
4337 tabular.cellIndex(i, j), true);
4340 case Tabular::UNSET_ROTATE_CELL:
4341 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4342 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4343 tabular.setRotateCell(
4344 tabular.cellIndex(i, j), false);
4347 case Tabular::TOGGLE_ROTATE_CELL:
4349 bool oneNotRotated = oneCellHasRotationState(false,
4350 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4352 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4353 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4354 tabular.setRotateCell(tabular.cellIndex(i, j),
4359 case Tabular::SET_USEBOX: {
4360 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4361 if (val == tabular.getUsebox(cur.idx()))
4362 val = Tabular::BOX_NONE;
4363 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4364 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4365 tabular.setUsebox(tabular.cellIndex(i, j), val);
4369 case Tabular::UNSET_LTFIRSTHEAD:
4371 case Tabular::SET_LTFIRSTHEAD:
4372 tabular.getRowOfLTFirstHead(row, ltt);
4373 checkLongtableSpecial(ltt, value, flag);
4374 tabular.setLTHead(row, flag, ltt, true);
4377 case Tabular::UNSET_LTHEAD:
4379 case Tabular::SET_LTHEAD:
4380 tabular.getRowOfLTHead(row, ltt);
4381 checkLongtableSpecial(ltt, value, flag);
4382 tabular.setLTHead(row, flag, ltt, false);
4385 case Tabular::UNSET_LTFOOT:
4387 case Tabular::SET_LTFOOT:
4388 tabular.getRowOfLTFoot(row, ltt);
4389 checkLongtableSpecial(ltt, value, flag);
4390 tabular.setLTFoot(row, flag, ltt, false);
4393 case Tabular::UNSET_LTLASTFOOT:
4395 case Tabular::SET_LTLASTFOOT:
4396 tabular.getRowOfLTLastFoot(row, ltt);
4397 checkLongtableSpecial(ltt, value, flag);
4398 tabular.setLTFoot(row, flag, ltt, true);
4401 case Tabular::SET_LTNEWPAGE:
4402 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4405 case Tabular::SET_BOOKTABS:
4406 tabular.setBookTabs(true);
4409 case Tabular::UNSET_BOOKTABS:
4410 tabular.setBookTabs(false);
4413 case Tabular::SET_TOP_SPACE: {
4415 if (value == "default")
4416 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4417 tabular.row_info[i].top_space_default = true;
4418 else if (isValidLength(value, &len))
4419 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4420 tabular.row_info[i].top_space_default = false;
4421 tabular.row_info[i].top_space = len;
4424 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4425 tabular.row_info[i].top_space_default = false;
4426 tabular.row_info[i].top_space = len;
4431 case Tabular::SET_BOTTOM_SPACE: {
4433 if (value == "default")
4434 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4435 tabular.row_info[i].bottom_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].bottom_space_default = false;
4439 tabular.row_info[i].bottom_space = len;
4442 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4443 tabular.row_info[i].bottom_space_default = false;
4444 tabular.row_info[i].bottom_space = len;
4449 case Tabular::SET_INTERLINE_SPACE: {
4451 if (value == "default")
4452 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4453 tabular.row_info[i].interline_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].interline_space_default = false;
4457 tabular.row_info[i].interline_space = len;
4460 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4461 tabular.row_info[i].interline_space_default = false;
4462 tabular.row_info[i].interline_space = len;
4467 // dummy stuff just to avoid warnings
4468 case Tabular::LAST_ACTION:
4474 bool InsetTabular::showInsetDialog(BufferView * bv) const
4476 InsetTabularMailer(*this).showDialog(bv);
4481 void InsetTabular::openLayoutDialog(BufferView * bv) const
4483 InsetTabularMailer(*this).showDialog(bv);
4487 bool InsetTabular::copySelection(Cursor & cur)
4489 if (!cur.selection())
4494 getSelection(cur, rs, re, cs, ce);
4496 paste_tabular.reset(new Tabular(tabular));
4498 for (row_type i = 0; i < rs; ++i)
4499 paste_tabular->deleteRow(0);
4501 row_type const rows = re - rs + 1;
4502 while (paste_tabular->rowCount() > rows)
4503 paste_tabular->deleteRow(rows);
4505 paste_tabular->setTopLine(0, true, true);
4506 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4509 for (col_type i = 0; i < cs; ++i)
4510 paste_tabular->deleteColumn(0);
4512 col_type const columns = ce - cs + 1;
4513 while (paste_tabular->columnCount() > columns)
4514 paste_tabular->deleteColumn(columns);
4516 paste_tabular->setLeftLine(0, true, true);
4517 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4520 odocstringstream os;
4521 OutputParams const runparams(0);
4522 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4523 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4524 cap::copySelection(cur, os.str());
4526 // mark tabular stack dirty
4527 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4528 // when we (hopefully) have a one-for-all paste mechanism.
4529 // This must be called after cap::copySelection.
4530 dirtyTabularStack(true);
4536 bool InsetTabular::pasteClipboard(Cursor & cur)
4540 col_type const actcol = tabular.cellColumn(cur.idx());
4541 row_type const actrow = tabular.cellRow(cur.idx());
4542 for (row_type r1 = 0, r2 = actrow;
4543 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4545 for (col_type c1 = 0, c2 = actcol;
4546 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4548 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4549 tabular.isPartOfMultiColumn(r2, c2))
4551 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4555 if (tabular.isPartOfMultiColumn(r2, c2)) {
4559 shared_ptr<InsetText> inset(
4560 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4561 tabular.setCellInset(r2, c2, inset);
4562 // FIXME: change tracking (MG)
4563 inset->setChange(Change(cur.buffer().params().trackChanges ?
4564 Change::INSERTED : Change::UNCHANGED));
4572 void InsetTabular::cutSelection(Cursor & cur)
4574 if (!cur.selection())
4579 getSelection(cur, rs, re, cs, ce);
4580 for (row_type i = rs; i <= re; ++i) {
4581 for (col_type j = cs; j <= ce; ++j) {
4582 shared_ptr<InsetText> t
4583 = cell(tabular.cellIndex(i, j));
4584 if (cur.buffer().params().trackChanges)
4585 // FIXME: Change tracking (MG)
4586 t->setChange(Change(Change::DELETED));
4592 // cursor position might be invalid now
4593 if (cur.pit() > cur.lastpit())
4594 cur.pit() = cur.lastpit();
4595 if (cur.pos() > cur.lastpos())
4596 cur.pos() = cur.lastpos();
4597 cur.clearSelection();
4601 bool InsetTabular::isRightToLeft(Cursor & cur) const
4603 BOOST_ASSERT(cur.depth() > 1);
4604 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4605 pos_type const parentpos = cur[cur.depth() - 2].pos();
4606 return parentpar.getFontSettings(cur.bv().buffer().params(),
4607 parentpos).language()->rightToLeft();
4611 void InsetTabular::getSelection(Cursor & cur,
4612 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4614 CursorSlice const & beg = cur.selBegin();
4615 CursorSlice const & end = cur.selEnd();
4616 cs = tabular.cellColumn(beg.idx());
4617 ce = tabular.cellColumn(end.idx());
4620 cs = tabular.cellColumn(end.idx());
4622 ce = tabular.cellRightColumn(end.idx());
4625 rs = tabular.cellRow(beg.idx());
4626 re = tabular.cellRow(end.idx());
4632 Text * InsetTabular::getText(int idx) const
4634 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4638 void InsetTabular::setChange(Change const & change)
4640 for (idx_type idx = 0; idx < nargs(); ++idx)
4641 cell(idx)->setChange(change);
4645 void InsetTabular::acceptChanges(BufferParams const & bparams)
4647 for (idx_type idx = 0; idx < nargs(); ++idx)
4648 cell(idx)->acceptChanges(bparams);
4652 void InsetTabular::rejectChanges(BufferParams const & bparams)
4654 for (idx_type idx = 0; idx < nargs(); ++idx)
4655 cell(idx)->rejectChanges(bparams);
4659 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4661 return tabular.getPWidth(cell).zero();
4665 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4668 if (buf.length() <= 0)
4671 Buffer const & buffer = bv.buffer();
4675 col_type maxCols = 1;
4676 docstring::size_type const len = buf.length();
4677 docstring::size_type p = 0;
4680 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4688 maxCols = max(cols, maxCols);
4694 maxCols = max(cols, maxCols);
4700 paste_tabular.reset(
4701 new Tabular(buffer.params(), rows, maxCols));
4702 loctab = paste_tabular.get();
4704 dirtyTabularStack(true);
4707 cell = bv.cursor().idx();
4708 ocol = tabular.cellColumn(cell);
4709 row = tabular.cellRow(cell);
4712 docstring::size_type op = 0;
4713 idx_type const cells = loctab->cellCount();
4716 rows = loctab->rowCount();
4717 col_type const columns = loctab->columnCount();
4719 while (cell < cells && p < len && row < rows &&
4720 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4726 // we can only set this if we are not too far right
4727 if (cols < columns) {
4728 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4729 Font const font = bv.textMetrics(&inset->text_).
4730 getDisplayFont(0, 0);
4731 inset->setText(buf.substr(op, p - op), font,
4732 buffer.params().trackChanges);
4738 // we can only set this if we are not too far right
4739 if (cols < columns) {
4740 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4741 Font const font = bv.textMetrics(&inset->text_).
4742 getDisplayFont(0, 0);
4743 inset->setText(buf.substr(op, p - op), font,
4744 buffer.params().trackChanges);
4749 cell = loctab->cellIndex(row, cols);
4755 // check for the last cell if there is no trailing '\n'
4756 if (cell < cells && op < len) {
4757 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4758 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4759 inset->setText(buf.substr(op, len - op), font,
4760 buffer.params().trackChanges);
4766 void InsetTabular::addPreview(PreviewLoader & loader) const
4768 row_type const rows = tabular.rowCount();
4769 col_type const columns = tabular.columnCount();
4770 for (row_type i = 0; i < rows; ++i) {
4771 for (col_type j = 0; j < columns; ++j)
4772 tabular.getCellInset(i, j)->addPreview(loader);
4777 bool InsetTabular::tablemode(Cursor & cur) const
4779 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4785 string const InsetTabularMailer::name_("tabular");
4787 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4788 : inset_(const_cast<InsetTabular &>(inset))
4792 string const InsetTabularMailer::inset2string(Buffer const &) const
4794 return params2string(inset_);
4798 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4800 istringstream data(in);
4802 lex.setStream(data);
4809 if (!lex || token != name_)
4810 return print_mailer_error("InsetTabularMailer", in, 1,
4813 // This is part of the inset proper that is usually swallowed
4814 // by Buffer::readInset
4816 if (!lex || token != "Tabular")
4817 return print_mailer_error("InsetTabularMailer", in, 2,
4820 Buffer const & buffer = inset.buffer();
4821 inset.read(buffer, lex);
4825 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4828 data << name_ << ' ';
4829 inset.write(inset.buffer(), data);
4830 data << "\\end_inset\n";