2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "CoordCache.h"
30 #include "CutAndPaste.h"
31 #include "support/debug.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
35 #include "support/gettext.h"
37 #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"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "support/convert.h"
50 #include "support/docstream.h"
51 #include "support/FileName.h"
52 #include "support/lstrings.h"
54 #include "frontends/alert.h"
55 #include "frontends/Clipboard.h"
56 #include "frontends/Painter.h"
57 #include "frontends/Selection.h"
59 #include <boost/scoped_ptr.hpp>
67 using boost::shared_ptr;
68 using boost::dynamic_pointer_cast;
72 using support::prefixIs;
73 using support::FileName;
76 using support::suffixIs;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2;
92 int const ADD_TO_TABULAR_WIDTH = 2;
93 int const default_line_space = 10;
94 int const WIDTH_OF_LINE = 5;
98 boost::scoped_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 { Tabular::APPEND_ROW, "append-row" },
110 { Tabular::APPEND_COLUMN, "append-column" },
111 { Tabular::DELETE_ROW, "delete-row" },
112 { Tabular::DELETE_COLUMN, "delete-column" },
113 { Tabular::COPY_ROW, "copy-row" },
114 { Tabular::COPY_COLUMN, "copy-column" },
115 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
116 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
117 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
118 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
119 { Tabular::ALIGN_LEFT, "align-left" },
120 { Tabular::ALIGN_RIGHT, "align-right" },
121 { Tabular::ALIGN_CENTER, "align-center" },
122 { Tabular::ALIGN_BLOCK, "align-block" },
123 { Tabular::VALIGN_TOP, "valign-top" },
124 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
125 { Tabular::VALIGN_MIDDLE, "valign-middle" },
126 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
127 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
128 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
129 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
130 { Tabular::M_ALIGN_LEFT, "m-align-left" },
131 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
132 { Tabular::M_ALIGN_CENTER, "m-align-center" },
133 { Tabular::M_VALIGN_TOP, "m-valign-top" },
134 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
135 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
136 { Tabular::MULTICOLUMN, "multicolumn" },
137 { Tabular::SET_ALL_LINES, "set-all-lines" },
138 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
139 { Tabular::SET_LONGTABULAR, "set-longtabular" },
140 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
141 { Tabular::SET_PWIDTH, "set-pwidth" },
142 { Tabular::SET_MPWIDTH, "set-mpwidth" },
143 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
144 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
145 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
146 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
147 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
148 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
149 { Tabular::SET_USEBOX, "set-usebox" },
150 { Tabular::SET_LTHEAD, "set-lthead" },
151 { Tabular::UNSET_LTHEAD, "unset-lthead" },
152 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
153 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
154 { Tabular::SET_LTFOOT, "set-ltfoot" },
155 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
156 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
157 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
158 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
159 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
160 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
161 { Tabular::SET_BOOKTABS, "set-booktabs" },
162 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
163 { Tabular::SET_TOP_SPACE, "set-top-space" },
164 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
165 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
166 { Tabular::LAST_ACTION, "" }
170 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
172 FeatureEqual(Tabular::Feature feature)
173 : feature_(feature) {}
174 bool operator()(TabularFeature const & tf) const {
175 return tf.action == feature_;
178 Tabular::Feature feature_;
183 string const write_attribute(string const & name, T const & t)
185 string const s = tostr(t);
186 return s.empty() ? s : " " + name + "=\"" + s + "\"";
190 string const write_attribute(string const & name, string const & t)
192 return t.empty() ? t : " " + name + "=\"" + t + "\"";
197 string const write_attribute(string const & name, docstring const & t)
199 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
204 string const write_attribute(string const & name, bool const & b)
206 // we write only true attribute values so we remove a bit of the
207 // file format bloat for tabulars.
208 return b ? write_attribute(name, convert<string>(b)) : string();
213 string const write_attribute(string const & name, int const & i)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return i ? write_attribute(name, convert<string>(i)) : string();
222 string const write_attribute(string const & name, Tabular::idx_type const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Length const & value)
233 // we write only the value if we really have one same reson as above.
234 return value.zero() ? string() : write_attribute(name, value.asString());
238 string const tostr(LyXAlignment const & num)
243 case LYX_ALIGN_BLOCK:
247 case LYX_ALIGN_CENTER:
249 case LYX_ALIGN_RIGHT:
251 case LYX_ALIGN_LAYOUT:
253 case LYX_ALIGN_SPECIAL:
260 string const tostr(Tabular::VAlignment const & num)
263 case Tabular::LYX_VALIGN_TOP:
265 case Tabular::LYX_VALIGN_MIDDLE:
267 case Tabular::LYX_VALIGN_BOTTOM:
274 string const tostr(Tabular::BoxType const & num)
277 case Tabular::BOX_NONE:
279 case Tabular::BOX_PARBOX:
281 case Tabular::BOX_MINIPAGE:
288 // I would have liked a fromstr template a lot better. (Lgb)
289 bool string2type(string const str, LyXAlignment & num)
292 num = LYX_ALIGN_NONE;
293 else if (str == "block")
294 num = LYX_ALIGN_BLOCK;
295 else if (str == "left")
296 num = LYX_ALIGN_LEFT;
297 else if (str == "center")
298 num = LYX_ALIGN_CENTER;
299 else if (str == "right")
300 num = LYX_ALIGN_RIGHT;
307 bool string2type(string const str, Tabular::VAlignment & num)
310 num = Tabular::LYX_VALIGN_TOP;
311 else if (str == "middle" )
312 num = Tabular::LYX_VALIGN_MIDDLE;
313 else if (str == "bottom")
314 num = Tabular::LYX_VALIGN_BOTTOM;
321 bool string2type(string const str, Tabular::BoxType & num)
324 num = Tabular::BOX_NONE;
325 else if (str == "parbox")
326 num = Tabular::BOX_PARBOX;
327 else if (str == "minipage")
328 num = Tabular::BOX_MINIPAGE;
335 bool string2type(string const str, bool & num)
339 else if (str == "false")
347 bool getTokenValue(string const & str, char const * token, string & ret)
350 size_t token_length = strlen(token);
351 string::size_type pos = str.find(token);
353 if (pos == string::npos || pos + token_length + 1 >= str.length()
354 || str[pos + token_length] != '=')
356 pos += token_length + 1;
358 if (ch != '"' && ch != '\'') { // only read till next space
362 while (pos < str.length() - 1 && str[++pos] != ch)
369 bool getTokenValue(string const & str, char const * token, docstring & ret)
372 bool const success = getTokenValue(str, token, tmp);
373 ret = from_utf8(tmp);
378 bool getTokenValue(string const & str, char const * token, int & num)
382 if (!getTokenValue(str, token, tmp))
384 num = convert<int>(tmp);
389 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
392 return getTokenValue(str, token, tmp) && string2type(tmp, num);
396 bool getTokenValue(string const & str, char const * token,
397 Tabular::VAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::BoxType & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token, bool & flag)
414 // set the flag always to false as this should be the default for bools
415 // not in the file-format.
418 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
422 bool getTokenValue(string const & str, char const * token, Length & len)
424 // set the length to be zero() as default as this it should be if not
425 // in the file format.
428 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
432 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
437 if (!getTokenValue(str, token, tmp))
439 if (tmp == "default") {
443 return isValidLength(tmp, &len);
447 void l_getline(istream & is, string & str)
450 while (str.empty()) {
452 if (!str.empty() && str[str.length() - 1] == '\r')
453 str.erase(str.length() - 1);
460 string const featureAsString(Tabular::Feature feature)
462 TabularFeature * end = tabularFeature +
463 sizeof(tabularFeature) / sizeof(TabularFeature);
464 TabularFeature * it = std::find_if(tabularFeature, end,
465 FeatureEqual(feature));
466 return (it == end) ? string() : it->feature;
471 /////////////////////////////////////////////////////////////////////
475 /////////////////////////////////////////////////////////////////////
478 Tabular::cellstruct::cellstruct(BufferParams const & bp)
481 multicolumn(Tabular::CELL_NORMAL),
482 alignment(LYX_ALIGN_CENTER),
483 valignment(LYX_VALIGN_TOP),
490 inset(new InsetText(bp))
494 Tabular::cellstruct::cellstruct(cellstruct const & cs)
497 multicolumn(cs.multicolumn),
498 alignment(cs.alignment),
499 valignment(cs.valignment),
500 top_line(cs.top_line),
501 bottom_line(cs.bottom_line),
502 left_line(cs.left_line),
503 right_line(cs.right_line),
506 align_special(cs.align_special),
508 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
512 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
519 void Tabular::cellstruct::swap(cellstruct & rhs)
521 std::swap(cellno, rhs.cellno);
522 std::swap(width, rhs.width);
523 std::swap(multicolumn, rhs.multicolumn);
524 std::swap(alignment, rhs.alignment);
525 std::swap(valignment, rhs.valignment);
526 std::swap(top_line, rhs.top_line);
527 std::swap(bottom_line, rhs.bottom_line);
528 std::swap(left_line, rhs.left_line);
529 std::swap(right_line, rhs.right_line);
530 std::swap(usebox, rhs.usebox);
531 std::swap(rotate, rhs.rotate);
532 std::swap(align_special, rhs.align_special);
533 p_width.swap(rhs.p_width);
534 inset.swap(rhs.inset);
538 Tabular::rowstruct::rowstruct()
543 top_space_default(false),
544 bottom_space_default(false),
545 interline_space_default(false),
554 Tabular::columnstruct::columnstruct()
555 : alignment(LYX_ALIGN_CENTER),
556 valignment(LYX_VALIGN_TOP),
564 Tabular::ltType::ltType()
577 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
578 col_type columns_arg)
580 init(bp, rows_arg, columns_arg);
584 // activates all lines and sets all widths to 0
585 void Tabular::init(BufferParams const & bp, row_type rows_arg,
586 col_type columns_arg)
588 row_info = row_vector(rows_arg);
589 column_info = column_vector(columns_arg);
590 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
591 row_info.reserve(10);
592 column_info.reserve(10);
593 cell_info.reserve(100);
595 for (row_type i = 0; i < rows_arg; ++i)
596 cell_info[i].back().right_line = true;
597 row_info.back().bottom_line = true;
598 row_info.front().bottom_line = true;
599 column_info.back().right_line = true;
600 is_long_tabular = false;
602 use_booktabs = false;
606 void Tabular::fixCellNums()
609 for (row_type i = 0; i < rowCount(); ++i) {
610 for (col_type j = 0; j < columnCount(); ++j) {
611 // When debugging it can be nice to set
613 cell_info[i][j].inset->setDrawFrame(false);
614 cell_info[i][j].cellno = cellno++;
616 cell_info[i].back().right_line = true;
623 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
625 row_type const row = cellRow(cell);
627 row_vector::iterator rit = row_info.begin() + row;
628 row_info.insert(rit, rowstruct());
629 // now set the values of the row before
630 row_info[row] = row_info[row + 1];
632 row_type const nrows = rowCount();
633 col_type const ncols = columnCount();
635 cell_vvector old(nrows - 1);
636 for (row_type i = 0; i < nrows - 1; ++i)
637 swap(cell_info[i], old[i]);
639 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
641 for (row_type i = 0; i <= row; ++i)
642 swap(cell_info[i], old[i]);
643 for (row_type i = row + 2; i < nrows; ++i)
644 swap(cell_info[i], old[i - 1]);
647 for (col_type j = 0; j < ncols; ++j)
648 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
654 void Tabular::deleteRow(row_type const row)
656 // Not allowed to delete last row
660 row_info.erase(row_info.begin() + row);
661 cell_info.erase(cell_info.begin() + row);
666 void Tabular::copyRow(BufferParams const & bp, row_type const row)
668 row_info.insert(row_info.begin() + row, row_info[row]);
669 cell_info.insert(cell_info.begin() + row, cell_info[row]);
672 for (col_type j = 0; j < columnCount(); ++j)
673 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
679 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
681 col_type const column = cellColumn(cell);
682 col_type const ncols = columnCount();
683 column_vector::iterator cit = column_info.begin() + column + 1;
684 column_info.insert(cit, columnstruct());
685 // set the column values of the column before
686 column_info[column + 1] = column_info[column];
688 for (row_type i = 0; i < rowCount(); ++i) {
689 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
691 // care about multicolumns
692 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
693 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
695 if (column + 2 >= ncols
696 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
697 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
700 for (row_type i = 0; i < rowCount(); ++i) {
701 cell_info[i][column + 1].inset->clear();
703 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
709 void Tabular::deleteColumn(col_type const column)
711 // Not allowed to delete last column
712 if (columnCount() == 1)
715 column_info.erase(column_info.begin() + column);
716 for (row_type i = 0; i < rowCount(); ++i) {
717 // Care about multicolumn cells
718 if (column + 1 < columnCount() &&
719 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
720 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
721 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
723 cell_info[i].erase(cell_info[i].begin() + column);
729 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
731 column_info.insert(column_info.begin() + column, column_info[column]);
733 for (row_type i = 0; i < rowCount(); ++i)
734 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
737 for (row_type i = 0; i < rowCount(); ++i)
738 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
743 void Tabular::updateIndexes()
746 // Count only non-multicol cells plus begin multicol
747 // cells, ignore non-begin multicol cells:
748 for (row_type row = 0; row < rowCount(); ++row) {
749 for (col_type column = 0; column < columnCount(); ++column) {
750 if (cell_info[row][column].multicolumn
751 != Tabular::CELL_PART_OF_MULTICOLUMN)
753 BOOST_ASSERT(numberofcells != 0);
754 cell_info[row][column].cellno =
759 rowofcell.resize(numberofcells);
760 columnofcell.resize(numberofcells);
765 c < numberofcells && row < rowCount() && column < columnCount();) {
767 columnofcell[c] = column;
771 } while (column < columnCount() &&
772 cell_info[row][column].multicolumn
773 == Tabular::CELL_PART_OF_MULTICOLUMN);
775 if (column == columnCount()) {
781 for (row_type row = 0; row < rowCount(); ++row) {
782 for (col_type column = 0; column < columnCount(); ++column) {
783 if (isPartOfMultiColumn(row,column))
785 cell_info[row][column].inset->setAutoBreakRows(
786 !getPWidth(cellIndex(row, column)).zero());
792 Tabular::idx_type Tabular::cellCount() const
794 return numberofcells;
798 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
800 row_type const row = cellRow(cell);
802 for (col_type i = 0; i < columnCount(); ++i)
803 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
809 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
811 if (!wholerow && isMultiColumn(cell) &&
812 !(use_booktabs && cellRow(cell) == 0))
813 return cellinfo_of_cell(cell).top_line;
814 return row_info[cellRow(cell)].top_line;
818 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
820 if (!wholerow && isMultiColumn(cell) &&
821 !(use_booktabs && isLastRow(cell)))
822 return cellinfo_of_cell(cell).bottom_line;
823 return row_info[cellRow(cell)].bottom_line;
827 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
831 if (!wholecolumn && isMultiColumn(cell) &&
832 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
833 return cellinfo_of_cell(cell).left_line;
834 return column_info[cellColumn(cell)].left_line;
838 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
842 if (!wholecolumn && isMultiColumn(cell) &&
843 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
844 return cellinfo_of_cell(cell).right_line;
845 return column_info[cellRightColumn(cell)].right_line;
849 bool Tabular::topAlreadyDrawn(idx_type cell) const
851 row_type row = cellRow(cell);
852 if (row > 0 && !getAdditionalHeight(row)) {
853 col_type column = cellColumn(cell);
856 && cell_info[row][column].multicolumn
857 == Tabular::CELL_PART_OF_MULTICOLUMN)
859 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
860 return row_info[row].bottom_line;
862 return cell_info[row][column].bottom_line;
868 bool Tabular::leftAlreadyDrawn(idx_type cell) const
870 col_type column = cellColumn(cell);
872 row_type row = cellRow(cell);
874 (cell_info[row][column].multicolumn ==
875 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
876 if (getAdditionalWidth(cell_info[row][column].cellno))
878 return rightLine(cell_info[row][column].cellno);
884 bool Tabular::isLastRow(idx_type cell) const
886 return cellRow(cell) == rowCount() - 1;
890 int Tabular::getAdditionalHeight(row_type row) const
892 if (!row || row >= rowCount())
898 for (col_type column = 0; column < columnCount() && bottom; ++column) {
899 switch (cell_info[row - 1][column].multicolumn) {
900 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
901 bottom = cell_info[row - 1][column].bottom_line;
903 case Tabular::CELL_NORMAL:
904 bottom = row_info[row - 1].bottom_line;
907 for (col_type column = 0; column < columnCount() && top; ++column) {
908 switch (cell_info[row][column].multicolumn) {
909 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
910 top = cell_info[row][column].top_line;
912 case Tabular::CELL_NORMAL:
913 top = row_info[row].top_line;
916 int const interline_space = row_info[row - 1].interline_space_default ?
918 row_info[row - 1].interline_space.inPixels(width());
920 return interline_space + WIDTH_OF_LINE;
921 return interline_space;
925 int Tabular::getAdditionalWidth(idx_type cell) const
927 // internally already set in setCellWidth
928 // used to get it back in text.cpp
929 col_type const col = cellRightColumn(cell);
930 row_type const row = cellRow(cell);
931 if (col < columnCount() - 1 && rightLine(cell) &&
932 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
934 return WIDTH_OF_LINE;
940 // returns the maximum over all rows
941 int Tabular::columnWidth(idx_type cell) const
943 col_type const column1 = cellColumn(cell);
944 col_type const column2 = cellRightColumn(cell);
946 for (col_type i = column1; i <= column2; ++i)
947 result += column_info[i].width;
952 int Tabular::width() const
955 for (col_type i = 0; i < columnCount(); ++i)
956 width += column_info[i].width;
961 // returns true if a complete update is necessary, otherwise false
962 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
964 if (!isMultiColumn(cell))
967 row_type const row = cellRow(cell);
968 col_type const column1 = cellColumn(cell);
969 col_type const column2 = cellRightColumn(cell);
970 int const old_val = cell_info[row][column2].width;
972 // first set columns to 0 so we can calculate the right width
973 for (col_type i = column1; i <= column2; ++i) {
974 cell_info[row][i].width = 0;
976 // set the width to MAX_WIDTH until width > 0
977 int width = new_width + 2 * WIDTH_OF_LINE;
978 col_type i = column1;
979 for (; i < column2 && width > column_info[i].width; ++i) {
980 cell_info[row][i].width = column_info[i].width;
981 width -= column_info[i].width;
984 cell_info[row][i].width = width;
986 if (old_val != cell_info[row][column2].width) {
987 // in this case we have to recalculate all multicolumn cells which
988 // have this column as one of theirs but not as last one
989 calculate_width_of_column_NMC(i);
990 recalculateMulticolumnsOfColumn(i);
991 calculate_width_of_column(i);
997 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
999 // the last column does not have to be recalculated because all
1000 // multicolumns will have here there last multicolumn cell which
1001 // always will have the whole rest of the width of the cell.
1002 if (columnCount() < 2 || column > (columnCount() - 2))
1004 for (row_type row = 0; row < rowCount(); ++row) {
1005 int mc = cell_info[row][column].multicolumn;
1006 int nmc = cell_info[row][column+1].multicolumn;
1007 // we only have to update multicolumns which do not have this
1008 // column as their last column!
1009 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1010 (mc == CELL_PART_OF_MULTICOLUMN &&
1011 nmc == CELL_PART_OF_MULTICOLUMN))
1013 idx_type const cellno = cell_info[row][column].cellno;
1014 setWidthOfMulticolCell(cellno,
1015 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1021 void Tabular::setCellWidth(idx_type cell, int new_width)
1023 row_type const row = cellRow(cell);
1024 col_type const column1 = cellColumn(cell);
1029 if (rightLine(cell_info[row][column1].cellno, true) &&
1030 column1 < columnCount() - 1 &&
1031 leftLine(cell_info[row][column1+1].cellno, true))
1033 add_width = WIDTH_OF_LINE;
1036 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1039 if (isMultiColumnReal(cell)) {
1040 tmp = setWidthOfMulticolCell(cell, new_width);
1042 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1043 cell_info[row][column1].width = width;
1044 tmp = calculate_width_of_column_NMC(column1);
1046 recalculateMulticolumnsOfColumn(column1);
1049 for (col_type i = 0; i < columnCount(); ++i)
1050 calculate_width_of_column(i);
1055 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1058 if (!isMultiColumn(cell) || onlycolumn)
1059 column_info[cellColumn(cell)].alignment = align;
1061 cellinfo_of_cell(cell).alignment = align;
1065 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1068 if (!isMultiColumn(cell) || onlycolumn)
1069 column_info[cellColumn(cell)].valignment = align;
1071 cellinfo_of_cell(cell).valignment = align;
1078 * Allow line and paragraph breaks for fixed width cells or disallow them,
1079 * merge cell paragraphs and reset layout to standard for variable width
1082 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1084 inset->setAutoBreakRows(fixedWidth);
1088 // merge all paragraphs to one
1089 BufferParams const & bp = cur.bv().buffer().params();
1090 while (inset->paragraphs().size() > 1)
1091 mergeParagraph(bp, inset->paragraphs(), 0);
1095 // undo information has already been recorded
1096 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1097 bp.getTextClass().defaultLayoutName());
1104 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1105 Length const & width)
1107 col_type const j = cellColumn(cell);
1109 column_info[j].p_width = width;
1110 for (row_type i = 0; i < rowCount(); ++i) {
1111 idx_type const cell = cellIndex(i, j);
1112 // because of multicolumns
1113 toggleFixedWidth(cur, getCellInset(cell).get(),
1114 !getPWidth(cell).zero());
1116 // cur paragraph can become invalid after paragraphs were merged
1117 if (cur.pit() > cur.lastpit())
1118 cur.pit() = cur.lastpit();
1119 // cur position can become invalid after newlines were removed
1120 if (cur.pos() > cur.lastpos())
1121 cur.pos() = cur.lastpos();
1125 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1126 Length const & width)
1128 if (!isMultiColumn(cell))
1131 cellinfo_of_cell(cell).p_width = width;
1132 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1133 // cur paragraph can become invalid after paragraphs were merged
1134 if (cur.pit() > cur.lastpit())
1135 cur.pit() = cur.lastpit();
1136 // cur position can become invalid after newlines were removed
1137 if (cur.pos() > cur.lastpos())
1138 cur.pos() = cur.lastpos();
1143 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1144 Tabular::Feature what)
1146 if (what == SET_SPECIAL_MULTI)
1147 cellinfo_of_cell(cell).align_special = special;
1149 column_info[cellColumn(cell)].align_special = special;
1153 void Tabular::setAllLines(idx_type cell, bool line)
1155 setTopLine(cell, line);
1156 setBottomLine(cell, line);
1157 setRightLine(cell, line);
1158 setLeftLine(cell, line);
1162 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1164 row_type const row = cellRow(cell);
1165 if (wholerow || !isMultiColumn(cell))
1166 row_info[row].top_line = line;
1168 cellinfo_of_cell(cell).top_line = line;
1172 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1174 if (wholerow || !isMultiColumn(cell))
1175 row_info[cellRow(cell)].bottom_line = line;
1177 cellinfo_of_cell(cell).bottom_line = line;
1181 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1183 if (wholecolumn || !isMultiColumn(cell))
1184 column_info[cellColumn(cell)].left_line = line;
1186 cellinfo_of_cell(cell).left_line = line;
1190 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1192 if (wholecolumn || !isMultiColumn(cell))
1193 column_info[cellRightColumn(cell)].right_line = line;
1195 cellinfo_of_cell(cell).right_line = line;
1199 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1201 if (!onlycolumn && isMultiColumn(cell))
1202 return cellinfo_of_cell(cell).alignment;
1203 return column_info[cellColumn(cell)].alignment;
1208 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1210 if (!onlycolumn && isMultiColumn(cell))
1211 return cellinfo_of_cell(cell).valignment;
1212 return column_info[cellColumn(cell)].valignment;
1216 Length const Tabular::getPWidth(idx_type cell) const
1218 if (isMultiColumn(cell))
1219 return cellinfo_of_cell(cell).p_width;
1220 return column_info[cellColumn(cell)].p_width;
1224 Length const Tabular::getColumnPWidth(idx_type cell) const
1226 return column_info[cellColumn(cell)].p_width;
1230 Length const Tabular::getMColumnPWidth(idx_type cell) const
1232 if (isMultiColumn(cell))
1233 return cellinfo_of_cell(cell).p_width;
1238 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1240 if (what == SET_SPECIAL_MULTI)
1241 return cellinfo_of_cell(cell).align_special;
1242 return column_info[cellColumn(cell)].align_special;
1246 int Tabular::cellWidth(idx_type cell) const
1248 row_type const row = cellRow(cell);
1249 col_type const column1 = cellColumn(cell);
1250 col_type const column2 = cellRightColumn(cell);
1252 for (col_type i = column1; i <= column2; ++i)
1253 result += cell_info[row][i].width;
1258 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1262 switch (getAlignment(cell)) {
1263 case LYX_ALIGN_CENTER:
1264 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1266 case LYX_ALIGN_RIGHT:
1267 x += columnWidth(cell) - cellWidth(cell);
1268 // + getAdditionalWidth(cell);
1271 // LYX_ALIGN_LEFT: nothing :-)
1275 // the LaTeX Way :-(
1281 bool Tabular::isFirstCellInRow(idx_type cell) const
1283 return cellColumn(cell) == 0;
1287 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1289 if (row > rowCount() - 1)
1290 row = rowCount() - 1;
1291 return cell_info[row][0].cellno;
1295 bool Tabular::isLastCellInRow(idx_type cell) const
1297 return cellRightColumn(cell) == columnCount() - 1;
1301 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1303 if (row > rowCount() - 1)
1304 row = rowCount() - 1;
1305 return cell_info[row][columnCount() - 1].cellno;
1309 void Tabular::calculate_width_of_column(col_type column)
1312 for (row_type i = 0; i < rowCount(); ++i)
1313 maximum = max(cell_info[i][column].width, maximum);
1314 column_info[column].width = maximum;
1319 // Calculate the columns regarding ONLY the normal cells and if this
1320 // column is inside a multicolumn cell then use it only if its the last
1321 // column of this multicolumn cell as this gives an added width to the
1322 // column, all the rest should be adapted!
1324 bool Tabular::calculate_width_of_column_NMC(col_type column)
1326 int const old_column_width = column_info[column].width;
1328 for (row_type i = 0; i < rowCount(); ++i) {
1329 idx_type cell = cellIndex(i, column);
1330 bool ismulti = isMultiColumnReal(cell);
1331 if ((!ismulti || column == cellRightColumn(cell)) &&
1332 cell_info[i][column].width > max)
1334 max = cell_info[i][column].width;
1337 column_info[column].width = max;
1338 return column_info[column].width != old_column_width;
1342 Tabular::row_type Tabular::cellRow(idx_type cell) const
1344 if (cell >= cellCount())
1345 return rowCount() - 1;
1348 return rowofcell[cell];
1352 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1354 if (cell >= cellCount())
1355 return columnCount() - 1;
1358 return columnofcell[cell];
1362 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1364 row_type const row = cellRow(cell);
1365 col_type column = cellColumn(cell);
1366 while (column < columnCount() - 1 &&
1367 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1373 void Tabular::write(Buffer const & buf, ostream & os) const
1377 << write_attribute("version", 3)
1378 << write_attribute("rows", rowCount())
1379 << write_attribute("columns", columnCount())
1381 // global longtable options
1383 << write_attribute("rotate", rotate)
1384 << write_attribute("booktabs", use_booktabs)
1385 << write_attribute("islongtable", is_long_tabular)
1386 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1387 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1388 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1389 << write_attribute("headTopDL", endhead.topDL)
1390 << write_attribute("headBottomDL", endhead.bottomDL)
1391 << write_attribute("footTopDL", endfoot.topDL)
1392 << write_attribute("footBottomDL", endfoot.bottomDL)
1393 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1394 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1395 << write_attribute("lastFootEmpty", endlastfoot.empty)
1397 for (col_type j = 0; j < columnCount(); ++j) {
1399 << write_attribute("alignment", column_info[j].alignment)
1400 << write_attribute("valignment", column_info[j].valignment)
1401 << write_attribute("leftline", column_info[j].left_line)
1402 << write_attribute("rightline", column_info[j].right_line)
1403 << write_attribute("width", column_info[j].p_width.asString())
1404 << write_attribute("special", column_info[j].align_special)
1407 for (row_type i = 0; i < rowCount(); ++i) {
1409 << write_attribute("topline", row_info[i].top_line)
1410 << write_attribute("bottomline", row_info[i].bottom_line);
1411 static const string def("default");
1412 if (row_info[i].top_space_default)
1413 os << write_attribute("topspace", def);
1415 os << write_attribute("topspace", row_info[i].top_space);
1416 if (row_info[i].bottom_space_default)
1417 os << write_attribute("bottomspace", def);
1419 os << write_attribute("bottomspace", row_info[i].bottom_space);
1420 if (row_info[i].interline_space_default)
1421 os << write_attribute("interlinespace", def);
1423 os << write_attribute("interlinespace", row_info[i].interline_space);
1424 os << write_attribute("endhead", row_info[i].endhead)
1425 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1426 << write_attribute("endfoot", row_info[i].endfoot)
1427 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1428 << write_attribute("newpage", row_info[i].newpage)
1430 for (col_type j = 0; j < columnCount(); ++j) {
1432 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1433 << write_attribute("alignment", cell_info[i][j].alignment)
1434 << write_attribute("valignment", cell_info[i][j].valignment)
1435 << write_attribute("topline", cell_info[i][j].top_line)
1436 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1437 << write_attribute("leftline", cell_info[i][j].left_line)
1438 << write_attribute("rightline", cell_info[i][j].right_line)
1439 << write_attribute("rotate", cell_info[i][j].rotate)
1440 << write_attribute("usebox", cell_info[i][j].usebox)
1441 << write_attribute("width", cell_info[i][j].p_width)
1442 << write_attribute("special", cell_info[i][j].align_special)
1444 os << "\\begin_inset ";
1445 cell_info[i][j].inset->write(buf, os);
1446 os << "\n\\end_inset\n"
1451 os << "</lyxtabular>\n";
1455 void Tabular::read(Buffer const & buf, Lexer & lex)
1458 istream & is = lex.getStream();
1460 l_getline(is, line);
1461 if (!prefixIs(line, "<lyxtabular ")
1462 && !prefixIs(line, "<Tabular ")) {
1463 BOOST_ASSERT(false);
1468 if (!getTokenValue(line, "version", version))
1470 BOOST_ASSERT(version >= 2);
1473 if (!getTokenValue(line, "rows", rows_arg))
1476 if (!getTokenValue(line, "columns", columns_arg))
1478 init(buf.params(), rows_arg, columns_arg);
1479 l_getline(is, line);
1480 if (!prefixIs(line, "<features")) {
1481 lyxerr << "Wrong tabular format (expected <features ...> got"
1482 << line << ')' << endl;
1485 getTokenValue(line, "rotate", rotate);
1486 getTokenValue(line, "booktabs", use_booktabs);
1487 getTokenValue(line, "islongtable", is_long_tabular);
1488 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1489 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1490 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1491 getTokenValue(line, "headTopDL", endhead.topDL);
1492 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1493 getTokenValue(line, "footTopDL", endfoot.topDL);
1494 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1495 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1496 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1497 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1499 for (col_type j = 0; j < columnCount(); ++j) {
1501 if (!prefixIs(line,"<column")) {
1502 lyxerr << "Wrong tabular format (expected <column ...> got"
1503 << line << ')' << endl;
1506 getTokenValue(line, "alignment", column_info[j].alignment);
1507 getTokenValue(line, "valignment", column_info[j].valignment);
1508 getTokenValue(line, "leftline", column_info[j].left_line);
1509 getTokenValue(line, "rightline", column_info[j].right_line);
1510 getTokenValue(line, "width", column_info[j].p_width);
1511 getTokenValue(line, "special", column_info[j].align_special);
1514 for (row_type i = 0; i < rowCount(); ++i) {
1515 l_getline(is, line);
1516 if (!prefixIs(line, "<row")) {
1517 lyxerr << "Wrong tabular format (expected <row ...> got"
1518 << line << ')' << endl;
1521 getTokenValue(line, "topline", row_info[i].top_line);
1522 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1523 getTokenValue(line, "topspace", row_info[i].top_space,
1524 row_info[i].top_space_default);
1525 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1526 row_info[i].bottom_space_default);
1527 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1528 row_info[i].interline_space_default);
1529 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1530 getTokenValue(line, "endhead", row_info[i].endhead);
1531 getTokenValue(line, "endfoot", row_info[i].endfoot);
1532 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1533 getTokenValue(line, "newpage", row_info[i].newpage);
1534 for (col_type j = 0; j < columnCount(); ++j) {
1535 l_getline(is, line);
1536 if (!prefixIs(line, "<cell")) {
1537 lyxerr << "Wrong tabular format (expected <cell ...> got"
1538 << line << ')' << endl;
1541 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1542 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1543 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1544 getTokenValue(line, "topline", cell_info[i][j].top_line);
1545 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1546 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1547 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1548 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1549 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1550 getTokenValue(line, "width", cell_info[i][j].p_width);
1551 getTokenValue(line, "special", cell_info[i][j].align_special);
1552 l_getline(is, line);
1553 if (prefixIs(line, "\\begin_inset")) {
1554 cell_info[i][j].inset->read(buf, lex);
1555 l_getline(is, line);
1557 if (!prefixIs(line, "</cell>")) {
1558 lyxerr << "Wrong tabular format (expected </cell> got"
1559 << line << ')' << endl;
1563 l_getline(is, line);
1564 if (!prefixIs(line, "</row>")) {
1565 lyxerr << "Wrong tabular format (expected </row> got"
1566 << line << ')' << endl;
1570 while (!prefixIs(line, "</lyxtabular>")) {
1571 l_getline(is, line);
1577 bool Tabular::isMultiColumn(idx_type cell) const
1579 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1583 bool Tabular::isMultiColumnReal(idx_type cell) const
1585 return cellColumn(cell) != cellRightColumn(cell) &&
1586 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1590 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1592 return cell_info[cellRow(cell)][cellColumn(cell)];
1596 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1599 cellstruct & cs = cellinfo_of_cell(cell);
1600 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1601 cs.alignment = column_info[cellColumn(cell)].alignment;
1602 cs.top_line = row_info[cellRow(cell)].top_line;
1603 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1604 cs.left_line = column_info[cellColumn(cell)].left_line;
1605 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1606 for (idx_type i = 1; i < number; ++i) {
1607 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1608 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1609 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1616 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1618 row_type const row = cellRow(cell);
1619 col_type column = cellColumn(cell);
1620 idx_type result = 1;
1622 while (column < columnCount() &&
1623 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1632 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1634 row_type const row = cellRow(cell);
1635 col_type column = cellColumn(cell);
1637 idx_type result = 0;
1639 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1640 cell_info[row][column].multicolumn = CELL_NORMAL;
1642 while (column < columnCount() &&
1643 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1645 cell_info[row][column].multicolumn = CELL_NORMAL;
1655 void Tabular::setBookTabs(bool what)
1657 use_booktabs = what;
1661 bool Tabular::useBookTabs() const
1663 return use_booktabs;
1667 void Tabular::setLongTabular(bool what)
1669 is_long_tabular = what;
1673 bool Tabular::isLongTabular() const
1675 return is_long_tabular;
1679 void Tabular::setRotateTabular(bool flag)
1685 bool Tabular::getRotateTabular() const
1691 void Tabular::setRotateCell(idx_type cell, bool flag)
1693 cellinfo_of_cell(cell).rotate = flag;
1697 bool Tabular::getRotateCell(idx_type cell) const
1699 return cellinfo_of_cell(cell).rotate;
1703 bool Tabular::needRotating() const
1707 for (row_type i = 0; i < rowCount(); ++i)
1708 for (col_type j = 0; j < columnCount(); ++j)
1709 if (cell_info[i][j].rotate)
1715 bool Tabular::isLastCell(idx_type cell) const
1717 if (cell + 1 < cellCount())
1723 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1725 if (cellRow(cell) > 0)
1726 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1731 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1733 if (cellRow(cell) + 1 < rowCount())
1734 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1739 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1741 if (cellRow(cell) == 0)
1743 if (!isMultiColumn(cell))
1744 return getCellAbove(cell);
1745 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1749 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1751 if (cellRow(cell) + 1 >= rowCount())
1753 if (!isMultiColumn(cell))
1754 return getCellBelow(cell);
1755 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1759 Tabular::idx_type Tabular::cellIndex(row_type row,
1760 col_type column) const
1762 BOOST_ASSERT(column != npos && column < columnCount() &&
1763 row != npos && row < rowCount());
1764 return cell_info[row][column].cellno;
1768 void Tabular::setUsebox(idx_type cell, BoxType type)
1770 cellinfo_of_cell(cell).usebox = type;
1774 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1776 if (column_info[cellColumn(cell)].p_width.zero() &&
1777 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1779 if (cellinfo_of_cell(cell).usebox > 1)
1780 return cellinfo_of_cell(cell).usebox;
1781 return useParbox(cell);
1786 // This are functions used for the longtable support
1788 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1794 row_info[row].endfirsthead = flag;
1798 row_info[row].endhead = flag;
1803 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1806 hd.set = haveLTHead();
1807 return row_info[row].endhead;
1811 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1814 hd.set = haveLTFirstHead();
1815 return row_info[row].endfirsthead;
1819 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1825 row_info[row].endlastfoot = flag;
1829 row_info[row].endfoot = flag;
1834 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1837 fd.set = haveLTFoot();
1838 return row_info[row].endfoot;
1842 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1845 fd.set = haveLTLastFoot();
1846 return row_info[row].endlastfoot;
1850 void Tabular::setLTNewPage(row_type row, bool what)
1852 row_info[row].newpage = what;
1856 bool Tabular::getLTNewPage(row_type row) const
1858 return row_info[row].newpage;
1862 bool Tabular::haveLTHead() const
1864 for (row_type i = 0; i < rowCount(); ++i)
1865 if (row_info[i].endhead)
1871 bool Tabular::haveLTFirstHead() const
1873 if (endfirsthead.empty)
1875 for (row_type i = 0; i < rowCount(); ++i)
1876 if (row_info[i].endfirsthead)
1882 bool Tabular::haveLTFoot() const
1884 for (row_type i = 0; i < rowCount(); ++i)
1885 if (row_info[i].endfoot)
1891 bool Tabular::haveLTLastFoot() const
1893 if (endlastfoot.empty)
1895 for (row_type i = 0; i < rowCount(); ++i)
1896 if (row_info[i].endlastfoot)
1902 // end longtable support functions
1904 void Tabular::setRowAscent(row_type row, int height)
1906 if (row >= rowCount() || row_info[row].ascent == height)
1908 row_info[row].ascent = height;
1912 void Tabular::setRowDescent(row_type row, int height)
1914 if (row >= rowCount() || row_info[row].descent == height)
1916 row_info[row].descent = height;
1920 int Tabular::rowAscent(row_type row) const
1922 if (row >= rowCount())
1924 return row_info[row].ascent;
1928 int Tabular::rowDescent(row_type row) const
1930 BOOST_ASSERT(row < rowCount());
1931 return row_info[row].descent;
1935 int Tabular::height() const
1938 for (row_type row = 0; row < rowCount(); ++row)
1939 height += rowAscent(row) + rowDescent(row) +
1940 getAdditionalHeight(row);
1945 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1947 BOOST_ASSERT(row < rowCount());
1948 BOOST_ASSERT(column < columnCount());
1949 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1953 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1955 // FIXME: assert or return 0 as in TeXBottomHLine()?
1956 BOOST_ASSERT(row != npos);
1957 BOOST_ASSERT(row < rowCount());
1959 idx_type const fcell = getFirstCellInRow(row);
1960 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1963 for (idx_type i = fcell; i < n; ++i) {
1967 if (use_booktabs && row == 0) {
1969 } else if (tmp == n - fcell) {
1970 os << (use_booktabs ? "\\midrule " : "\\hline ");
1972 for (idx_type i = fcell; i < n; ++i) {
1974 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1975 << cellColumn(i) + 1
1977 << cellRightColumn(i) + 1
1989 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1991 // FIXME: return 0 or assert as in TeXTopHLine()?
1992 if (row == npos || row >= rowCount())
1995 idx_type const fcell = getFirstCellInRow(row);
1996 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1999 for (idx_type i = fcell; i < n; ++i) {
2003 if (use_booktabs && row == rowCount() - 1) {
2004 os << "\\bottomrule";
2005 } else if (tmp == n - fcell) {
2006 os << (use_booktabs ? "\\midrule" : "\\hline");
2008 for (idx_type i = fcell; i < n; ++i) {
2009 if (bottomLine(i)) {
2010 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2011 << cellColumn(i) + 1
2013 << cellRightColumn(i) + 1
2025 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2029 if (getRotateCell(cell)) {
2030 os << "\\begin{sideways}\n";
2033 if (isMultiColumn(cell)) {
2034 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2035 if (leftLine(cell) &&
2036 (isFirstCellInRow(cell) ||
2037 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2038 !rightLine(cell - 1, true))))
2040 if (!cellinfo_of_cell(cell).align_special.empty()) {
2041 os << cellinfo_of_cell(cell).align_special;
2043 if (!getPWidth(cell).zero()) {
2044 switch (getVAlignment(cell)) {
2045 case LYX_VALIGN_TOP:
2048 case LYX_VALIGN_MIDDLE:
2051 case LYX_VALIGN_BOTTOM:
2056 << from_ascii(getPWidth(cell).asLatexString())
2059 switch (getAlignment(cell)) {
2060 case LYX_ALIGN_LEFT:
2063 case LYX_ALIGN_RIGHT:
2070 } // end if else !getPWidth
2071 } // end if else !cellinfo_of_cell
2072 if (rightLine(cell))
2074 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2079 if (getUsebox(cell) == BOX_PARBOX) {
2081 switch (getVAlignment(cell)) {
2082 case LYX_VALIGN_TOP:
2085 case LYX_VALIGN_MIDDLE:
2088 case LYX_VALIGN_BOTTOM:
2092 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2094 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2095 os << "\\begin{minipage}[";
2096 switch (getVAlignment(cell)) {
2097 case LYX_VALIGN_TOP:
2100 case LYX_VALIGN_MIDDLE:
2103 case LYX_VALIGN_BOTTOM:
2107 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2115 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2120 if (getUsebox(cell) == BOX_PARBOX)
2122 else if (getUsebox(cell) == BOX_MINIPAGE) {
2123 os << "%\n\\end{minipage}";
2126 if (isMultiColumn(cell)) {
2129 if (getRotateCell(cell)) {
2130 os << "%\n\\end{sideways}";
2137 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2138 OutputParams const & runparams) const
2140 if (!is_long_tabular)
2144 // output header info
2146 if (endhead.topDL) {
2150 for (row_type i = 0; i < rowCount(); ++i) {
2151 if (row_info[i].endhead) {
2152 ret += TeXRow(os, i, buf, runparams);
2155 if (endhead.bottomDL) {
2159 os << "\\endhead\n";
2161 if (endfirsthead.empty) {
2162 os << "\\endfirsthead\n";
2166 // output firstheader info
2167 if (haveLTFirstHead()) {
2168 if (endfirsthead.topDL) {
2172 for (row_type i = 0; i < rowCount(); ++i) {
2173 if (row_info[i].endfirsthead) {
2174 ret += TeXRow(os, i, buf, runparams);
2177 if (endfirsthead.bottomDL) {
2181 os << "\\endfirsthead\n";
2184 // output footer info
2186 if (endfoot.topDL) {
2190 for (row_type i = 0; i < rowCount(); ++i) {
2191 if (row_info[i].endfoot) {
2192 ret += TeXRow(os, i, buf, runparams);
2195 if (endfoot.bottomDL) {
2199 os << "\\endfoot\n";
2201 if (endlastfoot.empty) {
2202 os << "\\endlastfoot\n";
2206 // output lastfooter info
2207 if (haveLTLastFoot()) {
2208 if (endlastfoot.topDL) {
2212 for (row_type i = 0; i < rowCount(); ++i) {
2213 if (row_info[i].endlastfoot) {
2214 ret += TeXRow(os, i, buf, runparams);
2217 if (endlastfoot.bottomDL) {
2221 os << "\\endlastfoot\n";
2228 bool Tabular::isValidRow(row_type row) const
2230 if (!is_long_tabular)
2232 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2233 !row_info[row].endfoot && !row_info[row].endlastfoot;
2237 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2238 OutputParams const & runparams) const
2240 idx_type cell = cellIndex(i, 0);
2241 int ret = TeXTopHLine(os, i);
2242 if (row_info[i].top_space_default) {
2244 os << "\\addlinespace\n";
2246 os << "\\noalign{\\vskip\\doublerulesep}\n";
2248 } else if(!row_info[i].top_space.zero()) {
2250 os << "\\addlinespace["
2251 << from_ascii(row_info[i].top_space.asLatexString())
2254 os << "\\noalign{\\vskip"
2255 << from_ascii(row_info[i].top_space.asLatexString())
2261 for (col_type j = 0; j < columnCount(); ++j) {
2262 if (isPartOfMultiColumn(i, j))
2264 ret += TeXCellPreamble(os, cell);
2265 shared_ptr<InsetText> inset = getCellInset(cell);
2267 Paragraph const & par = inset->paragraphs().front();
2268 bool rtl = par.isRTL(buf.params())
2270 && getPWidth(cell).zero();
2273 if (par.getParLanguage(buf.params())->lang() ==
2276 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2278 // currently, remaning RTL languages are arabic_arabtex and hebrew
2282 ret += inset->latex(buf, os, runparams);
2286 ret += TeXCellPostamble(os, cell);
2287 if (!isLastCellInRow(cell)) { // not last cell in row
2292 os << "\\tabularnewline";
2293 if (row_info[i].bottom_space_default) {
2295 os << "\\addlinespace";
2297 os << "[\\doublerulesep]";
2298 } else if (!row_info[i].bottom_space.zero()) {
2300 os << "\\addlinespace";
2302 << from_ascii(row_info[i].bottom_space.asLatexString())
2307 ret += TeXBottomHLine(os, i);
2308 if (row_info[i].interline_space_default) {
2310 os << "\\addlinespace\n";
2312 os << "\\noalign{\\vskip\\doublerulesep}\n";
2314 } else if (!row_info[i].interline_space.zero()) {
2316 os << "\\addlinespace["
2317 << from_ascii(row_info[i].interline_space.asLatexString())
2320 os << "\\noalign{\\vskip"
2321 << from_ascii(row_info[i].interline_space.asLatexString())
2329 int Tabular::latex(Buffer const & buf, odocstream & os,
2330 OutputParams const & runparams) const
2334 //+---------------------------------------------------------------------
2335 //+ first the opening preamble +
2336 //+---------------------------------------------------------------------
2339 os << "\\begin{sideways}\n";
2342 if (is_long_tabular)
2343 os << "\\begin{longtable}{";
2345 os << "\\begin{tabular}{";
2346 for (col_type i = 0; i < columnCount(); ++i) {
2347 if (!use_booktabs && column_info[i].left_line)
2349 if (!column_info[i].align_special.empty()) {
2350 os << column_info[i].align_special;
2352 if (!column_info[i].p_width.zero()) {
2353 switch (column_info[i].alignment) {
2354 case LYX_ALIGN_LEFT:
2355 os << ">{\\raggedright}";
2357 case LYX_ALIGN_RIGHT:
2358 os << ">{\\raggedleft}";
2360 case LYX_ALIGN_CENTER:
2361 os << ">{\\centering}";
2363 case LYX_ALIGN_NONE:
2364 case LYX_ALIGN_BLOCK:
2365 case LYX_ALIGN_LAYOUT:
2366 case LYX_ALIGN_SPECIAL:
2370 switch (column_info[i].valignment) {
2371 case LYX_VALIGN_TOP:
2374 case LYX_VALIGN_MIDDLE:
2377 case LYX_VALIGN_BOTTOM:
2382 << from_ascii(column_info[i].p_width.asLatexString())
2385 switch (column_info[i].alignment) {
2386 case LYX_ALIGN_LEFT:
2389 case LYX_ALIGN_RIGHT:
2396 } // end if else !column_info[i].p_width
2397 } // end if else !column_info[i].align_special
2398 if (!use_booktabs && column_info[i].right_line)
2404 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2406 //+---------------------------------------------------------------------
2407 //+ the single row and columns (cells) +
2408 //+---------------------------------------------------------------------
2410 for (row_type i = 0; i < rowCount(); ++i) {
2411 if (isValidRow(i)) {
2412 ret += TeXRow(os, i, buf, runparams);
2413 if (is_long_tabular && row_info[i].newpage) {
2414 os << "\\newpage\n";
2420 //+---------------------------------------------------------------------
2421 //+ the closing of the tabular +
2422 //+---------------------------------------------------------------------
2424 if (is_long_tabular)
2425 os << "\\end{longtable}";
2427 os << "\\end{tabular}";
2429 os << "\n\\end{sideways}";
2437 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2438 OutputParams const & runparams) const
2441 idx_type cell = getFirstCellInRow(row);
2444 for (col_type j = 0; j < columnCount(); ++j) {
2445 if (isPartOfMultiColumn(row, j))
2448 os << "<entry align=\"";
2449 switch (getAlignment(cell)) {
2450 case LYX_ALIGN_LEFT:
2453 case LYX_ALIGN_RIGHT:
2461 os << "\" valign=\"";
2462 switch (getVAlignment(cell)) {
2463 case LYX_VALIGN_TOP:
2466 case LYX_VALIGN_BOTTOM:
2469 case LYX_VALIGN_MIDDLE:
2474 if (isMultiColumn(cell)) {
2475 os << " namest=\"col" << j << "\" ";
2476 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2480 ret += getCellInset(cell)->docbook(buf, os, runparams);
2489 int Tabular::docbook(Buffer const & buf, odocstream & os,
2490 OutputParams const & runparams) const
2494 //+---------------------------------------------------------------------
2495 //+ first the opening preamble +
2496 //+---------------------------------------------------------------------
2498 os << "<tgroup cols=\"" << columnCount()
2499 << "\" colsep=\"1\" rowsep=\"1\">\n";
2501 for (col_type i = 0; i < columnCount(); ++i) {
2502 os << "<colspec colname=\"col" << i << "\" align=\"";
2503 switch (column_info[i].alignment) {
2504 case LYX_ALIGN_LEFT:
2507 case LYX_ALIGN_RIGHT:
2515 if (runparams.flavor == OutputParams::XML)
2521 //+---------------------------------------------------------------------
2522 //+ Long Tabular case +
2523 //+---------------------------------------------------------------------
2525 // output header info
2526 if (haveLTHead() || haveLTFirstHead()) {
2529 for (row_type i = 0; i < rowCount(); ++i) {
2530 if (row_info[i].endhead || row_info[i].endfirsthead) {
2531 ret += docbookRow(buf, os, i, runparams);
2537 // output footer info
2538 if (haveLTFoot() || haveLTLastFoot()) {
2541 for (row_type i = 0; i < rowCount(); ++i) {
2542 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2543 ret += docbookRow(buf, os, i, runparams);
2550 //+---------------------------------------------------------------------
2551 //+ the single row and columns (cells) +
2552 //+---------------------------------------------------------------------
2556 for (row_type i = 0; i < rowCount(); ++i) {
2557 if (isValidRow(i)) {
2558 ret += docbookRow(buf, os, i, runparams);
2563 //+---------------------------------------------------------------------
2564 //+ the closing of the tabular +
2565 //+---------------------------------------------------------------------
2574 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2575 vector<unsigned int> const & clen) const
2577 idx_type const fcell = getFirstCellInRow(row);
2578 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2581 for (idx_type i = fcell; i < n; ++i) {
2591 for (idx_type i = fcell; i < n; ++i) {
2602 col_type column = cellColumn(i);
2603 int len = clen[column];
2604 while (column < columnCount() - 1
2605 && isPartOfMultiColumn(row, ++column))
2606 len += clen[column] + 4;
2607 os << docstring(len, ch);
2622 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2623 vector<unsigned int> const & clen) const
2625 idx_type const fcell = getFirstCellInRow(row);
2626 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2629 for (idx_type i = fcell; i < n; ++i) {
2630 if (bottomLine(i)) {
2639 for (idx_type i = fcell; i < n; ++i) {
2640 if (bottomLine(i)) {
2650 col_type column = cellColumn(i);
2651 int len = clen[column];
2652 while (column < columnCount() -1
2653 && isPartOfMultiColumn(row, ++column))
2654 len += clen[column] + 4;
2655 os << docstring(len, ch);
2656 if (bottomLine(i)) {
2670 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2671 OutputParams const & runparams,
2672 idx_type cell, row_type row, col_type column,
2673 vector<unsigned int> const & clen,
2674 bool onlydata) const
2676 odocstringstream sstr;
2677 getCellInset(cell)->plaintext(buf, sstr, runparams);
2689 unsigned int len1 = sstr.str().length();
2690 unsigned int len2 = clen[column];
2691 while (column < columnCount() -1
2692 && isPartOfMultiColumn(row, ++column))
2693 len2 += clen[column] + 4;
2696 switch (getAlignment(cell)) {
2698 case LYX_ALIGN_LEFT:
2701 case LYX_ALIGN_RIGHT:
2705 case LYX_ALIGN_CENTER:
2711 os << docstring(len1, ' ') << sstr.str()
2712 << docstring(len2, ' ');
2714 if (rightLine(cell))
2721 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2722 OutputParams const & runparams, int const depth,
2723 bool onlydata, char_type delim) const
2725 // first calculate the width of the single columns
2726 vector<unsigned int> clen(columnCount());
2729 // first all non (real) multicolumn cells!
2730 for (col_type j = 0; j < columnCount(); ++j) {
2732 for (row_type i = 0; i < rowCount(); ++i) {
2733 idx_type cell = cellIndex(i, j);
2734 if (isMultiColumnReal(cell))
2736 odocstringstream sstr;
2737 getCellInset(cell)->plaintext(buf, sstr, runparams);
2738 if (clen[j] < sstr.str().length())
2739 clen[j] = sstr.str().length();
2742 // then all (real) multicolumn cells!
2743 for (col_type j = 0; j < columnCount(); ++j) {
2744 for (row_type i = 0; i < rowCount(); ++i) {
2745 idx_type cell = cellIndex(i, j);
2746 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2748 odocstringstream sstr;
2749 getCellInset(cell)->plaintext(buf, sstr, runparams);
2750 int len = int(sstr.str().length());
2751 idx_type const n = cells_in_multicolumn(cell);
2752 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2754 if (len > int(clen[j + n - 1]))
2755 clen[j + n - 1] = len;
2760 for (row_type i = 0; i < rowCount(); ++i) {
2761 if (!onlydata && plaintextTopHLine(os, i, clen))
2762 os << docstring(depth * 2, ' ');
2763 for (col_type j = 0; j < columnCount(); ++j) {
2764 if (isPartOfMultiColumn(i, j))
2766 if (onlydata && j > 0)
2767 // we don't use operator<< for single UCS4 character.
2768 // see explanation in docstream.h
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);
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;
3005 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3008 first_visible_cell = Tabular::npos;
3009 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3011 int const a = tabular.rowAscent(i);
3012 int const d = tabular.rowDescent(i);
3013 idx = tabular.cellIndex(i, 0);
3014 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3015 if (tabular.isPartOfMultiColumn(i, j))
3017 if (first_visible_cell == Tabular::npos)
3018 first_visible_cell = idx;
3020 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3021 // Cache the Inset position.
3022 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3023 if (nx + tabular.columnWidth(idx) < 0
3024 || nx > bv->workWidth()
3026 || y - a > bv->workHeight()) {
3027 pi.pain.setDrawingEnabled(false);
3028 cell(idx)->draw(pi, cx, y);
3029 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3030 pi.pain.setDrawingEnabled(original_drawing_state);
3032 cell(idx)->draw(pi, cx, y);
3033 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3035 nx += tabular.columnWidth(idx);
3039 if (i + 1 < tabular.rowCount())
3040 y += d + tabular.rowAscent(i + 1) +
3041 tabular.getAdditionalHeight(i + 1);
3046 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3048 Cursor & cur = pi.base.bv->cursor();
3050 x += scx_ + ADD_TO_TABULAR_WIDTH;
3052 // FIXME: it is wrong to completely paint the background
3053 // if we want to do single row painting.
3055 // Paint background of current tabular
3056 int const w = tabular.width();
3057 int const h = tabular.height();
3058 int yy = y - tabular.rowAscent(0);
3059 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3061 if (!cur.selection())
3063 if (&cur.inset() != this)
3069 if (tablemode(cur)) {
3072 getSelection(cur, rs, re, cs, ce);
3073 y -= tabular.rowAscent(0);
3074 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3075 int const a = tabular.rowAscent(j);
3076 int const h = a + tabular.rowDescent(j);
3078 y += tabular.getAdditionalHeight(j);
3079 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3080 if (tabular.isPartOfMultiColumn(j, i))
3082 idx_type const cell =
3083 tabular.cellIndex(j, i);
3084 int const w = tabular.columnWidth(cell);
3085 if (i >= cs && i <= ce && j >= rs && j <= re)
3086 pi.pain.fillRectangle(xx, y, w, h,
3094 x += getCellXPos(cur.idx());
3095 x += tabular.getBeginningOfTextInCell(cur.idx());
3096 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3101 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3102 row_type row, idx_type cell, bool erased) const
3104 int x2 = x + tabular.columnWidth(cell);
3105 bool on_off = false;
3106 ColorCode col = Color_tabularline;
3107 ColorCode onoffcol = Color_tabularonoffline;
3110 col = Color_deletedtext;
3111 onoffcol = Color_deletedtext;
3114 if (!tabular.topAlreadyDrawn(cell)) {
3115 on_off = !tabular.topLine(cell);
3116 pain.line(x, y - tabular.rowAscent(row),
3117 x2, y - tabular.rowAscent(row),
3118 on_off ? onoffcol : col,
3119 on_off ? Painter::line_onoffdash : Painter::line_solid);
3121 on_off = !tabular.bottomLine(cell);
3122 pain.line(x, y + tabular.rowDescent(row),
3123 x2, y + tabular.rowDescent(row),
3124 on_off ? onoffcol : col,
3125 on_off ? Painter::line_onoffdash : Painter::line_solid);
3126 if (!tabular.leftAlreadyDrawn(cell)) {
3127 on_off = !tabular.leftLine(cell);
3128 pain.line(x, y - tabular.rowAscent(row),
3129 x, y + tabular.rowDescent(row),
3130 on_off ? onoffcol : col,
3131 on_off ? Painter::line_onoffdash : Painter::line_solid);
3133 on_off = !tabular.rightLine(cell);
3134 pain.line(x2 - tabular.getAdditionalWidth(cell),
3135 y - tabular.rowAscent(row),
3136 x2 - tabular.getAdditionalWidth(cell),
3137 y + tabular.rowDescent(row),
3138 on_off ? onoffcol : col,
3139 on_off ? Painter::line_onoffdash : Painter::line_solid);
3143 docstring const InsetTabular::editMessage() const
3145 return _("Opened table");
3149 void InsetTabular::edit(Cursor & cur, bool left)
3151 //lyxerr << "InsetTabular::edit: " << this << endl;
3153 cur.selection() = false;
3156 if (isRightToLeft(cur))
3157 cur.idx() = tabular.getLastCellInRow(0);
3163 if (isRightToLeft(cur))
3164 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3166 cur.idx() = tabular.cellCount() - 1;
3168 cur.pos() = cur.lastpos(); // FIXME crude guess
3170 // FIXME: this accesses the position cache before it is initialized
3172 //cur.bv().fitCursor();
3176 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3178 // In a longtable, tell captions what the current float is
3179 Counters & cnts = buf.params().getTextClass().counters();
3180 string const saveflt = cnts.current_float();
3181 if (tabular.isLongTabular())
3182 cnts.current_float("table");
3184 ParIterator it2 = it;
3186 size_t const end = it2.nargs();
3187 for ( ; it2.idx() < end; it2.top().forwardIdx())
3188 lyx::updateLabels(buf, it2);
3191 if (tabular.isLongTabular())
3192 cnts.current_float(saveflt);
3196 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3198 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3199 << "\n cur:" << cur);
3200 CursorSlice sl = cur.top();
3201 Cursor & bvcur = cur.bv().cursor();
3203 switch (cmd.action) {
3205 case LFUN_MOUSE_PRESS:
3206 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3208 // do not reset cursor/selection if we have selected
3209 // some cells (bug 2715).
3210 if (cmd.button() == mouse_button::button3
3211 && &bvcur.selBegin().inset() == this
3212 && tablemode(bvcur))
3215 // Let InsetText do it
3216 cell(cur.idx())->dispatch(cur, cmd);
3218 case LFUN_MOUSE_MOTION:
3219 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3220 if (cmd.button() == mouse_button::button1) {
3221 // only accept motions to places not deeper nested than the real anchor
3222 if (bvcur.anchor_.hasPart(cur)) {
3223 // only update if selection changes
3224 if (bvcur.idx() == cur.idx() &&
3225 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3227 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3228 bvcur.setCursor(cur);
3229 bvcur.selection() = true;
3235 case LFUN_MOUSE_RELEASE:
3236 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3237 if (cmd.button() == mouse_button::button3)
3238 InsetTabularMailer(*this).showDialog(&cur.bv());
3241 case LFUN_CELL_BACKWARD:
3243 cur.selection() = false;
3246 case LFUN_CELL_FORWARD:
3248 cur.selection() = false;
3251 case LFUN_CHAR_FORWARD_SELECT:
3252 case LFUN_CHAR_FORWARD:
3253 cell(cur.idx())->dispatch(cur, cmd);
3254 if (!cur.result().dispatched()) {
3256 if (sl == cur.top())
3257 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3263 case LFUN_CHAR_BACKWARD_SELECT:
3264 case LFUN_CHAR_BACKWARD:
3265 cell(cur.idx())->dispatch(cur, cmd);
3266 if (!cur.result().dispatched()) {
3268 if (sl == cur.top())
3269 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3275 case LFUN_CHAR_RIGHT_SELECT:
3276 case LFUN_CHAR_RIGHT:
3277 //FIXME: for visual cursor, really move right
3278 if (isRightToLeft(cur))
3279 lyx::dispatch(FuncRequest(
3280 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3281 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3283 lyx::dispatch(FuncRequest(
3284 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3285 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3288 case LFUN_CHAR_LEFT_SELECT:
3289 case LFUN_CHAR_LEFT:
3290 //FIXME: for visual cursor, really move left
3291 if (isRightToLeft(cur))
3292 lyx::dispatch(FuncRequest(
3293 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3294 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3296 lyx::dispatch(FuncRequest(
3297 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3298 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3301 case LFUN_DOWN_SELECT:
3303 cell(cur.idx())->dispatch(cur, cmd);
3304 cur.dispatched(); // override the cell's decision
3305 if (sl == cur.top())
3306 // if our Text didn't do anything to the cursor
3307 // then we try to put the cursor into the cell below
3308 // setting also the right targetX.
3309 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3310 cur.idx() = tabular.getCellBelow(cur.idx());
3312 TextMetrics const & tm =
3313 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3314 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3316 if (sl == cur.top()) {
3317 // we trick it to go to forward after leaving the
3319 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3324 case LFUN_UP_SELECT:
3326 cell(cur.idx())->dispatch(cur, cmd);
3327 cur.dispatched(); // override the cell's decision
3328 if (sl == cur.top())
3329 // if our Text didn't do anything to the cursor
3330 // then we try to put the cursor into the cell above
3331 // setting also the right targetX.
3332 if (tabular.cellRow(cur.idx()) != 0) {
3333 cur.idx() = tabular.getCellAbove(cur.idx());
3334 cur.pit() = cur.lastpit();
3335 Text const * text = cell(cur.idx())->getText(0);
3336 TextMetrics const & tm = cur.bv().textMetrics(text);
3337 ParagraphMetrics const & pm =
3338 tm.parMetrics(cur.lastpit());
3339 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3341 if (sl == cur.top()) {
3342 cmd = FuncRequest(LFUN_UP);
3347 // case LFUN_SCREEN_DOWN: {
3348 // //if (hasSelection())
3349 // // cur.selection() = false;
3350 // col_type const col = tabular.cellColumn(cur.idx());
3351 // int const t = cur.bv().top_y() + cur.bv().height();
3352 // if (t < yo() + tabular.getHeightOfTabular()) {
3353 // cur.bv().scrollDocView(t);
3354 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3356 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3363 // case LFUN_SCREEN_UP: {
3364 // //if (hasSelection())
3365 // // cur.selection() = false;
3366 // col_type const col = tabular.cellColumn(cur.idx());
3367 // int const t = cur.bv().top_y() + cur.bv().height();
3369 // cur.bv().scrollDocView(t);
3373 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3377 // cur.par() = cur.lastpar();
3378 // cur.pos() = cur.lastpos();
3382 case LFUN_LAYOUT_TABULAR:
3383 InsetTabularMailer(*this).showDialog(&cur.bv());
3386 case LFUN_INSET_DIALOG_UPDATE:
3387 InsetTabularMailer(*this).updateDialog(&cur.bv());
3390 case LFUN_TABULAR_FEATURE:
3391 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3395 // insert file functions
3396 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3397 case LFUN_FILE_INSERT_PLAINTEXT: {
3399 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3400 FileName(to_utf8(cmd.argument())));
3403 cur.recordUndoInset(INSERT_UNDO);
3404 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3405 // content has been replaced,
3406 // so cursor might be invalid
3407 cur.pos() = cur.lastpos();
3408 cur.pit() = cur.lastpit();
3409 bvcur.setCursor(cur);
3416 if (tablemode(cur)) {
3417 if (copySelection(cur)) {
3418 cur.recordUndoInset(DELETE_UNDO);
3423 cell(cur.idx())->dispatch(cur, cmd);
3426 case LFUN_CHAR_DELETE_BACKWARD:
3427 case LFUN_CHAR_DELETE_FORWARD:
3428 if (tablemode(cur)) {
3429 cur.recordUndoInset(DELETE_UNDO);
3433 cell(cur.idx())->dispatch(cur, cmd);
3437 if (!cur.selection())
3439 if (tablemode(cur)) {
3443 cell(cur.idx())->dispatch(cur, cmd);
3446 case LFUN_CLIPBOARD_PASTE:
3447 case LFUN_PRIMARY_SELECTION_PASTE: {
3448 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3449 theClipboard().getAsText() :
3450 theSelection().get();
3453 // pass to InsertPlaintextString, but
3454 // only if we have multi-cell content
3455 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3456 cur.recordUndoInset(INSERT_UNDO);
3457 if (insertPlaintextString(cur.bv(), clip, false)) {
3458 // content has been replaced,
3459 // so cursor might be invalid
3460 cur.pos() = cur.lastpos();
3461 cur.pit() = cur.lastpit();
3462 bvcur.setCursor(cur);
3466 // Let the cell handle normal text
3467 cell(cur.idx())->dispatch(cur, cmd);
3472 if (tabularStackDirty() && theClipboard().isInternal()) {
3473 cur.recordUndoInset(INSERT_UNDO);
3474 pasteClipboard(cur);
3477 cell(cur.idx())->dispatch(cur, cmd);
3480 case LFUN_FONT_EMPH:
3481 case LFUN_FONT_BOLD:
3482 case LFUN_FONT_ROMAN:
3483 case LFUN_FONT_NOUN:
3484 case LFUN_FONT_ITAL:
3485 case LFUN_FONT_FRAK:
3486 case LFUN_FONT_TYPEWRITER:
3487 case LFUN_FONT_SANS:
3488 case LFUN_FONT_FREE_APPLY:
3489 case LFUN_FONT_FREE_UPDATE:
3490 case LFUN_FONT_SIZE:
3491 case LFUN_FONT_UNDERLINE:
3493 case LFUN_WORD_CAPITALIZE:
3494 case LFUN_WORD_UPCASE:
3495 case LFUN_WORD_LOWCASE:
3496 case LFUN_CHARS_TRANSPOSE:
3497 if (tablemode(cur)) {
3500 getSelection(cur, rs, re, cs, ce);
3501 Cursor tmpcur = cur;
3502 for (row_type i = rs; i <= re; ++i) {
3503 for (col_type j = cs; j <= ce; ++j) {
3504 // cursor follows cell:
3505 tmpcur.idx() = tabular.cellIndex(i, j);
3506 // select this cell only:
3509 tmpcur.resetAnchor();
3510 tmpcur.pit() = tmpcur.lastpit();
3511 tmpcur.pos() = tmpcur.top().lastpos();
3512 tmpcur.setCursor(tmpcur);
3513 tmpcur.setSelection();
3514 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3519 cell(cur.idx())->dispatch(cur, cmd);
3523 // we try to handle this event in the insets dispatch function.
3524 cell(cur.idx())->dispatch(cur, cmd);
3530 // function sets an object as defined in func_status.h:
3531 // states OK, Unknown, Disabled, On, Off.
3532 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3533 FuncStatus & status) const
3535 switch (cmd.action) {
3536 case LFUN_TABULAR_FEATURE: {
3537 int action = Tabular::LAST_ACTION;
3539 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3540 string const tmp = tabularFeature[i].feature;
3541 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3542 action = tabularFeature[i].action;
3546 if (action == Tabular::LAST_ACTION) {
3548 status.unknown(true);
3552 string const argument
3553 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3555 row_type sel_row_start = 0;
3556 row_type sel_row_end = 0;
3557 col_type sel_col_start = 0;
3558 col_type sel_col_end = 0;
3559 Tabular::ltType dummyltt;
3562 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3565 case Tabular::SET_PWIDTH:
3566 case Tabular::SET_MPWIDTH:
3567 case Tabular::SET_SPECIAL_COLUMN:
3568 case Tabular::SET_SPECIAL_MULTI:
3569 case Tabular::APPEND_ROW:
3570 case Tabular::APPEND_COLUMN:
3571 case Tabular::DELETE_ROW:
3572 case Tabular::DELETE_COLUMN:
3573 case Tabular::COPY_ROW:
3574 case Tabular::COPY_COLUMN:
3575 case Tabular::SET_ALL_LINES:
3576 case Tabular::UNSET_ALL_LINES:
3577 case Tabular::SET_TOP_SPACE:
3578 case Tabular::SET_BOTTOM_SPACE:
3579 case Tabular::SET_INTERLINE_SPACE:
3583 case Tabular::MULTICOLUMN:
3584 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3587 case Tabular::M_TOGGLE_LINE_TOP:
3589 case Tabular::TOGGLE_LINE_TOP:
3590 status.setOnOff(tabular.topLine(cur.idx(), flag));
3593 case Tabular::M_TOGGLE_LINE_BOTTOM:
3595 case Tabular::TOGGLE_LINE_BOTTOM:
3596 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3599 case Tabular::M_TOGGLE_LINE_LEFT:
3601 case Tabular::TOGGLE_LINE_LEFT:
3602 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3605 case Tabular::M_TOGGLE_LINE_RIGHT:
3607 case Tabular::TOGGLE_LINE_RIGHT:
3608 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3611 case Tabular::M_ALIGN_LEFT:
3613 case Tabular::ALIGN_LEFT:
3614 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3617 case Tabular::M_ALIGN_RIGHT:
3619 case Tabular::ALIGN_RIGHT:
3620 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3623 case Tabular::M_ALIGN_CENTER:
3625 case Tabular::ALIGN_CENTER:
3626 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3629 case Tabular::ALIGN_BLOCK:
3630 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3631 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3634 case Tabular::M_VALIGN_TOP:
3636 case Tabular::VALIGN_TOP:
3638 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3641 case Tabular::M_VALIGN_BOTTOM:
3643 case Tabular::VALIGN_BOTTOM:
3645 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3648 case Tabular::M_VALIGN_MIDDLE:
3650 case Tabular::VALIGN_MIDDLE:
3652 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3655 case Tabular::SET_LONGTABULAR:
3656 status.setOnOff(tabular.isLongTabular());
3659 case Tabular::UNSET_LONGTABULAR:
3660 status.setOnOff(!tabular.isLongTabular());
3663 case Tabular::TOGGLE_ROTATE_TABULAR:
3664 case Tabular::SET_ROTATE_TABULAR:
3665 status.setOnOff(tabular.getRotateTabular());
3668 case Tabular::UNSET_ROTATE_TABULAR:
3669 status.setOnOff(!tabular.getRotateTabular());
3672 case Tabular::TOGGLE_ROTATE_CELL:
3673 case Tabular::SET_ROTATE_CELL:
3674 status.setOnOff(!oneCellHasRotationState(false,
3675 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3678 case Tabular::UNSET_ROTATE_CELL:
3679 status.setOnOff(!oneCellHasRotationState(true,
3680 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3683 case Tabular::SET_USEBOX:
3684 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3687 case Tabular::SET_LTFIRSTHEAD:
3688 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3691 case Tabular::UNSET_LTFIRSTHEAD:
3692 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3695 case Tabular::SET_LTHEAD:
3696 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3699 case Tabular::UNSET_LTHEAD:
3700 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3703 case Tabular::SET_LTFOOT:
3704 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3707 case Tabular::UNSET_LTFOOT:
3708 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3711 case Tabular::SET_LTLASTFOOT:
3712 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3715 case Tabular::UNSET_LTLASTFOOT:
3716 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3719 case Tabular::SET_LTNEWPAGE:
3720 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3723 case Tabular::SET_BOOKTABS:
3724 status.setOnOff(tabular.useBookTabs());
3727 case Tabular::UNSET_BOOKTABS:
3728 status.setOnOff(!tabular.useBookTabs());
3733 status.enabled(false);
3739 // These are only enabled inside tabular
3740 case LFUN_CELL_BACKWARD:
3741 case LFUN_CELL_FORWARD:
3742 status.enabled(true);
3745 // disable these with multiple cells selected
3746 case LFUN_INSET_INSERT:
3747 case LFUN_TABULAR_INSERT:
3748 case LFUN_FLEX_INSERT:
3749 case LFUN_FLOAT_INSERT:
3750 case LFUN_FLOAT_WIDE_INSERT:
3751 case LFUN_FOOTNOTE_INSERT:
3752 case LFUN_MARGINALNOTE_INSERT:
3753 case LFUN_MATH_INSERT:
3754 case LFUN_MATH_MODE:
3755 case LFUN_MATH_MUTATE:
3756 case LFUN_MATH_DISPLAY:
3757 case LFUN_NOTE_INSERT:
3758 case LFUN_OPTIONAL_INSERT:
3759 case LFUN_BOX_INSERT:
3760 case LFUN_BRANCH_INSERT:
3761 case LFUN_WRAP_INSERT:
3762 case LFUN_ERT_INSERT: {
3763 if (tablemode(cur)) {
3764 status.enabled(false);
3767 return cell(cur.idx())->getStatus(cur, cmd, status);
3770 // disable in non-fixed-width cells
3772 case LFUN_BREAK_PARAGRAPH:
3773 case LFUN_BREAK_PARAGRAPH_SKIP: {
3774 if (tabular.getPWidth(cur.idx()).zero()) {
3775 status.enabled(false);
3778 return cell(cur.idx())->getStatus(cur, cmd, status);
3782 if (tabularStackDirty() && theClipboard().isInternal()) {
3783 status.enabled(true);
3786 return cell(cur.idx())->getStatus(cur, cmd, status);
3788 case LFUN_INSET_MODIFY:
3789 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3790 status.enabled(true);
3796 // we try to handle this event in the insets dispatch function.
3797 return cell(cur.idx())->getStatus(cur, cmd, status);
3802 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3803 OutputParams const & runparams) const
3805 return tabular.latex(buf, os, runparams);
3809 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3810 OutputParams const & runparams) const
3812 os << '\n'; // output table on a new line
3813 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3814 tabular.plaintext(buf, os, runparams, dp, false, 0);
3815 return PLAINTEXT_NEWLINE;
3819 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3820 OutputParams const & runparams) const
3825 // FIXME: Why not pass a proper DocIterator here?
3827 // if the table is inside a float it doesn't need the informaltable
3828 // wrapper. Search for it.
3829 for (master = owner(); master; master = master->owner())
3830 if (master->lyxCode() == FLOAT_CODE)
3835 os << "<informaltable>";
3838 ret += tabular.docbook(buf, os, runparams);
3840 os << "</informaltable>";
3847 void InsetTabular::validate(LaTeXFeatures & features) const
3849 tabular.validate(features);
3853 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3855 return tabular.getCellInset(idx);
3859 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3861 return tabular.getCellInset(idx);
3865 void InsetTabular::cursorPos(BufferView const & bv,
3866 CursorSlice const & sl, bool boundary, int & x, int & y) const
3868 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3870 // y offset correction
3871 int const row = tabular.cellRow(sl.idx());
3872 for (int i = 0; i <= row; ++i) {
3874 y += tabular.rowAscent(i);
3875 y += tabular.getAdditionalHeight(i);
3878 y += tabular.rowDescent(i);
3881 // x offset correction
3882 int const col = tabular.cellColumn(sl.idx());
3883 int idx = tabular.cellIndex(row, 0);
3884 for (int j = 0; j < col; ++j) {
3885 if (tabular.isPartOfMultiColumn(row, j))
3887 x += tabular.columnWidth(idx);
3890 x += tabular.getBeginningOfTextInCell(idx);
3891 x += ADD_TO_TABULAR_WIDTH;
3896 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3900 Inset const & inset = *tabular.getCellInset(cell);
3901 Point o = bv.coordCache().getInsets().xy(&inset);
3902 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3903 int const xend = xbeg + tabular.columnWidth(cell);
3904 row_type const row = tabular.cellRow(cell);
3905 int const ybeg = o.y_ - tabular.rowAscent(row) -
3906 tabular.getAdditionalHeight(row);
3907 int const yend = o.y_ + tabular.rowDescent(row);
3919 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3920 // << " ybeg=" << ybeg << " yend=" << yend
3921 // << " xx=" << xx << " yy=" << yy
3922 // << " dist=" << xx + yy << endl;
3927 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3929 //lyxerr << "InsetTabular::editXY: " << this << endl;
3930 cur.selection() = false;
3932 cur.idx() = getNearestCell(cur.bv(), x, y);
3934 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3938 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3940 cur.idx() = getNearestCell(cur.bv(), x, y);
3941 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3945 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3947 idx_type idx_min = 0;
3948 int dist_min = std::numeric_limits<int>::max();
3949 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3950 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3951 int const d = dist(bv, i, x, y);
3962 int InsetTabular::getCellXPos(idx_type const cell) const
3966 for (; !tabular.isFirstCellInRow(c); --c)
3969 for (; c < cell; ++c)
3970 lx += tabular.columnWidth(c);
3976 void InsetTabular::resetPos(Cursor & cur) const
3978 BufferView & bv = cur.bv();
3979 int const maxwidth = bv.workWidth();
3981 if (&cur.inset() != this) {
3985 int const X2 = maxwidth;
3986 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3987 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3988 int const x2 = x1 + tabular.columnWidth(cur.idx());
3991 scx_ = X1 + 20 - x1;
3993 scx_ = X2 - 20 - x2;
3998 cur.updateFlags(Update::Force | Update::FitCursor);
4002 void InsetTabular::moveNextCell(Cursor & cur)
4004 if (isRightToLeft(cur)) {
4005 if (tabular.isFirstCellInRow(cur.idx())) {
4006 row_type const row = tabular.cellRow(cur.idx());
4007 if (row == tabular.rowCount() - 1)
4009 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4016 if (tabular.isLastCell(cur.idx()))
4026 void InsetTabular::movePrevCell(Cursor & cur)
4028 if (isRightToLeft(cur)) {
4029 if (tabular.isLastCellInRow(cur.idx())) {
4030 row_type const row = tabular.cellRow(cur.idx());
4033 cur.idx() = tabular.getFirstCellInRow(row);
4034 cur.idx() = tabular.getCellAbove(cur.idx());
4036 if (tabular.isLastCell(cur.idx()))
4041 if (cur.idx() == 0) // first cell
4045 cur.pit() = cur.lastpit();
4046 cur.pos() = cur.lastpos();
4048 // FIXME: this accesses the position cache before it is initialized
4053 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4055 Tabular::Feature action = Tabular::LAST_ACTION;
4058 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4059 string const tmp = tabularFeature[i].feature;
4061 if (tmp == what.substr(0, tmp.length())) {
4062 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4063 //tabularFeatures[i].feature.length()))
4064 action = tabularFeature[i].action;
4068 if (action == Tabular::LAST_ACTION)
4072 ltrim(what.substr(tabularFeature[i].feature.length()));
4073 tabularFeatures(cur, action, val);
4078 static void checkLongtableSpecial(Tabular::ltType & ltt,
4079 string const & special, bool & flag)
4081 if (special == "dl_above") {
4084 } else if (special == "dl_below") {
4085 ltt.bottomDL = flag;
4087 } else if (special == "empty") {
4096 bool InsetTabular::oneCellHasRotationState(bool rotated,
4097 row_type row_start, row_type row_end,
4098 col_type col_start, col_type col_end) const {
4100 for (row_type i = row_start; i <= row_end; ++i) {
4101 for (col_type j = col_start; j <= col_end; ++j) {
4102 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4111 void InsetTabular::tabularFeatures(Cursor & cur,
4112 Tabular::Feature feature, string const & value)
4114 BufferView & bv = cur.bv();
4115 col_type sel_col_start;
4116 col_type sel_col_end;
4117 row_type sel_row_start;
4118 row_type sel_row_end;
4119 bool setLines = false;
4120 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4121 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4125 case Tabular::M_ALIGN_LEFT:
4126 case Tabular::ALIGN_LEFT:
4127 setAlign = LYX_ALIGN_LEFT;
4130 case Tabular::M_ALIGN_RIGHT:
4131 case Tabular::ALIGN_RIGHT:
4132 setAlign = LYX_ALIGN_RIGHT;
4135 case Tabular::M_ALIGN_CENTER:
4136 case Tabular::ALIGN_CENTER:
4137 setAlign = LYX_ALIGN_CENTER;
4140 case Tabular::ALIGN_BLOCK:
4141 setAlign = LYX_ALIGN_BLOCK;
4144 case Tabular::M_VALIGN_TOP:
4145 case Tabular::VALIGN_TOP:
4146 setVAlign = Tabular::LYX_VALIGN_TOP;
4149 case Tabular::M_VALIGN_BOTTOM:
4150 case Tabular::VALIGN_BOTTOM:
4151 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4154 case Tabular::M_VALIGN_MIDDLE:
4155 case Tabular::VALIGN_MIDDLE:
4156 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4163 cur.recordUndoInset(ATOMIC_UNDO);
4165 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4166 row_type const row = tabular.cellRow(cur.idx());
4167 col_type const column = tabular.cellColumn(cur.idx());
4169 Tabular::ltType ltt;
4173 case Tabular::SET_PWIDTH: {
4174 Length const len(value);
4175 tabular.setColumnPWidth(cur, cur.idx(), len);
4177 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4178 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4182 case Tabular::SET_MPWIDTH:
4183 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4186 case Tabular::SET_SPECIAL_COLUMN:
4187 case Tabular::SET_SPECIAL_MULTI:
4188 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4191 case Tabular::APPEND_ROW:
4192 // append the row into the tabular
4193 tabular.appendRow(bv.buffer().params(), cur.idx());
4196 case Tabular::APPEND_COLUMN:
4197 // append the column into the tabular
4198 tabular.appendColumn(bv.buffer().params(), cur.idx());
4199 cur.idx() = tabular.cellIndex(row, column);
4202 case Tabular::DELETE_ROW:
4203 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4204 tabular.deleteRow(sel_row_start);
4205 if (sel_row_start >= tabular.rowCount())
4207 cur.idx() = tabular.cellIndex(sel_row_start, column);
4210 cur.selection() = false;
4213 case Tabular::DELETE_COLUMN:
4214 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4215 tabular.deleteColumn(sel_col_start);
4216 if (sel_col_start >= tabular.columnCount())
4218 cur.idx() = tabular.cellIndex(row, sel_col_start);
4221 cur.selection() = false;
4224 case Tabular::COPY_ROW:
4225 tabular.copyRow(bv.buffer().params(), row);
4228 case Tabular::COPY_COLUMN:
4229 tabular.copyColumn(bv.buffer().params(), column);
4230 cur.idx() = tabular.cellIndex(row, column);
4233 case Tabular::M_TOGGLE_LINE_TOP:
4235 case Tabular::TOGGLE_LINE_TOP: {
4236 bool lineSet = !tabular.topLine(cur.idx(), flag);
4237 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4238 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4240 tabular.cellIndex(i, j),
4245 case Tabular::M_TOGGLE_LINE_BOTTOM:
4247 case Tabular::TOGGLE_LINE_BOTTOM: {
4248 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4249 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4250 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4251 tabular.setBottomLine(
4252 tabular.cellIndex(i, j),
4258 case Tabular::M_TOGGLE_LINE_LEFT:
4260 case Tabular::TOGGLE_LINE_LEFT: {
4261 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4262 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4263 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4264 tabular.setLeftLine(
4265 tabular.cellIndex(i,j),
4271 case Tabular::M_TOGGLE_LINE_RIGHT:
4273 case Tabular::TOGGLE_LINE_RIGHT: {
4274 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4275 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4276 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4277 tabular.setRightLine(
4278 tabular.cellIndex(i,j),
4284 case Tabular::M_ALIGN_LEFT:
4285 case Tabular::M_ALIGN_RIGHT:
4286 case Tabular::M_ALIGN_CENTER:
4288 case Tabular::ALIGN_LEFT:
4289 case Tabular::ALIGN_RIGHT:
4290 case Tabular::ALIGN_CENTER:
4291 case Tabular::ALIGN_BLOCK:
4292 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4293 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4294 tabular.setAlignment(
4295 tabular.cellIndex(i, j),
4300 case Tabular::M_VALIGN_TOP:
4301 case Tabular::M_VALIGN_BOTTOM:
4302 case Tabular::M_VALIGN_MIDDLE:
4304 case Tabular::VALIGN_TOP:
4305 case Tabular::VALIGN_BOTTOM:
4306 case Tabular::VALIGN_MIDDLE:
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.setVAlignment(
4310 tabular.cellIndex(i, j),
4314 case Tabular::MULTICOLUMN: {
4315 if (sel_row_start != sel_row_end) {
4316 // FIXME: Need I say it ? This is horrible.
4318 Alert::error(_("Error setting multicolumn"),
4319 _("You cannot set multicolumn vertically."));
4322 if (!cur.selection()) {
4323 // just multicol for one single cell
4324 // check whether we are completely in a multicol
4325 if (tabular.isMultiColumn(cur.idx()))
4326 tabular.unsetMultiColumn(cur.idx());
4328 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4331 // we have a selection so this means we just add all this
4332 // cells to form a multicolumn cell
4333 idx_type const s_start = cur.selBegin().idx();
4334 idx_type const s_end = cur.selEnd().idx();
4335 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4336 cur.idx() = s_start;
4339 cur.selection() = false;
4343 case Tabular::SET_ALL_LINES:
4345 case Tabular::UNSET_ALL_LINES:
4346 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4347 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4348 tabular.setAllLines(
4349 tabular.cellIndex(i,j), setLines);
4352 case Tabular::SET_LONGTABULAR:
4353 tabular.setLongTabular(true);
4356 case Tabular::UNSET_LONGTABULAR:
4357 tabular.setLongTabular(false);
4360 case Tabular::SET_ROTATE_TABULAR:
4361 tabular.setRotateTabular(true);
4364 case Tabular::UNSET_ROTATE_TABULAR:
4365 tabular.setRotateTabular(false);
4368 case Tabular::TOGGLE_ROTATE_TABULAR:
4369 tabular.setRotateTabular(!tabular.getRotateTabular());
4372 case Tabular::SET_ROTATE_CELL:
4373 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4374 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4375 tabular.setRotateCell(
4376 tabular.cellIndex(i, j), true);
4379 case Tabular::UNSET_ROTATE_CELL:
4380 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4381 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4382 tabular.setRotateCell(
4383 tabular.cellIndex(i, j), false);
4386 case Tabular::TOGGLE_ROTATE_CELL:
4388 bool oneNotRotated = oneCellHasRotationState(false,
4389 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4391 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4392 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4393 tabular.setRotateCell(tabular.cellIndex(i, j),
4398 case Tabular::SET_USEBOX: {
4399 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4400 if (val == tabular.getUsebox(cur.idx()))
4401 val = Tabular::BOX_NONE;
4402 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4403 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4404 tabular.setUsebox(tabular.cellIndex(i, j), val);
4408 case Tabular::UNSET_LTFIRSTHEAD:
4410 case Tabular::SET_LTFIRSTHEAD:
4411 tabular.getRowOfLTFirstHead(row, ltt);
4412 checkLongtableSpecial(ltt, value, flag);
4413 tabular.setLTHead(row, flag, ltt, true);
4416 case Tabular::UNSET_LTHEAD:
4418 case Tabular::SET_LTHEAD:
4419 tabular.getRowOfLTHead(row, ltt);
4420 checkLongtableSpecial(ltt, value, flag);
4421 tabular.setLTHead(row, flag, ltt, false);
4424 case Tabular::UNSET_LTFOOT:
4426 case Tabular::SET_LTFOOT:
4427 tabular.getRowOfLTFoot(row, ltt);
4428 checkLongtableSpecial(ltt, value, flag);
4429 tabular.setLTFoot(row, flag, ltt, false);
4432 case Tabular::UNSET_LTLASTFOOT:
4434 case Tabular::SET_LTLASTFOOT:
4435 tabular.getRowOfLTLastFoot(row, ltt);
4436 checkLongtableSpecial(ltt, value, flag);
4437 tabular.setLTFoot(row, flag, ltt, true);
4440 case Tabular::SET_LTNEWPAGE:
4441 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4444 case Tabular::SET_BOOKTABS:
4445 tabular.setBookTabs(true);
4448 case Tabular::UNSET_BOOKTABS:
4449 tabular.setBookTabs(false);
4452 case Tabular::SET_TOP_SPACE: {
4454 if (value == "default")
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4456 tabular.row_info[i].top_space_default = true;
4457 else if (isValidLength(value, &len))
4458 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4459 tabular.row_info[i].top_space_default = false;
4460 tabular.row_info[i].top_space = len;
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4464 tabular.row_info[i].top_space_default = false;
4465 tabular.row_info[i].top_space = len;
4470 case Tabular::SET_BOTTOM_SPACE: {
4472 if (value == "default")
4473 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4474 tabular.row_info[i].bottom_space_default = true;
4475 else if (isValidLength(value, &len))
4476 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4477 tabular.row_info[i].bottom_space_default = false;
4478 tabular.row_info[i].bottom_space = len;
4481 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4482 tabular.row_info[i].bottom_space_default = false;
4483 tabular.row_info[i].bottom_space = len;
4488 case Tabular::SET_INTERLINE_SPACE: {
4490 if (value == "default")
4491 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4492 tabular.row_info[i].interline_space_default = true;
4493 else if (isValidLength(value, &len))
4494 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4495 tabular.row_info[i].interline_space_default = false;
4496 tabular.row_info[i].interline_space = len;
4499 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4500 tabular.row_info[i].interline_space_default = false;
4501 tabular.row_info[i].interline_space = len;
4506 // dummy stuff just to avoid warnings
4507 case Tabular::LAST_ACTION:
4513 bool InsetTabular::showInsetDialog(BufferView * bv) const
4515 InsetTabularMailer(*this).showDialog(bv);
4520 void InsetTabular::openLayoutDialog(BufferView * bv) const
4522 InsetTabularMailer(*this).showDialog(bv);
4526 bool InsetTabular::copySelection(Cursor & cur)
4528 if (!cur.selection())
4533 getSelection(cur, rs, re, cs, ce);
4535 paste_tabular.reset(new Tabular(tabular));
4537 for (row_type i = 0; i < rs; ++i)
4538 paste_tabular->deleteRow(0);
4540 row_type const rows = re - rs + 1;
4541 while (paste_tabular->rowCount() > rows)
4542 paste_tabular->deleteRow(rows);
4544 paste_tabular->setTopLine(0, true, true);
4545 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4548 for (col_type i = 0; i < cs; ++i)
4549 paste_tabular->deleteColumn(0);
4551 col_type const columns = ce - cs + 1;
4552 while (paste_tabular->columnCount() > columns)
4553 paste_tabular->deleteColumn(columns);
4555 paste_tabular->setLeftLine(0, true, true);
4556 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4559 odocstringstream os;
4560 OutputParams const runparams(0);
4561 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4562 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4563 cap::copySelection(cur, os.str());
4565 // mark tabular stack dirty
4566 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4567 // when we (hopefully) have a one-for-all paste mechanism.
4568 // This must be called after cap::copySelection.
4569 dirtyTabularStack(true);
4575 bool InsetTabular::pasteClipboard(Cursor & cur)
4579 col_type const actcol = tabular.cellColumn(cur.idx());
4580 row_type const actrow = tabular.cellRow(cur.idx());
4581 for (row_type r1 = 0, r2 = actrow;
4582 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4584 for (col_type c1 = 0, c2 = actcol;
4585 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4587 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4588 tabular.isPartOfMultiColumn(r2, c2))
4590 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4594 if (tabular.isPartOfMultiColumn(r2, c2)) {
4598 shared_ptr<InsetText> inset(
4599 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4600 tabular.setCellInset(r2, c2, inset);
4601 // FIXME: change tracking (MG)
4602 inset->setChange(Change(cur.buffer().params().trackChanges ?
4603 Change::INSERTED : Change::UNCHANGED));
4611 void InsetTabular::cutSelection(Cursor & cur)
4613 if (!cur.selection())
4618 getSelection(cur, rs, re, cs, ce);
4619 for (row_type i = rs; i <= re; ++i) {
4620 for (col_type j = cs; j <= ce; ++j) {
4621 shared_ptr<InsetText> t
4622 = cell(tabular.cellIndex(i, j));
4623 if (cur.buffer().params().trackChanges)
4624 // FIXME: Change tracking (MG)
4625 t->setChange(Change(Change::DELETED));
4631 // cursor position might be invalid now
4632 if (cur.pit() > cur.lastpit())
4633 cur.pit() = cur.lastpit();
4634 if (cur.pos() > cur.lastpos())
4635 cur.pos() = cur.lastpos();
4636 cur.clearSelection();
4640 bool InsetTabular::isRightToLeft(Cursor & cur) const
4642 BOOST_ASSERT(cur.depth() > 1);
4643 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4644 pos_type const parentpos = cur[cur.depth() - 2].pos();
4645 return parentpar.getFontSettings(cur.bv().buffer().params(),
4646 parentpos).language()->rightToLeft();
4650 void InsetTabular::getSelection(Cursor & cur,
4651 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4653 CursorSlice const & beg = cur.selBegin();
4654 CursorSlice const & end = cur.selEnd();
4655 cs = tabular.cellColumn(beg.idx());
4656 ce = tabular.cellColumn(end.idx());
4659 cs = tabular.cellColumn(end.idx());
4661 ce = tabular.cellRightColumn(end.idx());
4664 rs = tabular.cellRow(beg.idx());
4665 re = tabular.cellRow(end.idx());
4671 Text * InsetTabular::getText(int idx) const
4673 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4677 void InsetTabular::setChange(Change const & change)
4679 for (idx_type idx = 0; idx < nargs(); ++idx)
4680 cell(idx)->setChange(change);
4684 void InsetTabular::acceptChanges(BufferParams const & bparams)
4686 for (idx_type idx = 0; idx < nargs(); ++idx)
4687 cell(idx)->acceptChanges(bparams);
4691 void InsetTabular::rejectChanges(BufferParams const & bparams)
4693 for (idx_type idx = 0; idx < nargs(); ++idx)
4694 cell(idx)->rejectChanges(bparams);
4698 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4700 return tabular.getPWidth(cell).zero();
4704 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4707 if (buf.length() <= 0)
4710 Buffer const & buffer = bv.buffer();
4714 col_type maxCols = 1;
4715 docstring::size_type const len = buf.length();
4716 docstring::size_type p = 0;
4719 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4727 maxCols = max(cols, maxCols);
4733 maxCols = max(cols, maxCols);
4739 paste_tabular.reset(
4740 new Tabular(buffer.params(), rows, maxCols));
4741 loctab = paste_tabular.get();
4743 dirtyTabularStack(true);
4746 cell = bv.cursor().idx();
4747 ocol = tabular.cellColumn(cell);
4748 row = tabular.cellRow(cell);
4751 docstring::size_type op = 0;
4752 idx_type const cells = loctab->cellCount();
4755 rows = loctab->rowCount();
4756 col_type const columns = loctab->columnCount();
4758 while (cell < cells && p < len && row < rows &&
4759 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4765 // we can only set this if we are not too far right
4766 if (cols < columns) {
4767 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4768 Font const font = bv.textMetrics(&inset->text_).
4769 getDisplayFont(0, 0);
4770 inset->setText(buf.substr(op, p - op), font,
4771 buffer.params().trackChanges);
4777 // we can only set this if we are not too far right
4778 if (cols < columns) {
4779 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4780 Font const font = bv.textMetrics(&inset->text_).
4781 getDisplayFont(0, 0);
4782 inset->setText(buf.substr(op, p - op), font,
4783 buffer.params().trackChanges);
4788 cell = loctab->cellIndex(row, cols);
4794 // check for the last cell if there is no trailing '\n'
4795 if (cell < cells && op < len) {
4796 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4797 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4798 inset->setText(buf.substr(op, len - op), font,
4799 buffer.params().trackChanges);
4805 void InsetTabular::addPreview(PreviewLoader & loader) const
4807 row_type const rows = tabular.rowCount();
4808 col_type const columns = tabular.columnCount();
4809 for (row_type i = 0; i < rows; ++i) {
4810 for (col_type j = 0; j < columns; ++j)
4811 tabular.getCellInset(i, j)->addPreview(loader);
4816 bool InsetTabular::tablemode(Cursor & cur) const
4818 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4824 string const InsetTabularMailer::name_("tabular");
4826 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4827 : inset_(const_cast<InsetTabular &>(inset))
4831 string const InsetTabularMailer::inset2string(Buffer const &) const
4833 return params2string(inset_);
4837 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4839 istringstream data(in);
4841 lex.setStream(data);
4848 if (!lex || token != name_)
4849 return print_mailer_error("InsetTabularMailer", in, 1,
4852 // This is part of the inset proper that is usually swallowed
4853 // by Buffer::readInset
4855 if (!lex || token != "Tabular")
4856 return print_mailer_error("InsetTabularMailer", in, 2,
4859 Buffer const & buffer = inset.buffer();
4860 inset.read(buffer, lex);
4864 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4867 data << name_ << ' ';
4868 inset.write(inset.buffer(), data);
4869 data << "\\end_inset\n";