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 recordUndo(cur, Undo::DELETE);
639 cell(cur.idx())->dispatch(cur, cmd);
644 recordUndo(cur, Undo::DELETE);
648 cell(cur.idx())->dispatch(cur, cmd);
652 if (!cur.selection())
654 if (tablemode(cur)) {
658 cell(cur.idx())->dispatch(cur, cmd);
661 case LFUN_PASTESELECTION: {
662 string const clip = cur.bv().getClipboard();
665 if (clip.find('\t') != string::npos) {
668 col_type maxCols = 1;
669 size_t len = clip.length();
670 for (size_t p = 0; p < len; ++p) {
671 p = clip.find_first_of("\t\n", p);
672 if (p == string::npos)
681 maxCols = max(cols, maxCols);
686 maxCols = max(cols, maxCols);
689 new LyXTabular(cur.buffer().params(), rows, maxCols));
691 string::size_type op = 0;
693 idx_type const cells =
694 paste_tabular->getNumberOfCells();
697 for (size_t p = 0; cell < cells && p < len; ++p) {
698 p = clip.find_first_of("\t\n", p);
699 if (p == string::npos || p >= len)
703 paste_tabular->getCellInset(cell)->
704 setText(clip.substr(op, p - op), font);
709 paste_tabular->getCellInset(cell)->
710 setText(clip.substr(op, p - op), font);
711 while (cols++ < maxCols)
718 // check for the last cell if there is no trailing '\n'
719 if (cell < cells && op < len)
720 paste_tabular->getCellInset(cell)->
721 setText(clip.substr(op, len - op), font);
722 } else if (!insertAsciiString(cur.bv(), clip, true)) {
723 // so that the clipboard is used and it goes on
725 // and executes LFUN_PASTESELECTION in insettext!
726 paste_tabular.reset();
732 if (hasPasteBuffer() && tabularStackDirty()) {
733 recordUndo(cur, Undo::INSERT);
737 cell(cur.idx())->dispatch(cur, cmd);
748 case LFUN_FREEFONT_APPLY:
749 case LFUN_FREEFONT_UPDATE:
753 case LFUN_CAPITALIZE_WORD:
754 case LFUN_UPCASE_WORD:
755 case LFUN_LOWCASE_WORD:
756 case LFUN_TRANSPOSE_CHARS:
757 if (tablemode(cur)) {
760 getSelection(cur, rs, re, cs, ce);
761 for (row_type i = rs; i <= re; ++i) {
762 for (col_type j = cs; j <= ce; ++j) {
763 // cursor follows cell:
764 cur.idx() = tabular.getCellNumber(i, j);
765 // select this cell only:
768 cur.pos() = cur.top().lastpos();
771 cell(cur.idx())->dispatch(cur, cmd);
774 // Restore original selection
775 cur.idx() = tabular.getCellNumber(rs, cs);
778 cur.idx() = tabular.getCellNumber(re, ce);
779 cur.pos() = cur.top().lastpos();
784 cell(cur.idx())->dispatch(cur, cmd);
788 // we try to handle this event in the insets dispatch function.
789 cell(cur.idx())->dispatch(cur, cmd);
794 InsetTabularMailer(*this).updateDialog(&cur.bv());
798 // function sets an object as defined in func_status.h:
799 // states OK, Unknown, Disabled, On, Off.
800 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
801 FuncStatus & status) const
803 switch (cmd.action) {
804 case LFUN_TABULAR_FEATURE: {
805 int action = LyXTabular::LAST_ACTION;
807 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
808 string const tmp = tabularFeature[i].feature;
809 if (tmp == cmd.argument.substr(0, tmp.length())) {
810 action = tabularFeature[i].action;
814 if (action == LyXTabular::LAST_ACTION) {
816 status.unknown(true);
820 string const argument
821 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
823 row_type sel_row_start = 0;
824 row_type sel_row_end = 0;
826 LyXTabular::ltType dummyltt;
829 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
832 case LyXTabular::SET_PWIDTH:
833 case LyXTabular::SET_MPWIDTH:
834 case LyXTabular::SET_SPECIAL_COLUMN:
835 case LyXTabular::SET_SPECIAL_MULTI:
836 case LyXTabular::APPEND_ROW:
837 case LyXTabular::APPEND_COLUMN:
838 case LyXTabular::DELETE_ROW:
839 case LyXTabular::DELETE_COLUMN:
840 case LyXTabular::SET_ALL_LINES:
841 case LyXTabular::UNSET_ALL_LINES:
845 case LyXTabular::MULTICOLUMN:
846 status.setOnOff(tabular.isMultiColumn(cur.idx()));
849 case LyXTabular::M_TOGGLE_LINE_TOP:
851 case LyXTabular::TOGGLE_LINE_TOP:
852 status.setOnOff(tabular.topLine(cur.idx(), flag));
855 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
857 case LyXTabular::TOGGLE_LINE_BOTTOM:
858 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
861 case LyXTabular::M_TOGGLE_LINE_LEFT:
863 case LyXTabular::TOGGLE_LINE_LEFT:
864 status.setOnOff(tabular.leftLine(cur.idx(), flag));
867 case LyXTabular::M_TOGGLE_LINE_RIGHT:
869 case LyXTabular::TOGGLE_LINE_RIGHT:
870 status.setOnOff(tabular.rightLine(cur.idx(), flag));
873 case LyXTabular::M_ALIGN_LEFT:
875 case LyXTabular::ALIGN_LEFT:
876 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
879 case LyXTabular::M_ALIGN_RIGHT:
881 case LyXTabular::ALIGN_RIGHT:
882 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
885 case LyXTabular::M_ALIGN_CENTER:
887 case LyXTabular::ALIGN_CENTER:
888 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
891 case LyXTabular::ALIGN_BLOCK:
892 status.enabled(!tabular.getPWidth(cur.idx()).zero());
893 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
896 case LyXTabular::M_VALIGN_TOP:
898 case LyXTabular::VALIGN_TOP:
900 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
903 case LyXTabular::M_VALIGN_BOTTOM:
905 case LyXTabular::VALIGN_BOTTOM:
907 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
910 case LyXTabular::M_VALIGN_MIDDLE:
912 case LyXTabular::VALIGN_MIDDLE:
914 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
917 case LyXTabular::SET_LONGTABULAR:
918 status.setOnOff(tabular.isLongTabular());
921 case LyXTabular::UNSET_LONGTABULAR:
922 status.setOnOff(!tabular.isLongTabular());
925 case LyXTabular::SET_ROTATE_TABULAR:
926 status.setOnOff(tabular.getRotateTabular());
929 case LyXTabular::UNSET_ROTATE_TABULAR:
930 status.setOnOff(!tabular.getRotateTabular());
933 case LyXTabular::SET_ROTATE_CELL:
934 status.setOnOff(tabular.getRotateCell(cur.idx()));
937 case LyXTabular::UNSET_ROTATE_CELL:
938 status.setOnOff(!tabular.getRotateCell(cur.idx()));
941 case LyXTabular::SET_USEBOX:
942 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
945 case LyXTabular::SET_LTFIRSTHEAD:
946 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
949 case LyXTabular::UNSET_LTFIRSTHEAD:
950 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
953 case LyXTabular::SET_LTHEAD:
954 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
957 case LyXTabular::UNSET_LTHEAD:
958 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
961 case LyXTabular::SET_LTFOOT:
962 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
965 case LyXTabular::UNSET_LTFOOT:
966 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
969 case LyXTabular::SET_LTLASTFOOT:
970 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
973 case LyXTabular::UNSET_LTLASTFOOT:
974 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
977 case LyXTabular::SET_LTNEWPAGE:
978 status.setOnOff(tabular.getLTNewPage(sel_row_start));
983 status.enabled(false);
989 // These are only enabled inside tabular
990 case LFUN_CELL_BACKWARD:
991 case LFUN_CELL_FORWARD:
992 status.enabled(true);
995 // disable these with multiple cells selected
996 case LFUN_INSET_INSERT:
997 case LFUN_TABULAR_INSERT:
998 case LFUN_INSERT_CHARSTYLE:
999 case LFUN_INSET_FLOAT:
1000 case LFUN_INSET_WIDE_FLOAT:
1001 case LFUN_INSET_FOOTNOTE:
1002 case LFUN_INSET_MARGINAL:
1003 case LFUN_INSERT_MATH:
1004 case LFUN_MATH_MODE:
1005 case LFUN_MATH_MUTATE:
1006 case LFUN_MATH_DISPLAY:
1007 case LFUN_INSERT_NOTE:
1008 case LFUN_INSET_OPTARG:
1009 case LFUN_INSERT_BOX:
1010 case LFUN_INSERT_BRANCH:
1011 case LFUN_INSET_WRAP:
1012 case LFUN_INSET_ERT: {
1013 if (tablemode(cur)) {
1014 status.enabled(false);
1017 return cell(cur.idx())->getStatus(cur, cmd, status);
1020 // disable in non-fixed-width cells
1021 case LFUN_BREAKLINE:
1022 case LFUN_BREAKPARAGRAPH:
1023 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1024 case LFUN_BREAKPARAGRAPH_SKIP: {
1025 if (tabular.getPWidth(cur.idx()).zero()) {
1026 status.enabled(false);
1029 return cell(cur.idx())->getStatus(cur, cmd, status);
1032 case LFUN_INSET_MODIFY:
1033 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1034 status.enabled(true);
1040 // we try to handle this event in the insets dispatch function.
1041 return cell(cur.idx())->getStatus(cur, cmd, status);
1046 int InsetTabular::latex(Buffer const & buf, ostream & os,
1047 OutputParams const & runparams) const
1049 return tabular.latex(buf, os, runparams);
1053 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1054 OutputParams const & runparams) const
1056 int const dp = runparams.linelen ? runparams.depth : 0;
1057 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1061 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1062 OutputParams const & runparams) const
1064 return tabular.linuxdoc(buf,os, runparams);
1068 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1069 OutputParams const & runparams) const
1072 InsetBase * master = 0;
1074 #ifdef WITH_WARNINGS
1075 #warning Why not pass a proper DocIterator here?
1078 // if the table is inside a float it doesn't need the informaltable
1079 // wrapper. Search for it.
1080 for (master = owner(); master; master = master->owner())
1081 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1086 os << "<informaltable>";
1089 ret += tabular.docbook(buf, os, runparams);
1091 os << "</informaltable>";
1098 void InsetTabular::validate(LaTeXFeatures & features) const
1100 tabular.validate(features);
1104 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1106 return tabular.getCellInset(idx);
1110 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1112 return tabular.getCellInset(idx);
1116 void InsetTabular::cursorPos
1117 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1119 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1121 // y offset correction
1122 int const row = tabular.row_of_cell(sl.idx());
1123 for (int i = 0; i <= row; ++i) {
1125 y += tabular.getAscentOfRow(i);
1126 y += tabular.getAdditionalHeight(i);
1129 y += tabular.getDescentOfRow(i);
1132 // x offset correction
1133 int const col = tabular.column_of_cell(sl.idx());
1134 int idx = tabular.getCellNumber(row, 0);
1135 for (int j = 0; j < col; ++j) {
1136 if (tabular.isPartOfMultiColumn(row, j))
1138 x += tabular.getWidthOfColumn(idx);
1141 x += tabular.getBeginningOfTextInCell(idx);
1142 x += ADD_TO_TABULAR_WIDTH;
1147 int InsetTabular::dist(idx_type const cell, int x, int y) const
1151 InsetBase const & inset = *tabular.getCellInset(cell);
1152 Point o = theCoords.getInsets().xy(&inset);
1153 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1154 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1155 int const ybeg = o.y_ - inset.ascent();
1156 row_type const row = tabular.row_of_cell(cell);
1157 int const rowheight = tabular.getAscentOfRow(row)
1158 + tabular.getDescentOfRow(row)
1159 + tabular.getAdditionalHeight(row);
1160 int const yend = ybeg + rowheight;
1172 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1173 // << " ybeg=" << ybeg << " yend=" << yend
1174 // << " xx=" << xx << " yy=" << yy
1175 // << " dist=" << xx + yy << endl;
1180 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1182 //lyxerr << "InsetTabular::editXY: " << this << endl;
1183 cur.selection() = false;
1185 cur.idx() = getNearestCell(x, y);
1187 return cell(cur.idx())->text_.editXY(cur, x, y);
1191 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1193 cur.idx() = getNearestCell(x, y);
1194 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1198 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1200 idx_type idx_min = 0;
1201 int dist_min = std::numeric_limits<int>::max();
1202 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1203 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1204 int const d = dist(i, x, y);
1215 int InsetTabular::getCellXPos(idx_type const cell) const
1219 for (; !tabular.isFirstCellInRow(c); --c)
1221 int lx = tabular.getWidthOfColumn(cell);
1222 for (; c < cell; ++c)
1223 lx += tabular.getWidthOfColumn(c);
1225 return lx - tabular.getWidthOfColumn(cell);
1229 void InsetTabular::resetPos(LCursor & cur) const
1231 BufferView & bv = cur.bv();
1232 int const maxwidth = bv.workWidth();
1234 if (&cur.inset() != this) {
1238 int const X2 = maxwidth;
1239 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1240 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1241 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1244 scx_ = X1 + 20 - x1;
1246 scx_ = X2 - 20 - x2;
1253 InsetTabularMailer(*this).updateDialog(&bv);
1257 void InsetTabular::moveNextCell(LCursor & cur)
1259 if (isRightToLeft(cur)) {
1260 if (tabular.isFirstCellInRow(cur.idx())) {
1261 row_type const row = tabular.row_of_cell(cur.idx());
1262 if (row == tabular.rows() - 1)
1264 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1271 if (tabular.isLastCell(cur.idx()))
1281 void InsetTabular::movePrevCell(LCursor & cur)
1283 if (isRightToLeft(cur)) {
1284 if (tabular.isLastCellInRow(cur.idx())) {
1285 row_type const row = tabular.row_of_cell(cur.idx());
1288 cur.idx() = tabular.getFirstCellInRow(row);
1289 cur.idx() = tabular.getCellAbove(cur.idx());
1291 if (tabular.isLastCell(cur.idx()))
1296 if (cur.idx() == 0) // first cell
1300 cur.pit() = cur.lastpit();
1301 cur.pos() = cur.lastpos();
1306 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1308 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1311 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1312 string const tmp = tabularFeature[i].feature;
1314 if (tmp == what.substr(0, tmp.length())) {
1315 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1316 //tabularFeatures[i].feature.length())) {
1317 action = tabularFeature[i].action;
1321 if (action == LyXTabular::LAST_ACTION)
1325 ltrim(what.substr(tabularFeature[i].feature.length()));
1326 tabularFeatures(cur, action, val);
1333 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1334 string const & special, bool & flag)
1336 if (special == "dl_above") {
1339 } else if (special == "dl_below") {
1340 ltt.bottomDL = flag;
1342 } else if (special == "empty") {
1354 void InsetTabular::tabularFeatures(LCursor & cur,
1355 LyXTabular::Feature feature, string const & value)
1357 BufferView & bv = cur.bv();
1358 col_type sel_col_start;
1359 col_type sel_col_end;
1360 row_type sel_row_start;
1361 row_type sel_row_end;
1362 bool setLines = false;
1363 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1364 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1368 case LyXTabular::M_ALIGN_LEFT:
1369 case LyXTabular::ALIGN_LEFT:
1370 setAlign = LYX_ALIGN_LEFT;
1373 case LyXTabular::M_ALIGN_RIGHT:
1374 case LyXTabular::ALIGN_RIGHT:
1375 setAlign = LYX_ALIGN_RIGHT;
1378 case LyXTabular::M_ALIGN_CENTER:
1379 case LyXTabular::ALIGN_CENTER:
1380 setAlign = LYX_ALIGN_CENTER;
1383 case LyXTabular::ALIGN_BLOCK:
1384 setAlign = LYX_ALIGN_BLOCK;
1387 case LyXTabular::M_VALIGN_TOP:
1388 case LyXTabular::VALIGN_TOP:
1389 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1392 case LyXTabular::M_VALIGN_BOTTOM:
1393 case LyXTabular::VALIGN_BOTTOM:
1394 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1397 case LyXTabular::M_VALIGN_MIDDLE:
1398 case LyXTabular::VALIGN_MIDDLE:
1399 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1406 recordUndo(cur, Undo::ATOMIC);
1408 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1409 row_type const row = tabular.row_of_cell(cur.idx());
1410 col_type const column = tabular.column_of_cell(cur.idx());
1412 LyXTabular::ltType ltt;
1416 case LyXTabular::SET_PWIDTH: {
1417 LyXLength const len(value);
1418 tabular.setColumnPWidth(cur.idx(), len);
1420 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1421 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1425 case LyXTabular::SET_MPWIDTH:
1426 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1429 case LyXTabular::SET_SPECIAL_COLUMN:
1430 case LyXTabular::SET_SPECIAL_MULTI:
1431 tabular.setAlignSpecial(cur.idx(),value,feature);
1434 case LyXTabular::APPEND_ROW:
1435 // append the row into the tabular
1436 tabular.appendRow(bv.buffer()->params(), cur.idx());
1439 case LyXTabular::APPEND_COLUMN:
1440 // append the column into the tabular
1441 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1442 cur.idx() = tabular.getCellNumber(row, column);
1445 case LyXTabular::DELETE_ROW:
1446 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1447 tabular.deleteRow(sel_row_start);
1448 if (sel_row_start >= tabular.rows())
1450 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1453 cur.selection() = false;
1456 case LyXTabular::DELETE_COLUMN:
1457 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1458 tabular.deleteColumn(sel_col_start);
1459 if (sel_col_start >= tabular.columns())
1461 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1464 cur.selection() = false;
1467 case LyXTabular::M_TOGGLE_LINE_TOP:
1469 case LyXTabular::TOGGLE_LINE_TOP: {
1470 bool lineSet = !tabular.topLine(cur.idx(), flag);
1471 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1472 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1474 tabular.getCellNumber(i, j),
1479 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1481 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1482 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1483 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1484 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1485 tabular.setBottomLine(
1486 tabular.getCellNumber(i, j),
1492 case LyXTabular::M_TOGGLE_LINE_LEFT:
1494 case LyXTabular::TOGGLE_LINE_LEFT: {
1495 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1496 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1497 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1498 tabular.setLeftLine(
1499 tabular.getCellNumber(i,j),
1505 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1507 case LyXTabular::TOGGLE_LINE_RIGHT: {
1508 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1509 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1510 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1511 tabular.setRightLine(
1512 tabular.getCellNumber(i,j),
1518 case LyXTabular::M_ALIGN_LEFT:
1519 case LyXTabular::M_ALIGN_RIGHT:
1520 case LyXTabular::M_ALIGN_CENTER:
1522 case LyXTabular::ALIGN_LEFT:
1523 case LyXTabular::ALIGN_RIGHT:
1524 case LyXTabular::ALIGN_CENTER:
1525 case LyXTabular::ALIGN_BLOCK:
1526 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1527 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1528 tabular.setAlignment(
1529 tabular.getCellNumber(i, j),
1534 case LyXTabular::M_VALIGN_TOP:
1535 case LyXTabular::M_VALIGN_BOTTOM:
1536 case LyXTabular::M_VALIGN_MIDDLE:
1538 case LyXTabular::VALIGN_TOP:
1539 case LyXTabular::VALIGN_BOTTOM:
1540 case LyXTabular::VALIGN_MIDDLE:
1541 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1542 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1543 tabular.setVAlignment(
1544 tabular.getCellNumber(i, j),
1548 case LyXTabular::MULTICOLUMN: {
1549 if (sel_row_start != sel_row_end) {
1550 #ifdef WITH_WARNINGS
1551 #warning Need I say it ? This is horrible.
1553 Alert::error(_("Error setting multicolumn"),
1554 _("You cannot set multicolumn vertically."));
1557 if (!cur.selection()) {
1558 // just multicol for one single cell
1559 // check whether we are completely in a multicol
1560 if (tabular.isMultiColumn(cur.idx()))
1561 tabular.unsetMultiColumn(cur.idx());
1563 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1566 // we have a selection so this means we just add all this
1567 // cells to form a multicolumn cell
1568 idx_type const s_start = cur.selBegin().idx();
1569 idx_type const s_end = cur.selEnd().idx();
1570 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1571 cur.idx() = s_start;
1574 cur.selection() = false;
1578 case LyXTabular::SET_ALL_LINES:
1580 case LyXTabular::UNSET_ALL_LINES:
1581 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1582 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1583 tabular.setAllLines(
1584 tabular.getCellNumber(i,j), setLines);
1587 case LyXTabular::SET_LONGTABULAR:
1588 tabular.setLongTabular(true);
1591 case LyXTabular::UNSET_LONGTABULAR:
1592 tabular.setLongTabular(false);
1595 case LyXTabular::SET_ROTATE_TABULAR:
1596 tabular.setRotateTabular(true);
1599 case LyXTabular::UNSET_ROTATE_TABULAR:
1600 tabular.setRotateTabular(false);
1603 case LyXTabular::SET_ROTATE_CELL:
1604 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1605 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1606 tabular.setRotateCell(
1607 tabular.getCellNumber(i, j), true);
1610 case LyXTabular::UNSET_ROTATE_CELL:
1611 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1612 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1613 tabular.setRotateCell(
1614 tabular.getCellNumber(i, j), false);
1617 case LyXTabular::SET_USEBOX: {
1618 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1619 if (val == tabular.getUsebox(cur.idx()))
1620 val = LyXTabular::BOX_NONE;
1621 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1622 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1623 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1627 case LyXTabular::UNSET_LTFIRSTHEAD:
1629 case LyXTabular::SET_LTFIRSTHEAD:
1630 tabular.getRowOfLTFirstHead(row, ltt);
1631 checkLongtableSpecial(ltt, value, flag);
1632 tabular.setLTHead(row, flag, ltt, true);
1635 case LyXTabular::UNSET_LTHEAD:
1637 case LyXTabular::SET_LTHEAD:
1638 tabular.getRowOfLTHead(row, ltt);
1639 checkLongtableSpecial(ltt, value, flag);
1640 tabular.setLTHead(row, flag, ltt, false);
1643 case LyXTabular::UNSET_LTFOOT:
1645 case LyXTabular::SET_LTFOOT:
1646 tabular.getRowOfLTFoot(row, ltt);
1647 checkLongtableSpecial(ltt, value, flag);
1648 tabular.setLTFoot(row, flag, ltt, false);
1651 case LyXTabular::UNSET_LTLASTFOOT:
1653 case LyXTabular::SET_LTLASTFOOT:
1654 tabular.getRowOfLTLastFoot(row, ltt);
1655 checkLongtableSpecial(ltt, value, flag);
1656 tabular.setLTFoot(row, flag, ltt, true);
1659 case LyXTabular::SET_LTNEWPAGE:
1660 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1663 // dummy stuff just to avoid warnings
1664 case LyXTabular::LAST_ACTION:
1668 InsetTabularMailer(*this).updateDialog(&bv);
1672 bool InsetTabular::showInsetDialog(BufferView * bv) const
1674 InsetTabularMailer(*this).showDialog(bv);
1679 void InsetTabular::openLayoutDialog(BufferView * bv) const
1681 InsetTabularMailer(*this).showDialog(bv);
1685 void InsetTabular::getLabelList(Buffer const & buffer,
1686 vector<string> & list) const
1688 tabular.getLabelList(buffer, list);
1692 bool InsetTabular::copySelection(LCursor & cur)
1694 if (!cur.selection())
1699 getSelection(cur, rs, re, cs, ce);
1701 paste_tabular.reset(new LyXTabular(tabular));
1703 for (row_type i = 0; i < rs; ++i)
1704 paste_tabular->deleteRow(0);
1706 row_type const rows = re - rs + 1;
1707 while (paste_tabular->rows() > rows)
1708 paste_tabular->deleteRow(rows);
1710 paste_tabular->setTopLine(0, true, true);
1711 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1714 for (col_type i = 0; i < cs; ++i)
1715 paste_tabular->deleteColumn(0);
1717 col_type const columns = ce - cs + 1;
1718 while (paste_tabular->columns() > columns)
1719 paste_tabular->deleteColumn(columns);
1721 paste_tabular->setLeftLine(0, true, true);
1722 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1726 OutputParams const runparams;
1727 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1728 cur.bv().stuffClipboard(os.str());
1729 // mark tabular stack dirty
1730 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1731 // when we (hopefully) have a one-for-all paste mechanism.
1732 lyx::cap::dirtyTabularStack(true);
1738 bool InsetTabular::pasteSelection(LCursor & cur)
1742 col_type const actcol = tabular.column_of_cell(cur.idx());
1743 row_type const actrow = tabular.row_of_cell(cur.idx());
1744 for (row_type r1 = 0, r2 = actrow;
1745 r1 < paste_tabular->rows() && r2 < tabular.rows();
1747 for (col_type c1 = 0, c2 = actcol;
1748 c1 < paste_tabular->columns() && c2 < tabular.columns();
1750 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1751 tabular.isPartOfMultiColumn(r2, c2))
1753 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1757 if (tabular.isPartOfMultiColumn(r2, c2)) {
1761 shared_ptr<InsetText> inset(
1762 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1763 tabular.setCellInset(r2, c2, inset);
1772 void InsetTabular::cutSelection(LCursor & cur)
1774 if (!cur.selection())
1779 getSelection(cur, rs, re, cs, ce);
1780 for (row_type i = rs; i <= re; ++i) {
1781 for (col_type j = cs; j <= ce; ++j) {
1782 shared_ptr<InsetText> t
1783 = cell(tabular.getCellNumber(i, j));
1784 if (cur.buffer().params().tracking_changes)
1785 t->markErased(true);
1791 // cursor position might be invalid now
1792 cur.pos() = cur.lastpos();
1793 cur.clearSelection();
1797 bool InsetTabular::isRightToLeft(LCursor & cur) const
1799 BOOST_ASSERT(cur.depth() > 1);
1800 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1801 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1802 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1803 parentpos).language()->RightToLeft();
1807 void InsetTabular::getSelection(LCursor & cur,
1808 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1810 CursorSlice const & beg = cur.selBegin();
1811 CursorSlice const & end = cur.selEnd();
1812 cs = tabular.column_of_cell(beg.idx());
1813 ce = tabular.column_of_cell(end.idx());
1816 cs = tabular.column_of_cell(end.idx());
1818 ce = tabular.right_column_of_cell(end.idx());
1821 rs = tabular.row_of_cell(beg.idx());
1822 re = tabular.row_of_cell(end.idx());
1828 LyXText * InsetTabular::getText(int idx) const
1830 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1834 void InsetTabular::markErased(bool erased)
1836 for (idx_type idx = 0; idx < nargs(); ++idx)
1837 cell(idx)->markErased(erased);
1841 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1844 idx_type const cell = tabular.getCellFromInset(in);
1845 // FIXME: getCellFromInset() returns now always a valid cell, so
1846 // the stuff below can be deleted, and instead we have:
1847 return tabular.getPWidth(cell).zero();
1850 return tabular.getPWidth(cell).zero();
1852 // this is a workaround for a crash (New, Insert->Tabular,
1853 // Insert->FootNote)
1857 // well we didn't obviously find it so maybe our owner knows more
1858 BOOST_ASSERT(owner());
1859 return owner()->forceDefaultParagraphs(in);
1865 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1868 if (buf.length() <= 0)
1873 col_type maxCols = 1;
1874 string::size_type const len = buf.length();
1875 string::size_type p = 0;
1877 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1885 maxCols = max(cols, maxCols);
1891 maxCols = max(cols, maxCols);
1892 LyXTabular * loctab;
1897 paste_tabular.reset(
1898 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1899 loctab = paste_tabular.get();
1903 cell = bv.cursor().idx();
1904 ocol = tabular.column_of_cell(cell);
1905 row = tabular.row_of_cell(cell);
1908 string::size_type op = 0;
1909 idx_type const cells = loctab->getNumberOfCells();
1912 rows = loctab->rows();
1913 col_type const columns = loctab->columns();
1915 while (cell < cells && p < len && row < rows &&
1916 (p = buf.find_first_of("\t\n", p)) != string::npos)
1922 // we can only set this if we are not too far right
1923 if (cols < columns) {
1924 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1925 Paragraph & par = inset->text_.getPar(0);
1926 LyXFont const font = inset->text_.getFont(par, 0);
1927 inset->setText(buf.substr(op, p - op), font);
1933 // we can only set this if we are not too far right
1934 if (cols < columns) {
1935 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1936 Paragraph & par = inset->text_.getPar(0);
1937 LyXFont const font = inset->text_.getFont(par, 0);
1938 inset->setText(buf.substr(op, p - op), font);
1943 cell = loctab->getCellNumber(row, cols);
1949 // check for the last cell if there is no trailing '\n'
1950 if (cell < cells && op < len) {
1951 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1952 Paragraph & par = inset->text_.getPar(0);
1953 LyXFont const font = inset->text_.getFont(par, 0);
1954 inset->setText(buf.substr(op, len - op), font);
1960 void InsetTabular::addPreview(PreviewLoader & loader) const
1962 row_type const rows = tabular.rows();
1963 col_type const columns = tabular.columns();
1964 for (row_type i = 0; i < rows; ++i) {
1965 for (col_type j = 0; j < columns; ++j)
1966 tabular.getCellInset(i, j)->addPreview(loader);
1971 bool InsetTabular::tablemode(LCursor & cur) const
1973 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1980 string const InsetTabularMailer::name_("tabular");
1982 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1983 : inset_(const_cast<InsetTabular &>(inset))
1987 string const InsetTabularMailer::inset2string(Buffer const &) const
1989 return params2string(inset_);
1993 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1995 istringstream data(in);
1997 lex.setStream(data);
2004 if (!lex || token != name_)
2005 return print_mailer_error("InsetTabularMailer", in, 1,
2008 // This is part of the inset proper that is usually swallowed
2009 // by Buffer::readInset
2011 if (!lex || token != "Tabular")
2012 return print_mailer_error("InsetTabularMailer", in, 2,
2015 Buffer const & buffer = inset.buffer();
2016 inset.read(buffer, lex);
2020 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2023 data << name_ << ' ';
2024 inset.write(inset.buffer(), data);
2025 data << "\\end_inset\n";