2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "CoordCache.h"
30 #include "CutAndPaste.h"
31 #include "support/debug.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
35 #include "support/gettext.h"
37 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "paragraph_funcs.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "support/convert.h"
50 #include "support/docstream.h"
51 #include "support/FileName.h"
52 #include "support/lstrings.h"
54 #include "frontends/alert.h"
55 #include "frontends/Clipboard.h"
56 #include "frontends/Painter.h"
57 #include "frontends/Selection.h"
59 #include <boost/scoped_ptr.hpp>
67 using namespace lyx::support;
69 using boost::shared_ptr;
70 using boost::dynamic_pointer_cast;
74 using cap::dirtyTabularStack;
75 using cap::tabularStackDirty;
77 using graphics::PreviewLoader;
79 using frontend::Painter;
80 using frontend::Clipboard;
82 namespace Alert = frontend::Alert;
87 int const ADD_TO_HEIGHT = 2;
88 int const ADD_TO_TABULAR_WIDTH = 2;
89 int const default_line_space = 10;
90 int const WIDTH_OF_LINE = 5;
94 boost::scoped_ptr<Tabular> paste_tabular;
97 struct TabularFeature {
98 Tabular::Feature action;
103 TabularFeature tabularFeature[] =
105 { Tabular::APPEND_ROW, "append-row" },
106 { Tabular::APPEND_COLUMN, "append-column" },
107 { Tabular::DELETE_ROW, "delete-row" },
108 { Tabular::DELETE_COLUMN, "delete-column" },
109 { Tabular::COPY_ROW, "copy-row" },
110 { Tabular::COPY_COLUMN, "copy-column" },
111 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
112 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
113 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
114 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
115 { Tabular::ALIGN_LEFT, "align-left" },
116 { Tabular::ALIGN_RIGHT, "align-right" },
117 { Tabular::ALIGN_CENTER, "align-center" },
118 { Tabular::ALIGN_BLOCK, "align-block" },
119 { Tabular::VALIGN_TOP, "valign-top" },
120 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
121 { Tabular::VALIGN_MIDDLE, "valign-middle" },
122 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
123 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
124 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
125 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
126 { Tabular::M_ALIGN_LEFT, "m-align-left" },
127 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
128 { Tabular::M_ALIGN_CENTER, "m-align-center" },
129 { Tabular::M_VALIGN_TOP, "m-valign-top" },
130 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
131 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
132 { Tabular::MULTICOLUMN, "multicolumn" },
133 { Tabular::SET_ALL_LINES, "set-all-lines" },
134 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
135 { Tabular::SET_LONGTABULAR, "set-longtabular" },
136 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
137 { Tabular::SET_PWIDTH, "set-pwidth" },
138 { Tabular::SET_MPWIDTH, "set-mpwidth" },
139 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
140 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
141 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
142 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
143 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
144 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
145 { Tabular::SET_USEBOX, "set-usebox" },
146 { Tabular::SET_LTHEAD, "set-lthead" },
147 { Tabular::UNSET_LTHEAD, "unset-lthead" },
148 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
149 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
150 { Tabular::SET_LTFOOT, "set-ltfoot" },
151 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
152 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
153 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
154 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
155 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
156 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
157 { Tabular::SET_BOOKTABS, "set-booktabs" },
158 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
159 { Tabular::SET_TOP_SPACE, "set-top-space" },
160 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
161 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
162 { Tabular::LAST_ACTION, "" }
166 class FeatureEqual : public unary_function<TabularFeature, bool> {
168 FeatureEqual(Tabular::Feature feature)
169 : feature_(feature) {}
170 bool operator()(TabularFeature const & tf) const {
171 return tf.action == feature_;
174 Tabular::Feature feature_;
179 string const write_attribute(string const & name, T const & t)
181 string const s = tostr(t);
182 return s.empty() ? s : " " + name + "=\"" + s + "\"";
186 string const write_attribute(string const & name, string const & t)
188 return t.empty() ? t : " " + name + "=\"" + t + "\"";
193 string const write_attribute(string const & name, docstring const & t)
195 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
200 string const write_attribute(string const & name, bool const & b)
202 // we write only true attribute values so we remove a bit of the
203 // file format bloat for tabulars.
204 return b ? write_attribute(name, convert<string>(b)) : string();
209 string const write_attribute(string const & name, int const & i)
211 // we write only true attribute values so we remove a bit of the
212 // file format bloat for tabulars.
213 return i ? write_attribute(name, convert<string>(i)) : string();
218 string const write_attribute(string const & name, Tabular::idx_type const & i)
220 // we write only true attribute values so we remove a bit of the
221 // file format bloat for tabulars.
222 return i ? write_attribute(name, convert<string>(i)) : string();
227 string const write_attribute(string const & name, Length const & value)
229 // we write only the value if we really have one same reson as above.
230 return value.zero() ? string() : write_attribute(name, value.asString());
234 string const tostr(LyXAlignment const & num)
239 case LYX_ALIGN_BLOCK:
243 case LYX_ALIGN_CENTER:
245 case LYX_ALIGN_RIGHT:
247 case LYX_ALIGN_LAYOUT:
249 case LYX_ALIGN_SPECIAL:
256 string const tostr(Tabular::VAlignment const & num)
259 case Tabular::LYX_VALIGN_TOP:
261 case Tabular::LYX_VALIGN_MIDDLE:
263 case Tabular::LYX_VALIGN_BOTTOM:
270 string const tostr(Tabular::BoxType const & num)
273 case Tabular::BOX_NONE:
275 case Tabular::BOX_PARBOX:
277 case Tabular::BOX_MINIPAGE:
284 // I would have liked a fromstr template a lot better. (Lgb)
285 bool string2type(string const str, LyXAlignment & num)
288 num = LYX_ALIGN_NONE;
289 else if (str == "block")
290 num = LYX_ALIGN_BLOCK;
291 else if (str == "left")
292 num = LYX_ALIGN_LEFT;
293 else if (str == "center")
294 num = LYX_ALIGN_CENTER;
295 else if (str == "right")
296 num = LYX_ALIGN_RIGHT;
303 bool string2type(string const str, Tabular::VAlignment & num)
306 num = Tabular::LYX_VALIGN_TOP;
307 else if (str == "middle" )
308 num = Tabular::LYX_VALIGN_MIDDLE;
309 else if (str == "bottom")
310 num = Tabular::LYX_VALIGN_BOTTOM;
317 bool string2type(string const str, Tabular::BoxType & num)
320 num = Tabular::BOX_NONE;
321 else if (str == "parbox")
322 num = Tabular::BOX_PARBOX;
323 else if (str == "minipage")
324 num = Tabular::BOX_MINIPAGE;
331 bool string2type(string const str, bool & num)
335 else if (str == "false")
343 bool getTokenValue(string const & str, char const * token, string & ret)
346 size_t token_length = strlen(token);
347 string::size_type pos = str.find(token);
349 if (pos == string::npos || pos + token_length + 1 >= str.length()
350 || str[pos + token_length] != '=')
352 pos += token_length + 1;
354 if (ch != '"' && ch != '\'') { // only read till next space
358 while (pos < str.length() - 1 && str[++pos] != ch)
365 bool getTokenValue(string const & str, char const * token, docstring & ret)
368 bool const success = getTokenValue(str, token, tmp);
369 ret = from_utf8(tmp);
374 bool getTokenValue(string const & str, char const * token, int & num)
378 if (!getTokenValue(str, token, tmp))
380 num = convert<int>(tmp);
385 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
388 return getTokenValue(str, token, tmp) && string2type(tmp, num);
392 bool getTokenValue(string const & str, char const * token,
393 Tabular::VAlignment & num)
396 return getTokenValue(str, token, tmp) && string2type(tmp, num);
400 bool getTokenValue(string const & str, char const * token,
401 Tabular::BoxType & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token, bool & flag)
410 // set the flag always to false as this should be the default for bools
411 // not in the file-format.
414 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
418 bool getTokenValue(string const & str, char const * token, Length & len)
420 // set the length to be zero() as default as this it should be if not
421 // in the file format.
424 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
428 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
433 if (!getTokenValue(str, token, tmp))
435 if (tmp == "default") {
439 return isValidLength(tmp, &len);
443 void l_getline(istream & is, string & str)
446 while (str.empty()) {
448 if (!str.empty() && str[str.length() - 1] == '\r')
449 str.erase(str.length() - 1);
456 string const featureAsString(Tabular::Feature feature)
458 TabularFeature * end = tabularFeature +
459 sizeof(tabularFeature) / sizeof(TabularFeature);
460 TabularFeature * it = find_if(tabularFeature, end,
461 FeatureEqual(feature));
462 return (it == end) ? string() : it->feature;
467 /////////////////////////////////////////////////////////////////////
471 /////////////////////////////////////////////////////////////////////
474 Tabular::cellstruct::cellstruct(BufferParams const & bp)
477 multicolumn(Tabular::CELL_NORMAL),
478 alignment(LYX_ALIGN_CENTER),
479 valignment(LYX_VALIGN_TOP),
486 inset(new InsetText(bp))
490 Tabular::cellstruct::cellstruct(cellstruct const & cs)
493 multicolumn(cs.multicolumn),
494 alignment(cs.alignment),
495 valignment(cs.valignment),
496 top_line(cs.top_line),
497 bottom_line(cs.bottom_line),
498 left_line(cs.left_line),
499 right_line(cs.right_line),
502 align_special(cs.align_special),
504 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
508 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
515 void Tabular::cellstruct::swap(cellstruct & rhs)
517 std::swap(cellno, rhs.cellno);
518 std::swap(width, rhs.width);
519 std::swap(multicolumn, rhs.multicolumn);
520 std::swap(alignment, rhs.alignment);
521 std::swap(valignment, rhs.valignment);
522 std::swap(top_line, rhs.top_line);
523 std::swap(bottom_line, rhs.bottom_line);
524 std::swap(left_line, rhs.left_line);
525 std::swap(right_line, rhs.right_line);
526 std::swap(usebox, rhs.usebox);
527 std::swap(rotate, rhs.rotate);
528 std::swap(align_special, rhs.align_special);
529 p_width.swap(rhs.p_width);
530 inset.swap(rhs.inset);
534 Tabular::rowstruct::rowstruct()
539 top_space_default(false),
540 bottom_space_default(false),
541 interline_space_default(false),
550 Tabular::columnstruct::columnstruct()
551 : alignment(LYX_ALIGN_CENTER),
552 valignment(LYX_VALIGN_TOP),
560 Tabular::ltType::ltType()
573 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
574 col_type columns_arg)
576 init(bp, rows_arg, columns_arg);
580 // activates all lines and sets all widths to 0
581 void Tabular::init(BufferParams const & bp, row_type rows_arg,
582 col_type columns_arg)
584 row_info = row_vector(rows_arg);
585 column_info = column_vector(columns_arg);
586 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
587 row_info.reserve(10);
588 column_info.reserve(10);
589 cell_info.reserve(100);
591 for (row_type i = 0; i < rows_arg; ++i)
592 cell_info[i].back().right_line = true;
593 row_info.back().bottom_line = true;
594 row_info.front().bottom_line = true;
595 column_info.back().right_line = true;
596 is_long_tabular = false;
598 use_booktabs = false;
602 void Tabular::fixCellNums()
605 for (row_type i = 0; i < rowCount(); ++i) {
606 for (col_type j = 0; j < columnCount(); ++j) {
607 // When debugging it can be nice to set
609 cell_info[i][j].inset->setDrawFrame(false);
610 cell_info[i][j].cellno = cellno++;
612 cell_info[i].back().right_line = true;
619 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
621 row_type const row = cellRow(cell);
623 row_vector::iterator rit = row_info.begin() + row;
624 row_info.insert(rit, rowstruct());
625 // now set the values of the row before
626 row_info[row] = row_info[row + 1];
628 row_type const nrows = rowCount();
629 col_type const ncols = columnCount();
631 cell_vvector old(nrows - 1);
632 for (row_type i = 0; i < nrows - 1; ++i)
633 swap(cell_info[i], old[i]);
635 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
637 for (row_type i = 0; i <= row; ++i)
638 swap(cell_info[i], old[i]);
639 for (row_type i = row + 2; i < nrows; ++i)
640 swap(cell_info[i], old[i - 1]);
643 for (col_type j = 0; j < ncols; ++j)
644 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
650 void Tabular::deleteRow(row_type const row)
652 // Not allowed to delete last row
656 row_info.erase(row_info.begin() + row);
657 cell_info.erase(cell_info.begin() + row);
662 void Tabular::copyRow(BufferParams const & bp, row_type const row)
664 row_info.insert(row_info.begin() + row, row_info[row]);
665 cell_info.insert(cell_info.begin() + row, cell_info[row]);
668 for (col_type j = 0; j < columnCount(); ++j)
669 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
675 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
677 col_type const column = cellColumn(cell);
678 col_type const ncols = columnCount();
679 column_vector::iterator cit = column_info.begin() + column + 1;
680 column_info.insert(cit, columnstruct());
681 // set the column values of the column before
682 column_info[column + 1] = column_info[column];
684 for (row_type i = 0; i < rowCount(); ++i) {
685 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
687 // care about multicolumns
688 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
689 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
691 if (column + 2 >= ncols
692 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
693 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
696 for (row_type i = 0; i < rowCount(); ++i) {
697 cell_info[i][column + 1].inset->clear();
699 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
705 void Tabular::deleteColumn(col_type const column)
707 // Not allowed to delete last column
708 if (columnCount() == 1)
711 column_info.erase(column_info.begin() + column);
712 for (row_type i = 0; i < rowCount(); ++i) {
713 // Care about multicolumn cells
714 if (column + 1 < columnCount() &&
715 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
716 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
717 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
719 cell_info[i].erase(cell_info[i].begin() + column);
725 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
727 column_info.insert(column_info.begin() + column, column_info[column]);
729 for (row_type i = 0; i < rowCount(); ++i)
730 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
733 for (row_type i = 0; i < rowCount(); ++i)
734 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
739 void Tabular::updateIndexes()
742 // Count only non-multicol cells plus begin multicol
743 // cells, ignore non-begin multicol cells:
744 for (row_type row = 0; row < rowCount(); ++row) {
745 for (col_type column = 0; column < columnCount(); ++column) {
746 if (cell_info[row][column].multicolumn
747 != Tabular::CELL_PART_OF_MULTICOLUMN)
749 BOOST_ASSERT(numberofcells != 0);
750 cell_info[row][column].cellno =
755 rowofcell.resize(numberofcells);
756 columnofcell.resize(numberofcells);
761 c < numberofcells && row < rowCount() && column < columnCount();) {
763 columnofcell[c] = column;
767 } while (column < columnCount() &&
768 cell_info[row][column].multicolumn
769 == Tabular::CELL_PART_OF_MULTICOLUMN);
771 if (column == columnCount()) {
777 for (row_type row = 0; row < rowCount(); ++row) {
778 for (col_type column = 0; column < columnCount(); ++column) {
779 if (isPartOfMultiColumn(row,column))
781 cell_info[row][column].inset->setAutoBreakRows(
782 !getPWidth(cellIndex(row, column)).zero());
788 Tabular::idx_type Tabular::cellCount() const
790 return numberofcells;
794 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
796 row_type const row = cellRow(cell);
798 for (col_type i = 0; i < columnCount(); ++i)
799 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
805 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
807 if (!wholerow && isMultiColumn(cell) &&
808 !(use_booktabs && cellRow(cell) == 0))
809 return cellinfo_of_cell(cell).top_line;
810 return row_info[cellRow(cell)].top_line;
814 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
816 if (!wholerow && isMultiColumn(cell) &&
817 !(use_booktabs && isLastRow(cell)))
818 return cellinfo_of_cell(cell).bottom_line;
819 return row_info[cellRow(cell)].bottom_line;
823 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
827 if (!wholecolumn && isMultiColumn(cell) &&
828 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
829 return cellinfo_of_cell(cell).left_line;
830 return column_info[cellColumn(cell)].left_line;
834 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
838 if (!wholecolumn && isMultiColumn(cell) &&
839 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
840 return cellinfo_of_cell(cell).right_line;
841 return column_info[cellRightColumn(cell)].right_line;
845 bool Tabular::topAlreadyDrawn(idx_type cell) const
847 row_type row = cellRow(cell);
848 if (row > 0 && !getAdditionalHeight(row)) {
849 col_type column = cellColumn(cell);
852 && cell_info[row][column].multicolumn
853 == Tabular::CELL_PART_OF_MULTICOLUMN)
855 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
856 return row_info[row].bottom_line;
858 return cell_info[row][column].bottom_line;
864 bool Tabular::leftAlreadyDrawn(idx_type cell) const
866 col_type column = cellColumn(cell);
868 row_type row = cellRow(cell);
870 (cell_info[row][column].multicolumn ==
871 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
872 if (getAdditionalWidth(cell_info[row][column].cellno))
874 return rightLine(cell_info[row][column].cellno);
880 bool Tabular::isLastRow(idx_type cell) const
882 return cellRow(cell) == rowCount() - 1;
886 int Tabular::getAdditionalHeight(row_type row) const
888 if (!row || row >= rowCount())
894 for (col_type column = 0; column < columnCount() && bottom; ++column) {
895 switch (cell_info[row - 1][column].multicolumn) {
896 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
897 bottom = cell_info[row - 1][column].bottom_line;
899 case Tabular::CELL_NORMAL:
900 bottom = row_info[row - 1].bottom_line;
903 for (col_type column = 0; column < columnCount() && top; ++column) {
904 switch (cell_info[row][column].multicolumn) {
905 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
906 top = cell_info[row][column].top_line;
908 case Tabular::CELL_NORMAL:
909 top = row_info[row].top_line;
912 int const interline_space = row_info[row - 1].interline_space_default ?
914 row_info[row - 1].interline_space.inPixels(width());
916 return interline_space + WIDTH_OF_LINE;
917 return interline_space;
921 int Tabular::getAdditionalWidth(idx_type cell) const
923 // internally already set in setCellWidth
924 // used to get it back in text.cpp
925 col_type const col = cellRightColumn(cell);
926 row_type const row = cellRow(cell);
927 if (col < columnCount() - 1 && rightLine(cell) &&
928 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
930 return WIDTH_OF_LINE;
936 // returns the maximum over all rows
937 int Tabular::columnWidth(idx_type cell) const
939 col_type const column1 = cellColumn(cell);
940 col_type const column2 = cellRightColumn(cell);
942 for (col_type i = column1; i <= column2; ++i)
943 result += column_info[i].width;
948 int Tabular::width() const
951 for (col_type i = 0; i < columnCount(); ++i)
952 width += column_info[i].width;
957 // returns true if a complete update is necessary, otherwise false
958 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
960 if (!isMultiColumn(cell))
963 row_type const row = cellRow(cell);
964 col_type const column1 = cellColumn(cell);
965 col_type const column2 = cellRightColumn(cell);
966 int const old_val = cell_info[row][column2].width;
968 // first set columns to 0 so we can calculate the right width
969 for (col_type i = column1; i <= column2; ++i) {
970 cell_info[row][i].width = 0;
972 // set the width to MAX_WIDTH until width > 0
973 int width = new_width + 2 * WIDTH_OF_LINE;
974 col_type i = column1;
975 for (; i < column2 && width > column_info[i].width; ++i) {
976 cell_info[row][i].width = column_info[i].width;
977 width -= column_info[i].width;
980 cell_info[row][i].width = width;
982 if (old_val != cell_info[row][column2].width) {
983 // in this case we have to recalculate all multicolumn cells which
984 // have this column as one of theirs but not as last one
985 calculate_width_of_column_NMC(i);
986 recalculateMulticolumnsOfColumn(i);
987 calculate_width_of_column(i);
993 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
995 // the last column does not have to be recalculated because all
996 // multicolumns will have here there last multicolumn cell which
997 // always will have the whole rest of the width of the cell.
998 if (columnCount() < 2 || column > (columnCount() - 2))
1000 for (row_type row = 0; row < rowCount(); ++row) {
1001 int mc = cell_info[row][column].multicolumn;
1002 int nmc = cell_info[row][column+1].multicolumn;
1003 // we only have to update multicolumns which do not have this
1004 // column as their last column!
1005 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1006 (mc == CELL_PART_OF_MULTICOLUMN &&
1007 nmc == CELL_PART_OF_MULTICOLUMN))
1009 idx_type const cellno = cell_info[row][column].cellno;
1010 setWidthOfMulticolCell(cellno,
1011 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1017 void Tabular::setCellWidth(idx_type cell, int new_width)
1019 row_type const row = cellRow(cell);
1020 col_type const column1 = cellColumn(cell);
1025 if (rightLine(cell_info[row][column1].cellno, true) &&
1026 column1 < columnCount() - 1 &&
1027 leftLine(cell_info[row][column1+1].cellno, true))
1029 add_width = WIDTH_OF_LINE;
1032 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1035 if (isMultiColumnReal(cell)) {
1036 tmp = setWidthOfMulticolCell(cell, new_width);
1038 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1039 cell_info[row][column1].width = width;
1040 tmp = calculate_width_of_column_NMC(column1);
1042 recalculateMulticolumnsOfColumn(column1);
1045 for (col_type i = 0; i < columnCount(); ++i)
1046 calculate_width_of_column(i);
1051 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1054 if (!isMultiColumn(cell) || onlycolumn)
1055 column_info[cellColumn(cell)].alignment = align;
1057 cellinfo_of_cell(cell).alignment = align;
1061 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1064 if (!isMultiColumn(cell) || onlycolumn)
1065 column_info[cellColumn(cell)].valignment = align;
1067 cellinfo_of_cell(cell).valignment = align;
1074 * Allow line and paragraph breaks for fixed width cells or disallow them,
1075 * merge cell paragraphs and reset layout to standard for variable width
1078 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1080 inset->setAutoBreakRows(fixedWidth);
1084 // merge all paragraphs to one
1085 BufferParams const & bp = cur.bv().buffer().params();
1086 while (inset->paragraphs().size() > 1)
1087 mergeParagraph(bp, inset->paragraphs(), 0);
1091 // undo information has already been recorded
1092 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1093 bp.getTextClass().defaultLayoutName());
1100 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1101 Length const & width)
1103 col_type const j = cellColumn(cell);
1105 column_info[j].p_width = width;
1106 for (row_type i = 0; i < rowCount(); ++i) {
1107 idx_type const cell = cellIndex(i, j);
1108 // because of multicolumns
1109 toggleFixedWidth(cur, getCellInset(cell).get(),
1110 !getPWidth(cell).zero());
1112 // cur paragraph can become invalid after paragraphs were merged
1113 if (cur.pit() > cur.lastpit())
1114 cur.pit() = cur.lastpit();
1115 // cur position can become invalid after newlines were removed
1116 if (cur.pos() > cur.lastpos())
1117 cur.pos() = cur.lastpos();
1121 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1122 Length const & width)
1124 if (!isMultiColumn(cell))
1127 cellinfo_of_cell(cell).p_width = width;
1128 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1129 // cur paragraph can become invalid after paragraphs were merged
1130 if (cur.pit() > cur.lastpit())
1131 cur.pit() = cur.lastpit();
1132 // cur position can become invalid after newlines were removed
1133 if (cur.pos() > cur.lastpos())
1134 cur.pos() = cur.lastpos();
1139 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1140 Tabular::Feature what)
1142 if (what == SET_SPECIAL_MULTI)
1143 cellinfo_of_cell(cell).align_special = special;
1145 column_info[cellColumn(cell)].align_special = special;
1149 void Tabular::setAllLines(idx_type cell, bool line)
1151 setTopLine(cell, line);
1152 setBottomLine(cell, line);
1153 setRightLine(cell, line);
1154 setLeftLine(cell, line);
1158 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1160 row_type const row = cellRow(cell);
1161 if (wholerow || !isMultiColumn(cell))
1162 row_info[row].top_line = line;
1164 cellinfo_of_cell(cell).top_line = line;
1168 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1170 if (wholerow || !isMultiColumn(cell))
1171 row_info[cellRow(cell)].bottom_line = line;
1173 cellinfo_of_cell(cell).bottom_line = line;
1177 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1179 if (wholecolumn || !isMultiColumn(cell))
1180 column_info[cellColumn(cell)].left_line = line;
1182 cellinfo_of_cell(cell).left_line = line;
1186 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1188 if (wholecolumn || !isMultiColumn(cell))
1189 column_info[cellRightColumn(cell)].right_line = line;
1191 cellinfo_of_cell(cell).right_line = line;
1195 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1197 if (!onlycolumn && isMultiColumn(cell))
1198 return cellinfo_of_cell(cell).alignment;
1199 return column_info[cellColumn(cell)].alignment;
1204 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1206 if (!onlycolumn && isMultiColumn(cell))
1207 return cellinfo_of_cell(cell).valignment;
1208 return column_info[cellColumn(cell)].valignment;
1212 Length const Tabular::getPWidth(idx_type cell) const
1214 if (isMultiColumn(cell))
1215 return cellinfo_of_cell(cell).p_width;
1216 return column_info[cellColumn(cell)].p_width;
1220 Length const Tabular::getColumnPWidth(idx_type cell) const
1222 return column_info[cellColumn(cell)].p_width;
1226 Length const Tabular::getMColumnPWidth(idx_type cell) const
1228 if (isMultiColumn(cell))
1229 return cellinfo_of_cell(cell).p_width;
1234 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1236 if (what == SET_SPECIAL_MULTI)
1237 return cellinfo_of_cell(cell).align_special;
1238 return column_info[cellColumn(cell)].align_special;
1242 int Tabular::cellWidth(idx_type cell) const
1244 row_type const row = cellRow(cell);
1245 col_type const column1 = cellColumn(cell);
1246 col_type const column2 = cellRightColumn(cell);
1248 for (col_type i = column1; i <= column2; ++i)
1249 result += cell_info[row][i].width;
1254 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1258 switch (getAlignment(cell)) {
1259 case LYX_ALIGN_CENTER:
1260 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1262 case LYX_ALIGN_RIGHT:
1263 x += columnWidth(cell) - cellWidth(cell);
1264 // + getAdditionalWidth(cell);
1267 // LYX_ALIGN_LEFT: nothing :-)
1271 // the LaTeX Way :-(
1277 bool Tabular::isFirstCellInRow(idx_type cell) const
1279 return cellColumn(cell) == 0;
1283 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1285 if (row > rowCount() - 1)
1286 row = rowCount() - 1;
1287 return cell_info[row][0].cellno;
1291 bool Tabular::isLastCellInRow(idx_type cell) const
1293 return cellRightColumn(cell) == columnCount() - 1;
1297 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1299 if (row > rowCount() - 1)
1300 row = rowCount() - 1;
1301 return cell_info[row][columnCount() - 1].cellno;
1305 void Tabular::calculate_width_of_column(col_type column)
1308 for (row_type i = 0; i < rowCount(); ++i)
1309 maximum = max(cell_info[i][column].width, maximum);
1310 column_info[column].width = maximum;
1315 // Calculate the columns regarding ONLY the normal cells and if this
1316 // column is inside a multicolumn cell then use it only if its the last
1317 // column of this multicolumn cell as this gives an added width to the
1318 // column, all the rest should be adapted!
1320 bool Tabular::calculate_width_of_column_NMC(col_type column)
1322 int const old_column_width = column_info[column].width;
1324 for (row_type i = 0; i < rowCount(); ++i) {
1325 idx_type cell = cellIndex(i, column);
1326 bool ismulti = isMultiColumnReal(cell);
1327 if ((!ismulti || column == cellRightColumn(cell)) &&
1328 cell_info[i][column].width > max)
1330 max = cell_info[i][column].width;
1333 column_info[column].width = max;
1334 return column_info[column].width != old_column_width;
1338 Tabular::row_type Tabular::cellRow(idx_type cell) const
1340 if (cell >= cellCount())
1341 return rowCount() - 1;
1344 return rowofcell[cell];
1348 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1350 if (cell >= cellCount())
1351 return columnCount() - 1;
1354 return columnofcell[cell];
1358 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1360 row_type const row = cellRow(cell);
1361 col_type column = cellColumn(cell);
1362 while (column < columnCount() - 1 &&
1363 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1369 void Tabular::write(Buffer const & buf, ostream & os) const
1373 << write_attribute("version", 3)
1374 << write_attribute("rows", rowCount())
1375 << write_attribute("columns", columnCount())
1377 // global longtable options
1379 << write_attribute("rotate", rotate)
1380 << write_attribute("booktabs", use_booktabs)
1381 << write_attribute("islongtable", is_long_tabular)
1382 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1383 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1384 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1385 << write_attribute("headTopDL", endhead.topDL)
1386 << write_attribute("headBottomDL", endhead.bottomDL)
1387 << write_attribute("footTopDL", endfoot.topDL)
1388 << write_attribute("footBottomDL", endfoot.bottomDL)
1389 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1390 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1391 << write_attribute("lastFootEmpty", endlastfoot.empty)
1393 for (col_type j = 0; j < columnCount(); ++j) {
1395 << write_attribute("alignment", column_info[j].alignment)
1396 << write_attribute("valignment", column_info[j].valignment)
1397 << write_attribute("leftline", column_info[j].left_line)
1398 << write_attribute("rightline", column_info[j].right_line)
1399 << write_attribute("width", column_info[j].p_width.asString())
1400 << write_attribute("special", column_info[j].align_special)
1403 for (row_type i = 0; i < rowCount(); ++i) {
1405 << write_attribute("topline", row_info[i].top_line)
1406 << write_attribute("bottomline", row_info[i].bottom_line);
1407 static const string def("default");
1408 if (row_info[i].top_space_default)
1409 os << write_attribute("topspace", def);
1411 os << write_attribute("topspace", row_info[i].top_space);
1412 if (row_info[i].bottom_space_default)
1413 os << write_attribute("bottomspace", def);
1415 os << write_attribute("bottomspace", row_info[i].bottom_space);
1416 if (row_info[i].interline_space_default)
1417 os << write_attribute("interlinespace", def);
1419 os << write_attribute("interlinespace", row_info[i].interline_space);
1420 os << write_attribute("endhead", row_info[i].endhead)
1421 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1422 << write_attribute("endfoot", row_info[i].endfoot)
1423 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1424 << write_attribute("newpage", row_info[i].newpage)
1426 for (col_type j = 0; j < columnCount(); ++j) {
1428 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1429 << write_attribute("alignment", cell_info[i][j].alignment)
1430 << write_attribute("valignment", cell_info[i][j].valignment)
1431 << write_attribute("topline", cell_info[i][j].top_line)
1432 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1433 << write_attribute("leftline", cell_info[i][j].left_line)
1434 << write_attribute("rightline", cell_info[i][j].right_line)
1435 << write_attribute("rotate", cell_info[i][j].rotate)
1436 << write_attribute("usebox", cell_info[i][j].usebox)
1437 << write_attribute("width", cell_info[i][j].p_width)
1438 << write_attribute("special", cell_info[i][j].align_special)
1440 os << "\\begin_inset ";
1441 cell_info[i][j].inset->write(buf, os);
1442 os << "\n\\end_inset\n"
1447 os << "</lyxtabular>\n";
1451 void Tabular::read(Buffer const & buf, Lexer & lex)
1454 istream & is = lex.getStream();
1456 l_getline(is, line);
1457 if (!prefixIs(line, "<lyxtabular ")
1458 && !prefixIs(line, "<Tabular ")) {
1459 BOOST_ASSERT(false);
1464 if (!getTokenValue(line, "version", version))
1466 BOOST_ASSERT(version >= 2);
1469 if (!getTokenValue(line, "rows", rows_arg))
1472 if (!getTokenValue(line, "columns", columns_arg))
1474 init(buf.params(), rows_arg, columns_arg);
1475 l_getline(is, line);
1476 if (!prefixIs(line, "<features")) {
1477 lyxerr << "Wrong tabular format (expected <features ...> got"
1478 << line << ')' << endl;
1481 getTokenValue(line, "rotate", rotate);
1482 getTokenValue(line, "booktabs", use_booktabs);
1483 getTokenValue(line, "islongtable", is_long_tabular);
1484 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1485 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1486 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1487 getTokenValue(line, "headTopDL", endhead.topDL);
1488 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1489 getTokenValue(line, "footTopDL", endfoot.topDL);
1490 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1491 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1492 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1493 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1495 for (col_type j = 0; j < columnCount(); ++j) {
1497 if (!prefixIs(line,"<column")) {
1498 lyxerr << "Wrong tabular format (expected <column ...> got"
1499 << line << ')' << endl;
1502 getTokenValue(line, "alignment", column_info[j].alignment);
1503 getTokenValue(line, "valignment", column_info[j].valignment);
1504 getTokenValue(line, "leftline", column_info[j].left_line);
1505 getTokenValue(line, "rightline", column_info[j].right_line);
1506 getTokenValue(line, "width", column_info[j].p_width);
1507 getTokenValue(line, "special", column_info[j].align_special);
1510 for (row_type i = 0; i < rowCount(); ++i) {
1511 l_getline(is, line);
1512 if (!prefixIs(line, "<row")) {
1513 lyxerr << "Wrong tabular format (expected <row ...> got"
1514 << line << ')' << endl;
1517 getTokenValue(line, "topline", row_info[i].top_line);
1518 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1519 getTokenValue(line, "topspace", row_info[i].top_space,
1520 row_info[i].top_space_default);
1521 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1522 row_info[i].bottom_space_default);
1523 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1524 row_info[i].interline_space_default);
1525 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1526 getTokenValue(line, "endhead", row_info[i].endhead);
1527 getTokenValue(line, "endfoot", row_info[i].endfoot);
1528 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1529 getTokenValue(line, "newpage", row_info[i].newpage);
1530 for (col_type j = 0; j < columnCount(); ++j) {
1531 l_getline(is, line);
1532 if (!prefixIs(line, "<cell")) {
1533 lyxerr << "Wrong tabular format (expected <cell ...> got"
1534 << line << ')' << endl;
1537 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1538 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1539 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1540 getTokenValue(line, "topline", cell_info[i][j].top_line);
1541 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1542 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1543 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1544 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1545 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1546 getTokenValue(line, "width", cell_info[i][j].p_width);
1547 getTokenValue(line, "special", cell_info[i][j].align_special);
1548 l_getline(is, line);
1549 if (prefixIs(line, "\\begin_inset")) {
1550 cell_info[i][j].inset->read(buf, lex);
1551 l_getline(is, line);
1553 if (!prefixIs(line, "</cell>")) {
1554 lyxerr << "Wrong tabular format (expected </cell> got"
1555 << line << ')' << endl;
1559 l_getline(is, line);
1560 if (!prefixIs(line, "</row>")) {
1561 lyxerr << "Wrong tabular format (expected </row> got"
1562 << line << ')' << endl;
1566 while (!prefixIs(line, "</lyxtabular>")) {
1567 l_getline(is, line);
1573 bool Tabular::isMultiColumn(idx_type cell) const
1575 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1579 bool Tabular::isMultiColumnReal(idx_type cell) const
1581 return cellColumn(cell) != cellRightColumn(cell) &&
1582 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1586 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1588 return cell_info[cellRow(cell)][cellColumn(cell)];
1592 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1595 cellstruct & cs = cellinfo_of_cell(cell);
1596 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1597 cs.alignment = column_info[cellColumn(cell)].alignment;
1598 cs.top_line = row_info[cellRow(cell)].top_line;
1599 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1600 cs.left_line = column_info[cellColumn(cell)].left_line;
1601 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1602 for (idx_type i = 1; i < number; ++i) {
1603 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1604 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1605 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1612 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1614 row_type const row = cellRow(cell);
1615 col_type column = cellColumn(cell);
1616 idx_type result = 1;
1618 while (column < columnCount() &&
1619 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1628 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1630 row_type const row = cellRow(cell);
1631 col_type column = cellColumn(cell);
1633 idx_type result = 0;
1635 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1636 cell_info[row][column].multicolumn = CELL_NORMAL;
1638 while (column < columnCount() &&
1639 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1641 cell_info[row][column].multicolumn = CELL_NORMAL;
1651 void Tabular::setBookTabs(bool what)
1653 use_booktabs = what;
1657 bool Tabular::useBookTabs() const
1659 return use_booktabs;
1663 void Tabular::setLongTabular(bool what)
1665 is_long_tabular = what;
1669 bool Tabular::isLongTabular() const
1671 return is_long_tabular;
1675 void Tabular::setRotateTabular(bool flag)
1681 bool Tabular::getRotateTabular() const
1687 void Tabular::setRotateCell(idx_type cell, bool flag)
1689 cellinfo_of_cell(cell).rotate = flag;
1693 bool Tabular::getRotateCell(idx_type cell) const
1695 return cellinfo_of_cell(cell).rotate;
1699 bool Tabular::needRotating() const
1703 for (row_type i = 0; i < rowCount(); ++i)
1704 for (col_type j = 0; j < columnCount(); ++j)
1705 if (cell_info[i][j].rotate)
1711 bool Tabular::isLastCell(idx_type cell) const
1713 if (cell + 1 < cellCount())
1719 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1721 if (cellRow(cell) > 0)
1722 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1727 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1729 if (cellRow(cell) + 1 < rowCount())
1730 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1735 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1737 if (cellRow(cell) == 0)
1739 if (!isMultiColumn(cell))
1740 return getCellAbove(cell);
1741 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1745 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1747 if (cellRow(cell) + 1 >= rowCount())
1749 if (!isMultiColumn(cell))
1750 return getCellBelow(cell);
1751 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1755 Tabular::idx_type Tabular::cellIndex(row_type row,
1756 col_type column) const
1758 BOOST_ASSERT(column != npos && column < columnCount() &&
1759 row != npos && row < rowCount());
1760 return cell_info[row][column].cellno;
1764 void Tabular::setUsebox(idx_type cell, BoxType type)
1766 cellinfo_of_cell(cell).usebox = type;
1770 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1772 if (column_info[cellColumn(cell)].p_width.zero() &&
1773 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1775 if (cellinfo_of_cell(cell).usebox > 1)
1776 return cellinfo_of_cell(cell).usebox;
1777 return useParbox(cell);
1782 // This are functions used for the longtable support
1784 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1790 row_info[row].endfirsthead = flag;
1794 row_info[row].endhead = flag;
1799 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1802 hd.set = haveLTHead();
1803 return row_info[row].endhead;
1807 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1810 hd.set = haveLTFirstHead();
1811 return row_info[row].endfirsthead;
1815 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1821 row_info[row].endlastfoot = flag;
1825 row_info[row].endfoot = flag;
1830 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1833 fd.set = haveLTFoot();
1834 return row_info[row].endfoot;
1838 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1841 fd.set = haveLTLastFoot();
1842 return row_info[row].endlastfoot;
1846 void Tabular::setLTNewPage(row_type row, bool what)
1848 row_info[row].newpage = what;
1852 bool Tabular::getLTNewPage(row_type row) const
1854 return row_info[row].newpage;
1858 bool Tabular::haveLTHead() const
1860 for (row_type i = 0; i < rowCount(); ++i)
1861 if (row_info[i].endhead)
1867 bool Tabular::haveLTFirstHead() const
1869 if (endfirsthead.empty)
1871 for (row_type i = 0; i < rowCount(); ++i)
1872 if (row_info[i].endfirsthead)
1878 bool Tabular::haveLTFoot() const
1880 for (row_type i = 0; i < rowCount(); ++i)
1881 if (row_info[i].endfoot)
1887 bool Tabular::haveLTLastFoot() const
1889 if (endlastfoot.empty)
1891 for (row_type i = 0; i < rowCount(); ++i)
1892 if (row_info[i].endlastfoot)
1898 // end longtable support functions
1900 void Tabular::setRowAscent(row_type row, int height)
1902 if (row >= rowCount() || row_info[row].ascent == height)
1904 row_info[row].ascent = height;
1908 void Tabular::setRowDescent(row_type row, int height)
1910 if (row >= rowCount() || row_info[row].descent == height)
1912 row_info[row].descent = height;
1916 int Tabular::rowAscent(row_type row) const
1918 if (row >= rowCount())
1920 return row_info[row].ascent;
1924 int Tabular::rowDescent(row_type row) const
1926 BOOST_ASSERT(row < rowCount());
1927 return row_info[row].descent;
1931 int Tabular::height() const
1934 for (row_type row = 0; row < rowCount(); ++row)
1935 height += rowAscent(row) + rowDescent(row) +
1936 getAdditionalHeight(row);
1941 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1943 BOOST_ASSERT(row < rowCount());
1944 BOOST_ASSERT(column < columnCount());
1945 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1949 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1951 // FIXME: assert or return 0 as in TeXBottomHLine()?
1952 BOOST_ASSERT(row != npos);
1953 BOOST_ASSERT(row < rowCount());
1955 idx_type const fcell = getFirstCellInRow(row);
1956 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1959 for (idx_type i = fcell; i < n; ++i) {
1963 if (use_booktabs && row == 0) {
1965 } else if (tmp == n - fcell) {
1966 os << (use_booktabs ? "\\midrule " : "\\hline ");
1968 for (idx_type i = fcell; i < n; ++i) {
1970 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1971 << cellColumn(i) + 1
1973 << cellRightColumn(i) + 1
1985 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1987 // FIXME: return 0 or assert as in TeXTopHLine()?
1988 if (row == npos || row >= rowCount())
1991 idx_type const fcell = getFirstCellInRow(row);
1992 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1995 for (idx_type i = fcell; i < n; ++i) {
1999 if (use_booktabs && row == rowCount() - 1) {
2000 os << "\\bottomrule";
2001 } else if (tmp == n - fcell) {
2002 os << (use_booktabs ? "\\midrule" : "\\hline");
2004 for (idx_type i = fcell; i < n; ++i) {
2005 if (bottomLine(i)) {
2006 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2007 << cellColumn(i) + 1
2009 << cellRightColumn(i) + 1
2021 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2025 if (getRotateCell(cell)) {
2026 os << "\\begin{sideways}\n";
2029 if (isMultiColumn(cell)) {
2030 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2031 if (leftLine(cell) &&
2032 (isFirstCellInRow(cell) ||
2033 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2034 !rightLine(cell - 1, true))))
2036 if (!cellinfo_of_cell(cell).align_special.empty()) {
2037 os << cellinfo_of_cell(cell).align_special;
2039 if (!getPWidth(cell).zero()) {
2040 switch (getVAlignment(cell)) {
2041 case LYX_VALIGN_TOP:
2044 case LYX_VALIGN_MIDDLE:
2047 case LYX_VALIGN_BOTTOM:
2052 << from_ascii(getPWidth(cell).asLatexString())
2055 switch (getAlignment(cell)) {
2056 case LYX_ALIGN_LEFT:
2059 case LYX_ALIGN_RIGHT:
2066 } // end if else !getPWidth
2067 } // end if else !cellinfo_of_cell
2068 if (rightLine(cell))
2070 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2075 if (getUsebox(cell) == BOX_PARBOX) {
2077 switch (getVAlignment(cell)) {
2078 case LYX_VALIGN_TOP:
2081 case LYX_VALIGN_MIDDLE:
2084 case LYX_VALIGN_BOTTOM:
2088 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2090 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2091 os << "\\begin{minipage}[";
2092 switch (getVAlignment(cell)) {
2093 case LYX_VALIGN_TOP:
2096 case LYX_VALIGN_MIDDLE:
2099 case LYX_VALIGN_BOTTOM:
2103 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2111 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2116 if (getUsebox(cell) == BOX_PARBOX)
2118 else if (getUsebox(cell) == BOX_MINIPAGE) {
2119 os << "%\n\\end{minipage}";
2122 if (isMultiColumn(cell)) {
2125 if (getRotateCell(cell)) {
2126 os << "%\n\\end{sideways}";
2133 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2134 OutputParams const & runparams) const
2136 if (!is_long_tabular)
2140 // output header info
2142 if (endhead.topDL) {
2146 for (row_type i = 0; i < rowCount(); ++i) {
2147 if (row_info[i].endhead) {
2148 ret += TeXRow(os, i, buf, runparams);
2151 if (endhead.bottomDL) {
2155 os << "\\endhead\n";
2157 if (endfirsthead.empty) {
2158 os << "\\endfirsthead\n";
2162 // output firstheader info
2163 if (haveLTFirstHead()) {
2164 if (endfirsthead.topDL) {
2168 for (row_type i = 0; i < rowCount(); ++i) {
2169 if (row_info[i].endfirsthead) {
2170 ret += TeXRow(os, i, buf, runparams);
2173 if (endfirsthead.bottomDL) {
2177 os << "\\endfirsthead\n";
2180 // output footer info
2182 if (endfoot.topDL) {
2186 for (row_type i = 0; i < rowCount(); ++i) {
2187 if (row_info[i].endfoot) {
2188 ret += TeXRow(os, i, buf, runparams);
2191 if (endfoot.bottomDL) {
2195 os << "\\endfoot\n";
2197 if (endlastfoot.empty) {
2198 os << "\\endlastfoot\n";
2202 // output lastfooter info
2203 if (haveLTLastFoot()) {
2204 if (endlastfoot.topDL) {
2208 for (row_type i = 0; i < rowCount(); ++i) {
2209 if (row_info[i].endlastfoot) {
2210 ret += TeXRow(os, i, buf, runparams);
2213 if (endlastfoot.bottomDL) {
2217 os << "\\endlastfoot\n";
2224 bool Tabular::isValidRow(row_type row) const
2226 if (!is_long_tabular)
2228 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2229 !row_info[row].endfoot && !row_info[row].endlastfoot;
2233 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2234 OutputParams const & runparams) const
2236 idx_type cell = cellIndex(i, 0);
2237 int ret = TeXTopHLine(os, i);
2238 if (row_info[i].top_space_default) {
2240 os << "\\addlinespace\n";
2242 os << "\\noalign{\\vskip\\doublerulesep}\n";
2244 } else if(!row_info[i].top_space.zero()) {
2246 os << "\\addlinespace["
2247 << from_ascii(row_info[i].top_space.asLatexString())
2250 os << "\\noalign{\\vskip"
2251 << from_ascii(row_info[i].top_space.asLatexString())
2257 for (col_type j = 0; j < columnCount(); ++j) {
2258 if (isPartOfMultiColumn(i, j))
2260 ret += TeXCellPreamble(os, cell);
2261 shared_ptr<InsetText> inset = getCellInset(cell);
2263 Paragraph const & par = inset->paragraphs().front();
2264 bool rtl = par.isRTL(buf.params())
2266 && getPWidth(cell).zero();
2269 if (par.getParLanguage(buf.params())->lang() ==
2272 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2274 // currently, remaning RTL languages are arabic_arabtex and hebrew
2278 ret += inset->latex(buf, os, runparams);
2282 ret += TeXCellPostamble(os, cell);
2283 if (!isLastCellInRow(cell)) { // not last cell in row
2288 os << "\\tabularnewline";
2289 if (row_info[i].bottom_space_default) {
2291 os << "\\addlinespace";
2293 os << "[\\doublerulesep]";
2294 } else if (!row_info[i].bottom_space.zero()) {
2296 os << "\\addlinespace";
2298 << from_ascii(row_info[i].bottom_space.asLatexString())
2303 ret += TeXBottomHLine(os, i);
2304 if (row_info[i].interline_space_default) {
2306 os << "\\addlinespace\n";
2308 os << "\\noalign{\\vskip\\doublerulesep}\n";
2310 } else if (!row_info[i].interline_space.zero()) {
2312 os << "\\addlinespace["
2313 << from_ascii(row_info[i].interline_space.asLatexString())
2316 os << "\\noalign{\\vskip"
2317 << from_ascii(row_info[i].interline_space.asLatexString())
2325 int Tabular::latex(Buffer const & buf, odocstream & os,
2326 OutputParams const & runparams) const
2330 //+---------------------------------------------------------------------
2331 //+ first the opening preamble +
2332 //+---------------------------------------------------------------------
2335 os << "\\begin{sideways}\n";
2338 if (is_long_tabular)
2339 os << "\\begin{longtable}{";
2341 os << "\\begin{tabular}{";
2342 for (col_type i = 0; i < columnCount(); ++i) {
2343 if (!use_booktabs && column_info[i].left_line)
2345 if (!column_info[i].align_special.empty()) {
2346 os << column_info[i].align_special;
2348 if (!column_info[i].p_width.zero()) {
2349 switch (column_info[i].alignment) {
2350 case LYX_ALIGN_LEFT:
2351 os << ">{\\raggedright}";
2353 case LYX_ALIGN_RIGHT:
2354 os << ">{\\raggedleft}";
2356 case LYX_ALIGN_CENTER:
2357 os << ">{\\centering}";
2359 case LYX_ALIGN_NONE:
2360 case LYX_ALIGN_BLOCK:
2361 case LYX_ALIGN_LAYOUT:
2362 case LYX_ALIGN_SPECIAL:
2366 switch (column_info[i].valignment) {
2367 case LYX_VALIGN_TOP:
2370 case LYX_VALIGN_MIDDLE:
2373 case LYX_VALIGN_BOTTOM:
2378 << from_ascii(column_info[i].p_width.asLatexString())
2381 switch (column_info[i].alignment) {
2382 case LYX_ALIGN_LEFT:
2385 case LYX_ALIGN_RIGHT:
2392 } // end if else !column_info[i].p_width
2393 } // end if else !column_info[i].align_special
2394 if (!use_booktabs && column_info[i].right_line)
2400 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2402 //+---------------------------------------------------------------------
2403 //+ the single row and columns (cells) +
2404 //+---------------------------------------------------------------------
2406 for (row_type i = 0; i < rowCount(); ++i) {
2407 if (isValidRow(i)) {
2408 ret += TeXRow(os, i, buf, runparams);
2409 if (is_long_tabular && row_info[i].newpage) {
2410 os << "\\newpage\n";
2416 //+---------------------------------------------------------------------
2417 //+ the closing of the tabular +
2418 //+---------------------------------------------------------------------
2420 if (is_long_tabular)
2421 os << "\\end{longtable}";
2423 os << "\\end{tabular}";
2425 os << "\n\\end{sideways}";
2433 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2434 OutputParams const & runparams) const
2437 idx_type cell = getFirstCellInRow(row);
2440 for (col_type j = 0; j < columnCount(); ++j) {
2441 if (isPartOfMultiColumn(row, j))
2444 os << "<entry align=\"";
2445 switch (getAlignment(cell)) {
2446 case LYX_ALIGN_LEFT:
2449 case LYX_ALIGN_RIGHT:
2457 os << "\" valign=\"";
2458 switch (getVAlignment(cell)) {
2459 case LYX_VALIGN_TOP:
2462 case LYX_VALIGN_BOTTOM:
2465 case LYX_VALIGN_MIDDLE:
2470 if (isMultiColumn(cell)) {
2471 os << " namest=\"col" << j << "\" ";
2472 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2476 ret += getCellInset(cell)->docbook(buf, os, runparams);
2485 int Tabular::docbook(Buffer const & buf, odocstream & os,
2486 OutputParams const & runparams) const
2490 //+---------------------------------------------------------------------
2491 //+ first the opening preamble +
2492 //+---------------------------------------------------------------------
2494 os << "<tgroup cols=\"" << columnCount()
2495 << "\" colsep=\"1\" rowsep=\"1\">\n";
2497 for (col_type i = 0; i < columnCount(); ++i) {
2498 os << "<colspec colname=\"col" << i << "\" align=\"";
2499 switch (column_info[i].alignment) {
2500 case LYX_ALIGN_LEFT:
2503 case LYX_ALIGN_RIGHT:
2511 if (runparams.flavor == OutputParams::XML)
2517 //+---------------------------------------------------------------------
2518 //+ Long Tabular case +
2519 //+---------------------------------------------------------------------
2521 // output header info
2522 if (haveLTHead() || haveLTFirstHead()) {
2525 for (row_type i = 0; i < rowCount(); ++i) {
2526 if (row_info[i].endhead || row_info[i].endfirsthead) {
2527 ret += docbookRow(buf, os, i, runparams);
2533 // output footer info
2534 if (haveLTFoot() || haveLTLastFoot()) {
2537 for (row_type i = 0; i < rowCount(); ++i) {
2538 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2539 ret += docbookRow(buf, os, i, runparams);
2546 //+---------------------------------------------------------------------
2547 //+ the single row and columns (cells) +
2548 //+---------------------------------------------------------------------
2552 for (row_type i = 0; i < rowCount(); ++i) {
2553 if (isValidRow(i)) {
2554 ret += docbookRow(buf, os, i, runparams);
2559 //+---------------------------------------------------------------------
2560 //+ the closing of the tabular +
2561 //+---------------------------------------------------------------------
2570 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2571 vector<unsigned int> const & clen) const
2573 idx_type const fcell = getFirstCellInRow(row);
2574 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2577 for (idx_type i = fcell; i < n; ++i) {
2587 for (idx_type i = fcell; i < n; ++i) {
2598 col_type column = cellColumn(i);
2599 int len = clen[column];
2600 while (column < columnCount() - 1
2601 && isPartOfMultiColumn(row, ++column))
2602 len += clen[column] + 4;
2603 os << docstring(len, ch);
2618 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2619 vector<unsigned int> const & clen) const
2621 idx_type const fcell = getFirstCellInRow(row);
2622 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2625 for (idx_type i = fcell; i < n; ++i) {
2626 if (bottomLine(i)) {
2635 for (idx_type i = fcell; i < n; ++i) {
2636 if (bottomLine(i)) {
2646 col_type column = cellColumn(i);
2647 int len = clen[column];
2648 while (column < columnCount() -1
2649 && isPartOfMultiColumn(row, ++column))
2650 len += clen[column] + 4;
2651 os << docstring(len, ch);
2652 if (bottomLine(i)) {
2666 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2667 OutputParams const & runparams,
2668 idx_type cell, row_type row, col_type column,
2669 vector<unsigned int> const & clen,
2670 bool onlydata) const
2672 odocstringstream sstr;
2673 getCellInset(cell)->plaintext(buf, sstr, runparams);
2685 unsigned int len1 = sstr.str().length();
2686 unsigned int len2 = clen[column];
2687 while (column < columnCount() -1
2688 && isPartOfMultiColumn(row, ++column))
2689 len2 += clen[column] + 4;
2692 switch (getAlignment(cell)) {
2694 case LYX_ALIGN_LEFT:
2697 case LYX_ALIGN_RIGHT:
2701 case LYX_ALIGN_CENTER:
2707 os << docstring(len1, ' ') << sstr.str()
2708 << docstring(len2, ' ');
2710 if (rightLine(cell))
2717 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2718 OutputParams const & runparams, int const depth,
2719 bool onlydata, char_type delim) const
2721 // first calculate the width of the single columns
2722 vector<unsigned int> clen(columnCount());
2725 // first all non (real) multicolumn cells!
2726 for (col_type j = 0; j < columnCount(); ++j) {
2728 for (row_type i = 0; i < rowCount(); ++i) {
2729 idx_type cell = cellIndex(i, j);
2730 if (isMultiColumnReal(cell))
2732 odocstringstream sstr;
2733 getCellInset(cell)->plaintext(buf, sstr, runparams);
2734 if (clen[j] < sstr.str().length())
2735 clen[j] = sstr.str().length();
2738 // then all (real) multicolumn cells!
2739 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) || isPartOfMultiColumn(i, j))
2744 odocstringstream sstr;
2745 getCellInset(cell)->plaintext(buf, sstr, runparams);
2746 int len = int(sstr.str().length());
2747 idx_type const n = cells_in_multicolumn(cell);
2748 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2750 if (len > int(clen[j + n - 1]))
2751 clen[j + n - 1] = len;
2756 for (row_type i = 0; i < rowCount(); ++i) {
2757 if (!onlydata && plaintextTopHLine(os, i, clen))
2758 os << docstring(depth * 2, ' ');
2759 for (col_type j = 0; j < columnCount(); ++j) {
2760 if (isPartOfMultiColumn(i, j))
2762 if (onlydata && j > 0)
2763 // we don't use operator<< for single UCS4 character.
2764 // see explanation in docstream.h
2766 plaintextPrintCell(buf, os, runparams,
2767 cell, i, j, clen, onlydata);
2772 os << docstring(depth * 2, ' ');
2773 if (plaintextBottomHLine(os, i, clen))
2774 os << docstring(depth * 2, ' ');
2780 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2782 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2786 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2787 col_type column) const
2789 return cell_info[row][column].inset;
2793 void Tabular::setCellInset(row_type row, col_type column,
2794 shared_ptr<InsetText> ins) const
2796 cell_info[row][column].inset = ins;
2801 Tabular::getCellFromInset(Inset const * inset) const
2803 // is this inset part of the tabular?
2805 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2806 BOOST_ASSERT(false);
2809 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2810 if (getCellInset(cell).get() == inset) {
2811 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2812 << "cell=" << cell);
2816 // We should have found a cell at this point
2817 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2818 << inset << " not found!" << endl;
2819 BOOST_ASSERT(false);
2825 void Tabular::validate(LaTeXFeatures & features) const
2827 features.require("NeedTabularnewline");
2829 features.require("booktabs");
2830 if (isLongTabular())
2831 features.require("longtable");
2833 features.require("rotating");
2834 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2835 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2836 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2837 features.require("array");
2838 getCellInset(cell)->validate(features);
2843 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2845 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2846 ParagraphList::const_iterator cit = parlist.begin();
2847 ParagraphList::const_iterator end = parlist.end();
2849 for (; cit != end; ++cit)
2850 for (int i = 0; i < cit->size(); ++i)
2851 if (cit->isNewline(i))
2858 /////////////////////////////////////////////////////////////////////
2862 /////////////////////////////////////////////////////////////////////
2864 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2866 : tabular(buf.params(), max(rows, row_type(1)),
2867 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2871 InsetTabular::InsetTabular(InsetTabular const & tab)
2872 : Inset(tab), tabular(tab.tabular),
2873 buffer_(tab.buffer_), scx_(0)
2877 InsetTabular::~InsetTabular()
2879 InsetTabularMailer(*this).hideDialog();
2883 Inset * InsetTabular::clone() const
2885 return new InsetTabular(*this);
2889 Buffer const & InsetTabular::buffer() const
2895 void InsetTabular::buffer(Buffer const * b)
2901 bool InsetTabular::insetAllowed(InsetCode code) const
2903 if (code == MATHMACRO_CODE)
2910 void InsetTabular::write(Buffer const & buf, ostream & os) const
2912 os << "Tabular" << endl;
2913 tabular.write(buf, os);
2917 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2919 bool const old_format = (lex.getString() == "\\LyXTable");
2921 tabular.read(buf, lex);
2927 string token = lex.getString();
2928 while (lex && token != "\\end_inset") {
2930 token = lex.getString();
2933 lex.printError("Missing \\end_inset at this point. "
2939 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2941 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2942 // mi.base.textwidth << "\n";
2944 lyxerr << "InsetTabular::metrics: need bv" << endl;
2945 BOOST_ASSERT(false);
2949 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2952 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2953 if (tabular.isPartOfMultiColumn(i, j))
2954 // Multicolumn cell, but not first one
2959 if (tabular.cell_info[i][j].multicolumn ==
2960 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2961 p_width = tabular.cellinfo_of_cell(cell).p_width;
2963 p_width = tabular.column_info[j].p_width;
2964 if (!p_width.zero())
2965 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2966 tabular.getCellInset(cell)->metrics(m, dim);
2967 if (!p_width.zero())
2968 dim.wid = m.base.textwidth;
2969 tabular.setCellWidth(cell, dim.wid);
2970 if (p_width.zero()) {
2971 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2972 // FIXME there must be a way to get rid of
2973 // the second metrics call
2974 tabular.getCellInset(cell)->metrics(m, dim);
2976 maxAsc = max(maxAsc, dim.asc);
2977 maxDesc = max(maxDesc, dim.des);
2980 int const top_space = tabular.row_info[i].top_space_default ?
2981 default_line_space :
2982 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2983 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2984 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2985 default_line_space :
2986 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2987 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2990 dim.asc = tabular.rowAscent(0);
2991 dim.des = tabular.height() - dim.asc;
2992 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2996 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2998 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2999 BufferView * bv = pi.base.bv;
3001 // FIXME: As the full backrgound is painted in drawSelection(),
3002 // we have no choice but to do a full repaint for the Text cells.
3003 pi.full_repaint = true;
3005 resetPos(bv->cursor());
3008 x += ADD_TO_TABULAR_WIDTH;
3010 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3013 first_visible_cell = Tabular::npos;
3014 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3016 int const a = tabular.rowAscent(i);
3017 int const d = tabular.rowDescent(i);
3018 idx = tabular.cellIndex(i, 0);
3019 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3020 if (tabular.isPartOfMultiColumn(i, j))
3022 if (first_visible_cell == Tabular::npos)
3023 first_visible_cell = idx;
3025 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3026 // Cache the Inset position.
3027 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3028 if (nx + tabular.columnWidth(idx) < 0
3029 || nx > bv->workWidth()
3031 || y - a > bv->workHeight()) {
3032 pi.pain.setDrawingEnabled(false);
3033 cell(idx)->draw(pi, cx, y);
3034 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3035 pi.pain.setDrawingEnabled(original_drawing_state);
3037 cell(idx)->draw(pi, cx, y);
3038 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3040 nx += tabular.columnWidth(idx);
3044 if (i + 1 < tabular.rowCount())
3045 y += d + tabular.rowAscent(i + 1) +
3046 tabular.getAdditionalHeight(i + 1);
3051 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3053 Cursor & cur = pi.base.bv->cursor();
3055 x += scx_ + ADD_TO_TABULAR_WIDTH;
3057 // FIXME: it is wrong to completely paint the background
3058 // if we want to do single row painting.
3060 // Paint background of current tabular
3061 int const w = tabular.width();
3062 int const h = tabular.height();
3063 int yy = y - tabular.rowAscent(0);
3064 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3066 if (!cur.selection())
3068 if (&cur.inset() != this)
3074 if (tablemode(cur)) {
3077 getSelection(cur, rs, re, cs, ce);
3078 y -= tabular.rowAscent(0);
3079 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3080 int const a = tabular.rowAscent(j);
3081 int const h = a + tabular.rowDescent(j);
3083 y += tabular.getAdditionalHeight(j);
3084 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3085 if (tabular.isPartOfMultiColumn(j, i))
3087 idx_type const cell =
3088 tabular.cellIndex(j, i);
3089 int const w = tabular.columnWidth(cell);
3090 if (i >= cs && i <= ce && j >= rs && j <= re)
3091 pi.pain.fillRectangle(xx, y, w, h,
3099 x += getCellXPos(cur.idx());
3100 x += tabular.getBeginningOfTextInCell(cur.idx());
3101 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3106 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3107 row_type row, idx_type cell, bool erased) const
3109 int x2 = x + tabular.columnWidth(cell);
3110 bool on_off = false;
3111 ColorCode col = Color_tabularline;
3112 ColorCode onoffcol = Color_tabularonoffline;
3115 col = Color_deletedtext;
3116 onoffcol = Color_deletedtext;
3119 if (!tabular.topAlreadyDrawn(cell)) {
3120 on_off = !tabular.topLine(cell);
3121 pain.line(x, y - tabular.rowAscent(row),
3122 x2, y - tabular.rowAscent(row),
3123 on_off ? onoffcol : col,
3124 on_off ? Painter::line_onoffdash : Painter::line_solid);
3126 on_off = !tabular.bottomLine(cell);
3127 pain.line(x, y + tabular.rowDescent(row),
3128 x2, y + tabular.rowDescent(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3131 if (!tabular.leftAlreadyDrawn(cell)) {
3132 on_off = !tabular.leftLine(cell);
3133 pain.line(x, y - tabular.rowAscent(row),
3134 x, y + tabular.rowDescent(row),
3135 on_off ? onoffcol : col,
3136 on_off ? Painter::line_onoffdash : Painter::line_solid);
3138 on_off = !tabular.rightLine(cell);
3139 pain.line(x2 - tabular.getAdditionalWidth(cell),
3140 y - tabular.rowAscent(row),
3141 x2 - tabular.getAdditionalWidth(cell),
3142 y + tabular.rowDescent(row),
3143 on_off ? onoffcol : col,
3144 on_off ? Painter::line_onoffdash : Painter::line_solid);
3148 docstring const InsetTabular::editMessage() const
3150 return _("Opened table");
3154 void InsetTabular::edit(Cursor & cur, bool left)
3156 //lyxerr << "InsetTabular::edit: " << this << endl;
3158 cur.selection() = false;
3161 if (isRightToLeft(cur))
3162 cur.idx() = tabular.getLastCellInRow(0);
3168 if (isRightToLeft(cur))
3169 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3171 cur.idx() = tabular.cellCount() - 1;
3173 cur.pos() = cur.lastpos(); // FIXME crude guess
3175 // FIXME: this accesses the position cache before it is initialized
3177 //cur.bv().fitCursor();
3181 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3183 // In a longtable, tell captions what the current float is
3184 Counters & cnts = buf.params().getTextClass().counters();
3185 string const saveflt = cnts.current_float();
3186 if (tabular.isLongTabular())
3187 cnts.current_float("table");
3189 ParIterator it2 = it;
3191 size_t const end = it2.nargs();
3192 for ( ; it2.idx() < end; it2.top().forwardIdx())
3193 lyx::updateLabels(buf, it2);
3196 if (tabular.isLongTabular())
3197 cnts.current_float(saveflt);
3201 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3203 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3204 << "\n cur:" << cur);
3205 CursorSlice sl = cur.top();
3206 Cursor & bvcur = cur.bv().cursor();
3208 switch (cmd.action) {
3210 case LFUN_MOUSE_PRESS:
3211 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3213 // do not reset cursor/selection if we have selected
3214 // some cells (bug 2715).
3215 if (cmd.button() == mouse_button::button3
3216 && &bvcur.selBegin().inset() == this
3217 && tablemode(bvcur))
3220 // Let InsetText do it
3221 cell(cur.idx())->dispatch(cur, cmd);
3223 case LFUN_MOUSE_MOTION:
3224 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3225 if (cmd.button() == mouse_button::button1) {
3226 // only accept motions to places not deeper nested than the real anchor
3227 if (bvcur.anchor_.hasPart(cur)) {
3228 // only update if selection changes
3229 if (bvcur.idx() == cur.idx() &&
3230 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3232 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3233 bvcur.setCursor(cur);
3234 bvcur.selection() = true;
3240 case LFUN_MOUSE_RELEASE:
3241 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3242 if (cmd.button() == mouse_button::button3)
3243 InsetTabularMailer(*this).showDialog(&cur.bv());
3246 case LFUN_CELL_BACKWARD:
3248 cur.selection() = false;
3251 case LFUN_CELL_FORWARD:
3253 cur.selection() = false;
3256 case LFUN_CHAR_FORWARD_SELECT:
3257 case LFUN_CHAR_FORWARD:
3258 cell(cur.idx())->dispatch(cur, cmd);
3259 if (!cur.result().dispatched()) {
3261 if (sl == cur.top())
3262 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3268 case LFUN_CHAR_BACKWARD_SELECT:
3269 case LFUN_CHAR_BACKWARD:
3270 cell(cur.idx())->dispatch(cur, cmd);
3271 if (!cur.result().dispatched()) {
3273 if (sl == cur.top())
3274 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3280 case LFUN_CHAR_RIGHT_SELECT:
3281 case LFUN_CHAR_RIGHT:
3282 //FIXME: for visual cursor, really move right
3283 if (isRightToLeft(cur))
3284 lyx::dispatch(FuncRequest(
3285 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3286 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3288 lyx::dispatch(FuncRequest(
3289 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3290 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3293 case LFUN_CHAR_LEFT_SELECT:
3294 case LFUN_CHAR_LEFT:
3295 //FIXME: for visual cursor, really move left
3296 if (isRightToLeft(cur))
3297 lyx::dispatch(FuncRequest(
3298 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3299 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3301 lyx::dispatch(FuncRequest(
3302 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3303 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3306 case LFUN_DOWN_SELECT:
3308 cell(cur.idx())->dispatch(cur, cmd);
3309 cur.dispatched(); // override the cell's decision
3310 if (sl == cur.top())
3311 // if our Text didn't do anything to the cursor
3312 // then we try to put the cursor into the cell below
3313 // setting also the right targetX.
3314 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3315 cur.idx() = tabular.getCellBelow(cur.idx());
3317 TextMetrics const & tm =
3318 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3319 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3321 if (sl == cur.top()) {
3322 // we trick it to go to forward after leaving the
3324 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3329 case LFUN_UP_SELECT:
3331 cell(cur.idx())->dispatch(cur, cmd);
3332 cur.dispatched(); // override the cell's decision
3333 if (sl == cur.top())
3334 // if our Text didn't do anything to the cursor
3335 // then we try to put the cursor into the cell above
3336 // setting also the right targetX.
3337 if (tabular.cellRow(cur.idx()) != 0) {
3338 cur.idx() = tabular.getCellAbove(cur.idx());
3339 cur.pit() = cur.lastpit();
3340 Text const * text = cell(cur.idx())->getText(0);
3341 TextMetrics const & tm = cur.bv().textMetrics(text);
3342 ParagraphMetrics const & pm =
3343 tm.parMetrics(cur.lastpit());
3344 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3346 if (sl == cur.top()) {
3347 cmd = FuncRequest(LFUN_UP);
3352 // case LFUN_SCREEN_DOWN: {
3353 // //if (hasSelection())
3354 // // cur.selection() = false;
3355 // col_type const col = tabular.cellColumn(cur.idx());
3356 // int const t = cur.bv().top_y() + cur.bv().height();
3357 // if (t < yo() + tabular.getHeightOfTabular()) {
3358 // cur.bv().scrollDocView(t);
3359 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3361 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3368 // case LFUN_SCREEN_UP: {
3369 // //if (hasSelection())
3370 // // cur.selection() = false;
3371 // col_type const col = tabular.cellColumn(cur.idx());
3372 // int const t = cur.bv().top_y() + cur.bv().height();
3374 // cur.bv().scrollDocView(t);
3378 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3382 // cur.par() = cur.lastpar();
3383 // cur.pos() = cur.lastpos();
3387 case LFUN_LAYOUT_TABULAR:
3388 InsetTabularMailer(*this).showDialog(&cur.bv());
3391 case LFUN_INSET_DIALOG_UPDATE:
3392 InsetTabularMailer(*this).updateDialog(&cur.bv());
3395 case LFUN_TABULAR_FEATURE:
3396 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3400 // insert file functions
3401 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3402 case LFUN_FILE_INSERT_PLAINTEXT: {
3404 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3405 FileName(to_utf8(cmd.argument())));
3408 cur.recordUndoInset(INSERT_UNDO);
3409 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3410 // content has been replaced,
3411 // so cursor might be invalid
3412 cur.pos() = cur.lastpos();
3413 cur.pit() = cur.lastpit();
3414 bvcur.setCursor(cur);
3421 if (tablemode(cur)) {
3422 if (copySelection(cur)) {
3423 cur.recordUndoInset(DELETE_UNDO);
3428 cell(cur.idx())->dispatch(cur, cmd);
3431 case LFUN_CHAR_DELETE_BACKWARD:
3432 case LFUN_CHAR_DELETE_FORWARD:
3433 if (tablemode(cur)) {
3434 cur.recordUndoInset(DELETE_UNDO);
3438 cell(cur.idx())->dispatch(cur, cmd);
3442 if (!cur.selection())
3444 if (tablemode(cur)) {
3448 cell(cur.idx())->dispatch(cur, cmd);
3451 case LFUN_CLIPBOARD_PASTE:
3452 case LFUN_PRIMARY_SELECTION_PASTE: {
3453 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3454 theClipboard().getAsText() :
3455 theSelection().get();
3458 // pass to InsertPlaintextString, but
3459 // only if we have multi-cell content
3460 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3461 cur.recordUndoInset(INSERT_UNDO);
3462 if (insertPlaintextString(cur.bv(), clip, false)) {
3463 // content has been replaced,
3464 // so cursor might be invalid
3465 cur.pos() = cur.lastpos();
3466 cur.pit() = cur.lastpit();
3467 bvcur.setCursor(cur);
3471 // Let the cell handle normal text
3472 cell(cur.idx())->dispatch(cur, cmd);
3477 if (tabularStackDirty() && theClipboard().isInternal() ||
3478 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3479 cur.recordUndoInset(INSERT_UNDO);
3480 pasteClipboard(cur);
3483 cell(cur.idx())->dispatch(cur, cmd);
3486 case LFUN_FONT_EMPH:
3487 case LFUN_FONT_BOLD:
3488 case LFUN_FONT_ROMAN:
3489 case LFUN_FONT_NOUN:
3490 case LFUN_FONT_ITAL:
3491 case LFUN_FONT_FRAK:
3492 case LFUN_FONT_TYPEWRITER:
3493 case LFUN_FONT_SANS:
3494 case LFUN_FONT_FREE_APPLY:
3495 case LFUN_FONT_FREE_UPDATE:
3496 case LFUN_FONT_SIZE:
3497 case LFUN_FONT_UNDERLINE:
3499 case LFUN_WORD_CAPITALIZE:
3500 case LFUN_WORD_UPCASE:
3501 case LFUN_WORD_LOWCASE:
3502 case LFUN_CHARS_TRANSPOSE:
3503 if (tablemode(cur)) {
3506 getSelection(cur, rs, re, cs, ce);
3507 Cursor tmpcur = cur;
3508 for (row_type i = rs; i <= re; ++i) {
3509 for (col_type j = cs; j <= ce; ++j) {
3510 // cursor follows cell:
3511 tmpcur.idx() = tabular.cellIndex(i, j);
3512 // select this cell only:
3515 tmpcur.resetAnchor();
3516 tmpcur.pit() = tmpcur.lastpit();
3517 tmpcur.pos() = tmpcur.top().lastpos();
3518 tmpcur.setCursor(tmpcur);
3519 tmpcur.setSelection();
3520 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3525 cell(cur.idx())->dispatch(cur, cmd);
3529 // we try to handle this event in the insets dispatch function.
3530 cell(cur.idx())->dispatch(cur, cmd);
3536 // function sets an object as defined in func_status.h:
3537 // states OK, Unknown, Disabled, On, Off.
3538 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3539 FuncStatus & status) const
3541 switch (cmd.action) {
3542 case LFUN_TABULAR_FEATURE: {
3543 int action = Tabular::LAST_ACTION;
3545 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3546 string const tmp = tabularFeature[i].feature;
3547 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3548 action = tabularFeature[i].action;
3552 if (action == Tabular::LAST_ACTION) {
3554 status.unknown(true);
3558 string const argument
3559 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3561 row_type sel_row_start = 0;
3562 row_type sel_row_end = 0;
3563 col_type sel_col_start = 0;
3564 col_type sel_col_end = 0;
3565 Tabular::ltType dummyltt;
3568 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3571 case Tabular::SET_PWIDTH:
3572 case Tabular::SET_MPWIDTH:
3573 case Tabular::SET_SPECIAL_COLUMN:
3574 case Tabular::SET_SPECIAL_MULTI:
3575 case Tabular::APPEND_ROW:
3576 case Tabular::APPEND_COLUMN:
3577 case Tabular::DELETE_ROW:
3578 case Tabular::DELETE_COLUMN:
3579 case Tabular::COPY_ROW:
3580 case Tabular::COPY_COLUMN:
3581 case Tabular::SET_ALL_LINES:
3582 case Tabular::UNSET_ALL_LINES:
3583 case Tabular::SET_TOP_SPACE:
3584 case Tabular::SET_BOTTOM_SPACE:
3585 case Tabular::SET_INTERLINE_SPACE:
3589 case Tabular::MULTICOLUMN:
3590 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3593 case Tabular::M_TOGGLE_LINE_TOP:
3595 case Tabular::TOGGLE_LINE_TOP:
3596 status.setOnOff(tabular.topLine(cur.idx(), flag));
3599 case Tabular::M_TOGGLE_LINE_BOTTOM:
3601 case Tabular::TOGGLE_LINE_BOTTOM:
3602 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3605 case Tabular::M_TOGGLE_LINE_LEFT:
3607 case Tabular::TOGGLE_LINE_LEFT:
3608 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3611 case Tabular::M_TOGGLE_LINE_RIGHT:
3613 case Tabular::TOGGLE_LINE_RIGHT:
3614 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3617 case Tabular::M_ALIGN_LEFT:
3619 case Tabular::ALIGN_LEFT:
3620 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3623 case Tabular::M_ALIGN_RIGHT:
3625 case Tabular::ALIGN_RIGHT:
3626 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3629 case Tabular::M_ALIGN_CENTER:
3631 case Tabular::ALIGN_CENTER:
3632 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3635 case Tabular::ALIGN_BLOCK:
3636 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3637 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3640 case Tabular::M_VALIGN_TOP:
3642 case Tabular::VALIGN_TOP:
3644 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3647 case Tabular::M_VALIGN_BOTTOM:
3649 case Tabular::VALIGN_BOTTOM:
3651 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3654 case Tabular::M_VALIGN_MIDDLE:
3656 case Tabular::VALIGN_MIDDLE:
3658 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3661 case Tabular::SET_LONGTABULAR:
3662 status.setOnOff(tabular.isLongTabular());
3665 case Tabular::UNSET_LONGTABULAR:
3666 status.setOnOff(!tabular.isLongTabular());
3669 case Tabular::TOGGLE_ROTATE_TABULAR:
3670 case Tabular::SET_ROTATE_TABULAR:
3671 status.setOnOff(tabular.getRotateTabular());
3674 case Tabular::UNSET_ROTATE_TABULAR:
3675 status.setOnOff(!tabular.getRotateTabular());
3678 case Tabular::TOGGLE_ROTATE_CELL:
3679 case Tabular::SET_ROTATE_CELL:
3680 status.setOnOff(!oneCellHasRotationState(false,
3681 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3684 case Tabular::UNSET_ROTATE_CELL:
3685 status.setOnOff(!oneCellHasRotationState(true,
3686 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3689 case Tabular::SET_USEBOX:
3690 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3693 case Tabular::SET_LTFIRSTHEAD:
3694 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3697 case Tabular::UNSET_LTFIRSTHEAD:
3698 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3701 case Tabular::SET_LTHEAD:
3702 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3705 case Tabular::UNSET_LTHEAD:
3706 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3709 case Tabular::SET_LTFOOT:
3710 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3713 case Tabular::UNSET_LTFOOT:
3714 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3717 case Tabular::SET_LTLASTFOOT:
3718 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3721 case Tabular::UNSET_LTLASTFOOT:
3722 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3725 case Tabular::SET_LTNEWPAGE:
3726 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3729 case Tabular::SET_BOOKTABS:
3730 status.setOnOff(tabular.useBookTabs());
3733 case Tabular::UNSET_BOOKTABS:
3734 status.setOnOff(!tabular.useBookTabs());
3739 status.enabled(false);
3745 // These are only enabled inside tabular
3746 case LFUN_CELL_BACKWARD:
3747 case LFUN_CELL_FORWARD:
3748 status.enabled(true);
3751 // disable these with multiple cells selected
3752 case LFUN_INSET_INSERT:
3753 case LFUN_TABULAR_INSERT:
3754 case LFUN_FLEX_INSERT:
3755 case LFUN_FLOAT_INSERT:
3756 case LFUN_FLOAT_WIDE_INSERT:
3757 case LFUN_FOOTNOTE_INSERT:
3758 case LFUN_MARGINALNOTE_INSERT:
3759 case LFUN_MATH_INSERT:
3760 case LFUN_MATH_MODE:
3761 case LFUN_MATH_MUTATE:
3762 case LFUN_MATH_DISPLAY:
3763 case LFUN_NOTE_INSERT:
3764 case LFUN_OPTIONAL_INSERT:
3765 case LFUN_BOX_INSERT:
3766 case LFUN_BRANCH_INSERT:
3767 case LFUN_WRAP_INSERT:
3768 case LFUN_ERT_INSERT: {
3769 if (tablemode(cur)) {
3770 status.enabled(false);
3773 return cell(cur.idx())->getStatus(cur, cmd, status);
3776 // disable in non-fixed-width cells
3778 case LFUN_BREAK_PARAGRAPH:
3779 case LFUN_BREAK_PARAGRAPH_SKIP: {
3780 if (tabular.getPWidth(cur.idx()).zero()) {
3781 status.enabled(false);
3784 return cell(cur.idx())->getStatus(cur, cmd, status);
3788 if (tabularStackDirty() && theClipboard().isInternal()) {
3789 status.enabled(true);
3792 return cell(cur.idx())->getStatus(cur, cmd, status);
3794 case LFUN_INSET_MODIFY:
3795 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3796 status.enabled(true);
3802 // we try to handle this event in the insets dispatch function.
3803 return cell(cur.idx())->getStatus(cur, cmd, status);
3808 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3809 OutputParams const & runparams) const
3811 return tabular.latex(buf, os, runparams);
3815 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3816 OutputParams const & runparams) const
3818 os << '\n'; // output table on a new line
3819 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3820 tabular.plaintext(buf, os, runparams, dp, false, 0);
3821 return PLAINTEXT_NEWLINE;
3825 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3826 OutputParams const & runparams) const
3831 // FIXME: Why not pass a proper DocIterator here?
3833 // if the table is inside a float it doesn't need the informaltable
3834 // wrapper. Search for it.
3835 for (master = owner(); master; master = master->owner())
3836 if (master->lyxCode() == FLOAT_CODE)
3841 os << "<informaltable>";
3844 ret += tabular.docbook(buf, os, runparams);
3846 os << "</informaltable>";
3853 void InsetTabular::validate(LaTeXFeatures & features) const
3855 tabular.validate(features);
3859 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3861 return tabular.getCellInset(idx);
3865 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3867 return tabular.getCellInset(idx);
3871 void InsetTabular::cursorPos(BufferView const & bv,
3872 CursorSlice const & sl, bool boundary, int & x, int & y) const
3874 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3876 // y offset correction
3877 int const row = tabular.cellRow(sl.idx());
3878 for (int i = 0; i <= row; ++i) {
3880 y += tabular.rowAscent(i);
3881 y += tabular.getAdditionalHeight(i);
3884 y += tabular.rowDescent(i);
3887 // x offset correction
3888 int const col = tabular.cellColumn(sl.idx());
3889 int idx = tabular.cellIndex(row, 0);
3890 for (int j = 0; j < col; ++j) {
3891 if (tabular.isPartOfMultiColumn(row, j))
3893 x += tabular.columnWidth(idx);
3896 x += tabular.getBeginningOfTextInCell(idx);
3897 x += ADD_TO_TABULAR_WIDTH;
3902 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3906 Inset const & inset = *tabular.getCellInset(cell);
3907 Point o = bv.coordCache().getInsets().xy(&inset);
3908 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3909 int const xend = xbeg + tabular.columnWidth(cell);
3910 row_type const row = tabular.cellRow(cell);
3911 int const ybeg = o.y_ - tabular.rowAscent(row) -
3912 tabular.getAdditionalHeight(row);
3913 int const yend = o.y_ + tabular.rowDescent(row);
3925 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3926 // << " ybeg=" << ybeg << " yend=" << yend
3927 // << " xx=" << xx << " yy=" << yy
3928 // << " dist=" << xx + yy << endl;
3933 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3935 //lyxerr << "InsetTabular::editXY: " << this << endl;
3936 cur.selection() = false;
3938 cur.idx() = getNearestCell(cur.bv(), x, y);
3940 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3944 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3946 cur.idx() = getNearestCell(cur.bv(), x, y);
3947 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3951 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3953 idx_type idx_min = 0;
3954 int dist_min = numeric_limits<int>::max();
3955 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3956 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3957 int const d = dist(bv, i, x, y);
3968 int InsetTabular::getCellXPos(idx_type const cell) const
3972 for (; !tabular.isFirstCellInRow(c); --c)
3975 for (; c < cell; ++c)
3976 lx += tabular.columnWidth(c);
3982 void InsetTabular::resetPos(Cursor & cur) const
3984 BufferView & bv = cur.bv();
3985 int const maxwidth = bv.workWidth();
3987 if (&cur.inset() != this) {
3991 int const X2 = maxwidth;
3992 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3993 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3994 int const x2 = x1 + tabular.columnWidth(cur.idx());
3997 scx_ = X1 + 20 - x1;
3999 scx_ = X2 - 20 - x2;
4004 cur.updateFlags(Update::Force | Update::FitCursor);
4008 void InsetTabular::moveNextCell(Cursor & cur)
4010 if (isRightToLeft(cur)) {
4011 if (tabular.isFirstCellInRow(cur.idx())) {
4012 row_type const row = tabular.cellRow(cur.idx());
4013 if (row == tabular.rowCount() - 1)
4015 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4022 if (tabular.isLastCell(cur.idx()))
4032 void InsetTabular::movePrevCell(Cursor & cur)
4034 if (isRightToLeft(cur)) {
4035 if (tabular.isLastCellInRow(cur.idx())) {
4036 row_type const row = tabular.cellRow(cur.idx());
4039 cur.idx() = tabular.getFirstCellInRow(row);
4040 cur.idx() = tabular.getCellAbove(cur.idx());
4042 if (tabular.isLastCell(cur.idx()))
4047 if (cur.idx() == 0) // first cell
4051 cur.pit() = cur.lastpit();
4052 cur.pos() = cur.lastpos();
4054 // FIXME: this accesses the position cache before it is initialized
4059 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4061 Tabular::Feature action = Tabular::LAST_ACTION;
4064 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4065 string const tmp = tabularFeature[i].feature;
4067 if (tmp == what.substr(0, tmp.length())) {
4068 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4069 //tabularFeatures[i].feature.length()))
4070 action = tabularFeature[i].action;
4074 if (action == Tabular::LAST_ACTION)
4078 ltrim(what.substr(tabularFeature[i].feature.length()));
4079 tabularFeatures(cur, action, val);
4084 static void checkLongtableSpecial(Tabular::ltType & ltt,
4085 string const & special, bool & flag)
4087 if (special == "dl_above") {
4090 } else if (special == "dl_below") {
4091 ltt.bottomDL = flag;
4093 } else if (special == "empty") {
4102 bool InsetTabular::oneCellHasRotationState(bool rotated,
4103 row_type row_start, row_type row_end,
4104 col_type col_start, col_type col_end) const {
4106 for (row_type i = row_start; i <= row_end; ++i) {
4107 for (col_type j = col_start; j <= col_end; ++j) {
4108 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4117 void InsetTabular::tabularFeatures(Cursor & cur,
4118 Tabular::Feature feature, string const & value)
4120 BufferView & bv = cur.bv();
4121 col_type sel_col_start;
4122 col_type sel_col_end;
4123 row_type sel_row_start;
4124 row_type sel_row_end;
4125 bool setLines = false;
4126 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4127 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4131 case Tabular::M_ALIGN_LEFT:
4132 case Tabular::ALIGN_LEFT:
4133 setAlign = LYX_ALIGN_LEFT;
4136 case Tabular::M_ALIGN_RIGHT:
4137 case Tabular::ALIGN_RIGHT:
4138 setAlign = LYX_ALIGN_RIGHT;
4141 case Tabular::M_ALIGN_CENTER:
4142 case Tabular::ALIGN_CENTER:
4143 setAlign = LYX_ALIGN_CENTER;
4146 case Tabular::ALIGN_BLOCK:
4147 setAlign = LYX_ALIGN_BLOCK;
4150 case Tabular::M_VALIGN_TOP:
4151 case Tabular::VALIGN_TOP:
4152 setVAlign = Tabular::LYX_VALIGN_TOP;
4155 case Tabular::M_VALIGN_BOTTOM:
4156 case Tabular::VALIGN_BOTTOM:
4157 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4160 case Tabular::M_VALIGN_MIDDLE:
4161 case Tabular::VALIGN_MIDDLE:
4162 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4169 cur.recordUndoInset(ATOMIC_UNDO);
4171 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4172 row_type const row = tabular.cellRow(cur.idx());
4173 col_type const column = tabular.cellColumn(cur.idx());
4175 Tabular::ltType ltt;
4179 case Tabular::SET_PWIDTH: {
4180 Length const len(value);
4181 tabular.setColumnPWidth(cur, cur.idx(), len);
4183 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4184 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4188 case Tabular::SET_MPWIDTH:
4189 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4192 case Tabular::SET_SPECIAL_COLUMN:
4193 case Tabular::SET_SPECIAL_MULTI:
4194 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4197 case Tabular::APPEND_ROW:
4198 // append the row into the tabular
4199 tabular.appendRow(bv.buffer().params(), cur.idx());
4202 case Tabular::APPEND_COLUMN:
4203 // append the column into the tabular
4204 tabular.appendColumn(bv.buffer().params(), cur.idx());
4205 cur.idx() = tabular.cellIndex(row, column);
4208 case Tabular::DELETE_ROW:
4209 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4210 tabular.deleteRow(sel_row_start);
4211 if (sel_row_start >= tabular.rowCount())
4213 cur.idx() = tabular.cellIndex(sel_row_start, column);
4216 cur.selection() = false;
4219 case Tabular::DELETE_COLUMN:
4220 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4221 tabular.deleteColumn(sel_col_start);
4222 if (sel_col_start >= tabular.columnCount())
4224 cur.idx() = tabular.cellIndex(row, sel_col_start);
4227 cur.selection() = false;
4230 case Tabular::COPY_ROW:
4231 tabular.copyRow(bv.buffer().params(), row);
4234 case Tabular::COPY_COLUMN:
4235 tabular.copyColumn(bv.buffer().params(), column);
4236 cur.idx() = tabular.cellIndex(row, column);
4239 case Tabular::M_TOGGLE_LINE_TOP:
4241 case Tabular::TOGGLE_LINE_TOP: {
4242 bool lineSet = !tabular.topLine(cur.idx(), flag);
4243 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4244 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4246 tabular.cellIndex(i, j),
4251 case Tabular::M_TOGGLE_LINE_BOTTOM:
4253 case Tabular::TOGGLE_LINE_BOTTOM: {
4254 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4255 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4256 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4257 tabular.setBottomLine(
4258 tabular.cellIndex(i, j),
4264 case Tabular::M_TOGGLE_LINE_LEFT:
4266 case Tabular::TOGGLE_LINE_LEFT: {
4267 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4268 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4270 tabular.setLeftLine(
4271 tabular.cellIndex(i,j),
4277 case Tabular::M_TOGGLE_LINE_RIGHT:
4279 case Tabular::TOGGLE_LINE_RIGHT: {
4280 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4281 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4282 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4283 tabular.setRightLine(
4284 tabular.cellIndex(i,j),
4290 case Tabular::M_ALIGN_LEFT:
4291 case Tabular::M_ALIGN_RIGHT:
4292 case Tabular::M_ALIGN_CENTER:
4294 case Tabular::ALIGN_LEFT:
4295 case Tabular::ALIGN_RIGHT:
4296 case Tabular::ALIGN_CENTER:
4297 case Tabular::ALIGN_BLOCK:
4298 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4299 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4300 tabular.setAlignment(
4301 tabular.cellIndex(i, j),
4306 case Tabular::M_VALIGN_TOP:
4307 case Tabular::M_VALIGN_BOTTOM:
4308 case Tabular::M_VALIGN_MIDDLE:
4310 case Tabular::VALIGN_TOP:
4311 case Tabular::VALIGN_BOTTOM:
4312 case Tabular::VALIGN_MIDDLE:
4313 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4314 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4315 tabular.setVAlignment(
4316 tabular.cellIndex(i, j),
4320 case Tabular::MULTICOLUMN: {
4321 if (sel_row_start != sel_row_end) {
4322 // FIXME: Need I say it ? This is horrible.
4324 Alert::error(_("Error setting multicolumn"),
4325 _("You cannot set multicolumn vertically."));
4328 if (!cur.selection()) {
4329 // just multicol for one single cell
4330 // check whether we are completely in a multicol
4331 if (tabular.isMultiColumn(cur.idx()))
4332 tabular.unsetMultiColumn(cur.idx());
4334 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4337 // we have a selection so this means we just add all this
4338 // cells to form a multicolumn cell
4339 idx_type const s_start = cur.selBegin().idx();
4340 idx_type const s_end = cur.selEnd().idx();
4341 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4342 cur.idx() = s_start;
4345 cur.selection() = false;
4349 case Tabular::SET_ALL_LINES:
4351 case Tabular::UNSET_ALL_LINES:
4352 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4353 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4354 tabular.setAllLines(
4355 tabular.cellIndex(i,j), setLines);
4358 case Tabular::SET_LONGTABULAR:
4359 tabular.setLongTabular(true);
4362 case Tabular::UNSET_LONGTABULAR:
4363 tabular.setLongTabular(false);
4366 case Tabular::SET_ROTATE_TABULAR:
4367 tabular.setRotateTabular(true);
4370 case Tabular::UNSET_ROTATE_TABULAR:
4371 tabular.setRotateTabular(false);
4374 case Tabular::TOGGLE_ROTATE_TABULAR:
4375 tabular.setRotateTabular(!tabular.getRotateTabular());
4378 case Tabular::SET_ROTATE_CELL:
4379 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4380 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4381 tabular.setRotateCell(
4382 tabular.cellIndex(i, j), true);
4385 case Tabular::UNSET_ROTATE_CELL:
4386 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4387 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4388 tabular.setRotateCell(
4389 tabular.cellIndex(i, j), false);
4392 case Tabular::TOGGLE_ROTATE_CELL:
4394 bool oneNotRotated = oneCellHasRotationState(false,
4395 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4397 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4398 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4399 tabular.setRotateCell(tabular.cellIndex(i, j),
4404 case Tabular::SET_USEBOX: {
4405 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4406 if (val == tabular.getUsebox(cur.idx()))
4407 val = Tabular::BOX_NONE;
4408 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4409 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4410 tabular.setUsebox(tabular.cellIndex(i, j), val);
4414 case Tabular::UNSET_LTFIRSTHEAD:
4416 case Tabular::SET_LTFIRSTHEAD:
4417 tabular.getRowOfLTFirstHead(row, ltt);
4418 checkLongtableSpecial(ltt, value, flag);
4419 tabular.setLTHead(row, flag, ltt, true);
4422 case Tabular::UNSET_LTHEAD:
4424 case Tabular::SET_LTHEAD:
4425 tabular.getRowOfLTHead(row, ltt);
4426 checkLongtableSpecial(ltt, value, flag);
4427 tabular.setLTHead(row, flag, ltt, false);
4430 case Tabular::UNSET_LTFOOT:
4432 case Tabular::SET_LTFOOT:
4433 tabular.getRowOfLTFoot(row, ltt);
4434 checkLongtableSpecial(ltt, value, flag);
4435 tabular.setLTFoot(row, flag, ltt, false);
4438 case Tabular::UNSET_LTLASTFOOT:
4440 case Tabular::SET_LTLASTFOOT:
4441 tabular.getRowOfLTLastFoot(row, ltt);
4442 checkLongtableSpecial(ltt, value, flag);
4443 tabular.setLTFoot(row, flag, ltt, true);
4446 case Tabular::SET_LTNEWPAGE:
4447 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4450 case Tabular::SET_BOOKTABS:
4451 tabular.setBookTabs(true);
4454 case Tabular::UNSET_BOOKTABS:
4455 tabular.setBookTabs(false);
4458 case Tabular::SET_TOP_SPACE: {
4460 if (value == "default")
4461 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4462 tabular.row_info[i].top_space_default = true;
4463 else if (isValidLength(value, &len))
4464 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4465 tabular.row_info[i].top_space_default = false;
4466 tabular.row_info[i].top_space = len;
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4470 tabular.row_info[i].top_space_default = false;
4471 tabular.row_info[i].top_space = len;
4476 case Tabular::SET_BOTTOM_SPACE: {
4478 if (value == "default")
4479 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4480 tabular.row_info[i].bottom_space_default = true;
4481 else if (isValidLength(value, &len))
4482 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4483 tabular.row_info[i].bottom_space_default = false;
4484 tabular.row_info[i].bottom_space = len;
4487 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4488 tabular.row_info[i].bottom_space_default = false;
4489 tabular.row_info[i].bottom_space = len;
4494 case Tabular::SET_INTERLINE_SPACE: {
4496 if (value == "default")
4497 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4498 tabular.row_info[i].interline_space_default = true;
4499 else if (isValidLength(value, &len))
4500 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4501 tabular.row_info[i].interline_space_default = false;
4502 tabular.row_info[i].interline_space = len;
4505 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4506 tabular.row_info[i].interline_space_default = false;
4507 tabular.row_info[i].interline_space = len;
4512 // dummy stuff just to avoid warnings
4513 case Tabular::LAST_ACTION:
4519 bool InsetTabular::showInsetDialog(BufferView * bv) const
4521 InsetTabularMailer(*this).showDialog(bv);
4526 void InsetTabular::openLayoutDialog(BufferView * bv) const
4528 InsetTabularMailer(*this).showDialog(bv);
4532 bool InsetTabular::copySelection(Cursor & cur)
4534 if (!cur.selection())
4539 getSelection(cur, rs, re, cs, ce);
4541 paste_tabular.reset(new Tabular(tabular));
4543 for (row_type i = 0; i < rs; ++i)
4544 paste_tabular->deleteRow(0);
4546 row_type const rows = re - rs + 1;
4547 while (paste_tabular->rowCount() > rows)
4548 paste_tabular->deleteRow(rows);
4550 paste_tabular->setTopLine(0, true, true);
4551 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4554 for (col_type i = 0; i < cs; ++i)
4555 paste_tabular->deleteColumn(0);
4557 col_type const columns = ce - cs + 1;
4558 while (paste_tabular->columnCount() > columns)
4559 paste_tabular->deleteColumn(columns);
4561 paste_tabular->setLeftLine(0, true, true);
4562 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4565 odocstringstream os;
4566 OutputParams const runparams(0);
4567 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4568 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4569 cap::copySelection(cur, os.str());
4571 // mark tabular stack dirty
4572 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4573 // when we (hopefully) have a one-for-all paste mechanism.
4574 // This must be called after cap::copySelection.
4575 dirtyTabularStack(true);
4581 bool InsetTabular::pasteClipboard(Cursor & cur)
4585 col_type const actcol = tabular.cellColumn(cur.idx());
4586 row_type const actrow = tabular.cellRow(cur.idx());
4587 for (row_type r1 = 0, r2 = actrow;
4588 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4590 for (col_type c1 = 0, c2 = actcol;
4591 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4593 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4594 tabular.isPartOfMultiColumn(r2, c2))
4596 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4600 if (tabular.isPartOfMultiColumn(r2, c2)) {
4604 shared_ptr<InsetText> inset(
4605 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4606 tabular.setCellInset(r2, c2, inset);
4607 // FIXME: change tracking (MG)
4608 inset->setChange(Change(cur.buffer().params().trackChanges ?
4609 Change::INSERTED : Change::UNCHANGED));
4617 void InsetTabular::cutSelection(Cursor & cur)
4619 if (!cur.selection())
4624 getSelection(cur, rs, re, cs, ce);
4625 for (row_type i = rs; i <= re; ++i) {
4626 for (col_type j = cs; j <= ce; ++j) {
4627 shared_ptr<InsetText> t
4628 = cell(tabular.cellIndex(i, j));
4629 if (cur.buffer().params().trackChanges)
4630 // FIXME: Change tracking (MG)
4631 t->setChange(Change(Change::DELETED));
4637 // cursor position might be invalid now
4638 if (cur.pit() > cur.lastpit())
4639 cur.pit() = cur.lastpit();
4640 if (cur.pos() > cur.lastpos())
4641 cur.pos() = cur.lastpos();
4642 cur.clearSelection();
4646 bool InsetTabular::isRightToLeft(Cursor & cur) const
4648 BOOST_ASSERT(cur.depth() > 1);
4649 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4650 pos_type const parentpos = cur[cur.depth() - 2].pos();
4651 return parentpar.getFontSettings(cur.bv().buffer().params(),
4652 parentpos).language()->rightToLeft();
4656 void InsetTabular::getSelection(Cursor & cur,
4657 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4659 CursorSlice const & beg = cur.selBegin();
4660 CursorSlice const & end = cur.selEnd();
4661 cs = tabular.cellColumn(beg.idx());
4662 ce = tabular.cellColumn(end.idx());
4665 cs = tabular.cellColumn(end.idx());
4667 ce = tabular.cellRightColumn(end.idx());
4670 rs = tabular.cellRow(beg.idx());
4671 re = tabular.cellRow(end.idx());
4677 Text * InsetTabular::getText(int idx) const
4679 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4683 void InsetTabular::setChange(Change const & change)
4685 for (idx_type idx = 0; idx < nargs(); ++idx)
4686 cell(idx)->setChange(change);
4690 void InsetTabular::acceptChanges(BufferParams const & bparams)
4692 for (idx_type idx = 0; idx < nargs(); ++idx)
4693 cell(idx)->acceptChanges(bparams);
4697 void InsetTabular::rejectChanges(BufferParams const & bparams)
4699 for (idx_type idx = 0; idx < nargs(); ++idx)
4700 cell(idx)->rejectChanges(bparams);
4704 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4706 return tabular.getPWidth(cell).zero();
4710 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4713 if (buf.length() <= 0)
4716 Buffer const & buffer = bv.buffer();
4720 col_type maxCols = 1;
4721 docstring::size_type const len = buf.length();
4722 docstring::size_type p = 0;
4725 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4733 maxCols = max(cols, maxCols);
4739 maxCols = max(cols, maxCols);
4745 paste_tabular.reset(
4746 new Tabular(buffer.params(), rows, maxCols));
4747 loctab = paste_tabular.get();
4749 dirtyTabularStack(true);
4752 cell = bv.cursor().idx();
4753 ocol = tabular.cellColumn(cell);
4754 row = tabular.cellRow(cell);
4757 docstring::size_type op = 0;
4758 idx_type const cells = loctab->cellCount();
4761 rows = loctab->rowCount();
4762 col_type const columns = loctab->columnCount();
4764 while (cell < cells && p < len && row < rows &&
4765 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4771 // we can only set this if we are not too far right
4772 if (cols < columns) {
4773 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4774 Font const font = bv.textMetrics(&inset->text_).
4775 getDisplayFont(0, 0);
4776 inset->setText(buf.substr(op, p - op), font,
4777 buffer.params().trackChanges);
4783 // we can only set this if we are not too far right
4784 if (cols < columns) {
4785 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4786 Font const font = bv.textMetrics(&inset->text_).
4787 getDisplayFont(0, 0);
4788 inset->setText(buf.substr(op, p - op), font,
4789 buffer.params().trackChanges);
4794 cell = loctab->cellIndex(row, cols);
4800 // check for the last cell if there is no trailing '\n'
4801 if (cell < cells && op < len) {
4802 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4803 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4804 inset->setText(buf.substr(op, len - op), font,
4805 buffer.params().trackChanges);
4811 void InsetTabular::addPreview(PreviewLoader & loader) const
4813 row_type const rows = tabular.rowCount();
4814 col_type const columns = tabular.columnCount();
4815 for (row_type i = 0; i < rows; ++i) {
4816 for (col_type j = 0; j < columns; ++j)
4817 tabular.getCellInset(i, j)->addPreview(loader);
4822 bool InsetTabular::tablemode(Cursor & cur) const
4824 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4830 string const InsetTabularMailer::name_("tabular");
4832 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4833 : inset_(const_cast<InsetTabular &>(inset))
4837 string const InsetTabularMailer::inset2string(Buffer const &) const
4839 return params2string(inset_);
4843 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4845 istringstream data(in);
4847 lex.setStream(data);
4854 if (!lex || token != name_)
4855 return print_mailer_error("InsetTabularMailer", in, 1,
4858 // This is part of the inset proper that is usually swallowed
4859 // by Buffer::readInset
4861 if (!lex || token != "Tabular")
4862 return print_mailer_error("InsetTabularMailer", in, 2,
4865 Buffer const & buffer = inset.buffer();
4866 inset.read(buffer, lex);
4870 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4873 data << name_ << ' ';
4874 inset.write(inset.buffer(), data);
4875 data << "\\end_inset\n";