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(BufferParams const & bp)
478 multicolumn(Tabular::CELL_NORMAL),
479 alignment(LYX_ALIGN_CENTER),
480 valignment(LYX_VALIGN_TOP),
487 inset(new InsetText(bp))
489 inset->paragraphs().back().layout(bp.getTextClass().emptyLayout());
493 Tabular::cellstruct::cellstruct(cellstruct const & cs)
496 multicolumn(cs.multicolumn),
497 alignment(cs.alignment),
498 valignment(cs.valignment),
499 top_line(cs.top_line),
500 bottom_line(cs.bottom_line),
501 left_line(cs.left_line),
502 right_line(cs.right_line),
505 align_special(cs.align_special),
507 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
511 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
518 void Tabular::cellstruct::swap(cellstruct & rhs)
520 std::swap(cellno, rhs.cellno);
521 std::swap(width, rhs.width);
522 std::swap(multicolumn, rhs.multicolumn);
523 std::swap(alignment, rhs.alignment);
524 std::swap(valignment, rhs.valignment);
525 std::swap(top_line, rhs.top_line);
526 std::swap(bottom_line, rhs.bottom_line);
527 std::swap(left_line, rhs.left_line);
528 std::swap(right_line, rhs.right_line);
529 std::swap(usebox, rhs.usebox);
530 std::swap(rotate, rhs.rotate);
531 std::swap(align_special, rhs.align_special);
532 p_width.swap(rhs.p_width);
533 inset.swap(rhs.inset);
537 Tabular::rowstruct::rowstruct()
542 top_space_default(false),
543 bottom_space_default(false),
544 interline_space_default(false),
553 Tabular::columnstruct::columnstruct()
554 : alignment(LYX_ALIGN_CENTER),
555 valignment(LYX_VALIGN_TOP),
563 Tabular::ltType::ltType()
576 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
577 col_type columns_arg)
579 init(bp, rows_arg, columns_arg);
583 // activates all lines and sets all widths to 0
584 void Tabular::init(BufferParams const & bp, row_type rows_arg,
585 col_type columns_arg)
587 row_info = row_vector(rows_arg);
588 column_info = column_vector(columns_arg);
589 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
590 row_info.reserve(10);
591 column_info.reserve(10);
592 cell_info.reserve(100);
594 for (row_type i = 0; i < rows_arg; ++i)
595 cell_info[i].back().right_line = true;
596 row_info.back().bottom_line = true;
597 row_info.front().bottom_line = true;
598 column_info.back().right_line = true;
599 is_long_tabular = false;
601 use_booktabs = false;
605 void Tabular::fixCellNums()
608 for (row_type i = 0; i < rowCount(); ++i) {
609 for (col_type j = 0; j < columnCount(); ++j) {
610 // When debugging it can be nice to set
612 cell_info[i][j].inset->setDrawFrame(false);
613 cell_info[i][j].cellno = cellno++;
615 cell_info[i].back().right_line = true;
622 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
624 row_type const row = cellRow(cell);
626 row_vector::iterator rit = row_info.begin() + row;
627 row_info.insert(rit, rowstruct());
628 // now set the values of the row before
629 row_info[row] = row_info[row + 1];
631 row_type const nrows = rowCount();
632 col_type const ncols = columnCount();
634 cell_vvector old(nrows - 1);
635 for (row_type i = 0; i < nrows - 1; ++i)
636 swap(cell_info[i], old[i]);
638 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
640 for (row_type i = 0; i <= row; ++i)
641 swap(cell_info[i], old[i]);
642 for (row_type i = row + 2; i < nrows; ++i)
643 swap(cell_info[i], old[i - 1]);
646 for (col_type j = 0; j < ncols; ++j)
647 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
653 void Tabular::deleteRow(row_type const row)
655 // Not allowed to delete last row
659 row_info.erase(row_info.begin() + row);
660 cell_info.erase(cell_info.begin() + row);
665 void Tabular::copyRow(BufferParams const & bp, row_type const row)
667 row_info.insert(row_info.begin() + row, row_info[row]);
668 cell_info.insert(cell_info.begin() + row, cell_info[row]);
671 for (col_type j = 0; j < columnCount(); ++j)
672 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
678 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
680 col_type const column = cellColumn(cell);
681 col_type const ncols = columnCount();
682 column_vector::iterator cit = column_info.begin() + column + 1;
683 column_info.insert(cit, columnstruct());
684 // set the column values of the column before
685 column_info[column + 1] = column_info[column];
687 for (row_type i = 0; i < rowCount(); ++i) {
688 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
690 // care about multicolumns
691 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
692 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
694 if (column + 2 >= ncols
695 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
696 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
699 for (row_type i = 0; i < rowCount(); ++i) {
700 cell_info[i][column + 1].inset->clear();
702 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
708 void Tabular::deleteColumn(col_type const column)
710 // Not allowed to delete last column
711 if (columnCount() == 1)
714 column_info.erase(column_info.begin() + column);
715 for (row_type i = 0; i < rowCount(); ++i) {
716 // Care about multicolumn cells
717 if (column + 1 < columnCount() &&
718 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
719 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
720 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
722 cell_info[i].erase(cell_info[i].begin() + column);
728 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
730 column_info.insert(column_info.begin() + column, column_info[column]);
732 for (row_type i = 0; i < rowCount(); ++i)
733 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
736 for (row_type i = 0; i < rowCount(); ++i)
737 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
742 void Tabular::updateIndexes()
745 // Count only non-multicol cells plus begin multicol
746 // cells, ignore non-begin multicol cells:
747 for (row_type row = 0; row < rowCount(); ++row) {
748 for (col_type column = 0; column < columnCount(); ++column) {
749 if (cell_info[row][column].multicolumn
750 != Tabular::CELL_PART_OF_MULTICOLUMN)
752 BOOST_ASSERT(numberofcells != 0);
753 cell_info[row][column].cellno =
758 rowofcell.resize(numberofcells);
759 columnofcell.resize(numberofcells);
764 c < numberofcells && row < rowCount() && column < columnCount();) {
766 columnofcell[c] = column;
770 } while (column < columnCount() &&
771 cell_info[row][column].multicolumn
772 == Tabular::CELL_PART_OF_MULTICOLUMN);
774 if (column == columnCount()) {
780 for (row_type row = 0; row < rowCount(); ++row) {
781 for (col_type column = 0; column < columnCount(); ++column) {
782 if (isPartOfMultiColumn(row,column))
784 cell_info[row][column].inset->setAutoBreakRows(
785 !getPWidth(cellIndex(row, column)).zero());
791 Tabular::idx_type Tabular::cellCount() const
793 return numberofcells;
797 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
799 row_type const row = cellRow(cell);
801 for (col_type i = 0; i < columnCount(); ++i)
802 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
808 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
810 if (!wholerow && isMultiColumn(cell) &&
811 !(use_booktabs && cellRow(cell) == 0))
812 return cellinfo_of_cell(cell).top_line;
813 return row_info[cellRow(cell)].top_line;
817 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
819 if (!wholerow && isMultiColumn(cell) &&
820 !(use_booktabs && isLastRow(cell)))
821 return cellinfo_of_cell(cell).bottom_line;
822 return row_info[cellRow(cell)].bottom_line;
826 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
830 if (!wholecolumn && isMultiColumn(cell) &&
831 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
832 return cellinfo_of_cell(cell).left_line;
833 return column_info[cellColumn(cell)].left_line;
837 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
841 if (!wholecolumn && isMultiColumn(cell) &&
842 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
843 return cellinfo_of_cell(cell).right_line;
844 return column_info[cellRightColumn(cell)].right_line;
848 bool Tabular::topAlreadyDrawn(idx_type cell) const
850 row_type row = cellRow(cell);
851 if (row > 0 && !getAdditionalHeight(row)) {
852 col_type column = cellColumn(cell);
855 && cell_info[row][column].multicolumn
856 == Tabular::CELL_PART_OF_MULTICOLUMN)
858 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
859 return row_info[row].bottom_line;
861 return cell_info[row][column].bottom_line;
867 bool Tabular::leftAlreadyDrawn(idx_type cell) const
869 col_type column = cellColumn(cell);
871 row_type row = cellRow(cell);
873 (cell_info[row][column].multicolumn ==
874 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
875 if (getAdditionalWidth(cell_info[row][column].cellno))
877 return rightLine(cell_info[row][column].cellno);
883 bool Tabular::isLastRow(idx_type cell) const
885 return cellRow(cell) == rowCount() - 1;
889 int Tabular::getAdditionalHeight(row_type row) const
891 if (!row || row >= rowCount())
897 for (col_type column = 0; column < columnCount() && bottom; ++column) {
898 switch (cell_info[row - 1][column].multicolumn) {
899 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
900 bottom = cell_info[row - 1][column].bottom_line;
902 case Tabular::CELL_NORMAL:
903 bottom = row_info[row - 1].bottom_line;
906 for (col_type column = 0; column < columnCount() && top; ++column) {
907 switch (cell_info[row][column].multicolumn) {
908 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
909 top = cell_info[row][column].top_line;
911 case Tabular::CELL_NORMAL:
912 top = row_info[row].top_line;
915 int const interline_space = row_info[row - 1].interline_space_default ?
917 row_info[row - 1].interline_space.inPixels(width());
919 return interline_space + WIDTH_OF_LINE;
920 return interline_space;
924 int Tabular::getAdditionalWidth(idx_type cell) const
926 // internally already set in setCellWidth
927 // used to get it back in text.cpp
928 col_type const col = cellRightColumn(cell);
929 row_type const row = cellRow(cell);
930 if (col < columnCount() - 1 && rightLine(cell) &&
931 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
933 return WIDTH_OF_LINE;
939 // returns the maximum over all rows
940 int Tabular::columnWidth(idx_type cell) const
942 col_type const column1 = cellColumn(cell);
943 col_type const column2 = cellRightColumn(cell);
945 for (col_type i = column1; i <= column2; ++i)
946 result += column_info[i].width;
951 int Tabular::width() const
954 for (col_type i = 0; i < columnCount(); ++i)
955 width += column_info[i].width;
960 // returns true if a complete update is necessary, otherwise false
961 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
963 if (!isMultiColumn(cell))
966 row_type const row = cellRow(cell);
967 col_type const column1 = cellColumn(cell);
968 col_type const column2 = cellRightColumn(cell);
969 int const old_val = cell_info[row][column2].width;
971 // first set columns to 0 so we can calculate the right width
972 for (col_type i = column1; i <= column2; ++i) {
973 cell_info[row][i].width = 0;
975 // set the width to MAX_WIDTH until width > 0
976 int width = new_width + 2 * WIDTH_OF_LINE;
977 col_type i = column1;
978 for (; i < column2 && width > column_info[i].width; ++i) {
979 cell_info[row][i].width = column_info[i].width;
980 width -= column_info[i].width;
983 cell_info[row][i].width = width;
985 if (old_val != cell_info[row][column2].width) {
986 // in this case we have to recalculate all multicolumn cells which
987 // have this column as one of theirs but not as last one
988 calculate_width_of_column_NMC(i);
989 recalculateMulticolumnsOfColumn(i);
990 calculate_width_of_column(i);
996 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
998 // the last column does not have to be recalculated because all
999 // multicolumns will have here there last multicolumn cell which
1000 // always will have the whole rest of the width of the cell.
1001 if (columnCount() < 2 || column > (columnCount() - 2))
1003 for (row_type row = 0; row < rowCount(); ++row) {
1004 int mc = cell_info[row][column].multicolumn;
1005 int nmc = cell_info[row][column+1].multicolumn;
1006 // we only have to update multicolumns which do not have this
1007 // column as their last column!
1008 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1009 (mc == CELL_PART_OF_MULTICOLUMN &&
1010 nmc == CELL_PART_OF_MULTICOLUMN))
1012 idx_type const cellno = cell_info[row][column].cellno;
1013 setWidthOfMulticolCell(cellno,
1014 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1020 void Tabular::setCellWidth(idx_type cell, int new_width)
1022 row_type const row = cellRow(cell);
1023 col_type const column1 = cellColumn(cell);
1028 if (rightLine(cell_info[row][column1].cellno, true) &&
1029 column1 < columnCount() - 1 &&
1030 leftLine(cell_info[row][column1+1].cellno, true))
1032 add_width = WIDTH_OF_LINE;
1035 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1038 if (isMultiColumnReal(cell)) {
1039 tmp = setWidthOfMulticolCell(cell, new_width);
1041 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1042 cell_info[row][column1].width = width;
1043 tmp = calculate_width_of_column_NMC(column1);
1045 recalculateMulticolumnsOfColumn(column1);
1048 for (col_type i = 0; i < columnCount(); ++i)
1049 calculate_width_of_column(i);
1054 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1057 if (!isMultiColumn(cell) || onlycolumn)
1058 column_info[cellColumn(cell)].alignment = align;
1060 cellinfo_of_cell(cell).alignment = align;
1064 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1067 if (!isMultiColumn(cell) || onlycolumn)
1068 column_info[cellColumn(cell)].valignment = align;
1070 cellinfo_of_cell(cell).valignment = align;
1077 * Allow line and paragraph breaks for fixed width cells or disallow them,
1078 * merge cell paragraphs and reset layout to standard for variable width
1081 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1083 inset->setAutoBreakRows(fixedWidth);
1087 // merge all paragraphs to one
1088 BufferParams const & bp = cur.bv().buffer().params();
1089 while (inset->paragraphs().size() > 1)
1090 mergeParagraph(bp, inset->paragraphs(), 0);
1094 // undo information has already been recorded
1095 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1096 bp.getTextClass().emptyLayoutName());
1103 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1104 Length const & width)
1106 col_type const j = cellColumn(cell);
1108 column_info[j].p_width = width;
1109 for (row_type i = 0; i < rowCount(); ++i) {
1110 idx_type const cell = cellIndex(i, j);
1111 // because of multicolumns
1112 toggleFixedWidth(cur, getCellInset(cell).get(),
1113 !getPWidth(cell).zero());
1115 // cur paragraph can become invalid after paragraphs were merged
1116 if (cur.pit() > cur.lastpit())
1117 cur.pit() = cur.lastpit();
1118 // cur position can become invalid after newlines were removed
1119 if (cur.pos() > cur.lastpos())
1120 cur.pos() = cur.lastpos();
1124 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1125 Length const & width)
1127 if (!isMultiColumn(cell))
1130 cellinfo_of_cell(cell).p_width = width;
1131 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1132 // cur paragraph can become invalid after paragraphs were merged
1133 if (cur.pit() > cur.lastpit())
1134 cur.pit() = cur.lastpit();
1135 // cur position can become invalid after newlines were removed
1136 if (cur.pos() > cur.lastpos())
1137 cur.pos() = cur.lastpos();
1142 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1143 Tabular::Feature what)
1145 if (what == SET_SPECIAL_MULTI)
1146 cellinfo_of_cell(cell).align_special = special;
1148 column_info[cellColumn(cell)].align_special = special;
1152 void Tabular::setAllLines(idx_type cell, bool line)
1154 setTopLine(cell, line);
1155 setBottomLine(cell, line);
1156 setRightLine(cell, line);
1157 setLeftLine(cell, line);
1161 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1163 row_type const row = cellRow(cell);
1164 if (wholerow || !isMultiColumn(cell))
1165 row_info[row].top_line = line;
1167 cellinfo_of_cell(cell).top_line = line;
1171 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1173 if (wholerow || !isMultiColumn(cell))
1174 row_info[cellRow(cell)].bottom_line = line;
1176 cellinfo_of_cell(cell).bottom_line = line;
1180 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1182 if (wholecolumn || !isMultiColumn(cell))
1183 column_info[cellColumn(cell)].left_line = line;
1185 cellinfo_of_cell(cell).left_line = line;
1189 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1191 if (wholecolumn || !isMultiColumn(cell))
1192 column_info[cellRightColumn(cell)].right_line = line;
1194 cellinfo_of_cell(cell).right_line = line;
1198 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1200 if (!onlycolumn && isMultiColumn(cell))
1201 return cellinfo_of_cell(cell).alignment;
1202 return column_info[cellColumn(cell)].alignment;
1207 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1209 if (!onlycolumn && isMultiColumn(cell))
1210 return cellinfo_of_cell(cell).valignment;
1211 return column_info[cellColumn(cell)].valignment;
1215 Length const Tabular::getPWidth(idx_type cell) const
1217 if (isMultiColumn(cell))
1218 return cellinfo_of_cell(cell).p_width;
1219 return column_info[cellColumn(cell)].p_width;
1223 Length const Tabular::getColumnPWidth(idx_type cell) const
1225 return column_info[cellColumn(cell)].p_width;
1229 Length const Tabular::getMColumnPWidth(idx_type cell) const
1231 if (isMultiColumn(cell))
1232 return cellinfo_of_cell(cell).p_width;
1237 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1239 if (what == SET_SPECIAL_MULTI)
1240 return cellinfo_of_cell(cell).align_special;
1241 return column_info[cellColumn(cell)].align_special;
1245 int Tabular::cellWidth(idx_type cell) const
1247 row_type const row = cellRow(cell);
1248 col_type const column1 = cellColumn(cell);
1249 col_type const column2 = cellRightColumn(cell);
1251 for (col_type i = column1; i <= column2; ++i)
1252 result += cell_info[row][i].width;
1257 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1261 switch (getAlignment(cell)) {
1262 case LYX_ALIGN_CENTER:
1263 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1265 case LYX_ALIGN_RIGHT:
1266 x += columnWidth(cell) - cellWidth(cell);
1267 // + getAdditionalWidth(cell);
1270 // LYX_ALIGN_LEFT: nothing :-)
1274 // the LaTeX Way :-(
1280 bool Tabular::isFirstCellInRow(idx_type cell) const
1282 return cellColumn(cell) == 0;
1286 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1288 if (row > rowCount() - 1)
1289 row = rowCount() - 1;
1290 return cell_info[row][0].cellno;
1294 bool Tabular::isLastCellInRow(idx_type cell) const
1296 return cellRightColumn(cell) == columnCount() - 1;
1300 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1302 if (row > rowCount() - 1)
1303 row = rowCount() - 1;
1304 return cell_info[row][columnCount() - 1].cellno;
1308 void Tabular::calculate_width_of_column(col_type column)
1311 for (row_type i = 0; i < rowCount(); ++i)
1312 maximum = max(cell_info[i][column].width, maximum);
1313 column_info[column].width = maximum;
1318 // Calculate the columns regarding ONLY the normal cells and if this
1319 // column is inside a multicolumn cell then use it only if its the last
1320 // column of this multicolumn cell as this gives an added width to the
1321 // column, all the rest should be adapted!
1323 bool Tabular::calculate_width_of_column_NMC(col_type column)
1325 int const old_column_width = column_info[column].width;
1327 for (row_type i = 0; i < rowCount(); ++i) {
1328 idx_type cell = cellIndex(i, column);
1329 bool ismulti = isMultiColumnReal(cell);
1330 if ((!ismulti || column == cellRightColumn(cell)) &&
1331 cell_info[i][column].width > max)
1333 max = cell_info[i][column].width;
1336 column_info[column].width = max;
1337 return column_info[column].width != old_column_width;
1341 Tabular::row_type Tabular::cellRow(idx_type cell) const
1343 if (cell >= cellCount())
1344 return rowCount() - 1;
1347 return rowofcell[cell];
1351 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1353 if (cell >= cellCount())
1354 return columnCount() - 1;
1357 return columnofcell[cell];
1361 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1363 row_type const row = cellRow(cell);
1364 col_type column = cellColumn(cell);
1365 while (column < columnCount() - 1 &&
1366 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1372 void Tabular::write(Buffer const & buf, ostream & os) const
1376 << write_attribute("version", 3)
1377 << write_attribute("rows", rowCount())
1378 << write_attribute("columns", columnCount())
1380 // global longtable options
1382 << write_attribute("rotate", rotate)
1383 << write_attribute("booktabs", use_booktabs)
1384 << write_attribute("islongtable", is_long_tabular)
1385 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1386 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1387 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1388 << write_attribute("headTopDL", endhead.topDL)
1389 << write_attribute("headBottomDL", endhead.bottomDL)
1390 << write_attribute("footTopDL", endfoot.topDL)
1391 << write_attribute("footBottomDL", endfoot.bottomDL)
1392 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1393 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1394 << write_attribute("lastFootEmpty", endlastfoot.empty)
1396 for (col_type j = 0; j < columnCount(); ++j) {
1398 << write_attribute("alignment", column_info[j].alignment)
1399 << write_attribute("valignment", column_info[j].valignment)
1400 << write_attribute("leftline", column_info[j].left_line)
1401 << write_attribute("rightline", column_info[j].right_line)
1402 << write_attribute("width", column_info[j].p_width.asString())
1403 << write_attribute("special", column_info[j].align_special)
1406 for (row_type i = 0; i < rowCount(); ++i) {
1408 << write_attribute("topline", row_info[i].top_line)
1409 << write_attribute("bottomline", row_info[i].bottom_line);
1410 static const string def("default");
1411 if (row_info[i].top_space_default)
1412 os << write_attribute("topspace", def);
1414 os << write_attribute("topspace", row_info[i].top_space);
1415 if (row_info[i].bottom_space_default)
1416 os << write_attribute("bottomspace", def);
1418 os << write_attribute("bottomspace", row_info[i].bottom_space);
1419 if (row_info[i].interline_space_default)
1420 os << write_attribute("interlinespace", def);
1422 os << write_attribute("interlinespace", row_info[i].interline_space);
1423 os << write_attribute("endhead", row_info[i].endhead)
1424 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1425 << write_attribute("endfoot", row_info[i].endfoot)
1426 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1427 << write_attribute("newpage", row_info[i].newpage)
1429 for (col_type j = 0; j < columnCount(); ++j) {
1431 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1432 << write_attribute("alignment", cell_info[i][j].alignment)
1433 << write_attribute("valignment", cell_info[i][j].valignment)
1434 << write_attribute("topline", cell_info[i][j].top_line)
1435 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1436 << write_attribute("leftline", cell_info[i][j].left_line)
1437 << write_attribute("rightline", cell_info[i][j].right_line)
1438 << write_attribute("rotate", cell_info[i][j].rotate)
1439 << write_attribute("usebox", cell_info[i][j].usebox)
1440 << write_attribute("width", cell_info[i][j].p_width)
1441 << write_attribute("special", cell_info[i][j].align_special)
1443 os << "\\begin_inset ";
1444 cell_info[i][j].inset->write(buf, os);
1445 os << "\n\\end_inset\n"
1450 os << "</lyxtabular>\n";
1454 void Tabular::read(Buffer const & buf, Lexer & lex)
1457 istream & is = lex.getStream();
1459 l_getline(is, line);
1460 if (!prefixIs(line, "<lyxtabular ")
1461 && !prefixIs(line, "<Tabular ")) {
1462 BOOST_ASSERT(false);
1467 if (!getTokenValue(line, "version", version))
1469 BOOST_ASSERT(version >= 2);
1472 if (!getTokenValue(line, "rows", rows_arg))
1475 if (!getTokenValue(line, "columns", columns_arg))
1477 init(buf.params(), rows_arg, columns_arg);
1478 l_getline(is, line);
1479 if (!prefixIs(line, "<features")) {
1480 lyxerr << "Wrong tabular format (expected <features ...> got"
1481 << line << ')' << endl;
1484 getTokenValue(line, "rotate", rotate);
1485 getTokenValue(line, "booktabs", use_booktabs);
1486 getTokenValue(line, "islongtable", is_long_tabular);
1487 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1488 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1489 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1490 getTokenValue(line, "headTopDL", endhead.topDL);
1491 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1492 getTokenValue(line, "footTopDL", endfoot.topDL);
1493 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1494 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1495 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1496 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1498 for (col_type j = 0; j < columnCount(); ++j) {
1500 if (!prefixIs(line,"<column")) {
1501 lyxerr << "Wrong tabular format (expected <column ...> got"
1502 << line << ')' << endl;
1505 getTokenValue(line, "alignment", column_info[j].alignment);
1506 getTokenValue(line, "valignment", column_info[j].valignment);
1507 getTokenValue(line, "leftline", column_info[j].left_line);
1508 getTokenValue(line, "rightline", column_info[j].right_line);
1509 getTokenValue(line, "width", column_info[j].p_width);
1510 getTokenValue(line, "special", column_info[j].align_special);
1513 for (row_type i = 0; i < rowCount(); ++i) {
1514 l_getline(is, line);
1515 if (!prefixIs(line, "<row")) {
1516 lyxerr << "Wrong tabular format (expected <row ...> got"
1517 << line << ')' << endl;
1520 getTokenValue(line, "topline", row_info[i].top_line);
1521 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1522 getTokenValue(line, "topspace", row_info[i].top_space,
1523 row_info[i].top_space_default);
1524 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1525 row_info[i].bottom_space_default);
1526 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1527 row_info[i].interline_space_default);
1528 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1529 getTokenValue(line, "endhead", row_info[i].endhead);
1530 getTokenValue(line, "endfoot", row_info[i].endfoot);
1531 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1532 getTokenValue(line, "newpage", row_info[i].newpage);
1533 for (col_type j = 0; j < columnCount(); ++j) {
1534 l_getline(is, line);
1535 if (!prefixIs(line, "<cell")) {
1536 lyxerr << "Wrong tabular format (expected <cell ...> got"
1537 << line << ')' << endl;
1540 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1541 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1542 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1543 getTokenValue(line, "topline", cell_info[i][j].top_line);
1544 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1545 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1546 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1547 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1548 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1549 getTokenValue(line, "width", cell_info[i][j].p_width);
1550 getTokenValue(line, "special", cell_info[i][j].align_special);
1551 l_getline(is, line);
1552 if (prefixIs(line, "\\begin_inset")) {
1553 cell_info[i][j].inset->read(buf, lex);
1554 l_getline(is, line);
1556 if (!prefixIs(line, "</cell>")) {
1557 lyxerr << "Wrong tabular format (expected </cell> got"
1558 << line << ')' << endl;
1562 l_getline(is, line);
1563 if (!prefixIs(line, "</row>")) {
1564 lyxerr << "Wrong tabular format (expected </row> got"
1565 << line << ')' << endl;
1569 while (!prefixIs(line, "</lyxtabular>")) {
1570 l_getline(is, line);
1576 bool Tabular::isMultiColumn(idx_type cell) const
1578 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1582 bool Tabular::isMultiColumnReal(idx_type cell) const
1584 return cellColumn(cell) != cellRightColumn(cell) &&
1585 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1589 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1591 return cell_info[cellRow(cell)][cellColumn(cell)];
1595 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1598 cellstruct & cs = cellinfo_of_cell(cell);
1599 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1600 cs.alignment = column_info[cellColumn(cell)].alignment;
1601 cs.top_line = row_info[cellRow(cell)].top_line;
1602 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1603 cs.left_line = column_info[cellColumn(cell)].left_line;
1604 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1605 for (idx_type i = 1; i < number; ++i) {
1606 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1607 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1608 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1615 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1617 row_type const row = cellRow(cell);
1618 col_type column = cellColumn(cell);
1619 idx_type result = 1;
1621 while (column < columnCount() &&
1622 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1631 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1633 row_type const row = cellRow(cell);
1634 col_type column = cellColumn(cell);
1636 idx_type result = 0;
1638 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1639 cell_info[row][column].multicolumn = CELL_NORMAL;
1641 while (column < columnCount() &&
1642 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1644 cell_info[row][column].multicolumn = CELL_NORMAL;
1654 void Tabular::setBookTabs(bool what)
1656 use_booktabs = what;
1660 bool Tabular::useBookTabs() const
1662 return use_booktabs;
1666 void Tabular::setLongTabular(bool what)
1668 is_long_tabular = what;
1672 bool Tabular::isLongTabular() const
1674 return is_long_tabular;
1678 void Tabular::setRotateTabular(bool flag)
1684 bool Tabular::getRotateTabular() const
1690 void Tabular::setRotateCell(idx_type cell, bool flag)
1692 cellinfo_of_cell(cell).rotate = flag;
1696 bool Tabular::getRotateCell(idx_type cell) const
1698 return cellinfo_of_cell(cell).rotate;
1702 bool Tabular::needRotating() const
1706 for (row_type i = 0; i < rowCount(); ++i)
1707 for (col_type j = 0; j < columnCount(); ++j)
1708 if (cell_info[i][j].rotate)
1714 bool Tabular::isLastCell(idx_type cell) const
1716 if (cell + 1 < cellCount())
1722 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1724 if (cellRow(cell) > 0)
1725 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1730 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1732 if (cellRow(cell) + 1 < rowCount())
1733 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1738 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1740 if (cellRow(cell) == 0)
1742 if (!isMultiColumn(cell))
1743 return getCellAbove(cell);
1744 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1748 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1750 if (cellRow(cell) + 1 >= rowCount())
1752 if (!isMultiColumn(cell))
1753 return getCellBelow(cell);
1754 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1758 Tabular::idx_type Tabular::cellIndex(row_type row,
1759 col_type column) const
1761 BOOST_ASSERT(column != npos && column < columnCount() &&
1762 row != npos && row < rowCount());
1763 return cell_info[row][column].cellno;
1767 void Tabular::setUsebox(idx_type cell, BoxType type)
1769 cellinfo_of_cell(cell).usebox = type;
1773 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1775 if (column_info[cellColumn(cell)].p_width.zero() &&
1776 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1778 if (cellinfo_of_cell(cell).usebox > 1)
1779 return cellinfo_of_cell(cell).usebox;
1780 return useParbox(cell);
1785 // This are functions used for the longtable support
1787 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1793 row_info[row].endfirsthead = flag;
1797 row_info[row].endhead = flag;
1802 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1805 hd.set = haveLTHead();
1806 return row_info[row].endhead;
1810 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1813 hd.set = haveLTFirstHead();
1814 return row_info[row].endfirsthead;
1818 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1824 row_info[row].endlastfoot = flag;
1828 row_info[row].endfoot = flag;
1833 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1836 fd.set = haveLTFoot();
1837 return row_info[row].endfoot;
1841 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1844 fd.set = haveLTLastFoot();
1845 return row_info[row].endlastfoot;
1849 void Tabular::setLTNewPage(row_type row, bool what)
1851 row_info[row].newpage = what;
1855 bool Tabular::getLTNewPage(row_type row) const
1857 return row_info[row].newpage;
1861 bool Tabular::haveLTHead() const
1863 for (row_type i = 0; i < rowCount(); ++i)
1864 if (row_info[i].endhead)
1870 bool Tabular::haveLTFirstHead() const
1872 if (endfirsthead.empty)
1874 for (row_type i = 0; i < rowCount(); ++i)
1875 if (row_info[i].endfirsthead)
1881 bool Tabular::haveLTFoot() const
1883 for (row_type i = 0; i < rowCount(); ++i)
1884 if (row_info[i].endfoot)
1890 bool Tabular::haveLTLastFoot() const
1892 if (endlastfoot.empty)
1894 for (row_type i = 0; i < rowCount(); ++i)
1895 if (row_info[i].endlastfoot)
1901 // end longtable support functions
1903 void Tabular::setRowAscent(row_type row, int height)
1905 if (row >= rowCount() || row_info[row].ascent == height)
1907 row_info[row].ascent = height;
1911 void Tabular::setRowDescent(row_type row, int height)
1913 if (row >= rowCount() || row_info[row].descent == height)
1915 row_info[row].descent = height;
1919 int Tabular::rowAscent(row_type row) const
1921 if (row >= rowCount())
1923 return row_info[row].ascent;
1927 int Tabular::rowDescent(row_type row) const
1929 BOOST_ASSERT(row < rowCount());
1930 return row_info[row].descent;
1934 int Tabular::height() const
1937 for (row_type row = 0; row < rowCount(); ++row)
1938 height += rowAscent(row) + rowDescent(row) +
1939 getAdditionalHeight(row);
1944 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1946 BOOST_ASSERT(row < rowCount());
1947 BOOST_ASSERT(column < columnCount());
1948 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1952 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1954 // FIXME: assert or return 0 as in TeXBottomHLine()?
1955 BOOST_ASSERT(row != npos);
1956 BOOST_ASSERT(row < rowCount());
1958 idx_type const fcell = getFirstCellInRow(row);
1959 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1962 for (idx_type i = fcell; i < n; ++i) {
1966 if (use_booktabs && row == 0) {
1969 } else if (tmp == n - fcell) {
1970 os << (use_booktabs ? "\\midrule " : "\\hline ");
1972 for (idx_type i = fcell; i < n; ++i) {
1974 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1975 << cellColumn(i) + 1
1977 << cellRightColumn(i) + 1
1989 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1991 // FIXME: return 0 or assert as in TeXTopHLine()?
1992 if (row == npos || row >= rowCount())
1995 idx_type const fcell = getFirstCellInRow(row);
1996 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1999 for (idx_type i = fcell; i < n; ++i) {
2003 if (use_booktabs && row == rowCount() - 1) {
2004 if (bottomLine(fcell))
2005 os << "\\bottomrule";
2006 } else if (tmp == n - fcell) {
2007 os << (use_booktabs ? "\\midrule" : "\\hline");
2009 for (idx_type i = fcell; i < n; ++i) {
2010 if (bottomLine(i)) {
2011 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2012 << cellColumn(i) + 1
2014 << cellRightColumn(i) + 1
2026 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2030 if (getRotateCell(cell)) {
2031 os << "\\begin{sideways}\n";
2034 if (isMultiColumn(cell)) {
2035 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2036 if (leftLine(cell) &&
2037 (isFirstCellInRow(cell) ||
2038 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2039 !rightLine(cell - 1, true))))
2041 if (!cellinfo_of_cell(cell).align_special.empty()) {
2042 os << cellinfo_of_cell(cell).align_special;
2044 if (!getPWidth(cell).zero()) {
2045 switch (getVAlignment(cell)) {
2046 case LYX_VALIGN_TOP:
2049 case LYX_VALIGN_MIDDLE:
2052 case LYX_VALIGN_BOTTOM:
2057 << from_ascii(getPWidth(cell).asLatexString())
2060 switch (getAlignment(cell)) {
2061 case LYX_ALIGN_LEFT:
2064 case LYX_ALIGN_RIGHT:
2071 } // end if else !getPWidth
2072 } // end if else !cellinfo_of_cell
2073 if (rightLine(cell))
2075 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2080 if (getUsebox(cell) == BOX_PARBOX) {
2082 switch (getVAlignment(cell)) {
2083 case LYX_VALIGN_TOP:
2086 case LYX_VALIGN_MIDDLE:
2089 case LYX_VALIGN_BOTTOM:
2093 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2095 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2096 os << "\\begin{minipage}[";
2097 switch (getVAlignment(cell)) {
2098 case LYX_VALIGN_TOP:
2101 case LYX_VALIGN_MIDDLE:
2104 case LYX_VALIGN_BOTTOM:
2108 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2116 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2121 if (getUsebox(cell) == BOX_PARBOX)
2123 else if (getUsebox(cell) == BOX_MINIPAGE) {
2124 os << "%\n\\end{minipage}";
2127 if (isMultiColumn(cell)) {
2130 if (getRotateCell(cell)) {
2131 os << "%\n\\end{sideways}";
2138 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2139 OutputParams const & runparams) const
2141 if (!is_long_tabular)
2145 // output header info
2147 if (endhead.topDL) {
2151 for (row_type i = 0; i < rowCount(); ++i) {
2152 if (row_info[i].endhead) {
2153 ret += TeXRow(os, i, buf, runparams);
2156 if (endhead.bottomDL) {
2160 os << "\\endhead\n";
2162 if (endfirsthead.empty) {
2163 os << "\\endfirsthead\n";
2167 // output firstheader info
2168 if (haveLTFirstHead()) {
2169 if (endfirsthead.topDL) {
2173 for (row_type i = 0; i < rowCount(); ++i) {
2174 if (row_info[i].endfirsthead) {
2175 ret += TeXRow(os, i, buf, runparams);
2178 if (endfirsthead.bottomDL) {
2182 os << "\\endfirsthead\n";
2185 // output footer info
2187 if (endfoot.topDL) {
2191 for (row_type i = 0; i < rowCount(); ++i) {
2192 if (row_info[i].endfoot) {
2193 ret += TeXRow(os, i, buf, runparams);
2196 if (endfoot.bottomDL) {
2200 os << "\\endfoot\n";
2202 if (endlastfoot.empty) {
2203 os << "\\endlastfoot\n";
2207 // output lastfooter info
2208 if (haveLTLastFoot()) {
2209 if (endlastfoot.topDL) {
2213 for (row_type i = 0; i < rowCount(); ++i) {
2214 if (row_info[i].endlastfoot) {
2215 ret += TeXRow(os, i, buf, runparams);
2218 if (endlastfoot.bottomDL) {
2222 os << "\\endlastfoot\n";
2229 bool Tabular::isValidRow(row_type row) const
2231 if (!is_long_tabular)
2233 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2234 !row_info[row].endfoot && !row_info[row].endlastfoot;
2238 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2239 OutputParams const & runparams) const
2241 idx_type cell = cellIndex(i, 0);
2242 int ret = TeXTopHLine(os, i);
2243 if (row_info[i].top_space_default) {
2245 os << "\\addlinespace\n";
2247 os << "\\noalign{\\vskip\\doublerulesep}\n";
2249 } else if(!row_info[i].top_space.zero()) {
2251 os << "\\addlinespace["
2252 << from_ascii(row_info[i].top_space.asLatexString())
2255 os << "\\noalign{\\vskip"
2256 << from_ascii(row_info[i].top_space.asLatexString())
2262 for (col_type j = 0; j < columnCount(); ++j) {
2263 if (isPartOfMultiColumn(i, j))
2265 ret += TeXCellPreamble(os, cell);
2266 shared_ptr<InsetText> inset = getCellInset(cell);
2268 Paragraph const & par = inset->paragraphs().front();
2269 bool rtl = par.isRTL(buf.params())
2271 && getPWidth(cell).zero();
2274 if (par.getParLanguage(buf.params())->lang() ==
2277 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2279 // currently, remaning RTL languages are arabic_arabtex and hebrew
2283 ret += inset->latex(buf, os, runparams);
2287 ret += TeXCellPostamble(os, cell);
2288 if (!isLastCellInRow(cell)) { // not last cell in row
2293 os << "\\tabularnewline";
2294 if (row_info[i].bottom_space_default) {
2296 os << "\\addlinespace";
2298 os << "[\\doublerulesep]";
2299 } else if (!row_info[i].bottom_space.zero()) {
2301 os << "\\addlinespace";
2303 << from_ascii(row_info[i].bottom_space.asLatexString())
2308 ret += TeXBottomHLine(os, i);
2309 if (row_info[i].interline_space_default) {
2311 os << "\\addlinespace\n";
2313 os << "\\noalign{\\vskip\\doublerulesep}\n";
2315 } else if (!row_info[i].interline_space.zero()) {
2317 os << "\\addlinespace["
2318 << from_ascii(row_info[i].interline_space.asLatexString())
2321 os << "\\noalign{\\vskip"
2322 << from_ascii(row_info[i].interline_space.asLatexString())
2330 int Tabular::latex(Buffer const & buf, odocstream & os,
2331 OutputParams const & runparams) const
2335 //+---------------------------------------------------------------------
2336 //+ first the opening preamble +
2337 //+---------------------------------------------------------------------
2340 os << "\\begin{sideways}\n";
2343 if (is_long_tabular)
2344 os << "\\begin{longtable}{";
2346 os << "\\begin{tabular}{";
2347 for (col_type i = 0; i < columnCount(); ++i) {
2348 if (!use_booktabs && column_info[i].left_line)
2350 if (!column_info[i].align_special.empty()) {
2351 os << column_info[i].align_special;
2353 if (!column_info[i].p_width.zero()) {
2354 switch (column_info[i].alignment) {
2355 case LYX_ALIGN_LEFT:
2356 os << ">{\\raggedright}";
2358 case LYX_ALIGN_RIGHT:
2359 os << ">{\\raggedleft}";
2361 case LYX_ALIGN_CENTER:
2362 os << ">{\\centering}";
2364 case LYX_ALIGN_NONE:
2365 case LYX_ALIGN_BLOCK:
2366 case LYX_ALIGN_LAYOUT:
2367 case LYX_ALIGN_SPECIAL:
2371 switch (column_info[i].valignment) {
2372 case LYX_VALIGN_TOP:
2375 case LYX_VALIGN_MIDDLE:
2378 case LYX_VALIGN_BOTTOM:
2383 << from_ascii(column_info[i].p_width.asLatexString())
2386 switch (column_info[i].alignment) {
2387 case LYX_ALIGN_LEFT:
2390 case LYX_ALIGN_RIGHT:
2397 } // end if else !column_info[i].p_width
2398 } // end if else !column_info[i].align_special
2399 if (!use_booktabs && column_info[i].right_line)
2405 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2407 //+---------------------------------------------------------------------
2408 //+ the single row and columns (cells) +
2409 //+---------------------------------------------------------------------
2411 for (row_type i = 0; i < rowCount(); ++i) {
2412 if (isValidRow(i)) {
2413 ret += TeXRow(os, i, buf, runparams);
2414 if (is_long_tabular && row_info[i].newpage) {
2415 os << "\\newpage\n";
2421 //+---------------------------------------------------------------------
2422 //+ the closing of the tabular +
2423 //+---------------------------------------------------------------------
2425 if (is_long_tabular)
2426 os << "\\end{longtable}";
2428 os << "\\end{tabular}";
2430 os << "\n\\end{sideways}";
2438 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2439 OutputParams const & runparams) const
2442 idx_type cell = getFirstCellInRow(row);
2445 for (col_type j = 0; j < columnCount(); ++j) {
2446 if (isPartOfMultiColumn(row, j))
2449 os << "<entry align=\"";
2450 switch (getAlignment(cell)) {
2451 case LYX_ALIGN_LEFT:
2454 case LYX_ALIGN_RIGHT:
2462 os << "\" valign=\"";
2463 switch (getVAlignment(cell)) {
2464 case LYX_VALIGN_TOP:
2467 case LYX_VALIGN_BOTTOM:
2470 case LYX_VALIGN_MIDDLE:
2475 if (isMultiColumn(cell)) {
2476 os << " namest=\"col" << j << "\" ";
2477 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2481 ret += getCellInset(cell)->docbook(buf, os, runparams);
2490 int Tabular::docbook(Buffer const & buf, odocstream & os,
2491 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(buf, 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(buf, 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(buf, 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(Buffer const & buf, 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(buf, 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(Buffer const & buf, 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(buf, 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(buf, 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(buf, os, runparams,
2772 cell, i, j, clen, onlydata);
2777 os << docstring(depth * 2, ' ');
2778 if (plaintextBottomHLine(os, i, clen))
2779 os << docstring(depth * 2, ' ');
2785 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2787 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2791 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2792 col_type column) const
2794 return cell_info[row][column].inset;
2798 void Tabular::setCellInset(row_type row, col_type column,
2799 shared_ptr<InsetText> ins) const
2801 cell_info[row][column].inset = ins;
2806 Tabular::getCellFromInset(Inset const * inset) const
2808 // is this inset part of the tabular?
2810 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2811 BOOST_ASSERT(false);
2814 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2815 if (getCellInset(cell).get() == inset) {
2816 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2817 << "cell=" << cell);
2821 // We should have found a cell at this point
2822 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2823 << inset << " not found!" << endl;
2824 BOOST_ASSERT(false);
2830 void Tabular::validate(LaTeXFeatures & features) const
2832 features.require("NeedTabularnewline");
2834 features.require("booktabs");
2835 if (isLongTabular())
2836 features.require("longtable");
2838 features.require("rotating");
2839 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2840 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2841 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2842 features.require("array");
2843 getCellInset(cell)->validate(features);
2848 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2850 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2851 ParagraphList::const_iterator cit = parlist.begin();
2852 ParagraphList::const_iterator end = parlist.end();
2854 for (; cit != end; ++cit)
2855 for (int i = 0; i < cit->size(); ++i)
2856 if (cit->isNewline(i))
2863 /////////////////////////////////////////////////////////////////////
2867 /////////////////////////////////////////////////////////////////////
2869 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2871 : tabular(buf.params(), max(rows, row_type(1)),
2872 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2876 InsetTabular::InsetTabular(InsetTabular const & tab)
2877 : Inset(tab), tabular(tab.tabular),
2878 buffer_(tab.buffer_), scx_(0)
2882 InsetTabular::~InsetTabular()
2884 InsetTabularMailer(*this).hideDialog();
2888 Inset * InsetTabular::clone() const
2890 return new InsetTabular(*this);
2894 Buffer const & InsetTabular::buffer() const
2900 void InsetTabular::buffer(Buffer const * b)
2906 bool InsetTabular::insetAllowed(InsetCode code) const
2908 if (code == MATHMACRO_CODE)
2915 void InsetTabular::write(Buffer const & buf, ostream & os) const
2917 os << "Tabular" << endl;
2918 tabular.write(buf, os);
2922 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2924 bool const old_format = (lex.getString() == "\\LyXTable");
2926 tabular.read(buf, lex);
2932 string token = lex.getString();
2933 while (lex && token != "\\end_inset") {
2935 token = lex.getString();
2938 lex.printError("Missing \\end_inset at this point. "
2944 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2946 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2947 // mi.base.textwidth << "\n";
2949 lyxerr << "InsetTabular::metrics: need bv" << endl;
2950 BOOST_ASSERT(false);
2954 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2957 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2958 if (tabular.isPartOfMultiColumn(i, j))
2959 // Multicolumn cell, but not first one
2964 if (tabular.cell_info[i][j].multicolumn ==
2965 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2966 p_width = tabular.cellinfo_of_cell(cell).p_width;
2968 p_width = tabular.column_info[j].p_width;
2969 if (!p_width.zero())
2970 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2971 tabular.getCellInset(cell)->metrics(m, dim);
2972 if (!p_width.zero())
2973 dim.wid = m.base.textwidth;
2974 tabular.setCellWidth(cell, dim.wid);
2975 if (p_width.zero()) {
2976 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2977 // FIXME there must be a way to get rid of
2978 // the second metrics call
2979 tabular.getCellInset(cell)->metrics(m, dim);
2981 maxAsc = max(maxAsc, dim.asc);
2982 maxDesc = max(maxDesc, dim.des);
2985 int const top_space = tabular.row_info[i].top_space_default ?
2986 default_line_space :
2987 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2988 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2989 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2990 default_line_space :
2991 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2992 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2995 dim.asc = tabular.rowAscent(0);
2996 dim.des = tabular.height() - dim.asc;
2997 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3001 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3003 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3004 BufferView * bv = pi.base.bv;
3006 // FIXME: As the full backrgound is painted in drawSelection(),
3007 // we have no choice but to do a full repaint for the Text cells.
3008 pi.full_repaint = true;
3010 resetPos(bv->cursor());
3013 x += ADD_TO_TABULAR_WIDTH;
3015 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3018 first_visible_cell = Tabular::npos;
3019 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3021 int const a = tabular.rowAscent(i);
3022 int const d = tabular.rowDescent(i);
3023 idx = tabular.cellIndex(i, 0);
3024 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3025 if (tabular.isPartOfMultiColumn(i, j))
3027 if (first_visible_cell == Tabular::npos)
3028 first_visible_cell = idx;
3030 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3031 // Cache the Inset position.
3032 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3033 if (nx + tabular.columnWidth(idx) < 0
3034 || nx > bv->workWidth()
3036 || y - a > bv->workHeight()) {
3037 pi.pain.setDrawingEnabled(false);
3038 cell(idx)->draw(pi, cx, y);
3039 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3040 pi.pain.setDrawingEnabled(original_drawing_state);
3042 cell(idx)->draw(pi, cx, y);
3043 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3045 nx += tabular.columnWidth(idx);
3049 if (i + 1 < tabular.rowCount())
3050 y += d + tabular.rowAscent(i + 1) +
3051 tabular.getAdditionalHeight(i + 1);
3056 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3058 Cursor & cur = pi.base.bv->cursor();
3060 x += scx_ + ADD_TO_TABULAR_WIDTH;
3062 // FIXME: it is wrong to completely paint the background
3063 // if we want to do single row painting.
3065 // Paint background of current tabular
3066 int const w = tabular.width();
3067 int const h = tabular.height();
3068 int yy = y - tabular.rowAscent(0);
3069 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3071 if (!cur.selection())
3073 if (&cur.inset() != this)
3079 if (tablemode(cur)) {
3082 getSelection(cur, rs, re, cs, ce);
3083 y -= tabular.rowAscent(0);
3084 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3085 int const a = tabular.rowAscent(j);
3086 int const h = a + tabular.rowDescent(j);
3088 y += tabular.getAdditionalHeight(j);
3089 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3090 if (tabular.isPartOfMultiColumn(j, i))
3092 idx_type const cell =
3093 tabular.cellIndex(j, i);
3094 int const w = tabular.columnWidth(cell);
3095 if (i >= cs && i <= ce && j >= rs && j <= re)
3096 pi.pain.fillRectangle(xx, y, w, h,
3104 x += getCellXPos(cur.idx());
3105 x += tabular.getBeginningOfTextInCell(cur.idx());
3106 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3111 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3112 row_type row, idx_type cell, bool erased) const
3114 int x2 = x + tabular.columnWidth(cell);
3115 bool on_off = false;
3116 ColorCode col = Color_tabularline;
3117 ColorCode onoffcol = Color_tabularonoffline;
3120 col = Color_deletedtext;
3121 onoffcol = Color_deletedtext;
3124 if (!tabular.topAlreadyDrawn(cell)) {
3125 on_off = !tabular.topLine(cell);
3126 pain.line(x, y - tabular.rowAscent(row),
3127 x2, y - tabular.rowAscent(row),
3128 on_off ? onoffcol : col,
3129 on_off ? Painter::line_onoffdash : Painter::line_solid);
3131 on_off = !tabular.bottomLine(cell);
3132 pain.line(x, y + tabular.rowDescent(row),
3133 x2, y + tabular.rowDescent(row),
3134 on_off ? onoffcol : col,
3135 on_off ? Painter::line_onoffdash : Painter::line_solid);
3136 if (!tabular.leftAlreadyDrawn(cell)) {
3137 on_off = !tabular.leftLine(cell);
3138 pain.line(x, y - tabular.rowAscent(row),
3139 x, y + tabular.rowDescent(row),
3140 on_off ? onoffcol : col,
3141 on_off ? Painter::line_onoffdash : Painter::line_solid);
3143 on_off = !tabular.rightLine(cell);
3144 pain.line(x2 - tabular.getAdditionalWidth(cell),
3145 y - tabular.rowAscent(row),
3146 x2 - tabular.getAdditionalWidth(cell),
3147 y + tabular.rowDescent(row),
3148 on_off ? onoffcol : col,
3149 on_off ? Painter::line_onoffdash : Painter::line_solid);
3153 docstring const InsetTabular::editMessage() const
3155 return _("Opened table");
3159 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3161 //lyxerr << "InsetTabular::edit: " << this << endl;
3163 cur.selection() = false;
3166 if (isRightToLeft(cur))
3167 cur.idx() = tabular.getLastCellInRow(0);
3173 if (isRightToLeft(cur))
3174 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3176 cur.idx() = tabular.cellCount() - 1;
3178 cur.pos() = cur.lastpos(); // FIXME crude guess
3180 // FIXME: this accesses the position cache before it is initialized
3182 //cur.bv().fitCursor();
3186 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3188 // In a longtable, tell captions what the current float is
3189 Counters & cnts = buf.params().getTextClass().counters();
3190 string const saveflt = cnts.current_float();
3191 if (tabular.isLongTabular())
3192 cnts.current_float("table");
3194 ParIterator it2 = it;
3196 size_t const end = it2.nargs();
3197 for ( ; it2.idx() < end; it2.top().forwardIdx())
3198 lyx::updateLabels(buf, it2);
3201 if (tabular.isLongTabular())
3202 cnts.current_float(saveflt);
3206 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3208 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3209 << "\n cur:" << cur);
3210 CursorSlice sl = cur.top();
3211 Cursor & bvcur = cur.bv().cursor();
3213 switch (cmd.action) {
3215 case LFUN_MOUSE_PRESS:
3216 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3218 // do not reset cursor/selection if we have selected
3219 // some cells (bug 2715).
3220 if (cmd.button() == mouse_button::button3
3221 && &bvcur.selBegin().inset() == this
3222 && tablemode(bvcur))
3225 // Let InsetText do it
3226 cell(cur.idx())->dispatch(cur, cmd);
3228 case LFUN_MOUSE_MOTION:
3229 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3230 if (cmd.button() == mouse_button::button1) {
3231 // only accept motions to places not deeper nested than the real anchor
3232 if (bvcur.anchor_.hasPart(cur)) {
3233 // only update if selection changes
3234 if (bvcur.idx() == cur.idx() &&
3235 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3237 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3238 bvcur.setCursor(cur);
3239 bvcur.selection() = true;
3245 case LFUN_MOUSE_RELEASE:
3246 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3247 if (cmd.button() == mouse_button::button3)
3248 InsetTabularMailer(*this).showDialog(&cur.bv());
3251 case LFUN_CELL_BACKWARD:
3253 cur.selection() = false;
3256 case LFUN_CELL_FORWARD:
3258 cur.selection() = false;
3261 case LFUN_CHAR_FORWARD_SELECT:
3262 case LFUN_CHAR_FORWARD:
3263 cell(cur.idx())->dispatch(cur, cmd);
3264 if (!cur.result().dispatched()) {
3266 if (sl == cur.top())
3267 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3273 case LFUN_CHAR_BACKWARD_SELECT:
3274 case LFUN_CHAR_BACKWARD:
3275 cell(cur.idx())->dispatch(cur, cmd);
3276 if (!cur.result().dispatched()) {
3278 if (sl == cur.top())
3279 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3285 case LFUN_CHAR_RIGHT_SELECT:
3286 case LFUN_CHAR_RIGHT:
3287 //FIXME: for visual cursor, really move right
3288 if (isRightToLeft(cur))
3289 lyx::dispatch(FuncRequest(
3290 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3291 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3293 lyx::dispatch(FuncRequest(
3294 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3295 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3298 case LFUN_CHAR_LEFT_SELECT:
3299 case LFUN_CHAR_LEFT:
3300 //FIXME: for visual cursor, really move left
3301 if (isRightToLeft(cur))
3302 lyx::dispatch(FuncRequest(
3303 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3304 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3306 lyx::dispatch(FuncRequest(
3307 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3308 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3311 case LFUN_DOWN_SELECT:
3313 cell(cur.idx())->dispatch(cur, cmd);
3314 cur.dispatched(); // override the cell's decision
3315 if (sl == cur.top())
3316 // if our Text didn't do anything to the cursor
3317 // then we try to put the cursor into the cell below
3318 // setting also the right targetX.
3319 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3320 cur.idx() = tabular.getCellBelow(cur.idx());
3322 TextMetrics const & tm =
3323 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3324 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3326 if (sl == cur.top()) {
3327 // we trick it to go to forward after leaving the
3329 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3334 case LFUN_UP_SELECT:
3336 cell(cur.idx())->dispatch(cur, cmd);
3337 cur.dispatched(); // override the cell's decision
3338 if (sl == cur.top())
3339 // if our Text didn't do anything to the cursor
3340 // then we try to put the cursor into the cell above
3341 // setting also the right targetX.
3342 if (tabular.cellRow(cur.idx()) != 0) {
3343 cur.idx() = tabular.getCellAbove(cur.idx());
3344 cur.pit() = cur.lastpit();
3345 Text const * text = cell(cur.idx())->getText(0);
3346 TextMetrics const & tm = cur.bv().textMetrics(text);
3347 ParagraphMetrics const & pm =
3348 tm.parMetrics(cur.lastpit());
3349 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3351 if (sl == cur.top()) {
3352 cmd = FuncRequest(LFUN_UP);
3357 // case LFUN_SCREEN_DOWN: {
3358 // //if (hasSelection())
3359 // // cur.selection() = false;
3360 // col_type const col = tabular.cellColumn(cur.idx());
3361 // int const t = cur.bv().top_y() + cur.bv().height();
3362 // if (t < yo() + tabular.getHeightOfTabular()) {
3363 // cur.bv().scrollDocView(t);
3364 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3366 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3373 // case LFUN_SCREEN_UP: {
3374 // //if (hasSelection())
3375 // // cur.selection() = false;
3376 // col_type const col = tabular.cellColumn(cur.idx());
3377 // int const t = cur.bv().top_y() + cur.bv().height();
3379 // cur.bv().scrollDocView(t);
3383 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3387 // cur.par() = cur.lastpar();
3388 // cur.pos() = cur.lastpos();
3392 case LFUN_LAYOUT_TABULAR:
3393 InsetTabularMailer(*this).showDialog(&cur.bv());
3396 case LFUN_INSET_DIALOG_UPDATE:
3397 InsetTabularMailer(*this).updateDialog(&cur.bv());
3400 case LFUN_TABULAR_FEATURE:
3401 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3405 // insert file functions
3406 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3407 case LFUN_FILE_INSERT_PLAINTEXT: {
3409 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3410 FileName(to_utf8(cmd.argument())));
3413 cur.recordUndoInset(INSERT_UNDO);
3414 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3415 // content has been replaced,
3416 // so cursor might be invalid
3417 cur.pos() = cur.lastpos();
3418 cur.pit() = cur.lastpit();
3419 bvcur.setCursor(cur);
3426 if (tablemode(cur)) {
3427 if (copySelection(cur)) {
3428 cur.recordUndoInset(DELETE_UNDO);
3433 cell(cur.idx())->dispatch(cur, cmd);
3436 case LFUN_CHAR_DELETE_BACKWARD:
3437 case LFUN_CHAR_DELETE_FORWARD:
3438 if (tablemode(cur)) {
3439 cur.recordUndoInset(DELETE_UNDO);
3443 cell(cur.idx())->dispatch(cur, cmd);
3447 if (!cur.selection())
3449 if (tablemode(cur)) {
3453 cell(cur.idx())->dispatch(cur, cmd);
3456 case LFUN_CLIPBOARD_PASTE:
3457 case LFUN_PRIMARY_SELECTION_PASTE: {
3458 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3459 theClipboard().getAsText() :
3460 theSelection().get();
3463 // pass to InsertPlaintextString, but
3464 // only if we have multi-cell content
3465 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3466 cur.recordUndoInset(INSERT_UNDO);
3467 if (insertPlaintextString(cur.bv(), clip, false)) {
3468 // content has been replaced,
3469 // so cursor might be invalid
3470 cur.pos() = cur.lastpos();
3471 cur.pit() = cur.lastpit();
3472 bvcur.setCursor(cur);
3476 // Let the cell handle normal text
3477 cell(cur.idx())->dispatch(cur, cmd);
3482 if (tabularStackDirty() && theClipboard().isInternal() ||
3483 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3484 cur.recordUndoInset(INSERT_UNDO);
3485 pasteClipboard(cur);
3488 cell(cur.idx())->dispatch(cur, cmd);
3491 case LFUN_FONT_EMPH:
3492 case LFUN_FONT_BOLD:
3493 case LFUN_FONT_ROMAN:
3494 case LFUN_FONT_NOUN:
3495 case LFUN_FONT_ITAL:
3496 case LFUN_FONT_FRAK:
3497 case LFUN_FONT_TYPEWRITER:
3498 case LFUN_FONT_SANS:
3499 case LFUN_FONT_FREE_APPLY:
3500 case LFUN_FONT_FREE_UPDATE:
3501 case LFUN_FONT_SIZE:
3502 case LFUN_FONT_UNDERLINE:
3504 case LFUN_WORD_CAPITALIZE:
3505 case LFUN_WORD_UPCASE:
3506 case LFUN_WORD_LOWCASE:
3507 case LFUN_CHARS_TRANSPOSE:
3508 if (tablemode(cur)) {
3511 getSelection(cur, rs, re, cs, ce);
3512 Cursor tmpcur = cur;
3513 for (row_type i = rs; i <= re; ++i) {
3514 for (col_type j = cs; j <= ce; ++j) {
3515 // cursor follows cell:
3516 tmpcur.idx() = tabular.cellIndex(i, j);
3517 // select this cell only:
3520 tmpcur.resetAnchor();
3521 tmpcur.pit() = tmpcur.lastpit();
3522 tmpcur.pos() = tmpcur.top().lastpos();
3523 tmpcur.setCursor(tmpcur);
3524 tmpcur.setSelection();
3525 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3530 cell(cur.idx())->dispatch(cur, cmd);
3534 // we try to handle this event in the insets dispatch function.
3535 cell(cur.idx())->dispatch(cur, cmd);
3541 // function sets an object as defined in func_status.h:
3542 // states OK, Unknown, Disabled, On, Off.
3543 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3544 FuncStatus & status) const
3546 switch (cmd.action) {
3547 case LFUN_TABULAR_FEATURE: {
3548 int action = Tabular::LAST_ACTION;
3550 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3551 string const tmp = tabularFeature[i].feature;
3552 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3553 action = tabularFeature[i].action;
3557 if (action == Tabular::LAST_ACTION) {
3559 status.unknown(true);
3563 string const argument
3564 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3566 row_type sel_row_start = 0;
3567 row_type sel_row_end = 0;
3568 col_type sel_col_start = 0;
3569 col_type sel_col_end = 0;
3570 Tabular::ltType dummyltt;
3573 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3576 case Tabular::SET_PWIDTH:
3577 case Tabular::SET_MPWIDTH:
3578 case Tabular::SET_SPECIAL_COLUMN:
3579 case Tabular::SET_SPECIAL_MULTI:
3580 case Tabular::APPEND_ROW:
3581 case Tabular::APPEND_COLUMN:
3582 case Tabular::DELETE_ROW:
3583 case Tabular::DELETE_COLUMN:
3584 case Tabular::COPY_ROW:
3585 case Tabular::COPY_COLUMN:
3586 case Tabular::SET_ALL_LINES:
3587 case Tabular::UNSET_ALL_LINES:
3588 case Tabular::SET_TOP_SPACE:
3589 case Tabular::SET_BOTTOM_SPACE:
3590 case Tabular::SET_INTERLINE_SPACE:
3594 case Tabular::MULTICOLUMN:
3595 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3598 case Tabular::M_TOGGLE_LINE_TOP:
3600 case Tabular::TOGGLE_LINE_TOP:
3601 status.setOnOff(tabular.topLine(cur.idx(), flag));
3604 case Tabular::M_TOGGLE_LINE_BOTTOM:
3606 case Tabular::TOGGLE_LINE_BOTTOM:
3607 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3610 case Tabular::M_TOGGLE_LINE_LEFT:
3612 case Tabular::TOGGLE_LINE_LEFT:
3613 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3616 case Tabular::M_TOGGLE_LINE_RIGHT:
3618 case Tabular::TOGGLE_LINE_RIGHT:
3619 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3622 case Tabular::M_ALIGN_LEFT:
3624 case Tabular::ALIGN_LEFT:
3625 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3628 case Tabular::M_ALIGN_RIGHT:
3630 case Tabular::ALIGN_RIGHT:
3631 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3634 case Tabular::M_ALIGN_CENTER:
3636 case Tabular::ALIGN_CENTER:
3637 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3640 case Tabular::ALIGN_BLOCK:
3641 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3642 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3645 case Tabular::M_VALIGN_TOP:
3647 case Tabular::VALIGN_TOP:
3649 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3652 case Tabular::M_VALIGN_BOTTOM:
3654 case Tabular::VALIGN_BOTTOM:
3656 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3659 case Tabular::M_VALIGN_MIDDLE:
3661 case Tabular::VALIGN_MIDDLE:
3663 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3666 case Tabular::SET_LONGTABULAR:
3667 status.setOnOff(tabular.isLongTabular());
3670 case Tabular::UNSET_LONGTABULAR:
3671 status.setOnOff(!tabular.isLongTabular());
3674 case Tabular::TOGGLE_ROTATE_TABULAR:
3675 case Tabular::SET_ROTATE_TABULAR:
3676 status.setOnOff(tabular.getRotateTabular());
3679 case Tabular::UNSET_ROTATE_TABULAR:
3680 status.setOnOff(!tabular.getRotateTabular());
3683 case Tabular::TOGGLE_ROTATE_CELL:
3684 case Tabular::SET_ROTATE_CELL:
3685 status.setOnOff(!oneCellHasRotationState(false,
3686 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3689 case Tabular::UNSET_ROTATE_CELL:
3690 status.setOnOff(!oneCellHasRotationState(true,
3691 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3694 case Tabular::SET_USEBOX:
3695 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3698 case Tabular::SET_LTFIRSTHEAD:
3699 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3702 case Tabular::UNSET_LTFIRSTHEAD:
3703 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3706 case Tabular::SET_LTHEAD:
3707 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3710 case Tabular::UNSET_LTHEAD:
3711 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3714 case Tabular::SET_LTFOOT:
3715 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3718 case Tabular::UNSET_LTFOOT:
3719 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3722 case Tabular::SET_LTLASTFOOT:
3723 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3726 case Tabular::UNSET_LTLASTFOOT:
3727 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3730 case Tabular::SET_LTNEWPAGE:
3731 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3734 case Tabular::SET_BOOKTABS:
3735 status.setOnOff(tabular.useBookTabs());
3738 case Tabular::UNSET_BOOKTABS:
3739 status.setOnOff(!tabular.useBookTabs());
3744 status.enabled(false);
3750 // These are only enabled inside tabular
3751 case LFUN_CELL_BACKWARD:
3752 case LFUN_CELL_FORWARD:
3753 status.enabled(true);
3756 // disable these with multiple cells selected
3757 case LFUN_INSET_INSERT:
3758 case LFUN_TABULAR_INSERT:
3759 case LFUN_FLEX_INSERT:
3760 case LFUN_FLOAT_INSERT:
3761 case LFUN_FLOAT_WIDE_INSERT:
3762 case LFUN_FOOTNOTE_INSERT:
3763 case LFUN_MARGINALNOTE_INSERT:
3764 case LFUN_MATH_INSERT:
3765 case LFUN_MATH_MODE:
3766 case LFUN_MATH_MUTATE:
3767 case LFUN_MATH_DISPLAY:
3768 case LFUN_NOTE_INSERT:
3769 case LFUN_OPTIONAL_INSERT:
3770 case LFUN_BOX_INSERT:
3771 case LFUN_BRANCH_INSERT:
3772 case LFUN_WRAP_INSERT:
3773 case LFUN_ERT_INSERT: {
3774 if (tablemode(cur)) {
3775 status.enabled(false);
3778 return cell(cur.idx())->getStatus(cur, cmd, status);
3781 // disable in non-fixed-width cells
3783 case LFUN_BREAK_PARAGRAPH:
3784 case LFUN_BREAK_PARAGRAPH_SKIP: {
3785 if (tabular.getPWidth(cur.idx()).zero()) {
3786 status.enabled(false);
3789 return cell(cur.idx())->getStatus(cur, cmd, status);
3793 if (tabularStackDirty() && theClipboard().isInternal()) {
3794 status.enabled(true);
3797 return cell(cur.idx())->getStatus(cur, cmd, status);
3799 case LFUN_INSET_MODIFY:
3800 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3801 status.enabled(true);
3807 // we try to handle this event in the insets dispatch function.
3808 return cell(cur.idx())->getStatus(cur, cmd, status);
3813 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3814 OutputParams const & runparams) const
3816 return tabular.latex(buf, os, runparams);
3820 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3821 OutputParams const & runparams) const
3823 os << '\n'; // output table on a new line
3824 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3825 tabular.plaintext(buf, os, runparams, dp, false, 0);
3826 return PLAINTEXT_NEWLINE;
3830 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3831 OutputParams const & runparams) const
3836 // FIXME: Why not pass a proper DocIterator here?
3838 // if the table is inside a float it doesn't need the informaltable
3839 // wrapper. Search for it.
3840 for (master = owner(); master; master = master->owner())
3841 if (master->lyxCode() == FLOAT_CODE)
3846 os << "<informaltable>";
3849 ret += tabular.docbook(buf, os, runparams);
3851 os << "</informaltable>";
3858 void InsetTabular::validate(LaTeXFeatures & features) const
3860 tabular.validate(features);
3864 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3866 return tabular.getCellInset(idx);
3870 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3872 return tabular.getCellInset(idx);
3876 void InsetTabular::cursorPos(BufferView const & bv,
3877 CursorSlice const & sl, bool boundary, int & x, int & y) const
3879 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3881 // y offset correction
3882 int const row = tabular.cellRow(sl.idx());
3883 for (int i = 0; i <= row; ++i) {
3885 y += tabular.rowAscent(i);
3886 y += tabular.getAdditionalHeight(i);
3889 y += tabular.rowDescent(i);
3892 // x offset correction
3893 int const col = tabular.cellColumn(sl.idx());
3894 int idx = tabular.cellIndex(row, 0);
3895 for (int j = 0; j < col; ++j) {
3896 if (tabular.isPartOfMultiColumn(row, j))
3898 x += tabular.columnWidth(idx);
3901 x += tabular.getBeginningOfTextInCell(idx);
3902 x += ADD_TO_TABULAR_WIDTH;
3907 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3911 Inset const & inset = *tabular.getCellInset(cell);
3912 Point o = bv.coordCache().getInsets().xy(&inset);
3913 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3914 int const xend = xbeg + tabular.columnWidth(cell);
3915 row_type const row = tabular.cellRow(cell);
3916 int const ybeg = o.y_ - tabular.rowAscent(row) -
3917 tabular.getAdditionalHeight(row);
3918 int const yend = o.y_ + tabular.rowDescent(row);
3930 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3931 // << " ybeg=" << ybeg << " yend=" << yend
3932 // << " xx=" << xx << " yy=" << yy
3933 // << " dist=" << xx + yy << endl;
3938 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3940 //lyxerr << "InsetTabular::editXY: " << this << endl;
3941 cur.selection() = false;
3943 cur.idx() = getNearestCell(cur.bv(), x, y);
3945 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3949 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3951 cur.idx() = getNearestCell(cur.bv(), x, y);
3952 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3956 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3958 idx_type idx_min = 0;
3959 int dist_min = numeric_limits<int>::max();
3960 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3961 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3962 int const d = dist(bv, i, x, y);
3973 int InsetTabular::getCellXPos(idx_type const cell) const
3977 for (; !tabular.isFirstCellInRow(c); --c)
3980 for (; c < cell; ++c)
3981 lx += tabular.columnWidth(c);
3987 void InsetTabular::resetPos(Cursor & cur) const
3989 BufferView & bv = cur.bv();
3990 int const maxwidth = bv.workWidth();
3992 if (&cur.inset() != this) {
3996 int const X2 = maxwidth;
3997 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3998 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3999 int const x2 = x1 + tabular.columnWidth(cur.idx());
4002 scx_ = X1 + 20 - x1;
4004 scx_ = X2 - 20 - x2;
4009 cur.updateFlags(Update::Force | Update::FitCursor);
4013 void InsetTabular::moveNextCell(Cursor & cur)
4015 if (isRightToLeft(cur)) {
4016 if (tabular.isFirstCellInRow(cur.idx())) {
4017 row_type const row = tabular.cellRow(cur.idx());
4018 if (row == tabular.rowCount() - 1)
4020 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4027 if (tabular.isLastCell(cur.idx()))
4037 void InsetTabular::movePrevCell(Cursor & cur)
4039 if (isRightToLeft(cur)) {
4040 if (tabular.isLastCellInRow(cur.idx())) {
4041 row_type const row = tabular.cellRow(cur.idx());
4044 cur.idx() = tabular.getFirstCellInRow(row);
4045 cur.idx() = tabular.getCellAbove(cur.idx());
4047 if (tabular.isLastCell(cur.idx()))
4052 if (cur.idx() == 0) // first cell
4056 cur.pit() = cur.lastpit();
4057 cur.pos() = cur.lastpos();
4059 // FIXME: this accesses the position cache before it is initialized
4064 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4066 Tabular::Feature action = Tabular::LAST_ACTION;
4069 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4070 string const tmp = tabularFeature[i].feature;
4072 if (tmp == what.substr(0, tmp.length())) {
4073 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4074 //tabularFeatures[i].feature.length()))
4075 action = tabularFeature[i].action;
4079 if (action == Tabular::LAST_ACTION)
4083 ltrim(what.substr(tabularFeature[i].feature.length()));
4084 tabularFeatures(cur, action, val);
4089 static void checkLongtableSpecial(Tabular::ltType & ltt,
4090 string const & special, bool & flag)
4092 if (special == "dl_above") {
4095 } else if (special == "dl_below") {
4096 ltt.bottomDL = flag;
4098 } else if (special == "empty") {
4107 bool InsetTabular::oneCellHasRotationState(bool rotated,
4108 row_type row_start, row_type row_end,
4109 col_type col_start, col_type col_end) const {
4111 for (row_type i = row_start; i <= row_end; ++i) {
4112 for (col_type j = col_start; j <= col_end; ++j) {
4113 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4122 void InsetTabular::tabularFeatures(Cursor & cur,
4123 Tabular::Feature feature, string const & value)
4125 BufferView & bv = cur.bv();
4126 col_type sel_col_start;
4127 col_type sel_col_end;
4128 row_type sel_row_start;
4129 row_type sel_row_end;
4130 bool setLines = false;
4131 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4132 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4136 case Tabular::M_ALIGN_LEFT:
4137 case Tabular::ALIGN_LEFT:
4138 setAlign = LYX_ALIGN_LEFT;
4141 case Tabular::M_ALIGN_RIGHT:
4142 case Tabular::ALIGN_RIGHT:
4143 setAlign = LYX_ALIGN_RIGHT;
4146 case Tabular::M_ALIGN_CENTER:
4147 case Tabular::ALIGN_CENTER:
4148 setAlign = LYX_ALIGN_CENTER;
4151 case Tabular::ALIGN_BLOCK:
4152 setAlign = LYX_ALIGN_BLOCK;
4155 case Tabular::M_VALIGN_TOP:
4156 case Tabular::VALIGN_TOP:
4157 setVAlign = Tabular::LYX_VALIGN_TOP;
4160 case Tabular::M_VALIGN_BOTTOM:
4161 case Tabular::VALIGN_BOTTOM:
4162 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4165 case Tabular::M_VALIGN_MIDDLE:
4166 case Tabular::VALIGN_MIDDLE:
4167 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4174 cur.recordUndoInset(ATOMIC_UNDO);
4176 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4177 row_type const row = tabular.cellRow(cur.idx());
4178 col_type const column = tabular.cellColumn(cur.idx());
4180 Tabular::ltType ltt;
4184 case Tabular::SET_PWIDTH: {
4185 Length const len(value);
4186 tabular.setColumnPWidth(cur, cur.idx(), len);
4188 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4189 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4193 case Tabular::SET_MPWIDTH:
4194 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4197 case Tabular::SET_SPECIAL_COLUMN:
4198 case Tabular::SET_SPECIAL_MULTI:
4199 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4202 case Tabular::APPEND_ROW:
4203 // append the row into the tabular
4204 tabular.appendRow(bv.buffer().params(), cur.idx());
4207 case Tabular::APPEND_COLUMN:
4208 // append the column into the tabular
4209 tabular.appendColumn(bv.buffer().params(), cur.idx());
4210 cur.idx() = tabular.cellIndex(row, column);
4213 case Tabular::DELETE_ROW:
4214 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4215 tabular.deleteRow(sel_row_start);
4216 if (sel_row_start >= tabular.rowCount())
4218 cur.idx() = tabular.cellIndex(sel_row_start, column);
4221 cur.selection() = false;
4224 case Tabular::DELETE_COLUMN:
4225 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4226 tabular.deleteColumn(sel_col_start);
4227 if (sel_col_start >= tabular.columnCount())
4229 cur.idx() = tabular.cellIndex(row, sel_col_start);
4232 cur.selection() = false;
4235 case Tabular::COPY_ROW:
4236 tabular.copyRow(bv.buffer().params(), row);
4239 case Tabular::COPY_COLUMN:
4240 tabular.copyColumn(bv.buffer().params(), column);
4241 cur.idx() = tabular.cellIndex(row, column);
4244 case Tabular::M_TOGGLE_LINE_TOP:
4246 case Tabular::TOGGLE_LINE_TOP: {
4247 bool lineSet = !tabular.topLine(cur.idx(), flag);
4248 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4249 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4251 tabular.cellIndex(i, j),
4256 case Tabular::M_TOGGLE_LINE_BOTTOM:
4258 case Tabular::TOGGLE_LINE_BOTTOM: {
4259 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4260 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4261 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4262 tabular.setBottomLine(
4263 tabular.cellIndex(i, j),
4269 case Tabular::M_TOGGLE_LINE_LEFT:
4271 case Tabular::TOGGLE_LINE_LEFT: {
4272 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4273 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4274 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4275 tabular.setLeftLine(
4276 tabular.cellIndex(i,j),
4282 case Tabular::M_TOGGLE_LINE_RIGHT:
4284 case Tabular::TOGGLE_LINE_RIGHT: {
4285 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4286 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4287 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4288 tabular.setRightLine(
4289 tabular.cellIndex(i,j),
4295 case Tabular::M_ALIGN_LEFT:
4296 case Tabular::M_ALIGN_RIGHT:
4297 case Tabular::M_ALIGN_CENTER:
4299 case Tabular::ALIGN_LEFT:
4300 case Tabular::ALIGN_RIGHT:
4301 case Tabular::ALIGN_CENTER:
4302 case Tabular::ALIGN_BLOCK:
4303 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4304 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4305 tabular.setAlignment(
4306 tabular.cellIndex(i, j),
4311 case Tabular::M_VALIGN_TOP:
4312 case Tabular::M_VALIGN_BOTTOM:
4313 case Tabular::M_VALIGN_MIDDLE:
4315 case Tabular::VALIGN_TOP:
4316 case Tabular::VALIGN_BOTTOM:
4317 case Tabular::VALIGN_MIDDLE:
4318 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4319 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4320 tabular.setVAlignment(
4321 tabular.cellIndex(i, j),
4325 case Tabular::MULTICOLUMN: {
4326 if (sel_row_start != sel_row_end) {
4327 // FIXME: Need I say it ? This is horrible.
4329 Alert::error(_("Error setting multicolumn"),
4330 _("You cannot set multicolumn vertically."));
4333 if (!cur.selection()) {
4334 // just multicol for one single cell
4335 // check whether we are completely in a multicol
4336 if (tabular.isMultiColumn(cur.idx()))
4337 tabular.unsetMultiColumn(cur.idx());
4339 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4342 // we have a selection so this means we just add all this
4343 // cells to form a multicolumn cell
4344 idx_type const s_start = cur.selBegin().idx();
4345 idx_type const s_end = cur.selEnd().idx();
4346 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4347 cur.idx() = s_start;
4350 cur.selection() = false;
4354 case Tabular::SET_ALL_LINES:
4356 case Tabular::UNSET_ALL_LINES:
4357 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4358 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4359 tabular.setAllLines(
4360 tabular.cellIndex(i,j), setLines);
4363 case Tabular::SET_LONGTABULAR:
4364 tabular.setLongTabular(true);
4367 case Tabular::UNSET_LONGTABULAR:
4368 tabular.setLongTabular(false);
4371 case Tabular::SET_ROTATE_TABULAR:
4372 tabular.setRotateTabular(true);
4375 case Tabular::UNSET_ROTATE_TABULAR:
4376 tabular.setRotateTabular(false);
4379 case Tabular::TOGGLE_ROTATE_TABULAR:
4380 tabular.setRotateTabular(!tabular.getRotateTabular());
4383 case Tabular::SET_ROTATE_CELL:
4384 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4385 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4386 tabular.setRotateCell(
4387 tabular.cellIndex(i, j), true);
4390 case Tabular::UNSET_ROTATE_CELL:
4391 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4392 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4393 tabular.setRotateCell(
4394 tabular.cellIndex(i, j), false);
4397 case Tabular::TOGGLE_ROTATE_CELL:
4399 bool oneNotRotated = oneCellHasRotationState(false,
4400 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4402 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4403 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4404 tabular.setRotateCell(tabular.cellIndex(i, j),
4409 case Tabular::SET_USEBOX: {
4410 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4411 if (val == tabular.getUsebox(cur.idx()))
4412 val = Tabular::BOX_NONE;
4413 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4414 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4415 tabular.setUsebox(tabular.cellIndex(i, j), val);
4419 case Tabular::UNSET_LTFIRSTHEAD:
4421 case Tabular::SET_LTFIRSTHEAD:
4422 tabular.getRowOfLTFirstHead(row, ltt);
4423 checkLongtableSpecial(ltt, value, flag);
4424 tabular.setLTHead(row, flag, ltt, true);
4427 case Tabular::UNSET_LTHEAD:
4429 case Tabular::SET_LTHEAD:
4430 tabular.getRowOfLTHead(row, ltt);
4431 checkLongtableSpecial(ltt, value, flag);
4432 tabular.setLTHead(row, flag, ltt, false);
4435 case Tabular::UNSET_LTFOOT:
4437 case Tabular::SET_LTFOOT:
4438 tabular.getRowOfLTFoot(row, ltt);
4439 checkLongtableSpecial(ltt, value, flag);
4440 tabular.setLTFoot(row, flag, ltt, false);
4443 case Tabular::UNSET_LTLASTFOOT:
4445 case Tabular::SET_LTLASTFOOT:
4446 tabular.getRowOfLTLastFoot(row, ltt);
4447 checkLongtableSpecial(ltt, value, flag);
4448 tabular.setLTFoot(row, flag, ltt, true);
4451 case Tabular::SET_LTNEWPAGE:
4452 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4455 case Tabular::SET_BOOKTABS:
4456 tabular.setBookTabs(true);
4459 case Tabular::UNSET_BOOKTABS:
4460 tabular.setBookTabs(false);
4463 case Tabular::SET_TOP_SPACE: {
4465 if (value == "default")
4466 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4467 tabular.row_info[i].top_space_default = true;
4468 else if (isValidLength(value, &len))
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4470 tabular.row_info[i].top_space_default = false;
4471 tabular.row_info[i].top_space = len;
4474 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4475 tabular.row_info[i].top_space_default = false;
4476 tabular.row_info[i].top_space = len;
4481 case Tabular::SET_BOTTOM_SPACE: {
4483 if (value == "default")
4484 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4485 tabular.row_info[i].bottom_space_default = true;
4486 else if (isValidLength(value, &len))
4487 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4488 tabular.row_info[i].bottom_space_default = false;
4489 tabular.row_info[i].bottom_space = len;
4492 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4493 tabular.row_info[i].bottom_space_default = false;
4494 tabular.row_info[i].bottom_space = len;
4499 case Tabular::SET_INTERLINE_SPACE: {
4501 if (value == "default")
4502 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4503 tabular.row_info[i].interline_space_default = true;
4504 else if (isValidLength(value, &len))
4505 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4506 tabular.row_info[i].interline_space_default = false;
4507 tabular.row_info[i].interline_space = len;
4510 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4511 tabular.row_info[i].interline_space_default = false;
4512 tabular.row_info[i].interline_space = len;
4517 // dummy stuff just to avoid warnings
4518 case Tabular::LAST_ACTION:
4524 bool InsetTabular::showInsetDialog(BufferView * bv) const
4526 InsetTabularMailer(*this).showDialog(bv);
4531 void InsetTabular::openLayoutDialog(BufferView * bv) const
4533 InsetTabularMailer(*this).showDialog(bv);
4537 bool InsetTabular::copySelection(Cursor & cur)
4539 if (!cur.selection())
4544 getSelection(cur, rs, re, cs, ce);
4546 paste_tabular.reset(new Tabular(tabular));
4548 for (row_type i = 0; i < rs; ++i)
4549 paste_tabular->deleteRow(0);
4551 row_type const rows = re - rs + 1;
4552 while (paste_tabular->rowCount() > rows)
4553 paste_tabular->deleteRow(rows);
4555 paste_tabular->setTopLine(0, true, true);
4556 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4559 for (col_type i = 0; i < cs; ++i)
4560 paste_tabular->deleteColumn(0);
4562 col_type const columns = ce - cs + 1;
4563 while (paste_tabular->columnCount() > columns)
4564 paste_tabular->deleteColumn(columns);
4566 paste_tabular->setLeftLine(0, true, true);
4567 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4570 odocstringstream os;
4571 OutputParams const runparams(0);
4572 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4573 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4574 cap::copySelection(cur, os.str());
4576 // mark tabular stack dirty
4577 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4578 // when we (hopefully) have a one-for-all paste mechanism.
4579 // This must be called after cap::copySelection.
4580 dirtyTabularStack(true);
4586 bool InsetTabular::pasteClipboard(Cursor & cur)
4590 col_type const actcol = tabular.cellColumn(cur.idx());
4591 row_type const actrow = tabular.cellRow(cur.idx());
4592 for (row_type r1 = 0, r2 = actrow;
4593 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4595 for (col_type c1 = 0, c2 = actcol;
4596 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4598 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4599 tabular.isPartOfMultiColumn(r2, c2))
4601 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4605 if (tabular.isPartOfMultiColumn(r2, c2)) {
4609 shared_ptr<InsetText> inset(
4610 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4611 tabular.setCellInset(r2, c2, inset);
4612 // FIXME: change tracking (MG)
4613 inset->setChange(Change(cur.buffer().params().trackChanges ?
4614 Change::INSERTED : Change::UNCHANGED));
4622 void InsetTabular::cutSelection(Cursor & cur)
4624 if (!cur.selection())
4629 getSelection(cur, rs, re, cs, ce);
4630 for (row_type i = rs; i <= re; ++i) {
4631 for (col_type j = cs; j <= ce; ++j) {
4632 shared_ptr<InsetText> t
4633 = cell(tabular.cellIndex(i, j));
4634 if (cur.buffer().params().trackChanges)
4635 // FIXME: Change tracking (MG)
4636 t->setChange(Change(Change::DELETED));
4642 // cursor position might be invalid now
4643 if (cur.pit() > cur.lastpit())
4644 cur.pit() = cur.lastpit();
4645 if (cur.pos() > cur.lastpos())
4646 cur.pos() = cur.lastpos();
4647 cur.clearSelection();
4651 bool InsetTabular::isRightToLeft(Cursor & cur) const
4653 BOOST_ASSERT(cur.depth() > 1);
4654 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4655 pos_type const parentpos = cur[cur.depth() - 2].pos();
4656 return parentpar.getFontSettings(cur.bv().buffer().params(),
4657 parentpos).language()->rightToLeft();
4661 void InsetTabular::getSelection(Cursor & cur,
4662 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4664 CursorSlice const & beg = cur.selBegin();
4665 CursorSlice const & end = cur.selEnd();
4666 cs = tabular.cellColumn(beg.idx());
4667 ce = tabular.cellColumn(end.idx());
4670 cs = tabular.cellColumn(end.idx());
4672 ce = tabular.cellRightColumn(end.idx());
4675 rs = tabular.cellRow(beg.idx());
4676 re = tabular.cellRow(end.idx());
4682 Text * InsetTabular::getText(int idx) const
4684 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4688 void InsetTabular::setChange(Change const & change)
4690 for (idx_type idx = 0; idx < nargs(); ++idx)
4691 cell(idx)->setChange(change);
4695 void InsetTabular::acceptChanges(BufferParams const & bparams)
4697 for (idx_type idx = 0; idx < nargs(); ++idx)
4698 cell(idx)->acceptChanges(bparams);
4702 void InsetTabular::rejectChanges(BufferParams const & bparams)
4704 for (idx_type idx = 0; idx < nargs(); ++idx)
4705 cell(idx)->rejectChanges(bparams);
4709 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4711 return tabular.getPWidth(cell).zero();
4715 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4718 if (buf.length() <= 0)
4721 Buffer const & buffer = bv.buffer();
4725 col_type maxCols = 1;
4726 docstring::size_type const len = buf.length();
4727 docstring::size_type p = 0;
4730 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4738 maxCols = max(cols, maxCols);
4744 maxCols = max(cols, maxCols);
4750 paste_tabular.reset(
4751 new Tabular(buffer.params(), rows, maxCols));
4752 loctab = paste_tabular.get();
4754 dirtyTabularStack(true);
4757 cell = bv.cursor().idx();
4758 ocol = tabular.cellColumn(cell);
4759 row = tabular.cellRow(cell);
4762 docstring::size_type op = 0;
4763 idx_type const cells = loctab->cellCount();
4766 rows = loctab->rowCount();
4767 col_type const columns = loctab->columnCount();
4769 while (cell < cells && p < len && row < rows &&
4770 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4776 // we can only set this if we are not too far right
4777 if (cols < columns) {
4778 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4779 Font const font = bv.textMetrics(&inset->text_).
4780 getDisplayFont(0, 0);
4781 inset->setText(buf.substr(op, p - op), font,
4782 buffer.params().trackChanges);
4788 // we can only set this if we are not too far right
4789 if (cols < columns) {
4790 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4791 Font const font = bv.textMetrics(&inset->text_).
4792 getDisplayFont(0, 0);
4793 inset->setText(buf.substr(op, p - op), font,
4794 buffer.params().trackChanges);
4799 cell = loctab->cellIndex(row, cols);
4805 // check for the last cell if there is no trailing '\n'
4806 if (cell < cells && op < len) {
4807 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4808 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4809 inset->setText(buf.substr(op, len - op), font,
4810 buffer.params().trackChanges);
4816 void InsetTabular::addPreview(PreviewLoader & loader) const
4818 row_type const rows = tabular.rowCount();
4819 col_type const columns = tabular.columnCount();
4820 for (row_type i = 0; i < rows; ++i) {
4821 for (col_type j = 0; j < columns; ++j)
4822 tabular.getCellInset(i, j)->addPreview(loader);
4827 bool InsetTabular::tablemode(Cursor & cur) const
4829 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4835 string const InsetTabularMailer::name_("tabular");
4837 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4838 : inset_(const_cast<InsetTabular &>(inset))
4842 string const InsetTabularMailer::inset2string(Buffer const &) const
4844 return params2string(inset_);
4848 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4850 istringstream data(in);
4852 lex.setStream(data);
4859 if (!lex || token != name_)
4860 return print_mailer_error("InsetTabularMailer", in, 1,
4863 // This is part of the inset proper that is usually swallowed
4864 // by Buffer::readInset
4866 if (!lex || token != "Tabular")
4867 return print_mailer_error("InsetTabularMailer", in, 2,
4870 Buffer const & buffer = inset.buffer();
4871 inset.read(buffer, lex);
4875 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4878 data << name_ << ' ';
4879 inset.write(inset.buffer(), data);
4880 data << "\\end_inset\n";