2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/alert.h"
48 #include "frontends/Clipboard.h"
49 #include "frontends/Painter.h"
50 #include "frontends/Selection.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/gettext.h"
58 #include "support/lstrings.h"
60 #include <boost/scoped_ptr.hpp>
68 using namespace lyx::support;
70 using boost::shared_ptr;
71 using boost::dynamic_pointer_cast;
75 using cap::dirtyTabularStack;
76 using cap::tabularStackDirty;
78 using graphics::PreviewLoader;
80 using frontend::Painter;
81 using frontend::Clipboard;
83 namespace Alert = frontend::Alert;
88 int const ADD_TO_HEIGHT = 2; // in cell
89 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
90 int const default_line_space = 10; // ?
91 int const WIDTH_OF_LINE = 5; // space between double lines
95 boost::scoped_ptr<Tabular> paste_tabular;
98 struct TabularFeature {
99 Tabular::Feature action;
104 TabularFeature tabularFeature[] =
106 { Tabular::APPEND_ROW, "append-row" },
107 { Tabular::APPEND_COLUMN, "append-column" },
108 { Tabular::DELETE_ROW, "delete-row" },
109 { Tabular::DELETE_COLUMN, "delete-column" },
110 { Tabular::COPY_ROW, "copy-row" },
111 { Tabular::COPY_COLUMN, "copy-column" },
112 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
113 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
114 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
115 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
116 { Tabular::ALIGN_LEFT, "align-left" },
117 { Tabular::ALIGN_RIGHT, "align-right" },
118 { Tabular::ALIGN_CENTER, "align-center" },
119 { Tabular::ALIGN_BLOCK, "align-block" },
120 { Tabular::VALIGN_TOP, "valign-top" },
121 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
122 { Tabular::VALIGN_MIDDLE, "valign-middle" },
123 { Tabular::M_ALIGN_LEFT, "m-align-left" },
124 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
125 { Tabular::M_ALIGN_CENTER, "m-align-center" },
126 { Tabular::M_VALIGN_TOP, "m-valign-top" },
127 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
128 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
129 { Tabular::MULTICOLUMN, "multicolumn" },
130 { Tabular::SET_ALL_LINES, "set-all-lines" },
131 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
132 { Tabular::SET_LONGTABULAR, "set-longtabular" },
133 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
134 { Tabular::SET_PWIDTH, "set-pwidth" },
135 { Tabular::SET_MPWIDTH, "set-mpwidth" },
136 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
137 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
138 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
139 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
140 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
141 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
142 { Tabular::SET_USEBOX, "set-usebox" },
143 { Tabular::SET_LTHEAD, "set-lthead" },
144 { Tabular::UNSET_LTHEAD, "unset-lthead" },
145 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
146 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
147 { Tabular::SET_LTFOOT, "set-ltfoot" },
148 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
149 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
150 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
151 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
152 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
153 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
154 { Tabular::SET_BOOKTABS, "set-booktabs" },
155 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
156 { Tabular::SET_TOP_SPACE, "set-top-space" },
157 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
158 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
159 { Tabular::SET_BORDER_LINES, "set-border-lines" },
160 { Tabular::LAST_ACTION, "" }
164 class FeatureEqual : public unary_function<TabularFeature, bool> {
166 FeatureEqual(Tabular::Feature feature)
167 : feature_(feature) {}
168 bool operator()(TabularFeature const & tf) const {
169 return tf.action == feature_;
172 Tabular::Feature feature_;
177 string const write_attribute(string const & name, T const & t)
179 string const s = tostr(t);
180 return s.empty() ? s : " " + name + "=\"" + s + "\"";
184 string const write_attribute(string const & name, string const & t)
186 return t.empty() ? t : " " + name + "=\"" + t + "\"";
191 string const write_attribute(string const & name, docstring const & t)
193 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
198 string const write_attribute(string const & name, bool const & b)
200 // we write only true attribute values so we remove a bit of the
201 // file format bloat for tabulars.
202 return b ? write_attribute(name, convert<string>(b)) : string();
207 string const write_attribute(string const & name, int const & i)
209 // we write only true attribute values so we remove a bit of the
210 // file format bloat for tabulars.
211 return i ? write_attribute(name, convert<string>(i)) : string();
216 string const write_attribute(string const & name, Tabular::idx_type const & i)
218 // we write only true attribute values so we remove a bit of the
219 // file format bloat for tabulars.
220 return i ? write_attribute(name, convert<string>(i)) : string();
225 string const write_attribute(string const & name, Length const & value)
227 // we write only the value if we really have one same reson as above.
228 return value.zero() ? string() : write_attribute(name, value.asString());
232 string const tostr(LyXAlignment const & num)
237 case LYX_ALIGN_BLOCK:
241 case LYX_ALIGN_CENTER:
243 case LYX_ALIGN_RIGHT:
245 case LYX_ALIGN_LAYOUT:
247 case LYX_ALIGN_SPECIAL:
254 string const tostr(Tabular::VAlignment const & num)
257 case Tabular::LYX_VALIGN_TOP:
259 case Tabular::LYX_VALIGN_MIDDLE:
261 case Tabular::LYX_VALIGN_BOTTOM:
268 string const tostr(Tabular::BoxType const & num)
271 case Tabular::BOX_NONE:
273 case Tabular::BOX_PARBOX:
275 case Tabular::BOX_MINIPAGE:
282 // I would have liked a fromstr template a lot better. (Lgb)
283 bool string2type(string const str, LyXAlignment & num)
286 num = LYX_ALIGN_NONE;
287 else if (str == "block")
288 num = LYX_ALIGN_BLOCK;
289 else if (str == "left")
290 num = LYX_ALIGN_LEFT;
291 else if (str == "center")
292 num = LYX_ALIGN_CENTER;
293 else if (str == "right")
294 num = LYX_ALIGN_RIGHT;
301 bool string2type(string const str, Tabular::VAlignment & num)
304 num = Tabular::LYX_VALIGN_TOP;
305 else if (str == "middle" )
306 num = Tabular::LYX_VALIGN_MIDDLE;
307 else if (str == "bottom")
308 num = Tabular::LYX_VALIGN_BOTTOM;
315 bool string2type(string const str, Tabular::BoxType & num)
318 num = Tabular::BOX_NONE;
319 else if (str == "parbox")
320 num = Tabular::BOX_PARBOX;
321 else if (str == "minipage")
322 num = Tabular::BOX_MINIPAGE;
329 bool string2type(string const str, bool & num)
333 else if (str == "false")
341 bool getTokenValue(string const & str, char const * token, string & ret)
344 size_t token_length = strlen(token);
345 size_t pos = str.find(token);
347 if (pos == string::npos || pos + token_length + 1 >= str.length()
348 || str[pos + token_length] != '=')
350 pos += token_length + 1;
352 if (ch != '"' && ch != '\'') { // only read till next space
356 while (pos < str.length() - 1 && str[++pos] != ch)
363 bool getTokenValue(string const & str, char const * token, docstring & ret)
366 bool const success = getTokenValue(str, token, tmp);
367 ret = from_utf8(tmp);
372 bool getTokenValue(string const & str, char const * token, int & num)
376 if (!getTokenValue(str, token, tmp))
378 num = convert<int>(tmp);
383 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
386 return getTokenValue(str, token, tmp) && string2type(tmp, num);
390 bool getTokenValue(string const & str, char const * token,
391 Tabular::VAlignment & num)
394 return getTokenValue(str, token, tmp) && string2type(tmp, num);
398 bool getTokenValue(string const & str, char const * token,
399 Tabular::BoxType & num)
402 return getTokenValue(str, token, tmp) && string2type(tmp, num);
406 bool getTokenValue(string const & str, char const * token, bool & flag)
408 // set the flag always to false as this should be the default for bools
409 // not in the file-format.
412 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
416 bool getTokenValue(string const & str, char const * token, Length & len)
418 // set the length to be zero() as default as this it should be if not
419 // in the file format.
422 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
426 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
431 if (!getTokenValue(str, token, tmp))
433 if (tmp == "default") {
437 return isValidLength(tmp, &len);
441 void l_getline(istream & is, string & str)
444 while (str.empty()) {
446 if (!str.empty() && str[str.length() - 1] == '\r')
447 str.erase(str.length() - 1);
454 string const featureAsString(Tabular::Feature feature)
456 TabularFeature * end = tabularFeature +
457 sizeof(tabularFeature) / sizeof(TabularFeature);
458 TabularFeature * it = find_if(tabularFeature, end,
459 FeatureEqual(feature));
460 return (it == end) ? string() : it->feature;
465 /////////////////////////////////////////////////////////////////////
469 /////////////////////////////////////////////////////////////////////
472 Tabular::CellData::CellData(Buffer const & buf, Tabular const & table)
475 multicolumn(Tabular::CELL_NORMAL),
476 alignment(LYX_ALIGN_CENTER),
477 valignment(LYX_VALIGN_TOP),
484 inset(new InsetTableCell(buf, this, &table))
486 inset->setBuffer(const_cast<Buffer &>(buf));
487 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
491 Tabular::CellData::CellData(CellData const & cs)
494 multicolumn(cs.multicolumn),
495 alignment(cs.alignment),
496 valignment(cs.valignment),
497 top_line(cs.top_line),
498 bottom_line(cs.bottom_line),
499 left_line(cs.left_line),
500 right_line(cs.right_line),
503 align_special(cs.align_special),
505 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
507 inset->setCellData(this);
511 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
518 void Tabular::CellData::swap(CellData & rhs)
520 std::swap(cellno, rhs.cellno);
521 std::swap(width, rhs.width);
522 std::swap(multicolumn, rhs.multicolumn);
523 std::swap(alignment, rhs.alignment);
524 std::swap(valignment, rhs.valignment);
525 std::swap(top_line, rhs.top_line);
526 std::swap(bottom_line, rhs.bottom_line);
527 std::swap(left_line, rhs.left_line);
528 std::swap(right_line, rhs.right_line);
529 std::swap(usebox, rhs.usebox);
530 std::swap(rotate, rhs.rotate);
531 std::swap(align_special, rhs.align_special);
532 p_width.swap(rhs.p_width);
533 inset.swap(rhs.inset);
537 Tabular::RowData::RowData()
540 top_space_default(false),
541 bottom_space_default(false),
542 interline_space_default(false),
551 Tabular::ColumnData::ColumnData()
552 : alignment(LYX_ALIGN_CENTER),
553 valignment(LYX_VALIGN_TOP),
559 Tabular::ltType::ltType()
572 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
574 init(buffer, rows_arg, columns_arg);
578 // activates all lines and sets all widths to 0
579 void Tabular::init(Buffer const & buf, row_type rows_arg,
580 col_type columns_arg)
583 row_info = row_vector(rows_arg);
584 column_info = column_vector(columns_arg);
585 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf, *this)));
586 row_info.reserve(10);
587 column_info.reserve(10);
588 cell_info.reserve(100);
590 is_long_tabular = false;
592 use_booktabs = false;
593 // set silly default lines
594 for (row_type i = 0; i < rowCount(); ++i)
595 for (col_type j = 0; j < columnCount(); ++j) {
596 cell_info[i][j].top_line = true;
597 cell_info[i][j].left_line = true;
598 cell_info[i][j].bottom_line = i == 0 || i == rowCount() - 1;
599 cell_info[i][j].right_line = j == columnCount() - 1;
604 void Tabular::appendRow(idx_type const cell)
606 BufferParams const & bp = buffer().params();
607 row_type const row = cellRow(cell);
609 row_vector::iterator rit = row_info.begin() + row;
610 row_info.insert(rit, RowData());
611 // now set the values of the row before
612 row_info[row] = row_info[row + 1];
614 row_type const nrows = rowCount();
615 col_type const ncols = columnCount();
617 cell_vvector old(nrows - 1);
618 for (row_type i = 0; i < nrows - 1; ++i)
619 swap(cell_info[i], old[i]);
621 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer(), *this)));
623 for (row_type i = 0; i <= row; ++i)
624 swap(cell_info[i], old[i]);
625 for (row_type i = row + 2; i < nrows; ++i)
626 swap(cell_info[i], old[i - 1]);
629 for (col_type j = 0; j < ncols; ++j)
630 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
636 void Tabular::deleteRow(row_type const row)
638 // Not allowed to delete last row
642 row_info.erase(row_info.begin() + row);
643 cell_info.erase(cell_info.begin() + row);
648 void Tabular::copyRow(row_type const row)
650 row_info.insert(row_info.begin() + row, row_info[row]);
651 cell_info.insert(cell_info.begin() + row, cell_info[row]);
653 if (buffer().params().trackChanges)
654 for (col_type j = 0; j < columnCount(); ++j)
655 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
661 void Tabular::appendColumn(idx_type const cell)
663 col_type const column = cellColumn(cell);
664 column_vector::iterator cit = column_info.begin() + column + 1;
665 column_info.insert(cit, ColumnData());
666 col_type const ncols = columnCount();
667 // set the column values of the column before
668 column_info[column + 1] = column_info[column];
670 for (row_type i = 0; i < rowCount(); ++i) {
671 cell_info[i].insert(cell_info[i].begin() + column + 1, CellData(buffer(), *this));
672 col_type c = column + 2;
674 && cell_info[i][c].multicolumn == CELL_PART_OF_MULTICOLUMN) {
675 cell_info[i][c].multicolumn = CELL_NORMAL;
680 for (row_type i = 0; i < rowCount(); ++i) {
681 cell_info[i][column + 1].inset->clear();
682 if (buffer().params().trackChanges)
683 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
689 void Tabular::deleteColumn(col_type const column)
691 // Not allowed to delete last column
692 if (columnCount() == 1)
695 column_info.erase(column_info.begin() + column);
696 for (row_type i = 0; i < rowCount(); ++i) {
697 // Care about multicolumn cells
698 if (column + 1 < columnCount() &&
699 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
700 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
701 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
703 cell_info[i].erase(cell_info[i].begin() + column);
709 void Tabular::copyColumn(col_type const column)
711 BufferParams const & bp = buffer().params();
712 column_info.insert(column_info.begin() + column, column_info[column]);
714 for (row_type i = 0; i < rowCount(); ++i)
715 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
718 for (row_type i = 0; i < rowCount(); ++i)
719 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
724 void Tabular::updateIndexes()
726 col_type ncols = columnCount();
727 row_type nrows = rowCount();
729 for (row_type row = 0; row < nrows; ++row)
730 for (col_type column = 0; column < ncols; ++column) {
731 if (!isPartOfMultiColumn(row, column))
733 cell_info[row][column].cellno = numberofcells - 1;
736 rowofcell.resize(numberofcells);
737 columnofcell.resize(numberofcells);
739 for (row_type row = 0; row < nrows; ++row)
740 for (col_type column = 0; column < ncols; ++column) {
741 if (isPartOfMultiColumn(row, column))
744 columnofcell[i] = column;
750 Tabular::idx_type Tabular::cellCount() const
752 return numberofcells;
756 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
758 row_type const row = cellRow(cell);
760 for (col_type i = 0; i < columnCount(); ++i)
761 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
767 bool Tabular::topLine(idx_type const cell) const
769 return cellinfo_of_cell(cell).top_line;
773 bool Tabular::bottomLine(idx_type const cell) const
775 return cellinfo_of_cell(cell).bottom_line;
779 bool Tabular::leftLine(idx_type cell) const
783 return cellinfo_of_cell(cell).left_line;
787 bool Tabular::rightLine(idx_type cell) const
791 return cellinfo_of_cell(cell).right_line;
795 bool Tabular::topAlreadyDrawn(idx_type cell) const
797 row_type row = cellRow(cell);
800 idx_type i = cellIndex(row - 1, cellColumn(cell));
801 return !rowBottomLine(row - 1) && bottomLine(i);
805 bool Tabular::leftAlreadyDrawn(idx_type cell) const
807 col_type col = cellColumn(cell);
810 idx_type i = cellIndex(cellRow(cell), col - 1);
811 return !columnRightLine(col - 1) && rightLine(i);
815 bool Tabular::isLastRow(idx_type cell) const
817 return cellRow(cell) == rowCount() - 1;
821 int Tabular::getAdditionalHeight(row_type row) const
823 if (!row || row >= rowCount())
826 int const interline_space = row_info[row - 1].interline_space_default ?
828 row_info[row - 1].interline_space.inPixels(width());
829 if (rowTopLine(row) && rowBottomLine(row - 1))
830 return interline_space + WIDTH_OF_LINE;
831 return interline_space;
835 int Tabular::getAdditionalWidth(idx_type cell) const
837 // internally already set in setCellWidth
838 // used to get it back in text.cpp
839 col_type c = cellColumn(cell);
840 if (c < columnCount() - 1
841 && columnRightLine(c) && columnLeftLine(c + 1)
842 && cellinfo_of_cell(cell).multicolumn == CELL_NORMAL)
843 return WIDTH_OF_LINE;
848 // returns the maximum over all rows
849 int Tabular::columnWidth(idx_type cell) const
851 col_type const column1 = cellColumn(cell);
852 col_type const column2 = cellRightColumn(cell);
854 for (col_type i = column1; i <= column2; ++i)
855 result += column_info[i].width;
860 int Tabular::width() const
863 for (col_type i = 0; i < columnCount(); ++i)
864 width += column_info[i].width;
869 // returns true if a complete update is necessary, otherwise false
870 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
872 if (!isMultiColumn(cell))
875 row_type const row = cellRow(cell);
876 col_type const column1 = cellColumn(cell);
877 col_type const column2 = cellRightColumn(cell);
878 int const old_val = cell_info[row][column2].width;
880 // first set columns to 0 so we can calculate the right width
881 for (col_type i = column1; i <= column2; ++i) {
882 cell_info[row][i].width = 0;
884 // set the width to MAX_WIDTH until width > 0
885 int width = new_width + 2 * WIDTH_OF_LINE;
886 col_type i = column1;
887 for (; i < column2 && width > column_info[i].width; ++i) {
888 cell_info[row][i].width = column_info[i].width;
889 width -= column_info[i].width;
892 cell_info[row][i].width = width;
894 if (old_val != cell_info[row][column2].width) {
895 // in this case we have to recalculate all multicolumn cells which
896 // have this column as one of theirs but not as last one
897 calculate_width_of_column_NMC(i);
898 recalculateMulticolumnsOfColumn(i);
899 calculate_width_of_column(i);
905 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
907 // the last column does not have to be recalculated because all
908 // multicolumns will have here there last multicolumn cell which
909 // always will have the whole rest of the width of the cell.
910 if (columnCount() < 2 || column > (columnCount() - 2))
912 for (row_type row = 0; row < rowCount(); ++row) {
913 int mc = cell_info[row][column].multicolumn;
914 int nmc = cell_info[row][column+1].multicolumn;
915 // we only have to update multicolumns which do not have this
916 // column as their last column!
917 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
918 (mc == CELL_PART_OF_MULTICOLUMN &&
919 nmc == CELL_PART_OF_MULTICOLUMN))
921 idx_type const cellno = cell_info[row][column].cellno;
922 setWidthOfMulticolCell(cellno,
923 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
929 void Tabular::setCellWidth(idx_type cell, int new_width)
931 row_type const row = cellRow(cell);
932 col_type const col = cellColumn(cell);
937 if (col < columnCount() - 1 && columnRightLine(col) &&
938 columnLeftLine(col + 1)) {
939 add_width = WIDTH_OF_LINE;
942 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
945 if (isMultiColumnReal(cell)) {
946 tmp = setWidthOfMulticolCell(cell, new_width);
948 width = new_width + 2 * WIDTH_OF_LINE + add_width;
949 cell_info[row][col].width = width;
950 tmp = calculate_width_of_column_NMC(col);
952 recalculateMulticolumnsOfColumn(col);
955 for (col_type i = 0; i < columnCount(); ++i)
956 calculate_width_of_column(i);
961 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
964 if (!isMultiColumn(cell) || onlycolumn)
965 column_info[cellColumn(cell)].alignment = align;
967 cellinfo_of_cell(cell).alignment = align;
971 void Tabular::setVAlignment(idx_type cell, VAlignment align,
974 if (!isMultiColumn(cell) || onlycolumn)
975 column_info[cellColumn(cell)].valignment = align;
977 cellinfo_of_cell(cell).valignment = align;
984 * Allow line and paragraph breaks for fixed width cells or disallow them,
985 * merge cell paragraphs and reset layout to standard for variable width
988 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
990 inset->setAutoBreakRows(fixedWidth);
994 // merge all paragraphs to one
995 BufferParams const & bp = cur.bv().buffer().params();
996 while (inset->paragraphs().size() > 1)
997 mergeParagraph(bp, inset->paragraphs(), 0);
1001 // undo information has already been recorded
1002 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1003 bp.documentClass().emptyLayoutName());
1010 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1011 Length const & width)
1013 col_type const j = cellColumn(cell);
1015 column_info[j].p_width = width;
1016 for (row_type i = 0; i < rowCount(); ++i) {
1017 idx_type const cell = cellIndex(i, j);
1018 // because of multicolumns
1019 toggleFixedWidth(cur, getCellInset(cell).get(),
1020 !getPWidth(cell).zero());
1022 // cur paragraph can become invalid after paragraphs were merged
1023 if (cur.pit() > cur.lastpit())
1024 cur.pit() = cur.lastpit();
1025 // cur position can become invalid after newlines were removed
1026 if (cur.pos() > cur.lastpos())
1027 cur.pos() = cur.lastpos();
1031 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1032 Length const & width)
1034 if (!isMultiColumn(cell))
1037 cellinfo_of_cell(cell).p_width = width;
1038 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1039 // cur paragraph can become invalid after paragraphs were merged
1040 if (cur.pit() > cur.lastpit())
1041 cur.pit() = cur.lastpit();
1042 // cur position can become invalid after newlines were removed
1043 if (cur.pos() > cur.lastpos())
1044 cur.pos() = cur.lastpos();
1049 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1050 Tabular::Feature what)
1052 if (what == SET_SPECIAL_MULTI)
1053 cellinfo_of_cell(cell).align_special = special;
1055 column_info[cellColumn(cell)].align_special = special;
1059 void Tabular::setAllLines(idx_type cell, bool line)
1061 setTopLine(cell, line);
1062 setBottomLine(cell, line);
1063 setRightLine(cell, line);
1064 setLeftLine(cell, line);
1068 void Tabular::setTopLine(idx_type i, bool line)
1070 cellinfo_of_cell(i).top_line = line;
1074 void Tabular::setBottomLine(idx_type i, bool line)
1076 cellinfo_of_cell(i).bottom_line = line;
1080 void Tabular::setLeftLine(idx_type cell, bool line)
1082 cellinfo_of_cell(cell).left_line = line;
1086 void Tabular::setRightLine(idx_type cell, bool line)
1088 cellinfo_of_cell(cell).right_line = line;
1091 bool Tabular::rowTopLine(row_type r) const
1093 idx_type i0 = getFirstCellInRow(r);
1094 idx_type i1 = getLastCellInRow(r);
1095 bool all_rows_set = true;
1096 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1097 all_rows_set = cellinfo_of_cell(j).top_line;
1098 return all_rows_set;
1102 bool Tabular::rowBottomLine(row_type r) const
1104 idx_type i0 = getFirstCellInRow(r);
1105 idx_type i1 = getLastCellInRow(r);
1106 bool all_rows_set = true;
1107 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1108 all_rows_set = cellinfo_of_cell(j).bottom_line;
1109 return all_rows_set;
1113 bool Tabular::columnLeftLine(col_type c) const
1115 bool all_cols_set = true;
1116 row_type nrows = rowCount();
1117 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1118 idx_type i = cellIndex(r, c);
1119 if (columnSpan(i) == 1)
1120 all_cols_set = cellinfo_of_cell(i).left_line;
1122 return all_cols_set;
1126 bool Tabular::columnRightLine(col_type c) const
1128 bool all_cols_set = true;
1129 row_type nrows = rowCount();
1130 for (row_type r = 0; all_cols_set && r < nrows; ++r) {
1131 idx_type i = cellIndex(r, c);
1132 if (c == cellColumn(i) + columnSpan(i) - 1)
1133 all_cols_set = cellinfo_of_cell(i).right_line;
1135 return all_cols_set;
1139 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1141 if (!onlycolumn && isMultiColumn(cell))
1142 return cellinfo_of_cell(cell).alignment;
1143 return column_info[cellColumn(cell)].alignment;
1148 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1150 if (!onlycolumn && isMultiColumn(cell))
1151 return cellinfo_of_cell(cell).valignment;
1152 return column_info[cellColumn(cell)].valignment;
1156 Length const Tabular::getPWidth(idx_type cell) const
1158 if (isMultiColumn(cell))
1159 return cellinfo_of_cell(cell).p_width;
1160 return column_info[cellColumn(cell)].p_width;
1164 Length const Tabular::getColumnPWidth(idx_type cell) const
1166 return column_info[cellColumn(cell)].p_width;
1170 Length const Tabular::getMColumnPWidth(idx_type cell) const
1172 if (isMultiColumn(cell))
1173 return cellinfo_of_cell(cell).p_width;
1178 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1180 if (what == SET_SPECIAL_MULTI)
1181 return cellinfo_of_cell(cell).align_special;
1182 return column_info[cellColumn(cell)].align_special;
1186 int Tabular::cellWidth(idx_type cell) const
1188 row_type const row = cellRow(cell);
1189 col_type const column1 = cellColumn(cell);
1190 col_type const column2 = cellRightColumn(cell);
1192 for (col_type i = column1; i <= column2; ++i)
1193 result += cell_info[row][i].width;
1198 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1202 switch (getAlignment(cell)) {
1203 case LYX_ALIGN_CENTER:
1204 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1206 case LYX_ALIGN_RIGHT:
1207 x += columnWidth(cell) - cellWidth(cell);
1208 // + getAdditionalWidth(cell);
1211 // LYX_ALIGN_LEFT: nothing :-)
1215 // the LaTeX Way :-(
1221 bool Tabular::isFirstCellInRow(idx_type cell) const
1223 return cellColumn(cell) == 0;
1227 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1229 if (row > rowCount() - 1)
1230 row = rowCount() - 1;
1231 return cell_info[row][0].cellno;
1235 bool Tabular::isLastCellInRow(idx_type cell) const
1237 return cellRightColumn(cell) == columnCount() - 1;
1241 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1243 if (row > rowCount() - 1)
1244 row = rowCount() - 1;
1245 return cell_info[row][columnCount() - 1].cellno;
1249 void Tabular::calculate_width_of_column(col_type column)
1252 for (row_type i = 0; i < rowCount(); ++i)
1253 maximum = max(cell_info[i][column].width, maximum);
1254 column_info[column].width = maximum;
1259 // Calculate the columns regarding ONLY the normal cells and if this
1260 // column is inside a multicolumn cell then use it only if its the last
1261 // column of this multicolumn cell as this gives an added width to the
1262 // column, all the rest should be adapted!
1264 bool Tabular::calculate_width_of_column_NMC(col_type column)
1266 int const old_column_width = column_info[column].width;
1268 for (row_type i = 0; i < rowCount(); ++i) {
1269 idx_type cell = cellIndex(i, column);
1270 bool ismulti = isMultiColumnReal(cell);
1271 if ((!ismulti || column == cellRightColumn(cell)) &&
1272 cell_info[i][column].width > max)
1274 max = cell_info[i][column].width;
1277 column_info[column].width = max;
1278 return column_info[column].width != old_column_width;
1282 Tabular::row_type Tabular::cellRow(idx_type cell) const
1284 if (cell >= cellCount())
1285 return rowCount() - 1;
1288 return rowofcell[cell];
1292 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1294 if (cell >= cellCount())
1295 return columnCount() - 1;
1298 return columnofcell[cell];
1302 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1304 row_type const row = cellRow(cell);
1305 col_type column = cellColumn(cell);
1306 while (column < columnCount() - 1 &&
1307 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1313 void Tabular::write(ostream & os) const
1317 << write_attribute("version", 3)
1318 << write_attribute("rows", rowCount())
1319 << write_attribute("columns", columnCount())
1321 // global longtable options
1323 << write_attribute("rotate", rotate)
1324 << write_attribute("booktabs", use_booktabs)
1325 << write_attribute("islongtable", is_long_tabular)
1326 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1327 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1328 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1329 << write_attribute("headTopDL", endhead.topDL)
1330 << write_attribute("headBottomDL", endhead.bottomDL)
1331 << write_attribute("footTopDL", endfoot.topDL)
1332 << write_attribute("footBottomDL", endfoot.bottomDL)
1333 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1334 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1335 << write_attribute("lastFootEmpty", endlastfoot.empty)
1337 for (col_type j = 0; j < columnCount(); ++j) {
1339 << write_attribute("alignment", column_info[j].alignment)
1340 << write_attribute("valignment", column_info[j].valignment)
1341 << write_attribute("width", column_info[j].p_width.asString())
1342 << write_attribute("special", column_info[j].align_special)
1345 for (row_type i = 0; i < rowCount(); ++i) {
1346 static const string def("default");
1348 if (row_info[i].top_space_default)
1349 os << write_attribute("topspace", def);
1351 os << write_attribute("topspace", row_info[i].top_space);
1352 if (row_info[i].bottom_space_default)
1353 os << write_attribute("bottomspace", def);
1355 os << write_attribute("bottomspace", row_info[i].bottom_space);
1356 if (row_info[i].interline_space_default)
1357 os << write_attribute("interlinespace", def);
1359 os << write_attribute("interlinespace", row_info[i].interline_space);
1360 os << write_attribute("endhead", row_info[i].endhead)
1361 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1362 << write_attribute("endfoot", row_info[i].endfoot)
1363 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1364 << write_attribute("newpage", row_info[i].newpage)
1366 for (col_type j = 0; j < columnCount(); ++j) {
1368 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1369 << write_attribute("alignment", cell_info[i][j].alignment)
1370 << write_attribute("valignment", cell_info[i][j].valignment)
1371 << write_attribute("topline", cell_info[i][j].top_line)
1372 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1373 << write_attribute("leftline", cell_info[i][j].left_line)
1374 << write_attribute("rightline", cell_info[i][j].right_line)
1375 << write_attribute("rotate", cell_info[i][j].rotate)
1376 << write_attribute("usebox", cell_info[i][j].usebox)
1377 << write_attribute("width", cell_info[i][j].p_width)
1378 << write_attribute("special", cell_info[i][j].align_special)
1380 os << "\\begin_inset ";
1381 cell_info[i][j].inset->write(os);
1382 os << "\n\\end_inset\n"
1387 os << "</lyxtabular>\n";
1391 void Tabular::read(Lexer & lex)
1394 istream & is = lex.getStream();
1396 l_getline(is, line);
1397 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1398 BOOST_ASSERT(false);
1403 if (!getTokenValue(line, "version", version))
1405 BOOST_ASSERT(version >= 2);
1408 if (!getTokenValue(line, "rows", rows_arg))
1411 if (!getTokenValue(line, "columns", columns_arg))
1413 init(buffer(), rows_arg, columns_arg);
1414 l_getline(is, line);
1415 if (!prefixIs(line, "<features")) {
1416 lyxerr << "Wrong tabular format (expected <features ...> got"
1417 << line << ')' << endl;
1420 getTokenValue(line, "rotate", rotate);
1421 getTokenValue(line, "booktabs", use_booktabs);
1422 getTokenValue(line, "islongtable", is_long_tabular);
1423 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1424 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1425 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1426 getTokenValue(line, "headTopDL", endhead.topDL);
1427 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1428 getTokenValue(line, "footTopDL", endfoot.topDL);
1429 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1430 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1431 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1432 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1434 for (col_type j = 0; j < columnCount(); ++j) {
1436 if (!prefixIs(line,"<column")) {
1437 lyxerr << "Wrong tabular format (expected <column ...> got"
1438 << line << ')' << endl;
1441 getTokenValue(line, "alignment", column_info[j].alignment);
1442 getTokenValue(line, "valignment", column_info[j].valignment);
1443 getTokenValue(line, "width", column_info[j].p_width);
1444 getTokenValue(line, "special", column_info[j].align_special);
1447 for (row_type i = 0; i < rowCount(); ++i) {
1448 l_getline(is, line);
1449 if (!prefixIs(line, "<row")) {
1450 lyxerr << "Wrong tabular format (expected <row ...> got"
1451 << line << ')' << endl;
1454 getTokenValue(line, "topspace", row_info[i].top_space,
1455 row_info[i].top_space_default);
1456 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1457 row_info[i].bottom_space_default);
1458 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1459 row_info[i].interline_space_default);
1460 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1461 getTokenValue(line, "endhead", row_info[i].endhead);
1462 getTokenValue(line, "endfoot", row_info[i].endfoot);
1463 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1464 getTokenValue(line, "newpage", row_info[i].newpage);
1465 for (col_type j = 0; j < columnCount(); ++j) {
1466 l_getline(is, line);
1467 if (!prefixIs(line, "<cell")) {
1468 lyxerr << "Wrong tabular format (expected <cell ...> got"
1469 << line << ')' << endl;
1472 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1473 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1474 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1475 getTokenValue(line, "topline", cell_info[i][j].top_line);
1476 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1477 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1478 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1479 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1480 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1481 getTokenValue(line, "width", cell_info[i][j].p_width);
1482 getTokenValue(line, "special", cell_info[i][j].align_special);
1483 l_getline(is, line);
1484 if (prefixIs(line, "\\begin_inset")) {
1485 cell_info[i][j].inset->read(lex);
1486 l_getline(is, line);
1488 if (!prefixIs(line, "</cell>")) {
1489 lyxerr << "Wrong tabular format (expected </cell> got"
1490 << line << ')' << endl;
1494 l_getline(is, line);
1495 if (!prefixIs(line, "</row>")) {
1496 lyxerr << "Wrong tabular format (expected </row> got"
1497 << line << ')' << endl;
1501 while (!prefixIs(line, "</lyxtabular>")) {
1502 l_getline(is, line);
1508 bool Tabular::isMultiColumn(idx_type cell) const
1510 return cellinfo_of_cell(cell).multicolumn != CELL_NORMAL;
1514 bool Tabular::isMultiColumnReal(idx_type cell) const
1516 return cellColumn(cell) != cellRightColumn(cell) &&
1517 cellinfo_of_cell(cell).multicolumn != CELL_NORMAL;
1521 Tabular::CellData & Tabular::cellinfo_of_cell(idx_type cell) const
1523 return cell_info[cellRow(cell)][cellColumn(cell)];
1527 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1529 CellData & cs = cellinfo_of_cell(cell);
1530 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1531 cs.alignment = column_info[cellColumn(cell)].alignment;
1532 for (idx_type i = 1; i < number; ++i) {
1533 CellData & cs1 = cellinfo_of_cell(cell + i);
1534 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1535 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1538 setRightLine(cell, rightLine(cell + number - 1));
1543 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1545 row_type const row = cellRow(cell);
1546 col_type column = cellColumn(cell);
1547 idx_type result = 1;
1549 while (column < columnCount() &&
1550 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1559 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1561 row_type const row = cellRow(cell);
1562 col_type column = cellColumn(cell);
1564 idx_type result = 0;
1566 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1567 cell_info[row][column].multicolumn = CELL_NORMAL;
1569 while (column < columnCount() &&
1570 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1572 cell_info[row][column].multicolumn = CELL_NORMAL;
1582 void Tabular::setBookTabs(bool what)
1584 use_booktabs = what;
1588 bool Tabular::useBookTabs() const
1590 return use_booktabs;
1594 void Tabular::setLongTabular(bool what)
1596 is_long_tabular = what;
1600 bool Tabular::isLongTabular() const
1602 return is_long_tabular;
1606 void Tabular::setRotateTabular(bool flag)
1612 bool Tabular::getRotateTabular() const
1618 void Tabular::setRotateCell(idx_type cell, bool flag)
1620 cellinfo_of_cell(cell).rotate = flag;
1624 bool Tabular::getRotateCell(idx_type cell) const
1626 return cellinfo_of_cell(cell).rotate;
1630 bool Tabular::needRotating() const
1634 for (row_type i = 0; i < rowCount(); ++i)
1635 for (col_type j = 0; j < columnCount(); ++j)
1636 if (cell_info[i][j].rotate)
1642 bool Tabular::isLastCell(idx_type cell) const
1644 if (cell + 1 < cellCount())
1650 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1652 if (cellRow(cell) > 0)
1653 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1658 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1660 if (cellRow(cell) + 1 < rowCount())
1661 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1666 Tabular::idx_type Tabular::cellIndex(row_type row,
1667 col_type column) const
1669 BOOST_ASSERT(column != npos && column < columnCount() &&
1670 row != npos && row < rowCount());
1671 return cell_info[row][column].cellno;
1675 void Tabular::setUsebox(idx_type cell, BoxType type)
1677 cellinfo_of_cell(cell).usebox = type;
1681 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1683 if (column_info[cellColumn(cell)].p_width.zero() &&
1684 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1686 if (cellinfo_of_cell(cell).usebox > 1)
1687 return cellinfo_of_cell(cell).usebox;
1688 return useParbox(cell);
1693 // This are functions used for the longtable support
1695 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1701 row_info[row].endfirsthead = flag;
1705 row_info[row].endhead = flag;
1710 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1713 hd.set = haveLTHead();
1714 return row_info[row].endhead;
1718 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1721 hd.set = haveLTFirstHead();
1722 return row_info[row].endfirsthead;
1726 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1732 row_info[row].endlastfoot = flag;
1736 row_info[row].endfoot = flag;
1741 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1744 fd.set = haveLTFoot();
1745 return row_info[row].endfoot;
1749 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1752 fd.set = haveLTLastFoot();
1753 return row_info[row].endlastfoot;
1757 void Tabular::setLTNewPage(row_type row, bool what)
1759 row_info[row].newpage = what;
1763 bool Tabular::getLTNewPage(row_type row) const
1765 return row_info[row].newpage;
1769 bool Tabular::haveLTHead() const
1771 for (row_type i = 0; i < rowCount(); ++i)
1772 if (row_info[i].endhead)
1778 bool Tabular::haveLTFirstHead() const
1780 if (endfirsthead.empty)
1782 for (row_type i = 0; i < rowCount(); ++i)
1783 if (row_info[i].endfirsthead)
1789 bool Tabular::haveLTFoot() const
1791 for (row_type i = 0; i < rowCount(); ++i)
1792 if (row_info[i].endfoot)
1798 bool Tabular::haveLTLastFoot() const
1800 if (endlastfoot.empty)
1802 for (row_type i = 0; i < rowCount(); ++i)
1803 if (row_info[i].endlastfoot)
1809 // end longtable support functions
1811 void Tabular::setRowAscent(row_type row, int height)
1813 if (row >= rowCount() || row_info[row].ascent == height)
1815 row_info[row].ascent = height;
1819 void Tabular::setRowDescent(row_type row, int height)
1821 if (row >= rowCount() || row_info[row].descent == height)
1823 row_info[row].descent = height;
1827 int Tabular::rowAscent(row_type row) const
1829 if (row >= rowCount())
1831 return row_info[row].ascent;
1835 int Tabular::rowDescent(row_type row) const
1837 BOOST_ASSERT(row < rowCount());
1838 return row_info[row].descent;
1842 int Tabular::height() const
1845 for (row_type row = 0; row < rowCount(); ++row)
1846 height += rowAscent(row) + rowDescent(row) +
1847 getAdditionalHeight(row);
1852 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1854 BOOST_ASSERT(row < rowCount());
1855 BOOST_ASSERT(column < columnCount());
1856 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1860 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1862 // FIXME: assert or return 0 as in TeXBottomHLine()?
1863 BOOST_ASSERT(row != npos);
1864 BOOST_ASSERT(row < rowCount());
1866 idx_type const fcell = getFirstCellInRow(row);
1867 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1870 for (idx_type i = fcell; i < n; ++i) {
1874 if (use_booktabs && row == 0) {
1877 } else if (tmp == n - fcell) {
1878 os << (use_booktabs ? "\\midrule " : "\\hline ");
1880 for (idx_type i = fcell; i < n; ++i) {
1882 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1883 << cellColumn(i) + 1
1885 << cellRightColumn(i) + 1
1897 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1899 // FIXME: return 0 or assert as in TeXTopHLine()?
1900 if (row == npos || row >= rowCount())
1903 idx_type const fcell = getFirstCellInRow(row);
1904 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1907 for (idx_type i = fcell; i < n; ++i) {
1911 if (use_booktabs && row == rowCount() - 1) {
1912 if (bottomLine(fcell))
1913 os << "\\bottomrule";
1914 } else if (tmp == n - fcell) {
1915 os << (use_booktabs ? "\\midrule" : "\\hline");
1917 for (idx_type i = fcell; i < n; ++i) {
1918 if (bottomLine(i)) {
1919 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1920 << cellColumn(i) + 1
1922 << cellRightColumn(i) + 1
1934 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
1938 if (getRotateCell(cell)) {
1939 os << "\\begin{sideways}\n";
1942 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1943 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1944 col_type c = cellColumn(cell);
1945 if (isMultiColumn(cell)
1946 || (leftLine(cell) && !columnLeftLine(c))
1947 || (rightLine(cell) && !columnRightLine(c))) {
1948 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1951 if (!cellinfo_of_cell(cell).align_special.empty()) {
1952 os << cellinfo_of_cell(cell).align_special;
1954 if (!getPWidth(cell).zero()) {
1956 case LYX_VALIGN_TOP:
1959 case LYX_VALIGN_MIDDLE:
1962 case LYX_VALIGN_BOTTOM:
1967 << from_ascii(getPWidth(cell).asLatexString())
1971 case LYX_ALIGN_LEFT:
1974 case LYX_ALIGN_RIGHT:
1981 } // end if else !getPWidth
1982 } // end if else !cellinfo_of_cell
1983 if (rightLine(cell))
1987 if (getUsebox(cell) == BOX_PARBOX) {
1990 case LYX_VALIGN_TOP:
1993 case LYX_VALIGN_MIDDLE:
1996 case LYX_VALIGN_BOTTOM:
2000 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2002 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2003 os << "\\begin{minipage}[";
2005 case LYX_VALIGN_TOP:
2008 case LYX_VALIGN_MIDDLE:
2011 case LYX_VALIGN_BOTTOM:
2015 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2023 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2028 if (getUsebox(cell) == BOX_PARBOX)
2030 else if (getUsebox(cell) == BOX_MINIPAGE) {
2031 os << "%\n\\end{minipage}";
2034 col_type c = cellColumn(cell);
2035 if (isMultiColumn(cell)
2036 || (leftLine(cell) && !columnLeftLine(c))
2037 || (rightLine(cell) && !columnRightLine(c))) {
2040 if (getRotateCell(cell)) {
2041 os << "%\n\\end{sideways}";
2048 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2049 OutputParams const & runparams) const
2051 if (!is_long_tabular)
2055 // output header info
2057 if (endhead.topDL) {
2061 for (row_type i = 0; i < rowCount(); ++i) {
2062 if (row_info[i].endhead) {
2063 ret += TeXRow(os, i, runparams);
2066 if (endhead.bottomDL) {
2070 os << "\\endhead\n";
2072 if (endfirsthead.empty) {
2073 os << "\\endfirsthead\n";
2077 // output firstheader info
2078 if (haveLTFirstHead()) {
2079 if (endfirsthead.topDL) {
2083 for (row_type i = 0; i < rowCount(); ++i) {
2084 if (row_info[i].endfirsthead) {
2085 ret += TeXRow(os, i, runparams);
2088 if (endfirsthead.bottomDL) {
2092 os << "\\endfirsthead\n";
2095 // output footer info
2097 if (endfoot.topDL) {
2101 for (row_type i = 0; i < rowCount(); ++i) {
2102 if (row_info[i].endfoot) {
2103 ret += TeXRow(os, i, runparams);
2106 if (endfoot.bottomDL) {
2110 os << "\\endfoot\n";
2112 if (endlastfoot.empty) {
2113 os << "\\endlastfoot\n";
2117 // output lastfooter info
2118 if (haveLTLastFoot()) {
2119 if (endlastfoot.topDL) {
2123 for (row_type i = 0; i < rowCount(); ++i) {
2124 if (row_info[i].endlastfoot) {
2125 ret += TeXRow(os, i, runparams);
2128 if (endlastfoot.bottomDL) {
2132 os << "\\endlastfoot\n";
2139 bool Tabular::isValidRow(row_type row) const
2141 if (!is_long_tabular)
2143 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2144 !row_info[row].endfoot && !row_info[row].endlastfoot;
2148 int Tabular::TeXRow(odocstream & os, row_type i,
2149 OutputParams const & runparams) const
2151 idx_type cell = cellIndex(i, 0);
2152 int ret = TeXTopHLine(os, i);
2153 if (row_info[i].top_space_default) {
2155 os << "\\addlinespace\n";
2157 os << "\\noalign{\\vskip\\doublerulesep}\n";
2159 } else if(!row_info[i].top_space.zero()) {
2161 os << "\\addlinespace["
2162 << from_ascii(row_info[i].top_space.asLatexString())
2165 os << "\\noalign{\\vskip"
2166 << from_ascii(row_info[i].top_space.asLatexString())
2172 for (col_type j = 0; j < columnCount(); ++j) {
2173 if (isPartOfMultiColumn(i, j))
2175 ret += TeXCellPreamble(os, cell);
2176 shared_ptr<InsetTableCell> inset = getCellInset(cell);
2178 Paragraph const & par = inset->paragraphs().front();
2179 bool rtl = par.isRTL(buffer().params())
2181 && getPWidth(cell).zero();
2184 if (par.getParLanguage(buffer().params())->lang() ==
2187 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2189 // currently, remaning RTL languages are arabic_arabtex and hebrew
2193 ret += inset->latex(os, runparams);
2197 ret += TeXCellPostamble(os, cell);
2198 if (!isLastCellInRow(cell)) { // not last cell in row
2203 os << "\\tabularnewline";
2204 if (row_info[i].bottom_space_default) {
2206 os << "\\addlinespace";
2208 os << "[\\doublerulesep]";
2209 } else if (!row_info[i].bottom_space.zero()) {
2211 os << "\\addlinespace";
2213 << from_ascii(row_info[i].bottom_space.asLatexString())
2218 ret += TeXBottomHLine(os, i);
2219 if (row_info[i].interline_space_default) {
2221 os << "\\addlinespace\n";
2223 os << "\\noalign{\\vskip\\doublerulesep}\n";
2225 } else if (!row_info[i].interline_space.zero()) {
2227 os << "\\addlinespace["
2228 << from_ascii(row_info[i].interline_space.asLatexString())
2231 os << "\\noalign{\\vskip"
2232 << from_ascii(row_info[i].interline_space.asLatexString())
2240 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2244 //+---------------------------------------------------------------------
2245 //+ first the opening preamble +
2246 //+---------------------------------------------------------------------
2249 os << "\\begin{sideways}\n";
2252 if (is_long_tabular)
2253 os << "\\begin{longtable}{";
2255 os << "\\begin{tabular}{";
2257 for (col_type i = 0; i < columnCount(); ++i) {
2258 if (!use_booktabs && columnLeftLine(i))
2260 if (!column_info[i].align_special.empty()) {
2261 os << column_info[i].align_special;
2263 if (!column_info[i].p_width.zero()) {
2264 switch (column_info[i].alignment) {
2265 case LYX_ALIGN_LEFT:
2266 os << ">{\\raggedright}";
2268 case LYX_ALIGN_RIGHT:
2269 os << ">{\\raggedleft}";
2271 case LYX_ALIGN_CENTER:
2272 os << ">{\\centering}";
2274 case LYX_ALIGN_NONE:
2275 case LYX_ALIGN_BLOCK:
2276 case LYX_ALIGN_LAYOUT:
2277 case LYX_ALIGN_SPECIAL:
2281 switch (column_info[i].valignment) {
2282 case LYX_VALIGN_TOP:
2285 case LYX_VALIGN_MIDDLE:
2288 case LYX_VALIGN_BOTTOM:
2293 << from_ascii(column_info[i].p_width.asLatexString())
2296 switch (column_info[i].alignment) {
2297 case LYX_ALIGN_LEFT:
2300 case LYX_ALIGN_RIGHT:
2307 } // end if else !column_info[i].p_width
2308 } // end if else !column_info[i].align_special
2309 if (!use_booktabs && columnRightLine(i))
2315 ret += TeXLongtableHeaderFooter(os, runparams);
2317 //+---------------------------------------------------------------------
2318 //+ the single row and columns (cells) +
2319 //+---------------------------------------------------------------------
2321 for (row_type i = 0; i < rowCount(); ++i) {
2322 if (isValidRow(i)) {
2323 ret += TeXRow(os, i, runparams);
2324 if (is_long_tabular && row_info[i].newpage) {
2325 os << "\\newpage\n";
2331 //+---------------------------------------------------------------------
2332 //+ the closing of the tabular +
2333 //+---------------------------------------------------------------------
2335 if (is_long_tabular)
2336 os << "\\end{longtable}";
2338 os << "\\end{tabular}";
2340 os << "\n\\end{sideways}";
2348 int Tabular::docbookRow(odocstream & os, row_type row,
2349 OutputParams const & runparams) const
2352 idx_type cell = getFirstCellInRow(row);
2355 for (col_type j = 0; j < columnCount(); ++j) {
2356 if (isPartOfMultiColumn(row, j))
2359 os << "<entry align=\"";
2360 switch (getAlignment(cell)) {
2361 case LYX_ALIGN_LEFT:
2364 case LYX_ALIGN_RIGHT:
2372 os << "\" valign=\"";
2373 switch (getVAlignment(cell)) {
2374 case LYX_VALIGN_TOP:
2377 case LYX_VALIGN_BOTTOM:
2380 case LYX_VALIGN_MIDDLE:
2385 if (isMultiColumn(cell)) {
2386 os << " namest=\"col" << j << "\" ";
2387 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2391 ret += getCellInset(cell)->docbook(os, runparams);
2400 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2404 //+---------------------------------------------------------------------
2405 //+ first the opening preamble +
2406 //+---------------------------------------------------------------------
2408 os << "<tgroup cols=\"" << columnCount()
2409 << "\" colsep=\"1\" rowsep=\"1\">\n";
2411 for (col_type i = 0; i < columnCount(); ++i) {
2412 os << "<colspec colname=\"col" << i << "\" align=\"";
2413 switch (column_info[i].alignment) {
2414 case LYX_ALIGN_LEFT:
2417 case LYX_ALIGN_RIGHT:
2425 if (runparams.flavor == OutputParams::XML)
2431 //+---------------------------------------------------------------------
2432 //+ Long Tabular case +
2433 //+---------------------------------------------------------------------
2435 // output header info
2436 if (haveLTHead() || haveLTFirstHead()) {
2439 for (row_type i = 0; i < rowCount(); ++i) {
2440 if (row_info[i].endhead || row_info[i].endfirsthead) {
2441 ret += docbookRow(os, i, runparams);
2447 // output footer info
2448 if (haveLTFoot() || haveLTLastFoot()) {
2451 for (row_type i = 0; i < rowCount(); ++i) {
2452 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2453 ret += docbookRow(os, i, runparams);
2460 //+---------------------------------------------------------------------
2461 //+ the single row and columns (cells) +
2462 //+---------------------------------------------------------------------
2466 for (row_type i = 0; i < rowCount(); ++i) {
2467 if (isValidRow(i)) {
2468 ret += docbookRow(os, i, runparams);
2473 //+---------------------------------------------------------------------
2474 //+ the closing of the tabular +
2475 //+---------------------------------------------------------------------
2484 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2485 vector<unsigned int> const & clen) const
2487 idx_type const fcell = getFirstCellInRow(row);
2488 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2491 for (idx_type i = fcell; i < n; ++i) {
2501 for (idx_type i = fcell; i < n; ++i) {
2512 col_type column = cellColumn(i);
2513 int len = clen[column];
2514 while (column < columnCount() - 1
2515 && isPartOfMultiColumn(row, ++column))
2516 len += clen[column] + 4;
2517 os << docstring(len, ch);
2532 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2533 vector<unsigned int> const & clen) const
2535 idx_type const fcell = getFirstCellInRow(row);
2536 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2539 for (idx_type i = fcell; i < n; ++i) {
2540 if (bottomLine(i)) {
2549 for (idx_type i = fcell; i < n; ++i) {
2550 if (bottomLine(i)) {
2560 col_type column = cellColumn(i);
2561 int len = clen[column];
2562 while (column < columnCount() -1
2563 && isPartOfMultiColumn(row, ++column))
2564 len += clen[column] + 4;
2565 os << docstring(len, ch);
2566 if (bottomLine(i)) {
2580 void Tabular::plaintextPrintCell(odocstream & os,
2581 OutputParams const & runparams,
2582 idx_type cell, row_type row, col_type column,
2583 vector<unsigned int> const & clen,
2584 bool onlydata) const
2586 odocstringstream sstr;
2587 getCellInset(cell)->plaintext(sstr, runparams);
2599 unsigned int len1 = sstr.str().length();
2600 unsigned int len2 = clen[column];
2601 while (column < columnCount() -1
2602 && isPartOfMultiColumn(row, ++column))
2603 len2 += clen[column] + 4;
2606 switch (getAlignment(cell)) {
2608 case LYX_ALIGN_LEFT:
2611 case LYX_ALIGN_RIGHT:
2615 case LYX_ALIGN_CENTER:
2621 os << docstring(len1, ' ') << sstr.str()
2622 << docstring(len2, ' ');
2624 if (rightLine(cell))
2631 void Tabular::plaintext(odocstream & os,
2632 OutputParams const & runparams, int const depth,
2633 bool onlydata, char_type delim) const
2635 // first calculate the width of the single columns
2636 vector<unsigned int> clen(columnCount());
2639 // first all non (real) multicolumn cells!
2640 for (col_type j = 0; j < columnCount(); ++j) {
2642 for (row_type i = 0; i < rowCount(); ++i) {
2643 idx_type cell = cellIndex(i, j);
2644 if (isMultiColumnReal(cell))
2646 odocstringstream sstr;
2647 getCellInset(cell)->plaintext(sstr, runparams);
2648 if (clen[j] < sstr.str().length())
2649 clen[j] = sstr.str().length();
2652 // then all (real) multicolumn cells!
2653 for (col_type j = 0; j < columnCount(); ++j) {
2654 for (row_type i = 0; i < rowCount(); ++i) {
2655 idx_type cell = cellIndex(i, j);
2656 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2658 odocstringstream sstr;
2659 getCellInset(cell)->plaintext(sstr, runparams);
2660 int len = int(sstr.str().length());
2661 idx_type const n = columnSpan(cell);
2662 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2664 if (len > int(clen[j + n - 1]))
2665 clen[j + n - 1] = len;
2670 for (row_type i = 0; i < rowCount(); ++i) {
2671 if (!onlydata && plaintextTopHLine(os, i, clen))
2672 os << docstring(depth * 2, ' ');
2673 for (col_type j = 0; j < columnCount(); ++j) {
2674 if (isPartOfMultiColumn(i, j))
2676 if (onlydata && j > 0)
2677 // we don't use operator<< for single UCS4 character.
2678 // see explanation in docstream.h
2680 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2685 os << docstring(depth * 2, ' ');
2686 if (plaintextBottomHLine(os, i, clen))
2687 os << docstring(depth * 2, ' ');
2693 shared_ptr<InsetTableCell> Tabular::getCellInset(idx_type cell) const
2695 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2699 shared_ptr<InsetTableCell> Tabular::getCellInset(row_type row,
2700 col_type column) const
2702 return cell_info[row][column].inset;
2706 void Tabular::setCellInset(row_type row, col_type column,
2707 shared_ptr<InsetTableCell> ins) const
2709 CellData & cd = cell_info[row][column];
2711 // reset the InsetTableCell's pointers
2712 ins->setCellData(&cd);
2713 ins->setTabular(this);
2718 Tabular::getCellFromInset(Inset const * inset) const
2720 // is this inset part of the tabular?
2722 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2723 BOOST_ASSERT(false);
2726 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2727 if (getCellInset(cell).get() == inset) {
2728 LYXERR(Debug::INSETTEXT, "Tabular::getCellFromInset: "
2729 << "cell=" << cell);
2733 // We should have found a cell at this point
2734 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2735 << inset << " not found!" << endl;
2736 BOOST_ASSERT(false);
2742 void Tabular::validate(LaTeXFeatures & features) const
2744 features.require("NeedTabularnewline");
2746 features.require("booktabs");
2747 if (isLongTabular())
2748 features.require("longtable");
2750 features.require("rotating");
2751 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2752 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2753 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2754 features.require("array");
2755 getCellInset(cell)->validate(features);
2760 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2762 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2763 ParagraphList::const_iterator cit = parlist.begin();
2764 ParagraphList::const_iterator end = parlist.end();
2766 for (; cit != end; ++cit)
2767 for (int i = 0; i < cit->size(); ++i)
2768 if (cit->isNewline(i))
2775 /////////////////////////////////////////////////////////////////////
2779 /////////////////////////////////////////////////////////////////////
2781 InsetTableCell::InsetTableCell(Buffer const & buf,
2782 Tabular::CellData const * cell, Tabular const * table)
2783 : InsetText(buf), cell_data_(cell), table_(table)
2787 bool InsetTableCell::forceEmptyLayout(idx_type) const
2789 BOOST_ASSERT(table_);
2790 BOOST_ASSERT(cell_data_);
2791 return table_->getPWidth(cell_data_->cellno).zero();
2794 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2796 BOOST_ASSERT(table_);
2797 BOOST_ASSERT(cell_data_);
2798 return !table_->getPWidth(cell_data_->cellno).zero();
2801 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2802 FuncStatus & status) const
2805 switch (cmd.action) {
2807 enabled = !forceEmptyLayout();
2809 case LFUN_LAYOUT_PARAGRAPH:
2810 enabled = allowParagraphCustomization();
2813 return InsetText::getStatus(cur, cmd, status);
2815 status.enabled(enabled);
2819 /////////////////////////////////////////////////////////////////////
2823 /////////////////////////////////////////////////////////////////////
2825 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2827 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0)
2829 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2833 InsetTabular::InsetTabular(InsetTabular const & tab)
2834 : Inset(tab), tabular(tab.tabular), scx_(0)
2836 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2840 InsetTabular::~InsetTabular()
2842 InsetTabularMailer(*this).hideDialog();
2846 bool InsetTabular::insetAllowed(InsetCode code) const
2848 if (code == MATHMACRO_CODE)
2855 void InsetTabular::write(ostream & os) const
2857 os << "Tabular" << endl;
2862 void InsetTabular::read(Lexer & lex)
2864 bool const old_format = (lex.getString() == "\\LyXTable");
2872 string token = lex.getString();
2873 while (lex && token != "\\end_inset") {
2875 token = lex.getString();
2878 lex.printError("Missing \\end_inset at this point. "
2884 int InsetTabular::rowFromY(Cursor & cur, int y) const
2886 // top y coordinate of tabular
2887 int h = yo(cur.bv()) - tabular.rowAscent(0);
2888 size_t nrows = tabular.rowCount();
2890 for (;r < nrows && y > h; ++r) {
2891 h += tabular.rowAscent(r);
2892 h += tabular.rowDescent(r);
2893 h += tabular.getAdditionalHeight(r);
2899 int InsetTabular::columnFromX(Cursor & cur, int x) const
2901 // left x coordinate of tabular
2902 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2903 size_t ncols = tabular.columnCount();
2905 for (;c < ncols && x > w; ++c) {
2906 w += tabular.columnWidth(c);
2912 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2914 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2915 // mi.base.textwidth << "\n";
2917 lyxerr << "InsetTabular::metrics: need bv" << endl;
2918 BOOST_ASSERT(false);
2922 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2925 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2926 if (tabular.isPartOfMultiColumn(i, j))
2927 // Multicolumn cell, but not first one
2932 if (tabular.cell_info[i][j].multicolumn ==
2933 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2934 p_width = tabular.cellinfo_of_cell(cell).p_width;
2936 p_width = tabular.column_info[j].p_width;
2937 if (!p_width.zero())
2938 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2939 tabular.getCellInset(cell)->metrics(m, dim);
2940 if (!p_width.zero())
2941 dim.wid = m.base.textwidth;
2942 tabular.setCellWidth(cell, dim.wid);
2943 if (p_width.zero()) {
2944 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2945 // FIXME there must be a way to get rid of
2946 // the second metrics call
2947 tabular.getCellInset(cell)->metrics(m, dim);
2949 maxAsc = max(maxAsc, dim.asc);
2950 maxDesc = max(maxDesc, dim.des);
2953 int const top_space = tabular.row_info[i].top_space_default ?
2954 default_line_space :
2955 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2956 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2957 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2958 default_line_space :
2959 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2960 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2963 dim.asc = tabular.rowAscent(0);
2964 dim.des = tabular.height() - dim.asc;
2965 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2969 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2971 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2972 BufferView * bv = pi.base.bv;
2974 // FIXME: As the full backrgound is painted in drawSelection(),
2975 // we have no choice but to do a full repaint for the Text cells.
2976 pi.full_repaint = true;
2978 resetPos(bv->cursor());
2981 x += ADD_TO_TABULAR_WIDTH;
2983 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2986 first_visible_cell = Tabular::npos;
2987 for (row_type i = 0; i < tabular.rowCount(); ++i) {
2989 int const a = tabular.rowAscent(i);
2990 int const d = tabular.rowDescent(i);
2991 idx = tabular.cellIndex(i, 0);
2992 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2993 if (tabular.isPartOfMultiColumn(i, j))
2995 if (first_visible_cell == Tabular::npos)
2996 first_visible_cell = idx;
2998 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2999 // Cache the Inset position.
3000 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3001 if (nx + tabular.columnWidth(idx) < 0
3002 || nx > bv->workWidth()
3004 || y - a > bv->workHeight()) {
3005 pi.pain.setDrawingEnabled(false);
3006 cell(idx)->draw(pi, cx, y);
3007 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3008 pi.pain.setDrawingEnabled(original_drawing_state);
3010 cell(idx)->draw(pi, cx, y);
3011 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3013 nx += tabular.columnWidth(idx);
3017 if (i + 1 < tabular.rowCount())
3018 y += d + tabular.rowAscent(i + 1) +
3019 tabular.getAdditionalHeight(i + 1);
3024 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3026 Cursor & cur = pi.base.bv->cursor();
3028 x += scx_ + ADD_TO_TABULAR_WIDTH;
3030 // FIXME: it is wrong to completely paint the background
3031 // if we want to do single row painting.
3033 // Paint background of current tabular
3034 int const w = tabular.width();
3035 int const h = tabular.height();
3036 int yy = y - tabular.rowAscent(0);
3037 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3039 if (!cur.selection())
3041 if (&cur.inset() != this)
3047 if (tablemode(cur)) {
3050 getSelection(cur, rs, re, cs, ce);
3051 y -= tabular.rowAscent(0);
3052 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3053 int const a = tabular.rowAscent(j);
3054 int const h = a + tabular.rowDescent(j);
3056 y += tabular.getAdditionalHeight(j);
3057 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3058 if (tabular.isPartOfMultiColumn(j, i))
3060 idx_type const cell =
3061 tabular.cellIndex(j, i);
3062 int const w = tabular.columnWidth(cell);
3063 if (i >= cs && i <= ce && j >= rs && j <= re)
3064 pi.pain.fillRectangle(xx, y, w, h,
3072 x += getCellXPos(cur.idx());
3073 x += tabular.getBeginningOfTextInCell(cur.idx());
3074 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3079 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3080 row_type row, idx_type cell, bool erased) const
3082 int x2 = x + tabular.columnWidth(cell);
3083 bool on_off = false;
3084 ColorCode col = Color_tabularline;
3085 ColorCode onoffcol = Color_tabularonoffline;
3088 col = Color_deletedtext;
3089 onoffcol = Color_deletedtext;
3092 if (!tabular.topAlreadyDrawn(cell)) {
3093 on_off = !tabular.topLine(cell);
3094 pain.line(x, y - tabular.rowAscent(row),
3095 x2, y - tabular.rowAscent(row),
3096 on_off ? onoffcol : col,
3097 on_off ? Painter::line_onoffdash : Painter::line_solid);
3099 on_off = !tabular.bottomLine(cell);
3100 pain.line(x, y + tabular.rowDescent(row),
3101 x2, y + tabular.rowDescent(row),
3102 on_off ? onoffcol : col,
3103 on_off ? Painter::line_onoffdash : Painter::line_solid);
3104 if (!tabular.leftAlreadyDrawn(cell)) {
3105 on_off = !tabular.leftLine(cell);
3106 pain.line(x, y - tabular.rowAscent(row),
3107 x, y + tabular.rowDescent(row),
3108 on_off ? onoffcol : col,
3109 on_off ? Painter::line_onoffdash : Painter::line_solid);
3111 on_off = !tabular.rightLine(cell);
3112 pain.line(x2 - tabular.getAdditionalWidth(cell),
3113 y - tabular.rowAscent(row),
3114 x2 - tabular.getAdditionalWidth(cell),
3115 y + tabular.rowDescent(row),
3116 on_off ? onoffcol : col,
3117 on_off ? Painter::line_onoffdash : Painter::line_solid);
3121 docstring InsetTabular::editMessage() const
3123 return _("Opened table");
3127 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3129 //lyxerr << "InsetTabular::edit: " << this << endl;
3131 cur.selection() = false;
3134 if (isRightToLeft(cur))
3135 cur.idx() = tabular.getLastCellInRow(0);
3141 if (isRightToLeft(cur))
3142 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3144 cur.idx() = tabular.cellCount() - 1;
3146 cur.pos() = cur.lastpos(); // FIXME crude guess
3148 // FIXME: this accesses the position cache before it is initialized
3150 //cur.bv().fitCursor();
3154 void InsetTabular::updateLabels(ParIterator const & it)
3156 // In a longtable, tell captions what the current float is
3157 Counters & cnts = buffer().params().documentClass().counters();
3158 string const saveflt = cnts.current_float();
3159 if (tabular.isLongTabular())
3160 cnts.current_float("table");
3162 ParIterator it2 = it;
3164 size_t const end = it2.nargs();
3165 for ( ; it2.idx() < end; it2.top().forwardIdx())
3166 lyx::updateLabels(buffer(), it2);
3169 if (tabular.isLongTabular())
3170 cnts.current_float(saveflt);
3174 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3176 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3177 << "\n cur:" << cur);
3178 CursorSlice sl = cur.top();
3179 Cursor & bvcur = cur.bv().cursor();
3181 switch (cmd.action) {
3183 case LFUN_MOUSE_PRESS: {
3184 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3187 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3188 || cmd.x > xo(cur.bv()) + tabular.width()) {
3189 row_type r = rowFromY(cur, cmd.y);
3190 cur.idx() = tabular.getFirstCellInRow(r);
3193 cur.idx() = tabular.getLastCellInRow(r);
3194 cur.pos() = cur.lastpos();
3195 cur.selection() = true;
3200 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3201 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3202 || cmd.y > y0 + tabular.height()) {
3203 col_type c = columnFromX(cur, cmd.x);
3204 cur.idx() = tabular.cellIndex(0, c);
3207 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3208 cur.pos() = cur.lastpos();
3209 cur.selection() = true;
3213 // do not reset cursor/selection if we have selected
3214 // some cells (bug 2715).
3215 if (cmd.button() == mouse_button::button3
3216 && &bvcur.selBegin().inset() == this
3217 && tablemode(bvcur))
3220 // Let InsetTableCell do it
3221 cell(cur.idx())->dispatch(cur, cmd);
3224 case LFUN_MOUSE_MOTION:
3225 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3226 if (cmd.button() == mouse_button::button1) {
3227 // only accept motions to places not deeper nested than the real anchor
3228 if (!bvcur.anchor_.hasPart(cur)) {
3232 // select (additional) row
3233 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3234 || cmd.x > xo(cur.bv()) + tabular.width()) {
3235 row_type r = rowFromY(cur, cmd.y);
3236 cur.idx() = tabular.getLastCellInRow(r);
3237 bvcur.setCursor(cur);
3238 bvcur.selection() = true;
3241 // select (additional) column
3242 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3243 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3244 || cmd.y > y0 + tabular.height()) {
3245 col_type c = columnFromX(cur, cmd.x);
3246 cur.idx() = tabular.cellIndex(tabular.rowCount() - 1, c);
3247 bvcur.setCursor(cur);
3248 bvcur.selection() = true;
3251 // only update if selection changes
3252 if (bvcur.idx() == cur.idx() &&
3253 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3255 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3256 bvcur.setCursor(cur);
3257 bvcur.selection() = true;
3261 case LFUN_MOUSE_RELEASE:
3262 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3263 if (cmd.button() == mouse_button::button3)
3264 InsetTabularMailer(*this).showDialog(&cur.bv());
3267 case LFUN_CELL_BACKWARD:
3269 cur.selection() = false;
3272 case LFUN_CELL_FORWARD:
3274 cur.selection() = false;
3276 case LFUN_CHAR_FORWARD_SELECT:
3277 case LFUN_CHAR_FORWARD:
3278 cell(cur.idx())->dispatch(cur, cmd);
3279 if (!cur.result().dispatched()) {
3281 if (sl == cur.top())
3282 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3288 case LFUN_CHAR_BACKWARD_SELECT:
3289 case LFUN_CHAR_BACKWARD:
3290 cell(cur.idx())->dispatch(cur, cmd);
3291 if (!cur.result().dispatched()) {
3293 if (sl == cur.top())
3294 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3300 case LFUN_CHAR_RIGHT_SELECT:
3301 case LFUN_CHAR_RIGHT:
3302 //FIXME: for visual cursor, really move right
3303 if (isRightToLeft(cur))
3304 lyx::dispatch(FuncRequest(
3305 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3306 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3308 lyx::dispatch(FuncRequest(
3309 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3310 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3313 case LFUN_CHAR_LEFT_SELECT:
3314 case LFUN_CHAR_LEFT:
3315 //FIXME: for visual cursor, really move left
3316 if (isRightToLeft(cur))
3317 lyx::dispatch(FuncRequest(
3318 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3319 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3321 lyx::dispatch(FuncRequest(
3322 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3323 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3326 case LFUN_DOWN_SELECT:
3328 cell(cur.idx())->dispatch(cur, cmd);
3329 cur.dispatched(); // override the cell's decision
3330 if (sl == cur.top())
3331 // if our Text didn't do anything to the cursor
3332 // then we try to put the cursor into the cell below
3333 // setting also the right targetX.
3334 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3335 cur.idx() = tabular.getCellBelow(cur.idx());
3337 TextMetrics const & tm =
3338 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3339 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3341 if (sl == cur.top()) {
3342 // we trick it to go to forward after leaving the
3344 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3349 case LFUN_UP_SELECT:
3351 cell(cur.idx())->dispatch(cur, cmd);
3352 cur.dispatched(); // override the cell's decision
3353 if (sl == cur.top())
3354 // if our Text didn't do anything to the cursor
3355 // then we try to put the cursor into the cell above
3356 // setting also the right targetX.
3357 if (tabular.cellRow(cur.idx()) != 0) {
3358 cur.idx() = tabular.getCellAbove(cur.idx());
3359 cur.pit() = cur.lastpit();
3360 Text const * text = cell(cur.idx())->getText(0);
3361 TextMetrics const & tm = cur.bv().textMetrics(text);
3362 ParagraphMetrics const & pm =
3363 tm.parMetrics(cur.lastpit());
3364 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3366 if (sl == cur.top()) {
3367 cmd = FuncRequest(LFUN_UP);
3372 // case LFUN_SCREEN_DOWN: {
3373 // //if (hasSelection())
3374 // // cur.selection() = false;
3375 // col_type const col = tabular.cellColumn(cur.idx());
3376 // int const t = cur.bv().top_y() + cur.bv().height();
3377 // if (t < yo() + tabular.getHeightOfTabular()) {
3378 // cur.bv().scrollDocView(t);
3379 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3381 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3388 // case LFUN_SCREEN_UP: {
3389 // //if (hasSelection())
3390 // // cur.selection() = false;
3391 // col_type const col = tabular.cellColumn(cur.idx());
3392 // int const t = cur.bv().top_y() + cur.bv().height();
3394 // cur.bv().scrollDocView(t);
3398 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3402 // cur.par() = cur.lastpar();
3403 // cur.pos() = cur.lastpos();
3407 case LFUN_LAYOUT_TABULAR:
3408 InsetTabularMailer(*this).showDialog(&cur.bv());
3411 case LFUN_INSET_DIALOG_UPDATE:
3412 InsetTabularMailer(*this).updateDialog(&cur.bv());
3415 case LFUN_TABULAR_FEATURE:
3416 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3420 // insert file functions
3421 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3422 case LFUN_FILE_INSERT_PLAINTEXT: {
3424 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3425 FileName(to_utf8(cmd.argument())));
3428 cur.recordUndoInset(INSERT_UNDO);
3429 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3430 // content has been replaced,
3431 // so cursor might be invalid
3432 cur.pos() = cur.lastpos();
3433 cur.pit() = cur.lastpit();
3434 bvcur.setCursor(cur);
3441 if (tablemode(cur)) {
3442 if (copySelection(cur)) {
3443 cur.recordUndoInset(DELETE_UNDO);
3448 cell(cur.idx())->dispatch(cur, cmd);
3451 case LFUN_CHAR_DELETE_BACKWARD:
3452 case LFUN_CHAR_DELETE_FORWARD:
3453 if (tablemode(cur)) {
3454 cur.recordUndoInset(DELETE_UNDO);
3458 cell(cur.idx())->dispatch(cur, cmd);
3462 if (!cur.selection())
3464 if (tablemode(cur)) {
3468 cell(cur.idx())->dispatch(cur, cmd);
3471 case LFUN_CLIPBOARD_PASTE:
3472 case LFUN_PRIMARY_SELECTION_PASTE: {
3473 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3474 theClipboard().getAsText() :
3475 theSelection().get();
3478 // pass to InsertPlaintextString, but
3479 // only if we have multi-cell content
3480 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3481 cur.recordUndoInset(INSERT_UNDO);
3482 if (insertPlaintextString(cur.bv(), clip, false)) {
3483 // content has been replaced,
3484 // so cursor might be invalid
3485 cur.pos() = cur.lastpos();
3486 cur.pit() = cur.lastpit();
3487 bvcur.setCursor(cur);
3491 // Let the cell handle normal text
3492 cell(cur.idx())->dispatch(cur, cmd);
3497 if (tabularStackDirty() && theClipboard().isInternal() ||
3498 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3499 cur.recordUndoInset(INSERT_UNDO);
3500 pasteClipboard(cur);
3503 cell(cur.idx())->dispatch(cur, cmd);
3506 case LFUN_FONT_EMPH:
3507 case LFUN_FONT_BOLD:
3508 case LFUN_FONT_ROMAN:
3509 case LFUN_FONT_NOUN:
3510 case LFUN_FONT_ITAL:
3511 case LFUN_FONT_FRAK:
3512 case LFUN_FONT_TYPEWRITER:
3513 case LFUN_FONT_SANS:
3514 case LFUN_FONT_FREE_APPLY:
3515 case LFUN_FONT_FREE_UPDATE:
3516 case LFUN_FONT_SIZE:
3517 case LFUN_FONT_UNDERLINE:
3519 case LFUN_WORD_CAPITALIZE:
3520 case LFUN_WORD_UPCASE:
3521 case LFUN_WORD_LOWCASE:
3522 case LFUN_CHARS_TRANSPOSE:
3523 if (tablemode(cur)) {
3526 getSelection(cur, rs, re, cs, ce);
3527 Cursor tmpcur = cur;
3528 for (row_type i = rs; i <= re; ++i) {
3529 for (col_type j = cs; j <= ce; ++j) {
3530 // cursor follows cell:
3531 tmpcur.idx() = tabular.cellIndex(i, j);
3532 // select this cell only:
3535 tmpcur.resetAnchor();
3536 tmpcur.pit() = tmpcur.lastpit();
3537 tmpcur.pos() = tmpcur.top().lastpos();
3538 tmpcur.setCursor(tmpcur);
3539 tmpcur.setSelection();
3540 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3545 cell(cur.idx())->dispatch(cur, cmd);
3549 // we try to handle this event in the insets dispatch function.
3550 cell(cur.idx())->dispatch(cur, cmd);
3556 // function sets an object as defined in func_status.h:
3557 // states OK, Unknown, Disabled, On, Off.
3558 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3559 FuncStatus & status) const
3561 switch (cmd.action) {
3562 case LFUN_TABULAR_FEATURE: {
3563 int action = Tabular::LAST_ACTION;
3565 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3566 string const tmp = tabularFeature[i].feature;
3567 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3568 action = tabularFeature[i].action;
3572 if (action == Tabular::LAST_ACTION) {
3574 status.unknown(true);
3578 string const argument
3579 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3581 row_type sel_row_start = 0;
3582 row_type sel_row_end = 0;
3583 col_type sel_col_start = 0;
3584 col_type sel_col_end = 0;
3585 Tabular::ltType dummyltt;
3588 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3591 case Tabular::SET_PWIDTH:
3592 case Tabular::SET_MPWIDTH:
3593 case Tabular::SET_SPECIAL_COLUMN:
3594 case Tabular::SET_SPECIAL_MULTI:
3595 case Tabular::APPEND_ROW:
3596 case Tabular::APPEND_COLUMN:
3597 case Tabular::DELETE_ROW:
3598 case Tabular::DELETE_COLUMN:
3599 case Tabular::COPY_ROW:
3600 case Tabular::COPY_COLUMN:
3601 case Tabular::SET_ALL_LINES:
3602 case Tabular::UNSET_ALL_LINES:
3603 case Tabular::SET_TOP_SPACE:
3604 case Tabular::SET_BOTTOM_SPACE:
3605 case Tabular::SET_INTERLINE_SPACE:
3606 case Tabular::SET_BORDER_LINES:
3610 case Tabular::MULTICOLUMN:
3611 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3614 case Tabular::TOGGLE_LINE_TOP:
3615 status.setOnOff(tabular.topLine(cur.idx()));
3618 case Tabular::TOGGLE_LINE_BOTTOM:
3619 status.setOnOff(tabular.bottomLine(cur.idx()));
3622 case Tabular::TOGGLE_LINE_LEFT:
3623 status.setOnOff(tabular.leftLine(cur.idx()));
3626 case Tabular::TOGGLE_LINE_RIGHT:
3627 status.setOnOff(tabular.rightLine(cur.idx()));
3630 case Tabular::M_ALIGN_LEFT:
3632 case Tabular::ALIGN_LEFT:
3633 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3636 case Tabular::M_ALIGN_RIGHT:
3638 case Tabular::ALIGN_RIGHT:
3639 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3642 case Tabular::M_ALIGN_CENTER:
3644 case Tabular::ALIGN_CENTER:
3645 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3648 case Tabular::ALIGN_BLOCK:
3649 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3650 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3653 case Tabular::M_VALIGN_TOP:
3655 case Tabular::VALIGN_TOP:
3657 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3660 case Tabular::M_VALIGN_BOTTOM:
3662 case Tabular::VALIGN_BOTTOM:
3664 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3667 case Tabular::M_VALIGN_MIDDLE:
3669 case Tabular::VALIGN_MIDDLE:
3671 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3674 case Tabular::SET_LONGTABULAR:
3675 status.setOnOff(tabular.isLongTabular());
3678 case Tabular::UNSET_LONGTABULAR:
3679 status.setOnOff(!tabular.isLongTabular());
3682 case Tabular::TOGGLE_ROTATE_TABULAR:
3683 case Tabular::SET_ROTATE_TABULAR:
3684 status.setOnOff(tabular.getRotateTabular());
3687 case Tabular::UNSET_ROTATE_TABULAR:
3688 status.setOnOff(!tabular.getRotateTabular());
3691 case Tabular::TOGGLE_ROTATE_CELL:
3692 case Tabular::SET_ROTATE_CELL:
3693 status.setOnOff(!oneCellHasRotationState(false,
3694 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3697 case Tabular::UNSET_ROTATE_CELL:
3698 status.setOnOff(!oneCellHasRotationState(true,
3699 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3702 case Tabular::SET_USEBOX:
3703 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3706 case Tabular::SET_LTFIRSTHEAD:
3707 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3710 case Tabular::UNSET_LTFIRSTHEAD:
3711 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3714 case Tabular::SET_LTHEAD:
3715 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3718 case Tabular::UNSET_LTHEAD:
3719 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3722 case Tabular::SET_LTFOOT:
3723 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3726 case Tabular::UNSET_LTFOOT:
3727 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3730 case Tabular::SET_LTLASTFOOT:
3731 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3734 case Tabular::UNSET_LTLASTFOOT:
3735 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3738 case Tabular::SET_LTNEWPAGE:
3739 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3742 case Tabular::SET_BOOKTABS:
3743 status.setOnOff(tabular.useBookTabs());
3746 case Tabular::UNSET_BOOKTABS:
3747 status.setOnOff(!tabular.useBookTabs());
3752 status.enabled(false);
3758 // These are only enabled inside tabular
3759 case LFUN_CELL_BACKWARD:
3760 case LFUN_CELL_FORWARD:
3761 status.enabled(true);
3764 // disable these with multiple cells selected
3765 case LFUN_INSET_INSERT:
3766 case LFUN_TABULAR_INSERT:
3767 case LFUN_FLEX_INSERT:
3768 case LFUN_FLOAT_INSERT:
3769 case LFUN_FLOAT_WIDE_INSERT:
3770 case LFUN_FOOTNOTE_INSERT:
3771 case LFUN_MARGINALNOTE_INSERT:
3772 case LFUN_MATH_INSERT:
3773 case LFUN_MATH_MODE:
3774 case LFUN_MATH_MUTATE:
3775 case LFUN_MATH_DISPLAY:
3776 case LFUN_NOTE_INSERT:
3777 case LFUN_OPTIONAL_INSERT:
3778 case LFUN_BOX_INSERT:
3779 case LFUN_BRANCH_INSERT:
3780 case LFUN_WRAP_INSERT:
3781 case LFUN_ERT_INSERT: {
3782 if (tablemode(cur)) {
3783 status.enabled(false);
3786 return cell(cur.idx())->getStatus(cur, cmd, status);
3789 // disable in non-fixed-width cells
3791 case LFUN_BREAK_PARAGRAPH:
3792 case LFUN_BREAK_PARAGRAPH_SKIP: {
3793 if (tabular.getPWidth(cur.idx()).zero()) {
3794 status.enabled(false);
3797 return cell(cur.idx())->getStatus(cur, cmd, status);
3801 if (tabularStackDirty() && theClipboard().isInternal()) {
3802 status.enabled(true);
3805 return cell(cur.idx())->getStatus(cur, cmd, status);
3807 case LFUN_INSET_MODIFY:
3808 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3809 status.enabled(true);
3815 // we try to handle this event in the insets dispatch function.
3816 return cell(cur.idx())->getStatus(cur, cmd, status);
3821 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3823 return tabular.latex(os, runparams);
3827 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3829 os << '\n'; // output table on a new line
3830 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3831 tabular.plaintext(os, runparams, dp, false, 0);
3832 return PLAINTEXT_NEWLINE;
3836 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3841 // FIXME: Why not pass a proper DocIterator here?
3843 // if the table is inside a float it doesn't need the informaltable
3844 // wrapper. Search for it.
3845 for (master = owner(); master; master = master->owner())
3846 if (master->lyxCode() == FLOAT_CODE)
3851 os << "<informaltable>";
3854 ret += tabular.docbook(os, runparams);
3856 os << "</informaltable>";
3863 void InsetTabular::validate(LaTeXFeatures & features) const
3865 tabular.validate(features);
3869 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3871 return tabular.getCellInset(idx);
3875 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3877 return tabular.getCellInset(idx);
3881 void InsetTabular::cursorPos(BufferView const & bv,
3882 CursorSlice const & sl, bool boundary, int & x, int & y) const
3884 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3886 // y offset correction
3887 int const row = tabular.cellRow(sl.idx());
3888 for (int i = 0; i <= row; ++i) {
3890 y += tabular.rowAscent(i);
3891 y += tabular.getAdditionalHeight(i);
3894 y += tabular.rowDescent(i);
3897 // x offset correction
3898 int const col = tabular.cellColumn(sl.idx());
3899 int idx = tabular.cellIndex(row, 0);
3900 for (int j = 0; j < col; ++j) {
3901 if (tabular.isPartOfMultiColumn(row, j))
3903 x += tabular.columnWidth(idx);
3906 x += tabular.getBeginningOfTextInCell(idx);
3907 x += ADD_TO_TABULAR_WIDTH;
3912 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3916 Inset const & inset = *tabular.getCellInset(cell);
3917 Point o = bv.coordCache().getInsets().xy(&inset);
3918 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3919 int const xend = xbeg + tabular.columnWidth(cell);
3920 row_type const row = tabular.cellRow(cell);
3921 int const ybeg = o.y_ - tabular.rowAscent(row) -
3922 tabular.getAdditionalHeight(row);
3923 int const yend = o.y_ + tabular.rowDescent(row);
3935 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3936 // << " ybeg=" << ybeg << " yend=" << yend
3937 // << " xx=" << xx << " yy=" << yy
3938 // << " dist=" << xx + yy << endl;
3943 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3945 //lyxerr << "InsetTabular::editXY: " << this << endl;
3946 cur.selection() = false;
3948 cur.idx() = getNearestCell(cur.bv(), x, y);
3950 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3954 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3956 cur.idx() = getNearestCell(cur.bv(), x, y);
3957 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3961 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3963 idx_type idx_min = 0;
3964 int dist_min = numeric_limits<int>::max();
3965 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3966 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3967 int const d = dist(bv, i, x, y);
3978 int InsetTabular::getCellXPos(idx_type const cell) const
3982 for (; !tabular.isFirstCellInRow(c); --c)
3985 for (; c < cell; ++c)
3986 lx += tabular.columnWidth(c);
3992 void InsetTabular::resetPos(Cursor & cur) const
3994 BufferView & bv = cur.bv();
3995 int const maxwidth = bv.workWidth();
3997 if (&cur.inset() != this) {
4001 int const X2 = maxwidth;
4002 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4003 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
4004 int const x2 = x1 + tabular.columnWidth(cur.idx());
4007 scx_ = X1 + 20 - x1;
4009 scx_ = X2 - 20 - x2;
4014 cur.updateFlags(Update::Force | Update::FitCursor);
4018 void InsetTabular::moveNextCell(Cursor & cur)
4020 if (isRightToLeft(cur)) {
4021 if (tabular.isFirstCellInRow(cur.idx())) {
4022 row_type const row = tabular.cellRow(cur.idx());
4023 if (row == tabular.rowCount() - 1)
4025 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4032 if (tabular.isLastCell(cur.idx()))
4042 void InsetTabular::movePrevCell(Cursor & cur)
4044 if (isRightToLeft(cur)) {
4045 if (tabular.isLastCellInRow(cur.idx())) {
4046 row_type const row = tabular.cellRow(cur.idx());
4049 cur.idx() = tabular.getFirstCellInRow(row);
4050 cur.idx() = tabular.getCellAbove(cur.idx());
4052 if (tabular.isLastCell(cur.idx()))
4057 if (cur.idx() == 0) // first cell
4061 cur.pit() = cur.lastpit();
4062 cur.pos() = cur.lastpos();
4064 // FIXME: this accesses the position cache before it is initialized
4069 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4071 Tabular::Feature action = Tabular::LAST_ACTION;
4074 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4075 string const tmp = tabularFeature[i].feature;
4077 if (tmp == what.substr(0, tmp.length())) {
4078 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4079 //tabularFeatures[i].feature.length()))
4080 action = tabularFeature[i].action;
4084 if (action == Tabular::LAST_ACTION)
4088 ltrim(what.substr(tabularFeature[i].feature.length()));
4089 tabularFeatures(cur, action, val);
4094 static void checkLongtableSpecial(Tabular::ltType & ltt,
4095 string const & special, bool & flag)
4097 if (special == "dl_above") {
4100 } else if (special == "dl_below") {
4101 ltt.bottomDL = flag;
4103 } else if (special == "empty") {
4112 bool InsetTabular::oneCellHasRotationState(bool rotated,
4113 row_type row_start, row_type row_end,
4114 col_type col_start, col_type col_end) const {
4116 for (row_type i = row_start; i <= row_end; ++i) {
4117 for (col_type j = col_start; j <= col_end; ++j) {
4118 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4127 void InsetTabular::tabularFeatures(Cursor & cur,
4128 Tabular::Feature feature, string const & value)
4130 col_type sel_col_start;
4131 col_type sel_col_end;
4132 row_type sel_row_start;
4133 row_type sel_row_end;
4134 bool setLines = false;
4135 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4136 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4140 case Tabular::M_ALIGN_LEFT:
4141 case Tabular::ALIGN_LEFT:
4142 setAlign = LYX_ALIGN_LEFT;
4145 case Tabular::M_ALIGN_RIGHT:
4146 case Tabular::ALIGN_RIGHT:
4147 setAlign = LYX_ALIGN_RIGHT;
4150 case Tabular::M_ALIGN_CENTER:
4151 case Tabular::ALIGN_CENTER:
4152 setAlign = LYX_ALIGN_CENTER;
4155 case Tabular::ALIGN_BLOCK:
4156 setAlign = LYX_ALIGN_BLOCK;
4159 case Tabular::M_VALIGN_TOP:
4160 case Tabular::VALIGN_TOP:
4161 setVAlign = Tabular::LYX_VALIGN_TOP;
4164 case Tabular::M_VALIGN_BOTTOM:
4165 case Tabular::VALIGN_BOTTOM:
4166 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4169 case Tabular::M_VALIGN_MIDDLE:
4170 case Tabular::VALIGN_MIDDLE:
4171 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4178 cur.recordUndoInset(ATOMIC_UNDO);
4180 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4181 row_type const row = tabular.cellRow(cur.idx());
4182 col_type const column = tabular.cellColumn(cur.idx());
4184 Tabular::ltType ltt;
4188 case Tabular::SET_PWIDTH: {
4189 Length const len(value);
4190 tabular.setColumnPWidth(cur, cur.idx(), len);
4192 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4193 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4197 case Tabular::SET_MPWIDTH:
4198 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4201 case Tabular::SET_SPECIAL_COLUMN:
4202 case Tabular::SET_SPECIAL_MULTI:
4203 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4206 case Tabular::APPEND_ROW:
4207 // append the row into the tabular
4208 tabular.appendRow(cur.idx());
4211 case Tabular::APPEND_COLUMN:
4212 // append the column into the tabular
4213 tabular.appendColumn(cur.idx());
4214 cur.idx() = tabular.cellIndex(row, column);
4217 case Tabular::DELETE_ROW:
4218 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4219 tabular.deleteRow(sel_row_start);
4220 if (sel_row_start >= tabular.rowCount())
4222 cur.idx() = tabular.cellIndex(sel_row_start, column);
4225 cur.selection() = false;
4228 case Tabular::DELETE_COLUMN:
4229 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4230 tabular.deleteColumn(sel_col_start);
4231 if (sel_col_start >= tabular.columnCount())
4233 cur.idx() = tabular.cellIndex(row, sel_col_start);
4236 cur.selection() = false;
4239 case Tabular::COPY_ROW:
4240 tabular.copyRow(row);
4243 case Tabular::COPY_COLUMN:
4244 tabular.copyColumn(column);
4245 cur.idx() = tabular.cellIndex(row, column);
4248 case Tabular::TOGGLE_LINE_TOP: {
4249 bool lineSet = !tabular.topLine(cur.idx());
4250 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4251 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4252 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4256 case Tabular::TOGGLE_LINE_BOTTOM: {
4257 bool lineSet = !tabular.bottomLine(cur.idx());
4258 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4259 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4260 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4264 case Tabular::TOGGLE_LINE_LEFT: {
4265 bool lineSet = !tabular.leftLine(cur.idx());
4266 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4267 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4268 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4272 case Tabular::TOGGLE_LINE_RIGHT: {
4273 bool lineSet = !tabular.rightLine(cur.idx());
4274 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4275 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4276 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4280 case Tabular::M_ALIGN_LEFT:
4281 case Tabular::M_ALIGN_RIGHT:
4282 case Tabular::M_ALIGN_CENTER:
4284 case Tabular::ALIGN_LEFT:
4285 case Tabular::ALIGN_RIGHT:
4286 case Tabular::ALIGN_CENTER:
4287 case Tabular::ALIGN_BLOCK:
4288 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4289 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4290 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4293 case Tabular::M_VALIGN_TOP:
4294 case Tabular::M_VALIGN_BOTTOM:
4295 case Tabular::M_VALIGN_MIDDLE:
4297 case Tabular::VALIGN_TOP:
4298 case Tabular::VALIGN_BOTTOM:
4299 case Tabular::VALIGN_MIDDLE:
4300 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4301 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4302 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4305 case Tabular::MULTICOLUMN: {
4306 if (sel_row_start != sel_row_end) {
4307 // FIXME: Need I say it ? This is horrible.
4309 Alert::error(_("Error setting multicolumn"),
4310 _("You cannot set multicolumn vertically."));
4313 if (!cur.selection()) {
4314 // just multicol for one single cell
4315 // check whether we are completely in a multicol
4316 if (tabular.isMultiColumn(cur.idx()))
4317 tabular.unsetMultiColumn(cur.idx());
4319 tabular.setMultiColumn(cur.idx(), 1);
4322 // we have a selection so this means we just add all this
4323 // cells to form a multicolumn cell
4324 idx_type const s_start = cur.selBegin().idx();
4325 idx_type const s_end = cur.selEnd().idx();
4326 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4327 cur.idx() = s_start;
4330 cur.selection() = false;
4334 case Tabular::SET_ALL_LINES:
4336 case Tabular::UNSET_ALL_LINES:
4337 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4338 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4339 tabular.setAllLines(
4340 tabular.cellIndex(i,j), setLines);
4343 case Tabular::SET_BORDER_LINES:
4344 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4345 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4346 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4348 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4349 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4350 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4354 case Tabular::SET_LONGTABULAR:
4355 tabular.setLongTabular(true);
4358 case Tabular::UNSET_LONGTABULAR:
4359 tabular.setLongTabular(false);
4362 case Tabular::SET_ROTATE_TABULAR:
4363 tabular.setRotateTabular(true);
4366 case Tabular::UNSET_ROTATE_TABULAR:
4367 tabular.setRotateTabular(false);
4370 case Tabular::TOGGLE_ROTATE_TABULAR:
4371 tabular.setRotateTabular(!tabular.getRotateTabular());
4374 case Tabular::SET_ROTATE_CELL:
4375 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4376 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4377 tabular.setRotateCell(
4378 tabular.cellIndex(i, j), true);
4381 case Tabular::UNSET_ROTATE_CELL:
4382 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4383 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4384 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4387 case Tabular::TOGGLE_ROTATE_CELL:
4389 bool oneNotRotated = oneCellHasRotationState(false,
4390 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4392 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4393 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4394 tabular.setRotateCell(tabular.cellIndex(i, j),
4399 case Tabular::SET_USEBOX: {
4400 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4401 if (val == tabular.getUsebox(cur.idx()))
4402 val = Tabular::BOX_NONE;
4403 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4404 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4405 tabular.setUsebox(tabular.cellIndex(i, j), val);
4409 case Tabular::UNSET_LTFIRSTHEAD:
4411 case Tabular::SET_LTFIRSTHEAD:
4412 tabular.getRowOfLTFirstHead(row, ltt);
4413 checkLongtableSpecial(ltt, value, flag);
4414 tabular.setLTHead(row, flag, ltt, true);
4417 case Tabular::UNSET_LTHEAD:
4419 case Tabular::SET_LTHEAD:
4420 tabular.getRowOfLTHead(row, ltt);
4421 checkLongtableSpecial(ltt, value, flag);
4422 tabular.setLTHead(row, flag, ltt, false);
4425 case Tabular::UNSET_LTFOOT:
4427 case Tabular::SET_LTFOOT:
4428 tabular.getRowOfLTFoot(row, ltt);
4429 checkLongtableSpecial(ltt, value, flag);
4430 tabular.setLTFoot(row, flag, ltt, false);
4433 case Tabular::UNSET_LTLASTFOOT:
4435 case Tabular::SET_LTLASTFOOT:
4436 tabular.getRowOfLTLastFoot(row, ltt);
4437 checkLongtableSpecial(ltt, value, flag);
4438 tabular.setLTFoot(row, flag, ltt, true);
4441 case Tabular::SET_LTNEWPAGE:
4442 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4445 case Tabular::SET_BOOKTABS:
4446 tabular.setBookTabs(true);
4449 case Tabular::UNSET_BOOKTABS:
4450 tabular.setBookTabs(false);
4453 case Tabular::SET_TOP_SPACE: {
4455 if (value == "default")
4456 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4457 tabular.row_info[i].top_space_default = true;
4458 else if (isValidLength(value, &len))
4459 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4460 tabular.row_info[i].top_space_default = false;
4461 tabular.row_info[i].top_space = len;
4464 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4465 tabular.row_info[i].top_space_default = false;
4466 tabular.row_info[i].top_space = len;
4471 case Tabular::SET_BOTTOM_SPACE: {
4473 if (value == "default")
4474 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4475 tabular.row_info[i].bottom_space_default = true;
4476 else if (isValidLength(value, &len))
4477 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4478 tabular.row_info[i].bottom_space_default = false;
4479 tabular.row_info[i].bottom_space = len;
4482 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4483 tabular.row_info[i].bottom_space_default = false;
4484 tabular.row_info[i].bottom_space = len;
4489 case Tabular::SET_INTERLINE_SPACE: {
4491 if (value == "default")
4492 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4493 tabular.row_info[i].interline_space_default = true;
4494 else if (isValidLength(value, &len))
4495 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4496 tabular.row_info[i].interline_space_default = false;
4497 tabular.row_info[i].interline_space = len;
4500 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4501 tabular.row_info[i].interline_space_default = false;
4502 tabular.row_info[i].interline_space = len;
4507 // dummy stuff just to avoid warnings
4508 case Tabular::LAST_ACTION:
4514 bool InsetTabular::showInsetDialog(BufferView * bv) const
4516 InsetTabularMailer(*this).showDialog(bv);
4521 void InsetTabular::openLayoutDialog(BufferView * bv) const
4523 InsetTabularMailer(*this).showDialog(bv);
4527 bool InsetTabular::copySelection(Cursor & cur)
4529 if (!cur.selection())
4534 getSelection(cur, rs, re, cs, ce);
4536 paste_tabular.reset(new Tabular(tabular));
4538 for (row_type i = 0; i < rs; ++i)
4539 paste_tabular->deleteRow(0);
4541 row_type const rows = re - rs + 1;
4542 while (paste_tabular->rowCount() > rows)
4543 paste_tabular->deleteRow(rows);
4545 for (col_type i = 0; i < cs; ++i)
4546 paste_tabular->deleteColumn(0);
4548 col_type const columns = ce - cs + 1;
4549 while (paste_tabular->columnCount() > columns)
4550 paste_tabular->deleteColumn(columns);
4552 // We clear all the InsetTableCell pointers, since they
4553 // might now become invalid and there is no point in having
4554 // them point to temporary things in paste_tabular.
4555 for (row_type i = 0; i < paste_tabular->rowCount(); ++i)
4556 for (col_type j = 0; j < paste_tabular->columnCount(); ++j) {
4557 paste_tabular->getCellInset(i,j)->setCellData(0);
4558 paste_tabular->getCellInset(i,j)->setTabular(0);
4561 odocstringstream os;
4562 OutputParams const runparams(0);
4563 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4564 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4565 cap::copySelection(cur, os.str());
4567 // mark tabular stack dirty
4568 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4569 // when we (hopefully) have a one-for-all paste mechanism.
4570 // This must be called after cap::copySelection.
4571 dirtyTabularStack(true);
4577 bool InsetTabular::pasteClipboard(Cursor & cur)
4581 col_type const actcol = tabular.cellColumn(cur.idx());
4582 row_type const actrow = tabular.cellRow(cur.idx());
4583 for (row_type r1 = 0, r2 = actrow;
4584 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4586 for (col_type c1 = 0, c2 = actcol;
4587 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4589 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4590 tabular.isPartOfMultiColumn(r2, c2))
4592 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4596 if (tabular.isPartOfMultiColumn(r2, c2)) {
4600 shared_ptr<InsetTableCell> inset(
4601 new InsetTableCell(*paste_tabular->getCellInset(r1, c1)));
4602 // note that setCellInset will call InsetTableCell::setCellData()
4603 // and InsetTableCell::setTabular()
4604 tabular.setCellInset(r2, c2, inset);
4605 // FIXME: change tracking (MG)
4606 inset->setChange(Change(cur.buffer().params().trackChanges ?
4607 Change::INSERTED : Change::UNCHANGED));
4615 void InsetTabular::cutSelection(Cursor & cur)
4617 if (!cur.selection())
4622 getSelection(cur, rs, re, cs, ce);
4623 for (row_type i = rs; i <= re; ++i) {
4624 for (col_type j = cs; j <= ce; ++j) {
4625 shared_ptr<InsetTableCell> t
4626 = cell(tabular.cellIndex(i, j));
4627 if (cur.buffer().params().trackChanges)
4628 // FIXME: Change tracking (MG)
4629 t->setChange(Change(Change::DELETED));
4635 // cursor position might be invalid now
4636 if (cur.pit() > cur.lastpit())
4637 cur.pit() = cur.lastpit();
4638 if (cur.pos() > cur.lastpos())
4639 cur.pos() = cur.lastpos();
4640 cur.clearSelection();
4644 bool InsetTabular::isRightToLeft(Cursor & cur) const
4646 BOOST_ASSERT(cur.depth() > 1);
4647 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4648 pos_type const parentpos = cur[cur.depth() - 2].pos();
4649 return parentpar.getFontSettings(cur.bv().buffer().params(),
4650 parentpos).language()->rightToLeft();
4654 void InsetTabular::getSelection(Cursor & cur,
4655 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4657 CursorSlice const & beg = cur.selBegin();
4658 CursorSlice const & end = cur.selEnd();
4659 cs = tabular.cellColumn(beg.idx());
4660 ce = tabular.cellColumn(end.idx());
4663 cs = tabular.cellColumn(end.idx());
4665 ce = tabular.cellRightColumn(end.idx());
4668 rs = tabular.cellRow(beg.idx());
4669 re = tabular.cellRow(end.idx());
4675 Text * InsetTabular::getText(int idx) const
4677 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4681 void InsetTabular::setChange(Change const & change)
4683 for (idx_type idx = 0; idx < nargs(); ++idx)
4684 cell(idx)->setChange(change);
4688 void InsetTabular::acceptChanges(BufferParams const & bparams)
4690 for (idx_type idx = 0; idx < nargs(); ++idx)
4691 cell(idx)->acceptChanges(bparams);
4695 void InsetTabular::rejectChanges(BufferParams const & bparams)
4697 for (idx_type idx = 0; idx < nargs(); ++idx)
4698 cell(idx)->rejectChanges(bparams);
4702 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4704 return tabular.getPWidth(cell).zero();
4708 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4710 return !tabular.getPWidth(cell).zero();
4714 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4717 if (buf.length() <= 0)
4722 col_type maxCols = 1;
4723 size_t const len = buf.length();
4727 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4735 maxCols = max(cols, maxCols);
4741 maxCols = max(cols, maxCols);
4747 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4748 loctab = paste_tabular.get();
4750 dirtyTabularStack(true);
4753 cell = bv.cursor().idx();
4754 ocol = tabular.cellColumn(cell);
4755 row = tabular.cellRow(cell);
4759 idx_type const cells = loctab->cellCount();
4762 rows = loctab->rowCount();
4763 col_type const columns = loctab->columnCount();
4765 while (cell < cells && p < len && row < rows &&
4766 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4772 // we can only set this if we are not too far right
4773 if (cols < columns) {
4774 shared_ptr<InsetTableCell> inset = loctab->getCellInset(cell);
4775 Font const font = bv.textMetrics(&inset->text_).
4777 inset->setText(buf.substr(op, p - op), font,
4778 buffer().params().trackChanges);
4784 // we can only set this if we are not too far right
4785 if (cols < columns) {
4786 shared_ptr<InsetTableCell> inset = tabular.getCellInset(cell);
4787 Font const font = bv.textMetrics(&inset->text_).
4789 inset->setText(buf.substr(op, p - op), font,
4790 buffer().params().trackChanges);
4795 cell = loctab->cellIndex(row, cols);
4801 // check for the last cell if there is no trailing '\n'
4802 if (cell < cells && op < len) {
4803 shared_ptr<InsetTableCell> inset = loctab->getCellInset(cell);
4804 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4805 inset->setText(buf.substr(op, len - op), font,
4806 buffer().params().trackChanges);
4812 void InsetTabular::addPreview(PreviewLoader & loader) const
4814 row_type const rows = tabular.rowCount();
4815 col_type const columns = tabular.columnCount();
4816 for (row_type i = 0; i < rows; ++i) {
4817 for (col_type j = 0; j < columns; ++j)
4818 tabular.getCellInset(i, j)->addPreview(loader);
4823 bool InsetTabular::tablemode(Cursor & cur) const
4825 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4829 bool InsetTabular::completionSupported(Cursor const & cur) const
4831 Cursor const & bvCur = cur.bv().cursor();
4832 if (&bvCur.inset() != this)
4834 return cur.text()->completionSupported(cur);
4838 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4840 return completionSupported(cur);
4844 bool InsetTabular::automaticInlineCompletion() const
4846 return lyxrc.completion_inline_text;
4850 bool InsetTabular::automaticPopupCompletion() const
4852 return lyxrc.completion_popup_text;
4856 bool InsetTabular::showCompletionCursor() const
4858 return lyxrc.completion_cursor_text;
4862 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4864 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4868 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4870 if (!completionSupported(cur))
4872 return cur.text()->completionPrefix(cur);
4876 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4878 if (!completionSupported(cur))
4881 return cur.text()->insertCompletion(cur, s, finished);
4885 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4886 Dimension & dim) const
4888 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4889 tm.completionPosAndDim(cur, x, y, dim);
4893 string const InsetTabularMailer::name_("tabular");
4895 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4896 : inset_(const_cast<InsetTabular &>(inset))
4900 string const InsetTabularMailer::inset2string(Buffer const &) const
4902 return params2string(inset_);
4906 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4908 istringstream data(in);
4910 lex.setStream(data);
4917 if (!lex || token != name_)
4918 return print_mailer_error("InsetTabularMailer", in, 1,
4921 // This is part of the inset proper that is usually swallowed
4922 // by Buffer::readInset
4924 if (!lex || token != "Tabular")
4925 return print_mailer_error("InsetTabularMailer", in, 2, "Tabular");
4931 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4934 data << name_ << ' ';
4936 data << "\\end_inset\n";