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::UNSET_LTHEAD, "unset-lthead" },
121 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
122 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
123 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
124 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
125 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
126 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
127 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
128 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
129 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
130 { LyXTabular::LAST_ACTION, "" }
134 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
136 FeatureEqual(LyXTabular::Feature feature)
137 : feature_(feature) {}
138 bool operator()(TabularFeature const & tf) const {
139 return tf.action == feature_;
142 LyXTabular::Feature feature_;
148 string const featureAsString(LyXTabular::Feature feature)
150 TabularFeature * end = tabularFeature +
151 sizeof(tabularFeature) / sizeof(TabularFeature);
152 TabularFeature * it = std::find_if(tabularFeature, end,
153 FeatureEqual(feature));
154 return (it == end) ? string() : it->feature;
158 bool InsetTabular::hasPasteBuffer() const
160 return (paste_tabular.get() != 0);
164 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
166 : tabular(buf.params(), max(rows, row_type(1)),
167 max(columns, col_type(1))), buffer_(&buf), scx_(0)
171 InsetTabular::InsetTabular(InsetTabular const & tab)
172 : InsetOld(tab), tabular(tab.tabular),
173 buffer_(tab.buffer_), scx_(0)
177 InsetTabular::~InsetTabular()
179 InsetTabularMailer(*this).hideDialog();
183 auto_ptr<InsetBase> InsetTabular::doClone() const
185 return auto_ptr<InsetBase>(new InsetTabular(*this));
189 Buffer const & InsetTabular::buffer() const
195 void InsetTabular::buffer(Buffer const * b)
201 void InsetTabular::write(Buffer const & buf, ostream & os) const
203 os << "Tabular" << endl;
204 tabular.write(buf, os);
208 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
210 bool const old_format = (lex.getString() == "\\LyXTable");
212 tabular.read(buf, lex);
218 string token = lex.getString();
219 while (lex.isOK() && (token != "\\end_inset")) {
221 token = lex.getString();
223 if (token != "\\end_inset") {
224 lex.printError("Missing \\end_inset at this point. "
230 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
232 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
233 // mi.base.textwidth << "\n";
235 lyxerr << "InsetTabular::metrics: need bv" << endl;
240 for (idx_type cell = 0; i < tabular.rows(); ++i) {
243 for (col_type j = 0; j < tabular.columns(); ++j) {
244 if (tabular.isPartOfMultiColumn(i, j))
248 LyXLength p_width = tabular.column_info[j].p_width;
250 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
251 tabular.getCellInset(cell)->metrics(m, dim);
253 dim.wid = m.base.textwidth;
254 maxAsc = max(maxAsc, dim.asc);
255 maxDesc = max(maxDesc, dim.des);
256 tabular.setWidthOfCell(cell, dim.wid);
259 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
260 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
263 dim.asc = tabular.getAscentOfRow(0);
264 dim.des = tabular.getHeightOfTabular() - dim.asc;
265 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
270 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
272 setPosCache(pi, x, y);
274 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
275 BufferView * bv = pi.base.bv;
277 static NullPainter nop;
278 static PainterInfo nullpi(bv, nop);
280 //resetPos(bv->cursor());
283 x += ADD_TO_TABULAR_WIDTH;
286 first_visible_cell = LyXTabular::npos;
287 for (row_type i = 0; i < tabular.rows(); ++i) {
289 int const a = tabular.getAscentOfRow(i);
290 int const d = tabular.getDescentOfRow(i);
291 idx = tabular.getCellNumber(i, 0);
292 for (col_type j = 0; j < tabular.columns(); ++j) {
293 if (tabular.isPartOfMultiColumn(i, j))
295 if (first_visible_cell == LyXTabular::npos)
296 first_visible_cell = idx;
298 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
299 if (nx + tabular.getWidthOfColumn(idx) < 0
300 || nx > bv->workWidth()
302 || y - a > bv->workHeight()) {
303 cell(idx)->draw(nullpi, cx, y);
304 drawCellLines(nop, nx, y, i, idx);
306 cell(idx)->draw(pi, cx, y);
307 drawCellLines(pi.pain, nx, y, i, idx);
309 nx += tabular.getWidthOfColumn(idx);
313 if (i + 1 < tabular.rows())
314 y += d + tabular.getAscentOfRow(i + 1) +
315 tabular.getAdditionalHeight(i + 1);
320 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
322 setPosCache(pi, x, y);
324 LCursor & cur = pi.base.bv->cursor();
325 if (!cur.selection())
327 if (!ptr_cmp(&cur.inset(), this))
332 x += scx_ + ADD_TO_TABULAR_WIDTH;
334 if (tablemode(cur)) {
337 getSelection(cur, rs, re, cs, ce);
338 y -= tabular.getAscentOfRow(0);
339 for (row_type j = 0; j < tabular.rows(); ++j) {
340 int const a = tabular.getAscentOfRow(j);
341 int const h = a + tabular.getDescentOfRow(j);
343 y += tabular.getAdditionalHeight(j);
344 for (col_type i = 0; i < tabular.columns(); ++i) {
345 if (tabular.isPartOfMultiColumn(j, i))
347 idx_type const cell =
348 tabular.getCellNumber(j, i);
349 int const w = tabular.getWidthOfColumn(cell);
350 if (i >= cs && i <= ce && j >= rs && j <= re)
351 pi.pain.fillRectangle(xx, y, w, h,
360 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
365 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
366 row_type row, idx_type cell) const
368 int x2 = x + tabular.getWidthOfColumn(cell);
371 if (!tabular.topAlreadyDrawn(cell)) {
372 on_off = !tabular.topLine(cell);
373 pain.line(x, y - tabular.getAscentOfRow(row),
374 x2, y - tabular.getAscentOfRow(row),
375 on_off ? LColor::tabularonoffline : LColor::tabularline,
376 on_off ? Painter::line_onoffdash : Painter::line_solid);
378 on_off = !tabular.bottomLine(cell);
379 pain.line(x, y + tabular.getDescentOfRow(row),
380 x2, y + tabular.getDescentOfRow(row),
381 on_off ? LColor::tabularonoffline : LColor::tabularline,
382 on_off ? Painter::line_onoffdash : Painter::line_solid);
383 if (!tabular.leftAlreadyDrawn(cell)) {
384 on_off = !tabular.leftLine(cell);
385 pain.line(x, y - tabular.getAscentOfRow(row),
386 x, y + tabular.getDescentOfRow(row),
387 on_off ? LColor::tabularonoffline : LColor::tabularline,
388 on_off ? Painter::line_onoffdash : Painter::line_solid);
390 on_off = !tabular.rightLine(cell);
391 pain.line(x2 - tabular.getAdditionalWidth(cell),
392 y - tabular.getAscentOfRow(row),
393 x2 - tabular.getAdditionalWidth(cell),
394 y + tabular.getDescentOfRow(row),
395 on_off ? LColor::tabularonoffline : LColor::tabularline,
396 on_off ? Painter::line_onoffdash : Painter::line_solid);
400 string const InsetTabular::editMessage() const
402 return _("Opened table");
406 void InsetTabular::edit(LCursor & cur, bool left)
408 lyxerr << "InsetTabular::edit: " << this << endl;
410 cur.selection() = false;
413 if (isRightToLeft(cur))
414 cur.idx() = tabular.getLastCellInRow(0);
420 if (isRightToLeft(cur))
421 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
423 cur.idx() = tabular.getNumberOfCells() - 1;
425 cur.pos() = cur.lastpos(); // FIXME crude guess
427 // this accesses the position cache before it is initialized
429 //cur.bv().fitCursor();
433 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
435 //lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
436 //lyxerr << " cur:\n" << cur << endl;
437 CursorSlice sl = cur.top();
438 LCursor & bvcur = cur.bv().cursor();
440 switch (cmd.action) {
442 case LFUN_MOUSE_PRESS:
443 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
445 if (cmd.button() == mouse_button::button1) {
446 cur.selection() = false;
447 setCursorFromCoordinates(cur, cmd.x, cmd.y);
453 if (cmd.button() == mouse_button::button2) {
454 // FIXME: pasting multiple cells (with insettabular's own
455 // LFUN_PASTESELECTION still does not work! (jspitzm)
456 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
457 cell(cur.idx())->dispatch(cur, cmd);
461 // we'll pop up the table dialog on release
462 if (cmd.button() == mouse_button::button3)
466 case LFUN_MOUSE_MOTION:
467 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
468 if (cmd.button() == mouse_button::button1) {
469 // only accept motions to places not deeper nested than the real anchor
470 if (bvcur.anchor_.hasPart(cur)) {
471 setCursorFromCoordinates(cur, cmd.x, cmd.y);
472 bvcur.setCursor(cur);
473 bvcur.selection() = true;
479 case LFUN_MOUSE_RELEASE:
480 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
481 if (cmd.button() == mouse_button::button3)
482 InsetTabularMailer(*this).showDialog(&cur.bv());
485 case LFUN_CELL_BACKWARD:
487 cur.selection() = false;
490 case LFUN_CELL_FORWARD:
492 cur.selection() = false;
497 cell(cur.idx())->dispatch(cur, cmd);
498 cur.dispatched(); // override the cell's decision
500 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
501 if (sl == cur.top()) {
502 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
510 cell(cur.idx())->dispatch(cur, cmd);
511 cur.dispatched(); // override the cell's decision
513 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
514 if (sl == cur.top()) {
515 cmd = FuncRequest(LFUN_FINISHED_LEFT);
523 cell(cur.idx())->dispatch(cur, cmd);
524 cur.dispatched(); // override the cell's decision
526 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
527 cur.idx() = tabular.getCellBelow(cur.idx());
532 if (sl == cur.top()) {
533 cmd = FuncRequest(LFUN_FINISHED_DOWN);
541 cell(cur.idx())->dispatch(cur, cmd);
542 cur.dispatched(); // override the cell's decision
544 if (tabular.row_of_cell(cur.idx()) != 0) {
545 cur.idx() = tabular.getCellAbove(cur.idx());
546 cur.pit() = cur.lastpit();
547 cur.pos() = cur.lastpos();
550 if (sl == cur.top()) {
551 cmd = FuncRequest(LFUN_FINISHED_UP);
558 // //if (hasSelection())
559 // // cur.selection() = false;
560 // col_type const col = tabular.column_of_cell(cur.idx());
561 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
562 // if (t < yo() + tabular.getHeightOfTabular()) {
563 // cur.bv().scrollDocView(t);
564 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
566 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
574 // case LFUN_PRIOR: {
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();
580 // cur.bv().scrollDocView(t);
584 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
588 // cur.par() = cur.lastpar();
589 // cur.pos() = cur.lastpos();
594 case LFUN_LAYOUT_TABULAR:
595 InsetTabularMailer(*this).showDialog(&cur.bv());
598 case LFUN_INSET_DIALOG_UPDATE:
599 InsetTabularMailer(*this).updateDialog(&cur.bv());
602 case LFUN_TABULAR_FEATURE:
603 if (!tabularFeatures(cur, cmd.argument))
607 // insert file functions
608 case LFUN_FILE_INSERT_ASCII_PARA:
609 case LFUN_FILE_INSERT_ASCII: {
610 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
611 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
617 if (tablemode(cur)) {
618 if (copySelection(cur)) {
619 recordUndo(cur, Undo::DELETE);
624 cell(cur.idx())->dispatch(cur, cmd);
629 recordUndo(cur, Undo::DELETE);
633 cell(cur.idx())->dispatch(cur, cmd);
637 if (!cur.selection())
639 if (tablemode(cur)) {
643 cell(cur.idx())->dispatch(cur, cmd);
646 case LFUN_PASTESELECTION: {
647 string const clip = cur.bv().getClipboard();
650 if (clip.find('\t') != string::npos) {
653 col_type maxCols = 1;
654 size_t len = clip.length();
655 for (size_t p = 0; p < len; ++p) {
656 p = clip.find_first_of("\t\n", p);
657 if (p == string::npos)
666 maxCols = max(cols, maxCols);
671 maxCols = max(cols, maxCols);
674 new LyXTabular(cur.buffer().params(), rows, maxCols));
676 string::size_type op = 0;
678 idx_type const cells =
679 paste_tabular->getNumberOfCells();
682 for (size_t p = 0; cell < cells && p < len; ++p) {
683 p = clip.find_first_of("\t\n", p);
684 if (p == string::npos || p >= len)
688 paste_tabular->getCellInset(cell)->
689 setText(clip.substr(op, p - op), font);
694 paste_tabular->getCellInset(cell)->
695 setText(clip.substr(op, p - op), font);
696 while (cols++ < maxCols)
703 // check for the last cell if there is no trailing '\n'
704 if (cell < cells && op < len)
705 paste_tabular->getCellInset(cell)->
706 setText(clip.substr(op, len - op), font);
707 } else if (!insertAsciiString(cur.bv(), clip, true)) {
708 // so that the clipboard is used and it goes on
710 // and executes LFUN_PASTESELECTION in insettext!
711 paste_tabular.reset();
717 if (hasPasteBuffer()) {
718 recordUndo(cur, Undo::INSERT);
722 cell(cur.idx())->dispatch(cur, cmd);
733 case LFUN_FREEFONT_APPLY:
734 case LFUN_FREEFONT_UPDATE:
738 case LFUN_CAPITALIZE_WORD:
739 case LFUN_UPCASE_WORD:
740 case LFUN_LOWCASE_WORD:
741 case LFUN_TRANSPOSE_CHARS:
742 if (tablemode(cur)) {
745 getSelection(cur, rs, re, cs, ce);
746 for (row_type i = rs; i <= re; ++i) {
747 for (col_type j = cs; j <= ce; ++j) {
748 // cursor follows cell:
749 cur.idx() = tabular.getCellNumber(i, j);
750 // select this cell only:
753 cur.pos() = cur.top().lastpos();
756 cell(cur.idx())->dispatch(cur, cmd);
759 // Restore original selection
760 cur.idx() = tabular.getCellNumber(rs, cs);
763 cur.idx() = tabular.getCellNumber(re, ce);
764 cur.pos() = cur.top().lastpos();
769 cell(cur.idx())->dispatch(cur, cmd);
773 // we try to handle this event in the insets dispatch function.
774 cell(cur.idx())->dispatch(cur, cmd);
779 InsetTabularMailer(*this).updateDialog(&cur.bv());
783 // function sets an object as defined in func_status.h:
784 // states OK, Unknown, Disabled, On, Off.
785 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
786 FuncStatus & status) const
788 switch (cmd.action) {
789 case LFUN_TABULAR_FEATURE: {
790 int action = LyXTabular::LAST_ACTION;
792 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
793 string const tmp = tabularFeature[i].feature;
794 if (tmp == cmd.argument.substr(0, tmp.length())) {
795 action = tabularFeature[i].action;
799 if (action == LyXTabular::LAST_ACTION) {
801 status.unknown(true);
805 string const argument
806 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
808 row_type sel_row_start = 0;
809 row_type sel_row_end = 0;
811 LyXTabular::ltType dummyltt;
814 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
817 case LyXTabular::SET_PWIDTH:
818 case LyXTabular::SET_MPWIDTH:
819 case LyXTabular::SET_SPECIAL_COLUMN:
820 case LyXTabular::SET_SPECIAL_MULTI:
821 case LyXTabular::APPEND_ROW:
822 case LyXTabular::APPEND_COLUMN:
823 case LyXTabular::DELETE_ROW:
824 case LyXTabular::DELETE_COLUMN:
825 case LyXTabular::SET_ALL_LINES:
826 case LyXTabular::UNSET_ALL_LINES:
830 case LyXTabular::MULTICOLUMN:
831 status.setOnOff(tabular.isMultiColumn(cur.idx()));
834 case LyXTabular::M_TOGGLE_LINE_TOP:
836 case LyXTabular::TOGGLE_LINE_TOP:
837 status.setOnOff(tabular.topLine(cur.idx(), flag));
840 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
842 case LyXTabular::TOGGLE_LINE_BOTTOM:
843 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
846 case LyXTabular::M_TOGGLE_LINE_LEFT:
848 case LyXTabular::TOGGLE_LINE_LEFT:
849 status.setOnOff(tabular.leftLine(cur.idx(), flag));
852 case LyXTabular::M_TOGGLE_LINE_RIGHT:
854 case LyXTabular::TOGGLE_LINE_RIGHT:
855 status.setOnOff(tabular.rightLine(cur.idx(), flag));
858 case LyXTabular::M_ALIGN_LEFT:
860 case LyXTabular::ALIGN_LEFT:
861 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
864 case LyXTabular::M_ALIGN_RIGHT:
866 case LyXTabular::ALIGN_RIGHT:
867 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
870 case LyXTabular::M_ALIGN_CENTER:
872 case LyXTabular::ALIGN_CENTER:
873 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
876 case LyXTabular::ALIGN_BLOCK:
877 status.enabled(!tabular.getPWidth(cur.idx()).zero());
878 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
881 case LyXTabular::M_VALIGN_TOP:
883 case LyXTabular::VALIGN_TOP:
885 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
888 case LyXTabular::M_VALIGN_BOTTOM:
890 case LyXTabular::VALIGN_BOTTOM:
892 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
895 case LyXTabular::M_VALIGN_MIDDLE:
897 case LyXTabular::VALIGN_MIDDLE:
899 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
902 case LyXTabular::SET_LONGTABULAR:
903 status.setOnOff(tabular.isLongTabular());
906 case LyXTabular::UNSET_LONGTABULAR:
907 status.setOnOff(!tabular.isLongTabular());
910 case LyXTabular::SET_ROTATE_TABULAR:
911 status.setOnOff(tabular.getRotateTabular());
914 case LyXTabular::UNSET_ROTATE_TABULAR:
915 status.setOnOff(!tabular.getRotateTabular());
918 case LyXTabular::SET_ROTATE_CELL:
919 status.setOnOff(tabular.getRotateCell(cur.idx()));
922 case LyXTabular::UNSET_ROTATE_CELL:
923 status.setOnOff(!tabular.getRotateCell(cur.idx()));
926 case LyXTabular::SET_USEBOX:
927 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
930 case LyXTabular::SET_LTFIRSTHEAD:
931 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
934 case LyXTabular::UNSET_LTFIRSTHEAD:
935 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
938 case LyXTabular::SET_LTHEAD:
939 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
942 case LyXTabular::UNSET_LTHEAD:
943 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
946 case LyXTabular::SET_LTFOOT:
947 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
950 case LyXTabular::UNSET_LTFOOT:
951 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
954 case LyXTabular::SET_LTLASTFOOT:
955 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
958 case LyXTabular::UNSET_LTLASTFOOT:
959 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
962 case LyXTabular::SET_LTNEWPAGE:
963 status.setOnOff(tabular.getLTNewPage(sel_row_start));
968 status.enabled(false);
974 // These are only enabled inside tabular
975 case LFUN_CELL_BACKWARD:
976 case LFUN_CELL_FORWARD:
977 status.enabled(true);
980 // disable these with multiple cells selected
981 case LFUN_INSET_INSERT:
982 case LFUN_TABULAR_INSERT:
983 case LFUN_INSERT_CHARSTYLE:
984 case LFUN_INSET_FLOAT:
985 case LFUN_INSET_WIDE_FLOAT:
986 case LFUN_INSET_FOOTNOTE:
987 case LFUN_INSET_MARGINAL:
988 case LFUN_INSERT_MATH:
990 case LFUN_MATH_MUTATE:
991 case LFUN_MATH_DISPLAY:
992 case LFUN_INSERT_NOTE:
993 case LFUN_INSET_OPTARG:
994 case LFUN_INSERT_BOX:
995 case LFUN_INSERT_BRANCH:
996 case LFUN_INSET_WRAP:
997 case LFUN_INSET_ERT: {
998 if (tablemode(cur)) {
999 status.enabled(false);
1004 case LFUN_INSET_MODIFY:
1005 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1006 status.enabled(true);
1012 // we try to handle this event in the insets dispatch function.
1013 return cell(cur.idx())->getStatus(cur, cmd, status);
1018 int InsetTabular::latex(Buffer const & buf, ostream & os,
1019 OutputParams const & runparams) const
1021 return tabular.latex(buf, os, runparams);
1025 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1026 OutputParams const & runparams) const
1028 int const dp = runparams.linelen ? runparams.depth : 0;
1029 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1033 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1034 OutputParams const & runparams) const
1036 return tabular.linuxdoc(buf,os, runparams);
1040 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1041 OutputParams const & runparams) const
1044 InsetBase * master = 0;
1046 #ifdef WITH_WARNINGS
1047 #warning Why not pass a proper DocIterator here?
1050 // if the table is inside a float it doesn't need the informaltable
1051 // wrapper. Search for it.
1052 for (master = owner(); master; master = master->owner())
1053 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1058 os << "<informaltable>";
1061 ret += tabular.docbook(buf, os, runparams);
1063 os << "</informaltable>";
1070 void InsetTabular::validate(LaTeXFeatures & features) const
1072 tabular.validate(features);
1076 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1078 return tabular.getCellInset(idx);
1082 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1084 return tabular.getCellInset(idx);
1088 void InsetTabular::cursorPos
1089 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1091 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1093 // y offset correction
1094 int const row = tabular.row_of_cell(sl.idx());
1095 for (int i = 0; i <= row; ++i) {
1097 y += tabular.getAscentOfRow(i);
1098 y += tabular.getAdditionalHeight(i);
1101 y += tabular.getDescentOfRow(i);
1104 // x offset correction
1105 int const col = tabular.column_of_cell(sl.idx());
1106 int idx = tabular.getCellNumber(row, 0);
1107 for (int j = 0; j < col; ++j) {
1108 if (tabular.isPartOfMultiColumn(row, j))
1110 x += tabular.getWidthOfColumn(idx);
1113 x += tabular.getBeginningOfTextInCell(idx);
1114 x += ADD_TO_TABULAR_WIDTH;
1119 int InsetTabular::dist(idx_type const cell, int x, int y) const
1123 InsetBase const & inset = *tabular.getCellInset(cell);
1124 Point o = theCoords.getInsets().xy(&inset);
1125 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1126 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1127 int const ybeg = o.y_ - inset.ascent();
1128 row_type const row = tabular.row_of_cell(cell);
1129 int const rowheight = tabular.getAscentOfRow(row)
1130 + tabular.getDescentOfRow(row)
1131 + tabular.getAdditionalHeight(row);
1132 int const yend = ybeg + rowheight;
1144 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1145 // << " ybeg=" << ybeg << " yend=" << yend
1146 // << " xx=" << xx << " yy=" << yy
1147 // << " dist=" << xx + yy << endl;
1152 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1154 //lyxerr << "InsetTabular::editXY: " << this << endl;
1155 cur.selection() = false;
1157 cur.idx() = getNearestCell(x, y);
1159 return cell(cur.idx())->text_.editXY(cur, x, y);
1163 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1165 cur.idx() = getNearestCell(x, y);
1166 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1171 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1173 idx_type idx_min = 0;
1174 int dist_min = std::numeric_limits<int>::max();
1175 for (idx_type i = 0; i < nargs(); ++i) {
1176 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1177 int const d = dist(i, x, y);
1188 int InsetTabular::getCellXPos(idx_type const cell) const
1192 for (; !tabular.isFirstCellInRow(c); --c)
1194 int lx = tabular.getWidthOfColumn(cell);
1195 for (; c < cell; ++c)
1196 lx += tabular.getWidthOfColumn(c);
1198 return lx - tabular.getWidthOfColumn(cell);
1202 void InsetTabular::resetPos(LCursor & cur) const
1204 BufferView & bv = cur.bv();
1205 int const maxwidth = bv.workWidth();
1207 if (&cur.inset() != this) {
1211 int const X2 = maxwidth;
1212 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1213 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1214 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1217 scx_ = X1 + 20 - x1;
1219 scx_ = X2 - 20 - x2;
1226 InsetTabularMailer(*this).updateDialog(&bv);
1230 void InsetTabular::moveNextCell(LCursor & cur)
1232 if (isRightToLeft(cur)) {
1233 if (tabular.isFirstCellInRow(cur.idx())) {
1234 row_type const row = tabular.row_of_cell(cur.idx());
1235 if (row == tabular.rows() - 1)
1237 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1244 if (tabular.isLastCell(cur.idx()))
1254 void InsetTabular::movePrevCell(LCursor & cur)
1256 if (isRightToLeft(cur)) {
1257 if (tabular.isLastCellInRow(cur.idx())) {
1258 row_type const row = tabular.row_of_cell(cur.idx());
1261 cur.idx() = tabular.getFirstCellInRow(row);
1262 cur.idx() = tabular.getCellAbove(cur.idx());
1264 if (tabular.isLastCell(cur.idx()))
1269 if (cur.idx() == 0) // first cell
1273 cur.pit() = cur.lastpit();
1274 cur.pos() = cur.lastpos();
1279 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1281 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1284 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1285 string const tmp = tabularFeature[i].feature;
1287 if (tmp == what.substr(0, tmp.length())) {
1288 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1289 //tabularFeatures[i].feature.length())) {
1290 action = tabularFeature[i].action;
1294 if (action == LyXTabular::LAST_ACTION)
1298 ltrim(what.substr(tabularFeature[i].feature.length()));
1299 tabularFeatures(cur, action, val);
1306 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1307 string const & special, bool & flag)
1309 if (special == "dl_above") {
1312 } else if (special == "dl_below") {
1313 ltt.bottomDL = flag;
1315 } else if (special == "empty") {
1327 void InsetTabular::tabularFeatures(LCursor & cur,
1328 LyXTabular::Feature feature, string const & value)
1330 BufferView & bv = cur.bv();
1331 col_type sel_col_start;
1332 col_type sel_col_end;
1333 row_type sel_row_start;
1334 row_type sel_row_end;
1335 bool setLines = false;
1336 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1337 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1341 case LyXTabular::M_ALIGN_LEFT:
1342 case LyXTabular::ALIGN_LEFT:
1343 setAlign = LYX_ALIGN_LEFT;
1346 case LyXTabular::M_ALIGN_RIGHT:
1347 case LyXTabular::ALIGN_RIGHT:
1348 setAlign = LYX_ALIGN_RIGHT;
1351 case LyXTabular::M_ALIGN_CENTER:
1352 case LyXTabular::ALIGN_CENTER:
1353 setAlign = LYX_ALIGN_CENTER;
1356 case LyXTabular::ALIGN_BLOCK:
1357 setAlign = LYX_ALIGN_BLOCK;
1360 case LyXTabular::M_VALIGN_TOP:
1361 case LyXTabular::VALIGN_TOP:
1362 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1365 case LyXTabular::M_VALIGN_BOTTOM:
1366 case LyXTabular::VALIGN_BOTTOM:
1367 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1370 case LyXTabular::M_VALIGN_MIDDLE:
1371 case LyXTabular::VALIGN_MIDDLE:
1372 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1379 recordUndo(cur, Undo::ATOMIC);
1381 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1382 row_type const row = tabular.row_of_cell(cur.idx());
1383 col_type const column = tabular.column_of_cell(cur.idx());
1385 LyXTabular::ltType ltt;
1389 case LyXTabular::SET_PWIDTH: {
1390 LyXLength const len(value);
1391 tabular.setColumnPWidth(cur.idx(), len);
1393 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1394 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1395 else if (!len.zero()
1396 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1397 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1401 case LyXTabular::SET_MPWIDTH:
1402 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1405 case LyXTabular::SET_SPECIAL_COLUMN:
1406 case LyXTabular::SET_SPECIAL_MULTI:
1407 tabular.setAlignSpecial(cur.idx(),value,feature);
1410 case LyXTabular::APPEND_ROW:
1411 // append the row into the tabular
1412 tabular.appendRow(bv.buffer()->params(), cur.idx());
1415 case LyXTabular::APPEND_COLUMN:
1416 // append the column into the tabular
1417 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1418 cur.idx() = tabular.getCellNumber(row, column);
1421 case LyXTabular::DELETE_ROW:
1422 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1423 tabular.deleteRow(sel_row_start);
1424 if (sel_row_start >= tabular.rows())
1426 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1429 cur.selection() = false;
1432 case LyXTabular::DELETE_COLUMN:
1433 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1434 tabular.deleteColumn(sel_col_start);
1435 if (sel_col_start >= tabular.columns())
1437 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1440 cur.selection() = false;
1443 case LyXTabular::M_TOGGLE_LINE_TOP:
1445 case LyXTabular::TOGGLE_LINE_TOP: {
1446 bool lineSet = !tabular.topLine(cur.idx(), flag);
1447 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1448 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1450 tabular.getCellNumber(i, j),
1455 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1457 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1458 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1459 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1460 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1461 tabular.setBottomLine(
1462 tabular.getCellNumber(i, j),
1468 case LyXTabular::M_TOGGLE_LINE_LEFT:
1470 case LyXTabular::TOGGLE_LINE_LEFT: {
1471 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1472 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1473 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1474 tabular.setLeftLine(
1475 tabular.getCellNumber(i,j),
1481 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1483 case LyXTabular::TOGGLE_LINE_RIGHT: {
1484 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1485 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1486 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1487 tabular.setRightLine(
1488 tabular.getCellNumber(i,j),
1494 case LyXTabular::M_ALIGN_LEFT:
1495 case LyXTabular::M_ALIGN_RIGHT:
1496 case LyXTabular::M_ALIGN_CENTER:
1498 case LyXTabular::ALIGN_LEFT:
1499 case LyXTabular::ALIGN_RIGHT:
1500 case LyXTabular::ALIGN_CENTER:
1501 case LyXTabular::ALIGN_BLOCK:
1502 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1503 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1504 tabular.setAlignment(
1505 tabular.getCellNumber(i, j),
1510 case LyXTabular::M_VALIGN_TOP:
1511 case LyXTabular::M_VALIGN_BOTTOM:
1512 case LyXTabular::M_VALIGN_MIDDLE:
1514 case LyXTabular::VALIGN_TOP:
1515 case LyXTabular::VALIGN_BOTTOM:
1516 case LyXTabular::VALIGN_MIDDLE:
1517 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1518 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1519 tabular.setVAlignment(
1520 tabular.getCellNumber(i, j),
1524 case LyXTabular::MULTICOLUMN: {
1525 if (sel_row_start != sel_row_end) {
1526 #ifdef WITH_WARNINGS
1527 #warning Need I say it ? This is horrible.
1529 Alert::error(_("Error setting multicolumn"),
1530 _("You cannot set multicolumn vertically."));
1533 if (!cur.selection()) {
1534 // just multicol for one single cell
1535 // check whether we are completely in a multicol
1536 if (tabular.isMultiColumn(cur.idx()))
1537 tabular.unsetMultiColumn(cur.idx());
1539 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1542 // we have a selection so this means we just add all this
1543 // cells to form a multicolumn cell
1544 idx_type const s_start = cur.selBegin().idx();
1545 idx_type const s_end = cur.selEnd().idx();
1546 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1547 cur.idx() = s_start;
1550 cur.selection() = false;
1554 case LyXTabular::SET_ALL_LINES:
1556 case LyXTabular::UNSET_ALL_LINES:
1557 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1558 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1559 tabular.setAllLines(
1560 tabular.getCellNumber(i,j), setLines);
1563 case LyXTabular::SET_LONGTABULAR:
1564 tabular.setLongTabular(true);
1567 case LyXTabular::UNSET_LONGTABULAR:
1568 tabular.setLongTabular(false);
1571 case LyXTabular::SET_ROTATE_TABULAR:
1572 tabular.setRotateTabular(true);
1575 case LyXTabular::UNSET_ROTATE_TABULAR:
1576 tabular.setRotateTabular(false);
1579 case LyXTabular::SET_ROTATE_CELL:
1580 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1581 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1582 tabular.setRotateCell(
1583 tabular.getCellNumber(i, j), true);
1586 case LyXTabular::UNSET_ROTATE_CELL:
1587 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1588 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1589 tabular.setRotateCell(
1590 tabular.getCellNumber(i, j), false);
1593 case LyXTabular::SET_USEBOX: {
1594 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1595 if (val == tabular.getUsebox(cur.idx()))
1596 val = LyXTabular::BOX_NONE;
1597 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1598 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1599 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1603 case LyXTabular::UNSET_LTFIRSTHEAD:
1605 case LyXTabular::SET_LTFIRSTHEAD:
1606 tabular.getRowOfLTFirstHead(row, ltt);
1607 checkLongtableSpecial(ltt, value, flag);
1608 tabular.setLTHead(row, flag, ltt, true);
1611 case LyXTabular::UNSET_LTHEAD:
1613 case LyXTabular::SET_LTHEAD:
1614 tabular.getRowOfLTHead(row, ltt);
1615 checkLongtableSpecial(ltt, value, flag);
1616 tabular.setLTHead(row, flag, ltt, false);
1619 case LyXTabular::UNSET_LTFOOT:
1621 case LyXTabular::SET_LTFOOT:
1622 tabular.getRowOfLTFoot(row, ltt);
1623 checkLongtableSpecial(ltt, value, flag);
1624 tabular.setLTFoot(row, flag, ltt, false);
1627 case LyXTabular::UNSET_LTLASTFOOT:
1629 case LyXTabular::SET_LTLASTFOOT:
1630 tabular.getRowOfLTLastFoot(row, ltt);
1631 checkLongtableSpecial(ltt, value, flag);
1632 tabular.setLTFoot(row, flag, ltt, true);
1635 case LyXTabular::SET_LTNEWPAGE:
1636 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1639 // dummy stuff just to avoid warnings
1640 case LyXTabular::LAST_ACTION:
1644 InsetTabularMailer(*this).updateDialog(&bv);
1648 bool InsetTabular::showInsetDialog(BufferView * bv) const
1650 InsetTabularMailer(*this).showDialog(bv);
1655 void InsetTabular::openLayoutDialog(BufferView * bv) const
1657 InsetTabularMailer(*this).showDialog(bv);
1661 void InsetTabular::getLabelList(Buffer const & buffer,
1662 vector<string> & list) const
1664 tabular.getLabelList(buffer, list);
1668 bool InsetTabular::copySelection(LCursor & cur)
1670 if (!cur.selection())
1675 getSelection(cur, rs, re, cs, ce);
1677 paste_tabular.reset(new LyXTabular(tabular));
1679 for (row_type i = 0; i < rs; ++i)
1680 paste_tabular->deleteRow(0);
1682 row_type const rows = re - rs + 1;
1683 while (paste_tabular->rows() > rows)
1684 paste_tabular->deleteRow(rows);
1686 paste_tabular->setTopLine(0, true, true);
1687 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1690 for (col_type i = 0; i < cs; ++i)
1691 paste_tabular->deleteColumn(0);
1693 col_type const columns = ce - cs + 1;
1694 while (paste_tabular->columns() > columns)
1695 paste_tabular->deleteColumn(columns);
1697 paste_tabular->setLeftLine(0, true, true);
1698 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1702 OutputParams const runparams;
1703 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1704 cur.bv().stuffClipboard(os.str());
1709 bool InsetTabular::pasteSelection(LCursor & cur)
1713 col_type const actcol = tabular.column_of_cell(cur.idx());
1714 row_type const actrow = tabular.row_of_cell(cur.idx());
1715 for (row_type r1 = 0, r2 = actrow;
1716 r1 < paste_tabular->rows() && r2 < tabular.rows();
1718 for (col_type c1 = 0, c2 = actcol;
1719 c1 < paste_tabular->columns() && c2 < tabular.columns();
1721 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1722 tabular.isPartOfMultiColumn(r2, c2))
1724 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1728 if (tabular.isPartOfMultiColumn(r2, c2)) {
1732 shared_ptr<InsetText> inset(
1733 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1734 tabular.setCellInset(r2, c2, inset);
1743 void InsetTabular::cutSelection(LCursor & cur)
1745 if (!cur.selection())
1748 bool const track = cur.buffer().params().tracking_changes;
1751 getSelection(cur, rs, re, cs, ce);
1752 for (row_type i = rs; i <= re; ++i)
1753 for (col_type j = cs; j <= ce; ++j)
1754 cell(tabular.getCellNumber(i, j))->clear(track);
1756 // cursor position might be invalid now
1757 cur.pos() = cur.lastpos();
1758 cur.clearSelection();
1762 bool InsetTabular::isRightToLeft(LCursor & cur) const
1764 BOOST_ASSERT(cur.depth() > 1);
1765 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1766 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1767 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1768 parentpos).language()->RightToLeft();
1772 void InsetTabular::getSelection(LCursor & cur,
1773 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1775 CursorSlice const & beg = cur.selBegin();
1776 CursorSlice const & end = cur.selEnd();
1777 cs = tabular.column_of_cell(beg.idx());
1778 ce = tabular.column_of_cell(end.idx());
1781 cs = tabular.column_of_cell(end.idx());
1783 ce = tabular.right_column_of_cell(end.idx());
1786 rs = tabular.row_of_cell(beg.idx());
1787 re = tabular.row_of_cell(end.idx());
1793 size_t InsetTabular::nargs() const
1795 return tabular.getNumberOfCells();
1799 LyXText * InsetTabular::getText(int idx) const
1801 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1805 void InsetTabular::markErased()
1807 for (idx_type idx = 0; idx < nargs(); ++idx)
1808 cell(idx)->markErased();
1812 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1815 idx_type const cell = tabular.getCellFromInset(in);
1816 // FIXME: getCellFromInset() returns now always a valid cell, so
1817 // the stuff below can be deleted, and instead we have:
1818 return tabular.getPWidth(cell).zero();
1821 return tabular.getPWidth(cell).zero();
1823 // this is a workaround for a crash (New, Insert->Tabular,
1824 // Insert->FootNote)
1828 // well we didn't obviously find it so maybe our owner knows more
1829 BOOST_ASSERT(owner());
1830 return owner()->forceDefaultParagraphs(in);
1836 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1839 if (buf.length() <= 0)
1844 col_type maxCols = 1;
1845 string::size_type const len = buf.length();
1846 string::size_type p = 0;
1848 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1856 maxCols = max(cols, maxCols);
1862 maxCols = max(cols, maxCols);
1863 LyXTabular * loctab;
1868 paste_tabular.reset(
1869 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1870 loctab = paste_tabular.get();
1874 cell = bv.cursor().idx();
1875 ocol = tabular.column_of_cell(cell);
1876 row = tabular.row_of_cell(cell);
1879 string::size_type op = 0;
1880 idx_type const cells = loctab->getNumberOfCells();
1883 rows = loctab->rows();
1884 col_type const columns = loctab->columns();
1886 while (cell < cells && p < len && row < rows &&
1887 (p = buf.find_first_of("\t\n", p)) != string::npos)
1893 // we can only set this if we are not too far right
1894 if (cols < columns) {
1895 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1896 Paragraph & par = inset->text_.getPar(0);
1897 LyXFont const font = inset->text_.getFont(par, 0);
1898 inset->setText(buf.substr(op, p - op), font);
1904 // we can only set this if we are not too far right
1905 if (cols < columns) {
1906 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1907 Paragraph & par = inset->text_.getPar(0);
1908 LyXFont const font = inset->text_.getFont(par, 0);
1909 inset->setText(buf.substr(op, p - op), font);
1914 cell = loctab->getCellNumber(row, cols);
1920 // check for the last cell if there is no trailing '\n'
1921 if (cell < cells && op < len) {
1922 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1923 Paragraph & par = inset->text_.getPar(0);
1924 LyXFont const font = inset->text_.getFont(par, 0);
1925 inset->setText(buf.substr(op, len - op), font);
1931 void InsetTabular::addPreview(PreviewLoader & loader) const
1933 row_type const rows = tabular.rows();
1934 col_type const columns = tabular.columns();
1935 for (row_type i = 0; i < rows; ++i) {
1936 for (col_type j = 0; j < columns; ++j)
1937 tabular.getCellInset(i, j)->addPreview(loader);
1942 bool InsetTabular::tablemode(LCursor & cur) const
1944 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1951 string const InsetTabularMailer::name_("tabular");
1953 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1954 : inset_(const_cast<InsetTabular &>(inset))
1958 string const InsetTabularMailer::inset2string(Buffer const &) const
1960 return params2string(inset_);
1964 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1966 istringstream data(in);
1968 lex.setStream(data);
1975 if (!lex || token != name_)
1976 return print_mailer_error("InsetTabularMailer", in, 1,
1979 // This is part of the inset proper that is usually swallowed
1980 // by Buffer::readInset
1982 if (!lex || token != "Tabular")
1983 return print_mailer_error("InsetTabularMailer", in, 2,
1986 Buffer const & buffer = inset.buffer();
1987 inset.read(buffer, lex);
1991 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1994 data << name_ << ' ';
1995 inset.write(inset.buffer(), data);
1996 data << "\\end_inset\n";