2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #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/Application.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #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/lassert.h"
59 #include "support/lstrings.h"
61 #include <boost/scoped_ptr.hpp>
69 using namespace lyx::support;
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
106 TabularFeature tabularFeature[] =
108 { Tabular::APPEND_ROW, "append-row" },
109 { Tabular::APPEND_COLUMN, "append-column" },
110 { Tabular::DELETE_ROW, "delete-row" },
111 { Tabular::DELETE_COLUMN, "delete-column" },
112 { Tabular::COPY_ROW, "copy-row" },
113 { Tabular::COPY_COLUMN, "copy-column" },
114 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
115 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
116 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
117 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
118 { Tabular::ALIGN_LEFT, "align-left" },
119 { Tabular::ALIGN_RIGHT, "align-right" },
120 { Tabular::ALIGN_CENTER, "align-center" },
121 { Tabular::ALIGN_BLOCK, "align-block" },
122 { Tabular::VALIGN_TOP, "valign-top" },
123 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
124 { Tabular::VALIGN_MIDDLE, "valign-middle" },
125 { Tabular::M_ALIGN_LEFT, "m-align-left" },
126 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
127 { Tabular::M_ALIGN_CENTER, "m-align-center" },
128 { Tabular::M_VALIGN_TOP, "m-valign-top" },
129 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
130 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
131 { Tabular::MULTICOLUMN, "multicolumn" },
132 { Tabular::SET_ALL_LINES, "set-all-lines" },
133 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
134 { Tabular::SET_LONGTABULAR, "set-longtabular" },
135 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
136 { Tabular::SET_PWIDTH, "set-pwidth" },
137 { Tabular::SET_MPWIDTH, "set-mpwidth" },
138 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
139 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
140 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
141 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
142 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
143 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
144 { Tabular::SET_USEBOX, "set-usebox" },
145 { Tabular::SET_LTHEAD, "set-lthead" },
146 { Tabular::UNSET_LTHEAD, "unset-lthead" },
147 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
148 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
149 { Tabular::SET_LTFOOT, "set-ltfoot" },
150 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
151 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
152 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
153 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
154 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption" },
155 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
156 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
157 { Tabular::SET_BOOKTABS, "set-booktabs" },
158 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
159 { Tabular::SET_TOP_SPACE, "set-top-space" },
160 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
161 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
162 { Tabular::SET_BORDER_LINES, "set-border-lines" },
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 size_t 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::CellData::CellData(Buffer const & buf)
478 multicolumn(Tabular::CELL_NORMAL),
479 alignment(LYX_ALIGN_CENTER),
480 valignment(LYX_VALIGN_TOP),
487 inset(new InsetTableCell(buf))
489 inset->setBuffer(const_cast<Buffer &>(buf));
490 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
494 Tabular::CellData::CellData(CellData const & cs)
497 multicolumn(cs.multicolumn),
498 alignment(cs.alignment),
499 valignment(cs.valignment),
500 top_line(cs.top_line),
501 bottom_line(cs.bottom_line),
502 left_line(cs.left_line),
503 right_line(cs.right_line),
506 align_special(cs.align_special),
508 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
511 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
517 void Tabular::CellData::swap(CellData & rhs)
519 std::swap(cellno, rhs.cellno);
520 std::swap(width, rhs.width);
521 std::swap(multicolumn, rhs.multicolumn);
522 std::swap(alignment, rhs.alignment);
523 std::swap(valignment, rhs.valignment);
524 std::swap(top_line, rhs.top_line);
525 std::swap(bottom_line, rhs.bottom_line);
526 std::swap(left_line, rhs.left_line);
527 std::swap(right_line, rhs.right_line);
528 std::swap(usebox, rhs.usebox);
529 std::swap(rotate, rhs.rotate);
530 std::swap(align_special, rhs.align_special);
531 p_width.swap(rhs.p_width);
532 inset.swap(rhs.inset);
536 Tabular::RowData::RowData()
539 top_space_default(false),
540 bottom_space_default(false),
541 interline_space_default(false),
551 Tabular::ColumnData::ColumnData()
552 : alignment(LYX_ALIGN_CENTER),
553 valignment(LYX_VALIGN_TOP),
559 Tabular::ltType::ltType()
566 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
568 init(buffer, rows_arg, columns_arg);
572 // activates all lines and sets all widths to 0
573 void Tabular::init(Buffer const & buf, row_type rows_arg,
574 col_type columns_arg)
577 row_info = row_vector(rows_arg);
578 column_info = column_vector(columns_arg);
579 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
580 row_info.reserve(10);
581 column_info.reserve(10);
582 cell_info.reserve(100);
584 is_long_tabular = false;
586 use_booktabs = false;
587 size_t row_count = row_info.size();
588 size_t column_count = column_info.size();
589 // set silly default lines
590 for (row_type i = 0; i < row_count; ++i)
591 for (col_type j = 0; j < column_count; ++j) {
592 cell_info[i][j].top_line = true;
593 cell_info[i][j].left_line = true;
594 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
595 cell_info[i][j].right_line = j == column_count - 1;
600 void Tabular::appendRow(idx_type const cell)
602 BufferParams const & bp = buffer().params();
603 row_type const row = cellRow(cell);
605 row_vector::iterator rit = row_info.begin() + row;
606 row_info.insert(rit, RowData());
607 // now set the values of the row before
608 row_info[row] = row_info[row + 1];
610 row_type const nrows = row_info.size();
611 col_type const ncols = column_info.size();
613 cell_vvector old(nrows - 1);
614 for (row_type i = 0; i < nrows - 1; ++i)
615 swap(cell_info[i], old[i]);
617 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer())));
619 for (row_type i = 0; i <= row; ++i)
620 swap(cell_info[i], old[i]);
621 for (row_type i = row + 2; i < nrows; ++i)
622 swap(cell_info[i], old[i - 1]);
625 for (col_type c = 0; c < ncols; ++c) {
626 // inherit line settings
627 idx_type const i = cellIndex(row + 1, c);
628 idx_type const j = cellIndex(row, c);
629 setLeftLine(i, leftLine(j));
630 setRightLine(i, rightLine(j));
631 setTopLine(i, topLine(j));
632 if (topLine(j) && bottomLine(j)) {
633 setBottomLine(i, true);
634 setBottomLine(j, false);
636 // mark track changes
638 cellInfo(i).inset->setChange(Change(Change::INSERTED));
643 void Tabular::deleteRow(row_type const row)
645 // Not allowed to delete last row
646 if (row_info.size() == 1)
649 row_info.erase(row_info.begin() + row);
650 cell_info.erase(cell_info.begin() + row);
655 void Tabular::copyRow(row_type const row)
657 row_info.insert(row_info.begin() + row, row_info[row]);
658 cell_info.insert(cell_info.begin() + row, cell_info[row]);
660 if (buffer().params().trackChanges)
661 for (col_type j = 0; j < column_info.size(); ++j)
662 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
668 void Tabular::appendColumn(idx_type const cell)
670 col_type const c = cellColumn(cell);
671 column_vector::iterator cit = column_info.begin() + c + 1;
672 column_info.insert(cit, ColumnData());
673 row_type const nrows = row_info.size();
674 // set the column values of the column before
675 column_info[c + 1] = column_info[c];
677 for (row_type r = 0; r < nrows; ++r) {
678 cell_info[r].insert(cell_info[r].begin() + c + 1,
680 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
681 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
683 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
686 for (row_type r = 0; r < nrows; ++r) {
687 // inherit line settings
688 idx_type const i = cellIndex(r, c + 1);
689 idx_type const j = cellIndex(r, c);
690 setBottomLine(i, bottomLine(j));
691 setTopLine(i, topLine(j));
692 setLeftLine(i, leftLine(j));
693 if (rightLine(j) && rightLine(j)) {
694 setRightLine(i, true);
695 setRightLine(j, false);
698 cellInfo(i).inset->clear();
699 if (buffer().params().trackChanges)
700 cellInfo(i).inset->setChange(Change(Change::INSERTED));
705 void Tabular::deleteColumn(col_type const column)
707 // Not allowed to delete last column
708 if (column_info.size() == 1)
711 column_info.erase(column_info.begin() + column);
712 size_t row_count = row_info.size();
713 for (row_type i = 0; i < row_count; ++i) {
714 // Care about multicolumn cells
715 if (column + 1 < column_info.size() &&
716 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
717 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
718 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
720 cell_info[i].erase(cell_info[i].begin() + column);
726 void Tabular::copyColumn(col_type const column)
728 BufferParams const & bp = buffer().params();
729 column_info.insert(column_info.begin() + column, column_info[column]);
731 size_t row_count = row_info.size();
732 for (row_type i = 0; i < row_count; ++i)
733 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
736 for (row_type i = 0; i < row_count; ++i)
737 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
742 void Tabular::updateIndexes()
744 col_type ncols = column_info.size();
745 row_type nrows = row_info.size();
747 for (row_type row = 0; row < nrows; ++row)
748 for (col_type column = 0; column < ncols; ++column) {
749 if (!isPartOfMultiColumn(row, column))
751 cell_info[row][column].cellno = numberofcells - 1;
754 rowofcell.resize(numberofcells);
755 columnofcell.resize(numberofcells);
757 for (row_type row = 0; row < nrows; ++row)
758 for (col_type column = 0; column < ncols; ++column) {
759 if (isPartOfMultiColumn(row, column))
762 columnofcell[i] = column;
768 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
770 row_type const row = cellRow(cell);
772 for (col_type i = 0; i < column_info.size(); ++i)
773 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
779 bool Tabular::topLine(idx_type const cell) const
781 return cellInfo(cell).top_line;
785 bool Tabular::bottomLine(idx_type const cell) const
787 return cellInfo(cell).bottom_line;
791 bool Tabular::leftLine(idx_type cell) const
795 return cellInfo(cell).left_line;
799 bool Tabular::rightLine(idx_type cell) const
803 return cellInfo(cell).right_line;
807 bool Tabular::topAlreadyDrawn(idx_type cell) const
809 row_type row = cellRow(cell);
812 idx_type i = cellIndex(row - 1, cellColumn(cell));
813 return !rowTopLine(row) && bottomLine(i);
817 bool Tabular::leftAlreadyDrawn(idx_type cell) const
819 col_type col = cellColumn(cell);
822 idx_type i = cellIndex(cellRow(cell), col - 1);
823 return rightLine(i) && !leftLine(cell);
827 bool Tabular::isLastRow(idx_type cell) const
829 return cellRow(cell) == row_info.size() - 1;
833 int Tabular::getAdditionalHeight(row_type row) const
835 if (!row || row >= row_info.size())
838 int const interline_space = row_info[row - 1].interline_space_default ?
840 row_info[row - 1].interline_space.inPixels(width());
841 if (rowTopLine(row) && rowBottomLine(row - 1))
842 return interline_space + WIDTH_OF_LINE;
843 return interline_space;
847 int Tabular::getAdditionalWidth(idx_type cell) const
849 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
851 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
852 return WIDTH_OF_LINE;
857 int Tabular::columnWidth(idx_type cell) const
860 col_type const span = columnSpan(cell);
861 col_type const col = cellColumn(cell);
862 for(col_type c = col; c < col + span ; ++c)
863 w += column_info[c].width;
868 bool Tabular::updateColumnWidths()
870 col_type const ncols = column_info.size();
871 row_type const nrows = row_info.size();
873 // for each col get max of single col cells
874 for(col_type c = 0; c < ncols; ++c) {
876 for(row_type r = 0; r < nrows; ++r) {
877 idx_type const i = cellIndex(r, c);
878 if (columnSpan(i) == 1)
879 new_width = max(new_width, cellInfo(i).width);
882 if (column_info[c].width != new_width) {
883 column_info[c].width = new_width;
887 // update col widths to fit merged cells
888 for(col_type c = 0; c < ncols; ++c)
889 for(row_type r = 0; r < nrows; ++r) {
890 idx_type const i = cellIndex(r, c);
891 int const span = columnSpan(i);
892 if (span == 1 || c > cellColumn(i))
896 for(col_type j = c; j < c + span ; ++j)
897 old_width += column_info[j].width;
899 if (cellInfo(i).width > old_width) {
900 column_info[c + span - 1].width += cellInfo(i).width - old_width;
909 int Tabular::width() const
911 col_type const ncols = column_info.size();
913 for (col_type i = 0; i < ncols; ++i)
914 width += column_info[i].width;
919 void Tabular::setCellWidth(idx_type cell, int new_width)
921 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
922 + getAdditionalWidth(cell);
926 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
929 if (!isMultiColumn(cell) || onlycolumn)
930 column_info[cellColumn(cell)].alignment = align;
932 cellInfo(cell).alignment = align;
936 void Tabular::setVAlignment(idx_type cell, VAlignment align,
939 if (!isMultiColumn(cell) || onlycolumn)
940 column_info[cellColumn(cell)].valignment = align;
942 cellInfo(cell).valignment = align;
949 * Allow line and paragraph breaks for fixed width cells or disallow them,
950 * merge cell paragraphs and reset layout to standard for variable width
953 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
955 inset->setAutoBreakRows(fixedWidth);
956 inset->toggleFixedWidth(fixedWidth);
960 // merge all paragraphs to one
961 BufferParams const & bp = cur.bv().buffer().params();
962 while (inset->paragraphs().size() > 1)
963 mergeParagraph(bp, inset->paragraphs(), 0);
967 // undo information has already been recorded
968 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
969 bp.documentClass().emptyLayoutName());
976 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
977 Length const & width)
979 col_type const j = cellColumn(cell);
981 column_info[j].p_width = width;
982 for (row_type i = 0; i < row_info.size(); ++i) {
983 idx_type const cell = cellIndex(i, j);
984 // because of multicolumns
985 toggleFixedWidth(cur, cellInset(cell).get(),
986 !getPWidth(cell).zero());
988 // cur paragraph can become invalid after paragraphs were merged
989 if (cur.pit() > cur.lastpit())
990 cur.pit() = cur.lastpit();
991 // cur position can become invalid after newlines were removed
992 if (cur.pos() > cur.lastpos())
993 cur.pos() = cur.lastpos();
997 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
998 Length const & width)
1000 if (!isMultiColumn(cell))
1003 cellInfo(cell).p_width = width;
1004 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1005 // cur paragraph can become invalid after paragraphs were merged
1006 if (cur.pit() > cur.lastpit())
1007 cur.pit() = cur.lastpit();
1008 // cur position can become invalid after newlines were removed
1009 if (cur.pos() > cur.lastpos())
1010 cur.pos() = cur.lastpos();
1015 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1016 Tabular::Feature what)
1018 if (what == SET_SPECIAL_MULTI)
1019 cellInfo(cell).align_special = special;
1021 column_info[cellColumn(cell)].align_special = special;
1025 void Tabular::setAllLines(idx_type cell, bool line)
1027 setTopLine(cell, line);
1028 setBottomLine(cell, line);
1029 setRightLine(cell, line);
1030 setLeftLine(cell, line);
1034 void Tabular::setTopLine(idx_type i, bool line)
1036 cellInfo(i).top_line = line;
1040 void Tabular::setBottomLine(idx_type i, bool line)
1042 cellInfo(i).bottom_line = line;
1046 void Tabular::setLeftLine(idx_type cell, bool line)
1048 cellInfo(cell).left_line = line;
1052 void Tabular::setRightLine(idx_type cell, bool line)
1054 cellInfo(cell).right_line = line;
1057 bool Tabular::rowTopLine(row_type r) const
1059 idx_type i0 = getFirstCellInRow(r);
1060 idx_type i1 = getLastCellInRow(r);
1061 bool all_rows_set = true;
1062 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1063 all_rows_set = cellInfo(j).top_line;
1064 return all_rows_set;
1068 bool Tabular::rowBottomLine(row_type r) const
1070 idx_type i0 = getFirstCellInRow(r);
1071 idx_type i1 = getLastCellInRow(r);
1072 bool all_rows_set = true;
1073 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1074 all_rows_set = cellInfo(j).bottom_line;
1075 return all_rows_set;
1079 bool Tabular::columnLeftLine(col_type c) const
1086 row_type const nrows = row_info.size();
1087 for (row_type r = 0; r < nrows; ++r) {
1088 idx_type i = cellIndex(r, c);
1089 if (c == cellColumn(i)) {
1091 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1092 if (cellInfo(i).left_line || right)
1096 return 2 * nrows_left >= total;
1100 bool Tabular::columnRightLine(col_type c) const
1105 int nrows_right = 0;
1107 row_type const nrows = row_info.size();
1108 for (row_type r = 0; r < nrows; ++r) {
1109 idx_type i = cellIndex(r, c);
1110 if (c == cellColumn(i) + columnSpan(i) - 1) {
1112 bool left = c + 1 < column_info.size()
1113 && cellInfo(cellIndex(r, c + 1)).left_line
1114 || c + 1 == column_info.size();
1115 if (cellInfo(i).right_line && left)
1119 return 2 * nrows_right >= total;
1123 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1125 if (!onlycolumn && isMultiColumn(cell))
1126 return cellInfo(cell).alignment;
1127 return column_info[cellColumn(cell)].alignment;
1132 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1134 if (!onlycolumn && isMultiColumn(cell))
1135 return cellInfo(cell).valignment;
1136 return column_info[cellColumn(cell)].valignment;
1140 Length const Tabular::getPWidth(idx_type cell) const
1142 if (isMultiColumn(cell))
1143 return cellInfo(cell).p_width;
1144 return column_info[cellColumn(cell)].p_width;
1148 int Tabular::cellWidth(idx_type cell) const
1150 return cellInfo(cell).width;
1154 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1158 switch (getAlignment(cell)) {
1159 case LYX_ALIGN_CENTER:
1160 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1162 case LYX_ALIGN_RIGHT:
1163 x += columnWidth(cell) - cellWidth(cell);
1164 // + getAdditionalWidth(cell);
1167 // LYX_ALIGN_LEFT: nothing :-)
1171 // the LaTeX Way :-(
1177 bool Tabular::isFirstCellInRow(idx_type cell) const
1179 return cellColumn(cell) == 0;
1183 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1185 if (row > row_info.size() - 1)
1186 row = row_info.size() - 1;
1187 return cell_info[row][0].cellno;
1191 bool Tabular::isLastCellInRow(idx_type cell) const
1193 return cellRightColumn(cell) == column_info.size() - 1;
1197 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1199 if (row > row_info.size() - 1)
1200 row = row_info.size() - 1;
1201 return cell_info[row][column_info.size() - 1].cellno;
1205 Tabular::row_type Tabular::cellRow(idx_type cell) const
1207 if (cell >= numberofcells)
1208 return row_info.size() - 1;
1211 return rowofcell[cell];
1215 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1217 if (cell >= numberofcells)
1218 return column_info.size() - 1;
1221 return columnofcell[cell];
1225 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1227 row_type const row = cellRow(cell);
1228 col_type column = cellColumn(cell);
1229 while (column < column_info.size() - 1 &&
1230 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1236 void Tabular::write(ostream & os) const
1240 << write_attribute("version", 3)
1241 << write_attribute("rows", row_info.size())
1242 << write_attribute("columns", column_info.size())
1244 // global longtable options
1246 << write_attribute("rotate", rotate)
1247 << write_attribute("booktabs", use_booktabs)
1248 << write_attribute("islongtable", is_long_tabular)
1249 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1250 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1251 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1252 << write_attribute("headTopDL", endhead.topDL)
1253 << write_attribute("headBottomDL", endhead.bottomDL)
1254 << write_attribute("footTopDL", endfoot.topDL)
1255 << write_attribute("footBottomDL", endfoot.bottomDL)
1256 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1257 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1258 << write_attribute("lastFootEmpty", endlastfoot.empty)
1260 for (col_type j = 0; j < column_info.size(); ++j) {
1262 << write_attribute("alignment", column_info[j].alignment)
1263 << write_attribute("valignment", column_info[j].valignment)
1264 << write_attribute("width", column_info[j].p_width.asString())
1265 << write_attribute("special", column_info[j].align_special)
1268 for (row_type i = 0; i < row_info.size(); ++i) {
1269 static const string def("default");
1271 if (row_info[i].top_space_default)
1272 os << write_attribute("topspace", def);
1274 os << write_attribute("topspace", row_info[i].top_space);
1275 if (row_info[i].bottom_space_default)
1276 os << write_attribute("bottomspace", def);
1278 os << write_attribute("bottomspace", row_info[i].bottom_space);
1279 if (row_info[i].interline_space_default)
1280 os << write_attribute("interlinespace", def);
1282 os << write_attribute("interlinespace", row_info[i].interline_space);
1283 os << write_attribute("endhead", row_info[i].endhead)
1284 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1285 << write_attribute("endfoot", row_info[i].endfoot)
1286 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1287 << write_attribute("newpage", row_info[i].newpage)
1288 << write_attribute("caption", row_info[i].caption)
1290 for (col_type j = 0; j < column_info.size(); ++j) {
1292 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1293 << write_attribute("alignment", cell_info[i][j].alignment)
1294 << write_attribute("valignment", cell_info[i][j].valignment)
1295 << write_attribute("topline", cell_info[i][j].top_line)
1296 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1297 << write_attribute("leftline", cell_info[i][j].left_line)
1298 << write_attribute("rightline", cell_info[i][j].right_line)
1299 << write_attribute("rotate", cell_info[i][j].rotate)
1300 << write_attribute("usebox", cell_info[i][j].usebox)
1301 << write_attribute("width", cell_info[i][j].p_width)
1302 << write_attribute("special", cell_info[i][j].align_special)
1304 os << "\\begin_inset ";
1305 cell_info[i][j].inset->write(os);
1306 os << "\n\\end_inset\n"
1311 os << "</lyxtabular>\n";
1315 void Tabular::read(Lexer & lex)
1318 istream & is = lex.getStream();
1320 l_getline(is, line);
1321 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1322 LASSERT(false, /**/);
1327 if (!getTokenValue(line, "version", version))
1329 LASSERT(version >= 2, /**/);
1332 if (!getTokenValue(line, "rows", rows_arg))
1335 if (!getTokenValue(line, "columns", columns_arg))
1337 init(buffer(), rows_arg, columns_arg);
1338 l_getline(is, line);
1339 if (!prefixIs(line, "<features")) {
1340 lyxerr << "Wrong tabular format (expected <features ...> got"
1341 << line << ')' << endl;
1344 getTokenValue(line, "rotate", rotate);
1345 getTokenValue(line, "booktabs", use_booktabs);
1346 getTokenValue(line, "islongtable", is_long_tabular);
1347 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1348 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1349 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1350 getTokenValue(line, "headTopDL", endhead.topDL);
1351 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1352 getTokenValue(line, "footTopDL", endfoot.topDL);
1353 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1354 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1355 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1356 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1358 for (col_type j = 0; j < column_info.size(); ++j) {
1360 if (!prefixIs(line,"<column")) {
1361 lyxerr << "Wrong tabular format (expected <column ...> got"
1362 << line << ')' << endl;
1365 getTokenValue(line, "alignment", column_info[j].alignment);
1366 getTokenValue(line, "valignment", column_info[j].valignment);
1367 getTokenValue(line, "width", column_info[j].p_width);
1368 getTokenValue(line, "special", column_info[j].align_special);
1371 for (row_type i = 0; i < row_info.size(); ++i) {
1372 l_getline(is, line);
1373 if (!prefixIs(line, "<row")) {
1374 lyxerr << "Wrong tabular format (expected <row ...> got"
1375 << line << ')' << endl;
1378 getTokenValue(line, "topspace", row_info[i].top_space,
1379 row_info[i].top_space_default);
1380 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1381 row_info[i].bottom_space_default);
1382 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1383 row_info[i].interline_space_default);
1384 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1385 getTokenValue(line, "endhead", row_info[i].endhead);
1386 getTokenValue(line, "endfoot", row_info[i].endfoot);
1387 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1388 getTokenValue(line, "newpage", row_info[i].newpage);
1389 getTokenValue(line, "caption", row_info[i].caption);
1390 for (col_type j = 0; j < column_info.size(); ++j) {
1391 l_getline(is, line);
1392 if (!prefixIs(line, "<cell")) {
1393 lyxerr << "Wrong tabular format (expected <cell ...> got"
1394 << line << ')' << endl;
1397 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1398 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1399 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1400 getTokenValue(line, "topline", cell_info[i][j].top_line);
1401 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1402 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1403 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1404 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1405 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1406 getTokenValue(line, "width", cell_info[i][j].p_width);
1407 getTokenValue(line, "special", cell_info[i][j].align_special);
1408 l_getline(is, line);
1409 if (prefixIs(line, "\\begin_inset")) {
1410 cell_info[i][j].inset->read(lex);
1411 l_getline(is, line);
1413 if (!prefixIs(line, "</cell>")) {
1414 lyxerr << "Wrong tabular format (expected </cell> got"
1415 << line << ')' << endl;
1419 l_getline(is, line);
1420 if (!prefixIs(line, "</row>")) {
1421 lyxerr << "Wrong tabular format (expected </row> got"
1422 << line << ')' << endl;
1426 while (!prefixIs(line, "</lyxtabular>")) {
1427 l_getline(is, line);
1433 bool Tabular::isMultiColumn(idx_type cell) const
1435 return cellInfo(cell).multicolumn != CELL_NORMAL;
1439 bool Tabular::isMultiColumnReal(idx_type cell) const
1441 return cellColumn(cell) != cellRightColumn(cell) &&
1442 cellInfo(cell).multicolumn != CELL_NORMAL;
1446 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1448 return cell_info[cellRow(cell)][cellColumn(cell)];
1452 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1454 CellData & cs = cellInfo(cell);
1455 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1456 cs.alignment = column_info[cellColumn(cell)].alignment;
1457 setRightLine(cell, rightLine(cell + number - 1));
1458 for (idx_type i = 1; i < number; ++i) {
1459 CellData & cs1 = cellInfo(cell + i);
1460 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1461 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1468 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1470 row_type const row = cellRow(cell);
1471 col_type const ncols = column_info.size();
1472 idx_type result = 1;
1473 col_type column = cellColumn(cell) + 1;
1474 while (column < ncols && isPartOfMultiColumn(row, column)) {
1482 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1484 row_type const row = cellRow(cell);
1485 col_type column = cellColumn(cell);
1487 idx_type result = 0;
1489 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1490 cell_info[row][column].multicolumn = CELL_NORMAL;
1492 while (column < column_info.size() &&
1493 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1495 cell_info[row][column].multicolumn = CELL_NORMAL;
1505 void Tabular::setRotateCell(idx_type cell, bool flag)
1507 cellInfo(cell).rotate = flag;
1511 bool Tabular::getRotateCell(idx_type cell) const
1513 return cellInfo(cell).rotate;
1517 bool Tabular::needRotating() const
1521 for (row_type i = 0; i < row_info.size(); ++i)
1522 for (col_type j = 0; j < column_info.size(); ++j)
1523 if (cell_info[i][j].rotate)
1529 bool Tabular::isLastCell(idx_type cell) const
1531 if (cell + 1 < numberofcells)
1537 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1539 if (cellRow(cell) > 0)
1540 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1545 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1547 if (cellRow(cell) + 1 < row_info.size())
1548 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1553 Tabular::idx_type Tabular::cellIndex(row_type row,
1554 col_type column) const
1556 BOOST_ASSERT(column != npos && column < column_info.size()
1557 && row != npos && row < row_info.size());
1558 return cell_info[row][column].cellno;
1562 void Tabular::setUsebox(idx_type cell, BoxType type)
1564 cellInfo(cell).usebox = type;
1568 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1569 // adds line breaks, see bug 4886.
1570 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1572 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1573 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1575 if (cellInfo(cell).usebox > 1)
1576 return cellInfo(cell).usebox;
1577 return useParbox(cell);
1582 // This are functions used for the longtable support
1584 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1590 row_info[row].endfirsthead = flag;
1594 row_info[row].endhead = flag;
1599 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1602 hd.set = haveLTHead();
1603 return row_info[row].endhead;
1607 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1610 hd.set = haveLTFirstHead();
1611 return row_info[row].endfirsthead;
1615 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1621 row_info[row].endlastfoot = flag;
1625 row_info[row].endfoot = flag;
1630 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1633 fd.set = haveLTFoot();
1634 return row_info[row].endfoot;
1638 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1641 fd.set = haveLTLastFoot();
1642 return row_info[row].endlastfoot;
1646 void Tabular::setLTNewPage(row_type row, bool what)
1648 row_info[row].newpage = what;
1652 bool Tabular::getLTNewPage(row_type row) const
1654 return row_info[row].newpage;
1658 bool Tabular::haveLTHead() const
1660 for (row_type i = 0; i < row_info.size(); ++i)
1661 if (row_info[i].endhead)
1667 bool Tabular::haveLTFirstHead() const
1669 if (endfirsthead.empty)
1671 for (row_type i = 0; i < row_info.size(); ++i)
1672 if (row_info[i].endfirsthead)
1678 bool Tabular::haveLTFoot() const
1680 for (row_type i = 0; i < row_info.size(); ++i)
1681 if (row_info[i].endfoot)
1687 bool Tabular::haveLTLastFoot() const
1689 if (endlastfoot.empty)
1691 for (row_type i = 0; i < row_info.size(); ++i)
1692 if (row_info[i].endlastfoot)
1698 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1700 idx_type i = getFirstCellInRow(row);
1702 setMultiColumn(i, column_info.size());
1703 setTopLine(i, false);
1704 setBottomLine(i, false);
1705 setLeftLine(i, false);
1706 setRightLine(i, false);
1708 unsetMultiColumn(i);
1709 row_info[row].caption = what;
1714 bool Tabular::ltCaption(row_type row) const
1716 return row_info[row].caption;
1720 // end longtable support functions
1722 void Tabular::setRowAscent(row_type row, int height)
1724 if (row >= row_info.size() || row_info[row].ascent == height)
1726 row_info[row].ascent = height;
1730 void Tabular::setRowDescent(row_type row, int height)
1732 if (row >= row_info.size() || row_info[row].descent == height)
1734 row_info[row].descent = height;
1738 int Tabular::rowAscent(row_type row) const
1740 if (row >= row_info.size())
1742 return row_info[row].ascent;
1746 int Tabular::rowDescent(row_type row) const
1748 LASSERT(row < row_info.size(), /**/);
1749 return row_info[row].descent;
1753 int Tabular::height() const
1756 for (row_type row = 0; row < row_info.size(); ++row)
1757 height += rowAscent(row) + rowDescent(row) +
1758 getAdditionalHeight(row);
1763 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1765 LASSERT(row < row_info.size(), /**/);
1766 LASSERT(column < column_info.size(), /**/);
1767 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1771 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1773 // FIXME: assert or return 0 as in TeXBottomHLine()?
1774 LASSERT(row != npos, /**/);
1775 LASSERT(row < row_info.size(), /**/);
1777 idx_type const fcell = getFirstCellInRow(row);
1778 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1781 for (idx_type i = fcell; i < n; ++i) {
1785 if (use_booktabs && row == 0) {
1788 } else if (tmp == n - fcell) {
1789 os << (use_booktabs ? "\\midrule " : "\\hline ");
1791 for (idx_type i = fcell; i < n; ++i) {
1793 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1794 << cellColumn(i) + 1
1796 << cellRightColumn(i) + 1
1808 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1810 // FIXME: return 0 or assert as in TeXTopHLine()?
1811 if (row == npos || row >= row_info.size())
1814 idx_type const fcell = getFirstCellInRow(row);
1815 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1818 for (idx_type i = fcell; i < n; ++i) {
1822 if (use_booktabs && row == row_info.size() - 1) {
1823 if (bottomLine(fcell))
1824 os << "\\bottomrule";
1825 } else if (tmp == n - fcell) {
1826 os << (use_booktabs ? "\\midrule" : "\\hline");
1828 for (idx_type i = fcell; i < n; ++i) {
1829 if (bottomLine(i)) {
1830 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1831 << cellColumn(i) + 1
1833 << cellRightColumn(i) + 1
1845 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1848 row_type const r = cellRow(cell);
1849 if (is_long_tabular && row_info[r].caption)
1852 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1853 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1854 // figure out how to set the lines
1855 // we always set double lines to the right of the cell
1856 col_type const c = cellColumn(cell);
1857 col_type const nextcol = c + columnSpan(cell);
1858 bool colright = columnRightLine(c);
1859 bool colleft = columnLeftLine(c);
1860 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1861 bool nextcellleft = nextcol < column_info.size()
1862 && leftLine(cellIndex(r, nextcol));
1863 bool coldouble = colright && nextcolleft;
1864 bool celldouble = rightLine(cell) && nextcellleft;
1865 ismulticol = isMultiColumn(cell)
1866 || (c == 0 && colleft != leftLine(cell))
1867 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1868 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1869 || (coldouble != celldouble);
1871 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1872 if (c ==0 && leftLine(cell))
1874 if (!cellInfo(cell).align_special.empty()) {
1875 os << cellInfo(cell).align_special;
1877 if (!getPWidth(cell).zero()) {
1879 case LYX_VALIGN_TOP:
1882 case LYX_VALIGN_MIDDLE:
1885 case LYX_VALIGN_BOTTOM:
1890 << from_ascii(getPWidth(cell).asLatexString())
1894 case LYX_ALIGN_LEFT:
1897 case LYX_ALIGN_RIGHT:
1904 } // end if else !getPWidth
1905 } // end if else !cellinfo_of_cell
1906 if (rightLine(cell) || nextcellleft)
1909 // add extra vertical line if we want a double one
1913 if (getRotateCell(cell)) {
1914 os << "\\begin{sideways}\n";
1917 if (getUsebox(cell) == BOX_PARBOX) {
1920 case LYX_VALIGN_TOP:
1923 case LYX_VALIGN_MIDDLE:
1926 case LYX_VALIGN_BOTTOM:
1930 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1932 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1933 os << "\\begin{minipage}[";
1935 case LYX_VALIGN_TOP:
1938 case LYX_VALIGN_MIDDLE:
1941 case LYX_VALIGN_BOTTOM:
1945 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1953 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1956 row_type const r = cellRow(cell);
1957 if (is_long_tabular && row_info[r].caption)
1961 if (getUsebox(cell) == BOX_PARBOX)
1963 else if (getUsebox(cell) == BOX_MINIPAGE) {
1964 os << "%\n\\end{minipage}";
1967 if (getRotateCell(cell)) {
1968 os << "%\n\\end{sideways}";
1978 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1979 OutputParams const & runparams) const
1981 if (!is_long_tabular)
1985 // output header info
1987 if (endhead.topDL) {
1991 for (row_type i = 0; i < row_info.size(); ++i) {
1992 if (row_info[i].endhead) {
1993 ret += TeXRow(os, i, runparams);
1996 if (endhead.bottomDL) {
2000 os << "\\endhead\n";
2002 if (endfirsthead.empty) {
2003 os << "\\endfirsthead\n";
2007 // output firstheader info
2008 if (haveLTFirstHead()) {
2009 if (endfirsthead.topDL) {
2013 for (row_type i = 0; i < row_info.size(); ++i) {
2014 if (row_info[i].endfirsthead) {
2015 ret += TeXRow(os, i, runparams);
2018 if (endfirsthead.bottomDL) {
2022 os << "\\endfirsthead\n";
2025 // output footer info
2027 if (endfoot.topDL) {
2031 for (row_type i = 0; i < row_info.size(); ++i) {
2032 if (row_info[i].endfoot) {
2033 ret += TeXRow(os, i, runparams);
2036 if (endfoot.bottomDL) {
2040 os << "\\endfoot\n";
2042 if (endlastfoot.empty) {
2043 os << "\\endlastfoot\n";
2047 // output lastfooter info
2048 if (haveLTLastFoot()) {
2049 if (endlastfoot.topDL) {
2053 for (row_type i = 0; i < row_info.size(); ++i) {
2054 if (row_info[i].endlastfoot) {
2055 ret += TeXRow(os, i, runparams);
2058 if (endlastfoot.bottomDL) {
2062 os << "\\endlastfoot\n";
2069 bool Tabular::isValidRow(row_type row) const
2071 if (!is_long_tabular)
2073 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2074 !row_info[row].endfoot && !row_info[row].endlastfoot;
2078 int Tabular::TeXRow(odocstream & os, row_type i,
2079 OutputParams const & runparams) const
2081 idx_type cell = cellIndex(i, 0);
2082 int ret = TeXTopHLine(os, i);
2083 if (row_info[i].top_space_default) {
2085 os << "\\addlinespace\n";
2087 os << "\\noalign{\\vskip\\doublerulesep}\n";
2089 } else if(!row_info[i].top_space.zero()) {
2091 os << "\\addlinespace["
2092 << from_ascii(row_info[i].top_space.asLatexString())
2095 os << "\\noalign{\\vskip"
2096 << from_ascii(row_info[i].top_space.asLatexString())
2101 bool ismulticol = false;
2102 for (col_type j = 0; j < column_info.size(); ++j) {
2103 if (isPartOfMultiColumn(i, j))
2105 ret += TeXCellPreamble(os, cell, ismulticol);
2106 shared_ptr<InsetTableCell> inset = cellInset(cell);
2108 Paragraph const & par = inset->paragraphs().front();
2109 bool rtl = par.isRTL(buffer().params())
2111 && getPWidth(cell).zero();
2114 if (par.getParLanguage(buffer().params())->lang() ==
2117 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2119 // currently, remaning RTL languages are arabic_arabtex and hebrew
2123 ret += inset->latex(os, runparams);
2127 ret += TeXCellPostamble(os, cell, ismulticol);
2128 if (!isLastCellInRow(cell)) { // not last cell in row
2133 os << "\\tabularnewline";
2134 if (row_info[i].bottom_space_default) {
2136 os << "\\addlinespace";
2138 os << "[\\doublerulesep]";
2139 } else if (!row_info[i].bottom_space.zero()) {
2141 os << "\\addlinespace";
2143 << from_ascii(row_info[i].bottom_space.asLatexString())
2148 ret += TeXBottomHLine(os, i);
2149 if (row_info[i].interline_space_default) {
2151 os << "\\addlinespace\n";
2153 os << "\\noalign{\\vskip\\doublerulesep}\n";
2155 } else if (!row_info[i].interline_space.zero()) {
2157 os << "\\addlinespace["
2158 << from_ascii(row_info[i].interline_space.asLatexString())
2161 os << "\\noalign{\\vskip"
2162 << from_ascii(row_info[i].interline_space.asLatexString())
2170 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2174 //+---------------------------------------------------------------------
2175 //+ first the opening preamble +
2176 //+---------------------------------------------------------------------
2179 os << "\\begin{sideways}\n";
2182 if (is_long_tabular)
2183 os << "\\begin{longtable}{";
2185 os << "\\begin{tabular}{";
2187 for (col_type i = 0; i < column_info.size(); ++i) {
2188 if (columnLeftLine(i))
2190 if (!column_info[i].align_special.empty()) {
2191 os << column_info[i].align_special;
2193 if (!column_info[i].p_width.zero()) {
2194 switch (column_info[i].alignment) {
2195 case LYX_ALIGN_LEFT:
2196 os << ">{\\raggedright}";
2198 case LYX_ALIGN_RIGHT:
2199 os << ">{\\raggedleft}";
2201 case LYX_ALIGN_CENTER:
2202 os << ">{\\centering}";
2204 case LYX_ALIGN_NONE:
2205 case LYX_ALIGN_BLOCK:
2206 case LYX_ALIGN_LAYOUT:
2207 case LYX_ALIGN_SPECIAL:
2211 switch (column_info[i].valignment) {
2212 case LYX_VALIGN_TOP:
2215 case LYX_VALIGN_MIDDLE:
2218 case LYX_VALIGN_BOTTOM:
2223 << from_ascii(column_info[i].p_width.asLatexString())
2226 switch (column_info[i].alignment) {
2227 case LYX_ALIGN_LEFT:
2230 case LYX_ALIGN_RIGHT:
2237 } // end if else !column_info[i].p_width
2238 } // end if else !column_info[i].align_special
2239 if (columnRightLine(i))
2245 ret += TeXLongtableHeaderFooter(os, runparams);
2247 //+---------------------------------------------------------------------
2248 //+ the single row and columns (cells) +
2249 //+---------------------------------------------------------------------
2251 for (row_type i = 0; i < row_info.size(); ++i) {
2252 if (isValidRow(i)) {
2253 ret += TeXRow(os, i, runparams);
2254 if (is_long_tabular && row_info[i].newpage) {
2255 os << "\\newpage\n";
2261 //+---------------------------------------------------------------------
2262 //+ the closing of the tabular +
2263 //+---------------------------------------------------------------------
2265 if (is_long_tabular)
2266 os << "\\end{longtable}";
2268 os << "\\end{tabular}";
2270 os << "\n\\end{sideways}";
2278 int Tabular::docbookRow(odocstream & os, row_type row,
2279 OutputParams const & runparams) const
2282 idx_type cell = getFirstCellInRow(row);
2285 for (col_type j = 0; j < column_info.size(); ++j) {
2286 if (isPartOfMultiColumn(row, j))
2289 os << "<entry align=\"";
2290 switch (getAlignment(cell)) {
2291 case LYX_ALIGN_LEFT:
2294 case LYX_ALIGN_RIGHT:
2302 os << "\" valign=\"";
2303 switch (getVAlignment(cell)) {
2304 case LYX_VALIGN_TOP:
2307 case LYX_VALIGN_BOTTOM:
2310 case LYX_VALIGN_MIDDLE:
2315 if (isMultiColumn(cell)) {
2316 os << " namest=\"col" << j << "\" ";
2317 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2321 ret += cellInset(cell)->docbook(os, runparams);
2330 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2334 //+---------------------------------------------------------------------
2335 //+ first the opening preamble +
2336 //+---------------------------------------------------------------------
2338 os << "<tgroup cols=\"" << column_info.size()
2339 << "\" colsep=\"1\" rowsep=\"1\">\n";
2341 for (col_type i = 0; i < column_info.size(); ++i) {
2342 os << "<colspec colname=\"col" << i << "\" align=\"";
2343 switch (column_info[i].alignment) {
2344 case LYX_ALIGN_LEFT:
2347 case LYX_ALIGN_RIGHT:
2355 if (runparams.flavor == OutputParams::XML)
2361 //+---------------------------------------------------------------------
2362 //+ Long Tabular case +
2363 //+---------------------------------------------------------------------
2365 // output header info
2366 if (haveLTHead() || haveLTFirstHead()) {
2369 for (row_type i = 0; i < row_info.size(); ++i) {
2370 if (row_info[i].endhead || row_info[i].endfirsthead) {
2371 ret += docbookRow(os, i, runparams);
2377 // output footer info
2378 if (haveLTFoot() || haveLTLastFoot()) {
2381 for (row_type i = 0; i < row_info.size(); ++i) {
2382 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2383 ret += docbookRow(os, i, runparams);
2390 //+---------------------------------------------------------------------
2391 //+ the single row and columns (cells) +
2392 //+---------------------------------------------------------------------
2396 for (row_type i = 0; i < row_info.size(); ++i) {
2397 if (isValidRow(i)) {
2398 ret += docbookRow(os, i, runparams);
2403 //+---------------------------------------------------------------------
2404 //+ the closing of the tabular +
2405 //+---------------------------------------------------------------------
2414 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2415 vector<unsigned int> const & clen) const
2417 idx_type const fcell = getFirstCellInRow(row);
2418 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2421 for (idx_type i = fcell; i < n; ++i) {
2431 for (idx_type i = fcell; i < n; ++i) {
2442 col_type column = cellColumn(i);
2443 int len = clen[column];
2444 while (column < column_info.size() - 1
2445 && isPartOfMultiColumn(row, ++column))
2446 len += clen[column] + 4;
2447 os << docstring(len, ch);
2462 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2463 vector<unsigned int> const & clen) const
2465 idx_type const fcell = getFirstCellInRow(row);
2466 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2469 for (idx_type i = fcell; i < n; ++i) {
2470 if (bottomLine(i)) {
2479 for (idx_type i = fcell; i < n; ++i) {
2480 if (bottomLine(i)) {
2490 col_type column = cellColumn(i);
2491 int len = clen[column];
2492 while (column < column_info.size() -1
2493 && isPartOfMultiColumn(row, ++column))
2494 len += clen[column] + 4;
2495 os << docstring(len, ch);
2496 if (bottomLine(i)) {
2510 void Tabular::plaintextPrintCell(odocstream & os,
2511 OutputParams const & runparams,
2512 idx_type cell, row_type row, col_type column,
2513 vector<unsigned int> const & clen,
2514 bool onlydata) const
2516 odocstringstream sstr;
2517 cellInset(cell)->plaintext(sstr, runparams);
2529 unsigned int len1 = sstr.str().length();
2530 unsigned int len2 = clen[column];
2531 while (column < column_info.size() -1
2532 && isPartOfMultiColumn(row, ++column))
2533 len2 += clen[column] + 4;
2536 switch (getAlignment(cell)) {
2538 case LYX_ALIGN_LEFT:
2541 case LYX_ALIGN_RIGHT:
2545 case LYX_ALIGN_CENTER:
2551 os << docstring(len1, ' ') << sstr.str()
2552 << docstring(len2, ' ');
2554 if (rightLine(cell))
2561 void Tabular::plaintext(odocstream & os,
2562 OutputParams const & runparams, int const depth,
2563 bool onlydata, char_type delim) const
2565 // first calculate the width of the single columns
2566 vector<unsigned int> clen(column_info.size());
2569 // first all non (real) multicolumn cells!
2570 for (col_type j = 0; j < column_info.size(); ++j) {
2572 for (row_type i = 0; i < row_info.size(); ++i) {
2573 idx_type cell = cellIndex(i, j);
2574 if (isMultiColumnReal(cell))
2576 odocstringstream sstr;
2577 cellInset(cell)->plaintext(sstr, runparams);
2578 if (clen[j] < sstr.str().length())
2579 clen[j] = sstr.str().length();
2582 // then all (real) multicolumn cells!
2583 for (col_type j = 0; j < column_info.size(); ++j) {
2584 for (row_type i = 0; i < row_info.size(); ++i) {
2585 idx_type cell = cellIndex(i, j);
2586 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2588 odocstringstream sstr;
2589 cellInset(cell)->plaintext(sstr, runparams);
2590 int len = int(sstr.str().length());
2591 idx_type const n = columnSpan(cell);
2592 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2594 if (len > int(clen[j + n - 1]))
2595 clen[j + n - 1] = len;
2600 for (row_type i = 0; i < row_info.size(); ++i) {
2601 if (!onlydata && plaintextTopHLine(os, i, clen))
2602 os << docstring(depth * 2, ' ');
2603 for (col_type j = 0; j < column_info.size(); ++j) {
2604 if (isPartOfMultiColumn(i, j))
2606 if (onlydata && j > 0)
2607 // we don't use operator<< for single UCS4 character.
2608 // see explanation in docstream.h
2610 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2615 os << docstring(depth * 2, ' ');
2616 if (plaintextBottomHLine(os, i, clen))
2617 os << docstring(depth * 2, ' ');
2623 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2625 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2629 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2630 col_type column) const
2632 return cell_info[row][column].inset;
2636 void Tabular::setCellInset(row_type row, col_type column,
2637 shared_ptr<InsetTableCell> ins) const
2639 CellData & cd = cell_info[row][column];
2644 void Tabular::validate(LaTeXFeatures & features) const
2646 features.require("NeedTabularnewline");
2648 features.require("booktabs");
2649 if (is_long_tabular)
2650 features.require("longtable");
2652 features.require("rotating");
2653 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2654 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2655 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2656 features.require("array");
2657 cellInset(cell)->validate(features);
2662 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2664 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2665 ParagraphList::const_iterator cit = parlist.begin();
2666 ParagraphList::const_iterator end = parlist.end();
2668 for (; cit != end; ++cit)
2669 for (int i = 0; i < cit->size(); ++i)
2670 if (cit->isNewline(i))
2677 /////////////////////////////////////////////////////////////////////
2681 /////////////////////////////////////////////////////////////////////
2683 InsetTableCell::InsetTableCell(Buffer const & buf)
2684 : InsetText(buf), isFixedWidth(false)
2688 bool InsetTableCell::forcePlainLayout(idx_type) const
2690 return !isFixedWidth;
2693 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2695 return isFixedWidth;
2698 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2699 FuncStatus & status) const
2702 switch (cmd.action) {
2704 enabled = !forcePlainLayout();
2706 case LFUN_LAYOUT_PARAGRAPH:
2707 enabled = allowParagraphCustomization();
2710 return InsetText::getStatus(cur, cmd, status);
2712 status.setEnabled(enabled);
2716 /////////////////////////////////////////////////////////////////////
2720 /////////////////////////////////////////////////////////////////////
2722 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2724 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2725 rowselect_(false), colselect_(false)
2727 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2731 InsetTabular::InsetTabular(InsetTabular const & tab)
2732 : Inset(tab), tabular(tab.tabular), scx_(0)
2734 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2738 InsetTabular::~InsetTabular()
2740 hideDialogs("tabular", this);
2744 bool InsetTabular::insetAllowed(InsetCode code) const
2746 if (code == MATHMACRO_CODE)
2753 void InsetTabular::write(ostream & os) const
2755 os << "Tabular" << endl;
2760 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2762 // FIXME: depending on the selection state, we could offer a different menu.
2763 return from_ascii("context-tabular");
2767 void InsetTabular::read(Lexer & lex)
2769 //bool const old_format = (lex.getString() == "\\LyXTable");
2777 string token = lex.getString();
2778 while (lex && token != "\\end_inset") {
2780 token = lex.getString();
2783 lex.printError("Missing \\end_inset at this point. ");
2787 int InsetTabular::rowFromY(Cursor & cur, int y) const
2789 // top y coordinate of tabular
2790 int h = yo(cur.bv()) - tabular.rowAscent(0);
2791 size_t nrows = tabular.row_info.size();
2793 for (; r < nrows && y > h; ++r) {
2794 h += tabular.rowAscent(r);
2795 h += tabular.rowDescent(r);
2796 h += tabular.getAdditionalHeight(r);
2802 int InsetTabular::columnFromX(Cursor & cur, int x) const
2804 // left x coordinate of tabular
2805 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2806 size_t ncols = tabular.column_info.size();
2808 for (; c < ncols && x > w; ++c)
2809 w += tabular.columnWidth(c);
2814 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2816 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2817 // mi.base.textwidth << "\n";
2820 LASSERT(false, /**/);
2824 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2827 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2828 if (tabular.isPartOfMultiColumn(i, j))
2829 // Multicolumn cell, but not first one
2834 if (tabular.cell_info[i][j].multicolumn ==
2835 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2836 p_width = tabular.cellInfo(cell).p_width;
2838 p_width = tabular.column_info[j].p_width;
2839 if (!p_width.zero())
2840 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2841 tabular.cellInset(cell)->metrics(m, dim);
2842 if (!p_width.zero())
2843 dim.wid = m.base.textwidth;
2844 tabular.setCellWidth(cell, dim.wid);
2845 maxAsc = max(maxAsc, dim.asc);
2846 maxDesc = max(maxDesc, dim.des);
2849 int const top_space = tabular.row_info[i].top_space_default ?
2850 default_line_space :
2851 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2852 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2853 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2854 default_line_space :
2855 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2856 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2858 tabular.updateColumnWidths();
2859 dim.asc = tabular.rowAscent(0);
2860 dim.des = tabular.height() - dim.asc;
2861 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2865 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2867 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2868 BufferView * bv = pi.base.bv;
2870 // FIXME: As the full backrgound is painted in drawSelection(),
2871 // we have no choice but to do a full repaint for the Text cells.
2872 pi.full_repaint = true;
2874 resetPos(bv->cursor());
2877 x += ADD_TO_TABULAR_WIDTH;
2879 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2882 first_visible_cell = Tabular::npos;
2883 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
2885 int const a = tabular.rowAscent(i);
2886 int const d = tabular.rowDescent(i);
2887 idx = tabular.cellIndex(i, 0);
2888 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2889 if (tabular.isPartOfMultiColumn(i, j))
2891 if (first_visible_cell == Tabular::npos)
2892 first_visible_cell = idx;
2894 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2895 // Cache the Inset position.
2896 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2897 if (nx + tabular.columnWidth(idx) < 0
2898 || nx > bv->workWidth()
2900 || y - a > bv->workHeight()) {
2901 pi.pain.setDrawingEnabled(false);
2902 cell(idx)->draw(pi, cx, y);
2903 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2904 pi.pain.setDrawingEnabled(original_drawing_state);
2906 cell(idx)->draw(pi, cx, y);
2907 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2909 nx += tabular.columnWidth(idx);
2913 if (i + 1 < tabular.row_info.size())
2914 y += d + tabular.rowAscent(i + 1) +
2915 tabular.getAdditionalHeight(i + 1);
2920 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2922 Cursor & cur = pi.base.bv->cursor();
2924 x += scx_ + ADD_TO_TABULAR_WIDTH;
2926 // FIXME: it is wrong to completely paint the background
2927 // if we want to do single row painting.
2929 // Paint background of current tabular
2930 int const w = tabular.width();
2931 int const h = tabular.height();
2932 int yy = y - tabular.rowAscent(0);
2933 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2935 if (!cur.selection())
2937 if (&cur.inset() != this)
2943 if (tablemode(cur)) {
2946 getSelection(cur, rs, re, cs, ce);
2947 y -= tabular.rowAscent(0);
2948 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
2949 int const a = tabular.rowAscent(j);
2950 int const h = a + tabular.rowDescent(j);
2952 y += tabular.getAdditionalHeight(j);
2953 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
2954 if (tabular.isPartOfMultiColumn(j, i))
2956 idx_type const cell =
2957 tabular.cellIndex(j, i);
2958 int const w = tabular.columnWidth(cell);
2959 if (i >= cs && i <= ce && j >= rs && j <= re)
2960 pi.pain.fillRectangle(xx, y, w, h,
2968 x += cellXPos(cur.idx());
2969 x += tabular.getBeginningOfTextInCell(cur.idx());
2970 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
2975 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
2976 row_type row, idx_type cell, bool erased) const
2978 int x2 = x + tabular.columnWidth(cell);
2979 bool on_off = false;
2980 ColorCode col = Color_tabularline;
2981 ColorCode onoffcol = Color_tabularonoffline;
2984 col = Color_deletedtext;
2985 onoffcol = Color_deletedtext;
2988 if (!tabular.topAlreadyDrawn(cell)) {
2989 on_off = !tabular.topLine(cell);
2990 pain.line(x, y - tabular.rowAscent(row),
2991 x2, y - tabular.rowAscent(row),
2992 on_off ? onoffcol : col,
2993 on_off ? Painter::line_onoffdash : Painter::line_solid);
2995 on_off = !tabular.bottomLine(cell);
2996 pain.line(x, y + tabular.rowDescent(row),
2997 x2, y + tabular.rowDescent(row),
2998 on_off ? onoffcol : col,
2999 on_off ? Painter::line_onoffdash : Painter::line_solid);
3000 if (!tabular.leftAlreadyDrawn(cell)) {
3001 on_off = !tabular.leftLine(cell);
3002 pain.line(x, y - tabular.rowAscent(row),
3003 x, y + tabular.rowDescent(row),
3004 on_off ? onoffcol : col,
3005 on_off ? Painter::line_onoffdash : Painter::line_solid);
3007 on_off = !tabular.rightLine(cell);
3008 pain.line(x2 - tabular.getAdditionalWidth(cell),
3009 y - tabular.rowAscent(row),
3010 x2 - tabular.getAdditionalWidth(cell),
3011 y + tabular.rowDescent(row),
3012 on_off ? onoffcol : col,
3013 on_off ? Painter::line_onoffdash : Painter::line_solid);
3017 docstring InsetTabular::editMessage() const
3019 return _("Opened table");
3023 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3025 //lyxerr << "InsetTabular::edit: " << this << endl;
3027 cur.selection() = false;
3030 if (isRightToLeft(cur))
3031 cur.idx() = tabular.getLastCellInRow(0);
3037 if (isRightToLeft(cur))
3038 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3040 cur.idx() = tabular.numberofcells - 1;
3042 cur.pos() = cur.lastpos(); // FIXME crude guess
3044 // FIXME: this accesses the position cache before it is initialized
3046 //cur.bv().fitCursor();
3050 void InsetTabular::updateLabels(ParIterator const & it)
3052 // In a longtable, tell captions what the current float is
3053 Counters & cnts = buffer().params().documentClass().counters();
3054 string const saveflt = cnts.current_float();
3055 if (tabular.is_long_tabular)
3056 cnts.current_float("table");
3058 ParIterator it2 = it;
3060 size_t const end = it2.nargs();
3061 for ( ; it2.idx() < end; it2.top().forwardIdx())
3062 lyx::updateLabels(buffer(), it2);
3065 if (tabular.is_long_tabular)
3066 cnts.current_float(saveflt);
3070 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3072 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3073 << "\n cur:" << cur);
3074 CursorSlice sl = cur.top();
3075 Cursor & bvcur = cur.bv().cursor();
3077 switch (cmd.action) {
3079 case LFUN_MOUSE_PRESS: {
3080 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3082 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3083 || cmd.x > xo(cur.bv()) + tabular.width()) {
3084 row_type r = rowFromY(cur, cmd.y);
3085 cur.idx() = tabular.getFirstCellInRow(r);
3088 cur.idx() = tabular.getLastCellInRow(r);
3089 cur.pos() = cur.lastpos();
3090 cur.selection() = true;
3096 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3097 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3098 || cmd.y > y0 + tabular.height()) {
3099 col_type c = columnFromX(cur, cmd.x);
3100 cur.idx() = tabular.cellIndex(0, c);
3103 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3104 cur.pos() = cur.lastpos();
3105 cur.selection() = true;
3110 // do not reset cursor/selection if we have selected
3111 // some cells (bug 2715).
3112 if (cmd.button() == mouse_button::button3
3113 && &bvcur.selBegin().inset() == this
3114 && tablemode(bvcur))
3117 // Let InsetTableCell do it
3118 cell(cur.idx())->dispatch(cur, cmd);
3121 case LFUN_MOUSE_MOTION:
3122 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3123 if (cmd.button() == mouse_button::button1) {
3124 // only accept motions to places not deeper nested than the real anchor
3125 if (!bvcur.anchor_.hasPart(cur)) {
3129 // select (additional) row
3131 row_type r = rowFromY(cur, cmd.y);
3132 cur.idx() = tabular.getLastCellInRow(r);
3133 bvcur.setCursor(cur);
3134 bvcur.selection() = true;
3137 // select (additional) column
3139 col_type c = columnFromX(cur, cmd.x);
3140 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3141 bvcur.setCursor(cur);
3142 bvcur.selection() = true;
3145 // only update if selection changes
3146 if (bvcur.idx() == cur.idx() &&
3147 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3149 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3150 bvcur.setCursor(cur);
3151 bvcur.selection() = true;
3155 case LFUN_MOUSE_RELEASE:
3160 case LFUN_CELL_BACKWARD:
3162 cur.selection() = false;
3165 case LFUN_CELL_FORWARD:
3167 cur.selection() = false;
3169 case LFUN_CHAR_FORWARD_SELECT:
3170 case LFUN_CHAR_FORWARD:
3171 case LFUN_CHAR_BACKWARD_SELECT:
3172 case LFUN_CHAR_BACKWARD:
3173 case LFUN_CHAR_RIGHT_SELECT:
3174 case LFUN_CHAR_RIGHT:
3175 case LFUN_CHAR_LEFT_SELECT:
3176 case LFUN_CHAR_LEFT: {
3178 // determine whether we move to next or previous cell, where to enter
3179 // the new cell from, and which command to "finish" (i.e., exit the
3182 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3183 FuncCode finish_lfun;
3185 if (cmd.action == LFUN_CHAR_FORWARD
3186 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3188 finish_lfun = LFUN_FINISHED_FORWARD;
3190 else if (cmd.action == LFUN_CHAR_BACKWARD
3191 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3193 finish_lfun = LFUN_FINISHED_BACKWARD;
3195 // LEFT or RIGHT commands --- the interpretation will depend on the
3196 // table's direction.
3198 bool right = (cmd.action == LFUN_CHAR_RIGHT
3199 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3200 next_cell = (isRightToLeft(cur) != right);
3202 if (lyxrc.visual_cursor) {
3203 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3207 finish_lfun = LFUN_FINISHED_RIGHT;
3209 finish_lfun = LFUN_FINISHED_LEFT;
3213 // finally, now that we know what we want to do, do it!
3214 cell(cur.idx())->dispatch(cur, cmd);
3215 if (!cur.result().dispatched()) {
3216 // move to next/prev cell, as appropriate
3217 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3218 << " cell from: " << int(entry_from));
3220 moveNextCell(cur, entry_from);
3222 movePrevCell(cur, entry_from);
3223 // if we're exiting the table, call the appropriate FINISHED lfun
3224 if (sl == cur.top())
3225 cmd = FuncRequest(finish_lfun);
3233 case LFUN_DOWN_SELECT:
3235 cell(cur.idx())->dispatch(cur, cmd);
3236 cur.dispatched(); // override the cell's decision
3237 if (sl == cur.top())
3238 // if our Text didn't do anything to the cursor
3239 // then we try to put the cursor into the cell below
3240 // setting also the right targetX.
3241 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3242 cur.idx() = tabular.cellBelow(cur.idx());
3244 TextMetrics const & tm =
3245 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3246 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3248 if (sl == cur.top()) {
3249 // we trick it to go to forward after leaving the
3251 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3256 case LFUN_UP_SELECT:
3258 cell(cur.idx())->dispatch(cur, cmd);
3259 cur.dispatched(); // override the cell's decision
3260 if (sl == cur.top())
3261 // if our Text didn't do anything to the cursor
3262 // then we try to put the cursor into the cell above
3263 // setting also the right targetX.
3264 if (tabular.cellRow(cur.idx()) != 0) {
3265 cur.idx() = tabular.cellAbove(cur.idx());
3266 cur.pit() = cur.lastpit();
3267 Text const * text = cell(cur.idx())->getText(0);
3268 TextMetrics const & tm = cur.bv().textMetrics(text);
3269 ParagraphMetrics const & pm =
3270 tm.parMetrics(cur.lastpit());
3271 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3273 if (sl == cur.top()) {
3274 cmd = FuncRequest(LFUN_UP);
3279 // case LFUN_SCREEN_DOWN: {
3280 // //if (hasSelection())
3281 // // cur.selection() = false;
3282 // col_type const col = tabular.cellColumn(cur.idx());
3283 // int const t = cur.bv().top_y() + cur.bv().height();
3284 // if (t < yo() + tabular.getHeightOfTabular()) {
3285 // cur.bv().scrollDocView(t);
3286 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3288 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3295 // case LFUN_SCREEN_UP: {
3296 // //if (hasSelection())
3297 // // cur.selection() = false;
3298 // col_type const col = tabular.cellColumn(cur.idx());
3299 // int const t = cur.bv().top_y() + cur.bv().height();
3301 // cur.bv().scrollDocView(t);
3305 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3309 // cur.par() = cur.lastpar();
3310 // cur.pos() = cur.lastpos();
3314 case LFUN_LAYOUT_TABULAR:
3315 cur.bv().showDialog("tabular", params2string(*this), this);
3318 case LFUN_INSET_DIALOG_UPDATE:
3319 cur.bv().updateDialog("tabular", params2string(*this));
3322 case LFUN_TABULAR_FEATURE:
3323 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3327 // insert file functions
3328 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3329 case LFUN_FILE_INSERT_PLAINTEXT: {
3331 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3332 FileName(to_utf8(cmd.argument())));
3335 cur.recordUndoInset(INSERT_UNDO);
3336 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3337 // content has been replaced,
3338 // so cursor might be invalid
3339 cur.pos() = cur.lastpos();
3340 cur.pit() = cur.lastpit();
3341 bvcur.setCursor(cur);
3348 if (tablemode(cur)) {
3349 if (copySelection(cur)) {
3350 cur.recordUndoInset(DELETE_UNDO);
3355 cell(cur.idx())->dispatch(cur, cmd);
3358 case LFUN_CHAR_DELETE_BACKWARD:
3359 case LFUN_CHAR_DELETE_FORWARD:
3360 if (tablemode(cur)) {
3361 cur.recordUndoInset(DELETE_UNDO);
3365 cell(cur.idx())->dispatch(cur, cmd);
3369 if (!cur.selection())
3371 if (tablemode(cur)) {
3375 cell(cur.idx())->dispatch(cur, cmd);
3378 case LFUN_CLIPBOARD_PASTE:
3379 case LFUN_PRIMARY_SELECTION_PASTE: {
3380 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3381 theClipboard().getAsText() :
3382 theSelection().get();
3385 // pass to InsertPlaintextString, but
3386 // only if we have multi-cell content
3387 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3388 cur.recordUndoInset(INSERT_UNDO);
3389 if (insertPlaintextString(cur.bv(), clip, false)) {
3390 // content has been replaced,
3391 // so cursor might be invalid
3392 cur.pos() = cur.lastpos();
3393 cur.pit() = cur.lastpit();
3394 bvcur.setCursor(cur);
3398 // Let the cell handle normal text
3399 cell(cur.idx())->dispatch(cur, cmd);
3404 if (!tabularStackDirty()) {
3405 cell(cur.idx())->dispatch(cur, cmd);
3408 if (theClipboard().isInternal() ||
3409 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3410 cur.recordUndoInset(INSERT_UNDO);
3411 pasteClipboard(cur);
3415 case LFUN_FONT_EMPH:
3416 case LFUN_FONT_BOLD:
3417 case LFUN_FONT_ROMAN:
3418 case LFUN_FONT_NOUN:
3419 case LFUN_FONT_ITAL:
3420 case LFUN_FONT_FRAK:
3421 case LFUN_FONT_TYPEWRITER:
3422 case LFUN_FONT_SANS:
3423 case LFUN_TEXTSTYLE_APPLY:
3424 case LFUN_TEXTSTYLE_UPDATE:
3425 case LFUN_FONT_SIZE:
3426 case LFUN_FONT_UNDERLINE:
3428 case LFUN_WORD_CAPITALIZE:
3429 case LFUN_WORD_UPCASE:
3430 case LFUN_WORD_LOWCASE:
3431 case LFUN_CHARS_TRANSPOSE:
3432 if (tablemode(cur)) {
3435 getSelection(cur, rs, re, cs, ce);
3436 Cursor tmpcur = cur;
3437 for (row_type i = rs; i <= re; ++i) {
3438 for (col_type j = cs; j <= ce; ++j) {
3439 // cursor follows cell:
3440 tmpcur.idx() = tabular.cellIndex(i, j);
3441 // select this cell only:
3444 tmpcur.resetAnchor();
3445 tmpcur.pit() = tmpcur.lastpit();
3446 tmpcur.pos() = tmpcur.top().lastpos();
3447 tmpcur.setCursor(tmpcur);
3448 tmpcur.setSelection();
3449 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3454 cell(cur.idx())->dispatch(cur, cmd);
3458 // we try to handle this event in the insets dispatch function.
3459 cell(cur.idx())->dispatch(cur, cmd);
3465 // function sets an object as defined in func_status.h:
3466 // states OK, Unknown, Disabled, On, Off.
3467 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3468 FuncStatus & status) const
3470 switch (cmd.action) {
3471 case LFUN_TABULAR_FEATURE: {
3472 int action = Tabular::LAST_ACTION;
3474 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3475 string const tmp = tabularFeature[i].feature;
3476 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3477 action = tabularFeature[i].action;
3481 if (action == Tabular::LAST_ACTION) {
3483 status.unknown(true);
3487 string const argument
3488 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3490 row_type sel_row_start = 0;
3491 row_type sel_row_end = 0;
3492 col_type sel_col_start = 0;
3493 col_type sel_col_end = 0;
3494 Tabular::ltType dummyltt;
3497 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3500 case Tabular::SET_PWIDTH:
3501 case Tabular::SET_MPWIDTH:
3502 case Tabular::SET_SPECIAL_COLUMN:
3503 case Tabular::SET_SPECIAL_MULTI:
3504 case Tabular::APPEND_ROW:
3505 case Tabular::APPEND_COLUMN:
3506 case Tabular::DELETE_ROW:
3507 case Tabular::DELETE_COLUMN:
3508 case Tabular::COPY_ROW:
3509 case Tabular::COPY_COLUMN:
3510 case Tabular::SET_TOP_SPACE:
3511 case Tabular::SET_BOTTOM_SPACE:
3512 case Tabular::SET_INTERLINE_SPACE:
3516 case Tabular::MULTICOLUMN:
3517 status.setEnabled(sel_row_start == sel_row_end);
3518 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3521 case Tabular::SET_ALL_LINES:
3522 case Tabular::UNSET_ALL_LINES:
3523 case Tabular::SET_BORDER_LINES:
3524 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3527 case Tabular::TOGGLE_LINE_TOP:
3528 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3529 status.setOnOff(tabular.topLine(cur.idx()));
3532 case Tabular::TOGGLE_LINE_BOTTOM:
3533 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3534 status.setOnOff(tabular.bottomLine(cur.idx()));
3537 case Tabular::TOGGLE_LINE_LEFT:
3538 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3539 status.setOnOff(tabular.leftLine(cur.idx()));
3542 case Tabular::TOGGLE_LINE_RIGHT:
3543 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3544 status.setOnOff(tabular.rightLine(cur.idx()));
3547 case Tabular::M_ALIGN_LEFT:
3549 case Tabular::ALIGN_LEFT:
3550 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3553 case Tabular::M_ALIGN_RIGHT:
3555 case Tabular::ALIGN_RIGHT:
3556 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3559 case Tabular::M_ALIGN_CENTER:
3561 case Tabular::ALIGN_CENTER:
3562 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3565 case Tabular::ALIGN_BLOCK:
3566 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3567 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3570 case Tabular::M_VALIGN_TOP:
3572 case Tabular::VALIGN_TOP:
3574 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3577 case Tabular::M_VALIGN_BOTTOM:
3579 case Tabular::VALIGN_BOTTOM:
3581 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3584 case Tabular::M_VALIGN_MIDDLE:
3586 case Tabular::VALIGN_MIDDLE:
3588 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3591 case Tabular::SET_LONGTABULAR:
3592 status.setOnOff(tabular.is_long_tabular);
3595 case Tabular::UNSET_LONGTABULAR:
3596 status.setOnOff(!tabular.is_long_tabular);
3599 case Tabular::TOGGLE_ROTATE_TABULAR:
3600 case Tabular::SET_ROTATE_TABULAR:
3601 status.setOnOff(tabular.rotate);
3604 case Tabular::UNSET_ROTATE_TABULAR:
3605 status.setOnOff(!tabular.rotate);
3608 case Tabular::TOGGLE_ROTATE_CELL:
3609 case Tabular::SET_ROTATE_CELL:
3610 status.setOnOff(!oneCellHasRotationState(false,
3611 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3614 case Tabular::UNSET_ROTATE_CELL:
3615 status.setOnOff(!oneCellHasRotationState(true,
3616 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3619 case Tabular::SET_USEBOX:
3620 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3623 case Tabular::SET_LTFIRSTHEAD:
3624 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3627 case Tabular::UNSET_LTFIRSTHEAD:
3628 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3631 case Tabular::SET_LTHEAD:
3632 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3635 case Tabular::UNSET_LTHEAD:
3636 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3639 case Tabular::SET_LTFOOT:
3640 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3643 case Tabular::UNSET_LTFOOT:
3644 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3647 case Tabular::SET_LTLASTFOOT:
3648 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3651 case Tabular::UNSET_LTLASTFOOT:
3652 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3655 case Tabular::SET_LTNEWPAGE:
3656 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3659 case Tabular::TOGGLE_LTCAPTION:
3660 status.setEnabled(sel_row_start == sel_row_end);
3661 status.setOnOff(tabular.ltCaption(sel_row_start));
3664 case Tabular::SET_BOOKTABS:
3665 status.setOnOff(tabular.use_booktabs);
3668 case Tabular::UNSET_BOOKTABS:
3669 status.setOnOff(!tabular.use_booktabs);
3674 status.setEnabled(false);
3680 // These are only enabled inside tabular
3681 case LFUN_CELL_BACKWARD:
3682 case LFUN_CELL_FORWARD:
3683 status.setEnabled(true);
3686 // disable these with multiple cells selected
3687 case LFUN_INSET_INSERT:
3688 case LFUN_TABULAR_INSERT:
3689 case LFUN_FLEX_INSERT:
3690 case LFUN_FLOAT_INSERT:
3691 case LFUN_FLOAT_WIDE_INSERT:
3692 case LFUN_FOOTNOTE_INSERT:
3693 case LFUN_MARGINALNOTE_INSERT:
3694 case LFUN_MATH_INSERT:
3695 case LFUN_MATH_MODE:
3696 case LFUN_MATH_MUTATE:
3697 case LFUN_MATH_DISPLAY:
3698 case LFUN_NOTE_INSERT:
3699 case LFUN_OPTIONAL_INSERT:
3700 case LFUN_BOX_INSERT:
3701 case LFUN_BRANCH_INSERT:
3702 case LFUN_WRAP_INSERT:
3703 case LFUN_ERT_INSERT: {
3704 if (tablemode(cur)) {
3705 status.setEnabled(false);
3708 return cell(cur.idx())->getStatus(cur, cmd, status);
3711 // disable in non-fixed-width cells
3712 case LFUN_NEWLINE_INSERT:
3713 case LFUN_BREAK_PARAGRAPH: {
3714 if (tabular.getPWidth(cur.idx()).zero()) {
3715 status.setEnabled(false);
3718 return cell(cur.idx())->getStatus(cur, cmd, status);
3722 if (tabularStackDirty() && theClipboard().isInternal()) {
3723 status.setEnabled(true);
3726 return cell(cur.idx())->getStatus(cur, cmd, status);
3728 case LFUN_INSET_MODIFY:
3729 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3730 status.setEnabled(true);
3736 // we try to handle this event in the insets dispatch function.
3737 return cell(cur.idx())->getStatus(cur, cmd, status);
3742 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3744 return tabular.latex(os, runparams);
3748 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3750 os << '\n'; // output table on a new line
3751 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3752 tabular.plaintext(os, runparams, dp, false, 0);
3753 return PLAINTEXT_NEWLINE;
3757 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3762 // FIXME: Why not pass a proper DocIterator here?
3764 // if the table is inside a float it doesn't need the informaltable
3765 // wrapper. Search for it.
3766 for (master = owner(); master; master = master->owner())
3767 if (master->lyxCode() == FLOAT_CODE)
3772 os << "<informaltable>";
3775 ret += tabular.docbook(os, runparams);
3777 os << "</informaltable>";
3784 void InsetTabular::validate(LaTeXFeatures & features) const
3786 tabular.validate(features);
3790 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3792 return tabular.cellInset(idx);
3796 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3798 return tabular.cellInset(idx);
3802 void InsetTabular::cursorPos(BufferView const & bv,
3803 CursorSlice const & sl, bool boundary, int & x, int & y) const
3805 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3807 // y offset correction
3808 int const row = tabular.cellRow(sl.idx());
3809 for (int i = 0; i <= row; ++i) {
3811 y += tabular.rowAscent(i);
3812 y += tabular.getAdditionalHeight(i);
3815 y += tabular.rowDescent(i);
3818 // x offset correction
3819 int const col = tabular.cellColumn(sl.idx());
3820 int idx = tabular.cellIndex(row, 0);
3821 for (int j = 0; j < col; ++j) {
3822 if (tabular.isPartOfMultiColumn(row, j))
3824 x += tabular.columnWidth(idx);
3827 x += tabular.getBeginningOfTextInCell(idx);
3828 x += ADD_TO_TABULAR_WIDTH;
3833 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3837 Inset const & inset = *tabular.cellInset(cell);
3838 Point o = bv.coordCache().getInsets().xy(&inset);
3839 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3840 int const xend = xbeg + tabular.columnWidth(cell);
3841 row_type const row = tabular.cellRow(cell);
3842 int const ybeg = o.y_ - tabular.rowAscent(row) -
3843 tabular.getAdditionalHeight(row);
3844 int const yend = o.y_ + tabular.rowDescent(row);
3856 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3857 // << " ybeg=" << ybeg << " yend=" << yend
3858 // << " xx=" << xx << " yy=" << yy
3859 // << " dist=" << xx + yy << endl;
3864 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3866 //lyxerr << "InsetTabular::editXY: " << this << endl;
3867 cur.selection() = false;
3869 cur.idx() = getNearestCell(cur.bv(), x, y);
3871 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3875 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3877 cur.idx() = getNearestCell(cur.bv(), x, y);
3878 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3882 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3884 idx_type idx_min = 0;
3885 int dist_min = numeric_limits<int>::max();
3886 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3887 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3888 int const d = dist(bv, i, x, y);
3899 int InsetTabular::cellXPos(idx_type const cell) const
3903 for (; !tabular.isFirstCellInRow(c); --c)
3906 for (; c < cell; ++c)
3907 lx += tabular.columnWidth(c);
3913 void InsetTabular::resetPos(Cursor & cur) const
3915 BufferView & bv = cur.bv();
3916 int const maxwidth = bv.workWidth();
3918 if (&cur.inset() != this) {
3922 int const X2 = maxwidth;
3923 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3924 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3925 int const x2 = x1 + tabular.columnWidth(cur.idx());
3928 scx_ = X1 + 20 - x1;
3930 scx_ = X2 - 20 - x2;
3935 cur.updateFlags(Update::Force | Update::FitCursor);
3939 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
3941 if (isRightToLeft(cur)) {
3942 if (tabular.isFirstCellInRow(cur.idx())) {
3943 row_type const row = tabular.cellRow(cur.idx());
3944 if (row == tabular.row_info.size() - 1)
3946 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3953 if (tabular.isLastCell(cur.idx()))
3959 cur.boundary(false);
3961 // in visual mode, place cursor at extreme left or right
3963 switch(entry_from) {
3965 case ENTRY_DIRECTION_RIGHT:
3966 cur.posVisToRowExtremity(false /* !left */);
3968 case ENTRY_DIRECTION_LEFT:
3969 cur.posVisToRowExtremity(true /* left */);
3971 case ENTRY_DIRECTION_IGNORE:
3972 // nothing to do in this case
3981 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
3983 if (isRightToLeft(cur)) {
3984 if (tabular.isLastCellInRow(cur.idx())) {
3985 row_type const row = tabular.cellRow(cur.idx());
3988 cur.idx() = tabular.getFirstCellInRow(row);
3989 cur.idx() = tabular.cellAbove(cur.idx());
3991 if (tabular.isLastCell(cur.idx()))
3996 if (cur.idx() == 0) // first cell
4000 cur.pit() = cur.lastpit();
4001 cur.pos() = cur.lastpos();
4003 // in visual mode, place cursor at extreme left or right
4005 switch(entry_from) {
4007 case ENTRY_DIRECTION_RIGHT:
4008 cur.posVisToRowExtremity(false /* !left */);
4010 case ENTRY_DIRECTION_LEFT:
4011 cur.posVisToRowExtremity(true /* left */);
4013 case ENTRY_DIRECTION_IGNORE:
4014 // nothing to do in this case
4019 // FIXME: this accesses the position cache before it is initialized
4024 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4026 Tabular::Feature action = Tabular::LAST_ACTION;
4029 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4030 string const tmp = tabularFeature[i].feature;
4032 if (tmp == what.substr(0, tmp.length())) {
4033 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4034 //tabularFeatures[i].feature.length()))
4035 action = tabularFeature[i].action;
4039 if (action == Tabular::LAST_ACTION)
4043 ltrim(what.substr(tabularFeature[i].feature.length()));
4044 tabularFeatures(cur, action, val);
4049 static void checkLongtableSpecial(Tabular::ltType & ltt,
4050 string const & special, bool & flag)
4052 if (special == "dl_above") {
4055 } else if (special == "dl_below") {
4056 ltt.bottomDL = flag;
4058 } else if (special == "empty") {
4067 bool InsetTabular::oneCellHasRotationState(bool rotated,
4068 row_type row_start, row_type row_end,
4069 col_type col_start, col_type col_end) const {
4071 for (row_type i = row_start; i <= row_end; ++i) {
4072 for (col_type j = col_start; j <= col_end; ++j) {
4073 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4082 void InsetTabular::tabularFeatures(Cursor & cur,
4083 Tabular::Feature feature, string const & value)
4085 col_type sel_col_start;
4086 col_type sel_col_end;
4087 row_type sel_row_start;
4088 row_type sel_row_end;
4089 bool setLines = false;
4090 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4091 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4095 case Tabular::M_ALIGN_LEFT:
4096 case Tabular::ALIGN_LEFT:
4097 setAlign = LYX_ALIGN_LEFT;
4100 case Tabular::M_ALIGN_RIGHT:
4101 case Tabular::ALIGN_RIGHT:
4102 setAlign = LYX_ALIGN_RIGHT;
4105 case Tabular::M_ALIGN_CENTER:
4106 case Tabular::ALIGN_CENTER:
4107 setAlign = LYX_ALIGN_CENTER;
4110 case Tabular::ALIGN_BLOCK:
4111 setAlign = LYX_ALIGN_BLOCK;
4114 case Tabular::M_VALIGN_TOP:
4115 case Tabular::VALIGN_TOP:
4116 setVAlign = Tabular::LYX_VALIGN_TOP;
4119 case Tabular::M_VALIGN_BOTTOM:
4120 case Tabular::VALIGN_BOTTOM:
4121 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4124 case Tabular::M_VALIGN_MIDDLE:
4125 case Tabular::VALIGN_MIDDLE:
4126 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4133 cur.recordUndoInset(ATOMIC_UNDO);
4135 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4136 row_type const row = tabular.cellRow(cur.idx());
4137 col_type const column = tabular.cellColumn(cur.idx());
4139 Tabular::ltType ltt;
4143 case Tabular::SET_PWIDTH: {
4144 Length const len(value);
4145 tabular.setColumnPWidth(cur, cur.idx(), len);
4147 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4148 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4152 case Tabular::SET_MPWIDTH:
4153 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4156 case Tabular::SET_SPECIAL_COLUMN:
4157 case Tabular::SET_SPECIAL_MULTI:
4158 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4161 case Tabular::APPEND_ROW:
4162 // append the row into the tabular
4163 tabular.appendRow(cur.idx());
4166 case Tabular::APPEND_COLUMN:
4167 // append the column into the tabular
4168 tabular.appendColumn(cur.idx());
4169 cur.idx() = tabular.cellIndex(row, column);
4172 case Tabular::DELETE_ROW:
4173 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4174 tabular.deleteRow(sel_row_start);
4175 if (sel_row_start >= tabular.row_info.size())
4177 cur.idx() = tabular.cellIndex(sel_row_start, column);
4180 cur.selection() = false;
4183 case Tabular::DELETE_COLUMN:
4184 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4185 tabular.deleteColumn(sel_col_start);
4186 if (sel_col_start >= tabular.column_info.size())
4188 cur.idx() = tabular.cellIndex(row, sel_col_start);
4191 cur.selection() = false;
4194 case Tabular::COPY_ROW:
4195 tabular.copyRow(row);
4198 case Tabular::COPY_COLUMN:
4199 tabular.copyColumn(column);
4200 cur.idx() = tabular.cellIndex(row, column);
4203 case Tabular::TOGGLE_LINE_TOP: {
4204 bool lineSet = !tabular.topLine(cur.idx());
4205 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4206 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4207 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4211 case Tabular::TOGGLE_LINE_BOTTOM: {
4212 bool lineSet = !tabular.bottomLine(cur.idx());
4213 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4214 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4215 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4219 case Tabular::TOGGLE_LINE_LEFT: {
4220 bool lineSet = !tabular.leftLine(cur.idx());
4221 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4222 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4223 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4227 case Tabular::TOGGLE_LINE_RIGHT: {
4228 bool lineSet = !tabular.rightLine(cur.idx());
4229 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4230 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4231 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4235 case Tabular::M_ALIGN_LEFT:
4236 case Tabular::M_ALIGN_RIGHT:
4237 case Tabular::M_ALIGN_CENTER:
4239 case Tabular::ALIGN_LEFT:
4240 case Tabular::ALIGN_RIGHT:
4241 case Tabular::ALIGN_CENTER:
4242 case Tabular::ALIGN_BLOCK:
4243 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4244 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4245 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4248 case Tabular::M_VALIGN_TOP:
4249 case Tabular::M_VALIGN_BOTTOM:
4250 case Tabular::M_VALIGN_MIDDLE:
4252 case Tabular::VALIGN_TOP:
4253 case Tabular::VALIGN_BOTTOM:
4254 case Tabular::VALIGN_MIDDLE:
4255 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4256 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4257 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4260 case Tabular::MULTICOLUMN: {
4261 if (!cur.selection()) {
4262 // just multicol for one single cell
4263 // check whether we are completely in a multicol
4264 if (tabular.isMultiColumn(cur.idx()))
4265 tabular.unsetMultiColumn(cur.idx());
4267 tabular.setMultiColumn(cur.idx(), 1);
4270 // we have a selection so this means we just add all this
4271 // cells to form a multicolumn cell
4272 idx_type const s_start = cur.selBegin().idx();
4273 idx_type const s_end = cur.selEnd().idx();
4274 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4275 cur.idx() = s_start;
4278 cur.selection() = false;
4282 case Tabular::SET_ALL_LINES:
4284 case Tabular::UNSET_ALL_LINES:
4285 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4286 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4287 tabular.setAllLines(
4288 tabular.cellIndex(i,j), setLines);
4291 case Tabular::SET_BORDER_LINES:
4292 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4293 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4294 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4296 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4297 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4298 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4302 case Tabular::SET_LONGTABULAR:
4303 tabular.is_long_tabular = true;
4306 case Tabular::UNSET_LONGTABULAR:
4307 tabular.is_long_tabular = false;
4310 case Tabular::SET_ROTATE_TABULAR:
4311 tabular.rotate = true;
4314 case Tabular::UNSET_ROTATE_TABULAR:
4315 tabular.rotate = false;
4318 case Tabular::TOGGLE_ROTATE_TABULAR:
4319 tabular.rotate = !tabular.rotate;
4322 case Tabular::SET_ROTATE_CELL:
4323 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4324 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4325 tabular.setRotateCell(
4326 tabular.cellIndex(i, j), true);
4329 case Tabular::UNSET_ROTATE_CELL:
4330 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4331 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4332 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4335 case Tabular::TOGGLE_ROTATE_CELL:
4337 bool oneNotRotated = oneCellHasRotationState(false,
4338 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4340 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4341 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4342 tabular.setRotateCell(tabular.cellIndex(i, j),
4347 case Tabular::SET_USEBOX: {
4348 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4349 if (val == tabular.getUsebox(cur.idx()))
4350 val = Tabular::BOX_NONE;
4351 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4352 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4353 tabular.setUsebox(tabular.cellIndex(i, j), val);
4357 case Tabular::UNSET_LTFIRSTHEAD:
4359 case Tabular::SET_LTFIRSTHEAD:
4360 tabular.getRowOfLTFirstHead(row, ltt);
4361 checkLongtableSpecial(ltt, value, flag);
4362 tabular.setLTHead(row, flag, ltt, true);
4365 case Tabular::UNSET_LTHEAD:
4367 case Tabular::SET_LTHEAD:
4368 tabular.getRowOfLTHead(row, ltt);
4369 checkLongtableSpecial(ltt, value, flag);
4370 tabular.setLTHead(row, flag, ltt, false);
4373 case Tabular::UNSET_LTFOOT:
4375 case Tabular::SET_LTFOOT:
4376 tabular.getRowOfLTFoot(row, ltt);
4377 checkLongtableSpecial(ltt, value, flag);
4378 tabular.setLTFoot(row, flag, ltt, false);
4381 case Tabular::UNSET_LTLASTFOOT:
4383 case Tabular::SET_LTLASTFOOT:
4384 tabular.getRowOfLTLastFoot(row, ltt);
4385 checkLongtableSpecial(ltt, value, flag);
4386 tabular.setLTFoot(row, flag, ltt, true);
4389 case Tabular::SET_LTNEWPAGE:
4390 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4393 case Tabular::TOGGLE_LTCAPTION:
4394 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4397 cur.selection() = false;
4400 case Tabular::SET_BOOKTABS:
4401 tabular.use_booktabs = true;
4404 case Tabular::UNSET_BOOKTABS:
4405 tabular.use_booktabs = false;
4408 case Tabular::SET_TOP_SPACE: {
4410 if (value == "default")
4411 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4412 tabular.row_info[i].top_space_default = true;
4413 else if (isValidLength(value, &len))
4414 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4415 tabular.row_info[i].top_space_default = false;
4416 tabular.row_info[i].top_space = len;
4419 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4420 tabular.row_info[i].top_space_default = false;
4421 tabular.row_info[i].top_space = len;
4426 case Tabular::SET_BOTTOM_SPACE: {
4428 if (value == "default")
4429 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4430 tabular.row_info[i].bottom_space_default = true;
4431 else if (isValidLength(value, &len))
4432 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4433 tabular.row_info[i].bottom_space_default = false;
4434 tabular.row_info[i].bottom_space = len;
4437 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4438 tabular.row_info[i].bottom_space_default = false;
4439 tabular.row_info[i].bottom_space = len;
4444 case Tabular::SET_INTERLINE_SPACE: {
4446 if (value == "default")
4447 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4448 tabular.row_info[i].interline_space_default = true;
4449 else if (isValidLength(value, &len))
4450 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4451 tabular.row_info[i].interline_space_default = false;
4452 tabular.row_info[i].interline_space = len;
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4456 tabular.row_info[i].interline_space_default = false;
4457 tabular.row_info[i].interline_space = len;
4462 // dummy stuff just to avoid warnings
4463 case Tabular::LAST_ACTION:
4469 bool InsetTabular::showInsetDialog(BufferView * bv) const
4471 bv->showDialog("tabular", params2string(*this),
4472 const_cast<InsetTabular *>(this));
4477 void InsetTabular::openLayoutDialog(BufferView * bv) const
4479 bv->showDialog("tabular", params2string(*this),
4480 const_cast<InsetTabular *>(this));
4484 bool InsetTabular::copySelection(Cursor & cur)
4486 if (!cur.selection())
4491 getSelection(cur, rs, re, cs, ce);
4493 paste_tabular.reset(new Tabular(tabular));
4495 for (row_type i = 0; i < rs; ++i)
4496 paste_tabular->deleteRow(0);
4498 row_type const rows = re - rs + 1;
4499 while (paste_tabular->row_info.size() > rows)
4500 paste_tabular->deleteRow(rows);
4502 for (col_type i = 0; i < cs; ++i)
4503 paste_tabular->deleteColumn(0);
4505 col_type const columns = ce - cs + 1;
4506 while (paste_tabular->column_info.size() > columns)
4507 paste_tabular->deleteColumn(columns);
4509 odocstringstream os;
4510 OutputParams const runparams(0);
4511 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4512 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4513 cap::copySelection(cur, os.str());
4515 // mark tabular stack dirty
4516 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4517 // when we (hopefully) have a one-for-all paste mechanism.
4518 // This must be called after cap::copySelection.
4519 dirtyTabularStack(true);
4525 bool InsetTabular::pasteClipboard(Cursor & cur)
4529 col_type const actcol = tabular.cellColumn(cur.idx());
4530 row_type const actrow = tabular.cellRow(cur.idx());
4531 for (row_type r1 = 0, r2 = actrow;
4532 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4534 for (col_type c1 = 0, c2 = actcol;
4535 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4537 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4538 tabular.isPartOfMultiColumn(r2, c2))
4540 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4544 if (tabular.isPartOfMultiColumn(r2, c2)) {
4548 shared_ptr<InsetTableCell> inset(
4549 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4550 tabular.setCellInset(r2, c2, inset);
4551 // FIXME: change tracking (MG)
4552 inset->setChange(Change(cur.buffer().params().trackChanges ?
4553 Change::INSERTED : Change::UNCHANGED));
4561 void InsetTabular::cutSelection(Cursor & cur)
4563 if (!cur.selection())
4568 getSelection(cur, rs, re, cs, ce);
4569 for (row_type i = rs; i <= re; ++i) {
4570 for (col_type j = cs; j <= ce; ++j) {
4571 shared_ptr<InsetTableCell> t
4572 = cell(tabular.cellIndex(i, j));
4573 if (cur.buffer().params().trackChanges)
4574 // FIXME: Change tracking (MG)
4575 t->setChange(Change(Change::DELETED));
4581 // cursor position might be invalid now
4582 if (cur.pit() > cur.lastpit())
4583 cur.pit() = cur.lastpit();
4584 if (cur.pos() > cur.lastpos())
4585 cur.pos() = cur.lastpos();
4586 cur.clearSelection();
4590 bool InsetTabular::isRightToLeft(Cursor & cur) const
4592 LASSERT(cur.depth() > 1, /**/);
4593 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4594 pos_type const parentpos = cur[cur.depth() - 2].pos();
4595 return parentpar.getFontSettings(cur.bv().buffer().params(),
4596 parentpos).language()->rightToLeft();
4600 void InsetTabular::getSelection(Cursor & cur,
4601 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4603 CursorSlice const & beg = cur.selBegin();
4604 CursorSlice const & end = cur.selEnd();
4605 cs = tabular.cellColumn(beg.idx());
4606 ce = tabular.cellColumn(end.idx());
4609 cs = tabular.cellColumn(end.idx());
4611 ce = tabular.cellRightColumn(end.idx());
4614 rs = tabular.cellRow(beg.idx());
4615 re = tabular.cellRow(end.idx());
4621 Text * InsetTabular::getText(int idx) const
4623 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4627 void InsetTabular::setChange(Change const & change)
4629 for (idx_type idx = 0; idx < nargs(); ++idx)
4630 cell(idx)->setChange(change);
4634 void InsetTabular::acceptChanges(BufferParams const & bparams)
4636 for (idx_type idx = 0; idx < nargs(); ++idx)
4637 cell(idx)->acceptChanges(bparams);
4641 void InsetTabular::rejectChanges(BufferParams const & bparams)
4643 for (idx_type idx = 0; idx < nargs(); ++idx)
4644 cell(idx)->rejectChanges(bparams);
4648 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4650 return tabular.getPWidth(cell).zero();
4654 bool InsetTabular::forcePlainLayout(idx_type cell) const
4656 return !tabular.getPWidth(cell).zero();
4660 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4663 if (buf.length() <= 0)
4668 col_type maxCols = 1;
4669 size_t const len = buf.length();
4673 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4681 maxCols = max(cols, maxCols);
4687 maxCols = max(cols, maxCols);
4693 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4694 loctab = paste_tabular.get();
4696 dirtyTabularStack(true);
4699 cell = bv.cursor().idx();
4700 ocol = tabular.cellColumn(cell);
4701 row = tabular.cellRow(cell);
4705 idx_type const cells = loctab->numberofcells;
4708 rows = loctab->row_info.size();
4709 col_type const columns = loctab->column_info.size();
4711 while (cell < cells && p < len && row < rows &&
4712 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4718 // we can only set this if we are not too far right
4719 if (cols < columns) {
4720 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4721 Font const font = bv.textMetrics(&inset->text_).
4723 inset->setText(buf.substr(op, p - op), font,
4724 buffer().params().trackChanges);
4730 // we can only set this if we are not too far right
4731 if (cols < columns) {
4732 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4733 Font const font = bv.textMetrics(&inset->text_).
4735 inset->setText(buf.substr(op, p - op), font,
4736 buffer().params().trackChanges);
4741 cell = loctab->cellIndex(row, cols);
4747 // check for the last cell if there is no trailing '\n'
4748 if (cell < cells && op < len) {
4749 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4750 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4751 inset->setText(buf.substr(op, len - op), font,
4752 buffer().params().trackChanges);
4758 void InsetTabular::addPreview(PreviewLoader & loader) const
4760 row_type const rows = tabular.row_info.size();
4761 col_type const columns = tabular.column_info.size();
4762 for (row_type i = 0; i < rows; ++i) {
4763 for (col_type j = 0; j < columns; ++j)
4764 tabular.cellInset(i, j)->addPreview(loader);
4769 bool InsetTabular::tablemode(Cursor & cur) const
4771 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4775 bool InsetTabular::completionSupported(Cursor const & cur) const
4777 Cursor const & bvCur = cur.bv().cursor();
4778 if (&bvCur.inset() != this)
4780 return cur.text()->completionSupported(cur);
4784 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4786 return completionSupported(cur);
4790 bool InsetTabular::automaticInlineCompletion() const
4792 return lyxrc.completion_inline_text;
4796 bool InsetTabular::automaticPopupCompletion() const
4798 return lyxrc.completion_popup_text;
4802 bool InsetTabular::showCompletionCursor() const
4804 return lyxrc.completion_cursor_text;
4808 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4810 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4814 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4816 if (!completionSupported(cur))
4818 return cur.text()->completionPrefix(cur);
4822 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4824 if (!completionSupported(cur))
4827 return cur.text()->insertCompletion(cur, s, finished);
4831 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4832 Dimension & dim) const
4834 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4835 tm.completionPosAndDim(cur, x, y, dim);
4839 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4841 istringstream data(in);
4843 lex.setStream(data);
4850 if (!lex || token != "tabular") {
4851 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4855 // This is part of the inset proper that is usually swallowed
4856 // by Buffer::readInset
4858 if (!lex || token != "Tabular") {
4859 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4867 string InsetTabular::params2string(InsetTabular const & inset)
4870 data << "tabular" << ' ';
4872 data << "\\end_inset\n";