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 & 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().plainLayout());
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()))
512 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
518 void Tabular::CellData::swap(CellData & rhs)
520 std::swap(cellno, rhs.cellno);
521 std::swap(width, rhs.width);
522 std::swap(multicolumn, rhs.multicolumn);
523 std::swap(alignment, rhs.alignment);
524 std::swap(valignment, rhs.valignment);
525 std::swap(top_line, rhs.top_line);
526 std::swap(bottom_line, rhs.bottom_line);
527 std::swap(left_line, rhs.left_line);
528 std::swap(right_line, rhs.right_line);
529 std::swap(usebox, rhs.usebox);
530 std::swap(rotate, rhs.rotate);
531 std::swap(align_special, rhs.align_special);
532 p_width.swap(rhs.p_width);
533 inset.swap(rhs.inset);
537 Tabular::RowData::RowData()
540 top_space_default(false),
541 bottom_space_default(false),
542 interline_space_default(false),
552 Tabular::ColumnData::ColumnData()
553 : alignment(LYX_ALIGN_CENTER),
554 valignment(LYX_VALIGN_TOP),
560 Tabular::ltType::ltType()
567 Tabular::Tabular(Buffer & buffer, row_type rows_arg, col_type columns_arg)
569 init(buffer, rows_arg, columns_arg);
573 void Tabular::setBuffer(Buffer & buffer)
576 size_t row_count = row_info.size();
577 size_t column_count = column_info.size();
578 // set silly default lines
579 for (row_type i = 0; i < row_count; ++i)
580 for (col_type j = 0; j < column_count; ++j)
581 cell_info[i][j].inset->setBuffer(*buffer_);
585 // activates all lines and sets all widths to 0
586 void Tabular::init(Buffer & buf, row_type rows_arg,
587 col_type columns_arg)
590 row_info = row_vector(rows_arg);
591 column_info = column_vector(columns_arg);
592 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
593 row_info.reserve(10);
594 column_info.reserve(10);
595 cell_info.reserve(100);
597 is_long_tabular = false;
599 use_booktabs = false;
600 size_t row_count = row_info.size();
601 size_t column_count = column_info.size();
602 // set silly default lines
603 for (row_type i = 0; i < row_count; ++i)
604 for (col_type j = 0; j < column_count; ++j) {
605 cell_info[i][j].inset->setBuffer(*buffer_);
606 cell_info[i][j].top_line = true;
607 cell_info[i][j].left_line = true;
608 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
609 cell_info[i][j].right_line = j == column_count - 1;
614 void Tabular::appendRow(idx_type const cell)
616 BufferParams const & bp = buffer().params();
617 row_type const row = cellRow(cell);
619 row_vector::iterator rit = row_info.begin() + row;
620 row_info.insert(rit, RowData());
621 // now set the values of the row before
622 row_info[row] = row_info[row + 1];
624 row_type const nrows = row_info.size();
625 col_type const ncols = column_info.size();
627 cell_vvector old(nrows - 1);
628 for (row_type i = 0; i < nrows - 1; ++i)
629 swap(cell_info[i], old[i]);
631 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer())));
633 for (row_type i = 0; i <= row; ++i)
634 swap(cell_info[i], old[i]);
635 for (row_type i = row + 2; i < nrows; ++i)
636 swap(cell_info[i], old[i - 1]);
639 for (col_type c = 0; c < ncols; ++c) {
640 // inherit line settings
641 idx_type const i = cellIndex(row + 1, c);
642 idx_type const j = cellIndex(row, c);
643 setLeftLine(i, leftLine(j));
644 setRightLine(i, rightLine(j));
645 setTopLine(i, topLine(j));
646 if (topLine(j) && bottomLine(j)) {
647 setBottomLine(i, true);
648 setBottomLine(j, false);
650 // mark track changes
652 cellInfo(i).inset->setChange(Change(Change::INSERTED));
657 void Tabular::deleteRow(row_type const row)
659 // Not allowed to delete last row
660 if (row_info.size() == 1)
663 row_info.erase(row_info.begin() + row);
664 cell_info.erase(cell_info.begin() + row);
669 void Tabular::copyRow(row_type const row)
671 row_info.insert(row_info.begin() + row, row_info[row]);
672 cell_info.insert(cell_info.begin() + row, cell_info[row]);
674 if (buffer().params().trackChanges)
675 for (col_type j = 0; j < column_info.size(); ++j)
676 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
682 void Tabular::appendColumn(idx_type const cell)
684 col_type const c = cellColumn(cell);
685 column_vector::iterator cit = column_info.begin() + c + 1;
686 column_info.insert(cit, ColumnData());
687 row_type const nrows = row_info.size();
688 // set the column values of the column before
689 column_info[c + 1] = column_info[c];
691 for (row_type r = 0; r < nrows; ++r) {
692 cell_info[r].insert(cell_info[r].begin() + c + 1,
695 // FIXME: This code does not work. It deletes the cell's content and
696 // it triggers an assertion if the cursor is at pos > 0.
697 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
698 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
700 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
704 for (row_type r = 0; r < nrows; ++r) {
705 // inherit line settings
706 idx_type const i = cellIndex(r, c + 1);
707 idx_type const j = cellIndex(r, c);
708 setBottomLine(i, bottomLine(j));
709 setTopLine(i, topLine(j));
710 setLeftLine(i, leftLine(j));
711 if (rightLine(j) && rightLine(j)) {
712 setRightLine(i, true);
713 setRightLine(j, false);
716 cellInfo(i).inset->clear();
717 if (buffer().params().trackChanges)
718 cellInfo(i).inset->setChange(Change(Change::INSERTED));
723 void Tabular::deleteColumn(col_type const column)
725 // Not allowed to delete last column
726 if (column_info.size() == 1)
729 column_info.erase(column_info.begin() + column);
730 size_t row_count = row_info.size();
731 for (row_type i = 0; i < row_count; ++i) {
732 // Care about multicolumn cells
733 if (column + 1 < column_info.size() &&
734 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
735 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
736 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
738 cell_info[i].erase(cell_info[i].begin() + column);
744 void Tabular::copyColumn(col_type const column)
746 BufferParams const & bp = buffer().params();
747 column_info.insert(column_info.begin() + column, column_info[column]);
749 size_t row_count = row_info.size();
750 for (row_type i = 0; i < row_count; ++i)
751 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
754 for (row_type i = 0; i < row_count; ++i)
755 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
760 void Tabular::updateIndexes()
762 col_type ncols = column_info.size();
763 row_type nrows = row_info.size();
765 for (row_type row = 0; row < nrows; ++row)
766 for (col_type column = 0; column < ncols; ++column) {
767 if (!isPartOfMultiColumn(row, column))
769 cell_info[row][column].cellno = numberofcells - 1;
772 rowofcell.resize(numberofcells);
773 columnofcell.resize(numberofcells);
775 for (row_type row = 0; row < nrows; ++row)
776 for (col_type column = 0; column < ncols; ++column) {
777 if (isPartOfMultiColumn(row, column))
780 columnofcell[i] = column;
781 setFixedWidth(row, column);
782 updateContentAlignment(row, column);
788 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
790 row_type const row = cellRow(cell);
792 for (col_type i = 0; i < column_info.size(); ++i)
793 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
799 bool Tabular::topLine(idx_type const cell) const
801 return cellInfo(cell).top_line;
805 bool Tabular::bottomLine(idx_type const cell) const
807 return cellInfo(cell).bottom_line;
811 bool Tabular::leftLine(idx_type cell) const
815 return cellInfo(cell).left_line;
819 bool Tabular::rightLine(idx_type cell) const
823 return cellInfo(cell).right_line;
827 bool Tabular::topAlreadyDrawn(idx_type cell) const
829 row_type row = cellRow(cell);
832 idx_type i = cellIndex(row - 1, cellColumn(cell));
833 return !rowTopLine(row) && bottomLine(i);
837 bool Tabular::leftAlreadyDrawn(idx_type cell) const
839 col_type col = cellColumn(cell);
842 idx_type i = cellIndex(cellRow(cell), col - 1);
843 return rightLine(i) && !leftLine(cell);
847 bool Tabular::isLastRow(idx_type cell) const
849 return cellRow(cell) == row_info.size() - 1;
853 int Tabular::getAdditionalHeight(row_type row) const
855 if (!row || row >= row_info.size())
858 int const interline_space = row_info[row - 1].interline_space_default ?
860 row_info[row - 1].interline_space.inPixels(width());
861 if (rowTopLine(row) && rowBottomLine(row - 1))
862 return interline_space + WIDTH_OF_LINE;
863 return interline_space;
867 int Tabular::getAdditionalWidth(idx_type cell) const
869 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
871 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
872 return WIDTH_OF_LINE;
877 int Tabular::columnWidth(idx_type cell) const
880 col_type const span = columnSpan(cell);
881 col_type const col = cellColumn(cell);
882 for(col_type c = col; c < col + span ; ++c)
883 w += column_info[c].width;
888 bool Tabular::updateColumnWidths()
890 col_type const ncols = column_info.size();
891 row_type const nrows = row_info.size();
893 // for each col get max of single col cells
894 for(col_type c = 0; c < ncols; ++c) {
896 for(row_type r = 0; r < nrows; ++r) {
897 idx_type const i = cellIndex(r, c);
898 if (columnSpan(i) == 1)
899 new_width = max(new_width, cellInfo(i).width);
902 if (column_info[c].width != new_width) {
903 column_info[c].width = new_width;
907 // update col widths to fit merged cells
908 for(col_type c = 0; c < ncols; ++c)
909 for(row_type r = 0; r < nrows; ++r) {
910 idx_type const i = cellIndex(r, c);
911 int const span = columnSpan(i);
912 if (span == 1 || c > cellColumn(i))
916 for(col_type j = c; j < c + span ; ++j)
917 old_width += column_info[j].width;
919 if (cellInfo(i).width > old_width) {
920 column_info[c + span - 1].width += cellInfo(i).width - old_width;
929 int Tabular::width() const
931 col_type const ncols = column_info.size();
933 for (col_type i = 0; i < ncols; ++i)
934 width += column_info[i].width;
939 void Tabular::setCellWidth(idx_type cell, int new_width)
941 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
942 + getAdditionalWidth(cell);
946 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
949 if (!isMultiColumn(cell) || onlycolumn)
950 column_info[cellColumn(cell)].alignment = align;
952 cellInfo(cell).alignment = align;
953 cellInset(cell).get()->setContentAlignment(align);
957 void Tabular::setVAlignment(idx_type cell, VAlignment align,
960 if (!isMultiColumn(cell) || onlycolumn)
961 column_info[cellColumn(cell)].valignment = align;
963 cellInfo(cell).valignment = align;
970 * Allow line and paragraph breaks for fixed width cells or disallow them,
971 * merge cell paragraphs and reset layout to standard for variable width
974 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
976 inset->setAutoBreakRows(fixedWidth);
977 inset->toggleFixedWidth(fixedWidth);
981 // merge all paragraphs to one
982 BufferParams const & bp = cur.bv().buffer().params();
983 while (inset->paragraphs().size() > 1)
984 mergeParagraph(bp, inset->paragraphs(), 0);
988 // undo information has already been recorded
989 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
990 bp.documentClass().plainLayoutName());
997 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
998 Length const & width)
1000 col_type const j = cellColumn(cell);
1002 column_info[j].p_width = width;
1003 for (row_type i = 0; i < row_info.size(); ++i) {
1004 idx_type const cell = cellIndex(i, j);
1005 // because of multicolumns
1006 toggleFixedWidth(cur, cellInset(cell).get(),
1007 !getPWidth(cell).zero());
1009 // cur paragraph can become invalid after paragraphs were merged
1010 if (cur.pit() > cur.lastpit())
1011 cur.pit() = cur.lastpit();
1012 // cur position can become invalid after newlines were removed
1013 if (cur.pos() > cur.lastpos())
1014 cur.pos() = cur.lastpos();
1018 bool Tabular::setFixedWidth(row_type r, col_type c)
1020 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1021 if ((!column_info[c].p_width.zero() && !multicol)
1022 || (multicol && !cell_info[r][c].p_width.zero())) {
1023 cell_info[r][c].inset->toggleFixedWidth(true);
1030 void Tabular::updateContentAlignment(row_type r, col_type c)
1032 cell_info[r][c].inset->setContentAlignment(
1033 getAlignment(cellIndex(r, c)));
1037 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1038 Length const & width)
1040 if (!isMultiColumn(cell))
1043 cellInfo(cell).p_width = width;
1044 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1045 // cur paragraph can become invalid after paragraphs were merged
1046 if (cur.pit() > cur.lastpit())
1047 cur.pit() = cur.lastpit();
1048 // cur position can become invalid after newlines were removed
1049 if (cur.pos() > cur.lastpos())
1050 cur.pos() = cur.lastpos();
1055 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1056 Tabular::Feature what)
1058 if (what == SET_SPECIAL_MULTI)
1059 cellInfo(cell).align_special = special;
1061 column_info[cellColumn(cell)].align_special = special;
1065 void Tabular::setAllLines(idx_type cell, bool line)
1067 setTopLine(cell, line);
1068 setBottomLine(cell, line);
1069 setRightLine(cell, line);
1070 setLeftLine(cell, line);
1074 void Tabular::setTopLine(idx_type i, bool line)
1076 cellInfo(i).top_line = line;
1080 void Tabular::setBottomLine(idx_type i, bool line)
1082 cellInfo(i).bottom_line = line;
1086 void Tabular::setLeftLine(idx_type cell, bool line)
1088 cellInfo(cell).left_line = line;
1092 void Tabular::setRightLine(idx_type cell, bool line)
1094 cellInfo(cell).right_line = line;
1097 bool Tabular::rowTopLine(row_type r) const
1099 idx_type i0 = getFirstCellInRow(r);
1100 idx_type i1 = getLastCellInRow(r);
1101 bool all_rows_set = true;
1102 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1103 all_rows_set = cellInfo(j).top_line;
1104 return all_rows_set;
1108 bool Tabular::rowBottomLine(row_type r) const
1110 idx_type i0 = getFirstCellInRow(r);
1111 idx_type i1 = getLastCellInRow(r);
1112 bool all_rows_set = true;
1113 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1114 all_rows_set = cellInfo(j).bottom_line;
1115 return all_rows_set;
1119 bool Tabular::columnLeftLine(col_type c) const
1126 row_type const nrows = row_info.size();
1127 for (row_type r = 0; r < nrows; ++r) {
1128 idx_type i = cellIndex(r, c);
1129 if (c == cellColumn(i)) {
1131 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1132 if (cellInfo(i).left_line || right)
1136 return 2 * nrows_left >= total;
1140 bool Tabular::columnRightLine(col_type c) const
1145 int nrows_right = 0;
1147 row_type const nrows = row_info.size();
1148 for (row_type r = 0; r < nrows; ++r) {
1149 idx_type i = cellIndex(r, c);
1150 if (c == cellColumn(i) + columnSpan(i) - 1) {
1152 bool left = c + 1 < column_info.size()
1153 && cellInfo(cellIndex(r, c + 1)).left_line
1154 || c + 1 == column_info.size();
1155 if (cellInfo(i).right_line && left)
1159 return 2 * nrows_right >= total;
1163 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1165 if (!onlycolumn && isMultiColumn(cell))
1166 return cellInfo(cell).alignment;
1167 return column_info[cellColumn(cell)].alignment;
1172 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1174 if (!onlycolumn && isMultiColumn(cell))
1175 return cellInfo(cell).valignment;
1176 return column_info[cellColumn(cell)].valignment;
1180 Length const Tabular::getPWidth(idx_type cell) const
1182 if (isMultiColumn(cell))
1183 return cellInfo(cell).p_width;
1184 return column_info[cellColumn(cell)].p_width;
1188 int Tabular::cellWidth(idx_type cell) const
1190 return cellInfo(cell).width;
1194 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1198 switch (getAlignment(cell)) {
1199 case LYX_ALIGN_CENTER:
1200 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1202 case LYX_ALIGN_RIGHT:
1203 x += columnWidth(cell) - cellWidth(cell);
1204 // + getAdditionalWidth(cell);
1207 // LYX_ALIGN_LEFT: nothing :-)
1211 // the LaTeX Way :-(
1217 bool Tabular::isFirstCellInRow(idx_type cell) const
1219 return cellColumn(cell) == 0;
1223 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1225 if (row > row_info.size() - 1)
1226 row = row_info.size() - 1;
1227 return cell_info[row][0].cellno;
1231 bool Tabular::isLastCellInRow(idx_type cell) const
1233 return cellRightColumn(cell) == column_info.size() - 1;
1237 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1239 if (row > row_info.size() - 1)
1240 row = row_info.size() - 1;
1241 return cell_info[row][column_info.size() - 1].cellno;
1245 Tabular::row_type Tabular::cellRow(idx_type cell) const
1247 if (cell >= numberofcells)
1248 return row_info.size() - 1;
1251 return rowofcell[cell];
1255 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1257 if (cell >= numberofcells)
1258 return column_info.size() - 1;
1261 return columnofcell[cell];
1265 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1267 row_type const row = cellRow(cell);
1268 col_type column = cellColumn(cell);
1269 while (column < column_info.size() - 1 &&
1270 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1276 void Tabular::write(ostream & os) const
1280 << write_attribute("version", 3)
1281 << write_attribute("rows", row_info.size())
1282 << write_attribute("columns", column_info.size())
1284 // global longtable options
1286 << write_attribute("rotate", rotate)
1287 << write_attribute("booktabs", use_booktabs)
1288 << write_attribute("islongtable", is_long_tabular)
1289 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1290 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1291 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1292 << write_attribute("headTopDL", endhead.topDL)
1293 << write_attribute("headBottomDL", endhead.bottomDL)
1294 << write_attribute("footTopDL", endfoot.topDL)
1295 << write_attribute("footBottomDL", endfoot.bottomDL)
1296 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1297 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1298 << write_attribute("lastFootEmpty", endlastfoot.empty)
1300 for (col_type j = 0; j < column_info.size(); ++j) {
1302 << write_attribute("alignment", column_info[j].alignment)
1303 << write_attribute("valignment", column_info[j].valignment)
1304 << write_attribute("width", column_info[j].p_width.asString())
1305 << write_attribute("special", column_info[j].align_special)
1308 for (row_type i = 0; i < row_info.size(); ++i) {
1309 static const string def("default");
1311 if (row_info[i].top_space_default)
1312 os << write_attribute("topspace", def);
1314 os << write_attribute("topspace", row_info[i].top_space);
1315 if (row_info[i].bottom_space_default)
1316 os << write_attribute("bottomspace", def);
1318 os << write_attribute("bottomspace", row_info[i].bottom_space);
1319 if (row_info[i].interline_space_default)
1320 os << write_attribute("interlinespace", def);
1322 os << write_attribute("interlinespace", row_info[i].interline_space);
1323 os << write_attribute("endhead", row_info[i].endhead)
1324 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1325 << write_attribute("endfoot", row_info[i].endfoot)
1326 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1327 << write_attribute("newpage", row_info[i].newpage)
1328 << write_attribute("caption", row_info[i].caption)
1330 for (col_type j = 0; j < column_info.size(); ++j) {
1332 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1333 << write_attribute("alignment", cell_info[i][j].alignment)
1334 << write_attribute("valignment", cell_info[i][j].valignment)
1335 << write_attribute("topline", cell_info[i][j].top_line)
1336 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1337 << write_attribute("leftline", cell_info[i][j].left_line)
1338 << write_attribute("rightline", cell_info[i][j].right_line)
1339 << write_attribute("rotate", cell_info[i][j].rotate)
1340 << write_attribute("usebox", cell_info[i][j].usebox)
1341 << write_attribute("width", cell_info[i][j].p_width)
1342 << write_attribute("special", cell_info[i][j].align_special)
1344 os << "\\begin_inset ";
1345 cell_info[i][j].inset->write(os);
1346 os << "\n\\end_inset\n"
1351 os << "</lyxtabular>\n";
1355 void Tabular::read(Lexer & lex)
1358 istream & is = lex.getStream();
1360 l_getline(is, line);
1361 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1362 LASSERT(false, /**/);
1367 if (!getTokenValue(line, "version", version))
1369 LASSERT(version >= 2, /**/);
1372 if (!getTokenValue(line, "rows", rows_arg))
1375 if (!getTokenValue(line, "columns", columns_arg))
1377 init(buffer(), rows_arg, columns_arg);
1378 l_getline(is, line);
1379 if (!prefixIs(line, "<features")) {
1380 lyxerr << "Wrong tabular format (expected <features ...> got"
1381 << line << ')' << endl;
1384 getTokenValue(line, "rotate", rotate);
1385 getTokenValue(line, "booktabs", use_booktabs);
1386 getTokenValue(line, "islongtable", is_long_tabular);
1387 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1388 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1389 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1390 getTokenValue(line, "headTopDL", endhead.topDL);
1391 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1392 getTokenValue(line, "footTopDL", endfoot.topDL);
1393 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1394 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1395 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1396 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1398 for (col_type j = 0; j < column_info.size(); ++j) {
1400 if (!prefixIs(line,"<column")) {
1401 lyxerr << "Wrong tabular format (expected <column ...> got"
1402 << line << ')' << endl;
1405 getTokenValue(line, "alignment", column_info[j].alignment);
1406 getTokenValue(line, "valignment", column_info[j].valignment);
1407 getTokenValue(line, "width", column_info[j].p_width);
1408 getTokenValue(line, "special", column_info[j].align_special);
1411 for (row_type i = 0; i < row_info.size(); ++i) {
1412 l_getline(is, line);
1413 if (!prefixIs(line, "<row")) {
1414 lyxerr << "Wrong tabular format (expected <row ...> got"
1415 << line << ')' << endl;
1418 getTokenValue(line, "topspace", row_info[i].top_space,
1419 row_info[i].top_space_default);
1420 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1421 row_info[i].bottom_space_default);
1422 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1423 row_info[i].interline_space_default);
1424 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1425 getTokenValue(line, "endhead", row_info[i].endhead);
1426 getTokenValue(line, "endfoot", row_info[i].endfoot);
1427 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1428 getTokenValue(line, "newpage", row_info[i].newpage);
1429 getTokenValue(line, "caption", row_info[i].caption);
1430 for (col_type j = 0; j < column_info.size(); ++j) {
1431 l_getline(is, line);
1432 if (!prefixIs(line, "<cell")) {
1433 lyxerr << "Wrong tabular format (expected <cell ...> got"
1434 << line << ')' << endl;
1437 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1438 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1439 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1440 getTokenValue(line, "topline", cell_info[i][j].top_line);
1441 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1442 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1443 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1444 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1445 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1446 getTokenValue(line, "width", cell_info[i][j].p_width);
1448 getTokenValue(line, "special", cell_info[i][j].align_special);
1449 l_getline(is, line);
1450 if (prefixIs(line, "\\begin_inset")) {
1451 cell_info[i][j].inset->setBuffer(*buffer_);
1452 cell_info[i][j].inset->read(lex);
1453 l_getline(is, line);
1455 if (!prefixIs(line, "</cell>")) {
1456 lyxerr << "Wrong tabular format (expected </cell> got"
1457 << line << ')' << endl;
1461 l_getline(is, line);
1462 if (!prefixIs(line, "</row>")) {
1463 lyxerr << "Wrong tabular format (expected </row> got"
1464 << line << ')' << endl;
1468 while (!prefixIs(line, "</lyxtabular>")) {
1469 l_getline(is, line);
1475 bool Tabular::isMultiColumn(idx_type cell) const
1477 return cellInfo(cell).multicolumn != CELL_NORMAL;
1481 bool Tabular::isMultiColumnReal(idx_type cell) const
1483 return cellColumn(cell) != cellRightColumn(cell) &&
1484 cellInfo(cell).multicolumn != CELL_NORMAL;
1488 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1490 return cell_info[cellRow(cell)][cellColumn(cell)];
1494 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1496 CellData & cs = cellInfo(cell);
1497 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1498 cs.alignment = column_info[cellColumn(cell)].alignment;
1499 setRightLine(cell, rightLine(cell + number - 1));
1500 for (idx_type i = 1; i < number; ++i) {
1501 CellData & cs1 = cellInfo(cell + i);
1502 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1503 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1510 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1512 row_type const row = cellRow(cell);
1513 col_type const ncols = column_info.size();
1514 idx_type result = 1;
1515 col_type column = cellColumn(cell) + 1;
1516 while (column < ncols && isPartOfMultiColumn(row, column)) {
1524 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1526 row_type const row = cellRow(cell);
1527 col_type column = cellColumn(cell);
1529 idx_type result = 0;
1531 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1532 cell_info[row][column].multicolumn = CELL_NORMAL;
1534 while (column < column_info.size() &&
1535 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1537 cell_info[row][column].multicolumn = CELL_NORMAL;
1547 void Tabular::setRotateCell(idx_type cell, bool flag)
1549 cellInfo(cell).rotate = flag;
1553 bool Tabular::getRotateCell(idx_type cell) const
1555 return cellInfo(cell).rotate;
1559 bool Tabular::needRotating() const
1563 for (row_type i = 0; i < row_info.size(); ++i)
1564 for (col_type j = 0; j < column_info.size(); ++j)
1565 if (cell_info[i][j].rotate)
1571 bool Tabular::isLastCell(idx_type cell) const
1573 if (cell + 1 < numberofcells)
1579 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1581 if (cellRow(cell) > 0)
1582 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1587 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1589 if (cellRow(cell) + 1 < row_info.size())
1590 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1595 Tabular::idx_type Tabular::cellIndex(row_type row,
1596 col_type column) const
1598 BOOST_ASSERT(column != npos && column < column_info.size()
1599 && row != npos && row < row_info.size());
1600 return cell_info[row][column].cellno;
1604 void Tabular::setUsebox(idx_type cell, BoxType type)
1606 cellInfo(cell).usebox = type;
1610 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1611 // adds line breaks, see bug 4886.
1612 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1614 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1615 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1617 if (cellInfo(cell).usebox > 1)
1618 return cellInfo(cell).usebox;
1619 return useParbox(cell);
1624 // This are functions used for the longtable support
1626 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1632 row_info[row].endfirsthead = flag;
1636 row_info[row].endhead = flag;
1641 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1644 hd.set = haveLTHead();
1645 return row_info[row].endhead;
1649 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1652 hd.set = haveLTFirstHead();
1653 return row_info[row].endfirsthead;
1657 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1663 row_info[row].endlastfoot = flag;
1667 row_info[row].endfoot = flag;
1672 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1675 fd.set = haveLTFoot();
1676 return row_info[row].endfoot;
1680 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1683 fd.set = haveLTLastFoot();
1684 return row_info[row].endlastfoot;
1688 void Tabular::setLTNewPage(row_type row, bool what)
1690 row_info[row].newpage = what;
1694 bool Tabular::getLTNewPage(row_type row) const
1696 return row_info[row].newpage;
1700 bool Tabular::haveLTHead() const
1702 for (row_type i = 0; i < row_info.size(); ++i)
1703 if (row_info[i].endhead)
1709 bool Tabular::haveLTFirstHead() const
1711 if (endfirsthead.empty)
1713 for (row_type i = 0; i < row_info.size(); ++i)
1714 if (row_info[i].endfirsthead)
1720 bool Tabular::haveLTFoot() const
1722 for (row_type i = 0; i < row_info.size(); ++i)
1723 if (row_info[i].endfoot)
1729 bool Tabular::haveLTLastFoot() const
1731 if (endlastfoot.empty)
1733 for (row_type i = 0; i < row_info.size(); ++i)
1734 if (row_info[i].endlastfoot)
1740 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1742 idx_type i = getFirstCellInRow(row);
1744 setMultiColumn(i, column_info.size());
1745 setTopLine(i, false);
1746 setBottomLine(i, false);
1747 setLeftLine(i, false);
1748 setRightLine(i, false);
1750 unsetMultiColumn(i);
1751 row_info[row].caption = what;
1756 bool Tabular::ltCaption(row_type row) const
1758 return row_info[row].caption;
1762 // end longtable support functions
1764 void Tabular::setRowAscent(row_type row, int height)
1766 if (row >= row_info.size() || row_info[row].ascent == height)
1768 row_info[row].ascent = height;
1772 void Tabular::setRowDescent(row_type row, int height)
1774 if (row >= row_info.size() || row_info[row].descent == height)
1776 row_info[row].descent = height;
1780 int Tabular::rowAscent(row_type row) const
1782 if (row >= row_info.size())
1784 return row_info[row].ascent;
1788 int Tabular::rowDescent(row_type row) const
1790 LASSERT(row < row_info.size(), /**/);
1791 return row_info[row].descent;
1795 int Tabular::height() const
1798 for (row_type row = 0; row < row_info.size(); ++row)
1799 height += rowAscent(row) + rowDescent(row) +
1800 getAdditionalHeight(row);
1805 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1807 LASSERT(row < row_info.size(), /**/);
1808 LASSERT(column < column_info.size(), /**/);
1809 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1813 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1815 // we only output complete row lines and the 1st row here, the rest
1816 // is done in Tabular::TeXBottomHLine(...)
1818 // get for each column the topline (if any)
1819 col_type const ncols = column_info.size();
1820 vector<bool> topline;
1822 for (col_type c = 0; c < ncols; ++c) {
1823 topline.push_back(topLine(cellIndex(row, c)));
1828 // do nothing if empty first row, or incomplete row line after
1829 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1832 // only output complete row lines and the 1st row's clines
1833 if (nset == ncols) {
1835 os << (row == 0 ? "\\toprule " : "\\midrule ");
1839 } else if (row == 0) {
1840 for (col_type c = 0; c < ncols; ++c) {
1842 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1843 // get to last column of line span
1844 while (c < ncols && topline[c])
1855 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1857 // we output bottomlines of row r and the toplines of row r+1
1858 // if the latter do not span the whole tabular
1860 // get the bottomlines of row r, and toplines in next row
1861 bool lastrow = row == row_info.size() - 1;
1862 col_type const ncols = column_info.size();
1863 vector<bool> bottomline, topline;
1864 bool nextrowset = true;
1865 for (col_type c = 0; c < ncols; ++c) {
1866 bottomline.push_back(bottomLine(cellIndex(row, c)));
1867 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1868 nextrowset &= topline[c];
1871 // combine this row's bottom lines and next row's toplines if necessary
1873 for (col_type c = 0; c < ncols; ++c) {
1875 bottomline[c] = bottomline[c] || topline[c];
1880 // do nothing if empty, OR incomplete row line with a topline in next row
1881 if (nset == 0 || (nextrowset && nset != ncols))
1884 if (nset == ncols) {
1886 os << (lastrow ? "\\bottomrule" : "\\midrule");
1890 for (col_type c = 0; c < ncols; ++c) {
1891 if (bottomline[c]) {
1892 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1894 // get to last column of line span
1895 while (c < ncols && bottomline[c])
1906 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1909 row_type const r = cellRow(cell);
1910 if (is_long_tabular && row_info[r].caption)
1913 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1914 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1915 // figure out how to set the lines
1916 // we always set double lines to the right of the cell
1917 col_type const c = cellColumn(cell);
1918 col_type const nextcol = c + columnSpan(cell);
1919 bool colright = columnRightLine(c);
1920 bool colleft = columnLeftLine(c);
1921 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1922 bool nextcellleft = nextcol < column_info.size()
1923 && leftLine(cellIndex(r, nextcol));
1924 bool coldouble = colright && nextcolleft;
1925 bool celldouble = rightLine(cell) && nextcellleft;
1926 ismulticol = isMultiColumn(cell)
1927 || (c == 0 && colleft != leftLine(cell))
1928 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1929 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1930 || (coldouble != celldouble);
1932 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1933 if (c ==0 && leftLine(cell))
1935 if (!cellInfo(cell).align_special.empty()) {
1936 os << cellInfo(cell).align_special;
1938 if (!getPWidth(cell).zero()) {
1940 case LYX_ALIGN_LEFT:
1941 os << ">{\\raggedright}";
1943 case LYX_ALIGN_RIGHT:
1944 os << ">{\\raggedleft}";
1946 case LYX_ALIGN_CENTER:
1947 os << ">{\\centering}";
1953 case LYX_VALIGN_TOP:
1956 case LYX_VALIGN_MIDDLE:
1959 case LYX_VALIGN_BOTTOM:
1964 << from_ascii(getPWidth(cell).asLatexString())
1968 case LYX_ALIGN_LEFT:
1971 case LYX_ALIGN_RIGHT:
1978 } // end if else !getPWidth
1979 } // end if else !cellinfo_of_cell
1980 if (rightLine(cell) || nextcellleft)
1983 // add extra vertical line if we want a double one
1987 if (getRotateCell(cell)) {
1988 os << "\\begin{sideways}\n";
1991 if (getUsebox(cell) == BOX_PARBOX) {
1994 case LYX_VALIGN_TOP:
1997 case LYX_VALIGN_MIDDLE:
2000 case LYX_VALIGN_BOTTOM:
2004 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2006 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2007 os << "\\begin{minipage}[";
2009 case LYX_VALIGN_TOP:
2012 case LYX_VALIGN_MIDDLE:
2015 case LYX_VALIGN_BOTTOM:
2019 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2027 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2030 row_type const r = cellRow(cell);
2031 if (is_long_tabular && row_info[r].caption)
2035 if (getUsebox(cell) == BOX_PARBOX)
2037 else if (getUsebox(cell) == BOX_MINIPAGE) {
2038 os << "%\n\\end{minipage}";
2041 if (getRotateCell(cell)) {
2042 os << "%\n\\end{sideways}";
2052 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2053 OutputParams const & runparams) const
2055 if (!is_long_tabular)
2059 // output header info
2061 if (endhead.topDL) {
2065 for (row_type i = 0; i < row_info.size(); ++i) {
2066 if (row_info[i].endhead) {
2067 ret += TeXRow(os, i, runparams);
2070 if (endhead.bottomDL) {
2074 os << "\\endhead\n";
2076 if (endfirsthead.empty) {
2077 os << "\\endfirsthead\n";
2081 // output firstheader info
2082 if (haveLTFirstHead()) {
2083 if (endfirsthead.topDL) {
2087 for (row_type i = 0; i < row_info.size(); ++i) {
2088 if (row_info[i].endfirsthead) {
2089 ret += TeXRow(os, i, runparams);
2092 if (endfirsthead.bottomDL) {
2096 os << "\\endfirsthead\n";
2099 // output footer info
2101 if (endfoot.topDL) {
2105 for (row_type i = 0; i < row_info.size(); ++i) {
2106 if (row_info[i].endfoot) {
2107 ret += TeXRow(os, i, runparams);
2110 if (endfoot.bottomDL) {
2114 os << "\\endfoot\n";
2116 if (endlastfoot.empty) {
2117 os << "\\endlastfoot\n";
2121 // output lastfooter info
2122 if (haveLTLastFoot()) {
2123 if (endlastfoot.topDL) {
2127 for (row_type i = 0; i < row_info.size(); ++i) {
2128 if (row_info[i].endlastfoot) {
2129 ret += TeXRow(os, i, runparams);
2132 if (endlastfoot.bottomDL) {
2136 os << "\\endlastfoot\n";
2143 bool Tabular::isValidRow(row_type row) const
2145 if (!is_long_tabular)
2147 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2148 !row_info[row].endfoot && !row_info[row].endlastfoot;
2152 int Tabular::TeXRow(odocstream & os, row_type i,
2153 OutputParams const & runparams) const
2155 idx_type cell = cellIndex(i, 0);
2156 int ret = TeXTopHLine(os, i);
2157 if (row_info[i].top_space_default) {
2159 os << "\\addlinespace\n";
2161 os << "\\noalign{\\vskip\\doublerulesep}\n";
2163 } else if(!row_info[i].top_space.zero()) {
2165 os << "\\addlinespace["
2166 << from_ascii(row_info[i].top_space.asLatexString())
2169 os << "\\noalign{\\vskip"
2170 << from_ascii(row_info[i].top_space.asLatexString())
2175 bool ismulticol = false;
2176 for (col_type j = 0; j < column_info.size(); ++j) {
2177 if (isPartOfMultiColumn(i, j))
2179 ret += TeXCellPreamble(os, cell, ismulticol);
2180 shared_ptr<InsetTableCell> inset = cellInset(cell);
2182 Paragraph const & par = inset->paragraphs().front();
2183 bool rtl = par.isRTL(buffer().params())
2185 && getPWidth(cell).zero();
2189 par.getParLanguage(buffer().params())->lang();
2190 if (lang == "farsi")
2192 else if (lang == "arabic_arabi")
2194 // currently, remaning RTL languages are
2195 // arabic_arabtex and hebrew
2199 // pass to the OutputParams that we are in a cell and
2200 // which alignment we have set.
2201 // InsetNewline needs this context information.
2202 OutputParams newrp = runparams;
2203 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2204 ? OutputParams::PLAIN
2205 : OutputParams::ALIGNED;
2206 ret += inset->latex(os, newrp);
2210 ret += TeXCellPostamble(os, cell, ismulticol);
2211 if (!isLastCellInRow(cell)) { // not last cell in row
2216 os << "\\tabularnewline";
2217 if (row_info[i].bottom_space_default) {
2219 os << "\\addlinespace";
2221 os << "[\\doublerulesep]";
2222 } else if (!row_info[i].bottom_space.zero()) {
2224 os << "\\addlinespace";
2226 << from_ascii(row_info[i].bottom_space.asLatexString())
2231 ret += TeXBottomHLine(os, i);
2232 if (row_info[i].interline_space_default) {
2234 os << "\\addlinespace\n";
2236 os << "\\noalign{\\vskip\\doublerulesep}\n";
2238 } else if (!row_info[i].interline_space.zero()) {
2240 os << "\\addlinespace["
2241 << from_ascii(row_info[i].interline_space.asLatexString())
2244 os << "\\noalign{\\vskip"
2245 << from_ascii(row_info[i].interline_space.asLatexString())
2253 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2257 //+---------------------------------------------------------------------
2258 //+ first the opening preamble +
2259 //+---------------------------------------------------------------------
2262 os << "\\begin{sideways}\n";
2265 if (is_long_tabular)
2266 os << "\\begin{longtable}{";
2268 os << "\\begin{tabular}{";
2270 for (col_type i = 0; i < column_info.size(); ++i) {
2271 if (columnLeftLine(i))
2273 if (!column_info[i].align_special.empty()) {
2274 os << column_info[i].align_special;
2276 if (!column_info[i].p_width.zero()) {
2277 switch (column_info[i].alignment) {
2278 case LYX_ALIGN_LEFT:
2279 os << ">{\\raggedright}";
2281 case LYX_ALIGN_RIGHT:
2282 os << ">{\\raggedleft}";
2284 case LYX_ALIGN_CENTER:
2285 os << ">{\\centering}";
2287 case LYX_ALIGN_NONE:
2288 case LYX_ALIGN_BLOCK:
2289 case LYX_ALIGN_LAYOUT:
2290 case LYX_ALIGN_SPECIAL:
2294 switch (column_info[i].valignment) {
2295 case LYX_VALIGN_TOP:
2298 case LYX_VALIGN_MIDDLE:
2301 case LYX_VALIGN_BOTTOM:
2306 << from_ascii(column_info[i].p_width.asLatexString())
2309 switch (column_info[i].alignment) {
2310 case LYX_ALIGN_LEFT:
2313 case LYX_ALIGN_RIGHT:
2320 } // end if else !column_info[i].p_width
2321 } // end if else !column_info[i].align_special
2322 if (columnRightLine(i))
2328 ret += TeXLongtableHeaderFooter(os, runparams);
2330 //+---------------------------------------------------------------------
2331 //+ the single row and columns (cells) +
2332 //+---------------------------------------------------------------------
2334 for (row_type i = 0; i < row_info.size(); ++i) {
2335 if (isValidRow(i)) {
2336 ret += TeXRow(os, i, runparams);
2337 if (is_long_tabular && row_info[i].newpage) {
2338 os << "\\newpage\n";
2344 //+---------------------------------------------------------------------
2345 //+ the closing of the tabular +
2346 //+---------------------------------------------------------------------
2348 if (is_long_tabular)
2349 os << "\\end{longtable}";
2351 os << "\\end{tabular}";
2353 os << "\n\\end{sideways}";
2361 int Tabular::docbookRow(odocstream & os, row_type row,
2362 OutputParams const & runparams) const
2365 idx_type cell = getFirstCellInRow(row);
2368 for (col_type j = 0; j < column_info.size(); ++j) {
2369 if (isPartOfMultiColumn(row, j))
2372 os << "<entry align=\"";
2373 switch (getAlignment(cell)) {
2374 case LYX_ALIGN_LEFT:
2377 case LYX_ALIGN_RIGHT:
2385 os << "\" valign=\"";
2386 switch (getVAlignment(cell)) {
2387 case LYX_VALIGN_TOP:
2390 case LYX_VALIGN_BOTTOM:
2393 case LYX_VALIGN_MIDDLE:
2398 if (isMultiColumn(cell)) {
2399 os << " namest=\"col" << j << "\" ";
2400 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2404 ret += cellInset(cell)->docbook(os, runparams);
2413 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2417 //+---------------------------------------------------------------------
2418 //+ first the opening preamble +
2419 //+---------------------------------------------------------------------
2421 os << "<tgroup cols=\"" << column_info.size()
2422 << "\" colsep=\"1\" rowsep=\"1\">\n";
2424 for (col_type i = 0; i < column_info.size(); ++i) {
2425 os << "<colspec colname=\"col" << i << "\" align=\"";
2426 switch (column_info[i].alignment) {
2427 case LYX_ALIGN_LEFT:
2430 case LYX_ALIGN_RIGHT:
2438 if (runparams.flavor == OutputParams::XML)
2444 //+---------------------------------------------------------------------
2445 //+ Long Tabular case +
2446 //+---------------------------------------------------------------------
2448 // output header info
2449 if (haveLTHead() || haveLTFirstHead()) {
2452 for (row_type i = 0; i < row_info.size(); ++i) {
2453 if (row_info[i].endhead || row_info[i].endfirsthead) {
2454 ret += docbookRow(os, i, runparams);
2460 // output footer info
2461 if (haveLTFoot() || haveLTLastFoot()) {
2464 for (row_type i = 0; i < row_info.size(); ++i) {
2465 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2466 ret += docbookRow(os, i, runparams);
2473 //+---------------------------------------------------------------------
2474 //+ the single row and columns (cells) +
2475 //+---------------------------------------------------------------------
2479 for (row_type i = 0; i < row_info.size(); ++i) {
2480 if (isValidRow(i)) {
2481 ret += docbookRow(os, i, runparams);
2486 //+---------------------------------------------------------------------
2487 //+ the closing of the tabular +
2488 //+---------------------------------------------------------------------
2497 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2498 vector<unsigned int> const & clen) const
2500 idx_type const fcell = getFirstCellInRow(row);
2501 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2504 for (idx_type i = fcell; i < n; ++i) {
2514 for (idx_type i = fcell; i < n; ++i) {
2525 col_type column = cellColumn(i);
2526 int len = clen[column];
2527 while (column < column_info.size() - 1
2528 && isPartOfMultiColumn(row, ++column))
2529 len += clen[column] + 4;
2530 os << docstring(len, ch);
2545 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2546 vector<unsigned int> const & clen) const
2548 idx_type const fcell = getFirstCellInRow(row);
2549 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2552 for (idx_type i = fcell; i < n; ++i) {
2553 if (bottomLine(i)) {
2562 for (idx_type i = fcell; i < n; ++i) {
2563 if (bottomLine(i)) {
2573 col_type column = cellColumn(i);
2574 int len = clen[column];
2575 while (column < column_info.size() -1
2576 && isPartOfMultiColumn(row, ++column))
2577 len += clen[column] + 4;
2578 os << docstring(len, ch);
2579 if (bottomLine(i)) {
2593 void Tabular::plaintextPrintCell(odocstream & os,
2594 OutputParams const & runparams,
2595 idx_type cell, row_type row, col_type column,
2596 vector<unsigned int> const & clen,
2597 bool onlydata) const
2599 odocstringstream sstr;
2600 cellInset(cell)->plaintext(sstr, runparams);
2612 unsigned int len1 = sstr.str().length();
2613 unsigned int len2 = clen[column];
2614 while (column < column_info.size() -1
2615 && isPartOfMultiColumn(row, ++column))
2616 len2 += clen[column] + 4;
2619 switch (getAlignment(cell)) {
2621 case LYX_ALIGN_LEFT:
2624 case LYX_ALIGN_RIGHT:
2628 case LYX_ALIGN_CENTER:
2634 os << docstring(len1, ' ') << sstr.str()
2635 << docstring(len2, ' ');
2637 if (rightLine(cell))
2644 void Tabular::plaintext(odocstream & os,
2645 OutputParams const & runparams, int const depth,
2646 bool onlydata, char_type delim) const
2648 // first calculate the width of the single columns
2649 vector<unsigned int> clen(column_info.size());
2652 // first all non (real) multicolumn cells!
2653 for (col_type j = 0; j < column_info.size(); ++j) {
2655 for (row_type i = 0; i < row_info.size(); ++i) {
2656 idx_type cell = cellIndex(i, j);
2657 if (isMultiColumnReal(cell))
2659 odocstringstream sstr;
2660 cellInset(cell)->plaintext(sstr, runparams);
2661 if (clen[j] < sstr.str().length())
2662 clen[j] = sstr.str().length();
2665 // then all (real) multicolumn cells!
2666 for (col_type j = 0; j < column_info.size(); ++j) {
2667 for (row_type i = 0; i < row_info.size(); ++i) {
2668 idx_type cell = cellIndex(i, j);
2669 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2671 odocstringstream sstr;
2672 cellInset(cell)->plaintext(sstr, runparams);
2673 int len = int(sstr.str().length());
2674 idx_type const n = columnSpan(cell);
2675 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2677 if (len > int(clen[j + n - 1]))
2678 clen[j + n - 1] = len;
2683 for (row_type i = 0; i < row_info.size(); ++i) {
2684 if (!onlydata && plaintextTopHLine(os, i, clen))
2685 os << docstring(depth * 2, ' ');
2686 for (col_type j = 0; j < column_info.size(); ++j) {
2687 if (isPartOfMultiColumn(i, j))
2689 if (onlydata && j > 0)
2690 // we don't use operator<< for single UCS4 character.
2691 // see explanation in docstream.h
2693 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2698 os << docstring(depth * 2, ' ');
2699 if (plaintextBottomHLine(os, i, clen))
2700 os << docstring(depth * 2, ' ');
2706 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2708 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2712 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2713 col_type column) const
2715 return cell_info[row][column].inset;
2719 void Tabular::setCellInset(row_type row, col_type column,
2720 shared_ptr<InsetTableCell> ins) const
2722 CellData & cd = cell_info[row][column];
2727 void Tabular::validate(LaTeXFeatures & features) const
2729 features.require("NeedTabularnewline");
2731 features.require("booktabs");
2732 if (is_long_tabular)
2733 features.require("longtable");
2735 features.require("rotating");
2736 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2737 if (getVAlignment(cell) != LYX_VALIGN_TOP
2738 || !getPWidth(cell).zero())
2739 features.require("array");
2740 cellInset(cell)->validate(features);
2745 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2747 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2748 ParagraphList::const_iterator cit = parlist.begin();
2749 ParagraphList::const_iterator end = parlist.end();
2751 for (; cit != end; ++cit)
2752 for (int i = 0; i < cit->size(); ++i)
2753 if (cit->isNewline(i))
2760 /////////////////////////////////////////////////////////////////////
2764 /////////////////////////////////////////////////////////////////////
2766 InsetTableCell::InsetTableCell(Buffer & buf)
2767 : InsetText(buf), isFixedWidth(false),
2768 contentAlign(LYX_ALIGN_CENTER)
2772 bool InsetTableCell::forcePlainLayout(idx_type) const
2774 return !isFixedWidth;
2778 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2780 return isFixedWidth;
2784 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2785 FuncStatus & status) const
2788 switch (cmd.action) {
2790 enabled = !forcePlainLayout();
2792 case LFUN_LAYOUT_PARAGRAPH:
2793 enabled = allowParagraphCustomization();
2796 return InsetText::getStatus(cur, cmd, status);
2798 status.setEnabled(enabled);
2802 docstring InsetTableCell::asString(bool intoInsets)
2805 if (paragraphs().empty())
2807 ParagraphList::const_iterator it = paragraphs().begin();
2808 ParagraphList::const_iterator en = paragraphs().end();
2810 for (; it != en; ++it) {
2815 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2822 /////////////////////////////////////////////////////////////////////
2826 /////////////////////////////////////////////////////////////////////
2828 InsetTabular::InsetTabular(Buffer & buf, row_type rows,
2830 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2831 rowselect_(false), colselect_(false)
2833 setBuffer(buf); // FIXME: remove later
2837 InsetTabular::InsetTabular(InsetTabular const & tab)
2838 : Inset(tab), tabular(tab.tabular), scx_(0)
2843 InsetTabular::~InsetTabular()
2845 hideDialogs("tabular", this);
2849 void InsetTabular::setBuffer(Buffer & buf)
2851 tabular.setBuffer(buf);
2852 Inset::setBuffer(buf);
2856 bool InsetTabular::insetAllowed(InsetCode code) const
2858 if (code == MATHMACRO_CODE)
2865 void InsetTabular::write(ostream & os) const
2867 os << "Tabular" << endl;
2872 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2874 // FIXME: depending on the selection state, we could offer a different menu.
2875 return from_ascii("context-tabular");
2879 void InsetTabular::read(Lexer & lex)
2881 //bool const old_format = (lex.getString() == "\\LyXTable");
2889 string token = lex.getString();
2890 while (lex && token != "\\end_inset") {
2892 token = lex.getString();
2895 lex.printError("Missing \\end_inset at this point. ");
2899 int InsetTabular::rowFromY(Cursor & cur, int y) const
2901 // top y coordinate of tabular
2902 int h = yo(cur.bv()) - tabular.rowAscent(0);
2903 size_t nrows = tabular.row_info.size();
2905 for (; r < nrows && y > h; ++r) {
2906 h += tabular.rowAscent(r);
2907 h += tabular.rowDescent(r);
2908 h += tabular.getAdditionalHeight(r);
2914 int InsetTabular::columnFromX(Cursor & cur, int x) const
2916 // left x coordinate of tabular
2917 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2918 size_t ncols = tabular.column_info.size();
2920 for (; c < ncols && x > w; ++c)
2921 w += tabular.columnWidth(c);
2926 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2928 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2929 // mi.base.textwidth << "\n";
2932 LASSERT(false, /**/);
2936 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2939 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2940 if (tabular.isPartOfMultiColumn(i, j))
2941 // Multicolumn cell, but not first one
2946 if (tabular.cell_info[i][j].multicolumn ==
2947 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2948 p_width = tabular.cellInfo(cell).p_width;
2950 p_width = tabular.column_info[j].p_width;
2951 if (!p_width.zero())
2952 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2953 tabular.cellInset(cell)->metrics(m, dim);
2954 if (!p_width.zero())
2955 dim.wid = m.base.textwidth;
2956 tabular.setCellWidth(cell, dim.wid);
2957 maxAsc = max(maxAsc, dim.asc);
2958 maxDesc = max(maxDesc, dim.des);
2961 int const top_space = tabular.row_info[i].top_space_default ?
2962 default_line_space :
2963 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2964 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2965 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2966 default_line_space :
2967 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2968 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2970 tabular.updateColumnWidths();
2971 dim.asc = tabular.rowAscent(0);
2972 dim.des = tabular.height() - dim.asc;
2973 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2977 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2979 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2980 BufferView * bv = pi.base.bv;
2982 // FIXME: As the full backrgound is painted in drawSelection(),
2983 // we have no choice but to do a full repaint for the Text cells.
2984 pi.full_repaint = true;
2986 resetPos(bv->cursor());
2989 x += ADD_TO_TABULAR_WIDTH;
2991 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2994 first_visible_cell = Tabular::npos;
2995 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
2997 int const a = tabular.rowAscent(i);
2998 int const d = tabular.rowDescent(i);
2999 idx = tabular.cellIndex(i, 0);
3000 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3001 if (tabular.isPartOfMultiColumn(i, j))
3003 if (first_visible_cell == Tabular::npos)
3004 first_visible_cell = idx;
3006 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3007 // Cache the Inset position.
3008 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3009 if (nx + tabular.columnWidth(idx) < 0
3010 || nx > bv->workWidth()
3012 || y - a > bv->workHeight()) {
3013 pi.pain.setDrawingEnabled(false);
3014 cell(idx)->draw(pi, cx, y);
3015 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3016 pi.pain.setDrawingEnabled(original_drawing_state);
3018 cell(idx)->draw(pi, cx, y);
3019 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3021 nx += tabular.columnWidth(idx);
3025 if (i + 1 < tabular.row_info.size())
3026 y += d + tabular.rowAscent(i + 1) +
3027 tabular.getAdditionalHeight(i + 1);
3032 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3034 Cursor & cur = pi.base.bv->cursor();
3036 x += scx_ + ADD_TO_TABULAR_WIDTH;
3038 // FIXME: it is wrong to completely paint the background
3039 // if we want to do single row painting.
3041 // Paint background of current tabular
3042 int const w = tabular.width();
3043 int const h = tabular.height();
3044 int yy = y - tabular.rowAscent(0);
3045 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3047 if (!cur.selection())
3049 if (&cur.inset() != this)
3055 if (cur.selIsMultiCell()) {
3058 getSelection(cur, rs, re, cs, ce);
3059 y -= tabular.rowAscent(0);
3060 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3061 int const a = tabular.rowAscent(j);
3062 int const h = a + tabular.rowDescent(j);
3064 y += tabular.getAdditionalHeight(j);
3065 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3066 if (tabular.isPartOfMultiColumn(j, i))
3068 idx_type const cell =
3069 tabular.cellIndex(j, i);
3070 int const w = tabular.columnWidth(cell);
3071 if (i >= cs && i <= ce && j >= rs && j <= re)
3072 pi.pain.fillRectangle(xx, y, w, h,
3080 x += cellXPos(cur.idx());
3081 x += tabular.getBeginningOfTextInCell(cur.idx());
3082 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3087 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3088 row_type row, idx_type cell, bool erased) const
3090 int x2 = x + tabular.columnWidth(cell);
3091 bool on_off = false;
3092 ColorCode col = Color_tabularline;
3093 ColorCode onoffcol = Color_tabularonoffline;
3096 col = Color_deletedtext;
3097 onoffcol = Color_deletedtext;
3100 if (!tabular.topAlreadyDrawn(cell)) {
3101 on_off = !tabular.topLine(cell);
3102 pain.line(x, y - tabular.rowAscent(row),
3103 x2, y - tabular.rowAscent(row),
3104 on_off ? onoffcol : col,
3105 on_off ? Painter::line_onoffdash : Painter::line_solid);
3107 on_off = !tabular.bottomLine(cell);
3108 pain.line(x, y + tabular.rowDescent(row),
3109 x2, y + tabular.rowDescent(row),
3110 on_off ? onoffcol : col,
3111 on_off ? Painter::line_onoffdash : Painter::line_solid);
3112 if (!tabular.leftAlreadyDrawn(cell)) {
3113 on_off = !tabular.leftLine(cell);
3114 pain.line(x, y - tabular.rowAscent(row),
3115 x, y + tabular.rowDescent(row),
3116 on_off ? onoffcol : col,
3117 on_off ? Painter::line_onoffdash : Painter::line_solid);
3119 on_off = !tabular.rightLine(cell);
3120 pain.line(x2 - tabular.getAdditionalWidth(cell),
3121 y - tabular.rowAscent(row),
3122 x2 - tabular.getAdditionalWidth(cell),
3123 y + tabular.rowDescent(row),
3124 on_off ? onoffcol : col,
3125 on_off ? Painter::line_onoffdash : Painter::line_solid);
3129 docstring InsetTabular::editMessage() const
3131 return _("Opened table");
3135 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3137 //lyxerr << "InsetTabular::edit: " << this << endl;
3139 cur.setSelection(false);
3142 if (isRightToLeft(cur))
3143 cur.idx() = tabular.getLastCellInRow(0);
3149 if (isRightToLeft(cur))
3150 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3152 cur.idx() = tabular.numberofcells - 1;
3154 cur.pos() = cur.lastpos(); // FIXME crude guess
3156 // FIXME: this accesses the position cache before it is initialized
3158 //cur.bv().fitCursor();
3162 void InsetTabular::updateLabels(ParIterator const & it)
3164 // In a longtable, tell captions what the current float is
3165 Counters & cnts = buffer().params().documentClass().counters();
3166 string const saveflt = cnts.current_float();
3167 if (tabular.is_long_tabular)
3168 cnts.current_float("table");
3170 ParIterator it2 = it;
3172 size_t const end = it2.nargs();
3173 for ( ; it2.idx() < end; it2.top().forwardIdx())
3174 lyx::updateLabels(buffer(), it2);
3177 if (tabular.is_long_tabular)
3178 cnts.current_float(saveflt);
3182 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3184 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3185 << "\n cur:" << cur);
3186 CursorSlice sl = cur.top();
3187 Cursor & bvcur = cur.bv().cursor();
3189 switch (cmd.action) {
3191 case LFUN_MOUSE_PRESS: {
3192 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3194 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3195 || cmd.x > xo(cur.bv()) + tabular.width()) {
3196 row_type r = rowFromY(cur, cmd.y);
3197 cur.idx() = tabular.getFirstCellInRow(r);
3200 cur.idx() = tabular.getLastCellInRow(r);
3201 cur.pos() = cur.lastpos();
3202 cur.setSelection(true);
3208 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3209 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3210 || cmd.y > y0 + tabular.height()) {
3211 col_type c = columnFromX(cur, cmd.x);
3212 cur.idx() = tabular.cellIndex(0, c);
3215 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3216 cur.pos() = cur.lastpos();
3217 cur.setSelection(true);
3222 // do not reset cursor/selection if we have selected
3223 // some cells (bug 2715).
3224 if (cmd.button() == mouse_button::button3
3225 && &bvcur.selBegin().inset() == this
3226 && bvcur.selIsMultiCell())
3229 // Let InsetTableCell do it
3230 cell(cur.idx())->dispatch(cur, cmd);
3233 case LFUN_MOUSE_MOTION:
3234 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3235 if (cmd.button() == mouse_button::button1) {
3236 // only accept motions to places not deeper nested than the real anchor
3237 if (!bvcur.anchor_.hasPart(cur)) {
3241 // select (additional) row
3243 row_type r = rowFromY(cur, cmd.y);
3244 cur.idx() = tabular.getLastCellInRow(r);
3245 // we need to reset the cursor's pit and pos now, as the old ones
3246 // may no longer be valid.
3249 bvcur.setCursor(cur);
3250 bvcur.setSelection(true);
3253 // select (additional) column
3255 col_type c = columnFromX(cur, cmd.x);
3256 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3257 // we need to reset the cursor's pit and pos now, as the old ones
3258 // may no longer be valid.
3261 bvcur.setCursor(cur);
3262 bvcur.setSelection(true);
3265 // only update if selection changes
3266 if (bvcur.idx() == cur.idx() &&
3267 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3269 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3270 bvcur.setCursor(cur);
3271 bvcur.setSelection(true);
3272 // if this is a multicell selection, we just set the cursor to
3273 // the beginning of the cell's text.
3274 if (bvcur.selIsMultiCell()) {
3281 case LFUN_MOUSE_RELEASE:
3286 case LFUN_CELL_BACKWARD:
3288 cur.setSelection(false);
3291 case LFUN_CELL_FORWARD:
3293 cur.setSelection(false);
3296 case LFUN_CHAR_FORWARD_SELECT:
3297 case LFUN_CHAR_FORWARD:
3298 case LFUN_CHAR_BACKWARD_SELECT:
3299 case LFUN_CHAR_BACKWARD:
3300 case LFUN_CHAR_RIGHT_SELECT:
3301 case LFUN_CHAR_RIGHT:
3302 case LFUN_CHAR_LEFT_SELECT:
3303 case LFUN_CHAR_LEFT: {
3304 // determine whether we move to next or previous cell, where to enter
3305 // the new cell from, and which command to "finish" (i.e., exit the
3308 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3309 FuncCode finish_lfun;
3311 if (cmd.action == LFUN_CHAR_FORWARD
3312 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3314 finish_lfun = LFUN_FINISHED_FORWARD;
3316 else if (cmd.action == LFUN_CHAR_BACKWARD
3317 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3319 finish_lfun = LFUN_FINISHED_BACKWARD;
3321 // LEFT or RIGHT commands --- the interpretation will depend on the
3322 // table's direction.
3324 bool right = (cmd.action == LFUN_CHAR_RIGHT
3325 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3326 next_cell = (isRightToLeft(cur) != right);
3328 if (lyxrc.visual_cursor) {
3329 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3333 finish_lfun = LFUN_FINISHED_RIGHT;
3335 finish_lfun = LFUN_FINISHED_LEFT;
3338 // if we don't have a multicell selection...
3339 if (!cur.selIsMultiCell() ||
3340 // ...or we're not doing some LFUN_*_SELECT thing, anyway...
3341 (cmd.action != LFUN_CHAR_FORWARD_SELECT &&
3342 cmd.action != LFUN_CHAR_BACKWARD_SELECT &&
3343 cmd.action != LFUN_CHAR_RIGHT_SELECT &&
3344 cmd.action != LFUN_CHAR_LEFT_SELECT)) {
3345 // ...try to dispatch to the cell's inset.
3346 cell(cur.idx())->dispatch(cur, cmd);
3347 if (cur.result().dispatched())
3350 // move to next/prev cell, as appropriate
3351 // note that we will always do this if we're selecting and we have
3352 // a multicell selection
3353 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3354 << " cell from: " << int(entry_from));
3356 moveNextCell(cur, entry_from);
3358 movePrevCell(cur, entry_from);
3359 // if we're exiting the table, call the appropriate FINISHED lfun
3360 if (sl == cur.top())
3361 cmd = FuncRequest(finish_lfun);
3368 case LFUN_DOWN_SELECT:
3370 cell(cur.idx())->dispatch(cur, cmd);
3371 cur.dispatched(); // override the cell's decision
3372 if (sl == cur.top())
3373 // if our Text didn't do anything to the cursor
3374 // then we try to put the cursor into the cell below
3375 // setting also the right targetX.
3376 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3377 cur.idx() = tabular.cellBelow(cur.idx());
3379 TextMetrics const & tm =
3380 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3381 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3383 if (sl == cur.top()) {
3384 // we trick it to go to forward after leaving the
3386 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3391 case LFUN_UP_SELECT:
3393 cell(cur.idx())->dispatch(cur, cmd);
3394 cur.dispatched(); // override the cell's decision
3395 if (sl == cur.top())
3396 // if our Text didn't do anything to the cursor
3397 // then we try to put the cursor into the cell above
3398 // setting also the right targetX.
3399 if (tabular.cellRow(cur.idx()) != 0) {
3400 cur.idx() = tabular.cellAbove(cur.idx());
3401 cur.pit() = cur.lastpit();
3402 Text const * text = cell(cur.idx())->getText(0);
3403 TextMetrics const & tm = cur.bv().textMetrics(text);
3404 ParagraphMetrics const & pm =
3405 tm.parMetrics(cur.lastpit());
3406 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3408 if (sl == cur.top()) {
3409 cmd = FuncRequest(LFUN_UP);
3414 // case LFUN_SCREEN_DOWN: {
3415 // //if (hasSelection())
3416 // // cur.selection() = false;
3417 // col_type const col = tabular.cellColumn(cur.idx());
3418 // int const t = cur.bv().top_y() + cur.bv().height();
3419 // if (t < yo() + tabular.getHeightOfTabular()) {
3420 // cur.bv().scrollDocView(t);
3421 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3423 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3430 // case LFUN_SCREEN_UP: {
3431 // //if (hasSelection())
3432 // // cur.selection() = false;
3433 // col_type const col = tabular.cellColumn(cur.idx());
3434 // int const t = cur.bv().top_y() + cur.bv().height();
3436 // cur.bv().scrollDocView(t);
3440 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3444 // cur.par() = cur.lastpar();
3445 // cur.pos() = cur.lastpos();
3449 case LFUN_LAYOUT_TABULAR:
3450 cur.bv().showDialog("tabular", params2string(*this), this);
3453 case LFUN_INSET_DIALOG_UPDATE:
3454 cur.bv().updateDialog("tabular", params2string(*this));
3457 case LFUN_TABULAR_FEATURE:
3458 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3462 // insert file functions
3463 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3464 case LFUN_FILE_INSERT_PLAINTEXT: {
3466 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3467 FileName(to_utf8(cmd.argument())));
3470 cur.recordUndoInset(INSERT_UNDO);
3471 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3472 // content has been replaced,
3473 // so cursor might be invalid
3474 cur.pos() = cur.lastpos();
3475 cur.pit() = cur.lastpit();
3476 bvcur.setCursor(cur);
3483 if (cur.selIsMultiCell()) {
3484 if (copySelection(cur)) {
3485 cur.recordUndoInset(DELETE_UNDO);
3490 cell(cur.idx())->dispatch(cur, cmd);
3493 case LFUN_CHAR_DELETE_BACKWARD:
3494 case LFUN_CHAR_DELETE_FORWARD:
3495 if (cur.selIsMultiCell()) {
3496 cur.recordUndoInset(DELETE_UNDO);
3500 cell(cur.idx())->dispatch(cur, cmd);
3504 if (!cur.selection())
3506 if (cur.selIsMultiCell()) {
3510 cell(cur.idx())->dispatch(cur, cmd);
3513 case LFUN_CLIPBOARD_PASTE:
3514 case LFUN_PRIMARY_SELECTION_PASTE: {
3515 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3516 theClipboard().getAsText() :
3517 theSelection().get();
3520 // pass to InsertPlaintextString, but
3521 // only if we have multi-cell content
3522 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3523 cur.recordUndoInset(INSERT_UNDO);
3524 if (insertPlaintextString(cur.bv(), clip, false)) {
3525 // content has been replaced,
3526 // so cursor might be invalid
3527 cur.pos() = cur.lastpos();
3528 cur.pit() = cur.lastpit();
3529 bvcur.setCursor(cur);
3533 // Let the cell handle normal text
3534 cell(cur.idx())->dispatch(cur, cmd);
3539 if (!tabularStackDirty()) {
3540 cell(cur.idx())->dispatch(cur, cmd);
3543 if (theClipboard().isInternal() ||
3544 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3545 cur.recordUndoInset(INSERT_UNDO);
3546 pasteClipboard(cur);
3550 case LFUN_FONT_EMPH:
3551 case LFUN_FONT_BOLD:
3552 case LFUN_FONT_BOLDSYMBOL:
3553 case LFUN_FONT_ROMAN:
3554 case LFUN_FONT_NOUN:
3555 case LFUN_FONT_ITAL:
3556 case LFUN_FONT_FRAK:
3557 case LFUN_FONT_TYPEWRITER:
3558 case LFUN_FONT_SANS:
3559 case LFUN_TEXTSTYLE_APPLY:
3560 case LFUN_TEXTSTYLE_UPDATE:
3561 case LFUN_FONT_SIZE:
3562 case LFUN_FONT_UNDERLINE:
3564 case LFUN_WORD_CAPITALIZE:
3565 case LFUN_WORD_UPCASE:
3566 case LFUN_WORD_LOWCASE:
3567 case LFUN_CHARS_TRANSPOSE:
3568 if (cur.selIsMultiCell()) {
3571 getSelection(cur, rs, re, cs, ce);
3572 Cursor tmpcur = cur;
3573 for (row_type i = rs; i <= re; ++i) {
3574 for (col_type j = cs; j <= ce; ++j) {
3575 // cursor follows cell:
3576 tmpcur.idx() = tabular.cellIndex(i, j);
3577 // select this cell only:
3580 tmpcur.resetAnchor();
3581 tmpcur.pit() = tmpcur.lastpit();
3582 tmpcur.pos() = tmpcur.top().lastpos();
3583 tmpcur.setCursor(tmpcur);
3584 tmpcur.setSelection();
3585 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3590 cell(cur.idx())->dispatch(cur, cmd);
3594 // we try to handle this event in the insets dispatch function.
3595 cell(cur.idx())->dispatch(cur, cmd);
3601 // function sets an object as defined in func_status.h:
3602 // states OK, Unknown, Disabled, On, Off.
3603 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3604 FuncStatus & status) const
3606 switch (cmd.action) {
3607 case LFUN_TABULAR_FEATURE: {
3608 int action = Tabular::LAST_ACTION;
3610 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3611 string const tmp = tabularFeature[i].feature;
3612 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3613 action = tabularFeature[i].action;
3617 if (action == Tabular::LAST_ACTION) {
3619 status.unknown(true);
3623 string const argument
3624 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3626 row_type sel_row_start = 0;
3627 row_type sel_row_end = 0;
3628 col_type sel_col_start = 0;
3629 col_type sel_col_end = 0;
3630 Tabular::ltType dummyltt;
3633 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3636 case Tabular::SET_PWIDTH:
3637 case Tabular::SET_MPWIDTH:
3638 case Tabular::SET_SPECIAL_COLUMN:
3639 case Tabular::SET_SPECIAL_MULTI:
3640 case Tabular::APPEND_ROW:
3641 case Tabular::APPEND_COLUMN:
3642 case Tabular::DELETE_ROW:
3643 case Tabular::DELETE_COLUMN:
3644 case Tabular::COPY_ROW:
3645 case Tabular::COPY_COLUMN:
3646 case Tabular::SET_TOP_SPACE:
3647 case Tabular::SET_BOTTOM_SPACE:
3648 case Tabular::SET_INTERLINE_SPACE:
3652 case Tabular::MULTICOLUMN:
3653 status.setEnabled(sel_row_start == sel_row_end);
3654 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3657 case Tabular::SET_ALL_LINES:
3658 case Tabular::UNSET_ALL_LINES:
3659 case Tabular::SET_BORDER_LINES:
3660 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3663 case Tabular::TOGGLE_LINE_TOP:
3664 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3665 status.setOnOff(tabular.topLine(cur.idx()));
3668 case Tabular::TOGGLE_LINE_BOTTOM:
3669 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3670 status.setOnOff(tabular.bottomLine(cur.idx()));
3673 case Tabular::TOGGLE_LINE_LEFT:
3674 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3675 status.setOnOff(tabular.leftLine(cur.idx()));
3678 case Tabular::TOGGLE_LINE_RIGHT:
3679 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3680 status.setOnOff(tabular.rightLine(cur.idx()));
3683 case Tabular::M_ALIGN_LEFT:
3685 case Tabular::ALIGN_LEFT:
3686 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3689 case Tabular::M_ALIGN_RIGHT:
3691 case Tabular::ALIGN_RIGHT:
3692 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3695 case Tabular::M_ALIGN_CENTER:
3697 case Tabular::ALIGN_CENTER:
3698 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3701 case Tabular::ALIGN_BLOCK:
3702 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3703 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3706 case Tabular::M_VALIGN_TOP:
3708 case Tabular::VALIGN_TOP:
3710 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3713 case Tabular::M_VALIGN_BOTTOM:
3715 case Tabular::VALIGN_BOTTOM:
3717 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3720 case Tabular::M_VALIGN_MIDDLE:
3722 case Tabular::VALIGN_MIDDLE:
3724 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3727 case Tabular::SET_LONGTABULAR:
3728 status.setOnOff(tabular.is_long_tabular);
3731 case Tabular::UNSET_LONGTABULAR:
3732 status.setOnOff(!tabular.is_long_tabular);
3735 case Tabular::TOGGLE_ROTATE_TABULAR:
3736 case Tabular::SET_ROTATE_TABULAR:
3737 status.setOnOff(tabular.rotate);
3740 case Tabular::UNSET_ROTATE_TABULAR:
3741 status.setOnOff(!tabular.rotate);
3744 case Tabular::TOGGLE_ROTATE_CELL:
3745 case Tabular::SET_ROTATE_CELL:
3746 status.setOnOff(!oneCellHasRotationState(false,
3747 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3750 case Tabular::UNSET_ROTATE_CELL:
3751 status.setOnOff(!oneCellHasRotationState(true,
3752 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3755 case Tabular::SET_USEBOX:
3756 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3759 case Tabular::SET_LTFIRSTHEAD:
3760 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3763 case Tabular::UNSET_LTFIRSTHEAD:
3764 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3767 case Tabular::SET_LTHEAD:
3768 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3771 case Tabular::UNSET_LTHEAD:
3772 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3775 case Tabular::SET_LTFOOT:
3776 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3779 case Tabular::UNSET_LTFOOT:
3780 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3783 case Tabular::SET_LTLASTFOOT:
3784 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3787 case Tabular::UNSET_LTLASTFOOT:
3788 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3791 case Tabular::SET_LTNEWPAGE:
3792 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3795 case Tabular::TOGGLE_LTCAPTION:
3796 status.setEnabled(sel_row_start == sel_row_end);
3797 status.setOnOff(tabular.ltCaption(sel_row_start));
3800 case Tabular::SET_BOOKTABS:
3801 status.setOnOff(tabular.use_booktabs);
3804 case Tabular::UNSET_BOOKTABS:
3805 status.setOnOff(!tabular.use_booktabs);
3810 status.setEnabled(false);
3816 // These are only enabled inside tabular
3817 case LFUN_CELL_BACKWARD:
3818 case LFUN_CELL_FORWARD:
3819 status.setEnabled(true);
3822 // disable these with multiple cells selected
3823 case LFUN_INSET_INSERT:
3824 case LFUN_TABULAR_INSERT:
3825 case LFUN_FLEX_INSERT:
3826 case LFUN_FLOAT_INSERT:
3827 case LFUN_FLOAT_WIDE_INSERT:
3828 case LFUN_FOOTNOTE_INSERT:
3829 case LFUN_MARGINALNOTE_INSERT:
3830 case LFUN_MATH_INSERT:
3831 case LFUN_MATH_MODE:
3832 case LFUN_MATH_MUTATE:
3833 case LFUN_MATH_DISPLAY:
3834 case LFUN_NOTE_INSERT:
3835 case LFUN_OPTIONAL_INSERT:
3836 case LFUN_BOX_INSERT:
3837 case LFUN_BRANCH_INSERT:
3838 case LFUN_WRAP_INSERT:
3839 case LFUN_ERT_INSERT: {
3840 if (cur.selIsMultiCell()) {
3841 status.setEnabled(false);
3844 return cell(cur.idx())->getStatus(cur, cmd, status);
3847 // disable in non-fixed-width cells
3848 case LFUN_NEWLINE_INSERT:
3849 case LFUN_BREAK_PARAGRAPH: {
3850 if (tabular.getPWidth(cur.idx()).zero()) {
3851 status.setEnabled(false);
3854 return cell(cur.idx())->getStatus(cur, cmd, status);
3857 case LFUN_NEWPAGE_INSERT:
3858 status.setEnabled(false);
3862 if (cur.selIsMultiCell()) {
3863 status.setEnabled(false);
3864 status.message(_("You cannot paste into a multicell selection."));
3867 if (tabularStackDirty() && theClipboard().isInternal()) {
3868 status.setEnabled(true);
3871 return cell(cur.idx())->getStatus(cur, cmd, status);
3873 case LFUN_INSET_MODIFY:
3874 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3875 status.setEnabled(true);
3881 // we try to handle this event in the insets dispatch function.
3882 return cell(cur.idx())->getStatus(cur, cmd, status);
3887 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3889 return tabular.latex(os, runparams);
3893 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3895 os << '\n'; // output table on a new line
3896 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3897 tabular.plaintext(os, runparams, dp, false, 0);
3898 return PLAINTEXT_NEWLINE;
3902 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3907 // FIXME: Why not pass a proper DocIterator here?
3909 // if the table is inside a float it doesn't need the informaltable
3910 // wrapper. Search for it.
3911 for (master = owner(); master; master = master->owner())
3912 if (master->lyxCode() == FLOAT_CODE)
3917 os << "<informaltable>";
3920 ret += tabular.docbook(os, runparams);
3922 os << "</informaltable>";
3929 void InsetTabular::validate(LaTeXFeatures & features) const
3931 tabular.validate(features);
3935 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3937 return tabular.cellInset(idx);
3941 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3943 return tabular.cellInset(idx);
3947 void InsetTabular::cursorPos(BufferView const & bv,
3948 CursorSlice const & sl, bool boundary, int & x, int & y) const
3950 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3952 // y offset correction
3953 int const row = tabular.cellRow(sl.idx());
3954 for (int i = 0; i <= row; ++i) {
3956 y += tabular.rowAscent(i);
3957 y += tabular.getAdditionalHeight(i);
3960 y += tabular.rowDescent(i);
3963 // x offset correction
3964 int const col = tabular.cellColumn(sl.idx());
3965 int idx = tabular.cellIndex(row, 0);
3966 for (int j = 0; j < col; ++j) {
3967 if (tabular.isPartOfMultiColumn(row, j))
3969 x += tabular.columnWidth(idx);
3972 x += tabular.getBeginningOfTextInCell(idx);
3973 x += ADD_TO_TABULAR_WIDTH;
3978 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3982 Inset const & inset = *tabular.cellInset(cell);
3983 Point o = bv.coordCache().getInsets().xy(&inset);
3984 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3985 int const xend = xbeg + tabular.columnWidth(cell);
3986 row_type const row = tabular.cellRow(cell);
3987 int const ybeg = o.y_ - tabular.rowAscent(row) -
3988 tabular.getAdditionalHeight(row);
3989 int const yend = o.y_ + tabular.rowDescent(row);
4001 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4002 // << " ybeg=" << ybeg << " yend=" << yend
4003 // << " xx=" << xx << " yy=" << yy
4004 // << " dist=" << xx + yy << endl;
4009 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4011 //lyxerr << "InsetTabular::editXY: " << this << endl;
4012 cur.setSelection(false);
4014 cur.idx() = getNearestCell(cur.bv(), x, y);
4016 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4020 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4022 cur.idx() = getNearestCell(cur.bv(), x, y);
4023 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4027 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4029 idx_type idx_min = 0;
4030 int dist_min = numeric_limits<int>::max();
4031 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4032 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4033 int const d = dist(bv, i, x, y);
4044 int InsetTabular::cellXPos(idx_type const cell) const
4048 for (; !tabular.isFirstCellInRow(c); --c)
4051 for (; c < cell; ++c)
4052 lx += tabular.columnWidth(c);
4058 void InsetTabular::resetPos(Cursor & cur) const
4060 BufferView & bv = cur.bv();
4061 int const maxwidth = bv.workWidth();
4063 int const scx_old = scx_;
4064 int const i = cur.find(this);
4069 int const X2 = maxwidth;
4070 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4071 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4072 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4075 scx_ = X1 + 20 - x1;
4077 scx_ = X2 - 20 - x2;
4082 // only update if offset changed
4083 if (scx_ != scx_old)
4084 cur.updateFlags(Update::Force | Update::FitCursor);
4088 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4090 if (isRightToLeft(cur)) {
4091 if (tabular.isFirstCellInRow(cur.idx())) {
4092 row_type const row = tabular.cellRow(cur.idx());
4093 if (row == tabular.row_info.size() - 1)
4095 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4102 if (tabular.isLastCell(cur.idx()))
4108 cur.boundary(false);
4110 // in visual mode, place cursor at extreme left or right
4112 switch(entry_from) {
4114 case ENTRY_DIRECTION_RIGHT:
4115 cur.posVisToRowExtremity(false /* !left */);
4117 case ENTRY_DIRECTION_LEFT:
4118 cur.posVisToRowExtremity(true /* left */);
4120 case ENTRY_DIRECTION_IGNORE:
4121 // nothing to do in this case
4130 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4132 if (isRightToLeft(cur)) {
4133 if (tabular.isLastCellInRow(cur.idx())) {
4134 row_type const row = tabular.cellRow(cur.idx());
4137 cur.idx() = tabular.getFirstCellInRow(row);
4138 cur.idx() = tabular.cellAbove(cur.idx());
4140 if (tabular.isLastCell(cur.idx()))
4145 if (cur.idx() == 0) // first cell
4149 cur.pit() = cur.lastpit();
4150 cur.pos() = cur.lastpos();
4152 // in visual mode, place cursor at extreme left or right
4154 switch(entry_from) {
4156 case ENTRY_DIRECTION_RIGHT:
4157 cur.posVisToRowExtremity(false /* !left */);
4159 case ENTRY_DIRECTION_LEFT:
4160 cur.posVisToRowExtremity(true /* left */);
4162 case ENTRY_DIRECTION_IGNORE:
4163 // nothing to do in this case
4172 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4174 Tabular::Feature action = Tabular::LAST_ACTION;
4177 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4178 string const tmp = tabularFeature[i].feature;
4180 if (tmp == what.substr(0, tmp.length())) {
4181 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4182 //tabularFeatures[i].feature.length()))
4183 action = tabularFeature[i].action;
4187 if (action == Tabular::LAST_ACTION)
4191 ltrim(what.substr(tabularFeature[i].feature.length()));
4192 tabularFeatures(cur, action, val);
4197 static void checkLongtableSpecial(Tabular::ltType & ltt,
4198 string const & special, bool & flag)
4200 if (special == "dl_above") {
4203 } else if (special == "dl_below") {
4204 ltt.bottomDL = flag;
4206 } else if (special == "empty") {
4215 bool InsetTabular::oneCellHasRotationState(bool rotated,
4216 row_type row_start, row_type row_end,
4217 col_type col_start, col_type col_end) const {
4219 for (row_type i = row_start; i <= row_end; ++i) {
4220 for (col_type j = col_start; j <= col_end; ++j) {
4221 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4230 void InsetTabular::tabularFeatures(Cursor & cur,
4231 Tabular::Feature feature, string const & value)
4233 col_type sel_col_start;
4234 col_type sel_col_end;
4235 row_type sel_row_start;
4236 row_type sel_row_end;
4237 bool setLines = false;
4238 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4239 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4243 case Tabular::M_ALIGN_LEFT:
4244 case Tabular::ALIGN_LEFT:
4245 setAlign = LYX_ALIGN_LEFT;
4248 case Tabular::M_ALIGN_RIGHT:
4249 case Tabular::ALIGN_RIGHT:
4250 setAlign = LYX_ALIGN_RIGHT;
4253 case Tabular::M_ALIGN_CENTER:
4254 case Tabular::ALIGN_CENTER:
4255 setAlign = LYX_ALIGN_CENTER;
4258 case Tabular::ALIGN_BLOCK:
4259 setAlign = LYX_ALIGN_BLOCK;
4262 case Tabular::M_VALIGN_TOP:
4263 case Tabular::VALIGN_TOP:
4264 setVAlign = Tabular::LYX_VALIGN_TOP;
4267 case Tabular::M_VALIGN_BOTTOM:
4268 case Tabular::VALIGN_BOTTOM:
4269 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4272 case Tabular::M_VALIGN_MIDDLE:
4273 case Tabular::VALIGN_MIDDLE:
4274 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4281 cur.recordUndoInset(ATOMIC_UNDO);
4283 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4284 row_type const row = tabular.cellRow(cur.idx());
4285 col_type const column = tabular.cellColumn(cur.idx());
4287 Tabular::ltType ltt;
4291 case Tabular::SET_PWIDTH: {
4292 Length const len(value);
4293 tabular.setColumnPWidth(cur, cur.idx(), len);
4295 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4296 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4300 case Tabular::SET_MPWIDTH:
4301 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4304 case Tabular::SET_SPECIAL_COLUMN:
4305 case Tabular::SET_SPECIAL_MULTI:
4306 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4309 case Tabular::APPEND_ROW:
4310 // append the row into the tabular
4311 tabular.appendRow(cur.idx());
4314 case Tabular::APPEND_COLUMN:
4315 // append the column into the tabular
4316 tabular.appendColumn(cur.idx());
4317 cur.idx() = tabular.cellIndex(row, column);
4320 case Tabular::DELETE_ROW:
4321 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4322 tabular.deleteRow(sel_row_start);
4323 if (sel_row_start >= tabular.row_info.size())
4325 cur.idx() = tabular.cellIndex(sel_row_start, column);
4328 cur.setSelection(false);
4331 case Tabular::DELETE_COLUMN:
4332 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4333 tabular.deleteColumn(sel_col_start);
4334 if (sel_col_start >= tabular.column_info.size())
4336 cur.idx() = tabular.cellIndex(row, sel_col_start);
4339 cur.setSelection(false);
4342 case Tabular::COPY_ROW:
4343 tabular.copyRow(row);
4346 case Tabular::COPY_COLUMN:
4347 tabular.copyColumn(column);
4348 cur.idx() = tabular.cellIndex(row, column);
4351 case Tabular::TOGGLE_LINE_TOP: {
4352 bool lineSet = !tabular.topLine(cur.idx());
4353 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4354 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4355 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4359 case Tabular::TOGGLE_LINE_BOTTOM: {
4360 bool lineSet = !tabular.bottomLine(cur.idx());
4361 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4362 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4363 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4367 case Tabular::TOGGLE_LINE_LEFT: {
4368 bool lineSet = !tabular.leftLine(cur.idx());
4369 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4370 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4371 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4375 case Tabular::TOGGLE_LINE_RIGHT: {
4376 bool lineSet = !tabular.rightLine(cur.idx());
4377 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4378 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4379 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4383 case Tabular::M_ALIGN_LEFT:
4384 case Tabular::M_ALIGN_RIGHT:
4385 case Tabular::M_ALIGN_CENTER:
4387 case Tabular::ALIGN_LEFT:
4388 case Tabular::ALIGN_RIGHT:
4389 case Tabular::ALIGN_CENTER:
4390 case Tabular::ALIGN_BLOCK:
4391 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4392 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4393 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4396 case Tabular::M_VALIGN_TOP:
4397 case Tabular::M_VALIGN_BOTTOM:
4398 case Tabular::M_VALIGN_MIDDLE:
4400 case Tabular::VALIGN_TOP:
4401 case Tabular::VALIGN_BOTTOM:
4402 case Tabular::VALIGN_MIDDLE:
4403 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4404 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4405 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4408 case Tabular::MULTICOLUMN: {
4409 if (!cur.selection()) {
4410 // just multicol for one single cell
4411 // check whether we are completely in a multicol
4412 if (tabular.isMultiColumn(cur.idx()))
4413 tabular.unsetMultiColumn(cur.idx());
4415 tabular.setMultiColumn(cur.idx(), 1);
4418 // we have a selection so this means we just add all this
4419 // cells to form a multicolumn cell
4420 idx_type const s_start = cur.selBegin().idx();
4421 idx_type const s_end = cur.selEnd().idx();
4422 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4423 cur.idx() = s_start;
4426 cur.setSelection(false);
4430 case Tabular::SET_ALL_LINES:
4432 case Tabular::UNSET_ALL_LINES:
4433 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4434 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4435 tabular.setAllLines(
4436 tabular.cellIndex(i,j), setLines);
4439 case Tabular::SET_BORDER_LINES:
4440 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4441 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4442 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4444 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4445 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4446 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4450 case Tabular::SET_LONGTABULAR:
4451 tabular.is_long_tabular = true;
4454 case Tabular::UNSET_LONGTABULAR:
4455 tabular.is_long_tabular = false;
4458 case Tabular::SET_ROTATE_TABULAR:
4459 tabular.rotate = true;
4462 case Tabular::UNSET_ROTATE_TABULAR:
4463 tabular.rotate = false;
4466 case Tabular::TOGGLE_ROTATE_TABULAR:
4467 tabular.rotate = !tabular.rotate;
4470 case Tabular::SET_ROTATE_CELL:
4471 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4472 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4473 tabular.setRotateCell(
4474 tabular.cellIndex(i, j), true);
4477 case Tabular::UNSET_ROTATE_CELL:
4478 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4479 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4480 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4483 case Tabular::TOGGLE_ROTATE_CELL:
4485 bool oneNotRotated = oneCellHasRotationState(false,
4486 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4488 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4489 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4490 tabular.setRotateCell(tabular.cellIndex(i, j),
4495 case Tabular::SET_USEBOX: {
4496 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4497 if (val == tabular.getUsebox(cur.idx()))
4498 val = Tabular::BOX_NONE;
4499 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4500 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4501 tabular.setUsebox(tabular.cellIndex(i, j), val);
4505 case Tabular::UNSET_LTFIRSTHEAD:
4507 case Tabular::SET_LTFIRSTHEAD:
4508 tabular.getRowOfLTFirstHead(row, ltt);
4509 checkLongtableSpecial(ltt, value, flag);
4510 tabular.setLTHead(row, flag, ltt, true);
4513 case Tabular::UNSET_LTHEAD:
4515 case Tabular::SET_LTHEAD:
4516 tabular.getRowOfLTHead(row, ltt);
4517 checkLongtableSpecial(ltt, value, flag);
4518 tabular.setLTHead(row, flag, ltt, false);
4521 case Tabular::UNSET_LTFOOT:
4523 case Tabular::SET_LTFOOT:
4524 tabular.getRowOfLTFoot(row, ltt);
4525 checkLongtableSpecial(ltt, value, flag);
4526 tabular.setLTFoot(row, flag, ltt, false);
4529 case Tabular::UNSET_LTLASTFOOT:
4531 case Tabular::SET_LTLASTFOOT:
4532 tabular.getRowOfLTLastFoot(row, ltt);
4533 checkLongtableSpecial(ltt, value, flag);
4534 tabular.setLTFoot(row, flag, ltt, true);
4537 case Tabular::SET_LTNEWPAGE:
4538 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4541 case Tabular::TOGGLE_LTCAPTION:
4542 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4545 cur.setSelection(false);
4548 case Tabular::SET_BOOKTABS:
4549 tabular.use_booktabs = true;
4552 case Tabular::UNSET_BOOKTABS:
4553 tabular.use_booktabs = false;
4556 case Tabular::SET_TOP_SPACE: {
4558 if (value == "default")
4559 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4560 tabular.row_info[i].top_space_default = true;
4561 else if (isValidLength(value, &len))
4562 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4563 tabular.row_info[i].top_space_default = false;
4564 tabular.row_info[i].top_space = len;
4567 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4568 tabular.row_info[i].top_space_default = false;
4569 tabular.row_info[i].top_space = len;
4574 case Tabular::SET_BOTTOM_SPACE: {
4576 if (value == "default")
4577 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4578 tabular.row_info[i].bottom_space_default = true;
4579 else if (isValidLength(value, &len))
4580 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4581 tabular.row_info[i].bottom_space_default = false;
4582 tabular.row_info[i].bottom_space = len;
4585 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4586 tabular.row_info[i].bottom_space_default = false;
4587 tabular.row_info[i].bottom_space = len;
4592 case Tabular::SET_INTERLINE_SPACE: {
4594 if (value == "default")
4595 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4596 tabular.row_info[i].interline_space_default = true;
4597 else if (isValidLength(value, &len))
4598 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4599 tabular.row_info[i].interline_space_default = false;
4600 tabular.row_info[i].interline_space = len;
4603 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4604 tabular.row_info[i].interline_space_default = false;
4605 tabular.row_info[i].interline_space = len;
4610 // dummy stuff just to avoid warnings
4611 case Tabular::LAST_ACTION:
4617 bool InsetTabular::showInsetDialog(BufferView * bv) const
4619 bv->showDialog("tabular", params2string(*this),
4620 const_cast<InsetTabular *>(this));
4625 void InsetTabular::openLayoutDialog(BufferView * bv) const
4627 bv->showDialog("tabular", params2string(*this),
4628 const_cast<InsetTabular *>(this));
4632 bool InsetTabular::copySelection(Cursor & cur)
4634 if (!cur.selection())
4639 getSelection(cur, rs, re, cs, ce);
4641 paste_tabular.reset(new Tabular(tabular));
4643 for (row_type i = 0; i < rs; ++i)
4644 paste_tabular->deleteRow(0);
4646 row_type const rows = re - rs + 1;
4647 while (paste_tabular->row_info.size() > rows)
4648 paste_tabular->deleteRow(rows);
4650 for (col_type i = 0; i < cs; ++i)
4651 paste_tabular->deleteColumn(0);
4653 col_type const columns = ce - cs + 1;
4654 while (paste_tabular->column_info.size() > columns)
4655 paste_tabular->deleteColumn(columns);
4657 paste_tabular->setBuffer(tabular.buffer());
4659 odocstringstream os;
4660 OutputParams const runparams(0);
4661 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4662 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4663 cap::copySelection(cur, os.str());
4665 // mark tabular stack dirty
4666 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4667 // when we (hopefully) have a one-for-all paste mechanism.
4668 // This must be called after cap::copySelection.
4669 dirtyTabularStack(true);
4675 bool InsetTabular::pasteClipboard(Cursor & cur)
4679 col_type const actcol = tabular.cellColumn(cur.idx());
4680 row_type const actrow = tabular.cellRow(cur.idx());
4681 for (row_type r1 = 0, r2 = actrow;
4682 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4684 for (col_type c1 = 0, c2 = actcol;
4685 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4687 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4688 tabular.isPartOfMultiColumn(r2, c2))
4690 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4694 if (tabular.isPartOfMultiColumn(r2, c2)) {
4698 shared_ptr<InsetTableCell> inset(
4699 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4700 tabular.setCellInset(r2, c2, inset);
4701 // FIXME: change tracking (MG)
4702 inset->setChange(Change(cur.buffer().params().trackChanges ?
4703 Change::INSERTED : Change::UNCHANGED));
4711 void InsetTabular::cutSelection(Cursor & cur)
4713 if (!cur.selection())
4718 getSelection(cur, rs, re, cs, ce);
4719 for (row_type i = rs; i <= re; ++i) {
4720 for (col_type j = cs; j <= ce; ++j) {
4721 shared_ptr<InsetTableCell> t
4722 = cell(tabular.cellIndex(i, j));
4723 if (cur.buffer().params().trackChanges)
4724 // FIXME: Change tracking (MG)
4725 t->setChange(Change(Change::DELETED));
4731 // cursor position might be invalid now
4732 if (cur.pit() > cur.lastpit())
4733 cur.pit() = cur.lastpit();
4734 if (cur.pos() > cur.lastpos())
4735 cur.pos() = cur.lastpos();
4736 cur.clearSelection();
4740 bool InsetTabular::isRightToLeft(Cursor & cur) const
4742 LASSERT(cur.depth() > 1, /**/);
4743 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4744 pos_type const parentpos = cur[cur.depth() - 2].pos();
4745 return parentpar.getFontSettings(cur.bv().buffer().params(),
4746 parentpos).language()->rightToLeft();
4749 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
4752 LASSERT(stidx <= enidx, return docstring());
4754 col_type const col1 = tabular.cellColumn(stidx);
4755 col_type const col2 = tabular.cellColumn(enidx);
4756 row_type const row1 = tabular.cellRow(stidx);
4757 row_type const row2 = tabular.cellRow(enidx);
4759 for (col_type col = col1; col <= col2; col++)
4760 for (row_type row = row1; row <= row2; row++) {
4765 retval += tabular.cellInset(row, col)->asString(intoInsets);
4771 void InsetTabular::getSelection(Cursor & cur,
4772 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4774 CursorSlice const & beg = cur.selBegin();
4775 CursorSlice const & end = cur.selEnd();
4776 cs = tabular.cellColumn(beg.idx());
4777 ce = tabular.cellColumn(end.idx());
4780 cs = tabular.cellColumn(end.idx());
4782 ce = tabular.cellRightColumn(end.idx());
4785 rs = tabular.cellRow(beg.idx());
4786 re = tabular.cellRow(end.idx());
4792 Text * InsetTabular::getText(int idx) const
4794 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4798 void InsetTabular::setChange(Change const & change)
4800 for (idx_type idx = 0; idx < nargs(); ++idx)
4801 cell(idx)->setChange(change);
4805 void InsetTabular::acceptChanges(BufferParams const & bparams)
4807 for (idx_type idx = 0; idx < nargs(); ++idx)
4808 cell(idx)->acceptChanges(bparams);
4812 void InsetTabular::rejectChanges(BufferParams const & bparams)
4814 for (idx_type idx = 0; idx < nargs(); ++idx)
4815 cell(idx)->rejectChanges(bparams);
4819 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4821 return tabular.getPWidth(cell).zero();
4825 bool InsetTabular::forcePlainLayout(idx_type cell) const
4827 return !tabular.getPWidth(cell).zero();
4831 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4834 if (buf.length() <= 0)
4839 col_type maxCols = 1;
4840 size_t const len = buf.length();
4844 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4852 maxCols = max(cols, maxCols);
4858 maxCols = max(cols, maxCols);
4864 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4865 loctab = paste_tabular.get();
4867 dirtyTabularStack(true);
4870 cell = bv.cursor().idx();
4871 ocol = tabular.cellColumn(cell);
4872 row = tabular.cellRow(cell);
4876 idx_type const cells = loctab->numberofcells;
4879 rows = loctab->row_info.size();
4880 col_type const columns = loctab->column_info.size();
4882 while (cell < cells && p < len && row < rows &&
4883 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4889 // we can only set this if we are not too far right
4890 if (cols < columns) {
4891 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4892 Font const font = bv.textMetrics(&inset->text()).
4894 inset->setText(buf.substr(op, p - op), font,
4895 buffer().params().trackChanges);
4901 // we can only set this if we are not too far right
4902 if (cols < columns) {
4903 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4904 Font const font = bv.textMetrics(&inset->text()).
4906 inset->setText(buf.substr(op, p - op), font,
4907 buffer().params().trackChanges);
4912 cell = loctab->cellIndex(row, cols);
4918 // check for the last cell if there is no trailing '\n'
4919 if (cell < cells && op < len) {
4920 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4921 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
4922 inset->setText(buf.substr(op, len - op), font,
4923 buffer().params().trackChanges);
4929 void InsetTabular::addPreview(PreviewLoader & loader) const
4931 row_type const rows = tabular.row_info.size();
4932 col_type const columns = tabular.column_info.size();
4933 for (row_type i = 0; i < rows; ++i) {
4934 for (col_type j = 0; j < columns; ++j)
4935 tabular.cellInset(i, j)->addPreview(loader);
4940 bool InsetTabular::completionSupported(Cursor const & cur) const
4942 Cursor const & bvCur = cur.bv().cursor();
4943 if (&bvCur.inset() != this)
4945 return cur.text()->completionSupported(cur);
4949 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4951 return completionSupported(cur);
4955 bool InsetTabular::automaticInlineCompletion() const
4957 return lyxrc.completion_inline_text;
4961 bool InsetTabular::automaticPopupCompletion() const
4963 return lyxrc.completion_popup_text;
4967 bool InsetTabular::showCompletionCursor() const
4969 return lyxrc.completion_cursor_text;
4973 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4975 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4979 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4981 if (!completionSupported(cur))
4983 return cur.text()->completionPrefix(cur);
4987 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4989 if (!completionSupported(cur))
4992 return cur.text()->insertCompletion(cur, s, finished);
4996 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4997 Dimension & dim) const
4999 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5000 tm.completionPosAndDim(cur, x, y, dim);
5004 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5006 istringstream data(in);
5008 lex.setStream(data);
5015 if (!lex || token != "tabular") {
5016 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5020 // This is part of the inset proper that is usually swallowed
5021 // by Buffer::readInset
5023 if (!lex || token != "Tabular") {
5024 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5032 string InsetTabular::params2string(InsetTabular const & inset)
5035 data << "tabular" << ' ';
5037 data << "\\end_inset\n";