2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
38 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "paragraph_funcs.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextMetrics.h"
48 #include "support/convert.h"
49 #include "support/lstrings.h"
51 #include "frontends/alert.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
64 using std::istringstream;
67 using std::ostringstream;
72 #ifndef CXX_GLOBAL_CSTD
76 using boost::shared_ptr;
77 using boost::dynamic_pointer_cast;
81 using support::prefixIs;
84 using support::suffixIs;
86 using cap::dirtyTabularStack;
87 using cap::tabularStackDirty;
89 using graphics::PreviewLoader;
91 using frontend::Painter;
92 using frontend::Clipboard;
94 namespace Alert = frontend::Alert;
99 int const ADD_TO_HEIGHT = 2;
100 int const ADD_TO_TABULAR_WIDTH = 2;
101 int const default_line_space = 10;
102 int const WIDTH_OF_LINE = 5;
106 boost::scoped_ptr<Tabular> paste_tabular;
109 struct TabularFeature {
110 Tabular::Feature action;
115 TabularFeature tabularFeature[] =
117 { Tabular::APPEND_ROW, "append-row" },
118 { Tabular::APPEND_COLUMN, "append-column" },
119 { Tabular::DELETE_ROW, "delete-row" },
120 { Tabular::DELETE_COLUMN, "delete-column" },
121 { Tabular::COPY_ROW, "copy-row" },
122 { Tabular::COPY_COLUMN, "copy-column" },
123 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
124 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
125 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
126 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
127 { Tabular::ALIGN_LEFT, "align-left" },
128 { Tabular::ALIGN_RIGHT, "align-right" },
129 { Tabular::ALIGN_CENTER, "align-center" },
130 { Tabular::ALIGN_BLOCK, "align-block" },
131 { Tabular::VALIGN_TOP, "valign-top" },
132 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
133 { Tabular::VALIGN_MIDDLE, "valign-middle" },
134 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
135 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
136 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
137 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
138 { Tabular::M_ALIGN_LEFT, "m-align-left" },
139 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
140 { Tabular::M_ALIGN_CENTER, "m-align-center" },
141 { Tabular::M_VALIGN_TOP, "m-valign-top" },
142 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
143 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
144 { Tabular::MULTICOLUMN, "multicolumn" },
145 { Tabular::SET_ALL_LINES, "set-all-lines" },
146 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
147 { Tabular::SET_LONGTABULAR, "set-longtabular" },
148 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
149 { Tabular::SET_PWIDTH, "set-pwidth" },
150 { Tabular::SET_MPWIDTH, "set-mpwidth" },
151 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
152 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
153 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
154 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
155 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
156 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
157 { Tabular::SET_USEBOX, "set-usebox" },
158 { Tabular::SET_LTHEAD, "set-lthead" },
159 { Tabular::UNSET_LTHEAD, "unset-lthead" },
160 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
161 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
162 { Tabular::SET_LTFOOT, "set-ltfoot" },
163 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
164 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
165 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
166 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
167 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
168 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
169 { Tabular::SET_BOOKTABS, "set-booktabs" },
170 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
171 { Tabular::SET_TOP_SPACE, "set-top-space" },
172 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
173 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
174 { Tabular::LAST_ACTION, "" }
178 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
180 FeatureEqual(Tabular::Feature feature)
181 : feature_(feature) {}
182 bool operator()(TabularFeature const & tf) const {
183 return tf.action == feature_;
186 Tabular::Feature feature_;
191 string const write_attribute(string const & name, T const & t)
193 string const s = tostr(t);
194 return s.empty() ? s : " " + name + "=\"" + s + "\"";
198 string const write_attribute(string const & name, string const & t)
200 return t.empty() ? t : " " + name + "=\"" + t + "\"";
205 string const write_attribute(string const & name, docstring const & t)
207 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
212 string const write_attribute(string const & name, bool const & b)
214 // we write only true attribute values so we remove a bit of the
215 // file format bloat for tabulars.
216 return b ? write_attribute(name, convert<string>(b)) : string();
221 string const write_attribute(string const & name, int const & i)
223 // we write only true attribute values so we remove a bit of the
224 // file format bloat for tabulars.
225 return i ? write_attribute(name, convert<string>(i)) : string();
230 string const write_attribute(string const & name, Tabular::idx_type const & i)
232 // we write only true attribute values so we remove a bit of the
233 // file format bloat for tabulars.
234 return i ? write_attribute(name, convert<string>(i)) : string();
239 string const write_attribute(string const & name, Length const & value)
241 // we write only the value if we really have one same reson as above.
242 return value.zero() ? string() : write_attribute(name, value.asString());
246 string const tostr(LyXAlignment const & num)
251 case LYX_ALIGN_BLOCK:
255 case LYX_ALIGN_CENTER:
257 case LYX_ALIGN_RIGHT:
259 case LYX_ALIGN_LAYOUT:
261 case LYX_ALIGN_SPECIAL:
268 string const tostr(Tabular::VAlignment const & num)
271 case Tabular::LYX_VALIGN_TOP:
273 case Tabular::LYX_VALIGN_MIDDLE:
275 case Tabular::LYX_VALIGN_BOTTOM:
282 string const tostr(Tabular::BoxType const & num)
285 case Tabular::BOX_NONE:
287 case Tabular::BOX_PARBOX:
289 case Tabular::BOX_MINIPAGE:
296 // I would have liked a fromstr template a lot better. (Lgb)
297 bool string2type(string const str, LyXAlignment & num)
300 num = LYX_ALIGN_NONE;
301 else if (str == "block")
302 num = LYX_ALIGN_BLOCK;
303 else if (str == "left")
304 num = LYX_ALIGN_LEFT;
305 else if (str == "center")
306 num = LYX_ALIGN_CENTER;
307 else if (str == "right")
308 num = LYX_ALIGN_RIGHT;
315 bool string2type(string const str, Tabular::VAlignment & num)
318 num = Tabular::LYX_VALIGN_TOP;
319 else if (str == "middle" )
320 num = Tabular::LYX_VALIGN_MIDDLE;
321 else if (str == "bottom")
322 num = Tabular::LYX_VALIGN_BOTTOM;
329 bool string2type(string const str, Tabular::BoxType & num)
332 num = Tabular::BOX_NONE;
333 else if (str == "parbox")
334 num = Tabular::BOX_PARBOX;
335 else if (str == "minipage")
336 num = Tabular::BOX_MINIPAGE;
343 bool string2type(string const str, bool & num)
347 else if (str == "false")
355 bool getTokenValue(string const & str, char const * token, string & ret)
358 size_t token_length = strlen(token);
359 string::size_type pos = str.find(token);
361 if (pos == string::npos || pos + token_length + 1 >= str.length()
362 || str[pos + token_length] != '=')
364 pos += token_length + 1;
366 if (ch != '"' && ch != '\'') { // only read till next space
370 while (pos < str.length() - 1 && str[++pos] != ch)
377 bool getTokenValue(string const & str, char const * token, docstring & ret)
380 bool const success = getTokenValue(str, token, tmp);
381 ret = from_utf8(tmp);
386 bool getTokenValue(string const & str, char const * token, int & num)
390 if (!getTokenValue(str, token, tmp))
392 num = convert<int>(tmp);
397 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::VAlignment & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token,
413 Tabular::BoxType & num)
416 return getTokenValue(str, token, tmp) && string2type(tmp, num);
420 bool getTokenValue(string const & str, char const * token, bool & flag)
422 // set the flag always to false as this should be the default for bools
423 // not in the file-format.
426 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
430 bool getTokenValue(string const & str, char const * token, Length & len)
432 // set the length to be zero() as default as this it should be if not
433 // in the file format.
436 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
440 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
445 if (!getTokenValue(str, token, tmp))
447 if (tmp == "default") {
451 return isValidLength(tmp, &len);
455 void l_getline(istream & is, string & str)
458 while (str.empty()) {
460 if (!str.empty() && str[str.length() - 1] == '\r')
461 str.erase(str.length() - 1);
468 string const featureAsString(Tabular::Feature feature)
470 TabularFeature * end = tabularFeature +
471 sizeof(tabularFeature) / sizeof(TabularFeature);
472 TabularFeature * it = std::find_if(tabularFeature, end,
473 FeatureEqual(feature));
474 return (it == end) ? string() : it->feature;
479 /////////////////////////////////////////////////////////////////////
483 /////////////////////////////////////////////////////////////////////
486 Tabular::cellstruct::cellstruct(BufferParams const & bp)
489 multicolumn(Tabular::CELL_NORMAL),
490 alignment(LYX_ALIGN_CENTER),
491 valignment(LYX_VALIGN_TOP),
498 inset(new InsetText(bp))
502 Tabular::cellstruct::cellstruct(cellstruct const & cs)
505 multicolumn(cs.multicolumn),
506 alignment(cs.alignment),
507 valignment(cs.valignment),
508 top_line(cs.top_line),
509 bottom_line(cs.bottom_line),
510 left_line(cs.left_line),
511 right_line(cs.right_line),
514 align_special(cs.align_special),
516 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
520 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
527 void Tabular::cellstruct::swap(cellstruct & rhs)
529 std::swap(cellno, rhs.cellno);
530 std::swap(width, rhs.width);
531 std::swap(multicolumn, rhs.multicolumn);
532 std::swap(alignment, rhs.alignment);
533 std::swap(valignment, rhs.valignment);
534 std::swap(top_line, rhs.top_line);
535 std::swap(bottom_line, rhs.bottom_line);
536 std::swap(left_line, rhs.left_line);
537 std::swap(right_line, rhs.right_line);
538 std::swap(usebox, rhs.usebox);
539 std::swap(rotate, rhs.rotate);
540 std::swap(align_special, rhs.align_special);
541 p_width.swap(rhs.p_width);
542 inset.swap(rhs.inset);
546 Tabular::rowstruct::rowstruct()
551 top_space_default(false),
552 bottom_space_default(false),
553 interline_space_default(false),
562 Tabular::columnstruct::columnstruct()
563 : alignment(LYX_ALIGN_CENTER),
564 valignment(LYX_VALIGN_TOP),
572 Tabular::ltType::ltType()
585 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
586 col_type columns_arg)
588 init(bp, rows_arg, columns_arg);
592 // activates all lines and sets all widths to 0
593 void Tabular::init(BufferParams const & bp, row_type rows_arg,
594 col_type columns_arg)
596 row_info = row_vector(rows_arg);
597 column_info = column_vector(columns_arg);
598 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
599 row_info.reserve(10);
600 column_info.reserve(10);
601 cell_info.reserve(100);
603 for (row_type i = 0; i < rows_arg; ++i)
604 cell_info[i].back().right_line = true;
605 row_info.back().bottom_line = true;
606 row_info.front().bottom_line = true;
607 column_info.back().right_line = true;
608 is_long_tabular = false;
610 use_booktabs = false;
614 void Tabular::fixCellNums()
617 for (row_type i = 0; i < rowCount(); ++i) {
618 for (col_type j = 0; j < columnCount(); ++j) {
619 // When debugging it can be nice to set
621 cell_info[i][j].inset->setDrawFrame(false);
622 cell_info[i][j].cellno = cellno++;
624 cell_info[i].back().right_line = true;
631 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
633 row_type const row = cellRow(cell);
635 row_vector::iterator rit = row_info.begin() + row;
636 row_info.insert(rit, rowstruct());
637 // now set the values of the row before
638 row_info[row] = row_info[row + 1];
640 row_type const nrows = rowCount();
641 col_type const ncols = columnCount();
643 cell_vvector old(nrows - 1);
644 for (row_type i = 0; i < nrows - 1; ++i)
645 swap(cell_info[i], old[i]);
647 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
649 for (row_type i = 0; i <= row; ++i)
650 swap(cell_info[i], old[i]);
651 for (row_type i = row + 2; i < nrows; ++i)
652 swap(cell_info[i], old[i - 1]);
655 for (col_type j = 0; j < ncols; ++j)
656 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
662 void Tabular::deleteRow(row_type const row)
664 // Not allowed to delete last row
668 row_info.erase(row_info.begin() + row);
669 cell_info.erase(cell_info.begin() + row);
674 void Tabular::copyRow(BufferParams const & bp, row_type const row)
676 row_info.insert(row_info.begin() + row, row_info[row]);
677 cell_info.insert(cell_info.begin() + row, cell_info[row]);
680 for (col_type j = 0; j < columnCount(); ++j)
681 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
687 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
689 col_type const column = cellColumn(cell);
690 col_type const ncols = columnCount();
691 column_vector::iterator cit = column_info.begin() + column + 1;
692 column_info.insert(cit, columnstruct());
693 // set the column values of the column before
694 column_info[column + 1] = column_info[column];
696 for (row_type i = 0; i < rowCount(); ++i) {
697 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
699 // care about multicolumns
700 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
701 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
703 if (column + 2 >= ncols
704 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
705 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
708 for (row_type i = 0; i < rowCount(); ++i) {
709 cell_info[i][column + 1].inset->clear();
711 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
717 void Tabular::deleteColumn(col_type const column)
719 // Not allowed to delete last column
720 if (columnCount() == 1)
723 column_info.erase(column_info.begin() + column);
724 for (row_type i = 0; i < rowCount(); ++i) {
725 // Care about multicolumn cells
726 if (column + 1 < columnCount() &&
727 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
728 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
729 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
731 cell_info[i].erase(cell_info[i].begin() + column);
737 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
739 column_info.insert(column_info.begin() + column, column_info[column]);
741 for (row_type i = 0; i < rowCount(); ++i)
742 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
745 for (row_type i = 0; i < rowCount(); ++i)
746 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
751 void Tabular::updateIndexes()
754 // Count only non-multicol cells plus begin multicol
755 // cells, ignore non-begin multicol cells:
756 for (row_type row = 0; row < rowCount(); ++row) {
757 for (col_type column = 0; column < columnCount(); ++column) {
758 if (cell_info[row][column].multicolumn
759 != Tabular::CELL_PART_OF_MULTICOLUMN)
761 BOOST_ASSERT(numberofcells != 0);
762 cell_info[row][column].cellno =
767 rowofcell.resize(numberofcells);
768 columnofcell.resize(numberofcells);
773 c < numberofcells && row < rowCount() && column < columnCount();) {
775 columnofcell[c] = column;
779 } while (column < columnCount() &&
780 cell_info[row][column].multicolumn
781 == Tabular::CELL_PART_OF_MULTICOLUMN);
783 if (column == columnCount()) {
789 for (row_type row = 0; row < rowCount(); ++row) {
790 for (col_type column = 0; column < columnCount(); ++column) {
791 if (isPartOfMultiColumn(row,column))
793 cell_info[row][column].inset->setAutoBreakRows(
794 !getPWidth(cellIndex(row, column)).zero());
800 Tabular::idx_type Tabular::cellCount() const
802 return numberofcells;
806 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
808 row_type const row = cellRow(cell);
810 for (col_type i = 0; i < columnCount(); ++i)
811 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
817 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
819 if (!wholerow && isMultiColumn(cell) &&
820 !(use_booktabs && cellRow(cell) == 0))
821 return cellinfo_of_cell(cell).top_line;
822 return row_info[cellRow(cell)].top_line;
826 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
828 if (!wholerow && isMultiColumn(cell) &&
829 !(use_booktabs && isLastRow(cell)))
830 return cellinfo_of_cell(cell).bottom_line;
831 return row_info[cellRow(cell)].bottom_line;
835 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
839 if (!wholecolumn && isMultiColumn(cell) &&
840 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
841 return cellinfo_of_cell(cell).left_line;
842 return column_info[cellColumn(cell)].left_line;
846 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
850 if (!wholecolumn && isMultiColumn(cell) &&
851 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
852 return cellinfo_of_cell(cell).right_line;
853 return column_info[cellRightColumn(cell)].right_line;
857 bool Tabular::topAlreadyDrawn(idx_type cell) const
859 row_type row = cellRow(cell);
860 if (row > 0 && !getAdditionalHeight(row)) {
861 col_type column = cellColumn(cell);
864 && cell_info[row][column].multicolumn
865 == Tabular::CELL_PART_OF_MULTICOLUMN)
867 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
868 return row_info[row].bottom_line;
870 return cell_info[row][column].bottom_line;
876 bool Tabular::leftAlreadyDrawn(idx_type cell) const
878 col_type column = cellColumn(cell);
880 row_type row = cellRow(cell);
882 (cell_info[row][column].multicolumn ==
883 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
884 if (getAdditionalWidth(cell_info[row][column].cellno))
886 return rightLine(cell_info[row][column].cellno);
892 bool Tabular::isLastRow(idx_type cell) const
894 return cellRow(cell) == rowCount() - 1;
898 int Tabular::getAdditionalHeight(row_type row) const
900 if (!row || row >= rowCount())
906 for (col_type column = 0; column < columnCount() && bottom; ++column) {
907 switch (cell_info[row - 1][column].multicolumn) {
908 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
909 bottom = cell_info[row - 1][column].bottom_line;
911 case Tabular::CELL_NORMAL:
912 bottom = row_info[row - 1].bottom_line;
915 for (col_type column = 0; column < columnCount() && top; ++column) {
916 switch (cell_info[row][column].multicolumn) {
917 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
918 top = cell_info[row][column].top_line;
920 case Tabular::CELL_NORMAL:
921 top = row_info[row].top_line;
924 int const interline_space = row_info[row - 1].interline_space_default ?
926 row_info[row - 1].interline_space.inPixels(width());
928 return interline_space + WIDTH_OF_LINE;
929 return interline_space;
933 int Tabular::getAdditionalWidth(idx_type cell) const
935 // internally already set in setCellWidth
936 // used to get it back in text.cpp
937 col_type const col = cellRightColumn(cell);
938 row_type const row = cellRow(cell);
939 if (col < columnCount() - 1 && rightLine(cell) &&
940 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
942 return WIDTH_OF_LINE;
948 // returns the maximum over all rows
949 int Tabular::columnWidth(idx_type cell) const
951 col_type const column1 = cellColumn(cell);
952 col_type const column2 = cellRightColumn(cell);
954 for (col_type i = column1; i <= column2; ++i)
955 result += column_info[i].width;
960 int Tabular::width() const
963 for (col_type i = 0; i < columnCount(); ++i)
964 width += column_info[i].width;
969 // returns true if a complete update is necessary, otherwise false
970 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
972 if (!isMultiColumn(cell))
975 row_type const row = cellRow(cell);
976 col_type const column1 = cellColumn(cell);
977 col_type const column2 = cellRightColumn(cell);
978 int const old_val = cell_info[row][column2].width;
980 // first set columns to 0 so we can calculate the right width
981 for (col_type i = column1; i <= column2; ++i) {
982 cell_info[row][i].width = 0;
984 // set the width to MAX_WIDTH until width > 0
985 int width = new_width + 2 * WIDTH_OF_LINE;
986 col_type i = column1;
987 for (; i < column2 && width > column_info[i].width; ++i) {
988 cell_info[row][i].width = column_info[i].width;
989 width -= column_info[i].width;
992 cell_info[row][i].width = width;
994 if (old_val != cell_info[row][column2].width) {
995 // in this case we have to recalculate all multicolumn cells which
996 // have this column as one of theirs but not as last one
997 calculate_width_of_column_NMC(i);
998 recalculateMulticolumnsOfColumn(i);
999 calculate_width_of_column(i);
1005 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1007 // the last column does not have to be recalculated because all
1008 // multicolumns will have here there last multicolumn cell which
1009 // always will have the whole rest of the width of the cell.
1010 if (columnCount() < 2 || column > (columnCount() - 2))
1012 for (row_type row = 0; row < rowCount(); ++row) {
1013 int mc = cell_info[row][column].multicolumn;
1014 int nmc = cell_info[row][column+1].multicolumn;
1015 // we only have to update multicolumns which do not have this
1016 // column as their last column!
1017 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1018 (mc == CELL_PART_OF_MULTICOLUMN &&
1019 nmc == CELL_PART_OF_MULTICOLUMN))
1021 idx_type const cellno = cell_info[row][column].cellno;
1022 setWidthOfMulticolCell(cellno,
1023 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1029 void Tabular::setCellWidth(idx_type cell, int new_width)
1031 row_type const row = cellRow(cell);
1032 col_type const column1 = cellColumn(cell);
1037 if (rightLine(cell_info[row][column1].cellno, true) &&
1038 column1 < columnCount() - 1 &&
1039 leftLine(cell_info[row][column1+1].cellno, true))
1041 add_width = WIDTH_OF_LINE;
1044 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1047 if (isMultiColumnReal(cell)) {
1048 tmp = setWidthOfMulticolCell(cell, new_width);
1050 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1051 cell_info[row][column1].width = width;
1052 tmp = calculate_width_of_column_NMC(column1);
1054 recalculateMulticolumnsOfColumn(column1);
1057 for (col_type i = 0; i < columnCount(); ++i)
1058 calculate_width_of_column(i);
1063 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1066 if (!isMultiColumn(cell) || onlycolumn)
1067 column_info[cellColumn(cell)].alignment = align;
1069 cellinfo_of_cell(cell).alignment = align;
1073 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1076 if (!isMultiColumn(cell) || onlycolumn)
1077 column_info[cellColumn(cell)].valignment = align;
1079 cellinfo_of_cell(cell).valignment = align;
1086 * Allow line and paragraph breaks for fixed width cells or disallow them,
1087 * merge cell paragraphs and reset layout to standard for variable width
1090 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1092 inset->setAutoBreakRows(fixedWidth);
1096 // merge all paragraphs to one
1097 BufferParams const & bp = cur.bv().buffer().params();
1098 while (inset->paragraphs().size() > 1)
1099 mergeParagraph(bp, inset->paragraphs(), 0);
1103 // undo information has already been recorded
1104 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1105 bp.getTextClass().defaultLayoutName());
1112 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1113 Length const & width)
1115 col_type const j = cellColumn(cell);
1117 column_info[j].p_width = width;
1118 for (row_type i = 0; i < rowCount(); ++i) {
1119 idx_type const cell = cellIndex(i, j);
1120 // because of multicolumns
1121 toggleFixedWidth(cur, getCellInset(cell).get(),
1122 !getPWidth(cell).zero());
1124 // cur paragraph can become invalid after paragraphs were merged
1125 if (cur.pit() > cur.lastpit())
1126 cur.pit() = cur.lastpit();
1127 // cur position can become invalid after newlines were removed
1128 if (cur.pos() > cur.lastpos())
1129 cur.pos() = cur.lastpos();
1133 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1134 Length const & width)
1136 if (!isMultiColumn(cell))
1139 cellinfo_of_cell(cell).p_width = width;
1140 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1141 // cur paragraph can become invalid after paragraphs were merged
1142 if (cur.pit() > cur.lastpit())
1143 cur.pit() = cur.lastpit();
1144 // cur position can become invalid after newlines were removed
1145 if (cur.pos() > cur.lastpos())
1146 cur.pos() = cur.lastpos();
1151 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1152 Tabular::Feature what)
1154 if (what == SET_SPECIAL_MULTI)
1155 cellinfo_of_cell(cell).align_special = special;
1157 column_info[cellColumn(cell)].align_special = special;
1161 void Tabular::setAllLines(idx_type cell, bool line)
1163 setTopLine(cell, line);
1164 setBottomLine(cell, line);
1165 setRightLine(cell, line);
1166 setLeftLine(cell, line);
1170 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1172 row_type const row = cellRow(cell);
1173 if (wholerow || !isMultiColumn(cell))
1174 row_info[row].top_line = line;
1176 cellinfo_of_cell(cell).top_line = line;
1180 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1182 if (wholerow || !isMultiColumn(cell))
1183 row_info[cellRow(cell)].bottom_line = line;
1185 cellinfo_of_cell(cell).bottom_line = line;
1189 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1191 if (wholecolumn || !isMultiColumn(cell))
1192 column_info[cellColumn(cell)].left_line = line;
1194 cellinfo_of_cell(cell).left_line = line;
1198 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1200 if (wholecolumn || !isMultiColumn(cell))
1201 column_info[cellRightColumn(cell)].right_line = line;
1203 cellinfo_of_cell(cell).right_line = line;
1207 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1209 if (!onlycolumn && isMultiColumn(cell))
1210 return cellinfo_of_cell(cell).alignment;
1211 return column_info[cellColumn(cell)].alignment;
1216 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1218 if (!onlycolumn && isMultiColumn(cell))
1219 return cellinfo_of_cell(cell).valignment;
1220 return column_info[cellColumn(cell)].valignment;
1224 Length const Tabular::getPWidth(idx_type cell) const
1226 if (isMultiColumn(cell))
1227 return cellinfo_of_cell(cell).p_width;
1228 return column_info[cellColumn(cell)].p_width;
1232 Length const Tabular::getColumnPWidth(idx_type cell) const
1234 return column_info[cellColumn(cell)].p_width;
1238 Length const Tabular::getMColumnPWidth(idx_type cell) const
1240 if (isMultiColumn(cell))
1241 return cellinfo_of_cell(cell).p_width;
1246 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1248 if (what == SET_SPECIAL_MULTI)
1249 return cellinfo_of_cell(cell).align_special;
1250 return column_info[cellColumn(cell)].align_special;
1254 int Tabular::cellWidth(idx_type cell) const
1256 row_type const row = cellRow(cell);
1257 col_type const column1 = cellColumn(cell);
1258 col_type const column2 = cellRightColumn(cell);
1260 for (col_type i = column1; i <= column2; ++i)
1261 result += cell_info[row][i].width;
1266 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1270 switch (getAlignment(cell)) {
1271 case LYX_ALIGN_CENTER:
1272 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1274 case LYX_ALIGN_RIGHT:
1275 x += columnWidth(cell) - cellWidth(cell);
1276 // + getAdditionalWidth(cell);
1279 // LYX_ALIGN_LEFT: nothing :-)
1283 // the LaTeX Way :-(
1289 bool Tabular::isFirstCellInRow(idx_type cell) const
1291 return cellColumn(cell) == 0;
1295 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1297 if (row > rowCount() - 1)
1298 row = rowCount() - 1;
1299 return cell_info[row][0].cellno;
1303 bool Tabular::isLastCellInRow(idx_type cell) const
1305 return cellRightColumn(cell) == columnCount() - 1;
1309 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1311 if (row > rowCount() - 1)
1312 row = rowCount() - 1;
1313 return cell_info[row][columnCount() - 1].cellno;
1317 void Tabular::calculate_width_of_column(col_type column)
1320 for (row_type i = 0; i < rowCount(); ++i)
1321 maximum = max(cell_info[i][column].width, maximum);
1322 column_info[column].width = maximum;
1327 // Calculate the columns regarding ONLY the normal cells and if this
1328 // column is inside a multicolumn cell then use it only if its the last
1329 // column of this multicolumn cell as this gives an added width to the
1330 // column, all the rest should be adapted!
1332 bool Tabular::calculate_width_of_column_NMC(col_type column)
1334 int const old_column_width = column_info[column].width;
1336 for (row_type i = 0; i < rowCount(); ++i) {
1337 idx_type cell = cellIndex(i, column);
1338 bool ismulti = isMultiColumnReal(cell);
1339 if ((!ismulti || column == cellRightColumn(cell)) &&
1340 cell_info[i][column].width > max)
1342 max = cell_info[i][column].width;
1345 column_info[column].width = max;
1346 return column_info[column].width != old_column_width;
1350 Tabular::row_type Tabular::cellRow(idx_type cell) const
1352 if (cell >= cellCount())
1353 return rowCount() - 1;
1356 return rowofcell[cell];
1360 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1362 if (cell >= cellCount())
1363 return columnCount() - 1;
1366 return columnofcell[cell];
1370 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1372 row_type const row = cellRow(cell);
1373 col_type column = cellColumn(cell);
1374 while (column < columnCount() - 1 &&
1375 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1381 void Tabular::write(Buffer const & buf, ostream & os) const
1385 << write_attribute("version", 3)
1386 << write_attribute("rows", rowCount())
1387 << write_attribute("columns", columnCount())
1389 // global longtable options
1391 << write_attribute("rotate", rotate)
1392 << write_attribute("booktabs", use_booktabs)
1393 << write_attribute("islongtable", is_long_tabular)
1394 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1395 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1396 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1397 << write_attribute("headTopDL", endhead.topDL)
1398 << write_attribute("headBottomDL", endhead.bottomDL)
1399 << write_attribute("footTopDL", endfoot.topDL)
1400 << write_attribute("footBottomDL", endfoot.bottomDL)
1401 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1402 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1403 << write_attribute("lastFootEmpty", endlastfoot.empty)
1405 for (col_type j = 0; j < columnCount(); ++j) {
1407 << write_attribute("alignment", column_info[j].alignment)
1408 << write_attribute("valignment", column_info[j].valignment)
1409 << write_attribute("leftline", column_info[j].left_line)
1410 << write_attribute("rightline", column_info[j].right_line)
1411 << write_attribute("width", column_info[j].p_width.asString())
1412 << write_attribute("special", column_info[j].align_special)
1415 for (row_type i = 0; i < rowCount(); ++i) {
1417 << write_attribute("topline", row_info[i].top_line)
1418 << write_attribute("bottomline", row_info[i].bottom_line);
1419 static const string def("default");
1420 if (row_info[i].top_space_default)
1421 os << write_attribute("topspace", def);
1423 os << write_attribute("topspace", row_info[i].top_space);
1424 if (row_info[i].bottom_space_default)
1425 os << write_attribute("bottomspace", def);
1427 os << write_attribute("bottomspace", row_info[i].bottom_space);
1428 if (row_info[i].interline_space_default)
1429 os << write_attribute("interlinespace", def);
1431 os << write_attribute("interlinespace", row_info[i].interline_space);
1432 os << write_attribute("endhead", row_info[i].endhead)
1433 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1434 << write_attribute("endfoot", row_info[i].endfoot)
1435 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1436 << write_attribute("newpage", row_info[i].newpage)
1438 for (col_type j = 0; j < columnCount(); ++j) {
1440 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1441 << write_attribute("alignment", cell_info[i][j].alignment)
1442 << write_attribute("valignment", cell_info[i][j].valignment)
1443 << write_attribute("topline", cell_info[i][j].top_line)
1444 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1445 << write_attribute("leftline", cell_info[i][j].left_line)
1446 << write_attribute("rightline", cell_info[i][j].right_line)
1447 << write_attribute("rotate", cell_info[i][j].rotate)
1448 << write_attribute("usebox", cell_info[i][j].usebox)
1449 << write_attribute("width", cell_info[i][j].p_width)
1450 << write_attribute("special", cell_info[i][j].align_special)
1452 os << "\\begin_inset ";
1453 cell_info[i][j].inset->write(buf, os);
1454 os << "\n\\end_inset\n"
1459 os << "</lyxtabular>\n";
1463 void Tabular::read(Buffer const & buf, Lexer & lex)
1466 istream & is = lex.getStream();
1468 l_getline(is, line);
1469 if (!prefixIs(line, "<lyxtabular ")
1470 && !prefixIs(line, "<Tabular ")) {
1471 BOOST_ASSERT(false);
1476 if (!getTokenValue(line, "version", version))
1478 BOOST_ASSERT(version >= 2);
1481 if (!getTokenValue(line, "rows", rows_arg))
1484 if (!getTokenValue(line, "columns", columns_arg))
1486 init(buf.params(), rows_arg, columns_arg);
1487 l_getline(is, line);
1488 if (!prefixIs(line, "<features")) {
1489 lyxerr << "Wrong tabular format (expected <features ...> got"
1490 << line << ')' << endl;
1493 getTokenValue(line, "rotate", rotate);
1494 getTokenValue(line, "booktabs", use_booktabs);
1495 getTokenValue(line, "islongtable", is_long_tabular);
1496 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1497 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1498 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1499 getTokenValue(line, "headTopDL", endhead.topDL);
1500 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1501 getTokenValue(line, "footTopDL", endfoot.topDL);
1502 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1503 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1504 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1505 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1507 for (col_type j = 0; j < columnCount(); ++j) {
1509 if (!prefixIs(line,"<column")) {
1510 lyxerr << "Wrong tabular format (expected <column ...> got"
1511 << line << ')' << endl;
1514 getTokenValue(line, "alignment", column_info[j].alignment);
1515 getTokenValue(line, "valignment", column_info[j].valignment);
1516 getTokenValue(line, "leftline", column_info[j].left_line);
1517 getTokenValue(line, "rightline", column_info[j].right_line);
1518 getTokenValue(line, "width", column_info[j].p_width);
1519 getTokenValue(line, "special", column_info[j].align_special);
1522 for (row_type i = 0; i < rowCount(); ++i) {
1523 l_getline(is, line);
1524 if (!prefixIs(line, "<row")) {
1525 lyxerr << "Wrong tabular format (expected <row ...> got"
1526 << line << ')' << endl;
1529 getTokenValue(line, "topline", row_info[i].top_line);
1530 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1531 getTokenValue(line, "topspace", row_info[i].top_space,
1532 row_info[i].top_space_default);
1533 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1534 row_info[i].bottom_space_default);
1535 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1536 row_info[i].interline_space_default);
1537 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1538 getTokenValue(line, "endhead", row_info[i].endhead);
1539 getTokenValue(line, "endfoot", row_info[i].endfoot);
1540 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1541 getTokenValue(line, "newpage", row_info[i].newpage);
1542 for (col_type j = 0; j < columnCount(); ++j) {
1543 l_getline(is, line);
1544 if (!prefixIs(line, "<cell")) {
1545 lyxerr << "Wrong tabular format (expected <cell ...> got"
1546 << line << ')' << endl;
1549 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1550 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1551 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1552 getTokenValue(line, "topline", cell_info[i][j].top_line);
1553 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1554 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1555 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1556 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1557 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1558 getTokenValue(line, "width", cell_info[i][j].p_width);
1559 getTokenValue(line, "special", cell_info[i][j].align_special);
1560 l_getline(is, line);
1561 if (prefixIs(line, "\\begin_inset")) {
1562 cell_info[i][j].inset->read(buf, lex);
1563 l_getline(is, line);
1565 if (!prefixIs(line, "</cell>")) {
1566 lyxerr << "Wrong tabular format (expected </cell> got"
1567 << line << ')' << endl;
1571 l_getline(is, line);
1572 if (!prefixIs(line, "</row>")) {
1573 lyxerr << "Wrong tabular format (expected </row> got"
1574 << line << ')' << endl;
1578 while (!prefixIs(line, "</lyxtabular>")) {
1579 l_getline(is, line);
1585 bool Tabular::isMultiColumn(idx_type cell) const
1587 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1591 bool Tabular::isMultiColumnReal(idx_type cell) const
1593 return cellColumn(cell) != cellRightColumn(cell) &&
1594 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1598 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1600 return cell_info[cellRow(cell)][cellColumn(cell)];
1604 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1607 cellstruct & cs = cellinfo_of_cell(cell);
1608 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1609 cs.alignment = column_info[cellColumn(cell)].alignment;
1610 cs.top_line = row_info[cellRow(cell)].top_line;
1611 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1612 cs.left_line = column_info[cellColumn(cell)].left_line;
1613 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1614 for (idx_type i = 1; i < number; ++i) {
1615 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1616 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1617 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1624 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1626 row_type const row = cellRow(cell);
1627 col_type column = cellColumn(cell);
1628 idx_type result = 1;
1630 while (column < columnCount() &&
1631 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1640 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1642 row_type const row = cellRow(cell);
1643 col_type column = cellColumn(cell);
1645 idx_type result = 0;
1647 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1648 cell_info[row][column].multicolumn = CELL_NORMAL;
1650 while (column < columnCount() &&
1651 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1653 cell_info[row][column].multicolumn = CELL_NORMAL;
1663 void Tabular::setBookTabs(bool what)
1665 use_booktabs = what;
1669 bool Tabular::useBookTabs() const
1671 return use_booktabs;
1675 void Tabular::setLongTabular(bool what)
1677 is_long_tabular = what;
1681 bool Tabular::isLongTabular() const
1683 return is_long_tabular;
1687 void Tabular::setRotateTabular(bool flag)
1693 bool Tabular::getRotateTabular() const
1699 void Tabular::setRotateCell(idx_type cell, bool flag)
1701 cellinfo_of_cell(cell).rotate = flag;
1705 bool Tabular::getRotateCell(idx_type cell) const
1707 return cellinfo_of_cell(cell).rotate;
1711 bool Tabular::needRotating() const
1715 for (row_type i = 0; i < rowCount(); ++i)
1716 for (col_type j = 0; j < columnCount(); ++j)
1717 if (cell_info[i][j].rotate)
1723 bool Tabular::isLastCell(idx_type cell) const
1725 if (cell + 1 < cellCount())
1731 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1733 if (cellRow(cell) > 0)
1734 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1739 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1741 if (cellRow(cell) + 1 < rowCount())
1742 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1747 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1749 if (cellRow(cell) == 0)
1751 if (!isMultiColumn(cell))
1752 return getCellAbove(cell);
1753 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1757 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1759 if (cellRow(cell) + 1 >= rowCount())
1761 if (!isMultiColumn(cell))
1762 return getCellBelow(cell);
1763 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1767 Tabular::idx_type Tabular::cellIndex(row_type row,
1768 col_type column) const
1770 BOOST_ASSERT(column != npos && column < columnCount() &&
1771 row != npos && row < rowCount());
1772 return cell_info[row][column].cellno;
1776 void Tabular::setUsebox(idx_type cell, BoxType type)
1778 cellinfo_of_cell(cell).usebox = type;
1782 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1784 if (column_info[cellColumn(cell)].p_width.zero() &&
1785 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1787 if (cellinfo_of_cell(cell).usebox > 1)
1788 return cellinfo_of_cell(cell).usebox;
1789 return useParbox(cell);
1794 // This are functions used for the longtable support
1796 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1802 row_info[row].endfirsthead = flag;
1806 row_info[row].endhead = flag;
1811 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1814 hd.set = haveLTHead();
1815 return row_info[row].endhead;
1819 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1822 hd.set = haveLTFirstHead();
1823 return row_info[row].endfirsthead;
1827 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1833 row_info[row].endlastfoot = flag;
1837 row_info[row].endfoot = flag;
1842 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1845 fd.set = haveLTFoot();
1846 return row_info[row].endfoot;
1850 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1853 fd.set = haveLTLastFoot();
1854 return row_info[row].endlastfoot;
1858 void Tabular::setLTNewPage(row_type row, bool what)
1860 row_info[row].newpage = what;
1864 bool Tabular::getLTNewPage(row_type row) const
1866 return row_info[row].newpage;
1870 bool Tabular::haveLTHead() const
1872 for (row_type i = 0; i < rowCount(); ++i)
1873 if (row_info[i].endhead)
1879 bool Tabular::haveLTFirstHead() const
1881 if (endfirsthead.empty)
1883 for (row_type i = 0; i < rowCount(); ++i)
1884 if (row_info[i].endfirsthead)
1890 bool Tabular::haveLTFoot() const
1892 for (row_type i = 0; i < rowCount(); ++i)
1893 if (row_info[i].endfoot)
1899 bool Tabular::haveLTLastFoot() const
1901 if (endlastfoot.empty)
1903 for (row_type i = 0; i < rowCount(); ++i)
1904 if (row_info[i].endlastfoot)
1910 // end longtable support functions
1912 void Tabular::setRowAscent(row_type row, int height)
1914 if (row >= rowCount() || row_info[row].ascent == height)
1916 row_info[row].ascent = height;
1920 void Tabular::setRowDescent(row_type row, int height)
1922 if (row >= rowCount() || row_info[row].descent == height)
1924 row_info[row].descent = height;
1928 int Tabular::rowAscent(row_type row) const
1930 if (row >= rowCount())
1932 return row_info[row].ascent;
1936 int Tabular::rowDescent(row_type row) const
1938 BOOST_ASSERT(row < rowCount());
1939 return row_info[row].descent;
1943 int Tabular::height() const
1946 for (row_type row = 0; row < rowCount(); ++row)
1947 height += rowAscent(row) + rowDescent(row) +
1948 getAdditionalHeight(row);
1953 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1955 BOOST_ASSERT(row < rowCount());
1956 BOOST_ASSERT(column < columnCount());
1957 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1961 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1963 // FIXME: assert or return 0 as in TeXBottomHLine()?
1964 BOOST_ASSERT(row != npos);
1965 BOOST_ASSERT(row < rowCount());
1967 idx_type const fcell = getFirstCellInRow(row);
1968 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1971 for (idx_type i = fcell; i < n; ++i) {
1975 if (use_booktabs && row == 0) {
1977 } else if (tmp == n - fcell) {
1978 os << (use_booktabs ? "\\midrule " : "\\hline ");
1980 for (idx_type i = fcell; i < n; ++i) {
1982 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1983 << cellColumn(i) + 1
1985 << cellRightColumn(i) + 1
1997 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1999 // FIXME: return 0 or assert as in TeXTopHLine()?
2000 if (row == npos || row >= rowCount())
2003 idx_type const fcell = getFirstCellInRow(row);
2004 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2007 for (idx_type i = fcell; i < n; ++i) {
2011 if (use_booktabs && row == rowCount() - 1) {
2012 os << "\\bottomrule";
2013 } else if (tmp == n - fcell) {
2014 os << (use_booktabs ? "\\midrule" : "\\hline");
2016 for (idx_type i = fcell; i < n; ++i) {
2017 if (bottomLine(i)) {
2018 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2019 << cellColumn(i) + 1
2021 << cellRightColumn(i) + 1
2033 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2037 if (getRotateCell(cell)) {
2038 os << "\\begin{sideways}\n";
2041 if (isMultiColumn(cell)) {
2042 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2043 if (leftLine(cell) &&
2044 (isFirstCellInRow(cell) ||
2045 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2046 !rightLine(cell - 1, true))))
2048 if (!cellinfo_of_cell(cell).align_special.empty()) {
2049 os << cellinfo_of_cell(cell).align_special;
2051 if (!getPWidth(cell).zero()) {
2052 switch (getVAlignment(cell)) {
2053 case LYX_VALIGN_TOP:
2056 case LYX_VALIGN_MIDDLE:
2059 case LYX_VALIGN_BOTTOM:
2064 << from_ascii(getPWidth(cell).asLatexString())
2067 switch (getAlignment(cell)) {
2068 case LYX_ALIGN_LEFT:
2071 case LYX_ALIGN_RIGHT:
2078 } // end if else !getPWidth
2079 } // end if else !cellinfo_of_cell
2080 if (rightLine(cell))
2082 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2087 if (getUsebox(cell) == BOX_PARBOX) {
2089 switch (getVAlignment(cell)) {
2090 case LYX_VALIGN_TOP:
2093 case LYX_VALIGN_MIDDLE:
2096 case LYX_VALIGN_BOTTOM:
2100 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2102 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2103 os << "\\begin{minipage}[";
2104 switch (getVAlignment(cell)) {
2105 case LYX_VALIGN_TOP:
2108 case LYX_VALIGN_MIDDLE:
2111 case LYX_VALIGN_BOTTOM:
2115 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2123 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2128 if (getUsebox(cell) == BOX_PARBOX)
2130 else if (getUsebox(cell) == BOX_MINIPAGE) {
2131 os << "%\n\\end{minipage}";
2134 if (isMultiColumn(cell)) {
2137 if (getRotateCell(cell)) {
2138 os << "%\n\\end{sideways}";
2145 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2146 OutputParams const & runparams) const
2148 if (!is_long_tabular)
2152 // output header info
2154 if (endhead.topDL) {
2158 for (row_type i = 0; i < rowCount(); ++i) {
2159 if (row_info[i].endhead) {
2160 ret += TeXRow(os, i, buf, runparams);
2163 if (endhead.bottomDL) {
2167 os << "\\endhead\n";
2169 if (endfirsthead.empty) {
2170 os << "\\endfirsthead\n";
2174 // output firstheader info
2175 if (haveLTFirstHead()) {
2176 if (endfirsthead.topDL) {
2180 for (row_type i = 0; i < rowCount(); ++i) {
2181 if (row_info[i].endfirsthead) {
2182 ret += TeXRow(os, i, buf, runparams);
2185 if (endfirsthead.bottomDL) {
2189 os << "\\endfirsthead\n";
2192 // output footer info
2194 if (endfoot.topDL) {
2198 for (row_type i = 0; i < rowCount(); ++i) {
2199 if (row_info[i].endfoot) {
2200 ret += TeXRow(os, i, buf, runparams);
2203 if (endfoot.bottomDL) {
2207 os << "\\endfoot\n";
2209 if (endlastfoot.empty) {
2210 os << "\\endlastfoot\n";
2214 // output lastfooter info
2215 if (haveLTLastFoot()) {
2216 if (endlastfoot.topDL) {
2220 for (row_type i = 0; i < rowCount(); ++i) {
2221 if (row_info[i].endlastfoot) {
2222 ret += TeXRow(os, i, buf, runparams);
2225 if (endlastfoot.bottomDL) {
2229 os << "\\endlastfoot\n";
2236 bool Tabular::isValidRow(row_type row) const
2238 if (!is_long_tabular)
2240 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2241 !row_info[row].endfoot && !row_info[row].endlastfoot;
2245 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2246 OutputParams const & runparams) const
2248 idx_type cell = cellIndex(i, 0);
2249 int ret = TeXTopHLine(os, i);
2250 if (row_info[i].top_space_default) {
2252 os << "\\addlinespace\n";
2254 os << "\\noalign{\\vskip\\doublerulesep}\n";
2256 } else if(!row_info[i].top_space.zero()) {
2258 os << "\\addlinespace["
2259 << from_ascii(row_info[i].top_space.asLatexString())
2262 os << "\\noalign{\\vskip"
2263 << from_ascii(row_info[i].top_space.asLatexString())
2269 for (col_type j = 0; j < columnCount(); ++j) {
2270 if (isPartOfMultiColumn(i, j))
2272 ret += TeXCellPreamble(os, cell);
2273 shared_ptr<InsetText> inset = getCellInset(cell);
2275 Paragraph const & par = inset->paragraphs().front();
2276 bool rtl = par.isRTL(buf.params())
2278 && getPWidth(cell).zero();
2281 if (par.getParLanguage(buf.params())->lang() ==
2284 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2286 // currently, remaning RTL languages are arabic_arabtex and hebrew
2290 ret += inset->latex(buf, os, runparams);
2294 ret += TeXCellPostamble(os, cell);
2295 if (!isLastCellInRow(cell)) { // not last cell in row
2300 os << "\\tabularnewline";
2301 if (row_info[i].bottom_space_default) {
2303 os << "\\addlinespace";
2305 os << "[\\doublerulesep]";
2306 } else if (!row_info[i].bottom_space.zero()) {
2308 os << "\\addlinespace";
2310 << from_ascii(row_info[i].bottom_space.asLatexString())
2315 ret += TeXBottomHLine(os, i);
2316 if (row_info[i].interline_space_default) {
2318 os << "\\addlinespace\n";
2320 os << "\\noalign{\\vskip\\doublerulesep}\n";
2322 } else if (!row_info[i].interline_space.zero()) {
2324 os << "\\addlinespace["
2325 << from_ascii(row_info[i].interline_space.asLatexString())
2328 os << "\\noalign{\\vskip"
2329 << from_ascii(row_info[i].interline_space.asLatexString())
2337 int Tabular::latex(Buffer const & buf, odocstream & os,
2338 OutputParams const & runparams) const
2342 //+---------------------------------------------------------------------
2343 //+ first the opening preamble +
2344 //+---------------------------------------------------------------------
2347 os << "\\begin{sideways}\n";
2350 if (is_long_tabular)
2351 os << "\\begin{longtable}{";
2353 os << "\\begin{tabular}{";
2354 for (col_type i = 0; i < columnCount(); ++i) {
2355 if (!use_booktabs && column_info[i].left_line)
2357 if (!column_info[i].align_special.empty()) {
2358 os << column_info[i].align_special;
2360 if (!column_info[i].p_width.zero()) {
2361 switch (column_info[i].alignment) {
2362 case LYX_ALIGN_LEFT:
2363 os << ">{\\raggedright}";
2365 case LYX_ALIGN_RIGHT:
2366 os << ">{\\raggedleft}";
2368 case LYX_ALIGN_CENTER:
2369 os << ">{\\centering}";
2371 case LYX_ALIGN_NONE:
2372 case LYX_ALIGN_BLOCK:
2373 case LYX_ALIGN_LAYOUT:
2374 case LYX_ALIGN_SPECIAL:
2378 switch (column_info[i].valignment) {
2379 case LYX_VALIGN_TOP:
2382 case LYX_VALIGN_MIDDLE:
2385 case LYX_VALIGN_BOTTOM:
2390 << from_ascii(column_info[i].p_width.asLatexString())
2393 switch (column_info[i].alignment) {
2394 case LYX_ALIGN_LEFT:
2397 case LYX_ALIGN_RIGHT:
2404 } // end if else !column_info[i].p_width
2405 } // end if else !column_info[i].align_special
2406 if (!use_booktabs && column_info[i].right_line)
2412 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2414 //+---------------------------------------------------------------------
2415 //+ the single row and columns (cells) +
2416 //+---------------------------------------------------------------------
2418 for (row_type i = 0; i < rowCount(); ++i) {
2419 if (isValidRow(i)) {
2420 ret += TeXRow(os, i, buf, runparams);
2421 if (is_long_tabular && row_info[i].newpage) {
2422 os << "\\newpage\n";
2428 //+---------------------------------------------------------------------
2429 //+ the closing of the tabular +
2430 //+---------------------------------------------------------------------
2432 if (is_long_tabular)
2433 os << "\\end{longtable}";
2435 os << "\\end{tabular}";
2437 os << "\n\\end{sideways}";
2445 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2446 OutputParams const & runparams) const
2449 idx_type cell = getFirstCellInRow(row);
2452 for (col_type j = 0; j < columnCount(); ++j) {
2453 if (isPartOfMultiColumn(row, j))
2456 os << "<entry align=\"";
2457 switch (getAlignment(cell)) {
2458 case LYX_ALIGN_LEFT:
2461 case LYX_ALIGN_RIGHT:
2469 os << "\" valign=\"";
2470 switch (getVAlignment(cell)) {
2471 case LYX_VALIGN_TOP:
2474 case LYX_VALIGN_BOTTOM:
2477 case LYX_VALIGN_MIDDLE:
2482 if (isMultiColumn(cell)) {
2483 os << " namest=\"col" << j << "\" ";
2484 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2488 ret += getCellInset(cell)->docbook(buf, os, runparams);
2497 int Tabular::docbook(Buffer const & buf, odocstream & os,
2498 OutputParams const & runparams) const
2502 //+---------------------------------------------------------------------
2503 //+ first the opening preamble +
2504 //+---------------------------------------------------------------------
2506 os << "<tgroup cols=\"" << columnCount()
2507 << "\" colsep=\"1\" rowsep=\"1\">\n";
2509 for (col_type i = 0; i < columnCount(); ++i) {
2510 os << "<colspec colname=\"col" << i << "\" align=\"";
2511 switch (column_info[i].alignment) {
2512 case LYX_ALIGN_LEFT:
2515 case LYX_ALIGN_RIGHT:
2523 if (runparams.flavor == OutputParams::XML)
2529 //+---------------------------------------------------------------------
2530 //+ Long Tabular case +
2531 //+---------------------------------------------------------------------
2533 // output header info
2534 if (haveLTHead() || haveLTFirstHead()) {
2537 for (row_type i = 0; i < rowCount(); ++i) {
2538 if (row_info[i].endhead || row_info[i].endfirsthead) {
2539 ret += docbookRow(buf, os, i, runparams);
2545 // output footer info
2546 if (haveLTFoot() || haveLTLastFoot()) {
2549 for (row_type i = 0; i < rowCount(); ++i) {
2550 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2551 ret += docbookRow(buf, os, i, runparams);
2558 //+---------------------------------------------------------------------
2559 //+ the single row and columns (cells) +
2560 //+---------------------------------------------------------------------
2564 for (row_type i = 0; i < rowCount(); ++i) {
2565 if (isValidRow(i)) {
2566 ret += docbookRow(buf, os, i, runparams);
2571 //+---------------------------------------------------------------------
2572 //+ the closing of the tabular +
2573 //+---------------------------------------------------------------------
2582 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2583 vector<unsigned int> const & clen) const
2585 idx_type const fcell = getFirstCellInRow(row);
2586 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2589 for (idx_type i = fcell; i < n; ++i) {
2599 for (idx_type i = fcell; i < n; ++i) {
2610 col_type column = cellColumn(i);
2611 int len = clen[column];
2612 while (column < columnCount() - 1
2613 && isPartOfMultiColumn(row, ++column))
2614 len += clen[column] + 4;
2615 os << docstring(len, ch);
2630 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2631 vector<unsigned int> const & clen) const
2633 idx_type const fcell = getFirstCellInRow(row);
2634 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2637 for (idx_type i = fcell; i < n; ++i) {
2638 if (bottomLine(i)) {
2647 for (idx_type i = fcell; i < n; ++i) {
2648 if (bottomLine(i)) {
2658 col_type column = cellColumn(i);
2659 int len = clen[column];
2660 while (column < columnCount() -1
2661 && isPartOfMultiColumn(row, ++column))
2662 len += clen[column] + 4;
2663 os << docstring(len, ch);
2664 if (bottomLine(i)) {
2678 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2679 OutputParams const & runparams,
2680 idx_type cell, row_type row, col_type column,
2681 vector<unsigned int> const & clen,
2682 bool onlydata) const
2684 odocstringstream sstr;
2685 getCellInset(cell)->plaintext(buf, sstr, runparams);
2697 unsigned int len1 = sstr.str().length();
2698 unsigned int len2 = clen[column];
2699 while (column < columnCount() -1
2700 && isPartOfMultiColumn(row, ++column))
2701 len2 += clen[column] + 4;
2704 switch (getAlignment(cell)) {
2706 case LYX_ALIGN_LEFT:
2709 case LYX_ALIGN_RIGHT:
2713 case LYX_ALIGN_CENTER:
2719 os << docstring(len1, ' ') << sstr.str()
2720 << docstring(len2, ' ');
2722 if (rightLine(cell))
2729 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2730 OutputParams const & runparams, int const depth,
2731 bool onlydata, unsigned char delim) const
2733 // first calculate the width of the single columns
2734 vector<unsigned int> clen(columnCount());
2737 // first all non (real) multicolumn cells!
2738 for (col_type j = 0; j < columnCount(); ++j) {
2740 for (row_type i = 0; i < rowCount(); ++i) {
2741 idx_type cell = cellIndex(i, j);
2742 if (isMultiColumnReal(cell))
2744 odocstringstream sstr;
2745 getCellInset(cell)->plaintext(buf, sstr, runparams);
2746 if (clen[j] < sstr.str().length())
2747 clen[j] = sstr.str().length();
2750 // then all (real) multicolumn cells!
2751 for (col_type j = 0; j < columnCount(); ++j) {
2752 for (row_type i = 0; i < rowCount(); ++i) {
2753 idx_type cell = cellIndex(i, j);
2754 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2756 odocstringstream sstr;
2757 getCellInset(cell)->plaintext(buf, sstr, runparams);
2758 int len = int(sstr.str().length());
2759 idx_type const n = cells_in_multicolumn(cell);
2760 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2762 if (len > int(clen[j + n - 1]))
2763 clen[j + n - 1] = len;
2768 for (row_type i = 0; i < rowCount(); ++i) {
2769 if (!onlydata && plaintextTopHLine(os, i, clen))
2770 os << docstring(depth * 2, ' ');
2771 for (col_type j = 0; j < columnCount(); ++j) {
2772 if (isPartOfMultiColumn(i, j))
2774 if (onlydata && j > 0)
2776 plaintextPrintCell(buf, os, runparams,
2777 cell, i, j, clen, onlydata);
2782 os << docstring(depth * 2, ' ');
2783 if (plaintextBottomHLine(os, i, clen))
2784 os << docstring(depth * 2, ' ');
2790 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2792 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2796 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2797 col_type column) const
2799 return cell_info[row][column].inset;
2803 void Tabular::setCellInset(row_type row, col_type column,
2804 shared_ptr<InsetText> ins) const
2806 cell_info[row][column].inset = ins;
2811 Tabular::getCellFromInset(Inset const * inset) const
2813 // is this inset part of the tabular?
2815 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2816 BOOST_ASSERT(false);
2819 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2820 if (getCellInset(cell).get() == inset) {
2821 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2822 << "cell=" << cell << endl;
2826 // We should have found a cell at this point
2827 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2828 << inset << " not found!" << endl;
2829 BOOST_ASSERT(false);
2835 void Tabular::validate(LaTeXFeatures & features) const
2837 features.require("NeedTabularnewline");
2839 features.require("booktabs");
2840 if (isLongTabular())
2841 features.require("longtable");
2843 features.require("rotating");
2844 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2845 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2846 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2847 features.require("array");
2848 getCellInset(cell)->validate(features);
2853 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2855 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2856 ParagraphList::const_iterator cit = parlist.begin();
2857 ParagraphList::const_iterator end = parlist.end();
2859 for (; cit != end; ++cit)
2860 for (int i = 0; i < cit->size(); ++i)
2861 if (cit->isNewline(i))
2868 /////////////////////////////////////////////////////////////////////
2872 /////////////////////////////////////////////////////////////////////
2874 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2876 : tabular(buf.params(), max(rows, row_type(1)),
2877 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2881 InsetTabular::InsetTabular(InsetTabular const & tab)
2882 : Inset(tab), tabular(tab.tabular),
2883 buffer_(tab.buffer_), scx_(0)
2887 InsetTabular::~InsetTabular()
2889 InsetTabularMailer(*this).hideDialog();
2893 Inset * InsetTabular::clone() const
2895 return new InsetTabular(*this);
2899 Buffer const & InsetTabular::buffer() const
2905 void InsetTabular::buffer(Buffer const * b)
2911 void InsetTabular::write(Buffer const & buf, ostream & os) const
2913 os << "Tabular" << endl;
2914 tabular.write(buf, os);
2918 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2920 bool const old_format = (lex.getString() == "\\LyXTable");
2922 tabular.read(buf, lex);
2928 string token = lex.getString();
2929 while (lex && token != "\\end_inset") {
2931 token = lex.getString();
2934 lex.printError("Missing \\end_inset at this point. "
2940 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2942 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2943 // mi.base.textwidth << "\n";
2945 lyxerr << "InsetTabular::metrics: need bv" << endl;
2946 BOOST_ASSERT(false);
2950 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2953 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2954 if (tabular.isPartOfMultiColumn(i, j))
2955 // Multicolumn cell, but not first one
2960 if (tabular.cell_info[i][j].multicolumn ==
2961 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2962 p_width = tabular.cellinfo_of_cell(cell).p_width;
2964 p_width = tabular.column_info[j].p_width;
2965 if (!p_width.zero())
2966 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2967 tabular.getCellInset(cell)->metrics(m, dim);
2968 if (!p_width.zero())
2969 dim.wid = m.base.textwidth;
2970 tabular.setCellWidth(cell, dim.wid);
2971 if (p_width.zero()) {
2972 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2973 // FIXME there must be a way to get rid of
2974 // the second metrics call
2975 tabular.getCellInset(cell)->metrics(m, dim);
2977 maxAsc = max(maxAsc, dim.asc);
2978 maxDesc = max(maxDesc, dim.des);
2981 int const top_space = tabular.row_info[i].top_space_default ?
2982 default_line_space :
2983 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2984 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2985 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2986 default_line_space :
2987 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2988 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2991 dim.asc = tabular.rowAscent(0);
2992 dim.des = tabular.height() - dim.asc;
2993 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2997 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2999 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3000 BufferView * bv = pi.base.bv;
3002 // FIXME: As the full backrgound is painted in drawSelection(),
3003 // we have no choice but to do a full repaint for the Text cells.
3004 pi.full_repaint = true;
3006 resetPos(bv->cursor());
3009 x += ADD_TO_TABULAR_WIDTH;
3012 first_visible_cell = Tabular::npos;
3013 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3015 int const a = tabular.rowAscent(i);
3016 int const d = tabular.rowDescent(i);
3017 idx = tabular.cellIndex(i, 0);
3018 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3019 if (tabular.isPartOfMultiColumn(i, j))
3021 if (first_visible_cell == Tabular::npos)
3022 first_visible_cell = idx;
3024 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3025 // Cache the Inset position.
3026 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3027 if (nx + tabular.columnWidth(idx) < 0
3028 || nx > bv->workWidth()
3030 || y - a > bv->workHeight()) {
3031 pi.pain.setDrawingEnabled(false);
3032 cell(idx)->draw(pi, cx, y);
3033 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3034 pi.pain.setDrawingEnabled(true);
3036 cell(idx)->draw(pi, cx, y);
3037 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3039 nx += tabular.columnWidth(idx);
3043 if (i + 1 < tabular.rowCount())
3044 y += d + tabular.rowAscent(i + 1) +
3045 tabular.getAdditionalHeight(i + 1);
3050 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3052 Cursor & cur = pi.base.bv->cursor();
3054 x += scx_ + ADD_TO_TABULAR_WIDTH;
3056 // FIXME: it is wrong to completely paint the background
3057 // if we want to do single row painting.
3059 // Paint background of current tabular
3060 int const w = tabular.width();
3061 int const h = tabular.height();
3062 int yy = y - tabular.rowAscent(0);
3063 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3065 if (!cur.selection())
3067 if (!ptr_cmp(&cur.inset(), this))
3073 if (tablemode(cur)) {
3076 getSelection(cur, rs, re, cs, ce);
3077 y -= tabular.rowAscent(0);
3078 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3079 int const a = tabular.rowAscent(j);
3080 int const h = a + tabular.rowDescent(j);
3082 y += tabular.getAdditionalHeight(j);
3083 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3084 if (tabular.isPartOfMultiColumn(j, i))
3086 idx_type const cell =
3087 tabular.cellIndex(j, i);
3088 int const w = tabular.columnWidth(cell);
3089 if (i >= cs && i <= ce && j >= rs && j <= re)
3090 pi.pain.fillRectangle(xx, y, w, h,
3098 x += getCellXPos(cur.idx());
3099 x += tabular.getBeginningOfTextInCell(cur.idx());
3100 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3105 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3106 row_type row, idx_type cell, bool erased) const
3108 int x2 = x + tabular.columnWidth(cell);
3109 bool on_off = false;
3110 Color::color col = Color::tabularline;
3111 Color::color onoffcol = Color::tabularonoffline;
3114 col = Color::deletedtext;
3115 onoffcol = Color::deletedtext;
3118 if (!tabular.topAlreadyDrawn(cell)) {
3119 on_off = !tabular.topLine(cell);
3120 pain.line(x, y - tabular.rowAscent(row),
3121 x2, y - tabular.rowAscent(row),
3122 on_off ? onoffcol : col,
3123 on_off ? Painter::line_onoffdash : Painter::line_solid);
3125 on_off = !tabular.bottomLine(cell);
3126 pain.line(x, y + tabular.rowDescent(row),
3127 x2, y + tabular.rowDescent(row),
3128 on_off ? onoffcol : col,
3129 on_off ? Painter::line_onoffdash : Painter::line_solid);
3130 if (!tabular.leftAlreadyDrawn(cell)) {
3131 on_off = !tabular.leftLine(cell);
3132 pain.line(x, y - tabular.rowAscent(row),
3133 x, y + tabular.rowDescent(row),
3134 on_off ? onoffcol : col,
3135 on_off ? Painter::line_onoffdash : Painter::line_solid);
3137 on_off = !tabular.rightLine(cell);
3138 pain.line(x2 - tabular.getAdditionalWidth(cell),
3139 y - tabular.rowAscent(row),
3140 x2 - tabular.getAdditionalWidth(cell),
3141 y + tabular.rowDescent(row),
3142 on_off ? onoffcol : col,
3143 on_off ? Painter::line_onoffdash : Painter::line_solid);
3147 docstring const InsetTabular::editMessage() const
3149 return _("Opened table");
3153 void InsetTabular::edit(Cursor & cur, bool left)
3155 //lyxerr << "InsetTabular::edit: " << this << endl;
3157 cur.selection() = false;
3160 if (isRightToLeft(cur))
3161 cur.idx() = tabular.getLastCellInRow(0);
3167 if (isRightToLeft(cur))
3168 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3170 cur.idx() = tabular.cellCount() - 1;
3172 cur.pos() = cur.lastpos(); // FIXME crude guess
3174 // FIXME: this accesses the position cache before it is initialized
3176 //cur.bv().fitCursor();
3180 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3182 // In a longtable, tell captions what the current float is
3183 Counters & cnts = buf.params().getTextClass().counters();
3184 string const saveflt = cnts.current_float();
3185 if (tabular.isLongTabular())
3186 cnts.current_float("table");
3188 ParIterator it2 = it;
3190 size_t const end = it2.nargs();
3191 for ( ; it2.idx() < end; it2.top().forwardIdx())
3192 lyx::updateLabels(buf, it2);
3195 if (tabular.isLongTabular())
3196 cnts.current_float(saveflt);
3200 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3202 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3203 << "\n cur:" << cur << endl;
3204 CursorSlice sl = cur.top();
3205 Cursor & bvcur = cur.bv().cursor();
3207 switch (cmd.action) {
3209 case LFUN_MOUSE_PRESS:
3210 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3212 // do not reset cursor/selection if we have selected
3213 // some cells (bug 2715).
3214 if (cmd.button() == mouse_button::button3
3215 && &bvcur.selBegin().inset() == this
3216 && tablemode(bvcur))
3219 // Let InsetText do it
3220 cell(cur.idx())->dispatch(cur, cmd);
3222 case LFUN_MOUSE_MOTION:
3223 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3224 if (cmd.button() == mouse_button::button1) {
3225 // only accept motions to places not deeper nested than the real anchor
3226 if (bvcur.anchor_.hasPart(cur)) {
3227 // only update if selection changes
3228 if (bvcur.idx() == cur.idx() &&
3229 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3231 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3232 bvcur.setCursor(cur);
3233 bvcur.selection() = true;
3239 case LFUN_MOUSE_RELEASE:
3240 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3241 if (cmd.button() == mouse_button::button3)
3242 InsetTabularMailer(*this).showDialog(&cur.bv());
3245 case LFUN_CELL_BACKWARD:
3247 cur.selection() = false;
3250 case LFUN_CELL_FORWARD:
3252 cur.selection() = false;
3255 case LFUN_CHAR_FORWARD_SELECT:
3256 case LFUN_CHAR_FORWARD:
3257 cell(cur.idx())->dispatch(cur, cmd);
3258 if (!cur.result().dispatched()) {
3259 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3260 if (sl == cur.top())
3261 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3267 case LFUN_CHAR_BACKWARD_SELECT:
3268 case LFUN_CHAR_BACKWARD:
3269 cell(cur.idx())->dispatch(cur, cmd);
3270 if (!cur.result().dispatched()) {
3271 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3272 if (sl == cur.top())
3273 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3279 case LFUN_DOWN_SELECT:
3281 cell(cur.idx())->dispatch(cur, cmd);
3282 cur.dispatched(); // override the cell's decision
3283 if (sl == cur.top())
3284 // if our Text didn't do anything to the cursor
3285 // then we try to put the cursor into the cell below
3286 // setting also the right targetX.
3287 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3288 cur.idx() = tabular.getCellBelow(cur.idx());
3290 TextMetrics const & tm =
3291 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3292 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3294 if (sl == cur.top()) {
3295 // we trick it to go to the RIGHT after leaving the
3297 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3302 case LFUN_UP_SELECT:
3304 cell(cur.idx())->dispatch(cur, cmd);
3305 cur.dispatched(); // override the cell's decision
3306 if (sl == cur.top())
3307 // if our Text didn't do anything to the cursor
3308 // then we try to put the cursor into the cell above
3309 // setting also the right targetX.
3310 if (tabular.cellRow(cur.idx()) != 0) {
3311 cur.idx() = tabular.getCellAbove(cur.idx());
3312 cur.pit() = cur.lastpit();
3313 Text const * text = cell(cur.idx())->getText(0);
3314 TextMetrics const & tm = cur.bv().textMetrics(text);
3315 ParagraphMetrics const & pm =
3316 tm.parMetrics(cur.lastpit());
3317 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3319 if (sl == cur.top()) {
3320 cmd = FuncRequest(LFUN_UP);
3325 // case LFUN_SCREEN_DOWN: {
3326 // //if (hasSelection())
3327 // // cur.selection() = false;
3328 // col_type const col = tabular.cellColumn(cur.idx());
3329 // int const t = cur.bv().top_y() + cur.bv().height();
3330 // if (t < yo() + tabular.getHeightOfTabular()) {
3331 // cur.bv().scrollDocView(t);
3332 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3334 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3341 // case LFUN_SCREEN_UP: {
3342 // //if (hasSelection())
3343 // // cur.selection() = false;
3344 // col_type const col = tabular.cellColumn(cur.idx());
3345 // int const t = cur.bv().top_y() + cur.bv().height();
3347 // cur.bv().scrollDocView(t);
3351 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3355 // cur.par() = cur.lastpar();
3356 // cur.pos() = cur.lastpos();
3360 case LFUN_LAYOUT_TABULAR:
3361 InsetTabularMailer(*this).showDialog(&cur.bv());
3364 case LFUN_INSET_DIALOG_UPDATE:
3365 InsetTabularMailer(*this).updateDialog(&cur.bv());
3368 case LFUN_TABULAR_FEATURE:
3369 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3373 // insert file functions
3374 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3375 case LFUN_FILE_INSERT_PLAINTEXT: {
3377 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
3378 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3384 if (tablemode(cur)) {
3385 if (copySelection(cur)) {
3386 cur.recordUndoInset(DELETE_UNDO);
3391 cell(cur.idx())->dispatch(cur, cmd);
3394 case LFUN_CHAR_DELETE_BACKWARD:
3395 case LFUN_CHAR_DELETE_FORWARD:
3396 if (tablemode(cur)) {
3397 cur.recordUndoInset(DELETE_UNDO);
3401 cell(cur.idx())->dispatch(cur, cmd);
3405 if (!cur.selection())
3407 if (tablemode(cur)) {
3411 cell(cur.idx())->dispatch(cur, cmd);
3414 case LFUN_CLIPBOARD_PASTE:
3415 case LFUN_PRIMARY_SELECTION_PASTE: {
3416 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3417 theClipboard().getAsText() :
3418 theSelection().get();
3421 // pass to InsertPlaintextString, but
3422 // only if we have multi-cell content
3423 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3424 if (insertPlaintextString(cur.bv(), clip, false)) {
3425 // content has been replaced,
3426 // so cursor might be invalid
3427 cur.pos() = cur.lastpos();
3428 bvcur.setCursor(cur);
3432 // Let the cell handle normal text
3433 cell(cur.idx())->dispatch(cur, cmd);
3438 if (tabularStackDirty() && theClipboard().isInternal()) {
3439 cur.recordUndoInset(INSERT_UNDO);
3440 pasteClipboard(cur);
3443 cell(cur.idx())->dispatch(cur, cmd);
3446 case LFUN_FONT_EMPH:
3447 case LFUN_FONT_BOLD:
3448 case LFUN_FONT_ROMAN:
3449 case LFUN_FONT_NOUN:
3450 case LFUN_FONT_ITAL:
3451 case LFUN_FONT_FRAK:
3452 case LFUN_FONT_TYPEWRITER:
3453 case LFUN_FONT_SANS:
3454 case LFUN_FONT_FREE_APPLY:
3455 case LFUN_FONT_FREE_UPDATE:
3456 case LFUN_FONT_SIZE:
3457 case LFUN_FONT_UNDERLINE:
3459 case LFUN_WORD_CAPITALIZE:
3460 case LFUN_WORD_UPCASE:
3461 case LFUN_WORD_LOWCASE:
3462 case LFUN_CHARS_TRANSPOSE:
3463 if (tablemode(cur)) {
3466 getSelection(cur, rs, re, cs, ce);
3467 Cursor tmpcur = cur;
3468 for (row_type i = rs; i <= re; ++i) {
3469 for (col_type j = cs; j <= ce; ++j) {
3470 // cursor follows cell:
3471 tmpcur.idx() = tabular.cellIndex(i, j);
3472 // select this cell only:
3475 tmpcur.resetAnchor();
3476 tmpcur.pit() = tmpcur.lastpit();
3477 tmpcur.pos() = tmpcur.top().lastpos();
3478 tmpcur.setCursor(tmpcur);
3479 tmpcur.setSelection();
3480 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3485 cell(cur.idx())->dispatch(cur, cmd);
3489 // we try to handle this event in the insets dispatch function.
3490 cell(cur.idx())->dispatch(cur, cmd);
3496 // function sets an object as defined in func_status.h:
3497 // states OK, Unknown, Disabled, On, Off.
3498 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3499 FuncStatus & status) const
3501 switch (cmd.action) {
3502 case LFUN_TABULAR_FEATURE: {
3503 int action = Tabular::LAST_ACTION;
3505 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3506 string const tmp = tabularFeature[i].feature;
3507 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3508 action = tabularFeature[i].action;
3512 if (action == Tabular::LAST_ACTION) {
3514 status.unknown(true);
3518 string const argument
3519 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3521 row_type sel_row_start = 0;
3522 row_type sel_row_end = 0;
3523 col_type sel_col_start = 0;
3524 col_type sel_col_end = 0;
3525 Tabular::ltType dummyltt;
3528 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3531 case Tabular::SET_PWIDTH:
3532 case Tabular::SET_MPWIDTH:
3533 case Tabular::SET_SPECIAL_COLUMN:
3534 case Tabular::SET_SPECIAL_MULTI:
3535 case Tabular::APPEND_ROW:
3536 case Tabular::APPEND_COLUMN:
3537 case Tabular::DELETE_ROW:
3538 case Tabular::DELETE_COLUMN:
3539 case Tabular::COPY_ROW:
3540 case Tabular::COPY_COLUMN:
3541 case Tabular::SET_ALL_LINES:
3542 case Tabular::UNSET_ALL_LINES:
3543 case Tabular::SET_TOP_SPACE:
3544 case Tabular::SET_BOTTOM_SPACE:
3545 case Tabular::SET_INTERLINE_SPACE:
3549 case Tabular::MULTICOLUMN:
3550 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3553 case Tabular::M_TOGGLE_LINE_TOP:
3555 case Tabular::TOGGLE_LINE_TOP:
3556 status.setOnOff(tabular.topLine(cur.idx(), flag));
3559 case Tabular::M_TOGGLE_LINE_BOTTOM:
3561 case Tabular::TOGGLE_LINE_BOTTOM:
3562 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3565 case Tabular::M_TOGGLE_LINE_LEFT:
3567 case Tabular::TOGGLE_LINE_LEFT:
3568 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3571 case Tabular::M_TOGGLE_LINE_RIGHT:
3573 case Tabular::TOGGLE_LINE_RIGHT:
3574 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3577 case Tabular::M_ALIGN_LEFT:
3579 case Tabular::ALIGN_LEFT:
3580 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3583 case Tabular::M_ALIGN_RIGHT:
3585 case Tabular::ALIGN_RIGHT:
3586 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3589 case Tabular::M_ALIGN_CENTER:
3591 case Tabular::ALIGN_CENTER:
3592 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3595 case Tabular::ALIGN_BLOCK:
3596 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3597 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3600 case Tabular::M_VALIGN_TOP:
3602 case Tabular::VALIGN_TOP:
3604 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3607 case Tabular::M_VALIGN_BOTTOM:
3609 case Tabular::VALIGN_BOTTOM:
3611 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3614 case Tabular::M_VALIGN_MIDDLE:
3616 case Tabular::VALIGN_MIDDLE:
3618 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3621 case Tabular::SET_LONGTABULAR:
3622 status.setOnOff(tabular.isLongTabular());
3625 case Tabular::UNSET_LONGTABULAR:
3626 status.setOnOff(!tabular.isLongTabular());
3629 case Tabular::TOGGLE_ROTATE_TABULAR:
3630 case Tabular::SET_ROTATE_TABULAR:
3631 status.setOnOff(tabular.getRotateTabular());
3634 case Tabular::UNSET_ROTATE_TABULAR:
3635 status.setOnOff(!tabular.getRotateTabular());
3638 case Tabular::TOGGLE_ROTATE_CELL:
3639 case Tabular::SET_ROTATE_CELL:
3640 status.setOnOff(!oneCellHasRotationState(false,
3641 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3644 case Tabular::UNSET_ROTATE_CELL:
3645 status.setOnOff(!oneCellHasRotationState(true,
3646 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3649 case Tabular::SET_USEBOX:
3650 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3653 case Tabular::SET_LTFIRSTHEAD:
3654 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3657 case Tabular::UNSET_LTFIRSTHEAD:
3658 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3661 case Tabular::SET_LTHEAD:
3662 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3665 case Tabular::UNSET_LTHEAD:
3666 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3669 case Tabular::SET_LTFOOT:
3670 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3673 case Tabular::UNSET_LTFOOT:
3674 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3677 case Tabular::SET_LTLASTFOOT:
3678 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3681 case Tabular::UNSET_LTLASTFOOT:
3682 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3685 case Tabular::SET_LTNEWPAGE:
3686 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3689 case Tabular::SET_BOOKTABS:
3690 status.setOnOff(tabular.useBookTabs());
3693 case Tabular::UNSET_BOOKTABS:
3694 status.setOnOff(!tabular.useBookTabs());
3699 status.enabled(false);
3705 // These are only enabled inside tabular
3706 case LFUN_CELL_BACKWARD:
3707 case LFUN_CELL_FORWARD:
3708 status.enabled(true);
3711 // disable these with multiple cells selected
3712 case LFUN_INSET_INSERT:
3713 case LFUN_TABULAR_INSERT:
3714 case LFUN_FLEX_INSERT:
3715 case LFUN_FLOAT_INSERT:
3716 case LFUN_FLOAT_WIDE_INSERT:
3717 case LFUN_FOOTNOTE_INSERT:
3718 case LFUN_MARGINALNOTE_INSERT:
3719 case LFUN_MATH_INSERT:
3720 case LFUN_MATH_MODE:
3721 case LFUN_MATH_MUTATE:
3722 case LFUN_MATH_DISPLAY:
3723 case LFUN_NOTE_INSERT:
3724 case LFUN_OPTIONAL_INSERT:
3725 case LFUN_BOX_INSERT:
3726 case LFUN_BRANCH_INSERT:
3727 case LFUN_WRAP_INSERT:
3728 case LFUN_ERT_INSERT: {
3729 if (tablemode(cur)) {
3730 status.enabled(false);
3733 return cell(cur.idx())->getStatus(cur, cmd, status);
3736 // disable in non-fixed-width cells
3737 case LFUN_BREAK_LINE:
3738 case LFUN_BREAK_PARAGRAPH:
3739 case LFUN_BREAK_PARAGRAPH_SKIP: {
3740 if (tabular.getPWidth(cur.idx()).zero()) {
3741 status.enabled(false);
3744 return cell(cur.idx())->getStatus(cur, cmd, status);
3748 if (tabularStackDirty() && theClipboard().isInternal()) {
3749 status.enabled(true);
3752 return cell(cur.idx())->getStatus(cur, cmd, status);
3754 case LFUN_INSET_MODIFY:
3755 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3756 status.enabled(true);
3762 // we try to handle this event in the insets dispatch function.
3763 return cell(cur.idx())->getStatus(cur, cmd, status);
3768 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3769 OutputParams const & runparams) const
3771 return tabular.latex(buf, os, runparams);
3775 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3776 OutputParams const & runparams) const
3778 os << '\n'; // output table on a new line
3779 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3780 tabular.plaintext(buf, os, runparams, dp, false, 0);
3781 return PLAINTEXT_NEWLINE;
3785 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3786 OutputParams const & runparams) const
3791 // FIXME: Why not pass a proper DocIterator here?
3793 // if the table is inside a float it doesn't need the informaltable
3794 // wrapper. Search for it.
3795 for (master = owner(); master; master = master->owner())
3796 if (master->lyxCode() == FLOAT_CODE)
3801 os << "<informaltable>";
3804 ret += tabular.docbook(buf, os, runparams);
3806 os << "</informaltable>";
3813 void InsetTabular::validate(LaTeXFeatures & features) const
3815 tabular.validate(features);
3819 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3821 return tabular.getCellInset(idx);
3825 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3827 return tabular.getCellInset(idx);
3831 void InsetTabular::cursorPos(BufferView const & bv,
3832 CursorSlice const & sl, bool boundary, int & x, int & y) const
3834 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3836 // y offset correction
3837 int const row = tabular.cellRow(sl.idx());
3838 for (int i = 0; i <= row; ++i) {
3840 y += tabular.rowAscent(i);
3841 y += tabular.getAdditionalHeight(i);
3844 y += tabular.rowDescent(i);
3847 // x offset correction
3848 int const col = tabular.cellColumn(sl.idx());
3849 int idx = tabular.cellIndex(row, 0);
3850 for (int j = 0; j < col; ++j) {
3851 if (tabular.isPartOfMultiColumn(row, j))
3853 x += tabular.columnWidth(idx);
3856 x += tabular.getBeginningOfTextInCell(idx);
3857 x += ADD_TO_TABULAR_WIDTH;
3862 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3866 Inset const & inset = *tabular.getCellInset(cell);
3867 Point o = bv.coordCache().getInsets().xy(&inset);
3868 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3869 int const xend = xbeg + tabular.columnWidth(cell);
3870 row_type const row = tabular.cellRow(cell);
3871 int const ybeg = o.y_ - tabular.rowAscent(row) -
3872 tabular.getAdditionalHeight(row);
3873 int const yend = o.y_ + tabular.rowDescent(row);
3885 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3886 // << " ybeg=" << ybeg << " yend=" << yend
3887 // << " xx=" << xx << " yy=" << yy
3888 // << " dist=" << xx + yy << endl;
3893 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3895 //lyxerr << "InsetTabular::editXY: " << this << endl;
3896 cur.selection() = false;
3898 cur.idx() = getNearestCell(cur.bv(), x, y);
3900 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3904 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3906 cur.idx() = getNearestCell(cur.bv(), x, y);
3907 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3911 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3913 idx_type idx_min = 0;
3914 int dist_min = std::numeric_limits<int>::max();
3915 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3916 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3917 int const d = dist(bv, i, x, y);
3928 int InsetTabular::getCellXPos(idx_type const cell) const
3932 for (; !tabular.isFirstCellInRow(c); --c)
3935 for (; c < cell; ++c)
3936 lx += tabular.columnWidth(c);
3942 void InsetTabular::resetPos(Cursor & cur) const
3944 BufferView & bv = cur.bv();
3945 int const maxwidth = bv.workWidth();
3947 if (&cur.inset() != this) {
3951 int const X2 = maxwidth;
3952 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3953 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3954 int const x2 = x1 + tabular.columnWidth(cur.idx());
3957 scx_ = X1 + 20 - x1;
3959 scx_ = X2 - 20 - x2;
3964 cur.updateFlags(Update::Force | Update::FitCursor);
3968 void InsetTabular::moveNextCell(Cursor & cur)
3970 if (isRightToLeft(cur)) {
3971 if (tabular.isFirstCellInRow(cur.idx())) {
3972 row_type const row = tabular.cellRow(cur.idx());
3973 if (row == tabular.rowCount() - 1)
3975 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3982 if (tabular.isLastCell(cur.idx()))
3992 void InsetTabular::movePrevCell(Cursor & cur)
3994 if (isRightToLeft(cur)) {
3995 if (tabular.isLastCellInRow(cur.idx())) {
3996 row_type const row = tabular.cellRow(cur.idx());
3999 cur.idx() = tabular.getFirstCellInRow(row);
4000 cur.idx() = tabular.getCellAbove(cur.idx());
4002 if (tabular.isLastCell(cur.idx()))
4007 if (cur.idx() == 0) // first cell
4011 cur.pit() = cur.lastpit();
4012 cur.pos() = cur.lastpos();
4014 // FIXME: this accesses the position cache before it is initialized
4019 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4021 Tabular::Feature action = Tabular::LAST_ACTION;
4024 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4025 string const tmp = tabularFeature[i].feature;
4027 if (tmp == what.substr(0, tmp.length())) {
4028 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4029 //tabularFeatures[i].feature.length()))
4030 action = tabularFeature[i].action;
4034 if (action == Tabular::LAST_ACTION)
4038 ltrim(what.substr(tabularFeature[i].feature.length()));
4039 tabularFeatures(cur, action, val);
4044 static void checkLongtableSpecial(Tabular::ltType & ltt,
4045 string const & special, bool & flag)
4047 if (special == "dl_above") {
4050 } else if (special == "dl_below") {
4051 ltt.bottomDL = flag;
4053 } else if (special == "empty") {
4062 bool InsetTabular::oneCellHasRotationState(bool rotated,
4063 row_type row_start, row_type row_end,
4064 col_type col_start, col_type col_end) const {
4066 for (row_type i = row_start; i <= row_end; ++i) {
4067 for (col_type j = col_start; j <= col_end; ++j) {
4068 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4077 void InsetTabular::tabularFeatures(Cursor & cur,
4078 Tabular::Feature feature, string const & value)
4080 BufferView & bv = cur.bv();
4081 col_type sel_col_start;
4082 col_type sel_col_end;
4083 row_type sel_row_start;
4084 row_type sel_row_end;
4085 bool setLines = false;
4086 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4087 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4091 case Tabular::M_ALIGN_LEFT:
4092 case Tabular::ALIGN_LEFT:
4093 setAlign = LYX_ALIGN_LEFT;
4096 case Tabular::M_ALIGN_RIGHT:
4097 case Tabular::ALIGN_RIGHT:
4098 setAlign = LYX_ALIGN_RIGHT;
4101 case Tabular::M_ALIGN_CENTER:
4102 case Tabular::ALIGN_CENTER:
4103 setAlign = LYX_ALIGN_CENTER;
4106 case Tabular::ALIGN_BLOCK:
4107 setAlign = LYX_ALIGN_BLOCK;
4110 case Tabular::M_VALIGN_TOP:
4111 case Tabular::VALIGN_TOP:
4112 setVAlign = Tabular::LYX_VALIGN_TOP;
4115 case Tabular::M_VALIGN_BOTTOM:
4116 case Tabular::VALIGN_BOTTOM:
4117 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4120 case Tabular::M_VALIGN_MIDDLE:
4121 case Tabular::VALIGN_MIDDLE:
4122 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4129 cur.recordUndoInset(ATOMIC_UNDO);
4131 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4132 row_type const row = tabular.cellRow(cur.idx());
4133 col_type const column = tabular.cellColumn(cur.idx());
4135 Tabular::ltType ltt;
4139 case Tabular::SET_PWIDTH: {
4140 Length const len(value);
4141 tabular.setColumnPWidth(cur, cur.idx(), len);
4143 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4144 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4148 case Tabular::SET_MPWIDTH:
4149 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4152 case Tabular::SET_SPECIAL_COLUMN:
4153 case Tabular::SET_SPECIAL_MULTI:
4154 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4157 case Tabular::APPEND_ROW:
4158 // append the row into the tabular
4159 tabular.appendRow(bv.buffer().params(), cur.idx());
4162 case Tabular::APPEND_COLUMN:
4163 // append the column into the tabular
4164 tabular.appendColumn(bv.buffer().params(), cur.idx());
4165 cur.idx() = tabular.cellIndex(row, column);
4168 case Tabular::DELETE_ROW:
4169 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4170 tabular.deleteRow(sel_row_start);
4171 if (sel_row_start >= tabular.rowCount())
4173 cur.idx() = tabular.cellIndex(sel_row_start, column);
4176 cur.selection() = false;
4179 case Tabular::DELETE_COLUMN:
4180 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4181 tabular.deleteColumn(sel_col_start);
4182 if (sel_col_start >= tabular.columnCount())
4184 cur.idx() = tabular.cellIndex(row, sel_col_start);
4187 cur.selection() = false;
4190 case Tabular::COPY_ROW:
4191 tabular.copyRow(bv.buffer().params(), row);
4194 case Tabular::COPY_COLUMN:
4195 tabular.copyColumn(bv.buffer().params(), column);
4196 cur.idx() = tabular.cellIndex(row, column);
4199 case Tabular::M_TOGGLE_LINE_TOP:
4201 case Tabular::TOGGLE_LINE_TOP: {
4202 bool lineSet = !tabular.topLine(cur.idx(), flag);
4203 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4204 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4206 tabular.cellIndex(i, j),
4211 case Tabular::M_TOGGLE_LINE_BOTTOM:
4213 case Tabular::TOGGLE_LINE_BOTTOM: {
4214 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4215 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4216 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4217 tabular.setBottomLine(
4218 tabular.cellIndex(i, j),
4224 case Tabular::M_TOGGLE_LINE_LEFT:
4226 case Tabular::TOGGLE_LINE_LEFT: {
4227 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4228 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4229 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4230 tabular.setLeftLine(
4231 tabular.cellIndex(i,j),
4237 case Tabular::M_TOGGLE_LINE_RIGHT:
4239 case Tabular::TOGGLE_LINE_RIGHT: {
4240 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4241 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4242 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4243 tabular.setRightLine(
4244 tabular.cellIndex(i,j),
4250 case Tabular::M_ALIGN_LEFT:
4251 case Tabular::M_ALIGN_RIGHT:
4252 case Tabular::M_ALIGN_CENTER:
4254 case Tabular::ALIGN_LEFT:
4255 case Tabular::ALIGN_RIGHT:
4256 case Tabular::ALIGN_CENTER:
4257 case Tabular::ALIGN_BLOCK:
4258 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4259 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4260 tabular.setAlignment(
4261 tabular.cellIndex(i, j),
4266 case Tabular::M_VALIGN_TOP:
4267 case Tabular::M_VALIGN_BOTTOM:
4268 case Tabular::M_VALIGN_MIDDLE:
4270 case Tabular::VALIGN_TOP:
4271 case Tabular::VALIGN_BOTTOM:
4272 case Tabular::VALIGN_MIDDLE:
4273 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4274 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4275 tabular.setVAlignment(
4276 tabular.cellIndex(i, j),
4280 case Tabular::MULTICOLUMN: {
4281 if (sel_row_start != sel_row_end) {
4282 // FIXME: Need I say it ? This is horrible.
4284 Alert::error(_("Error setting multicolumn"),
4285 _("You cannot set multicolumn vertically."));
4288 if (!cur.selection()) {
4289 // just multicol for one single cell
4290 // check whether we are completely in a multicol
4291 if (tabular.isMultiColumn(cur.idx()))
4292 tabular.unsetMultiColumn(cur.idx());
4294 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4297 // we have a selection so this means we just add all this
4298 // cells to form a multicolumn cell
4299 idx_type const s_start = cur.selBegin().idx();
4300 idx_type const s_end = cur.selEnd().idx();
4301 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4302 cur.idx() = s_start;
4305 cur.selection() = false;
4309 case Tabular::SET_ALL_LINES:
4311 case Tabular::UNSET_ALL_LINES:
4312 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4313 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4314 tabular.setAllLines(
4315 tabular.cellIndex(i,j), setLines);
4318 case Tabular::SET_LONGTABULAR:
4319 tabular.setLongTabular(true);
4322 case Tabular::UNSET_LONGTABULAR:
4323 tabular.setLongTabular(false);
4326 case Tabular::SET_ROTATE_TABULAR:
4327 tabular.setRotateTabular(true);
4330 case Tabular::UNSET_ROTATE_TABULAR:
4331 tabular.setRotateTabular(false);
4334 case Tabular::TOGGLE_ROTATE_TABULAR:
4335 tabular.setRotateTabular(!tabular.getRotateTabular());
4338 case Tabular::SET_ROTATE_CELL:
4339 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4340 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4341 tabular.setRotateCell(
4342 tabular.cellIndex(i, j), true);
4345 case Tabular::UNSET_ROTATE_CELL:
4346 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4347 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4348 tabular.setRotateCell(
4349 tabular.cellIndex(i, j), false);
4352 case Tabular::TOGGLE_ROTATE_CELL:
4354 bool oneNotRotated = oneCellHasRotationState(false,
4355 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4357 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4358 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4359 tabular.setRotateCell(tabular.cellIndex(i, j),
4364 case Tabular::SET_USEBOX: {
4365 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4366 if (val == tabular.getUsebox(cur.idx()))
4367 val = Tabular::BOX_NONE;
4368 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4369 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4370 tabular.setUsebox(tabular.cellIndex(i, j), val);
4374 case Tabular::UNSET_LTFIRSTHEAD:
4376 case Tabular::SET_LTFIRSTHEAD:
4377 tabular.getRowOfLTFirstHead(row, ltt);
4378 checkLongtableSpecial(ltt, value, flag);
4379 tabular.setLTHead(row, flag, ltt, true);
4382 case Tabular::UNSET_LTHEAD:
4384 case Tabular::SET_LTHEAD:
4385 tabular.getRowOfLTHead(row, ltt);
4386 checkLongtableSpecial(ltt, value, flag);
4387 tabular.setLTHead(row, flag, ltt, false);
4390 case Tabular::UNSET_LTFOOT:
4392 case Tabular::SET_LTFOOT:
4393 tabular.getRowOfLTFoot(row, ltt);
4394 checkLongtableSpecial(ltt, value, flag);
4395 tabular.setLTFoot(row, flag, ltt, false);
4398 case Tabular::UNSET_LTLASTFOOT:
4400 case Tabular::SET_LTLASTFOOT:
4401 tabular.getRowOfLTLastFoot(row, ltt);
4402 checkLongtableSpecial(ltt, value, flag);
4403 tabular.setLTFoot(row, flag, ltt, true);
4406 case Tabular::SET_LTNEWPAGE:
4407 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4410 case Tabular::SET_BOOKTABS:
4411 tabular.setBookTabs(true);
4414 case Tabular::UNSET_BOOKTABS:
4415 tabular.setBookTabs(false);
4418 case Tabular::SET_TOP_SPACE: {
4420 if (value == "default")
4421 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4422 tabular.row_info[i].top_space_default = true;
4423 else if (isValidLength(value, &len))
4424 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4425 tabular.row_info[i].top_space_default = false;
4426 tabular.row_info[i].top_space = len;
4429 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4430 tabular.row_info[i].top_space_default = false;
4431 tabular.row_info[i].top_space = len;
4436 case Tabular::SET_BOTTOM_SPACE: {
4438 if (value == "default")
4439 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4440 tabular.row_info[i].bottom_space_default = true;
4441 else if (isValidLength(value, &len))
4442 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4443 tabular.row_info[i].bottom_space_default = false;
4444 tabular.row_info[i].bottom_space = len;
4447 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4448 tabular.row_info[i].bottom_space_default = false;
4449 tabular.row_info[i].bottom_space = len;
4454 case Tabular::SET_INTERLINE_SPACE: {
4456 if (value == "default")
4457 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4458 tabular.row_info[i].interline_space_default = true;
4459 else if (isValidLength(value, &len))
4460 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4461 tabular.row_info[i].interline_space_default = false;
4462 tabular.row_info[i].interline_space = len;
4465 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4466 tabular.row_info[i].interline_space_default = false;
4467 tabular.row_info[i].interline_space = len;
4472 // dummy stuff just to avoid warnings
4473 case Tabular::LAST_ACTION:
4479 bool InsetTabular::showInsetDialog(BufferView * bv) const
4481 InsetTabularMailer(*this).showDialog(bv);
4486 void InsetTabular::openLayoutDialog(BufferView * bv) const
4488 InsetTabularMailer(*this).showDialog(bv);
4492 bool InsetTabular::copySelection(Cursor & cur)
4494 if (!cur.selection())
4499 getSelection(cur, rs, re, cs, ce);
4501 paste_tabular.reset(new Tabular(tabular));
4503 for (row_type i = 0; i < rs; ++i)
4504 paste_tabular->deleteRow(0);
4506 row_type const rows = re - rs + 1;
4507 while (paste_tabular->rowCount() > rows)
4508 paste_tabular->deleteRow(rows);
4510 paste_tabular->setTopLine(0, true, true);
4511 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4514 for (col_type i = 0; i < cs; ++i)
4515 paste_tabular->deleteColumn(0);
4517 col_type const columns = ce - cs + 1;
4518 while (paste_tabular->columnCount() > columns)
4519 paste_tabular->deleteColumn(columns);
4521 paste_tabular->setLeftLine(0, true, true);
4522 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4525 odocstringstream os;
4526 OutputParams const runparams(0);
4527 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4528 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4529 cap::copySelection(cur, os.str());
4531 // mark tabular stack dirty
4532 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4533 // when we (hopefully) have a one-for-all paste mechanism.
4534 // This must be called after cap::copySelection.
4535 dirtyTabularStack(true);
4541 bool InsetTabular::pasteClipboard(Cursor & cur)
4545 col_type const actcol = tabular.cellColumn(cur.idx());
4546 row_type const actrow = tabular.cellRow(cur.idx());
4547 for (row_type r1 = 0, r2 = actrow;
4548 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4550 for (col_type c1 = 0, c2 = actcol;
4551 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4553 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4554 tabular.isPartOfMultiColumn(r2, c2))
4556 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4560 if (tabular.isPartOfMultiColumn(r2, c2)) {
4564 shared_ptr<InsetText> inset(
4565 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4566 tabular.setCellInset(r2, c2, inset);
4567 // FIXME: change tracking (MG)
4568 inset->setChange(Change(cur.buffer().params().trackChanges ?
4569 Change::INSERTED : Change::UNCHANGED));
4577 void InsetTabular::cutSelection(Cursor & cur)
4579 if (!cur.selection())
4584 getSelection(cur, rs, re, cs, ce);
4585 for (row_type i = rs; i <= re; ++i) {
4586 for (col_type j = cs; j <= ce; ++j) {
4587 shared_ptr<InsetText> t
4588 = cell(tabular.cellIndex(i, j));
4589 if (cur.buffer().params().trackChanges)
4590 // FIXME: Change tracking (MG)
4591 t->setChange(Change(Change::DELETED));
4597 // cursor position might be invalid now
4598 if (cur.pit() > cur.lastpit())
4599 cur.pit() = cur.lastpit();
4600 if (cur.pos() > cur.lastpos())
4601 cur.pos() = cur.lastpos();
4602 cur.clearSelection();
4606 bool InsetTabular::isRightToLeft(Cursor & cur) const
4608 BOOST_ASSERT(cur.depth() > 1);
4609 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4610 pos_type const parentpos = cur[cur.depth() - 2].pos();
4611 return parentpar.getFontSettings(cur.bv().buffer().params(),
4612 parentpos).language()->rightToLeft();
4616 void InsetTabular::getSelection(Cursor & cur,
4617 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4619 CursorSlice const & beg = cur.selBegin();
4620 CursorSlice const & end = cur.selEnd();
4621 cs = tabular.cellColumn(beg.idx());
4622 ce = tabular.cellColumn(end.idx());
4625 cs = tabular.cellColumn(end.idx());
4627 ce = tabular.cellRightColumn(end.idx());
4630 rs = tabular.cellRow(beg.idx());
4631 re = tabular.cellRow(end.idx());
4637 Text * InsetTabular::getText(int idx) const
4639 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4643 void InsetTabular::setChange(Change const & change)
4645 for (idx_type idx = 0; idx < nargs(); ++idx)
4646 cell(idx)->setChange(change);
4650 void InsetTabular::acceptChanges(BufferParams const & bparams)
4652 for (idx_type idx = 0; idx < nargs(); ++idx)
4653 cell(idx)->acceptChanges(bparams);
4657 void InsetTabular::rejectChanges(BufferParams const & bparams)
4659 for (idx_type idx = 0; idx < nargs(); ++idx)
4660 cell(idx)->rejectChanges(bparams);
4664 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4666 return tabular.getPWidth(cell).zero();
4670 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4673 if (buf.length() <= 0)
4676 Buffer const & buffer = bv.buffer();
4680 col_type maxCols = 1;
4681 docstring::size_type const len = buf.length();
4682 docstring::size_type p = 0;
4685 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4693 maxCols = max(cols, maxCols);
4699 maxCols = max(cols, maxCols);
4705 paste_tabular.reset(
4706 new Tabular(buffer.params(), rows, maxCols));
4707 loctab = paste_tabular.get();
4709 dirtyTabularStack(true);
4712 cell = bv.cursor().idx();
4713 ocol = tabular.cellColumn(cell);
4714 row = tabular.cellRow(cell);
4717 docstring::size_type op = 0;
4718 idx_type const cells = loctab->cellCount();
4721 rows = loctab->rowCount();
4722 col_type const columns = loctab->columnCount();
4724 while (cell < cells && p < len && row < rows &&
4725 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4731 // we can only set this if we are not too far right
4732 if (cols < columns) {
4733 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4734 Font const font = bv.textMetrics(&inset->text_).
4735 getDisplayFont(0, 0);
4736 inset->setText(buf.substr(op, p - op), font,
4737 buffer.params().trackChanges);
4743 // we can only set this if we are not too far right
4744 if (cols < columns) {
4745 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4746 Font const font = bv.textMetrics(&inset->text_).
4747 getDisplayFont(0, 0);
4748 inset->setText(buf.substr(op, p - op), font,
4749 buffer.params().trackChanges);
4754 cell = loctab->cellIndex(row, cols);
4760 // check for the last cell if there is no trailing '\n'
4761 if (cell < cells && op < len) {
4762 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4763 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4764 inset->setText(buf.substr(op, len - op), font,
4765 buffer.params().trackChanges);
4771 void InsetTabular::addPreview(PreviewLoader & loader) const
4773 row_type const rows = tabular.rowCount();
4774 col_type const columns = tabular.columnCount();
4775 for (row_type i = 0; i < rows; ++i) {
4776 for (col_type j = 0; j < columns; ++j)
4777 tabular.getCellInset(i, j)->addPreview(loader);
4782 bool InsetTabular::tablemode(Cursor & cur) const
4784 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4790 string const InsetTabularMailer::name_("tabular");
4792 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4793 : inset_(const_cast<InsetTabular &>(inset))
4797 string const InsetTabularMailer::inset2string(Buffer const &) const
4799 return params2string(inset_);
4803 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4805 istringstream data(in);
4807 lex.setStream(data);
4814 if (!lex || token != name_)
4815 return print_mailer_error("InsetTabularMailer", in, 1,
4818 // This is part of the inset proper that is usually swallowed
4819 // by Buffer::readInset
4821 if (!lex || token != "Tabular")
4822 return print_mailer_error("InsetTabularMailer", in, 2,
4825 Buffer const & buffer = inset.buffer();
4826 inset.read(buffer, lex);
4830 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4833 data << name_ << ' ';
4834 inset.write(inset.buffer(), data);
4835 data << "\\end_inset\n";