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);
728 // we try to handle this event in the insets dispatch function.
729 cell(cur.idx())->dispatch(cur, cmd);
733 InsetTabularMailer(*this).updateDialog(&cur.bv());
737 // function sets an object as defined in func_status.h:
738 // states OK, Unknown, Disabled, On, Off.
739 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
740 FuncStatus & status) const
742 switch (cmd.action) {
743 case LFUN_TABULAR_FEATURE: {
744 int action = LyXTabular::LAST_ACTION;
746 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
747 string const tmp = tabularFeature[i].feature;
748 if (tmp == cmd.argument.substr(0, tmp.length())) {
749 action = tabularFeature[i].action;
753 if (action == LyXTabular::LAST_ACTION) {
755 status.unknown(true);
759 string const argument
760 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
762 row_type sel_row_start = 0;
763 row_type sel_row_end = 0;
765 LyXTabular::ltType dummyltt;
768 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
771 case LyXTabular::SET_PWIDTH:
772 case LyXTabular::SET_MPWIDTH:
773 case LyXTabular::SET_SPECIAL_COLUMN:
774 case LyXTabular::SET_SPECIAL_MULTI:
775 case LyXTabular::APPEND_ROW:
776 case LyXTabular::APPEND_COLUMN:
777 case LyXTabular::DELETE_ROW:
778 case LyXTabular::DELETE_COLUMN:
779 case LyXTabular::SET_ALL_LINES:
780 case LyXTabular::UNSET_ALL_LINES:
784 case LyXTabular::MULTICOLUMN:
785 status.setOnOff(tabular.isMultiColumn(cur.idx()));
788 case LyXTabular::M_TOGGLE_LINE_TOP:
790 case LyXTabular::TOGGLE_LINE_TOP:
791 status.setOnOff(tabular.topLine(cur.idx(), flag));
794 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
796 case LyXTabular::TOGGLE_LINE_BOTTOM:
797 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
800 case LyXTabular::M_TOGGLE_LINE_LEFT:
802 case LyXTabular::TOGGLE_LINE_LEFT:
803 status.setOnOff(tabular.leftLine(cur.idx(), flag));
806 case LyXTabular::M_TOGGLE_LINE_RIGHT:
808 case LyXTabular::TOGGLE_LINE_RIGHT:
809 status.setOnOff(tabular.rightLine(cur.idx(), flag));
812 case LyXTabular::M_ALIGN_LEFT:
814 case LyXTabular::ALIGN_LEFT:
815 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
818 case LyXTabular::M_ALIGN_RIGHT:
820 case LyXTabular::ALIGN_RIGHT:
821 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
824 case LyXTabular::M_ALIGN_CENTER:
826 case LyXTabular::ALIGN_CENTER:
827 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
830 case LyXTabular::ALIGN_BLOCK:
831 status.enabled(!tabular.getPWidth(cur.idx()).zero());
832 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
835 case LyXTabular::M_VALIGN_TOP:
837 case LyXTabular::VALIGN_TOP:
839 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
842 case LyXTabular::M_VALIGN_BOTTOM:
844 case LyXTabular::VALIGN_BOTTOM:
846 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
849 case LyXTabular::M_VALIGN_MIDDLE:
851 case LyXTabular::VALIGN_MIDDLE:
853 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
856 case LyXTabular::SET_LONGTABULAR:
857 status.setOnOff(tabular.isLongTabular());
860 case LyXTabular::UNSET_LONGTABULAR:
861 status.setOnOff(!tabular.isLongTabular());
864 case LyXTabular::SET_ROTATE_TABULAR:
865 status.setOnOff(tabular.getRotateTabular());
868 case LyXTabular::UNSET_ROTATE_TABULAR:
869 status.setOnOff(!tabular.getRotateTabular());
872 case LyXTabular::SET_ROTATE_CELL:
873 status.setOnOff(tabular.getRotateCell(cur.idx()));
876 case LyXTabular::UNSET_ROTATE_CELL:
877 status.setOnOff(!tabular.getRotateCell(cur.idx()));
880 case LyXTabular::SET_USEBOX:
881 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
884 case LyXTabular::SET_LTFIRSTHEAD:
885 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
888 case LyXTabular::SET_LTHEAD:
889 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
892 case LyXTabular::SET_LTFOOT:
893 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
896 case LyXTabular::SET_LTLASTFOOT:
897 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
900 case LyXTabular::SET_LTNEWPAGE:
901 status.setOnOff(tabular.getLTNewPage(sel_row_start));
906 status.enabled(false);
913 // we try to handle this event in the insets dispatch function.
914 return cell(cur.idx())->getStatus(cur, cmd, status);
919 int InsetTabular::latex(Buffer const & buf, ostream & os,
920 OutputParams const & runparams) const
922 return tabular.latex(buf, os, runparams);
926 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
927 OutputParams const & runparams) const
929 int const dp = runparams.linelen ? runparams.depth : 0;
930 return tabular.plaintext(buf, os, runparams, dp, false, 0);
934 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
935 OutputParams const & runparams) const
937 return tabular.linuxdoc(buf,os, runparams);
941 int InsetTabular::docbook(Buffer const & buf, ostream & os,
942 OutputParams const & runparams) const
945 InsetBase * master = 0;
948 #warning Why not pass a proper DocIterator here?
951 // if the table is inside a float it doesn't need the informaltable
952 // wrapper. Search for it.
953 for (master = owner(); master; master = master->owner())
954 if (master->lyxCode() == InsetBase::FLOAT_CODE)
959 os << "<informaltable>";
962 ret += tabular.docbook(buf, os, runparams);
964 os << "</informaltable>";
971 void InsetTabular::validate(LaTeXFeatures & features) const
973 tabular.validate(features);
977 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
979 return tabular.getCellInset(idx);
983 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
985 return tabular.getCellInset(idx);
989 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
991 cell(sl.idx())->getCursorPos(sl, x, y);
993 // y offset correction
994 int const row = tabular.row_of_cell(sl.idx());
995 for (int i = 0; i <= row; ++i) {
997 y += tabular.getAscentOfRow(i);
998 y += tabular.getAdditionalHeight(i);
1001 y += tabular.getDescentOfRow(i);
1004 // x offset correction
1005 int const col = tabular.column_of_cell(sl.idx());
1006 int idx = tabular.getCellNumber(row, 0);
1007 for (int j = 0; j < col; ++j) {
1008 if (tabular.isPartOfMultiColumn(row, j))
1010 x += tabular.getWidthOfColumn(idx);
1013 x += tabular.getBeginningOfTextInCell(idx);
1014 x += ADD_TO_TABULAR_WIDTH;
1022 // Manhattan distance to nearest corner
1023 int dist(InsetOld const & inset, int x, int y)
1027 Point o = theCoords.insets_.xy(&inset);
1028 int const xo = o.x_;
1029 int const yo = o.y_;
1033 else if (x > xo + inset.width())
1034 xx = x - xo - inset.width();
1036 if (y < yo - inset.ascent())
1037 yy = yo - inset.ascent() - y;
1038 else if (y > yo + inset.descent())
1039 yy = y - yo - inset.descent();
1041 lyxerr << " xo_=" << xo << " yo_=" << yo
1042 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1043 << " descent=" << inset.descent()
1044 << " dist=" << xx + yy << endl;
1052 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
1054 lyxerr << "# InsetTabular::setPos() x=" << x << " y=" << y << endl;
1055 idx_type idx_min = 0;
1056 int dist_min = std::numeric_limits<int>::max();
1057 for (idx_type i = 0; i < nargs(); ++i) {
1058 if (theCoords.insets_.has(tabular.getCellInset(i).get())) {
1059 int d = dist(*tabular.getCellInset(i), x, y);
1066 cur.idx() = idx_min;
1067 //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
1069 return cell(cur.idx())->text_.editXY(cur, x, y);
1073 int InsetTabular::getCellXPos(idx_type const cell) const
1077 for (; !tabular.isFirstCellInRow(c); --c)
1079 int lx = tabular.getWidthOfColumn(cell);
1080 for (; c < cell; ++c)
1081 lx += tabular.getWidthOfColumn(c);
1083 return lx - tabular.getWidthOfColumn(cell);
1087 void InsetTabular::resetPos(LCursor & cur) const
1089 BufferView & bv = cur.bv();
1090 // col_type const actcol = tabular.column_of_cell(cur.idx());
1091 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1092 // int const new_x = getCellXPos(cur.idx()) + offset;
1093 // int const old_x = cursorx_;
1094 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1095 // cursorx_ = new_x;
1096 // cursor.x(getCellXPos(cur.idx()) + offset);
1097 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1098 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1100 // scroll(bv, 0.0F);
1101 // } else if (cursorx_ - offset > 20 &&
1102 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1103 // scroll(bv, - col_width - 20);
1104 // } else if (cursorx_ - offset < 20) {
1105 // scroll(bv, 20 - cursorx_ + offset);
1106 // } else if (scroll() && xo() > 20 &&
1107 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1108 // scroll(bv, old_x - cursorx_);
1111 if (&cur.inset() != this) {
1115 int const X2 = bv.workWidth();
1116 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1117 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1118 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1121 scroll(bv, X1 + 20 - x1);
1123 scroll(bv, X2 - 20 - x2);
1128 InsetTabularMailer(*this).updateDialog(&bv);
1132 void InsetTabular::moveNextCell(LCursor & cur)
1134 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1135 if (isRightToLeft(cur)) {
1136 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1137 if (tabular.isFirstCellInRow(cur.idx())) {
1138 row_type const row = tabular.row_of_cell(cur.idx());
1139 if (row == tabular.rows() - 1)
1141 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1148 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1149 if (tabular.isLastCell(cur.idx()))
1155 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1160 void InsetTabular::movePrevCell(LCursor & cur)
1162 if (isRightToLeft(cur)) {
1163 if (tabular.isLastCellInRow(cur.idx())) {
1164 row_type const row = tabular.row_of_cell(cur.idx());
1167 cur.idx() = tabular.getFirstCellInRow(row);
1168 cur.idx() = tabular.getCellAbove(cur.idx());
1170 if (tabular.isLastCell(cur.idx()))
1175 if (cur.idx() == 0) // first cell
1185 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1187 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1190 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1191 string const tmp = tabularFeature[i].feature;
1193 if (tmp == what.substr(0, tmp.length())) {
1194 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1195 //tabularFeatures[i].feature.length())) {
1196 action = tabularFeature[i].action;
1200 if (action == LyXTabular::LAST_ACTION)
1204 ltrim(what.substr(tabularFeature[i].feature.length()));
1205 tabularFeatures(cur, action, val);
1212 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1213 string const & special, bool & flag)
1215 if (special == "dl_above") {
1218 } else if (special == "dl_below") {
1219 ltt.bottomDL = flag;
1221 } else if (special == "empty") {
1233 void InsetTabular::tabularFeatures(LCursor & cur,
1234 LyXTabular::Feature feature, string const & value)
1236 BufferView & bv = cur.bv();
1237 col_type sel_col_start;
1238 col_type sel_col_end;
1239 row_type sel_row_start;
1240 row_type sel_row_end;
1241 bool setLines = false;
1242 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1243 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1247 case LyXTabular::M_ALIGN_LEFT:
1248 case LyXTabular::ALIGN_LEFT:
1249 setAlign = LYX_ALIGN_LEFT;
1252 case LyXTabular::M_ALIGN_RIGHT:
1253 case LyXTabular::ALIGN_RIGHT:
1254 setAlign = LYX_ALIGN_RIGHT;
1257 case LyXTabular::M_ALIGN_CENTER:
1258 case LyXTabular::ALIGN_CENTER:
1259 setAlign = LYX_ALIGN_CENTER;
1262 case LyXTabular::ALIGN_BLOCK:
1263 setAlign = LYX_ALIGN_BLOCK;
1266 case LyXTabular::M_VALIGN_TOP:
1267 case LyXTabular::VALIGN_TOP:
1268 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1271 case LyXTabular::M_VALIGN_BOTTOM:
1272 case LyXTabular::VALIGN_BOTTOM:
1273 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1276 case LyXTabular::M_VALIGN_MIDDLE:
1277 case LyXTabular::VALIGN_MIDDLE:
1278 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1285 recordUndo(cur, Undo::ATOMIC);
1287 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1288 row_type const row = tabular.row_of_cell(cur.idx());
1289 col_type const column = tabular.column_of_cell(cur.idx());
1291 LyXTabular::ltType ltt;
1295 case LyXTabular::SET_PWIDTH: {
1296 LyXLength const len(value);
1297 tabular.setColumnPWidth(cur.idx(), len);
1299 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1300 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1301 else if (!len.zero()
1302 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1303 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1307 case LyXTabular::SET_MPWIDTH:
1308 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1311 case LyXTabular::SET_SPECIAL_COLUMN:
1312 case LyXTabular::SET_SPECIAL_MULTI:
1313 tabular.setAlignSpecial(cur.idx(),value,feature);
1316 case LyXTabular::APPEND_ROW:
1317 // append the row into the tabular
1318 tabular.appendRow(bv.buffer()->params(), cur.idx());
1321 case LyXTabular::APPEND_COLUMN:
1322 // append the column into the tabular
1323 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1326 case LyXTabular::DELETE_ROW:
1327 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1328 tabular.deleteRow(sel_row_start);
1329 if (sel_row_start >= tabular.rows())
1331 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1334 cur.selection() = false;
1337 case LyXTabular::DELETE_COLUMN:
1338 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1339 tabular.deleteColumn(sel_col_start);
1340 if (sel_col_start >= tabular.columns())
1342 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1345 cur.selection() = false;
1348 case LyXTabular::M_TOGGLE_LINE_TOP:
1350 case LyXTabular::TOGGLE_LINE_TOP: {
1351 bool lineSet = !tabular.topLine(cur.idx(), flag);
1352 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1353 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1355 tabular.getCellNumber(i, j),
1360 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1362 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1363 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1364 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1365 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1366 tabular.setBottomLine(
1367 tabular.getCellNumber(i, j),
1373 case LyXTabular::M_TOGGLE_LINE_LEFT:
1375 case LyXTabular::TOGGLE_LINE_LEFT: {
1376 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1377 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1378 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1379 tabular.setLeftLine(
1380 tabular.getCellNumber(i,j),
1386 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1388 case LyXTabular::TOGGLE_LINE_RIGHT: {
1389 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1390 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1391 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1392 tabular.setRightLine(
1393 tabular.getCellNumber(i,j),
1399 case LyXTabular::M_ALIGN_LEFT:
1400 case LyXTabular::M_ALIGN_RIGHT:
1401 case LyXTabular::M_ALIGN_CENTER:
1403 case LyXTabular::ALIGN_LEFT:
1404 case LyXTabular::ALIGN_RIGHT:
1405 case LyXTabular::ALIGN_CENTER:
1406 case LyXTabular::ALIGN_BLOCK:
1407 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1408 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1409 tabular.setAlignment(
1410 tabular.getCellNumber(i, j),
1415 case LyXTabular::M_VALIGN_TOP:
1416 case LyXTabular::M_VALIGN_BOTTOM:
1417 case LyXTabular::M_VALIGN_MIDDLE:
1419 case LyXTabular::VALIGN_TOP:
1420 case LyXTabular::VALIGN_BOTTOM:
1421 case LyXTabular::VALIGN_MIDDLE:
1422 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1423 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1424 tabular.setVAlignment(
1425 tabular.getCellNumber(i, j),
1429 case LyXTabular::MULTICOLUMN: {
1430 if (sel_row_start != sel_row_end) {
1431 #ifdef WITH_WARNINGS
1432 #warning Need I say it ? This is horrible.
1434 Alert::error(_("Error setting multicolumn"),
1435 _("You cannot set multicolumn vertically."));
1438 if (!cur.selection()) {
1439 // just multicol for one single cell
1440 // check whether we are completely in a multicol
1441 if (tabular.isMultiColumn(cur.idx()))
1442 tabular.unsetMultiColumn(cur.idx());
1444 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1447 // we have a selection so this means we just add all this
1448 // cells to form a multicolumn cell
1449 idx_type const s_start = cur.selBegin().idx();
1450 idx_type const s_end = cur.selEnd().idx();
1451 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1452 cur.idx() = s_start;
1455 cur.selection() = false;
1459 case LyXTabular::SET_ALL_LINES:
1461 case LyXTabular::UNSET_ALL_LINES:
1462 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1463 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1464 tabular.setAllLines(
1465 tabular.getCellNumber(i,j), setLines);
1468 case LyXTabular::SET_LONGTABULAR:
1469 tabular.setLongTabular(true);
1472 case LyXTabular::UNSET_LONGTABULAR:
1473 tabular.setLongTabular(false);
1476 case LyXTabular::SET_ROTATE_TABULAR:
1477 tabular.setRotateTabular(true);
1480 case LyXTabular::UNSET_ROTATE_TABULAR:
1481 tabular.setRotateTabular(false);
1484 case LyXTabular::SET_ROTATE_CELL:
1485 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1486 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1487 tabular.setRotateCell(
1488 tabular.getCellNumber(i, j), true);
1491 case LyXTabular::UNSET_ROTATE_CELL:
1492 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1493 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1494 tabular.setRotateCell(
1495 tabular.getCellNumber(i, j), false);
1498 case LyXTabular::SET_USEBOX: {
1499 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1500 if (val == tabular.getUsebox(cur.idx()))
1501 val = LyXTabular::BOX_NONE;
1502 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1503 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1504 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1508 case LyXTabular::UNSET_LTFIRSTHEAD:
1510 case LyXTabular::SET_LTFIRSTHEAD:
1511 tabular.getRowOfLTFirstHead(row, ltt);
1512 checkLongtableSpecial(ltt, value, flag);
1513 tabular.setLTHead(row, flag, ltt, true);
1516 case LyXTabular::UNSET_LTHEAD:
1518 case LyXTabular::SET_LTHEAD:
1519 tabular.getRowOfLTHead(row, ltt);
1520 checkLongtableSpecial(ltt, value, flag);
1521 tabular.setLTHead(row, flag, ltt, false);
1524 case LyXTabular::UNSET_LTFOOT:
1526 case LyXTabular::SET_LTFOOT:
1527 tabular.getRowOfLTFoot(row, ltt);
1528 checkLongtableSpecial(ltt, value, flag);
1529 tabular.setLTFoot(row, flag, ltt, false);
1532 case LyXTabular::UNSET_LTLASTFOOT:
1534 case LyXTabular::SET_LTLASTFOOT:
1535 tabular.getRowOfLTLastFoot(row, ltt);
1536 checkLongtableSpecial(ltt, value, flag);
1537 tabular.setLTFoot(row, flag, ltt, true);
1540 case LyXTabular::SET_LTNEWPAGE:
1541 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1544 // dummy stuff just to avoid warnings
1545 case LyXTabular::LAST_ACTION:
1549 InsetTabularMailer(*this).updateDialog(&bv);
1553 bool InsetTabular::showInsetDialog(BufferView * bv) const
1555 InsetTabularMailer(*this).showDialog(bv);
1560 void InsetTabular::openLayoutDialog(BufferView * bv) const
1562 InsetTabularMailer(*this).showDialog(bv);
1566 void InsetTabular::getLabelList(Buffer const & buffer,
1567 vector<string> & list) const
1569 tabular.getLabelList(buffer, list);
1573 bool InsetTabular::copySelection(LCursor & cur)
1575 if (!cur.selection())
1580 getSelection(cur, rs, re, cs, ce);
1582 paste_tabular.reset(new LyXTabular(tabular));
1584 for (row_type i = 0; i < rs; ++i)
1585 paste_tabular->deleteRow(0);
1587 row_type const rows = re - rs + 1;
1588 while (paste_tabular->rows() > rows)
1589 paste_tabular->deleteRow(rows);
1591 paste_tabular->setTopLine(0, true, true);
1592 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1595 for (col_type i = 0; i < cs; ++i)
1596 paste_tabular->deleteColumn(0);
1598 col_type const columns = ce - cs + 1;
1599 while (paste_tabular->columns() > columns)
1600 paste_tabular->deleteColumn(columns);
1602 paste_tabular->setLeftLine(0, true, true);
1603 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1607 OutputParams const runparams;
1608 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1609 cur.bv().stuffClipboard(os.str());
1614 bool InsetTabular::pasteSelection(LCursor & cur)
1618 col_type const actcol = tabular.column_of_cell(cur.idx());
1619 row_type const actrow = tabular.row_of_cell(cur.idx());
1620 for (row_type r1 = 0, r2 = actrow;
1621 r1 < paste_tabular->rows() && r2 < tabular.rows();
1623 for (col_type c1 = 0, c2 = actcol;
1624 c1 < paste_tabular->columns() && c2 < tabular.columns();
1626 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1627 tabular.isPartOfMultiColumn(r2, c2))
1629 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1633 if (tabular.isPartOfMultiColumn(r2, c2)) {
1637 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1638 inset = paste_tabular->getCellInset(r1, c1);
1646 void InsetTabular::cutSelection(LCursor & cur)
1648 if (!cur.selection())
1651 bool const track = cur.buffer().params().tracking_changes;
1654 getSelection(cur, rs, re, cs, ce);
1655 for (row_type i = rs; i <= re; ++i)
1656 for (col_type j = cs; j <= ce; ++j)
1657 cell(tabular.getCellNumber(i, j))->clear(track);
1659 // cursor position might be invalid now
1660 cur.pos() = cur.lastpos();
1661 cur.clearSelection();
1665 bool InsetTabular::isRightToLeft(LCursor & cur) const
1667 BOOST_ASSERT(cur.size() > 1);
1668 Paragraph const & parentpar = cur[cur.size() - 2].paragraph();
1669 LCursor::pos_type const parentpos = cur[cur.size() - 2].pos();
1670 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1671 parentpos).language()->RightToLeft();
1675 void InsetTabular::getSelection(LCursor & cur,
1676 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1678 CursorSlice const & beg = cur.selBegin();
1679 CursorSlice const & end = cur.selEnd();
1680 cs = tabular.column_of_cell(beg.idx());
1681 ce = tabular.column_of_cell(end.idx());
1684 cs = tabular.column_of_cell(end.idx());
1686 ce = tabular.right_column_of_cell(end.idx());
1689 rs = tabular.row_of_cell(beg.idx());
1690 re = tabular.row_of_cell(end.idx());
1696 size_t InsetTabular::nargs() const
1698 return tabular.getNumberOfCells();
1702 LyXText * InsetTabular::getText(int idx) const
1704 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1708 void InsetTabular::markErased()
1710 for (idx_type idx = 0; idx < nargs(); ++idx)
1711 cell(idx)->markErased();
1715 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1718 idx_type const cell = tabular.getCellFromInset(in);
1719 // FIXME: getCellFromInset() returns now always a valid cell, so
1720 // the stuff below can be deleted, and instead we have:
1721 return tabular.getPWidth(cell).zero();
1724 return tabular.getPWidth(cell).zero();
1726 // this is a workaround for a crash (New, Insert->Tabular,
1727 // Insert->FootNote)
1731 // well we didn't obviously find it so maybe our owner knows more
1732 BOOST_ASSERT(owner());
1733 return owner()->forceDefaultParagraphs(in);
1739 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1742 if (buf.length() <= 0)
1747 col_type maxCols = 1;
1748 string::size_type const len = buf.length();
1749 string::size_type p = 0;
1751 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1759 maxCols = max(cols, maxCols);
1765 maxCols = max(cols, maxCols);
1766 LyXTabular * loctab;
1771 paste_tabular.reset(
1772 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1773 loctab = paste_tabular.get();
1777 cell = bv.cursor().idx();
1778 ocol = tabular.column_of_cell(cell);
1779 row = tabular.row_of_cell(cell);
1782 string::size_type op = 0;
1783 idx_type const cells = loctab->getNumberOfCells();
1786 rows = loctab->rows();
1787 col_type const columns = loctab->columns();
1789 while (cell < cells && p < len && row < rows &&
1790 (p = buf.find_first_of("\t\n", p)) != string::npos)
1796 // we can only set this if we are not too far right
1797 if (cols < columns) {
1798 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1799 Paragraph & par = inset->text_.getPar(0);
1800 LyXFont const font = inset->text_.getFont(par, 0);
1801 inset->setText(buf.substr(op, p - op), font);
1807 // we can only set this if we are not too far right
1808 if (cols < columns) {
1809 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1810 Paragraph & par = inset->text_.getPar(0);
1811 LyXFont const font = inset->text_.getFont(par, 0);
1812 inset->setText(buf.substr(op, p - op), font);
1817 cell = loctab->getCellNumber(row, cols);
1823 // check for the last cell if there is no trailing '\n'
1824 if (cell < cells && op < len) {
1825 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1826 Paragraph & par = inset->text_.getPar(0);
1827 LyXFont const font = inset->text_.getFont(par, 0);
1828 inset->setText(buf.substr(op, len - op), font);
1834 void InsetTabular::addPreview(PreviewLoader & loader) const
1836 row_type const rows = tabular.rows();
1837 col_type const columns = tabular.columns();
1838 for (row_type i = 0; i < rows; ++i) {
1839 for (col_type j = 0; j < columns; ++j)
1840 tabular.getCellInset(i, j)->addPreview(loader);
1845 bool InsetTabular::tablemode(LCursor & cur) const
1847 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1854 string const InsetTabularMailer::name_("tabular");
1856 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1857 : inset_(const_cast<InsetTabular &>(inset))
1861 string const InsetTabularMailer::inset2string(Buffer const &) const
1863 return params2string(inset_);
1867 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1869 istringstream data(in);
1871 lex.setStream(data);
1878 if (!lex || token != name_)
1879 return print_mailer_error("InsetTabularMailer", in, 1,
1882 // This is part of the inset proper that is usually swallowed
1883 // by Buffer::readInset
1885 if (!lex || token != "Tabular")
1886 return print_mailer_error("InsetTabularMailer", in, 2,
1889 Buffer const & buffer = inset.buffer();
1890 inset.read(buffer, lex);
1894 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1897 data << name_ << ' ';
1898 inset.write(inset.buffer(), data);
1899 data << "\\end_inset\n";