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, string const lang) 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 //babel makes the "-" character an active one, so we have to suppress this here
1843 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1844 if (lang == "slovak" || lang == "czech")
1845 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1846 "\\expandafter\\cline\\expandafter{\\expandafter")
1847 << c + 1 << "\\string-";
1849 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1850 // get to last column of line span
1851 while (c < ncols && topline[c])
1862 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1864 // we output bottomlines of row r and the toplines of row r+1
1865 // if the latter do not span the whole tabular
1867 // get the bottomlines of row r, and toplines in next row
1868 bool lastrow = row == row_info.size() - 1;
1869 col_type const ncols = column_info.size();
1870 vector<bool> bottomline, topline;
1871 bool nextrowset = true;
1872 for (col_type c = 0; c < ncols; ++c) {
1873 bottomline.push_back(bottomLine(cellIndex(row, c)));
1874 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1875 nextrowset &= topline[c];
1878 // combine this row's bottom lines and next row's toplines if necessary
1880 for (col_type c = 0; c < ncols; ++c) {
1882 bottomline[c] = bottomline[c] || topline[c];
1887 // do nothing if empty, OR incomplete row line with a topline in next row
1888 if (nset == 0 || (nextrowset && nset != ncols))
1891 if (nset == ncols) {
1893 os << (lastrow ? "\\bottomrule" : "\\midrule");
1897 for (col_type c = 0; c < ncols; ++c) {
1898 if (bottomline[c]) {
1899 //babel makes the "-" character an active one, so we have to suppress this here
1900 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1901 if (lang == "slovak" || lang == "czech")
1902 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1903 "\\expandafter\\cline\\expandafter{\\expandafter")
1904 << c + 1 << "\\string-";
1906 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1907 // get to last column of line span
1908 while (c < ncols && bottomline[c])
1919 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1922 row_type const r = cellRow(cell);
1923 if (is_long_tabular && row_info[r].caption)
1926 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1927 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1928 // figure out how to set the lines
1929 // we always set double lines to the right of the cell
1930 col_type const c = cellColumn(cell);
1931 col_type const nextcol = c + columnSpan(cell);
1932 bool colright = columnRightLine(c);
1933 bool colleft = columnLeftLine(c);
1934 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1935 bool nextcellleft = nextcol < column_info.size()
1936 && leftLine(cellIndex(r, nextcol));
1937 bool coldouble = colright && nextcolleft;
1938 bool celldouble = rightLine(cell) && nextcellleft;
1939 ismulticol = isMultiColumn(cell)
1940 || (c == 0 && colleft != leftLine(cell))
1941 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1942 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1943 || (coldouble != celldouble);
1945 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1946 if (c ==0 && leftLine(cell))
1948 if (!cellInfo(cell).align_special.empty()) {
1949 os << cellInfo(cell).align_special;
1951 if (!getPWidth(cell).zero()) {
1953 case LYX_ALIGN_LEFT:
1954 os << ">{\\raggedright}";
1956 case LYX_ALIGN_RIGHT:
1957 os << ">{\\raggedleft}";
1959 case LYX_ALIGN_CENTER:
1960 os << ">{\\centering}";
1966 case LYX_VALIGN_TOP:
1969 case LYX_VALIGN_MIDDLE:
1972 case LYX_VALIGN_BOTTOM:
1977 << from_ascii(getPWidth(cell).asLatexString())
1981 case LYX_ALIGN_LEFT:
1984 case LYX_ALIGN_RIGHT:
1991 } // end if else !getPWidth
1992 } // end if else !cellinfo_of_cell
1993 if (rightLine(cell) || nextcellleft)
1996 // add extra vertical line if we want a double one
2000 if (getRotateCell(cell)) {
2001 os << "\\begin{sideways}\n";
2004 if (getUsebox(cell) == BOX_PARBOX) {
2007 case LYX_VALIGN_TOP:
2010 case LYX_VALIGN_MIDDLE:
2013 case LYX_VALIGN_BOTTOM:
2017 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2019 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2020 os << "\\begin{minipage}[";
2022 case LYX_VALIGN_TOP:
2025 case LYX_VALIGN_MIDDLE:
2028 case LYX_VALIGN_BOTTOM:
2032 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2040 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2043 row_type const r = cellRow(cell);
2044 if (is_long_tabular && row_info[r].caption)
2048 if (getUsebox(cell) == BOX_PARBOX)
2050 else if (getUsebox(cell) == BOX_MINIPAGE) {
2051 os << "%\n\\end{minipage}";
2054 if (getRotateCell(cell)) {
2055 os << "%\n\\end{sideways}";
2065 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2066 OutputParams const & runparams) const
2068 if (!is_long_tabular)
2072 // output header info
2074 if (endhead.topDL) {
2078 for (row_type i = 0; i < row_info.size(); ++i) {
2079 if (row_info[i].endhead) {
2080 ret += TeXRow(os, i, runparams);
2083 if (endhead.bottomDL) {
2087 os << "\\endhead\n";
2089 if (endfirsthead.empty) {
2090 os << "\\endfirsthead\n";
2094 // output firstheader info
2095 if (haveLTFirstHead()) {
2096 if (endfirsthead.topDL) {
2100 for (row_type i = 0; i < row_info.size(); ++i) {
2101 if (row_info[i].endfirsthead) {
2102 ret += TeXRow(os, i, runparams);
2105 if (endfirsthead.bottomDL) {
2109 os << "\\endfirsthead\n";
2112 // output footer info
2114 if (endfoot.topDL) {
2118 for (row_type i = 0; i < row_info.size(); ++i) {
2119 if (row_info[i].endfoot) {
2120 ret += TeXRow(os, i, runparams);
2123 if (endfoot.bottomDL) {
2127 os << "\\endfoot\n";
2129 if (endlastfoot.empty) {
2130 os << "\\endlastfoot\n";
2134 // output lastfooter info
2135 if (haveLTLastFoot()) {
2136 if (endlastfoot.topDL) {
2140 for (row_type i = 0; i < row_info.size(); ++i) {
2141 if (row_info[i].endlastfoot) {
2142 ret += TeXRow(os, i, runparams);
2145 if (endlastfoot.bottomDL) {
2149 os << "\\endlastfoot\n";
2156 bool Tabular::isValidRow(row_type row) const
2158 if (!is_long_tabular)
2160 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2161 !row_info[row].endfoot && !row_info[row].endlastfoot;
2165 int Tabular::TeXRow(odocstream & os, row_type i,
2166 OutputParams const & runparams) const
2168 idx_type cell = cellIndex(i, 0);
2169 shared_ptr<InsetTableCell> inset = cellInset(cell);
2170 Paragraph const & par = inset->paragraphs().front();
2171 string const lang = par.getParLanguage(buffer().params())->lang();
2173 //output the top line
2174 int ret = TeXTopHLine(os, i, lang);
2176 if (row_info[i].top_space_default) {
2178 os << "\\addlinespace\n";
2180 os << "\\noalign{\\vskip\\doublerulesep}\n";
2182 } else if(!row_info[i].top_space.zero()) {
2184 os << "\\addlinespace["
2185 << from_ascii(row_info[i].top_space.asLatexString())
2188 os << "\\noalign{\\vskip"
2189 << from_ascii(row_info[i].top_space.asLatexString())
2194 bool ismulticol = false;
2195 for (col_type j = 0; j < column_info.size(); ++j) {
2196 if (isPartOfMultiColumn(i, j))
2198 ret += TeXCellPreamble(os, cell, ismulticol);
2199 shared_ptr<InsetTableCell> inset = cellInset(cell);
2201 Paragraph const & par = inset->paragraphs().front();
2202 bool rtl = par.isRTL(buffer().params())
2204 && getPWidth(cell).zero();
2208 par.getParLanguage(buffer().params())->lang();
2209 if (lang == "farsi")
2211 else if (lang == "arabic_arabi")
2213 // currently, remaning RTL languages are
2214 // arabic_arabtex and hebrew
2218 // pass to the OutputParams that we are in a cell and
2219 // which alignment we have set.
2220 // InsetNewline needs this context information.
2221 OutputParams newrp(runparams);
2222 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2223 ? OutputParams::PLAIN
2224 : OutputParams::ALIGNED;
2225 ret += inset->latex(os, newrp);
2226 runparams.encoding = newrp.encoding;
2230 ret += TeXCellPostamble(os, cell, ismulticol);
2231 if (!isLastCellInRow(cell)) { // not last cell in row
2236 os << "\\tabularnewline";
2237 if (row_info[i].bottom_space_default) {
2239 os << "\\addlinespace";
2241 os << "[\\doublerulesep]";
2242 } else if (!row_info[i].bottom_space.zero()) {
2244 os << "\\addlinespace";
2246 << from_ascii(row_info[i].bottom_space.asLatexString())
2252 //output the bottom line
2253 ret += TeXBottomHLine(os, i, lang);
2255 if (row_info[i].interline_space_default) {
2257 os << "\\addlinespace\n";
2259 os << "\\noalign{\\vskip\\doublerulesep}\n";
2261 } else if (!row_info[i].interline_space.zero()) {
2263 os << "\\addlinespace["
2264 << from_ascii(row_info[i].interline_space.asLatexString())
2267 os << "\\noalign{\\vskip"
2268 << from_ascii(row_info[i].interline_space.asLatexString())
2276 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2280 //+---------------------------------------------------------------------
2281 //+ first the opening preamble +
2282 //+---------------------------------------------------------------------
2285 os << "\\begin{sideways}\n";
2288 if (is_long_tabular)
2289 os << "\\begin{longtable}{";
2291 os << "\\begin{tabular}{";
2293 for (col_type i = 0; i < column_info.size(); ++i) {
2294 if (columnLeftLine(i))
2296 if (!column_info[i].align_special.empty()) {
2297 os << column_info[i].align_special;
2299 if (!column_info[i].p_width.zero()) {
2300 switch (column_info[i].alignment) {
2301 case LYX_ALIGN_LEFT:
2302 os << ">{\\raggedright}";
2304 case LYX_ALIGN_RIGHT:
2305 os << ">{\\raggedleft}";
2307 case LYX_ALIGN_CENTER:
2308 os << ">{\\centering}";
2310 case LYX_ALIGN_NONE:
2311 case LYX_ALIGN_BLOCK:
2312 case LYX_ALIGN_LAYOUT:
2313 case LYX_ALIGN_SPECIAL:
2317 switch (column_info[i].valignment) {
2318 case LYX_VALIGN_TOP:
2321 case LYX_VALIGN_MIDDLE:
2324 case LYX_VALIGN_BOTTOM:
2329 << from_ascii(column_info[i].p_width.asLatexString())
2332 switch (column_info[i].alignment) {
2333 case LYX_ALIGN_LEFT:
2336 case LYX_ALIGN_RIGHT:
2343 } // end if else !column_info[i].p_width
2344 } // end if else !column_info[i].align_special
2345 if (columnRightLine(i))
2351 ret += TeXLongtableHeaderFooter(os, runparams);
2353 //+---------------------------------------------------------------------
2354 //+ the single row and columns (cells) +
2355 //+---------------------------------------------------------------------
2357 for (row_type i = 0; i < row_info.size(); ++i) {
2358 if (isValidRow(i)) {
2359 ret += TeXRow(os, i, runparams);
2360 if (is_long_tabular && row_info[i].newpage) {
2361 os << "\\newpage\n";
2367 //+---------------------------------------------------------------------
2368 //+ the closing of the tabular +
2369 //+---------------------------------------------------------------------
2371 if (is_long_tabular)
2372 os << "\\end{longtable}";
2374 os << "\\end{tabular}";
2376 os << "\n\\end{sideways}";
2384 int Tabular::docbookRow(odocstream & os, row_type row,
2385 OutputParams const & runparams) const
2388 idx_type cell = getFirstCellInRow(row);
2391 for (col_type j = 0; j < column_info.size(); ++j) {
2392 if (isPartOfMultiColumn(row, j))
2395 os << "<entry align=\"";
2396 switch (getAlignment(cell)) {
2397 case LYX_ALIGN_LEFT:
2400 case LYX_ALIGN_RIGHT:
2408 os << "\" valign=\"";
2409 switch (getVAlignment(cell)) {
2410 case LYX_VALIGN_TOP:
2413 case LYX_VALIGN_BOTTOM:
2416 case LYX_VALIGN_MIDDLE:
2421 if (isMultiColumn(cell)) {
2422 os << " namest=\"col" << j << "\" ";
2423 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2427 ret += cellInset(cell)->docbook(os, runparams);
2436 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2440 //+---------------------------------------------------------------------
2441 //+ first the opening preamble +
2442 //+---------------------------------------------------------------------
2444 os << "<tgroup cols=\"" << column_info.size()
2445 << "\" colsep=\"1\" rowsep=\"1\">\n";
2447 for (col_type i = 0; i < column_info.size(); ++i) {
2448 os << "<colspec colname=\"col" << i << "\" align=\"";
2449 switch (column_info[i].alignment) {
2450 case LYX_ALIGN_LEFT:
2453 case LYX_ALIGN_RIGHT:
2461 if (runparams.flavor == OutputParams::XML)
2467 //+---------------------------------------------------------------------
2468 //+ Long Tabular case +
2469 //+---------------------------------------------------------------------
2471 // output header info
2472 if (haveLTHead() || haveLTFirstHead()) {
2475 for (row_type i = 0; i < row_info.size(); ++i) {
2476 if (row_info[i].endhead || row_info[i].endfirsthead) {
2477 ret += docbookRow(os, i, runparams);
2483 // output footer info
2484 if (haveLTFoot() || haveLTLastFoot()) {
2487 for (row_type i = 0; i < row_info.size(); ++i) {
2488 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2489 ret += docbookRow(os, i, runparams);
2496 //+---------------------------------------------------------------------
2497 //+ the single row and columns (cells) +
2498 //+---------------------------------------------------------------------
2502 for (row_type i = 0; i < row_info.size(); ++i) {
2503 if (isValidRow(i)) {
2504 ret += docbookRow(os, i, runparams);
2509 //+---------------------------------------------------------------------
2510 //+ the closing of the tabular +
2511 //+---------------------------------------------------------------------
2520 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2521 vector<unsigned int> const & clen) const
2523 idx_type const fcell = getFirstCellInRow(row);
2524 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2527 for (idx_type i = fcell; i < n; ++i) {
2537 for (idx_type i = fcell; i < n; ++i) {
2548 col_type column = cellColumn(i);
2549 int len = clen[column];
2550 while (column < column_info.size() - 1
2551 && isPartOfMultiColumn(row, ++column))
2552 len += clen[column] + 4;
2553 os << docstring(len, ch);
2568 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2569 vector<unsigned int> const & clen) const
2571 idx_type const fcell = getFirstCellInRow(row);
2572 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2575 for (idx_type i = fcell; i < n; ++i) {
2576 if (bottomLine(i)) {
2585 for (idx_type i = fcell; i < n; ++i) {
2586 if (bottomLine(i)) {
2596 col_type column = cellColumn(i);
2597 int len = clen[column];
2598 while (column < column_info.size() -1
2599 && isPartOfMultiColumn(row, ++column))
2600 len += clen[column] + 4;
2601 os << docstring(len, ch);
2602 if (bottomLine(i)) {
2616 void Tabular::plaintextPrintCell(odocstream & os,
2617 OutputParams const & runparams,
2618 idx_type cell, row_type row, col_type column,
2619 vector<unsigned int> const & clen,
2620 bool onlydata) const
2622 odocstringstream sstr;
2623 cellInset(cell)->plaintext(sstr, runparams);
2635 unsigned int len1 = sstr.str().length();
2636 unsigned int len2 = clen[column];
2637 while (column < column_info.size() -1
2638 && isPartOfMultiColumn(row, ++column))
2639 len2 += clen[column] + 4;
2642 switch (getAlignment(cell)) {
2644 case LYX_ALIGN_LEFT:
2647 case LYX_ALIGN_RIGHT:
2651 case LYX_ALIGN_CENTER:
2657 os << docstring(len1, ' ') << sstr.str()
2658 << docstring(len2, ' ');
2660 if (rightLine(cell))
2667 void Tabular::plaintext(odocstream & os,
2668 OutputParams const & runparams, int const depth,
2669 bool onlydata, char_type delim) const
2671 // first calculate the width of the single columns
2672 vector<unsigned int> clen(column_info.size());
2675 // first all non (real) multicolumn cells!
2676 for (col_type j = 0; j < column_info.size(); ++j) {
2678 for (row_type i = 0; i < row_info.size(); ++i) {
2679 idx_type cell = cellIndex(i, j);
2680 if (isMultiColumnReal(cell))
2682 odocstringstream sstr;
2683 cellInset(cell)->plaintext(sstr, runparams);
2684 if (clen[j] < sstr.str().length())
2685 clen[j] = sstr.str().length();
2688 // then all (real) multicolumn cells!
2689 for (col_type j = 0; j < column_info.size(); ++j) {
2690 for (row_type i = 0; i < row_info.size(); ++i) {
2691 idx_type cell = cellIndex(i, j);
2692 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2694 odocstringstream sstr;
2695 cellInset(cell)->plaintext(sstr, runparams);
2696 int len = int(sstr.str().length());
2697 idx_type const n = columnSpan(cell);
2698 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2700 if (len > int(clen[j + n - 1]))
2701 clen[j + n - 1] = len;
2706 for (row_type i = 0; i < row_info.size(); ++i) {
2707 if (!onlydata && plaintextTopHLine(os, i, clen))
2708 os << docstring(depth * 2, ' ');
2709 for (col_type j = 0; j < column_info.size(); ++j) {
2710 if (isPartOfMultiColumn(i, j))
2712 if (onlydata && j > 0)
2713 // we don't use operator<< for single UCS4 character.
2714 // see explanation in docstream.h
2716 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2721 os << docstring(depth * 2, ' ');
2722 if (plaintextBottomHLine(os, i, clen))
2723 os << docstring(depth * 2, ' ');
2729 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2731 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2735 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2736 col_type column) const
2738 return cell_info[row][column].inset;
2742 void Tabular::setCellInset(row_type row, col_type column,
2743 shared_ptr<InsetTableCell> ins) const
2745 CellData & cd = cell_info[row][column];
2750 void Tabular::validate(LaTeXFeatures & features) const
2752 features.require("NeedTabularnewline");
2754 features.require("booktabs");
2755 if (is_long_tabular)
2756 features.require("longtable");
2758 features.require("rotating");
2759 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2760 if (getVAlignment(cell) != LYX_VALIGN_TOP
2761 || !getPWidth(cell).zero())
2762 features.require("array");
2763 cellInset(cell)->validate(features);
2768 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2770 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2771 ParagraphList::const_iterator cit = parlist.begin();
2772 ParagraphList::const_iterator end = parlist.end();
2774 for (; cit != end; ++cit)
2775 for (int i = 0; i < cit->size(); ++i)
2776 if (cit->isNewline(i))
2783 /////////////////////////////////////////////////////////////////////
2787 /////////////////////////////////////////////////////////////////////
2789 InsetTableCell::InsetTableCell(Buffer & buf)
2790 : InsetText(buf), isFixedWidth(false),
2791 contentAlign(LYX_ALIGN_CENTER)
2795 bool InsetTableCell::forcePlainLayout(idx_type) const
2797 return !isFixedWidth;
2801 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2803 return isFixedWidth;
2807 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2808 FuncStatus & status) const
2811 switch (cmd.action) {
2813 enabled = !forcePlainLayout();
2815 case LFUN_LAYOUT_PARAGRAPH:
2816 enabled = allowParagraphCustomization();
2819 return InsetText::getStatus(cur, cmd, status);
2821 status.setEnabled(enabled);
2825 docstring InsetTableCell::asString(bool intoInsets)
2828 if (paragraphs().empty())
2830 ParagraphList::const_iterator it = paragraphs().begin();
2831 ParagraphList::const_iterator en = paragraphs().end();
2833 for (; it != en; ++it) {
2838 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2845 /////////////////////////////////////////////////////////////////////
2849 /////////////////////////////////////////////////////////////////////
2851 InsetTabular::InsetTabular(Buffer & buf, row_type rows,
2853 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2854 rowselect_(false), colselect_(false)
2856 setBuffer(buf); // FIXME: remove later
2860 InsetTabular::InsetTabular(InsetTabular const & tab)
2861 : Inset(tab), tabular(tab.tabular), scx_(0)
2866 InsetTabular::~InsetTabular()
2868 hideDialogs("tabular", this);
2872 void InsetTabular::setBuffer(Buffer & buf)
2874 tabular.setBuffer(buf);
2875 Inset::setBuffer(buf);
2879 bool InsetTabular::insetAllowed(InsetCode code) const
2881 if (code == MATHMACRO_CODE)
2888 void InsetTabular::write(ostream & os) const
2890 os << "Tabular" << endl;
2895 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2897 // FIXME: depending on the selection state, we could offer a different menu.
2898 return from_ascii("context-tabular");
2902 void InsetTabular::read(Lexer & lex)
2904 //bool const old_format = (lex.getString() == "\\LyXTable");
2912 string token = lex.getString();
2913 while (lex && token != "\\end_inset") {
2915 token = lex.getString();
2918 lex.printError("Missing \\end_inset at this point. ");
2922 int InsetTabular::rowFromY(Cursor & cur, int y) const
2924 // top y coordinate of tabular
2925 int h = yo(cur.bv()) - tabular.rowAscent(0);
2926 size_t nrows = tabular.row_info.size();
2928 for (; r < nrows && y > h; ++r) {
2929 h += tabular.rowAscent(r);
2930 h += tabular.rowDescent(r);
2931 h += tabular.getAdditionalHeight(r);
2937 int InsetTabular::columnFromX(Cursor & cur, int x) const
2939 // left x coordinate of tabular
2940 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2941 size_t ncols = tabular.column_info.size();
2943 for (; c < ncols && x > w; ++c)
2944 w += tabular.columnWidth(c);
2949 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2951 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2952 // mi.base.textwidth << "\n";
2955 LASSERT(false, /**/);
2959 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2962 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2963 if (tabular.isPartOfMultiColumn(i, j))
2964 // Multicolumn cell, but not first one
2969 if (tabular.cell_info[i][j].multicolumn ==
2970 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2971 p_width = tabular.cellInfo(cell).p_width;
2973 p_width = tabular.column_info[j].p_width;
2974 if (!p_width.zero())
2975 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2976 tabular.cellInset(cell)->metrics(m, dim);
2977 if (!p_width.zero())
2978 dim.wid = m.base.textwidth;
2979 tabular.setCellWidth(cell, dim.wid);
2980 maxAsc = max(maxAsc, dim.asc);
2981 maxDesc = max(maxDesc, dim.des);
2984 int const top_space = tabular.row_info[i].top_space_default ?
2985 default_line_space :
2986 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2987 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2988 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2989 default_line_space :
2990 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2991 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2993 tabular.updateColumnWidths();
2994 dim.asc = tabular.rowAscent(0);
2995 dim.des = tabular.height() - dim.asc;
2996 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2999 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3002 if (&cur.inset() == this && cur.selection()) {
3003 if (cur.selIsMultiCell()) {
3006 getSelection(cur, rs, re, cs, ce);
3008 if (col >= cs && col <= ce && row >= rs && row <= re)
3011 if (col == tabular.cellColumn(cur.idx())
3012 && row == tabular.cellRow(cur.idx())) {
3013 CursorSlice const & beg = cur.selBegin();
3014 CursorSlice const & end = cur.selEnd();
3016 if ((end.lastpos() > 0 || end.lastpit() > 0)
3017 && end.pos() == end.lastpos() && beg.pos() == 0
3018 && end.pit() == end.lastpit() && beg.pit() == 0)
3026 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3028 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3029 BufferView * bv = pi.base.bv;
3030 Cursor & cur = pi.base.bv->cursor();
3032 // FIXME: As the full backrgound is painted in drawSelection(),
3033 // we have no choice but to do a full repaint for the Text cells.
3034 pi.full_repaint = true;
3036 resetPos(bv->cursor());
3039 x += ADD_TO_TABULAR_WIDTH;
3041 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3042 bool const original_selection_state = pi.selected;
3045 first_visible_cell = Tabular::npos;
3046 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3048 int const a = tabular.rowAscent(i);
3049 int const d = tabular.rowDescent(i);
3050 idx = tabular.cellIndex(i, 0);
3051 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3052 if (tabular.isPartOfMultiColumn(i, j))
3054 if (first_visible_cell == Tabular::npos)
3055 first_visible_cell = idx;
3057 pi.selected |= isCellSelected(cur, i, j);
3058 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3059 // Cache the Inset position.
3060 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3061 if (nx + tabular.columnWidth(idx) < 0
3062 || nx > bv->workWidth()
3064 || y - a > bv->workHeight()) {
3065 pi.pain.setDrawingEnabled(false);
3066 cell(idx)->draw(pi, cx, y);
3067 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3068 pi.pain.setDrawingEnabled(original_drawing_state);
3070 cell(idx)->draw(pi, cx, y);
3071 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3073 nx += tabular.columnWidth(idx);
3075 pi.selected = original_selection_state;
3078 if (i + 1 < tabular.row_info.size())
3079 y += d + tabular.rowAscent(i + 1) +
3080 tabular.getAdditionalHeight(i + 1);
3085 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3087 Cursor & cur = pi.base.bv->cursor();
3089 x += scx_ + ADD_TO_TABULAR_WIDTH;
3091 // FIXME: it is wrong to completely paint the background
3092 // if we want to do single row painting.
3094 // Paint background of current tabular
3095 int const w = tabular.width();
3096 int const h = tabular.height();
3097 int yy = y - tabular.rowAscent(0);
3098 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3100 if (!cur.selection())
3102 if (&cur.inset() != this)
3107 bool const full_cell_selected = isCellSelected(cur,
3108 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3110 if (cur.selIsMultiCell() || full_cell_selected) {
3111 y -= tabular.rowAscent(0);
3112 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3113 int const a = tabular.rowAscent(j);
3114 int const h = a + tabular.rowDescent(j);
3116 y += tabular.getAdditionalHeight(j);
3117 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3118 if (tabular.isPartOfMultiColumn(j, i))
3120 idx_type const cell =
3121 tabular.cellIndex(j, i);
3122 int const w = tabular.columnWidth(cell);
3123 if (isCellSelected(cur, j, i))
3124 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3131 x += cellXPos(cur.idx());
3132 x += tabular.getBeginningOfTextInCell(cur.idx());
3133 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3138 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3139 row_type row, idx_type cell, bool erased) const
3141 int x2 = x + tabular.columnWidth(cell);
3142 bool on_off = false;
3143 ColorCode col = Color_tabularline;
3144 ColorCode onoffcol = Color_tabularonoffline;
3147 col = Color_deletedtext;
3148 onoffcol = Color_deletedtext;
3151 if (!tabular.topAlreadyDrawn(cell)) {
3152 on_off = !tabular.topLine(cell);
3153 pain.line(x, y - tabular.rowAscent(row),
3154 x2, y - tabular.rowAscent(row),
3155 on_off ? onoffcol : col,
3156 on_off ? Painter::line_onoffdash : Painter::line_solid);
3158 on_off = !tabular.bottomLine(cell);
3159 pain.line(x, y + tabular.rowDescent(row),
3160 x2, y + tabular.rowDescent(row),
3161 on_off ? onoffcol : col,
3162 on_off ? Painter::line_onoffdash : Painter::line_solid);
3163 if (!tabular.leftAlreadyDrawn(cell)) {
3164 on_off = !tabular.leftLine(cell);
3165 pain.line(x, y - tabular.rowAscent(row),
3166 x, y + tabular.rowDescent(row),
3167 on_off ? onoffcol : col,
3168 on_off ? Painter::line_onoffdash : Painter::line_solid);
3170 on_off = !tabular.rightLine(cell);
3171 pain.line(x2 - tabular.getAdditionalWidth(cell),
3172 y - tabular.rowAscent(row),
3173 x2 - tabular.getAdditionalWidth(cell),
3174 y + tabular.rowDescent(row),
3175 on_off ? onoffcol : col,
3176 on_off ? Painter::line_onoffdash : Painter::line_solid);
3180 docstring InsetTabular::editMessage() const
3182 return _("Opened table");
3186 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3188 //lyxerr << "InsetTabular::edit: " << this << endl;
3190 cur.setSelection(false);
3193 if (isRightToLeft(cur))
3194 cur.idx() = tabular.getLastCellInRow(0);
3200 if (isRightToLeft(cur))
3201 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3203 cur.idx() = tabular.numberofcells - 1;
3205 cur.pos() = cur.lastpos(); // FIXME crude guess
3207 // FIXME: this accesses the position cache before it is initialized
3209 //cur.bv().fitCursor();
3213 void InsetTabular::updateLabels(ParIterator const & it)
3215 // In a longtable, tell captions what the current float is
3216 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3217 string const saveflt = cnts.current_float();
3218 if (tabular.is_long_tabular)
3219 cnts.current_float("table");
3221 ParIterator it2 = it;
3223 size_t const end = it2.nargs();
3224 for ( ; it2.idx() < end; it2.top().forwardIdx())
3225 buffer().updateLabels(it2);
3228 if (tabular.is_long_tabular)
3229 cnts.current_float(saveflt);
3233 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3235 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3236 << "\n cur:" << cur);
3237 CursorSlice sl = cur.top();
3238 Cursor & bvcur = cur.bv().cursor();
3240 switch (cmd.action) {
3242 case LFUN_MOUSE_PRESS: {
3243 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3245 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3246 || cmd.x > xo(cur.bv()) + tabular.width()) {
3247 row_type r = rowFromY(cur, cmd.y);
3248 cur.idx() = tabular.getFirstCellInRow(r);
3251 cur.idx() = tabular.getLastCellInRow(r);
3252 cur.pos() = cur.lastpos();
3253 cur.setSelection(true);
3259 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3260 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3261 || cmd.y > y0 + tabular.height()) {
3262 col_type c = columnFromX(cur, cmd.x);
3263 cur.idx() = tabular.cellIndex(0, c);
3266 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3267 cur.pos() = cur.lastpos();
3268 cur.setSelection(true);
3273 // do not reset cursor/selection if we have selected
3274 // some cells (bug 2715).
3275 if (cmd.button() == mouse_button::button3
3276 && &bvcur.selBegin().inset() == this
3277 && bvcur.selIsMultiCell())
3280 // Let InsetTableCell do it
3281 cell(cur.idx())->dispatch(cur, cmd);
3284 case LFUN_MOUSE_MOTION:
3285 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3286 if (cmd.button() == mouse_button::button1) {
3287 // only accept motions to places not deeper nested than the real anchor
3288 if (!bvcur.anchor_.hasPart(cur)) {
3292 // select (additional) row
3294 row_type r = rowFromY(cur, cmd.y);
3295 cur.idx() = tabular.getLastCellInRow(r);
3296 // we need to reset the cursor's pit and pos now, as the old ones
3297 // may no longer be valid.
3300 bvcur.setCursor(cur);
3301 bvcur.setSelection(true);
3304 // select (additional) column
3306 col_type c = columnFromX(cur, cmd.x);
3307 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3308 // we need to reset the cursor's pit and pos now, as the old ones
3309 // may no longer be valid.
3312 bvcur.setCursor(cur);
3313 bvcur.setSelection(true);
3316 // only update if selection changes
3317 if (bvcur.idx() == cur.idx() &&
3318 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3320 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3321 bvcur.setCursor(cur);
3322 bvcur.setSelection(true);
3323 // if this is a multicell selection, we just set the cursor to
3324 // the beginning of the cell's text.
3325 if (bvcur.selIsMultiCell()) {
3326 bvcur.pit() = bvcur.lastpit();
3327 bvcur.pos() = bvcur.lastpos();
3332 case LFUN_MOUSE_RELEASE:
3337 case LFUN_CELL_BACKWARD:
3339 cur.setSelection(false);
3342 case LFUN_CELL_FORWARD:
3344 cur.setSelection(false);
3347 case LFUN_CHAR_FORWARD_SELECT:
3348 case LFUN_CHAR_FORWARD:
3349 case LFUN_CHAR_BACKWARD_SELECT:
3350 case LFUN_CHAR_BACKWARD:
3351 case LFUN_CHAR_RIGHT_SELECT:
3352 case LFUN_CHAR_RIGHT:
3353 case LFUN_CHAR_LEFT_SELECT:
3354 case LFUN_CHAR_LEFT: {
3355 // determine whether we move to next or previous cell, where to enter
3356 // the new cell from, and which command to "finish" (i.e., exit the
3359 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3360 FuncCode finish_lfun;
3362 if (cmd.action == LFUN_CHAR_FORWARD
3363 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3365 finish_lfun = LFUN_FINISHED_FORWARD;
3367 else if (cmd.action == LFUN_CHAR_BACKWARD
3368 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3370 finish_lfun = LFUN_FINISHED_BACKWARD;
3372 // LEFT or RIGHT commands --- the interpretation will depend on the
3373 // table's direction.
3375 bool right = (cmd.action == LFUN_CHAR_RIGHT
3376 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3377 next_cell = (isRightToLeft(cur) != right);
3379 if (lyxrc.visual_cursor) {
3380 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3384 finish_lfun = LFUN_FINISHED_RIGHT;
3386 finish_lfun = LFUN_FINISHED_LEFT;
3389 // if we don't have a multicell selection...
3390 if (!cur.selIsMultiCell() ||
3391 // ...or we're not doing some LFUN_*_SELECT thing, anyway...
3392 (cmd.action != LFUN_CHAR_FORWARD_SELECT &&
3393 cmd.action != LFUN_CHAR_BACKWARD_SELECT &&
3394 cmd.action != LFUN_CHAR_RIGHT_SELECT &&
3395 cmd.action != LFUN_CHAR_LEFT_SELECT)) {
3396 // ...try to dispatch to the cell's inset.
3397 cell(cur.idx())->dispatch(cur, cmd);
3398 if (cur.result().dispatched())
3401 // move to next/prev cell, as appropriate
3402 // note that we will always do this if we're selecting and we have
3403 // a multicell selection
3404 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3405 << " cell from: " << int(entry_from));
3407 moveNextCell(cur, entry_from);
3409 movePrevCell(cur, entry_from);
3410 // if we're exiting the table, call the appropriate FINISHED lfun
3411 if (sl == cur.top())
3412 cmd = FuncRequest(finish_lfun);
3419 case LFUN_DOWN_SELECT:
3421 cell(cur.idx())->dispatch(cur, cmd);
3422 cur.dispatched(); // override the cell's decision
3423 if (sl == cur.top())
3424 // if our Text didn't do anything to the cursor
3425 // then we try to put the cursor into the cell below
3426 // setting also the right targetX.
3427 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3428 cur.idx() = tabular.cellBelow(cur.idx());
3430 TextMetrics const & tm =
3431 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3432 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3434 if (sl == cur.top()) {
3435 // we trick it to go to forward after leaving the
3437 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3440 if (cur.selIsMultiCell()) {
3441 cur.pit() = cur.lastpit();
3442 cur.pos() = cur.lastpos();
3447 case LFUN_UP_SELECT:
3449 cell(cur.idx())->dispatch(cur, cmd);
3450 cur.dispatched(); // override the cell's decision
3451 if (sl == cur.top())
3452 // if our Text didn't do anything to the cursor
3453 // then we try to put the cursor into the cell above
3454 // setting also the right targetX.
3455 if (tabular.cellRow(cur.idx()) != 0) {
3456 cur.idx() = tabular.cellAbove(cur.idx());
3457 cur.pit() = cur.lastpit();
3458 Text const * text = cell(cur.idx())->getText(0);
3459 TextMetrics const & tm = cur.bv().textMetrics(text);
3460 ParagraphMetrics const & pm =
3461 tm.parMetrics(cur.lastpit());
3462 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3464 if (sl == cur.top()) {
3465 cmd = FuncRequest(LFUN_UP);
3468 if (cur.selIsMultiCell()) {
3469 cur.pit() = cur.lastpit();
3470 cur.pos() = cur.lastpos();
3475 // case LFUN_SCREEN_DOWN: {
3476 // //if (hasSelection())
3477 // // cur.selection() = false;
3478 // col_type const col = tabular.cellColumn(cur.idx());
3479 // int const t = cur.bv().top_y() + cur.bv().height();
3480 // if (t < yo() + tabular.getHeightOfTabular()) {
3481 // cur.bv().scrollDocView(t);
3482 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3484 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3491 // case LFUN_SCREEN_UP: {
3492 // //if (hasSelection())
3493 // // cur.selection() = false;
3494 // col_type const col = tabular.cellColumn(cur.idx());
3495 // int const t = cur.bv().top_y() + cur.bv().height();
3497 // cur.bv().scrollDocView(t);
3501 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3505 // cur.par() = cur.lastpar();
3506 // cur.pos() = cur.lastpos();
3510 case LFUN_LAYOUT_TABULAR:
3511 cur.bv().showDialog("tabular", params2string(*this), this);
3514 case LFUN_INSET_DIALOG_UPDATE:
3515 cur.bv().updateDialog("tabular", params2string(*this));
3518 case LFUN_TABULAR_FEATURE:
3519 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3523 // insert file functions
3524 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3525 case LFUN_FILE_INSERT_PLAINTEXT: {
3527 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3528 FileName(to_utf8(cmd.argument())));
3531 cur.recordUndoInset(INSERT_UNDO);
3532 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3533 // content has been replaced,
3534 // so cursor might be invalid
3535 cur.pos() = cur.lastpos();
3536 cur.pit() = cur.lastpit();
3537 bvcur.setCursor(cur);
3544 if (cur.selIsMultiCell()) {
3545 if (copySelection(cur)) {
3546 cur.recordUndoInset(DELETE_UNDO);
3551 cell(cur.idx())->dispatch(cur, cmd);
3554 case LFUN_SELF_INSERT:
3555 if (cur.selIsMultiCell()) {
3556 cur.recordUndoInset(DELETE_UNDO);
3559 cell(cur.idx())->dispatch(cur, cmd);
3562 case LFUN_CHAR_DELETE_BACKWARD:
3563 case LFUN_CHAR_DELETE_FORWARD:
3564 if (cur.selIsMultiCell()) {
3565 cur.recordUndoInset(DELETE_UNDO);
3569 cell(cur.idx())->dispatch(cur, cmd);
3573 if (!cur.selection())
3575 if (cur.selIsMultiCell()) {
3579 cell(cur.idx())->dispatch(cur, cmd);
3582 case LFUN_CLIPBOARD_PASTE:
3583 case LFUN_PRIMARY_SELECTION_PASTE: {
3584 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3585 theClipboard().getAsText() :
3586 theSelection().get();
3589 // pass to InsertPlaintextString, but
3590 // only if we have multi-cell content
3591 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3592 cur.recordUndoInset(INSERT_UNDO);
3593 if (insertPlaintextString(cur.bv(), clip, false)) {
3594 // content has been replaced,
3595 // so cursor might be invalid
3596 cur.pos() = cur.lastpos();
3597 cur.pit() = cur.lastpit();
3598 bvcur.setCursor(cur);
3602 // Let the cell handle normal text
3603 cell(cur.idx())->dispatch(cur, cmd);
3608 if (!tabularStackDirty()) {
3609 cell(cur.idx())->dispatch(cur, cmd);
3612 if (theClipboard().isInternal() ||
3613 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3614 cur.recordUndoInset(INSERT_UNDO);
3615 pasteClipboard(cur);
3619 case LFUN_FONT_EMPH:
3620 case LFUN_FONT_BOLD:
3621 case LFUN_FONT_BOLDSYMBOL:
3622 case LFUN_FONT_ROMAN:
3623 case LFUN_FONT_NOUN:
3624 case LFUN_FONT_ITAL:
3625 case LFUN_FONT_FRAK:
3626 case LFUN_FONT_TYPEWRITER:
3627 case LFUN_FONT_SANS:
3628 case LFUN_TEXTSTYLE_APPLY:
3629 case LFUN_TEXTSTYLE_UPDATE:
3630 case LFUN_FONT_SIZE:
3631 case LFUN_FONT_UNDERLINE:
3633 case LFUN_WORD_CAPITALIZE:
3634 case LFUN_WORD_UPCASE:
3635 case LFUN_WORD_LOWCASE:
3636 case LFUN_CHARS_TRANSPOSE:
3637 if (cur.selIsMultiCell()) {
3640 getSelection(cur, rs, re, cs, ce);
3641 Cursor tmpcur = cur;
3642 for (row_type i = rs; i <= re; ++i) {
3643 for (col_type j = cs; j <= ce; ++j) {
3644 // cursor follows cell:
3645 tmpcur.idx() = tabular.cellIndex(i, j);
3646 // select this cell only:
3649 tmpcur.resetAnchor();
3650 tmpcur.pit() = tmpcur.lastpit();
3651 tmpcur.pos() = tmpcur.top().lastpos();
3652 tmpcur.setCursor(tmpcur);
3653 tmpcur.setSelection();
3654 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3659 cell(cur.idx())->dispatch(cur, cmd);
3663 // we try to handle this event in the insets dispatch function.
3664 cell(cur.idx())->dispatch(cur, cmd);
3670 // function sets an object as defined in func_status.h:
3671 // states OK, Unknown, Disabled, On, Off.
3672 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3673 FuncStatus & status) const
3675 switch (cmd.action) {
3676 case LFUN_TABULAR_FEATURE: {
3677 int action = Tabular::LAST_ACTION;
3679 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3680 string const tmp = tabularFeature[i].feature;
3681 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3682 action = tabularFeature[i].action;
3686 if (action == Tabular::LAST_ACTION) {
3688 status.unknown(true);
3692 string const argument
3693 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3695 row_type sel_row_start = 0;
3696 row_type sel_row_end = 0;
3697 col_type sel_col_start = 0;
3698 col_type sel_col_end = 0;
3699 Tabular::ltType dummyltt;
3702 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3705 case Tabular::SET_PWIDTH:
3706 case Tabular::SET_MPWIDTH:
3707 case Tabular::SET_SPECIAL_COLUMN:
3708 case Tabular::SET_SPECIAL_MULTI:
3709 case Tabular::APPEND_ROW:
3710 case Tabular::APPEND_COLUMN:
3711 case Tabular::DELETE_ROW:
3712 case Tabular::DELETE_COLUMN:
3713 case Tabular::COPY_ROW:
3714 case Tabular::COPY_COLUMN:
3715 case Tabular::SET_TOP_SPACE:
3716 case Tabular::SET_BOTTOM_SPACE:
3717 case Tabular::SET_INTERLINE_SPACE:
3721 case Tabular::MULTICOLUMN:
3722 status.setEnabled(sel_row_start == sel_row_end);
3723 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3726 case Tabular::SET_ALL_LINES:
3727 case Tabular::UNSET_ALL_LINES:
3728 case Tabular::SET_BORDER_LINES:
3729 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3732 case Tabular::TOGGLE_LINE_TOP:
3733 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3734 status.setOnOff(tabular.topLine(cur.idx()));
3737 case Tabular::TOGGLE_LINE_BOTTOM:
3738 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3739 status.setOnOff(tabular.bottomLine(cur.idx()));
3742 case Tabular::TOGGLE_LINE_LEFT:
3743 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3744 status.setOnOff(tabular.leftLine(cur.idx()));
3747 case Tabular::TOGGLE_LINE_RIGHT:
3748 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3749 status.setOnOff(tabular.rightLine(cur.idx()));
3752 case Tabular::M_ALIGN_LEFT:
3754 case Tabular::ALIGN_LEFT:
3755 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3758 case Tabular::M_ALIGN_RIGHT:
3760 case Tabular::ALIGN_RIGHT:
3761 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3764 case Tabular::M_ALIGN_CENTER:
3766 case Tabular::ALIGN_CENTER:
3767 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3770 case Tabular::ALIGN_BLOCK:
3771 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3772 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3775 case Tabular::M_VALIGN_TOP:
3777 case Tabular::VALIGN_TOP:
3779 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3782 case Tabular::M_VALIGN_BOTTOM:
3784 case Tabular::VALIGN_BOTTOM:
3786 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3789 case Tabular::M_VALIGN_MIDDLE:
3791 case Tabular::VALIGN_MIDDLE:
3793 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3796 case Tabular::SET_LONGTABULAR:
3797 status.setOnOff(tabular.is_long_tabular);
3800 case Tabular::UNSET_LONGTABULAR:
3801 status.setOnOff(!tabular.is_long_tabular);
3804 case Tabular::TOGGLE_ROTATE_TABULAR:
3805 case Tabular::SET_ROTATE_TABULAR:
3806 status.setOnOff(tabular.rotate);
3809 case Tabular::UNSET_ROTATE_TABULAR:
3810 status.setOnOff(!tabular.rotate);
3813 case Tabular::TOGGLE_ROTATE_CELL:
3814 case Tabular::SET_ROTATE_CELL:
3815 status.setOnOff(!oneCellHasRotationState(false,
3816 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3819 case Tabular::UNSET_ROTATE_CELL:
3820 status.setOnOff(!oneCellHasRotationState(true,
3821 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3824 case Tabular::SET_USEBOX:
3825 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3828 case Tabular::SET_LTFIRSTHEAD:
3829 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3832 case Tabular::UNSET_LTFIRSTHEAD:
3833 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3836 case Tabular::SET_LTHEAD:
3837 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3840 case Tabular::UNSET_LTHEAD:
3841 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3844 case Tabular::SET_LTFOOT:
3845 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3848 case Tabular::UNSET_LTFOOT:
3849 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3852 case Tabular::SET_LTLASTFOOT:
3853 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3856 case Tabular::UNSET_LTLASTFOOT:
3857 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3860 case Tabular::SET_LTNEWPAGE:
3861 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3864 case Tabular::TOGGLE_LTCAPTION:
3865 status.setEnabled(sel_row_start == sel_row_end);
3866 status.setOnOff(tabular.ltCaption(sel_row_start));
3869 case Tabular::SET_BOOKTABS:
3870 status.setOnOff(tabular.use_booktabs);
3873 case Tabular::UNSET_BOOKTABS:
3874 status.setOnOff(!tabular.use_booktabs);
3879 status.setEnabled(false);
3885 // These are only enabled inside tabular
3886 case LFUN_CELL_BACKWARD:
3887 case LFUN_CELL_FORWARD:
3888 status.setEnabled(true);
3891 // disable these with multiple cells selected
3892 case LFUN_INSET_INSERT:
3893 case LFUN_TABULAR_INSERT:
3894 case LFUN_FLEX_INSERT:
3895 case LFUN_FLOAT_INSERT:
3896 case LFUN_FLOAT_WIDE_INSERT:
3897 case LFUN_FOOTNOTE_INSERT:
3898 case LFUN_MARGINALNOTE_INSERT:
3899 case LFUN_MATH_INSERT:
3900 case LFUN_MATH_MODE:
3901 case LFUN_MATH_MUTATE:
3902 case LFUN_MATH_DISPLAY:
3903 case LFUN_NOTE_INSERT:
3904 case LFUN_OPTIONAL_INSERT:
3905 case LFUN_BOX_INSERT:
3906 case LFUN_BRANCH_INSERT:
3907 case LFUN_WRAP_INSERT:
3908 case LFUN_ERT_INSERT: {
3909 if (cur.selIsMultiCell()) {
3910 status.setEnabled(false);
3913 return cell(cur.idx())->getStatus(cur, cmd, status);
3916 // disable in non-fixed-width cells
3917 case LFUN_NEWLINE_INSERT:
3918 case LFUN_BREAK_PARAGRAPH: {
3919 if (tabular.getPWidth(cur.idx()).zero()) {
3920 status.setEnabled(false);
3923 return cell(cur.idx())->getStatus(cur, cmd, status);
3926 case LFUN_NEWPAGE_INSERT:
3927 status.setEnabled(false);
3931 if (cur.selIsMultiCell()) {
3932 status.setEnabled(false);
3933 status.message(_("You cannot paste into a multicell selection."));
3936 if (tabularStackDirty() && theClipboard().isInternal()) {
3937 status.setEnabled(true);
3940 return cell(cur.idx())->getStatus(cur, cmd, status);
3942 case LFUN_INSET_MODIFY:
3943 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3944 status.setEnabled(true);
3950 // we try to handle this event in the insets dispatch function.
3951 return cell(cur.idx())->getStatus(cur, cmd, status);
3956 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3958 return tabular.latex(os, runparams);
3962 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3964 os << '\n'; // output table on a new line
3965 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3966 tabular.plaintext(os, runparams, dp, false, 0);
3967 return PLAINTEXT_NEWLINE;
3971 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3976 // FIXME: Why not pass a proper DocIterator here?
3978 // if the table is inside a float it doesn't need the informaltable
3979 // wrapper. Search for it.
3980 for (master = owner(); master; master = master->owner())
3981 if (master->lyxCode() == FLOAT_CODE)
3986 os << "<informaltable>";
3989 ret += tabular.docbook(os, runparams);
3991 os << "</informaltable>";
3998 void InsetTabular::validate(LaTeXFeatures & features) const
4000 tabular.validate(features);
4004 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4006 return tabular.cellInset(idx);
4010 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4012 return tabular.cellInset(idx);
4016 void InsetTabular::cursorPos(BufferView const & bv,
4017 CursorSlice const & sl, bool boundary, int & x, int & y) const
4019 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4021 // y offset correction
4022 int const row = tabular.cellRow(sl.idx());
4023 for (int i = 0; i <= row; ++i) {
4025 y += tabular.rowAscent(i);
4026 y += tabular.getAdditionalHeight(i);
4029 y += tabular.rowDescent(i);
4032 // x offset correction
4033 int const col = tabular.cellColumn(sl.idx());
4034 int idx = tabular.cellIndex(row, 0);
4035 for (int j = 0; j < col; ++j) {
4036 if (tabular.isPartOfMultiColumn(row, j))
4038 x += tabular.columnWidth(idx);
4041 x += tabular.getBeginningOfTextInCell(idx);
4042 x += ADD_TO_TABULAR_WIDTH;
4047 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4051 Inset const & inset = *tabular.cellInset(cell);
4052 Point o = bv.coordCache().getInsets().xy(&inset);
4053 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
4054 int const xend = xbeg + tabular.columnWidth(cell);
4055 row_type const row = tabular.cellRow(cell);
4056 int const ybeg = o.y_ - tabular.rowAscent(row) -
4057 tabular.getAdditionalHeight(row);
4058 int const yend = o.y_ + tabular.rowDescent(row);
4070 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4071 // << " ybeg=" << ybeg << " yend=" << yend
4072 // << " xx=" << xx << " yy=" << yy
4073 // << " dist=" << xx + yy << endl;
4078 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4080 //lyxerr << "InsetTabular::editXY: " << this << endl;
4081 cur.setSelection(false);
4083 cur.idx() = getNearestCell(cur.bv(), x, y);
4085 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4089 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4091 cur.idx() = getNearestCell(cur.bv(), x, y);
4092 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4096 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4098 idx_type idx_min = 0;
4099 int dist_min = numeric_limits<int>::max();
4100 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4101 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4102 int const d = dist(bv, i, x, y);
4113 int InsetTabular::cellXPos(idx_type const cell) const
4117 for (; !tabular.isFirstCellInRow(c); --c)
4120 for (; c < cell; ++c)
4121 lx += tabular.columnWidth(c);
4127 void InsetTabular::resetPos(Cursor & cur) const
4129 BufferView & bv = cur.bv();
4130 int const maxwidth = bv.workWidth();
4132 int const scx_old = scx_;
4133 int const i = cur.find(this);
4138 int const X2 = maxwidth;
4139 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4140 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4141 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4144 scx_ = X1 + 20 - x1;
4146 scx_ = X2 - 20 - x2;
4151 // only update if offset changed
4152 if (scx_ != scx_old)
4153 cur.updateFlags(Update::Force | Update::FitCursor);
4157 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4159 if (isRightToLeft(cur)) {
4160 if (tabular.isFirstCellInRow(cur.idx())) {
4161 row_type const row = tabular.cellRow(cur.idx());
4162 if (row == tabular.row_info.size() - 1)
4164 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4171 if (tabular.isLastCell(cur.idx()))
4176 cur.boundary(false);
4178 if (cur.selIsMultiCell()) {
4179 cur.pit() = cur.lastpit();
4180 cur.pos() = cur.lastpos();
4188 // in visual mode, place cursor at extreme left or right
4190 switch(entry_from) {
4192 case ENTRY_DIRECTION_RIGHT:
4193 cur.posVisToRowExtremity(false /* !left */);
4195 case ENTRY_DIRECTION_LEFT:
4196 cur.posVisToRowExtremity(true /* left */);
4198 case ENTRY_DIRECTION_IGNORE:
4199 // nothing to do in this case
4208 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4210 if (isRightToLeft(cur)) {
4211 if (tabular.isLastCellInRow(cur.idx())) {
4212 row_type const row = tabular.cellRow(cur.idx());
4215 cur.idx() = tabular.getFirstCellInRow(row);
4216 cur.idx() = tabular.cellAbove(cur.idx());
4218 if (tabular.isLastCell(cur.idx()))
4223 if (cur.idx() == 0) // first cell
4228 if (cur.selIsMultiCell()) {
4229 cur.pit() = cur.lastpit();
4230 cur.pos() = cur.lastpos();
4235 cur.pit() = cur.lastpit();
4236 cur.pos() = cur.lastpos();
4238 // in visual mode, place cursor at extreme left or right
4240 switch(entry_from) {
4242 case ENTRY_DIRECTION_RIGHT:
4243 cur.posVisToRowExtremity(false /* !left */);
4245 case ENTRY_DIRECTION_LEFT:
4246 cur.posVisToRowExtremity(true /* left */);
4248 case ENTRY_DIRECTION_IGNORE:
4249 // nothing to do in this case
4258 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4260 Tabular::Feature action = Tabular::LAST_ACTION;
4263 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4264 string const tmp = tabularFeature[i].feature;
4266 if (tmp == what.substr(0, tmp.length())) {
4267 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4268 //tabularFeatures[i].feature.length()))
4269 action = tabularFeature[i].action;
4273 if (action == Tabular::LAST_ACTION)
4277 ltrim(what.substr(tabularFeature[i].feature.length()));
4278 tabularFeatures(cur, action, val);
4283 static void checkLongtableSpecial(Tabular::ltType & ltt,
4284 string const & special, bool & flag)
4286 if (special == "dl_above") {
4289 } else if (special == "dl_below") {
4290 ltt.bottomDL = flag;
4292 } else if (special == "empty") {
4301 bool InsetTabular::oneCellHasRotationState(bool rotated,
4302 row_type row_start, row_type row_end,
4303 col_type col_start, col_type col_end) const {
4305 for (row_type i = row_start; i <= row_end; ++i) {
4306 for (col_type j = col_start; j <= col_end; ++j) {
4307 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4316 void InsetTabular::tabularFeatures(Cursor & cur,
4317 Tabular::Feature feature, string const & value)
4319 col_type sel_col_start;
4320 col_type sel_col_end;
4321 row_type sel_row_start;
4322 row_type sel_row_end;
4323 bool setLines = false;
4324 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4325 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4329 case Tabular::M_ALIGN_LEFT:
4330 case Tabular::ALIGN_LEFT:
4331 setAlign = LYX_ALIGN_LEFT;
4334 case Tabular::M_ALIGN_RIGHT:
4335 case Tabular::ALIGN_RIGHT:
4336 setAlign = LYX_ALIGN_RIGHT;
4339 case Tabular::M_ALIGN_CENTER:
4340 case Tabular::ALIGN_CENTER:
4341 setAlign = LYX_ALIGN_CENTER;
4344 case Tabular::ALIGN_BLOCK:
4345 setAlign = LYX_ALIGN_BLOCK;
4348 case Tabular::M_VALIGN_TOP:
4349 case Tabular::VALIGN_TOP:
4350 setVAlign = Tabular::LYX_VALIGN_TOP;
4353 case Tabular::M_VALIGN_BOTTOM:
4354 case Tabular::VALIGN_BOTTOM:
4355 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4358 case Tabular::M_VALIGN_MIDDLE:
4359 case Tabular::VALIGN_MIDDLE:
4360 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4367 cur.recordUndoInset(ATOMIC_UNDO);
4369 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4370 row_type const row = tabular.cellRow(cur.idx());
4371 col_type const column = tabular.cellColumn(cur.idx());
4373 Tabular::ltType ltt;
4377 case Tabular::SET_PWIDTH: {
4378 Length const len(value);
4379 tabular.setColumnPWidth(cur, cur.idx(), len);
4381 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4382 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4386 case Tabular::SET_MPWIDTH:
4387 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4390 case Tabular::SET_SPECIAL_COLUMN:
4391 case Tabular::SET_SPECIAL_MULTI:
4392 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4395 case Tabular::APPEND_ROW:
4396 // append the row into the tabular
4397 tabular.appendRow(cur.idx());
4400 case Tabular::APPEND_COLUMN:
4401 // append the column into the tabular
4402 tabular.appendColumn(cur.idx());
4403 cur.idx() = tabular.cellIndex(row, column);
4406 case Tabular::DELETE_ROW:
4407 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4408 tabular.deleteRow(sel_row_start);
4409 if (sel_row_start >= tabular.row_info.size())
4411 cur.idx() = tabular.cellIndex(sel_row_start, column);
4414 cur.setSelection(false);
4417 case Tabular::DELETE_COLUMN:
4418 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4419 tabular.deleteColumn(sel_col_start);
4420 if (sel_col_start >= tabular.column_info.size())
4422 cur.idx() = tabular.cellIndex(row, sel_col_start);
4425 cur.setSelection(false);
4428 case Tabular::COPY_ROW:
4429 tabular.copyRow(row);
4432 case Tabular::COPY_COLUMN:
4433 tabular.copyColumn(column);
4434 cur.idx() = tabular.cellIndex(row, column);
4437 case Tabular::TOGGLE_LINE_TOP: {
4438 bool lineSet = !tabular.topLine(cur.idx());
4439 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4440 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4441 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4445 case Tabular::TOGGLE_LINE_BOTTOM: {
4446 bool lineSet = !tabular.bottomLine(cur.idx());
4447 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4448 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4449 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4453 case Tabular::TOGGLE_LINE_LEFT: {
4454 bool lineSet = !tabular.leftLine(cur.idx());
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4456 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4457 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4461 case Tabular::TOGGLE_LINE_RIGHT: {
4462 bool lineSet = !tabular.rightLine(cur.idx());
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4464 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4465 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4469 case Tabular::M_ALIGN_LEFT:
4470 case Tabular::M_ALIGN_RIGHT:
4471 case Tabular::M_ALIGN_CENTER:
4473 case Tabular::ALIGN_LEFT:
4474 case Tabular::ALIGN_RIGHT:
4475 case Tabular::ALIGN_CENTER:
4476 case Tabular::ALIGN_BLOCK:
4477 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4478 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4479 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4482 case Tabular::M_VALIGN_TOP:
4483 case Tabular::M_VALIGN_BOTTOM:
4484 case Tabular::M_VALIGN_MIDDLE:
4486 case Tabular::VALIGN_TOP:
4487 case Tabular::VALIGN_BOTTOM:
4488 case Tabular::VALIGN_MIDDLE:
4489 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4490 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4491 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4494 case Tabular::MULTICOLUMN: {
4495 if (!cur.selection()) {
4496 // just multicol for one single cell
4497 // check whether we are completely in a multicol
4498 if (tabular.isMultiColumn(cur.idx()))
4499 tabular.unsetMultiColumn(cur.idx());
4501 tabular.setMultiColumn(cur.idx(), 1);
4504 // we have a selection so this means we just add all this
4505 // cells to form a multicolumn cell
4506 idx_type const s_start = cur.selBegin().idx();
4507 idx_type const s_end = cur.selEnd().idx();
4508 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4509 cur.idx() = s_start;
4512 cur.setSelection(false);
4516 case Tabular::SET_ALL_LINES:
4518 case Tabular::UNSET_ALL_LINES:
4519 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4520 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4521 tabular.setAllLines(
4522 tabular.cellIndex(i,j), setLines);
4525 case Tabular::SET_BORDER_LINES:
4526 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4527 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4528 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4530 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4531 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4532 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4536 case Tabular::SET_LONGTABULAR:
4537 tabular.is_long_tabular = true;
4540 case Tabular::UNSET_LONGTABULAR:
4541 tabular.is_long_tabular = false;
4544 case Tabular::SET_ROTATE_TABULAR:
4545 tabular.rotate = true;
4548 case Tabular::UNSET_ROTATE_TABULAR:
4549 tabular.rotate = false;
4552 case Tabular::TOGGLE_ROTATE_TABULAR:
4553 tabular.rotate = !tabular.rotate;
4556 case Tabular::SET_ROTATE_CELL:
4557 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4558 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4559 tabular.setRotateCell(
4560 tabular.cellIndex(i, j), true);
4563 case Tabular::UNSET_ROTATE_CELL:
4564 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4565 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4566 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4569 case Tabular::TOGGLE_ROTATE_CELL:
4571 bool oneNotRotated = oneCellHasRotationState(false,
4572 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4574 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4575 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4576 tabular.setRotateCell(tabular.cellIndex(i, j),
4581 case Tabular::SET_USEBOX: {
4582 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4583 if (val == tabular.getUsebox(cur.idx()))
4584 val = Tabular::BOX_NONE;
4585 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4586 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4587 tabular.setUsebox(tabular.cellIndex(i, j), val);
4591 case Tabular::UNSET_LTFIRSTHEAD:
4593 case Tabular::SET_LTFIRSTHEAD:
4594 tabular.getRowOfLTFirstHead(row, ltt);
4595 checkLongtableSpecial(ltt, value, flag);
4596 tabular.setLTHead(row, flag, ltt, true);
4599 case Tabular::UNSET_LTHEAD:
4601 case Tabular::SET_LTHEAD:
4602 tabular.getRowOfLTHead(row, ltt);
4603 checkLongtableSpecial(ltt, value, flag);
4604 tabular.setLTHead(row, flag, ltt, false);
4607 case Tabular::UNSET_LTFOOT:
4609 case Tabular::SET_LTFOOT:
4610 tabular.getRowOfLTFoot(row, ltt);
4611 checkLongtableSpecial(ltt, value, flag);
4612 tabular.setLTFoot(row, flag, ltt, false);
4615 case Tabular::UNSET_LTLASTFOOT:
4617 case Tabular::SET_LTLASTFOOT:
4618 tabular.getRowOfLTLastFoot(row, ltt);
4619 checkLongtableSpecial(ltt, value, flag);
4620 tabular.setLTFoot(row, flag, ltt, true);
4623 case Tabular::SET_LTNEWPAGE:
4624 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4627 case Tabular::TOGGLE_LTCAPTION:
4628 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4631 cur.setSelection(false);
4634 case Tabular::SET_BOOKTABS:
4635 tabular.use_booktabs = true;
4638 case Tabular::UNSET_BOOKTABS:
4639 tabular.use_booktabs = false;
4642 case Tabular::SET_TOP_SPACE: {
4644 if (value == "default")
4645 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4646 tabular.row_info[i].top_space_default = true;
4647 else if (isValidLength(value, &len))
4648 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4649 tabular.row_info[i].top_space_default = false;
4650 tabular.row_info[i].top_space = len;
4653 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4654 tabular.row_info[i].top_space_default = false;
4655 tabular.row_info[i].top_space = len;
4660 case Tabular::SET_BOTTOM_SPACE: {
4662 if (value == "default")
4663 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4664 tabular.row_info[i].bottom_space_default = true;
4665 else if (isValidLength(value, &len))
4666 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4667 tabular.row_info[i].bottom_space_default = false;
4668 tabular.row_info[i].bottom_space = len;
4671 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4672 tabular.row_info[i].bottom_space_default = false;
4673 tabular.row_info[i].bottom_space = len;
4678 case Tabular::SET_INTERLINE_SPACE: {
4680 if (value == "default")
4681 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4682 tabular.row_info[i].interline_space_default = true;
4683 else if (isValidLength(value, &len))
4684 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4685 tabular.row_info[i].interline_space_default = false;
4686 tabular.row_info[i].interline_space = len;
4689 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4690 tabular.row_info[i].interline_space_default = false;
4691 tabular.row_info[i].interline_space = len;
4696 // dummy stuff just to avoid warnings
4697 case Tabular::LAST_ACTION:
4703 bool InsetTabular::showInsetDialog(BufferView * bv) const
4705 bv->showDialog("tabular", params2string(*this),
4706 const_cast<InsetTabular *>(this));
4711 void InsetTabular::openLayoutDialog(BufferView * bv) const
4713 bv->showDialog("tabular", params2string(*this),
4714 const_cast<InsetTabular *>(this));
4718 bool InsetTabular::copySelection(Cursor & cur)
4720 if (!cur.selection())
4725 getSelection(cur, rs, re, cs, ce);
4727 paste_tabular.reset(new Tabular(tabular));
4729 for (row_type i = 0; i < rs; ++i)
4730 paste_tabular->deleteRow(0);
4732 row_type const rows = re - rs + 1;
4733 while (paste_tabular->row_info.size() > rows)
4734 paste_tabular->deleteRow(rows);
4736 for (col_type i = 0; i < cs; ++i)
4737 paste_tabular->deleteColumn(0);
4739 col_type const columns = ce - cs + 1;
4740 while (paste_tabular->column_info.size() > columns)
4741 paste_tabular->deleteColumn(columns);
4743 paste_tabular->setBuffer(tabular.buffer());
4745 odocstringstream os;
4746 OutputParams const runparams(0);
4747 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4748 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4749 cap::copySelection(cur, os.str());
4751 // mark tabular stack dirty
4752 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4753 // when we (hopefully) have a one-for-all paste mechanism.
4754 // This must be called after cap::copySelection.
4755 dirtyTabularStack(true);
4761 bool InsetTabular::pasteClipboard(Cursor & cur)
4765 col_type const actcol = tabular.cellColumn(cur.idx());
4766 row_type const actrow = tabular.cellRow(cur.idx());
4767 for (row_type r1 = 0, r2 = actrow;
4768 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4770 for (col_type c1 = 0, c2 = actcol;
4771 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4773 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4774 tabular.isPartOfMultiColumn(r2, c2))
4776 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4780 if (tabular.isPartOfMultiColumn(r2, c2)) {
4784 shared_ptr<InsetTableCell> inset(
4785 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4786 tabular.setCellInset(r2, c2, inset);
4787 // FIXME: change tracking (MG)
4788 inset->setChange(Change(buffer().params().trackChanges ?
4789 Change::INSERTED : Change::UNCHANGED));
4797 void InsetTabular::cutSelection(Cursor & cur)
4799 if (!cur.selection())
4804 getSelection(cur, rs, re, cs, ce);
4805 for (row_type i = rs; i <= re; ++i) {
4806 for (col_type j = cs; j <= ce; ++j) {
4807 shared_ptr<InsetTableCell> t
4808 = cell(tabular.cellIndex(i, j));
4809 if (buffer().params().trackChanges)
4810 // FIXME: Change tracking (MG)
4811 t->setChange(Change(Change::DELETED));
4817 // cursor position might be invalid now
4818 if (cur.pit() > cur.lastpit())
4819 cur.pit() = cur.lastpit();
4820 if (cur.pos() > cur.lastpos())
4821 cur.pos() = cur.lastpos();
4822 cur.clearSelection();
4826 bool InsetTabular::isRightToLeft(Cursor & cur) const
4828 LASSERT(cur.depth() > 1, /**/);
4829 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4830 pos_type const parentpos = cur[cur.depth() - 2].pos();
4831 return parentpar.getFontSettings(buffer().params(),
4832 parentpos).language()->rightToLeft();
4835 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
4838 LASSERT(stidx <= enidx, return docstring());
4840 col_type const col1 = tabular.cellColumn(stidx);
4841 col_type const col2 = tabular.cellColumn(enidx);
4842 row_type const row1 = tabular.cellRow(stidx);
4843 row_type const row2 = tabular.cellRow(enidx);
4845 for (col_type col = col1; col <= col2; col++)
4846 for (row_type row = row1; row <= row2; row++) {
4851 retval += tabular.cellInset(row, col)->asString(intoInsets);
4857 void InsetTabular::getSelection(Cursor & cur,
4858 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4860 CursorSlice const & beg = cur.selBegin();
4861 CursorSlice const & end = cur.selEnd();
4862 cs = tabular.cellColumn(beg.idx());
4863 ce = tabular.cellColumn(end.idx());
4866 cs = tabular.cellColumn(end.idx());
4868 ce = tabular.cellRightColumn(end.idx());
4871 rs = tabular.cellRow(beg.idx());
4872 re = tabular.cellRow(end.idx());
4878 Text * InsetTabular::getText(int idx) const
4880 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4884 void InsetTabular::setChange(Change const & change)
4886 for (idx_type idx = 0; idx < nargs(); ++idx)
4887 cell(idx)->setChange(change);
4891 void InsetTabular::acceptChanges(BufferParams const & bparams)
4893 for (idx_type idx = 0; idx < nargs(); ++idx)
4894 cell(idx)->acceptChanges(bparams);
4898 void InsetTabular::rejectChanges(BufferParams const & bparams)
4900 for (idx_type idx = 0; idx < nargs(); ++idx)
4901 cell(idx)->rejectChanges(bparams);
4905 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4907 return tabular.getPWidth(cell).zero();
4911 bool InsetTabular::forcePlainLayout(idx_type cell) const
4913 return !tabular.getPWidth(cell).zero();
4917 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4920 if (buf.length() <= 0)
4925 col_type maxCols = 1;
4926 size_t const len = buf.length();
4930 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4938 maxCols = max(cols, maxCols);
4944 maxCols = max(cols, maxCols);
4950 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4951 loctab = paste_tabular.get();
4953 dirtyTabularStack(true);
4956 cell = bv.cursor().idx();
4957 ocol = tabular.cellColumn(cell);
4958 row = tabular.cellRow(cell);
4962 idx_type const cells = loctab->numberofcells;
4965 rows = loctab->row_info.size();
4966 col_type const columns = loctab->column_info.size();
4968 while (cell < cells && p < len && row < rows &&
4969 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4975 // we can only set this if we are not too far right
4976 if (cols < columns) {
4977 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4978 Font const font = bv.textMetrics(&inset->text()).
4980 inset->setText(buf.substr(op, p - op), font,
4981 buffer().params().trackChanges);
4987 // we can only set this if we are not too far right
4988 if (cols < columns) {
4989 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4990 Font const font = bv.textMetrics(&inset->text()).
4992 inset->setText(buf.substr(op, p - op), font,
4993 buffer().params().trackChanges);
4998 cell = loctab->cellIndex(row, cols);
5004 // check for the last cell if there is no trailing '\n'
5005 if (cell < cells && op < len) {
5006 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5007 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5008 inset->setText(buf.substr(op, len - op), font,
5009 buffer().params().trackChanges);
5015 void InsetTabular::addPreview(PreviewLoader & loader) const
5017 row_type const rows = tabular.row_info.size();
5018 col_type const columns = tabular.column_info.size();
5019 for (row_type i = 0; i < rows; ++i) {
5020 for (col_type j = 0; j < columns; ++j)
5021 tabular.cellInset(i, j)->addPreview(loader);
5026 bool InsetTabular::completionSupported(Cursor const & cur) const
5028 Cursor const & bvCur = cur.bv().cursor();
5029 if (&bvCur.inset() != this)
5031 return cur.text()->completionSupported(cur);
5035 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5037 return completionSupported(cur);
5041 bool InsetTabular::automaticInlineCompletion() const
5043 return lyxrc.completion_inline_text;
5047 bool InsetTabular::automaticPopupCompletion() const
5049 return lyxrc.completion_popup_text;
5053 bool InsetTabular::showCompletionCursor() const
5055 return lyxrc.completion_cursor_text;
5059 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5061 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5065 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5067 if (!completionSupported(cur))
5069 return cur.text()->completionPrefix(cur);
5073 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5075 if (!completionSupported(cur))
5078 return cur.text()->insertCompletion(cur, s, finished);
5082 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5083 Dimension & dim) const
5085 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5086 tm.completionPosAndDim(cur, x, y, dim);
5090 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5092 istringstream data(in);
5094 lex.setStream(data);
5101 if (!lex || token != "tabular") {
5102 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5106 // This is part of the inset proper that is usually swallowed
5107 // by Buffer::readInset
5109 if (!lex || token != "Tabular") {
5110 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5118 string InsetTabular::params2string(InsetTabular const & inset)
5121 data << "tabular" << ' ';
5123 data << "\\end_inset\n";