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().setLayout(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))), scx_(0)
2874 setBuffer(const_cast<Buffer *>(&buf)); // FIXME: remove later
2878 InsetTabular::InsetTabular(InsetTabular const & tab)
2879 : Inset(tab), tabular(tab.tabular), scx_(0)
2881 setBuffer(const_cast<Buffer *>(tab.buffer())); // FIXME: remove later
2885 InsetTabular::~InsetTabular()
2887 InsetTabularMailer(*this).hideDialog();
2891 Inset * InsetTabular::clone() const
2893 return new InsetTabular(*this);
2897 bool InsetTabular::insetAllowed(InsetCode code) const
2899 if (code == MATHMACRO_CODE)
2906 void InsetTabular::write(Buffer const & buf, ostream & os) const
2908 os << "Tabular" << endl;
2909 tabular.write(buf, os);
2913 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2915 bool const old_format = (lex.getString() == "\\LyXTable");
2917 tabular.read(buf, lex);
2923 string token = lex.getString();
2924 while (lex && token != "\\end_inset") {
2926 token = lex.getString();
2929 lex.printError("Missing \\end_inset at this point. "
2935 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2937 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2938 // mi.base.textwidth << "\n";
2940 lyxerr << "InsetTabular::metrics: need bv" << endl;
2941 BOOST_ASSERT(false);
2945 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2948 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2949 if (tabular.isPartOfMultiColumn(i, j))
2950 // Multicolumn cell, but not first one
2955 if (tabular.cell_info[i][j].multicolumn ==
2956 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2957 p_width = tabular.cellinfo_of_cell(cell).p_width;
2959 p_width = tabular.column_info[j].p_width;
2960 if (!p_width.zero())
2961 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2962 tabular.getCellInset(cell)->metrics(m, dim);
2963 if (!p_width.zero())
2964 dim.wid = m.base.textwidth;
2965 tabular.setCellWidth(cell, dim.wid);
2966 if (p_width.zero()) {
2967 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2968 // FIXME there must be a way to get rid of
2969 // the second metrics call
2970 tabular.getCellInset(cell)->metrics(m, dim);
2972 maxAsc = max(maxAsc, dim.asc);
2973 maxDesc = max(maxDesc, dim.des);
2976 int const top_space = tabular.row_info[i].top_space_default ?
2977 default_line_space :
2978 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2979 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2980 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2981 default_line_space :
2982 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2983 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2986 dim.asc = tabular.rowAscent(0);
2987 dim.des = tabular.height() - dim.asc;
2988 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2992 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2994 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2995 BufferView * bv = pi.base.bv;
2997 // FIXME: As the full backrgound is painted in drawSelection(),
2998 // we have no choice but to do a full repaint for the Text cells.
2999 pi.full_repaint = true;
3001 resetPos(bv->cursor());
3004 x += ADD_TO_TABULAR_WIDTH;
3006 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3009 first_visible_cell = Tabular::npos;
3010 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3012 int const a = tabular.rowAscent(i);
3013 int const d = tabular.rowDescent(i);
3014 idx = tabular.cellIndex(i, 0);
3015 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3016 if (tabular.isPartOfMultiColumn(i, j))
3018 if (first_visible_cell == Tabular::npos)
3019 first_visible_cell = idx;
3021 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3022 // Cache the Inset position.
3023 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3024 if (nx + tabular.columnWidth(idx) < 0
3025 || nx > bv->workWidth()
3027 || y - a > bv->workHeight()) {
3028 pi.pain.setDrawingEnabled(false);
3029 cell(idx)->draw(pi, cx, y);
3030 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3031 pi.pain.setDrawingEnabled(original_drawing_state);
3033 cell(idx)->draw(pi, cx, y);
3034 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3036 nx += tabular.columnWidth(idx);
3040 if (i + 1 < tabular.rowCount())
3041 y += d + tabular.rowAscent(i + 1) +
3042 tabular.getAdditionalHeight(i + 1);
3047 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3049 Cursor & cur = pi.base.bv->cursor();
3051 x += scx_ + ADD_TO_TABULAR_WIDTH;
3053 // FIXME: it is wrong to completely paint the background
3054 // if we want to do single row painting.
3056 // Paint background of current tabular
3057 int const w = tabular.width();
3058 int const h = tabular.height();
3059 int yy = y - tabular.rowAscent(0);
3060 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3062 if (!cur.selection())
3064 if (&cur.inset() != this)
3070 if (tablemode(cur)) {
3073 getSelection(cur, rs, re, cs, ce);
3074 y -= tabular.rowAscent(0);
3075 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3076 int const a = tabular.rowAscent(j);
3077 int const h = a + tabular.rowDescent(j);
3079 y += tabular.getAdditionalHeight(j);
3080 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3081 if (tabular.isPartOfMultiColumn(j, i))
3083 idx_type const cell =
3084 tabular.cellIndex(j, i);
3085 int const w = tabular.columnWidth(cell);
3086 if (i >= cs && i <= ce && j >= rs && j <= re)
3087 pi.pain.fillRectangle(xx, y, w, h,
3095 x += getCellXPos(cur.idx());
3096 x += tabular.getBeginningOfTextInCell(cur.idx());
3097 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3102 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3103 row_type row, idx_type cell, bool erased) const
3105 int x2 = x + tabular.columnWidth(cell);
3106 bool on_off = false;
3107 ColorCode col = Color_tabularline;
3108 ColorCode onoffcol = Color_tabularonoffline;
3111 col = Color_deletedtext;
3112 onoffcol = Color_deletedtext;
3115 if (!tabular.topAlreadyDrawn(cell)) {
3116 on_off = !tabular.topLine(cell);
3117 pain.line(x, y - tabular.rowAscent(row),
3118 x2, y - tabular.rowAscent(row),
3119 on_off ? onoffcol : col,
3120 on_off ? Painter::line_onoffdash : Painter::line_solid);
3122 on_off = !tabular.bottomLine(cell);
3123 pain.line(x, y + tabular.rowDescent(row),
3124 x2, y + tabular.rowDescent(row),
3125 on_off ? onoffcol : col,
3126 on_off ? Painter::line_onoffdash : Painter::line_solid);
3127 if (!tabular.leftAlreadyDrawn(cell)) {
3128 on_off = !tabular.leftLine(cell);
3129 pain.line(x, y - tabular.rowAscent(row),
3130 x, y + tabular.rowDescent(row),
3131 on_off ? onoffcol : col,
3132 on_off ? Painter::line_onoffdash : Painter::line_solid);
3134 on_off = !tabular.rightLine(cell);
3135 pain.line(x2 - tabular.getAdditionalWidth(cell),
3136 y - tabular.rowAscent(row),
3137 x2 - tabular.getAdditionalWidth(cell),
3138 y + tabular.rowDescent(row),
3139 on_off ? onoffcol : col,
3140 on_off ? Painter::line_onoffdash : Painter::line_solid);
3144 docstring const InsetTabular::editMessage() const
3146 return _("Opened table");
3150 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3152 //lyxerr << "InsetTabular::edit: " << this << endl;
3154 cur.selection() = false;
3157 if (isRightToLeft(cur))
3158 cur.idx() = tabular.getLastCellInRow(0);
3164 if (isRightToLeft(cur))
3165 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3167 cur.idx() = tabular.cellCount() - 1;
3169 cur.pos() = cur.lastpos(); // FIXME crude guess
3171 // FIXME: this accesses the position cache before it is initialized
3173 //cur.bv().fitCursor();
3177 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3179 // In a longtable, tell captions what the current float is
3180 Counters & cnts = buf.params().getTextClass().counters();
3181 string const saveflt = cnts.current_float();
3182 if (tabular.isLongTabular())
3183 cnts.current_float("table");
3185 ParIterator it2 = it;
3187 size_t const end = it2.nargs();
3188 for ( ; it2.idx() < end; it2.top().forwardIdx())
3189 lyx::updateLabels(buf, it2);
3192 if (tabular.isLongTabular())
3193 cnts.current_float(saveflt);
3197 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3199 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3200 << "\n cur:" << cur);
3201 CursorSlice sl = cur.top();
3202 Cursor & bvcur = cur.bv().cursor();
3204 switch (cmd.action) {
3206 case LFUN_MOUSE_PRESS:
3207 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3209 // do not reset cursor/selection if we have selected
3210 // some cells (bug 2715).
3211 if (cmd.button() == mouse_button::button3
3212 && &bvcur.selBegin().inset() == this
3213 && tablemode(bvcur))
3216 // Let InsetText do it
3217 cell(cur.idx())->dispatch(cur, cmd);
3219 case LFUN_MOUSE_MOTION:
3220 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3221 if (cmd.button() == mouse_button::button1) {
3222 // only accept motions to places not deeper nested than the real anchor
3223 if (bvcur.anchor_.hasPart(cur)) {
3224 // only update if selection changes
3225 if (bvcur.idx() == cur.idx() &&
3226 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3228 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3229 bvcur.setCursor(cur);
3230 bvcur.selection() = true;
3236 case LFUN_MOUSE_RELEASE:
3237 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3238 if (cmd.button() == mouse_button::button3)
3239 InsetTabularMailer(*this).showDialog(&cur.bv());
3242 case LFUN_CELL_BACKWARD:
3244 cur.selection() = false;
3247 case LFUN_CELL_FORWARD:
3249 cur.selection() = false;
3252 case LFUN_CHAR_FORWARD_SELECT:
3253 case LFUN_CHAR_FORWARD:
3254 cell(cur.idx())->dispatch(cur, cmd);
3255 if (!cur.result().dispatched()) {
3257 if (sl == cur.top())
3258 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3264 case LFUN_CHAR_BACKWARD_SELECT:
3265 case LFUN_CHAR_BACKWARD:
3266 cell(cur.idx())->dispatch(cur, cmd);
3267 if (!cur.result().dispatched()) {
3269 if (sl == cur.top())
3270 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3276 case LFUN_CHAR_RIGHT_SELECT:
3277 case LFUN_CHAR_RIGHT:
3278 //FIXME: for visual cursor, really move right
3279 if (isRightToLeft(cur))
3280 lyx::dispatch(FuncRequest(
3281 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3282 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3284 lyx::dispatch(FuncRequest(
3285 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3286 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3289 case LFUN_CHAR_LEFT_SELECT:
3290 case LFUN_CHAR_LEFT:
3291 //FIXME: for visual cursor, really move left
3292 if (isRightToLeft(cur))
3293 lyx::dispatch(FuncRequest(
3294 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3295 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3297 lyx::dispatch(FuncRequest(
3298 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3299 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3302 case LFUN_DOWN_SELECT:
3304 cell(cur.idx())->dispatch(cur, cmd);
3305 cur.dispatched(); // override the cell's decision
3306 if (sl == cur.top())
3307 // if our Text didn't do anything to the cursor
3308 // then we try to put the cursor into the cell below
3309 // setting also the right targetX.
3310 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3311 cur.idx() = tabular.getCellBelow(cur.idx());
3313 TextMetrics const & tm =
3314 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3315 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3317 if (sl == cur.top()) {
3318 // we trick it to go to forward after leaving the
3320 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3325 case LFUN_UP_SELECT:
3327 cell(cur.idx())->dispatch(cur, cmd);
3328 cur.dispatched(); // override the cell's decision
3329 if (sl == cur.top())
3330 // if our Text didn't do anything to the cursor
3331 // then we try to put the cursor into the cell above
3332 // setting also the right targetX.
3333 if (tabular.cellRow(cur.idx()) != 0) {
3334 cur.idx() = tabular.getCellAbove(cur.idx());
3335 cur.pit() = cur.lastpit();
3336 Text const * text = cell(cur.idx())->getText(0);
3337 TextMetrics const & tm = cur.bv().textMetrics(text);
3338 ParagraphMetrics const & pm =
3339 tm.parMetrics(cur.lastpit());
3340 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3342 if (sl == cur.top()) {
3343 cmd = FuncRequest(LFUN_UP);
3348 // case LFUN_SCREEN_DOWN: {
3349 // //if (hasSelection())
3350 // // cur.selection() = false;
3351 // col_type const col = tabular.cellColumn(cur.idx());
3352 // int const t = cur.bv().top_y() + cur.bv().height();
3353 // if (t < yo() + tabular.getHeightOfTabular()) {
3354 // cur.bv().scrollDocView(t);
3355 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3357 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3364 // case LFUN_SCREEN_UP: {
3365 // //if (hasSelection())
3366 // // cur.selection() = false;
3367 // col_type const col = tabular.cellColumn(cur.idx());
3368 // int const t = cur.bv().top_y() + cur.bv().height();
3370 // cur.bv().scrollDocView(t);
3374 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3378 // cur.par() = cur.lastpar();
3379 // cur.pos() = cur.lastpos();
3383 case LFUN_LAYOUT_TABULAR:
3384 InsetTabularMailer(*this).showDialog(&cur.bv());
3387 case LFUN_INSET_DIALOG_UPDATE:
3388 InsetTabularMailer(*this).updateDialog(&cur.bv());
3391 case LFUN_TABULAR_FEATURE:
3392 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3396 // insert file functions
3397 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3398 case LFUN_FILE_INSERT_PLAINTEXT: {
3400 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3401 FileName(to_utf8(cmd.argument())));
3404 cur.recordUndoInset(INSERT_UNDO);
3405 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3406 // content has been replaced,
3407 // so cursor might be invalid
3408 cur.pos() = cur.lastpos();
3409 cur.pit() = cur.lastpit();
3410 bvcur.setCursor(cur);
3417 if (tablemode(cur)) {
3418 if (copySelection(cur)) {
3419 cur.recordUndoInset(DELETE_UNDO);
3424 cell(cur.idx())->dispatch(cur, cmd);
3427 case LFUN_CHAR_DELETE_BACKWARD:
3428 case LFUN_CHAR_DELETE_FORWARD:
3429 if (tablemode(cur)) {
3430 cur.recordUndoInset(DELETE_UNDO);
3434 cell(cur.idx())->dispatch(cur, cmd);
3438 if (!cur.selection())
3440 if (tablemode(cur)) {
3444 cell(cur.idx())->dispatch(cur, cmd);
3447 case LFUN_CLIPBOARD_PASTE:
3448 case LFUN_PRIMARY_SELECTION_PASTE: {
3449 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3450 theClipboard().getAsText() :
3451 theSelection().get();
3454 // pass to InsertPlaintextString, but
3455 // only if we have multi-cell content
3456 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3457 cur.recordUndoInset(INSERT_UNDO);
3458 if (insertPlaintextString(cur.bv(), clip, false)) {
3459 // content has been replaced,
3460 // so cursor might be invalid
3461 cur.pos() = cur.lastpos();
3462 cur.pit() = cur.lastpit();
3463 bvcur.setCursor(cur);
3467 // Let the cell handle normal text
3468 cell(cur.idx())->dispatch(cur, cmd);
3473 if (tabularStackDirty() && theClipboard().isInternal() ||
3474 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3475 cur.recordUndoInset(INSERT_UNDO);
3476 pasteClipboard(cur);
3479 cell(cur.idx())->dispatch(cur, cmd);
3482 case LFUN_FONT_EMPH:
3483 case LFUN_FONT_BOLD:
3484 case LFUN_FONT_ROMAN:
3485 case LFUN_FONT_NOUN:
3486 case LFUN_FONT_ITAL:
3487 case LFUN_FONT_FRAK:
3488 case LFUN_FONT_TYPEWRITER:
3489 case LFUN_FONT_SANS:
3490 case LFUN_FONT_FREE_APPLY:
3491 case LFUN_FONT_FREE_UPDATE:
3492 case LFUN_FONT_SIZE:
3493 case LFUN_FONT_UNDERLINE:
3495 case LFUN_WORD_CAPITALIZE:
3496 case LFUN_WORD_UPCASE:
3497 case LFUN_WORD_LOWCASE:
3498 case LFUN_CHARS_TRANSPOSE:
3499 if (tablemode(cur)) {
3502 getSelection(cur, rs, re, cs, ce);
3503 Cursor tmpcur = cur;
3504 for (row_type i = rs; i <= re; ++i) {
3505 for (col_type j = cs; j <= ce; ++j) {
3506 // cursor follows cell:
3507 tmpcur.idx() = tabular.cellIndex(i, j);
3508 // select this cell only:
3511 tmpcur.resetAnchor();
3512 tmpcur.pit() = tmpcur.lastpit();
3513 tmpcur.pos() = tmpcur.top().lastpos();
3514 tmpcur.setCursor(tmpcur);
3515 tmpcur.setSelection();
3516 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3521 cell(cur.idx())->dispatch(cur, cmd);
3525 // we try to handle this event in the insets dispatch function.
3526 cell(cur.idx())->dispatch(cur, cmd);
3532 // function sets an object as defined in func_status.h:
3533 // states OK, Unknown, Disabled, On, Off.
3534 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3535 FuncStatus & status) const
3537 switch (cmd.action) {
3538 case LFUN_TABULAR_FEATURE: {
3539 int action = Tabular::LAST_ACTION;
3541 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3542 string const tmp = tabularFeature[i].feature;
3543 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3544 action = tabularFeature[i].action;
3548 if (action == Tabular::LAST_ACTION) {
3550 status.unknown(true);
3554 string const argument
3555 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3557 row_type sel_row_start = 0;
3558 row_type sel_row_end = 0;
3559 col_type sel_col_start = 0;
3560 col_type sel_col_end = 0;
3561 Tabular::ltType dummyltt;
3564 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3567 case Tabular::SET_PWIDTH:
3568 case Tabular::SET_MPWIDTH:
3569 case Tabular::SET_SPECIAL_COLUMN:
3570 case Tabular::SET_SPECIAL_MULTI:
3571 case Tabular::APPEND_ROW:
3572 case Tabular::APPEND_COLUMN:
3573 case Tabular::DELETE_ROW:
3574 case Tabular::DELETE_COLUMN:
3575 case Tabular::COPY_ROW:
3576 case Tabular::COPY_COLUMN:
3577 case Tabular::SET_ALL_LINES:
3578 case Tabular::UNSET_ALL_LINES:
3579 case Tabular::SET_TOP_SPACE:
3580 case Tabular::SET_BOTTOM_SPACE:
3581 case Tabular::SET_INTERLINE_SPACE:
3585 case Tabular::MULTICOLUMN:
3586 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3589 case Tabular::M_TOGGLE_LINE_TOP:
3591 case Tabular::TOGGLE_LINE_TOP:
3592 status.setOnOff(tabular.topLine(cur.idx(), flag));
3595 case Tabular::M_TOGGLE_LINE_BOTTOM:
3597 case Tabular::TOGGLE_LINE_BOTTOM:
3598 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3601 case Tabular::M_TOGGLE_LINE_LEFT:
3603 case Tabular::TOGGLE_LINE_LEFT:
3604 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3607 case Tabular::M_TOGGLE_LINE_RIGHT:
3609 case Tabular::TOGGLE_LINE_RIGHT:
3610 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3613 case Tabular::M_ALIGN_LEFT:
3615 case Tabular::ALIGN_LEFT:
3616 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3619 case Tabular::M_ALIGN_RIGHT:
3621 case Tabular::ALIGN_RIGHT:
3622 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3625 case Tabular::M_ALIGN_CENTER:
3627 case Tabular::ALIGN_CENTER:
3628 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3631 case Tabular::ALIGN_BLOCK:
3632 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3633 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3636 case Tabular::M_VALIGN_TOP:
3638 case Tabular::VALIGN_TOP:
3640 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3643 case Tabular::M_VALIGN_BOTTOM:
3645 case Tabular::VALIGN_BOTTOM:
3647 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3650 case Tabular::M_VALIGN_MIDDLE:
3652 case Tabular::VALIGN_MIDDLE:
3654 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3657 case Tabular::SET_LONGTABULAR:
3658 status.setOnOff(tabular.isLongTabular());
3661 case Tabular::UNSET_LONGTABULAR:
3662 status.setOnOff(!tabular.isLongTabular());
3665 case Tabular::TOGGLE_ROTATE_TABULAR:
3666 case Tabular::SET_ROTATE_TABULAR:
3667 status.setOnOff(tabular.getRotateTabular());
3670 case Tabular::UNSET_ROTATE_TABULAR:
3671 status.setOnOff(!tabular.getRotateTabular());
3674 case Tabular::TOGGLE_ROTATE_CELL:
3675 case Tabular::SET_ROTATE_CELL:
3676 status.setOnOff(!oneCellHasRotationState(false,
3677 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3680 case Tabular::UNSET_ROTATE_CELL:
3681 status.setOnOff(!oneCellHasRotationState(true,
3682 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3685 case Tabular::SET_USEBOX:
3686 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3689 case Tabular::SET_LTFIRSTHEAD:
3690 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3693 case Tabular::UNSET_LTFIRSTHEAD:
3694 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3697 case Tabular::SET_LTHEAD:
3698 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3701 case Tabular::UNSET_LTHEAD:
3702 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3705 case Tabular::SET_LTFOOT:
3706 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3709 case Tabular::UNSET_LTFOOT:
3710 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3713 case Tabular::SET_LTLASTFOOT:
3714 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3717 case Tabular::UNSET_LTLASTFOOT:
3718 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3721 case Tabular::SET_LTNEWPAGE:
3722 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3725 case Tabular::SET_BOOKTABS:
3726 status.setOnOff(tabular.useBookTabs());
3729 case Tabular::UNSET_BOOKTABS:
3730 status.setOnOff(!tabular.useBookTabs());
3735 status.enabled(false);
3741 // These are only enabled inside tabular
3742 case LFUN_CELL_BACKWARD:
3743 case LFUN_CELL_FORWARD:
3744 status.enabled(true);
3747 // disable these with multiple cells selected
3748 case LFUN_INSET_INSERT:
3749 case LFUN_TABULAR_INSERT:
3750 case LFUN_FLEX_INSERT:
3751 case LFUN_FLOAT_INSERT:
3752 case LFUN_FLOAT_WIDE_INSERT:
3753 case LFUN_FOOTNOTE_INSERT:
3754 case LFUN_MARGINALNOTE_INSERT:
3755 case LFUN_MATH_INSERT:
3756 case LFUN_MATH_MODE:
3757 case LFUN_MATH_MUTATE:
3758 case LFUN_MATH_DISPLAY:
3759 case LFUN_NOTE_INSERT:
3760 case LFUN_OPTIONAL_INSERT:
3761 case LFUN_BOX_INSERT:
3762 case LFUN_BRANCH_INSERT:
3763 case LFUN_WRAP_INSERT:
3764 case LFUN_ERT_INSERT: {
3765 if (tablemode(cur)) {
3766 status.enabled(false);
3769 return cell(cur.idx())->getStatus(cur, cmd, status);
3772 // disable in non-fixed-width cells
3774 case LFUN_BREAK_PARAGRAPH:
3775 case LFUN_BREAK_PARAGRAPH_SKIP: {
3776 if (tabular.getPWidth(cur.idx()).zero()) {
3777 status.enabled(false);
3780 return cell(cur.idx())->getStatus(cur, cmd, status);
3784 if (tabularStackDirty() && theClipboard().isInternal()) {
3785 status.enabled(true);
3788 return cell(cur.idx())->getStatus(cur, cmd, status);
3790 case LFUN_INSET_MODIFY:
3791 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3792 status.enabled(true);
3798 // we try to handle this event in the insets dispatch function.
3799 return cell(cur.idx())->getStatus(cur, cmd, status);
3804 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3805 OutputParams const & runparams) const
3807 return tabular.latex(buf, os, runparams);
3811 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3812 OutputParams const & runparams) const
3814 os << '\n'; // output table on a new line
3815 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3816 tabular.plaintext(buf, os, runparams, dp, false, 0);
3817 return PLAINTEXT_NEWLINE;
3821 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3822 OutputParams const & runparams) const
3827 // FIXME: Why not pass a proper DocIterator here?
3829 // if the table is inside a float it doesn't need the informaltable
3830 // wrapper. Search for it.
3831 for (master = owner(); master; master = master->owner())
3832 if (master->lyxCode() == FLOAT_CODE)
3837 os << "<informaltable>";
3840 ret += tabular.docbook(buf, os, runparams);
3842 os << "</informaltable>";
3849 void InsetTabular::validate(LaTeXFeatures & features) const
3851 tabular.validate(features);
3855 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3857 return tabular.getCellInset(idx);
3861 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3863 return tabular.getCellInset(idx);
3867 void InsetTabular::cursorPos(BufferView const & bv,
3868 CursorSlice const & sl, bool boundary, int & x, int & y) const
3870 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3872 // y offset correction
3873 int const row = tabular.cellRow(sl.idx());
3874 for (int i = 0; i <= row; ++i) {
3876 y += tabular.rowAscent(i);
3877 y += tabular.getAdditionalHeight(i);
3880 y += tabular.rowDescent(i);
3883 // x offset correction
3884 int const col = tabular.cellColumn(sl.idx());
3885 int idx = tabular.cellIndex(row, 0);
3886 for (int j = 0; j < col; ++j) {
3887 if (tabular.isPartOfMultiColumn(row, j))
3889 x += tabular.columnWidth(idx);
3892 x += tabular.getBeginningOfTextInCell(idx);
3893 x += ADD_TO_TABULAR_WIDTH;
3898 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3902 Inset const & inset = *tabular.getCellInset(cell);
3903 Point o = bv.coordCache().getInsets().xy(&inset);
3904 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3905 int const xend = xbeg + tabular.columnWidth(cell);
3906 row_type const row = tabular.cellRow(cell);
3907 int const ybeg = o.y_ - tabular.rowAscent(row) -
3908 tabular.getAdditionalHeight(row);
3909 int const yend = o.y_ + tabular.rowDescent(row);
3921 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3922 // << " ybeg=" << ybeg << " yend=" << yend
3923 // << " xx=" << xx << " yy=" << yy
3924 // << " dist=" << xx + yy << endl;
3929 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3931 //lyxerr << "InsetTabular::editXY: " << this << endl;
3932 cur.selection() = false;
3934 cur.idx() = getNearestCell(cur.bv(), x, y);
3936 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3940 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3942 cur.idx() = getNearestCell(cur.bv(), x, y);
3943 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3947 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3949 idx_type idx_min = 0;
3950 int dist_min = numeric_limits<int>::max();
3951 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3952 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3953 int const d = dist(bv, i, x, y);
3964 int InsetTabular::getCellXPos(idx_type const cell) const
3968 for (; !tabular.isFirstCellInRow(c); --c)
3971 for (; c < cell; ++c)
3972 lx += tabular.columnWidth(c);
3978 void InsetTabular::resetPos(Cursor & cur) const
3980 BufferView & bv = cur.bv();
3981 int const maxwidth = bv.workWidth();
3983 if (&cur.inset() != this) {
3987 int const X2 = maxwidth;
3988 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3989 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3990 int const x2 = x1 + tabular.columnWidth(cur.idx());
3993 scx_ = X1 + 20 - x1;
3995 scx_ = X2 - 20 - x2;
4000 cur.updateFlags(Update::Force | Update::FitCursor);
4004 void InsetTabular::moveNextCell(Cursor & cur)
4006 if (isRightToLeft(cur)) {
4007 if (tabular.isFirstCellInRow(cur.idx())) {
4008 row_type const row = tabular.cellRow(cur.idx());
4009 if (row == tabular.rowCount() - 1)
4011 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4018 if (tabular.isLastCell(cur.idx()))
4028 void InsetTabular::movePrevCell(Cursor & cur)
4030 if (isRightToLeft(cur)) {
4031 if (tabular.isLastCellInRow(cur.idx())) {
4032 row_type const row = tabular.cellRow(cur.idx());
4035 cur.idx() = tabular.getFirstCellInRow(row);
4036 cur.idx() = tabular.getCellAbove(cur.idx());
4038 if (tabular.isLastCell(cur.idx()))
4043 if (cur.idx() == 0) // first cell
4047 cur.pit() = cur.lastpit();
4048 cur.pos() = cur.lastpos();
4050 // FIXME: this accesses the position cache before it is initialized
4055 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4057 Tabular::Feature action = Tabular::LAST_ACTION;
4060 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4061 string const tmp = tabularFeature[i].feature;
4063 if (tmp == what.substr(0, tmp.length())) {
4064 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4065 //tabularFeatures[i].feature.length()))
4066 action = tabularFeature[i].action;
4070 if (action == Tabular::LAST_ACTION)
4074 ltrim(what.substr(tabularFeature[i].feature.length()));
4075 tabularFeatures(cur, action, val);
4080 static void checkLongtableSpecial(Tabular::ltType & ltt,
4081 string const & special, bool & flag)
4083 if (special == "dl_above") {
4086 } else if (special == "dl_below") {
4087 ltt.bottomDL = flag;
4089 } else if (special == "empty") {
4098 bool InsetTabular::oneCellHasRotationState(bool rotated,
4099 row_type row_start, row_type row_end,
4100 col_type col_start, col_type col_end) const {
4102 for (row_type i = row_start; i <= row_end; ++i) {
4103 for (col_type j = col_start; j <= col_end; ++j) {
4104 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4113 void InsetTabular::tabularFeatures(Cursor & cur,
4114 Tabular::Feature feature, string const & value)
4116 BufferView & bv = cur.bv();
4117 col_type sel_col_start;
4118 col_type sel_col_end;
4119 row_type sel_row_start;
4120 row_type sel_row_end;
4121 bool setLines = false;
4122 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4123 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4127 case Tabular::M_ALIGN_LEFT:
4128 case Tabular::ALIGN_LEFT:
4129 setAlign = LYX_ALIGN_LEFT;
4132 case Tabular::M_ALIGN_RIGHT:
4133 case Tabular::ALIGN_RIGHT:
4134 setAlign = LYX_ALIGN_RIGHT;
4137 case Tabular::M_ALIGN_CENTER:
4138 case Tabular::ALIGN_CENTER:
4139 setAlign = LYX_ALIGN_CENTER;
4142 case Tabular::ALIGN_BLOCK:
4143 setAlign = LYX_ALIGN_BLOCK;
4146 case Tabular::M_VALIGN_TOP:
4147 case Tabular::VALIGN_TOP:
4148 setVAlign = Tabular::LYX_VALIGN_TOP;
4151 case Tabular::M_VALIGN_BOTTOM:
4152 case Tabular::VALIGN_BOTTOM:
4153 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4156 case Tabular::M_VALIGN_MIDDLE:
4157 case Tabular::VALIGN_MIDDLE:
4158 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4165 cur.recordUndoInset(ATOMIC_UNDO);
4167 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4168 row_type const row = tabular.cellRow(cur.idx());
4169 col_type const column = tabular.cellColumn(cur.idx());
4171 Tabular::ltType ltt;
4175 case Tabular::SET_PWIDTH: {
4176 Length const len(value);
4177 tabular.setColumnPWidth(cur, cur.idx(), len);
4179 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4180 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4184 case Tabular::SET_MPWIDTH:
4185 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4188 case Tabular::SET_SPECIAL_COLUMN:
4189 case Tabular::SET_SPECIAL_MULTI:
4190 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4193 case Tabular::APPEND_ROW:
4194 // append the row into the tabular
4195 tabular.appendRow(bv.buffer().params(), cur.idx());
4198 case Tabular::APPEND_COLUMN:
4199 // append the column into the tabular
4200 tabular.appendColumn(bv.buffer().params(), cur.idx());
4201 cur.idx() = tabular.cellIndex(row, column);
4204 case Tabular::DELETE_ROW:
4205 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4206 tabular.deleteRow(sel_row_start);
4207 if (sel_row_start >= tabular.rowCount())
4209 cur.idx() = tabular.cellIndex(sel_row_start, column);
4212 cur.selection() = false;
4215 case Tabular::DELETE_COLUMN:
4216 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4217 tabular.deleteColumn(sel_col_start);
4218 if (sel_col_start >= tabular.columnCount())
4220 cur.idx() = tabular.cellIndex(row, sel_col_start);
4223 cur.selection() = false;
4226 case Tabular::COPY_ROW:
4227 tabular.copyRow(bv.buffer().params(), row);
4230 case Tabular::COPY_COLUMN:
4231 tabular.copyColumn(bv.buffer().params(), column);
4232 cur.idx() = tabular.cellIndex(row, column);
4235 case Tabular::M_TOGGLE_LINE_TOP:
4237 case Tabular::TOGGLE_LINE_TOP: {
4238 bool lineSet = !tabular.topLine(cur.idx(), flag);
4239 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4240 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4242 tabular.cellIndex(i, j),
4247 case Tabular::M_TOGGLE_LINE_BOTTOM:
4249 case Tabular::TOGGLE_LINE_BOTTOM: {
4250 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4251 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4252 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4253 tabular.setBottomLine(
4254 tabular.cellIndex(i, j),
4260 case Tabular::M_TOGGLE_LINE_LEFT:
4262 case Tabular::TOGGLE_LINE_LEFT: {
4263 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4264 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4265 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4266 tabular.setLeftLine(
4267 tabular.cellIndex(i,j),
4273 case Tabular::M_TOGGLE_LINE_RIGHT:
4275 case Tabular::TOGGLE_LINE_RIGHT: {
4276 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4277 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4278 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4279 tabular.setRightLine(
4280 tabular.cellIndex(i,j),
4286 case Tabular::M_ALIGN_LEFT:
4287 case Tabular::M_ALIGN_RIGHT:
4288 case Tabular::M_ALIGN_CENTER:
4290 case Tabular::ALIGN_LEFT:
4291 case Tabular::ALIGN_RIGHT:
4292 case Tabular::ALIGN_CENTER:
4293 case Tabular::ALIGN_BLOCK:
4294 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4295 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4296 tabular.setAlignment(
4297 tabular.cellIndex(i, j),
4302 case Tabular::M_VALIGN_TOP:
4303 case Tabular::M_VALIGN_BOTTOM:
4304 case Tabular::M_VALIGN_MIDDLE:
4306 case Tabular::VALIGN_TOP:
4307 case Tabular::VALIGN_BOTTOM:
4308 case Tabular::VALIGN_MIDDLE:
4309 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4310 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4311 tabular.setVAlignment(
4312 tabular.cellIndex(i, j),
4316 case Tabular::MULTICOLUMN: {
4317 if (sel_row_start != sel_row_end) {
4318 // FIXME: Need I say it ? This is horrible.
4320 Alert::error(_("Error setting multicolumn"),
4321 _("You cannot set multicolumn vertically."));
4324 if (!cur.selection()) {
4325 // just multicol for one single cell
4326 // check whether we are completely in a multicol
4327 if (tabular.isMultiColumn(cur.idx()))
4328 tabular.unsetMultiColumn(cur.idx());
4330 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4333 // we have a selection so this means we just add all this
4334 // cells to form a multicolumn cell
4335 idx_type const s_start = cur.selBegin().idx();
4336 idx_type const s_end = cur.selEnd().idx();
4337 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4338 cur.idx() = s_start;
4341 cur.selection() = false;
4345 case Tabular::SET_ALL_LINES:
4347 case Tabular::UNSET_ALL_LINES:
4348 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4349 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4350 tabular.setAllLines(
4351 tabular.cellIndex(i,j), setLines);
4354 case Tabular::SET_LONGTABULAR:
4355 tabular.setLongTabular(true);
4358 case Tabular::UNSET_LONGTABULAR:
4359 tabular.setLongTabular(false);
4362 case Tabular::SET_ROTATE_TABULAR:
4363 tabular.setRotateTabular(true);
4366 case Tabular::UNSET_ROTATE_TABULAR:
4367 tabular.setRotateTabular(false);
4370 case Tabular::TOGGLE_ROTATE_TABULAR:
4371 tabular.setRotateTabular(!tabular.getRotateTabular());
4374 case Tabular::SET_ROTATE_CELL:
4375 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4376 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4377 tabular.setRotateCell(
4378 tabular.cellIndex(i, j), true);
4381 case Tabular::UNSET_ROTATE_CELL:
4382 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4383 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4384 tabular.setRotateCell(
4385 tabular.cellIndex(i, j), false);
4388 case Tabular::TOGGLE_ROTATE_CELL:
4390 bool oneNotRotated = oneCellHasRotationState(false,
4391 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4393 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4394 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4395 tabular.setRotateCell(tabular.cellIndex(i, j),
4400 case Tabular::SET_USEBOX: {
4401 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4402 if (val == tabular.getUsebox(cur.idx()))
4403 val = Tabular::BOX_NONE;
4404 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4405 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4406 tabular.setUsebox(tabular.cellIndex(i, j), val);
4410 case Tabular::UNSET_LTFIRSTHEAD:
4412 case Tabular::SET_LTFIRSTHEAD:
4413 tabular.getRowOfLTFirstHead(row, ltt);
4414 checkLongtableSpecial(ltt, value, flag);
4415 tabular.setLTHead(row, flag, ltt, true);
4418 case Tabular::UNSET_LTHEAD:
4420 case Tabular::SET_LTHEAD:
4421 tabular.getRowOfLTHead(row, ltt);
4422 checkLongtableSpecial(ltt, value, flag);
4423 tabular.setLTHead(row, flag, ltt, false);
4426 case Tabular::UNSET_LTFOOT:
4428 case Tabular::SET_LTFOOT:
4429 tabular.getRowOfLTFoot(row, ltt);
4430 checkLongtableSpecial(ltt, value, flag);
4431 tabular.setLTFoot(row, flag, ltt, false);
4434 case Tabular::UNSET_LTLASTFOOT:
4436 case Tabular::SET_LTLASTFOOT:
4437 tabular.getRowOfLTLastFoot(row, ltt);
4438 checkLongtableSpecial(ltt, value, flag);
4439 tabular.setLTFoot(row, flag, ltt, true);
4442 case Tabular::SET_LTNEWPAGE:
4443 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4446 case Tabular::SET_BOOKTABS:
4447 tabular.setBookTabs(true);
4450 case Tabular::UNSET_BOOKTABS:
4451 tabular.setBookTabs(false);
4454 case Tabular::SET_TOP_SPACE: {
4456 if (value == "default")
4457 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4458 tabular.row_info[i].top_space_default = true;
4459 else if (isValidLength(value, &len))
4460 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4461 tabular.row_info[i].top_space_default = false;
4462 tabular.row_info[i].top_space = len;
4465 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4466 tabular.row_info[i].top_space_default = false;
4467 tabular.row_info[i].top_space = len;
4472 case Tabular::SET_BOTTOM_SPACE: {
4474 if (value == "default")
4475 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4476 tabular.row_info[i].bottom_space_default = true;
4477 else if (isValidLength(value, &len))
4478 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4479 tabular.row_info[i].bottom_space_default = false;
4480 tabular.row_info[i].bottom_space = len;
4483 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4484 tabular.row_info[i].bottom_space_default = false;
4485 tabular.row_info[i].bottom_space = len;
4490 case Tabular::SET_INTERLINE_SPACE: {
4492 if (value == "default")
4493 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4494 tabular.row_info[i].interline_space_default = true;
4495 else if (isValidLength(value, &len))
4496 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4497 tabular.row_info[i].interline_space_default = false;
4498 tabular.row_info[i].interline_space = len;
4501 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4502 tabular.row_info[i].interline_space_default = false;
4503 tabular.row_info[i].interline_space = len;
4508 // dummy stuff just to avoid warnings
4509 case Tabular::LAST_ACTION:
4515 bool InsetTabular::showInsetDialog(BufferView * bv) const
4517 InsetTabularMailer(*this).showDialog(bv);
4522 void InsetTabular::openLayoutDialog(BufferView * bv) const
4524 InsetTabularMailer(*this).showDialog(bv);
4528 bool InsetTabular::copySelection(Cursor & cur)
4530 if (!cur.selection())
4535 getSelection(cur, rs, re, cs, ce);
4537 paste_tabular.reset(new Tabular(tabular));
4539 for (row_type i = 0; i < rs; ++i)
4540 paste_tabular->deleteRow(0);
4542 row_type const rows = re - rs + 1;
4543 while (paste_tabular->rowCount() > rows)
4544 paste_tabular->deleteRow(rows);
4546 paste_tabular->setTopLine(0, true, true);
4547 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4550 for (col_type i = 0; i < cs; ++i)
4551 paste_tabular->deleteColumn(0);
4553 col_type const columns = ce - cs + 1;
4554 while (paste_tabular->columnCount() > columns)
4555 paste_tabular->deleteColumn(columns);
4557 paste_tabular->setLeftLine(0, true, true);
4558 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4561 odocstringstream os;
4562 OutputParams const runparams(0);
4563 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4564 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4565 cap::copySelection(cur, os.str());
4567 // mark tabular stack dirty
4568 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4569 // when we (hopefully) have a one-for-all paste mechanism.
4570 // This must be called after cap::copySelection.
4571 dirtyTabularStack(true);
4577 bool InsetTabular::pasteClipboard(Cursor & cur)
4581 col_type const actcol = tabular.cellColumn(cur.idx());
4582 row_type const actrow = tabular.cellRow(cur.idx());
4583 for (row_type r1 = 0, r2 = actrow;
4584 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4586 for (col_type c1 = 0, c2 = actcol;
4587 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4589 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4590 tabular.isPartOfMultiColumn(r2, c2))
4592 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4596 if (tabular.isPartOfMultiColumn(r2, c2)) {
4600 shared_ptr<InsetText> inset(
4601 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4602 tabular.setCellInset(r2, c2, inset);
4603 // FIXME: change tracking (MG)
4604 inset->setChange(Change(cur.buffer().params().trackChanges ?
4605 Change::INSERTED : Change::UNCHANGED));
4613 void InsetTabular::cutSelection(Cursor & cur)
4615 if (!cur.selection())
4620 getSelection(cur, rs, re, cs, ce);
4621 for (row_type i = rs; i <= re; ++i) {
4622 for (col_type j = cs; j <= ce; ++j) {
4623 shared_ptr<InsetText> t
4624 = cell(tabular.cellIndex(i, j));
4625 if (cur.buffer().params().trackChanges)
4626 // FIXME: Change tracking (MG)
4627 t->setChange(Change(Change::DELETED));
4633 // cursor position might be invalid now
4634 if (cur.pit() > cur.lastpit())
4635 cur.pit() = cur.lastpit();
4636 if (cur.pos() > cur.lastpos())
4637 cur.pos() = cur.lastpos();
4638 cur.clearSelection();
4642 bool InsetTabular::isRightToLeft(Cursor & cur) const
4644 BOOST_ASSERT(cur.depth() > 1);
4645 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4646 pos_type const parentpos = cur[cur.depth() - 2].pos();
4647 return parentpar.getFontSettings(cur.bv().buffer().params(),
4648 parentpos).language()->rightToLeft();
4652 void InsetTabular::getSelection(Cursor & cur,
4653 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4655 CursorSlice const & beg = cur.selBegin();
4656 CursorSlice const & end = cur.selEnd();
4657 cs = tabular.cellColumn(beg.idx());
4658 ce = tabular.cellColumn(end.idx());
4661 cs = tabular.cellColumn(end.idx());
4663 ce = tabular.cellRightColumn(end.idx());
4666 rs = tabular.cellRow(beg.idx());
4667 re = tabular.cellRow(end.idx());
4673 Text * InsetTabular::getText(int idx) const
4675 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4679 void InsetTabular::setChange(Change const & change)
4681 for (idx_type idx = 0; idx < nargs(); ++idx)
4682 cell(idx)->setChange(change);
4686 void InsetTabular::acceptChanges(BufferParams const & bparams)
4688 for (idx_type idx = 0; idx < nargs(); ++idx)
4689 cell(idx)->acceptChanges(bparams);
4693 void InsetTabular::rejectChanges(BufferParams const & bparams)
4695 for (idx_type idx = 0; idx < nargs(); ++idx)
4696 cell(idx)->rejectChanges(bparams);
4700 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4702 return tabular.getPWidth(cell).zero();
4706 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4709 if (buf.length() <= 0)
4712 Buffer const & buffer = bv.buffer();
4716 col_type maxCols = 1;
4717 docstring::size_type const len = buf.length();
4718 docstring::size_type p = 0;
4721 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4729 maxCols = max(cols, maxCols);
4735 maxCols = max(cols, maxCols);
4741 paste_tabular.reset(
4742 new Tabular(buffer.params(), rows, maxCols));
4743 loctab = paste_tabular.get();
4745 dirtyTabularStack(true);
4748 cell = bv.cursor().idx();
4749 ocol = tabular.cellColumn(cell);
4750 row = tabular.cellRow(cell);
4753 docstring::size_type op = 0;
4754 idx_type const cells = loctab->cellCount();
4757 rows = loctab->rowCount();
4758 col_type const columns = loctab->columnCount();
4760 while (cell < cells && p < len && row < rows &&
4761 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4767 // we can only set this if we are not too far right
4768 if (cols < columns) {
4769 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4770 Font const font = bv.textMetrics(&inset->text_).
4771 getDisplayFont(0, 0);
4772 inset->setText(buf.substr(op, p - op), font,
4773 buffer.params().trackChanges);
4779 // we can only set this if we are not too far right
4780 if (cols < columns) {
4781 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4782 Font const font = bv.textMetrics(&inset->text_).
4783 getDisplayFont(0, 0);
4784 inset->setText(buf.substr(op, p - op), font,
4785 buffer.params().trackChanges);
4790 cell = loctab->cellIndex(row, cols);
4796 // check for the last cell if there is no trailing '\n'
4797 if (cell < cells && op < len) {
4798 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4799 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4800 inset->setText(buf.substr(op, len - op), font,
4801 buffer.params().trackChanges);
4807 void InsetTabular::addPreview(PreviewLoader & loader) const
4809 row_type const rows = tabular.rowCount();
4810 col_type const columns = tabular.columnCount();
4811 for (row_type i = 0; i < rows; ++i) {
4812 for (col_type j = 0; j < columns; ++j)
4813 tabular.getCellInset(i, j)->addPreview(loader);
4818 bool InsetTabular::tablemode(Cursor & cur) const
4820 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4826 string const InsetTabularMailer::name_("tabular");
4828 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4829 : inset_(const_cast<InsetTabular &>(inset))
4833 string const InsetTabularMailer::inset2string(Buffer const &) const
4835 return params2string(inset_);
4839 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4841 istringstream data(in);
4843 lex.setStream(data);
4850 if (!lex || token != name_)
4851 return print_mailer_error("InsetTabularMailer", in, 1,
4854 // This is part of the inset proper that is usually swallowed
4855 // by Buffer::readInset
4857 if (!lex || token != "Tabular")
4858 return print_mailer_error("InsetTabularMailer", in, 2,
4861 Buffer const & buffer = *inset.buffer();
4862 inset.read(buffer, lex);
4866 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4869 data << name_ << ' ';
4870 inset.write(*inset.buffer(), data);
4871 data << "\\end_inset\n";