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"
31 #include "DispatchResult.h"
32 #include "FuncRequest.h"
33 #include "FuncStatus.h"
35 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/alert.h"
48 #include "frontends/Clipboard.h"
49 #include "frontends/Painter.h"
50 #include "frontends/Selection.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/gettext.h"
58 #include "support/lstrings.h"
60 #include <boost/scoped_ptr.hpp>
68 using namespace lyx::support;
70 using boost::shared_ptr;
71 using boost::dynamic_pointer_cast;
75 using cap::dirtyTabularStack;
76 using cap::tabularStackDirty;
78 using graphics::PreviewLoader;
80 using frontend::Painter;
81 using frontend::Clipboard;
83 namespace Alert = frontend::Alert;
88 int const ADD_TO_HEIGHT = 2;
89 int const ADD_TO_TABULAR_WIDTH = 2;
90 int const default_line_space = 10;
91 int const WIDTH_OF_LINE = 5;
95 boost::scoped_ptr<Tabular> paste_tabular;
98 struct TabularFeature {
99 Tabular::Feature action;
104 TabularFeature tabularFeature[] =
106 { Tabular::APPEND_ROW, "append-row" },
107 { Tabular::APPEND_COLUMN, "append-column" },
108 { Tabular::DELETE_ROW, "delete-row" },
109 { Tabular::DELETE_COLUMN, "delete-column" },
110 { Tabular::COPY_ROW, "copy-row" },
111 { Tabular::COPY_COLUMN, "copy-column" },
112 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
113 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
114 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
115 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
116 { Tabular::ALIGN_LEFT, "align-left" },
117 { Tabular::ALIGN_RIGHT, "align-right" },
118 { Tabular::ALIGN_CENTER, "align-center" },
119 { Tabular::ALIGN_BLOCK, "align-block" },
120 { Tabular::VALIGN_TOP, "valign-top" },
121 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
122 { Tabular::VALIGN_MIDDLE, "valign-middle" },
123 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
124 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
125 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
126 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
127 { Tabular::M_ALIGN_LEFT, "m-align-left" },
128 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
129 { Tabular::M_ALIGN_CENTER, "m-align-center" },
130 { Tabular::M_VALIGN_TOP, "m-valign-top" },
131 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
132 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
133 { Tabular::MULTICOLUMN, "multicolumn" },
134 { Tabular::SET_ALL_LINES, "set-all-lines" },
135 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
136 { Tabular::SET_LONGTABULAR, "set-longtabular" },
137 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
138 { Tabular::SET_PWIDTH, "set-pwidth" },
139 { Tabular::SET_MPWIDTH, "set-mpwidth" },
140 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
141 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
142 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
143 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
144 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
145 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
146 { Tabular::SET_USEBOX, "set-usebox" },
147 { Tabular::SET_LTHEAD, "set-lthead" },
148 { Tabular::UNSET_LTHEAD, "unset-lthead" },
149 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
150 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
151 { Tabular::SET_LTFOOT, "set-ltfoot" },
152 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
153 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
154 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
155 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
156 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
157 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
158 { Tabular::SET_BOOKTABS, "set-booktabs" },
159 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
160 { Tabular::SET_TOP_SPACE, "set-top-space" },
161 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
162 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
163 { Tabular::LAST_ACTION, "" }
167 class FeatureEqual : public unary_function<TabularFeature, bool> {
169 FeatureEqual(Tabular::Feature feature)
170 : feature_(feature) {}
171 bool operator()(TabularFeature const & tf) const {
172 return tf.action == feature_;
175 Tabular::Feature feature_;
180 string const write_attribute(string const & name, T const & t)
182 string const s = tostr(t);
183 return s.empty() ? s : " " + name + "=\"" + s + "\"";
187 string const write_attribute(string const & name, string const & t)
189 return t.empty() ? t : " " + name + "=\"" + t + "\"";
194 string const write_attribute(string const & name, docstring const & t)
196 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
201 string const write_attribute(string const & name, bool const & b)
203 // we write only true attribute values so we remove a bit of the
204 // file format bloat for tabulars.
205 return b ? write_attribute(name, convert<string>(b)) : string();
210 string const write_attribute(string const & name, int const & i)
212 // we write only true attribute values so we remove a bit of the
213 // file format bloat for tabulars.
214 return i ? write_attribute(name, convert<string>(i)) : string();
219 string const write_attribute(string const & name, Tabular::idx_type const & i)
221 // we write only true attribute values so we remove a bit of the
222 // file format bloat for tabulars.
223 return i ? write_attribute(name, convert<string>(i)) : string();
228 string const write_attribute(string const & name, Length const & value)
230 // we write only the value if we really have one same reson as above.
231 return value.zero() ? string() : write_attribute(name, value.asString());
235 string const tostr(LyXAlignment const & num)
240 case LYX_ALIGN_BLOCK:
244 case LYX_ALIGN_CENTER:
246 case LYX_ALIGN_RIGHT:
248 case LYX_ALIGN_LAYOUT:
250 case LYX_ALIGN_SPECIAL:
257 string const tostr(Tabular::VAlignment const & num)
260 case Tabular::LYX_VALIGN_TOP:
262 case Tabular::LYX_VALIGN_MIDDLE:
264 case Tabular::LYX_VALIGN_BOTTOM:
271 string const tostr(Tabular::BoxType const & num)
274 case Tabular::BOX_NONE:
276 case Tabular::BOX_PARBOX:
278 case Tabular::BOX_MINIPAGE:
285 // I would have liked a fromstr template a lot better. (Lgb)
286 bool string2type(string const str, LyXAlignment & num)
289 num = LYX_ALIGN_NONE;
290 else if (str == "block")
291 num = LYX_ALIGN_BLOCK;
292 else if (str == "left")
293 num = LYX_ALIGN_LEFT;
294 else if (str == "center")
295 num = LYX_ALIGN_CENTER;
296 else if (str == "right")
297 num = LYX_ALIGN_RIGHT;
304 bool string2type(string const str, Tabular::VAlignment & num)
307 num = Tabular::LYX_VALIGN_TOP;
308 else if (str == "middle" )
309 num = Tabular::LYX_VALIGN_MIDDLE;
310 else if (str == "bottom")
311 num = Tabular::LYX_VALIGN_BOTTOM;
318 bool string2type(string const str, Tabular::BoxType & num)
321 num = Tabular::BOX_NONE;
322 else if (str == "parbox")
323 num = Tabular::BOX_PARBOX;
324 else if (str == "minipage")
325 num = Tabular::BOX_MINIPAGE;
332 bool string2type(string const str, bool & num)
336 else if (str == "false")
344 bool getTokenValue(string const & str, char const * token, string & ret)
347 size_t token_length = strlen(token);
348 string::size_type pos = str.find(token);
350 if (pos == string::npos || pos + token_length + 1 >= str.length()
351 || str[pos + token_length] != '=')
353 pos += token_length + 1;
355 if (ch != '"' && ch != '\'') { // only read till next space
359 while (pos < str.length() - 1 && str[++pos] != ch)
366 bool getTokenValue(string const & str, char const * token, docstring & ret)
369 bool const success = getTokenValue(str, token, tmp);
370 ret = from_utf8(tmp);
375 bool getTokenValue(string const & str, char const * token, int & num)
379 if (!getTokenValue(str, token, tmp))
381 num = convert<int>(tmp);
386 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
389 return getTokenValue(str, token, tmp) && string2type(tmp, num);
393 bool getTokenValue(string const & str, char const * token,
394 Tabular::VAlignment & num)
397 return getTokenValue(str, token, tmp) && string2type(tmp, num);
401 bool getTokenValue(string const & str, char const * token,
402 Tabular::BoxType & num)
405 return getTokenValue(str, token, tmp) && string2type(tmp, num);
409 bool getTokenValue(string const & str, char const * token, bool & flag)
411 // set the flag always to false as this should be the default for bools
412 // not in the file-format.
415 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
419 bool getTokenValue(string const & str, char const * token, Length & len)
421 // set the length to be zero() as default as this it should be if not
422 // in the file format.
425 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
429 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
434 if (!getTokenValue(str, token, tmp))
436 if (tmp == "default") {
440 return isValidLength(tmp, &len);
444 void l_getline(istream & is, string & str)
447 while (str.empty()) {
449 if (!str.empty() && str[str.length() - 1] == '\r')
450 str.erase(str.length() - 1);
457 string const featureAsString(Tabular::Feature feature)
459 TabularFeature * end = tabularFeature +
460 sizeof(tabularFeature) / sizeof(TabularFeature);
461 TabularFeature * it = find_if(tabularFeature, end,
462 FeatureEqual(feature));
463 return (it == end) ? string() : it->feature;
468 /////////////////////////////////////////////////////////////////////
472 /////////////////////////////////////////////////////////////////////
475 Tabular::cellstruct::cellstruct(Buffer const & buffer)
478 multicolumn(Tabular::CELL_NORMAL),
479 alignment(LYX_ALIGN_CENTER),
480 valignment(LYX_VALIGN_TOP),
487 inset(new InsetText(buffer.params()))
489 inset->setBuffer(const_cast<Buffer &>(buffer));
490 inset->paragraphs().back().setLayout(buffer.params().textClass().emptyLayout());
494 Tabular::cellstruct::cellstruct(cellstruct const & cs)
497 multicolumn(cs.multicolumn),
498 alignment(cs.alignment),
499 valignment(cs.valignment),
500 top_line(cs.top_line),
501 bottom_line(cs.bottom_line),
502 left_line(cs.left_line),
503 right_line(cs.right_line),
506 align_special(cs.align_special),
508 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
512 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
519 void Tabular::cellstruct::swap(cellstruct & rhs)
521 std::swap(cellno, rhs.cellno);
522 std::swap(width, rhs.width);
523 std::swap(multicolumn, rhs.multicolumn);
524 std::swap(alignment, rhs.alignment);
525 std::swap(valignment, rhs.valignment);
526 std::swap(top_line, rhs.top_line);
527 std::swap(bottom_line, rhs.bottom_line);
528 std::swap(left_line, rhs.left_line);
529 std::swap(right_line, rhs.right_line);
530 std::swap(usebox, rhs.usebox);
531 std::swap(rotate, rhs.rotate);
532 std::swap(align_special, rhs.align_special);
533 p_width.swap(rhs.p_width);
534 inset.swap(rhs.inset);
538 Tabular::rowstruct::rowstruct()
543 top_space_default(false),
544 bottom_space_default(false),
545 interline_space_default(false),
554 Tabular::columnstruct::columnstruct()
555 : alignment(LYX_ALIGN_CENTER),
556 valignment(LYX_VALIGN_TOP),
564 Tabular::ltType::ltType()
577 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
579 init(buffer, rows_arg, columns_arg);
583 // activates all lines and sets all widths to 0
584 void Tabular::init(Buffer const & buf, row_type rows_arg,
585 col_type columns_arg)
588 row_info = row_vector(rows_arg);
589 column_info = column_vector(columns_arg);
590 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(buf)));
591 row_info.reserve(10);
592 column_info.reserve(10);
593 cell_info.reserve(100);
595 for (row_type i = 0; i < rows_arg; ++i)
596 cell_info[i].back().right_line = true;
597 row_info.back().bottom_line = true;
598 row_info.front().bottom_line = true;
599 column_info.back().right_line = true;
600 is_long_tabular = false;
602 use_booktabs = false;
606 void Tabular::fixCellNums()
609 for (row_type i = 0; i < rowCount(); ++i) {
610 for (col_type j = 0; j < columnCount(); ++j) {
611 // When debugging it can be nice to set
613 cell_info[i][j].inset->setDrawFrame(false);
614 cell_info[i][j].cellno = cellno++;
616 cell_info[i].back().right_line = true;
623 void Tabular::appendRow(idx_type const cell)
625 BufferParams const & bp = buffer().params();
626 row_type const row = cellRow(cell);
628 row_vector::iterator rit = row_info.begin() + row;
629 row_info.insert(rit, rowstruct());
630 // now set the values of the row before
631 row_info[row] = row_info[row + 1];
633 row_type const nrows = rowCount();
634 col_type const ncols = columnCount();
636 cell_vvector old(nrows - 1);
637 for (row_type i = 0; i < nrows - 1; ++i)
638 swap(cell_info[i], old[i]);
640 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(buffer())));
642 for (row_type i = 0; i <= row; ++i)
643 swap(cell_info[i], old[i]);
644 for (row_type i = row + 2; i < nrows; ++i)
645 swap(cell_info[i], old[i - 1]);
648 for (col_type j = 0; j < ncols; ++j)
649 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
655 void Tabular::deleteRow(row_type const row)
657 // Not allowed to delete last row
661 row_info.erase(row_info.begin() + row);
662 cell_info.erase(cell_info.begin() + row);
667 void Tabular::copyRow(row_type const row)
669 row_info.insert(row_info.begin() + row, row_info[row]);
670 cell_info.insert(cell_info.begin() + row, cell_info[row]);
672 if (buffer().params().trackChanges)
673 for (col_type j = 0; j < columnCount(); ++j)
674 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
680 void Tabular::appendColumn(idx_type const cell)
682 col_type const column = cellColumn(cell);
683 col_type const ncols = columnCount();
684 column_vector::iterator cit = column_info.begin() + column + 1;
685 column_info.insert(cit, columnstruct());
686 // set the column values of the column before
687 column_info[column + 1] = column_info[column];
689 BufferParams const & bp = buffer().params();
690 for (row_type i = 0; i < rowCount(); ++i) {
691 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(buffer()));
693 // care about multicolumns
694 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
695 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
697 if (column + 2 >= ncols
698 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
699 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
702 for (row_type i = 0; i < rowCount(); ++i) {
703 cell_info[i][column + 1].inset->clear();
705 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
711 void Tabular::deleteColumn(col_type const column)
713 // Not allowed to delete last column
714 if (columnCount() == 1)
717 column_info.erase(column_info.begin() + column);
718 for (row_type i = 0; i < rowCount(); ++i) {
719 // Care about multicolumn cells
720 if (column + 1 < columnCount() &&
721 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
722 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
723 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
725 cell_info[i].erase(cell_info[i].begin() + column);
731 void Tabular::copyColumn(col_type const column)
733 BufferParams const & bp = buffer().params();
734 column_info.insert(column_info.begin() + column, column_info[column]);
736 for (row_type i = 0; i < rowCount(); ++i)
737 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
740 for (row_type i = 0; i < rowCount(); ++i)
741 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
746 void Tabular::updateIndexes()
749 // Count only non-multicol cells plus begin multicol
750 // cells, ignore non-begin multicol cells:
751 for (row_type row = 0; row < rowCount(); ++row) {
752 for (col_type column = 0; column < columnCount(); ++column) {
753 if (cell_info[row][column].multicolumn
754 != Tabular::CELL_PART_OF_MULTICOLUMN)
756 BOOST_ASSERT(numberofcells != 0);
757 cell_info[row][column].cellno =
762 rowofcell.resize(numberofcells);
763 columnofcell.resize(numberofcells);
768 c < numberofcells && row < rowCount() && column < columnCount();) {
770 columnofcell[c] = column;
774 } while (column < columnCount() &&
775 cell_info[row][column].multicolumn
776 == Tabular::CELL_PART_OF_MULTICOLUMN);
778 if (column == columnCount()) {
784 for (row_type row = 0; row < rowCount(); ++row) {
785 for (col_type column = 0; column < columnCount(); ++column) {
786 if (isPartOfMultiColumn(row,column))
788 cell_info[row][column].inset->setAutoBreakRows(
789 !getPWidth(cellIndex(row, column)).zero());
795 Tabular::idx_type Tabular::cellCount() const
797 return numberofcells;
801 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
803 row_type const row = cellRow(cell);
805 for (col_type i = 0; i < columnCount(); ++i)
806 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
812 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
814 if (!wholerow && isMultiColumn(cell) &&
815 !(use_booktabs && cellRow(cell) == 0))
816 return cellinfo_of_cell(cell).top_line;
817 return row_info[cellRow(cell)].top_line;
821 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
823 if (!wholerow && isMultiColumn(cell) &&
824 !(use_booktabs && isLastRow(cell)))
825 return cellinfo_of_cell(cell).bottom_line;
826 return row_info[cellRow(cell)].bottom_line;
830 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
834 if (!wholecolumn && isMultiColumn(cell) &&
835 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
836 return cellinfo_of_cell(cell).left_line;
837 return column_info[cellColumn(cell)].left_line;
841 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
845 if (!wholecolumn && isMultiColumn(cell) &&
846 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
847 return cellinfo_of_cell(cell).right_line;
848 return column_info[cellRightColumn(cell)].right_line;
852 bool Tabular::topAlreadyDrawn(idx_type cell) const
854 row_type row = cellRow(cell);
855 if (row > 0 && !getAdditionalHeight(row)) {
856 col_type column = cellColumn(cell);
859 && cell_info[row][column].multicolumn
860 == Tabular::CELL_PART_OF_MULTICOLUMN)
862 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
863 return row_info[row].bottom_line;
865 return cell_info[row][column].bottom_line;
871 bool Tabular::leftAlreadyDrawn(idx_type cell) const
873 col_type column = cellColumn(cell);
875 row_type row = cellRow(cell);
877 (cell_info[row][column].multicolumn ==
878 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
879 if (getAdditionalWidth(cell_info[row][column].cellno))
881 return rightLine(cell_info[row][column].cellno);
887 bool Tabular::isLastRow(idx_type cell) const
889 return cellRow(cell) == rowCount() - 1;
893 int Tabular::getAdditionalHeight(row_type row) const
895 if (!row || row >= rowCount())
901 for (col_type column = 0; column < columnCount() && bottom; ++column) {
902 switch (cell_info[row - 1][column].multicolumn) {
903 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
904 bottom = cell_info[row - 1][column].bottom_line;
906 case Tabular::CELL_NORMAL:
907 bottom = row_info[row - 1].bottom_line;
910 for (col_type column = 0; column < columnCount() && top; ++column) {
911 switch (cell_info[row][column].multicolumn) {
912 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
913 top = cell_info[row][column].top_line;
915 case Tabular::CELL_NORMAL:
916 top = row_info[row].top_line;
919 int const interline_space = row_info[row - 1].interline_space_default ?
921 row_info[row - 1].interline_space.inPixels(width());
923 return interline_space + WIDTH_OF_LINE;
924 return interline_space;
928 int Tabular::getAdditionalWidth(idx_type cell) const
930 // internally already set in setCellWidth
931 // used to get it back in text.cpp
932 col_type const col = cellRightColumn(cell);
933 row_type const row = cellRow(cell);
934 if (col < columnCount() - 1 && rightLine(cell) &&
935 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
937 return WIDTH_OF_LINE;
943 // returns the maximum over all rows
944 int Tabular::columnWidth(idx_type cell) const
946 col_type const column1 = cellColumn(cell);
947 col_type const column2 = cellRightColumn(cell);
949 for (col_type i = column1; i <= column2; ++i)
950 result += column_info[i].width;
955 int Tabular::width() const
958 for (col_type i = 0; i < columnCount(); ++i)
959 width += column_info[i].width;
964 // returns true if a complete update is necessary, otherwise false
965 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
967 if (!isMultiColumn(cell))
970 row_type const row = cellRow(cell);
971 col_type const column1 = cellColumn(cell);
972 col_type const column2 = cellRightColumn(cell);
973 int const old_val = cell_info[row][column2].width;
975 // first set columns to 0 so we can calculate the right width
976 for (col_type i = column1; i <= column2; ++i) {
977 cell_info[row][i].width = 0;
979 // set the width to MAX_WIDTH until width > 0
980 int width = new_width + 2 * WIDTH_OF_LINE;
981 col_type i = column1;
982 for (; i < column2 && width > column_info[i].width; ++i) {
983 cell_info[row][i].width = column_info[i].width;
984 width -= column_info[i].width;
987 cell_info[row][i].width = width;
989 if (old_val != cell_info[row][column2].width) {
990 // in this case we have to recalculate all multicolumn cells which
991 // have this column as one of theirs but not as last one
992 calculate_width_of_column_NMC(i);
993 recalculateMulticolumnsOfColumn(i);
994 calculate_width_of_column(i);
1000 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1002 // the last column does not have to be recalculated because all
1003 // multicolumns will have here there last multicolumn cell which
1004 // always will have the whole rest of the width of the cell.
1005 if (columnCount() < 2 || column > (columnCount() - 2))
1007 for (row_type row = 0; row < rowCount(); ++row) {
1008 int mc = cell_info[row][column].multicolumn;
1009 int nmc = cell_info[row][column+1].multicolumn;
1010 // we only have to update multicolumns which do not have this
1011 // column as their last column!
1012 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1013 (mc == CELL_PART_OF_MULTICOLUMN &&
1014 nmc == CELL_PART_OF_MULTICOLUMN))
1016 idx_type const cellno = cell_info[row][column].cellno;
1017 setWidthOfMulticolCell(cellno,
1018 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1024 void Tabular::setCellWidth(idx_type cell, int new_width)
1026 row_type const row = cellRow(cell);
1027 col_type const column1 = cellColumn(cell);
1032 if (rightLine(cell_info[row][column1].cellno, true) &&
1033 column1 < columnCount() - 1 &&
1034 leftLine(cell_info[row][column1+1].cellno, true))
1036 add_width = WIDTH_OF_LINE;
1039 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1042 if (isMultiColumnReal(cell)) {
1043 tmp = setWidthOfMulticolCell(cell, new_width);
1045 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1046 cell_info[row][column1].width = width;
1047 tmp = calculate_width_of_column_NMC(column1);
1049 recalculateMulticolumnsOfColumn(column1);
1052 for (col_type i = 0; i < columnCount(); ++i)
1053 calculate_width_of_column(i);
1058 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1061 if (!isMultiColumn(cell) || onlycolumn)
1062 column_info[cellColumn(cell)].alignment = align;
1064 cellinfo_of_cell(cell).alignment = align;
1068 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1071 if (!isMultiColumn(cell) || onlycolumn)
1072 column_info[cellColumn(cell)].valignment = align;
1074 cellinfo_of_cell(cell).valignment = align;
1081 * Allow line and paragraph breaks for fixed width cells or disallow them,
1082 * merge cell paragraphs and reset layout to standard for variable width
1085 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1087 inset->setAutoBreakRows(fixedWidth);
1091 // merge all paragraphs to one
1092 BufferParams const & bp = cur.bv().buffer().params();
1093 while (inset->paragraphs().size() > 1)
1094 mergeParagraph(bp, inset->paragraphs(), 0);
1098 // undo information has already been recorded
1099 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1100 bp.textClass().emptyLayoutName());
1107 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1108 Length const & width)
1110 col_type const j = cellColumn(cell);
1112 column_info[j].p_width = width;
1113 for (row_type i = 0; i < rowCount(); ++i) {
1114 idx_type const cell = cellIndex(i, j);
1115 // because of multicolumns
1116 toggleFixedWidth(cur, getCellInset(cell).get(),
1117 !getPWidth(cell).zero());
1119 // cur paragraph can become invalid after paragraphs were merged
1120 if (cur.pit() > cur.lastpit())
1121 cur.pit() = cur.lastpit();
1122 // cur position can become invalid after newlines were removed
1123 if (cur.pos() > cur.lastpos())
1124 cur.pos() = cur.lastpos();
1128 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1129 Length const & width)
1131 if (!isMultiColumn(cell))
1134 cellinfo_of_cell(cell).p_width = width;
1135 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1136 // cur paragraph can become invalid after paragraphs were merged
1137 if (cur.pit() > cur.lastpit())
1138 cur.pit() = cur.lastpit();
1139 // cur position can become invalid after newlines were removed
1140 if (cur.pos() > cur.lastpos())
1141 cur.pos() = cur.lastpos();
1146 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1147 Tabular::Feature what)
1149 if (what == SET_SPECIAL_MULTI)
1150 cellinfo_of_cell(cell).align_special = special;
1152 column_info[cellColumn(cell)].align_special = special;
1156 void Tabular::setAllLines(idx_type cell, bool line)
1158 setTopLine(cell, line);
1159 setBottomLine(cell, line);
1160 setRightLine(cell, line);
1161 setLeftLine(cell, line);
1165 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1167 row_type const row = cellRow(cell);
1168 if (wholerow || !isMultiColumn(cell))
1169 row_info[row].top_line = line;
1171 cellinfo_of_cell(cell).top_line = line;
1175 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1177 if (wholerow || !isMultiColumn(cell))
1178 row_info[cellRow(cell)].bottom_line = line;
1180 cellinfo_of_cell(cell).bottom_line = line;
1184 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1186 if (wholecolumn || !isMultiColumn(cell))
1187 column_info[cellColumn(cell)].left_line = line;
1189 cellinfo_of_cell(cell).left_line = line;
1193 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1195 if (wholecolumn || !isMultiColumn(cell))
1196 column_info[cellRightColumn(cell)].right_line = line;
1198 cellinfo_of_cell(cell).right_line = line;
1202 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1204 if (!onlycolumn && isMultiColumn(cell))
1205 return cellinfo_of_cell(cell).alignment;
1206 return column_info[cellColumn(cell)].alignment;
1211 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1213 if (!onlycolumn && isMultiColumn(cell))
1214 return cellinfo_of_cell(cell).valignment;
1215 return column_info[cellColumn(cell)].valignment;
1219 Length const Tabular::getPWidth(idx_type cell) const
1221 if (isMultiColumn(cell))
1222 return cellinfo_of_cell(cell).p_width;
1223 return column_info[cellColumn(cell)].p_width;
1227 Length const Tabular::getColumnPWidth(idx_type cell) const
1229 return column_info[cellColumn(cell)].p_width;
1233 Length const Tabular::getMColumnPWidth(idx_type cell) const
1235 if (isMultiColumn(cell))
1236 return cellinfo_of_cell(cell).p_width;
1241 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1243 if (what == SET_SPECIAL_MULTI)
1244 return cellinfo_of_cell(cell).align_special;
1245 return column_info[cellColumn(cell)].align_special;
1249 int Tabular::cellWidth(idx_type cell) const
1251 row_type const row = cellRow(cell);
1252 col_type const column1 = cellColumn(cell);
1253 col_type const column2 = cellRightColumn(cell);
1255 for (col_type i = column1; i <= column2; ++i)
1256 result += cell_info[row][i].width;
1261 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1265 switch (getAlignment(cell)) {
1266 case LYX_ALIGN_CENTER:
1267 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1269 case LYX_ALIGN_RIGHT:
1270 x += columnWidth(cell) - cellWidth(cell);
1271 // + getAdditionalWidth(cell);
1274 // LYX_ALIGN_LEFT: nothing :-)
1278 // the LaTeX Way :-(
1284 bool Tabular::isFirstCellInRow(idx_type cell) const
1286 return cellColumn(cell) == 0;
1290 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1292 if (row > rowCount() - 1)
1293 row = rowCount() - 1;
1294 return cell_info[row][0].cellno;
1298 bool Tabular::isLastCellInRow(idx_type cell) const
1300 return cellRightColumn(cell) == columnCount() - 1;
1304 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1306 if (row > rowCount() - 1)
1307 row = rowCount() - 1;
1308 return cell_info[row][columnCount() - 1].cellno;
1312 void Tabular::calculate_width_of_column(col_type column)
1315 for (row_type i = 0; i < rowCount(); ++i)
1316 maximum = max(cell_info[i][column].width, maximum);
1317 column_info[column].width = maximum;
1322 // Calculate the columns regarding ONLY the normal cells and if this
1323 // column is inside a multicolumn cell then use it only if its the last
1324 // column of this multicolumn cell as this gives an added width to the
1325 // column, all the rest should be adapted!
1327 bool Tabular::calculate_width_of_column_NMC(col_type column)
1329 int const old_column_width = column_info[column].width;
1331 for (row_type i = 0; i < rowCount(); ++i) {
1332 idx_type cell = cellIndex(i, column);
1333 bool ismulti = isMultiColumnReal(cell);
1334 if ((!ismulti || column == cellRightColumn(cell)) &&
1335 cell_info[i][column].width > max)
1337 max = cell_info[i][column].width;
1340 column_info[column].width = max;
1341 return column_info[column].width != old_column_width;
1345 Tabular::row_type Tabular::cellRow(idx_type cell) const
1347 if (cell >= cellCount())
1348 return rowCount() - 1;
1351 return rowofcell[cell];
1355 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1357 if (cell >= cellCount())
1358 return columnCount() - 1;
1361 return columnofcell[cell];
1365 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1367 row_type const row = cellRow(cell);
1368 col_type column = cellColumn(cell);
1369 while (column < columnCount() - 1 &&
1370 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1376 void Tabular::write(ostream & os) const
1380 << write_attribute("version", 3)
1381 << write_attribute("rows", rowCount())
1382 << write_attribute("columns", columnCount())
1384 // global longtable options
1386 << write_attribute("rotate", rotate)
1387 << write_attribute("booktabs", use_booktabs)
1388 << write_attribute("islongtable", is_long_tabular)
1389 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1390 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1391 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1392 << write_attribute("headTopDL", endhead.topDL)
1393 << write_attribute("headBottomDL", endhead.bottomDL)
1394 << write_attribute("footTopDL", endfoot.topDL)
1395 << write_attribute("footBottomDL", endfoot.bottomDL)
1396 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1397 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1398 << write_attribute("lastFootEmpty", endlastfoot.empty)
1400 for (col_type j = 0; j < columnCount(); ++j) {
1402 << write_attribute("alignment", column_info[j].alignment)
1403 << write_attribute("valignment", column_info[j].valignment)
1404 << write_attribute("leftline", column_info[j].left_line)
1405 << write_attribute("rightline", column_info[j].right_line)
1406 << write_attribute("width", column_info[j].p_width.asString())
1407 << write_attribute("special", column_info[j].align_special)
1410 for (row_type i = 0; i < rowCount(); ++i) {
1412 << write_attribute("topline", row_info[i].top_line)
1413 << write_attribute("bottomline", row_info[i].bottom_line);
1414 static const string def("default");
1415 if (row_info[i].top_space_default)
1416 os << write_attribute("topspace", def);
1418 os << write_attribute("topspace", row_info[i].top_space);
1419 if (row_info[i].bottom_space_default)
1420 os << write_attribute("bottomspace", def);
1422 os << write_attribute("bottomspace", row_info[i].bottom_space);
1423 if (row_info[i].interline_space_default)
1424 os << write_attribute("interlinespace", def);
1426 os << write_attribute("interlinespace", row_info[i].interline_space);
1427 os << write_attribute("endhead", row_info[i].endhead)
1428 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1429 << write_attribute("endfoot", row_info[i].endfoot)
1430 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1431 << write_attribute("newpage", row_info[i].newpage)
1433 for (col_type j = 0; j < columnCount(); ++j) {
1435 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1436 << write_attribute("alignment", cell_info[i][j].alignment)
1437 << write_attribute("valignment", cell_info[i][j].valignment)
1438 << write_attribute("topline", cell_info[i][j].top_line)
1439 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1440 << write_attribute("leftline", cell_info[i][j].left_line)
1441 << write_attribute("rightline", cell_info[i][j].right_line)
1442 << write_attribute("rotate", cell_info[i][j].rotate)
1443 << write_attribute("usebox", cell_info[i][j].usebox)
1444 << write_attribute("width", cell_info[i][j].p_width)
1445 << write_attribute("special", cell_info[i][j].align_special)
1447 os << "\\begin_inset ";
1448 cell_info[i][j].inset->write(os);
1449 os << "\n\\end_inset\n"
1454 os << "</lyxtabular>\n";
1458 void Tabular::read(Lexer & lex)
1461 istream & is = lex.getStream();
1463 l_getline(is, line);
1464 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1465 BOOST_ASSERT(false);
1470 if (!getTokenValue(line, "version", version))
1472 BOOST_ASSERT(version >= 2);
1475 if (!getTokenValue(line, "rows", rows_arg))
1478 if (!getTokenValue(line, "columns", columns_arg))
1480 init(buffer(), rows_arg, columns_arg);
1481 l_getline(is, line);
1482 if (!prefixIs(line, "<features")) {
1483 lyxerr << "Wrong tabular format (expected <features ...> got"
1484 << line << ')' << endl;
1487 getTokenValue(line, "rotate", rotate);
1488 getTokenValue(line, "booktabs", use_booktabs);
1489 getTokenValue(line, "islongtable", is_long_tabular);
1490 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1491 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1492 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1493 getTokenValue(line, "headTopDL", endhead.topDL);
1494 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1495 getTokenValue(line, "footTopDL", endfoot.topDL);
1496 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1497 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1498 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1499 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1501 for (col_type j = 0; j < columnCount(); ++j) {
1503 if (!prefixIs(line,"<column")) {
1504 lyxerr << "Wrong tabular format (expected <column ...> got"
1505 << line << ')' << endl;
1508 getTokenValue(line, "alignment", column_info[j].alignment);
1509 getTokenValue(line, "valignment", column_info[j].valignment);
1510 getTokenValue(line, "leftline", column_info[j].left_line);
1511 getTokenValue(line, "rightline", column_info[j].right_line);
1512 getTokenValue(line, "width", column_info[j].p_width);
1513 getTokenValue(line, "special", column_info[j].align_special);
1516 for (row_type i = 0; i < rowCount(); ++i) {
1517 l_getline(is, line);
1518 if (!prefixIs(line, "<row")) {
1519 lyxerr << "Wrong tabular format (expected <row ...> got"
1520 << line << ')' << endl;
1523 getTokenValue(line, "topline", row_info[i].top_line);
1524 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1525 getTokenValue(line, "topspace", row_info[i].top_space,
1526 row_info[i].top_space_default);
1527 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1528 row_info[i].bottom_space_default);
1529 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1530 row_info[i].interline_space_default);
1531 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1532 getTokenValue(line, "endhead", row_info[i].endhead);
1533 getTokenValue(line, "endfoot", row_info[i].endfoot);
1534 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1535 getTokenValue(line, "newpage", row_info[i].newpage);
1536 for (col_type j = 0; j < columnCount(); ++j) {
1537 l_getline(is, line);
1538 if (!prefixIs(line, "<cell")) {
1539 lyxerr << "Wrong tabular format (expected <cell ...> got"
1540 << line << ')' << endl;
1543 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1544 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1545 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1546 getTokenValue(line, "topline", cell_info[i][j].top_line);
1547 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1548 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1549 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1550 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1551 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1552 getTokenValue(line, "width", cell_info[i][j].p_width);
1553 getTokenValue(line, "special", cell_info[i][j].align_special);
1554 l_getline(is, line);
1555 if (prefixIs(line, "\\begin_inset")) {
1556 cell_info[i][j].inset->read(lex);
1557 l_getline(is, line);
1559 if (!prefixIs(line, "</cell>")) {
1560 lyxerr << "Wrong tabular format (expected </cell> got"
1561 << line << ')' << endl;
1565 l_getline(is, line);
1566 if (!prefixIs(line, "</row>")) {
1567 lyxerr << "Wrong tabular format (expected </row> got"
1568 << line << ')' << endl;
1572 while (!prefixIs(line, "</lyxtabular>")) {
1573 l_getline(is, line);
1579 bool Tabular::isMultiColumn(idx_type cell) const
1581 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1585 bool Tabular::isMultiColumnReal(idx_type cell) const
1587 return cellColumn(cell) != cellRightColumn(cell) &&
1588 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1592 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1594 return cell_info[cellRow(cell)][cellColumn(cell)];
1598 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1600 cellstruct & cs = cellinfo_of_cell(cell);
1601 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1602 cs.alignment = column_info[cellColumn(cell)].alignment;
1603 cs.top_line = row_info[cellRow(cell)].top_line;
1604 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1605 cs.left_line = column_info[cellColumn(cell)].left_line;
1606 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1607 for (idx_type i = 1; i < number; ++i) {
1608 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1609 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1610 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1617 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1619 row_type const row = cellRow(cell);
1620 col_type column = cellColumn(cell);
1621 idx_type result = 1;
1623 while (column < columnCount() &&
1624 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1633 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1635 row_type const row = cellRow(cell);
1636 col_type column = cellColumn(cell);
1638 idx_type result = 0;
1640 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1641 cell_info[row][column].multicolumn = CELL_NORMAL;
1643 while (column < columnCount() &&
1644 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1646 cell_info[row][column].multicolumn = CELL_NORMAL;
1656 void Tabular::setBookTabs(bool what)
1658 use_booktabs = what;
1662 bool Tabular::useBookTabs() const
1664 return use_booktabs;
1668 void Tabular::setLongTabular(bool what)
1670 is_long_tabular = what;
1674 bool Tabular::isLongTabular() const
1676 return is_long_tabular;
1680 void Tabular::setRotateTabular(bool flag)
1686 bool Tabular::getRotateTabular() const
1692 void Tabular::setRotateCell(idx_type cell, bool flag)
1694 cellinfo_of_cell(cell).rotate = flag;
1698 bool Tabular::getRotateCell(idx_type cell) const
1700 return cellinfo_of_cell(cell).rotate;
1704 bool Tabular::needRotating() const
1708 for (row_type i = 0; i < rowCount(); ++i)
1709 for (col_type j = 0; j < columnCount(); ++j)
1710 if (cell_info[i][j].rotate)
1716 bool Tabular::isLastCell(idx_type cell) const
1718 if (cell + 1 < cellCount())
1724 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1726 if (cellRow(cell) > 0)
1727 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1732 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1734 if (cellRow(cell) + 1 < rowCount())
1735 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1740 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1742 if (cellRow(cell) == 0)
1744 if (!isMultiColumn(cell))
1745 return getCellAbove(cell);
1746 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1750 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1752 if (cellRow(cell) + 1 >= rowCount())
1754 if (!isMultiColumn(cell))
1755 return getCellBelow(cell);
1756 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1760 Tabular::idx_type Tabular::cellIndex(row_type row,
1761 col_type column) const
1763 BOOST_ASSERT(column != npos && column < columnCount() &&
1764 row != npos && row < rowCount());
1765 return cell_info[row][column].cellno;
1769 void Tabular::setUsebox(idx_type cell, BoxType type)
1771 cellinfo_of_cell(cell).usebox = type;
1775 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1777 if (column_info[cellColumn(cell)].p_width.zero() &&
1778 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1780 if (cellinfo_of_cell(cell).usebox > 1)
1781 return cellinfo_of_cell(cell).usebox;
1782 return useParbox(cell);
1787 // This are functions used for the longtable support
1789 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1795 row_info[row].endfirsthead = flag;
1799 row_info[row].endhead = flag;
1804 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1807 hd.set = haveLTHead();
1808 return row_info[row].endhead;
1812 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1815 hd.set = haveLTFirstHead();
1816 return row_info[row].endfirsthead;
1820 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1826 row_info[row].endlastfoot = flag;
1830 row_info[row].endfoot = flag;
1835 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1838 fd.set = haveLTFoot();
1839 return row_info[row].endfoot;
1843 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1846 fd.set = haveLTLastFoot();
1847 return row_info[row].endlastfoot;
1851 void Tabular::setLTNewPage(row_type row, bool what)
1853 row_info[row].newpage = what;
1857 bool Tabular::getLTNewPage(row_type row) const
1859 return row_info[row].newpage;
1863 bool Tabular::haveLTHead() const
1865 for (row_type i = 0; i < rowCount(); ++i)
1866 if (row_info[i].endhead)
1872 bool Tabular::haveLTFirstHead() const
1874 if (endfirsthead.empty)
1876 for (row_type i = 0; i < rowCount(); ++i)
1877 if (row_info[i].endfirsthead)
1883 bool Tabular::haveLTFoot() const
1885 for (row_type i = 0; i < rowCount(); ++i)
1886 if (row_info[i].endfoot)
1892 bool Tabular::haveLTLastFoot() const
1894 if (endlastfoot.empty)
1896 for (row_type i = 0; i < rowCount(); ++i)
1897 if (row_info[i].endlastfoot)
1903 // end longtable support functions
1905 void Tabular::setRowAscent(row_type row, int height)
1907 if (row >= rowCount() || row_info[row].ascent == height)
1909 row_info[row].ascent = height;
1913 void Tabular::setRowDescent(row_type row, int height)
1915 if (row >= rowCount() || row_info[row].descent == height)
1917 row_info[row].descent = height;
1921 int Tabular::rowAscent(row_type row) const
1923 if (row >= rowCount())
1925 return row_info[row].ascent;
1929 int Tabular::rowDescent(row_type row) const
1931 BOOST_ASSERT(row < rowCount());
1932 return row_info[row].descent;
1936 int Tabular::height() const
1939 for (row_type row = 0; row < rowCount(); ++row)
1940 height += rowAscent(row) + rowDescent(row) +
1941 getAdditionalHeight(row);
1946 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1948 BOOST_ASSERT(row < rowCount());
1949 BOOST_ASSERT(column < columnCount());
1950 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1954 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1956 // FIXME: assert or return 0 as in TeXBottomHLine()?
1957 BOOST_ASSERT(row != npos);
1958 BOOST_ASSERT(row < rowCount());
1960 idx_type const fcell = getFirstCellInRow(row);
1961 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1964 for (idx_type i = fcell; i < n; ++i) {
1968 if (use_booktabs && row == 0) {
1971 } else if (tmp == n - fcell) {
1972 os << (use_booktabs ? "\\midrule " : "\\hline ");
1974 for (idx_type i = fcell; i < n; ++i) {
1976 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1977 << cellColumn(i) + 1
1979 << cellRightColumn(i) + 1
1991 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1993 // FIXME: return 0 or assert as in TeXTopHLine()?
1994 if (row == npos || row >= rowCount())
1997 idx_type const fcell = getFirstCellInRow(row);
1998 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2001 for (idx_type i = fcell; i < n; ++i) {
2005 if (use_booktabs && row == rowCount() - 1) {
2006 if (bottomLine(fcell))
2007 os << "\\bottomrule";
2008 } else if (tmp == n - fcell) {
2009 os << (use_booktabs ? "\\midrule" : "\\hline");
2011 for (idx_type i = fcell; i < n; ++i) {
2012 if (bottomLine(i)) {
2013 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2014 << cellColumn(i) + 1
2016 << cellRightColumn(i) + 1
2028 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2032 if (getRotateCell(cell)) {
2033 os << "\\begin{sideways}\n";
2036 if (isMultiColumn(cell)) {
2037 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2038 if (leftLine(cell) &&
2039 (isFirstCellInRow(cell) ||
2040 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2041 !rightLine(cell - 1, true))))
2043 if (!cellinfo_of_cell(cell).align_special.empty()) {
2044 os << cellinfo_of_cell(cell).align_special;
2046 if (!getPWidth(cell).zero()) {
2047 switch (getVAlignment(cell)) {
2048 case LYX_VALIGN_TOP:
2051 case LYX_VALIGN_MIDDLE:
2054 case LYX_VALIGN_BOTTOM:
2059 << from_ascii(getPWidth(cell).asLatexString())
2062 switch (getAlignment(cell)) {
2063 case LYX_ALIGN_LEFT:
2066 case LYX_ALIGN_RIGHT:
2073 } // end if else !getPWidth
2074 } // end if else !cellinfo_of_cell
2075 if (rightLine(cell))
2077 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2082 if (getUsebox(cell) == BOX_PARBOX) {
2084 switch (getVAlignment(cell)) {
2085 case LYX_VALIGN_TOP:
2088 case LYX_VALIGN_MIDDLE:
2091 case LYX_VALIGN_BOTTOM:
2095 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2097 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2098 os << "\\begin{minipage}[";
2099 switch (getVAlignment(cell)) {
2100 case LYX_VALIGN_TOP:
2103 case LYX_VALIGN_MIDDLE:
2106 case LYX_VALIGN_BOTTOM:
2110 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2118 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2123 if (getUsebox(cell) == BOX_PARBOX)
2125 else if (getUsebox(cell) == BOX_MINIPAGE) {
2126 os << "%\n\\end{minipage}";
2129 if (isMultiColumn(cell)) {
2132 if (getRotateCell(cell)) {
2133 os << "%\n\\end{sideways}";
2140 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2141 OutputParams const & runparams) const
2143 if (!is_long_tabular)
2147 // output header info
2149 if (endhead.topDL) {
2153 for (row_type i = 0; i < rowCount(); ++i) {
2154 if (row_info[i].endhead) {
2155 ret += TeXRow(os, i, runparams);
2158 if (endhead.bottomDL) {
2162 os << "\\endhead\n";
2164 if (endfirsthead.empty) {
2165 os << "\\endfirsthead\n";
2169 // output firstheader info
2170 if (haveLTFirstHead()) {
2171 if (endfirsthead.topDL) {
2175 for (row_type i = 0; i < rowCount(); ++i) {
2176 if (row_info[i].endfirsthead) {
2177 ret += TeXRow(os, i, runparams);
2180 if (endfirsthead.bottomDL) {
2184 os << "\\endfirsthead\n";
2187 // output footer info
2189 if (endfoot.topDL) {
2193 for (row_type i = 0; i < rowCount(); ++i) {
2194 if (row_info[i].endfoot) {
2195 ret += TeXRow(os, i, runparams);
2198 if (endfoot.bottomDL) {
2202 os << "\\endfoot\n";
2204 if (endlastfoot.empty) {
2205 os << "\\endlastfoot\n";
2209 // output lastfooter info
2210 if (haveLTLastFoot()) {
2211 if (endlastfoot.topDL) {
2215 for (row_type i = 0; i < rowCount(); ++i) {
2216 if (row_info[i].endlastfoot) {
2217 ret += TeXRow(os, i, runparams);
2220 if (endlastfoot.bottomDL) {
2224 os << "\\endlastfoot\n";
2231 bool Tabular::isValidRow(row_type row) const
2233 if (!is_long_tabular)
2235 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2236 !row_info[row].endfoot && !row_info[row].endlastfoot;
2240 int Tabular::TeXRow(odocstream & os, row_type i,
2241 OutputParams const & runparams) const
2243 idx_type cell = cellIndex(i, 0);
2244 int ret = TeXTopHLine(os, i);
2245 if (row_info[i].top_space_default) {
2247 os << "\\addlinespace\n";
2249 os << "\\noalign{\\vskip\\doublerulesep}\n";
2251 } else if(!row_info[i].top_space.zero()) {
2253 os << "\\addlinespace["
2254 << from_ascii(row_info[i].top_space.asLatexString())
2257 os << "\\noalign{\\vskip"
2258 << from_ascii(row_info[i].top_space.asLatexString())
2264 for (col_type j = 0; j < columnCount(); ++j) {
2265 if (isPartOfMultiColumn(i, j))
2267 ret += TeXCellPreamble(os, cell);
2268 shared_ptr<InsetText> inset = getCellInset(cell);
2270 Paragraph const & par = inset->paragraphs().front();
2271 bool rtl = par.isRTL(buffer().params())
2273 && getPWidth(cell).zero();
2276 if (par.getParLanguage(buffer().params())->lang() ==
2279 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2281 // currently, remaning RTL languages are arabic_arabtex and hebrew
2285 ret += inset->latex(os, runparams);
2289 ret += TeXCellPostamble(os, cell);
2290 if (!isLastCellInRow(cell)) { // not last cell in row
2295 os << "\\tabularnewline";
2296 if (row_info[i].bottom_space_default) {
2298 os << "\\addlinespace";
2300 os << "[\\doublerulesep]";
2301 } else if (!row_info[i].bottom_space.zero()) {
2303 os << "\\addlinespace";
2305 << from_ascii(row_info[i].bottom_space.asLatexString())
2310 ret += TeXBottomHLine(os, i);
2311 if (row_info[i].interline_space_default) {
2313 os << "\\addlinespace\n";
2315 os << "\\noalign{\\vskip\\doublerulesep}\n";
2317 } else if (!row_info[i].interline_space.zero()) {
2319 os << "\\addlinespace["
2320 << from_ascii(row_info[i].interline_space.asLatexString())
2323 os << "\\noalign{\\vskip"
2324 << from_ascii(row_info[i].interline_space.asLatexString())
2332 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2336 //+---------------------------------------------------------------------
2337 //+ first the opening preamble +
2338 //+---------------------------------------------------------------------
2341 os << "\\begin{sideways}\n";
2344 if (is_long_tabular)
2345 os << "\\begin{longtable}{";
2347 os << "\\begin{tabular}{";
2348 for (col_type i = 0; i < columnCount(); ++i) {
2349 if (!use_booktabs && column_info[i].left_line)
2351 if (!column_info[i].align_special.empty()) {
2352 os << column_info[i].align_special;
2354 if (!column_info[i].p_width.zero()) {
2355 switch (column_info[i].alignment) {
2356 case LYX_ALIGN_LEFT:
2357 os << ">{\\raggedright}";
2359 case LYX_ALIGN_RIGHT:
2360 os << ">{\\raggedleft}";
2362 case LYX_ALIGN_CENTER:
2363 os << ">{\\centering}";
2365 case LYX_ALIGN_NONE:
2366 case LYX_ALIGN_BLOCK:
2367 case LYX_ALIGN_LAYOUT:
2368 case LYX_ALIGN_SPECIAL:
2372 switch (column_info[i].valignment) {
2373 case LYX_VALIGN_TOP:
2376 case LYX_VALIGN_MIDDLE:
2379 case LYX_VALIGN_BOTTOM:
2384 << from_ascii(column_info[i].p_width.asLatexString())
2387 switch (column_info[i].alignment) {
2388 case LYX_ALIGN_LEFT:
2391 case LYX_ALIGN_RIGHT:
2398 } // end if else !column_info[i].p_width
2399 } // end if else !column_info[i].align_special
2400 if (!use_booktabs && column_info[i].right_line)
2406 ret += TeXLongtableHeaderFooter(os, runparams);
2408 //+---------------------------------------------------------------------
2409 //+ the single row and columns (cells) +
2410 //+---------------------------------------------------------------------
2412 for (row_type i = 0; i < rowCount(); ++i) {
2413 if (isValidRow(i)) {
2414 ret += TeXRow(os, i, runparams);
2415 if (is_long_tabular && row_info[i].newpage) {
2416 os << "\\newpage\n";
2422 //+---------------------------------------------------------------------
2423 //+ the closing of the tabular +
2424 //+---------------------------------------------------------------------
2426 if (is_long_tabular)
2427 os << "\\end{longtable}";
2429 os << "\\end{tabular}";
2431 os << "\n\\end{sideways}";
2439 int Tabular::docbookRow(odocstream & os, row_type row,
2440 OutputParams const & runparams) const
2443 idx_type cell = getFirstCellInRow(row);
2446 for (col_type j = 0; j < columnCount(); ++j) {
2447 if (isPartOfMultiColumn(row, j))
2450 os << "<entry align=\"";
2451 switch (getAlignment(cell)) {
2452 case LYX_ALIGN_LEFT:
2455 case LYX_ALIGN_RIGHT:
2463 os << "\" valign=\"";
2464 switch (getVAlignment(cell)) {
2465 case LYX_VALIGN_TOP:
2468 case LYX_VALIGN_BOTTOM:
2471 case LYX_VALIGN_MIDDLE:
2476 if (isMultiColumn(cell)) {
2477 os << " namest=\"col" << j << "\" ";
2478 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2482 ret += getCellInset(cell)->docbook(os, runparams);
2491 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2495 //+---------------------------------------------------------------------
2496 //+ first the opening preamble +
2497 //+---------------------------------------------------------------------
2499 os << "<tgroup cols=\"" << columnCount()
2500 << "\" colsep=\"1\" rowsep=\"1\">\n";
2502 for (col_type i = 0; i < columnCount(); ++i) {
2503 os << "<colspec colname=\"col" << i << "\" align=\"";
2504 switch (column_info[i].alignment) {
2505 case LYX_ALIGN_LEFT:
2508 case LYX_ALIGN_RIGHT:
2516 if (runparams.flavor == OutputParams::XML)
2522 //+---------------------------------------------------------------------
2523 //+ Long Tabular case +
2524 //+---------------------------------------------------------------------
2526 // output header info
2527 if (haveLTHead() || haveLTFirstHead()) {
2530 for (row_type i = 0; i < rowCount(); ++i) {
2531 if (row_info[i].endhead || row_info[i].endfirsthead) {
2532 ret += docbookRow(os, i, runparams);
2538 // output footer info
2539 if (haveLTFoot() || haveLTLastFoot()) {
2542 for (row_type i = 0; i < rowCount(); ++i) {
2543 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2544 ret += docbookRow(os, i, runparams);
2551 //+---------------------------------------------------------------------
2552 //+ the single row and columns (cells) +
2553 //+---------------------------------------------------------------------
2557 for (row_type i = 0; i < rowCount(); ++i) {
2558 if (isValidRow(i)) {
2559 ret += docbookRow(os, i, runparams);
2564 //+---------------------------------------------------------------------
2565 //+ the closing of the tabular +
2566 //+---------------------------------------------------------------------
2575 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2576 vector<unsigned int> const & clen) const
2578 idx_type const fcell = getFirstCellInRow(row);
2579 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2582 for (idx_type i = fcell; i < n; ++i) {
2592 for (idx_type i = fcell; i < n; ++i) {
2603 col_type column = cellColumn(i);
2604 int len = clen[column];
2605 while (column < columnCount() - 1
2606 && isPartOfMultiColumn(row, ++column))
2607 len += clen[column] + 4;
2608 os << docstring(len, ch);
2623 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2624 vector<unsigned int> const & clen) const
2626 idx_type const fcell = getFirstCellInRow(row);
2627 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2630 for (idx_type i = fcell; i < n; ++i) {
2631 if (bottomLine(i)) {
2640 for (idx_type i = fcell; i < n; ++i) {
2641 if (bottomLine(i)) {
2651 col_type column = cellColumn(i);
2652 int len = clen[column];
2653 while (column < columnCount() -1
2654 && isPartOfMultiColumn(row, ++column))
2655 len += clen[column] + 4;
2656 os << docstring(len, ch);
2657 if (bottomLine(i)) {
2671 void Tabular::plaintextPrintCell(odocstream & os,
2672 OutputParams const & runparams,
2673 idx_type cell, row_type row, col_type column,
2674 vector<unsigned int> const & clen,
2675 bool onlydata) const
2677 odocstringstream sstr;
2678 getCellInset(cell)->plaintext(sstr, runparams);
2690 unsigned int len1 = sstr.str().length();
2691 unsigned int len2 = clen[column];
2692 while (column < columnCount() -1
2693 && isPartOfMultiColumn(row, ++column))
2694 len2 += clen[column] + 4;
2697 switch (getAlignment(cell)) {
2699 case LYX_ALIGN_LEFT:
2702 case LYX_ALIGN_RIGHT:
2706 case LYX_ALIGN_CENTER:
2712 os << docstring(len1, ' ') << sstr.str()
2713 << docstring(len2, ' ');
2715 if (rightLine(cell))
2722 void Tabular::plaintext(odocstream & os,
2723 OutputParams const & runparams, int const depth,
2724 bool onlydata, char_type delim) const
2726 // first calculate the width of the single columns
2727 vector<unsigned int> clen(columnCount());
2730 // first all non (real) multicolumn cells!
2731 for (col_type j = 0; j < columnCount(); ++j) {
2733 for (row_type i = 0; i < rowCount(); ++i) {
2734 idx_type cell = cellIndex(i, j);
2735 if (isMultiColumnReal(cell))
2737 odocstringstream sstr;
2738 getCellInset(cell)->plaintext(sstr, runparams);
2739 if (clen[j] < sstr.str().length())
2740 clen[j] = sstr.str().length();
2743 // then all (real) multicolumn cells!
2744 for (col_type j = 0; j < columnCount(); ++j) {
2745 for (row_type i = 0; i < rowCount(); ++i) {
2746 idx_type cell = cellIndex(i, j);
2747 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2749 odocstringstream sstr;
2750 getCellInset(cell)->plaintext(sstr, runparams);
2751 int len = int(sstr.str().length());
2752 idx_type const n = cells_in_multicolumn(cell);
2753 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2755 if (len > int(clen[j + n - 1]))
2756 clen[j + n - 1] = len;
2761 for (row_type i = 0; i < rowCount(); ++i) {
2762 if (!onlydata && plaintextTopHLine(os, i, clen))
2763 os << docstring(depth * 2, ' ');
2764 for (col_type j = 0; j < columnCount(); ++j) {
2765 if (isPartOfMultiColumn(i, j))
2767 if (onlydata && j > 0)
2768 // we don't use operator<< for single UCS4 character.
2769 // see explanation in docstream.h
2771 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2776 os << docstring(depth * 2, ' ');
2777 if (plaintextBottomHLine(os, i, clen))
2778 os << docstring(depth * 2, ' ');
2784 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2786 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2790 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2791 col_type column) const
2793 return cell_info[row][column].inset;
2797 void Tabular::setCellInset(row_type row, col_type column,
2798 shared_ptr<InsetText> ins) const
2800 cell_info[row][column].inset = ins;
2805 Tabular::getCellFromInset(Inset const * inset) const
2807 // is this inset part of the tabular?
2809 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2810 BOOST_ASSERT(false);
2813 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2814 if (getCellInset(cell).get() == inset) {
2815 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2816 << "cell=" << cell);
2820 // We should have found a cell at this point
2821 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2822 << inset << " not found!" << endl;
2823 BOOST_ASSERT(false);
2829 void Tabular::validate(LaTeXFeatures & features) const
2831 features.require("NeedTabularnewline");
2833 features.require("booktabs");
2834 if (isLongTabular())
2835 features.require("longtable");
2837 features.require("rotating");
2838 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2839 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2840 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2841 features.require("array");
2842 getCellInset(cell)->validate(features);
2847 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2849 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2850 ParagraphList::const_iterator cit = parlist.begin();
2851 ParagraphList::const_iterator end = parlist.end();
2853 for (; cit != end; ++cit)
2854 for (int i = 0; i < cit->size(); ++i)
2855 if (cit->isNewline(i))
2862 /////////////////////////////////////////////////////////////////////
2866 /////////////////////////////////////////////////////////////////////
2868 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2870 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0)
2872 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2876 InsetTabular::InsetTabular(InsetTabular const & tab)
2877 : Inset(tab), tabular(tab.tabular), scx_(0)
2879 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2883 InsetTabular::~InsetTabular()
2885 InsetTabularMailer(*this).hideDialog();
2889 Inset * InsetTabular::clone() const
2891 return new InsetTabular(*this);
2895 bool InsetTabular::insetAllowed(InsetCode code) const
2897 if (code == MATHMACRO_CODE)
2904 void InsetTabular::write(ostream & os) const
2906 os << "Tabular" << endl;
2911 void InsetTabular::read(Lexer & lex)
2913 bool const old_format = (lex.getString() == "\\LyXTable");
2921 string token = lex.getString();
2922 while (lex && token != "\\end_inset") {
2924 token = lex.getString();
2927 lex.printError("Missing \\end_inset at this point. "
2933 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2935 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2936 // mi.base.textwidth << "\n";
2938 lyxerr << "InsetTabular::metrics: need bv" << endl;
2939 BOOST_ASSERT(false);
2943 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2946 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2947 if (tabular.isPartOfMultiColumn(i, j))
2948 // Multicolumn cell, but not first one
2953 if (tabular.cell_info[i][j].multicolumn ==
2954 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2955 p_width = tabular.cellinfo_of_cell(cell).p_width;
2957 p_width = tabular.column_info[j].p_width;
2958 if (!p_width.zero())
2959 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2960 tabular.getCellInset(cell)->metrics(m, dim);
2961 if (!p_width.zero())
2962 dim.wid = m.base.textwidth;
2963 tabular.setCellWidth(cell, dim.wid);
2964 if (p_width.zero()) {
2965 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2966 // FIXME there must be a way to get rid of
2967 // the second metrics call
2968 tabular.getCellInset(cell)->metrics(m, dim);
2970 maxAsc = max(maxAsc, dim.asc);
2971 maxDesc = max(maxDesc, dim.des);
2974 int const top_space = tabular.row_info[i].top_space_default ?
2975 default_line_space :
2976 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2977 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2978 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2979 default_line_space :
2980 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2981 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2984 dim.asc = tabular.rowAscent(0);
2985 dim.des = tabular.height() - dim.asc;
2986 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2990 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2992 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2993 BufferView * bv = pi.base.bv;
2995 // FIXME: As the full backrgound is painted in drawSelection(),
2996 // we have no choice but to do a full repaint for the Text cells.
2997 pi.full_repaint = true;
2999 resetPos(bv->cursor());
3002 x += ADD_TO_TABULAR_WIDTH;
3004 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3007 first_visible_cell = Tabular::npos;
3008 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3010 int const a = tabular.rowAscent(i);
3011 int const d = tabular.rowDescent(i);
3012 idx = tabular.cellIndex(i, 0);
3013 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3014 if (tabular.isPartOfMultiColumn(i, j))
3016 if (first_visible_cell == Tabular::npos)
3017 first_visible_cell = idx;
3019 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3020 // Cache the Inset position.
3021 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3022 if (nx + tabular.columnWidth(idx) < 0
3023 || nx > bv->workWidth()
3025 || y - a > bv->workHeight()) {
3026 pi.pain.setDrawingEnabled(false);
3027 cell(idx)->draw(pi, cx, y);
3028 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3029 pi.pain.setDrawingEnabled(original_drawing_state);
3031 cell(idx)->draw(pi, cx, y);
3032 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3034 nx += tabular.columnWidth(idx);
3038 if (i + 1 < tabular.rowCount())
3039 y += d + tabular.rowAscent(i + 1) +
3040 tabular.getAdditionalHeight(i + 1);
3045 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3047 Cursor & cur = pi.base.bv->cursor();
3049 x += scx_ + ADD_TO_TABULAR_WIDTH;
3051 // FIXME: it is wrong to completely paint the background
3052 // if we want to do single row painting.
3054 // Paint background of current tabular
3055 int const w = tabular.width();
3056 int const h = tabular.height();
3057 int yy = y - tabular.rowAscent(0);
3058 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3060 if (!cur.selection())
3062 if (&cur.inset() != this)
3068 if (tablemode(cur)) {
3071 getSelection(cur, rs, re, cs, ce);
3072 y -= tabular.rowAscent(0);
3073 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3074 int const a = tabular.rowAscent(j);
3075 int const h = a + tabular.rowDescent(j);
3077 y += tabular.getAdditionalHeight(j);
3078 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3079 if (tabular.isPartOfMultiColumn(j, i))
3081 idx_type const cell =
3082 tabular.cellIndex(j, i);
3083 int const w = tabular.columnWidth(cell);
3084 if (i >= cs && i <= ce && j >= rs && j <= re)
3085 pi.pain.fillRectangle(xx, y, w, h,
3093 x += getCellXPos(cur.idx());
3094 x += tabular.getBeginningOfTextInCell(cur.idx());
3095 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3100 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3101 row_type row, idx_type cell, bool erased) const
3103 int x2 = x + tabular.columnWidth(cell);
3104 bool on_off = false;
3105 ColorCode col = Color_tabularline;
3106 ColorCode onoffcol = Color_tabularonoffline;
3109 col = Color_deletedtext;
3110 onoffcol = Color_deletedtext;
3113 if (!tabular.topAlreadyDrawn(cell)) {
3114 on_off = !tabular.topLine(cell);
3115 pain.line(x, y - tabular.rowAscent(row),
3116 x2, y - tabular.rowAscent(row),
3117 on_off ? onoffcol : col,
3118 on_off ? Painter::line_onoffdash : Painter::line_solid);
3120 on_off = !tabular.bottomLine(cell);
3121 pain.line(x, y + tabular.rowDescent(row),
3122 x2, y + tabular.rowDescent(row),
3123 on_off ? onoffcol : col,
3124 on_off ? Painter::line_onoffdash : Painter::line_solid);
3125 if (!tabular.leftAlreadyDrawn(cell)) {
3126 on_off = !tabular.leftLine(cell);
3127 pain.line(x, y - tabular.rowAscent(row),
3128 x, y + tabular.rowDescent(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3132 on_off = !tabular.rightLine(cell);
3133 pain.line(x2 - tabular.getAdditionalWidth(cell),
3134 y - tabular.rowAscent(row),
3135 x2 - tabular.getAdditionalWidth(cell),
3136 y + tabular.rowDescent(row),
3137 on_off ? onoffcol : col,
3138 on_off ? Painter::line_onoffdash : Painter::line_solid);
3142 docstring InsetTabular::editMessage() const
3144 return _("Opened table");
3148 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3150 //lyxerr << "InsetTabular::edit: " << this << endl;
3152 cur.selection() = false;
3155 if (isRightToLeft(cur))
3156 cur.idx() = tabular.getLastCellInRow(0);
3162 if (isRightToLeft(cur))
3163 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3165 cur.idx() = tabular.cellCount() - 1;
3167 cur.pos() = cur.lastpos(); // FIXME crude guess
3169 // FIXME: this accesses the position cache before it is initialized
3171 //cur.bv().fitCursor();
3175 void InsetTabular::updateLabels(ParIterator const & it)
3177 // In a longtable, tell captions what the current float is
3178 Counters & cnts = buffer().params().textClass().counters();
3179 string const saveflt = cnts.current_float();
3180 if (tabular.isLongTabular())
3181 cnts.current_float("table");
3183 ParIterator it2 = it;
3185 size_t const end = it2.nargs();
3186 for ( ; it2.idx() < end; it2.top().forwardIdx())
3187 lyx::updateLabels(buffer(), it2);
3190 if (tabular.isLongTabular())
3191 cnts.current_float(saveflt);
3195 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3197 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3198 << "\n cur:" << cur);
3199 CursorSlice sl = cur.top();
3200 Cursor & bvcur = cur.bv().cursor();
3202 switch (cmd.action) {
3204 case LFUN_MOUSE_PRESS:
3205 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3207 // do not reset cursor/selection if we have selected
3208 // some cells (bug 2715).
3209 if (cmd.button() == mouse_button::button3
3210 && &bvcur.selBegin().inset() == this
3211 && tablemode(bvcur))
3214 // Let InsetText do it
3215 cell(cur.idx())->dispatch(cur, cmd);
3217 case LFUN_MOUSE_MOTION:
3218 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3219 if (cmd.button() == mouse_button::button1) {
3220 // only accept motions to places not deeper nested than the real anchor
3221 if (bvcur.anchor_.hasPart(cur)) {
3222 // only update if selection changes
3223 if (bvcur.idx() == cur.idx() &&
3224 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3226 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3227 bvcur.setCursor(cur);
3228 bvcur.selection() = true;
3234 case LFUN_MOUSE_RELEASE:
3235 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3236 if (cmd.button() == mouse_button::button3)
3237 InsetTabularMailer(*this).showDialog(&cur.bv());
3240 case LFUN_CELL_BACKWARD:
3242 cur.selection() = false;
3245 case LFUN_CELL_FORWARD:
3247 cur.selection() = false;
3250 case LFUN_CHAR_FORWARD_SELECT:
3251 case LFUN_CHAR_FORWARD:
3252 cell(cur.idx())->dispatch(cur, cmd);
3253 if (!cur.result().dispatched()) {
3255 if (sl == cur.top())
3256 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3262 case LFUN_CHAR_BACKWARD_SELECT:
3263 case LFUN_CHAR_BACKWARD:
3264 cell(cur.idx())->dispatch(cur, cmd);
3265 if (!cur.result().dispatched()) {
3267 if (sl == cur.top())
3268 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3274 case LFUN_CHAR_RIGHT_SELECT:
3275 case LFUN_CHAR_RIGHT:
3276 //FIXME: for visual cursor, really move right
3277 if (isRightToLeft(cur))
3278 lyx::dispatch(FuncRequest(
3279 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3280 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3282 lyx::dispatch(FuncRequest(
3283 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3284 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3287 case LFUN_CHAR_LEFT_SELECT:
3288 case LFUN_CHAR_LEFT:
3289 //FIXME: for visual cursor, really move left
3290 if (isRightToLeft(cur))
3291 lyx::dispatch(FuncRequest(
3292 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3293 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3295 lyx::dispatch(FuncRequest(
3296 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3297 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3300 case LFUN_DOWN_SELECT:
3302 cell(cur.idx())->dispatch(cur, cmd);
3303 cur.dispatched(); // override the cell's decision
3304 if (sl == cur.top())
3305 // if our Text didn't do anything to the cursor
3306 // then we try to put the cursor into the cell below
3307 // setting also the right targetX.
3308 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3309 cur.idx() = tabular.getCellBelow(cur.idx());
3311 TextMetrics const & tm =
3312 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3313 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3315 if (sl == cur.top()) {
3316 // we trick it to go to forward after leaving the
3318 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3323 case LFUN_UP_SELECT:
3325 cell(cur.idx())->dispatch(cur, cmd);
3326 cur.dispatched(); // override the cell's decision
3327 if (sl == cur.top())
3328 // if our Text didn't do anything to the cursor
3329 // then we try to put the cursor into the cell above
3330 // setting also the right targetX.
3331 if (tabular.cellRow(cur.idx()) != 0) {
3332 cur.idx() = tabular.getCellAbove(cur.idx());
3333 cur.pit() = cur.lastpit();
3334 Text const * text = cell(cur.idx())->getText(0);
3335 TextMetrics const & tm = cur.bv().textMetrics(text);
3336 ParagraphMetrics const & pm =
3337 tm.parMetrics(cur.lastpit());
3338 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3340 if (sl == cur.top()) {
3341 cmd = FuncRequest(LFUN_UP);
3346 // case LFUN_SCREEN_DOWN: {
3347 // //if (hasSelection())
3348 // // cur.selection() = false;
3349 // col_type const col = tabular.cellColumn(cur.idx());
3350 // int const t = cur.bv().top_y() + cur.bv().height();
3351 // if (t < yo() + tabular.getHeightOfTabular()) {
3352 // cur.bv().scrollDocView(t);
3353 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3355 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3362 // case LFUN_SCREEN_UP: {
3363 // //if (hasSelection())
3364 // // cur.selection() = false;
3365 // col_type const col = tabular.cellColumn(cur.idx());
3366 // int const t = cur.bv().top_y() + cur.bv().height();
3368 // cur.bv().scrollDocView(t);
3372 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3376 // cur.par() = cur.lastpar();
3377 // cur.pos() = cur.lastpos();
3381 case LFUN_LAYOUT_TABULAR:
3382 InsetTabularMailer(*this).showDialog(&cur.bv());
3385 case LFUN_INSET_DIALOG_UPDATE:
3386 InsetTabularMailer(*this).updateDialog(&cur.bv());
3389 case LFUN_TABULAR_FEATURE:
3390 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3394 // insert file functions
3395 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3396 case LFUN_FILE_INSERT_PLAINTEXT: {
3398 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3399 FileName(to_utf8(cmd.argument())));
3402 cur.recordUndoInset(INSERT_UNDO);
3403 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3404 // content has been replaced,
3405 // so cursor might be invalid
3406 cur.pos() = cur.lastpos();
3407 cur.pit() = cur.lastpit();
3408 bvcur.setCursor(cur);
3415 if (tablemode(cur)) {
3416 if (copySelection(cur)) {
3417 cur.recordUndoInset(DELETE_UNDO);
3422 cell(cur.idx())->dispatch(cur, cmd);
3425 case LFUN_CHAR_DELETE_BACKWARD:
3426 case LFUN_CHAR_DELETE_FORWARD:
3427 if (tablemode(cur)) {
3428 cur.recordUndoInset(DELETE_UNDO);
3432 cell(cur.idx())->dispatch(cur, cmd);
3436 if (!cur.selection())
3438 if (tablemode(cur)) {
3442 cell(cur.idx())->dispatch(cur, cmd);
3445 case LFUN_CLIPBOARD_PASTE:
3446 case LFUN_PRIMARY_SELECTION_PASTE: {
3447 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3448 theClipboard().getAsText() :
3449 theSelection().get();
3452 // pass to InsertPlaintextString, but
3453 // only if we have multi-cell content
3454 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3455 cur.recordUndoInset(INSERT_UNDO);
3456 if (insertPlaintextString(cur.bv(), clip, false)) {
3457 // content has been replaced,
3458 // so cursor might be invalid
3459 cur.pos() = cur.lastpos();
3460 cur.pit() = cur.lastpit();
3461 bvcur.setCursor(cur);
3465 // Let the cell handle normal text
3466 cell(cur.idx())->dispatch(cur, cmd);
3471 if (tabularStackDirty() && theClipboard().isInternal() ||
3472 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
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
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(odocstream & os, OutputParams const & runparams) const
3804 return tabular.latex(os, runparams);
3808 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3810 os << '\n'; // output table on a new line
3811 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3812 tabular.plaintext(os, runparams, dp, false, 0);
3813 return PLAINTEXT_NEWLINE;
3817 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3822 // FIXME: Why not pass a proper DocIterator here?
3824 // if the table is inside a float it doesn't need the informaltable
3825 // wrapper. Search for it.
3826 for (master = owner(); master; master = master->owner())
3827 if (master->lyxCode() == FLOAT_CODE)
3832 os << "<informaltable>";
3835 ret += tabular.docbook(os, runparams);
3837 os << "</informaltable>";
3844 void InsetTabular::validate(LaTeXFeatures & features) const
3846 tabular.validate(features);
3850 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3852 return tabular.getCellInset(idx);
3856 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3858 return tabular.getCellInset(idx);
3862 void InsetTabular::cursorPos(BufferView const & bv,
3863 CursorSlice const & sl, bool boundary, int & x, int & y) const
3865 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3867 // y offset correction
3868 int const row = tabular.cellRow(sl.idx());
3869 for (int i = 0; i <= row; ++i) {
3871 y += tabular.rowAscent(i);
3872 y += tabular.getAdditionalHeight(i);
3875 y += tabular.rowDescent(i);
3878 // x offset correction
3879 int const col = tabular.cellColumn(sl.idx());
3880 int idx = tabular.cellIndex(row, 0);
3881 for (int j = 0; j < col; ++j) {
3882 if (tabular.isPartOfMultiColumn(row, j))
3884 x += tabular.columnWidth(idx);
3887 x += tabular.getBeginningOfTextInCell(idx);
3888 x += ADD_TO_TABULAR_WIDTH;
3893 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3897 Inset const & inset = *tabular.getCellInset(cell);
3898 Point o = bv.coordCache().getInsets().xy(&inset);
3899 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3900 int const xend = xbeg + tabular.columnWidth(cell);
3901 row_type const row = tabular.cellRow(cell);
3902 int const ybeg = o.y_ - tabular.rowAscent(row) -
3903 tabular.getAdditionalHeight(row);
3904 int const yend = o.y_ + tabular.rowDescent(row);
3916 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3917 // << " ybeg=" << ybeg << " yend=" << yend
3918 // << " xx=" << xx << " yy=" << yy
3919 // << " dist=" << xx + yy << endl;
3924 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3926 //lyxerr << "InsetTabular::editXY: " << this << endl;
3927 cur.selection() = false;
3929 cur.idx() = getNearestCell(cur.bv(), x, y);
3931 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3935 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3937 cur.idx() = getNearestCell(cur.bv(), x, y);
3938 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3942 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3944 idx_type idx_min = 0;
3945 int dist_min = numeric_limits<int>::max();
3946 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3947 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3948 int const d = dist(bv, i, x, y);
3959 int InsetTabular::getCellXPos(idx_type const cell) const
3963 for (; !tabular.isFirstCellInRow(c); --c)
3966 for (; c < cell; ++c)
3967 lx += tabular.columnWidth(c);
3973 void InsetTabular::resetPos(Cursor & cur) const
3975 BufferView & bv = cur.bv();
3976 int const maxwidth = bv.workWidth();
3978 if (&cur.inset() != this) {
3982 int const X2 = maxwidth;
3983 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3984 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3985 int const x2 = x1 + tabular.columnWidth(cur.idx());
3988 scx_ = X1 + 20 - x1;
3990 scx_ = X2 - 20 - x2;
3995 cur.updateFlags(Update::Force | Update::FitCursor);
3999 void InsetTabular::moveNextCell(Cursor & cur)
4001 if (isRightToLeft(cur)) {
4002 if (tabular.isFirstCellInRow(cur.idx())) {
4003 row_type const row = tabular.cellRow(cur.idx());
4004 if (row == tabular.rowCount() - 1)
4006 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4013 if (tabular.isLastCell(cur.idx()))
4023 void InsetTabular::movePrevCell(Cursor & cur)
4025 if (isRightToLeft(cur)) {
4026 if (tabular.isLastCellInRow(cur.idx())) {
4027 row_type const row = tabular.cellRow(cur.idx());
4030 cur.idx() = tabular.getFirstCellInRow(row);
4031 cur.idx() = tabular.getCellAbove(cur.idx());
4033 if (tabular.isLastCell(cur.idx()))
4038 if (cur.idx() == 0) // first cell
4042 cur.pit() = cur.lastpit();
4043 cur.pos() = cur.lastpos();
4045 // FIXME: this accesses the position cache before it is initialized
4050 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4052 Tabular::Feature action = Tabular::LAST_ACTION;
4055 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4056 string const tmp = tabularFeature[i].feature;
4058 if (tmp == what.substr(0, tmp.length())) {
4059 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4060 //tabularFeatures[i].feature.length()))
4061 action = tabularFeature[i].action;
4065 if (action == Tabular::LAST_ACTION)
4069 ltrim(what.substr(tabularFeature[i].feature.length()));
4070 tabularFeatures(cur, action, val);
4075 static void checkLongtableSpecial(Tabular::ltType & ltt,
4076 string const & special, bool & flag)
4078 if (special == "dl_above") {
4081 } else if (special == "dl_below") {
4082 ltt.bottomDL = flag;
4084 } else if (special == "empty") {
4093 bool InsetTabular::oneCellHasRotationState(bool rotated,
4094 row_type row_start, row_type row_end,
4095 col_type col_start, col_type col_end) const {
4097 for (row_type i = row_start; i <= row_end; ++i) {
4098 for (col_type j = col_start; j <= col_end; ++j) {
4099 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4108 void InsetTabular::tabularFeatures(Cursor & cur,
4109 Tabular::Feature feature, string const & value)
4111 col_type sel_col_start;
4112 col_type sel_col_end;
4113 row_type sel_row_start;
4114 row_type sel_row_end;
4115 bool setLines = false;
4116 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4117 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4121 case Tabular::M_ALIGN_LEFT:
4122 case Tabular::ALIGN_LEFT:
4123 setAlign = LYX_ALIGN_LEFT;
4126 case Tabular::M_ALIGN_RIGHT:
4127 case Tabular::ALIGN_RIGHT:
4128 setAlign = LYX_ALIGN_RIGHT;
4131 case Tabular::M_ALIGN_CENTER:
4132 case Tabular::ALIGN_CENTER:
4133 setAlign = LYX_ALIGN_CENTER;
4136 case Tabular::ALIGN_BLOCK:
4137 setAlign = LYX_ALIGN_BLOCK;
4140 case Tabular::M_VALIGN_TOP:
4141 case Tabular::VALIGN_TOP:
4142 setVAlign = Tabular::LYX_VALIGN_TOP;
4145 case Tabular::M_VALIGN_BOTTOM:
4146 case Tabular::VALIGN_BOTTOM:
4147 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4150 case Tabular::M_VALIGN_MIDDLE:
4151 case Tabular::VALIGN_MIDDLE:
4152 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4159 cur.recordUndoInset(ATOMIC_UNDO);
4161 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4162 row_type const row = tabular.cellRow(cur.idx());
4163 col_type const column = tabular.cellColumn(cur.idx());
4165 Tabular::ltType ltt;
4169 case Tabular::SET_PWIDTH: {
4170 Length const len(value);
4171 tabular.setColumnPWidth(cur, cur.idx(), len);
4173 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4174 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4178 case Tabular::SET_MPWIDTH:
4179 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4182 case Tabular::SET_SPECIAL_COLUMN:
4183 case Tabular::SET_SPECIAL_MULTI:
4184 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4187 case Tabular::APPEND_ROW:
4188 // append the row into the tabular
4189 tabular.appendRow(cur.idx());
4192 case Tabular::APPEND_COLUMN:
4193 // append the column into the tabular
4194 tabular.appendColumn(cur.idx());
4195 cur.idx() = tabular.cellIndex(row, column);
4198 case Tabular::DELETE_ROW:
4199 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4200 tabular.deleteRow(sel_row_start);
4201 if (sel_row_start >= tabular.rowCount())
4203 cur.idx() = tabular.cellIndex(sel_row_start, column);
4206 cur.selection() = false;
4209 case Tabular::DELETE_COLUMN:
4210 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4211 tabular.deleteColumn(sel_col_start);
4212 if (sel_col_start >= tabular.columnCount())
4214 cur.idx() = tabular.cellIndex(row, sel_col_start);
4217 cur.selection() = false;
4220 case Tabular::COPY_ROW:
4221 tabular.copyRow(row);
4224 case Tabular::COPY_COLUMN:
4225 tabular.copyColumn(column);
4226 cur.idx() = tabular.cellIndex(row, column);
4229 case Tabular::M_TOGGLE_LINE_TOP:
4231 case Tabular::TOGGLE_LINE_TOP: {
4232 bool lineSet = !tabular.topLine(cur.idx(), flag);
4233 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4234 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4236 tabular.cellIndex(i, j),
4241 case Tabular::M_TOGGLE_LINE_BOTTOM:
4243 case Tabular::TOGGLE_LINE_BOTTOM: {
4244 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4245 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4246 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4247 tabular.setBottomLine(
4248 tabular.cellIndex(i, j),
4254 case Tabular::M_TOGGLE_LINE_LEFT:
4256 case Tabular::TOGGLE_LINE_LEFT: {
4257 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4258 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4259 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4260 tabular.setLeftLine(
4261 tabular.cellIndex(i,j),
4267 case Tabular::M_TOGGLE_LINE_RIGHT:
4269 case Tabular::TOGGLE_LINE_RIGHT: {
4270 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4271 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4272 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4273 tabular.setRightLine(tabular.cellIndex(i,j), lineSet, flag);
4277 case Tabular::M_ALIGN_LEFT:
4278 case Tabular::M_ALIGN_RIGHT:
4279 case Tabular::M_ALIGN_CENTER:
4281 case Tabular::ALIGN_LEFT:
4282 case Tabular::ALIGN_RIGHT:
4283 case Tabular::ALIGN_CENTER:
4284 case Tabular::ALIGN_BLOCK:
4285 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4286 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4287 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4290 case Tabular::M_VALIGN_TOP:
4291 case Tabular::M_VALIGN_BOTTOM:
4292 case Tabular::M_VALIGN_MIDDLE:
4294 case Tabular::VALIGN_TOP:
4295 case Tabular::VALIGN_BOTTOM:
4296 case Tabular::VALIGN_MIDDLE:
4297 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4298 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4299 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4302 case Tabular::MULTICOLUMN: {
4303 if (sel_row_start != sel_row_end) {
4304 // FIXME: Need I say it ? This is horrible.
4306 Alert::error(_("Error setting multicolumn"),
4307 _("You cannot set multicolumn vertically."));
4310 if (!cur.selection()) {
4311 // just multicol for one single cell
4312 // check whether we are completely in a multicol
4313 if (tabular.isMultiColumn(cur.idx()))
4314 tabular.unsetMultiColumn(cur.idx());
4316 tabular.setMultiColumn(cur.idx(), 1);
4319 // we have a selection so this means we just add all this
4320 // cells to form a multicolumn cell
4321 idx_type const s_start = cur.selBegin().idx();
4322 idx_type const s_end = cur.selEnd().idx();
4323 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4324 cur.idx() = s_start;
4327 cur.selection() = false;
4331 case Tabular::SET_ALL_LINES:
4333 case Tabular::UNSET_ALL_LINES:
4334 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4335 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4336 tabular.setAllLines(
4337 tabular.cellIndex(i,j), setLines);
4340 case Tabular::SET_LONGTABULAR:
4341 tabular.setLongTabular(true);
4344 case Tabular::UNSET_LONGTABULAR:
4345 tabular.setLongTabular(false);
4348 case Tabular::SET_ROTATE_TABULAR:
4349 tabular.setRotateTabular(true);
4352 case Tabular::UNSET_ROTATE_TABULAR:
4353 tabular.setRotateTabular(false);
4356 case Tabular::TOGGLE_ROTATE_TABULAR:
4357 tabular.setRotateTabular(!tabular.getRotateTabular());
4360 case Tabular::SET_ROTATE_CELL:
4361 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4362 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4363 tabular.setRotateCell(
4364 tabular.cellIndex(i, j), true);
4367 case Tabular::UNSET_ROTATE_CELL:
4368 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4369 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4370 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4373 case Tabular::TOGGLE_ROTATE_CELL:
4375 bool oneNotRotated = oneCellHasRotationState(false,
4376 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4378 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4379 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4380 tabular.setRotateCell(tabular.cellIndex(i, j),
4385 case Tabular::SET_USEBOX: {
4386 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4387 if (val == tabular.getUsebox(cur.idx()))
4388 val = Tabular::BOX_NONE;
4389 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4390 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4391 tabular.setUsebox(tabular.cellIndex(i, j), val);
4395 case Tabular::UNSET_LTFIRSTHEAD:
4397 case Tabular::SET_LTFIRSTHEAD:
4398 tabular.getRowOfLTFirstHead(row, ltt);
4399 checkLongtableSpecial(ltt, value, flag);
4400 tabular.setLTHead(row, flag, ltt, true);
4403 case Tabular::UNSET_LTHEAD:
4405 case Tabular::SET_LTHEAD:
4406 tabular.getRowOfLTHead(row, ltt);
4407 checkLongtableSpecial(ltt, value, flag);
4408 tabular.setLTHead(row, flag, ltt, false);
4411 case Tabular::UNSET_LTFOOT:
4413 case Tabular::SET_LTFOOT:
4414 tabular.getRowOfLTFoot(row, ltt);
4415 checkLongtableSpecial(ltt, value, flag);
4416 tabular.setLTFoot(row, flag, ltt, false);
4419 case Tabular::UNSET_LTLASTFOOT:
4421 case Tabular::SET_LTLASTFOOT:
4422 tabular.getRowOfLTLastFoot(row, ltt);
4423 checkLongtableSpecial(ltt, value, flag);
4424 tabular.setLTFoot(row, flag, ltt, true);
4427 case Tabular::SET_LTNEWPAGE:
4428 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4431 case Tabular::SET_BOOKTABS:
4432 tabular.setBookTabs(true);
4435 case Tabular::UNSET_BOOKTABS:
4436 tabular.setBookTabs(false);
4439 case Tabular::SET_TOP_SPACE: {
4441 if (value == "default")
4442 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4443 tabular.row_info[i].top_space_default = true;
4444 else if (isValidLength(value, &len))
4445 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4446 tabular.row_info[i].top_space_default = false;
4447 tabular.row_info[i].top_space = len;
4450 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4451 tabular.row_info[i].top_space_default = false;
4452 tabular.row_info[i].top_space = len;
4457 case Tabular::SET_BOTTOM_SPACE: {
4459 if (value == "default")
4460 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4461 tabular.row_info[i].bottom_space_default = true;
4462 else if (isValidLength(value, &len))
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4464 tabular.row_info[i].bottom_space_default = false;
4465 tabular.row_info[i].bottom_space = len;
4468 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4469 tabular.row_info[i].bottom_space_default = false;
4470 tabular.row_info[i].bottom_space = len;
4475 case Tabular::SET_INTERLINE_SPACE: {
4477 if (value == "default")
4478 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4479 tabular.row_info[i].interline_space_default = true;
4480 else if (isValidLength(value, &len))
4481 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4482 tabular.row_info[i].interline_space_default = false;
4483 tabular.row_info[i].interline_space = len;
4486 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4487 tabular.row_info[i].interline_space_default = false;
4488 tabular.row_info[i].interline_space = len;
4493 // dummy stuff just to avoid warnings
4494 case Tabular::LAST_ACTION:
4500 bool InsetTabular::showInsetDialog(BufferView * bv) const
4502 InsetTabularMailer(*this).showDialog(bv);
4507 void InsetTabular::openLayoutDialog(BufferView * bv) const
4509 InsetTabularMailer(*this).showDialog(bv);
4513 bool InsetTabular::copySelection(Cursor & cur)
4515 if (!cur.selection())
4520 getSelection(cur, rs, re, cs, ce);
4522 paste_tabular.reset(new Tabular(tabular));
4524 for (row_type i = 0; i < rs; ++i)
4525 paste_tabular->deleteRow(0);
4527 row_type const rows = re - rs + 1;
4528 while (paste_tabular->rowCount() > rows)
4529 paste_tabular->deleteRow(rows);
4531 paste_tabular->setTopLine(0, true, true);
4532 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4535 for (col_type i = 0; i < cs; ++i)
4536 paste_tabular->deleteColumn(0);
4538 col_type const columns = ce - cs + 1;
4539 while (paste_tabular->columnCount() > columns)
4540 paste_tabular->deleteColumn(columns);
4542 paste_tabular->setLeftLine(0, true, true);
4543 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4546 odocstringstream os;
4547 OutputParams const runparams(0);
4548 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4549 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4550 cap::copySelection(cur, os.str());
4552 // mark tabular stack dirty
4553 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4554 // when we (hopefully) have a one-for-all paste mechanism.
4555 // This must be called after cap::copySelection.
4556 dirtyTabularStack(true);
4562 bool InsetTabular::pasteClipboard(Cursor & cur)
4566 col_type const actcol = tabular.cellColumn(cur.idx());
4567 row_type const actrow = tabular.cellRow(cur.idx());
4568 for (row_type r1 = 0, r2 = actrow;
4569 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4571 for (col_type c1 = 0, c2 = actcol;
4572 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4574 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4575 tabular.isPartOfMultiColumn(r2, c2))
4577 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4581 if (tabular.isPartOfMultiColumn(r2, c2)) {
4585 shared_ptr<InsetText> inset(
4586 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4587 tabular.setCellInset(r2, c2, inset);
4588 // FIXME: change tracking (MG)
4589 inset->setChange(Change(cur.buffer().params().trackChanges ?
4590 Change::INSERTED : Change::UNCHANGED));
4598 void InsetTabular::cutSelection(Cursor & cur)
4600 if (!cur.selection())
4605 getSelection(cur, rs, re, cs, ce);
4606 for (row_type i = rs; i <= re; ++i) {
4607 for (col_type j = cs; j <= ce; ++j) {
4608 shared_ptr<InsetText> t
4609 = cell(tabular.cellIndex(i, j));
4610 if (cur.buffer().params().trackChanges)
4611 // FIXME: Change tracking (MG)
4612 t->setChange(Change(Change::DELETED));
4618 // cursor position might be invalid now
4619 if (cur.pit() > cur.lastpit())
4620 cur.pit() = cur.lastpit();
4621 if (cur.pos() > cur.lastpos())
4622 cur.pos() = cur.lastpos();
4623 cur.clearSelection();
4627 bool InsetTabular::isRightToLeft(Cursor & cur) const
4629 BOOST_ASSERT(cur.depth() > 1);
4630 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4631 pos_type const parentpos = cur[cur.depth() - 2].pos();
4632 return parentpar.getFontSettings(cur.bv().buffer().params(),
4633 parentpos).language()->rightToLeft();
4637 void InsetTabular::getSelection(Cursor & cur,
4638 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4640 CursorSlice const & beg = cur.selBegin();
4641 CursorSlice const & end = cur.selEnd();
4642 cs = tabular.cellColumn(beg.idx());
4643 ce = tabular.cellColumn(end.idx());
4646 cs = tabular.cellColumn(end.idx());
4648 ce = tabular.cellRightColumn(end.idx());
4651 rs = tabular.cellRow(beg.idx());
4652 re = tabular.cellRow(end.idx());
4658 Text * InsetTabular::getText(int idx) const
4660 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4664 void InsetTabular::setChange(Change const & change)
4666 for (idx_type idx = 0; idx < nargs(); ++idx)
4667 cell(idx)->setChange(change);
4671 void InsetTabular::acceptChanges(BufferParams const & bparams)
4673 for (idx_type idx = 0; idx < nargs(); ++idx)
4674 cell(idx)->acceptChanges(bparams);
4678 void InsetTabular::rejectChanges(BufferParams const & bparams)
4680 for (idx_type idx = 0; idx < nargs(); ++idx)
4681 cell(idx)->rejectChanges(bparams);
4685 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4687 return tabular.getPWidth(cell).zero();
4691 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4694 if (buf.length() <= 0)
4699 col_type maxCols = 1;
4700 docstring::size_type const len = buf.length();
4701 docstring::size_type p = 0;
4704 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4712 maxCols = max(cols, maxCols);
4718 maxCols = max(cols, maxCols);
4724 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4725 loctab = paste_tabular.get();
4727 dirtyTabularStack(true);
4730 cell = bv.cursor().idx();
4731 ocol = tabular.cellColumn(cell);
4732 row = tabular.cellRow(cell);
4735 docstring::size_type op = 0;
4736 idx_type const cells = loctab->cellCount();
4739 rows = loctab->rowCount();
4740 col_type const columns = loctab->columnCount();
4742 while (cell < cells && p < len && row < rows &&
4743 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4749 // we can only set this if we are not too far right
4750 if (cols < columns) {
4751 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4752 Font const font = bv.textMetrics(&inset->text_).
4753 getDisplayFont(0, 0);
4754 inset->setText(buf.substr(op, p - op), font,
4755 buffer().params().trackChanges);
4761 // we can only set this if we are not too far right
4762 if (cols < columns) {
4763 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4764 Font const font = bv.textMetrics(&inset->text_).
4765 getDisplayFont(0, 0);
4766 inset->setText(buf.substr(op, p - op), font,
4767 buffer().params().trackChanges);
4772 cell = loctab->cellIndex(row, cols);
4778 // check for the last cell if there is no trailing '\n'
4779 if (cell < cells && op < len) {
4780 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4781 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4782 inset->setText(buf.substr(op, len - op), font,
4783 buffer().params().trackChanges);
4789 void InsetTabular::addPreview(PreviewLoader & loader) const
4791 row_type const rows = tabular.rowCount();
4792 col_type const columns = tabular.columnCount();
4793 for (row_type i = 0; i < rows; ++i) {
4794 for (col_type j = 0; j < columns; ++j)
4795 tabular.getCellInset(i, j)->addPreview(loader);
4800 bool InsetTabular::tablemode(Cursor & cur) const
4802 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4808 string const InsetTabularMailer::name_("tabular");
4810 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4811 : inset_(const_cast<InsetTabular &>(inset))
4815 string const InsetTabularMailer::inset2string(Buffer const &) const
4817 return params2string(inset_);
4821 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4823 istringstream data(in);
4825 lex.setStream(data);
4832 if (!lex || token != name_)
4833 return print_mailer_error("InsetTabularMailer", in, 1,
4836 // This is part of the inset proper that is usually swallowed
4837 // by Buffer::readInset
4839 if (!lex || token != "Tabular")
4840 return print_mailer_error("InsetTabularMailer", in, 2, "Tabular");
4846 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4849 data << name_ << ' ';
4851 data << "\\end_inset\n";