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 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
431 lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
432 //lyxerr << " cur:\n" << cur << endl;
433 CursorSlice sl = cur.top();
434 LCursor & bvcur = cur.bv().cursor();
436 switch (cmd.action) {
438 case LFUN_MOUSE_PRESS:
439 lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
441 if (cmd.button() == mouse_button::button1) {
442 cur.selection() = false;
443 setCursorFromCoordinates(cur, cmd.x, cmd.y);
449 if (cmd.button() == mouse_button::button2) {
450 // FIXME: pasting multiple cells (with insettabular's own
451 // LFUN_PASTESELECTION still does not work! (jspitzm)
452 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
453 cell(cur.idx())->dispatch(cur, cmd);
457 // we'll pop up the table dialog on release
458 if (cmd.button() == mouse_button::button3)
462 case LFUN_MOUSE_MOTION:
463 lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
464 if (cmd.button() == mouse_button::button1) {
465 // only accept motions to places not deeper nested than the real anchor
466 if (bvcur.anchor_.hasPart(cur)) {
467 setCursorFromCoordinates(cur, cmd.x, cmd.y);
468 bvcur.setCursor(cur);
469 bvcur.selection() = true;
475 case LFUN_MOUSE_RELEASE:
476 lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
477 if (cmd.button() == mouse_button::button3)
478 InsetTabularMailer(*this).showDialog(&cur.bv());
481 case LFUN_CELL_BACKWARD:
483 cur.selection() = false;
486 case LFUN_CELL_FORWARD:
488 cur.selection() = false;
491 case LFUN_SCROLL_INSET:
492 if (cmd.argument.empty())
494 if (cmd.argument.find('.') != cmd.argument.npos)
495 scroll(cur.bv(), static_cast<float>(convert<double>(cmd.argument)));
497 scroll(cur.bv(), convert<int>(cmd.argument));
502 cell(cur.idx())->dispatch(cur, cmd);
503 cur.dispatched(); // override the cell's decision
505 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
506 if (sl == cur.top()) {
507 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
515 cell(cur.idx())->dispatch(cur, cmd);
516 cur.dispatched(); // override the cell's decision
518 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
519 if (sl == cur.top()) {
520 cmd = FuncRequest(LFUN_FINISHED_LEFT);
528 cell(cur.idx())->dispatch(cur, cmd);
529 cur.dispatched(); // override the cell's decision
531 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
532 cur.idx() = tabular.getCellBelow(cur.idx());
537 if (sl == cur.top()) {
538 cmd = FuncRequest(LFUN_FINISHED_DOWN);
546 cell(cur.idx())->dispatch(cur, cmd);
547 cur.dispatched(); // override the cell's decision
549 if (tabular.row_of_cell(cur.idx()) != 0) {
550 cur.idx() = tabular.getCellAbove(cur.idx());
551 cur.pit() = cur.lastpit();
552 cur.pos() = cur.lastpos();
555 if (sl == cur.top()) {
556 cmd = FuncRequest(LFUN_FINISHED_UP);
563 // //if (hasSelection())
564 // // cur.selection() = false;
565 // col_type const col = tabular.column_of_cell(cur.idx());
566 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
567 // if (t < yo() + tabular.getHeightOfTabular()) {
568 // cur.bv().scrollDocView(t);
569 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
571 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
579 // case LFUN_PRIOR: {
580 // //if (hasSelection())
581 // // cur.selection() = false;
582 // col_type const col = tabular.column_of_cell(cur.idx());
583 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
585 // cur.bv().scrollDocView(t);
589 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
593 // cur.par() = cur.lastpar();
594 // cur.pos() = cur.lastpos();
599 case LFUN_LAYOUT_TABULAR:
600 InsetTabularMailer(*this).showDialog(&cur.bv());
603 case LFUN_INSET_DIALOG_UPDATE:
604 InsetTabularMailer(*this).updateDialog(&cur.bv());
607 case LFUN_TABULAR_FEATURE:
608 if (!tabularFeatures(cur, cmd.argument))
612 // insert file functions
613 case LFUN_FILE_INSERT_ASCII_PARA:
614 case LFUN_FILE_INSERT_ASCII: {
615 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
616 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
622 if (tablemode(cur)) {
623 if (copySelection(cur)) {
624 recordUndo(cur, Undo::DELETE);
629 cell(cur.idx())->dispatch(cur, cmd);
634 recordUndo(cur, Undo::DELETE);
638 cell(cur.idx())->dispatch(cur, cmd);
642 if (!cur.selection())
644 if (tablemode(cur)) {
648 cell(cur.idx())->dispatch(cur, cmd);
651 case LFUN_PASTESELECTION: {
652 string const clip = cur.bv().getClipboard();
655 if (clip.find('\t') != string::npos) {
658 col_type maxCols = 1;
659 size_t len = clip.length();
660 for (size_t p = 0; p < len; ++p) {
661 p = clip.find_first_of("\t\n", p);
662 if (p == string::npos)
671 maxCols = max(cols, maxCols);
676 maxCols = max(cols, maxCols);
679 new LyXTabular(cur.buffer().params(), rows, maxCols));
681 string::size_type op = 0;
683 idx_type const cells =
684 paste_tabular->getNumberOfCells();
687 for (size_t p = 0; cell < cells && p < len; ++p) {
688 p = clip.find_first_of("\t\n", p);
689 if (p == string::npos || p >= len)
693 paste_tabular->getCellInset(cell)->
694 setText(clip.substr(op, p - op), font);
699 paste_tabular->getCellInset(cell)->
700 setText(clip.substr(op, p - op), font);
701 while (cols++ < maxCols)
708 // check for the last cell if there is no trailing '\n'
709 if (cell < cells && op < len)
710 paste_tabular->getCellInset(cell)->
711 setText(clip.substr(op, len - op), font);
712 } else if (!insertAsciiString(cur.bv(), clip, true)) {
713 // so that the clipboard is used and it goes on
715 // and executes LFUN_PASTESELECTION in insettext!
716 paste_tabular.reset();
722 if (hasPasteBuffer()) {
723 recordUndo(cur, Undo::INSERT);
727 cell(cur.idx())->dispatch(cur, cmd);
738 case LFUN_FREEFONT_APPLY:
739 case LFUN_FREEFONT_UPDATE:
743 case LFUN_CAPITALIZE_WORD:
744 case LFUN_UPCASE_WORD:
745 case LFUN_LOWCASE_WORD:
746 case LFUN_TRANSPOSE_CHARS:
747 if (tablemode(cur)) {
750 getSelection(cur, rs, re, cs, ce);
751 for (row_type i = rs; i <= re; ++i)
752 for (col_type j = cs; j <= ce; ++j) {
753 // cursor follows cell:
754 cur.idx() = tabular.getCellNumber(i, j);
755 // select this cell only:
758 cur.pos() = cur.top().lastpos();
761 cell(cur.idx())->dispatch(cur, cmd);
763 // Restore original selection
764 cur.idx() = tabular.getCellNumber(rs, cs);
767 cur.idx() = tabular.getCellNumber(re, ce);
768 cur.pos() = cur.top().lastpos();
773 cell(cur.idx())->dispatch(cur, cmd);
777 // we try to handle this event in the insets dispatch function.
778 cell(cur.idx())->dispatch(cur, cmd);
782 InsetTabularMailer(*this).updateDialog(&cur.bv());
786 // function sets an object as defined in func_status.h:
787 // states OK, Unknown, Disabled, On, Off.
788 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
789 FuncStatus & status) const
791 switch (cmd.action) {
792 case LFUN_TABULAR_FEATURE: {
793 int action = LyXTabular::LAST_ACTION;
795 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
796 string const tmp = tabularFeature[i].feature;
797 if (tmp == cmd.argument.substr(0, tmp.length())) {
798 action = tabularFeature[i].action;
802 if (action == LyXTabular::LAST_ACTION) {
804 status.unknown(true);
808 string const argument
809 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
811 row_type sel_row_start = 0;
812 row_type sel_row_end = 0;
814 LyXTabular::ltType dummyltt;
817 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
820 case LyXTabular::SET_PWIDTH:
821 case LyXTabular::SET_MPWIDTH:
822 case LyXTabular::SET_SPECIAL_COLUMN:
823 case LyXTabular::SET_SPECIAL_MULTI:
824 case LyXTabular::APPEND_ROW:
825 case LyXTabular::APPEND_COLUMN:
826 case LyXTabular::DELETE_ROW:
827 case LyXTabular::DELETE_COLUMN:
828 case LyXTabular::SET_ALL_LINES:
829 case LyXTabular::UNSET_ALL_LINES:
833 case LyXTabular::MULTICOLUMN:
834 status.setOnOff(tabular.isMultiColumn(cur.idx()));
837 case LyXTabular::M_TOGGLE_LINE_TOP:
839 case LyXTabular::TOGGLE_LINE_TOP:
840 status.setOnOff(tabular.topLine(cur.idx(), flag));
843 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
845 case LyXTabular::TOGGLE_LINE_BOTTOM:
846 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
849 case LyXTabular::M_TOGGLE_LINE_LEFT:
851 case LyXTabular::TOGGLE_LINE_LEFT:
852 status.setOnOff(tabular.leftLine(cur.idx(), flag));
855 case LyXTabular::M_TOGGLE_LINE_RIGHT:
857 case LyXTabular::TOGGLE_LINE_RIGHT:
858 status.setOnOff(tabular.rightLine(cur.idx(), flag));
861 case LyXTabular::M_ALIGN_LEFT:
863 case LyXTabular::ALIGN_LEFT:
864 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
867 case LyXTabular::M_ALIGN_RIGHT:
869 case LyXTabular::ALIGN_RIGHT:
870 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
873 case LyXTabular::M_ALIGN_CENTER:
875 case LyXTabular::ALIGN_CENTER:
876 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
879 case LyXTabular::ALIGN_BLOCK:
880 status.enabled(!tabular.getPWidth(cur.idx()).zero());
881 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
884 case LyXTabular::M_VALIGN_TOP:
886 case LyXTabular::VALIGN_TOP:
888 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
891 case LyXTabular::M_VALIGN_BOTTOM:
893 case LyXTabular::VALIGN_BOTTOM:
895 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
898 case LyXTabular::M_VALIGN_MIDDLE:
900 case LyXTabular::VALIGN_MIDDLE:
902 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
905 case LyXTabular::SET_LONGTABULAR:
906 status.setOnOff(tabular.isLongTabular());
909 case LyXTabular::UNSET_LONGTABULAR:
910 status.setOnOff(!tabular.isLongTabular());
913 case LyXTabular::SET_ROTATE_TABULAR:
914 status.setOnOff(tabular.getRotateTabular());
917 case LyXTabular::UNSET_ROTATE_TABULAR:
918 status.setOnOff(!tabular.getRotateTabular());
921 case LyXTabular::SET_ROTATE_CELL:
922 status.setOnOff(tabular.getRotateCell(cur.idx()));
925 case LyXTabular::UNSET_ROTATE_CELL:
926 status.setOnOff(!tabular.getRotateCell(cur.idx()));
929 case LyXTabular::SET_USEBOX:
930 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
933 case LyXTabular::SET_LTFIRSTHEAD:
934 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
937 case LyXTabular::SET_LTHEAD:
938 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
941 case LyXTabular::SET_LTFOOT:
942 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
945 case LyXTabular::SET_LTLASTFOOT:
946 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
949 case LyXTabular::SET_LTNEWPAGE:
950 status.setOnOff(tabular.getLTNewPage(sel_row_start));
955 status.enabled(false);
961 // These are only enabled inside tabular
962 case LFUN_CELL_BACKWARD:
963 case LFUN_CELL_FORWARD:
964 status.enabled(true);
967 // disable these with multiple cells selected
968 case LFUN_INSERT_CHARSTYLE:
969 case LFUN_INSET_FLOAT:
970 case LFUN_INSET_WIDE_FLOAT:
971 case LFUN_INSET_FOOTNOTE:
972 case LFUN_INSET_MARGINAL:
973 case LFUN_INSERT_MATH:
975 case LFUN_MATH_MUTATE:
976 case LFUN_MATH_DISPLAY:
977 case LFUN_INSERT_NOTE:
978 case LFUN_INSET_OPTARG:
979 case LFUN_INSERT_BOX:
980 case LFUN_INSERT_BRANCH:
981 case LFUN_INSET_WRAP:
982 case LFUN_INSET_ERT: {
983 if (tablemode(cur)) {
984 status.enabled(false);
990 // we try to handle this event in the insets dispatch function.
991 return cell(cur.idx())->getStatus(cur, cmd, status);
996 int InsetTabular::latex(Buffer const & buf, ostream & os,
997 OutputParams const & runparams) const
999 return tabular.latex(buf, os, runparams);
1003 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1004 OutputParams const & runparams) const
1006 int const dp = runparams.linelen ? runparams.depth : 0;
1007 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1011 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1012 OutputParams const & runparams) const
1014 return tabular.linuxdoc(buf,os, runparams);
1018 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1019 OutputParams const & runparams) const
1022 InsetBase * master = 0;
1024 #ifdef WITH_WARNINGS
1025 #warning Why not pass a proper DocIterator here?
1028 // if the table is inside a float it doesn't need the informaltable
1029 // wrapper. Search for it.
1030 for (master = owner(); master; master = master->owner())
1031 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1036 os << "<informaltable>";
1039 ret += tabular.docbook(buf, os, runparams);
1041 os << "</informaltable>";
1048 void InsetTabular::validate(LaTeXFeatures & features) const
1050 tabular.validate(features);
1054 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1056 return tabular.getCellInset(idx);
1060 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1062 return tabular.getCellInset(idx);
1066 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1068 cell(sl.idx())->getCursorPos(sl, x, y);
1070 // y offset correction
1071 int const row = tabular.row_of_cell(sl.idx());
1072 for (int i = 0; i <= row; ++i) {
1074 y += tabular.getAscentOfRow(i);
1075 y += tabular.getAdditionalHeight(i);
1078 y += tabular.getDescentOfRow(i);
1081 // x offset correction
1082 int const col = tabular.column_of_cell(sl.idx());
1083 int idx = tabular.getCellNumber(row, 0);
1084 for (int j = 0; j < col; ++j) {
1085 if (tabular.isPartOfMultiColumn(row, j))
1087 x += tabular.getWidthOfColumn(idx);
1090 x += tabular.getBeginningOfTextInCell(idx);
1091 x += ADD_TO_TABULAR_WIDTH;
1099 // Manhattan distance to nearest corner
1100 int dist(InsetOld const & inset, int x, int y)
1104 Point o = theCoords.getInsets().xy(&inset);
1105 int const xo = o.x_;
1106 int const yo = o.y_;
1110 else if (x > xo + inset.width())
1111 xx = x - xo - inset.width();
1113 if (y < yo - inset.ascent())
1114 yy = yo - inset.ascent() - y;
1115 else if (y > yo + inset.descent())
1116 yy = y - yo - inset.descent();
1118 lyxerr << " xo_=" << xo << " yo_=" << yo
1119 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1120 << " descent=" << inset.descent()
1121 << " dist=" << xx + yy << endl;
1129 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
1131 //lyxerr << "InsetTabular::editXY: " << this << endl;
1132 cur.selection() = false;
1133 cur.push(const_cast<InsetTabular&>(*this));
1134 cur.idx() = getNearestCell(x, y);
1136 return cell(cur.idx())->text_.editXY(cur, x, y);
1137 //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1141 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1143 //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1144 cur.idx() = getNearestCell(x, y);
1146 return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1150 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1152 lyxerr << "# InsetTabular::getNearestCell() x=" << x << " y=" << y << endl;
1153 idx_type idx_min = 0;
1154 int dist_min = std::numeric_limits<int>::max();
1155 for (idx_type i = 0; i < nargs(); ++i) {
1156 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1157 int d = dist(*tabular.getCellInset(i), x, y);
1168 int InsetTabular::getCellXPos(idx_type const cell) const
1172 for (; !tabular.isFirstCellInRow(c); --c)
1174 int lx = tabular.getWidthOfColumn(cell);
1175 for (; c < cell; ++c)
1176 lx += tabular.getWidthOfColumn(c);
1178 return lx - tabular.getWidthOfColumn(cell);
1182 void InsetTabular::resetPos(LCursor & cur) const
1184 BufferView & bv = cur.bv();
1185 // col_type const actcol = tabular.column_of_cell(cur.idx());
1186 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1187 // int const new_x = getCellXPos(cur.idx()) + offset;
1188 // int const old_x = cursorx_;
1189 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1190 // cursorx_ = new_x;
1191 // cursor.x(getCellXPos(cur.idx()) + offset);
1192 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1193 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1195 // scroll(bv, 0.0F);
1196 // } else if (cursorx_ - offset > 20 &&
1197 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1198 // scroll(bv, - col_width - 20);
1199 // } else if (cursorx_ - offset < 20) {
1200 // scroll(bv, 20 - cursorx_ + offset);
1201 // } else if (scroll() && xo() > 20 &&
1202 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1203 // scroll(bv, old_x - cursorx_);
1206 if (&cur.inset() != this) {
1210 int const X2 = bv.workWidth();
1211 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1212 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1213 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1216 scroll(bv, X1 + 20 - x1);
1218 scroll(bv, X2 - 20 - x2);
1223 InsetTabularMailer(*this).updateDialog(&bv);
1227 void InsetTabular::moveNextCell(LCursor & cur)
1229 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1230 if (isRightToLeft(cur)) {
1231 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1232 if (tabular.isFirstCellInRow(cur.idx())) {
1233 row_type const row = tabular.row_of_cell(cur.idx());
1234 if (row == tabular.rows() - 1)
1236 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1243 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1244 if (tabular.isLastCell(cur.idx()))
1250 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1255 void InsetTabular::movePrevCell(LCursor & cur)
1257 if (isRightToLeft(cur)) {
1258 if (tabular.isLastCellInRow(cur.idx())) {
1259 row_type const row = tabular.row_of_cell(cur.idx());
1262 cur.idx() = tabular.getFirstCellInRow(row);
1263 cur.idx() = tabular.getCellAbove(cur.idx());
1265 if (tabular.isLastCell(cur.idx()))
1270 if (cur.idx() == 0) // first cell
1274 cur.pit() = cur.lastpit();
1275 cur.pos() = cur.lastpos();
1280 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1282 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1285 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1286 string const tmp = tabularFeature[i].feature;
1288 if (tmp == what.substr(0, tmp.length())) {
1289 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1290 //tabularFeatures[i].feature.length())) {
1291 action = tabularFeature[i].action;
1295 if (action == LyXTabular::LAST_ACTION)
1299 ltrim(what.substr(tabularFeature[i].feature.length()));
1300 tabularFeatures(cur, action, val);
1307 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1308 string const & special, bool & flag)
1310 if (special == "dl_above") {
1313 } else if (special == "dl_below") {
1314 ltt.bottomDL = flag;
1316 } else if (special == "empty") {
1328 void InsetTabular::tabularFeatures(LCursor & cur,
1329 LyXTabular::Feature feature, string const & value)
1331 BufferView & bv = cur.bv();
1332 col_type sel_col_start;
1333 col_type sel_col_end;
1334 row_type sel_row_start;
1335 row_type sel_row_end;
1336 bool setLines = false;
1337 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1338 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1342 case LyXTabular::M_ALIGN_LEFT:
1343 case LyXTabular::ALIGN_LEFT:
1344 setAlign = LYX_ALIGN_LEFT;
1347 case LyXTabular::M_ALIGN_RIGHT:
1348 case LyXTabular::ALIGN_RIGHT:
1349 setAlign = LYX_ALIGN_RIGHT;
1352 case LyXTabular::M_ALIGN_CENTER:
1353 case LyXTabular::ALIGN_CENTER:
1354 setAlign = LYX_ALIGN_CENTER;
1357 case LyXTabular::ALIGN_BLOCK:
1358 setAlign = LYX_ALIGN_BLOCK;
1361 case LyXTabular::M_VALIGN_TOP:
1362 case LyXTabular::VALIGN_TOP:
1363 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1366 case LyXTabular::M_VALIGN_BOTTOM:
1367 case LyXTabular::VALIGN_BOTTOM:
1368 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1371 case LyXTabular::M_VALIGN_MIDDLE:
1372 case LyXTabular::VALIGN_MIDDLE:
1373 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1380 recordUndo(cur, Undo::ATOMIC);
1382 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1383 row_type const row = tabular.row_of_cell(cur.idx());
1384 col_type const column = tabular.column_of_cell(cur.idx());
1386 LyXTabular::ltType ltt;
1390 case LyXTabular::SET_PWIDTH: {
1391 LyXLength const len(value);
1392 tabular.setColumnPWidth(cur.idx(), len);
1394 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1395 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1396 else if (!len.zero()
1397 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1398 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1402 case LyXTabular::SET_MPWIDTH:
1403 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1406 case LyXTabular::SET_SPECIAL_COLUMN:
1407 case LyXTabular::SET_SPECIAL_MULTI:
1408 tabular.setAlignSpecial(cur.idx(),value,feature);
1411 case LyXTabular::APPEND_ROW:
1412 // append the row into the tabular
1413 tabular.appendRow(bv.buffer()->params(), cur.idx());
1416 case LyXTabular::APPEND_COLUMN:
1417 // append the column into the tabular
1418 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1421 case LyXTabular::DELETE_ROW:
1422 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1423 tabular.deleteRow(sel_row_start);
1424 if (sel_row_start >= tabular.rows())
1426 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1429 cur.selection() = false;
1432 case LyXTabular::DELETE_COLUMN:
1433 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1434 tabular.deleteColumn(sel_col_start);
1435 if (sel_col_start >= tabular.columns())
1437 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1440 cur.selection() = false;
1443 case LyXTabular::M_TOGGLE_LINE_TOP:
1445 case LyXTabular::TOGGLE_LINE_TOP: {
1446 bool lineSet = !tabular.topLine(cur.idx(), flag);
1447 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1448 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1450 tabular.getCellNumber(i, j),
1455 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1457 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1458 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1459 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1460 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1461 tabular.setBottomLine(
1462 tabular.getCellNumber(i, j),
1468 case LyXTabular::M_TOGGLE_LINE_LEFT:
1470 case LyXTabular::TOGGLE_LINE_LEFT: {
1471 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1472 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1473 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1474 tabular.setLeftLine(
1475 tabular.getCellNumber(i,j),
1481 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1483 case LyXTabular::TOGGLE_LINE_RIGHT: {
1484 bool lineSet = !tabular.rightLine(cur.idx(), flag);
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.setRightLine(
1488 tabular.getCellNumber(i,j),
1494 case LyXTabular::M_ALIGN_LEFT:
1495 case LyXTabular::M_ALIGN_RIGHT:
1496 case LyXTabular::M_ALIGN_CENTER:
1498 case LyXTabular::ALIGN_LEFT:
1499 case LyXTabular::ALIGN_RIGHT:
1500 case LyXTabular::ALIGN_CENTER:
1501 case LyXTabular::ALIGN_BLOCK:
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.setAlignment(
1505 tabular.getCellNumber(i, j),
1510 case LyXTabular::M_VALIGN_TOP:
1511 case LyXTabular::M_VALIGN_BOTTOM:
1512 case LyXTabular::M_VALIGN_MIDDLE:
1514 case LyXTabular::VALIGN_TOP:
1515 case LyXTabular::VALIGN_BOTTOM:
1516 case LyXTabular::VALIGN_MIDDLE:
1517 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1518 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1519 tabular.setVAlignment(
1520 tabular.getCellNumber(i, j),
1524 case LyXTabular::MULTICOLUMN: {
1525 if (sel_row_start != sel_row_end) {
1526 #ifdef WITH_WARNINGS
1527 #warning Need I say it ? This is horrible.
1529 Alert::error(_("Error setting multicolumn"),
1530 _("You cannot set multicolumn vertically."));
1533 if (!cur.selection()) {
1534 // just multicol for one single cell
1535 // check whether we are completely in a multicol
1536 if (tabular.isMultiColumn(cur.idx()))
1537 tabular.unsetMultiColumn(cur.idx());
1539 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1542 // we have a selection so this means we just add all this
1543 // cells to form a multicolumn cell
1544 idx_type const s_start = cur.selBegin().idx();
1545 idx_type const s_end = cur.selEnd().idx();
1546 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1547 cur.idx() = s_start;
1550 cur.selection() = false;
1554 case LyXTabular::SET_ALL_LINES:
1556 case LyXTabular::UNSET_ALL_LINES:
1557 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1558 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1559 tabular.setAllLines(
1560 tabular.getCellNumber(i,j), setLines);
1563 case LyXTabular::SET_LONGTABULAR:
1564 tabular.setLongTabular(true);
1567 case LyXTabular::UNSET_LONGTABULAR:
1568 tabular.setLongTabular(false);
1571 case LyXTabular::SET_ROTATE_TABULAR:
1572 tabular.setRotateTabular(true);
1575 case LyXTabular::UNSET_ROTATE_TABULAR:
1576 tabular.setRotateTabular(false);
1579 case LyXTabular::SET_ROTATE_CELL:
1580 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1581 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1582 tabular.setRotateCell(
1583 tabular.getCellNumber(i, j), true);
1586 case LyXTabular::UNSET_ROTATE_CELL:
1587 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1588 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1589 tabular.setRotateCell(
1590 tabular.getCellNumber(i, j), false);
1593 case LyXTabular::SET_USEBOX: {
1594 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1595 if (val == tabular.getUsebox(cur.idx()))
1596 val = LyXTabular::BOX_NONE;
1597 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1598 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1599 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1603 case LyXTabular::UNSET_LTFIRSTHEAD:
1605 case LyXTabular::SET_LTFIRSTHEAD:
1606 tabular.getRowOfLTFirstHead(row, ltt);
1607 checkLongtableSpecial(ltt, value, flag);
1608 tabular.setLTHead(row, flag, ltt, true);
1611 case LyXTabular::UNSET_LTHEAD:
1613 case LyXTabular::SET_LTHEAD:
1614 tabular.getRowOfLTHead(row, ltt);
1615 checkLongtableSpecial(ltt, value, flag);
1616 tabular.setLTHead(row, flag, ltt, false);
1619 case LyXTabular::UNSET_LTFOOT:
1621 case LyXTabular::SET_LTFOOT:
1622 tabular.getRowOfLTFoot(row, ltt);
1623 checkLongtableSpecial(ltt, value, flag);
1624 tabular.setLTFoot(row, flag, ltt, false);
1627 case LyXTabular::UNSET_LTLASTFOOT:
1629 case LyXTabular::SET_LTLASTFOOT:
1630 tabular.getRowOfLTLastFoot(row, ltt);
1631 checkLongtableSpecial(ltt, value, flag);
1632 tabular.setLTFoot(row, flag, ltt, true);
1635 case LyXTabular::SET_LTNEWPAGE:
1636 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1639 // dummy stuff just to avoid warnings
1640 case LyXTabular::LAST_ACTION:
1644 InsetTabularMailer(*this).updateDialog(&bv);
1648 bool InsetTabular::showInsetDialog(BufferView * bv) const
1650 InsetTabularMailer(*this).showDialog(bv);
1655 void InsetTabular::openLayoutDialog(BufferView * bv) const
1657 InsetTabularMailer(*this).showDialog(bv);
1661 void InsetTabular::getLabelList(Buffer const & buffer,
1662 vector<string> & list) const
1664 tabular.getLabelList(buffer, list);
1668 bool InsetTabular::copySelection(LCursor & cur)
1670 if (!cur.selection())
1675 getSelection(cur, rs, re, cs, ce);
1677 paste_tabular.reset(new LyXTabular(tabular));
1679 for (row_type i = 0; i < rs; ++i)
1680 paste_tabular->deleteRow(0);
1682 row_type const rows = re - rs + 1;
1683 while (paste_tabular->rows() > rows)
1684 paste_tabular->deleteRow(rows);
1686 paste_tabular->setTopLine(0, true, true);
1687 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1690 for (col_type i = 0; i < cs; ++i)
1691 paste_tabular->deleteColumn(0);
1693 col_type const columns = ce - cs + 1;
1694 while (paste_tabular->columns() > columns)
1695 paste_tabular->deleteColumn(columns);
1697 paste_tabular->setLeftLine(0, true, true);
1698 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1702 OutputParams const runparams;
1703 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1704 cur.bv().stuffClipboard(os.str());
1709 bool InsetTabular::pasteSelection(LCursor & cur)
1713 col_type const actcol = tabular.column_of_cell(cur.idx());
1714 row_type const actrow = tabular.row_of_cell(cur.idx());
1715 for (row_type r1 = 0, r2 = actrow;
1716 r1 < paste_tabular->rows() && r2 < tabular.rows();
1718 for (col_type c1 = 0, c2 = actcol;
1719 c1 < paste_tabular->columns() && c2 < tabular.columns();
1721 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1722 tabular.isPartOfMultiColumn(r2, c2))
1724 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1728 if (tabular.isPartOfMultiColumn(r2, c2)) {
1732 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1733 inset = paste_tabular->getCellInset(r1, c1);
1741 void InsetTabular::cutSelection(LCursor & cur)
1743 if (!cur.selection())
1746 bool const track = cur.buffer().params().tracking_changes;
1749 getSelection(cur, rs, re, cs, ce);
1750 for (row_type i = rs; i <= re; ++i)
1751 for (col_type j = cs; j <= ce; ++j)
1752 cell(tabular.getCellNumber(i, j))->clear(track);
1754 // cursor position might be invalid now
1755 cur.pos() = cur.lastpos();
1756 cur.clearSelection();
1760 bool InsetTabular::isRightToLeft(LCursor & cur) const
1762 BOOST_ASSERT(cur.depth() > 1);
1763 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1764 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1765 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1766 parentpos).language()->RightToLeft();
1770 void InsetTabular::getSelection(LCursor & cur,
1771 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1773 CursorSlice const & beg = cur.selBegin();
1774 CursorSlice const & end = cur.selEnd();
1775 cs = tabular.column_of_cell(beg.idx());
1776 ce = tabular.column_of_cell(end.idx());
1779 cs = tabular.column_of_cell(end.idx());
1781 ce = tabular.right_column_of_cell(end.idx());
1784 rs = tabular.row_of_cell(beg.idx());
1785 re = tabular.row_of_cell(end.idx());
1791 size_t InsetTabular::nargs() const
1793 return tabular.getNumberOfCells();
1797 LyXText * InsetTabular::getText(int idx) const
1799 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1803 void InsetTabular::markErased()
1805 for (idx_type idx = 0; idx < nargs(); ++idx)
1806 cell(idx)->markErased();
1810 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1813 idx_type const cell = tabular.getCellFromInset(in);
1814 // FIXME: getCellFromInset() returns now always a valid cell, so
1815 // the stuff below can be deleted, and instead we have:
1816 return tabular.getPWidth(cell).zero();
1819 return tabular.getPWidth(cell).zero();
1821 // this is a workaround for a crash (New, Insert->Tabular,
1822 // Insert->FootNote)
1826 // well we didn't obviously find it so maybe our owner knows more
1827 BOOST_ASSERT(owner());
1828 return owner()->forceDefaultParagraphs(in);
1834 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1837 if (buf.length() <= 0)
1842 col_type maxCols = 1;
1843 string::size_type const len = buf.length();
1844 string::size_type p = 0;
1846 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1854 maxCols = max(cols, maxCols);
1860 maxCols = max(cols, maxCols);
1861 LyXTabular * loctab;
1866 paste_tabular.reset(
1867 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1868 loctab = paste_tabular.get();
1872 cell = bv.cursor().idx();
1873 ocol = tabular.column_of_cell(cell);
1874 row = tabular.row_of_cell(cell);
1877 string::size_type op = 0;
1878 idx_type const cells = loctab->getNumberOfCells();
1881 rows = loctab->rows();
1882 col_type const columns = loctab->columns();
1884 while (cell < cells && p < len && row < rows &&
1885 (p = buf.find_first_of("\t\n", p)) != string::npos)
1891 // we can only set this if we are not too far right
1892 if (cols < columns) {
1893 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1894 Paragraph & par = inset->text_.getPar(0);
1895 LyXFont const font = inset->text_.getFont(par, 0);
1896 inset->setText(buf.substr(op, p - op), font);
1902 // we can only set this if we are not too far right
1903 if (cols < columns) {
1904 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1905 Paragraph & par = inset->text_.getPar(0);
1906 LyXFont const font = inset->text_.getFont(par, 0);
1907 inset->setText(buf.substr(op, p - op), font);
1912 cell = loctab->getCellNumber(row, cols);
1918 // check for the last cell if there is no trailing '\n'
1919 if (cell < cells && op < len) {
1920 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1921 Paragraph & par = inset->text_.getPar(0);
1922 LyXFont const font = inset->text_.getFont(par, 0);
1923 inset->setText(buf.substr(op, len - op), font);
1929 void InsetTabular::addPreview(PreviewLoader & loader) const
1931 row_type const rows = tabular.rows();
1932 col_type const columns = tabular.columns();
1933 for (row_type i = 0; i < rows; ++i) {
1934 for (col_type j = 0; j < columns; ++j)
1935 tabular.getCellInset(i, j)->addPreview(loader);
1940 bool InsetTabular::tablemode(LCursor & cur) const
1942 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1949 string const InsetTabularMailer::name_("tabular");
1951 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1952 : inset_(const_cast<InsetTabular &>(inset))
1956 string const InsetTabularMailer::inset2string(Buffer const &) const
1958 return params2string(inset_);
1962 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1964 istringstream data(in);
1966 lex.setStream(data);
1973 if (!lex || token != name_)
1974 return print_mailer_error("InsetTabularMailer", in, 1,
1977 // This is part of the inset proper that is usually swallowed
1978 // by Buffer::readInset
1980 if (!lex || token != "Tabular")
1981 return print_mailer_error("InsetTabularMailer", in, 2,
1984 Buffer const & buffer = inset.buffer();
1985 inset.read(buffer, lex);
1989 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1992 data << name_ << ' ';
1993 inset.write(inset.buffer(), data);
1994 data << "\\end_inset\n";