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::SET_LTFIRSTHEAD, "set-ltfirsthead" },
121 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
122 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
123 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
124 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
125 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
126 { LyXTabular::LAST_ACTION, "" }
130 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
132 FeatureEqual(LyXTabular::Feature feature)
133 : feature_(feature) {}
134 bool operator()(TabularFeature const & tf) const {
135 return tf.action == feature_;
138 LyXTabular::Feature feature_;
144 string const featureAsString(LyXTabular::Feature feature)
146 TabularFeature * end = tabularFeature +
147 sizeof(tabularFeature) / sizeof(TabularFeature);
148 TabularFeature * it = std::find_if(tabularFeature, end,
149 FeatureEqual(feature));
150 return (it == end) ? string() : it->feature;
154 bool InsetTabular::hasPasteBuffer() const
156 return (paste_tabular.get() != 0);
160 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
162 : tabular(buf.params(), max(rows, row_type(1)),
163 max(columns, col_type(1))), buffer_(&buf), cursorx_(0)
167 InsetTabular::InsetTabular(InsetTabular const & tab)
168 : UpdatableInset(tab), tabular(tab.tabular),
169 buffer_(tab.buffer_), cursorx_(0)
173 InsetTabular::~InsetTabular()
175 InsetTabularMailer(*this).hideDialog();
179 auto_ptr<InsetBase> InsetTabular::doClone() const
181 return auto_ptr<InsetBase>(new InsetTabular(*this));
185 Buffer const & InsetTabular::buffer() const
191 void InsetTabular::buffer(Buffer const * b)
197 void InsetTabular::write(Buffer const & buf, ostream & os) const
199 os << "Tabular" << endl;
200 tabular.write(buf, os);
204 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
206 bool const old_format = (lex.getString() == "\\LyXTable");
208 tabular.read(buf, lex);
214 string token = lex.getString();
215 while (lex.isOK() && (token != "\\end_inset")) {
217 token = lex.getString();
219 if (token != "\\end_inset") {
220 lex.printError("Missing \\end_inset at this point. "
226 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
228 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
229 // mi.base.textwidth << "\n";
231 lyxerr << "InsetTabular::metrics: need bv" << endl;
236 for (idx_type cell = 0; i < tabular.rows(); ++i) {
239 for (col_type j = 0; j < tabular.columns(); ++j) {
240 if (tabular.isPartOfMultiColumn(i, j))
244 LyXLength p_width = tabular.column_info[j].p_width;
245 if (!p_width.zero()) {
246 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
248 tabular.getCellInset(cell)->metrics(m, dim);
249 maxAsc = max(maxAsc, dim.asc);
250 maxDesc = max(maxDesc, dim.des);
251 tabular.setWidthOfCell(cell, dim.wid);
254 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
255 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
258 dim.asc = tabular.getAscentOfRow(0);
259 dim.des = tabular.getHeightOfTabular() - dim.asc;
260 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
265 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
267 setPosCache(pi, x, y);
269 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
270 BufferView * bv = pi.base.bv;
272 static NullPainter nop;
273 static PainterInfo nullpi(bv, nop);
275 resetPos(bv->cursor());
278 x += ADD_TO_TABULAR_WIDTH;
281 first_visible_cell = LyXTabular::npos;
282 for (row_type i = 0; i < tabular.rows(); ++i) {
284 int const a = tabular.getAscentOfRow(i);
285 int const d = tabular.getDescentOfRow(i);
286 idx = tabular.getCellNumber(i, 0);
287 for (col_type j = 0; j < tabular.columns(); ++j) {
288 if (tabular.isPartOfMultiColumn(i, j))
290 if (first_visible_cell == LyXTabular::npos)
291 first_visible_cell = idx;
293 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
294 if (nx + tabular.getWidthOfColumn(idx) < 0
295 || nx > bv->workWidth()
297 || y - a > bv->workHeight()) {
298 cell(idx)->draw(nullpi, cx, y);
299 drawCellLines(nop, nx, y, i, idx);
301 cell(idx)->draw(pi, cx, y);
302 drawCellLines(pi.pain, nx, y, i, idx);
304 nx += tabular.getWidthOfColumn(idx);
308 if (i + 1 < tabular.rows())
309 y += d + tabular.getAscentOfRow(i + 1) +
310 tabular.getAdditionalHeight(i + 1);
315 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
317 setPosCache(pi, x, y);
319 LCursor & cur = pi.base.bv->cursor();
320 if (!cur.selection())
322 if (!ptr_cmp(&cur.inset(), this))
328 x += ADD_TO_TABULAR_WIDTH;
330 if (tablemode(cur)) {
333 getSelection(cur, rs, re, cs, ce);
334 for (row_type j = 0; j < tabular.rows(); ++j) {
335 int const a = tabular.getAscentOfRow(j);
336 int const h = a + tabular.getDescentOfRow(j);
338 y += tabular.getAdditionalHeight(j);
339 for (col_type i = 0; i < tabular.columns(); ++i) {
340 if (tabular.isPartOfMultiColumn(j, i))
342 idx_type const cell =
343 tabular.getCellNumber(j, i);
344 int const w = tabular.getWidthOfColumn(cell);
345 if (i >= cs && i <= ce && j >= rs && j <= re)
346 pi.pain.fillRectangle(xx, y - a, w, h,
355 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
360 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
361 row_type row, idx_type cell) const
363 int x2 = x + tabular.getWidthOfColumn(cell);
366 if (!tabular.topAlreadyDrawn(cell)) {
367 on_off = !tabular.topLine(cell);
368 pain.line(x, y - tabular.getAscentOfRow(row),
369 x2, y - tabular.getAscentOfRow(row),
370 on_off ? LColor::tabularonoffline : LColor::tabularline,
371 on_off ? Painter::line_onoffdash : Painter::line_solid);
373 on_off = !tabular.bottomLine(cell);
374 pain.line(x, y + tabular.getDescentOfRow(row),
375 x2, y + tabular.getDescentOfRow(row),
376 on_off ? LColor::tabularonoffline : LColor::tabularline,
377 on_off ? Painter::line_onoffdash : Painter::line_solid);
378 if (!tabular.leftAlreadyDrawn(cell)) {
379 on_off = !tabular.leftLine(cell);
380 pain.line(x, y - tabular.getAscentOfRow(row),
381 x, y + tabular.getDescentOfRow(row),
382 on_off ? LColor::tabularonoffline : LColor::tabularline,
383 on_off ? Painter::line_onoffdash : Painter::line_solid);
385 on_off = !tabular.rightLine(cell);
386 pain.line(x2 - tabular.getAdditionalWidth(cell),
387 y - tabular.getAscentOfRow(row),
388 x2 - tabular.getAdditionalWidth(cell),
389 y + tabular.getDescentOfRow(row),
390 on_off ? LColor::tabularonoffline : LColor::tabularline,
391 on_off ? Painter::line_onoffdash : Painter::line_solid);
395 string const InsetTabular::editMessage() const
397 return _("Opened table");
401 void InsetTabular::edit(LCursor & cur, bool left)
403 lyxerr << "InsetTabular::edit: " << this << endl;
405 cur.selection() = false;
408 if (isRightToLeft(cur))
409 cur.idx() = tabular.getLastCellInRow(0);
415 if (isRightToLeft(cur))
416 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
418 cur.idx() = tabular.getNumberOfCells() - 1;
420 cur.pos() = cur.lastpos(); // FIXME crude guess
422 // this accesses the position cache before it is initialized
424 //cur.bv().fitCursor();
428 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
430 //lyxerr << "InsetTabular::editXY: " << this << endl;
431 cur.selection() = false;
432 cur.push(const_cast<InsetTabular&>(*this));
433 return setPos(cur, x, y);
434 //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
438 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
440 lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
441 //lyxerr << " cur:\n" << cur << endl;
442 CursorSlice sl = cur.top();
443 LCursor & bvcur = cur.bv().cursor();
445 switch (cmd.action) {
447 case LFUN_MOUSE_PRESS:
448 lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
450 if (cmd.button() == mouse_button::button1) {
451 cur.selection() = false;
452 setPos(cur, cmd.x, cmd.y);
458 //if (cmd.button() == mouse_button::button2)
459 // dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
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 setPos(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 (copySelection(cur)) {
627 recordUndo(cur, Undo::DELETE);
634 recordUndo(cur, Undo::DELETE);
638 cell(cur.idx())->dispatch(cur, cmd);
642 if (!cur.selection())
648 case LFUN_PASTESELECTION: {
649 string const clip = cur.bv().getClipboard();
652 if (clip.find('\t') != string::npos) {
655 col_type maxCols = 1;
656 size_t len = clip.length();
657 for (size_t p = 0; p < len; ++p) {
658 p = clip.find_first_of("\t\n", p);
659 if (p == string::npos)
668 maxCols = max(cols, maxCols);
673 maxCols = max(cols, maxCols);
676 new LyXTabular(cur.buffer().params(), rows, maxCols));
678 string::size_type op = 0;
680 idx_type const cells =
681 paste_tabular->getNumberOfCells();
684 for (size_t p = 0; cell < cells && p < len; ++p) {
685 p = clip.find_first_of("\t\n", p);
686 if (p == string::npos || p >= len)
690 paste_tabular->getCellInset(cell)->
691 setText(clip.substr(op, p - op), font);
696 paste_tabular->getCellInset(cell)->
697 setText(clip.substr(op, p - op), font);
698 while (cols++ < maxCols)
705 // check for the last cell if there is no trailing '\n'
706 if (cell < cells && op < len)
707 paste_tabular->getCellInset(cell)->
708 setText(clip.substr(op, len - op), font);
709 } else if (!insertAsciiString(cur.bv(), clip, true)) {
710 // so that the clipboard is used and it goes on
712 // and executes LFUN_PASTESELECTION in insettext!
713 paste_tabular.reset();
719 if (hasPasteBuffer()) {
720 recordUndo(cur, Undo::INSERT);
724 cell(cur.idx())->dispatch(cur, cmd);
735 case LFUN_FREEFONT_APPLY:
736 case LFUN_FREEFONT_UPDATE:
740 case LFUN_CAPITALIZE_WORD:
741 case LFUN_UPCASE_WORD:
742 case LFUN_LOWCASE_WORD:
743 case LFUN_TRANSPOSE_CHARS:
744 if (tablemode(cur)) {
747 getSelection(cur, rs, re, cs, ce);
748 for (row_type i = rs; i <= re; ++i)
749 for (col_type j = cs; j <= ce; ++j) {
750 // cursor follows cell:
751 cur.idx() = tabular.getCellNumber(i, j);
752 // select this cell only:
755 cur.pos() = cur.top().lastpos();
758 cell(cur.idx())->dispatch(cur, cmd);
760 // Restore original selection
761 cur.idx() = tabular.getCellNumber(rs, cs);
764 cur.idx() = tabular.getCellNumber(re, ce);
765 cur.pos() = cur.top().lastpos();
770 cell(cur.idx())->dispatch(cur, cmd);
774 // we try to handle this event in the insets dispatch function.
775 cell(cur.idx())->dispatch(cur, cmd);
779 InsetTabularMailer(*this).updateDialog(&cur.bv());
783 // function sets an object as defined in func_status.h:
784 // states OK, Unknown, Disabled, On, Off.
785 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
786 FuncStatus & status) const
788 switch (cmd.action) {
789 case LFUN_TABULAR_FEATURE: {
790 int action = LyXTabular::LAST_ACTION;
792 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
793 string const tmp = tabularFeature[i].feature;
794 if (tmp == cmd.argument.substr(0, tmp.length())) {
795 action = tabularFeature[i].action;
799 if (action == LyXTabular::LAST_ACTION) {
801 status.unknown(true);
805 string const argument
806 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
808 row_type sel_row_start = 0;
809 row_type sel_row_end = 0;
811 LyXTabular::ltType dummyltt;
814 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
817 case LyXTabular::SET_PWIDTH:
818 case LyXTabular::SET_MPWIDTH:
819 case LyXTabular::SET_SPECIAL_COLUMN:
820 case LyXTabular::SET_SPECIAL_MULTI:
821 case LyXTabular::APPEND_ROW:
822 case LyXTabular::APPEND_COLUMN:
823 case LyXTabular::DELETE_ROW:
824 case LyXTabular::DELETE_COLUMN:
825 case LyXTabular::SET_ALL_LINES:
826 case LyXTabular::UNSET_ALL_LINES:
830 case LyXTabular::MULTICOLUMN:
831 status.setOnOff(tabular.isMultiColumn(cur.idx()));
834 case LyXTabular::M_TOGGLE_LINE_TOP:
836 case LyXTabular::TOGGLE_LINE_TOP:
837 status.setOnOff(tabular.topLine(cur.idx(), flag));
840 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
842 case LyXTabular::TOGGLE_LINE_BOTTOM:
843 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
846 case LyXTabular::M_TOGGLE_LINE_LEFT:
848 case LyXTabular::TOGGLE_LINE_LEFT:
849 status.setOnOff(tabular.leftLine(cur.idx(), flag));
852 case LyXTabular::M_TOGGLE_LINE_RIGHT:
854 case LyXTabular::TOGGLE_LINE_RIGHT:
855 status.setOnOff(tabular.rightLine(cur.idx(), flag));
858 case LyXTabular::M_ALIGN_LEFT:
860 case LyXTabular::ALIGN_LEFT:
861 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
864 case LyXTabular::M_ALIGN_RIGHT:
866 case LyXTabular::ALIGN_RIGHT:
867 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
870 case LyXTabular::M_ALIGN_CENTER:
872 case LyXTabular::ALIGN_CENTER:
873 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
876 case LyXTabular::ALIGN_BLOCK:
877 status.enabled(!tabular.getPWidth(cur.idx()).zero());
878 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
881 case LyXTabular::M_VALIGN_TOP:
883 case LyXTabular::VALIGN_TOP:
885 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
888 case LyXTabular::M_VALIGN_BOTTOM:
890 case LyXTabular::VALIGN_BOTTOM:
892 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
895 case LyXTabular::M_VALIGN_MIDDLE:
897 case LyXTabular::VALIGN_MIDDLE:
899 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
902 case LyXTabular::SET_LONGTABULAR:
903 status.setOnOff(tabular.isLongTabular());
906 case LyXTabular::UNSET_LONGTABULAR:
907 status.setOnOff(!tabular.isLongTabular());
910 case LyXTabular::SET_ROTATE_TABULAR:
911 status.setOnOff(tabular.getRotateTabular());
914 case LyXTabular::UNSET_ROTATE_TABULAR:
915 status.setOnOff(!tabular.getRotateTabular());
918 case LyXTabular::SET_ROTATE_CELL:
919 status.setOnOff(tabular.getRotateCell(cur.idx()));
922 case LyXTabular::UNSET_ROTATE_CELL:
923 status.setOnOff(!tabular.getRotateCell(cur.idx()));
926 case LyXTabular::SET_USEBOX:
927 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
930 case LyXTabular::SET_LTFIRSTHEAD:
931 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
934 case LyXTabular::SET_LTHEAD:
935 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
938 case LyXTabular::SET_LTFOOT:
939 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
942 case LyXTabular::SET_LTLASTFOOT:
943 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
946 case LyXTabular::SET_LTNEWPAGE:
947 status.setOnOff(tabular.getLTNewPage(sel_row_start));
952 status.enabled(false);
958 // disable these with multiple cells selected
959 case LFUN_INSERT_CHARSTYLE:
960 case LFUN_INSET_FLOAT:
961 case LFUN_INSET_WIDE_FLOAT:
962 case LFUN_INSET_FOOTNOTE:
963 case LFUN_INSET_MARGINAL:
964 case LFUN_INSERT_MATH:
966 case LFUN_MATH_MUTATE:
967 case LFUN_MATH_DISPLAY:
968 case LFUN_INSERT_NOTE:
969 case LFUN_INSET_OPTARG:
970 case LFUN_INSERT_BOX:
971 case LFUN_INSERT_BRANCH:
972 case LFUN_INSET_WRAP:
973 case LFUN_INSET_ERT: {
974 if (tablemode(cur)) {
975 status.enabled(false);
981 // we try to handle this event in the insets dispatch function.
982 return cell(cur.idx())->getStatus(cur, cmd, status);
987 int InsetTabular::latex(Buffer const & buf, ostream & os,
988 OutputParams const & runparams) const
990 return tabular.latex(buf, os, runparams);
994 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
995 OutputParams const & runparams) const
997 int const dp = runparams.linelen ? runparams.depth : 0;
998 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1002 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1003 OutputParams const & runparams) const
1005 return tabular.linuxdoc(buf,os, runparams);
1009 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1010 OutputParams const & runparams) const
1013 InsetBase * master = 0;
1015 #ifdef WITH_WARNINGS
1016 #warning Why not pass a proper DocIterator here?
1019 // if the table is inside a float it doesn't need the informaltable
1020 // wrapper. Search for it.
1021 for (master = owner(); master; master = master->owner())
1022 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1027 os << "<informaltable>";
1030 ret += tabular.docbook(buf, os, runparams);
1032 os << "</informaltable>";
1039 void InsetTabular::validate(LaTeXFeatures & features) const
1041 tabular.validate(features);
1045 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1047 return tabular.getCellInset(idx);
1051 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1053 return tabular.getCellInset(idx);
1057 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1059 cell(sl.idx())->getCursorPos(sl, x, y);
1061 // y offset correction
1062 int const row = tabular.row_of_cell(sl.idx());
1063 for (int i = 0; i <= row; ++i) {
1065 y += tabular.getAscentOfRow(i);
1066 y += tabular.getAdditionalHeight(i);
1069 y += tabular.getDescentOfRow(i);
1072 // x offset correction
1073 int const col = tabular.column_of_cell(sl.idx());
1074 int idx = tabular.getCellNumber(row, 0);
1075 for (int j = 0; j < col; ++j) {
1076 if (tabular.isPartOfMultiColumn(row, j))
1078 x += tabular.getWidthOfColumn(idx);
1081 x += tabular.getBeginningOfTextInCell(idx);
1082 x += ADD_TO_TABULAR_WIDTH;
1090 // Manhattan distance to nearest corner
1091 int dist(InsetOld const & inset, int x, int y)
1095 Point o = theCoords.getInsets().xy(&inset);
1096 int const xo = o.x_;
1097 int const yo = o.y_;
1101 else if (x > xo + inset.width())
1102 xx = x - xo - inset.width();
1104 if (y < yo - inset.ascent())
1105 yy = yo - inset.ascent() - y;
1106 else if (y > yo + inset.descent())
1107 yy = y - yo - inset.descent();
1109 lyxerr << " xo_=" << xo << " yo_=" << yo
1110 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1111 << " descent=" << inset.descent()
1112 << " dist=" << xx + yy << endl;
1120 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
1122 lyxerr << "# InsetTabular::setPos() x=" << x << " y=" << y << endl;
1123 idx_type idx_min = 0;
1124 int dist_min = std::numeric_limits<int>::max();
1125 for (idx_type i = 0; i < nargs(); ++i) {
1126 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1127 int d = dist(*tabular.getCellInset(i), x, y);
1134 cur.idx() = idx_min;
1135 //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
1137 return cell(cur.idx())->text_.editXY(cur, x, y);
1141 int InsetTabular::getCellXPos(idx_type const cell) const
1145 for (; !tabular.isFirstCellInRow(c); --c)
1147 int lx = tabular.getWidthOfColumn(cell);
1148 for (; c < cell; ++c)
1149 lx += tabular.getWidthOfColumn(c);
1151 return lx - tabular.getWidthOfColumn(cell);
1155 void InsetTabular::resetPos(LCursor & cur) const
1157 BufferView & bv = cur.bv();
1158 // col_type const actcol = tabular.column_of_cell(cur.idx());
1159 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1160 // int const new_x = getCellXPos(cur.idx()) + offset;
1161 // int const old_x = cursorx_;
1162 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1163 // cursorx_ = new_x;
1164 // cursor.x(getCellXPos(cur.idx()) + offset);
1165 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1166 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1168 // scroll(bv, 0.0F);
1169 // } else if (cursorx_ - offset > 20 &&
1170 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1171 // scroll(bv, - col_width - 20);
1172 // } else if (cursorx_ - offset < 20) {
1173 // scroll(bv, 20 - cursorx_ + offset);
1174 // } else if (scroll() && xo() > 20 &&
1175 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1176 // scroll(bv, old_x - cursorx_);
1179 if (&cur.inset() != this) {
1183 int const X2 = bv.workWidth();
1184 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1185 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1186 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1189 scroll(bv, X1 + 20 - x1);
1191 scroll(bv, X2 - 20 - x2);
1196 InsetTabularMailer(*this).updateDialog(&bv);
1200 void InsetTabular::moveNextCell(LCursor & cur)
1202 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1203 if (isRightToLeft(cur)) {
1204 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1205 if (tabular.isFirstCellInRow(cur.idx())) {
1206 row_type const row = tabular.row_of_cell(cur.idx());
1207 if (row == tabular.rows() - 1)
1209 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1216 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1217 if (tabular.isLastCell(cur.idx()))
1223 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1228 void InsetTabular::movePrevCell(LCursor & cur)
1230 if (isRightToLeft(cur)) {
1231 if (tabular.isLastCellInRow(cur.idx())) {
1232 row_type const row = tabular.row_of_cell(cur.idx());
1235 cur.idx() = tabular.getFirstCellInRow(row);
1236 cur.idx() = tabular.getCellAbove(cur.idx());
1238 if (tabular.isLastCell(cur.idx()))
1243 if (cur.idx() == 0) // first cell
1247 cur.pit() = cur.lastpit();
1248 cur.pos() = cur.lastpos();
1253 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1255 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1258 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1259 string const tmp = tabularFeature[i].feature;
1261 if (tmp == what.substr(0, tmp.length())) {
1262 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1263 //tabularFeatures[i].feature.length())) {
1264 action = tabularFeature[i].action;
1268 if (action == LyXTabular::LAST_ACTION)
1272 ltrim(what.substr(tabularFeature[i].feature.length()));
1273 tabularFeatures(cur, action, val);
1280 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1281 string const & special, bool & flag)
1283 if (special == "dl_above") {
1286 } else if (special == "dl_below") {
1287 ltt.bottomDL = flag;
1289 } else if (special == "empty") {
1301 void InsetTabular::tabularFeatures(LCursor & cur,
1302 LyXTabular::Feature feature, string const & value)
1304 BufferView & bv = cur.bv();
1305 col_type sel_col_start;
1306 col_type sel_col_end;
1307 row_type sel_row_start;
1308 row_type sel_row_end;
1309 bool setLines = false;
1310 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1311 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1315 case LyXTabular::M_ALIGN_LEFT:
1316 case LyXTabular::ALIGN_LEFT:
1317 setAlign = LYX_ALIGN_LEFT;
1320 case LyXTabular::M_ALIGN_RIGHT:
1321 case LyXTabular::ALIGN_RIGHT:
1322 setAlign = LYX_ALIGN_RIGHT;
1325 case LyXTabular::M_ALIGN_CENTER:
1326 case LyXTabular::ALIGN_CENTER:
1327 setAlign = LYX_ALIGN_CENTER;
1330 case LyXTabular::ALIGN_BLOCK:
1331 setAlign = LYX_ALIGN_BLOCK;
1334 case LyXTabular::M_VALIGN_TOP:
1335 case LyXTabular::VALIGN_TOP:
1336 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1339 case LyXTabular::M_VALIGN_BOTTOM:
1340 case LyXTabular::VALIGN_BOTTOM:
1341 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1344 case LyXTabular::M_VALIGN_MIDDLE:
1345 case LyXTabular::VALIGN_MIDDLE:
1346 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1353 recordUndo(cur, Undo::ATOMIC);
1355 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1356 row_type const row = tabular.row_of_cell(cur.idx());
1357 col_type const column = tabular.column_of_cell(cur.idx());
1359 LyXTabular::ltType ltt;
1363 case LyXTabular::SET_PWIDTH: {
1364 LyXLength const len(value);
1365 tabular.setColumnPWidth(cur.idx(), len);
1367 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1368 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1369 else if (!len.zero()
1370 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1371 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1375 case LyXTabular::SET_MPWIDTH:
1376 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1379 case LyXTabular::SET_SPECIAL_COLUMN:
1380 case LyXTabular::SET_SPECIAL_MULTI:
1381 tabular.setAlignSpecial(cur.idx(),value,feature);
1384 case LyXTabular::APPEND_ROW:
1385 // append the row into the tabular
1386 tabular.appendRow(bv.buffer()->params(), cur.idx());
1389 case LyXTabular::APPEND_COLUMN:
1390 // append the column into the tabular
1391 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1394 case LyXTabular::DELETE_ROW:
1395 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1396 tabular.deleteRow(sel_row_start);
1397 if (sel_row_start >= tabular.rows())
1399 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1402 cur.selection() = false;
1405 case LyXTabular::DELETE_COLUMN:
1406 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1407 tabular.deleteColumn(sel_col_start);
1408 if (sel_col_start >= tabular.columns())
1410 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1413 cur.selection() = false;
1416 case LyXTabular::M_TOGGLE_LINE_TOP:
1418 case LyXTabular::TOGGLE_LINE_TOP: {
1419 bool lineSet = !tabular.topLine(cur.idx(), flag);
1420 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1421 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1423 tabular.getCellNumber(i, j),
1428 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1430 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1431 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1432 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1433 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1434 tabular.setBottomLine(
1435 tabular.getCellNumber(i, j),
1441 case LyXTabular::M_TOGGLE_LINE_LEFT:
1443 case LyXTabular::TOGGLE_LINE_LEFT: {
1444 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1445 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1446 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1447 tabular.setLeftLine(
1448 tabular.getCellNumber(i,j),
1454 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1456 case LyXTabular::TOGGLE_LINE_RIGHT: {
1457 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1458 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1459 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1460 tabular.setRightLine(
1461 tabular.getCellNumber(i,j),
1467 case LyXTabular::M_ALIGN_LEFT:
1468 case LyXTabular::M_ALIGN_RIGHT:
1469 case LyXTabular::M_ALIGN_CENTER:
1471 case LyXTabular::ALIGN_LEFT:
1472 case LyXTabular::ALIGN_RIGHT:
1473 case LyXTabular::ALIGN_CENTER:
1474 case LyXTabular::ALIGN_BLOCK:
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)
1477 tabular.setAlignment(
1478 tabular.getCellNumber(i, j),
1483 case LyXTabular::M_VALIGN_TOP:
1484 case LyXTabular::M_VALIGN_BOTTOM:
1485 case LyXTabular::M_VALIGN_MIDDLE:
1487 case LyXTabular::VALIGN_TOP:
1488 case LyXTabular::VALIGN_BOTTOM:
1489 case LyXTabular::VALIGN_MIDDLE:
1490 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1491 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1492 tabular.setVAlignment(
1493 tabular.getCellNumber(i, j),
1497 case LyXTabular::MULTICOLUMN: {
1498 if (sel_row_start != sel_row_end) {
1499 #ifdef WITH_WARNINGS
1500 #warning Need I say it ? This is horrible.
1502 Alert::error(_("Error setting multicolumn"),
1503 _("You cannot set multicolumn vertically."));
1506 if (!cur.selection()) {
1507 // just multicol for one single cell
1508 // check whether we are completely in a multicol
1509 if (tabular.isMultiColumn(cur.idx()))
1510 tabular.unsetMultiColumn(cur.idx());
1512 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1515 // we have a selection so this means we just add all this
1516 // cells to form a multicolumn cell
1517 idx_type const s_start = cur.selBegin().idx();
1518 idx_type const s_end = cur.selEnd().idx();
1519 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1520 cur.idx() = s_start;
1523 cur.selection() = false;
1527 case LyXTabular::SET_ALL_LINES:
1529 case LyXTabular::UNSET_ALL_LINES:
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.setAllLines(
1533 tabular.getCellNumber(i,j), setLines);
1536 case LyXTabular::SET_LONGTABULAR:
1537 tabular.setLongTabular(true);
1540 case LyXTabular::UNSET_LONGTABULAR:
1541 tabular.setLongTabular(false);
1544 case LyXTabular::SET_ROTATE_TABULAR:
1545 tabular.setRotateTabular(true);
1548 case LyXTabular::UNSET_ROTATE_TABULAR:
1549 tabular.setRotateTabular(false);
1552 case LyXTabular::SET_ROTATE_CELL:
1553 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1554 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1555 tabular.setRotateCell(
1556 tabular.getCellNumber(i, j), true);
1559 case LyXTabular::UNSET_ROTATE_CELL:
1560 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1561 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1562 tabular.setRotateCell(
1563 tabular.getCellNumber(i, j), false);
1566 case LyXTabular::SET_USEBOX: {
1567 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1568 if (val == tabular.getUsebox(cur.idx()))
1569 val = LyXTabular::BOX_NONE;
1570 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1571 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1572 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1576 case LyXTabular::UNSET_LTFIRSTHEAD:
1578 case LyXTabular::SET_LTFIRSTHEAD:
1579 tabular.getRowOfLTFirstHead(row, ltt);
1580 checkLongtableSpecial(ltt, value, flag);
1581 tabular.setLTHead(row, flag, ltt, true);
1584 case LyXTabular::UNSET_LTHEAD:
1586 case LyXTabular::SET_LTHEAD:
1587 tabular.getRowOfLTHead(row, ltt);
1588 checkLongtableSpecial(ltt, value, flag);
1589 tabular.setLTHead(row, flag, ltt, false);
1592 case LyXTabular::UNSET_LTFOOT:
1594 case LyXTabular::SET_LTFOOT:
1595 tabular.getRowOfLTFoot(row, ltt);
1596 checkLongtableSpecial(ltt, value, flag);
1597 tabular.setLTFoot(row, flag, ltt, false);
1600 case LyXTabular::UNSET_LTLASTFOOT:
1602 case LyXTabular::SET_LTLASTFOOT:
1603 tabular.getRowOfLTLastFoot(row, ltt);
1604 checkLongtableSpecial(ltt, value, flag);
1605 tabular.setLTFoot(row, flag, ltt, true);
1608 case LyXTabular::SET_LTNEWPAGE:
1609 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1612 // dummy stuff just to avoid warnings
1613 case LyXTabular::LAST_ACTION:
1617 InsetTabularMailer(*this).updateDialog(&bv);
1621 bool InsetTabular::showInsetDialog(BufferView * bv) const
1623 InsetTabularMailer(*this).showDialog(bv);
1628 void InsetTabular::openLayoutDialog(BufferView * bv) const
1630 InsetTabularMailer(*this).showDialog(bv);
1634 void InsetTabular::getLabelList(Buffer const & buffer,
1635 vector<string> & list) const
1637 tabular.getLabelList(buffer, list);
1641 bool InsetTabular::copySelection(LCursor & cur)
1643 if (!cur.selection())
1648 getSelection(cur, rs, re, cs, ce);
1650 paste_tabular.reset(new LyXTabular(tabular));
1652 for (row_type i = 0; i < rs; ++i)
1653 paste_tabular->deleteRow(0);
1655 row_type const rows = re - rs + 1;
1656 while (paste_tabular->rows() > rows)
1657 paste_tabular->deleteRow(rows);
1659 paste_tabular->setTopLine(0, true, true);
1660 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1663 for (col_type i = 0; i < cs; ++i)
1664 paste_tabular->deleteColumn(0);
1666 col_type const columns = ce - cs + 1;
1667 while (paste_tabular->columns() > columns)
1668 paste_tabular->deleteColumn(columns);
1670 paste_tabular->setLeftLine(0, true, true);
1671 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1675 OutputParams const runparams;
1676 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1677 cur.bv().stuffClipboard(os.str());
1682 bool InsetTabular::pasteSelection(LCursor & cur)
1686 col_type const actcol = tabular.column_of_cell(cur.idx());
1687 row_type const actrow = tabular.row_of_cell(cur.idx());
1688 for (row_type r1 = 0, r2 = actrow;
1689 r1 < paste_tabular->rows() && r2 < tabular.rows();
1691 for (col_type c1 = 0, c2 = actcol;
1692 c1 < paste_tabular->columns() && c2 < tabular.columns();
1694 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1695 tabular.isPartOfMultiColumn(r2, c2))
1697 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1701 if (tabular.isPartOfMultiColumn(r2, c2)) {
1705 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1706 inset = paste_tabular->getCellInset(r1, c1);
1714 void InsetTabular::cutSelection(LCursor & cur)
1716 if (!cur.selection())
1719 bool const track = cur.buffer().params().tracking_changes;
1722 getSelection(cur, rs, re, cs, ce);
1723 for (row_type i = rs; i <= re; ++i)
1724 for (col_type j = cs; j <= ce; ++j)
1725 cell(tabular.getCellNumber(i, j))->clear(track);
1727 // cursor position might be invalid now
1728 cur.pos() = cur.lastpos();
1729 cur.clearSelection();
1733 bool InsetTabular::isRightToLeft(LCursor & cur) const
1735 BOOST_ASSERT(cur.depth() > 1);
1736 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1737 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1738 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1739 parentpos).language()->RightToLeft();
1743 void InsetTabular::getSelection(LCursor & cur,
1744 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1746 CursorSlice const & beg = cur.selBegin();
1747 CursorSlice const & end = cur.selEnd();
1748 cs = tabular.column_of_cell(beg.idx());
1749 ce = tabular.column_of_cell(end.idx());
1752 cs = tabular.column_of_cell(end.idx());
1754 ce = tabular.right_column_of_cell(end.idx());
1757 rs = tabular.row_of_cell(beg.idx());
1758 re = tabular.row_of_cell(end.idx());
1764 size_t InsetTabular::nargs() const
1766 return tabular.getNumberOfCells();
1770 LyXText * InsetTabular::getText(int idx) const
1772 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1776 void InsetTabular::markErased()
1778 for (idx_type idx = 0; idx < nargs(); ++idx)
1779 cell(idx)->markErased();
1783 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1786 idx_type const cell = tabular.getCellFromInset(in);
1787 // FIXME: getCellFromInset() returns now always a valid cell, so
1788 // the stuff below can be deleted, and instead we have:
1789 return tabular.getPWidth(cell).zero();
1792 return tabular.getPWidth(cell).zero();
1794 // this is a workaround for a crash (New, Insert->Tabular,
1795 // Insert->FootNote)
1799 // well we didn't obviously find it so maybe our owner knows more
1800 BOOST_ASSERT(owner());
1801 return owner()->forceDefaultParagraphs(in);
1807 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1810 if (buf.length() <= 0)
1815 col_type maxCols = 1;
1816 string::size_type const len = buf.length();
1817 string::size_type p = 0;
1819 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1827 maxCols = max(cols, maxCols);
1833 maxCols = max(cols, maxCols);
1834 LyXTabular * loctab;
1839 paste_tabular.reset(
1840 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1841 loctab = paste_tabular.get();
1845 cell = bv.cursor().idx();
1846 ocol = tabular.column_of_cell(cell);
1847 row = tabular.row_of_cell(cell);
1850 string::size_type op = 0;
1851 idx_type const cells = loctab->getNumberOfCells();
1854 rows = loctab->rows();
1855 col_type const columns = loctab->columns();
1857 while (cell < cells && p < len && row < rows &&
1858 (p = buf.find_first_of("\t\n", p)) != string::npos)
1864 // we can only set this if we are not too far right
1865 if (cols < columns) {
1866 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1867 Paragraph & par = inset->text_.getPar(0);
1868 LyXFont const font = inset->text_.getFont(par, 0);
1869 inset->setText(buf.substr(op, p - op), font);
1875 // we can only set this if we are not too far right
1876 if (cols < columns) {
1877 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1878 Paragraph & par = inset->text_.getPar(0);
1879 LyXFont const font = inset->text_.getFont(par, 0);
1880 inset->setText(buf.substr(op, p - op), font);
1885 cell = loctab->getCellNumber(row, cols);
1891 // check for the last cell if there is no trailing '\n'
1892 if (cell < cells && op < len) {
1893 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1894 Paragraph & par = inset->text_.getPar(0);
1895 LyXFont const font = inset->text_.getFont(par, 0);
1896 inset->setText(buf.substr(op, len - op), font);
1902 void InsetTabular::addPreview(PreviewLoader & loader) const
1904 row_type const rows = tabular.rows();
1905 col_type const columns = tabular.columns();
1906 for (row_type i = 0; i < rows; ++i) {
1907 for (col_type j = 0; j < columns; ++j)
1908 tabular.getCellInset(i, j)->addPreview(loader);
1913 bool InsetTabular::tablemode(LCursor & cur) const
1915 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1922 string const InsetTabularMailer::name_("tabular");
1924 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1925 : inset_(const_cast<InsetTabular &>(inset))
1929 string const InsetTabularMailer::inset2string(Buffer const &) const
1931 return params2string(inset_);
1935 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1937 istringstream data(in);
1939 lex.setStream(data);
1946 if (!lex || token != name_)
1947 return print_mailer_error("InsetTabularMailer", in, 1,
1950 // This is part of the inset proper that is usually swallowed
1951 // by Buffer::readInset
1953 if (!lex || token != "Tabular")
1954 return print_mailer_error("InsetTabularMailer", in, 2,
1957 Buffer const & buffer = inset.buffer();
1958 inset.read(buffer, lex);
1962 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1965 data << name_ << ' ';
1966 inset.write(inset.buffer(), data);
1967 data << "\\end_inset\n";