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_INSET_INSERT:
969 case LFUN_INSERT_CHARSTYLE:
970 case LFUN_INSET_FLOAT:
971 case LFUN_INSET_WIDE_FLOAT:
972 case LFUN_INSET_FOOTNOTE:
973 case LFUN_INSET_MARGINAL:
974 case LFUN_INSERT_MATH:
976 case LFUN_MATH_MUTATE:
977 case LFUN_MATH_DISPLAY:
978 case LFUN_INSERT_NOTE:
979 case LFUN_INSET_OPTARG:
980 case LFUN_INSERT_BOX:
981 case LFUN_INSERT_BRANCH:
982 case LFUN_INSET_WRAP:
983 case LFUN_INSET_ERT: {
984 if (tablemode(cur)) {
985 status.enabled(false);
990 case LFUN_INSET_MODIFY:
991 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
992 status.enabled(true);
998 // we try to handle this event in the insets dispatch function.
999 return cell(cur.idx())->getStatus(cur, cmd, status);
1004 int InsetTabular::latex(Buffer const & buf, ostream & os,
1005 OutputParams const & runparams) const
1007 return tabular.latex(buf, os, runparams);
1011 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1012 OutputParams const & runparams) const
1014 int const dp = runparams.linelen ? runparams.depth : 0;
1015 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1019 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1020 OutputParams const & runparams) const
1022 return tabular.linuxdoc(buf,os, runparams);
1026 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1027 OutputParams const & runparams) const
1030 InsetBase * master = 0;
1032 #ifdef WITH_WARNINGS
1033 #warning Why not pass a proper DocIterator here?
1036 // if the table is inside a float it doesn't need the informaltable
1037 // wrapper. Search for it.
1038 for (master = owner(); master; master = master->owner())
1039 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1044 os << "<informaltable>";
1047 ret += tabular.docbook(buf, os, runparams);
1049 os << "</informaltable>";
1056 void InsetTabular::validate(LaTeXFeatures & features) const
1058 tabular.validate(features);
1062 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1064 return tabular.getCellInset(idx);
1068 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1070 return tabular.getCellInset(idx);
1074 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1076 cell(sl.idx())->getCursorPos(sl, x, y);
1078 // y offset correction
1079 int const row = tabular.row_of_cell(sl.idx());
1080 for (int i = 0; i <= row; ++i) {
1082 y += tabular.getAscentOfRow(i);
1083 y += tabular.getAdditionalHeight(i);
1086 y += tabular.getDescentOfRow(i);
1089 // x offset correction
1090 int const col = tabular.column_of_cell(sl.idx());
1091 int idx = tabular.getCellNumber(row, 0);
1092 for (int j = 0; j < col; ++j) {
1093 if (tabular.isPartOfMultiColumn(row, j))
1095 x += tabular.getWidthOfColumn(idx);
1098 x += tabular.getBeginningOfTextInCell(idx);
1099 x += ADD_TO_TABULAR_WIDTH;
1107 // Manhattan distance to nearest corner
1108 int dist(InsetOld const & inset, int x, int y)
1112 Point o = theCoords.getInsets().xy(&inset);
1113 int const xo = o.x_;
1114 int const yo = o.y_;
1118 else if (x > xo + inset.width())
1119 xx = x - xo - inset.width();
1121 if (y < yo - inset.ascent())
1122 yy = yo - inset.ascent() - y;
1123 else if (y > yo + inset.descent())
1124 yy = y - yo - inset.descent();
1126 lyxerr << " xo_=" << xo << " yo_=" << yo
1127 << " width_=" << inset.width() << " ascent=" << inset.ascent()
1128 << " descent=" << inset.descent()
1129 << " dist=" << xx + yy << endl;
1137 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
1139 //lyxerr << "InsetTabular::editXY: " << this << endl;
1140 cur.selection() = false;
1141 cur.push(const_cast<InsetTabular&>(*this));
1142 cur.idx() = getNearestCell(x, y);
1144 return cell(cur.idx())->text_.editXY(cur, x, y);
1145 //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1149 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1151 //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1152 cur.idx() = getNearestCell(x, y);
1154 return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1158 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1160 lyxerr << "# InsetTabular::getNearestCell() x=" << x << " y=" << y << endl;
1161 idx_type idx_min = 0;
1162 int dist_min = std::numeric_limits<int>::max();
1163 for (idx_type i = 0; i < nargs(); ++i) {
1164 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1165 int d = dist(*tabular.getCellInset(i), x, y);
1176 int InsetTabular::getCellXPos(idx_type const cell) const
1180 for (; !tabular.isFirstCellInRow(c); --c)
1182 int lx = tabular.getWidthOfColumn(cell);
1183 for (; c < cell; ++c)
1184 lx += tabular.getWidthOfColumn(c);
1186 return lx - tabular.getWidthOfColumn(cell);
1190 void InsetTabular::resetPos(LCursor & cur) const
1192 BufferView & bv = cur.bv();
1193 // col_type const actcol = tabular.column_of_cell(cur.idx());
1194 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1195 // int const new_x = getCellXPos(cur.idx()) + offset;
1196 // int const old_x = cursorx_;
1197 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1198 // cursorx_ = new_x;
1199 // cursor.x(getCellXPos(cur.idx()) + offset);
1200 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1201 // tabular.getWidthOfTabular() < bv.workWidth()-20)
1203 // scroll(bv, 0.0F);
1204 // } else if (cursorx_ - offset > 20 &&
1205 // cursorx_ - offset + col_width > bv.workWidth() - 20) {
1206 // scroll(bv, - col_width - 20);
1207 // } else if (cursorx_ - offset < 20) {
1208 // scroll(bv, 20 - cursorx_ + offset);
1209 // } else if (scroll() && xo() > 20 &&
1210 // xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1211 // scroll(bv, old_x - cursorx_);
1214 if (&cur.inset() != this) {
1218 int const X2 = bv.workWidth();
1219 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1220 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1221 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1224 scroll(bv, X1 + 20 - x1);
1226 scroll(bv, X2 - 20 - x2);
1231 InsetTabularMailer(*this).updateDialog(&bv);
1235 void InsetTabular::moveNextCell(LCursor & cur)
1237 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1238 if (isRightToLeft(cur)) {
1239 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1240 if (tabular.isFirstCellInRow(cur.idx())) {
1241 row_type const row = tabular.row_of_cell(cur.idx());
1242 if (row == tabular.rows() - 1)
1244 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1251 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1252 if (tabular.isLastCell(cur.idx()))
1258 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1263 void InsetTabular::movePrevCell(LCursor & cur)
1265 if (isRightToLeft(cur)) {
1266 if (tabular.isLastCellInRow(cur.idx())) {
1267 row_type const row = tabular.row_of_cell(cur.idx());
1270 cur.idx() = tabular.getFirstCellInRow(row);
1271 cur.idx() = tabular.getCellAbove(cur.idx());
1273 if (tabular.isLastCell(cur.idx()))
1278 if (cur.idx() == 0) // first cell
1282 cur.pit() = cur.lastpit();
1283 cur.pos() = cur.lastpos();
1288 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1290 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1293 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1294 string const tmp = tabularFeature[i].feature;
1296 if (tmp == what.substr(0, tmp.length())) {
1297 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1298 //tabularFeatures[i].feature.length())) {
1299 action = tabularFeature[i].action;
1303 if (action == LyXTabular::LAST_ACTION)
1307 ltrim(what.substr(tabularFeature[i].feature.length()));
1308 tabularFeatures(cur, action, val);
1315 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1316 string const & special, bool & flag)
1318 if (special == "dl_above") {
1321 } else if (special == "dl_below") {
1322 ltt.bottomDL = flag;
1324 } else if (special == "empty") {
1336 void InsetTabular::tabularFeatures(LCursor & cur,
1337 LyXTabular::Feature feature, string const & value)
1339 BufferView & bv = cur.bv();
1340 col_type sel_col_start;
1341 col_type sel_col_end;
1342 row_type sel_row_start;
1343 row_type sel_row_end;
1344 bool setLines = false;
1345 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1346 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1350 case LyXTabular::M_ALIGN_LEFT:
1351 case LyXTabular::ALIGN_LEFT:
1352 setAlign = LYX_ALIGN_LEFT;
1355 case LyXTabular::M_ALIGN_RIGHT:
1356 case LyXTabular::ALIGN_RIGHT:
1357 setAlign = LYX_ALIGN_RIGHT;
1360 case LyXTabular::M_ALIGN_CENTER:
1361 case LyXTabular::ALIGN_CENTER:
1362 setAlign = LYX_ALIGN_CENTER;
1365 case LyXTabular::ALIGN_BLOCK:
1366 setAlign = LYX_ALIGN_BLOCK;
1369 case LyXTabular::M_VALIGN_TOP:
1370 case LyXTabular::VALIGN_TOP:
1371 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1374 case LyXTabular::M_VALIGN_BOTTOM:
1375 case LyXTabular::VALIGN_BOTTOM:
1376 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1379 case LyXTabular::M_VALIGN_MIDDLE:
1380 case LyXTabular::VALIGN_MIDDLE:
1381 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1388 recordUndo(cur, Undo::ATOMIC);
1390 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1391 row_type const row = tabular.row_of_cell(cur.idx());
1392 col_type const column = tabular.column_of_cell(cur.idx());
1394 LyXTabular::ltType ltt;
1398 case LyXTabular::SET_PWIDTH: {
1399 LyXLength const len(value);
1400 tabular.setColumnPWidth(cur.idx(), len);
1402 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1403 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1404 else if (!len.zero()
1405 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1406 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1410 case LyXTabular::SET_MPWIDTH:
1411 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1414 case LyXTabular::SET_SPECIAL_COLUMN:
1415 case LyXTabular::SET_SPECIAL_MULTI:
1416 tabular.setAlignSpecial(cur.idx(),value,feature);
1419 case LyXTabular::APPEND_ROW:
1420 // append the row into the tabular
1421 tabular.appendRow(bv.buffer()->params(), cur.idx());
1424 case LyXTabular::APPEND_COLUMN:
1425 // append the column into the tabular
1426 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1429 case LyXTabular::DELETE_ROW:
1430 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1431 tabular.deleteRow(sel_row_start);
1432 if (sel_row_start >= tabular.rows())
1434 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1437 cur.selection() = false;
1440 case LyXTabular::DELETE_COLUMN:
1441 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1442 tabular.deleteColumn(sel_col_start);
1443 if (sel_col_start >= tabular.columns())
1445 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1448 cur.selection() = false;
1451 case LyXTabular::M_TOGGLE_LINE_TOP:
1453 case LyXTabular::TOGGLE_LINE_TOP: {
1454 bool lineSet = !tabular.topLine(cur.idx(), flag);
1455 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1456 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1458 tabular.getCellNumber(i, j),
1463 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1465 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1466 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1467 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1468 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1469 tabular.setBottomLine(
1470 tabular.getCellNumber(i, j),
1476 case LyXTabular::M_TOGGLE_LINE_LEFT:
1478 case LyXTabular::TOGGLE_LINE_LEFT: {
1479 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1480 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1481 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1482 tabular.setLeftLine(
1483 tabular.getCellNumber(i,j),
1489 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1491 case LyXTabular::TOGGLE_LINE_RIGHT: {
1492 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1493 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1494 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1495 tabular.setRightLine(
1496 tabular.getCellNumber(i,j),
1502 case LyXTabular::M_ALIGN_LEFT:
1503 case LyXTabular::M_ALIGN_RIGHT:
1504 case LyXTabular::M_ALIGN_CENTER:
1506 case LyXTabular::ALIGN_LEFT:
1507 case LyXTabular::ALIGN_RIGHT:
1508 case LyXTabular::ALIGN_CENTER:
1509 case LyXTabular::ALIGN_BLOCK:
1510 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1511 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1512 tabular.setAlignment(
1513 tabular.getCellNumber(i, j),
1518 case LyXTabular::M_VALIGN_TOP:
1519 case LyXTabular::M_VALIGN_BOTTOM:
1520 case LyXTabular::M_VALIGN_MIDDLE:
1522 case LyXTabular::VALIGN_TOP:
1523 case LyXTabular::VALIGN_BOTTOM:
1524 case LyXTabular::VALIGN_MIDDLE:
1525 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1526 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1527 tabular.setVAlignment(
1528 tabular.getCellNumber(i, j),
1532 case LyXTabular::MULTICOLUMN: {
1533 if (sel_row_start != sel_row_end) {
1534 #ifdef WITH_WARNINGS
1535 #warning Need I say it ? This is horrible.
1537 Alert::error(_("Error setting multicolumn"),
1538 _("You cannot set multicolumn vertically."));
1541 if (!cur.selection()) {
1542 // just multicol for one single cell
1543 // check whether we are completely in a multicol
1544 if (tabular.isMultiColumn(cur.idx()))
1545 tabular.unsetMultiColumn(cur.idx());
1547 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1550 // we have a selection so this means we just add all this
1551 // cells to form a multicolumn cell
1552 idx_type const s_start = cur.selBegin().idx();
1553 idx_type const s_end = cur.selEnd().idx();
1554 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1555 cur.idx() = s_start;
1558 cur.selection() = false;
1562 case LyXTabular::SET_ALL_LINES:
1564 case LyXTabular::UNSET_ALL_LINES:
1565 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1566 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1567 tabular.setAllLines(
1568 tabular.getCellNumber(i,j), setLines);
1571 case LyXTabular::SET_LONGTABULAR:
1572 tabular.setLongTabular(true);
1575 case LyXTabular::UNSET_LONGTABULAR:
1576 tabular.setLongTabular(false);
1579 case LyXTabular::SET_ROTATE_TABULAR:
1580 tabular.setRotateTabular(true);
1583 case LyXTabular::UNSET_ROTATE_TABULAR:
1584 tabular.setRotateTabular(false);
1587 case LyXTabular::SET_ROTATE_CELL:
1588 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1589 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1590 tabular.setRotateCell(
1591 tabular.getCellNumber(i, j), true);
1594 case LyXTabular::UNSET_ROTATE_CELL:
1595 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1596 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1597 tabular.setRotateCell(
1598 tabular.getCellNumber(i, j), false);
1601 case LyXTabular::SET_USEBOX: {
1602 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1603 if (val == tabular.getUsebox(cur.idx()))
1604 val = LyXTabular::BOX_NONE;
1605 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1606 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1607 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1611 case LyXTabular::UNSET_LTFIRSTHEAD:
1613 case LyXTabular::SET_LTFIRSTHEAD:
1614 tabular.getRowOfLTFirstHead(row, ltt);
1615 checkLongtableSpecial(ltt, value, flag);
1616 tabular.setLTHead(row, flag, ltt, true);
1619 case LyXTabular::UNSET_LTHEAD:
1621 case LyXTabular::SET_LTHEAD:
1622 tabular.getRowOfLTHead(row, ltt);
1623 checkLongtableSpecial(ltt, value, flag);
1624 tabular.setLTHead(row, flag, ltt, false);
1627 case LyXTabular::UNSET_LTFOOT:
1629 case LyXTabular::SET_LTFOOT:
1630 tabular.getRowOfLTFoot(row, ltt);
1631 checkLongtableSpecial(ltt, value, flag);
1632 tabular.setLTFoot(row, flag, ltt, false);
1635 case LyXTabular::UNSET_LTLASTFOOT:
1637 case LyXTabular::SET_LTLASTFOOT:
1638 tabular.getRowOfLTLastFoot(row, ltt);
1639 checkLongtableSpecial(ltt, value, flag);
1640 tabular.setLTFoot(row, flag, ltt, true);
1643 case LyXTabular::SET_LTNEWPAGE:
1644 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1647 // dummy stuff just to avoid warnings
1648 case LyXTabular::LAST_ACTION:
1652 InsetTabularMailer(*this).updateDialog(&bv);
1656 bool InsetTabular::showInsetDialog(BufferView * bv) const
1658 InsetTabularMailer(*this).showDialog(bv);
1663 void InsetTabular::openLayoutDialog(BufferView * bv) const
1665 InsetTabularMailer(*this).showDialog(bv);
1669 void InsetTabular::getLabelList(Buffer const & buffer,
1670 vector<string> & list) const
1672 tabular.getLabelList(buffer, list);
1676 bool InsetTabular::copySelection(LCursor & cur)
1678 if (!cur.selection())
1683 getSelection(cur, rs, re, cs, ce);
1685 paste_tabular.reset(new LyXTabular(tabular));
1687 for (row_type i = 0; i < rs; ++i)
1688 paste_tabular->deleteRow(0);
1690 row_type const rows = re - rs + 1;
1691 while (paste_tabular->rows() > rows)
1692 paste_tabular->deleteRow(rows);
1694 paste_tabular->setTopLine(0, true, true);
1695 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1698 for (col_type i = 0; i < cs; ++i)
1699 paste_tabular->deleteColumn(0);
1701 col_type const columns = ce - cs + 1;
1702 while (paste_tabular->columns() > columns)
1703 paste_tabular->deleteColumn(columns);
1705 paste_tabular->setLeftLine(0, true, true);
1706 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1710 OutputParams const runparams;
1711 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1712 cur.bv().stuffClipboard(os.str());
1717 bool InsetTabular::pasteSelection(LCursor & cur)
1721 col_type const actcol = tabular.column_of_cell(cur.idx());
1722 row_type const actrow = tabular.row_of_cell(cur.idx());
1723 for (row_type r1 = 0, r2 = actrow;
1724 r1 < paste_tabular->rows() && r2 < tabular.rows();
1726 for (col_type c1 = 0, c2 = actcol;
1727 c1 < paste_tabular->columns() && c2 < tabular.columns();
1729 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1730 tabular.isPartOfMultiColumn(r2, c2))
1732 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1736 if (tabular.isPartOfMultiColumn(r2, c2)) {
1740 shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1741 inset = paste_tabular->getCellInset(r1, c1);
1749 void InsetTabular::cutSelection(LCursor & cur)
1751 if (!cur.selection())
1754 bool const track = cur.buffer().params().tracking_changes;
1757 getSelection(cur, rs, re, cs, ce);
1758 for (row_type i = rs; i <= re; ++i)
1759 for (col_type j = cs; j <= ce; ++j)
1760 cell(tabular.getCellNumber(i, j))->clear(track);
1762 // cursor position might be invalid now
1763 cur.pos() = cur.lastpos();
1764 cur.clearSelection();
1768 bool InsetTabular::isRightToLeft(LCursor & cur) const
1770 BOOST_ASSERT(cur.depth() > 1);
1771 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1772 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1773 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1774 parentpos).language()->RightToLeft();
1778 void InsetTabular::getSelection(LCursor & cur,
1779 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1781 CursorSlice const & beg = cur.selBegin();
1782 CursorSlice const & end = cur.selEnd();
1783 cs = tabular.column_of_cell(beg.idx());
1784 ce = tabular.column_of_cell(end.idx());
1787 cs = tabular.column_of_cell(end.idx());
1789 ce = tabular.right_column_of_cell(end.idx());
1792 rs = tabular.row_of_cell(beg.idx());
1793 re = tabular.row_of_cell(end.idx());
1799 size_t InsetTabular::nargs() const
1801 return tabular.getNumberOfCells();
1805 LyXText * InsetTabular::getText(int idx) const
1807 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1811 void InsetTabular::markErased()
1813 for (idx_type idx = 0; idx < nargs(); ++idx)
1814 cell(idx)->markErased();
1818 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1821 idx_type const cell = tabular.getCellFromInset(in);
1822 // FIXME: getCellFromInset() returns now always a valid cell, so
1823 // the stuff below can be deleted, and instead we have:
1824 return tabular.getPWidth(cell).zero();
1827 return tabular.getPWidth(cell).zero();
1829 // this is a workaround for a crash (New, Insert->Tabular,
1830 // Insert->FootNote)
1834 // well we didn't obviously find it so maybe our owner knows more
1835 BOOST_ASSERT(owner());
1836 return owner()->forceDefaultParagraphs(in);
1842 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1845 if (buf.length() <= 0)
1850 col_type maxCols = 1;
1851 string::size_type const len = buf.length();
1852 string::size_type p = 0;
1854 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1862 maxCols = max(cols, maxCols);
1868 maxCols = max(cols, maxCols);
1869 LyXTabular * loctab;
1874 paste_tabular.reset(
1875 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1876 loctab = paste_tabular.get();
1880 cell = bv.cursor().idx();
1881 ocol = tabular.column_of_cell(cell);
1882 row = tabular.row_of_cell(cell);
1885 string::size_type op = 0;
1886 idx_type const cells = loctab->getNumberOfCells();
1889 rows = loctab->rows();
1890 col_type const columns = loctab->columns();
1892 while (cell < cells && p < len && row < rows &&
1893 (p = buf.find_first_of("\t\n", p)) != string::npos)
1899 // we can only set this if we are not too far right
1900 if (cols < columns) {
1901 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1902 Paragraph & par = inset->text_.getPar(0);
1903 LyXFont const font = inset->text_.getFont(par, 0);
1904 inset->setText(buf.substr(op, p - op), font);
1910 // we can only set this if we are not too far right
1911 if (cols < columns) {
1912 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1913 Paragraph & par = inset->text_.getPar(0);
1914 LyXFont const font = inset->text_.getFont(par, 0);
1915 inset->setText(buf.substr(op, p - op), font);
1920 cell = loctab->getCellNumber(row, cols);
1926 // check for the last cell if there is no trailing '\n'
1927 if (cell < cells && op < len) {
1928 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1929 Paragraph & par = inset->text_.getPar(0);
1930 LyXFont const font = inset->text_.getFont(par, 0);
1931 inset->setText(buf.substr(op, len - op), font);
1937 void InsetTabular::addPreview(PreviewLoader & loader) const
1939 row_type const rows = tabular.rows();
1940 col_type const columns = tabular.columns();
1941 for (row_type i = 0; i < rows; ++i) {
1942 for (col_type j = 0; j < columns; ++j)
1943 tabular.getCellInset(i, j)->addPreview(loader);
1948 bool InsetTabular::tablemode(LCursor & cur) const
1950 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1957 string const InsetTabularMailer::name_("tabular");
1959 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1960 : inset_(const_cast<InsetTabular &>(inset))
1964 string const InsetTabularMailer::inset2string(Buffer const &) const
1966 return params2string(inset_);
1970 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1972 istringstream data(in);
1974 lex.setStream(data);
1981 if (!lex || token != name_)
1982 return print_mailer_error("InsetTabularMailer", in, 1,
1985 // This is part of the inset proper that is usually swallowed
1986 // by Buffer::readInset
1988 if (!lex || token != "Tabular")
1989 return print_mailer_error("InsetTabularMailer", in, 2,
1992 Buffer const & buffer = inset.buffer();
1993 inset.read(buffer, lex);
1997 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2000 data << name_ << ' ';
2001 inset.write(inset.buffer(), data);
2002 data << "\\end_inset\n";