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 "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
43 #include "frontends/nullpainter.h"
49 using lyx::cap::tabularStackDirty;
51 using lyx::graphics::PreviewLoader;
53 using lyx::support::ltrim;
55 using boost::shared_ptr;
61 using std::istringstream;
63 using std::ostringstream;
70 int const ADD_TO_HEIGHT = 2;
71 int const ADD_TO_TABULAR_WIDTH = 2;
74 boost::scoped_ptr<LyXTabular> paste_tabular;
77 struct TabularFeature {
78 LyXTabular::Feature action;
83 TabularFeature tabularFeature[] =
85 { LyXTabular::APPEND_ROW, "append-row" },
86 { LyXTabular::APPEND_COLUMN, "append-column" },
87 { LyXTabular::DELETE_ROW, "delete-row" },
88 { LyXTabular::DELETE_COLUMN, "delete-column" },
89 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
90 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
91 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
92 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
93 { LyXTabular::ALIGN_LEFT, "align-left" },
94 { LyXTabular::ALIGN_RIGHT, "align-right" },
95 { LyXTabular::ALIGN_CENTER, "align-center" },
96 { LyXTabular::ALIGN_BLOCK, "align-block" },
97 { LyXTabular::VALIGN_TOP, "valign-top" },
98 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
99 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
100 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
101 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
102 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
103 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
104 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
105 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
106 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
107 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
108 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
109 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
110 { LyXTabular::MULTICOLUMN, "multicolumn" },
111 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
112 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
113 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
114 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
115 { LyXTabular::SET_PWIDTH, "set-pwidth" },
116 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
117 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
118 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
119 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
120 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
121 { LyXTabular::SET_USEBOX, "set-usebox" },
122 { LyXTabular::SET_LTHEAD, "set-lthead" },
123 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
124 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
125 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
126 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
127 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
128 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
129 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
130 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
131 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
132 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
133 { LyXTabular::LAST_ACTION, "" }
137 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
139 FeatureEqual(LyXTabular::Feature feature)
140 : feature_(feature) {}
141 bool operator()(TabularFeature const & tf) const {
142 return tf.action == feature_;
145 LyXTabular::Feature feature_;
151 string const featureAsString(LyXTabular::Feature feature)
153 TabularFeature * end = tabularFeature +
154 sizeof(tabularFeature) / sizeof(TabularFeature);
155 TabularFeature * it = std::find_if(tabularFeature, end,
156 FeatureEqual(feature));
157 return (it == end) ? string() : it->feature;
161 bool InsetTabular::hasPasteBuffer() const
163 return (paste_tabular.get() != 0);
167 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
169 : tabular(buf.params(), max(rows, row_type(1)),
170 max(columns, col_type(1))), buffer_(&buf), scx_(0)
174 InsetTabular::InsetTabular(InsetTabular const & tab)
175 : InsetOld(tab), tabular(tab.tabular),
176 buffer_(tab.buffer_), scx_(0)
180 InsetTabular::~InsetTabular()
182 InsetTabularMailer(*this).hideDialog();
186 auto_ptr<InsetBase> InsetTabular::doClone() const
188 return auto_ptr<InsetBase>(new InsetTabular(*this));
192 Buffer const & InsetTabular::buffer() const
198 void InsetTabular::buffer(Buffer const * b)
204 void InsetTabular::write(Buffer const & buf, ostream & os) const
206 os << "Tabular" << endl;
207 tabular.write(buf, os);
211 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
213 bool const old_format = (lex.getString() == "\\LyXTable");
215 tabular.read(buf, lex);
221 string token = lex.getString();
222 while (lex.isOK() && (token != "\\end_inset")) {
224 token = lex.getString();
226 if (token != "\\end_inset") {
227 lex.printError("Missing \\end_inset at this point. "
233 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
235 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
236 // mi.base.textwidth << "\n";
238 lyxerr << "InsetTabular::metrics: need bv" << endl;
243 for (idx_type cell = 0; i < tabular.rows(); ++i) {
246 for (col_type j = 0; j < tabular.columns(); ++j) {
247 if (tabular.isPartOfMultiColumn(i, j))
251 LyXLength p_width = tabular.column_info[j].p_width;
253 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
254 tabular.getCellInset(cell)->metrics(m, dim);
256 dim.wid = m.base.textwidth;
257 maxAsc = max(maxAsc, dim.asc);
258 maxDesc = max(maxDesc, dim.des);
259 tabular.setWidthOfCell(cell, dim.wid);
262 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
263 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
266 dim.asc = tabular.getAscentOfRow(0);
267 dim.des = tabular.getHeightOfTabular() - dim.asc;
268 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
273 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
275 setPosCache(pi, x, y);
277 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
278 BufferView * bv = pi.base.bv;
280 static NullPainter nop;
281 static PainterInfo nullpi(bv, nop);
283 //resetPos(bv->cursor());
286 x += ADD_TO_TABULAR_WIDTH;
289 first_visible_cell = LyXTabular::npos;
290 for (row_type i = 0; i < tabular.rows(); ++i) {
292 int const a = tabular.getAscentOfRow(i);
293 int const d = tabular.getDescentOfRow(i);
294 idx = tabular.getCellNumber(i, 0);
295 for (col_type j = 0; j < tabular.columns(); ++j) {
296 if (tabular.isPartOfMultiColumn(i, j))
298 if (first_visible_cell == LyXTabular::npos)
299 first_visible_cell = idx;
301 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
302 if (nx + tabular.getWidthOfColumn(idx) < 0
303 || nx > bv->workWidth()
305 || y - a > bv->workHeight()) {
306 cell(idx)->draw(nullpi, cx, y);
307 drawCellLines(nop, nx, y, i, idx, pi.erased_);
309 cell(idx)->draw(pi, cx, y);
310 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
312 nx += tabular.getWidthOfColumn(idx);
316 if (i + 1 < tabular.rows())
317 y += d + tabular.getAscentOfRow(i + 1) +
318 tabular.getAdditionalHeight(i + 1);
323 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
325 setPosCache(pi, x, y);
327 LCursor & cur = pi.base.bv->cursor();
328 if (!cur.selection())
330 if (!ptr_cmp(&cur.inset(), this))
335 x += scx_ + ADD_TO_TABULAR_WIDTH;
337 if (tablemode(cur)) {
340 getSelection(cur, rs, re, cs, ce);
341 y -= tabular.getAscentOfRow(0);
342 for (row_type j = 0; j < tabular.rows(); ++j) {
343 int const a = tabular.getAscentOfRow(j);
344 int const h = a + tabular.getDescentOfRow(j);
346 y += tabular.getAdditionalHeight(j);
347 for (col_type i = 0; i < tabular.columns(); ++i) {
348 if (tabular.isPartOfMultiColumn(j, i))
350 idx_type const cell =
351 tabular.getCellNumber(j, i);
352 int const w = tabular.getWidthOfColumn(cell);
353 if (i >= cs && i <= ce && j >= rs && j <= re)
354 pi.pain.fillRectangle(xx, y, w, h,
363 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
368 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
369 row_type row, idx_type cell, bool erased) const
371 int x2 = x + tabular.getWidthOfColumn(cell);
373 LColor::color col = LColor::tabularline;
374 LColor::color onoffcol = LColor::tabularonoffline;
377 col = LColor::strikeout;
378 onoffcol = LColor::strikeout;
381 if (!tabular.topAlreadyDrawn(cell)) {
382 on_off = !tabular.topLine(cell);
383 pain.line(x, y - tabular.getAscentOfRow(row),
384 x2, y - tabular.getAscentOfRow(row),
385 on_off ? onoffcol : col,
386 on_off ? Painter::line_onoffdash : Painter::line_solid);
388 on_off = !tabular.bottomLine(cell);
389 pain.line(x, y + tabular.getDescentOfRow(row),
390 x2, y + tabular.getDescentOfRow(row),
391 on_off ? onoffcol : col,
392 on_off ? Painter::line_onoffdash : Painter::line_solid);
393 if (!tabular.leftAlreadyDrawn(cell)) {
394 on_off = !tabular.leftLine(cell);
395 pain.line(x, y - tabular.getAscentOfRow(row),
396 x, y + tabular.getDescentOfRow(row),
397 on_off ? onoffcol : col,
398 on_off ? Painter::line_onoffdash : Painter::line_solid);
400 on_off = !tabular.rightLine(cell);
401 pain.line(x2 - tabular.getAdditionalWidth(cell),
402 y - tabular.getAscentOfRow(row),
403 x2 - tabular.getAdditionalWidth(cell),
404 y + tabular.getDescentOfRow(row),
405 on_off ? onoffcol : col,
406 on_off ? Painter::line_onoffdash : Painter::line_solid);
410 string const InsetTabular::editMessage() const
412 return _("Opened table");
416 void InsetTabular::edit(LCursor & cur, bool left)
418 lyxerr << "InsetTabular::edit: " << this << endl;
420 cur.selection() = false;
423 if (isRightToLeft(cur))
424 cur.idx() = tabular.getLastCellInRow(0);
430 if (isRightToLeft(cur))
431 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
433 cur.idx() = tabular.getNumberOfCells() - 1;
435 cur.pos() = cur.lastpos(); // FIXME crude guess
437 // this accesses the position cache before it is initialized
439 //cur.bv().fitCursor();
443 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
445 lyxerr << "# InsetTabular::doDispatch: cmd: " << cmd << endl;
446 lyxerr << " cur:\n" << cur << endl;
447 CursorSlice sl = cur.top();
448 LCursor & bvcur = cur.bv().cursor();
450 switch (cmd.action) {
452 case LFUN_MOUSE_PRESS:
453 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
455 if (cmd.button() == mouse_button::button1) {
456 cur.selection() = false;
457 setCursorFromCoordinates(cur, cmd.x, cmd.y);
463 if (cmd.button() == mouse_button::button2) {
464 // FIXME: pasting multiple cells (with insettabular's own
465 // LFUN_PASTESELECTION still does not work! (jspitzm)
466 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
467 cell(cur.idx())->dispatch(cur, cmd);
471 // we'll pop up the table dialog on release
472 if (cmd.button() == mouse_button::button3)
476 case LFUN_MOUSE_MOTION:
477 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
478 if (cmd.button() == mouse_button::button1) {
479 // only accept motions to places not deeper nested than the real anchor
480 if (bvcur.anchor_.hasPart(cur)) {
481 setCursorFromCoordinates(cur, cmd.x, cmd.y);
482 bvcur.setCursor(cur);
483 bvcur.selection() = true;
489 case LFUN_MOUSE_RELEASE:
490 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
491 if (cmd.button() == mouse_button::button3)
492 InsetTabularMailer(*this).showDialog(&cur.bv());
495 case LFUN_CELL_BACKWARD:
497 cur.selection() = false;
500 case LFUN_CELL_FORWARD:
502 cur.selection() = false;
507 cell(cur.idx())->dispatch(cur, cmd);
508 cur.dispatched(); // override the cell's decision
510 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
511 if (sl == cur.top()) {
512 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
519 cell(cur.idx())->dispatch(cur, cmd);
520 cur.dispatched(); // override the cell's decision
522 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
523 if (sl == cur.top()) {
524 cmd = FuncRequest(LFUN_FINISHED_LEFT);
531 cell(cur.idx())->dispatch(cur, cmd);
532 cur.dispatched(); // override the cell's decision
534 // if our LyXText didn't do anything to the cursor
535 // then we try to put the cursor into the cell below
536 // setting also the right targetX.
537 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
538 cur.idx() = tabular.getCellBelow(cur.idx());
540 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
541 cur.pit(), 0, cur.targetX());
543 if (sl == cur.top()) {
544 // we trick it to go to the RIGHT after leaving the
546 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
553 cell(cur.idx())->dispatch(cur, cmd);
554 cur.dispatched(); // override the cell's decision
556 // if our LyXText didn't do anything to the cursor
557 // then we try to put the cursor into the cell above
558 // setting also the right targetX.
559 if (tabular.row_of_cell(cur.idx()) != 0) {
560 cur.idx() = tabular.getCellAbove(cur.idx());
561 cur.pit() = cur.lastpit();
562 LyXText const * text = cell(cur.idx())->getText(0);
563 cur.pos() = text->x2pos(
565 text->paragraphs().back().rows().size()-1,
568 if (sl == cur.top()) {
569 cmd = FuncRequest(LFUN_FINISHED_UP);
575 // //if (hasSelection())
576 // // cur.selection() = false;
577 // col_type const col = tabular.column_of_cell(cur.idx());
578 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
579 // if (t < yo() + tabular.getHeightOfTabular()) {
580 // cur.bv().scrollDocView(t);
581 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
583 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
590 // case LFUN_PRIOR: {
591 // //if (hasSelection())
592 // // cur.selection() = false;
593 // col_type const col = tabular.column_of_cell(cur.idx());
594 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
596 // cur.bv().scrollDocView(t);
600 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
604 // cur.par() = cur.lastpar();
605 // cur.pos() = cur.lastpos();
609 case LFUN_LAYOUT_TABULAR:
610 InsetTabularMailer(*this).showDialog(&cur.bv());
613 case LFUN_INSET_DIALOG_UPDATE:
614 InsetTabularMailer(*this).updateDialog(&cur.bv());
617 case LFUN_TABULAR_FEATURE:
618 if (!tabularFeatures(cur, cmd.argument))
622 // insert file functions
623 case LFUN_FILE_INSERT_ASCII_PARA:
624 case LFUN_FILE_INSERT_ASCII: {
625 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
626 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
632 if (tablemode(cur)) {
633 if (copySelection(cur)) {
634 recordUndoInset(cur, Undo::DELETE);
639 cell(cur.idx())->dispatch(cur, cmd);
644 if (tablemode(cur)) {
645 recordUndoInset(cur, Undo::DELETE);
649 cell(cur.idx())->dispatch(cur, cmd);
653 if (!cur.selection())
655 if (tablemode(cur)) {
659 cell(cur.idx())->dispatch(cur, cmd);
662 case LFUN_PASTESELECTION: {
663 string const clip = cur.bv().getClipboard();
666 if (clip.find('\t') != string::npos) {
669 col_type maxCols = 1;
670 size_t len = clip.length();
671 for (size_t p = 0; p < len; ++p) {
672 p = clip.find_first_of("\t\n", p);
673 if (p == string::npos)
682 maxCols = max(cols, maxCols);
687 maxCols = max(cols, maxCols);
690 new LyXTabular(cur.buffer().params(), rows, maxCols));
692 string::size_type op = 0;
694 idx_type const cells =
695 paste_tabular->getNumberOfCells();
698 for (size_t p = 0; cell < cells && p < len; ++p) {
699 p = clip.find_first_of("\t\n", p);
700 if (p == string::npos || p >= len)
704 paste_tabular->getCellInset(cell)->
705 setText(clip.substr(op, p - op), font);
710 paste_tabular->getCellInset(cell)->
711 setText(clip.substr(op, p - op), font);
712 while (cols++ < maxCols)
719 // check for the last cell if there is no trailing '\n'
720 if (cell < cells && op < len)
721 paste_tabular->getCellInset(cell)->
722 setText(clip.substr(op, len - op), font);
723 } else if (!insertAsciiString(cur.bv(), clip, true)) {
724 // so that the clipboard is used and it goes on
726 // and executes LFUN_PASTESELECTION in insettext!
727 paste_tabular.reset();
733 if (hasPasteBuffer() && tabularStackDirty()) {
734 recordUndoInset(cur, Undo::INSERT);
738 cell(cur.idx())->dispatch(cur, cmd);
749 case LFUN_FREEFONT_APPLY:
750 case LFUN_FREEFONT_UPDATE:
754 case LFUN_CAPITALIZE_WORD:
755 case LFUN_UPCASE_WORD:
756 case LFUN_LOWCASE_WORD:
757 case LFUN_TRANSPOSE_CHARS:
758 if (tablemode(cur)) {
761 getSelection(cur, rs, re, cs, ce);
762 for (row_type i = rs; i <= re; ++i) {
763 for (col_type j = cs; j <= ce; ++j) {
764 // cursor follows cell:
765 cur.idx() = tabular.getCellNumber(i, j);
766 // select this cell only:
769 cur.pos() = cur.top().lastpos();
772 cell(cur.idx())->dispatch(cur, cmd);
775 // Restore original selection
776 cur.idx() = tabular.getCellNumber(rs, cs);
779 cur.idx() = tabular.getCellNumber(re, ce);
780 cur.pos() = cur.top().lastpos();
785 cell(cur.idx())->dispatch(cur, cmd);
789 // we try to handle this event in the insets dispatch function.
790 cell(cur.idx())->dispatch(cur, cmd);
795 InsetTabularMailer(*this).updateDialog(&cur.bv());
799 // function sets an object as defined in func_status.h:
800 // states OK, Unknown, Disabled, On, Off.
801 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
802 FuncStatus & status) const
804 switch (cmd.action) {
805 case LFUN_TABULAR_FEATURE: {
806 int action = LyXTabular::LAST_ACTION;
808 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
809 string const tmp = tabularFeature[i].feature;
810 if (tmp == cmd.argument.substr(0, tmp.length())) {
811 action = tabularFeature[i].action;
815 if (action == LyXTabular::LAST_ACTION) {
817 status.unknown(true);
821 string const argument
822 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
824 row_type sel_row_start = 0;
825 row_type sel_row_end = 0;
827 LyXTabular::ltType dummyltt;
830 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
833 case LyXTabular::SET_PWIDTH:
834 case LyXTabular::SET_MPWIDTH:
835 case LyXTabular::SET_SPECIAL_COLUMN:
836 case LyXTabular::SET_SPECIAL_MULTI:
837 case LyXTabular::APPEND_ROW:
838 case LyXTabular::APPEND_COLUMN:
839 case LyXTabular::DELETE_ROW:
840 case LyXTabular::DELETE_COLUMN:
841 case LyXTabular::SET_ALL_LINES:
842 case LyXTabular::UNSET_ALL_LINES:
846 case LyXTabular::MULTICOLUMN:
847 status.setOnOff(tabular.isMultiColumn(cur.idx()));
850 case LyXTabular::M_TOGGLE_LINE_TOP:
852 case LyXTabular::TOGGLE_LINE_TOP:
853 status.setOnOff(tabular.topLine(cur.idx(), flag));
856 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
858 case LyXTabular::TOGGLE_LINE_BOTTOM:
859 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
862 case LyXTabular::M_TOGGLE_LINE_LEFT:
864 case LyXTabular::TOGGLE_LINE_LEFT:
865 status.setOnOff(tabular.leftLine(cur.idx(), flag));
868 case LyXTabular::M_TOGGLE_LINE_RIGHT:
870 case LyXTabular::TOGGLE_LINE_RIGHT:
871 status.setOnOff(tabular.rightLine(cur.idx(), flag));
874 case LyXTabular::M_ALIGN_LEFT:
876 case LyXTabular::ALIGN_LEFT:
877 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
880 case LyXTabular::M_ALIGN_RIGHT:
882 case LyXTabular::ALIGN_RIGHT:
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
886 case LyXTabular::M_ALIGN_CENTER:
888 case LyXTabular::ALIGN_CENTER:
889 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
892 case LyXTabular::ALIGN_BLOCK:
893 status.enabled(!tabular.getPWidth(cur.idx()).zero());
894 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
897 case LyXTabular::M_VALIGN_TOP:
899 case LyXTabular::VALIGN_TOP:
901 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
904 case LyXTabular::M_VALIGN_BOTTOM:
906 case LyXTabular::VALIGN_BOTTOM:
908 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
911 case LyXTabular::M_VALIGN_MIDDLE:
913 case LyXTabular::VALIGN_MIDDLE:
915 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
918 case LyXTabular::SET_LONGTABULAR:
919 status.setOnOff(tabular.isLongTabular());
922 case LyXTabular::UNSET_LONGTABULAR:
923 status.setOnOff(!tabular.isLongTabular());
926 case LyXTabular::SET_ROTATE_TABULAR:
927 status.setOnOff(tabular.getRotateTabular());
930 case LyXTabular::UNSET_ROTATE_TABULAR:
931 status.setOnOff(!tabular.getRotateTabular());
934 case LyXTabular::SET_ROTATE_CELL:
935 status.setOnOff(tabular.getRotateCell(cur.idx()));
938 case LyXTabular::UNSET_ROTATE_CELL:
939 status.setOnOff(!tabular.getRotateCell(cur.idx()));
942 case LyXTabular::SET_USEBOX:
943 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
946 case LyXTabular::SET_LTFIRSTHEAD:
947 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
950 case LyXTabular::UNSET_LTFIRSTHEAD:
951 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
954 case LyXTabular::SET_LTHEAD:
955 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
958 case LyXTabular::UNSET_LTHEAD:
959 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
962 case LyXTabular::SET_LTFOOT:
963 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
966 case LyXTabular::UNSET_LTFOOT:
967 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
970 case LyXTabular::SET_LTLASTFOOT:
971 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
974 case LyXTabular::UNSET_LTLASTFOOT:
975 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
978 case LyXTabular::SET_LTNEWPAGE:
979 status.setOnOff(tabular.getLTNewPage(sel_row_start));
984 status.enabled(false);
990 // These are only enabled inside tabular
991 case LFUN_CELL_BACKWARD:
992 case LFUN_CELL_FORWARD:
993 status.enabled(true);
996 // disable these with multiple cells selected
997 case LFUN_INSET_INSERT:
998 case LFUN_TABULAR_INSERT:
999 case LFUN_INSERT_CHARSTYLE:
1000 case LFUN_INSET_FLOAT:
1001 case LFUN_INSET_WIDE_FLOAT:
1002 case LFUN_INSET_FOOTNOTE:
1003 case LFUN_INSET_MARGINAL:
1004 case LFUN_INSERT_MATH:
1005 case LFUN_MATH_MODE:
1006 case LFUN_MATH_MUTATE:
1007 case LFUN_MATH_DISPLAY:
1008 case LFUN_INSERT_NOTE:
1009 case LFUN_INSET_OPTARG:
1010 case LFUN_INSERT_BOX:
1011 case LFUN_INSERT_BRANCH:
1012 case LFUN_INSET_WRAP:
1013 case LFUN_INSET_ERT: {
1014 if (tablemode(cur)) {
1015 status.enabled(false);
1018 return cell(cur.idx())->getStatus(cur, cmd, status);
1021 // disable in non-fixed-width cells
1022 case LFUN_BREAKLINE:
1023 case LFUN_BREAKPARAGRAPH:
1024 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1025 case LFUN_BREAKPARAGRAPH_SKIP: {
1026 if (tabular.getPWidth(cur.idx()).zero()) {
1027 status.enabled(false);
1030 return cell(cur.idx())->getStatus(cur, cmd, status);
1033 case LFUN_INSET_MODIFY:
1034 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1035 status.enabled(true);
1041 // we try to handle this event in the insets dispatch function.
1042 return cell(cur.idx())->getStatus(cur, cmd, status);
1047 int InsetTabular::latex(Buffer const & buf, ostream & os,
1048 OutputParams const & runparams) const
1050 return tabular.latex(buf, os, runparams);
1054 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1055 OutputParams const & runparams) const
1057 int const dp = runparams.linelen ? runparams.depth : 0;
1058 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1062 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1063 OutputParams const & runparams) const
1065 return tabular.linuxdoc(buf,os, runparams);
1069 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1070 OutputParams const & runparams) const
1073 InsetBase * master = 0;
1075 #ifdef WITH_WARNINGS
1076 #warning Why not pass a proper DocIterator here?
1079 // if the table is inside a float it doesn't need the informaltable
1080 // wrapper. Search for it.
1081 for (master = owner(); master; master = master->owner())
1082 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1087 os << "<informaltable>";
1090 ret += tabular.docbook(buf, os, runparams);
1092 os << "</informaltable>";
1099 void InsetTabular::validate(LaTeXFeatures & features) const
1101 tabular.validate(features);
1105 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1107 return tabular.getCellInset(idx);
1111 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1113 return tabular.getCellInset(idx);
1117 void InsetTabular::cursorPos
1118 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1120 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1122 // y offset correction
1123 int const row = tabular.row_of_cell(sl.idx());
1124 for (int i = 0; i <= row; ++i) {
1126 y += tabular.getAscentOfRow(i);
1127 y += tabular.getAdditionalHeight(i);
1130 y += tabular.getDescentOfRow(i);
1133 // x offset correction
1134 int const col = tabular.column_of_cell(sl.idx());
1135 int idx = tabular.getCellNumber(row, 0);
1136 for (int j = 0; j < col; ++j) {
1137 if (tabular.isPartOfMultiColumn(row, j))
1139 x += tabular.getWidthOfColumn(idx);
1142 x += tabular.getBeginningOfTextInCell(idx);
1143 x += ADD_TO_TABULAR_WIDTH;
1148 int InsetTabular::dist(idx_type const cell, int x, int y) const
1152 InsetBase const & inset = *tabular.getCellInset(cell);
1153 Point o = theCoords.getInsets().xy(&inset);
1154 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1155 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1156 int const ybeg = o.y_ - inset.ascent();
1157 row_type const row = tabular.row_of_cell(cell);
1158 int const rowheight = tabular.getAscentOfRow(row)
1159 + tabular.getDescentOfRow(row)
1160 + tabular.getAdditionalHeight(row);
1161 int const yend = ybeg + rowheight;
1173 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1174 // << " ybeg=" << ybeg << " yend=" << yend
1175 // << " xx=" << xx << " yy=" << yy
1176 // << " dist=" << xx + yy << endl;
1181 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1183 //lyxerr << "InsetTabular::editXY: " << this << endl;
1184 cur.selection() = false;
1186 cur.idx() = getNearestCell(x, y);
1188 return cell(cur.idx())->text_.editXY(cur, x, y);
1192 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1194 cur.idx() = getNearestCell(x, y);
1195 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1199 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1201 idx_type idx_min = 0;
1202 int dist_min = std::numeric_limits<int>::max();
1203 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1204 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1205 int const d = dist(i, x, y);
1216 int InsetTabular::getCellXPos(idx_type const cell) const
1220 for (; !tabular.isFirstCellInRow(c); --c)
1222 int lx = tabular.getWidthOfColumn(cell);
1223 for (; c < cell; ++c)
1224 lx += tabular.getWidthOfColumn(c);
1226 return lx - tabular.getWidthOfColumn(cell);
1230 void InsetTabular::resetPos(LCursor & cur) const
1232 BufferView & bv = cur.bv();
1233 int const maxwidth = bv.workWidth();
1235 if (&cur.inset() != this) {
1239 int const X2 = maxwidth;
1240 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1241 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1242 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1245 scx_ = X1 + 20 - x1;
1247 scx_ = X2 - 20 - x2;
1254 InsetTabularMailer(*this).updateDialog(&bv);
1258 void InsetTabular::moveNextCell(LCursor & cur)
1260 if (isRightToLeft(cur)) {
1261 if (tabular.isFirstCellInRow(cur.idx())) {
1262 row_type const row = tabular.row_of_cell(cur.idx());
1263 if (row == tabular.rows() - 1)
1265 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1272 if (tabular.isLastCell(cur.idx()))
1282 void InsetTabular::movePrevCell(LCursor & cur)
1284 if (isRightToLeft(cur)) {
1285 if (tabular.isLastCellInRow(cur.idx())) {
1286 row_type const row = tabular.row_of_cell(cur.idx());
1289 cur.idx() = tabular.getFirstCellInRow(row);
1290 cur.idx() = tabular.getCellAbove(cur.idx());
1292 if (tabular.isLastCell(cur.idx()))
1297 if (cur.idx() == 0) // first cell
1301 cur.pit() = cur.lastpit();
1302 cur.pos() = cur.lastpos();
1307 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1309 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1312 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1313 string const tmp = tabularFeature[i].feature;
1315 if (tmp == what.substr(0, tmp.length())) {
1316 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1317 //tabularFeatures[i].feature.length())) {
1318 action = tabularFeature[i].action;
1322 if (action == LyXTabular::LAST_ACTION)
1326 ltrim(what.substr(tabularFeature[i].feature.length()));
1327 tabularFeatures(cur, action, val);
1334 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1335 string const & special, bool & flag)
1337 if (special == "dl_above") {
1340 } else if (special == "dl_below") {
1341 ltt.bottomDL = flag;
1343 } else if (special == "empty") {
1355 void InsetTabular::tabularFeatures(LCursor & cur,
1356 LyXTabular::Feature feature, string const & value)
1358 BufferView & bv = cur.bv();
1359 col_type sel_col_start;
1360 col_type sel_col_end;
1361 row_type sel_row_start;
1362 row_type sel_row_end;
1363 bool setLines = false;
1364 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1365 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1369 case LyXTabular::M_ALIGN_LEFT:
1370 case LyXTabular::ALIGN_LEFT:
1371 setAlign = LYX_ALIGN_LEFT;
1374 case LyXTabular::M_ALIGN_RIGHT:
1375 case LyXTabular::ALIGN_RIGHT:
1376 setAlign = LYX_ALIGN_RIGHT;
1379 case LyXTabular::M_ALIGN_CENTER:
1380 case LyXTabular::ALIGN_CENTER:
1381 setAlign = LYX_ALIGN_CENTER;
1384 case LyXTabular::ALIGN_BLOCK:
1385 setAlign = LYX_ALIGN_BLOCK;
1388 case LyXTabular::M_VALIGN_TOP:
1389 case LyXTabular::VALIGN_TOP:
1390 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1393 case LyXTabular::M_VALIGN_BOTTOM:
1394 case LyXTabular::VALIGN_BOTTOM:
1395 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1398 case LyXTabular::M_VALIGN_MIDDLE:
1399 case LyXTabular::VALIGN_MIDDLE:
1400 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1407 recordUndoInset(cur, Undo::ATOMIC);
1409 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1410 row_type const row = tabular.row_of_cell(cur.idx());
1411 col_type const column = tabular.column_of_cell(cur.idx());
1413 LyXTabular::ltType ltt;
1417 case LyXTabular::SET_PWIDTH: {
1418 LyXLength const len(value);
1419 tabular.setColumnPWidth(cur.idx(), len);
1421 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1422 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1426 case LyXTabular::SET_MPWIDTH:
1427 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1430 case LyXTabular::SET_SPECIAL_COLUMN:
1431 case LyXTabular::SET_SPECIAL_MULTI:
1432 tabular.setAlignSpecial(cur.idx(),value,feature);
1435 case LyXTabular::APPEND_ROW:
1436 // append the row into the tabular
1437 tabular.appendRow(bv.buffer()->params(), cur.idx());
1440 case LyXTabular::APPEND_COLUMN:
1441 // append the column into the tabular
1442 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1443 cur.idx() = tabular.getCellNumber(row, column);
1446 case LyXTabular::DELETE_ROW:
1447 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1448 tabular.deleteRow(sel_row_start);
1449 if (sel_row_start >= tabular.rows())
1451 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1454 cur.selection() = false;
1457 case LyXTabular::DELETE_COLUMN:
1458 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1459 tabular.deleteColumn(sel_col_start);
1460 if (sel_col_start >= tabular.columns())
1462 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1465 cur.selection() = false;
1468 case LyXTabular::M_TOGGLE_LINE_TOP:
1470 case LyXTabular::TOGGLE_LINE_TOP: {
1471 bool lineSet = !tabular.topLine(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)
1475 tabular.getCellNumber(i, j),
1480 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1482 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1483 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1484 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1485 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1486 tabular.setBottomLine(
1487 tabular.getCellNumber(i, j),
1493 case LyXTabular::M_TOGGLE_LINE_LEFT:
1495 case LyXTabular::TOGGLE_LINE_LEFT: {
1496 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1497 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1498 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1499 tabular.setLeftLine(
1500 tabular.getCellNumber(i,j),
1506 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1508 case LyXTabular::TOGGLE_LINE_RIGHT: {
1509 bool lineSet = !tabular.rightLine(cur.idx(), flag);
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.setRightLine(
1513 tabular.getCellNumber(i,j),
1519 case LyXTabular::M_ALIGN_LEFT:
1520 case LyXTabular::M_ALIGN_RIGHT:
1521 case LyXTabular::M_ALIGN_CENTER:
1523 case LyXTabular::ALIGN_LEFT:
1524 case LyXTabular::ALIGN_RIGHT:
1525 case LyXTabular::ALIGN_CENTER:
1526 case LyXTabular::ALIGN_BLOCK:
1527 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1528 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1529 tabular.setAlignment(
1530 tabular.getCellNumber(i, j),
1535 case LyXTabular::M_VALIGN_TOP:
1536 case LyXTabular::M_VALIGN_BOTTOM:
1537 case LyXTabular::M_VALIGN_MIDDLE:
1539 case LyXTabular::VALIGN_TOP:
1540 case LyXTabular::VALIGN_BOTTOM:
1541 case LyXTabular::VALIGN_MIDDLE:
1542 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1543 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1544 tabular.setVAlignment(
1545 tabular.getCellNumber(i, j),
1549 case LyXTabular::MULTICOLUMN: {
1550 if (sel_row_start != sel_row_end) {
1551 #ifdef WITH_WARNINGS
1552 #warning Need I say it ? This is horrible.
1554 Alert::error(_("Error setting multicolumn"),
1555 _("You cannot set multicolumn vertically."));
1558 if (!cur.selection()) {
1559 // just multicol for one single cell
1560 // check whether we are completely in a multicol
1561 if (tabular.isMultiColumn(cur.idx()))
1562 tabular.unsetMultiColumn(cur.idx());
1564 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1567 // we have a selection so this means we just add all this
1568 // cells to form a multicolumn cell
1569 idx_type const s_start = cur.selBegin().idx();
1570 idx_type const s_end = cur.selEnd().idx();
1571 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1572 cur.idx() = s_start;
1575 cur.selection() = false;
1579 case LyXTabular::SET_ALL_LINES:
1581 case LyXTabular::UNSET_ALL_LINES:
1582 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1583 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1584 tabular.setAllLines(
1585 tabular.getCellNumber(i,j), setLines);
1588 case LyXTabular::SET_LONGTABULAR:
1589 tabular.setLongTabular(true);
1592 case LyXTabular::UNSET_LONGTABULAR:
1593 tabular.setLongTabular(false);
1596 case LyXTabular::SET_ROTATE_TABULAR:
1597 tabular.setRotateTabular(true);
1600 case LyXTabular::UNSET_ROTATE_TABULAR:
1601 tabular.setRotateTabular(false);
1604 case LyXTabular::SET_ROTATE_CELL:
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.setRotateCell(
1608 tabular.getCellNumber(i, j), true);
1611 case LyXTabular::UNSET_ROTATE_CELL:
1612 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1613 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1614 tabular.setRotateCell(
1615 tabular.getCellNumber(i, j), false);
1618 case LyXTabular::SET_USEBOX: {
1619 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1620 if (val == tabular.getUsebox(cur.idx()))
1621 val = LyXTabular::BOX_NONE;
1622 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1623 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1624 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1628 case LyXTabular::UNSET_LTFIRSTHEAD:
1630 case LyXTabular::SET_LTFIRSTHEAD:
1631 tabular.getRowOfLTFirstHead(row, ltt);
1632 checkLongtableSpecial(ltt, value, flag);
1633 tabular.setLTHead(row, flag, ltt, true);
1636 case LyXTabular::UNSET_LTHEAD:
1638 case LyXTabular::SET_LTHEAD:
1639 tabular.getRowOfLTHead(row, ltt);
1640 checkLongtableSpecial(ltt, value, flag);
1641 tabular.setLTHead(row, flag, ltt, false);
1644 case LyXTabular::UNSET_LTFOOT:
1646 case LyXTabular::SET_LTFOOT:
1647 tabular.getRowOfLTFoot(row, ltt);
1648 checkLongtableSpecial(ltt, value, flag);
1649 tabular.setLTFoot(row, flag, ltt, false);
1652 case LyXTabular::UNSET_LTLASTFOOT:
1654 case LyXTabular::SET_LTLASTFOOT:
1655 tabular.getRowOfLTLastFoot(row, ltt);
1656 checkLongtableSpecial(ltt, value, flag);
1657 tabular.setLTFoot(row, flag, ltt, true);
1660 case LyXTabular::SET_LTNEWPAGE:
1661 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1664 // dummy stuff just to avoid warnings
1665 case LyXTabular::LAST_ACTION:
1669 InsetTabularMailer(*this).updateDialog(&bv);
1673 bool InsetTabular::showInsetDialog(BufferView * bv) const
1675 InsetTabularMailer(*this).showDialog(bv);
1680 void InsetTabular::openLayoutDialog(BufferView * bv) const
1682 InsetTabularMailer(*this).showDialog(bv);
1686 void InsetTabular::getLabelList(Buffer const & buffer,
1687 vector<string> & list) const
1689 tabular.getLabelList(buffer, list);
1693 bool InsetTabular::copySelection(LCursor & cur)
1695 if (!cur.selection())
1700 getSelection(cur, rs, re, cs, ce);
1702 paste_tabular.reset(new LyXTabular(tabular));
1704 for (row_type i = 0; i < rs; ++i)
1705 paste_tabular->deleteRow(0);
1707 row_type const rows = re - rs + 1;
1708 while (paste_tabular->rows() > rows)
1709 paste_tabular->deleteRow(rows);
1711 paste_tabular->setTopLine(0, true, true);
1712 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1715 for (col_type i = 0; i < cs; ++i)
1716 paste_tabular->deleteColumn(0);
1718 col_type const columns = ce - cs + 1;
1719 while (paste_tabular->columns() > columns)
1720 paste_tabular->deleteColumn(columns);
1722 paste_tabular->setLeftLine(0, true, true);
1723 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1727 OutputParams const runparams;
1728 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1729 cur.bv().stuffClipboard(os.str());
1730 // mark tabular stack dirty
1731 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1732 // when we (hopefully) have a one-for-all paste mechanism.
1733 lyx::cap::dirtyTabularStack(true);
1739 bool InsetTabular::pasteSelection(LCursor & cur)
1743 col_type const actcol = tabular.column_of_cell(cur.idx());
1744 row_type const actrow = tabular.row_of_cell(cur.idx());
1745 for (row_type r1 = 0, r2 = actrow;
1746 r1 < paste_tabular->rows() && r2 < tabular.rows();
1748 for (col_type c1 = 0, c2 = actcol;
1749 c1 < paste_tabular->columns() && c2 < tabular.columns();
1751 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1752 tabular.isPartOfMultiColumn(r2, c2))
1754 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1758 if (tabular.isPartOfMultiColumn(r2, c2)) {
1762 shared_ptr<InsetText> inset(
1763 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1764 tabular.setCellInset(r2, c2, inset);
1773 void InsetTabular::cutSelection(LCursor & cur)
1775 if (!cur.selection())
1780 getSelection(cur, rs, re, cs, ce);
1781 for (row_type i = rs; i <= re; ++i) {
1782 for (col_type j = cs; j <= ce; ++j) {
1783 shared_ptr<InsetText> t
1784 = cell(tabular.getCellNumber(i, j));
1785 if (cur.buffer().params().tracking_changes)
1786 t->markErased(true);
1792 // cursor position might be invalid now
1793 cur.pos() = cur.lastpos();
1794 cur.clearSelection();
1798 bool InsetTabular::isRightToLeft(LCursor & cur) const
1800 BOOST_ASSERT(cur.depth() > 1);
1801 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1802 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1803 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1804 parentpos).language()->RightToLeft();
1808 void InsetTabular::getSelection(LCursor & cur,
1809 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1811 CursorSlice const & beg = cur.selBegin();
1812 CursorSlice const & end = cur.selEnd();
1813 cs = tabular.column_of_cell(beg.idx());
1814 ce = tabular.column_of_cell(end.idx());
1817 cs = tabular.column_of_cell(end.idx());
1819 ce = tabular.right_column_of_cell(end.idx());
1822 rs = tabular.row_of_cell(beg.idx());
1823 re = tabular.row_of_cell(end.idx());
1829 LyXText * InsetTabular::getText(int idx) const
1831 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1835 void InsetTabular::markErased(bool erased)
1837 for (idx_type idx = 0; idx < nargs(); ++idx)
1838 cell(idx)->markErased(erased);
1842 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1845 idx_type const cell = tabular.getCellFromInset(in);
1846 // FIXME: getCellFromInset() returns now always a valid cell, so
1847 // the stuff below can be deleted, and instead we have:
1848 return tabular.getPWidth(cell).zero();
1851 return tabular.getPWidth(cell).zero();
1853 // this is a workaround for a crash (New, Insert->Tabular,
1854 // Insert->FootNote)
1858 // well we didn't obviously find it so maybe our owner knows more
1859 BOOST_ASSERT(owner());
1860 return owner()->forceDefaultParagraphs(in);
1866 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1869 if (buf.length() <= 0)
1874 col_type maxCols = 1;
1875 string::size_type const len = buf.length();
1876 string::size_type p = 0;
1878 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1886 maxCols = max(cols, maxCols);
1892 maxCols = max(cols, maxCols);
1893 LyXTabular * loctab;
1898 paste_tabular.reset(
1899 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1900 loctab = paste_tabular.get();
1904 cell = bv.cursor().idx();
1905 ocol = tabular.column_of_cell(cell);
1906 row = tabular.row_of_cell(cell);
1909 string::size_type op = 0;
1910 idx_type const cells = loctab->getNumberOfCells();
1913 rows = loctab->rows();
1914 col_type const columns = loctab->columns();
1916 while (cell < cells && p < len && row < rows &&
1917 (p = buf.find_first_of("\t\n", p)) != string::npos)
1923 // we can only set this if we are not too far right
1924 if (cols < columns) {
1925 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1926 Paragraph & par = inset->text_.getPar(0);
1927 LyXFont const font = inset->text_.getFont(par, 0);
1928 inset->setText(buf.substr(op, p - op), font);
1934 // we can only set this if we are not too far right
1935 if (cols < columns) {
1936 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1937 Paragraph & par = inset->text_.getPar(0);
1938 LyXFont const font = inset->text_.getFont(par, 0);
1939 inset->setText(buf.substr(op, p - op), font);
1944 cell = loctab->getCellNumber(row, cols);
1950 // check for the last cell if there is no trailing '\n'
1951 if (cell < cells && op < len) {
1952 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1953 Paragraph & par = inset->text_.getPar(0);
1954 LyXFont const font = inset->text_.getFont(par, 0);
1955 inset->setText(buf.substr(op, len - op), font);
1961 void InsetTabular::addPreview(PreviewLoader & loader) const
1963 row_type const rows = tabular.rows();
1964 col_type const columns = tabular.columns();
1965 for (row_type i = 0; i < rows; ++i) {
1966 for (col_type j = 0; j < columns; ++j)
1967 tabular.getCellInset(i, j)->addPreview(loader);
1972 bool InsetTabular::tablemode(LCursor & cur) const
1974 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1981 string const InsetTabularMailer::name_("tabular");
1983 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1984 : inset_(const_cast<InsetTabular &>(inset))
1988 string const InsetTabularMailer::inset2string(Buffer const &) const
1990 return params2string(inset_);
1994 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1996 istringstream data(in);
1998 lex.setStream(data);
2005 if (!lex || token != name_)
2006 return print_mailer_error("InsetTabularMailer", in, 1,
2009 // This is part of the inset proper that is usually swallowed
2010 // by Buffer::readInset
2012 if (!lex || token != "Tabular")
2013 return print_mailer_error("InsetTabularMailer", in, 2,
2016 Buffer const & buffer = inset.buffer();
2017 inset.read(buffer, lex);
2021 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2024 data << name_ << ' ';
2025 inset.write(inset.buffer(), data);
2026 data << "\\end_inset\n";