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>
69 using std::istringstream;
72 using std::ostringstream;
77 #ifndef CXX_GLOBAL_CSTD
81 using boost::shared_ptr;
82 using boost::dynamic_pointer_cast;
86 using support::prefixIs;
87 using support::FileName;
90 using support::suffixIs;
92 using cap::dirtyTabularStack;
93 using cap::tabularStackDirty;
95 using graphics::PreviewLoader;
97 using frontend::Painter;
98 using frontend::Clipboard;
100 namespace Alert = frontend::Alert;
105 int const ADD_TO_HEIGHT = 2;
106 int const ADD_TO_TABULAR_WIDTH = 2;
107 int const default_line_space = 10;
108 int const WIDTH_OF_LINE = 5;
112 boost::scoped_ptr<Tabular> paste_tabular;
115 struct TabularFeature {
116 Tabular::Feature action;
121 TabularFeature tabularFeature[] =
123 { Tabular::APPEND_ROW, "append-row" },
124 { Tabular::APPEND_COLUMN, "append-column" },
125 { Tabular::DELETE_ROW, "delete-row" },
126 { Tabular::DELETE_COLUMN, "delete-column" },
127 { Tabular::COPY_ROW, "copy-row" },
128 { Tabular::COPY_COLUMN, "copy-column" },
129 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
130 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
131 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
132 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
133 { Tabular::ALIGN_LEFT, "align-left" },
134 { Tabular::ALIGN_RIGHT, "align-right" },
135 { Tabular::ALIGN_CENTER, "align-center" },
136 { Tabular::ALIGN_BLOCK, "align-block" },
137 { Tabular::VALIGN_TOP, "valign-top" },
138 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
139 { Tabular::VALIGN_MIDDLE, "valign-middle" },
140 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
141 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
142 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
143 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
144 { Tabular::M_ALIGN_LEFT, "m-align-left" },
145 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
146 { Tabular::M_ALIGN_CENTER, "m-align-center" },
147 { Tabular::M_VALIGN_TOP, "m-valign-top" },
148 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
149 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
150 { Tabular::MULTICOLUMN, "multicolumn" },
151 { Tabular::SET_ALL_LINES, "set-all-lines" },
152 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
153 { Tabular::SET_LONGTABULAR, "set-longtabular" },
154 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
155 { Tabular::SET_PWIDTH, "set-pwidth" },
156 { Tabular::SET_MPWIDTH, "set-mpwidth" },
157 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
158 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
159 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
160 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
161 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
162 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
163 { Tabular::SET_USEBOX, "set-usebox" },
164 { Tabular::SET_LTHEAD, "set-lthead" },
165 { Tabular::UNSET_LTHEAD, "unset-lthead" },
166 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
167 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
168 { Tabular::SET_LTFOOT, "set-ltfoot" },
169 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
170 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
171 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
172 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
173 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
174 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
175 { Tabular::SET_BOOKTABS, "set-booktabs" },
176 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
177 { Tabular::SET_TOP_SPACE, "set-top-space" },
178 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
179 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
180 { Tabular::LAST_ACTION, "" }
184 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
186 FeatureEqual(Tabular::Feature feature)
187 : feature_(feature) {}
188 bool operator()(TabularFeature const & tf) const {
189 return tf.action == feature_;
192 Tabular::Feature feature_;
197 string const write_attribute(string const & name, T const & t)
199 string const s = tostr(t);
200 return s.empty() ? s : " " + name + "=\"" + s + "\"";
204 string const write_attribute(string const & name, string const & t)
206 return t.empty() ? t : " " + name + "=\"" + t + "\"";
211 string const write_attribute(string const & name, docstring const & t)
213 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
218 string const write_attribute(string const & name, bool const & b)
220 // we write only true attribute values so we remove a bit of the
221 // file format bloat for tabulars.
222 return b ? write_attribute(name, convert<string>(b)) : string();
227 string const write_attribute(string const & name, int const & i)
229 // we write only true attribute values so we remove a bit of the
230 // file format bloat for tabulars.
231 return i ? write_attribute(name, convert<string>(i)) : string();
236 string const write_attribute(string const & name, Tabular::idx_type const & i)
238 // we write only true attribute values so we remove a bit of the
239 // file format bloat for tabulars.
240 return i ? write_attribute(name, convert<string>(i)) : string();
245 string const write_attribute(string const & name, Length const & value)
247 // we write only the value if we really have one same reson as above.
248 return value.zero() ? string() : write_attribute(name, value.asString());
252 string const tostr(LyXAlignment const & num)
257 case LYX_ALIGN_BLOCK:
261 case LYX_ALIGN_CENTER:
263 case LYX_ALIGN_RIGHT:
265 case LYX_ALIGN_LAYOUT:
267 case LYX_ALIGN_SPECIAL:
274 string const tostr(Tabular::VAlignment const & num)
277 case Tabular::LYX_VALIGN_TOP:
279 case Tabular::LYX_VALIGN_MIDDLE:
281 case Tabular::LYX_VALIGN_BOTTOM:
288 string const tostr(Tabular::BoxType const & num)
291 case Tabular::BOX_NONE:
293 case Tabular::BOX_PARBOX:
295 case Tabular::BOX_MINIPAGE:
302 // I would have liked a fromstr template a lot better. (Lgb)
303 bool string2type(string const str, LyXAlignment & num)
306 num = LYX_ALIGN_NONE;
307 else if (str == "block")
308 num = LYX_ALIGN_BLOCK;
309 else if (str == "left")
310 num = LYX_ALIGN_LEFT;
311 else if (str == "center")
312 num = LYX_ALIGN_CENTER;
313 else if (str == "right")
314 num = LYX_ALIGN_RIGHT;
321 bool string2type(string const str, Tabular::VAlignment & num)
324 num = Tabular::LYX_VALIGN_TOP;
325 else if (str == "middle" )
326 num = Tabular::LYX_VALIGN_MIDDLE;
327 else if (str == "bottom")
328 num = Tabular::LYX_VALIGN_BOTTOM;
335 bool string2type(string const str, Tabular::BoxType & num)
338 num = Tabular::BOX_NONE;
339 else if (str == "parbox")
340 num = Tabular::BOX_PARBOX;
341 else if (str == "minipage")
342 num = Tabular::BOX_MINIPAGE;
349 bool string2type(string const str, bool & num)
353 else if (str == "false")
361 bool getTokenValue(string const & str, char const * token, string & ret)
364 size_t token_length = strlen(token);
365 string::size_type pos = str.find(token);
367 if (pos == string::npos || pos + token_length + 1 >= str.length()
368 || str[pos + token_length] != '=')
370 pos += token_length + 1;
372 if (ch != '"' && ch != '\'') { // only read till next space
376 while (pos < str.length() - 1 && str[++pos] != ch)
383 bool getTokenValue(string const & str, char const * token, docstring & ret)
386 bool const success = getTokenValue(str, token, tmp);
387 ret = from_utf8(tmp);
392 bool getTokenValue(string const & str, char const * token, int & num)
396 if (!getTokenValue(str, token, tmp))
398 num = convert<int>(tmp);
403 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
406 return getTokenValue(str, token, tmp) && string2type(tmp, num);
410 bool getTokenValue(string const & str, char const * token,
411 Tabular::VAlignment & num)
414 return getTokenValue(str, token, tmp) && string2type(tmp, num);
418 bool getTokenValue(string const & str, char const * token,
419 Tabular::BoxType & num)
422 return getTokenValue(str, token, tmp) && string2type(tmp, num);
426 bool getTokenValue(string const & str, char const * token, bool & flag)
428 // set the flag always to false as this should be the default for bools
429 // not in the file-format.
432 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
436 bool getTokenValue(string const & str, char const * token, Length & len)
438 // set the length to be zero() as default as this it should be if not
439 // in the file format.
442 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
446 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
451 if (!getTokenValue(str, token, tmp))
453 if (tmp == "default") {
457 return isValidLength(tmp, &len);
461 void l_getline(istream & is, string & str)
464 while (str.empty()) {
466 if (!str.empty() && str[str.length() - 1] == '\r')
467 str.erase(str.length() - 1);
474 string const featureAsString(Tabular::Feature feature)
476 TabularFeature * end = tabularFeature +
477 sizeof(tabularFeature) / sizeof(TabularFeature);
478 TabularFeature * it = std::find_if(tabularFeature, end,
479 FeatureEqual(feature));
480 return (it == end) ? string() : it->feature;
485 /////////////////////////////////////////////////////////////////////
489 /////////////////////////////////////////////////////////////////////
492 Tabular::cellstruct::cellstruct(BufferParams const & bp)
495 multicolumn(Tabular::CELL_NORMAL),
496 alignment(LYX_ALIGN_CENTER),
497 valignment(LYX_VALIGN_TOP),
504 inset(new InsetText(bp))
508 Tabular::cellstruct::cellstruct(cellstruct const & cs)
511 multicolumn(cs.multicolumn),
512 alignment(cs.alignment),
513 valignment(cs.valignment),
514 top_line(cs.top_line),
515 bottom_line(cs.bottom_line),
516 left_line(cs.left_line),
517 right_line(cs.right_line),
520 align_special(cs.align_special),
522 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
526 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
533 void Tabular::cellstruct::swap(cellstruct & rhs)
535 std::swap(cellno, rhs.cellno);
536 std::swap(width, rhs.width);
537 std::swap(multicolumn, rhs.multicolumn);
538 std::swap(alignment, rhs.alignment);
539 std::swap(valignment, rhs.valignment);
540 std::swap(top_line, rhs.top_line);
541 std::swap(bottom_line, rhs.bottom_line);
542 std::swap(left_line, rhs.left_line);
543 std::swap(right_line, rhs.right_line);
544 std::swap(usebox, rhs.usebox);
545 std::swap(rotate, rhs.rotate);
546 std::swap(align_special, rhs.align_special);
547 p_width.swap(rhs.p_width);
548 inset.swap(rhs.inset);
552 Tabular::rowstruct::rowstruct()
557 top_space_default(false),
558 bottom_space_default(false),
559 interline_space_default(false),
568 Tabular::columnstruct::columnstruct()
569 : alignment(LYX_ALIGN_CENTER),
570 valignment(LYX_VALIGN_TOP),
578 Tabular::ltType::ltType()
591 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
592 col_type columns_arg)
594 init(bp, rows_arg, columns_arg);
598 // activates all lines and sets all widths to 0
599 void Tabular::init(BufferParams const & bp, row_type rows_arg,
600 col_type columns_arg)
602 row_info = row_vector(rows_arg);
603 column_info = column_vector(columns_arg);
604 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
605 row_info.reserve(10);
606 column_info.reserve(10);
607 cell_info.reserve(100);
609 for (row_type i = 0; i < rows_arg; ++i)
610 cell_info[i].back().right_line = true;
611 row_info.back().bottom_line = true;
612 row_info.front().bottom_line = true;
613 column_info.back().right_line = true;
614 is_long_tabular = false;
616 use_booktabs = false;
620 void Tabular::fixCellNums()
623 for (row_type i = 0; i < rowCount(); ++i) {
624 for (col_type j = 0; j < columnCount(); ++j) {
625 // When debugging it can be nice to set
627 cell_info[i][j].inset->setDrawFrame(false);
628 cell_info[i][j].cellno = cellno++;
630 cell_info[i].back().right_line = true;
637 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
639 row_type const row = cellRow(cell);
641 row_vector::iterator rit = row_info.begin() + row;
642 row_info.insert(rit, rowstruct());
643 // now set the values of the row before
644 row_info[row] = row_info[row + 1];
646 row_type const nrows = rowCount();
647 col_type const ncols = columnCount();
649 cell_vvector old(nrows - 1);
650 for (row_type i = 0; i < nrows - 1; ++i)
651 swap(cell_info[i], old[i]);
653 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
655 for (row_type i = 0; i <= row; ++i)
656 swap(cell_info[i], old[i]);
657 for (row_type i = row + 2; i < nrows; ++i)
658 swap(cell_info[i], old[i - 1]);
661 for (col_type j = 0; j < ncols; ++j)
662 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
668 void Tabular::deleteRow(row_type const row)
670 // Not allowed to delete last row
674 row_info.erase(row_info.begin() + row);
675 cell_info.erase(cell_info.begin() + row);
680 void Tabular::copyRow(BufferParams const & bp, row_type const row)
682 row_info.insert(row_info.begin() + row, row_info[row]);
683 cell_info.insert(cell_info.begin() + row, cell_info[row]);
686 for (col_type j = 0; j < columnCount(); ++j)
687 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
693 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
695 col_type const column = cellColumn(cell);
696 col_type const ncols = columnCount();
697 column_vector::iterator cit = column_info.begin() + column + 1;
698 column_info.insert(cit, columnstruct());
699 // set the column values of the column before
700 column_info[column + 1] = column_info[column];
702 for (row_type i = 0; i < rowCount(); ++i) {
703 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
705 // care about multicolumns
706 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
707 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
709 if (column + 2 >= ncols
710 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
711 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
714 for (row_type i = 0; i < rowCount(); ++i) {
715 cell_info[i][column + 1].inset->clear();
717 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
723 void Tabular::deleteColumn(col_type const column)
725 // Not allowed to delete last column
726 if (columnCount() == 1)
729 column_info.erase(column_info.begin() + column);
730 for (row_type i = 0; i < rowCount(); ++i) {
731 // Care about multicolumn cells
732 if (column + 1 < columnCount() &&
733 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
734 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
735 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
737 cell_info[i].erase(cell_info[i].begin() + column);
743 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
745 column_info.insert(column_info.begin() + column, column_info[column]);
747 for (row_type i = 0; i < rowCount(); ++i)
748 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
751 for (row_type i = 0; i < rowCount(); ++i)
752 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
757 void Tabular::updateIndexes()
760 // Count only non-multicol cells plus begin multicol
761 // cells, ignore non-begin multicol cells:
762 for (row_type row = 0; row < rowCount(); ++row) {
763 for (col_type column = 0; column < columnCount(); ++column) {
764 if (cell_info[row][column].multicolumn
765 != Tabular::CELL_PART_OF_MULTICOLUMN)
767 BOOST_ASSERT(numberofcells != 0);
768 cell_info[row][column].cellno =
773 rowofcell.resize(numberofcells);
774 columnofcell.resize(numberofcells);
779 c < numberofcells && row < rowCount() && column < columnCount();) {
781 columnofcell[c] = column;
785 } while (column < columnCount() &&
786 cell_info[row][column].multicolumn
787 == Tabular::CELL_PART_OF_MULTICOLUMN);
789 if (column == columnCount()) {
795 for (row_type row = 0; row < rowCount(); ++row) {
796 for (col_type column = 0; column < columnCount(); ++column) {
797 if (isPartOfMultiColumn(row,column))
799 cell_info[row][column].inset->setAutoBreakRows(
800 !getPWidth(cellIndex(row, column)).zero());
806 Tabular::idx_type Tabular::cellCount() const
808 return numberofcells;
812 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
814 row_type const row = cellRow(cell);
816 for (col_type i = 0; i < columnCount(); ++i)
817 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
823 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
825 if (!wholerow && isMultiColumn(cell) &&
826 !(use_booktabs && cellRow(cell) == 0))
827 return cellinfo_of_cell(cell).top_line;
828 return row_info[cellRow(cell)].top_line;
832 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
834 if (!wholerow && isMultiColumn(cell) &&
835 !(use_booktabs && isLastRow(cell)))
836 return cellinfo_of_cell(cell).bottom_line;
837 return row_info[cellRow(cell)].bottom_line;
841 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
845 if (!wholecolumn && isMultiColumn(cell) &&
846 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
847 return cellinfo_of_cell(cell).left_line;
848 return column_info[cellColumn(cell)].left_line;
852 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
856 if (!wholecolumn && isMultiColumn(cell) &&
857 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
858 return cellinfo_of_cell(cell).right_line;
859 return column_info[cellRightColumn(cell)].right_line;
863 bool Tabular::topAlreadyDrawn(idx_type cell) const
865 row_type row = cellRow(cell);
866 if (row > 0 && !getAdditionalHeight(row)) {
867 col_type column = cellColumn(cell);
870 && cell_info[row][column].multicolumn
871 == Tabular::CELL_PART_OF_MULTICOLUMN)
873 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
874 return row_info[row].bottom_line;
876 return cell_info[row][column].bottom_line;
882 bool Tabular::leftAlreadyDrawn(idx_type cell) const
884 col_type column = cellColumn(cell);
886 row_type row = cellRow(cell);
888 (cell_info[row][column].multicolumn ==
889 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
890 if (getAdditionalWidth(cell_info[row][column].cellno))
892 return rightLine(cell_info[row][column].cellno);
898 bool Tabular::isLastRow(idx_type cell) const
900 return cellRow(cell) == rowCount() - 1;
904 int Tabular::getAdditionalHeight(row_type row) const
906 if (!row || row >= rowCount())
912 for (col_type column = 0; column < columnCount() && bottom; ++column) {
913 switch (cell_info[row - 1][column].multicolumn) {
914 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
915 bottom = cell_info[row - 1][column].bottom_line;
917 case Tabular::CELL_NORMAL:
918 bottom = row_info[row - 1].bottom_line;
921 for (col_type column = 0; column < columnCount() && top; ++column) {
922 switch (cell_info[row][column].multicolumn) {
923 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
924 top = cell_info[row][column].top_line;
926 case Tabular::CELL_NORMAL:
927 top = row_info[row].top_line;
930 int const interline_space = row_info[row - 1].interline_space_default ?
932 row_info[row - 1].interline_space.inPixels(width());
934 return interline_space + WIDTH_OF_LINE;
935 return interline_space;
939 int Tabular::getAdditionalWidth(idx_type cell) const
941 // internally already set in setCellWidth
942 // used to get it back in text.cpp
943 col_type const col = cellRightColumn(cell);
944 row_type const row = cellRow(cell);
945 if (col < columnCount() - 1 && rightLine(cell) &&
946 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
948 return WIDTH_OF_LINE;
954 // returns the maximum over all rows
955 int Tabular::columnWidth(idx_type cell) const
957 col_type const column1 = cellColumn(cell);
958 col_type const column2 = cellRightColumn(cell);
960 for (col_type i = column1; i <= column2; ++i)
961 result += column_info[i].width;
966 int Tabular::width() const
969 for (col_type i = 0; i < columnCount(); ++i)
970 width += column_info[i].width;
975 // returns true if a complete update is necessary, otherwise false
976 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
978 if (!isMultiColumn(cell))
981 row_type const row = cellRow(cell);
982 col_type const column1 = cellColumn(cell);
983 col_type const column2 = cellRightColumn(cell);
984 int const old_val = cell_info[row][column2].width;
986 // first set columns to 0 so we can calculate the right width
987 for (col_type i = column1; i <= column2; ++i) {
988 cell_info[row][i].width = 0;
990 // set the width to MAX_WIDTH until width > 0
991 int width = new_width + 2 * WIDTH_OF_LINE;
992 col_type i = column1;
993 for (; i < column2 && width > column_info[i].width; ++i) {
994 cell_info[row][i].width = column_info[i].width;
995 width -= column_info[i].width;
998 cell_info[row][i].width = width;
1000 if (old_val != cell_info[row][column2].width) {
1001 // in this case we have to recalculate all multicolumn cells which
1002 // have this column as one of theirs but not as last one
1003 calculate_width_of_column_NMC(i);
1004 recalculateMulticolumnsOfColumn(i);
1005 calculate_width_of_column(i);
1011 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1013 // the last column does not have to be recalculated because all
1014 // multicolumns will have here there last multicolumn cell which
1015 // always will have the whole rest of the width of the cell.
1016 if (columnCount() < 2 || column > (columnCount() - 2))
1018 for (row_type row = 0; row < rowCount(); ++row) {
1019 int mc = cell_info[row][column].multicolumn;
1020 int nmc = cell_info[row][column+1].multicolumn;
1021 // we only have to update multicolumns which do not have this
1022 // column as their last column!
1023 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1024 (mc == CELL_PART_OF_MULTICOLUMN &&
1025 nmc == CELL_PART_OF_MULTICOLUMN))
1027 idx_type const cellno = cell_info[row][column].cellno;
1028 setWidthOfMulticolCell(cellno,
1029 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1035 void Tabular::setCellWidth(idx_type cell, int new_width)
1037 row_type const row = cellRow(cell);
1038 col_type const column1 = cellColumn(cell);
1043 if (rightLine(cell_info[row][column1].cellno, true) &&
1044 column1 < columnCount() - 1 &&
1045 leftLine(cell_info[row][column1+1].cellno, true))
1047 add_width = WIDTH_OF_LINE;
1050 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1053 if (isMultiColumnReal(cell)) {
1054 tmp = setWidthOfMulticolCell(cell, new_width);
1056 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1057 cell_info[row][column1].width = width;
1058 tmp = calculate_width_of_column_NMC(column1);
1060 recalculateMulticolumnsOfColumn(column1);
1063 for (col_type i = 0; i < columnCount(); ++i)
1064 calculate_width_of_column(i);
1069 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1072 if (!isMultiColumn(cell) || onlycolumn)
1073 column_info[cellColumn(cell)].alignment = align;
1075 cellinfo_of_cell(cell).alignment = align;
1079 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1082 if (!isMultiColumn(cell) || onlycolumn)
1083 column_info[cellColumn(cell)].valignment = align;
1085 cellinfo_of_cell(cell).valignment = align;
1092 * Allow line and paragraph breaks for fixed width cells or disallow them,
1093 * merge cell paragraphs and reset layout to standard for variable width
1096 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1098 inset->setAutoBreakRows(fixedWidth);
1102 // merge all paragraphs to one
1103 BufferParams const & bp = cur.bv().buffer().params();
1104 while (inset->paragraphs().size() > 1)
1105 mergeParagraph(bp, inset->paragraphs(), 0);
1109 // undo information has already been recorded
1110 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1111 bp.getTextClass().defaultLayoutName());
1118 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1119 Length const & width)
1121 col_type const j = cellColumn(cell);
1123 column_info[j].p_width = width;
1124 for (row_type i = 0; i < rowCount(); ++i) {
1125 idx_type const cell = cellIndex(i, j);
1126 // because of multicolumns
1127 toggleFixedWidth(cur, getCellInset(cell).get(),
1128 !getPWidth(cell).zero());
1130 // cur paragraph can become invalid after paragraphs were merged
1131 if (cur.pit() > cur.lastpit())
1132 cur.pit() = cur.lastpit();
1133 // cur position can become invalid after newlines were removed
1134 if (cur.pos() > cur.lastpos())
1135 cur.pos() = cur.lastpos();
1139 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1140 Length const & width)
1142 if (!isMultiColumn(cell))
1145 cellinfo_of_cell(cell).p_width = width;
1146 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1147 // cur paragraph can become invalid after paragraphs were merged
1148 if (cur.pit() > cur.lastpit())
1149 cur.pit() = cur.lastpit();
1150 // cur position can become invalid after newlines were removed
1151 if (cur.pos() > cur.lastpos())
1152 cur.pos() = cur.lastpos();
1157 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1158 Tabular::Feature what)
1160 if (what == SET_SPECIAL_MULTI)
1161 cellinfo_of_cell(cell).align_special = special;
1163 column_info[cellColumn(cell)].align_special = special;
1167 void Tabular::setAllLines(idx_type cell, bool line)
1169 setTopLine(cell, line);
1170 setBottomLine(cell, line);
1171 setRightLine(cell, line);
1172 setLeftLine(cell, line);
1176 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1178 row_type const row = cellRow(cell);
1179 if (wholerow || !isMultiColumn(cell))
1180 row_info[row].top_line = line;
1182 cellinfo_of_cell(cell).top_line = line;
1186 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1188 if (wholerow || !isMultiColumn(cell))
1189 row_info[cellRow(cell)].bottom_line = line;
1191 cellinfo_of_cell(cell).bottom_line = line;
1195 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1197 if (wholecolumn || !isMultiColumn(cell))
1198 column_info[cellColumn(cell)].left_line = line;
1200 cellinfo_of_cell(cell).left_line = line;
1204 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1206 if (wholecolumn || !isMultiColumn(cell))
1207 column_info[cellRightColumn(cell)].right_line = line;
1209 cellinfo_of_cell(cell).right_line = line;
1213 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1215 if (!onlycolumn && isMultiColumn(cell))
1216 return cellinfo_of_cell(cell).alignment;
1217 return column_info[cellColumn(cell)].alignment;
1222 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1224 if (!onlycolumn && isMultiColumn(cell))
1225 return cellinfo_of_cell(cell).valignment;
1226 return column_info[cellColumn(cell)].valignment;
1230 Length const Tabular::getPWidth(idx_type cell) const
1232 if (isMultiColumn(cell))
1233 return cellinfo_of_cell(cell).p_width;
1234 return column_info[cellColumn(cell)].p_width;
1238 Length const Tabular::getColumnPWidth(idx_type cell) const
1240 return column_info[cellColumn(cell)].p_width;
1244 Length const Tabular::getMColumnPWidth(idx_type cell) const
1246 if (isMultiColumn(cell))
1247 return cellinfo_of_cell(cell).p_width;
1252 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1254 if (what == SET_SPECIAL_MULTI)
1255 return cellinfo_of_cell(cell).align_special;
1256 return column_info[cellColumn(cell)].align_special;
1260 int Tabular::cellWidth(idx_type cell) const
1262 row_type const row = cellRow(cell);
1263 col_type const column1 = cellColumn(cell);
1264 col_type const column2 = cellRightColumn(cell);
1266 for (col_type i = column1; i <= column2; ++i)
1267 result += cell_info[row][i].width;
1272 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1276 switch (getAlignment(cell)) {
1277 case LYX_ALIGN_CENTER:
1278 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1280 case LYX_ALIGN_RIGHT:
1281 x += columnWidth(cell) - cellWidth(cell);
1282 // + getAdditionalWidth(cell);
1285 // LYX_ALIGN_LEFT: nothing :-)
1289 // the LaTeX Way :-(
1295 bool Tabular::isFirstCellInRow(idx_type cell) const
1297 return cellColumn(cell) == 0;
1301 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1303 if (row > rowCount() - 1)
1304 row = rowCount() - 1;
1305 return cell_info[row][0].cellno;
1309 bool Tabular::isLastCellInRow(idx_type cell) const
1311 return cellRightColumn(cell) == columnCount() - 1;
1315 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1317 if (row > rowCount() - 1)
1318 row = rowCount() - 1;
1319 return cell_info[row][columnCount() - 1].cellno;
1323 void Tabular::calculate_width_of_column(col_type column)
1326 for (row_type i = 0; i < rowCount(); ++i)
1327 maximum = max(cell_info[i][column].width, maximum);
1328 column_info[column].width = maximum;
1333 // Calculate the columns regarding ONLY the normal cells and if this
1334 // column is inside a multicolumn cell then use it only if its the last
1335 // column of this multicolumn cell as this gives an added width to the
1336 // column, all the rest should be adapted!
1338 bool Tabular::calculate_width_of_column_NMC(col_type column)
1340 int const old_column_width = column_info[column].width;
1342 for (row_type i = 0; i < rowCount(); ++i) {
1343 idx_type cell = cellIndex(i, column);
1344 bool ismulti = isMultiColumnReal(cell);
1345 if ((!ismulti || column == cellRightColumn(cell)) &&
1346 cell_info[i][column].width > max)
1348 max = cell_info[i][column].width;
1351 column_info[column].width = max;
1352 return column_info[column].width != old_column_width;
1356 Tabular::row_type Tabular::cellRow(idx_type cell) const
1358 if (cell >= cellCount())
1359 return rowCount() - 1;
1362 return rowofcell[cell];
1366 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1368 if (cell >= cellCount())
1369 return columnCount() - 1;
1372 return columnofcell[cell];
1376 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1378 row_type const row = cellRow(cell);
1379 col_type column = cellColumn(cell);
1380 while (column < columnCount() - 1 &&
1381 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1387 void Tabular::write(Buffer const & buf, ostream & os) const
1391 << write_attribute("version", 3)
1392 << write_attribute("rows", rowCount())
1393 << write_attribute("columns", columnCount())
1395 // global longtable options
1397 << write_attribute("rotate", rotate)
1398 << write_attribute("booktabs", use_booktabs)
1399 << write_attribute("islongtable", is_long_tabular)
1400 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1401 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1402 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1403 << write_attribute("headTopDL", endhead.topDL)
1404 << write_attribute("headBottomDL", endhead.bottomDL)
1405 << write_attribute("footTopDL", endfoot.topDL)
1406 << write_attribute("footBottomDL", endfoot.bottomDL)
1407 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1408 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1409 << write_attribute("lastFootEmpty", endlastfoot.empty)
1411 for (col_type j = 0; j < columnCount(); ++j) {
1413 << write_attribute("alignment", column_info[j].alignment)
1414 << write_attribute("valignment", column_info[j].valignment)
1415 << write_attribute("leftline", column_info[j].left_line)
1416 << write_attribute("rightline", column_info[j].right_line)
1417 << write_attribute("width", column_info[j].p_width.asString())
1418 << write_attribute("special", column_info[j].align_special)
1421 for (row_type i = 0; i < rowCount(); ++i) {
1423 << write_attribute("topline", row_info[i].top_line)
1424 << write_attribute("bottomline", row_info[i].bottom_line);
1425 static const string def("default");
1426 if (row_info[i].top_space_default)
1427 os << write_attribute("topspace", def);
1429 os << write_attribute("topspace", row_info[i].top_space);
1430 if (row_info[i].bottom_space_default)
1431 os << write_attribute("bottomspace", def);
1433 os << write_attribute("bottomspace", row_info[i].bottom_space);
1434 if (row_info[i].interline_space_default)
1435 os << write_attribute("interlinespace", def);
1437 os << write_attribute("interlinespace", row_info[i].interline_space);
1438 os << write_attribute("endhead", row_info[i].endhead)
1439 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1440 << write_attribute("endfoot", row_info[i].endfoot)
1441 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1442 << write_attribute("newpage", row_info[i].newpage)
1444 for (col_type j = 0; j < columnCount(); ++j) {
1446 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1447 << write_attribute("alignment", cell_info[i][j].alignment)
1448 << write_attribute("valignment", cell_info[i][j].valignment)
1449 << write_attribute("topline", cell_info[i][j].top_line)
1450 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1451 << write_attribute("leftline", cell_info[i][j].left_line)
1452 << write_attribute("rightline", cell_info[i][j].right_line)
1453 << write_attribute("rotate", cell_info[i][j].rotate)
1454 << write_attribute("usebox", cell_info[i][j].usebox)
1455 << write_attribute("width", cell_info[i][j].p_width)
1456 << write_attribute("special", cell_info[i][j].align_special)
1458 os << "\\begin_inset ";
1459 cell_info[i][j].inset->write(buf, os);
1460 os << "\n\\end_inset\n"
1465 os << "</lyxtabular>\n";
1469 void Tabular::read(Buffer const & buf, Lexer & lex)
1472 istream & is = lex.getStream();
1474 l_getline(is, line);
1475 if (!prefixIs(line, "<lyxtabular ")
1476 && !prefixIs(line, "<Tabular ")) {
1477 BOOST_ASSERT(false);
1482 if (!getTokenValue(line, "version", version))
1484 BOOST_ASSERT(version >= 2);
1487 if (!getTokenValue(line, "rows", rows_arg))
1490 if (!getTokenValue(line, "columns", columns_arg))
1492 init(buf.params(), rows_arg, columns_arg);
1493 l_getline(is, line);
1494 if (!prefixIs(line, "<features")) {
1495 lyxerr << "Wrong tabular format (expected <features ...> got"
1496 << line << ')' << endl;
1499 getTokenValue(line, "rotate", rotate);
1500 getTokenValue(line, "booktabs", use_booktabs);
1501 getTokenValue(line, "islongtable", is_long_tabular);
1502 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1503 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1504 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1505 getTokenValue(line, "headTopDL", endhead.topDL);
1506 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1507 getTokenValue(line, "footTopDL", endfoot.topDL);
1508 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1509 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1510 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1511 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1513 for (col_type j = 0; j < columnCount(); ++j) {
1515 if (!prefixIs(line,"<column")) {
1516 lyxerr << "Wrong tabular format (expected <column ...> got"
1517 << line << ')' << endl;
1520 getTokenValue(line, "alignment", column_info[j].alignment);
1521 getTokenValue(line, "valignment", column_info[j].valignment);
1522 getTokenValue(line, "leftline", column_info[j].left_line);
1523 getTokenValue(line, "rightline", column_info[j].right_line);
1524 getTokenValue(line, "width", column_info[j].p_width);
1525 getTokenValue(line, "special", column_info[j].align_special);
1528 for (row_type i = 0; i < rowCount(); ++i) {
1529 l_getline(is, line);
1530 if (!prefixIs(line, "<row")) {
1531 lyxerr << "Wrong tabular format (expected <row ...> got"
1532 << line << ')' << endl;
1535 getTokenValue(line, "topline", row_info[i].top_line);
1536 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1537 getTokenValue(line, "topspace", row_info[i].top_space,
1538 row_info[i].top_space_default);
1539 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1540 row_info[i].bottom_space_default);
1541 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1542 row_info[i].interline_space_default);
1543 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1544 getTokenValue(line, "endhead", row_info[i].endhead);
1545 getTokenValue(line, "endfoot", row_info[i].endfoot);
1546 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1547 getTokenValue(line, "newpage", row_info[i].newpage);
1548 for (col_type j = 0; j < columnCount(); ++j) {
1549 l_getline(is, line);
1550 if (!prefixIs(line, "<cell")) {
1551 lyxerr << "Wrong tabular format (expected <cell ...> got"
1552 << line << ')' << endl;
1555 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1556 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1557 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1558 getTokenValue(line, "topline", cell_info[i][j].top_line);
1559 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1560 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1561 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1562 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1563 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1564 getTokenValue(line, "width", cell_info[i][j].p_width);
1565 getTokenValue(line, "special", cell_info[i][j].align_special);
1566 l_getline(is, line);
1567 if (prefixIs(line, "\\begin_inset")) {
1568 cell_info[i][j].inset->read(buf, lex);
1569 l_getline(is, line);
1571 if (!prefixIs(line, "</cell>")) {
1572 lyxerr << "Wrong tabular format (expected </cell> got"
1573 << line << ')' << endl;
1577 l_getline(is, line);
1578 if (!prefixIs(line, "</row>")) {
1579 lyxerr << "Wrong tabular format (expected </row> got"
1580 << line << ')' << endl;
1584 while (!prefixIs(line, "</lyxtabular>")) {
1585 l_getline(is, line);
1591 bool Tabular::isMultiColumn(idx_type cell) const
1593 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1597 bool Tabular::isMultiColumnReal(idx_type cell) const
1599 return cellColumn(cell) != cellRightColumn(cell) &&
1600 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1604 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1606 return cell_info[cellRow(cell)][cellColumn(cell)];
1610 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1613 cellstruct & cs = cellinfo_of_cell(cell);
1614 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1615 cs.alignment = column_info[cellColumn(cell)].alignment;
1616 cs.top_line = row_info[cellRow(cell)].top_line;
1617 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1618 cs.left_line = column_info[cellColumn(cell)].left_line;
1619 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1620 for (idx_type i = 1; i < number; ++i) {
1621 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1622 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1623 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1630 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1632 row_type const row = cellRow(cell);
1633 col_type column = cellColumn(cell);
1634 idx_type result = 1;
1636 while (column < columnCount() &&
1637 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1646 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1648 row_type const row = cellRow(cell);
1649 col_type column = cellColumn(cell);
1651 idx_type result = 0;
1653 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1654 cell_info[row][column].multicolumn = CELL_NORMAL;
1656 while (column < columnCount() &&
1657 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1659 cell_info[row][column].multicolumn = CELL_NORMAL;
1669 void Tabular::setBookTabs(bool what)
1671 use_booktabs = what;
1675 bool Tabular::useBookTabs() const
1677 return use_booktabs;
1681 void Tabular::setLongTabular(bool what)
1683 is_long_tabular = what;
1687 bool Tabular::isLongTabular() const
1689 return is_long_tabular;
1693 void Tabular::setRotateTabular(bool flag)
1699 bool Tabular::getRotateTabular() const
1705 void Tabular::setRotateCell(idx_type cell, bool flag)
1707 cellinfo_of_cell(cell).rotate = flag;
1711 bool Tabular::getRotateCell(idx_type cell) const
1713 return cellinfo_of_cell(cell).rotate;
1717 bool Tabular::needRotating() const
1721 for (row_type i = 0; i < rowCount(); ++i)
1722 for (col_type j = 0; j < columnCount(); ++j)
1723 if (cell_info[i][j].rotate)
1729 bool Tabular::isLastCell(idx_type cell) const
1731 if (cell + 1 < cellCount())
1737 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1739 if (cellRow(cell) > 0)
1740 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1745 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1747 if (cellRow(cell) + 1 < rowCount())
1748 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1753 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1755 if (cellRow(cell) == 0)
1757 if (!isMultiColumn(cell))
1758 return getCellAbove(cell);
1759 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1763 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1765 if (cellRow(cell) + 1 >= rowCount())
1767 if (!isMultiColumn(cell))
1768 return getCellBelow(cell);
1769 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1773 Tabular::idx_type Tabular::cellIndex(row_type row,
1774 col_type column) const
1776 BOOST_ASSERT(column != npos && column < columnCount() &&
1777 row != npos && row < rowCount());
1778 return cell_info[row][column].cellno;
1782 void Tabular::setUsebox(idx_type cell, BoxType type)
1784 cellinfo_of_cell(cell).usebox = type;
1788 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1790 if (column_info[cellColumn(cell)].p_width.zero() &&
1791 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1793 if (cellinfo_of_cell(cell).usebox > 1)
1794 return cellinfo_of_cell(cell).usebox;
1795 return useParbox(cell);
1800 // This are functions used for the longtable support
1802 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1808 row_info[row].endfirsthead = flag;
1812 row_info[row].endhead = flag;
1817 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1820 hd.set = haveLTHead();
1821 return row_info[row].endhead;
1825 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1828 hd.set = haveLTFirstHead();
1829 return row_info[row].endfirsthead;
1833 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1839 row_info[row].endlastfoot = flag;
1843 row_info[row].endfoot = flag;
1848 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1851 fd.set = haveLTFoot();
1852 return row_info[row].endfoot;
1856 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1859 fd.set = haveLTLastFoot();
1860 return row_info[row].endlastfoot;
1864 void Tabular::setLTNewPage(row_type row, bool what)
1866 row_info[row].newpage = what;
1870 bool Tabular::getLTNewPage(row_type row) const
1872 return row_info[row].newpage;
1876 bool Tabular::haveLTHead() const
1878 for (row_type i = 0; i < rowCount(); ++i)
1879 if (row_info[i].endhead)
1885 bool Tabular::haveLTFirstHead() const
1887 if (endfirsthead.empty)
1889 for (row_type i = 0; i < rowCount(); ++i)
1890 if (row_info[i].endfirsthead)
1896 bool Tabular::haveLTFoot() const
1898 for (row_type i = 0; i < rowCount(); ++i)
1899 if (row_info[i].endfoot)
1905 bool Tabular::haveLTLastFoot() const
1907 if (endlastfoot.empty)
1909 for (row_type i = 0; i < rowCount(); ++i)
1910 if (row_info[i].endlastfoot)
1916 // end longtable support functions
1918 void Tabular::setRowAscent(row_type row, int height)
1920 if (row >= rowCount() || row_info[row].ascent == height)
1922 row_info[row].ascent = height;
1926 void Tabular::setRowDescent(row_type row, int height)
1928 if (row >= rowCount() || row_info[row].descent == height)
1930 row_info[row].descent = height;
1934 int Tabular::rowAscent(row_type row) const
1936 if (row >= rowCount())
1938 return row_info[row].ascent;
1942 int Tabular::rowDescent(row_type row) const
1944 BOOST_ASSERT(row < rowCount());
1945 return row_info[row].descent;
1949 int Tabular::height() const
1952 for (row_type row = 0; row < rowCount(); ++row)
1953 height += rowAscent(row) + rowDescent(row) +
1954 getAdditionalHeight(row);
1959 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1961 BOOST_ASSERT(row < rowCount());
1962 BOOST_ASSERT(column < columnCount());
1963 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1967 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1969 // FIXME: assert or return 0 as in TeXBottomHLine()?
1970 BOOST_ASSERT(row != npos);
1971 BOOST_ASSERT(row < rowCount());
1973 idx_type const fcell = getFirstCellInRow(row);
1974 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1977 for (idx_type i = fcell; i < n; ++i) {
1981 if (use_booktabs && row == 0) {
1983 } else if (tmp == n - fcell) {
1984 os << (use_booktabs ? "\\midrule " : "\\hline ");
1986 for (idx_type i = fcell; i < n; ++i) {
1988 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1989 << cellColumn(i) + 1
1991 << cellRightColumn(i) + 1
2003 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2005 // FIXME: return 0 or assert as in TeXTopHLine()?
2006 if (row == npos || row >= rowCount())
2009 idx_type const fcell = getFirstCellInRow(row);
2010 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2013 for (idx_type i = fcell; i < n; ++i) {
2017 if (use_booktabs && row == rowCount() - 1) {
2018 os << "\\bottomrule";
2019 } else if (tmp == n - fcell) {
2020 os << (use_booktabs ? "\\midrule" : "\\hline");
2022 for (idx_type i = fcell; i < n; ++i) {
2023 if (bottomLine(i)) {
2024 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2025 << cellColumn(i) + 1
2027 << cellRightColumn(i) + 1
2039 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2043 if (getRotateCell(cell)) {
2044 os << "\\begin{sideways}\n";
2047 if (isMultiColumn(cell)) {
2048 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2049 if (leftLine(cell) &&
2050 (isFirstCellInRow(cell) ||
2051 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2052 !rightLine(cell - 1, true))))
2054 if (!cellinfo_of_cell(cell).align_special.empty()) {
2055 os << cellinfo_of_cell(cell).align_special;
2057 if (!getPWidth(cell).zero()) {
2058 switch (getVAlignment(cell)) {
2059 case LYX_VALIGN_TOP:
2062 case LYX_VALIGN_MIDDLE:
2065 case LYX_VALIGN_BOTTOM:
2070 << from_ascii(getPWidth(cell).asLatexString())
2073 switch (getAlignment(cell)) {
2074 case LYX_ALIGN_LEFT:
2077 case LYX_ALIGN_RIGHT:
2084 } // end if else !getPWidth
2085 } // end if else !cellinfo_of_cell
2086 if (rightLine(cell))
2088 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2093 if (getUsebox(cell) == BOX_PARBOX) {
2095 switch (getVAlignment(cell)) {
2096 case LYX_VALIGN_TOP:
2099 case LYX_VALIGN_MIDDLE:
2102 case LYX_VALIGN_BOTTOM:
2106 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2108 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2109 os << "\\begin{minipage}[";
2110 switch (getVAlignment(cell)) {
2111 case LYX_VALIGN_TOP:
2114 case LYX_VALIGN_MIDDLE:
2117 case LYX_VALIGN_BOTTOM:
2121 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2129 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2134 if (getUsebox(cell) == BOX_PARBOX)
2136 else if (getUsebox(cell) == BOX_MINIPAGE) {
2137 os << "%\n\\end{minipage}";
2140 if (isMultiColumn(cell)) {
2143 if (getRotateCell(cell)) {
2144 os << "%\n\\end{sideways}";
2151 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2152 OutputParams const & runparams) const
2154 if (!is_long_tabular)
2158 // output header info
2160 if (endhead.topDL) {
2164 for (row_type i = 0; i < rowCount(); ++i) {
2165 if (row_info[i].endhead) {
2166 ret += TeXRow(os, i, buf, runparams);
2169 if (endhead.bottomDL) {
2173 os << "\\endhead\n";
2175 if (endfirsthead.empty) {
2176 os << "\\endfirsthead\n";
2180 // output firstheader info
2181 if (haveLTFirstHead()) {
2182 if (endfirsthead.topDL) {
2186 for (row_type i = 0; i < rowCount(); ++i) {
2187 if (row_info[i].endfirsthead) {
2188 ret += TeXRow(os, i, buf, runparams);
2191 if (endfirsthead.bottomDL) {
2195 os << "\\endfirsthead\n";
2198 // output footer info
2200 if (endfoot.topDL) {
2204 for (row_type i = 0; i < rowCount(); ++i) {
2205 if (row_info[i].endfoot) {
2206 ret += TeXRow(os, i, buf, runparams);
2209 if (endfoot.bottomDL) {
2213 os << "\\endfoot\n";
2215 if (endlastfoot.empty) {
2216 os << "\\endlastfoot\n";
2220 // output lastfooter info
2221 if (haveLTLastFoot()) {
2222 if (endlastfoot.topDL) {
2226 for (row_type i = 0; i < rowCount(); ++i) {
2227 if (row_info[i].endlastfoot) {
2228 ret += TeXRow(os, i, buf, runparams);
2231 if (endlastfoot.bottomDL) {
2235 os << "\\endlastfoot\n";
2242 bool Tabular::isValidRow(row_type row) const
2244 if (!is_long_tabular)
2246 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2247 !row_info[row].endfoot && !row_info[row].endlastfoot;
2251 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2252 OutputParams const & runparams) const
2254 idx_type cell = cellIndex(i, 0);
2255 int ret = TeXTopHLine(os, i);
2256 if (row_info[i].top_space_default) {
2258 os << "\\addlinespace\n";
2260 os << "\\noalign{\\vskip\\doublerulesep}\n";
2262 } else if(!row_info[i].top_space.zero()) {
2264 os << "\\addlinespace["
2265 << from_ascii(row_info[i].top_space.asLatexString())
2268 os << "\\noalign{\\vskip"
2269 << from_ascii(row_info[i].top_space.asLatexString())
2275 for (col_type j = 0; j < columnCount(); ++j) {
2276 if (isPartOfMultiColumn(i, j))
2278 ret += TeXCellPreamble(os, cell);
2279 shared_ptr<InsetText> inset = getCellInset(cell);
2281 Paragraph const & par = inset->paragraphs().front();
2282 bool rtl = par.isRTL(buf.params())
2284 && getPWidth(cell).zero();
2287 if (par.getParLanguage(buf.params())->lang() ==
2290 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2292 // currently, remaning RTL languages are arabic_arabtex and hebrew
2296 ret += inset->latex(buf, os, runparams);
2300 ret += TeXCellPostamble(os, cell);
2301 if (!isLastCellInRow(cell)) { // not last cell in row
2306 os << "\\tabularnewline";
2307 if (row_info[i].bottom_space_default) {
2309 os << "\\addlinespace";
2311 os << "[\\doublerulesep]";
2312 } else if (!row_info[i].bottom_space.zero()) {
2314 os << "\\addlinespace";
2316 << from_ascii(row_info[i].bottom_space.asLatexString())
2321 ret += TeXBottomHLine(os, i);
2322 if (row_info[i].interline_space_default) {
2324 os << "\\addlinespace\n";
2326 os << "\\noalign{\\vskip\\doublerulesep}\n";
2328 } else if (!row_info[i].interline_space.zero()) {
2330 os << "\\addlinespace["
2331 << from_ascii(row_info[i].interline_space.asLatexString())
2334 os << "\\noalign{\\vskip"
2335 << from_ascii(row_info[i].interline_space.asLatexString())
2343 int Tabular::latex(Buffer const & buf, odocstream & os,
2344 OutputParams const & runparams) const
2348 //+---------------------------------------------------------------------
2349 //+ first the opening preamble +
2350 //+---------------------------------------------------------------------
2353 os << "\\begin{sideways}\n";
2356 if (is_long_tabular)
2357 os << "\\begin{longtable}{";
2359 os << "\\begin{tabular}{";
2360 for (col_type i = 0; i < columnCount(); ++i) {
2361 if (!use_booktabs && column_info[i].left_line)
2363 if (!column_info[i].align_special.empty()) {
2364 os << column_info[i].align_special;
2366 if (!column_info[i].p_width.zero()) {
2367 switch (column_info[i].alignment) {
2368 case LYX_ALIGN_LEFT:
2369 os << ">{\\raggedright}";
2371 case LYX_ALIGN_RIGHT:
2372 os << ">{\\raggedleft}";
2374 case LYX_ALIGN_CENTER:
2375 os << ">{\\centering}";
2377 case LYX_ALIGN_NONE:
2378 case LYX_ALIGN_BLOCK:
2379 case LYX_ALIGN_LAYOUT:
2380 case LYX_ALIGN_SPECIAL:
2384 switch (column_info[i].valignment) {
2385 case LYX_VALIGN_TOP:
2388 case LYX_VALIGN_MIDDLE:
2391 case LYX_VALIGN_BOTTOM:
2396 << from_ascii(column_info[i].p_width.asLatexString())
2399 switch (column_info[i].alignment) {
2400 case LYX_ALIGN_LEFT:
2403 case LYX_ALIGN_RIGHT:
2410 } // end if else !column_info[i].p_width
2411 } // end if else !column_info[i].align_special
2412 if (!use_booktabs && column_info[i].right_line)
2418 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2420 //+---------------------------------------------------------------------
2421 //+ the single row and columns (cells) +
2422 //+---------------------------------------------------------------------
2424 for (row_type i = 0; i < rowCount(); ++i) {
2425 if (isValidRow(i)) {
2426 ret += TeXRow(os, i, buf, runparams);
2427 if (is_long_tabular && row_info[i].newpage) {
2428 os << "\\newpage\n";
2434 //+---------------------------------------------------------------------
2435 //+ the closing of the tabular +
2436 //+---------------------------------------------------------------------
2438 if (is_long_tabular)
2439 os << "\\end{longtable}";
2441 os << "\\end{tabular}";
2443 os << "\n\\end{sideways}";
2451 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2452 OutputParams const & runparams) const
2455 idx_type cell = getFirstCellInRow(row);
2458 for (col_type j = 0; j < columnCount(); ++j) {
2459 if (isPartOfMultiColumn(row, j))
2462 os << "<entry align=\"";
2463 switch (getAlignment(cell)) {
2464 case LYX_ALIGN_LEFT:
2467 case LYX_ALIGN_RIGHT:
2475 os << "\" valign=\"";
2476 switch (getVAlignment(cell)) {
2477 case LYX_VALIGN_TOP:
2480 case LYX_VALIGN_BOTTOM:
2483 case LYX_VALIGN_MIDDLE:
2488 if (isMultiColumn(cell)) {
2489 os << " namest=\"col" << j << "\" ";
2490 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2494 ret += getCellInset(cell)->docbook(buf, os, runparams);
2503 int Tabular::docbook(Buffer const & buf, odocstream & os,
2504 OutputParams const & runparams) const
2508 //+---------------------------------------------------------------------
2509 //+ first the opening preamble +
2510 //+---------------------------------------------------------------------
2512 os << "<tgroup cols=\"" << columnCount()
2513 << "\" colsep=\"1\" rowsep=\"1\">\n";
2515 for (col_type i = 0; i < columnCount(); ++i) {
2516 os << "<colspec colname=\"col" << i << "\" align=\"";
2517 switch (column_info[i].alignment) {
2518 case LYX_ALIGN_LEFT:
2521 case LYX_ALIGN_RIGHT:
2529 if (runparams.flavor == OutputParams::XML)
2535 //+---------------------------------------------------------------------
2536 //+ Long Tabular case +
2537 //+---------------------------------------------------------------------
2539 // output header info
2540 if (haveLTHead() || haveLTFirstHead()) {
2543 for (row_type i = 0; i < rowCount(); ++i) {
2544 if (row_info[i].endhead || row_info[i].endfirsthead) {
2545 ret += docbookRow(buf, os, i, runparams);
2551 // output footer info
2552 if (haveLTFoot() || haveLTLastFoot()) {
2555 for (row_type i = 0; i < rowCount(); ++i) {
2556 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2557 ret += docbookRow(buf, os, i, runparams);
2564 //+---------------------------------------------------------------------
2565 //+ the single row and columns (cells) +
2566 //+---------------------------------------------------------------------
2570 for (row_type i = 0; i < rowCount(); ++i) {
2571 if (isValidRow(i)) {
2572 ret += docbookRow(buf, os, i, runparams);
2577 //+---------------------------------------------------------------------
2578 //+ the closing of the tabular +
2579 //+---------------------------------------------------------------------
2588 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2589 vector<unsigned int> const & clen) const
2591 idx_type const fcell = getFirstCellInRow(row);
2592 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2595 for (idx_type i = fcell; i < n; ++i) {
2605 for (idx_type i = fcell; i < n; ++i) {
2616 col_type column = cellColumn(i);
2617 int len = clen[column];
2618 while (column < columnCount() - 1
2619 && isPartOfMultiColumn(row, ++column))
2620 len += clen[column] + 4;
2621 os << docstring(len, ch);
2636 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2637 vector<unsigned int> const & clen) const
2639 idx_type const fcell = getFirstCellInRow(row);
2640 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2643 for (idx_type i = fcell; i < n; ++i) {
2644 if (bottomLine(i)) {
2653 for (idx_type i = fcell; i < n; ++i) {
2654 if (bottomLine(i)) {
2664 col_type column = cellColumn(i);
2665 int len = clen[column];
2666 while (column < columnCount() -1
2667 && isPartOfMultiColumn(row, ++column))
2668 len += clen[column] + 4;
2669 os << docstring(len, ch);
2670 if (bottomLine(i)) {
2684 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2685 OutputParams const & runparams,
2686 idx_type cell, row_type row, col_type column,
2687 vector<unsigned int> const & clen,
2688 bool onlydata) const
2690 odocstringstream sstr;
2691 getCellInset(cell)->plaintext(buf, sstr, runparams);
2703 unsigned int len1 = sstr.str().length();
2704 unsigned int len2 = clen[column];
2705 while (column < columnCount() -1
2706 && isPartOfMultiColumn(row, ++column))
2707 len2 += clen[column] + 4;
2710 switch (getAlignment(cell)) {
2712 case LYX_ALIGN_LEFT:
2715 case LYX_ALIGN_RIGHT:
2719 case LYX_ALIGN_CENTER:
2725 os << docstring(len1, ' ') << sstr.str()
2726 << docstring(len2, ' ');
2728 if (rightLine(cell))
2735 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2736 OutputParams const & runparams, int const depth,
2737 bool onlydata, char_type delim) const
2739 // first calculate the width of the single columns
2740 vector<unsigned int> clen(columnCount());
2743 // first all non (real) multicolumn cells!
2744 for (col_type j = 0; j < columnCount(); ++j) {
2746 for (row_type i = 0; i < rowCount(); ++i) {
2747 idx_type cell = cellIndex(i, j);
2748 if (isMultiColumnReal(cell))
2750 odocstringstream sstr;
2751 getCellInset(cell)->plaintext(buf, sstr, runparams);
2752 if (clen[j] < sstr.str().length())
2753 clen[j] = sstr.str().length();
2756 // then all (real) multicolumn cells!
2757 for (col_type j = 0; j < columnCount(); ++j) {
2758 for (row_type i = 0; i < rowCount(); ++i) {
2759 idx_type cell = cellIndex(i, j);
2760 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2762 odocstringstream sstr;
2763 getCellInset(cell)->plaintext(buf, sstr, runparams);
2764 int len = int(sstr.str().length());
2765 idx_type const n = cells_in_multicolumn(cell);
2766 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2768 if (len > int(clen[j + n - 1]))
2769 clen[j + n - 1] = len;
2774 for (row_type i = 0; i < rowCount(); ++i) {
2775 if (!onlydata && plaintextTopHLine(os, i, clen))
2776 os << docstring(depth * 2, ' ');
2777 for (col_type j = 0; j < columnCount(); ++j) {
2778 if (isPartOfMultiColumn(i, j))
2780 if (onlydata && j > 0)
2781 // we don't use operator<< for single UCS4 character.
2782 // see explanation in docstream.h
2784 plaintextPrintCell(buf, os, runparams,
2785 cell, i, j, clen, onlydata);
2790 os << docstring(depth * 2, ' ');
2791 if (plaintextBottomHLine(os, i, clen))
2792 os << docstring(depth * 2, ' ');
2798 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2800 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2804 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2805 col_type column) const
2807 return cell_info[row][column].inset;
2811 void Tabular::setCellInset(row_type row, col_type column,
2812 shared_ptr<InsetText> ins) const
2814 cell_info[row][column].inset = ins;
2819 Tabular::getCellFromInset(Inset const * inset) const
2821 // is this inset part of the tabular?
2823 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2824 BOOST_ASSERT(false);
2827 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2828 if (getCellInset(cell).get() == inset) {
2829 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2830 << "cell=" << cell);
2834 // We should have found a cell at this point
2835 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2836 << inset << " not found!" << endl;
2837 BOOST_ASSERT(false);
2843 void Tabular::validate(LaTeXFeatures & features) const
2845 features.require("NeedTabularnewline");
2847 features.require("booktabs");
2848 if (isLongTabular())
2849 features.require("longtable");
2851 features.require("rotating");
2852 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2853 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2854 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2855 features.require("array");
2856 getCellInset(cell)->validate(features);
2861 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2863 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2864 ParagraphList::const_iterator cit = parlist.begin();
2865 ParagraphList::const_iterator end = parlist.end();
2867 for (; cit != end; ++cit)
2868 for (int i = 0; i < cit->size(); ++i)
2869 if (cit->isNewline(i))
2876 /////////////////////////////////////////////////////////////////////
2880 /////////////////////////////////////////////////////////////////////
2882 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2884 : tabular(buf.params(), max(rows, row_type(1)),
2885 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2889 InsetTabular::InsetTabular(InsetTabular const & tab)
2890 : Inset(tab), tabular(tab.tabular),
2891 buffer_(tab.buffer_), scx_(0)
2895 InsetTabular::~InsetTabular()
2897 InsetTabularMailer(*this).hideDialog();
2901 Inset * InsetTabular::clone() const
2903 return new InsetTabular(*this);
2907 Buffer const & InsetTabular::buffer() const
2913 void InsetTabular::buffer(Buffer const * b)
2919 void InsetTabular::write(Buffer const & buf, ostream & os) const
2921 os << "Tabular" << endl;
2922 tabular.write(buf, os);
2926 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2928 bool const old_format = (lex.getString() == "\\LyXTable");
2930 tabular.read(buf, lex);
2936 string token = lex.getString();
2937 while (lex && token != "\\end_inset") {
2939 token = lex.getString();
2942 lex.printError("Missing \\end_inset at this point. "
2948 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2950 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2951 // mi.base.textwidth << "\n";
2953 lyxerr << "InsetTabular::metrics: need bv" << endl;
2954 BOOST_ASSERT(false);
2958 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2961 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2962 if (tabular.isPartOfMultiColumn(i, j))
2963 // Multicolumn cell, but not first one
2968 if (tabular.cell_info[i][j].multicolumn ==
2969 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2970 p_width = tabular.cellinfo_of_cell(cell).p_width;
2972 p_width = tabular.column_info[j].p_width;
2973 if (!p_width.zero())
2974 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2975 tabular.getCellInset(cell)->metrics(m, dim);
2976 if (!p_width.zero())
2977 dim.wid = m.base.textwidth;
2978 tabular.setCellWidth(cell, dim.wid);
2979 if (p_width.zero()) {
2980 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2981 // FIXME there must be a way to get rid of
2982 // the second metrics call
2983 tabular.getCellInset(cell)->metrics(m, dim);
2985 maxAsc = max(maxAsc, dim.asc);
2986 maxDesc = max(maxDesc, dim.des);
2989 int const top_space = tabular.row_info[i].top_space_default ?
2990 default_line_space :
2991 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2992 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2993 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2994 default_line_space :
2995 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2996 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2999 dim.asc = tabular.rowAscent(0);
3000 dim.des = tabular.height() - dim.asc;
3001 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3005 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3007 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3008 BufferView * bv = pi.base.bv;
3010 // FIXME: As the full backrgound is painted in drawSelection(),
3011 // we have no choice but to do a full repaint for the Text cells.
3012 pi.full_repaint = true;
3014 resetPos(bv->cursor());
3017 x += ADD_TO_TABULAR_WIDTH;
3019 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3022 first_visible_cell = Tabular::npos;
3023 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3025 int const a = tabular.rowAscent(i);
3026 int const d = tabular.rowDescent(i);
3027 idx = tabular.cellIndex(i, 0);
3028 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3029 if (tabular.isPartOfMultiColumn(i, j))
3031 if (first_visible_cell == Tabular::npos)
3032 first_visible_cell = idx;
3034 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3035 // Cache the Inset position.
3036 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3037 if (nx + tabular.columnWidth(idx) < 0
3038 || nx > bv->workWidth()
3040 || y - a > bv->workHeight()) {
3041 pi.pain.setDrawingEnabled(false);
3042 cell(idx)->draw(pi, cx, y);
3043 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3044 pi.pain.setDrawingEnabled(original_drawing_state);
3046 cell(idx)->draw(pi, cx, y);
3047 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3049 nx += tabular.columnWidth(idx);
3053 if (i + 1 < tabular.rowCount())
3054 y += d + tabular.rowAscent(i + 1) +
3055 tabular.getAdditionalHeight(i + 1);
3060 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3062 Cursor & cur = pi.base.bv->cursor();
3064 x += scx_ + ADD_TO_TABULAR_WIDTH;
3066 // FIXME: it is wrong to completely paint the background
3067 // if we want to do single row painting.
3069 // Paint background of current tabular
3070 int const w = tabular.width();
3071 int const h = tabular.height();
3072 int yy = y - tabular.rowAscent(0);
3073 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3075 if (!cur.selection())
3077 if (&cur.inset() != this)
3083 if (tablemode(cur)) {
3086 getSelection(cur, rs, re, cs, ce);
3087 y -= tabular.rowAscent(0);
3088 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3089 int const a = tabular.rowAscent(j);
3090 int const h = a + tabular.rowDescent(j);
3092 y += tabular.getAdditionalHeight(j);
3093 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3094 if (tabular.isPartOfMultiColumn(j, i))
3096 idx_type const cell =
3097 tabular.cellIndex(j, i);
3098 int const w = tabular.columnWidth(cell);
3099 if (i >= cs && i <= ce && j >= rs && j <= re)
3100 pi.pain.fillRectangle(xx, y, w, h,
3108 x += getCellXPos(cur.idx());
3109 x += tabular.getBeginningOfTextInCell(cur.idx());
3110 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3115 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3116 row_type row, idx_type cell, bool erased) const
3118 int x2 = x + tabular.columnWidth(cell);
3119 bool on_off = false;
3120 ColorCode col = Color_tabularline;
3121 ColorCode onoffcol = Color_tabularonoffline;
3124 col = Color_deletedtext;
3125 onoffcol = Color_deletedtext;
3128 if (!tabular.topAlreadyDrawn(cell)) {
3129 on_off = !tabular.topLine(cell);
3130 pain.line(x, y - tabular.rowAscent(row),
3131 x2, y - tabular.rowAscent(row),
3132 on_off ? onoffcol : col,
3133 on_off ? Painter::line_onoffdash : Painter::line_solid);
3135 on_off = !tabular.bottomLine(cell);
3136 pain.line(x, y + tabular.rowDescent(row),
3137 x2, y + tabular.rowDescent(row),
3138 on_off ? onoffcol : col,
3139 on_off ? Painter::line_onoffdash : Painter::line_solid);
3140 if (!tabular.leftAlreadyDrawn(cell)) {
3141 on_off = !tabular.leftLine(cell);
3142 pain.line(x, y - tabular.rowAscent(row),
3143 x, y + tabular.rowDescent(row),
3144 on_off ? onoffcol : col,
3145 on_off ? Painter::line_onoffdash : Painter::line_solid);
3147 on_off = !tabular.rightLine(cell);
3148 pain.line(x2 - tabular.getAdditionalWidth(cell),
3149 y - tabular.rowAscent(row),
3150 x2 - tabular.getAdditionalWidth(cell),
3151 y + tabular.rowDescent(row),
3152 on_off ? onoffcol : col,
3153 on_off ? Painter::line_onoffdash : Painter::line_solid);
3157 docstring const InsetTabular::editMessage() const
3159 return _("Opened table");
3163 void InsetTabular::edit(Cursor & cur, bool left)
3165 //lyxerr << "InsetTabular::edit: " << this << endl;
3167 cur.selection() = false;
3170 if (isRightToLeft(cur))
3171 cur.idx() = tabular.getLastCellInRow(0);
3177 if (isRightToLeft(cur))
3178 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3180 cur.idx() = tabular.cellCount() - 1;
3182 cur.pos() = cur.lastpos(); // FIXME crude guess
3184 // FIXME: this accesses the position cache before it is initialized
3186 //cur.bv().fitCursor();
3190 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3192 // In a longtable, tell captions what the current float is
3193 Counters & cnts = buf.params().getTextClass().counters();
3194 string const saveflt = cnts.current_float();
3195 if (tabular.isLongTabular())
3196 cnts.current_float("table");
3198 ParIterator it2 = it;
3200 size_t const end = it2.nargs();
3201 for ( ; it2.idx() < end; it2.top().forwardIdx())
3202 lyx::updateLabels(buf, it2);
3205 if (tabular.isLongTabular())
3206 cnts.current_float(saveflt);
3210 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3212 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3213 << "\n cur:" << cur);
3214 CursorSlice sl = cur.top();
3215 Cursor & bvcur = cur.bv().cursor();
3217 switch (cmd.action) {
3219 case LFUN_MOUSE_PRESS:
3220 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3222 // do not reset cursor/selection if we have selected
3223 // some cells (bug 2715).
3224 if (cmd.button() == mouse_button::button3
3225 && &bvcur.selBegin().inset() == this
3226 && tablemode(bvcur))
3229 // Let InsetText do it
3230 cell(cur.idx())->dispatch(cur, cmd);
3232 case LFUN_MOUSE_MOTION:
3233 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3234 if (cmd.button() == mouse_button::button1) {
3235 // only accept motions to places not deeper nested than the real anchor
3236 if (bvcur.anchor_.hasPart(cur)) {
3237 // only update if selection changes
3238 if (bvcur.idx() == cur.idx() &&
3239 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3241 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3242 bvcur.setCursor(cur);
3243 bvcur.selection() = true;
3249 case LFUN_MOUSE_RELEASE:
3250 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3251 if (cmd.button() == mouse_button::button3)
3252 InsetTabularMailer(*this).showDialog(&cur.bv());
3255 case LFUN_CELL_BACKWARD:
3257 cur.selection() = false;
3260 case LFUN_CELL_FORWARD:
3262 cur.selection() = false;
3265 case LFUN_CHAR_FORWARD_SELECT:
3266 case LFUN_CHAR_FORWARD:
3267 cell(cur.idx())->dispatch(cur, cmd);
3268 if (!cur.result().dispatched()) {
3270 if (sl == cur.top())
3271 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3277 case LFUN_CHAR_BACKWARD_SELECT:
3278 case LFUN_CHAR_BACKWARD:
3279 cell(cur.idx())->dispatch(cur, cmd);
3280 if (!cur.result().dispatched()) {
3282 if (sl == cur.top())
3283 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3289 case LFUN_CHAR_RIGHT_SELECT:
3290 case LFUN_CHAR_RIGHT:
3291 //FIXME: for visual cursor, really move right
3292 if (isRightToLeft(cur))
3293 lyx::dispatch(FuncRequest(
3294 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3295 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3297 lyx::dispatch(FuncRequest(
3298 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3299 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3302 case LFUN_CHAR_LEFT_SELECT:
3303 case LFUN_CHAR_LEFT:
3304 //FIXME: for visual cursor, really move left
3305 if (isRightToLeft(cur))
3306 lyx::dispatch(FuncRequest(
3307 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3308 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3310 lyx::dispatch(FuncRequest(
3311 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3312 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3315 case LFUN_DOWN_SELECT:
3317 cell(cur.idx())->dispatch(cur, cmd);
3318 cur.dispatched(); // override the cell's decision
3319 if (sl == cur.top())
3320 // if our Text didn't do anything to the cursor
3321 // then we try to put the cursor into the cell below
3322 // setting also the right targetX.
3323 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3324 cur.idx() = tabular.getCellBelow(cur.idx());
3326 TextMetrics const & tm =
3327 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3328 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3330 if (sl == cur.top()) {
3331 // we trick it to go to forward after leaving the
3333 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3338 case LFUN_UP_SELECT:
3340 cell(cur.idx())->dispatch(cur, cmd);
3341 cur.dispatched(); // override the cell's decision
3342 if (sl == cur.top())
3343 // if our Text didn't do anything to the cursor
3344 // then we try to put the cursor into the cell above
3345 // setting also the right targetX.
3346 if (tabular.cellRow(cur.idx()) != 0) {
3347 cur.idx() = tabular.getCellAbove(cur.idx());
3348 cur.pit() = cur.lastpit();
3349 Text const * text = cell(cur.idx())->getText(0);
3350 TextMetrics const & tm = cur.bv().textMetrics(text);
3351 ParagraphMetrics const & pm =
3352 tm.parMetrics(cur.lastpit());
3353 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3355 if (sl == cur.top()) {
3356 cmd = FuncRequest(LFUN_UP);
3361 // case LFUN_SCREEN_DOWN: {
3362 // //if (hasSelection())
3363 // // cur.selection() = false;
3364 // col_type const col = tabular.cellColumn(cur.idx());
3365 // int const t = cur.bv().top_y() + cur.bv().height();
3366 // if (t < yo() + tabular.getHeightOfTabular()) {
3367 // cur.bv().scrollDocView(t);
3368 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3370 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3377 // case LFUN_SCREEN_UP: {
3378 // //if (hasSelection())
3379 // // cur.selection() = false;
3380 // col_type const col = tabular.cellColumn(cur.idx());
3381 // int const t = cur.bv().top_y() + cur.bv().height();
3383 // cur.bv().scrollDocView(t);
3387 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3391 // cur.par() = cur.lastpar();
3392 // cur.pos() = cur.lastpos();
3396 case LFUN_LAYOUT_TABULAR:
3397 InsetTabularMailer(*this).showDialog(&cur.bv());
3400 case LFUN_INSET_DIALOG_UPDATE:
3401 InsetTabularMailer(*this).updateDialog(&cur.bv());
3404 case LFUN_TABULAR_FEATURE:
3405 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3409 // insert file functions
3410 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3411 case LFUN_FILE_INSERT_PLAINTEXT: {
3413 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3414 FileName(to_utf8(cmd.argument())), false);
3417 cur.recordUndoInset(INSERT_UNDO);
3418 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3419 // content has been replaced,
3420 // so cursor might be invalid
3421 cur.pos() = cur.lastpos();
3422 cur.pit() = cur.lastpit();
3423 bvcur.setCursor(cur);
3430 if (tablemode(cur)) {
3431 if (copySelection(cur)) {
3432 cur.recordUndoInset(DELETE_UNDO);
3437 cell(cur.idx())->dispatch(cur, cmd);
3440 case LFUN_CHAR_DELETE_BACKWARD:
3441 case LFUN_CHAR_DELETE_FORWARD:
3442 if (tablemode(cur)) {
3443 cur.recordUndoInset(DELETE_UNDO);
3447 cell(cur.idx())->dispatch(cur, cmd);
3451 if (!cur.selection())
3453 if (tablemode(cur)) {
3457 cell(cur.idx())->dispatch(cur, cmd);
3460 case LFUN_CLIPBOARD_PASTE:
3461 case LFUN_PRIMARY_SELECTION_PASTE: {
3462 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3463 theClipboard().getAsText() :
3464 theSelection().get();
3467 // pass to InsertPlaintextString, but
3468 // only if we have multi-cell content
3469 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3470 cur.recordUndoInset(INSERT_UNDO);
3471 if (insertPlaintextString(cur.bv(), clip, false)) {
3472 // content has been replaced,
3473 // so cursor might be invalid
3474 cur.pos() = cur.lastpos();
3475 cur.pit() = cur.lastpit();
3476 bvcur.setCursor(cur);
3480 // Let the cell handle normal text
3481 cell(cur.idx())->dispatch(cur, cmd);
3486 if (tabularStackDirty() && theClipboard().isInternal()) {
3487 cur.recordUndoInset(INSERT_UNDO);
3488 pasteClipboard(cur);
3491 cell(cur.idx())->dispatch(cur, cmd);
3494 case LFUN_FONT_EMPH:
3495 case LFUN_FONT_BOLD:
3496 case LFUN_FONT_ROMAN:
3497 case LFUN_FONT_NOUN:
3498 case LFUN_FONT_ITAL:
3499 case LFUN_FONT_FRAK:
3500 case LFUN_FONT_TYPEWRITER:
3501 case LFUN_FONT_SANS:
3502 case LFUN_FONT_FREE_APPLY:
3503 case LFUN_FONT_FREE_UPDATE:
3504 case LFUN_FONT_SIZE:
3505 case LFUN_FONT_UNDERLINE:
3507 case LFUN_WORD_CAPITALIZE:
3508 case LFUN_WORD_UPCASE:
3509 case LFUN_WORD_LOWCASE:
3510 case LFUN_CHARS_TRANSPOSE:
3511 if (tablemode(cur)) {
3514 getSelection(cur, rs, re, cs, ce);
3515 Cursor tmpcur = cur;
3516 for (row_type i = rs; i <= re; ++i) {
3517 for (col_type j = cs; j <= ce; ++j) {
3518 // cursor follows cell:
3519 tmpcur.idx() = tabular.cellIndex(i, j);
3520 // select this cell only:
3523 tmpcur.resetAnchor();
3524 tmpcur.pit() = tmpcur.lastpit();
3525 tmpcur.pos() = tmpcur.top().lastpos();
3526 tmpcur.setCursor(tmpcur);
3527 tmpcur.setSelection();
3528 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3533 cell(cur.idx())->dispatch(cur, cmd);
3537 // we try to handle this event in the insets dispatch function.
3538 cell(cur.idx())->dispatch(cur, cmd);
3544 // function sets an object as defined in func_status.h:
3545 // states OK, Unknown, Disabled, On, Off.
3546 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3547 FuncStatus & status) const
3549 switch (cmd.action) {
3550 case LFUN_TABULAR_FEATURE: {
3551 int action = Tabular::LAST_ACTION;
3553 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3554 string const tmp = tabularFeature[i].feature;
3555 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3556 action = tabularFeature[i].action;
3560 if (action == Tabular::LAST_ACTION) {
3562 status.unknown(true);
3566 string const argument
3567 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3569 row_type sel_row_start = 0;
3570 row_type sel_row_end = 0;
3571 col_type sel_col_start = 0;
3572 col_type sel_col_end = 0;
3573 Tabular::ltType dummyltt;
3576 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3579 case Tabular::SET_PWIDTH:
3580 case Tabular::SET_MPWIDTH:
3581 case Tabular::SET_SPECIAL_COLUMN:
3582 case Tabular::SET_SPECIAL_MULTI:
3583 case Tabular::APPEND_ROW:
3584 case Tabular::APPEND_COLUMN:
3585 case Tabular::DELETE_ROW:
3586 case Tabular::DELETE_COLUMN:
3587 case Tabular::COPY_ROW:
3588 case Tabular::COPY_COLUMN:
3589 case Tabular::SET_ALL_LINES:
3590 case Tabular::UNSET_ALL_LINES:
3591 case Tabular::SET_TOP_SPACE:
3592 case Tabular::SET_BOTTOM_SPACE:
3593 case Tabular::SET_INTERLINE_SPACE:
3597 case Tabular::MULTICOLUMN:
3598 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3601 case Tabular::M_TOGGLE_LINE_TOP:
3603 case Tabular::TOGGLE_LINE_TOP:
3604 status.setOnOff(tabular.topLine(cur.idx(), flag));
3607 case Tabular::M_TOGGLE_LINE_BOTTOM:
3609 case Tabular::TOGGLE_LINE_BOTTOM:
3610 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3613 case Tabular::M_TOGGLE_LINE_LEFT:
3615 case Tabular::TOGGLE_LINE_LEFT:
3616 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3619 case Tabular::M_TOGGLE_LINE_RIGHT:
3621 case Tabular::TOGGLE_LINE_RIGHT:
3622 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3625 case Tabular::M_ALIGN_LEFT:
3627 case Tabular::ALIGN_LEFT:
3628 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3631 case Tabular::M_ALIGN_RIGHT:
3633 case Tabular::ALIGN_RIGHT:
3634 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3637 case Tabular::M_ALIGN_CENTER:
3639 case Tabular::ALIGN_CENTER:
3640 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3643 case Tabular::ALIGN_BLOCK:
3644 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3645 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3648 case Tabular::M_VALIGN_TOP:
3650 case Tabular::VALIGN_TOP:
3652 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3655 case Tabular::M_VALIGN_BOTTOM:
3657 case Tabular::VALIGN_BOTTOM:
3659 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3662 case Tabular::M_VALIGN_MIDDLE:
3664 case Tabular::VALIGN_MIDDLE:
3666 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3669 case Tabular::SET_LONGTABULAR:
3670 status.setOnOff(tabular.isLongTabular());
3673 case Tabular::UNSET_LONGTABULAR:
3674 status.setOnOff(!tabular.isLongTabular());
3677 case Tabular::TOGGLE_ROTATE_TABULAR:
3678 case Tabular::SET_ROTATE_TABULAR:
3679 status.setOnOff(tabular.getRotateTabular());
3682 case Tabular::UNSET_ROTATE_TABULAR:
3683 status.setOnOff(!tabular.getRotateTabular());
3686 case Tabular::TOGGLE_ROTATE_CELL:
3687 case Tabular::SET_ROTATE_CELL:
3688 status.setOnOff(!oneCellHasRotationState(false,
3689 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3692 case Tabular::UNSET_ROTATE_CELL:
3693 status.setOnOff(!oneCellHasRotationState(true,
3694 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3697 case Tabular::SET_USEBOX:
3698 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3701 case Tabular::SET_LTFIRSTHEAD:
3702 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3705 case Tabular::UNSET_LTFIRSTHEAD:
3706 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3709 case Tabular::SET_LTHEAD:
3710 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3713 case Tabular::UNSET_LTHEAD:
3714 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3717 case Tabular::SET_LTFOOT:
3718 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3721 case Tabular::UNSET_LTFOOT:
3722 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3725 case Tabular::SET_LTLASTFOOT:
3726 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3729 case Tabular::UNSET_LTLASTFOOT:
3730 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3733 case Tabular::SET_LTNEWPAGE:
3734 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3737 case Tabular::SET_BOOKTABS:
3738 status.setOnOff(tabular.useBookTabs());
3741 case Tabular::UNSET_BOOKTABS:
3742 status.setOnOff(!tabular.useBookTabs());
3747 status.enabled(false);
3753 // These are only enabled inside tabular
3754 case LFUN_CELL_BACKWARD:
3755 case LFUN_CELL_FORWARD:
3756 status.enabled(true);
3759 // disable these with multiple cells selected
3760 case LFUN_INSET_INSERT:
3761 case LFUN_TABULAR_INSERT:
3762 case LFUN_FLEX_INSERT:
3763 case LFUN_FLOAT_INSERT:
3764 case LFUN_FLOAT_WIDE_INSERT:
3765 case LFUN_FOOTNOTE_INSERT:
3766 case LFUN_MARGINALNOTE_INSERT:
3767 case LFUN_MATH_INSERT:
3768 case LFUN_MATH_MODE:
3769 case LFUN_MATH_MUTATE:
3770 case LFUN_MATH_DISPLAY:
3771 case LFUN_NOTE_INSERT:
3772 case LFUN_OPTIONAL_INSERT:
3773 case LFUN_BOX_INSERT:
3774 case LFUN_BRANCH_INSERT:
3775 case LFUN_WRAP_INSERT:
3776 case LFUN_ERT_INSERT: {
3777 if (tablemode(cur)) {
3778 status.enabled(false);
3781 return cell(cur.idx())->getStatus(cur, cmd, status);
3784 // disable in non-fixed-width cells
3786 case LFUN_BREAK_PARAGRAPH:
3787 case LFUN_BREAK_PARAGRAPH_SKIP: {
3788 if (tabular.getPWidth(cur.idx()).zero()) {
3789 status.enabled(false);
3792 return cell(cur.idx())->getStatus(cur, cmd, status);
3796 if (tabularStackDirty() && theClipboard().isInternal()) {
3797 status.enabled(true);
3800 return cell(cur.idx())->getStatus(cur, cmd, status);
3802 case LFUN_INSET_MODIFY:
3803 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3804 status.enabled(true);
3810 // we try to handle this event in the insets dispatch function.
3811 return cell(cur.idx())->getStatus(cur, cmd, status);
3816 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3817 OutputParams const & runparams) const
3819 return tabular.latex(buf, os, runparams);
3823 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3824 OutputParams const & runparams) const
3826 os << '\n'; // output table on a new line
3827 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3828 tabular.plaintext(buf, os, runparams, dp, false, 0);
3829 return PLAINTEXT_NEWLINE;
3833 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3834 OutputParams const & runparams) const
3839 // FIXME: Why not pass a proper DocIterator here?
3841 // if the table is inside a float it doesn't need the informaltable
3842 // wrapper. Search for it.
3843 for (master = owner(); master; master = master->owner())
3844 if (master->lyxCode() == FLOAT_CODE)
3849 os << "<informaltable>";
3852 ret += tabular.docbook(buf, os, runparams);
3854 os << "</informaltable>";
3861 void InsetTabular::validate(LaTeXFeatures & features) const
3863 tabular.validate(features);
3867 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3869 return tabular.getCellInset(idx);
3873 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3875 return tabular.getCellInset(idx);
3879 void InsetTabular::cursorPos(BufferView const & bv,
3880 CursorSlice const & sl, bool boundary, int & x, int & y) const
3882 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3884 // y offset correction
3885 int const row = tabular.cellRow(sl.idx());
3886 for (int i = 0; i <= row; ++i) {
3888 y += tabular.rowAscent(i);
3889 y += tabular.getAdditionalHeight(i);
3892 y += tabular.rowDescent(i);
3895 // x offset correction
3896 int const col = tabular.cellColumn(sl.idx());
3897 int idx = tabular.cellIndex(row, 0);
3898 for (int j = 0; j < col; ++j) {
3899 if (tabular.isPartOfMultiColumn(row, j))
3901 x += tabular.columnWidth(idx);
3904 x += tabular.getBeginningOfTextInCell(idx);
3905 x += ADD_TO_TABULAR_WIDTH;
3910 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3914 Inset const & inset = *tabular.getCellInset(cell);
3915 Point o = bv.coordCache().getInsets().xy(&inset);
3916 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3917 int const xend = xbeg + tabular.columnWidth(cell);
3918 row_type const row = tabular.cellRow(cell);
3919 int const ybeg = o.y_ - tabular.rowAscent(row) -
3920 tabular.getAdditionalHeight(row);
3921 int const yend = o.y_ + tabular.rowDescent(row);
3933 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3934 // << " ybeg=" << ybeg << " yend=" << yend
3935 // << " xx=" << xx << " yy=" << yy
3936 // << " dist=" << xx + yy << endl;
3941 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3943 //lyxerr << "InsetTabular::editXY: " << this << endl;
3944 cur.selection() = false;
3946 cur.idx() = getNearestCell(cur.bv(), x, y);
3948 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3952 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3954 cur.idx() = getNearestCell(cur.bv(), x, y);
3955 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3959 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3961 idx_type idx_min = 0;
3962 int dist_min = std::numeric_limits<int>::max();
3963 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3964 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3965 int const d = dist(bv, i, x, y);
3976 int InsetTabular::getCellXPos(idx_type const cell) const
3980 for (; !tabular.isFirstCellInRow(c); --c)
3983 for (; c < cell; ++c)
3984 lx += tabular.columnWidth(c);
3990 void InsetTabular::resetPos(Cursor & cur) const
3992 BufferView & bv = cur.bv();
3993 int const maxwidth = bv.workWidth();
3995 if (&cur.inset() != this) {
3999 int const X2 = maxwidth;
4000 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4001 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
4002 int const x2 = x1 + tabular.columnWidth(cur.idx());
4005 scx_ = X1 + 20 - x1;
4007 scx_ = X2 - 20 - x2;
4012 cur.updateFlags(Update::Force | Update::FitCursor);
4016 void InsetTabular::moveNextCell(Cursor & cur)
4018 if (isRightToLeft(cur)) {
4019 if (tabular.isFirstCellInRow(cur.idx())) {
4020 row_type const row = tabular.cellRow(cur.idx());
4021 if (row == tabular.rowCount() - 1)
4023 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4030 if (tabular.isLastCell(cur.idx()))
4040 void InsetTabular::movePrevCell(Cursor & cur)
4042 if (isRightToLeft(cur)) {
4043 if (tabular.isLastCellInRow(cur.idx())) {
4044 row_type const row = tabular.cellRow(cur.idx());
4047 cur.idx() = tabular.getFirstCellInRow(row);
4048 cur.idx() = tabular.getCellAbove(cur.idx());
4050 if (tabular.isLastCell(cur.idx()))
4055 if (cur.idx() == 0) // first cell
4059 cur.pit() = cur.lastpit();
4060 cur.pos() = cur.lastpos();
4062 // FIXME: this accesses the position cache before it is initialized
4067 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4069 Tabular::Feature action = Tabular::LAST_ACTION;
4072 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4073 string const tmp = tabularFeature[i].feature;
4075 if (tmp == what.substr(0, tmp.length())) {
4076 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4077 //tabularFeatures[i].feature.length()))
4078 action = tabularFeature[i].action;
4082 if (action == Tabular::LAST_ACTION)
4086 ltrim(what.substr(tabularFeature[i].feature.length()));
4087 tabularFeatures(cur, action, val);
4092 static void checkLongtableSpecial(Tabular::ltType & ltt,
4093 string const & special, bool & flag)
4095 if (special == "dl_above") {
4098 } else if (special == "dl_below") {
4099 ltt.bottomDL = flag;
4101 } else if (special == "empty") {
4110 bool InsetTabular::oneCellHasRotationState(bool rotated,
4111 row_type row_start, row_type row_end,
4112 col_type col_start, col_type col_end) const {
4114 for (row_type i = row_start; i <= row_end; ++i) {
4115 for (col_type j = col_start; j <= col_end; ++j) {
4116 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4125 void InsetTabular::tabularFeatures(Cursor & cur,
4126 Tabular::Feature feature, string const & value)
4128 BufferView & bv = cur.bv();
4129 col_type sel_col_start;
4130 col_type sel_col_end;
4131 row_type sel_row_start;
4132 row_type sel_row_end;
4133 bool setLines = false;
4134 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4135 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4139 case Tabular::M_ALIGN_LEFT:
4140 case Tabular::ALIGN_LEFT:
4141 setAlign = LYX_ALIGN_LEFT;
4144 case Tabular::M_ALIGN_RIGHT:
4145 case Tabular::ALIGN_RIGHT:
4146 setAlign = LYX_ALIGN_RIGHT;
4149 case Tabular::M_ALIGN_CENTER:
4150 case Tabular::ALIGN_CENTER:
4151 setAlign = LYX_ALIGN_CENTER;
4154 case Tabular::ALIGN_BLOCK:
4155 setAlign = LYX_ALIGN_BLOCK;
4158 case Tabular::M_VALIGN_TOP:
4159 case Tabular::VALIGN_TOP:
4160 setVAlign = Tabular::LYX_VALIGN_TOP;
4163 case Tabular::M_VALIGN_BOTTOM:
4164 case Tabular::VALIGN_BOTTOM:
4165 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4168 case Tabular::M_VALIGN_MIDDLE:
4169 case Tabular::VALIGN_MIDDLE:
4170 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4177 cur.recordUndoInset(ATOMIC_UNDO);
4179 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4180 row_type const row = tabular.cellRow(cur.idx());
4181 col_type const column = tabular.cellColumn(cur.idx());
4183 Tabular::ltType ltt;
4187 case Tabular::SET_PWIDTH: {
4188 Length const len(value);
4189 tabular.setColumnPWidth(cur, cur.idx(), len);
4191 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4192 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4196 case Tabular::SET_MPWIDTH:
4197 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4200 case Tabular::SET_SPECIAL_COLUMN:
4201 case Tabular::SET_SPECIAL_MULTI:
4202 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4205 case Tabular::APPEND_ROW:
4206 // append the row into the tabular
4207 tabular.appendRow(bv.buffer().params(), cur.idx());
4210 case Tabular::APPEND_COLUMN:
4211 // append the column into the tabular
4212 tabular.appendColumn(bv.buffer().params(), cur.idx());
4213 cur.idx() = tabular.cellIndex(row, column);
4216 case Tabular::DELETE_ROW:
4217 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4218 tabular.deleteRow(sel_row_start);
4219 if (sel_row_start >= tabular.rowCount())
4221 cur.idx() = tabular.cellIndex(sel_row_start, column);
4224 cur.selection() = false;
4227 case Tabular::DELETE_COLUMN:
4228 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4229 tabular.deleteColumn(sel_col_start);
4230 if (sel_col_start >= tabular.columnCount())
4232 cur.idx() = tabular.cellIndex(row, sel_col_start);
4235 cur.selection() = false;
4238 case Tabular::COPY_ROW:
4239 tabular.copyRow(bv.buffer().params(), row);
4242 case Tabular::COPY_COLUMN:
4243 tabular.copyColumn(bv.buffer().params(), column);
4244 cur.idx() = tabular.cellIndex(row, column);
4247 case Tabular::M_TOGGLE_LINE_TOP:
4249 case Tabular::TOGGLE_LINE_TOP: {
4250 bool lineSet = !tabular.topLine(cur.idx(), flag);
4251 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4252 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4254 tabular.cellIndex(i, j),
4259 case Tabular::M_TOGGLE_LINE_BOTTOM:
4261 case Tabular::TOGGLE_LINE_BOTTOM: {
4262 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4263 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4264 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4265 tabular.setBottomLine(
4266 tabular.cellIndex(i, j),
4272 case Tabular::M_TOGGLE_LINE_LEFT:
4274 case Tabular::TOGGLE_LINE_LEFT: {
4275 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4276 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4277 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4278 tabular.setLeftLine(
4279 tabular.cellIndex(i,j),
4285 case Tabular::M_TOGGLE_LINE_RIGHT:
4287 case Tabular::TOGGLE_LINE_RIGHT: {
4288 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4289 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4290 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4291 tabular.setRightLine(
4292 tabular.cellIndex(i,j),
4298 case Tabular::M_ALIGN_LEFT:
4299 case Tabular::M_ALIGN_RIGHT:
4300 case Tabular::M_ALIGN_CENTER:
4302 case Tabular::ALIGN_LEFT:
4303 case Tabular::ALIGN_RIGHT:
4304 case Tabular::ALIGN_CENTER:
4305 case Tabular::ALIGN_BLOCK:
4306 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4307 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4308 tabular.setAlignment(
4309 tabular.cellIndex(i, j),
4314 case Tabular::M_VALIGN_TOP:
4315 case Tabular::M_VALIGN_BOTTOM:
4316 case Tabular::M_VALIGN_MIDDLE:
4318 case Tabular::VALIGN_TOP:
4319 case Tabular::VALIGN_BOTTOM:
4320 case Tabular::VALIGN_MIDDLE:
4321 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4322 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4323 tabular.setVAlignment(
4324 tabular.cellIndex(i, j),
4328 case Tabular::MULTICOLUMN: {
4329 if (sel_row_start != sel_row_end) {
4330 // FIXME: Need I say it ? This is horrible.
4332 Alert::error(_("Error setting multicolumn"),
4333 _("You cannot set multicolumn vertically."));
4336 if (!cur.selection()) {
4337 // just multicol for one single cell
4338 // check whether we are completely in a multicol
4339 if (tabular.isMultiColumn(cur.idx()))
4340 tabular.unsetMultiColumn(cur.idx());
4342 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4345 // we have a selection so this means we just add all this
4346 // cells to form a multicolumn cell
4347 idx_type const s_start = cur.selBegin().idx();
4348 idx_type const s_end = cur.selEnd().idx();
4349 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4350 cur.idx() = s_start;
4353 cur.selection() = false;
4357 case Tabular::SET_ALL_LINES:
4359 case Tabular::UNSET_ALL_LINES:
4360 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4361 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4362 tabular.setAllLines(
4363 tabular.cellIndex(i,j), setLines);
4366 case Tabular::SET_LONGTABULAR:
4367 tabular.setLongTabular(true);
4370 case Tabular::UNSET_LONGTABULAR:
4371 tabular.setLongTabular(false);
4374 case Tabular::SET_ROTATE_TABULAR:
4375 tabular.setRotateTabular(true);
4378 case Tabular::UNSET_ROTATE_TABULAR:
4379 tabular.setRotateTabular(false);
4382 case Tabular::TOGGLE_ROTATE_TABULAR:
4383 tabular.setRotateTabular(!tabular.getRotateTabular());
4386 case Tabular::SET_ROTATE_CELL:
4387 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4388 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4389 tabular.setRotateCell(
4390 tabular.cellIndex(i, j), true);
4393 case Tabular::UNSET_ROTATE_CELL:
4394 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4395 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4396 tabular.setRotateCell(
4397 tabular.cellIndex(i, j), false);
4400 case Tabular::TOGGLE_ROTATE_CELL:
4402 bool oneNotRotated = oneCellHasRotationState(false,
4403 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4405 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4406 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4407 tabular.setRotateCell(tabular.cellIndex(i, j),
4412 case Tabular::SET_USEBOX: {
4413 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4414 if (val == tabular.getUsebox(cur.idx()))
4415 val = Tabular::BOX_NONE;
4416 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4417 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4418 tabular.setUsebox(tabular.cellIndex(i, j), val);
4422 case Tabular::UNSET_LTFIRSTHEAD:
4424 case Tabular::SET_LTFIRSTHEAD:
4425 tabular.getRowOfLTFirstHead(row, ltt);
4426 checkLongtableSpecial(ltt, value, flag);
4427 tabular.setLTHead(row, flag, ltt, true);
4430 case Tabular::UNSET_LTHEAD:
4432 case Tabular::SET_LTHEAD:
4433 tabular.getRowOfLTHead(row, ltt);
4434 checkLongtableSpecial(ltt, value, flag);
4435 tabular.setLTHead(row, flag, ltt, false);
4438 case Tabular::UNSET_LTFOOT:
4440 case Tabular::SET_LTFOOT:
4441 tabular.getRowOfLTFoot(row, ltt);
4442 checkLongtableSpecial(ltt, value, flag);
4443 tabular.setLTFoot(row, flag, ltt, false);
4446 case Tabular::UNSET_LTLASTFOOT:
4448 case Tabular::SET_LTLASTFOOT:
4449 tabular.getRowOfLTLastFoot(row, ltt);
4450 checkLongtableSpecial(ltt, value, flag);
4451 tabular.setLTFoot(row, flag, ltt, true);
4454 case Tabular::SET_LTNEWPAGE:
4455 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4458 case Tabular::SET_BOOKTABS:
4459 tabular.setBookTabs(true);
4462 case Tabular::UNSET_BOOKTABS:
4463 tabular.setBookTabs(false);
4466 case Tabular::SET_TOP_SPACE: {
4468 if (value == "default")
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4470 tabular.row_info[i].top_space_default = true;
4471 else if (isValidLength(value, &len))
4472 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4473 tabular.row_info[i].top_space_default = false;
4474 tabular.row_info[i].top_space = len;
4477 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4478 tabular.row_info[i].top_space_default = false;
4479 tabular.row_info[i].top_space = len;
4484 case Tabular::SET_BOTTOM_SPACE: {
4486 if (value == "default")
4487 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4488 tabular.row_info[i].bottom_space_default = true;
4489 else if (isValidLength(value, &len))
4490 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4491 tabular.row_info[i].bottom_space_default = false;
4492 tabular.row_info[i].bottom_space = len;
4495 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4496 tabular.row_info[i].bottom_space_default = false;
4497 tabular.row_info[i].bottom_space = len;
4502 case Tabular::SET_INTERLINE_SPACE: {
4504 if (value == "default")
4505 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4506 tabular.row_info[i].interline_space_default = true;
4507 else if (isValidLength(value, &len))
4508 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4509 tabular.row_info[i].interline_space_default = false;
4510 tabular.row_info[i].interline_space = len;
4513 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4514 tabular.row_info[i].interline_space_default = false;
4515 tabular.row_info[i].interline_space = len;
4520 // dummy stuff just to avoid warnings
4521 case Tabular::LAST_ACTION:
4527 bool InsetTabular::showInsetDialog(BufferView * bv) const
4529 InsetTabularMailer(*this).showDialog(bv);
4534 void InsetTabular::openLayoutDialog(BufferView * bv) const
4536 InsetTabularMailer(*this).showDialog(bv);
4540 bool InsetTabular::copySelection(Cursor & cur)
4542 if (!cur.selection())
4547 getSelection(cur, rs, re, cs, ce);
4549 paste_tabular.reset(new Tabular(tabular));
4551 for (row_type i = 0; i < rs; ++i)
4552 paste_tabular->deleteRow(0);
4554 row_type const rows = re - rs + 1;
4555 while (paste_tabular->rowCount() > rows)
4556 paste_tabular->deleteRow(rows);
4558 paste_tabular->setTopLine(0, true, true);
4559 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4562 for (col_type i = 0; i < cs; ++i)
4563 paste_tabular->deleteColumn(0);
4565 col_type const columns = ce - cs + 1;
4566 while (paste_tabular->columnCount() > columns)
4567 paste_tabular->deleteColumn(columns);
4569 paste_tabular->setLeftLine(0, true, true);
4570 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4573 odocstringstream os;
4574 OutputParams const runparams(0);
4575 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4576 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4577 cap::copySelection(cur, os.str());
4579 // mark tabular stack dirty
4580 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4581 // when we (hopefully) have a one-for-all paste mechanism.
4582 // This must be called after cap::copySelection.
4583 dirtyTabularStack(true);
4589 bool InsetTabular::pasteClipboard(Cursor & cur)
4593 col_type const actcol = tabular.cellColumn(cur.idx());
4594 row_type const actrow = tabular.cellRow(cur.idx());
4595 for (row_type r1 = 0, r2 = actrow;
4596 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4598 for (col_type c1 = 0, c2 = actcol;
4599 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4601 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4602 tabular.isPartOfMultiColumn(r2, c2))
4604 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4608 if (tabular.isPartOfMultiColumn(r2, c2)) {
4612 shared_ptr<InsetText> inset(
4613 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4614 tabular.setCellInset(r2, c2, inset);
4615 // FIXME: change tracking (MG)
4616 inset->setChange(Change(cur.buffer().params().trackChanges ?
4617 Change::INSERTED : Change::UNCHANGED));
4625 void InsetTabular::cutSelection(Cursor & cur)
4627 if (!cur.selection())
4632 getSelection(cur, rs, re, cs, ce);
4633 for (row_type i = rs; i <= re; ++i) {
4634 for (col_type j = cs; j <= ce; ++j) {
4635 shared_ptr<InsetText> t
4636 = cell(tabular.cellIndex(i, j));
4637 if (cur.buffer().params().trackChanges)
4638 // FIXME: Change tracking (MG)
4639 t->setChange(Change(Change::DELETED));
4645 // cursor position might be invalid now
4646 if (cur.pit() > cur.lastpit())
4647 cur.pit() = cur.lastpit();
4648 if (cur.pos() > cur.lastpos())
4649 cur.pos() = cur.lastpos();
4650 cur.clearSelection();
4654 bool InsetTabular::isRightToLeft(Cursor & cur) const
4656 BOOST_ASSERT(cur.depth() > 1);
4657 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4658 pos_type const parentpos = cur[cur.depth() - 2].pos();
4659 return parentpar.getFontSettings(cur.bv().buffer().params(),
4660 parentpos).language()->rightToLeft();
4664 void InsetTabular::getSelection(Cursor & cur,
4665 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4667 CursorSlice const & beg = cur.selBegin();
4668 CursorSlice const & end = cur.selEnd();
4669 cs = tabular.cellColumn(beg.idx());
4670 ce = tabular.cellColumn(end.idx());
4673 cs = tabular.cellColumn(end.idx());
4675 ce = tabular.cellRightColumn(end.idx());
4678 rs = tabular.cellRow(beg.idx());
4679 re = tabular.cellRow(end.idx());
4685 Text * InsetTabular::getText(int idx) const
4687 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4691 void InsetTabular::setChange(Change const & change)
4693 for (idx_type idx = 0; idx < nargs(); ++idx)
4694 cell(idx)->setChange(change);
4698 void InsetTabular::acceptChanges(BufferParams const & bparams)
4700 for (idx_type idx = 0; idx < nargs(); ++idx)
4701 cell(idx)->acceptChanges(bparams);
4705 void InsetTabular::rejectChanges(BufferParams const & bparams)
4707 for (idx_type idx = 0; idx < nargs(); ++idx)
4708 cell(idx)->rejectChanges(bparams);
4712 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4714 return tabular.getPWidth(cell).zero();
4718 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4721 if (buf.length() <= 0)
4724 Buffer const & buffer = bv.buffer();
4728 col_type maxCols = 1;
4729 docstring::size_type const len = buf.length();
4730 docstring::size_type p = 0;
4733 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4741 maxCols = max(cols, maxCols);
4747 maxCols = max(cols, maxCols);
4753 paste_tabular.reset(
4754 new Tabular(buffer.params(), rows, maxCols));
4755 loctab = paste_tabular.get();
4757 dirtyTabularStack(true);
4760 cell = bv.cursor().idx();
4761 ocol = tabular.cellColumn(cell);
4762 row = tabular.cellRow(cell);
4765 docstring::size_type op = 0;
4766 idx_type const cells = loctab->cellCount();
4769 rows = loctab->rowCount();
4770 col_type const columns = loctab->columnCount();
4772 while (cell < cells && p < len && row < rows &&
4773 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4779 // we can only set this if we are not too far right
4780 if (cols < columns) {
4781 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4782 Font const font = bv.textMetrics(&inset->text_).
4783 getDisplayFont(0, 0);
4784 inset->setText(buf.substr(op, p - op), font,
4785 buffer.params().trackChanges);
4791 // we can only set this if we are not too far right
4792 if (cols < columns) {
4793 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4794 Font const font = bv.textMetrics(&inset->text_).
4795 getDisplayFont(0, 0);
4796 inset->setText(buf.substr(op, p - op), font,
4797 buffer.params().trackChanges);
4802 cell = loctab->cellIndex(row, cols);
4808 // check for the last cell if there is no trailing '\n'
4809 if (cell < cells && op < len) {
4810 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4811 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4812 inset->setText(buf.substr(op, len - op), font,
4813 buffer.params().trackChanges);
4819 void InsetTabular::addPreview(PreviewLoader & loader) const
4821 row_type const rows = tabular.rowCount();
4822 col_type const columns = tabular.columnCount();
4823 for (row_type i = 0; i < rows; ++i) {
4824 for (col_type j = 0; j < columns; ++j)
4825 tabular.getCellInset(i, j)->addPreview(loader);
4830 bool InsetTabular::tablemode(Cursor & cur) const
4832 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4838 string const InsetTabularMailer::name_("tabular");
4840 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4841 : inset_(const_cast<InsetTabular &>(inset))
4845 string const InsetTabularMailer::inset2string(Buffer const &) const
4847 return params2string(inset_);
4851 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4853 istringstream data(in);
4855 lex.setStream(data);
4862 if (!lex || token != name_)
4863 return print_mailer_error("InsetTabularMailer", in, 1,
4866 // This is part of the inset proper that is usually swallowed
4867 // by Buffer::readInset
4869 if (!lex || token != "Tabular")
4870 return print_mailer_error("InsetTabularMailer", in, 2,
4873 Buffer const & buffer = inset.buffer();
4874 inset.read(buffer, lex);
4878 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4881 data << name_ << ' ';
4882 inset.write(inset.buffer(), data);
4883 data << "\\end_inset\n";