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"
61 using support::prefixIs;
64 using support::suffixIs;
66 using boost::shared_ptr;
67 using boost::dynamic_pointer_cast;
74 using std::istringstream;
77 using std::ostringstream;
82 #ifndef CXX_GLOBAL_CSTD
86 using cap::dirtyTabularStack;
87 using cap::tabularStackDirty;
88 using cap::saveSelection;
90 using graphics::PreviewLoader;
92 using frontend::Painter;
93 using frontend::Clipboard;
95 namespace Alert = frontend::Alert;
100 int const ADD_TO_HEIGHT = 2;
101 int const ADD_TO_TABULAR_WIDTH = 2;
102 int const default_line_space = 10;
103 int const WIDTH_OF_LINE = 5;
107 boost::scoped_ptr<Tabular> paste_tabular;
110 struct TabularFeature {
111 Tabular::Feature action;
116 TabularFeature tabularFeature[] =
118 { Tabular::APPEND_ROW, "append-row" },
119 { Tabular::APPEND_COLUMN, "append-column" },
120 { Tabular::DELETE_ROW, "delete-row" },
121 { Tabular::DELETE_COLUMN, "delete-column" },
122 { Tabular::COPY_ROW, "copy-row" },
123 { Tabular::COPY_COLUMN, "copy-column" },
124 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
125 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
126 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
127 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
128 { Tabular::ALIGN_LEFT, "align-left" },
129 { Tabular::ALIGN_RIGHT, "align-right" },
130 { Tabular::ALIGN_CENTER, "align-center" },
131 { Tabular::ALIGN_BLOCK, "align-block" },
132 { Tabular::VALIGN_TOP, "valign-top" },
133 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
134 { Tabular::VALIGN_MIDDLE, "valign-middle" },
135 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
136 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
137 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
138 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
139 { Tabular::M_ALIGN_LEFT, "m-align-left" },
140 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
141 { Tabular::M_ALIGN_CENTER, "m-align-center" },
142 { Tabular::M_VALIGN_TOP, "m-valign-top" },
143 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
144 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
145 { Tabular::MULTICOLUMN, "multicolumn" },
146 { Tabular::SET_ALL_LINES, "set-all-lines" },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
148 { Tabular::SET_LONGTABULAR, "set-longtabular" },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
150 { Tabular::SET_PWIDTH, "set-pwidth" },
151 { Tabular::SET_MPWIDTH, "set-mpwidth" },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
158 { Tabular::SET_USEBOX, "set-usebox" },
159 { Tabular::SET_LTHEAD, "set-lthead" },
160 { Tabular::UNSET_LTHEAD, "unset-lthead" },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
163 { Tabular::SET_LTFOOT, "set-ltfoot" },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
168 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
169 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
170 { Tabular::SET_BOOKTABS, "set-booktabs" },
171 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
172 { Tabular::SET_TOP_SPACE, "set-top-space" },
173 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
174 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
175 { Tabular::LAST_ACTION, "" }
179 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
181 FeatureEqual(Tabular::Feature feature)
182 : feature_(feature) {}
183 bool operator()(TabularFeature const & tf) const {
184 return tf.action == feature_;
187 Tabular::Feature feature_;
192 string const write_attribute(string const & name, T const & t)
194 string const s = tostr(t);
195 return s.empty() ? s : " " + name + "=\"" + s + "\"";
199 string const write_attribute(string const & name, string const & t)
201 return t.empty() ? t : " " + name + "=\"" + t + "\"";
206 string const write_attribute(string const & name, docstring const & t)
208 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
213 string const write_attribute(string const & name, bool const & b)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return b ? write_attribute(name, convert<string>(b)) : string();
222 string const write_attribute(string const & name, int const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Tabular::idx_type const & i)
233 // we write only true attribute values so we remove a bit of the
234 // file format bloat for tabulars.
235 return i ? write_attribute(name, convert<string>(i)) : string();
240 string const write_attribute(string const & name, Length const & value)
242 // we write only the value if we really have one same reson as above.
243 return value.zero() ? string() : write_attribute(name, value.asString());
247 string const tostr(LyXAlignment const & num)
252 case LYX_ALIGN_BLOCK:
256 case LYX_ALIGN_CENTER:
258 case LYX_ALIGN_RIGHT:
260 case LYX_ALIGN_LAYOUT:
262 case LYX_ALIGN_SPECIAL:
269 string const tostr(Tabular::VAlignment const & num)
272 case Tabular::LYX_VALIGN_TOP:
274 case Tabular::LYX_VALIGN_MIDDLE:
276 case Tabular::LYX_VALIGN_BOTTOM:
283 string const tostr(Tabular::BoxType const & num)
286 case Tabular::BOX_NONE:
288 case Tabular::BOX_PARBOX:
290 case Tabular::BOX_MINIPAGE:
297 // I would have liked a fromstr template a lot better. (Lgb)
298 bool string2type(string const str, LyXAlignment & num)
301 num = LYX_ALIGN_NONE;
302 else if (str == "block")
303 num = LYX_ALIGN_BLOCK;
304 else if (str == "left")
305 num = LYX_ALIGN_LEFT;
306 else if (str == "center")
307 num = LYX_ALIGN_CENTER;
308 else if (str == "right")
309 num = LYX_ALIGN_RIGHT;
316 bool string2type(string const str, Tabular::VAlignment & num)
319 num = Tabular::LYX_VALIGN_TOP;
320 else if (str == "middle" )
321 num = Tabular::LYX_VALIGN_MIDDLE;
322 else if (str == "bottom")
323 num = Tabular::LYX_VALIGN_BOTTOM;
330 bool string2type(string const str, Tabular::BoxType & num)
333 num = Tabular::BOX_NONE;
334 else if (str == "parbox")
335 num = Tabular::BOX_PARBOX;
336 else if (str == "minipage")
337 num = Tabular::BOX_MINIPAGE;
344 bool string2type(string const str, bool & num)
348 else if (str == "false")
356 bool getTokenValue(string const & str, char const * token, string & ret)
359 size_t token_length = strlen(token);
360 string::size_type pos = str.find(token);
362 if (pos == string::npos || pos + token_length + 1 >= str.length()
363 || str[pos + token_length] != '=')
365 pos += token_length + 1;
367 if (ch != '"' && ch != '\'') { // only read till next space
371 while (pos < str.length() - 1 && str[++pos] != ch)
378 bool getTokenValue(string const & str, char const * token, docstring & ret)
381 bool const success = getTokenValue(str, token, tmp);
382 ret = from_utf8(tmp);
387 bool getTokenValue(string const & str, char const * token, int & num)
391 if (!getTokenValue(str, token, tmp))
393 num = convert<int>(tmp);
398 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
401 return getTokenValue(str, token, tmp) && string2type(tmp, num);
405 bool getTokenValue(string const & str, char const * token,
406 Tabular::VAlignment & num)
409 return getTokenValue(str, token, tmp) && string2type(tmp, num);
413 bool getTokenValue(string const & str, char const * token,
414 Tabular::BoxType & num)
417 return getTokenValue(str, token, tmp) && string2type(tmp, num);
421 bool getTokenValue(string const & str, char const * token, bool & flag)
423 // set the flag always to false as this should be the default for bools
424 // not in the file-format.
427 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
431 bool getTokenValue(string const & str, char const * token, Length & len)
433 // set the lenght to be zero() as default as this it should be if not
434 // in the file format.
437 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
441 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
446 if (!getTokenValue(str, token, tmp))
448 if (tmp == "default") {
452 return isValidLength(tmp, &len);
456 void l_getline(istream & is, string & str)
459 while (str.empty()) {
461 if (!str.empty() && str[str.length() - 1] == '\r')
462 str.erase(str.length() - 1);
469 string const featureAsString(Tabular::Feature feature)
471 TabularFeature * end = tabularFeature +
472 sizeof(tabularFeature) / sizeof(TabularFeature);
473 TabularFeature * it = std::find_if(tabularFeature, end,
474 FeatureEqual(feature));
475 return (it == end) ? string() : it->feature;
480 /////////////////////////////////////////////////////////////////////
484 /////////////////////////////////////////////////////////////////////
487 Tabular::cellstruct::cellstruct(BufferParams const & bp)
490 multicolumn(Tabular::CELL_NORMAL),
491 alignment(LYX_ALIGN_CENTER),
492 valignment(LYX_VALIGN_TOP),
499 inset(new InsetText(bp))
503 Tabular::cellstruct::cellstruct(cellstruct const & cs)
505 width_of_cell(cs.width_of_cell),
506 multicolumn(cs.multicolumn),
507 alignment(cs.alignment),
508 valignment(cs.valignment),
509 top_line(cs.top_line),
510 bottom_line(cs.bottom_line),
511 left_line(cs.left_line),
512 right_line(cs.right_line),
515 align_special(cs.align_special),
517 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
521 Tabular::cellstruct &
522 Tabular::cellstruct::operator=(cellstruct cs)
530 Tabular::cellstruct::swap(cellstruct & rhs)
532 std::swap(cellno, rhs.cellno);
533 std::swap(width_of_cell, rhs.width_of_cell);
534 std::swap(multicolumn, rhs.multicolumn);
535 std::swap(alignment, rhs.alignment);
536 std::swap(valignment, rhs.valignment);
537 std::swap(top_line, rhs.top_line);
538 std::swap(bottom_line, rhs.bottom_line);
539 std::swap(left_line, rhs.left_line);
540 std::swap(right_line, rhs.right_line);
541 std::swap(usebox, rhs.usebox);
542 std::swap(rotate, rhs.rotate);
543 std::swap(align_special, rhs.align_special);
544 p_width.swap(rhs.p_width);
545 inset.swap(rhs.inset);
549 Tabular::rowstruct::rowstruct()
554 top_space_default(false),
555 bottom_space_default(false),
556 interline_space_default(false),
565 Tabular::columnstruct::columnstruct()
566 : alignment(LYX_ALIGN_CENTER),
567 valignment(LYX_VALIGN_TOP),
575 Tabular::ltType::ltType()
582 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
583 col_type columns_arg)
585 init(bp, rows_arg, columns_arg);
589 // activates all lines and sets all widths to 0
590 void Tabular::init(BufferParams const & bp, row_type rows_arg,
591 col_type columns_arg)
594 columns_ = columns_arg;
595 row_info = row_vector(rows_);
596 column_info = column_vector(columns_);
597 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
598 row_info.reserve(10);
599 column_info.reserve(10);
600 cell_info.reserve(100);
602 for (row_type i = 0; i < rows_; ++i)
603 cell_info[i].back().right_line = true;
604 row_info.back().bottom_line = true;
605 row_info.front().bottom_line = true;
606 column_info.back().right_line = true;
607 is_long_tabular = false;
609 use_booktabs = false;
613 void Tabular::fixCellNums()
616 for (row_type i = 0; i < rows_; ++i) {
617 for (col_type j = 0; j < columns_; ++j) {
618 // When debugging it can be nice to set
620 cell_info[i][j].inset->setDrawFrame(false);
621 cell_info[i][j].cellno = cellno++;
623 cell_info[i].back().right_line = true;
626 set_row_column_number_info();
630 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
634 row_type const row = row_of_cell(cell);
636 row_vector::iterator rit = row_info.begin() + row;
637 row_info.insert(rit, rowstruct());
638 // now set the values of the row before
639 row_info[row] = row_info[row + 1];
641 cell_vvector old(rows_ - 1);
642 for (row_type i = 0; i < rows_ - 1; ++i)
643 swap(cell_info[i], old[i]);
645 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
647 for (row_type i = 0; i <= row; ++i)
648 swap(cell_info[i], old[i]);
649 for (row_type i = row + 2; i < rows_; ++i)
650 swap(cell_info[i], old[i - 1]);
653 for (col_type j = 0; j < columns_; ++j)
654 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
656 set_row_column_number_info();
660 void Tabular::deleteRow(row_type const row)
662 // Not allowed to delete last row
666 row_info.erase(row_info.begin() + row);
667 cell_info.erase(cell_info.begin() + row);
673 void Tabular::copyRow(BufferParams const & bp, row_type const row)
677 row_info.insert(row_info.begin() + row, row_info[row]);
678 cell_info.insert(cell_info.begin() + row, cell_info[row]);
681 for (col_type j = 0; j < columns_; ++j)
682 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
684 set_row_column_number_info();
688 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
692 col_type const column = column_of_cell(cell);
693 column_vector::iterator cit = column_info.begin() + column + 1;
694 column_info.insert(cit, columnstruct());
695 // set the column values of the column before
696 column_info[column + 1] = column_info[column];
698 for (row_type i = 0; i < rows_; ++i) {
699 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
701 // care about multicolumns
702 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
703 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
705 if (column + 2 >= columns_
706 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
707 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
710 for (row_type i = 0; i < rows_; ++i) {
711 cell_info[i][column + 1].inset->clear();
713 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
719 void Tabular::deleteColumn(col_type const column)
721 // Not allowed to delete last column
725 column_info.erase(column_info.begin() + column);
726 for (row_type i = 0; i < rows_; ++i)
727 cell_info[i].erase(cell_info[i].begin() + column);
733 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
737 column_info.insert(column_info.begin() + column, column_info[column]);
739 for (row_type i = 0; i < rows_; ++i)
740 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
743 for (row_type i = 0; i < rows_; ++i)
744 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
749 void Tabular::set_row_column_number_info()
752 for (row_type row = 0; row < rows_; ++row) {
753 for (col_type column = 0; column < columns_; ++column) {
754 if (cell_info[row][column].multicolumn
755 != Tabular::CELL_PART_OF_MULTICOLUMN)
757 if (numberofcells == 0)
758 // FIXME: Is this intended?
759 cell_info[row][column].cellno = npos;
761 cell_info[row][column].cellno =
766 rowofcell.resize(numberofcells);
767 columnofcell.resize(numberofcells);
772 c < numberofcells && row < rows_ && column < columns_;) {
774 columnofcell[c] = column;
778 } while (column < columns_ &&
779 cell_info[row][column].multicolumn
780 == Tabular::CELL_PART_OF_MULTICOLUMN);
782 if (column == columns_) {
788 for (row_type row = 0; row < rows_; ++row) {
789 for (col_type column = 0; column < columns_; ++column) {
790 if (isPartOfMultiColumn(row,column))
792 cell_info[row][column].inset->setAutoBreakRows(
793 !getPWidth(getCellNumber(row, column)).zero());
799 Tabular::idx_type Tabular::getNumberOfCells() const
801 return numberofcells;
805 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
807 row_type const row = row_of_cell(cell);
809 for (col_type i = 0; i < columns_; ++i)
810 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
816 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
818 if (!wholerow && isMultiColumn(cell) &&
819 !(use_booktabs && row_of_cell(cell) == 0))
820 return cellinfo_of_cell(cell).top_line;
821 return row_info[row_of_cell(cell)].top_line;
825 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
827 if (!wholerow && isMultiColumn(cell) &&
828 !(use_booktabs && isLastRow(cell)))
829 return cellinfo_of_cell(cell).bottom_line;
830 return row_info[row_of_cell(cell)].bottom_line;
834 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
838 if (!wholecolumn && isMultiColumn(cell) &&
839 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
840 return cellinfo_of_cell(cell).left_line;
841 return column_info[column_of_cell(cell)].left_line;
845 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
849 if (!wholecolumn && isMultiColumn(cell) &&
850 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
851 return cellinfo_of_cell(cell).right_line;
852 return column_info[right_column_of_cell(cell)].right_line;
856 bool Tabular::topAlreadyDrawn(idx_type cell) const
858 row_type row = row_of_cell(cell);
859 if (row > 0 && !getAdditionalHeight(row)) {
860 col_type column = column_of_cell(cell);
863 && cell_info[row][column].multicolumn
864 == Tabular::CELL_PART_OF_MULTICOLUMN)
866 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
867 return row_info[row].bottom_line;
869 return cell_info[row][column].bottom_line;
875 bool Tabular::leftAlreadyDrawn(idx_type cell) const
877 col_type column = column_of_cell(cell);
879 row_type row = row_of_cell(cell);
881 (cell_info[row][column].multicolumn ==
882 Tabular::CELL_PART_OF_MULTICOLUMN));
883 if (getAdditionalWidth(cell_info[row][column].cellno))
885 return rightLine(cell_info[row][column].cellno);
891 bool Tabular::isLastRow(idx_type cell) const
893 return row_of_cell(cell) == rows_ - 1;
897 int Tabular::getAdditionalHeight(row_type row) const
899 if (!row || row >= rows_)
905 for (col_type column = 0; column < columns_ && bottom; ++column) {
906 switch (cell_info[row - 1][column].multicolumn) {
907 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
908 bottom = cell_info[row - 1][column].bottom_line;
910 case Tabular::CELL_NORMAL:
911 bottom = row_info[row - 1].bottom_line;
914 for (col_type column = 0; column < columns_ && top; ++column) {
915 switch (cell_info[row][column].multicolumn) {
916 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
917 top = cell_info[row][column].top_line;
919 case Tabular::CELL_NORMAL:
920 top = row_info[row].top_line;
923 int const interline_space = row_info[row - 1].interline_space_default ?
925 row_info[row - 1].interline_space.inPixels(width_of_tabular);
927 return interline_space + WIDTH_OF_LINE;
928 return interline_space;
932 int Tabular::getAdditionalWidth(idx_type cell) const
934 // internally already set in setWidthOfCell
935 // used to get it back in text.cpp
936 col_type const col = right_column_of_cell(cell);
937 row_type const row = row_of_cell(cell);
938 if (col < columns_ - 1 && rightLine(cell) &&
939 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
941 return WIDTH_OF_LINE;
947 // returns the maximum over all rows
948 int Tabular::getWidthOfColumn(idx_type cell) const
950 col_type const column1 = column_of_cell(cell);
951 col_type const column2 = right_column_of_cell(cell);
953 for (col_type i = column1; i <= column2; ++i)
954 result += column_info[i].width_of_column;
959 int Tabular::getWidthOfTabular() const
961 return width_of_tabular;
965 // returns true if a complete update is necessary, otherwise false
966 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
968 if (!isMultiColumn(cell))
971 row_type const row = row_of_cell(cell);
972 col_type const column1 = column_of_cell(cell);
973 col_type const column2 = right_column_of_cell(cell);
974 int const old_val = cell_info[row][column2].width_of_cell;
976 // first set columns to 0 so we can calculate the right width
977 for (col_type i = column1; i <= column2; ++i) {
978 cell_info[row][i].width_of_cell = 0;
980 // set the width to MAX_WIDTH until width > 0
981 int width = new_width + 2 * WIDTH_OF_LINE;
982 col_type i = column1;
983 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
984 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
985 width -= column_info[i].width_of_column;
988 cell_info[row][i].width_of_cell = width;
990 if (old_val != cell_info[row][column2].width_of_cell) {
991 // in this case we have to recalculate all multicolumn cells which
992 // have this column as one of theirs but not as last one
993 calculate_width_of_column_NMC(i);
994 recalculateMulticolumnsOfColumn(i);
995 calculate_width_of_column(i);
1001 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1003 // the last column does not have to be recalculated because all
1004 // multicolumns will have here there last multicolumn cell which
1005 // always will have the whole rest of the width of the cell.
1006 if (columns_ < 2 || column > (columns_ - 2))
1008 for (row_type row = 0; row < rows_; ++row) {
1009 int mc = cell_info[row][column].multicolumn;
1010 int nmc = cell_info[row][column+1].multicolumn;
1011 // we only have to update multicolumns which do not have this
1012 // column as their last column!
1013 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1014 (mc == CELL_PART_OF_MULTICOLUMN &&
1015 nmc == CELL_PART_OF_MULTICOLUMN))
1017 idx_type const cellno = cell_info[row][column].cellno;
1018 setWidthOfMulticolCell(cellno,
1019 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1025 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1027 row_type const row = row_of_cell(cell);
1028 col_type const column1 = column_of_cell(cell);
1033 if (rightLine(cell_info[row][column1].cellno, true) &&
1034 column1 < columns_ - 1 &&
1035 leftLine(cell_info[row][column1+1].cellno, true))
1037 add_width = WIDTH_OF_LINE;
1040 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1043 if (isMultiColumnReal(cell)) {
1044 tmp = setWidthOfMulticolCell(cell, new_width);
1046 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1047 cell_info[row][column1].width_of_cell = width;
1048 tmp = calculate_width_of_column_NMC(column1);
1050 recalculateMulticolumnsOfColumn(column1);
1053 for (col_type i = 0; i < columns_; ++i)
1054 calculate_width_of_column(i);
1055 calculate_width_of_tabular();
1060 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1063 if (!isMultiColumn(cell) || onlycolumn)
1064 column_info[column_of_cell(cell)].alignment = align;
1066 cellinfo_of_cell(cell).alignment = align;
1070 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1073 if (!isMultiColumn(cell) || onlycolumn)
1074 column_info[column_of_cell(cell)].valignment = align;
1076 cellinfo_of_cell(cell).valignment = align;
1083 * Allow line and paragraph breaks for fixed width cells or disallow them,
1084 * merge cell paragraphs and reset layout to standard for variable width
1087 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1089 inset->setAutoBreakRows(fixedWidth);
1093 // merge all paragraphs to one
1094 BufferParams const & bp = cur.bv().buffer()->params();
1095 while (inset->paragraphs().size() > 1)
1096 mergeParagraph(bp, inset->paragraphs(), 0);
1100 // undo information has already been recorded
1101 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1102 bp.getTextClass().defaultLayoutName());
1109 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1110 Length const & width)
1112 col_type const j = column_of_cell(cell);
1114 column_info[j].p_width = width;
1115 for (row_type i = 0; i < rows_; ++i) {
1116 idx_type const cell = getCellNumber(i, j);
1117 // because of multicolumns
1118 toggleFixedWidth(cur, getCellInset(cell).get(),
1119 !getPWidth(cell).zero());
1121 // cur paragraph can become invalid after paragraphs were merged
1122 if (cur.pit() > cur.lastpit())
1123 cur.pit() = cur.lastpit();
1124 // cur position can become invalid after newlines were removed
1125 if (cur.pos() > cur.lastpos())
1126 cur.pos() = cur.lastpos();
1130 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1131 Length const & width)
1133 if (!isMultiColumn(cell))
1136 cellinfo_of_cell(cell).p_width = width;
1137 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1138 // cur paragraph can become invalid after paragraphs were merged
1139 if (cur.pit() > cur.lastpit())
1140 cur.pit() = cur.lastpit();
1141 // cur position can become invalid after newlines were removed
1142 if (cur.pos() > cur.lastpos())
1143 cur.pos() = cur.lastpos();
1148 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1149 Tabular::Feature what)
1151 if (what == SET_SPECIAL_MULTI)
1152 cellinfo_of_cell(cell).align_special = special;
1154 column_info[column_of_cell(cell)].align_special = special;
1158 void Tabular::setAllLines(idx_type cell, bool line)
1160 setTopLine(cell, line);
1161 setBottomLine(cell, line);
1162 setRightLine(cell, line);
1163 setLeftLine(cell, line);
1167 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1169 row_type const row = row_of_cell(cell);
1170 if (wholerow || !isMultiColumn(cell))
1171 row_info[row].top_line = line;
1173 cellinfo_of_cell(cell).top_line = line;
1177 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1179 if (wholerow || !isMultiColumn(cell))
1180 row_info[row_of_cell(cell)].bottom_line = line;
1182 cellinfo_of_cell(cell).bottom_line = line;
1186 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1188 if (wholecolumn || !isMultiColumn(cell))
1189 column_info[column_of_cell(cell)].left_line = line;
1191 cellinfo_of_cell(cell).left_line = line;
1195 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1197 if (wholecolumn || !isMultiColumn(cell))
1198 column_info[right_column_of_cell(cell)].right_line = line;
1200 cellinfo_of_cell(cell).right_line = line;
1204 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1206 if (!onlycolumn && isMultiColumn(cell))
1207 return cellinfo_of_cell(cell).alignment;
1208 return column_info[column_of_cell(cell)].alignment;
1213 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1215 if (!onlycolumn && isMultiColumn(cell))
1216 return cellinfo_of_cell(cell).valignment;
1217 return column_info[column_of_cell(cell)].valignment;
1221 Length const Tabular::getPWidth(idx_type cell) const
1223 if (isMultiColumn(cell))
1224 return cellinfo_of_cell(cell).p_width;
1225 return column_info[column_of_cell(cell)].p_width;
1229 Length const Tabular::getColumnPWidth(idx_type cell) const
1231 return column_info[column_of_cell(cell)].p_width;
1235 Length const Tabular::getMColumnPWidth(idx_type cell) const
1237 if (isMultiColumn(cell))
1238 return cellinfo_of_cell(cell).p_width;
1243 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1245 if (what == SET_SPECIAL_MULTI)
1246 return cellinfo_of_cell(cell).align_special;
1247 return column_info[column_of_cell(cell)].align_special;
1251 int Tabular::getWidthOfCell(idx_type cell) const
1253 row_type const row = row_of_cell(cell);
1254 col_type const column1 = column_of_cell(cell);
1255 col_type const column2 = right_column_of_cell(cell);
1257 for (col_type i = column1; i <= column2; ++i)
1258 result += cell_info[row][i].width_of_cell;
1263 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1267 switch (getAlignment(cell)) {
1268 case LYX_ALIGN_CENTER:
1269 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1271 case LYX_ALIGN_RIGHT:
1272 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1273 // + getAdditionalWidth(cell);
1276 // LYX_ALIGN_LEFT: nothing :-)
1280 // the LaTeX Way :-(
1286 bool Tabular::isFirstCellInRow(idx_type cell) const
1288 return column_of_cell(cell) == 0;
1292 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1294 if (row > rows_ - 1)
1296 return cell_info[row][0].cellno;
1300 bool Tabular::isLastCellInRow(idx_type cell) const
1302 return right_column_of_cell(cell) == columns_ - 1;
1306 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1308 if (row > rows_ - 1)
1310 return cell_info[row][columns_-1].cellno;
1314 void Tabular::calculate_width_of_column(col_type column)
1317 for (row_type i = 0; i < rows_; ++i)
1318 maximum = max(cell_info[i][column].width_of_cell, maximum);
1319 column_info[column].width_of_column = maximum;
1324 // Calculate the columns regarding ONLY the normal cells and if this
1325 // column is inside a multicolumn cell then use it only if its the last
1326 // column of this multicolumn cell as this gives an added width to the
1327 // column, all the rest should be adapted!
1329 bool Tabular::calculate_width_of_column_NMC(col_type column)
1331 int const old_column_width = column_info[column].width_of_column;
1333 for (row_type i = 0; i < rows_; ++i) {
1334 idx_type cell = getCellNumber(i, column);
1335 bool ismulti = isMultiColumnReal(cell);
1336 if ((!ismulti || column == right_column_of_cell(cell)) &&
1337 cell_info[i][column].width_of_cell > max)
1339 max = cell_info[i][column].width_of_cell;
1342 column_info[column].width_of_column = max;
1343 return column_info[column].width_of_column != old_column_width;
1347 void Tabular::calculate_width_of_tabular()
1349 width_of_tabular = 0;
1350 for (col_type i = 0; i < columns_; ++i)
1351 width_of_tabular += column_info[i].width_of_column;
1355 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1357 if (cell >= numberofcells)
1361 return rowofcell[cell];
1365 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1367 if (cell >= numberofcells)
1368 return columns_ - 1;
1371 return columnofcell[cell];
1375 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1377 row_type const row = row_of_cell(cell);
1378 col_type column = column_of_cell(cell);
1379 while (column < columns_ - 1 &&
1380 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1386 void Tabular::write(Buffer const & buf, ostream & os) const
1390 << write_attribute("version", 3)
1391 << write_attribute("rows", rows_)
1392 << write_attribute("columns", columns_)
1394 // global longtable options
1396 << write_attribute("rotate", rotate)
1397 << write_attribute("booktabs", use_booktabs)
1398 << write_attribute("islongtable", is_long_tabular)
1399 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1400 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1401 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1402 << write_attribute("headTopDL", endhead.topDL)
1403 << write_attribute("headBottomDL", endhead.bottomDL)
1404 << write_attribute("footTopDL", endfoot.topDL)
1405 << write_attribute("footBottomDL", endfoot.bottomDL)
1406 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1407 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1408 << write_attribute("lastFootEmpty", endlastfoot.empty)
1410 for (col_type j = 0; j < columns_; ++j) {
1412 << write_attribute("alignment", column_info[j].alignment)
1413 << write_attribute("valignment", column_info[j].valignment)
1414 << write_attribute("leftline", column_info[j].left_line)
1415 << write_attribute("rightline", column_info[j].right_line)
1416 << write_attribute("width", column_info[j].p_width.asString())
1417 << write_attribute("special", column_info[j].align_special)
1420 for (row_type i = 0; i < rows_; ++i) {
1422 << write_attribute("topline", row_info[i].top_line)
1423 << write_attribute("bottomline", row_info[i].bottom_line);
1424 static const string def("default");
1425 if (row_info[i].top_space_default)
1426 os << write_attribute("topspace", def);
1428 os << write_attribute("topspace", row_info[i].top_space);
1429 if (row_info[i].bottom_space_default)
1430 os << write_attribute("bottomspace", def);
1432 os << write_attribute("bottomspace", row_info[i].bottom_space);
1433 if (row_info[i].interline_space_default)
1434 os << write_attribute("interlinespace", def);
1436 os << write_attribute("interlinespace", row_info[i].interline_space);
1437 os << write_attribute("endhead", row_info[i].endhead)
1438 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1439 << write_attribute("endfoot", row_info[i].endfoot)
1440 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1441 << write_attribute("newpage", row_info[i].newpage)
1443 for (col_type j = 0; j < columns_; ++j) {
1445 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1446 << write_attribute("alignment", cell_info[i][j].alignment)
1447 << write_attribute("valignment", cell_info[i][j].valignment)
1448 << write_attribute("topline", cell_info[i][j].top_line)
1449 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1450 << write_attribute("leftline", cell_info[i][j].left_line)
1451 << write_attribute("rightline", cell_info[i][j].right_line)
1452 << write_attribute("rotate", cell_info[i][j].rotate)
1453 << write_attribute("usebox", cell_info[i][j].usebox)
1454 << write_attribute("width", cell_info[i][j].p_width)
1455 << write_attribute("special", cell_info[i][j].align_special)
1457 os << "\\begin_inset ";
1458 cell_info[i][j].inset->write(buf, os);
1459 os << "\n\\end_inset\n"
1464 os << "</lyxtabular>\n";
1468 void Tabular::read(Buffer const & buf, Lexer & lex)
1471 istream & is = lex.getStream();
1473 l_getline(is, line);
1474 if (!prefixIs(line, "<lyxtabular ")
1475 && !prefixIs(line, "<Tabular ")) {
1476 BOOST_ASSERT(false);
1481 if (!getTokenValue(line, "version", version))
1483 BOOST_ASSERT(version >= 2);
1486 if (!getTokenValue(line, "rows", rows_arg))
1489 if (!getTokenValue(line, "columns", columns_arg))
1491 init(buf.params(), rows_arg, columns_arg);
1492 l_getline(is, line);
1493 if (!prefixIs(line, "<features")) {
1494 lyxerr << "Wrong tabular format (expected <features ...> got"
1495 << line << ')' << endl;
1498 getTokenValue(line, "rotate", rotate);
1499 getTokenValue(line, "booktabs", use_booktabs);
1500 getTokenValue(line, "islongtable", is_long_tabular);
1501 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1502 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1503 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1504 getTokenValue(line, "headTopDL", endhead.topDL);
1505 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1506 getTokenValue(line, "footTopDL", endfoot.topDL);
1507 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1508 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1509 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1510 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1512 for (col_type j = 0; j < columns_; ++j) {
1514 if (!prefixIs(line,"<column")) {
1515 lyxerr << "Wrong tabular format (expected <column ...> got"
1516 << line << ')' << endl;
1519 getTokenValue(line, "alignment", column_info[j].alignment);
1520 getTokenValue(line, "valignment", column_info[j].valignment);
1521 getTokenValue(line, "leftline", column_info[j].left_line);
1522 getTokenValue(line, "rightline", column_info[j].right_line);
1523 getTokenValue(line, "width", column_info[j].p_width);
1524 getTokenValue(line, "special", column_info[j].align_special);
1527 for (row_type i = 0; i < rows_; ++i) {
1528 l_getline(is, line);
1529 if (!prefixIs(line, "<row")) {
1530 lyxerr << "Wrong tabular format (expected <row ...> got"
1531 << line << ')' << endl;
1534 getTokenValue(line, "topline", row_info[i].top_line);
1535 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1536 getTokenValue(line, "topspace", row_info[i].top_space,
1537 row_info[i].top_space_default);
1538 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1539 row_info[i].bottom_space_default);
1540 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1541 row_info[i].interline_space_default);
1542 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1543 getTokenValue(line, "endhead", row_info[i].endhead);
1544 getTokenValue(line, "endfoot", row_info[i].endfoot);
1545 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1546 getTokenValue(line, "newpage", row_info[i].newpage);
1547 for (col_type j = 0; j < columns_; ++j) {
1548 l_getline(is, line);
1549 if (!prefixIs(line, "<cell")) {
1550 lyxerr << "Wrong tabular format (expected <cell ...> got"
1551 << line << ')' << endl;
1554 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1555 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1556 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1557 getTokenValue(line, "topline", cell_info[i][j].top_line);
1558 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1559 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1560 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1561 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1562 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1563 getTokenValue(line, "width", cell_info[i][j].p_width);
1564 getTokenValue(line, "special", cell_info[i][j].align_special);
1565 l_getline(is, line);
1566 if (prefixIs(line, "\\begin_inset")) {
1567 cell_info[i][j].inset->read(buf, lex);
1568 l_getline(is, line);
1570 if (!prefixIs(line, "</cell>")) {
1571 lyxerr << "Wrong tabular format (expected </cell> got"
1572 << line << ')' << endl;
1576 l_getline(is, line);
1577 if (!prefixIs(line, "</row>")) {
1578 lyxerr << "Wrong tabular format (expected </row> got"
1579 << line << ')' << endl;
1583 while (!prefixIs(line, "</lyxtabular>")) {
1584 l_getline(is, line);
1586 set_row_column_number_info();
1590 bool Tabular::isMultiColumn(idx_type cell) const
1592 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1596 bool Tabular::isMultiColumnReal(idx_type cell) const
1598 return column_of_cell(cell) != right_column_of_cell(cell) &&
1599 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1603 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1605 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1609 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1612 cellstruct & cs = cellinfo_of_cell(cell);
1613 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1614 cs.alignment = column_info[column_of_cell(cell)].alignment;
1615 cs.top_line = row_info[row_of_cell(cell)].top_line;
1616 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1617 cs.left_line = column_info[column_of_cell(cell)].left_line;
1618 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1619 for (idx_type i = 1; i < number; ++i) {
1620 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1621 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1622 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1625 set_row_column_number_info();
1629 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1631 row_type const row = row_of_cell(cell);
1632 col_type column = column_of_cell(cell);
1633 idx_type result = 1;
1635 while (column < columns_ &&
1636 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1645 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1647 row_type const row = row_of_cell(cell);
1648 col_type column = column_of_cell(cell);
1650 idx_type result = 0;
1652 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1653 cell_info[row][column].multicolumn = CELL_NORMAL;
1655 while (column < columns_ &&
1656 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1658 cell_info[row][column].multicolumn = CELL_NORMAL;
1663 set_row_column_number_info();
1668 void Tabular::setBookTabs(bool what)
1670 use_booktabs = what;
1674 bool Tabular::useBookTabs() const
1676 return use_booktabs;
1680 void Tabular::setLongTabular(bool what)
1682 is_long_tabular = what;
1686 bool Tabular::isLongTabular() const
1688 return is_long_tabular;
1692 void Tabular::setRotateTabular(bool flag)
1698 bool Tabular::getRotateTabular() const
1704 void Tabular::setRotateCell(idx_type cell, bool flag)
1706 cellinfo_of_cell(cell).rotate = flag;
1710 bool Tabular::getRotateCell(idx_type cell) const
1712 return cellinfo_of_cell(cell).rotate;
1716 bool Tabular::needRotating() const
1720 for (row_type i = 0; i < rows_; ++i)
1721 for (col_type j = 0; j < columns_; ++j)
1722 if (cell_info[i][j].rotate)
1728 bool Tabular::isLastCell(idx_type cell) const
1730 if (cell + 1 < numberofcells)
1736 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1738 if (row_of_cell(cell) > 0)
1739 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1744 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1746 if (row_of_cell(cell) + 1 < rows_)
1747 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1752 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1754 if (row_of_cell(cell) == 0)
1756 if (!isMultiColumn(cell))
1757 return getCellAbove(cell);
1758 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1762 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1764 if (row_of_cell(cell) + 1 >= rows_)
1766 if (!isMultiColumn(cell))
1767 return getCellBelow(cell);
1768 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1772 Tabular::idx_type Tabular::getCellNumber(row_type row,
1773 col_type column) const
1775 BOOST_ASSERT(column != npos && column < columns_ &&
1776 row != npos && row < rows_);
1777 return cell_info[row][column].cellno;
1781 void Tabular::setUsebox(idx_type cell, BoxType type)
1783 cellinfo_of_cell(cell).usebox = type;
1787 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1789 if (column_info[column_of_cell(cell)].p_width.zero() &&
1790 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1792 if (cellinfo_of_cell(cell).usebox > 1)
1793 return cellinfo_of_cell(cell).usebox;
1794 return useParbox(cell);
1799 // This are functions used for the longtable support
1801 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1807 row_info[row].endfirsthead = flag;
1811 row_info[row].endhead = flag;
1816 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1819 hd.set = haveLTHead();
1820 return row_info[row].endhead;
1824 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1827 hd.set = haveLTFirstHead();
1828 return row_info[row].endfirsthead;
1832 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1838 row_info[row].endlastfoot = flag;
1842 row_info[row].endfoot = flag;
1847 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1850 fd.set = haveLTFoot();
1851 return row_info[row].endfoot;
1855 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1858 fd.set = haveLTLastFoot();
1859 return row_info[row].endlastfoot;
1863 void Tabular::setLTNewPage(row_type row, bool what)
1865 row_info[row].newpage = what;
1869 bool Tabular::getLTNewPage(row_type row) const
1871 return row_info[row].newpage;
1875 bool Tabular::haveLTHead() const
1877 for (row_type i = 0; i < rows_; ++i)
1878 if (row_info[i].endhead)
1884 bool Tabular::haveLTFirstHead() const
1886 if (endfirsthead.empty)
1888 for (row_type i = 0; i < rows_; ++i)
1889 if (row_info[i].endfirsthead)
1895 bool Tabular::haveLTFoot() const
1897 for (row_type i = 0; i < rows_; ++i)
1898 if (row_info[i].endfoot)
1904 bool Tabular::haveLTLastFoot() const
1906 if (endlastfoot.empty)
1908 for (row_type i = 0; i < rows_; ++i)
1909 if (row_info[i].endlastfoot)
1915 // end longtable support functions
1917 void Tabular::setAscentOfRow(row_type row, int height)
1919 if (row >= rows_ || row_info[row].ascent_of_row == height)
1921 row_info[row].ascent_of_row = height;
1925 void Tabular::setDescentOfRow(row_type row, int height)
1927 if (row >= rows_ || row_info[row].descent_of_row == height)
1929 row_info[row].descent_of_row = height;
1933 int Tabular::getAscentOfRow(row_type row) const
1937 return row_info[row].ascent_of_row;
1941 int Tabular::getDescentOfRow(row_type row) const
1943 BOOST_ASSERT(row < rows_);
1944 return row_info[row].descent_of_row;
1948 int Tabular::getHeightOfTabular() const
1951 for (row_type row = 0; row < rows_; ++row)
1952 height += getAscentOfRow(row) + getDescentOfRow(row) +
1953 getAdditionalHeight(row);
1958 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1960 BOOST_ASSERT(row < rows_);
1961 BOOST_ASSERT(column < columns_);
1962 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1966 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1968 // FIXME: assert or return 0 as in TeXBottomHLine()?
1969 BOOST_ASSERT(row != npos);
1970 BOOST_ASSERT(row < rows_);
1972 idx_type const fcell = getFirstCellInRow(row);
1973 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1976 for (idx_type i = fcell; i < n; ++i) {
1980 if (use_booktabs && row == 0) {
1982 } else if (tmp == n - fcell) {
1983 os << (use_booktabs ? "\\midrule " : "\\hline ");
1985 for (idx_type i = fcell; i < n; ++i) {
1987 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1988 << column_of_cell(i) + 1
1990 << right_column_of_cell(i) + 1
2002 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2004 // FIXME: return 0 or assert as in TeXTopHLine()?
2005 if (row == npos || row >= rows_)
2008 idx_type const fcell = getFirstCellInRow(row);
2009 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2012 for (idx_type i = fcell; i < n; ++i) {
2016 if (use_booktabs && row == rows_ - 1) {
2017 os << "\\bottomrule";
2018 } else if (tmp == n - fcell) {
2019 os << (use_booktabs ? "\\midrule" : "\\hline");
2021 for (idx_type i = fcell; i < n; ++i) {
2022 if (bottomLine(i)) {
2023 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2024 << column_of_cell(i) + 1
2026 << right_column_of_cell(i) + 1
2038 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2042 if (getRotateCell(cell)) {
2043 os << "\\begin{sideways}\n";
2046 if (isMultiColumn(cell)) {
2047 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2048 if (leftLine(cell) &&
2049 (isFirstCellInRow(cell) ||
2050 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2051 !rightLine(cell - 1, true))))
2053 if (!cellinfo_of_cell(cell).align_special.empty()) {
2054 os << cellinfo_of_cell(cell).align_special;
2056 if (!getPWidth(cell).zero()) {
2057 switch (getVAlignment(cell)) {
2058 case LYX_VALIGN_TOP:
2061 case LYX_VALIGN_MIDDLE:
2064 case LYX_VALIGN_BOTTOM:
2069 << from_ascii(getPWidth(cell).asLatexString())
2072 switch (getAlignment(cell)) {
2073 case LYX_ALIGN_LEFT:
2076 case LYX_ALIGN_RIGHT:
2083 } // end if else !getPWidth
2084 } // end if else !cellinfo_of_cell
2085 if (rightLine(cell))
2087 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2092 if (getUsebox(cell) == BOX_PARBOX) {
2094 switch (getVAlignment(cell)) {
2095 case LYX_VALIGN_TOP:
2098 case LYX_VALIGN_MIDDLE:
2101 case LYX_VALIGN_BOTTOM:
2105 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2107 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2108 os << "\\begin{minipage}[";
2109 switch (getVAlignment(cell)) {
2110 case LYX_VALIGN_TOP:
2113 case LYX_VALIGN_MIDDLE:
2116 case LYX_VALIGN_BOTTOM:
2120 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2128 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2133 if (getUsebox(cell) == BOX_PARBOX)
2135 else if (getUsebox(cell) == BOX_MINIPAGE) {
2136 os << "%\n\\end{minipage}";
2139 if (isMultiColumn(cell)) {
2142 if (getRotateCell(cell)) {
2143 os << "%\n\\end{sideways}";
2150 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2151 OutputParams const & runparams) const
2153 if (!is_long_tabular)
2157 // output header info
2159 if (endhead.topDL) {
2163 for (row_type i = 0; i < rows_; ++i) {
2164 if (row_info[i].endhead) {
2165 ret += TeXRow(os, i, buf, runparams);
2168 if (endhead.bottomDL) {
2172 os << "\\endhead\n";
2174 if (endfirsthead.empty) {
2175 os << "\\endfirsthead\n";
2179 // output firstheader info
2180 if (haveLTFirstHead()) {
2181 if (endfirsthead.topDL) {
2185 for (row_type i = 0; i < rows_; ++i) {
2186 if (row_info[i].endfirsthead) {
2187 ret += TeXRow(os, i, buf, runparams);
2190 if (endfirsthead.bottomDL) {
2194 os << "\\endfirsthead\n";
2197 // output footer info
2199 if (endfoot.topDL) {
2203 for (row_type i = 0; i < rows_; ++i) {
2204 if (row_info[i].endfoot) {
2205 ret += TeXRow(os, i, buf, runparams);
2208 if (endfoot.bottomDL) {
2212 os << "\\endfoot\n";
2214 if (endlastfoot.empty) {
2215 os << "\\endlastfoot\n";
2219 // output lastfooter info
2220 if (haveLTLastFoot()) {
2221 if (endlastfoot.topDL) {
2225 for (row_type i = 0; i < rows_; ++i) {
2226 if (row_info[i].endlastfoot) {
2227 ret += TeXRow(os, i, buf, runparams);
2230 if (endlastfoot.bottomDL) {
2234 os << "\\endlastfoot\n";
2241 bool Tabular::isValidRow(row_type row) const
2243 if (!is_long_tabular)
2245 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2246 !row_info[row].endfoot && !row_info[row].endlastfoot;
2250 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2251 OutputParams const & runparams) const
2253 idx_type cell = getCellNumber(i, 0);
2254 int ret = TeXTopHLine(os, i);
2255 if (row_info[i].top_space_default) {
2257 os << "\\addlinespace\n";
2259 os << "\\noalign{\\vskip\\doublerulesep}\n";
2261 } else if(!row_info[i].top_space.zero()) {
2263 os << "\\addlinespace["
2264 << from_ascii(row_info[i].top_space.asLatexString())
2267 os << "\\noalign{\\vskip"
2268 << from_ascii(row_info[i].top_space.asLatexString())
2274 for (col_type j = 0; j < columns_; ++j) {
2275 if (isPartOfMultiColumn(i, j))
2277 ret += TeXCellPreamble(os, cell);
2278 shared_ptr<InsetText> inset = getCellInset(cell);
2280 Paragraph const & par = inset->paragraphs().front();
2281 bool rtl = par.isRightToLeftPar(buf.params())
2283 && getPWidth(cell).zero();
2286 if (par.getParLanguage(buf.params())->lang() ==
2292 ret += inset->latex(buf, os, runparams);
2296 ret += TeXCellPostamble(os, cell);
2297 if (!isLastCellInRow(cell)) { // not last cell in row
2302 os << "\\tabularnewline";
2303 if (row_info[i].bottom_space_default) {
2305 os << "\\addlinespace";
2307 os << "[\\doublerulesep]";
2308 } else if (!row_info[i].bottom_space.zero()) {
2310 os << "\\addlinespace";
2312 << from_ascii(row_info[i].bottom_space.asLatexString())
2317 ret += TeXBottomHLine(os, i);
2318 if (row_info[i].interline_space_default) {
2320 os << "\\addlinespace\n";
2322 os << "\\noalign{\\vskip\\doublerulesep}\n";
2324 } else if (!row_info[i].interline_space.zero()) {
2326 os << "\\addlinespace["
2327 << from_ascii(row_info[i].interline_space.asLatexString())
2330 os << "\\noalign{\\vskip"
2331 << from_ascii(row_info[i].interline_space.asLatexString())
2339 int Tabular::latex(Buffer const & buf, odocstream & os,
2340 OutputParams const & runparams) const
2344 //+---------------------------------------------------------------------
2345 //+ first the opening preamble +
2346 //+---------------------------------------------------------------------
2349 os << "\\begin{sideways}\n";
2352 if (is_long_tabular)
2353 os << "\\begin{longtable}{";
2355 os << "\\begin{tabular}{";
2356 for (col_type i = 0; i < columns_; ++i) {
2357 if (!use_booktabs && column_info[i].left_line)
2359 if (!column_info[i].align_special.empty()) {
2360 os << column_info[i].align_special;
2362 if (!column_info[i].p_width.zero()) {
2363 switch (column_info[i].alignment) {
2364 case LYX_ALIGN_LEFT:
2365 os << ">{\\raggedright}";
2367 case LYX_ALIGN_RIGHT:
2368 os << ">{\\raggedleft}";
2370 case LYX_ALIGN_CENTER:
2371 os << ">{\\centering}";
2373 case LYX_ALIGN_NONE:
2374 case LYX_ALIGN_BLOCK:
2375 case LYX_ALIGN_LAYOUT:
2376 case LYX_ALIGN_SPECIAL:
2380 switch (column_info[i].valignment) {
2381 case LYX_VALIGN_TOP:
2384 case LYX_VALIGN_MIDDLE:
2387 case LYX_VALIGN_BOTTOM:
2392 << from_ascii(column_info[i].p_width.asLatexString())
2395 switch (column_info[i].alignment) {
2396 case LYX_ALIGN_LEFT:
2399 case LYX_ALIGN_RIGHT:
2406 } // end if else !column_info[i].p_width
2407 } // end if else !column_info[i].align_special
2408 if (!use_booktabs && column_info[i].right_line)
2414 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2416 //+---------------------------------------------------------------------
2417 //+ the single row and columns (cells) +
2418 //+---------------------------------------------------------------------
2420 for (row_type i = 0; i < rows_; ++i) {
2421 if (isValidRow(i)) {
2422 ret += TeXRow(os, i, buf, runparams);
2423 if (is_long_tabular && row_info[i].newpage) {
2424 os << "\\newpage\n";
2430 //+---------------------------------------------------------------------
2431 //+ the closing of the tabular +
2432 //+---------------------------------------------------------------------
2434 if (is_long_tabular)
2435 os << "\\end{longtable}";
2437 os << "\\end{tabular}";
2439 os << "\n\\end{sideways}";
2447 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2448 OutputParams const & runparams) const
2451 idx_type cell = getFirstCellInRow(row);
2454 for (col_type j = 0; j < columns_; ++j) {
2455 if (isPartOfMultiColumn(row, j))
2458 os << "<entry align=\"";
2459 switch (getAlignment(cell)) {
2460 case LYX_ALIGN_LEFT:
2463 case LYX_ALIGN_RIGHT:
2471 os << "\" valign=\"";
2472 switch (getVAlignment(cell)) {
2473 case LYX_VALIGN_TOP:
2476 case LYX_VALIGN_BOTTOM:
2479 case LYX_VALIGN_MIDDLE:
2484 if (isMultiColumn(cell)) {
2485 os << " namest=\"col" << j << "\" ";
2486 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2490 ret += getCellInset(cell)->docbook(buf, os, runparams);
2499 int Tabular::docbook(Buffer const & buf, odocstream & os,
2500 OutputParams const & runparams) const
2504 //+---------------------------------------------------------------------
2505 //+ first the opening preamble +
2506 //+---------------------------------------------------------------------
2508 os << "<tgroup cols=\"" << columns_
2509 << "\" colsep=\"1\" rowsep=\"1\">\n";
2511 for (col_type i = 0; i < columns_; ++i) {
2512 os << "<colspec colname=\"col" << i << "\" align=\"";
2513 switch (column_info[i].alignment) {
2514 case LYX_ALIGN_LEFT:
2517 case LYX_ALIGN_RIGHT:
2525 if (runparams.flavor == OutputParams::XML)
2531 //+---------------------------------------------------------------------
2532 //+ Long Tabular case +
2533 //+---------------------------------------------------------------------
2535 // output header info
2536 if (haveLTHead() || haveLTFirstHead()) {
2539 for (row_type i = 0; i < rows_; ++i) {
2540 if (row_info[i].endhead || row_info[i].endfirsthead) {
2541 ret += docbookRow(buf, os, i, runparams);
2547 // output footer info
2548 if (haveLTFoot() || haveLTLastFoot()) {
2551 for (row_type i = 0; i < rows_; ++i) {
2552 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2553 ret += docbookRow(buf, os, i, runparams);
2560 //+---------------------------------------------------------------------
2561 //+ the single row and columns (cells) +
2562 //+---------------------------------------------------------------------
2566 for (row_type i = 0; i < rows_; ++i) {
2567 if (isValidRow(i)) {
2568 ret += docbookRow(buf, os, i, runparams);
2573 //+---------------------------------------------------------------------
2574 //+ the closing of the tabular +
2575 //+---------------------------------------------------------------------
2584 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2585 vector<unsigned int> const & clen) const
2587 idx_type const fcell = getFirstCellInRow(row);
2588 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2591 for (idx_type i = fcell; i < n; ++i) {
2601 for (idx_type i = fcell; i < n; ++i) {
2612 col_type column = column_of_cell(i);
2613 int len = clen[column];
2614 while (column < columns_ - 1
2615 && isPartOfMultiColumn(row, ++column))
2616 len += clen[column] + 4;
2617 os << docstring(len, ch);
2632 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2633 vector<unsigned int> const & clen) const
2635 idx_type const fcell = getFirstCellInRow(row);
2636 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2639 for (idx_type i = fcell; i < n; ++i) {
2640 if (bottomLine(i)) {
2649 for (idx_type i = fcell; i < n; ++i) {
2650 if (bottomLine(i)) {
2660 col_type column = column_of_cell(i);
2661 int len = clen[column];
2662 while (column < columns_ -1
2663 && isPartOfMultiColumn(row, ++column))
2664 len += clen[column] + 4;
2665 os << docstring(len, ch);
2666 if (bottomLine(i)) {
2680 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2681 OutputParams const & runparams,
2682 idx_type cell, row_type row, col_type column,
2683 vector<unsigned int> const & clen,
2684 bool onlydata) const
2686 odocstringstream sstr;
2687 getCellInset(cell)->plaintext(buf, sstr, runparams);
2699 unsigned int len1 = sstr.str().length();
2700 unsigned int len2 = clen[column];
2701 while (column < columns_ -1
2702 && isPartOfMultiColumn(row, ++column))
2703 len2 += clen[column] + 4;
2706 switch (getAlignment(cell)) {
2708 case LYX_ALIGN_LEFT:
2711 case LYX_ALIGN_RIGHT:
2715 case LYX_ALIGN_CENTER:
2721 os << docstring(len1, ' ') << sstr.str()
2722 << docstring(len2, ' ');
2724 if (rightLine(cell))
2731 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2732 OutputParams const & runparams, int const depth,
2733 bool onlydata, unsigned char delim) const
2735 // first calculate the width of the single columns
2736 vector<unsigned int> clen(columns_);
2739 // first all non (real) multicolumn cells!
2740 for (col_type j = 0; j < columns_; ++j) {
2742 for (row_type i = 0; i < rows_; ++i) {
2743 idx_type cell = getCellNumber(i, j);
2744 if (isMultiColumnReal(cell))
2746 odocstringstream sstr;
2747 getCellInset(cell)->plaintext(buf, sstr, runparams);
2748 if (clen[j] < sstr.str().length())
2749 clen[j] = sstr.str().length();
2752 // then all (real) multicolumn cells!
2753 for (col_type j = 0; j < columns_; ++j) {
2754 for (row_type i = 0; i < rows_; ++i) {
2755 idx_type cell = getCellNumber(i, j);
2756 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2758 odocstringstream sstr;
2759 getCellInset(cell)->plaintext(buf, sstr, runparams);
2760 int len = int(sstr.str().length());
2761 idx_type const n = cells_in_multicolumn(cell);
2762 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2764 if (len > int(clen[j + n - 1]))
2765 clen[j + n - 1] = len;
2770 for (row_type i = 0; i < rows_; ++i) {
2771 if (!onlydata && plaintextTopHLine(os, i, clen))
2772 os << docstring(depth * 2, ' ');
2773 for (col_type j = 0; j < columns_; ++j) {
2774 if (isPartOfMultiColumn(i, j))
2776 if (onlydata && j > 0)
2778 plaintextPrintCell(buf, os, runparams,
2779 cell, i, j, clen, onlydata);
2784 os << docstring(depth * 2, ' ');
2785 if (plaintextBottomHLine(os, i, clen))
2786 os << docstring(depth * 2, ' ');
2792 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2794 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2798 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2799 col_type column) const
2801 return cell_info[row][column].inset;
2805 void Tabular::setCellInset(row_type row, col_type column,
2806 shared_ptr<InsetText> ins) const
2808 cell_info[row][column].inset = ins;
2813 Tabular::getCellFromInset(Inset const * inset) const
2815 // is this inset part of the tabular?
2817 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2818 BOOST_ASSERT(false);
2821 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2822 if (getCellInset(cell).get() == inset) {
2823 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2824 << "cell=" << cell << endl;
2828 // We should have found a cell at this point
2829 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2830 << inset << " not found!" << endl;
2831 BOOST_ASSERT(false);
2837 void Tabular::validate(LaTeXFeatures & features) const
2839 features.require("NeedTabularnewline");
2841 features.require("booktabs");
2842 if (isLongTabular())
2843 features.require("longtable");
2845 features.require("rotating");
2846 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2847 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2848 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2849 features.require("array");
2850 getCellInset(cell)->validate(features);
2855 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2857 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2858 ParagraphList::const_iterator cit = parlist.begin();
2859 ParagraphList::const_iterator end = parlist.end();
2861 for (; cit != end; ++cit)
2862 for (int i = 0; i < cit->size(); ++i)
2863 if (cit->isNewline(i))
2870 /////////////////////////////////////////////////////////////////////
2874 /////////////////////////////////////////////////////////////////////
2876 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2878 : tabular(buf.params(), max(rows, row_type(1)),
2879 max(columns, col_type(1))), buffer_(&buf), scx_(0), is_deleted_(false)
2883 InsetTabular::InsetTabular(InsetTabular const & tab)
2884 : Inset(tab), tabular(tab.tabular),
2885 buffer_(tab.buffer_), scx_(0), is_deleted_(false)
2889 InsetTabular::~InsetTabular()
2891 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 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
3104 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3105 row_type row, idx_type cell, bool erased) const
3107 int x2 = x + tabular.getWidthOfColumn(cell);
3108 bool on_off = false;
3109 Color::color col = Color::tabularline;
3110 Color::color onoffcol = Color::tabularonoffline;
3113 col = Color::deletedtext;
3114 onoffcol = Color::deletedtext;
3117 if (!tabular.topAlreadyDrawn(cell)) {
3118 on_off = !tabular.topLine(cell);
3119 pain.line(x, y - tabular.getAscentOfRow(row),
3120 x2, y - tabular.getAscentOfRow(row),
3121 on_off ? onoffcol : col,
3122 on_off ? Painter::line_onoffdash : Painter::line_solid);
3124 on_off = !tabular.bottomLine(cell);
3125 pain.line(x, y + tabular.getDescentOfRow(row),
3126 x2, y + tabular.getDescentOfRow(row),
3127 on_off ? onoffcol : col,
3128 on_off ? Painter::line_onoffdash : Painter::line_solid);
3129 if (!tabular.leftAlreadyDrawn(cell)) {
3130 on_off = !tabular.leftLine(cell);
3131 pain.line(x, y - tabular.getAscentOfRow(row),
3132 x, y + tabular.getDescentOfRow(row),
3133 on_off ? onoffcol : col,
3134 on_off ? Painter::line_onoffdash : Painter::line_solid);
3136 on_off = !tabular.rightLine(cell);
3137 pain.line(x2 - tabular.getAdditionalWidth(cell),
3138 y - tabular.getAscentOfRow(row),
3139 x2 - tabular.getAdditionalWidth(cell),
3140 y + tabular.getDescentOfRow(row),
3141 on_off ? onoffcol : col,
3142 on_off ? Painter::line_onoffdash : Painter::line_solid);
3146 docstring const InsetTabular::editMessage() const
3148 return _("Opened table");
3152 void InsetTabular::edit(Cursor & cur, bool left)
3154 //lyxerr << "InsetTabular::edit: " << this << endl;
3156 cur.selection() = false;
3159 if (isRightToLeft(cur))
3160 cur.idx() = tabular.getLastCellInRow(0);
3166 if (isRightToLeft(cur))
3167 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3169 cur.idx() = tabular.getNumberOfCells() - 1;
3171 cur.pos() = cur.lastpos(); // FIXME crude guess
3173 // FIXME: this accesses the position cache before it is initialized
3175 //cur.bv().fitCursor();
3179 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3181 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3182 << "\n cur:" << cur << endl;
3183 CursorSlice sl = cur.top();
3184 Cursor & bvcur = cur.bv().cursor();
3186 switch (cmd.action) {
3188 case LFUN_MOUSE_PRESS:
3189 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3191 if (cmd.button() == mouse_button::button1
3192 || (cmd.button() == mouse_button::button3
3193 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
3194 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
3196 cur.selection() = false;
3197 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3198 cur.bv().mouseSetCursor(cur);
3202 if (cmd.button() == mouse_button::button2) {
3203 if (cap::selection()) {
3204 // See comment in Text::dispatch why we
3206 // FIXME This does not use paste_tabular,
3207 // another reason why paste_tabular should go.
3208 cap::copySelectionToStack();
3209 cmd = FuncRequest(LFUN_PASTE, "0");
3211 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
3214 doDispatch(cur, cmd);
3218 case LFUN_MOUSE_MOTION:
3219 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3220 if (cmd.button() == mouse_button::button1) {
3221 // only accept motions to places not deeper nested than the real anchor
3222 if (bvcur.anchor_.hasPart(cur)) {
3223 // only update if selection changes
3224 if (bvcur.idx() == cur.idx() &&
3225 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3227 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3228 bvcur.setCursor(cur);
3229 bvcur.selection() = true;
3235 case LFUN_MOUSE_RELEASE:
3236 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3237 if (cmd.button() == mouse_button::button1) {
3238 if (bvcur.selection()) {
3239 // Bug3238: disable persistent selection for table cells for now
3240 if (tablemode(bvcur))
3241 theSelection().haveSelection(true);
3243 saveSelection(bvcur);
3245 } else if (cmd.button() == mouse_button::button3)
3246 InsetTabularMailer(*this).showDialog(&cur.bv());
3249 case LFUN_CELL_BACKWARD:
3251 cur.selection() = false;
3255 case LFUN_CELL_FORWARD:
3257 cur.selection() = false;
3261 case LFUN_CHAR_FORWARD_SELECT:
3262 case LFUN_CHAR_FORWARD:
3263 cell(cur.idx())->dispatch(cur, cmd);
3264 if (!cur.result().dispatched()) {
3265 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3266 if (cmd.action == LFUN_CHAR_FORWARD_SELECT)
3268 if (sl == cur.top())
3269 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3275 case LFUN_CHAR_BACKWARD_SELECT:
3276 case LFUN_CHAR_BACKWARD:
3277 cell(cur.idx())->dispatch(cur, cmd);
3278 if (!cur.result().dispatched()) {
3279 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3280 if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
3282 if (sl == cur.top())
3283 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3289 case LFUN_DOWN_SELECT:
3291 cell(cur.idx())->dispatch(cur, cmd);
3292 cur.dispatched(); // override the cell's decision
3293 if (sl == cur.top())
3294 // if our Text didn't do anything to the cursor
3295 // then we try to put the cursor into the cell below
3296 // setting also the right targetX.
3297 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3298 cur.idx() = tabular.getCellBelow(cur.idx());
3300 TextMetrics const & tm =
3301 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3302 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3303 if (cmd.action == LFUN_DOWN_SELECT) {
3304 // Bug3238: disable persistent selection for table cells for now
3306 theSelection().haveSelection(true);
3311 if (sl == cur.top()) {
3312 // we trick it to go to the RIGHT after leaving the
3314 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3319 case LFUN_UP_SELECT:
3321 cell(cur.idx())->dispatch(cur, cmd);
3322 cur.dispatched(); // override the cell's decision
3323 if (sl == cur.top())
3324 // if our Text didn't do anything to the cursor
3325 // then we try to put the cursor into the cell above
3326 // setting also the right targetX.
3327 if (tabular.row_of_cell(cur.idx()) != 0) {
3328 cur.idx() = tabular.getCellAbove(cur.idx());
3329 cur.pit() = cur.lastpit();
3330 Text const * text = cell(cur.idx())->getText(0);
3331 TextMetrics const & tm = cur.bv().textMetrics(text);
3332 ParagraphMetrics const & pm =
3333 tm.parMetrics(cur.lastpit());
3334 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3335 if (cmd.action == LFUN_UP_SELECT) {
3336 // Bug3238: disable persistent selection for table cells for now
3338 theSelection().haveSelection(true);
3343 if (sl == cur.top()) {
3344 cmd = FuncRequest(LFUN_UP);
3349 // case LFUN_SCREEN_DOWN: {
3350 // //if (hasSelection())
3351 // // cur.selection() = false;
3352 // col_type const col = tabular.column_of_cell(cur.idx());
3353 // int const t = cur.bv().top_y() + cur.bv().height();
3354 // if (t < yo() + tabular.getHeightOfTabular()) {
3355 // cur.bv().scrollDocView(t);
3356 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3358 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3365 // case LFUN_SCREEN_UP: {
3366 // //if (hasSelection())
3367 // // cur.selection() = false;
3368 // col_type const col = tabular.column_of_cell(cur.idx());
3369 // int const t = cur.bv().top_y() + cur.bv().height();
3371 // cur.bv().scrollDocView(t);
3375 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3379 // cur.par() = cur.lastpar();
3380 // cur.pos() = cur.lastpos();
3384 case LFUN_LAYOUT_TABULAR:
3385 InsetTabularMailer(*this).showDialog(&cur.bv());
3388 case LFUN_INSET_DIALOG_UPDATE:
3389 InsetTabularMailer(*this).updateDialog(&cur.bv());
3392 case LFUN_TABULAR_FEATURE:
3393 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3397 // insert file functions
3398 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3399 case LFUN_FILE_INSERT_PLAINTEXT: {
3401 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3402 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3408 if (tablemode(cur)) {
3409 if (copySelection(cur)) {
3410 recordUndoInset(cur, Undo::DELETE);
3415 cell(cur.idx())->dispatch(cur, cmd);
3418 case LFUN_CHAR_DELETE_BACKWARD:
3419 case LFUN_CHAR_DELETE_FORWARD:
3420 if (tablemode(cur)) {
3421 recordUndoInset(cur, Undo::DELETE);
3425 cell(cur.idx())->dispatch(cur, cmd);
3429 if (!cur.selection())
3431 if (tablemode(cur)) {
3435 cell(cur.idx())->dispatch(cur, cmd);
3438 case LFUN_CLIPBOARD_PASTE:
3439 case LFUN_PRIMARY_SELECTION_PASTE: {
3440 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3441 theClipboard().getAsText() :
3442 theSelection().get();
3445 // pass to InsertPlaintextString, but
3446 // only if we have multi-cell content
3447 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3448 if (insertPlaintextString(cur.bv(), clip, false)) {
3449 // content has been replaced,
3450 // so cursor might be invalid
3451 cur.pos() = cur.lastpos();
3452 bvcur.setCursor(cur);
3456 // Let the cell handle normal text
3457 cell(cur.idx())->dispatch(cur, cmd);
3462 if (tabularStackDirty() && theClipboard().isInternal()) {
3463 recordUndoInset(cur, Undo::INSERT);
3464 pasteClipboard(cur);
3467 cell(cur.idx())->dispatch(cur, cmd);
3470 case LFUN_FONT_EMPH:
3471 case LFUN_FONT_BOLD:
3472 case LFUN_FONT_ROMAN:
3473 case LFUN_FONT_NOUN:
3474 case LFUN_FONT_ITAL:
3475 case LFUN_FONT_FRAK:
3476 case LFUN_FONT_CODE:
3477 case LFUN_FONT_SANS:
3478 case LFUN_FONT_FREE_APPLY:
3479 case LFUN_FONT_FREE_UPDATE:
3480 case LFUN_FONT_SIZE:
3481 case LFUN_FONT_UNDERLINE:
3483 case LFUN_WORD_CAPITALIZE:
3484 case LFUN_WORD_UPCASE:
3485 case LFUN_WORD_LOWCASE:
3486 case LFUN_CHARS_TRANSPOSE:
3487 if (tablemode(cur)) {
3490 getSelection(cur, rs, re, cs, ce);
3491 Cursor tmpcur = cur;
3492 for (row_type i = rs; i <= re; ++i) {
3493 for (col_type j = cs; j <= ce; ++j) {
3494 // cursor follows cell:
3495 tmpcur.idx() = tabular.getCellNumber(i, j);
3496 // select this cell only:
3499 tmpcur.resetAnchor();
3500 tmpcur.pit() = tmpcur.lastpit();
3501 tmpcur.pos() = tmpcur.top().lastpos();
3502 tmpcur.setCursor(tmpcur);
3503 tmpcur.setSelection();
3504 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3509 cell(cur.idx())->dispatch(cur, cmd);
3513 // we try to handle this event in the insets dispatch function.
3514 cell(cur.idx())->dispatch(cur, cmd);
3518 // FIXME: this accesses the position cache before it is initialized
3521 InsetTabularMailer(*this).updateDialog(&cur.bv());
3525 // function sets an object as defined in func_status.h:
3526 // states OK, Unknown, Disabled, On, Off.
3527 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3528 FuncStatus & status) const
3530 switch (cmd.action) {
3531 case LFUN_TABULAR_FEATURE: {
3532 int action = Tabular::LAST_ACTION;
3534 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3535 string const tmp = tabularFeature[i].feature;
3536 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3537 action = tabularFeature[i].action;
3541 if (action == Tabular::LAST_ACTION) {
3543 status.unknown(true);
3547 string const argument
3548 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3550 row_type sel_row_start = 0;
3551 row_type sel_row_end = 0;
3552 col_type sel_col_start = 0;
3553 col_type sel_col_end = 0;
3554 Tabular::ltType dummyltt;
3557 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3560 case Tabular::SET_PWIDTH:
3561 case Tabular::SET_MPWIDTH:
3562 case Tabular::SET_SPECIAL_COLUMN:
3563 case Tabular::SET_SPECIAL_MULTI:
3564 case Tabular::APPEND_ROW:
3565 case Tabular::APPEND_COLUMN:
3566 case Tabular::DELETE_ROW:
3567 case Tabular::DELETE_COLUMN:
3568 case Tabular::COPY_ROW:
3569 case Tabular::COPY_COLUMN:
3570 case Tabular::SET_ALL_LINES:
3571 case Tabular::UNSET_ALL_LINES:
3572 case Tabular::SET_TOP_SPACE:
3573 case Tabular::SET_BOTTOM_SPACE:
3574 case Tabular::SET_INTERLINE_SPACE:
3578 case Tabular::MULTICOLUMN:
3579 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3582 case Tabular::M_TOGGLE_LINE_TOP:
3584 case Tabular::TOGGLE_LINE_TOP:
3585 status.setOnOff(tabular.topLine(cur.idx(), flag));
3588 case Tabular::M_TOGGLE_LINE_BOTTOM:
3590 case Tabular::TOGGLE_LINE_BOTTOM:
3591 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3594 case Tabular::M_TOGGLE_LINE_LEFT:
3596 case Tabular::TOGGLE_LINE_LEFT:
3597 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3600 case Tabular::M_TOGGLE_LINE_RIGHT:
3602 case Tabular::TOGGLE_LINE_RIGHT:
3603 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3606 case Tabular::M_ALIGN_LEFT:
3608 case Tabular::ALIGN_LEFT:
3609 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3612 case Tabular::M_ALIGN_RIGHT:
3614 case Tabular::ALIGN_RIGHT:
3615 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3618 case Tabular::M_ALIGN_CENTER:
3620 case Tabular::ALIGN_CENTER:
3621 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3624 case Tabular::ALIGN_BLOCK:
3625 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3626 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3629 case Tabular::M_VALIGN_TOP:
3631 case Tabular::VALIGN_TOP:
3633 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3636 case Tabular::M_VALIGN_BOTTOM:
3638 case Tabular::VALIGN_BOTTOM:
3640 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3643 case Tabular::M_VALIGN_MIDDLE:
3645 case Tabular::VALIGN_MIDDLE:
3647 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3650 case Tabular::SET_LONGTABULAR:
3651 status.setOnOff(tabular.isLongTabular());
3654 case Tabular::UNSET_LONGTABULAR:
3655 status.setOnOff(!tabular.isLongTabular());
3658 case Tabular::TOGGLE_ROTATE_TABULAR:
3659 case Tabular::SET_ROTATE_TABULAR:
3660 status.setOnOff(tabular.getRotateTabular());
3663 case Tabular::UNSET_ROTATE_TABULAR:
3664 status.setOnOff(!tabular.getRotateTabular());
3667 case Tabular::TOGGLE_ROTATE_CELL:
3668 case Tabular::SET_ROTATE_CELL:
3669 status.setOnOff(!oneCellHasRotationState(false,
3670 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3673 case Tabular::UNSET_ROTATE_CELL:
3674 status.setOnOff(!oneCellHasRotationState(true,
3675 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3678 case Tabular::SET_USEBOX:
3679 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3682 case Tabular::SET_LTFIRSTHEAD:
3683 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3686 case Tabular::UNSET_LTFIRSTHEAD:
3687 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3690 case Tabular::SET_LTHEAD:
3691 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3694 case Tabular::UNSET_LTHEAD:
3695 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3698 case Tabular::SET_LTFOOT:
3699 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3702 case Tabular::UNSET_LTFOOT:
3703 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3706 case Tabular::SET_LTLASTFOOT:
3707 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3710 case Tabular::UNSET_LTLASTFOOT:
3711 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3714 case Tabular::SET_LTNEWPAGE:
3715 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3718 case Tabular::SET_BOOKTABS:
3719 status.setOnOff(tabular.useBookTabs());
3722 case Tabular::UNSET_BOOKTABS:
3723 status.setOnOff(!tabular.useBookTabs());
3728 status.enabled(false);
3734 // These are only enabled inside tabular
3735 case LFUN_CELL_BACKWARD:
3736 case LFUN_CELL_FORWARD:
3737 status.enabled(true);
3740 // disable these with multiple cells selected
3741 case LFUN_INSET_INSERT:
3742 case LFUN_TABULAR_INSERT:
3743 case LFUN_CHARSTYLE_INSERT:
3744 case LFUN_FLOAT_INSERT:
3745 case LFUN_FLOAT_WIDE_INSERT:
3746 case LFUN_FOOTNOTE_INSERT:
3747 case LFUN_MARGINALNOTE_INSERT:
3748 case LFUN_MATH_INSERT:
3749 case LFUN_MATH_MODE:
3750 case LFUN_MATH_MUTATE:
3751 case LFUN_MATH_DISPLAY:
3752 case LFUN_NOTE_INSERT:
3753 case LFUN_OPTIONAL_INSERT:
3754 case LFUN_BOX_INSERT:
3755 case LFUN_BRANCH_INSERT:
3756 case LFUN_WRAP_INSERT:
3757 case LFUN_ERT_INSERT: {
3758 if (tablemode(cur)) {
3759 status.enabled(false);
3762 return cell(cur.idx())->getStatus(cur, cmd, status);
3765 // disable in non-fixed-width cells
3766 case LFUN_BREAK_LINE:
3767 case LFUN_BREAK_PARAGRAPH:
3768 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3769 case LFUN_BREAK_PARAGRAPH_SKIP: {
3770 if (tabular.getPWidth(cur.idx()).zero()) {
3771 status.enabled(false);
3774 return cell(cur.idx())->getStatus(cur, cmd, status);
3778 if (tabularStackDirty() && theClipboard().isInternal()) {
3779 status.enabled(true);
3782 return cell(cur.idx())->getStatus(cur, cmd, status);
3784 case LFUN_INSET_MODIFY:
3785 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3786 status.enabled(true);
3792 // we try to handle this event in the insets dispatch function.
3793 return cell(cur.idx())->getStatus(cur, cmd, status);
3798 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3799 OutputParams const & runparams) const
3801 return tabular.latex(buf, os, runparams);
3805 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3806 OutputParams const & runparams) const
3808 os << '\n'; // output table on a new line
3809 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3810 tabular.plaintext(buf, os, runparams, dp, false, 0);
3811 return PLAINTEXT_NEWLINE;
3815 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3816 OutputParams const & runparams) const
3821 #ifdef WITH_WARNINGS
3822 #warning Why not pass a proper DocIterator here?
3825 // if the table is inside a float it doesn't need the informaltable
3826 // wrapper. Search for it.
3827 for (master = owner(); master; master = master->owner())
3828 if (master->lyxCode() == Inset::FLOAT_CODE)
3833 os << "<informaltable>";
3836 ret += tabular.docbook(buf, os, runparams);
3838 os << "</informaltable>";
3845 void InsetTabular::validate(LaTeXFeatures & features) const
3847 tabular.validate(features);
3851 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3853 return tabular.getCellInset(idx);
3857 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3859 return tabular.getCellInset(idx);
3863 void InsetTabular::cursorPos(BufferView const & bv,
3864 CursorSlice const & sl, bool boundary, int & x, int & y) const
3866 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3868 // y offset correction
3869 int const row = tabular.row_of_cell(sl.idx());
3870 for (int i = 0; i <= row; ++i) {
3872 y += tabular.getAscentOfRow(i);
3873 y += tabular.getAdditionalHeight(i);
3876 y += tabular.getDescentOfRow(i);
3879 // x offset correction
3880 int const col = tabular.column_of_cell(sl.idx());
3881 int idx = tabular.getCellNumber(row, 0);
3882 for (int j = 0; j < col; ++j) {
3883 if (tabular.isPartOfMultiColumn(row, j))
3885 x += tabular.getWidthOfColumn(idx);
3888 x += tabular.getBeginningOfTextInCell(idx);
3889 x += ADD_TO_TABULAR_WIDTH;
3894 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3898 Inset const & inset = *tabular.getCellInset(cell);
3899 Point o = bv.coordCache().getInsets().xy(&inset);
3900 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3901 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3902 row_type const row = tabular.row_of_cell(cell);
3903 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3904 tabular.getAdditionalHeight(row);
3905 int const yend = o.y_ + tabular.getDescentOfRow(row);
3917 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3918 // << " ybeg=" << ybeg << " yend=" << yend
3919 // << " xx=" << xx << " yy=" << yy
3920 // << " dist=" << xx + yy << endl;
3925 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3927 //lyxerr << "InsetTabular::editXY: " << this << endl;
3928 cur.selection() = false;
3930 cur.idx() = getNearestCell(cur.bv(), x, y);
3932 return cell(cur.idx())->text_.editXY(cur, x, y);
3936 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3938 cur.idx() = getNearestCell(cur.bv(), x, y);
3939 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3943 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3945 idx_type idx_min = 0;
3946 int dist_min = std::numeric_limits<int>::max();
3947 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3948 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3949 int const d = dist(bv, i, x, y);
3960 int InsetTabular::getCellXPos(idx_type const cell) const
3964 for (; !tabular.isFirstCellInRow(c); --c)
3966 int lx = tabular.getWidthOfColumn(cell);
3967 for (; c < cell; ++c)
3968 lx += tabular.getWidthOfColumn(c);
3970 return lx - tabular.getWidthOfColumn(cell);
3974 void InsetTabular::resetPos(Cursor & cur) const
3976 BufferView & bv = cur.bv();
3977 int const maxwidth = bv.workWidth();
3979 if (&cur.inset() != this) {
3983 int const X2 = maxwidth;
3984 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3985 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3986 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3989 scx_ = X1 + 20 - x1;
3991 scx_ = X2 - 20 - x2;
3996 cur.updateFlags(Update::Force | Update::FitCursor);
4000 void InsetTabular::moveNextCell(Cursor & cur)
4002 if (isRightToLeft(cur)) {
4003 if (tabular.isFirstCellInRow(cur.idx())) {
4004 row_type const row = tabular.row_of_cell(cur.idx());
4005 if (row == tabular.rows() - 1)
4007 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4014 if (tabular.isLastCell(cur.idx()))
4024 void InsetTabular::movePrevCell(Cursor & cur)
4026 if (isRightToLeft(cur)) {
4027 if (tabular.isLastCellInRow(cur.idx())) {
4028 row_type const row = tabular.row_of_cell(cur.idx());
4031 cur.idx() = tabular.getFirstCellInRow(row);
4032 cur.idx() = tabular.getCellAbove(cur.idx());
4034 if (tabular.isLastCell(cur.idx()))
4039 if (cur.idx() == 0) // first cell
4043 cur.pit() = cur.lastpit();
4044 cur.pos() = cur.lastpos();
4046 // FIXME: this accesses the position cache before it is initialized
4051 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4053 Tabular::Feature action = Tabular::LAST_ACTION;
4056 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4057 string const tmp = tabularFeature[i].feature;
4059 if (tmp == what.substr(0, tmp.length())) {
4060 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4061 //tabularFeatures[i].feature.length()))
4062 action = tabularFeature[i].action;
4066 if (action == Tabular::LAST_ACTION)
4070 ltrim(what.substr(tabularFeature[i].feature.length()));
4071 tabularFeatures(cur, action, val);
4076 static void checkLongtableSpecial(Tabular::ltType & ltt,
4077 string const & special, bool & flag)
4079 if (special == "dl_above") {
4082 } else if (special == "dl_below") {
4083 ltt.bottomDL = flag;
4085 } else if (special == "empty") {
4094 bool InsetTabular::oneCellHasRotationState(bool rotated,
4095 row_type row_start, row_type row_end,
4096 col_type col_start, col_type col_end) const {
4098 for (row_type i = row_start; i <= row_end; ++i) {
4099 for (col_type j = col_start; j <= col_end; ++j) {
4100 if (tabular.getRotateCell(tabular.getCellNumber(i, j))
4109 void InsetTabular::tabularFeatures(Cursor & cur,
4110 Tabular::Feature feature, string const & value)
4112 BufferView & bv = cur.bv();
4113 col_type sel_col_start;
4114 col_type sel_col_end;
4115 row_type sel_row_start;
4116 row_type sel_row_end;
4117 bool setLines = false;
4118 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4119 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4123 case Tabular::M_ALIGN_LEFT:
4124 case Tabular::ALIGN_LEFT:
4125 setAlign = LYX_ALIGN_LEFT;
4128 case Tabular::M_ALIGN_RIGHT:
4129 case Tabular::ALIGN_RIGHT:
4130 setAlign = LYX_ALIGN_RIGHT;
4133 case Tabular::M_ALIGN_CENTER:
4134 case Tabular::ALIGN_CENTER:
4135 setAlign = LYX_ALIGN_CENTER;
4138 case Tabular::ALIGN_BLOCK:
4139 setAlign = LYX_ALIGN_BLOCK;
4142 case Tabular::M_VALIGN_TOP:
4143 case Tabular::VALIGN_TOP:
4144 setVAlign = Tabular::LYX_VALIGN_TOP;
4147 case Tabular::M_VALIGN_BOTTOM:
4148 case Tabular::VALIGN_BOTTOM:
4149 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4152 case Tabular::M_VALIGN_MIDDLE:
4153 case Tabular::VALIGN_MIDDLE:
4154 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4161 recordUndoInset(cur, Undo::ATOMIC);
4163 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4164 row_type const row = tabular.row_of_cell(cur.idx());
4165 col_type const column = tabular.column_of_cell(cur.idx());
4167 Tabular::ltType ltt;
4171 case Tabular::SET_PWIDTH: {
4172 Length const len(value);
4173 tabular.setColumnPWidth(cur, cur.idx(), len);
4175 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4176 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4180 case Tabular::SET_MPWIDTH:
4181 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4184 case Tabular::SET_SPECIAL_COLUMN:
4185 case Tabular::SET_SPECIAL_MULTI:
4186 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4189 case Tabular::APPEND_ROW:
4190 // append the row into the tabular
4191 tabular.appendRow(bv.buffer()->params(), cur.idx());
4194 case Tabular::APPEND_COLUMN:
4195 // append the column into the tabular
4196 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4197 cur.idx() = tabular.getCellNumber(row, column);
4200 case Tabular::DELETE_ROW:
4201 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4202 tabular.deleteRow(sel_row_start);
4203 if (sel_row_start >= tabular.rows())
4205 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4208 cur.selection() = false;
4211 case Tabular::DELETE_COLUMN:
4212 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4213 tabular.deleteColumn(sel_col_start);
4214 if (sel_col_start >= tabular.columns())
4216 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4219 cur.selection() = false;
4222 case Tabular::COPY_ROW:
4223 tabular.copyRow(bv.buffer()->params(), row);
4226 case Tabular::COPY_COLUMN:
4227 tabular.copyColumn(bv.buffer()->params(), column);
4228 cur.idx() = tabular.getCellNumber(row, column);
4231 case Tabular::M_TOGGLE_LINE_TOP:
4233 case Tabular::TOGGLE_LINE_TOP: {
4234 bool lineSet = !tabular.topLine(cur.idx(), flag);
4235 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4236 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4238 tabular.getCellNumber(i, j),
4243 case Tabular::M_TOGGLE_LINE_BOTTOM:
4245 case Tabular::TOGGLE_LINE_BOTTOM: {
4246 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4247 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4248 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4249 tabular.setBottomLine(
4250 tabular.getCellNumber(i, j),
4256 case Tabular::M_TOGGLE_LINE_LEFT:
4258 case Tabular::TOGGLE_LINE_LEFT: {
4259 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4260 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4261 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4262 tabular.setLeftLine(
4263 tabular.getCellNumber(i,j),
4269 case Tabular::M_TOGGLE_LINE_RIGHT:
4271 case Tabular::TOGGLE_LINE_RIGHT: {
4272 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4273 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4274 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4275 tabular.setRightLine(
4276 tabular.getCellNumber(i,j),
4282 case Tabular::M_ALIGN_LEFT:
4283 case Tabular::M_ALIGN_RIGHT:
4284 case Tabular::M_ALIGN_CENTER:
4286 case Tabular::ALIGN_LEFT:
4287 case Tabular::ALIGN_RIGHT:
4288 case Tabular::ALIGN_CENTER:
4289 case Tabular::ALIGN_BLOCK:
4290 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4291 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4292 tabular.setAlignment(
4293 tabular.getCellNumber(i, j),
4298 case Tabular::M_VALIGN_TOP:
4299 case Tabular::M_VALIGN_BOTTOM:
4300 case Tabular::M_VALIGN_MIDDLE:
4302 case Tabular::VALIGN_TOP:
4303 case Tabular::VALIGN_BOTTOM:
4304 case Tabular::VALIGN_MIDDLE:
4305 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4306 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4307 tabular.setVAlignment(
4308 tabular.getCellNumber(i, j),
4312 case Tabular::MULTICOLUMN: {
4313 if (sel_row_start != sel_row_end) {
4314 #ifdef WITH_WARNINGS
4315 #warning Need I say it ? This is horrible.
4318 Alert::error(_("Error setting multicolumn"),
4319 _("You cannot set multicolumn vertically."));
4322 if (!cur.selection()) {
4323 // just multicol for one single cell
4324 // check whether we are completely in a multicol
4325 if (tabular.isMultiColumn(cur.idx()))
4326 tabular.unsetMultiColumn(cur.idx());
4328 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4331 // we have a selection so this means we just add all this
4332 // cells to form a multicolumn cell
4333 idx_type const s_start = cur.selBegin().idx();
4334 idx_type const s_end = cur.selEnd().idx();
4335 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4336 cur.idx() = s_start;
4339 cur.selection() = false;
4343 case Tabular::SET_ALL_LINES:
4345 case Tabular::UNSET_ALL_LINES:
4346 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4347 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4348 tabular.setAllLines(
4349 tabular.getCellNumber(i,j), setLines);
4352 case Tabular::SET_LONGTABULAR:
4353 tabular.setLongTabular(true);
4356 case Tabular::UNSET_LONGTABULAR:
4357 tabular.setLongTabular(false);
4360 case Tabular::SET_ROTATE_TABULAR:
4361 tabular.setRotateTabular(true);
4364 case Tabular::UNSET_ROTATE_TABULAR:
4365 tabular.setRotateTabular(false);
4368 case Tabular::TOGGLE_ROTATE_TABULAR:
4369 tabular.setRotateTabular(!tabular.getRotateTabular());
4372 case Tabular::SET_ROTATE_CELL:
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.setRotateCell(
4376 tabular.getCellNumber(i, j), true);
4379 case Tabular::UNSET_ROTATE_CELL:
4380 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4381 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4382 tabular.setRotateCell(
4383 tabular.getCellNumber(i, j), false);
4386 case Tabular::TOGGLE_ROTATE_CELL:
4388 bool oneNotRotated = oneCellHasRotationState(false,
4389 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4391 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4392 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4393 tabular.setRotateCell(tabular.getCellNumber(i, j),
4398 case Tabular::SET_USEBOX: {
4399 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4400 if (val == tabular.getUsebox(cur.idx()))
4401 val = Tabular::BOX_NONE;
4402 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4403 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4404 tabular.setUsebox(tabular.getCellNumber(i, j), val);
4408 case Tabular::UNSET_LTFIRSTHEAD:
4410 case Tabular::SET_LTFIRSTHEAD:
4411 tabular.getRowOfLTFirstHead(row, ltt);
4412 checkLongtableSpecial(ltt, value, flag);
4413 tabular.setLTHead(row, flag, ltt, true);
4416 case Tabular::UNSET_LTHEAD:
4418 case Tabular::SET_LTHEAD:
4419 tabular.getRowOfLTHead(row, ltt);
4420 checkLongtableSpecial(ltt, value, flag);
4421 tabular.setLTHead(row, flag, ltt, false);
4424 case Tabular::UNSET_LTFOOT:
4426 case Tabular::SET_LTFOOT:
4427 tabular.getRowOfLTFoot(row, ltt);
4428 checkLongtableSpecial(ltt, value, flag);
4429 tabular.setLTFoot(row, flag, ltt, false);
4432 case Tabular::UNSET_LTLASTFOOT:
4434 case Tabular::SET_LTLASTFOOT:
4435 tabular.getRowOfLTLastFoot(row, ltt);
4436 checkLongtableSpecial(ltt, value, flag);
4437 tabular.setLTFoot(row, flag, ltt, true);
4440 case Tabular::SET_LTNEWPAGE:
4441 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4444 case Tabular::SET_BOOKTABS:
4445 tabular.setBookTabs(true);
4448 case Tabular::UNSET_BOOKTABS:
4449 tabular.setBookTabs(false);
4452 case Tabular::SET_TOP_SPACE: {
4454 if (value == "default")
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4456 tabular.row_info[i].top_space_default = true;
4457 else if (isValidLength(value, &len))
4458 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4459 tabular.row_info[i].top_space_default = false;
4460 tabular.row_info[i].top_space = len;
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4464 tabular.row_info[i].top_space_default = false;
4465 tabular.row_info[i].top_space = len;
4470 case Tabular::SET_BOTTOM_SPACE: {
4472 if (value == "default")
4473 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4474 tabular.row_info[i].bottom_space_default = true;
4475 else if (isValidLength(value, &len))
4476 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4477 tabular.row_info[i].bottom_space_default = false;
4478 tabular.row_info[i].bottom_space = len;
4481 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4482 tabular.row_info[i].bottom_space_default = false;
4483 tabular.row_info[i].bottom_space = len;
4488 case Tabular::SET_INTERLINE_SPACE: {
4490 if (value == "default")
4491 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4492 tabular.row_info[i].interline_space_default = true;
4493 else if (isValidLength(value, &len))
4494 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4495 tabular.row_info[i].interline_space_default = false;
4496 tabular.row_info[i].interline_space = len;
4499 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4500 tabular.row_info[i].interline_space_default = false;
4501 tabular.row_info[i].interline_space = len;
4506 // dummy stuff just to avoid warnings
4507 case Tabular::LAST_ACTION:
4513 bool InsetTabular::showInsetDialog(BufferView * bv) const
4515 InsetTabularMailer(*this).showDialog(bv);
4520 void InsetTabular::openLayoutDialog(BufferView * bv) const
4522 InsetTabularMailer(*this).showDialog(bv);
4526 bool InsetTabular::copySelection(Cursor & cur)
4528 if (!cur.selection())
4533 getSelection(cur, rs, re, cs, ce);
4535 paste_tabular.reset(new Tabular(tabular));
4537 for (row_type i = 0; i < rs; ++i)
4538 paste_tabular->deleteRow(0);
4540 row_type const rows = re - rs + 1;
4541 while (paste_tabular->rows() > rows)
4542 paste_tabular->deleteRow(rows);
4544 paste_tabular->setTopLine(0, true, true);
4545 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4548 for (col_type i = 0; i < cs; ++i)
4549 paste_tabular->deleteColumn(0);
4551 col_type const columns = ce - cs + 1;
4552 while (paste_tabular->columns() > columns)
4553 paste_tabular->deleteColumn(columns);
4555 paste_tabular->setLeftLine(0, true, true);
4556 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4559 odocstringstream os;
4560 OutputParams const runparams(0);
4561 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4562 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4563 cap::copySelection(cur, os.str());
4565 // mark tabular stack dirty
4566 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4567 // when we (hopefully) have a one-for-all paste mechanism.
4568 // This must be called after cap::copySelection.
4569 dirtyTabularStack(true);
4575 bool InsetTabular::pasteClipboard(Cursor & cur)
4579 col_type const actcol = tabular.column_of_cell(cur.idx());
4580 row_type const actrow = tabular.row_of_cell(cur.idx());
4581 for (row_type r1 = 0, r2 = actrow;
4582 r1 < paste_tabular->rows() && r2 < tabular.rows();
4584 for (col_type c1 = 0, c2 = actcol;
4585 c1 < paste_tabular->columns() && c2 < tabular.columns();
4587 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4588 tabular.isPartOfMultiColumn(r2, c2))
4590 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4594 if (tabular.isPartOfMultiColumn(r2, c2)) {
4598 shared_ptr<InsetText> inset(
4599 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4600 tabular.setCellInset(r2, c2, inset);
4601 // FIXME: change tracking (MG)
4602 inset->setChange(Change(cur.buffer().params().trackChanges ?
4603 Change::INSERTED : Change::UNCHANGED));
4611 void InsetTabular::cutSelection(Cursor & cur)
4613 if (!cur.selection())
4618 getSelection(cur, rs, re, cs, ce);
4619 for (row_type i = rs; i <= re; ++i) {
4620 for (col_type j = cs; j <= ce; ++j) {
4621 shared_ptr<InsetText> t
4622 = cell(tabular.getCellNumber(i, j));
4623 if (cur.buffer().params().trackChanges)
4624 // FIXME: Change tracking (MG)
4625 t->setChange(Change(Change::DELETED));
4631 // cursor position might be invalid now
4632 if (cur.pit() > cur.lastpit())
4633 cur.pit() = cur.lastpit();
4634 if (cur.pos() > cur.lastpos())
4635 cur.pos() = cur.lastpos();
4636 cur.clearSelection();
4637 theSelection().haveSelection(false);
4641 bool InsetTabular::isRightToLeft(Cursor & cur) const
4643 BOOST_ASSERT(cur.depth() > 1);
4644 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4645 pos_type const parentpos = cur[cur.depth() - 2].pos();
4646 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4647 parentpos).language()->rightToLeft();
4651 void InsetTabular::getSelection(Cursor & cur,
4652 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4654 CursorSlice const & beg = cur.selBegin();
4655 CursorSlice const & end = cur.selEnd();
4656 cs = tabular.column_of_cell(beg.idx());
4657 ce = tabular.column_of_cell(end.idx());
4660 cs = tabular.column_of_cell(end.idx());
4662 ce = tabular.right_column_of_cell(end.idx());
4665 rs = tabular.row_of_cell(beg.idx());
4666 re = tabular.row_of_cell(end.idx());
4672 Text * InsetTabular::getText(int idx) const
4674 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4678 void InsetTabular::setChange(Change const & change)
4680 for (idx_type idx = 0; idx < nargs(); ++idx)
4681 cell(idx)->setChange(change);
4685 void InsetTabular::acceptChanges(BufferParams const & bparams)
4687 for (idx_type idx = 0; idx < nargs(); ++idx)
4688 cell(idx)->acceptChanges(bparams);
4692 void InsetTabular::rejectChanges(BufferParams const & bparams)
4694 for (idx_type idx = 0; idx < nargs(); ++idx)
4695 cell(idx)->rejectChanges(bparams);
4699 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4701 return tabular.getPWidth(cell).zero();
4705 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4708 if (buf.length() <= 0)
4711 Buffer const & buffer = *bv.buffer();
4715 col_type maxCols = 1;
4716 docstring::size_type const len = buf.length();
4717 docstring::size_type p = 0;
4720 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4728 maxCols = max(cols, maxCols);
4734 maxCols = max(cols, maxCols);
4740 paste_tabular.reset(
4741 new Tabular(buffer.params(), rows, maxCols));
4742 loctab = paste_tabular.get();
4744 dirtyTabularStack(true);
4747 cell = bv.cursor().idx();
4748 ocol = tabular.column_of_cell(cell);
4749 row = tabular.row_of_cell(cell);
4752 docstring::size_type op = 0;
4753 idx_type const cells = loctab->getNumberOfCells();
4756 rows = loctab->rows();
4757 col_type const columns = loctab->columns();
4759 while (cell < cells && p < len && row < rows &&
4760 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4766 // we can only set this if we are not too far right
4767 if (cols < columns) {
4768 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4769 Paragraph & par = inset->text_.getPar(0);
4770 Font const font = inset->text_.getFont(buffer, par, 0);
4771 inset->setText(buf.substr(op, p - op), font,
4772 buffer.params().trackChanges);
4778 // we can only set this if we are not too far right
4779 if (cols < columns) {
4780 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4781 Paragraph & par = inset->text_.getPar(0);
4782 Font const font = inset->text_.getFont(buffer, par, 0);
4783 inset->setText(buf.substr(op, p - op), font,
4784 buffer.params().trackChanges);
4789 cell = loctab->getCellNumber(row, cols);
4795 // check for the last cell if there is no trailing '\n'
4796 if (cell < cells && op < len) {
4797 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4798 Paragraph & par = inset->text_.getPar(0);
4799 Font const font = inset->text_.getFont(buffer, par, 0);
4800 inset->setText(buf.substr(op, len - op), font,
4801 buffer.params().trackChanges);
4807 void InsetTabular::addPreview(PreviewLoader & loader) const
4809 row_type const rows = tabular.rows();
4810 col_type const columns = tabular.columns();
4811 for (row_type i = 0; i < rows; ++i) {
4812 for (col_type j = 0; j < columns; ++j)
4813 tabular.getCellInset(i, j)->addPreview(loader);
4818 bool InsetTabular::tablemode(Cursor & cur) const
4820 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4827 string const InsetTabularMailer::name_("tabular");
4829 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4830 : inset_(const_cast<InsetTabular &>(inset))
4834 string const InsetTabularMailer::inset2string(Buffer const &) const
4836 return params2string(inset_);
4840 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4842 istringstream data(in);
4844 lex.setStream(data);
4851 if (!lex || token != name_)
4852 return print_mailer_error("InsetTabularMailer", in, 1,
4855 // This is part of the inset proper that is usually swallowed
4856 // by Buffer::readInset
4858 if (!lex || token != "Tabular")
4859 return print_mailer_error("InsetTabularMailer", in, 2,
4862 Buffer const & buffer = inset.buffer();
4863 inset.read(buffer, lex);
4867 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4870 data << name_ << ' ';
4871 inset.write(inset.buffer(), data);
4872 data << "\\end_inset\n";