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 "coordcache.h"
21 #include "dispatchresult.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
29 #include "metricsinfo.h"
30 #include "outputparams.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
36 #include "support/convert.h"
38 #include "frontends/Alert.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/Painter.h"
42 #include "frontends/nullpainter.h"
48 using lyx::graphics::PreviewLoader;
50 using lyx::support::ltrim;
52 using boost::shared_ptr;
58 using std::istringstream;
60 using std::ostringstream;
67 int const ADD_TO_HEIGHT = 2;
68 int const ADD_TO_TABULAR_WIDTH = 2;
71 boost::scoped_ptr<LyXTabular> paste_tabular;
74 struct TabularFeature {
75 LyXTabular::Feature action;
80 TabularFeature tabularFeature[] =
82 { LyXTabular::APPEND_ROW, "append-row" },
83 { LyXTabular::APPEND_COLUMN, "append-column" },
84 { LyXTabular::DELETE_ROW, "delete-row" },
85 { LyXTabular::DELETE_COLUMN, "delete-column" },
86 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
87 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
88 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
89 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
90 { LyXTabular::ALIGN_LEFT, "align-left" },
91 { LyXTabular::ALIGN_RIGHT, "align-right" },
92 { LyXTabular::ALIGN_CENTER, "align-center" },
93 { LyXTabular::ALIGN_BLOCK, "align-block" },
94 { LyXTabular::VALIGN_TOP, "valign-top" },
95 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
96 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
97 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
98 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
99 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
100 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
101 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
102 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
103 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
104 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
105 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
106 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
107 { LyXTabular::MULTICOLUMN, "multicolumn" },
108 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
109 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
110 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
111 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
112 { LyXTabular::SET_PWIDTH, "set-pwidth" },
113 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
114 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
115 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
116 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
117 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
118 { LyXTabular::SET_USEBOX, "set-usebox" },
119 { LyXTabular::SET_LTHEAD, "set-lthead" },
120 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
121 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
122 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
123 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
124 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
125 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
126 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
127 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
128 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
129 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
130 { LyXTabular::LAST_ACTION, "" }
134 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
136 FeatureEqual(LyXTabular::Feature feature)
137 : feature_(feature) {}
138 bool operator()(TabularFeature const & tf) const {
139 return tf.action == feature_;
142 LyXTabular::Feature feature_;
148 string const featureAsString(LyXTabular::Feature feature)
150 TabularFeature * end = tabularFeature +
151 sizeof(tabularFeature) / sizeof(TabularFeature);
152 TabularFeature * it = std::find_if(tabularFeature, end,
153 FeatureEqual(feature));
154 return (it == end) ? string() : it->feature;
158 bool InsetTabular::hasPasteBuffer() const
160 return (paste_tabular.get() != 0);
164 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
166 : tabular(buf.params(), max(rows, row_type(1)),
167 max(columns, col_type(1))), buffer_(&buf), cursorx_(0)
171 InsetTabular::InsetTabular(InsetTabular const & tab)
172 : UpdatableInset(tab), tabular(tab.tabular),
173 buffer_(tab.buffer_), cursorx_(0)
177 InsetTabular::~InsetTabular()
179 InsetTabularMailer(*this).hideDialog();
183 auto_ptr<InsetBase> InsetTabular::doClone() const
185 return auto_ptr<InsetBase>(new InsetTabular(*this));
189 Buffer const & InsetTabular::buffer() const
195 void InsetTabular::buffer(Buffer const * b)
201 void InsetTabular::write(Buffer const & buf, ostream & os) const
203 os << "Tabular" << endl;
204 tabular.write(buf, os);
208 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
210 bool const old_format = (lex.getString() == "\\LyXTable");
212 tabular.read(buf, lex);
218 string token = lex.getString();
219 while (lex.isOK() && (token != "\\end_inset")) {
221 token = lex.getString();
223 if (token != "\\end_inset") {
224 lex.printError("Missing \\end_inset at this point. "
230 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
232 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
233 // mi.base.textwidth << "\n";
235 lyxerr << "InsetTabular::metrics: need bv" << endl;
240 for (idx_type cell = 0; i < tabular.rows(); ++i) {
243 for (col_type j = 0; j < tabular.columns(); ++j) {
244 if (tabular.isPartOfMultiColumn(i, j))
248 LyXLength p_width = tabular.column_info[j].p_width;
249 if (!p_width.zero()) {
250 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
252 tabular.getCellInset(cell)->metrics(m, dim);
253 maxAsc = max(maxAsc, dim.asc);
254 maxDesc = max(maxDesc, dim.des);
255 tabular.setWidthOfCell(cell, dim.wid);
258 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
259 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
262 dim.asc = tabular.getAscentOfRow(0);
263 dim.des = tabular.getHeightOfTabular() - dim.asc;
264 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
269 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
271 setPosCache(pi, x, y);
273 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
274 BufferView * bv = pi.base.bv;
276 static NullPainter nop;
277 static PainterInfo nullpi(bv, nop);
279 resetPos(bv->cursor());
282 x += ADD_TO_TABULAR_WIDTH;
285 first_visible_cell = LyXTabular::npos;
286 for (row_type i = 0; i < tabular.rows(); ++i) {
288 int const a = tabular.getAscentOfRow(i);
289 int const d = tabular.getDescentOfRow(i);
290 idx = tabular.getCellNumber(i, 0);
291 for (col_type j = 0; j < tabular.columns(); ++j) {
292 if (tabular.isPartOfMultiColumn(i, j))
294 if (first_visible_cell == LyXTabular::npos)
295 first_visible_cell = idx;
297 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
298 if (nx + tabular.getWidthOfColumn(idx) < 0
299 || nx > bv->workWidth()
301 || y - a > bv->workHeight()) {
302 cell(idx)->draw(nullpi, cx, y);
303 drawCellLines(nop, nx, y, i, idx);
305 cell(idx)->draw(pi, cx, y);
306 drawCellLines(pi.pain, nx, y, i, idx);
308 nx += tabular.getWidthOfColumn(idx);
312 if (i + 1 < tabular.rows())
313 y += d + tabular.getAscentOfRow(i + 1) +
314 tabular.getAdditionalHeight(i + 1);
319 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
321 setPosCache(pi, x, y);
323 LCursor & cur = pi.base.bv->cursor();
324 if (!cur.selection())
326 if (!ptr_cmp(&cur.inset(), this))
332 x += ADD_TO_TABULAR_WIDTH;
334 if (tablemode(cur)) {
337 getSelection(cur, rs, re, cs, ce);
338 y -= tabular.getAscentOfRow(0);
339 for (row_type j = 0; j < tabular.rows(); ++j) {
340 int const a = tabular.getAscentOfRow(j);
341 int const h = a + tabular.getDescentOfRow(j);
343 y += tabular.getAdditionalHeight(j);
344 for (col_type i = 0; i < tabular.columns(); ++i) {
345 if (tabular.isPartOfMultiColumn(j, i))
347 idx_type const cell =
348 tabular.getCellNumber(j, i);
349 int const w = tabular.getWidthOfColumn(cell);
350 if (i >= cs && i <= ce && j >= rs && j <= re)
351 pi.pain.fillRectangle(xx, y, w, h,
360 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
365 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
366 row_type row, idx_type cell) const
368 int x2 = x + tabular.getWidthOfColumn(cell);
371 if (!tabular.topAlreadyDrawn(cell)) {
372 on_off = !tabular.topLine(cell);
373 pain.line(x, y - tabular.getAscentOfRow(row),
374 x2, y - tabular.getAscentOfRow(row),
375 on_off ? LColor::tabularonoffline : LColor::tabularline,
376 on_off ? Painter::line_onoffdash : Painter::line_solid);
378 on_off = !tabular.bottomLine(cell);
379 pain.line(x, y + tabular.getDescentOfRow(row),
380 x2, y + tabular.getDescentOfRow(row),
381 on_off ? LColor::tabularonoffline : LColor::tabularline,
382 on_off ? Painter::line_onoffdash : Painter::line_solid);
383 if (!tabular.leftAlreadyDrawn(cell)) {
384 on_off = !tabular.leftLine(cell);
385 pain.line(x, y - tabular.getAscentOfRow(row),
386 x, y + tabular.getDescentOfRow(row),
387 on_off ? LColor::tabularonoffline : LColor::tabularline,
388 on_off ? Painter::line_onoffdash : Painter::line_solid);
390 on_off = !tabular.rightLine(cell);
391 pain.line(x2 - tabular.getAdditionalWidth(cell),
392 y - tabular.getAscentOfRow(row),
393 x2 - tabular.getAdditionalWidth(cell),
394 y + tabular.getDescentOfRow(row),
395 on_off ? LColor::tabularonoffline : LColor::tabularline,
396 on_off ? Painter::line_onoffdash : Painter::line_solid);
400 string const InsetTabular::editMessage() const
402 return _("Opened table");
406 void InsetTabular::edit(LCursor & cur, bool left)
408 lyxerr << "InsetTabular::edit: " << this << endl;
410 cur.selection() = false;
413 if (isRightToLeft(cur))
414 cur.idx() = tabular.getLastCellInRow(0);
420 if (isRightToLeft(cur))
421 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
423 cur.idx() = tabular.getNumberOfCells() - 1;
425 cur.pos() = cur.lastpos(); // FIXME crude guess
427 // this accesses the position cache before it is initialized
429 //cur.bv().fitCursor();
433 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
435 lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
436 //lyxerr << " cur:\n" << cur << endl;
437 CursorSlice sl = cur.top();
438 LCursor & bvcur = cur.bv().cursor();
440 switch (cmd.action) {
442 case LFUN_MOUSE_PRESS:
443 lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
445 if (cmd.button() == mouse_button::button1) {
446 cur.selection() = false;
447 setCursorFromCoordinates(cur, cmd.x, cmd.y);
453 if (cmd.button() == mouse_button::button2) {
454 // FIXME: pasting multiple cells (with insettabular's own
455 // LFUN_PASTESELECTION still does not work! (jspitzm)
456 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
457 cell(cur.idx())->dispatch(cur, cmd);
461 // we'll pop up the table dialog on release
462 if (cmd.button() == mouse_button::button3)
466 case LFUN_MOUSE_MOTION:
467 lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
468 if (cmd.button() == mouse_button::button1) {
469 // only accept motions to places not deeper nested than the real anchor
470 if (bvcur.anchor_.hasPart(cur)) {
471 setCursorFromCoordinates(cur, cmd.x, cmd.y);
472 bvcur.setCursor(cur);
473 bvcur.selection() = true;
479 case LFUN_MOUSE_RELEASE:
480 lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
481 if (cmd.button() == mouse_button::button3)
482 InsetTabularMailer(*this).showDialog(&cur.bv());
485 case LFUN_CELL_BACKWARD:
487 cur.selection() = false;
490 case LFUN_CELL_FORWARD:
492 cur.selection() = false;
495 case LFUN_SCROLL_INSET:
496 if (cmd.argument.empty())
498 if (cmd.argument.find('.') != cmd.argument.npos)
499 scroll(cur.bv(), static_cast<float>(convert<double>(cmd.argument)));
501 scroll(cur.bv(), convert<int>(cmd.argument));
506 cell(cur.idx())->dispatch(cur, cmd);
507 cur.dispatched(); // override the cell's decision
509 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
510 if (sl == cur.top()) {
511 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
519 cell(cur.idx())->dispatch(cur, cmd);
520 cur.dispatched(); // override the cell's decision
522 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
523 if (sl == cur.top()) {
524 cmd = FuncRequest(LFUN_FINISHED_LEFT);
532 cell(cur.idx())->dispatch(cur, cmd);
533 cur.dispatched(); // override the cell's decision
535 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
536 cur.idx() = tabular.getCellBelow(cur.idx());
541 if (sl == cur.top()) {
542 cmd = FuncRequest(LFUN_FINISHED_DOWN);
550 cell(cur.idx())->dispatch(cur, cmd);
551 cur.dispatched(); // override the cell's decision
553 if (tabular.row_of_cell(cur.idx()) != 0) {
554 cur.idx() = tabular.getCellAbove(cur.idx());
555 cur.pit() = cur.lastpit();
556 cur.pos() = cur.lastpos();
559 if (sl == cur.top()) {
560 cmd = FuncRequest(LFUN_FINISHED_UP);
567 // //if (hasSelection())
568 // // cur.selection() = false;
569 // col_type const col = tabular.column_of_cell(cur.idx());
570 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
571 // if (t < yo() + tabular.getHeightOfTabular()) {
572 // cur.bv().scrollDocView(t);
573 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
575 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
583 // case LFUN_PRIOR: {
584 // //if (hasSelection())
585 // // cur.selection() = false;
586 // col_type const col = tabular.column_of_cell(cur.idx());
587 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
589 // cur.bv().scrollDocView(t);
593 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
597 // cur.par() = cur.lastpar();
598 // cur.pos() = cur.lastpos();
603 case LFUN_LAYOUT_TABULAR:
604 InsetTabularMailer(*this).showDialog(&cur.bv());
607 case LFUN_INSET_DIALOG_UPDATE:
608 InsetTabularMailer(*this).updateDialog(&cur.bv());
611 case LFUN_TABULAR_FEATURE:
612 if (!tabularFeatures(cur, cmd.argument))
616 // insert file functions
617 case LFUN_FILE_INSERT_ASCII_PARA:
618 case LFUN_FILE_INSERT_ASCII: {
619 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
620 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
626 if (tablemode(cur)) {
627 if (copySelection(cur)) {
628 recordUndo(cur, Undo::DELETE);
633 cell(cur.idx())->dispatch(cur, cmd);
638 recordUndo(cur, Undo::DELETE);
642 cell(cur.idx())->dispatch(cur, cmd);
646 if (!cur.selection())
648 if (tablemode(cur)) {
652 cell(cur.idx())->dispatch(cur, cmd);
655 case LFUN_PASTESELECTION: {
656 string const clip = cur.bv().getClipboard();
659 if (clip.find('\t') != string::npos) {
662 col_type maxCols = 1;
663 size_t len = clip.length();
664 for (size_t p = 0; p < len; ++p) {
665 p = clip.find_first_of("\t\n", p);
666 if (p == string::npos)
675 maxCols = max(cols, maxCols);
680 maxCols = max(cols, maxCols);
683 new LyXTabular(cur.buffer().params(), rows, maxCols));
685 string::size_type op = 0;
687 idx_type const cells =
688 paste_tabular->getNumberOfCells();
691 for (size_t p = 0; cell < cells && p < len; ++p) {
692 p = clip.find_first_of("\t\n", p);
693 if (p == string::npos || p >= len)
697 paste_tabular->getCellInset(cell)->
698 setText(clip.substr(op, p - op), font);
703 paste_tabular->getCellInset(cell)->
704 setText(clip.substr(op, p - op), font);
705 while (cols++ < maxCols)
712 // check for the last cell if there is no trailing '\n'
713 if (cell < cells && op < len)
714 paste_tabular->getCellInset(cell)->
715 setText(clip.substr(op, len - op), font);
716 } else if (!insertAsciiString(cur.bv(), clip, true)) {
717 // so that the clipboard is used and it goes on
719 // and executes LFUN_PASTESELECTION in insettext!
720 paste_tabular.reset();
726 if (hasPasteBuffer()) {
727 recordUndo(cur, Undo::INSERT);
731 cell(cur.idx())->dispatch(cur, cmd);
742 case LFUN_FREEFONT_APPLY:
743 case LFUN_FREEFONT_UPDATE:
747 case LFUN_CAPITALIZE_WORD:
748 case LFUN_UPCASE_WORD:
749 case LFUN_LOWCASE_WORD:
750 case LFUN_TRANSPOSE_CHARS:
751 if (tablemode(cur)) {
754 getSelection(cur, rs, re, cs, ce);
755 for (row_type i = rs; i <= re; ++i)
756 for (col_type j = cs; j <= ce; ++j) {
757 // cursor follows cell:
758 cur.idx() = tabular.getCellNumber(i, j);
759 // select this cell only:
762 cur.pos() = cur.top().lastpos();
765 cell(cur.idx())->dispatch(cur, cmd);
767 // Restore original selection
768 cur.idx() = tabular.getCellNumber(rs, cs);
771 cur.idx() = tabular.getCellNumber(re, ce);
772 cur.pos() = cur.top().lastpos();
777 cell(cur.idx())->dispatch(cur, cmd);
781 // we try to handle this event in the insets dispatch function.
782 cell(cur.idx())->dispatch(cur, cmd);
786 InsetTabularMailer(*this).updateDialog(&cur.bv());
790 // function sets an object as defined in func_status.h:
791 // states OK, Unknown, Disabled, On, Off.
792 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
793 FuncStatus & status) const
795 switch (cmd.action) {
796 case LFUN_TABULAR_FEATURE: {
797 int action = LyXTabular::LAST_ACTION;
799 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
800 string const tmp = tabularFeature[i].feature;
801 if (tmp == cmd.argument.substr(0, tmp.length())) {
802 action = tabularFeature[i].action;
806 if (action == LyXTabular::LAST_ACTION) {
808 status.unknown(true);
812 string const argument
813 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
815 row_type sel_row_start = 0;
816 row_type sel_row_end = 0;
818 LyXTabular::ltType dummyltt;
821 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
824 case LyXTabular::SET_PWIDTH:
825 case LyXTabular::SET_MPWIDTH:
826 case LyXTabular::SET_SPECIAL_COLUMN:
827 case LyXTabular::SET_SPECIAL_MULTI:
828 case LyXTabular::APPEND_ROW:
829 case LyXTabular::APPEND_COLUMN:
830 case LyXTabular::DELETE_ROW:
831 case LyXTabular::DELETE_COLUMN:
832 case LyXTabular::SET_ALL_LINES:
833 case LyXTabular::UNSET_ALL_LINES:
837 case LyXTabular::MULTICOLUMN:
838 status.setOnOff(tabular.isMultiColumn(cur.idx()));
841 case LyXTabular::M_TOGGLE_LINE_TOP:
843 case LyXTabular::TOGGLE_LINE_TOP:
844 status.setOnOff(tabular.topLine(cur.idx(), flag));
847 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
849 case LyXTabular::TOGGLE_LINE_BOTTOM:
850 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
853 case LyXTabular::M_TOGGLE_LINE_LEFT:
855 case LyXTabular::TOGGLE_LINE_LEFT:
856 status.setOnOff(tabular.leftLine(cur.idx(), flag));
859 case LyXTabular::M_TOGGLE_LINE_RIGHT:
861 case LyXTabular::TOGGLE_LINE_RIGHT:
862 status.setOnOff(tabular.rightLine(cur.idx(), flag));
865 case LyXTabular::M_ALIGN_LEFT:
867 case LyXTabular::ALIGN_LEFT:
868 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
871 case LyXTabular::M_ALIGN_RIGHT:
873 case LyXTabular::ALIGN_RIGHT:
874 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
877 case LyXTabular::M_ALIGN_CENTER:
879 case LyXTabular::ALIGN_CENTER:
880 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
883 case LyXTabular::ALIGN_BLOCK:
884 status.enabled(!tabular.getPWidth(cur.idx()).zero());
885 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
888 case LyXTabular::M_VALIGN_TOP:
890 case LyXTabular::VALIGN_TOP:
892 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
895 case LyXTabular::M_VALIGN_BOTTOM:
897 case LyXTabular::VALIGN_BOTTOM:
899 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
902 case LyXTabular::M_VALIGN_MIDDLE:
904 case LyXTabular::VALIGN_MIDDLE:
906 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
909 case LyXTabular::SET_LONGTABULAR:
910 status.setOnOff(tabular.isLongTabular());
913 case LyXTabular::UNSET_LONGTABULAR:
914 status.setOnOff(!tabular.isLongTabular());
917 case LyXTabular::SET_ROTATE_TABULAR:
918 status.setOnOff(tabular.getRotateTabular());
921 case LyXTabular::UNSET_ROTATE_TABULAR:
922 status.setOnOff(!tabular.getRotateTabular());
925 case LyXTabular::SET_ROTATE_CELL:
926 status.setOnOff(tabular.getRotateCell(cur.idx()));
929 case LyXTabular::UNSET_ROTATE_CELL:
930 status.setOnOff(!tabular.getRotateCell(cur.idx()));
933 case LyXTabular::SET_USEBOX:
934 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
937 case LyXTabular::SET_LTFIRSTHEAD:
938 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
941 case LyXTabular::UNSET_LTFIRSTHEAD:
942 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
945 case LyXTabular::SET_LTHEAD:
946 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
949 case LyXTabular::UNSET_LTHEAD:
950 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
953 case LyXTabular::SET_LTFOOT:
954 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
957 case LyXTabular::UNSET_LTFOOT:
958 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
961 case LyXTabular::SET_LTLASTFOOT:
962 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
965 case LyXTabular::UNSET_LTLASTFOOT:
966 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
969 case LyXTabular::SET_LTNEWPAGE:
970 status.setOnOff(tabular.getLTNewPage(sel_row_start));
975 status.enabled(false);
981 // These are only enabled inside tabular
982 case LFUN_CELL_BACKWARD:
983 case LFUN_CELL_FORWARD:
984 status.enabled(true);
987 // disable these with multiple cells selected
988 case LFUN_INSET_INSERT:
989 case LFUN_INSERT_CHARSTYLE:
990 case LFUN_INSET_FLOAT:
991 case LFUN_INSET_WIDE_FLOAT:
992 case LFUN_INSET_FOOTNOTE:
993 case LFUN_INSET_MARGINAL:
994 case LFUN_INSERT_MATH:
996 case LFUN_MATH_MUTATE:
997 case LFUN_MATH_DISPLAY:
998 case LFUN_INSERT_NOTE:
999 case LFUN_INSET_OPTARG:
1000 case LFUN_INSERT_BOX:
1001 case LFUN_INSERT_BRANCH:
1002 case LFUN_INSET_WRAP:
1003 case LFUN_INSET_ERT: {
1004 if (tablemode(cur)) {
1005 status.enabled(false);
1010 case LFUN_INSET_MODIFY:
1011 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1012 status.enabled(true);
1018 // we try to handle this event in the insets dispatch function.
1019 return cell(cur.idx())->getStatus(cur, cmd, status);
1024 int InsetTabular::latex(Buffer const & buf, ostream & os,
1025 OutputParams const & runparams) const
1027 return tabular.latex(buf, os, runparams);
1031 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1032 OutputParams const & runparams) const
1034 int const dp = runparams.linelen ? runparams.depth : 0;
1035 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1039 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1040 OutputParams const & runparams) const
1042 return tabular.linuxdoc(buf,os, runparams);
1046 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1047 OutputParams const & runparams) const
1050 InsetBase * master = 0;
1052 #ifdef WITH_WARNINGS
1053 #warning Why not pass a proper DocIterator here?
1056 // if the table is inside a float it doesn't need the informaltable
1057 // wrapper. Search for it.
1058 for (master = owner(); master; master = master->owner())
1059 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1064 os << "<informaltable>";
1067 ret += tabular.docbook(buf, os, runparams);
1069 os << "</informaltable>";
1076 void InsetTabular::validate(LaTeXFeatures & features) const
1078 tabular.validate(features);
1082 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1084 return tabular.getCellInset(idx);
1088 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1090 return tabular.getCellInset(idx);
1094 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1096 cell(sl.idx())->getCursorPos(sl, x, y);
1098 // y offset correction
1099 int const row = tabular.row_of_cell(sl.idx());
1100 for (int i = 0; i <= row; ++i) {
1102 y += tabular.getAscentOfRow(i);
1103 y += tabular.getAdditionalHeight(i);
1106 y += tabular.getDescentOfRow(i);
1109 // x offset correction
1110 int const col = tabular.column_of_cell(sl.idx());
1111 int idx = tabular.getCellNumber(row, 0);
1112 for (int j = 0; j < col; ++j) {
1113 if (tabular.isPartOfMultiColumn(row, j))
1115 x += tabular.getWidthOfColumn(idx);
1118 x += tabular.getBeginningOfTextInCell(idx);
1119 x += ADD_TO_TABULAR_WIDTH;
1127 // Manhattan distance to nearest corner
1128 int dist(InsetOld const & inset, int x, int y)
1132 Point o = theCoords.getInsets().xy(&inset);
1133 int const xo = o.x_;
1134 int const yo = o.y_;
1138 else if (x > xo + inset.width())
1139 xx = x - xo - inset.width();
1141 if (y < yo - inset.ascent())
1142 yy = yo - inset.ascent() - y;
1143 else if (y > yo + inset.descent())
1144 yy = y - yo - inset.descent();
1146 lyxerr << " xo_=" << xo << " yo_=" << yo
1147 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1148 << " descent=" << inset.descent()
1149 << " dist=" << xx + yy << endl;
1157 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
1159 //lyxerr << "InsetTabular::editXY: " << this << endl;
1160 cur.selection() = false;
1161 cur.push(const_cast<InsetTabular&>(*this));
1162 cur.idx() = getNearestCell(x, y);
1164 return cell(cur.idx())->text_.editXY(cur, x, y);
1165 //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1169 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1171 //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1172 cur.idx() = getNearestCell(x, y);
1174 return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1178 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1180 lyxerr << "# InsetTabular::getNearestCell() x=" << x << " y=" << y << endl;
1181 idx_type idx_min = 0;
1182 int dist_min = std::numeric_limits<int>::max();
1183 for (idx_type i = 0; i < nargs(); ++i) {
1184 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1185 int d = dist(*tabular.getCellInset(i), x, y);
1196 int InsetTabular::getCellXPos(idx_type const cell) const
1200 for (; !tabular.isFirstCellInRow(c); --c)
1202 int lx = tabular.getWidthOfColumn(cell);
1203 for (; c < cell; ++c)
1204 lx += tabular.getWidthOfColumn(c);
1206 return lx - tabular.getWidthOfColumn(cell);
1210 void InsetTabular::resetPos(LCursor & cur) const
1212 BufferView & bv = cur.bv();
1213 // col_type const actcol = tabular.column_of_cell(cur.idx());
1214 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1215 // int const new_x = getCellXPos(cur.idx()) + offset;
1216 // int const old_x = cursorx_;
1217 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1218 // cursorx_ = new_x;
1219 // cursor.x(getCellXPos(cur.idx()) + offset);
1220 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1221 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1223 // scroll(bv, 0.0F);
1224 // } else if (cursorx_ - offset > 20 &&
1225 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1226 // scroll(bv, - col_width - 20);
1227 // } else if (cursorx_ - offset < 20) {
1228 // scroll(bv, 20 - cursorx_ + offset);
1229 // } else if (scroll() && xo() > 20 &&
1230 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1231 // scroll(bv, old_x - cursorx_);
1234 if (&cur.inset() != this) {
1238 int const X2 = bv.workWidth();
1239 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1240 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1241 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1244 scroll(bv, X1 + 20 - x1);
1246 scroll(bv, X2 - 20 - x2);
1251 InsetTabularMailer(*this).updateDialog(&bv);
1255 void InsetTabular::moveNextCell(LCursor & cur)
1257 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1258 if (isRightToLeft(cur)) {
1259 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1260 if (tabular.isFirstCellInRow(cur.idx())) {
1261 row_type const row = tabular.row_of_cell(cur.idx());
1262 if (row == tabular.rows() - 1)
1264 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1271 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1272 if (tabular.isLastCell(cur.idx()))
1278 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1283 void InsetTabular::movePrevCell(LCursor & cur)
1285 if (isRightToLeft(cur)) {
1286 if (tabular.isLastCellInRow(cur.idx())) {
1287 row_type const row = tabular.row_of_cell(cur.idx());
1290 cur.idx() = tabular.getFirstCellInRow(row);
1291 cur.idx() = tabular.getCellAbove(cur.idx());
1293 if (tabular.isLastCell(cur.idx()))
1298 if (cur.idx() == 0) // first cell
1302 cur.pit() = cur.lastpit();
1303 cur.pos() = cur.lastpos();
1308 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1310 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1313 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1314 string const tmp = tabularFeature[i].feature;
1316 if (tmp == what.substr(0, tmp.length())) {
1317 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1318 //tabularFeatures[i].feature.length())) {
1319 action = tabularFeature[i].action;
1323 if (action == LyXTabular::LAST_ACTION)
1327 ltrim(what.substr(tabularFeature[i].feature.length()));
1328 tabularFeatures(cur, action, val);
1335 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1336 string const & special, bool & flag)
1338 if (special == "dl_above") {
1341 } else if (special == "dl_below") {
1342 ltt.bottomDL = flag;
1344 } else if (special == "empty") {
1356 void InsetTabular::tabularFeatures(LCursor & cur,
1357 LyXTabular::Feature feature, string const & value)
1359 BufferView & bv = cur.bv();
1360 col_type sel_col_start;
1361 col_type sel_col_end;
1362 row_type sel_row_start;
1363 row_type sel_row_end;
1364 bool setLines = false;
1365 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1366 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1370 case LyXTabular::M_ALIGN_LEFT:
1371 case LyXTabular::ALIGN_LEFT:
1372 setAlign = LYX_ALIGN_LEFT;
1375 case LyXTabular::M_ALIGN_RIGHT:
1376 case LyXTabular::ALIGN_RIGHT:
1377 setAlign = LYX_ALIGN_RIGHT;
1380 case LyXTabular::M_ALIGN_CENTER:
1381 case LyXTabular::ALIGN_CENTER:
1382 setAlign = LYX_ALIGN_CENTER;
1385 case LyXTabular::ALIGN_BLOCK:
1386 setAlign = LYX_ALIGN_BLOCK;
1389 case LyXTabular::M_VALIGN_TOP:
1390 case LyXTabular::VALIGN_TOP:
1391 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1394 case LyXTabular::M_VALIGN_BOTTOM:
1395 case LyXTabular::VALIGN_BOTTOM:
1396 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1399 case LyXTabular::M_VALIGN_MIDDLE:
1400 case LyXTabular::VALIGN_MIDDLE:
1401 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1408 recordUndo(cur, Undo::ATOMIC);
1410 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1411 row_type const row = tabular.row_of_cell(cur.idx());
1412 col_type const column = tabular.column_of_cell(cur.idx());
1414 LyXTabular::ltType ltt;
1418 case LyXTabular::SET_PWIDTH: {
1419 LyXLength const len(value);
1420 tabular.setColumnPWidth(cur.idx(), len);
1422 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1423 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1424 else if (!len.zero()
1425 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1426 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1430 case LyXTabular::SET_MPWIDTH:
1431 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1434 case LyXTabular::SET_SPECIAL_COLUMN:
1435 case LyXTabular::SET_SPECIAL_MULTI:
1436 tabular.setAlignSpecial(cur.idx(),value,feature);
1439 case LyXTabular::APPEND_ROW:
1440 // append the row into the tabular
1441 tabular.appendRow(bv.buffer()->params(), cur.idx());
1444 case LyXTabular::APPEND_COLUMN:
1445 // append the column into the tabular
1446 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1449 case LyXTabular::DELETE_ROW:
1450 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1451 tabular.deleteRow(sel_row_start);
1452 if (sel_row_start >= tabular.rows())
1454 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1457 cur.selection() = false;
1460 case LyXTabular::DELETE_COLUMN:
1461 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1462 tabular.deleteColumn(sel_col_start);
1463 if (sel_col_start >= tabular.columns())
1465 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1468 cur.selection() = false;
1471 case LyXTabular::M_TOGGLE_LINE_TOP:
1473 case LyXTabular::TOGGLE_LINE_TOP: {
1474 bool lineSet = !tabular.topLine(cur.idx(), flag);
1475 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1476 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1478 tabular.getCellNumber(i, j),
1483 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1485 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1486 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1487 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1488 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1489 tabular.setBottomLine(
1490 tabular.getCellNumber(i, j),
1496 case LyXTabular::M_TOGGLE_LINE_LEFT:
1498 case LyXTabular::TOGGLE_LINE_LEFT: {
1499 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1500 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1501 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1502 tabular.setLeftLine(
1503 tabular.getCellNumber(i,j),
1509 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1511 case LyXTabular::TOGGLE_LINE_RIGHT: {
1512 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1513 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1514 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1515 tabular.setRightLine(
1516 tabular.getCellNumber(i,j),
1522 case LyXTabular::M_ALIGN_LEFT:
1523 case LyXTabular::M_ALIGN_RIGHT:
1524 case LyXTabular::M_ALIGN_CENTER:
1526 case LyXTabular::ALIGN_LEFT:
1527 case LyXTabular::ALIGN_RIGHT:
1528 case LyXTabular::ALIGN_CENTER:
1529 case LyXTabular::ALIGN_BLOCK:
1530 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1531 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1532 tabular.setAlignment(
1533 tabular.getCellNumber(i, j),
1538 case LyXTabular::M_VALIGN_TOP:
1539 case LyXTabular::M_VALIGN_BOTTOM:
1540 case LyXTabular::M_VALIGN_MIDDLE:
1542 case LyXTabular::VALIGN_TOP:
1543 case LyXTabular::VALIGN_BOTTOM:
1544 case LyXTabular::VALIGN_MIDDLE:
1545 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1546 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1547 tabular.setVAlignment(
1548 tabular.getCellNumber(i, j),
1552 case LyXTabular::MULTICOLUMN: {
1553 if (sel_row_start != sel_row_end) {
1554 #ifdef WITH_WARNINGS
1555 #warning Need I say it ? This is horrible.
1557 Alert::error(_("Error setting multicolumn"),
1558 _("You cannot set multicolumn vertically."));
1561 if (!cur.selection()) {
1562 // just multicol for one single cell
1563 // check whether we are completely in a multicol
1564 if (tabular.isMultiColumn(cur.idx()))
1565 tabular.unsetMultiColumn(cur.idx());
1567 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1570 // we have a selection so this means we just add all this
1571 // cells to form a multicolumn cell
1572 idx_type const s_start = cur.selBegin().idx();
1573 idx_type const s_end = cur.selEnd().idx();
1574 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1575 cur.idx() = s_start;
1578 cur.selection() = false;
1582 case LyXTabular::SET_ALL_LINES:
1584 case LyXTabular::UNSET_ALL_LINES:
1585 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1586 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1587 tabular.setAllLines(
1588 tabular.getCellNumber(i,j), setLines);
1591 case LyXTabular::SET_LONGTABULAR:
1592 tabular.setLongTabular(true);
1595 case LyXTabular::UNSET_LONGTABULAR:
1596 tabular.setLongTabular(false);
1599 case LyXTabular::SET_ROTATE_TABULAR:
1600 tabular.setRotateTabular(true);
1603 case LyXTabular::UNSET_ROTATE_TABULAR:
1604 tabular.setRotateTabular(false);
1607 case LyXTabular::SET_ROTATE_CELL:
1608 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1609 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1610 tabular.setRotateCell(
1611 tabular.getCellNumber(i, j), true);
1614 case LyXTabular::UNSET_ROTATE_CELL:
1615 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1616 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1617 tabular.setRotateCell(
1618 tabular.getCellNumber(i, j), false);
1621 case LyXTabular::SET_USEBOX: {
1622 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1623 if (val == tabular.getUsebox(cur.idx()))
1624 val = LyXTabular::BOX_NONE;
1625 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1626 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1627 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1631 case LyXTabular::UNSET_LTFIRSTHEAD:
1633 case LyXTabular::SET_LTFIRSTHEAD:
1634 tabular.getRowOfLTFirstHead(row, ltt);
1635 checkLongtableSpecial(ltt, value, flag);
1636 tabular.setLTHead(row, flag, ltt, true);
1639 case LyXTabular::UNSET_LTHEAD:
1641 case LyXTabular::SET_LTHEAD:
1642 tabular.getRowOfLTHead(row, ltt);
1643 checkLongtableSpecial(ltt, value, flag);
1644 tabular.setLTHead(row, flag, ltt, false);
1647 case LyXTabular::UNSET_LTFOOT:
1649 case LyXTabular::SET_LTFOOT:
1650 tabular.getRowOfLTFoot(row, ltt);
1651 checkLongtableSpecial(ltt, value, flag);
1652 tabular.setLTFoot(row, flag, ltt, false);
1655 case LyXTabular::UNSET_LTLASTFOOT:
1657 case LyXTabular::SET_LTLASTFOOT:
1658 tabular.getRowOfLTLastFoot(row, ltt);
1659 checkLongtableSpecial(ltt, value, flag);
1660 tabular.setLTFoot(row, flag, ltt, true);
1663 case LyXTabular::SET_LTNEWPAGE:
1664 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1667 // dummy stuff just to avoid warnings
1668 case LyXTabular::LAST_ACTION:
1672 InsetTabularMailer(*this).updateDialog(&bv);
1676 bool InsetTabular::showInsetDialog(BufferView * bv) const
1678 InsetTabularMailer(*this).showDialog(bv);
1683 void InsetTabular::openLayoutDialog(BufferView * bv) const
1685 InsetTabularMailer(*this).showDialog(bv);
1689 void InsetTabular::getLabelList(Buffer const & buffer,
1690 vector<string> & list) const
1692 tabular.getLabelList(buffer, list);
1696 bool InsetTabular::copySelection(LCursor & cur)
1698 if (!cur.selection())
1703 getSelection(cur, rs, re, cs, ce);
1705 paste_tabular.reset(new LyXTabular(tabular));
1707 for (row_type i = 0; i < rs; ++i)
1708 paste_tabular->deleteRow(0);
1710 row_type const rows = re - rs + 1;
1711 while (paste_tabular->rows() > rows)
1712 paste_tabular->deleteRow(rows);
1714 paste_tabular->setTopLine(0, true, true);
1715 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1718 for (col_type i = 0; i < cs; ++i)
1719 paste_tabular->deleteColumn(0);
1721 col_type const columns = ce - cs + 1;
1722 while (paste_tabular->columns() > columns)
1723 paste_tabular->deleteColumn(columns);
1725 paste_tabular->setLeftLine(0, true, true);
1726 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1730 OutputParams const runparams;
1731 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1732 cur.bv().stuffClipboard(os.str());
1737 bool InsetTabular::pasteSelection(LCursor & cur)
1741 col_type const actcol = tabular.column_of_cell(cur.idx());
1742 row_type const actrow = tabular.row_of_cell(cur.idx());
1743 for (row_type r1 = 0, r2 = actrow;
1744 r1 < paste_tabular->rows() && r2 < tabular.rows();
1746 for (col_type c1 = 0, c2 = actcol;
1747 c1 < paste_tabular->columns() && c2 < tabular.columns();
1749 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1750 tabular.isPartOfMultiColumn(r2, c2))
1752 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1756 if (tabular.isPartOfMultiColumn(r2, c2)) {
1760 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1761 inset = paste_tabular->getCellInset(r1, c1);
1769 void InsetTabular::cutSelection(LCursor & cur)
1771 if (!cur.selection())
1774 bool const track = cur.buffer().params().tracking_changes;
1777 getSelection(cur, rs, re, cs, ce);
1778 for (row_type i = rs; i <= re; ++i)
1779 for (col_type j = cs; j <= ce; ++j)
1780 cell(tabular.getCellNumber(i, j))->clear(track);
1782 // cursor position might be invalid now
1783 cur.pos() = cur.lastpos();
1784 cur.clearSelection();
1788 bool InsetTabular::isRightToLeft(LCursor & cur) const
1790 BOOST_ASSERT(cur.depth() > 1);
1791 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1792 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1793 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1794 parentpos).language()->RightToLeft();
1798 void InsetTabular::getSelection(LCursor & cur,
1799 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1801 CursorSlice const & beg = cur.selBegin();
1802 CursorSlice const & end = cur.selEnd();
1803 cs = tabular.column_of_cell(beg.idx());
1804 ce = tabular.column_of_cell(end.idx());
1807 cs = tabular.column_of_cell(end.idx());
1809 ce = tabular.right_column_of_cell(end.idx());
1812 rs = tabular.row_of_cell(beg.idx());
1813 re = tabular.row_of_cell(end.idx());
1819 size_t InsetTabular::nargs() const
1821 return tabular.getNumberOfCells();
1825 LyXText * InsetTabular::getText(int idx) const
1827 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1831 void InsetTabular::markErased()
1833 for (idx_type idx = 0; idx < nargs(); ++idx)
1834 cell(idx)->markErased();
1838 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1841 idx_type const cell = tabular.getCellFromInset(in);
1842 // FIXME: getCellFromInset() returns now always a valid cell, so
1843 // the stuff below can be deleted, and instead we have:
1844 return tabular.getPWidth(cell).zero();
1847 return tabular.getPWidth(cell).zero();
1849 // this is a workaround for a crash (New, Insert->Tabular,
1850 // Insert->FootNote)
1854 // well we didn't obviously find it so maybe our owner knows more
1855 BOOST_ASSERT(owner());
1856 return owner()->forceDefaultParagraphs(in);
1862 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1865 if (buf.length() <= 0)
1870 col_type maxCols = 1;
1871 string::size_type const len = buf.length();
1872 string::size_type p = 0;
1874 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1882 maxCols = max(cols, maxCols);
1888 maxCols = max(cols, maxCols);
1889 LyXTabular * loctab;
1894 paste_tabular.reset(
1895 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1896 loctab = paste_tabular.get();
1900 cell = bv.cursor().idx();
1901 ocol = tabular.column_of_cell(cell);
1902 row = tabular.row_of_cell(cell);
1905 string::size_type op = 0;
1906 idx_type const cells = loctab->getNumberOfCells();
1909 rows = loctab->rows();
1910 col_type const columns = loctab->columns();
1912 while (cell < cells && p < len && row < rows &&
1913 (p = buf.find_first_of("\t\n", p)) != string::npos)
1919 // we can only set this if we are not too far right
1920 if (cols < columns) {
1921 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1922 Paragraph & par = inset->text_.getPar(0);
1923 LyXFont const font = inset->text_.getFont(par, 0);
1924 inset->setText(buf.substr(op, p - op), font);
1930 // we can only set this if we are not too far right
1931 if (cols < columns) {
1932 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1933 Paragraph & par = inset->text_.getPar(0);
1934 LyXFont const font = inset->text_.getFont(par, 0);
1935 inset->setText(buf.substr(op, p - op), font);
1940 cell = loctab->getCellNumber(row, cols);
1946 // check for the last cell if there is no trailing '\n'
1947 if (cell < cells && op < len) {
1948 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1949 Paragraph & par = inset->text_.getPar(0);
1950 LyXFont const font = inset->text_.getFont(par, 0);
1951 inset->setText(buf.substr(op, len - op), font);
1957 void InsetTabular::addPreview(PreviewLoader & loader) const
1959 row_type const rows = tabular.rows();
1960 col_type const columns = tabular.columns();
1961 for (row_type i = 0; i < rows; ++i) {
1962 for (col_type j = 0; j < columns; ++j)
1963 tabular.getCellInset(i, j)->addPreview(loader);
1968 bool InsetTabular::tablemode(LCursor & cur) const
1970 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1977 string const InsetTabularMailer::name_("tabular");
1979 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1980 : inset_(const_cast<InsetTabular &>(inset))
1984 string const InsetTabularMailer::inset2string(Buffer const &) const
1986 return params2string(inset_);
1990 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1992 istringstream data(in);
1994 lex.setStream(data);
2001 if (!lex || token != name_)
2002 return print_mailer_error("InsetTabularMailer", in, 1,
2005 // This is part of the inset proper that is usually swallowed
2006 // by Buffer::readInset
2008 if (!lex || token != "Tabular")
2009 return print_mailer_error("InsetTabularMailer", in, 2,
2012 Buffer const & buffer = inset.buffer();
2013 inset.read(buffer, lex);
2017 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2020 data << name_ << ' ';
2021 inset.write(inset.buffer(), data);
2022 data << "\\end_inset\n";