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;
89 using graphics::PreviewLoader;
91 using frontend::Painter;
92 using frontend::Clipboard;
94 namespace Alert = frontend::Alert;
99 int const ADD_TO_HEIGHT = 2;
100 int const ADD_TO_TABULAR_WIDTH = 2;
101 int const default_line_space = 10;
102 int const WIDTH_OF_LINE = 5;
106 boost::scoped_ptr<Tabular> paste_tabular;
109 struct TabularFeature {
110 Tabular::Feature action;
115 TabularFeature tabularFeature[] =
117 { Tabular::APPEND_ROW, "append-row" },
118 { Tabular::APPEND_COLUMN, "append-column" },
119 { Tabular::DELETE_ROW, "delete-row" },
120 { Tabular::DELETE_COLUMN, "delete-column" },
121 { Tabular::COPY_ROW, "copy-row" },
122 { Tabular::COPY_COLUMN, "copy-column" },
123 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
124 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
125 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
126 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
127 { Tabular::ALIGN_LEFT, "align-left" },
128 { Tabular::ALIGN_RIGHT, "align-right" },
129 { Tabular::ALIGN_CENTER, "align-center" },
130 { Tabular::ALIGN_BLOCK, "align-block" },
131 { Tabular::VALIGN_TOP, "valign-top" },
132 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
133 { Tabular::VALIGN_MIDDLE, "valign-middle" },
134 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
135 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
136 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
137 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
138 { Tabular::M_ALIGN_LEFT, "m-align-left" },
139 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
140 { Tabular::M_ALIGN_CENTER, "m-align-center" },
141 { Tabular::M_VALIGN_TOP, "m-valign-top" },
142 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
143 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
144 { Tabular::MULTICOLUMN, "multicolumn" },
145 { Tabular::SET_ALL_LINES, "set-all-lines" },
146 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
147 { Tabular::SET_LONGTABULAR, "set-longtabular" },
148 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
149 { Tabular::SET_PWIDTH, "set-pwidth" },
150 { Tabular::SET_MPWIDTH, "set-mpwidth" },
151 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
152 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
153 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
154 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
155 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
156 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
157 { Tabular::SET_USEBOX, "set-usebox" },
158 { Tabular::SET_LTHEAD, "set-lthead" },
159 { Tabular::UNSET_LTHEAD, "unset-lthead" },
160 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
161 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
162 { Tabular::SET_LTFOOT, "set-ltfoot" },
163 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
164 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
165 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
166 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
167 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
168 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
169 { Tabular::SET_BOOKTABS, "set-booktabs" },
170 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
171 { Tabular::SET_TOP_SPACE, "set-top-space" },
172 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
173 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
174 { Tabular::LAST_ACTION, "" }
178 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
180 FeatureEqual(Tabular::Feature feature)
181 : feature_(feature) {}
182 bool operator()(TabularFeature const & tf) const {
183 return tf.action == feature_;
186 Tabular::Feature feature_;
191 string const write_attribute(string const & name, T const & t)
193 string const s = tostr(t);
194 return s.empty() ? s : " " + name + "=\"" + s + "\"";
198 string const write_attribute(string const & name, string const & t)
200 return t.empty() ? t : " " + name + "=\"" + t + "\"";
205 string const write_attribute(string const & name, docstring const & t)
207 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
212 string const write_attribute(string const & name, bool const & b)
214 // we write only true attribute values so we remove a bit of the
215 // file format bloat for tabulars.
216 return b ? write_attribute(name, convert<string>(b)) : string();
221 string const write_attribute(string const & name, int const & i)
223 // we write only true attribute values so we remove a bit of the
224 // file format bloat for tabulars.
225 return i ? write_attribute(name, convert<string>(i)) : string();
230 string const write_attribute(string const & name, Tabular::idx_type const & i)
232 // we write only true attribute values so we remove a bit of the
233 // file format bloat for tabulars.
234 return i ? write_attribute(name, convert<string>(i)) : string();
239 string const write_attribute(string const & name, Length const & value)
241 // we write only the value if we really have one same reson as above.
242 return value.zero() ? string() : write_attribute(name, value.asString());
246 string const tostr(LyXAlignment const & num)
251 case LYX_ALIGN_BLOCK:
255 case LYX_ALIGN_CENTER:
257 case LYX_ALIGN_RIGHT:
259 case LYX_ALIGN_LAYOUT:
261 case LYX_ALIGN_SPECIAL:
268 string const tostr(Tabular::VAlignment const & num)
271 case Tabular::LYX_VALIGN_TOP:
273 case Tabular::LYX_VALIGN_MIDDLE:
275 case Tabular::LYX_VALIGN_BOTTOM:
282 string const tostr(Tabular::BoxType const & num)
285 case Tabular::BOX_NONE:
287 case Tabular::BOX_PARBOX:
289 case Tabular::BOX_MINIPAGE:
296 // I would have liked a fromstr template a lot better. (Lgb)
297 bool string2type(string const str, LyXAlignment & num)
300 num = LYX_ALIGN_NONE;
301 else if (str == "block")
302 num = LYX_ALIGN_BLOCK;
303 else if (str == "left")
304 num = LYX_ALIGN_LEFT;
305 else if (str == "center")
306 num = LYX_ALIGN_CENTER;
307 else if (str == "right")
308 num = LYX_ALIGN_RIGHT;
315 bool string2type(string const str, Tabular::VAlignment & num)
318 num = Tabular::LYX_VALIGN_TOP;
319 else if (str == "middle" )
320 num = Tabular::LYX_VALIGN_MIDDLE;
321 else if (str == "bottom")
322 num = Tabular::LYX_VALIGN_BOTTOM;
329 bool string2type(string const str, Tabular::BoxType & num)
332 num = Tabular::BOX_NONE;
333 else if (str == "parbox")
334 num = Tabular::BOX_PARBOX;
335 else if (str == "minipage")
336 num = Tabular::BOX_MINIPAGE;
343 bool string2type(string const str, bool & num)
347 else if (str == "false")
355 bool getTokenValue(string const & str, char const * token, string & ret)
358 size_t token_length = strlen(token);
359 string::size_type pos = str.find(token);
361 if (pos == string::npos || pos + token_length + 1 >= str.length()
362 || str[pos + token_length] != '=')
364 pos += token_length + 1;
366 if (ch != '"' && ch != '\'') { // only read till next space
370 while (pos < str.length() - 1 && str[++pos] != ch)
377 bool getTokenValue(string const & str, char const * token, docstring & ret)
380 bool const success = getTokenValue(str, token, tmp);
381 ret = from_utf8(tmp);
386 bool getTokenValue(string const & str, char const * token, int & num)
390 if (!getTokenValue(str, token, tmp))
392 num = convert<int>(tmp);
397 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::VAlignment & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token,
413 Tabular::BoxType & num)
416 return getTokenValue(str, token, tmp) && string2type(tmp, num);
420 bool getTokenValue(string const & str, char const * token, bool & flag)
422 // set the flag always to false as this should be the default for bools
423 // not in the file-format.
426 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
430 bool getTokenValue(string const & str, char const * token, Length & len)
432 // set the lenght to be zero() as default as this it should be if not
433 // in the file format.
436 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
440 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
445 if (!getTokenValue(str, token, tmp))
447 if (tmp == "default") {
451 return isValidLength(tmp, &len);
455 void l_getline(istream & is, string & str)
458 while (str.empty()) {
460 if (!str.empty() && str[str.length() - 1] == '\r')
461 str.erase(str.length() - 1);
468 string const featureAsString(Tabular::Feature feature)
470 TabularFeature * end = tabularFeature +
471 sizeof(tabularFeature) / sizeof(TabularFeature);
472 TabularFeature * it = std::find_if(tabularFeature, end,
473 FeatureEqual(feature));
474 return (it == end) ? string() : it->feature;
479 /////////////////////////////////////////////////////////////////////
483 /////////////////////////////////////////////////////////////////////
486 Tabular::cellstruct::cellstruct(BufferParams const & bp)
489 multicolumn(Tabular::CELL_NORMAL),
490 alignment(LYX_ALIGN_CENTER),
491 valignment(LYX_VALIGN_TOP),
498 inset(new InsetText(bp))
502 Tabular::cellstruct::cellstruct(cellstruct const & cs)
504 width_of_cell(cs.width_of_cell),
505 multicolumn(cs.multicolumn),
506 alignment(cs.alignment),
507 valignment(cs.valignment),
508 top_line(cs.top_line),
509 bottom_line(cs.bottom_line),
510 left_line(cs.left_line),
511 right_line(cs.right_line),
514 align_special(cs.align_special),
516 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
520 Tabular::cellstruct &
521 Tabular::cellstruct::operator=(cellstruct cs)
529 Tabular::cellstruct::swap(cellstruct & rhs)
531 std::swap(cellno, rhs.cellno);
532 std::swap(width_of_cell, rhs.width_of_cell);
533 std::swap(multicolumn, rhs.multicolumn);
534 std::swap(alignment, rhs.alignment);
535 std::swap(valignment, rhs.valignment);
536 std::swap(top_line, rhs.top_line);
537 std::swap(bottom_line, rhs.bottom_line);
538 std::swap(left_line, rhs.left_line);
539 std::swap(right_line, rhs.right_line);
540 std::swap(usebox, rhs.usebox);
541 std::swap(rotate, rhs.rotate);
542 std::swap(align_special, rhs.align_special);
543 p_width.swap(rhs.p_width);
544 inset.swap(rhs.inset);
548 Tabular::rowstruct::rowstruct()
553 top_space_default(false),
554 bottom_space_default(false),
555 interline_space_default(false),
564 Tabular::columnstruct::columnstruct()
565 : alignment(LYX_ALIGN_CENTER),
566 valignment(LYX_VALIGN_TOP),
574 Tabular::ltType::ltType()
581 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
582 col_type columns_arg)
584 init(bp, rows_arg, columns_arg);
588 // activates all lines and sets all widths to 0
589 void Tabular::init(BufferParams const & bp, row_type rows_arg,
590 col_type columns_arg)
593 columns_ = columns_arg;
594 row_info = row_vector(rows_);
595 column_info = column_vector(columns_);
596 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
597 row_info.reserve(10);
598 column_info.reserve(10);
599 cell_info.reserve(100);
601 for (row_type i = 0; i < rows_; ++i)
602 cell_info[i].back().right_line = true;
603 row_info.back().bottom_line = true;
604 row_info.front().bottom_line = true;
605 column_info.back().right_line = true;
606 is_long_tabular = false;
608 use_booktabs = false;
612 void Tabular::fixCellNums()
615 for (row_type i = 0; i < rows_; ++i) {
616 for (col_type j = 0; j < columns_; ++j) {
617 // When debugging it can be nice to set
619 cell_info[i][j].inset->setDrawFrame(false);
620 cell_info[i][j].cellno = cellno++;
622 cell_info[i].back().right_line = true;
625 set_row_column_number_info();
629 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
633 row_type const row = row_of_cell(cell);
635 row_vector::iterator rit = row_info.begin() + row;
636 row_info.insert(rit, rowstruct());
637 // now set the values of the row before
638 row_info[row] = row_info[row + 1];
640 cell_vvector old(rows_ - 1);
641 for (row_type i = 0; i < rows_ - 1; ++i)
642 swap(cell_info[i], old[i]);
644 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
646 for (row_type i = 0; i <= row; ++i)
647 swap(cell_info[i], old[i]);
648 for (row_type i = row + 2; i < rows_; ++i)
649 swap(cell_info[i], old[i - 1]);
652 for (col_type j = 0; j < columns_; ++j)
653 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
655 set_row_column_number_info();
659 void Tabular::deleteRow(row_type const row)
661 // Not allowed to delete last row
665 row_info.erase(row_info.begin() + row);
666 cell_info.erase(cell_info.begin() + row);
672 void Tabular::copyRow(BufferParams const & bp, row_type const row)
676 row_info.insert(row_info.begin() + row, row_info[row]);
677 cell_info.insert(cell_info.begin() + row, cell_info[row]);
680 for (col_type j = 0; j < columns_; ++j)
681 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
683 set_row_column_number_info();
687 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
691 col_type const column = column_of_cell(cell);
692 column_vector::iterator cit = column_info.begin() + column + 1;
693 column_info.insert(cit, columnstruct());
694 // set the column values of the column before
695 column_info[column + 1] = column_info[column];
697 for (row_type i = 0; i < rows_; ++i) {
698 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
700 // care about multicolumns
701 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
702 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
704 if (column + 2 >= columns_
705 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
706 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
709 for (row_type i = 0; i < rows_; ++i) {
710 cell_info[i][column + 1].inset->clear();
712 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
718 void Tabular::deleteColumn(col_type const column)
720 // Not allowed to delete last column
724 column_info.erase(column_info.begin() + column);
725 for (row_type i = 0; i < rows_; ++i)
726 cell_info[i].erase(cell_info[i].begin() + column);
732 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
736 column_info.insert(column_info.begin() + column, column_info[column]);
738 for (row_type i = 0; i < rows_; ++i)
739 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
742 for (row_type i = 0; i < rows_; ++i)
743 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
748 void Tabular::set_row_column_number_info()
751 for (row_type row = 0; row < rows_; ++row) {
752 for (col_type column = 0; column < columns_; ++column) {
753 if (cell_info[row][column].multicolumn
754 != Tabular::CELL_PART_OF_MULTICOLUMN)
756 if (numberofcells == 0)
757 // FIXME: Is this intended?
758 cell_info[row][column].cellno = npos;
760 cell_info[row][column].cellno =
765 rowofcell.resize(numberofcells);
766 columnofcell.resize(numberofcells);
771 c < numberofcells && row < rows_ && column < columns_;) {
773 columnofcell[c] = column;
777 } while (column < columns_ &&
778 cell_info[row][column].multicolumn
779 == Tabular::CELL_PART_OF_MULTICOLUMN);
781 if (column == columns_) {
787 for (row_type row = 0; row < rows_; ++row) {
788 for (col_type column = 0; column < columns_; ++column) {
789 if (isPartOfMultiColumn(row,column))
791 cell_info[row][column].inset->setAutoBreakRows(
792 !getPWidth(getCellNumber(row, column)).zero());
798 Tabular::idx_type Tabular::getNumberOfCells() const
800 return numberofcells;
804 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
806 row_type const row = row_of_cell(cell);
808 for (col_type i = 0; i < columns_; ++i)
809 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
815 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
817 if (!wholerow && isMultiColumn(cell) &&
818 !(use_booktabs && row_of_cell(cell) == 0))
819 return cellinfo_of_cell(cell).top_line;
820 return row_info[row_of_cell(cell)].top_line;
824 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
826 if (!wholerow && isMultiColumn(cell) &&
827 !(use_booktabs && isLastRow(cell)))
828 return cellinfo_of_cell(cell).bottom_line;
829 return row_info[row_of_cell(cell)].bottom_line;
833 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
837 if (!wholecolumn && isMultiColumn(cell) &&
838 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
839 return cellinfo_of_cell(cell).left_line;
840 return column_info[column_of_cell(cell)].left_line;
844 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
848 if (!wholecolumn && isMultiColumn(cell) &&
849 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
850 return cellinfo_of_cell(cell).right_line;
851 return column_info[right_column_of_cell(cell)].right_line;
855 bool Tabular::topAlreadyDrawn(idx_type cell) const
857 row_type row = row_of_cell(cell);
858 if (row > 0 && !getAdditionalHeight(row)) {
859 col_type column = column_of_cell(cell);
862 && cell_info[row][column].multicolumn
863 == Tabular::CELL_PART_OF_MULTICOLUMN)
865 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
866 return row_info[row].bottom_line;
868 return cell_info[row][column].bottom_line;
874 bool Tabular::leftAlreadyDrawn(idx_type cell) const
876 col_type column = column_of_cell(cell);
878 row_type row = row_of_cell(cell);
880 (cell_info[row][column].multicolumn ==
881 Tabular::CELL_PART_OF_MULTICOLUMN));
882 if (getAdditionalWidth(cell_info[row][column].cellno))
884 return rightLine(cell_info[row][column].cellno);
890 bool Tabular::isLastRow(idx_type cell) const
892 return row_of_cell(cell) == rows_ - 1;
896 int Tabular::getAdditionalHeight(row_type row) const
898 if (!row || row >= rows_)
904 for (col_type column = 0; column < columns_ && bottom; ++column) {
905 switch (cell_info[row - 1][column].multicolumn) {
906 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
907 bottom = cell_info[row - 1][column].bottom_line;
909 case Tabular::CELL_NORMAL:
910 bottom = row_info[row - 1].bottom_line;
913 for (col_type column = 0; column < columns_ && top; ++column) {
914 switch (cell_info[row][column].multicolumn) {
915 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
916 top = cell_info[row][column].top_line;
918 case Tabular::CELL_NORMAL:
919 top = row_info[row].top_line;
922 int const interline_space = row_info[row - 1].interline_space_default ?
924 row_info[row - 1].interline_space.inPixels(width_of_tabular);
926 return interline_space + WIDTH_OF_LINE;
927 return interline_space;
931 int Tabular::getAdditionalWidth(idx_type cell) const
933 // internally already set in setWidthOfCell
934 // used to get it back in text.cpp
935 col_type const col = right_column_of_cell(cell);
936 row_type const row = row_of_cell(cell);
937 if (col < columns_ - 1 && rightLine(cell) &&
938 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
940 return WIDTH_OF_LINE;
946 // returns the maximum over all rows
947 int Tabular::getWidthOfColumn(idx_type cell) const
949 col_type const column1 = column_of_cell(cell);
950 col_type const column2 = right_column_of_cell(cell);
952 for (col_type i = column1; i <= column2; ++i)
953 result += column_info[i].width_of_column;
958 int Tabular::getWidthOfTabular() const
960 return width_of_tabular;
964 // returns true if a complete update is necessary, otherwise false
965 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
967 if (!isMultiColumn(cell))
970 row_type const row = row_of_cell(cell);
971 col_type const column1 = column_of_cell(cell);
972 col_type const column2 = right_column_of_cell(cell);
973 int const old_val = cell_info[row][column2].width_of_cell;
975 // first set columns to 0 so we can calculate the right width
976 for (col_type i = column1; i <= column2; ++i) {
977 cell_info[row][i].width_of_cell = 0;
979 // set the width to MAX_WIDTH until width > 0
980 int width = new_width + 2 * WIDTH_OF_LINE;
981 col_type i = column1;
982 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
983 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
984 width -= column_info[i].width_of_column;
987 cell_info[row][i].width_of_cell = width;
989 if (old_val != cell_info[row][column2].width_of_cell) {
990 // in this case we have to recalculate all multicolumn cells which
991 // have this column as one of theirs but not as last one
992 calculate_width_of_column_NMC(i);
993 recalculateMulticolumnsOfColumn(i);
994 calculate_width_of_column(i);
1000 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1002 // the last column does not have to be recalculated because all
1003 // multicolumns will have here there last multicolumn cell which
1004 // always will have the whole rest of the width of the cell.
1005 if (columns_ < 2 || column > (columns_ - 2))
1007 for (row_type row = 0; row < rows_; ++row) {
1008 int mc = cell_info[row][column].multicolumn;
1009 int nmc = cell_info[row][column+1].multicolumn;
1010 // we only have to update multicolumns which do not have this
1011 // column as their last column!
1012 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1013 (mc == CELL_PART_OF_MULTICOLUMN &&
1014 nmc == CELL_PART_OF_MULTICOLUMN))
1016 idx_type const cellno = cell_info[row][column].cellno;
1017 setWidthOfMulticolCell(cellno,
1018 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1024 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1026 row_type const row = row_of_cell(cell);
1027 col_type const column1 = column_of_cell(cell);
1032 if (rightLine(cell_info[row][column1].cellno, true) &&
1033 column1 < columns_ - 1 &&
1034 leftLine(cell_info[row][column1+1].cellno, true))
1036 add_width = WIDTH_OF_LINE;
1039 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1042 if (isMultiColumnReal(cell)) {
1043 tmp = setWidthOfMulticolCell(cell, new_width);
1045 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1046 cell_info[row][column1].width_of_cell = width;
1047 tmp = calculate_width_of_column_NMC(column1);
1049 recalculateMulticolumnsOfColumn(column1);
1052 for (col_type i = 0; i < columns_; ++i)
1053 calculate_width_of_column(i);
1054 calculate_width_of_tabular();
1059 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1062 if (!isMultiColumn(cell) || onlycolumn)
1063 column_info[column_of_cell(cell)].alignment = align;
1065 cellinfo_of_cell(cell).alignment = align;
1069 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1072 if (!isMultiColumn(cell) || onlycolumn)
1073 column_info[column_of_cell(cell)].valignment = align;
1075 cellinfo_of_cell(cell).valignment = align;
1082 * Allow line and paragraph breaks for fixed width cells or disallow them,
1083 * merge cell paragraphs and reset layout to standard for variable width
1086 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1088 inset->setAutoBreakRows(fixedWidth);
1092 // merge all paragraphs to one
1093 BufferParams const & bp = cur.bv().buffer()->params();
1094 while (inset->paragraphs().size() > 1)
1095 mergeParagraph(bp, inset->paragraphs(), 0);
1099 // undo information has already been recorded
1100 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1101 bp.getTextClass().defaultLayoutName());
1108 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1109 Length const & width)
1111 col_type const j = column_of_cell(cell);
1113 column_info[j].p_width = width;
1114 for (row_type i = 0; i < rows_; ++i) {
1115 idx_type const cell = getCellNumber(i, j);
1116 // because of multicolumns
1117 toggleFixedWidth(cur, getCellInset(cell).get(),
1118 !getPWidth(cell).zero());
1120 // cur paragraph can become invalid after paragraphs were merged
1121 if (cur.pit() > cur.lastpit())
1122 cur.pit() = cur.lastpit();
1123 // cur position can become invalid after newlines were removed
1124 if (cur.pos() > cur.lastpos())
1125 cur.pos() = cur.lastpos();
1129 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1130 Length const & width)
1132 if (!isMultiColumn(cell))
1135 cellinfo_of_cell(cell).p_width = width;
1136 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1137 // cur paragraph can become invalid after paragraphs were merged
1138 if (cur.pit() > cur.lastpit())
1139 cur.pit() = cur.lastpit();
1140 // cur position can become invalid after newlines were removed
1141 if (cur.pos() > cur.lastpos())
1142 cur.pos() = cur.lastpos();
1147 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1148 Tabular::Feature what)
1150 if (what == SET_SPECIAL_MULTI)
1151 cellinfo_of_cell(cell).align_special = special;
1153 column_info[column_of_cell(cell)].align_special = special;
1157 void Tabular::setAllLines(idx_type cell, bool line)
1159 setTopLine(cell, line);
1160 setBottomLine(cell, line);
1161 setRightLine(cell, line);
1162 setLeftLine(cell, line);
1166 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1168 row_type const row = row_of_cell(cell);
1169 if (wholerow || !isMultiColumn(cell))
1170 row_info[row].top_line = line;
1172 cellinfo_of_cell(cell).top_line = line;
1176 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1178 if (wholerow || !isMultiColumn(cell))
1179 row_info[row_of_cell(cell)].bottom_line = line;
1181 cellinfo_of_cell(cell).bottom_line = line;
1185 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1187 if (wholecolumn || !isMultiColumn(cell))
1188 column_info[column_of_cell(cell)].left_line = line;
1190 cellinfo_of_cell(cell).left_line = line;
1194 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1196 if (wholecolumn || !isMultiColumn(cell))
1197 column_info[right_column_of_cell(cell)].right_line = line;
1199 cellinfo_of_cell(cell).right_line = line;
1203 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1205 if (!onlycolumn && isMultiColumn(cell))
1206 return cellinfo_of_cell(cell).alignment;
1207 return column_info[column_of_cell(cell)].alignment;
1212 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1214 if (!onlycolumn && isMultiColumn(cell))
1215 return cellinfo_of_cell(cell).valignment;
1216 return column_info[column_of_cell(cell)].valignment;
1220 Length const Tabular::getPWidth(idx_type cell) const
1222 if (isMultiColumn(cell))
1223 return cellinfo_of_cell(cell).p_width;
1224 return column_info[column_of_cell(cell)].p_width;
1228 Length const Tabular::getColumnPWidth(idx_type cell) const
1230 return column_info[column_of_cell(cell)].p_width;
1234 Length const Tabular::getMColumnPWidth(idx_type cell) const
1236 if (isMultiColumn(cell))
1237 return cellinfo_of_cell(cell).p_width;
1242 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1244 if (what == SET_SPECIAL_MULTI)
1245 return cellinfo_of_cell(cell).align_special;
1246 return column_info[column_of_cell(cell)].align_special;
1250 int Tabular::getWidthOfCell(idx_type cell) const
1252 row_type const row = row_of_cell(cell);
1253 col_type const column1 = column_of_cell(cell);
1254 col_type const column2 = right_column_of_cell(cell);
1256 for (col_type i = column1; i <= column2; ++i)
1257 result += cell_info[row][i].width_of_cell;
1262 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1266 switch (getAlignment(cell)) {
1267 case LYX_ALIGN_CENTER:
1268 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1270 case LYX_ALIGN_RIGHT:
1271 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1272 // + getAdditionalWidth(cell);
1275 // LYX_ALIGN_LEFT: nothing :-)
1279 // the LaTeX Way :-(
1285 bool Tabular::isFirstCellInRow(idx_type cell) const
1287 return column_of_cell(cell) == 0;
1291 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1293 if (row > rows_ - 1)
1295 return cell_info[row][0].cellno;
1299 bool Tabular::isLastCellInRow(idx_type cell) const
1301 return right_column_of_cell(cell) == columns_ - 1;
1305 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1307 if (row > rows_ - 1)
1309 return cell_info[row][columns_-1].cellno;
1313 void Tabular::calculate_width_of_column(col_type column)
1316 for (row_type i = 0; i < rows_; ++i)
1317 maximum = max(cell_info[i][column].width_of_cell, maximum);
1318 column_info[column].width_of_column = maximum;
1323 // Calculate the columns regarding ONLY the normal cells and if this
1324 // column is inside a multicolumn cell then use it only if its the last
1325 // column of this multicolumn cell as this gives an added width to the
1326 // column, all the rest should be adapted!
1328 bool Tabular::calculate_width_of_column_NMC(col_type column)
1330 int const old_column_width = column_info[column].width_of_column;
1332 for (row_type i = 0; i < rows_; ++i) {
1333 idx_type cell = getCellNumber(i, column);
1334 bool ismulti = isMultiColumnReal(cell);
1335 if ((!ismulti || column == right_column_of_cell(cell)) &&
1336 cell_info[i][column].width_of_cell > max)
1338 max = cell_info[i][column].width_of_cell;
1341 column_info[column].width_of_column = max;
1342 return column_info[column].width_of_column != old_column_width;
1346 void Tabular::calculate_width_of_tabular()
1348 width_of_tabular = 0;
1349 for (col_type i = 0; i < columns_; ++i)
1350 width_of_tabular += column_info[i].width_of_column;
1354 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1356 if (cell >= numberofcells)
1360 return rowofcell[cell];
1364 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1366 if (cell >= numberofcells)
1367 return columns_ - 1;
1370 return columnofcell[cell];
1374 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1376 row_type const row = row_of_cell(cell);
1377 col_type column = column_of_cell(cell);
1378 while (column < columns_ - 1 &&
1379 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1385 void Tabular::write(Buffer const & buf, ostream & os) const
1389 << write_attribute("version", 3)
1390 << write_attribute("rows", rows_)
1391 << write_attribute("columns", columns_)
1393 // global longtable options
1395 << write_attribute("rotate", rotate)
1396 << write_attribute("booktabs", use_booktabs)
1397 << write_attribute("islongtable", is_long_tabular)
1398 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1399 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1400 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1401 << write_attribute("headTopDL", endhead.topDL)
1402 << write_attribute("headBottomDL", endhead.bottomDL)
1403 << write_attribute("footTopDL", endfoot.topDL)
1404 << write_attribute("footBottomDL", endfoot.bottomDL)
1405 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1406 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1407 << write_attribute("lastFootEmpty", endlastfoot.empty)
1409 for (col_type j = 0; j < columns_; ++j) {
1411 << write_attribute("alignment", column_info[j].alignment)
1412 << write_attribute("valignment", column_info[j].valignment)
1413 << write_attribute("leftline", column_info[j].left_line)
1414 << write_attribute("rightline", column_info[j].right_line)
1415 << write_attribute("width", column_info[j].p_width.asString())
1416 << write_attribute("special", column_info[j].align_special)
1419 for (row_type i = 0; i < rows_; ++i) {
1421 << write_attribute("topline", row_info[i].top_line)
1422 << write_attribute("bottomline", row_info[i].bottom_line);
1423 static const string def("default");
1424 if (row_info[i].top_space_default)
1425 os << write_attribute("topspace", def);
1427 os << write_attribute("topspace", row_info[i].top_space);
1428 if (row_info[i].bottom_space_default)
1429 os << write_attribute("bottomspace", def);
1431 os << write_attribute("bottomspace", row_info[i].bottom_space);
1432 if (row_info[i].interline_space_default)
1433 os << write_attribute("interlinespace", def);
1435 os << write_attribute("interlinespace", row_info[i].interline_space);
1436 os << write_attribute("endhead", row_info[i].endhead)
1437 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1438 << write_attribute("endfoot", row_info[i].endfoot)
1439 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1440 << write_attribute("newpage", row_info[i].newpage)
1442 for (col_type j = 0; j < columns_; ++j) {
1444 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1445 << write_attribute("alignment", cell_info[i][j].alignment)
1446 << write_attribute("valignment", cell_info[i][j].valignment)
1447 << write_attribute("topline", cell_info[i][j].top_line)
1448 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1449 << write_attribute("leftline", cell_info[i][j].left_line)
1450 << write_attribute("rightline", cell_info[i][j].right_line)
1451 << write_attribute("rotate", cell_info[i][j].rotate)
1452 << write_attribute("usebox", cell_info[i][j].usebox)
1453 << write_attribute("width", cell_info[i][j].p_width)
1454 << write_attribute("special", cell_info[i][j].align_special)
1456 os << "\\begin_inset ";
1457 cell_info[i][j].inset->write(buf, os);
1458 os << "\n\\end_inset\n"
1463 os << "</lyxtabular>\n";
1467 void Tabular::read(Buffer const & buf, Lexer & lex)
1470 istream & is = lex.getStream();
1472 l_getline(is, line);
1473 if (!prefixIs(line, "<lyxtabular ")
1474 && !prefixIs(line, "<Tabular ")) {
1475 BOOST_ASSERT(false);
1480 if (!getTokenValue(line, "version", version))
1482 BOOST_ASSERT(version >= 2);
1485 if (!getTokenValue(line, "rows", rows_arg))
1488 if (!getTokenValue(line, "columns", columns_arg))
1490 init(buf.params(), rows_arg, columns_arg);
1491 l_getline(is, line);
1492 if (!prefixIs(line, "<features")) {
1493 lyxerr << "Wrong tabular format (expected <features ...> got"
1494 << line << ')' << endl;
1497 getTokenValue(line, "rotate", rotate);
1498 getTokenValue(line, "booktabs", use_booktabs);
1499 getTokenValue(line, "islongtable", is_long_tabular);
1500 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1501 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1502 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1503 getTokenValue(line, "headTopDL", endhead.topDL);
1504 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1505 getTokenValue(line, "footTopDL", endfoot.topDL);
1506 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1507 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1508 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1509 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1511 for (col_type j = 0; j < columns_; ++j) {
1513 if (!prefixIs(line,"<column")) {
1514 lyxerr << "Wrong tabular format (expected <column ...> got"
1515 << line << ')' << endl;
1518 getTokenValue(line, "alignment", column_info[j].alignment);
1519 getTokenValue(line, "valignment", column_info[j].valignment);
1520 getTokenValue(line, "leftline", column_info[j].left_line);
1521 getTokenValue(line, "rightline", column_info[j].right_line);
1522 getTokenValue(line, "width", column_info[j].p_width);
1523 getTokenValue(line, "special", column_info[j].align_special);
1526 for (row_type i = 0; i < rows_; ++i) {
1527 l_getline(is, line);
1528 if (!prefixIs(line, "<row")) {
1529 lyxerr << "Wrong tabular format (expected <row ...> got"
1530 << line << ')' << endl;
1533 getTokenValue(line, "topline", row_info[i].top_line);
1534 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1535 getTokenValue(line, "topspace", row_info[i].top_space,
1536 row_info[i].top_space_default);
1537 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1538 row_info[i].bottom_space_default);
1539 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1540 row_info[i].interline_space_default);
1541 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1542 getTokenValue(line, "endhead", row_info[i].endhead);
1543 getTokenValue(line, "endfoot", row_info[i].endfoot);
1544 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1545 getTokenValue(line, "newpage", row_info[i].newpage);
1546 for (col_type j = 0; j < columns_; ++j) {
1547 l_getline(is, line);
1548 if (!prefixIs(line, "<cell")) {
1549 lyxerr << "Wrong tabular format (expected <cell ...> got"
1550 << line << ')' << endl;
1553 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1554 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1555 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1556 getTokenValue(line, "topline", cell_info[i][j].top_line);
1557 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1558 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1559 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1560 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1561 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1562 getTokenValue(line, "width", cell_info[i][j].p_width);
1563 getTokenValue(line, "special", cell_info[i][j].align_special);
1564 l_getline(is, line);
1565 if (prefixIs(line, "\\begin_inset")) {
1566 cell_info[i][j].inset->read(buf, lex);
1567 l_getline(is, line);
1569 if (!prefixIs(line, "</cell>")) {
1570 lyxerr << "Wrong tabular format (expected </cell> got"
1571 << line << ')' << endl;
1575 l_getline(is, line);
1576 if (!prefixIs(line, "</row>")) {
1577 lyxerr << "Wrong tabular format (expected </row> got"
1578 << line << ')' << endl;
1582 while (!prefixIs(line, "</lyxtabular>")) {
1583 l_getline(is, line);
1585 set_row_column_number_info();
1589 bool Tabular::isMultiColumn(idx_type cell) const
1591 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1595 bool Tabular::isMultiColumnReal(idx_type cell) const
1597 return column_of_cell(cell) != right_column_of_cell(cell) &&
1598 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1602 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1604 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1608 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1611 cellstruct & cs = cellinfo_of_cell(cell);
1612 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1613 cs.alignment = column_info[column_of_cell(cell)].alignment;
1614 cs.top_line = row_info[row_of_cell(cell)].top_line;
1615 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1616 cs.left_line = column_info[column_of_cell(cell)].left_line;
1617 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1618 for (idx_type i = 1; i < number; ++i) {
1619 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1620 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1621 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1624 set_row_column_number_info();
1628 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1630 row_type const row = row_of_cell(cell);
1631 col_type column = column_of_cell(cell);
1632 idx_type result = 1;
1634 while (column < columns_ &&
1635 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1644 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1646 row_type const row = row_of_cell(cell);
1647 col_type column = column_of_cell(cell);
1649 idx_type result = 0;
1651 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1652 cell_info[row][column].multicolumn = CELL_NORMAL;
1654 while (column < columns_ &&
1655 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1657 cell_info[row][column].multicolumn = CELL_NORMAL;
1662 set_row_column_number_info();
1667 void Tabular::setBookTabs(bool what)
1669 use_booktabs = what;
1673 bool Tabular::useBookTabs() const
1675 return use_booktabs;
1679 void Tabular::setLongTabular(bool what)
1681 is_long_tabular = what;
1685 bool Tabular::isLongTabular() const
1687 return is_long_tabular;
1691 void Tabular::setRotateTabular(bool flag)
1697 bool Tabular::getRotateTabular() const
1703 void Tabular::setRotateCell(idx_type cell, bool flag)
1705 cellinfo_of_cell(cell).rotate = flag;
1709 bool Tabular::getRotateCell(idx_type cell) const
1711 return cellinfo_of_cell(cell).rotate;
1715 bool Tabular::needRotating() const
1719 for (row_type i = 0; i < rows_; ++i)
1720 for (col_type j = 0; j < columns_; ++j)
1721 if (cell_info[i][j].rotate)
1727 bool Tabular::isLastCell(idx_type cell) const
1729 if (cell + 1 < numberofcells)
1735 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1737 if (row_of_cell(cell) > 0)
1738 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1743 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1745 if (row_of_cell(cell) + 1 < rows_)
1746 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1751 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1753 if (row_of_cell(cell) == 0)
1755 if (!isMultiColumn(cell))
1756 return getCellAbove(cell);
1757 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1761 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1763 if (row_of_cell(cell) + 1 >= rows_)
1765 if (!isMultiColumn(cell))
1766 return getCellBelow(cell);
1767 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1771 Tabular::idx_type Tabular::getCellNumber(row_type row,
1772 col_type column) const
1774 BOOST_ASSERT(column != npos && column < columns_ &&
1775 row != npos && row < rows_);
1776 return cell_info[row][column].cellno;
1780 void Tabular::setUsebox(idx_type cell, BoxType type)
1782 cellinfo_of_cell(cell).usebox = type;
1786 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1788 if (column_info[column_of_cell(cell)].p_width.zero() &&
1789 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1791 if (cellinfo_of_cell(cell).usebox > 1)
1792 return cellinfo_of_cell(cell).usebox;
1793 return useParbox(cell);
1798 // This are functions used for the longtable support
1800 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1806 row_info[row].endfirsthead = flag;
1810 row_info[row].endhead = flag;
1815 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1818 hd.set = haveLTHead();
1819 return row_info[row].endhead;
1823 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1826 hd.set = haveLTFirstHead();
1827 return row_info[row].endfirsthead;
1831 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1837 row_info[row].endlastfoot = flag;
1841 row_info[row].endfoot = flag;
1846 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1849 fd.set = haveLTFoot();
1850 return row_info[row].endfoot;
1854 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1857 fd.set = haveLTLastFoot();
1858 return row_info[row].endlastfoot;
1862 void Tabular::setLTNewPage(row_type row, bool what)
1864 row_info[row].newpage = what;
1868 bool Tabular::getLTNewPage(row_type row) const
1870 return row_info[row].newpage;
1874 bool Tabular::haveLTHead() const
1876 for (row_type i = 0; i < rows_; ++i)
1877 if (row_info[i].endhead)
1883 bool Tabular::haveLTFirstHead() const
1885 if (endfirsthead.empty)
1887 for (row_type i = 0; i < rows_; ++i)
1888 if (row_info[i].endfirsthead)
1894 bool Tabular::haveLTFoot() const
1896 for (row_type i = 0; i < rows_; ++i)
1897 if (row_info[i].endfoot)
1903 bool Tabular::haveLTLastFoot() const
1905 if (endlastfoot.empty)
1907 for (row_type i = 0; i < rows_; ++i)
1908 if (row_info[i].endlastfoot)
1914 // end longtable support functions
1916 void Tabular::setAscentOfRow(row_type row, int height)
1918 if (row >= rows_ || row_info[row].ascent_of_row == height)
1920 row_info[row].ascent_of_row = height;
1924 void Tabular::setDescentOfRow(row_type row, int height)
1926 if (row >= rows_ || row_info[row].descent_of_row == height)
1928 row_info[row].descent_of_row = height;
1932 int Tabular::getAscentOfRow(row_type row) const
1936 return row_info[row].ascent_of_row;
1940 int Tabular::getDescentOfRow(row_type row) const
1942 BOOST_ASSERT(row < rows_);
1943 return row_info[row].descent_of_row;
1947 int Tabular::getHeightOfTabular() const
1950 for (row_type row = 0; row < rows_; ++row)
1951 height += getAscentOfRow(row) + getDescentOfRow(row) +
1952 getAdditionalHeight(row);
1957 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1959 BOOST_ASSERT(row < rows_);
1960 BOOST_ASSERT(column < columns_);
1961 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1965 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1967 // FIXME: assert or return 0 as in TeXBottomHLine()?
1968 BOOST_ASSERT(row != npos);
1969 BOOST_ASSERT(row < rows_);
1971 idx_type const fcell = getFirstCellInRow(row);
1972 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1975 for (idx_type i = fcell; i < n; ++i) {
1979 if (use_booktabs && row == 0) {
1981 } else if (tmp == n - fcell) {
1982 os << (use_booktabs ? "\\midrule " : "\\hline ");
1984 for (idx_type i = fcell; i < n; ++i) {
1986 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1987 << column_of_cell(i) + 1
1989 << right_column_of_cell(i) + 1
2001 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2003 // FIXME: return 0 or assert as in TeXTopHLine()?
2004 if (row == npos || row >= rows_)
2007 idx_type const fcell = getFirstCellInRow(row);
2008 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2011 for (idx_type i = fcell; i < n; ++i) {
2015 if (use_booktabs && row == rows_ - 1) {
2016 os << "\\bottomrule";
2017 } else if (tmp == n - fcell) {
2018 os << (use_booktabs ? "\\midrule" : "\\hline");
2020 for (idx_type i = fcell; i < n; ++i) {
2021 if (bottomLine(i)) {
2022 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2023 << column_of_cell(i) + 1
2025 << right_column_of_cell(i) + 1
2037 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2041 if (getRotateCell(cell)) {
2042 os << "\\begin{sideways}\n";
2045 if (isMultiColumn(cell)) {
2046 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2047 if (leftLine(cell) &&
2048 (isFirstCellInRow(cell) ||
2049 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2050 !rightLine(cell - 1, true))))
2052 if (!cellinfo_of_cell(cell).align_special.empty()) {
2053 os << cellinfo_of_cell(cell).align_special;
2055 if (!getPWidth(cell).zero()) {
2056 switch (getVAlignment(cell)) {
2057 case LYX_VALIGN_TOP:
2060 case LYX_VALIGN_MIDDLE:
2063 case LYX_VALIGN_BOTTOM:
2068 << from_ascii(getPWidth(cell).asLatexString())
2071 switch (getAlignment(cell)) {
2072 case LYX_ALIGN_LEFT:
2075 case LYX_ALIGN_RIGHT:
2082 } // end if else !getPWidth
2083 } // end if else !cellinfo_of_cell
2084 if (rightLine(cell))
2086 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2091 if (getUsebox(cell) == BOX_PARBOX) {
2093 switch (getVAlignment(cell)) {
2094 case LYX_VALIGN_TOP:
2097 case LYX_VALIGN_MIDDLE:
2100 case LYX_VALIGN_BOTTOM:
2104 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2106 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2107 os << "\\begin{minipage}[";
2108 switch (getVAlignment(cell)) {
2109 case LYX_VALIGN_TOP:
2112 case LYX_VALIGN_MIDDLE:
2115 case LYX_VALIGN_BOTTOM:
2119 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2127 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2132 if (getUsebox(cell) == BOX_PARBOX)
2134 else if (getUsebox(cell) == BOX_MINIPAGE) {
2135 os << "%\n\\end{minipage}";
2138 if (isMultiColumn(cell)) {
2141 if (getRotateCell(cell)) {
2142 os << "%\n\\end{sideways}";
2149 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2150 OutputParams const & runparams) const
2152 if (!is_long_tabular)
2156 // output header info
2158 if (endhead.topDL) {
2162 for (row_type i = 0; i < rows_; ++i) {
2163 if (row_info[i].endhead) {
2164 ret += TeXRow(os, i, buf, runparams);
2167 if (endhead.bottomDL) {
2171 os << "\\endhead\n";
2173 if (endfirsthead.empty) {
2174 os << "\\endfirsthead\n";
2178 // output firstheader info
2179 if (haveLTFirstHead()) {
2180 if (endfirsthead.topDL) {
2184 for (row_type i = 0; i < rows_; ++i) {
2185 if (row_info[i].endfirsthead) {
2186 ret += TeXRow(os, i, buf, runparams);
2189 if (endfirsthead.bottomDL) {
2193 os << "\\endfirsthead\n";
2196 // output footer info
2198 if (endfoot.topDL) {
2202 for (row_type i = 0; i < rows_; ++i) {
2203 if (row_info[i].endfoot) {
2204 ret += TeXRow(os, i, buf, runparams);
2207 if (endfoot.bottomDL) {
2211 os << "\\endfoot\n";
2213 if (endlastfoot.empty) {
2214 os << "\\endlastfoot\n";
2218 // output lastfooter info
2219 if (haveLTLastFoot()) {
2220 if (endlastfoot.topDL) {
2224 for (row_type i = 0; i < rows_; ++i) {
2225 if (row_info[i].endlastfoot) {
2226 ret += TeXRow(os, i, buf, runparams);
2229 if (endlastfoot.bottomDL) {
2233 os << "\\endlastfoot\n";
2240 bool Tabular::isValidRow(row_type row) const
2242 if (!is_long_tabular)
2244 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2245 !row_info[row].endfoot && !row_info[row].endlastfoot;
2249 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2250 OutputParams const & runparams) const
2252 idx_type cell = getCellNumber(i, 0);
2253 int ret = TeXTopHLine(os, i);
2254 if (row_info[i].top_space_default) {
2256 os << "\\addlinespace\n";
2258 os << "\\noalign{\\vskip\\doublerulesep}\n";
2260 } else if(!row_info[i].top_space.zero()) {
2262 os << "\\addlinespace["
2263 << from_ascii(row_info[i].top_space.asLatexString())
2266 os << "\\noalign{\\vskip"
2267 << from_ascii(row_info[i].top_space.asLatexString())
2273 for (col_type j = 0; j < columns_; ++j) {
2274 if (isPartOfMultiColumn(i, j))
2276 ret += TeXCellPreamble(os, cell);
2277 shared_ptr<InsetText> inset = getCellInset(cell);
2279 Paragraph const & par = inset->paragraphs().front();
2280 bool rtl = par.isRightToLeftPar(buf.params())
2282 && getPWidth(cell).zero();
2285 if (par.getParLanguage(buf.params())->lang() ==
2288 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2290 // currently, remaning RTL languages are arabic_arabtex and hebrew
2294 ret += inset->latex(buf, os, runparams);
2298 ret += TeXCellPostamble(os, cell);
2299 if (!isLastCellInRow(cell)) { // not last cell in row
2304 os << "\\tabularnewline";
2305 if (row_info[i].bottom_space_default) {
2307 os << "\\addlinespace";
2309 os << "[\\doublerulesep]";
2310 } else if (!row_info[i].bottom_space.zero()) {
2312 os << "\\addlinespace";
2314 << from_ascii(row_info[i].bottom_space.asLatexString())
2319 ret += TeXBottomHLine(os, i);
2320 if (row_info[i].interline_space_default) {
2322 os << "\\addlinespace\n";
2324 os << "\\noalign{\\vskip\\doublerulesep}\n";
2326 } else if (!row_info[i].interline_space.zero()) {
2328 os << "\\addlinespace["
2329 << from_ascii(row_info[i].interline_space.asLatexString())
2332 os << "\\noalign{\\vskip"
2333 << from_ascii(row_info[i].interline_space.asLatexString())
2341 int Tabular::latex(Buffer const & buf, odocstream & os,
2342 OutputParams const & runparams) const
2346 //+---------------------------------------------------------------------
2347 //+ first the opening preamble +
2348 //+---------------------------------------------------------------------
2351 os << "\\begin{sideways}\n";
2354 if (is_long_tabular)
2355 os << "\\begin{longtable}{";
2357 os << "\\begin{tabular}{";
2358 for (col_type i = 0; i < columns_; ++i) {
2359 if (!use_booktabs && column_info[i].left_line)
2361 if (!column_info[i].align_special.empty()) {
2362 os << column_info[i].align_special;
2364 if (!column_info[i].p_width.zero()) {
2365 switch (column_info[i].alignment) {
2366 case LYX_ALIGN_LEFT:
2367 os << ">{\\raggedright}";
2369 case LYX_ALIGN_RIGHT:
2370 os << ">{\\raggedleft}";
2372 case LYX_ALIGN_CENTER:
2373 os << ">{\\centering}";
2375 case LYX_ALIGN_NONE:
2376 case LYX_ALIGN_BLOCK:
2377 case LYX_ALIGN_LAYOUT:
2378 case LYX_ALIGN_SPECIAL:
2382 switch (column_info[i].valignment) {
2383 case LYX_VALIGN_TOP:
2386 case LYX_VALIGN_MIDDLE:
2389 case LYX_VALIGN_BOTTOM:
2394 << from_ascii(column_info[i].p_width.asLatexString())
2397 switch (column_info[i].alignment) {
2398 case LYX_ALIGN_LEFT:
2401 case LYX_ALIGN_RIGHT:
2408 } // end if else !column_info[i].p_width
2409 } // end if else !column_info[i].align_special
2410 if (!use_booktabs && column_info[i].right_line)
2416 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2418 //+---------------------------------------------------------------------
2419 //+ the single row and columns (cells) +
2420 //+---------------------------------------------------------------------
2422 for (row_type i = 0; i < rows_; ++i) {
2423 if (isValidRow(i)) {
2424 ret += TeXRow(os, i, buf, runparams);
2425 if (is_long_tabular && row_info[i].newpage) {
2426 os << "\\newpage\n";
2432 //+---------------------------------------------------------------------
2433 //+ the closing of the tabular +
2434 //+---------------------------------------------------------------------
2436 if (is_long_tabular)
2437 os << "\\end{longtable}";
2439 os << "\\end{tabular}";
2441 os << "\n\\end{sideways}";
2449 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2450 OutputParams const & runparams) const
2453 idx_type cell = getFirstCellInRow(row);
2456 for (col_type j = 0; j < columns_; ++j) {
2457 if (isPartOfMultiColumn(row, j))
2460 os << "<entry align=\"";
2461 switch (getAlignment(cell)) {
2462 case LYX_ALIGN_LEFT:
2465 case LYX_ALIGN_RIGHT:
2473 os << "\" valign=\"";
2474 switch (getVAlignment(cell)) {
2475 case LYX_VALIGN_TOP:
2478 case LYX_VALIGN_BOTTOM:
2481 case LYX_VALIGN_MIDDLE:
2486 if (isMultiColumn(cell)) {
2487 os << " namest=\"col" << j << "\" ";
2488 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2492 ret += getCellInset(cell)->docbook(buf, os, runparams);
2501 int Tabular::docbook(Buffer const & buf, odocstream & os,
2502 OutputParams const & runparams) const
2506 //+---------------------------------------------------------------------
2507 //+ first the opening preamble +
2508 //+---------------------------------------------------------------------
2510 os << "<tgroup cols=\"" << columns_
2511 << "\" colsep=\"1\" rowsep=\"1\">\n";
2513 for (col_type i = 0; i < columns_; ++i) {
2514 os << "<colspec colname=\"col" << i << "\" align=\"";
2515 switch (column_info[i].alignment) {
2516 case LYX_ALIGN_LEFT:
2519 case LYX_ALIGN_RIGHT:
2527 if (runparams.flavor == OutputParams::XML)
2533 //+---------------------------------------------------------------------
2534 //+ Long Tabular case +
2535 //+---------------------------------------------------------------------
2537 // output header info
2538 if (haveLTHead() || haveLTFirstHead()) {
2541 for (row_type i = 0; i < rows_; ++i) {
2542 if (row_info[i].endhead || row_info[i].endfirsthead) {
2543 ret += docbookRow(buf, os, i, runparams);
2549 // output footer info
2550 if (haveLTFoot() || haveLTLastFoot()) {
2553 for (row_type i = 0; i < rows_; ++i) {
2554 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2555 ret += docbookRow(buf, os, i, runparams);
2562 //+---------------------------------------------------------------------
2563 //+ the single row and columns (cells) +
2564 //+---------------------------------------------------------------------
2568 for (row_type i = 0; i < rows_; ++i) {
2569 if (isValidRow(i)) {
2570 ret += docbookRow(buf, os, i, runparams);
2575 //+---------------------------------------------------------------------
2576 //+ the closing of the tabular +
2577 //+---------------------------------------------------------------------
2586 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2587 vector<unsigned int> const & clen) const
2589 idx_type const fcell = getFirstCellInRow(row);
2590 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2593 for (idx_type i = fcell; i < n; ++i) {
2603 for (idx_type i = fcell; i < n; ++i) {
2614 col_type column = column_of_cell(i);
2615 int len = clen[column];
2616 while (column < columns_ - 1
2617 && isPartOfMultiColumn(row, ++column))
2618 len += clen[column] + 4;
2619 os << docstring(len, ch);
2634 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2635 vector<unsigned int> const & clen) const
2637 idx_type const fcell = getFirstCellInRow(row);
2638 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2641 for (idx_type i = fcell; i < n; ++i) {
2642 if (bottomLine(i)) {
2651 for (idx_type i = fcell; i < n; ++i) {
2652 if (bottomLine(i)) {
2662 col_type column = column_of_cell(i);
2663 int len = clen[column];
2664 while (column < columns_ -1
2665 && isPartOfMultiColumn(row, ++column))
2666 len += clen[column] + 4;
2667 os << docstring(len, ch);
2668 if (bottomLine(i)) {
2682 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2683 OutputParams const & runparams,
2684 idx_type cell, row_type row, col_type column,
2685 vector<unsigned int> const & clen,
2686 bool onlydata) const
2688 odocstringstream sstr;
2689 getCellInset(cell)->plaintext(buf, sstr, runparams);
2701 unsigned int len1 = sstr.str().length();
2702 unsigned int len2 = clen[column];
2703 while (column < columns_ -1
2704 && isPartOfMultiColumn(row, ++column))
2705 len2 += clen[column] + 4;
2708 switch (getAlignment(cell)) {
2710 case LYX_ALIGN_LEFT:
2713 case LYX_ALIGN_RIGHT:
2717 case LYX_ALIGN_CENTER:
2723 os << docstring(len1, ' ') << sstr.str()
2724 << docstring(len2, ' ');
2726 if (rightLine(cell))
2733 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2734 OutputParams const & runparams, int const depth,
2735 bool onlydata, unsigned char delim) const
2737 // first calculate the width of the single columns
2738 vector<unsigned int> clen(columns_);
2741 // first all non (real) multicolumn cells!
2742 for (col_type j = 0; j < columns_; ++j) {
2744 for (row_type i = 0; i < rows_; ++i) {
2745 idx_type cell = getCellNumber(i, j);
2746 if (isMultiColumnReal(cell))
2748 odocstringstream sstr;
2749 getCellInset(cell)->plaintext(buf, sstr, runparams);
2750 if (clen[j] < sstr.str().length())
2751 clen[j] = sstr.str().length();
2754 // then all (real) multicolumn cells!
2755 for (col_type j = 0; j < columns_; ++j) {
2756 for (row_type i = 0; i < rows_; ++i) {
2757 idx_type cell = getCellNumber(i, j);
2758 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2760 odocstringstream sstr;
2761 getCellInset(cell)->plaintext(buf, sstr, runparams);
2762 int len = int(sstr.str().length());
2763 idx_type const n = cells_in_multicolumn(cell);
2764 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2766 if (len > int(clen[j + n - 1]))
2767 clen[j + n - 1] = len;
2772 for (row_type i = 0; i < rows_; ++i) {
2773 if (!onlydata && plaintextTopHLine(os, i, clen))
2774 os << docstring(depth * 2, ' ');
2775 for (col_type j = 0; j < columns_; ++j) {
2776 if (isPartOfMultiColumn(i, j))
2778 if (onlydata && j > 0)
2780 plaintextPrintCell(buf, os, runparams,
2781 cell, i, j, clen, onlydata);
2786 os << docstring(depth * 2, ' ');
2787 if (plaintextBottomHLine(os, i, clen))
2788 os << docstring(depth * 2, ' ');
2794 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2796 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2800 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2801 col_type column) const
2803 return cell_info[row][column].inset;
2807 void Tabular::setCellInset(row_type row, col_type column,
2808 shared_ptr<InsetText> ins) const
2810 cell_info[row][column].inset = ins;
2815 Tabular::getCellFromInset(Inset const * inset) const
2817 // is this inset part of the tabular?
2819 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2820 BOOST_ASSERT(false);
2823 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2824 if (getCellInset(cell).get() == inset) {
2825 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2826 << "cell=" << cell << endl;
2830 // We should have found a cell at this point
2831 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2832 << inset << " not found!" << endl;
2833 BOOST_ASSERT(false);
2839 void Tabular::validate(LaTeXFeatures & features) const
2841 features.require("NeedTabularnewline");
2843 features.require("booktabs");
2844 if (isLongTabular())
2845 features.require("longtable");
2847 features.require("rotating");
2848 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2849 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2850 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2851 features.require("array");
2852 getCellInset(cell)->validate(features);
2857 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2859 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2860 ParagraphList::const_iterator cit = parlist.begin();
2861 ParagraphList::const_iterator end = parlist.end();
2863 for (; cit != end; ++cit)
2864 for (int i = 0; i < cit->size(); ++i)
2865 if (cit->isNewline(i))
2872 /////////////////////////////////////////////////////////////////////
2876 /////////////////////////////////////////////////////////////////////
2878 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2880 : tabular(buf.params(), max(rows, row_type(1)),
2881 max(columns, col_type(1))), buffer_(&buf), scx_(0), is_deleted_(false)
2885 InsetTabular::InsetTabular(InsetTabular const & tab)
2886 : Inset(tab), tabular(tab.tabular),
2887 buffer_(tab.buffer_), scx_(0), is_deleted_(false)
2891 InsetTabular::~InsetTabular()
2893 InsetTabularMailer(*this).hideDialog();
2898 auto_ptr<Inset> InsetTabular::doClone() const
2900 return auto_ptr<Inset>(new InsetTabular(*this));
2904 Buffer const & InsetTabular::buffer() const
2910 void InsetTabular::buffer(Buffer const * b)
2916 void InsetTabular::write(Buffer const & buf, ostream & os) const
2918 os << "Tabular" << endl;
2919 tabular.write(buf, os);
2923 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2925 bool const old_format = (lex.getString() == "\\LyXTable");
2927 tabular.read(buf, lex);
2933 string token = lex.getString();
2934 while (lex && token != "\\end_inset") {
2936 token = lex.getString();
2939 lex.printError("Missing \\end_inset at this point. "
2945 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2947 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2948 // mi.base.textwidth << "\n";
2950 lyxerr << "InsetTabular::metrics: need bv" << endl;
2951 BOOST_ASSERT(false);
2955 for (idx_type cell = 0; i < tabular.rows(); ++i) {
2958 for (col_type j = 0; j < tabular.columns(); ++j) {
2959 if (tabular.isPartOfMultiColumn(i, j))
2960 // Multicolumn cell, but not first one
2965 if (tabular.cell_info[i][j].multicolumn ==
2966 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2967 p_width = tabular.cellinfo_of_cell(cell).p_width;
2969 p_width = tabular.column_info[j].p_width;
2970 if (!p_width.zero())
2971 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2972 tabular.getCellInset(cell)->metrics(m, dim);
2973 if (!p_width.zero())
2974 dim.wid = m.base.textwidth;
2975 tabular.setWidthOfCell(cell, dim.wid);
2976 if (p_width.zero()) {
2977 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2978 // FIXME there must be a way to get rid of
2979 // the second metrics call
2980 tabular.getCellInset(cell)->metrics(m, dim);
2982 maxAsc = max(maxAsc, dim.asc);
2983 maxDesc = max(maxDesc, dim.des);
2986 int const top_space = tabular.row_info[i].top_space_default ?
2987 default_line_space :
2988 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2989 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
2990 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2991 default_line_space :
2992 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2993 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2996 dim.asc = tabular.getAscentOfRow(0);
2997 dim.des = tabular.getHeightOfTabular() - dim.asc;
2998 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
2999 bool const changed = dim_ != dim;
3005 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3007 setPosCache(pi, x, y);
3009 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3010 BufferView * bv = pi.base.bv;
3012 resetPos(bv->cursor());
3015 x += ADD_TO_TABULAR_WIDTH;
3018 first_visible_cell = Tabular::npos;
3019 for (row_type i = 0; i < tabular.rows(); ++i) {
3021 int const a = tabular.getAscentOfRow(i);
3022 int const d = tabular.getDescentOfRow(i);
3023 idx = tabular.getCellNumber(i, 0);
3024 for (col_type j = 0; j < tabular.columns(); ++j) {
3025 if (tabular.isPartOfMultiColumn(i, j))
3027 if (first_visible_cell == Tabular::npos)
3028 first_visible_cell = idx;
3030 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3031 if (nx + tabular.getWidthOfColumn(idx) < 0
3032 || nx > bv->workWidth()
3034 || y - a > bv->workHeight()) {
3035 pi.pain.setDrawingEnabled(false);
3036 cell(idx)->draw(pi, cx, y);
3037 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3038 pi.pain.setDrawingEnabled(true);
3040 cell(idx)->draw(pi, cx, y);
3041 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3043 nx += tabular.getWidthOfColumn(idx);
3047 if (i + 1 < tabular.rows())
3048 y += d + tabular.getAscentOfRow(i + 1) +
3049 tabular.getAdditionalHeight(i + 1);
3054 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3056 setPosCache(pi, x, y);
3058 Cursor & cur = pi.base.bv->cursor();
3060 x += scx_ + ADD_TO_TABULAR_WIDTH;
3062 // Paint background of current tabular
3063 int const w = tabular.getWidthOfTabular();
3064 int const h = tabular.getHeightOfTabular();
3065 int yy = y - tabular.getAscentOfRow(0);
3066 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3068 if (!cur.selection())
3070 if (!ptr_cmp(&cur.inset(), this))
3076 if (tablemode(cur)) {
3079 getSelection(cur, rs, re, cs, ce);
3080 y -= tabular.getAscentOfRow(0);
3081 for (row_type j = 0; j < tabular.rows(); ++j) {
3082 int const a = tabular.getAscentOfRow(j);
3083 int const h = a + tabular.getDescentOfRow(j);
3085 y += tabular.getAdditionalHeight(j);
3086 for (col_type i = 0; i < tabular.columns(); ++i) {
3087 if (tabular.isPartOfMultiColumn(j, i))
3089 idx_type const cell =
3090 tabular.getCellNumber(j, i);
3091 int const w = tabular.getWidthOfColumn(cell);
3092 if (i >= cs && i <= ce && j >= rs && j <= re)
3093 pi.pain.fillRectangle(xx, y, w, h,
3101 x += getCellXPos(cur.idx());
3102 x += tabular.getBeginningOfTextInCell(cur.idx());
3103 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3108 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3109 row_type row, idx_type cell, bool erased) const
3111 int x2 = x + tabular.getWidthOfColumn(cell);
3112 bool on_off = false;
3113 Color::color col = Color::tabularline;
3114 Color::color onoffcol = Color::tabularonoffline;
3117 col = Color::deletedtext;
3118 onoffcol = Color::deletedtext;
3121 if (!tabular.topAlreadyDrawn(cell)) {
3122 on_off = !tabular.topLine(cell);
3123 pain.line(x, y - tabular.getAscentOfRow(row),
3124 x2, y - tabular.getAscentOfRow(row),
3125 on_off ? onoffcol : col,
3126 on_off ? Painter::line_onoffdash : Painter::line_solid);
3128 on_off = !tabular.bottomLine(cell);
3129 pain.line(x, y + tabular.getDescentOfRow(row),
3130 x2, y + tabular.getDescentOfRow(row),
3131 on_off ? onoffcol : col,
3132 on_off ? Painter::line_onoffdash : Painter::line_solid);
3133 if (!tabular.leftAlreadyDrawn(cell)) {
3134 on_off = !tabular.leftLine(cell);
3135 pain.line(x, y - tabular.getAscentOfRow(row),
3136 x, y + tabular.getDescentOfRow(row),
3137 on_off ? onoffcol : col,
3138 on_off ? Painter::line_onoffdash : Painter::line_solid);
3140 on_off = !tabular.rightLine(cell);
3141 pain.line(x2 - tabular.getAdditionalWidth(cell),
3142 y - tabular.getAscentOfRow(row),
3143 x2 - tabular.getAdditionalWidth(cell),
3144 y + tabular.getDescentOfRow(row),
3145 on_off ? onoffcol : col,
3146 on_off ? Painter::line_onoffdash : Painter::line_solid);
3150 docstring const InsetTabular::editMessage() const
3152 return _("Opened table");
3156 void InsetTabular::edit(Cursor & cur, bool left)
3158 //lyxerr << "InsetTabular::edit: " << this << endl;
3160 cur.selection() = false;
3163 if (isRightToLeft(cur))
3164 cur.idx() = tabular.getLastCellInRow(0);
3170 if (isRightToLeft(cur))
3171 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3173 cur.idx() = tabular.getNumberOfCells() - 1;
3175 cur.pos() = cur.lastpos(); // FIXME crude guess
3177 // FIXME: this accesses the position cache before it is initialized
3179 //cur.bv().fitCursor();
3183 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3185 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3186 << "\n cur:" << cur << endl;
3187 CursorSlice sl = cur.top();
3188 Cursor & bvcur = cur.bv().cursor();
3190 switch (cmd.action) {
3192 case LFUN_MOUSE_PRESS:
3193 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3195 if (cmd.button() == mouse_button::button1
3196 || (cmd.button() == mouse_button::button3
3197 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
3198 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
3200 cur.selection() = false;
3201 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3202 cur.bv().mouseSetCursor(cur);
3206 if (cmd.button() == mouse_button::button2) {
3207 if (cap::selection()) {
3208 // See comment in Text::dispatch why we
3210 // FIXME This does not use paste_tabular,
3211 // another reason why paste_tabular should go.
3212 cap::copySelectionToStack();
3213 cmd = FuncRequest(LFUN_PASTE, "0");
3215 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
3218 doDispatch(cur, cmd);
3219 cur.bv().buffer()->markDirty();
3220 cur.bv().mouseSetCursor(cur);
3224 case LFUN_MOUSE_MOTION:
3225 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3226 if (cmd.button() == mouse_button::button1) {
3227 // only accept motions to places not deeper nested than the real anchor
3228 if (bvcur.anchor_.hasPart(cur)) {
3229 // only update if selection changes
3230 if (bvcur.idx() == cur.idx() &&
3231 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3233 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3234 bvcur.setCursor(cur);
3235 bvcur.selection() = true;
3241 case LFUN_MOUSE_RELEASE:
3242 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3243 if (cmd.button() == mouse_button::button3)
3244 InsetTabularMailer(*this).showDialog(&cur.bv());
3247 case LFUN_CELL_BACKWARD:
3249 cur.selection() = false;
3252 case LFUN_CELL_FORWARD:
3254 cur.selection() = false;
3257 case LFUN_CHAR_FORWARD_SELECT:
3258 case LFUN_CHAR_FORWARD:
3259 cell(cur.idx())->dispatch(cur, cmd);
3260 if (!cur.result().dispatched()) {
3261 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3262 if (sl == cur.top())
3263 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3269 case LFUN_CHAR_BACKWARD_SELECT:
3270 case LFUN_CHAR_BACKWARD:
3271 cell(cur.idx())->dispatch(cur, cmd);
3272 if (!cur.result().dispatched()) {
3273 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3274 if (sl == cur.top())
3275 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3281 case LFUN_DOWN_SELECT:
3283 cell(cur.idx())->dispatch(cur, cmd);
3284 cur.dispatched(); // override the cell's decision
3285 if (sl == cur.top())
3286 // if our Text didn't do anything to the cursor
3287 // then we try to put the cursor into the cell below
3288 // setting also the right targetX.
3289 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3290 cur.idx() = tabular.getCellBelow(cur.idx());
3292 TextMetrics const & tm =
3293 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3294 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3296 if (sl == cur.top()) {
3297 // we trick it to go to the RIGHT after leaving the
3299 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3304 case LFUN_UP_SELECT:
3306 cell(cur.idx())->dispatch(cur, cmd);
3307 cur.dispatched(); // override the cell's decision
3308 if (sl == cur.top())
3309 // if our Text didn't do anything to the cursor
3310 // then we try to put the cursor into the cell above
3311 // setting also the right targetX.
3312 if (tabular.row_of_cell(cur.idx()) != 0) {
3313 cur.idx() = tabular.getCellAbove(cur.idx());
3314 cur.pit() = cur.lastpit();
3315 Text const * text = cell(cur.idx())->getText(0);
3316 TextMetrics const & tm = cur.bv().textMetrics(text);
3317 ParagraphMetrics const & pm =
3318 tm.parMetrics(cur.lastpit());
3319 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3321 if (sl == cur.top()) {
3322 cmd = FuncRequest(LFUN_UP);
3327 // case LFUN_SCREEN_DOWN: {
3328 // //if (hasSelection())
3329 // // cur.selection() = false;
3330 // col_type const col = tabular.column_of_cell(cur.idx());
3331 // int const t = cur.bv().top_y() + cur.bv().height();
3332 // if (t < yo() + tabular.getHeightOfTabular()) {
3333 // cur.bv().scrollDocView(t);
3334 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3336 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3343 // case LFUN_SCREEN_UP: {
3344 // //if (hasSelection())
3345 // // cur.selection() = false;
3346 // col_type const col = tabular.column_of_cell(cur.idx());
3347 // int const t = cur.bv().top_y() + cur.bv().height();
3349 // cur.bv().scrollDocView(t);
3353 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3357 // cur.par() = cur.lastpar();
3358 // cur.pos() = cur.lastpos();
3362 case LFUN_LAYOUT_TABULAR:
3363 InsetTabularMailer(*this).showDialog(&cur.bv());
3366 case LFUN_INSET_DIALOG_UPDATE:
3367 InsetTabularMailer(*this).updateDialog(&cur.bv());
3370 case LFUN_TABULAR_FEATURE:
3371 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3375 // insert file functions
3376 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3377 case LFUN_FILE_INSERT_PLAINTEXT: {
3379 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3380 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3386 if (tablemode(cur)) {
3387 if (copySelection(cur)) {
3388 recordUndoInset(cur, Undo::DELETE);
3393 cell(cur.idx())->dispatch(cur, cmd);
3396 case LFUN_CHAR_DELETE_BACKWARD:
3397 case LFUN_CHAR_DELETE_FORWARD:
3398 if (tablemode(cur)) {
3399 recordUndoInset(cur, Undo::DELETE);
3403 cell(cur.idx())->dispatch(cur, cmd);
3407 if (!cur.selection())
3409 if (tablemode(cur)) {
3413 cell(cur.idx())->dispatch(cur, cmd);
3416 case LFUN_CLIPBOARD_PASTE:
3417 case LFUN_PRIMARY_SELECTION_PASTE: {
3418 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3419 theClipboard().getAsText() :
3420 theSelection().get();
3423 // pass to InsertPlaintextString, but
3424 // only if we have multi-cell content
3425 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3426 if (insertPlaintextString(cur.bv(), clip, false)) {
3427 // content has been replaced,
3428 // so cursor might be invalid
3429 cur.pos() = cur.lastpos();
3430 bvcur.setCursor(cur);
3434 // Let the cell handle normal text
3435 cell(cur.idx())->dispatch(cur, cmd);
3440 if (tabularStackDirty() && theClipboard().isInternal()) {
3441 recordUndoInset(cur, Undo::INSERT);
3442 pasteClipboard(cur);
3445 cell(cur.idx())->dispatch(cur, cmd);
3448 case LFUN_FONT_EMPH:
3449 case LFUN_FONT_BOLD:
3450 case LFUN_FONT_ROMAN:
3451 case LFUN_FONT_NOUN:
3452 case LFUN_FONT_ITAL:
3453 case LFUN_FONT_FRAK:
3454 case LFUN_FONT_CODE:
3455 case LFUN_FONT_SANS:
3456 case LFUN_FONT_FREE_APPLY:
3457 case LFUN_FONT_FREE_UPDATE:
3458 case LFUN_FONT_SIZE:
3459 case LFUN_FONT_UNDERLINE:
3461 case LFUN_WORD_CAPITALIZE:
3462 case LFUN_WORD_UPCASE:
3463 case LFUN_WORD_LOWCASE:
3464 case LFUN_CHARS_TRANSPOSE:
3465 if (tablemode(cur)) {
3468 getSelection(cur, rs, re, cs, ce);
3469 Cursor tmpcur = cur;
3470 for (row_type i = rs; i <= re; ++i) {
3471 for (col_type j = cs; j <= ce; ++j) {
3472 // cursor follows cell:
3473 tmpcur.idx() = tabular.getCellNumber(i, j);
3474 // select this cell only:
3477 tmpcur.resetAnchor();
3478 tmpcur.pit() = tmpcur.lastpit();
3479 tmpcur.pos() = tmpcur.top().lastpos();
3480 tmpcur.setCursor(tmpcur);
3481 tmpcur.setSelection();
3482 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3487 cell(cur.idx())->dispatch(cur, cmd);
3491 // we try to handle this event in the insets dispatch function.
3492 cell(cur.idx())->dispatch(cur, cmd);
3496 // FIXME: this accesses the position cache before it is initialized
3499 InsetTabularMailer(*this).updateDialog(&cur.bv());
3503 // function sets an object as defined in func_status.h:
3504 // states OK, Unknown, Disabled, On, Off.
3505 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3506 FuncStatus & status) const
3508 switch (cmd.action) {
3509 case LFUN_TABULAR_FEATURE: {
3510 int action = Tabular::LAST_ACTION;
3512 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3513 string const tmp = tabularFeature[i].feature;
3514 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3515 action = tabularFeature[i].action;
3519 if (action == Tabular::LAST_ACTION) {
3521 status.unknown(true);
3525 string const argument
3526 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3528 row_type sel_row_start = 0;
3529 row_type sel_row_end = 0;
3530 col_type sel_col_start = 0;
3531 col_type sel_col_end = 0;
3532 Tabular::ltType dummyltt;
3535 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3538 case Tabular::SET_PWIDTH:
3539 case Tabular::SET_MPWIDTH:
3540 case Tabular::SET_SPECIAL_COLUMN:
3541 case Tabular::SET_SPECIAL_MULTI:
3542 case Tabular::APPEND_ROW:
3543 case Tabular::APPEND_COLUMN:
3544 case Tabular::DELETE_ROW:
3545 case Tabular::DELETE_COLUMN:
3546 case Tabular::COPY_ROW:
3547 case Tabular::COPY_COLUMN:
3548 case Tabular::SET_ALL_LINES:
3549 case Tabular::UNSET_ALL_LINES:
3550 case Tabular::SET_TOP_SPACE:
3551 case Tabular::SET_BOTTOM_SPACE:
3552 case Tabular::SET_INTERLINE_SPACE:
3556 case Tabular::MULTICOLUMN:
3557 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3560 case Tabular::M_TOGGLE_LINE_TOP:
3562 case Tabular::TOGGLE_LINE_TOP:
3563 status.setOnOff(tabular.topLine(cur.idx(), flag));
3566 case Tabular::M_TOGGLE_LINE_BOTTOM:
3568 case Tabular::TOGGLE_LINE_BOTTOM:
3569 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3572 case Tabular::M_TOGGLE_LINE_LEFT:
3574 case Tabular::TOGGLE_LINE_LEFT:
3575 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3578 case Tabular::M_TOGGLE_LINE_RIGHT:
3580 case Tabular::TOGGLE_LINE_RIGHT:
3581 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3584 case Tabular::M_ALIGN_LEFT:
3586 case Tabular::ALIGN_LEFT:
3587 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3590 case Tabular::M_ALIGN_RIGHT:
3592 case Tabular::ALIGN_RIGHT:
3593 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3596 case Tabular::M_ALIGN_CENTER:
3598 case Tabular::ALIGN_CENTER:
3599 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3602 case Tabular::ALIGN_BLOCK:
3603 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3604 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3607 case Tabular::M_VALIGN_TOP:
3609 case Tabular::VALIGN_TOP:
3611 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3614 case Tabular::M_VALIGN_BOTTOM:
3616 case Tabular::VALIGN_BOTTOM:
3618 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3621 case Tabular::M_VALIGN_MIDDLE:
3623 case Tabular::VALIGN_MIDDLE:
3625 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3628 case Tabular::SET_LONGTABULAR:
3629 status.setOnOff(tabular.isLongTabular());
3632 case Tabular::UNSET_LONGTABULAR:
3633 status.setOnOff(!tabular.isLongTabular());
3636 case Tabular::TOGGLE_ROTATE_TABULAR:
3637 case Tabular::SET_ROTATE_TABULAR:
3638 status.setOnOff(tabular.getRotateTabular());
3641 case Tabular::UNSET_ROTATE_TABULAR:
3642 status.setOnOff(!tabular.getRotateTabular());
3645 case Tabular::TOGGLE_ROTATE_CELL:
3646 case Tabular::SET_ROTATE_CELL:
3647 status.setOnOff(!oneCellHasRotationState(false,
3648 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3651 case Tabular::UNSET_ROTATE_CELL:
3652 status.setOnOff(!oneCellHasRotationState(true,
3653 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3656 case Tabular::SET_USEBOX:
3657 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3660 case Tabular::SET_LTFIRSTHEAD:
3661 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3664 case Tabular::UNSET_LTFIRSTHEAD:
3665 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3668 case Tabular::SET_LTHEAD:
3669 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3672 case Tabular::UNSET_LTHEAD:
3673 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3676 case Tabular::SET_LTFOOT:
3677 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3680 case Tabular::UNSET_LTFOOT:
3681 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3684 case Tabular::SET_LTLASTFOOT:
3685 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3688 case Tabular::UNSET_LTLASTFOOT:
3689 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3692 case Tabular::SET_LTNEWPAGE:
3693 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3696 case Tabular::SET_BOOKTABS:
3697 status.setOnOff(tabular.useBookTabs());
3700 case Tabular::UNSET_BOOKTABS:
3701 status.setOnOff(!tabular.useBookTabs());
3706 status.enabled(false);
3712 // These are only enabled inside tabular
3713 case LFUN_CELL_BACKWARD:
3714 case LFUN_CELL_FORWARD:
3715 status.enabled(true);
3718 // disable these with multiple cells selected
3719 case LFUN_INSET_INSERT:
3720 case LFUN_TABULAR_INSERT:
3721 case LFUN_CHARSTYLE_INSERT:
3722 case LFUN_FLOAT_INSERT:
3723 case LFUN_FLOAT_WIDE_INSERT:
3724 case LFUN_FOOTNOTE_INSERT:
3725 case LFUN_MARGINALNOTE_INSERT:
3726 case LFUN_MATH_INSERT:
3727 case LFUN_MATH_MODE:
3728 case LFUN_MATH_MUTATE:
3729 case LFUN_MATH_DISPLAY:
3730 case LFUN_NOTE_INSERT:
3731 case LFUN_OPTIONAL_INSERT:
3732 case LFUN_BOX_INSERT:
3733 case LFUN_BRANCH_INSERT:
3734 case LFUN_WRAP_INSERT:
3735 case LFUN_ERT_INSERT: {
3736 if (tablemode(cur)) {
3737 status.enabled(false);
3740 return cell(cur.idx())->getStatus(cur, cmd, status);
3743 // disable in non-fixed-width cells
3744 case LFUN_BREAK_LINE:
3745 case LFUN_BREAK_PARAGRAPH:
3746 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3747 case LFUN_BREAK_PARAGRAPH_SKIP: {
3748 if (tabular.getPWidth(cur.idx()).zero()) {
3749 status.enabled(false);
3752 return cell(cur.idx())->getStatus(cur, cmd, status);
3756 if (tabularStackDirty() && theClipboard().isInternal()) {
3757 status.enabled(true);
3760 return cell(cur.idx())->getStatus(cur, cmd, status);
3762 case LFUN_INSET_MODIFY:
3763 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3764 status.enabled(true);
3770 // we try to handle this event in the insets dispatch function.
3771 return cell(cur.idx())->getStatus(cur, cmd, status);
3776 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3777 OutputParams const & runparams) const
3779 return tabular.latex(buf, os, runparams);
3783 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3784 OutputParams const & runparams) const
3786 os << '\n'; // output table on a new line
3787 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3788 tabular.plaintext(buf, os, runparams, dp, false, 0);
3789 return PLAINTEXT_NEWLINE;
3793 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3794 OutputParams const & runparams) const
3799 #ifdef WITH_WARNINGS
3800 #warning Why not pass a proper DocIterator here?
3803 // if the table is inside a float it doesn't need the informaltable
3804 // wrapper. Search for it.
3805 for (master = owner(); master; master = master->owner())
3806 if (master->lyxCode() == Inset::FLOAT_CODE)
3811 os << "<informaltable>";
3814 ret += tabular.docbook(buf, os, runparams);
3816 os << "</informaltable>";
3823 void InsetTabular::validate(LaTeXFeatures & features) const
3825 tabular.validate(features);
3829 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3831 return tabular.getCellInset(idx);
3835 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3837 return tabular.getCellInset(idx);
3841 void InsetTabular::cursorPos(BufferView const & bv,
3842 CursorSlice const & sl, bool boundary, int & x, int & y) const
3844 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3846 // y offset correction
3847 int const row = tabular.row_of_cell(sl.idx());
3848 for (int i = 0; i <= row; ++i) {
3850 y += tabular.getAscentOfRow(i);
3851 y += tabular.getAdditionalHeight(i);
3854 y += tabular.getDescentOfRow(i);
3857 // x offset correction
3858 int const col = tabular.column_of_cell(sl.idx());
3859 int idx = tabular.getCellNumber(row, 0);
3860 for (int j = 0; j < col; ++j) {
3861 if (tabular.isPartOfMultiColumn(row, j))
3863 x += tabular.getWidthOfColumn(idx);
3866 x += tabular.getBeginningOfTextInCell(idx);
3867 x += ADD_TO_TABULAR_WIDTH;
3872 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3876 Inset const & inset = *tabular.getCellInset(cell);
3877 Point o = bv.coordCache().getInsets().xy(&inset);
3878 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3879 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3880 row_type const row = tabular.row_of_cell(cell);
3881 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3882 tabular.getAdditionalHeight(row);
3883 int const yend = o.y_ + tabular.getDescentOfRow(row);
3895 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3896 // << " ybeg=" << ybeg << " yend=" << yend
3897 // << " xx=" << xx << " yy=" << yy
3898 // << " dist=" << xx + yy << endl;
3903 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3905 //lyxerr << "InsetTabular::editXY: " << this << endl;
3906 cur.selection() = false;
3908 cur.idx() = getNearestCell(cur.bv(), x, y);
3910 return cell(cur.idx())->text_.editXY(cur, x, y);
3914 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3916 cur.idx() = getNearestCell(cur.bv(), x, y);
3917 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3921 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3923 idx_type idx_min = 0;
3924 int dist_min = std::numeric_limits<int>::max();
3925 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3926 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3927 int const d = dist(bv, i, x, y);
3938 int InsetTabular::getCellXPos(idx_type const cell) const
3942 for (; !tabular.isFirstCellInRow(c); --c)
3945 for (; c < cell; ++c)
3946 lx += tabular.getWidthOfColumn(c);
3952 void InsetTabular::resetPos(Cursor & cur) const
3954 BufferView & bv = cur.bv();
3955 int const maxwidth = bv.workWidth();
3957 if (&cur.inset() != this) {
3961 int const X2 = maxwidth;
3962 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3963 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3964 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3967 scx_ = X1 + 20 - x1;
3969 scx_ = X2 - 20 - x2;
3974 cur.updateFlags(Update::Force | Update::FitCursor);
3978 void InsetTabular::moveNextCell(Cursor & cur)
3980 if (isRightToLeft(cur)) {
3981 if (tabular.isFirstCellInRow(cur.idx())) {
3982 row_type const row = tabular.row_of_cell(cur.idx());
3983 if (row == tabular.rows() - 1)
3985 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3992 if (tabular.isLastCell(cur.idx()))
4002 void InsetTabular::movePrevCell(Cursor & cur)
4004 if (isRightToLeft(cur)) {
4005 if (tabular.isLastCellInRow(cur.idx())) {
4006 row_type const row = tabular.row_of_cell(cur.idx());
4009 cur.idx() = tabular.getFirstCellInRow(row);
4010 cur.idx() = tabular.getCellAbove(cur.idx());
4012 if (tabular.isLastCell(cur.idx()))
4017 if (cur.idx() == 0) // first cell
4021 cur.pit() = cur.lastpit();
4022 cur.pos() = cur.lastpos();
4024 // FIXME: this accesses the position cache before it is initialized
4029 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4031 Tabular::Feature action = Tabular::LAST_ACTION;
4034 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4035 string const tmp = tabularFeature[i].feature;
4037 if (tmp == what.substr(0, tmp.length())) {
4038 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4039 //tabularFeatures[i].feature.length()))
4040 action = tabularFeature[i].action;
4044 if (action == Tabular::LAST_ACTION)
4048 ltrim(what.substr(tabularFeature[i].feature.length()));
4049 tabularFeatures(cur, action, val);
4054 static void checkLongtableSpecial(Tabular::ltType & ltt,
4055 string const & special, bool & flag)
4057 if (special == "dl_above") {
4060 } else if (special == "dl_below") {
4061 ltt.bottomDL = flag;
4063 } else if (special == "empty") {
4072 bool InsetTabular::oneCellHasRotationState(bool rotated,
4073 row_type row_start, row_type row_end,
4074 col_type col_start, col_type col_end) const {
4076 for (row_type i = row_start; i <= row_end; ++i) {
4077 for (col_type j = col_start; j <= col_end; ++j) {
4078 if (tabular.getRotateCell(tabular.getCellNumber(i, j))
4087 void InsetTabular::tabularFeatures(Cursor & cur,
4088 Tabular::Feature feature, string const & value)
4090 BufferView & bv = cur.bv();
4091 col_type sel_col_start;
4092 col_type sel_col_end;
4093 row_type sel_row_start;
4094 row_type sel_row_end;
4095 bool setLines = false;
4096 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4097 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4101 case Tabular::M_ALIGN_LEFT:
4102 case Tabular::ALIGN_LEFT:
4103 setAlign = LYX_ALIGN_LEFT;
4106 case Tabular::M_ALIGN_RIGHT:
4107 case Tabular::ALIGN_RIGHT:
4108 setAlign = LYX_ALIGN_RIGHT;
4111 case Tabular::M_ALIGN_CENTER:
4112 case Tabular::ALIGN_CENTER:
4113 setAlign = LYX_ALIGN_CENTER;
4116 case Tabular::ALIGN_BLOCK:
4117 setAlign = LYX_ALIGN_BLOCK;
4120 case Tabular::M_VALIGN_TOP:
4121 case Tabular::VALIGN_TOP:
4122 setVAlign = Tabular::LYX_VALIGN_TOP;
4125 case Tabular::M_VALIGN_BOTTOM:
4126 case Tabular::VALIGN_BOTTOM:
4127 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4130 case Tabular::M_VALIGN_MIDDLE:
4131 case Tabular::VALIGN_MIDDLE:
4132 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4139 recordUndoInset(cur, Undo::ATOMIC);
4141 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4142 row_type const row = tabular.row_of_cell(cur.idx());
4143 col_type const column = tabular.column_of_cell(cur.idx());
4145 Tabular::ltType ltt;
4149 case Tabular::SET_PWIDTH: {
4150 Length const len(value);
4151 tabular.setColumnPWidth(cur, cur.idx(), len);
4153 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4154 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4158 case Tabular::SET_MPWIDTH:
4159 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4162 case Tabular::SET_SPECIAL_COLUMN:
4163 case Tabular::SET_SPECIAL_MULTI:
4164 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4167 case Tabular::APPEND_ROW:
4168 // append the row into the tabular
4169 tabular.appendRow(bv.buffer()->params(), cur.idx());
4172 case Tabular::APPEND_COLUMN:
4173 // append the column into the tabular
4174 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4175 cur.idx() = tabular.getCellNumber(row, column);
4178 case Tabular::DELETE_ROW:
4179 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4180 tabular.deleteRow(sel_row_start);
4181 if (sel_row_start >= tabular.rows())
4183 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4186 cur.selection() = false;
4189 case Tabular::DELETE_COLUMN:
4190 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4191 tabular.deleteColumn(sel_col_start);
4192 if (sel_col_start >= tabular.columns())
4194 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4197 cur.selection() = false;
4200 case Tabular::COPY_ROW:
4201 tabular.copyRow(bv.buffer()->params(), row);
4204 case Tabular::COPY_COLUMN:
4205 tabular.copyColumn(bv.buffer()->params(), column);
4206 cur.idx() = tabular.getCellNumber(row, column);
4209 case Tabular::M_TOGGLE_LINE_TOP:
4211 case Tabular::TOGGLE_LINE_TOP: {
4212 bool lineSet = !tabular.topLine(cur.idx(), flag);
4213 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4214 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4216 tabular.getCellNumber(i, j),
4221 case Tabular::M_TOGGLE_LINE_BOTTOM:
4223 case Tabular::TOGGLE_LINE_BOTTOM: {
4224 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4225 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4226 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4227 tabular.setBottomLine(
4228 tabular.getCellNumber(i, j),
4234 case Tabular::M_TOGGLE_LINE_LEFT:
4236 case Tabular::TOGGLE_LINE_LEFT: {
4237 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4238 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4239 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4240 tabular.setLeftLine(
4241 tabular.getCellNumber(i,j),
4247 case Tabular::M_TOGGLE_LINE_RIGHT:
4249 case Tabular::TOGGLE_LINE_RIGHT: {
4250 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4251 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4252 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4253 tabular.setRightLine(
4254 tabular.getCellNumber(i,j),
4260 case Tabular::M_ALIGN_LEFT:
4261 case Tabular::M_ALIGN_RIGHT:
4262 case Tabular::M_ALIGN_CENTER:
4264 case Tabular::ALIGN_LEFT:
4265 case Tabular::ALIGN_RIGHT:
4266 case Tabular::ALIGN_CENTER:
4267 case Tabular::ALIGN_BLOCK:
4268 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4270 tabular.setAlignment(
4271 tabular.getCellNumber(i, j),
4276 case Tabular::M_VALIGN_TOP:
4277 case Tabular::M_VALIGN_BOTTOM:
4278 case Tabular::M_VALIGN_MIDDLE:
4280 case Tabular::VALIGN_TOP:
4281 case Tabular::VALIGN_BOTTOM:
4282 case Tabular::VALIGN_MIDDLE:
4283 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4284 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4285 tabular.setVAlignment(
4286 tabular.getCellNumber(i, j),
4290 case Tabular::MULTICOLUMN: {
4291 if (sel_row_start != sel_row_end) {
4292 #ifdef WITH_WARNINGS
4293 #warning Need I say it ? This is horrible.
4296 Alert::error(_("Error setting multicolumn"),
4297 _("You cannot set multicolumn vertically."));
4300 if (!cur.selection()) {
4301 // just multicol for one single cell
4302 // check whether we are completely in a multicol
4303 if (tabular.isMultiColumn(cur.idx()))
4304 tabular.unsetMultiColumn(cur.idx());
4306 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4309 // we have a selection so this means we just add all this
4310 // cells to form a multicolumn cell
4311 idx_type const s_start = cur.selBegin().idx();
4312 idx_type const s_end = cur.selEnd().idx();
4313 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4314 cur.idx() = s_start;
4317 cur.selection() = false;
4321 case Tabular::SET_ALL_LINES:
4323 case Tabular::UNSET_ALL_LINES:
4324 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4325 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4326 tabular.setAllLines(
4327 tabular.getCellNumber(i,j), setLines);
4330 case Tabular::SET_LONGTABULAR:
4331 tabular.setLongTabular(true);
4334 case Tabular::UNSET_LONGTABULAR:
4335 tabular.setLongTabular(false);
4338 case Tabular::SET_ROTATE_TABULAR:
4339 tabular.setRotateTabular(true);
4342 case Tabular::UNSET_ROTATE_TABULAR:
4343 tabular.setRotateTabular(false);
4346 case Tabular::TOGGLE_ROTATE_TABULAR:
4347 tabular.setRotateTabular(!tabular.getRotateTabular());
4350 case Tabular::SET_ROTATE_CELL:
4351 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4352 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4353 tabular.setRotateCell(
4354 tabular.getCellNumber(i, j), true);
4357 case Tabular::UNSET_ROTATE_CELL:
4358 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4359 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4360 tabular.setRotateCell(
4361 tabular.getCellNumber(i, j), false);
4364 case Tabular::TOGGLE_ROTATE_CELL:
4366 bool oneNotRotated = oneCellHasRotationState(false,
4367 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4369 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4370 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4371 tabular.setRotateCell(tabular.getCellNumber(i, j),
4376 case Tabular::SET_USEBOX: {
4377 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4378 if (val == tabular.getUsebox(cur.idx()))
4379 val = Tabular::BOX_NONE;
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.setUsebox(tabular.getCellNumber(i, j), val);
4386 case Tabular::UNSET_LTFIRSTHEAD:
4388 case Tabular::SET_LTFIRSTHEAD:
4389 tabular.getRowOfLTFirstHead(row, ltt);
4390 checkLongtableSpecial(ltt, value, flag);
4391 tabular.setLTHead(row, flag, ltt, true);
4394 case Tabular::UNSET_LTHEAD:
4396 case Tabular::SET_LTHEAD:
4397 tabular.getRowOfLTHead(row, ltt);
4398 checkLongtableSpecial(ltt, value, flag);
4399 tabular.setLTHead(row, flag, ltt, false);
4402 case Tabular::UNSET_LTFOOT:
4404 case Tabular::SET_LTFOOT:
4405 tabular.getRowOfLTFoot(row, ltt);
4406 checkLongtableSpecial(ltt, value, flag);
4407 tabular.setLTFoot(row, flag, ltt, false);
4410 case Tabular::UNSET_LTLASTFOOT:
4412 case Tabular::SET_LTLASTFOOT:
4413 tabular.getRowOfLTLastFoot(row, ltt);
4414 checkLongtableSpecial(ltt, value, flag);
4415 tabular.setLTFoot(row, flag, ltt, true);
4418 case Tabular::SET_LTNEWPAGE:
4419 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4422 case Tabular::SET_BOOKTABS:
4423 tabular.setBookTabs(true);
4426 case Tabular::UNSET_BOOKTABS:
4427 tabular.setBookTabs(false);
4430 case Tabular::SET_TOP_SPACE: {
4432 if (value == "default")
4433 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4434 tabular.row_info[i].top_space_default = true;
4435 else if (isValidLength(value, &len))
4436 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4437 tabular.row_info[i].top_space_default = false;
4438 tabular.row_info[i].top_space = len;
4441 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4442 tabular.row_info[i].top_space_default = false;
4443 tabular.row_info[i].top_space = len;
4448 case Tabular::SET_BOTTOM_SPACE: {
4450 if (value == "default")
4451 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4452 tabular.row_info[i].bottom_space_default = true;
4453 else if (isValidLength(value, &len))
4454 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4455 tabular.row_info[i].bottom_space_default = false;
4456 tabular.row_info[i].bottom_space = len;
4459 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4460 tabular.row_info[i].bottom_space_default = false;
4461 tabular.row_info[i].bottom_space = len;
4466 case Tabular::SET_INTERLINE_SPACE: {
4468 if (value == "default")
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4470 tabular.row_info[i].interline_space_default = true;
4471 else if (isValidLength(value, &len))
4472 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4473 tabular.row_info[i].interline_space_default = false;
4474 tabular.row_info[i].interline_space = len;
4477 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4478 tabular.row_info[i].interline_space_default = false;
4479 tabular.row_info[i].interline_space = len;
4484 // dummy stuff just to avoid warnings
4485 case Tabular::LAST_ACTION:
4491 bool InsetTabular::showInsetDialog(BufferView * bv) const
4493 InsetTabularMailer(*this).showDialog(bv);
4498 void InsetTabular::openLayoutDialog(BufferView * bv) const
4500 InsetTabularMailer(*this).showDialog(bv);
4504 bool InsetTabular::copySelection(Cursor & cur)
4506 if (!cur.selection())
4511 getSelection(cur, rs, re, cs, ce);
4513 paste_tabular.reset(new Tabular(tabular));
4515 for (row_type i = 0; i < rs; ++i)
4516 paste_tabular->deleteRow(0);
4518 row_type const rows = re - rs + 1;
4519 while (paste_tabular->rows() > rows)
4520 paste_tabular->deleteRow(rows);
4522 paste_tabular->setTopLine(0, true, true);
4523 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4526 for (col_type i = 0; i < cs; ++i)
4527 paste_tabular->deleteColumn(0);
4529 col_type const columns = ce - cs + 1;
4530 while (paste_tabular->columns() > columns)
4531 paste_tabular->deleteColumn(columns);
4533 paste_tabular->setLeftLine(0, true, true);
4534 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4537 odocstringstream os;
4538 OutputParams const runparams(0);
4539 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4540 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4541 cap::copySelection(cur, os.str());
4543 // mark tabular stack dirty
4544 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4545 // when we (hopefully) have a one-for-all paste mechanism.
4546 // This must be called after cap::copySelection.
4547 dirtyTabularStack(true);
4553 bool InsetTabular::pasteClipboard(Cursor & cur)
4557 col_type const actcol = tabular.column_of_cell(cur.idx());
4558 row_type const actrow = tabular.row_of_cell(cur.idx());
4559 for (row_type r1 = 0, r2 = actrow;
4560 r1 < paste_tabular->rows() && r2 < tabular.rows();
4562 for (col_type c1 = 0, c2 = actcol;
4563 c1 < paste_tabular->columns() && c2 < tabular.columns();
4565 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4566 tabular.isPartOfMultiColumn(r2, c2))
4568 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4572 if (tabular.isPartOfMultiColumn(r2, c2)) {
4576 shared_ptr<InsetText> inset(
4577 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4578 tabular.setCellInset(r2, c2, inset);
4579 // FIXME: change tracking (MG)
4580 inset->setChange(Change(cur.buffer().params().trackChanges ?
4581 Change::INSERTED : Change::UNCHANGED));
4589 void InsetTabular::cutSelection(Cursor & cur)
4591 if (!cur.selection())
4596 getSelection(cur, rs, re, cs, ce);
4597 for (row_type i = rs; i <= re; ++i) {
4598 for (col_type j = cs; j <= ce; ++j) {
4599 shared_ptr<InsetText> t
4600 = cell(tabular.getCellNumber(i, j));
4601 if (cur.buffer().params().trackChanges)
4602 // FIXME: Change tracking (MG)
4603 t->setChange(Change(Change::DELETED));
4609 // cursor position might be invalid now
4610 if (cur.pit() > cur.lastpit())
4611 cur.pit() = cur.lastpit();
4612 if (cur.pos() > cur.lastpos())
4613 cur.pos() = cur.lastpos();
4614 cur.clearSelection();
4618 bool InsetTabular::isRightToLeft(Cursor & cur) const
4620 BOOST_ASSERT(cur.depth() > 1);
4621 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4622 pos_type const parentpos = cur[cur.depth() - 2].pos();
4623 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4624 parentpos).language()->rightToLeft();
4628 void InsetTabular::getSelection(Cursor & cur,
4629 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4631 CursorSlice const & beg = cur.selBegin();
4632 CursorSlice const & end = cur.selEnd();
4633 cs = tabular.column_of_cell(beg.idx());
4634 ce = tabular.column_of_cell(end.idx());
4637 cs = tabular.column_of_cell(end.idx());
4639 ce = tabular.right_column_of_cell(end.idx());
4642 rs = tabular.row_of_cell(beg.idx());
4643 re = tabular.row_of_cell(end.idx());
4649 Text * InsetTabular::getText(int idx) const
4651 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4655 void InsetTabular::setChange(Change const & change)
4657 for (idx_type idx = 0; idx < nargs(); ++idx)
4658 cell(idx)->setChange(change);
4662 void InsetTabular::acceptChanges(BufferParams const & bparams)
4664 for (idx_type idx = 0; idx < nargs(); ++idx)
4665 cell(idx)->acceptChanges(bparams);
4669 void InsetTabular::rejectChanges(BufferParams const & bparams)
4671 for (idx_type idx = 0; idx < nargs(); ++idx)
4672 cell(idx)->rejectChanges(bparams);
4676 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4678 return tabular.getPWidth(cell).zero();
4682 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4685 if (buf.length() <= 0)
4688 Buffer const & buffer = *bv.buffer();
4692 col_type maxCols = 1;
4693 docstring::size_type const len = buf.length();
4694 docstring::size_type p = 0;
4697 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4705 maxCols = max(cols, maxCols);
4711 maxCols = max(cols, maxCols);
4717 paste_tabular.reset(
4718 new Tabular(buffer.params(), rows, maxCols));
4719 loctab = paste_tabular.get();
4721 dirtyTabularStack(true);
4724 cell = bv.cursor().idx();
4725 ocol = tabular.column_of_cell(cell);
4726 row = tabular.row_of_cell(cell);
4729 docstring::size_type op = 0;
4730 idx_type const cells = loctab->getNumberOfCells();
4733 rows = loctab->rows();
4734 col_type const columns = loctab->columns();
4736 while (cell < cells && p < len && row < rows &&
4737 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4743 // we can only set this if we are not too far right
4744 if (cols < columns) {
4745 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4746 Paragraph & par = inset->text_.getPar(0);
4747 Font const font = inset->text_.getFont(buffer, par, 0);
4748 inset->setText(buf.substr(op, p - op), font,
4749 buffer.params().trackChanges);
4755 // we can only set this if we are not too far right
4756 if (cols < columns) {
4757 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4758 Paragraph & par = inset->text_.getPar(0);
4759 Font const font = inset->text_.getFont(buffer, par, 0);
4760 inset->setText(buf.substr(op, p - op), font,
4761 buffer.params().trackChanges);
4766 cell = loctab->getCellNumber(row, cols);
4772 // check for the last cell if there is no trailing '\n'
4773 if (cell < cells && op < len) {
4774 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4775 Paragraph & par = inset->text_.getPar(0);
4776 Font const font = inset->text_.getFont(buffer, par, 0);
4777 inset->setText(buf.substr(op, len - op), font,
4778 buffer.params().trackChanges);
4784 void InsetTabular::addPreview(PreviewLoader & loader) const
4786 row_type const rows = tabular.rows();
4787 col_type const columns = tabular.columns();
4788 for (row_type i = 0; i < rows; ++i) {
4789 for (col_type j = 0; j < columns; ++j)
4790 tabular.getCellInset(i, j)->addPreview(loader);
4795 bool InsetTabular::tablemode(Cursor & cur) const
4797 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4804 string const InsetTabularMailer::name_("tabular");
4806 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4807 : inset_(const_cast<InsetTabular &>(inset))
4811 string const InsetTabularMailer::inset2string(Buffer const &) const
4813 return params2string(inset_);
4817 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4819 istringstream data(in);
4821 lex.setStream(data);
4828 if (!lex || token != name_)
4829 return print_mailer_error("InsetTabularMailer", in, 1,
4832 // This is part of the inset proper that is usually swallowed
4833 // by Buffer::readInset
4835 if (!lex || token != "Tabular")
4836 return print_mailer_error("InsetTabularMailer", in, 2,
4839 Buffer const & buffer = inset.buffer();
4840 inset.read(buffer, lex);
4844 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4847 data << name_ << ' ';
4848 inset.write(inset.buffer(), data);
4849 data << "\\end_inset\n";