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. ");
2836 int InsetTabular::rowFromY(Cursor & cur, int y) const
2838 // top y coordinate of tabular
2839 int h = yo(cur.bv()) - tabular.rowAscent(0);
2840 size_t nrows = tabular.rowCount();
2842 for (; r < nrows && y > h; ++r) {
2843 h += tabular.rowAscent(r);
2844 h += tabular.rowDescent(r);
2845 h += tabular.getAdditionalHeight(r);
2851 int InsetTabular::columnFromX(Cursor & cur, int x) const
2853 // left x coordinate of tabular
2854 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2855 size_t ncols = tabular.columnCount();
2857 for (; c < ncols && x > w; ++c)
2858 w += tabular.columnWidth(c);
2863 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2865 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2866 // mi.base.textwidth << "\n";
2869 BOOST_ASSERT(false);
2873 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2876 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2877 if (tabular.isPartOfMultiColumn(i, j))
2878 // Multicolumn cell, but not first one
2883 if (tabular.cell_info[i][j].multicolumn ==
2884 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2885 p_width = tabular.cellInfo(cell).p_width;
2887 p_width = tabular.column_info[j].p_width;
2888 if (!p_width.zero())
2889 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2890 tabular.cellInset(cell)->metrics(m, dim);
2891 if (!p_width.zero())
2892 dim.wid = m.base.textwidth;
2893 tabular.setCellWidth(cell, dim.wid);
2894 maxAsc = max(maxAsc, dim.asc);
2895 maxDesc = max(maxDesc, dim.des);
2898 int const top_space = tabular.row_info[i].top_space_default ?
2899 default_line_space :
2900 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2901 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2902 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2903 default_line_space :
2904 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2905 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2907 tabular.updateColumnWidths();
2908 dim.asc = tabular.rowAscent(0);
2909 dim.des = tabular.height() - dim.asc;
2910 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2914 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2916 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2917 BufferView * bv = pi.base.bv;
2919 // FIXME: As the full backrgound is painted in drawSelection(),
2920 // we have no choice but to do a full repaint for the Text cells.
2921 pi.full_repaint = true;
2923 resetPos(bv->cursor());
2926 x += ADD_TO_TABULAR_WIDTH;
2928 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2931 first_visible_cell = Tabular::npos;
2932 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2934 int const a = tabular.rowAscent(i);
2935 int const d = tabular.rowDescent(i);
2936 idx = tabular.cellIndex(i, 0);
2937 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2938 if (tabular.isPartOfMultiColumn(i, j))
2940 if (first_visible_cell == Tabular::npos)
2941 first_visible_cell = idx;
2943 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2944 // Cache the Inset position.
2945 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2946 if (nx + tabular.columnWidth(idx) < 0
2947 || nx > bv->workWidth()
2949 || y - a > bv->workHeight()) {
2950 pi.pain.setDrawingEnabled(false);
2951 cell(idx)->draw(pi, cx, y);
2952 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2953 pi.pain.setDrawingEnabled(original_drawing_state);
2955 cell(idx)->draw(pi, cx, y);
2956 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2958 nx += tabular.columnWidth(idx);
2962 if (i + 1 < tabular.rowCount())
2963 y += d + tabular.rowAscent(i + 1) +
2964 tabular.getAdditionalHeight(i + 1);
2969 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2971 Cursor & cur = pi.base.bv->cursor();
2973 x += scx_ + ADD_TO_TABULAR_WIDTH;
2975 // FIXME: it is wrong to completely paint the background
2976 // if we want to do single row painting.
2978 // Paint background of current tabular
2979 int const w = tabular.width();
2980 int const h = tabular.height();
2981 int yy = y - tabular.rowAscent(0);
2982 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2984 if (!cur.selection())
2986 if (&cur.inset() != this)
2992 if (tablemode(cur)) {
2995 getSelection(cur, rs, re, cs, ce);
2996 y -= tabular.rowAscent(0);
2997 for (row_type j = 0; j < tabular.rowCount(); ++j) {
2998 int const a = tabular.rowAscent(j);
2999 int const h = a + tabular.rowDescent(j);
3001 y += tabular.getAdditionalHeight(j);
3002 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3003 if (tabular.isPartOfMultiColumn(j, i))
3005 idx_type const cell =
3006 tabular.cellIndex(j, i);
3007 int const w = tabular.columnWidth(cell);
3008 if (i >= cs && i <= ce && j >= rs && j <= re)
3009 pi.pain.fillRectangle(xx, y, w, h,
3017 x += cellXPos(cur.idx());
3018 x += tabular.getBeginningOfTextInCell(cur.idx());
3019 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3024 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3025 row_type row, idx_type cell, bool erased) const
3027 int x2 = x + tabular.columnWidth(cell);
3028 bool on_off = false;
3029 ColorCode col = Color_tabularline;
3030 ColorCode onoffcol = Color_tabularonoffline;
3033 col = Color_deletedtext;
3034 onoffcol = Color_deletedtext;
3037 if (!tabular.topAlreadyDrawn(cell)) {
3038 on_off = !tabular.topLine(cell);
3039 pain.line(x, y - tabular.rowAscent(row),
3040 x2, y - tabular.rowAscent(row),
3041 on_off ? onoffcol : col,
3042 on_off ? Painter::line_onoffdash : Painter::line_solid);
3044 on_off = !tabular.bottomLine(cell);
3045 pain.line(x, y + tabular.rowDescent(row),
3046 x2, y + tabular.rowDescent(row),
3047 on_off ? onoffcol : col,
3048 on_off ? Painter::line_onoffdash : Painter::line_solid);
3049 if (!tabular.leftAlreadyDrawn(cell)) {
3050 on_off = !tabular.leftLine(cell);
3051 pain.line(x, y - tabular.rowAscent(row),
3052 x, y + tabular.rowDescent(row),
3053 on_off ? onoffcol : col,
3054 on_off ? Painter::line_onoffdash : Painter::line_solid);
3056 on_off = !tabular.rightLine(cell);
3057 pain.line(x2 - tabular.getAdditionalWidth(cell),
3058 y - tabular.rowAscent(row),
3059 x2 - tabular.getAdditionalWidth(cell),
3060 y + tabular.rowDescent(row),
3061 on_off ? onoffcol : col,
3062 on_off ? Painter::line_onoffdash : Painter::line_solid);
3066 docstring InsetTabular::editMessage() const
3068 return _("Opened table");
3072 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3074 //lyxerr << "InsetTabular::edit: " << this << endl;
3076 cur.selection() = false;
3079 if (isRightToLeft(cur))
3080 cur.idx() = tabular.getLastCellInRow(0);
3086 if (isRightToLeft(cur))
3087 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3089 cur.idx() = tabular.cellCount() - 1;
3091 cur.pos() = cur.lastpos(); // FIXME crude guess
3093 // FIXME: this accesses the position cache before it is initialized
3095 //cur.bv().fitCursor();
3099 void InsetTabular::updateLabels(ParIterator const & it)
3101 // In a longtable, tell captions what the current float is
3102 Counters & cnts = buffer().params().documentClass().counters();
3103 string const saveflt = cnts.current_float();
3104 if (tabular.isLongTabular())
3105 cnts.current_float("table");
3107 ParIterator it2 = it;
3109 size_t const end = it2.nargs();
3110 for ( ; it2.idx() < end; it2.top().forwardIdx())
3111 lyx::updateLabels(buffer(), it2);
3114 if (tabular.isLongTabular())
3115 cnts.current_float(saveflt);
3119 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3121 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3122 << "\n cur:" << cur);
3123 CursorSlice sl = cur.top();
3124 Cursor & bvcur = cur.bv().cursor();
3126 switch (cmd.action) {
3128 case LFUN_MOUSE_PRESS: {
3129 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3131 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3132 || cmd.x > xo(cur.bv()) + tabular.width()) {
3133 row_type r = rowFromY(cur, cmd.y);
3134 cur.idx() = tabular.getFirstCellInRow(r);
3137 cur.idx() = tabular.getLastCellInRow(r);
3138 cur.pos() = cur.lastpos();
3139 cur.selection() = true;
3145 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3146 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3147 || cmd.y > y0 + tabular.height()) {
3148 col_type c = columnFromX(cur, cmd.x);
3149 cur.idx() = tabular.cellIndex(0, c);
3152 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3153 cur.pos() = cur.lastpos();
3154 cur.selection() = true;
3159 // do not reset cursor/selection if we have selected
3160 // some cells (bug 2715).
3161 if (cmd.button() == mouse_button::button3
3162 && &bvcur.selBegin().inset() == this
3163 && tablemode(bvcur))
3166 // Let InsetTableCell do it
3167 cell(cur.idx())->dispatch(cur, cmd);
3170 case LFUN_MOUSE_MOTION:
3171 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3172 if (cmd.button() == mouse_button::button1) {
3173 // only accept motions to places not deeper nested than the real anchor
3174 if (!bvcur.anchor_.hasPart(cur)) {
3178 // select (additional) row
3180 row_type r = rowFromY(cur, cmd.y);
3181 cur.idx() = tabular.getLastCellInRow(r);
3182 bvcur.setCursor(cur);
3183 bvcur.selection() = true;
3186 // select (additional) column
3188 col_type c = columnFromX(cur, cmd.x);
3189 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3190 bvcur.setCursor(cur);
3191 bvcur.selection() = true;
3194 // only update if selection changes
3195 if (bvcur.idx() == cur.idx() &&
3196 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3198 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3199 bvcur.setCursor(cur);
3200 bvcur.selection() = true;
3204 case LFUN_MOUSE_RELEASE:
3209 case LFUN_CELL_BACKWARD:
3211 cur.selection() = false;
3214 case LFUN_CELL_FORWARD:
3216 cur.selection() = false;
3218 case LFUN_CHAR_FORWARD_SELECT:
3219 case LFUN_CHAR_FORWARD:
3220 cell(cur.idx())->dispatch(cur, cmd);
3221 if (!cur.result().dispatched()) {
3223 if (sl == cur.top())
3224 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3230 case LFUN_CHAR_BACKWARD_SELECT:
3231 case LFUN_CHAR_BACKWARD:
3232 cell(cur.idx())->dispatch(cur, cmd);
3233 if (!cur.result().dispatched()) {
3235 if (sl == cur.top())
3236 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3242 case LFUN_CHAR_RIGHT_SELECT:
3243 case LFUN_CHAR_RIGHT:
3244 //FIXME: for visual cursor, really move right
3245 if (isRightToLeft(cur))
3246 lyx::dispatch(FuncRequest(
3247 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3248 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3250 lyx::dispatch(FuncRequest(
3251 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3252 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3255 case LFUN_CHAR_LEFT_SELECT:
3256 case LFUN_CHAR_LEFT:
3257 //FIXME: for visual cursor, really move left
3258 if (isRightToLeft(cur))
3259 lyx::dispatch(FuncRequest(
3260 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3261 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3263 lyx::dispatch(FuncRequest(
3264 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3265 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3268 case LFUN_DOWN_SELECT:
3270 cell(cur.idx())->dispatch(cur, cmd);
3271 cur.dispatched(); // override the cell's decision
3272 if (sl == cur.top())
3273 // if our Text didn't do anything to the cursor
3274 // then we try to put the cursor into the cell below
3275 // setting also the right targetX.
3276 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3277 cur.idx() = tabular.cellBelow(cur.idx());
3279 TextMetrics const & tm =
3280 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3281 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3283 if (sl == cur.top()) {
3284 // we trick it to go to forward after leaving the
3286 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3291 case LFUN_UP_SELECT:
3293 cell(cur.idx())->dispatch(cur, cmd);
3294 cur.dispatched(); // override the cell's decision
3295 if (sl == cur.top())
3296 // if our Text didn't do anything to the cursor
3297 // then we try to put the cursor into the cell above
3298 // setting also the right targetX.
3299 if (tabular.cellRow(cur.idx()) != 0) {
3300 cur.idx() = tabular.cellAbove(cur.idx());
3301 cur.pit() = cur.lastpit();
3302 Text const * text = cell(cur.idx())->getText(0);
3303 TextMetrics const & tm = cur.bv().textMetrics(text);
3304 ParagraphMetrics const & pm =
3305 tm.parMetrics(cur.lastpit());
3306 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3308 if (sl == cur.top()) {
3309 cmd = FuncRequest(LFUN_UP);
3314 // case LFUN_SCREEN_DOWN: {
3315 // //if (hasSelection())
3316 // // cur.selection() = false;
3317 // col_type const col = tabular.cellColumn(cur.idx());
3318 // int const t = cur.bv().top_y() + cur.bv().height();
3319 // if (t < yo() + tabular.getHeightOfTabular()) {
3320 // cur.bv().scrollDocView(t);
3321 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3323 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3330 // case LFUN_SCREEN_UP: {
3331 // //if (hasSelection())
3332 // // cur.selection() = false;
3333 // col_type const col = tabular.cellColumn(cur.idx());
3334 // int const t = cur.bv().top_y() + cur.bv().height();
3336 // cur.bv().scrollDocView(t);
3340 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3344 // cur.par() = cur.lastpar();
3345 // cur.pos() = cur.lastpos();
3349 case LFUN_LAYOUT_TABULAR:
3350 cur.bv().showDialog("tabular", params2string(*this), this);
3353 case LFUN_INSET_DIALOG_UPDATE:
3354 cur.bv().updateDialog("tabular", params2string(*this));
3357 case LFUN_TABULAR_FEATURE:
3358 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3362 // insert file functions
3363 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3364 case LFUN_FILE_INSERT_PLAINTEXT: {
3366 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3367 FileName(to_utf8(cmd.argument())));
3370 cur.recordUndoInset(INSERT_UNDO);
3371 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3372 // content has been replaced,
3373 // so cursor might be invalid
3374 cur.pos() = cur.lastpos();
3375 cur.pit() = cur.lastpit();
3376 bvcur.setCursor(cur);
3383 if (tablemode(cur)) {
3384 if (copySelection(cur)) {
3385 cur.recordUndoInset(DELETE_UNDO);
3390 cell(cur.idx())->dispatch(cur, cmd);
3393 case LFUN_CHAR_DELETE_BACKWARD:
3394 case LFUN_CHAR_DELETE_FORWARD:
3395 if (tablemode(cur)) {
3396 cur.recordUndoInset(DELETE_UNDO);
3400 cell(cur.idx())->dispatch(cur, cmd);
3404 if (!cur.selection())
3406 if (tablemode(cur)) {
3410 cell(cur.idx())->dispatch(cur, cmd);
3413 case LFUN_CLIPBOARD_PASTE:
3414 case LFUN_PRIMARY_SELECTION_PASTE: {
3415 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3416 theClipboard().getAsText() :
3417 theSelection().get();
3420 // pass to InsertPlaintextString, but
3421 // only if we have multi-cell content
3422 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3423 cur.recordUndoInset(INSERT_UNDO);
3424 if (insertPlaintextString(cur.bv(), clip, false)) {
3425 // content has been replaced,
3426 // so cursor might be invalid
3427 cur.pos() = cur.lastpos();
3428 cur.pit() = cur.lastpit();
3429 bvcur.setCursor(cur);
3433 // Let the cell handle normal text
3434 cell(cur.idx())->dispatch(cur, cmd);
3439 if (tabularStackDirty() && theClipboard().isInternal() ||
3440 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3441 cur.recordUndoInset(INSERT_UNDO);
3442 pasteClipboard(cur);
3445 cell(cur.idx())->dispatch(cur, cmd);
3448 case LFUN_FONT_EMPH:
3449 case LFUN_FONT_BOLD:
3450 case LFUN_FONT_ROMAN:
3451 case LFUN_FONT_NOUN:
3452 case LFUN_FONT_ITAL:
3453 case LFUN_FONT_FRAK:
3454 case LFUN_FONT_TYPEWRITER:
3455 case LFUN_FONT_SANS:
3456 case LFUN_FONT_FREE_APPLY:
3457 case LFUN_FONT_FREE_UPDATE:
3458 case LFUN_FONT_SIZE:
3459 case LFUN_FONT_UNDERLINE:
3461 case LFUN_WORD_CAPITALIZE:
3462 case LFUN_WORD_UPCASE:
3463 case LFUN_WORD_LOWCASE:
3464 case LFUN_CHARS_TRANSPOSE:
3465 if (tablemode(cur)) {
3468 getSelection(cur, rs, re, cs, ce);
3469 Cursor tmpcur = cur;
3470 for (row_type i = rs; i <= re; ++i) {
3471 for (col_type j = cs; j <= ce; ++j) {
3472 // cursor follows cell:
3473 tmpcur.idx() = tabular.cellIndex(i, j);
3474 // select this cell only:
3477 tmpcur.resetAnchor();
3478 tmpcur.pit() = tmpcur.lastpit();
3479 tmpcur.pos() = tmpcur.top().lastpos();
3480 tmpcur.setCursor(tmpcur);
3481 tmpcur.setSelection();
3482 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3487 cell(cur.idx())->dispatch(cur, cmd);
3491 // we try to handle this event in the insets dispatch function.
3492 cell(cur.idx())->dispatch(cur, cmd);
3498 // function sets an object as defined in func_status.h:
3499 // states OK, Unknown, Disabled, On, Off.
3500 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3501 FuncStatus & status) const
3503 switch (cmd.action) {
3504 case LFUN_TABULAR_FEATURE: {
3505 int action = Tabular::LAST_ACTION;
3507 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3508 string const tmp = tabularFeature[i].feature;
3509 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3510 action = tabularFeature[i].action;
3514 if (action == Tabular::LAST_ACTION) {
3516 status.unknown(true);
3520 string const argument
3521 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3523 row_type sel_row_start = 0;
3524 row_type sel_row_end = 0;
3525 col_type sel_col_start = 0;
3526 col_type sel_col_end = 0;
3527 Tabular::ltType dummyltt;
3530 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3533 case Tabular::SET_PWIDTH:
3534 case Tabular::SET_MPWIDTH:
3535 case Tabular::SET_SPECIAL_COLUMN:
3536 case Tabular::SET_SPECIAL_MULTI:
3537 case Tabular::APPEND_ROW:
3538 case Tabular::APPEND_COLUMN:
3539 case Tabular::DELETE_ROW:
3540 case Tabular::DELETE_COLUMN:
3541 case Tabular::COPY_ROW:
3542 case Tabular::COPY_COLUMN:
3543 case Tabular::SET_ALL_LINES:
3544 case Tabular::UNSET_ALL_LINES:
3545 case Tabular::SET_TOP_SPACE:
3546 case Tabular::SET_BOTTOM_SPACE:
3547 case Tabular::SET_INTERLINE_SPACE:
3548 case Tabular::SET_BORDER_LINES:
3552 case Tabular::MULTICOLUMN:
3553 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3556 case Tabular::TOGGLE_LINE_TOP:
3557 status.setOnOff(tabular.topLine(cur.idx()));
3560 case Tabular::TOGGLE_LINE_BOTTOM:
3561 status.setOnOff(tabular.bottomLine(cur.idx()));
3564 case Tabular::TOGGLE_LINE_LEFT:
3565 status.setOnOff(tabular.leftLine(cur.idx()));
3568 case Tabular::TOGGLE_LINE_RIGHT:
3569 status.setOnOff(tabular.rightLine(cur.idx()));
3572 case Tabular::M_ALIGN_LEFT:
3574 case Tabular::ALIGN_LEFT:
3575 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3578 case Tabular::M_ALIGN_RIGHT:
3580 case Tabular::ALIGN_RIGHT:
3581 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3584 case Tabular::M_ALIGN_CENTER:
3586 case Tabular::ALIGN_CENTER:
3587 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3590 case Tabular::ALIGN_BLOCK:
3591 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3592 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3595 case Tabular::M_VALIGN_TOP:
3597 case Tabular::VALIGN_TOP:
3599 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3602 case Tabular::M_VALIGN_BOTTOM:
3604 case Tabular::VALIGN_BOTTOM:
3606 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3609 case Tabular::M_VALIGN_MIDDLE:
3611 case Tabular::VALIGN_MIDDLE:
3613 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3616 case Tabular::SET_LONGTABULAR:
3617 status.setOnOff(tabular.isLongTabular());
3620 case Tabular::UNSET_LONGTABULAR:
3621 status.setOnOff(!tabular.isLongTabular());
3624 case Tabular::TOGGLE_ROTATE_TABULAR:
3625 case Tabular::SET_ROTATE_TABULAR:
3626 status.setOnOff(tabular.getRotateTabular());
3629 case Tabular::UNSET_ROTATE_TABULAR:
3630 status.setOnOff(!tabular.getRotateTabular());
3633 case Tabular::TOGGLE_ROTATE_CELL:
3634 case Tabular::SET_ROTATE_CELL:
3635 status.setOnOff(!oneCellHasRotationState(false,
3636 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3639 case Tabular::UNSET_ROTATE_CELL:
3640 status.setOnOff(!oneCellHasRotationState(true,
3641 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3644 case Tabular::SET_USEBOX:
3645 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3648 case Tabular::SET_LTFIRSTHEAD:
3649 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3652 case Tabular::UNSET_LTFIRSTHEAD:
3653 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3656 case Tabular::SET_LTHEAD:
3657 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3660 case Tabular::UNSET_LTHEAD:
3661 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3664 case Tabular::SET_LTFOOT:
3665 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3668 case Tabular::UNSET_LTFOOT:
3669 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3672 case Tabular::SET_LTLASTFOOT:
3673 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3676 case Tabular::UNSET_LTLASTFOOT:
3677 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3680 case Tabular::SET_LTNEWPAGE:
3681 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3684 case Tabular::SET_BOOKTABS:
3685 status.setOnOff(tabular.useBookTabs());
3688 case Tabular::UNSET_BOOKTABS:
3689 status.setOnOff(!tabular.useBookTabs());
3694 status.enabled(false);
3700 // These are only enabled inside tabular
3701 case LFUN_CELL_BACKWARD:
3702 case LFUN_CELL_FORWARD:
3703 status.enabled(true);
3706 // disable these with multiple cells selected
3707 case LFUN_INSET_INSERT:
3708 case LFUN_TABULAR_INSERT:
3709 case LFUN_FLEX_INSERT:
3710 case LFUN_FLOAT_INSERT:
3711 case LFUN_FLOAT_WIDE_INSERT:
3712 case LFUN_FOOTNOTE_INSERT:
3713 case LFUN_MARGINALNOTE_INSERT:
3714 case LFUN_MATH_INSERT:
3715 case LFUN_MATH_MODE:
3716 case LFUN_MATH_MUTATE:
3717 case LFUN_MATH_DISPLAY:
3718 case LFUN_NOTE_INSERT:
3719 case LFUN_OPTIONAL_INSERT:
3720 case LFUN_BOX_INSERT:
3721 case LFUN_BRANCH_INSERT:
3722 case LFUN_WRAP_INSERT:
3723 case LFUN_ERT_INSERT: {
3724 if (tablemode(cur)) {
3725 status.enabled(false);
3728 return cell(cur.idx())->getStatus(cur, cmd, status);
3731 // disable in non-fixed-width cells
3732 case LFUN_NEWLINE_INSERT:
3733 case LFUN_BREAK_PARAGRAPH:
3734 case LFUN_BREAK_PARAGRAPH_SKIP: {
3735 if (tabular.getPWidth(cur.idx()).zero()) {
3736 status.enabled(false);
3739 return cell(cur.idx())->getStatus(cur, cmd, status);
3743 if (tabularStackDirty() && theClipboard().isInternal()) {
3744 status.enabled(true);
3747 return cell(cur.idx())->getStatus(cur, cmd, status);
3749 case LFUN_INSET_MODIFY:
3750 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3751 status.enabled(true);
3757 // we try to handle this event in the insets dispatch function.
3758 return cell(cur.idx())->getStatus(cur, cmd, status);
3763 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3765 return tabular.latex(os, runparams);
3769 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3771 os << '\n'; // output table on a new line
3772 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3773 tabular.plaintext(os, runparams, dp, false, 0);
3774 return PLAINTEXT_NEWLINE;
3778 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3783 // FIXME: Why not pass a proper DocIterator here?
3785 // if the table is inside a float it doesn't need the informaltable
3786 // wrapper. Search for it.
3787 for (master = owner(); master; master = master->owner())
3788 if (master->lyxCode() == FLOAT_CODE)
3793 os << "<informaltable>";
3796 ret += tabular.docbook(os, runparams);
3798 os << "</informaltable>";
3805 void InsetTabular::validate(LaTeXFeatures & features) const
3807 tabular.validate(features);
3811 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3813 return tabular.cellInset(idx);
3817 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3819 return tabular.cellInset(idx);
3823 void InsetTabular::cursorPos(BufferView const & bv,
3824 CursorSlice const & sl, bool boundary, int & x, int & y) const
3826 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3828 // y offset correction
3829 int const row = tabular.cellRow(sl.idx());
3830 for (int i = 0; i <= row; ++i) {
3832 y += tabular.rowAscent(i);
3833 y += tabular.getAdditionalHeight(i);
3836 y += tabular.rowDescent(i);
3839 // x offset correction
3840 int const col = tabular.cellColumn(sl.idx());
3841 int idx = tabular.cellIndex(row, 0);
3842 for (int j = 0; j < col; ++j) {
3843 if (tabular.isPartOfMultiColumn(row, j))
3845 x += tabular.columnWidth(idx);
3848 x += tabular.getBeginningOfTextInCell(idx);
3849 x += ADD_TO_TABULAR_WIDTH;
3854 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3858 Inset const & inset = *tabular.cellInset(cell);
3859 Point o = bv.coordCache().getInsets().xy(&inset);
3860 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3861 int const xend = xbeg + tabular.columnWidth(cell);
3862 row_type const row = tabular.cellRow(cell);
3863 int const ybeg = o.y_ - tabular.rowAscent(row) -
3864 tabular.getAdditionalHeight(row);
3865 int const yend = o.y_ + tabular.rowDescent(row);
3877 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3878 // << " ybeg=" << ybeg << " yend=" << yend
3879 // << " xx=" << xx << " yy=" << yy
3880 // << " dist=" << xx + yy << endl;
3885 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3887 //lyxerr << "InsetTabular::editXY: " << this << endl;
3888 cur.selection() = false;
3890 cur.idx() = getNearestCell(cur.bv(), x, y);
3892 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3896 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3898 cur.idx() = getNearestCell(cur.bv(), x, y);
3899 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3903 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3905 idx_type idx_min = 0;
3906 int dist_min = numeric_limits<int>::max();
3907 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3908 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3909 int const d = dist(bv, i, x, y);
3920 int InsetTabular::cellXPos(idx_type const cell) const
3924 for (; !tabular.isFirstCellInRow(c); --c)
3927 for (; c < cell; ++c)
3928 lx += tabular.columnWidth(c);
3934 void InsetTabular::resetPos(Cursor & cur) const
3936 BufferView & bv = cur.bv();
3937 int const maxwidth = bv.workWidth();
3939 if (&cur.inset() != this) {
3943 int const X2 = maxwidth;
3944 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3945 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3946 int const x2 = x1 + tabular.columnWidth(cur.idx());
3949 scx_ = X1 + 20 - x1;
3951 scx_ = X2 - 20 - x2;
3956 cur.updateFlags(Update::Force | Update::FitCursor);
3960 void InsetTabular::moveNextCell(Cursor & cur)
3962 if (isRightToLeft(cur)) {
3963 if (tabular.isFirstCellInRow(cur.idx())) {
3964 row_type const row = tabular.cellRow(cur.idx());
3965 if (row == tabular.rowCount() - 1)
3967 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3974 if (tabular.isLastCell(cur.idx()))
3984 void InsetTabular::movePrevCell(Cursor & cur)
3986 if (isRightToLeft(cur)) {
3987 if (tabular.isLastCellInRow(cur.idx())) {
3988 row_type const row = tabular.cellRow(cur.idx());
3991 cur.idx() = tabular.getFirstCellInRow(row);
3992 cur.idx() = tabular.cellAbove(cur.idx());
3994 if (tabular.isLastCell(cur.idx()))
3999 if (cur.idx() == 0) // first cell
4003 cur.pit() = cur.lastpit();
4004 cur.pos() = cur.lastpos();
4006 // FIXME: this accesses the position cache before it is initialized
4011 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4013 Tabular::Feature action = Tabular::LAST_ACTION;
4016 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4017 string const tmp = tabularFeature[i].feature;
4019 if (tmp == what.substr(0, tmp.length())) {
4020 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4021 //tabularFeatures[i].feature.length()))
4022 action = tabularFeature[i].action;
4026 if (action == Tabular::LAST_ACTION)
4030 ltrim(what.substr(tabularFeature[i].feature.length()));
4031 tabularFeatures(cur, action, val);
4036 static void checkLongtableSpecial(Tabular::ltType & ltt,
4037 string const & special, bool & flag)
4039 if (special == "dl_above") {
4042 } else if (special == "dl_below") {
4043 ltt.bottomDL = flag;
4045 } else if (special == "empty") {
4054 bool InsetTabular::oneCellHasRotationState(bool rotated,
4055 row_type row_start, row_type row_end,
4056 col_type col_start, col_type col_end) const {
4058 for (row_type i = row_start; i <= row_end; ++i) {
4059 for (col_type j = col_start; j <= col_end; ++j) {
4060 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4069 void InsetTabular::tabularFeatures(Cursor & cur,
4070 Tabular::Feature feature, string const & value)
4072 col_type sel_col_start;
4073 col_type sel_col_end;
4074 row_type sel_row_start;
4075 row_type sel_row_end;
4076 bool setLines = false;
4077 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4078 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4082 case Tabular::M_ALIGN_LEFT:
4083 case Tabular::ALIGN_LEFT:
4084 setAlign = LYX_ALIGN_LEFT;
4087 case Tabular::M_ALIGN_RIGHT:
4088 case Tabular::ALIGN_RIGHT:
4089 setAlign = LYX_ALIGN_RIGHT;
4092 case Tabular::M_ALIGN_CENTER:
4093 case Tabular::ALIGN_CENTER:
4094 setAlign = LYX_ALIGN_CENTER;
4097 case Tabular::ALIGN_BLOCK:
4098 setAlign = LYX_ALIGN_BLOCK;
4101 case Tabular::M_VALIGN_TOP:
4102 case Tabular::VALIGN_TOP:
4103 setVAlign = Tabular::LYX_VALIGN_TOP;
4106 case Tabular::M_VALIGN_BOTTOM:
4107 case Tabular::VALIGN_BOTTOM:
4108 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4111 case Tabular::M_VALIGN_MIDDLE:
4112 case Tabular::VALIGN_MIDDLE:
4113 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4120 cur.recordUndoInset(ATOMIC_UNDO);
4122 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4123 row_type const row = tabular.cellRow(cur.idx());
4124 col_type const column = tabular.cellColumn(cur.idx());
4126 Tabular::ltType ltt;
4130 case Tabular::SET_PWIDTH: {
4131 Length const len(value);
4132 tabular.setColumnPWidth(cur, cur.idx(), len);
4134 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4135 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4139 case Tabular::SET_MPWIDTH:
4140 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4143 case Tabular::SET_SPECIAL_COLUMN:
4144 case Tabular::SET_SPECIAL_MULTI:
4145 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4148 case Tabular::APPEND_ROW:
4149 // append the row into the tabular
4150 tabular.appendRow(cur.idx());
4153 case Tabular::APPEND_COLUMN:
4154 // append the column into the tabular
4155 tabular.appendColumn(cur.idx());
4156 cur.idx() = tabular.cellIndex(row, column);
4159 case Tabular::DELETE_ROW:
4160 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4161 tabular.deleteRow(sel_row_start);
4162 if (sel_row_start >= tabular.rowCount())
4164 cur.idx() = tabular.cellIndex(sel_row_start, column);
4167 cur.selection() = false;
4170 case Tabular::DELETE_COLUMN:
4171 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4172 tabular.deleteColumn(sel_col_start);
4173 if (sel_col_start >= tabular.columnCount())
4175 cur.idx() = tabular.cellIndex(row, sel_col_start);
4178 cur.selection() = false;
4181 case Tabular::COPY_ROW:
4182 tabular.copyRow(row);
4185 case Tabular::COPY_COLUMN:
4186 tabular.copyColumn(column);
4187 cur.idx() = tabular.cellIndex(row, column);
4190 case Tabular::TOGGLE_LINE_TOP: {
4191 bool lineSet = !tabular.topLine(cur.idx());
4192 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4193 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4194 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4198 case Tabular::TOGGLE_LINE_BOTTOM: {
4199 bool lineSet = !tabular.bottomLine(cur.idx());
4200 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4201 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4202 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4206 case Tabular::TOGGLE_LINE_LEFT: {
4207 bool lineSet = !tabular.leftLine(cur.idx());
4208 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4209 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4210 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4214 case Tabular::TOGGLE_LINE_RIGHT: {
4215 bool lineSet = !tabular.rightLine(cur.idx());
4216 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4217 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4218 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4222 case Tabular::M_ALIGN_LEFT:
4223 case Tabular::M_ALIGN_RIGHT:
4224 case Tabular::M_ALIGN_CENTER:
4226 case Tabular::ALIGN_LEFT:
4227 case Tabular::ALIGN_RIGHT:
4228 case Tabular::ALIGN_CENTER:
4229 case Tabular::ALIGN_BLOCK:
4230 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4231 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4232 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4235 case Tabular::M_VALIGN_TOP:
4236 case Tabular::M_VALIGN_BOTTOM:
4237 case Tabular::M_VALIGN_MIDDLE:
4239 case Tabular::VALIGN_TOP:
4240 case Tabular::VALIGN_BOTTOM:
4241 case Tabular::VALIGN_MIDDLE:
4242 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4243 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4244 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4247 case Tabular::MULTICOLUMN: {
4248 if (sel_row_start != sel_row_end) {
4249 // FIXME: Need I say it ? This is horrible.
4251 Alert::error(_("Error setting multicolumn"),
4252 _("You cannot set multicolumn vertically."));
4255 if (!cur.selection()) {
4256 // just multicol for one single cell
4257 // check whether we are completely in a multicol
4258 if (tabular.isMultiColumn(cur.idx()))
4259 tabular.unsetMultiColumn(cur.idx());
4261 tabular.setMultiColumn(cur.idx(), 1);
4264 // we have a selection so this means we just add all this
4265 // cells to form a multicolumn cell
4266 idx_type const s_start = cur.selBegin().idx();
4267 idx_type const s_end = cur.selEnd().idx();
4268 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4269 cur.idx() = s_start;
4272 cur.selection() = false;
4276 case Tabular::SET_ALL_LINES:
4278 case Tabular::UNSET_ALL_LINES:
4279 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4280 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4281 tabular.setAllLines(
4282 tabular.cellIndex(i,j), setLines);
4285 case Tabular::SET_BORDER_LINES:
4286 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4287 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4288 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4290 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4291 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4292 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4296 case Tabular::SET_LONGTABULAR:
4297 tabular.setLongTabular(true);
4300 case Tabular::UNSET_LONGTABULAR:
4301 tabular.setLongTabular(false);
4304 case Tabular::SET_ROTATE_TABULAR:
4305 tabular.setRotateTabular(true);
4308 case Tabular::UNSET_ROTATE_TABULAR:
4309 tabular.setRotateTabular(false);
4312 case Tabular::TOGGLE_ROTATE_TABULAR:
4313 tabular.setRotateTabular(!tabular.getRotateTabular());
4316 case Tabular::SET_ROTATE_CELL:
4317 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4318 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4319 tabular.setRotateCell(
4320 tabular.cellIndex(i, j), true);
4323 case Tabular::UNSET_ROTATE_CELL:
4324 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4325 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4326 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4329 case Tabular::TOGGLE_ROTATE_CELL:
4331 bool oneNotRotated = oneCellHasRotationState(false,
4332 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4334 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4335 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4336 tabular.setRotateCell(tabular.cellIndex(i, j),
4341 case Tabular::SET_USEBOX: {
4342 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4343 if (val == tabular.getUsebox(cur.idx()))
4344 val = Tabular::BOX_NONE;
4345 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4346 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4347 tabular.setUsebox(tabular.cellIndex(i, j), val);
4351 case Tabular::UNSET_LTFIRSTHEAD:
4353 case Tabular::SET_LTFIRSTHEAD:
4354 tabular.getRowOfLTFirstHead(row, ltt);
4355 checkLongtableSpecial(ltt, value, flag);
4356 tabular.setLTHead(row, flag, ltt, true);
4359 case Tabular::UNSET_LTHEAD:
4361 case Tabular::SET_LTHEAD:
4362 tabular.getRowOfLTHead(row, ltt);
4363 checkLongtableSpecial(ltt, value, flag);
4364 tabular.setLTHead(row, flag, ltt, false);
4367 case Tabular::UNSET_LTFOOT:
4369 case Tabular::SET_LTFOOT:
4370 tabular.getRowOfLTFoot(row, ltt);
4371 checkLongtableSpecial(ltt, value, flag);
4372 tabular.setLTFoot(row, flag, ltt, false);
4375 case Tabular::UNSET_LTLASTFOOT:
4377 case Tabular::SET_LTLASTFOOT:
4378 tabular.getRowOfLTLastFoot(row, ltt);
4379 checkLongtableSpecial(ltt, value, flag);
4380 tabular.setLTFoot(row, flag, ltt, true);
4383 case Tabular::SET_LTNEWPAGE:
4384 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4387 case Tabular::SET_BOOKTABS:
4388 tabular.setBookTabs(true);
4391 case Tabular::UNSET_BOOKTABS:
4392 tabular.setBookTabs(false);
4395 case Tabular::SET_TOP_SPACE: {
4397 if (value == "default")
4398 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4399 tabular.row_info[i].top_space_default = true;
4400 else if (isValidLength(value, &len))
4401 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4402 tabular.row_info[i].top_space_default = false;
4403 tabular.row_info[i].top_space = len;
4406 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4407 tabular.row_info[i].top_space_default = false;
4408 tabular.row_info[i].top_space = len;
4413 case Tabular::SET_BOTTOM_SPACE: {
4415 if (value == "default")
4416 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4417 tabular.row_info[i].bottom_space_default = true;
4418 else if (isValidLength(value, &len))
4419 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4420 tabular.row_info[i].bottom_space_default = false;
4421 tabular.row_info[i].bottom_space = len;
4424 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4425 tabular.row_info[i].bottom_space_default = false;
4426 tabular.row_info[i].bottom_space = len;
4431 case Tabular::SET_INTERLINE_SPACE: {
4433 if (value == "default")
4434 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4435 tabular.row_info[i].interline_space_default = true;
4436 else if (isValidLength(value, &len))
4437 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4438 tabular.row_info[i].interline_space_default = false;
4439 tabular.row_info[i].interline_space = len;
4442 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4443 tabular.row_info[i].interline_space_default = false;
4444 tabular.row_info[i].interline_space = len;
4449 // dummy stuff just to avoid warnings
4450 case Tabular::LAST_ACTION:
4456 bool InsetTabular::showInsetDialog(BufferView * bv) const
4458 bv->showDialog("tabular", params2string(*this),
4459 const_cast<InsetTabular *>(this));
4464 void InsetTabular::openLayoutDialog(BufferView * bv) const
4466 bv->showDialog("tabular", params2string(*this),
4467 const_cast<InsetTabular *>(this));
4471 bool InsetTabular::copySelection(Cursor & cur)
4473 if (!cur.selection())
4478 getSelection(cur, rs, re, cs, ce);
4480 paste_tabular.reset(new Tabular(tabular));
4482 for (row_type i = 0; i < rs; ++i)
4483 paste_tabular->deleteRow(0);
4485 row_type const rows = re - rs + 1;
4486 while (paste_tabular->rowCount() > rows)
4487 paste_tabular->deleteRow(rows);
4489 for (col_type i = 0; i < cs; ++i)
4490 paste_tabular->deleteColumn(0);
4492 col_type const columns = ce - cs + 1;
4493 while (paste_tabular->columnCount() > columns)
4494 paste_tabular->deleteColumn(columns);
4496 // We clear all the InsetTableCell pointers, since they
4497 // might now become invalid and there is no point in having
4498 // them point to temporary things in paste_tabular.
4499 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4500 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4501 paste_tabular->cellInset(i,j)->setCellData(0);
4502 paste_tabular->cellInset(i,j)->setTabular(0);
4505 odocstringstream os;
4506 OutputParams const runparams(0);
4507 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4508 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4509 cap::copySelection(cur, os.str());
4511 // mark tabular stack dirty
4512 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4513 // when we (hopefully) have a one-for-all paste mechanism.
4514 // This must be called after cap::copySelection.
4515 dirtyTabularStack(true);
4521 bool InsetTabular::pasteClipboard(Cursor & cur)
4525 col_type const actcol = tabular.cellColumn(cur.idx());
4526 row_type const actrow = tabular.cellRow(cur.idx());
4527 for (row_type r1 = 0, r2 = actrow;
4528 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4530 for (col_type c1 = 0, c2 = actcol;
4531 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4533 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4534 tabular.isPartOfMultiColumn(r2, c2))
4536 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4540 if (tabular.isPartOfMultiColumn(r2, c2)) {
4544 shared_ptr<InsetTableCell> inset(
4545 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4546 // note that setCellInset will call InsetTableCell::setCellData()
4547 // and InsetTableCell::setTabular()
4548 tabular.setCellInset(r2, c2, inset);
4549 // FIXME: change tracking (MG)
4550 inset->setChange(Change(cur.buffer().params().trackChanges ?
4551 Change::INSERTED : Change::UNCHANGED));
4559 void InsetTabular::cutSelection(Cursor & cur)
4561 if (!cur.selection())
4566 getSelection(cur, rs, re, cs, ce);
4567 for (row_type i = rs; i <= re; ++i) {
4568 for (col_type j = cs; j <= ce; ++j) {
4569 shared_ptr<InsetTableCell> t
4570 = cell(tabular.cellIndex(i, j));
4571 if (cur.buffer().params().trackChanges)
4572 // FIXME: Change tracking (MG)
4573 t->setChange(Change(Change::DELETED));
4579 // cursor position might be invalid now
4580 if (cur.pit() > cur.lastpit())
4581 cur.pit() = cur.lastpit();
4582 if (cur.pos() > cur.lastpos())
4583 cur.pos() = cur.lastpos();
4584 cur.clearSelection();
4588 bool InsetTabular::isRightToLeft(Cursor & cur) const
4590 BOOST_ASSERT(cur.depth() > 1);
4591 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4592 pos_type const parentpos = cur[cur.depth() - 2].pos();
4593 return parentpar.getFontSettings(cur.bv().buffer().params(),
4594 parentpos).language()->rightToLeft();
4598 void InsetTabular::getSelection(Cursor & cur,
4599 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4601 CursorSlice const & beg = cur.selBegin();
4602 CursorSlice const & end = cur.selEnd();
4603 cs = tabular.cellColumn(beg.idx());
4604 ce = tabular.cellColumn(end.idx());
4607 cs = tabular.cellColumn(end.idx());
4609 ce = tabular.cellRightColumn(end.idx());
4612 rs = tabular.cellRow(beg.idx());
4613 re = tabular.cellRow(end.idx());
4619 Text * InsetTabular::getText(int idx) const
4621 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4625 void InsetTabular::setChange(Change const & change)
4627 for (idx_type idx = 0; idx < nargs(); ++idx)
4628 cell(idx)->setChange(change);
4632 void InsetTabular::acceptChanges(BufferParams const & bparams)
4634 for (idx_type idx = 0; idx < nargs(); ++idx)
4635 cell(idx)->acceptChanges(bparams);
4639 void InsetTabular::rejectChanges(BufferParams const & bparams)
4641 for (idx_type idx = 0; idx < nargs(); ++idx)
4642 cell(idx)->rejectChanges(bparams);
4646 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4648 return tabular.getPWidth(cell).zero();
4652 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4654 return !tabular.getPWidth(cell).zero();
4658 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4661 if (buf.length() <= 0)
4666 col_type maxCols = 1;
4667 size_t const len = buf.length();
4671 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4679 maxCols = max(cols, maxCols);
4685 maxCols = max(cols, maxCols);
4691 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4692 loctab = paste_tabular.get();
4694 dirtyTabularStack(true);
4697 cell = bv.cursor().idx();
4698 ocol = tabular.cellColumn(cell);
4699 row = tabular.cellRow(cell);
4703 idx_type const cells = loctab->cellCount();
4706 rows = loctab->rowCount();
4707 col_type const columns = loctab->columnCount();
4709 while (cell < cells && p < len && row < rows &&
4710 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4716 // we can only set this if we are not too far right
4717 if (cols < columns) {
4718 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4719 Font const font = bv.textMetrics(&inset->text_).
4721 inset->setText(buf.substr(op, p - op), font,
4722 buffer().params().trackChanges);
4728 // we can only set this if we are not too far right
4729 if (cols < columns) {
4730 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4731 Font const font = bv.textMetrics(&inset->text_).
4733 inset->setText(buf.substr(op, p - op), font,
4734 buffer().params().trackChanges);
4739 cell = loctab->cellIndex(row, cols);
4745 // check for the last cell if there is no trailing '\n'
4746 if (cell < cells && op < len) {
4747 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4748 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4749 inset->setText(buf.substr(op, len - op), font,
4750 buffer().params().trackChanges);
4756 void InsetTabular::addPreview(PreviewLoader & loader) const
4758 row_type const rows = tabular.rowCount();
4759 col_type const columns = tabular.columnCount();
4760 for (row_type i = 0; i < rows; ++i) {
4761 for (col_type j = 0; j < columns; ++j)
4762 tabular.cellInset(i, j)->addPreview(loader);
4767 bool InsetTabular::tablemode(Cursor & cur) const
4769 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4773 bool InsetTabular::completionSupported(Cursor const & cur) const
4775 Cursor const & bvCur = cur.bv().cursor();
4776 if (&bvCur.inset() != this)
4778 return cur.text()->completionSupported(cur);
4782 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4784 return completionSupported(cur);
4788 bool InsetTabular::automaticInlineCompletion() const
4790 return lyxrc.completion_inline_text;
4794 bool InsetTabular::automaticPopupCompletion() const
4796 return lyxrc.completion_popup_text;
4800 bool InsetTabular::showCompletionCursor() const
4802 return lyxrc.completion_cursor_text;
4806 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4808 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4812 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4814 if (!completionSupported(cur))
4816 return cur.text()->completionPrefix(cur);
4820 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4822 if (!completionSupported(cur))
4825 return cur.text()->insertCompletion(cur, s, finished);
4829 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4830 Dimension & dim) const
4832 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4833 tm.completionPosAndDim(cur, x, y, dim);
4837 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4839 istringstream data(in);
4841 lex.setStream(data);
4848 if (!lex || token != "tabular") {
4849 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4853 // This is part of the inset proper that is usually swallowed
4854 // by Buffer::readInset
4856 if (!lex || token != "Tabular") {
4857 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4865 string InsetTabular::params2string(InsetTabular const & inset)
4868 data << "tabular" << ' ';
4870 data << "\\end_inset\n";