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 "frontends/Alert.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/Painter.h"
40 #include "frontends/nullpainter.h"
45 using lyx::graphics::PreviewLoader;
47 using lyx::support::ltrim;
48 using lyx::support::strToInt;
49 using lyx::support::strToDbl;
51 using boost::shared_ptr;
57 using std::istringstream;
59 using std::ostringstream;
66 int const ADD_TO_HEIGHT = 2;
67 int const ADD_TO_TABULAR_WIDTH = 2;
70 boost::scoped_ptr<LyXTabular> paste_tabular;
73 struct TabularFeature {
74 LyXTabular::Feature action;
79 TabularFeature tabularFeature[] =
81 { LyXTabular::APPEND_ROW, "append-row" },
82 { LyXTabular::APPEND_COLUMN, "append-column" },
83 { LyXTabular::DELETE_ROW, "delete-row" },
84 { LyXTabular::DELETE_COLUMN, "delete-column" },
85 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
86 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
87 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
88 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
89 { LyXTabular::ALIGN_LEFT, "align-left" },
90 { LyXTabular::ALIGN_RIGHT, "align-right" },
91 { LyXTabular::ALIGN_CENTER, "align-center" },
92 { LyXTabular::ALIGN_BLOCK, "align-block" },
93 { LyXTabular::VALIGN_TOP, "valign-top" },
94 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
95 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
96 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
97 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
98 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
99 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
100 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
101 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
102 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
103 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
104 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
105 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
106 { LyXTabular::MULTICOLUMN, "multicolumn" },
107 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
108 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
109 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
110 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
111 { LyXTabular::SET_PWIDTH, "set-pwidth" },
112 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
113 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
114 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
115 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
116 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
117 { LyXTabular::SET_USEBOX, "set-usebox" },
118 { LyXTabular::SET_LTHEAD, "set-lthead" },
119 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
120 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
121 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
122 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
123 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
124 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
125 { LyXTabular::LAST_ACTION, "" }
129 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
131 FeatureEqual(LyXTabular::Feature feature)
132 : feature_(feature) {}
133 bool operator()(TabularFeature const & tf) const {
134 return tf.action == feature_;
137 LyXTabular::Feature feature_;
143 string const featureAsString(LyXTabular::Feature feature)
145 TabularFeature * end = tabularFeature +
146 sizeof(tabularFeature) / sizeof(TabularFeature);
147 TabularFeature * it = std::find_if(tabularFeature, end,
148 FeatureEqual(feature));
149 return (it == end) ? string() : it->feature;
153 bool InsetTabular::hasPasteBuffer() const
155 return (paste_tabular.get() != 0);
159 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
160 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
161 buffer_(&buf), cursorx_(0)
165 InsetTabular::InsetTabular(InsetTabular const & tab)
166 : UpdatableInset(tab), tabular(tab.tabular),
167 buffer_(tab.buffer_), cursorx_(0)
171 InsetTabular::~InsetTabular()
173 InsetTabularMailer(*this).hideDialog();
177 auto_ptr<InsetBase> InsetTabular::doClone() const
179 return auto_ptr<InsetBase>(new InsetTabular(*this));
183 Buffer const & InsetTabular::buffer() const
189 void InsetTabular::buffer(Buffer const * b)
195 void InsetTabular::write(Buffer const & buf, ostream & os) const
197 os << "Tabular" << endl;
198 tabular.write(buf, os);
202 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
204 bool const old_format = (lex.getString() == "\\LyXTable");
206 tabular.read(buf, lex);
212 string token = lex.getString();
213 while (lex.isOK() && (token != "\\end_inset")) {
215 token = lex.getString();
217 if (token != "\\end_inset") {
218 lex.printError("Missing \\end_inset at this point. "
224 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
226 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
227 // mi.base.textwidth << "\n";
229 lyxerr << "InsetTabular::metrics: need bv" << endl;
233 for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
236 for (int j = 0; j < tabular.columns(); ++j) {
237 if (tabular.isPartOfMultiColumn(i, j))
242 LyXLength p_width = tabular.column_info[j].p_width;
243 if (!p_width.zero()) {
244 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
246 tabular.getCellInset(cell)->metrics(m, dim);
247 maxAsc = max(maxAsc, dim.asc);
248 maxDesc = max(maxDesc, dim.des);
249 tabular.setWidthOfCell(cell, dim.wid);
251 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
252 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
255 dim.asc = tabular.getAscentOfRow(0);
256 dim.des = tabular.getHeightOfTabular() - dim.asc;
257 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
262 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
264 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
265 BufferView * bv = pi.base.bv;
267 static NullPainter nop;
268 static PainterInfo nullpi(bv, nop);
270 resetPos(bv->cursor());
272 setPosCache(pi, x, y);
275 x += ADD_TO_TABULAR_WIDTH;
278 first_visible_cell = -1;
279 for (int i = 0; i < tabular.rows(); ++i) {
281 int const a = tabular.getAscentOfRow(i);
282 int const d = tabular.getDescentOfRow(i);
283 idx = tabular.getCellNumber(i, 0);
284 if (y + d <= 0 && y - a < pi.pain.paperHeight()) {
285 y += d + tabular.getAscentOfRow(i + 1)
286 + tabular.getAdditionalHeight(i + 1);
289 for (int j = 0; j < tabular.columns(); ++j) {
290 if (tabular.isPartOfMultiColumn(i, j))
292 if (first_visible_cell < 0)
293 first_visible_cell = idx;
295 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
296 if (nx + tabular.getWidthOfColumn(idx) < 0
297 || nx > bv->workWidth()
298 || y - a > bv->workHeight()) {
299 cell(idx)->draw(nullpi, cx, y);
300 drawCellLines(nop, nx, y, i, idx);
302 cell(idx)->draw(pi, cx, y);
303 drawCellLines(pi.pain, nx, y, i, idx);
305 nx += tabular.getWidthOfColumn(idx);
309 if (i + 1 < tabular.rows())
310 y += d + tabular.getAscentOfRow(i + 1) +
311 tabular.getAdditionalHeight(i + 1);
316 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
318 LCursor & cur = pi.base.bv->cursor();
319 if (!cur.selection())
321 if (!ptr_cmp(&cur.inset(), this))
325 x += ADD_TO_TABULAR_WIDTH;
327 if (tablemode(cur)) {
329 getSelection(cur, rs, re, cs, ce);
330 for (int j = 0; j < tabular.rows(); ++j) {
331 int const a = tabular.getAscentOfRow(j);
332 int const h = a + tabular.getDescentOfRow(j);
334 y += tabular.getAdditionalHeight(j);
335 for (int i = 0; i < tabular.columns(); ++i) {
336 if (tabular.isPartOfMultiColumn(j, i))
338 int const cell = tabular.getCellNumber(j, i);
339 int const w = tabular.getWidthOfColumn(cell);
340 if (i >= cs && i <= ce && j >= rs && j <= re)
341 pi.pain.fillRectangle(xx, y - a, w, h,
350 cur.text()->drawSelection(pi, x, y);
355 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
356 int row, int cell) const
358 int x2 = x + tabular.getWidthOfColumn(cell);
361 if (!tabular.topAlreadyDrawn(cell)) {
362 on_off = !tabular.topLine(cell);
363 pain.line(x, y - tabular.getAscentOfRow(row),
364 x2, y - tabular.getAscentOfRow(row),
365 on_off ? LColor::tabularonoffline : LColor::tabularline,
366 on_off ? Painter::line_onoffdash : Painter::line_solid);
368 on_off = !tabular.bottomLine(cell);
369 pain.line(x, y + tabular.getDescentOfRow(row),
370 x2, y + tabular.getDescentOfRow(row),
371 on_off ? LColor::tabularonoffline : LColor::tabularline,
372 on_off ? Painter::line_onoffdash : Painter::line_solid);
373 if (!tabular.leftAlreadyDrawn(cell)) {
374 on_off = !tabular.leftLine(cell);
375 pain.line(x, y - tabular.getAscentOfRow(row),
376 x, y + tabular.getDescentOfRow(row),
377 on_off ? LColor::tabularonoffline : LColor::tabularline,
378 on_off ? Painter::line_onoffdash : Painter::line_solid);
380 on_off = !tabular.rightLine(cell);
381 pain.line(x2 - tabular.getAdditionalWidth(cell),
382 y - tabular.getAscentOfRow(row),
383 x2 - tabular.getAdditionalWidth(cell),
384 y + tabular.getDescentOfRow(row),
385 on_off ? LColor::tabularonoffline : LColor::tabularline,
386 on_off ? Painter::line_onoffdash : Painter::line_solid);
390 string const InsetTabular::editMessage() const
392 return _("Opened table");
396 void InsetTabular::edit(LCursor & cur, bool left)
398 lyxerr << "InsetTabular::edit: " << this << endl;
400 cur.selection() = false;
403 if (isRightToLeft(cur))
404 cur.idx() = tabular.getLastCellInRow(0);
410 if (isRightToLeft(cur))
411 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
413 cur.idx() = tabular.getNumberOfCells() - 1;
415 cur.pos() = cur.lastpos(); // FIXME crude guess
417 // this accesses the position cache before it is initialized
419 //cur.bv().fitCursor();
423 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
425 //lyxerr << "InsetTabular::editXY: " << this << endl;
426 cur.selection() = false;
427 cur.push(const_cast<InsetTabular&>(*this));
428 return setPos(cur, x, y);
429 //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
433 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
435 lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
436 //lyxerr << " cur:\n" << cur << endl;
437 CursorSlice sl = cur.top();
438 LCursor & bvcur = cur.bv().cursor();
440 switch (cmd.action) {
442 case LFUN_MOUSE_PRESS:
443 lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
445 if (cmd.button() == mouse_button::button1) {
446 cur.selection() = false;
447 setPos(cur, cmd.x, cmd.y);
453 //if (cmd.button() == mouse_button::button2)
454 // dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
456 // we'll pop up the table dialog on release
457 if (cmd.button() == mouse_button::button3)
461 case LFUN_MOUSE_MOTION:
462 lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
463 if (cmd.button() == mouse_button::button1) {
464 // only accept motions to places not deeper nested than the real anchor
465 if (bvcur.anchor_.hasPart(cur)) {
466 setPos(cur, cmd.x, cmd.y);
467 bvcur.setCursor(cur);
468 bvcur.selection() = true;
474 case LFUN_MOUSE_RELEASE:
475 lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
476 if (cmd.button() == mouse_button::button3)
477 InsetTabularMailer(*this).showDialog(&cur.bv());
480 case LFUN_CELL_BACKWARD:
482 cur.selection() = false;
485 case LFUN_CELL_FORWARD:
487 cur.selection() = false;
490 case LFUN_SCROLL_INSET:
491 if (cmd.argument.empty())
493 if (cmd.argument.find('.') != cmd.argument.npos)
494 scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
496 scroll(cur.bv(), strToInt(cmd.argument));
501 cell(cur.idx())->dispatch(cur, cmd);
502 cur.dispatched(); // override the cell's decision
504 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
505 if (sl == cur.top()) {
506 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
513 cell(cur.idx())->dispatch(cur, cmd);
514 cur.dispatched(); // override the cell's decision
516 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
517 if (sl == cur.top()) {
518 cmd = FuncRequest(LFUN_FINISHED_LEFT);
525 cell(cur.idx())->dispatch(cur, cmd);
526 cur.dispatched(); // override the cell's decision
528 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
529 cur.idx() = tabular.getCellBelow(cur.idx());
534 if (sl == cur.top()) {
535 cmd = FuncRequest(LFUN_FINISHED_DOWN);
543 cell(cur.idx())->dispatch(cur, cmd);
544 cur.dispatched(); // override the cell's decision
546 if (tabular.row_of_cell(cur.idx()) != 0) {
547 cur.idx() = tabular.getCellAbove(cur.idx());
548 cur.pit() = cur.lastpit();
549 cur.pos() = cur.lastpos();
552 if (sl == cur.top()) {
553 cmd = FuncRequest(LFUN_FINISHED_UP);
560 // //if (hasSelection())
561 // // cur.selection() = false;
562 // int const col = tabular.column_of_cell(cur.idx());
563 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
564 // if (t < yo() + tabular.getHeightOfTabular()) {
565 // cur.bv().scrollDocView(t);
566 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
568 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
576 // case LFUN_PRIOR: {
577 // //if (hasSelection())
578 // // cur.selection() = false;
579 // int const col = tabular.column_of_cell(cur.idx());
580 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
582 // cur.bv().scrollDocView(t);
586 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
590 // cur.par() = cur.lastpar();
591 // cur.pos() = cur.lastpos();
596 case LFUN_LAYOUT_TABULAR:
597 InsetTabularMailer(*this).showDialog(&cur.bv());
600 case LFUN_INSET_DIALOG_UPDATE:
601 InsetTabularMailer(*this).updateDialog(&cur.bv());
604 case LFUN_TABULAR_FEATURE:
605 if (!tabularFeatures(cur, cmd.argument))
609 // insert file functions
610 case LFUN_FILE_INSERT_ASCII_PARA:
611 case LFUN_FILE_INSERT_ASCII: {
612 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
613 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
619 if (copySelection(cur)) {
620 recordUndo(cur, Undo::DELETE);
627 recordUndo(cur, Undo::DELETE);
631 cell(cur.idx())->dispatch(cur, cmd);
635 if (!cur.selection())
641 case LFUN_PASTESELECTION: {
642 string const clip = cur.bv().getClipboard();
645 if (clip.find('\t') != string::npos) {
649 size_t len = clip.length();
650 for (size_t p = 0; p < len; ++p) {
651 p = clip.find_first_of("\t\n", p);
652 if (p == string::npos)
661 maxCols = max(cols, maxCols);
666 maxCols = max(cols, maxCols);
669 new LyXTabular(cur.buffer().params(), rows, maxCols));
671 string::size_type op = 0;
673 int const cells = paste_tabular->getNumberOfCells();
676 for (size_t p = 0; cell < cells && p < len; ++p) {
677 p = clip.find_first_of("\t\n", p);
678 if (p == string::npos || p >= len)
682 paste_tabular->getCellInset(cell)->
683 setText(clip.substr(op, p - op), font);
688 paste_tabular->getCellInset(cell)->
689 setText(clip.substr(op, p - op), font);
690 while (cols++ < maxCols)
697 // check for the last cell if there is no trailing '\n'
698 if (cell < cells && op < len)
699 paste_tabular->getCellInset(cell)->
700 setText(clip.substr(op, len - op), font);
701 } else if (!insertAsciiString(cur.bv(), clip, true)) {
702 // so that the clipboard is used and it goes on
704 // and executes LFUN_PASTESELECTION in insettext!
705 paste_tabular.reset();
711 if (hasPasteBuffer()) {
712 recordUndo(cur, Undo::INSERT);
716 cell(cur.idx())->dispatch(cur, cmd);
720 // we try to handle this event in the insets dispatch function.
721 cell(cur.idx())->dispatch(cur, cmd);
725 InsetTabularMailer(*this).updateDialog(&cur.bv());
729 // function sets an object as defined in func_status.h:
730 // states OK, Unknown, Disabled, On, Off.
731 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
732 FuncStatus & status) const
734 switch (cmd.action) {
735 case LFUN_TABULAR_FEATURE: {
736 int action = LyXTabular::LAST_ACTION;
738 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
739 string const tmp = tabularFeature[i].feature;
740 if (tmp == cmd.argument.substr(0, tmp.length())) {
741 action = tabularFeature[i].action;
745 if (action == LyXTabular::LAST_ACTION) {
747 status.unknown(true);
751 string const argument
752 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
754 int sel_row_start = 0;
757 LyXTabular::ltType dummyltt;
760 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
763 case LyXTabular::SET_PWIDTH:
764 case LyXTabular::SET_MPWIDTH:
765 case LyXTabular::SET_SPECIAL_COLUMN:
766 case LyXTabular::SET_SPECIAL_MULTI:
767 case LyXTabular::APPEND_ROW:
768 case LyXTabular::APPEND_COLUMN:
769 case LyXTabular::DELETE_ROW:
770 case LyXTabular::DELETE_COLUMN:
771 case LyXTabular::SET_ALL_LINES:
772 case LyXTabular::UNSET_ALL_LINES:
776 case LyXTabular::MULTICOLUMN:
777 status.setOnOff(tabular.isMultiColumn(cur.idx()));
780 case LyXTabular::M_TOGGLE_LINE_TOP:
782 case LyXTabular::TOGGLE_LINE_TOP:
783 status.setOnOff(tabular.topLine(cur.idx(), flag));
786 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
788 case LyXTabular::TOGGLE_LINE_BOTTOM:
789 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
792 case LyXTabular::M_TOGGLE_LINE_LEFT:
794 case LyXTabular::TOGGLE_LINE_LEFT:
795 status.setOnOff(tabular.leftLine(cur.idx(), flag));
798 case LyXTabular::M_TOGGLE_LINE_RIGHT:
800 case LyXTabular::TOGGLE_LINE_RIGHT:
801 status.setOnOff(tabular.rightLine(cur.idx(), flag));
804 case LyXTabular::M_ALIGN_LEFT:
806 case LyXTabular::ALIGN_LEFT:
807 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
810 case LyXTabular::M_ALIGN_RIGHT:
812 case LyXTabular::ALIGN_RIGHT:
813 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
816 case LyXTabular::M_ALIGN_CENTER:
818 case LyXTabular::ALIGN_CENTER:
819 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
822 case LyXTabular::ALIGN_BLOCK:
823 status.enabled(!tabular.getPWidth(cur.idx()).zero());
824 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
827 case LyXTabular::M_VALIGN_TOP:
829 case LyXTabular::VALIGN_TOP:
831 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
834 case LyXTabular::M_VALIGN_BOTTOM:
836 case LyXTabular::VALIGN_BOTTOM:
838 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
841 case LyXTabular::M_VALIGN_MIDDLE:
843 case LyXTabular::VALIGN_MIDDLE:
845 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
848 case LyXTabular::SET_LONGTABULAR:
849 status.setOnOff(tabular.isLongTabular());
852 case LyXTabular::UNSET_LONGTABULAR:
853 status.setOnOff(!tabular.isLongTabular());
856 case LyXTabular::SET_ROTATE_TABULAR:
857 status.setOnOff(tabular.getRotateTabular());
860 case LyXTabular::UNSET_ROTATE_TABULAR:
861 status.setOnOff(!tabular.getRotateTabular());
864 case LyXTabular::SET_ROTATE_CELL:
865 status.setOnOff(tabular.getRotateCell(cur.idx()));
868 case LyXTabular::UNSET_ROTATE_CELL:
869 status.setOnOff(!tabular.getRotateCell(cur.idx()));
872 case LyXTabular::SET_USEBOX:
873 status.setOnOff(strToInt(argument) == tabular.getUsebox(cur.idx()));
876 case LyXTabular::SET_LTFIRSTHEAD:
877 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
880 case LyXTabular::SET_LTHEAD:
881 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
884 case LyXTabular::SET_LTFOOT:
885 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
888 case LyXTabular::SET_LTLASTFOOT:
889 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
892 case LyXTabular::SET_LTNEWPAGE:
893 status.setOnOff(tabular.getLTNewPage(sel_row_start));
898 status.enabled(false);
905 // we try to handle this event in the insets dispatch function.
906 return cell(cur.idx())->getStatus(cur, cmd, status);
911 int InsetTabular::latex(Buffer const & buf, ostream & os,
912 OutputParams const & runparams) const
914 return tabular.latex(buf, os, runparams);
918 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
919 OutputParams const & runparams) const
921 int const dp = runparams.linelen ? runparams.depth : 0;
922 return tabular.plaintext(buf, os, runparams, dp, false, 0);
926 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
927 OutputParams const & runparams) const
929 return tabular.linuxdoc(buf,os, runparams);
933 int InsetTabular::docbook(Buffer const & buf, ostream & os,
934 OutputParams const & runparams) const
937 InsetBase * master = 0;
940 #warning Why not pass a proper DocIterator here?
943 // if the table is inside a float it doesn't need the informaltable
944 // wrapper. Search for it.
945 for (master = owner(); master; master = master->owner())
946 if (master->lyxCode() == InsetBase::FLOAT_CODE)
951 os << "<informaltable>";
954 ret += tabular.docbook(buf, os, runparams);
956 os << "</informaltable>";
963 void InsetTabular::validate(LaTeXFeatures & features) const
965 tabular.validate(features);
969 shared_ptr<InsetText const> InsetTabular::cell(int idx) const
971 return tabular.getCellInset(idx);
975 shared_ptr<InsetText> InsetTabular::cell(int idx)
977 return tabular.getCellInset(idx);
981 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
983 cell(sl.idx())->getCursorPos(sl, x, y);
985 // y offset correction
986 int const row = tabular.row_of_cell(sl.idx());
987 for (int i = 0; i <= row; ++i) {
989 y += tabular.getAscentOfRow(i);
990 y += tabular.getAdditionalHeight(i);
993 y += tabular.getDescentOfRow(i);
996 // x offset correction
997 int const col = tabular.column_of_cell(sl.idx());
998 int idx = tabular.getCellNumber(row, 0);
999 for (int j = 0; j < col; ++j) {
1000 if (tabular.isPartOfMultiColumn(row, j))
1002 x += tabular.getWidthOfColumn(idx);
1005 x += tabular.getBeginningOfTextInCell(idx);
1006 x += ADD_TO_TABULAR_WIDTH;
1014 // Manhattan distance to nearest corner
1015 int dist(InsetOld const & inset, int x, int y)
1019 Point o = theCoords.insets_.xy(&inset);
1020 int const xo = o.x_;
1021 int const yo = o.y_;
1025 else if (x > xo + inset.width())
1026 xx = x - xo - inset.width();
1028 if (y < yo - inset.ascent())
1029 yy = yo - inset.ascent() - y;
1030 else if (y > yo + inset.descent())
1031 yy = y - yo - inset.descent();
1033 lyxerr << " xo_=" << xo << " yo_=" << yo
1034 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1035 << " descent=" << inset.descent()
1036 << " dist=" << xx + yy << endl;
1044 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
1046 lyxerr << "# InsetTabular::setPos() x=" << x << " y=" << y << endl;
1048 int dist_min = std::numeric_limits<int>::max();
1049 for (idx_type i = 0; i < nargs(); ++i) {
1050 if (theCoords.insets_.has(tabular.getCellInset(i).get())) {
1051 int d = dist(*tabular.getCellInset(i), x, y);
1058 cur.idx() = idx_min;
1059 //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
1060 return cell(cur.idx())->text_.editXY(cur, x, y);
1064 int InsetTabular::getCellXPos(int const cell) const
1068 for (; !tabular.isFirstCellInRow(c); --c)
1070 int lx = tabular.getWidthOfColumn(cell);
1071 for (; c < cell; ++c)
1072 lx += tabular.getWidthOfColumn(c);
1074 return lx - tabular.getWidthOfColumn(cell) + xo();
1078 void InsetTabular::resetPos(LCursor & cur) const
1082 BufferView & bv = cur.bv();
1084 // int const actcol = tabular.column_of_cell(cur.idx());
1085 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1086 // int const new_x = getCellXPos(cur.idx()) + offset;
1087 // int const old_x = cursorx_;
1088 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1089 // cursorx_ = new_x;
1090 // cursor.x(getCellXPos(cur.idx()) + offset);
1091 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1092 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1094 // scroll(bv, 0.0F);
1095 // } else if (cursorx_ - offset > 20 &&
1096 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1097 // scroll(bv, - col_width - 20);
1098 // } else if (cursorx_ - offset < 20) {
1099 // scroll(bv, 20 - cursorx_ + offset);
1100 // } else if (scroll() && xo() > 20 &&
1101 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1102 // scroll(bv, old_x - cursorx_);
1105 if (&cur.inset() != this) {
1109 int const X2 = bv.workWidth();
1110 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1111 int const col_width = tabular.getWidthOfColumn(cur.idx());
1112 int const x1 = getCellXPos(cur.idx()) + offset + scroll();
1113 int const x2 = x1 + col_width;
1116 scroll(bv, X1 + 20 - x1);
1117 else if (x2 > X2 - 20)
1118 scroll(bv, X2 - 20 - x2);
1123 InsetTabularMailer(*this).updateDialog(&bv);
1127 void InsetTabular::moveNextCell(LCursor & cur)
1129 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1130 if (isRightToLeft(cur)) {
1131 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1132 if (tabular.isFirstCellInRow(cur.idx())) {
1133 int const row = tabular.row_of_cell(cur.idx());
1134 if (row == tabular.rows() - 1)
1136 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1143 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1144 if (tabular.isLastCell(cur.idx()))
1150 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1155 void InsetTabular::movePrevCell(LCursor & cur)
1157 if (isRightToLeft(cur)) {
1158 if (tabular.isLastCellInRow(cur.idx())) {
1159 int const row = tabular.row_of_cell(cur.idx());
1162 cur.idx() = tabular.getFirstCellInRow(row);
1163 cur.idx() = tabular.getCellAbove(cur.idx());
1165 if (tabular.isLastCell(cur.idx()))
1170 if (cur.idx() == 0) // first cell
1180 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1182 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1185 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1186 string const tmp = tabularFeature[i].feature;
1188 if (tmp == what.substr(0, tmp.length())) {
1189 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1190 //tabularFeatures[i].feature.length())) {
1191 action = tabularFeature[i].action;
1195 if (action == LyXTabular::LAST_ACTION)
1199 ltrim(what.substr(tabularFeature[i].feature.length()));
1200 tabularFeatures(cur, action, val);
1207 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1208 string const & special, bool & flag)
1210 if (special == "dl_above") {
1213 } else if (special == "dl_below") {
1214 ltt.bottomDL = flag;
1216 } else if (special == "empty") {
1228 void InsetTabular::tabularFeatures(LCursor & cur,
1229 LyXTabular::Feature feature, string const & value)
1231 BufferView & bv = cur.bv();
1236 bool setLines = false;
1237 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1238 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1242 case LyXTabular::M_ALIGN_LEFT:
1243 case LyXTabular::ALIGN_LEFT:
1244 setAlign = LYX_ALIGN_LEFT;
1247 case LyXTabular::M_ALIGN_RIGHT:
1248 case LyXTabular::ALIGN_RIGHT:
1249 setAlign = LYX_ALIGN_RIGHT;
1252 case LyXTabular::M_ALIGN_CENTER:
1253 case LyXTabular::ALIGN_CENTER:
1254 setAlign = LYX_ALIGN_CENTER;
1257 case LyXTabular::ALIGN_BLOCK:
1258 setAlign = LYX_ALIGN_BLOCK;
1261 case LyXTabular::M_VALIGN_TOP:
1262 case LyXTabular::VALIGN_TOP:
1263 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1266 case LyXTabular::M_VALIGN_BOTTOM:
1267 case LyXTabular::VALIGN_BOTTOM:
1268 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1271 case LyXTabular::M_VALIGN_MIDDLE:
1272 case LyXTabular::VALIGN_MIDDLE:
1273 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1280 recordUndo(cur, Undo::ATOMIC);
1282 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1283 int const row = tabular.row_of_cell(cur.idx());
1284 int const column = tabular.column_of_cell(cur.idx());
1286 LyXTabular::ltType ltt;
1290 case LyXTabular::SET_PWIDTH: {
1291 LyXLength const len(value);
1292 tabular.setColumnPWidth(cur.idx(), len);
1294 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1295 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1296 else if (!len.zero()
1297 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1298 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1302 case LyXTabular::SET_MPWIDTH:
1303 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1306 case LyXTabular::SET_SPECIAL_COLUMN:
1307 case LyXTabular::SET_SPECIAL_MULTI:
1308 tabular.setAlignSpecial(cur.idx(),value,feature);
1311 case LyXTabular::APPEND_ROW:
1312 // append the row into the tabular
1313 tabular.appendRow(bv.buffer()->params(), cur.idx());
1316 case LyXTabular::APPEND_COLUMN:
1317 // append the column into the tabular
1318 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1321 case LyXTabular::DELETE_ROW:
1322 for (int i = sel_row_start; i <= sel_row_end; ++i)
1323 tabular.deleteRow(sel_row_start);
1324 if (sel_row_start >= tabular.rows())
1326 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1329 cur.selection() = false;
1332 case LyXTabular::DELETE_COLUMN:
1333 for (int i = sel_col_start; i <= sel_col_end; ++i)
1334 tabular.deleteColumn(sel_col_start);
1335 if (sel_col_start >= tabular.columns())
1337 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1340 cur.selection() = false;
1343 case LyXTabular::M_TOGGLE_LINE_TOP:
1345 case LyXTabular::TOGGLE_LINE_TOP: {
1346 bool lineSet = !tabular.topLine(cur.idx(), flag);
1347 for (int i = sel_row_start; i <= sel_row_end; ++i)
1348 for (int j = sel_col_start; j <= sel_col_end; ++j)
1350 tabular.getCellNumber(i, j),
1355 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1357 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1358 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1359 for (int i = sel_row_start; i <= sel_row_end; ++i)
1360 for (int j = sel_col_start; j <= sel_col_end; ++j)
1361 tabular.setBottomLine(
1362 tabular.getCellNumber(i, j),
1368 case LyXTabular::M_TOGGLE_LINE_LEFT:
1370 case LyXTabular::TOGGLE_LINE_LEFT: {
1371 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1372 for (int i = sel_row_start; i <= sel_row_end; ++i)
1373 for (int j = sel_col_start; j <= sel_col_end; ++j)
1374 tabular.setLeftLine(
1375 tabular.getCellNumber(i,j),
1381 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1383 case LyXTabular::TOGGLE_LINE_RIGHT: {
1384 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1385 for (int i = sel_row_start; i <= sel_row_end; ++i)
1386 for (int j = sel_col_start; j <= sel_col_end; ++j)
1387 tabular.setRightLine(
1388 tabular.getCellNumber(i,j),
1394 case LyXTabular::M_ALIGN_LEFT:
1395 case LyXTabular::M_ALIGN_RIGHT:
1396 case LyXTabular::M_ALIGN_CENTER:
1398 case LyXTabular::ALIGN_LEFT:
1399 case LyXTabular::ALIGN_RIGHT:
1400 case LyXTabular::ALIGN_CENTER:
1401 case LyXTabular::ALIGN_BLOCK:
1402 for (int i = sel_row_start; i <= sel_row_end; ++i)
1403 for (int j = sel_col_start; j <= sel_col_end; ++j)
1404 tabular.setAlignment(
1405 tabular.getCellNumber(i, j),
1410 case LyXTabular::M_VALIGN_TOP:
1411 case LyXTabular::M_VALIGN_BOTTOM:
1412 case LyXTabular::M_VALIGN_MIDDLE:
1414 case LyXTabular::VALIGN_TOP:
1415 case LyXTabular::VALIGN_BOTTOM:
1416 case LyXTabular::VALIGN_MIDDLE:
1417 for (int i = sel_row_start; i <= sel_row_end; ++i)
1418 for (int j = sel_col_start; j <= sel_col_end; ++j)
1419 tabular.setVAlignment(
1420 tabular.getCellNumber(i, j),
1424 case LyXTabular::MULTICOLUMN: {
1425 if (sel_row_start != sel_row_end) {
1426 #ifdef WITH_WARNINGS
1427 #warning Need I say it ? This is horrible.
1429 Alert::error(_("Error setting multicolumn"),
1430 _("You cannot set multicolumn vertically."));
1433 if (!cur.selection()) {
1434 // just multicol for one single cell
1435 // check whether we are completely in a multicol
1436 if (tabular.isMultiColumn(cur.idx()))
1437 tabular.unsetMultiColumn(cur.idx());
1439 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1442 // we have a selection so this means we just add all this
1443 // cells to form a multicolumn cell
1444 CursorSlice::idx_type const s_start = cur.selBegin().idx();
1445 CursorSlice::idx_type const s_end = cur.selEnd().idx();
1446 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1447 cur.idx() = s_start;
1450 cur.selection() = false;
1454 case LyXTabular::SET_ALL_LINES:
1456 case LyXTabular::UNSET_ALL_LINES:
1458 for (int i = sel_row_start; i <= sel_row_end; ++i)
1459 for (int j = sel_col_start; j <= sel_col_end; ++j)
1460 tabular.setAllLines(
1461 tabular.getCellNumber(i,j), setLines);
1465 case LyXTabular::SET_LONGTABULAR:
1466 tabular.setLongTabular(true);
1469 case LyXTabular::UNSET_LONGTABULAR:
1470 tabular.setLongTabular(false);
1473 case LyXTabular::SET_ROTATE_TABULAR:
1474 tabular.setRotateTabular(true);
1477 case LyXTabular::UNSET_ROTATE_TABULAR:
1478 tabular.setRotateTabular(false);
1481 case LyXTabular::SET_ROTATE_CELL:
1482 for (int i = sel_row_start; i <= sel_row_end; ++i)
1483 for (int j = sel_col_start; j <= sel_col_end; ++j)
1484 tabular.setRotateCell(
1485 tabular.getCellNumber(i, j), true);
1488 case LyXTabular::UNSET_ROTATE_CELL:
1489 for (int i = sel_row_start; i <= sel_row_end; ++i)
1490 for (int j = sel_col_start; j <= sel_col_end; ++j)
1491 tabular.setRotateCell(
1492 tabular.getCellNumber(i, j), false);
1495 case LyXTabular::SET_USEBOX: {
1496 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1497 if (val == tabular.getUsebox(cur.idx()))
1498 val = LyXTabular::BOX_NONE;
1499 for (int i = sel_row_start; i <= sel_row_end; ++i)
1500 for (int j = sel_col_start; j <= sel_col_end; ++j)
1501 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1505 case LyXTabular::UNSET_LTFIRSTHEAD:
1507 case LyXTabular::SET_LTFIRSTHEAD:
1508 tabular.getRowOfLTFirstHead(row, ltt);
1509 checkLongtableSpecial(ltt, value, flag);
1510 tabular.setLTHead(row, flag, ltt, true);
1513 case LyXTabular::UNSET_LTHEAD:
1515 case LyXTabular::SET_LTHEAD:
1516 tabular.getRowOfLTHead(row, ltt);
1517 checkLongtableSpecial(ltt, value, flag);
1518 tabular.setLTHead(row, flag, ltt, false);
1521 case LyXTabular::UNSET_LTFOOT:
1523 case LyXTabular::SET_LTFOOT:
1524 tabular.getRowOfLTFoot(row, ltt);
1525 checkLongtableSpecial(ltt, value, flag);
1526 tabular.setLTFoot(row, flag, ltt, false);
1529 case LyXTabular::UNSET_LTLASTFOOT:
1531 case LyXTabular::SET_LTLASTFOOT:
1532 tabular.getRowOfLTLastFoot(row, ltt);
1533 checkLongtableSpecial(ltt, value, flag);
1534 tabular.setLTFoot(row, flag, ltt, true);
1537 case LyXTabular::SET_LTNEWPAGE:
1538 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1541 // dummy stuff just to avoid warnings
1542 case LyXTabular::LAST_ACTION:
1546 InsetTabularMailer(*this).updateDialog(&bv);
1550 bool InsetTabular::showInsetDialog(BufferView * bv) const
1552 InsetTabularMailer(*this).showDialog(bv);
1557 void InsetTabular::openLayoutDialog(BufferView * bv) const
1559 InsetTabularMailer(*this).showDialog(bv);
1563 void InsetTabular::getLabelList(Buffer const & buffer,
1564 vector<string> & list) const
1566 tabular.getLabelList(buffer, list);
1570 bool InsetTabular::copySelection(LCursor & cur)
1572 if (!cur.selection())
1576 getSelection(cur, rs, re, cs, ce);
1578 paste_tabular.reset(new LyXTabular(tabular));
1580 for (int i = 0; i < rs; ++i)
1581 paste_tabular->deleteRow(0);
1583 int const rows = re - rs + 1;
1584 while (paste_tabular->rows() > rows)
1585 paste_tabular->deleteRow(rows);
1587 paste_tabular->setTopLine(0, true, true);
1588 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1591 for (int i = 0; i < cs; ++i)
1592 paste_tabular->deleteColumn(0);
1594 int const columns = ce - cs + 1;
1595 while (paste_tabular->columns() > columns)
1596 paste_tabular->deleteColumn(columns);
1598 paste_tabular->setLeftLine(0, true, true);
1599 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1603 OutputParams const runparams;
1604 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1605 cur.bv().stuffClipboard(os.str());
1610 bool InsetTabular::pasteSelection(LCursor & cur)
1614 int const actcol = tabular.column_of_cell(cur.idx());
1615 int const actrow = tabular.row_of_cell(cur.idx());
1616 for (int r1 = 0, r2 = actrow;
1617 r1 < paste_tabular->rows() && r2 < tabular.rows();
1619 for (int c1 = 0, c2 = actcol;
1620 c1 < paste_tabular->columns() && c2 < tabular.columns();
1622 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1623 tabular.isPartOfMultiColumn(r2, c2))
1625 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1629 if (tabular.isPartOfMultiColumn(r2, c2)) {
1633 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1634 inset = paste_tabular->getCellInset(r1, c1);
1642 void InsetTabular::cutSelection(LCursor & cur)
1644 if (!cur.selection())
1647 bool const track = cur.buffer().params().tracking_changes;
1649 getSelection(cur, rs, re, cs, ce);
1650 for (int i = rs; i <= re; ++i)
1651 for (int j = cs; j <= ce; ++j)
1652 cell(tabular.getCellNumber(i, j))->clear(track);
1654 // cursor position might be invalid now
1655 cur.pos() = cur.lastpos();
1656 cur.clearSelection();
1660 bool InsetTabular::isRightToLeft(LCursor & cur) const
1662 BOOST_ASSERT(cur.size() > 1);
1663 Paragraph const & parentpar = cur[cur.size() - 2].paragraph();
1664 LCursor::pos_type const parentpos = cur[cur.size() - 2].pos();
1665 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1666 parentpos).language()->RightToLeft();
1670 void InsetTabular::getSelection(LCursor & cur,
1671 int & rs, int & re, int & cs, int & ce) const
1673 CursorSlice const & beg = cur.selBegin();
1674 CursorSlice const & end = cur.selEnd();
1675 cs = tabular.column_of_cell(beg.idx());
1676 ce = tabular.column_of_cell(end.idx());
1679 cs = tabular.column_of_cell(end.idx());
1681 ce = tabular.right_column_of_cell(end.idx());
1684 rs = tabular.row_of_cell(beg.idx());
1685 re = tabular.row_of_cell(end.idx());
1691 size_t InsetTabular::nargs() const
1693 return tabular.getNumberOfCells();
1697 LyXText * InsetTabular::getText(int idx) const
1699 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1703 void InsetTabular::markErased()
1705 for (idx_type idx = 0; idx < nargs(); ++idx)
1706 cell(idx)->markErased();
1710 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1713 int const cell = tabular.getCellFromInset(in);
1716 return tabular.getPWidth(cell).zero();
1718 // this is a workaround for a crash (New, Insert->Tabular,
1719 // Insert->FootNote)
1723 // well we didn't obviously find it so maybe our owner knows more
1724 BOOST_ASSERT(owner());
1725 return owner()->forceDefaultParagraphs(in);
1731 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1734 if (buf.length() <= 0)
1740 string::size_type const len = buf.length();
1741 string::size_type p = 0;
1743 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1751 maxCols = max(cols, maxCols);
1757 maxCols = max(cols, maxCols);
1758 LyXTabular * loctab;
1763 paste_tabular.reset(
1764 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1765 loctab = paste_tabular.get();
1769 cell = bv.cursor().idx();
1770 ocol = tabular.column_of_cell(cell);
1771 row = tabular.row_of_cell(cell);
1774 string::size_type op = 0;
1775 int const cells = loctab->getNumberOfCells();
1778 rows = loctab->rows();
1779 int const columns = loctab->columns();
1781 while (cell < cells && p < len && row < rows &&
1782 (p = buf.find_first_of("\t\n", p)) != string::npos)
1788 // we can only set this if we are not too far right
1789 if (cols < columns) {
1790 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1791 Paragraph & par = inset->text_.getPar(0);
1792 LyXFont const font = inset->text_.getFont(par, 0);
1793 inset->setText(buf.substr(op, p - op), font);
1799 // we can only set this if we are not too far right
1800 if (cols < columns) {
1801 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1802 Paragraph & par = inset->text_.getPar(0);
1803 LyXFont const font = inset->text_.getFont(par, 0);
1804 inset->setText(buf.substr(op, p - op), font);
1809 cell = loctab->getCellNumber(row, cols);
1815 // check for the last cell if there is no trailing '\n'
1816 if (cell < cells && op < len) {
1817 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1818 Paragraph & par = inset->text_.getPar(0);
1819 LyXFont const font = inset->text_.getFont(par, 0);
1820 inset->setText(buf.substr(op, len - op), font);
1826 void InsetTabular::addPreview(PreviewLoader & loader) const
1828 int const rows = tabular.rows();
1829 int const columns = tabular.columns();
1830 for (int i = 0; i < rows; ++i) {
1831 for (int j = 0; j < columns; ++j)
1832 tabular.getCellInset(i, j)->addPreview(loader);
1837 bool InsetTabular::tablemode(LCursor & cur) const
1839 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1846 string const InsetTabularMailer::name_("tabular");
1848 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1849 : inset_(const_cast<InsetTabular &>(inset))
1853 string const InsetTabularMailer::inset2string(Buffer const &) const
1855 return params2string(inset_);
1859 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1861 istringstream data(in);
1863 lex.setStream(data);
1870 if (!lex || token != name_)
1871 return print_mailer_error("InsetTabularMailer", in, 1,
1874 // This is part of the inset proper that is usually swallowed
1875 // by Buffer::readInset
1877 if (!lex || token != "Tabular")
1878 return print_mailer_error("InsetTabularMailer", in, 2,
1881 Buffer const & buffer = inset.buffer();
1882 inset.read(buffer, lex);
1886 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1889 data << name_ << ' ';
1890 inset.write(inset.buffer(), data);
1891 data << "\\end_inset\n";