3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
43 #include "frontends/nullpainter.h"
49 using lyx::cap::tabularStackDirty;
51 using lyx::graphics::PreviewLoader;
53 using lyx::support::ltrim;
55 using boost::shared_ptr;
61 using std::istringstream;
63 using std::ostringstream;
70 int const ADD_TO_HEIGHT = 2;
71 int const ADD_TO_TABULAR_WIDTH = 2;
74 boost::scoped_ptr<LyXTabular> paste_tabular;
77 struct TabularFeature {
78 LyXTabular::Feature action;
83 TabularFeature tabularFeature[] =
85 { LyXTabular::APPEND_ROW, "append-row" },
86 { LyXTabular::APPEND_COLUMN, "append-column" },
87 { LyXTabular::DELETE_ROW, "delete-row" },
88 { LyXTabular::DELETE_COLUMN, "delete-column" },
89 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
90 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
91 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
92 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
93 { LyXTabular::ALIGN_LEFT, "align-left" },
94 { LyXTabular::ALIGN_RIGHT, "align-right" },
95 { LyXTabular::ALIGN_CENTER, "align-center" },
96 { LyXTabular::ALIGN_BLOCK, "align-block" },
97 { LyXTabular::VALIGN_TOP, "valign-top" },
98 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
99 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
100 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
101 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
102 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
103 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
104 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
105 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
106 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
107 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
108 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
109 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
110 { LyXTabular::MULTICOLUMN, "multicolumn" },
111 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
112 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
113 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
114 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
115 { LyXTabular::SET_PWIDTH, "set-pwidth" },
116 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
117 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
118 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
119 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
120 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
121 { LyXTabular::SET_USEBOX, "set-usebox" },
122 { LyXTabular::SET_LTHEAD, "set-lthead" },
123 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
124 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
125 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
126 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
127 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
128 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
129 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
130 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
131 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
132 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
133 { LyXTabular::LAST_ACTION, "" }
137 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
139 FeatureEqual(LyXTabular::Feature feature)
140 : feature_(feature) {}
141 bool operator()(TabularFeature const & tf) const {
142 return tf.action == feature_;
145 LyXTabular::Feature feature_;
151 string const featureAsString(LyXTabular::Feature feature)
153 TabularFeature * end = tabularFeature +
154 sizeof(tabularFeature) / sizeof(TabularFeature);
155 TabularFeature * it = std::find_if(tabularFeature, end,
156 FeatureEqual(feature));
157 return (it == end) ? string() : it->feature;
161 bool InsetTabular::hasPasteBuffer() const
163 return (paste_tabular.get() != 0);
167 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
169 : tabular(buf.params(), max(rows, row_type(1)),
170 max(columns, col_type(1))), buffer_(&buf), scx_(0)
174 InsetTabular::InsetTabular(InsetTabular const & tab)
175 : InsetOld(tab), tabular(tab.tabular),
176 buffer_(tab.buffer_), scx_(0)
180 InsetTabular::~InsetTabular()
182 InsetTabularMailer(*this).hideDialog();
186 auto_ptr<InsetBase> InsetTabular::doClone() const
188 return auto_ptr<InsetBase>(new InsetTabular(*this));
192 Buffer const & InsetTabular::buffer() const
198 void InsetTabular::buffer(Buffer const * b)
204 void InsetTabular::write(Buffer const & buf, ostream & os) const
206 os << "Tabular" << endl;
207 tabular.write(buf, os);
211 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
213 bool const old_format = (lex.getString() == "\\LyXTable");
215 tabular.read(buf, lex);
221 string token = lex.getString();
222 while (lex.isOK() && (token != "\\end_inset")) {
224 token = lex.getString();
226 if (token != "\\end_inset") {
227 lex.printError("Missing \\end_inset at this point. "
233 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
235 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
236 // mi.base.textwidth << "\n";
238 lyxerr << "InsetTabular::metrics: need bv" << endl;
243 for (idx_type cell = 0; i < tabular.rows(); ++i) {
246 for (col_type j = 0; j < tabular.columns(); ++j) {
247 if (tabular.isPartOfMultiColumn(i, j))
248 // Multicolumn cell, but not first one
253 if (tabular.cell_info[i][j].multicolumn ==
254 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
255 p_width = tabular.cellinfo_of_cell(cell).p_width;
257 p_width = tabular.column_info[j].p_width;
259 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
260 tabular.getCellInset(cell)->metrics(m, dim);
262 dim.wid = m.base.textwidth;
263 maxAsc = max(maxAsc, dim.asc);
264 maxDesc = max(maxDesc, dim.des);
265 tabular.setWidthOfCell(cell, dim.wid);
268 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
269 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
272 dim.asc = tabular.getAscentOfRow(0);
273 dim.des = tabular.getHeightOfTabular() - dim.asc;
274 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
279 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
281 setPosCache(pi, x, y);
283 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
284 BufferView * bv = pi.base.bv;
286 static NullPainter nop;
287 static PainterInfo nullpi(bv, nop);
289 //resetPos(bv->cursor());
292 x += ADD_TO_TABULAR_WIDTH;
295 first_visible_cell = LyXTabular::npos;
296 for (row_type i = 0; i < tabular.rows(); ++i) {
298 int const a = tabular.getAscentOfRow(i);
299 int const d = tabular.getDescentOfRow(i);
300 idx = tabular.getCellNumber(i, 0);
301 for (col_type j = 0; j < tabular.columns(); ++j) {
302 if (tabular.isPartOfMultiColumn(i, j))
304 if (first_visible_cell == LyXTabular::npos)
305 first_visible_cell = idx;
307 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
308 if (nx + tabular.getWidthOfColumn(idx) < 0
309 || nx > bv->workWidth()
311 || y - a > bv->workHeight()) {
312 cell(idx)->draw(nullpi, cx, y);
313 drawCellLines(nop, nx, y, i, idx, pi.erased_);
315 cell(idx)->draw(pi, cx, y);
316 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
318 nx += tabular.getWidthOfColumn(idx);
322 if (i + 1 < tabular.rows())
323 y += d + tabular.getAscentOfRow(i + 1) +
324 tabular.getAdditionalHeight(i + 1);
329 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
331 setPosCache(pi, x, y);
333 LCursor & cur = pi.base.bv->cursor();
334 if (!cur.selection())
336 if (!ptr_cmp(&cur.inset(), this))
341 x += scx_ + ADD_TO_TABULAR_WIDTH;
343 if (tablemode(cur)) {
346 getSelection(cur, rs, re, cs, ce);
347 y -= tabular.getAscentOfRow(0);
348 for (row_type j = 0; j < tabular.rows(); ++j) {
349 int const a = tabular.getAscentOfRow(j);
350 int const h = a + tabular.getDescentOfRow(j);
352 y += tabular.getAdditionalHeight(j);
353 for (col_type i = 0; i < tabular.columns(); ++i) {
354 if (tabular.isPartOfMultiColumn(j, i))
356 idx_type const cell =
357 tabular.getCellNumber(j, i);
358 int const w = tabular.getWidthOfColumn(cell);
359 if (i >= cs && i <= ce && j >= rs && j <= re)
360 pi.pain.fillRectangle(xx, y, w, h,
369 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
374 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
375 row_type row, idx_type cell, bool erased) const
377 int x2 = x + tabular.getWidthOfColumn(cell);
379 LColor::color col = LColor::tabularline;
380 LColor::color onoffcol = LColor::tabularonoffline;
383 col = LColor::strikeout;
384 onoffcol = LColor::strikeout;
387 if (!tabular.topAlreadyDrawn(cell)) {
388 on_off = !tabular.topLine(cell);
389 pain.line(x, y - tabular.getAscentOfRow(row),
390 x2, y - tabular.getAscentOfRow(row),
391 on_off ? onoffcol : col,
392 on_off ? Painter::line_onoffdash : Painter::line_solid);
394 on_off = !tabular.bottomLine(cell);
395 pain.line(x, y + tabular.getDescentOfRow(row),
396 x2, y + tabular.getDescentOfRow(row),
397 on_off ? onoffcol : col,
398 on_off ? Painter::line_onoffdash : Painter::line_solid);
399 if (!tabular.leftAlreadyDrawn(cell)) {
400 on_off = !tabular.leftLine(cell);
401 pain.line(x, y - tabular.getAscentOfRow(row),
402 x, y + tabular.getDescentOfRow(row),
403 on_off ? onoffcol : col,
404 on_off ? Painter::line_onoffdash : Painter::line_solid);
406 on_off = !tabular.rightLine(cell);
407 pain.line(x2 - tabular.getAdditionalWidth(cell),
408 y - tabular.getAscentOfRow(row),
409 x2 - tabular.getAdditionalWidth(cell),
410 y + tabular.getDescentOfRow(row),
411 on_off ? onoffcol : col,
412 on_off ? Painter::line_onoffdash : Painter::line_solid);
416 string const InsetTabular::editMessage() const
418 return _("Opened table");
422 void InsetTabular::edit(LCursor & cur, bool left)
424 lyxerr << "InsetTabular::edit: " << this << endl;
426 cur.selection() = false;
429 if (isRightToLeft(cur))
430 cur.idx() = tabular.getLastCellInRow(0);
436 if (isRightToLeft(cur))
437 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
439 cur.idx() = tabular.getNumberOfCells() - 1;
441 cur.pos() = cur.lastpos(); // FIXME crude guess
443 // this accesses the position cache before it is initialized
445 //cur.bv().fitCursor();
449 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
451 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
452 << "\n cur:" << cur << endl;
453 CursorSlice sl = cur.top();
454 LCursor & bvcur = cur.bv().cursor();
456 switch (cmd.action) {
458 case LFUN_MOUSE_PRESS:
459 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
461 if (cmd.button() == mouse_button::button1) {
462 cur.selection() = false;
463 setCursorFromCoordinates(cur, cmd.x, cmd.y);
469 if (cmd.button() == mouse_button::button2) {
470 // FIXME: pasting multiple cells (with insettabular's own
471 // LFUN_PASTESELECTION still does not work! (jspitzm)
472 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
473 cell(cur.idx())->dispatch(cur, cmd);
477 // we'll pop up the table dialog on release
478 if (cmd.button() == mouse_button::button3)
482 case LFUN_MOUSE_MOTION:
483 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
484 if (cmd.button() == mouse_button::button1) {
485 // only accept motions to places not deeper nested than the real anchor
486 if (bvcur.anchor_.hasPart(cur)) {
487 setCursorFromCoordinates(cur, cmd.x, cmd.y);
488 bvcur.setCursor(cur);
489 bvcur.selection() = true;
495 case LFUN_MOUSE_RELEASE:
496 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
497 if (cmd.button() == mouse_button::button3)
498 InsetTabularMailer(*this).showDialog(&cur.bv());
501 case LFUN_CELL_BACKWARD:
503 cur.selection() = false;
506 case LFUN_CELL_FORWARD:
508 cur.selection() = false;
513 cell(cur.idx())->dispatch(cur, cmd);
514 if (!cur.result().dispatched()) {
515 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
517 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
525 cell(cur.idx())->dispatch(cur, cmd);
526 if (!cur.result().dispatched()) {
527 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
529 cmd = FuncRequest(LFUN_FINISHED_LEFT);
537 cell(cur.idx())->dispatch(cur, cmd);
538 cur.dispatched(); // override the cell's decision
540 // if our LyXText didn't do anything to the cursor
541 // then we try to put the cursor into the cell below
542 // setting also the right targetX.
543 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
544 cur.idx() = tabular.getCellBelow(cur.idx());
546 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
547 cur.pit(), 0, cur.targetX());
549 if (sl == cur.top()) {
550 // we trick it to go to the RIGHT after leaving the
552 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
559 cell(cur.idx())->dispatch(cur, cmd);
560 cur.dispatched(); // override the cell's decision
562 // if our LyXText didn't do anything to the cursor
563 // then we try to put the cursor into the cell above
564 // setting also the right targetX.
565 if (tabular.row_of_cell(cur.idx()) != 0) {
566 cur.idx() = tabular.getCellAbove(cur.idx());
567 cur.pit() = cur.lastpit();
568 LyXText const * text = cell(cur.idx())->getText(0);
569 cur.pos() = text->x2pos(
571 text->paragraphs().back().rows().size()-1,
574 if (sl == cur.top()) {
575 cmd = FuncRequest(LFUN_FINISHED_UP);
581 // //if (hasSelection())
582 // // cur.selection() = false;
583 // col_type const col = tabular.column_of_cell(cur.idx());
584 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
585 // if (t < yo() + tabular.getHeightOfTabular()) {
586 // cur.bv().scrollDocView(t);
587 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
589 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
596 // case LFUN_PRIOR: {
597 // //if (hasSelection())
598 // // cur.selection() = false;
599 // col_type const col = tabular.column_of_cell(cur.idx());
600 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
602 // cur.bv().scrollDocView(t);
606 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
610 // cur.par() = cur.lastpar();
611 // cur.pos() = cur.lastpos();
615 case LFUN_LAYOUT_TABULAR:
616 InsetTabularMailer(*this).showDialog(&cur.bv());
619 case LFUN_INSET_DIALOG_UPDATE:
620 InsetTabularMailer(*this).updateDialog(&cur.bv());
623 case LFUN_TABULAR_FEATURE:
624 if (!tabularFeatures(cur, cmd.argument))
628 // insert file functions
629 case LFUN_FILE_INSERT_ASCII_PARA:
630 case LFUN_FILE_INSERT_ASCII: {
631 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
632 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
638 if (tablemode(cur)) {
639 if (copySelection(cur)) {
640 recordUndoInset(cur, Undo::DELETE);
645 cell(cur.idx())->dispatch(cur, cmd);
650 if (tablemode(cur)) {
651 recordUndoInset(cur, Undo::DELETE);
655 cell(cur.idx())->dispatch(cur, cmd);
659 if (!cur.selection())
661 if (tablemode(cur)) {
665 cell(cur.idx())->dispatch(cur, cmd);
668 case LFUN_PASTESELECTION: {
669 string const clip = cur.bv().getClipboard();
672 if (clip.find('\t') != string::npos) {
675 col_type maxCols = 1;
676 size_t len = clip.length();
677 for (size_t p = 0; p < len; ++p) {
678 p = clip.find_first_of("\t\n", p);
679 if (p == string::npos)
688 maxCols = max(cols, maxCols);
693 maxCols = max(cols, maxCols);
696 new LyXTabular(cur.buffer().params(), rows, maxCols));
698 string::size_type op = 0;
700 idx_type const cells =
701 paste_tabular->getNumberOfCells();
704 for (size_t p = 0; cell < cells && p < len; ++p) {
705 p = clip.find_first_of("\t\n", p);
706 if (p == string::npos || p >= len)
710 paste_tabular->getCellInset(cell)->
711 setText(clip.substr(op, p - op), font);
716 paste_tabular->getCellInset(cell)->
717 setText(clip.substr(op, p - op), font);
718 while (cols++ < maxCols)
725 // check for the last cell if there is no trailing '\n'
726 if (cell < cells && op < len)
727 paste_tabular->getCellInset(cell)->
728 setText(clip.substr(op, len - op), font);
729 } else if (!insertAsciiString(cur.bv(), clip, true)) {
730 // so that the clipboard is used and it goes on
732 // and executes LFUN_PASTESELECTION in insettext!
733 paste_tabular.reset();
739 if (hasPasteBuffer() && tabularStackDirty()) {
740 recordUndoInset(cur, Undo::INSERT);
744 cell(cur.idx())->dispatch(cur, cmd);
755 case LFUN_FREEFONT_APPLY:
756 case LFUN_FREEFONT_UPDATE:
760 case LFUN_CAPITALIZE_WORD:
761 case LFUN_UPCASE_WORD:
762 case LFUN_LOWCASE_WORD:
763 case LFUN_TRANSPOSE_CHARS:
764 if (tablemode(cur)) {
767 getSelection(cur, rs, re, cs, ce);
768 for (row_type i = rs; i <= re; ++i) {
769 for (col_type j = cs; j <= ce; ++j) {
770 // cursor follows cell:
771 cur.idx() = tabular.getCellNumber(i, j);
772 // select this cell only:
775 cur.pos() = cur.top().lastpos();
778 cell(cur.idx())->dispatch(cur, cmd);
781 // Restore original selection
782 cur.idx() = tabular.getCellNumber(rs, cs);
785 cur.idx() = tabular.getCellNumber(re, ce);
786 cur.pos() = cur.top().lastpos();
791 cell(cur.idx())->dispatch(cur, cmd);
795 // we try to handle this event in the insets dispatch function.
796 cell(cur.idx())->dispatch(cur, cmd);
801 InsetTabularMailer(*this).updateDialog(&cur.bv());
805 // function sets an object as defined in func_status.h:
806 // states OK, Unknown, Disabled, On, Off.
807 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
808 FuncStatus & status) const
810 switch (cmd.action) {
811 case LFUN_TABULAR_FEATURE: {
812 int action = LyXTabular::LAST_ACTION;
814 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
815 string const tmp = tabularFeature[i].feature;
816 if (tmp == cmd.argument.substr(0, tmp.length())) {
817 action = tabularFeature[i].action;
821 if (action == LyXTabular::LAST_ACTION) {
823 status.unknown(true);
827 string const argument
828 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
830 row_type sel_row_start = 0;
831 row_type sel_row_end = 0;
833 LyXTabular::ltType dummyltt;
836 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
839 case LyXTabular::SET_PWIDTH:
840 case LyXTabular::SET_MPWIDTH:
841 case LyXTabular::SET_SPECIAL_COLUMN:
842 case LyXTabular::SET_SPECIAL_MULTI:
843 case LyXTabular::APPEND_ROW:
844 case LyXTabular::APPEND_COLUMN:
845 case LyXTabular::DELETE_ROW:
846 case LyXTabular::DELETE_COLUMN:
847 case LyXTabular::SET_ALL_LINES:
848 case LyXTabular::UNSET_ALL_LINES:
852 case LyXTabular::MULTICOLUMN:
853 status.setOnOff(tabular.isMultiColumn(cur.idx()));
856 case LyXTabular::M_TOGGLE_LINE_TOP:
858 case LyXTabular::TOGGLE_LINE_TOP:
859 status.setOnOff(tabular.topLine(cur.idx(), flag));
862 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
864 case LyXTabular::TOGGLE_LINE_BOTTOM:
865 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
868 case LyXTabular::M_TOGGLE_LINE_LEFT:
870 case LyXTabular::TOGGLE_LINE_LEFT:
871 status.setOnOff(tabular.leftLine(cur.idx(), flag));
874 case LyXTabular::M_TOGGLE_LINE_RIGHT:
876 case LyXTabular::TOGGLE_LINE_RIGHT:
877 status.setOnOff(tabular.rightLine(cur.idx(), flag));
880 case LyXTabular::M_ALIGN_LEFT:
882 case LyXTabular::ALIGN_LEFT:
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
886 case LyXTabular::M_ALIGN_RIGHT:
888 case LyXTabular::ALIGN_RIGHT:
889 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
892 case LyXTabular::M_ALIGN_CENTER:
894 case LyXTabular::ALIGN_CENTER:
895 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
898 case LyXTabular::ALIGN_BLOCK:
899 status.enabled(!tabular.getPWidth(cur.idx()).zero());
900 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
903 case LyXTabular::M_VALIGN_TOP:
905 case LyXTabular::VALIGN_TOP:
907 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
910 case LyXTabular::M_VALIGN_BOTTOM:
912 case LyXTabular::VALIGN_BOTTOM:
914 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
917 case LyXTabular::M_VALIGN_MIDDLE:
919 case LyXTabular::VALIGN_MIDDLE:
921 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
924 case LyXTabular::SET_LONGTABULAR:
925 status.setOnOff(tabular.isLongTabular());
928 case LyXTabular::UNSET_LONGTABULAR:
929 status.setOnOff(!tabular.isLongTabular());
932 case LyXTabular::SET_ROTATE_TABULAR:
933 status.setOnOff(tabular.getRotateTabular());
936 case LyXTabular::UNSET_ROTATE_TABULAR:
937 status.setOnOff(!tabular.getRotateTabular());
940 case LyXTabular::SET_ROTATE_CELL:
941 status.setOnOff(tabular.getRotateCell(cur.idx()));
944 case LyXTabular::UNSET_ROTATE_CELL:
945 status.setOnOff(!tabular.getRotateCell(cur.idx()));
948 case LyXTabular::SET_USEBOX:
949 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
952 case LyXTabular::SET_LTFIRSTHEAD:
953 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
956 case LyXTabular::UNSET_LTFIRSTHEAD:
957 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
960 case LyXTabular::SET_LTHEAD:
961 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
964 case LyXTabular::UNSET_LTHEAD:
965 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
968 case LyXTabular::SET_LTFOOT:
969 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
972 case LyXTabular::UNSET_LTFOOT:
973 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
976 case LyXTabular::SET_LTLASTFOOT:
977 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
980 case LyXTabular::UNSET_LTLASTFOOT:
981 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
984 case LyXTabular::SET_LTNEWPAGE:
985 status.setOnOff(tabular.getLTNewPage(sel_row_start));
990 status.enabled(false);
996 // These are only enabled inside tabular
997 case LFUN_CELL_BACKWARD:
998 case LFUN_CELL_FORWARD:
999 status.enabled(true);
1002 // disable these with multiple cells selected
1003 case LFUN_INSET_INSERT:
1004 case LFUN_TABULAR_INSERT:
1005 case LFUN_INSERT_CHARSTYLE:
1006 case LFUN_INSET_FLOAT:
1007 case LFUN_INSET_WIDE_FLOAT:
1008 case LFUN_INSET_FOOTNOTE:
1009 case LFUN_INSET_MARGINAL:
1010 case LFUN_INSERT_MATH:
1011 case LFUN_MATH_MODE:
1012 case LFUN_MATH_MUTATE:
1013 case LFUN_MATH_DISPLAY:
1014 case LFUN_INSERT_NOTE:
1015 case LFUN_INSET_OPTARG:
1016 case LFUN_INSERT_BOX:
1017 case LFUN_INSERT_BRANCH:
1018 case LFUN_INSET_WRAP:
1019 case LFUN_INSET_ERT: {
1020 if (tablemode(cur)) {
1021 status.enabled(false);
1024 return cell(cur.idx())->getStatus(cur, cmd, status);
1027 // disable in non-fixed-width cells
1028 case LFUN_BREAKLINE:
1029 case LFUN_BREAKPARAGRAPH:
1030 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1031 case LFUN_BREAKPARAGRAPH_SKIP: {
1032 if (tabular.getPWidth(cur.idx()).zero()) {
1033 status.enabled(false);
1036 return cell(cur.idx())->getStatus(cur, cmd, status);
1039 case LFUN_INSET_MODIFY:
1040 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1041 status.enabled(true);
1047 // we try to handle this event in the insets dispatch function.
1048 return cell(cur.idx())->getStatus(cur, cmd, status);
1053 int InsetTabular::latex(Buffer const & buf, ostream & os,
1054 OutputParams const & runparams) const
1056 return tabular.latex(buf, os, runparams);
1060 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1061 OutputParams const & runparams) const
1063 int const dp = runparams.linelen ? runparams.depth : 0;
1064 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1068 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1069 OutputParams const & runparams) const
1071 return tabular.linuxdoc(buf,os, runparams);
1075 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1076 OutputParams const & runparams) const
1079 InsetBase * master = 0;
1081 #ifdef WITH_WARNINGS
1082 #warning Why not pass a proper DocIterator here?
1085 // if the table is inside a float it doesn't need the informaltable
1086 // wrapper. Search for it.
1087 for (master = owner(); master; master = master->owner())
1088 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1093 os << "<informaltable>";
1096 ret += tabular.docbook(buf, os, runparams);
1098 os << "</informaltable>";
1105 void InsetTabular::validate(LaTeXFeatures & features) const
1107 tabular.validate(features);
1111 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1113 return tabular.getCellInset(idx);
1117 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1119 return tabular.getCellInset(idx);
1123 void InsetTabular::cursorPos
1124 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1126 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1128 // y offset correction
1129 int const row = tabular.row_of_cell(sl.idx());
1130 for (int i = 0; i <= row; ++i) {
1132 y += tabular.getAscentOfRow(i);
1133 y += tabular.getAdditionalHeight(i);
1136 y += tabular.getDescentOfRow(i);
1139 // x offset correction
1140 int const col = tabular.column_of_cell(sl.idx());
1141 int idx = tabular.getCellNumber(row, 0);
1142 for (int j = 0; j < col; ++j) {
1143 if (tabular.isPartOfMultiColumn(row, j))
1145 x += tabular.getWidthOfColumn(idx);
1148 x += tabular.getBeginningOfTextInCell(idx);
1149 x += ADD_TO_TABULAR_WIDTH;
1154 int InsetTabular::dist(idx_type const cell, int x, int y) const
1158 InsetBase const & inset = *tabular.getCellInset(cell);
1159 Point o = theCoords.getInsets().xy(&inset);
1160 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1161 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1162 int const ybeg = o.y_ - inset.ascent();
1163 row_type const row = tabular.row_of_cell(cell);
1164 int const rowheight = tabular.getAscentOfRow(row)
1165 + tabular.getDescentOfRow(row)
1166 + tabular.getAdditionalHeight(row);
1167 int const yend = ybeg + rowheight;
1179 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1180 // << " ybeg=" << ybeg << " yend=" << yend
1181 // << " xx=" << xx << " yy=" << yy
1182 // << " dist=" << xx + yy << endl;
1187 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1189 //lyxerr << "InsetTabular::editXY: " << this << endl;
1190 cur.selection() = false;
1192 cur.idx() = getNearestCell(x, y);
1194 return cell(cur.idx())->text_.editXY(cur, x, y);
1198 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1200 cur.idx() = getNearestCell(x, y);
1201 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1205 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1207 idx_type idx_min = 0;
1208 int dist_min = std::numeric_limits<int>::max();
1209 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1210 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1211 int const d = dist(i, x, y);
1222 int InsetTabular::getCellXPos(idx_type const cell) const
1226 for (; !tabular.isFirstCellInRow(c); --c)
1228 int lx = tabular.getWidthOfColumn(cell);
1229 for (; c < cell; ++c)
1230 lx += tabular.getWidthOfColumn(c);
1232 return lx - tabular.getWidthOfColumn(cell);
1236 void InsetTabular::resetPos(LCursor & cur) const
1238 BufferView & bv = cur.bv();
1239 int const maxwidth = bv.workWidth();
1241 if (&cur.inset() != this) {
1245 int const X2 = maxwidth;
1246 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1247 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1248 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1251 scx_ = X1 + 20 - x1;
1253 scx_ = X2 - 20 - x2;
1260 InsetTabularMailer(*this).updateDialog(&bv);
1264 void InsetTabular::moveNextCell(LCursor & cur)
1266 if (isRightToLeft(cur)) {
1267 if (tabular.isFirstCellInRow(cur.idx())) {
1268 row_type const row = tabular.row_of_cell(cur.idx());
1269 if (row == tabular.rows() - 1)
1271 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1278 if (tabular.isLastCell(cur.idx()))
1288 void InsetTabular::movePrevCell(LCursor & cur)
1290 if (isRightToLeft(cur)) {
1291 if (tabular.isLastCellInRow(cur.idx())) {
1292 row_type const row = tabular.row_of_cell(cur.idx());
1295 cur.idx() = tabular.getFirstCellInRow(row);
1296 cur.idx() = tabular.getCellAbove(cur.idx());
1298 if (tabular.isLastCell(cur.idx()))
1303 if (cur.idx() == 0) // first cell
1307 cur.pit() = cur.lastpit();
1308 cur.pos() = cur.lastpos();
1313 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1315 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1318 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1319 string const tmp = tabularFeature[i].feature;
1321 if (tmp == what.substr(0, tmp.length())) {
1322 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1323 //tabularFeatures[i].feature.length())) {
1324 action = tabularFeature[i].action;
1328 if (action == LyXTabular::LAST_ACTION)
1332 ltrim(what.substr(tabularFeature[i].feature.length()));
1333 tabularFeatures(cur, action, val);
1340 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1341 string const & special, bool & flag)
1343 if (special == "dl_above") {
1346 } else if (special == "dl_below") {
1347 ltt.bottomDL = flag;
1349 } else if (special == "empty") {
1361 void InsetTabular::tabularFeatures(LCursor & cur,
1362 LyXTabular::Feature feature, string const & value)
1364 BufferView & bv = cur.bv();
1365 col_type sel_col_start;
1366 col_type sel_col_end;
1367 row_type sel_row_start;
1368 row_type sel_row_end;
1369 bool setLines = false;
1370 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1371 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1375 case LyXTabular::M_ALIGN_LEFT:
1376 case LyXTabular::ALIGN_LEFT:
1377 setAlign = LYX_ALIGN_LEFT;
1380 case LyXTabular::M_ALIGN_RIGHT:
1381 case LyXTabular::ALIGN_RIGHT:
1382 setAlign = LYX_ALIGN_RIGHT;
1385 case LyXTabular::M_ALIGN_CENTER:
1386 case LyXTabular::ALIGN_CENTER:
1387 setAlign = LYX_ALIGN_CENTER;
1390 case LyXTabular::ALIGN_BLOCK:
1391 setAlign = LYX_ALIGN_BLOCK;
1394 case LyXTabular::M_VALIGN_TOP:
1395 case LyXTabular::VALIGN_TOP:
1396 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1399 case LyXTabular::M_VALIGN_BOTTOM:
1400 case LyXTabular::VALIGN_BOTTOM:
1401 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1404 case LyXTabular::M_VALIGN_MIDDLE:
1405 case LyXTabular::VALIGN_MIDDLE:
1406 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1413 recordUndoInset(cur, Undo::ATOMIC);
1415 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1416 row_type const row = tabular.row_of_cell(cur.idx());
1417 col_type const column = tabular.column_of_cell(cur.idx());
1419 LyXTabular::ltType ltt;
1423 case LyXTabular::SET_PWIDTH: {
1424 LyXLength const len(value);
1425 tabular.setColumnPWidth(cur.idx(), len);
1426 // cur position can become invalid after newlines were removed
1427 if (cur.pos() > cur.lastpos())
1428 cur.pos() = cur.lastpos();
1430 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1431 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1435 case LyXTabular::SET_MPWIDTH:
1436 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1437 // cur position can become invalid after newlines were removed
1438 if (cur.pos() > cur.lastpos())
1439 cur.pos() = cur.lastpos();
1442 case LyXTabular::SET_SPECIAL_COLUMN:
1443 case LyXTabular::SET_SPECIAL_MULTI:
1444 tabular.setAlignSpecial(cur.idx(),value,feature);
1447 case LyXTabular::APPEND_ROW:
1448 // append the row into the tabular
1449 tabular.appendRow(bv.buffer()->params(), cur.idx());
1452 case LyXTabular::APPEND_COLUMN:
1453 // append the column into the tabular
1454 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1455 cur.idx() = tabular.getCellNumber(row, column);
1458 case LyXTabular::DELETE_ROW:
1459 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1460 tabular.deleteRow(sel_row_start);
1461 if (sel_row_start >= tabular.rows())
1463 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1466 cur.selection() = false;
1469 case LyXTabular::DELETE_COLUMN:
1470 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1471 tabular.deleteColumn(sel_col_start);
1472 if (sel_col_start >= tabular.columns())
1474 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1477 cur.selection() = false;
1480 case LyXTabular::M_TOGGLE_LINE_TOP:
1482 case LyXTabular::TOGGLE_LINE_TOP: {
1483 bool lineSet = !tabular.topLine(cur.idx(), flag);
1484 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1485 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1487 tabular.getCellNumber(i, j),
1492 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1494 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1495 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1496 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1497 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1498 tabular.setBottomLine(
1499 tabular.getCellNumber(i, j),
1505 case LyXTabular::M_TOGGLE_LINE_LEFT:
1507 case LyXTabular::TOGGLE_LINE_LEFT: {
1508 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1509 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1510 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1511 tabular.setLeftLine(
1512 tabular.getCellNumber(i,j),
1518 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1520 case LyXTabular::TOGGLE_LINE_RIGHT: {
1521 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1522 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1523 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1524 tabular.setRightLine(
1525 tabular.getCellNumber(i,j),
1531 case LyXTabular::M_ALIGN_LEFT:
1532 case LyXTabular::M_ALIGN_RIGHT:
1533 case LyXTabular::M_ALIGN_CENTER:
1535 case LyXTabular::ALIGN_LEFT:
1536 case LyXTabular::ALIGN_RIGHT:
1537 case LyXTabular::ALIGN_CENTER:
1538 case LyXTabular::ALIGN_BLOCK:
1539 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1540 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1541 tabular.setAlignment(
1542 tabular.getCellNumber(i, j),
1547 case LyXTabular::M_VALIGN_TOP:
1548 case LyXTabular::M_VALIGN_BOTTOM:
1549 case LyXTabular::M_VALIGN_MIDDLE:
1551 case LyXTabular::VALIGN_TOP:
1552 case LyXTabular::VALIGN_BOTTOM:
1553 case LyXTabular::VALIGN_MIDDLE:
1554 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1555 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1556 tabular.setVAlignment(
1557 tabular.getCellNumber(i, j),
1561 case LyXTabular::MULTICOLUMN: {
1562 if (sel_row_start != sel_row_end) {
1563 #ifdef WITH_WARNINGS
1564 #warning Need I say it ? This is horrible.
1566 Alert::error(_("Error setting multicolumn"),
1567 _("You cannot set multicolumn vertically."));
1570 if (!cur.selection()) {
1571 // just multicol for one single cell
1572 // check whether we are completely in a multicol
1573 if (tabular.isMultiColumn(cur.idx()))
1574 tabular.unsetMultiColumn(cur.idx());
1576 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1579 // we have a selection so this means we just add all this
1580 // cells to form a multicolumn cell
1581 idx_type const s_start = cur.selBegin().idx();
1582 idx_type const s_end = cur.selEnd().idx();
1583 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1584 cur.idx() = s_start;
1587 cur.selection() = false;
1591 case LyXTabular::SET_ALL_LINES:
1593 case LyXTabular::UNSET_ALL_LINES:
1594 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1595 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1596 tabular.setAllLines(
1597 tabular.getCellNumber(i,j), setLines);
1600 case LyXTabular::SET_LONGTABULAR:
1601 tabular.setLongTabular(true);
1604 case LyXTabular::UNSET_LONGTABULAR:
1605 tabular.setLongTabular(false);
1608 case LyXTabular::SET_ROTATE_TABULAR:
1609 tabular.setRotateTabular(true);
1612 case LyXTabular::UNSET_ROTATE_TABULAR:
1613 tabular.setRotateTabular(false);
1616 case LyXTabular::SET_ROTATE_CELL:
1617 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1618 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1619 tabular.setRotateCell(
1620 tabular.getCellNumber(i, j), true);
1623 case LyXTabular::UNSET_ROTATE_CELL:
1624 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1625 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1626 tabular.setRotateCell(
1627 tabular.getCellNumber(i, j), false);
1630 case LyXTabular::SET_USEBOX: {
1631 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1632 if (val == tabular.getUsebox(cur.idx()))
1633 val = LyXTabular::BOX_NONE;
1634 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1635 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1636 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1640 case LyXTabular::UNSET_LTFIRSTHEAD:
1642 case LyXTabular::SET_LTFIRSTHEAD:
1643 tabular.getRowOfLTFirstHead(row, ltt);
1644 checkLongtableSpecial(ltt, value, flag);
1645 tabular.setLTHead(row, flag, ltt, true);
1648 case LyXTabular::UNSET_LTHEAD:
1650 case LyXTabular::SET_LTHEAD:
1651 tabular.getRowOfLTHead(row, ltt);
1652 checkLongtableSpecial(ltt, value, flag);
1653 tabular.setLTHead(row, flag, ltt, false);
1656 case LyXTabular::UNSET_LTFOOT:
1658 case LyXTabular::SET_LTFOOT:
1659 tabular.getRowOfLTFoot(row, ltt);
1660 checkLongtableSpecial(ltt, value, flag);
1661 tabular.setLTFoot(row, flag, ltt, false);
1664 case LyXTabular::UNSET_LTLASTFOOT:
1666 case LyXTabular::SET_LTLASTFOOT:
1667 tabular.getRowOfLTLastFoot(row, ltt);
1668 checkLongtableSpecial(ltt, value, flag);
1669 tabular.setLTFoot(row, flag, ltt, true);
1672 case LyXTabular::SET_LTNEWPAGE:
1673 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1676 // dummy stuff just to avoid warnings
1677 case LyXTabular::LAST_ACTION:
1681 InsetTabularMailer(*this).updateDialog(&bv);
1685 bool InsetTabular::showInsetDialog(BufferView * bv) const
1687 InsetTabularMailer(*this).showDialog(bv);
1692 void InsetTabular::openLayoutDialog(BufferView * bv) const
1694 InsetTabularMailer(*this).showDialog(bv);
1698 void InsetTabular::getLabelList(Buffer const & buffer,
1699 vector<string> & list) const
1701 tabular.getLabelList(buffer, list);
1705 bool InsetTabular::copySelection(LCursor & cur)
1707 if (!cur.selection())
1712 getSelection(cur, rs, re, cs, ce);
1714 paste_tabular.reset(new LyXTabular(tabular));
1716 for (row_type i = 0; i < rs; ++i)
1717 paste_tabular->deleteRow(0);
1719 row_type const rows = re - rs + 1;
1720 while (paste_tabular->rows() > rows)
1721 paste_tabular->deleteRow(rows);
1723 paste_tabular->setTopLine(0, true, true);
1724 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1727 for (col_type i = 0; i < cs; ++i)
1728 paste_tabular->deleteColumn(0);
1730 col_type const columns = ce - cs + 1;
1731 while (paste_tabular->columns() > columns)
1732 paste_tabular->deleteColumn(columns);
1734 paste_tabular->setLeftLine(0, true, true);
1735 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1739 OutputParams const runparams;
1740 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1741 cur.bv().stuffClipboard(os.str());
1742 // mark tabular stack dirty
1743 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1744 // when we (hopefully) have a one-for-all paste mechanism.
1745 lyx::cap::dirtyTabularStack(true);
1751 bool InsetTabular::pasteSelection(LCursor & cur)
1755 col_type const actcol = tabular.column_of_cell(cur.idx());
1756 row_type const actrow = tabular.row_of_cell(cur.idx());
1757 for (row_type r1 = 0, r2 = actrow;
1758 r1 < paste_tabular->rows() && r2 < tabular.rows();
1760 for (col_type c1 = 0, c2 = actcol;
1761 c1 < paste_tabular->columns() && c2 < tabular.columns();
1763 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1764 tabular.isPartOfMultiColumn(r2, c2))
1766 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1770 if (tabular.isPartOfMultiColumn(r2, c2)) {
1774 shared_ptr<InsetText> inset(
1775 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1776 tabular.setCellInset(r2, c2, inset);
1785 void InsetTabular::cutSelection(LCursor & cur)
1787 if (!cur.selection())
1792 getSelection(cur, rs, re, cs, ce);
1793 for (row_type i = rs; i <= re; ++i) {
1794 for (col_type j = cs; j <= ce; ++j) {
1795 shared_ptr<InsetText> t
1796 = cell(tabular.getCellNumber(i, j));
1797 if (cur.buffer().params().tracking_changes)
1798 t->markErased(true);
1804 // cursor position might be invalid now
1805 cur.pos() = cur.lastpos();
1806 cur.clearSelection();
1810 bool InsetTabular::isRightToLeft(LCursor & cur) const
1812 BOOST_ASSERT(cur.depth() > 1);
1813 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1814 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1815 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1816 parentpos).language()->RightToLeft();
1820 void InsetTabular::getSelection(LCursor & cur,
1821 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1823 CursorSlice const & beg = cur.selBegin();
1824 CursorSlice const & end = cur.selEnd();
1825 cs = tabular.column_of_cell(beg.idx());
1826 ce = tabular.column_of_cell(end.idx());
1829 cs = tabular.column_of_cell(end.idx());
1831 ce = tabular.right_column_of_cell(end.idx());
1834 rs = tabular.row_of_cell(beg.idx());
1835 re = tabular.row_of_cell(end.idx());
1841 LyXText * InsetTabular::getText(int idx) const
1843 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1847 void InsetTabular::markErased(bool erased)
1849 for (idx_type idx = 0; idx < nargs(); ++idx)
1850 cell(idx)->markErased(erased);
1854 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1856 return tabular.getPWidth(cell).zero();
1860 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1863 if (buf.length() <= 0)
1868 col_type maxCols = 1;
1869 string::size_type const len = buf.length();
1870 string::size_type p = 0;
1872 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1880 maxCols = max(cols, maxCols);
1886 maxCols = max(cols, maxCols);
1887 LyXTabular * loctab;
1892 paste_tabular.reset(
1893 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1894 loctab = paste_tabular.get();
1898 cell = bv.cursor().idx();
1899 ocol = tabular.column_of_cell(cell);
1900 row = tabular.row_of_cell(cell);
1903 string::size_type op = 0;
1904 idx_type const cells = loctab->getNumberOfCells();
1907 rows = loctab->rows();
1908 col_type const columns = loctab->columns();
1910 while (cell < cells && p < len && row < rows &&
1911 (p = buf.find_first_of("\t\n", p)) != string::npos)
1917 // we can only set this if we are not too far right
1918 if (cols < columns) {
1919 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1920 Paragraph & par = inset->text_.getPar(0);
1921 LyXFont const font = inset->text_.getFont(par, 0);
1922 inset->setText(buf.substr(op, p - op), font);
1928 // we can only set this if we are not too far right
1929 if (cols < columns) {
1930 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1931 Paragraph & par = inset->text_.getPar(0);
1932 LyXFont const font = inset->text_.getFont(par, 0);
1933 inset->setText(buf.substr(op, p - op), font);
1938 cell = loctab->getCellNumber(row, cols);
1944 // check for the last cell if there is no trailing '\n'
1945 if (cell < cells && op < len) {
1946 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1947 Paragraph & par = inset->text_.getPar(0);
1948 LyXFont const font = inset->text_.getFont(par, 0);
1949 inset->setText(buf.substr(op, len - op), font);
1955 void InsetTabular::addPreview(PreviewLoader & loader) const
1957 row_type const rows = tabular.rows();
1958 col_type const columns = tabular.columns();
1959 for (row_type i = 0; i < rows; ++i) {
1960 for (col_type j = 0; j < columns; ++j)
1961 tabular.getCellInset(i, j)->addPreview(loader);
1966 bool InsetTabular::tablemode(LCursor & cur) const
1968 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1975 string const InsetTabularMailer::name_("tabular");
1977 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1978 : inset_(const_cast<InsetTabular &>(inset))
1982 string const InsetTabularMailer::inset2string(Buffer const &) const
1984 return params2string(inset_);
1988 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1990 istringstream data(in);
1992 lex.setStream(data);
1999 if (!lex || token != name_)
2000 return print_mailer_error("InsetTabularMailer", in, 1,
2003 // This is part of the inset proper that is usually swallowed
2004 // by Buffer::readInset
2006 if (!lex || token != "Tabular")
2007 return print_mailer_error("InsetTabularMailer", in, 2,
2010 Buffer const & buffer = inset.buffer();
2011 inset.read(buffer, lex);
2015 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2018 data << name_ << ' ';
2019 inset.write(inset.buffer(), data);
2020 data << "\\end_inset\n";