2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
27 #include "CutAndPaste.h"
28 #include "CoordCache.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
35 #include "LaTeXFeatures.h"
39 #include "MetricsInfo.h"
40 #include "OutputParams.h"
41 #include "Paragraph.h"
42 #include "paragraph_funcs.h"
43 #include "ParagraphParameters.h"
46 #include "support/convert.h"
47 #include "support/lstrings.h"
49 #include "frontends/alert.h"
50 #include "frontends/Clipboard.h"
51 #include "frontends/Painter.h"
52 #include "frontends/Selection.h"
63 using std::istringstream;
66 using std::ostringstream;
71 #ifndef CXX_GLOBAL_CSTD
75 using boost::shared_ptr;
76 using boost::dynamic_pointer_cast;
80 using support::prefixIs;
83 using support::suffixIs;
85 using cap::dirtyTabularStack;
86 using cap::tabularStackDirty;
88 using graphics::PreviewLoader;
90 using frontend::Painter;
91 using frontend::Clipboard;
93 namespace Alert = frontend::Alert;
98 int const ADD_TO_HEIGHT = 2;
99 int const ADD_TO_TABULAR_WIDTH = 2;
100 int const default_line_space = 10;
101 int const WIDTH_OF_LINE = 5;
105 boost::scoped_ptr<Tabular> paste_tabular;
108 struct TabularFeature {
109 Tabular::Feature action;
114 TabularFeature tabularFeature[] =
116 { Tabular::APPEND_ROW, "append-row" },
117 { Tabular::APPEND_COLUMN, "append-column" },
118 { Tabular::DELETE_ROW, "delete-row" },
119 { Tabular::DELETE_COLUMN, "delete-column" },
120 { Tabular::COPY_ROW, "copy-row" },
121 { Tabular::COPY_COLUMN, "copy-column" },
122 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
123 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
124 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
125 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
126 { Tabular::ALIGN_LEFT, "align-left" },
127 { Tabular::ALIGN_RIGHT, "align-right" },
128 { Tabular::ALIGN_CENTER, "align-center" },
129 { Tabular::ALIGN_BLOCK, "align-block" },
130 { Tabular::VALIGN_TOP, "valign-top" },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
132 { Tabular::VALIGN_MIDDLE, "valign-middle" },
133 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
134 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
135 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
136 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
137 { Tabular::M_ALIGN_LEFT, "m-align-left" },
138 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
139 { Tabular::M_ALIGN_CENTER, "m-align-center" },
140 { Tabular::M_VALIGN_TOP, "m-valign-top" },
141 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
142 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
143 { Tabular::MULTICOLUMN, "multicolumn" },
144 { Tabular::SET_ALL_LINES, "set-all-lines" },
145 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
146 { Tabular::SET_LONGTABULAR, "set-longtabular" },
147 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
148 { Tabular::SET_PWIDTH, "set-pwidth" },
149 { Tabular::SET_MPWIDTH, "set-mpwidth" },
150 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
151 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
152 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
153 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
154 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
155 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
156 { Tabular::SET_USEBOX, "set-usebox" },
157 { Tabular::SET_LTHEAD, "set-lthead" },
158 { Tabular::UNSET_LTHEAD, "unset-lthead" },
159 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
160 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
161 { Tabular::SET_LTFOOT, "set-ltfoot" },
162 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
163 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
164 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
165 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
166 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
167 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
168 { Tabular::SET_BOOKTABS, "set-booktabs" },
169 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
170 { Tabular::SET_TOP_SPACE, "set-top-space" },
171 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
172 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
173 { Tabular::LAST_ACTION, "" }
177 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
179 FeatureEqual(Tabular::Feature feature)
180 : feature_(feature) {}
181 bool operator()(TabularFeature const & tf) const {
182 return tf.action == feature_;
185 Tabular::Feature feature_;
190 string const write_attribute(string const & name, T const & t)
192 string const s = tostr(t);
193 return s.empty() ? s : " " + name + "=\"" + s + "\"";
197 string const write_attribute(string const & name, string const & t)
199 return t.empty() ? t : " " + name + "=\"" + t + "\"";
204 string const write_attribute(string const & name, docstring const & t)
206 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
211 string const write_attribute(string const & name, bool const & b)
213 // we write only true attribute values so we remove a bit of the
214 // file format bloat for tabulars.
215 return b ? write_attribute(name, convert<string>(b)) : string();
220 string const write_attribute(string const & name, int const & i)
222 // we write only true attribute values so we remove a bit of the
223 // file format bloat for tabulars.
224 return i ? write_attribute(name, convert<string>(i)) : string();
229 string const write_attribute(string const & name, Tabular::idx_type const & i)
231 // we write only true attribute values so we remove a bit of the
232 // file format bloat for tabulars.
233 return i ? write_attribute(name, convert<string>(i)) : string();
238 string const write_attribute(string const & name, Length const & value)
240 // we write only the value if we really have one same reson as above.
241 return value.zero() ? string() : write_attribute(name, value.asString());
245 string const tostr(LyXAlignment const & num)
250 case LYX_ALIGN_BLOCK:
254 case LYX_ALIGN_CENTER:
256 case LYX_ALIGN_RIGHT:
258 case LYX_ALIGN_LAYOUT:
260 case LYX_ALIGN_SPECIAL:
267 string const tostr(Tabular::VAlignment const & num)
270 case Tabular::LYX_VALIGN_TOP:
272 case Tabular::LYX_VALIGN_MIDDLE:
274 case Tabular::LYX_VALIGN_BOTTOM:
281 string const tostr(Tabular::BoxType const & num)
284 case Tabular::BOX_NONE:
286 case Tabular::BOX_PARBOX:
288 case Tabular::BOX_MINIPAGE:
295 // I would have liked a fromstr template a lot better. (Lgb)
296 bool string2type(string const str, LyXAlignment & num)
299 num = LYX_ALIGN_NONE;
300 else if (str == "block")
301 num = LYX_ALIGN_BLOCK;
302 else if (str == "left")
303 num = LYX_ALIGN_LEFT;
304 else if (str == "center")
305 num = LYX_ALIGN_CENTER;
306 else if (str == "right")
307 num = LYX_ALIGN_RIGHT;
314 bool string2type(string const str, Tabular::VAlignment & num)
317 num = Tabular::LYX_VALIGN_TOP;
318 else if (str == "middle" )
319 num = Tabular::LYX_VALIGN_MIDDLE;
320 else if (str == "bottom")
321 num = Tabular::LYX_VALIGN_BOTTOM;
328 bool string2type(string const str, Tabular::BoxType & num)
331 num = Tabular::BOX_NONE;
332 else if (str == "parbox")
333 num = Tabular::BOX_PARBOX;
334 else if (str == "minipage")
335 num = Tabular::BOX_MINIPAGE;
342 bool string2type(string const str, bool & num)
346 else if (str == "false")
354 bool getTokenValue(string const & str, char const * token, string & ret)
357 size_t token_length = strlen(token);
358 string::size_type pos = str.find(token);
360 if (pos == string::npos || pos + token_length + 1 >= str.length()
361 || str[pos + token_length] != '=')
363 pos += token_length + 1;
365 if (ch != '"' && ch != '\'') { // only read till next space
369 while (pos < str.length() - 1 && str[++pos] != ch)
376 bool getTokenValue(string const & str, char const * token, docstring & ret)
379 bool const success = getTokenValue(str, token, tmp);
380 ret = from_utf8(tmp);
385 bool getTokenValue(string const & str, char const * token, int & num)
389 if (!getTokenValue(str, token, tmp))
391 num = convert<int>(tmp);
396 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
399 return getTokenValue(str, token, tmp) && string2type(tmp, num);
403 bool getTokenValue(string const & str, char const * token,
404 Tabular::VAlignment & num)
407 return getTokenValue(str, token, tmp) && string2type(tmp, num);
411 bool getTokenValue(string const & str, char const * token,
412 Tabular::BoxType & num)
415 return getTokenValue(str, token, tmp) && string2type(tmp, num);
419 bool getTokenValue(string const & str, char const * token, bool & flag)
421 // set the flag always to false as this should be the default for bools
422 // not in the file-format.
425 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
429 bool getTokenValue(string const & str, char const * token, Length & len)
431 // set the lenght to be zero() as default as this it should be if not
432 // in the file format.
435 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
439 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
444 if (!getTokenValue(str, token, tmp))
446 if (tmp == "default") {
450 return isValidLength(tmp, &len);
454 void l_getline(istream & is, string & str)
457 while (str.empty()) {
459 if (!str.empty() && str[str.length() - 1] == '\r')
460 str.erase(str.length() - 1);
467 string const featureAsString(Tabular::Feature feature)
469 TabularFeature * end = tabularFeature +
470 sizeof(tabularFeature) / sizeof(TabularFeature);
471 TabularFeature * it = std::find_if(tabularFeature, end,
472 FeatureEqual(feature));
473 return (it == end) ? string() : it->feature;
478 /////////////////////////////////////////////////////////////////////
482 /////////////////////////////////////////////////////////////////////
485 Tabular::cellstruct::cellstruct(BufferParams const & bp)
488 multicolumn(Tabular::CELL_NORMAL),
489 alignment(LYX_ALIGN_CENTER),
490 valignment(LYX_VALIGN_TOP),
497 inset(new InsetText(bp))
501 Tabular::cellstruct::cellstruct(cellstruct const & cs)
503 width_of_cell(cs.width_of_cell),
504 multicolumn(cs.multicolumn),
505 alignment(cs.alignment),
506 valignment(cs.valignment),
507 top_line(cs.top_line),
508 bottom_line(cs.bottom_line),
509 left_line(cs.left_line),
510 right_line(cs.right_line),
513 align_special(cs.align_special),
515 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
519 Tabular::cellstruct &
520 Tabular::cellstruct::operator=(cellstruct cs)
528 Tabular::cellstruct::swap(cellstruct & rhs)
530 std::swap(cellno, rhs.cellno);
531 std::swap(width_of_cell, rhs.width_of_cell);
532 std::swap(multicolumn, rhs.multicolumn);
533 std::swap(alignment, rhs.alignment);
534 std::swap(valignment, rhs.valignment);
535 std::swap(top_line, rhs.top_line);
536 std::swap(bottom_line, rhs.bottom_line);
537 std::swap(left_line, rhs.left_line);
538 std::swap(right_line, rhs.right_line);
539 std::swap(usebox, rhs.usebox);
540 std::swap(rotate, rhs.rotate);
541 std::swap(align_special, rhs.align_special);
542 p_width.swap(rhs.p_width);
543 inset.swap(rhs.inset);
547 Tabular::rowstruct::rowstruct()
552 top_space_default(false),
553 bottom_space_default(false),
554 interline_space_default(false),
563 Tabular::columnstruct::columnstruct()
564 : alignment(LYX_ALIGN_CENTER),
565 valignment(LYX_VALIGN_TOP),
573 Tabular::ltType::ltType()
580 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
581 col_type columns_arg)
583 init(bp, rows_arg, columns_arg);
587 // activates all lines and sets all widths to 0
588 void Tabular::init(BufferParams const & bp, row_type rows_arg,
589 col_type columns_arg)
592 columns_ = columns_arg;
593 row_info = row_vector(rows_);
594 column_info = column_vector(columns_);
595 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
596 row_info.reserve(10);
597 column_info.reserve(10);
598 cell_info.reserve(100);
600 for (row_type i = 0; i < rows_; ++i)
601 cell_info[i].back().right_line = true;
602 row_info.back().bottom_line = true;
603 row_info.front().bottom_line = true;
604 column_info.back().right_line = true;
605 is_long_tabular = false;
607 use_booktabs = false;
611 void Tabular::fixCellNums()
614 for (row_type i = 0; i < rows_; ++i) {
615 for (col_type j = 0; j < columns_; ++j) {
616 // When debugging it can be nice to set
618 cell_info[i][j].inset->setDrawFrame(false);
619 cell_info[i][j].cellno = cellno++;
621 cell_info[i].back().right_line = true;
624 set_row_column_number_info();
628 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
632 row_type const row = row_of_cell(cell);
634 row_vector::iterator rit = row_info.begin() + row;
635 row_info.insert(rit, rowstruct());
636 // now set the values of the row before
637 row_info[row] = row_info[row + 1];
639 cell_vvector old(rows_ - 1);
640 for (row_type i = 0; i < rows_ - 1; ++i)
641 swap(cell_info[i], old[i]);
643 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
645 for (row_type i = 0; i <= row; ++i)
646 swap(cell_info[i], old[i]);
647 for (row_type i = row + 2; i < rows_; ++i)
648 swap(cell_info[i], old[i - 1]);
651 for (col_type j = 0; j < columns_; ++j)
652 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
654 set_row_column_number_info();
658 void Tabular::deleteRow(row_type const row)
660 // Not allowed to delete last row
664 row_info.erase(row_info.begin() + row);
665 cell_info.erase(cell_info.begin() + row);
671 void Tabular::copyRow(BufferParams const & bp, row_type const row)
675 row_info.insert(row_info.begin() + row, row_info[row]);
676 cell_info.insert(cell_info.begin() + row, cell_info[row]);
679 for (col_type j = 0; j < columns_; ++j)
680 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
682 set_row_column_number_info();
686 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
690 col_type const column = column_of_cell(cell);
691 column_vector::iterator cit = column_info.begin() + column + 1;
692 column_info.insert(cit, columnstruct());
693 // set the column values of the column before
694 column_info[column + 1] = column_info[column];
696 for (row_type i = 0; i < rows_; ++i) {
697 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
699 // care about multicolumns
700 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
701 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
703 if (column + 2 >= columns_
704 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
705 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
708 for (row_type i = 0; i < rows_; ++i) {
709 cell_info[i][column + 1].inset->clear();
711 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
717 void Tabular::deleteColumn(col_type const column)
719 // Not allowed to delete last column
723 column_info.erase(column_info.begin() + column);
724 for (row_type i = 0; i < rows_; ++i)
725 cell_info[i].erase(cell_info[i].begin() + column);
731 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
735 column_info.insert(column_info.begin() + column, column_info[column]);
737 for (row_type i = 0; i < rows_; ++i)
738 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
741 for (row_type i = 0; i < rows_; ++i)
742 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
747 void Tabular::set_row_column_number_info()
750 for (row_type row = 0; row < rows_; ++row) {
751 for (col_type column = 0; column < columns_; ++column) {
752 if (cell_info[row][column].multicolumn
753 != Tabular::CELL_PART_OF_MULTICOLUMN)
755 if (numberofcells == 0)
756 // FIXME: Is this intended?
757 cell_info[row][column].cellno = npos;
759 cell_info[row][column].cellno =
764 rowofcell.resize(numberofcells);
765 columnofcell.resize(numberofcells);
770 c < numberofcells && row < rows_ && column < columns_;) {
772 columnofcell[c] = column;
776 } while (column < columns_ &&
777 cell_info[row][column].multicolumn
778 == Tabular::CELL_PART_OF_MULTICOLUMN);
780 if (column == columns_) {
786 for (row_type row = 0; row < rows_; ++row) {
787 for (col_type column = 0; column < columns_; ++column) {
788 if (isPartOfMultiColumn(row,column))
790 cell_info[row][column].inset->setAutoBreakRows(
791 !getPWidth(getCellNumber(row, column)).zero());
797 Tabular::idx_type Tabular::getNumberOfCells() const
799 return numberofcells;
803 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
805 row_type const row = row_of_cell(cell);
807 for (col_type i = 0; i < columns_; ++i)
808 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
814 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
816 if (!wholerow && isMultiColumn(cell) &&
817 !(use_booktabs && row_of_cell(cell) == 0))
818 return cellinfo_of_cell(cell).top_line;
819 return row_info[row_of_cell(cell)].top_line;
823 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
825 if (!wholerow && isMultiColumn(cell) &&
826 !(use_booktabs && isLastRow(cell)))
827 return cellinfo_of_cell(cell).bottom_line;
828 return row_info[row_of_cell(cell)].bottom_line;
832 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
836 if (!wholecolumn && isMultiColumn(cell) &&
837 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
838 return cellinfo_of_cell(cell).left_line;
839 return column_info[column_of_cell(cell)].left_line;
843 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
847 if (!wholecolumn && isMultiColumn(cell) &&
848 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
849 return cellinfo_of_cell(cell).right_line;
850 return column_info[right_column_of_cell(cell)].right_line;
854 bool Tabular::topAlreadyDrawn(idx_type cell) const
856 row_type row = row_of_cell(cell);
857 if (row > 0 && !getAdditionalHeight(row)) {
858 col_type column = column_of_cell(cell);
861 && cell_info[row][column].multicolumn
862 == Tabular::CELL_PART_OF_MULTICOLUMN)
864 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
865 return row_info[row].bottom_line;
867 return cell_info[row][column].bottom_line;
873 bool Tabular::leftAlreadyDrawn(idx_type cell) const
875 col_type column = column_of_cell(cell);
877 row_type row = row_of_cell(cell);
879 (cell_info[row][column].multicolumn ==
880 Tabular::CELL_PART_OF_MULTICOLUMN));
881 if (getAdditionalWidth(cell_info[row][column].cellno))
883 return rightLine(cell_info[row][column].cellno);
889 bool Tabular::isLastRow(idx_type cell) const
891 return row_of_cell(cell) == rows_ - 1;
895 int Tabular::getAdditionalHeight(row_type row) const
897 if (!row || row >= rows_)
903 for (col_type column = 0; column < columns_ && bottom; ++column) {
904 switch (cell_info[row - 1][column].multicolumn) {
905 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
906 bottom = cell_info[row - 1][column].bottom_line;
908 case Tabular::CELL_NORMAL:
909 bottom = row_info[row - 1].bottom_line;
912 for (col_type column = 0; column < columns_ && top; ++column) {
913 switch (cell_info[row][column].multicolumn) {
914 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
915 top = cell_info[row][column].top_line;
917 case Tabular::CELL_NORMAL:
918 top = row_info[row].top_line;
921 int const interline_space = row_info[row - 1].interline_space_default ?
923 row_info[row - 1].interline_space.inPixels(width_of_tabular);
925 return interline_space + WIDTH_OF_LINE;
926 return interline_space;
930 int Tabular::getAdditionalWidth(idx_type cell) const
932 // internally already set in setWidthOfCell
933 // used to get it back in text.cpp
934 col_type const col = right_column_of_cell(cell);
935 row_type const row = row_of_cell(cell);
936 if (col < columns_ - 1 && rightLine(cell) &&
937 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
939 return WIDTH_OF_LINE;
945 // returns the maximum over all rows
946 int Tabular::getWidthOfColumn(idx_type cell) const
948 col_type const column1 = column_of_cell(cell);
949 col_type const column2 = right_column_of_cell(cell);
951 for (col_type i = column1; i <= column2; ++i)
952 result += column_info[i].width_of_column;
957 int Tabular::getWidthOfTabular() const
959 return width_of_tabular;
963 // returns true if a complete update is necessary, otherwise false
964 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
966 if (!isMultiColumn(cell))
969 row_type const row = row_of_cell(cell);
970 col_type const column1 = column_of_cell(cell);
971 col_type const column2 = right_column_of_cell(cell);
972 int const old_val = cell_info[row][column2].width_of_cell;
974 // first set columns to 0 so we can calculate the right width
975 for (col_type i = column1; i <= column2; ++i) {
976 cell_info[row][i].width_of_cell = 0;
978 // set the width to MAX_WIDTH until width > 0
979 int width = new_width + 2 * WIDTH_OF_LINE;
980 col_type i = column1;
981 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
982 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
983 width -= column_info[i].width_of_column;
986 cell_info[row][i].width_of_cell = width;
988 if (old_val != cell_info[row][column2].width_of_cell) {
989 // in this case we have to recalculate all multicolumn cells which
990 // have this column as one of theirs but not as last one
991 calculate_width_of_column_NMC(i);
992 recalculateMulticolumnsOfColumn(i);
993 calculate_width_of_column(i);
999 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1001 // the last column does not have to be recalculated because all
1002 // multicolumns will have here there last multicolumn cell which
1003 // always will have the whole rest of the width of the cell.
1004 if (columns_ < 2 || column > (columns_ - 2))
1006 for (row_type row = 0; row < rows_; ++row) {
1007 int mc = cell_info[row][column].multicolumn;
1008 int nmc = cell_info[row][column+1].multicolumn;
1009 // we only have to update multicolumns which do not have this
1010 // column as their last column!
1011 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1012 (mc == CELL_PART_OF_MULTICOLUMN &&
1013 nmc == CELL_PART_OF_MULTICOLUMN))
1015 idx_type const cellno = cell_info[row][column].cellno;
1016 setWidthOfMulticolCell(cellno,
1017 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1023 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1025 row_type const row = row_of_cell(cell);
1026 col_type const column1 = column_of_cell(cell);
1031 if (rightLine(cell_info[row][column1].cellno, true) &&
1032 column1 < columns_ - 1 &&
1033 leftLine(cell_info[row][column1+1].cellno, true))
1035 add_width = WIDTH_OF_LINE;
1038 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1041 if (isMultiColumnReal(cell)) {
1042 tmp = setWidthOfMulticolCell(cell, new_width);
1044 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1045 cell_info[row][column1].width_of_cell = width;
1046 tmp = calculate_width_of_column_NMC(column1);
1048 recalculateMulticolumnsOfColumn(column1);
1051 for (col_type i = 0; i < columns_; ++i)
1052 calculate_width_of_column(i);
1053 calculate_width_of_tabular();
1058 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1061 if (!isMultiColumn(cell) || onlycolumn)
1062 column_info[column_of_cell(cell)].alignment = align;
1064 cellinfo_of_cell(cell).alignment = align;
1068 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1071 if (!isMultiColumn(cell) || onlycolumn)
1072 column_info[column_of_cell(cell)].valignment = align;
1074 cellinfo_of_cell(cell).valignment = align;
1081 * Allow line and paragraph breaks for fixed width cells or disallow them,
1082 * merge cell paragraphs and reset layout to standard for variable width
1085 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1087 inset->setAutoBreakRows(fixedWidth);
1091 // merge all paragraphs to one
1092 BufferParams const & bp = cur.bv().buffer()->params();
1093 while (inset->paragraphs().size() > 1)
1094 mergeParagraph(bp, inset->paragraphs(), 0);
1098 // undo information has already been recorded
1099 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1100 bp.getTextClass().defaultLayoutName());
1107 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1108 Length const & width)
1110 col_type const j = column_of_cell(cell);
1112 column_info[j].p_width = width;
1113 for (row_type i = 0; i < rows_; ++i) {
1114 idx_type const cell = getCellNumber(i, j);
1115 // because of multicolumns
1116 toggleFixedWidth(cur, getCellInset(cell).get(),
1117 !getPWidth(cell).zero());
1119 // cur paragraph can become invalid after paragraphs were merged
1120 if (cur.pit() > cur.lastpit())
1121 cur.pit() = cur.lastpit();
1122 // cur position can become invalid after newlines were removed
1123 if (cur.pos() > cur.lastpos())
1124 cur.pos() = cur.lastpos();
1128 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1129 Length const & width)
1131 if (!isMultiColumn(cell))
1134 cellinfo_of_cell(cell).p_width = width;
1135 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1136 // cur paragraph can become invalid after paragraphs were merged
1137 if (cur.pit() > cur.lastpit())
1138 cur.pit() = cur.lastpit();
1139 // cur position can become invalid after newlines were removed
1140 if (cur.pos() > cur.lastpos())
1141 cur.pos() = cur.lastpos();
1146 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1147 Tabular::Feature what)
1149 if (what == SET_SPECIAL_MULTI)
1150 cellinfo_of_cell(cell).align_special = special;
1152 column_info[column_of_cell(cell)].align_special = special;
1156 void Tabular::setAllLines(idx_type cell, bool line)
1158 setTopLine(cell, line);
1159 setBottomLine(cell, line);
1160 setRightLine(cell, line);
1161 setLeftLine(cell, line);
1165 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1167 row_type const row = row_of_cell(cell);
1168 if (wholerow || !isMultiColumn(cell))
1169 row_info[row].top_line = line;
1171 cellinfo_of_cell(cell).top_line = line;
1175 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1177 if (wholerow || !isMultiColumn(cell))
1178 row_info[row_of_cell(cell)].bottom_line = line;
1180 cellinfo_of_cell(cell).bottom_line = line;
1184 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1186 if (wholecolumn || !isMultiColumn(cell))
1187 column_info[column_of_cell(cell)].left_line = line;
1189 cellinfo_of_cell(cell).left_line = line;
1193 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1195 if (wholecolumn || !isMultiColumn(cell))
1196 column_info[right_column_of_cell(cell)].right_line = line;
1198 cellinfo_of_cell(cell).right_line = line;
1202 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1204 if (!onlycolumn && isMultiColumn(cell))
1205 return cellinfo_of_cell(cell).alignment;
1206 return column_info[column_of_cell(cell)].alignment;
1211 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1213 if (!onlycolumn && isMultiColumn(cell))
1214 return cellinfo_of_cell(cell).valignment;
1215 return column_info[column_of_cell(cell)].valignment;
1219 Length const Tabular::getPWidth(idx_type cell) const
1221 if (isMultiColumn(cell))
1222 return cellinfo_of_cell(cell).p_width;
1223 return column_info[column_of_cell(cell)].p_width;
1227 Length const Tabular::getColumnPWidth(idx_type cell) const
1229 return column_info[column_of_cell(cell)].p_width;
1233 Length const Tabular::getMColumnPWidth(idx_type cell) const
1235 if (isMultiColumn(cell))
1236 return cellinfo_of_cell(cell).p_width;
1241 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1243 if (what == SET_SPECIAL_MULTI)
1244 return cellinfo_of_cell(cell).align_special;
1245 return column_info[column_of_cell(cell)].align_special;
1249 int Tabular::getWidthOfCell(idx_type cell) const
1251 row_type const row = row_of_cell(cell);
1252 col_type const column1 = column_of_cell(cell);
1253 col_type const column2 = right_column_of_cell(cell);
1255 for (col_type i = column1; i <= column2; ++i)
1256 result += cell_info[row][i].width_of_cell;
1261 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1265 switch (getAlignment(cell)) {
1266 case LYX_ALIGN_CENTER:
1267 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1269 case LYX_ALIGN_RIGHT:
1270 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1271 // + getAdditionalWidth(cell);
1274 // LYX_ALIGN_LEFT: nothing :-)
1278 // the LaTeX Way :-(
1284 bool Tabular::isFirstCellInRow(idx_type cell) const
1286 return column_of_cell(cell) == 0;
1290 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1292 if (row > rows_ - 1)
1294 return cell_info[row][0].cellno;
1298 bool Tabular::isLastCellInRow(idx_type cell) const
1300 return right_column_of_cell(cell) == columns_ - 1;
1304 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1306 if (row > rows_ - 1)
1308 return cell_info[row][columns_-1].cellno;
1312 void Tabular::calculate_width_of_column(col_type column)
1315 for (row_type i = 0; i < rows_; ++i)
1316 maximum = max(cell_info[i][column].width_of_cell, maximum);
1317 column_info[column].width_of_column = maximum;
1322 // Calculate the columns regarding ONLY the normal cells and if this
1323 // column is inside a multicolumn cell then use it only if its the last
1324 // column of this multicolumn cell as this gives an added width to the
1325 // column, all the rest should be adapted!
1327 bool Tabular::calculate_width_of_column_NMC(col_type column)
1329 int const old_column_width = column_info[column].width_of_column;
1331 for (row_type i = 0; i < rows_; ++i) {
1332 idx_type cell = getCellNumber(i, column);
1333 bool ismulti = isMultiColumnReal(cell);
1334 if ((!ismulti || column == right_column_of_cell(cell)) &&
1335 cell_info[i][column].width_of_cell > max)
1337 max = cell_info[i][column].width_of_cell;
1340 column_info[column].width_of_column = max;
1341 return column_info[column].width_of_column != old_column_width;
1345 void Tabular::calculate_width_of_tabular()
1347 width_of_tabular = 0;
1348 for (col_type i = 0; i < columns_; ++i)
1349 width_of_tabular += column_info[i].width_of_column;
1353 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1355 if (cell >= numberofcells)
1359 return rowofcell[cell];
1363 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1365 if (cell >= numberofcells)
1366 return columns_ - 1;
1369 return columnofcell[cell];
1373 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1375 row_type const row = row_of_cell(cell);
1376 col_type column = column_of_cell(cell);
1377 while (column < columns_ - 1 &&
1378 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1384 void Tabular::write(Buffer const & buf, ostream & os) const
1388 << write_attribute("version", 3)
1389 << write_attribute("rows", rows_)
1390 << write_attribute("columns", columns_)
1392 // global longtable options
1394 << write_attribute("rotate", rotate)
1395 << write_attribute("booktabs", use_booktabs)
1396 << write_attribute("islongtable", is_long_tabular)
1397 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1398 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1399 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1400 << write_attribute("headTopDL", endhead.topDL)
1401 << write_attribute("headBottomDL", endhead.bottomDL)
1402 << write_attribute("footTopDL", endfoot.topDL)
1403 << write_attribute("footBottomDL", endfoot.bottomDL)
1404 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1405 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1406 << write_attribute("lastFootEmpty", endlastfoot.empty)
1408 for (col_type j = 0; j < columns_; ++j) {
1410 << write_attribute("alignment", column_info[j].alignment)
1411 << write_attribute("valignment", column_info[j].valignment)
1412 << write_attribute("leftline", column_info[j].left_line)
1413 << write_attribute("rightline", column_info[j].right_line)
1414 << write_attribute("width", column_info[j].p_width.asString())
1415 << write_attribute("special", column_info[j].align_special)
1418 for (row_type i = 0; i < rows_; ++i) {
1420 << write_attribute("topline", row_info[i].top_line)
1421 << write_attribute("bottomline", row_info[i].bottom_line);
1422 static const string def("default");
1423 if (row_info[i].top_space_default)
1424 os << write_attribute("topspace", def);
1426 os << write_attribute("topspace", row_info[i].top_space);
1427 if (row_info[i].bottom_space_default)
1428 os << write_attribute("bottomspace", def);
1430 os << write_attribute("bottomspace", row_info[i].bottom_space);
1431 if (row_info[i].interline_space_default)
1432 os << write_attribute("interlinespace", def);
1434 os << write_attribute("interlinespace", row_info[i].interline_space);
1435 os << write_attribute("endhead", row_info[i].endhead)
1436 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1437 << write_attribute("endfoot", row_info[i].endfoot)
1438 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1439 << write_attribute("newpage", row_info[i].newpage)
1441 for (col_type j = 0; j < columns_; ++j) {
1443 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1444 << write_attribute("alignment", cell_info[i][j].alignment)
1445 << write_attribute("valignment", cell_info[i][j].valignment)
1446 << write_attribute("topline", cell_info[i][j].top_line)
1447 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1448 << write_attribute("leftline", cell_info[i][j].left_line)
1449 << write_attribute("rightline", cell_info[i][j].right_line)
1450 << write_attribute("rotate", cell_info[i][j].rotate)
1451 << write_attribute("usebox", cell_info[i][j].usebox)
1452 << write_attribute("width", cell_info[i][j].p_width)
1453 << write_attribute("special", cell_info[i][j].align_special)
1455 os << "\\begin_inset ";
1456 cell_info[i][j].inset->write(buf, os);
1457 os << "\n\\end_inset\n"
1462 os << "</lyxtabular>\n";
1466 void Tabular::read(Buffer const & buf, Lexer & lex)
1469 istream & is = lex.getStream();
1471 l_getline(is, line);
1472 if (!prefixIs(line, "<lyxtabular ")
1473 && !prefixIs(line, "<Tabular ")) {
1474 BOOST_ASSERT(false);
1479 if (!getTokenValue(line, "version", version))
1481 BOOST_ASSERT(version >= 2);
1484 if (!getTokenValue(line, "rows", rows_arg))
1487 if (!getTokenValue(line, "columns", columns_arg))
1489 init(buf.params(), rows_arg, columns_arg);
1490 l_getline(is, line);
1491 if (!prefixIs(line, "<features")) {
1492 lyxerr << "Wrong tabular format (expected <features ...> got"
1493 << line << ')' << endl;
1496 getTokenValue(line, "rotate", rotate);
1497 getTokenValue(line, "booktabs", use_booktabs);
1498 getTokenValue(line, "islongtable", is_long_tabular);
1499 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1500 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1501 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1502 getTokenValue(line, "headTopDL", endhead.topDL);
1503 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1504 getTokenValue(line, "footTopDL", endfoot.topDL);
1505 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1506 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1507 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1508 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1510 for (col_type j = 0; j < columns_; ++j) {
1512 if (!prefixIs(line,"<column")) {
1513 lyxerr << "Wrong tabular format (expected <column ...> got"
1514 << line << ')' << endl;
1517 getTokenValue(line, "alignment", column_info[j].alignment);
1518 getTokenValue(line, "valignment", column_info[j].valignment);
1519 getTokenValue(line, "leftline", column_info[j].left_line);
1520 getTokenValue(line, "rightline", column_info[j].right_line);
1521 getTokenValue(line, "width", column_info[j].p_width);
1522 getTokenValue(line, "special", column_info[j].align_special);
1525 for (row_type i = 0; i < rows_; ++i) {
1526 l_getline(is, line);
1527 if (!prefixIs(line, "<row")) {
1528 lyxerr << "Wrong tabular format (expected <row ...> got"
1529 << line << ')' << endl;
1532 getTokenValue(line, "topline", row_info[i].top_line);
1533 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1534 getTokenValue(line, "topspace", row_info[i].top_space,
1535 row_info[i].top_space_default);
1536 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1537 row_info[i].bottom_space_default);
1538 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1539 row_info[i].interline_space_default);
1540 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1541 getTokenValue(line, "endhead", row_info[i].endhead);
1542 getTokenValue(line, "endfoot", row_info[i].endfoot);
1543 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1544 getTokenValue(line, "newpage", row_info[i].newpage);
1545 for (col_type j = 0; j < columns_; ++j) {
1546 l_getline(is, line);
1547 if (!prefixIs(line, "<cell")) {
1548 lyxerr << "Wrong tabular format (expected <cell ...> got"
1549 << line << ')' << endl;
1552 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1553 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1554 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1555 getTokenValue(line, "topline", cell_info[i][j].top_line);
1556 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1557 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1558 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1559 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1560 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1561 getTokenValue(line, "width", cell_info[i][j].p_width);
1562 getTokenValue(line, "special", cell_info[i][j].align_special);
1563 l_getline(is, line);
1564 if (prefixIs(line, "\\begin_inset")) {
1565 cell_info[i][j].inset->read(buf, lex);
1566 l_getline(is, line);
1568 if (!prefixIs(line, "</cell>")) {
1569 lyxerr << "Wrong tabular format (expected </cell> got"
1570 << line << ')' << endl;
1574 l_getline(is, line);
1575 if (!prefixIs(line, "</row>")) {
1576 lyxerr << "Wrong tabular format (expected </row> got"
1577 << line << ')' << endl;
1581 while (!prefixIs(line, "</lyxtabular>")) {
1582 l_getline(is, line);
1584 set_row_column_number_info();
1588 bool Tabular::isMultiColumn(idx_type cell) const
1590 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1594 bool Tabular::isMultiColumnReal(idx_type cell) const
1596 return column_of_cell(cell) != right_column_of_cell(cell) &&
1597 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1601 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1603 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1607 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1610 cellstruct & cs = cellinfo_of_cell(cell);
1611 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1612 cs.alignment = column_info[column_of_cell(cell)].alignment;
1613 cs.top_line = row_info[row_of_cell(cell)].top_line;
1614 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1615 cs.left_line = column_info[column_of_cell(cell)].left_line;
1616 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1617 for (idx_type i = 1; i < number; ++i) {
1618 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1619 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1620 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1623 set_row_column_number_info();
1627 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1629 row_type const row = row_of_cell(cell);
1630 col_type column = column_of_cell(cell);
1631 idx_type result = 1;
1633 while (column < columns_ &&
1634 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1643 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1645 row_type const row = row_of_cell(cell);
1646 col_type column = column_of_cell(cell);
1648 idx_type result = 0;
1650 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1651 cell_info[row][column].multicolumn = CELL_NORMAL;
1653 while (column < columns_ &&
1654 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1656 cell_info[row][column].multicolumn = CELL_NORMAL;
1661 set_row_column_number_info();
1666 void Tabular::setBookTabs(bool what)
1668 use_booktabs = what;
1672 bool Tabular::useBookTabs() const
1674 return use_booktabs;
1678 void Tabular::setLongTabular(bool what)
1680 is_long_tabular = what;
1684 bool Tabular::isLongTabular() const
1686 return is_long_tabular;
1690 void Tabular::setRotateTabular(bool flag)
1696 bool Tabular::getRotateTabular() const
1702 void Tabular::setRotateCell(idx_type cell, bool flag)
1704 cellinfo_of_cell(cell).rotate = flag;
1708 bool Tabular::getRotateCell(idx_type cell) const
1710 return cellinfo_of_cell(cell).rotate;
1714 bool Tabular::needRotating() const
1718 for (row_type i = 0; i < rows_; ++i)
1719 for (col_type j = 0; j < columns_; ++j)
1720 if (cell_info[i][j].rotate)
1726 bool Tabular::isLastCell(idx_type cell) const
1728 if (cell + 1 < numberofcells)
1734 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1736 if (row_of_cell(cell) > 0)
1737 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1742 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1744 if (row_of_cell(cell) + 1 < rows_)
1745 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1750 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1752 if (row_of_cell(cell) == 0)
1754 if (!isMultiColumn(cell))
1755 return getCellAbove(cell);
1756 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1760 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1762 if (row_of_cell(cell) + 1 >= rows_)
1764 if (!isMultiColumn(cell))
1765 return getCellBelow(cell);
1766 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1770 Tabular::idx_type Tabular::getCellNumber(row_type row,
1771 col_type column) const
1773 BOOST_ASSERT(column != npos && column < columns_ &&
1774 row != npos && row < rows_);
1775 return cell_info[row][column].cellno;
1779 void Tabular::setUsebox(idx_type cell, BoxType type)
1781 cellinfo_of_cell(cell).usebox = type;
1785 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1787 if (column_info[column_of_cell(cell)].p_width.zero() &&
1788 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1790 if (cellinfo_of_cell(cell).usebox > 1)
1791 return cellinfo_of_cell(cell).usebox;
1792 return useParbox(cell);
1797 // This are functions used for the longtable support
1799 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1805 row_info[row].endfirsthead = flag;
1809 row_info[row].endhead = flag;
1814 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1817 hd.set = haveLTHead();
1818 return row_info[row].endhead;
1822 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1825 hd.set = haveLTFirstHead();
1826 return row_info[row].endfirsthead;
1830 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1836 row_info[row].endlastfoot = flag;
1840 row_info[row].endfoot = flag;
1845 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1848 fd.set = haveLTFoot();
1849 return row_info[row].endfoot;
1853 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1856 fd.set = haveLTLastFoot();
1857 return row_info[row].endlastfoot;
1861 void Tabular::setLTNewPage(row_type row, bool what)
1863 row_info[row].newpage = what;
1867 bool Tabular::getLTNewPage(row_type row) const
1869 return row_info[row].newpage;
1873 bool Tabular::haveLTHead() const
1875 for (row_type i = 0; i < rows_; ++i)
1876 if (row_info[i].endhead)
1882 bool Tabular::haveLTFirstHead() const
1884 if (endfirsthead.empty)
1886 for (row_type i = 0; i < rows_; ++i)
1887 if (row_info[i].endfirsthead)
1893 bool Tabular::haveLTFoot() const
1895 for (row_type i = 0; i < rows_; ++i)
1896 if (row_info[i].endfoot)
1902 bool Tabular::haveLTLastFoot() const
1904 if (endlastfoot.empty)
1906 for (row_type i = 0; i < rows_; ++i)
1907 if (row_info[i].endlastfoot)
1913 // end longtable support functions
1915 void Tabular::setAscentOfRow(row_type row, int height)
1917 if (row >= rows_ || row_info[row].ascent_of_row == height)
1919 row_info[row].ascent_of_row = height;
1923 void Tabular::setDescentOfRow(row_type row, int height)
1925 if (row >= rows_ || row_info[row].descent_of_row == height)
1927 row_info[row].descent_of_row = height;
1931 int Tabular::getAscentOfRow(row_type row) const
1935 return row_info[row].ascent_of_row;
1939 int Tabular::getDescentOfRow(row_type row) const
1941 BOOST_ASSERT(row < rows_);
1942 return row_info[row].descent_of_row;
1946 int Tabular::getHeightOfTabular() const
1949 for (row_type row = 0; row < rows_; ++row)
1950 height += getAscentOfRow(row) + getDescentOfRow(row) +
1951 getAdditionalHeight(row);
1956 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1958 BOOST_ASSERT(row < rows_);
1959 BOOST_ASSERT(column < columns_);
1960 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1964 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1966 // FIXME: assert or return 0 as in TeXBottomHLine()?
1967 BOOST_ASSERT(row != npos);
1968 BOOST_ASSERT(row < rows_);
1970 idx_type const fcell = getFirstCellInRow(row);
1971 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1974 for (idx_type i = fcell; i < n; ++i) {
1978 if (use_booktabs && row == 0) {
1980 } else if (tmp == n - fcell) {
1981 os << (use_booktabs ? "\\midrule " : "\\hline ");
1983 for (idx_type i = fcell; i < n; ++i) {
1985 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1986 << column_of_cell(i) + 1
1988 << right_column_of_cell(i) + 1
2000 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2002 // FIXME: return 0 or assert as in TeXTopHLine()?
2003 if (row == npos || row >= rows_)
2006 idx_type const fcell = getFirstCellInRow(row);
2007 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2010 for (idx_type i = fcell; i < n; ++i) {
2014 if (use_booktabs && row == rows_ - 1) {
2015 os << "\\bottomrule";
2016 } else if (tmp == n - fcell) {
2017 os << (use_booktabs ? "\\midrule" : "\\hline");
2019 for (idx_type i = fcell; i < n; ++i) {
2020 if (bottomLine(i)) {
2021 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2022 << column_of_cell(i) + 1
2024 << right_column_of_cell(i) + 1
2036 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2040 if (getRotateCell(cell)) {
2041 os << "\\begin{sideways}\n";
2044 if (isMultiColumn(cell)) {
2045 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2046 if (leftLine(cell) &&
2047 (isFirstCellInRow(cell) ||
2048 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2049 !rightLine(cell - 1, true))))
2051 if (!cellinfo_of_cell(cell).align_special.empty()) {
2052 os << cellinfo_of_cell(cell).align_special;
2054 if (!getPWidth(cell).zero()) {
2055 switch (getVAlignment(cell)) {
2056 case LYX_VALIGN_TOP:
2059 case LYX_VALIGN_MIDDLE:
2062 case LYX_VALIGN_BOTTOM:
2067 << from_ascii(getPWidth(cell).asLatexString())
2070 switch (getAlignment(cell)) {
2071 case LYX_ALIGN_LEFT:
2074 case LYX_ALIGN_RIGHT:
2081 } // end if else !getPWidth
2082 } // end if else !cellinfo_of_cell
2083 if (rightLine(cell))
2085 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2090 if (getUsebox(cell) == BOX_PARBOX) {
2092 switch (getVAlignment(cell)) {
2093 case LYX_VALIGN_TOP:
2096 case LYX_VALIGN_MIDDLE:
2099 case LYX_VALIGN_BOTTOM:
2103 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2105 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2106 os << "\\begin{minipage}[";
2107 switch (getVAlignment(cell)) {
2108 case LYX_VALIGN_TOP:
2111 case LYX_VALIGN_MIDDLE:
2114 case LYX_VALIGN_BOTTOM:
2118 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2126 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2131 if (getUsebox(cell) == BOX_PARBOX)
2133 else if (getUsebox(cell) == BOX_MINIPAGE) {
2134 os << "%\n\\end{minipage}";
2137 if (isMultiColumn(cell)) {
2140 if (getRotateCell(cell)) {
2141 os << "%\n\\end{sideways}";
2148 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2149 OutputParams const & runparams) const
2151 if (!is_long_tabular)
2155 // output header info
2157 if (endhead.topDL) {
2161 for (row_type i = 0; i < rows_; ++i) {
2162 if (row_info[i].endhead) {
2163 ret += TeXRow(os, i, buf, runparams);
2166 if (endhead.bottomDL) {
2170 os << "\\endhead\n";
2172 if (endfirsthead.empty) {
2173 os << "\\endfirsthead\n";
2177 // output firstheader info
2178 if (haveLTFirstHead()) {
2179 if (endfirsthead.topDL) {
2183 for (row_type i = 0; i < rows_; ++i) {
2184 if (row_info[i].endfirsthead) {
2185 ret += TeXRow(os, i, buf, runparams);
2188 if (endfirsthead.bottomDL) {
2192 os << "\\endfirsthead\n";
2195 // output footer info
2197 if (endfoot.topDL) {
2201 for (row_type i = 0; i < rows_; ++i) {
2202 if (row_info[i].endfoot) {
2203 ret += TeXRow(os, i, buf, runparams);
2206 if (endfoot.bottomDL) {
2210 os << "\\endfoot\n";
2212 if (endlastfoot.empty) {
2213 os << "\\endlastfoot\n";
2217 // output lastfooter info
2218 if (haveLTLastFoot()) {
2219 if (endlastfoot.topDL) {
2223 for (row_type i = 0; i < rows_; ++i) {
2224 if (row_info[i].endlastfoot) {
2225 ret += TeXRow(os, i, buf, runparams);
2228 if (endlastfoot.bottomDL) {
2232 os << "\\endlastfoot\n";
2239 bool Tabular::isValidRow(row_type row) const
2241 if (!is_long_tabular)
2243 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2244 !row_info[row].endfoot && !row_info[row].endlastfoot;
2248 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2249 OutputParams const & runparams) const
2251 idx_type cell = getCellNumber(i, 0);
2252 int ret = TeXTopHLine(os, i);
2253 if (row_info[i].top_space_default) {
2255 os << "\\addlinespace\n";
2257 os << "\\noalign{\\vskip\\doublerulesep}\n";
2259 } else if(!row_info[i].top_space.zero()) {
2261 os << "\\addlinespace["
2262 << from_ascii(row_info[i].top_space.asLatexString())
2265 os << "\\noalign{\\vskip"
2266 << from_ascii(row_info[i].top_space.asLatexString())
2272 for (col_type j = 0; j < columns_; ++j) {
2273 if (isPartOfMultiColumn(i, j))
2275 ret += TeXCellPreamble(os, cell);
2276 shared_ptr<InsetText> inset = getCellInset(cell);
2278 Paragraph const & par = inset->paragraphs().front();
2279 bool rtl = par.isRightToLeftPar(buf.params())
2281 && getPWidth(cell).zero();
2284 if (par.getParLanguage(buf.params())->lang() ==
2287 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2289 // currently, remaning RTL languages are arabic_arabtex and hebrew
2293 ret += inset->latex(buf, os, runparams);
2297 ret += TeXCellPostamble(os, cell);
2298 if (!isLastCellInRow(cell)) { // not last cell in row
2303 os << "\\tabularnewline";
2304 if (row_info[i].bottom_space_default) {
2306 os << "\\addlinespace";
2308 os << "[\\doublerulesep]";
2309 } else if (!row_info[i].bottom_space.zero()) {
2311 os << "\\addlinespace";
2313 << from_ascii(row_info[i].bottom_space.asLatexString())
2318 ret += TeXBottomHLine(os, i);
2319 if (row_info[i].interline_space_default) {
2321 os << "\\addlinespace\n";
2323 os << "\\noalign{\\vskip\\doublerulesep}\n";
2325 } else if (!row_info[i].interline_space.zero()) {
2327 os << "\\addlinespace["
2328 << from_ascii(row_info[i].interline_space.asLatexString())
2331 os << "\\noalign{\\vskip"
2332 << from_ascii(row_info[i].interline_space.asLatexString())
2340 int Tabular::latex(Buffer const & buf, odocstream & os,
2341 OutputParams const & runparams) const
2345 //+---------------------------------------------------------------------
2346 //+ first the opening preamble +
2347 //+---------------------------------------------------------------------
2350 os << "\\begin{sideways}\n";
2353 if (is_long_tabular)
2354 os << "\\begin{longtable}{";
2356 os << "\\begin{tabular}{";
2357 for (col_type i = 0; i < columns_; ++i) {
2358 if (!use_booktabs && column_info[i].left_line)
2360 if (!column_info[i].align_special.empty()) {
2361 os << column_info[i].align_special;
2363 if (!column_info[i].p_width.zero()) {
2364 switch (column_info[i].alignment) {
2365 case LYX_ALIGN_LEFT:
2366 os << ">{\\raggedright}";
2368 case LYX_ALIGN_RIGHT:
2369 os << ">{\\raggedleft}";
2371 case LYX_ALIGN_CENTER:
2372 os << ">{\\centering}";
2374 case LYX_ALIGN_NONE:
2375 case LYX_ALIGN_BLOCK:
2376 case LYX_ALIGN_LAYOUT:
2377 case LYX_ALIGN_SPECIAL:
2381 switch (column_info[i].valignment) {
2382 case LYX_VALIGN_TOP:
2385 case LYX_VALIGN_MIDDLE:
2388 case LYX_VALIGN_BOTTOM:
2393 << from_ascii(column_info[i].p_width.asLatexString())
2396 switch (column_info[i].alignment) {
2397 case LYX_ALIGN_LEFT:
2400 case LYX_ALIGN_RIGHT:
2407 } // end if else !column_info[i].p_width
2408 } // end if else !column_info[i].align_special
2409 if (!use_booktabs && column_info[i].right_line)
2415 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2417 //+---------------------------------------------------------------------
2418 //+ the single row and columns (cells) +
2419 //+---------------------------------------------------------------------
2421 for (row_type i = 0; i < rows_; ++i) {
2422 if (isValidRow(i)) {
2423 ret += TeXRow(os, i, buf, runparams);
2424 if (is_long_tabular && row_info[i].newpage) {
2425 os << "\\newpage\n";
2431 //+---------------------------------------------------------------------
2432 //+ the closing of the tabular +
2433 //+---------------------------------------------------------------------
2435 if (is_long_tabular)
2436 os << "\\end{longtable}";
2438 os << "\\end{tabular}";
2440 os << "\n\\end{sideways}";
2448 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2449 OutputParams const & runparams) const
2452 idx_type cell = getFirstCellInRow(row);
2455 for (col_type j = 0; j < columns_; ++j) {
2456 if (isPartOfMultiColumn(row, j))
2459 os << "<entry align=\"";
2460 switch (getAlignment(cell)) {
2461 case LYX_ALIGN_LEFT:
2464 case LYX_ALIGN_RIGHT:
2472 os << "\" valign=\"";
2473 switch (getVAlignment(cell)) {
2474 case LYX_VALIGN_TOP:
2477 case LYX_VALIGN_BOTTOM:
2480 case LYX_VALIGN_MIDDLE:
2485 if (isMultiColumn(cell)) {
2486 os << " namest=\"col" << j << "\" ";
2487 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2491 ret += getCellInset(cell)->docbook(buf, os, runparams);
2500 int Tabular::docbook(Buffer const & buf, odocstream & os,
2501 OutputParams const & runparams) const
2505 //+---------------------------------------------------------------------
2506 //+ first the opening preamble +
2507 //+---------------------------------------------------------------------
2509 os << "<tgroup cols=\"" << columns_
2510 << "\" colsep=\"1\" rowsep=\"1\">\n";
2512 for (col_type i = 0; i < columns_; ++i) {
2513 os << "<colspec colname=\"col" << i << "\" align=\"";
2514 switch (column_info[i].alignment) {
2515 case LYX_ALIGN_LEFT:
2518 case LYX_ALIGN_RIGHT:
2526 if (runparams.flavor == OutputParams::XML)
2532 //+---------------------------------------------------------------------
2533 //+ Long Tabular case +
2534 //+---------------------------------------------------------------------
2536 // output header info
2537 if (haveLTHead() || haveLTFirstHead()) {
2540 for (row_type i = 0; i < rows_; ++i) {
2541 if (row_info[i].endhead || row_info[i].endfirsthead) {
2542 ret += docbookRow(buf, os, i, runparams);
2548 // output footer info
2549 if (haveLTFoot() || haveLTLastFoot()) {
2552 for (row_type i = 0; i < rows_; ++i) {
2553 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2554 ret += docbookRow(buf, os, i, runparams);
2561 //+---------------------------------------------------------------------
2562 //+ the single row and columns (cells) +
2563 //+---------------------------------------------------------------------
2567 for (row_type i = 0; i < rows_; ++i) {
2568 if (isValidRow(i)) {
2569 ret += docbookRow(buf, os, i, runparams);
2574 //+---------------------------------------------------------------------
2575 //+ the closing of the tabular +
2576 //+---------------------------------------------------------------------
2585 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2586 vector<unsigned int> const & clen) const
2588 idx_type const fcell = getFirstCellInRow(row);
2589 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2592 for (idx_type i = fcell; i < n; ++i) {
2602 for (idx_type i = fcell; i < n; ++i) {
2613 col_type column = column_of_cell(i);
2614 int len = clen[column];
2615 while (column < columns_ - 1
2616 && isPartOfMultiColumn(row, ++column))
2617 len += clen[column] + 4;
2618 os << docstring(len, ch);
2633 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2634 vector<unsigned int> const & clen) const
2636 idx_type const fcell = getFirstCellInRow(row);
2637 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2640 for (idx_type i = fcell; i < n; ++i) {
2641 if (bottomLine(i)) {
2650 for (idx_type i = fcell; i < n; ++i) {
2651 if (bottomLine(i)) {
2661 col_type column = column_of_cell(i);
2662 int len = clen[column];
2663 while (column < columns_ -1
2664 && isPartOfMultiColumn(row, ++column))
2665 len += clen[column] + 4;
2666 os << docstring(len, ch);
2667 if (bottomLine(i)) {
2681 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2682 OutputParams const & runparams,
2683 idx_type cell, row_type row, col_type column,
2684 vector<unsigned int> const & clen,
2685 bool onlydata) const
2687 odocstringstream sstr;
2688 getCellInset(cell)->plaintext(buf, sstr, runparams);
2700 unsigned int len1 = sstr.str().length();
2701 unsigned int len2 = clen[column];
2702 while (column < columns_ -1
2703 && isPartOfMultiColumn(row, ++column))
2704 len2 += clen[column] + 4;
2707 switch (getAlignment(cell)) {
2709 case LYX_ALIGN_LEFT:
2712 case LYX_ALIGN_RIGHT:
2716 case LYX_ALIGN_CENTER:
2722 os << docstring(len1, ' ') << sstr.str()
2723 << docstring(len2, ' ');
2725 if (rightLine(cell))
2732 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2733 OutputParams const & runparams, int const depth,
2734 bool onlydata, unsigned char delim) const
2736 // first calculate the width of the single columns
2737 vector<unsigned int> clen(columns_);
2740 // first all non (real) multicolumn cells!
2741 for (col_type j = 0; j < columns_; ++j) {
2743 for (row_type i = 0; i < rows_; ++i) {
2744 idx_type cell = getCellNumber(i, j);
2745 if (isMultiColumnReal(cell))
2747 odocstringstream sstr;
2748 getCellInset(cell)->plaintext(buf, sstr, runparams);
2749 if (clen[j] < sstr.str().length())
2750 clen[j] = sstr.str().length();
2753 // then all (real) multicolumn cells!
2754 for (col_type j = 0; j < columns_; ++j) {
2755 for (row_type i = 0; i < rows_; ++i) {
2756 idx_type cell = getCellNumber(i, j);
2757 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2759 odocstringstream sstr;
2760 getCellInset(cell)->plaintext(buf, sstr, runparams);
2761 int len = int(sstr.str().length());
2762 idx_type const n = cells_in_multicolumn(cell);
2763 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2765 if (len > int(clen[j + n - 1]))
2766 clen[j + n - 1] = len;
2771 for (row_type i = 0; i < rows_; ++i) {
2772 if (!onlydata && plaintextTopHLine(os, i, clen))
2773 os << docstring(depth * 2, ' ');
2774 for (col_type j = 0; j < columns_; ++j) {
2775 if (isPartOfMultiColumn(i, j))
2777 if (onlydata && j > 0)
2779 plaintextPrintCell(buf, os, runparams,
2780 cell, i, j, clen, onlydata);
2785 os << docstring(depth * 2, ' ');
2786 if (plaintextBottomHLine(os, i, clen))
2787 os << docstring(depth * 2, ' ');
2793 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2795 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2799 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2800 col_type column) const
2802 return cell_info[row][column].inset;
2806 void Tabular::setCellInset(row_type row, col_type column,
2807 shared_ptr<InsetText> ins) const
2809 cell_info[row][column].inset = ins;
2814 Tabular::getCellFromInset(Inset const * inset) const
2816 // is this inset part of the tabular?
2818 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2819 BOOST_ASSERT(false);
2822 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2823 if (getCellInset(cell).get() == inset) {
2824 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2825 << "cell=" << cell << endl;
2829 // We should have found a cell at this point
2830 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2831 << inset << " not found!" << endl;
2832 BOOST_ASSERT(false);
2838 void Tabular::validate(LaTeXFeatures & features) const
2840 features.require("NeedTabularnewline");
2842 features.require("booktabs");
2843 if (isLongTabular())
2844 features.require("longtable");
2846 features.require("rotating");
2847 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2848 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2849 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2850 features.require("array");
2851 getCellInset(cell)->validate(features);
2856 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2858 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2859 ParagraphList::const_iterator cit = parlist.begin();
2860 ParagraphList::const_iterator end = parlist.end();
2862 for (; cit != end; ++cit)
2863 for (int i = 0; i < cit->size(); ++i)
2864 if (cit->isNewline(i))
2871 /////////////////////////////////////////////////////////////////////
2875 /////////////////////////////////////////////////////////////////////
2877 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2879 : tabular(buf.params(), max(rows, row_type(1)),
2880 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2884 InsetTabular::InsetTabular(InsetTabular const & tab)
2885 : Inset(tab), tabular(tab.tabular),
2886 buffer_(tab.buffer_), scx_(0)
2890 InsetTabular::~InsetTabular()
2892 InsetTabularMailer(*this).hideDialog();
2896 auto_ptr<Inset> InsetTabular::doClone() const
2898 return auto_ptr<Inset>(new InsetTabular(*this));
2902 Buffer const & InsetTabular::buffer() const
2908 void InsetTabular::buffer(Buffer const * b)
2914 void InsetTabular::write(Buffer const & buf, ostream & os) const
2916 os << "Tabular" << endl;
2917 tabular.write(buf, os);
2921 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2923 bool const old_format = (lex.getString() == "\\LyXTable");
2925 tabular.read(buf, lex);
2931 string token = lex.getString();
2932 while (lex && token != "\\end_inset") {
2934 token = lex.getString();
2937 lex.printError("Missing \\end_inset at this point. "
2943 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2945 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2946 // mi.base.textwidth << "\n";
2948 lyxerr << "InsetTabular::metrics: need bv" << endl;
2949 BOOST_ASSERT(false);
2953 for (idx_type cell = 0; i < tabular.rows(); ++i) {
2956 for (col_type j = 0; j < tabular.columns(); ++j) {
2957 if (tabular.isPartOfMultiColumn(i, j))
2958 // Multicolumn cell, but not first one
2963 if (tabular.cell_info[i][j].multicolumn ==
2964 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2965 p_width = tabular.cellinfo_of_cell(cell).p_width;
2967 p_width = tabular.column_info[j].p_width;
2968 if (!p_width.zero())
2969 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2970 tabular.getCellInset(cell)->metrics(m, dim);
2971 if (!p_width.zero())
2972 dim.wid = m.base.textwidth;
2973 tabular.setWidthOfCell(cell, dim.wid);
2974 if (p_width.zero()) {
2975 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2976 // FIXME there must be a way to get rid of
2977 // the second metrics call
2978 tabular.getCellInset(cell)->metrics(m, dim);
2980 maxAsc = max(maxAsc, dim.asc);
2981 maxDesc = max(maxDesc, dim.des);
2984 int const top_space = tabular.row_info[i].top_space_default ?
2985 default_line_space :
2986 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2987 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
2988 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2989 default_line_space :
2990 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2991 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2994 dim.asc = tabular.getAscentOfRow(0);
2995 dim.des = tabular.getHeightOfTabular() - dim.asc;
2996 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
2997 bool const changed = dim_ != dim;
3003 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3005 setPosCache(pi, x, y);
3007 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3008 BufferView * bv = pi.base.bv;
3010 resetPos(bv->cursor());
3013 x += ADD_TO_TABULAR_WIDTH;
3016 first_visible_cell = Tabular::npos;
3017 for (row_type i = 0; i < tabular.rows(); ++i) {
3019 int const a = tabular.getAscentOfRow(i);
3020 int const d = tabular.getDescentOfRow(i);
3021 idx = tabular.getCellNumber(i, 0);
3022 for (col_type j = 0; j < tabular.columns(); ++j) {
3023 if (tabular.isPartOfMultiColumn(i, j))
3025 if (first_visible_cell == Tabular::npos)
3026 first_visible_cell = idx;
3028 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3029 if (nx + tabular.getWidthOfColumn(idx) < 0
3030 || nx > bv->workWidth()
3032 || y - a > bv->workHeight()) {
3033 pi.pain.setDrawingEnabled(false);
3034 cell(idx)->draw(pi, cx, y);
3035 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3036 pi.pain.setDrawingEnabled(true);
3038 cell(idx)->draw(pi, cx, y);
3039 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3041 nx += tabular.getWidthOfColumn(idx);
3045 if (i + 1 < tabular.rows())
3046 y += d + tabular.getAscentOfRow(i + 1) +
3047 tabular.getAdditionalHeight(i + 1);
3052 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3054 setPosCache(pi, x, y);
3056 Cursor & cur = pi.base.bv->cursor();
3058 x += scx_ + ADD_TO_TABULAR_WIDTH;
3060 // Paint background of current tabular
3061 int const w = tabular.getWidthOfTabular();
3062 int const h = tabular.getHeightOfTabular();
3063 int yy = y - tabular.getAscentOfRow(0);
3064 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3066 if (!cur.selection())
3068 if (!ptr_cmp(&cur.inset(), this))
3074 if (tablemode(cur)) {
3077 getSelection(cur, rs, re, cs, ce);
3078 y -= tabular.getAscentOfRow(0);
3079 for (row_type j = 0; j < tabular.rows(); ++j) {
3080 int const a = tabular.getAscentOfRow(j);
3081 int const h = a + tabular.getDescentOfRow(j);
3083 y += tabular.getAdditionalHeight(j);
3084 for (col_type i = 0; i < tabular.columns(); ++i) {
3085 if (tabular.isPartOfMultiColumn(j, i))
3087 idx_type const cell =
3088 tabular.getCellNumber(j, i);
3089 int const w = tabular.getWidthOfColumn(cell);
3090 if (i >= cs && i <= ce && j >= rs && j <= re)
3091 pi.pain.fillRectangle(xx, y, w, h,
3099 x += getCellXPos(cur.idx());
3100 x += tabular.getBeginningOfTextInCell(cur.idx());
3101 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3106 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3107 row_type row, idx_type cell, bool erased) const
3109 int x2 = x + tabular.getWidthOfColumn(cell);
3110 bool on_off = false;
3111 Color::color col = Color::tabularline;
3112 Color::color onoffcol = Color::tabularonoffline;
3115 col = Color::deletedtext;
3116 onoffcol = Color::deletedtext;
3119 if (!tabular.topAlreadyDrawn(cell)) {
3120 on_off = !tabular.topLine(cell);
3121 pain.line(x, y - tabular.getAscentOfRow(row),
3122 x2, y - tabular.getAscentOfRow(row),
3123 on_off ? onoffcol : col,
3124 on_off ? Painter::line_onoffdash : Painter::line_solid);
3126 on_off = !tabular.bottomLine(cell);
3127 pain.line(x, y + tabular.getDescentOfRow(row),
3128 x2, y + tabular.getDescentOfRow(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3131 if (!tabular.leftAlreadyDrawn(cell)) {
3132 on_off = !tabular.leftLine(cell);
3133 pain.line(x, y - tabular.getAscentOfRow(row),
3134 x, y + tabular.getDescentOfRow(row),
3135 on_off ? onoffcol : col,
3136 on_off ? Painter::line_onoffdash : Painter::line_solid);
3138 on_off = !tabular.rightLine(cell);
3139 pain.line(x2 - tabular.getAdditionalWidth(cell),
3140 y - tabular.getAscentOfRow(row),
3141 x2 - tabular.getAdditionalWidth(cell),
3142 y + tabular.getDescentOfRow(row),
3143 on_off ? onoffcol : col,
3144 on_off ? Painter::line_onoffdash : Painter::line_solid);
3148 docstring const InsetTabular::editMessage() const
3150 return _("Opened table");
3154 void InsetTabular::edit(Cursor & cur, bool left)
3156 //lyxerr << "InsetTabular::edit: " << this << endl;
3158 cur.selection() = false;
3161 if (isRightToLeft(cur))
3162 cur.idx() = tabular.getLastCellInRow(0);
3168 if (isRightToLeft(cur))
3169 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3171 cur.idx() = tabular.getNumberOfCells() - 1;
3173 cur.pos() = cur.lastpos(); // FIXME crude guess
3175 // FIXME: this accesses the position cache before it is initialized
3177 //cur.bv().fitCursor();
3181 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3183 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3184 << "\n cur:" << cur << endl;
3185 CursorSlice sl = cur.top();
3186 Cursor & bvcur = cur.bv().cursor();
3188 switch (cmd.action) {
3190 case LFUN_MOUSE_PRESS:
3191 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3193 if (cmd.button() == mouse_button::button1
3194 || (cmd.button() == mouse_button::button3
3195 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
3196 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
3198 cur.selection() = false;
3199 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3200 cur.bv().mouseSetCursor(cur);
3204 if (cmd.button() == mouse_button::button2) {
3205 if (cap::selection()) {
3206 // See comment in Text::dispatch why we
3208 // FIXME This does not use paste_tabular,
3209 // another reason why paste_tabular should go.
3210 cap::copySelectionToStack();
3211 cmd = FuncRequest(LFUN_PASTE, "0");
3213 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
3216 doDispatch(cur, cmd);
3217 cur.bv().buffer()->markDirty();
3218 cur.bv().mouseSetCursor(cur);
3222 case LFUN_MOUSE_MOTION:
3223 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3224 if (cmd.button() == mouse_button::button1) {
3225 // only accept motions to places not deeper nested than the real anchor
3226 if (bvcur.anchor_.hasPart(cur)) {
3227 // only update if selection changes
3228 if (bvcur.idx() == cur.idx() &&
3229 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3231 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3232 bvcur.setCursor(cur);
3233 bvcur.selection() = true;
3239 case LFUN_MOUSE_RELEASE:
3240 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3241 if (cmd.button() == mouse_button::button3)
3242 InsetTabularMailer(*this).showDialog(&cur.bv());
3245 case LFUN_CELL_BACKWARD:
3247 cur.selection() = false;
3250 case LFUN_CELL_FORWARD:
3252 cur.selection() = false;
3255 case LFUN_CHAR_FORWARD_SELECT:
3256 case LFUN_CHAR_FORWARD:
3257 cell(cur.idx())->dispatch(cur, cmd);
3258 if (!cur.result().dispatched()) {
3259 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3260 if (sl == cur.top())
3261 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3267 case LFUN_CHAR_BACKWARD_SELECT:
3268 case LFUN_CHAR_BACKWARD:
3269 cell(cur.idx())->dispatch(cur, cmd);
3270 if (!cur.result().dispatched()) {
3271 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3272 if (sl == cur.top())
3273 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3279 case LFUN_DOWN_SELECT:
3281 cell(cur.idx())->dispatch(cur, cmd);
3282 cur.dispatched(); // override the cell's decision
3283 if (sl == cur.top())
3284 // if our Text didn't do anything to the cursor
3285 // then we try to put the cursor into the cell below
3286 // setting also the right targetX.
3287 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3288 cur.idx() = tabular.getCellBelow(cur.idx());
3290 TextMetrics const & tm =
3291 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3292 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3294 if (sl == cur.top()) {
3295 // we trick it to go to the RIGHT after leaving the
3297 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3302 case LFUN_UP_SELECT:
3304 cell(cur.idx())->dispatch(cur, cmd);
3305 cur.dispatched(); // override the cell's decision
3306 if (sl == cur.top())
3307 // if our Text didn't do anything to the cursor
3308 // then we try to put the cursor into the cell above
3309 // setting also the right targetX.
3310 if (tabular.row_of_cell(cur.idx()) != 0) {
3311 cur.idx() = tabular.getCellAbove(cur.idx());
3312 cur.pit() = cur.lastpit();
3313 Text const * text = cell(cur.idx())->getText(0);
3314 TextMetrics const & tm = cur.bv().textMetrics(text);
3315 ParagraphMetrics const & pm =
3316 tm.parMetrics(cur.lastpit());
3317 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3319 if (sl == cur.top()) {
3320 cmd = FuncRequest(LFUN_UP);
3325 // case LFUN_SCREEN_DOWN: {
3326 // //if (hasSelection())
3327 // // cur.selection() = false;
3328 // col_type const col = tabular.column_of_cell(cur.idx());
3329 // int const t = cur.bv().top_y() + cur.bv().height();
3330 // if (t < yo() + tabular.getHeightOfTabular()) {
3331 // cur.bv().scrollDocView(t);
3332 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3334 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3341 // case LFUN_SCREEN_UP: {
3342 // //if (hasSelection())
3343 // // cur.selection() = false;
3344 // col_type const col = tabular.column_of_cell(cur.idx());
3345 // int const t = cur.bv().top_y() + cur.bv().height();
3347 // cur.bv().scrollDocView(t);
3351 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3355 // cur.par() = cur.lastpar();
3356 // cur.pos() = cur.lastpos();
3360 case LFUN_LAYOUT_TABULAR:
3361 InsetTabularMailer(*this).showDialog(&cur.bv());
3364 case LFUN_INSET_DIALOG_UPDATE:
3365 InsetTabularMailer(*this).updateDialog(&cur.bv());
3368 case LFUN_TABULAR_FEATURE:
3369 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3373 // insert file functions
3374 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3375 case LFUN_FILE_INSERT_PLAINTEXT: {
3377 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3378 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3384 if (tablemode(cur)) {
3385 if (copySelection(cur)) {
3386 recordUndoInset(cur, Undo::DELETE);
3391 cell(cur.idx())->dispatch(cur, cmd);
3394 case LFUN_CHAR_DELETE_BACKWARD:
3395 case LFUN_CHAR_DELETE_FORWARD:
3396 if (tablemode(cur)) {
3397 recordUndoInset(cur, Undo::DELETE);
3401 cell(cur.idx())->dispatch(cur, cmd);
3405 if (!cur.selection())
3407 if (tablemode(cur)) {
3411 cell(cur.idx())->dispatch(cur, cmd);
3414 case LFUN_CLIPBOARD_PASTE:
3415 case LFUN_PRIMARY_SELECTION_PASTE: {
3416 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3417 theClipboard().getAsText() :
3418 theSelection().get();
3421 // pass to InsertPlaintextString, but
3422 // only if we have multi-cell content
3423 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3424 if (insertPlaintextString(cur.bv(), clip, false)) {
3425 // content has been replaced,
3426 // so cursor might be invalid
3427 cur.pos() = cur.lastpos();
3428 bvcur.setCursor(cur);
3432 // Let the cell handle normal text
3433 cell(cur.idx())->dispatch(cur, cmd);
3438 if (tabularStackDirty() && theClipboard().isInternal()) {
3439 recordUndoInset(cur, Undo::INSERT);
3440 pasteClipboard(cur);
3443 cell(cur.idx())->dispatch(cur, cmd);
3446 case LFUN_FONT_EMPH:
3447 case LFUN_FONT_BOLD:
3448 case LFUN_FONT_ROMAN:
3449 case LFUN_FONT_NOUN:
3450 case LFUN_FONT_ITAL:
3451 case LFUN_FONT_FRAK:
3452 case LFUN_FONT_CODE:
3453 case LFUN_FONT_SANS:
3454 case LFUN_FONT_FREE_APPLY:
3455 case LFUN_FONT_FREE_UPDATE:
3456 case LFUN_FONT_SIZE:
3457 case LFUN_FONT_UNDERLINE:
3459 case LFUN_WORD_CAPITALIZE:
3460 case LFUN_WORD_UPCASE:
3461 case LFUN_WORD_LOWCASE:
3462 case LFUN_CHARS_TRANSPOSE:
3463 if (tablemode(cur)) {
3466 getSelection(cur, rs, re, cs, ce);
3467 Cursor tmpcur = cur;
3468 for (row_type i = rs; i <= re; ++i) {
3469 for (col_type j = cs; j <= ce; ++j) {
3470 // cursor follows cell:
3471 tmpcur.idx() = tabular.getCellNumber(i, j);
3472 // select this cell only:
3475 tmpcur.resetAnchor();
3476 tmpcur.pit() = tmpcur.lastpit();
3477 tmpcur.pos() = tmpcur.top().lastpos();
3478 tmpcur.setCursor(tmpcur);
3479 tmpcur.setSelection();
3480 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3485 cell(cur.idx())->dispatch(cur, cmd);
3489 // we try to handle this event in the insets dispatch function.
3490 cell(cur.idx())->dispatch(cur, cmd);
3496 // function sets an object as defined in func_status.h:
3497 // states OK, Unknown, Disabled, On, Off.
3498 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3499 FuncStatus & status) const
3501 switch (cmd.action) {
3502 case LFUN_TABULAR_FEATURE: {
3503 int action = Tabular::LAST_ACTION;
3505 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3506 string const tmp = tabularFeature[i].feature;
3507 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3508 action = tabularFeature[i].action;
3512 if (action == Tabular::LAST_ACTION) {
3514 status.unknown(true);
3518 string const argument
3519 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3521 row_type sel_row_start = 0;
3522 row_type sel_row_end = 0;
3523 col_type sel_col_start = 0;
3524 col_type sel_col_end = 0;
3525 Tabular::ltType dummyltt;
3528 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3531 case Tabular::SET_PWIDTH:
3532 case Tabular::SET_MPWIDTH:
3533 case Tabular::SET_SPECIAL_COLUMN:
3534 case Tabular::SET_SPECIAL_MULTI:
3535 case Tabular::APPEND_ROW:
3536 case Tabular::APPEND_COLUMN:
3537 case Tabular::DELETE_ROW:
3538 case Tabular::DELETE_COLUMN:
3539 case Tabular::COPY_ROW:
3540 case Tabular::COPY_COLUMN:
3541 case Tabular::SET_ALL_LINES:
3542 case Tabular::UNSET_ALL_LINES:
3543 case Tabular::SET_TOP_SPACE:
3544 case Tabular::SET_BOTTOM_SPACE:
3545 case Tabular::SET_INTERLINE_SPACE:
3549 case Tabular::MULTICOLUMN:
3550 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3553 case Tabular::M_TOGGLE_LINE_TOP:
3555 case Tabular::TOGGLE_LINE_TOP:
3556 status.setOnOff(tabular.topLine(cur.idx(), flag));
3559 case Tabular::M_TOGGLE_LINE_BOTTOM:
3561 case Tabular::TOGGLE_LINE_BOTTOM:
3562 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3565 case Tabular::M_TOGGLE_LINE_LEFT:
3567 case Tabular::TOGGLE_LINE_LEFT:
3568 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3571 case Tabular::M_TOGGLE_LINE_RIGHT:
3573 case Tabular::TOGGLE_LINE_RIGHT:
3574 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3577 case Tabular::M_ALIGN_LEFT:
3579 case Tabular::ALIGN_LEFT:
3580 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3583 case Tabular::M_ALIGN_RIGHT:
3585 case Tabular::ALIGN_RIGHT:
3586 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3589 case Tabular::M_ALIGN_CENTER:
3591 case Tabular::ALIGN_CENTER:
3592 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3595 case Tabular::ALIGN_BLOCK:
3596 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3597 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3600 case Tabular::M_VALIGN_TOP:
3602 case Tabular::VALIGN_TOP:
3604 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3607 case Tabular::M_VALIGN_BOTTOM:
3609 case Tabular::VALIGN_BOTTOM:
3611 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3614 case Tabular::M_VALIGN_MIDDLE:
3616 case Tabular::VALIGN_MIDDLE:
3618 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3621 case Tabular::SET_LONGTABULAR:
3622 status.setOnOff(tabular.isLongTabular());
3625 case Tabular::UNSET_LONGTABULAR:
3626 status.setOnOff(!tabular.isLongTabular());
3629 case Tabular::TOGGLE_ROTATE_TABULAR:
3630 case Tabular::SET_ROTATE_TABULAR:
3631 status.setOnOff(tabular.getRotateTabular());
3634 case Tabular::UNSET_ROTATE_TABULAR:
3635 status.setOnOff(!tabular.getRotateTabular());
3638 case Tabular::TOGGLE_ROTATE_CELL:
3639 case Tabular::SET_ROTATE_CELL:
3640 status.setOnOff(!oneCellHasRotationState(false,
3641 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3644 case Tabular::UNSET_ROTATE_CELL:
3645 status.setOnOff(!oneCellHasRotationState(true,
3646 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3649 case Tabular::SET_USEBOX:
3650 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3653 case Tabular::SET_LTFIRSTHEAD:
3654 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3657 case Tabular::UNSET_LTFIRSTHEAD:
3658 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3661 case Tabular::SET_LTHEAD:
3662 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3665 case Tabular::UNSET_LTHEAD:
3666 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3669 case Tabular::SET_LTFOOT:
3670 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3673 case Tabular::UNSET_LTFOOT:
3674 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3677 case Tabular::SET_LTLASTFOOT:
3678 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3681 case Tabular::UNSET_LTLASTFOOT:
3682 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3685 case Tabular::SET_LTNEWPAGE:
3686 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3689 case Tabular::SET_BOOKTABS:
3690 status.setOnOff(tabular.useBookTabs());
3693 case Tabular::UNSET_BOOKTABS:
3694 status.setOnOff(!tabular.useBookTabs());
3699 status.enabled(false);
3705 // These are only enabled inside tabular
3706 case LFUN_CELL_BACKWARD:
3707 case LFUN_CELL_FORWARD:
3708 status.enabled(true);
3711 // disable these with multiple cells selected
3712 case LFUN_INSET_INSERT:
3713 case LFUN_TABULAR_INSERT:
3714 case LFUN_CHARSTYLE_INSERT:
3715 case LFUN_FLOAT_INSERT:
3716 case LFUN_FLOAT_WIDE_INSERT:
3717 case LFUN_FOOTNOTE_INSERT:
3718 case LFUN_MARGINALNOTE_INSERT:
3719 case LFUN_MATH_INSERT:
3720 case LFUN_MATH_MODE:
3721 case LFUN_MATH_MUTATE:
3722 case LFUN_MATH_DISPLAY:
3723 case LFUN_NOTE_INSERT:
3724 case LFUN_OPTIONAL_INSERT:
3725 case LFUN_BOX_INSERT:
3726 case LFUN_BRANCH_INSERT:
3727 case LFUN_WRAP_INSERT:
3728 case LFUN_ERT_INSERT: {
3729 if (tablemode(cur)) {
3730 status.enabled(false);
3733 return cell(cur.idx())->getStatus(cur, cmd, status);
3736 // disable in non-fixed-width cells
3737 case LFUN_BREAK_LINE:
3738 case LFUN_BREAK_PARAGRAPH:
3739 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3740 case LFUN_BREAK_PARAGRAPH_SKIP: {
3741 if (tabular.getPWidth(cur.idx()).zero()) {
3742 status.enabled(false);
3745 return cell(cur.idx())->getStatus(cur, cmd, status);
3749 if (tabularStackDirty() && theClipboard().isInternal()) {
3750 status.enabled(true);
3753 return cell(cur.idx())->getStatus(cur, cmd, status);
3755 case LFUN_INSET_MODIFY:
3756 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3757 status.enabled(true);
3763 // we try to handle this event in the insets dispatch function.
3764 return cell(cur.idx())->getStatus(cur, cmd, status);
3769 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3770 OutputParams const & runparams) const
3772 return tabular.latex(buf, os, runparams);
3776 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3777 OutputParams const & runparams) const
3779 os << '\n'; // output table on a new line
3780 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3781 tabular.plaintext(buf, os, runparams, dp, false, 0);
3782 return PLAINTEXT_NEWLINE;
3786 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3787 OutputParams const & runparams) const
3792 #ifdef WITH_WARNINGS
3793 #warning Why not pass a proper DocIterator here?
3796 // if the table is inside a float it doesn't need the informaltable
3797 // wrapper. Search for it.
3798 for (master = owner(); master; master = master->owner())
3799 if (master->lyxCode() == Inset::FLOAT_CODE)
3804 os << "<informaltable>";
3807 ret += tabular.docbook(buf, os, runparams);
3809 os << "</informaltable>";
3816 void InsetTabular::validate(LaTeXFeatures & features) const
3818 tabular.validate(features);
3822 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3824 return tabular.getCellInset(idx);
3828 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3830 return tabular.getCellInset(idx);
3834 void InsetTabular::cursorPos(BufferView const & bv,
3835 CursorSlice const & sl, bool boundary, int & x, int & y) const
3837 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3839 // y offset correction
3840 int const row = tabular.row_of_cell(sl.idx());
3841 for (int i = 0; i <= row; ++i) {
3843 y += tabular.getAscentOfRow(i);
3844 y += tabular.getAdditionalHeight(i);
3847 y += tabular.getDescentOfRow(i);
3850 // x offset correction
3851 int const col = tabular.column_of_cell(sl.idx());
3852 int idx = tabular.getCellNumber(row, 0);
3853 for (int j = 0; j < col; ++j) {
3854 if (tabular.isPartOfMultiColumn(row, j))
3856 x += tabular.getWidthOfColumn(idx);
3859 x += tabular.getBeginningOfTextInCell(idx);
3860 x += ADD_TO_TABULAR_WIDTH;
3865 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3869 Inset const & inset = *tabular.getCellInset(cell);
3870 Point o = bv.coordCache().getInsets().xy(&inset);
3871 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3872 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3873 row_type const row = tabular.row_of_cell(cell);
3874 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3875 tabular.getAdditionalHeight(row);
3876 int const yend = o.y_ + tabular.getDescentOfRow(row);
3888 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3889 // << " ybeg=" << ybeg << " yend=" << yend
3890 // << " xx=" << xx << " yy=" << yy
3891 // << " dist=" << xx + yy << endl;
3896 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3898 //lyxerr << "InsetTabular::editXY: " << this << endl;
3899 cur.selection() = false;
3901 cur.idx() = getNearestCell(cur.bv(), x, y);
3903 return cell(cur.idx())->text_.editXY(cur, x, y);
3907 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3909 cur.idx() = getNearestCell(cur.bv(), x, y);
3910 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3914 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3916 idx_type idx_min = 0;
3917 int dist_min = std::numeric_limits<int>::max();
3918 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3919 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3920 int const d = dist(bv, i, x, y);
3931 int InsetTabular::getCellXPos(idx_type const cell) const
3935 for (; !tabular.isFirstCellInRow(c); --c)
3938 for (; c < cell; ++c)
3939 lx += tabular.getWidthOfColumn(c);
3945 void InsetTabular::resetPos(Cursor & cur) const
3947 BufferView & bv = cur.bv();
3948 int const maxwidth = bv.workWidth();
3950 if (&cur.inset() != this) {
3954 int const X2 = maxwidth;
3955 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3956 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3957 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3960 scx_ = X1 + 20 - x1;
3962 scx_ = X2 - 20 - x2;
3967 cur.updateFlags(Update::Force | Update::FitCursor);
3971 void InsetTabular::moveNextCell(Cursor & cur)
3973 if (isRightToLeft(cur)) {
3974 if (tabular.isFirstCellInRow(cur.idx())) {
3975 row_type const row = tabular.row_of_cell(cur.idx());
3976 if (row == tabular.rows() - 1)
3978 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3985 if (tabular.isLastCell(cur.idx()))
3995 void InsetTabular::movePrevCell(Cursor & cur)
3997 if (isRightToLeft(cur)) {
3998 if (tabular.isLastCellInRow(cur.idx())) {
3999 row_type const row = tabular.row_of_cell(cur.idx());
4002 cur.idx() = tabular.getFirstCellInRow(row);
4003 cur.idx() = tabular.getCellAbove(cur.idx());
4005 if (tabular.isLastCell(cur.idx()))
4010 if (cur.idx() == 0) // first cell
4014 cur.pit() = cur.lastpit();
4015 cur.pos() = cur.lastpos();
4017 // FIXME: this accesses the position cache before it is initialized
4022 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4024 Tabular::Feature action = Tabular::LAST_ACTION;
4027 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4028 string const tmp = tabularFeature[i].feature;
4030 if (tmp == what.substr(0, tmp.length())) {
4031 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4032 //tabularFeatures[i].feature.length()))
4033 action = tabularFeature[i].action;
4037 if (action == Tabular::LAST_ACTION)
4041 ltrim(what.substr(tabularFeature[i].feature.length()));
4042 tabularFeatures(cur, action, val);
4047 static void checkLongtableSpecial(Tabular::ltType & ltt,
4048 string const & special, bool & flag)
4050 if (special == "dl_above") {
4053 } else if (special == "dl_below") {
4054 ltt.bottomDL = flag;
4056 } else if (special == "empty") {
4065 bool InsetTabular::oneCellHasRotationState(bool rotated,
4066 row_type row_start, row_type row_end,
4067 col_type col_start, col_type col_end) const {
4069 for (row_type i = row_start; i <= row_end; ++i) {
4070 for (col_type j = col_start; j <= col_end; ++j) {
4071 if (tabular.getRotateCell(tabular.getCellNumber(i, j))
4080 void InsetTabular::tabularFeatures(Cursor & cur,
4081 Tabular::Feature feature, string const & value)
4083 BufferView & bv = cur.bv();
4084 col_type sel_col_start;
4085 col_type sel_col_end;
4086 row_type sel_row_start;
4087 row_type sel_row_end;
4088 bool setLines = false;
4089 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4090 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4094 case Tabular::M_ALIGN_LEFT:
4095 case Tabular::ALIGN_LEFT:
4096 setAlign = LYX_ALIGN_LEFT;
4099 case Tabular::M_ALIGN_RIGHT:
4100 case Tabular::ALIGN_RIGHT:
4101 setAlign = LYX_ALIGN_RIGHT;
4104 case Tabular::M_ALIGN_CENTER:
4105 case Tabular::ALIGN_CENTER:
4106 setAlign = LYX_ALIGN_CENTER;
4109 case Tabular::ALIGN_BLOCK:
4110 setAlign = LYX_ALIGN_BLOCK;
4113 case Tabular::M_VALIGN_TOP:
4114 case Tabular::VALIGN_TOP:
4115 setVAlign = Tabular::LYX_VALIGN_TOP;
4118 case Tabular::M_VALIGN_BOTTOM:
4119 case Tabular::VALIGN_BOTTOM:
4120 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4123 case Tabular::M_VALIGN_MIDDLE:
4124 case Tabular::VALIGN_MIDDLE:
4125 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4132 recordUndoInset(cur, Undo::ATOMIC);
4134 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4135 row_type const row = tabular.row_of_cell(cur.idx());
4136 col_type const column = tabular.column_of_cell(cur.idx());
4138 Tabular::ltType ltt;
4142 case Tabular::SET_PWIDTH: {
4143 Length const len(value);
4144 tabular.setColumnPWidth(cur, cur.idx(), len);
4146 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4147 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4151 case Tabular::SET_MPWIDTH:
4152 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4155 case Tabular::SET_SPECIAL_COLUMN:
4156 case Tabular::SET_SPECIAL_MULTI:
4157 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4160 case Tabular::APPEND_ROW:
4161 // append the row into the tabular
4162 tabular.appendRow(bv.buffer()->params(), cur.idx());
4165 case Tabular::APPEND_COLUMN:
4166 // append the column into the tabular
4167 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4168 cur.idx() = tabular.getCellNumber(row, column);
4171 case Tabular::DELETE_ROW:
4172 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4173 tabular.deleteRow(sel_row_start);
4174 if (sel_row_start >= tabular.rows())
4176 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4179 cur.selection() = false;
4182 case Tabular::DELETE_COLUMN:
4183 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4184 tabular.deleteColumn(sel_col_start);
4185 if (sel_col_start >= tabular.columns())
4187 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4190 cur.selection() = false;
4193 case Tabular::COPY_ROW:
4194 tabular.copyRow(bv.buffer()->params(), row);
4197 case Tabular::COPY_COLUMN:
4198 tabular.copyColumn(bv.buffer()->params(), column);
4199 cur.idx() = tabular.getCellNumber(row, column);
4202 case Tabular::M_TOGGLE_LINE_TOP:
4204 case Tabular::TOGGLE_LINE_TOP: {
4205 bool lineSet = !tabular.topLine(cur.idx(), flag);
4206 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4207 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4209 tabular.getCellNumber(i, j),
4214 case Tabular::M_TOGGLE_LINE_BOTTOM:
4216 case Tabular::TOGGLE_LINE_BOTTOM: {
4217 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4218 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4219 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4220 tabular.setBottomLine(
4221 tabular.getCellNumber(i, j),
4227 case Tabular::M_TOGGLE_LINE_LEFT:
4229 case Tabular::TOGGLE_LINE_LEFT: {
4230 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4231 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4232 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4233 tabular.setLeftLine(
4234 tabular.getCellNumber(i,j),
4240 case Tabular::M_TOGGLE_LINE_RIGHT:
4242 case Tabular::TOGGLE_LINE_RIGHT: {
4243 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4244 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4245 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4246 tabular.setRightLine(
4247 tabular.getCellNumber(i,j),
4253 case Tabular::M_ALIGN_LEFT:
4254 case Tabular::M_ALIGN_RIGHT:
4255 case Tabular::M_ALIGN_CENTER:
4257 case Tabular::ALIGN_LEFT:
4258 case Tabular::ALIGN_RIGHT:
4259 case Tabular::ALIGN_CENTER:
4260 case Tabular::ALIGN_BLOCK:
4261 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4262 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4263 tabular.setAlignment(
4264 tabular.getCellNumber(i, j),
4269 case Tabular::M_VALIGN_TOP:
4270 case Tabular::M_VALIGN_BOTTOM:
4271 case Tabular::M_VALIGN_MIDDLE:
4273 case Tabular::VALIGN_TOP:
4274 case Tabular::VALIGN_BOTTOM:
4275 case Tabular::VALIGN_MIDDLE:
4276 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4277 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4278 tabular.setVAlignment(
4279 tabular.getCellNumber(i, j),
4283 case Tabular::MULTICOLUMN: {
4284 if (sel_row_start != sel_row_end) {
4285 #ifdef WITH_WARNINGS
4286 #warning Need I say it ? This is horrible.
4289 Alert::error(_("Error setting multicolumn"),
4290 _("You cannot set multicolumn vertically."));
4293 if (!cur.selection()) {
4294 // just multicol for one single cell
4295 // check whether we are completely in a multicol
4296 if (tabular.isMultiColumn(cur.idx()))
4297 tabular.unsetMultiColumn(cur.idx());
4299 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4302 // we have a selection so this means we just add all this
4303 // cells to form a multicolumn cell
4304 idx_type const s_start = cur.selBegin().idx();
4305 idx_type const s_end = cur.selEnd().idx();
4306 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4307 cur.idx() = s_start;
4310 cur.selection() = false;
4314 case Tabular::SET_ALL_LINES:
4316 case Tabular::UNSET_ALL_LINES:
4317 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4318 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4319 tabular.setAllLines(
4320 tabular.getCellNumber(i,j), setLines);
4323 case Tabular::SET_LONGTABULAR:
4324 tabular.setLongTabular(true);
4327 case Tabular::UNSET_LONGTABULAR:
4328 tabular.setLongTabular(false);
4331 case Tabular::SET_ROTATE_TABULAR:
4332 tabular.setRotateTabular(true);
4335 case Tabular::UNSET_ROTATE_TABULAR:
4336 tabular.setRotateTabular(false);
4339 case Tabular::TOGGLE_ROTATE_TABULAR:
4340 tabular.setRotateTabular(!tabular.getRotateTabular());
4343 case Tabular::SET_ROTATE_CELL:
4344 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4345 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4346 tabular.setRotateCell(
4347 tabular.getCellNumber(i, j), true);
4350 case Tabular::UNSET_ROTATE_CELL:
4351 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4352 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4353 tabular.setRotateCell(
4354 tabular.getCellNumber(i, j), false);
4357 case Tabular::TOGGLE_ROTATE_CELL:
4359 bool oneNotRotated = oneCellHasRotationState(false,
4360 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4362 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4363 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4364 tabular.setRotateCell(tabular.getCellNumber(i, j),
4369 case Tabular::SET_USEBOX: {
4370 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4371 if (val == tabular.getUsebox(cur.idx()))
4372 val = Tabular::BOX_NONE;
4373 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4374 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4375 tabular.setUsebox(tabular.getCellNumber(i, j), val);
4379 case Tabular::UNSET_LTFIRSTHEAD:
4381 case Tabular::SET_LTFIRSTHEAD:
4382 tabular.getRowOfLTFirstHead(row, ltt);
4383 checkLongtableSpecial(ltt, value, flag);
4384 tabular.setLTHead(row, flag, ltt, true);
4387 case Tabular::UNSET_LTHEAD:
4389 case Tabular::SET_LTHEAD:
4390 tabular.getRowOfLTHead(row, ltt);
4391 checkLongtableSpecial(ltt, value, flag);
4392 tabular.setLTHead(row, flag, ltt, false);
4395 case Tabular::UNSET_LTFOOT:
4397 case Tabular::SET_LTFOOT:
4398 tabular.getRowOfLTFoot(row, ltt);
4399 checkLongtableSpecial(ltt, value, flag);
4400 tabular.setLTFoot(row, flag, ltt, false);
4403 case Tabular::UNSET_LTLASTFOOT:
4405 case Tabular::SET_LTLASTFOOT:
4406 tabular.getRowOfLTLastFoot(row, ltt);
4407 checkLongtableSpecial(ltt, value, flag);
4408 tabular.setLTFoot(row, flag, ltt, true);
4411 case Tabular::SET_LTNEWPAGE:
4412 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4415 case Tabular::SET_BOOKTABS:
4416 tabular.setBookTabs(true);
4419 case Tabular::UNSET_BOOKTABS:
4420 tabular.setBookTabs(false);
4423 case Tabular::SET_TOP_SPACE: {
4425 if (value == "default")
4426 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4427 tabular.row_info[i].top_space_default = true;
4428 else if (isValidLength(value, &len))
4429 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4430 tabular.row_info[i].top_space_default = false;
4431 tabular.row_info[i].top_space = len;
4434 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4435 tabular.row_info[i].top_space_default = false;
4436 tabular.row_info[i].top_space = len;
4441 case Tabular::SET_BOTTOM_SPACE: {
4443 if (value == "default")
4444 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4445 tabular.row_info[i].bottom_space_default = true;
4446 else if (isValidLength(value, &len))
4447 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4448 tabular.row_info[i].bottom_space_default = false;
4449 tabular.row_info[i].bottom_space = len;
4452 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4453 tabular.row_info[i].bottom_space_default = false;
4454 tabular.row_info[i].bottom_space = len;
4459 case Tabular::SET_INTERLINE_SPACE: {
4461 if (value == "default")
4462 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4463 tabular.row_info[i].interline_space_default = true;
4464 else if (isValidLength(value, &len))
4465 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4466 tabular.row_info[i].interline_space_default = false;
4467 tabular.row_info[i].interline_space = len;
4470 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4471 tabular.row_info[i].interline_space_default = false;
4472 tabular.row_info[i].interline_space = len;
4477 // dummy stuff just to avoid warnings
4478 case Tabular::LAST_ACTION:
4484 bool InsetTabular::showInsetDialog(BufferView * bv) const
4486 InsetTabularMailer(*this).showDialog(bv);
4491 void InsetTabular::openLayoutDialog(BufferView * bv) const
4493 InsetTabularMailer(*this).showDialog(bv);
4497 bool InsetTabular::copySelection(Cursor & cur)
4499 if (!cur.selection())
4504 getSelection(cur, rs, re, cs, ce);
4506 paste_tabular.reset(new Tabular(tabular));
4508 for (row_type i = 0; i < rs; ++i)
4509 paste_tabular->deleteRow(0);
4511 row_type const rows = re - rs + 1;
4512 while (paste_tabular->rows() > rows)
4513 paste_tabular->deleteRow(rows);
4515 paste_tabular->setTopLine(0, true, true);
4516 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4519 for (col_type i = 0; i < cs; ++i)
4520 paste_tabular->deleteColumn(0);
4522 col_type const columns = ce - cs + 1;
4523 while (paste_tabular->columns() > columns)
4524 paste_tabular->deleteColumn(columns);
4526 paste_tabular->setLeftLine(0, true, true);
4527 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4530 odocstringstream os;
4531 OutputParams const runparams(0);
4532 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4533 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4534 cap::copySelection(cur, os.str());
4536 // mark tabular stack dirty
4537 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4538 // when we (hopefully) have a one-for-all paste mechanism.
4539 // This must be called after cap::copySelection.
4540 dirtyTabularStack(true);
4546 bool InsetTabular::pasteClipboard(Cursor & cur)
4550 col_type const actcol = tabular.column_of_cell(cur.idx());
4551 row_type const actrow = tabular.row_of_cell(cur.idx());
4552 for (row_type r1 = 0, r2 = actrow;
4553 r1 < paste_tabular->rows() && r2 < tabular.rows();
4555 for (col_type c1 = 0, c2 = actcol;
4556 c1 < paste_tabular->columns() && c2 < tabular.columns();
4558 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4559 tabular.isPartOfMultiColumn(r2, c2))
4561 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4565 if (tabular.isPartOfMultiColumn(r2, c2)) {
4569 shared_ptr<InsetText> inset(
4570 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4571 tabular.setCellInset(r2, c2, inset);
4572 // FIXME: change tracking (MG)
4573 inset->setChange(Change(cur.buffer().params().trackChanges ?
4574 Change::INSERTED : Change::UNCHANGED));
4582 void InsetTabular::cutSelection(Cursor & cur)
4584 if (!cur.selection())
4589 getSelection(cur, rs, re, cs, ce);
4590 for (row_type i = rs; i <= re; ++i) {
4591 for (col_type j = cs; j <= ce; ++j) {
4592 shared_ptr<InsetText> t
4593 = cell(tabular.getCellNumber(i, j));
4594 if (cur.buffer().params().trackChanges)
4595 // FIXME: Change tracking (MG)
4596 t->setChange(Change(Change::DELETED));
4602 // cursor position might be invalid now
4603 if (cur.pit() > cur.lastpit())
4604 cur.pit() = cur.lastpit();
4605 if (cur.pos() > cur.lastpos())
4606 cur.pos() = cur.lastpos();
4607 cur.clearSelection();
4611 bool InsetTabular::isRightToLeft(Cursor & cur) const
4613 BOOST_ASSERT(cur.depth() > 1);
4614 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4615 pos_type const parentpos = cur[cur.depth() - 2].pos();
4616 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4617 parentpos).language()->rightToLeft();
4621 void InsetTabular::getSelection(Cursor & cur,
4622 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4624 CursorSlice const & beg = cur.selBegin();
4625 CursorSlice const & end = cur.selEnd();
4626 cs = tabular.column_of_cell(beg.idx());
4627 ce = tabular.column_of_cell(end.idx());
4630 cs = tabular.column_of_cell(end.idx());
4632 ce = tabular.right_column_of_cell(end.idx());
4635 rs = tabular.row_of_cell(beg.idx());
4636 re = tabular.row_of_cell(end.idx());
4642 Text * InsetTabular::getText(int idx) const
4644 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4648 void InsetTabular::setChange(Change const & change)
4650 for (idx_type idx = 0; idx < nargs(); ++idx)
4651 cell(idx)->setChange(change);
4655 void InsetTabular::acceptChanges(BufferParams const & bparams)
4657 for (idx_type idx = 0; idx < nargs(); ++idx)
4658 cell(idx)->acceptChanges(bparams);
4662 void InsetTabular::rejectChanges(BufferParams const & bparams)
4664 for (idx_type idx = 0; idx < nargs(); ++idx)
4665 cell(idx)->rejectChanges(bparams);
4669 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4671 return tabular.getPWidth(cell).zero();
4675 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4678 if (buf.length() <= 0)
4681 Buffer const & buffer = *bv.buffer();
4685 col_type maxCols = 1;
4686 docstring::size_type const len = buf.length();
4687 docstring::size_type p = 0;
4690 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4698 maxCols = max(cols, maxCols);
4704 maxCols = max(cols, maxCols);
4710 paste_tabular.reset(
4711 new Tabular(buffer.params(), rows, maxCols));
4712 loctab = paste_tabular.get();
4714 dirtyTabularStack(true);
4717 cell = bv.cursor().idx();
4718 ocol = tabular.column_of_cell(cell);
4719 row = tabular.row_of_cell(cell);
4722 docstring::size_type op = 0;
4723 idx_type const cells = loctab->getNumberOfCells();
4726 rows = loctab->rows();
4727 col_type const columns = loctab->columns();
4729 while (cell < cells && p < len && row < rows &&
4730 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4736 // we can only set this if we are not too far right
4737 if (cols < columns) {
4738 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4739 Paragraph & par = inset->text_.getPar(0);
4740 Font const font = inset->text_.getFont(buffer, par, 0);
4741 inset->setText(buf.substr(op, p - op), font,
4742 buffer.params().trackChanges);
4748 // we can only set this if we are not too far right
4749 if (cols < columns) {
4750 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4751 Paragraph & par = inset->text_.getPar(0);
4752 Font const font = inset->text_.getFont(buffer, par, 0);
4753 inset->setText(buf.substr(op, p - op), font,
4754 buffer.params().trackChanges);
4759 cell = loctab->getCellNumber(row, cols);
4765 // check for the last cell if there is no trailing '\n'
4766 if (cell < cells && op < len) {
4767 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4768 Paragraph & par = inset->text_.getPar(0);
4769 Font const font = inset->text_.getFont(buffer, par, 0);
4770 inset->setText(buf.substr(op, len - op), font,
4771 buffer.params().trackChanges);
4777 void InsetTabular::addPreview(PreviewLoader & loader) const
4779 row_type const rows = tabular.rows();
4780 col_type const columns = tabular.columns();
4781 for (row_type i = 0; i < rows; ++i) {
4782 for (col_type j = 0; j < columns; ++j)
4783 tabular.getCellInset(i, j)->addPreview(loader);
4788 bool InsetTabular::tablemode(Cursor & cur) const
4790 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4797 string const InsetTabularMailer::name_("tabular");
4799 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4800 : inset_(const_cast<InsetTabular &>(inset))
4804 string const InsetTabularMailer::inset2string(Buffer const &) const
4806 return params2string(inset_);
4810 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4812 istringstream data(in);
4814 lex.setStream(data);
4821 if (!lex || token != name_)
4822 return print_mailer_error("InsetTabularMailer", in, 1,
4825 // This is part of the inset proper that is usually swallowed
4826 // by Buffer::readInset
4828 if (!lex || token != "Tabular")
4829 return print_mailer_error("InsetTabularMailer", in, 2,
4832 Buffer const & buffer = inset.buffer();
4833 inset.read(buffer, lex);
4837 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4840 data << name_ << ' ';
4841 inset.write(inset.buffer(), data);
4842 data << "\\end_inset\n";