2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/Application.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #include "frontends/Selection.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/gettext.h"
58 #include "support/lstrings.h"
60 #include <boost/scoped_ptr.hpp>
68 using namespace lyx::support;
70 using boost::shared_ptr;
71 using boost::dynamic_pointer_cast;
76 using cap::dirtyTabularStack;
77 using cap::tabularStackDirty;
79 using graphics::PreviewLoader;
81 using frontend::Painter;
82 using frontend::Clipboard;
84 namespace Alert = frontend::Alert;
89 int const ADD_TO_HEIGHT = 2; // in cell
90 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
91 int const default_line_space = 10; // ?
92 int const WIDTH_OF_LINE = 5; // space between double lines
96 boost::scoped_ptr<Tabular> paste_tabular;
99 struct TabularFeature {
100 Tabular::Feature action;
105 TabularFeature tabularFeature[] =
107 { Tabular::APPEND_ROW, "append-row" },
108 { Tabular::APPEND_COLUMN, "append-column" },
109 { Tabular::DELETE_ROW, "delete-row" },
110 { Tabular::DELETE_COLUMN, "delete-column" },
111 { Tabular::COPY_ROW, "copy-row" },
112 { Tabular::COPY_COLUMN, "copy-column" },
113 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
114 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
115 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
116 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
117 { Tabular::ALIGN_LEFT, "align-left" },
118 { Tabular::ALIGN_RIGHT, "align-right" },
119 { Tabular::ALIGN_CENTER, "align-center" },
120 { Tabular::ALIGN_BLOCK, "align-block" },
121 { Tabular::VALIGN_TOP, "valign-top" },
122 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
123 { Tabular::VALIGN_MIDDLE, "valign-middle" },
124 { Tabular::M_ALIGN_LEFT, "m-align-left" },
125 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
126 { Tabular::M_ALIGN_CENTER, "m-align-center" },
127 { Tabular::M_VALIGN_TOP, "m-valign-top" },
128 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
129 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
130 { Tabular::MULTICOLUMN, "multicolumn" },
131 { Tabular::SET_ALL_LINES, "set-all-lines" },
132 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
133 { Tabular::SET_LONGTABULAR, "set-longtabular" },
134 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
135 { Tabular::SET_PWIDTH, "set-pwidth" },
136 { Tabular::SET_MPWIDTH, "set-mpwidth" },
137 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
138 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
139 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
140 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
141 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
142 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
143 { Tabular::SET_USEBOX, "set-usebox" },
144 { Tabular::SET_LTHEAD, "set-lthead" },
145 { Tabular::UNSET_LTHEAD, "unset-lthead" },
146 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
147 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
148 { Tabular::SET_LTFOOT, "set-ltfoot" },
149 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
150 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
151 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
152 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
153 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
154 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
155 { Tabular::SET_BOOKTABS, "set-booktabs" },
156 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
157 { Tabular::SET_TOP_SPACE, "set-top-space" },
158 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
159 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
160 { Tabular::SET_BORDER_LINES, "set-border-lines" },
161 { Tabular::LAST_ACTION, "" }
165 class FeatureEqual : public unary_function<TabularFeature, bool> {
167 FeatureEqual(Tabular::Feature feature)
168 : feature_(feature) {}
169 bool operator()(TabularFeature const & tf) const {
170 return tf.action == feature_;
173 Tabular::Feature feature_;
178 string const write_attribute(string const & name, T const & t)
180 string const s = tostr(t);
181 return s.empty() ? s : " " + name + "=\"" + s + "\"";
185 string const write_attribute(string const & name, string const & t)
187 return t.empty() ? t : " " + name + "=\"" + t + "\"";
192 string const write_attribute(string const & name, docstring const & t)
194 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
199 string const write_attribute(string const & name, bool const & b)
201 // we write only true attribute values so we remove a bit of the
202 // file format bloat for tabulars.
203 return b ? write_attribute(name, convert<string>(b)) : string();
208 string const write_attribute(string const & name, int const & i)
210 // we write only true attribute values so we remove a bit of the
211 // file format bloat for tabulars.
212 return i ? write_attribute(name, convert<string>(i)) : string();
217 string const write_attribute(string const & name, Tabular::idx_type const & i)
219 // we write only true attribute values so we remove a bit of the
220 // file format bloat for tabulars.
221 return i ? write_attribute(name, convert<string>(i)) : string();
226 string const write_attribute(string const & name, Length const & value)
228 // we write only the value if we really have one same reson as above.
229 return value.zero() ? string() : write_attribute(name, value.asString());
233 string const tostr(LyXAlignment const & num)
238 case LYX_ALIGN_BLOCK:
242 case LYX_ALIGN_CENTER:
244 case LYX_ALIGN_RIGHT:
246 case LYX_ALIGN_LAYOUT:
248 case LYX_ALIGN_SPECIAL:
255 string const tostr(Tabular::VAlignment const & num)
258 case Tabular::LYX_VALIGN_TOP:
260 case Tabular::LYX_VALIGN_MIDDLE:
262 case Tabular::LYX_VALIGN_BOTTOM:
269 string const tostr(Tabular::BoxType const & num)
272 case Tabular::BOX_NONE:
274 case Tabular::BOX_PARBOX:
276 case Tabular::BOX_MINIPAGE:
283 // I would have liked a fromstr template a lot better. (Lgb)
284 bool string2type(string const str, LyXAlignment & num)
287 num = LYX_ALIGN_NONE;
288 else if (str == "block")
289 num = LYX_ALIGN_BLOCK;
290 else if (str == "left")
291 num = LYX_ALIGN_LEFT;
292 else if (str == "center")
293 num = LYX_ALIGN_CENTER;
294 else if (str == "right")
295 num = LYX_ALIGN_RIGHT;
302 bool string2type(string const str, Tabular::VAlignment & num)
305 num = Tabular::LYX_VALIGN_TOP;
306 else if (str == "middle" )
307 num = Tabular::LYX_VALIGN_MIDDLE;
308 else if (str == "bottom")
309 num = Tabular::LYX_VALIGN_BOTTOM;
316 bool string2type(string const str, Tabular::BoxType & num)
319 num = Tabular::BOX_NONE;
320 else if (str == "parbox")
321 num = Tabular::BOX_PARBOX;
322 else if (str == "minipage")
323 num = Tabular::BOX_MINIPAGE;
330 bool string2type(string const str, bool & num)
334 else if (str == "false")
342 bool getTokenValue(string const & str, char const * token, string & ret)
345 size_t token_length = strlen(token);
346 size_t pos = str.find(token);
348 if (pos == string::npos || pos + token_length + 1 >= str.length()
349 || str[pos + token_length] != '=')
351 pos += token_length + 1;
353 if (ch != '"' && ch != '\'') { // only read till next space
357 while (pos < str.length() - 1 && str[++pos] != ch)
364 bool getTokenValue(string const & str, char const * token, docstring & ret)
367 bool const success = getTokenValue(str, token, tmp);
368 ret = from_utf8(tmp);
373 bool getTokenValue(string const & str, char const * token, int & num)
377 if (!getTokenValue(str, token, tmp))
379 num = convert<int>(tmp);
384 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
387 return getTokenValue(str, token, tmp) && string2type(tmp, num);
391 bool getTokenValue(string const & str, char const * token,
392 Tabular::VAlignment & num)
395 return getTokenValue(str, token, tmp) && string2type(tmp, num);
399 bool getTokenValue(string const & str, char const * token,
400 Tabular::BoxType & num)
403 return getTokenValue(str, token, tmp) && string2type(tmp, num);
407 bool getTokenValue(string const & str, char const * token, bool & flag)
409 // set the flag always to false as this should be the default for bools
410 // not in the file-format.
413 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
417 bool getTokenValue(string const & str, char const * token, Length & len)
419 // set the length to be zero() as default as this it should be if not
420 // in the file format.
423 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
427 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
432 if (!getTokenValue(str, token, tmp))
434 if (tmp == "default") {
438 return isValidLength(tmp, &len);
442 void l_getline(istream & is, string & str)
445 while (str.empty()) {
447 if (!str.empty() && str[str.length() - 1] == '\r')
448 str.erase(str.length() - 1);
455 string const featureAsString(Tabular::Feature feature)
457 TabularFeature * end = tabularFeature +
458 sizeof(tabularFeature) / sizeof(TabularFeature);
459 TabularFeature * it = find_if(tabularFeature, end,
460 FeatureEqual(feature));
461 return (it == end) ? string() : it->feature;
466 /////////////////////////////////////////////////////////////////////
470 /////////////////////////////////////////////////////////////////////
473 Tabular::CellData::CellData(Buffer const & buf, Tabular const & table)
476 multicolumn(Tabular::CELL_NORMAL),
477 alignment(LYX_ALIGN_CENTER),
478 valignment(LYX_VALIGN_TOP),
485 inset(new InsetTableCell(buf, this, &table))
487 inset->setBuffer(const_cast<Buffer &>(buf));
488 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
492 Tabular::CellData::CellData(CellData const & cs)
495 multicolumn(cs.multicolumn),
496 alignment(cs.alignment),
497 valignment(cs.valignment),
498 top_line(cs.top_line),
499 bottom_line(cs.bottom_line),
500 left_line(cs.left_line),
501 right_line(cs.right_line),
504 align_special(cs.align_special),
506 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
508 inset->setCellData(this);
512 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
519 void Tabular::CellData::swap(CellData & rhs)
521 std::swap(cellno, rhs.cellno);
522 std::swap(width, rhs.width);
523 std::swap(multicolumn, rhs.multicolumn);
524 std::swap(alignment, rhs.alignment);
525 std::swap(valignment, rhs.valignment);
526 std::swap(top_line, rhs.top_line);
527 std::swap(bottom_line, rhs.bottom_line);
528 std::swap(left_line, rhs.left_line);
529 std::swap(right_line, rhs.right_line);
530 std::swap(usebox, rhs.usebox);
531 std::swap(rotate, rhs.rotate);
532 std::swap(align_special, rhs.align_special);
533 p_width.swap(rhs.p_width);
534 inset.swap(rhs.inset);
538 Tabular::RowData::RowData()
541 top_space_default(false),
542 bottom_space_default(false),
543 interline_space_default(false),
552 Tabular::ColumnData::ColumnData()
553 : alignment(LYX_ALIGN_CENTER),
554 valignment(LYX_VALIGN_TOP),
560 Tabular::ltType::ltType()
573 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
575 init(buffer, rows_arg, columns_arg);
579 // activates all lines and sets all widths to 0
580 void Tabular::init(Buffer const & buf, row_type rows_arg,
581 col_type columns_arg)
584 row_info = row_vector(rows_arg);
585 column_info = column_vector(columns_arg);
586 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf, *this)));
587 row_info.reserve(10);
588 column_info.reserve(10);
589 cell_info.reserve(100);
591 is_long_tabular = false;
593 use_booktabs = false;
594 // set silly default lines
595 for (row_type i = 0; i < rowCount(); ++i)
596 for (col_type j = 0; j < columnCount(); ++j) {
597 cell_info[i][j].top_line = true;
598 cell_info[i][j].left_line = true;
599 cell_info[i][j].bottom_line = i == 0 || i == rowCount() - 1;
600 cell_info[i][j].right_line = j == columnCount() - 1;
605 void Tabular::appendRow(idx_type const cell)
607 BufferParams const & bp = buffer().params();
608 row_type const row = cellRow(cell);
610 row_vector::iterator rit = row_info.begin() + row;
611 row_info.insert(rit, RowData());
612 // now set the values of the row before
613 row_info[row] = row_info[row + 1];
615 row_type const nrows = rowCount();
616 col_type const ncols = columnCount();
618 cell_vvector old(nrows - 1);
619 for (row_type i = 0; i < nrows - 1; ++i)
620 swap(cell_info[i], old[i]);
622 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer(), *this)));
624 for (row_type i = 0; i <= row; ++i)
625 swap(cell_info[i], old[i]);
626 for (row_type i = row + 2; i < nrows; ++i)
627 swap(cell_info[i], old[i - 1]);
630 for (col_type c = 0; c < ncols; ++c) {
631 // inherit line settings
632 idx_type const i = cellIndex(row + 1, c);
633 idx_type const j = cellIndex(row, c);
634 setLeftLine(i, isPartOfMultiColumn(row, c) ? false : leftLine(j));
635 if (cell_info[row][c].multicolumn == CELL_NORMAL || c == ncols - 1
637 && cell_info[row][c].multicolumn != CELL_NORMAL
638 && cell_info[row][c + 1].multicolumn == CELL_NORMAL))
639 setRightLine(i, rightLine(j));
641 setRightLine(i, false);
642 // mark track changes
644 cellInfo(i).inset->setChange(Change(Change::INSERTED));
649 void Tabular::deleteRow(row_type const row)
651 // Not allowed to delete last row
655 row_info.erase(row_info.begin() + row);
656 cell_info.erase(cell_info.begin() + row);
661 void Tabular::copyRow(row_type const row)
663 row_info.insert(row_info.begin() + row, row_info[row]);
664 cell_info.insert(cell_info.begin() + row, cell_info[row]);
666 if (buffer().params().trackChanges)
667 for (col_type j = 0; j < columnCount(); ++j)
668 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
674 void Tabular::appendColumn(idx_type const cell)
676 col_type const column = cellColumn(cell);
677 column_vector::iterator cit = column_info.begin() + column + 1;
678 column_info.insert(cit, ColumnData());
679 row_type const nrows = rowCount();
680 col_type const ncols = columnCount();
681 // set the column values of the column before
682 column_info[column + 1] = column_info[column];
684 for (row_type r = 0; r < nrows; ++r) {
685 cell_info[r].insert(cell_info[r].begin() + column + 1,
686 CellData(buffer(), *this));
687 col_type c = column + 2;
689 && cell_info[r][c].multicolumn == CELL_PART_OF_MULTICOLUMN) {
690 cell_info[r][c].multicolumn = CELL_NORMAL;
695 for (row_type r = 0; r < nrows; ++r) {
696 // inherit line settings
697 idx_type const i = cellIndex(r, column + 1);
698 idx_type const j = cellIndex(r, column);
699 setBottomLine(i, bottomLine(j));
700 setTopLine(i, topLine(j));
702 cellInfo(i).inset->clear();
703 if (buffer().params().trackChanges)
704 cellInfo(i).inset->setChange(Change(Change::INSERTED));
709 void Tabular::deleteColumn(col_type const column)
711 // Not allowed to delete last column
712 if (columnCount() == 1)
715 column_info.erase(column_info.begin() + column);
716 for (row_type i = 0; i < rowCount(); ++i) {
717 // Care about multicolumn cells
718 if (column + 1 < columnCount() &&
719 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
720 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
721 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
723 cell_info[i].erase(cell_info[i].begin() + column);
729 void Tabular::copyColumn(col_type const column)
731 BufferParams const & bp = buffer().params();
732 column_info.insert(column_info.begin() + column, column_info[column]);
734 for (row_type i = 0; i < rowCount(); ++i)
735 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
738 for (row_type i = 0; i < rowCount(); ++i)
739 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
744 void Tabular::updateIndexes()
746 col_type ncols = columnCount();
747 row_type nrows = rowCount();
749 for (row_type row = 0; row < nrows; ++row)
750 for (col_type column = 0; column < ncols; ++column) {
751 if (!isPartOfMultiColumn(row, column))
753 cell_info[row][column].cellno = numberofcells - 1;
756 rowofcell.resize(numberofcells);
757 columnofcell.resize(numberofcells);
759 for (row_type row = 0; row < nrows; ++row)
760 for (col_type column = 0; column < ncols; ++column) {
761 if (isPartOfMultiColumn(row, column))
764 columnofcell[i] = column;
770 Tabular::idx_type Tabular::cellCount() const
772 return numberofcells;
776 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
778 row_type const row = cellRow(cell);
780 for (col_type i = 0; i < columnCount(); ++i)
781 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
787 bool Tabular::topLine(idx_type const cell) const
789 return cellInfo(cell).top_line;
793 bool Tabular::bottomLine(idx_type const cell) const
795 return cellInfo(cell).bottom_line;
799 bool Tabular::leftLine(idx_type cell) const
803 return cellInfo(cell).left_line;
807 bool Tabular::rightLine(idx_type cell) const
811 return cellInfo(cell).right_line;
815 bool Tabular::topAlreadyDrawn(idx_type cell) const
817 row_type row = cellRow(cell);
820 idx_type i = cellIndex(row - 1, cellColumn(cell));
821 return !rowBottomLine(row - 1) && bottomLine(i);
825 bool Tabular::leftAlreadyDrawn(idx_type cell) const
827 col_type col = cellColumn(cell);
830 idx_type i = cellIndex(cellRow(cell), col - 1);
831 return rightLine(i) && !leftLine(cell);
835 bool Tabular::isLastRow(idx_type cell) const
837 return cellRow(cell) == rowCount() - 1;
841 int Tabular::getAdditionalHeight(row_type row) const
843 if (!row || row >= rowCount())
846 int const interline_space = row_info[row - 1].interline_space_default ?
848 row_info[row - 1].interline_space.inPixels(width());
849 if (rowTopLine(row) && rowBottomLine(row - 1))
850 return interline_space + WIDTH_OF_LINE;
851 return interline_space;
855 int Tabular::getAdditionalWidth(idx_type cell) const
857 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
859 && nextcol < columnCount() && leftLine(cellIndex(cellRow(cell), nextcol)))
860 return WIDTH_OF_LINE;
865 int Tabular::columnWidth(idx_type cell) const
868 col_type const span = columnSpan(cell);
869 col_type const col = cellColumn(cell);
870 for(col_type c = col; c < col + span ; ++c)
871 w += column_info[c].width;
876 bool Tabular::updateColumnWidths()
878 col_type const ncols = columnCount();
879 row_type const nrows = rowCount();
881 // for each col get max of single col cells
882 for(col_type c = 0; c < ncols; ++c) {
884 for(row_type r = 0; r < nrows; ++r) {
885 idx_type const i = cellIndex(r, c);
886 if (columnSpan(i) == 1)
887 new_width = max(new_width, cellInfo(i).width);
890 if (column_info[c].width != new_width) {
891 column_info[c].width = new_width;
895 // update col widths to fit merged cells
896 for(col_type c = 0; c < ncols; ++c)
897 for(row_type r = 0; r < nrows; ++r) {
898 idx_type const i = cellIndex(r, c);
899 int const span = columnSpan(i);
900 if (span == 1 || c > cellColumn(i))
904 for(col_type j = c; j < c + span ; ++j)
905 old_width += column_info[j].width;
907 if (cellInfo(i).width > old_width) {
908 column_info[c + span - 1].width += cellInfo(i).width - old_width;
917 int Tabular::width() const
919 col_type const ncols = columnCount();
921 for (col_type i = 0; i < ncols; ++i)
922 width += column_info[i].width;
927 void Tabular::setCellWidth(idx_type cell, int new_width)
929 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
930 + getAdditionalWidth(cell);
934 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
937 if (!isMultiColumn(cell) || onlycolumn)
938 column_info[cellColumn(cell)].alignment = align;
940 cellInfo(cell).alignment = align;
944 void Tabular::setVAlignment(idx_type cell, VAlignment align,
947 if (!isMultiColumn(cell) || onlycolumn)
948 column_info[cellColumn(cell)].valignment = align;
950 cellInfo(cell).valignment = align;
957 * Allow line and paragraph breaks for fixed width cells or disallow them,
958 * merge cell paragraphs and reset layout to standard for variable width
961 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
963 inset->setAutoBreakRows(fixedWidth);
967 // merge all paragraphs to one
968 BufferParams const & bp = cur.bv().buffer().params();
969 while (inset->paragraphs().size() > 1)
970 mergeParagraph(bp, inset->paragraphs(), 0);
974 // undo information has already been recorded
975 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
976 bp.documentClass().emptyLayoutName());
983 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
984 Length const & width)
986 col_type const j = cellColumn(cell);
988 column_info[j].p_width = width;
989 for (row_type i = 0; i < rowCount(); ++i) {
990 idx_type const cell = cellIndex(i, j);
991 // because of multicolumns
992 toggleFixedWidth(cur, cellInset(cell).get(),
993 !getPWidth(cell).zero());
995 // cur paragraph can become invalid after paragraphs were merged
996 if (cur.pit() > cur.lastpit())
997 cur.pit() = cur.lastpit();
998 // cur position can become invalid after newlines were removed
999 if (cur.pos() > cur.lastpos())
1000 cur.pos() = cur.lastpos();
1004 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1005 Length const & width)
1007 if (!isMultiColumn(cell))
1010 cellInfo(cell).p_width = width;
1011 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1012 // cur paragraph can become invalid after paragraphs were merged
1013 if (cur.pit() > cur.lastpit())
1014 cur.pit() = cur.lastpit();
1015 // cur position can become invalid after newlines were removed
1016 if (cur.pos() > cur.lastpos())
1017 cur.pos() = cur.lastpos();
1022 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1023 Tabular::Feature what)
1025 if (what == SET_SPECIAL_MULTI)
1026 cellInfo(cell).align_special = special;
1028 column_info[cellColumn(cell)].align_special = special;
1032 void Tabular::setAllLines(idx_type cell, bool line)
1034 setTopLine(cell, line);
1035 setBottomLine(cell, line);
1036 setRightLine(cell, line);
1037 setLeftLine(cell, line);
1041 void Tabular::setTopLine(idx_type i, bool line)
1043 cellInfo(i).top_line = line;
1047 void Tabular::setBottomLine(idx_type i, bool line)
1049 cellInfo(i).bottom_line = line;
1053 void Tabular::setLeftLine(idx_type cell, bool line)
1055 cellInfo(cell).left_line = line;
1059 void Tabular::setRightLine(idx_type cell, bool line)
1061 cellInfo(cell).right_line = line;
1064 bool Tabular::rowTopLine(row_type r) const
1066 idx_type i0 = getFirstCellInRow(r);
1067 idx_type i1 = getLastCellInRow(r);
1068 bool all_rows_set = true;
1069 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1070 all_rows_set = cellInfo(j).top_line;
1071 return all_rows_set;
1075 bool Tabular::rowBottomLine(row_type r) const
1077 idx_type i0 = getFirstCellInRow(r);
1078 idx_type i1 = getLastCellInRow(r);
1079 bool all_rows_set = true;
1080 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1081 all_rows_set = cellInfo(j).bottom_line;
1082 return all_rows_set;
1086 bool Tabular::columnLeftLine(col_type c) const
1088 bool all_cols_set = true;
1089 row_type const nrows = rowCount();
1090 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1091 idx_type i = cellIndex(r, c);
1092 if (c == cellColumn(i))
1093 all_cols_set = cellInfo(i).left_line;
1095 return all_cols_set;
1099 bool Tabular::columnRightLine(col_type c) const
1101 bool all_cols_set = true;
1102 row_type const nrows = rowCount();
1103 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1104 idx_type i = cellIndex(r, c);
1105 if (c == cellColumn(i) + columnSpan(i) - 1)
1106 all_cols_set = cellInfo(i).right_line;
1108 return all_cols_set;
1112 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1114 if (!onlycolumn && isMultiColumn(cell))
1115 return cellInfo(cell).alignment;
1116 return column_info[cellColumn(cell)].alignment;
1121 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1123 if (!onlycolumn && isMultiColumn(cell))
1124 return cellInfo(cell).valignment;
1125 return column_info[cellColumn(cell)].valignment;
1129 Length const Tabular::getPWidth(idx_type cell) const
1131 if (isMultiColumn(cell))
1132 return cellInfo(cell).p_width;
1133 return column_info[cellColumn(cell)].p_width;
1137 Length const Tabular::getColumnPWidth(idx_type cell) const
1139 return column_info[cellColumn(cell)].p_width;
1143 Length const Tabular::getMColumnPWidth(idx_type cell) const
1145 if (isMultiColumn(cell))
1146 return cellInfo(cell).p_width;
1151 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1153 if (what == SET_SPECIAL_MULTI)
1154 return cellInfo(cell).align_special;
1155 return column_info[cellColumn(cell)].align_special;
1159 int Tabular::cellWidth(idx_type cell) const
1161 return cellInfo(cell).width;
1165 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1169 switch (getAlignment(cell)) {
1170 case LYX_ALIGN_CENTER:
1171 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1173 case LYX_ALIGN_RIGHT:
1174 x += columnWidth(cell) - cellWidth(cell);
1175 // + getAdditionalWidth(cell);
1178 // LYX_ALIGN_LEFT: nothing :-)
1182 // the LaTeX Way :-(
1188 bool Tabular::isFirstCellInRow(idx_type cell) const
1190 return cellColumn(cell) == 0;
1194 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1196 if (row > rowCount() - 1)
1197 row = rowCount() - 1;
1198 return cell_info[row][0].cellno;
1202 bool Tabular::isLastCellInRow(idx_type cell) const
1204 return cellRightColumn(cell) == columnCount() - 1;
1208 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1210 if (row > rowCount() - 1)
1211 row = rowCount() - 1;
1212 return cell_info[row][columnCount() - 1].cellno;
1216 Tabular::row_type Tabular::cellRow(idx_type cell) const
1218 if (cell >= cellCount())
1219 return rowCount() - 1;
1222 return rowofcell[cell];
1226 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1228 if (cell >= cellCount())
1229 return columnCount() - 1;
1232 return columnofcell[cell];
1236 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1238 row_type const row = cellRow(cell);
1239 col_type column = cellColumn(cell);
1240 while (column < columnCount() - 1 &&
1241 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1247 void Tabular::write(ostream & os) const
1251 << write_attribute("version", 3)
1252 << write_attribute("rows", rowCount())
1253 << write_attribute("columns", columnCount())
1255 // global longtable options
1257 << write_attribute("rotate", rotate)
1258 << write_attribute("booktabs", use_booktabs)
1259 << write_attribute("islongtable", is_long_tabular)
1260 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1261 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1262 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1263 << write_attribute("headTopDL", endhead.topDL)
1264 << write_attribute("headBottomDL", endhead.bottomDL)
1265 << write_attribute("footTopDL", endfoot.topDL)
1266 << write_attribute("footBottomDL", endfoot.bottomDL)
1267 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1268 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1269 << write_attribute("lastFootEmpty", endlastfoot.empty)
1271 for (col_type j = 0; j < columnCount(); ++j) {
1273 << write_attribute("alignment", column_info[j].alignment)
1274 << write_attribute("valignment", column_info[j].valignment)
1275 << write_attribute("width", column_info[j].p_width.asString())
1276 << write_attribute("special", column_info[j].align_special)
1279 for (row_type i = 0; i < rowCount(); ++i) {
1280 static const string def("default");
1282 if (row_info[i].top_space_default)
1283 os << write_attribute("topspace", def);
1285 os << write_attribute("topspace", row_info[i].top_space);
1286 if (row_info[i].bottom_space_default)
1287 os << write_attribute("bottomspace", def);
1289 os << write_attribute("bottomspace", row_info[i].bottom_space);
1290 if (row_info[i].interline_space_default)
1291 os << write_attribute("interlinespace", def);
1293 os << write_attribute("interlinespace", row_info[i].interline_space);
1294 os << write_attribute("endhead", row_info[i].endhead)
1295 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1296 << write_attribute("endfoot", row_info[i].endfoot)
1297 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1298 << write_attribute("newpage", row_info[i].newpage)
1300 for (col_type j = 0; j < columnCount(); ++j) {
1302 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1303 << write_attribute("alignment", cell_info[i][j].alignment)
1304 << write_attribute("valignment", cell_info[i][j].valignment)
1305 << write_attribute("topline", cell_info[i][j].top_line)
1306 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1307 << write_attribute("leftline", cell_info[i][j].left_line)
1308 << write_attribute("rightline", cell_info[i][j].right_line)
1309 << write_attribute("rotate", cell_info[i][j].rotate)
1310 << write_attribute("usebox", cell_info[i][j].usebox)
1311 << write_attribute("width", cell_info[i][j].p_width)
1312 << write_attribute("special", cell_info[i][j].align_special)
1314 os << "\\begin_inset ";
1315 cell_info[i][j].inset->write(os);
1316 os << "\n\\end_inset\n"
1321 os << "</lyxtabular>\n";
1325 void Tabular::read(Lexer & lex)
1328 istream & is = lex.getStream();
1330 l_getline(is, line);
1331 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1332 BOOST_ASSERT(false);
1337 if (!getTokenValue(line, "version", version))
1339 BOOST_ASSERT(version >= 2);
1342 if (!getTokenValue(line, "rows", rows_arg))
1345 if (!getTokenValue(line, "columns", columns_arg))
1347 init(buffer(), rows_arg, columns_arg);
1348 l_getline(is, line);
1349 if (!prefixIs(line, "<features")) {
1350 lyxerr << "Wrong tabular format (expected <features ...> got"
1351 << line << ')' << endl;
1354 getTokenValue(line, "rotate", rotate);
1355 getTokenValue(line, "booktabs", use_booktabs);
1356 getTokenValue(line, "islongtable", is_long_tabular);
1357 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1358 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1359 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1360 getTokenValue(line, "headTopDL", endhead.topDL);
1361 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1362 getTokenValue(line, "footTopDL", endfoot.topDL);
1363 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1364 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1365 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1366 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1368 for (col_type j = 0; j < columnCount(); ++j) {
1370 if (!prefixIs(line,"<column")) {
1371 lyxerr << "Wrong tabular format (expected <column ...> got"
1372 << line << ')' << endl;
1375 getTokenValue(line, "alignment", column_info[j].alignment);
1376 getTokenValue(line, "valignment", column_info[j].valignment);
1377 getTokenValue(line, "width", column_info[j].p_width);
1378 getTokenValue(line, "special", column_info[j].align_special);
1381 for (row_type i = 0; i < rowCount(); ++i) {
1382 l_getline(is, line);
1383 if (!prefixIs(line, "<row")) {
1384 lyxerr << "Wrong tabular format (expected <row ...> got"
1385 << line << ')' << endl;
1388 getTokenValue(line, "topspace", row_info[i].top_space,
1389 row_info[i].top_space_default);
1390 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1391 row_info[i].bottom_space_default);
1392 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1393 row_info[i].interline_space_default);
1394 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1395 getTokenValue(line, "endhead", row_info[i].endhead);
1396 getTokenValue(line, "endfoot", row_info[i].endfoot);
1397 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1398 getTokenValue(line, "newpage", row_info[i].newpage);
1399 for (col_type j = 0; j < columnCount(); ++j) {
1400 l_getline(is, line);
1401 if (!prefixIs(line, "<cell")) {
1402 lyxerr << "Wrong tabular format (expected <cell ...> got"
1403 << line << ')' << endl;
1406 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1407 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1408 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1409 getTokenValue(line, "topline", cell_info[i][j].top_line);
1410 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1411 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1412 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1413 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1414 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1415 getTokenValue(line, "width", cell_info[i][j].p_width);
1416 getTokenValue(line, "special", cell_info[i][j].align_special);
1417 l_getline(is, line);
1418 if (prefixIs(line, "\\begin_inset")) {
1419 cell_info[i][j].inset->read(lex);
1420 l_getline(is, line);
1422 if (!prefixIs(line, "</cell>")) {
1423 lyxerr << "Wrong tabular format (expected </cell> got"
1424 << line << ')' << endl;
1428 l_getline(is, line);
1429 if (!prefixIs(line, "</row>")) {
1430 lyxerr << "Wrong tabular format (expected </row> got"
1431 << line << ')' << endl;
1435 while (!prefixIs(line, "</lyxtabular>")) {
1436 l_getline(is, line);
1442 bool Tabular::isMultiColumn(idx_type cell) const
1444 return cellInfo(cell).multicolumn != CELL_NORMAL;
1448 bool Tabular::isMultiColumnReal(idx_type cell) const
1450 return cellColumn(cell) != cellRightColumn(cell) &&
1451 cellInfo(cell).multicolumn != CELL_NORMAL;
1455 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1457 return cell_info[cellRow(cell)][cellColumn(cell)];
1461 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1463 CellData & cs = cellInfo(cell);
1464 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1465 cs.alignment = column_info[cellColumn(cell)].alignment;
1466 setRightLine(cell, rightLine(cell + number - 1));
1467 for (idx_type i = 1; i < number; ++i) {
1468 CellData & cs1 = cellInfo(cell + i);
1469 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1470 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1477 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1479 row_type const row = cellRow(cell);
1480 col_type const ncols = columnCount();
1481 idx_type result = 1;
1482 col_type column = cellColumn(cell) + 1;
1483 while (column < ncols && isPartOfMultiColumn(row, column)) {
1491 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1493 row_type const row = cellRow(cell);
1494 col_type column = cellColumn(cell);
1496 idx_type result = 0;
1498 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1499 cell_info[row][column].multicolumn = CELL_NORMAL;
1501 while (column < columnCount() &&
1502 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1504 cell_info[row][column].multicolumn = CELL_NORMAL;
1514 void Tabular::setBookTabs(bool what)
1516 use_booktabs = what;
1520 bool Tabular::useBookTabs() const
1522 return use_booktabs;
1526 void Tabular::setLongTabular(bool what)
1528 is_long_tabular = what;
1532 bool Tabular::isLongTabular() const
1534 return is_long_tabular;
1538 void Tabular::setRotateTabular(bool flag)
1544 bool Tabular::getRotateTabular() const
1550 void Tabular::setRotateCell(idx_type cell, bool flag)
1552 cellInfo(cell).rotate = flag;
1556 bool Tabular::getRotateCell(idx_type cell) const
1558 return cellInfo(cell).rotate;
1562 bool Tabular::needRotating() const
1566 for (row_type i = 0; i < rowCount(); ++i)
1567 for (col_type j = 0; j < columnCount(); ++j)
1568 if (cell_info[i][j].rotate)
1574 bool Tabular::isLastCell(idx_type cell) const
1576 if (cell + 1 < cellCount())
1582 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1584 if (cellRow(cell) > 0)
1585 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1590 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1592 if (cellRow(cell) + 1 < rowCount())
1593 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1598 Tabular::idx_type Tabular::cellIndex(row_type row,
1599 col_type column) const
1601 BOOST_ASSERT(column != npos && column < columnCount() &&
1602 row != npos && row < rowCount());
1603 return cell_info[row][column].cellno;
1607 void Tabular::setUsebox(idx_type cell, BoxType type)
1609 cellInfo(cell).usebox = type;
1613 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1615 if (column_info[cellColumn(cell)].p_width.zero() &&
1616 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1618 if (cellInfo(cell).usebox > 1)
1619 return cellInfo(cell).usebox;
1620 return useParbox(cell);
1625 // This are functions used for the longtable support
1627 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1633 row_info[row].endfirsthead = flag;
1637 row_info[row].endhead = flag;
1642 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1645 hd.set = haveLTHead();
1646 return row_info[row].endhead;
1650 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1653 hd.set = haveLTFirstHead();
1654 return row_info[row].endfirsthead;
1658 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1664 row_info[row].endlastfoot = flag;
1668 row_info[row].endfoot = flag;
1673 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1676 fd.set = haveLTFoot();
1677 return row_info[row].endfoot;
1681 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1684 fd.set = haveLTLastFoot();
1685 return row_info[row].endlastfoot;
1689 void Tabular::setLTNewPage(row_type row, bool what)
1691 row_info[row].newpage = what;
1695 bool Tabular::getLTNewPage(row_type row) const
1697 return row_info[row].newpage;
1701 bool Tabular::haveLTHead() const
1703 for (row_type i = 0; i < rowCount(); ++i)
1704 if (row_info[i].endhead)
1710 bool Tabular::haveLTFirstHead() const
1712 if (endfirsthead.empty)
1714 for (row_type i = 0; i < rowCount(); ++i)
1715 if (row_info[i].endfirsthead)
1721 bool Tabular::haveLTFoot() const
1723 for (row_type i = 0; i < rowCount(); ++i)
1724 if (row_info[i].endfoot)
1730 bool Tabular::haveLTLastFoot() const
1732 if (endlastfoot.empty)
1734 for (row_type i = 0; i < rowCount(); ++i)
1735 if (row_info[i].endlastfoot)
1741 // end longtable support functions
1743 void Tabular::setRowAscent(row_type row, int height)
1745 if (row >= rowCount() || row_info[row].ascent == height)
1747 row_info[row].ascent = height;
1751 void Tabular::setRowDescent(row_type row, int height)
1753 if (row >= rowCount() || row_info[row].descent == height)
1755 row_info[row].descent = height;
1759 int Tabular::rowAscent(row_type row) const
1761 if (row >= rowCount())
1763 return row_info[row].ascent;
1767 int Tabular::rowDescent(row_type row) const
1769 BOOST_ASSERT(row < rowCount());
1770 return row_info[row].descent;
1774 int Tabular::height() const
1777 for (row_type row = 0; row < rowCount(); ++row)
1778 height += rowAscent(row) + rowDescent(row) +
1779 getAdditionalHeight(row);
1784 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1786 BOOST_ASSERT(row < rowCount());
1787 BOOST_ASSERT(column < columnCount());
1788 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1792 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1794 // FIXME: assert or return 0 as in TeXBottomHLine()?
1795 BOOST_ASSERT(row != npos);
1796 BOOST_ASSERT(row < rowCount());
1798 idx_type const fcell = getFirstCellInRow(row);
1799 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1802 for (idx_type i = fcell; i < n; ++i) {
1806 if (use_booktabs && row == 0) {
1809 } else if (tmp == n - fcell) {
1810 os << (use_booktabs ? "\\midrule " : "\\hline ");
1812 for (idx_type i = fcell; i < n; ++i) {
1814 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1815 << cellColumn(i) + 1
1817 << cellRightColumn(i) + 1
1829 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1831 // FIXME: return 0 or assert as in TeXTopHLine()?
1832 if (row == npos || row >= rowCount())
1835 idx_type const fcell = getFirstCellInRow(row);
1836 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1839 for (idx_type i = fcell; i < n; ++i) {
1843 if (use_booktabs && row == rowCount() - 1) {
1844 if (bottomLine(fcell))
1845 os << "\\bottomrule";
1846 } else if (tmp == n - fcell) {
1847 os << (use_booktabs ? "\\midrule" : "\\hline");
1849 for (idx_type i = fcell; i < n; ++i) {
1850 if (bottomLine(i)) {
1851 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1852 << cellColumn(i) + 1
1854 << cellRightColumn(i) + 1
1866 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1870 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1871 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1872 // figure out how to set the lines
1873 // we always set double lines to the right of the cell
1874 row_type const r = cellRow(cell);
1875 col_type const c = cellColumn(cell);
1876 col_type const nextcol = c + columnSpan(cell);
1877 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1878 bool forceleft = ismulticol && !prevcellright && leftLine(cell);
1879 bool nextcolleft = nextcol < columnCount() && columnLeftLine(nextcol);
1880 bool coldouble = columnRightLine(c) && nextcolleft;
1881 bool nextcellleft = nextcol < columnCount()
1882 && leftLine(cellIndex(r, nextcol));
1883 bool celldouble = rightLine(cell) && nextcellleft;
1884 bool doubleright = celldouble && (isMultiColumn(cell) || !coldouble);
1885 ismulticol = isMultiColumn(cell)
1886 || (leftLine(cell) && !columnLeftLine(c))
1887 || (rightLine(cell) && !columnRightLine(c))
1888 || (!celldouble && coldouble) || doubleright || forceleft;
1890 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1891 if (leftLine(cell) || forceleft)
1893 if (!cellInfo(cell).align_special.empty()) {
1894 os << cellInfo(cell).align_special;
1896 if (!getPWidth(cell).zero()) {
1898 case LYX_VALIGN_TOP:
1901 case LYX_VALIGN_MIDDLE:
1904 case LYX_VALIGN_BOTTOM:
1909 << from_ascii(getPWidth(cell).asLatexString())
1913 case LYX_ALIGN_LEFT:
1916 case LYX_ALIGN_RIGHT:
1923 } // end if else !getPWidth
1924 } // end if else !cellinfo_of_cell
1925 if (rightLine(cell))
1928 // add extra vertical line if we want a double one
1932 if (getRotateCell(cell)) {
1933 os << "\\begin{sideways}\n";
1936 if (getUsebox(cell) == BOX_PARBOX) {
1939 case LYX_VALIGN_TOP:
1942 case LYX_VALIGN_MIDDLE:
1945 case LYX_VALIGN_BOTTOM:
1949 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1951 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1952 os << "\\begin{minipage}[";
1954 case LYX_VALIGN_TOP:
1957 case LYX_VALIGN_MIDDLE:
1960 case LYX_VALIGN_BOTTOM:
1964 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1972 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1977 if (getUsebox(cell) == BOX_PARBOX)
1979 else if (getUsebox(cell) == BOX_MINIPAGE) {
1980 os << "%\n\\end{minipage}";
1983 if (getRotateCell(cell)) {
1984 os << "%\n\\end{sideways}";
1994 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1995 OutputParams const & runparams) const
1997 if (!is_long_tabular)
2001 // output header info
2003 if (endhead.topDL) {
2007 for (row_type i = 0; i < rowCount(); ++i) {
2008 if (row_info[i].endhead) {
2009 ret += TeXRow(os, i, runparams);
2012 if (endhead.bottomDL) {
2016 os << "\\endhead\n";
2018 if (endfirsthead.empty) {
2019 os << "\\endfirsthead\n";
2023 // output firstheader info
2024 if (haveLTFirstHead()) {
2025 if (endfirsthead.topDL) {
2029 for (row_type i = 0; i < rowCount(); ++i) {
2030 if (row_info[i].endfirsthead) {
2031 ret += TeXRow(os, i, runparams);
2034 if (endfirsthead.bottomDL) {
2038 os << "\\endfirsthead\n";
2041 // output footer info
2043 if (endfoot.topDL) {
2047 for (row_type i = 0; i < rowCount(); ++i) {
2048 if (row_info[i].endfoot) {
2049 ret += TeXRow(os, i, runparams);
2052 if (endfoot.bottomDL) {
2056 os << "\\endfoot\n";
2058 if (endlastfoot.empty) {
2059 os << "\\endlastfoot\n";
2063 // output lastfooter info
2064 if (haveLTLastFoot()) {
2065 if (endlastfoot.topDL) {
2069 for (row_type i = 0; i < rowCount(); ++i) {
2070 if (row_info[i].endlastfoot) {
2071 ret += TeXRow(os, i, runparams);
2074 if (endlastfoot.bottomDL) {
2078 os << "\\endlastfoot\n";
2085 bool Tabular::isValidRow(row_type row) const
2087 if (!is_long_tabular)
2089 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2090 !row_info[row].endfoot && !row_info[row].endlastfoot;
2094 int Tabular::TeXRow(odocstream & os, row_type i,
2095 OutputParams const & runparams) const
2097 idx_type cell = cellIndex(i, 0);
2098 int ret = TeXTopHLine(os, i);
2099 if (row_info[i].top_space_default) {
2101 os << "\\addlinespace\n";
2103 os << "\\noalign{\\vskip\\doublerulesep}\n";
2105 } else if(!row_info[i].top_space.zero()) {
2107 os << "\\addlinespace["
2108 << from_ascii(row_info[i].top_space.asLatexString())
2111 os << "\\noalign{\\vskip"
2112 << from_ascii(row_info[i].top_space.asLatexString())
2117 bool ismulticol = false;
2118 for (col_type j = 0; j < columnCount(); ++j) {
2119 if (isPartOfMultiColumn(i, j))
2121 ret += TeXCellPreamble(os, cell, ismulticol);
2122 shared_ptr<InsetTableCell> inset = cellInset(cell);
2124 Paragraph const & par = inset->paragraphs().front();
2125 bool rtl = par.isRTL(buffer().params())
2127 && getPWidth(cell).zero();
2130 if (par.getParLanguage(buffer().params())->lang() ==
2133 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2135 // currently, remaning RTL languages are arabic_arabtex and hebrew
2139 ret += inset->latex(os, runparams);
2143 ret += TeXCellPostamble(os, cell, ismulticol);
2144 if (!isLastCellInRow(cell)) { // not last cell in row
2149 os << "\\tabularnewline";
2150 if (row_info[i].bottom_space_default) {
2152 os << "\\addlinespace";
2154 os << "[\\doublerulesep]";
2155 } else if (!row_info[i].bottom_space.zero()) {
2157 os << "\\addlinespace";
2159 << from_ascii(row_info[i].bottom_space.asLatexString())
2164 ret += TeXBottomHLine(os, i);
2165 if (row_info[i].interline_space_default) {
2167 os << "\\addlinespace\n";
2169 os << "\\noalign{\\vskip\\doublerulesep}\n";
2171 } else if (!row_info[i].interline_space.zero()) {
2173 os << "\\addlinespace["
2174 << from_ascii(row_info[i].interline_space.asLatexString())
2177 os << "\\noalign{\\vskip"
2178 << from_ascii(row_info[i].interline_space.asLatexString())
2186 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2190 //+---------------------------------------------------------------------
2191 //+ first the opening preamble +
2192 //+---------------------------------------------------------------------
2195 os << "\\begin{sideways}\n";
2198 if (is_long_tabular)
2199 os << "\\begin{longtable}{";
2201 os << "\\begin{tabular}{";
2203 for (col_type i = 0; i < columnCount(); ++i) {
2204 if (!use_booktabs && columnLeftLine(i))
2206 if (!column_info[i].align_special.empty()) {
2207 os << column_info[i].align_special;
2209 if (!column_info[i].p_width.zero()) {
2210 switch (column_info[i].alignment) {
2211 case LYX_ALIGN_LEFT:
2212 os << ">{\\raggedright}";
2214 case LYX_ALIGN_RIGHT:
2215 os << ">{\\raggedleft}";
2217 case LYX_ALIGN_CENTER:
2218 os << ">{\\centering}";
2220 case LYX_ALIGN_NONE:
2221 case LYX_ALIGN_BLOCK:
2222 case LYX_ALIGN_LAYOUT:
2223 case LYX_ALIGN_SPECIAL:
2227 switch (column_info[i].valignment) {
2228 case LYX_VALIGN_TOP:
2231 case LYX_VALIGN_MIDDLE:
2234 case LYX_VALIGN_BOTTOM:
2239 << from_ascii(column_info[i].p_width.asLatexString())
2242 switch (column_info[i].alignment) {
2243 case LYX_ALIGN_LEFT:
2246 case LYX_ALIGN_RIGHT:
2253 } // end if else !column_info[i].p_width
2254 } // end if else !column_info[i].align_special
2255 if (!use_booktabs && columnRightLine(i))
2261 ret += TeXLongtableHeaderFooter(os, runparams);
2263 //+---------------------------------------------------------------------
2264 //+ the single row and columns (cells) +
2265 //+---------------------------------------------------------------------
2267 for (row_type i = 0; i < rowCount(); ++i) {
2268 if (isValidRow(i)) {
2269 ret += TeXRow(os, i, runparams);
2270 if (is_long_tabular && row_info[i].newpage) {
2271 os << "\\newpage\n";
2277 //+---------------------------------------------------------------------
2278 //+ the closing of the tabular +
2279 //+---------------------------------------------------------------------
2281 if (is_long_tabular)
2282 os << "\\end{longtable}";
2284 os << "\\end{tabular}";
2286 os << "\n\\end{sideways}";
2294 int Tabular::docbookRow(odocstream & os, row_type row,
2295 OutputParams const & runparams) const
2298 idx_type cell = getFirstCellInRow(row);
2301 for (col_type j = 0; j < columnCount(); ++j) {
2302 if (isPartOfMultiColumn(row, j))
2305 os << "<entry align=\"";
2306 switch (getAlignment(cell)) {
2307 case LYX_ALIGN_LEFT:
2310 case LYX_ALIGN_RIGHT:
2318 os << "\" valign=\"";
2319 switch (getVAlignment(cell)) {
2320 case LYX_VALIGN_TOP:
2323 case LYX_VALIGN_BOTTOM:
2326 case LYX_VALIGN_MIDDLE:
2331 if (isMultiColumn(cell)) {
2332 os << " namest=\"col" << j << "\" ";
2333 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2337 ret += cellInset(cell)->docbook(os, runparams);
2346 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2350 //+---------------------------------------------------------------------
2351 //+ first the opening preamble +
2352 //+---------------------------------------------------------------------
2354 os << "<tgroup cols=\"" << columnCount()
2355 << "\" colsep=\"1\" rowsep=\"1\">\n";
2357 for (col_type i = 0; i < columnCount(); ++i) {
2358 os << "<colspec colname=\"col" << i << "\" align=\"";
2359 switch (column_info[i].alignment) {
2360 case LYX_ALIGN_LEFT:
2363 case LYX_ALIGN_RIGHT:
2371 if (runparams.flavor == OutputParams::XML)
2377 //+---------------------------------------------------------------------
2378 //+ Long Tabular case +
2379 //+---------------------------------------------------------------------
2381 // output header info
2382 if (haveLTHead() || haveLTFirstHead()) {
2385 for (row_type i = 0; i < rowCount(); ++i) {
2386 if (row_info[i].endhead || row_info[i].endfirsthead) {
2387 ret += docbookRow(os, i, runparams);
2393 // output footer info
2394 if (haveLTFoot() || haveLTLastFoot()) {
2397 for (row_type i = 0; i < rowCount(); ++i) {
2398 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2399 ret += docbookRow(os, i, runparams);
2406 //+---------------------------------------------------------------------
2407 //+ the single row and columns (cells) +
2408 //+---------------------------------------------------------------------
2412 for (row_type i = 0; i < rowCount(); ++i) {
2413 if (isValidRow(i)) {
2414 ret += docbookRow(os, i, runparams);
2419 //+---------------------------------------------------------------------
2420 //+ the closing of the tabular +
2421 //+---------------------------------------------------------------------
2430 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2431 vector<unsigned int> const & clen) const
2433 idx_type const fcell = getFirstCellInRow(row);
2434 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2437 for (idx_type i = fcell; i < n; ++i) {
2447 for (idx_type i = fcell; i < n; ++i) {
2458 col_type column = cellColumn(i);
2459 int len = clen[column];
2460 while (column < columnCount() - 1
2461 && isPartOfMultiColumn(row, ++column))
2462 len += clen[column] + 4;
2463 os << docstring(len, ch);
2478 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2479 vector<unsigned int> const & clen) const
2481 idx_type const fcell = getFirstCellInRow(row);
2482 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2485 for (idx_type i = fcell; i < n; ++i) {
2486 if (bottomLine(i)) {
2495 for (idx_type i = fcell; i < n; ++i) {
2496 if (bottomLine(i)) {
2506 col_type column = cellColumn(i);
2507 int len = clen[column];
2508 while (column < columnCount() -1
2509 && isPartOfMultiColumn(row, ++column))
2510 len += clen[column] + 4;
2511 os << docstring(len, ch);
2512 if (bottomLine(i)) {
2526 void Tabular::plaintextPrintCell(odocstream & os,
2527 OutputParams const & runparams,
2528 idx_type cell, row_type row, col_type column,
2529 vector<unsigned int> const & clen,
2530 bool onlydata) const
2532 odocstringstream sstr;
2533 cellInset(cell)->plaintext(sstr, runparams);
2545 unsigned int len1 = sstr.str().length();
2546 unsigned int len2 = clen[column];
2547 while (column < columnCount() -1
2548 && isPartOfMultiColumn(row, ++column))
2549 len2 += clen[column] + 4;
2552 switch (getAlignment(cell)) {
2554 case LYX_ALIGN_LEFT:
2557 case LYX_ALIGN_RIGHT:
2561 case LYX_ALIGN_CENTER:
2567 os << docstring(len1, ' ') << sstr.str()
2568 << docstring(len2, ' ');
2570 if (rightLine(cell))
2577 void Tabular::plaintext(odocstream & os,
2578 OutputParams const & runparams, int const depth,
2579 bool onlydata, char_type delim) const
2581 // first calculate the width of the single columns
2582 vector<unsigned int> clen(columnCount());
2585 // first all non (real) multicolumn cells!
2586 for (col_type j = 0; j < columnCount(); ++j) {
2588 for (row_type i = 0; i < rowCount(); ++i) {
2589 idx_type cell = cellIndex(i, j);
2590 if (isMultiColumnReal(cell))
2592 odocstringstream sstr;
2593 cellInset(cell)->plaintext(sstr, runparams);
2594 if (clen[j] < sstr.str().length())
2595 clen[j] = sstr.str().length();
2598 // then all (real) multicolumn cells!
2599 for (col_type j = 0; j < columnCount(); ++j) {
2600 for (row_type i = 0; i < rowCount(); ++i) {
2601 idx_type cell = cellIndex(i, j);
2602 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2604 odocstringstream sstr;
2605 cellInset(cell)->plaintext(sstr, runparams);
2606 int len = int(sstr.str().length());
2607 idx_type const n = columnSpan(cell);
2608 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2610 if (len > int(clen[j + n - 1]))
2611 clen[j + n - 1] = len;
2616 for (row_type i = 0; i < rowCount(); ++i) {
2617 if (!onlydata && plaintextTopHLine(os, i, clen))
2618 os << docstring(depth * 2, ' ');
2619 for (col_type j = 0; j < columnCount(); ++j) {
2620 if (isPartOfMultiColumn(i, j))
2622 if (onlydata && j > 0)
2623 // we don't use operator<< for single UCS4 character.
2624 // see explanation in docstream.h
2626 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2631 os << docstring(depth * 2, ' ');
2632 if (plaintextBottomHLine(os, i, clen))
2633 os << docstring(depth * 2, ' ');
2639 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2641 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2645 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2646 col_type column) const
2648 return cell_info[row][column].inset;
2652 void Tabular::setCellInset(row_type row, col_type column,
2653 shared_ptr<InsetTableCell> ins) const
2655 CellData & cd = cell_info[row][column];
2657 // reset the InsetTableCell's pointers
2658 ins->setCellData(&cd);
2659 ins->setTabular(this);
2664 Tabular::cellFromInset(Inset const * inset) const
2666 // is this inset part of the tabular?
2668 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2669 BOOST_ASSERT(false);
2672 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2673 if (cellInset(cell).get() == inset) {
2674 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2675 << "cell=" << cell);
2679 // We should have found a cell at this point
2680 lyxerr << "Tabular::cellFromInset: Cell of inset "
2681 << inset << " not found!" << endl;
2682 BOOST_ASSERT(false);
2688 void Tabular::validate(LaTeXFeatures & features) const
2690 features.require("NeedTabularnewline");
2692 features.require("booktabs");
2693 if (isLongTabular())
2694 features.require("longtable");
2696 features.require("rotating");
2697 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2698 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2699 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2700 features.require("array");
2701 cellInset(cell)->validate(features);
2706 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2708 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2709 ParagraphList::const_iterator cit = parlist.begin();
2710 ParagraphList::const_iterator end = parlist.end();
2712 for (; cit != end; ++cit)
2713 for (int i = 0; i < cit->size(); ++i)
2714 if (cit->isNewline(i))
2721 /////////////////////////////////////////////////////////////////////
2725 /////////////////////////////////////////////////////////////////////
2727 InsetTableCell::InsetTableCell(Buffer const & buf,
2728 Tabular::CellData const * cell, Tabular const * table)
2729 : InsetText(buf), cell_data_(cell), table_(table)
2733 bool InsetTableCell::forceEmptyLayout(idx_type) const
2735 BOOST_ASSERT(table_);
2736 BOOST_ASSERT(cell_data_);
2737 return table_->getPWidth(cell_data_->cellno).zero();
2740 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2742 BOOST_ASSERT(table_);
2743 BOOST_ASSERT(cell_data_);
2744 return !table_->getPWidth(cell_data_->cellno).zero();
2747 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2748 FuncStatus & status) const
2751 switch (cmd.action) {
2753 enabled = !forceEmptyLayout();
2755 case LFUN_LAYOUT_PARAGRAPH:
2756 enabled = allowParagraphCustomization();
2759 return InsetText::getStatus(cur, cmd, status);
2761 status.enabled(enabled);
2765 /////////////////////////////////////////////////////////////////////
2769 /////////////////////////////////////////////////////////////////////
2771 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2773 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2774 rowselect_(false), colselect_(false)
2776 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2780 InsetTabular::InsetTabular(InsetTabular const & tab)
2781 : Inset(tab), tabular(tab.tabular), scx_(0)
2783 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2787 InsetTabular::~InsetTabular()
2789 hideDialogs("tabular", this);
2793 bool InsetTabular::insetAllowed(InsetCode code) const
2795 if (code == MATHMACRO_CODE)
2802 void InsetTabular::write(ostream & os) const
2804 os << "Tabular" << endl;
2809 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2811 // FIXME: depending on the selection state, we could offer a different menu.
2812 return from_ascii("context-tabular");
2816 void InsetTabular::read(Lexer & lex)
2818 //bool const old_format = (lex.getString() == "\\LyXTable");
2826 string token = lex.getString();
2827 while (lex && token != "\\end_inset") {
2829 token = lex.getString();
2832 lex.printError("Missing \\end_inset at this point. "
2838 int InsetTabular::rowFromY(Cursor & cur, int y) const
2840 // top y coordinate of tabular
2841 int h = yo(cur.bv()) - tabular.rowAscent(0);
2842 size_t nrows = tabular.rowCount();
2844 for (;r < nrows && y > h; ++r) {
2845 h += tabular.rowAscent(r);
2846 h += tabular.rowDescent(r);
2847 h += tabular.getAdditionalHeight(r);
2853 int InsetTabular::columnFromX(Cursor & cur, int x) const
2855 // left x coordinate of tabular
2856 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2857 size_t ncols = tabular.columnCount();
2859 for (;c < ncols && x > w; ++c) {
2860 w += tabular.columnWidth(c);
2866 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2868 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2869 // mi.base.textwidth << "\n";
2871 lyxerr << "InsetTabular::metrics: need bv" << endl;
2872 BOOST_ASSERT(false);
2876 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2879 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2880 if (tabular.isPartOfMultiColumn(i, j))
2881 // Multicolumn cell, but not first one
2886 if (tabular.cell_info[i][j].multicolumn ==
2887 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2888 p_width = tabular.cellInfo(cell).p_width;
2890 p_width = tabular.column_info[j].p_width;
2891 if (!p_width.zero())
2892 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2893 tabular.cellInset(cell)->metrics(m, dim);
2894 if (!p_width.zero())
2895 dim.wid = m.base.textwidth;
2896 tabular.setCellWidth(cell, dim.wid);
2897 maxAsc = max(maxAsc, dim.asc);
2898 maxDesc = max(maxDesc, dim.des);
2901 int const top_space = tabular.row_info[i].top_space_default ?
2902 default_line_space :
2903 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2904 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2905 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2906 default_line_space :
2907 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2908 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2910 tabular.updateColumnWidths();
2911 dim.asc = tabular.rowAscent(0);
2912 dim.des = tabular.height() - dim.asc;
2913 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2917 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2919 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2920 BufferView * bv = pi.base.bv;
2922 // FIXME: As the full backrgound is painted in drawSelection(),
2923 // we have no choice but to do a full repaint for the Text cells.
2924 pi.full_repaint = true;
2926 resetPos(bv->cursor());
2929 x += ADD_TO_TABULAR_WIDTH;
2931 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2934 first_visible_cell = Tabular::npos;
2935 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2937 int const a = tabular.rowAscent(i);
2938 int const d = tabular.rowDescent(i);
2939 idx = tabular.cellIndex(i, 0);
2940 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2941 if (tabular.isPartOfMultiColumn(i, j))
2943 if (first_visible_cell == Tabular::npos)
2944 first_visible_cell = idx;
2946 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2947 // Cache the Inset position.
2948 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2949 if (nx + tabular.columnWidth(idx) < 0
2950 || nx > bv->workWidth()
2952 || y - a > bv->workHeight()) {
2953 pi.pain.setDrawingEnabled(false);
2954 cell(idx)->draw(pi, cx, y);
2955 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2956 pi.pain.setDrawingEnabled(original_drawing_state);
2958 cell(idx)->draw(pi, cx, y);
2959 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2961 nx += tabular.columnWidth(idx);
2965 if (i + 1 < tabular.rowCount())
2966 y += d + tabular.rowAscent(i + 1) +
2967 tabular.getAdditionalHeight(i + 1);
2972 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2974 Cursor & cur = pi.base.bv->cursor();
2976 x += scx_ + ADD_TO_TABULAR_WIDTH;
2978 // FIXME: it is wrong to completely paint the background
2979 // if we want to do single row painting.
2981 // Paint background of current tabular
2982 int const w = tabular.width();
2983 int const h = tabular.height();
2984 int yy = y - tabular.rowAscent(0);
2985 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2987 if (!cur.selection())
2989 if (&cur.inset() != this)
2995 if (tablemode(cur)) {
2998 getSelection(cur, rs, re, cs, ce);
2999 y -= tabular.rowAscent(0);
3000 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3001 int const a = tabular.rowAscent(j);
3002 int const h = a + tabular.rowDescent(j);
3004 y += tabular.getAdditionalHeight(j);
3005 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3006 if (tabular.isPartOfMultiColumn(j, i))
3008 idx_type const cell =
3009 tabular.cellIndex(j, i);
3010 int const w = tabular.columnWidth(cell);
3011 if (i >= cs && i <= ce && j >= rs && j <= re)
3012 pi.pain.fillRectangle(xx, y, w, h,
3020 x += cellXPos(cur.idx());
3021 x += tabular.getBeginningOfTextInCell(cur.idx());
3022 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3027 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3028 row_type row, idx_type cell, bool erased) const
3030 int x2 = x + tabular.columnWidth(cell);
3031 bool on_off = false;
3032 ColorCode col = Color_tabularline;
3033 ColorCode onoffcol = Color_tabularonoffline;
3036 col = Color_deletedtext;
3037 onoffcol = Color_deletedtext;
3040 if (!tabular.topAlreadyDrawn(cell)) {
3041 on_off = !tabular.topLine(cell);
3042 pain.line(x, y - tabular.rowAscent(row),
3043 x2, y - tabular.rowAscent(row),
3044 on_off ? onoffcol : col,
3045 on_off ? Painter::line_onoffdash : Painter::line_solid);
3047 on_off = !tabular.bottomLine(cell);
3048 pain.line(x, y + tabular.rowDescent(row),
3049 x2, y + tabular.rowDescent(row),
3050 on_off ? onoffcol : col,
3051 on_off ? Painter::line_onoffdash : Painter::line_solid);
3052 if (!tabular.leftAlreadyDrawn(cell)) {
3053 on_off = !tabular.leftLine(cell);
3054 pain.line(x, y - tabular.rowAscent(row),
3055 x, y + tabular.rowDescent(row),
3056 on_off ? onoffcol : col,
3057 on_off ? Painter::line_onoffdash : Painter::line_solid);
3059 on_off = !tabular.rightLine(cell);
3060 pain.line(x2 - tabular.getAdditionalWidth(cell),
3061 y - tabular.rowAscent(row),
3062 x2 - tabular.getAdditionalWidth(cell),
3063 y + tabular.rowDescent(row),
3064 on_off ? onoffcol : col,
3065 on_off ? Painter::line_onoffdash : Painter::line_solid);
3069 docstring InsetTabular::editMessage() const
3071 return _("Opened table");
3075 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3077 //lyxerr << "InsetTabular::edit: " << this << endl;
3079 cur.selection() = false;
3082 if (isRightToLeft(cur))
3083 cur.idx() = tabular.getLastCellInRow(0);
3089 if (isRightToLeft(cur))
3090 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3092 cur.idx() = tabular.cellCount() - 1;
3094 cur.pos() = cur.lastpos(); // FIXME crude guess
3096 // FIXME: this accesses the position cache before it is initialized
3098 //cur.bv().fitCursor();
3102 void InsetTabular::updateLabels(ParIterator const & it)
3104 // In a longtable, tell captions what the current float is
3105 Counters & cnts = buffer().params().documentClass().counters();
3106 string const saveflt = cnts.current_float();
3107 if (tabular.isLongTabular())
3108 cnts.current_float("table");
3110 ParIterator it2 = it;
3112 size_t const end = it2.nargs();
3113 for ( ; it2.idx() < end; it2.top().forwardIdx())
3114 lyx::updateLabels(buffer(), it2);
3117 if (tabular.isLongTabular())
3118 cnts.current_float(saveflt);
3122 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3124 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3125 << "\n cur:" << cur);
3126 CursorSlice sl = cur.top();
3127 Cursor & bvcur = cur.bv().cursor();
3129 switch (cmd.action) {
3131 case LFUN_MOUSE_PRESS: {
3132 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3134 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3135 || cmd.x > xo(cur.bv()) + tabular.width()) {
3136 row_type r = rowFromY(cur, cmd.y);
3137 cur.idx() = tabular.getFirstCellInRow(r);
3140 cur.idx() = tabular.getLastCellInRow(r);
3141 cur.pos() = cur.lastpos();
3142 cur.selection() = true;
3148 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3149 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3150 || cmd.y > y0 + tabular.height()) {
3151 col_type c = columnFromX(cur, cmd.x);
3152 cur.idx() = tabular.cellIndex(0, c);
3155 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3156 cur.pos() = cur.lastpos();
3157 cur.selection() = true;
3162 // do not reset cursor/selection if we have selected
3163 // some cells (bug 2715).
3164 if (cmd.button() == mouse_button::button3
3165 && &bvcur.selBegin().inset() == this
3166 && tablemode(bvcur))
3169 // Let InsetTableCell do it
3170 cell(cur.idx())->dispatch(cur, cmd);
3173 case LFUN_MOUSE_MOTION:
3174 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3175 if (cmd.button() == mouse_button::button1) {
3176 // only accept motions to places not deeper nested than the real anchor
3177 if (!bvcur.anchor_.hasPart(cur)) {
3181 // select (additional) row
3183 row_type r = rowFromY(cur, cmd.y);
3184 cur.idx() = tabular.getLastCellInRow(r);
3185 bvcur.setCursor(cur);
3186 bvcur.selection() = true;
3189 // select (additional) column
3191 col_type c = columnFromX(cur, cmd.x);
3192 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3193 bvcur.setCursor(cur);
3194 bvcur.selection() = true;
3197 // only update if selection changes
3198 if (bvcur.idx() == cur.idx() &&
3199 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3201 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3202 bvcur.setCursor(cur);
3203 bvcur.selection() = true;
3207 case LFUN_MOUSE_RELEASE:
3212 case LFUN_CELL_BACKWARD:
3214 cur.selection() = false;
3217 case LFUN_CELL_FORWARD:
3219 cur.selection() = false;
3221 case LFUN_CHAR_FORWARD_SELECT:
3222 case LFUN_CHAR_FORWARD:
3223 cell(cur.idx())->dispatch(cur, cmd);
3224 if (!cur.result().dispatched()) {
3226 if (sl == cur.top())
3227 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3233 case LFUN_CHAR_BACKWARD_SELECT:
3234 case LFUN_CHAR_BACKWARD:
3235 cell(cur.idx())->dispatch(cur, cmd);
3236 if (!cur.result().dispatched()) {
3238 if (sl == cur.top())
3239 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3245 case LFUN_CHAR_RIGHT_SELECT:
3246 case LFUN_CHAR_RIGHT:
3247 //FIXME: for visual cursor, really move right
3248 if (isRightToLeft(cur))
3249 lyx::dispatch(FuncRequest(
3250 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3251 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3253 lyx::dispatch(FuncRequest(
3254 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3255 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3258 case LFUN_CHAR_LEFT_SELECT:
3259 case LFUN_CHAR_LEFT:
3260 //FIXME: for visual cursor, really move left
3261 if (isRightToLeft(cur))
3262 lyx::dispatch(FuncRequest(
3263 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3264 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3266 lyx::dispatch(FuncRequest(
3267 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3268 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3271 case LFUN_DOWN_SELECT:
3273 cell(cur.idx())->dispatch(cur, cmd);
3274 cur.dispatched(); // override the cell's decision
3275 if (sl == cur.top())
3276 // if our Text didn't do anything to the cursor
3277 // then we try to put the cursor into the cell below
3278 // setting also the right targetX.
3279 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3280 cur.idx() = tabular.cellBelow(cur.idx());
3282 TextMetrics const & tm =
3283 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3284 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3286 if (sl == cur.top()) {
3287 // we trick it to go to forward after leaving the
3289 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3294 case LFUN_UP_SELECT:
3296 cell(cur.idx())->dispatch(cur, cmd);
3297 cur.dispatched(); // override the cell's decision
3298 if (sl == cur.top())
3299 // if our Text didn't do anything to the cursor
3300 // then we try to put the cursor into the cell above
3301 // setting also the right targetX.
3302 if (tabular.cellRow(cur.idx()) != 0) {
3303 cur.idx() = tabular.cellAbove(cur.idx());
3304 cur.pit() = cur.lastpit();
3305 Text const * text = cell(cur.idx())->getText(0);
3306 TextMetrics const & tm = cur.bv().textMetrics(text);
3307 ParagraphMetrics const & pm =
3308 tm.parMetrics(cur.lastpit());
3309 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3311 if (sl == cur.top()) {
3312 cmd = FuncRequest(LFUN_UP);
3317 // case LFUN_SCREEN_DOWN: {
3318 // //if (hasSelection())
3319 // // cur.selection() = false;
3320 // col_type const col = tabular.cellColumn(cur.idx());
3321 // int const t = cur.bv().top_y() + cur.bv().height();
3322 // if (t < yo() + tabular.getHeightOfTabular()) {
3323 // cur.bv().scrollDocView(t);
3324 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3326 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3333 // case LFUN_SCREEN_UP: {
3334 // //if (hasSelection())
3335 // // cur.selection() = false;
3336 // col_type const col = tabular.cellColumn(cur.idx());
3337 // int const t = cur.bv().top_y() + cur.bv().height();
3339 // cur.bv().scrollDocView(t);
3343 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3347 // cur.par() = cur.lastpar();
3348 // cur.pos() = cur.lastpos();
3352 case LFUN_LAYOUT_TABULAR:
3353 cur.bv().showDialog("tabular", params2string(*this), this);
3356 case LFUN_INSET_DIALOG_UPDATE:
3357 cur.bv().updateDialog("tabular", params2string(*this));
3360 case LFUN_TABULAR_FEATURE:
3361 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3365 // insert file functions
3366 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3367 case LFUN_FILE_INSERT_PLAINTEXT: {
3369 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3370 FileName(to_utf8(cmd.argument())));
3373 cur.recordUndoInset(INSERT_UNDO);
3374 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3375 // content has been replaced,
3376 // so cursor might be invalid
3377 cur.pos() = cur.lastpos();
3378 cur.pit() = cur.lastpit();
3379 bvcur.setCursor(cur);
3386 if (tablemode(cur)) {
3387 if (copySelection(cur)) {
3388 cur.recordUndoInset(DELETE_UNDO);
3393 cell(cur.idx())->dispatch(cur, cmd);
3396 case LFUN_CHAR_DELETE_BACKWARD:
3397 case LFUN_CHAR_DELETE_FORWARD:
3398 if (tablemode(cur)) {
3399 cur.recordUndoInset(DELETE_UNDO);
3403 cell(cur.idx())->dispatch(cur, cmd);
3407 if (!cur.selection())
3409 if (tablemode(cur)) {
3413 cell(cur.idx())->dispatch(cur, cmd);
3416 case LFUN_CLIPBOARD_PASTE:
3417 case LFUN_PRIMARY_SELECTION_PASTE: {
3418 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3419 theClipboard().getAsText() :
3420 theSelection().get();
3423 // pass to InsertPlaintextString, but
3424 // only if we have multi-cell content
3425 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3426 cur.recordUndoInset(INSERT_UNDO);
3427 if (insertPlaintextString(cur.bv(), clip, false)) {
3428 // content has been replaced,
3429 // so cursor might be invalid
3430 cur.pos() = cur.lastpos();
3431 cur.pit() = cur.lastpit();
3432 bvcur.setCursor(cur);
3436 // Let the cell handle normal text
3437 cell(cur.idx())->dispatch(cur, cmd);
3442 if (tabularStackDirty() && theClipboard().isInternal() ||
3443 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3444 cur.recordUndoInset(INSERT_UNDO);
3445 pasteClipboard(cur);
3448 cell(cur.idx())->dispatch(cur, cmd);
3451 case LFUN_FONT_EMPH:
3452 case LFUN_FONT_BOLD:
3453 case LFUN_FONT_ROMAN:
3454 case LFUN_FONT_NOUN:
3455 case LFUN_FONT_ITAL:
3456 case LFUN_FONT_FRAK:
3457 case LFUN_FONT_TYPEWRITER:
3458 case LFUN_FONT_SANS:
3459 case LFUN_FONT_FREE_APPLY:
3460 case LFUN_FONT_FREE_UPDATE:
3461 case LFUN_FONT_SIZE:
3462 case LFUN_FONT_UNDERLINE:
3464 case LFUN_WORD_CAPITALIZE:
3465 case LFUN_WORD_UPCASE:
3466 case LFUN_WORD_LOWCASE:
3467 case LFUN_CHARS_TRANSPOSE:
3468 if (tablemode(cur)) {
3471 getSelection(cur, rs, re, cs, ce);
3472 Cursor tmpcur = cur;
3473 for (row_type i = rs; i <= re; ++i) {
3474 for (col_type j = cs; j <= ce; ++j) {
3475 // cursor follows cell:
3476 tmpcur.idx() = tabular.cellIndex(i, j);
3477 // select this cell only:
3480 tmpcur.resetAnchor();
3481 tmpcur.pit() = tmpcur.lastpit();
3482 tmpcur.pos() = tmpcur.top().lastpos();
3483 tmpcur.setCursor(tmpcur);
3484 tmpcur.setSelection();
3485 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3490 cell(cur.idx())->dispatch(cur, cmd);
3494 // we try to handle this event in the insets dispatch function.
3495 cell(cur.idx())->dispatch(cur, cmd);
3501 // function sets an object as defined in func_status.h:
3502 // states OK, Unknown, Disabled, On, Off.
3503 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3504 FuncStatus & status) const
3506 switch (cmd.action) {
3507 case LFUN_TABULAR_FEATURE: {
3508 int action = Tabular::LAST_ACTION;
3510 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3511 string const tmp = tabularFeature[i].feature;
3512 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3513 action = tabularFeature[i].action;
3517 if (action == Tabular::LAST_ACTION) {
3519 status.unknown(true);
3523 string const argument
3524 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3526 row_type sel_row_start = 0;
3527 row_type sel_row_end = 0;
3528 col_type sel_col_start = 0;
3529 col_type sel_col_end = 0;
3530 Tabular::ltType dummyltt;
3533 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3536 case Tabular::SET_PWIDTH:
3537 case Tabular::SET_MPWIDTH:
3538 case Tabular::SET_SPECIAL_COLUMN:
3539 case Tabular::SET_SPECIAL_MULTI:
3540 case Tabular::APPEND_ROW:
3541 case Tabular::APPEND_COLUMN:
3542 case Tabular::DELETE_ROW:
3543 case Tabular::DELETE_COLUMN:
3544 case Tabular::COPY_ROW:
3545 case Tabular::COPY_COLUMN:
3546 case Tabular::SET_ALL_LINES:
3547 case Tabular::UNSET_ALL_LINES:
3548 case Tabular::SET_TOP_SPACE:
3549 case Tabular::SET_BOTTOM_SPACE:
3550 case Tabular::SET_INTERLINE_SPACE:
3551 case Tabular::SET_BORDER_LINES:
3555 case Tabular::MULTICOLUMN:
3556 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3559 case Tabular::TOGGLE_LINE_TOP:
3560 status.setOnOff(tabular.topLine(cur.idx()));
3563 case Tabular::TOGGLE_LINE_BOTTOM:
3564 status.setOnOff(tabular.bottomLine(cur.idx()));
3567 case Tabular::TOGGLE_LINE_LEFT:
3568 status.setOnOff(tabular.leftLine(cur.idx()));
3571 case Tabular::TOGGLE_LINE_RIGHT:
3572 status.setOnOff(tabular.rightLine(cur.idx()));
3575 case Tabular::M_ALIGN_LEFT:
3577 case Tabular::ALIGN_LEFT:
3578 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3581 case Tabular::M_ALIGN_RIGHT:
3583 case Tabular::ALIGN_RIGHT:
3584 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3587 case Tabular::M_ALIGN_CENTER:
3589 case Tabular::ALIGN_CENTER:
3590 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3593 case Tabular::ALIGN_BLOCK:
3594 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3595 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3598 case Tabular::M_VALIGN_TOP:
3600 case Tabular::VALIGN_TOP:
3602 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3605 case Tabular::M_VALIGN_BOTTOM:
3607 case Tabular::VALIGN_BOTTOM:
3609 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3612 case Tabular::M_VALIGN_MIDDLE:
3614 case Tabular::VALIGN_MIDDLE:
3616 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3619 case Tabular::SET_LONGTABULAR:
3620 status.setOnOff(tabular.isLongTabular());
3623 case Tabular::UNSET_LONGTABULAR:
3624 status.setOnOff(!tabular.isLongTabular());
3627 case Tabular::TOGGLE_ROTATE_TABULAR:
3628 case Tabular::SET_ROTATE_TABULAR:
3629 status.setOnOff(tabular.getRotateTabular());
3632 case Tabular::UNSET_ROTATE_TABULAR:
3633 status.setOnOff(!tabular.getRotateTabular());
3636 case Tabular::TOGGLE_ROTATE_CELL:
3637 case Tabular::SET_ROTATE_CELL:
3638 status.setOnOff(!oneCellHasRotationState(false,
3639 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3642 case Tabular::UNSET_ROTATE_CELL:
3643 status.setOnOff(!oneCellHasRotationState(true,
3644 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3647 case Tabular::SET_USEBOX:
3648 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3651 case Tabular::SET_LTFIRSTHEAD:
3652 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3655 case Tabular::UNSET_LTFIRSTHEAD:
3656 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3659 case Tabular::SET_LTHEAD:
3660 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3663 case Tabular::UNSET_LTHEAD:
3664 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3667 case Tabular::SET_LTFOOT:
3668 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3671 case Tabular::UNSET_LTFOOT:
3672 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3675 case Tabular::SET_LTLASTFOOT:
3676 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3679 case Tabular::UNSET_LTLASTFOOT:
3680 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3683 case Tabular::SET_LTNEWPAGE:
3684 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3687 case Tabular::SET_BOOKTABS:
3688 status.setOnOff(tabular.useBookTabs());
3691 case Tabular::UNSET_BOOKTABS:
3692 status.setOnOff(!tabular.useBookTabs());
3697 status.enabled(false);
3703 // These are only enabled inside tabular
3704 case LFUN_CELL_BACKWARD:
3705 case LFUN_CELL_FORWARD:
3706 status.enabled(true);
3709 // disable these with multiple cells selected
3710 case LFUN_INSET_INSERT:
3711 case LFUN_TABULAR_INSERT:
3712 case LFUN_FLEX_INSERT:
3713 case LFUN_FLOAT_INSERT:
3714 case LFUN_FLOAT_WIDE_INSERT:
3715 case LFUN_FOOTNOTE_INSERT:
3716 case LFUN_MARGINALNOTE_INSERT:
3717 case LFUN_MATH_INSERT:
3718 case LFUN_MATH_MODE:
3719 case LFUN_MATH_MUTATE:
3720 case LFUN_MATH_DISPLAY:
3721 case LFUN_NOTE_INSERT:
3722 case LFUN_OPTIONAL_INSERT:
3723 case LFUN_BOX_INSERT:
3724 case LFUN_BRANCH_INSERT:
3725 case LFUN_WRAP_INSERT:
3726 case LFUN_ERT_INSERT: {
3727 if (tablemode(cur)) {
3728 status.enabled(false);
3731 return cell(cur.idx())->getStatus(cur, cmd, status);
3734 // disable in non-fixed-width cells
3735 case LFUN_NEWLINE_INSERT:
3736 case LFUN_BREAK_PARAGRAPH:
3737 case LFUN_BREAK_PARAGRAPH_SKIP: {
3738 if (tabular.getPWidth(cur.idx()).zero()) {
3739 status.enabled(false);
3742 return cell(cur.idx())->getStatus(cur, cmd, status);
3746 if (tabularStackDirty() && theClipboard().isInternal()) {
3747 status.enabled(true);
3750 return cell(cur.idx())->getStatus(cur, cmd, status);
3752 case LFUN_INSET_MODIFY:
3753 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3754 status.enabled(true);
3760 // we try to handle this event in the insets dispatch function.
3761 return cell(cur.idx())->getStatus(cur, cmd, status);
3766 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3768 return tabular.latex(os, runparams);
3772 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3774 os << '\n'; // output table on a new line
3775 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3776 tabular.plaintext(os, runparams, dp, false, 0);
3777 return PLAINTEXT_NEWLINE;
3781 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3786 // FIXME: Why not pass a proper DocIterator here?
3788 // if the table is inside a float it doesn't need the informaltable
3789 // wrapper. Search for it.
3790 for (master = owner(); master; master = master->owner())
3791 if (master->lyxCode() == FLOAT_CODE)
3796 os << "<informaltable>";
3799 ret += tabular.docbook(os, runparams);
3801 os << "</informaltable>";
3808 void InsetTabular::validate(LaTeXFeatures & features) const
3810 tabular.validate(features);
3814 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3816 return tabular.cellInset(idx);
3820 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3822 return tabular.cellInset(idx);
3826 void InsetTabular::cursorPos(BufferView const & bv,
3827 CursorSlice const & sl, bool boundary, int & x, int & y) const
3829 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3831 // y offset correction
3832 int const row = tabular.cellRow(sl.idx());
3833 for (int i = 0; i <= row; ++i) {
3835 y += tabular.rowAscent(i);
3836 y += tabular.getAdditionalHeight(i);
3839 y += tabular.rowDescent(i);
3842 // x offset correction
3843 int const col = tabular.cellColumn(sl.idx());
3844 int idx = tabular.cellIndex(row, 0);
3845 for (int j = 0; j < col; ++j) {
3846 if (tabular.isPartOfMultiColumn(row, j))
3848 x += tabular.columnWidth(idx);
3851 x += tabular.getBeginningOfTextInCell(idx);
3852 x += ADD_TO_TABULAR_WIDTH;
3857 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3861 Inset const & inset = *tabular.cellInset(cell);
3862 Point o = bv.coordCache().getInsets().xy(&inset);
3863 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3864 int const xend = xbeg + tabular.columnWidth(cell);
3865 row_type const row = tabular.cellRow(cell);
3866 int const ybeg = o.y_ - tabular.rowAscent(row) -
3867 tabular.getAdditionalHeight(row);
3868 int const yend = o.y_ + tabular.rowDescent(row);
3880 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3881 // << " ybeg=" << ybeg << " yend=" << yend
3882 // << " xx=" << xx << " yy=" << yy
3883 // << " dist=" << xx + yy << endl;
3888 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3890 //lyxerr << "InsetTabular::editXY: " << this << endl;
3891 cur.selection() = false;
3893 cur.idx() = getNearestCell(cur.bv(), x, y);
3895 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3899 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3901 cur.idx() = getNearestCell(cur.bv(), x, y);
3902 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3906 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3908 idx_type idx_min = 0;
3909 int dist_min = numeric_limits<int>::max();
3910 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3911 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3912 int const d = dist(bv, i, x, y);
3923 int InsetTabular::cellXPos(idx_type const cell) const
3927 for (; !tabular.isFirstCellInRow(c); --c)
3930 for (; c < cell; ++c)
3931 lx += tabular.columnWidth(c);
3937 void InsetTabular::resetPos(Cursor & cur) const
3939 BufferView & bv = cur.bv();
3940 int const maxwidth = bv.workWidth();
3942 if (&cur.inset() != this) {
3946 int const X2 = maxwidth;
3947 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3948 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3949 int const x2 = x1 + tabular.columnWidth(cur.idx());
3952 scx_ = X1 + 20 - x1;
3954 scx_ = X2 - 20 - x2;
3959 cur.updateFlags(Update::Force | Update::FitCursor);
3963 void InsetTabular::moveNextCell(Cursor & cur)
3965 if (isRightToLeft(cur)) {
3966 if (tabular.isFirstCellInRow(cur.idx())) {
3967 row_type const row = tabular.cellRow(cur.idx());
3968 if (row == tabular.rowCount() - 1)
3970 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3977 if (tabular.isLastCell(cur.idx()))
3987 void InsetTabular::movePrevCell(Cursor & cur)
3989 if (isRightToLeft(cur)) {
3990 if (tabular.isLastCellInRow(cur.idx())) {
3991 row_type const row = tabular.cellRow(cur.idx());
3994 cur.idx() = tabular.getFirstCellInRow(row);
3995 cur.idx() = tabular.cellAbove(cur.idx());
3997 if (tabular.isLastCell(cur.idx()))
4002 if (cur.idx() == 0) // first cell
4006 cur.pit() = cur.lastpit();
4007 cur.pos() = cur.lastpos();
4009 // FIXME: this accesses the position cache before it is initialized
4014 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4016 Tabular::Feature action = Tabular::LAST_ACTION;
4019 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4020 string const tmp = tabularFeature[i].feature;
4022 if (tmp == what.substr(0, tmp.length())) {
4023 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4024 //tabularFeatures[i].feature.length()))
4025 action = tabularFeature[i].action;
4029 if (action == Tabular::LAST_ACTION)
4033 ltrim(what.substr(tabularFeature[i].feature.length()));
4034 tabularFeatures(cur, action, val);
4039 static void checkLongtableSpecial(Tabular::ltType & ltt,
4040 string const & special, bool & flag)
4042 if (special == "dl_above") {
4045 } else if (special == "dl_below") {
4046 ltt.bottomDL = flag;
4048 } else if (special == "empty") {
4057 bool InsetTabular::oneCellHasRotationState(bool rotated,
4058 row_type row_start, row_type row_end,
4059 col_type col_start, col_type col_end) const {
4061 for (row_type i = row_start; i <= row_end; ++i) {
4062 for (col_type j = col_start; j <= col_end; ++j) {
4063 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4072 void InsetTabular::tabularFeatures(Cursor & cur,
4073 Tabular::Feature feature, string const & value)
4075 col_type sel_col_start;
4076 col_type sel_col_end;
4077 row_type sel_row_start;
4078 row_type sel_row_end;
4079 bool setLines = false;
4080 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4081 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4085 case Tabular::M_ALIGN_LEFT:
4086 case Tabular::ALIGN_LEFT:
4087 setAlign = LYX_ALIGN_LEFT;
4090 case Tabular::M_ALIGN_RIGHT:
4091 case Tabular::ALIGN_RIGHT:
4092 setAlign = LYX_ALIGN_RIGHT;
4095 case Tabular::M_ALIGN_CENTER:
4096 case Tabular::ALIGN_CENTER:
4097 setAlign = LYX_ALIGN_CENTER;
4100 case Tabular::ALIGN_BLOCK:
4101 setAlign = LYX_ALIGN_BLOCK;
4104 case Tabular::M_VALIGN_TOP:
4105 case Tabular::VALIGN_TOP:
4106 setVAlign = Tabular::LYX_VALIGN_TOP;
4109 case Tabular::M_VALIGN_BOTTOM:
4110 case Tabular::VALIGN_BOTTOM:
4111 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4114 case Tabular::M_VALIGN_MIDDLE:
4115 case Tabular::VALIGN_MIDDLE:
4116 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4123 cur.recordUndoInset(ATOMIC_UNDO);
4125 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4126 row_type const row = tabular.cellRow(cur.idx());
4127 col_type const column = tabular.cellColumn(cur.idx());
4129 Tabular::ltType ltt;
4133 case Tabular::SET_PWIDTH: {
4134 Length const len(value);
4135 tabular.setColumnPWidth(cur, cur.idx(), len);
4137 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4138 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4142 case Tabular::SET_MPWIDTH:
4143 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4146 case Tabular::SET_SPECIAL_COLUMN:
4147 case Tabular::SET_SPECIAL_MULTI:
4148 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4151 case Tabular::APPEND_ROW:
4152 // append the row into the tabular
4153 tabular.appendRow(cur.idx());
4156 case Tabular::APPEND_COLUMN:
4157 // append the column into the tabular
4158 tabular.appendColumn(cur.idx());
4159 cur.idx() = tabular.cellIndex(row, column);
4162 case Tabular::DELETE_ROW:
4163 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4164 tabular.deleteRow(sel_row_start);
4165 if (sel_row_start >= tabular.rowCount())
4167 cur.idx() = tabular.cellIndex(sel_row_start, column);
4170 cur.selection() = false;
4173 case Tabular::DELETE_COLUMN:
4174 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4175 tabular.deleteColumn(sel_col_start);
4176 if (sel_col_start >= tabular.columnCount())
4178 cur.idx() = tabular.cellIndex(row, sel_col_start);
4181 cur.selection() = false;
4184 case Tabular::COPY_ROW:
4185 tabular.copyRow(row);
4188 case Tabular::COPY_COLUMN:
4189 tabular.copyColumn(column);
4190 cur.idx() = tabular.cellIndex(row, column);
4193 case Tabular::TOGGLE_LINE_TOP: {
4194 bool lineSet = !tabular.topLine(cur.idx());
4195 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4196 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4197 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4201 case Tabular::TOGGLE_LINE_BOTTOM: {
4202 bool lineSet = !tabular.bottomLine(cur.idx());
4203 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4204 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4205 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4209 case Tabular::TOGGLE_LINE_LEFT: {
4210 bool lineSet = !tabular.leftLine(cur.idx());
4211 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4212 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4213 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4217 case Tabular::TOGGLE_LINE_RIGHT: {
4218 bool lineSet = !tabular.rightLine(cur.idx());
4219 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4220 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4221 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4225 case Tabular::M_ALIGN_LEFT:
4226 case Tabular::M_ALIGN_RIGHT:
4227 case Tabular::M_ALIGN_CENTER:
4229 case Tabular::ALIGN_LEFT:
4230 case Tabular::ALIGN_RIGHT:
4231 case Tabular::ALIGN_CENTER:
4232 case Tabular::ALIGN_BLOCK:
4233 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4234 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4235 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4238 case Tabular::M_VALIGN_TOP:
4239 case Tabular::M_VALIGN_BOTTOM:
4240 case Tabular::M_VALIGN_MIDDLE:
4242 case Tabular::VALIGN_TOP:
4243 case Tabular::VALIGN_BOTTOM:
4244 case Tabular::VALIGN_MIDDLE:
4245 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4246 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4247 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4250 case Tabular::MULTICOLUMN: {
4251 if (sel_row_start != sel_row_end) {
4252 // FIXME: Need I say it ? This is horrible.
4254 Alert::error(_("Error setting multicolumn"),
4255 _("You cannot set multicolumn vertically."));
4258 if (!cur.selection()) {
4259 // just multicol for one single cell
4260 // check whether we are completely in a multicol
4261 if (tabular.isMultiColumn(cur.idx()))
4262 tabular.unsetMultiColumn(cur.idx());
4264 tabular.setMultiColumn(cur.idx(), 1);
4267 // we have a selection so this means we just add all this
4268 // cells to form a multicolumn cell
4269 idx_type const s_start = cur.selBegin().idx();
4270 idx_type const s_end = cur.selEnd().idx();
4271 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4272 cur.idx() = s_start;
4275 cur.selection() = false;
4279 case Tabular::SET_ALL_LINES:
4281 case Tabular::UNSET_ALL_LINES:
4282 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4283 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4284 tabular.setAllLines(
4285 tabular.cellIndex(i,j), setLines);
4288 case Tabular::SET_BORDER_LINES:
4289 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4290 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4291 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4293 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4294 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4295 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4299 case Tabular::SET_LONGTABULAR:
4300 tabular.setLongTabular(true);
4303 case Tabular::UNSET_LONGTABULAR:
4304 tabular.setLongTabular(false);
4307 case Tabular::SET_ROTATE_TABULAR:
4308 tabular.setRotateTabular(true);
4311 case Tabular::UNSET_ROTATE_TABULAR:
4312 tabular.setRotateTabular(false);
4315 case Tabular::TOGGLE_ROTATE_TABULAR:
4316 tabular.setRotateTabular(!tabular.getRotateTabular());
4319 case Tabular::SET_ROTATE_CELL:
4320 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4321 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4322 tabular.setRotateCell(
4323 tabular.cellIndex(i, j), true);
4326 case Tabular::UNSET_ROTATE_CELL:
4327 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4328 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4329 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4332 case Tabular::TOGGLE_ROTATE_CELL:
4334 bool oneNotRotated = oneCellHasRotationState(false,
4335 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4337 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4338 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4339 tabular.setRotateCell(tabular.cellIndex(i, j),
4344 case Tabular::SET_USEBOX: {
4345 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4346 if (val == tabular.getUsebox(cur.idx()))
4347 val = Tabular::BOX_NONE;
4348 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4349 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4350 tabular.setUsebox(tabular.cellIndex(i, j), val);
4354 case Tabular::UNSET_LTFIRSTHEAD:
4356 case Tabular::SET_LTFIRSTHEAD:
4357 tabular.getRowOfLTFirstHead(row, ltt);
4358 checkLongtableSpecial(ltt, value, flag);
4359 tabular.setLTHead(row, flag, ltt, true);
4362 case Tabular::UNSET_LTHEAD:
4364 case Tabular::SET_LTHEAD:
4365 tabular.getRowOfLTHead(row, ltt);
4366 checkLongtableSpecial(ltt, value, flag);
4367 tabular.setLTHead(row, flag, ltt, false);
4370 case Tabular::UNSET_LTFOOT:
4372 case Tabular::SET_LTFOOT:
4373 tabular.getRowOfLTFoot(row, ltt);
4374 checkLongtableSpecial(ltt, value, flag);
4375 tabular.setLTFoot(row, flag, ltt, false);
4378 case Tabular::UNSET_LTLASTFOOT:
4380 case Tabular::SET_LTLASTFOOT:
4381 tabular.getRowOfLTLastFoot(row, ltt);
4382 checkLongtableSpecial(ltt, value, flag);
4383 tabular.setLTFoot(row, flag, ltt, true);
4386 case Tabular::SET_LTNEWPAGE:
4387 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4390 case Tabular::SET_BOOKTABS:
4391 tabular.setBookTabs(true);
4394 case Tabular::UNSET_BOOKTABS:
4395 tabular.setBookTabs(false);
4398 case Tabular::SET_TOP_SPACE: {
4400 if (value == "default")
4401 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4402 tabular.row_info[i].top_space_default = true;
4403 else if (isValidLength(value, &len))
4404 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4405 tabular.row_info[i].top_space_default = false;
4406 tabular.row_info[i].top_space = len;
4409 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4410 tabular.row_info[i].top_space_default = false;
4411 tabular.row_info[i].top_space = len;
4416 case Tabular::SET_BOTTOM_SPACE: {
4418 if (value == "default")
4419 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4420 tabular.row_info[i].bottom_space_default = true;
4421 else if (isValidLength(value, &len))
4422 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4423 tabular.row_info[i].bottom_space_default = false;
4424 tabular.row_info[i].bottom_space = len;
4427 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4428 tabular.row_info[i].bottom_space_default = false;
4429 tabular.row_info[i].bottom_space = len;
4434 case Tabular::SET_INTERLINE_SPACE: {
4436 if (value == "default")
4437 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4438 tabular.row_info[i].interline_space_default = true;
4439 else if (isValidLength(value, &len))
4440 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4441 tabular.row_info[i].interline_space_default = false;
4442 tabular.row_info[i].interline_space = len;
4445 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4446 tabular.row_info[i].interline_space_default = false;
4447 tabular.row_info[i].interline_space = len;
4452 // dummy stuff just to avoid warnings
4453 case Tabular::LAST_ACTION:
4459 bool InsetTabular::showInsetDialog(BufferView * bv) const
4461 bv->showDialog("tabular", params2string(*this),
4462 const_cast<InsetTabular *>(this));
4467 void InsetTabular::openLayoutDialog(BufferView * bv) const
4469 bv->showDialog("tabular", params2string(*this),
4470 const_cast<InsetTabular *>(this));
4474 bool InsetTabular::copySelection(Cursor & cur)
4476 if (!cur.selection())
4481 getSelection(cur, rs, re, cs, ce);
4483 paste_tabular.reset(new Tabular(tabular));
4485 for (row_type i = 0; i < rs; ++i)
4486 paste_tabular->deleteRow(0);
4488 row_type const rows = re - rs + 1;
4489 while (paste_tabular->rowCount() > rows)
4490 paste_tabular->deleteRow(rows);
4492 for (col_type i = 0; i < cs; ++i)
4493 paste_tabular->deleteColumn(0);
4495 col_type const columns = ce - cs + 1;
4496 while (paste_tabular->columnCount() > columns)
4497 paste_tabular->deleteColumn(columns);
4499 // We clear all the InsetTableCell pointers, since they
4500 // might now become invalid and there is no point in having
4501 // them point to temporary things in paste_tabular.
4502 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4503 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4504 paste_tabular->cellInset(i,j)->setCellData(0);
4505 paste_tabular->cellInset(i,j)->setTabular(0);
4508 odocstringstream os;
4509 OutputParams const runparams(0);
4510 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4511 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4512 cap::copySelection(cur, os.str());
4514 // mark tabular stack dirty
4515 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4516 // when we (hopefully) have a one-for-all paste mechanism.
4517 // This must be called after cap::copySelection.
4518 dirtyTabularStack(true);
4524 bool InsetTabular::pasteClipboard(Cursor & cur)
4528 col_type const actcol = tabular.cellColumn(cur.idx());
4529 row_type const actrow = tabular.cellRow(cur.idx());
4530 for (row_type r1 = 0, r2 = actrow;
4531 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4533 for (col_type c1 = 0, c2 = actcol;
4534 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4536 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4537 tabular.isPartOfMultiColumn(r2, c2))
4539 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4543 if (tabular.isPartOfMultiColumn(r2, c2)) {
4547 shared_ptr<InsetTableCell> inset(
4548 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4549 // note that setCellInset will call InsetTableCell::setCellData()
4550 // and InsetTableCell::setTabular()
4551 tabular.setCellInset(r2, c2, inset);
4552 // FIXME: change tracking (MG)
4553 inset->setChange(Change(cur.buffer().params().trackChanges ?
4554 Change::INSERTED : Change::UNCHANGED));
4562 void InsetTabular::cutSelection(Cursor & cur)
4564 if (!cur.selection())
4569 getSelection(cur, rs, re, cs, ce);
4570 for (row_type i = rs; i <= re; ++i) {
4571 for (col_type j = cs; j <= ce; ++j) {
4572 shared_ptr<InsetTableCell> t
4573 = cell(tabular.cellIndex(i, j));
4574 if (cur.buffer().params().trackChanges)
4575 // FIXME: Change tracking (MG)
4576 t->setChange(Change(Change::DELETED));
4582 // cursor position might be invalid now
4583 if (cur.pit() > cur.lastpit())
4584 cur.pit() = cur.lastpit();
4585 if (cur.pos() > cur.lastpos())
4586 cur.pos() = cur.lastpos();
4587 cur.clearSelection();
4591 bool InsetTabular::isRightToLeft(Cursor & cur) const
4593 BOOST_ASSERT(cur.depth() > 1);
4594 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4595 pos_type const parentpos = cur[cur.depth() - 2].pos();
4596 return parentpar.getFontSettings(cur.bv().buffer().params(),
4597 parentpos).language()->rightToLeft();
4601 void InsetTabular::getSelection(Cursor & cur,
4602 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4604 CursorSlice const & beg = cur.selBegin();
4605 CursorSlice const & end = cur.selEnd();
4606 cs = tabular.cellColumn(beg.idx());
4607 ce = tabular.cellColumn(end.idx());
4610 cs = tabular.cellColumn(end.idx());
4612 ce = tabular.cellRightColumn(end.idx());
4615 rs = tabular.cellRow(beg.idx());
4616 re = tabular.cellRow(end.idx());
4622 Text * InsetTabular::getText(int idx) const
4624 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4628 void InsetTabular::setChange(Change const & change)
4630 for (idx_type idx = 0; idx < nargs(); ++idx)
4631 cell(idx)->setChange(change);
4635 void InsetTabular::acceptChanges(BufferParams const & bparams)
4637 for (idx_type idx = 0; idx < nargs(); ++idx)
4638 cell(idx)->acceptChanges(bparams);
4642 void InsetTabular::rejectChanges(BufferParams const & bparams)
4644 for (idx_type idx = 0; idx < nargs(); ++idx)
4645 cell(idx)->rejectChanges(bparams);
4649 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4651 return tabular.getPWidth(cell).zero();
4655 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4657 return !tabular.getPWidth(cell).zero();
4661 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4664 if (buf.length() <= 0)
4669 col_type maxCols = 1;
4670 size_t const len = buf.length();
4674 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4682 maxCols = max(cols, maxCols);
4688 maxCols = max(cols, maxCols);
4694 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4695 loctab = paste_tabular.get();
4697 dirtyTabularStack(true);
4700 cell = bv.cursor().idx();
4701 ocol = tabular.cellColumn(cell);
4702 row = tabular.cellRow(cell);
4706 idx_type const cells = loctab->cellCount();
4709 rows = loctab->rowCount();
4710 col_type const columns = loctab->columnCount();
4712 while (cell < cells && p < len && row < rows &&
4713 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4719 // we can only set this if we are not too far right
4720 if (cols < columns) {
4721 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4722 Font const font = bv.textMetrics(&inset->text_).
4724 inset->setText(buf.substr(op, p - op), font,
4725 buffer().params().trackChanges);
4731 // we can only set this if we are not too far right
4732 if (cols < columns) {
4733 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4734 Font const font = bv.textMetrics(&inset->text_).
4736 inset->setText(buf.substr(op, p - op), font,
4737 buffer().params().trackChanges);
4742 cell = loctab->cellIndex(row, cols);
4748 // check for the last cell if there is no trailing '\n'
4749 if (cell < cells && op < len) {
4750 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4751 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4752 inset->setText(buf.substr(op, len - op), font,
4753 buffer().params().trackChanges);
4759 void InsetTabular::addPreview(PreviewLoader & loader) const
4761 row_type const rows = tabular.rowCount();
4762 col_type const columns = tabular.columnCount();
4763 for (row_type i = 0; i < rows; ++i) {
4764 for (col_type j = 0; j < columns; ++j)
4765 tabular.cellInset(i, j)->addPreview(loader);
4770 bool InsetTabular::tablemode(Cursor & cur) const
4772 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4776 bool InsetTabular::completionSupported(Cursor const & cur) const
4778 Cursor const & bvCur = cur.bv().cursor();
4779 if (&bvCur.inset() != this)
4781 return cur.text()->completionSupported(cur);
4785 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4787 return completionSupported(cur);
4791 bool InsetTabular::automaticInlineCompletion() const
4793 return lyxrc.completion_inline_text;
4797 bool InsetTabular::automaticPopupCompletion() const
4799 return lyxrc.completion_popup_text;
4803 bool InsetTabular::showCompletionCursor() const
4805 return lyxrc.completion_cursor_text;
4809 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4811 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4815 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4817 if (!completionSupported(cur))
4819 return cur.text()->completionPrefix(cur);
4823 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4825 if (!completionSupported(cur))
4828 return cur.text()->insertCompletion(cur, s, finished);
4832 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4833 Dimension & dim) const
4835 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4836 tm.completionPosAndDim(cur, x, y, dim);
4840 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4842 istringstream data(in);
4844 lex.setStream(data);
4851 if (!lex || token != "tabular") {
4852 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4856 // This is part of the inset proper that is usually swallowed
4857 // by Buffer::readInset
4859 if (!lex || token != "Tabular") {
4860 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4868 string InsetTabular::params2string(InsetTabular const & inset)
4871 data << "tabular" << ' ';
4873 data << "\\end_inset\n";