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 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
73 using boost::shared_ptr;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2; // in cell
92 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
93 int const default_line_space = 10; // ?
94 int const WIDTH_OF_LINE = 5; // space between double lines
98 boost::scoped_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 { Tabular::APPEND_ROW, "append-row" },
110 { Tabular::APPEND_COLUMN, "append-column" },
111 { Tabular::DELETE_ROW, "delete-row" },
112 { Tabular::DELETE_COLUMN, "delete-column" },
113 { Tabular::COPY_ROW, "copy-row" },
114 { Tabular::COPY_COLUMN, "copy-column" },
115 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
116 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
117 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
118 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
119 { Tabular::ALIGN_LEFT, "align-left" },
120 { Tabular::ALIGN_RIGHT, "align-right" },
121 { Tabular::ALIGN_CENTER, "align-center" },
122 { Tabular::ALIGN_BLOCK, "align-block" },
123 { Tabular::VALIGN_TOP, "valign-top" },
124 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
125 { Tabular::VALIGN_MIDDLE, "valign-middle" },
126 { Tabular::M_ALIGN_LEFT, "m-align-left" },
127 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
128 { Tabular::M_ALIGN_CENTER, "m-align-center" },
129 { Tabular::M_VALIGN_TOP, "m-valign-top" },
130 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
131 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
132 { Tabular::MULTICOLUMN, "multicolumn" },
133 { Tabular::MULTIROW, "multirow" },
134 { Tabular::SET_ALL_LINES, "set-all-lines" },
135 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
136 { Tabular::SET_LONGTABULAR, "set-longtabular" },
137 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
138 { Tabular::SET_PWIDTH, "set-pwidth" },
139 { Tabular::SET_MPWIDTH, "set-mpwidth" },
140 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
141 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
142 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
143 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
144 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
145 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
146 { Tabular::SET_USEBOX, "set-usebox" },
147 { Tabular::SET_LTHEAD, "set-lthead" },
148 { Tabular::UNSET_LTHEAD, "unset-lthead" },
149 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
150 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
151 { Tabular::SET_LTFOOT, "set-ltfoot" },
152 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
153 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
154 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
155 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
156 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption" },
157 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
158 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn" },
159 { Tabular::SET_SPECIAL_MULTIROW, "set-special-multirow" },
160 { Tabular::SET_BOOKTABS, "set-booktabs" },
161 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
162 { Tabular::SET_TOP_SPACE, "set-top-space" },
163 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
164 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
165 { Tabular::SET_BORDER_LINES, "set-border-lines" },
166 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top"},
167 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle"},
168 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom"},
169 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left" },
170 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center" },
171 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right" },
172 { Tabular::LAST_ACTION, "" }
176 class FeatureEqual : public unary_function<TabularFeature, bool> {
178 FeatureEqual(Tabular::Feature feature)
179 : feature_(feature) {}
180 bool operator()(TabularFeature const & tf) const {
181 return tf.action == feature_;
184 Tabular::Feature feature_;
189 string const write_attribute(string const & name, T const & t)
191 string const s = tostr(t);
192 return s.empty() ? s : " " + name + "=\"" + s + "\"";
196 string const write_attribute(string const & name, string const & t)
198 return t.empty() ? t : " " + name + "=\"" + t + "\"";
203 string const write_attribute(string const & name, docstring const & t)
205 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
210 string const write_attribute(string const & name, bool const & b)
212 // we write only true attribute values so we remove a bit of the
213 // file format bloat for tabulars.
214 return b ? write_attribute(name, convert<string>(b)) : string();
219 string const write_attribute(string const & name, int 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, Tabular::idx_type const & i)
230 // we write only true attribute values so we remove a bit of the
231 // file format bloat for tabulars.
232 return i ? write_attribute(name, convert<string>(i)) : string();
237 string const write_attribute(string const & name, Length const & value)
239 // we write only the value if we really have one same reson as above.
240 return value.zero() ? string() : write_attribute(name, value.asString());
244 string const tostr(LyXAlignment const & num)
249 case LYX_ALIGN_BLOCK:
253 case LYX_ALIGN_CENTER:
255 case LYX_ALIGN_RIGHT:
257 case LYX_ALIGN_LAYOUT:
259 case LYX_ALIGN_SPECIAL:
266 string const tostr(Tabular::HAlignment const & num)
269 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
271 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
273 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
280 string const tostr(Tabular::VAlignment const & num)
283 case Tabular::LYX_VALIGN_TOP:
285 case Tabular::LYX_VALIGN_MIDDLE:
287 case Tabular::LYX_VALIGN_BOTTOM:
294 string const tostr(Tabular::BoxType const & num)
297 case Tabular::BOX_NONE:
299 case Tabular::BOX_PARBOX:
301 case Tabular::BOX_MINIPAGE:
308 // I would have liked a fromstr template a lot better. (Lgb)
309 bool string2type(string const str, LyXAlignment & num)
312 num = LYX_ALIGN_NONE;
313 else if (str == "block")
314 num = LYX_ALIGN_BLOCK;
315 else if (str == "left")
316 num = LYX_ALIGN_LEFT;
317 else if (str == "center")
318 num = LYX_ALIGN_CENTER;
319 else if (str == "right")
320 num = LYX_ALIGN_RIGHT;
327 bool string2type(string const str, Tabular::HAlignment & num)
330 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
331 else if (str == "center" )
332 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
333 else if (str == "right")
334 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
341 bool string2type(string const str, Tabular::VAlignment & num)
344 num = Tabular::LYX_VALIGN_TOP;
345 else if (str == "middle" )
346 num = Tabular::LYX_VALIGN_MIDDLE;
347 else if (str == "bottom")
348 num = Tabular::LYX_VALIGN_BOTTOM;
355 bool string2type(string const str, Tabular::BoxType & num)
358 num = Tabular::BOX_NONE;
359 else if (str == "parbox")
360 num = Tabular::BOX_PARBOX;
361 else if (str == "minipage")
362 num = Tabular::BOX_MINIPAGE;
369 bool string2type(string const str, bool & num)
373 else if (str == "false")
381 bool getTokenValue(string const & str, char const * token, string & ret)
384 size_t token_length = strlen(token);
385 size_t pos = str.find(token);
387 if (pos == string::npos || pos + token_length + 1 >= str.length()
388 || str[pos + token_length] != '=')
390 pos += token_length + 1;
392 if (ch != '"' && ch != '\'') { // only read till next space
396 while (pos < str.length() - 1 && str[++pos] != ch)
403 bool getTokenValue(string const & str, char const * token, docstring & ret)
406 bool const success = getTokenValue(str, token, tmp);
407 ret = from_utf8(tmp);
412 bool getTokenValue(string const & str, char const * token, int & num)
416 if (!getTokenValue(str, token, tmp))
418 num = convert<int>(tmp);
423 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
426 return getTokenValue(str, token, tmp) && string2type(tmp, num);
430 bool getTokenValue(string const & str, char const * token,
431 Tabular::HAlignment & num)
434 return getTokenValue(str, token, tmp) && string2type(tmp, num);
438 bool getTokenValue(string const & str, char const * token,
439 Tabular::VAlignment & num)
442 return getTokenValue(str, token, tmp) && string2type(tmp, num);
446 bool getTokenValue(string const & str, char const * token,
447 Tabular::BoxType & num)
450 return getTokenValue(str, token, tmp) && string2type(tmp, num);
454 bool getTokenValue(string const & str, char const * token, bool & flag)
456 // set the flag always to false as this should be the default for bools
457 // not in the file-format.
460 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
464 bool getTokenValue(string const & str, char const * token, Length & len)
466 // set the length to be zero() as default as this it should be if not
467 // in the file format.
470 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
474 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
479 if (!getTokenValue(str, token, tmp))
481 if (tmp == "default") {
485 return isValidLength(tmp, &len);
489 void l_getline(istream & is, string & str)
492 while (str.empty()) {
494 if (!str.empty() && str[str.length() - 1] == '\r')
495 str.erase(str.length() - 1);
502 string const featureAsString(Tabular::Feature feature)
504 TabularFeature * end = tabularFeature +
505 sizeof(tabularFeature) / sizeof(TabularFeature);
506 TabularFeature * it = find_if(tabularFeature, end,
507 FeatureEqual(feature));
508 return (it == end) ? string() : it->feature;
513 /////////////////////////////////////////////////////////////////////
517 /////////////////////////////////////////////////////////////////////
520 Tabular::CellData::CellData(Buffer * buf)
523 multicolumn(Tabular::CELL_NORMAL),
524 multirow(Tabular::CELL_NORMAL),
525 alignment(LYX_ALIGN_CENTER),
526 valignment(LYX_VALIGN_TOP),
533 inset(new InsetTableCell(buf))
535 inset->setBuffer(*buf);
539 Tabular::CellData::CellData(CellData const & cs)
542 multicolumn(cs.multicolumn),
543 multirow(cs.multirow),
544 alignment(cs.alignment),
545 valignment(cs.valignment),
546 top_line(cs.top_line),
547 bottom_line(cs.bottom_line),
548 left_line(cs.left_line),
549 right_line(cs.right_line),
552 align_special(cs.align_special),
554 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
558 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
564 void Tabular::CellData::swap(CellData & rhs)
566 std::swap(cellno, rhs.cellno);
567 std::swap(width, rhs.width);
568 std::swap(multicolumn, rhs.multicolumn);
569 std::swap(multirow, rhs.multirow);
570 std::swap(alignment, rhs.alignment);
571 std::swap(valignment, rhs.valignment);
572 std::swap(top_line, rhs.top_line);
573 std::swap(bottom_line, rhs.bottom_line);
574 std::swap(left_line, rhs.left_line);
575 std::swap(right_line, rhs.right_line);
576 std::swap(usebox, rhs.usebox);
577 std::swap(rotate, rhs.rotate);
578 std::swap(align_special, rhs.align_special);
579 p_width.swap(rhs.p_width);
580 inset.swap(rhs.inset);
584 Tabular::RowData::RowData()
587 top_space_default(false),
588 bottom_space_default(false),
589 interline_space_default(false),
599 Tabular::ColumnData::ColumnData()
600 : alignment(LYX_ALIGN_CENTER),
601 valignment(LYX_VALIGN_TOP),
607 Tabular::ltType::ltType()
614 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
616 init(buffer, rows_arg, columns_arg);
620 void Tabular::setBuffer(Buffer & buffer)
623 row_type const nrows = row_info.size();
624 col_type const ncols = column_info.size();
625 for (row_type i = 0; i < nrows; ++i)
626 for (col_type j = 0; j < ncols; ++j)
627 cell_info[i][j].inset->setBuffer(*buffer_);
631 // activates all lines and sets all widths to 0
632 void Tabular::init(Buffer * buf, row_type rows_arg,
633 col_type columns_arg)
636 row_info = row_vector(rows_arg);
637 column_info = column_vector(columns_arg);
638 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
639 row_info.reserve(10);
640 column_info.reserve(10);
641 cell_info.reserve(100);
643 is_long_tabular = false;
644 tabular_valignment = LYX_VALIGN_MIDDLE;
645 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
647 use_booktabs = false;
648 size_t row_count = row_info.size();
649 size_t column_count = column_info.size();
650 // set silly default lines
651 for (row_type i = 0; i < row_count; ++i)
652 for (col_type j = 0; j < column_count; ++j) {
653 cell_info[i][j].inset->setBuffer(*buffer_);
654 cell_info[i][j].top_line = true;
655 cell_info[i][j].left_line = true;
656 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
657 cell_info[i][j].right_line = j == column_count - 1;
662 void Tabular::appendRow(idx_type const cell)
664 row_type const row = cellRow(cell);
666 row_info.insert(row_info.begin() + row + 1, RowData());
667 row_info[row + 1] = row_info[row];
669 col_type const ncols = column_info.size();
670 cell_info.insert(cell_info.begin() + row + 1,
671 cell_vector(ncols, CellData(buffer_)));
672 for (col_type c = 0; c < ncols; ++c) {
673 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
674 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
676 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
680 for (col_type c = 0; c < ncols; ++c) {
681 if (isPartOfMultiRow(row, c))
683 // inherit line settings
684 idx_type const i = cellIndex(row + 1, c);
685 idx_type const j = cellIndex(row, c);
686 setLeftLine(i, leftLine(j));
687 setRightLine(i, rightLine(j));
688 setTopLine(i, topLine(j));
689 if (topLine(j) && bottomLine(j)) {
690 setBottomLine(i, true);
691 setBottomLine(j, false);
693 // mark track changes
694 if (buffer().params().trackChanges)
695 cellInfo(i).inset->setChange(Change(Change::INSERTED));
700 void Tabular::deleteRow(row_type const row)
702 // Not allowed to delete last row
703 if (row_info.size() == 1)
706 size_t const column_count = column_info.size();
707 for (col_type i = 0; i < column_count; ++i) {
708 // Care about multirow cells
709 if (row + 1 < row_info.size() &&
710 cell_info[row][i].multirow == CELL_BEGIN_OF_MULTIROW &&
711 cell_info[row][i + 1].multirow == CELL_PART_OF_MULTIROW) {
712 cell_info[row][i + 1].multirow = CELL_BEGIN_OF_MULTIROW;
715 row_info.erase(row_info.begin() + row);
716 cell_info.erase(cell_info.begin() + row);
721 void Tabular::copyRow(row_type const row)
723 row_info.insert(row_info.begin() + row, row_info[row]);
724 cell_info.insert(cell_info.begin() + row, cell_info[row]);
726 if (buffer().params().trackChanges)
727 for (col_type j = 0; j < column_info.size(); ++j)
728 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
734 void Tabular::appendColumn(idx_type const cell)
736 col_type const c = cellColumn(cell);
738 column_info.insert(column_info.begin() + c + 1, ColumnData());
739 column_info[c + 1] = column_info[c];
741 row_type const nrows = row_info.size();
742 for (row_type r = 0; r < nrows; ++r) {
743 cell_info[r].insert(cell_info[r].begin() + c + 1,
745 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
746 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
748 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
751 for (row_type r = 0; r < nrows; ++r) {
752 // inherit line settings
753 idx_type const i = cellIndex(r, c + 1);
754 idx_type const j = cellIndex(r, c);
755 setBottomLine(i, bottomLine(j));
756 setTopLine(i, topLine(j));
757 setLeftLine(i, leftLine(j));
758 if (rightLine(j) && rightLine(j)) {
759 setRightLine(i, true);
760 setRightLine(j, false);
762 if (buffer().params().trackChanges)
763 cellInfo(i).inset->setChange(Change(Change::INSERTED));
768 void Tabular::deleteColumn(col_type const col)
770 // Not allowed to delete last column
771 if (column_info.size() == 1)
774 row_type const nrows = row_info.size();
775 for (row_type r = 0; r < nrows; ++r) {
776 // Care about multicolumn cells
777 if (col + 1 < column_info.size() &&
778 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
779 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
780 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
782 cell_info[r].erase(cell_info[r].begin() + col);
784 column_info.erase(column_info.begin() + col);
789 void Tabular::copyColumn(col_type const col)
791 BufferParams const & bp = buffer().params();
792 column_info.insert(column_info.begin() + col, column_info[col]);
794 row_type nrows = row_info.size();
795 for (row_type r = 0; r < nrows; ++r) {
796 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
798 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
804 void Tabular::updateIndexes()
807 col_type ncols = column_info.size();
808 row_type nrows = row_info.size();
810 for (row_type row = 0; row < nrows; ++row)
811 for (col_type column = 0; column < ncols; ++column) {
812 if (!isPartOfMultiColumn(row, column)
813 && !isPartOfMultiRow(row, column))
815 if (isPartOfMultiRow(row, column))
816 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
818 cell_info[row][column].cellno = numberofcells - 1;
821 rowofcell.resize(numberofcells);
822 columnofcell.resize(numberofcells);
824 for (row_type row = 0; row < nrows; ++row)
825 for (col_type column = 0; column < ncols; ++column) {
826 if (isPartOfMultiColumn(row, column)
827 || isPartOfMultiRow(row, column))
830 columnofcell[i] = column;
831 setFixedWidth(row, column);
832 updateContentAlignment(row, column);
838 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
840 row_type const row = cellRow(cell);
842 for (col_type i = 0; i < column_info.size(); ++i)
843 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
849 bool Tabular::topLine(idx_type const cell) const
851 return cellInfo(cell).top_line;
855 bool Tabular::bottomLine(idx_type const cell) const
857 return cellInfo(cell).bottom_line;
861 bool Tabular::leftLine(idx_type cell) const
865 return cellInfo(cell).left_line;
869 bool Tabular::rightLine(idx_type cell) const
873 return cellInfo(cell).right_line;
877 bool Tabular::topAlreadyDrawn(idx_type cell) const
879 row_type const row = cellRow(cell);
883 return !rowTopLine(row) && bottomLine(cellAbove(cell));
887 bool Tabular::leftAlreadyDrawn(idx_type cell) const
889 col_type col = cellColumn(cell);
892 idx_type i = cellIndex(cellRow(cell), col - 1);
893 return rightLine(i) && !leftLine(cell);
897 bool Tabular::isLastRow(idx_type cell) const
899 return cellRow(cell) == row_info.size() - 1;
903 int Tabular::interRowSpace(row_type row) const
905 if (!row || row >= row_info.size())
908 int const interline_space = row_info[row - 1].interline_space_default ?
910 row_info[row - 1].interline_space.inPixels(width());
911 if (rowTopLine(row) && rowBottomLine(row - 1))
912 return interline_space + WIDTH_OF_LINE;
913 return interline_space;
917 int Tabular::interColumnSpace(idx_type cell) const
919 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
920 if (rightLine(cell) && nextcol < column_info.size()
921 && leftLine(cellIndex(cellRow(cell), nextcol)))
922 return WIDTH_OF_LINE;
927 int Tabular::columnWidth(idx_type cell) const
930 col_type const span = columnSpan(cell);
931 col_type const col = cellColumn(cell);
932 for(col_type c = col; c < col + span ; ++c)
933 w += column_info[c].width;
938 int Tabular::rowHeight(idx_type cell) const
940 row_type const span = rowSpan(cell);
941 row_type const row = cellRow(cell);
942 int h = rowAscent(row) + rowDescent(row);
944 for(row_type r = row; r < row + span ; ++r) {
947 h += interRowSpace(r);
949 if (r < row + span - 1)
956 bool Tabular::updateColumnWidths()
958 col_type const ncols = column_info.size();
959 row_type const nrows = row_info.size();
961 // for each col get max of single col cells
962 for(col_type c = 0; c < ncols; ++c) {
964 for(row_type r = 0; r < nrows; ++r) {
965 idx_type const i = cellIndex(r, c);
966 if (columnSpan(i) == 1)
967 new_width = max(new_width, cellInfo(i).width);
970 if (column_info[c].width != new_width) {
971 column_info[c].width = new_width;
975 // update col widths to fit merged cells
976 for(col_type c = 0; c < ncols; ++c)
977 for(row_type r = 0; r < nrows; ++r) {
978 idx_type const i = cellIndex(r, c);
979 int const span = columnSpan(i);
980 if (span == 1 || c > cellColumn(i))
984 for(col_type j = c; j < c + span ; ++j)
985 old_width += column_info[j].width;
987 if (cellInfo(i).width > old_width) {
988 column_info[c + span - 1].width += cellInfo(i).width - old_width;
997 int Tabular::width() const
999 col_type const ncols = column_info.size();
1001 for (col_type i = 0; i < ncols; ++i)
1002 width += column_info[i].width;
1007 void Tabular::setCellWidth(idx_type cell, int new_width)
1009 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
1010 + interColumnSpace(cell);
1014 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1017 if (!isMultiColumn(cell) || onlycolumn)
1018 column_info[cellColumn(cell)].alignment = align;
1020 cellInfo(cell).alignment = align;
1021 cellInset(cell).get()->setContentAlignment(align);
1025 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1028 if (!isMultiColumn(cell) || onlycolumn)
1029 column_info[cellColumn(cell)].valignment = align;
1031 cellInfo(cell).valignment = align;
1038 * Allow line and paragraph breaks for fixed width cells or disallow them,
1039 * merge cell paragraphs and reset layout to standard for variable width
1042 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1044 inset->setAutoBreakRows(fixedWidth);
1045 inset->toggleFixedWidth(fixedWidth);
1049 // merge all paragraphs to one
1050 BufferParams const & bp = cur.bv().buffer().params();
1051 while (inset->paragraphs().size() > 1)
1052 mergeParagraph(bp, inset->paragraphs(), 0);
1056 // undo information has already been recorded
1057 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1058 bp.documentClass().plainLayoutName());
1065 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1066 Length const & width)
1068 col_type const j = cellColumn(cell);
1070 column_info[j].p_width = width;
1071 for (row_type i = 0; i < row_info.size(); ++i) {
1072 idx_type const cell = cellIndex(i, j);
1073 // because of multicolumns
1074 toggleFixedWidth(cur, cellInset(cell).get(),
1075 !getPWidth(cell).zero());
1077 // cur paragraph can become invalid after paragraphs were merged
1078 if (cur.pit() > cur.lastpit())
1079 cur.pit() = cur.lastpit();
1080 // cur position can become invalid after newlines were removed
1081 if (cur.pos() > cur.lastpos())
1082 cur.pos() = cur.lastpos();
1086 bool Tabular::setFixedWidth(row_type r, col_type c)
1088 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1089 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1090 || (multicol && !cell_info[r][c].p_width.zero());
1091 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1096 void Tabular::updateContentAlignment(row_type r, col_type c)
1098 cell_info[r][c].inset->setContentAlignment(
1099 getAlignment(cellIndex(r, c)));
1103 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1104 Length const & width)
1106 if (!isMultiColumn(cell))
1109 cellInfo(cell).p_width = width;
1110 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1111 // cur paragraph can become invalid after paragraphs were merged
1112 if (cur.pit() > cur.lastpit())
1113 cur.pit() = cur.lastpit();
1114 // cur position can become invalid after newlines were removed
1115 if (cur.pos() > cur.lastpos())
1116 cur.pos() = cur.lastpos();
1121 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1122 Tabular::Feature what)
1124 if (what == SET_SPECIAL_MULTICOLUMN)
1125 cellInfo(cell).align_special = special;
1127 column_info[cellColumn(cell)].align_special = special;
1131 void Tabular::setAllLines(idx_type cell, bool line)
1133 setTopLine(cell, line);
1134 setBottomLine(cell, line);
1135 setRightLine(cell, line);
1136 setLeftLine(cell, line);
1140 void Tabular::setTopLine(idx_type i, bool line)
1142 cellInfo(i).top_line = line;
1146 void Tabular::setBottomLine(idx_type i, bool line)
1148 cellInfo(i).bottom_line = line;
1152 void Tabular::setLeftLine(idx_type cell, bool line)
1154 cellInfo(cell).left_line = line;
1158 void Tabular::setRightLine(idx_type cell, bool line)
1160 cellInfo(cell).right_line = line;
1163 bool Tabular::rowTopLine(row_type r) const
1165 col_type const ncols = column_info.size();
1166 bool all_rows_set = true;
1167 for (col_type c = 0; all_rows_set && c < ncols; ++c) {
1168 idx_type const i = cellIndex(r, c);
1169 all_rows_set = cellInfo(i).top_line;
1171 return all_rows_set;
1175 bool Tabular::rowBottomLine(row_type r) const
1177 col_type const ncols = column_info.size();
1178 bool all_rows_set = true;
1179 for (col_type c = 0; all_rows_set && c < ncols; ++c) {
1180 idx_type const i = cellIndex(r, c);
1181 all_rows_set = cellInfo(i).bottom_line;
1183 return all_rows_set;
1187 bool Tabular::columnLeftLine(col_type c) const
1194 row_type const nrows = row_info.size();
1195 for (row_type r = 0; r < nrows; ++r) {
1196 idx_type i = cellIndex(r, c);
1197 if (c == cellColumn(i)) {
1199 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1200 if (cellInfo(i).left_line || right)
1204 return 2 * nrows_left >= total;
1208 bool Tabular::columnRightLine(col_type c) const
1213 int nrows_right = 0;
1215 row_type const nrows = row_info.size();
1216 for (row_type r = 0; r < nrows; ++r) {
1217 idx_type i = cellIndex(r, c);
1218 if (c == cellColumn(i) + columnSpan(i) - 1) {
1220 bool left = (c + 1 < column_info.size()
1221 && cellInfo(cellIndex(r, c + 1)).left_line)
1222 || c + 1 == column_info.size();
1223 if (cellInfo(i).right_line && left)
1227 return 2 * nrows_right >= total;
1231 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1233 if (!onlycolumn && isMultiColumn(cell))
1234 return cellInfo(cell).alignment;
1235 return column_info[cellColumn(cell)].alignment;
1240 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1242 if (!onlycolumn && isMultiColumn(cell))
1243 return cellInfo(cell).valignment;
1244 return column_info[cellColumn(cell)].valignment;
1248 Length const Tabular::getPWidth(idx_type cell) const
1250 if (isMultiColumn(cell))
1251 return cellInfo(cell).p_width;
1252 return column_info[cellColumn(cell)].p_width;
1256 int Tabular::cellWidth(idx_type cell) const
1258 return cellInfo(cell).width;
1262 int Tabular::textHOffset(idx_type cell) const
1264 // the LaTeX Way :-(
1265 int x = WIDTH_OF_LINE;
1267 switch (getAlignment(cell)) {
1268 case LYX_ALIGN_CENTER:
1269 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1271 case LYX_ALIGN_RIGHT:
1272 x += columnWidth(cell) - cellWidth(cell);
1273 // + interColumnSpace(cell);
1276 // LYX_ALIGN_LEFT: nothing :-)
1284 int Tabular::textVOffset(idx_type cell) const
1286 int h = rowHeight(cell);
1289 switch (getVAlignment(cell)) {
1290 case LYX_VALIGN_TOP:
1292 case LYX_VALIGN_MIDDLE:
1295 case LYX_VALIGN_BOTTOM:
1304 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1306 if (row > row_info.size() - 1)
1307 row = row_info.size() - 1;
1310 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1313 return cell_info[row][0].cellno;
1317 bool Tabular::isLastCellInRow(idx_type cell) const
1319 return cellRightColumn(cell) == column_info.size() - 1;
1323 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1325 if (row > row_info.size() - 1)
1326 row = row_info.size() - 1;
1327 return cell_info[row][column_info.size() - 1].cellno;
1331 Tabular::row_type Tabular::cellRow(idx_type cell) const
1333 if (cell >= numberofcells)
1334 return row_info.size() - 1;
1337 return rowofcell[cell];
1341 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1343 if (cell >= numberofcells)
1344 return column_info.size() - 1;
1347 return columnofcell[cell];
1351 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1353 row_type const row = cellRow(cell);
1354 col_type column = cellColumn(cell);
1355 while (column < column_info.size() - 1 &&
1356 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1362 void Tabular::write(ostream & os) const
1366 << write_attribute("version", 3)
1367 << write_attribute("rows", row_info.size())
1368 << write_attribute("columns", column_info.size())
1370 // global longtable options
1372 << write_attribute("rotate", rotate)
1373 << write_attribute("booktabs", use_booktabs)
1374 << write_attribute("islongtable", is_long_tabular)
1375 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1376 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1377 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1378 << write_attribute("headTopDL", endhead.topDL)
1379 << write_attribute("headBottomDL", endhead.bottomDL)
1380 << write_attribute("footTopDL", endfoot.topDL)
1381 << write_attribute("footBottomDL", endfoot.bottomDL)
1382 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1383 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1384 << write_attribute("lastFootEmpty", endlastfoot.empty);
1385 // longtables cannot be aligned vertically
1386 if (!is_long_tabular)
1387 os << write_attribute("tabularvalignment", tabular_valignment);
1388 if (is_long_tabular)
1389 os << write_attribute("longtabularalignment",
1390 longtabular_alignment);
1392 for (col_type j = 0; j < column_info.size(); ++j) {
1394 << write_attribute("alignment", column_info[j].alignment)
1395 << write_attribute("valignment", column_info[j].valignment)
1396 << write_attribute("width", column_info[j].p_width.asString())
1397 << write_attribute("special", column_info[j].align_special)
1400 for (row_type i = 0; i < row_info.size(); ++i) {
1401 static const string def("default");
1403 if (row_info[i].top_space_default)
1404 os << write_attribute("topspace", def);
1406 os << write_attribute("topspace", row_info[i].top_space);
1407 if (row_info[i].bottom_space_default)
1408 os << write_attribute("bottomspace", def);
1410 os << write_attribute("bottomspace", row_info[i].bottom_space);
1411 if (row_info[i].interline_space_default)
1412 os << write_attribute("interlinespace", def);
1414 os << write_attribute("interlinespace", row_info[i].interline_space);
1415 os << write_attribute("endhead", row_info[i].endhead)
1416 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1417 << write_attribute("endfoot", row_info[i].endfoot)
1418 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1419 << write_attribute("newpage", row_info[i].newpage)
1420 << write_attribute("caption", row_info[i].caption)
1422 for (col_type j = 0; j < column_info.size(); ++j) {
1424 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1425 << write_attribute("multirow", cell_info[i][j].multirow)
1426 << write_attribute("alignment", cell_info[i][j].alignment)
1427 << write_attribute("valignment", cell_info[i][j].valignment)
1428 << write_attribute("topline", cell_info[i][j].top_line)
1429 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1430 << write_attribute("leftline", cell_info[i][j].left_line)
1431 << write_attribute("rightline", cell_info[i][j].right_line)
1432 << write_attribute("rotate", cell_info[i][j].rotate)
1433 << write_attribute("usebox", cell_info[i][j].usebox)
1434 << write_attribute("width", cell_info[i][j].p_width)
1435 << write_attribute("special", cell_info[i][j].align_special)
1437 os << "\\begin_inset ";
1438 cell_info[i][j].inset->write(os);
1439 os << "\n\\end_inset\n"
1444 os << "</lyxtabular>\n";
1448 void Tabular::read(Lexer & lex)
1451 istream & is = lex.getStream();
1453 l_getline(is, line);
1454 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1455 LASSERT(false, /**/);
1460 if (!getTokenValue(line, "version", version))
1462 LASSERT(version >= 2, /**/);
1465 if (!getTokenValue(line, "rows", rows_arg))
1468 if (!getTokenValue(line, "columns", columns_arg))
1470 init(buffer_, rows_arg, columns_arg);
1471 l_getline(is, line);
1472 if (!prefixIs(line, "<features")) {
1473 lyxerr << "Wrong tabular format (expected <features ...> got"
1474 << line << ')' << endl;
1477 getTokenValue(line, "rotate", rotate);
1478 getTokenValue(line, "booktabs", use_booktabs);
1479 getTokenValue(line, "islongtable", is_long_tabular);
1480 getTokenValue(line, "tabularvalignment", tabular_valignment);
1481 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1482 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1483 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1484 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1485 getTokenValue(line, "headTopDL", endhead.topDL);
1486 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1487 getTokenValue(line, "footTopDL", endfoot.topDL);
1488 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1489 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1490 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1491 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1493 for (col_type j = 0; j < column_info.size(); ++j) {
1495 if (!prefixIs(line,"<column")) {
1496 lyxerr << "Wrong tabular format (expected <column ...> got"
1497 << line << ')' << endl;
1500 getTokenValue(line, "alignment", column_info[j].alignment);
1501 getTokenValue(line, "valignment", column_info[j].valignment);
1502 getTokenValue(line, "width", column_info[j].p_width);
1503 getTokenValue(line, "special", column_info[j].align_special);
1506 for (row_type i = 0; i < row_info.size(); ++i) {
1507 l_getline(is, line);
1508 if (!prefixIs(line, "<row")) {
1509 lyxerr << "Wrong tabular format (expected <row ...> got"
1510 << line << ')' << endl;
1513 getTokenValue(line, "topspace", row_info[i].top_space,
1514 row_info[i].top_space_default);
1515 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1516 row_info[i].bottom_space_default);
1517 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1518 row_info[i].interline_space_default);
1519 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1520 getTokenValue(line, "endhead", row_info[i].endhead);
1521 getTokenValue(line, "endfoot", row_info[i].endfoot);
1522 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1523 getTokenValue(line, "newpage", row_info[i].newpage);
1524 getTokenValue(line, "caption", row_info[i].caption);
1525 for (col_type j = 0; j < column_info.size(); ++j) {
1526 l_getline(is, line);
1527 if (!prefixIs(line, "<cell")) {
1528 lyxerr << "Wrong tabular format (expected <cell ...> got"
1529 << line << ')' << endl;
1532 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1533 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1534 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1535 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1536 getTokenValue(line, "topline", cell_info[i][j].top_line);
1537 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1538 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1539 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1540 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1541 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1542 getTokenValue(line, "width", cell_info[i][j].p_width);
1544 getTokenValue(line, "special", cell_info[i][j].align_special);
1545 l_getline(is, line);
1546 if (prefixIs(line, "\\begin_inset")) {
1547 cell_info[i][j].inset->setBuffer(*buffer_);
1548 cell_info[i][j].inset->read(lex);
1549 l_getline(is, line);
1551 if (!prefixIs(line, "</cell>")) {
1552 lyxerr << "Wrong tabular format (expected </cell> got"
1553 << line << ')' << endl;
1557 l_getline(is, line);
1558 if (!prefixIs(line, "</row>")) {
1559 lyxerr << "Wrong tabular format (expected </row> got"
1560 << line << ')' << endl;
1564 while (!prefixIs(line, "</lyxtabular>")) {
1565 l_getline(is, line);
1571 bool Tabular::isMultiColumn(idx_type cell) const
1573 if (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1574 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN)
1581 bool Tabular::isMultiColumnReal(idx_type cell) const
1583 return cellColumn(cell) != cellRightColumn(cell) &&
1584 (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1585 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1589 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1591 return cell_info[cellRow(cell)][cellColumn(cell)];
1595 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1597 CellData & cs = cellInfo(cell);
1598 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1599 cs.alignment = column_info[cellColumn(cell)].alignment;
1600 setRightLine(cell, rightLine(cell + number - 1));
1601 for (idx_type i = 1; i < number; ++i) {
1602 CellData & cs1 = cellInfo(cell + i);
1603 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1604 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1611 bool Tabular::isMultiRow(idx_type cell) const
1613 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1614 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1618 void Tabular::setMultiRow(idx_type cell, idx_type number)
1620 idx_type const column = cellColumn(cell);
1621 idx_type row = cellRow(cell);
1622 idx_type const ncolumns = column_info.size();
1623 CellData & cs = cellInfo(cell);
1624 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1625 // FIXME: the horizontal alignment can only be changed for
1626 // the whole table, support for this needs to be implemented
1627 // assigning this to uwestoehr
1628 cs.valignment = LYX_VALIGN_MIDDLE;
1629 // set the bottom row of the last selected cell
1630 setBottomLine(cell, bottomLine(cell + (number - 1)*ncolumns));
1631 for (idx_type i = 1; i < number; ++i) {
1632 CellData & cs1 = cell_info[row + i][column];
1633 cs1.multirow = CELL_PART_OF_MULTIROW;
1634 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1641 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1643 row_type const row = cellRow(cell);
1644 col_type const ncols = column_info.size();
1645 col_type column = cellColumn(cell) + 1;
1646 while (column < ncols && isPartOfMultiColumn(row, column))
1649 return column - cellColumn(cell);
1653 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1655 row_type const nrows = row_info.size();
1656 col_type const column = cellColumn(cell);
1657 idx_type result = 1;
1658 col_type row = cellRow(cell) + 1;
1659 while (row < nrows && isPartOfMultiRow(row, column))
1662 return row - cellRow(cell);
1666 void Tabular::unsetMultiColumn(idx_type cell)
1668 row_type const row = cellRow(cell);
1669 col_type const column = cellColumn(cell);
1670 row_type const span = columnSpan(cell);
1671 for (col_type c = 0; c < span; ++c)
1672 cell_info[row][column + c].multicolumn = CELL_NORMAL;
1677 void Tabular::unsetMultiRow(idx_type cell)
1679 row_type const row = cellRow(cell);
1680 col_type const column = cellColumn(cell);
1681 row_type const span = rowSpan(cell);
1682 for (row_type r = 0; r < span; ++r)
1683 cell_info[row + r][column].multirow = CELL_NORMAL;
1688 void Tabular::setRotateCell(idx_type cell, bool flag)
1690 cellInfo(cell).rotate = flag;
1694 bool Tabular::getRotateCell(idx_type cell) const
1696 return cellInfo(cell).rotate;
1700 bool Tabular::needRotating() const
1704 for (row_type i = 0; i < row_info.size(); ++i)
1705 for (col_type j = 0; j < column_info.size(); ++j)
1706 if (cell_info[i][j].rotate)
1712 bool Tabular::isLastCell(idx_type cell) const
1714 if (cell + 1 < numberofcells)
1720 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1722 if (cellRow(cell) == 0)
1725 col_type const col = cellColumn(cell);
1726 row_type r = cellRow(cell) - 1;
1727 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1730 return cell_info[r][col].cellno;
1734 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1736 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1737 if (nextrow < row_info.size())
1738 return cell_info[nextrow][cellColumn(cell)].cellno;
1743 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1745 LASSERT(column != npos && column < column_info.size()
1746 && row != npos && row < row_info.size(), /**/);
1747 return cell_info[row][column].cellno;
1751 void Tabular::setUsebox(idx_type cell, BoxType type)
1753 cellInfo(cell).usebox = type;
1757 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1758 // adds line breaks, see bug 4886.
1759 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1761 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1762 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1764 if (cellInfo(cell).usebox > 1)
1765 return cellInfo(cell).usebox;
1766 return useParbox(cell);
1771 // This are functions used for the longtable support
1773 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1779 row_info[row].endfirsthead = flag;
1783 row_info[row].endhead = flag;
1788 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1791 hd.set = haveLTHead();
1792 return row_info[row].endhead;
1796 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1799 hd.set = haveLTFirstHead();
1800 return row_info[row].endfirsthead;
1804 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1810 row_info[row].endlastfoot = flag;
1814 row_info[row].endfoot = flag;
1819 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1822 fd.set = haveLTFoot();
1823 return row_info[row].endfoot;
1827 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1830 fd.set = haveLTLastFoot();
1831 return row_info[row].endlastfoot;
1835 void Tabular::setLTNewPage(row_type row, bool what)
1837 row_info[row].newpage = what;
1841 bool Tabular::getLTNewPage(row_type row) const
1843 return row_info[row].newpage;
1847 bool Tabular::haveLTHead() const
1849 for (row_type i = 0; i < row_info.size(); ++i)
1850 if (row_info[i].endhead)
1856 bool Tabular::haveLTFirstHead() const
1858 if (endfirsthead.empty)
1860 for (row_type i = 0; i < row_info.size(); ++i)
1861 if (row_info[i].endfirsthead)
1867 bool Tabular::haveLTFoot() const
1869 for (row_type i = 0; i < row_info.size(); ++i)
1870 if (row_info[i].endfoot)
1876 bool Tabular::haveLTLastFoot() const
1878 if (endlastfoot.empty)
1880 for (row_type i = 0; i < row_info.size(); ++i)
1881 if (row_info[i].endlastfoot)
1887 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1889 idx_type i = getFirstCellInRow(row);
1891 setMultiColumn(i, numberOfCellsInRow(i));
1892 setTopLine(i, false);
1893 setBottomLine(i, false);
1894 setLeftLine(i, false);
1895 setRightLine(i, false);
1897 unsetMultiColumn(i);
1898 // When unsetting a caption row, also all existing
1899 // captions in this row must be dissolved.
1901 row_info[row].caption = what;
1906 bool Tabular::ltCaption(row_type row) const
1908 return row_info[row].caption;
1912 bool Tabular::haveLTCaption() const
1914 for (row_type i = 0; i < row_info.size(); ++i)
1915 if (row_info[i].caption)
1921 // end longtable support functions
1923 void Tabular::setRowAscent(row_type row, int height)
1925 if (row >= row_info.size() || row_info[row].ascent == height)
1927 row_info[row].ascent = height;
1931 void Tabular::setRowDescent(row_type row, int height)
1933 if (row >= row_info.size() || row_info[row].descent == height)
1935 row_info[row].descent = height;
1939 int Tabular::rowAscent(row_type row) const
1941 LASSERT(row < row_info.size(), /**/);
1942 return row_info[row].ascent;
1946 int Tabular::rowDescent(row_type row) const
1948 LASSERT(row < row_info.size(), /**/);
1949 return row_info[row].descent;
1953 int Tabular::height() const
1956 for (row_type row = 0; row < row_info.size(); ++row)
1957 height += rowAscent(row) + rowDescent(row) +
1963 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1965 LASSERT(row < row_info.size(), /**/);
1966 LASSERT(column < column_info.size(), /**/);
1967 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1971 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1973 LASSERT(row < row_info.size(), /**/);
1974 LASSERT(column < column_info.size(), /**/);
1975 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1979 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1981 // we only output complete row lines and the 1st row here, the rest
1982 // is done in Tabular::TeXBottomHLine(...)
1984 // get for each column the topline (if any)
1985 col_type const ncols = column_info.size();
1986 vector<bool> topline;
1988 for (col_type c = 0; c < ncols; ++c) {
1989 topline.push_back(topLine(cellIndex(row, c)));
1990 // If cell is part of a multirow and not the first cell of the
1991 // multirow, no line must be drawn.
1993 if (isMultiRow(cellIndex(row, c))
1994 && isMultiRow(cellIndex(row - 1, c)))
2000 // do nothing if empty first row, or incomplete row line after
2001 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
2004 // only output complete row lines and the 1st row's clines
2005 if (nset == ncols) {
2007 os << (row == 0 ? "\\toprule " : "\\midrule ");
2011 } else if (row == 0) {
2012 for (col_type c = 0; c < ncols; ++c) {
2014 //babel makes the "-" character an active one, so we have to suppress this here
2015 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2016 if (lang == "slovak" || lang == "czech")
2017 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2018 "\\expandafter\\cline\\expandafter{\\expandafter")
2019 << c + 1 << "\\string-";
2021 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2022 // get to last column of line span
2023 while (c < ncols && topline[c])
2034 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2036 // we output bottomlines of row r and the toplines of row r+1
2037 // if the latter do not span the whole tabular
2039 // get the bottomlines of row r, and toplines in next row
2040 bool lastrow = row == row_info.size() - 1;
2041 col_type const ncols = column_info.size();
2042 vector<bool> bottomline, topline;
2043 bool nextrowset = true;
2044 for (col_type c = 0; c < ncols; ++c) {
2045 bottomline.push_back(bottomLine(cellIndex(row, c)));
2046 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2047 // If cell is part of a multirow and not the last or first cell of the
2048 // multirow, no line must be drawn.
2050 if (isMultiRow(cellIndex(row, c))
2051 && isMultiRow(cellIndex(row + 1, c))) {
2052 bottomline[c] = false;
2055 nextrowset &= topline[c];
2058 // combine this row's bottom lines and next row's toplines if necessary
2060 for (col_type c = 0; c < ncols; ++c) {
2062 bottomline[c] = bottomline[c] || topline[c];
2067 // do nothing if empty, OR incomplete row line with a topline in next row
2068 if (nset == 0 || (nextrowset && nset != ncols))
2071 if (nset == ncols) {
2073 os << (lastrow ? "\\bottomrule" : "\\midrule");
2077 for (col_type c = 0; c < ncols; ++c) {
2078 if (bottomline[c]) {
2079 //babel makes the "-" character an active one, so we have to suppress this here
2080 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2081 if (lang == "slovak" || lang == "czech")
2082 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2083 "\\expandafter\\cline\\expandafter{\\expandafter")
2084 << c + 1 << "\\string-";
2086 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2087 // get to last column of line span
2088 while (c < ncols && bottomline[c])
2099 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2100 bool & ismulticol, bool & ismultirow) const
2103 row_type const r = cellRow(cell);
2104 if (is_long_tabular && row_info[r].caption)
2107 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2108 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2109 // figure out how to set the lines
2110 // we always set double lines to the right of the cell
2111 col_type const c = cellColumn(cell);
2112 col_type const nextcol = c + columnSpan(cell);
2113 bool colright = columnRightLine(c);
2114 bool colleft = columnLeftLine(c);
2115 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
2116 bool nextcellleft = nextcol < column_info.size()
2117 && leftLine(cellIndex(r, nextcol));
2118 bool coldouble = colright && nextcolleft;
2119 bool celldouble = rightLine(cell) && nextcellleft;
2121 ismulticol = isMultiColumn(cell)
2122 || (c == 0 && colleft != leftLine(cell))
2123 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2124 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2125 || (coldouble != celldouble);
2127 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2128 if (c ==0 && leftLine(cell))
2130 if (!cellInfo(cell).align_special.empty()) {
2131 os << cellInfo(cell).align_special;
2133 if (!getPWidth(cell).zero()) {
2135 case LYX_ALIGN_LEFT:
2136 os << ">{\\raggedright}";
2138 case LYX_ALIGN_RIGHT:
2139 os << ">{\\raggedleft}";
2141 case LYX_ALIGN_CENTER:
2142 os << ">{\\centering}";
2148 case LYX_VALIGN_TOP:
2151 case LYX_VALIGN_MIDDLE:
2154 case LYX_VALIGN_BOTTOM:
2159 << from_ascii(getPWidth(cell).asLatexString())
2163 case LYX_ALIGN_LEFT:
2166 case LYX_ALIGN_RIGHT:
2173 } // end if else !getPWidth
2174 } // end if else !cellinfo_of_cell
2175 if (rightLine(cell) || nextcellleft)
2178 // add extra vertical line if we want a double one
2181 } // end if ismulticol
2183 // we only need code for the first multirow cell
2184 ismultirow = isMultiRow(cell);
2186 os << "\\multirow{" << rowSpan(cell) << "}{";
2187 if (!getPWidth(cell).zero())
2188 os << from_ascii(getPWidth(cell).asLatexString());
2190 // we need to set a default value
2191 // needs to be discussed
2194 } // end if ismultirow
2196 if (getRotateCell(cell)) {
2197 os << "\\begin{sideways}\n";
2200 if (getUsebox(cell) == BOX_PARBOX) {
2203 case LYX_VALIGN_TOP:
2206 case LYX_VALIGN_MIDDLE:
2209 case LYX_VALIGN_BOTTOM:
2213 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2215 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2216 os << "\\begin{minipage}[";
2218 case LYX_VALIGN_TOP:
2221 case LYX_VALIGN_MIDDLE:
2224 case LYX_VALIGN_BOTTOM:
2228 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2236 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2237 bool ismulticol, bool ismultirow) const
2240 row_type const r = cellRow(cell);
2241 if (is_long_tabular && row_info[r].caption)
2245 if (getUsebox(cell) == BOX_PARBOX)
2247 else if (getUsebox(cell) == BOX_MINIPAGE) {
2248 os << "%\n\\end{minipage}";
2251 if (getRotateCell(cell)) {
2252 os << "%\n\\end{sideways}";
2255 if (ismulticol || ismultirow) {
2262 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2263 OutputParams const & runparams) const
2265 if (!is_long_tabular)
2270 // the caption must be output before the headers
2271 if (haveLTCaption()) {
2272 for (row_type i = 0; i < row_info.size(); ++i) {
2273 if (row_info[i].caption) {
2274 ret += TeXRow(os, i, runparams);
2278 // output first header info
2279 // first header must be output before the header, otherwise the
2280 // correct caption placement becomes really weird
2281 if (haveLTFirstHead()) {
2282 if (endfirsthead.topDL) {
2286 for (row_type i = 0; i < row_info.size(); ++i) {
2287 if (row_info[i].endfirsthead) {
2288 ret += TeXRow(os, i, runparams);
2291 if (endfirsthead.bottomDL) {
2295 os << "\\endfirsthead\n";
2298 // output header info
2300 if (endfirsthead.empty && !haveLTFirstHead()) {
2301 os << "\\endfirsthead\n";
2304 if (endhead.topDL) {
2308 for (row_type i = 0; i < row_info.size(); ++i) {
2309 if (row_info[i].endhead) {
2310 ret += TeXRow(os, i, runparams);
2313 if (endhead.bottomDL) {
2317 os << "\\endhead\n";
2320 // output footer info
2322 if (endfoot.topDL) {
2326 for (row_type i = 0; i < row_info.size(); ++i) {
2327 if (row_info[i].endfoot) {
2328 ret += TeXRow(os, i, runparams);
2331 if (endfoot.bottomDL) {
2335 os << "\\endfoot\n";
2337 if (endlastfoot.empty && !haveLTLastFoot()) {
2338 os << "\\endlastfoot\n";
2342 // output lastfooter info
2343 if (haveLTLastFoot()) {
2344 if (endlastfoot.topDL) {
2348 for (row_type i = 0; i < row_info.size(); ++i) {
2349 if (row_info[i].endlastfoot) {
2350 ret += TeXRow(os, i, runparams);
2353 if (endlastfoot.bottomDL) {
2357 os << "\\endlastfoot\n";
2364 bool Tabular::isValidRow(row_type row) const
2366 if (!is_long_tabular)
2368 return !row_info[row].endhead && !row_info[row].endfirsthead
2369 && !row_info[row].endfoot && !row_info[row].endlastfoot
2370 && !row_info[row].caption;
2374 int Tabular::TeXRow(odocstream & os, row_type i,
2375 OutputParams const & runparams) const
2377 idx_type cell = cellIndex(i, 0);
2378 shared_ptr<InsetTableCell> inset = cellInset(cell);
2379 Paragraph const & par = inset->paragraphs().front();
2380 string const lang = par.getParLanguage(buffer().params())->lang();
2382 //output the top line
2383 int ret = TeXTopHLine(os, i, lang);
2385 if (row_info[i].top_space_default) {
2387 os << "\\addlinespace\n";
2389 os << "\\noalign{\\vskip\\doublerulesep}\n";
2391 } else if(!row_info[i].top_space.zero()) {
2393 os << "\\addlinespace["
2394 << from_ascii(row_info[i].top_space.asLatexString())
2397 os << "\\noalign{\\vskip"
2398 << from_ascii(row_info[i].top_space.asLatexString())
2403 bool ismulticol = false;
2404 bool ismultirow = false;
2405 for (col_type j = 0; j < column_info.size(); ++j) {
2406 if (isPartOfMultiRow(i, j))
2407 os << " & "; // we need to add a further column
2408 if (isPartOfMultiColumn(i, j) || isPartOfMultiRow(i, j))
2410 cell = cellIndex(i, j);
2411 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2412 shared_ptr<InsetTableCell> inset = cellInset(cell);
2414 Paragraph const & par = inset->paragraphs().front();
2415 bool rtl = par.isRTL(buffer().params())
2417 && getPWidth(cell).zero();
2421 par.getParLanguage(buffer().params())->lang();
2422 if (lang == "farsi")
2424 else if (lang == "arabic_arabi")
2426 // currently, remaning RTL languages are
2427 // arabic_arabtex and hebrew
2431 // pass to the OutputParams that we are in a cell and
2432 // which alignment we have set.
2433 // InsetNewline needs this context information.
2434 OutputParams newrp(runparams);
2435 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2436 ? OutputParams::PLAIN
2437 : OutputParams::ALIGNED;
2438 ret += inset->latex(os, newrp);
2439 runparams.encoding = newrp.encoding;
2443 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2444 if (!isLastCellInRow(cell)) { // not last cell in row
2448 if (row_info[i].caption && !endfirsthead.empty && !haveLTFirstHead())
2449 // if no first header and no empty first header is used,
2450 // the caption needs to be terminated by \endfirsthead
2452 os << "\\endfirsthead";
2454 os << "\\tabularnewline";
2455 if (row_info[i].bottom_space_default) {
2457 os << "\\addlinespace";
2459 os << "[\\doublerulesep]";
2460 } else if (!row_info[i].bottom_space.zero()) {
2462 os << "\\addlinespace";
2464 << from_ascii(row_info[i].bottom_space.asLatexString())
2470 //output the bottom line
2471 ret += TeXBottomHLine(os, i, lang);
2473 if (row_info[i].interline_space_default) {
2475 os << "\\addlinespace\n";
2477 os << "\\noalign{\\vskip\\doublerulesep}\n";
2479 } else if (!row_info[i].interline_space.zero()) {
2481 os << "\\addlinespace["
2482 << from_ascii(row_info[i].interline_space.asLatexString())
2485 os << "\\noalign{\\vskip"
2486 << from_ascii(row_info[i].interline_space.asLatexString())
2494 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2498 //+---------------------------------------------------------------------
2499 //+ first the opening preamble +
2500 //+---------------------------------------------------------------------
2503 os << "\\begin{sideways}\n";
2506 if (is_long_tabular) {
2507 os << "\\begin{longtable}";
2508 switch (longtabular_alignment) {
2509 case LYX_LONGTABULAR_ALIGN_LEFT:
2512 case LYX_LONGTABULAR_ALIGN_CENTER:
2514 case LYX_LONGTABULAR_ALIGN_RIGHT:
2519 os << "\\begin{tabular}";
2520 switch (tabular_valignment) {
2521 case LYX_VALIGN_TOP:
2524 case LYX_VALIGN_MIDDLE:
2526 case LYX_VALIGN_BOTTOM:
2534 for (col_type i = 0; i < column_info.size(); ++i) {
2535 if (columnLeftLine(i))
2537 if (!column_info[i].align_special.empty()) {
2538 os << column_info[i].align_special;
2540 if (!column_info[i].p_width.zero()) {
2541 switch (column_info[i].alignment) {
2542 case LYX_ALIGN_LEFT:
2543 os << ">{\\raggedright}";
2545 case LYX_ALIGN_RIGHT:
2546 os << ">{\\raggedleft}";
2548 case LYX_ALIGN_CENTER:
2549 os << ">{\\centering}";
2551 case LYX_ALIGN_NONE:
2552 case LYX_ALIGN_BLOCK:
2553 case LYX_ALIGN_LAYOUT:
2554 case LYX_ALIGN_SPECIAL:
2558 switch (column_info[i].valignment) {
2559 case LYX_VALIGN_TOP:
2562 case LYX_VALIGN_MIDDLE:
2565 case LYX_VALIGN_BOTTOM:
2570 << from_ascii(column_info[i].p_width.asLatexString())
2573 switch (column_info[i].alignment) {
2574 case LYX_ALIGN_LEFT:
2577 case LYX_ALIGN_RIGHT:
2584 } // end if else !column_info[i].p_width
2585 } // end if else !column_info[i].align_special
2586 if (columnRightLine(i))
2592 ret += TeXLongtableHeaderFooter(os, runparams);
2594 //+---------------------------------------------------------------------
2595 //+ the single row and columns (cells) +
2596 //+---------------------------------------------------------------------
2598 for (row_type i = 0; i < row_info.size(); ++i) {
2599 if (isValidRow(i)) {
2600 ret += TeXRow(os, i, runparams);
2601 if (is_long_tabular && row_info[i].newpage) {
2602 os << "\\newpage\n";
2608 //+---------------------------------------------------------------------
2609 //+ the closing of the tabular +
2610 //+---------------------------------------------------------------------
2612 if (is_long_tabular)
2613 os << "\\end{longtable}";
2615 os << "\\end{tabular}";
2617 os << "\n\\end{sideways}";
2625 int Tabular::docbookRow(odocstream & os, row_type row,
2626 OutputParams const & runparams) const
2629 idx_type cell = getFirstCellInRow(row);
2632 for (col_type j = 0; j < column_info.size(); ++j) {
2633 if (isPartOfMultiColumn(row, j))
2636 os << "<entry align=\"";
2637 switch (getAlignment(cell)) {
2638 case LYX_ALIGN_LEFT:
2641 case LYX_ALIGN_RIGHT:
2649 os << "\" valign=\"";
2650 switch (getVAlignment(cell)) {
2651 case LYX_VALIGN_TOP:
2654 case LYX_VALIGN_BOTTOM:
2657 case LYX_VALIGN_MIDDLE:
2662 if (isMultiColumn(cell)) {
2663 os << " namest=\"col" << j << "\" ";
2664 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2668 ret += cellInset(cell)->docbook(os, runparams);
2677 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2681 //+---------------------------------------------------------------------
2682 //+ first the opening preamble +
2683 //+---------------------------------------------------------------------
2685 os << "<tgroup cols=\"" << column_info.size()
2686 << "\" colsep=\"1\" rowsep=\"1\">\n";
2688 for (col_type i = 0; i < column_info.size(); ++i) {
2689 os << "<colspec colname=\"col" << i << "\" align=\"";
2690 switch (column_info[i].alignment) {
2691 case LYX_ALIGN_LEFT:
2694 case LYX_ALIGN_RIGHT:
2702 if (runparams.flavor == OutputParams::XML)
2708 //+---------------------------------------------------------------------
2709 //+ Long Tabular case +
2710 //+---------------------------------------------------------------------
2712 // output caption info
2713 if (haveLTCaption()) {
2714 os << "<caption>\n";
2716 for (row_type i = 0; i < row_info.size(); ++i) {
2717 if (row_info[i].caption) {
2718 ret += docbookRow(os, i, runparams);
2721 os << "</caption>\n";
2724 // output header info
2725 if (haveLTHead() || haveLTFirstHead()) {
2728 for (row_type i = 0; i < row_info.size(); ++i) {
2729 if (row_info[i].endhead || row_info[i].endfirsthead) {
2730 ret += docbookRow(os, i, runparams);
2736 // output footer info
2737 if (haveLTFoot() || haveLTLastFoot()) {
2740 for (row_type i = 0; i < row_info.size(); ++i) {
2741 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2742 ret += docbookRow(os, i, runparams);
2749 //+---------------------------------------------------------------------
2750 //+ the single row and columns (cells) +
2751 //+---------------------------------------------------------------------
2755 for (row_type i = 0; i < row_info.size(); ++i) {
2756 if (isValidRow(i)) {
2757 ret += docbookRow(os, i, runparams);
2762 //+---------------------------------------------------------------------
2763 //+ the closing of the tabular +
2764 //+---------------------------------------------------------------------
2773 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2774 OutputParams const & runparams) const
2777 idx_type cell = getFirstCellInRow(row);
2779 xs << html::StartTag("tr");
2780 for (col_type j = 0; j < column_info.size(); ++j) {
2781 if (isPartOfMultiColumn(row, j))
2786 switch (getAlignment(cell)) {
2787 case LYX_ALIGN_LEFT:
2790 case LYX_ALIGN_RIGHT:
2798 attr << " valign='";
2799 switch (getVAlignment(cell)) {
2800 case LYX_VALIGN_TOP:
2803 case LYX_VALIGN_BOTTOM:
2806 case LYX_VALIGN_MIDDLE:
2811 if (isMultiColumn(cell))
2812 attr << " colspan='" << columnSpan(cell) << "'";
2814 xs << html::StartTag("td", attr.str());
2815 ret += cellInset(cell)->xhtml(xs, runparams);
2816 xs << html::EndTag("td");
2819 xs << html::EndTag("tr");
2824 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2827 // It's unclear to me if we need to mess with the long table stuff.
2828 // We can borrow that too from docbook, if so.
2830 xs << html::StartTag("tbody");
2831 for (row_type i = 0; i < row_info.size(); ++i) {
2832 if (isValidRow(i)) {
2833 ret += xhtmlRow(xs, i, runparams);
2836 xs << html::EndTag("tbody");
2841 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2842 vector<unsigned int> const & clen) const
2844 idx_type const fcell = getFirstCellInRow(row);
2845 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2848 for (idx_type i = fcell; i < n; ++i) {
2858 for (idx_type i = fcell; i < n; ++i) {
2869 col_type column = cellColumn(i);
2870 int len = clen[column];
2871 while (column < column_info.size() - 1
2872 && isPartOfMultiColumn(row, ++column))
2873 len += clen[column] + 4;
2874 os << docstring(len, ch);
2889 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2890 vector<unsigned int> const & clen) const
2892 idx_type const fcell = getFirstCellInRow(row);
2893 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2896 for (idx_type i = fcell; i < n; ++i) {
2897 if (bottomLine(i)) {
2906 for (idx_type i = fcell; i < n; ++i) {
2907 if (bottomLine(i)) {
2917 col_type column = cellColumn(i);
2918 int len = clen[column];
2919 while (column < column_info.size() -1
2920 && isPartOfMultiColumn(row, ++column))
2921 len += clen[column] + 4;
2922 os << docstring(len, ch);
2923 if (bottomLine(i)) {
2937 void Tabular::plaintextPrintCell(odocstream & os,
2938 OutputParams const & runparams,
2939 idx_type cell, row_type row, col_type column,
2940 vector<unsigned int> const & clen,
2941 bool onlydata) const
2943 odocstringstream sstr;
2944 cellInset(cell)->plaintext(sstr, runparams);
2956 unsigned int len1 = sstr.str().length();
2957 unsigned int len2 = clen[column];
2958 while (column < column_info.size() -1
2959 && isPartOfMultiColumn(row, ++column))
2960 len2 += clen[column] + 4;
2963 switch (getAlignment(cell)) {
2965 case LYX_ALIGN_LEFT:
2968 case LYX_ALIGN_RIGHT:
2972 case LYX_ALIGN_CENTER:
2978 os << docstring(len1, ' ') << sstr.str()
2979 << docstring(len2, ' ');
2981 if (rightLine(cell))
2988 void Tabular::plaintext(odocstream & os,
2989 OutputParams const & runparams, int const depth,
2990 bool onlydata, char_type delim) const
2992 // first calculate the width of the single columns
2993 vector<unsigned int> clen(column_info.size());
2996 // first all non (real) multicolumn cells!
2997 for (col_type j = 0; j < column_info.size(); ++j) {
2999 for (row_type i = 0; i < row_info.size(); ++i) {
3000 idx_type cell = cellIndex(i, j);
3001 if (isMultiColumnReal(cell))
3003 odocstringstream sstr;
3004 cellInset(cell)->plaintext(sstr, runparams);
3005 if (clen[j] < sstr.str().length())
3006 clen[j] = sstr.str().length();
3009 // then all (real) multicolumn cells!
3010 for (col_type j = 0; j < column_info.size(); ++j) {
3011 for (row_type i = 0; i < row_info.size(); ++i) {
3012 idx_type cell = cellIndex(i, j);
3013 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
3015 odocstringstream sstr;
3016 cellInset(cell)->plaintext(sstr, runparams);
3017 int len = int(sstr.str().length());
3018 idx_type const n = columnSpan(cell);
3019 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
3021 if (len > int(clen[j + n - 1]))
3022 clen[j + n - 1] = len;
3027 for (row_type i = 0; i < row_info.size(); ++i) {
3028 if (!onlydata && plaintextTopHLine(os, i, clen))
3029 os << docstring(depth * 2, ' ');
3030 for (col_type j = 0; j < column_info.size(); ++j) {
3031 if (isPartOfMultiColumn(i, j))
3033 if (onlydata && j > 0)
3034 // we don't use operator<< for single UCS4 character.
3035 // see explanation in docstream.h
3037 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
3042 os << docstring(depth * 2, ' ');
3043 if (plaintextBottomHLine(os, i, clen))
3044 os << docstring(depth * 2, ' ');
3050 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3052 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3056 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3057 col_type column) const
3059 return cell_info[row][column].inset;
3063 void Tabular::setCellInset(row_type row, col_type column,
3064 shared_ptr<InsetTableCell> ins) const
3066 CellData & cd = cell_info[row][column];
3071 void Tabular::validate(LaTeXFeatures & features) const
3073 features.require("NeedTabularnewline");
3075 features.require("booktabs");
3076 if (is_long_tabular)
3077 features.require("longtable");
3079 features.require("rotating");
3080 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3081 if (isMultiRow(cell))
3082 features.require("multirow");
3083 if (getVAlignment(cell) != LYX_VALIGN_TOP
3084 || !getPWidth(cell).zero())
3085 features.require("array");
3086 cellInset(cell)->validate(features);
3091 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3093 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3094 ParagraphList::const_iterator cit = parlist.begin();
3095 ParagraphList::const_iterator end = parlist.end();
3097 for (; cit != end; ++cit)
3098 for (int i = 0; i < cit->size(); ++i)
3099 if (cit->isNewline(i))
3106 /////////////////////////////////////////////////////////////////////
3110 /////////////////////////////////////////////////////////////////////
3112 InsetTableCell::InsetTableCell(Buffer * buf)
3113 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3114 contentAlign(LYX_ALIGN_CENTER)
3118 bool InsetTableCell::forcePlainLayout(idx_type) const
3120 return !isFixedWidth;
3124 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3126 return isFixedWidth;
3130 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3131 FuncStatus & status) const
3134 switch (cmd.action) {
3136 enabled = !forcePlainLayout();
3138 case LFUN_LAYOUT_PARAGRAPH:
3139 enabled = allowParagraphCustomization();
3142 return InsetText::getStatus(cur, cmd, status);
3144 status.setEnabled(enabled);
3148 docstring InsetTableCell::asString(bool intoInsets)
3151 if (paragraphs().empty())
3153 ParagraphList::const_iterator it = paragraphs().begin();
3154 ParagraphList::const_iterator en = paragraphs().end();
3156 for (; it != en; ++it) {
3161 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3167 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3170 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3171 return InsetText::xhtml(xs, rp);
3176 /////////////////////////////////////////////////////////////////////
3180 /////////////////////////////////////////////////////////////////////
3182 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3184 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3185 rowselect_(false), colselect_(false)
3190 InsetTabular::InsetTabular(InsetTabular const & tab)
3191 : Inset(tab), tabular(tab.tabular), scx_(0)
3196 InsetTabular::~InsetTabular()
3198 hideDialogs("tabular", this);
3202 void InsetTabular::setBuffer(Buffer & buf)
3204 tabular.setBuffer(buf);
3205 Inset::setBuffer(buf);
3209 bool InsetTabular::insetAllowed(InsetCode code) const
3211 if (code == MATHMACRO_CODE
3212 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3219 void InsetTabular::write(ostream & os) const
3221 os << "Tabular" << endl;
3226 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3228 // FIXME: depending on the selection state, we could offer a different menu.
3229 return from_ascii("context-tabular");
3233 void InsetTabular::read(Lexer & lex)
3235 //bool const old_format = (lex.getString() == "\\LyXTable");
3243 string token = lex.getString();
3244 while (lex && token != "\\end_inset") {
3246 token = lex.getString();
3249 lex.printError("Missing \\end_inset at this point. ");
3253 int InsetTabular::rowFromY(Cursor & cur, int y) const
3255 // top y coordinate of tabular
3256 int h = yo(cur.bv()) - tabular.rowAscent(0);
3257 size_t nrows = tabular.row_info.size();
3259 for (; r < nrows && y > h; ++r)
3260 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3261 + tabular.interRowSpace(r);
3267 int InsetTabular::columnFromX(Cursor & cur, int x) const
3269 // left x coordinate of tabular
3270 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3271 size_t ncols = tabular.column_info.size();
3273 for (; c < ncols && x > w; ++c)
3274 w += tabular.columnWidth(c);
3279 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3281 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3282 // mi.base.textwidth << "\n";
3285 LASSERT(false, /**/);
3288 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3291 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3292 if (tabular.isPartOfMultiColumn(i, j)
3293 || tabular.isPartOfMultiRow(i, j))
3294 // multicolumn or multirow cell, but not first one
3296 idx_type const cell = tabular.cellIndex(i, j);
3300 if (tabular.cell_info[i][j].multicolumn ==
3301 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3302 p_width = tabular.cellInfo(cell).p_width;
3304 p_width = tabular.column_info[j].p_width;
3305 if (!p_width.zero())
3306 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3307 tabular.cellInset(cell)->metrics(m, dim);
3308 if (!p_width.zero())
3309 dim.wid = m.base.textwidth;
3310 tabular.setCellWidth(cell, dim.wid);
3311 maxAsc = max(maxAsc, dim.asc);
3312 maxDesc = max(maxDesc, dim.des);
3314 int const top_space = tabular.row_info[i].top_space_default ?
3315 default_line_space :
3316 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
3317 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
3318 int const bottom_space = tabular.row_info[i].bottom_space_default ?
3319 default_line_space :
3320 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3321 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3323 tabular.updateColumnWidths();
3324 dim.asc = tabular.rowAscent(0);
3325 dim.des = tabular.height() - dim.asc;
3326 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3329 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3332 if (&cur.inset() == this && cur.selection()) {
3333 if (cur.selIsMultiCell()) {
3336 getSelection(cur, rs, re, cs, ce);
3338 if (col >= cs && col <= ce && row >= rs && row <= re)
3341 if (col == tabular.cellColumn(cur.idx())
3342 && row == tabular.cellRow(cur.idx())) {
3343 CursorSlice const & beg = cur.selBegin();
3344 CursorSlice const & end = cur.selEnd();
3346 if ((end.lastpos() > 0 || end.lastpit() > 0)
3347 && end.pos() == end.lastpos() && beg.pos() == 0
3348 && end.pit() == end.lastpit() && beg.pit() == 0)
3356 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3358 x += scx_ + ADD_TO_TABULAR_WIDTH;
3360 BufferView * bv = pi.base.bv;
3361 Cursor & cur = pi.base.bv->cursor();
3364 // FIXME: As the full background is painted in drawSelection(),
3365 // we have no choice but to do a full repaint for the Text cells.
3366 pi.full_repaint = true;
3368 bool const original_selection_state = pi.selected;
3371 first_visible_cell = Tabular::npos;
3372 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3374 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3375 if (tabular.isPartOfMultiColumn(i, j))
3378 idx = tabular.cellIndex(i, j);
3380 if (tabular.isPartOfMultiRow(i, j)) {
3381 nx += tabular.columnWidth(idx);
3385 if (first_visible_cell == Tabular::npos)
3386 first_visible_cell = idx;
3388 pi.selected |= isCellSelected(cur, i, j);
3389 int const cx = nx + tabular.textHOffset(idx);
3390 int const cy = y + tabular.textVOffset(idx);
3391 // Cache the Inset position.
3392 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3393 cell(idx)->draw(pi, cx, cy);
3394 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3395 nx += tabular.columnWidth(idx);
3396 pi.selected = original_selection_state;
3399 if (i + 1 < tabular.row_info.size())
3400 y += tabular.rowDescent(i) + tabular.rowAscent(i + 1)
3401 + tabular.interRowSpace(i + 1);
3406 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3408 Cursor & cur = pi.base.bv->cursor();
3411 x += scx_ + ADD_TO_TABULAR_WIDTH;
3413 // FIXME: it is wrong to completely paint the background
3414 // if we want to do single row painting.
3416 // Paint background of current tabular
3417 int const w = tabular.width();
3418 int const h = tabular.height();
3419 int yy = y - tabular.rowAscent(0);
3420 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3422 if (!cur.selection())
3424 if (&cur.inset() != this)
3429 bool const full_cell_selected = isCellSelected(cur,
3430 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3432 if (cur.selIsMultiCell() || full_cell_selected) {
3433 y -= tabular.rowAscent(0);
3434 for (row_type r = 0; r < tabular.row_info.size(); ++r) {
3436 for (col_type c = 0; c < tabular.column_info.size(); ++c) {
3437 if (tabular.isPartOfMultiColumn(r, c))
3440 idx_type const cell = tabular.cellIndex(r, c);
3442 if (tabular.isPartOfMultiRow(r, c)) {
3443 xx += tabular.columnWidth(cell);
3446 int const w = tabular.columnWidth(cell);
3447 int const h = tabular.rowHeight(cell);
3448 if (isCellSelected(cur, r, c))
3449 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3452 if (r + 1 < tabular.row_info.size())
3453 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3454 + tabular.interRowSpace(r + 1);
3458 x += cellXPos(cur.idx());
3459 x += tabular.textHOffset(cur.idx());
3460 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3465 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3466 row_type row, idx_type cell, Change const & change) const
3468 y = y - tabular.rowAscent(row);
3469 int const w = tabular.columnWidth(cell);
3470 int const h = tabular.rowHeight(cell);
3471 bool on_off = false;
3472 Color col = Color_tabularline;
3473 Color onoffcol = Color_tabularonoffline;
3475 if (change.changed()) {
3476 col = change.color();
3477 onoffcol = change.color();
3480 if (!tabular.topAlreadyDrawn(cell)) {
3481 on_off = !tabular.topLine(cell);
3482 pain.line(x, y, x + w, y,
3483 on_off ? onoffcol : col,
3484 on_off ? Painter::line_onoffdash : Painter::line_solid);
3486 on_off = !tabular.bottomLine(cell);
3487 pain.line(x, y + h, x + w, y + h,
3488 on_off ? onoffcol : col,
3489 on_off ? Painter::line_onoffdash : Painter::line_solid);
3490 if (!tabular.leftAlreadyDrawn(cell)) {
3491 on_off = !tabular.leftLine(cell);
3492 pain.line(x, y, x, y + h,
3493 on_off ? onoffcol : col,
3494 on_off ? Painter::line_onoffdash : Painter::line_solid);
3496 on_off = !tabular.rightLine(cell);
3497 pain.line(x + w - tabular.interColumnSpace(cell), y,
3498 x + w - tabular.interColumnSpace(cell), y + h,
3499 on_off ? onoffcol : col,
3500 on_off ? Painter::line_onoffdash : Painter::line_solid);
3504 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3506 //lyxerr << "InsetTabular::edit: " << this << endl;
3508 cur.setSelection(false);
3511 if (isRightToLeft(cur))
3512 cur.idx() = tabular.getLastCellInRow(0);
3518 if (isRightToLeft(cur))
3519 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3521 cur.idx() = tabular.numberofcells - 1;
3523 cur.pos() = cur.lastpos(); // FIXME crude guess
3525 cur.setCurrentFont();
3526 // FIXME: this accesses the position cache before it is initialized
3528 //cur.bv().fitCursor();
3532 void InsetTabular::updateLabels(ParIterator const & it, UpdateType utype)
3534 // In a longtable, tell captions what the current float is
3535 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3536 string const saveflt = cnts.current_float();
3537 if (tabular.is_long_tabular)
3538 cnts.current_float("table");
3540 ParIterator it2 = it;
3542 size_t const end = it2.nargs();
3543 for ( ; it2.idx() < end; it2.top().forwardIdx())
3544 buffer().updateLabels(it2, utype);
3547 if (tabular.is_long_tabular)
3548 cnts.current_float(saveflt);
3552 void InsetTabular::addToToc(DocIterator const & cpit)
3554 DocIterator dit = cpit;
3556 size_t const end = dit.nargs();
3557 for ( ; dit.idx() < end; dit.top().forwardIdx())
3558 cell(dit.idx())->addToToc(dit);
3562 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3564 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3565 << "\n cur:" << cur);
3566 CursorSlice sl = cur.top();
3567 Cursor & bvcur = cur.bv().cursor();
3569 switch (cmd.action) {
3571 case LFUN_MOUSE_PRESS: {
3572 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3574 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3575 || cmd.x > xo(cur.bv()) + tabular.width()) {
3576 row_type r = rowFromY(cur, cmd.y);
3577 cur.idx() = tabular.getFirstCellInRow(r);
3580 cur.idx() = tabular.getLastCellInRow(r);
3581 cur.pos() = cur.lastpos();
3582 cur.setSelection(true);
3588 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3589 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3590 || cmd.y > y0 + tabular.height()) {
3591 col_type c = columnFromX(cur, cmd.x);
3592 cur.idx() = tabular.cellIndex(0, c);
3595 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3596 cur.pos() = cur.lastpos();
3597 cur.setSelection(true);
3602 // do not reset cursor/selection if we have selected
3603 // some cells (bug 2715).
3604 if (cmd.button() == mouse_button::button3
3605 && &bvcur.selBegin().inset() == this
3606 && bvcur.selIsMultiCell())
3609 // Let InsetTableCell do it
3610 cell(cur.idx())->dispatch(cur, cmd);
3613 case LFUN_MOUSE_MOTION:
3614 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3615 if (cmd.button() == mouse_button::button1) {
3616 // only accept motions to places not deeper nested than the real anchor
3617 if (!bvcur.anchor_.hasPart(cur)) {
3621 // select (additional) row
3623 row_type r = rowFromY(cur, cmd.y);
3624 cur.idx() = tabular.getLastCellInRow(r);
3625 // we need to reset the cursor's pit and pos now, as the old ones
3626 // may no longer be valid.
3629 bvcur.setCursor(cur);
3630 bvcur.setSelection(true);
3633 // select (additional) column
3635 col_type c = columnFromX(cur, cmd.x);
3636 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3637 // we need to reset the cursor's pit and pos now, as the old ones
3638 // may no longer be valid.
3641 bvcur.setCursor(cur);
3642 bvcur.setSelection(true);
3645 // only update if selection changes
3646 if (bvcur.idx() == cur.idx() &&
3647 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3649 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3650 bvcur.setCursor(cur);
3651 bvcur.setSelection(true);
3652 // if this is a multicell selection, we just set the cursor to
3653 // the beginning of the cell's text.
3654 if (bvcur.selIsMultiCell()) {
3655 bvcur.pit() = bvcur.lastpit();
3656 bvcur.pos() = bvcur.lastpos();
3661 case LFUN_MOUSE_RELEASE:
3666 case LFUN_CELL_BACKWARD:
3668 cur.setSelection(false);
3671 case LFUN_CELL_FORWARD:
3673 cur.setSelection(false);
3676 case LFUN_CHAR_FORWARD_SELECT:
3677 case LFUN_CHAR_FORWARD:
3678 case LFUN_CHAR_BACKWARD_SELECT:
3679 case LFUN_CHAR_BACKWARD:
3680 case LFUN_CHAR_RIGHT_SELECT:
3681 case LFUN_CHAR_RIGHT:
3682 case LFUN_CHAR_LEFT_SELECT:
3683 case LFUN_CHAR_LEFT: {
3684 // determine whether we move to next or previous cell, where to enter
3685 // the new cell from, and which command to "finish" (i.e., exit the
3688 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3689 FuncCode finish_lfun;
3691 if (cmd.action == LFUN_CHAR_FORWARD
3692 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3694 finish_lfun = LFUN_FINISHED_FORWARD;
3696 else if (cmd.action == LFUN_CHAR_BACKWARD
3697 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3699 finish_lfun = LFUN_FINISHED_BACKWARD;
3701 // LEFT or RIGHT commands --- the interpretation will depend on the
3702 // table's direction.
3704 bool const right = cmd.action == LFUN_CHAR_RIGHT
3705 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3706 next_cell = isRightToLeft(cur) != right;
3708 if (lyxrc.visual_cursor)
3709 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3711 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3714 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3715 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3716 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3717 cmd.action == LFUN_CHAR_LEFT_SELECT;
3719 // If we have a multicell selection or we're
3720 // not doing some LFUN_*_SELECT thing anyway...
3721 if (!cur.selIsMultiCell() || !select) {
3722 col_type const c = tabular.cellColumn(cur.idx());
3723 row_type const r = tabular.cellRow(cur.idx());
3724 // Are we trying to select the whole cell and is the whole cell
3725 // not yet selected?
3726 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3727 ((next_cell && cur.pit() == cur.lastpit()
3728 && cur.pos() == cur.lastpos())
3729 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3731 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3733 // ...try to dispatch to the cell's inset.
3734 cell(cur.idx())->dispatch(cur, cmd);
3736 // When we already have a selection we want to select the whole cell
3737 // before going to the next cell.
3738 if (select_whole && !empty_cell){
3739 getText(cur.idx())->selectAll(cur);
3744 // FIXME: When we support the selection of an empty cell, remove
3745 // the !empty_cell from this condition. For now we jump to the next
3746 // cell if the current cell is empty.
3747 if (cur.result().dispatched() && !empty_cell)
3751 // move to next/prev cell, as appropriate
3752 // note that we will always do this if we're selecting and we have
3753 // a multicell selection
3754 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3755 << " cell from: " << int(entry_from));
3757 moveNextCell(cur, entry_from);
3759 movePrevCell(cur, entry_from);
3760 // if we're exiting the table, call the appropriate FINISHED lfun
3761 if (sl == cur.top())
3762 cmd = FuncRequest(finish_lfun);
3769 case LFUN_DOWN_SELECT:
3771 if (!(cur.selection() && cur.selIsMultiCell()))
3772 cell(cur.idx())->dispatch(cur, cmd);
3774 cur.dispatched(); // override the cell's decision
3775 if (sl == cur.top()) {
3776 // if our Text didn't do anything to the cursor
3777 // then we try to put the cursor into the cell below
3778 // setting also the right targetX.
3779 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3780 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3781 cur.idx() = tabular.cellBelow(cur.idx());
3783 TextMetrics const & tm =
3784 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3785 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3786 cur.setCurrentFont();
3789 if (sl == cur.top()) {
3790 // we trick it to go to forward after leaving the
3792 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3795 if (cur.selIsMultiCell()) {
3796 cur.pit() = cur.lastpit();
3797 cur.pos() = cur.lastpos();
3798 cur.setCurrentFont();
3803 case LFUN_UP_SELECT:
3805 if (!(cur.selection() && cur.selIsMultiCell()))
3806 cell(cur.idx())->dispatch(cur, cmd);
3807 cur.dispatched(); // override the cell's decision
3808 if (sl == cur.top()) {
3809 // if our Text didn't do anything to the cursor
3810 // then we try to put the cursor into the cell above
3811 // setting also the right targetX.
3812 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3813 if (tabular.cellRow(cur.idx()) != 0) {
3814 cur.idx() = tabular.cellAbove(cur.idx());
3815 cur.pit() = cur.lastpit();
3816 Text const * text = cell(cur.idx())->getText(0);
3817 TextMetrics const & tm = cur.bv().textMetrics(text);
3818 ParagraphMetrics const & pm =
3819 tm.parMetrics(cur.lastpit());
3820 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3821 cur.setCurrentFont();
3824 if (sl == cur.top()) {
3825 cmd = FuncRequest(LFUN_UP);
3828 if (cur.selIsMultiCell()) {
3830 cur.pos() = cur.lastpos();
3831 cur.setCurrentFont();
3836 // case LFUN_SCREEN_DOWN: {
3837 // //if (hasSelection())
3838 // // cur.selection() = false;
3839 // col_type const col = tabular.cellColumn(cur.idx());
3840 // int const t = cur.bv().top_y() + cur.bv().height();
3841 // if (t < yo() + tabular.getHeightOfTabular()) {
3842 // cur.bv().scrollDocView(t);
3843 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3845 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3852 // case LFUN_SCREEN_UP: {
3853 // //if (hasSelection())
3854 // // cur.selection() = false;
3855 // col_type const col = tabular.cellColumn(cur.idx());
3856 // int const t = cur.bv().top_y() + cur.bv().height();
3858 // cur.bv().scrollDocView(t);
3862 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3866 // cur.par() = cur.lastpar();
3867 // cur.pos() = cur.lastpos();
3871 case LFUN_LAYOUT_TABULAR:
3872 cur.bv().showDialog("tabular", params2string(*this), this);
3875 case LFUN_INSET_DIALOG_UPDATE:
3876 cur.bv().updateDialog("tabular", params2string(*this));
3879 case LFUN_TABULAR_FEATURE:
3880 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3884 // insert file functions
3885 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3886 case LFUN_FILE_INSERT_PLAINTEXT:
3888 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3889 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3890 FileName(to_utf8(cmd.argument())));
3893 cur.recordUndoInset(INSERT_UNDO);
3894 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3895 // content has been replaced,
3896 // so cursor might be invalid
3897 cur.pos() = cur.lastpos();
3898 cur.pit() = cur.lastpit();
3899 bvcur.setCursor(cur);
3906 if (cur.selIsMultiCell()) {
3907 if (copySelection(cur)) {
3908 cur.recordUndoInset(DELETE_UNDO);
3912 cell(cur.idx())->dispatch(cur, cmd);
3915 case LFUN_SELF_INSERT:
3916 if (cur.selIsMultiCell()) {
3917 cur.recordUndoInset(DELETE_UNDO);
3920 cell(cur.idx())->dispatch(cur, cmd);
3923 case LFUN_CHAR_DELETE_BACKWARD:
3924 case LFUN_CHAR_DELETE_FORWARD:
3925 if (cur.selIsMultiCell()) {
3926 cur.recordUndoInset(DELETE_UNDO);
3929 cell(cur.idx())->dispatch(cur, cmd);
3933 if (!cur.selection())
3935 if (cur.selIsMultiCell()) {
3939 cell(cur.idx())->dispatch(cur, cmd);
3942 case LFUN_CLIPBOARD_PASTE:
3943 case LFUN_PRIMARY_SELECTION_PASTE: {
3944 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3945 theClipboard().getAsText() :
3946 theSelection().get();
3949 // pass to InsertPlaintextString, but
3950 // only if we have multi-cell content
3951 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3952 cur.recordUndoInset(INSERT_UNDO);
3953 if (insertPlaintextString(cur.bv(), clip, false)) {
3954 // content has been replaced,
3955 // so cursor might be invalid
3956 cur.pos() = cur.lastpos();
3957 cur.pit() = cur.lastpit();
3958 bvcur.setCursor(cur);
3962 // Let the cell handle normal text
3963 cell(cur.idx())->dispatch(cur, cmd);
3968 if (!tabularStackDirty()) {
3969 if (!cur.selIsMultiCell())
3970 cell(cur.idx())->dispatch(cur, cmd);
3973 if (theClipboard().isInternal() ||
3974 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3975 cur.recordUndoInset(INSERT_UNDO);
3976 pasteClipboard(cur);
3980 case LFUN_FONT_EMPH:
3981 case LFUN_FONT_BOLD:
3982 case LFUN_FONT_BOLDSYMBOL:
3983 case LFUN_FONT_ROMAN:
3984 case LFUN_FONT_NOUN:
3985 case LFUN_FONT_ITAL:
3986 case LFUN_FONT_FRAK:
3987 case LFUN_FONT_TYPEWRITER:
3988 case LFUN_FONT_SANS:
3989 case LFUN_TEXTSTYLE_APPLY:
3990 case LFUN_TEXTSTYLE_UPDATE:
3991 case LFUN_FONT_SIZE:
3992 case LFUN_FONT_UNDERLINE:
3993 case LFUN_FONT_STRIKEOUT:
3994 case LFUN_FONT_UULINE:
3995 case LFUN_FONT_UWAVE:
3997 case LFUN_WORD_CAPITALIZE:
3998 case LFUN_WORD_UPCASE:
3999 case LFUN_WORD_LOWCASE:
4000 case LFUN_CHARS_TRANSPOSE:
4001 if (cur.selIsMultiCell()) {
4004 getSelection(cur, rs, re, cs, ce);
4005 Cursor tmpcur = cur;
4006 for (row_type i = rs; i <= re; ++i) {
4007 for (col_type j = cs; j <= ce; ++j) {
4008 // cursor follows cell:
4009 tmpcur.idx() = tabular.cellIndex(i, j);
4010 // select this cell only:
4013 tmpcur.resetAnchor();
4014 tmpcur.pit() = tmpcur.lastpit();
4015 tmpcur.pos() = tmpcur.top().lastpos();
4016 tmpcur.setCursor(tmpcur);
4017 tmpcur.setSelection();
4018 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4023 cell(cur.idx())->dispatch(cur, cmd);
4027 case LFUN_INSET_SETTINGS:
4028 // relay this lfun to Inset, not to the cell.
4029 Inset::doDispatch(cur, cmd);
4033 // we try to handle this event in the insets dispatch function.
4034 cell(cur.idx())->dispatch(cur, cmd);
4040 // function sets an object as defined in func_status.h:
4041 // states OK, Unknown, Disabled, On, Off.
4042 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4043 FuncStatus & status) const
4045 switch (cmd.action) {
4046 case LFUN_TABULAR_FEATURE: {
4047 int action = Tabular::LAST_ACTION;
4049 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4050 string const tmp = tabularFeature[i].feature;
4051 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
4052 action = tabularFeature[i].action;
4056 if (action == Tabular::LAST_ACTION) {
4058 status.unknown(true);
4062 string const argument
4063 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
4065 row_type sel_row_start = 0;
4066 row_type sel_row_end = 0;
4067 col_type sel_col_start = 0;
4068 col_type sel_col_end = 0;
4069 Tabular::ltType dummyltt;
4072 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4075 case Tabular::SET_PWIDTH:
4076 case Tabular::SET_MPWIDTH:
4077 case Tabular::SET_SPECIAL_COLUMN:
4078 case Tabular::SET_SPECIAL_MULTICOLUMN:
4079 case Tabular::SET_SPECIAL_MULTIROW:
4080 case Tabular::APPEND_ROW:
4081 case Tabular::APPEND_COLUMN:
4082 case Tabular::DELETE_ROW:
4083 case Tabular::DELETE_COLUMN:
4084 case Tabular::COPY_ROW:
4085 case Tabular::COPY_COLUMN:
4086 case Tabular::SET_TOP_SPACE:
4087 case Tabular::SET_BOTTOM_SPACE:
4088 case Tabular::SET_INTERLINE_SPACE:
4092 case Tabular::MULTICOLUMN:
4093 // If a row is set as longtable caption, it must not be allowed
4094 // to unset that this row is a multicolumn.
4095 status.setEnabled(sel_row_start == sel_row_end
4096 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4097 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4100 case Tabular::MULTIROW:
4101 // If a row is set as longtable caption, it must not be allowed
4102 // to unset that this row is a multirow.
4103 status.setEnabled(sel_col_start == sel_col_end
4104 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4105 status.setOnOff(tabular.isMultiRow(cur.idx()));
4108 case Tabular::SET_ALL_LINES:
4109 case Tabular::UNSET_ALL_LINES:
4110 case Tabular::SET_BORDER_LINES:
4111 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4114 case Tabular::TOGGLE_LINE_TOP:
4115 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4116 status.setOnOff(tabular.topLine(cur.idx()));
4119 case Tabular::TOGGLE_LINE_BOTTOM:
4120 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4121 status.setOnOff(tabular.bottomLine(cur.idx()));
4124 case Tabular::TOGGLE_LINE_LEFT:
4125 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4126 status.setOnOff(tabular.leftLine(cur.idx()));
4129 case Tabular::TOGGLE_LINE_RIGHT:
4130 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4131 status.setOnOff(tabular.rightLine(cur.idx()));
4134 case Tabular::M_ALIGN_LEFT:
4136 case Tabular::ALIGN_LEFT:
4137 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4140 case Tabular::M_ALIGN_RIGHT:
4142 case Tabular::ALIGN_RIGHT:
4143 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4146 case Tabular::M_ALIGN_CENTER:
4148 case Tabular::ALIGN_CENTER:
4149 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4152 case Tabular::ALIGN_BLOCK:
4153 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
4154 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4157 case Tabular::M_VALIGN_TOP:
4159 case Tabular::VALIGN_TOP:
4161 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4164 case Tabular::M_VALIGN_BOTTOM:
4166 case Tabular::VALIGN_BOTTOM:
4168 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4171 case Tabular::M_VALIGN_MIDDLE:
4173 case Tabular::VALIGN_MIDDLE:
4175 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4178 case Tabular::SET_LONGTABULAR:
4179 status.setOnOff(tabular.is_long_tabular);
4182 case Tabular::UNSET_LONGTABULAR:
4183 status.setOnOff(!tabular.is_long_tabular);
4186 case Tabular::TOGGLE_ROTATE_TABULAR:
4187 case Tabular::SET_ROTATE_TABULAR:
4188 status.setOnOff(tabular.rotate);
4191 case Tabular::TABULAR_VALIGN_TOP:
4192 status.setOnOff(tabular.tabular_valignment
4193 == Tabular::LYX_VALIGN_TOP);
4195 case Tabular::TABULAR_VALIGN_MIDDLE:
4196 status.setOnOff(tabular.tabular_valignment
4197 == Tabular::LYX_VALIGN_MIDDLE);
4199 case Tabular::TABULAR_VALIGN_BOTTOM:
4200 status.setOnOff(tabular.tabular_valignment
4201 == Tabular::LYX_VALIGN_BOTTOM);
4204 case Tabular::LONGTABULAR_ALIGN_LEFT:
4205 status.setOnOff(tabular.longtabular_alignment
4206 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4208 case Tabular::LONGTABULAR_ALIGN_CENTER:
4209 status.setOnOff(tabular.longtabular_alignment
4210 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4212 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4213 status.setOnOff(tabular.longtabular_alignment
4214 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4217 case Tabular::UNSET_ROTATE_TABULAR:
4218 status.setOnOff(!tabular.rotate);
4221 case Tabular::TOGGLE_ROTATE_CELL:
4222 case Tabular::SET_ROTATE_CELL:
4223 status.setOnOff(!oneCellHasRotationState(false,
4224 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4227 case Tabular::UNSET_ROTATE_CELL:
4228 status.setOnOff(!oneCellHasRotationState(true,
4229 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4232 case Tabular::SET_USEBOX:
4233 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4236 // every row can only be one thing:
4237 // either a footer or header or caption
4238 case Tabular::SET_LTFIRSTHEAD:
4239 status.setEnabled(sel_row_start == sel_row_end
4240 && !tabular.ltCaption(sel_row_start));
4241 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4244 case Tabular::UNSET_LTFIRSTHEAD:
4245 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4248 case Tabular::SET_LTHEAD:
4249 status.setEnabled(sel_row_start == sel_row_end
4250 && !tabular.ltCaption(sel_row_start));
4251 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4254 case Tabular::UNSET_LTHEAD:
4255 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4258 case Tabular::SET_LTFOOT:
4259 status.setEnabled(sel_row_start == sel_row_end
4260 && !tabular.ltCaption(sel_row_start));
4261 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4264 case Tabular::UNSET_LTFOOT:
4265 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4268 case Tabular::SET_LTLASTFOOT:
4269 status.setEnabled(sel_row_start == sel_row_end
4270 && !tabular.ltCaption(sel_row_start));
4271 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4274 case Tabular::UNSET_LTLASTFOOT:
4275 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4278 case Tabular::SET_LTNEWPAGE:
4279 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4282 // only one row can be the caption
4283 case Tabular::TOGGLE_LTCAPTION:
4284 status.setEnabled(sel_row_start == sel_row_end
4285 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4286 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4287 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4288 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4289 && (!tabular.haveLTCaption()
4290 || tabular.ltCaption(sel_row_start)));
4291 status.setOnOff(tabular.ltCaption(sel_row_start));
4294 case Tabular::SET_BOOKTABS:
4295 status.setOnOff(tabular.use_booktabs);
4298 case Tabular::UNSET_BOOKTABS:
4299 status.setOnOff(!tabular.use_booktabs);
4304 status.setEnabled(false);
4310 // These are only enabled inside tabular
4311 case LFUN_CELL_BACKWARD:
4312 case LFUN_CELL_FORWARD:
4313 status.setEnabled(true);
4316 // disable these with multiple cells selected
4317 case LFUN_INSET_INSERT:
4318 case LFUN_TABULAR_INSERT:
4319 case LFUN_FLEX_INSERT:
4320 case LFUN_FLOAT_INSERT:
4321 case LFUN_FLOAT_WIDE_INSERT:
4322 case LFUN_FOOTNOTE_INSERT:
4323 case LFUN_MARGINALNOTE_INSERT:
4324 case LFUN_MATH_INSERT:
4325 case LFUN_MATH_MODE:
4326 case LFUN_MATH_MUTATE:
4327 case LFUN_MATH_DISPLAY:
4328 case LFUN_NOTE_INSERT:
4329 case LFUN_OPTIONAL_INSERT:
4330 case LFUN_BOX_INSERT:
4331 case LFUN_BRANCH_INSERT:
4332 case LFUN_PHANTOM_INSERT:
4333 case LFUN_WRAP_INSERT:
4334 case LFUN_ERT_INSERT: {
4335 if (cur.selIsMultiCell()) {
4336 status.setEnabled(false);
4339 return cell(cur.idx())->getStatus(cur, cmd, status);
4342 // disable in non-fixed-width cells
4343 case LFUN_NEWLINE_INSERT:
4344 case LFUN_BREAK_PARAGRAPH: {
4345 if (tabular.getPWidth(cur.idx()).zero()) {
4346 status.setEnabled(false);
4349 return cell(cur.idx())->getStatus(cur, cmd, status);
4352 case LFUN_NEWPAGE_INSERT:
4353 status.setEnabled(false);
4357 if (tabularStackDirty() && theClipboard().isInternal()) {
4358 if (cur.selIsMultiCell()) {
4361 getSelection(cur, rs, re, cs, ce);
4362 if (paste_tabular && paste_tabular->column_info.size() == ce - cs + 1
4363 && paste_tabular->row_info.size() == re - rs + 1)
4364 status.setEnabled(true);
4366 status.setEnabled(false);
4367 status.message(_("Selection size should match clipboard content."));
4370 status.setEnabled(true);
4373 return cell(cur.idx())->getStatus(cur, cmd, status);
4375 case LFUN_INSET_SETTINGS:
4376 // relay this lfun to Inset, not to the cell.
4377 return Inset::getStatus(cur, cmd, status);
4379 case LFUN_INSET_MODIFY:
4380 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
4381 status.setEnabled(true);
4387 // we try to handle this event in the insets dispatch function.
4388 return cell(cur.idx())->getStatus(cur, cmd, status);
4393 Inset::DisplayType InsetTabular::display() const
4395 if (tabular.is_long_tabular) {
4396 switch (tabular.longtabular_alignment) {
4397 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4399 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4401 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4411 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4413 return tabular.latex(os, runparams);
4417 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4419 os << '\n'; // output table on a new line
4420 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4421 tabular.plaintext(os, runparams, dp, false, 0);
4422 return PLAINTEXT_NEWLINE;
4426 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4431 // FIXME: Why not pass a proper DocIterator here?
4433 // if the table is inside a float it doesn't need the informaltable
4434 // wrapper. Search for it.
4435 for (master = owner(); master; master = master->owner())
4436 if (master->lyxCode() == FLOAT_CODE)
4441 os << "<informaltable>";
4444 ret += tabular.docbook(os, runparams);
4446 os << "</informaltable>";
4453 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4456 // It'd be better to be able to get this from an InsetLayout, but at present
4457 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4458 xs << html::StartTag("table");
4459 docstring ret = tabular.xhtml(xs, rp);
4460 xs << html::EndTag("table");
4465 void InsetTabular::validate(LaTeXFeatures & features) const
4467 tabular.validate(features);
4469 // It'd be better to be able to get this from an InsetLayout, but at present
4470 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4471 if (features.runparams().flavor == OutputParams::HTML)
4472 features.addPreambleSnippet("<style type=\"text/css\">\n"
4473 "table { border: 1px solid black; display: inline-block; }\n"
4474 "td { border: 1px solid black; padding: 0.5ex; }\n"
4479 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4481 return tabular.cellInset(idx);
4485 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4487 return tabular.cellInset(idx);
4491 void InsetTabular::cursorPos(BufferView const & bv,
4492 CursorSlice const & sl, bool boundary, int & x, int & y) const
4494 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4496 int const row = tabular.cellRow(sl.idx());
4497 int const col = tabular.cellColumn(sl.idx());
4499 // y offset correction
4500 for (int i = 0; i <= row; ++i) {
4501 if (tabular.isPartOfMultiRow(i, col))
4504 y += tabular.rowAscent(i);
4505 y += tabular.interRowSpace(i);
4508 y += tabular.rowDescent(i);
4510 y += tabular.textVOffset(sl.idx());
4512 // x offset correction
4513 for (int c = 0; c < col; ++c)
4514 x += tabular.column_info[c].width;
4516 x += tabular.textHOffset(sl.idx());
4517 x += ADD_TO_TABULAR_WIDTH;
4522 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4526 Inset const & inset = *tabular.cellInset(cell);
4527 Point o = bv.coordCache().getInsets().xy(&inset);
4528 int const xbeg = o.x_ - tabular.textHOffset(cell);
4529 int const xend = xbeg + tabular.columnWidth(cell);
4530 row_type const row = tabular.cellRow(cell);
4531 int const ybeg = o.y_ - tabular.rowAscent(row)
4532 - tabular.interRowSpace(row);
4533 int const yend = o.y_ + tabular.rowDescent(row);
4545 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4546 // << " ybeg=" << ybeg << " yend=" << yend
4547 // << " xx=" << xx << " yy=" << yy
4548 // << " dist=" << xx + yy << endl;
4553 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4555 //lyxerr << "InsetTabular::editXY: " << this << endl;
4556 cur.setSelection(false);
4558 cur.idx() = getNearestCell(cur.bv(), x, y);
4560 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4564 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4566 cur.idx() = getNearestCell(cur.bv(), x, y);
4567 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4571 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4573 idx_type idx_min = 0;
4574 int dist_min = numeric_limits<int>::max();
4575 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4576 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4577 int const d = dist(bv, i, x, y);
4588 int InsetTabular::cellXPos(idx_type const cell) const
4590 col_type col = tabular.cellColumn(cell);
4592 for (col_type c = 0; c < col; ++c)
4593 lx += tabular.columnWidth(c);
4599 void InsetTabular::resetPos(Cursor & cur) const
4601 BufferView & bv = cur.bv();
4602 int const maxwidth = bv.workWidth();
4604 int const scx_old = scx_;
4605 int const i = cur.find(this);
4610 int const X2 = maxwidth;
4611 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4612 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4613 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4616 scx_ = X1 + 20 - x1;
4618 scx_ = X2 - 20 - x2;
4623 // only update if offset changed
4624 if (scx_ != scx_old)
4625 cur.updateFlags(Update::Force | Update::FitCursor);
4629 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4631 row_type const row = tabular.cellRow(cur.idx());
4632 col_type const col = tabular.cellColumn(cur.idx());
4634 if (isRightToLeft(cur)) {
4635 if (tabular.cellColumn(cur.idx()) == 0) {
4636 if (row == tabular.row_info.size() - 1)
4638 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4643 cur.idx() = tabular.getLastCellInRow(row - 1);
4645 cur.idx() = tabular.cellIndex(row, col - 1);
4648 if (tabular.isLastCell(cur.idx()))
4650 if (tabular.isLastCellInRow(cur.idx()))
4651 cur.idx() = tabular.cellIndex(row + 1, 0);
4653 cur.idx() = tabular.cellIndex(row, col + 1);
4656 cur.boundary(false);
4658 if (cur.selIsMultiCell()) {
4659 cur.pit() = cur.lastpit();
4660 cur.pos() = cur.lastpos();
4668 // in visual mode, place cursor at extreme left or right
4670 switch(entry_from) {
4672 case ENTRY_DIRECTION_RIGHT:
4673 cur.posVisToRowExtremity(false /* !left */);
4675 case ENTRY_DIRECTION_LEFT:
4676 cur.posVisToRowExtremity(true /* left */);
4678 case ENTRY_DIRECTION_IGNORE:
4679 // nothing to do in this case
4683 cur.setCurrentFont();
4688 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4690 row_type const row = tabular.cellRow(cur.idx());
4691 col_type const col = tabular.cellColumn(cur.idx());
4693 if (isRightToLeft(cur)) {
4694 if (tabular.isLastCellInRow(cur.idx())) {
4697 cur.idx() = tabular.getFirstCellInRow(row);
4698 cur.idx() = tabular.cellAbove(cur.idx());
4700 if (tabular.isLastCell(cur.idx()))
4702 if (tabular.isLastCellInRow(cur.idx()))
4703 cur.idx() = tabular.cellIndex(row + 1, 0);
4705 cur.idx() = tabular.cellIndex(row, col + 1);
4708 if (cur.idx() == 0) // first cell
4711 cur.idx() = tabular.getLastCellInRow(row - 1);
4713 cur.idx() = tabular.cellIndex(row, col - 1);
4716 if (cur.selIsMultiCell()) {
4717 cur.pit() = cur.lastpit();
4718 cur.pos() = cur.lastpos();
4723 cur.pit() = cur.lastpit();
4724 cur.pos() = cur.lastpos();
4726 // in visual mode, place cursor at extreme left or right
4728 switch(entry_from) {
4730 case ENTRY_DIRECTION_RIGHT:
4731 cur.posVisToRowExtremity(false /* !left */);
4733 case ENTRY_DIRECTION_LEFT:
4734 cur.posVisToRowExtremity(true /* left */);
4736 case ENTRY_DIRECTION_IGNORE:
4737 // nothing to do in this case
4741 cur.setCurrentFont();
4746 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4748 Tabular::Feature action = Tabular::LAST_ACTION;
4751 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4752 string const tmp = tabularFeature[i].feature;
4754 if (tmp == what.substr(0, tmp.length())) {
4755 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4756 //tabularFeatures[i].feature.length()))
4757 action = tabularFeature[i].action;
4761 if (action == Tabular::LAST_ACTION)
4765 ltrim(what.substr(tabularFeature[i].feature.length()));
4766 tabularFeatures(cur, action, val);
4771 static void checkLongtableSpecial(Tabular::ltType & ltt,
4772 string const & special, bool & flag)
4774 if (special == "dl_above") {
4777 } else if (special == "dl_below") {
4778 ltt.bottomDL = flag;
4780 } else if (special == "empty") {
4789 bool InsetTabular::oneCellHasRotationState(bool rotated,
4790 row_type row_start, row_type row_end,
4791 col_type col_start, col_type col_end) const {
4793 for (row_type i = row_start; i <= row_end; ++i) {
4794 for (col_type j = col_start; j <= col_end; ++j) {
4795 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4804 void InsetTabular::tabularFeatures(Cursor & cur,
4805 Tabular::Feature feature, string const & value)
4807 col_type sel_col_start;
4808 col_type sel_col_end;
4809 row_type sel_row_start;
4810 row_type sel_row_end;
4811 bool setLines = false;
4812 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4813 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4817 case Tabular::M_ALIGN_LEFT:
4818 case Tabular::ALIGN_LEFT:
4819 setAlign = LYX_ALIGN_LEFT;
4822 case Tabular::M_ALIGN_RIGHT:
4823 case Tabular::ALIGN_RIGHT:
4824 setAlign = LYX_ALIGN_RIGHT;
4827 case Tabular::M_ALIGN_CENTER:
4828 case Tabular::ALIGN_CENTER:
4829 setAlign = LYX_ALIGN_CENTER;
4832 case Tabular::ALIGN_BLOCK:
4833 setAlign = LYX_ALIGN_BLOCK;
4836 case Tabular::M_VALIGN_TOP:
4837 case Tabular::VALIGN_TOP:
4838 setVAlign = Tabular::LYX_VALIGN_TOP;
4841 case Tabular::M_VALIGN_BOTTOM:
4842 case Tabular::VALIGN_BOTTOM:
4843 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4846 case Tabular::M_VALIGN_MIDDLE:
4847 case Tabular::VALIGN_MIDDLE:
4848 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4855 cur.recordUndoInset(ATOMIC_UNDO);
4857 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4858 row_type const row = tabular.cellRow(cur.idx());
4859 col_type const column = tabular.cellColumn(cur.idx());
4861 Tabular::ltType ltt;
4865 case Tabular::SET_PWIDTH: {
4866 Length const len(value);
4867 tabular.setColumnPWidth(cur, cur.idx(), len);
4869 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4870 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4874 case Tabular::SET_MPWIDTH:
4875 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4878 case Tabular::SET_SPECIAL_COLUMN:
4879 case Tabular::SET_SPECIAL_MULTICOLUMN:
4880 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4883 case Tabular::APPEND_ROW:
4884 // append the row into the tabular
4885 tabular.appendRow(cur.idx());
4888 case Tabular::APPEND_COLUMN:
4889 // append the column into the tabular
4890 tabular.appendColumn(cur.idx());
4891 cur.idx() = tabular.cellIndex(row, column);
4894 case Tabular::DELETE_ROW:
4895 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4896 tabular.deleteRow(sel_row_start);
4897 if (sel_row_start >= tabular.row_info.size())
4899 cur.idx() = tabular.cellIndex(sel_row_start, column);
4902 cur.setSelection(false);
4905 case Tabular::DELETE_COLUMN:
4906 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4907 tabular.deleteColumn(sel_col_start);
4908 if (sel_col_start >= tabular.column_info.size())
4910 cur.idx() = tabular.cellIndex(row, sel_col_start);
4913 cur.setSelection(false);
4916 case Tabular::COPY_ROW:
4917 tabular.copyRow(row);
4920 case Tabular::COPY_COLUMN:
4921 tabular.copyColumn(column);
4922 cur.idx() = tabular.cellIndex(row, column);
4925 case Tabular::TOGGLE_LINE_TOP: {
4926 bool lineSet = !tabular.topLine(cur.idx());
4927 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4928 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4929 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4933 case Tabular::TOGGLE_LINE_BOTTOM: {
4934 bool lineSet = !tabular.bottomLine(cur.idx());
4935 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4936 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4937 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4941 case Tabular::TOGGLE_LINE_LEFT: {
4942 bool lineSet = !tabular.leftLine(cur.idx());
4943 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4944 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4945 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4949 case Tabular::TOGGLE_LINE_RIGHT: {
4950 bool lineSet = !tabular.rightLine(cur.idx());
4951 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4952 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4953 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4957 case Tabular::M_ALIGN_LEFT:
4958 case Tabular::M_ALIGN_RIGHT:
4959 case Tabular::M_ALIGN_CENTER:
4961 case Tabular::ALIGN_LEFT:
4962 case Tabular::ALIGN_RIGHT:
4963 case Tabular::ALIGN_CENTER:
4964 case Tabular::ALIGN_BLOCK:
4965 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4966 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4967 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4970 case Tabular::M_VALIGN_TOP:
4971 case Tabular::M_VALIGN_BOTTOM:
4972 case Tabular::M_VALIGN_MIDDLE:
4974 case Tabular::VALIGN_TOP:
4975 case Tabular::VALIGN_BOTTOM:
4976 case Tabular::VALIGN_MIDDLE:
4977 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4978 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4979 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4982 case Tabular::MULTICOLUMN: {
4983 if (!cur.selection()) {
4984 // just multicol for one single cell
4985 // check whether we are completely in a multicol
4986 if (tabular.isMultiColumn(cur.idx()))
4987 tabular.unsetMultiColumn(cur.idx());
4989 tabular.setMultiColumn(cur.idx(), 1);
4992 // we have a selection so this means we just add all this
4993 // cells to form a multicolumn cell
4994 idx_type const s_start = cur.selBegin().idx();
4995 row_type const row_start = tabular.cellRow(s_start);
4996 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
4997 tabular.setMultiRow(s_start, row_end - row_start + 1);
4998 cur.idx() = s_start;
5001 cur.setSelection(false);
5005 case Tabular::MULTIROW: {
5006 if (!cur.selection()) {
5007 // just multirow for one single cell
5008 // check whether we are completely in a multirow
5009 if (tabular.isMultiRow(cur.idx()))
5010 tabular.unsetMultiRow(cur.idx());
5012 tabular.setMultiRow(cur.idx(), 1);
5015 // we have a selection so this means we just add all this
5016 // cells to form a multirow cell
5017 idx_type const s_start = cur.selBegin().idx();
5018 idx_type const s_end = cur.selEnd().idx();
5019 // the cell index is counted from left to right, we therefore
5020 // need to know the number of columns of the table to calculate
5021 // the number of selected rows
5022 idx_type const ncolumns = tabular.column_info.size();
5023 tabular.setMultiRow(s_start, (s_end - s_start)/ncolumns + 1);
5024 cur.idx() = s_start;
5027 cur.setSelection(false);
5031 case Tabular::SET_ALL_LINES:
5033 case Tabular::UNSET_ALL_LINES:
5034 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5035 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5036 tabular.setAllLines(
5037 tabular.cellIndex(i,j), setLines);
5040 case Tabular::SET_BORDER_LINES:
5041 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5042 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
5043 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
5045 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
5046 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
5047 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
5051 case Tabular::SET_LONGTABULAR:
5052 tabular.is_long_tabular = true;
5055 case Tabular::UNSET_LONGTABULAR:
5056 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
5057 if (tabular.ltCaption(i)) {
5058 cur.idx() = tabular.cellIndex(i, 0);
5061 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5064 tabular.is_long_tabular = false;
5067 case Tabular::SET_ROTATE_TABULAR:
5068 tabular.rotate = true;
5071 case Tabular::UNSET_ROTATE_TABULAR:
5072 tabular.rotate = false;
5075 case Tabular::TOGGLE_ROTATE_TABULAR:
5076 tabular.rotate = !tabular.rotate;
5079 case Tabular::TABULAR_VALIGN_TOP:
5080 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5083 case Tabular::TABULAR_VALIGN_MIDDLE:
5084 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5087 case Tabular::TABULAR_VALIGN_BOTTOM:
5088 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5091 case Tabular::LONGTABULAR_ALIGN_LEFT:
5092 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5095 case Tabular::LONGTABULAR_ALIGN_CENTER:
5096 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5099 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5100 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5105 case Tabular::SET_ROTATE_CELL:
5106 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5107 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5108 tabular.setRotateCell(
5109 tabular.cellIndex(i, j), true);
5112 case Tabular::UNSET_ROTATE_CELL:
5113 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5114 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5115 tabular.setRotateCell(tabular.cellIndex(i, j), false);
5118 case Tabular::TOGGLE_ROTATE_CELL:
5120 bool oneNotRotated = oneCellHasRotationState(false,
5121 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5123 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5124 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5125 tabular.setRotateCell(tabular.cellIndex(i, j),
5130 case Tabular::SET_USEBOX: {
5131 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5132 if (val == tabular.getUsebox(cur.idx()))
5133 val = Tabular::BOX_NONE;
5134 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5135 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5136 tabular.setUsebox(tabular.cellIndex(i, j), val);
5140 case Tabular::UNSET_LTFIRSTHEAD:
5142 case Tabular::SET_LTFIRSTHEAD:
5143 tabular.getRowOfLTFirstHead(row, ltt);
5144 checkLongtableSpecial(ltt, value, flag);
5145 tabular.setLTHead(row, flag, ltt, true);
5148 case Tabular::UNSET_LTHEAD:
5150 case Tabular::SET_LTHEAD:
5151 tabular.getRowOfLTHead(row, ltt);
5152 checkLongtableSpecial(ltt, value, flag);
5153 tabular.setLTHead(row, flag, ltt, false);
5156 case Tabular::UNSET_LTFOOT:
5158 case Tabular::SET_LTFOOT:
5159 tabular.getRowOfLTFoot(row, ltt);
5160 checkLongtableSpecial(ltt, value, flag);
5161 tabular.setLTFoot(row, flag, ltt, false);
5164 case Tabular::UNSET_LTLASTFOOT:
5166 case Tabular::SET_LTLASTFOOT:
5167 tabular.getRowOfLTLastFoot(row, ltt);
5168 checkLongtableSpecial(ltt, value, flag);
5169 tabular.setLTFoot(row, flag, ltt, true);
5172 case Tabular::SET_LTNEWPAGE:
5173 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5176 case Tabular::TOGGLE_LTCAPTION: {
5177 bool const set = !tabular.ltCaption(row);
5178 cur.idx() = tabular.setLTCaption(row, set);
5181 cur.setSelection(false);
5184 // When a row is set as caption, then also insert
5185 // a caption. Otherwise the LaTeX output is broken.
5186 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5187 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5189 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5190 if (lyx::getStatus(fr).enabled())
5196 case Tabular::SET_BOOKTABS:
5197 tabular.use_booktabs = true;
5200 case Tabular::UNSET_BOOKTABS:
5201 tabular.use_booktabs = false;
5204 case Tabular::SET_TOP_SPACE: {
5206 if (value == "default")
5207 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5208 tabular.row_info[i].top_space_default = true;
5209 else if (isValidLength(value, &len))
5210 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5211 tabular.row_info[i].top_space_default = false;
5212 tabular.row_info[i].top_space = len;
5215 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5216 tabular.row_info[i].top_space_default = false;
5217 tabular.row_info[i].top_space = len;
5222 case Tabular::SET_BOTTOM_SPACE: {
5224 if (value == "default")
5225 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5226 tabular.row_info[i].bottom_space_default = true;
5227 else if (isValidLength(value, &len))
5228 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5229 tabular.row_info[i].bottom_space_default = false;
5230 tabular.row_info[i].bottom_space = len;
5233 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5234 tabular.row_info[i].bottom_space_default = false;
5235 tabular.row_info[i].bottom_space = len;
5240 case Tabular::SET_INTERLINE_SPACE: {
5242 if (value == "default")
5243 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5244 tabular.row_info[i].interline_space_default = true;
5245 else if (isValidLength(value, &len))
5246 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5247 tabular.row_info[i].interline_space_default = false;
5248 tabular.row_info[i].interline_space = len;
5251 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5252 tabular.row_info[i].interline_space_default = false;
5253 tabular.row_info[i].interline_space = len;
5258 // dummy stuff just to avoid warnings
5259 case Tabular::LAST_ACTION:
5265 bool InsetTabular::showInsetDialog(BufferView * bv) const
5267 bv->showDialog("tabular", params2string(*this),
5268 const_cast<InsetTabular *>(this));
5273 void InsetTabular::openLayoutDialog(BufferView * bv) const
5275 bv->showDialog("tabular", params2string(*this),
5276 const_cast<InsetTabular *>(this));
5280 bool InsetTabular::copySelection(Cursor & cur)
5282 if (!cur.selection())
5287 getSelection(cur, rs, re, cs, ce);
5289 paste_tabular.reset(new Tabular(tabular));
5291 for (row_type i = 0; i < rs; ++i)
5292 paste_tabular->deleteRow(0);
5294 row_type const rows = re - rs + 1;
5295 while (paste_tabular->row_info.size() > rows)
5296 paste_tabular->deleteRow(rows);
5298 for (col_type i = 0; i < cs; ++i)
5299 paste_tabular->deleteColumn(0);
5301 col_type const columns = ce - cs + 1;
5302 while (paste_tabular->column_info.size() > columns)
5303 paste_tabular->deleteColumn(columns);
5305 paste_tabular->setBuffer(tabular.buffer());
5307 odocstringstream os;
5308 OutputParams const runparams(0);
5309 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5310 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5311 cap::copySelection(cur, os.str());
5313 // mark tabular stack dirty
5314 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5315 // when we (hopefully) have a one-for-all paste mechanism.
5316 // This must be called after cap::copySelection.
5317 dirtyTabularStack(true);
5323 bool InsetTabular::pasteClipboard(Cursor & cur)
5327 col_type actcol = tabular.cellColumn(cur.idx());
5328 row_type actrow = tabular.cellRow(cur.idx());
5330 if (cur.selIsMultiCell()) {
5333 getSelection(cur, actrow, re, actcol, ce);
5336 for (row_type r1 = 0, r2 = actrow;
5337 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
5339 for (col_type c1 = 0, c2 = actcol;
5340 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
5342 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5343 tabular.isPartOfMultiColumn(r2, c2))
5345 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5349 if (tabular.isPartOfMultiColumn(r2, c2)) {
5353 shared_ptr<InsetTableCell> inset(
5354 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5355 tabular.setCellInset(r2, c2, inset);
5356 // FIXME?: why do we need to do this explicitly? (EL)
5357 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5359 // FIXME: change tracking (MG)
5360 inset->setChange(Change(buffer().params().trackChanges ?
5361 Change::INSERTED : Change::UNCHANGED));
5369 void InsetTabular::cutSelection(Cursor & cur)
5371 if (!cur.selection())
5376 getSelection(cur, rs, re, cs, ce);
5377 for (row_type i = rs; i <= re; ++i) {
5378 for (col_type j = cs; j <= ce; ++j) {
5379 shared_ptr<InsetTableCell> t
5380 = cell(tabular.cellIndex(i, j));
5381 if (buffer().params().trackChanges)
5382 // FIXME: Change tracking (MG)
5383 t->setChange(Change(Change::DELETED));
5389 // cursor position might be invalid now
5390 if (cur.pit() > cur.lastpit())
5391 cur.pit() = cur.lastpit();
5392 if (cur.pos() > cur.lastpos())
5393 cur.pos() = cur.lastpos();
5394 cur.clearSelection();
5398 bool InsetTabular::isRightToLeft(Cursor & cur) const
5400 LASSERT(cur.depth() > 1, /**/);
5401 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5402 pos_type const parentpos = cur[cur.depth() - 2].pos();
5403 return parentpar.getFontSettings(buffer().params(),
5404 parentpos).language()->rightToLeft();
5407 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5410 LASSERT(stidx <= enidx, return docstring());
5412 col_type const col1 = tabular.cellColumn(stidx);
5413 col_type const col2 = tabular.cellColumn(enidx);
5414 row_type const row1 = tabular.cellRow(stidx);
5415 row_type const row2 = tabular.cellRow(enidx);
5417 for (col_type col = col1; col <= col2; col++)
5418 for (row_type row = row1; row <= row2; row++) {
5423 retval += tabular.cellInset(row, col)->asString(intoInsets);
5429 void InsetTabular::getSelection(Cursor & cur,
5430 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5432 CursorSlice const & beg = cur.selBegin();
5433 CursorSlice const & end = cur.selEnd();
5434 cs = tabular.cellColumn(beg.idx());
5435 ce = tabular.cellColumn(end.idx());
5438 cs = tabular.cellColumn(end.idx());
5440 ce = tabular.cellRightColumn(end.idx());
5443 rs = tabular.cellRow(beg.idx());
5444 re = tabular.cellRow(end.idx());
5450 Text * InsetTabular::getText(int idx) const
5452 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5456 void InsetTabular::setChange(Change const & change)
5458 for (idx_type idx = 0; idx < nargs(); ++idx)
5459 cell(idx)->setChange(change);
5463 void InsetTabular::acceptChanges()
5465 for (idx_type idx = 0; idx < nargs(); ++idx)
5466 cell(idx)->acceptChanges();
5470 void InsetTabular::rejectChanges()
5472 for (idx_type idx = 0; idx < nargs(); ++idx)
5473 cell(idx)->rejectChanges();
5477 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5479 return tabular.getPWidth(cell).zero();
5483 bool InsetTabular::forcePlainLayout(idx_type cell) const
5485 return !tabular.getPWidth(cell).zero();
5489 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5492 if (buf.length() <= 0)
5497 col_type maxCols = 1;
5498 size_t const len = buf.length();
5502 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5510 maxCols = max(cols, maxCols);
5516 maxCols = max(cols, maxCols);
5522 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5523 loctab = paste_tabular.get();
5525 dirtyTabularStack(true);
5528 cell = bv.cursor().idx();
5529 ocol = tabular.cellColumn(cell);
5530 row = tabular.cellRow(cell);
5534 idx_type const cells = loctab->numberofcells;
5537 rows = loctab->row_info.size();
5538 col_type const columns = loctab->column_info.size();
5540 while (cell < cells && p < len && row < rows &&
5541 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5547 // we can only set this if we are not too far right
5548 if (cols < columns) {
5549 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5550 Font const font = bv.textMetrics(&inset->text()).
5552 inset->setText(buf.substr(op, p - op), font,
5553 buffer().params().trackChanges);
5559 // we can only set this if we are not too far right
5560 if (cols < columns) {
5561 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5562 Font const font = bv.textMetrics(&inset->text()).
5564 inset->setText(buf.substr(op, p - op), font,
5565 buffer().params().trackChanges);
5570 cell = loctab->cellIndex(row, cols);
5576 // check for the last cell if there is no trailing '\n'
5577 if (cell < cells && op < len) {
5578 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5579 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5580 inset->setText(buf.substr(op, len - op), font,
5581 buffer().params().trackChanges);
5587 void InsetTabular::addPreview(DocIterator const & inset_pos,
5588 PreviewLoader & loader) const
5590 row_type const rows = tabular.row_info.size();
5591 col_type const columns = tabular.column_info.size();
5592 DocIterator cell_pos = inset_pos;
5594 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5595 for (row_type i = 0; i < rows; ++i) {
5596 for (col_type j = 0; j < columns; ++j) {
5597 cell_pos.top().idx() = tabular.cellIndex(i, j);
5598 tabular.cellInset(i, j)->addPreview(cell_pos, loader);
5604 bool InsetTabular::completionSupported(Cursor const & cur) const
5606 Cursor const & bvCur = cur.bv().cursor();
5607 if (&bvCur.inset() != this)
5609 return cur.text()->completionSupported(cur);
5613 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5615 return completionSupported(cur);
5619 bool InsetTabular::automaticInlineCompletion() const
5621 return lyxrc.completion_inline_text;
5625 bool InsetTabular::automaticPopupCompletion() const
5627 return lyxrc.completion_popup_text;
5631 bool InsetTabular::showCompletionCursor() const
5633 return lyxrc.completion_cursor_text;
5637 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5639 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5643 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5645 if (!completionSupported(cur))
5647 return cur.text()->completionPrefix(cur);
5651 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5653 if (!completionSupported(cur))
5656 return cur.text()->insertCompletion(cur, s, finished);
5660 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5661 Dimension & dim) const
5663 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5664 tm.completionPosAndDim(cur, x, y, dim);
5668 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5670 istringstream data(in);
5672 lex.setStream(data);
5679 if (!lex || token != "tabular") {
5680 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5684 // This is part of the inset proper that is usually swallowed
5685 // by Buffer::readInset
5687 if (!lex || token != "Tabular") {
5688 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5696 string InsetTabular::params2string(InsetTabular const & inset)
5699 data << "tabular" << ' ';
5701 data << "\\end_inset\n";