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"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "CoordCache.h"
30 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
37 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "paragraph_funcs.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "support/convert.h"
50 #include "support/docstream.h"
51 #include "support/lstrings.h"
53 #include "frontends/alert.h"
54 #include "frontends/Clipboard.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
58 #include <boost/scoped_ptr.hpp>
68 using std::istringstream;
71 using std::ostringstream;
76 #ifndef CXX_GLOBAL_CSTD
80 using boost::shared_ptr;
81 using boost::dynamic_pointer_cast;
85 using support::prefixIs;
88 using support::suffixIs;
90 using cap::dirtyTabularStack;
91 using cap::tabularStackDirty;
93 using graphics::PreviewLoader;
95 using frontend::Painter;
96 using frontend::Clipboard;
98 namespace Alert = frontend::Alert;
103 int const ADD_TO_HEIGHT = 2;
104 int const ADD_TO_TABULAR_WIDTH = 2;
105 int const default_line_space = 10;
106 int const WIDTH_OF_LINE = 5;
110 boost::scoped_ptr<Tabular> paste_tabular;
113 struct TabularFeature {
114 Tabular::Feature action;
119 TabularFeature tabularFeature[] =
121 { Tabular::APPEND_ROW, "append-row" },
122 { Tabular::APPEND_COLUMN, "append-column" },
123 { Tabular::DELETE_ROW, "delete-row" },
124 { Tabular::DELETE_COLUMN, "delete-column" },
125 { Tabular::COPY_ROW, "copy-row" },
126 { Tabular::COPY_COLUMN, "copy-column" },
127 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
128 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
129 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
130 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
131 { Tabular::ALIGN_LEFT, "align-left" },
132 { Tabular::ALIGN_RIGHT, "align-right" },
133 { Tabular::ALIGN_CENTER, "align-center" },
134 { Tabular::ALIGN_BLOCK, "align-block" },
135 { Tabular::VALIGN_TOP, "valign-top" },
136 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
137 { Tabular::VALIGN_MIDDLE, "valign-middle" },
138 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
139 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
140 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
141 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
142 { Tabular::M_ALIGN_LEFT, "m-align-left" },
143 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
144 { Tabular::M_ALIGN_CENTER, "m-align-center" },
145 { Tabular::M_VALIGN_TOP, "m-valign-top" },
146 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
147 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
148 { Tabular::MULTICOLUMN, "multicolumn" },
149 { Tabular::SET_ALL_LINES, "set-all-lines" },
150 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
151 { Tabular::SET_LONGTABULAR, "set-longtabular" },
152 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
153 { Tabular::SET_PWIDTH, "set-pwidth" },
154 { Tabular::SET_MPWIDTH, "set-mpwidth" },
155 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
156 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
157 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
158 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
159 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
160 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
161 { Tabular::SET_USEBOX, "set-usebox" },
162 { Tabular::SET_LTHEAD, "set-lthead" },
163 { Tabular::UNSET_LTHEAD, "unset-lthead" },
164 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
165 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
166 { Tabular::SET_LTFOOT, "set-ltfoot" },
167 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
168 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
169 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
170 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
171 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
172 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
173 { Tabular::SET_BOOKTABS, "set-booktabs" },
174 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
175 { Tabular::SET_TOP_SPACE, "set-top-space" },
176 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
177 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
178 { Tabular::LAST_ACTION, "" }
182 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
184 FeatureEqual(Tabular::Feature feature)
185 : feature_(feature) {}
186 bool operator()(TabularFeature const & tf) const {
187 return tf.action == feature_;
190 Tabular::Feature feature_;
195 string const write_attribute(string const & name, T const & t)
197 string const s = tostr(t);
198 return s.empty() ? s : " " + name + "=\"" + s + "\"";
202 string const write_attribute(string const & name, string const & t)
204 return t.empty() ? t : " " + name + "=\"" + t + "\"";
209 string const write_attribute(string const & name, docstring const & t)
211 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
216 string const write_attribute(string const & name, bool const & b)
218 // we write only true attribute values so we remove a bit of the
219 // file format bloat for tabulars.
220 return b ? write_attribute(name, convert<string>(b)) : string();
225 string const write_attribute(string const & name, int const & i)
227 // we write only true attribute values so we remove a bit of the
228 // file format bloat for tabulars.
229 return i ? write_attribute(name, convert<string>(i)) : string();
234 string const write_attribute(string const & name, Tabular::idx_type const & i)
236 // we write only true attribute values so we remove a bit of the
237 // file format bloat for tabulars.
238 return i ? write_attribute(name, convert<string>(i)) : string();
243 string const write_attribute(string const & name, Length const & value)
245 // we write only the value if we really have one same reson as above.
246 return value.zero() ? string() : write_attribute(name, value.asString());
250 string const tostr(LyXAlignment const & num)
255 case LYX_ALIGN_BLOCK:
259 case LYX_ALIGN_CENTER:
261 case LYX_ALIGN_RIGHT:
263 case LYX_ALIGN_LAYOUT:
265 case LYX_ALIGN_SPECIAL:
272 string const tostr(Tabular::VAlignment const & num)
275 case Tabular::LYX_VALIGN_TOP:
277 case Tabular::LYX_VALIGN_MIDDLE:
279 case Tabular::LYX_VALIGN_BOTTOM:
286 string const tostr(Tabular::BoxType const & num)
289 case Tabular::BOX_NONE:
291 case Tabular::BOX_PARBOX:
293 case Tabular::BOX_MINIPAGE:
300 // I would have liked a fromstr template a lot better. (Lgb)
301 bool string2type(string const str, LyXAlignment & num)
304 num = LYX_ALIGN_NONE;
305 else if (str == "block")
306 num = LYX_ALIGN_BLOCK;
307 else if (str == "left")
308 num = LYX_ALIGN_LEFT;
309 else if (str == "center")
310 num = LYX_ALIGN_CENTER;
311 else if (str == "right")
312 num = LYX_ALIGN_RIGHT;
319 bool string2type(string const str, Tabular::VAlignment & num)
322 num = Tabular::LYX_VALIGN_TOP;
323 else if (str == "middle" )
324 num = Tabular::LYX_VALIGN_MIDDLE;
325 else if (str == "bottom")
326 num = Tabular::LYX_VALIGN_BOTTOM;
333 bool string2type(string const str, Tabular::BoxType & num)
336 num = Tabular::BOX_NONE;
337 else if (str == "parbox")
338 num = Tabular::BOX_PARBOX;
339 else if (str == "minipage")
340 num = Tabular::BOX_MINIPAGE;
347 bool string2type(string const str, bool & num)
351 else if (str == "false")
359 bool getTokenValue(string const & str, char const * token, string & ret)
362 size_t token_length = strlen(token);
363 string::size_type pos = str.find(token);
365 if (pos == string::npos || pos + token_length + 1 >= str.length()
366 || str[pos + token_length] != '=')
368 pos += token_length + 1;
370 if (ch != '"' && ch != '\'') { // only read till next space
374 while (pos < str.length() - 1 && str[++pos] != ch)
381 bool getTokenValue(string const & str, char const * token, docstring & ret)
384 bool const success = getTokenValue(str, token, tmp);
385 ret = from_utf8(tmp);
390 bool getTokenValue(string const & str, char const * token, int & num)
394 if (!getTokenValue(str, token, tmp))
396 num = convert<int>(tmp);
401 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token,
409 Tabular::VAlignment & num)
412 return getTokenValue(str, token, tmp) && string2type(tmp, num);
416 bool getTokenValue(string const & str, char const * token,
417 Tabular::BoxType & num)
420 return getTokenValue(str, token, tmp) && string2type(tmp, num);
424 bool getTokenValue(string const & str, char const * token, bool & flag)
426 // set the flag always to false as this should be the default for bools
427 // not in the file-format.
430 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
434 bool getTokenValue(string const & str, char const * token, Length & len)
436 // set the length to be zero() as default as this it should be if not
437 // in the file format.
440 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
444 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
449 if (!getTokenValue(str, token, tmp))
451 if (tmp == "default") {
455 return isValidLength(tmp, &len);
459 void l_getline(istream & is, string & str)
462 while (str.empty()) {
464 if (!str.empty() && str[str.length() - 1] == '\r')
465 str.erase(str.length() - 1);
472 string const featureAsString(Tabular::Feature feature)
474 TabularFeature * end = tabularFeature +
475 sizeof(tabularFeature) / sizeof(TabularFeature);
476 TabularFeature * it = std::find_if(tabularFeature, end,
477 FeatureEqual(feature));
478 return (it == end) ? string() : it->feature;
483 /////////////////////////////////////////////////////////////////////
487 /////////////////////////////////////////////////////////////////////
490 Tabular::cellstruct::cellstruct(BufferParams const & bp)
493 multicolumn(Tabular::CELL_NORMAL),
494 alignment(LYX_ALIGN_CENTER),
495 valignment(LYX_VALIGN_TOP),
502 inset(new InsetText(bp))
506 Tabular::cellstruct::cellstruct(cellstruct const & cs)
509 multicolumn(cs.multicolumn),
510 alignment(cs.alignment),
511 valignment(cs.valignment),
512 top_line(cs.top_line),
513 bottom_line(cs.bottom_line),
514 left_line(cs.left_line),
515 right_line(cs.right_line),
518 align_special(cs.align_special),
520 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
524 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
531 void Tabular::cellstruct::swap(cellstruct & rhs)
533 std::swap(cellno, rhs.cellno);
534 std::swap(width, rhs.width);
535 std::swap(multicolumn, rhs.multicolumn);
536 std::swap(alignment, rhs.alignment);
537 std::swap(valignment, rhs.valignment);
538 std::swap(top_line, rhs.top_line);
539 std::swap(bottom_line, rhs.bottom_line);
540 std::swap(left_line, rhs.left_line);
541 std::swap(right_line, rhs.right_line);
542 std::swap(usebox, rhs.usebox);
543 std::swap(rotate, rhs.rotate);
544 std::swap(align_special, rhs.align_special);
545 p_width.swap(rhs.p_width);
546 inset.swap(rhs.inset);
550 Tabular::rowstruct::rowstruct()
555 top_space_default(false),
556 bottom_space_default(false),
557 interline_space_default(false),
566 Tabular::columnstruct::columnstruct()
567 : alignment(LYX_ALIGN_CENTER),
568 valignment(LYX_VALIGN_TOP),
576 Tabular::ltType::ltType()
589 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
590 col_type columns_arg)
592 init(bp, rows_arg, columns_arg);
596 // activates all lines and sets all widths to 0
597 void Tabular::init(BufferParams const & bp, row_type rows_arg,
598 col_type columns_arg)
600 row_info = row_vector(rows_arg);
601 column_info = column_vector(columns_arg);
602 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
603 row_info.reserve(10);
604 column_info.reserve(10);
605 cell_info.reserve(100);
607 for (row_type i = 0; i < rows_arg; ++i)
608 cell_info[i].back().right_line = true;
609 row_info.back().bottom_line = true;
610 row_info.front().bottom_line = true;
611 column_info.back().right_line = true;
612 is_long_tabular = false;
614 use_booktabs = false;
618 void Tabular::fixCellNums()
621 for (row_type i = 0; i < rowCount(); ++i) {
622 for (col_type j = 0; j < columnCount(); ++j) {
623 // When debugging it can be nice to set
625 cell_info[i][j].inset->setDrawFrame(false);
626 cell_info[i][j].cellno = cellno++;
628 cell_info[i].back().right_line = true;
635 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
637 row_type const row = cellRow(cell);
639 row_vector::iterator rit = row_info.begin() + row;
640 row_info.insert(rit, rowstruct());
641 // now set the values of the row before
642 row_info[row] = row_info[row + 1];
644 row_type const nrows = rowCount();
645 col_type const ncols = columnCount();
647 cell_vvector old(nrows - 1);
648 for (row_type i = 0; i < nrows - 1; ++i)
649 swap(cell_info[i], old[i]);
651 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
653 for (row_type i = 0; i <= row; ++i)
654 swap(cell_info[i], old[i]);
655 for (row_type i = row + 2; i < nrows; ++i)
656 swap(cell_info[i], old[i - 1]);
659 for (col_type j = 0; j < ncols; ++j)
660 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
666 void Tabular::deleteRow(row_type const row)
668 // Not allowed to delete last row
672 row_info.erase(row_info.begin() + row);
673 cell_info.erase(cell_info.begin() + row);
678 void Tabular::copyRow(BufferParams const & bp, row_type const row)
680 row_info.insert(row_info.begin() + row, row_info[row]);
681 cell_info.insert(cell_info.begin() + row, cell_info[row]);
684 for (col_type j = 0; j < columnCount(); ++j)
685 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
691 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
693 col_type const column = cellColumn(cell);
694 col_type const ncols = columnCount();
695 column_vector::iterator cit = column_info.begin() + column + 1;
696 column_info.insert(cit, columnstruct());
697 // set the column values of the column before
698 column_info[column + 1] = column_info[column];
700 for (row_type i = 0; i < rowCount(); ++i) {
701 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
703 // care about multicolumns
704 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
705 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
707 if (column + 2 >= ncols
708 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
709 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
712 for (row_type i = 0; i < rowCount(); ++i) {
713 cell_info[i][column + 1].inset->clear();
715 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
721 void Tabular::deleteColumn(col_type const column)
723 // Not allowed to delete last column
724 if (columnCount() == 1)
727 column_info.erase(column_info.begin() + column);
728 for (row_type i = 0; i < rowCount(); ++i) {
729 // Care about multicolumn cells
730 if (column + 1 < columnCount() &&
731 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
732 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
733 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
735 cell_info[i].erase(cell_info[i].begin() + column);
741 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
743 column_info.insert(column_info.begin() + column, column_info[column]);
745 for (row_type i = 0; i < rowCount(); ++i)
746 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
749 for (row_type i = 0; i < rowCount(); ++i)
750 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
755 void Tabular::updateIndexes()
758 // Count only non-multicol cells plus begin multicol
759 // cells, ignore non-begin multicol cells:
760 for (row_type row = 0; row < rowCount(); ++row) {
761 for (col_type column = 0; column < columnCount(); ++column) {
762 if (cell_info[row][column].multicolumn
763 != Tabular::CELL_PART_OF_MULTICOLUMN)
765 BOOST_ASSERT(numberofcells != 0);
766 cell_info[row][column].cellno =
771 rowofcell.resize(numberofcells);
772 columnofcell.resize(numberofcells);
777 c < numberofcells && row < rowCount() && column < columnCount();) {
779 columnofcell[c] = column;
783 } while (column < columnCount() &&
784 cell_info[row][column].multicolumn
785 == Tabular::CELL_PART_OF_MULTICOLUMN);
787 if (column == columnCount()) {
793 for (row_type row = 0; row < rowCount(); ++row) {
794 for (col_type column = 0; column < columnCount(); ++column) {
795 if (isPartOfMultiColumn(row,column))
797 cell_info[row][column].inset->setAutoBreakRows(
798 !getPWidth(cellIndex(row, column)).zero());
804 Tabular::idx_type Tabular::cellCount() const
806 return numberofcells;
810 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
812 row_type const row = cellRow(cell);
814 for (col_type i = 0; i < columnCount(); ++i)
815 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
821 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
823 if (!wholerow && isMultiColumn(cell) &&
824 !(use_booktabs && cellRow(cell) == 0))
825 return cellinfo_of_cell(cell).top_line;
826 return row_info[cellRow(cell)].top_line;
830 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
832 if (!wholerow && isMultiColumn(cell) &&
833 !(use_booktabs && isLastRow(cell)))
834 return cellinfo_of_cell(cell).bottom_line;
835 return row_info[cellRow(cell)].bottom_line;
839 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
843 if (!wholecolumn && isMultiColumn(cell) &&
844 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
845 return cellinfo_of_cell(cell).left_line;
846 return column_info[cellColumn(cell)].left_line;
850 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
854 if (!wholecolumn && isMultiColumn(cell) &&
855 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
856 return cellinfo_of_cell(cell).right_line;
857 return column_info[cellRightColumn(cell)].right_line;
861 bool Tabular::topAlreadyDrawn(idx_type cell) const
863 row_type row = cellRow(cell);
864 if (row > 0 && !getAdditionalHeight(row)) {
865 col_type column = cellColumn(cell);
868 && cell_info[row][column].multicolumn
869 == Tabular::CELL_PART_OF_MULTICOLUMN)
871 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
872 return row_info[row].bottom_line;
874 return cell_info[row][column].bottom_line;
880 bool Tabular::leftAlreadyDrawn(idx_type cell) const
882 col_type column = cellColumn(cell);
884 row_type row = cellRow(cell);
886 (cell_info[row][column].multicolumn ==
887 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
888 if (getAdditionalWidth(cell_info[row][column].cellno))
890 return rightLine(cell_info[row][column].cellno);
896 bool Tabular::isLastRow(idx_type cell) const
898 return cellRow(cell) == rowCount() - 1;
902 int Tabular::getAdditionalHeight(row_type row) const
904 if (!row || row >= rowCount())
910 for (col_type column = 0; column < columnCount() && bottom; ++column) {
911 switch (cell_info[row - 1][column].multicolumn) {
912 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
913 bottom = cell_info[row - 1][column].bottom_line;
915 case Tabular::CELL_NORMAL:
916 bottom = row_info[row - 1].bottom_line;
919 for (col_type column = 0; column < columnCount() && top; ++column) {
920 switch (cell_info[row][column].multicolumn) {
921 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
922 top = cell_info[row][column].top_line;
924 case Tabular::CELL_NORMAL:
925 top = row_info[row].top_line;
928 int const interline_space = row_info[row - 1].interline_space_default ?
930 row_info[row - 1].interline_space.inPixels(width());
932 return interline_space + WIDTH_OF_LINE;
933 return interline_space;
937 int Tabular::getAdditionalWidth(idx_type cell) const
939 // internally already set in setCellWidth
940 // used to get it back in text.cpp
941 col_type const col = cellRightColumn(cell);
942 row_type const row = cellRow(cell);
943 if (col < columnCount() - 1 && rightLine(cell) &&
944 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
946 return WIDTH_OF_LINE;
952 // returns the maximum over all rows
953 int Tabular::columnWidth(idx_type cell) const
955 col_type const column1 = cellColumn(cell);
956 col_type const column2 = cellRightColumn(cell);
958 for (col_type i = column1; i <= column2; ++i)
959 result += column_info[i].width;
964 int Tabular::width() const
967 for (col_type i = 0; i < columnCount(); ++i)
968 width += column_info[i].width;
973 // returns true if a complete update is necessary, otherwise false
974 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
976 if (!isMultiColumn(cell))
979 row_type const row = cellRow(cell);
980 col_type const column1 = cellColumn(cell);
981 col_type const column2 = cellRightColumn(cell);
982 int const old_val = cell_info[row][column2].width;
984 // first set columns to 0 so we can calculate the right width
985 for (col_type i = column1; i <= column2; ++i) {
986 cell_info[row][i].width = 0;
988 // set the width to MAX_WIDTH until width > 0
989 int width = new_width + 2 * WIDTH_OF_LINE;
990 col_type i = column1;
991 for (; i < column2 && width > column_info[i].width; ++i) {
992 cell_info[row][i].width = column_info[i].width;
993 width -= column_info[i].width;
996 cell_info[row][i].width = width;
998 if (old_val != cell_info[row][column2].width) {
999 // in this case we have to recalculate all multicolumn cells which
1000 // have this column as one of theirs but not as last one
1001 calculate_width_of_column_NMC(i);
1002 recalculateMulticolumnsOfColumn(i);
1003 calculate_width_of_column(i);
1009 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1011 // the last column does not have to be recalculated because all
1012 // multicolumns will have here there last multicolumn cell which
1013 // always will have the whole rest of the width of the cell.
1014 if (columnCount() < 2 || column > (columnCount() - 2))
1016 for (row_type row = 0; row < rowCount(); ++row) {
1017 int mc = cell_info[row][column].multicolumn;
1018 int nmc = cell_info[row][column+1].multicolumn;
1019 // we only have to update multicolumns which do not have this
1020 // column as their last column!
1021 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1022 (mc == CELL_PART_OF_MULTICOLUMN &&
1023 nmc == CELL_PART_OF_MULTICOLUMN))
1025 idx_type const cellno = cell_info[row][column].cellno;
1026 setWidthOfMulticolCell(cellno,
1027 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1033 void Tabular::setCellWidth(idx_type cell, int new_width)
1035 row_type const row = cellRow(cell);
1036 col_type const column1 = cellColumn(cell);
1041 if (rightLine(cell_info[row][column1].cellno, true) &&
1042 column1 < columnCount() - 1 &&
1043 leftLine(cell_info[row][column1+1].cellno, true))
1045 add_width = WIDTH_OF_LINE;
1048 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1051 if (isMultiColumnReal(cell)) {
1052 tmp = setWidthOfMulticolCell(cell, new_width);
1054 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1055 cell_info[row][column1].width = width;
1056 tmp = calculate_width_of_column_NMC(column1);
1058 recalculateMulticolumnsOfColumn(column1);
1061 for (col_type i = 0; i < columnCount(); ++i)
1062 calculate_width_of_column(i);
1067 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1070 if (!isMultiColumn(cell) || onlycolumn)
1071 column_info[cellColumn(cell)].alignment = align;
1073 cellinfo_of_cell(cell).alignment = align;
1077 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1080 if (!isMultiColumn(cell) || onlycolumn)
1081 column_info[cellColumn(cell)].valignment = align;
1083 cellinfo_of_cell(cell).valignment = align;
1090 * Allow line and paragraph breaks for fixed width cells or disallow them,
1091 * merge cell paragraphs and reset layout to standard for variable width
1094 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1096 inset->setAutoBreakRows(fixedWidth);
1100 // merge all paragraphs to one
1101 BufferParams const & bp = cur.bv().buffer().params();
1102 while (inset->paragraphs().size() > 1)
1103 mergeParagraph(bp, inset->paragraphs(), 0);
1107 // undo information has already been recorded
1108 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1109 bp.getTextClass().defaultLayoutName());
1116 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1117 Length const & width)
1119 col_type const j = cellColumn(cell);
1121 column_info[j].p_width = width;
1122 for (row_type i = 0; i < rowCount(); ++i) {
1123 idx_type const cell = cellIndex(i, j);
1124 // because of multicolumns
1125 toggleFixedWidth(cur, getCellInset(cell).get(),
1126 !getPWidth(cell).zero());
1128 // cur paragraph can become invalid after paragraphs were merged
1129 if (cur.pit() > cur.lastpit())
1130 cur.pit() = cur.lastpit();
1131 // cur position can become invalid after newlines were removed
1132 if (cur.pos() > cur.lastpos())
1133 cur.pos() = cur.lastpos();
1137 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1138 Length const & width)
1140 if (!isMultiColumn(cell))
1143 cellinfo_of_cell(cell).p_width = width;
1144 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1145 // cur paragraph can become invalid after paragraphs were merged
1146 if (cur.pit() > cur.lastpit())
1147 cur.pit() = cur.lastpit();
1148 // cur position can become invalid after newlines were removed
1149 if (cur.pos() > cur.lastpos())
1150 cur.pos() = cur.lastpos();
1155 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1156 Tabular::Feature what)
1158 if (what == SET_SPECIAL_MULTI)
1159 cellinfo_of_cell(cell).align_special = special;
1161 column_info[cellColumn(cell)].align_special = special;
1165 void Tabular::setAllLines(idx_type cell, bool line)
1167 setTopLine(cell, line);
1168 setBottomLine(cell, line);
1169 setRightLine(cell, line);
1170 setLeftLine(cell, line);
1174 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1176 row_type const row = cellRow(cell);
1177 if (wholerow || !isMultiColumn(cell))
1178 row_info[row].top_line = line;
1180 cellinfo_of_cell(cell).top_line = line;
1184 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1186 if (wholerow || !isMultiColumn(cell))
1187 row_info[cellRow(cell)].bottom_line = line;
1189 cellinfo_of_cell(cell).bottom_line = line;
1193 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1195 if (wholecolumn || !isMultiColumn(cell))
1196 column_info[cellColumn(cell)].left_line = line;
1198 cellinfo_of_cell(cell).left_line = line;
1202 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1204 if (wholecolumn || !isMultiColumn(cell))
1205 column_info[cellRightColumn(cell)].right_line = line;
1207 cellinfo_of_cell(cell).right_line = line;
1211 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1213 if (!onlycolumn && isMultiColumn(cell))
1214 return cellinfo_of_cell(cell).alignment;
1215 return column_info[cellColumn(cell)].alignment;
1220 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1222 if (!onlycolumn && isMultiColumn(cell))
1223 return cellinfo_of_cell(cell).valignment;
1224 return column_info[cellColumn(cell)].valignment;
1228 Length const Tabular::getPWidth(idx_type cell) const
1230 if (isMultiColumn(cell))
1231 return cellinfo_of_cell(cell).p_width;
1232 return column_info[cellColumn(cell)].p_width;
1236 Length const Tabular::getColumnPWidth(idx_type cell) const
1238 return column_info[cellColumn(cell)].p_width;
1242 Length const Tabular::getMColumnPWidth(idx_type cell) const
1244 if (isMultiColumn(cell))
1245 return cellinfo_of_cell(cell).p_width;
1250 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1252 if (what == SET_SPECIAL_MULTI)
1253 return cellinfo_of_cell(cell).align_special;
1254 return column_info[cellColumn(cell)].align_special;
1258 int Tabular::cellWidth(idx_type cell) const
1260 row_type const row = cellRow(cell);
1261 col_type const column1 = cellColumn(cell);
1262 col_type const column2 = cellRightColumn(cell);
1264 for (col_type i = column1; i <= column2; ++i)
1265 result += cell_info[row][i].width;
1270 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1274 switch (getAlignment(cell)) {
1275 case LYX_ALIGN_CENTER:
1276 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1278 case LYX_ALIGN_RIGHT:
1279 x += columnWidth(cell) - cellWidth(cell);
1280 // + getAdditionalWidth(cell);
1283 // LYX_ALIGN_LEFT: nothing :-)
1287 // the LaTeX Way :-(
1293 bool Tabular::isFirstCellInRow(idx_type cell) const
1295 return cellColumn(cell) == 0;
1299 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1301 if (row > rowCount() - 1)
1302 row = rowCount() - 1;
1303 return cell_info[row][0].cellno;
1307 bool Tabular::isLastCellInRow(idx_type cell) const
1309 return cellRightColumn(cell) == columnCount() - 1;
1313 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1315 if (row > rowCount() - 1)
1316 row = rowCount() - 1;
1317 return cell_info[row][columnCount() - 1].cellno;
1321 void Tabular::calculate_width_of_column(col_type column)
1324 for (row_type i = 0; i < rowCount(); ++i)
1325 maximum = max(cell_info[i][column].width, maximum);
1326 column_info[column].width = maximum;
1331 // Calculate the columns regarding ONLY the normal cells and if this
1332 // column is inside a multicolumn cell then use it only if its the last
1333 // column of this multicolumn cell as this gives an added width to the
1334 // column, all the rest should be adapted!
1336 bool Tabular::calculate_width_of_column_NMC(col_type column)
1338 int const old_column_width = column_info[column].width;
1340 for (row_type i = 0; i < rowCount(); ++i) {
1341 idx_type cell = cellIndex(i, column);
1342 bool ismulti = isMultiColumnReal(cell);
1343 if ((!ismulti || column == cellRightColumn(cell)) &&
1344 cell_info[i][column].width > max)
1346 max = cell_info[i][column].width;
1349 column_info[column].width = max;
1350 return column_info[column].width != old_column_width;
1354 Tabular::row_type Tabular::cellRow(idx_type cell) const
1356 if (cell >= cellCount())
1357 return rowCount() - 1;
1360 return rowofcell[cell];
1364 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1366 if (cell >= cellCount())
1367 return columnCount() - 1;
1370 return columnofcell[cell];
1374 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1376 row_type const row = cellRow(cell);
1377 col_type column = cellColumn(cell);
1378 while (column < columnCount() - 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", rowCount())
1391 << write_attribute("columns", columnCount())
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 < columnCount(); ++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 < rowCount(); ++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 < columnCount(); ++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 < columnCount(); ++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 < rowCount(); ++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 < columnCount(); ++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);
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 cellColumn(cell) != cellRightColumn(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[cellRow(cell)][cellColumn(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[cellColumn(cell)].alignment;
1614 cs.top_line = row_info[cellRow(cell)].top_line;
1615 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1616 cs.left_line = column_info[cellColumn(cell)].left_line;
1617 cs.right_line = column_info[cellColumn(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());
1628 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1630 row_type const row = cellRow(cell);
1631 col_type column = cellColumn(cell);
1632 idx_type result = 1;
1634 while (column < columnCount() &&
1635 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1644 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1646 row_type const row = cellRow(cell);
1647 col_type column = cellColumn(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 < columnCount() &&
1655 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1657 cell_info[row][column].multicolumn = CELL_NORMAL;
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 < rowCount(); ++i)
1720 for (col_type j = 0; j < columnCount(); ++j)
1721 if (cell_info[i][j].rotate)
1727 bool Tabular::isLastCell(idx_type cell) const
1729 if (cell + 1 < cellCount())
1735 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1737 if (cellRow(cell) > 0)
1738 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1743 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1745 if (cellRow(cell) + 1 < rowCount())
1746 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1751 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1753 if (cellRow(cell) == 0)
1755 if (!isMultiColumn(cell))
1756 return getCellAbove(cell);
1757 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1761 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1763 if (cellRow(cell) + 1 >= rowCount())
1765 if (!isMultiColumn(cell))
1766 return getCellBelow(cell);
1767 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1771 Tabular::idx_type Tabular::cellIndex(row_type row,
1772 col_type column) const
1774 BOOST_ASSERT(column != npos && column < columnCount() &&
1775 row != npos && row < rowCount());
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[cellColumn(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 < rowCount(); ++i)
1877 if (row_info[i].endhead)
1883 bool Tabular::haveLTFirstHead() const
1885 if (endfirsthead.empty)
1887 for (row_type i = 0; i < rowCount(); ++i)
1888 if (row_info[i].endfirsthead)
1894 bool Tabular::haveLTFoot() const
1896 for (row_type i = 0; i < rowCount(); ++i)
1897 if (row_info[i].endfoot)
1903 bool Tabular::haveLTLastFoot() const
1905 if (endlastfoot.empty)
1907 for (row_type i = 0; i < rowCount(); ++i)
1908 if (row_info[i].endlastfoot)
1914 // end longtable support functions
1916 void Tabular::setRowAscent(row_type row, int height)
1918 if (row >= rowCount() || row_info[row].ascent == height)
1920 row_info[row].ascent = height;
1924 void Tabular::setRowDescent(row_type row, int height)
1926 if (row >= rowCount() || row_info[row].descent == height)
1928 row_info[row].descent = height;
1932 int Tabular::rowAscent(row_type row) const
1934 if (row >= rowCount())
1936 return row_info[row].ascent;
1940 int Tabular::rowDescent(row_type row) const
1942 BOOST_ASSERT(row < rowCount());
1943 return row_info[row].descent;
1947 int Tabular::height() const
1950 for (row_type row = 0; row < rowCount(); ++row)
1951 height += rowAscent(row) + rowDescent(row) +
1952 getAdditionalHeight(row);
1957 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1959 BOOST_ASSERT(row < rowCount());
1960 BOOST_ASSERT(column < columnCount());
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 < rowCount());
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 << cellColumn(i) + 1
1989 << cellRightColumn(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 >= rowCount())
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 == rowCount() - 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 << cellColumn(i) + 1
2025 << cellRightColumn(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) < cellCount()) && !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 < rowCount(); ++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 < rowCount(); ++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 < rowCount(); ++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 < rowCount(); ++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 = cellIndex(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 < columnCount(); ++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.isRTL(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 < columnCount(); ++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 < rowCount(); ++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 < columnCount(); ++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=\"" << columnCount()
2511 << "\" colsep=\"1\" rowsep=\"1\">\n";
2513 for (col_type i = 0; i < columnCount(); ++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 < rowCount(); ++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 < rowCount(); ++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 < rowCount(); ++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 = cellColumn(i);
2615 int len = clen[column];
2616 while (column < columnCount() - 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 = cellColumn(i);
2663 int len = clen[column];
2664 while (column < columnCount() -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 < columnCount() -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, char_type delim) const
2737 // first calculate the width of the single columns
2738 vector<unsigned int> clen(columnCount());
2741 // first all non (real) multicolumn cells!
2742 for (col_type j = 0; j < columnCount(); ++j) {
2744 for (row_type i = 0; i < rowCount(); ++i) {
2745 idx_type cell = cellIndex(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 < columnCount(); ++j) {
2756 for (row_type i = 0; i < rowCount(); ++i) {
2757 idx_type cell = cellIndex(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 < rowCount(); ++i) {
2773 if (!onlydata && plaintextTopHLine(os, i, clen))
2774 os << docstring(depth * 2, ' ');
2775 for (col_type j = 0; j < columnCount(); ++j) {
2776 if (isPartOfMultiColumn(i, j))
2778 if (onlydata && j > 0)
2779 // we don't use operator<< for single UCS4 character.
2780 // see explanation in docstream.h
2782 plaintextPrintCell(buf, os, runparams,
2783 cell, i, j, clen, onlydata);
2788 os << docstring(depth * 2, ' ');
2789 if (plaintextBottomHLine(os, i, clen))
2790 os << docstring(depth * 2, ' ');
2796 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2798 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2802 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2803 col_type column) const
2805 return cell_info[row][column].inset;
2809 void Tabular::setCellInset(row_type row, col_type column,
2810 shared_ptr<InsetText> ins) const
2812 cell_info[row][column].inset = ins;
2817 Tabular::getCellFromInset(Inset const * inset) const
2819 // is this inset part of the tabular?
2821 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2822 BOOST_ASSERT(false);
2825 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2826 if (getCellInset(cell).get() == inset) {
2827 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2828 << "cell=" << cell);
2832 // We should have found a cell at this point
2833 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2834 << inset << " not found!" << endl;
2835 BOOST_ASSERT(false);
2841 void Tabular::validate(LaTeXFeatures & features) const
2843 features.require("NeedTabularnewline");
2845 features.require("booktabs");
2846 if (isLongTabular())
2847 features.require("longtable");
2849 features.require("rotating");
2850 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2851 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2852 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2853 features.require("array");
2854 getCellInset(cell)->validate(features);
2859 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2861 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2862 ParagraphList::const_iterator cit = parlist.begin();
2863 ParagraphList::const_iterator end = parlist.end();
2865 for (; cit != end; ++cit)
2866 for (int i = 0; i < cit->size(); ++i)
2867 if (cit->isNewline(i))
2874 /////////////////////////////////////////////////////////////////////
2878 /////////////////////////////////////////////////////////////////////
2880 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2882 : tabular(buf.params(), max(rows, row_type(1)),
2883 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2887 InsetTabular::InsetTabular(InsetTabular const & tab)
2888 : Inset(tab), tabular(tab.tabular),
2889 buffer_(tab.buffer_), scx_(0)
2893 InsetTabular::~InsetTabular()
2895 InsetTabularMailer(*this).hideDialog();
2899 Inset * InsetTabular::clone() const
2901 return new InsetTabular(*this);
2905 Buffer const & InsetTabular::buffer() const
2911 void InsetTabular::buffer(Buffer const * b)
2917 void InsetTabular::write(Buffer const & buf, ostream & os) const
2919 os << "Tabular" << endl;
2920 tabular.write(buf, os);
2924 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2926 bool const old_format = (lex.getString() == "\\LyXTable");
2928 tabular.read(buf, lex);
2934 string token = lex.getString();
2935 while (lex && token != "\\end_inset") {
2937 token = lex.getString();
2940 lex.printError("Missing \\end_inset at this point. "
2946 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2948 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2949 // mi.base.textwidth << "\n";
2951 lyxerr << "InsetTabular::metrics: need bv" << endl;
2952 BOOST_ASSERT(false);
2956 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2959 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2960 if (tabular.isPartOfMultiColumn(i, j))
2961 // Multicolumn cell, but not first one
2966 if (tabular.cell_info[i][j].multicolumn ==
2967 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2968 p_width = tabular.cellinfo_of_cell(cell).p_width;
2970 p_width = tabular.column_info[j].p_width;
2971 if (!p_width.zero())
2972 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2973 tabular.getCellInset(cell)->metrics(m, dim);
2974 if (!p_width.zero())
2975 dim.wid = m.base.textwidth;
2976 tabular.setCellWidth(cell, dim.wid);
2977 if (p_width.zero()) {
2978 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2979 // FIXME there must be a way to get rid of
2980 // the second metrics call
2981 tabular.getCellInset(cell)->metrics(m, dim);
2983 maxAsc = max(maxAsc, dim.asc);
2984 maxDesc = max(maxDesc, dim.des);
2987 int const top_space = tabular.row_info[i].top_space_default ?
2988 default_line_space :
2989 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2990 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2991 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2992 default_line_space :
2993 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2994 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2997 dim.asc = tabular.rowAscent(0);
2998 dim.des = tabular.height() - dim.asc;
2999 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3003 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3005 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3006 BufferView * bv = pi.base.bv;
3008 // FIXME: As the full backrgound is painted in drawSelection(),
3009 // we have no choice but to do a full repaint for the Text cells.
3010 pi.full_repaint = true;
3012 resetPos(bv->cursor());
3015 x += ADD_TO_TABULAR_WIDTH;
3017 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3020 first_visible_cell = Tabular::npos;
3021 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3023 int const a = tabular.rowAscent(i);
3024 int const d = tabular.rowDescent(i);
3025 idx = tabular.cellIndex(i, 0);
3026 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3027 if (tabular.isPartOfMultiColumn(i, j))
3029 if (first_visible_cell == Tabular::npos)
3030 first_visible_cell = idx;
3032 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3033 // Cache the Inset position.
3034 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3035 if (nx + tabular.columnWidth(idx) < 0
3036 || nx > bv->workWidth()
3038 || y - a > bv->workHeight()) {
3039 pi.pain.setDrawingEnabled(false);
3040 cell(idx)->draw(pi, cx, y);
3041 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3042 pi.pain.setDrawingEnabled(original_drawing_state);
3044 cell(idx)->draw(pi, cx, y);
3045 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3047 nx += tabular.columnWidth(idx);
3051 if (i + 1 < tabular.rowCount())
3052 y += d + tabular.rowAscent(i + 1) +
3053 tabular.getAdditionalHeight(i + 1);
3058 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3060 Cursor & cur = pi.base.bv->cursor();
3062 x += scx_ + ADD_TO_TABULAR_WIDTH;
3064 // FIXME: it is wrong to completely paint the background
3065 // if we want to do single row painting.
3067 // Paint background of current tabular
3068 int const w = tabular.width();
3069 int const h = tabular.height();
3070 int yy = y - tabular.rowAscent(0);
3071 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3073 if (!cur.selection())
3075 if (&cur.inset() != this)
3081 if (tablemode(cur)) {
3084 getSelection(cur, rs, re, cs, ce);
3085 y -= tabular.rowAscent(0);
3086 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3087 int const a = tabular.rowAscent(j);
3088 int const h = a + tabular.rowDescent(j);
3090 y += tabular.getAdditionalHeight(j);
3091 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3092 if (tabular.isPartOfMultiColumn(j, i))
3094 idx_type const cell =
3095 tabular.cellIndex(j, i);
3096 int const w = tabular.columnWidth(cell);
3097 if (i >= cs && i <= ce && j >= rs && j <= re)
3098 pi.pain.fillRectangle(xx, y, w, h,
3106 x += getCellXPos(cur.idx());
3107 x += tabular.getBeginningOfTextInCell(cur.idx());
3108 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3113 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3114 row_type row, idx_type cell, bool erased) const
3116 int x2 = x + tabular.columnWidth(cell);
3117 bool on_off = false;
3118 ColorCode col = Color_tabularline;
3119 ColorCode onoffcol = Color_tabularonoffline;
3122 col = Color_deletedtext;
3123 onoffcol = Color_deletedtext;
3126 if (!tabular.topAlreadyDrawn(cell)) {
3127 on_off = !tabular.topLine(cell);
3128 pain.line(x, y - tabular.rowAscent(row),
3129 x2, y - tabular.rowAscent(row),
3130 on_off ? onoffcol : col,
3131 on_off ? Painter::line_onoffdash : Painter::line_solid);
3133 on_off = !tabular.bottomLine(cell);
3134 pain.line(x, y + tabular.rowDescent(row),
3135 x2, y + tabular.rowDescent(row),
3136 on_off ? onoffcol : col,
3137 on_off ? Painter::line_onoffdash : Painter::line_solid);
3138 if (!tabular.leftAlreadyDrawn(cell)) {
3139 on_off = !tabular.leftLine(cell);
3140 pain.line(x, y - tabular.rowAscent(row),
3141 x, y + tabular.rowDescent(row),
3142 on_off ? onoffcol : col,
3143 on_off ? Painter::line_onoffdash : Painter::line_solid);
3145 on_off = !tabular.rightLine(cell);
3146 pain.line(x2 - tabular.getAdditionalWidth(cell),
3147 y - tabular.rowAscent(row),
3148 x2 - tabular.getAdditionalWidth(cell),
3149 y + tabular.rowDescent(row),
3150 on_off ? onoffcol : col,
3151 on_off ? Painter::line_onoffdash : Painter::line_solid);
3155 docstring const InsetTabular::editMessage() const
3157 return _("Opened table");
3161 void InsetTabular::edit(Cursor & cur, bool left)
3163 //lyxerr << "InsetTabular::edit: " << this << endl;
3165 cur.selection() = false;
3168 if (isRightToLeft(cur))
3169 cur.idx() = tabular.getLastCellInRow(0);
3175 if (isRightToLeft(cur))
3176 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3178 cur.idx() = tabular.cellCount() - 1;
3180 cur.pos() = cur.lastpos(); // FIXME crude guess
3182 // FIXME: this accesses the position cache before it is initialized
3184 //cur.bv().fitCursor();
3188 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3190 // In a longtable, tell captions what the current float is
3191 Counters & cnts = buf.params().getTextClass().counters();
3192 string const saveflt = cnts.current_float();
3193 if (tabular.isLongTabular())
3194 cnts.current_float("table");
3196 ParIterator it2 = it;
3198 size_t const end = it2.nargs();
3199 for ( ; it2.idx() < end; it2.top().forwardIdx())
3200 lyx::updateLabels(buf, it2);
3203 if (tabular.isLongTabular())
3204 cnts.current_float(saveflt);
3208 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3210 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3211 << "\n cur:" << cur);
3212 CursorSlice sl = cur.top();
3213 Cursor & bvcur = cur.bv().cursor();
3215 switch (cmd.action) {
3217 case LFUN_MOUSE_PRESS:
3218 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3220 // do not reset cursor/selection if we have selected
3221 // some cells (bug 2715).
3222 if (cmd.button() == mouse_button::button3
3223 && &bvcur.selBegin().inset() == this
3224 && tablemode(bvcur))
3227 // Let InsetText do it
3228 cell(cur.idx())->dispatch(cur, cmd);
3230 case LFUN_MOUSE_MOTION:
3231 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3232 if (cmd.button() == mouse_button::button1) {
3233 // only accept motions to places not deeper nested than the real anchor
3234 if (bvcur.anchor_.hasPart(cur)) {
3235 // only update if selection changes
3236 if (bvcur.idx() == cur.idx() &&
3237 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3239 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3240 bvcur.setCursor(cur);
3241 bvcur.selection() = true;
3247 case LFUN_MOUSE_RELEASE:
3248 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3249 if (cmd.button() == mouse_button::button3)
3250 InsetTabularMailer(*this).showDialog(&cur.bv());
3253 case LFUN_CELL_BACKWARD:
3255 cur.selection() = false;
3258 case LFUN_CELL_FORWARD:
3260 cur.selection() = false;
3263 case LFUN_CHAR_FORWARD_SELECT:
3264 case LFUN_CHAR_FORWARD:
3265 cell(cur.idx())->dispatch(cur, cmd);
3266 if (!cur.result().dispatched()) {
3268 if (sl == cur.top())
3269 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3275 case LFUN_CHAR_BACKWARD_SELECT:
3276 case LFUN_CHAR_BACKWARD:
3277 cell(cur.idx())->dispatch(cur, cmd);
3278 if (!cur.result().dispatched()) {
3280 if (sl == cur.top())
3281 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3287 case LFUN_CHAR_RIGHT_SELECT:
3288 case LFUN_CHAR_RIGHT:
3289 //FIXME: for visual cursor, really move right
3290 if (isRightToLeft(cur))
3291 lyx::dispatch(FuncRequest(
3292 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3293 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3295 lyx::dispatch(FuncRequest(
3296 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3297 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3300 case LFUN_CHAR_LEFT_SELECT:
3301 case LFUN_CHAR_LEFT:
3302 //FIXME: for visual cursor, really move left
3303 if (isRightToLeft(cur))
3304 lyx::dispatch(FuncRequest(
3305 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3306 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3308 lyx::dispatch(FuncRequest(
3309 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3310 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3313 case LFUN_DOWN_SELECT:
3315 cell(cur.idx())->dispatch(cur, cmd);
3316 cur.dispatched(); // override the cell's decision
3317 if (sl == cur.top())
3318 // if our Text didn't do anything to the cursor
3319 // then we try to put the cursor into the cell below
3320 // setting also the right targetX.
3321 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3322 cur.idx() = tabular.getCellBelow(cur.idx());
3324 TextMetrics const & tm =
3325 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3326 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3328 if (sl == cur.top()) {
3329 // we trick it to go to forward after leaving the
3331 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3336 case LFUN_UP_SELECT:
3338 cell(cur.idx())->dispatch(cur, cmd);
3339 cur.dispatched(); // override the cell's decision
3340 if (sl == cur.top())
3341 // if our Text didn't do anything to the cursor
3342 // then we try to put the cursor into the cell above
3343 // setting also the right targetX.
3344 if (tabular.cellRow(cur.idx()) != 0) {
3345 cur.idx() = tabular.getCellAbove(cur.idx());
3346 cur.pit() = cur.lastpit();
3347 Text const * text = cell(cur.idx())->getText(0);
3348 TextMetrics const & tm = cur.bv().textMetrics(text);
3349 ParagraphMetrics const & pm =
3350 tm.parMetrics(cur.lastpit());
3351 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3353 if (sl == cur.top()) {
3354 cmd = FuncRequest(LFUN_UP);
3359 // case LFUN_SCREEN_DOWN: {
3360 // //if (hasSelection())
3361 // // cur.selection() = false;
3362 // col_type const col = tabular.cellColumn(cur.idx());
3363 // int const t = cur.bv().top_y() + cur.bv().height();
3364 // if (t < yo() + tabular.getHeightOfTabular()) {
3365 // cur.bv().scrollDocView(t);
3366 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3368 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3375 // case LFUN_SCREEN_UP: {
3376 // //if (hasSelection())
3377 // // cur.selection() = false;
3378 // col_type const col = tabular.cellColumn(cur.idx());
3379 // int const t = cur.bv().top_y() + cur.bv().height();
3381 // cur.bv().scrollDocView(t);
3385 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3389 // cur.par() = cur.lastpar();
3390 // cur.pos() = cur.lastpos();
3394 case LFUN_LAYOUT_TABULAR:
3395 InsetTabularMailer(*this).showDialog(&cur.bv());
3398 case LFUN_INSET_DIALOG_UPDATE:
3399 InsetTabularMailer(*this).updateDialog(&cur.bv());
3402 case LFUN_TABULAR_FEATURE:
3403 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3407 // insert file functions
3408 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3409 case LFUN_FILE_INSERT_PLAINTEXT: {
3411 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
3412 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3418 if (tablemode(cur)) {
3419 if (copySelection(cur)) {
3420 cur.recordUndoInset(DELETE_UNDO);
3425 cell(cur.idx())->dispatch(cur, cmd);
3428 case LFUN_CHAR_DELETE_BACKWARD:
3429 case LFUN_CHAR_DELETE_FORWARD:
3430 if (tablemode(cur)) {
3431 cur.recordUndoInset(DELETE_UNDO);
3435 cell(cur.idx())->dispatch(cur, cmd);
3439 if (!cur.selection())
3441 if (tablemode(cur)) {
3445 cell(cur.idx())->dispatch(cur, cmd);
3448 case LFUN_CLIPBOARD_PASTE:
3449 case LFUN_PRIMARY_SELECTION_PASTE: {
3450 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3451 theClipboard().getAsText() :
3452 theSelection().get();
3455 // pass to InsertPlaintextString, but
3456 // only if we have multi-cell content
3457 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3458 if (insertPlaintextString(cur.bv(), clip, false)) {
3459 // content has been replaced,
3460 // so cursor might be invalid
3461 cur.pos() = cur.lastpos();
3462 bvcur.setCursor(cur);
3466 // Let the cell handle normal text
3467 cell(cur.idx())->dispatch(cur, cmd);
3472 if (tabularStackDirty() && theClipboard().isInternal()) {
3473 cur.recordUndoInset(INSERT_UNDO);
3474 pasteClipboard(cur);
3477 cell(cur.idx())->dispatch(cur, cmd);
3480 case LFUN_FONT_EMPH:
3481 case LFUN_FONT_BOLD:
3482 case LFUN_FONT_ROMAN:
3483 case LFUN_FONT_NOUN:
3484 case LFUN_FONT_ITAL:
3485 case LFUN_FONT_FRAK:
3486 case LFUN_FONT_TYPEWRITER:
3487 case LFUN_FONT_SANS:
3488 case LFUN_FONT_FREE_APPLY:
3489 case LFUN_FONT_FREE_UPDATE:
3490 case LFUN_FONT_SIZE:
3491 case LFUN_FONT_UNDERLINE:
3493 case LFUN_WORD_CAPITALIZE:
3494 case LFUN_WORD_UPCASE:
3495 case LFUN_WORD_LOWCASE:
3496 case LFUN_CHARS_TRANSPOSE:
3497 if (tablemode(cur)) {
3500 getSelection(cur, rs, re, cs, ce);
3501 Cursor tmpcur = cur;
3502 for (row_type i = rs; i <= re; ++i) {
3503 for (col_type j = cs; j <= ce; ++j) {
3504 // cursor follows cell:
3505 tmpcur.idx() = tabular.cellIndex(i, j);
3506 // select this cell only:
3509 tmpcur.resetAnchor();
3510 tmpcur.pit() = tmpcur.lastpit();
3511 tmpcur.pos() = tmpcur.top().lastpos();
3512 tmpcur.setCursor(tmpcur);
3513 tmpcur.setSelection();
3514 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3519 cell(cur.idx())->dispatch(cur, cmd);
3523 // we try to handle this event in the insets dispatch function.
3524 cell(cur.idx())->dispatch(cur, cmd);
3530 // function sets an object as defined in func_status.h:
3531 // states OK, Unknown, Disabled, On, Off.
3532 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3533 FuncStatus & status) const
3535 switch (cmd.action) {
3536 case LFUN_TABULAR_FEATURE: {
3537 int action = Tabular::LAST_ACTION;
3539 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3540 string const tmp = tabularFeature[i].feature;
3541 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3542 action = tabularFeature[i].action;
3546 if (action == Tabular::LAST_ACTION) {
3548 status.unknown(true);
3552 string const argument
3553 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3555 row_type sel_row_start = 0;
3556 row_type sel_row_end = 0;
3557 col_type sel_col_start = 0;
3558 col_type sel_col_end = 0;
3559 Tabular::ltType dummyltt;
3562 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3565 case Tabular::SET_PWIDTH:
3566 case Tabular::SET_MPWIDTH:
3567 case Tabular::SET_SPECIAL_COLUMN:
3568 case Tabular::SET_SPECIAL_MULTI:
3569 case Tabular::APPEND_ROW:
3570 case Tabular::APPEND_COLUMN:
3571 case Tabular::DELETE_ROW:
3572 case Tabular::DELETE_COLUMN:
3573 case Tabular::COPY_ROW:
3574 case Tabular::COPY_COLUMN:
3575 case Tabular::SET_ALL_LINES:
3576 case Tabular::UNSET_ALL_LINES:
3577 case Tabular::SET_TOP_SPACE:
3578 case Tabular::SET_BOTTOM_SPACE:
3579 case Tabular::SET_INTERLINE_SPACE:
3583 case Tabular::MULTICOLUMN:
3584 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3587 case Tabular::M_TOGGLE_LINE_TOP:
3589 case Tabular::TOGGLE_LINE_TOP:
3590 status.setOnOff(tabular.topLine(cur.idx(), flag));
3593 case Tabular::M_TOGGLE_LINE_BOTTOM:
3595 case Tabular::TOGGLE_LINE_BOTTOM:
3596 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3599 case Tabular::M_TOGGLE_LINE_LEFT:
3601 case Tabular::TOGGLE_LINE_LEFT:
3602 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3605 case Tabular::M_TOGGLE_LINE_RIGHT:
3607 case Tabular::TOGGLE_LINE_RIGHT:
3608 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3611 case Tabular::M_ALIGN_LEFT:
3613 case Tabular::ALIGN_LEFT:
3614 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3617 case Tabular::M_ALIGN_RIGHT:
3619 case Tabular::ALIGN_RIGHT:
3620 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3623 case Tabular::M_ALIGN_CENTER:
3625 case Tabular::ALIGN_CENTER:
3626 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3629 case Tabular::ALIGN_BLOCK:
3630 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3631 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3634 case Tabular::M_VALIGN_TOP:
3636 case Tabular::VALIGN_TOP:
3638 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3641 case Tabular::M_VALIGN_BOTTOM:
3643 case Tabular::VALIGN_BOTTOM:
3645 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3648 case Tabular::M_VALIGN_MIDDLE:
3650 case Tabular::VALIGN_MIDDLE:
3652 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3655 case Tabular::SET_LONGTABULAR:
3656 status.setOnOff(tabular.isLongTabular());
3659 case Tabular::UNSET_LONGTABULAR:
3660 status.setOnOff(!tabular.isLongTabular());
3663 case Tabular::TOGGLE_ROTATE_TABULAR:
3664 case Tabular::SET_ROTATE_TABULAR:
3665 status.setOnOff(tabular.getRotateTabular());
3668 case Tabular::UNSET_ROTATE_TABULAR:
3669 status.setOnOff(!tabular.getRotateTabular());
3672 case Tabular::TOGGLE_ROTATE_CELL:
3673 case Tabular::SET_ROTATE_CELL:
3674 status.setOnOff(!oneCellHasRotationState(false,
3675 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3678 case Tabular::UNSET_ROTATE_CELL:
3679 status.setOnOff(!oneCellHasRotationState(true,
3680 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3683 case Tabular::SET_USEBOX:
3684 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3687 case Tabular::SET_LTFIRSTHEAD:
3688 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3691 case Tabular::UNSET_LTFIRSTHEAD:
3692 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3695 case Tabular::SET_LTHEAD:
3696 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3699 case Tabular::UNSET_LTHEAD:
3700 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3703 case Tabular::SET_LTFOOT:
3704 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3707 case Tabular::UNSET_LTFOOT:
3708 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3711 case Tabular::SET_LTLASTFOOT:
3712 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3715 case Tabular::UNSET_LTLASTFOOT:
3716 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3719 case Tabular::SET_LTNEWPAGE:
3720 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3723 case Tabular::SET_BOOKTABS:
3724 status.setOnOff(tabular.useBookTabs());
3727 case Tabular::UNSET_BOOKTABS:
3728 status.setOnOff(!tabular.useBookTabs());
3733 status.enabled(false);
3739 // These are only enabled inside tabular
3740 case LFUN_CELL_BACKWARD:
3741 case LFUN_CELL_FORWARD:
3742 status.enabled(true);
3745 // disable these with multiple cells selected
3746 case LFUN_INSET_INSERT:
3747 case LFUN_TABULAR_INSERT:
3748 case LFUN_FLEX_INSERT:
3749 case LFUN_FLOAT_INSERT:
3750 case LFUN_FLOAT_WIDE_INSERT:
3751 case LFUN_FOOTNOTE_INSERT:
3752 case LFUN_MARGINALNOTE_INSERT:
3753 case LFUN_MATH_INSERT:
3754 case LFUN_MATH_MODE:
3755 case LFUN_MATH_MUTATE:
3756 case LFUN_MATH_DISPLAY:
3757 case LFUN_NOTE_INSERT:
3758 case LFUN_OPTIONAL_INSERT:
3759 case LFUN_BOX_INSERT:
3760 case LFUN_BRANCH_INSERT:
3761 case LFUN_WRAP_INSERT:
3762 case LFUN_ERT_INSERT: {
3763 if (tablemode(cur)) {
3764 status.enabled(false);
3767 return cell(cur.idx())->getStatus(cur, cmd, status);
3770 // disable in non-fixed-width cells
3771 case LFUN_BREAK_LINE:
3772 case LFUN_BREAK_PARAGRAPH:
3773 case LFUN_BREAK_PARAGRAPH_SKIP: {
3774 if (tabular.getPWidth(cur.idx()).zero()) {
3775 status.enabled(false);
3778 return cell(cur.idx())->getStatus(cur, cmd, status);
3782 if (tabularStackDirty() && theClipboard().isInternal()) {
3783 status.enabled(true);
3786 return cell(cur.idx())->getStatus(cur, cmd, status);
3788 case LFUN_INSET_MODIFY:
3789 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3790 status.enabled(true);
3796 // we try to handle this event in the insets dispatch function.
3797 return cell(cur.idx())->getStatus(cur, cmd, status);
3802 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3803 OutputParams const & runparams) const
3805 return tabular.latex(buf, os, runparams);
3809 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3810 OutputParams const & runparams) const
3812 os << '\n'; // output table on a new line
3813 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3814 tabular.plaintext(buf, os, runparams, dp, false, 0);
3815 return PLAINTEXT_NEWLINE;
3819 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3820 OutputParams const & runparams) const
3825 // FIXME: Why not pass a proper DocIterator here?
3827 // if the table is inside a float it doesn't need the informaltable
3828 // wrapper. Search for it.
3829 for (master = owner(); master; master = master->owner())
3830 if (master->lyxCode() == FLOAT_CODE)
3835 os << "<informaltable>";
3838 ret += tabular.docbook(buf, os, runparams);
3840 os << "</informaltable>";
3847 void InsetTabular::validate(LaTeXFeatures & features) const
3849 tabular.validate(features);
3853 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3855 return tabular.getCellInset(idx);
3859 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3861 return tabular.getCellInset(idx);
3865 void InsetTabular::cursorPos(BufferView const & bv,
3866 CursorSlice const & sl, bool boundary, int & x, int & y) const
3868 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3870 // y offset correction
3871 int const row = tabular.cellRow(sl.idx());
3872 for (int i = 0; i <= row; ++i) {
3874 y += tabular.rowAscent(i);
3875 y += tabular.getAdditionalHeight(i);
3878 y += tabular.rowDescent(i);
3881 // x offset correction
3882 int const col = tabular.cellColumn(sl.idx());
3883 int idx = tabular.cellIndex(row, 0);
3884 for (int j = 0; j < col; ++j) {
3885 if (tabular.isPartOfMultiColumn(row, j))
3887 x += tabular.columnWidth(idx);
3890 x += tabular.getBeginningOfTextInCell(idx);
3891 x += ADD_TO_TABULAR_WIDTH;
3896 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3900 Inset const & inset = *tabular.getCellInset(cell);
3901 Point o = bv.coordCache().getInsets().xy(&inset);
3902 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3903 int const xend = xbeg + tabular.columnWidth(cell);
3904 row_type const row = tabular.cellRow(cell);
3905 int const ybeg = o.y_ - tabular.rowAscent(row) -
3906 tabular.getAdditionalHeight(row);
3907 int const yend = o.y_ + tabular.rowDescent(row);
3919 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3920 // << " ybeg=" << ybeg << " yend=" << yend
3921 // << " xx=" << xx << " yy=" << yy
3922 // << " dist=" << xx + yy << endl;
3927 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3929 //lyxerr << "InsetTabular::editXY: " << this << endl;
3930 cur.selection() = false;
3932 cur.idx() = getNearestCell(cur.bv(), x, y);
3934 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3938 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3940 cur.idx() = getNearestCell(cur.bv(), x, y);
3941 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3945 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3947 idx_type idx_min = 0;
3948 int dist_min = std::numeric_limits<int>::max();
3949 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3950 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3951 int const d = dist(bv, i, x, y);
3962 int InsetTabular::getCellXPos(idx_type const cell) const
3966 for (; !tabular.isFirstCellInRow(c); --c)
3969 for (; c < cell; ++c)
3970 lx += tabular.columnWidth(c);
3976 void InsetTabular::resetPos(Cursor & cur) const
3978 BufferView & bv = cur.bv();
3979 int const maxwidth = bv.workWidth();
3981 if (&cur.inset() != this) {
3985 int const X2 = maxwidth;
3986 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3987 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3988 int const x2 = x1 + tabular.columnWidth(cur.idx());
3991 scx_ = X1 + 20 - x1;
3993 scx_ = X2 - 20 - x2;
3998 cur.updateFlags(Update::Force | Update::FitCursor);
4002 void InsetTabular::moveNextCell(Cursor & cur)
4004 if (isRightToLeft(cur)) {
4005 if (tabular.isFirstCellInRow(cur.idx())) {
4006 row_type const row = tabular.cellRow(cur.idx());
4007 if (row == tabular.rowCount() - 1)
4009 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4016 if (tabular.isLastCell(cur.idx()))
4026 void InsetTabular::movePrevCell(Cursor & cur)
4028 if (isRightToLeft(cur)) {
4029 if (tabular.isLastCellInRow(cur.idx())) {
4030 row_type const row = tabular.cellRow(cur.idx());
4033 cur.idx() = tabular.getFirstCellInRow(row);
4034 cur.idx() = tabular.getCellAbove(cur.idx());
4036 if (tabular.isLastCell(cur.idx()))
4041 if (cur.idx() == 0) // first cell
4045 cur.pit() = cur.lastpit();
4046 cur.pos() = cur.lastpos();
4048 // FIXME: this accesses the position cache before it is initialized
4053 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4055 Tabular::Feature action = Tabular::LAST_ACTION;
4058 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4059 string const tmp = tabularFeature[i].feature;
4061 if (tmp == what.substr(0, tmp.length())) {
4062 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4063 //tabularFeatures[i].feature.length()))
4064 action = tabularFeature[i].action;
4068 if (action == Tabular::LAST_ACTION)
4072 ltrim(what.substr(tabularFeature[i].feature.length()));
4073 tabularFeatures(cur, action, val);
4078 static void checkLongtableSpecial(Tabular::ltType & ltt,
4079 string const & special, bool & flag)
4081 if (special == "dl_above") {
4084 } else if (special == "dl_below") {
4085 ltt.bottomDL = flag;
4087 } else if (special == "empty") {
4096 bool InsetTabular::oneCellHasRotationState(bool rotated,
4097 row_type row_start, row_type row_end,
4098 col_type col_start, col_type col_end) const {
4100 for (row_type i = row_start; i <= row_end; ++i) {
4101 for (col_type j = col_start; j <= col_end; ++j) {
4102 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4111 void InsetTabular::tabularFeatures(Cursor & cur,
4112 Tabular::Feature feature, string const & value)
4114 BufferView & bv = cur.bv();
4115 col_type sel_col_start;
4116 col_type sel_col_end;
4117 row_type sel_row_start;
4118 row_type sel_row_end;
4119 bool setLines = false;
4120 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4121 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4125 case Tabular::M_ALIGN_LEFT:
4126 case Tabular::ALIGN_LEFT:
4127 setAlign = LYX_ALIGN_LEFT;
4130 case Tabular::M_ALIGN_RIGHT:
4131 case Tabular::ALIGN_RIGHT:
4132 setAlign = LYX_ALIGN_RIGHT;
4135 case Tabular::M_ALIGN_CENTER:
4136 case Tabular::ALIGN_CENTER:
4137 setAlign = LYX_ALIGN_CENTER;
4140 case Tabular::ALIGN_BLOCK:
4141 setAlign = LYX_ALIGN_BLOCK;
4144 case Tabular::M_VALIGN_TOP:
4145 case Tabular::VALIGN_TOP:
4146 setVAlign = Tabular::LYX_VALIGN_TOP;
4149 case Tabular::M_VALIGN_BOTTOM:
4150 case Tabular::VALIGN_BOTTOM:
4151 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4154 case Tabular::M_VALIGN_MIDDLE:
4155 case Tabular::VALIGN_MIDDLE:
4156 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4163 cur.recordUndoInset(ATOMIC_UNDO);
4165 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4166 row_type const row = tabular.cellRow(cur.idx());
4167 col_type const column = tabular.cellColumn(cur.idx());
4169 Tabular::ltType ltt;
4173 case Tabular::SET_PWIDTH: {
4174 Length const len(value);
4175 tabular.setColumnPWidth(cur, cur.idx(), len);
4177 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4178 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4182 case Tabular::SET_MPWIDTH:
4183 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4186 case Tabular::SET_SPECIAL_COLUMN:
4187 case Tabular::SET_SPECIAL_MULTI:
4188 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4191 case Tabular::APPEND_ROW:
4192 // append the row into the tabular
4193 tabular.appendRow(bv.buffer().params(), cur.idx());
4196 case Tabular::APPEND_COLUMN:
4197 // append the column into the tabular
4198 tabular.appendColumn(bv.buffer().params(), cur.idx());
4199 cur.idx() = tabular.cellIndex(row, column);
4202 case Tabular::DELETE_ROW:
4203 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4204 tabular.deleteRow(sel_row_start);
4205 if (sel_row_start >= tabular.rowCount())
4207 cur.idx() = tabular.cellIndex(sel_row_start, column);
4210 cur.selection() = false;
4213 case Tabular::DELETE_COLUMN:
4214 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4215 tabular.deleteColumn(sel_col_start);
4216 if (sel_col_start >= tabular.columnCount())
4218 cur.idx() = tabular.cellIndex(row, sel_col_start);
4221 cur.selection() = false;
4224 case Tabular::COPY_ROW:
4225 tabular.copyRow(bv.buffer().params(), row);
4228 case Tabular::COPY_COLUMN:
4229 tabular.copyColumn(bv.buffer().params(), column);
4230 cur.idx() = tabular.cellIndex(row, column);
4233 case Tabular::M_TOGGLE_LINE_TOP:
4235 case Tabular::TOGGLE_LINE_TOP: {
4236 bool lineSet = !tabular.topLine(cur.idx(), flag);
4237 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4238 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4240 tabular.cellIndex(i, j),
4245 case Tabular::M_TOGGLE_LINE_BOTTOM:
4247 case Tabular::TOGGLE_LINE_BOTTOM: {
4248 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4249 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4250 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4251 tabular.setBottomLine(
4252 tabular.cellIndex(i, j),
4258 case Tabular::M_TOGGLE_LINE_LEFT:
4260 case Tabular::TOGGLE_LINE_LEFT: {
4261 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4262 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4263 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4264 tabular.setLeftLine(
4265 tabular.cellIndex(i,j),
4271 case Tabular::M_TOGGLE_LINE_RIGHT:
4273 case Tabular::TOGGLE_LINE_RIGHT: {
4274 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4275 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4276 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4277 tabular.setRightLine(
4278 tabular.cellIndex(i,j),
4284 case Tabular::M_ALIGN_LEFT:
4285 case Tabular::M_ALIGN_RIGHT:
4286 case Tabular::M_ALIGN_CENTER:
4288 case Tabular::ALIGN_LEFT:
4289 case Tabular::ALIGN_RIGHT:
4290 case Tabular::ALIGN_CENTER:
4291 case Tabular::ALIGN_BLOCK:
4292 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4293 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4294 tabular.setAlignment(
4295 tabular.cellIndex(i, j),
4300 case Tabular::M_VALIGN_TOP:
4301 case Tabular::M_VALIGN_BOTTOM:
4302 case Tabular::M_VALIGN_MIDDLE:
4304 case Tabular::VALIGN_TOP:
4305 case Tabular::VALIGN_BOTTOM:
4306 case Tabular::VALIGN_MIDDLE:
4307 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4308 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4309 tabular.setVAlignment(
4310 tabular.cellIndex(i, j),
4314 case Tabular::MULTICOLUMN: {
4315 if (sel_row_start != sel_row_end) {
4316 // FIXME: Need I say it ? This is horrible.
4318 Alert::error(_("Error setting multicolumn"),
4319 _("You cannot set multicolumn vertically."));
4322 if (!cur.selection()) {
4323 // just multicol for one single cell
4324 // check whether we are completely in a multicol
4325 if (tabular.isMultiColumn(cur.idx()))
4326 tabular.unsetMultiColumn(cur.idx());
4328 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4331 // we have a selection so this means we just add all this
4332 // cells to form a multicolumn cell
4333 idx_type const s_start = cur.selBegin().idx();
4334 idx_type const s_end = cur.selEnd().idx();
4335 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4336 cur.idx() = s_start;
4339 cur.selection() = false;
4343 case Tabular::SET_ALL_LINES:
4345 case Tabular::UNSET_ALL_LINES:
4346 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4347 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4348 tabular.setAllLines(
4349 tabular.cellIndex(i,j), setLines);
4352 case Tabular::SET_LONGTABULAR:
4353 tabular.setLongTabular(true);
4356 case Tabular::UNSET_LONGTABULAR:
4357 tabular.setLongTabular(false);
4360 case Tabular::SET_ROTATE_TABULAR:
4361 tabular.setRotateTabular(true);
4364 case Tabular::UNSET_ROTATE_TABULAR:
4365 tabular.setRotateTabular(false);
4368 case Tabular::TOGGLE_ROTATE_TABULAR:
4369 tabular.setRotateTabular(!tabular.getRotateTabular());
4372 case Tabular::SET_ROTATE_CELL:
4373 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4374 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4375 tabular.setRotateCell(
4376 tabular.cellIndex(i, j), true);
4379 case Tabular::UNSET_ROTATE_CELL:
4380 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4381 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4382 tabular.setRotateCell(
4383 tabular.cellIndex(i, j), false);
4386 case Tabular::TOGGLE_ROTATE_CELL:
4388 bool oneNotRotated = oneCellHasRotationState(false,
4389 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4391 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4392 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4393 tabular.setRotateCell(tabular.cellIndex(i, j),
4398 case Tabular::SET_USEBOX: {
4399 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4400 if (val == tabular.getUsebox(cur.idx()))
4401 val = Tabular::BOX_NONE;
4402 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4403 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4404 tabular.setUsebox(tabular.cellIndex(i, j), val);
4408 case Tabular::UNSET_LTFIRSTHEAD:
4410 case Tabular::SET_LTFIRSTHEAD:
4411 tabular.getRowOfLTFirstHead(row, ltt);
4412 checkLongtableSpecial(ltt, value, flag);
4413 tabular.setLTHead(row, flag, ltt, true);
4416 case Tabular::UNSET_LTHEAD:
4418 case Tabular::SET_LTHEAD:
4419 tabular.getRowOfLTHead(row, ltt);
4420 checkLongtableSpecial(ltt, value, flag);
4421 tabular.setLTHead(row, flag, ltt, false);
4424 case Tabular::UNSET_LTFOOT:
4426 case Tabular::SET_LTFOOT:
4427 tabular.getRowOfLTFoot(row, ltt);
4428 checkLongtableSpecial(ltt, value, flag);
4429 tabular.setLTFoot(row, flag, ltt, false);
4432 case Tabular::UNSET_LTLASTFOOT:
4434 case Tabular::SET_LTLASTFOOT:
4435 tabular.getRowOfLTLastFoot(row, ltt);
4436 checkLongtableSpecial(ltt, value, flag);
4437 tabular.setLTFoot(row, flag, ltt, true);
4440 case Tabular::SET_LTNEWPAGE:
4441 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4444 case Tabular::SET_BOOKTABS:
4445 tabular.setBookTabs(true);
4448 case Tabular::UNSET_BOOKTABS:
4449 tabular.setBookTabs(false);
4452 case Tabular::SET_TOP_SPACE: {
4454 if (value == "default")
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4456 tabular.row_info[i].top_space_default = true;
4457 else if (isValidLength(value, &len))
4458 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4459 tabular.row_info[i].top_space_default = false;
4460 tabular.row_info[i].top_space = len;
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4464 tabular.row_info[i].top_space_default = false;
4465 tabular.row_info[i].top_space = len;
4470 case Tabular::SET_BOTTOM_SPACE: {
4472 if (value == "default")
4473 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4474 tabular.row_info[i].bottom_space_default = true;
4475 else if (isValidLength(value, &len))
4476 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4477 tabular.row_info[i].bottom_space_default = false;
4478 tabular.row_info[i].bottom_space = len;
4481 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4482 tabular.row_info[i].bottom_space_default = false;
4483 tabular.row_info[i].bottom_space = len;
4488 case Tabular::SET_INTERLINE_SPACE: {
4490 if (value == "default")
4491 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4492 tabular.row_info[i].interline_space_default = true;
4493 else if (isValidLength(value, &len))
4494 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4495 tabular.row_info[i].interline_space_default = false;
4496 tabular.row_info[i].interline_space = len;
4499 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4500 tabular.row_info[i].interline_space_default = false;
4501 tabular.row_info[i].interline_space = len;
4506 // dummy stuff just to avoid warnings
4507 case Tabular::LAST_ACTION:
4513 bool InsetTabular::showInsetDialog(BufferView * bv) const
4515 InsetTabularMailer(*this).showDialog(bv);
4520 void InsetTabular::openLayoutDialog(BufferView * bv) const
4522 InsetTabularMailer(*this).showDialog(bv);
4526 bool InsetTabular::copySelection(Cursor & cur)
4528 if (!cur.selection())
4533 getSelection(cur, rs, re, cs, ce);
4535 paste_tabular.reset(new Tabular(tabular));
4537 for (row_type i = 0; i < rs; ++i)
4538 paste_tabular->deleteRow(0);
4540 row_type const rows = re - rs + 1;
4541 while (paste_tabular->rowCount() > rows)
4542 paste_tabular->deleteRow(rows);
4544 paste_tabular->setTopLine(0, true, true);
4545 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4548 for (col_type i = 0; i < cs; ++i)
4549 paste_tabular->deleteColumn(0);
4551 col_type const columns = ce - cs + 1;
4552 while (paste_tabular->columnCount() > columns)
4553 paste_tabular->deleteColumn(columns);
4555 paste_tabular->setLeftLine(0, true, true);
4556 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4559 odocstringstream os;
4560 OutputParams const runparams(0);
4561 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4562 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4563 cap::copySelection(cur, os.str());
4565 // mark tabular stack dirty
4566 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4567 // when we (hopefully) have a one-for-all paste mechanism.
4568 // This must be called after cap::copySelection.
4569 dirtyTabularStack(true);
4575 bool InsetTabular::pasteClipboard(Cursor & cur)
4579 col_type const actcol = tabular.cellColumn(cur.idx());
4580 row_type const actrow = tabular.cellRow(cur.idx());
4581 for (row_type r1 = 0, r2 = actrow;
4582 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4584 for (col_type c1 = 0, c2 = actcol;
4585 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4587 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4588 tabular.isPartOfMultiColumn(r2, c2))
4590 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4594 if (tabular.isPartOfMultiColumn(r2, c2)) {
4598 shared_ptr<InsetText> inset(
4599 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4600 tabular.setCellInset(r2, c2, inset);
4601 // FIXME: change tracking (MG)
4602 inset->setChange(Change(cur.buffer().params().trackChanges ?
4603 Change::INSERTED : Change::UNCHANGED));
4611 void InsetTabular::cutSelection(Cursor & cur)
4613 if (!cur.selection())
4618 getSelection(cur, rs, re, cs, ce);
4619 for (row_type i = rs; i <= re; ++i) {
4620 for (col_type j = cs; j <= ce; ++j) {
4621 shared_ptr<InsetText> t
4622 = cell(tabular.cellIndex(i, j));
4623 if (cur.buffer().params().trackChanges)
4624 // FIXME: Change tracking (MG)
4625 t->setChange(Change(Change::DELETED));
4631 // cursor position might be invalid now
4632 if (cur.pit() > cur.lastpit())
4633 cur.pit() = cur.lastpit();
4634 if (cur.pos() > cur.lastpos())
4635 cur.pos() = cur.lastpos();
4636 cur.clearSelection();
4640 bool InsetTabular::isRightToLeft(Cursor & cur) const
4642 BOOST_ASSERT(cur.depth() > 1);
4643 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4644 pos_type const parentpos = cur[cur.depth() - 2].pos();
4645 return parentpar.getFontSettings(cur.bv().buffer().params(),
4646 parentpos).language()->rightToLeft();
4650 void InsetTabular::getSelection(Cursor & cur,
4651 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4653 CursorSlice const & beg = cur.selBegin();
4654 CursorSlice const & end = cur.selEnd();
4655 cs = tabular.cellColumn(beg.idx());
4656 ce = tabular.cellColumn(end.idx());
4659 cs = tabular.cellColumn(end.idx());
4661 ce = tabular.cellRightColumn(end.idx());
4664 rs = tabular.cellRow(beg.idx());
4665 re = tabular.cellRow(end.idx());
4671 Text * InsetTabular::getText(int idx) const
4673 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4677 void InsetTabular::setChange(Change const & change)
4679 for (idx_type idx = 0; idx < nargs(); ++idx)
4680 cell(idx)->setChange(change);
4684 void InsetTabular::acceptChanges(BufferParams const & bparams)
4686 for (idx_type idx = 0; idx < nargs(); ++idx)
4687 cell(idx)->acceptChanges(bparams);
4691 void InsetTabular::rejectChanges(BufferParams const & bparams)
4693 for (idx_type idx = 0; idx < nargs(); ++idx)
4694 cell(idx)->rejectChanges(bparams);
4698 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4700 return tabular.getPWidth(cell).zero();
4704 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4707 if (buf.length() <= 0)
4710 Buffer const & buffer = bv.buffer();
4714 col_type maxCols = 1;
4715 docstring::size_type const len = buf.length();
4716 docstring::size_type p = 0;
4719 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4727 maxCols = max(cols, maxCols);
4733 maxCols = max(cols, maxCols);
4739 paste_tabular.reset(
4740 new Tabular(buffer.params(), rows, maxCols));
4741 loctab = paste_tabular.get();
4743 dirtyTabularStack(true);
4746 cell = bv.cursor().idx();
4747 ocol = tabular.cellColumn(cell);
4748 row = tabular.cellRow(cell);
4751 docstring::size_type op = 0;
4752 idx_type const cells = loctab->cellCount();
4755 rows = loctab->rowCount();
4756 col_type const columns = loctab->columnCount();
4758 while (cell < cells && p < len && row < rows &&
4759 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4765 // we can only set this if we are not too far right
4766 if (cols < columns) {
4767 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4768 Font const font = bv.textMetrics(&inset->text_).
4769 getDisplayFont(0, 0);
4770 inset->setText(buf.substr(op, p - op), font,
4771 buffer.params().trackChanges);
4777 // we can only set this if we are not too far right
4778 if (cols < columns) {
4779 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4780 Font const font = bv.textMetrics(&inset->text_).
4781 getDisplayFont(0, 0);
4782 inset->setText(buf.substr(op, p - op), font,
4783 buffer.params().trackChanges);
4788 cell = loctab->cellIndex(row, cols);
4794 // check for the last cell if there is no trailing '\n'
4795 if (cell < cells && op < len) {
4796 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4797 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4798 inset->setText(buf.substr(op, len - op), font,
4799 buffer.params().trackChanges);
4805 void InsetTabular::addPreview(PreviewLoader & loader) const
4807 row_type const rows = tabular.rowCount();
4808 col_type const columns = tabular.columnCount();
4809 for (row_type i = 0; i < rows; ++i) {
4810 for (col_type j = 0; j < columns; ++j)
4811 tabular.getCellInset(i, j)->addPreview(loader);
4816 bool InsetTabular::tablemode(Cursor & cur) const
4818 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4824 string const InsetTabularMailer::name_("tabular");
4826 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4827 : inset_(const_cast<InsetTabular &>(inset))
4831 string const InsetTabularMailer::inset2string(Buffer const &) const
4833 return params2string(inset_);
4837 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4839 istringstream data(in);
4841 lex.setStream(data);
4848 if (!lex || token != name_)
4849 return print_mailer_error("InsetTabularMailer", in, 1,
4852 // This is part of the inset proper that is usually swallowed
4853 // by Buffer::readInset
4855 if (!lex || token != "Tabular")
4856 return print_mailer_error("InsetTabularMailer", in, 2,
4859 Buffer const & buffer = inset.buffer();
4860 inset.read(buffer, lex);
4864 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4867 data << name_ << ' ';
4868 inset.write(inset.buffer(), data);
4869 data << "\\end_inset\n";