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"
24 #include "BufferParams.h"
25 #include "BufferView.h"
27 #include "CutAndPaste.h"
28 #include "CoordCache.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
35 #include "LaTeXFeatures.h"
39 #include "MetricsInfo.h"
40 #include "OutputParams.h"
41 #include "Paragraph.h"
42 #include "paragraph_funcs.h"
43 #include "ParagraphParameters.h"
46 #include "support/convert.h"
47 #include "support/lstrings.h"
49 #include "frontends/alert.h"
50 #include "frontends/Clipboard.h"
51 #include "frontends/Painter.h"
52 #include "frontends/Selection.h"
63 using std::istringstream;
66 using std::ostringstream;
71 #ifndef CXX_GLOBAL_CSTD
75 using boost::shared_ptr;
76 using boost::dynamic_pointer_cast;
80 using support::prefixIs;
83 using support::suffixIs;
85 using cap::dirtyTabularStack;
86 using cap::tabularStackDirty;
88 using graphics::PreviewLoader;
90 using frontend::Painter;
91 using frontend::Clipboard;
93 namespace Alert = frontend::Alert;
98 int const ADD_TO_HEIGHT = 2;
99 int const ADD_TO_TABULAR_WIDTH = 2;
100 int const default_line_space = 10;
101 int const WIDTH_OF_LINE = 5;
105 boost::scoped_ptr<Tabular> paste_tabular;
108 struct TabularFeature {
109 Tabular::Feature action;
114 TabularFeature tabularFeature[] =
116 { Tabular::APPEND_ROW, "append-row" },
117 { Tabular::APPEND_COLUMN, "append-column" },
118 { Tabular::DELETE_ROW, "delete-row" },
119 { Tabular::DELETE_COLUMN, "delete-column" },
120 { Tabular::COPY_ROW, "copy-row" },
121 { Tabular::COPY_COLUMN, "copy-column" },
122 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
123 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
124 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
125 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
126 { Tabular::ALIGN_LEFT, "align-left" },
127 { Tabular::ALIGN_RIGHT, "align-right" },
128 { Tabular::ALIGN_CENTER, "align-center" },
129 { Tabular::ALIGN_BLOCK, "align-block" },
130 { Tabular::VALIGN_TOP, "valign-top" },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
132 { Tabular::VALIGN_MIDDLE, "valign-middle" },
133 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
134 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
135 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
136 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
137 { Tabular::M_ALIGN_LEFT, "m-align-left" },
138 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
139 { Tabular::M_ALIGN_CENTER, "m-align-center" },
140 { Tabular::M_VALIGN_TOP, "m-valign-top" },
141 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
142 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
143 { Tabular::MULTICOLUMN, "multicolumn" },
144 { Tabular::SET_ALL_LINES, "set-all-lines" },
145 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
146 { Tabular::SET_LONGTABULAR, "set-longtabular" },
147 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
148 { Tabular::SET_PWIDTH, "set-pwidth" },
149 { Tabular::SET_MPWIDTH, "set-mpwidth" },
150 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
151 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
152 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
153 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
154 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
155 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
156 { Tabular::SET_USEBOX, "set-usebox" },
157 { Tabular::SET_LTHEAD, "set-lthead" },
158 { Tabular::UNSET_LTHEAD, "unset-lthead" },
159 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
160 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
161 { Tabular::SET_LTFOOT, "set-ltfoot" },
162 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
163 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
164 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
165 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
166 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
167 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
168 { Tabular::SET_BOOKTABS, "set-booktabs" },
169 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
170 { Tabular::SET_TOP_SPACE, "set-top-space" },
171 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
172 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
173 { Tabular::LAST_ACTION, "" }
177 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
179 FeatureEqual(Tabular::Feature feature)
180 : feature_(feature) {}
181 bool operator()(TabularFeature const & tf) const {
182 return tf.action == feature_;
185 Tabular::Feature feature_;
190 string const write_attribute(string const & name, T const & t)
192 string const s = tostr(t);
193 return s.empty() ? s : " " + name + "=\"" + s + "\"";
197 string const write_attribute(string const & name, string const & t)
199 return t.empty() ? t : " " + name + "=\"" + t + "\"";
204 string const write_attribute(string const & name, docstring const & t)
206 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
211 string const write_attribute(string const & name, bool const & b)
213 // we write only true attribute values so we remove a bit of the
214 // file format bloat for tabulars.
215 return b ? write_attribute(name, convert<string>(b)) : string();
220 string const write_attribute(string const & name, int const & i)
222 // we write only true attribute values so we remove a bit of the
223 // file format bloat for tabulars.
224 return i ? write_attribute(name, convert<string>(i)) : string();
229 string const write_attribute(string const & name, Tabular::idx_type const & i)
231 // we write only true attribute values so we remove a bit of the
232 // file format bloat for tabulars.
233 return i ? write_attribute(name, convert<string>(i)) : string();
238 string const write_attribute(string const & name, Length const & value)
240 // we write only the value if we really have one same reson as above.
241 return value.zero() ? string() : write_attribute(name, value.asString());
245 string const tostr(LyXAlignment const & num)
250 case LYX_ALIGN_BLOCK:
254 case LYX_ALIGN_CENTER:
256 case LYX_ALIGN_RIGHT:
258 case LYX_ALIGN_LAYOUT:
260 case LYX_ALIGN_SPECIAL:
267 string const tostr(Tabular::VAlignment const & num)
270 case Tabular::LYX_VALIGN_TOP:
272 case Tabular::LYX_VALIGN_MIDDLE:
274 case Tabular::LYX_VALIGN_BOTTOM:
281 string const tostr(Tabular::BoxType const & num)
284 case Tabular::BOX_NONE:
286 case Tabular::BOX_PARBOX:
288 case Tabular::BOX_MINIPAGE:
295 // I would have liked a fromstr template a lot better. (Lgb)
296 bool string2type(string const str, LyXAlignment & num)
299 num = LYX_ALIGN_NONE;
300 else if (str == "block")
301 num = LYX_ALIGN_BLOCK;
302 else if (str == "left")
303 num = LYX_ALIGN_LEFT;
304 else if (str == "center")
305 num = LYX_ALIGN_CENTER;
306 else if (str == "right")
307 num = LYX_ALIGN_RIGHT;
314 bool string2type(string const str, Tabular::VAlignment & num)
317 num = Tabular::LYX_VALIGN_TOP;
318 else if (str == "middle" )
319 num = Tabular::LYX_VALIGN_MIDDLE;
320 else if (str == "bottom")
321 num = Tabular::LYX_VALIGN_BOTTOM;
328 bool string2type(string const str, Tabular::BoxType & num)
331 num = Tabular::BOX_NONE;
332 else if (str == "parbox")
333 num = Tabular::BOX_PARBOX;
334 else if (str == "minipage")
335 num = Tabular::BOX_MINIPAGE;
342 bool string2type(string const str, bool & num)
346 else if (str == "false")
354 bool getTokenValue(string const & str, char const * token, string & ret)
357 size_t token_length = strlen(token);
358 string::size_type pos = str.find(token);
360 if (pos == string::npos || pos + token_length + 1 >= str.length()
361 || str[pos + token_length] != '=')
363 pos += token_length + 1;
365 if (ch != '"' && ch != '\'') { // only read till next space
369 while (pos < str.length() - 1 && str[++pos] != ch)
376 bool getTokenValue(string const & str, char const * token, docstring & ret)
379 bool const success = getTokenValue(str, token, tmp);
380 ret = from_utf8(tmp);
385 bool getTokenValue(string const & str, char const * token, int & num)
389 if (!getTokenValue(str, token, tmp))
391 num = convert<int>(tmp);
396 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
399 return getTokenValue(str, token, tmp) && string2type(tmp, num);
403 bool getTokenValue(string const & str, char const * token,
404 Tabular::VAlignment & num)
407 return getTokenValue(str, token, tmp) && string2type(tmp, num);
411 bool getTokenValue(string const & str, char const * token,
412 Tabular::BoxType & num)
415 return getTokenValue(str, token, tmp) && string2type(tmp, num);
419 bool getTokenValue(string const & str, char const * token, bool & flag)
421 // set the flag always to false as this should be the default for bools
422 // not in the file-format.
425 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
429 bool getTokenValue(string const & str, char const * token, Length & len)
431 // set the lenght to be zero() as default as this it should be if not
432 // in the file format.
435 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
439 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
444 if (!getTokenValue(str, token, tmp))
446 if (tmp == "default") {
450 return isValidLength(tmp, &len);
454 void l_getline(istream & is, string & str)
457 while (str.empty()) {
459 if (!str.empty() && str[str.length() - 1] == '\r')
460 str.erase(str.length() - 1);
467 string const featureAsString(Tabular::Feature feature)
469 TabularFeature * end = tabularFeature +
470 sizeof(tabularFeature) / sizeof(TabularFeature);
471 TabularFeature * it = std::find_if(tabularFeature, end,
472 FeatureEqual(feature));
473 return (it == end) ? string() : it->feature;
478 /////////////////////////////////////////////////////////////////////
482 /////////////////////////////////////////////////////////////////////
485 Tabular::cellstruct::cellstruct(BufferParams const & bp)
488 multicolumn(Tabular::CELL_NORMAL),
489 alignment(LYX_ALIGN_CENTER),
490 valignment(LYX_VALIGN_TOP),
497 inset(new InsetText(bp))
501 Tabular::cellstruct::cellstruct(cellstruct const & cs)
503 width_of_cell(cs.width_of_cell),
504 multicolumn(cs.multicolumn),
505 alignment(cs.alignment),
506 valignment(cs.valignment),
507 top_line(cs.top_line),
508 bottom_line(cs.bottom_line),
509 left_line(cs.left_line),
510 right_line(cs.right_line),
513 align_special(cs.align_special),
515 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
519 Tabular::cellstruct &
520 Tabular::cellstruct::operator=(cellstruct cs)
528 Tabular::cellstruct::swap(cellstruct & rhs)
530 std::swap(cellno, rhs.cellno);
531 std::swap(width_of_cell, rhs.width_of_cell);
532 std::swap(multicolumn, rhs.multicolumn);
533 std::swap(alignment, rhs.alignment);
534 std::swap(valignment, rhs.valignment);
535 std::swap(top_line, rhs.top_line);
536 std::swap(bottom_line, rhs.bottom_line);
537 std::swap(left_line, rhs.left_line);
538 std::swap(right_line, rhs.right_line);
539 std::swap(usebox, rhs.usebox);
540 std::swap(rotate, rhs.rotate);
541 std::swap(align_special, rhs.align_special);
542 p_width.swap(rhs.p_width);
543 inset.swap(rhs.inset);
547 Tabular::rowstruct::rowstruct()
552 top_space_default(false),
553 bottom_space_default(false),
554 interline_space_default(false),
563 Tabular::columnstruct::columnstruct()
564 : alignment(LYX_ALIGN_CENTER),
565 valignment(LYX_VALIGN_TOP),
573 Tabular::ltType::ltType()
580 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
581 col_type columns_arg)
583 init(bp, rows_arg, columns_arg);
587 // activates all lines and sets all widths to 0
588 void Tabular::init(BufferParams const & bp, row_type rows_arg,
589 col_type columns_arg)
592 columns_ = columns_arg;
593 row_info = row_vector(rows_);
594 column_info = column_vector(columns_);
595 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
596 row_info.reserve(10);
597 column_info.reserve(10);
598 cell_info.reserve(100);
600 for (row_type i = 0; i < rows_; ++i)
601 cell_info[i].back().right_line = true;
602 row_info.back().bottom_line = true;
603 row_info.front().bottom_line = true;
604 column_info.back().right_line = true;
605 is_long_tabular = false;
607 use_booktabs = false;
611 void Tabular::fixCellNums()
614 for (row_type i = 0; i < rows_; ++i) {
615 for (col_type j = 0; j < columns_; ++j) {
616 // When debugging it can be nice to set
618 cell_info[i][j].inset->setDrawFrame(false);
619 cell_info[i][j].cellno = cellno++;
621 cell_info[i].back().right_line = true;
624 set_row_column_number_info();
628 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
632 row_type const row = row_of_cell(cell);
634 row_vector::iterator rit = row_info.begin() + row;
635 row_info.insert(rit, rowstruct());
636 // now set the values of the row before
637 row_info[row] = row_info[row + 1];
639 cell_vvector old(rows_ - 1);
640 for (row_type i = 0; i < rows_ - 1; ++i)
641 swap(cell_info[i], old[i]);
643 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
645 for (row_type i = 0; i <= row; ++i)
646 swap(cell_info[i], old[i]);
647 for (row_type i = row + 2; i < rows_; ++i)
648 swap(cell_info[i], old[i - 1]);
651 for (col_type j = 0; j < columns_; ++j)
652 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
654 set_row_column_number_info();
658 void Tabular::deleteRow(row_type const row)
660 // Not allowed to delete last row
664 row_info.erase(row_info.begin() + row);
665 cell_info.erase(cell_info.begin() + row);
671 void Tabular::copyRow(BufferParams const & bp, row_type const row)
675 row_info.insert(row_info.begin() + row, row_info[row]);
676 cell_info.insert(cell_info.begin() + row, cell_info[row]);
679 for (col_type j = 0; j < columns_; ++j)
680 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
682 set_row_column_number_info();
686 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
690 col_type const column = column_of_cell(cell);
691 column_vector::iterator cit = column_info.begin() + column + 1;
692 column_info.insert(cit, columnstruct());
693 // set the column values of the column before
694 column_info[column + 1] = column_info[column];
696 for (row_type i = 0; i < rows_; ++i) {
697 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
699 // care about multicolumns
700 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
701 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
703 if (column + 2 >= columns_
704 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
705 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
708 for (row_type i = 0; i < rows_; ++i) {
709 cell_info[i][column + 1].inset->clear();
711 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
717 void Tabular::deleteColumn(col_type const column)
719 // Not allowed to delete last column
723 column_info.erase(column_info.begin() + column);
724 for (row_type i = 0; i < rows_; ++i)
725 cell_info[i].erase(cell_info[i].begin() + column);
731 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
735 column_info.insert(column_info.begin() + column, column_info[column]);
737 for (row_type i = 0; i < rows_; ++i)
738 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
741 for (row_type i = 0; i < rows_; ++i)
742 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
747 void Tabular::set_row_column_number_info()
750 for (row_type row = 0; row < rows_; ++row) {
751 for (col_type column = 0; column < columns_; ++column) {
753 // If on its left is either the edge, or a normal cell,
754 // then this cannot be a non-begin multicol cell.
755 // Clean up as well as we can:
756 if (cell_info[row][column].multicolumn
757 == CELL_PART_OF_MULTICOLUMN) {
759 cell_info[row][column - 1]
760 .multicolumn == CELL_NORMAL)
761 cell_info[row][column].multicolumn =
764 // Count only non-multicol cells plus begin multicol
765 // cells, ignore non-begin multicol cells:
766 if (cell_info[row][column].multicolumn
767 != Tabular::CELL_PART_OF_MULTICOLUMN)
772 rowofcell.resize(numberofcells);
773 columnofcell.resize(numberofcells);
778 c < numberofcells && row < rows_ && column < columns_;) {
780 columnofcell[c] = column;
784 } while (column < columns_ &&
785 cell_info[row][column].multicolumn
786 == Tabular::CELL_PART_OF_MULTICOLUMN);
788 if (column == columns_) {
794 for (row_type row = 0; row < rows_; ++row) {
795 for (col_type column = 0; column < columns_; ++column) {
796 if (isPartOfMultiColumn(row,column))
798 cell_info[row][column].inset->setAutoBreakRows(
799 !getPWidth(getCellNumber(row, column)).zero());
805 Tabular::idx_type Tabular::getNumberOfCells() const
807 return numberofcells;
811 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
813 row_type const row = row_of_cell(cell);
815 for (col_type i = 0; i < columns_; ++i)
816 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
822 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
824 if (!wholerow && isMultiColumn(cell) &&
825 !(use_booktabs && row_of_cell(cell) == 0))
826 return cellinfo_of_cell(cell).top_line;
827 return row_info[row_of_cell(cell)].top_line;
831 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
833 if (!wholerow && isMultiColumn(cell) &&
834 !(use_booktabs && isLastRow(cell)))
835 return cellinfo_of_cell(cell).bottom_line;
836 return row_info[row_of_cell(cell)].bottom_line;
840 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
844 if (!wholecolumn && isMultiColumn(cell) &&
845 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
846 return cellinfo_of_cell(cell).left_line;
847 return column_info[column_of_cell(cell)].left_line;
851 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
855 if (!wholecolumn && isMultiColumn(cell) &&
856 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
857 return cellinfo_of_cell(cell).right_line;
858 return column_info[right_column_of_cell(cell)].right_line;
862 bool Tabular::topAlreadyDrawn(idx_type cell) const
864 row_type row = row_of_cell(cell);
865 if (row > 0 && !getAdditionalHeight(row)) {
866 col_type column = column_of_cell(cell);
869 && cell_info[row][column].multicolumn
870 == Tabular::CELL_PART_OF_MULTICOLUMN)
872 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
873 return row_info[row].bottom_line;
875 return cell_info[row][column].bottom_line;
881 bool Tabular::leftAlreadyDrawn(idx_type cell) const
883 col_type column = column_of_cell(cell);
885 row_type row = row_of_cell(cell);
887 (cell_info[row][column].multicolumn ==
888 Tabular::CELL_PART_OF_MULTICOLUMN));
889 if (getAdditionalWidth(cell_info[row][column].cellno))
891 return rightLine(cell_info[row][column].cellno);
897 bool Tabular::isLastRow(idx_type cell) const
899 return row_of_cell(cell) == rows_ - 1;
903 int Tabular::getAdditionalHeight(row_type row) const
905 if (!row || row >= rows_)
911 for (col_type column = 0; column < columns_ && bottom; ++column) {
912 switch (cell_info[row - 1][column].multicolumn) {
913 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
914 bottom = cell_info[row - 1][column].bottom_line;
916 case Tabular::CELL_NORMAL:
917 bottom = row_info[row - 1].bottom_line;
920 for (col_type column = 0; column < columns_ && top; ++column) {
921 switch (cell_info[row][column].multicolumn) {
922 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
923 top = cell_info[row][column].top_line;
925 case Tabular::CELL_NORMAL:
926 top = row_info[row].top_line;
929 int const interline_space = row_info[row - 1].interline_space_default ?
931 row_info[row - 1].interline_space.inPixels(width_of_tabular);
933 return interline_space + WIDTH_OF_LINE;
934 return interline_space;
938 int Tabular::getAdditionalWidth(idx_type cell) const
940 // internally already set in setWidthOfCell
941 // used to get it back in text.cpp
942 col_type const col = right_column_of_cell(cell);
943 row_type const row = row_of_cell(cell);
944 if (col < columns_ - 1 && rightLine(cell) &&
945 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
947 return WIDTH_OF_LINE;
953 // returns the maximum over all rows
954 int Tabular::getWidthOfColumn(idx_type cell) const
956 col_type const column1 = column_of_cell(cell);
957 col_type const column2 = right_column_of_cell(cell);
959 for (col_type i = column1; i <= column2; ++i)
960 result += column_info[i].width_of_column;
965 int Tabular::getWidthOfTabular() const
967 return width_of_tabular;
971 // returns true if a complete update is necessary, otherwise false
972 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
974 if (!isMultiColumn(cell))
977 row_type const row = row_of_cell(cell);
978 col_type const column1 = column_of_cell(cell);
979 col_type const column2 = right_column_of_cell(cell);
980 int const old_val = cell_info[row][column2].width_of_cell;
982 // first set columns to 0 so we can calculate the right width
983 for (col_type i = column1; i <= column2; ++i) {
984 cell_info[row][i].width_of_cell = 0;
986 // set the width to MAX_WIDTH until width > 0
987 int width = new_width + 2 * WIDTH_OF_LINE;
988 col_type i = column1;
989 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
990 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
991 width -= column_info[i].width_of_column;
994 cell_info[row][i].width_of_cell = width;
996 if (old_val != cell_info[row][column2].width_of_cell) {
997 // in this case we have to recalculate all multicolumn cells which
998 // have this column as one of theirs but not as last one
999 calculate_width_of_column_NMC(i);
1000 recalculateMulticolumnsOfColumn(i);
1001 calculate_width_of_column(i);
1007 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1009 // the last column does not have to be recalculated because all
1010 // multicolumns will have here there last multicolumn cell which
1011 // always will have the whole rest of the width of the cell.
1012 if (columns_ < 2 || column > (columns_ - 2))
1014 for (row_type row = 0; row < rows_; ++row) {
1015 int mc = cell_info[row][column].multicolumn;
1016 int nmc = cell_info[row][column+1].multicolumn;
1017 // we only have to update multicolumns which do not have this
1018 // column as their last column!
1019 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1020 (mc == CELL_PART_OF_MULTICOLUMN &&
1021 nmc == CELL_PART_OF_MULTICOLUMN))
1023 idx_type const cellno = cell_info[row][column].cellno;
1024 setWidthOfMulticolCell(cellno,
1025 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1031 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1033 row_type const row = row_of_cell(cell);
1034 col_type const column1 = column_of_cell(cell);
1039 if (rightLine(cell_info[row][column1].cellno, true) &&
1040 column1 < columns_ - 1 &&
1041 leftLine(cell_info[row][column1+1].cellno, true))
1043 add_width = WIDTH_OF_LINE;
1046 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1049 if (isMultiColumnReal(cell)) {
1050 tmp = setWidthOfMulticolCell(cell, new_width);
1052 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1053 cell_info[row][column1].width_of_cell = width;
1054 tmp = calculate_width_of_column_NMC(column1);
1056 recalculateMulticolumnsOfColumn(column1);
1059 for (col_type i = 0; i < columns_; ++i)
1060 calculate_width_of_column(i);
1061 calculate_width_of_tabular();
1066 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1069 if (!isMultiColumn(cell) || onlycolumn)
1070 column_info[column_of_cell(cell)].alignment = align;
1072 cellinfo_of_cell(cell).alignment = align;
1076 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1079 if (!isMultiColumn(cell) || onlycolumn)
1080 column_info[column_of_cell(cell)].valignment = align;
1082 cellinfo_of_cell(cell).valignment = align;
1089 * Allow line and paragraph breaks for fixed width cells or disallow them,
1090 * merge cell paragraphs and reset layout to standard for variable width
1093 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1095 inset->setAutoBreakRows(fixedWidth);
1099 // merge all paragraphs to one
1100 BufferParams const & bp = cur.bv().buffer()->params();
1101 while (inset->paragraphs().size() > 1)
1102 mergeParagraph(bp, inset->paragraphs(), 0);
1106 // undo information has already been recorded
1107 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1108 bp.getTextClass().defaultLayoutName());
1115 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1116 Length const & width)
1118 col_type const j = column_of_cell(cell);
1120 column_info[j].p_width = width;
1121 for (row_type i = 0; i < rows_; ++i) {
1122 idx_type const cell = getCellNumber(i, j);
1123 // because of multicolumns
1124 toggleFixedWidth(cur, getCellInset(cell).get(),
1125 !getPWidth(cell).zero());
1127 // cur paragraph can become invalid after paragraphs were merged
1128 if (cur.pit() > cur.lastpit())
1129 cur.pit() = cur.lastpit();
1130 // cur position can become invalid after newlines were removed
1131 if (cur.pos() > cur.lastpos())
1132 cur.pos() = cur.lastpos();
1136 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1137 Length const & width)
1139 if (!isMultiColumn(cell))
1142 cellinfo_of_cell(cell).p_width = width;
1143 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1144 // cur paragraph can become invalid after paragraphs were merged
1145 if (cur.pit() > cur.lastpit())
1146 cur.pit() = cur.lastpit();
1147 // cur position can become invalid after newlines were removed
1148 if (cur.pos() > cur.lastpos())
1149 cur.pos() = cur.lastpos();
1154 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1155 Tabular::Feature what)
1157 if (what == SET_SPECIAL_MULTI)
1158 cellinfo_of_cell(cell).align_special = special;
1160 column_info[column_of_cell(cell)].align_special = special;
1164 void Tabular::setAllLines(idx_type cell, bool line)
1166 setTopLine(cell, line);
1167 setBottomLine(cell, line);
1168 setRightLine(cell, line);
1169 setLeftLine(cell, line);
1173 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1175 row_type const row = row_of_cell(cell);
1176 if (wholerow || !isMultiColumn(cell))
1177 row_info[row].top_line = line;
1179 cellinfo_of_cell(cell).top_line = line;
1183 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1185 if (wholerow || !isMultiColumn(cell))
1186 row_info[row_of_cell(cell)].bottom_line = line;
1188 cellinfo_of_cell(cell).bottom_line = line;
1192 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1194 if (wholecolumn || !isMultiColumn(cell))
1195 column_info[column_of_cell(cell)].left_line = line;
1197 cellinfo_of_cell(cell).left_line = line;
1201 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1203 if (wholecolumn || !isMultiColumn(cell))
1204 column_info[right_column_of_cell(cell)].right_line = line;
1206 cellinfo_of_cell(cell).right_line = line;
1210 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1212 if (!onlycolumn && isMultiColumn(cell))
1213 return cellinfo_of_cell(cell).alignment;
1214 return column_info[column_of_cell(cell)].alignment;
1219 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1221 if (!onlycolumn && isMultiColumn(cell))
1222 return cellinfo_of_cell(cell).valignment;
1223 return column_info[column_of_cell(cell)].valignment;
1227 Length const Tabular::getPWidth(idx_type cell) const
1229 if (isMultiColumn(cell))
1230 return cellinfo_of_cell(cell).p_width;
1231 return column_info[column_of_cell(cell)].p_width;
1235 Length const Tabular::getColumnPWidth(idx_type cell) const
1237 return column_info[column_of_cell(cell)].p_width;
1241 Length const Tabular::getMColumnPWidth(idx_type cell) const
1243 if (isMultiColumn(cell))
1244 return cellinfo_of_cell(cell).p_width;
1249 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1251 if (what == SET_SPECIAL_MULTI)
1252 return cellinfo_of_cell(cell).align_special;
1253 return column_info[column_of_cell(cell)].align_special;
1257 int Tabular::getWidthOfCell(idx_type cell) const
1259 row_type const row = row_of_cell(cell);
1260 col_type const column1 = column_of_cell(cell);
1261 col_type const column2 = right_column_of_cell(cell);
1263 for (col_type i = column1; i <= column2; ++i)
1264 result += cell_info[row][i].width_of_cell;
1269 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1273 switch (getAlignment(cell)) {
1274 case LYX_ALIGN_CENTER:
1275 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1277 case LYX_ALIGN_RIGHT:
1278 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1279 // + getAdditionalWidth(cell);
1282 // LYX_ALIGN_LEFT: nothing :-)
1286 // the LaTeX Way :-(
1292 bool Tabular::isFirstCellInRow(idx_type cell) const
1294 return column_of_cell(cell) == 0;
1298 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1300 if (row > rows_ - 1)
1302 return cell_info[row][0].cellno;
1306 bool Tabular::isLastCellInRow(idx_type cell) const
1308 return right_column_of_cell(cell) == columns_ - 1;
1312 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1314 if (row > rows_ - 1)
1316 return cell_info[row][columns_-1].cellno;
1320 void Tabular::calculate_width_of_column(col_type column)
1323 for (row_type i = 0; i < rows_; ++i)
1324 maximum = max(cell_info[i][column].width_of_cell, maximum);
1325 column_info[column].width_of_column = maximum;
1330 // Calculate the columns regarding ONLY the normal cells and if this
1331 // column is inside a multicolumn cell then use it only if its the last
1332 // column of this multicolumn cell as this gives an added width to the
1333 // column, all the rest should be adapted!
1335 bool Tabular::calculate_width_of_column_NMC(col_type column)
1337 int const old_column_width = column_info[column].width_of_column;
1339 for (row_type i = 0; i < rows_; ++i) {
1340 idx_type cell = getCellNumber(i, column);
1341 bool ismulti = isMultiColumnReal(cell);
1342 if ((!ismulti || column == right_column_of_cell(cell)) &&
1343 cell_info[i][column].width_of_cell > max)
1345 max = cell_info[i][column].width_of_cell;
1348 column_info[column].width_of_column = max;
1349 return column_info[column].width_of_column != old_column_width;
1353 void Tabular::calculate_width_of_tabular()
1355 width_of_tabular = 0;
1356 for (col_type i = 0; i < columns_; ++i)
1357 width_of_tabular += column_info[i].width_of_column;
1361 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1363 if (cell >= numberofcells)
1367 return rowofcell[cell];
1371 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1373 if (cell >= numberofcells)
1374 return columns_ - 1;
1377 return columnofcell[cell];
1381 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1383 row_type const row = row_of_cell(cell);
1384 col_type column = column_of_cell(cell);
1385 while (column < columns_ - 1 &&
1386 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1392 void Tabular::write(Buffer const & buf, ostream & os) const
1396 << write_attribute("version", 3)
1397 << write_attribute("rows", rows_)
1398 << write_attribute("columns", columns_)
1400 // global longtable options
1402 << write_attribute("rotate", rotate)
1403 << write_attribute("booktabs", use_booktabs)
1404 << write_attribute("islongtable", is_long_tabular)
1405 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1406 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1407 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1408 << write_attribute("headTopDL", endhead.topDL)
1409 << write_attribute("headBottomDL", endhead.bottomDL)
1410 << write_attribute("footTopDL", endfoot.topDL)
1411 << write_attribute("footBottomDL", endfoot.bottomDL)
1412 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1413 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1414 << write_attribute("lastFootEmpty", endlastfoot.empty)
1416 for (col_type j = 0; j < columns_; ++j) {
1418 << write_attribute("alignment", column_info[j].alignment)
1419 << write_attribute("valignment", column_info[j].valignment)
1420 << write_attribute("leftline", column_info[j].left_line)
1421 << write_attribute("rightline", column_info[j].right_line)
1422 << write_attribute("width", column_info[j].p_width.asString())
1423 << write_attribute("special", column_info[j].align_special)
1426 for (row_type i = 0; i < rows_; ++i) {
1428 << write_attribute("topline", row_info[i].top_line)
1429 << write_attribute("bottomline", row_info[i].bottom_line);
1430 static const string def("default");
1431 if (row_info[i].top_space_default)
1432 os << write_attribute("topspace", def);
1434 os << write_attribute("topspace", row_info[i].top_space);
1435 if (row_info[i].bottom_space_default)
1436 os << write_attribute("bottomspace", def);
1438 os << write_attribute("bottomspace", row_info[i].bottom_space);
1439 if (row_info[i].interline_space_default)
1440 os << write_attribute("interlinespace", def);
1442 os << write_attribute("interlinespace", row_info[i].interline_space);
1443 os << write_attribute("endhead", row_info[i].endhead)
1444 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1445 << write_attribute("endfoot", row_info[i].endfoot)
1446 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1447 << write_attribute("newpage", row_info[i].newpage)
1449 for (col_type j = 0; j < columns_; ++j) {
1451 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1452 << write_attribute("alignment", cell_info[i][j].alignment)
1453 << write_attribute("valignment", cell_info[i][j].valignment)
1454 << write_attribute("topline", cell_info[i][j].top_line)
1455 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1456 << write_attribute("leftline", cell_info[i][j].left_line)
1457 << write_attribute("rightline", cell_info[i][j].right_line)
1458 << write_attribute("rotate", cell_info[i][j].rotate)
1459 << write_attribute("usebox", cell_info[i][j].usebox)
1460 << write_attribute("width", cell_info[i][j].p_width)
1461 << write_attribute("special", cell_info[i][j].align_special)
1463 os << "\\begin_inset ";
1464 cell_info[i][j].inset->write(buf, os);
1465 os << "\n\\end_inset\n"
1470 os << "</lyxtabular>\n";
1474 void Tabular::read(Buffer const & buf, Lexer & lex)
1477 istream & is = lex.getStream();
1479 l_getline(is, line);
1480 if (!prefixIs(line, "<lyxtabular ")
1481 && !prefixIs(line, "<Tabular ")) {
1482 BOOST_ASSERT(false);
1487 if (!getTokenValue(line, "version", version))
1489 BOOST_ASSERT(version >= 2);
1492 if (!getTokenValue(line, "rows", rows_arg))
1495 if (!getTokenValue(line, "columns", columns_arg))
1497 init(buf.params(), rows_arg, columns_arg);
1498 l_getline(is, line);
1499 if (!prefixIs(line, "<features")) {
1500 lyxerr << "Wrong tabular format (expected <features ...> got"
1501 << line << ')' << endl;
1504 getTokenValue(line, "rotate", rotate);
1505 getTokenValue(line, "booktabs", use_booktabs);
1506 getTokenValue(line, "islongtable", is_long_tabular);
1507 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1508 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1509 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1510 getTokenValue(line, "headTopDL", endhead.topDL);
1511 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1512 getTokenValue(line, "footTopDL", endfoot.topDL);
1513 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1514 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1515 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1516 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1518 for (col_type j = 0; j < columns_; ++j) {
1520 if (!prefixIs(line,"<column")) {
1521 lyxerr << "Wrong tabular format (expected <column ...> got"
1522 << line << ')' << endl;
1525 getTokenValue(line, "alignment", column_info[j].alignment);
1526 getTokenValue(line, "valignment", column_info[j].valignment);
1527 getTokenValue(line, "leftline", column_info[j].left_line);
1528 getTokenValue(line, "rightline", column_info[j].right_line);
1529 getTokenValue(line, "width", column_info[j].p_width);
1530 getTokenValue(line, "special", column_info[j].align_special);
1533 for (row_type i = 0; i < rows_; ++i) {
1534 l_getline(is, line);
1535 if (!prefixIs(line, "<row")) {
1536 lyxerr << "Wrong tabular format (expected <row ...> got"
1537 << line << ')' << endl;
1540 getTokenValue(line, "topline", row_info[i].top_line);
1541 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1542 getTokenValue(line, "topspace", row_info[i].top_space,
1543 row_info[i].top_space_default);
1544 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1545 row_info[i].bottom_space_default);
1546 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1547 row_info[i].interline_space_default);
1548 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1549 getTokenValue(line, "endhead", row_info[i].endhead);
1550 getTokenValue(line, "endfoot", row_info[i].endfoot);
1551 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1552 getTokenValue(line, "newpage", row_info[i].newpage);
1553 for (col_type j = 0; j < columns_; ++j) {
1554 l_getline(is, line);
1555 if (!prefixIs(line, "<cell")) {
1556 lyxerr << "Wrong tabular format (expected <cell ...> got"
1557 << line << ')' << endl;
1560 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1561 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1562 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1563 getTokenValue(line, "topline", cell_info[i][j].top_line);
1564 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1565 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1566 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1567 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1568 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1569 getTokenValue(line, "width", cell_info[i][j].p_width);
1570 getTokenValue(line, "special", cell_info[i][j].align_special);
1571 l_getline(is, line);
1572 if (prefixIs(line, "\\begin_inset")) {
1573 cell_info[i][j].inset->read(buf, lex);
1574 l_getline(is, line);
1576 if (!prefixIs(line, "</cell>")) {
1577 lyxerr << "Wrong tabular format (expected </cell> got"
1578 << line << ')' << endl;
1582 l_getline(is, line);
1583 if (!prefixIs(line, "</row>")) {
1584 lyxerr << "Wrong tabular format (expected </row> got"
1585 << line << ')' << endl;
1589 while (!prefixIs(line, "</lyxtabular>")) {
1590 l_getline(is, line);
1592 set_row_column_number_info();
1596 bool Tabular::isMultiColumn(idx_type cell) const
1598 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1602 bool Tabular::isMultiColumnReal(idx_type cell) const
1604 return column_of_cell(cell) != right_column_of_cell(cell) &&
1605 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1609 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1611 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1615 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1618 cellstruct & cs = cellinfo_of_cell(cell);
1619 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1620 cs.alignment = column_info[column_of_cell(cell)].alignment;
1621 cs.top_line = row_info[row_of_cell(cell)].top_line;
1622 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1623 cs.left_line = column_info[column_of_cell(cell)].left_line;
1624 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1625 for (idx_type i = 1; i < number; ++i) {
1626 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1627 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1628 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1631 set_row_column_number_info();
1635 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1637 row_type const row = row_of_cell(cell);
1638 col_type column = column_of_cell(cell);
1639 idx_type result = 1;
1641 while (column < columns_ &&
1642 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1651 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1653 row_type const row = row_of_cell(cell);
1654 col_type column = column_of_cell(cell);
1656 idx_type result = 0;
1658 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1659 cell_info[row][column].multicolumn = CELL_NORMAL;
1661 while (column < columns_ &&
1662 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1664 cell_info[row][column].multicolumn = CELL_NORMAL;
1669 set_row_column_number_info();
1674 void Tabular::setBookTabs(bool what)
1676 use_booktabs = what;
1680 bool Tabular::useBookTabs() const
1682 return use_booktabs;
1686 void Tabular::setLongTabular(bool what)
1688 is_long_tabular = what;
1692 bool Tabular::isLongTabular() const
1694 return is_long_tabular;
1698 void Tabular::setRotateTabular(bool flag)
1704 bool Tabular::getRotateTabular() const
1710 void Tabular::setRotateCell(idx_type cell, bool flag)
1712 cellinfo_of_cell(cell).rotate = flag;
1716 bool Tabular::getRotateCell(idx_type cell) const
1718 return cellinfo_of_cell(cell).rotate;
1722 bool Tabular::needRotating() const
1726 for (row_type i = 0; i < rows_; ++i)
1727 for (col_type j = 0; j < columns_; ++j)
1728 if (cell_info[i][j].rotate)
1734 bool Tabular::isLastCell(idx_type cell) const
1736 if (cell + 1 < numberofcells)
1742 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1744 if (row_of_cell(cell) > 0)
1745 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1750 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1752 if (row_of_cell(cell) + 1 < rows_)
1753 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1758 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1760 if (row_of_cell(cell) == 0)
1762 if (!isMultiColumn(cell))
1763 return getCellAbove(cell);
1764 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1768 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1770 if (row_of_cell(cell) + 1 >= rows_)
1772 if (!isMultiColumn(cell))
1773 return getCellBelow(cell);
1774 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1778 Tabular::idx_type Tabular::getCellNumber(row_type row,
1779 col_type column) const
1781 BOOST_ASSERT(column != npos && column < columns_ &&
1782 row != npos && row < rows_);
1783 return cell_info[row][column].cellno;
1787 void Tabular::setUsebox(idx_type cell, BoxType type)
1789 cellinfo_of_cell(cell).usebox = type;
1793 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1795 if (column_info[column_of_cell(cell)].p_width.zero() &&
1796 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1798 if (cellinfo_of_cell(cell).usebox > 1)
1799 return cellinfo_of_cell(cell).usebox;
1800 return useParbox(cell);
1805 // This are functions used for the longtable support
1807 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1813 row_info[row].endfirsthead = flag;
1817 row_info[row].endhead = flag;
1822 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1825 hd.set = haveLTHead();
1826 return row_info[row].endhead;
1830 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1833 hd.set = haveLTFirstHead();
1834 return row_info[row].endfirsthead;
1838 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1844 row_info[row].endlastfoot = flag;
1848 row_info[row].endfoot = flag;
1853 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1856 fd.set = haveLTFoot();
1857 return row_info[row].endfoot;
1861 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1864 fd.set = haveLTLastFoot();
1865 return row_info[row].endlastfoot;
1869 void Tabular::setLTNewPage(row_type row, bool what)
1871 row_info[row].newpage = what;
1875 bool Tabular::getLTNewPage(row_type row) const
1877 return row_info[row].newpage;
1881 bool Tabular::haveLTHead() const
1883 for (row_type i = 0; i < rows_; ++i)
1884 if (row_info[i].endhead)
1890 bool Tabular::haveLTFirstHead() const
1892 if (endfirsthead.empty)
1894 for (row_type i = 0; i < rows_; ++i)
1895 if (row_info[i].endfirsthead)
1901 bool Tabular::haveLTFoot() const
1903 for (row_type i = 0; i < rows_; ++i)
1904 if (row_info[i].endfoot)
1910 bool Tabular::haveLTLastFoot() const
1912 if (endlastfoot.empty)
1914 for (row_type i = 0; i < rows_; ++i)
1915 if (row_info[i].endlastfoot)
1921 // end longtable support functions
1923 void Tabular::setAscentOfRow(row_type row, int height)
1925 if (row >= rows_ || row_info[row].ascent_of_row == height)
1927 row_info[row].ascent_of_row = height;
1931 void Tabular::setDescentOfRow(row_type row, int height)
1933 if (row >= rows_ || row_info[row].descent_of_row == height)
1935 row_info[row].descent_of_row = height;
1939 int Tabular::getAscentOfRow(row_type row) const
1943 return row_info[row].ascent_of_row;
1947 int Tabular::getDescentOfRow(row_type row) const
1949 BOOST_ASSERT(row < rows_);
1950 return row_info[row].descent_of_row;
1954 int Tabular::getHeightOfTabular() const
1957 for (row_type row = 0; row < rows_; ++row)
1958 height += getAscentOfRow(row) + getDescentOfRow(row) +
1959 getAdditionalHeight(row);
1964 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1966 BOOST_ASSERT(row < rows_);
1967 BOOST_ASSERT(column < columns_);
1968 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1972 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1974 // FIXME: assert or return 0 as in TeXBottomHLine()?
1975 BOOST_ASSERT(row != npos);
1976 BOOST_ASSERT(row < rows_);
1978 idx_type const fcell = getFirstCellInRow(row);
1979 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1982 for (idx_type i = fcell; i < n; ++i) {
1986 if (use_booktabs && row == 0) {
1988 } else if (tmp == n - fcell) {
1989 os << (use_booktabs ? "\\midrule " : "\\hline ");
1991 for (idx_type i = fcell; i < n; ++i) {
1993 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1994 << column_of_cell(i) + 1
1996 << right_column_of_cell(i) + 1
2008 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2010 // FIXME: return 0 or assert as in TeXTopHLine()?
2011 if (row == npos || row >= rows_)
2014 idx_type const fcell = getFirstCellInRow(row);
2015 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2018 for (idx_type i = fcell; i < n; ++i) {
2022 if (use_booktabs && row == rows_ - 1) {
2023 os << "\\bottomrule";
2024 } else if (tmp == n - fcell) {
2025 os << (use_booktabs ? "\\midrule" : "\\hline");
2027 for (idx_type i = fcell; i < n; ++i) {
2028 if (bottomLine(i)) {
2029 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2030 << column_of_cell(i) + 1
2032 << right_column_of_cell(i) + 1
2044 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2048 if (getRotateCell(cell)) {
2049 os << "\\begin{sideways}\n";
2052 if (isMultiColumn(cell)) {
2053 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2054 if (leftLine(cell) &&
2055 (isFirstCellInRow(cell) ||
2056 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2057 !rightLine(cell - 1, true))))
2059 if (!cellinfo_of_cell(cell).align_special.empty()) {
2060 os << cellinfo_of_cell(cell).align_special;
2062 if (!getPWidth(cell).zero()) {
2063 switch (getVAlignment(cell)) {
2064 case LYX_VALIGN_TOP:
2067 case LYX_VALIGN_MIDDLE:
2070 case LYX_VALIGN_BOTTOM:
2075 << from_ascii(getPWidth(cell).asLatexString())
2078 switch (getAlignment(cell)) {
2079 case LYX_ALIGN_LEFT:
2082 case LYX_ALIGN_RIGHT:
2089 } // end if else !getPWidth
2090 } // end if else !cellinfo_of_cell
2091 if (rightLine(cell))
2093 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2098 if (getUsebox(cell) == BOX_PARBOX) {
2100 switch (getVAlignment(cell)) {
2101 case LYX_VALIGN_TOP:
2104 case LYX_VALIGN_MIDDLE:
2107 case LYX_VALIGN_BOTTOM:
2111 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2113 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2114 os << "\\begin{minipage}[";
2115 switch (getVAlignment(cell)) {
2116 case LYX_VALIGN_TOP:
2119 case LYX_VALIGN_MIDDLE:
2122 case LYX_VALIGN_BOTTOM:
2126 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2134 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2139 if (getUsebox(cell) == BOX_PARBOX)
2141 else if (getUsebox(cell) == BOX_MINIPAGE) {
2142 os << "%\n\\end{minipage}";
2145 if (isMultiColumn(cell)) {
2148 if (getRotateCell(cell)) {
2149 os << "%\n\\end{sideways}";
2156 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2157 OutputParams const & runparams) const
2159 if (!is_long_tabular)
2163 // output header info
2165 if (endhead.topDL) {
2169 for (row_type i = 0; i < rows_; ++i) {
2170 if (row_info[i].endhead) {
2171 ret += TeXRow(os, i, buf, runparams);
2174 if (endhead.bottomDL) {
2178 os << "\\endhead\n";
2180 if (endfirsthead.empty) {
2181 os << "\\endfirsthead\n";
2185 // output firstheader info
2186 if (haveLTFirstHead()) {
2187 if (endfirsthead.topDL) {
2191 for (row_type i = 0; i < rows_; ++i) {
2192 if (row_info[i].endfirsthead) {
2193 ret += TeXRow(os, i, buf, runparams);
2196 if (endfirsthead.bottomDL) {
2200 os << "\\endfirsthead\n";
2203 // output footer info
2205 if (endfoot.topDL) {
2209 for (row_type i = 0; i < rows_; ++i) {
2210 if (row_info[i].endfoot) {
2211 ret += TeXRow(os, i, buf, runparams);
2214 if (endfoot.bottomDL) {
2218 os << "\\endfoot\n";
2220 if (endlastfoot.empty) {
2221 os << "\\endlastfoot\n";
2225 // output lastfooter info
2226 if (haveLTLastFoot()) {
2227 if (endlastfoot.topDL) {
2231 for (row_type i = 0; i < rows_; ++i) {
2232 if (row_info[i].endlastfoot) {
2233 ret += TeXRow(os, i, buf, runparams);
2236 if (endlastfoot.bottomDL) {
2240 os << "\\endlastfoot\n";
2247 bool Tabular::isValidRow(row_type row) const
2249 if (!is_long_tabular)
2251 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2252 !row_info[row].endfoot && !row_info[row].endlastfoot;
2256 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2257 OutputParams const & runparams) const
2259 idx_type cell = getCellNumber(i, 0);
2260 int ret = TeXTopHLine(os, i);
2261 if (row_info[i].top_space_default) {
2263 os << "\\addlinespace\n";
2265 os << "\\noalign{\\vskip\\doublerulesep}\n";
2267 } else if(!row_info[i].top_space.zero()) {
2269 os << "\\addlinespace["
2270 << from_ascii(row_info[i].top_space.asLatexString())
2273 os << "\\noalign{\\vskip"
2274 << from_ascii(row_info[i].top_space.asLatexString())
2280 for (col_type j = 0; j < columns_; ++j) {
2281 if (isPartOfMultiColumn(i, j))
2283 ret += TeXCellPreamble(os, cell);
2284 shared_ptr<InsetText> inset = getCellInset(cell);
2286 Paragraph const & par = inset->paragraphs().front();
2287 bool rtl = par.isRightToLeftPar(buf.params())
2289 && getPWidth(cell).zero();
2292 if (par.getParLanguage(buf.params())->lang() ==
2295 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2297 // currently, remaning RTL languages are arabic_arabtex and hebrew
2301 ret += inset->latex(buf, os, runparams);
2305 ret += TeXCellPostamble(os, cell);
2306 if (!isLastCellInRow(cell)) { // not last cell in row
2311 os << "\\tabularnewline";
2312 if (row_info[i].bottom_space_default) {
2314 os << "\\addlinespace";
2316 os << "[\\doublerulesep]";
2317 } else if (!row_info[i].bottom_space.zero()) {
2319 os << "\\addlinespace";
2321 << from_ascii(row_info[i].bottom_space.asLatexString())
2326 ret += TeXBottomHLine(os, i);
2327 if (row_info[i].interline_space_default) {
2329 os << "\\addlinespace\n";
2331 os << "\\noalign{\\vskip\\doublerulesep}\n";
2333 } else if (!row_info[i].interline_space.zero()) {
2335 os << "\\addlinespace["
2336 << from_ascii(row_info[i].interline_space.asLatexString())
2339 os << "\\noalign{\\vskip"
2340 << from_ascii(row_info[i].interline_space.asLatexString())
2348 int Tabular::latex(Buffer const & buf, odocstream & os,
2349 OutputParams const & runparams) const
2353 //+---------------------------------------------------------------------
2354 //+ first the opening preamble +
2355 //+---------------------------------------------------------------------
2358 os << "\\begin{sideways}\n";
2361 if (is_long_tabular)
2362 os << "\\begin{longtable}{";
2364 os << "\\begin{tabular}{";
2365 for (col_type i = 0; i < columns_; ++i) {
2366 if (!use_booktabs && column_info[i].left_line)
2368 if (!column_info[i].align_special.empty()) {
2369 os << column_info[i].align_special;
2371 if (!column_info[i].p_width.zero()) {
2372 switch (column_info[i].alignment) {
2373 case LYX_ALIGN_LEFT:
2374 os << ">{\\raggedright}";
2376 case LYX_ALIGN_RIGHT:
2377 os << ">{\\raggedleft}";
2379 case LYX_ALIGN_CENTER:
2380 os << ">{\\centering}";
2382 case LYX_ALIGN_NONE:
2383 case LYX_ALIGN_BLOCK:
2384 case LYX_ALIGN_LAYOUT:
2385 case LYX_ALIGN_SPECIAL:
2389 switch (column_info[i].valignment) {
2390 case LYX_VALIGN_TOP:
2393 case LYX_VALIGN_MIDDLE:
2396 case LYX_VALIGN_BOTTOM:
2401 << from_ascii(column_info[i].p_width.asLatexString())
2404 switch (column_info[i].alignment) {
2405 case LYX_ALIGN_LEFT:
2408 case LYX_ALIGN_RIGHT:
2415 } // end if else !column_info[i].p_width
2416 } // end if else !column_info[i].align_special
2417 if (!use_booktabs && column_info[i].right_line)
2423 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2425 //+---------------------------------------------------------------------
2426 //+ the single row and columns (cells) +
2427 //+---------------------------------------------------------------------
2429 for (row_type i = 0; i < rows_; ++i) {
2430 if (isValidRow(i)) {
2431 ret += TeXRow(os, i, buf, runparams);
2432 if (is_long_tabular && row_info[i].newpage) {
2433 os << "\\newpage\n";
2439 //+---------------------------------------------------------------------
2440 //+ the closing of the tabular +
2441 //+---------------------------------------------------------------------
2443 if (is_long_tabular)
2444 os << "\\end{longtable}";
2446 os << "\\end{tabular}";
2448 os << "\n\\end{sideways}";
2456 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2457 OutputParams const & runparams) const
2460 idx_type cell = getFirstCellInRow(row);
2463 for (col_type j = 0; j < columns_; ++j) {
2464 if (isPartOfMultiColumn(row, j))
2467 os << "<entry align=\"";
2468 switch (getAlignment(cell)) {
2469 case LYX_ALIGN_LEFT:
2472 case LYX_ALIGN_RIGHT:
2480 os << "\" valign=\"";
2481 switch (getVAlignment(cell)) {
2482 case LYX_VALIGN_TOP:
2485 case LYX_VALIGN_BOTTOM:
2488 case LYX_VALIGN_MIDDLE:
2493 if (isMultiColumn(cell)) {
2494 os << " namest=\"col" << j << "\" ";
2495 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2499 ret += getCellInset(cell)->docbook(buf, os, runparams);
2508 int Tabular::docbook(Buffer const & buf, odocstream & os,
2509 OutputParams const & runparams) const
2513 //+---------------------------------------------------------------------
2514 //+ first the opening preamble +
2515 //+---------------------------------------------------------------------
2517 os << "<tgroup cols=\"" << columns_
2518 << "\" colsep=\"1\" rowsep=\"1\">\n";
2520 for (col_type i = 0; i < columns_; ++i) {
2521 os << "<colspec colname=\"col" << i << "\" align=\"";
2522 switch (column_info[i].alignment) {
2523 case LYX_ALIGN_LEFT:
2526 case LYX_ALIGN_RIGHT:
2534 if (runparams.flavor == OutputParams::XML)
2540 //+---------------------------------------------------------------------
2541 //+ Long Tabular case +
2542 //+---------------------------------------------------------------------
2544 // output header info
2545 if (haveLTHead() || haveLTFirstHead()) {
2548 for (row_type i = 0; i < rows_; ++i) {
2549 if (row_info[i].endhead || row_info[i].endfirsthead) {
2550 ret += docbookRow(buf, os, i, runparams);
2556 // output footer info
2557 if (haveLTFoot() || haveLTLastFoot()) {
2560 for (row_type i = 0; i < rows_; ++i) {
2561 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2562 ret += docbookRow(buf, os, i, runparams);
2569 //+---------------------------------------------------------------------
2570 //+ the single row and columns (cells) +
2571 //+---------------------------------------------------------------------
2575 for (row_type i = 0; i < rows_; ++i) {
2576 if (isValidRow(i)) {
2577 ret += docbookRow(buf, os, i, runparams);
2582 //+---------------------------------------------------------------------
2583 //+ the closing of the tabular +
2584 //+---------------------------------------------------------------------
2593 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2594 vector<unsigned int> const & clen) const
2596 idx_type const fcell = getFirstCellInRow(row);
2597 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2600 for (idx_type i = fcell; i < n; ++i) {
2610 for (idx_type i = fcell; i < n; ++i) {
2621 col_type column = column_of_cell(i);
2622 int len = clen[column];
2623 while (column < columns_ - 1
2624 && isPartOfMultiColumn(row, ++column))
2625 len += clen[column] + 4;
2626 os << docstring(len, ch);
2641 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2642 vector<unsigned int> const & clen) const
2644 idx_type const fcell = getFirstCellInRow(row);
2645 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2648 for (idx_type i = fcell; i < n; ++i) {
2649 if (bottomLine(i)) {
2658 for (idx_type i = fcell; i < n; ++i) {
2659 if (bottomLine(i)) {
2669 col_type column = column_of_cell(i);
2670 int len = clen[column];
2671 while (column < columns_ -1
2672 && isPartOfMultiColumn(row, ++column))
2673 len += clen[column] + 4;
2674 os << docstring(len, ch);
2675 if (bottomLine(i)) {
2689 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2690 OutputParams const & runparams,
2691 idx_type cell, row_type row, col_type column,
2692 vector<unsigned int> const & clen,
2693 bool onlydata) const
2695 odocstringstream sstr;
2696 getCellInset(cell)->plaintext(buf, sstr, runparams);
2708 unsigned int len1 = sstr.str().length();
2709 unsigned int len2 = clen[column];
2710 while (column < columns_ -1
2711 && isPartOfMultiColumn(row, ++column))
2712 len2 += clen[column] + 4;
2715 switch (getAlignment(cell)) {
2717 case LYX_ALIGN_LEFT:
2720 case LYX_ALIGN_RIGHT:
2724 case LYX_ALIGN_CENTER:
2730 os << docstring(len1, ' ') << sstr.str()
2731 << docstring(len2, ' ');
2733 if (rightLine(cell))
2740 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2741 OutputParams const & runparams, int const depth,
2742 bool onlydata, unsigned char delim) const
2744 // first calculate the width of the single columns
2745 vector<unsigned int> clen(columns_);
2748 // first all non (real) multicolumn cells!
2749 for (col_type j = 0; j < columns_; ++j) {
2751 for (row_type i = 0; i < rows_; ++i) {
2752 idx_type cell = getCellNumber(i, j);
2753 if (isMultiColumnReal(cell))
2755 odocstringstream sstr;
2756 getCellInset(cell)->plaintext(buf, sstr, runparams);
2757 if (clen[j] < sstr.str().length())
2758 clen[j] = sstr.str().length();
2761 // then all (real) multicolumn cells!
2762 for (col_type j = 0; j < columns_; ++j) {
2763 for (row_type i = 0; i < rows_; ++i) {
2764 idx_type cell = getCellNumber(i, j);
2765 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2767 odocstringstream sstr;
2768 getCellInset(cell)->plaintext(buf, sstr, runparams);
2769 int len = int(sstr.str().length());
2770 idx_type const n = cells_in_multicolumn(cell);
2771 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2773 if (len > int(clen[j + n - 1]))
2774 clen[j + n - 1] = len;
2779 for (row_type i = 0; i < rows_; ++i) {
2780 if (!onlydata && plaintextTopHLine(os, i, clen))
2781 os << docstring(depth * 2, ' ');
2782 for (col_type j = 0; j < columns_; ++j) {
2783 if (isPartOfMultiColumn(i, j))
2785 if (onlydata && j > 0)
2787 plaintextPrintCell(buf, os, runparams,
2788 cell, i, j, clen, onlydata);
2793 os << docstring(depth * 2, ' ');
2794 if (plaintextBottomHLine(os, i, clen))
2795 os << docstring(depth * 2, ' ');
2801 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2803 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2807 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2808 col_type column) const
2810 return cell_info[row][column].inset;
2814 void Tabular::setCellInset(row_type row, col_type column,
2815 shared_ptr<InsetText> ins) const
2817 cell_info[row][column].inset = ins;
2822 Tabular::getCellFromInset(Inset const * inset) const
2824 // is this inset part of the tabular?
2826 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2827 BOOST_ASSERT(false);
2830 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2831 if (getCellInset(cell).get() == inset) {
2832 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2833 << "cell=" << cell << endl;
2837 // We should have found a cell at this point
2838 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2839 << inset << " not found!" << endl;
2840 BOOST_ASSERT(false);
2846 void Tabular::validate(LaTeXFeatures & features) const
2848 features.require("NeedTabularnewline");
2850 features.require("booktabs");
2851 if (isLongTabular())
2852 features.require("longtable");
2854 features.require("rotating");
2855 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2856 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2857 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2858 features.require("array");
2859 getCellInset(cell)->validate(features);
2864 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2866 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2867 ParagraphList::const_iterator cit = parlist.begin();
2868 ParagraphList::const_iterator end = parlist.end();
2870 for (; cit != end; ++cit)
2871 for (int i = 0; i < cit->size(); ++i)
2872 if (cit->isNewline(i))
2879 /////////////////////////////////////////////////////////////////////
2883 /////////////////////////////////////////////////////////////////////
2885 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2887 : tabular(buf.params(), max(rows, row_type(1)),
2888 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2892 InsetTabular::InsetTabular(InsetTabular const & tab)
2893 : Inset(tab), tabular(tab.tabular),
2894 buffer_(tab.buffer_), scx_(0)
2898 InsetTabular::~InsetTabular()
2900 InsetTabularMailer(*this).hideDialog();
2904 auto_ptr<Inset> InsetTabular::doClone() const
2906 return auto_ptr<Inset>(new InsetTabular(*this));
2910 Buffer const & InsetTabular::buffer() const
2916 void InsetTabular::buffer(Buffer const * b)
2922 void InsetTabular::write(Buffer const & buf, ostream & os) const
2924 os << "Tabular" << endl;
2925 tabular.write(buf, os);
2929 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2931 bool const old_format = (lex.getString() == "\\LyXTable");
2933 tabular.read(buf, lex);
2939 string token = lex.getString();
2940 while (lex && token != "\\end_inset") {
2942 token = lex.getString();
2945 lex.printError("Missing \\end_inset at this point. "
2951 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2953 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2954 // mi.base.textwidth << "\n";
2956 lyxerr << "InsetTabular::metrics: need bv" << endl;
2957 BOOST_ASSERT(false);
2961 for (idx_type cell = 0; i < tabular.rows(); ++i) {
2964 for (col_type j = 0; j < tabular.columns(); ++j) {
2965 if (tabular.isPartOfMultiColumn(i, j))
2966 // Multicolumn cell, but not first one
2971 if (tabular.cell_info[i][j].multicolumn ==
2972 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2973 p_width = tabular.cellinfo_of_cell(cell).p_width;
2975 p_width = tabular.column_info[j].p_width;
2976 if (!p_width.zero())
2977 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2978 tabular.getCellInset(cell)->metrics(m, dim);
2979 if (!p_width.zero())
2980 dim.wid = m.base.textwidth;
2981 tabular.setWidthOfCell(cell, dim.wid);
2982 if (p_width.zero()) {
2983 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2984 // FIXME there must be a way to get rid of
2985 // the second metrics call
2986 tabular.getCellInset(cell)->metrics(m, dim);
2988 maxAsc = max(maxAsc, dim.asc);
2989 maxDesc = max(maxDesc, dim.des);
2992 int const top_space = tabular.row_info[i].top_space_default ?
2993 default_line_space :
2994 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2995 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
2996 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2997 default_line_space :
2998 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2999 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3002 dim.asc = tabular.getAscentOfRow(0);
3003 dim.des = tabular.getHeightOfTabular() - dim.asc;
3004 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
3005 bool const changed = dim_ != dim;
3011 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3013 setPosCache(pi, x, y);
3015 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3016 BufferView * bv = pi.base.bv;
3018 resetPos(bv->cursor());
3021 x += ADD_TO_TABULAR_WIDTH;
3024 first_visible_cell = Tabular::npos;
3025 for (row_type i = 0; i < tabular.rows(); ++i) {
3027 int const a = tabular.getAscentOfRow(i);
3028 int const d = tabular.getDescentOfRow(i);
3029 idx = tabular.getCellNumber(i, 0);
3030 for (col_type j = 0; j < tabular.columns(); ++j) {
3031 if (tabular.isPartOfMultiColumn(i, j))
3033 if (first_visible_cell == Tabular::npos)
3034 first_visible_cell = idx;
3036 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3037 if (nx + tabular.getWidthOfColumn(idx) < 0
3038 || nx > bv->workWidth()
3040 || y - a > bv->workHeight()) {
3041 pi.pain.setDrawingEnabled(false);
3042 cell(idx)->draw(pi, cx, y);
3043 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3044 pi.pain.setDrawingEnabled(true);
3046 cell(idx)->draw(pi, cx, y);
3047 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3049 nx += tabular.getWidthOfColumn(idx);
3053 if (i + 1 < tabular.rows())
3054 y += d + tabular.getAscentOfRow(i + 1) +
3055 tabular.getAdditionalHeight(i + 1);
3060 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3062 setPosCache(pi, x, y);
3064 Cursor & cur = pi.base.bv->cursor();
3066 x += scx_ + ADD_TO_TABULAR_WIDTH;
3068 // Paint background of current tabular
3069 int const w = tabular.getWidthOfTabular();
3070 int const h = tabular.getHeightOfTabular();
3071 int yy = y - tabular.getAscentOfRow(0);
3072 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3074 if (!cur.selection())
3076 if (!ptr_cmp(&cur.inset(), this))
3082 if (tablemode(cur)) {
3085 getSelection(cur, rs, re, cs, ce);
3086 y -= tabular.getAscentOfRow(0);
3087 for (row_type j = 0; j < tabular.rows(); ++j) {
3088 int const a = tabular.getAscentOfRow(j);
3089 int const h = a + tabular.getDescentOfRow(j);
3091 y += tabular.getAdditionalHeight(j);
3092 for (col_type i = 0; i < tabular.columns(); ++i) {
3093 if (tabular.isPartOfMultiColumn(j, i))
3095 idx_type const cell =
3096 tabular.getCellNumber(j, i);
3097 int const w = tabular.getWidthOfColumn(cell);
3098 if (i >= cs && i <= ce && j >= rs && j <= re)
3099 pi.pain.fillRectangle(xx, y, w, h,
3107 x += getCellXPos(cur.idx());
3108 x += tabular.getBeginningOfTextInCell(cur.idx());
3109 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3114 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3115 row_type row, idx_type cell, bool erased) const
3117 int x2 = x + tabular.getWidthOfColumn(cell);
3118 bool on_off = false;
3119 Color::color col = Color::tabularline;
3120 Color::color onoffcol = Color::tabularonoffline;
3123 col = Color::deletedtext;
3124 onoffcol = Color::deletedtext;
3127 if (!tabular.topAlreadyDrawn(cell)) {
3128 on_off = !tabular.topLine(cell);
3129 pain.line(x, y - tabular.getAscentOfRow(row),
3130 x2, y - tabular.getAscentOfRow(row),
3131 on_off ? onoffcol : col,
3132 on_off ? Painter::line_onoffdash : Painter::line_solid);
3134 on_off = !tabular.bottomLine(cell);
3135 pain.line(x, y + tabular.getDescentOfRow(row),
3136 x2, y + tabular.getDescentOfRow(row),
3137 on_off ? onoffcol : col,
3138 on_off ? Painter::line_onoffdash : Painter::line_solid);
3139 if (!tabular.leftAlreadyDrawn(cell)) {
3140 on_off = !tabular.leftLine(cell);
3141 pain.line(x, y - tabular.getAscentOfRow(row),
3142 x, y + tabular.getDescentOfRow(row),
3143 on_off ? onoffcol : col,
3144 on_off ? Painter::line_onoffdash : Painter::line_solid);
3146 on_off = !tabular.rightLine(cell);
3147 pain.line(x2 - tabular.getAdditionalWidth(cell),
3148 y - tabular.getAscentOfRow(row),
3149 x2 - tabular.getAdditionalWidth(cell),
3150 y + tabular.getDescentOfRow(row),
3151 on_off ? onoffcol : col,
3152 on_off ? Painter::line_onoffdash : Painter::line_solid);
3156 docstring const InsetTabular::editMessage() const
3158 return _("Opened table");
3162 void InsetTabular::edit(Cursor & cur, bool left)
3164 //lyxerr << "InsetTabular::edit: " << this << endl;
3166 cur.selection() = false;
3169 if (isRightToLeft(cur))
3170 cur.idx() = tabular.getLastCellInRow(0);
3176 if (isRightToLeft(cur))
3177 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3179 cur.idx() = tabular.getNumberOfCells() - 1;
3181 cur.pos() = cur.lastpos(); // FIXME crude guess
3183 // FIXME: this accesses the position cache before it is initialized
3185 //cur.bv().fitCursor();
3189 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3191 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3192 << "\n cur:" << cur << endl;
3193 CursorSlice sl = cur.top();
3194 Cursor & bvcur = cur.bv().cursor();
3196 switch (cmd.action) {
3198 case LFUN_MOUSE_PRESS:
3199 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3201 if (cmd.button() == mouse_button::button1
3202 || (cmd.button() == mouse_button::button3
3203 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
3204 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
3206 cur.selection() = false;
3207 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3208 cur.bv().mouseSetCursor(cur);
3212 if (cmd.button() == mouse_button::button2) {
3213 if (cap::selection()) {
3214 // See comment in Text::dispatch why we
3216 // FIXME This does not use paste_tabular,
3217 // another reason why paste_tabular should go.
3218 cap::copySelectionToStack();
3219 cmd = FuncRequest(LFUN_PASTE, "0");
3221 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
3224 doDispatch(cur, cmd);
3225 cur.bv().buffer()->markDirty();
3226 cur.bv().mouseSetCursor(cur);
3230 case LFUN_MOUSE_MOTION:
3231 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3232 if (cmd.button() == mouse_button::button1) {
3233 // only accept motions to places not deeper nested than the real anchor
3234 if (bvcur.anchor_.hasPart(cur)) {
3235 // only update if selection changes
3236 if (bvcur.idx() == cur.idx() &&
3237 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3239 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3240 bvcur.setCursor(cur);
3241 bvcur.selection() = true;
3247 case LFUN_MOUSE_RELEASE:
3248 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3249 if (cmd.button() == mouse_button::button3)
3250 InsetTabularMailer(*this).showDialog(&cur.bv());
3253 case LFUN_CELL_BACKWARD:
3255 cur.selection() = false;
3258 case LFUN_CELL_FORWARD:
3260 cur.selection() = false;
3263 case LFUN_CHAR_FORWARD_SELECT:
3264 case LFUN_CHAR_FORWARD:
3265 cell(cur.idx())->dispatch(cur, cmd);
3266 if (!cur.result().dispatched()) {
3267 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3268 if (sl == cur.top())
3269 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3275 case LFUN_CHAR_BACKWARD_SELECT:
3276 case LFUN_CHAR_BACKWARD:
3277 cell(cur.idx())->dispatch(cur, cmd);
3278 if (!cur.result().dispatched()) {
3279 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3280 if (sl == cur.top())
3281 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3287 case LFUN_DOWN_SELECT:
3289 cell(cur.idx())->dispatch(cur, cmd);
3290 cur.dispatched(); // override the cell's decision
3291 if (sl == cur.top())
3292 // if our Text didn't do anything to the cursor
3293 // then we try to put the cursor into the cell below
3294 // setting also the right targetX.
3295 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3296 cur.idx() = tabular.getCellBelow(cur.idx());
3298 TextMetrics const & tm =
3299 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3300 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3302 if (sl == cur.top()) {
3303 // we trick it to go to the RIGHT after leaving the
3305 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3310 case LFUN_UP_SELECT:
3312 cell(cur.idx())->dispatch(cur, cmd);
3313 cur.dispatched(); // override the cell's decision
3314 if (sl == cur.top())
3315 // if our Text didn't do anything to the cursor
3316 // then we try to put the cursor into the cell above
3317 // setting also the right targetX.
3318 if (tabular.row_of_cell(cur.idx()) != 0) {
3319 cur.idx() = tabular.getCellAbove(cur.idx());
3320 cur.pit() = cur.lastpit();
3321 Text const * text = cell(cur.idx())->getText(0);
3322 TextMetrics const & tm = cur.bv().textMetrics(text);
3323 ParagraphMetrics const & pm =
3324 tm.parMetrics(cur.lastpit());
3325 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3327 if (sl == cur.top()) {
3328 cmd = FuncRequest(LFUN_UP);
3333 // case LFUN_SCREEN_DOWN: {
3334 // //if (hasSelection())
3335 // // cur.selection() = false;
3336 // col_type const col = tabular.column_of_cell(cur.idx());
3337 // int const t = cur.bv().top_y() + cur.bv().height();
3338 // if (t < yo() + tabular.getHeightOfTabular()) {
3339 // cur.bv().scrollDocView(t);
3340 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3342 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3349 // case LFUN_SCREEN_UP: {
3350 // //if (hasSelection())
3351 // // cur.selection() = false;
3352 // col_type const col = tabular.column_of_cell(cur.idx());
3353 // int const t = cur.bv().top_y() + cur.bv().height();
3355 // cur.bv().scrollDocView(t);
3359 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3363 // cur.par() = cur.lastpar();
3364 // cur.pos() = cur.lastpos();
3368 case LFUN_LAYOUT_TABULAR:
3369 InsetTabularMailer(*this).showDialog(&cur.bv());
3372 case LFUN_INSET_DIALOG_UPDATE:
3373 InsetTabularMailer(*this).updateDialog(&cur.bv());
3376 case LFUN_TABULAR_FEATURE:
3377 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3381 // insert file functions
3382 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3383 case LFUN_FILE_INSERT_PLAINTEXT: {
3385 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3386 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3392 if (tablemode(cur)) {
3393 if (copySelection(cur)) {
3394 recordUndoInset(cur, Undo::DELETE);
3399 cell(cur.idx())->dispatch(cur, cmd);
3402 case LFUN_CHAR_DELETE_BACKWARD:
3403 case LFUN_CHAR_DELETE_FORWARD:
3404 if (tablemode(cur)) {
3405 recordUndoInset(cur, Undo::DELETE);
3409 cell(cur.idx())->dispatch(cur, cmd);
3413 if (!cur.selection())
3415 if (tablemode(cur)) {
3419 cell(cur.idx())->dispatch(cur, cmd);
3422 case LFUN_CLIPBOARD_PASTE:
3423 case LFUN_PRIMARY_SELECTION_PASTE: {
3424 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3425 theClipboard().getAsText() :
3426 theSelection().get();
3429 // pass to InsertPlaintextString, but
3430 // only if we have multi-cell content
3431 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3432 if (insertPlaintextString(cur.bv(), clip, false)) {
3433 // content has been replaced,
3434 // so cursor might be invalid
3435 cur.pos() = cur.lastpos();
3436 bvcur.setCursor(cur);
3440 // Let the cell handle normal text
3441 cell(cur.idx())->dispatch(cur, cmd);
3446 if (tabularStackDirty() && theClipboard().isInternal()) {
3447 recordUndoInset(cur, Undo::INSERT);
3448 pasteClipboard(cur);
3451 cell(cur.idx())->dispatch(cur, cmd);
3454 case LFUN_FONT_EMPH:
3455 case LFUN_FONT_BOLD:
3456 case LFUN_FONT_ROMAN:
3457 case LFUN_FONT_NOUN:
3458 case LFUN_FONT_ITAL:
3459 case LFUN_FONT_FRAK:
3460 case LFUN_FONT_CODE:
3461 case LFUN_FONT_SANS:
3462 case LFUN_FONT_FREE_APPLY:
3463 case LFUN_FONT_FREE_UPDATE:
3464 case LFUN_FONT_SIZE:
3465 case LFUN_FONT_UNDERLINE:
3467 case LFUN_WORD_CAPITALIZE:
3468 case LFUN_WORD_UPCASE:
3469 case LFUN_WORD_LOWCASE:
3470 case LFUN_CHARS_TRANSPOSE:
3471 if (tablemode(cur)) {
3474 getSelection(cur, rs, re, cs, ce);
3475 Cursor tmpcur = cur;
3476 for (row_type i = rs; i <= re; ++i) {
3477 for (col_type j = cs; j <= ce; ++j) {
3478 // cursor follows cell:
3479 tmpcur.idx() = tabular.getCellNumber(i, j);
3480 // select this cell only:
3483 tmpcur.resetAnchor();
3484 tmpcur.pit() = tmpcur.lastpit();
3485 tmpcur.pos() = tmpcur.top().lastpos();
3486 tmpcur.setCursor(tmpcur);
3487 tmpcur.setSelection();
3488 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3493 cell(cur.idx())->dispatch(cur, cmd);
3497 // we try to handle this event in the insets dispatch function.
3498 cell(cur.idx())->dispatch(cur, cmd);
3504 // function sets an object as defined in func_status.h:
3505 // states OK, Unknown, Disabled, On, Off.
3506 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3507 FuncStatus & status) const
3509 switch (cmd.action) {
3510 case LFUN_TABULAR_FEATURE: {
3511 int action = Tabular::LAST_ACTION;
3513 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3514 string const tmp = tabularFeature[i].feature;
3515 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3516 action = tabularFeature[i].action;
3520 if (action == Tabular::LAST_ACTION) {
3522 status.unknown(true);
3526 string const argument
3527 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3529 row_type sel_row_start = 0;
3530 row_type sel_row_end = 0;
3531 col_type sel_col_start = 0;
3532 col_type sel_col_end = 0;
3533 Tabular::ltType dummyltt;
3536 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3539 case Tabular::SET_PWIDTH:
3540 case Tabular::SET_MPWIDTH:
3541 case Tabular::SET_SPECIAL_COLUMN:
3542 case Tabular::SET_SPECIAL_MULTI:
3543 case Tabular::APPEND_ROW:
3544 case Tabular::APPEND_COLUMN:
3545 case Tabular::DELETE_ROW:
3546 case Tabular::DELETE_COLUMN:
3547 case Tabular::COPY_ROW:
3548 case Tabular::COPY_COLUMN:
3549 case Tabular::SET_ALL_LINES:
3550 case Tabular::UNSET_ALL_LINES:
3551 case Tabular::SET_TOP_SPACE:
3552 case Tabular::SET_BOTTOM_SPACE:
3553 case Tabular::SET_INTERLINE_SPACE:
3557 case Tabular::MULTICOLUMN:
3558 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3561 case Tabular::M_TOGGLE_LINE_TOP:
3563 case Tabular::TOGGLE_LINE_TOP:
3564 status.setOnOff(tabular.topLine(cur.idx(), flag));
3567 case Tabular::M_TOGGLE_LINE_BOTTOM:
3569 case Tabular::TOGGLE_LINE_BOTTOM:
3570 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3573 case Tabular::M_TOGGLE_LINE_LEFT:
3575 case Tabular::TOGGLE_LINE_LEFT:
3576 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3579 case Tabular::M_TOGGLE_LINE_RIGHT:
3581 case Tabular::TOGGLE_LINE_RIGHT:
3582 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3585 case Tabular::M_ALIGN_LEFT:
3587 case Tabular::ALIGN_LEFT:
3588 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3591 case Tabular::M_ALIGN_RIGHT:
3593 case Tabular::ALIGN_RIGHT:
3594 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3597 case Tabular::M_ALIGN_CENTER:
3599 case Tabular::ALIGN_CENTER:
3600 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3603 case Tabular::ALIGN_BLOCK:
3604 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3605 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3608 case Tabular::M_VALIGN_TOP:
3610 case Tabular::VALIGN_TOP:
3612 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3615 case Tabular::M_VALIGN_BOTTOM:
3617 case Tabular::VALIGN_BOTTOM:
3619 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3622 case Tabular::M_VALIGN_MIDDLE:
3624 case Tabular::VALIGN_MIDDLE:
3626 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3629 case Tabular::SET_LONGTABULAR:
3630 status.setOnOff(tabular.isLongTabular());
3633 case Tabular::UNSET_LONGTABULAR:
3634 status.setOnOff(!tabular.isLongTabular());
3637 case Tabular::TOGGLE_ROTATE_TABULAR:
3638 case Tabular::SET_ROTATE_TABULAR:
3639 status.setOnOff(tabular.getRotateTabular());
3642 case Tabular::UNSET_ROTATE_TABULAR:
3643 status.setOnOff(!tabular.getRotateTabular());
3646 case Tabular::TOGGLE_ROTATE_CELL:
3647 case Tabular::SET_ROTATE_CELL:
3648 status.setOnOff(!oneCellHasRotationState(false,
3649 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3652 case Tabular::UNSET_ROTATE_CELL:
3653 status.setOnOff(!oneCellHasRotationState(true,
3654 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3657 case Tabular::SET_USEBOX:
3658 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3661 case Tabular::SET_LTFIRSTHEAD:
3662 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3665 case Tabular::UNSET_LTFIRSTHEAD:
3666 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3669 case Tabular::SET_LTHEAD:
3670 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3673 case Tabular::UNSET_LTHEAD:
3674 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3677 case Tabular::SET_LTFOOT:
3678 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3681 case Tabular::UNSET_LTFOOT:
3682 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3685 case Tabular::SET_LTLASTFOOT:
3686 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3689 case Tabular::UNSET_LTLASTFOOT:
3690 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3693 case Tabular::SET_LTNEWPAGE:
3694 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3697 case Tabular::SET_BOOKTABS:
3698 status.setOnOff(tabular.useBookTabs());
3701 case Tabular::UNSET_BOOKTABS:
3702 status.setOnOff(!tabular.useBookTabs());
3707 status.enabled(false);
3713 // These are only enabled inside tabular
3714 case LFUN_CELL_BACKWARD:
3715 case LFUN_CELL_FORWARD:
3716 status.enabled(true);
3719 // disable these with multiple cells selected
3720 case LFUN_INSET_INSERT:
3721 case LFUN_TABULAR_INSERT:
3722 case LFUN_CHARSTYLE_INSERT:
3723 case LFUN_FLOAT_INSERT:
3724 case LFUN_FLOAT_WIDE_INSERT:
3725 case LFUN_FOOTNOTE_INSERT:
3726 case LFUN_MARGINALNOTE_INSERT:
3727 case LFUN_MATH_INSERT:
3728 case LFUN_MATH_MODE:
3729 case LFUN_MATH_MUTATE:
3730 case LFUN_MATH_DISPLAY:
3731 case LFUN_NOTE_INSERT:
3732 case LFUN_OPTIONAL_INSERT:
3733 case LFUN_BOX_INSERT:
3734 case LFUN_BRANCH_INSERT:
3735 case LFUN_WRAP_INSERT:
3736 case LFUN_ERT_INSERT: {
3737 if (tablemode(cur)) {
3738 status.enabled(false);
3741 return cell(cur.idx())->getStatus(cur, cmd, status);
3744 // disable in non-fixed-width cells
3745 case LFUN_BREAK_LINE:
3746 case LFUN_BREAK_PARAGRAPH:
3747 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3748 case LFUN_BREAK_PARAGRAPH_SKIP: {
3749 if (tabular.getPWidth(cur.idx()).zero()) {
3750 status.enabled(false);
3753 return cell(cur.idx())->getStatus(cur, cmd, status);
3757 if (tabularStackDirty() && theClipboard().isInternal()) {
3758 status.enabled(true);
3761 return cell(cur.idx())->getStatus(cur, cmd, status);
3763 case LFUN_INSET_MODIFY:
3764 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3765 status.enabled(true);
3771 // we try to handle this event in the insets dispatch function.
3772 return cell(cur.idx())->getStatus(cur, cmd, status);
3777 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3778 OutputParams const & runparams) const
3780 return tabular.latex(buf, os, runparams);
3784 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3785 OutputParams const & runparams) const
3787 os << '\n'; // output table on a new line
3788 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3789 tabular.plaintext(buf, os, runparams, dp, false, 0);
3790 return PLAINTEXT_NEWLINE;
3794 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3795 OutputParams const & runparams) const
3800 // FIXME: Why not pass a proper DocIterator here?
3802 // if the table is inside a float it doesn't need the informaltable
3803 // wrapper. Search for it.
3804 for (master = owner(); master; master = master->owner())
3805 if (master->lyxCode() == Inset::FLOAT_CODE)
3810 os << "<informaltable>";
3813 ret += tabular.docbook(buf, os, runparams);
3815 os << "</informaltable>";
3822 void InsetTabular::validate(LaTeXFeatures & features) const
3824 tabular.validate(features);
3828 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3830 return tabular.getCellInset(idx);
3834 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3836 return tabular.getCellInset(idx);
3840 void InsetTabular::cursorPos(BufferView const & bv,
3841 CursorSlice const & sl, bool boundary, int & x, int & y) const
3843 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3845 // y offset correction
3846 int const row = tabular.row_of_cell(sl.idx());
3847 for (int i = 0; i <= row; ++i) {
3849 y += tabular.getAscentOfRow(i);
3850 y += tabular.getAdditionalHeight(i);
3853 y += tabular.getDescentOfRow(i);
3856 // x offset correction
3857 int const col = tabular.column_of_cell(sl.idx());
3858 int idx = tabular.getCellNumber(row, 0);
3859 for (int j = 0; j < col; ++j) {
3860 if (tabular.isPartOfMultiColumn(row, j))
3862 x += tabular.getWidthOfColumn(idx);
3865 x += tabular.getBeginningOfTextInCell(idx);
3866 x += ADD_TO_TABULAR_WIDTH;
3871 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3875 Inset const & inset = *tabular.getCellInset(cell);
3876 Point o = bv.coordCache().getInsets().xy(&inset);
3877 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3878 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3879 row_type const row = tabular.row_of_cell(cell);
3880 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3881 tabular.getAdditionalHeight(row);
3882 int const yend = o.y_ + tabular.getDescentOfRow(row);
3894 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3895 // << " ybeg=" << ybeg << " yend=" << yend
3896 // << " xx=" << xx << " yy=" << yy
3897 // << " dist=" << xx + yy << endl;
3902 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3904 //lyxerr << "InsetTabular::editXY: " << this << endl;
3905 cur.selection() = false;
3907 cur.idx() = getNearestCell(cur.bv(), x, y);
3909 return cell(cur.idx())->text_.editXY(cur, x, y);
3913 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3915 cur.idx() = getNearestCell(cur.bv(), x, y);
3916 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3920 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3922 idx_type idx_min = 0;
3923 int dist_min = std::numeric_limits<int>::max();
3924 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3925 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3926 int const d = dist(bv, i, x, y);
3937 int InsetTabular::getCellXPos(idx_type const cell) const
3941 for (; !tabular.isFirstCellInRow(c); --c)
3944 for (; c < cell; ++c)
3945 lx += tabular.getWidthOfColumn(c);
3951 void InsetTabular::resetPos(Cursor & cur) const
3953 BufferView & bv = cur.bv();
3954 int const maxwidth = bv.workWidth();
3956 if (&cur.inset() != this) {
3960 int const X2 = maxwidth;
3961 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3962 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3963 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3966 scx_ = X1 + 20 - x1;
3968 scx_ = X2 - 20 - x2;
3973 cur.updateFlags(Update::Force | Update::FitCursor);
3977 void InsetTabular::moveNextCell(Cursor & cur)
3979 if (isRightToLeft(cur)) {
3980 if (tabular.isFirstCellInRow(cur.idx())) {
3981 row_type const row = tabular.row_of_cell(cur.idx());
3982 if (row == tabular.rows() - 1)
3984 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3991 if (tabular.isLastCell(cur.idx()))
4001 void InsetTabular::movePrevCell(Cursor & cur)
4003 if (isRightToLeft(cur)) {
4004 if (tabular.isLastCellInRow(cur.idx())) {
4005 row_type const row = tabular.row_of_cell(cur.idx());
4008 cur.idx() = tabular.getFirstCellInRow(row);
4009 cur.idx() = tabular.getCellAbove(cur.idx());
4011 if (tabular.isLastCell(cur.idx()))
4016 if (cur.idx() == 0) // first cell
4020 cur.pit() = cur.lastpit();
4021 cur.pos() = cur.lastpos();
4023 // FIXME: this accesses the position cache before it is initialized
4028 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4030 Tabular::Feature action = Tabular::LAST_ACTION;
4033 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4034 string const tmp = tabularFeature[i].feature;
4036 if (tmp == what.substr(0, tmp.length())) {
4037 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4038 //tabularFeatures[i].feature.length()))
4039 action = tabularFeature[i].action;
4043 if (action == Tabular::LAST_ACTION)
4047 ltrim(what.substr(tabularFeature[i].feature.length()));
4048 tabularFeatures(cur, action, val);
4053 static void checkLongtableSpecial(Tabular::ltType & ltt,
4054 string const & special, bool & flag)
4056 if (special == "dl_above") {
4059 } else if (special == "dl_below") {
4060 ltt.bottomDL = flag;
4062 } else if (special == "empty") {
4071 bool InsetTabular::oneCellHasRotationState(bool rotated,
4072 row_type row_start, row_type row_end,
4073 col_type col_start, col_type col_end) const {
4075 for (row_type i = row_start; i <= row_end; ++i) {
4076 for (col_type j = col_start; j <= col_end; ++j) {
4077 if (tabular.getRotateCell(tabular.getCellNumber(i, j))
4086 void InsetTabular::tabularFeatures(Cursor & cur,
4087 Tabular::Feature feature, string const & value)
4089 BufferView & bv = cur.bv();
4090 col_type sel_col_start;
4091 col_type sel_col_end;
4092 row_type sel_row_start;
4093 row_type sel_row_end;
4094 bool setLines = false;
4095 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4096 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4100 case Tabular::M_ALIGN_LEFT:
4101 case Tabular::ALIGN_LEFT:
4102 setAlign = LYX_ALIGN_LEFT;
4105 case Tabular::M_ALIGN_RIGHT:
4106 case Tabular::ALIGN_RIGHT:
4107 setAlign = LYX_ALIGN_RIGHT;
4110 case Tabular::M_ALIGN_CENTER:
4111 case Tabular::ALIGN_CENTER:
4112 setAlign = LYX_ALIGN_CENTER;
4115 case Tabular::ALIGN_BLOCK:
4116 setAlign = LYX_ALIGN_BLOCK;
4119 case Tabular::M_VALIGN_TOP:
4120 case Tabular::VALIGN_TOP:
4121 setVAlign = Tabular::LYX_VALIGN_TOP;
4124 case Tabular::M_VALIGN_BOTTOM:
4125 case Tabular::VALIGN_BOTTOM:
4126 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4129 case Tabular::M_VALIGN_MIDDLE:
4130 case Tabular::VALIGN_MIDDLE:
4131 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4138 recordUndoInset(cur, Undo::ATOMIC);
4140 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4141 row_type const row = tabular.row_of_cell(cur.idx());
4142 col_type const column = tabular.column_of_cell(cur.idx());
4144 Tabular::ltType ltt;
4148 case Tabular::SET_PWIDTH: {
4149 Length const len(value);
4150 tabular.setColumnPWidth(cur, cur.idx(), len);
4152 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4153 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4157 case Tabular::SET_MPWIDTH:
4158 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4161 case Tabular::SET_SPECIAL_COLUMN:
4162 case Tabular::SET_SPECIAL_MULTI:
4163 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4166 case Tabular::APPEND_ROW:
4167 // append the row into the tabular
4168 tabular.appendRow(bv.buffer()->params(), cur.idx());
4171 case Tabular::APPEND_COLUMN:
4172 // append the column into the tabular
4173 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4174 cur.idx() = tabular.getCellNumber(row, column);
4177 case Tabular::DELETE_ROW:
4178 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4179 tabular.deleteRow(sel_row_start);
4180 if (sel_row_start >= tabular.rows())
4182 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4185 cur.selection() = false;
4188 case Tabular::DELETE_COLUMN:
4189 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4190 tabular.deleteColumn(sel_col_start);
4191 if (sel_col_start >= tabular.columns())
4193 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4196 cur.selection() = false;
4199 case Tabular::COPY_ROW:
4200 tabular.copyRow(bv.buffer()->params(), row);
4203 case Tabular::COPY_COLUMN:
4204 tabular.copyColumn(bv.buffer()->params(), column);
4205 cur.idx() = tabular.getCellNumber(row, column);
4208 case Tabular::M_TOGGLE_LINE_TOP:
4210 case Tabular::TOGGLE_LINE_TOP: {
4211 bool lineSet = !tabular.topLine(cur.idx(), flag);
4212 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4213 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4215 tabular.getCellNumber(i, j),
4220 case Tabular::M_TOGGLE_LINE_BOTTOM:
4222 case Tabular::TOGGLE_LINE_BOTTOM: {
4223 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4224 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4225 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4226 tabular.setBottomLine(
4227 tabular.getCellNumber(i, j),
4233 case Tabular::M_TOGGLE_LINE_LEFT:
4235 case Tabular::TOGGLE_LINE_LEFT: {
4236 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4237 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4238 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4239 tabular.setLeftLine(
4240 tabular.getCellNumber(i,j),
4246 case Tabular::M_TOGGLE_LINE_RIGHT:
4248 case Tabular::TOGGLE_LINE_RIGHT: {
4249 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4250 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4251 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4252 tabular.setRightLine(
4253 tabular.getCellNumber(i,j),
4259 case Tabular::M_ALIGN_LEFT:
4260 case Tabular::M_ALIGN_RIGHT:
4261 case Tabular::M_ALIGN_CENTER:
4263 case Tabular::ALIGN_LEFT:
4264 case Tabular::ALIGN_RIGHT:
4265 case Tabular::ALIGN_CENTER:
4266 case Tabular::ALIGN_BLOCK:
4267 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4268 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4269 tabular.setAlignment(
4270 tabular.getCellNumber(i, j),
4275 case Tabular::M_VALIGN_TOP:
4276 case Tabular::M_VALIGN_BOTTOM:
4277 case Tabular::M_VALIGN_MIDDLE:
4279 case Tabular::VALIGN_TOP:
4280 case Tabular::VALIGN_BOTTOM:
4281 case Tabular::VALIGN_MIDDLE:
4282 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4283 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4284 tabular.setVAlignment(
4285 tabular.getCellNumber(i, j),
4289 case Tabular::MULTICOLUMN: {
4290 if (sel_row_start != sel_row_end) {
4291 // FIXME: Need I say it ? This is horrible.
4293 Alert::error(_("Error setting multicolumn"),
4294 _("You cannot set multicolumn vertically."));
4297 if (!cur.selection()) {
4298 // just multicol for one single cell
4299 // check whether we are completely in a multicol
4300 if (tabular.isMultiColumn(cur.idx()))
4301 tabular.unsetMultiColumn(cur.idx());
4303 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4306 // we have a selection so this means we just add all this
4307 // cells to form a multicolumn cell
4308 idx_type const s_start = cur.selBegin().idx();
4309 idx_type const s_end = cur.selEnd().idx();
4310 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4311 cur.idx() = s_start;
4314 cur.selection() = false;
4318 case Tabular::SET_ALL_LINES:
4320 case Tabular::UNSET_ALL_LINES:
4321 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4322 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4323 tabular.setAllLines(
4324 tabular.getCellNumber(i,j), setLines);
4327 case Tabular::SET_LONGTABULAR:
4328 tabular.setLongTabular(true);
4331 case Tabular::UNSET_LONGTABULAR:
4332 tabular.setLongTabular(false);
4335 case Tabular::SET_ROTATE_TABULAR:
4336 tabular.setRotateTabular(true);
4339 case Tabular::UNSET_ROTATE_TABULAR:
4340 tabular.setRotateTabular(false);
4343 case Tabular::TOGGLE_ROTATE_TABULAR:
4344 tabular.setRotateTabular(!tabular.getRotateTabular());
4347 case Tabular::SET_ROTATE_CELL:
4348 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4349 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4350 tabular.setRotateCell(
4351 tabular.getCellNumber(i, j), true);
4354 case Tabular::UNSET_ROTATE_CELL:
4355 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4356 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4357 tabular.setRotateCell(
4358 tabular.getCellNumber(i, j), false);
4361 case Tabular::TOGGLE_ROTATE_CELL:
4363 bool oneNotRotated = oneCellHasRotationState(false,
4364 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4366 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4367 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4368 tabular.setRotateCell(tabular.getCellNumber(i, j),
4373 case Tabular::SET_USEBOX: {
4374 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4375 if (val == tabular.getUsebox(cur.idx()))
4376 val = Tabular::BOX_NONE;
4377 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4378 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4379 tabular.setUsebox(tabular.getCellNumber(i, j), val);
4383 case Tabular::UNSET_LTFIRSTHEAD:
4385 case Tabular::SET_LTFIRSTHEAD:
4386 tabular.getRowOfLTFirstHead(row, ltt);
4387 checkLongtableSpecial(ltt, value, flag);
4388 tabular.setLTHead(row, flag, ltt, true);
4391 case Tabular::UNSET_LTHEAD:
4393 case Tabular::SET_LTHEAD:
4394 tabular.getRowOfLTHead(row, ltt);
4395 checkLongtableSpecial(ltt, value, flag);
4396 tabular.setLTHead(row, flag, ltt, false);
4399 case Tabular::UNSET_LTFOOT:
4401 case Tabular::SET_LTFOOT:
4402 tabular.getRowOfLTFoot(row, ltt);
4403 checkLongtableSpecial(ltt, value, flag);
4404 tabular.setLTFoot(row, flag, ltt, false);
4407 case Tabular::UNSET_LTLASTFOOT:
4409 case Tabular::SET_LTLASTFOOT:
4410 tabular.getRowOfLTLastFoot(row, ltt);
4411 checkLongtableSpecial(ltt, value, flag);
4412 tabular.setLTFoot(row, flag, ltt, true);
4415 case Tabular::SET_LTNEWPAGE:
4416 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4419 case Tabular::SET_BOOKTABS:
4420 tabular.setBookTabs(true);
4423 case Tabular::UNSET_BOOKTABS:
4424 tabular.setBookTabs(false);
4427 case Tabular::SET_TOP_SPACE: {
4429 if (value == "default")
4430 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4431 tabular.row_info[i].top_space_default = true;
4432 else if (isValidLength(value, &len))
4433 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4434 tabular.row_info[i].top_space_default = false;
4435 tabular.row_info[i].top_space = len;
4438 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4439 tabular.row_info[i].top_space_default = false;
4440 tabular.row_info[i].top_space = len;
4445 case Tabular::SET_BOTTOM_SPACE: {
4447 if (value == "default")
4448 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4449 tabular.row_info[i].bottom_space_default = true;
4450 else if (isValidLength(value, &len))
4451 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4452 tabular.row_info[i].bottom_space_default = false;
4453 tabular.row_info[i].bottom_space = len;
4456 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4457 tabular.row_info[i].bottom_space_default = false;
4458 tabular.row_info[i].bottom_space = len;
4463 case Tabular::SET_INTERLINE_SPACE: {
4465 if (value == "default")
4466 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4467 tabular.row_info[i].interline_space_default = true;
4468 else if (isValidLength(value, &len))
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4470 tabular.row_info[i].interline_space_default = false;
4471 tabular.row_info[i].interline_space = len;
4474 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4475 tabular.row_info[i].interline_space_default = false;
4476 tabular.row_info[i].interline_space = len;
4481 // dummy stuff just to avoid warnings
4482 case Tabular::LAST_ACTION:
4488 bool InsetTabular::showInsetDialog(BufferView * bv) const
4490 InsetTabularMailer(*this).showDialog(bv);
4495 void InsetTabular::openLayoutDialog(BufferView * bv) const
4497 InsetTabularMailer(*this).showDialog(bv);
4501 bool InsetTabular::copySelection(Cursor & cur)
4503 if (!cur.selection())
4508 getSelection(cur, rs, re, cs, ce);
4510 paste_tabular.reset(new Tabular(tabular));
4512 for (row_type i = 0; i < rs; ++i)
4513 paste_tabular->deleteRow(0);
4515 row_type const rows = re - rs + 1;
4516 while (paste_tabular->rows() > rows)
4517 paste_tabular->deleteRow(rows);
4519 paste_tabular->setTopLine(0, true, true);
4520 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4523 for (col_type i = 0; i < cs; ++i)
4524 paste_tabular->deleteColumn(0);
4526 col_type const columns = ce - cs + 1;
4527 while (paste_tabular->columns() > columns)
4528 paste_tabular->deleteColumn(columns);
4530 paste_tabular->setLeftLine(0, true, true);
4531 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4534 odocstringstream os;
4535 OutputParams const runparams(0);
4536 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4537 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4538 cap::copySelection(cur, os.str());
4540 // mark tabular stack dirty
4541 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4542 // when we (hopefully) have a one-for-all paste mechanism.
4543 // This must be called after cap::copySelection.
4544 dirtyTabularStack(true);
4550 bool InsetTabular::pasteClipboard(Cursor & cur)
4554 col_type const actcol = tabular.column_of_cell(cur.idx());
4555 row_type const actrow = tabular.row_of_cell(cur.idx());
4556 for (row_type r1 = 0, r2 = actrow;
4557 r1 < paste_tabular->rows() && r2 < tabular.rows();
4559 for (col_type c1 = 0, c2 = actcol;
4560 c1 < paste_tabular->columns() && c2 < tabular.columns();
4562 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4563 tabular.isPartOfMultiColumn(r2, c2))
4565 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4569 if (tabular.isPartOfMultiColumn(r2, c2)) {
4573 shared_ptr<InsetText> inset(
4574 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4575 tabular.setCellInset(r2, c2, inset);
4576 // FIXME: change tracking (MG)
4577 inset->setChange(Change(cur.buffer().params().trackChanges ?
4578 Change::INSERTED : Change::UNCHANGED));
4586 void InsetTabular::cutSelection(Cursor & cur)
4588 if (!cur.selection())
4593 getSelection(cur, rs, re, cs, ce);
4594 for (row_type i = rs; i <= re; ++i) {
4595 for (col_type j = cs; j <= ce; ++j) {
4596 shared_ptr<InsetText> t
4597 = cell(tabular.getCellNumber(i, j));
4598 if (cur.buffer().params().trackChanges)
4599 // FIXME: Change tracking (MG)
4600 t->setChange(Change(Change::DELETED));
4606 // cursor position might be invalid now
4607 if (cur.pit() > cur.lastpit())
4608 cur.pit() = cur.lastpit();
4609 if (cur.pos() > cur.lastpos())
4610 cur.pos() = cur.lastpos();
4611 cur.clearSelection();
4615 bool InsetTabular::isRightToLeft(Cursor & cur) const
4617 BOOST_ASSERT(cur.depth() > 1);
4618 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4619 pos_type const parentpos = cur[cur.depth() - 2].pos();
4620 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4621 parentpos).language()->rightToLeft();
4625 void InsetTabular::getSelection(Cursor & cur,
4626 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4628 CursorSlice const & beg = cur.selBegin();
4629 CursorSlice const & end = cur.selEnd();
4630 cs = tabular.column_of_cell(beg.idx());
4631 ce = tabular.column_of_cell(end.idx());
4634 cs = tabular.column_of_cell(end.idx());
4636 ce = tabular.right_column_of_cell(end.idx());
4639 rs = tabular.row_of_cell(beg.idx());
4640 re = tabular.row_of_cell(end.idx());
4646 Text * InsetTabular::getText(int idx) const
4648 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4652 void InsetTabular::setChange(Change const & change)
4654 for (idx_type idx = 0; idx < nargs(); ++idx)
4655 cell(idx)->setChange(change);
4659 void InsetTabular::acceptChanges(BufferParams const & bparams)
4661 for (idx_type idx = 0; idx < nargs(); ++idx)
4662 cell(idx)->acceptChanges(bparams);
4666 void InsetTabular::rejectChanges(BufferParams const & bparams)
4668 for (idx_type idx = 0; idx < nargs(); ++idx)
4669 cell(idx)->rejectChanges(bparams);
4673 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4675 return tabular.getPWidth(cell).zero();
4679 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4682 if (buf.length() <= 0)
4685 Buffer const & buffer = *bv.buffer();
4689 col_type maxCols = 1;
4690 docstring::size_type const len = buf.length();
4691 docstring::size_type p = 0;
4694 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4702 maxCols = max(cols, maxCols);
4708 maxCols = max(cols, maxCols);
4714 paste_tabular.reset(
4715 new Tabular(buffer.params(), rows, maxCols));
4716 loctab = paste_tabular.get();
4718 dirtyTabularStack(true);
4721 cell = bv.cursor().idx();
4722 ocol = tabular.column_of_cell(cell);
4723 row = tabular.row_of_cell(cell);
4726 docstring::size_type op = 0;
4727 idx_type const cells = loctab->getNumberOfCells();
4730 rows = loctab->rows();
4731 col_type const columns = loctab->columns();
4733 while (cell < cells && p < len && row < rows &&
4734 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4740 // we can only set this if we are not too far right
4741 if (cols < columns) {
4742 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4743 Paragraph & par = inset->text_.getPar(0);
4744 Font const font = inset->text_.getFont(buffer, par, 0);
4745 inset->setText(buf.substr(op, p - op), font,
4746 buffer.params().trackChanges);
4752 // we can only set this if we are not too far right
4753 if (cols < columns) {
4754 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4755 Paragraph & par = inset->text_.getPar(0);
4756 Font const font = inset->text_.getFont(buffer, par, 0);
4757 inset->setText(buf.substr(op, p - op), font,
4758 buffer.params().trackChanges);
4763 cell = loctab->getCellNumber(row, cols);
4769 // check for the last cell if there is no trailing '\n'
4770 if (cell < cells && op < len) {
4771 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4772 Paragraph & par = inset->text_.getPar(0);
4773 Font const font = inset->text_.getFont(buffer, par, 0);
4774 inset->setText(buf.substr(op, len - op), font,
4775 buffer.params().trackChanges);
4781 void InsetTabular::addPreview(PreviewLoader & loader) const
4783 row_type const rows = tabular.rows();
4784 col_type const columns = tabular.columns();
4785 for (row_type i = 0; i < rows; ++i) {
4786 for (col_type j = 0; j < columns; ++j)
4787 tabular.getCellInset(i, j)->addPreview(loader);
4792 bool InsetTabular::tablemode(Cursor & cur) const
4794 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4801 string const InsetTabularMailer::name_("tabular");
4803 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4804 : inset_(const_cast<InsetTabular &>(inset))
4808 string const InsetTabularMailer::inset2string(Buffer const &) const
4810 return params2string(inset_);
4814 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4816 istringstream data(in);
4818 lex.setStream(data);
4825 if (!lex || token != name_)
4826 return print_mailer_error("InsetTabularMailer", in, 1,
4829 // This is part of the inset proper that is usually swallowed
4830 // by Buffer::readInset
4832 if (!lex || token != "Tabular")
4833 return print_mailer_error("InsetTabularMailer", in, 2,
4836 Buffer const & buffer = inset.buffer();
4837 inset.read(buffer, lex);
4841 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4844 data << name_ << ' ';
4845 inset.write(inset.buffer(), data);
4846 data << "\\end_inset\n";