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(cell).top_line;
773 bool Tabular::bottomLine(idx_type const cell) const
775 return cellInfo(cell).bottom_line;
779 bool Tabular::leftLine(idx_type cell) const
783 return cellInfo(cell).left_line;
787 bool Tabular::rightLine(idx_type cell) const
791 return cellInfo(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 rightLine(i) && !leftLine(cell);
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 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
839 && nextcol < columnCount() && leftLine(cellIndex(cellRow(cell), nextcol)))
840 return WIDTH_OF_LINE;
845 int Tabular::columnWidth(idx_type cell) const
848 col_type const span = columnSpan(cell);
849 col_type const col = cellColumn(cell);
850 for(col_type c = col; c < col + span ; ++c)
851 w += column_info[c].width;
856 bool Tabular::updateColumnWidths()
858 col_type const ncols = columnCount();
859 row_type const nrows = rowCount();
861 // for each col get max of single col cells
862 for(col_type c = 0; c < ncols; ++c) {
864 for(row_type r = 0; r < nrows; ++r) {
865 idx_type const i = cellIndex(r, c);
866 if (columnSpan(i) == 1)
867 new_width = max(new_width, cellInfo(i).width);
870 if (column_info[c].width != new_width) {
871 column_info[c].width = new_width;
875 // update col widths to fit merged cells
876 for(col_type c = 0; c < ncols; ++c)
877 for(row_type r = 0; r < nrows; ++r) {
878 idx_type const i = cellIndex(r, c);
879 int const span = columnSpan(i);
880 if (span == 1 || c > cellColumn(i))
884 for(col_type j = c; j < c + span ; ++j)
885 old_width += column_info[j].width;
887 if (cellInfo(i).width > old_width) {
888 column_info[c + span - 1].width += cellInfo(i).width - old_width;
897 int Tabular::width() const
899 col_type const ncols = columnCount();
901 for (col_type i = 0; i < ncols; ++i)
902 width += column_info[i].width;
907 void Tabular::setCellWidth(idx_type cell, int new_width)
909 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
910 + getAdditionalWidth(cell);
914 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
917 if (!isMultiColumn(cell) || onlycolumn)
918 column_info[cellColumn(cell)].alignment = align;
920 cellInfo(cell).alignment = align;
924 void Tabular::setVAlignment(idx_type cell, VAlignment align,
927 if (!isMultiColumn(cell) || onlycolumn)
928 column_info[cellColumn(cell)].valignment = align;
930 cellInfo(cell).valignment = align;
937 * Allow line and paragraph breaks for fixed width cells or disallow them,
938 * merge cell paragraphs and reset layout to standard for variable width
941 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
943 inset->setAutoBreakRows(fixedWidth);
947 // merge all paragraphs to one
948 BufferParams const & bp = cur.bv().buffer().params();
949 while (inset->paragraphs().size() > 1)
950 mergeParagraph(bp, inset->paragraphs(), 0);
954 // undo information has already been recorded
955 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
956 bp.documentClass().emptyLayoutName());
963 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
964 Length const & width)
966 col_type const j = cellColumn(cell);
968 column_info[j].p_width = width;
969 for (row_type i = 0; i < rowCount(); ++i) {
970 idx_type const cell = cellIndex(i, j);
971 // because of multicolumns
972 toggleFixedWidth(cur, cellInset(cell).get(),
973 !getPWidth(cell).zero());
975 // cur paragraph can become invalid after paragraphs were merged
976 if (cur.pit() > cur.lastpit())
977 cur.pit() = cur.lastpit();
978 // cur position can become invalid after newlines were removed
979 if (cur.pos() > cur.lastpos())
980 cur.pos() = cur.lastpos();
984 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
985 Length const & width)
987 if (!isMultiColumn(cell))
990 cellInfo(cell).p_width = width;
991 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
992 // cur paragraph can become invalid after paragraphs were merged
993 if (cur.pit() > cur.lastpit())
994 cur.pit() = cur.lastpit();
995 // cur position can become invalid after newlines were removed
996 if (cur.pos() > cur.lastpos())
997 cur.pos() = cur.lastpos();
1002 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1003 Tabular::Feature what)
1005 if (what == SET_SPECIAL_MULTI)
1006 cellInfo(cell).align_special = special;
1008 column_info[cellColumn(cell)].align_special = special;
1012 void Tabular::setAllLines(idx_type cell, bool line)
1014 setTopLine(cell, line);
1015 setBottomLine(cell, line);
1016 setRightLine(cell, line);
1017 setLeftLine(cell, line);
1021 void Tabular::setTopLine(idx_type i, bool line)
1023 cellInfo(i).top_line = line;
1027 void Tabular::setBottomLine(idx_type i, bool line)
1029 cellInfo(i).bottom_line = line;
1033 void Tabular::setLeftLine(idx_type cell, bool line)
1035 cellInfo(cell).left_line = line;
1039 void Tabular::setRightLine(idx_type cell, bool line)
1041 cellInfo(cell).right_line = line;
1044 bool Tabular::rowTopLine(row_type r) const
1046 idx_type i0 = getFirstCellInRow(r);
1047 idx_type i1 = getLastCellInRow(r);
1048 bool all_rows_set = true;
1049 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1050 all_rows_set = cellInfo(j).top_line;
1051 return all_rows_set;
1055 bool Tabular::rowBottomLine(row_type r) const
1057 idx_type i0 = getFirstCellInRow(r);
1058 idx_type i1 = getLastCellInRow(r);
1059 bool all_rows_set = true;
1060 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1061 all_rows_set = cellInfo(j).bottom_line;
1062 return all_rows_set;
1066 bool Tabular::columnLeftLine(col_type c) const
1068 bool all_cols_set = true;
1069 row_type const nrows = rowCount();
1070 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1071 idx_type i = cellIndex(r, c);
1072 if (c == cellColumn(i))
1073 all_cols_set = cellInfo(i).left_line;
1075 return all_cols_set;
1079 bool Tabular::columnRightLine(col_type c) const
1081 bool all_cols_set = true;
1082 row_type const nrows = rowCount();
1083 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1084 idx_type i = cellIndex(r, c);
1085 if (c == cellColumn(i) + columnSpan(i) - 1)
1086 all_cols_set = cellInfo(i).right_line;
1088 return all_cols_set;
1092 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1094 if (!onlycolumn && isMultiColumn(cell))
1095 return cellInfo(cell).alignment;
1096 return column_info[cellColumn(cell)].alignment;
1101 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1103 if (!onlycolumn && isMultiColumn(cell))
1104 return cellInfo(cell).valignment;
1105 return column_info[cellColumn(cell)].valignment;
1109 Length const Tabular::getPWidth(idx_type cell) const
1111 if (isMultiColumn(cell))
1112 return cellInfo(cell).p_width;
1113 return column_info[cellColumn(cell)].p_width;
1117 Length const Tabular::getColumnPWidth(idx_type cell) const
1119 return column_info[cellColumn(cell)].p_width;
1123 Length const Tabular::getMColumnPWidth(idx_type cell) const
1125 if (isMultiColumn(cell))
1126 return cellInfo(cell).p_width;
1131 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1133 if (what == SET_SPECIAL_MULTI)
1134 return cellInfo(cell).align_special;
1135 return column_info[cellColumn(cell)].align_special;
1139 int Tabular::cellWidth(idx_type cell) const
1141 return cellInfo(cell).width;
1145 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1149 switch (getAlignment(cell)) {
1150 case LYX_ALIGN_CENTER:
1151 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1153 case LYX_ALIGN_RIGHT:
1154 x += columnWidth(cell) - cellWidth(cell);
1155 // + getAdditionalWidth(cell);
1158 // LYX_ALIGN_LEFT: nothing :-)
1162 // the LaTeX Way :-(
1168 bool Tabular::isFirstCellInRow(idx_type cell) const
1170 return cellColumn(cell) == 0;
1174 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1176 if (row > rowCount() - 1)
1177 row = rowCount() - 1;
1178 return cell_info[row][0].cellno;
1182 bool Tabular::isLastCellInRow(idx_type cell) const
1184 return cellRightColumn(cell) == columnCount() - 1;
1188 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1190 if (row > rowCount() - 1)
1191 row = rowCount() - 1;
1192 return cell_info[row][columnCount() - 1].cellno;
1196 Tabular::row_type Tabular::cellRow(idx_type cell) const
1198 if (cell >= cellCount())
1199 return rowCount() - 1;
1202 return rowofcell[cell];
1206 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1208 if (cell >= cellCount())
1209 return columnCount() - 1;
1212 return columnofcell[cell];
1216 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1218 row_type const row = cellRow(cell);
1219 col_type column = cellColumn(cell);
1220 while (column < columnCount() - 1 &&
1221 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1227 void Tabular::write(ostream & os) const
1231 << write_attribute("version", 3)
1232 << write_attribute("rows", rowCount())
1233 << write_attribute("columns", columnCount())
1235 // global longtable options
1237 << write_attribute("rotate", rotate)
1238 << write_attribute("booktabs", use_booktabs)
1239 << write_attribute("islongtable", is_long_tabular)
1240 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1241 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1242 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1243 << write_attribute("headTopDL", endhead.topDL)
1244 << write_attribute("headBottomDL", endhead.bottomDL)
1245 << write_attribute("footTopDL", endfoot.topDL)
1246 << write_attribute("footBottomDL", endfoot.bottomDL)
1247 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1248 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1249 << write_attribute("lastFootEmpty", endlastfoot.empty)
1251 for (col_type j = 0; j < columnCount(); ++j) {
1253 << write_attribute("alignment", column_info[j].alignment)
1254 << write_attribute("valignment", column_info[j].valignment)
1255 << write_attribute("width", column_info[j].p_width.asString())
1256 << write_attribute("special", column_info[j].align_special)
1259 for (row_type i = 0; i < rowCount(); ++i) {
1260 static const string def("default");
1262 if (row_info[i].top_space_default)
1263 os << write_attribute("topspace", def);
1265 os << write_attribute("topspace", row_info[i].top_space);
1266 if (row_info[i].bottom_space_default)
1267 os << write_attribute("bottomspace", def);
1269 os << write_attribute("bottomspace", row_info[i].bottom_space);
1270 if (row_info[i].interline_space_default)
1271 os << write_attribute("interlinespace", def);
1273 os << write_attribute("interlinespace", row_info[i].interline_space);
1274 os << write_attribute("endhead", row_info[i].endhead)
1275 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1276 << write_attribute("endfoot", row_info[i].endfoot)
1277 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1278 << write_attribute("newpage", row_info[i].newpage)
1280 for (col_type j = 0; j < columnCount(); ++j) {
1282 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1283 << write_attribute("alignment", cell_info[i][j].alignment)
1284 << write_attribute("valignment", cell_info[i][j].valignment)
1285 << write_attribute("topline", cell_info[i][j].top_line)
1286 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1287 << write_attribute("leftline", cell_info[i][j].left_line)
1288 << write_attribute("rightline", cell_info[i][j].right_line)
1289 << write_attribute("rotate", cell_info[i][j].rotate)
1290 << write_attribute("usebox", cell_info[i][j].usebox)
1291 << write_attribute("width", cell_info[i][j].p_width)
1292 << write_attribute("special", cell_info[i][j].align_special)
1294 os << "\\begin_inset ";
1295 cell_info[i][j].inset->write(os);
1296 os << "\n\\end_inset\n"
1301 os << "</lyxtabular>\n";
1305 void Tabular::read(Lexer & lex)
1308 istream & is = lex.getStream();
1310 l_getline(is, line);
1311 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1312 BOOST_ASSERT(false);
1317 if (!getTokenValue(line, "version", version))
1319 BOOST_ASSERT(version >= 2);
1322 if (!getTokenValue(line, "rows", rows_arg))
1325 if (!getTokenValue(line, "columns", columns_arg))
1327 init(buffer(), rows_arg, columns_arg);
1328 l_getline(is, line);
1329 if (!prefixIs(line, "<features")) {
1330 lyxerr << "Wrong tabular format (expected <features ...> got"
1331 << line << ')' << endl;
1334 getTokenValue(line, "rotate", rotate);
1335 getTokenValue(line, "booktabs", use_booktabs);
1336 getTokenValue(line, "islongtable", is_long_tabular);
1337 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1338 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1339 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1340 getTokenValue(line, "headTopDL", endhead.topDL);
1341 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1342 getTokenValue(line, "footTopDL", endfoot.topDL);
1343 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1344 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1345 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1346 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1348 for (col_type j = 0; j < columnCount(); ++j) {
1350 if (!prefixIs(line,"<column")) {
1351 lyxerr << "Wrong tabular format (expected <column ...> got"
1352 << line << ')' << endl;
1355 getTokenValue(line, "alignment", column_info[j].alignment);
1356 getTokenValue(line, "valignment", column_info[j].valignment);
1357 getTokenValue(line, "width", column_info[j].p_width);
1358 getTokenValue(line, "special", column_info[j].align_special);
1361 for (row_type i = 0; i < rowCount(); ++i) {
1362 l_getline(is, line);
1363 if (!prefixIs(line, "<row")) {
1364 lyxerr << "Wrong tabular format (expected <row ...> got"
1365 << line << ')' << endl;
1368 getTokenValue(line, "topspace", row_info[i].top_space,
1369 row_info[i].top_space_default);
1370 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1371 row_info[i].bottom_space_default);
1372 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1373 row_info[i].interline_space_default);
1374 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1375 getTokenValue(line, "endhead", row_info[i].endhead);
1376 getTokenValue(line, "endfoot", row_info[i].endfoot);
1377 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1378 getTokenValue(line, "newpage", row_info[i].newpage);
1379 for (col_type j = 0; j < columnCount(); ++j) {
1380 l_getline(is, line);
1381 if (!prefixIs(line, "<cell")) {
1382 lyxerr << "Wrong tabular format (expected <cell ...> got"
1383 << line << ')' << endl;
1386 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1387 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1388 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1389 getTokenValue(line, "topline", cell_info[i][j].top_line);
1390 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1391 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1392 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1393 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1394 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1395 getTokenValue(line, "width", cell_info[i][j].p_width);
1396 getTokenValue(line, "special", cell_info[i][j].align_special);
1397 l_getline(is, line);
1398 if (prefixIs(line, "\\begin_inset")) {
1399 cell_info[i][j].inset->read(lex);
1400 l_getline(is, line);
1402 if (!prefixIs(line, "</cell>")) {
1403 lyxerr << "Wrong tabular format (expected </cell> got"
1404 << line << ')' << endl;
1408 l_getline(is, line);
1409 if (!prefixIs(line, "</row>")) {
1410 lyxerr << "Wrong tabular format (expected </row> got"
1411 << line << ')' << endl;
1415 while (!prefixIs(line, "</lyxtabular>")) {
1416 l_getline(is, line);
1422 bool Tabular::isMultiColumn(idx_type cell) const
1424 return cellInfo(cell).multicolumn != CELL_NORMAL;
1428 bool Tabular::isMultiColumnReal(idx_type cell) const
1430 return cellColumn(cell) != cellRightColumn(cell) &&
1431 cellInfo(cell).multicolumn != CELL_NORMAL;
1435 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1437 return cell_info[cellRow(cell)][cellColumn(cell)];
1441 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1443 CellData & cs = cellInfo(cell);
1444 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1445 cs.alignment = column_info[cellColumn(cell)].alignment;
1446 for (idx_type i = 1; i < number; ++i) {
1447 CellData & cs1 = cellInfo(cell + i);
1448 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1449 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1452 setRightLine(cell, rightLine(cell + number - 1));
1457 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1459 row_type const row = cellRow(cell);
1460 col_type const ncols = columnCount();
1461 idx_type result = 1;
1462 col_type column = cellColumn(cell) + 1;
1463 while (column < ncols && isPartOfMultiColumn(row, column)) {
1471 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1473 row_type const row = cellRow(cell);
1474 col_type column = cellColumn(cell);
1476 idx_type result = 0;
1478 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1479 cell_info[row][column].multicolumn = CELL_NORMAL;
1481 while (column < columnCount() &&
1482 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1484 cell_info[row][column].multicolumn = CELL_NORMAL;
1494 void Tabular::setBookTabs(bool what)
1496 use_booktabs = what;
1500 bool Tabular::useBookTabs() const
1502 return use_booktabs;
1506 void Tabular::setLongTabular(bool what)
1508 is_long_tabular = what;
1512 bool Tabular::isLongTabular() const
1514 return is_long_tabular;
1518 void Tabular::setRotateTabular(bool flag)
1524 bool Tabular::getRotateTabular() const
1530 void Tabular::setRotateCell(idx_type cell, bool flag)
1532 cellInfo(cell).rotate = flag;
1536 bool Tabular::getRotateCell(idx_type cell) const
1538 return cellInfo(cell).rotate;
1542 bool Tabular::needRotating() const
1546 for (row_type i = 0; i < rowCount(); ++i)
1547 for (col_type j = 0; j < columnCount(); ++j)
1548 if (cell_info[i][j].rotate)
1554 bool Tabular::isLastCell(idx_type cell) const
1556 if (cell + 1 < cellCount())
1562 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1564 if (cellRow(cell) > 0)
1565 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1570 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1572 if (cellRow(cell) + 1 < rowCount())
1573 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1578 Tabular::idx_type Tabular::cellIndex(row_type row,
1579 col_type column) const
1581 BOOST_ASSERT(column != npos && column < columnCount() &&
1582 row != npos && row < rowCount());
1583 return cell_info[row][column].cellno;
1587 void Tabular::setUsebox(idx_type cell, BoxType type)
1589 cellInfo(cell).usebox = type;
1593 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1595 if (column_info[cellColumn(cell)].p_width.zero() &&
1596 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1598 if (cellInfo(cell).usebox > 1)
1599 return cellInfo(cell).usebox;
1600 return useParbox(cell);
1605 // This are functions used for the longtable support
1607 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1613 row_info[row].endfirsthead = flag;
1617 row_info[row].endhead = flag;
1622 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1625 hd.set = haveLTHead();
1626 return row_info[row].endhead;
1630 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1633 hd.set = haveLTFirstHead();
1634 return row_info[row].endfirsthead;
1638 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1644 row_info[row].endlastfoot = flag;
1648 row_info[row].endfoot = flag;
1653 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1656 fd.set = haveLTFoot();
1657 return row_info[row].endfoot;
1661 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1664 fd.set = haveLTLastFoot();
1665 return row_info[row].endlastfoot;
1669 void Tabular::setLTNewPage(row_type row, bool what)
1671 row_info[row].newpage = what;
1675 bool Tabular::getLTNewPage(row_type row) const
1677 return row_info[row].newpage;
1681 bool Tabular::haveLTHead() const
1683 for (row_type i = 0; i < rowCount(); ++i)
1684 if (row_info[i].endhead)
1690 bool Tabular::haveLTFirstHead() const
1692 if (endfirsthead.empty)
1694 for (row_type i = 0; i < rowCount(); ++i)
1695 if (row_info[i].endfirsthead)
1701 bool Tabular::haveLTFoot() const
1703 for (row_type i = 0; i < rowCount(); ++i)
1704 if (row_info[i].endfoot)
1710 bool Tabular::haveLTLastFoot() const
1712 if (endlastfoot.empty)
1714 for (row_type i = 0; i < rowCount(); ++i)
1715 if (row_info[i].endlastfoot)
1721 // end longtable support functions
1723 void Tabular::setRowAscent(row_type row, int height)
1725 if (row >= rowCount() || row_info[row].ascent == height)
1727 row_info[row].ascent = height;
1731 void Tabular::setRowDescent(row_type row, int height)
1733 if (row >= rowCount() || row_info[row].descent == height)
1735 row_info[row].descent = height;
1739 int Tabular::rowAscent(row_type row) const
1741 if (row >= rowCount())
1743 return row_info[row].ascent;
1747 int Tabular::rowDescent(row_type row) const
1749 BOOST_ASSERT(row < rowCount());
1750 return row_info[row].descent;
1754 int Tabular::height() const
1757 for (row_type row = 0; row < rowCount(); ++row)
1758 height += rowAscent(row) + rowDescent(row) +
1759 getAdditionalHeight(row);
1764 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1766 BOOST_ASSERT(row < rowCount());
1767 BOOST_ASSERT(column < columnCount());
1768 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1772 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1774 // FIXME: assert or return 0 as in TeXBottomHLine()?
1775 BOOST_ASSERT(row != npos);
1776 BOOST_ASSERT(row < rowCount());
1778 idx_type const fcell = getFirstCellInRow(row);
1779 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1782 for (idx_type i = fcell; i < n; ++i) {
1786 if (use_booktabs && row == 0) {
1789 } else if (tmp == n - fcell) {
1790 os << (use_booktabs ? "\\midrule " : "\\hline ");
1792 for (idx_type i = fcell; i < n; ++i) {
1794 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1795 << cellColumn(i) + 1
1797 << cellRightColumn(i) + 1
1809 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1811 // FIXME: return 0 or assert as in TeXTopHLine()?
1812 if (row == npos || row >= rowCount())
1815 idx_type const fcell = getFirstCellInRow(row);
1816 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1819 for (idx_type i = fcell; i < n; ++i) {
1823 if (use_booktabs && row == rowCount() - 1) {
1824 if (bottomLine(fcell))
1825 os << "\\bottomrule";
1826 } else if (tmp == n - fcell) {
1827 os << (use_booktabs ? "\\midrule" : "\\hline");
1829 for (idx_type i = fcell; i < n; ++i) {
1830 if (bottomLine(i)) {
1831 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1832 << cellColumn(i) + 1
1834 << cellRightColumn(i) + 1
1846 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1850 if (getRotateCell(cell)) {
1851 os << "\\begin{sideways}\n";
1854 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1855 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1856 // figure out how to set the lines
1857 // we always set double lines to the right of the cell
1858 row_type const r = cellRow(cell);
1859 col_type const c = cellColumn(cell);
1860 col_type const nextcol = c + columnSpan(cell);
1861 bool prevmulticol = ismulticol;
1862 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1863 bool nextcellleft = nextcol < columnCount()
1864 && leftLine(cellIndex(r, nextcol));
1865 bool nextcolleft = nextcol < columnCount() && columnLeftLine(nextcol);
1866 bool coldouble = columnRightLine(c) && nextcolleft;
1867 bool celldouble = rightLine(cell) && nextcellleft;
1868 bool forceleft = prevmulticol && !prevcellright && leftLine(cell);
1869 bool doubleright = celldouble && (isMultiColumn(cell) || !coldouble);
1870 ismulticol = isMultiColumn(cell)
1871 || (leftLine(cell) && !columnLeftLine(c))
1872 || (rightLine(cell) && !columnRightLine(c))
1873 || (!celldouble && coldouble)
1874 || doubleright || forceleft;
1876 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1877 if (leftLine(cell) || forceleft)
1879 if (!cellInfo(cell).align_special.empty()) {
1880 os << cellInfo(cell).align_special;
1882 if (!getPWidth(cell).zero()) {
1884 case LYX_VALIGN_TOP:
1887 case LYX_VALIGN_MIDDLE:
1890 case LYX_VALIGN_BOTTOM:
1895 << from_ascii(getPWidth(cell).asLatexString())
1899 case LYX_ALIGN_LEFT:
1902 case LYX_ALIGN_RIGHT:
1909 } // end if else !getPWidth
1910 } // end if else !cellinfo_of_cell
1911 if (rightLine(cell))
1914 // add extra vertical line if we want a double one
1918 if (getUsebox(cell) == BOX_PARBOX) {
1921 case LYX_VALIGN_TOP:
1924 case LYX_VALIGN_MIDDLE:
1927 case LYX_VALIGN_BOTTOM:
1931 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1933 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1934 os << "\\begin{minipage}[";
1936 case LYX_VALIGN_TOP:
1939 case LYX_VALIGN_MIDDLE:
1942 case LYX_VALIGN_BOTTOM:
1946 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1954 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1959 if (getUsebox(cell) == BOX_PARBOX)
1961 else if (getUsebox(cell) == BOX_MINIPAGE) {
1962 os << "%\n\\end{minipage}";
1968 if (getRotateCell(cell)) {
1969 os << "%\n\\end{sideways}";
1976 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1977 OutputParams const & runparams) const
1979 if (!is_long_tabular)
1983 // output header info
1985 if (endhead.topDL) {
1989 for (row_type i = 0; i < rowCount(); ++i) {
1990 if (row_info[i].endhead) {
1991 ret += TeXRow(os, i, runparams);
1994 if (endhead.bottomDL) {
1998 os << "\\endhead\n";
2000 if (endfirsthead.empty) {
2001 os << "\\endfirsthead\n";
2005 // output firstheader info
2006 if (haveLTFirstHead()) {
2007 if (endfirsthead.topDL) {
2011 for (row_type i = 0; i < rowCount(); ++i) {
2012 if (row_info[i].endfirsthead) {
2013 ret += TeXRow(os, i, runparams);
2016 if (endfirsthead.bottomDL) {
2020 os << "\\endfirsthead\n";
2023 // output footer info
2025 if (endfoot.topDL) {
2029 for (row_type i = 0; i < rowCount(); ++i) {
2030 if (row_info[i].endfoot) {
2031 ret += TeXRow(os, i, runparams);
2034 if (endfoot.bottomDL) {
2038 os << "\\endfoot\n";
2040 if (endlastfoot.empty) {
2041 os << "\\endlastfoot\n";
2045 // output lastfooter info
2046 if (haveLTLastFoot()) {
2047 if (endlastfoot.topDL) {
2051 for (row_type i = 0; i < rowCount(); ++i) {
2052 if (row_info[i].endlastfoot) {
2053 ret += TeXRow(os, i, runparams);
2056 if (endlastfoot.bottomDL) {
2060 os << "\\endlastfoot\n";
2067 bool Tabular::isValidRow(row_type row) const
2069 if (!is_long_tabular)
2071 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2072 !row_info[row].endfoot && !row_info[row].endlastfoot;
2076 int Tabular::TeXRow(odocstream & os, row_type i,
2077 OutputParams const & runparams) const
2079 idx_type cell = cellIndex(i, 0);
2080 int ret = TeXTopHLine(os, i);
2081 if (row_info[i].top_space_default) {
2083 os << "\\addlinespace\n";
2085 os << "\\noalign{\\vskip\\doublerulesep}\n";
2087 } else if(!row_info[i].top_space.zero()) {
2089 os << "\\addlinespace["
2090 << from_ascii(row_info[i].top_space.asLatexString())
2093 os << "\\noalign{\\vskip"
2094 << from_ascii(row_info[i].top_space.asLatexString())
2099 bool ismulticol = false;
2100 for (col_type j = 0; j < columnCount(); ++j) {
2101 if (isPartOfMultiColumn(i, j))
2103 ret += TeXCellPreamble(os, cell, ismulticol);
2104 shared_ptr<InsetTableCell> inset = cellInset(cell);
2106 Paragraph const & par = inset->paragraphs().front();
2107 bool rtl = par.isRTL(buffer().params())
2109 && getPWidth(cell).zero();
2112 if (par.getParLanguage(buffer().params())->lang() ==
2115 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2117 // currently, remaning RTL languages are arabic_arabtex and hebrew
2121 ret += inset->latex(os, runparams);
2125 ret += TeXCellPostamble(os, cell, ismulticol);
2126 if (!isLastCellInRow(cell)) { // not last cell in row
2131 os << "\\tabularnewline";
2132 if (row_info[i].bottom_space_default) {
2134 os << "\\addlinespace";
2136 os << "[\\doublerulesep]";
2137 } else if (!row_info[i].bottom_space.zero()) {
2139 os << "\\addlinespace";
2141 << from_ascii(row_info[i].bottom_space.asLatexString())
2146 ret += TeXBottomHLine(os, i);
2147 if (row_info[i].interline_space_default) {
2149 os << "\\addlinespace\n";
2151 os << "\\noalign{\\vskip\\doublerulesep}\n";
2153 } else if (!row_info[i].interline_space.zero()) {
2155 os << "\\addlinespace["
2156 << from_ascii(row_info[i].interline_space.asLatexString())
2159 os << "\\noalign{\\vskip"
2160 << from_ascii(row_info[i].interline_space.asLatexString())
2168 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2172 //+---------------------------------------------------------------------
2173 //+ first the opening preamble +
2174 //+---------------------------------------------------------------------
2177 os << "\\begin{sideways}\n";
2180 if (is_long_tabular)
2181 os << "\\begin{longtable}{";
2183 os << "\\begin{tabular}{";
2185 for (col_type i = 0; i < columnCount(); ++i) {
2186 if (!use_booktabs && columnLeftLine(i))
2188 if (!column_info[i].align_special.empty()) {
2189 os << column_info[i].align_special;
2191 if (!column_info[i].p_width.zero()) {
2192 switch (column_info[i].alignment) {
2193 case LYX_ALIGN_LEFT:
2194 os << ">{\\raggedright}";
2196 case LYX_ALIGN_RIGHT:
2197 os << ">{\\raggedleft}";
2199 case LYX_ALIGN_CENTER:
2200 os << ">{\\centering}";
2202 case LYX_ALIGN_NONE:
2203 case LYX_ALIGN_BLOCK:
2204 case LYX_ALIGN_LAYOUT:
2205 case LYX_ALIGN_SPECIAL:
2209 switch (column_info[i].valignment) {
2210 case LYX_VALIGN_TOP:
2213 case LYX_VALIGN_MIDDLE:
2216 case LYX_VALIGN_BOTTOM:
2221 << from_ascii(column_info[i].p_width.asLatexString())
2224 switch (column_info[i].alignment) {
2225 case LYX_ALIGN_LEFT:
2228 case LYX_ALIGN_RIGHT:
2235 } // end if else !column_info[i].p_width
2236 } // end if else !column_info[i].align_special
2237 if (!use_booktabs && columnRightLine(i))
2243 ret += TeXLongtableHeaderFooter(os, runparams);
2245 //+---------------------------------------------------------------------
2246 //+ the single row and columns (cells) +
2247 //+---------------------------------------------------------------------
2249 for (row_type i = 0; i < rowCount(); ++i) {
2250 if (isValidRow(i)) {
2251 ret += TeXRow(os, i, runparams);
2252 if (is_long_tabular && row_info[i].newpage) {
2253 os << "\\newpage\n";
2259 //+---------------------------------------------------------------------
2260 //+ the closing of the tabular +
2261 //+---------------------------------------------------------------------
2263 if (is_long_tabular)
2264 os << "\\end{longtable}";
2266 os << "\\end{tabular}";
2268 os << "\n\\end{sideways}";
2276 int Tabular::docbookRow(odocstream & os, row_type row,
2277 OutputParams const & runparams) const
2280 idx_type cell = getFirstCellInRow(row);
2283 for (col_type j = 0; j < columnCount(); ++j) {
2284 if (isPartOfMultiColumn(row, j))
2287 os << "<entry align=\"";
2288 switch (getAlignment(cell)) {
2289 case LYX_ALIGN_LEFT:
2292 case LYX_ALIGN_RIGHT:
2300 os << "\" valign=\"";
2301 switch (getVAlignment(cell)) {
2302 case LYX_VALIGN_TOP:
2305 case LYX_VALIGN_BOTTOM:
2308 case LYX_VALIGN_MIDDLE:
2313 if (isMultiColumn(cell)) {
2314 os << " namest=\"col" << j << "\" ";
2315 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2319 ret += cellInset(cell)->docbook(os, runparams);
2328 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2332 //+---------------------------------------------------------------------
2333 //+ first the opening preamble +
2334 //+---------------------------------------------------------------------
2336 os << "<tgroup cols=\"" << columnCount()
2337 << "\" colsep=\"1\" rowsep=\"1\">\n";
2339 for (col_type i = 0; i < columnCount(); ++i) {
2340 os << "<colspec colname=\"col" << i << "\" align=\"";
2341 switch (column_info[i].alignment) {
2342 case LYX_ALIGN_LEFT:
2345 case LYX_ALIGN_RIGHT:
2353 if (runparams.flavor == OutputParams::XML)
2359 //+---------------------------------------------------------------------
2360 //+ Long Tabular case +
2361 //+---------------------------------------------------------------------
2363 // output header info
2364 if (haveLTHead() || haveLTFirstHead()) {
2367 for (row_type i = 0; i < rowCount(); ++i) {
2368 if (row_info[i].endhead || row_info[i].endfirsthead) {
2369 ret += docbookRow(os, i, runparams);
2375 // output footer info
2376 if (haveLTFoot() || haveLTLastFoot()) {
2379 for (row_type i = 0; i < rowCount(); ++i) {
2380 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2381 ret += docbookRow(os, i, runparams);
2388 //+---------------------------------------------------------------------
2389 //+ the single row and columns (cells) +
2390 //+---------------------------------------------------------------------
2394 for (row_type i = 0; i < rowCount(); ++i) {
2395 if (isValidRow(i)) {
2396 ret += docbookRow(os, i, runparams);
2401 //+---------------------------------------------------------------------
2402 //+ the closing of the tabular +
2403 //+---------------------------------------------------------------------
2412 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2413 vector<unsigned int> const & clen) const
2415 idx_type const fcell = getFirstCellInRow(row);
2416 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2419 for (idx_type i = fcell; i < n; ++i) {
2429 for (idx_type i = fcell; i < n; ++i) {
2440 col_type column = cellColumn(i);
2441 int len = clen[column];
2442 while (column < columnCount() - 1
2443 && isPartOfMultiColumn(row, ++column))
2444 len += clen[column] + 4;
2445 os << docstring(len, ch);
2460 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2461 vector<unsigned int> const & clen) const
2463 idx_type const fcell = getFirstCellInRow(row);
2464 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2467 for (idx_type i = fcell; i < n; ++i) {
2468 if (bottomLine(i)) {
2477 for (idx_type i = fcell; i < n; ++i) {
2478 if (bottomLine(i)) {
2488 col_type column = cellColumn(i);
2489 int len = clen[column];
2490 while (column < columnCount() -1
2491 && isPartOfMultiColumn(row, ++column))
2492 len += clen[column] + 4;
2493 os << docstring(len, ch);
2494 if (bottomLine(i)) {
2508 void Tabular::plaintextPrintCell(odocstream & os,
2509 OutputParams const & runparams,
2510 idx_type cell, row_type row, col_type column,
2511 vector<unsigned int> const & clen,
2512 bool onlydata) const
2514 odocstringstream sstr;
2515 cellInset(cell)->plaintext(sstr, runparams);
2527 unsigned int len1 = sstr.str().length();
2528 unsigned int len2 = clen[column];
2529 while (column < columnCount() -1
2530 && isPartOfMultiColumn(row, ++column))
2531 len2 += clen[column] + 4;
2534 switch (getAlignment(cell)) {
2536 case LYX_ALIGN_LEFT:
2539 case LYX_ALIGN_RIGHT:
2543 case LYX_ALIGN_CENTER:
2549 os << docstring(len1, ' ') << sstr.str()
2550 << docstring(len2, ' ');
2552 if (rightLine(cell))
2559 void Tabular::plaintext(odocstream & os,
2560 OutputParams const & runparams, int const depth,
2561 bool onlydata, char_type delim) const
2563 // first calculate the width of the single columns
2564 vector<unsigned int> clen(columnCount());
2567 // first all non (real) multicolumn cells!
2568 for (col_type j = 0; j < columnCount(); ++j) {
2570 for (row_type i = 0; i < rowCount(); ++i) {
2571 idx_type cell = cellIndex(i, j);
2572 if (isMultiColumnReal(cell))
2574 odocstringstream sstr;
2575 cellInset(cell)->plaintext(sstr, runparams);
2576 if (clen[j] < sstr.str().length())
2577 clen[j] = sstr.str().length();
2580 // then all (real) multicolumn cells!
2581 for (col_type j = 0; j < columnCount(); ++j) {
2582 for (row_type i = 0; i < rowCount(); ++i) {
2583 idx_type cell = cellIndex(i, j);
2584 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2586 odocstringstream sstr;
2587 cellInset(cell)->plaintext(sstr, runparams);
2588 int len = int(sstr.str().length());
2589 idx_type const n = columnSpan(cell);
2590 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2592 if (len > int(clen[j + n - 1]))
2593 clen[j + n - 1] = len;
2598 for (row_type i = 0; i < rowCount(); ++i) {
2599 if (!onlydata && plaintextTopHLine(os, i, clen))
2600 os << docstring(depth * 2, ' ');
2601 for (col_type j = 0; j < columnCount(); ++j) {
2602 if (isPartOfMultiColumn(i, j))
2604 if (onlydata && j > 0)
2605 // we don't use operator<< for single UCS4 character.
2606 // see explanation in docstream.h
2608 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2613 os << docstring(depth * 2, ' ');
2614 if (plaintextBottomHLine(os, i, clen))
2615 os << docstring(depth * 2, ' ');
2621 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2623 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2627 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2628 col_type column) const
2630 return cell_info[row][column].inset;
2634 void Tabular::setCellInset(row_type row, col_type column,
2635 shared_ptr<InsetTableCell> ins) const
2637 CellData & cd = cell_info[row][column];
2639 // reset the InsetTableCell's pointers
2640 ins->setCellData(&cd);
2641 ins->setTabular(this);
2646 Tabular::cellFromInset(Inset const * inset) const
2648 // is this inset part of the tabular?
2650 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2651 BOOST_ASSERT(false);
2654 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2655 if (cellInset(cell).get() == inset) {
2656 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2657 << "cell=" << cell);
2661 // We should have found a cell at this point
2662 lyxerr << "Tabular::cellFromInset: Cell of inset "
2663 << inset << " not found!" << endl;
2664 BOOST_ASSERT(false);
2670 void Tabular::validate(LaTeXFeatures & features) const
2672 features.require("NeedTabularnewline");
2674 features.require("booktabs");
2675 if (isLongTabular())
2676 features.require("longtable");
2678 features.require("rotating");
2679 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2680 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2681 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2682 features.require("array");
2683 cellInset(cell)->validate(features);
2688 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2690 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2691 ParagraphList::const_iterator cit = parlist.begin();
2692 ParagraphList::const_iterator end = parlist.end();
2694 for (; cit != end; ++cit)
2695 for (int i = 0; i < cit->size(); ++i)
2696 if (cit->isNewline(i))
2703 /////////////////////////////////////////////////////////////////////
2707 /////////////////////////////////////////////////////////////////////
2709 InsetTableCell::InsetTableCell(Buffer const & buf,
2710 Tabular::CellData const * cell, Tabular const * table)
2711 : InsetText(buf), cell_data_(cell), table_(table)
2715 bool InsetTableCell::forceEmptyLayout(idx_type) const
2717 BOOST_ASSERT(table_);
2718 BOOST_ASSERT(cell_data_);
2719 return table_->getPWidth(cell_data_->cellno).zero();
2722 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2724 BOOST_ASSERT(table_);
2725 BOOST_ASSERT(cell_data_);
2726 return !table_->getPWidth(cell_data_->cellno).zero();
2729 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2730 FuncStatus & status) const
2733 switch (cmd.action) {
2735 enabled = !forceEmptyLayout();
2737 case LFUN_LAYOUT_PARAGRAPH:
2738 enabled = allowParagraphCustomization();
2741 return InsetText::getStatus(cur, cmd, status);
2743 status.enabled(enabled);
2747 /////////////////////////////////////////////////////////////////////
2751 /////////////////////////////////////////////////////////////////////
2753 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2755 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2756 rowselect_(false), colselect_(false)
2758 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2762 InsetTabular::InsetTabular(InsetTabular const & tab)
2763 : Inset(tab), tabular(tab.tabular), scx_(0)
2765 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2769 InsetTabular::~InsetTabular()
2771 InsetTabularMailer(*this).hideDialog();
2775 bool InsetTabular::insetAllowed(InsetCode code) const
2777 if (code == MATHMACRO_CODE)
2784 void InsetTabular::write(ostream & os) const
2786 os << "Tabular" << endl;
2791 void InsetTabular::read(Lexer & lex)
2793 bool const old_format = (lex.getString() == "\\LyXTable");
2801 string token = lex.getString();
2802 while (lex && token != "\\end_inset") {
2804 token = lex.getString();
2807 lex.printError("Missing \\end_inset at this point. "
2813 int InsetTabular::rowFromY(Cursor & cur, int y) const
2815 // top y coordinate of tabular
2816 int h = yo(cur.bv()) - tabular.rowAscent(0);
2817 size_t nrows = tabular.rowCount();
2819 for (;r < nrows && y > h; ++r) {
2820 h += tabular.rowAscent(r);
2821 h += tabular.rowDescent(r);
2822 h += tabular.getAdditionalHeight(r);
2828 int InsetTabular::columnFromX(Cursor & cur, int x) const
2830 // left x coordinate of tabular
2831 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2832 size_t ncols = tabular.columnCount();
2834 for (;c < ncols && x > w; ++c) {
2835 w += tabular.columnWidth(c);
2841 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2843 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2844 // mi.base.textwidth << "\n";
2846 lyxerr << "InsetTabular::metrics: need bv" << endl;
2847 BOOST_ASSERT(false);
2851 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2854 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2855 if (tabular.isPartOfMultiColumn(i, j))
2856 // Multicolumn cell, but not first one
2861 if (tabular.cell_info[i][j].multicolumn ==
2862 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2863 p_width = tabular.cellInfo(cell).p_width;
2865 p_width = tabular.column_info[j].p_width;
2866 if (!p_width.zero())
2867 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2868 tabular.cellInset(cell)->metrics(m, dim);
2869 if (!p_width.zero())
2870 dim.wid = m.base.textwidth;
2871 tabular.setCellWidth(cell, dim.wid);
2872 maxAsc = max(maxAsc, dim.asc);
2873 maxDesc = max(maxDesc, dim.des);
2876 int const top_space = tabular.row_info[i].top_space_default ?
2877 default_line_space :
2878 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2879 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2880 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2881 default_line_space :
2882 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2883 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2885 tabular.updateColumnWidths();
2886 dim.asc = tabular.rowAscent(0);
2887 dim.des = tabular.height() - dim.asc;
2888 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2892 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2894 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2895 BufferView * bv = pi.base.bv;
2897 // FIXME: As the full backrgound is painted in drawSelection(),
2898 // we have no choice but to do a full repaint for the Text cells.
2899 pi.full_repaint = true;
2901 resetPos(bv->cursor());
2904 x += ADD_TO_TABULAR_WIDTH;
2906 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2909 first_visible_cell = Tabular::npos;
2910 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2912 int const a = tabular.rowAscent(i);
2913 int const d = tabular.rowDescent(i);
2914 idx = tabular.cellIndex(i, 0);
2915 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2916 if (tabular.isPartOfMultiColumn(i, j))
2918 if (first_visible_cell == Tabular::npos)
2919 first_visible_cell = idx;
2921 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2922 // Cache the Inset position.
2923 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2924 if (nx + tabular.columnWidth(idx) < 0
2925 || nx > bv->workWidth()
2927 || y - a > bv->workHeight()) {
2928 pi.pain.setDrawingEnabled(false);
2929 cell(idx)->draw(pi, cx, y);
2930 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2931 pi.pain.setDrawingEnabled(original_drawing_state);
2933 cell(idx)->draw(pi, cx, y);
2934 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2936 nx += tabular.columnWidth(idx);
2940 if (i + 1 < tabular.rowCount())
2941 y += d + tabular.rowAscent(i + 1) +
2942 tabular.getAdditionalHeight(i + 1);
2947 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2949 Cursor & cur = pi.base.bv->cursor();
2951 x += scx_ + ADD_TO_TABULAR_WIDTH;
2953 // FIXME: it is wrong to completely paint the background
2954 // if we want to do single row painting.
2956 // Paint background of current tabular
2957 int const w = tabular.width();
2958 int const h = tabular.height();
2959 int yy = y - tabular.rowAscent(0);
2960 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2962 if (!cur.selection())
2964 if (&cur.inset() != this)
2970 if (tablemode(cur)) {
2973 getSelection(cur, rs, re, cs, ce);
2974 y -= tabular.rowAscent(0);
2975 for (row_type j = 0; j < tabular.rowCount(); ++j) {
2976 int const a = tabular.rowAscent(j);
2977 int const h = a + tabular.rowDescent(j);
2979 y += tabular.getAdditionalHeight(j);
2980 for (col_type i = 0; i < tabular.columnCount(); ++i) {
2981 if (tabular.isPartOfMultiColumn(j, i))
2983 idx_type const cell =
2984 tabular.cellIndex(j, i);
2985 int const w = tabular.columnWidth(cell);
2986 if (i >= cs && i <= ce && j >= rs && j <= re)
2987 pi.pain.fillRectangle(xx, y, w, h,
2995 x += cellXPos(cur.idx());
2996 x += tabular.getBeginningOfTextInCell(cur.idx());
2997 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3002 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3003 row_type row, idx_type cell, bool erased) const
3005 int x2 = x + tabular.columnWidth(cell);
3006 bool on_off = false;
3007 ColorCode col = Color_tabularline;
3008 ColorCode onoffcol = Color_tabularonoffline;
3011 col = Color_deletedtext;
3012 onoffcol = Color_deletedtext;
3015 if (!tabular.topAlreadyDrawn(cell)) {
3016 on_off = !tabular.topLine(cell);
3017 pain.line(x, y - tabular.rowAscent(row),
3018 x2, y - tabular.rowAscent(row),
3019 on_off ? onoffcol : col,
3020 on_off ? Painter::line_onoffdash : Painter::line_solid);
3022 on_off = !tabular.bottomLine(cell);
3023 pain.line(x, y + tabular.rowDescent(row),
3024 x2, y + tabular.rowDescent(row),
3025 on_off ? onoffcol : col,
3026 on_off ? Painter::line_onoffdash : Painter::line_solid);
3027 if (!tabular.leftAlreadyDrawn(cell)) {
3028 on_off = !tabular.leftLine(cell);
3029 pain.line(x, y - tabular.rowAscent(row),
3030 x, y + tabular.rowDescent(row),
3031 on_off ? onoffcol : col,
3032 on_off ? Painter::line_onoffdash : Painter::line_solid);
3034 on_off = !tabular.rightLine(cell);
3035 pain.line(x2 - tabular.getAdditionalWidth(cell),
3036 y - tabular.rowAscent(row),
3037 x2 - tabular.getAdditionalWidth(cell),
3038 y + tabular.rowDescent(row),
3039 on_off ? onoffcol : col,
3040 on_off ? Painter::line_onoffdash : Painter::line_solid);
3044 docstring InsetTabular::editMessage() const
3046 return _("Opened table");
3050 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3052 //lyxerr << "InsetTabular::edit: " << this << endl;
3054 cur.selection() = false;
3057 if (isRightToLeft(cur))
3058 cur.idx() = tabular.getLastCellInRow(0);
3064 if (isRightToLeft(cur))
3065 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3067 cur.idx() = tabular.cellCount() - 1;
3069 cur.pos() = cur.lastpos(); // FIXME crude guess
3071 // FIXME: this accesses the position cache before it is initialized
3073 //cur.bv().fitCursor();
3077 void InsetTabular::updateLabels(ParIterator const & it)
3079 // In a longtable, tell captions what the current float is
3080 Counters & cnts = buffer().params().documentClass().counters();
3081 string const saveflt = cnts.current_float();
3082 if (tabular.isLongTabular())
3083 cnts.current_float("table");
3085 ParIterator it2 = it;
3087 size_t const end = it2.nargs();
3088 for ( ; it2.idx() < end; it2.top().forwardIdx())
3089 lyx::updateLabels(buffer(), it2);
3092 if (tabular.isLongTabular())
3093 cnts.current_float(saveflt);
3097 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3099 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3100 << "\n cur:" << cur);
3101 CursorSlice sl = cur.top();
3102 Cursor & bvcur = cur.bv().cursor();
3104 switch (cmd.action) {
3106 case LFUN_MOUSE_PRESS: {
3107 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3109 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3110 || cmd.x > xo(cur.bv()) + tabular.width()) {
3111 row_type r = rowFromY(cur, cmd.y);
3112 cur.idx() = tabular.getFirstCellInRow(r);
3115 cur.idx() = tabular.getLastCellInRow(r);
3116 cur.pos() = cur.lastpos();
3117 cur.selection() = true;
3123 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3124 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3125 || cmd.y > y0 + tabular.height()) {
3126 col_type c = columnFromX(cur, cmd.x);
3127 cur.idx() = tabular.cellIndex(0, c);
3130 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3131 cur.pos() = cur.lastpos();
3132 cur.selection() = true;
3137 // do not reset cursor/selection if we have selected
3138 // some cells (bug 2715).
3139 if (cmd.button() == mouse_button::button3
3140 && &bvcur.selBegin().inset() == this
3141 && tablemode(bvcur))
3144 // Let InsetTableCell do it
3145 cell(cur.idx())->dispatch(cur, cmd);
3148 case LFUN_MOUSE_MOTION:
3149 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3150 if (cmd.button() == mouse_button::button1) {
3151 // only accept motions to places not deeper nested than the real anchor
3152 if (!bvcur.anchor_.hasPart(cur)) {
3156 // select (additional) row
3158 row_type r = rowFromY(cur, cmd.y);
3159 cur.idx() = tabular.getLastCellInRow(r);
3160 bvcur.setCursor(cur);
3161 bvcur.selection() = true;
3164 // select (additional) column
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:
3187 case LFUN_CELL_BACKWARD:
3189 cur.selection() = false;
3192 case LFUN_CELL_FORWARD:
3194 cur.selection() = false;
3196 case LFUN_CHAR_FORWARD_SELECT:
3197 case LFUN_CHAR_FORWARD:
3198 cell(cur.idx())->dispatch(cur, cmd);
3199 if (!cur.result().dispatched()) {
3201 if (sl == cur.top())
3202 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3208 case LFUN_CHAR_BACKWARD_SELECT:
3209 case LFUN_CHAR_BACKWARD:
3210 cell(cur.idx())->dispatch(cur, cmd);
3211 if (!cur.result().dispatched()) {
3213 if (sl == cur.top())
3214 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3220 case LFUN_CHAR_RIGHT_SELECT:
3221 case LFUN_CHAR_RIGHT:
3222 //FIXME: for visual cursor, really move right
3223 if (isRightToLeft(cur))
3224 lyx::dispatch(FuncRequest(
3225 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3226 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3228 lyx::dispatch(FuncRequest(
3229 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3230 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3233 case LFUN_CHAR_LEFT_SELECT:
3234 case LFUN_CHAR_LEFT:
3235 //FIXME: for visual cursor, really move left
3236 if (isRightToLeft(cur))
3237 lyx::dispatch(FuncRequest(
3238 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3239 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3241 lyx::dispatch(FuncRequest(
3242 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3243 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3246 case LFUN_DOWN_SELECT:
3248 cell(cur.idx())->dispatch(cur, cmd);
3249 cur.dispatched(); // override the cell's decision
3250 if (sl == cur.top())
3251 // if our Text didn't do anything to the cursor
3252 // then we try to put the cursor into the cell below
3253 // setting also the right targetX.
3254 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3255 cur.idx() = tabular.cellBelow(cur.idx());
3257 TextMetrics const & tm =
3258 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3259 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3261 if (sl == cur.top()) {
3262 // we trick it to go to forward after leaving the
3264 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3269 case LFUN_UP_SELECT:
3271 cell(cur.idx())->dispatch(cur, cmd);
3272 cur.dispatched(); // override the cell's decision
3273 if (sl == cur.top())
3274 // if our Text didn't do anything to the cursor
3275 // then we try to put the cursor into the cell above
3276 // setting also the right targetX.
3277 if (tabular.cellRow(cur.idx()) != 0) {
3278 cur.idx() = tabular.cellAbove(cur.idx());
3279 cur.pit() = cur.lastpit();
3280 Text const * text = cell(cur.idx())->getText(0);
3281 TextMetrics const & tm = cur.bv().textMetrics(text);
3282 ParagraphMetrics const & pm =
3283 tm.parMetrics(cur.lastpit());
3284 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3286 if (sl == cur.top()) {
3287 cmd = FuncRequest(LFUN_UP);
3292 // case LFUN_SCREEN_DOWN: {
3293 // //if (hasSelection())
3294 // // cur.selection() = false;
3295 // col_type const col = tabular.cellColumn(cur.idx());
3296 // int const t = cur.bv().top_y() + cur.bv().height();
3297 // if (t < yo() + tabular.getHeightOfTabular()) {
3298 // cur.bv().scrollDocView(t);
3299 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3301 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3308 // case LFUN_SCREEN_UP: {
3309 // //if (hasSelection())
3310 // // cur.selection() = false;
3311 // col_type const col = tabular.cellColumn(cur.idx());
3312 // int const t = cur.bv().top_y() + cur.bv().height();
3314 // cur.bv().scrollDocView(t);
3318 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3322 // cur.par() = cur.lastpar();
3323 // cur.pos() = cur.lastpos();
3327 case LFUN_LAYOUT_TABULAR:
3328 InsetTabularMailer(*this).showDialog(&cur.bv());
3331 case LFUN_INSET_DIALOG_UPDATE:
3332 InsetTabularMailer(*this).updateDialog(&cur.bv());
3335 case LFUN_TABULAR_FEATURE:
3336 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3340 // insert file functions
3341 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3342 case LFUN_FILE_INSERT_PLAINTEXT: {
3344 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3345 FileName(to_utf8(cmd.argument())));
3348 cur.recordUndoInset(INSERT_UNDO);
3349 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3350 // content has been replaced,
3351 // so cursor might be invalid
3352 cur.pos() = cur.lastpos();
3353 cur.pit() = cur.lastpit();
3354 bvcur.setCursor(cur);
3361 if (tablemode(cur)) {
3362 if (copySelection(cur)) {
3363 cur.recordUndoInset(DELETE_UNDO);
3368 cell(cur.idx())->dispatch(cur, cmd);
3371 case LFUN_CHAR_DELETE_BACKWARD:
3372 case LFUN_CHAR_DELETE_FORWARD:
3373 if (tablemode(cur)) {
3374 cur.recordUndoInset(DELETE_UNDO);
3378 cell(cur.idx())->dispatch(cur, cmd);
3382 if (!cur.selection())
3384 if (tablemode(cur)) {
3388 cell(cur.idx())->dispatch(cur, cmd);
3391 case LFUN_CLIPBOARD_PASTE:
3392 case LFUN_PRIMARY_SELECTION_PASTE: {
3393 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3394 theClipboard().getAsText() :
3395 theSelection().get();
3398 // pass to InsertPlaintextString, but
3399 // only if we have multi-cell content
3400 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3401 cur.recordUndoInset(INSERT_UNDO);
3402 if (insertPlaintextString(cur.bv(), clip, false)) {
3403 // content has been replaced,
3404 // so cursor might be invalid
3405 cur.pos() = cur.lastpos();
3406 cur.pit() = cur.lastpit();
3407 bvcur.setCursor(cur);
3411 // Let the cell handle normal text
3412 cell(cur.idx())->dispatch(cur, cmd);
3417 if (tabularStackDirty() && theClipboard().isInternal() ||
3418 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3419 cur.recordUndoInset(INSERT_UNDO);
3420 pasteClipboard(cur);
3423 cell(cur.idx())->dispatch(cur, cmd);
3426 case LFUN_FONT_EMPH:
3427 case LFUN_FONT_BOLD:
3428 case LFUN_FONT_ROMAN:
3429 case LFUN_FONT_NOUN:
3430 case LFUN_FONT_ITAL:
3431 case LFUN_FONT_FRAK:
3432 case LFUN_FONT_TYPEWRITER:
3433 case LFUN_FONT_SANS:
3434 case LFUN_FONT_FREE_APPLY:
3435 case LFUN_FONT_FREE_UPDATE:
3436 case LFUN_FONT_SIZE:
3437 case LFUN_FONT_UNDERLINE:
3439 case LFUN_WORD_CAPITALIZE:
3440 case LFUN_WORD_UPCASE:
3441 case LFUN_WORD_LOWCASE:
3442 case LFUN_CHARS_TRANSPOSE:
3443 if (tablemode(cur)) {
3446 getSelection(cur, rs, re, cs, ce);
3447 Cursor tmpcur = cur;
3448 for (row_type i = rs; i <= re; ++i) {
3449 for (col_type j = cs; j <= ce; ++j) {
3450 // cursor follows cell:
3451 tmpcur.idx() = tabular.cellIndex(i, j);
3452 // select this cell only:
3455 tmpcur.resetAnchor();
3456 tmpcur.pit() = tmpcur.lastpit();
3457 tmpcur.pos() = tmpcur.top().lastpos();
3458 tmpcur.setCursor(tmpcur);
3459 tmpcur.setSelection();
3460 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3465 cell(cur.idx())->dispatch(cur, cmd);
3469 // we try to handle this event in the insets dispatch function.
3470 cell(cur.idx())->dispatch(cur, cmd);
3476 // function sets an object as defined in func_status.h:
3477 // states OK, Unknown, Disabled, On, Off.
3478 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3479 FuncStatus & status) const
3481 switch (cmd.action) {
3482 case LFUN_TABULAR_FEATURE: {
3483 int action = Tabular::LAST_ACTION;
3485 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3486 string const tmp = tabularFeature[i].feature;
3487 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3488 action = tabularFeature[i].action;
3492 if (action == Tabular::LAST_ACTION) {
3494 status.unknown(true);
3498 string const argument
3499 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3501 row_type sel_row_start = 0;
3502 row_type sel_row_end = 0;
3503 col_type sel_col_start = 0;
3504 col_type sel_col_end = 0;
3505 Tabular::ltType dummyltt;
3508 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3511 case Tabular::SET_PWIDTH:
3512 case Tabular::SET_MPWIDTH:
3513 case Tabular::SET_SPECIAL_COLUMN:
3514 case Tabular::SET_SPECIAL_MULTI:
3515 case Tabular::APPEND_ROW:
3516 case Tabular::APPEND_COLUMN:
3517 case Tabular::DELETE_ROW:
3518 case Tabular::DELETE_COLUMN:
3519 case Tabular::COPY_ROW:
3520 case Tabular::COPY_COLUMN:
3521 case Tabular::SET_ALL_LINES:
3522 case Tabular::UNSET_ALL_LINES:
3523 case Tabular::SET_TOP_SPACE:
3524 case Tabular::SET_BOTTOM_SPACE:
3525 case Tabular::SET_INTERLINE_SPACE:
3526 case Tabular::SET_BORDER_LINES:
3530 case Tabular::MULTICOLUMN:
3531 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3534 case Tabular::TOGGLE_LINE_TOP:
3535 status.setOnOff(tabular.topLine(cur.idx()));
3538 case Tabular::TOGGLE_LINE_BOTTOM:
3539 status.setOnOff(tabular.bottomLine(cur.idx()));
3542 case Tabular::TOGGLE_LINE_LEFT:
3543 status.setOnOff(tabular.leftLine(cur.idx()));
3546 case Tabular::TOGGLE_LINE_RIGHT:
3547 status.setOnOff(tabular.rightLine(cur.idx()));
3550 case Tabular::M_ALIGN_LEFT:
3552 case Tabular::ALIGN_LEFT:
3553 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3556 case Tabular::M_ALIGN_RIGHT:
3558 case Tabular::ALIGN_RIGHT:
3559 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3562 case Tabular::M_ALIGN_CENTER:
3564 case Tabular::ALIGN_CENTER:
3565 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3568 case Tabular::ALIGN_BLOCK:
3569 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3570 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3573 case Tabular::M_VALIGN_TOP:
3575 case Tabular::VALIGN_TOP:
3577 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3580 case Tabular::M_VALIGN_BOTTOM:
3582 case Tabular::VALIGN_BOTTOM:
3584 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3587 case Tabular::M_VALIGN_MIDDLE:
3589 case Tabular::VALIGN_MIDDLE:
3591 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3594 case Tabular::SET_LONGTABULAR:
3595 status.setOnOff(tabular.isLongTabular());
3598 case Tabular::UNSET_LONGTABULAR:
3599 status.setOnOff(!tabular.isLongTabular());
3602 case Tabular::TOGGLE_ROTATE_TABULAR:
3603 case Tabular::SET_ROTATE_TABULAR:
3604 status.setOnOff(tabular.getRotateTabular());
3607 case Tabular::UNSET_ROTATE_TABULAR:
3608 status.setOnOff(!tabular.getRotateTabular());
3611 case Tabular::TOGGLE_ROTATE_CELL:
3612 case Tabular::SET_ROTATE_CELL:
3613 status.setOnOff(!oneCellHasRotationState(false,
3614 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3617 case Tabular::UNSET_ROTATE_CELL:
3618 status.setOnOff(!oneCellHasRotationState(true,
3619 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3622 case Tabular::SET_USEBOX:
3623 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3626 case Tabular::SET_LTFIRSTHEAD:
3627 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3630 case Tabular::UNSET_LTFIRSTHEAD:
3631 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3634 case Tabular::SET_LTHEAD:
3635 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3638 case Tabular::UNSET_LTHEAD:
3639 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3642 case Tabular::SET_LTFOOT:
3643 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3646 case Tabular::UNSET_LTFOOT:
3647 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3650 case Tabular::SET_LTLASTFOOT:
3651 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3654 case Tabular::UNSET_LTLASTFOOT:
3655 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3658 case Tabular::SET_LTNEWPAGE:
3659 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3662 case Tabular::SET_BOOKTABS:
3663 status.setOnOff(tabular.useBookTabs());
3666 case Tabular::UNSET_BOOKTABS:
3667 status.setOnOff(!tabular.useBookTabs());
3672 status.enabled(false);
3678 // These are only enabled inside tabular
3679 case LFUN_CELL_BACKWARD:
3680 case LFUN_CELL_FORWARD:
3681 status.enabled(true);
3684 // disable these with multiple cells selected
3685 case LFUN_INSET_INSERT:
3686 case LFUN_TABULAR_INSERT:
3687 case LFUN_FLEX_INSERT:
3688 case LFUN_FLOAT_INSERT:
3689 case LFUN_FLOAT_WIDE_INSERT:
3690 case LFUN_FOOTNOTE_INSERT:
3691 case LFUN_MARGINALNOTE_INSERT:
3692 case LFUN_MATH_INSERT:
3693 case LFUN_MATH_MODE:
3694 case LFUN_MATH_MUTATE:
3695 case LFUN_MATH_DISPLAY:
3696 case LFUN_NOTE_INSERT:
3697 case LFUN_OPTIONAL_INSERT:
3698 case LFUN_BOX_INSERT:
3699 case LFUN_BRANCH_INSERT:
3700 case LFUN_WRAP_INSERT:
3701 case LFUN_ERT_INSERT: {
3702 if (tablemode(cur)) {
3703 status.enabled(false);
3706 return cell(cur.idx())->getStatus(cur, cmd, status);
3709 // disable in non-fixed-width cells
3710 case LFUN_NEWLINE_INSERT:
3711 case LFUN_BREAK_PARAGRAPH:
3712 case LFUN_BREAK_PARAGRAPH_SKIP: {
3713 if (tabular.getPWidth(cur.idx()).zero()) {
3714 status.enabled(false);
3717 return cell(cur.idx())->getStatus(cur, cmd, status);
3721 if (tabularStackDirty() && theClipboard().isInternal()) {
3722 status.enabled(true);
3725 return cell(cur.idx())->getStatus(cur, cmd, status);
3727 case LFUN_INSET_MODIFY:
3728 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3729 status.enabled(true);
3735 // we try to handle this event in the insets dispatch function.
3736 return cell(cur.idx())->getStatus(cur, cmd, status);
3741 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3743 return tabular.latex(os, runparams);
3747 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3749 os << '\n'; // output table on a new line
3750 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3751 tabular.plaintext(os, runparams, dp, false, 0);
3752 return PLAINTEXT_NEWLINE;
3756 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3761 // FIXME: Why not pass a proper DocIterator here?
3763 // if the table is inside a float it doesn't need the informaltable
3764 // wrapper. Search for it.
3765 for (master = owner(); master; master = master->owner())
3766 if (master->lyxCode() == FLOAT_CODE)
3771 os << "<informaltable>";
3774 ret += tabular.docbook(os, runparams);
3776 os << "</informaltable>";
3783 void InsetTabular::validate(LaTeXFeatures & features) const
3785 tabular.validate(features);
3789 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3791 return tabular.cellInset(idx);
3795 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3797 return tabular.cellInset(idx);
3801 void InsetTabular::cursorPos(BufferView const & bv,
3802 CursorSlice const & sl, bool boundary, int & x, int & y) const
3804 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3806 // y offset correction
3807 int const row = tabular.cellRow(sl.idx());
3808 for (int i = 0; i <= row; ++i) {
3810 y += tabular.rowAscent(i);
3811 y += tabular.getAdditionalHeight(i);
3814 y += tabular.rowDescent(i);
3817 // x offset correction
3818 int const col = tabular.cellColumn(sl.idx());
3819 int idx = tabular.cellIndex(row, 0);
3820 for (int j = 0; j < col; ++j) {
3821 if (tabular.isPartOfMultiColumn(row, j))
3823 x += tabular.columnWidth(idx);
3826 x += tabular.getBeginningOfTextInCell(idx);
3827 x += ADD_TO_TABULAR_WIDTH;
3832 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3836 Inset const & inset = *tabular.cellInset(cell);
3837 Point o = bv.coordCache().getInsets().xy(&inset);
3838 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3839 int const xend = xbeg + tabular.columnWidth(cell);
3840 row_type const row = tabular.cellRow(cell);
3841 int const ybeg = o.y_ - tabular.rowAscent(row) -
3842 tabular.getAdditionalHeight(row);
3843 int const yend = o.y_ + tabular.rowDescent(row);
3855 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3856 // << " ybeg=" << ybeg << " yend=" << yend
3857 // << " xx=" << xx << " yy=" << yy
3858 // << " dist=" << xx + yy << endl;
3863 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3865 //lyxerr << "InsetTabular::editXY: " << this << endl;
3866 cur.selection() = false;
3868 cur.idx() = getNearestCell(cur.bv(), x, y);
3870 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3874 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3876 cur.idx() = getNearestCell(cur.bv(), x, y);
3877 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3881 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3883 idx_type idx_min = 0;
3884 int dist_min = numeric_limits<int>::max();
3885 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3886 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3887 int const d = dist(bv, i, x, y);
3898 int InsetTabular::cellXPos(idx_type const cell) const
3902 for (; !tabular.isFirstCellInRow(c); --c)
3905 for (; c < cell; ++c)
3906 lx += tabular.columnWidth(c);
3912 void InsetTabular::resetPos(Cursor & cur) const
3914 BufferView & bv = cur.bv();
3915 int const maxwidth = bv.workWidth();
3917 if (&cur.inset() != this) {
3921 int const X2 = maxwidth;
3922 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3923 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3924 int const x2 = x1 + tabular.columnWidth(cur.idx());
3927 scx_ = X1 + 20 - x1;
3929 scx_ = X2 - 20 - x2;
3934 cur.updateFlags(Update::Force | Update::FitCursor);
3938 void InsetTabular::moveNextCell(Cursor & cur)
3940 if (isRightToLeft(cur)) {
3941 if (tabular.isFirstCellInRow(cur.idx())) {
3942 row_type const row = tabular.cellRow(cur.idx());
3943 if (row == tabular.rowCount() - 1)
3945 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3952 if (tabular.isLastCell(cur.idx()))
3962 void InsetTabular::movePrevCell(Cursor & cur)
3964 if (isRightToLeft(cur)) {
3965 if (tabular.isLastCellInRow(cur.idx())) {
3966 row_type const row = tabular.cellRow(cur.idx());
3969 cur.idx() = tabular.getFirstCellInRow(row);
3970 cur.idx() = tabular.cellAbove(cur.idx());
3972 if (tabular.isLastCell(cur.idx()))
3977 if (cur.idx() == 0) // first cell
3981 cur.pit() = cur.lastpit();
3982 cur.pos() = cur.lastpos();
3984 // FIXME: this accesses the position cache before it is initialized
3989 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
3991 Tabular::Feature action = Tabular::LAST_ACTION;
3994 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3995 string const tmp = tabularFeature[i].feature;
3997 if (tmp == what.substr(0, tmp.length())) {
3998 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
3999 //tabularFeatures[i].feature.length()))
4000 action = tabularFeature[i].action;
4004 if (action == Tabular::LAST_ACTION)
4008 ltrim(what.substr(tabularFeature[i].feature.length()));
4009 tabularFeatures(cur, action, val);
4014 static void checkLongtableSpecial(Tabular::ltType & ltt,
4015 string const & special, bool & flag)
4017 if (special == "dl_above") {
4020 } else if (special == "dl_below") {
4021 ltt.bottomDL = flag;
4023 } else if (special == "empty") {
4032 bool InsetTabular::oneCellHasRotationState(bool rotated,
4033 row_type row_start, row_type row_end,
4034 col_type col_start, col_type col_end) const {
4036 for (row_type i = row_start; i <= row_end; ++i) {
4037 for (col_type j = col_start; j <= col_end; ++j) {
4038 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4047 void InsetTabular::tabularFeatures(Cursor & cur,
4048 Tabular::Feature feature, string const & value)
4050 col_type sel_col_start;
4051 col_type sel_col_end;
4052 row_type sel_row_start;
4053 row_type sel_row_end;
4054 bool setLines = false;
4055 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4056 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4060 case Tabular::M_ALIGN_LEFT:
4061 case Tabular::ALIGN_LEFT:
4062 setAlign = LYX_ALIGN_LEFT;
4065 case Tabular::M_ALIGN_RIGHT:
4066 case Tabular::ALIGN_RIGHT:
4067 setAlign = LYX_ALIGN_RIGHT;
4070 case Tabular::M_ALIGN_CENTER:
4071 case Tabular::ALIGN_CENTER:
4072 setAlign = LYX_ALIGN_CENTER;
4075 case Tabular::ALIGN_BLOCK:
4076 setAlign = LYX_ALIGN_BLOCK;
4079 case Tabular::M_VALIGN_TOP:
4080 case Tabular::VALIGN_TOP:
4081 setVAlign = Tabular::LYX_VALIGN_TOP;
4084 case Tabular::M_VALIGN_BOTTOM:
4085 case Tabular::VALIGN_BOTTOM:
4086 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4089 case Tabular::M_VALIGN_MIDDLE:
4090 case Tabular::VALIGN_MIDDLE:
4091 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4098 cur.recordUndoInset(ATOMIC_UNDO);
4100 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4101 row_type const row = tabular.cellRow(cur.idx());
4102 col_type const column = tabular.cellColumn(cur.idx());
4104 Tabular::ltType ltt;
4108 case Tabular::SET_PWIDTH: {
4109 Length const len(value);
4110 tabular.setColumnPWidth(cur, cur.idx(), len);
4112 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4113 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4117 case Tabular::SET_MPWIDTH:
4118 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4121 case Tabular::SET_SPECIAL_COLUMN:
4122 case Tabular::SET_SPECIAL_MULTI:
4123 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4126 case Tabular::APPEND_ROW:
4127 // append the row into the tabular
4128 tabular.appendRow(cur.idx());
4131 case Tabular::APPEND_COLUMN:
4132 // append the column into the tabular
4133 tabular.appendColumn(cur.idx());
4134 cur.idx() = tabular.cellIndex(row, column);
4137 case Tabular::DELETE_ROW:
4138 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4139 tabular.deleteRow(sel_row_start);
4140 if (sel_row_start >= tabular.rowCount())
4142 cur.idx() = tabular.cellIndex(sel_row_start, column);
4145 cur.selection() = false;
4148 case Tabular::DELETE_COLUMN:
4149 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4150 tabular.deleteColumn(sel_col_start);
4151 if (sel_col_start >= tabular.columnCount())
4153 cur.idx() = tabular.cellIndex(row, sel_col_start);
4156 cur.selection() = false;
4159 case Tabular::COPY_ROW:
4160 tabular.copyRow(row);
4163 case Tabular::COPY_COLUMN:
4164 tabular.copyColumn(column);
4165 cur.idx() = tabular.cellIndex(row, column);
4168 case Tabular::TOGGLE_LINE_TOP: {
4169 bool lineSet = !tabular.topLine(cur.idx());
4170 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4171 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4172 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4176 case Tabular::TOGGLE_LINE_BOTTOM: {
4177 bool lineSet = !tabular.bottomLine(cur.idx());
4178 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4179 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4180 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4184 case Tabular::TOGGLE_LINE_LEFT: {
4185 bool lineSet = !tabular.leftLine(cur.idx());
4186 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4187 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4188 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4192 case Tabular::TOGGLE_LINE_RIGHT: {
4193 bool lineSet = !tabular.rightLine(cur.idx());
4194 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4195 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4196 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4200 case Tabular::M_ALIGN_LEFT:
4201 case Tabular::M_ALIGN_RIGHT:
4202 case Tabular::M_ALIGN_CENTER:
4204 case Tabular::ALIGN_LEFT:
4205 case Tabular::ALIGN_RIGHT:
4206 case Tabular::ALIGN_CENTER:
4207 case Tabular::ALIGN_BLOCK:
4208 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4209 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4210 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4213 case Tabular::M_VALIGN_TOP:
4214 case Tabular::M_VALIGN_BOTTOM:
4215 case Tabular::M_VALIGN_MIDDLE:
4217 case Tabular::VALIGN_TOP:
4218 case Tabular::VALIGN_BOTTOM:
4219 case Tabular::VALIGN_MIDDLE:
4220 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4221 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4222 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4225 case Tabular::MULTICOLUMN: {
4226 if (sel_row_start != sel_row_end) {
4227 // FIXME: Need I say it ? This is horrible.
4229 Alert::error(_("Error setting multicolumn"),
4230 _("You cannot set multicolumn vertically."));
4233 if (!cur.selection()) {
4234 // just multicol for one single cell
4235 // check whether we are completely in a multicol
4236 if (tabular.isMultiColumn(cur.idx()))
4237 tabular.unsetMultiColumn(cur.idx());
4239 tabular.setMultiColumn(cur.idx(), 1);
4242 // we have a selection so this means we just add all this
4243 // cells to form a multicolumn cell
4244 idx_type const s_start = cur.selBegin().idx();
4245 idx_type const s_end = cur.selEnd().idx();
4246 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4247 cur.idx() = s_start;
4250 cur.selection() = false;
4254 case Tabular::SET_ALL_LINES:
4256 case Tabular::UNSET_ALL_LINES:
4257 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4258 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4259 tabular.setAllLines(
4260 tabular.cellIndex(i,j), setLines);
4263 case Tabular::SET_BORDER_LINES:
4264 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4265 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4266 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4268 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4269 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4270 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4274 case Tabular::SET_LONGTABULAR:
4275 tabular.setLongTabular(true);
4278 case Tabular::UNSET_LONGTABULAR:
4279 tabular.setLongTabular(false);
4282 case Tabular::SET_ROTATE_TABULAR:
4283 tabular.setRotateTabular(true);
4286 case Tabular::UNSET_ROTATE_TABULAR:
4287 tabular.setRotateTabular(false);
4290 case Tabular::TOGGLE_ROTATE_TABULAR:
4291 tabular.setRotateTabular(!tabular.getRotateTabular());
4294 case Tabular::SET_ROTATE_CELL:
4295 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4296 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4297 tabular.setRotateCell(
4298 tabular.cellIndex(i, j), true);
4301 case Tabular::UNSET_ROTATE_CELL:
4302 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4303 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4304 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4307 case Tabular::TOGGLE_ROTATE_CELL:
4309 bool oneNotRotated = oneCellHasRotationState(false,
4310 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4312 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4313 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4314 tabular.setRotateCell(tabular.cellIndex(i, j),
4319 case Tabular::SET_USEBOX: {
4320 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4321 if (val == tabular.getUsebox(cur.idx()))
4322 val = Tabular::BOX_NONE;
4323 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4324 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4325 tabular.setUsebox(tabular.cellIndex(i, j), val);
4329 case Tabular::UNSET_LTFIRSTHEAD:
4331 case Tabular::SET_LTFIRSTHEAD:
4332 tabular.getRowOfLTFirstHead(row, ltt);
4333 checkLongtableSpecial(ltt, value, flag);
4334 tabular.setLTHead(row, flag, ltt, true);
4337 case Tabular::UNSET_LTHEAD:
4339 case Tabular::SET_LTHEAD:
4340 tabular.getRowOfLTHead(row, ltt);
4341 checkLongtableSpecial(ltt, value, flag);
4342 tabular.setLTHead(row, flag, ltt, false);
4345 case Tabular::UNSET_LTFOOT:
4347 case Tabular::SET_LTFOOT:
4348 tabular.getRowOfLTFoot(row, ltt);
4349 checkLongtableSpecial(ltt, value, flag);
4350 tabular.setLTFoot(row, flag, ltt, false);
4353 case Tabular::UNSET_LTLASTFOOT:
4355 case Tabular::SET_LTLASTFOOT:
4356 tabular.getRowOfLTLastFoot(row, ltt);
4357 checkLongtableSpecial(ltt, value, flag);
4358 tabular.setLTFoot(row, flag, ltt, true);
4361 case Tabular::SET_LTNEWPAGE:
4362 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4365 case Tabular::SET_BOOKTABS:
4366 tabular.setBookTabs(true);
4369 case Tabular::UNSET_BOOKTABS:
4370 tabular.setBookTabs(false);
4373 case Tabular::SET_TOP_SPACE: {
4375 if (value == "default")
4376 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4377 tabular.row_info[i].top_space_default = true;
4378 else if (isValidLength(value, &len))
4379 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4380 tabular.row_info[i].top_space_default = false;
4381 tabular.row_info[i].top_space = len;
4384 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4385 tabular.row_info[i].top_space_default = false;
4386 tabular.row_info[i].top_space = len;
4391 case Tabular::SET_BOTTOM_SPACE: {
4393 if (value == "default")
4394 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4395 tabular.row_info[i].bottom_space_default = true;
4396 else if (isValidLength(value, &len))
4397 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4398 tabular.row_info[i].bottom_space_default = false;
4399 tabular.row_info[i].bottom_space = len;
4402 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4403 tabular.row_info[i].bottom_space_default = false;
4404 tabular.row_info[i].bottom_space = len;
4409 case Tabular::SET_INTERLINE_SPACE: {
4411 if (value == "default")
4412 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4413 tabular.row_info[i].interline_space_default = true;
4414 else if (isValidLength(value, &len))
4415 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4416 tabular.row_info[i].interline_space_default = false;
4417 tabular.row_info[i].interline_space = len;
4420 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4421 tabular.row_info[i].interline_space_default = false;
4422 tabular.row_info[i].interline_space = len;
4427 // dummy stuff just to avoid warnings
4428 case Tabular::LAST_ACTION:
4434 bool InsetTabular::showInsetDialog(BufferView * bv) const
4436 InsetTabularMailer(*this).showDialog(bv);
4441 void InsetTabular::openLayoutDialog(BufferView * bv) const
4443 InsetTabularMailer(*this).showDialog(bv);
4447 bool InsetTabular::copySelection(Cursor & cur)
4449 if (!cur.selection())
4454 getSelection(cur, rs, re, cs, ce);
4456 paste_tabular.reset(new Tabular(tabular));
4458 for (row_type i = 0; i < rs; ++i)
4459 paste_tabular->deleteRow(0);
4461 row_type const rows = re - rs + 1;
4462 while (paste_tabular->rowCount() > rows)
4463 paste_tabular->deleteRow(rows);
4465 for (col_type i = 0; i < cs; ++i)
4466 paste_tabular->deleteColumn(0);
4468 col_type const columns = ce - cs + 1;
4469 while (paste_tabular->columnCount() > columns)
4470 paste_tabular->deleteColumn(columns);
4472 // We clear all the InsetTableCell pointers, since they
4473 // might now become invalid and there is no point in having
4474 // them point to temporary things in paste_tabular.
4475 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4476 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4477 paste_tabular->cellInset(i,j)->setCellData(0);
4478 paste_tabular->cellInset(i,j)->setTabular(0);
4481 odocstringstream os;
4482 OutputParams const runparams(0);
4483 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4484 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4485 cap::copySelection(cur, os.str());
4487 // mark tabular stack dirty
4488 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4489 // when we (hopefully) have a one-for-all paste mechanism.
4490 // This must be called after cap::copySelection.
4491 dirtyTabularStack(true);
4497 bool InsetTabular::pasteClipboard(Cursor & cur)
4501 col_type const actcol = tabular.cellColumn(cur.idx());
4502 row_type const actrow = tabular.cellRow(cur.idx());
4503 for (row_type r1 = 0, r2 = actrow;
4504 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4506 for (col_type c1 = 0, c2 = actcol;
4507 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4509 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4510 tabular.isPartOfMultiColumn(r2, c2))
4512 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4516 if (tabular.isPartOfMultiColumn(r2, c2)) {
4520 shared_ptr<InsetTableCell> inset(
4521 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4522 // note that setCellInset will call InsetTableCell::setCellData()
4523 // and InsetTableCell::setTabular()
4524 tabular.setCellInset(r2, c2, inset);
4525 // FIXME: change tracking (MG)
4526 inset->setChange(Change(cur.buffer().params().trackChanges ?
4527 Change::INSERTED : Change::UNCHANGED));
4535 void InsetTabular::cutSelection(Cursor & cur)
4537 if (!cur.selection())
4542 getSelection(cur, rs, re, cs, ce);
4543 for (row_type i = rs; i <= re; ++i) {
4544 for (col_type j = cs; j <= ce; ++j) {
4545 shared_ptr<InsetTableCell> t
4546 = cell(tabular.cellIndex(i, j));
4547 if (cur.buffer().params().trackChanges)
4548 // FIXME: Change tracking (MG)
4549 t->setChange(Change(Change::DELETED));
4555 // cursor position might be invalid now
4556 if (cur.pit() > cur.lastpit())
4557 cur.pit() = cur.lastpit();
4558 if (cur.pos() > cur.lastpos())
4559 cur.pos() = cur.lastpos();
4560 cur.clearSelection();
4564 bool InsetTabular::isRightToLeft(Cursor & cur) const
4566 BOOST_ASSERT(cur.depth() > 1);
4567 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4568 pos_type const parentpos = cur[cur.depth() - 2].pos();
4569 return parentpar.getFontSettings(cur.bv().buffer().params(),
4570 parentpos).language()->rightToLeft();
4574 void InsetTabular::getSelection(Cursor & cur,
4575 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4577 CursorSlice const & beg = cur.selBegin();
4578 CursorSlice const & end = cur.selEnd();
4579 cs = tabular.cellColumn(beg.idx());
4580 ce = tabular.cellColumn(end.idx());
4583 cs = tabular.cellColumn(end.idx());
4585 ce = tabular.cellRightColumn(end.idx());
4588 rs = tabular.cellRow(beg.idx());
4589 re = tabular.cellRow(end.idx());
4595 Text * InsetTabular::getText(int idx) const
4597 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4601 void InsetTabular::setChange(Change const & change)
4603 for (idx_type idx = 0; idx < nargs(); ++idx)
4604 cell(idx)->setChange(change);
4608 void InsetTabular::acceptChanges(BufferParams const & bparams)
4610 for (idx_type idx = 0; idx < nargs(); ++idx)
4611 cell(idx)->acceptChanges(bparams);
4615 void InsetTabular::rejectChanges(BufferParams const & bparams)
4617 for (idx_type idx = 0; idx < nargs(); ++idx)
4618 cell(idx)->rejectChanges(bparams);
4622 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4624 return tabular.getPWidth(cell).zero();
4628 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4630 return !tabular.getPWidth(cell).zero();
4634 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4637 if (buf.length() <= 0)
4642 col_type maxCols = 1;
4643 size_t const len = buf.length();
4647 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4655 maxCols = max(cols, maxCols);
4661 maxCols = max(cols, maxCols);
4667 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4668 loctab = paste_tabular.get();
4670 dirtyTabularStack(true);
4673 cell = bv.cursor().idx();
4674 ocol = tabular.cellColumn(cell);
4675 row = tabular.cellRow(cell);
4679 idx_type const cells = loctab->cellCount();
4682 rows = loctab->rowCount();
4683 col_type const columns = loctab->columnCount();
4685 while (cell < cells && p < len && row < rows &&
4686 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4692 // we can only set this if we are not too far right
4693 if (cols < columns) {
4694 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4695 Font const font = bv.textMetrics(&inset->text_).
4697 inset->setText(buf.substr(op, p - op), font,
4698 buffer().params().trackChanges);
4704 // we can only set this if we are not too far right
4705 if (cols < columns) {
4706 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4707 Font const font = bv.textMetrics(&inset->text_).
4709 inset->setText(buf.substr(op, p - op), font,
4710 buffer().params().trackChanges);
4715 cell = loctab->cellIndex(row, cols);
4721 // check for the last cell if there is no trailing '\n'
4722 if (cell < cells && op < len) {
4723 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4724 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4725 inset->setText(buf.substr(op, len - op), font,
4726 buffer().params().trackChanges);
4732 void InsetTabular::addPreview(PreviewLoader & loader) const
4734 row_type const rows = tabular.rowCount();
4735 col_type const columns = tabular.columnCount();
4736 for (row_type i = 0; i < rows; ++i) {
4737 for (col_type j = 0; j < columns; ++j)
4738 tabular.cellInset(i, j)->addPreview(loader);
4743 bool InsetTabular::tablemode(Cursor & cur) const
4745 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4749 bool InsetTabular::completionSupported(Cursor const & cur) const
4751 Cursor const & bvCur = cur.bv().cursor();
4752 if (&bvCur.inset() != this)
4754 return cur.text()->completionSupported(cur);
4758 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4760 return completionSupported(cur);
4764 bool InsetTabular::automaticInlineCompletion() const
4766 return lyxrc.completion_inline_text;
4770 bool InsetTabular::automaticPopupCompletion() const
4772 return lyxrc.completion_popup_text;
4776 bool InsetTabular::showCompletionCursor() const
4778 return lyxrc.completion_cursor_text;
4782 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4784 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4788 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4790 if (!completionSupported(cur))
4792 return cur.text()->completionPrefix(cur);
4796 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4798 if (!completionSupported(cur))
4801 return cur.text()->insertCompletion(cur, s, finished);
4805 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4806 Dimension & dim) const
4808 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4809 tm.completionPosAndDim(cur, x, y, dim);
4813 string const InsetTabularMailer::name_("tabular");
4815 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4816 : inset_(const_cast<InsetTabular &>(inset))
4820 string const InsetTabularMailer::inset2string(Buffer const &) const
4822 return params2string(inset_);
4826 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4828 istringstream data(in);
4830 lex.setStream(data);
4837 if (!lex || token != name_)
4838 return print_mailer_error("InsetTabularMailer", in, 1,
4841 // This is part of the inset proper that is usually swallowed
4842 // by Buffer::readInset
4844 if (!lex || token != "Tabular")
4845 return print_mailer_error("InsetTabularMailer", in, 2, "Tabular");
4851 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4854 data << name_ << ' ';
4856 data << "\\end_inset\n";