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/alert.h"
48 #include "frontends/Clipboard.h"
49 #include "frontends/Painter.h"
50 #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;
75 using cap::dirtyTabularStack;
76 using cap::tabularStackDirty;
78 using graphics::PreviewLoader;
80 using frontend::Painter;
81 using frontend::Clipboard;
83 namespace Alert = frontend::Alert;
88 int const ADD_TO_HEIGHT = 2; // in cell
89 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
90 int const default_line_space = 10; // ?
91 int const WIDTH_OF_LINE = 5; // space between double lines
95 boost::scoped_ptr<Tabular> paste_tabular;
98 struct TabularFeature {
99 Tabular::Feature action;
104 TabularFeature tabularFeature[] =
106 { Tabular::APPEND_ROW, "append-row" },
107 { Tabular::APPEND_COLUMN, "append-column" },
108 { Tabular::DELETE_ROW, "delete-row" },
109 { Tabular::DELETE_COLUMN, "delete-column" },
110 { Tabular::COPY_ROW, "copy-row" },
111 { Tabular::COPY_COLUMN, "copy-column" },
112 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
113 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
114 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
115 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
116 { Tabular::ALIGN_LEFT, "align-left" },
117 { Tabular::ALIGN_RIGHT, "align-right" },
118 { Tabular::ALIGN_CENTER, "align-center" },
119 { Tabular::ALIGN_BLOCK, "align-block" },
120 { Tabular::VALIGN_TOP, "valign-top" },
121 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
122 { Tabular::VALIGN_MIDDLE, "valign-middle" },
123 { Tabular::M_ALIGN_LEFT, "m-align-left" },
124 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
125 { Tabular::M_ALIGN_CENTER, "m-align-center" },
126 { Tabular::M_VALIGN_TOP, "m-valign-top" },
127 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
128 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
129 { Tabular::MULTICOLUMN, "multicolumn" },
130 { Tabular::SET_ALL_LINES, "set-all-lines" },
131 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
132 { Tabular::SET_LONGTABULAR, "set-longtabular" },
133 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
134 { Tabular::SET_PWIDTH, "set-pwidth" },
135 { Tabular::SET_MPWIDTH, "set-mpwidth" },
136 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
137 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
138 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
139 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
140 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
141 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
142 { Tabular::SET_USEBOX, "set-usebox" },
143 { Tabular::SET_LTHEAD, "set-lthead" },
144 { Tabular::UNSET_LTHEAD, "unset-lthead" },
145 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
146 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
147 { Tabular::SET_LTFOOT, "set-ltfoot" },
148 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
149 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
150 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
151 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
152 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
153 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
154 { Tabular::SET_BOOKTABS, "set-booktabs" },
155 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
156 { Tabular::SET_TOP_SPACE, "set-top-space" },
157 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
158 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
159 { Tabular::SET_BORDER_LINES, "set-border-lines" },
160 { Tabular::LAST_ACTION, "" }
164 class FeatureEqual : public unary_function<TabularFeature, bool> {
166 FeatureEqual(Tabular::Feature feature)
167 : feature_(feature) {}
168 bool operator()(TabularFeature const & tf) const {
169 return tf.action == feature_;
172 Tabular::Feature feature_;
177 string const write_attribute(string const & name, T const & t)
179 string const s = tostr(t);
180 return s.empty() ? s : " " + name + "=\"" + s + "\"";
184 string const write_attribute(string const & name, string const & t)
186 return t.empty() ? t : " " + name + "=\"" + t + "\"";
191 string const write_attribute(string const & name, docstring const & t)
193 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
198 string const write_attribute(string const & name, bool const & b)
200 // we write only true attribute values so we remove a bit of the
201 // file format bloat for tabulars.
202 return b ? write_attribute(name, convert<string>(b)) : string();
207 string const write_attribute(string const & name, int const & i)
209 // we write only true attribute values so we remove a bit of the
210 // file format bloat for tabulars.
211 return i ? write_attribute(name, convert<string>(i)) : string();
216 string const write_attribute(string const & name, Tabular::idx_type const & i)
218 // we write only true attribute values so we remove a bit of the
219 // file format bloat for tabulars.
220 return i ? write_attribute(name, convert<string>(i)) : string();
225 string const write_attribute(string const & name, Length const & value)
227 // we write only the value if we really have one same reson as above.
228 return value.zero() ? string() : write_attribute(name, value.asString());
232 string const tostr(LyXAlignment const & num)
237 case LYX_ALIGN_BLOCK:
241 case LYX_ALIGN_CENTER:
243 case LYX_ALIGN_RIGHT:
245 case LYX_ALIGN_LAYOUT:
247 case LYX_ALIGN_SPECIAL:
254 string const tostr(Tabular::VAlignment const & num)
257 case Tabular::LYX_VALIGN_TOP:
259 case Tabular::LYX_VALIGN_MIDDLE:
261 case Tabular::LYX_VALIGN_BOTTOM:
268 string const tostr(Tabular::BoxType const & num)
271 case Tabular::BOX_NONE:
273 case Tabular::BOX_PARBOX:
275 case Tabular::BOX_MINIPAGE:
282 // I would have liked a fromstr template a lot better. (Lgb)
283 bool string2type(string const str, LyXAlignment & num)
286 num = LYX_ALIGN_NONE;
287 else if (str == "block")
288 num = LYX_ALIGN_BLOCK;
289 else if (str == "left")
290 num = LYX_ALIGN_LEFT;
291 else if (str == "center")
292 num = LYX_ALIGN_CENTER;
293 else if (str == "right")
294 num = LYX_ALIGN_RIGHT;
301 bool string2type(string const str, Tabular::VAlignment & num)
304 num = Tabular::LYX_VALIGN_TOP;
305 else if (str == "middle" )
306 num = Tabular::LYX_VALIGN_MIDDLE;
307 else if (str == "bottom")
308 num = Tabular::LYX_VALIGN_BOTTOM;
315 bool string2type(string const str, Tabular::BoxType & num)
318 num = Tabular::BOX_NONE;
319 else if (str == "parbox")
320 num = Tabular::BOX_PARBOX;
321 else if (str == "minipage")
322 num = Tabular::BOX_MINIPAGE;
329 bool string2type(string const str, bool & num)
333 else if (str == "false")
341 bool getTokenValue(string const & str, char const * token, string & ret)
344 size_t token_length = strlen(token);
345 size_t pos = str.find(token);
347 if (pos == string::npos || pos + token_length + 1 >= str.length()
348 || str[pos + token_length] != '=')
350 pos += token_length + 1;
352 if (ch != '"' && ch != '\'') { // only read till next space
356 while (pos < str.length() - 1 && str[++pos] != ch)
363 bool getTokenValue(string const & str, char const * token, docstring & ret)
366 bool const success = getTokenValue(str, token, tmp);
367 ret = from_utf8(tmp);
372 bool getTokenValue(string const & str, char const * token, int & num)
376 if (!getTokenValue(str, token, tmp))
378 num = convert<int>(tmp);
383 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
386 return getTokenValue(str, token, tmp) && string2type(tmp, num);
390 bool getTokenValue(string const & str, char const * token,
391 Tabular::VAlignment & num)
394 return getTokenValue(str, token, tmp) && string2type(tmp, num);
398 bool getTokenValue(string const & str, char const * token,
399 Tabular::BoxType & num)
402 return getTokenValue(str, token, tmp) && string2type(tmp, num);
406 bool getTokenValue(string const & str, char const * token, bool & flag)
408 // set the flag always to false as this should be the default for bools
409 // not in the file-format.
412 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
416 bool getTokenValue(string const & str, char const * token, Length & len)
418 // set the length to be zero() as default as this it should be if not
419 // in the file format.
422 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
426 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
431 if (!getTokenValue(str, token, tmp))
433 if (tmp == "default") {
437 return isValidLength(tmp, &len);
441 void l_getline(istream & is, string & str)
444 while (str.empty()) {
446 if (!str.empty() && str[str.length() - 1] == '\r')
447 str.erase(str.length() - 1);
454 string const featureAsString(Tabular::Feature feature)
456 TabularFeature * end = tabularFeature +
457 sizeof(tabularFeature) / sizeof(TabularFeature);
458 TabularFeature * it = find_if(tabularFeature, end,
459 FeatureEqual(feature));
460 return (it == end) ? string() : it->feature;
465 /////////////////////////////////////////////////////////////////////
469 /////////////////////////////////////////////////////////////////////
472 Tabular::CellData::CellData(Buffer const & buf, Tabular const & table)
475 multicolumn(Tabular::CELL_NORMAL),
476 alignment(LYX_ALIGN_CENTER),
477 valignment(LYX_VALIGN_TOP),
484 inset(new InsetTableCell(buf, this, &table))
486 inset->setBuffer(const_cast<Buffer &>(buf));
487 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
491 Tabular::CellData::CellData(CellData const & cs)
494 multicolumn(cs.multicolumn),
495 alignment(cs.alignment),
496 valignment(cs.valignment),
497 top_line(cs.top_line),
498 bottom_line(cs.bottom_line),
499 left_line(cs.left_line),
500 right_line(cs.right_line),
503 align_special(cs.align_special),
505 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
507 inset->setCellData(this);
511 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
518 void Tabular::CellData::swap(CellData & rhs)
520 std::swap(cellno, rhs.cellno);
521 std::swap(width, rhs.width);
522 std::swap(multicolumn, rhs.multicolumn);
523 std::swap(alignment, rhs.alignment);
524 std::swap(valignment, rhs.valignment);
525 std::swap(top_line, rhs.top_line);
526 std::swap(bottom_line, rhs.bottom_line);
527 std::swap(left_line, rhs.left_line);
528 std::swap(right_line, rhs.right_line);
529 std::swap(usebox, rhs.usebox);
530 std::swap(rotate, rhs.rotate);
531 std::swap(align_special, rhs.align_special);
532 p_width.swap(rhs.p_width);
533 inset.swap(rhs.inset);
537 Tabular::RowData::RowData()
540 top_space_default(false),
541 bottom_space_default(false),
542 interline_space_default(false),
551 Tabular::ColumnData::ColumnData()
552 : alignment(LYX_ALIGN_CENTER),
553 valignment(LYX_VALIGN_TOP),
559 Tabular::ltType::ltType()
572 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
574 init(buffer, rows_arg, columns_arg);
578 // activates all lines and sets all widths to 0
579 void Tabular::init(Buffer const & buf, row_type rows_arg,
580 col_type columns_arg)
583 row_info = row_vector(rows_arg);
584 column_info = column_vector(columns_arg);
585 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf, *this)));
586 row_info.reserve(10);
587 column_info.reserve(10);
588 cell_info.reserve(100);
590 is_long_tabular = false;
592 use_booktabs = false;
593 // set silly default lines
594 for (row_type i = 0; i < rowCount(); ++i)
595 for (col_type j = 0; j < columnCount(); ++j) {
596 cell_info[i][j].top_line = true;
597 cell_info[i][j].left_line = true;
598 cell_info[i][j].bottom_line = i == 0 || i == rowCount() - 1;
599 cell_info[i][j].right_line = j == columnCount() - 1;
604 void Tabular::appendRow(idx_type const cell)
606 BufferParams const & bp = buffer().params();
607 row_type const row = cellRow(cell);
609 row_vector::iterator rit = row_info.begin() + row;
610 row_info.insert(rit, RowData());
611 // now set the values of the row before
612 row_info[row] = row_info[row + 1];
614 row_type const nrows = rowCount();
615 col_type const ncols = columnCount();
617 cell_vvector old(nrows - 1);
618 for (row_type i = 0; i < nrows - 1; ++i)
619 swap(cell_info[i], old[i]);
621 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer(), *this)));
623 for (row_type i = 0; i <= row; ++i)
624 swap(cell_info[i], old[i]);
625 for (row_type i = row + 2; i < nrows; ++i)
626 swap(cell_info[i], old[i - 1]);
629 for (col_type j = 0; j < ncols; ++j)
630 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
636 void Tabular::deleteRow(row_type const row)
638 // Not allowed to delete last row
642 row_info.erase(row_info.begin() + row);
643 cell_info.erase(cell_info.begin() + row);
648 void Tabular::copyRow(row_type const row)
650 row_info.insert(row_info.begin() + row, row_info[row]);
651 cell_info.insert(cell_info.begin() + row, cell_info[row]);
653 if (buffer().params().trackChanges)
654 for (col_type j = 0; j < columnCount(); ++j)
655 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
661 void Tabular::appendColumn(idx_type const cell)
663 col_type const column = cellColumn(cell);
664 column_vector::iterator cit = column_info.begin() + column + 1;
665 column_info.insert(cit, ColumnData());
666 col_type const ncols = columnCount();
667 // set the column values of the column before
668 column_info[column + 1] = column_info[column];
670 for (row_type i = 0; i < rowCount(); ++i) {
671 cell_info[i].insert(cell_info[i].begin() + column + 1, CellData(buffer(), *this));
672 col_type c = column + 2;
674 && cell_info[i][c].multicolumn == CELL_PART_OF_MULTICOLUMN) {
675 cell_info[i][c].multicolumn = CELL_NORMAL;
680 for (row_type i = 0; i < rowCount(); ++i) {
681 cell_info[i][column + 1].inset->clear();
682 if (buffer().params().trackChanges)
683 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
689 void Tabular::deleteColumn(col_type const column)
691 // Not allowed to delete last column
692 if (columnCount() == 1)
695 column_info.erase(column_info.begin() + column);
696 for (row_type i = 0; i < rowCount(); ++i) {
697 // Care about multicolumn cells
698 if (column + 1 < columnCount() &&
699 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
700 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
701 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
703 cell_info[i].erase(cell_info[i].begin() + column);
709 void Tabular::copyColumn(col_type const column)
711 BufferParams const & bp = buffer().params();
712 column_info.insert(column_info.begin() + column, column_info[column]);
714 for (row_type i = 0; i < rowCount(); ++i)
715 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
718 for (row_type i = 0; i < rowCount(); ++i)
719 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
724 void Tabular::updateIndexes()
726 col_type ncols = columnCount();
727 row_type nrows = rowCount();
729 for (row_type row = 0; row < nrows; ++row)
730 for (col_type column = 0; column < ncols; ++column) {
731 if (!isPartOfMultiColumn(row, column))
733 cell_info[row][column].cellno = numberofcells - 1;
736 rowofcell.resize(numberofcells);
737 columnofcell.resize(numberofcells);
739 for (row_type row = 0; row < nrows; ++row)
740 for (col_type column = 0; column < ncols; ++column) {
741 if (isPartOfMultiColumn(row, column))
744 columnofcell[i] = column;
750 Tabular::idx_type Tabular::cellCount() const
752 return numberofcells;
756 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
758 row_type const row = cellRow(cell);
760 for (col_type i = 0; i < columnCount(); ++i)
761 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
767 bool Tabular::topLine(idx_type const cell) const
769 return cellinfo_of_cell(cell).top_line;
773 bool Tabular::bottomLine(idx_type const cell) const
775 return cellinfo_of_cell(cell).bottom_line;
779 bool Tabular::leftLine(idx_type cell) const
783 return cellinfo_of_cell(cell).left_line;
787 bool Tabular::rightLine(idx_type cell) const
791 return cellinfo_of_cell(cell).right_line;
795 bool Tabular::topAlreadyDrawn(idx_type cell) const
797 row_type row = cellRow(cell);
800 idx_type i = cellIndex(row - 1, cellColumn(cell));
801 return !rowBottomLine(row - 1) && bottomLine(i);
805 bool Tabular::leftAlreadyDrawn(idx_type cell) const
807 col_type col = cellColumn(cell);
810 idx_type i = cellIndex(cellRow(cell), col - 1);
811 return !columnRightLine(col - 1) && rightLine(i);
815 bool Tabular::isLastRow(idx_type cell) const
817 return cellRow(cell) == rowCount() - 1;
821 int Tabular::getAdditionalHeight(row_type row) const
823 if (!row || row >= rowCount())
826 int const interline_space = row_info[row - 1].interline_space_default ?
828 row_info[row - 1].interline_space.inPixels(width());
829 if (rowTopLine(row) && rowBottomLine(row - 1))
830 return interline_space + WIDTH_OF_LINE;
831 return interline_space;
835 int Tabular::getAdditionalWidth(idx_type cell) const
837 // internally already set in setCellWidth
838 // used to get it back in text.cpp
839 col_type c = cellColumn(cell);
840 if (c < columnCount() - 1
841 && columnRightLine(c) && columnLeftLine(c + 1)
842 && cellinfo_of_cell(cell).multicolumn == CELL_NORMAL)
843 return WIDTH_OF_LINE;
848 int Tabular::columnWidth(idx_type cell) const
851 col_type const span = columnSpan(cell);
852 col_type const col = cellColumn(cell);
853 for(col_type c = col; c < col + span ; ++c)
854 w += column_info[c].width;
859 bool Tabular::updateColumnWidths()
861 col_type const ncols = columnCount();
862 row_type const nrows = rowCount();
864 // for each col get max of single col cells
865 for(col_type c = 0; c < ncols; ++c) {
867 for(row_type r = 0; r < nrows; ++r) {
868 idx_type const i = cellIndex(r, c);
869 if (columnSpan(i) == 1)
870 new_width = max(new_width, cellinfo_of_cell(i).width);
873 if (column_info[c].width != new_width) {
874 column_info[c].width = new_width;
878 // update col widths to fit merged cells
879 for(col_type c = 0; c < ncols; ++c)
880 for(row_type r = 0; r < nrows; ++r) {
881 idx_type const i = cellIndex(r, c);
882 int const span = columnSpan(i);
883 if (span == 1 || c > cellColumn(i))
887 for(col_type j = c; j < c + span ; ++j)
888 old_width += column_info[j].width;
890 if (cellinfo_of_cell(i).width > old_width) {
891 column_info[c + span - 1].width += cellinfo_of_cell(i).width - old_width;
900 int Tabular::width() const
902 col_type const ncols = columnCount();
904 for (col_type i = 0; i < ncols; ++i)
905 width += column_info[i].width;
910 void Tabular::setCellWidth(idx_type cell, int new_width)
912 col_type const col = cellColumn(cell);
914 if (col < columnCount() - 1 && columnRightLine(col) &&
915 columnLeftLine(col + 1)) {
916 add_width = WIDTH_OF_LINE;
918 cellinfo_of_cell(cell).width = new_width + 2 * WIDTH_OF_LINE + add_width;
923 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
926 if (!isMultiColumn(cell) || onlycolumn)
927 column_info[cellColumn(cell)].alignment = align;
929 cellinfo_of_cell(cell).alignment = align;
933 void Tabular::setVAlignment(idx_type cell, VAlignment align,
936 if (!isMultiColumn(cell) || onlycolumn)
937 column_info[cellColumn(cell)].valignment = align;
939 cellinfo_of_cell(cell).valignment = align;
946 * Allow line and paragraph breaks for fixed width cells or disallow them,
947 * merge cell paragraphs and reset layout to standard for variable width
950 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
952 inset->setAutoBreakRows(fixedWidth);
956 // merge all paragraphs to one
957 BufferParams const & bp = cur.bv().buffer().params();
958 while (inset->paragraphs().size() > 1)
959 mergeParagraph(bp, inset->paragraphs(), 0);
963 // undo information has already been recorded
964 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
965 bp.documentClass().emptyLayoutName());
972 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
973 Length const & width)
975 col_type const j = cellColumn(cell);
977 column_info[j].p_width = width;
978 for (row_type i = 0; i < rowCount(); ++i) {
979 idx_type const cell = cellIndex(i, j);
980 // because of multicolumns
981 toggleFixedWidth(cur, getCellInset(cell).get(),
982 !getPWidth(cell).zero());
984 // cur paragraph can become invalid after paragraphs were merged
985 if (cur.pit() > cur.lastpit())
986 cur.pit() = cur.lastpit();
987 // cur position can become invalid after newlines were removed
988 if (cur.pos() > cur.lastpos())
989 cur.pos() = cur.lastpos();
993 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
994 Length const & width)
996 if (!isMultiColumn(cell))
999 cellinfo_of_cell(cell).p_width = width;
1000 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1001 // cur paragraph can become invalid after paragraphs were merged
1002 if (cur.pit() > cur.lastpit())
1003 cur.pit() = cur.lastpit();
1004 // cur position can become invalid after newlines were removed
1005 if (cur.pos() > cur.lastpos())
1006 cur.pos() = cur.lastpos();
1011 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1012 Tabular::Feature what)
1014 if (what == SET_SPECIAL_MULTI)
1015 cellinfo_of_cell(cell).align_special = special;
1017 column_info[cellColumn(cell)].align_special = special;
1021 void Tabular::setAllLines(idx_type cell, bool line)
1023 setTopLine(cell, line);
1024 setBottomLine(cell, line);
1025 setRightLine(cell, line);
1026 setLeftLine(cell, line);
1030 void Tabular::setTopLine(idx_type i, bool line)
1032 cellinfo_of_cell(i).top_line = line;
1036 void Tabular::setBottomLine(idx_type i, bool line)
1038 cellinfo_of_cell(i).bottom_line = line;
1042 void Tabular::setLeftLine(idx_type cell, bool line)
1044 cellinfo_of_cell(cell).left_line = line;
1048 void Tabular::setRightLine(idx_type cell, bool line)
1050 cellinfo_of_cell(cell).right_line = line;
1053 bool Tabular::rowTopLine(row_type r) const
1055 idx_type i0 = getFirstCellInRow(r);
1056 idx_type i1 = getLastCellInRow(r);
1057 bool all_rows_set = true;
1058 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1059 all_rows_set = cellinfo_of_cell(j).top_line;
1060 return all_rows_set;
1064 bool Tabular::rowBottomLine(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_of_cell(j).bottom_line;
1071 return all_rows_set;
1075 bool Tabular::columnLeftLine(col_type c) const
1077 bool all_cols_set = true;
1078 row_type nrows = rowCount();
1079 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1080 idx_type i = cellIndex(r, c);
1081 if (columnSpan(i) == 1)
1082 all_cols_set = cellinfo_of_cell(i).left_line;
1084 return all_cols_set;
1088 bool Tabular::columnRightLine(col_type c) const
1090 bool all_cols_set = true;
1091 row_type nrows = rowCount();
1092 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1093 idx_type i = cellIndex(r, c);
1094 if (c == cellColumn(i) + columnSpan(i) - 1)
1095 all_cols_set = cellinfo_of_cell(i).right_line;
1097 return all_cols_set;
1101 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1103 if (!onlycolumn && isMultiColumn(cell))
1104 return cellinfo_of_cell(cell).alignment;
1105 return column_info[cellColumn(cell)].alignment;
1110 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1112 if (!onlycolumn && isMultiColumn(cell))
1113 return cellinfo_of_cell(cell).valignment;
1114 return column_info[cellColumn(cell)].valignment;
1118 Length const Tabular::getPWidth(idx_type cell) const
1120 if (isMultiColumn(cell))
1121 return cellinfo_of_cell(cell).p_width;
1122 return column_info[cellColumn(cell)].p_width;
1126 Length const Tabular::getColumnPWidth(idx_type cell) const
1128 return column_info[cellColumn(cell)].p_width;
1132 Length const Tabular::getMColumnPWidth(idx_type cell) const
1134 if (isMultiColumn(cell))
1135 return cellinfo_of_cell(cell).p_width;
1140 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1142 if (what == SET_SPECIAL_MULTI)
1143 return cellinfo_of_cell(cell).align_special;
1144 return column_info[cellColumn(cell)].align_special;
1148 int Tabular::cellWidth(idx_type cell) const
1150 return cellinfo_of_cell(cell).width;
1154 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1158 switch (getAlignment(cell)) {
1159 case LYX_ALIGN_CENTER:
1160 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1162 case LYX_ALIGN_RIGHT:
1163 x += columnWidth(cell) - cellWidth(cell);
1164 // + getAdditionalWidth(cell);
1167 // LYX_ALIGN_LEFT: nothing :-)
1171 // the LaTeX Way :-(
1177 bool Tabular::isFirstCellInRow(idx_type cell) const
1179 return cellColumn(cell) == 0;
1183 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1185 if (row > rowCount() - 1)
1186 row = rowCount() - 1;
1187 return cell_info[row][0].cellno;
1191 bool Tabular::isLastCellInRow(idx_type cell) const
1193 return cellRightColumn(cell) == columnCount() - 1;
1197 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1199 if (row > rowCount() - 1)
1200 row = rowCount() - 1;
1201 return cell_info[row][columnCount() - 1].cellno;
1205 Tabular::row_type Tabular::cellRow(idx_type cell) const
1207 if (cell >= cellCount())
1208 return rowCount() - 1;
1211 return rowofcell[cell];
1215 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1217 if (cell >= cellCount())
1218 return columnCount() - 1;
1221 return columnofcell[cell];
1225 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1227 row_type const row = cellRow(cell);
1228 col_type column = cellColumn(cell);
1229 while (column < columnCount() - 1 &&
1230 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1236 void Tabular::write(ostream & os) const
1240 << write_attribute("version", 3)
1241 << write_attribute("rows", rowCount())
1242 << write_attribute("columns", columnCount())
1244 // global longtable options
1246 << write_attribute("rotate", rotate)
1247 << write_attribute("booktabs", use_booktabs)
1248 << write_attribute("islongtable", is_long_tabular)
1249 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1250 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1251 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1252 << write_attribute("headTopDL", endhead.topDL)
1253 << write_attribute("headBottomDL", endhead.bottomDL)
1254 << write_attribute("footTopDL", endfoot.topDL)
1255 << write_attribute("footBottomDL", endfoot.bottomDL)
1256 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1257 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1258 << write_attribute("lastFootEmpty", endlastfoot.empty)
1260 for (col_type j = 0; j < columnCount(); ++j) {
1262 << write_attribute("alignment", column_info[j].alignment)
1263 << write_attribute("valignment", column_info[j].valignment)
1264 << write_attribute("width", column_info[j].p_width.asString())
1265 << write_attribute("special", column_info[j].align_special)
1268 for (row_type i = 0; i < rowCount(); ++i) {
1269 static const string def("default");
1271 if (row_info[i].top_space_default)
1272 os << write_attribute("topspace", def);
1274 os << write_attribute("topspace", row_info[i].top_space);
1275 if (row_info[i].bottom_space_default)
1276 os << write_attribute("bottomspace", def);
1278 os << write_attribute("bottomspace", row_info[i].bottom_space);
1279 if (row_info[i].interline_space_default)
1280 os << write_attribute("interlinespace", def);
1282 os << write_attribute("interlinespace", row_info[i].interline_space);
1283 os << write_attribute("endhead", row_info[i].endhead)
1284 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1285 << write_attribute("endfoot", row_info[i].endfoot)
1286 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1287 << write_attribute("newpage", row_info[i].newpage)
1289 for (col_type j = 0; j < columnCount(); ++j) {
1291 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1292 << write_attribute("alignment", cell_info[i][j].alignment)
1293 << write_attribute("valignment", cell_info[i][j].valignment)
1294 << write_attribute("topline", cell_info[i][j].top_line)
1295 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1296 << write_attribute("leftline", cell_info[i][j].left_line)
1297 << write_attribute("rightline", cell_info[i][j].right_line)
1298 << write_attribute("rotate", cell_info[i][j].rotate)
1299 << write_attribute("usebox", cell_info[i][j].usebox)
1300 << write_attribute("width", cell_info[i][j].p_width)
1301 << write_attribute("special", cell_info[i][j].align_special)
1303 os << "\\begin_inset ";
1304 cell_info[i][j].inset->write(os);
1305 os << "\n\\end_inset\n"
1310 os << "</lyxtabular>\n";
1314 void Tabular::read(Lexer & lex)
1317 istream & is = lex.getStream();
1319 l_getline(is, line);
1320 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1321 BOOST_ASSERT(false);
1326 if (!getTokenValue(line, "version", version))
1328 BOOST_ASSERT(version >= 2);
1331 if (!getTokenValue(line, "rows", rows_arg))
1334 if (!getTokenValue(line, "columns", columns_arg))
1336 init(buffer(), rows_arg, columns_arg);
1337 l_getline(is, line);
1338 if (!prefixIs(line, "<features")) {
1339 lyxerr << "Wrong tabular format (expected <features ...> got"
1340 << line << ')' << endl;
1343 getTokenValue(line, "rotate", rotate);
1344 getTokenValue(line, "booktabs", use_booktabs);
1345 getTokenValue(line, "islongtable", is_long_tabular);
1346 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1347 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1348 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1349 getTokenValue(line, "headTopDL", endhead.topDL);
1350 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1351 getTokenValue(line, "footTopDL", endfoot.topDL);
1352 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1353 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1354 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1355 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1357 for (col_type j = 0; j < columnCount(); ++j) {
1359 if (!prefixIs(line,"<column")) {
1360 lyxerr << "Wrong tabular format (expected <column ...> got"
1361 << line << ')' << endl;
1364 getTokenValue(line, "alignment", column_info[j].alignment);
1365 getTokenValue(line, "valignment", column_info[j].valignment);
1366 getTokenValue(line, "width", column_info[j].p_width);
1367 getTokenValue(line, "special", column_info[j].align_special);
1370 for (row_type i = 0; i < rowCount(); ++i) {
1371 l_getline(is, line);
1372 if (!prefixIs(line, "<row")) {
1373 lyxerr << "Wrong tabular format (expected <row ...> got"
1374 << line << ')' << endl;
1377 getTokenValue(line, "topspace", row_info[i].top_space,
1378 row_info[i].top_space_default);
1379 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1380 row_info[i].bottom_space_default);
1381 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1382 row_info[i].interline_space_default);
1383 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1384 getTokenValue(line, "endhead", row_info[i].endhead);
1385 getTokenValue(line, "endfoot", row_info[i].endfoot);
1386 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1387 getTokenValue(line, "newpage", row_info[i].newpage);
1388 for (col_type j = 0; j < columnCount(); ++j) {
1389 l_getline(is, line);
1390 if (!prefixIs(line, "<cell")) {
1391 lyxerr << "Wrong tabular format (expected <cell ...> got"
1392 << line << ')' << endl;
1395 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1396 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1397 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1398 getTokenValue(line, "topline", cell_info[i][j].top_line);
1399 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1400 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1401 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1402 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1403 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1404 getTokenValue(line, "width", cell_info[i][j].p_width);
1405 getTokenValue(line, "special", cell_info[i][j].align_special);
1406 l_getline(is, line);
1407 if (prefixIs(line, "\\begin_inset")) {
1408 cell_info[i][j].inset->read(lex);
1409 l_getline(is, line);
1411 if (!prefixIs(line, "</cell>")) {
1412 lyxerr << "Wrong tabular format (expected </cell> got"
1413 << line << ')' << endl;
1417 l_getline(is, line);
1418 if (!prefixIs(line, "</row>")) {
1419 lyxerr << "Wrong tabular format (expected </row> got"
1420 << line << ')' << endl;
1424 while (!prefixIs(line, "</lyxtabular>")) {
1425 l_getline(is, line);
1431 bool Tabular::isMultiColumn(idx_type cell) const
1433 return cellinfo_of_cell(cell).multicolumn != CELL_NORMAL;
1437 bool Tabular::isMultiColumnReal(idx_type cell) const
1439 return cellColumn(cell) != cellRightColumn(cell) &&
1440 cellinfo_of_cell(cell).multicolumn != CELL_NORMAL;
1444 Tabular::CellData & Tabular::cellinfo_of_cell(idx_type cell) const
1446 return cell_info[cellRow(cell)][cellColumn(cell)];
1450 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1452 CellData & cs = cellinfo_of_cell(cell);
1453 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1454 cs.alignment = column_info[cellColumn(cell)].alignment;
1455 for (idx_type i = 1; i < number; ++i) {
1456 CellData & cs1 = cellinfo_of_cell(cell + i);
1457 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1458 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1461 setRightLine(cell, rightLine(cell + number - 1));
1466 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1468 row_type const row = cellRow(cell);
1469 col_type const ncols = columnCount();
1470 idx_type result = 1;
1471 col_type column = cellColumn(cell) + 1;
1472 while (column < ncols && isPartOfMultiColumn(row, column)) {
1480 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1482 row_type const row = cellRow(cell);
1483 col_type column = cellColumn(cell);
1485 idx_type result = 0;
1487 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1488 cell_info[row][column].multicolumn = CELL_NORMAL;
1490 while (column < columnCount() &&
1491 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1493 cell_info[row][column].multicolumn = CELL_NORMAL;
1503 void Tabular::setBookTabs(bool what)
1505 use_booktabs = what;
1509 bool Tabular::useBookTabs() const
1511 return use_booktabs;
1515 void Tabular::setLongTabular(bool what)
1517 is_long_tabular = what;
1521 bool Tabular::isLongTabular() const
1523 return is_long_tabular;
1527 void Tabular::setRotateTabular(bool flag)
1533 bool Tabular::getRotateTabular() const
1539 void Tabular::setRotateCell(idx_type cell, bool flag)
1541 cellinfo_of_cell(cell).rotate = flag;
1545 bool Tabular::getRotateCell(idx_type cell) const
1547 return cellinfo_of_cell(cell).rotate;
1551 bool Tabular::needRotating() const
1555 for (row_type i = 0; i < rowCount(); ++i)
1556 for (col_type j = 0; j < columnCount(); ++j)
1557 if (cell_info[i][j].rotate)
1563 bool Tabular::isLastCell(idx_type cell) const
1565 if (cell + 1 < cellCount())
1571 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1573 if (cellRow(cell) > 0)
1574 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1579 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1581 if (cellRow(cell) + 1 < rowCount())
1582 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1587 Tabular::idx_type Tabular::cellIndex(row_type row,
1588 col_type column) const
1590 BOOST_ASSERT(column != npos && column < columnCount() &&
1591 row != npos && row < rowCount());
1592 return cell_info[row][column].cellno;
1596 void Tabular::setUsebox(idx_type cell, BoxType type)
1598 cellinfo_of_cell(cell).usebox = type;
1602 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1604 if (column_info[cellColumn(cell)].p_width.zero() &&
1605 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1607 if (cellinfo_of_cell(cell).usebox > 1)
1608 return cellinfo_of_cell(cell).usebox;
1609 return useParbox(cell);
1614 // This are functions used for the longtable support
1616 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1622 row_info[row].endfirsthead = flag;
1626 row_info[row].endhead = flag;
1631 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1634 hd.set = haveLTHead();
1635 return row_info[row].endhead;
1639 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1642 hd.set = haveLTFirstHead();
1643 return row_info[row].endfirsthead;
1647 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1653 row_info[row].endlastfoot = flag;
1657 row_info[row].endfoot = flag;
1662 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1665 fd.set = haveLTFoot();
1666 return row_info[row].endfoot;
1670 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1673 fd.set = haveLTLastFoot();
1674 return row_info[row].endlastfoot;
1678 void Tabular::setLTNewPage(row_type row, bool what)
1680 row_info[row].newpage = what;
1684 bool Tabular::getLTNewPage(row_type row) const
1686 return row_info[row].newpage;
1690 bool Tabular::haveLTHead() const
1692 for (row_type i = 0; i < rowCount(); ++i)
1693 if (row_info[i].endhead)
1699 bool Tabular::haveLTFirstHead() const
1701 if (endfirsthead.empty)
1703 for (row_type i = 0; i < rowCount(); ++i)
1704 if (row_info[i].endfirsthead)
1710 bool Tabular::haveLTFoot() const
1712 for (row_type i = 0; i < rowCount(); ++i)
1713 if (row_info[i].endfoot)
1719 bool Tabular::haveLTLastFoot() const
1721 if (endlastfoot.empty)
1723 for (row_type i = 0; i < rowCount(); ++i)
1724 if (row_info[i].endlastfoot)
1730 // end longtable support functions
1732 void Tabular::setRowAscent(row_type row, int height)
1734 if (row >= rowCount() || row_info[row].ascent == height)
1736 row_info[row].ascent = height;
1740 void Tabular::setRowDescent(row_type row, int height)
1742 if (row >= rowCount() || row_info[row].descent == height)
1744 row_info[row].descent = height;
1748 int Tabular::rowAscent(row_type row) const
1750 if (row >= rowCount())
1752 return row_info[row].ascent;
1756 int Tabular::rowDescent(row_type row) const
1758 BOOST_ASSERT(row < rowCount());
1759 return row_info[row].descent;
1763 int Tabular::height() const
1766 for (row_type row = 0; row < rowCount(); ++row)
1767 height += rowAscent(row) + rowDescent(row) +
1768 getAdditionalHeight(row);
1773 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1775 BOOST_ASSERT(row < rowCount());
1776 BOOST_ASSERT(column < columnCount());
1777 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1781 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1783 // FIXME: assert or return 0 as in TeXBottomHLine()?
1784 BOOST_ASSERT(row != npos);
1785 BOOST_ASSERT(row < rowCount());
1787 idx_type const fcell = getFirstCellInRow(row);
1788 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1791 for (idx_type i = fcell; i < n; ++i) {
1795 if (use_booktabs && row == 0) {
1798 } else if (tmp == n - fcell) {
1799 os << (use_booktabs ? "\\midrule " : "\\hline ");
1801 for (idx_type i = fcell; i < n; ++i) {
1803 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1804 << cellColumn(i) + 1
1806 << cellRightColumn(i) + 1
1818 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1820 // FIXME: return 0 or assert as in TeXTopHLine()?
1821 if (row == npos || row >= rowCount())
1824 idx_type const fcell = getFirstCellInRow(row);
1825 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1828 for (idx_type i = fcell; i < n; ++i) {
1832 if (use_booktabs && row == rowCount() - 1) {
1833 if (bottomLine(fcell))
1834 os << "\\bottomrule";
1835 } else if (tmp == n - fcell) {
1836 os << (use_booktabs ? "\\midrule" : "\\hline");
1838 for (idx_type i = fcell; i < n; ++i) {
1839 if (bottomLine(i)) {
1840 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1841 << cellColumn(i) + 1
1843 << cellRightColumn(i) + 1
1855 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
1859 if (getRotateCell(cell)) {
1860 os << "\\begin{sideways}\n";
1863 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1864 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1865 col_type c = cellColumn(cell);
1866 if (isMultiColumn(cell)
1867 || (leftLine(cell) && !columnLeftLine(c))
1868 || (rightLine(cell) && !columnRightLine(c))) {
1869 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1872 if (!cellinfo_of_cell(cell).align_special.empty()) {
1873 os << cellinfo_of_cell(cell).align_special;
1875 if (!getPWidth(cell).zero()) {
1877 case LYX_VALIGN_TOP:
1880 case LYX_VALIGN_MIDDLE:
1883 case LYX_VALIGN_BOTTOM:
1888 << from_ascii(getPWidth(cell).asLatexString())
1892 case LYX_ALIGN_LEFT:
1895 case LYX_ALIGN_RIGHT:
1902 } // end if else !getPWidth
1903 } // end if else !cellinfo_of_cell
1904 if (rightLine(cell))
1908 if (getUsebox(cell) == BOX_PARBOX) {
1911 case LYX_VALIGN_TOP:
1914 case LYX_VALIGN_MIDDLE:
1917 case LYX_VALIGN_BOTTOM:
1921 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1923 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1924 os << "\\begin{minipage}[";
1926 case LYX_VALIGN_TOP:
1929 case LYX_VALIGN_MIDDLE:
1932 case LYX_VALIGN_BOTTOM:
1936 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1944 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
1949 if (getUsebox(cell) == BOX_PARBOX)
1951 else if (getUsebox(cell) == BOX_MINIPAGE) {
1952 os << "%\n\\end{minipage}";
1955 col_type c = cellColumn(cell);
1956 if (isMultiColumn(cell)
1957 || (leftLine(cell) && !columnLeftLine(c))
1958 || (rightLine(cell) && !columnRightLine(c))) {
1961 if (getRotateCell(cell)) {
1962 os << "%\n\\end{sideways}";
1969 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1970 OutputParams const & runparams) const
1972 if (!is_long_tabular)
1976 // output header info
1978 if (endhead.topDL) {
1982 for (row_type i = 0; i < rowCount(); ++i) {
1983 if (row_info[i].endhead) {
1984 ret += TeXRow(os, i, runparams);
1987 if (endhead.bottomDL) {
1991 os << "\\endhead\n";
1993 if (endfirsthead.empty) {
1994 os << "\\endfirsthead\n";
1998 // output firstheader info
1999 if (haveLTFirstHead()) {
2000 if (endfirsthead.topDL) {
2004 for (row_type i = 0; i < rowCount(); ++i) {
2005 if (row_info[i].endfirsthead) {
2006 ret += TeXRow(os, i, runparams);
2009 if (endfirsthead.bottomDL) {
2013 os << "\\endfirsthead\n";
2016 // output footer info
2018 if (endfoot.topDL) {
2022 for (row_type i = 0; i < rowCount(); ++i) {
2023 if (row_info[i].endfoot) {
2024 ret += TeXRow(os, i, runparams);
2027 if (endfoot.bottomDL) {
2031 os << "\\endfoot\n";
2033 if (endlastfoot.empty) {
2034 os << "\\endlastfoot\n";
2038 // output lastfooter info
2039 if (haveLTLastFoot()) {
2040 if (endlastfoot.topDL) {
2044 for (row_type i = 0; i < rowCount(); ++i) {
2045 if (row_info[i].endlastfoot) {
2046 ret += TeXRow(os, i, runparams);
2049 if (endlastfoot.bottomDL) {
2053 os << "\\endlastfoot\n";
2060 bool Tabular::isValidRow(row_type row) const
2062 if (!is_long_tabular)
2064 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2065 !row_info[row].endfoot && !row_info[row].endlastfoot;
2069 int Tabular::TeXRow(odocstream & os, row_type i,
2070 OutputParams const & runparams) const
2072 idx_type cell = cellIndex(i, 0);
2073 int ret = TeXTopHLine(os, i);
2074 if (row_info[i].top_space_default) {
2076 os << "\\addlinespace\n";
2078 os << "\\noalign{\\vskip\\doublerulesep}\n";
2080 } else if(!row_info[i].top_space.zero()) {
2082 os << "\\addlinespace["
2083 << from_ascii(row_info[i].top_space.asLatexString())
2086 os << "\\noalign{\\vskip"
2087 << from_ascii(row_info[i].top_space.asLatexString())
2093 for (col_type j = 0; j < columnCount(); ++j) {
2094 if (isPartOfMultiColumn(i, j))
2096 ret += TeXCellPreamble(os, cell);
2097 shared_ptr<InsetTableCell> inset = getCellInset(cell);
2099 Paragraph const & par = inset->paragraphs().front();
2100 bool rtl = par.isRTL(buffer().params())
2102 && getPWidth(cell).zero();
2105 if (par.getParLanguage(buffer().params())->lang() ==
2108 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2110 // currently, remaning RTL languages are arabic_arabtex and hebrew
2114 ret += inset->latex(os, runparams);
2118 ret += TeXCellPostamble(os, cell);
2119 if (!isLastCellInRow(cell)) { // not last cell in row
2124 os << "\\tabularnewline";
2125 if (row_info[i].bottom_space_default) {
2127 os << "\\addlinespace";
2129 os << "[\\doublerulesep]";
2130 } else if (!row_info[i].bottom_space.zero()) {
2132 os << "\\addlinespace";
2134 << from_ascii(row_info[i].bottom_space.asLatexString())
2139 ret += TeXBottomHLine(os, i);
2140 if (row_info[i].interline_space_default) {
2142 os << "\\addlinespace\n";
2144 os << "\\noalign{\\vskip\\doublerulesep}\n";
2146 } else if (!row_info[i].interline_space.zero()) {
2148 os << "\\addlinespace["
2149 << from_ascii(row_info[i].interline_space.asLatexString())
2152 os << "\\noalign{\\vskip"
2153 << from_ascii(row_info[i].interline_space.asLatexString())
2161 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2165 //+---------------------------------------------------------------------
2166 //+ first the opening preamble +
2167 //+---------------------------------------------------------------------
2170 os << "\\begin{sideways}\n";
2173 if (is_long_tabular)
2174 os << "\\begin{longtable}{";
2176 os << "\\begin{tabular}{";
2178 for (col_type i = 0; i < columnCount(); ++i) {
2179 if (!use_booktabs && columnLeftLine(i))
2181 if (!column_info[i].align_special.empty()) {
2182 os << column_info[i].align_special;
2184 if (!column_info[i].p_width.zero()) {
2185 switch (column_info[i].alignment) {
2186 case LYX_ALIGN_LEFT:
2187 os << ">{\\raggedright}";
2189 case LYX_ALIGN_RIGHT:
2190 os << ">{\\raggedleft}";
2192 case LYX_ALIGN_CENTER:
2193 os << ">{\\centering}";
2195 case LYX_ALIGN_NONE:
2196 case LYX_ALIGN_BLOCK:
2197 case LYX_ALIGN_LAYOUT:
2198 case LYX_ALIGN_SPECIAL:
2202 switch (column_info[i].valignment) {
2203 case LYX_VALIGN_TOP:
2206 case LYX_VALIGN_MIDDLE:
2209 case LYX_VALIGN_BOTTOM:
2214 << from_ascii(column_info[i].p_width.asLatexString())
2217 switch (column_info[i].alignment) {
2218 case LYX_ALIGN_LEFT:
2221 case LYX_ALIGN_RIGHT:
2228 } // end if else !column_info[i].p_width
2229 } // end if else !column_info[i].align_special
2230 if (!use_booktabs && columnRightLine(i))
2236 ret += TeXLongtableHeaderFooter(os, runparams);
2238 //+---------------------------------------------------------------------
2239 //+ the single row and columns (cells) +
2240 //+---------------------------------------------------------------------
2242 for (row_type i = 0; i < rowCount(); ++i) {
2243 if (isValidRow(i)) {
2244 ret += TeXRow(os, i, runparams);
2245 if (is_long_tabular && row_info[i].newpage) {
2246 os << "\\newpage\n";
2252 //+---------------------------------------------------------------------
2253 //+ the closing of the tabular +
2254 //+---------------------------------------------------------------------
2256 if (is_long_tabular)
2257 os << "\\end{longtable}";
2259 os << "\\end{tabular}";
2261 os << "\n\\end{sideways}";
2269 int Tabular::docbookRow(odocstream & os, row_type row,
2270 OutputParams const & runparams) const
2273 idx_type cell = getFirstCellInRow(row);
2276 for (col_type j = 0; j < columnCount(); ++j) {
2277 if (isPartOfMultiColumn(row, j))
2280 os << "<entry align=\"";
2281 switch (getAlignment(cell)) {
2282 case LYX_ALIGN_LEFT:
2285 case LYX_ALIGN_RIGHT:
2293 os << "\" valign=\"";
2294 switch (getVAlignment(cell)) {
2295 case LYX_VALIGN_TOP:
2298 case LYX_VALIGN_BOTTOM:
2301 case LYX_VALIGN_MIDDLE:
2306 if (isMultiColumn(cell)) {
2307 os << " namest=\"col" << j << "\" ";
2308 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2312 ret += getCellInset(cell)->docbook(os, runparams);
2321 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2325 //+---------------------------------------------------------------------
2326 //+ first the opening preamble +
2327 //+---------------------------------------------------------------------
2329 os << "<tgroup cols=\"" << columnCount()
2330 << "\" colsep=\"1\" rowsep=\"1\">\n";
2332 for (col_type i = 0; i < columnCount(); ++i) {
2333 os << "<colspec colname=\"col" << i << "\" align=\"";
2334 switch (column_info[i].alignment) {
2335 case LYX_ALIGN_LEFT:
2338 case LYX_ALIGN_RIGHT:
2346 if (runparams.flavor == OutputParams::XML)
2352 //+---------------------------------------------------------------------
2353 //+ Long Tabular case +
2354 //+---------------------------------------------------------------------
2356 // output header info
2357 if (haveLTHead() || haveLTFirstHead()) {
2360 for (row_type i = 0; i < rowCount(); ++i) {
2361 if (row_info[i].endhead || row_info[i].endfirsthead) {
2362 ret += docbookRow(os, i, runparams);
2368 // output footer info
2369 if (haveLTFoot() || haveLTLastFoot()) {
2372 for (row_type i = 0; i < rowCount(); ++i) {
2373 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2374 ret += docbookRow(os, i, runparams);
2381 //+---------------------------------------------------------------------
2382 //+ the single row and columns (cells) +
2383 //+---------------------------------------------------------------------
2387 for (row_type i = 0; i < rowCount(); ++i) {
2388 if (isValidRow(i)) {
2389 ret += docbookRow(os, i, runparams);
2394 //+---------------------------------------------------------------------
2395 //+ the closing of the tabular +
2396 //+---------------------------------------------------------------------
2405 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2406 vector<unsigned int> const & clen) const
2408 idx_type const fcell = getFirstCellInRow(row);
2409 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2412 for (idx_type i = fcell; i < n; ++i) {
2422 for (idx_type i = fcell; i < n; ++i) {
2433 col_type column = cellColumn(i);
2434 int len = clen[column];
2435 while (column < columnCount() - 1
2436 && isPartOfMultiColumn(row, ++column))
2437 len += clen[column] + 4;
2438 os << docstring(len, ch);
2453 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2454 vector<unsigned int> const & clen) const
2456 idx_type const fcell = getFirstCellInRow(row);
2457 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2460 for (idx_type i = fcell; i < n; ++i) {
2461 if (bottomLine(i)) {
2470 for (idx_type i = fcell; i < n; ++i) {
2471 if (bottomLine(i)) {
2481 col_type column = cellColumn(i);
2482 int len = clen[column];
2483 while (column < columnCount() -1
2484 && isPartOfMultiColumn(row, ++column))
2485 len += clen[column] + 4;
2486 os << docstring(len, ch);
2487 if (bottomLine(i)) {
2501 void Tabular::plaintextPrintCell(odocstream & os,
2502 OutputParams const & runparams,
2503 idx_type cell, row_type row, col_type column,
2504 vector<unsigned int> const & clen,
2505 bool onlydata) const
2507 odocstringstream sstr;
2508 getCellInset(cell)->plaintext(sstr, runparams);
2520 unsigned int len1 = sstr.str().length();
2521 unsigned int len2 = clen[column];
2522 while (column < columnCount() -1
2523 && isPartOfMultiColumn(row, ++column))
2524 len2 += clen[column] + 4;
2527 switch (getAlignment(cell)) {
2529 case LYX_ALIGN_LEFT:
2532 case LYX_ALIGN_RIGHT:
2536 case LYX_ALIGN_CENTER:
2542 os << docstring(len1, ' ') << sstr.str()
2543 << docstring(len2, ' ');
2545 if (rightLine(cell))
2552 void Tabular::plaintext(odocstream & os,
2553 OutputParams const & runparams, int const depth,
2554 bool onlydata, char_type delim) const
2556 // first calculate the width of the single columns
2557 vector<unsigned int> clen(columnCount());
2560 // first all non (real) multicolumn cells!
2561 for (col_type j = 0; j < columnCount(); ++j) {
2563 for (row_type i = 0; i < rowCount(); ++i) {
2564 idx_type cell = cellIndex(i, j);
2565 if (isMultiColumnReal(cell))
2567 odocstringstream sstr;
2568 getCellInset(cell)->plaintext(sstr, runparams);
2569 if (clen[j] < sstr.str().length())
2570 clen[j] = sstr.str().length();
2573 // then all (real) multicolumn cells!
2574 for (col_type j = 0; j < columnCount(); ++j) {
2575 for (row_type i = 0; i < rowCount(); ++i) {
2576 idx_type cell = cellIndex(i, j);
2577 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2579 odocstringstream sstr;
2580 getCellInset(cell)->plaintext(sstr, runparams);
2581 int len = int(sstr.str().length());
2582 idx_type const n = columnSpan(cell);
2583 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2585 if (len > int(clen[j + n - 1]))
2586 clen[j + n - 1] = len;
2591 for (row_type i = 0; i < rowCount(); ++i) {
2592 if (!onlydata && plaintextTopHLine(os, i, clen))
2593 os << docstring(depth * 2, ' ');
2594 for (col_type j = 0; j < columnCount(); ++j) {
2595 if (isPartOfMultiColumn(i, j))
2597 if (onlydata && j > 0)
2598 // we don't use operator<< for single UCS4 character.
2599 // see explanation in docstream.h
2601 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2606 os << docstring(depth * 2, ' ');
2607 if (plaintextBottomHLine(os, i, clen))
2608 os << docstring(depth * 2, ' ');
2614 shared_ptr<InsetTableCell> Tabular::getCellInset(idx_type cell) const
2616 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2620 shared_ptr<InsetTableCell> Tabular::getCellInset(row_type row,
2621 col_type column) const
2623 return cell_info[row][column].inset;
2627 void Tabular::setCellInset(row_type row, col_type column,
2628 shared_ptr<InsetTableCell> ins) const
2630 CellData & cd = cell_info[row][column];
2632 // reset the InsetTableCell's pointers
2633 ins->setCellData(&cd);
2634 ins->setTabular(this);
2639 Tabular::getCellFromInset(Inset const * inset) const
2641 // is this inset part of the tabular?
2643 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2644 BOOST_ASSERT(false);
2647 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2648 if (getCellInset(cell).get() == inset) {
2649 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2650 << "cell=" << cell);
2654 // We should have found a cell at this point
2655 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2656 << inset << " not found!" << endl;
2657 BOOST_ASSERT(false);
2663 void Tabular::validate(LaTeXFeatures & features) const
2665 features.require("NeedTabularnewline");
2667 features.require("booktabs");
2668 if (isLongTabular())
2669 features.require("longtable");
2671 features.require("rotating");
2672 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2673 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2674 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2675 features.require("array");
2676 getCellInset(cell)->validate(features);
2681 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2683 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2684 ParagraphList::const_iterator cit = parlist.begin();
2685 ParagraphList::const_iterator end = parlist.end();
2687 for (; cit != end; ++cit)
2688 for (int i = 0; i < cit->size(); ++i)
2689 if (cit->isNewline(i))
2696 /////////////////////////////////////////////////////////////////////
2700 /////////////////////////////////////////////////////////////////////
2702 InsetTableCell::InsetTableCell(Buffer const & buf,
2703 Tabular::CellData const * cell, Tabular const * table)
2704 : InsetText(buf), cell_data_(cell), table_(table)
2708 bool InsetTableCell::forceEmptyLayout(idx_type) const
2710 BOOST_ASSERT(table_);
2711 BOOST_ASSERT(cell_data_);
2712 return table_->getPWidth(cell_data_->cellno).zero();
2715 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2717 BOOST_ASSERT(table_);
2718 BOOST_ASSERT(cell_data_);
2719 return !table_->getPWidth(cell_data_->cellno).zero();
2722 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2723 FuncStatus & status) const
2726 switch (cmd.action) {
2728 enabled = !forceEmptyLayout();
2730 case LFUN_LAYOUT_PARAGRAPH:
2731 enabled = allowParagraphCustomization();
2734 return InsetText::getStatus(cur, cmd, status);
2736 status.enabled(enabled);
2740 /////////////////////////////////////////////////////////////////////
2744 /////////////////////////////////////////////////////////////////////
2746 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2748 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0)
2750 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2754 InsetTabular::InsetTabular(InsetTabular const & tab)
2755 : Inset(tab), tabular(tab.tabular), scx_(0)
2757 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2761 InsetTabular::~InsetTabular()
2763 InsetTabularMailer(*this).hideDialog();
2767 bool InsetTabular::insetAllowed(InsetCode code) const
2769 if (code == MATHMACRO_CODE)
2776 void InsetTabular::write(ostream & os) const
2778 os << "Tabular" << endl;
2783 void InsetTabular::read(Lexer & lex)
2785 bool const old_format = (lex.getString() == "\\LyXTable");
2793 string token = lex.getString();
2794 while (lex && token != "\\end_inset") {
2796 token = lex.getString();
2799 lex.printError("Missing \\end_inset at this point. "
2805 int InsetTabular::rowFromY(Cursor & cur, int y) const
2807 // top y coordinate of tabular
2808 int h = yo(cur.bv()) - tabular.rowAscent(0);
2809 size_t nrows = tabular.rowCount();
2811 for (;r < nrows && y > h; ++r) {
2812 h += tabular.rowAscent(r);
2813 h += tabular.rowDescent(r);
2814 h += tabular.getAdditionalHeight(r);
2820 int InsetTabular::columnFromX(Cursor & cur, int x) const
2822 // left x coordinate of tabular
2823 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2824 size_t ncols = tabular.columnCount();
2826 for (;c < ncols && x > w; ++c) {
2827 w += tabular.columnWidth(c);
2833 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2835 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2836 // mi.base.textwidth << "\n";
2838 lyxerr << "InsetTabular::metrics: need bv" << endl;
2839 BOOST_ASSERT(false);
2843 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2846 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2847 if (tabular.isPartOfMultiColumn(i, j))
2848 // Multicolumn cell, but not first one
2853 if (tabular.cell_info[i][j].multicolumn ==
2854 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2855 p_width = tabular.cellinfo_of_cell(cell).p_width;
2857 p_width = tabular.column_info[j].p_width;
2858 if (!p_width.zero())
2859 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2860 tabular.getCellInset(cell)->metrics(m, dim);
2861 if (!p_width.zero())
2862 dim.wid = m.base.textwidth;
2863 tabular.setCellWidth(cell, dim.wid);
2864 if (p_width.zero()) {
2865 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2866 // FIXME there must be a way to get rid of
2867 // the second metrics call
2868 tabular.getCellInset(cell)->metrics(m, dim);
2870 maxAsc = max(maxAsc, dim.asc);
2871 maxDesc = max(maxDesc, dim.des);
2874 int const top_space = tabular.row_info[i].top_space_default ?
2875 default_line_space :
2876 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2877 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2878 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2879 default_line_space :
2880 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2881 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2883 tabular.updateColumnWidths();
2884 dim.asc = tabular.rowAscent(0);
2885 dim.des = tabular.height() - dim.asc;
2886 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2890 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2892 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2893 BufferView * bv = pi.base.bv;
2895 // FIXME: As the full backrgound is painted in drawSelection(),
2896 // we have no choice but to do a full repaint for the Text cells.
2897 pi.full_repaint = true;
2899 resetPos(bv->cursor());
2902 x += ADD_TO_TABULAR_WIDTH;
2904 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2907 first_visible_cell = Tabular::npos;
2908 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2910 int const a = tabular.rowAscent(i);
2911 int const d = tabular.rowDescent(i);
2912 idx = tabular.cellIndex(i, 0);
2913 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2914 if (tabular.isPartOfMultiColumn(i, j))
2916 if (first_visible_cell == Tabular::npos)
2917 first_visible_cell = idx;
2919 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2920 // Cache the Inset position.
2921 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2922 if (nx + tabular.columnWidth(idx) < 0
2923 || nx > bv->workWidth()
2925 || y - a > bv->workHeight()) {
2926 pi.pain.setDrawingEnabled(false);
2927 cell(idx)->draw(pi, cx, y);
2928 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2929 pi.pain.setDrawingEnabled(original_drawing_state);
2931 cell(idx)->draw(pi, cx, y);
2932 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2934 nx += tabular.columnWidth(idx);
2938 if (i + 1 < tabular.rowCount())
2939 y += d + tabular.rowAscent(i + 1) +
2940 tabular.getAdditionalHeight(i + 1);
2945 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2947 Cursor & cur = pi.base.bv->cursor();
2949 x += scx_ + ADD_TO_TABULAR_WIDTH;
2951 // FIXME: it is wrong to completely paint the background
2952 // if we want to do single row painting.
2954 // Paint background of current tabular
2955 int const w = tabular.width();
2956 int const h = tabular.height();
2957 int yy = y - tabular.rowAscent(0);
2958 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2960 if (!cur.selection())
2962 if (&cur.inset() != this)
2968 if (tablemode(cur)) {
2971 getSelection(cur, rs, re, cs, ce);
2972 y -= tabular.rowAscent(0);
2973 for (row_type j = 0; j < tabular.rowCount(); ++j) {
2974 int const a = tabular.rowAscent(j);
2975 int const h = a + tabular.rowDescent(j);
2977 y += tabular.getAdditionalHeight(j);
2978 for (col_type i = 0; i < tabular.columnCount(); ++i) {
2979 if (tabular.isPartOfMultiColumn(j, i))
2981 idx_type const cell =
2982 tabular.cellIndex(j, i);
2983 int const w = tabular.columnWidth(cell);
2984 if (i >= cs && i <= ce && j >= rs && j <= re)
2985 pi.pain.fillRectangle(xx, y, w, h,
2993 x += getCellXPos(cur.idx());
2994 x += tabular.getBeginningOfTextInCell(cur.idx());
2995 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3000 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3001 row_type row, idx_type cell, bool erased) const
3003 int x2 = x + tabular.columnWidth(cell);
3004 bool on_off = false;
3005 ColorCode col = Color_tabularline;
3006 ColorCode onoffcol = Color_tabularonoffline;
3009 col = Color_deletedtext;
3010 onoffcol = Color_deletedtext;
3013 if (!tabular.topAlreadyDrawn(cell)) {
3014 on_off = !tabular.topLine(cell);
3015 pain.line(x, y - tabular.rowAscent(row),
3016 x2, y - tabular.rowAscent(row),
3017 on_off ? onoffcol : col,
3018 on_off ? Painter::line_onoffdash : Painter::line_solid);
3020 on_off = !tabular.bottomLine(cell);
3021 pain.line(x, y + tabular.rowDescent(row),
3022 x2, y + tabular.rowDescent(row),
3023 on_off ? onoffcol : col,
3024 on_off ? Painter::line_onoffdash : Painter::line_solid);
3025 if (!tabular.leftAlreadyDrawn(cell)) {
3026 on_off = !tabular.leftLine(cell);
3027 pain.line(x, y - tabular.rowAscent(row),
3028 x, y + tabular.rowDescent(row),
3029 on_off ? onoffcol : col,
3030 on_off ? Painter::line_onoffdash : Painter::line_solid);
3032 on_off = !tabular.rightLine(cell);
3033 pain.line(x2 - tabular.getAdditionalWidth(cell),
3034 y - tabular.rowAscent(row),
3035 x2 - tabular.getAdditionalWidth(cell),
3036 y + tabular.rowDescent(row),
3037 on_off ? onoffcol : col,
3038 on_off ? Painter::line_onoffdash : Painter::line_solid);
3042 docstring InsetTabular::editMessage() const
3044 return _("Opened table");
3048 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3050 //lyxerr << "InsetTabular::edit: " << this << endl;
3052 cur.selection() = false;
3055 if (isRightToLeft(cur))
3056 cur.idx() = tabular.getLastCellInRow(0);
3062 if (isRightToLeft(cur))
3063 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3065 cur.idx() = tabular.cellCount() - 1;
3067 cur.pos() = cur.lastpos(); // FIXME crude guess
3069 // FIXME: this accesses the position cache before it is initialized
3071 //cur.bv().fitCursor();
3075 void InsetTabular::updateLabels(ParIterator const & it)
3077 // In a longtable, tell captions what the current float is
3078 Counters & cnts = buffer().params().documentClass().counters();
3079 string const saveflt = cnts.current_float();
3080 if (tabular.isLongTabular())
3081 cnts.current_float("table");
3083 ParIterator it2 = it;
3085 size_t const end = it2.nargs();
3086 for ( ; it2.idx() < end; it2.top().forwardIdx())
3087 lyx::updateLabels(buffer(), it2);
3090 if (tabular.isLongTabular())
3091 cnts.current_float(saveflt);
3095 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3097 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3098 << "\n cur:" << cur);
3099 CursorSlice sl = cur.top();
3100 Cursor & bvcur = cur.bv().cursor();
3102 switch (cmd.action) {
3104 case LFUN_MOUSE_PRESS: {
3105 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3108 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3109 || cmd.x > xo(cur.bv()) + tabular.width()) {
3110 row_type r = rowFromY(cur, cmd.y);
3111 cur.idx() = tabular.getFirstCellInRow(r);
3114 cur.idx() = tabular.getLastCellInRow(r);
3115 cur.pos() = cur.lastpos();
3116 cur.selection() = true;
3121 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3122 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3123 || cmd.y > y0 + tabular.height()) {
3124 col_type c = columnFromX(cur, cmd.x);
3125 cur.idx() = tabular.cellIndex(0, c);
3128 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3129 cur.pos() = cur.lastpos();
3130 cur.selection() = true;
3134 // do not reset cursor/selection if we have selected
3135 // some cells (bug 2715).
3136 if (cmd.button() == mouse_button::button3
3137 && &bvcur.selBegin().inset() == this
3138 && tablemode(bvcur))
3141 // Let InsetTableCell do it
3142 cell(cur.idx())->dispatch(cur, cmd);
3145 case LFUN_MOUSE_MOTION:
3146 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3147 if (cmd.button() == mouse_button::button1) {
3148 // only accept motions to places not deeper nested than the real anchor
3149 if (!bvcur.anchor_.hasPart(cur)) {
3153 // select (additional) row
3154 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3155 || cmd.x > xo(cur.bv()) + tabular.width()) {
3156 row_type r = rowFromY(cur, cmd.y);
3157 cur.idx() = tabular.getLastCellInRow(r);
3158 bvcur.setCursor(cur);
3159 bvcur.selection() = true;
3162 // select (additional) column
3163 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3164 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3165 || cmd.y > y0 + tabular.height()) {
3166 col_type c = columnFromX(cur, cmd.x);
3167 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3168 bvcur.setCursor(cur);
3169 bvcur.selection() = true;
3172 // only update if selection changes
3173 if (bvcur.idx() == cur.idx() &&
3174 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3176 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3177 bvcur.setCursor(cur);
3178 bvcur.selection() = true;
3182 case LFUN_MOUSE_RELEASE:
3183 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3184 if (cmd.button() == mouse_button::button3)
3185 InsetTabularMailer(*this).showDialog(&cur.bv());
3188 case LFUN_CELL_BACKWARD:
3190 cur.selection() = false;
3193 case LFUN_CELL_FORWARD:
3195 cur.selection() = false;
3197 case LFUN_CHAR_FORWARD_SELECT:
3198 case LFUN_CHAR_FORWARD:
3199 cell(cur.idx())->dispatch(cur, cmd);
3200 if (!cur.result().dispatched()) {
3202 if (sl == cur.top())
3203 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3209 case LFUN_CHAR_BACKWARD_SELECT:
3210 case LFUN_CHAR_BACKWARD:
3211 cell(cur.idx())->dispatch(cur, cmd);
3212 if (!cur.result().dispatched()) {
3214 if (sl == cur.top())
3215 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3221 case LFUN_CHAR_RIGHT_SELECT:
3222 case LFUN_CHAR_RIGHT:
3223 //FIXME: for visual cursor, really move right
3224 if (isRightToLeft(cur))
3225 lyx::dispatch(FuncRequest(
3226 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3227 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3229 lyx::dispatch(FuncRequest(
3230 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3231 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3234 case LFUN_CHAR_LEFT_SELECT:
3235 case LFUN_CHAR_LEFT:
3236 //FIXME: for visual cursor, really move left
3237 if (isRightToLeft(cur))
3238 lyx::dispatch(FuncRequest(
3239 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3240 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3242 lyx::dispatch(FuncRequest(
3243 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3244 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3247 case LFUN_DOWN_SELECT:
3249 cell(cur.idx())->dispatch(cur, cmd);
3250 cur.dispatched(); // override the cell's decision
3251 if (sl == cur.top())
3252 // if our Text didn't do anything to the cursor
3253 // then we try to put the cursor into the cell below
3254 // setting also the right targetX.
3255 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3256 cur.idx() = tabular.getCellBelow(cur.idx());
3258 TextMetrics const & tm =
3259 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3260 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3262 if (sl == cur.top()) {
3263 // we trick it to go to forward after leaving the
3265 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3270 case LFUN_UP_SELECT:
3272 cell(cur.idx())->dispatch(cur, cmd);
3273 cur.dispatched(); // override the cell's decision
3274 if (sl == cur.top())
3275 // if our Text didn't do anything to the cursor
3276 // then we try to put the cursor into the cell above
3277 // setting also the right targetX.
3278 if (tabular.cellRow(cur.idx()) != 0) {
3279 cur.idx() = tabular.getCellAbove(cur.idx());
3280 cur.pit() = cur.lastpit();
3281 Text const * text = cell(cur.idx())->getText(0);
3282 TextMetrics const & tm = cur.bv().textMetrics(text);
3283 ParagraphMetrics const & pm =
3284 tm.parMetrics(cur.lastpit());
3285 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3287 if (sl == cur.top()) {
3288 cmd = FuncRequest(LFUN_UP);
3293 // case LFUN_SCREEN_DOWN: {
3294 // //if (hasSelection())
3295 // // cur.selection() = false;
3296 // col_type const col = tabular.cellColumn(cur.idx());
3297 // int const t = cur.bv().top_y() + cur.bv().height();
3298 // if (t < yo() + tabular.getHeightOfTabular()) {
3299 // cur.bv().scrollDocView(t);
3300 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3302 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3309 // case LFUN_SCREEN_UP: {
3310 // //if (hasSelection())
3311 // // cur.selection() = false;
3312 // col_type const col = tabular.cellColumn(cur.idx());
3313 // int const t = cur.bv().top_y() + cur.bv().height();
3315 // cur.bv().scrollDocView(t);
3319 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3323 // cur.par() = cur.lastpar();
3324 // cur.pos() = cur.lastpos();
3328 case LFUN_LAYOUT_TABULAR:
3329 InsetTabularMailer(*this).showDialog(&cur.bv());
3332 case LFUN_INSET_DIALOG_UPDATE:
3333 InsetTabularMailer(*this).updateDialog(&cur.bv());
3336 case LFUN_TABULAR_FEATURE:
3337 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3341 // insert file functions
3342 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3343 case LFUN_FILE_INSERT_PLAINTEXT: {
3345 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3346 FileName(to_utf8(cmd.argument())));
3349 cur.recordUndoInset(INSERT_UNDO);
3350 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3351 // content has been replaced,
3352 // so cursor might be invalid
3353 cur.pos() = cur.lastpos();
3354 cur.pit() = cur.lastpit();
3355 bvcur.setCursor(cur);
3362 if (tablemode(cur)) {
3363 if (copySelection(cur)) {
3364 cur.recordUndoInset(DELETE_UNDO);
3369 cell(cur.idx())->dispatch(cur, cmd);
3372 case LFUN_CHAR_DELETE_BACKWARD:
3373 case LFUN_CHAR_DELETE_FORWARD:
3374 if (tablemode(cur)) {
3375 cur.recordUndoInset(DELETE_UNDO);
3379 cell(cur.idx())->dispatch(cur, cmd);
3383 if (!cur.selection())
3385 if (tablemode(cur)) {
3389 cell(cur.idx())->dispatch(cur, cmd);
3392 case LFUN_CLIPBOARD_PASTE:
3393 case LFUN_PRIMARY_SELECTION_PASTE: {
3394 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3395 theClipboard().getAsText() :
3396 theSelection().get();
3399 // pass to InsertPlaintextString, but
3400 // only if we have multi-cell content
3401 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3402 cur.recordUndoInset(INSERT_UNDO);
3403 if (insertPlaintextString(cur.bv(), clip, false)) {
3404 // content has been replaced,
3405 // so cursor might be invalid
3406 cur.pos() = cur.lastpos();
3407 cur.pit() = cur.lastpit();
3408 bvcur.setCursor(cur);
3412 // Let the cell handle normal text
3413 cell(cur.idx())->dispatch(cur, cmd);
3418 if (tabularStackDirty() && theClipboard().isInternal() ||
3419 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3420 cur.recordUndoInset(INSERT_UNDO);
3421 pasteClipboard(cur);
3424 cell(cur.idx())->dispatch(cur, cmd);
3427 case LFUN_FONT_EMPH:
3428 case LFUN_FONT_BOLD:
3429 case LFUN_FONT_ROMAN:
3430 case LFUN_FONT_NOUN:
3431 case LFUN_FONT_ITAL:
3432 case LFUN_FONT_FRAK:
3433 case LFUN_FONT_TYPEWRITER:
3434 case LFUN_FONT_SANS:
3435 case LFUN_FONT_FREE_APPLY:
3436 case LFUN_FONT_FREE_UPDATE:
3437 case LFUN_FONT_SIZE:
3438 case LFUN_FONT_UNDERLINE:
3440 case LFUN_WORD_CAPITALIZE:
3441 case LFUN_WORD_UPCASE:
3442 case LFUN_WORD_LOWCASE:
3443 case LFUN_CHARS_TRANSPOSE:
3444 if (tablemode(cur)) {
3447 getSelection(cur, rs, re, cs, ce);
3448 Cursor tmpcur = cur;
3449 for (row_type i = rs; i <= re; ++i) {
3450 for (col_type j = cs; j <= ce; ++j) {
3451 // cursor follows cell:
3452 tmpcur.idx() = tabular.cellIndex(i, j);
3453 // select this cell only:
3456 tmpcur.resetAnchor();
3457 tmpcur.pit() = tmpcur.lastpit();
3458 tmpcur.pos() = tmpcur.top().lastpos();
3459 tmpcur.setCursor(tmpcur);
3460 tmpcur.setSelection();
3461 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3466 cell(cur.idx())->dispatch(cur, cmd);
3470 // we try to handle this event in the insets dispatch function.
3471 cell(cur.idx())->dispatch(cur, cmd);
3477 // function sets an object as defined in func_status.h:
3478 // states OK, Unknown, Disabled, On, Off.
3479 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3480 FuncStatus & status) const
3482 switch (cmd.action) {
3483 case LFUN_TABULAR_FEATURE: {
3484 int action = Tabular::LAST_ACTION;
3486 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3487 string const tmp = tabularFeature[i].feature;
3488 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3489 action = tabularFeature[i].action;
3493 if (action == Tabular::LAST_ACTION) {
3495 status.unknown(true);
3499 string const argument
3500 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3502 row_type sel_row_start = 0;
3503 row_type sel_row_end = 0;
3504 col_type sel_col_start = 0;
3505 col_type sel_col_end = 0;
3506 Tabular::ltType dummyltt;
3509 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3512 case Tabular::SET_PWIDTH:
3513 case Tabular::SET_MPWIDTH:
3514 case Tabular::SET_SPECIAL_COLUMN:
3515 case Tabular::SET_SPECIAL_MULTI:
3516 case Tabular::APPEND_ROW:
3517 case Tabular::APPEND_COLUMN:
3518 case Tabular::DELETE_ROW:
3519 case Tabular::DELETE_COLUMN:
3520 case Tabular::COPY_ROW:
3521 case Tabular::COPY_COLUMN:
3522 case Tabular::SET_ALL_LINES:
3523 case Tabular::UNSET_ALL_LINES:
3524 case Tabular::SET_TOP_SPACE:
3525 case Tabular::SET_BOTTOM_SPACE:
3526 case Tabular::SET_INTERLINE_SPACE:
3527 case Tabular::SET_BORDER_LINES:
3531 case Tabular::MULTICOLUMN:
3532 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3535 case Tabular::TOGGLE_LINE_TOP:
3536 status.setOnOff(tabular.topLine(cur.idx()));
3539 case Tabular::TOGGLE_LINE_BOTTOM:
3540 status.setOnOff(tabular.bottomLine(cur.idx()));
3543 case Tabular::TOGGLE_LINE_LEFT:
3544 status.setOnOff(tabular.leftLine(cur.idx()));
3547 case Tabular::TOGGLE_LINE_RIGHT:
3548 status.setOnOff(tabular.rightLine(cur.idx()));
3551 case Tabular::M_ALIGN_LEFT:
3553 case Tabular::ALIGN_LEFT:
3554 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3557 case Tabular::M_ALIGN_RIGHT:
3559 case Tabular::ALIGN_RIGHT:
3560 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3563 case Tabular::M_ALIGN_CENTER:
3565 case Tabular::ALIGN_CENTER:
3566 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3569 case Tabular::ALIGN_BLOCK:
3570 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3571 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3574 case Tabular::M_VALIGN_TOP:
3576 case Tabular::VALIGN_TOP:
3578 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3581 case Tabular::M_VALIGN_BOTTOM:
3583 case Tabular::VALIGN_BOTTOM:
3585 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3588 case Tabular::M_VALIGN_MIDDLE:
3590 case Tabular::VALIGN_MIDDLE:
3592 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3595 case Tabular::SET_LONGTABULAR:
3596 status.setOnOff(tabular.isLongTabular());
3599 case Tabular::UNSET_LONGTABULAR:
3600 status.setOnOff(!tabular.isLongTabular());
3603 case Tabular::TOGGLE_ROTATE_TABULAR:
3604 case Tabular::SET_ROTATE_TABULAR:
3605 status.setOnOff(tabular.getRotateTabular());
3608 case Tabular::UNSET_ROTATE_TABULAR:
3609 status.setOnOff(!tabular.getRotateTabular());
3612 case Tabular::TOGGLE_ROTATE_CELL:
3613 case Tabular::SET_ROTATE_CELL:
3614 status.setOnOff(!oneCellHasRotationState(false,
3615 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3618 case Tabular::UNSET_ROTATE_CELL:
3619 status.setOnOff(!oneCellHasRotationState(true,
3620 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3623 case Tabular::SET_USEBOX:
3624 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3627 case Tabular::SET_LTFIRSTHEAD:
3628 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3631 case Tabular::UNSET_LTFIRSTHEAD:
3632 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3635 case Tabular::SET_LTHEAD:
3636 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3639 case Tabular::UNSET_LTHEAD:
3640 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3643 case Tabular::SET_LTFOOT:
3644 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3647 case Tabular::UNSET_LTFOOT:
3648 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3651 case Tabular::SET_LTLASTFOOT:
3652 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3655 case Tabular::UNSET_LTLASTFOOT:
3656 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3659 case Tabular::SET_LTNEWPAGE:
3660 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3663 case Tabular::SET_BOOKTABS:
3664 status.setOnOff(tabular.useBookTabs());
3667 case Tabular::UNSET_BOOKTABS:
3668 status.setOnOff(!tabular.useBookTabs());
3673 status.enabled(false);
3679 // These are only enabled inside tabular
3680 case LFUN_CELL_BACKWARD:
3681 case LFUN_CELL_FORWARD:
3682 status.enabled(true);
3685 // disable these with multiple cells selected
3686 case LFUN_INSET_INSERT:
3687 case LFUN_TABULAR_INSERT:
3688 case LFUN_FLEX_INSERT:
3689 case LFUN_FLOAT_INSERT:
3690 case LFUN_FLOAT_WIDE_INSERT:
3691 case LFUN_FOOTNOTE_INSERT:
3692 case LFUN_MARGINALNOTE_INSERT:
3693 case LFUN_MATH_INSERT:
3694 case LFUN_MATH_MODE:
3695 case LFUN_MATH_MUTATE:
3696 case LFUN_MATH_DISPLAY:
3697 case LFUN_NOTE_INSERT:
3698 case LFUN_OPTIONAL_INSERT:
3699 case LFUN_BOX_INSERT:
3700 case LFUN_BRANCH_INSERT:
3701 case LFUN_WRAP_INSERT:
3702 case LFUN_ERT_INSERT: {
3703 if (tablemode(cur)) {
3704 status.enabled(false);
3707 return cell(cur.idx())->getStatus(cur, cmd, status);
3710 // disable in non-fixed-width cells
3712 case LFUN_BREAK_PARAGRAPH:
3713 case LFUN_BREAK_PARAGRAPH_SKIP: {
3714 if (tabular.getPWidth(cur.idx()).zero()) {
3715 status.enabled(false);
3718 return cell(cur.idx())->getStatus(cur, cmd, status);
3722 if (tabularStackDirty() && theClipboard().isInternal()) {
3723 status.enabled(true);
3726 return cell(cur.idx())->getStatus(cur, cmd, status);
3728 case LFUN_INSET_MODIFY:
3729 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3730 status.enabled(true);
3736 // we try to handle this event in the insets dispatch function.
3737 return cell(cur.idx())->getStatus(cur, cmd, status);
3742 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3744 return tabular.latex(os, runparams);
3748 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3750 os << '\n'; // output table on a new line
3751 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3752 tabular.plaintext(os, runparams, dp, false, 0);
3753 return PLAINTEXT_NEWLINE;
3757 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3762 // FIXME: Why not pass a proper DocIterator here?
3764 // if the table is inside a float it doesn't need the informaltable
3765 // wrapper. Search for it.
3766 for (master = owner(); master; master = master->owner())
3767 if (master->lyxCode() == FLOAT_CODE)
3772 os << "<informaltable>";
3775 ret += tabular.docbook(os, runparams);
3777 os << "</informaltable>";
3784 void InsetTabular::validate(LaTeXFeatures & features) const
3786 tabular.validate(features);
3790 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3792 return tabular.getCellInset(idx);
3796 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3798 return tabular.getCellInset(idx);
3802 void InsetTabular::cursorPos(BufferView const & bv,
3803 CursorSlice const & sl, bool boundary, int & x, int & y) const
3805 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3807 // y offset correction
3808 int const row = tabular.cellRow(sl.idx());
3809 for (int i = 0; i <= row; ++i) {
3811 y += tabular.rowAscent(i);
3812 y += tabular.getAdditionalHeight(i);
3815 y += tabular.rowDescent(i);
3818 // x offset correction
3819 int const col = tabular.cellColumn(sl.idx());
3820 int idx = tabular.cellIndex(row, 0);
3821 for (int j = 0; j < col; ++j) {
3822 if (tabular.isPartOfMultiColumn(row, j))
3824 x += tabular.columnWidth(idx);
3827 x += tabular.getBeginningOfTextInCell(idx);
3828 x += ADD_TO_TABULAR_WIDTH;
3833 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3837 Inset const & inset = *tabular.getCellInset(cell);
3838 Point o = bv.coordCache().getInsets().xy(&inset);
3839 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3840 int const xend = xbeg + tabular.columnWidth(cell);
3841 row_type const row = tabular.cellRow(cell);
3842 int const ybeg = o.y_ - tabular.rowAscent(row) -
3843 tabular.getAdditionalHeight(row);
3844 int const yend = o.y_ + tabular.rowDescent(row);
3856 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3857 // << " ybeg=" << ybeg << " yend=" << yend
3858 // << " xx=" << xx << " yy=" << yy
3859 // << " dist=" << xx + yy << endl;
3864 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3866 //lyxerr << "InsetTabular::editXY: " << this << endl;
3867 cur.selection() = false;
3869 cur.idx() = getNearestCell(cur.bv(), x, y);
3871 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3875 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3877 cur.idx() = getNearestCell(cur.bv(), x, y);
3878 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3882 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3884 idx_type idx_min = 0;
3885 int dist_min = numeric_limits<int>::max();
3886 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3887 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3888 int const d = dist(bv, i, x, y);
3899 int InsetTabular::getCellXPos(idx_type const cell) const
3903 for (; !tabular.isFirstCellInRow(c); --c)
3906 for (; c < cell; ++c)
3907 lx += tabular.columnWidth(c);
3913 void InsetTabular::resetPos(Cursor & cur) const
3915 BufferView & bv = cur.bv();
3916 int const maxwidth = bv.workWidth();
3918 if (&cur.inset() != this) {
3922 int const X2 = maxwidth;
3923 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3924 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3925 int const x2 = x1 + tabular.columnWidth(cur.idx());
3928 scx_ = X1 + 20 - x1;
3930 scx_ = X2 - 20 - x2;
3935 cur.updateFlags(Update::Force | Update::FitCursor);
3939 void InsetTabular::moveNextCell(Cursor & cur)
3941 if (isRightToLeft(cur)) {
3942 if (tabular.isFirstCellInRow(cur.idx())) {
3943 row_type const row = tabular.cellRow(cur.idx());
3944 if (row == tabular.rowCount() - 1)
3946 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3953 if (tabular.isLastCell(cur.idx()))
3963 void InsetTabular::movePrevCell(Cursor & cur)
3965 if (isRightToLeft(cur)) {
3966 if (tabular.isLastCellInRow(cur.idx())) {
3967 row_type const row = tabular.cellRow(cur.idx());
3970 cur.idx() = tabular.getFirstCellInRow(row);
3971 cur.idx() = tabular.getCellAbove(cur.idx());
3973 if (tabular.isLastCell(cur.idx()))
3978 if (cur.idx() == 0) // first cell
3982 cur.pit() = cur.lastpit();
3983 cur.pos() = cur.lastpos();
3985 // FIXME: this accesses the position cache before it is initialized
3990 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
3992 Tabular::Feature action = Tabular::LAST_ACTION;
3995 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3996 string const tmp = tabularFeature[i].feature;
3998 if (tmp == what.substr(0, tmp.length())) {
3999 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4000 //tabularFeatures[i].feature.length()))
4001 action = tabularFeature[i].action;
4005 if (action == Tabular::LAST_ACTION)
4009 ltrim(what.substr(tabularFeature[i].feature.length()));
4010 tabularFeatures(cur, action, val);
4015 static void checkLongtableSpecial(Tabular::ltType & ltt,
4016 string const & special, bool & flag)
4018 if (special == "dl_above") {
4021 } else if (special == "dl_below") {
4022 ltt.bottomDL = flag;
4024 } else if (special == "empty") {
4033 bool InsetTabular::oneCellHasRotationState(bool rotated,
4034 row_type row_start, row_type row_end,
4035 col_type col_start, col_type col_end) const {
4037 for (row_type i = row_start; i <= row_end; ++i) {
4038 for (col_type j = col_start; j <= col_end; ++j) {
4039 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4048 void InsetTabular::tabularFeatures(Cursor & cur,
4049 Tabular::Feature feature, string const & value)
4051 col_type sel_col_start;
4052 col_type sel_col_end;
4053 row_type sel_row_start;
4054 row_type sel_row_end;
4055 bool setLines = false;
4056 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4057 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4061 case Tabular::M_ALIGN_LEFT:
4062 case Tabular::ALIGN_LEFT:
4063 setAlign = LYX_ALIGN_LEFT;
4066 case Tabular::M_ALIGN_RIGHT:
4067 case Tabular::ALIGN_RIGHT:
4068 setAlign = LYX_ALIGN_RIGHT;
4071 case Tabular::M_ALIGN_CENTER:
4072 case Tabular::ALIGN_CENTER:
4073 setAlign = LYX_ALIGN_CENTER;
4076 case Tabular::ALIGN_BLOCK:
4077 setAlign = LYX_ALIGN_BLOCK;
4080 case Tabular::M_VALIGN_TOP:
4081 case Tabular::VALIGN_TOP:
4082 setVAlign = Tabular::LYX_VALIGN_TOP;
4085 case Tabular::M_VALIGN_BOTTOM:
4086 case Tabular::VALIGN_BOTTOM:
4087 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4090 case Tabular::M_VALIGN_MIDDLE:
4091 case Tabular::VALIGN_MIDDLE:
4092 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4099 cur.recordUndoInset(ATOMIC_UNDO);
4101 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4102 row_type const row = tabular.cellRow(cur.idx());
4103 col_type const column = tabular.cellColumn(cur.idx());
4105 Tabular::ltType ltt;
4109 case Tabular::SET_PWIDTH: {
4110 Length const len(value);
4111 tabular.setColumnPWidth(cur, cur.idx(), len);
4113 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4114 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4118 case Tabular::SET_MPWIDTH:
4119 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4122 case Tabular::SET_SPECIAL_COLUMN:
4123 case Tabular::SET_SPECIAL_MULTI:
4124 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4127 case Tabular::APPEND_ROW:
4128 // append the row into the tabular
4129 tabular.appendRow(cur.idx());
4132 case Tabular::APPEND_COLUMN:
4133 // append the column into the tabular
4134 tabular.appendColumn(cur.idx());
4135 cur.idx() = tabular.cellIndex(row, column);
4138 case Tabular::DELETE_ROW:
4139 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4140 tabular.deleteRow(sel_row_start);
4141 if (sel_row_start >= tabular.rowCount())
4143 cur.idx() = tabular.cellIndex(sel_row_start, column);
4146 cur.selection() = false;
4149 case Tabular::DELETE_COLUMN:
4150 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4151 tabular.deleteColumn(sel_col_start);
4152 if (sel_col_start >= tabular.columnCount())
4154 cur.idx() = tabular.cellIndex(row, sel_col_start);
4157 cur.selection() = false;
4160 case Tabular::COPY_ROW:
4161 tabular.copyRow(row);
4164 case Tabular::COPY_COLUMN:
4165 tabular.copyColumn(column);
4166 cur.idx() = tabular.cellIndex(row, column);
4169 case Tabular::TOGGLE_LINE_TOP: {
4170 bool lineSet = !tabular.topLine(cur.idx());
4171 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4172 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4173 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4177 case Tabular::TOGGLE_LINE_BOTTOM: {
4178 bool lineSet = !tabular.bottomLine(cur.idx());
4179 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4180 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4181 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4185 case Tabular::TOGGLE_LINE_LEFT: {
4186 bool lineSet = !tabular.leftLine(cur.idx());
4187 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4188 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4189 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4193 case Tabular::TOGGLE_LINE_RIGHT: {
4194 bool lineSet = !tabular.rightLine(cur.idx());
4195 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4196 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4197 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4201 case Tabular::M_ALIGN_LEFT:
4202 case Tabular::M_ALIGN_RIGHT:
4203 case Tabular::M_ALIGN_CENTER:
4205 case Tabular::ALIGN_LEFT:
4206 case Tabular::ALIGN_RIGHT:
4207 case Tabular::ALIGN_CENTER:
4208 case Tabular::ALIGN_BLOCK:
4209 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4210 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4211 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4214 case Tabular::M_VALIGN_TOP:
4215 case Tabular::M_VALIGN_BOTTOM:
4216 case Tabular::M_VALIGN_MIDDLE:
4218 case Tabular::VALIGN_TOP:
4219 case Tabular::VALIGN_BOTTOM:
4220 case Tabular::VALIGN_MIDDLE:
4221 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4222 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4223 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4226 case Tabular::MULTICOLUMN: {
4227 if (sel_row_start != sel_row_end) {
4228 // FIXME: Need I say it ? This is horrible.
4230 Alert::error(_("Error setting multicolumn"),
4231 _("You cannot set multicolumn vertically."));
4234 if (!cur.selection()) {
4235 // just multicol for one single cell
4236 // check whether we are completely in a multicol
4237 if (tabular.isMultiColumn(cur.idx()))
4238 tabular.unsetMultiColumn(cur.idx());
4240 tabular.setMultiColumn(cur.idx(), 1);
4243 // we have a selection so this means we just add all this
4244 // cells to form a multicolumn cell
4245 idx_type const s_start = cur.selBegin().idx();
4246 idx_type const s_end = cur.selEnd().idx();
4247 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4248 cur.idx() = s_start;
4251 cur.selection() = false;
4255 case Tabular::SET_ALL_LINES:
4257 case Tabular::UNSET_ALL_LINES:
4258 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4259 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4260 tabular.setAllLines(
4261 tabular.cellIndex(i,j), setLines);
4264 case Tabular::SET_BORDER_LINES:
4265 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4266 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4267 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4270 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4271 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4275 case Tabular::SET_LONGTABULAR:
4276 tabular.setLongTabular(true);
4279 case Tabular::UNSET_LONGTABULAR:
4280 tabular.setLongTabular(false);
4283 case Tabular::SET_ROTATE_TABULAR:
4284 tabular.setRotateTabular(true);
4287 case Tabular::UNSET_ROTATE_TABULAR:
4288 tabular.setRotateTabular(false);
4291 case Tabular::TOGGLE_ROTATE_TABULAR:
4292 tabular.setRotateTabular(!tabular.getRotateTabular());
4295 case Tabular::SET_ROTATE_CELL:
4296 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4297 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4298 tabular.setRotateCell(
4299 tabular.cellIndex(i, j), true);
4302 case Tabular::UNSET_ROTATE_CELL:
4303 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4304 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4305 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4308 case Tabular::TOGGLE_ROTATE_CELL:
4310 bool oneNotRotated = oneCellHasRotationState(false,
4311 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4313 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4314 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4315 tabular.setRotateCell(tabular.cellIndex(i, j),
4320 case Tabular::SET_USEBOX: {
4321 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4322 if (val == tabular.getUsebox(cur.idx()))
4323 val = Tabular::BOX_NONE;
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.setUsebox(tabular.cellIndex(i, j), val);
4330 case Tabular::UNSET_LTFIRSTHEAD:
4332 case Tabular::SET_LTFIRSTHEAD:
4333 tabular.getRowOfLTFirstHead(row, ltt);
4334 checkLongtableSpecial(ltt, value, flag);
4335 tabular.setLTHead(row, flag, ltt, true);
4338 case Tabular::UNSET_LTHEAD:
4340 case Tabular::SET_LTHEAD:
4341 tabular.getRowOfLTHead(row, ltt);
4342 checkLongtableSpecial(ltt, value, flag);
4343 tabular.setLTHead(row, flag, ltt, false);
4346 case Tabular::UNSET_LTFOOT:
4348 case Tabular::SET_LTFOOT:
4349 tabular.getRowOfLTFoot(row, ltt);
4350 checkLongtableSpecial(ltt, value, flag);
4351 tabular.setLTFoot(row, flag, ltt, false);
4354 case Tabular::UNSET_LTLASTFOOT:
4356 case Tabular::SET_LTLASTFOOT:
4357 tabular.getRowOfLTLastFoot(row, ltt);
4358 checkLongtableSpecial(ltt, value, flag);
4359 tabular.setLTFoot(row, flag, ltt, true);
4362 case Tabular::SET_LTNEWPAGE:
4363 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4366 case Tabular::SET_BOOKTABS:
4367 tabular.setBookTabs(true);
4370 case Tabular::UNSET_BOOKTABS:
4371 tabular.setBookTabs(false);
4374 case Tabular::SET_TOP_SPACE: {
4376 if (value == "default")
4377 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4378 tabular.row_info[i].top_space_default = true;
4379 else if (isValidLength(value, &len))
4380 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4381 tabular.row_info[i].top_space_default = false;
4382 tabular.row_info[i].top_space = len;
4385 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4386 tabular.row_info[i].top_space_default = false;
4387 tabular.row_info[i].top_space = len;
4392 case Tabular::SET_BOTTOM_SPACE: {
4394 if (value == "default")
4395 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4396 tabular.row_info[i].bottom_space_default = true;
4397 else if (isValidLength(value, &len))
4398 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4399 tabular.row_info[i].bottom_space_default = false;
4400 tabular.row_info[i].bottom_space = len;
4403 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4404 tabular.row_info[i].bottom_space_default = false;
4405 tabular.row_info[i].bottom_space = len;
4410 case Tabular::SET_INTERLINE_SPACE: {
4412 if (value == "default")
4413 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4414 tabular.row_info[i].interline_space_default = true;
4415 else if (isValidLength(value, &len))
4416 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4417 tabular.row_info[i].interline_space_default = false;
4418 tabular.row_info[i].interline_space = len;
4421 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4422 tabular.row_info[i].interline_space_default = false;
4423 tabular.row_info[i].interline_space = len;
4428 // dummy stuff just to avoid warnings
4429 case Tabular::LAST_ACTION:
4435 bool InsetTabular::showInsetDialog(BufferView * bv) const
4437 InsetTabularMailer(*this).showDialog(bv);
4442 void InsetTabular::openLayoutDialog(BufferView * bv) const
4444 InsetTabularMailer(*this).showDialog(bv);
4448 bool InsetTabular::copySelection(Cursor & cur)
4450 if (!cur.selection())
4455 getSelection(cur, rs, re, cs, ce);
4457 paste_tabular.reset(new Tabular(tabular));
4459 for (row_type i = 0; i < rs; ++i)
4460 paste_tabular->deleteRow(0);
4462 row_type const rows = re - rs + 1;
4463 while (paste_tabular->rowCount() > rows)
4464 paste_tabular->deleteRow(rows);
4466 for (col_type i = 0; i < cs; ++i)
4467 paste_tabular->deleteColumn(0);
4469 col_type const columns = ce - cs + 1;
4470 while (paste_tabular->columnCount() > columns)
4471 paste_tabular->deleteColumn(columns);
4473 // We clear all the InsetTableCell pointers, since they
4474 // might now become invalid and there is no point in having
4475 // them point to temporary things in paste_tabular.
4476 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4477 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4478 paste_tabular->getCellInset(i,j)->setCellData(0);
4479 paste_tabular->getCellInset(i,j)->setTabular(0);
4482 odocstringstream os;
4483 OutputParams const runparams(0);
4484 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4485 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4486 cap::copySelection(cur, os.str());
4488 // mark tabular stack dirty
4489 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4490 // when we (hopefully) have a one-for-all paste mechanism.
4491 // This must be called after cap::copySelection.
4492 dirtyTabularStack(true);
4498 bool InsetTabular::pasteClipboard(Cursor & cur)
4502 col_type const actcol = tabular.cellColumn(cur.idx());
4503 row_type const actrow = tabular.cellRow(cur.idx());
4504 for (row_type r1 = 0, r2 = actrow;
4505 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4507 for (col_type c1 = 0, c2 = actcol;
4508 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4510 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4511 tabular.isPartOfMultiColumn(r2, c2))
4513 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4517 if (tabular.isPartOfMultiColumn(r2, c2)) {
4521 shared_ptr<InsetTableCell> inset(
4522 new InsetTableCell(*paste_tabular->getCellInset(r1, c1)));
4523 // note that setCellInset will call InsetTableCell::setCellData()
4524 // and InsetTableCell::setTabular()
4525 tabular.setCellInset(r2, c2, inset);
4526 // FIXME: change tracking (MG)
4527 inset->setChange(Change(cur.buffer().params().trackChanges ?
4528 Change::INSERTED : Change::UNCHANGED));
4536 void InsetTabular::cutSelection(Cursor & cur)
4538 if (!cur.selection())
4543 getSelection(cur, rs, re, cs, ce);
4544 for (row_type i = rs; i <= re; ++i) {
4545 for (col_type j = cs; j <= ce; ++j) {
4546 shared_ptr<InsetTableCell> t
4547 = cell(tabular.cellIndex(i, j));
4548 if (cur.buffer().params().trackChanges)
4549 // FIXME: Change tracking (MG)
4550 t->setChange(Change(Change::DELETED));
4556 // cursor position might be invalid now
4557 if (cur.pit() > cur.lastpit())
4558 cur.pit() = cur.lastpit();
4559 if (cur.pos() > cur.lastpos())
4560 cur.pos() = cur.lastpos();
4561 cur.clearSelection();
4565 bool InsetTabular::isRightToLeft(Cursor & cur) const
4567 BOOST_ASSERT(cur.depth() > 1);
4568 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4569 pos_type const parentpos = cur[cur.depth() - 2].pos();
4570 return parentpar.getFontSettings(cur.bv().buffer().params(),
4571 parentpos).language()->rightToLeft();
4575 void InsetTabular::getSelection(Cursor & cur,
4576 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4578 CursorSlice const & beg = cur.selBegin();
4579 CursorSlice const & end = cur.selEnd();
4580 cs = tabular.cellColumn(beg.idx());
4581 ce = tabular.cellColumn(end.idx());
4584 cs = tabular.cellColumn(end.idx());
4586 ce = tabular.cellRightColumn(end.idx());
4589 rs = tabular.cellRow(beg.idx());
4590 re = tabular.cellRow(end.idx());
4596 Text * InsetTabular::getText(int idx) const
4598 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4602 void InsetTabular::setChange(Change const & change)
4604 for (idx_type idx = 0; idx < nargs(); ++idx)
4605 cell(idx)->setChange(change);
4609 void InsetTabular::acceptChanges(BufferParams const & bparams)
4611 for (idx_type idx = 0; idx < nargs(); ++idx)
4612 cell(idx)->acceptChanges(bparams);
4616 void InsetTabular::rejectChanges(BufferParams const & bparams)
4618 for (idx_type idx = 0; idx < nargs(); ++idx)
4619 cell(idx)->rejectChanges(bparams);
4623 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4625 return tabular.getPWidth(cell).zero();
4629 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4631 return !tabular.getPWidth(cell).zero();
4635 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4638 if (buf.length() <= 0)
4643 col_type maxCols = 1;
4644 size_t const len = buf.length();
4648 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4656 maxCols = max(cols, maxCols);
4662 maxCols = max(cols, maxCols);
4668 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4669 loctab = paste_tabular.get();
4671 dirtyTabularStack(true);
4674 cell = bv.cursor().idx();
4675 ocol = tabular.cellColumn(cell);
4676 row = tabular.cellRow(cell);
4680 idx_type const cells = loctab->cellCount();
4683 rows = loctab->rowCount();
4684 col_type const columns = loctab->columnCount();
4686 while (cell < cells && p < len && row < rows &&
4687 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4693 // we can only set this if we are not too far right
4694 if (cols < columns) {
4695 shared_ptr<InsetTableCell> inset = loctab->getCellInset(cell);
4696 Font const font = bv.textMetrics(&inset->text_).
4698 inset->setText(buf.substr(op, p - op), font,
4699 buffer().params().trackChanges);
4705 // we can only set this if we are not too far right
4706 if (cols < columns) {
4707 shared_ptr<InsetTableCell> inset = tabular.getCellInset(cell);
4708 Font const font = bv.textMetrics(&inset->text_).
4710 inset->setText(buf.substr(op, p - op), font,
4711 buffer().params().trackChanges);
4716 cell = loctab->cellIndex(row, cols);
4722 // check for the last cell if there is no trailing '\n'
4723 if (cell < cells && op < len) {
4724 shared_ptr<InsetTableCell> inset = loctab->getCellInset(cell);
4725 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4726 inset->setText(buf.substr(op, len - op), font,
4727 buffer().params().trackChanges);
4733 void InsetTabular::addPreview(PreviewLoader & loader) const
4735 row_type const rows = tabular.rowCount();
4736 col_type const columns = tabular.columnCount();
4737 for (row_type i = 0; i < rows; ++i) {
4738 for (col_type j = 0; j < columns; ++j)
4739 tabular.getCellInset(i, j)->addPreview(loader);
4744 bool InsetTabular::tablemode(Cursor & cur) const
4746 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4750 bool InsetTabular::completionSupported(Cursor const & cur) const
4752 Cursor const & bvCur = cur.bv().cursor();
4753 if (&bvCur.inset() != this)
4755 return cur.text()->completionSupported(cur);
4759 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4761 return completionSupported(cur);
4765 bool InsetTabular::automaticInlineCompletion() const
4767 return lyxrc.completion_inline_text;
4771 bool InsetTabular::automaticPopupCompletion() const
4773 return lyxrc.completion_popup_text;
4777 bool InsetTabular::showCompletionCursor() const
4779 return lyxrc.completion_cursor_text;
4783 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4785 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4789 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4791 if (!completionSupported(cur))
4793 return cur.text()->completionPrefix(cur);
4797 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4799 if (!completionSupported(cur))
4802 return cur.text()->insertCompletion(cur, s, finished);
4806 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4807 Dimension & dim) const
4809 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4810 tm.completionPosAndDim(cur, x, y, dim);
4814 string const InsetTabularMailer::name_("tabular");
4816 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4817 : inset_(const_cast<InsetTabular &>(inset))
4821 string const InsetTabularMailer::inset2string(Buffer const &) const
4823 return params2string(inset_);
4827 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4829 istringstream data(in);
4831 lex.setStream(data);
4838 if (!lex || token != name_)
4839 return print_mailer_error("InsetTabularMailer", in, 1,
4842 // This is part of the inset proper that is usually swallowed
4843 // by Buffer::readInset
4845 if (!lex || token != "Tabular")
4846 return print_mailer_error("InsetTabularMailer", in, 2, "Tabular");
4852 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4855 data << name_ << ' ';
4857 data << "\\end_inset\n";