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 // These are only enabled inside tabular
972 case LFUN_CELL_BACKWARD:
973 case LFUN_CELL_FORWARD:
974 status.enabled(true);
977 // disable these with multiple cells selected
978 case LFUN_INSERT_CHARSTYLE:
979 case LFUN_INSET_FLOAT:
980 case LFUN_INSET_WIDE_FLOAT:
981 case LFUN_INSET_FOOTNOTE:
982 case LFUN_INSET_MARGINAL:
983 case LFUN_INSERT_MATH:
985 case LFUN_MATH_MUTATE:
986 case LFUN_MATH_DISPLAY:
987 case LFUN_INSERT_NOTE:
988 case LFUN_INSET_OPTARG:
989 case LFUN_INSERT_BOX:
990 case LFUN_INSERT_BRANCH:
991 case LFUN_INSET_WRAP:
992 case LFUN_INSET_ERT: {
993 if (tablemode(cur)) {
994 status.enabled(false);
1000 // we try to handle this event in the insets dispatch function.
1001 return cell(cur.idx())->getStatus(cur, cmd, status);
1006 int InsetTabular::latex(Buffer const & buf, ostream & os,
1007 OutputParams const & runparams) const
1009 return tabular.latex(buf, os, runparams);
1013 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1014 OutputParams const & runparams) const
1016 int const dp = runparams.linelen ? runparams.depth : 0;
1017 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1021 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1022 OutputParams const & runparams) const
1024 return tabular.linuxdoc(buf,os, runparams);
1028 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1029 OutputParams const & runparams) const
1032 InsetBase * master = 0;
1034 #ifdef WITH_WARNINGS
1035 #warning Why not pass a proper DocIterator here?
1038 // if the table is inside a float it doesn't need the informaltable
1039 // wrapper. Search for it.
1040 for (master = owner(); master; master = master->owner())
1041 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1046 os << "<informaltable>";
1049 ret += tabular.docbook(buf, os, runparams);
1051 os << "</informaltable>";
1058 void InsetTabular::validate(LaTeXFeatures & features) const
1060 tabular.validate(features);
1064 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1066 return tabular.getCellInset(idx);
1070 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1072 return tabular.getCellInset(idx);
1076 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1078 cell(sl.idx())->getCursorPos(sl, x, y);
1080 // y offset correction
1081 int const row = tabular.row_of_cell(sl.idx());
1082 for (int i = 0; i <= row; ++i) {
1084 y += tabular.getAscentOfRow(i);
1085 y += tabular.getAdditionalHeight(i);
1088 y += tabular.getDescentOfRow(i);
1091 // x offset correction
1092 int const col = tabular.column_of_cell(sl.idx());
1093 int idx = tabular.getCellNumber(row, 0);
1094 for (int j = 0; j < col; ++j) {
1095 if (tabular.isPartOfMultiColumn(row, j))
1097 x += tabular.getWidthOfColumn(idx);
1100 x += tabular.getBeginningOfTextInCell(idx);
1101 x += ADD_TO_TABULAR_WIDTH;
1109 // Manhattan distance to nearest corner
1110 int dist(InsetOld const & inset, int x, int y)
1114 Point o = theCoords.getInsets().xy(&inset);
1115 int const xo = o.x_;
1116 int const yo = o.y_;
1120 else if (x > xo + inset.width())
1121 xx = x - xo - inset.width();
1123 if (y < yo - inset.ascent())
1124 yy = yo - inset.ascent() - y;
1125 else if (y > yo + inset.descent())
1126 yy = y - yo - inset.descent();
1128 lyxerr << " xo_=" << xo << " yo_=" << yo
1129 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1130 << " descent=" << inset.descent()
1131 << " dist=" << xx + yy << endl;
1139 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
1141 lyxerr << "# InsetTabular::setPos() x=" << x << " y=" << y << endl;
1142 idx_type idx_min = 0;
1143 int dist_min = std::numeric_limits<int>::max();
1144 for (idx_type i = 0; i < nargs(); ++i) {
1145 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1146 int d = dist(*tabular.getCellInset(i), x, y);
1153 cur.idx() = idx_min;
1154 //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
1156 return cell(cur.idx())->text_.editXY(cur, x, y);
1160 int InsetTabular::getCellXPos(idx_type const cell) const
1164 for (; !tabular.isFirstCellInRow(c); --c)
1166 int lx = tabular.getWidthOfColumn(cell);
1167 for (; c < cell; ++c)
1168 lx += tabular.getWidthOfColumn(c);
1170 return lx - tabular.getWidthOfColumn(cell);
1174 void InsetTabular::resetPos(LCursor & cur) const
1176 BufferView & bv = cur.bv();
1177 // col_type const actcol = tabular.column_of_cell(cur.idx());
1178 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1179 // int const new_x = getCellXPos(cur.idx()) + offset;
1180 // int const old_x = cursorx_;
1181 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1182 // cursorx_ = new_x;
1183 // cursor.x(getCellXPos(cur.idx()) + offset);
1184 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1185 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1187 // scroll(bv, 0.0F);
1188 // } else if (cursorx_ - offset > 20 &&
1189 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1190 // scroll(bv, - col_width - 20);
1191 // } else if (cursorx_ - offset < 20) {
1192 // scroll(bv, 20 - cursorx_ + offset);
1193 // } else if (scroll() && xo() > 20 &&
1194 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1195 // scroll(bv, old_x - cursorx_);
1198 if (&cur.inset() != this) {
1202 int const X2 = bv.workWidth();
1203 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1204 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1205 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1208 scroll(bv, X1 + 20 - x1);
1210 scroll(bv, X2 - 20 - x2);
1215 InsetTabularMailer(*this).updateDialog(&bv);
1219 void InsetTabular::moveNextCell(LCursor & cur)
1221 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1222 if (isRightToLeft(cur)) {
1223 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1224 if (tabular.isFirstCellInRow(cur.idx())) {
1225 row_type const row = tabular.row_of_cell(cur.idx());
1226 if (row == tabular.rows() - 1)
1228 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1235 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1236 if (tabular.isLastCell(cur.idx()))
1242 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1247 void InsetTabular::movePrevCell(LCursor & cur)
1249 if (isRightToLeft(cur)) {
1250 if (tabular.isLastCellInRow(cur.idx())) {
1251 row_type const row = tabular.row_of_cell(cur.idx());
1254 cur.idx() = tabular.getFirstCellInRow(row);
1255 cur.idx() = tabular.getCellAbove(cur.idx());
1257 if (tabular.isLastCell(cur.idx()))
1262 if (cur.idx() == 0) // first cell
1266 cur.pit() = cur.lastpit();
1267 cur.pos() = cur.lastpos();
1272 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1274 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1277 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1278 string const tmp = tabularFeature[i].feature;
1280 if (tmp == what.substr(0, tmp.length())) {
1281 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1282 //tabularFeatures[i].feature.length())) {
1283 action = tabularFeature[i].action;
1287 if (action == LyXTabular::LAST_ACTION)
1291 ltrim(what.substr(tabularFeature[i].feature.length()));
1292 tabularFeatures(cur, action, val);
1299 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1300 string const & special, bool & flag)
1302 if (special == "dl_above") {
1305 } else if (special == "dl_below") {
1306 ltt.bottomDL = flag;
1308 } else if (special == "empty") {
1320 void InsetTabular::tabularFeatures(LCursor & cur,
1321 LyXTabular::Feature feature, string const & value)
1323 BufferView & bv = cur.bv();
1324 col_type sel_col_start;
1325 col_type sel_col_end;
1326 row_type sel_row_start;
1327 row_type sel_row_end;
1328 bool setLines = false;
1329 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1330 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1334 case LyXTabular::M_ALIGN_LEFT:
1335 case LyXTabular::ALIGN_LEFT:
1336 setAlign = LYX_ALIGN_LEFT;
1339 case LyXTabular::M_ALIGN_RIGHT:
1340 case LyXTabular::ALIGN_RIGHT:
1341 setAlign = LYX_ALIGN_RIGHT;
1344 case LyXTabular::M_ALIGN_CENTER:
1345 case LyXTabular::ALIGN_CENTER:
1346 setAlign = LYX_ALIGN_CENTER;
1349 case LyXTabular::ALIGN_BLOCK:
1350 setAlign = LYX_ALIGN_BLOCK;
1353 case LyXTabular::M_VALIGN_TOP:
1354 case LyXTabular::VALIGN_TOP:
1355 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1358 case LyXTabular::M_VALIGN_BOTTOM:
1359 case LyXTabular::VALIGN_BOTTOM:
1360 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1363 case LyXTabular::M_VALIGN_MIDDLE:
1364 case LyXTabular::VALIGN_MIDDLE:
1365 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1372 recordUndo(cur, Undo::ATOMIC);
1374 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1375 row_type const row = tabular.row_of_cell(cur.idx());
1376 col_type const column = tabular.column_of_cell(cur.idx());
1378 LyXTabular::ltType ltt;
1382 case LyXTabular::SET_PWIDTH: {
1383 LyXLength const len(value);
1384 tabular.setColumnPWidth(cur.idx(), len);
1386 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1387 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1388 else if (!len.zero()
1389 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1390 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1394 case LyXTabular::SET_MPWIDTH:
1395 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1398 case LyXTabular::SET_SPECIAL_COLUMN:
1399 case LyXTabular::SET_SPECIAL_MULTI:
1400 tabular.setAlignSpecial(cur.idx(),value,feature);
1403 case LyXTabular::APPEND_ROW:
1404 // append the row into the tabular
1405 tabular.appendRow(bv.buffer()->params(), cur.idx());
1408 case LyXTabular::APPEND_COLUMN:
1409 // append the column into the tabular
1410 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1413 case LyXTabular::DELETE_ROW:
1414 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1415 tabular.deleteRow(sel_row_start);
1416 if (sel_row_start >= tabular.rows())
1418 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1421 cur.selection() = false;
1424 case LyXTabular::DELETE_COLUMN:
1425 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1426 tabular.deleteColumn(sel_col_start);
1427 if (sel_col_start >= tabular.columns())
1429 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1432 cur.selection() = false;
1435 case LyXTabular::M_TOGGLE_LINE_TOP:
1437 case LyXTabular::TOGGLE_LINE_TOP: {
1438 bool lineSet = !tabular.topLine(cur.idx(), flag);
1439 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1440 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1442 tabular.getCellNumber(i, j),
1447 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1449 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1450 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1451 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1452 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1453 tabular.setBottomLine(
1454 tabular.getCellNumber(i, j),
1460 case LyXTabular::M_TOGGLE_LINE_LEFT:
1462 case LyXTabular::TOGGLE_LINE_LEFT: {
1463 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1464 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1465 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1466 tabular.setLeftLine(
1467 tabular.getCellNumber(i,j),
1473 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1475 case LyXTabular::TOGGLE_LINE_RIGHT: {
1476 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1477 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1478 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1479 tabular.setRightLine(
1480 tabular.getCellNumber(i,j),
1486 case LyXTabular::M_ALIGN_LEFT:
1487 case LyXTabular::M_ALIGN_RIGHT:
1488 case LyXTabular::M_ALIGN_CENTER:
1490 case LyXTabular::ALIGN_LEFT:
1491 case LyXTabular::ALIGN_RIGHT:
1492 case LyXTabular::ALIGN_CENTER:
1493 case LyXTabular::ALIGN_BLOCK:
1494 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1495 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1496 tabular.setAlignment(
1497 tabular.getCellNumber(i, j),
1502 case LyXTabular::M_VALIGN_TOP:
1503 case LyXTabular::M_VALIGN_BOTTOM:
1504 case LyXTabular::M_VALIGN_MIDDLE:
1506 case LyXTabular::VALIGN_TOP:
1507 case LyXTabular::VALIGN_BOTTOM:
1508 case LyXTabular::VALIGN_MIDDLE:
1509 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1510 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1511 tabular.setVAlignment(
1512 tabular.getCellNumber(i, j),
1516 case LyXTabular::MULTICOLUMN: {
1517 if (sel_row_start != sel_row_end) {
1518 #ifdef WITH_WARNINGS
1519 #warning Need I say it ? This is horrible.
1521 Alert::error(_("Error setting multicolumn"),
1522 _("You cannot set multicolumn vertically."));
1525 if (!cur.selection()) {
1526 // just multicol for one single cell
1527 // check whether we are completely in a multicol
1528 if (tabular.isMultiColumn(cur.idx()))
1529 tabular.unsetMultiColumn(cur.idx());
1531 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1534 // we have a selection so this means we just add all this
1535 // cells to form a multicolumn cell
1536 idx_type const s_start = cur.selBegin().idx();
1537 idx_type const s_end = cur.selEnd().idx();
1538 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1539 cur.idx() = s_start;
1542 cur.selection() = false;
1546 case LyXTabular::SET_ALL_LINES:
1548 case LyXTabular::UNSET_ALL_LINES:
1549 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1550 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1551 tabular.setAllLines(
1552 tabular.getCellNumber(i,j), setLines);
1555 case LyXTabular::SET_LONGTABULAR:
1556 tabular.setLongTabular(true);
1559 case LyXTabular::UNSET_LONGTABULAR:
1560 tabular.setLongTabular(false);
1563 case LyXTabular::SET_ROTATE_TABULAR:
1564 tabular.setRotateTabular(true);
1567 case LyXTabular::UNSET_ROTATE_TABULAR:
1568 tabular.setRotateTabular(false);
1571 case LyXTabular::SET_ROTATE_CELL:
1572 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1573 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1574 tabular.setRotateCell(
1575 tabular.getCellNumber(i, j), true);
1578 case LyXTabular::UNSET_ROTATE_CELL:
1579 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1580 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1581 tabular.setRotateCell(
1582 tabular.getCellNumber(i, j), false);
1585 case LyXTabular::SET_USEBOX: {
1586 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1587 if (val == tabular.getUsebox(cur.idx()))
1588 val = LyXTabular::BOX_NONE;
1589 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1590 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1591 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1595 case LyXTabular::UNSET_LTFIRSTHEAD:
1597 case LyXTabular::SET_LTFIRSTHEAD:
1598 tabular.getRowOfLTFirstHead(row, ltt);
1599 checkLongtableSpecial(ltt, value, flag);
1600 tabular.setLTHead(row, flag, ltt, true);
1603 case LyXTabular::UNSET_LTHEAD:
1605 case LyXTabular::SET_LTHEAD:
1606 tabular.getRowOfLTHead(row, ltt);
1607 checkLongtableSpecial(ltt, value, flag);
1608 tabular.setLTHead(row, flag, ltt, false);
1611 case LyXTabular::UNSET_LTFOOT:
1613 case LyXTabular::SET_LTFOOT:
1614 tabular.getRowOfLTFoot(row, ltt);
1615 checkLongtableSpecial(ltt, value, flag);
1616 tabular.setLTFoot(row, flag, ltt, false);
1619 case LyXTabular::UNSET_LTLASTFOOT:
1621 case LyXTabular::SET_LTLASTFOOT:
1622 tabular.getRowOfLTLastFoot(row, ltt);
1623 checkLongtableSpecial(ltt, value, flag);
1624 tabular.setLTFoot(row, flag, ltt, true);
1627 case LyXTabular::SET_LTNEWPAGE:
1628 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1631 // dummy stuff just to avoid warnings
1632 case LyXTabular::LAST_ACTION:
1636 InsetTabularMailer(*this).updateDialog(&bv);
1640 bool InsetTabular::showInsetDialog(BufferView * bv) const
1642 InsetTabularMailer(*this).showDialog(bv);
1647 void InsetTabular::openLayoutDialog(BufferView * bv) const
1649 InsetTabularMailer(*this).showDialog(bv);
1653 void InsetTabular::getLabelList(Buffer const & buffer,
1654 vector<string> & list) const
1656 tabular.getLabelList(buffer, list);
1660 bool InsetTabular::copySelection(LCursor & cur)
1662 if (!cur.selection())
1667 getSelection(cur, rs, re, cs, ce);
1669 paste_tabular.reset(new LyXTabular(tabular));
1671 for (row_type i = 0; i < rs; ++i)
1672 paste_tabular->deleteRow(0);
1674 row_type const rows = re - rs + 1;
1675 while (paste_tabular->rows() > rows)
1676 paste_tabular->deleteRow(rows);
1678 paste_tabular->setTopLine(0, true, true);
1679 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1682 for (col_type i = 0; i < cs; ++i)
1683 paste_tabular->deleteColumn(0);
1685 col_type const columns = ce - cs + 1;
1686 while (paste_tabular->columns() > columns)
1687 paste_tabular->deleteColumn(columns);
1689 paste_tabular->setLeftLine(0, true, true);
1690 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1694 OutputParams const runparams;
1695 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1696 cur.bv().stuffClipboard(os.str());
1701 bool InsetTabular::pasteSelection(LCursor & cur)
1705 col_type const actcol = tabular.column_of_cell(cur.idx());
1706 row_type const actrow = tabular.row_of_cell(cur.idx());
1707 for (row_type r1 = 0, r2 = actrow;
1708 r1 < paste_tabular->rows() && r2 < tabular.rows();
1710 for (col_type c1 = 0, c2 = actcol;
1711 c1 < paste_tabular->columns() && c2 < tabular.columns();
1713 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1714 tabular.isPartOfMultiColumn(r2, c2))
1716 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1720 if (tabular.isPartOfMultiColumn(r2, c2)) {
1724 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1725 inset = paste_tabular->getCellInset(r1, c1);
1733 void InsetTabular::cutSelection(LCursor & cur)
1735 if (!cur.selection())
1738 bool const track = cur.buffer().params().tracking_changes;
1741 getSelection(cur, rs, re, cs, ce);
1742 for (row_type i = rs; i <= re; ++i)
1743 for (col_type j = cs; j <= ce; ++j)
1744 cell(tabular.getCellNumber(i, j))->clear(track);
1746 // cursor position might be invalid now
1747 cur.pos() = cur.lastpos();
1748 cur.clearSelection();
1752 bool InsetTabular::isRightToLeft(LCursor & cur) const
1754 BOOST_ASSERT(cur.depth() > 1);
1755 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1756 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1757 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1758 parentpos).language()->RightToLeft();
1762 void InsetTabular::getSelection(LCursor & cur,
1763 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1765 CursorSlice const & beg = cur.selBegin();
1766 CursorSlice const & end = cur.selEnd();
1767 cs = tabular.column_of_cell(beg.idx());
1768 ce = tabular.column_of_cell(end.idx());
1771 cs = tabular.column_of_cell(end.idx());
1773 ce = tabular.right_column_of_cell(end.idx());
1776 rs = tabular.row_of_cell(beg.idx());
1777 re = tabular.row_of_cell(end.idx());
1783 size_t InsetTabular::nargs() const
1785 return tabular.getNumberOfCells();
1789 LyXText * InsetTabular::getText(int idx) const
1791 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1795 void InsetTabular::markErased()
1797 for (idx_type idx = 0; idx < nargs(); ++idx)
1798 cell(idx)->markErased();
1802 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1805 idx_type const cell = tabular.getCellFromInset(in);
1806 // FIXME: getCellFromInset() returns now always a valid cell, so
1807 // the stuff below can be deleted, and instead we have:
1808 return tabular.getPWidth(cell).zero();
1811 return tabular.getPWidth(cell).zero();
1813 // this is a workaround for a crash (New, Insert->Tabular,
1814 // Insert->FootNote)
1818 // well we didn't obviously find it so maybe our owner knows more
1819 BOOST_ASSERT(owner());
1820 return owner()->forceDefaultParagraphs(in);
1826 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1829 if (buf.length() <= 0)
1834 col_type maxCols = 1;
1835 string::size_type const len = buf.length();
1836 string::size_type p = 0;
1838 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1846 maxCols = max(cols, maxCols);
1852 maxCols = max(cols, maxCols);
1853 LyXTabular * loctab;
1858 paste_tabular.reset(
1859 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1860 loctab = paste_tabular.get();
1864 cell = bv.cursor().idx();
1865 ocol = tabular.column_of_cell(cell);
1866 row = tabular.row_of_cell(cell);
1869 string::size_type op = 0;
1870 idx_type const cells = loctab->getNumberOfCells();
1873 rows = loctab->rows();
1874 col_type const columns = loctab->columns();
1876 while (cell < cells && p < len && row < rows &&
1877 (p = buf.find_first_of("\t\n", p)) != string::npos)
1883 // we can only set this if we are not too far right
1884 if (cols < columns) {
1885 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1886 Paragraph & par = inset->text_.getPar(0);
1887 LyXFont const font = inset->text_.getFont(par, 0);
1888 inset->setText(buf.substr(op, p - op), font);
1894 // we can only set this if we are not too far right
1895 if (cols < columns) {
1896 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1897 Paragraph & par = inset->text_.getPar(0);
1898 LyXFont const font = inset->text_.getFont(par, 0);
1899 inset->setText(buf.substr(op, p - op), font);
1904 cell = loctab->getCellNumber(row, cols);
1910 // check for the last cell if there is no trailing '\n'
1911 if (cell < cells && op < len) {
1912 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1913 Paragraph & par = inset->text_.getPar(0);
1914 LyXFont const font = inset->text_.getFont(par, 0);
1915 inset->setText(buf.substr(op, len - op), font);
1921 void InsetTabular::addPreview(PreviewLoader & loader) const
1923 row_type const rows = tabular.rows();
1924 col_type const columns = tabular.columns();
1925 for (row_type i = 0; i < rows; ++i) {
1926 for (col_type j = 0; j < columns; ++j)
1927 tabular.getCellInset(i, j)->addPreview(loader);
1932 bool InsetTabular::tablemode(LCursor & cur) const
1934 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1941 string const InsetTabularMailer::name_("tabular");
1943 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1944 : inset_(const_cast<InsetTabular &>(inset))
1948 string const InsetTabularMailer::inset2string(Buffer const &) const
1950 return params2string(inset_);
1954 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1956 istringstream data(in);
1958 lex.setStream(data);
1965 if (!lex || token != name_)
1966 return print_mailer_error("InsetTabularMailer", in, 1,
1969 // This is part of the inset proper that is usually swallowed
1970 // by Buffer::readInset
1972 if (!lex || token != "Tabular")
1973 return print_mailer_error("InsetTabularMailer", in, 2,
1976 Buffer const & buffer = inset.buffer();
1977 inset.read(buffer, lex);
1981 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1984 data << name_ << ' ';
1985 inset.write(inset.buffer(), data);
1986 data << "\\end_inset\n";