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 namespace lyx::support;
69 using boost::shared_ptr;
70 using boost::dynamic_pointer_cast;
74 using cap::dirtyTabularStack;
75 using cap::tabularStackDirty;
77 using graphics::PreviewLoader;
79 using frontend::Painter;
80 using frontend::Clipboard;
82 namespace Alert = frontend::Alert;
87 int const ADD_TO_HEIGHT = 2;
88 int const ADD_TO_TABULAR_WIDTH = 2;
89 int const default_line_space = 10;
90 int const WIDTH_OF_LINE = 5;
94 boost::scoped_ptr<Tabular> paste_tabular;
97 struct TabularFeature {
98 Tabular::Feature action;
103 TabularFeature tabularFeature[] =
105 { Tabular::APPEND_ROW, "append-row" },
106 { Tabular::APPEND_COLUMN, "append-column" },
107 { Tabular::DELETE_ROW, "delete-row" },
108 { Tabular::DELETE_COLUMN, "delete-column" },
109 { Tabular::COPY_ROW, "copy-row" },
110 { Tabular::COPY_COLUMN, "copy-column" },
111 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
112 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
113 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
114 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
115 { Tabular::ALIGN_LEFT, "align-left" },
116 { Tabular::ALIGN_RIGHT, "align-right" },
117 { Tabular::ALIGN_CENTER, "align-center" },
118 { Tabular::ALIGN_BLOCK, "align-block" },
119 { Tabular::VALIGN_TOP, "valign-top" },
120 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
121 { Tabular::VALIGN_MIDDLE, "valign-middle" },
122 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
123 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
124 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
125 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
126 { Tabular::M_ALIGN_LEFT, "m-align-left" },
127 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
128 { Tabular::M_ALIGN_CENTER, "m-align-center" },
129 { Tabular::M_VALIGN_TOP, "m-valign-top" },
130 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
131 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
132 { Tabular::MULTICOLUMN, "multicolumn" },
133 { Tabular::SET_ALL_LINES, "set-all-lines" },
134 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
135 { Tabular::SET_LONGTABULAR, "set-longtabular" },
136 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
137 { Tabular::SET_PWIDTH, "set-pwidth" },
138 { Tabular::SET_MPWIDTH, "set-mpwidth" },
139 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
140 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
141 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
142 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
143 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
144 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
145 { Tabular::SET_USEBOX, "set-usebox" },
146 { Tabular::SET_LTHEAD, "set-lthead" },
147 { Tabular::UNSET_LTHEAD, "unset-lthead" },
148 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
149 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
150 { Tabular::SET_LTFOOT, "set-ltfoot" },
151 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
152 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
153 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
154 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
155 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
156 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
157 { Tabular::SET_BOOKTABS, "set-booktabs" },
158 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
159 { Tabular::SET_TOP_SPACE, "set-top-space" },
160 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
161 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
162 { Tabular::LAST_ACTION, "" }
166 class FeatureEqual : public unary_function<TabularFeature, bool> {
168 FeatureEqual(Tabular::Feature feature)
169 : feature_(feature) {}
170 bool operator()(TabularFeature const & tf) const {
171 return tf.action == feature_;
174 Tabular::Feature feature_;
179 string const write_attribute(string const & name, T const & t)
181 string const s = tostr(t);
182 return s.empty() ? s : " " + name + "=\"" + s + "\"";
186 string const write_attribute(string const & name, string const & t)
188 return t.empty() ? t : " " + name + "=\"" + t + "\"";
193 string const write_attribute(string const & name, docstring const & t)
195 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
200 string const write_attribute(string const & name, bool const & b)
202 // we write only true attribute values so we remove a bit of the
203 // file format bloat for tabulars.
204 return b ? write_attribute(name, convert<string>(b)) : string();
209 string const write_attribute(string const & name, int const & i)
211 // we write only true attribute values so we remove a bit of the
212 // file format bloat for tabulars.
213 return i ? write_attribute(name, convert<string>(i)) : string();
218 string const write_attribute(string const & name, Tabular::idx_type const & i)
220 // we write only true attribute values so we remove a bit of the
221 // file format bloat for tabulars.
222 return i ? write_attribute(name, convert<string>(i)) : string();
227 string const write_attribute(string const & name, Length const & value)
229 // we write only the value if we really have one same reson as above.
230 return value.zero() ? string() : write_attribute(name, value.asString());
234 string const tostr(LyXAlignment const & num)
239 case LYX_ALIGN_BLOCK:
243 case LYX_ALIGN_CENTER:
245 case LYX_ALIGN_RIGHT:
247 case LYX_ALIGN_LAYOUT:
249 case LYX_ALIGN_SPECIAL:
256 string const tostr(Tabular::VAlignment const & num)
259 case Tabular::LYX_VALIGN_TOP:
261 case Tabular::LYX_VALIGN_MIDDLE:
263 case Tabular::LYX_VALIGN_BOTTOM:
270 string const tostr(Tabular::BoxType const & num)
273 case Tabular::BOX_NONE:
275 case Tabular::BOX_PARBOX:
277 case Tabular::BOX_MINIPAGE:
284 // I would have liked a fromstr template a lot better. (Lgb)
285 bool string2type(string const str, LyXAlignment & num)
288 num = LYX_ALIGN_NONE;
289 else if (str == "block")
290 num = LYX_ALIGN_BLOCK;
291 else if (str == "left")
292 num = LYX_ALIGN_LEFT;
293 else if (str == "center")
294 num = LYX_ALIGN_CENTER;
295 else if (str == "right")
296 num = LYX_ALIGN_RIGHT;
303 bool string2type(string const str, Tabular::VAlignment & num)
306 num = Tabular::LYX_VALIGN_TOP;
307 else if (str == "middle" )
308 num = Tabular::LYX_VALIGN_MIDDLE;
309 else if (str == "bottom")
310 num = Tabular::LYX_VALIGN_BOTTOM;
317 bool string2type(string const str, Tabular::BoxType & num)
320 num = Tabular::BOX_NONE;
321 else if (str == "parbox")
322 num = Tabular::BOX_PARBOX;
323 else if (str == "minipage")
324 num = Tabular::BOX_MINIPAGE;
331 bool string2type(string const str, bool & num)
335 else if (str == "false")
343 bool getTokenValue(string const & str, char const * token, string & ret)
346 size_t token_length = strlen(token);
347 string::size_type pos = str.find(token);
349 if (pos == string::npos || pos + token_length + 1 >= str.length()
350 || str[pos + token_length] != '=')
352 pos += token_length + 1;
354 if (ch != '"' && ch != '\'') { // only read till next space
358 while (pos < str.length() - 1 && str[++pos] != ch)
365 bool getTokenValue(string const & str, char const * token, docstring & ret)
368 bool const success = getTokenValue(str, token, tmp);
369 ret = from_utf8(tmp);
374 bool getTokenValue(string const & str, char const * token, int & num)
378 if (!getTokenValue(str, token, tmp))
380 num = convert<int>(tmp);
385 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
388 return getTokenValue(str, token, tmp) && string2type(tmp, num);
392 bool getTokenValue(string const & str, char const * token,
393 Tabular::VAlignment & num)
396 return getTokenValue(str, token, tmp) && string2type(tmp, num);
400 bool getTokenValue(string const & str, char const * token,
401 Tabular::BoxType & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token, bool & flag)
410 // set the flag always to false as this should be the default for bools
411 // not in the file-format.
414 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
418 bool getTokenValue(string const & str, char const * token, Length & len)
420 // set the length to be zero() as default as this it should be if not
421 // in the file format.
424 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
428 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
433 if (!getTokenValue(str, token, tmp))
435 if (tmp == "default") {
439 return isValidLength(tmp, &len);
443 void l_getline(istream & is, string & str)
446 while (str.empty()) {
448 if (!str.empty() && str[str.length() - 1] == '\r')
449 str.erase(str.length() - 1);
456 string const featureAsString(Tabular::Feature feature)
458 TabularFeature * end = tabularFeature +
459 sizeof(tabularFeature) / sizeof(TabularFeature);
460 TabularFeature * it = find_if(tabularFeature, end,
461 FeatureEqual(feature));
462 return (it == end) ? string() : it->feature;
467 /////////////////////////////////////////////////////////////////////
471 /////////////////////////////////////////////////////////////////////
474 Tabular::cellstruct::cellstruct(BufferParams const & bp)
477 multicolumn(Tabular::CELL_NORMAL),
478 alignment(LYX_ALIGN_CENTER),
479 valignment(LYX_VALIGN_TOP),
486 inset(new InsetText(bp))
488 inset->paragraphs().back().layout(bp.getTextClass().emptyLayout());
492 Tabular::cellstruct::cellstruct(cellstruct const & cs)
495 multicolumn(cs.multicolumn),
496 alignment(cs.alignment),
497 valignment(cs.valignment),
498 top_line(cs.top_line),
499 bottom_line(cs.bottom_line),
500 left_line(cs.left_line),
501 right_line(cs.right_line),
504 align_special(cs.align_special),
506 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
510 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
517 void Tabular::cellstruct::swap(cellstruct & rhs)
519 std::swap(cellno, rhs.cellno);
520 std::swap(width, rhs.width);
521 std::swap(multicolumn, rhs.multicolumn);
522 std::swap(alignment, rhs.alignment);
523 std::swap(valignment, rhs.valignment);
524 std::swap(top_line, rhs.top_line);
525 std::swap(bottom_line, rhs.bottom_line);
526 std::swap(left_line, rhs.left_line);
527 std::swap(right_line, rhs.right_line);
528 std::swap(usebox, rhs.usebox);
529 std::swap(rotate, rhs.rotate);
530 std::swap(align_special, rhs.align_special);
531 p_width.swap(rhs.p_width);
532 inset.swap(rhs.inset);
536 Tabular::rowstruct::rowstruct()
541 top_space_default(false),
542 bottom_space_default(false),
543 interline_space_default(false),
552 Tabular::columnstruct::columnstruct()
553 : alignment(LYX_ALIGN_CENTER),
554 valignment(LYX_VALIGN_TOP),
562 Tabular::ltType::ltType()
575 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
576 col_type columns_arg)
578 init(bp, rows_arg, columns_arg);
582 // activates all lines and sets all widths to 0
583 void Tabular::init(BufferParams const & bp, row_type rows_arg,
584 col_type columns_arg)
586 row_info = row_vector(rows_arg);
587 column_info = column_vector(columns_arg);
588 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
589 row_info.reserve(10);
590 column_info.reserve(10);
591 cell_info.reserve(100);
593 for (row_type i = 0; i < rows_arg; ++i)
594 cell_info[i].back().right_line = true;
595 row_info.back().bottom_line = true;
596 row_info.front().bottom_line = true;
597 column_info.back().right_line = true;
598 is_long_tabular = false;
600 use_booktabs = false;
604 void Tabular::fixCellNums()
607 for (row_type i = 0; i < rowCount(); ++i) {
608 for (col_type j = 0; j < columnCount(); ++j) {
609 // When debugging it can be nice to set
611 cell_info[i][j].inset->setDrawFrame(false);
612 cell_info[i][j].cellno = cellno++;
614 cell_info[i].back().right_line = true;
621 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
623 row_type const row = cellRow(cell);
625 row_vector::iterator rit = row_info.begin() + row;
626 row_info.insert(rit, rowstruct());
627 // now set the values of the row before
628 row_info[row] = row_info[row + 1];
630 row_type const nrows = rowCount();
631 col_type const ncols = columnCount();
633 cell_vvector old(nrows - 1);
634 for (row_type i = 0; i < nrows - 1; ++i)
635 swap(cell_info[i], old[i]);
637 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
639 for (row_type i = 0; i <= row; ++i)
640 swap(cell_info[i], old[i]);
641 for (row_type i = row + 2; i < nrows; ++i)
642 swap(cell_info[i], old[i - 1]);
645 for (col_type j = 0; j < ncols; ++j)
646 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
652 void Tabular::deleteRow(row_type const row)
654 // Not allowed to delete last row
658 row_info.erase(row_info.begin() + row);
659 cell_info.erase(cell_info.begin() + row);
664 void Tabular::copyRow(BufferParams const & bp, row_type const row)
666 row_info.insert(row_info.begin() + row, row_info[row]);
667 cell_info.insert(cell_info.begin() + row, cell_info[row]);
670 for (col_type j = 0; j < columnCount(); ++j)
671 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
677 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
679 col_type const column = cellColumn(cell);
680 col_type const ncols = columnCount();
681 column_vector::iterator cit = column_info.begin() + column + 1;
682 column_info.insert(cit, columnstruct());
683 // set the column values of the column before
684 column_info[column + 1] = column_info[column];
686 for (row_type i = 0; i < rowCount(); ++i) {
687 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
689 // care about multicolumns
690 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
691 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
693 if (column + 2 >= ncols
694 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
695 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
698 for (row_type i = 0; i < rowCount(); ++i) {
699 cell_info[i][column + 1].inset->clear();
701 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
707 void Tabular::deleteColumn(col_type const column)
709 // Not allowed to delete last column
710 if (columnCount() == 1)
713 column_info.erase(column_info.begin() + column);
714 for (row_type i = 0; i < rowCount(); ++i) {
715 // Care about multicolumn cells
716 if (column + 1 < columnCount() &&
717 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
718 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
719 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
721 cell_info[i].erase(cell_info[i].begin() + column);
727 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
729 column_info.insert(column_info.begin() + column, column_info[column]);
731 for (row_type i = 0; i < rowCount(); ++i)
732 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
735 for (row_type i = 0; i < rowCount(); ++i)
736 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
741 void Tabular::updateIndexes()
744 // Count only non-multicol cells plus begin multicol
745 // cells, ignore non-begin multicol cells:
746 for (row_type row = 0; row < rowCount(); ++row) {
747 for (col_type column = 0; column < columnCount(); ++column) {
748 if (cell_info[row][column].multicolumn
749 != Tabular::CELL_PART_OF_MULTICOLUMN)
751 BOOST_ASSERT(numberofcells != 0);
752 cell_info[row][column].cellno =
757 rowofcell.resize(numberofcells);
758 columnofcell.resize(numberofcells);
763 c < numberofcells && row < rowCount() && column < columnCount();) {
765 columnofcell[c] = column;
769 } while (column < columnCount() &&
770 cell_info[row][column].multicolumn
771 == Tabular::CELL_PART_OF_MULTICOLUMN);
773 if (column == columnCount()) {
779 for (row_type row = 0; row < rowCount(); ++row) {
780 for (col_type column = 0; column < columnCount(); ++column) {
781 if (isPartOfMultiColumn(row,column))
783 cell_info[row][column].inset->setAutoBreakRows(
784 !getPWidth(cellIndex(row, column)).zero());
790 Tabular::idx_type Tabular::cellCount() const
792 return numberofcells;
796 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
798 row_type const row = cellRow(cell);
800 for (col_type i = 0; i < columnCount(); ++i)
801 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
807 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
809 if (!wholerow && isMultiColumn(cell) &&
810 !(use_booktabs && cellRow(cell) == 0))
811 return cellinfo_of_cell(cell).top_line;
812 return row_info[cellRow(cell)].top_line;
816 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
818 if (!wholerow && isMultiColumn(cell) &&
819 !(use_booktabs && isLastRow(cell)))
820 return cellinfo_of_cell(cell).bottom_line;
821 return row_info[cellRow(cell)].bottom_line;
825 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
829 if (!wholecolumn && isMultiColumn(cell) &&
830 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
831 return cellinfo_of_cell(cell).left_line;
832 return column_info[cellColumn(cell)].left_line;
836 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
840 if (!wholecolumn && isMultiColumn(cell) &&
841 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
842 return cellinfo_of_cell(cell).right_line;
843 return column_info[cellRightColumn(cell)].right_line;
847 bool Tabular::topAlreadyDrawn(idx_type cell) const
849 row_type row = cellRow(cell);
850 if (row > 0 && !getAdditionalHeight(row)) {
851 col_type column = cellColumn(cell);
854 && cell_info[row][column].multicolumn
855 == Tabular::CELL_PART_OF_MULTICOLUMN)
857 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
858 return row_info[row].bottom_line;
860 return cell_info[row][column].bottom_line;
866 bool Tabular::leftAlreadyDrawn(idx_type cell) const
868 col_type column = cellColumn(cell);
870 row_type row = cellRow(cell);
872 (cell_info[row][column].multicolumn ==
873 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
874 if (getAdditionalWidth(cell_info[row][column].cellno))
876 return rightLine(cell_info[row][column].cellno);
882 bool Tabular::isLastRow(idx_type cell) const
884 return cellRow(cell) == rowCount() - 1;
888 int Tabular::getAdditionalHeight(row_type row) const
890 if (!row || row >= rowCount())
896 for (col_type column = 0; column < columnCount() && bottom; ++column) {
897 switch (cell_info[row - 1][column].multicolumn) {
898 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
899 bottom = cell_info[row - 1][column].bottom_line;
901 case Tabular::CELL_NORMAL:
902 bottom = row_info[row - 1].bottom_line;
905 for (col_type column = 0; column < columnCount() && top; ++column) {
906 switch (cell_info[row][column].multicolumn) {
907 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
908 top = cell_info[row][column].top_line;
910 case Tabular::CELL_NORMAL:
911 top = row_info[row].top_line;
914 int const interline_space = row_info[row - 1].interline_space_default ?
916 row_info[row - 1].interline_space.inPixels(width());
918 return interline_space + WIDTH_OF_LINE;
919 return interline_space;
923 int Tabular::getAdditionalWidth(idx_type cell) const
925 // internally already set in setCellWidth
926 // used to get it back in text.cpp
927 col_type const col = cellRightColumn(cell);
928 row_type const row = cellRow(cell);
929 if (col < columnCount() - 1 && rightLine(cell) &&
930 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
932 return WIDTH_OF_LINE;
938 // returns the maximum over all rows
939 int Tabular::columnWidth(idx_type cell) const
941 col_type const column1 = cellColumn(cell);
942 col_type const column2 = cellRightColumn(cell);
944 for (col_type i = column1; i <= column2; ++i)
945 result += column_info[i].width;
950 int Tabular::width() const
953 for (col_type i = 0; i < columnCount(); ++i)
954 width += column_info[i].width;
959 // returns true if a complete update is necessary, otherwise false
960 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
962 if (!isMultiColumn(cell))
965 row_type const row = cellRow(cell);
966 col_type const column1 = cellColumn(cell);
967 col_type const column2 = cellRightColumn(cell);
968 int const old_val = cell_info[row][column2].width;
970 // first set columns to 0 so we can calculate the right width
971 for (col_type i = column1; i <= column2; ++i) {
972 cell_info[row][i].width = 0;
974 // set the width to MAX_WIDTH until width > 0
975 int width = new_width + 2 * WIDTH_OF_LINE;
976 col_type i = column1;
977 for (; i < column2 && width > column_info[i].width; ++i) {
978 cell_info[row][i].width = column_info[i].width;
979 width -= column_info[i].width;
982 cell_info[row][i].width = width;
984 if (old_val != cell_info[row][column2].width) {
985 // in this case we have to recalculate all multicolumn cells which
986 // have this column as one of theirs but not as last one
987 calculate_width_of_column_NMC(i);
988 recalculateMulticolumnsOfColumn(i);
989 calculate_width_of_column(i);
995 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
997 // the last column does not have to be recalculated because all
998 // multicolumns will have here there last multicolumn cell which
999 // always will have the whole rest of the width of the cell.
1000 if (columnCount() < 2 || column > (columnCount() - 2))
1002 for (row_type row = 0; row < rowCount(); ++row) {
1003 int mc = cell_info[row][column].multicolumn;
1004 int nmc = cell_info[row][column+1].multicolumn;
1005 // we only have to update multicolumns which do not have this
1006 // column as their last column!
1007 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1008 (mc == CELL_PART_OF_MULTICOLUMN &&
1009 nmc == CELL_PART_OF_MULTICOLUMN))
1011 idx_type const cellno = cell_info[row][column].cellno;
1012 setWidthOfMulticolCell(cellno,
1013 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1019 void Tabular::setCellWidth(idx_type cell, int new_width)
1021 row_type const row = cellRow(cell);
1022 col_type const column1 = cellColumn(cell);
1027 if (rightLine(cell_info[row][column1].cellno, true) &&
1028 column1 < columnCount() - 1 &&
1029 leftLine(cell_info[row][column1+1].cellno, true))
1031 add_width = WIDTH_OF_LINE;
1034 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1037 if (isMultiColumnReal(cell)) {
1038 tmp = setWidthOfMulticolCell(cell, new_width);
1040 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1041 cell_info[row][column1].width = width;
1042 tmp = calculate_width_of_column_NMC(column1);
1044 recalculateMulticolumnsOfColumn(column1);
1047 for (col_type i = 0; i < columnCount(); ++i)
1048 calculate_width_of_column(i);
1053 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1056 if (!isMultiColumn(cell) || onlycolumn)
1057 column_info[cellColumn(cell)].alignment = align;
1059 cellinfo_of_cell(cell).alignment = align;
1063 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1066 if (!isMultiColumn(cell) || onlycolumn)
1067 column_info[cellColumn(cell)].valignment = align;
1069 cellinfo_of_cell(cell).valignment = align;
1076 * Allow line and paragraph breaks for fixed width cells or disallow them,
1077 * merge cell paragraphs and reset layout to standard for variable width
1080 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1082 inset->setAutoBreakRows(fixedWidth);
1086 // merge all paragraphs to one
1087 BufferParams const & bp = cur.bv().buffer().params();
1088 while (inset->paragraphs().size() > 1)
1089 mergeParagraph(bp, inset->paragraphs(), 0);
1093 // undo information has already been recorded
1094 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1095 bp.getTextClass().emptyLayoutName());
1102 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1103 Length const & width)
1105 col_type const j = cellColumn(cell);
1107 column_info[j].p_width = width;
1108 for (row_type i = 0; i < rowCount(); ++i) {
1109 idx_type const cell = cellIndex(i, j);
1110 // because of multicolumns
1111 toggleFixedWidth(cur, getCellInset(cell).get(),
1112 !getPWidth(cell).zero());
1114 // cur paragraph can become invalid after paragraphs were merged
1115 if (cur.pit() > cur.lastpit())
1116 cur.pit() = cur.lastpit();
1117 // cur position can become invalid after newlines were removed
1118 if (cur.pos() > cur.lastpos())
1119 cur.pos() = cur.lastpos();
1123 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1124 Length const & width)
1126 if (!isMultiColumn(cell))
1129 cellinfo_of_cell(cell).p_width = width;
1130 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1131 // cur paragraph can become invalid after paragraphs were merged
1132 if (cur.pit() > cur.lastpit())
1133 cur.pit() = cur.lastpit();
1134 // cur position can become invalid after newlines were removed
1135 if (cur.pos() > cur.lastpos())
1136 cur.pos() = cur.lastpos();
1141 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1142 Tabular::Feature what)
1144 if (what == SET_SPECIAL_MULTI)
1145 cellinfo_of_cell(cell).align_special = special;
1147 column_info[cellColumn(cell)].align_special = special;
1151 void Tabular::setAllLines(idx_type cell, bool line)
1153 setTopLine(cell, line);
1154 setBottomLine(cell, line);
1155 setRightLine(cell, line);
1156 setLeftLine(cell, line);
1160 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1162 row_type const row = cellRow(cell);
1163 if (wholerow || !isMultiColumn(cell))
1164 row_info[row].top_line = line;
1166 cellinfo_of_cell(cell).top_line = line;
1170 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1172 if (wholerow || !isMultiColumn(cell))
1173 row_info[cellRow(cell)].bottom_line = line;
1175 cellinfo_of_cell(cell).bottom_line = line;
1179 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1181 if (wholecolumn || !isMultiColumn(cell))
1182 column_info[cellColumn(cell)].left_line = line;
1184 cellinfo_of_cell(cell).left_line = line;
1188 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1190 if (wholecolumn || !isMultiColumn(cell))
1191 column_info[cellRightColumn(cell)].right_line = line;
1193 cellinfo_of_cell(cell).right_line = line;
1197 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1199 if (!onlycolumn && isMultiColumn(cell))
1200 return cellinfo_of_cell(cell).alignment;
1201 return column_info[cellColumn(cell)].alignment;
1206 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1208 if (!onlycolumn && isMultiColumn(cell))
1209 return cellinfo_of_cell(cell).valignment;
1210 return column_info[cellColumn(cell)].valignment;
1214 Length const Tabular::getPWidth(idx_type cell) const
1216 if (isMultiColumn(cell))
1217 return cellinfo_of_cell(cell).p_width;
1218 return column_info[cellColumn(cell)].p_width;
1222 Length const Tabular::getColumnPWidth(idx_type cell) const
1224 return column_info[cellColumn(cell)].p_width;
1228 Length const Tabular::getMColumnPWidth(idx_type cell) const
1230 if (isMultiColumn(cell))
1231 return cellinfo_of_cell(cell).p_width;
1236 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1238 if (what == SET_SPECIAL_MULTI)
1239 return cellinfo_of_cell(cell).align_special;
1240 return column_info[cellColumn(cell)].align_special;
1244 int Tabular::cellWidth(idx_type cell) const
1246 row_type const row = cellRow(cell);
1247 col_type const column1 = cellColumn(cell);
1248 col_type const column2 = cellRightColumn(cell);
1250 for (col_type i = column1; i <= column2; ++i)
1251 result += cell_info[row][i].width;
1256 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1260 switch (getAlignment(cell)) {
1261 case LYX_ALIGN_CENTER:
1262 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1264 case LYX_ALIGN_RIGHT:
1265 x += columnWidth(cell) - cellWidth(cell);
1266 // + getAdditionalWidth(cell);
1269 // LYX_ALIGN_LEFT: nothing :-)
1273 // the LaTeX Way :-(
1279 bool Tabular::isFirstCellInRow(idx_type cell) const
1281 return cellColumn(cell) == 0;
1285 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1287 if (row > rowCount() - 1)
1288 row = rowCount() - 1;
1289 return cell_info[row][0].cellno;
1293 bool Tabular::isLastCellInRow(idx_type cell) const
1295 return cellRightColumn(cell) == columnCount() - 1;
1299 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1301 if (row > rowCount() - 1)
1302 row = rowCount() - 1;
1303 return cell_info[row][columnCount() - 1].cellno;
1307 void Tabular::calculate_width_of_column(col_type column)
1310 for (row_type i = 0; i < rowCount(); ++i)
1311 maximum = max(cell_info[i][column].width, maximum);
1312 column_info[column].width = maximum;
1317 // Calculate the columns regarding ONLY the normal cells and if this
1318 // column is inside a multicolumn cell then use it only if its the last
1319 // column of this multicolumn cell as this gives an added width to the
1320 // column, all the rest should be adapted!
1322 bool Tabular::calculate_width_of_column_NMC(col_type column)
1324 int const old_column_width = column_info[column].width;
1326 for (row_type i = 0; i < rowCount(); ++i) {
1327 idx_type cell = cellIndex(i, column);
1328 bool ismulti = isMultiColumnReal(cell);
1329 if ((!ismulti || column == cellRightColumn(cell)) &&
1330 cell_info[i][column].width > max)
1332 max = cell_info[i][column].width;
1335 column_info[column].width = max;
1336 return column_info[column].width != old_column_width;
1340 Tabular::row_type Tabular::cellRow(idx_type cell) const
1342 if (cell >= cellCount())
1343 return rowCount() - 1;
1346 return rowofcell[cell];
1350 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1352 if (cell >= cellCount())
1353 return columnCount() - 1;
1356 return columnofcell[cell];
1360 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1362 row_type const row = cellRow(cell);
1363 col_type column = cellColumn(cell);
1364 while (column < columnCount() - 1 &&
1365 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1371 void Tabular::write(Buffer const & buf, ostream & os) const
1375 << write_attribute("version", 3)
1376 << write_attribute("rows", rowCount())
1377 << write_attribute("columns", columnCount())
1379 // global longtable options
1381 << write_attribute("rotate", rotate)
1382 << write_attribute("booktabs", use_booktabs)
1383 << write_attribute("islongtable", is_long_tabular)
1384 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1385 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1386 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1387 << write_attribute("headTopDL", endhead.topDL)
1388 << write_attribute("headBottomDL", endhead.bottomDL)
1389 << write_attribute("footTopDL", endfoot.topDL)
1390 << write_attribute("footBottomDL", endfoot.bottomDL)
1391 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1392 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1393 << write_attribute("lastFootEmpty", endlastfoot.empty)
1395 for (col_type j = 0; j < columnCount(); ++j) {
1397 << write_attribute("alignment", column_info[j].alignment)
1398 << write_attribute("valignment", column_info[j].valignment)
1399 << write_attribute("leftline", column_info[j].left_line)
1400 << write_attribute("rightline", column_info[j].right_line)
1401 << write_attribute("width", column_info[j].p_width.asString())
1402 << write_attribute("special", column_info[j].align_special)
1405 for (row_type i = 0; i < rowCount(); ++i) {
1407 << write_attribute("topline", row_info[i].top_line)
1408 << write_attribute("bottomline", row_info[i].bottom_line);
1409 static const string def("default");
1410 if (row_info[i].top_space_default)
1411 os << write_attribute("topspace", def);
1413 os << write_attribute("topspace", row_info[i].top_space);
1414 if (row_info[i].bottom_space_default)
1415 os << write_attribute("bottomspace", def);
1417 os << write_attribute("bottomspace", row_info[i].bottom_space);
1418 if (row_info[i].interline_space_default)
1419 os << write_attribute("interlinespace", def);
1421 os << write_attribute("interlinespace", row_info[i].interline_space);
1422 os << write_attribute("endhead", row_info[i].endhead)
1423 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1424 << write_attribute("endfoot", row_info[i].endfoot)
1425 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1426 << write_attribute("newpage", row_info[i].newpage)
1428 for (col_type j = 0; j < columnCount(); ++j) {
1430 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1431 << write_attribute("alignment", cell_info[i][j].alignment)
1432 << write_attribute("valignment", cell_info[i][j].valignment)
1433 << write_attribute("topline", cell_info[i][j].top_line)
1434 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1435 << write_attribute("leftline", cell_info[i][j].left_line)
1436 << write_attribute("rightline", cell_info[i][j].right_line)
1437 << write_attribute("rotate", cell_info[i][j].rotate)
1438 << write_attribute("usebox", cell_info[i][j].usebox)
1439 << write_attribute("width", cell_info[i][j].p_width)
1440 << write_attribute("special", cell_info[i][j].align_special)
1442 os << "\\begin_inset ";
1443 cell_info[i][j].inset->write(buf, os);
1444 os << "\n\\end_inset\n"
1449 os << "</lyxtabular>\n";
1453 void Tabular::read(Buffer const & buf, Lexer & lex)
1456 istream & is = lex.getStream();
1458 l_getline(is, line);
1459 if (!prefixIs(line, "<lyxtabular ")
1460 && !prefixIs(line, "<Tabular ")) {
1461 BOOST_ASSERT(false);
1466 if (!getTokenValue(line, "version", version))
1468 BOOST_ASSERT(version >= 2);
1471 if (!getTokenValue(line, "rows", rows_arg))
1474 if (!getTokenValue(line, "columns", columns_arg))
1476 init(buf.params(), rows_arg, columns_arg);
1477 l_getline(is, line);
1478 if (!prefixIs(line, "<features")) {
1479 lyxerr << "Wrong tabular format (expected <features ...> got"
1480 << line << ')' << endl;
1483 getTokenValue(line, "rotate", rotate);
1484 getTokenValue(line, "booktabs", use_booktabs);
1485 getTokenValue(line, "islongtable", is_long_tabular);
1486 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1487 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1488 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1489 getTokenValue(line, "headTopDL", endhead.topDL);
1490 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1491 getTokenValue(line, "footTopDL", endfoot.topDL);
1492 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1493 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1494 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1495 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1497 for (col_type j = 0; j < columnCount(); ++j) {
1499 if (!prefixIs(line,"<column")) {
1500 lyxerr << "Wrong tabular format (expected <column ...> got"
1501 << line << ')' << endl;
1504 getTokenValue(line, "alignment", column_info[j].alignment);
1505 getTokenValue(line, "valignment", column_info[j].valignment);
1506 getTokenValue(line, "leftline", column_info[j].left_line);
1507 getTokenValue(line, "rightline", column_info[j].right_line);
1508 getTokenValue(line, "width", column_info[j].p_width);
1509 getTokenValue(line, "special", column_info[j].align_special);
1512 for (row_type i = 0; i < rowCount(); ++i) {
1513 l_getline(is, line);
1514 if (!prefixIs(line, "<row")) {
1515 lyxerr << "Wrong tabular format (expected <row ...> got"
1516 << line << ')' << endl;
1519 getTokenValue(line, "topline", row_info[i].top_line);
1520 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1521 getTokenValue(line, "topspace", row_info[i].top_space,
1522 row_info[i].top_space_default);
1523 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1524 row_info[i].bottom_space_default);
1525 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1526 row_info[i].interline_space_default);
1527 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1528 getTokenValue(line, "endhead", row_info[i].endhead);
1529 getTokenValue(line, "endfoot", row_info[i].endfoot);
1530 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1531 getTokenValue(line, "newpage", row_info[i].newpage);
1532 for (col_type j = 0; j < columnCount(); ++j) {
1533 l_getline(is, line);
1534 if (!prefixIs(line, "<cell")) {
1535 lyxerr << "Wrong tabular format (expected <cell ...> got"
1536 << line << ')' << endl;
1539 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1540 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1541 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1542 getTokenValue(line, "topline", cell_info[i][j].top_line);
1543 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1544 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1545 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1546 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1547 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1548 getTokenValue(line, "width", cell_info[i][j].p_width);
1549 getTokenValue(line, "special", cell_info[i][j].align_special);
1550 l_getline(is, line);
1551 if (prefixIs(line, "\\begin_inset")) {
1552 cell_info[i][j].inset->read(buf, lex);
1553 l_getline(is, line);
1555 if (!prefixIs(line, "</cell>")) {
1556 lyxerr << "Wrong tabular format (expected </cell> got"
1557 << line << ')' << endl;
1561 l_getline(is, line);
1562 if (!prefixIs(line, "</row>")) {
1563 lyxerr << "Wrong tabular format (expected </row> got"
1564 << line << ')' << endl;
1568 while (!prefixIs(line, "</lyxtabular>")) {
1569 l_getline(is, line);
1575 bool Tabular::isMultiColumn(idx_type cell) const
1577 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1581 bool Tabular::isMultiColumnReal(idx_type cell) const
1583 return cellColumn(cell) != cellRightColumn(cell) &&
1584 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1588 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1590 return cell_info[cellRow(cell)][cellColumn(cell)];
1594 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1597 cellstruct & cs = cellinfo_of_cell(cell);
1598 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1599 cs.alignment = column_info[cellColumn(cell)].alignment;
1600 cs.top_line = row_info[cellRow(cell)].top_line;
1601 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1602 cs.left_line = column_info[cellColumn(cell)].left_line;
1603 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1604 for (idx_type i = 1; i < number; ++i) {
1605 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1606 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1607 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1614 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1616 row_type const row = cellRow(cell);
1617 col_type column = cellColumn(cell);
1618 idx_type result = 1;
1620 while (column < columnCount() &&
1621 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1630 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1632 row_type const row = cellRow(cell);
1633 col_type column = cellColumn(cell);
1635 idx_type result = 0;
1637 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1638 cell_info[row][column].multicolumn = CELL_NORMAL;
1640 while (column < columnCount() &&
1641 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1643 cell_info[row][column].multicolumn = CELL_NORMAL;
1653 void Tabular::setBookTabs(bool what)
1655 use_booktabs = what;
1659 bool Tabular::useBookTabs() const
1661 return use_booktabs;
1665 void Tabular::setLongTabular(bool what)
1667 is_long_tabular = what;
1671 bool Tabular::isLongTabular() const
1673 return is_long_tabular;
1677 void Tabular::setRotateTabular(bool flag)
1683 bool Tabular::getRotateTabular() const
1689 void Tabular::setRotateCell(idx_type cell, bool flag)
1691 cellinfo_of_cell(cell).rotate = flag;
1695 bool Tabular::getRotateCell(idx_type cell) const
1697 return cellinfo_of_cell(cell).rotate;
1701 bool Tabular::needRotating() const
1705 for (row_type i = 0; i < rowCount(); ++i)
1706 for (col_type j = 0; j < columnCount(); ++j)
1707 if (cell_info[i][j].rotate)
1713 bool Tabular::isLastCell(idx_type cell) const
1715 if (cell + 1 < cellCount())
1721 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1723 if (cellRow(cell) > 0)
1724 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1729 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1731 if (cellRow(cell) + 1 < rowCount())
1732 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1737 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1739 if (cellRow(cell) == 0)
1741 if (!isMultiColumn(cell))
1742 return getCellAbove(cell);
1743 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1747 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1749 if (cellRow(cell) + 1 >= rowCount())
1751 if (!isMultiColumn(cell))
1752 return getCellBelow(cell);
1753 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1757 Tabular::idx_type Tabular::cellIndex(row_type row,
1758 col_type column) const
1760 BOOST_ASSERT(column != npos && column < columnCount() &&
1761 row != npos && row < rowCount());
1762 return cell_info[row][column].cellno;
1766 void Tabular::setUsebox(idx_type cell, BoxType type)
1768 cellinfo_of_cell(cell).usebox = type;
1772 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1774 if (column_info[cellColumn(cell)].p_width.zero() &&
1775 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1777 if (cellinfo_of_cell(cell).usebox > 1)
1778 return cellinfo_of_cell(cell).usebox;
1779 return useParbox(cell);
1784 // This are functions used for the longtable support
1786 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1792 row_info[row].endfirsthead = flag;
1796 row_info[row].endhead = flag;
1801 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1804 hd.set = haveLTHead();
1805 return row_info[row].endhead;
1809 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1812 hd.set = haveLTFirstHead();
1813 return row_info[row].endfirsthead;
1817 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1823 row_info[row].endlastfoot = flag;
1827 row_info[row].endfoot = flag;
1832 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1835 fd.set = haveLTFoot();
1836 return row_info[row].endfoot;
1840 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1843 fd.set = haveLTLastFoot();
1844 return row_info[row].endlastfoot;
1848 void Tabular::setLTNewPage(row_type row, bool what)
1850 row_info[row].newpage = what;
1854 bool Tabular::getLTNewPage(row_type row) const
1856 return row_info[row].newpage;
1860 bool Tabular::haveLTHead() const
1862 for (row_type i = 0; i < rowCount(); ++i)
1863 if (row_info[i].endhead)
1869 bool Tabular::haveLTFirstHead() const
1871 if (endfirsthead.empty)
1873 for (row_type i = 0; i < rowCount(); ++i)
1874 if (row_info[i].endfirsthead)
1880 bool Tabular::haveLTFoot() const
1882 for (row_type i = 0; i < rowCount(); ++i)
1883 if (row_info[i].endfoot)
1889 bool Tabular::haveLTLastFoot() const
1891 if (endlastfoot.empty)
1893 for (row_type i = 0; i < rowCount(); ++i)
1894 if (row_info[i].endlastfoot)
1900 // end longtable support functions
1902 void Tabular::setRowAscent(row_type row, int height)
1904 if (row >= rowCount() || row_info[row].ascent == height)
1906 row_info[row].ascent = height;
1910 void Tabular::setRowDescent(row_type row, int height)
1912 if (row >= rowCount() || row_info[row].descent == height)
1914 row_info[row].descent = height;
1918 int Tabular::rowAscent(row_type row) const
1920 if (row >= rowCount())
1922 return row_info[row].ascent;
1926 int Tabular::rowDescent(row_type row) const
1928 BOOST_ASSERT(row < rowCount());
1929 return row_info[row].descent;
1933 int Tabular::height() const
1936 for (row_type row = 0; row < rowCount(); ++row)
1937 height += rowAscent(row) + rowDescent(row) +
1938 getAdditionalHeight(row);
1943 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1945 BOOST_ASSERT(row < rowCount());
1946 BOOST_ASSERT(column < columnCount());
1947 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1951 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1953 // FIXME: assert or return 0 as in TeXBottomHLine()?
1954 BOOST_ASSERT(row != npos);
1955 BOOST_ASSERT(row < rowCount());
1957 idx_type const fcell = getFirstCellInRow(row);
1958 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1961 for (idx_type i = fcell; i < n; ++i) {
1965 if (use_booktabs && row == 0 && topLine(row)) {
1967 } else if (tmp == n - fcell) {
1968 os << (use_booktabs ? "\\midrule " : "\\hline ");
1970 for (idx_type i = fcell; i < n; ++i) {
1972 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1973 << cellColumn(i) + 1
1975 << cellRightColumn(i) + 1
1987 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1989 // FIXME: return 0 or assert as in TeXTopHLine()?
1990 if (row == npos || row >= rowCount())
1993 idx_type const fcell = getFirstCellInRow(row);
1994 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1997 for (idx_type i = fcell; i < n; ++i) {
2001 if (use_booktabs && row == rowCount() - 1) {
2002 os << "\\bottomrule";
2003 } else if (tmp == n - fcell) {
2004 os << (use_booktabs ? "\\midrule" : "\\hline");
2006 for (idx_type i = fcell; i < n; ++i) {
2007 if (bottomLine(i)) {
2008 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2009 << cellColumn(i) + 1
2011 << cellRightColumn(i) + 1
2023 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2027 if (getRotateCell(cell)) {
2028 os << "\\begin{sideways}\n";
2031 if (isMultiColumn(cell)) {
2032 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2033 if (leftLine(cell) &&
2034 (isFirstCellInRow(cell) ||
2035 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2036 !rightLine(cell - 1, true))))
2038 if (!cellinfo_of_cell(cell).align_special.empty()) {
2039 os << cellinfo_of_cell(cell).align_special;
2041 if (!getPWidth(cell).zero()) {
2042 switch (getVAlignment(cell)) {
2043 case LYX_VALIGN_TOP:
2046 case LYX_VALIGN_MIDDLE:
2049 case LYX_VALIGN_BOTTOM:
2054 << from_ascii(getPWidth(cell).asLatexString())
2057 switch (getAlignment(cell)) {
2058 case LYX_ALIGN_LEFT:
2061 case LYX_ALIGN_RIGHT:
2068 } // end if else !getPWidth
2069 } // end if else !cellinfo_of_cell
2070 if (rightLine(cell))
2072 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2077 if (getUsebox(cell) == BOX_PARBOX) {
2079 switch (getVAlignment(cell)) {
2080 case LYX_VALIGN_TOP:
2083 case LYX_VALIGN_MIDDLE:
2086 case LYX_VALIGN_BOTTOM:
2090 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2092 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2093 os << "\\begin{minipage}[";
2094 switch (getVAlignment(cell)) {
2095 case LYX_VALIGN_TOP:
2098 case LYX_VALIGN_MIDDLE:
2101 case LYX_VALIGN_BOTTOM:
2105 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2113 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2118 if (getUsebox(cell) == BOX_PARBOX)
2120 else if (getUsebox(cell) == BOX_MINIPAGE) {
2121 os << "%\n\\end{minipage}";
2124 if (isMultiColumn(cell)) {
2127 if (getRotateCell(cell)) {
2128 os << "%\n\\end{sideways}";
2135 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2136 OutputParams const & runparams) const
2138 if (!is_long_tabular)
2142 // output header info
2144 if (endhead.topDL) {
2148 for (row_type i = 0; i < rowCount(); ++i) {
2149 if (row_info[i].endhead) {
2150 ret += TeXRow(os, i, buf, runparams);
2153 if (endhead.bottomDL) {
2157 os << "\\endhead\n";
2159 if (endfirsthead.empty) {
2160 os << "\\endfirsthead\n";
2164 // output firstheader info
2165 if (haveLTFirstHead()) {
2166 if (endfirsthead.topDL) {
2170 for (row_type i = 0; i < rowCount(); ++i) {
2171 if (row_info[i].endfirsthead) {
2172 ret += TeXRow(os, i, buf, runparams);
2175 if (endfirsthead.bottomDL) {
2179 os << "\\endfirsthead\n";
2182 // output footer info
2184 if (endfoot.topDL) {
2188 for (row_type i = 0; i < rowCount(); ++i) {
2189 if (row_info[i].endfoot) {
2190 ret += TeXRow(os, i, buf, runparams);
2193 if (endfoot.bottomDL) {
2197 os << "\\endfoot\n";
2199 if (endlastfoot.empty) {
2200 os << "\\endlastfoot\n";
2204 // output lastfooter info
2205 if (haveLTLastFoot()) {
2206 if (endlastfoot.topDL) {
2210 for (row_type i = 0; i < rowCount(); ++i) {
2211 if (row_info[i].endlastfoot) {
2212 ret += TeXRow(os, i, buf, runparams);
2215 if (endlastfoot.bottomDL) {
2219 os << "\\endlastfoot\n";
2226 bool Tabular::isValidRow(row_type row) const
2228 if (!is_long_tabular)
2230 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2231 !row_info[row].endfoot && !row_info[row].endlastfoot;
2235 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2236 OutputParams const & runparams) const
2238 idx_type cell = cellIndex(i, 0);
2239 int ret = TeXTopHLine(os, i);
2240 if (row_info[i].top_space_default) {
2242 os << "\\addlinespace\n";
2244 os << "\\noalign{\\vskip\\doublerulesep}\n";
2246 } else if(!row_info[i].top_space.zero()) {
2248 os << "\\addlinespace["
2249 << from_ascii(row_info[i].top_space.asLatexString())
2252 os << "\\noalign{\\vskip"
2253 << from_ascii(row_info[i].top_space.asLatexString())
2259 for (col_type j = 0; j < columnCount(); ++j) {
2260 if (isPartOfMultiColumn(i, j))
2262 ret += TeXCellPreamble(os, cell);
2263 shared_ptr<InsetText> inset = getCellInset(cell);
2265 Paragraph const & par = inset->paragraphs().front();
2266 bool rtl = par.isRTL(buf.params())
2268 && getPWidth(cell).zero();
2271 if (par.getParLanguage(buf.params())->lang() ==
2274 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2276 // currently, remaning RTL languages are arabic_arabtex and hebrew
2280 ret += inset->latex(buf, os, runparams);
2284 ret += TeXCellPostamble(os, cell);
2285 if (!isLastCellInRow(cell)) { // not last cell in row
2290 os << "\\tabularnewline";
2291 if (row_info[i].bottom_space_default) {
2293 os << "\\addlinespace";
2295 os << "[\\doublerulesep]";
2296 } else if (!row_info[i].bottom_space.zero()) {
2298 os << "\\addlinespace";
2300 << from_ascii(row_info[i].bottom_space.asLatexString())
2305 ret += TeXBottomHLine(os, i);
2306 if (row_info[i].interline_space_default) {
2308 os << "\\addlinespace\n";
2310 os << "\\noalign{\\vskip\\doublerulesep}\n";
2312 } else if (!row_info[i].interline_space.zero()) {
2314 os << "\\addlinespace["
2315 << from_ascii(row_info[i].interline_space.asLatexString())
2318 os << "\\noalign{\\vskip"
2319 << from_ascii(row_info[i].interline_space.asLatexString())
2327 int Tabular::latex(Buffer const & buf, odocstream & os,
2328 OutputParams const & runparams) const
2332 //+---------------------------------------------------------------------
2333 //+ first the opening preamble +
2334 //+---------------------------------------------------------------------
2337 os << "\\begin{sideways}\n";
2340 if (is_long_tabular)
2341 os << "\\begin{longtable}{";
2343 os << "\\begin{tabular}{";
2344 for (col_type i = 0; i < columnCount(); ++i) {
2345 if (!use_booktabs && column_info[i].left_line)
2347 if (!column_info[i].align_special.empty()) {
2348 os << column_info[i].align_special;
2350 if (!column_info[i].p_width.zero()) {
2351 switch (column_info[i].alignment) {
2352 case LYX_ALIGN_LEFT:
2353 os << ">{\\raggedright}";
2355 case LYX_ALIGN_RIGHT:
2356 os << ">{\\raggedleft}";
2358 case LYX_ALIGN_CENTER:
2359 os << ">{\\centering}";
2361 case LYX_ALIGN_NONE:
2362 case LYX_ALIGN_BLOCK:
2363 case LYX_ALIGN_LAYOUT:
2364 case LYX_ALIGN_SPECIAL:
2368 switch (column_info[i].valignment) {
2369 case LYX_VALIGN_TOP:
2372 case LYX_VALIGN_MIDDLE:
2375 case LYX_VALIGN_BOTTOM:
2380 << from_ascii(column_info[i].p_width.asLatexString())
2383 switch (column_info[i].alignment) {
2384 case LYX_ALIGN_LEFT:
2387 case LYX_ALIGN_RIGHT:
2394 } // end if else !column_info[i].p_width
2395 } // end if else !column_info[i].align_special
2396 if (!use_booktabs && column_info[i].right_line)
2402 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2404 //+---------------------------------------------------------------------
2405 //+ the single row and columns (cells) +
2406 //+---------------------------------------------------------------------
2408 for (row_type i = 0; i < rowCount(); ++i) {
2409 if (isValidRow(i)) {
2410 ret += TeXRow(os, i, buf, runparams);
2411 if (is_long_tabular && row_info[i].newpage) {
2412 os << "\\newpage\n";
2418 //+---------------------------------------------------------------------
2419 //+ the closing of the tabular +
2420 //+---------------------------------------------------------------------
2422 if (is_long_tabular)
2423 os << "\\end{longtable}";
2425 os << "\\end{tabular}";
2427 os << "\n\\end{sideways}";
2435 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2436 OutputParams const & runparams) const
2439 idx_type cell = getFirstCellInRow(row);
2442 for (col_type j = 0; j < columnCount(); ++j) {
2443 if (isPartOfMultiColumn(row, j))
2446 os << "<entry align=\"";
2447 switch (getAlignment(cell)) {
2448 case LYX_ALIGN_LEFT:
2451 case LYX_ALIGN_RIGHT:
2459 os << "\" valign=\"";
2460 switch (getVAlignment(cell)) {
2461 case LYX_VALIGN_TOP:
2464 case LYX_VALIGN_BOTTOM:
2467 case LYX_VALIGN_MIDDLE:
2472 if (isMultiColumn(cell)) {
2473 os << " namest=\"col" << j << "\" ";
2474 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2478 ret += getCellInset(cell)->docbook(buf, os, runparams);
2487 int Tabular::docbook(Buffer const & buf, odocstream & os,
2488 OutputParams const & runparams) const
2492 //+---------------------------------------------------------------------
2493 //+ first the opening preamble +
2494 //+---------------------------------------------------------------------
2496 os << "<tgroup cols=\"" << columnCount()
2497 << "\" colsep=\"1\" rowsep=\"1\">\n";
2499 for (col_type i = 0; i < columnCount(); ++i) {
2500 os << "<colspec colname=\"col" << i << "\" align=\"";
2501 switch (column_info[i].alignment) {
2502 case LYX_ALIGN_LEFT:
2505 case LYX_ALIGN_RIGHT:
2513 if (runparams.flavor == OutputParams::XML)
2519 //+---------------------------------------------------------------------
2520 //+ Long Tabular case +
2521 //+---------------------------------------------------------------------
2523 // output header info
2524 if (haveLTHead() || haveLTFirstHead()) {
2527 for (row_type i = 0; i < rowCount(); ++i) {
2528 if (row_info[i].endhead || row_info[i].endfirsthead) {
2529 ret += docbookRow(buf, os, i, runparams);
2535 // output footer info
2536 if (haveLTFoot() || haveLTLastFoot()) {
2539 for (row_type i = 0; i < rowCount(); ++i) {
2540 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2541 ret += docbookRow(buf, os, i, runparams);
2548 //+---------------------------------------------------------------------
2549 //+ the single row and columns (cells) +
2550 //+---------------------------------------------------------------------
2554 for (row_type i = 0; i < rowCount(); ++i) {
2555 if (isValidRow(i)) {
2556 ret += docbookRow(buf, os, i, runparams);
2561 //+---------------------------------------------------------------------
2562 //+ the closing of the tabular +
2563 //+---------------------------------------------------------------------
2572 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2573 vector<unsigned int> const & clen) const
2575 idx_type const fcell = getFirstCellInRow(row);
2576 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2579 for (idx_type i = fcell; i < n; ++i) {
2589 for (idx_type i = fcell; i < n; ++i) {
2600 col_type column = cellColumn(i);
2601 int len = clen[column];
2602 while (column < columnCount() - 1
2603 && isPartOfMultiColumn(row, ++column))
2604 len += clen[column] + 4;
2605 os << docstring(len, ch);
2620 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2621 vector<unsigned int> const & clen) const
2623 idx_type const fcell = getFirstCellInRow(row);
2624 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2627 for (idx_type i = fcell; i < n; ++i) {
2628 if (bottomLine(i)) {
2637 for (idx_type i = fcell; i < n; ++i) {
2638 if (bottomLine(i)) {
2648 col_type column = cellColumn(i);
2649 int len = clen[column];
2650 while (column < columnCount() -1
2651 && isPartOfMultiColumn(row, ++column))
2652 len += clen[column] + 4;
2653 os << docstring(len, ch);
2654 if (bottomLine(i)) {
2668 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2669 OutputParams const & runparams,
2670 idx_type cell, row_type row, col_type column,
2671 vector<unsigned int> const & clen,
2672 bool onlydata) const
2674 odocstringstream sstr;
2675 getCellInset(cell)->plaintext(buf, sstr, runparams);
2687 unsigned int len1 = sstr.str().length();
2688 unsigned int len2 = clen[column];
2689 while (column < columnCount() -1
2690 && isPartOfMultiColumn(row, ++column))
2691 len2 += clen[column] + 4;
2694 switch (getAlignment(cell)) {
2696 case LYX_ALIGN_LEFT:
2699 case LYX_ALIGN_RIGHT:
2703 case LYX_ALIGN_CENTER:
2709 os << docstring(len1, ' ') << sstr.str()
2710 << docstring(len2, ' ');
2712 if (rightLine(cell))
2719 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2720 OutputParams const & runparams, int const depth,
2721 bool onlydata, char_type delim) const
2723 // first calculate the width of the single columns
2724 vector<unsigned int> clen(columnCount());
2727 // first all non (real) multicolumn cells!
2728 for (col_type j = 0; j < columnCount(); ++j) {
2730 for (row_type i = 0; i < rowCount(); ++i) {
2731 idx_type cell = cellIndex(i, j);
2732 if (isMultiColumnReal(cell))
2734 odocstringstream sstr;
2735 getCellInset(cell)->plaintext(buf, sstr, runparams);
2736 if (clen[j] < sstr.str().length())
2737 clen[j] = sstr.str().length();
2740 // then all (real) multicolumn cells!
2741 for (col_type j = 0; j < columnCount(); ++j) {
2742 for (row_type i = 0; i < rowCount(); ++i) {
2743 idx_type cell = cellIndex(i, j);
2744 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2746 odocstringstream sstr;
2747 getCellInset(cell)->plaintext(buf, sstr, runparams);
2748 int len = int(sstr.str().length());
2749 idx_type const n = cells_in_multicolumn(cell);
2750 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2752 if (len > int(clen[j + n - 1]))
2753 clen[j + n - 1] = len;
2758 for (row_type i = 0; i < rowCount(); ++i) {
2759 if (!onlydata && plaintextTopHLine(os, i, clen))
2760 os << docstring(depth * 2, ' ');
2761 for (col_type j = 0; j < columnCount(); ++j) {
2762 if (isPartOfMultiColumn(i, j))
2764 if (onlydata && j > 0)
2765 // we don't use operator<< for single UCS4 character.
2766 // see explanation in docstream.h
2768 plaintextPrintCell(buf, os, runparams,
2769 cell, i, j, clen, onlydata);
2774 os << docstring(depth * 2, ' ');
2775 if (plaintextBottomHLine(os, i, clen))
2776 os << docstring(depth * 2, ' ');
2782 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2784 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2788 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2789 col_type column) const
2791 return cell_info[row][column].inset;
2795 void Tabular::setCellInset(row_type row, col_type column,
2796 shared_ptr<InsetText> ins) const
2798 cell_info[row][column].inset = ins;
2803 Tabular::getCellFromInset(Inset const * inset) const
2805 // is this inset part of the tabular?
2807 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2808 BOOST_ASSERT(false);
2811 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2812 if (getCellInset(cell).get() == inset) {
2813 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2814 << "cell=" << cell);
2818 // We should have found a cell at this point
2819 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2820 << inset << " not found!" << endl;
2821 BOOST_ASSERT(false);
2827 void Tabular::validate(LaTeXFeatures & features) const
2829 features.require("NeedTabularnewline");
2831 features.require("booktabs");
2832 if (isLongTabular())
2833 features.require("longtable");
2835 features.require("rotating");
2836 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2837 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2838 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2839 features.require("array");
2840 getCellInset(cell)->validate(features);
2845 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2847 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2848 ParagraphList::const_iterator cit = parlist.begin();
2849 ParagraphList::const_iterator end = parlist.end();
2851 for (; cit != end; ++cit)
2852 for (int i = 0; i < cit->size(); ++i)
2853 if (cit->isNewline(i))
2860 /////////////////////////////////////////////////////////////////////
2864 /////////////////////////////////////////////////////////////////////
2866 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2868 : tabular(buf.params(), max(rows, row_type(1)),
2869 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2873 InsetTabular::InsetTabular(InsetTabular const & tab)
2874 : Inset(tab), tabular(tab.tabular),
2875 buffer_(tab.buffer_), scx_(0)
2879 InsetTabular::~InsetTabular()
2881 InsetTabularMailer(*this).hideDialog();
2885 Inset * InsetTabular::clone() const
2887 return new InsetTabular(*this);
2891 Buffer const & InsetTabular::buffer() const
2897 void InsetTabular::buffer(Buffer const * b)
2903 bool InsetTabular::insetAllowed(InsetCode code) const
2905 if (code == MATHMACRO_CODE)
2912 void InsetTabular::write(Buffer const & buf, ostream & os) const
2914 os << "Tabular" << endl;
2915 tabular.write(buf, os);
2919 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2921 bool const old_format = (lex.getString() == "\\LyXTable");
2923 tabular.read(buf, lex);
2929 string token = lex.getString();
2930 while (lex && token != "\\end_inset") {
2932 token = lex.getString();
2935 lex.printError("Missing \\end_inset at this point. "
2941 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2943 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2944 // mi.base.textwidth << "\n";
2946 lyxerr << "InsetTabular::metrics: need bv" << endl;
2947 BOOST_ASSERT(false);
2951 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2954 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2955 if (tabular.isPartOfMultiColumn(i, j))
2956 // Multicolumn cell, but not first one
2961 if (tabular.cell_info[i][j].multicolumn ==
2962 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2963 p_width = tabular.cellinfo_of_cell(cell).p_width;
2965 p_width = tabular.column_info[j].p_width;
2966 if (!p_width.zero())
2967 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2968 tabular.getCellInset(cell)->metrics(m, dim);
2969 if (!p_width.zero())
2970 dim.wid = m.base.textwidth;
2971 tabular.setCellWidth(cell, dim.wid);
2972 if (p_width.zero()) {
2973 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2974 // FIXME there must be a way to get rid of
2975 // the second metrics call
2976 tabular.getCellInset(cell)->metrics(m, dim);
2978 maxAsc = max(maxAsc, dim.asc);
2979 maxDesc = max(maxDesc, dim.des);
2982 int const top_space = tabular.row_info[i].top_space_default ?
2983 default_line_space :
2984 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2985 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2986 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2987 default_line_space :
2988 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2989 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2992 dim.asc = tabular.rowAscent(0);
2993 dim.des = tabular.height() - dim.asc;
2994 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2998 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3000 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3001 BufferView * bv = pi.base.bv;
3003 // FIXME: As the full backrgound is painted in drawSelection(),
3004 // we have no choice but to do a full repaint for the Text cells.
3005 pi.full_repaint = true;
3007 resetPos(bv->cursor());
3010 x += ADD_TO_TABULAR_WIDTH;
3012 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3015 first_visible_cell = Tabular::npos;
3016 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3018 int const a = tabular.rowAscent(i);
3019 int const d = tabular.rowDescent(i);
3020 idx = tabular.cellIndex(i, 0);
3021 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3022 if (tabular.isPartOfMultiColumn(i, j))
3024 if (first_visible_cell == Tabular::npos)
3025 first_visible_cell = idx;
3027 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3028 // Cache the Inset position.
3029 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3030 if (nx + tabular.columnWidth(idx) < 0
3031 || nx > bv->workWidth()
3033 || y - a > bv->workHeight()) {
3034 pi.pain.setDrawingEnabled(false);
3035 cell(idx)->draw(pi, cx, y);
3036 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3037 pi.pain.setDrawingEnabled(original_drawing_state);
3039 cell(idx)->draw(pi, cx, y);
3040 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3042 nx += tabular.columnWidth(idx);
3046 if (i + 1 < tabular.rowCount())
3047 y += d + tabular.rowAscent(i + 1) +
3048 tabular.getAdditionalHeight(i + 1);
3053 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3055 Cursor & cur = pi.base.bv->cursor();
3057 x += scx_ + ADD_TO_TABULAR_WIDTH;
3059 // FIXME: it is wrong to completely paint the background
3060 // if we want to do single row painting.
3062 // Paint background of current tabular
3063 int const w = tabular.width();
3064 int const h = tabular.height();
3065 int yy = y - tabular.rowAscent(0);
3066 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3068 if (!cur.selection())
3070 if (&cur.inset() != this)
3076 if (tablemode(cur)) {
3079 getSelection(cur, rs, re, cs, ce);
3080 y -= tabular.rowAscent(0);
3081 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3082 int const a = tabular.rowAscent(j);
3083 int const h = a + tabular.rowDescent(j);
3085 y += tabular.getAdditionalHeight(j);
3086 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3087 if (tabular.isPartOfMultiColumn(j, i))
3089 idx_type const cell =
3090 tabular.cellIndex(j, i);
3091 int const w = tabular.columnWidth(cell);
3092 if (i >= cs && i <= ce && j >= rs && j <= re)
3093 pi.pain.fillRectangle(xx, y, w, h,
3101 x += getCellXPos(cur.idx());
3102 x += tabular.getBeginningOfTextInCell(cur.idx());
3103 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3108 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3109 row_type row, idx_type cell, bool erased) const
3111 int x2 = x + tabular.columnWidth(cell);
3112 bool on_off = false;
3113 ColorCode col = Color_tabularline;
3114 ColorCode onoffcol = Color_tabularonoffline;
3117 col = Color_deletedtext;
3118 onoffcol = Color_deletedtext;
3121 if (!tabular.topAlreadyDrawn(cell)) {
3122 on_off = !tabular.topLine(cell);
3123 pain.line(x, y - tabular.rowAscent(row),
3124 x2, y - tabular.rowAscent(row),
3125 on_off ? onoffcol : col,
3126 on_off ? Painter::line_onoffdash : Painter::line_solid);
3128 on_off = !tabular.bottomLine(cell);
3129 pain.line(x, y + tabular.rowDescent(row),
3130 x2, y + tabular.rowDescent(row),
3131 on_off ? onoffcol : col,
3132 on_off ? Painter::line_onoffdash : Painter::line_solid);
3133 if (!tabular.leftAlreadyDrawn(cell)) {
3134 on_off = !tabular.leftLine(cell);
3135 pain.line(x, y - tabular.rowAscent(row),
3136 x, y + tabular.rowDescent(row),
3137 on_off ? onoffcol : col,
3138 on_off ? Painter::line_onoffdash : Painter::line_solid);
3140 on_off = !tabular.rightLine(cell);
3141 pain.line(x2 - tabular.getAdditionalWidth(cell),
3142 y - tabular.rowAscent(row),
3143 x2 - tabular.getAdditionalWidth(cell),
3144 y + tabular.rowDescent(row),
3145 on_off ? onoffcol : col,
3146 on_off ? Painter::line_onoffdash : Painter::line_solid);
3150 docstring const InsetTabular::editMessage() const
3152 return _("Opened table");
3156 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3158 //lyxerr << "InsetTabular::edit: " << this << endl;
3160 cur.selection() = false;
3163 if (isRightToLeft(cur))
3164 cur.idx() = tabular.getLastCellInRow(0);
3170 if (isRightToLeft(cur))
3171 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3173 cur.idx() = tabular.cellCount() - 1;
3175 cur.pos() = cur.lastpos(); // FIXME crude guess
3177 // FIXME: this accesses the position cache before it is initialized
3179 //cur.bv().fitCursor();
3183 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3185 // In a longtable, tell captions what the current float is
3186 Counters & cnts = buf.params().getTextClass().counters();
3187 string const saveflt = cnts.current_float();
3188 if (tabular.isLongTabular())
3189 cnts.current_float("table");
3191 ParIterator it2 = it;
3193 size_t const end = it2.nargs();
3194 for ( ; it2.idx() < end; it2.top().forwardIdx())
3195 lyx::updateLabels(buf, it2);
3198 if (tabular.isLongTabular())
3199 cnts.current_float(saveflt);
3203 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3205 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3206 << "\n cur:" << cur);
3207 CursorSlice sl = cur.top();
3208 Cursor & bvcur = cur.bv().cursor();
3210 switch (cmd.action) {
3212 case LFUN_MOUSE_PRESS:
3213 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3215 // do not reset cursor/selection if we have selected
3216 // some cells (bug 2715).
3217 if (cmd.button() == mouse_button::button3
3218 && &bvcur.selBegin().inset() == this
3219 && tablemode(bvcur))
3222 // Let InsetText do it
3223 cell(cur.idx())->dispatch(cur, cmd);
3225 case LFUN_MOUSE_MOTION:
3226 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3227 if (cmd.button() == mouse_button::button1) {
3228 // only accept motions to places not deeper nested than the real anchor
3229 if (bvcur.anchor_.hasPart(cur)) {
3230 // only update if selection changes
3231 if (bvcur.idx() == cur.idx() &&
3232 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3234 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3235 bvcur.setCursor(cur);
3236 bvcur.selection() = true;
3242 case LFUN_MOUSE_RELEASE:
3243 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3244 if (cmd.button() == mouse_button::button3)
3245 InsetTabularMailer(*this).showDialog(&cur.bv());
3248 case LFUN_CELL_BACKWARD:
3250 cur.selection() = false;
3253 case LFUN_CELL_FORWARD:
3255 cur.selection() = false;
3258 case LFUN_CHAR_FORWARD_SELECT:
3259 case LFUN_CHAR_FORWARD:
3260 cell(cur.idx())->dispatch(cur, cmd);
3261 if (!cur.result().dispatched()) {
3263 if (sl == cur.top())
3264 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3270 case LFUN_CHAR_BACKWARD_SELECT:
3271 case LFUN_CHAR_BACKWARD:
3272 cell(cur.idx())->dispatch(cur, cmd);
3273 if (!cur.result().dispatched()) {
3275 if (sl == cur.top())
3276 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3282 case LFUN_CHAR_RIGHT_SELECT:
3283 case LFUN_CHAR_RIGHT:
3284 //FIXME: for visual cursor, really move right
3285 if (isRightToLeft(cur))
3286 lyx::dispatch(FuncRequest(
3287 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3288 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3290 lyx::dispatch(FuncRequest(
3291 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3292 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3295 case LFUN_CHAR_LEFT_SELECT:
3296 case LFUN_CHAR_LEFT:
3297 //FIXME: for visual cursor, really move left
3298 if (isRightToLeft(cur))
3299 lyx::dispatch(FuncRequest(
3300 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3301 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3303 lyx::dispatch(FuncRequest(
3304 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3305 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3308 case LFUN_DOWN_SELECT:
3310 cell(cur.idx())->dispatch(cur, cmd);
3311 cur.dispatched(); // override the cell's decision
3312 if (sl == cur.top())
3313 // if our Text didn't do anything to the cursor
3314 // then we try to put the cursor into the cell below
3315 // setting also the right targetX.
3316 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3317 cur.idx() = tabular.getCellBelow(cur.idx());
3319 TextMetrics const & tm =
3320 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3321 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3323 if (sl == cur.top()) {
3324 // we trick it to go to forward after leaving the
3326 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3331 case LFUN_UP_SELECT:
3333 cell(cur.idx())->dispatch(cur, cmd);
3334 cur.dispatched(); // override the cell's decision
3335 if (sl == cur.top())
3336 // if our Text didn't do anything to the cursor
3337 // then we try to put the cursor into the cell above
3338 // setting also the right targetX.
3339 if (tabular.cellRow(cur.idx()) != 0) {
3340 cur.idx() = tabular.getCellAbove(cur.idx());
3341 cur.pit() = cur.lastpit();
3342 Text const * text = cell(cur.idx())->getText(0);
3343 TextMetrics const & tm = cur.bv().textMetrics(text);
3344 ParagraphMetrics const & pm =
3345 tm.parMetrics(cur.lastpit());
3346 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3348 if (sl == cur.top()) {
3349 cmd = FuncRequest(LFUN_UP);
3354 // case LFUN_SCREEN_DOWN: {
3355 // //if (hasSelection())
3356 // // cur.selection() = false;
3357 // col_type const col = tabular.cellColumn(cur.idx());
3358 // int const t = cur.bv().top_y() + cur.bv().height();
3359 // if (t < yo() + tabular.getHeightOfTabular()) {
3360 // cur.bv().scrollDocView(t);
3361 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3363 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3370 // case LFUN_SCREEN_UP: {
3371 // //if (hasSelection())
3372 // // cur.selection() = false;
3373 // col_type const col = tabular.cellColumn(cur.idx());
3374 // int const t = cur.bv().top_y() + cur.bv().height();
3376 // cur.bv().scrollDocView(t);
3380 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3384 // cur.par() = cur.lastpar();
3385 // cur.pos() = cur.lastpos();
3389 case LFUN_LAYOUT_TABULAR:
3390 InsetTabularMailer(*this).showDialog(&cur.bv());
3393 case LFUN_INSET_DIALOG_UPDATE:
3394 InsetTabularMailer(*this).updateDialog(&cur.bv());
3397 case LFUN_TABULAR_FEATURE:
3398 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3402 // insert file functions
3403 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3404 case LFUN_FILE_INSERT_PLAINTEXT: {
3406 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3407 FileName(to_utf8(cmd.argument())));
3410 cur.recordUndoInset(INSERT_UNDO);
3411 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3412 // content has been replaced,
3413 // so cursor might be invalid
3414 cur.pos() = cur.lastpos();
3415 cur.pit() = cur.lastpit();
3416 bvcur.setCursor(cur);
3423 if (tablemode(cur)) {
3424 if (copySelection(cur)) {
3425 cur.recordUndoInset(DELETE_UNDO);
3430 cell(cur.idx())->dispatch(cur, cmd);
3433 case LFUN_CHAR_DELETE_BACKWARD:
3434 case LFUN_CHAR_DELETE_FORWARD:
3435 if (tablemode(cur)) {
3436 cur.recordUndoInset(DELETE_UNDO);
3440 cell(cur.idx())->dispatch(cur, cmd);
3444 if (!cur.selection())
3446 if (tablemode(cur)) {
3450 cell(cur.idx())->dispatch(cur, cmd);
3453 case LFUN_CLIPBOARD_PASTE:
3454 case LFUN_PRIMARY_SELECTION_PASTE: {
3455 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3456 theClipboard().getAsText() :
3457 theSelection().get();
3460 // pass to InsertPlaintextString, but
3461 // only if we have multi-cell content
3462 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3463 cur.recordUndoInset(INSERT_UNDO);
3464 if (insertPlaintextString(cur.bv(), clip, false)) {
3465 // content has been replaced,
3466 // so cursor might be invalid
3467 cur.pos() = cur.lastpos();
3468 cur.pit() = cur.lastpit();
3469 bvcur.setCursor(cur);
3473 // Let the cell handle normal text
3474 cell(cur.idx())->dispatch(cur, cmd);
3479 if (tabularStackDirty() && theClipboard().isInternal() ||
3480 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3481 cur.recordUndoInset(INSERT_UNDO);
3482 pasteClipboard(cur);
3485 cell(cur.idx())->dispatch(cur, cmd);
3488 case LFUN_FONT_EMPH:
3489 case LFUN_FONT_BOLD:
3490 case LFUN_FONT_ROMAN:
3491 case LFUN_FONT_NOUN:
3492 case LFUN_FONT_ITAL:
3493 case LFUN_FONT_FRAK:
3494 case LFUN_FONT_TYPEWRITER:
3495 case LFUN_FONT_SANS:
3496 case LFUN_FONT_FREE_APPLY:
3497 case LFUN_FONT_FREE_UPDATE:
3498 case LFUN_FONT_SIZE:
3499 case LFUN_FONT_UNDERLINE:
3501 case LFUN_WORD_CAPITALIZE:
3502 case LFUN_WORD_UPCASE:
3503 case LFUN_WORD_LOWCASE:
3504 case LFUN_CHARS_TRANSPOSE:
3505 if (tablemode(cur)) {
3508 getSelection(cur, rs, re, cs, ce);
3509 Cursor tmpcur = cur;
3510 for (row_type i = rs; i <= re; ++i) {
3511 for (col_type j = cs; j <= ce; ++j) {
3512 // cursor follows cell:
3513 tmpcur.idx() = tabular.cellIndex(i, j);
3514 // select this cell only:
3517 tmpcur.resetAnchor();
3518 tmpcur.pit() = tmpcur.lastpit();
3519 tmpcur.pos() = tmpcur.top().lastpos();
3520 tmpcur.setCursor(tmpcur);
3521 tmpcur.setSelection();
3522 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3527 cell(cur.idx())->dispatch(cur, cmd);
3531 // we try to handle this event in the insets dispatch function.
3532 cell(cur.idx())->dispatch(cur, cmd);
3538 // function sets an object as defined in func_status.h:
3539 // states OK, Unknown, Disabled, On, Off.
3540 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3541 FuncStatus & status) const
3543 switch (cmd.action) {
3544 case LFUN_TABULAR_FEATURE: {
3545 int action = Tabular::LAST_ACTION;
3547 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3548 string const tmp = tabularFeature[i].feature;
3549 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3550 action = tabularFeature[i].action;
3554 if (action == Tabular::LAST_ACTION) {
3556 status.unknown(true);
3560 string const argument
3561 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3563 row_type sel_row_start = 0;
3564 row_type sel_row_end = 0;
3565 col_type sel_col_start = 0;
3566 col_type sel_col_end = 0;
3567 Tabular::ltType dummyltt;
3570 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3573 case Tabular::SET_PWIDTH:
3574 case Tabular::SET_MPWIDTH:
3575 case Tabular::SET_SPECIAL_COLUMN:
3576 case Tabular::SET_SPECIAL_MULTI:
3577 case Tabular::APPEND_ROW:
3578 case Tabular::APPEND_COLUMN:
3579 case Tabular::DELETE_ROW:
3580 case Tabular::DELETE_COLUMN:
3581 case Tabular::COPY_ROW:
3582 case Tabular::COPY_COLUMN:
3583 case Tabular::SET_ALL_LINES:
3584 case Tabular::UNSET_ALL_LINES:
3585 case Tabular::SET_TOP_SPACE:
3586 case Tabular::SET_BOTTOM_SPACE:
3587 case Tabular::SET_INTERLINE_SPACE:
3591 case Tabular::MULTICOLUMN:
3592 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3595 case Tabular::M_TOGGLE_LINE_TOP:
3597 case Tabular::TOGGLE_LINE_TOP:
3598 status.setOnOff(tabular.topLine(cur.idx(), flag));
3601 case Tabular::M_TOGGLE_LINE_BOTTOM:
3603 case Tabular::TOGGLE_LINE_BOTTOM:
3604 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3607 case Tabular::M_TOGGLE_LINE_LEFT:
3609 case Tabular::TOGGLE_LINE_LEFT:
3610 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3613 case Tabular::M_TOGGLE_LINE_RIGHT:
3615 case Tabular::TOGGLE_LINE_RIGHT:
3616 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3619 case Tabular::M_ALIGN_LEFT:
3621 case Tabular::ALIGN_LEFT:
3622 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3625 case Tabular::M_ALIGN_RIGHT:
3627 case Tabular::ALIGN_RIGHT:
3628 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3631 case Tabular::M_ALIGN_CENTER:
3633 case Tabular::ALIGN_CENTER:
3634 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3637 case Tabular::ALIGN_BLOCK:
3638 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3639 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3642 case Tabular::M_VALIGN_TOP:
3644 case Tabular::VALIGN_TOP:
3646 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3649 case Tabular::M_VALIGN_BOTTOM:
3651 case Tabular::VALIGN_BOTTOM:
3653 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3656 case Tabular::M_VALIGN_MIDDLE:
3658 case Tabular::VALIGN_MIDDLE:
3660 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3663 case Tabular::SET_LONGTABULAR:
3664 status.setOnOff(tabular.isLongTabular());
3667 case Tabular::UNSET_LONGTABULAR:
3668 status.setOnOff(!tabular.isLongTabular());
3671 case Tabular::TOGGLE_ROTATE_TABULAR:
3672 case Tabular::SET_ROTATE_TABULAR:
3673 status.setOnOff(tabular.getRotateTabular());
3676 case Tabular::UNSET_ROTATE_TABULAR:
3677 status.setOnOff(!tabular.getRotateTabular());
3680 case Tabular::TOGGLE_ROTATE_CELL:
3681 case Tabular::SET_ROTATE_CELL:
3682 status.setOnOff(!oneCellHasRotationState(false,
3683 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3686 case Tabular::UNSET_ROTATE_CELL:
3687 status.setOnOff(!oneCellHasRotationState(true,
3688 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3691 case Tabular::SET_USEBOX:
3692 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3695 case Tabular::SET_LTFIRSTHEAD:
3696 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3699 case Tabular::UNSET_LTFIRSTHEAD:
3700 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3703 case Tabular::SET_LTHEAD:
3704 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3707 case Tabular::UNSET_LTHEAD:
3708 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3711 case Tabular::SET_LTFOOT:
3712 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3715 case Tabular::UNSET_LTFOOT:
3716 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3719 case Tabular::SET_LTLASTFOOT:
3720 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3723 case Tabular::UNSET_LTLASTFOOT:
3724 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3727 case Tabular::SET_LTNEWPAGE:
3728 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3731 case Tabular::SET_BOOKTABS:
3732 status.setOnOff(tabular.useBookTabs());
3735 case Tabular::UNSET_BOOKTABS:
3736 status.setOnOff(!tabular.useBookTabs());
3741 status.enabled(false);
3747 // These are only enabled inside tabular
3748 case LFUN_CELL_BACKWARD:
3749 case LFUN_CELL_FORWARD:
3750 status.enabled(true);
3753 // disable these with multiple cells selected
3754 case LFUN_INSET_INSERT:
3755 case LFUN_TABULAR_INSERT:
3756 case LFUN_FLEX_INSERT:
3757 case LFUN_FLOAT_INSERT:
3758 case LFUN_FLOAT_WIDE_INSERT:
3759 case LFUN_FOOTNOTE_INSERT:
3760 case LFUN_MARGINALNOTE_INSERT:
3761 case LFUN_MATH_INSERT:
3762 case LFUN_MATH_MODE:
3763 case LFUN_MATH_MUTATE:
3764 case LFUN_MATH_DISPLAY:
3765 case LFUN_NOTE_INSERT:
3766 case LFUN_OPTIONAL_INSERT:
3767 case LFUN_BOX_INSERT:
3768 case LFUN_BRANCH_INSERT:
3769 case LFUN_WRAP_INSERT:
3770 case LFUN_ERT_INSERT: {
3771 if (tablemode(cur)) {
3772 status.enabled(false);
3775 return cell(cur.idx())->getStatus(cur, cmd, status);
3778 // disable in non-fixed-width cells
3780 case LFUN_BREAK_PARAGRAPH:
3781 case LFUN_BREAK_PARAGRAPH_SKIP: {
3782 if (tabular.getPWidth(cur.idx()).zero()) {
3783 status.enabled(false);
3786 return cell(cur.idx())->getStatus(cur, cmd, status);
3790 if (tabularStackDirty() && theClipboard().isInternal()) {
3791 status.enabled(true);
3794 return cell(cur.idx())->getStatus(cur, cmd, status);
3796 case LFUN_INSET_MODIFY:
3797 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3798 status.enabled(true);
3804 // we try to handle this event in the insets dispatch function.
3805 return cell(cur.idx())->getStatus(cur, cmd, status);
3810 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3811 OutputParams const & runparams) const
3813 return tabular.latex(buf, os, runparams);
3817 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3818 OutputParams const & runparams) const
3820 os << '\n'; // output table on a new line
3821 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3822 tabular.plaintext(buf, os, runparams, dp, false, 0);
3823 return PLAINTEXT_NEWLINE;
3827 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3828 OutputParams const & runparams) const
3833 // FIXME: Why not pass a proper DocIterator here?
3835 // if the table is inside a float it doesn't need the informaltable
3836 // wrapper. Search for it.
3837 for (master = owner(); master; master = master->owner())
3838 if (master->lyxCode() == FLOAT_CODE)
3843 os << "<informaltable>";
3846 ret += tabular.docbook(buf, os, runparams);
3848 os << "</informaltable>";
3855 void InsetTabular::validate(LaTeXFeatures & features) const
3857 tabular.validate(features);
3861 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3863 return tabular.getCellInset(idx);
3867 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3869 return tabular.getCellInset(idx);
3873 void InsetTabular::cursorPos(BufferView const & bv,
3874 CursorSlice const & sl, bool boundary, int & x, int & y) const
3876 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3878 // y offset correction
3879 int const row = tabular.cellRow(sl.idx());
3880 for (int i = 0; i <= row; ++i) {
3882 y += tabular.rowAscent(i);
3883 y += tabular.getAdditionalHeight(i);
3886 y += tabular.rowDescent(i);
3889 // x offset correction
3890 int const col = tabular.cellColumn(sl.idx());
3891 int idx = tabular.cellIndex(row, 0);
3892 for (int j = 0; j < col; ++j) {
3893 if (tabular.isPartOfMultiColumn(row, j))
3895 x += tabular.columnWidth(idx);
3898 x += tabular.getBeginningOfTextInCell(idx);
3899 x += ADD_TO_TABULAR_WIDTH;
3904 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3908 Inset const & inset = *tabular.getCellInset(cell);
3909 Point o = bv.coordCache().getInsets().xy(&inset);
3910 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3911 int const xend = xbeg + tabular.columnWidth(cell);
3912 row_type const row = tabular.cellRow(cell);
3913 int const ybeg = o.y_ - tabular.rowAscent(row) -
3914 tabular.getAdditionalHeight(row);
3915 int const yend = o.y_ + tabular.rowDescent(row);
3927 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3928 // << " ybeg=" << ybeg << " yend=" << yend
3929 // << " xx=" << xx << " yy=" << yy
3930 // << " dist=" << xx + yy << endl;
3935 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3937 //lyxerr << "InsetTabular::editXY: " << this << endl;
3938 cur.selection() = false;
3940 cur.idx() = getNearestCell(cur.bv(), x, y);
3942 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3946 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3948 cur.idx() = getNearestCell(cur.bv(), x, y);
3949 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3953 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3955 idx_type idx_min = 0;
3956 int dist_min = numeric_limits<int>::max();
3957 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3958 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3959 int const d = dist(bv, i, x, y);
3970 int InsetTabular::getCellXPos(idx_type const cell) const
3974 for (; !tabular.isFirstCellInRow(c); --c)
3977 for (; c < cell; ++c)
3978 lx += tabular.columnWidth(c);
3984 void InsetTabular::resetPos(Cursor & cur) const
3986 BufferView & bv = cur.bv();
3987 int const maxwidth = bv.workWidth();
3989 if (&cur.inset() != this) {
3993 int const X2 = maxwidth;
3994 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3995 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3996 int const x2 = x1 + tabular.columnWidth(cur.idx());
3999 scx_ = X1 + 20 - x1;
4001 scx_ = X2 - 20 - x2;
4006 cur.updateFlags(Update::Force | Update::FitCursor);
4010 void InsetTabular::moveNextCell(Cursor & cur)
4012 if (isRightToLeft(cur)) {
4013 if (tabular.isFirstCellInRow(cur.idx())) {
4014 row_type const row = tabular.cellRow(cur.idx());
4015 if (row == tabular.rowCount() - 1)
4017 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4024 if (tabular.isLastCell(cur.idx()))
4034 void InsetTabular::movePrevCell(Cursor & cur)
4036 if (isRightToLeft(cur)) {
4037 if (tabular.isLastCellInRow(cur.idx())) {
4038 row_type const row = tabular.cellRow(cur.idx());
4041 cur.idx() = tabular.getFirstCellInRow(row);
4042 cur.idx() = tabular.getCellAbove(cur.idx());
4044 if (tabular.isLastCell(cur.idx()))
4049 if (cur.idx() == 0) // first cell
4053 cur.pit() = cur.lastpit();
4054 cur.pos() = cur.lastpos();
4056 // FIXME: this accesses the position cache before it is initialized
4061 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4063 Tabular::Feature action = Tabular::LAST_ACTION;
4066 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4067 string const tmp = tabularFeature[i].feature;
4069 if (tmp == what.substr(0, tmp.length())) {
4070 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4071 //tabularFeatures[i].feature.length()))
4072 action = tabularFeature[i].action;
4076 if (action == Tabular::LAST_ACTION)
4080 ltrim(what.substr(tabularFeature[i].feature.length()));
4081 tabularFeatures(cur, action, val);
4086 static void checkLongtableSpecial(Tabular::ltType & ltt,
4087 string const & special, bool & flag)
4089 if (special == "dl_above") {
4092 } else if (special == "dl_below") {
4093 ltt.bottomDL = flag;
4095 } else if (special == "empty") {
4104 bool InsetTabular::oneCellHasRotationState(bool rotated,
4105 row_type row_start, row_type row_end,
4106 col_type col_start, col_type col_end) const {
4108 for (row_type i = row_start; i <= row_end; ++i) {
4109 for (col_type j = col_start; j <= col_end; ++j) {
4110 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4119 void InsetTabular::tabularFeatures(Cursor & cur,
4120 Tabular::Feature feature, string const & value)
4122 BufferView & bv = cur.bv();
4123 col_type sel_col_start;
4124 col_type sel_col_end;
4125 row_type sel_row_start;
4126 row_type sel_row_end;
4127 bool setLines = false;
4128 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4129 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4133 case Tabular::M_ALIGN_LEFT:
4134 case Tabular::ALIGN_LEFT:
4135 setAlign = LYX_ALIGN_LEFT;
4138 case Tabular::M_ALIGN_RIGHT:
4139 case Tabular::ALIGN_RIGHT:
4140 setAlign = LYX_ALIGN_RIGHT;
4143 case Tabular::M_ALIGN_CENTER:
4144 case Tabular::ALIGN_CENTER:
4145 setAlign = LYX_ALIGN_CENTER;
4148 case Tabular::ALIGN_BLOCK:
4149 setAlign = LYX_ALIGN_BLOCK;
4152 case Tabular::M_VALIGN_TOP:
4153 case Tabular::VALIGN_TOP:
4154 setVAlign = Tabular::LYX_VALIGN_TOP;
4157 case Tabular::M_VALIGN_BOTTOM:
4158 case Tabular::VALIGN_BOTTOM:
4159 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4162 case Tabular::M_VALIGN_MIDDLE:
4163 case Tabular::VALIGN_MIDDLE:
4164 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4171 cur.recordUndoInset(ATOMIC_UNDO);
4173 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4174 row_type const row = tabular.cellRow(cur.idx());
4175 col_type const column = tabular.cellColumn(cur.idx());
4177 Tabular::ltType ltt;
4181 case Tabular::SET_PWIDTH: {
4182 Length const len(value);
4183 tabular.setColumnPWidth(cur, cur.idx(), len);
4185 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4186 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4190 case Tabular::SET_MPWIDTH:
4191 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4194 case Tabular::SET_SPECIAL_COLUMN:
4195 case Tabular::SET_SPECIAL_MULTI:
4196 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4199 case Tabular::APPEND_ROW:
4200 // append the row into the tabular
4201 tabular.appendRow(bv.buffer().params(), cur.idx());
4204 case Tabular::APPEND_COLUMN:
4205 // append the column into the tabular
4206 tabular.appendColumn(bv.buffer().params(), cur.idx());
4207 cur.idx() = tabular.cellIndex(row, column);
4210 case Tabular::DELETE_ROW:
4211 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4212 tabular.deleteRow(sel_row_start);
4213 if (sel_row_start >= tabular.rowCount())
4215 cur.idx() = tabular.cellIndex(sel_row_start, column);
4218 cur.selection() = false;
4221 case Tabular::DELETE_COLUMN:
4222 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4223 tabular.deleteColumn(sel_col_start);
4224 if (sel_col_start >= tabular.columnCount())
4226 cur.idx() = tabular.cellIndex(row, sel_col_start);
4229 cur.selection() = false;
4232 case Tabular::COPY_ROW:
4233 tabular.copyRow(bv.buffer().params(), row);
4236 case Tabular::COPY_COLUMN:
4237 tabular.copyColumn(bv.buffer().params(), column);
4238 cur.idx() = tabular.cellIndex(row, column);
4241 case Tabular::M_TOGGLE_LINE_TOP:
4243 case Tabular::TOGGLE_LINE_TOP: {
4244 bool lineSet = !tabular.topLine(cur.idx(), flag);
4245 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4246 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4248 tabular.cellIndex(i, j),
4253 case Tabular::M_TOGGLE_LINE_BOTTOM:
4255 case Tabular::TOGGLE_LINE_BOTTOM: {
4256 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4257 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4258 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4259 tabular.setBottomLine(
4260 tabular.cellIndex(i, j),
4266 case Tabular::M_TOGGLE_LINE_LEFT:
4268 case Tabular::TOGGLE_LINE_LEFT: {
4269 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4270 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4271 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4272 tabular.setLeftLine(
4273 tabular.cellIndex(i,j),
4279 case Tabular::M_TOGGLE_LINE_RIGHT:
4281 case Tabular::TOGGLE_LINE_RIGHT: {
4282 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4283 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4284 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4285 tabular.setRightLine(
4286 tabular.cellIndex(i,j),
4292 case Tabular::M_ALIGN_LEFT:
4293 case Tabular::M_ALIGN_RIGHT:
4294 case Tabular::M_ALIGN_CENTER:
4296 case Tabular::ALIGN_LEFT:
4297 case Tabular::ALIGN_RIGHT:
4298 case Tabular::ALIGN_CENTER:
4299 case Tabular::ALIGN_BLOCK:
4300 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4301 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4302 tabular.setAlignment(
4303 tabular.cellIndex(i, j),
4308 case Tabular::M_VALIGN_TOP:
4309 case Tabular::M_VALIGN_BOTTOM:
4310 case Tabular::M_VALIGN_MIDDLE:
4312 case Tabular::VALIGN_TOP:
4313 case Tabular::VALIGN_BOTTOM:
4314 case Tabular::VALIGN_MIDDLE:
4315 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4316 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4317 tabular.setVAlignment(
4318 tabular.cellIndex(i, j),
4322 case Tabular::MULTICOLUMN: {
4323 if (sel_row_start != sel_row_end) {
4324 // FIXME: Need I say it ? This is horrible.
4326 Alert::error(_("Error setting multicolumn"),
4327 _("You cannot set multicolumn vertically."));
4330 if (!cur.selection()) {
4331 // just multicol for one single cell
4332 // check whether we are completely in a multicol
4333 if (tabular.isMultiColumn(cur.idx()))
4334 tabular.unsetMultiColumn(cur.idx());
4336 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4339 // we have a selection so this means we just add all this
4340 // cells to form a multicolumn cell
4341 idx_type const s_start = cur.selBegin().idx();
4342 idx_type const s_end = cur.selEnd().idx();
4343 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4344 cur.idx() = s_start;
4347 cur.selection() = false;
4351 case Tabular::SET_ALL_LINES:
4353 case Tabular::UNSET_ALL_LINES:
4354 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4355 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4356 tabular.setAllLines(
4357 tabular.cellIndex(i,j), setLines);
4360 case Tabular::SET_LONGTABULAR:
4361 tabular.setLongTabular(true);
4364 case Tabular::UNSET_LONGTABULAR:
4365 tabular.setLongTabular(false);
4368 case Tabular::SET_ROTATE_TABULAR:
4369 tabular.setRotateTabular(true);
4372 case Tabular::UNSET_ROTATE_TABULAR:
4373 tabular.setRotateTabular(false);
4376 case Tabular::TOGGLE_ROTATE_TABULAR:
4377 tabular.setRotateTabular(!tabular.getRotateTabular());
4380 case Tabular::SET_ROTATE_CELL:
4381 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4382 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4383 tabular.setRotateCell(
4384 tabular.cellIndex(i, j), true);
4387 case Tabular::UNSET_ROTATE_CELL:
4388 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4389 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4390 tabular.setRotateCell(
4391 tabular.cellIndex(i, j), false);
4394 case Tabular::TOGGLE_ROTATE_CELL:
4396 bool oneNotRotated = oneCellHasRotationState(false,
4397 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4399 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4400 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4401 tabular.setRotateCell(tabular.cellIndex(i, j),
4406 case Tabular::SET_USEBOX: {
4407 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4408 if (val == tabular.getUsebox(cur.idx()))
4409 val = Tabular::BOX_NONE;
4410 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4411 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4412 tabular.setUsebox(tabular.cellIndex(i, j), val);
4416 case Tabular::UNSET_LTFIRSTHEAD:
4418 case Tabular::SET_LTFIRSTHEAD:
4419 tabular.getRowOfLTFirstHead(row, ltt);
4420 checkLongtableSpecial(ltt, value, flag);
4421 tabular.setLTHead(row, flag, ltt, true);
4424 case Tabular::UNSET_LTHEAD:
4426 case Tabular::SET_LTHEAD:
4427 tabular.getRowOfLTHead(row, ltt);
4428 checkLongtableSpecial(ltt, value, flag);
4429 tabular.setLTHead(row, flag, ltt, false);
4432 case Tabular::UNSET_LTFOOT:
4434 case Tabular::SET_LTFOOT:
4435 tabular.getRowOfLTFoot(row, ltt);
4436 checkLongtableSpecial(ltt, value, flag);
4437 tabular.setLTFoot(row, flag, ltt, false);
4440 case Tabular::UNSET_LTLASTFOOT:
4442 case Tabular::SET_LTLASTFOOT:
4443 tabular.getRowOfLTLastFoot(row, ltt);
4444 checkLongtableSpecial(ltt, value, flag);
4445 tabular.setLTFoot(row, flag, ltt, true);
4448 case Tabular::SET_LTNEWPAGE:
4449 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4452 case Tabular::SET_BOOKTABS:
4453 tabular.setBookTabs(true);
4456 case Tabular::UNSET_BOOKTABS:
4457 tabular.setBookTabs(false);
4460 case Tabular::SET_TOP_SPACE: {
4462 if (value == "default")
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4464 tabular.row_info[i].top_space_default = true;
4465 else if (isValidLength(value, &len))
4466 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4467 tabular.row_info[i].top_space_default = false;
4468 tabular.row_info[i].top_space = len;
4471 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4472 tabular.row_info[i].top_space_default = false;
4473 tabular.row_info[i].top_space = len;
4478 case Tabular::SET_BOTTOM_SPACE: {
4480 if (value == "default")
4481 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4482 tabular.row_info[i].bottom_space_default = true;
4483 else if (isValidLength(value, &len))
4484 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4485 tabular.row_info[i].bottom_space_default = false;
4486 tabular.row_info[i].bottom_space = len;
4489 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4490 tabular.row_info[i].bottom_space_default = false;
4491 tabular.row_info[i].bottom_space = len;
4496 case Tabular::SET_INTERLINE_SPACE: {
4498 if (value == "default")
4499 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4500 tabular.row_info[i].interline_space_default = true;
4501 else if (isValidLength(value, &len))
4502 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4503 tabular.row_info[i].interline_space_default = false;
4504 tabular.row_info[i].interline_space = len;
4507 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4508 tabular.row_info[i].interline_space_default = false;
4509 tabular.row_info[i].interline_space = len;
4514 // dummy stuff just to avoid warnings
4515 case Tabular::LAST_ACTION:
4521 bool InsetTabular::showInsetDialog(BufferView * bv) const
4523 InsetTabularMailer(*this).showDialog(bv);
4528 void InsetTabular::openLayoutDialog(BufferView * bv) const
4530 InsetTabularMailer(*this).showDialog(bv);
4534 bool InsetTabular::copySelection(Cursor & cur)
4536 if (!cur.selection())
4541 getSelection(cur, rs, re, cs, ce);
4543 paste_tabular.reset(new Tabular(tabular));
4545 for (row_type i = 0; i < rs; ++i)
4546 paste_tabular->deleteRow(0);
4548 row_type const rows = re - rs + 1;
4549 while (paste_tabular->rowCount() > rows)
4550 paste_tabular->deleteRow(rows);
4552 paste_tabular->setTopLine(0, true, true);
4553 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4556 for (col_type i = 0; i < cs; ++i)
4557 paste_tabular->deleteColumn(0);
4559 col_type const columns = ce - cs + 1;
4560 while (paste_tabular->columnCount() > columns)
4561 paste_tabular->deleteColumn(columns);
4563 paste_tabular->setLeftLine(0, true, true);
4564 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4567 odocstringstream os;
4568 OutputParams const runparams(0);
4569 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4570 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4571 cap::copySelection(cur, os.str());
4573 // mark tabular stack dirty
4574 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4575 // when we (hopefully) have a one-for-all paste mechanism.
4576 // This must be called after cap::copySelection.
4577 dirtyTabularStack(true);
4583 bool InsetTabular::pasteClipboard(Cursor & cur)
4587 col_type const actcol = tabular.cellColumn(cur.idx());
4588 row_type const actrow = tabular.cellRow(cur.idx());
4589 for (row_type r1 = 0, r2 = actrow;
4590 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4592 for (col_type c1 = 0, c2 = actcol;
4593 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4595 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4596 tabular.isPartOfMultiColumn(r2, c2))
4598 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4602 if (tabular.isPartOfMultiColumn(r2, c2)) {
4606 shared_ptr<InsetText> inset(
4607 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4608 tabular.setCellInset(r2, c2, inset);
4609 // FIXME: change tracking (MG)
4610 inset->setChange(Change(cur.buffer().params().trackChanges ?
4611 Change::INSERTED : Change::UNCHANGED));
4619 void InsetTabular::cutSelection(Cursor & cur)
4621 if (!cur.selection())
4626 getSelection(cur, rs, re, cs, ce);
4627 for (row_type i = rs; i <= re; ++i) {
4628 for (col_type j = cs; j <= ce; ++j) {
4629 shared_ptr<InsetText> t
4630 = cell(tabular.cellIndex(i, j));
4631 if (cur.buffer().params().trackChanges)
4632 // FIXME: Change tracking (MG)
4633 t->setChange(Change(Change::DELETED));
4639 // cursor position might be invalid now
4640 if (cur.pit() > cur.lastpit())
4641 cur.pit() = cur.lastpit();
4642 if (cur.pos() > cur.lastpos())
4643 cur.pos() = cur.lastpos();
4644 cur.clearSelection();
4648 bool InsetTabular::isRightToLeft(Cursor & cur) const
4650 BOOST_ASSERT(cur.depth() > 1);
4651 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4652 pos_type const parentpos = cur[cur.depth() - 2].pos();
4653 return parentpar.getFontSettings(cur.bv().buffer().params(),
4654 parentpos).language()->rightToLeft();
4658 void InsetTabular::getSelection(Cursor & cur,
4659 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4661 CursorSlice const & beg = cur.selBegin();
4662 CursorSlice const & end = cur.selEnd();
4663 cs = tabular.cellColumn(beg.idx());
4664 ce = tabular.cellColumn(end.idx());
4667 cs = tabular.cellColumn(end.idx());
4669 ce = tabular.cellRightColumn(end.idx());
4672 rs = tabular.cellRow(beg.idx());
4673 re = tabular.cellRow(end.idx());
4679 Text * InsetTabular::getText(int idx) const
4681 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4685 void InsetTabular::setChange(Change const & change)
4687 for (idx_type idx = 0; idx < nargs(); ++idx)
4688 cell(idx)->setChange(change);
4692 void InsetTabular::acceptChanges(BufferParams const & bparams)
4694 for (idx_type idx = 0; idx < nargs(); ++idx)
4695 cell(idx)->acceptChanges(bparams);
4699 void InsetTabular::rejectChanges(BufferParams const & bparams)
4701 for (idx_type idx = 0; idx < nargs(); ++idx)
4702 cell(idx)->rejectChanges(bparams);
4706 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4708 return tabular.getPWidth(cell).zero();
4712 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4715 if (buf.length() <= 0)
4718 Buffer const & buffer = bv.buffer();
4722 col_type maxCols = 1;
4723 docstring::size_type const len = buf.length();
4724 docstring::size_type p = 0;
4727 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4735 maxCols = max(cols, maxCols);
4741 maxCols = max(cols, maxCols);
4747 paste_tabular.reset(
4748 new Tabular(buffer.params(), rows, maxCols));
4749 loctab = paste_tabular.get();
4751 dirtyTabularStack(true);
4754 cell = bv.cursor().idx();
4755 ocol = tabular.cellColumn(cell);
4756 row = tabular.cellRow(cell);
4759 docstring::size_type op = 0;
4760 idx_type const cells = loctab->cellCount();
4763 rows = loctab->rowCount();
4764 col_type const columns = loctab->columnCount();
4766 while (cell < cells && p < len && row < rows &&
4767 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4773 // we can only set this if we are not too far right
4774 if (cols < columns) {
4775 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4776 Font const font = bv.textMetrics(&inset->text_).
4777 getDisplayFont(0, 0);
4778 inset->setText(buf.substr(op, p - op), font,
4779 buffer.params().trackChanges);
4785 // we can only set this if we are not too far right
4786 if (cols < columns) {
4787 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4788 Font const font = bv.textMetrics(&inset->text_).
4789 getDisplayFont(0, 0);
4790 inset->setText(buf.substr(op, p - op), font,
4791 buffer.params().trackChanges);
4796 cell = loctab->cellIndex(row, cols);
4802 // check for the last cell if there is no trailing '\n'
4803 if (cell < cells && op < len) {
4804 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4805 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4806 inset->setText(buf.substr(op, len - op), font,
4807 buffer.params().trackChanges);
4813 void InsetTabular::addPreview(PreviewLoader & loader) const
4815 row_type const rows = tabular.rowCount();
4816 col_type const columns = tabular.columnCount();
4817 for (row_type i = 0; i < rows; ++i) {
4818 for (col_type j = 0; j < columns; ++j)
4819 tabular.getCellInset(i, j)->addPreview(loader);
4824 bool InsetTabular::tablemode(Cursor & cur) const
4826 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4832 string const InsetTabularMailer::name_("tabular");
4834 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4835 : inset_(const_cast<InsetTabular &>(inset))
4839 string const InsetTabularMailer::inset2string(Buffer const &) const
4841 return params2string(inset_);
4845 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4847 istringstream data(in);
4849 lex.setStream(data);
4856 if (!lex || token != name_)
4857 return print_mailer_error("InsetTabularMailer", in, 1,
4860 // This is part of the inset proper that is usually swallowed
4861 // by Buffer::readInset
4863 if (!lex || token != "Tabular")
4864 return print_mailer_error("InsetTabularMailer", in, 2,
4867 Buffer const & buffer = inset.buffer();
4868 inset.read(buffer, lex);
4872 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4875 data << name_ << ' ';
4876 inset.write(inset.buffer(), data);
4877 data << "\\end_inset\n";