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 // Care about multicolumn cells
726 if (column + 1 < columns_ &&
727 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
728 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
729 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
731 cell_info[i].erase(cell_info[i].begin() + column);
738 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
742 column_info.insert(column_info.begin() + column, column_info[column]);
744 for (row_type i = 0; i < rows_; ++i)
745 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
748 for (row_type i = 0; i < rows_; ++i)
749 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
754 void Tabular::set_row_column_number_info()
757 // Count only non-multicol cells plus begin multicol
758 // cells, ignore non-begin multicol cells:
759 for (row_type row = 0; row < rows_; ++row) {
760 for (col_type column = 0; column < columns_; ++column) {
761 if (cell_info[row][column].multicolumn
762 != Tabular::CELL_PART_OF_MULTICOLUMN)
767 rowofcell.resize(numberofcells);
768 columnofcell.resize(numberofcells);
773 c < numberofcells && row < rows_ && column < columns_;) {
775 columnofcell[c] = column;
779 } while (column < columns_ &&
780 cell_info[row][column].multicolumn
781 == Tabular::CELL_PART_OF_MULTICOLUMN);
783 if (column == columns_) {
789 for (row_type row = 0; row < rows_; ++row) {
790 for (col_type column = 0; column < columns_; ++column) {
791 if (isPartOfMultiColumn(row,column))
793 cell_info[row][column].inset->setAutoBreakRows(
794 !getPWidth(getCellNumber(row, column)).zero());
800 Tabular::idx_type Tabular::getNumberOfCells() const
802 return numberofcells;
806 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
808 row_type const row = row_of_cell(cell);
810 for (col_type i = 0; i < columns_; ++i)
811 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
817 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
819 if (!wholerow && isMultiColumn(cell) &&
820 !(use_booktabs && row_of_cell(cell) == 0))
821 return cellinfo_of_cell(cell).top_line;
822 return row_info[row_of_cell(cell)].top_line;
826 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
828 if (!wholerow && isMultiColumn(cell) &&
829 !(use_booktabs && isLastRow(cell)))
830 return cellinfo_of_cell(cell).bottom_line;
831 return row_info[row_of_cell(cell)].bottom_line;
835 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
839 if (!wholecolumn && isMultiColumn(cell) &&
840 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
841 return cellinfo_of_cell(cell).left_line;
842 return column_info[column_of_cell(cell)].left_line;
846 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
850 if (!wholecolumn && isMultiColumn(cell) &&
851 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
852 return cellinfo_of_cell(cell).right_line;
853 return column_info[right_column_of_cell(cell)].right_line;
857 bool Tabular::topAlreadyDrawn(idx_type cell) const
859 row_type row = row_of_cell(cell);
860 if (row > 0 && !getAdditionalHeight(row)) {
861 col_type column = column_of_cell(cell);
864 && cell_info[row][column].multicolumn
865 == Tabular::CELL_PART_OF_MULTICOLUMN)
867 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
868 return row_info[row].bottom_line;
870 return cell_info[row][column].bottom_line;
876 bool Tabular::leftAlreadyDrawn(idx_type cell) const
878 col_type column = column_of_cell(cell);
880 row_type row = row_of_cell(cell);
882 (cell_info[row][column].multicolumn ==
883 Tabular::CELL_PART_OF_MULTICOLUMN));
884 if (getAdditionalWidth(cell_info[row][column].cellno))
886 return rightLine(cell_info[row][column].cellno);
892 bool Tabular::isLastRow(idx_type cell) const
894 return row_of_cell(cell) == rows_ - 1;
898 int Tabular::getAdditionalHeight(row_type row) const
900 if (!row || row >= rows_)
906 for (col_type column = 0; column < columns_ && bottom; ++column) {
907 switch (cell_info[row - 1][column].multicolumn) {
908 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
909 bottom = cell_info[row - 1][column].bottom_line;
911 case Tabular::CELL_NORMAL:
912 bottom = row_info[row - 1].bottom_line;
915 for (col_type column = 0; column < columns_ && top; ++column) {
916 switch (cell_info[row][column].multicolumn) {
917 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
918 top = cell_info[row][column].top_line;
920 case Tabular::CELL_NORMAL:
921 top = row_info[row].top_line;
924 int const interline_space = row_info[row - 1].interline_space_default ?
926 row_info[row - 1].interline_space.inPixels(width_of_tabular);
928 return interline_space + WIDTH_OF_LINE;
929 return interline_space;
933 int Tabular::getAdditionalWidth(idx_type cell) const
935 // internally already set in setWidthOfCell
936 // used to get it back in text.cpp
937 col_type const col = right_column_of_cell(cell);
938 row_type const row = row_of_cell(cell);
939 if (col < columns_ - 1 && rightLine(cell) &&
940 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
942 return WIDTH_OF_LINE;
948 // returns the maximum over all rows
949 int Tabular::getWidthOfColumn(idx_type cell) const
951 col_type const column1 = column_of_cell(cell);
952 col_type const column2 = right_column_of_cell(cell);
954 for (col_type i = column1; i <= column2; ++i)
955 result += column_info[i].width_of_column;
960 int Tabular::getWidthOfTabular() const
962 return width_of_tabular;
966 // returns true if a complete update is necessary, otherwise false
967 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
969 if (!isMultiColumn(cell))
972 row_type const row = row_of_cell(cell);
973 col_type const column1 = column_of_cell(cell);
974 col_type const column2 = right_column_of_cell(cell);
975 int const old_val = cell_info[row][column2].width_of_cell;
977 // first set columns to 0 so we can calculate the right width
978 for (col_type i = column1; i <= column2; ++i) {
979 cell_info[row][i].width_of_cell = 0;
981 // set the width to MAX_WIDTH until width > 0
982 int width = new_width + 2 * WIDTH_OF_LINE;
983 col_type i = column1;
984 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
985 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
986 width -= column_info[i].width_of_column;
989 cell_info[row][i].width_of_cell = width;
991 if (old_val != cell_info[row][column2].width_of_cell) {
992 // in this case we have to recalculate all multicolumn cells which
993 // have this column as one of theirs but not as last one
994 calculate_width_of_column_NMC(i);
995 recalculateMulticolumnsOfColumn(i);
996 calculate_width_of_column(i);
1002 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1004 // the last column does not have to be recalculated because all
1005 // multicolumns will have here there last multicolumn cell which
1006 // always will have the whole rest of the width of the cell.
1007 if (columns_ < 2 || column > (columns_ - 2))
1009 for (row_type row = 0; row < rows_; ++row) {
1010 int mc = cell_info[row][column].multicolumn;
1011 int nmc = cell_info[row][column+1].multicolumn;
1012 // we only have to update multicolumns which do not have this
1013 // column as their last column!
1014 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1015 (mc == CELL_PART_OF_MULTICOLUMN &&
1016 nmc == CELL_PART_OF_MULTICOLUMN))
1018 idx_type const cellno = cell_info[row][column].cellno;
1019 setWidthOfMulticolCell(cellno,
1020 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1026 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1028 row_type const row = row_of_cell(cell);
1029 col_type const column1 = column_of_cell(cell);
1034 if (rightLine(cell_info[row][column1].cellno, true) &&
1035 column1 < columns_ - 1 &&
1036 leftLine(cell_info[row][column1+1].cellno, true))
1038 add_width = WIDTH_OF_LINE;
1041 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1044 if (isMultiColumnReal(cell)) {
1045 tmp = setWidthOfMulticolCell(cell, new_width);
1047 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1048 cell_info[row][column1].width_of_cell = width;
1049 tmp = calculate_width_of_column_NMC(column1);
1051 recalculateMulticolumnsOfColumn(column1);
1054 for (col_type i = 0; i < columns_; ++i)
1055 calculate_width_of_column(i);
1056 calculate_width_of_tabular();
1061 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1064 if (!isMultiColumn(cell) || onlycolumn)
1065 column_info[column_of_cell(cell)].alignment = align;
1067 cellinfo_of_cell(cell).alignment = align;
1071 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1074 if (!isMultiColumn(cell) || onlycolumn)
1075 column_info[column_of_cell(cell)].valignment = align;
1077 cellinfo_of_cell(cell).valignment = align;
1084 * Allow line and paragraph breaks for fixed width cells or disallow them,
1085 * merge cell paragraphs and reset layout to standard for variable width
1088 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1090 inset->setAutoBreakRows(fixedWidth);
1094 // merge all paragraphs to one
1095 BufferParams const & bp = cur.bv().buffer()->params();
1096 while (inset->paragraphs().size() > 1)
1097 mergeParagraph(bp, inset->paragraphs(), 0);
1101 // undo information has already been recorded
1102 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1103 bp.getTextClass().defaultLayoutName());
1110 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1111 Length const & width)
1113 col_type const j = column_of_cell(cell);
1115 column_info[j].p_width = width;
1116 for (row_type i = 0; i < rows_; ++i) {
1117 idx_type const cell = getCellNumber(i, j);
1118 // because of multicolumns
1119 toggleFixedWidth(cur, getCellInset(cell).get(),
1120 !getPWidth(cell).zero());
1122 // cur paragraph can become invalid after paragraphs were merged
1123 if (cur.pit() > cur.lastpit())
1124 cur.pit() = cur.lastpit();
1125 // cur position can become invalid after newlines were removed
1126 if (cur.pos() > cur.lastpos())
1127 cur.pos() = cur.lastpos();
1131 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1132 Length const & width)
1134 if (!isMultiColumn(cell))
1137 cellinfo_of_cell(cell).p_width = width;
1138 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1139 // cur paragraph can become invalid after paragraphs were merged
1140 if (cur.pit() > cur.lastpit())
1141 cur.pit() = cur.lastpit();
1142 // cur position can become invalid after newlines were removed
1143 if (cur.pos() > cur.lastpos())
1144 cur.pos() = cur.lastpos();
1149 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1150 Tabular::Feature what)
1152 if (what == SET_SPECIAL_MULTI)
1153 cellinfo_of_cell(cell).align_special = special;
1155 column_info[column_of_cell(cell)].align_special = special;
1159 void Tabular::setAllLines(idx_type cell, bool line)
1161 setTopLine(cell, line);
1162 setBottomLine(cell, line);
1163 setRightLine(cell, line);
1164 setLeftLine(cell, line);
1168 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1170 row_type const row = row_of_cell(cell);
1171 if (wholerow || !isMultiColumn(cell))
1172 row_info[row].top_line = line;
1174 cellinfo_of_cell(cell).top_line = line;
1178 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1180 if (wholerow || !isMultiColumn(cell))
1181 row_info[row_of_cell(cell)].bottom_line = line;
1183 cellinfo_of_cell(cell).bottom_line = line;
1187 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1189 if (wholecolumn || !isMultiColumn(cell))
1190 column_info[column_of_cell(cell)].left_line = line;
1192 cellinfo_of_cell(cell).left_line = line;
1196 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1198 if (wholecolumn || !isMultiColumn(cell))
1199 column_info[right_column_of_cell(cell)].right_line = line;
1201 cellinfo_of_cell(cell).right_line = line;
1205 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1207 if (!onlycolumn && isMultiColumn(cell))
1208 return cellinfo_of_cell(cell).alignment;
1209 return column_info[column_of_cell(cell)].alignment;
1214 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1216 if (!onlycolumn && isMultiColumn(cell))
1217 return cellinfo_of_cell(cell).valignment;
1218 return column_info[column_of_cell(cell)].valignment;
1222 Length const Tabular::getPWidth(idx_type cell) const
1224 if (isMultiColumn(cell))
1225 return cellinfo_of_cell(cell).p_width;
1226 return column_info[column_of_cell(cell)].p_width;
1230 Length const Tabular::getColumnPWidth(idx_type cell) const
1232 return column_info[column_of_cell(cell)].p_width;
1236 Length const Tabular::getMColumnPWidth(idx_type cell) const
1238 if (isMultiColumn(cell))
1239 return cellinfo_of_cell(cell).p_width;
1244 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1246 if (what == SET_SPECIAL_MULTI)
1247 return cellinfo_of_cell(cell).align_special;
1248 return column_info[column_of_cell(cell)].align_special;
1252 int Tabular::getWidthOfCell(idx_type cell) const
1254 row_type const row = row_of_cell(cell);
1255 col_type const column1 = column_of_cell(cell);
1256 col_type const column2 = right_column_of_cell(cell);
1258 for (col_type i = column1; i <= column2; ++i)
1259 result += cell_info[row][i].width_of_cell;
1264 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1268 switch (getAlignment(cell)) {
1269 case LYX_ALIGN_CENTER:
1270 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1272 case LYX_ALIGN_RIGHT:
1273 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1274 // + getAdditionalWidth(cell);
1277 // LYX_ALIGN_LEFT: nothing :-)
1281 // the LaTeX Way :-(
1287 bool Tabular::isFirstCellInRow(idx_type cell) const
1289 return column_of_cell(cell) == 0;
1293 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1295 if (row > rows_ - 1)
1297 return cell_info[row][0].cellno;
1301 bool Tabular::isLastCellInRow(idx_type cell) const
1303 return right_column_of_cell(cell) == columns_ - 1;
1307 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1309 if (row > rows_ - 1)
1311 return cell_info[row][columns_-1].cellno;
1315 void Tabular::calculate_width_of_column(col_type column)
1318 for (row_type i = 0; i < rows_; ++i)
1319 maximum = max(cell_info[i][column].width_of_cell, maximum);
1320 column_info[column].width_of_column = maximum;
1325 // Calculate the columns regarding ONLY the normal cells and if this
1326 // column is inside a multicolumn cell then use it only if its the last
1327 // column of this multicolumn cell as this gives an added width to the
1328 // column, all the rest should be adapted!
1330 bool Tabular::calculate_width_of_column_NMC(col_type column)
1332 int const old_column_width = column_info[column].width_of_column;
1334 for (row_type i = 0; i < rows_; ++i) {
1335 idx_type cell = getCellNumber(i, column);
1336 bool ismulti = isMultiColumnReal(cell);
1337 if ((!ismulti || column == right_column_of_cell(cell)) &&
1338 cell_info[i][column].width_of_cell > max)
1340 max = cell_info[i][column].width_of_cell;
1343 column_info[column].width_of_column = max;
1344 return column_info[column].width_of_column != old_column_width;
1348 void Tabular::calculate_width_of_tabular()
1350 width_of_tabular = 0;
1351 for (col_type i = 0; i < columns_; ++i)
1352 width_of_tabular += column_info[i].width_of_column;
1356 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1358 if (cell >= numberofcells)
1362 return rowofcell[cell];
1366 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1368 if (cell >= numberofcells)
1369 return columns_ - 1;
1372 return columnofcell[cell];
1376 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1378 row_type const row = row_of_cell(cell);
1379 col_type column = column_of_cell(cell);
1380 while (column < columns_ - 1 &&
1381 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1387 void Tabular::write(Buffer const & buf, ostream & os) const
1391 << write_attribute("version", 3)
1392 << write_attribute("rows", rows_)
1393 << write_attribute("columns", columns_)
1395 // global longtable options
1397 << write_attribute("rotate", rotate)
1398 << write_attribute("booktabs", use_booktabs)
1399 << write_attribute("islongtable", is_long_tabular)
1400 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1401 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1402 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1403 << write_attribute("headTopDL", endhead.topDL)
1404 << write_attribute("headBottomDL", endhead.bottomDL)
1405 << write_attribute("footTopDL", endfoot.topDL)
1406 << write_attribute("footBottomDL", endfoot.bottomDL)
1407 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1408 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1409 << write_attribute("lastFootEmpty", endlastfoot.empty)
1411 for (col_type j = 0; j < columns_; ++j) {
1413 << write_attribute("alignment", column_info[j].alignment)
1414 << write_attribute("valignment", column_info[j].valignment)
1415 << write_attribute("leftline", column_info[j].left_line)
1416 << write_attribute("rightline", column_info[j].right_line)
1417 << write_attribute("width", column_info[j].p_width.asString())
1418 << write_attribute("special", column_info[j].align_special)
1421 for (row_type i = 0; i < rows_; ++i) {
1423 << write_attribute("topline", row_info[i].top_line)
1424 << write_attribute("bottomline", row_info[i].bottom_line);
1425 static const string def("default");
1426 if (row_info[i].top_space_default)
1427 os << write_attribute("topspace", def);
1429 os << write_attribute("topspace", row_info[i].top_space);
1430 if (row_info[i].bottom_space_default)
1431 os << write_attribute("bottomspace", def);
1433 os << write_attribute("bottomspace", row_info[i].bottom_space);
1434 if (row_info[i].interline_space_default)
1435 os << write_attribute("interlinespace", def);
1437 os << write_attribute("interlinespace", row_info[i].interline_space);
1438 os << write_attribute("endhead", row_info[i].endhead)
1439 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1440 << write_attribute("endfoot", row_info[i].endfoot)
1441 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1442 << write_attribute("newpage", row_info[i].newpage)
1444 for (col_type j = 0; j < columns_; ++j) {
1446 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1447 << write_attribute("alignment", cell_info[i][j].alignment)
1448 << write_attribute("valignment", cell_info[i][j].valignment)
1449 << write_attribute("topline", cell_info[i][j].top_line)
1450 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1451 << write_attribute("leftline", cell_info[i][j].left_line)
1452 << write_attribute("rightline", cell_info[i][j].right_line)
1453 << write_attribute("rotate", cell_info[i][j].rotate)
1454 << write_attribute("usebox", cell_info[i][j].usebox)
1455 << write_attribute("width", cell_info[i][j].p_width)
1456 << write_attribute("special", cell_info[i][j].align_special)
1458 os << "\\begin_inset ";
1459 cell_info[i][j].inset->write(buf, os);
1460 os << "\n\\end_inset\n"
1465 os << "</lyxtabular>\n";
1469 void Tabular::read(Buffer const & buf, Lexer & lex)
1472 istream & is = lex.getStream();
1474 l_getline(is, line);
1475 if (!prefixIs(line, "<lyxtabular ")
1476 && !prefixIs(line, "<Tabular ")) {
1477 BOOST_ASSERT(false);
1482 if (!getTokenValue(line, "version", version))
1484 BOOST_ASSERT(version >= 2);
1487 if (!getTokenValue(line, "rows", rows_arg))
1490 if (!getTokenValue(line, "columns", columns_arg))
1492 init(buf.params(), rows_arg, columns_arg);
1493 l_getline(is, line);
1494 if (!prefixIs(line, "<features")) {
1495 lyxerr << "Wrong tabular format (expected <features ...> got"
1496 << line << ')' << endl;
1499 getTokenValue(line, "rotate", rotate);
1500 getTokenValue(line, "booktabs", use_booktabs);
1501 getTokenValue(line, "islongtable", is_long_tabular);
1502 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1503 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1504 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1505 getTokenValue(line, "headTopDL", endhead.topDL);
1506 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1507 getTokenValue(line, "footTopDL", endfoot.topDL);
1508 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1509 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1510 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1511 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1513 for (col_type j = 0; j < columns_; ++j) {
1515 if (!prefixIs(line,"<column")) {
1516 lyxerr << "Wrong tabular format (expected <column ...> got"
1517 << line << ')' << endl;
1520 getTokenValue(line, "alignment", column_info[j].alignment);
1521 getTokenValue(line, "valignment", column_info[j].valignment);
1522 getTokenValue(line, "leftline", column_info[j].left_line);
1523 getTokenValue(line, "rightline", column_info[j].right_line);
1524 getTokenValue(line, "width", column_info[j].p_width);
1525 getTokenValue(line, "special", column_info[j].align_special);
1528 for (row_type i = 0; i < rows_; ++i) {
1529 l_getline(is, line);
1530 if (!prefixIs(line, "<row")) {
1531 lyxerr << "Wrong tabular format (expected <row ...> got"
1532 << line << ')' << endl;
1535 getTokenValue(line, "topline", row_info[i].top_line);
1536 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1537 getTokenValue(line, "topspace", row_info[i].top_space,
1538 row_info[i].top_space_default);
1539 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1540 row_info[i].bottom_space_default);
1541 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1542 row_info[i].interline_space_default);
1543 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1544 getTokenValue(line, "endhead", row_info[i].endhead);
1545 getTokenValue(line, "endfoot", row_info[i].endfoot);
1546 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1547 getTokenValue(line, "newpage", row_info[i].newpage);
1548 for (col_type j = 0; j < columns_; ++j) {
1549 l_getline(is, line);
1550 if (!prefixIs(line, "<cell")) {
1551 lyxerr << "Wrong tabular format (expected <cell ...> got"
1552 << line << ')' << endl;
1555 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1556 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1557 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1558 getTokenValue(line, "topline", cell_info[i][j].top_line);
1559 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1560 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1561 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1562 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1563 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1564 getTokenValue(line, "width", cell_info[i][j].p_width);
1565 getTokenValue(line, "special", cell_info[i][j].align_special);
1566 l_getline(is, line);
1567 if (prefixIs(line, "\\begin_inset")) {
1568 cell_info[i][j].inset->read(buf, lex);
1569 l_getline(is, line);
1571 if (!prefixIs(line, "</cell>")) {
1572 lyxerr << "Wrong tabular format (expected </cell> got"
1573 << line << ')' << endl;
1577 l_getline(is, line);
1578 if (!prefixIs(line, "</row>")) {
1579 lyxerr << "Wrong tabular format (expected </row> got"
1580 << line << ')' << endl;
1584 while (!prefixIs(line, "</lyxtabular>")) {
1585 l_getline(is, line);
1587 set_row_column_number_info();
1591 bool Tabular::isMultiColumn(idx_type cell) const
1593 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1597 bool Tabular::isMultiColumnReal(idx_type cell) const
1599 return column_of_cell(cell) != right_column_of_cell(cell) &&
1600 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1604 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1606 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1610 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1613 cellstruct & cs = cellinfo_of_cell(cell);
1614 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1615 cs.alignment = column_info[column_of_cell(cell)].alignment;
1616 cs.top_line = row_info[row_of_cell(cell)].top_line;
1617 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1618 cs.left_line = column_info[column_of_cell(cell)].left_line;
1619 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1620 for (idx_type i = 1; i < number; ++i) {
1621 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1622 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1623 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1626 set_row_column_number_info();
1630 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1632 row_type const row = row_of_cell(cell);
1633 col_type column = column_of_cell(cell);
1634 idx_type result = 1;
1636 while (column < columns_ &&
1637 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1646 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1648 row_type const row = row_of_cell(cell);
1649 col_type column = column_of_cell(cell);
1651 idx_type result = 0;
1653 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1654 cell_info[row][column].multicolumn = CELL_NORMAL;
1656 while (column < columns_ &&
1657 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1659 cell_info[row][column].multicolumn = CELL_NORMAL;
1664 set_row_column_number_info();
1669 void Tabular::setBookTabs(bool what)
1671 use_booktabs = what;
1675 bool Tabular::useBookTabs() const
1677 return use_booktabs;
1681 void Tabular::setLongTabular(bool what)
1683 is_long_tabular = what;
1687 bool Tabular::isLongTabular() const
1689 return is_long_tabular;
1693 void Tabular::setRotateTabular(bool flag)
1699 bool Tabular::getRotateTabular() const
1705 void Tabular::setRotateCell(idx_type cell, bool flag)
1707 cellinfo_of_cell(cell).rotate = flag;
1711 bool Tabular::getRotateCell(idx_type cell) const
1713 return cellinfo_of_cell(cell).rotate;
1717 bool Tabular::needRotating() const
1721 for (row_type i = 0; i < rows_; ++i)
1722 for (col_type j = 0; j < columns_; ++j)
1723 if (cell_info[i][j].rotate)
1729 bool Tabular::isLastCell(idx_type cell) const
1731 if (cell + 1 < numberofcells)
1737 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1739 if (row_of_cell(cell) > 0)
1740 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1745 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1747 if (row_of_cell(cell) + 1 < rows_)
1748 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1753 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1755 if (row_of_cell(cell) == 0)
1757 if (!isMultiColumn(cell))
1758 return getCellAbove(cell);
1759 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1763 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1765 if (row_of_cell(cell) + 1 >= rows_)
1767 if (!isMultiColumn(cell))
1768 return getCellBelow(cell);
1769 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1773 Tabular::idx_type Tabular::getCellNumber(row_type row,
1774 col_type column) const
1776 BOOST_ASSERT(column != npos && column < columns_ &&
1777 row != npos && row < rows_);
1778 return cell_info[row][column].cellno;
1782 void Tabular::setUsebox(idx_type cell, BoxType type)
1784 cellinfo_of_cell(cell).usebox = type;
1788 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1790 if (column_info[column_of_cell(cell)].p_width.zero() &&
1791 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1793 if (cellinfo_of_cell(cell).usebox > 1)
1794 return cellinfo_of_cell(cell).usebox;
1795 return useParbox(cell);
1800 // This are functions used for the longtable support
1802 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1808 row_info[row].endfirsthead = flag;
1812 row_info[row].endhead = flag;
1817 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1820 hd.set = haveLTHead();
1821 return row_info[row].endhead;
1825 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1828 hd.set = haveLTFirstHead();
1829 return row_info[row].endfirsthead;
1833 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1839 row_info[row].endlastfoot = flag;
1843 row_info[row].endfoot = flag;
1848 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1851 fd.set = haveLTFoot();
1852 return row_info[row].endfoot;
1856 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1859 fd.set = haveLTLastFoot();
1860 return row_info[row].endlastfoot;
1864 void Tabular::setLTNewPage(row_type row, bool what)
1866 row_info[row].newpage = what;
1870 bool Tabular::getLTNewPage(row_type row) const
1872 return row_info[row].newpage;
1876 bool Tabular::haveLTHead() const
1878 for (row_type i = 0; i < rows_; ++i)
1879 if (row_info[i].endhead)
1885 bool Tabular::haveLTFirstHead() const
1887 if (endfirsthead.empty)
1889 for (row_type i = 0; i < rows_; ++i)
1890 if (row_info[i].endfirsthead)
1896 bool Tabular::haveLTFoot() const
1898 for (row_type i = 0; i < rows_; ++i)
1899 if (row_info[i].endfoot)
1905 bool Tabular::haveLTLastFoot() const
1907 if (endlastfoot.empty)
1909 for (row_type i = 0; i < rows_; ++i)
1910 if (row_info[i].endlastfoot)
1916 // end longtable support functions
1918 void Tabular::setAscentOfRow(row_type row, int height)
1920 if (row >= rows_ || row_info[row].ascent_of_row == height)
1922 row_info[row].ascent_of_row = height;
1926 void Tabular::setDescentOfRow(row_type row, int height)
1928 if (row >= rows_ || row_info[row].descent_of_row == height)
1930 row_info[row].descent_of_row = height;
1934 int Tabular::getAscentOfRow(row_type row) const
1938 return row_info[row].ascent_of_row;
1942 int Tabular::getDescentOfRow(row_type row) const
1944 BOOST_ASSERT(row < rows_);
1945 return row_info[row].descent_of_row;
1949 int Tabular::getHeightOfTabular() const
1952 for (row_type row = 0; row < rows_; ++row)
1953 height += getAscentOfRow(row) + getDescentOfRow(row) +
1954 getAdditionalHeight(row);
1959 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1961 BOOST_ASSERT(row < rows_);
1962 BOOST_ASSERT(column < columns_);
1963 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1967 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1969 // FIXME: assert or return 0 as in TeXBottomHLine()?
1970 BOOST_ASSERT(row != npos);
1971 BOOST_ASSERT(row < rows_);
1973 idx_type const fcell = getFirstCellInRow(row);
1974 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1977 for (idx_type i = fcell; i < n; ++i) {
1981 if (use_booktabs && row == 0) {
1983 } else if (tmp == n - fcell) {
1984 os << (use_booktabs ? "\\midrule " : "\\hline ");
1986 for (idx_type i = fcell; i < n; ++i) {
1988 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1989 << column_of_cell(i) + 1
1991 << right_column_of_cell(i) + 1
2003 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2005 // FIXME: return 0 or assert as in TeXTopHLine()?
2006 if (row == npos || row >= rows_)
2009 idx_type const fcell = getFirstCellInRow(row);
2010 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2013 for (idx_type i = fcell; i < n; ++i) {
2017 if (use_booktabs && row == rows_ - 1) {
2018 os << "\\bottomrule";
2019 } else if (tmp == n - fcell) {
2020 os << (use_booktabs ? "\\midrule" : "\\hline");
2022 for (idx_type i = fcell; i < n; ++i) {
2023 if (bottomLine(i)) {
2024 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2025 << column_of_cell(i) + 1
2027 << right_column_of_cell(i) + 1
2039 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2043 if (getRotateCell(cell)) {
2044 os << "\\begin{sideways}\n";
2047 if (isMultiColumn(cell)) {
2048 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2049 if (leftLine(cell) &&
2050 (isFirstCellInRow(cell) ||
2051 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2052 !rightLine(cell - 1, true))))
2054 if (!cellinfo_of_cell(cell).align_special.empty()) {
2055 os << cellinfo_of_cell(cell).align_special;
2057 if (!getPWidth(cell).zero()) {
2058 switch (getVAlignment(cell)) {
2059 case LYX_VALIGN_TOP:
2062 case LYX_VALIGN_MIDDLE:
2065 case LYX_VALIGN_BOTTOM:
2070 << from_ascii(getPWidth(cell).asLatexString())
2073 switch (getAlignment(cell)) {
2074 case LYX_ALIGN_LEFT:
2077 case LYX_ALIGN_RIGHT:
2084 } // end if else !getPWidth
2085 } // end if else !cellinfo_of_cell
2086 if (rightLine(cell))
2088 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2093 if (getUsebox(cell) == BOX_PARBOX) {
2095 switch (getVAlignment(cell)) {
2096 case LYX_VALIGN_TOP:
2099 case LYX_VALIGN_MIDDLE:
2102 case LYX_VALIGN_BOTTOM:
2106 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2108 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2109 os << "\\begin{minipage}[";
2110 switch (getVAlignment(cell)) {
2111 case LYX_VALIGN_TOP:
2114 case LYX_VALIGN_MIDDLE:
2117 case LYX_VALIGN_BOTTOM:
2121 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2129 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2134 if (getUsebox(cell) == BOX_PARBOX)
2136 else if (getUsebox(cell) == BOX_MINIPAGE) {
2137 os << "%\n\\end{minipage}";
2140 if (isMultiColumn(cell)) {
2143 if (getRotateCell(cell)) {
2144 os << "%\n\\end{sideways}";
2151 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2152 OutputParams const & runparams) const
2154 if (!is_long_tabular)
2158 // output header info
2160 if (endhead.topDL) {
2164 for (row_type i = 0; i < rows_; ++i) {
2165 if (row_info[i].endhead) {
2166 ret += TeXRow(os, i, buf, runparams);
2169 if (endhead.bottomDL) {
2173 os << "\\endhead\n";
2175 if (endfirsthead.empty) {
2176 os << "\\endfirsthead\n";
2180 // output firstheader info
2181 if (haveLTFirstHead()) {
2182 if (endfirsthead.topDL) {
2186 for (row_type i = 0; i < rows_; ++i) {
2187 if (row_info[i].endfirsthead) {
2188 ret += TeXRow(os, i, buf, runparams);
2191 if (endfirsthead.bottomDL) {
2195 os << "\\endfirsthead\n";
2198 // output footer info
2200 if (endfoot.topDL) {
2204 for (row_type i = 0; i < rows_; ++i) {
2205 if (row_info[i].endfoot) {
2206 ret += TeXRow(os, i, buf, runparams);
2209 if (endfoot.bottomDL) {
2213 os << "\\endfoot\n";
2215 if (endlastfoot.empty) {
2216 os << "\\endlastfoot\n";
2220 // output lastfooter info
2221 if (haveLTLastFoot()) {
2222 if (endlastfoot.topDL) {
2226 for (row_type i = 0; i < rows_; ++i) {
2227 if (row_info[i].endlastfoot) {
2228 ret += TeXRow(os, i, buf, runparams);
2231 if (endlastfoot.bottomDL) {
2235 os << "\\endlastfoot\n";
2242 bool Tabular::isValidRow(row_type row) const
2244 if (!is_long_tabular)
2246 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2247 !row_info[row].endfoot && !row_info[row].endlastfoot;
2251 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2252 OutputParams const & runparams) const
2254 idx_type cell = getCellNumber(i, 0);
2255 int ret = TeXTopHLine(os, i);
2256 if (row_info[i].top_space_default) {
2258 os << "\\addlinespace\n";
2260 os << "\\noalign{\\vskip\\doublerulesep}\n";
2262 } else if(!row_info[i].top_space.zero()) {
2264 os << "\\addlinespace["
2265 << from_ascii(row_info[i].top_space.asLatexString())
2268 os << "\\noalign{\\vskip"
2269 << from_ascii(row_info[i].top_space.asLatexString())
2275 for (col_type j = 0; j < columns_; ++j) {
2276 if (isPartOfMultiColumn(i, j))
2278 ret += TeXCellPreamble(os, cell);
2279 shared_ptr<InsetText> inset = getCellInset(cell);
2281 Paragraph const & par = inset->paragraphs().front();
2282 bool rtl = par.isRightToLeftPar(buf.params())
2284 && getPWidth(cell).zero();
2287 if (par.getParLanguage(buf.params())->lang() ==
2290 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2292 // currently, remaning RTL languages are arabic_arabtex and hebrew
2296 ret += inset->latex(buf, os, runparams);
2300 ret += TeXCellPostamble(os, cell);
2301 if (!isLastCellInRow(cell)) { // not last cell in row
2306 os << "\\tabularnewline";
2307 if (row_info[i].bottom_space_default) {
2309 os << "\\addlinespace";
2311 os << "[\\doublerulesep]";
2312 } else if (!row_info[i].bottom_space.zero()) {
2314 os << "\\addlinespace";
2316 << from_ascii(row_info[i].bottom_space.asLatexString())
2321 ret += TeXBottomHLine(os, i);
2322 if (row_info[i].interline_space_default) {
2324 os << "\\addlinespace\n";
2326 os << "\\noalign{\\vskip\\doublerulesep}\n";
2328 } else if (!row_info[i].interline_space.zero()) {
2330 os << "\\addlinespace["
2331 << from_ascii(row_info[i].interline_space.asLatexString())
2334 os << "\\noalign{\\vskip"
2335 << from_ascii(row_info[i].interline_space.asLatexString())
2343 int Tabular::latex(Buffer const & buf, odocstream & os,
2344 OutputParams const & runparams) const
2348 //+---------------------------------------------------------------------
2349 //+ first the opening preamble +
2350 //+---------------------------------------------------------------------
2353 os << "\\begin{sideways}\n";
2356 if (is_long_tabular)
2357 os << "\\begin{longtable}{";
2359 os << "\\begin{tabular}{";
2360 for (col_type i = 0; i < columns_; ++i) {
2361 if (!use_booktabs && column_info[i].left_line)
2363 if (!column_info[i].align_special.empty()) {
2364 os << column_info[i].align_special;
2366 if (!column_info[i].p_width.zero()) {
2367 switch (column_info[i].alignment) {
2368 case LYX_ALIGN_LEFT:
2369 os << ">{\\raggedright}";
2371 case LYX_ALIGN_RIGHT:
2372 os << ">{\\raggedleft}";
2374 case LYX_ALIGN_CENTER:
2375 os << ">{\\centering}";
2377 case LYX_ALIGN_NONE:
2378 case LYX_ALIGN_BLOCK:
2379 case LYX_ALIGN_LAYOUT:
2380 case LYX_ALIGN_SPECIAL:
2384 switch (column_info[i].valignment) {
2385 case LYX_VALIGN_TOP:
2388 case LYX_VALIGN_MIDDLE:
2391 case LYX_VALIGN_BOTTOM:
2396 << from_ascii(column_info[i].p_width.asLatexString())
2399 switch (column_info[i].alignment) {
2400 case LYX_ALIGN_LEFT:
2403 case LYX_ALIGN_RIGHT:
2410 } // end if else !column_info[i].p_width
2411 } // end if else !column_info[i].align_special
2412 if (!use_booktabs && column_info[i].right_line)
2418 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2420 //+---------------------------------------------------------------------
2421 //+ the single row and columns (cells) +
2422 //+---------------------------------------------------------------------
2424 for (row_type i = 0; i < rows_; ++i) {
2425 if (isValidRow(i)) {
2426 ret += TeXRow(os, i, buf, runparams);
2427 if (is_long_tabular && row_info[i].newpage) {
2428 os << "\\newpage\n";
2434 //+---------------------------------------------------------------------
2435 //+ the closing of the tabular +
2436 //+---------------------------------------------------------------------
2438 if (is_long_tabular)
2439 os << "\\end{longtable}";
2441 os << "\\end{tabular}";
2443 os << "\n\\end{sideways}";
2451 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2452 OutputParams const & runparams) const
2455 idx_type cell = getFirstCellInRow(row);
2458 for (col_type j = 0; j < columns_; ++j) {
2459 if (isPartOfMultiColumn(row, j))
2462 os << "<entry align=\"";
2463 switch (getAlignment(cell)) {
2464 case LYX_ALIGN_LEFT:
2467 case LYX_ALIGN_RIGHT:
2475 os << "\" valign=\"";
2476 switch (getVAlignment(cell)) {
2477 case LYX_VALIGN_TOP:
2480 case LYX_VALIGN_BOTTOM:
2483 case LYX_VALIGN_MIDDLE:
2488 if (isMultiColumn(cell)) {
2489 os << " namest=\"col" << j << "\" ";
2490 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2494 ret += getCellInset(cell)->docbook(buf, os, runparams);
2503 int Tabular::docbook(Buffer const & buf, odocstream & os,
2504 OutputParams const & runparams) const
2508 //+---------------------------------------------------------------------
2509 //+ first the opening preamble +
2510 //+---------------------------------------------------------------------
2512 os << "<tgroup cols=\"" << columns_
2513 << "\" colsep=\"1\" rowsep=\"1\">\n";
2515 for (col_type i = 0; i < columns_; ++i) {
2516 os << "<colspec colname=\"col" << i << "\" align=\"";
2517 switch (column_info[i].alignment) {
2518 case LYX_ALIGN_LEFT:
2521 case LYX_ALIGN_RIGHT:
2529 if (runparams.flavor == OutputParams::XML)
2535 //+---------------------------------------------------------------------
2536 //+ Long Tabular case +
2537 //+---------------------------------------------------------------------
2539 // output header info
2540 if (haveLTHead() || haveLTFirstHead()) {
2543 for (row_type i = 0; i < rows_; ++i) {
2544 if (row_info[i].endhead || row_info[i].endfirsthead) {
2545 ret += docbookRow(buf, os, i, runparams);
2551 // output footer info
2552 if (haveLTFoot() || haveLTLastFoot()) {
2555 for (row_type i = 0; i < rows_; ++i) {
2556 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2557 ret += docbookRow(buf, os, i, runparams);
2564 //+---------------------------------------------------------------------
2565 //+ the single row and columns (cells) +
2566 //+---------------------------------------------------------------------
2570 for (row_type i = 0; i < rows_; ++i) {
2571 if (isValidRow(i)) {
2572 ret += docbookRow(buf, os, i, runparams);
2577 //+---------------------------------------------------------------------
2578 //+ the closing of the tabular +
2579 //+---------------------------------------------------------------------
2588 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2589 vector<unsigned int> const & clen) const
2591 idx_type const fcell = getFirstCellInRow(row);
2592 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2595 for (idx_type i = fcell; i < n; ++i) {
2605 for (idx_type i = fcell; i < n; ++i) {
2616 col_type column = column_of_cell(i);
2617 int len = clen[column];
2618 while (column < columns_ - 1
2619 && isPartOfMultiColumn(row, ++column))
2620 len += clen[column] + 4;
2621 os << docstring(len, ch);
2636 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2637 vector<unsigned int> const & clen) const
2639 idx_type const fcell = getFirstCellInRow(row);
2640 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2643 for (idx_type i = fcell; i < n; ++i) {
2644 if (bottomLine(i)) {
2653 for (idx_type i = fcell; i < n; ++i) {
2654 if (bottomLine(i)) {
2664 col_type column = column_of_cell(i);
2665 int len = clen[column];
2666 while (column < columns_ -1
2667 && isPartOfMultiColumn(row, ++column))
2668 len += clen[column] + 4;
2669 os << docstring(len, ch);
2670 if (bottomLine(i)) {
2684 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2685 OutputParams const & runparams,
2686 idx_type cell, row_type row, col_type column,
2687 vector<unsigned int> const & clen,
2688 bool onlydata) const
2690 odocstringstream sstr;
2691 getCellInset(cell)->plaintext(buf, sstr, runparams);
2703 unsigned int len1 = sstr.str().length();
2704 unsigned int len2 = clen[column];
2705 while (column < columns_ -1
2706 && isPartOfMultiColumn(row, ++column))
2707 len2 += clen[column] + 4;
2710 switch (getAlignment(cell)) {
2712 case LYX_ALIGN_LEFT:
2715 case LYX_ALIGN_RIGHT:
2719 case LYX_ALIGN_CENTER:
2725 os << docstring(len1, ' ') << sstr.str()
2726 << docstring(len2, ' ');
2728 if (rightLine(cell))
2735 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2736 OutputParams const & runparams, int const depth,
2737 bool onlydata, unsigned char delim) const
2739 // first calculate the width of the single columns
2740 vector<unsigned int> clen(columns_);
2743 // first all non (real) multicolumn cells!
2744 for (col_type j = 0; j < columns_; ++j) {
2746 for (row_type i = 0; i < rows_; ++i) {
2747 idx_type cell = getCellNumber(i, j);
2748 if (isMultiColumnReal(cell))
2750 odocstringstream sstr;
2751 getCellInset(cell)->plaintext(buf, sstr, runparams);
2752 if (clen[j] < sstr.str().length())
2753 clen[j] = sstr.str().length();
2756 // then all (real) multicolumn cells!
2757 for (col_type j = 0; j < columns_; ++j) {
2758 for (row_type i = 0; i < rows_; ++i) {
2759 idx_type cell = getCellNumber(i, j);
2760 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2762 odocstringstream sstr;
2763 getCellInset(cell)->plaintext(buf, sstr, runparams);
2764 int len = int(sstr.str().length());
2765 idx_type const n = cells_in_multicolumn(cell);
2766 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2768 if (len > int(clen[j + n - 1]))
2769 clen[j + n - 1] = len;
2774 for (row_type i = 0; i < rows_; ++i) {
2775 if (!onlydata && plaintextTopHLine(os, i, clen))
2776 os << docstring(depth * 2, ' ');
2777 for (col_type j = 0; j < columns_; ++j) {
2778 if (isPartOfMultiColumn(i, j))
2780 if (onlydata && j > 0)
2782 plaintextPrintCell(buf, os, runparams,
2783 cell, i, j, clen, onlydata);
2788 os << docstring(depth * 2, ' ');
2789 if (plaintextBottomHLine(os, i, clen))
2790 os << docstring(depth * 2, ' ');
2796 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2798 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2802 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2803 col_type column) const
2805 return cell_info[row][column].inset;
2809 void Tabular::setCellInset(row_type row, col_type column,
2810 shared_ptr<InsetText> ins) const
2812 cell_info[row][column].inset = ins;
2817 Tabular::getCellFromInset(Inset const * inset) const
2819 // is this inset part of the tabular?
2821 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2822 BOOST_ASSERT(false);
2825 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2826 if (getCellInset(cell).get() == inset) {
2827 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2828 << "cell=" << cell << endl;
2832 // We should have found a cell at this point
2833 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2834 << inset << " not found!" << endl;
2835 BOOST_ASSERT(false);
2841 void Tabular::validate(LaTeXFeatures & features) const
2843 features.require("NeedTabularnewline");
2845 features.require("booktabs");
2846 if (isLongTabular())
2847 features.require("longtable");
2849 features.require("rotating");
2850 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2851 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2852 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2853 features.require("array");
2854 getCellInset(cell)->validate(features);
2859 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2861 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2862 ParagraphList::const_iterator cit = parlist.begin();
2863 ParagraphList::const_iterator end = parlist.end();
2865 for (; cit != end; ++cit)
2866 for (int i = 0; i < cit->size(); ++i)
2867 if (cit->isNewline(i))
2874 /////////////////////////////////////////////////////////////////////
2878 /////////////////////////////////////////////////////////////////////
2880 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2882 : tabular(buf.params(), max(rows, row_type(1)),
2883 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2887 InsetTabular::InsetTabular(InsetTabular const & tab)
2888 : Inset(tab), tabular(tab.tabular),
2889 buffer_(tab.buffer_), scx_(0)
2893 InsetTabular::~InsetTabular()
2895 InsetTabularMailer(*this).hideDialog();
2899 auto_ptr<Inset> InsetTabular::doClone() const
2901 return auto_ptr<Inset>(new InsetTabular(*this));
2905 Buffer const & InsetTabular::buffer() const
2911 void InsetTabular::buffer(Buffer const * b)
2917 void InsetTabular::write(Buffer const & buf, ostream & os) const
2919 os << "Tabular" << endl;
2920 tabular.write(buf, os);
2924 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2926 bool const old_format = (lex.getString() == "\\LyXTable");
2928 tabular.read(buf, lex);
2934 string token = lex.getString();
2935 while (lex && token != "\\end_inset") {
2937 token = lex.getString();
2940 lex.printError("Missing \\end_inset at this point. "
2946 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2948 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2949 // mi.base.textwidth << "\n";
2951 lyxerr << "InsetTabular::metrics: need bv" << endl;
2952 BOOST_ASSERT(false);
2956 for (idx_type cell = 0; i < tabular.rows(); ++i) {
2959 for (col_type j = 0; j < tabular.columns(); ++j) {
2960 if (tabular.isPartOfMultiColumn(i, j))
2961 // Multicolumn cell, but not first one
2966 if (tabular.cell_info[i][j].multicolumn ==
2967 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2968 p_width = tabular.cellinfo_of_cell(cell).p_width;
2970 p_width = tabular.column_info[j].p_width;
2971 if (!p_width.zero())
2972 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2973 tabular.getCellInset(cell)->metrics(m, dim);
2974 if (!p_width.zero())
2975 dim.wid = m.base.textwidth;
2976 tabular.setWidthOfCell(cell, dim.wid);
2977 if (p_width.zero()) {
2978 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2979 // FIXME there must be a way to get rid of
2980 // the second metrics call
2981 tabular.getCellInset(cell)->metrics(m, dim);
2983 maxAsc = max(maxAsc, dim.asc);
2984 maxDesc = max(maxDesc, dim.des);
2987 int const top_space = tabular.row_info[i].top_space_default ?
2988 default_line_space :
2989 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2990 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
2991 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2992 default_line_space :
2993 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2994 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2997 dim.asc = tabular.getAscentOfRow(0);
2998 dim.des = tabular.getHeightOfTabular() - dim.asc;
2999 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
3000 bool const changed = dim_ != dim;
3006 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3008 setPosCache(pi, x, y);
3010 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3011 BufferView * bv = pi.base.bv;
3013 resetPos(bv->cursor());
3016 x += ADD_TO_TABULAR_WIDTH;
3019 first_visible_cell = Tabular::npos;
3020 for (row_type i = 0; i < tabular.rows(); ++i) {
3022 int const a = tabular.getAscentOfRow(i);
3023 int const d = tabular.getDescentOfRow(i);
3024 idx = tabular.getCellNumber(i, 0);
3025 for (col_type j = 0; j < tabular.columns(); ++j) {
3026 if (tabular.isPartOfMultiColumn(i, j))
3028 if (first_visible_cell == Tabular::npos)
3029 first_visible_cell = idx;
3031 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3032 if (nx + tabular.getWidthOfColumn(idx) < 0
3033 || nx > bv->workWidth()
3035 || y - a > bv->workHeight()) {
3036 pi.pain.setDrawingEnabled(false);
3037 cell(idx)->draw(pi, cx, y);
3038 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3039 pi.pain.setDrawingEnabled(true);
3041 cell(idx)->draw(pi, cx, y);
3042 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3044 nx += tabular.getWidthOfColumn(idx);
3048 if (i + 1 < tabular.rows())
3049 y += d + tabular.getAscentOfRow(i + 1) +
3050 tabular.getAdditionalHeight(i + 1);
3055 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3057 setPosCache(pi, x, y);
3059 Cursor & cur = pi.base.bv->cursor();
3061 x += scx_ + ADD_TO_TABULAR_WIDTH;
3063 // Paint background of current tabular
3064 int const w = tabular.getWidthOfTabular();
3065 int const h = tabular.getHeightOfTabular();
3066 int yy = y - tabular.getAscentOfRow(0);
3067 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3069 if (!cur.selection())
3071 if (!ptr_cmp(&cur.inset(), this))
3077 if (tablemode(cur)) {
3080 getSelection(cur, rs, re, cs, ce);
3081 y -= tabular.getAscentOfRow(0);
3082 for (row_type j = 0; j < tabular.rows(); ++j) {
3083 int const a = tabular.getAscentOfRow(j);
3084 int const h = a + tabular.getDescentOfRow(j);
3086 y += tabular.getAdditionalHeight(j);
3087 for (col_type i = 0; i < tabular.columns(); ++i) {
3088 if (tabular.isPartOfMultiColumn(j, i))
3090 idx_type const cell =
3091 tabular.getCellNumber(j, i);
3092 int const w = tabular.getWidthOfColumn(cell);
3093 if (i >= cs && i <= ce && j >= rs && j <= re)
3094 pi.pain.fillRectangle(xx, y, w, h,
3102 x += getCellXPos(cur.idx());
3103 x += tabular.getBeginningOfTextInCell(cur.idx());
3104 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3109 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3110 row_type row, idx_type cell, bool erased) const
3112 int x2 = x + tabular.getWidthOfColumn(cell);
3113 bool on_off = false;
3114 Color::color col = Color::tabularline;
3115 Color::color onoffcol = Color::tabularonoffline;
3118 col = Color::deletedtext;
3119 onoffcol = Color::deletedtext;
3122 if (!tabular.topAlreadyDrawn(cell)) {
3123 on_off = !tabular.topLine(cell);
3124 pain.line(x, y - tabular.getAscentOfRow(row),
3125 x2, y - tabular.getAscentOfRow(row),
3126 on_off ? onoffcol : col,
3127 on_off ? Painter::line_onoffdash : Painter::line_solid);
3129 on_off = !tabular.bottomLine(cell);
3130 pain.line(x, y + tabular.getDescentOfRow(row),
3131 x2, y + tabular.getDescentOfRow(row),
3132 on_off ? onoffcol : col,
3133 on_off ? Painter::line_onoffdash : Painter::line_solid);
3134 if (!tabular.leftAlreadyDrawn(cell)) {
3135 on_off = !tabular.leftLine(cell);
3136 pain.line(x, y - tabular.getAscentOfRow(row),
3137 x, y + tabular.getDescentOfRow(row),
3138 on_off ? onoffcol : col,
3139 on_off ? Painter::line_onoffdash : Painter::line_solid);
3141 on_off = !tabular.rightLine(cell);
3142 pain.line(x2 - tabular.getAdditionalWidth(cell),
3143 y - tabular.getAscentOfRow(row),
3144 x2 - tabular.getAdditionalWidth(cell),
3145 y + tabular.getDescentOfRow(row),
3146 on_off ? onoffcol : col,
3147 on_off ? Painter::line_onoffdash : Painter::line_solid);
3151 docstring const InsetTabular::editMessage() const
3153 return _("Opened table");
3157 void InsetTabular::edit(Cursor & cur, bool left)
3159 //lyxerr << "InsetTabular::edit: " << this << endl;
3161 cur.selection() = false;
3164 if (isRightToLeft(cur))
3165 cur.idx() = tabular.getLastCellInRow(0);
3171 if (isRightToLeft(cur))
3172 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3174 cur.idx() = tabular.getNumberOfCells() - 1;
3176 cur.pos() = cur.lastpos(); // FIXME crude guess
3178 // FIXME: this accesses the position cache before it is initialized
3180 //cur.bv().fitCursor();
3184 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3186 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3187 << "\n cur:" << cur << endl;
3188 CursorSlice sl = cur.top();
3189 Cursor & bvcur = cur.bv().cursor();
3191 switch (cmd.action) {
3193 case LFUN_MOUSE_PRESS:
3194 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3196 if (cmd.button() == mouse_button::button1
3197 || (cmd.button() == mouse_button::button3
3198 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
3199 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
3201 cur.selection() = false;
3202 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3203 cur.bv().mouseSetCursor(cur);
3207 if (cmd.button() == mouse_button::button2) {
3208 if (cap::selection()) {
3209 // See comment in Text::dispatch why we
3211 // FIXME This does not use paste_tabular,
3212 // another reason why paste_tabular should go.
3213 cap::copySelectionToStack();
3214 cmd = FuncRequest(LFUN_PASTE, "0");
3216 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
3219 doDispatch(cur, cmd);
3220 cur.bv().buffer()->markDirty();
3221 cur.bv().mouseSetCursor(cur);
3225 case LFUN_MOUSE_MOTION:
3226 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3227 if (cmd.button() == mouse_button::button1) {
3228 // only accept motions to places not deeper nested than the real anchor
3229 if (bvcur.anchor_.hasPart(cur)) {
3230 // only update if selection changes
3231 if (bvcur.idx() == cur.idx() &&
3232 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3234 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3235 bvcur.setCursor(cur);
3236 bvcur.selection() = true;
3242 case LFUN_MOUSE_RELEASE:
3243 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3244 if (cmd.button() == mouse_button::button3)
3245 InsetTabularMailer(*this).showDialog(&cur.bv());
3248 case LFUN_CELL_BACKWARD:
3250 cur.selection() = false;
3253 case LFUN_CELL_FORWARD:
3255 cur.selection() = false;
3258 case LFUN_CHAR_FORWARD_SELECT:
3259 case LFUN_CHAR_FORWARD:
3260 cell(cur.idx())->dispatch(cur, cmd);
3261 if (!cur.result().dispatched()) {
3262 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3263 if (sl == cur.top())
3264 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3270 case LFUN_CHAR_BACKWARD_SELECT:
3271 case LFUN_CHAR_BACKWARD:
3272 cell(cur.idx())->dispatch(cur, cmd);
3273 if (!cur.result().dispatched()) {
3274 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3275 if (sl == cur.top())
3276 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3282 case LFUN_DOWN_SELECT:
3284 cell(cur.idx())->dispatch(cur, cmd);
3285 cur.dispatched(); // override the cell's decision
3286 if (sl == cur.top())
3287 // if our Text didn't do anything to the cursor
3288 // then we try to put the cursor into the cell below
3289 // setting also the right targetX.
3290 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3291 cur.idx() = tabular.getCellBelow(cur.idx());
3293 TextMetrics const & tm =
3294 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3295 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3297 if (sl == cur.top()) {
3298 // we trick it to go to the RIGHT after leaving the
3300 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3305 case LFUN_UP_SELECT:
3307 cell(cur.idx())->dispatch(cur, cmd);
3308 cur.dispatched(); // override the cell's decision
3309 if (sl == cur.top())
3310 // if our Text didn't do anything to the cursor
3311 // then we try to put the cursor into the cell above
3312 // setting also the right targetX.
3313 if (tabular.row_of_cell(cur.idx()) != 0) {
3314 cur.idx() = tabular.getCellAbove(cur.idx());
3315 cur.pit() = cur.lastpit();
3316 Text const * text = cell(cur.idx())->getText(0);
3317 TextMetrics const & tm = cur.bv().textMetrics(text);
3318 ParagraphMetrics const & pm =
3319 tm.parMetrics(cur.lastpit());
3320 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3322 if (sl == cur.top()) {
3323 cmd = FuncRequest(LFUN_UP);
3328 // case LFUN_SCREEN_DOWN: {
3329 // //if (hasSelection())
3330 // // cur.selection() = false;
3331 // col_type const col = tabular.column_of_cell(cur.idx());
3332 // int const t = cur.bv().top_y() + cur.bv().height();
3333 // if (t < yo() + tabular.getHeightOfTabular()) {
3334 // cur.bv().scrollDocView(t);
3335 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3337 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3344 // case LFUN_SCREEN_UP: {
3345 // //if (hasSelection())
3346 // // cur.selection() = false;
3347 // col_type const col = tabular.column_of_cell(cur.idx());
3348 // int const t = cur.bv().top_y() + cur.bv().height();
3350 // cur.bv().scrollDocView(t);
3354 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3358 // cur.par() = cur.lastpar();
3359 // cur.pos() = cur.lastpos();
3363 case LFUN_LAYOUT_TABULAR:
3364 InsetTabularMailer(*this).showDialog(&cur.bv());
3367 case LFUN_INSET_DIALOG_UPDATE:
3368 InsetTabularMailer(*this).updateDialog(&cur.bv());
3371 case LFUN_TABULAR_FEATURE:
3372 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3376 // insert file functions
3377 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3378 case LFUN_FILE_INSERT_PLAINTEXT: {
3380 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3381 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3387 if (tablemode(cur)) {
3388 if (copySelection(cur)) {
3389 recordUndoInset(cur, Undo::DELETE);
3394 cell(cur.idx())->dispatch(cur, cmd);
3397 case LFUN_CHAR_DELETE_BACKWARD:
3398 case LFUN_CHAR_DELETE_FORWARD:
3399 if (tablemode(cur)) {
3400 recordUndoInset(cur, Undo::DELETE);
3404 cell(cur.idx())->dispatch(cur, cmd);
3408 if (!cur.selection())
3410 if (tablemode(cur)) {
3414 cell(cur.idx())->dispatch(cur, cmd);
3417 case LFUN_CLIPBOARD_PASTE:
3418 case LFUN_PRIMARY_SELECTION_PASTE: {
3419 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3420 theClipboard().getAsText() :
3421 theSelection().get();
3424 // pass to InsertPlaintextString, but
3425 // only if we have multi-cell content
3426 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3427 if (insertPlaintextString(cur.bv(), clip, false)) {
3428 // content has been replaced,
3429 // so cursor might be invalid
3430 cur.pos() = cur.lastpos();
3431 bvcur.setCursor(cur);
3435 // Let the cell handle normal text
3436 cell(cur.idx())->dispatch(cur, cmd);
3441 if (tabularStackDirty() && theClipboard().isInternal()) {
3442 recordUndoInset(cur, Undo::INSERT);
3443 pasteClipboard(cur);
3446 cell(cur.idx())->dispatch(cur, cmd);
3449 case LFUN_FONT_EMPH:
3450 case LFUN_FONT_BOLD:
3451 case LFUN_FONT_ROMAN:
3452 case LFUN_FONT_NOUN:
3453 case LFUN_FONT_ITAL:
3454 case LFUN_FONT_FRAK:
3455 case LFUN_FONT_CODE:
3456 case LFUN_FONT_SANS:
3457 case LFUN_FONT_FREE_APPLY:
3458 case LFUN_FONT_FREE_UPDATE:
3459 case LFUN_FONT_SIZE:
3460 case LFUN_FONT_UNDERLINE:
3462 case LFUN_WORD_CAPITALIZE:
3463 case LFUN_WORD_UPCASE:
3464 case LFUN_WORD_LOWCASE:
3465 case LFUN_CHARS_TRANSPOSE:
3466 if (tablemode(cur)) {
3469 getSelection(cur, rs, re, cs, ce);
3470 Cursor tmpcur = cur;
3471 for (row_type i = rs; i <= re; ++i) {
3472 for (col_type j = cs; j <= ce; ++j) {
3473 // cursor follows cell:
3474 tmpcur.idx() = tabular.getCellNumber(i, j);
3475 // select this cell only:
3478 tmpcur.resetAnchor();
3479 tmpcur.pit() = tmpcur.lastpit();
3480 tmpcur.pos() = tmpcur.top().lastpos();
3481 tmpcur.setCursor(tmpcur);
3482 tmpcur.setSelection();
3483 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3488 cell(cur.idx())->dispatch(cur, cmd);
3492 // we try to handle this event in the insets dispatch function.
3493 cell(cur.idx())->dispatch(cur, cmd);
3499 // function sets an object as defined in func_status.h:
3500 // states OK, Unknown, Disabled, On, Off.
3501 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3502 FuncStatus & status) const
3504 switch (cmd.action) {
3505 case LFUN_TABULAR_FEATURE: {
3506 int action = Tabular::LAST_ACTION;
3508 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3509 string const tmp = tabularFeature[i].feature;
3510 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3511 action = tabularFeature[i].action;
3515 if (action == Tabular::LAST_ACTION) {
3517 status.unknown(true);
3521 string const argument
3522 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3524 row_type sel_row_start = 0;
3525 row_type sel_row_end = 0;
3526 col_type sel_col_start = 0;
3527 col_type sel_col_end = 0;
3528 Tabular::ltType dummyltt;
3531 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3534 case Tabular::SET_PWIDTH:
3535 case Tabular::SET_MPWIDTH:
3536 case Tabular::SET_SPECIAL_COLUMN:
3537 case Tabular::SET_SPECIAL_MULTI:
3538 case Tabular::APPEND_ROW:
3539 case Tabular::APPEND_COLUMN:
3540 case Tabular::DELETE_ROW:
3541 case Tabular::DELETE_COLUMN:
3542 case Tabular::COPY_ROW:
3543 case Tabular::COPY_COLUMN:
3544 case Tabular::SET_ALL_LINES:
3545 case Tabular::UNSET_ALL_LINES:
3546 case Tabular::SET_TOP_SPACE:
3547 case Tabular::SET_BOTTOM_SPACE:
3548 case Tabular::SET_INTERLINE_SPACE:
3552 case Tabular::MULTICOLUMN:
3553 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3556 case Tabular::M_TOGGLE_LINE_TOP:
3558 case Tabular::TOGGLE_LINE_TOP:
3559 status.setOnOff(tabular.topLine(cur.idx(), flag));
3562 case Tabular::M_TOGGLE_LINE_BOTTOM:
3564 case Tabular::TOGGLE_LINE_BOTTOM:
3565 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3568 case Tabular::M_TOGGLE_LINE_LEFT:
3570 case Tabular::TOGGLE_LINE_LEFT:
3571 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3574 case Tabular::M_TOGGLE_LINE_RIGHT:
3576 case Tabular::TOGGLE_LINE_RIGHT:
3577 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3580 case Tabular::M_ALIGN_LEFT:
3582 case Tabular::ALIGN_LEFT:
3583 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3586 case Tabular::M_ALIGN_RIGHT:
3588 case Tabular::ALIGN_RIGHT:
3589 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3592 case Tabular::M_ALIGN_CENTER:
3594 case Tabular::ALIGN_CENTER:
3595 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3598 case Tabular::ALIGN_BLOCK:
3599 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3600 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3603 case Tabular::M_VALIGN_TOP:
3605 case Tabular::VALIGN_TOP:
3607 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3610 case Tabular::M_VALIGN_BOTTOM:
3612 case Tabular::VALIGN_BOTTOM:
3614 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3617 case Tabular::M_VALIGN_MIDDLE:
3619 case Tabular::VALIGN_MIDDLE:
3621 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3624 case Tabular::SET_LONGTABULAR:
3625 status.setOnOff(tabular.isLongTabular());
3628 case Tabular::UNSET_LONGTABULAR:
3629 status.setOnOff(!tabular.isLongTabular());
3632 case Tabular::TOGGLE_ROTATE_TABULAR:
3633 case Tabular::SET_ROTATE_TABULAR:
3634 status.setOnOff(tabular.getRotateTabular());
3637 case Tabular::UNSET_ROTATE_TABULAR:
3638 status.setOnOff(!tabular.getRotateTabular());
3641 case Tabular::TOGGLE_ROTATE_CELL:
3642 case Tabular::SET_ROTATE_CELL:
3643 status.setOnOff(!oneCellHasRotationState(false,
3644 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3647 case Tabular::UNSET_ROTATE_CELL:
3648 status.setOnOff(!oneCellHasRotationState(true,
3649 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3652 case Tabular::SET_USEBOX:
3653 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3656 case Tabular::SET_LTFIRSTHEAD:
3657 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3660 case Tabular::UNSET_LTFIRSTHEAD:
3661 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3664 case Tabular::SET_LTHEAD:
3665 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3668 case Tabular::UNSET_LTHEAD:
3669 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3672 case Tabular::SET_LTFOOT:
3673 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3676 case Tabular::UNSET_LTFOOT:
3677 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3680 case Tabular::SET_LTLASTFOOT:
3681 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3684 case Tabular::UNSET_LTLASTFOOT:
3685 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3688 case Tabular::SET_LTNEWPAGE:
3689 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3692 case Tabular::SET_BOOKTABS:
3693 status.setOnOff(tabular.useBookTabs());
3696 case Tabular::UNSET_BOOKTABS:
3697 status.setOnOff(!tabular.useBookTabs());
3702 status.enabled(false);
3708 // These are only enabled inside tabular
3709 case LFUN_CELL_BACKWARD:
3710 case LFUN_CELL_FORWARD:
3711 status.enabled(true);
3714 // disable these with multiple cells selected
3715 case LFUN_INSET_INSERT:
3716 case LFUN_TABULAR_INSERT:
3717 case LFUN_CHARSTYLE_INSERT:
3718 case LFUN_FLOAT_INSERT:
3719 case LFUN_FLOAT_WIDE_INSERT:
3720 case LFUN_FOOTNOTE_INSERT:
3721 case LFUN_MARGINALNOTE_INSERT:
3722 case LFUN_MATH_INSERT:
3723 case LFUN_MATH_MODE:
3724 case LFUN_MATH_MUTATE:
3725 case LFUN_MATH_DISPLAY:
3726 case LFUN_NOTE_INSERT:
3727 case LFUN_OPTIONAL_INSERT:
3728 case LFUN_BOX_INSERT:
3729 case LFUN_BRANCH_INSERT:
3730 case LFUN_WRAP_INSERT:
3731 case LFUN_ERT_INSERT: {
3732 if (tablemode(cur)) {
3733 status.enabled(false);
3736 return cell(cur.idx())->getStatus(cur, cmd, status);
3739 // disable in non-fixed-width cells
3740 case LFUN_BREAK_LINE:
3741 case LFUN_BREAK_PARAGRAPH:
3742 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3743 case LFUN_BREAK_PARAGRAPH_SKIP: {
3744 if (tabular.getPWidth(cur.idx()).zero()) {
3745 status.enabled(false);
3748 return cell(cur.idx())->getStatus(cur, cmd, status);
3752 if (tabularStackDirty() && theClipboard().isInternal()) {
3753 status.enabled(true);
3756 return cell(cur.idx())->getStatus(cur, cmd, status);
3758 case LFUN_INSET_MODIFY:
3759 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3760 status.enabled(true);
3766 // we try to handle this event in the insets dispatch function.
3767 return cell(cur.idx())->getStatus(cur, cmd, status);
3772 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3773 OutputParams const & runparams) const
3775 return tabular.latex(buf, os, runparams);
3779 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3780 OutputParams const & runparams) const
3782 os << '\n'; // output table on a new line
3783 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3784 tabular.plaintext(buf, os, runparams, dp, false, 0);
3785 return PLAINTEXT_NEWLINE;
3789 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3790 OutputParams const & runparams) const
3795 // FIXME: Why not pass a proper DocIterator here?
3797 // if the table is inside a float it doesn't need the informaltable
3798 // wrapper. Search for it.
3799 for (master = owner(); master; master = master->owner())
3800 if (master->lyxCode() == Inset::FLOAT_CODE)
3805 os << "<informaltable>";
3808 ret += tabular.docbook(buf, os, runparams);
3810 os << "</informaltable>";
3817 void InsetTabular::validate(LaTeXFeatures & features) const
3819 tabular.validate(features);
3823 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3825 return tabular.getCellInset(idx);
3829 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3831 return tabular.getCellInset(idx);
3835 void InsetTabular::cursorPos(BufferView const & bv,
3836 CursorSlice const & sl, bool boundary, int & x, int & y) const
3838 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3840 // y offset correction
3841 int const row = tabular.row_of_cell(sl.idx());
3842 for (int i = 0; i <= row; ++i) {
3844 y += tabular.getAscentOfRow(i);
3845 y += tabular.getAdditionalHeight(i);
3848 y += tabular.getDescentOfRow(i);
3851 // x offset correction
3852 int const col = tabular.column_of_cell(sl.idx());
3853 int idx = tabular.getCellNumber(row, 0);
3854 for (int j = 0; j < col; ++j) {
3855 if (tabular.isPartOfMultiColumn(row, j))
3857 x += tabular.getWidthOfColumn(idx);
3860 x += tabular.getBeginningOfTextInCell(idx);
3861 x += ADD_TO_TABULAR_WIDTH;
3866 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3870 Inset const & inset = *tabular.getCellInset(cell);
3871 Point o = bv.coordCache().getInsets().xy(&inset);
3872 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3873 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3874 row_type const row = tabular.row_of_cell(cell);
3875 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3876 tabular.getAdditionalHeight(row);
3877 int const yend = o.y_ + tabular.getDescentOfRow(row);
3889 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3890 // << " ybeg=" << ybeg << " yend=" << yend
3891 // << " xx=" << xx << " yy=" << yy
3892 // << " dist=" << xx + yy << endl;
3897 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3899 //lyxerr << "InsetTabular::editXY: " << this << endl;
3900 cur.selection() = false;
3902 cur.idx() = getNearestCell(cur.bv(), x, y);
3904 return cell(cur.idx())->text_.editXY(cur, x, y);
3908 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3910 cur.idx() = getNearestCell(cur.bv(), x, y);
3911 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3915 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3917 idx_type idx_min = 0;
3918 int dist_min = std::numeric_limits<int>::max();
3919 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3920 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3921 int const d = dist(bv, i, x, y);
3932 int InsetTabular::getCellXPos(idx_type const cell) const
3936 for (; !tabular.isFirstCellInRow(c); --c)
3939 for (; c < cell; ++c)
3940 lx += tabular.getWidthOfColumn(c);
3946 void InsetTabular::resetPos(Cursor & cur) const
3948 BufferView & bv = cur.bv();
3949 int const maxwidth = bv.workWidth();
3951 if (&cur.inset() != this) {
3955 int const X2 = maxwidth;
3956 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3957 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3958 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3961 scx_ = X1 + 20 - x1;
3963 scx_ = X2 - 20 - x2;
3968 cur.updateFlags(Update::Force | Update::FitCursor);
3972 void InsetTabular::moveNextCell(Cursor & cur)
3974 if (isRightToLeft(cur)) {
3975 if (tabular.isFirstCellInRow(cur.idx())) {
3976 row_type const row = tabular.row_of_cell(cur.idx());
3977 if (row == tabular.rows() - 1)
3979 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3986 if (tabular.isLastCell(cur.idx()))
3996 void InsetTabular::movePrevCell(Cursor & cur)
3998 if (isRightToLeft(cur)) {
3999 if (tabular.isLastCellInRow(cur.idx())) {
4000 row_type const row = tabular.row_of_cell(cur.idx());
4003 cur.idx() = tabular.getFirstCellInRow(row);
4004 cur.idx() = tabular.getCellAbove(cur.idx());
4006 if (tabular.isLastCell(cur.idx()))
4011 if (cur.idx() == 0) // first cell
4015 cur.pit() = cur.lastpit();
4016 cur.pos() = cur.lastpos();
4018 // FIXME: this accesses the position cache before it is initialized
4023 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4025 Tabular::Feature action = Tabular::LAST_ACTION;
4028 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4029 string const tmp = tabularFeature[i].feature;
4031 if (tmp == what.substr(0, tmp.length())) {
4032 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4033 //tabularFeatures[i].feature.length()))
4034 action = tabularFeature[i].action;
4038 if (action == Tabular::LAST_ACTION)
4042 ltrim(what.substr(tabularFeature[i].feature.length()));
4043 tabularFeatures(cur, action, val);
4048 static void checkLongtableSpecial(Tabular::ltType & ltt,
4049 string const & special, bool & flag)
4051 if (special == "dl_above") {
4054 } else if (special == "dl_below") {
4055 ltt.bottomDL = flag;
4057 } else if (special == "empty") {
4066 bool InsetTabular::oneCellHasRotationState(bool rotated,
4067 row_type row_start, row_type row_end,
4068 col_type col_start, col_type col_end) const {
4070 for (row_type i = row_start; i <= row_end; ++i) {
4071 for (col_type j = col_start; j <= col_end; ++j) {
4072 if (tabular.getRotateCell(tabular.getCellNumber(i, j))
4081 void InsetTabular::tabularFeatures(Cursor & cur,
4082 Tabular::Feature feature, string const & value)
4084 BufferView & bv = cur.bv();
4085 col_type sel_col_start;
4086 col_type sel_col_end;
4087 row_type sel_row_start;
4088 row_type sel_row_end;
4089 bool setLines = false;
4090 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4091 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4095 case Tabular::M_ALIGN_LEFT:
4096 case Tabular::ALIGN_LEFT:
4097 setAlign = LYX_ALIGN_LEFT;
4100 case Tabular::M_ALIGN_RIGHT:
4101 case Tabular::ALIGN_RIGHT:
4102 setAlign = LYX_ALIGN_RIGHT;
4105 case Tabular::M_ALIGN_CENTER:
4106 case Tabular::ALIGN_CENTER:
4107 setAlign = LYX_ALIGN_CENTER;
4110 case Tabular::ALIGN_BLOCK:
4111 setAlign = LYX_ALIGN_BLOCK;
4114 case Tabular::M_VALIGN_TOP:
4115 case Tabular::VALIGN_TOP:
4116 setVAlign = Tabular::LYX_VALIGN_TOP;
4119 case Tabular::M_VALIGN_BOTTOM:
4120 case Tabular::VALIGN_BOTTOM:
4121 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4124 case Tabular::M_VALIGN_MIDDLE:
4125 case Tabular::VALIGN_MIDDLE:
4126 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4133 recordUndoInset(cur, Undo::ATOMIC);
4135 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4136 row_type const row = tabular.row_of_cell(cur.idx());
4137 col_type const column = tabular.column_of_cell(cur.idx());
4139 Tabular::ltType ltt;
4143 case Tabular::SET_PWIDTH: {
4144 Length const len(value);
4145 tabular.setColumnPWidth(cur, cur.idx(), len);
4147 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4148 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4152 case Tabular::SET_MPWIDTH:
4153 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4156 case Tabular::SET_SPECIAL_COLUMN:
4157 case Tabular::SET_SPECIAL_MULTI:
4158 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4161 case Tabular::APPEND_ROW:
4162 // append the row into the tabular
4163 tabular.appendRow(bv.buffer()->params(), cur.idx());
4166 case Tabular::APPEND_COLUMN:
4167 // append the column into the tabular
4168 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4169 cur.idx() = tabular.getCellNumber(row, column);
4172 case Tabular::DELETE_ROW:
4173 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4174 tabular.deleteRow(sel_row_start);
4175 if (sel_row_start >= tabular.rows())
4177 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4180 cur.selection() = false;
4183 case Tabular::DELETE_COLUMN:
4184 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4185 tabular.deleteColumn(sel_col_start);
4186 if (sel_col_start >= tabular.columns())
4188 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4191 cur.selection() = false;
4194 case Tabular::COPY_ROW:
4195 tabular.copyRow(bv.buffer()->params(), row);
4198 case Tabular::COPY_COLUMN:
4199 tabular.copyColumn(bv.buffer()->params(), column);
4200 cur.idx() = tabular.getCellNumber(row, column);
4203 case Tabular::M_TOGGLE_LINE_TOP:
4205 case Tabular::TOGGLE_LINE_TOP: {
4206 bool lineSet = !tabular.topLine(cur.idx(), flag);
4207 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4208 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4210 tabular.getCellNumber(i, j),
4215 case Tabular::M_TOGGLE_LINE_BOTTOM:
4217 case Tabular::TOGGLE_LINE_BOTTOM: {
4218 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4219 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4220 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4221 tabular.setBottomLine(
4222 tabular.getCellNumber(i, j),
4228 case Tabular::M_TOGGLE_LINE_LEFT:
4230 case Tabular::TOGGLE_LINE_LEFT: {
4231 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4232 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4233 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4234 tabular.setLeftLine(
4235 tabular.getCellNumber(i,j),
4241 case Tabular::M_TOGGLE_LINE_RIGHT:
4243 case Tabular::TOGGLE_LINE_RIGHT: {
4244 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4245 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4246 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4247 tabular.setRightLine(
4248 tabular.getCellNumber(i,j),
4254 case Tabular::M_ALIGN_LEFT:
4255 case Tabular::M_ALIGN_RIGHT:
4256 case Tabular::M_ALIGN_CENTER:
4258 case Tabular::ALIGN_LEFT:
4259 case Tabular::ALIGN_RIGHT:
4260 case Tabular::ALIGN_CENTER:
4261 case Tabular::ALIGN_BLOCK:
4262 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4263 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4264 tabular.setAlignment(
4265 tabular.getCellNumber(i, j),
4270 case Tabular::M_VALIGN_TOP:
4271 case Tabular::M_VALIGN_BOTTOM:
4272 case Tabular::M_VALIGN_MIDDLE:
4274 case Tabular::VALIGN_TOP:
4275 case Tabular::VALIGN_BOTTOM:
4276 case Tabular::VALIGN_MIDDLE:
4277 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4278 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4279 tabular.setVAlignment(
4280 tabular.getCellNumber(i, j),
4284 case Tabular::MULTICOLUMN: {
4285 if (sel_row_start != sel_row_end) {
4286 // FIXME: Need I say it ? This is horrible.
4288 Alert::error(_("Error setting multicolumn"),
4289 _("You cannot set multicolumn vertically."));
4292 if (!cur.selection()) {
4293 // just multicol for one single cell
4294 // check whether we are completely in a multicol
4295 if (tabular.isMultiColumn(cur.idx()))
4296 tabular.unsetMultiColumn(cur.idx());
4298 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4301 // we have a selection so this means we just add all this
4302 // cells to form a multicolumn cell
4303 idx_type const s_start = cur.selBegin().idx();
4304 idx_type const s_end = cur.selEnd().idx();
4305 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4306 cur.idx() = s_start;
4309 cur.selection() = false;
4313 case Tabular::SET_ALL_LINES:
4315 case Tabular::UNSET_ALL_LINES:
4316 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4317 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4318 tabular.setAllLines(
4319 tabular.getCellNumber(i,j), setLines);
4322 case Tabular::SET_LONGTABULAR:
4323 tabular.setLongTabular(true);
4326 case Tabular::UNSET_LONGTABULAR:
4327 tabular.setLongTabular(false);
4330 case Tabular::SET_ROTATE_TABULAR:
4331 tabular.setRotateTabular(true);
4334 case Tabular::UNSET_ROTATE_TABULAR:
4335 tabular.setRotateTabular(false);
4338 case Tabular::TOGGLE_ROTATE_TABULAR:
4339 tabular.setRotateTabular(!tabular.getRotateTabular());
4342 case Tabular::SET_ROTATE_CELL:
4343 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4344 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4345 tabular.setRotateCell(
4346 tabular.getCellNumber(i, j), true);
4349 case Tabular::UNSET_ROTATE_CELL:
4350 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4351 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4352 tabular.setRotateCell(
4353 tabular.getCellNumber(i, j), false);
4356 case Tabular::TOGGLE_ROTATE_CELL:
4358 bool oneNotRotated = oneCellHasRotationState(false,
4359 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4361 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4362 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4363 tabular.setRotateCell(tabular.getCellNumber(i, j),
4368 case Tabular::SET_USEBOX: {
4369 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4370 if (val == tabular.getUsebox(cur.idx()))
4371 val = Tabular::BOX_NONE;
4372 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4373 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4374 tabular.setUsebox(tabular.getCellNumber(i, j), val);
4378 case Tabular::UNSET_LTFIRSTHEAD:
4380 case Tabular::SET_LTFIRSTHEAD:
4381 tabular.getRowOfLTFirstHead(row, ltt);
4382 checkLongtableSpecial(ltt, value, flag);
4383 tabular.setLTHead(row, flag, ltt, true);
4386 case Tabular::UNSET_LTHEAD:
4388 case Tabular::SET_LTHEAD:
4389 tabular.getRowOfLTHead(row, ltt);
4390 checkLongtableSpecial(ltt, value, flag);
4391 tabular.setLTHead(row, flag, ltt, false);
4394 case Tabular::UNSET_LTFOOT:
4396 case Tabular::SET_LTFOOT:
4397 tabular.getRowOfLTFoot(row, ltt);
4398 checkLongtableSpecial(ltt, value, flag);
4399 tabular.setLTFoot(row, flag, ltt, false);
4402 case Tabular::UNSET_LTLASTFOOT:
4404 case Tabular::SET_LTLASTFOOT:
4405 tabular.getRowOfLTLastFoot(row, ltt);
4406 checkLongtableSpecial(ltt, value, flag);
4407 tabular.setLTFoot(row, flag, ltt, true);
4410 case Tabular::SET_LTNEWPAGE:
4411 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4414 case Tabular::SET_BOOKTABS:
4415 tabular.setBookTabs(true);
4418 case Tabular::UNSET_BOOKTABS:
4419 tabular.setBookTabs(false);
4422 case Tabular::SET_TOP_SPACE: {
4424 if (value == "default")
4425 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4426 tabular.row_info[i].top_space_default = true;
4427 else if (isValidLength(value, &len))
4428 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4429 tabular.row_info[i].top_space_default = false;
4430 tabular.row_info[i].top_space = 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;
4440 case Tabular::SET_BOTTOM_SPACE: {
4442 if (value == "default")
4443 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4444 tabular.row_info[i].bottom_space_default = true;
4445 else if (isValidLength(value, &len))
4446 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4447 tabular.row_info[i].bottom_space_default = false;
4448 tabular.row_info[i].bottom_space = 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;
4458 case Tabular::SET_INTERLINE_SPACE: {
4460 if (value == "default")
4461 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4462 tabular.row_info[i].interline_space_default = true;
4463 else if (isValidLength(value, &len))
4464 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4465 tabular.row_info[i].interline_space_default = false;
4466 tabular.row_info[i].interline_space = 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;
4476 // dummy stuff just to avoid warnings
4477 case Tabular::LAST_ACTION:
4483 bool InsetTabular::showInsetDialog(BufferView * bv) const
4485 InsetTabularMailer(*this).showDialog(bv);
4490 void InsetTabular::openLayoutDialog(BufferView * bv) const
4492 InsetTabularMailer(*this).showDialog(bv);
4496 bool InsetTabular::copySelection(Cursor & cur)
4498 if (!cur.selection())
4503 getSelection(cur, rs, re, cs, ce);
4505 paste_tabular.reset(new Tabular(tabular));
4507 for (row_type i = 0; i < rs; ++i)
4508 paste_tabular->deleteRow(0);
4510 row_type const rows = re - rs + 1;
4511 while (paste_tabular->rows() > rows)
4512 paste_tabular->deleteRow(rows);
4514 paste_tabular->setTopLine(0, true, true);
4515 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4518 for (col_type i = 0; i < cs; ++i)
4519 paste_tabular->deleteColumn(0);
4521 col_type const columns = ce - cs + 1;
4522 while (paste_tabular->columns() > columns)
4523 paste_tabular->deleteColumn(columns);
4525 paste_tabular->setLeftLine(0, true, true);
4526 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4529 odocstringstream os;
4530 OutputParams const runparams(0);
4531 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4532 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4533 cap::copySelection(cur, os.str());
4535 // mark tabular stack dirty
4536 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4537 // when we (hopefully) have a one-for-all paste mechanism.
4538 // This must be called after cap::copySelection.
4539 dirtyTabularStack(true);
4545 bool InsetTabular::pasteClipboard(Cursor & cur)
4549 col_type const actcol = tabular.column_of_cell(cur.idx());
4550 row_type const actrow = tabular.row_of_cell(cur.idx());
4551 for (row_type r1 = 0, r2 = actrow;
4552 r1 < paste_tabular->rows() && r2 < tabular.rows();
4554 for (col_type c1 = 0, c2 = actcol;
4555 c1 < paste_tabular->columns() && c2 < tabular.columns();
4557 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4558 tabular.isPartOfMultiColumn(r2, c2))
4560 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4564 if (tabular.isPartOfMultiColumn(r2, c2)) {
4568 shared_ptr<InsetText> inset(
4569 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4570 tabular.setCellInset(r2, c2, inset);
4571 // FIXME: change tracking (MG)
4572 inset->setChange(Change(cur.buffer().params().trackChanges ?
4573 Change::INSERTED : Change::UNCHANGED));
4581 void InsetTabular::cutSelection(Cursor & cur)
4583 if (!cur.selection())
4588 getSelection(cur, rs, re, cs, ce);
4589 for (row_type i = rs; i <= re; ++i) {
4590 for (col_type j = cs; j <= ce; ++j) {
4591 shared_ptr<InsetText> t
4592 = cell(tabular.getCellNumber(i, j));
4593 if (cur.buffer().params().trackChanges)
4594 // FIXME: Change tracking (MG)
4595 t->setChange(Change(Change::DELETED));
4601 // cursor position might be invalid now
4602 if (cur.pit() > cur.lastpit())
4603 cur.pit() = cur.lastpit();
4604 if (cur.pos() > cur.lastpos())
4605 cur.pos() = cur.lastpos();
4606 cur.clearSelection();
4610 bool InsetTabular::isRightToLeft(Cursor & cur) const
4612 BOOST_ASSERT(cur.depth() > 1);
4613 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4614 pos_type const parentpos = cur[cur.depth() - 2].pos();
4615 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4616 parentpos).language()->rightToLeft();
4620 void InsetTabular::getSelection(Cursor & cur,
4621 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4623 CursorSlice const & beg = cur.selBegin();
4624 CursorSlice const & end = cur.selEnd();
4625 cs = tabular.column_of_cell(beg.idx());
4626 ce = tabular.column_of_cell(end.idx());
4629 cs = tabular.column_of_cell(end.idx());
4631 ce = tabular.right_column_of_cell(end.idx());
4634 rs = tabular.row_of_cell(beg.idx());
4635 re = tabular.row_of_cell(end.idx());
4641 Text * InsetTabular::getText(int idx) const
4643 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4647 void InsetTabular::setChange(Change const & change)
4649 for (idx_type idx = 0; idx < nargs(); ++idx)
4650 cell(idx)->setChange(change);
4654 void InsetTabular::acceptChanges(BufferParams const & bparams)
4656 for (idx_type idx = 0; idx < nargs(); ++idx)
4657 cell(idx)->acceptChanges(bparams);
4661 void InsetTabular::rejectChanges(BufferParams const & bparams)
4663 for (idx_type idx = 0; idx < nargs(); ++idx)
4664 cell(idx)->rejectChanges(bparams);
4668 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4670 return tabular.getPWidth(cell).zero();
4674 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4677 if (buf.length() <= 0)
4680 Buffer const & buffer = *bv.buffer();
4684 col_type maxCols = 1;
4685 docstring::size_type const len = buf.length();
4686 docstring::size_type p = 0;
4689 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4697 maxCols = max(cols, maxCols);
4703 maxCols = max(cols, maxCols);
4709 paste_tabular.reset(
4710 new Tabular(buffer.params(), rows, maxCols));
4711 loctab = paste_tabular.get();
4713 dirtyTabularStack(true);
4716 cell = bv.cursor().idx();
4717 ocol = tabular.column_of_cell(cell);
4718 row = tabular.row_of_cell(cell);
4721 docstring::size_type op = 0;
4722 idx_type const cells = loctab->getNumberOfCells();
4725 rows = loctab->rows();
4726 col_type const columns = loctab->columns();
4728 while (cell < cells && p < len && row < rows &&
4729 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4735 // we can only set this if we are not too far right
4736 if (cols < columns) {
4737 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4738 Paragraph & par = inset->text_.getPar(0);
4739 Font const font = inset->text_.getFont(buffer, par, 0);
4740 inset->setText(buf.substr(op, p - op), font,
4741 buffer.params().trackChanges);
4747 // we can only set this if we are not too far right
4748 if (cols < columns) {
4749 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4750 Paragraph & par = inset->text_.getPar(0);
4751 Font const font = inset->text_.getFont(buffer, par, 0);
4752 inset->setText(buf.substr(op, p - op), font,
4753 buffer.params().trackChanges);
4758 cell = loctab->getCellNumber(row, cols);
4764 // check for the last cell if there is no trailing '\n'
4765 if (cell < cells && op < len) {
4766 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4767 Paragraph & par = inset->text_.getPar(0);
4768 Font const font = inset->text_.getFont(buffer, par, 0);
4769 inset->setText(buf.substr(op, len - op), font,
4770 buffer.params().trackChanges);
4776 void InsetTabular::addPreview(PreviewLoader & loader) const
4778 row_type const rows = tabular.rows();
4779 col_type const columns = tabular.columns();
4780 for (row_type i = 0; i < rows; ++i) {
4781 for (col_type j = 0; j < columns; ++j)
4782 tabular.getCellInset(i, j)->addPreview(loader);
4787 bool InsetTabular::tablemode(Cursor & cur) const
4789 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4796 string const InsetTabularMailer::name_("tabular");
4798 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4799 : inset_(const_cast<InsetTabular &>(inset))
4803 string const InsetTabularMailer::inset2string(Buffer const &) const
4805 return params2string(inset_);
4809 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4811 istringstream data(in);
4813 lex.setStream(data);
4820 if (!lex || token != name_)
4821 return print_mailer_error("InsetTabularMailer", in, 1,
4824 // This is part of the inset proper that is usually swallowed
4825 // by Buffer::readInset
4827 if (!lex || token != "Tabular")
4828 return print_mailer_error("InsetTabularMailer", in, 2,
4831 Buffer const & buffer = inset.buffer();
4832 inset.read(buffer, lex);
4836 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4839 data << name_ << ' ';
4840 inset.write(inset.buffer(), data);
4841 data << "\\end_inset\n";