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 y -= tabular.getAscentOfRow(0);
335 for (row_type j = 0; j < tabular.rows(); ++j) {
336 int const a = tabular.getAscentOfRow(j);
337 int const h = a + tabular.getDescentOfRow(j);
339 y += tabular.getAdditionalHeight(j);
340 for (col_type i = 0; i < tabular.columns(); ++i) {
341 if (tabular.isPartOfMultiColumn(j, i))
343 idx_type const cell =
344 tabular.getCellNumber(j, i);
345 int const w = tabular.getWidthOfColumn(cell);
346 if (i >= cs && i <= ce && j >= rs && j <= re)
347 pi.pain.fillRectangle(xx, y, w, h,
356 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
361 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
362 row_type row, idx_type cell) const
364 int x2 = x + tabular.getWidthOfColumn(cell);
367 if (!tabular.topAlreadyDrawn(cell)) {
368 on_off = !tabular.topLine(cell);
369 pain.line(x, y - tabular.getAscentOfRow(row),
370 x2, y - tabular.getAscentOfRow(row),
371 on_off ? LColor::tabularonoffline : LColor::tabularline,
372 on_off ? Painter::line_onoffdash : Painter::line_solid);
374 on_off = !tabular.bottomLine(cell);
375 pain.line(x, y + tabular.getDescentOfRow(row),
376 x2, y + tabular.getDescentOfRow(row),
377 on_off ? LColor::tabularonoffline : LColor::tabularline,
378 on_off ? Painter::line_onoffdash : Painter::line_solid);
379 if (!tabular.leftAlreadyDrawn(cell)) {
380 on_off = !tabular.leftLine(cell);
381 pain.line(x, y - tabular.getAscentOfRow(row),
382 x, y + tabular.getDescentOfRow(row),
383 on_off ? LColor::tabularonoffline : LColor::tabularline,
384 on_off ? Painter::line_onoffdash : Painter::line_solid);
386 on_off = !tabular.rightLine(cell);
387 pain.line(x2 - tabular.getAdditionalWidth(cell),
388 y - tabular.getAscentOfRow(row),
389 x2 - tabular.getAdditionalWidth(cell),
390 y + tabular.getDescentOfRow(row),
391 on_off ? LColor::tabularonoffline : LColor::tabularline,
392 on_off ? Painter::line_onoffdash : Painter::line_solid);
396 string const InsetTabular::editMessage() const
398 return _("Opened table");
402 void InsetTabular::edit(LCursor & cur, bool left)
404 lyxerr << "InsetTabular::edit: " << this << endl;
406 cur.selection() = false;
409 if (isRightToLeft(cur))
410 cur.idx() = tabular.getLastCellInRow(0);
416 if (isRightToLeft(cur))
417 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
419 cur.idx() = tabular.getNumberOfCells() - 1;
421 cur.pos() = cur.lastpos(); // FIXME crude guess
423 // this accesses the position cache before it is initialized
425 //cur.bv().fitCursor();
429 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
431 //lyxerr << "InsetTabular::editXY: " << this << endl;
432 cur.selection() = false;
433 cur.push(const_cast<InsetTabular&>(*this));
434 return setPos(cur, x, y);
435 //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
439 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
441 lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
442 //lyxerr << " cur:\n" << cur << endl;
443 CursorSlice sl = cur.top();
444 LCursor & bvcur = cur.bv().cursor();
446 switch (cmd.action) {
448 case LFUN_MOUSE_PRESS:
449 lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
451 if (cmd.button() == mouse_button::button1) {
452 cur.selection() = false;
453 setPos(cur, cmd.x, cmd.y);
459 if (cmd.button() == mouse_button::button2) {
460 // FIXME: pasting multiple cells (with insettabular's own
461 // LFUN_PASTESELECTION still does not work! (jspitzm)
462 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
463 cell(cur.idx())->dispatch(cur, cmd);
467 // we'll pop up the table dialog on release
468 if (cmd.button() == mouse_button::button3)
472 case LFUN_MOUSE_MOTION:
473 lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
474 if (cmd.button() == mouse_button::button1) {
475 // only accept motions to places not deeper nested than the real anchor
476 if (bvcur.anchor_.hasPart(cur)) {
477 setPos(cur, cmd.x, cmd.y);
478 bvcur.setCursor(cur);
479 bvcur.selection() = true;
485 case LFUN_MOUSE_RELEASE:
486 lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
487 if (cmd.button() == mouse_button::button3)
488 InsetTabularMailer(*this).showDialog(&cur.bv());
491 case LFUN_CELL_BACKWARD:
493 cur.selection() = false;
496 case LFUN_CELL_FORWARD:
498 cur.selection() = false;
501 case LFUN_SCROLL_INSET:
502 if (cmd.argument.empty())
504 if (cmd.argument.find('.') != cmd.argument.npos)
505 scroll(cur.bv(), static_cast<float>(convert<double>(cmd.argument)));
507 scroll(cur.bv(), convert<int>(cmd.argument));
512 cell(cur.idx())->dispatch(cur, cmd);
513 cur.dispatched(); // override the cell's decision
515 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
516 if (sl == cur.top()) {
517 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
525 cell(cur.idx())->dispatch(cur, cmd);
526 cur.dispatched(); // override the cell's decision
528 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
529 if (sl == cur.top()) {
530 cmd = FuncRequest(LFUN_FINISHED_LEFT);
538 cell(cur.idx())->dispatch(cur, cmd);
539 cur.dispatched(); // override the cell's decision
541 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
542 cur.idx() = tabular.getCellBelow(cur.idx());
547 if (sl == cur.top()) {
548 cmd = FuncRequest(LFUN_FINISHED_DOWN);
556 cell(cur.idx())->dispatch(cur, cmd);
557 cur.dispatched(); // override the cell's decision
559 if (tabular.row_of_cell(cur.idx()) != 0) {
560 cur.idx() = tabular.getCellAbove(cur.idx());
561 cur.pit() = cur.lastpit();
562 cur.pos() = cur.lastpos();
565 if (sl == cur.top()) {
566 cmd = FuncRequest(LFUN_FINISHED_UP);
573 // //if (hasSelection())
574 // // cur.selection() = false;
575 // col_type const col = tabular.column_of_cell(cur.idx());
576 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
577 // if (t < yo() + tabular.getHeightOfTabular()) {
578 // cur.bv().scrollDocView(t);
579 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
581 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
589 // case LFUN_PRIOR: {
590 // //if (hasSelection())
591 // // cur.selection() = false;
592 // col_type const col = tabular.column_of_cell(cur.idx());
593 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
595 // cur.bv().scrollDocView(t);
599 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
603 // cur.par() = cur.lastpar();
604 // cur.pos() = cur.lastpos();
609 case LFUN_LAYOUT_TABULAR:
610 InsetTabularMailer(*this).showDialog(&cur.bv());
613 case LFUN_INSET_DIALOG_UPDATE:
614 InsetTabularMailer(*this).updateDialog(&cur.bv());
617 case LFUN_TABULAR_FEATURE:
618 if (!tabularFeatures(cur, cmd.argument))
622 // insert file functions
623 case LFUN_FILE_INSERT_ASCII_PARA:
624 case LFUN_FILE_INSERT_ASCII: {
625 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
626 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
632 if (tablemode(cur)) {
633 if (copySelection(cur)) {
634 recordUndo(cur, Undo::DELETE);
639 cell(cur.idx())->dispatch(cur, cmd);
644 recordUndo(cur, Undo::DELETE);
648 cell(cur.idx())->dispatch(cur, cmd);
652 if (!cur.selection())
654 if (tablemode(cur)) {
658 cell(cur.idx())->dispatch(cur, cmd);
661 case LFUN_PASTESELECTION: {
662 string const clip = cur.bv().getClipboard();
665 if (clip.find('\t') != string::npos) {
668 col_type maxCols = 1;
669 size_t len = clip.length();
670 for (size_t p = 0; p < len; ++p) {
671 p = clip.find_first_of("\t\n", p);
672 if (p == string::npos)
681 maxCols = max(cols, maxCols);
686 maxCols = max(cols, maxCols);
689 new LyXTabular(cur.buffer().params(), rows, maxCols));
691 string::size_type op = 0;
693 idx_type const cells =
694 paste_tabular->getNumberOfCells();
697 for (size_t p = 0; cell < cells && p < len; ++p) {
698 p = clip.find_first_of("\t\n", p);
699 if (p == string::npos || p >= len)
703 paste_tabular->getCellInset(cell)->
704 setText(clip.substr(op, p - op), font);
709 paste_tabular->getCellInset(cell)->
710 setText(clip.substr(op, p - op), font);
711 while (cols++ < maxCols)
718 // check for the last cell if there is no trailing '\n'
719 if (cell < cells && op < len)
720 paste_tabular->getCellInset(cell)->
721 setText(clip.substr(op, len - op), font);
722 } else if (!insertAsciiString(cur.bv(), clip, true)) {
723 // so that the clipboard is used and it goes on
725 // and executes LFUN_PASTESELECTION in insettext!
726 paste_tabular.reset();
732 if (hasPasteBuffer()) {
733 recordUndo(cur, Undo::INSERT);
737 cell(cur.idx())->dispatch(cur, cmd);
748 case LFUN_FREEFONT_APPLY:
749 case LFUN_FREEFONT_UPDATE:
753 case LFUN_CAPITALIZE_WORD:
754 case LFUN_UPCASE_WORD:
755 case LFUN_LOWCASE_WORD:
756 case LFUN_TRANSPOSE_CHARS:
757 if (tablemode(cur)) {
760 getSelection(cur, rs, re, cs, ce);
761 for (row_type i = rs; i <= re; ++i)
762 for (col_type j = cs; j <= ce; ++j) {
763 // cursor follows cell:
764 cur.idx() = tabular.getCellNumber(i, j);
765 // select this cell only:
768 cur.pos() = cur.top().lastpos();
771 cell(cur.idx())->dispatch(cur, cmd);
773 // Restore original selection
774 cur.idx() = tabular.getCellNumber(rs, cs);
777 cur.idx() = tabular.getCellNumber(re, ce);
778 cur.pos() = cur.top().lastpos();
783 cell(cur.idx())->dispatch(cur, cmd);
787 // we try to handle this event in the insets dispatch function.
788 cell(cur.idx())->dispatch(cur, cmd);
792 InsetTabularMailer(*this).updateDialog(&cur.bv());
796 // function sets an object as defined in func_status.h:
797 // states OK, Unknown, Disabled, On, Off.
798 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
799 FuncStatus & status) const
801 switch (cmd.action) {
802 case LFUN_TABULAR_FEATURE: {
803 int action = LyXTabular::LAST_ACTION;
805 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
806 string const tmp = tabularFeature[i].feature;
807 if (tmp == cmd.argument.substr(0, tmp.length())) {
808 action = tabularFeature[i].action;
812 if (action == LyXTabular::LAST_ACTION) {
814 status.unknown(true);
818 string const argument
819 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
821 row_type sel_row_start = 0;
822 row_type sel_row_end = 0;
824 LyXTabular::ltType dummyltt;
827 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
830 case LyXTabular::SET_PWIDTH:
831 case LyXTabular::SET_MPWIDTH:
832 case LyXTabular::SET_SPECIAL_COLUMN:
833 case LyXTabular::SET_SPECIAL_MULTI:
834 case LyXTabular::APPEND_ROW:
835 case LyXTabular::APPEND_COLUMN:
836 case LyXTabular::DELETE_ROW:
837 case LyXTabular::DELETE_COLUMN:
838 case LyXTabular::SET_ALL_LINES:
839 case LyXTabular::UNSET_ALL_LINES:
843 case LyXTabular::MULTICOLUMN:
844 status.setOnOff(tabular.isMultiColumn(cur.idx()));
847 case LyXTabular::M_TOGGLE_LINE_TOP:
849 case LyXTabular::TOGGLE_LINE_TOP:
850 status.setOnOff(tabular.topLine(cur.idx(), flag));
853 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
855 case LyXTabular::TOGGLE_LINE_BOTTOM:
856 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
859 case LyXTabular::M_TOGGLE_LINE_LEFT:
861 case LyXTabular::TOGGLE_LINE_LEFT:
862 status.setOnOff(tabular.leftLine(cur.idx(), flag));
865 case LyXTabular::M_TOGGLE_LINE_RIGHT:
867 case LyXTabular::TOGGLE_LINE_RIGHT:
868 status.setOnOff(tabular.rightLine(cur.idx(), flag));
871 case LyXTabular::M_ALIGN_LEFT:
873 case LyXTabular::ALIGN_LEFT:
874 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
877 case LyXTabular::M_ALIGN_RIGHT:
879 case LyXTabular::ALIGN_RIGHT:
880 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
883 case LyXTabular::M_ALIGN_CENTER:
885 case LyXTabular::ALIGN_CENTER:
886 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
889 case LyXTabular::ALIGN_BLOCK:
890 status.enabled(!tabular.getPWidth(cur.idx()).zero());
891 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
894 case LyXTabular::M_VALIGN_TOP:
896 case LyXTabular::VALIGN_TOP:
898 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
901 case LyXTabular::M_VALIGN_BOTTOM:
903 case LyXTabular::VALIGN_BOTTOM:
905 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
908 case LyXTabular::M_VALIGN_MIDDLE:
910 case LyXTabular::VALIGN_MIDDLE:
912 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
915 case LyXTabular::SET_LONGTABULAR:
916 status.setOnOff(tabular.isLongTabular());
919 case LyXTabular::UNSET_LONGTABULAR:
920 status.setOnOff(!tabular.isLongTabular());
923 case LyXTabular::SET_ROTATE_TABULAR:
924 status.setOnOff(tabular.getRotateTabular());
927 case LyXTabular::UNSET_ROTATE_TABULAR:
928 status.setOnOff(!tabular.getRotateTabular());
931 case LyXTabular::SET_ROTATE_CELL:
932 status.setOnOff(tabular.getRotateCell(cur.idx()));
935 case LyXTabular::UNSET_ROTATE_CELL:
936 status.setOnOff(!tabular.getRotateCell(cur.idx()));
939 case LyXTabular::SET_USEBOX:
940 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
943 case LyXTabular::SET_LTFIRSTHEAD:
944 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
947 case LyXTabular::SET_LTHEAD:
948 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
951 case LyXTabular::SET_LTFOOT:
952 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
955 case LyXTabular::SET_LTLASTFOOT:
956 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
959 case LyXTabular::SET_LTNEWPAGE:
960 status.setOnOff(tabular.getLTNewPage(sel_row_start));
965 status.enabled(false);
971 // disable these with multiple cells selected
972 case LFUN_INSERT_CHARSTYLE:
973 case LFUN_INSET_FLOAT:
974 case LFUN_INSET_WIDE_FLOAT:
975 case LFUN_INSET_FOOTNOTE:
976 case LFUN_INSET_MARGINAL:
977 case LFUN_INSERT_MATH:
979 case LFUN_MATH_MUTATE:
980 case LFUN_MATH_DISPLAY:
981 case LFUN_INSERT_NOTE:
982 case LFUN_INSET_OPTARG:
983 case LFUN_INSERT_BOX:
984 case LFUN_INSERT_BRANCH:
985 case LFUN_INSET_WRAP:
986 case LFUN_INSET_ERT: {
987 if (tablemode(cur)) {
988 status.enabled(false);
994 // we try to handle this event in the insets dispatch function.
995 return cell(cur.idx())->getStatus(cur, cmd, status);
1000 int InsetTabular::latex(Buffer const & buf, ostream & os,
1001 OutputParams const & runparams) const
1003 return tabular.latex(buf, os, runparams);
1007 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1008 OutputParams const & runparams) const
1010 int const dp = runparams.linelen ? runparams.depth : 0;
1011 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1015 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1016 OutputParams const & runparams) const
1018 return tabular.linuxdoc(buf,os, runparams);
1022 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1023 OutputParams const & runparams) const
1026 InsetBase * master = 0;
1028 #ifdef WITH_WARNINGS
1029 #warning Why not pass a proper DocIterator here?
1032 // if the table is inside a float it doesn't need the informaltable
1033 // wrapper. Search for it.
1034 for (master = owner(); master; master = master->owner())
1035 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1040 os << "<informaltable>";
1043 ret += tabular.docbook(buf, os, runparams);
1045 os << "</informaltable>";
1052 void InsetTabular::validate(LaTeXFeatures & features) const
1054 tabular.validate(features);
1058 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1060 return tabular.getCellInset(idx);
1064 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1066 return tabular.getCellInset(idx);
1070 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1072 cell(sl.idx())->getCursorPos(sl, x, y);
1074 // y offset correction
1075 int const row = tabular.row_of_cell(sl.idx());
1076 for (int i = 0; i <= row; ++i) {
1078 y += tabular.getAscentOfRow(i);
1079 y += tabular.getAdditionalHeight(i);
1082 y += tabular.getDescentOfRow(i);
1085 // x offset correction
1086 int const col = tabular.column_of_cell(sl.idx());
1087 int idx = tabular.getCellNumber(row, 0);
1088 for (int j = 0; j < col; ++j) {
1089 if (tabular.isPartOfMultiColumn(row, j))
1091 x += tabular.getWidthOfColumn(idx);
1094 x += tabular.getBeginningOfTextInCell(idx);
1095 x += ADD_TO_TABULAR_WIDTH;
1103 // Manhattan distance to nearest corner
1104 int dist(InsetOld const & inset, int x, int y)
1108 Point o = theCoords.getInsets().xy(&inset);
1109 int const xo = o.x_;
1110 int const yo = o.y_;
1114 else if (x > xo + inset.width())
1115 xx = x - xo - inset.width();
1117 if (y < yo - inset.ascent())
1118 yy = yo - inset.ascent() - y;
1119 else if (y > yo + inset.descent())
1120 yy = y - yo - inset.descent();
1122 lyxerr << " xo_=" << xo << " yo_=" << yo
1123 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1124 << " descent=" << inset.descent()
1125 << " dist=" << xx + yy << endl;
1133 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
1135 lyxerr << "# InsetTabular::setPos() x=" << x << " y=" << y << endl;
1136 idx_type idx_min = 0;
1137 int dist_min = std::numeric_limits<int>::max();
1138 for (idx_type i = 0; i < nargs(); ++i) {
1139 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1140 int d = dist(*tabular.getCellInset(i), x, y);
1147 cur.idx() = idx_min;
1148 //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
1150 return cell(cur.idx())->text_.editXY(cur, x, y);
1154 int InsetTabular::getCellXPos(idx_type const cell) const
1158 for (; !tabular.isFirstCellInRow(c); --c)
1160 int lx = tabular.getWidthOfColumn(cell);
1161 for (; c < cell; ++c)
1162 lx += tabular.getWidthOfColumn(c);
1164 return lx - tabular.getWidthOfColumn(cell);
1168 void InsetTabular::resetPos(LCursor & cur) const
1170 BufferView & bv = cur.bv();
1171 // col_type const actcol = tabular.column_of_cell(cur.idx());
1172 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1173 // int const new_x = getCellXPos(cur.idx()) + offset;
1174 // int const old_x = cursorx_;
1175 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1176 // cursorx_ = new_x;
1177 // cursor.x(getCellXPos(cur.idx()) + offset);
1178 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1179 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1181 // scroll(bv, 0.0F);
1182 // } else if (cursorx_ - offset > 20 &&
1183 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1184 // scroll(bv, - col_width - 20);
1185 // } else if (cursorx_ - offset < 20) {
1186 // scroll(bv, 20 - cursorx_ + offset);
1187 // } else if (scroll() && xo() > 20 &&
1188 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1189 // scroll(bv, old_x - cursorx_);
1192 if (&cur.inset() != this) {
1196 int const X2 = bv.workWidth();
1197 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1198 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1199 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1202 scroll(bv, X1 + 20 - x1);
1204 scroll(bv, X2 - 20 - x2);
1209 InsetTabularMailer(*this).updateDialog(&bv);
1213 void InsetTabular::moveNextCell(LCursor & cur)
1215 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1216 if (isRightToLeft(cur)) {
1217 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1218 if (tabular.isFirstCellInRow(cur.idx())) {
1219 row_type const row = tabular.row_of_cell(cur.idx());
1220 if (row == tabular.rows() - 1)
1222 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1229 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1230 if (tabular.isLastCell(cur.idx()))
1236 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1241 void InsetTabular::movePrevCell(LCursor & cur)
1243 if (isRightToLeft(cur)) {
1244 if (tabular.isLastCellInRow(cur.idx())) {
1245 row_type const row = tabular.row_of_cell(cur.idx());
1248 cur.idx() = tabular.getFirstCellInRow(row);
1249 cur.idx() = tabular.getCellAbove(cur.idx());
1251 if (tabular.isLastCell(cur.idx()))
1256 if (cur.idx() == 0) // first cell
1260 cur.pit() = cur.lastpit();
1261 cur.pos() = cur.lastpos();
1266 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1268 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1271 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1272 string const tmp = tabularFeature[i].feature;
1274 if (tmp == what.substr(0, tmp.length())) {
1275 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1276 //tabularFeatures[i].feature.length())) {
1277 action = tabularFeature[i].action;
1281 if (action == LyXTabular::LAST_ACTION)
1285 ltrim(what.substr(tabularFeature[i].feature.length()));
1286 tabularFeatures(cur, action, val);
1293 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1294 string const & special, bool & flag)
1296 if (special == "dl_above") {
1299 } else if (special == "dl_below") {
1300 ltt.bottomDL = flag;
1302 } else if (special == "empty") {
1314 void InsetTabular::tabularFeatures(LCursor & cur,
1315 LyXTabular::Feature feature, string const & value)
1317 BufferView & bv = cur.bv();
1318 col_type sel_col_start;
1319 col_type sel_col_end;
1320 row_type sel_row_start;
1321 row_type sel_row_end;
1322 bool setLines = false;
1323 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1324 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1328 case LyXTabular::M_ALIGN_LEFT:
1329 case LyXTabular::ALIGN_LEFT:
1330 setAlign = LYX_ALIGN_LEFT;
1333 case LyXTabular::M_ALIGN_RIGHT:
1334 case LyXTabular::ALIGN_RIGHT:
1335 setAlign = LYX_ALIGN_RIGHT;
1338 case LyXTabular::M_ALIGN_CENTER:
1339 case LyXTabular::ALIGN_CENTER:
1340 setAlign = LYX_ALIGN_CENTER;
1343 case LyXTabular::ALIGN_BLOCK:
1344 setAlign = LYX_ALIGN_BLOCK;
1347 case LyXTabular::M_VALIGN_TOP:
1348 case LyXTabular::VALIGN_TOP:
1349 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1352 case LyXTabular::M_VALIGN_BOTTOM:
1353 case LyXTabular::VALIGN_BOTTOM:
1354 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1357 case LyXTabular::M_VALIGN_MIDDLE:
1358 case LyXTabular::VALIGN_MIDDLE:
1359 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1366 recordUndo(cur, Undo::ATOMIC);
1368 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1369 row_type const row = tabular.row_of_cell(cur.idx());
1370 col_type const column = tabular.column_of_cell(cur.idx());
1372 LyXTabular::ltType ltt;
1376 case LyXTabular::SET_PWIDTH: {
1377 LyXLength const len(value);
1378 tabular.setColumnPWidth(cur.idx(), len);
1380 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1381 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1382 else if (!len.zero()
1383 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1384 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1388 case LyXTabular::SET_MPWIDTH:
1389 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1392 case LyXTabular::SET_SPECIAL_COLUMN:
1393 case LyXTabular::SET_SPECIAL_MULTI:
1394 tabular.setAlignSpecial(cur.idx(),value,feature);
1397 case LyXTabular::APPEND_ROW:
1398 // append the row into the tabular
1399 tabular.appendRow(bv.buffer()->params(), cur.idx());
1402 case LyXTabular::APPEND_COLUMN:
1403 // append the column into the tabular
1404 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1407 case LyXTabular::DELETE_ROW:
1408 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1409 tabular.deleteRow(sel_row_start);
1410 if (sel_row_start >= tabular.rows())
1412 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1415 cur.selection() = false;
1418 case LyXTabular::DELETE_COLUMN:
1419 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1420 tabular.deleteColumn(sel_col_start);
1421 if (sel_col_start >= tabular.columns())
1423 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1426 cur.selection() = false;
1429 case LyXTabular::M_TOGGLE_LINE_TOP:
1431 case LyXTabular::TOGGLE_LINE_TOP: {
1432 bool lineSet = !tabular.topLine(cur.idx(), flag);
1433 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1434 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1436 tabular.getCellNumber(i, j),
1441 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1443 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1444 bool lineSet = !tabular.bottomLine(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.setBottomLine(
1448 tabular.getCellNumber(i, j),
1454 case LyXTabular::M_TOGGLE_LINE_LEFT:
1456 case LyXTabular::TOGGLE_LINE_LEFT: {
1457 bool lineSet = !tabular.leftLine(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.setLeftLine(
1461 tabular.getCellNumber(i,j),
1467 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1469 case LyXTabular::TOGGLE_LINE_RIGHT: {
1470 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1471 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1472 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1473 tabular.setRightLine(
1474 tabular.getCellNumber(i,j),
1480 case LyXTabular::M_ALIGN_LEFT:
1481 case LyXTabular::M_ALIGN_RIGHT:
1482 case LyXTabular::M_ALIGN_CENTER:
1484 case LyXTabular::ALIGN_LEFT:
1485 case LyXTabular::ALIGN_RIGHT:
1486 case LyXTabular::ALIGN_CENTER:
1487 case LyXTabular::ALIGN_BLOCK:
1488 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1489 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1490 tabular.setAlignment(
1491 tabular.getCellNumber(i, j),
1496 case LyXTabular::M_VALIGN_TOP:
1497 case LyXTabular::M_VALIGN_BOTTOM:
1498 case LyXTabular::M_VALIGN_MIDDLE:
1500 case LyXTabular::VALIGN_TOP:
1501 case LyXTabular::VALIGN_BOTTOM:
1502 case LyXTabular::VALIGN_MIDDLE:
1503 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1504 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1505 tabular.setVAlignment(
1506 tabular.getCellNumber(i, j),
1510 case LyXTabular::MULTICOLUMN: {
1511 if (sel_row_start != sel_row_end) {
1512 #ifdef WITH_WARNINGS
1513 #warning Need I say it ? This is horrible.
1515 Alert::error(_("Error setting multicolumn"),
1516 _("You cannot set multicolumn vertically."));
1519 if (!cur.selection()) {
1520 // just multicol for one single cell
1521 // check whether we are completely in a multicol
1522 if (tabular.isMultiColumn(cur.idx()))
1523 tabular.unsetMultiColumn(cur.idx());
1525 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1528 // we have a selection so this means we just add all this
1529 // cells to form a multicolumn cell
1530 idx_type const s_start = cur.selBegin().idx();
1531 idx_type const s_end = cur.selEnd().idx();
1532 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1533 cur.idx() = s_start;
1536 cur.selection() = false;
1540 case LyXTabular::SET_ALL_LINES:
1542 case LyXTabular::UNSET_ALL_LINES:
1543 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1544 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1545 tabular.setAllLines(
1546 tabular.getCellNumber(i,j), setLines);
1549 case LyXTabular::SET_LONGTABULAR:
1550 tabular.setLongTabular(true);
1553 case LyXTabular::UNSET_LONGTABULAR:
1554 tabular.setLongTabular(false);
1557 case LyXTabular::SET_ROTATE_TABULAR:
1558 tabular.setRotateTabular(true);
1561 case LyXTabular::UNSET_ROTATE_TABULAR:
1562 tabular.setRotateTabular(false);
1565 case LyXTabular::SET_ROTATE_CELL:
1566 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1567 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1568 tabular.setRotateCell(
1569 tabular.getCellNumber(i, j), true);
1572 case LyXTabular::UNSET_ROTATE_CELL:
1573 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1574 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1575 tabular.setRotateCell(
1576 tabular.getCellNumber(i, j), false);
1579 case LyXTabular::SET_USEBOX: {
1580 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1581 if (val == tabular.getUsebox(cur.idx()))
1582 val = LyXTabular::BOX_NONE;
1583 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1584 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1585 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1589 case LyXTabular::UNSET_LTFIRSTHEAD:
1591 case LyXTabular::SET_LTFIRSTHEAD:
1592 tabular.getRowOfLTFirstHead(row, ltt);
1593 checkLongtableSpecial(ltt, value, flag);
1594 tabular.setLTHead(row, flag, ltt, true);
1597 case LyXTabular::UNSET_LTHEAD:
1599 case LyXTabular::SET_LTHEAD:
1600 tabular.getRowOfLTHead(row, ltt);
1601 checkLongtableSpecial(ltt, value, flag);
1602 tabular.setLTHead(row, flag, ltt, false);
1605 case LyXTabular::UNSET_LTFOOT:
1607 case LyXTabular::SET_LTFOOT:
1608 tabular.getRowOfLTFoot(row, ltt);
1609 checkLongtableSpecial(ltt, value, flag);
1610 tabular.setLTFoot(row, flag, ltt, false);
1613 case LyXTabular::UNSET_LTLASTFOOT:
1615 case LyXTabular::SET_LTLASTFOOT:
1616 tabular.getRowOfLTLastFoot(row, ltt);
1617 checkLongtableSpecial(ltt, value, flag);
1618 tabular.setLTFoot(row, flag, ltt, true);
1621 case LyXTabular::SET_LTNEWPAGE:
1622 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1625 // dummy stuff just to avoid warnings
1626 case LyXTabular::LAST_ACTION:
1630 InsetTabularMailer(*this).updateDialog(&bv);
1634 bool InsetTabular::showInsetDialog(BufferView * bv) const
1636 InsetTabularMailer(*this).showDialog(bv);
1641 void InsetTabular::openLayoutDialog(BufferView * bv) const
1643 InsetTabularMailer(*this).showDialog(bv);
1647 void InsetTabular::getLabelList(Buffer const & buffer,
1648 vector<string> & list) const
1650 tabular.getLabelList(buffer, list);
1654 bool InsetTabular::copySelection(LCursor & cur)
1656 if (!cur.selection())
1661 getSelection(cur, rs, re, cs, ce);
1663 paste_tabular.reset(new LyXTabular(tabular));
1665 for (row_type i = 0; i < rs; ++i)
1666 paste_tabular->deleteRow(0);
1668 row_type const rows = re - rs + 1;
1669 while (paste_tabular->rows() > rows)
1670 paste_tabular->deleteRow(rows);
1672 paste_tabular->setTopLine(0, true, true);
1673 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1676 for (col_type i = 0; i < cs; ++i)
1677 paste_tabular->deleteColumn(0);
1679 col_type const columns = ce - cs + 1;
1680 while (paste_tabular->columns() > columns)
1681 paste_tabular->deleteColumn(columns);
1683 paste_tabular->setLeftLine(0, true, true);
1684 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1688 OutputParams const runparams;
1689 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1690 cur.bv().stuffClipboard(os.str());
1695 bool InsetTabular::pasteSelection(LCursor & cur)
1699 col_type const actcol = tabular.column_of_cell(cur.idx());
1700 row_type const actrow = tabular.row_of_cell(cur.idx());
1701 for (row_type r1 = 0, r2 = actrow;
1702 r1 < paste_tabular->rows() && r2 < tabular.rows();
1704 for (col_type c1 = 0, c2 = actcol;
1705 c1 < paste_tabular->columns() && c2 < tabular.columns();
1707 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1708 tabular.isPartOfMultiColumn(r2, c2))
1710 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1714 if (tabular.isPartOfMultiColumn(r2, c2)) {
1718 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1719 inset = paste_tabular->getCellInset(r1, c1);
1727 void InsetTabular::cutSelection(LCursor & cur)
1729 if (!cur.selection())
1732 bool const track = cur.buffer().params().tracking_changes;
1735 getSelection(cur, rs, re, cs, ce);
1736 for (row_type i = rs; i <= re; ++i)
1737 for (col_type j = cs; j <= ce; ++j)
1738 cell(tabular.getCellNumber(i, j))->clear(track);
1740 // cursor position might be invalid now
1741 cur.pos() = cur.lastpos();
1742 cur.clearSelection();
1746 bool InsetTabular::isRightToLeft(LCursor & cur) const
1748 BOOST_ASSERT(cur.depth() > 1);
1749 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1750 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1751 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1752 parentpos).language()->RightToLeft();
1756 void InsetTabular::getSelection(LCursor & cur,
1757 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1759 CursorSlice const & beg = cur.selBegin();
1760 CursorSlice const & end = cur.selEnd();
1761 cs = tabular.column_of_cell(beg.idx());
1762 ce = tabular.column_of_cell(end.idx());
1765 cs = tabular.column_of_cell(end.idx());
1767 ce = tabular.right_column_of_cell(end.idx());
1770 rs = tabular.row_of_cell(beg.idx());
1771 re = tabular.row_of_cell(end.idx());
1777 size_t InsetTabular::nargs() const
1779 return tabular.getNumberOfCells();
1783 LyXText * InsetTabular::getText(int idx) const
1785 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1789 void InsetTabular::markErased()
1791 for (idx_type idx = 0; idx < nargs(); ++idx)
1792 cell(idx)->markErased();
1796 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1799 idx_type const cell = tabular.getCellFromInset(in);
1800 // FIXME: getCellFromInset() returns now always a valid cell, so
1801 // the stuff below can be deleted, and instead we have:
1802 return tabular.getPWidth(cell).zero();
1805 return tabular.getPWidth(cell).zero();
1807 // this is a workaround for a crash (New, Insert->Tabular,
1808 // Insert->FootNote)
1812 // well we didn't obviously find it so maybe our owner knows more
1813 BOOST_ASSERT(owner());
1814 return owner()->forceDefaultParagraphs(in);
1820 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1823 if (buf.length() <= 0)
1828 col_type maxCols = 1;
1829 string::size_type const len = buf.length();
1830 string::size_type p = 0;
1832 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1840 maxCols = max(cols, maxCols);
1846 maxCols = max(cols, maxCols);
1847 LyXTabular * loctab;
1852 paste_tabular.reset(
1853 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1854 loctab = paste_tabular.get();
1858 cell = bv.cursor().idx();
1859 ocol = tabular.column_of_cell(cell);
1860 row = tabular.row_of_cell(cell);
1863 string::size_type op = 0;
1864 idx_type const cells = loctab->getNumberOfCells();
1867 rows = loctab->rows();
1868 col_type const columns = loctab->columns();
1870 while (cell < cells && p < len && row < rows &&
1871 (p = buf.find_first_of("\t\n", p)) != string::npos)
1877 // we can only set this if we are not too far right
1878 if (cols < columns) {
1879 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1880 Paragraph & par = inset->text_.getPar(0);
1881 LyXFont const font = inset->text_.getFont(par, 0);
1882 inset->setText(buf.substr(op, p - op), font);
1888 // we can only set this if we are not too far right
1889 if (cols < columns) {
1890 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1891 Paragraph & par = inset->text_.getPar(0);
1892 LyXFont const font = inset->text_.getFont(par, 0);
1893 inset->setText(buf.substr(op, p - op), font);
1898 cell = loctab->getCellNumber(row, cols);
1904 // check for the last cell if there is no trailing '\n'
1905 if (cell < cells && op < len) {
1906 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1907 Paragraph & par = inset->text_.getPar(0);
1908 LyXFont const font = inset->text_.getFont(par, 0);
1909 inset->setText(buf.substr(op, len - op), font);
1915 void InsetTabular::addPreview(PreviewLoader & loader) const
1917 row_type const rows = tabular.rows();
1918 col_type const columns = tabular.columns();
1919 for (row_type i = 0; i < rows; ++i) {
1920 for (col_type j = 0; j < columns; ++j)
1921 tabular.getCellInset(i, j)->addPreview(loader);
1926 bool InsetTabular::tablemode(LCursor & cur) const
1928 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1935 string const InsetTabularMailer::name_("tabular");
1937 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1938 : inset_(const_cast<InsetTabular &>(inset))
1942 string const InsetTabularMailer::inset2string(Buffer const &) const
1944 return params2string(inset_);
1948 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1950 istringstream data(in);
1952 lex.setStream(data);
1959 if (!lex || token != name_)
1960 return print_mailer_error("InsetTabularMailer", in, 1,
1963 // This is part of the inset proper that is usually swallowed
1964 // by Buffer::readInset
1966 if (!lex || token != "Tabular")
1967 return print_mailer_error("InsetTabularMailer", in, 2,
1970 Buffer const & buffer = inset.buffer();
1971 inset.read(buffer, lex);
1975 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1978 data << name_ << ' ';
1979 inset.write(inset.buffer(), data);
1980 data << "\\end_inset\n";